Skip to content

Commit b0ce020

Browse files
SPIRVShaderResources: add Create method
1 parent 8bffc4d commit b0ce020

File tree

5 files changed

+44
-26
lines changed

5 files changed

+44
-26
lines changed

Graphics/GraphicsEngineVulkan/src/ShaderVkImpl.cpp

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -235,29 +235,17 @@ void ShaderVkImpl::Initialize(const ShaderCreateInfo& ShaderCI,
235235
{
236236
if ((ShaderCI.CompileFlags & SHADER_COMPILE_FLAG_SKIP_REFLECTION) == 0)
237237
{
238-
IMemoryAllocator& Allocator = GetRawAllocator();
239-
240-
std::unique_ptr<void, STDDeleterRawMem<void>> pRawMem{
241-
ALLOCATE(Allocator, "Memory for SPIRVShaderResources", SPIRVShaderResources, 1),
242-
STDDeleterRawMem<void>(Allocator),
243-
};
244-
245238
SPIRVShaderResources::CreateInfo ResCI;
246239
ResCI.ShaderType = m_Desc.ShaderType;
247240
ResCI.Name = m_Desc.Name;
248241
ResCI.CombinedSamplerSuffix = m_Desc.UseCombinedTextureSamplers ? m_Desc.CombinedSamplerSuffix : nullptr;
249242
ResCI.LoadShaderStageInputs = m_Desc.ShaderType == SHADER_TYPE_VERTEX;
250243
ResCI.LoadUniformBufferReflection = ShaderCI.LoadConstantBufferReflection;
251-
new (pRawMem.get()) SPIRVShaderResources // May throw
252-
{
253-
Allocator,
254-
m_SPIRV,
255-
ResCI,
256-
m_EntryPoint,
257-
};
244+
245+
m_pShaderResources = SPIRVShaderResources::Create(GetRawAllocator(), m_SPIRV, ResCI, &m_EntryPoint);
246+
258247
VERIFY_EXPR(ShaderCI.ByteCode != nullptr || m_EntryPoint == ShaderCI.EntryPoint ||
259248
(m_EntryPoint == "main" && (ShaderCI.CompileFlags & SHADER_COMPILE_FLAG_HLSL_TO_SPIRV_VIA_GLSL) != 0));
260-
m_pShaderResources.reset(static_cast<SPIRVShaderResources*>(pRawMem.release()), STDDeleterRawMem<SPIRVShaderResources>(Allocator));
261249

262250
if (ResCI.LoadShaderStageInputs && m_pShaderResources->IsHLSLSource())
263251
{

Graphics/GraphicsEngineWebGPU/src/ShaderWebGPUImpl.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,6 @@ std::vector<uint32_t> CompileShaderToSPIRV(const ShaderCreateInfo& S
6969
{
7070
SPIRV = GLSLangUtils::HLSLtoSPIRV(ShaderCI, GLSLangUtils::SpirvVersion::Vk100, WebGPUDefine, WebGPUShaderCI.ppCompilerOutput);
7171

72-
std::string EntryPoint;
73-
7472
SPIRVShaderResources::CreateInfo ResCI;
7573
ResCI.ShaderType = ShaderCI.Desc.ShaderType;
7674
ResCI.Name = ShaderCI.Desc.Name;
@@ -82,7 +80,6 @@ std::vector<uint32_t> CompileShaderToSPIRV(const ShaderCreateInfo& S
8280
GetRawAllocator(),
8381
SPIRV,
8482
ResCI,
85-
EntryPoint,
8683
};
8784

8885
if (ShaderCI.Desc.ShaderType == SHADER_TYPE_VERTEX)

Graphics/ShaderTools/include/SPIRVShaderResources.hpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,13 @@ class SPIRVShaderResources
207207
SPIRVShaderResources(IMemoryAllocator& Allocator,
208208
std::vector<uint32_t> spirv_binary,
209209
const CreateInfo& CI,
210-
std::string& EntryPoint) noexcept(false);
210+
std::string* pEntryPoint = nullptr) noexcept(false);
211+
212+
static std::shared_ptr<const SPIRVShaderResources> Create(
213+
IMemoryAllocator& Allocator,
214+
std::vector<uint32_t> spirv_binary,
215+
const CreateInfo& CI,
216+
std::string* pEntryPoint = nullptr) noexcept(false);
211217

212218
// clang-format off
213219
SPIRVShaderResources (const SPIRVShaderResources&) = delete;
@@ -390,9 +396,10 @@ class SPIRVShaderResources
390396

391397
// clang-format off
392398

393-
const char* GetCombinedSamplerSuffix() const { return m_CombinedSamplerSuffix; }
394-
const char* GetShaderName() const { return m_ShaderName; }
395-
bool IsUsingCombinedSamplers() const { return m_CombinedSamplerSuffix != nullptr; }
399+
const char* GetCombinedSamplerSuffix() const { return m_CombinedSamplerSuffix; }
400+
const char* GetShaderName() const { return m_ShaderName; }
401+
bool IsUsingCombinedSamplers() const { return m_CombinedSamplerSuffix != nullptr; }
402+
bool HasUniformBufferReflection() const { return m_UBReflectionBuffer != nullptr; }
396403

397404
// clang-format on
398405

Graphics/ShaderTools/src/SPIRVShaderResources.cpp

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ ShaderCodeBufferDescX LoadUBReflection(const diligent_spirv_cross::Compiler& Com
472472
SPIRVShaderResources::SPIRVShaderResources(IMemoryAllocator& Allocator,
473473
std::vector<uint32_t> spirv_binary,
474474
const CreateInfo& CI,
475-
std::string& EntryPoint) noexcept(false) :
475+
std::string* pEntryPoint) noexcept(false) :
476476
m_ShaderType{CI.ShaderType}
477477
{
478478
// https://github.com/KhronosGroup/SPIRV-Cross/wiki/Reflection-API-user-guide
@@ -483,6 +483,9 @@ SPIRVShaderResources::SPIRVShaderResources(IMemoryAllocator& Allocator,
483483
m_IsHLSLSource = ParsedIRSource.hlsl;
484484
diligent_spirv_cross::Compiler Compiler{std::move(parser.get_parsed_ir())};
485485

486+
std::string EntryPointLocal;
487+
std::string& EntryPoint = pEntryPoint != nullptr ? *pEntryPoint : EntryPointLocal;
488+
486489
spv::ExecutionModel ExecutionModel = ShaderTypeToSpvExecutionModel(m_ShaderType);
487490
auto EntryPoints = Compiler.get_entry_points_and_stages();
488491
for (const diligent_spirv_cross::EntryPoint& CurrEntryPoint : EntryPoints)
@@ -1117,4 +1120,30 @@ std::string SPIRVShaderResources::DumpResources() const
11171120
return ss.str();
11181121
}
11191122

1123+
1124+
std::shared_ptr<const SPIRVShaderResources> SPIRVShaderResources::Create(
1125+
IMemoryAllocator& Allocator,
1126+
std::vector<uint32_t> spirv_binary,
1127+
const CreateInfo& CI,
1128+
std::string* pEntryPoint) noexcept(false)
1129+
{
1130+
std::unique_ptr<void, STDDeleterRawMem<void>> pRawMem{
1131+
ALLOCATE(Allocator, "Memory for SPIRVShaderResources", SPIRVShaderResources, 1),
1132+
STDDeleterRawMem<void>(Allocator),
1133+
};
1134+
1135+
new (pRawMem.get()) SPIRVShaderResources // May throw
1136+
{
1137+
Allocator,
1138+
std::move(spirv_binary),
1139+
CI,
1140+
pEntryPoint,
1141+
};
1142+
1143+
return {
1144+
static_cast<SPIRVShaderResources*>(pRawMem.release()),
1145+
STDDeleterRawMem<SPIRVShaderResources>(Allocator),
1146+
};
1147+
}
1148+
11201149
} // namespace Diligent

Tests/DiligentCoreTest/src/ShaderTools/SPIRVShaderResourcesTest.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,13 +211,10 @@ void TestSPIRVResources(const char*
211211
SPIRVShaderResources::CreateInfo ResCI;
212212
ResCI.ShaderType = ShaderType;
213213
ResCI.Name = "SPIRVResources test";
214-
215-
std::string EntryPoint;
216214
SPIRVShaderResources Resources{
217215
GetRawAllocator(),
218216
SPIRV,
219217
ResCI,
220-
EntryPoint,
221218
};
222219

223220
LOG_INFO_MESSAGE("SPIRV Resources:\n", Resources.DumpResources());

0 commit comments

Comments
 (0)