git.s-ol.nu ~forks/DiligentCore / 35f5c5d
RunTimeResourceArray Test: added RW textures and buffers assiduous 6 months ago
4 changed file(s) with 108 addition(s) and 16 deletion(s). Raw diff Collapse all Expand all
1515 {
1616 vec4 Data;
1717 }g_StructuredBufferInst[];
18
19 layout(rgba8) uniform image2D g_RWTextures[];
20
21 layout(std140) buffer g_RWStructBuffers
22 {
23 vec4 Data;
24 }g_RWStructBuffersInst[];
25
1826
1927 vec4 CheckValue(vec4 Val, vec4 Expected)
2028 {
4654 ConstBuffRefValues[5] = ConstBuff_Ref5;
4755 ConstBuffRefValues[6] = ConstBuff_Ref6;
4856
49 float4 FmtBuffRefValues[NUM_FMT_BUFFERS];
57 vec4 FmtBuffRefValues[NUM_FMT_BUFFERS];
5058 FmtBuffRefValues[0] = FmtBuff_Ref0;
5159 FmtBuffRefValues[1] = FmtBuff_Ref1;
5260 FmtBuffRefValues[2] = FmtBuff_Ref2;
5361 FmtBuffRefValues[3] = FmtBuff_Ref3;
5462 FmtBuffRefValues[4] = FmtBuff_Ref4;
5563
56 float4 StructBuffRefValues[NUM_STRUCT_BUFFERS];
64 vec4 StructBuffRefValues[NUM_STRUCT_BUFFERS];
5765 StructBuffRefValues[0] = StructBuff_Ref0;
5866 StructBuffRefValues[1] = StructBuff_Ref1;
5967 StructBuffRefValues[2] = StructBuff_Ref2;
6068
61 uint TexIdx = index % NUM_TEXTURES;
62 uint SamIdx = index % NUM_SAMPLERS;
63 uint BuffIdx = index % NUM_CONST_BUFFERS;
64 uint FmtBuffIdx = index % NUM_FMT_BUFFERS;
65 uint StructBuffIdx = index % NUM_STRUCT_BUFFERS;
69 vec4 RWTexRefValues[NUM_TEXTURES];
70 RWTexRefValues[0] = RWTex2D_Ref0;
71 RWTexRefValues[1] = RWTex2D_Ref1;
72 RWTexRefValues[2] = RWTex2D_Ref2;
73
74 float4 RWStructBuffRefValues[NUM_RWSTRUCT_BUFFERS];
75 RWStructBuffRefValues[0] = RWStructBuff_Ref0;
76 RWStructBuffRefValues[1] = RWStructBuff_Ref1;
77 RWStructBuffRefValues[2] = RWStructBuff_Ref2;
78 RWStructBuffRefValues[3] = RWStructBuff_Ref3;
79
80 uint TexIdx = index % NUM_TEXTURES;
81 uint SamIdx = index % NUM_SAMPLERS;
82 uint BuffIdx = index % NUM_CONST_BUFFERS;
83 uint FmtBuffIdx = index % NUM_FMT_BUFFERS;
84 uint StructBuffIdx = index % NUM_STRUCT_BUFFERS;
85 uint RWTexIdx = index % NUM_RWTEXTURES;
86 uint RWStructBuffIdx = index % NUM_RWSTRUCT_BUFFERS;
6687
6788 vec4 AllCorrect = vec4(1.0, 1.0, 1.0, 1.0);
6889 AllCorrect *= CheckValue(textureLod(sampler2D(g_Textures[nonuniformEXT(TexIdx)], g_Samplers[nonuniformEXT(SamIdx)]), coord, 0.0), TexRefValues[TexIdx]);
6990 AllCorrect *= CheckValue(g_ConstantBufferInst[nonuniformEXT(BuffIdx)].Data, ConstBuffRefValues[BuffIdx]);
7091 AllCorrect *= CheckValue(texelFetch(g_FormattedBuffers[nonuniformEXT(FmtBuffIdx)], 0), FmtBuffRefValues[FmtBuffIdx]);
71 AllCorrect *= CheckValue(g_ConstantBufferInst[nonuniformEXT(StructBuffIdx)].Data, StructBuffRefValues[StructBuffIdx]);
92 AllCorrect *= CheckValue(g_StructuredBufferInst[nonuniformEXT(StructBuffIdx)].Data, StructBuffRefValues[StructBuffIdx]);
93 AllCorrect *= CheckValue(imageLoad(g_RWTextures[nonuniformEXT(RWTexIdx)], ivec2(coord * 10)), RWTexRefValues[RWTexIdx]);
94 AllCorrect *= CheckValue(g_RWStructBuffersInst[nonuniformEXT(RWStructBuffIdx)].Data, RWStructBuffRefValues[RWStructBuffIdx]);
7295
7396 return AllCorrect;
7497 }
1313 float4 Data;
1414 };
1515 StructuredBuffer<StructBuffData> g_StructuredBuffers[];
16
17 RWTexture2D<unorm float4 /*format=rgba8*/> g_RWTextures[] : register(u10, space5);
18
19
20 #ifndef VULKAN // RW structured buffers are not supported by DXC
21 struct RWStructBuffData
22 {
23 float4 Data;
24 };
25 RWStructuredBuffer<RWStructBuffData> g_RWStructBuffers[] : register(u10, space6);
26 #endif
27
1628
1729 float4 CheckValue(float4 Val, float4 Expected)
1830 {
5668 StructBuffRefValues[1] = StructBuff_Ref1;
5769 StructBuffRefValues[2] = StructBuff_Ref2;
5870
59 uint TexIdx = index % NUM_TEXTURES;
60 uint SamIdx = index % NUM_SAMPLERS;
61 uint ConstBuffIdx = index % NUM_CONST_BUFFERS;
62 uint FmtBuffIdx = index % NUM_FMT_BUFFERS;
63 uint StructBuffIdx = index % NUM_STRUCT_BUFFERS;
71 float4 RWTexRefValues[NUM_TEXTURES];
72 RWTexRefValues[0] = RWTex2D_Ref0;
73 RWTexRefValues[1] = RWTex2D_Ref1;
74 RWTexRefValues[2] = RWTex2D_Ref2;
75
76 float4 RWStructBuffRefValues[NUM_RWSTRUCT_BUFFERS];
77 RWStructBuffRefValues[0] = RWStructBuff_Ref0;
78 RWStructBuffRefValues[1] = RWStructBuff_Ref1;
79 RWStructBuffRefValues[2] = RWStructBuff_Ref2;
80 RWStructBuffRefValues[3] = RWStructBuff_Ref3;
81
82 uint TexIdx = index % NUM_TEXTURES;
83 uint SamIdx = index % NUM_SAMPLERS;
84 uint ConstBuffIdx = index % NUM_CONST_BUFFERS;
85 uint FmtBuffIdx = index % NUM_FMT_BUFFERS;
86 uint StructBuffIdx = index % NUM_STRUCT_BUFFERS;
87 uint RWTexIdx = index % NUM_RWTEXTURES;
88 uint RWStructBuffIdx = index % NUM_RWSTRUCT_BUFFERS;
6489
6590 float4 AllCorrect = float4(1.0, 1.0, 1.0, 1.0);
6691 AllCorrect *= CheckValue(g_Textures[NonUniformResourceIndex(TexIdx)].SampleLevel(g_Samplers[NonUniformResourceIndex(SamIdx)], coord, 0.0), TexRefValues[TexIdx]);
6792 AllCorrect *= CheckValue(g_ConstantBuffers[NonUniformResourceIndex(ConstBuffIdx)].Data, ConstBuffRefValues[ConstBuffIdx]);
6893 AllCorrect *= CheckValue(g_FormattedBuffers[NonUniformResourceIndex(FmtBuffIdx)].Load(0), FmtBuffRefValues[FmtBuffIdx]);
6994 AllCorrect *= CheckValue(g_StructuredBuffers[NonUniformResourceIndex(StructBuffIdx)][0].Data, StructBuffRefValues[StructBuffIdx]);
95 AllCorrect *= CheckValue(g_RWTextures[NonUniformResourceIndex(RWTexIdx)][int2(coord*10)], RWTexRefValues[RWTexIdx]);
96 #ifndef VULKAN
97 AllCorrect *= CheckValue(g_RWStructBuffers[NonUniformResourceIndex(RWStructBuffIdx)][0].Data, RWStructBuffRefValues[RWStructBuffIdx]);
98 #endif
7099
71100 return AllCorrect;
72101 }
2727 #pragma once
2828
2929 #include <vector>
30 #include <atomic>
3031
3132 #include "RenderDevice.h"
3233 #include "DeviceContext.h"
6162
6263 for (Uint32 i = 0; i < NumBuffers; ++i)
6364 {
65 static std::atomic_int Counter{10};
66
6467 auto& Value = Values[i];
65 auto v = static_cast<float>(i * 10);
66 Value = float4(v + 1, v + 2, v + 3, v + 4);
68 float v = static_cast<float>(Counter.fetch_add(10));
69 Value = float4{v + 1, v + 2, v + 3, v + 4};
6770
6871 std::vector<float4> InitData(16, Value);
6972
169172 auto& pTexture = Textures[i];
170173 auto& Value = Values[i];
171174
172 int v = (i % 15) + 1;
175 static std::atomic_int Counter{1};
176
177 int v = (Counter.fetch_add(1) % 15) + 1;
173178 Value = float4{
174179 (v & 0x01) ? 1.f : 0.f,
175180 (v & 0x02) ? 1.f : 0.f,
15791579 TEXTURE_VIEW_SHADER_RESOURCE //
15801580 };
15811581
1582 constexpr Uint32 RWTexArraySize = 3;
1583 ReferenceTextures RefRWTextures{
1584 RWTexArraySize,
1585 128, 128,
1586 USAGE_DEFAULT,
1587 BIND_UNORDERED_ACCESS,
1588 TEXTURE_VIEW_UNORDERED_ACCESS //
1589 };
1590
15821591 constexpr Uint32 SamArraySize = 3;
15831592
15841593 constexpr Uint32 ConstBuffArraySize = 7;
16031612 USAGE_DEFAULT,
16041613 BIND_SHADER_RESOURCE,
16051614 BUFFER_VIEW_SHADER_RESOURCE,
1615 BUFFER_MODE_STRUCTURED //
1616 };
1617
1618 constexpr Uint32 RWStructBuffArraySize = 4;
1619 ReferenceBuffers RefRWStructBuffers{
1620 RWStructBuffArraySize,
1621 USAGE_DEFAULT,
1622 BIND_UNORDERED_ACCESS,
1623 BUFFER_VIEW_UNORDERED_ACCESS,
16061624 BUFFER_MODE_STRUCTURED //
16071625 };
16081626
16161634 {SHADER_TYPE_COMPUTE, "g_ConstantBuffers", ConstBuffArraySize, SHADER_RESOURCE_TYPE_CONSTANT_BUFFER, SHADER_RESOURCE_VARIABLE_TYPE_MUTABLE, PIPELINE_RESOURCE_FLAG_RUNTIME_ARRAY},
16171635 {SHADER_TYPE_COMPUTE, "g_FormattedBuffers", FmtBuffArraySize, SHADER_RESOURCE_TYPE_BUFFER_SRV, SHADER_RESOURCE_VARIABLE_TYPE_MUTABLE, PIPELINE_RESOURCE_FLAG_FORMATTED_BUFFER | PIPELINE_RESOURCE_FLAG_RUNTIME_ARRAY},
16181636 {SHADER_TYPE_COMPUTE, "g_StructuredBuffers", StructBuffArraySize, SHADER_RESOURCE_TYPE_BUFFER_SRV, SHADER_RESOURCE_VARIABLE_TYPE_MUTABLE, PIPELINE_RESOURCE_FLAG_RUNTIME_ARRAY},
1637 {SHADER_TYPE_COMPUTE, "g_RWTextures", RWTexArraySize, SHADER_RESOURCE_TYPE_TEXTURE_UAV, SHADER_RESOURCE_VARIABLE_TYPE_MUTABLE, PIPELINE_RESOURCE_FLAG_RUNTIME_ARRAY},
1638 {SHADER_TYPE_COMPUTE, "g_RWStructBuffers", RWStructBuffArraySize, SHADER_RESOURCE_TYPE_BUFFER_UAV, SHADER_RESOURCE_VARIABLE_TYPE_MUTABLE, PIPELINE_RESOURCE_FLAG_RUNTIME_ARRAY},
16191639 {SHADER_TYPE_COMPUTE, "g_OutImage", 1, SHADER_RESOURCE_TYPE_TEXTURE_UAV, SHADER_RESOURCE_VARIABLE_TYPE_MUTABLE} //
16201640 };
16211641
16511671 Macros.AddShaderMacro("NUM_CONST_BUFFERS", ConstBuffArraySize);
16521672 Macros.AddShaderMacro("NUM_FMT_BUFFERS", FmtBuffArraySize);
16531673 Macros.AddShaderMacro("NUM_STRUCT_BUFFERS", StructBuffArraySize);
1674 Macros.AddShaderMacro("NUM_RWTEXTURES", RWTexArraySize);
1675 Macros.AddShaderMacro("NUM_RWSTRUCT_BUFFERS", RWStructBuffArraySize);
16541676 if (IsGLSL)
16551677 Macros.AddShaderMacro("float4", "vec4");
16561678 for (Uint32 i = 0; i < TexArraySize; ++i)
16611683 Macros.AddShaderMacro((String{"FmtBuff_Ref"} + std::to_string(i)).c_str(), RefFmtBuffers.GetValue(i));
16621684 for (Uint32 i = 0; i < StructBuffArraySize; ++i)
16631685 Macros.AddShaderMacro((String{"StructBuff_Ref"} + std::to_string(i)).c_str(), RefStructBuffers.GetValue(i));
1686 for (Uint32 i = 0; i < RWTexArraySize; ++i)
1687 Macros.AddShaderMacro((String{"RWTex2D_Ref"} + std::to_string(i)).c_str(), RefRWTextures.GetColor(i));
1688 for (Uint32 i = 0; i < RWStructBuffArraySize; ++i)
1689 Macros.AddShaderMacro((String{"RWStructBuff_Ref"} + std::to_string(i)).c_str(), RefRWStructBuffers.GetValue(i));
16641690
16651691 RefCntAutoPtr<IShader> pCS;
16661692 {
16751701 ShaderCI.CompileFlags = SHADER_COMPILE_FLAG_ENABLE_UNBOUNDED_ARRAYS;
16761702 ShaderCI.ShaderCompiler = pEnv->GetDefaultCompiler(ShaderCI.SourceLanguage);
16771703
1704 if (!deviceCaps.IsD3DDevice() && !IsGLSL)
1705 {
1706 // Run-time resource arrays are not handled well by GLSLang: NonUniformResourceIndex is not defined;
1707 // Constant buffer, structured buffer and RW structured buffer arrays have issues.
1708 ShaderCI.ShaderCompiler = SHADER_COMPILER_DXC;
1709 }
1710
16781711 pDevice->CreateShader(ShaderCI, &pCS);
16791712 ASSERT_NE(pCS, nullptr);
16801713 }
17051738 pSRB->GetVariableByName(SHADER_TYPE_COMPUTE, "g_ConstantBuffers")->SetArray(RefConstBuffers.GetBuffObjects(0), 0, ConstBuffArraySize);
17061739 pSRB->GetVariableByName(SHADER_TYPE_COMPUTE, "g_FormattedBuffers")->SetArray(RefFmtBuffers.GetViewObjects(0), 0, FmtBuffArraySize);
17071740 pSRB->GetVariableByName(SHADER_TYPE_COMPUTE, "g_StructuredBuffers")->SetArray(RefStructBuffers.GetViewObjects(0), 0, StructBuffArraySize);
1741 pSRB->GetVariableByName(SHADER_TYPE_COMPUTE, "g_RWTextures")->SetArray(RefRWTextures.GetViewObjects(0), 0, RWTexArraySize);
1742 pSRB->GetVariableByName(SHADER_TYPE_COMPUTE, "g_RWStructBuffers")->SetArray(RefRWStructBuffers.GetViewObjects(0), 0, RWStructBuffArraySize);
17081743 pContext->CommitShaderResources(pSRB, RESOURCE_STATE_TRANSITION_MODE_TRANSITION);
17091744
17101745 pContext->SetPipelineState(pPSO);