From b46910e9f5bf91a0722ea601ea4ffba5d5d53f1f Mon Sep 17 00:00:00 2001 From: Hans-Kristian Arntzen Date: Tue, 13 Mar 2018 12:13:01 +0100 Subject: [PATCH] Access chain into packed vectors as arrays. Cleaner and should be more compatible with storing to packed vector elements. Fix CompositeExtract bug with packed vectors on MSL. --- .../shaders-msl/flatten/swizzle.flatten.vert | 2 +- .../opt/shaders-msl/frag/packing-test-3.frag | 2 +- .../shaders-msl/flatten/swizzle.flatten.vert | 2 +- spirv_glsl.cpp | 22 +++++++++++-------- 4 files changed, 16 insertions(+), 12 deletions(-) diff --git a/reference/opt/shaders-msl/flatten/swizzle.flatten.vert b/reference/opt/shaders-msl/flatten/swizzle.flatten.vert index d6915065..52940411 100644 --- a/reference/opt/shaders-msl/flatten/swizzle.flatten.vert +++ b/reference/opt/shaders-msl/flatten/swizzle.flatten.vert @@ -39,7 +39,7 @@ vertex main0_out main0(constant UBO& _22 [[buffer(0)]]) out.oA = _22.A; out.oB = float4(_22.B0, _22.B1); out.oC = float4(_22.C0, _22.C1) + float4(_22.C1.xy, _22.C1.z, _22.C0); - out.oD = float4(_22.D0, _22.D1) + float4(_22.D0.xy, float3(_22.D0).z, _22.D1); + out.oD = float4(_22.D0[0], _22.D0[1], _22.D0[2], _22.D1) + float4(_22.D0[0], _22.D0[1], _22.D0[2u], _22.D1); out.oE = float4(_22.E0, _22.E1, _22.E2, _22.E3); out.oF = float4(_22.F0, _22.F1, _22.F2); return out; diff --git a/reference/opt/shaders-msl/frag/packing-test-3.frag b/reference/opt/shaders-msl/frag/packing-test-3.frag index 6ed07cde..436829e8 100644 --- a/reference/opt/shaders-msl/frag/packing-test-3.frag +++ b/reference/opt/shaders-msl/frag/packing-test-3.frag @@ -22,7 +22,7 @@ struct main0_out fragment main0_out main0(constant CB0& _26 [[buffer(0)]], float4 gl_FragCoord [[position]]) { main0_out out = {}; - out._entryPointOutput = float4(_26.CB0[1].position, _26.CB0[1].radius); + out._entryPointOutput = float4(_26.CB0[1].position[0], _26.CB0[1].position[1], _26.CB0[1].position[2], _26.CB0[1].radius); return out; } diff --git a/reference/shaders-msl/flatten/swizzle.flatten.vert b/reference/shaders-msl/flatten/swizzle.flatten.vert index 53fc21f9..046d6e85 100644 --- a/reference/shaders-msl/flatten/swizzle.flatten.vert +++ b/reference/shaders-msl/flatten/swizzle.flatten.vert @@ -39,7 +39,7 @@ vertex main0_out main0(constant UBO& _22 [[buffer(0)]]) out.oA = _22.A; out.oB = float4(_22.B0, _22.B1); out.oC = float4(_22.C0, _22.C1) + float4(_22.C1.xy, _22.C1.z, _22.C0); - out.oD = float4(_22.D0, _22.D1) + float4(float3(_22.D0).xy, float3(_22.D0).z, _22.D1); + out.oD = float4(_22.D0[0], _22.D0[1], _22.D0[2], _22.D1) + float4(float3(_22.D0).xy, _22.D0[2u], _22.D1); out.oE = float4(_22.E0, _22.E1, _22.E2, _22.E3); out.oF = float4(_22.F0, _22.F1, _22.F2); return out; diff --git a/spirv_glsl.cpp b/spirv_glsl.cpp index 48164a34..fb078c7f 100644 --- a/spirv_glsl.cpp +++ b/spirv_glsl.cpp @@ -4667,7 +4667,7 @@ string CompilerGLSL::access_chain_internal(uint32_t base, const uint32_t *indice bool access_chain_is_arrayed = false; bool row_major_matrix_needs_conversion = is_non_native_row_major_matrix(base); - bool is_packed = false; + bool is_packed = has_decoration(base, DecorationCPacked); bool pending_array_enclose = false; bool dimension_flatten = false; @@ -4826,23 +4826,22 @@ string CompilerGLSL::access_chain_internal(uint32_t base, const uint32_t *indice // Vector -> Scalar else if (type->vecsize > 1) { - if (is_packed) - { - expr = unpack_expression_type(expr, *type); - is_packed = false; - } - - if (index_is_literal) + if (index_is_literal && !is_packed) { expr += "."; expr += index_to_swizzle(index); } - else if (ids[index].get_type() == TypeConstant) + else if (ids[index].get_type() == TypeConstant && !is_packed) { auto &c = get(index); expr += "."; expr += index_to_swizzle(c.scalar()); } + else if (index_is_literal) + { + // For packed vectors, we can only access them as an array, not by swizzle. + expr += join("[", index, "]"); + } else { expr += "["; @@ -4850,6 +4849,7 @@ string CompilerGLSL::access_chain_internal(uint32_t base, const uint32_t *indice expr += "]"; } + is_packed = false; type_id = type->parent_type; type = &get(type_id); } @@ -5993,6 +5993,10 @@ void CompilerGLSL::emit_instruction(const Instruction &instruction) if (composite_type.basetype == SPIRType::Struct || !composite_type.array.empty()) allow_base_expression = false; + // Packed expressions cannot be split up. + if (has_decoration(ops[2], DecorationCPacked)) + allow_base_expression = false; + // Only apply this optimization if result is scalar. if (allow_base_expression && should_forward(ops[2]) && type.vecsize == 1 && type.columns == 1 && length == 1) {