Skip to content

Commit facb350

Browse files
hzqstTheMostDiligent
authored andcommitted
Add TestConvertUBOToPushConstant_InvalidBlockName and TestConvertUBOToPushConstant_InvalidResourceType, so ConvertUBOToPushConstants with invalid block name, or invalid resource type error out properly.
1 parent fe2f239 commit facb350

File tree

2 files changed

+104
-4
lines changed

2 files changed

+104
-4
lines changed

Graphics/ShaderTools/src/ConvertUBOToPushConstant.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ class ConvertUBOToPushConstantPass : public spvtools::opt::Pass
9393
if (candidate_ids.empty())
9494
{
9595
// Block name not found
96-
return Status::SuccessWithoutChange;
96+
return Status::Failure;
9797
}
9898

9999
// Try each candidate ID to find a UniformBuffer
@@ -188,7 +188,7 @@ class ConvertUBOToPushConstantPass : public spvtools::opt::Pass
188188
if (target_var == nullptr)
189189
{
190190
// No UniformBuffer found with the given block name
191-
return Status::SuccessWithoutChange;
191+
return Status::Failure;
192192
}
193193

194194
uint32_t target_var_id = target_var->result_id();
@@ -197,7 +197,7 @@ class ConvertUBOToPushConstantPass : public spvtools::opt::Pass
197197
spvtools::opt::Instruction* ptr_type_inst = get_def_use_mgr()->GetDef(target_var->type_id());
198198
if (ptr_type_inst == nullptr || ptr_type_inst->opcode() != spv::Op::OpTypePointer)
199199
{
200-
return Status::SuccessWithoutChange;
200+
return Status::Failure;
201201
}
202202

203203
// Get the pointee type ID

Tests/DiligentCoreTest/src/ShaderTools/SPIRVShaderResourcesTest.cpp

Lines changed: 101 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,11 +194,21 @@ void TestSPIRVResources(const char*
194194
std::vector<unsigned int> SPIRV = (SourceLanguage == SHADER_SOURCE_LANGUAGE_GLSL) ?
195195
LoadSPIRVFromGLSL(FilePath, ShaderType) :
196196
LoadSPIRVFromHLSL(FilePath, ShaderType, Compiler);
197-
ASSERT_TRUE(!SPIRV.empty()) << "Failed to compile shader: " << FilePath;
197+
VERIFY(!SPIRV.empty(), "Failed to compile shader: ", FilePath);
198+
199+
if (SPIRV.empty())
200+
return;
198201

199202
if (PatchSPIRVCallback)
203+
{
200204
PatchSPIRVCallback(SPIRV);
201205

206+
VERIFY(!SPIRV.empty(), "Failed to patch shader: ", FilePath);
207+
}
208+
209+
if (SPIRV.empty())
210+
return;
211+
202212
ShaderDesc ShaderDesc;
203213
ShaderDesc.Name = "SPIRVResources test";
204214
ShaderDesc.ShaderType = ShaderType;
@@ -319,6 +329,10 @@ void TestConvertUBOToPushConstant(SHADER_COMPILER Compiler)
319329
SHADER_SOURCE_LANGUAGE_HLSL,
320330
[PatchedAttribName](std::vector<unsigned int>& SPIRV) {
321331
SPIRV = ConvertUBOToPushConstants(SPIRV, PatchedAttribName);
332+
if (SPIRV.empty())
333+
{
334+
LOG_ERROR("ConvertUBOToPushConstants: Could not find uniform buffer block: ", PatchedAttribName);
335+
}
322336
});
323337
}
324338
}
@@ -333,6 +347,92 @@ TEST_F(SPIRVShaderResourcesTest, ConvertUBOToPushConstant_DXC)
333347
TestConvertUBOToPushConstant(SHADER_COMPILER_DXC);
334348
}
335349

350+
void TestConvertUBOToPushConstant_InvalidBlockName(SHADER_COMPILER Compiler)
351+
{
352+
TestingEnvironment* const pEnv = TestingEnvironment::GetInstance();
353+
354+
pEnv->SetErrorAllowance(2, "Errors below are expected: testing ConvertUBOToPushConstants invalid block name failure\n");
355+
pEnv->PushExpectedErrorSubstring("Failed to patch shader:", false);
356+
pEnv->PushExpectedErrorSubstring("ConvertUBOToPushConstants: Could not find uniform buffer block", false);
357+
358+
//"CB5" is not available in given HLSL thus cannot be patched with ConvertUBOToPushConstants.
359+
std::string PatchedAttribName = "CB5";
360+
361+
TestSPIRVResources("UniformBuffers.psh",
362+
{
363+
SPIRVShaderResourceRefAttribs{"CB1", 1, SPIRVResourceType::UniformBuffer, RESOURCE_DIM_BUFFER, 0, 48, 0},
364+
SPIRVShaderResourceRefAttribs{"CB2", 1, SPIRVResourceType::UniformBuffer, RESOURCE_DIM_BUFFER, 0, 16, 0},
365+
SPIRVShaderResourceRefAttribs{"CB3", 1, SPIRVResourceType::UniformBuffer, RESOURCE_DIM_BUFFER, 0, 32, 0},
366+
SPIRVShaderResourceRefAttribs{"CB4", 1, SPIRVResourceType::UniformBuffer, RESOURCE_DIM_BUFFER, 0, 32, 0},
367+
},
368+
Compiler,
369+
SHADER_TYPE_PIXEL,
370+
SHADER_SOURCE_LANGUAGE_HLSL,
371+
[PatchedAttribName](std::vector<unsigned int>& SPIRV) {
372+
SPIRV = ConvertUBOToPushConstants(SPIRV, PatchedAttribName);
373+
if (SPIRV.empty())
374+
{
375+
LOG_ERROR("ConvertUBOToPushConstants: Could not find uniform buffer block: ", PatchedAttribName);
376+
}
377+
});
378+
379+
pEnv->SetErrorAllowance(0);
380+
}
381+
382+
TEST_F(SPIRVShaderResourcesTest, ConvertUBOToPushConstant_InvalidBlockName_GLSLang)
383+
{
384+
TestConvertUBOToPushConstant_InvalidBlockName(SHADER_COMPILER_GLSLANG);
385+
}
386+
387+
TEST_F(SPIRVShaderResourcesTest, ConvertUBOToPushConstant_InvalidBlockName_DXC)
388+
{
389+
TestConvertUBOToPushConstant_InvalidBlockName(SHADER_COMPILER_DXC);
390+
}
391+
392+
void TestConvertUBOToPushConstant_InvalidResourceType(SHADER_COMPILER Compiler)
393+
{
394+
TestingEnvironment* const pEnv = TestingEnvironment::GetInstance();
395+
396+
pEnv->SetErrorAllowance(2, "Errors below are expected: testing ConvertUBOToPushConstants invalid resource type failure\n");
397+
pEnv->PushExpectedErrorSubstring("Failed to patch shader:", false);
398+
pEnv->PushExpectedErrorSubstring("ConvertUBOToPushConstants: Could not find uniform buffer block", false);
399+
400+
//"g_ROBuffer" is a ROStorageBuffer and cannot be patched with ConvertUBOToPushConstants.
401+
std::string PatchedAttribName = "g_ROBuffer";
402+
403+
TestSPIRVResources("StorageBuffers.psh",
404+
{
405+
// StructuredBuffers have BufferStaticSize=0 (runtime array) and BufferStride is the element size
406+
SPIRVShaderResourceRefAttribs{"g_ROBuffer", 1, SPIRVResourceType::ROStorageBuffer, RESOURCE_DIM_BUFFER, 0, 0, 32},
407+
SPIRVShaderResourceRefAttribs{"g_RWBuffer", 1, SPIRVResourceType::RWStorageBuffer, RESOURCE_DIM_BUFFER, 0, 0, 64},
408+
// ByteAddressBuffers also have BufferStaticSize=0 and BufferStride=4 (uint size)
409+
SPIRVShaderResourceRefAttribs{"g_ROAtomicBuffer", 1, SPIRVResourceType::ROStorageBuffer, RESOURCE_DIM_BUFFER, 0, 0, 4},
410+
SPIRVShaderResourceRefAttribs{"g_RWAtomicBuffer", 1, SPIRVResourceType::RWStorageBuffer, RESOURCE_DIM_BUFFER, 0, 0, 4},
411+
},
412+
Compiler,
413+
SHADER_TYPE_PIXEL,
414+
SHADER_SOURCE_LANGUAGE_HLSL,
415+
[PatchedAttribName](std::vector<unsigned int>& SPIRV) {
416+
SPIRV = ConvertUBOToPushConstants(SPIRV, PatchedAttribName);
417+
if (SPIRV.empty())
418+
{
419+
LOG_ERROR("ConvertUBOToPushConstants: Could not find uniform buffer block: ", PatchedAttribName);
420+
}
421+
});
422+
423+
pEnv->SetErrorAllowance(0);
424+
}
425+
426+
TEST_F(SPIRVShaderResourcesTest, ConvertUBOToPushConstant_InvalidResourceType_GLSLang)
427+
{
428+
TestConvertUBOToPushConstant_InvalidResourceType(SHADER_COMPILER_GLSLANG);
429+
}
430+
431+
TEST_F(SPIRVShaderResourcesTest, ConvertUBOToPushConstant_InvalidResourceType_DXC)
432+
{
433+
TestConvertUBOToPushConstant_InvalidResourceType(SHADER_COMPILER_DXC);
434+
}
435+
336436
void TestStorageBuffers(SHADER_COMPILER Compiler)
337437
{
338438
TestSPIRVResources("StorageBuffers.psh",

0 commit comments

Comments
 (0)