remove forced descriptor slot api

This commit is contained in:
jacob 2025-12-09 19:09:25 -06:00
parent f47f7114d0
commit c7e560f98c
10 changed files with 64 additions and 124 deletions

View File

@ -28,7 +28,7 @@ void G_BootstrapCommon(void)
/* Init point sampler */ /* Init point sampler */
{ {
G_ResourceHandle pt_sampler = G_PushSampler(gpu_perm, (G_SamplerResourceDesc) { .filter = G_Filter_MinMagMipPoint }); G_ResourceHandle pt_sampler = G_PushSampler(gpu_perm, (G_SamplerResourceDesc) { .filter = G_Filter_MinMagMipPoint });
G_PushSamplerStateRef(gpu_perm, pt_sampler, .forced = G_BasicPointSampler.v); g->basic_sampler = G_PushSamplerStateRef(gpu_perm, pt_sampler);
} }
/* Init noise texture */ /* Init noise texture */
@ -49,7 +49,7 @@ void G_BootstrapCommon(void)
noise_data.text, noise_dims, noise_data.text, noise_dims,
RNG3I32(VEC3I32(0, 0, 0), noise_dims)); RNG3I32(VEC3I32(0, 0, 0), noise_dims));
G_PushTexture3DRef(gpu_perm, noise_tex, .forced = G_BasicNoiseTexture.v); g->basic_noise = G_PushTexture3DRef(gpu_perm, noise_tex);
} }
} }
@ -104,7 +104,17 @@ Rng2 G_ScissorFromTexture(G_ResourceHandle texture)
//- Shared resources //- Shared resources
G_IndexBufferDesc G_GetSharedQuadIndices(void) G_IndexBufferDesc G_QuadIndices(void)
{ {
return G_shared_util_state.quad_indices; return G_shared_util_state.quad_indices;
} }
G_SamplerStateRef G_BasicSampler(void)
{
return G_shared_util_state.basic_sampler;
}
G_Texture3DRef G_BasicNoiseTexture(void)
{
return G_shared_util_state.basic_noise;
}

View File

@ -5,6 +5,8 @@ Struct(G_SharedUtilState)
{ {
/* Common shared resources */ /* Common shared resources */
G_IndexBufferDesc quad_indices; G_IndexBufferDesc quad_indices;
G_SamplerStateRef basic_sampler;
G_Texture3DRef basic_noise;
} extern G_shared_util_state; } extern G_shared_util_state;
extern ThreadLocal G_ArenaHandle G_t_perm_arena; extern ThreadLocal G_ArenaHandle G_t_perm_arena;
@ -34,4 +36,6 @@ Rng2 G_ScissorFromTexture(G_ResourceHandle texture);
//- Shared resources //- Shared resources
G_IndexBufferDesc G_GetSharedQuadIndices(void); G_IndexBufferDesc G_QuadIndices(void);
G_SamplerStateRef G_BasicSampler(void);
G_Texture3DRef G_BasicNoiseTexture(void);

View File

@ -433,14 +433,6 @@ Struct(G_RefDesc)
G_RefKind kind; G_RefKind kind;
u64 element_size; u64 element_size;
u64 element_offset; u64 element_offset;
/* If set, then the layer will attempt to allocate the shader-visible
* ref in the specified slot. This slot can only be allocated to
* once globally. Any subsequent static allocations to the same slot will
* cause a panic unless it is freed. 0 will result in a regular
* dynamically-chosen slot for the reference.
*/
u32 forced;
}; };
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////

View File

@ -226,7 +226,7 @@ void G_Bootstrap(void)
{ {
/* Push an empty descriptor at index 0, so that a handle with a value of 0 always represents nil */ /* Push an empty descriptor at index 0, so that a handle with a value of 0 always represents nil */
G_D12_Arena *gpu_perm = G_D12_ArenaFromHandle(G_PermArena()); G_D12_Arena *gpu_perm = G_D12_ArenaFromHandle(G_PermArena());
G_D12_Descriptor *nil_descriptor = G_D12_PushDescriptor(gpu_perm, kind, 0); G_D12_Descriptor *nil_descriptor = G_D12_PushDescriptor(gpu_perm, kind);
Assert(nil_descriptor->index == 0); Assert(nil_descriptor->index == 0);
} }
@ -650,9 +650,9 @@ G_D12_RawCommandList *G_D12_PrepareRawCommandList(G_QueueKind queue_kind)
G_D12_Arena *gpu_perm = G_D12_ArenaFromHandle(G_PermArena()); G_D12_Arena *gpu_perm = G_D12_ArenaFromHandle(G_PermArena());
for (u32 i = 0; i < countof(cl->rtv_descriptors); ++i) for (u32 i = 0; i < countof(cl->rtv_descriptors); ++i)
{ {
cl->rtv_descriptors[i] = G_D12_PushDescriptor(gpu_perm, G_D12_DescriptorHeapKind_Rtv, 0); cl->rtv_descriptors[i] = G_D12_PushDescriptor(gpu_perm, G_D12_DescriptorHeapKind_Rtv);
} }
cl->rtv_clear_descriptor = G_D12_PushDescriptor(gpu_perm, G_D12_DescriptorHeapKind_Rtv, 0); cl->rtv_clear_descriptor = G_D12_PushDescriptor(gpu_perm, G_D12_DescriptorHeapKind_Rtv);
} }
} }
@ -751,7 +751,7 @@ G_D12_Descriptor *G_D12_DescriptorFromIndex(G_D12_DescriptorHeapKind heap_kind,
return &descriptors[index]; return &descriptors[index];
} }
G_D12_Descriptor *G_D12_PushDescriptor(G_D12_Arena *gpu_arena, G_D12_DescriptorHeapKind heap_kind, u32 forced) G_D12_Descriptor *G_D12_PushDescriptor(G_D12_Arena *gpu_arena, G_D12_DescriptorHeapKind heap_kind)
{ {
G_D12_SharedState *g = &G_D12_shared_state; G_D12_SharedState *g = &G_D12_shared_state;
G_D12_DescriptorHeap *heap = &g->descriptor_heaps[heap_kind]; G_D12_DescriptorHeap *heap = &g->descriptor_heaps[heap_kind];
@ -759,105 +759,49 @@ G_D12_Descriptor *G_D12_PushDescriptor(G_D12_Arena *gpu_arena, G_D12_DescriptorH
G_D12_Descriptor *descriptor = 0; G_D12_Descriptor *descriptor = 0;
/* Grab completed descriptor from arena */ /* Grab completed descriptor from arena */
if (forced == 0) G_D12_DescriptorList *descriptors_by_queue = gpu_arena->committed_descriptors_by_heap_and_queue[heap_kind];
for (G_QueueKind queue_kind = 0; !descriptor && queue_kind < G_NumQueues; ++queue_kind)
{ {
G_D12_DescriptorList *descriptors_by_queue = gpu_arena->committed_descriptors_by_heap_and_queue[heap_kind]; G_D12_DescriptorList *descriptors = &descriptors_by_queue[queue_kind];
for (G_QueueKind queue_kind = 0; !descriptor && queue_kind < G_NumQueues; ++queue_kind) descriptor = descriptors->first;
if (descriptor)
{ {
G_D12_DescriptorList *descriptors = &descriptors_by_queue[queue_kind]; G_D12_Queue *queue = G_D12_QueueFromKind(queue_kind);
descriptor = descriptors->first; u64 queue_commit_completion = ID3D12Fence_GetCompletedValue(queue->commit_fence);
if (descriptor) if (queue_commit_completion >= descriptor->queue_commit_target)
{ {
G_D12_Queue *queue = G_D12_QueueFromKind(queue_kind); /* Descriptor no longer in use by gpu, reuse it */
u64 queue_commit_completion = ID3D12Fence_GetCompletedValue(queue->commit_fence); DllQueueRemove(descriptors->first, descriptors->last, descriptor);
if (queue_commit_completion >= descriptor->queue_commit_target) }
{ else
/* Descriptor no longer in use by gpu, reuse it */ {
DllQueueRemove(descriptors->first, descriptors->last, descriptor); /* Descriptor may still be in use by gpu */
} descriptor = 0;
else
{
/* Descriptor may still be in use by gpu */
descriptor = 0;
}
} }
} }
} }
/* Allocate new descriptor from heap */ /* Allocate new descriptor from heap */
u32 index = forced; u32 index = 0;
if (!descriptor) if (!descriptor)
{ {
Lock lock = LockE(&heap->mutex); Lock lock = LockE(&heap->mutex);
{ {
if (index == 0) if (heap->first_free)
{ {
if (heap->first_free) descriptor = heap->first_free;
{ DllStackRemove(heap->first_free, descriptor);
descriptor = heap->first_free; index = descriptor->index;
DllStackRemove(heap->first_free, descriptor);
index = descriptor->index;
}
else
{
u32 descriptors_count = ArenaCount(heap->descriptors_arena, G_D12_Descriptor);
if (descriptors_count >= heap->max_count)
{
Panic(Lit("Max descriptors reached in heap"));
}
descriptor = PushStruct(heap->descriptors_arena, G_D12_Descriptor);
index = descriptors_count;
}
} }
else else
{ {
if (index >= heap->max_count) u32 descriptors_count = ArenaCount(heap->descriptors_arena, G_D12_Descriptor);
if (descriptors_count >= heap->max_count)
{ {
Panic(Lit("Max descriptors reached in heap")); Panic(Lit("Max descriptors reached in heap"));
} }
descriptor = PushStruct(heap->descriptors_arena, G_D12_Descriptor);
/* Push descriptors if index slot is past end of heap */ index = descriptors_count;
u32 descriptors_count = ArenaCount(heap->descriptors_arena, G_D12_Descriptor);
if (index >= descriptors_count)
{
u32 pushed_count = index - descriptors_count + 1;
PushStructs(heap->descriptors_arena, G_D12_Descriptor, pushed_count);
for (u32 pushed_index = descriptors_count; pushed_index < descriptors_count + pushed_count; ++pushed_index)
{
G_D12_Descriptor *pushed = &(ArenaFirst(heap->descriptors_arena, G_D12_Descriptor)[pushed_index]);
if (pushed_index < index)
{
pushed->heap = heap;
pushed->index = pushed_index;
pushed->handle.ptr = heap->start_handle.ptr + (pushed_index * heap->descriptor_size);
DllStackPush(heap->first_free, pushed);
}
else
{
descriptor = pushed;
}
}
}
/* Search free list for freed descriptor with matching index */
if (!descriptor)
{
for (G_D12_Descriptor *n = heap->first_free; n; n = n->next)
{
if (n->index == index)
{
DllStackRemove(heap->first_free, n);
descriptor = n;
break;
}
}
}
if (!descriptor)
{
Arena *perm = PermArena();
Panic(StringF(perm, "Tried to force push a GPU pointer into slot %F, but a descriptor already exists there (current heap count: %F)", FmtUint(index), FmtUint(ArenaCount(heap->descriptors_arena, G_D12_Descriptor))));
}
} }
} }
Unlock(&lock); Unlock(&lock);
@ -1111,7 +1055,7 @@ u32 G_PushRef(G_ArenaHandle arena_handle, G_ResourceHandle resource_handle, G_Re
G_D12_Descriptor *descriptor = 0; G_D12_Descriptor *descriptor = 0;
if (is_buffer) if (is_buffer)
{ {
descriptor = G_D12_PushDescriptor(gpu_arena, G_D12_DescriptorHeapKind_CbvSrvUav, ref_desc.forced); descriptor = G_D12_PushDescriptor(gpu_arena, G_D12_DescriptorHeapKind_CbvSrvUav);
u64 num_elements_in_buffer = 0; u64 num_elements_in_buffer = 0;
u64 num_elements_after_offset = 0; u64 num_elements_after_offset = 0;
if (is_raw) if (is_raw)
@ -1168,7 +1112,7 @@ u32 G_PushRef(G_ArenaHandle arena_handle, G_ResourceHandle resource_handle, G_Re
} }
else if (is_texture) else if (is_texture)
{ {
descriptor = G_D12_PushDescriptor(gpu_arena, G_D12_DescriptorHeapKind_CbvSrvUav, ref_desc.forced); descriptor = G_D12_PushDescriptor(gpu_arena, G_D12_DescriptorHeapKind_CbvSrvUav);
if (is_uav) if (is_uav)
{ {
ID3D12Device_CreateUnorderedAccessView(g->device, resource->d3d_resource, 0, 0, descriptor->handle); ID3D12Device_CreateUnorderedAccessView(g->device, resource->d3d_resource, 0, 0, descriptor->handle);
@ -1180,7 +1124,7 @@ u32 G_PushRef(G_ArenaHandle arena_handle, G_ResourceHandle resource_handle, G_Re
} }
else if (is_sampler) else if (is_sampler)
{ {
descriptor = G_D12_PushDescriptor(gpu_arena, G_D12_DescriptorHeapKind_Sampler, ref_desc.forced); descriptor = G_D12_PushDescriptor(gpu_arena, G_D12_DescriptorHeapKind_Sampler);
G_SamplerResourceDesc sampler_desc = resource->sampler_desc; G_SamplerResourceDesc sampler_desc = resource->sampler_desc;
D3D12_SAMPLER_DESC d3d_desc = ZI; D3D12_SAMPLER_DESC d3d_desc = ZI;
{ {

View File

@ -417,7 +417,7 @@ G_D12_Queue *G_D12_QueueFromKind(G_QueueKind kind);
//~ Resource helpers //~ Resource helpers
G_D12_Descriptor *G_D12_DescriptorFromIndex(G_D12_DescriptorHeapKind heap_kind, u32 index); G_D12_Descriptor *G_D12_DescriptorFromIndex(G_D12_DescriptorHeapKind heap_kind, u32 index);
G_D12_Descriptor *G_D12_PushDescriptor(G_D12_Arena *gpu_arena, G_D12_DescriptorHeapKind heap_kind, u32 forced); G_D12_Descriptor *G_D12_PushDescriptor(G_D12_Arena *gpu_arena, G_D12_DescriptorHeapKind heap_kind);
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
//~ Raw command list //~ Raw command list

View File

@ -94,18 +94,6 @@ Struct(G_SamplerStateRef) { u32 v; };
template<typename T> Vec3U32 countof(RWTexture3D<T> tex) { Vec3U32 result; tex.GetDimensions(result.x, result.y, result.z); return result; } template<typename T> Vec3U32 countof(RWTexture3D<T> tex) { Vec3U32 result; tex.GetDimensions(result.x, result.y, result.z); return result; }
#endif #endif
////////////////////////////////////////////////////////////
//~ Static refs
#if IsLanguageC
#define G_StaticRef(type, v) ((type) { (v) })
#elif IsLanguageG
#define G_StaticRef(type, v) (type(v))
#endif
#define G_BasicPointSampler G_StaticRef(G_SamplerStateRef, 1)
#define G_BasicNoiseTexture G_StaticRef(G_Texture3DRef, 2)
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
//~ Reserved constants //~ Reserved constants

View File

@ -37,7 +37,7 @@ void PT_RunForever(WaveLaneCtx *lane)
G_SetConstant(cl, PT_ShaderConst_TestConst, 3.123); G_SetConstant(cl, PT_ShaderConst_TestConst, 3.123);
G_SetConstant(cl, PT_ShaderConst_BlitSampler, G_BasicPointSampler); G_SetConstant(cl, PT_ShaderConst_BlitSampler, G_BasicPointSampler);
G_SetConstant(cl, PT_ShaderConst_BlitSrc, final_target_rhandle); G_SetConstant(cl, PT_ShaderConst_BlitSrc, final_target_rhandle);
G_SetConstant(cl, PT_ShaderConst_NoiseTex, G_BasicNoiseTexture); G_SetConstant(cl, PT_ShaderConst_NoiseTex, G_BasicNoiseTexture());
} }
/* Test pass */ /* Test pass */

View File

@ -1388,8 +1388,9 @@ void UI_EndFrame(UI_Frame *frame)
UI_DParams params = ZI; UI_DParams params = ZI;
{ {
params.target_size = draw_size; params.target_size = draw_size;
params.target_ro = draw_target_ro; params.target_ro = draw_target_ro;
params.rects = rects_ro; params.rects = rects_ro;
params.sampler = G_BasicSampler();
} }
G_ResourceHandle params_buff = G_PushBufferFromString(frame->gpu_arena, frame->cl, StringFromStruct(&params)); G_ResourceHandle params_buff = G_PushBufferFromString(frame->gpu_arena, frame->cl, StringFromStruct(&params));
G_StructuredBufferRef params_ro = G_PushStructuredBufferRef(frame->gpu_arena, params_buff, UI_DParams); G_StructuredBufferRef params_ro = G_PushStructuredBufferRef(frame->gpu_arena, params_buff, UI_DParams);
@ -1416,7 +1417,7 @@ void UI_EndFrame(UI_Frame *frame)
/* Render rects */ /* Render rects */
G_Rasterize(frame->cl, G_Rasterize(frame->cl,
UI_DRectVS, UI_DRectPS, UI_DRectVS, UI_DRectPS,
1, G_GetSharedQuadIndices(), 1, G_QuadIndices(),
1, &draw_target, 1, &draw_target,
draw_viewport, draw_scissor, draw_viewport, draw_scissor,
G_RasterMode_TriangleList); G_RasterMode_TriangleList);
@ -1427,7 +1428,7 @@ void UI_EndFrame(UI_Frame *frame)
G_SetConstant(frame->cl, UI_ShaderConst_DebugDraw, 1); G_SetConstant(frame->cl, UI_ShaderConst_DebugDraw, 1);
G_Rasterize(frame->cl, G_Rasterize(frame->cl,
UI_DRectVS, UI_DRectPS, UI_DRectVS, UI_DRectPS,
1, G_GetSharedQuadIndices(), 1, G_QuadIndices(),
1, &draw_target, 1, &draw_target,
draw_viewport, draw_scissor, draw_viewport, draw_scissor,
G_RasterMode_WireTriangleList); G_RasterMode_WireTriangleList);
@ -1442,7 +1443,7 @@ void UI_EndFrame(UI_Frame *frame)
{ {
G_Rasterize(frame->cl, G_Rasterize(frame->cl,
UI_BlitVS, UI_BlitPS, UI_BlitVS, UI_BlitPS,
1, G_GetSharedQuadIndices(), 1, G_QuadIndices(),
1, &backbuffer, 1, &backbuffer,
draw_viewport, draw_scissor, draw_viewport, draw_scissor,
G_RasterMode_TriangleList); G_RasterMode_TriangleList);

View File

@ -1,14 +1,15 @@
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
//~ Constant types //~ Constant types
G_DeclConstant(G_StructuredBufferRef, UI_ShaderConst_Params, 0); G_DeclConstant(G_StructuredBufferRef, UI_ShaderConst_Params, 0);
G_DeclConstant(b32, UI_ShaderConst_DebugDraw, 1); G_DeclConstant(b32, UI_ShaderConst_DebugDraw, 1);
Struct(UI_DParams) Struct(UI_DParams)
{ {
Vec2I32 target_size; Vec2I32 target_size;
G_Texture2DRef target_ro; G_Texture2DRef target_ro;
G_StructuredBufferRef rects; G_StructuredBufferRef rects;
G_SamplerStateRef sampler;
}; };
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////

View File

@ -33,7 +33,7 @@ PixelShader(UI_DRectPS, UI_DRectPSOutput, UI_DRectPSInput input)
{ {
UI_DParams params = G_StructuredBufferFromRef<UI_DParams>(UI_ShaderConst_Params)[0]; UI_DParams params = G_StructuredBufferFromRef<UI_DParams>(UI_ShaderConst_Params)[0];
StructuredBuffer<UI_DRect> rects = G_StructuredBufferFromRef<UI_DRect>(params.rects); StructuredBuffer<UI_DRect> rects = G_StructuredBufferFromRef<UI_DRect>(params.rects);
SamplerState sampler = G_SamplerStateFromRef(G_BasicPointSampler); SamplerState sampler = G_SamplerStateFromRef(params.sampler);
UI_DRect rect = rects[input.rect_idx]; UI_DRect rect = rects[input.rect_idx];
@ -140,7 +140,7 @@ PixelShader(UI_BlitPS, UI_BlitPSOutput, UI_BlitPSInput input)
{ {
UI_DParams params = G_StructuredBufferFromRef<UI_DParams>(UI_ShaderConst_Params)[0]; UI_DParams params = G_StructuredBufferFromRef<UI_DParams>(UI_ShaderConst_Params)[0];
Texture2D<Vec4> tex = G_Texture2DFromRef<Vec4>(params.target_ro); Texture2D<Vec4> tex = G_Texture2DFromRef<Vec4>(params.target_ro);
SamplerState sampler = G_SamplerStateFromRef(G_BasicPointSampler); SamplerState sampler = G_SamplerStateFromRef(params.sampler);
Vec2 uv = input.src_uv; Vec2 uv = input.src_uv;
Vec4 result = tex.Sample(sampler, uv); Vec4 result = tex.Sample(sampler, uv);