Indexed rendering

This commit is contained in:
nefrace 2025-05-09 22:12:20 +03:00
parent 3701ab66dd
commit 607ee77703
1 changed files with 29 additions and 8 deletions

View File

@ -33,11 +33,14 @@ main :: proc() {
{{-0.5, 0, 0}, {1, 0, 0}}, {{-0.5, 0, 0}, {1, 0, 0}},
{{0, 1.0, 0}, {0, 1, 0}}, {{0, 1.0, 0}, {0, 1, 0}},
{{0.5, 0, 0}, {0, 0, 1}}, {{0.5, 0, 0}, {0, 0, 1}},
{{0.5, 0, 0}, {1, 1, 0}},
{{0, -0.5, 0}, {0, 1, 1}}, {{0, -0.5, 0}, {0, 1, 1}},
{{-0.5, -0, 0}, {1, 0, 1}},
} }
vertices_byte_size := len(vertices) * size_of(vertices[0]) vertices_byte_size := len(vertices) * size_of(vertices[0])
indices := []u16{0, 1, 2, 0, 2, 3}
indices_byte_size := len(indices) * size_of(indices[0])
vertex_attrs := []sdl.GPUVertexAttribute { vertex_attrs := []sdl.GPUVertexAttribute {
{location = 0, format = .FLOAT3, offset = 0}, // Position {location = 0, format = .FLOAT3, offset = 0}, // Position
{location = 1, format = .FLOAT3, offset = size_of(Vec3)}, // Color {location = 1, format = .FLOAT3, offset = size_of(Vec3)}, // Color
@ -46,14 +49,21 @@ main :: proc() {
gpu, gpu,
sdl.GPUBufferCreateInfo{usage = {.VERTEX}, size = u32(vertices_byte_size)}, sdl.GPUBufferCreateInfo{usage = {.VERTEX}, size = u32(vertices_byte_size)},
) )
index_buf := sdl.CreateGPUBuffer(
gpu,
sdl.GPUBufferCreateInfo{usage = {.INDEX}, size = u32(indices_byte_size)},
)
transfer_buf := sdl.CreateGPUTransferBuffer( transfer_buf := sdl.CreateGPUTransferBuffer(
gpu, gpu,
sdl.GPUTransferBufferCreateInfo{usage = .UPLOAD, size = u32(vertices_byte_size)}, sdl.GPUTransferBufferCreateInfo {
usage = .UPLOAD,
size = u32(vertices_byte_size + indices_byte_size),
},
) )
{ // Copy verticies to the transfer buffer { // Copy verticies to the transfer buffer
transfer_mem := sdl.MapGPUTransferBuffer( transfer_mem := transmute([^]byte)sdl.MapGPUTransferBuffer(
gpu, gpu,
transfer_buf, transfer_buf,
false, false,
@ -61,6 +71,7 @@ main :: proc() {
defer sdl.UnmapGPUTransferBuffer(gpu, transfer_buf) defer sdl.UnmapGPUTransferBuffer(gpu, transfer_buf)
mem.copy(transfer_mem, raw_data(vertices), vertices_byte_size) mem.copy(transfer_mem, raw_data(vertices), vertices_byte_size)
mem.copy(transfer_mem[vertices_byte_size:], raw_data(indices), indices_byte_size)
} }
{ // Upload buffer to gpu { // Upload buffer to gpu
@ -75,6 +86,12 @@ main :: proc() {
{buffer = vertex_buf, size = u32(vertices_byte_size)}, {buffer = vertex_buf, size = u32(vertices_byte_size)},
false, false,
) )
sdl.UploadToGPUBuffer(
copy_pass,
{transfer_buffer = transfer_buf, offset = u32(vertices_byte_size)},
{buffer = index_buf, size = u32(indices_byte_size)},
false,
)
} }
sdl.ReleaseGPUTransferBuffer(gpu, transfer_buf) sdl.ReleaseGPUTransferBuffer(gpu, transfer_buf)
@ -136,9 +153,6 @@ main :: proc() {
if ev.key.scancode == .ESCAPE do break loop if ev.key.scancode == .ESCAPE do break loop
} }
} }
cmd_buf := sdl.AcquireGPUCommandBuffer(gpu)
swaptex: ^sdl.GPUTexture
ok = sdl.WaitAndAcquireGPUSwapchainTexture(cmd_buf, window, &swaptex, nil, nil);assert(ok)
rotation += 1 * delta rotation += 1 * delta
@ -147,6 +161,11 @@ main :: proc() {
linalg.matrix4_rotate_f32(rotation, {0, 1, 0}) linalg.matrix4_rotate_f32(rotation, {0, 1, 0})
ubo := UBO{proj * model_mat} ubo := UBO{proj * model_mat}
cmd_buf := sdl.AcquireGPUCommandBuffer(gpu)
swaptex: ^sdl.GPUTexture
ok = sdl.WaitAndAcquireGPUSwapchainTexture(cmd_buf, window, &swaptex, nil, nil);assert(ok)
if swaptex != nil { if swaptex != nil {
color_target := sdl.GPUColorTargetInfo { color_target := sdl.GPUColorTargetInfo {
texture = swaptex, texture = swaptex,
@ -163,8 +182,10 @@ main :: proc() {
&(sdl.GPUBufferBinding{buffer = vertex_buf}), &(sdl.GPUBufferBinding{buffer = vertex_buf}),
1, 1,
) )
sdl.BindGPUIndexBuffer(render_pass, {buffer = index_buf}, ._16BIT)
sdl.PushGPUVertexUniformData(cmd_buf, 0, &ubo, size_of(ubo)) sdl.PushGPUVertexUniformData(cmd_buf, 0, &ubo, size_of(ubo))
sdl.DrawGPUPrimitives(render_pass, u32(len(vertices)), 1, 0, 0) // sdl.DrawGPUPrimitives(render_pass, u32(len(vertices)), 1, 0, 0)
sdl.DrawGPUIndexedPrimitives(render_pass, 6, 1, 0, 0, 0)
sdl.EndGPURenderPass(render_pass) sdl.EndGPURenderPass(render_pass)
} }