Skip to content

Commit 54ef580

Browse files
committed
Add DXC tests.
1 parent ce3da9d commit 54ef580

File tree

10 files changed

+84
-19
lines changed

10 files changed

+84
-19
lines changed

Tests/DiligentCoreTest/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ if(WEBGPU_SUPPORTED)
6464
target_link_libraries(DiligentCoreTest PRIVATE libtint)
6565
endif()
6666

67+
if(PLATFORM_WIN32)
68+
copy_required_dlls(DiligentCoreTest
69+
DXC_REQUIRED YES
70+
)
71+
endif()
72+
6773
source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${SOURCE} ${SHADERS}})
6874

6975
set_target_properties(DiligentCoreTest

Tests/DiligentCoreTest/assets/shaders/SPIRV/InputAttachments.psh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ float4 main() : SV_Target
1111

1212
// Process the input color
1313
return color * 2.0;
14-
}
14+
}

Tests/DiligentCoreTest/assets/shaders/SPIRV/MixedResources.psh

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,23 @@ struct PushConstants_t
3535
uint FrameCount;
3636
};
3737

38+
#if DXC
39+
40+
#define g_PushConstants PushConstants
41+
42+
[[vk::push_constant]]
43+
PushConstants_t g_PushConstants;
44+
45+
#else
46+
3847
[[vk::push_constant]]
3948
cbuffer PushConstants
4049
{
4150
PushConstants_t g_PushConstants;
4251
};
4352

53+
#endif
54+
4455
float4 main() : SV_Target
4556
{
4657
// Use uniform buffer data
@@ -63,4 +74,4 @@ float4 main() : SV_Target
6374
color.rgb *= sin(g_PushConstants.Time) * 0.5 + 0.5;
6475

6576
return color;
66-
}
77+
}

Tests/DiligentCoreTest/assets/shaders/SPIRV/PushConstants.psh

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Test for push constants
1+
// Test struct for push constants
22
struct PushConstants_t
33
{
44
float4x4 g_MVPMatrix;
@@ -8,12 +8,42 @@ struct PushConstants_t
88
uint g_Padding;
99
};
1010

11+
/*
12+
Note that glslang allows following declaration:
13+
1114
[[vk::push_constant]]
1215
cbuffer PushConstants
1316
{
1417
PushConstants_t g_PushConstants;
1518
};
1619

20+
to generate a push constant resource: "Push Constants 'PushConstants'".
21+
22+
but DXC does not allow it.
23+
24+
Glslang converts "[[vk::push_constant]] PushConstants_t g_PushConstants;" into an unnamed resource:
25+
"Uniform Buffer '$Global'" instead of what we expected: "Push Constants 'PushConstants'",
26+
27+
Thus we have to use "[[vk::push_constant]] cbuffer PushConstants" under glslang while "[[vk::push_constant]] PushConstants_t g_PushConstants;" under DXC.
28+
*/
29+
30+
#if DXC
31+
32+
#define g_PushConstants PushConstants
33+
34+
[[vk::push_constant]]
35+
PushConstants_t g_PushConstants;
36+
37+
#else
38+
39+
[[vk::push_constant]]
40+
cbuffer PushConstants
41+
{
42+
PushConstants_t g_PushConstants;
43+
};
44+
45+
#endif
46+
1747
float4 main() : SV_Target
1848
{
1949
// Use push constant data through the structure instance
@@ -23,4 +53,4 @@ float4 main() : SV_Target
2353
result.xy += g_PushConstants.g_Offset;
2454

2555
return result;
26-
}
56+
}

Tests/DiligentCoreTest/assets/shaders/SPIRV/StorageBuffers.psh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,4 @@ float4 main() : SV_Target
4040
g_RWAtomicBuffer.Store(0, atomicValue + 1);
4141

4242
return result;
43-
}
43+
}

Tests/DiligentCoreTest/assets/shaders/SPIRV/StorageImages.psh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ float4 main() : SV_Target
1717
color += g_RWImage3D[uint3(0, 0, 0)];
1818

1919
return color;
20-
}
20+
}

Tests/DiligentCoreTest/assets/shaders/SPIRV/TexelBuffers.psh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ float4 main() : SV_Target
1212

1313
// Read back from storage texel buffer
1414
return g_StorageTexelBuffer[0];
15-
}
15+
}

Tests/DiligentCoreTest/assets/shaders/SPIRV/Textures.psh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,4 @@ float4 main() : SV_Target
2727
float4 color6 = g_Texture.Sample(g_Texture_sampler, float2(0.5, 0.5));
2828

2929
return color1 + color2 + color3 + color4 + color5 + color6;
30-
}
30+
}

Tests/DiligentCoreTest/assets/shaders/SPIRV/UniformBuffers.psh

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
// Test for uniform buffers (constant buffers)
2-
cbuffer CB0 : register(b0)
3-
{
4-
float4x4 g_WorldViewProj;
5-
};
62

73
cbuffer CB1 : register(b1)
84
{
@@ -21,4 +17,4 @@ cbuffer CB2 : register(b2)
2117
float4 main() : SV_Target
2218
{
2319
return g_Color * g_MaterialParams;
24-
}
20+
}

Tests/DiligentCoreTest/src/ShaderTools/SPIRVShaderResourcesTest.cpp

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,23 @@ using namespace Diligent::Testing;
4444

4545
namespace
4646
{
47+
std::unique_ptr<IDXCompiler> g_pDXCompiler;
4748

4849
class SPIRVShaderResourcesTest : public ::testing::Test
4950
{
5051
protected:
5152
static void SetUpTestSuite()
5253
{
5354
GLSLangUtils::InitializeGlslang();
55+
56+
g_pDXCompiler = CreateDXCompiler(DXCompilerTarget::Vulkan, 0, nullptr);
5457
}
5558

5659
static void TearDownTestSuite()
5760
{
5861
GLSLangUtils::FinalizeGlslang();
62+
63+
g_pDXCompiler.reset();
5964
}
6065
};
6166

@@ -78,8 +83,7 @@ std::vector<unsigned int> LoadSPIRVFromHLSL(const char* FilePath, SHADER_TYPE Sh
7883
if (UseDXC)
7984
{
8085
// Use DXC to compile HLSL to SPIR-V.
81-
auto pDXC = CreateDXCompiler(DXCompilerTarget::Vulkan, 0, nullptr);
82-
if (!pDXC || !pDXC->IsLoaded())
86+
if (!g_pDXCompiler || !g_pDXCompiler->IsLoaded())
8387
return {};
8488

8589
ShaderCreateInfo ShaderCI;
@@ -88,14 +92,24 @@ std::vector<unsigned int> LoadSPIRVFromHLSL(const char* FilePath, SHADER_TYPE Sh
8892
ShaderCI.Desc = {"SPIRV test shader", ShaderType};
8993
ShaderCI.EntryPoint = "main";
9094

95+
ShaderMacro Macros[] = {{"DXC", "1"}};
96+
ShaderCI.Macros = {Macros, _countof(Macros)};
97+
9198
RefCntAutoPtr<IShaderSourceInputStreamFactory> pShaderSourceStreamFactory;
9299
CreateDefaultShaderSourceStreamFactory("shaders/SPIRV", &pShaderSourceStreamFactory);
93100
if (!pShaderSourceStreamFactory)
94101
return {};
102+
95103
ShaderCI.pShaderSourceStreamFactory = pShaderSourceStreamFactory;
96104

97105
RefCntAutoPtr<IDataBlob> pCompilerOutput;
98-
pDXC->Compile(ShaderCI, ShaderVersion{6, 0}, nullptr, nullptr, &SPIRV, &pCompilerOutput);
106+
g_pDXCompiler->Compile(ShaderCI, ShaderVersion{6, 0}, nullptr, nullptr, &SPIRV, &pCompilerOutput);
107+
108+
if (pCompilerOutput && pCompilerOutput->GetSize() > 0)
109+
{
110+
std::string CompilerOutput = static_cast<const char*>(pCompilerOutput->GetConstDataPtr());
111+
LOG_INFO_MESSAGE("DXC compiler output:\n", CompilerOutput);
112+
}
99113
}
100114
else
101115
{
@@ -219,15 +233,24 @@ void TestSPIRVResources(const char* FilePa
219233
bool IsGLSL = false)
220234
{
221235
const auto& SPIRV = IsGLSL ? LoadSPIRVFromGLSL(FilePath, ShaderType) : LoadSPIRVFromHLSL(FilePath, ShaderType);
222-
ASSERT_FALSE(SPIRV.empty()) << "Failed to compile HLSL to SPIRV with glslang: " << FilePath;
223-
236+
ASSERT_FALSE(SPIRV.empty()) <<
237+
(IsGLSL ?
238+
"Failed to compile GLSL to SPIRV with glslang: " :
239+
"Failed to compile HLSL to SPIRV with glslang: "
240+
) << FilePath;
241+
242+
LOG_INFO_MESSAGE(IsGLSL ? "Testing with GLSL->SPIRV with glslang:\n" : "Testing with HLSL->SPIRV with glslang:\n", FilePath);
243+
224244
TestSPIRVResourcesInternal(FilePath, RefResources, SPIRV, ShaderType, CombinedSamplerSuffix);
225245

226246
if (!IsGLSL)
227247
{
228248
//Test with DXC
229249
const auto& SPIRV_DXC = LoadSPIRVFromHLSL(FilePath, ShaderType, true);
230250
ASSERT_FALSE(SPIRV_DXC.empty()) << "Failed to compile HLSL to SPIRV with DXC: " << FilePath;
251+
252+
LOG_INFO_MESSAGE("Testing with HLSL->SPIRV with DXC:\n", FilePath);
253+
231254
TestSPIRVResourcesInternal(FilePath, RefResources, SPIRV_DXC, ShaderType, CombinedSamplerSuffix);
232255
}
233256
}
@@ -238,7 +261,6 @@ TEST_F(SPIRVShaderResourcesTest, UniformBuffers)
238261
{
239262
TestSPIRVResources("UniformBuffers.psh",
240263
{
241-
// CB0 is optimized away as it's not used in the shader
242264
SPIRVShaderResourceRefAttribs{"CB1", 1, SPIRVResourceType::UniformBuffer, RESOURCE_DIM_BUFFER, 0, 48, 0},
243265
SPIRVShaderResourceRefAttribs{"CB2", 1, SPIRVResourceType::UniformBuffer, RESOURCE_DIM_BUFFER, 0, 16, 0},
244266
});

0 commit comments

Comments
 (0)