Do not merge swizzles for anything other than vectors.

This commit is contained in:
Hans-Kristian Arntzen 2018-01-22 09:52:57 +01:00
Родитель 921c555cf3
Коммит d0ce948df4
5 изменённых файлов: 98 добавлений и 6 удалений

Просмотреть файл

@ -0,0 +1,18 @@
#version 310 es
precision mediump float;
precision highp int;
struct SwizzleTest
{
float a;
float b;
};
layout(location = 0) in vec2 foo;
layout(location = 0) out float FooOut;
void main()
{
FooOut = foo.x + foo.y;
}

Просмотреть файл

@ -0,0 +1,19 @@
#version 310 es
precision mediump float;
precision highp int;
struct SwizzleTest
{
float a;
float b;
};
layout(location = 0) in vec2 foo;
layout(location = 0) out float FooOut;
void main()
{
SwizzleTest _22 = SwizzleTest(foo.x, foo.y);
FooOut = _22.a + _22.b;
}

Просмотреть файл

@ -0,0 +1,51 @@
; SPIR-V
; Version: 1.0
; Generator: Khronos Glslang Reference Front End; 3
; Bound: 39
; Schema: 0
OpCapability Shader
%1 = OpExtInstImport "GLSL.std.450"
OpMemoryModel Logical GLSL450
OpEntryPoint Fragment %main "main" %foo %FooOut
OpExecutionMode %main OriginUpperLeft
OpSource ESSL 310
OpName %main "main"
OpName %foo "foo"
OpName %SwizzleTest "SwizzleTest"
OpMemberName %SwizzleTest 0 "a"
OpMemberName %SwizzleTest 1 "b"
OpName %FooOut "FooOut"
OpDecorate %foo RelaxedPrecision
OpDecorate %foo Location 0
OpDecorate %12 RelaxedPrecision
OpMemberDecorate %SwizzleTest 0 RelaxedPrecision
OpMemberDecorate %SwizzleTest 1 RelaxedPrecision
OpDecorate %FooOut RelaxedPrecision
OpDecorate %FooOut Location 0
OpDecorate %34 RelaxedPrecision
%void = OpTypeVoid
%3 = OpTypeFunction %void
%float = OpTypeFloat 32
%v2float = OpTypeVector %float 2
%_ptr_Function_v2float = OpTypePointer Function %v2float
%_ptr_Input_v2float = OpTypePointer Input %v2float
%foo = OpVariable %_ptr_Input_v2float Input
%SwizzleTest = OpTypeStruct %float %float
%_ptr_Function_SwizzleTest = OpTypePointer Function %SwizzleTest
%uint = OpTypeInt 32 0
%_ptr_Function_float = OpTypePointer Function %float
%_ptr_Output_float = OpTypePointer Output %float
%FooOut = OpVariable %_ptr_Output_float Output
%int = OpTypeInt 32 1
%main = OpFunction %void None %3
%5 = OpLabel
%12 = OpLoad %v2float %foo
%36 = OpCompositeExtract %float %12 0
%38 = OpCompositeExtract %float %12 1
%test0 = OpCompositeConstruct %SwizzleTest %36 %38
%new0 = OpCompositeExtract %float %test0 0
%new1 = OpCompositeExtract %float %test0 1
%34 = OpFAdd %float %new0 %new1
OpStore %FooOut %34
OpReturn
OpFunctionEnd

Просмотреть файл

@ -5071,20 +5071,24 @@ bool CompilerGLSL::remove_unity_swizzle(uint32_t base, string &op)
return true;
}
string CompilerGLSL::build_composite_combiner(const uint32_t *elems, uint32_t length)
string CompilerGLSL::build_composite_combiner(uint32_t return_type, const uint32_t *elems, uint32_t length)
{
uint32_t base = 0;
bool swizzle_optimization = false;
string op;
string subop;
// Can only merge swizzles for vectors.
auto &type = get<SPIRType>(return_type);
bool can_apply_swizzle_opt = type.basetype != SPIRType::Struct && type.array.empty() && type.columns == 1;
bool swizzle_optimization = false;
for (uint32_t i = 0; i < length; i++)
{
auto *e = maybe_get<SPIRExpression>(elems[i]);
// If we're merging another scalar which belongs to the same base
// object, just merge the swizzles to avoid triggering more than 1 expression read as much as possible!
if (e && e->base_expression && e->base_expression == base)
if (can_apply_swizzle_opt && e && e->base_expression && e->base_expression == base)
{
// Only supposed to be used for vector swizzle -> scalar.
assert(!e->expression.empty() && e->expression.front() == '.');
@ -5491,7 +5495,7 @@ void CompilerGLSL::emit_instruction(const Instruction &instruction)
if (splat)
constructor_op += to_expression(elems[0]);
else
constructor_op += build_composite_combiner(elems, length);
constructor_op += build_composite_combiner(result_type, elems, length);
constructor_op += " }";
}
else if (swizzle_splat && !composite)
@ -5504,7 +5508,7 @@ void CompilerGLSL::emit_instruction(const Instruction &instruction)
if (splat)
constructor_op += to_expression(elems[0]);
else
constructor_op += build_composite_combiner(elems, length);
constructor_op += build_composite_combiner(result_type, elems, length);
constructor_op += ")";
}

Просмотреть файл

@ -426,7 +426,7 @@ protected:
std::string bitcast_expression(SPIRType::BaseType target_type, uint32_t arg);
std::string bitcast_expression(const SPIRType &target_type, SPIRType::BaseType expr_type, const std::string &expr);
std::string build_composite_combiner(const uint32_t *elems, uint32_t length);
std::string build_composite_combiner(uint32_t result_type, const uint32_t *elems, uint32_t length);
bool remove_duplicate_swizzle(std::string &op);
bool remove_unity_swizzle(uint32_t base, std::string &op);