From 9de72dceba7e75e555659d09d41c8e03033cbff2 Mon Sep 17 00:00:00 2001 From: jacob Date: Thu, 19 Jun 2025 16:18:40 -0500 Subject: [PATCH] dx12 cmd test --- res/sh/sh_common.h | 1 + src/gp_dx12.c | 62 +++++++++++++++++++++++++++++++++++++--------- 2 files changed, 51 insertions(+), 12 deletions(-) diff --git a/res/sh/sh_common.h b/res/sh/sh_common.h index ef254d85..f42ea40b 100644 --- a/res/sh/sh_common.h +++ b/res/sh/sh_common.h @@ -68,6 +68,7 @@ SH_STRUCT(sh_material_instance { SH_DECL(float2x3, xf); SH_DECL(float2, uv0); SH_DECL(float2, uv1); + SH_DECL(uint, texture_nuri); SH_DECL(uint, tint_srgb); SH_DECL(float, emittance); }); diff --git a/src/gp_dx12.c b/src/gp_dx12.c index 3369567f..b9cf7bca 100644 --- a/src/gp_dx12.c +++ b/src/gp_dx12.c @@ -13,6 +13,7 @@ #include "atomic.h" #include "util.h" #include "rand.h" +#include "sprite.h" /* Include common shader types */ #define SH_CPU 1 @@ -146,6 +147,8 @@ struct command_buffer_group { struct descriptor { struct cpu_descriptor_heap *heap; + + u32 index; D3D12_CPU_DESCRIPTOR_HANDLE handle; struct descriptor *next_free; @@ -397,16 +400,18 @@ INTERNAL struct handle_entry *handle_get_entry(struct gp_handle handle, struct s INTERNAL void *handle_get_data(struct gp_handle handle, enum handle_kind kind) { void *data = NULL; - struct sys_lock lock = sys_mutex_lock_s(G.handle_entries_mutex); - { - struct handle_entry *entry = handle_get_entry(handle, &lock); - data = entry->data; + if (handle.gen) { + struct sys_lock lock = sys_mutex_lock_s(G.handle_entries_mutex); + { + struct handle_entry *entry = handle_get_entry(handle, &lock); + data = entry->data; #if RTC - /* Handle should match expected kind */ - ASSERT(entry->kind == kind); + /* Handle should match expected kind */ + ASSERT(entry->kind == kind); #endif + } + sys_mutex_unlock(&lock); } - sys_mutex_unlock(&lock); return data; } @@ -1159,19 +1164,22 @@ INTERNAL void pipeline_release(struct pipeline *pipeline) INTERNAL struct descriptor *descriptor_alloc(struct cpu_descriptor_heap *dh) { struct descriptor *d = NULL; + u32 index = 0; D3D12_CPU_DESCRIPTOR_HANDLE handle = ZI; { struct sys_lock lock = sys_mutex_lock_e(dh->mutex); if (dh->first_free_descriptor) { d = dh->first_free_descriptor; handle = d->handle; + index = d->index; } else { if (dh->num_descriptors_reserved >= dh->num_descriptors_capacity) { sys_panic(LIT("Max descriptors reached in heap")); } d = arena_push_no_zero(dh->arena, struct descriptor); - handle.ptr = dh->handle.ptr + (dh->num_descriptors_reserved * dh->descriptor_size); + index = dh->num_descriptors_reserved; + handle.ptr = dh->handle.ptr + (index * dh->descriptor_size); ++dh->num_descriptors_reserved; } sys_mutex_unlock(&lock); @@ -1233,10 +1241,11 @@ INTERNAL void cpu_descriptor_heap_release(struct cpu_descriptor_heap *dh) * Flow * ========================== */ - /* TODO: Move command list out of flow struct */ struct flow { struct arena *arena; + /* Below fields are reset each dispatch */ + struct sprite_scope *sprite_scope; struct arena *material_instances_arena; struct flow *next_free; @@ -1251,6 +1260,7 @@ INTERNAL struct flow *flow_alloc(void) flow->arena = arena; } + flow->sprite_scope = sprite_scope_begin(); flow->material_instances_arena = arena_alloc(GIGABYTE(1)); return flow; @@ -1264,8 +1274,32 @@ struct gp_handle gp_flow_alloc(void) void gp_push_cmd(struct gp_handle gp_flow, struct gp_cmd_params params) { - (UNUSED)gp_flow; - (UNUSED)params; + struct flow *flow = handle_get_data(gp_flow, DX12_HANDLE_KIND_FLOW); + if (flow) { + switch (params.kind) { + default: break; + + case GP_CMD_KIND_DRAW_TEXTURE: + { + struct dx12_resource *texture = NULL; + if (params.texture.texture.gen != 0) { + texture = handle_get_data(params.texture.texture, DX12_HANDLE_KIND_RESOURCE); + } else if (params.texture.sprite.hash != 0) { + struct sprite_texture *st = sprite_texture_from_tag_async(flow->sprite_scope, params.texture.sprite); + texture = handle_get_data(st->texture, DX12_HANDLE_KIND_RESOURCE); + } + if (texture) { + struct sh_material_instance *instance = arena_push(flow->material_instances_arena, struct sh_material_instance); + instance->xf = sh_float2x3_from_xform(params.texture.xf); + instance->uv0 = sh_float2_from_v2(params.texture.clip.p0); + instance->uv1 = sh_float2_from_v2(params.texture.clip.p1); + instance->texture_nuri = sh_uint_from_u32(texture->srv_descriptor->index); + instance->tint_srgb = sh_uint_from_u32(params.texture.tint); + instance->emittance = sh_float_from_f32(params.texture.emittance); + } + } break; + } + } } /* ========================== * @@ -1877,7 +1911,6 @@ void gp_dispatch(struct gp_dispatch_params params) /* Upload instance buffer */ struct command_buffer *instance_buffer = command_list_push_buffer(cl, STRING_FROM_ARENA(flow->material_instances_arena)); - arena_reset(flow->material_instances_arena); /* Upload descriptor heap */ struct command_descriptor_heap *descriptor_heap = command_list_push_descriptor_heap(cl, G.cbv_srv_uav_heap); @@ -1951,6 +1984,11 @@ void gp_dispatch(struct gp_dispatch_params params) /* Execute command list */ command_list_close(cl); + + /* Reset flow */ + sprite_scope_end(flow->sprite_scope); + flow->sprite_scope = sprite_scope_begin(); + arena_reset(flow->material_instances_arena); } /* ========================== *