From 05ef2b5d1f9ed44ab47309fe10a2feecb2e11e21 Mon Sep 17 00:00:00 2001 From: jacob Date: Mon, 9 Mar 2026 17:35:18 -0500 Subject: [PATCH] better sync wip --- src/gpu/gpu_dx12/gpu_dx12_core.c | 387 +++++++++++++++++++++++-------- src/gpu/gpu_dx12/gpu_dx12_core.h | 7 +- 2 files changed, 298 insertions(+), 96 deletions(-) diff --git a/src/gpu/gpu_dx12/gpu_dx12_core.c b/src/gpu/gpu_dx12/gpu_dx12_core.c index b6642b56..7154ce6b 100644 --- a/src/gpu/gpu_dx12/gpu_dx12_core.c +++ b/src/gpu/gpu_dx12/gpu_dx12_core.c @@ -2141,6 +2141,112 @@ void G_D12_InsertEvent(ID3D12GraphicsCommandList7 *d3d_cl, G_D12_EventKind kind, //////////////////////////////////////////////////////////// //~ Tracking +// void G_D12_UpdateTrackedUsage(Arena *arena, G_D12_CmdBatch *batch, G_D12_Resource *resource, RngI32 mips, G_D12_TrackedUsageKind usage_kind) +// { +// b32 should_track = !AnyBit(resource->d3d_desc.Flags, D3D12_RESOURCE_FLAG_ALLOW_SIMULTANEOUS_ACCESS); +// if (should_track) +// { +// G_D12_CmdBatch *prev_batch = batch->prev; +// G_D12_TrackedResourceNode *trn = 0; +// G_D12_TrackedResourceNode *prev_trn = 0; +// { +// u64 hash = MixU64(resource->uid); +// // Fetch/create tracked resource in current batch +// { +// if (!batch->tracked_resource_bins) +// { +// batch->tracked_resource_bins = PushStructs(arena, G_D12_TrackedResourceBin, G_D12_TrackedResourceBinsCount); +// } +// G_D12_TrackedResourceBin *bin = &batch->tracked_resource_bins[hash % G_D12_TrackedResourceBinsCount]; +// G_D12_TrackedResourceNode *tmp = bin->first; +// for (; tmp; tmp = tmp->next_in_bin) +// { +// if (tmp->hash == hash) +// { +// break; +// } +// } +// if (!tmp) +// { +// tmp = PushStruct(arena, G_D12_TrackedResourceNode); +// tmp->resource = resource; +// tmp->hash = hash; +// ++batch->tracked_resources_count; +// SllQueuePush(batch->first_tracked_resource, batch->last_tracked_resource, tmp); +// SllStackPushN(bin->first, tmp, next_in_bin); +// } +// trn = tmp; +// } +// // Fetch/create tracked resource in previous batch +// if (prev_batch) +// { +// if (!prev_batch->tracked_resource_bins) +// { +// prev_batch->tracked_resource_bins = PushStructs(arena, G_D12_TrackedResourceBin, G_D12_TrackedResourceBinsCount); +// } +// G_D12_TrackedResourceBin *bin = &prev_batch->tracked_resource_bins[hash % G_D12_TrackedResourceBinsCount]; +// G_D12_TrackedResourceNode *tmp = bin->first; +// for (; tmp; tmp = tmp->next_in_bin) +// { +// if (tmp->hash == hash) +// { +// break; +// } +// } +// if (!tmp) +// { +// tmp = PushStruct(arena, G_D12_TrackedResourceNode); +// tmp->resource = resource; +// tmp->hash = hash; +// SllQueuePush(prev_batch->first_tracked_resource, prev_batch->last_tracked_resource, tmp); +// SllStackPushN(bin->first, tmp, next_in_bin); +// } +// prev_trn = tmp; +// } +// } + +// for (i32 mip_idx = mips.min; mip_idx <= mips.max; ++mip_idx) +// { +// G_D12_TrackedMip *mip = &trn->mips[mip_idx]; +// G_D12_TrackedMip *prev_mip = 0; +// if (prev_trn) +// { +// prev_mip = &prev_trn->mips[mip_idx]; +// } + +// if (usage_kind > mip->usage) +// { +// mip->usage = usage_kind; +// } + +// if (usage_kind == G_D12_TrackedUsageKind_Acquire) +// { +// mip->prev_usage = G_D12_TrackedUsageKind_Release; +// } +// else +// { +// if (usage_kind == G_D12_TrackedUsageKind_Release) +// { +// mip->next_usage = G_D12_TrackedUsageKind_Release; +// } +// if (prev_mip) +// { +// mip->prev_usage = prev_mip->usage; +// } +// } + +// if (prev_mip) +// { +// prev_mip->usage = mip->prev_usage; +// prev_mip->next_usage = mip->usage; +// } +// } +// } +// } + + + + void G_D12_UpdateTrackedUsage(Arena *arena, G_D12_CmdBatch *batch, G_D12_Resource *resource, RngI32 mips, G_D12_TrackedUsageKind usage_kind) { b32 should_track = !AnyBit(resource->d3d_desc.Flags, D3D12_RESOURCE_FLAG_ALLOW_SIMULTANEOUS_ACCESS); @@ -2177,7 +2283,7 @@ void G_D12_UpdateTrackedUsage(Arena *arena, G_D12_CmdBatch *batch, G_D12_Resourc } trn = tmp; } - // Fetch/create tracked resource in previous batch + // Fetch tracked resource in previous batch if (prev_batch) { if (!prev_batch->tracked_resource_bins) @@ -2193,14 +2299,6 @@ void G_D12_UpdateTrackedUsage(Arena *arena, G_D12_CmdBatch *batch, G_D12_Resourc break; } } - if (!tmp) - { - tmp = PushStruct(arena, G_D12_TrackedResourceNode); - tmp->resource = resource; - tmp->hash = hash; - SllQueuePush(prev_batch->first_tracked_resource, prev_batch->last_tracked_resource, tmp); - SllStackPushN(bin->first, tmp, next_in_bin); - } prev_trn = tmp; } } @@ -2244,106 +2342,201 @@ void G_D12_UpdateTrackedUsage(Arena *arena, G_D12_CmdBatch *batch, G_D12_Resourc } } + + + + + + + + + G_D12_BarrierInfo G_D12_BarrierInfoFromBatch(G_D12_CmdBatch *batch, G_QueueKind queue_kind) { G_D12_BarrierInfo result = Zi; + // result.access = D3D12_BARRIER_ACCESS_NO_ACCESS; + + // result.sync = D3D12_BARRIER_SYNC_ALL; + result.sync = D3D12_BARRIER_SYNC_NONE; + result.access = D3D12_BARRIER_ACCESS_COMMON; + + + - if (batch->contains_compute_shader) { - result.sync |= D3D12_BARRIER_SYNC_COMPUTE_SHADING; - result.access |= D3D12_BARRIER_ACCESS_UNORDERED_ACCESS | D3D12_BARRIER_ACCESS_SHADER_RESOURCE; - } - if (batch->contains_draw_shader) - { - result.sync |= D3D12_BARRIER_SYNC_VERTEX_SHADING | D3D12_BARRIER_SYNC_PIXEL_SHADING; - result.access |= D3D12_BARRIER_ACCESS_UNORDERED_ACCESS | D3D12_BARRIER_ACCESS_SHADER_RESOURCE; - } - if (batch->contains_indirect) - { - result.sync |= D3D12_BARRIER_SYNC_EXECUTE_INDIRECT; - result.access |= D3D12_BARRIER_ACCESS_INDIRECT_ARGUMENT; - } - if (batch->contains_rtv) - { - result.sync |= D3D12_BARRIER_SYNC_RENDER_TARGET; - result.access |= D3D12_BARRIER_ACCESS_RENDER_TARGET; - } - if (batch->contains_dsv_read) - { - result.sync |= D3D12_BARRIER_SYNC_DEPTH_STENCIL; - result.access |= D3D12_BARRIER_ACCESS_DEPTH_STENCIL_READ; - } - if (batch->contains_dsv_write) - { - result.sync |= D3D12_BARRIER_SYNC_DEPTH_STENCIL; - result.access |= D3D12_BARRIER_ACCESS_DEPTH_STENCIL_WRITE; - } - if (batch->contains_copy) - { - result.sync |= D3D12_BARRIER_SYNC_COPY; - result.access |= D3D12_BARRIER_ACCESS_COPY_DEST | D3D12_BARRIER_ACCESS_COPY_SOURCE; + if (batch->contains_compute_shader) + { + result.sync |= D3D12_BARRIER_SYNC_COMPUTE_SHADING; + // result.access |= D3D12_BARRIER_ACCESS_UNORDERED_ACCESS | D3D12_BARRIER_ACCESS_SHADER_RESOURCE; + } + if (batch->contains_draw_shader) + { + result.sync |= D3D12_BARRIER_SYNC_VERTEX_SHADING | D3D12_BARRIER_SYNC_PIXEL_SHADING; + // result.access |= D3D12_BARRIER_ACCESS_UNORDERED_ACCESS | D3D12_BARRIER_ACCESS_SHADER_RESOURCE; + } + if (batch->contains_indirect) + { + result.sync |= D3D12_BARRIER_SYNC_EXECUTE_INDIRECT; + // result.access |= D3D12_BARRIER_ACCESS_INDIRECT_ARGUMENT; + } + if (batch->contains_rtv) + { + result.sync |= D3D12_BARRIER_SYNC_RENDER_TARGET; + // result.access |= D3D12_BARRIER_ACCESS_RENDER_TARGET; + } + if (batch->contains_dsv_read) + { + result.sync |= D3D12_BARRIER_SYNC_DEPTH_STENCIL; + // result.access |= D3D12_BARRIER_ACCESS_DEPTH_STENCIL_READ; + } + if (batch->contains_dsv_write) + { + result.sync |= D3D12_BARRIER_SYNC_DEPTH_STENCIL; + // result.access |= D3D12_BARRIER_ACCESS_DEPTH_STENCIL_WRITE; + } + if (batch->contains_copy) + { + result.sync |= D3D12_BARRIER_SYNC_COPY; + // result.access |= D3D12_BARRIER_ACCESS_COPY_DEST | D3D12_BARRIER_ACCESS_COPY_SOURCE; + } } + + + // { + // if (batch->contains_compute_shader) + // { + // result.sync |= D3D12_BARRIER_SYNC_COMPUTE_SHADING; + // result.access |= D3D12_BARRIER_ACCESS_UNORDERED_ACCESS | D3D12_BARRIER_ACCESS_SHADER_RESOURCE; + // } + // if (batch->contains_draw_shader) + // { + // result.sync |= D3D12_BARRIER_SYNC_VERTEX_SHADING | D3D12_BARRIER_SYNC_PIXEL_SHADING; + // result.access |= D3D12_BARRIER_ACCESS_UNORDERED_ACCESS | D3D12_BARRIER_ACCESS_SHADER_RESOURCE; + // } + // if (batch->contains_indirect) + // { + // result.sync |= D3D12_BARRIER_SYNC_EXECUTE_INDIRECT; + // result.access |= D3D12_BARRIER_ACCESS_INDIRECT_ARGUMENT; + // } + // if (batch->contains_rtv) + // { + // result.sync |= D3D12_BARRIER_SYNC_RENDER_TARGET; + // result.access |= D3D12_BARRIER_ACCESS_RENDER_TARGET; + // } + // if (batch->contains_dsv_read) + // { + // result.sync |= D3D12_BARRIER_SYNC_DEPTH_STENCIL; + // result.access |= D3D12_BARRIER_ACCESS_DEPTH_STENCIL_READ; + // } + // if (batch->contains_dsv_write) + // { + // result.sync |= D3D12_BARRIER_SYNC_DEPTH_STENCIL; + // result.access |= D3D12_BARRIER_ACCESS_DEPTH_STENCIL_WRITE; + // } + // if (batch->contains_copy) + // { + // result.sync |= D3D12_BARRIER_SYNC_COPY; + // result.access |= D3D12_BARRIER_ACCESS_COPY_DEST | D3D12_BARRIER_ACCESS_COPY_SOURCE; + // } + // } + + // if (result.access != D3D12_BARRIER_ACCESS_NO_ACCESS) + // { + // result.access &= ~D3D12_BARRIER_ACCESS_NO_ACCESS; + // } + return result; } G_D12_BarrierInfo G_D12_BarrierInfoFromUsageKind(G_D12_TrackedUsageKind usage_kind, G_D12_Resource *resource, G_D12_BarrierInfo global, G_QueueKind queue_kind) { G_D12_BarrierInfo result = Zi; + result.sync = global.sync; + result.access = global.access; + + + + + { if (usage_kind == G_D12_TrackedUsageKind_RenderTarget) { result.layout = D3D12_BARRIER_LAYOUT_RENDER_TARGET; - result.sync |= D3D12_BARRIER_SYNC_RENDER_TARGET; - result.access |= D3D12_BARRIER_ACCESS_RENDER_TARGET; } else if (usage_kind == G_D12_TrackedUsageKind_DepthStencilRead) { result.layout = D3D12_BARRIER_LAYOUT_DIRECT_QUEUE_GENERIC_READ; - - result.sync |= D3D12_BARRIER_SYNC_DEPTH_STENCIL; - result.sync |= (global.sync & D3D12_BARRIER_SYNC_COPY); - result.sync |= (global.sync & D3D12_BARRIER_SYNC_VERTEX_SHADING); - result.sync |= (global.sync & D3D12_BARRIER_SYNC_PIXEL_SHADING); - result.sync |= (global.sync & D3D12_BARRIER_SYNC_COMPUTE_SHADING); - - result.access |= D3D12_BARRIER_ACCESS_DEPTH_STENCIL_READ; - result.access |= (global.access & D3D12_BARRIER_ACCESS_COPY_SOURCE); - result.access |= (global.access & D3D12_BARRIER_ACCESS_SHADER_RESOURCE); } else if (usage_kind == G_D12_TrackedUsageKind_DepthStencilReadWrite) { result.layout = D3D12_BARRIER_LAYOUT_DEPTH_STENCIL_WRITE; - result.sync |= D3D12_BARRIER_SYNC_DEPTH_STENCIL; - result.access |= D3D12_BARRIER_ACCESS_DEPTH_STENCIL_WRITE; + } + else if (usage_kind == G_D12_TrackedUsageKind_Release) + { + result.layout = D3D12_BARRIER_LAYOUT_COMMON; } else { - if (usage_kind == G_D12_TrackedUsageKind_Release) - { - result.layout = D3D12_BARRIER_LAYOUT_COMMON; - } - else - { - result.layout = G_D12_CommonLayoutFromQueueKind(queue_kind); - if (resource->d3d_desc.Flags & D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS) - { - result.access |= (global.access & D3D12_BARRIER_ACCESS_UNORDERED_ACCESS); - } - } - - result.sync |= D3D12_BARRIER_SYNC_VERTEX_SHADING; - result.sync |= D3D12_BARRIER_SYNC_PIXEL_SHADING; - result.sync |= D3D12_BARRIER_SYNC_COMPUTE_SHADING; - result.sync |= (global.sync & D3D12_BARRIER_SYNC_COPY); - - result.access |= D3D12_BARRIER_ACCESS_SHADER_RESOURCE; - result.access |= (global.access & D3D12_BARRIER_ACCESS_COPY_SOURCE); - result.access |= (global.access & D3D12_BARRIER_ACCESS_COPY_DEST); + result.layout = G_D12_CommonLayoutFromQueueKind(queue_kind); } } + + + + + // { + // if (usage_kind == G_D12_TrackedUsageKind_RenderTarget) + // { + // result.layout = D3D12_BARRIER_LAYOUT_RENDER_TARGET; + // result.sync |= D3D12_BARRIER_SYNC_RENDER_TARGET; + // result.access |= D3D12_BARRIER_ACCESS_RENDER_TARGET; + // } + // else if (usage_kind == G_D12_TrackedUsageKind_DepthStencilRead) + // { + // result.layout = D3D12_BARRIER_LAYOUT_DIRECT_QUEUE_GENERIC_READ; + + // result.sync |= D3D12_BARRIER_SYNC_DEPTH_STENCIL; + // result.sync |= (global.sync & D3D12_BARRIER_SYNC_COPY); + // result.sync |= (global.sync & D3D12_BARRIER_SYNC_VERTEX_SHADING); + // result.sync |= (global.sync & D3D12_BARRIER_SYNC_PIXEL_SHADING); + // result.sync |= (global.sync & D3D12_BARRIER_SYNC_COMPUTE_SHADING); + + // result.access |= D3D12_BARRIER_ACCESS_DEPTH_STENCIL_READ; + // result.access |= (global.access & D3D12_BARRIER_ACCESS_COPY_SOURCE); + // result.access |= (global.access & D3D12_BARRIER_ACCESS_SHADER_RESOURCE); + // } + // else if (usage_kind == G_D12_TrackedUsageKind_DepthStencilReadWrite) + // { + // result.layout = D3D12_BARRIER_LAYOUT_DEPTH_STENCIL_WRITE; + // result.sync |= D3D12_BARRIER_SYNC_DEPTH_STENCIL; + // result.access |= D3D12_BARRIER_ACCESS_DEPTH_STENCIL_WRITE; + // } + // else if (usage_kind == G_D12_TrackedUsageKind_Release) + // { + // result.layout = D3D12_BARRIER_LAYOUT_COMMON; + // result.access = D3D12_BARRIER_ACCESS_COMMON; + // } + // else + // { + // result.layout = G_D12_CommonLayoutFromQueueKind(queue_kind); + // result.sync |= (global.sync & D3D12_BARRIER_SYNC_COPY); + // result.sync |= (global.sync & D3D12_BARRIER_SYNC_VERTEX_SHADING); + // result.sync |= (global.sync & D3D12_BARRIER_SYNC_PIXEL_SHADING); + // result.sync |= (global.sync & D3D12_BARRIER_SYNC_COMPUTE_SHADING); + + // result.access |= (global.access & D3D12_BARRIER_ACCESS_COPY_SOURCE); + // result.access |= (global.access & D3D12_BARRIER_ACCESS_COPY_DEST); + // result.access |= (global.access & D3D12_BARRIER_ACCESS_SHADER_RESOURCE); + // if (resource->d3d_desc.Flags & D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS) + // { + // result.access |= (global.access & D3D12_BARRIER_ACCESS_UNORDERED_ACCESS); + // } + // } + // } + return result; } @@ -2399,7 +2592,9 @@ i64 G_CommitCommandList(G_CommandListHandle cl_handle) G_D12_CmdBatch *last_batch = 0; { + ++batches_count; G_D12_CmdBatch *batch = PushStruct(scratch.arena, G_D12_CmdBatch); + DllQueuePush(first_batch, last_batch, batch); for (G_D12_CmdNode *cmd_node = cl->first_cmd_node; cmd_node; cmd_node = cmd_node->next) { @@ -2471,8 +2666,8 @@ i64 G_CommitCommandList(G_CommandListHandle cl_handle) if (batch->contains_hazard) { ++batches_count; - DllQueuePush(first_batch, last_batch, batch); batch = PushStruct(scratch.arena, G_D12_CmdBatch); + DllQueuePush(first_batch, last_batch, batch); } G_D12_Resource *resource = cmd->barrier.resource; @@ -2499,19 +2694,15 @@ i64 G_CommitCommandList(G_CommandListHandle cl_handle) // TODO: Only perform tracking on batches containing draws/clear/discard } - if (batch->first_cmd || batch->first_tracked_resource) + if (last_batch->first_cmd || last_batch->first_tracked_resource) { - // Submit open batch - ++batches_count; - DllQueuePush(first_batch, last_batch, batch); - - if (batch->first_tracked_resource) - { - // Final empty batch to implicitly decay tracked resources - batch = PushStruct(scratch.arena, G_D12_CmdBatch); - ++batches_count; - DllQueuePush(first_batch, last_batch, batch); - } + // if (batch->first_tracked_resource && batch->contains_hazard) + // { + // // Final empty batch to implicitly decay tracked resources + // batch = PushStruct(scratch.arena, G_D12_CmdBatch); + // ++batches_count; + // DllQueuePush(first_batch, last_batch, batch); + // } } } @@ -2546,7 +2737,8 @@ i64 G_CommitCommandList(G_CommandListHandle cl_handle) } // Push decay transition to next batch - if (next_batch && mip->usage != mip->next_usage) + // if (next_batch && mip->usage != mip->next_usage) + if (next_batch && mip->next_usage == G_D12_TrackedUsageKind_Untracked) { ++next_batch->transitions_count; G_D12_TransitionNode *tn = PushStruct(scratch.arena, G_D12_TransitionNode); @@ -2675,9 +2867,9 @@ i64 G_CommitCommandList(G_CommandListHandle cl_handle) { D3D12_GLOBAL_BARRIER global_barrier = Zi; global_barrier.SyncBefore = global_before.sync; - global_barrier.SyncAfter = global_before.sync; + global_barrier.SyncAfter = global_after.sync; global_barrier.AccessBefore = global_before.access; - global_barrier.AccessAfter = global_before.access; + global_barrier.AccessAfter = global_after.access; u32 barrier_groups_count = 0; D3D12_BARRIER_GROUP barrier_groups[2] = Zi; @@ -3782,7 +3974,14 @@ G_ResourceHandle G_PrepareBackbuffer(G_SwapchainHandle swapchain_handle, G_Forma backbuffer->texture_mips = 1; backbuffer->swapchain = swapchain; - G_D12_SetObjectName((ID3D12Object *)backbuffer->d3d_resource, StringF(scratch.arena, "Backbuffer [%F]", FmtUint(backbuffer_idx))); + String name = StringF(scratch.arena, "Backbuffer [%F]", FmtUint(backbuffer_idx)); + name.len = MinU64(name.len, G_D12_MaxNameLen); + { + backbuffer->name_len = name.len; + CopyBytes(backbuffer->name_cstr, name.text, name.len); + backbuffer->name_cstr[name.len] = 0; + G_D12_SetObjectName((ID3D12Object *)backbuffer->d3d_resource, name); + } } } swapchain->backbuffers_format = format; diff --git a/src/gpu/gpu_dx12/gpu_dx12_core.h b/src/gpu/gpu_dx12/gpu_dx12_core.h index 315df33b..04266937 100644 --- a/src/gpu/gpu_dx12/gpu_dx12_core.h +++ b/src/gpu/gpu_dx12/gpu_dx12_core.h @@ -494,14 +494,17 @@ Struct(G_D12_CmdBatch) G_D12_BatchedCmdNode *first_cmd; G_D12_BatchedCmdNode *last_cmd; + // Stage hazards b32 contains_hazard; b32 contains_compute_shader; b32 contains_draw_shader; + b32 contains_indirect; + b32 contains_copy; + + // Access hazards b32 contains_rtv; b32 contains_dsv_read; b32 contains_dsv_write; - b32 contains_indirect; - b32 contains_copy; }; ////////////////////////////////////////////////////////////