From c7b5a41523aa54674c92df0b9c8206cede08ba1f Mon Sep 17 00:00:00 2001 From: jacob Date: Mon, 2 Jun 2025 17:06:30 -0500 Subject: [PATCH] gpu run -> gpu pass. use per-run pass params. set draw view via gpu cmd --- src/draw.c | 12 ++ src/draw.h | 6 + src/gpu.h | 25 +++- src/gpu_dx11.c | 397 ++++++++++++++++++++++++++----------------------- src/user.c | 42 ++++-- 5 files changed, 277 insertions(+), 205 deletions(-) diff --git a/src/draw.c b/src/draw.c index 96d60e64..2ec4124a 100644 --- a/src/draw.c +++ b/src/draw.c @@ -24,6 +24,18 @@ struct draw_startup_receipt draw_startup(struct gpu_startup_receipt *gpu_sr, return (struct draw_startup_receipt) { 0 }; } +/* ========================== * + * View + * ========================== */ + +void draw_set_view(struct gpu_cmd_store store, struct xform xf) +{ + struct gpu_cmd_params cmd = ZI; + cmd.kind = GPU_CMD_KIND_DRAW_VIEW; + cmd.view.xf = xf; + gpu_push_cmd(store, cmd); +} + /* ========================== * * Texture * ========================== */ diff --git a/src/draw.h b/src/draw.h index 07bcbebd..c511f260 100644 --- a/src/draw.h +++ b/src/draw.h @@ -10,6 +10,12 @@ struct draw_startup_receipt { i32 _; }; struct draw_startup_receipt draw_startup(struct gpu_startup_receipt *gpu_sr, struct font_startup_receipt *font_sr); +/* ========================== * + * View + * ========================== */ + +void draw_set_view(struct gpu_cmd_store store, struct xform xf); + /* ========================== * * Texture * ========================== */ diff --git a/src/gpu.h b/src/gpu.h index fd30b29b..2932f9f1 100644 --- a/src/gpu.h +++ b/src/gpu.h @@ -63,6 +63,7 @@ struct gpu_indices { enum gpu_cmd_kind { GPU_CMD_KIND_NONE, + GPU_CMD_KIND_DRAW_VIEW, GPU_CMD_KIND_DRAW_MESH, GPU_CMD_KIND_DRAW_TEXTURE, GPU_CMD_KIND_DRAW_GRID, @@ -74,6 +75,9 @@ enum gpu_cmd_kind { struct gpu_cmd_params { enum gpu_cmd_kind kind; union { + struct { + struct xform xf; + } view; struct { struct v2_array vertices; struct gpu_indices indices; @@ -116,17 +120,28 @@ void gpu_push_cmd(struct gpu_cmd_store gpu_cmd_store, struct gpu_cmd_params para void gpu_submit_cmds(struct gpu_cmd_store gpu_cmd_store); /* ========================== * - * Run + * Pass * ========================== */ -struct gpu_run_state { +struct gpu_pass_cmds_array { + u64 count; + struct gpu_cmd_store **cmds; +}; + +struct gpu_pass_params { + struct gpu_pass_cmds_array cmds_array; + struct gpu_texture draw_target; + struct rect draw_target_viewport; +}; + +struct gpu_pass_state { u64 handle; }; -struct gpu_run_state gpu_run_state_alloc(void); +struct gpu_pass_state gpu_pass_state_alloc(void); -void gpu_run_state_release(struct gpu_run_state gpu_run_state); +void gpu_pass_state_release(struct gpu_pass_state gpu_pass_state); -void gpu_run_cmds(struct gpu_run_state gpu_run_state, struct gpu_cmd_store gpu_cmd_store, struct gpu_texture target, struct xform view, struct rect viewport); +void gpu_run_pass(struct gpu_pass_state gpu_pass_state, struct gpu_pass_params params); #endif diff --git a/src/gpu_dx11.c b/src/gpu_dx11.c index 9d22c3d7..6b0ed1fd 100644 --- a/src/gpu_dx11.c +++ b/src/gpu_dx11.c @@ -60,9 +60,8 @@ struct dx11_shader { ID3D11PixelShader *ps; }; -struct dx11_run_state { - i32 _; - struct dx11_run_state *next_free; +struct dx11_pass_state { + struct dx11_pass_state *next_free; }; struct dx11_buffer { @@ -99,6 +98,9 @@ struct dx11_cmd_buffers { struct dx11_cmd { enum gpu_cmd_kind kind; union { + struct { + struct xform xf; + } view; struct { u32 vertex_offset; u32 vertex_count; @@ -214,10 +216,10 @@ GLOBAL struct { struct arena cmd_stores_arena; struct dx11_cmd_store *first_free_cmd_store; - /* Run state pool */ - struct sys_mutex run_states_mutex; - struct arena run_states_arena; - struct dx11_run_state *first_free_run_state; + /* Pass state pool */ + struct sys_mutex pass_states_mutex; + struct arena pass_states_arena; + struct dx11_pass_state *first_free_pass_state; /* Texture pool */ struct sys_mutex textures_mutex; @@ -272,9 +274,9 @@ struct gpu_startup_receipt gpu_startup(struct sys_window *window) G.cmd_stores_mutex = sys_mutex_alloc(); G.cmd_stores_arena = arena_alloc(GIGABYTE(64)); - /* Initialize run state pool */ - G.run_states_mutex = sys_mutex_alloc(); - G.run_states_arena = arena_alloc(GIGABYTE(64)); + /* Initialize pass state pool */ + G.pass_states_mutex = sys_mutex_alloc(); + G.pass_states_arena = arena_alloc(GIGABYTE(64)); /* Initialize texture pool */ G.textures_mutex = sys_mutex_alloc(); @@ -1167,6 +1169,19 @@ void gpu_push_cmd(struct gpu_cmd_store gpu_cmd_store, struct gpu_cmd_params para ASSERT(false); } break; + case GPU_CMD_KIND_DRAW_VIEW: + { + struct dx11_cmd *cmd = arena_push(&store->cpu_cmds_arena, struct dx11_cmd); + cmd->kind = params.kind; + cmd->view.xf = params.view.xf; + if (store->cpu_last_cmd) { + store->cpu_last_cmd->next = cmd; + } else { + store->cpu_first_cmd = cmd; + } + store->cpu_last_cmd = cmd; + } break; + case GPU_CMD_KIND_DRAW_MESH: { struct dx11_cmd *cmd = store->cpu_last_cmd; @@ -1334,21 +1349,21 @@ void gpu_submit_cmds(struct gpu_cmd_store gpu_cmd_store) } /* ========================== * - * Run state + * Pass state * ========================== */ -struct gpu_run_state gpu_run_state_alloc(void) +struct gpu_pass_state gpu_pass_state_alloc(void) { __prof; - struct dx11_run_state *state = NULL; + struct dx11_pass_state *state = NULL; { { - struct sys_lock lock = sys_mutex_lock_e(&G.run_states_mutex); - if (G.first_free_run_state) { - state = G.first_free_run_state; - G.first_free_run_state = state->next_free; + struct sys_lock lock = sys_mutex_lock_e(&G.pass_states_mutex); + if (G.first_free_pass_state) { + state = G.first_free_pass_state; + G.first_free_pass_state = state->next_free; } else { - state = arena_push_no_zero(&G.run_states_arena, struct dx11_run_state); + state = arena_push_no_zero(&G.pass_states_arena, struct dx11_pass_state); } sys_mutex_unlock(&lock); } @@ -1356,21 +1371,21 @@ struct gpu_run_state gpu_run_state_alloc(void) } - struct gpu_run_state res = ZI; + struct gpu_pass_state res = ZI; res.handle = (u64)state; return res; } -void gpu_run_state_release(struct gpu_run_state gpu_run_state) +void gpu_pass_state_release(struct gpu_pass_state gpu_pass_state) { /* TODO */ __prof; ASSERT(false); - (UNUSED)gpu_run_state; + (UNUSED)gpu_pass_state; } /* ========================== * - * Run + * Pass * ========================== */ enum dx11_unbind_flags { @@ -1439,23 +1454,25 @@ INTERNAL void dx11_unbind(u32 flags) } } -/* TODO: Lock resources during run */ -void gpu_run_cmds(struct gpu_run_state gpu_run_state, struct gpu_cmd_store gpu_cmd_store, struct gpu_texture target, struct xform view, struct rect viewport) +/* TODO: Lock resources during pass */ +void gpu_run_pass(struct gpu_pass_state gpu_pass_state, struct gpu_pass_params params) { __prof; - __profscope_dx11(G.profiling_ctx, Run, RGB_F(0.5, 0.2, 0.2)); + __profscope_dx11(G.profiling_ctx, Run pass, RGB_F(0.5, 0.2, 0.2)); struct sprite_scope *sprite_scope = sprite_scope_begin(); - struct dx11_run_state *run_state = (struct dx11_run_state *)gpu_run_state.handle; + struct dx11_pass_state *pass_state = (struct dx11_pass_state *)gpu_pass_state.handle; - (UNUSED)run_state; + (UNUSED)pass_state; - struct dx11_cmd_store *store = (struct dx11_cmd_store *)gpu_cmd_store.handle; - struct dx11_texture *target_texture = (struct dx11_texture *)target.handle; + + struct dx11_texture *draw_target = (struct dx11_texture *)params.draw_target.handle; + struct rect viewport = params.draw_target_viewport; + struct mat4x4 vp_matrix = calculate_vp(XFORM_IDENT, viewport.width, viewport.height); /* Set render targets */ - ID3D11DeviceContext_OMSetRenderTargets(G.devcon, 1, &target_texture->rtv, NULL); + ID3D11DeviceContext_OMSetRenderTargets(G.devcon, 1, &draw_target->rtv, NULL); D3D11_VIEWPORT d3d11_viewport = ZI; d3d11_viewport.Width = viewport.width; @@ -1466,87 +1483,140 @@ void gpu_run_cmds(struct gpu_run_state gpu_run_state, struct gpu_cmd_store gpu_c d3d11_viewport.TopLeftY = viewport.y; ID3D11DeviceContext_RSSetViewports(G.devcon, 1, &d3d11_viewport); - /* TODO: Set viewport & vp matrix in a cmd */ - struct mat4x4 vp_matrix = calculate_vp(view, viewport.width, viewport.height); + for (u64 cmd_stores_array_index = 0; cmd_stores_array_index < params.cmds_array.count; ++cmd_stores_array_index) { + struct dx11_cmd_store *store = (struct dx11_cmd_store *)params.cmds_array.cmds[cmd_stores_array_index]->handle; + for (struct dx11_cmd *cmd = store->gpu_first_cmd; cmd; cmd = cmd->next) { + enum gpu_cmd_kind cmd_kind = cmd->kind; - for (struct dx11_cmd *cmd = store->gpu_first_cmd; cmd; cmd = cmd->next) { - enum gpu_cmd_kind cmd_kind = cmd->kind; + switch (cmd_kind) { + default: + { + /* Unknown shader */ + ASSERT(false); + } break; - switch (cmd_kind) { - default: - { - /* Unknown shader */ - ASSERT(false); - } break; + case GPU_CMD_KIND_DRAW_VIEW: + { + __profscope(Set draw view); + vp_matrix = calculate_vp(cmd->view.xf, viewport.width, viewport.height); + } break; - case GPU_CMD_KIND_DRAW_MESH: - { - __profscope(Draw mesh); - __profscope_dx11(G.profiling_ctx, Draw mesh, RGB_F(0.5, 0.2, 0.2)); - struct dx11_shader *shader = &G.shaders[DX11_SHADER_KIND_MESH]; - if (shader->valid) { - struct dx11_buffer *constant_buffer = store->buffers.constant_buffer; - struct dx11_buffer *vertex_buffer = store->buffers.mesh.vertex_buffer; - struct dx11_buffer *index_buffer = store->buffers.mesh.index_buffer; + case GPU_CMD_KIND_DRAW_MESH: + { + __profscope(Draw mesh); + __profscope_dx11(G.profiling_ctx, Draw mesh, RGB_F(0.5, 0.2, 0.2)); + struct dx11_shader *shader = &G.shaders[DX11_SHADER_KIND_MESH]; + if (shader->valid) { + struct dx11_buffer *constant_buffer = store->buffers.constant_buffer; + struct dx11_buffer *vertex_buffer = store->buffers.mesh.vertex_buffer; + struct dx11_buffer *index_buffer = store->buffers.mesh.index_buffer; - u32 vertex_offset = cmd->mesh.vertex_offset; - u32 index_offset = cmd->mesh.index_offset; - u32 index_count = cmd->mesh.index_count; + u32 vertex_offset = cmd->mesh.vertex_offset; + u32 index_offset = cmd->mesh.index_offset; + u32 index_count = cmd->mesh.index_count; - /* Bind shader */ - ID3D11DeviceContext_VSSetShader(G.devcon, shader->vs, 0, 0); - ID3D11DeviceContext_PSSetShader(G.devcon, shader->ps, 0, 0); + /* Bind shader */ + ID3D11DeviceContext_VSSetShader(G.devcon, shader->vs, 0, 0); + ID3D11DeviceContext_PSSetShader(G.devcon, shader->ps, 0, 0); - /* Bind input layout */ - ID3D11DeviceContext_IASetInputLayout(G.devcon, shader->input_layout); + /* Bind input layout */ + ID3D11DeviceContext_IASetInputLayout(G.devcon, shader->input_layout); - /* Fill & bind constant buffer */ - { - struct dx11_mesh_uniform *uniform = dx11_buffer_push(constant_buffer, sizeof(struct dx11_mesh_uniform)); - uniform->vp = vp_matrix; - dx11_buffer_submit(constant_buffer); + /* Fill & bind constant buffer */ + { + struct dx11_mesh_uniform *uniform = dx11_buffer_push(constant_buffer, sizeof(struct dx11_mesh_uniform)); + uniform->vp = vp_matrix; + 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 vertex buffer */ + 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); + + /* Draw */ + ID3D11DeviceContext_IASetPrimitiveTopology(G.devcon, D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); + ID3D11DeviceContext_DrawIndexed(G.devcon, index_count, index_offset, vertex_offset); + + /* Unbind */ + dx11_unbind(DX11_UNBIND_VS | DX11_UNBIND_PS | DX11_UNBIND_IA | DX11_UNBIND_CBUFF | DX11_UNBIND_VBUFF | DX11_UNBIND_IBUFF); } - ID3D11DeviceContext_VSSetConstantBuffers(G.devcon, 0, 1, &constant_buffer->gpu_buffer); - ID3D11DeviceContext_PSSetConstantBuffers(G.devcon, 0, 1, &constant_buffer->gpu_buffer); + } break; - /* Bind vertex buffer */ - 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); + case GPU_CMD_KIND_DRAW_TEXTURE: + { + __profscope(Draw texture); + __profscope_dx11(G.profiling_ctx, Draw texture, RGB_F(0.2, 0.5, 0.2)); + struct dx11_shader *shader = &G.shaders[DX11_SHADER_KIND_TEXTURE]; + if (shader->valid) { + struct dx11_texture *texture = NULL; + if (cmd->texture.texture.handle) { + /* Load texture if handle is set */ + texture = (struct dx11_texture *)cmd->texture.texture.handle; + } 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 dx11_texture *)sprite_texture->texture.handle; + } + } - /* Draw */ - ID3D11DeviceContext_IASetPrimitiveTopology(G.devcon, D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); - ID3D11DeviceContext_DrawIndexed(G.devcon, index_count, index_offset, vertex_offset); + if (texture && texture->srv && shader->valid) { + struct dx11_buffer *constant_buffer = store->buffers.constant_buffer; + struct dx11_buffer *instance_buffer = store->buffers.texture.instance_buffer; + u32 instance_offset = cmd->texture.instance_offset; + u32 instance_count = cmd->texture.instance_count; - /* Unbind */ - dx11_unbind(DX11_UNBIND_VS | DX11_UNBIND_PS | DX11_UNBIND_IA | DX11_UNBIND_CBUFF | DX11_UNBIND_VBUFF | DX11_UNBIND_IBUFF); - } - } break; + /* Bind shader */ + ID3D11DeviceContext_VSSetShader(G.devcon, shader->vs, 0, 0); + ID3D11DeviceContext_PSSetShader(G.devcon, shader->ps, 0, 0); - case GPU_CMD_KIND_DRAW_TEXTURE: - { - __profscope(Draw texture); - __profscope_dx11(G.profiling_ctx, Draw texture, RGB_F(0.2, 0.5, 0.2)); - struct dx11_shader *shader = &G.shaders[DX11_SHADER_KIND_TEXTURE]; - if (shader->valid) { - struct dx11_texture *texture = NULL; - if (cmd->texture.texture.handle) { - /* Load texture if handle is set */ - texture = (struct dx11_texture *)cmd->texture.texture.handle; - } 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 dx11_texture *)sprite_texture->texture.handle; + /* 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 instance buffer */ + ID3D11DeviceContext_VSSetShaderResources(G.devcon, 0, 1, &instance_buffer->srv); + ID3D11DeviceContext_PSSetShaderResources(G.devcon, 0, 1, &instance_buffer->srv); + + /* Bind texture */ + ID3D11DeviceContext_VSSetShaderResources(G.devcon, 1, 1, &texture->srv); + ID3D11DeviceContext_PSSetShaderResources(G.devcon, 1, 1, &texture->srv); + + /* 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); } } + } break; - if (texture && texture->srv && shader->valid) { + case GPU_CMD_KIND_DRAW_GRID: + { + __profscope(Draw grid); + __profscope_dx11(G.profiling_ctx, Draw grid, RGB_F(0.2, 0.2, 0.5)); + struct dx11_shader *shader = &G.shaders[DX11_SHADER_KIND_GRID]; + if (shader->valid) { struct dx11_buffer *constant_buffer = store->buffers.constant_buffer; - struct dx11_buffer *instance_buffer = store->buffers.texture.instance_buffer; - u32 instance_offset = cmd->texture.instance_offset; - u32 instance_count = cmd->texture.instance_count; + struct dx11_buffer *instance_buffer = store->buffers.grid.instance_buffer; + u32 instance_offset = cmd->grid.instance_offset; + u32 instance_count = cmd->grid.instance_count; /* Bind shader */ ID3D11DeviceContext_VSSetShader(G.devcon, shader->vs, 0, 0); @@ -1554,7 +1624,7 @@ void gpu_run_cmds(struct gpu_run_state gpu_run_state, struct gpu_cmd_store gpu_c /* Fill & bind constant buffer */ { - struct dx11_texture_uniform *uniform = dx11_buffer_push(constant_buffer, sizeof(struct dx11_texture_uniform)); + struct dx11_grid_uniform *uniform = dx11_buffer_push(constant_buffer, sizeof(struct dx11_grid_uniform)); uniform->vp = vp_matrix; uniform->instance_offset = instance_offset; dx11_buffer_submit(constant_buffer); @@ -1571,9 +1641,48 @@ void gpu_run_cmds(struct gpu_run_state gpu_run_state, struct gpu_cmd_store gpu_c ID3D11DeviceContext_VSSetShaderResources(G.devcon, 0, 1, &instance_buffer->srv); ID3D11DeviceContext_PSSetShaderResources(G.devcon, 0, 1, &instance_buffer->srv); - /* Bind texture */ - ID3D11DeviceContext_VSSetShaderResources(G.devcon, 1, 1, &texture->srv); - ID3D11DeviceContext_PSSetShaderResources(G.devcon, 1, 1, &texture->srv); + /* 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); + } + } break; + + case GPU_CMD_KIND_TEST: + { + __profscope(Test); + __profscope_dx11(G.profiling_ctx, Test, RGB_F(1, 0.2, 1)); + struct dx11_shader *shader = &G.shaders[DX11_SHADER_KIND_TEST]; + if (shader->valid) { + struct dx11_buffer *constant_buffer = store->buffers.constant_buffer; + struct dx11_buffer *instance_buffer = store->buffers.test.instance_buffer; + u32 instance_offset = cmd->test.instance_offset; + u32 instance_count = cmd->test.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_test_uniform *uniform = dx11_buffer_push(constant_buffer, sizeof(struct dx11_test_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 instance buffer */ + ID3D11DeviceContext_VSSetShaderResources(G.devcon, 0, 1, &instance_buffer->srv); + ID3D11DeviceContext_PSSetShaderResources(G.devcon, 0, 1, &instance_buffer->srv); /* Draw */ ID3D11DeviceContext_IASetPrimitiveTopology(G.devcon, D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); @@ -1582,94 +1691,8 @@ void gpu_run_cmds(struct gpu_run_state gpu_run_state, struct gpu_cmd_store gpu_c /* Unbind */ dx11_unbind(DX11_UNBIND_VS | DX11_UNBIND_PS | DX11_UNBIND_CBUFF | DX11_UNBIND_VBUFF | DX11_UNBIND_IBUFF | DX11_UNBIND_SRV); } - } - } break; - - case GPU_CMD_KIND_DRAW_GRID: - { - __profscope(Draw grid); - __profscope_dx11(G.profiling_ctx, Draw grid, RGB_F(0.2, 0.2, 0.5)); - struct dx11_shader *shader = &G.shaders[DX11_SHADER_KIND_GRID]; - if (shader->valid) { - struct dx11_buffer *constant_buffer = store->buffers.constant_buffer; - struct dx11_buffer *instance_buffer = store->buffers.grid.instance_buffer; - u32 instance_offset = cmd->grid.instance_offset; - u32 instance_count = cmd->grid.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_grid_uniform *uniform = dx11_buffer_push(constant_buffer, sizeof(struct dx11_grid_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 instance buffer */ - ID3D11DeviceContext_VSSetShaderResources(G.devcon, 0, 1, &instance_buffer->srv); - ID3D11DeviceContext_PSSetShaderResources(G.devcon, 0, 1, &instance_buffer->srv); - - /* 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); - } - } break; - - case GPU_CMD_KIND_TEST: - { - __profscope(Test); - __profscope_dx11(G.profiling_ctx, Test, RGB_F(1, 0.2, 1)); - struct dx11_shader *shader = &G.shaders[DX11_SHADER_KIND_TEST]; - if (shader->valid) { - struct dx11_buffer *constant_buffer = store->buffers.constant_buffer; - struct dx11_buffer *instance_buffer = store->buffers.test.instance_buffer; - u32 instance_offset = cmd->test.instance_offset; - u32 instance_count = cmd->test.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_test_uniform *uniform = dx11_buffer_push(constant_buffer, sizeof(struct dx11_test_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 instance buffer */ - ID3D11DeviceContext_VSSetShaderResources(G.devcon, 0, 1, &instance_buffer->srv); - ID3D11DeviceContext_PSSetShaderResources(G.devcon, 0, 1, &instance_buffer->srv); - - /* 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); - } - } break; + } break; + } } } diff --git a/src/user.c b/src/user.c index a07a6221..c84c57ee 100644 --- a/src/user.c +++ b/src/user.c @@ -78,8 +78,8 @@ GLOBAL struct { struct gpu_cmd_store user_gpu_cmd_store; struct gpu_cmd_store backbuffer_gpu_cmd_store; - struct gpu_run_state user_gpu_run_state; - struct gpu_run_state backbuffer_gpu_run_state; + struct gpu_pass_state user_pass_state; + struct gpu_pass_state backbuffer_pass_state; struct xform world_to_user_xf; @@ -260,8 +260,8 @@ struct user_startup_receipt user_startup(struct work_startup_receipt *work_sr, G.world_gpu_cmd_store = gpu_cmd_store_alloc(); G.user_gpu_cmd_store = gpu_cmd_store_alloc(); G.backbuffer_gpu_cmd_store = gpu_cmd_store_alloc(); - G.user_gpu_run_state = gpu_run_state_alloc(); - G.backbuffer_gpu_run_state = gpu_run_state_alloc(); + G.user_pass_state = gpu_pass_state_alloc(); + G.backbuffer_pass_state = gpu_pass_state_alloc(); //log_register_callback(debug_console_log_callback, LOG_LEVEL_SUCCESS); log_register_callback(debug_console_log_callback, LOG_LEVEL_DEBUG); @@ -1026,6 +1026,13 @@ INTERNAL void user_update(void) } G.world_cursor = xform_invert_mul_v2(G.world_to_user_xf, G.user_cursor); + /* ========================== * + * Set draw views + * ========================== */ + + draw_set_view(G.world_gpu_cmd_store, G.world_to_user_xf); + draw_set_view(G.user_gpu_cmd_store, XFORM_IDENT); + /* ========================== * * Update listener from view * ========================== */ @@ -2080,9 +2087,6 @@ INTERNAL void user_update(void) /* Draw user texture to backbuffer texture */ { - //struct xform xf = XFORM_TRS(.t = G.user_screen_offset, .s = G.user_size); - //xf = xform_translated(xf, V2(0.5, 0.5)); - //struct xform xf = XFORM_IDENT; struct xform xf = XFORM_TRS(.t = v2_mul(V2(backbuffer_resolution.x, backbuffer_resolution.y), 0.5), .s = G.user_size); struct draw_texture_params params = DRAW_TEXTURE_PARAMS(.xf = xf, .texture = G.user_texture); draw_texture(G.backbuffer_gpu_cmd_store, params); @@ -2097,14 +2101,26 @@ INTERNAL void user_update(void) gpu_texture_clear(G.user_texture, 0); gpu_texture_clear(G.backbuffer_texture, RGBA_F(0, 0, 0, 1)); - /* Run cmds */ + /* Render to user texture */ { - /* Render to user texture */ - gpu_run_cmds(G.user_gpu_run_state, G.world_gpu_cmd_store, G.user_texture, G.world_to_user_xf, user_viewport); - gpu_run_cmds(G.user_gpu_run_state, G.user_gpu_cmd_store, G.user_texture, XFORM_IDENT, user_viewport); + struct gpu_cmd_store *pass_cmds[] = { &G.world_gpu_cmd_store, &G.user_gpu_cmd_store }; + struct gpu_pass_params params = ZI; + params.cmds_array.cmds = pass_cmds; + params.cmds_array.count = ARRAY_COUNT(pass_cmds); + params.draw_target = G.user_texture; + params.draw_target_viewport = user_viewport; + gpu_run_pass(G.user_pass_state, params); + } - /* Render to backbuffer */ - gpu_run_cmds(G.backbuffer_gpu_run_state, G.backbuffer_gpu_cmd_store, G.backbuffer_texture, XFORM_IDENT, backbuffer_viewport); + /* Render to backbuffer texture */ + { + struct gpu_cmd_store *pass_cmds[] = { &G.backbuffer_gpu_cmd_store }; + struct gpu_pass_params params = ZI; + params.cmds_array.cmds = pass_cmds; + params.cmds_array.count = ARRAY_COUNT(pass_cmds); + params.draw_target = G.backbuffer_texture; + params.draw_target_viewport = backbuffer_viewport; + gpu_run_pass(G.backbuffer_pass_state, params); } /* Swap backbuffer */