From b49acb11ca78e12ff8470ddc1c2db0e990b016b6 Mon Sep 17 00:00:00 2001 From: Hans-Kristian Arntzen Date: Fri, 23 Aug 2024 10:53:01 +0200 Subject: [PATCH] Handle SpecConstantOp used as switch case. --- .../frag/switch-spec-constant-op.frag | 32 +++++++++++++++++++ .../frag/switch-spec-constant-op.frag | 23 +++++++++++++ spirv_cross.cpp | 5 +++ 3 files changed, 60 insertions(+) create mode 100644 reference/shaders-no-opt/frag/switch-spec-constant-op.frag create mode 100644 shaders-no-opt/frag/switch-spec-constant-op.frag diff --git a/reference/shaders-no-opt/frag/switch-spec-constant-op.frag b/reference/shaders-no-opt/frag/switch-spec-constant-op.frag new file mode 100644 index 000000000..7dc375418 --- /dev/null +++ b/reference/shaders-no-opt/frag/switch-spec-constant-op.frag @@ -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; + } + } +} + diff --git a/shaders-no-opt/frag/switch-spec-constant-op.frag b/shaders-no-opt/frag/switch-spec-constant-op.frag new file mode 100644 index 000000000..1098618e1 --- /dev/null +++ b/shaders-no-opt/frag/switch-spec-constant-op.frag @@ -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; + } +} diff --git a/spirv_cross.cpp b/spirv_cross.cpp index 8c3e7d381..5471b3515 100644 --- a/spirv_cross.cpp +++ b/spirv_cross.cpp @@ -1850,6 +1850,11 @@ const SmallVector &Compiler::get_case_list(const SPIRBlock &blo const auto &type = get(constant->constant_type); width = type.width; } + else if (const auto *op = maybe_get(block.condition)) + { + const auto &type = get(op->basetype); + width = type.width; + } else if (const auto *var = maybe_get(block.condition)) { const auto &type = get(var->basetype);