gpu run -> gpu pass. use per-run pass params. set draw view via gpu cmd

This commit is contained in:
jacob 2025-06-02 17:06:30 -05:00
parent ec77a62fda
commit c7b5a41523
5 changed files with 277 additions and 205 deletions

View File

@ -24,6 +24,18 @@ struct draw_startup_receipt draw_startup(struct gpu_startup_receipt *gpu_sr,
return (struct draw_startup_receipt) { 0 }; 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 * Texture
* ========================== */ * ========================== */

View File

@ -10,6 +10,12 @@ struct draw_startup_receipt { i32 _; };
struct draw_startup_receipt draw_startup(struct gpu_startup_receipt *gpu_sr, struct draw_startup_receipt draw_startup(struct gpu_startup_receipt *gpu_sr,
struct font_startup_receipt *font_sr); struct font_startup_receipt *font_sr);
/* ========================== *
* View
* ========================== */
void draw_set_view(struct gpu_cmd_store store, struct xform xf);
/* ========================== * /* ========================== *
* Texture * Texture
* ========================== */ * ========================== */

View File

@ -63,6 +63,7 @@ struct gpu_indices {
enum gpu_cmd_kind { enum gpu_cmd_kind {
GPU_CMD_KIND_NONE, GPU_CMD_KIND_NONE,
GPU_CMD_KIND_DRAW_VIEW,
GPU_CMD_KIND_DRAW_MESH, GPU_CMD_KIND_DRAW_MESH,
GPU_CMD_KIND_DRAW_TEXTURE, GPU_CMD_KIND_DRAW_TEXTURE,
GPU_CMD_KIND_DRAW_GRID, GPU_CMD_KIND_DRAW_GRID,
@ -74,6 +75,9 @@ enum gpu_cmd_kind {
struct gpu_cmd_params { struct gpu_cmd_params {
enum gpu_cmd_kind kind; enum gpu_cmd_kind kind;
union { union {
struct {
struct xform xf;
} view;
struct { struct {
struct v2_array vertices; struct v2_array vertices;
struct gpu_indices indices; 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); 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; 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 #endif

View File

@ -60,9 +60,8 @@ struct dx11_shader {
ID3D11PixelShader *ps; ID3D11PixelShader *ps;
}; };
struct dx11_run_state { struct dx11_pass_state {
i32 _; struct dx11_pass_state *next_free;
struct dx11_run_state *next_free;
}; };
struct dx11_buffer { struct dx11_buffer {
@ -99,6 +98,9 @@ struct dx11_cmd_buffers {
struct dx11_cmd { struct dx11_cmd {
enum gpu_cmd_kind kind; enum gpu_cmd_kind kind;
union { union {
struct {
struct xform xf;
} view;
struct { struct {
u32 vertex_offset; u32 vertex_offset;
u32 vertex_count; u32 vertex_count;
@ -214,10 +216,10 @@ GLOBAL struct {
struct arena cmd_stores_arena; struct arena cmd_stores_arena;
struct dx11_cmd_store *first_free_cmd_store; struct dx11_cmd_store *first_free_cmd_store;
/* Run state pool */ /* Pass state pool */
struct sys_mutex run_states_mutex; struct sys_mutex pass_states_mutex;
struct arena run_states_arena; struct arena pass_states_arena;
struct dx11_run_state *first_free_run_state; struct dx11_pass_state *first_free_pass_state;
/* Texture pool */ /* Texture pool */
struct sys_mutex textures_mutex; 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_mutex = sys_mutex_alloc();
G.cmd_stores_arena = arena_alloc(GIGABYTE(64)); G.cmd_stores_arena = arena_alloc(GIGABYTE(64));
/* Initialize run state pool */ /* Initialize pass state pool */
G.run_states_mutex = sys_mutex_alloc(); G.pass_states_mutex = sys_mutex_alloc();
G.run_states_arena = arena_alloc(GIGABYTE(64)); G.pass_states_arena = arena_alloc(GIGABYTE(64));
/* Initialize texture pool */ /* Initialize texture pool */
G.textures_mutex = sys_mutex_alloc(); 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); ASSERT(false);
} break; } 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: case GPU_CMD_KIND_DRAW_MESH:
{ {
struct dx11_cmd *cmd = store->cpu_last_cmd; 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; __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); struct sys_lock lock = sys_mutex_lock_e(&G.pass_states_mutex);
if (G.first_free_run_state) { if (G.first_free_pass_state) {
state = G.first_free_run_state; state = G.first_free_pass_state;
G.first_free_run_state = state->next_free; G.first_free_pass_state = state->next_free;
} else { } 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); 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; res.handle = (u64)state;
return res; 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 */ /* TODO */
__prof; __prof;
ASSERT(false); ASSERT(false);
(UNUSED)gpu_run_state; (UNUSED)gpu_pass_state;
} }
/* ========================== * /* ========================== *
* Run * Pass
* ========================== */ * ========================== */
enum dx11_unbind_flags { enum dx11_unbind_flags {
@ -1439,23 +1454,25 @@ INTERNAL void dx11_unbind(u32 flags)
} }
} }
/* TODO: Lock resources during run */ /* TODO: Lock resources during pass */
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)
{ {
__prof; __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 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 */ /* 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 d3d11_viewport = ZI;
d3d11_viewport.Width = viewport.width; 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; d3d11_viewport.TopLeftY = viewport.y;
ID3D11DeviceContext_RSSetViewports(G.devcon, 1, &d3d11_viewport); ID3D11DeviceContext_RSSetViewports(G.devcon, 1, &d3d11_viewport);
/* TODO: Set viewport & vp matrix in a cmd */ for (u64 cmd_stores_array_index = 0; cmd_stores_array_index < params.cmds_array.count; ++cmd_stores_array_index) {
struct mat4x4 vp_matrix = calculate_vp(view, viewport.width, viewport.height); 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) { switch (cmd_kind) {
enum gpu_cmd_kind cmd_kind = cmd->kind; default:
{
/* Unknown shader */
ASSERT(false);
} break;
switch (cmd_kind) { case GPU_CMD_KIND_DRAW_VIEW:
default: {
{ __profscope(Set draw view);
/* Unknown shader */ vp_matrix = calculate_vp(cmd->view.xf, viewport.width, viewport.height);
ASSERT(false); } break;
} break;
case GPU_CMD_KIND_DRAW_MESH: case GPU_CMD_KIND_DRAW_MESH:
{ {
__profscope(Draw mesh); __profscope(Draw mesh);
__profscope_dx11(G.profiling_ctx, Draw mesh, RGB_F(0.5, 0.2, 0.2)); __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]; struct dx11_shader *shader = &G.shaders[DX11_SHADER_KIND_MESH];
if (shader->valid) { if (shader->valid) {
struct dx11_buffer *constant_buffer = store->buffers.constant_buffer; struct dx11_buffer *constant_buffer = store->buffers.constant_buffer;
struct dx11_buffer *vertex_buffer = store->buffers.mesh.vertex_buffer; struct dx11_buffer *vertex_buffer = store->buffers.mesh.vertex_buffer;
struct dx11_buffer *index_buffer = store->buffers.mesh.index_buffer; struct dx11_buffer *index_buffer = store->buffers.mesh.index_buffer;
u32 vertex_offset = cmd->mesh.vertex_offset; u32 vertex_offset = cmd->mesh.vertex_offset;
u32 index_offset = cmd->mesh.index_offset; u32 index_offset = cmd->mesh.index_offset;
u32 index_count = cmd->mesh.index_count; u32 index_count = cmd->mesh.index_count;
/* Bind shader */ /* Bind shader */
ID3D11DeviceContext_VSSetShader(G.devcon, shader->vs, 0, 0); ID3D11DeviceContext_VSSetShader(G.devcon, shader->vs, 0, 0);
ID3D11DeviceContext_PSSetShader(G.devcon, shader->ps, 0, 0); ID3D11DeviceContext_PSSetShader(G.devcon, shader->ps, 0, 0);
/* Bind input layout */ /* Bind input layout */
ID3D11DeviceContext_IASetInputLayout(G.devcon, shader->input_layout); ID3D11DeviceContext_IASetInputLayout(G.devcon, shader->input_layout);
/* Fill & bind constant buffer */ /* Fill & bind constant buffer */
{ {
struct dx11_mesh_uniform *uniform = dx11_buffer_push(constant_buffer, sizeof(struct dx11_mesh_uniform)); struct dx11_mesh_uniform *uniform = dx11_buffer_push(constant_buffer, sizeof(struct dx11_mesh_uniform));
uniform->vp = vp_matrix; uniform->vp = vp_matrix;
dx11_buffer_submit(constant_buffer); 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); } break;
ID3D11DeviceContext_PSSetConstantBuffers(G.devcon, 0, 1, &constant_buffer->gpu_buffer);
/* Bind vertex buffer */ case GPU_CMD_KIND_DRAW_TEXTURE:
u32 zero = 0; {
u32 stride = sizeof(struct dx11_mesh_vertex); __profscope(Draw texture);
ID3D11DeviceContext_IASetVertexBuffers(G.devcon, 0, 1, &vertex_buffer->gpu_buffer, &stride, &zero); __profscope_dx11(G.profiling_ctx, Draw texture, RGB_F(0.2, 0.5, 0.2));
ID3D11DeviceContext_IASetIndexBuffer(G.devcon, index_buffer->gpu_buffer, DXGI_FORMAT_R16_UINT, zero); 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 */ if (texture && texture->srv && shader->valid) {
ID3D11DeviceContext_IASetPrimitiveTopology(G.devcon, D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); struct dx11_buffer *constant_buffer = store->buffers.constant_buffer;
ID3D11DeviceContext_DrawIndexed(G.devcon, index_count, index_offset, vertex_offset); 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 */ /* Bind shader */
dx11_unbind(DX11_UNBIND_VS | DX11_UNBIND_PS | DX11_UNBIND_IA | DX11_UNBIND_CBUFF | DX11_UNBIND_VBUFF | DX11_UNBIND_IBUFF); ID3D11DeviceContext_VSSetShader(G.devcon, shader->vs, 0, 0);
} ID3D11DeviceContext_PSSetShader(G.devcon, shader->ps, 0, 0);
} break;
case GPU_CMD_KIND_DRAW_TEXTURE: /* Fill & bind constant buffer */
{ {
__profscope(Draw texture); struct dx11_texture_uniform *uniform = dx11_buffer_push(constant_buffer, sizeof(struct dx11_texture_uniform));
__profscope_dx11(G.profiling_ctx, Draw texture, RGB_F(0.2, 0.5, 0.2)); uniform->vp = vp_matrix;
struct dx11_shader *shader = &G.shaders[DX11_SHADER_KIND_TEXTURE]; uniform->instance_offset = instance_offset;
if (shader->valid) { dx11_buffer_submit(constant_buffer);
struct dx11_texture *texture = NULL; }
if (cmd->texture.texture.handle) { ID3D11DeviceContext_VSSetConstantBuffers(G.devcon, 0, 1, &constant_buffer->gpu_buffer);
/* Load texture if handle is set */ ID3D11DeviceContext_PSSetConstantBuffers(G.devcon, 0, 1, &constant_buffer->gpu_buffer);
texture = (struct dx11_texture *)cmd->texture.texture.handle;
} else if (cmd->texture.sprite.hash) { /* Bind dummy vertex buffer */
/* Otherwise load sprite */ u32 zero = 0;
struct sprite_texture *sprite_texture = sprite_texture_from_tag_async(sprite_scope, cmd->texture.sprite); ID3D11DeviceContext_IASetVertexBuffers(G.devcon, 0, 1, &G.dummy_vertex_buffer->gpu_buffer, &zero, &zero);
if (sprite_texture->loaded) { ID3D11DeviceContext_IASetIndexBuffer(G.devcon, G.quad_index_buffer->gpu_buffer, DXGI_FORMAT_R16_UINT, zero);
texture = (struct dx11_texture *)sprite_texture->texture.handle;
/* 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 *constant_buffer = store->buffers.constant_buffer;
struct dx11_buffer *instance_buffer = store->buffers.texture.instance_buffer; struct dx11_buffer *instance_buffer = store->buffers.grid.instance_buffer;
u32 instance_offset = cmd->texture.instance_offset; u32 instance_offset = cmd->grid.instance_offset;
u32 instance_count = cmd->texture.instance_count; u32 instance_count = cmd->grid.instance_count;
/* Bind shader */ /* Bind shader */
ID3D11DeviceContext_VSSetShader(G.devcon, shader->vs, 0, 0); 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 */ /* 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->vp = vp_matrix;
uniform->instance_offset = instance_offset; uniform->instance_offset = instance_offset;
dx11_buffer_submit(constant_buffer); 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_VSSetShaderResources(G.devcon, 0, 1, &instance_buffer->srv);
ID3D11DeviceContext_PSSetShaderResources(G.devcon, 0, 1, &instance_buffer->srv); ID3D11DeviceContext_PSSetShaderResources(G.devcon, 0, 1, &instance_buffer->srv);
/* Bind texture */ /* Draw */
ID3D11DeviceContext_VSSetShaderResources(G.devcon, 1, 1, &texture->srv); ID3D11DeviceContext_IASetPrimitiveTopology(G.devcon, D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
ID3D11DeviceContext_PSSetShaderResources(G.devcon, 1, 1, &texture->srv); 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 */ /* Draw */
ID3D11DeviceContext_IASetPrimitiveTopology(G.devcon, D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); 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 */ /* Unbind */
dx11_unbind(DX11_UNBIND_VS | DX11_UNBIND_PS | DX11_UNBIND_CBUFF | DX11_UNBIND_VBUFF | DX11_UNBIND_IBUFF | DX11_UNBIND_SRV); dx11_unbind(DX11_UNBIND_VS | DX11_UNBIND_PS | DX11_UNBIND_CBUFF | DX11_UNBIND_VBUFF | DX11_UNBIND_IBUFF | DX11_UNBIND_SRV);
} }
} } break;
} 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;
} }
} }

View File

@ -78,8 +78,8 @@ GLOBAL struct {
struct gpu_cmd_store user_gpu_cmd_store; struct gpu_cmd_store user_gpu_cmd_store;
struct gpu_cmd_store backbuffer_gpu_cmd_store; struct gpu_cmd_store backbuffer_gpu_cmd_store;
struct gpu_run_state user_gpu_run_state; struct gpu_pass_state user_pass_state;
struct gpu_run_state backbuffer_gpu_run_state; struct gpu_pass_state backbuffer_pass_state;
struct xform world_to_user_xf; 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.world_gpu_cmd_store = gpu_cmd_store_alloc();
G.user_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.backbuffer_gpu_cmd_store = gpu_cmd_store_alloc();
G.user_gpu_run_state = gpu_run_state_alloc(); G.user_pass_state = gpu_pass_state_alloc();
G.backbuffer_gpu_run_state = gpu_run_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_SUCCESS);
log_register_callback(debug_console_log_callback, LOG_LEVEL_DEBUG); 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); 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 * Update listener from view
* ========================== */ * ========================== */
@ -2080,9 +2087,6 @@ INTERNAL void user_update(void)
/* Draw user texture to backbuffer texture */ /* 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 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); struct draw_texture_params params = DRAW_TEXTURE_PARAMS(.xf = xf, .texture = G.user_texture);
draw_texture(G.backbuffer_gpu_cmd_store, params); 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.user_texture, 0);
gpu_texture_clear(G.backbuffer_texture, RGBA_F(0, 0, 0, 1)); gpu_texture_clear(G.backbuffer_texture, RGBA_F(0, 0, 0, 1));
/* Run cmds */ /* Render to user texture */
{ {
/* Render to user texture */ struct gpu_cmd_store *pass_cmds[] = { &G.world_gpu_cmd_store, &G.user_gpu_cmd_store };
gpu_run_cmds(G.user_gpu_run_state, G.world_gpu_cmd_store, G.user_texture, G.world_to_user_xf, user_viewport); struct gpu_pass_params params = ZI;
gpu_run_cmds(G.user_gpu_run_state, G.user_gpu_cmd_store, G.user_texture, XFORM_IDENT, user_viewport); 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 */ /* Render to backbuffer texture */
gpu_run_cmds(G.backbuffer_gpu_run_state, G.backbuffer_gpu_cmd_store, G.backbuffer_texture, XFORM_IDENT, backbuffer_viewport); {
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 */ /* Swap backbuffer */