|
26 | 26 |
|
27 | 27 | #include "SPIRVShaderResources.hpp" |
28 | 28 | #include "GLSLangUtils.hpp" |
| 29 | +#include "DXCompiler.hpp" |
29 | 30 | #include "DefaultShaderSourceStreamFactory.h" |
30 | 31 | #include "RefCntAutoPtr.hpp" |
31 | 32 | #include "EngineMemory.h" |
@@ -70,22 +71,52 @@ struct SPIRVShaderResourceRefAttribs |
70 | 71 | const Uint32 BufferStride; |
71 | 72 | }; |
72 | 73 |
|
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) |
74 | 75 | { |
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; |
80 | 77 |
|
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"; |
85 | 108 |
|
86 | | - ShaderCI.pShaderSourceStreamFactory = pShaderSourceStreamFactory; |
| 109 | + RefCntAutoPtr<IShaderSourceInputStreamFactory> pShaderSourceStreamFactory; |
| 110 | + CreateDefaultShaderSourceStreamFactory("shaders/SPIRV", &pShaderSourceStreamFactory); |
| 111 | + if (!pShaderSourceStreamFactory) |
| 112 | + return {}; |
87 | 113 |
|
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; |
89 | 120 | } |
90 | 121 |
|
91 | 122 | 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 |
130 | 161 | return GLSLangUtils::GLSLtoSPIRV(Attribs); |
131 | 162 | } |
132 | 163 |
|
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) |
138 | 169 | { |
139 | | - const auto SPIRV = IsGLSL ? LoadSPIRVFromGLSL(FilePath, ShaderType) : LoadSPIRVFromHLSL(FilePath, ShaderType); |
140 | | - ASSERT_FALSE(SPIRV.empty()) << "Failed to compile HLSL to SPIRV: " << FilePath; |
141 | | - |
142 | 170 | ShaderDesc ShaderDesc; |
143 | 171 | ShaderDesc.Name = "SPIRVResources test"; |
144 | 172 | ShaderDesc.ShaderType = ShaderType; |
@@ -184,6 +212,26 @@ void TestSPIRVResources(const char* FilePa |
184 | 212 | } |
185 | 213 | } |
186 | 214 |
|
| 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 | + |
187 | 235 | using SPIRVResourceType = SPIRVShaderResourceAttribs::ResourceType; |
188 | 236 |
|
189 | 237 | TEST_F(SPIRVShaderResourcesTest, UniformBuffers) |
|
0 commit comments