From 934dd0f13fe092bb1e76ebf87b608ae210de423f Mon Sep 17 00:00:00 2001 From: jacob Date: Sun, 8 Jun 2025 16:15:58 -0500 Subject: [PATCH] use 32 bit indices for mesh buffer --- src/config.h | 2 +- src/draw.c | 28 +++++---------- src/gpu.h | 4 +-- src/gpu_dx11.c | 6 ++-- src/gpu_dx12.c | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 108 insertions(+), 25 deletions(-) diff --git a/src/config.h b/src/config.h index 3b07856b..3496649b 100644 --- a/src/config.h +++ b/src/config.h @@ -80,7 +80,7 @@ -#define DX12_TEST 1 +#define DX12_TEST 0 diff --git a/src/draw.c b/src/draw.c index 59432a44..25527d00 100644 --- a/src/draw.c +++ b/src/draw.c @@ -79,7 +79,7 @@ void draw_poly(struct gpu_handle cmd_list, struct v2_array vertices, u32 color) /* Generate indices in a fan pattern */ struct gpu_indices indices = { .count = num_indices, - .indices = arena_push_array_no_zero(scratch.arena, u16, num_indices) + .indices = arena_push_array_no_zero(scratch.arena, u32, num_indices) }; for (u32 i = 0; i < num_tris; ++i) { u32 tri_offset = i * 3; @@ -100,9 +100,10 @@ void draw_circle(struct gpu_handle cmd_list, struct v2 pos, f32 radius, u32 colo struct v2 *points = arena_push_array_no_zero(scratch.arena, struct v2, detail); for(u32 i = 0; i < detail; ++i) { + f32 angle = ((f32)i / (f32)detail) * TAU; struct v2 p = V2( - radius * math_cos(i * (PI * 2.f) / detail), - radius * math_sin(i * (PI * 2.f) / detail) + radius * math_cos(angle), + radius * math_sin(angle) ); points[i] = v2_add(pos, p); } @@ -118,7 +119,7 @@ void draw_circle(struct gpu_handle cmd_list, struct v2 pos, f32 radius, u32 colo void draw_quad(struct gpu_handle cmd_list, struct quad quad, u32 color) { - LOCAL_PERSIST u16 indices_array[6] = { + LOCAL_PERSIST u32 indices_array[6] = { 0, 1, 2, 0, 2, 3 }; @@ -180,9 +181,10 @@ void draw_circle_line(struct gpu_handle cmd_list, struct v2 pos, f32 radius, f32 struct v2 *points = arena_push_array_no_zero(scratch.arena, struct v2, detail); for (u32 i = 0; i < detail; ++i) { + f32 angle = ((f32)i / (f32)detail) * TAU; struct v2 p = V2( - radius * math_cos(i * (PI * 2.f) / detail), - radius * math_sin(i * (PI * 2.f) / detail) + radius * math_cos(angle), + radius * math_sin(angle) ); points[i] = v2_add(pos, p); } @@ -241,12 +243,8 @@ void draw_arrow_ray(struct gpu_handle cmd_list, struct v2 pos, struct v2 rel, f3 void draw_collider_line(struct gpu_handle cmd_list, struct collider_shape shape, struct xform shape_xf, f32 thickness, u32 color, u32 detail) { - /* FIXME: Graphics bug when lots of colliders are drawn */ - DEBUGBREAKABLE; struct arena_temp scratch = scratch_begin_no_conflict(); - struct v2_array poly = ZI; - if (shape.radius == 0) { poly.count = shape.count; poly.points = arena_push_array_no_zero(scratch.arena, struct v2, shape.count); @@ -258,21 +256,13 @@ void draw_collider_line(struct gpu_handle cmd_list, struct collider_shape shape, poly.count = detail; poly.points = arena_push_array_no_zero(scratch.arena, struct v2, detail); for (u32 i = 0; i < detail; ++i) { - f32 angle = ((f32)i / (f32)detail) * (2 * PI); + f32 angle = ((f32)i / (f32)detail) * TAU; struct v2 dir = V2(math_cos(angle), math_sin(angle)); struct v2 p = collider_get_support_point(&shape, shape_xf, dir).p; poly.points[i] = p; } } - - DEBUGBREAKABLE; -#if 1 draw_poly_line(cmd_list, poly, true, thickness, color); -#else - draw_poly(cmd_list, poly, color); - (UNUSED)thickness; -#endif - scratch_end(scratch); } diff --git a/src/gpu.h b/src/gpu.h index be5ea837..4e867b1b 100644 --- a/src/gpu.h +++ b/src/gpu.h @@ -62,8 +62,8 @@ struct v2i32 gpu_texture_get_size(struct gpu_handle texture); * ========================== */ struct gpu_indices { - u16 count; - u16 *indices; + u32 count; + u32 *indices; }; enum gpu_cmd_kind { diff --git a/src/gpu_dx11.c b/src/gpu_dx11.c index d0c99253..124dd81a 100644 --- a/src/gpu_dx11.c +++ b/src/gpu_dx11.c @@ -1403,7 +1403,7 @@ void gpu_push_cmd(struct gpu_handle gpu_cmd_list, struct gpu_cmd_params params) cmd = arena_push(&list->cpu_cmds_arena, struct dx11_cmd); cmd->kind = params.kind; cmd->mesh.vertex_offset = (list->buffers.mesh.vertex_buffer->cpu_buffer_arena.pos / sizeof(struct dx11_mesh_vertex)); - cmd->mesh.index_offset = (list->buffers.mesh.index_buffer->cpu_buffer_arena.pos / sizeof(u16)); + cmd->mesh.index_offset = (list->buffers.mesh.index_buffer->cpu_buffer_arena.pos / sizeof(u32)); if (list->cpu_last_cmd) { list->cpu_last_cmd->next = cmd; } else { @@ -1425,7 +1425,7 @@ void gpu_push_cmd(struct gpu_handle gpu_cmd_list, struct gpu_cmd_params params) /* Push indices */ u64 index_count = params.mesh.indices.count; - u16 *indices = dx11_buffer_push(list->buffers.mesh.index_buffer, sizeof(u16) * index_count); + u32 *indices = dx11_buffer_push(list->buffers.mesh.index_buffer, sizeof(u32) * index_count); cmd->mesh.index_count += index_count; for (u64 i = 0; i < index_count; ++i) { indices[i] = start_index + params.mesh.indices.indices[i]; @@ -1751,7 +1751,7 @@ void gpu_dispatch(struct gpu_handle gpu_dispatch_state, struct gpu_dispatch_para u32 zero = 0; u32 stride = sizeof(struct dx11_mesh_vertex); ID3D11DeviceContext_IASetVertexBuffers(G.devcon, 0, 1, &vertex_buffer->gpu_buffer, &stride, &zero); - ID3D11DeviceContext_IASetIndexBuffer(G.devcon, index_buffer->gpu_buffer, DXGI_FORMAT_R16_UINT, zero); + ID3D11DeviceContext_IASetIndexBuffer(G.devcon, index_buffer->gpu_buffer, DXGI_FORMAT_R32_UINT, zero); /* Bind RTVs */ ID3D11RenderTargetView *rtvs[] = { final_tex->rtv }; diff --git a/src/gpu_dx12.c b/src/gpu_dx12.c index 3db1840d..db97ecbf 100644 --- a/src/gpu_dx12.c +++ b/src/gpu_dx12.c @@ -1027,6 +1027,99 @@ void gpu_dispatch(struct gpu_handle gpu_dispatch_state, struct gpu_dispatch_para { (UNUSED)gpu_dispatch_state; (UNUSED)params; + + struct pipeline_scope *pipeline_scope = pipeline_scope_begin(); + + /* Texture pass */ + { + struct pipeline *pipeline = dx12_get_pipeline(pipeline_scope, LIT("shaders/texture.hlsl")); + + } + + pipeline_scope_end(pipeline_scope); + +#if 0 + __prof; + struct sprite_scope *sprite_scope = sprite_scope_begin(); + struct dx11_dispatch_state *state = (struct dx11_dispatch_state *)gpu_dispatch_state.v; + + struct rect viewport = params.draw_target_viewport; + + /* Set viewport */ + D3D11_VIEWPORT d3d11_viewport = ZI; + d3d11_viewport.Width = viewport.width; + d3d11_viewport.Height = viewport.height; + d3d11_viewport.MinDepth = 0.0f; + d3d11_viewport.MaxDepth = 1.0f; + d3d11_viewport.TopLeftX = viewport.x; + d3d11_viewport.TopLeftY = viewport.y; + ID3D11DeviceContext_RSSetViewports(G.devcon, 1, &d3d11_viewport); + + struct dx12_buffer *final_tex = (struct dx12_buffer *)params.draw_target.v; + struct v2i32 final_tex_size = final_tex->size; + + /* Texture pass */ + { + __profscope(Texture pass); + struct dx11_shader *shader = &G.shaders[DX11_SHADER_KIND_TEXTURE]; + if (shader->valid) { + struct dx12_buffer *texture = NULL; + if (cmd->texture.texture.v) { + /* Load texture if handle is set */ + texture = (struct dx12_buffer *)cmd->texture.texture.v; + } else if (cmd->texture.sprite.hash) { + /* Otherwise load sprite */ + struct sprite_texture *sprite_texture = sprite_texture_from_tag_async(sprite_scope, cmd->texture.sprite); + if (sprite_texture->loaded) { + texture = (struct dx12_buffer *)sprite_texture->texture.v; + } + } + + if (texture && texture->srv) { + struct dx11_buffer *instance_buffer = list->buffers.texture.instance_buffer; + u32 instance_offset = cmd->texture.instance_offset; + u32 instance_count = cmd->texture.instance_count; + + /* Bind shader */ + ID3D11DeviceContext_VSSetShader(G.devcon, shader->vs, 0, 0); + ID3D11DeviceContext_PSSetShader(G.devcon, shader->ps, 0, 0); + + /* Fill & bind constant buffer */ + { + struct dx11_texture_uniform *uniform = dx11_buffer_push(constant_buffer, sizeof(struct dx11_texture_uniform)); + uniform->vp = vp_matrix; + uniform->instance_offset = instance_offset; + dx11_buffer_submit(constant_buffer); + } + ID3D11DeviceContext_VSSetConstantBuffers(G.devcon, 0, 1, &constant_buffer->gpu_buffer); + ID3D11DeviceContext_PSSetConstantBuffers(G.devcon, 0, 1, &constant_buffer->gpu_buffer); + + /* Bind dummy vertex buffer */ + u32 zero = 0; + ID3D11DeviceContext_IASetVertexBuffers(G.devcon, 0, 1, &G.dummy_vertex_buffer->gpu_buffer, &zero, &zero); + ID3D11DeviceContext_IASetIndexBuffer(G.devcon, G.quad_index_buffer->gpu_buffer, DXGI_FORMAT_R16_UINT, zero); + + /* Bind SRVs */ + ID3D11ShaderResourceView *srvs[] = { instance_buffer->srv, texture->srv }; + ID3D11DeviceContext_VSSetShaderResources(G.devcon, 0, ARRAY_COUNT(srvs), srvs); + ID3D11DeviceContext_PSSetShaderResources(G.devcon, 0, ARRAY_COUNT(srvs), srvs); + + /* Bind RTVs */ + ID3D11RenderTargetView *rtvs[] = { final_tex->rtv }; + ID3D11DeviceContext_OMSetRenderTargets(G.devcon, ARRAY_COUNT(rtvs), rtvs, NULL); + + /* Draw */ + ID3D11DeviceContext_IASetPrimitiveTopology(G.devcon, D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); + ID3D11DeviceContext_DrawIndexedInstanced(G.devcon, 6, instance_count, 0, 0, 0); + + /* Unbind */ + dx11_unbind(DX11_UNBIND_VS | DX11_UNBIND_PS | DX11_UNBIND_CBUFF | DX11_UNBIND_VBUFF | DX11_UNBIND_IBUFF | DX11_UNBIND_SRV | DX11_UNBIND_RTV); + } + } + } + + sprite_scope_end(sprite_scope); +#endif } /* ========================== *