git.s-ol.nu ~forks/DiligentCore / 7b35bc8
RunTimeResourceArray: added constant buffers assiduous 6 months ago
6 changed file(s) with 97 addition(s) and 27 deletion(s). Raw diff Collapse all Expand all
633633 "Compile the shader using SM5.1+ or change the resource layout to use only one space.");
634634 }
635635
636 #ifdef DILIGENT_DEVELOPMENT
637 // Validate resources before remapping
638 DvpValidateShaderResources(pShader, pLocalRootSig);
639 #endif
640
636641 CComPtr<ID3DBlob> pBlob;
637642 if (IsDXILBytecode(pBytecode->GetBufferPointer(), pBytecode->GetBufferSize()))
638643 {
651656 LOG_ERROR_AND_THROW("Failed to remap resource bindings in shader '", pShader->GetDesc().Name, "'.");
652657 }
653658 pBytecode = pBlob;
654
655 #ifdef DILIGENT_DEVELOPMENT
656 DvpValidateShaderResources(pShader, pLocalRootSig);
657 #endif
658659 }
659660 }
660661 }
101101 SkipCount = 1;
102102
103103 UINT BindCount = BindingDesc.BindCount;
104 if (BindCount == UINT_MAX)
105 {
106 // For some reason
107 // Texture2D g_Textures[]
108 // produces BindCount == 0, but
109 // ConstantBuffer<CBData> g_ConstantBuffers[]
110 // produces BindCount == UINT_MAX
111 BindCount = 0;
112 }
104113
105114 // Handle arrays
106115 // For shader models 5_0 and before, every resource array element is enumerated individually.
830830 {
831831 NameAndBinding.second.SrcBindPoint = ResDesc.BindPoint;
832832 NameAndBinding.second.SrcSpace = ResDesc.Space;
833 VERIFY_EXPR(ResDesc.BindCount == 0 || NameAndBinding.second.ArraySize >= ResDesc.BindCount);
833 // For some reason
834 // Texture2D g_Textures[]
835 // produces BindCount == 0, but
836 // ConstantBuffer<CBData> g_ConstantBuffers[]
837 // produces BindCount == UINT_MAX
838 VERIFY_EXPR(ResDesc.BindCount == 0 || ResDesc.BindCount == UINT_MAX || NameAndBinding.second.ArraySize >= ResDesc.BindCount);
834839
835840 switch (ResDesc.Type)
836841 {
33
44 uniform texture2D g_Textures[];
55 uniform sampler g_Sampler;
6
7 uniform g_ConstantBuffers
8 {
9 vec4 Data;
10 }g_ConstantBufferInst[];
611
712 vec4 CheckValue(vec4 Val, vec4 Expected)
813 {
1520
1621 vec4 VerifyResources(uint index, vec2 coord)
1722 {
18 vec4 RefValues[NUM_TEXTURES];
19 RefValues[0] = Tex2D_Ref0;
20 RefValues[1] = Tex2D_Ref1;
21 RefValues[2] = Tex2D_Ref2;
22 RefValues[3] = Tex2D_Ref3;
23 RefValues[4] = Tex2D_Ref4;
24 RefValues[5] = Tex2D_Ref5;
25 RefValues[6] = Tex2D_Ref6;
26 RefValues[7] = Tex2D_Ref7;
23 vec4 TexRefValues[NUM_TEXTURES];
24 TexRefValues[0] = Tex2D_Ref0;
25 TexRefValues[1] = Tex2D_Ref1;
26 TexRefValues[2] = Tex2D_Ref2;
27 TexRefValues[3] = Tex2D_Ref3;
28 TexRefValues[4] = Tex2D_Ref4;
29 TexRefValues[5] = Tex2D_Ref5;
30 TexRefValues[6] = Tex2D_Ref6;
31 TexRefValues[7] = Tex2D_Ref7;
2732
28 return CheckValue(textureLod(sampler2D(g_Textures[nonuniformEXT(index)], g_Sampler), coord, 0.0), RefValues[index]);
33 vec4 ConstBuffRefValues[NUM_CONST_BUFFERS];
34 ConstBuffRefValues[0] = ConstBuff_Ref0;
35 ConstBuffRefValues[1] = ConstBuff_Ref1;
36 ConstBuffRefValues[2] = ConstBuff_Ref2;
37 ConstBuffRefValues[3] = ConstBuff_Ref3;
38 ConstBuffRefValues[4] = ConstBuff_Ref4;
39 ConstBuffRefValues[5] = ConstBuff_Ref5;
40 ConstBuffRefValues[6] = ConstBuff_Ref6;
41
42 uint TexIdx = index % NUM_TEXTURES;
43 uint BuffIdx = index % NUM_CONST_BUFFERS;
44
45 vec4 AllCorrect = vec4(1.0, 1.0, 1.0, 1.0);
46 AllCorrect *= CheckValue(textureLod(sampler2D(g_Textures[nonuniformEXT(TexIdx)], g_Sampler), coord, 0.0), TexRefValues[TexIdx]);
47 AllCorrect *= CheckValue(g_ConstantBufferInst[nonuniformEXT(BuffIdx)].Data, ConstBuffRefValues[BuffIdx]);
48
49 return AllCorrect;
2950 }
3051
3152 layout(rgba8) writeonly uniform image2D g_OutImage;
3960
4061 vec4 Color = vec4(vec2(gl_GlobalInvocationID.xy % 256u) / 256.0, 0.0, 1.0);
4162 vec2 uv = vec2(gl_GlobalInvocationID.xy + vec2(0.5,0.5)) / vec2(Dim);
42 Color *= VerifyResources(gl_LocalInvocationIndex % NUM_TEXTURES, uv);
63 Color *= VerifyResources(gl_LocalInvocationIndex, uv);
4364
4465 imageStore(g_OutImage, ivec2(gl_GlobalInvocationID.xy), Color);
4566 }
00 Texture2D g_Textures[] : register(t0, space1);
11 SamplerState g_Sampler;
2
3 struct CBData
4 {
5 float4 Data;
6 };
7 ConstantBuffer<CBData> g_ConstantBuffers[] : register(b10, space5);
28
39 float4 CheckValue(float4 Val, float4 Expected)
410 {
1117
1218 float4 VerifyResources(uint index, float2 coord)
1319 {
14 float4 RefValues[NUM_TEXTURES];
15 RefValues[0] = Tex2D_Ref0;
16 RefValues[1] = Tex2D_Ref1;
17 RefValues[2] = Tex2D_Ref2;
18 RefValues[3] = Tex2D_Ref3;
19 RefValues[4] = Tex2D_Ref4;
20 RefValues[5] = Tex2D_Ref5;
21 RefValues[6] = Tex2D_Ref6;
22 RefValues[7] = Tex2D_Ref7;
20 float4 TexRefValues[NUM_TEXTURES];
21 TexRefValues[0] = Tex2D_Ref0;
22 TexRefValues[1] = Tex2D_Ref1;
23 TexRefValues[2] = Tex2D_Ref2;
24 TexRefValues[3] = Tex2D_Ref3;
25 TexRefValues[4] = Tex2D_Ref4;
26 TexRefValues[5] = Tex2D_Ref5;
27 TexRefValues[6] = Tex2D_Ref6;
28 TexRefValues[7] = Tex2D_Ref7;
2329
24 return CheckValue(g_Textures[NonUniformResourceIndex(index)].SampleLevel(g_Sampler, coord, 0.0), RefValues[index]);
30 float4 ConstBuffRefValues[NUM_CONST_BUFFERS];
31 ConstBuffRefValues[0] = ConstBuff_Ref0;
32 ConstBuffRefValues[1] = ConstBuff_Ref1;
33 ConstBuffRefValues[2] = ConstBuff_Ref2;
34 ConstBuffRefValues[3] = ConstBuff_Ref3;
35 ConstBuffRefValues[4] = ConstBuff_Ref4;
36 ConstBuffRefValues[5] = ConstBuff_Ref5;
37 ConstBuffRefValues[6] = ConstBuff_Ref6;
38
39 uint TexIdx = index % NUM_TEXTURES;
40 uint BuffIdx = index % NUM_CONST_BUFFERS;
41
42 float4 AllCorrect = float4(1.0, 1.0, 1.0, 1.0);
43 AllCorrect *= CheckValue(g_Textures[NonUniformResourceIndex(TexIdx)].SampleLevel(g_Sampler, coord, 0.0), TexRefValues[TexIdx]);
44 AllCorrect *= CheckValue(g_ConstantBuffers[NonUniformResourceIndex(BuffIdx)].Data, ConstBuffRefValues[BuffIdx]);
45
46 return AllCorrect;
2547 }
2648
2749 RWTexture2D<float4> g_OutImage;
3759
3860 float4 Color = float4(float2(GlobalInvocationID.xy % 256u) / 256.0, 0.0, 1.0);
3961 float2 uv = float2(GlobalInvocationID.xy + float2(0.5,0.5)) / float2(Dim);
40 Color *= VerifyResources(LocalInvocationIndex % NUM_TEXTURES, uv);
62 Color *= VerifyResources(LocalInvocationIndex, uv);
4163
4264 g_OutImage[GlobalInvocationID.xy] = Color;
4365 }
15791579 TEXTURE_VIEW_SHADER_RESOURCE //
15801580 };
15811581
1582 constexpr Uint32 ConstBuffArraySize = 7;
1583 ReferenceBuffers RefConstBuffers{
1584 ConstBuffArraySize,
1585 USAGE_DEFAULT,
1586 BIND_UNIFORM_BUFFER //
1587 };
1588
15821589 RefCntAutoPtr<IPipelineResourceSignature> pSignature;
15831590
15841591 {
15861593 {
15871594 {SHADER_TYPE_COMPUTE, "g_Textures", TexArraySize, SHADER_RESOURCE_TYPE_TEXTURE_SRV, SHADER_RESOURCE_VARIABLE_TYPE_MUTABLE, PIPELINE_RESOURCE_FLAG_RUNTIME_ARRAY},
15881595 {SHADER_TYPE_COMPUTE, "g_Sampler", 1, SHADER_RESOURCE_TYPE_SAMPLER, SHADER_RESOURCE_VARIABLE_TYPE_MUTABLE},
1596 {SHADER_TYPE_COMPUTE, "g_ConstantBuffers", ConstBuffArraySize, SHADER_RESOURCE_TYPE_CONSTANT_BUFFER, SHADER_RESOURCE_VARIABLE_TYPE_MUTABLE, PIPELINE_RESOURCE_FLAG_RUNTIME_ARRAY},
15891597 {SHADER_TYPE_COMPUTE, "g_OutImage", 1, SHADER_RESOURCE_TYPE_TEXTURE_UAV, SHADER_RESOURCE_VARIABLE_TYPE_MUTABLE} //
15901598 };
15911599
16151623 ShaderMacroHelper Macros;
16161624
16171625 Macros.AddShaderMacro("NUM_TEXTURES", TexArraySize);
1626 Macros.AddShaderMacro("NUM_CONST_BUFFERS", ConstBuffArraySize);
16181627 if (IsGLSL)
16191628 Macros.AddShaderMacro("float4", "vec4");
16201629 for (Uint32 i = 0; i < TexArraySize; ++i)
16211630 Macros.AddShaderMacro((String{"Tex2D_Ref"} + std::to_string(i)).c_str(), RefTextures.GetColor(i));
1631 for (Uint32 i = 0; i < ConstBuffArraySize; ++i)
1632 Macros.AddShaderMacro((String{"ConstBuff_Ref"} + std::to_string(i)).c_str(), RefConstBuffers.GetValue(i));
16221633
16231634 RefCntAutoPtr<IShader> pCS;
16241635 {
16591670 ASSERT_TRUE(pTestingSwapChain);
16601671 pSRB->GetVariableByName(SHADER_TYPE_COMPUTE, "g_OutImage")->Set(pTestingSwapChain->GetCurrentBackBufferUAV());
16611672 pSRB->GetVariableByName(SHADER_TYPE_COMPUTE, "g_Textures")->SetArray(RefTextures.GetViewObjects(0), 0, TexArraySize);
1673 pSRB->GetVariableByName(SHADER_TYPE_COMPUTE, "g_ConstantBuffers")->SetArray(RefConstBuffers.GetBuffObjects(0), 0, ConstBuffArraySize);
16621674 pContext->CommitShaderResources(pSRB, RESOURCE_STATE_TRANSITION_MODE_TRANSITION);
16631675
16641676 pContext->SetPipelineState(pPSO);