Skip to content

Commit 8bffc4d

Browse files
SPIRVShaderResources: use CreateInfo struct to group constructor parameters
1 parent 697eff0 commit 8bffc4d

File tree

5 files changed

+53
-46
lines changed

5 files changed

+53
-46
lines changed

Graphics/GraphicsEngineVulkan/src/ShaderVkImpl.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2025 Diligent Graphics LLC
2+
* Copyright 2019-2026 Diligent Graphics LLC
33
* Copyright 2015-2019 Egor Yusov
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -241,22 +241,25 @@ void ShaderVkImpl::Initialize(const ShaderCreateInfo& ShaderCI,
241241
ALLOCATE(Allocator, "Memory for SPIRVShaderResources", SPIRVShaderResources, 1),
242242
STDDeleterRawMem<void>(Allocator),
243243
};
244-
const bool LoadShaderInputs = m_Desc.ShaderType == SHADER_TYPE_VERTEX;
244+
245+
SPIRVShaderResources::CreateInfo ResCI;
246+
ResCI.ShaderType = m_Desc.ShaderType;
247+
ResCI.Name = m_Desc.Name;
248+
ResCI.CombinedSamplerSuffix = m_Desc.UseCombinedTextureSamplers ? m_Desc.CombinedSamplerSuffix : nullptr;
249+
ResCI.LoadShaderStageInputs = m_Desc.ShaderType == SHADER_TYPE_VERTEX;
250+
ResCI.LoadUniformBufferReflection = ShaderCI.LoadConstantBufferReflection;
245251
new (pRawMem.get()) SPIRVShaderResources // May throw
246252
{
247253
Allocator,
248254
m_SPIRV,
249-
m_Desc,
250-
m_Desc.UseCombinedTextureSamplers ? m_Desc.CombinedSamplerSuffix : nullptr,
251-
LoadShaderInputs,
252-
ShaderCI.LoadConstantBufferReflection,
253-
m_EntryPoint //
255+
ResCI,
256+
m_EntryPoint,
254257
};
255258
VERIFY_EXPR(ShaderCI.ByteCode != nullptr || m_EntryPoint == ShaderCI.EntryPoint ||
256259
(m_EntryPoint == "main" && (ShaderCI.CompileFlags & SHADER_COMPILE_FLAG_HLSL_TO_SPIRV_VIA_GLSL) != 0));
257260
m_pShaderResources.reset(static_cast<SPIRVShaderResources*>(pRawMem.release()), STDDeleterRawMem<SPIRVShaderResources>(Allocator));
258261

259-
if (LoadShaderInputs && m_pShaderResources->IsHLSLSource())
262+
if (ResCI.LoadShaderStageInputs && m_pShaderResources->IsHLSLSource())
260263
{
261264
m_pShaderResources->MapHLSLVertexShaderInputs(m_SPIRV);
262265
}

Graphics/GraphicsEngineWebGPU/src/ShaderWebGPUImpl.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023-2025 Diligent Graphics LLC
2+
* Copyright 2023-2026 Diligent Graphics LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -71,13 +71,17 @@ std::vector<uint32_t> CompileShaderToSPIRV(const ShaderCreateInfo& S
7171

7272
std::string EntryPoint;
7373

74+
SPIRVShaderResources::CreateInfo ResCI;
75+
ResCI.ShaderType = ShaderCI.Desc.ShaderType;
76+
ResCI.Name = ShaderCI.Desc.Name;
77+
ResCI.CombinedSamplerSuffix = ShaderCI.Desc.UseCombinedTextureSamplers ? ShaderCI.Desc.CombinedSamplerSuffix : nullptr;
78+
ResCI.LoadShaderStageInputs = ShaderCI.Desc.ShaderType == SHADER_TYPE_VERTEX;
79+
ResCI.LoadUniformBufferReflection = false;
80+
7481
SPIRVShaderResources Resources{
7582
GetRawAllocator(),
7683
SPIRV,
77-
ShaderCI.Desc,
78-
ShaderCI.Desc.UseCombinedTextureSamplers ? ShaderCI.Desc.CombinedSamplerSuffix : nullptr,
79-
ShaderCI.Desc.ShaderType == SHADER_TYPE_VERTEX, // LoadShaderStageInputs
80-
false, // LoadUniformBufferReflection
84+
ResCI,
8185
EntryPoint,
8286
};
8387

Graphics/ShaderTools/include/SPIRVShaderResources.hpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,12 +196,17 @@ static_assert(sizeof(SPIRVShaderStageInputAttribs) % sizeof(void*) == 0, "Size o
196196
class SPIRVShaderResources
197197
{
198198
public:
199+
struct CreateInfo
200+
{
201+
SHADER_TYPE ShaderType = SHADER_TYPE_UNKNOWN;
202+
const char* Name = nullptr;
203+
const char* CombinedSamplerSuffix = nullptr;
204+
bool LoadShaderStageInputs = false;
205+
bool LoadUniformBufferReflection = false;
206+
};
199207
SPIRVShaderResources(IMemoryAllocator& Allocator,
200208
std::vector<uint32_t> spirv_binary,
201-
const ShaderDesc& shaderDesc,
202-
const char* CombinedSamplerSuffix,
203-
bool LoadShaderStageInputs,
204-
bool LoadUniformBufferReflection,
209+
const CreateInfo& CI,
205210
std::string& EntryPoint) noexcept(false);
206211

207212
// clang-format off

Graphics/ShaderTools/src/SPIRVShaderResources.cpp

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2025 Diligent Graphics LLC
2+
* Copyright 2019-2026 Diligent Graphics LLC
33
* Copyright 2015-2019 Egor Yusov
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -471,12 +471,9 @@ ShaderCodeBufferDescX LoadUBReflection(const diligent_spirv_cross::Compiler& Com
471471

472472
SPIRVShaderResources::SPIRVShaderResources(IMemoryAllocator& Allocator,
473473
std::vector<uint32_t> spirv_binary,
474-
const ShaderDesc& shaderDesc,
475-
const char* CombinedSamplerSuffix,
476-
bool LoadShaderStageInputs,
477-
bool LoadUniformBufferReflection,
474+
const CreateInfo& CI,
478475
std::string& EntryPoint) noexcept(false) :
479-
m_ShaderType{shaderDesc.ShaderType}
476+
m_ShaderType{CI.ShaderType}
480477
{
481478
// https://github.com/KhronosGroup/SPIRV-Cross/wiki/Reflection-API-user-guide
482479
diligent_spirv_cross::Parser parser{std::move(spirv_binary)};
@@ -486,15 +483,15 @@ SPIRVShaderResources::SPIRVShaderResources(IMemoryAllocator& Allocator,
486483
m_IsHLSLSource = ParsedIRSource.hlsl;
487484
diligent_spirv_cross::Compiler Compiler{std::move(parser.get_parsed_ir())};
488485

489-
spv::ExecutionModel ExecutionModel = ShaderTypeToSpvExecutionModel(shaderDesc.ShaderType);
486+
spv::ExecutionModel ExecutionModel = ShaderTypeToSpvExecutionModel(m_ShaderType);
490487
auto EntryPoints = Compiler.get_entry_points_and_stages();
491488
for (const diligent_spirv_cross::EntryPoint& CurrEntryPoint : EntryPoints)
492489
{
493490
if (CurrEntryPoint.execution_model == ExecutionModel)
494491
{
495492
if (!EntryPoint.empty())
496493
{
497-
LOG_WARNING_MESSAGE("More than one entry point of type ", GetShaderTypeLiteralName(shaderDesc.ShaderType), " found in SPIRV binary for shader '", shaderDesc.Name, "'. The first one ('", EntryPoint, "') will be used.");
494+
LOG_WARNING_MESSAGE("More than one entry point of type ", GetShaderTypeLiteralName(m_ShaderType), " found in SPIRV binary for shader '", CI.Name, "'. The first one ('", EntryPoint, "') will be used.");
498495
}
499496
else
500497
{
@@ -504,7 +501,7 @@ SPIRVShaderResources::SPIRVShaderResources(IMemoryAllocator& Allocator,
504501
}
505502
if (EntryPoint.empty())
506503
{
507-
LOG_ERROR_AND_THROW("Unable to find entry point of type ", GetShaderTypeLiteralName(shaderDesc.ShaderType), " in SPIRV binary for shader '", shaderDesc.Name, "'");
504+
LOG_ERROR_AND_THROW("Unable to find entry point of type ", GetShaderTypeLiteralName(m_ShaderType), " in SPIRV binary for shader '", CI.Name, "'");
508505
}
509506
Compiler.set_entry_point(EntryPoint, ExecutionModel);
510507

@@ -521,7 +518,7 @@ SPIRVShaderResources::SPIRVShaderResources(IMemoryAllocator& Allocator,
521518
// Process push constant buffers - Vulkan spec allows only one push_constant buffer per pipeline
522519
if (resources.push_constant_buffers.size() > 1)
523520
{
524-
LOG_ERROR_AND_THROW("Shader '", shaderDesc.Name, "' contains ", resources.push_constant_buffers.size(),
521+
LOG_ERROR_AND_THROW("Shader '", CI.Name, "' contains ", resources.push_constant_buffers.size(),
525522
" push constant buffers, but Vulkan spec allows only one push_constant buffer per pipeline.");
526523
}
527524
// For push constants, use GetPushConstantName which also tries to get the name from the base type
@@ -543,16 +540,17 @@ SPIRVShaderResources::SPIRVShaderResources(IMemoryAllocator& Allocator,
543540
ResourceNamesPoolSize += res.name.length() + 1;
544541
}
545542

546-
if (CombinedSamplerSuffix != nullptr)
543+
if (CI.CombinedSamplerSuffix != nullptr)
547544
{
548-
ResourceNamesPoolSize += strlen(CombinedSamplerSuffix) + 1;
545+
ResourceNamesPoolSize += strlen(CI.CombinedSamplerSuffix) + 1;
549546
}
550547

551-
VERIFY_EXPR(shaderDesc.Name != nullptr);
552-
ResourceNamesPoolSize += strlen(shaderDesc.Name) + 1;
548+
VERIFY_EXPR(CI.Name != nullptr);
549+
ResourceNamesPoolSize += strlen(CI.Name) + 1;
553550

554551
Uint32 NumShaderStageInputs = 0;
555552

553+
bool LoadShaderStageInputs = CI.LoadShaderStageInputs;
556554
if (!m_IsHLSLSource || resources.stage_inputs.empty())
557555
LoadShaderStageInputs = false;
558556
if (LoadShaderStageInputs)
@@ -587,7 +585,7 @@ SPIRVShaderResources::SPIRVShaderResources(IMemoryAllocator& Allocator,
587585
LoadShaderStageInputs = false;
588586
if (m_IsHLSLSource)
589587
{
590-
LOG_WARNING_MESSAGE("SPIRV byte code of shader '", shaderDesc.Name,
588+
LOG_WARNING_MESSAGE("SPIRV byte code of shader '", CI.Name,
591589
"' does not use SPV_GOOGLE_hlsl_functionality1 extension. "
592590
"As a result, it is not possible to get semantics of shader inputs and map them to proper locations. "
593591
"The shader will still work correctly if all attributes are declared in ascending order without any gaps. "
@@ -632,7 +630,7 @@ SPIRVShaderResources::SPIRVShaderResources(IMemoryAllocator& Allocator,
632630
static_cast<Uint32>(Size) //
633631
};
634632

635-
if (LoadUniformBufferReflection)
633+
if (CI.LoadUniformBufferReflection)
636634
{
637635
UBReflections.emplace_back(LoadUBReflection(Compiler, UB, m_IsHLSLSource));
638636
}
@@ -813,12 +811,12 @@ SPIRVShaderResources::SPIRVShaderResources(IMemoryAllocator& Allocator,
813811

814812
static_assert(Uint32{SPIRVShaderResourceAttribs::ResourceType::NumResourceTypes} == 13, "Please initialize SPIRVShaderResourceAttribs for the new resource type here");
815813

816-
if (CombinedSamplerSuffix != nullptr)
814+
if (CI.CombinedSamplerSuffix != nullptr)
817815
{
818-
m_CombinedSamplerSuffix = ResourceNamesPool.CopyString(CombinedSamplerSuffix);
816+
m_CombinedSamplerSuffix = ResourceNamesPool.CopyString(CI.CombinedSamplerSuffix);
819817
}
820818

821-
m_ShaderName = ResourceNamesPool.CopyString(shaderDesc.Name);
819+
m_ShaderName = ResourceNamesPool.CopyString(CI.Name);
822820

823821
if (LoadShaderStageInputs)
824822
{
@@ -840,15 +838,15 @@ SPIRVShaderResources::SPIRVShaderResources(IMemoryAllocator& Allocator,
840838

841839
VERIFY(ResourceNamesPool.GetRemainingSize() == 0, "Names pool must be empty");
842840

843-
if (shaderDesc.ShaderType == SHADER_TYPE_COMPUTE)
841+
if (m_ShaderType == SHADER_TYPE_COMPUTE)
844842
{
845843
for (uint32_t i = 0; i < m_ComputeGroupSize.size(); ++i)
846844
m_ComputeGroupSize[i] = Compiler.get_execution_mode_argument(spv::ExecutionModeLocalSize, i);
847845
}
848846

849847
if (!UBReflections.empty())
850848
{
851-
VERIFY_EXPR(LoadUniformBufferReflection);
849+
VERIFY_EXPR(CI.LoadUniformBufferReflection);
852850
VERIFY_EXPR(UBReflections.size() == GetNumUBs());
853851
m_UBReflectionBuffer = ShaderCodeBufferDescX::PackArray(UBReflections.cbegin(), UBReflections.cend(), GetRawAllocator());
854852
}

Tests/DiligentCoreTest/src/ShaderTools/SPIRVShaderResourcesTest.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2025 Diligent Graphics LLC
2+
* Copyright 2025-2026 Diligent Graphics LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -208,18 +208,15 @@ void TestSPIRVResources(const char*
208208
ASSERT_FALSE(SPIRV.empty()) << "Failed to patch shader: " << FilePath;
209209
}
210210

211-
ShaderDesc ShaderDesc;
212-
ShaderDesc.Name = "SPIRVResources test";
213-
ShaderDesc.ShaderType = ShaderType;
211+
SPIRVShaderResources::CreateInfo ResCI;
212+
ResCI.ShaderType = ShaderType;
213+
ResCI.Name = "SPIRVResources test";
214214

215215
std::string EntryPoint;
216216
SPIRVShaderResources Resources{
217217
GetRawAllocator(),
218218
SPIRV,
219-
ShaderDesc,
220-
nullptr,
221-
false, // LoadShaderStageInputs
222-
false, // LoadUniformBufferReflection
219+
ResCI,
223220
EntryPoint,
224221
};
225222

0 commit comments

Comments
 (0)