Skip to content

Commit ce3da9d

Browse files
committed
Let's test with DXC
1 parent cd07435 commit ce3da9d

File tree

1 file changed

+68
-20
lines changed

1 file changed

+68
-20
lines changed

Tests/DiligentCoreTest/src/ShaderTools/SPIRVShaderResourcesTest.cpp

Lines changed: 68 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
#include "SPIRVShaderResources.hpp"
2828
#include "GLSLangUtils.hpp"
29+
#include "DXCompiler.hpp"
2930
#include "DefaultShaderSourceStreamFactory.h"
3031
#include "RefCntAutoPtr.hpp"
3132
#include "EngineMemory.h"
@@ -70,22 +71,52 @@ struct SPIRVShaderResourceRefAttribs
7071
const Uint32 BufferStride;
7172
};
7273

73-
std::vector<unsigned int> LoadSPIRVFromHLSL(const char* FilePath, SHADER_TYPE ShaderType = SHADER_TYPE_PIXEL)
74+
std::vector<unsigned int> LoadSPIRVFromHLSL(const char* FilePath, SHADER_TYPE ShaderType = SHADER_TYPE_PIXEL, bool UseDXC = false)
7475
{
75-
ShaderCreateInfo ShaderCI;
76-
ShaderCI.SourceLanguage = SHADER_SOURCE_LANGUAGE_HLSL;
77-
ShaderCI.FilePath = FilePath;
78-
ShaderCI.Desc = {"SPIRV test shader", ShaderType};
79-
ShaderCI.EntryPoint = "main";
76+
std::vector<unsigned int> SPIRV;
8077

81-
RefCntAutoPtr<IShaderSourceInputStreamFactory> pShaderSourceStreamFactory;
82-
CreateDefaultShaderSourceStreamFactory("shaders/SPIRV", &pShaderSourceStreamFactory);
83-
if (!pShaderSourceStreamFactory)
84-
return {};
78+
if (UseDXC)
79+
{
80+
// Use DXC to compile HLSL to SPIR-V.
81+
auto pDXC = CreateDXCompiler(DXCompilerTarget::Vulkan, 0, nullptr);
82+
if (!pDXC || !pDXC->IsLoaded())
83+
return {};
84+
85+
ShaderCreateInfo ShaderCI;
86+
ShaderCI.SourceLanguage = SHADER_SOURCE_LANGUAGE_HLSL;
87+
ShaderCI.FilePath = FilePath;
88+
ShaderCI.Desc = {"SPIRV test shader", ShaderType};
89+
ShaderCI.EntryPoint = "main";
90+
91+
RefCntAutoPtr<IShaderSourceInputStreamFactory> pShaderSourceStreamFactory;
92+
CreateDefaultShaderSourceStreamFactory("shaders/SPIRV", &pShaderSourceStreamFactory);
93+
if (!pShaderSourceStreamFactory)
94+
return {};
95+
ShaderCI.pShaderSourceStreamFactory = pShaderSourceStreamFactory;
96+
97+
RefCntAutoPtr<IDataBlob> pCompilerOutput;
98+
pDXC->Compile(ShaderCI, ShaderVersion{6, 0}, nullptr, nullptr, &SPIRV, &pCompilerOutput);
99+
}
100+
else
101+
{
102+
// Use Glslang to compile HLSL to SPIR-V.
103+
ShaderCreateInfo ShaderCI;
104+
ShaderCI.SourceLanguage = SHADER_SOURCE_LANGUAGE_HLSL;
105+
ShaderCI.FilePath = FilePath;
106+
ShaderCI.Desc = {"SPIRV test shader", ShaderType};
107+
ShaderCI.EntryPoint = "main";
85108

86-
ShaderCI.pShaderSourceStreamFactory = pShaderSourceStreamFactory;
109+
RefCntAutoPtr<IShaderSourceInputStreamFactory> pShaderSourceStreamFactory;
110+
CreateDefaultShaderSourceStreamFactory("shaders/SPIRV", &pShaderSourceStreamFactory);
111+
if (!pShaderSourceStreamFactory)
112+
return {};
87113

88-
return GLSLangUtils::HLSLtoSPIRV(ShaderCI, GLSLangUtils::SpirvVersion::Vk100, nullptr, nullptr);
114+
ShaderCI.pShaderSourceStreamFactory = pShaderSourceStreamFactory;
115+
116+
SPIRV = GLSLangUtils::HLSLtoSPIRV(ShaderCI, GLSLangUtils::SpirvVersion::Vk100, nullptr, nullptr);
117+
}
118+
119+
return SPIRV;
89120
}
90121

91122
std::vector<unsigned int> LoadSPIRVFromGLSL(const char* FilePath, SHADER_TYPE ShaderType = SHADER_TYPE_PIXEL)
@@ -130,15 +161,12 @@ std::vector<unsigned int> LoadSPIRVFromGLSL(const char* FilePath, SHADER_TYPE Sh
130161
return GLSLangUtils::GLSLtoSPIRV(Attribs);
131162
}
132163

133-
void TestSPIRVResources(const char* FilePath,
134-
const std::vector<SPIRVShaderResourceRefAttribs>& RefResources,
135-
SHADER_TYPE ShaderType = SHADER_TYPE_PIXEL,
136-
const char* CombinedSamplerSuffix = nullptr,
137-
bool IsGLSL = false)
164+
void TestSPIRVResourcesInternal(const char* FilePath,
165+
const std::vector<SPIRVShaderResourceRefAttribs>& RefResources,
166+
const std::vector<unsigned int>& SPIRV,
167+
SHADER_TYPE ShaderType = SHADER_TYPE_PIXEL,
168+
const char* CombinedSamplerSuffix = nullptr)
138169
{
139-
const auto SPIRV = IsGLSL ? LoadSPIRVFromGLSL(FilePath, ShaderType) : LoadSPIRVFromHLSL(FilePath, ShaderType);
140-
ASSERT_FALSE(SPIRV.empty()) << "Failed to compile HLSL to SPIRV: " << FilePath;
141-
142170
ShaderDesc ShaderDesc;
143171
ShaderDesc.Name = "SPIRVResources test";
144172
ShaderDesc.ShaderType = ShaderType;
@@ -184,6 +212,26 @@ void TestSPIRVResources(const char* FilePa
184212
}
185213
}
186214

215+
void TestSPIRVResources(const char* FilePath,
216+
const std::vector<SPIRVShaderResourceRefAttribs>& RefResources,
217+
SHADER_TYPE ShaderType = SHADER_TYPE_PIXEL,
218+
const char* CombinedSamplerSuffix = nullptr,
219+
bool IsGLSL = false)
220+
{
221+
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+
224+
TestSPIRVResourcesInternal(FilePath, RefResources, SPIRV, ShaderType, CombinedSamplerSuffix);
225+
226+
if (!IsGLSL)
227+
{
228+
//Test with DXC
229+
const auto& SPIRV_DXC = LoadSPIRVFromHLSL(FilePath, ShaderType, true);
230+
ASSERT_FALSE(SPIRV_DXC.empty()) << "Failed to compile HLSL to SPIRV with DXC: " << FilePath;
231+
TestSPIRVResourcesInternal(FilePath, RefResources, SPIRV_DXC, ShaderType, CombinedSamplerSuffix);
232+
}
233+
}
234+
187235
using SPIRVResourceType = SPIRVShaderResourceAttribs::ResourceType;
188236

189237
TEST_F(SPIRVShaderResourcesTest, UniformBuffers)

0 commit comments

Comments
 (0)