From 8fbcb004fbe610f55f0c5a2955449e23959a24e2 Mon Sep 17 00:00:00 2001 From: jacob Date: Tue, 25 Nov 2025 05:58:38 -0600 Subject: [PATCH] parameter-isolated root constants --- src/base/base.h | 80 ++++++++--------------- src/base/base_entry.h | 6 -- src/base/base_gpu.h | 55 ++++++---------- src/gpu/gpu_common.c | 2 +- src/gpu/gpu_common.h | 4 +- src/gpu/gpu_core.h | 39 ++++------- src/gpu/gpu_dx12/gpu_dx12.c | 127 +++++++++++------------------------- src/gpu/gpu_dx12/gpu_dx12.h | 5 -- src/proto/proto.c | 4 +- src/proto/proto_shaders.gpu | 9 +-- src/proto/proto_shaders.h | 9 ++- 11 files changed, 113 insertions(+), 227 deletions(-) diff --git a/src/base/base.h b/src/base/base.h index 421141a2..3ade035c 100644 --- a/src/base/base.h +++ b/src/base/base.h @@ -510,11 +510,10 @@ typedef double f64; typedef i8 b8; typedef u32 b32; + Struct(U128) { u64 hi; u64 lo; }; #elif IsLanguageGpu typedef int i32; - typedef int2 i64; typedef uint u32; - typedef uint2 u64; typedef float f32; typedef uint b32; #endif @@ -572,7 +571,7 @@ AlignedStruct(Atomic16Padded, CachelineSize) { Atomic16 v; }; AlignedStruct(Atomic32Padded, CachelineSize) { Atomic32 v; }; AlignedStruct(Atomic64Padded, CachelineSize) { Atomic64 v; }; - StaticAssert(alignof(Atomic8Padded) == CachelineSize && sizeof(Atomic8Padded) % CachelineSize == 0); + StaticAssert(alignof(Atomic8Padded) == CachelineSize && sizeof(Atomic8Padded) % CachelineSize == 0); StaticAssert(alignof(Atomic16Padded) == CachelineSize && sizeof(Atomic16Padded) % CachelineSize == 0); StaticAssert(alignof(Atomic32Padded) == CachelineSize && sizeof(Atomic32Padded) % CachelineSize == 0); StaticAssert(alignof(Atomic64Padded) == CachelineSize && sizeof(Atomic64Padded) % CachelineSize == 0); @@ -698,15 +697,6 @@ }; #endif -//////////////////////////////////////////////////////////// -//~ U128 types - -Struct(U128) -{ - u64 hi; - u64 lo; -}; - //////////////////////////////////////////////////////////// //~ Resource types @@ -727,59 +717,47 @@ Struct(U128) //////////////////////////////////////////////////////////// //~ Shader types -/* D3D12 - 64 maximum root constants - * Vulkan - 32 maximum push constants - */ -#define MaxShaderConstants (32) - -#define ShaderConstant(type, name) name##__shaderconst__##type - +//- Shader linkage #if IsLanguageC - //- Shader linkage - Struct(VertexShader) { ResourceKey resource; }; Struct(PixelShader) { ResourceKey resource; }; Struct(ComputeShader) { ResourceKey resource; }; - - //- Shader object handles - - Struct(StructuredBufferHandle) { u32 v; }; - Struct(RWStructuredBufferHandle) { u32 v; }; - Struct(Texture1DHandle) { u32 v; }; - Struct(RWTexture1DHandle) { u32 v; }; - Struct(Texture2DHandle) { u32 v; }; - Struct(RWTexture2DHandle) { u32 v; }; - Struct(Texture3DHandle) { u32 v; }; - Struct(RWTexture3DHandle) { u32 v; }; - Struct(SamplerHandle) { u32 v; }; - - #define IsGpuHandleNil(h) ((h).v == 0) #elif IsLanguageGpu - //- Shader declaration - + #define Semantic(t, n) t n : n #define ComputeShader(name, x) [numthreads(x, 1, 1)] void name(Semantic(u32, SV_DispatchThreadID)) #define ComputeShader2D(name, x, y) [numthreads(x, y, 1)] void name(Semantic(Vec2U32, SV_DispatchThreadID)) #define ComputeShader3D(name, x, y, z) [numthreads(x, y, z)] void name(Semantic(Vec3U32, SV_DispatchThreadID)) #define VertexShader(name, return_type) return_type name(Semantic(u32, SV_InstanceID), Semantic(u32, SV_VertexID)) #define PixelShader(name, return_type, ...) return_type name(__VA_ARGS__) +#endif - //- Semantic declaration +//- Shader resource handles +Struct(StructuredBufferHandle) { u32 v; }; +Struct(RWStructuredBufferHandle) { u32 v; }; +Struct(Texture1DHandle) { u32 v; }; +Struct(RWTexture1DHandle) { u32 v; }; +Struct(Texture2DHandle) { u32 v; }; +Struct(RWTexture2DHandle) { u32 v; }; +Struct(Texture3DHandle) { u32 v; }; +Struct(RWTexture3DHandle) { u32 v; }; +Struct(SamplerStateHandle) { u32 v; }; +#define IsGpuHandleNil(h) ((h).v == 0) - # define Semantic(t, n) t n : n +//- Shader constants - //- Shader object handles +/* D3D12 - 64 maximum root constants + * Vulkan - 32 maximum push constants + */ +#define MaxShaderConstants (32) - typedef u32 StructuredBufferHandle; - typedef u32 RWStructuredBufferHandle; - typedef u32 Texture1DHandle; - typedef u32 RWTexture1DHandle; - typedef u32 Texture2DHandle; - typedef u32 RWTexture2DHandle; - typedef u32 Texture3DHandle; - typedef u32 RWTexture3DHandle; - typedef u32 SamplerHandle; - - #define IsGpuHandleNil(h) ((h) == 0) +#if IsLanguageC + #define ShaderConstant(type, name, slot) \ + StaticAssert(sizeof(type) <= 4); \ + StaticAssert(slot < MaxShaderConstants); \ + Enum(name##__shaderconstantenum) { name = slot }; \ + Struct(name##__shaderconstanttype) { type v; } +#elif IsLanguageGpu + #define ShaderConstant(type, name, slot) cbuffer name : register(b##slot) { type name; } #endif //////////////////////////////////////////////////////////// diff --git a/src/base/base_entry.h b/src/base/base_entry.h index e7894cad..c4080162 100644 --- a/src/base/base_entry.h +++ b/src/base/base_entry.h @@ -1,9 +1,3 @@ -//////////////////////////////////////////////////////////// -//~ Exit callback types - -#define ExitFuncDef(name) void name(void) -typedef ExitFuncDef(ExitFunc); - //////////////////////////////////////////////////////////// //~ @hookdecl Swap hooks diff --git a/src/base/base_gpu.h b/src/base/base_gpu.h index 3e724873..262723ed 100644 --- a/src/base/base_gpu.h +++ b/src/base/base_gpu.h @@ -21,45 +21,30 @@ typedef float4 Aabb; typedef float4 Quad; typedef float4x4 Mat4x4; -//////////////////////////////////////////////////////////// -//~ Root constants - -Struct(RootConstants_f32) { f32 c[MaxShaderConstants]; }; -Struct(RootConstants_u32) { u32 c[MaxShaderConstants]; }; -Struct(RootConstants_StructuredBufferHandle) { StructuredBufferHandle c[MaxShaderConstants]; }; -Struct(RootConstants_RWStructuredBufferHandle) { RWStructuredBufferHandle c[MaxShaderConstants]; }; -Struct(RootConstants_Texture1DHandle) { Texture1DHandle c[MaxShaderConstants]; }; -Struct(RootConstants_RWTexture1DHandle) { RWTexture1DHandle c[MaxShaderConstants]; }; -Struct(RootConstants_Texture2DHandle) { Texture2DHandle c[MaxShaderConstants]; }; -Struct(RootConstants_RWTexture2DHandle) { RWTexture2DHandle c[MaxShaderConstants]; }; -Struct(RootConstants_Texture3DHandle) { Texture3DHandle c[MaxShaderConstants]; }; -Struct(RootConstants_RWTexture3DHandle) { RWTexture3DHandle c[MaxShaderConstants]; }; -Struct(RootConstants_SamplerHandle) { SamplerHandle c[MaxShaderConstants]; }; - -ConstantBuffer g_root_constants_f32 : register(b0); -ConstantBuffer g_root_constants_u32 : register(b0); -ConstantBuffer g_root_constants_StructuredBufferHandle : register(b0); -ConstantBuffer g_root_constants_RWStructuredBufferHandle : register(b0); -ConstantBuffer g_root_constants_Texture1DHandle : register(b0); -ConstantBuffer g_root_constants_RWTexture1DHandle : register(b0); -ConstantBuffer g_root_constants_Texture2DHandle : register(b0); -ConstantBuffer g_root_constants_RWTexture2DHandle : register(b0); -ConstantBuffer g_root_constants_Texture3DHandle : register(b0); -ConstantBuffer g_root_constants_RWTexture3DHandle : register(b0); -ConstantBuffer g_root_constants_SamplerHandle : register(b0); - -#define GetConstant(type, name) (g_root_constants_##type.c[ShaderConstant(type, name)]) - //////////////////////////////////////////////////////////// //~ Handle dereference -#define ResourceFromUniformHandle(h) ResourceDescriptorHeap[(h)] -#define ResourceFromNonUniformHandle(h) ResourceDescriptorHeap[NonUniformResourceIndex(h)] -#define ResourceFromConstantHandle(type, name) ResourceFromUniformHandle(GetConstant(type, name)) +//- Uniform resource access +template StructuredBuffer StructuredBufferFromUniformHandle(StructuredBufferHandle h) { return ResourceDescriptorHeap[h.v]; } +template RWStructuredBuffer RWStructuredBufferFromUniformHandle(RWStructuredBufferHandle h) { return ResourceDescriptorHeap[h.v]; } +template Texture1D Texture1DFromUniformHandle(Texture1DHandle h) { return ResourceDescriptorHeap[h.v]; } +template RWTexture1D RWTexture1DFromUniformHandle(RWTexture1DHandle h) { return ResourceDescriptorHeap[h.v]; } +template Texture2D Texture2DFromUniformHandle(Texture2DHandle h) { return ResourceDescriptorHeap[h.v]; } +template RWTexture2D RWTexture2DFromUniformHandle(RWTexture2DHandle h) { return ResourceDescriptorHeap[h.v]; } +template Texture3D Texture3DFromUniformHandle(Texture3DHandle h) { return ResourceDescriptorHeap[h.v]; } +template RWTexture3D RWTexture3DFromUniformHandle(RWTexture3DHandle h) { return ResourceDescriptorHeap[h.v]; } +SamplerState SamplerStateFromUniformHandle(SamplerStateHandle h) { return SamplerDescriptorHeap[h.v]; } -#define SamplerFromUniformHandle(h) SamplerDescriptorHeap[(h)] -#define SamplerFromNonUniformHandle(h) SamplerDescriptorHeap[NonUniformResourceIndex(h)] -#define SamplerFromConstantHandle(type, name) SamplerFromUniformHandle(GetConstant(type, name)) +//- Non-uniform resource access +template StructuredBuffer StructuredBufferFromNonUniformHandle(StructuredBufferHandle h) { return ResourceDescriptorHeap[NonUniformResourceIndex(h.v)]; } +template RWStructuredBuffer RWStructuredBufferFromNonUniformHandle(RWStructuredBufferHandle h) { return ResourceDescriptorHeap[NonUniformResourceIndex(h.v)]; } +template Texture1D Texture1DFromNonUniformHandle(Texture1DHandle h) { return ResourceDescriptorHeap[NonUniformResourceIndex(h.v)]; } +template RWTexture1D RWTexture1DFromNonUniformHandle(RWTexture1DHandle h) { return ResourceDescriptorHeap[NonUniformResourceIndex(h.v)]; } +template Texture2D Texture2DFromNonUniformHandle(Texture2DHandle h) { return ResourceDescriptorHeap[NonUniformResourceIndex(h.v)]; } +template RWTexture2D RWTexture2DFromNonUniformHandle(RWTexture2DHandle h) { return ResourceDescriptorHeap[NonUniformResourceIndex(h.v)]; } +template Texture3D Texture3DFromNonUniformHandle(Texture3DHandle h) { return ResourceDescriptorHeap[NonUniformResourceIndex(h.v)]; } +template RWTexture3D RWTexture3DFromNonUniformHandle(RWTexture3DHandle h) { return ResourceDescriptorHeap[NonUniformResourceIndex(h.v)]; } +SamplerState SamplerStateFromNonUniformHandle(SamplerStateHandle h) { return SamplerDescriptorHeap[NonUniformResourceIndex(h.v)]; } //////////////////////////////////////////////////////////// //~ Texture dimension helpers diff --git a/src/gpu/gpu_common.c b/src/gpu/gpu_common.c index f3cc718e..67533d50 100644 --- a/src/gpu/gpu_common.c +++ b/src/gpu/gpu_common.c @@ -72,7 +72,7 @@ void GPU_CopyResourceFromCpu(GPU_CommandListHandle cl, GPU_ResourceHandle dst, S //////////////////////////////////////////////////////////// //~ Common resource helpers -SamplerHandle GPU_GetCommonPointSampler(void) +SamplerStateHandle GPU_GetCommonPointSampler(void) { return GPU_shared_util_state.pt_sampler; } diff --git a/src/gpu/gpu_common.h b/src/gpu/gpu_common.h index 6644ee5d..576586db 100644 --- a/src/gpu/gpu_common.h +++ b/src/gpu/gpu_common.h @@ -4,7 +4,7 @@ Struct(GPU_SharedUtilState) { /* Common shared resources */ - SamplerHandle pt_sampler; + SamplerStateHandle pt_sampler; GPU_IndexBufferDesc quad_indices; Texture3DHandle noise_tex; @@ -29,6 +29,6 @@ void GPU_CopyResourceFromCpu(GPU_CommandListHandle cl, GPU_ResourceHandle dst, S //////////////////////////////////////////////////////////// //~ Common resource helpers -SamplerHandle GPU_GetCommonPointSampler(void); +SamplerStateHandle GPU_GetCommonPointSampler(void); GPU_IndexBufferDesc GPU_GetCommonQuadIndices(void); Texture3DHandle GPU_GetCommonNoise(void); diff --git a/src/gpu/gpu_core.h b/src/gpu/gpu_core.h index f68e56c9..db5464e4 100644 --- a/src/gpu/gpu_core.h +++ b/src/gpu/gpu_core.h @@ -543,7 +543,7 @@ b32 GPU_IsResourceNil(GPU_ResourceHandle handle); } \ ) -//- Shader handle creation +//- Shader resource handle creation StructuredBufferHandle GPU_PushStructuredBufferHandleEx (GPU_ArenaHandle arena, GPU_ResourceHandle resource, u32 element_size, RngU32 element_range); RWStructuredBufferHandle GPU_PushRWStructuredBufferHandleEx (GPU_ArenaHandle arena, GPU_ResourceHandle resource, u32 element_size, RngU32 element_range); @@ -553,19 +553,19 @@ Texture2DHandle GPU_PushTexture2DHandle (GPU_ArenaHandle RWTexture2DHandle GPU_PushRWTexture2DHandle (GPU_ArenaHandle arena, GPU_ResourceHandle resource); Texture3DHandle GPU_PushTexture3DHandle (GPU_ArenaHandle arena, GPU_ResourceHandle resource); RWTexture3DHandle GPU_PushRWTexture3DHandle (GPU_ArenaHandle arena, GPU_ResourceHandle resource); -SamplerHandle GPU_PushSamplerHandle (GPU_ArenaHandle arena, GPU_ResourceHandle resource); +SamplerStateHandle GPU_PushSamplerStateHandle (GPU_ArenaHandle arena, GPU_ResourceHandle resource); #define GPU_PushStructuredBufferHandle(arena, resource, type) GPU_PushStructuredBufferHandleEx((arena), (resource), sizeof(type), RNGU32(0, GPU_CountBuffer((resource), type))) #define GPU_PushRWStructuredBufferHandle(arena, resource, type) GPU_PushRWStructuredBufferHandleEx((arena), (resource), sizeof(type), RNGU32(0, GPU_CountBuffer((resource), type))) //- Count -u64 GPU_CountBufferEx(GPU_ResourceHandle buffer, u64 element_size); +u64 GPU_CountBufferBytes(GPU_ResourceHandle buffer); u64 GPU_Count1D(GPU_ResourceHandle texture1d); u64 GPU_Count2D(GPU_ResourceHandle texture2d); u64 GPU_Count3D(GPU_ResourceHandle texture3d); -#define GPU_CountBuffer(buffer, type) GPU_CountBufferEx((buffer), sizeof(type)) +#define GPU_CountBuffer(buffer, type) GPU_CountBufferSize(buffer) / sizeof(type) //////////////////////////////////////////////////////////// //~ @hookdecl Command @@ -574,6 +574,7 @@ u64 GPU_Count3D(GPU_ResourceHandle texture3d); GPU_CommandListHandle GPU_PrepareCommandList(void); void GPU_CommitCommandListEx(GPU_CommandListHandle cl, GPU_QueueKind queue, u64 fence_ops_count, GPU_FenceOp *fence_ops); + #define GPU_CommitCommandList(cl, queue) GPU_CommitCommandListEx((cl), (queue), 0, 0) //- Arena @@ -590,31 +591,15 @@ void GPU_CopyCpuTexels(GPU_CommandListHandle cl, GPU_ResourceHandle dst, Vec3I32 void GPU_CopyBytes(GPU_CommandListHandle cl, GPU_ResourceHandle dst, u64 dst_offset, GPU_ResourceHandle src, RngU64 src_copy_range); void GPU_CopyTexels(GPU_CommandListHandle cl, GPU_ResourceHandle dst, Vec3I32 dst_offset, GPU_ResourceHandle src, Rng3I32 src_copy_range); -//- Constants +//- Constant -void GPU_SetConstantU32_ (GPU_CommandListHandle cl, i32 slot, u32 v); -void GPU_SetConstantF32_ (GPU_CommandListHandle cl, i32 slot, f32 v); -void GPU_SetConstantStructuredBufferHandle_ (GPU_CommandListHandle cl, i32 slot, StructuredBufferHandle v); -void GPU_SetConstantRWStructuredBufferHandle_ (GPU_CommandListHandle cl, i32 slot, RWStructuredBufferHandle v); -void GPU_SetConstantTexture1DHandle_ (GPU_CommandListHandle cl, i32 slot, Texture1DHandle v); -void GPU_SetConstantRWTexture1DHandle_ (GPU_CommandListHandle cl, i32 slot, RWTexture1DHandle v); -void GPU_SetConstantTexture2DHandle_ (GPU_CommandListHandle cl, i32 slot, Texture2DHandle v); -void GPU_SetConstantRWTexture2DHandle_ (GPU_CommandListHandle cl, i32 slot, RWTexture2DHandle v); -void GPU_SetConstantTexture3DHandle_ (GPU_CommandListHandle cl, i32 slot, Texture3DHandle v); -void GPU_SetConstantRWTexture3DHandle_ (GPU_CommandListHandle cl, i32 slot, RWTexture3DHandle v); -void GPU_SetConstantSamplerHandle_ (GPU_CommandListHandle cl, i32 slot, SamplerHandle v); +void GPU_SetConstant_(GPU_CommandListHandle cl, i32 slot, void *src_32bit, u32 size); -#define GPU_SetConstantU32(cl, name, v) GPU_SetConstantU32_((cl), ShaderConstant(u32, name), (v)) -#define GPU_SetConstantF32(cl, name, v) GPU_SetConstantF32_((cl), ShaderConstant(f32, name), (v)) -#define GPU_SetConstantStructuredBufferHandle(cl, name, v) GPU_SetConstantStructuredBufferHandle_((cl), ShaderConstant(StructuredBufferHandle, name), (v)) -#define GPU_SetConstantRWStructuredBufferHandle(cl, name, v) GPU_SetConstantRWStructuredBufferHandle_((cl), ShaderConstant(RWStructuredBufferHandle, name), (v)) -#define GPU_SetConstantTexture1DHandle(cl, name, v) GPU_SetConstantTexture1DHandle_((cl), ShaderConstant(Texture1DHandle, name), (v)) -#define GPU_SetConstantRWTexture1DHandle(cl, name, v) GPU_SetConstantRWTexture1DHandle_((cl), ShaderConstant(RWTexture1DHandle, name), (v)) -#define GPU_SetConstantTexture2DHandle(cl, name, v) GPU_SetConstantTexture2DHandle_((cl), ShaderConstant(Texture2DHandle, name), (v)) -#define GPU_SetConstantRWTexture2DHandle(cl, name, v) GPU_SetConstantRWTexture2DHandle_((cl), ShaderConstant(RWTexture2DHandle, name), (v)) -#define GPU_SetConstantTexture3DHandle(cl, name, v) GPU_SetConstantTexture3DHandle_((cl), ShaderConstant(Texture3DHandle, name), (v)) -#define GPU_SetConstantRWTexture3DHandle(cl, name, v) GPU_SetConstantRWTexture3DHandle_((cl), ShaderConstant(RWTexture3DHandle, name), (v)) -#define GPU_SetConstantSamplerHandle(cl, name, v) GPU_SetConstantSamplerHandle_((cl), ShaderConstant(SamplerHandle, name), (v)) +#define GPU_SetConstant(cl, name, value) do { \ + name##__shaderconstanttype __src; \ + __src.v = value; \ + GPU_SetConstant_((cl), (name), &__src, sizeof(__src)); \ +} while (0) //- Barrier diff --git a/src/gpu/gpu_dx12/gpu_dx12.c b/src/gpu/gpu_dx12/gpu_dx12.c index 449709e8..60914303 100644 --- a/src/gpu/gpu_dx12/gpu_dx12.c +++ b/src/gpu/gpu_dx12/gpu_dx12.c @@ -1,9 +1,9 @@ GPU_D12_SharedState GPU_D12_shared_state = ZI; //////////////////////////////////////////////////////////// -//~ Startup +//~ @hookimpl Startup -void GPU_D12_Startup(void) +void GPU_Startup(void) { GPU_D12_SharedState *g = &GPU_D12_shared_state; TempArena scratch = BeginScratchNoConflict(); @@ -254,16 +254,20 @@ void GPU_D12_Startup(void) { __profn("Serialize root signature"); - D3D12_ROOT_PARAMETER param = ZI; - param.ParameterType = D3D12_ROOT_PARAMETER_TYPE_32BIT_CONSTANTS; - param.ShaderVisibility = D3D12_SHADER_VISIBILITY_ALL; - param.Constants.ShaderRegister = 0; - param.Constants.RegisterSpace = 0; - param.Constants.Num32BitValues = 64; + D3D12_ROOT_PARAMETER params[MaxShaderConstants] = ZI; + for (i32 slot = 0; slot < MaxShaderConstants; ++slot) + { + D3D12_ROOT_PARAMETER *param = ¶ms[slot]; + param->ParameterType = D3D12_ROOT_PARAMETER_TYPE_32BIT_CONSTANTS; + param->ShaderVisibility = D3D12_SHADER_VISIBILITY_ALL; + param->Constants.ShaderRegister = slot; + param->Constants.RegisterSpace = 0; + param->Constants.Num32BitValues = 1; + } D3D12_ROOT_SIGNATURE_DESC desc = ZI; - desc.NumParameters = 1; - desc.pParameters = ¶m; + desc.NumParameters = countof(params); + desc.pParameters = params; desc.NumStaticSamplers = 0; desc.pStaticSamplers = 0; desc.Flags = D3D12_ROOT_SIGNATURE_FLAG_CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED | D3D12_ROOT_SIGNATURE_FLAG_SAMPLER_HEAP_DIRECTLY_INDEXED; @@ -729,14 +733,6 @@ void GPU_D12_CommitRawCommandList(GPU_D12_RawCommandList *cl) // } // } -//////////////////////////////////////////////////////////// -//~ @hookimpl Startup hook - -void GPU_Startup(void) -{ - GPU_D12_Startup(); -} - //////////////////////////////////////////////////////////// //~ @hookimpl Fence hooks @@ -1312,15 +1308,15 @@ RWTexture3DHandle GPU_PushRWTexture3DHandle(GPU_ArenaHandle arena, GPU_ResourceH return (RWTexture3DHandle) { 0 }; } -SamplerHandle GPU_PushSamplerHandle(GPU_ArenaHandle arena, GPU_ResourceHandle resource) +SamplerStateHandle GPU_PushSamplerStateHandle(GPU_ArenaHandle arena, GPU_ResourceHandle resource) { /* TODO */ - return (SamplerHandle) { 0 }; + return (SamplerStateHandle) { 0 }; } //- Count -u64 GPU_CountBufferEx(GPU_ResourceHandle buffer, u64 element_size) +u64 GPU_CountBufferBytes(GPU_ResourceHandle buffer) { /* TODO */ return 0; @@ -1448,9 +1444,12 @@ void GPU_CommitCommandListEx(GPU_CommandListHandle cl_handle, GPU_QueueKind queu GPU_D12_Pipeline *bound_pipeline = 0; /* Constants state */ - u32 slotted_constants[MaxShaderConstants] = ZI; - u32 bound_graphics_constants[MaxShaderConstants] = ZI; - u32 bound_compute_constants[MaxShaderConstants] = ZI; + u64 slotted_constants[MaxShaderConstants]; + u64 bound_compute_constants[MaxShaderConstants]; + u64 bound_graphics_constants[MaxShaderConstants]; + for (i32 i = 0; i < countof(slotted_constants); ++i) { slotted_constants[i] = U64Max; } + for (i32 i = 0; i < countof(bound_compute_constants); ++i) { bound_compute_constants[i] = U64Max; } + for (i32 i = 0; i < countof(bound_graphics_constants); ++i) { bound_graphics_constants[i] = U64Max; } /* Rasterizer state */ D3D12_VIEWPORT bound_viewport = ZI; @@ -1567,7 +1566,7 @@ void GPU_CommitCommandListEx(GPU_CommandListHandle cl_handle, GPU_QueueKind queu { i32 slot = cmd->constant.slot; u32 value = cmd->constant.value; - if (slot > 0 && slot < countof(slotted_constants) && slotted_constants[slot] != value) + if (slot >= 0 && slot < countof(slotted_constants)) { slotted_constants[slot] = value; } @@ -1777,8 +1776,6 @@ void GPU_CommitCommandListEx(GPU_CommandListHandle cl_handle, GPU_QueueKind queu if (!compute_rootsig_set) { ID3D12GraphicsCommandList_SetComputeRootSignature(d3d_cl, g->bindless_rootsig); - ID3D12GraphicsCommandList_SetComputeRoot32BitConstants(d3d_cl, 0, MaxShaderConstants, slotted_constants, 0); - CopyBytes(bound_compute_constants, slotted_constants, sizeof(bound_compute_constants)); compute_rootsig_set = 1; } @@ -1792,11 +1789,10 @@ void GPU_CommitCommandListEx(GPU_CommandListHandle cl_handle, GPU_QueueKind queu /* Update root constants */ for (i32 slot = 0; slot < MaxShaderConstants; ++slot) { - u32 v = slotted_constants[slot]; - if (bound_compute_constants[slot] != v) + if (bound_compute_constants[slot] != slotted_constants[slot]) { - ID3D12GraphicsCommandList_SetComputeRoot32BitConstant(d3d_cl, 0, v, slot); - bound_compute_constants[slot] = v; + ID3D12GraphicsCommandList_SetComputeRoot32BitConstant(d3d_cl, slot, slotted_constants[slot], 0); + bound_compute_constants[slot] = slotted_constants[slot]; } } @@ -1893,8 +1889,6 @@ void GPU_CommitCommandListEx(GPU_CommandListHandle cl_handle, GPU_QueueKind queu if (!graphics_rootsig_set) { ID3D12GraphicsCommandList_SetGraphicsRootSignature(d3d_cl, g->bindless_rootsig); - ID3D12GraphicsCommandList_SetGraphicsRoot32BitConstants(d3d_cl, 0, MaxShaderConstants, slotted_constants, 0); - CopyBytes(bound_graphics_constants, slotted_constants, sizeof(bound_graphics_constants)); graphics_rootsig_set = 1; } @@ -1908,11 +1902,10 @@ void GPU_CommitCommandListEx(GPU_CommandListHandle cl_handle, GPU_QueueKind queu /* Update root constants */ for (i32 slot = 0; slot < MaxShaderConstants; ++slot) { - u32 v = slotted_constants[slot]; - if (bound_graphics_constants[slot] != v) + if (bound_graphics_constants[slot] != slotted_constants[slot]) { - ID3D12GraphicsCommandList_SetGraphicsRoot32BitConstant(d3d_cl, 0, v, slot); - bound_graphics_constants[slot] = v; + ID3D12GraphicsCommandList_SetGraphicsRoot32BitConstant(d3d_cl, slot, slotted_constants[slot], 0); + bound_graphics_constants[slot] = slotted_constants[slot]; } } @@ -2153,61 +2146,15 @@ void GPU_CopyTexels(GPU_CommandListHandle cl, GPU_ResourceHandle dst, Vec3I32 ds /* TODO */ } -//- Constants +//- Constant -void GPU_SetConstantU32_(GPU_CommandListHandle cl_handle, i32 slot, u32 v) +void GPU_SetConstant_(GPU_CommandListHandle cl_handle, i32 slot, void *src_32bit, u32 size) { - GPU_D12_PushConstCmd(GPU_D12_CmdListFromHandle(cl_handle), slot, &v); -} - -void GPU_SetConstantF32_(GPU_CommandListHandle cl_handle, i32 slot, f32 v) -{ - GPU_D12_PushConstCmd(GPU_D12_CmdListFromHandle(cl_handle), slot, &v); -} - -void GPU_SetConstantStructuredBufferHandle_(GPU_CommandListHandle cl_handle, i32 slot, StructuredBufferHandle v) -{ - GPU_D12_PushConstCmd(GPU_D12_CmdListFromHandle(cl_handle), slot, &v); -} - -void GPU_SetConstantRWStructuredBufferHandle_(GPU_CommandListHandle cl_handle, i32 slot, RWStructuredBufferHandle v) -{ - GPU_D12_PushConstCmd(GPU_D12_CmdListFromHandle(cl_handle), slot, &v); -} - -void GPU_SetConstantTexture1DHandle_(GPU_CommandListHandle cl_handle, i32 slot, Texture1DHandle v) -{ - GPU_D12_PushConstCmd(GPU_D12_CmdListFromHandle(cl_handle), slot, &v); -} - -void GPU_SetConstantRWTexture1DHandle_(GPU_CommandListHandle cl_handle, i32 slot, RWTexture1DHandle v) -{ - GPU_D12_PushConstCmd(GPU_D12_CmdListFromHandle(cl_handle), slot, &v); -} - -void GPU_SetConstantTexture2DHandle_(GPU_CommandListHandle cl_handle, i32 slot, Texture2DHandle v) -{ - GPU_D12_PushConstCmd(GPU_D12_CmdListFromHandle(cl_handle), slot, &v); -} - -void GPU_SetConstantRWTexture2DHandle_(GPU_CommandListHandle cl_handle, i32 slot, RWTexture2DHandle v) -{ - GPU_D12_PushConstCmd(GPU_D12_CmdListFromHandle(cl_handle), slot, &v); -} - -void GPU_SetConstantTexture3DHandle_(GPU_CommandListHandle cl_handle, i32 slot, Texture3DHandle v) -{ - GPU_D12_PushConstCmd(GPU_D12_CmdListFromHandle(cl_handle), slot, &v); -} - -void GPU_SetConstantRWTexture3DHandle_(GPU_CommandListHandle cl_handle, i32 slot, RWTexture3DHandle v) -{ - GPU_D12_PushConstCmd(GPU_D12_CmdListFromHandle(cl_handle), slot, &v); -} - -void GPU_SetConstantSamplerHandle_(GPU_CommandListHandle cl_handle, i32 slot, SamplerHandle v) -{ - GPU_D12_PushConstCmd(GPU_D12_CmdListFromHandle(cl_handle), slot, &v); + GPU_D12_CmdList *cl = GPU_D12_CmdListFromHandle(cl_handle); + GPU_D12_Cmd *cmd = GPU_D12_PushCmd(cl); + cmd->kind = GPU_D12_CmdKind_Constant; + cmd->constant.slot = slot; + CopyBytes(&cmd->constant.value, src_32bit, MinU32(size, 4)); } //- Barrier diff --git a/src/gpu/gpu_dx12/gpu_dx12.h b/src/gpu/gpu_dx12/gpu_dx12.h index 70fa016f..c4621c36 100644 --- a/src/gpu/gpu_dx12/gpu_dx12.h +++ b/src/gpu/gpu_dx12/gpu_dx12.h @@ -335,11 +335,6 @@ Struct(GPU_D12_SharedState) ID3D12Device *device; } extern GPU_D12_shared_state; -//////////////////////////////////////////////////////////// -//~ Startup - -void GPU_D12_Startup(void); - //////////////////////////////////////////////////////////// //~ Helpers diff --git a/src/proto/proto.c b/src/proto/proto.c index f8ac347c..4e4e1638 100644 --- a/src/proto/proto.c +++ b/src/proto/proto.c @@ -43,7 +43,9 @@ JobImpl(PR_RunForever, _sig, _id) /* Prep test pass */ { - GPU_SetConstantU32(cl, PR_DConst_TestConst, 5); + final_target_rwhandle.v = 12; + GPU_SetConstant(cl, PR_ShaderConst_TestTarget, final_target_rwhandle); + GPU_SetConstant(cl, PR_ShaderConst_TestConst, 3.123); } /* Test pass */ diff --git a/src/proto/proto_shaders.gpu b/src/proto/proto_shaders.gpu index 7aed54c1..60b367fd 100644 --- a/src/proto/proto_shaders.gpu +++ b/src/proto/proto_shaders.gpu @@ -1,14 +1,15 @@ - //////////////////////////////////////////////////////////// //~ Test shader ComputeShader2D(PR_TestCS, 8, 8) { - RWTexture2D target_tex = ResourceFromConstantHandle(RWTexture2DHandle, PR_DConst_TestTarget); + RWTexture2D target_tex = RWTexture2DFromUniformHandle(PR_ShaderConst_TestTarget); Vec2U32 target_tex_size = Count2D(target_tex); - Vec2U32 id = SV_DispatchThreadID; - if (id.x < target_tex_size.x && id.y < target_tex_size.y) + f32 testf = PR_ShaderConst_TestConst; + + Vec2I32 id = SV_DispatchThreadID; + if ((id.x < target_tex_size.x && id.y < target_tex_size.y) || testf < 3) { target_tex[id] = Vec4(0, 1, 0, 1); } diff --git a/src/proto/proto_shaders.h b/src/proto/proto_shaders.h index 547b42e2..56068190 100644 --- a/src/proto/proto_shaders.h +++ b/src/proto/proto_shaders.h @@ -1,6 +1,5 @@ +//////////////////////////////////////////////////////////// +//~ Constants -Enum(PR_DConst) -{ - ShaderConstant(RWTexture2DHandle, PR_DConst_TestTarget), - ShaderConstant(u32, PR_DConst_TestConst), -}; +ShaderConstant(RWTexture2DHandle, PR_ShaderConst_TestTarget, 0); +ShaderConstant(f32, PR_ShaderConst_TestConst, 1);