From ec857f6778ff9d4bb7484c0442525491f5743ff6 Mon Sep 17 00:00:00 2001 From: Chip Davis Date: Wed, 19 Sep 2018 09:13:30 -0500 Subject: [PATCH] Cast uses of Layer and ViewportIndex to the expected type. --- .../shaders-msl/vert/layer.msl11.invalid.vert | 2 +- .../vert/viewport-index.msl2.invalid.vert | 2 +- .../shaders-msl/vert/layer.msl11.invalid.vert | 2 +- .../vert/viewport-index.msl2.invalid.vert | 2 +- spirv_msl.cpp | 28 +++++++++++++++++-- 5 files changed, 30 insertions(+), 6 deletions(-) diff --git a/reference/opt/shaders-msl/vert/layer.msl11.invalid.vert b/reference/opt/shaders-msl/vert/layer.msl11.invalid.vert index 1fa64660..b6f39dca 100644 --- a/reference/opt/shaders-msl/vert/layer.msl11.invalid.vert +++ b/reference/opt/shaders-msl/vert/layer.msl11.invalid.vert @@ -18,7 +18,7 @@ vertex main0_out main0(main0_in in [[stage_in]]) { main0_out out = {}; out.gl_Position = in.coord; - out.gl_Layer = int(in.coord.z); + out.gl_Layer = uint(int(in.coord.z)); return out; } diff --git a/reference/opt/shaders-msl/vert/viewport-index.msl2.invalid.vert b/reference/opt/shaders-msl/vert/viewport-index.msl2.invalid.vert index 89d3bb6c..e5316c07 100644 --- a/reference/opt/shaders-msl/vert/viewport-index.msl2.invalid.vert +++ b/reference/opt/shaders-msl/vert/viewport-index.msl2.invalid.vert @@ -18,7 +18,7 @@ vertex main0_out main0(main0_in in [[stage_in]]) { main0_out out = {}; out.gl_Position = in.coord; - out.gl_ViewportIndex = int(in.coord.z); + out.gl_ViewportIndex = uint(int(in.coord.z)); return out; } diff --git a/reference/shaders-msl/vert/layer.msl11.invalid.vert b/reference/shaders-msl/vert/layer.msl11.invalid.vert index 1fa64660..b6f39dca 100644 --- a/reference/shaders-msl/vert/layer.msl11.invalid.vert +++ b/reference/shaders-msl/vert/layer.msl11.invalid.vert @@ -18,7 +18,7 @@ vertex main0_out main0(main0_in in [[stage_in]]) { main0_out out = {}; out.gl_Position = in.coord; - out.gl_Layer = int(in.coord.z); + out.gl_Layer = uint(int(in.coord.z)); return out; } diff --git a/reference/shaders-msl/vert/viewport-index.msl2.invalid.vert b/reference/shaders-msl/vert/viewport-index.msl2.invalid.vert index 89d3bb6c..e5316c07 100644 --- a/reference/shaders-msl/vert/viewport-index.msl2.invalid.vert +++ b/reference/shaders-msl/vert/viewport-index.msl2.invalid.vert @@ -18,7 +18,7 @@ vertex main0_out main0(main0_in in [[stage_in]]) { main0_out out = {}; out.gl_Position = in.coord; - out.gl_ViewportIndex = int(in.coord.z); + out.gl_ViewportIndex = uint(int(in.coord.z)); return out; } diff --git a/spirv_msl.cpp b/spirv_msl.cpp index b8d44956..49db9518 100644 --- a/spirv_msl.cpp +++ b/spirv_msl.cpp @@ -4714,6 +4714,8 @@ void CompilerMSL::bitcast_from_builtin_load(uint32_t source_id, std::string &exp case BuiltInLocalInvocationIndex: case BuiltInWorkgroupSize: case BuiltInNumWorkgroups: + case BuiltInLayer: + case BuiltInViewportIndex: expected_type = SPIRType::UInt; break; @@ -4725,9 +4727,31 @@ void CompilerMSL::bitcast_from_builtin_load(uint32_t source_id, std::string &exp expr = bitcast_expression(expr_type, expected_type, expr); } -// MSL always declares output builtins with the SPIR-V type. -void CompilerMSL::bitcast_to_builtin_store(uint32_t, std::string &, const SPIRType &) +void CompilerMSL::bitcast_to_builtin_store(uint32_t target_id, std::string &expr, const SPIRType &expr_type) { + // Only interested in standalone builtin variables. + if (!has_decoration(target_id, DecorationBuiltIn)) + return; + + auto builtin = static_cast(get_decoration(target_id, DecorationBuiltIn)); + auto expected_type = expr_type.basetype; + switch (builtin) + { + case BuiltInLayer: + case BuiltInViewportIndex: + expected_type = SPIRType::UInt; + break; + + default: + break; + } + + if (expected_type != expr_type.basetype) + { + auto type = expr_type; + type.basetype = expected_type; + expr = bitcast_expression(type, expr_type.basetype, expr); + } } std::string CompilerMSL::to_initializer_expression(const SPIRVariable &var)