This commit is contained in:
Peter Li 2025-04-11 18:00:58 -07:00
parent 8c59117ccd
commit 22d3322867
3 changed files with 58 additions and 8 deletions

View File

@ -9,6 +9,13 @@ struct Output
float4 Position : SV_Position;
};
struct Scene
{
float3 color;
};
StructuredBuffer<Scene> test: register(t0, space0);
Output main(Input input)
{
Output output;
@ -16,21 +23,21 @@ Output main(Input input)
if (input.VertexIndex == 0)
{
pos = (-1.0f).xx;
output.Color = float4(1.0f, 0.0f, 0.0f, 1.0f);
output.Color = float4(test[0].color, 1.0f);
}
else
{
if (input.VertexIndex == 1)
{
pos = float2(1.0f, -1.0f);
output.Color = float4(0.0f, 1.0f, 0.0f, 1.0f);
output.Color = float4(test[1].color, 1.0f);
}
else
{
if (input.VertexIndex == 2)
{
pos = float2(0.0f, 1.0f);
output.Color = float4(0.0f, 0.0f, 1.0f, 1.0f);
output.Color = float4(test[2].color, 1.0f);
}
}
}

View File

@ -838,8 +838,8 @@ pub const GPUDevice = opaque {
}
// SDL_MapGPUTransferBuffer
pub inline fn mapGPUTransferBuffer(device: *GPUDevice, transfer_buffer: *GPUTransferBuffer, cycle: bool) [*c]void {
return @bitCast(c.SDL_MapGPUTransferBuffer(@ptrCast(device), @ptrCast(transfer_buffer), cycle));
pub inline fn mapGPUTransferBuffer(device: *GPUDevice, transfer_buffer: *GPUTransferBuffer, cycle: bool) [*c]u8 {
return @ptrCast(c.SDL_MapGPUTransferBuffer(@ptrCast(device), @ptrCast(transfer_buffer), cycle));
}
// SDL_UnmapGPUTransferBuffer

View File

@ -11,11 +11,15 @@ window: *sdl3.Window = undefined,
device: *gpu.GPUDevice = undefined,
shaderpath: []const u8 = undefined,
shadersuffix: []const u8 = undefined,
entrypoint: []const u8 = "main",
fragment: *gpu.GPUShader = undefined,
vertex: *gpu.GPUShader = undefined,
pipeline: *gpu.GPUGraphicsPipeline = undefined,
shaderformat: gpu.GPUShaderFormat = undefined,
colorBuffer: *gpu.GPUBuffer = undefined,
colorBufferTransfer: *gpu.GPUTransferBuffer = undefined,
pub fn create(allocator: std.mem.Allocator) !*@This() {
const self = try allocator.create(@This());
self.* = .{
@ -52,6 +56,7 @@ pub fn startContext(self: *@This()) !void {
} else if (formats.shaderformatMsl) {
self.shaderpath = "./content/_cooked/msl"; // shaderformatSpirv
self.shadersuffix = ".msl";
self.entrypoint = "main0";
self.shaderformat = .{ .shaderformatMsl = true };
} else if (formats.shaderformatDxil) {
self.shaderpath = "./content/_cooked/dxil"; // shaderformatSpirv
@ -59,8 +64,8 @@ pub fn startContext(self: *@This()) !void {
self.shaderformat = .{ .shaderformatDxil = true };
}
self.vertex = try self.loadShader("hello-triangle.vert", .shaderstageVertex, 0, 0, 1, 0);
self.fragment = try self.loadShader("hello-triangle.frag", .shaderstageFragment, 0, 0, 0, 0);
self.vertex = try self.loadShader("hello-triangle.vert", .shaderstageVertex, 0, 0, 0, 0);
var pci = std.mem.zeroes(gpu.GPUGraphicsPipelineCreateInfo);
pci.fragment_shader = self.fragment;
@ -76,6 +81,44 @@ pub fn startContext(self: *@This()) !void {
pci.rasterizer_state.fill_mode = .fillmodeFill;
self.pipeline = self.device.createGPUGraphicsPipeline(&pci);
self.colorBuffer = self.device.createGPUBuffer(&.{
.usage = .{ .bufferusageVertex = true },
.size = 8192 * 2,
.props = 0,
});
self.colorBufferTransfer = self.device.createGPUTransferBuffer(&.{
.usage = .transferbufferusageUpload,
.size = 8192 * 2,
.props = 0,
});
{
const buffer = self.device.mapGPUTransferBuffer(self.colorBufferTransfer, true);
var b: [*][3]f32 = @ptrCast(@alignCast(buffer));
b[0] = .{ 1.0, 0.0, 0.0 };
b[1] = .{ 1.0, 1.0, 0.0 };
b[2] = .{ 1.0, 0.0, 1.0 };
self.device.unmapGPUTransferBuffer(self.colorBufferTransfer);
}
{
const cmd = self.device.acquireGPUCommandBuffer();
defer _ = cmd.submitGPUCommandBufferAndAcquireFence();
{
const copyPass = cmd.beginGPUCopyPass();
defer copyPass.endGPUCopyPass();
copyPass.uploadToGPUBuffer(&.{ .transfer_buffer = self.colorBufferTransfer, .offset = 0 }, &.{
.buffer = self.colorBuffer,
.offset = 0,
.size = 4 * 12,
}, true);
}
}
}
pub fn loadFileAlloc(filename: []const u8, comptime alignment: usize, allocator: std.mem.Allocator) ![]u8 {
@ -107,7 +150,7 @@ pub fn loadShader(
defer self.allocator.free(fileContents);
const sci = gpu.GPUShaderCreateInfo{
.code = @ptrCast(fileContents.ptr),
.entrypoint = "main",
.entrypoint = @ptrCast(self.entrypoint.ptr),
.format = self.shaderformat,
.code_size = fileContents.len - 1,
.stage = stage,
@ -141,7 +184,6 @@ fn update(self: *@This(), dt: f64) void {
self.draw(dt);
}
std.debug.print("frameTime: {d}ms ({d}fps)\n", .{ dt * 1000, 1 / dt });
}
pub fn draw(self: *@This(), dt: f64) void {
@ -157,6 +199,7 @@ pub fn draw(self: *@This(), dt: f64) void {
const renderpass = cmd.beginGPURenderPass(&targetInfo, 1, null);
renderpass.bindGPUGraphicsPipeline(self.pipeline);
renderpass.bindGPUVertexStorageBuffers(0, &self.colorBuffer, 1);
renderpass.setGPUScissor(&self.scissor);
renderpass.drawGPUPrimitives(3, 1, 0, 0);
renderpass.endGPURenderPass();