Merge pull request #398 from KhronosGroup/fix-396

Do not use inline for-loop initializers with different types.
This commit is contained in:
Hans-Kristian Arntzen 2018-01-17 10:10:01 +01:00 коммит произвёл GitHub
Родитель 34cbe91d12 b902d5400c
Коммит 377c6db480
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
5 изменённых файлов: 184 добавлений и 0 удалений

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

@ -0,0 +1,23 @@
#version 310 es
precision mediump float;
precision highp int;
layout(location = 0) out vec4 FragColor;
layout(location = 0) flat in mediump int counter;
void main()
{
int _37;
mediump int _22;
uint _38;
mediump uint _25;
FragColor = vec4(0.0);
int _53 = 0;
uint _54 = 1u;
for (; (_53 < 10) && (int(_54) < int(20u)); _22 = _53 + counter, _25 = _54 + uint(counter), _53 = _22, _54 = _25)
{
FragColor += vec4(float(_53));
FragColor += vec4(float(_54));
}
}

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

@ -0,0 +1,19 @@
#version 310 es
precision mediump float;
precision highp int;
layout(location = 0) out vec4 FragColor;
layout(location = 0) flat in mediump int counter;
void main()
{
FragColor = vec4(0.0);
mediump int i = 0;
mediump uint j = 1u;
for (; (i < 10) && (int(j) < int(20u)); i += counter, j += uint(counter))
{
FragColor += vec4(float(i));
FragColor += vec4(float(j));
}
}

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

@ -0,0 +1,111 @@
; SPIR-V
; Version: 1.0
; Generator: Khronos Glslang Reference Front End; 3
; Bound: 52
; Schema: 0
OpCapability Shader
%1 = OpExtInstImport "GLSL.std.450"
OpMemoryModel Logical GLSL450
OpEntryPoint Fragment %main "main" %FragColor %counter %ucounter
OpExecutionMode %main OriginUpperLeft
OpSource ESSL 310
OpName %main "main"
OpName %FragColor "FragColor"
OpName %i "i"
OpName %j "j"
OpName %counter "counter"
OpName %ucounter "ucounter"
OpDecorate %FragColor RelaxedPrecision
OpDecorate %FragColor Location 0
OpDecorate %i RelaxedPrecision
OpDecorate %j RelaxedPrecision
OpDecorate %23 RelaxedPrecision
OpDecorate %27 RelaxedPrecision
OpDecorate %31 RelaxedPrecision
OpDecorate %32 RelaxedPrecision
OpDecorate %33 RelaxedPrecision
OpDecorate %34 RelaxedPrecision
OpDecorate %35 RelaxedPrecision
OpDecorate %36 RelaxedPrecision
OpDecorate %37 RelaxedPrecision
OpDecorate %38 RelaxedPrecision
OpDecorate %39 RelaxedPrecision
OpDecorate %40 RelaxedPrecision
OpDecorate %counter RelaxedPrecision
OpDecorate %counter Flat
OpDecorate %counter Location 0
OpDecorate %43 RelaxedPrecision
OpDecorate %44 RelaxedPrecision
OpDecorate %45 RelaxedPrecision
OpDecorate %46 RelaxedPrecision
OpDecorate %47 RelaxedPrecision
OpDecorate %48 RelaxedPrecision
OpDecorate %ucounter RelaxedPrecision
OpDecorate %ucounter Flat
OpDecorate %ucounter Location 1
%void = OpTypeVoid
%3 = OpTypeFunction %void
%float = OpTypeFloat 32
%v4float = OpTypeVector %float 4
%_ptr_Output_v4float = OpTypePointer Output %v4float
%FragColor = OpVariable %_ptr_Output_v4float Output
%float_0 = OpConstant %float 0
%11 = OpConstantComposite %v4float %float_0 %float_0 %float_0 %float_0
%int = OpTypeInt 32 1
%_ptr_Function_int = OpTypePointer Function %int
%uint = OpTypeInt 32 0
%_ptr_Function_uint = OpTypePointer Function %uint
%int_0 = OpConstant %int 0
%int_1 = OpConstant %uint 1
%int_10 = OpConstant %int 10
%bool = OpTypeBool
%int_20 = OpConstant %uint 20
%_ptr_Input_int = OpTypePointer Input %int
%counter = OpVariable %_ptr_Input_int Input
%_ptr_Input_uint = OpTypePointer Input %uint
%ucounter = OpVariable %_ptr_Input_uint Input
%main = OpFunction %void None %3
%5 = OpLabel
%i = OpVariable %_ptr_Function_int Function
%j = OpVariable %_ptr_Function_uint Function
OpStore %FragColor %11
OpStore %i %int_0
OpStore %j %int_1
OpBranch %18
%18 = OpLabel
OpLoopMerge %20 %21 None
OpBranch %22
%22 = OpLabel
%23 = OpLoad %int %i
%26 = OpSLessThan %bool %23 %int_10
%27 = OpLoad %uint %j
%29 = OpSLessThan %bool %27 %int_20
%30 = OpLogicalAnd %bool %26 %29
OpBranchConditional %30 %19 %20
%19 = OpLabel
%31 = OpLoad %int %i
%32 = OpConvertSToF %float %31
%33 = OpCompositeConstruct %v4float %32 %32 %32 %32
%34 = OpLoad %v4float %FragColor
%35 = OpFAdd %v4float %34 %33
OpStore %FragColor %35
%36 = OpLoad %uint %j
%37 = OpConvertUToF %float %36
%38 = OpCompositeConstruct %v4float %37 %37 %37 %37
%39 = OpLoad %v4float %FragColor
%40 = OpFAdd %v4float %39 %38
OpStore %FragColor %40
OpBranch %21
%21 = OpLabel
%43 = OpLoad %int %counter
%44 = OpLoad %int %i
%45 = OpIAdd %int %44 %43
OpStore %i %45
%46 = OpLoad %int %counter
%47 = OpLoad %uint %j
%48 = OpIAdd %uint %47 %46
OpStore %j %48
OpBranch %18
%20 = OpLabel
OpReturn
OpFunctionEnd

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

@ -8027,10 +8027,20 @@ string CompilerGLSL::emit_for_loop_initializers(const SPIRBlock &block)
if (block.loop_variables.empty())
return "";
bool same_types = for_loop_initializers_are_same_type(block);
// We can only declare for loop initializers if all variables are of same type.
// If we cannot do this, declare individual variables before the loop header.
if (block.loop_variables.size() == 1)
{
return variable_decl(get<SPIRVariable>(block.loop_variables.front()));
}
else if (!same_types)
{
for (auto &loop_var : block.loop_variables)
statement(variable_decl(get<SPIRVariable>(loop_var)), ";");
return "";
}
else
{
auto &var = get<SPIRVariable>(block.loop_variables.front());
@ -8052,6 +8062,26 @@ string CompilerGLSL::emit_for_loop_initializers(const SPIRBlock &block)
}
}
bool CompilerGLSL::for_loop_initializers_are_same_type(const SPIRBlock &block)
{
if (block.loop_variables.size() <= 1)
return true;
uint32_t expected = get<SPIRVariable>(block.loop_variables[0]).basetype;
uint64_t expected_flags = get_decoration_mask(block.loop_variables[0]);
for (auto &var : block.loop_variables)
{
if (expected != get<SPIRVariable>(var).basetype)
return false;
// Precision flags and things like that must also match.
if (expected_flags != get_decoration_mask(var))
return false;
}
return true;
}
bool CompilerGLSL::attempt_emit_loop_header(SPIRBlock &block, SPIRBlock::Method method)
{
SPIRBlock::ContinueBlockType continue_type = continue_block_type(get<SPIRBlock>(block.continue_block));

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

@ -496,6 +496,7 @@ protected:
void find_static_extensions();
std::string emit_for_loop_initializers(const SPIRBlock &block);
bool for_loop_initializers_are_same_type(const SPIRBlock &block);
bool optimize_read_modify_write(const std::string &lhs, const std::string &rhs);
void fixup_image_load_store_access();