Skip to content

Commit

Permalink
Merge pull request #2367 from KhronosGroup/spec-constant-switch
Browse files Browse the repository at this point in the history
Handle SpecConstantOp used as switch case.
  • Loading branch information
HansKristian-Work authored Aug 23, 2024
2 parents d33a390 + b49acb1 commit dae7a68
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
32 changes: 32 additions & 0 deletions reference/shaders-no-opt/frag/switch-spec-constant-op.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#version 450

#ifndef SPIRV_CROSS_CONSTANT_ID_0
#define SPIRV_CROSS_CONSTANT_ID_0 4
#endif
const int FOO = SPIRV_CROSS_CONSTANT_ID_0;
const int _9 = (FOO + 4);

layout(location = 0) out vec4 FragColor;

void main()
{
switch (_9)
{
case 4:
{
FragColor = vec4(10.0);
break;
}
case 6:
{
FragColor = vec4(20.0);
break;
}
default:
{
FragColor = vec4(0.0);
break;
}
}
}

23 changes: 23 additions & 0 deletions shaders-no-opt/frag/switch-spec-constant-op.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#version 450

layout(location = 0) out vec4 FragColor;
layout(location = 0) in vec4 Attr;
layout(constant_id = 0) const int FOO = 4;

void main()
{
switch (FOO + 4)
{
case 4:
FragColor = vec4(10.0);
break;

case 6:
FragColor = vec4(20.0);
break;

default:
FragColor = vec4(0.0);
break;
}
}
5 changes: 5 additions & 0 deletions spirv_cross.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1850,6 +1850,11 @@ const SmallVector<SPIRBlock::Case> &Compiler::get_case_list(const SPIRBlock &blo
const auto &type = get<SPIRType>(constant->constant_type);
width = type.width;
}
else if (const auto *op = maybe_get<SPIRConstantOp>(block.condition))
{
const auto &type = get<SPIRType>(op->basetype);
width = type.width;
}
else if (const auto *var = maybe_get<SPIRVariable>(block.condition))
{
const auto &type = get<SPIRType>(var->basetype);
Expand Down

0 comments on commit dae7a68

Please sign in to comment.