Merge pull request #624 from KhronosGroup/fix-619
Support branch/loop hints in HLSL.
This commit is contained in:
commit
994f789465
@ -0,0 +1,32 @@
|
||||
RWByteAddressBuffer bar : register(u0);
|
||||
RWByteAddressBuffer foo : register(u1);
|
||||
|
||||
void comp_main()
|
||||
{
|
||||
[unroll]
|
||||
for (int _135 = 0; _135 < 16; )
|
||||
{
|
||||
bar.Store4(_135 * 16 + 0, asuint(asfloat(foo.Load4(_135 * 16 + 0))));
|
||||
_135++;
|
||||
continue;
|
||||
}
|
||||
[loop]
|
||||
for (int _136 = 0; _136 < 16; )
|
||||
{
|
||||
bar.Store4((15 - _136) * 16 + 0, asuint(asfloat(foo.Load4(_136 * 16 + 0))));
|
||||
_136++;
|
||||
continue;
|
||||
}
|
||||
[branch]
|
||||
if (asfloat(bar.Load(160)) > 10.0f)
|
||||
{
|
||||
foo.Store4(320, asuint(5.0f.xxxx));
|
||||
}
|
||||
foo.Store4(320, asuint(20.0f.xxxx));
|
||||
}
|
||||
|
||||
[numthreads(1, 1, 1)]
|
||||
void main()
|
||||
{
|
||||
comp_main();
|
||||
}
|
41
reference/shaders-hlsl/asm/comp/control-flow-hints.asm.comp
Normal file
41
reference/shaders-hlsl/asm/comp/control-flow-hints.asm.comp
Normal file
@ -0,0 +1,41 @@
|
||||
RWByteAddressBuffer bar : register(u0);
|
||||
RWByteAddressBuffer foo : register(u1);
|
||||
|
||||
void _main()
|
||||
{
|
||||
[unroll]
|
||||
for (int i = 0; i < 16; i++)
|
||||
{
|
||||
bar.Store4(i * 16 + 0, asuint(asfloat(foo.Load4(i * 16 + 0))));
|
||||
}
|
||||
[loop]
|
||||
for (int i_1 = 0; i_1 < 16; i_1++)
|
||||
{
|
||||
bar.Store4((15 - i_1) * 16 + 0, asuint(asfloat(foo.Load4(i_1 * 16 + 0))));
|
||||
}
|
||||
float v = asfloat(bar.Load(160));
|
||||
float w = asfloat(foo.Load(160));
|
||||
[branch]
|
||||
if (v > 10.0f)
|
||||
{
|
||||
foo.Store4(320, asuint(5.0f.xxxx));
|
||||
}
|
||||
float value = 20.0f;
|
||||
[flatten]
|
||||
if (w > 40.0f)
|
||||
{
|
||||
value = 20.0f;
|
||||
}
|
||||
foo.Store4(320, asuint(value.xxxx));
|
||||
}
|
||||
|
||||
void comp_main()
|
||||
{
|
||||
_main();
|
||||
}
|
||||
|
||||
[numthreads(1, 1, 1)]
|
||||
void main()
|
||||
{
|
||||
comp_main();
|
||||
}
|
146
shaders-hlsl/asm/comp/control-flow-hints.asm.comp
Normal file
146
shaders-hlsl/asm/comp/control-flow-hints.asm.comp
Normal file
@ -0,0 +1,146 @@
|
||||
; SPIR-V
|
||||
; Version: 1.0
|
||||
; Generator: Khronos Glslang Reference Front End; 6
|
||||
; Bound: 85
|
||||
; Schema: 0
|
||||
OpCapability Shader
|
||||
%1 = OpExtInstImport "GLSL.std.450"
|
||||
OpMemoryModel Logical GLSL450
|
||||
OpEntryPoint GLCompute %main "main"
|
||||
OpExecutionMode %main LocalSize 1 1 1
|
||||
OpSource HLSL 500
|
||||
OpName %main "main"
|
||||
OpName %_main_ "@main("
|
||||
OpName %i "i"
|
||||
OpName %bar "bar"
|
||||
OpMemberName %bar 0 "@data"
|
||||
OpName %bar_0 "bar"
|
||||
OpName %foo "foo"
|
||||
OpName %i_0 "i"
|
||||
OpName %v "v"
|
||||
OpName %w "w"
|
||||
OpName %value "value"
|
||||
OpDecorate %_runtimearr_v4float ArrayStride 16
|
||||
OpMemberDecorate %bar 0 Offset 0
|
||||
OpDecorate %bar BufferBlock
|
||||
OpDecorate %bar_0 DescriptorSet 0
|
||||
OpDecorate %bar_0 Binding 0
|
||||
OpDecorate %foo DescriptorSet 0
|
||||
OpDecorate %foo Binding 1
|
||||
%void = OpTypeVoid
|
||||
%3 = OpTypeFunction %void
|
||||
%int = OpTypeInt 32 1
|
||||
%_ptr_Function_int = OpTypePointer Function %int
|
||||
%int_0 = OpConstant %int 0
|
||||
%int_16 = OpConstant %int 16
|
||||
%bool = OpTypeBool
|
||||
%float = OpTypeFloat 32
|
||||
%v4float = OpTypeVector %float 4
|
||||
%_runtimearr_v4float = OpTypeRuntimeArray %v4float
|
||||
%bar = OpTypeStruct %_runtimearr_v4float
|
||||
%_ptr_Uniform_bar = OpTypePointer Uniform %bar
|
||||
%bar_0 = OpVariable %_ptr_Uniform_bar Uniform
|
||||
%foo = OpVariable %_ptr_Uniform_bar Uniform
|
||||
%_ptr_Uniform_v4float = OpTypePointer Uniform %v4float
|
||||
%int_1 = OpConstant %int 1
|
||||
%int_15 = OpConstant %int 15
|
||||
%_ptr_Function_float = OpTypePointer Function %float
|
||||
%int_10 = OpConstant %int 10
|
||||
%uint = OpTypeInt 32 0
|
||||
%uint_0 = OpConstant %uint 0
|
||||
%_ptr_Uniform_float = OpTypePointer Uniform %float
|
||||
%float_10 = OpConstant %float 10
|
||||
%int_20 = OpConstant %int 20
|
||||
%float_5 = OpConstant %float 5
|
||||
%72 = OpConstantComposite %v4float %float_5 %float_5 %float_5 %float_5
|
||||
%float_20 = OpConstant %float 20
|
||||
%float_40 = OpConstant %float 40
|
||||
%main = OpFunction %void None %3
|
||||
%5 = OpLabel
|
||||
%84 = OpFunctionCall %void %_main_
|
||||
OpReturn
|
||||
OpFunctionEnd
|
||||
%_main_ = OpFunction %void None %3
|
||||
%7 = OpLabel
|
||||
%i = OpVariable %_ptr_Function_int Function
|
||||
%i_0 = OpVariable %_ptr_Function_int Function
|
||||
%v = OpVariable %_ptr_Function_float Function
|
||||
%w = OpVariable %_ptr_Function_float Function
|
||||
%value = OpVariable %_ptr_Function_float Function
|
||||
OpStore %i %int_0
|
||||
OpBranch %12
|
||||
%12 = OpLabel
|
||||
OpLoopMerge %14 %15 Unroll
|
||||
OpBranch %16
|
||||
%16 = OpLabel
|
||||
%17 = OpLoad %int %i
|
||||
%20 = OpSLessThan %bool %17 %int_16
|
||||
OpBranchConditional %20 %13 %14
|
||||
%13 = OpLabel
|
||||
%27 = OpLoad %int %i
|
||||
%29 = OpLoad %int %i
|
||||
%31 = OpAccessChain %_ptr_Uniform_v4float %foo %int_0 %29
|
||||
%32 = OpLoad %v4float %31
|
||||
%33 = OpAccessChain %_ptr_Uniform_v4float %bar_0 %int_0 %27
|
||||
OpStore %33 %32
|
||||
OpBranch %15
|
||||
%15 = OpLabel
|
||||
%34 = OpLoad %int %i
|
||||
%36 = OpIAdd %int %34 %int_1
|
||||
OpStore %i %36
|
||||
OpBranch %12
|
||||
%14 = OpLabel
|
||||
OpStore %i_0 %int_0
|
||||
OpBranch %38
|
||||
%38 = OpLabel
|
||||
OpLoopMerge %40 %41 DontUnroll
|
||||
OpBranch %42
|
||||
%42 = OpLabel
|
||||
%43 = OpLoad %int %i_0
|
||||
%44 = OpSLessThan %bool %43 %int_16
|
||||
OpBranchConditional %44 %39 %40
|
||||
%39 = OpLabel
|
||||
%46 = OpLoad %int %i_0
|
||||
%47 = OpISub %int %int_15 %46
|
||||
%48 = OpLoad %int %i_0
|
||||
%49 = OpAccessChain %_ptr_Uniform_v4float %foo %int_0 %48
|
||||
%50 = OpLoad %v4float %49
|
||||
%51 = OpAccessChain %_ptr_Uniform_v4float %bar_0 %int_0 %47
|
||||
OpStore %51 %50
|
||||
OpBranch %41
|
||||
%41 = OpLabel
|
||||
%52 = OpLoad %int %i_0
|
||||
%53 = OpIAdd %int %52 %int_1
|
||||
OpStore %i_0 %53
|
||||
OpBranch %38
|
||||
%40 = OpLabel
|
||||
%60 = OpAccessChain %_ptr_Uniform_float %bar_0 %int_0 %int_10 %uint_0
|
||||
%61 = OpLoad %float %60
|
||||
OpStore %v %61
|
||||
%63 = OpAccessChain %_ptr_Uniform_float %foo %int_0 %int_10 %uint_0
|
||||
%64 = OpLoad %float %63
|
||||
OpStore %w %64
|
||||
%65 = OpLoad %float %v
|
||||
%67 = OpFOrdGreaterThan %bool %65 %float_10
|
||||
OpSelectionMerge %69 DontFlatten
|
||||
OpBranchConditional %67 %68 %69
|
||||
%68 = OpLabel
|
||||
%73 = OpAccessChain %_ptr_Uniform_v4float %foo %int_0 %int_20
|
||||
OpStore %73 %72
|
||||
OpBranch %69
|
||||
%69 = OpLabel
|
||||
OpStore %value %float_20
|
||||
%76 = OpLoad %float %w
|
||||
%78 = OpFOrdGreaterThan %bool %76 %float_40
|
||||
OpSelectionMerge %80 Flatten
|
||||
OpBranchConditional %78 %79 %80
|
||||
%79 = OpLabel
|
||||
OpStore %value %float_20
|
||||
OpBranch %80
|
||||
%80 = OpLabel
|
||||
%81 = OpLoad %float %value
|
||||
%82 = OpCompositeConstruct %v4float %81 %81 %81 %81
|
||||
%83 = OpAccessChain %_ptr_Uniform_v4float %foo %int_0 %int_20
|
||||
OpStore %83 %82
|
||||
OpReturn
|
||||
OpFunctionEnd
|
@ -578,6 +578,15 @@ struct SPIRBlock : IVariant
|
||||
MergeSelection
|
||||
};
|
||||
|
||||
enum Hints
|
||||
{
|
||||
HintNone,
|
||||
HintUnroll,
|
||||
HintDontUnroll,
|
||||
HintFlatten,
|
||||
HintDontFlatten
|
||||
};
|
||||
|
||||
enum Method
|
||||
{
|
||||
MergeToSelectForLoop,
|
||||
@ -610,6 +619,7 @@ struct SPIRBlock : IVariant
|
||||
|
||||
Terminator terminator = Unknown;
|
||||
Merge merge = MergeNone;
|
||||
Hints hint = HintNone;
|
||||
uint32_t next_block = 0;
|
||||
uint32_t merge_block = 0;
|
||||
uint32_t continue_block = 0;
|
||||
|
@ -2221,6 +2221,14 @@ void Compiler::parse(const Instruction &instruction)
|
||||
current_block->next_block = ops[0];
|
||||
current_block->merge = SPIRBlock::MergeSelection;
|
||||
selection_merge_targets.insert(current_block->next_block);
|
||||
|
||||
if (length >= 2)
|
||||
{
|
||||
if (ops[1] & SelectionControlFlattenMask)
|
||||
current_block->hint = SPIRBlock::HintFlatten;
|
||||
else if (ops[1] & SelectionControlDontFlattenMask)
|
||||
current_block->hint = SPIRBlock::HintDontFlatten;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
@ -2243,6 +2251,14 @@ void Compiler::parse(const Instruction &instruction)
|
||||
// they are treated as continues.
|
||||
if (current_block->continue_block != current_block->self)
|
||||
continue_blocks.insert(current_block->continue_block);
|
||||
|
||||
if (length >= 3)
|
||||
{
|
||||
if (ops[2] & LoopControlUnrollMask)
|
||||
current_block->hint = SPIRBlock::HintUnroll;
|
||||
else if (ops[2] & LoopControlDontUnrollMask)
|
||||
current_block->hint = SPIRBlock::HintDontUnroll;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
|
@ -9101,6 +9101,7 @@ void CompilerGLSL::branch(uint32_t from, uint32_t cond, uint32_t true_block, uin
|
||||
|
||||
if (true_sub)
|
||||
{
|
||||
emit_block_hints(get<SPIRBlock>(from));
|
||||
statement("if (", to_expression(cond), ")");
|
||||
begin_scope();
|
||||
branch(from, true_block);
|
||||
@ -9124,6 +9125,7 @@ void CompilerGLSL::branch(uint32_t from, uint32_t cond, uint32_t true_block, uin
|
||||
else if (false_sub && !true_sub)
|
||||
{
|
||||
// Only need false path, use negative conditional.
|
||||
emit_block_hints(get<SPIRBlock>(from));
|
||||
statement("if (!", to_enclosed_expression(cond), ")");
|
||||
begin_scope();
|
||||
branch(from, false_block);
|
||||
@ -9352,6 +9354,7 @@ bool CompilerGLSL::attempt_emit_loop_header(SPIRBlock &block, SPIRBlock::Method
|
||||
{
|
||||
// This block may be a dominating block, so make sure we flush undeclared variables before building the for loop header.
|
||||
flush_undeclared_variables(block);
|
||||
emit_block_hints(block);
|
||||
|
||||
// Important that we do this in this order because
|
||||
// emitting the continue block can invalidate the condition expression.
|
||||
@ -9370,6 +9373,7 @@ bool CompilerGLSL::attempt_emit_loop_header(SPIRBlock &block, SPIRBlock::Method
|
||||
case SPIRBlock::WhileLoop:
|
||||
// This block may be a dominating block, so make sure we flush undeclared variables before building the while loop header.
|
||||
flush_undeclared_variables(block);
|
||||
emit_block_hints(block);
|
||||
statement("while (", to_expression(block.condition), ")");
|
||||
break;
|
||||
|
||||
@ -9417,11 +9421,13 @@ bool CompilerGLSL::attempt_emit_loop_header(SPIRBlock &block, SPIRBlock::Method
|
||||
auto initializer = emit_for_loop_initializers(block);
|
||||
auto condition = to_expression(child.condition);
|
||||
auto continue_block = emit_continue_block(block.continue_block);
|
||||
emit_block_hints(block);
|
||||
statement("for (", initializer, "; ", condition, "; ", continue_block, ")");
|
||||
break;
|
||||
}
|
||||
|
||||
case SPIRBlock::WhileLoop:
|
||||
emit_block_hints(block);
|
||||
statement("while (", to_expression(child.condition), ")");
|
||||
break;
|
||||
|
||||
@ -9632,6 +9638,7 @@ void CompilerGLSL::emit_block_chain(SPIRBlock &block)
|
||||
auto &type = expression_type(block.condition);
|
||||
bool uint32_t_case = type.basetype == SPIRType::UInt;
|
||||
|
||||
emit_block_hints(block);
|
||||
statement("switch (", to_expression(block.condition), ")");
|
||||
begin_scope();
|
||||
|
||||
@ -9944,3 +9951,7 @@ void CompilerGLSL::bitcast_to_builtin_store(uint32_t target_id, std::string &exp
|
||||
expr = bitcast_expression(type, expr_type.basetype, expr);
|
||||
}
|
||||
}
|
||||
|
||||
void CompilerGLSL::emit_block_hints(const SPIRBlock &)
|
||||
{
|
||||
}
|
||||
|
@ -462,6 +462,7 @@ protected:
|
||||
std::string to_combined_image_sampler(uint32_t image_id, uint32_t samp_id);
|
||||
virtual bool skip_argument(uint32_t id) const;
|
||||
virtual void emit_array_copy(const std::string &lhs, uint32_t rhs_id);
|
||||
virtual void emit_block_hints(const SPIRBlock &block);
|
||||
|
||||
bool buffer_is_packing_standard(const SPIRType &type, BufferPackingStandard packing, uint32_t start_offset = 0,
|
||||
uint32_t end_offset = std::numeric_limits<uint32_t>::max());
|
||||
|
@ -4610,3 +4610,24 @@ string CompilerHLSL::compile()
|
||||
|
||||
return buffer->str();
|
||||
}
|
||||
|
||||
void CompilerHLSL::emit_block_hints(const SPIRBlock &block)
|
||||
{
|
||||
switch (block.hint)
|
||||
{
|
||||
case SPIRBlock::HintFlatten:
|
||||
statement("[flatten]");
|
||||
break;
|
||||
case SPIRBlock::HintDontFlatten:
|
||||
statement("[branch]");
|
||||
break;
|
||||
case SPIRBlock::HintUnroll:
|
||||
statement("[unroll]");
|
||||
break;
|
||||
case SPIRBlock::HintDontUnroll:
|
||||
statement("[loop]");
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -158,6 +158,7 @@ private:
|
||||
void emit_store(const Instruction &instruction);
|
||||
void emit_atomic(const uint32_t *ops, uint32_t length, spv::Op op);
|
||||
void emit_subgroup_op(const Instruction &i) override;
|
||||
void emit_block_hints(const SPIRBlock &block) override;
|
||||
|
||||
void emit_struct_member(const SPIRType &type, uint32_t member_type_id, uint32_t index, const std::string &qualifier,
|
||||
uint32_t base_offset = 0) override;
|
||||
|
Loading…
Reference in New Issue
Block a user