diff --git a/src/gpu/gpu_common.c b/src/gpu/gpu_common.c index e25395d9..26804625 100644 --- a/src/gpu/gpu_common.c +++ b/src/gpu/gpu_common.c @@ -28,7 +28,7 @@ void G_BootstrapCommon(void) /* Init point sampler */ { 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 */ @@ -49,7 +49,7 @@ void G_BootstrapCommon(void) noise_data.text, 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 -G_IndexBufferDesc G_GetSharedQuadIndices(void) +G_IndexBufferDesc G_QuadIndices(void) { 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; +} diff --git a/src/gpu/gpu_common.h b/src/gpu/gpu_common.h index d0c6eef0..33cd588e 100644 --- a/src/gpu/gpu_common.h +++ b/src/gpu/gpu_common.h @@ -5,6 +5,8 @@ Struct(G_SharedUtilState) { /* Common shared resources */ G_IndexBufferDesc quad_indices; + G_SamplerStateRef basic_sampler; + G_Texture3DRef basic_noise; } extern G_shared_util_state; extern ThreadLocal G_ArenaHandle G_t_perm_arena; @@ -34,4 +36,6 @@ Rng2 G_ScissorFromTexture(G_ResourceHandle texture); //- Shared resources -G_IndexBufferDesc G_GetSharedQuadIndices(void); +G_IndexBufferDesc G_QuadIndices(void); +G_SamplerStateRef G_BasicSampler(void); +G_Texture3DRef G_BasicNoiseTexture(void); diff --git a/src/gpu/gpu_core.h b/src/gpu/gpu_core.h index 734f6a20..a0b3dfb6 100644 --- a/src/gpu/gpu_core.h +++ b/src/gpu/gpu_core.h @@ -433,14 +433,6 @@ Struct(G_RefDesc) G_RefKind kind; u64 element_size; 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; }; //////////////////////////////////////////////////////////// diff --git a/src/gpu/gpu_dx12/gpu_dx12_core.c b/src/gpu/gpu_dx12/gpu_dx12_core.c index c55f2ba7..7990c0db 100644 --- a/src/gpu/gpu_dx12/gpu_dx12_core.c +++ b/src/gpu/gpu_dx12/gpu_dx12_core.c @@ -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 */ 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); } @@ -650,9 +650,9 @@ G_D12_RawCommandList *G_D12_PrepareRawCommandList(G_QueueKind queue_kind) G_D12_Arena *gpu_perm = G_D12_ArenaFromHandle(G_PermArena()); 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]; } -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_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; /* 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]; - for (G_QueueKind queue_kind = 0; !descriptor && queue_kind < G_NumQueues; ++queue_kind) + G_D12_DescriptorList *descriptors = &descriptors_by_queue[queue_kind]; + descriptor = descriptors->first; + if (descriptor) { - G_D12_DescriptorList *descriptors = &descriptors_by_queue[queue_kind]; - descriptor = descriptors->first; - if (descriptor) + G_D12_Queue *queue = G_D12_QueueFromKind(queue_kind); + u64 queue_commit_completion = ID3D12Fence_GetCompletedValue(queue->commit_fence); + if (queue_commit_completion >= descriptor->queue_commit_target) { - G_D12_Queue *queue = G_D12_QueueFromKind(queue_kind); - u64 queue_commit_completion = ID3D12Fence_GetCompletedValue(queue->commit_fence); - if (queue_commit_completion >= descriptor->queue_commit_target) - { - /* Descriptor no longer in use by gpu, reuse it */ - DllQueueRemove(descriptors->first, descriptors->last, descriptor); - } - else - { - /* Descriptor may still be in use by gpu */ - descriptor = 0; - } + /* Descriptor no longer in use by gpu, reuse it */ + DllQueueRemove(descriptors->first, descriptors->last, descriptor); + } + else + { + /* Descriptor may still be in use by gpu */ + descriptor = 0; } } } /* Allocate new descriptor from heap */ - u32 index = forced; + u32 index = 0; if (!descriptor) { 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); - 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; - } + descriptor = heap->first_free; + DllStackRemove(heap->first_free, descriptor); + index = descriptor->index; } 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")); } - - /* Push descriptors if index slot is past end of heap */ - 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)))); - } + descriptor = PushStruct(heap->descriptors_arena, G_D12_Descriptor); + index = descriptors_count; } } Unlock(&lock); @@ -1111,7 +1055,7 @@ u32 G_PushRef(G_ArenaHandle arena_handle, G_ResourceHandle resource_handle, G_Re G_D12_Descriptor *descriptor = 0; 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_after_offset = 0; if (is_raw) @@ -1168,7 +1112,7 @@ u32 G_PushRef(G_ArenaHandle arena_handle, G_ResourceHandle resource_handle, G_Re } 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) { 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) { - 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; D3D12_SAMPLER_DESC d3d_desc = ZI; { diff --git a/src/gpu/gpu_dx12/gpu_dx12_core.h b/src/gpu/gpu_dx12/gpu_dx12_core.h index fd5e49dc..8498e532 100644 --- a/src/gpu/gpu_dx12/gpu_dx12_core.h +++ b/src/gpu/gpu_dx12/gpu_dx12_core.h @@ -417,7 +417,7 @@ G_D12_Queue *G_D12_QueueFromKind(G_QueueKind kind); //~ Resource helpers 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 diff --git a/src/gpu/gpu_shader_core.cgh b/src/gpu/gpu_shader_core.cgh index e6653d9a..53d375c8 100644 --- a/src/gpu/gpu_shader_core.cgh +++ b/src/gpu/gpu_shader_core.cgh @@ -94,18 +94,6 @@ Struct(G_SamplerStateRef) { u32 v; }; template Vec3U32 countof(RWTexture3D tex) { Vec3U32 result; tex.GetDimensions(result.x, result.y, result.z); return result; } #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 diff --git a/src/proto/proto.c b/src/proto/proto.c index 84fa845a..4dff77a3 100644 --- a/src/proto/proto.c +++ b/src/proto/proto.c @@ -37,7 +37,7 @@ void PT_RunForever(WaveLaneCtx *lane) G_SetConstant(cl, PT_ShaderConst_TestConst, 3.123); G_SetConstant(cl, PT_ShaderConst_BlitSampler, G_BasicPointSampler); 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 */ diff --git a/src/ui/ui_core.c b/src/ui/ui_core.c index 235ca133..ec091e67 100644 --- a/src/ui/ui_core.c +++ b/src/ui/ui_core.c @@ -1388,8 +1388,9 @@ void UI_EndFrame(UI_Frame *frame) UI_DParams params = ZI; { params.target_size = draw_size; - params.target_ro = draw_target_ro; - params.rects = rects_ro; + params.target_ro = draw_target_ro; + params.rects = rects_ro; + params.sampler = G_BasicSampler(); } G_ResourceHandle params_buff = G_PushBufferFromString(frame->gpu_arena, frame->cl, StringFromStruct(¶ms)); 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 */ G_Rasterize(frame->cl, UI_DRectVS, UI_DRectPS, - 1, G_GetSharedQuadIndices(), + 1, G_QuadIndices(), 1, &draw_target, draw_viewport, draw_scissor, G_RasterMode_TriangleList); @@ -1427,7 +1428,7 @@ void UI_EndFrame(UI_Frame *frame) G_SetConstant(frame->cl, UI_ShaderConst_DebugDraw, 1); G_Rasterize(frame->cl, UI_DRectVS, UI_DRectPS, - 1, G_GetSharedQuadIndices(), + 1, G_QuadIndices(), 1, &draw_target, draw_viewport, draw_scissor, G_RasterMode_WireTriangleList); @@ -1442,7 +1443,7 @@ void UI_EndFrame(UI_Frame *frame) { G_Rasterize(frame->cl, UI_BlitVS, UI_BlitPS, - 1, G_GetSharedQuadIndices(), + 1, G_QuadIndices(), 1, &backbuffer, draw_viewport, draw_scissor, G_RasterMode_TriangleList); diff --git a/src/ui/ui_shaders.cgh b/src/ui/ui_shaders.cgh index 5438c51d..9221a58c 100644 --- a/src/ui/ui_shaders.cgh +++ b/src/ui/ui_shaders.cgh @@ -1,14 +1,15 @@ //////////////////////////////////////////////////////////// //~ Constant types -G_DeclConstant(G_StructuredBufferRef, UI_ShaderConst_Params, 0); -G_DeclConstant(b32, UI_ShaderConst_DebugDraw, 1); +G_DeclConstant(G_StructuredBufferRef, UI_ShaderConst_Params, 0); +G_DeclConstant(b32, UI_ShaderConst_DebugDraw, 1); Struct(UI_DParams) { - Vec2I32 target_size; - G_Texture2DRef target_ro; + Vec2I32 target_size; + G_Texture2DRef target_ro; G_StructuredBufferRef rects; + G_SamplerStateRef sampler; }; //////////////////////////////////////////////////////////// diff --git a/src/ui/ui_shaders.g b/src/ui/ui_shaders.g index d04a4131..a72ceb2f 100644 --- a/src/ui/ui_shaders.g +++ b/src/ui/ui_shaders.g @@ -33,7 +33,7 @@ PixelShader(UI_DRectPS, UI_DRectPSOutput, UI_DRectPSInput input) { UI_DParams params = G_StructuredBufferFromRef(UI_ShaderConst_Params)[0]; StructuredBuffer rects = G_StructuredBufferFromRef(params.rects); - SamplerState sampler = G_SamplerStateFromRef(G_BasicPointSampler); + SamplerState sampler = G_SamplerStateFromRef(params.sampler); 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_ShaderConst_Params)[0]; Texture2D tex = G_Texture2DFromRef(params.target_ro); - SamplerState sampler = G_SamplerStateFromRef(G_BasicPointSampler); + SamplerState sampler = G_SamplerStateFromRef(params.sampler); Vec2 uv = input.src_uv; Vec4 result = tex.Sample(sampler, uv);