diff options
author | Hans-Kristian Arntzen <post@arntzen-software.no> | 2019-03-21 13:25:41 +0300 |
---|---|---|
committer | Hans-Kristian Arntzen <post@arntzen-software.no> | 2019-03-21 13:25:41 +0300 |
commit | d2961b30db8539fbf78b32a48a2195e721101e06 (patch) | |
tree | d8d747ac050d543dcec6eb737e0f5242b502343a | |
parent | 1b00a6e48a4d9e1bd80bedc0e8243246f0a33a72 (diff) |
GLSL: Unroll loads from builtin pos/point arrays.
Odd-ball case for certain geometry shaders coming from HLSL.
-rw-r--r-- | reference/opt/shaders/asm/geom/unroll-glposition-load.asm.geom | 22 | ||||
-rw-r--r-- | reference/shaders/asm/geom/unroll-glposition-load.asm.geom | 35 | ||||
-rw-r--r-- | shaders/asm/geom/unroll-glposition-load.asm.geom | 102 | ||||
-rw-r--r-- | spirv_glsl.cpp | 40 | ||||
-rw-r--r-- | spirv_glsl.hpp | 1 |
5 files changed, 200 insertions, 0 deletions
diff --git a/reference/opt/shaders/asm/geom/unroll-glposition-load.asm.geom b/reference/opt/shaders/asm/geom/unroll-glposition-load.asm.geom new file mode 100644 index 00000000..d1f8963f --- /dev/null +++ b/reference/opt/shaders/asm/geom/unroll-glposition-load.asm.geom @@ -0,0 +1,22 @@ +#version 450 +layout(triangles) in; +layout(max_vertices = 3, triangle_strip) out; + +void main() +{ + vec4 _35_unrolled[3]; + for (int i = 0; i < int(3); i++) + { + _35_unrolled[i] = gl_in[i].gl_Position; + } + vec4 param[3] = _35_unrolled; + for (int _73 = 0; _73 < 3; ) + { + gl_Position = param[_73]; + EmitVertex(); + _73++; + continue; + } + EndPrimitive(); +} + diff --git a/reference/shaders/asm/geom/unroll-glposition-load.asm.geom b/reference/shaders/asm/geom/unroll-glposition-load.asm.geom new file mode 100644 index 00000000..de437fae --- /dev/null +++ b/reference/shaders/asm/geom/unroll-glposition-load.asm.geom @@ -0,0 +1,35 @@ +#version 450 +layout(triangles) in; +layout(max_vertices = 3, triangle_strip) out; + +struct SceneOut +{ + vec4 pos; +}; + +void _main(vec4 positions[3], SceneOut OUT) +{ + for (int i = 0; i < 3; i++) + { + SceneOut o; + o.pos = positions[i]; + gl_Position = o.pos; + EmitVertex(); + } + EndPrimitive(); +} + +void main() +{ + vec4 _35_unrolled[3]; + for (int i = 0; i < int(3); i++) + { + _35_unrolled[i] = gl_in[i].gl_Position; + } + vec4 positions[3] = _35_unrolled; + vec4 param[3] = positions; + SceneOut param_1; + _main(param, param_1); + SceneOut OUT = param_1; +} + diff --git a/shaders/asm/geom/unroll-glposition-load.asm.geom b/shaders/asm/geom/unroll-glposition-load.asm.geom new file mode 100644 index 00000000..8c10de39 --- /dev/null +++ b/shaders/asm/geom/unroll-glposition-load.asm.geom @@ -0,0 +1,102 @@ +; SPIR-V +; Version: 1.0 +; Generator: Khronos Glslang Reference Front End; 7 +; Bound: 55 +; Schema: 0 + OpCapability Geometry + %1 = OpExtInstImport "GLSL.std.450" + OpMemoryModel Logical GLSL450 + OpEntryPoint Geometry %main "main" %OUT_pos %positions_1 + OpExecutionMode %main Triangles + OpExecutionMode %main Invocations 1 + OpExecutionMode %main OutputTriangleStrip + OpExecutionMode %main OutputVertices 3 + OpSource HLSL 500 + OpName %main "main" + OpName %SceneOut "SceneOut" + OpMemberName %SceneOut 0 "pos" + OpName %_main_vf4_3__struct_SceneOut_vf41_ "@main(vf4[3];struct-SceneOut-vf41;" + OpName %positions "positions" + OpName %OUT "OUT" + OpName %i "i" + OpName %o "o" + OpName %OUT_pos "OUT.pos" + OpName %positions_0 "positions" + OpName %positions_1 "positions" + OpName %OUT_0 "OUT" + OpName %param "param" + OpName %param_0 "param" + OpDecorate %OUT_pos BuiltIn Position + OpDecorate %positions_1 BuiltIn Position + %void = OpTypeVoid + %3 = OpTypeFunction %void + %float = OpTypeFloat 32 + %v4float = OpTypeVector %float 4 + %uint = OpTypeInt 32 0 + %uint_3 = OpConstant %uint 3 +%_arr_v4float_uint_3 = OpTypeArray %v4float %uint_3 +%_ptr_Function__arr_v4float_uint_3 = OpTypePointer Function %_arr_v4float_uint_3 + %SceneOut = OpTypeStruct %v4float +%_ptr_Function_SceneOut = OpTypePointer Function %SceneOut + %14 = OpTypeFunction %void %_ptr_Function__arr_v4float_uint_3 %_ptr_Function_SceneOut + %int = OpTypeInt 32 1 +%_ptr_Function_int = OpTypePointer Function %int + %int_0 = OpConstant %int 0 + %int_3 = OpConstant %int 3 + %bool = OpTypeBool +%_ptr_Function_v4float = OpTypePointer Function %v4float +%_ptr_Output_v4float = OpTypePointer Output %v4float + %OUT_pos = OpVariable %_ptr_Output_v4float Output + %int_1 = OpConstant %int 1 +%_ptr_Input__arr_v4float_uint_3 = OpTypePointer Input %_arr_v4float_uint_3 +%positions_1 = OpVariable %_ptr_Input__arr_v4float_uint_3 Input + %main = OpFunction %void None %3 + %5 = OpLabel +%positions_0 = OpVariable %_ptr_Function__arr_v4float_uint_3 Function + %OUT_0 = OpVariable %_ptr_Function_SceneOut Function + %param = OpVariable %_ptr_Function__arr_v4float_uint_3 Function + %param_0 = OpVariable %_ptr_Function_SceneOut Function + %48 = OpLoad %_arr_v4float_uint_3 %positions_1 + OpStore %positions_0 %48 + %51 = OpLoad %_arr_v4float_uint_3 %positions_0 + OpStore %param %51 + %53 = OpFunctionCall %void %_main_vf4_3__struct_SceneOut_vf41_ %param %param_0 + %54 = OpLoad %SceneOut %param_0 + OpStore %OUT_0 %54 + OpReturn + OpFunctionEnd +%_main_vf4_3__struct_SceneOut_vf41_ = OpFunction %void None %14 + %positions = OpFunctionParameter %_ptr_Function__arr_v4float_uint_3 + %OUT = OpFunctionParameter %_ptr_Function_SceneOut + %18 = OpLabel + %i = OpVariable %_ptr_Function_int Function + %o = OpVariable %_ptr_Function_SceneOut Function + OpStore %i %int_0 + OpBranch %23 + %23 = OpLabel + OpLoopMerge %25 %26 None + OpBranch %27 + %27 = OpLabel + %28 = OpLoad %int %i + %31 = OpSLessThan %bool %28 %int_3 + OpBranchConditional %31 %24 %25 + %24 = OpLabel + %33 = OpLoad %int %i + %35 = OpAccessChain %_ptr_Function_v4float %positions %33 + %36 = OpLoad %v4float %35 + %37 = OpAccessChain %_ptr_Function_v4float %o %int_0 + OpStore %37 %36 + %40 = OpAccessChain %_ptr_Function_v4float %o %int_0 + %41 = OpLoad %v4float %40 + OpStore %OUT_pos %41 + OpEmitVertex + OpBranch %26 + %26 = OpLabel + %42 = OpLoad %int %i + %44 = OpIAdd %int %42 %int_1 + OpStore %i %44 + OpBranch %23 + %25 = OpLabel + OpEndPrimitive + OpReturn + OpFunctionEnd diff --git a/spirv_glsl.cpp b/spirv_glsl.cpp index e8260b1d..b5a32b5a 100644 --- a/spirv_glsl.cpp +++ b/spirv_glsl.cpp @@ -6909,6 +6909,10 @@ void CompilerGLSL::emit_instruction(const Instruction &instruction) // We might need to bitcast in order to load from a builtin. bitcast_from_builtin_load(ptr, expr, get<SPIRType>(result_type)); + // We might be trying to load a gl_Position[N], where we should be + // doing float4[](gl_in[i].gl_Position, ...) instead. + unroll_array_from_builtin_load(id, ptr, expr); + if (ptr_expression) ptr_expression->need_transpose = old_need_transpose; @@ -11041,6 +11045,42 @@ void CompilerGLSL::emit_array_copy(const string &lhs, uint32_t rhs_id) statement(lhs, " = ", to_expression(rhs_id), ";"); } +void CompilerGLSL::unroll_array_from_builtin_load(uint32_t target_id, uint32_t source_id, std::string &expr) +{ + if (!backend.force_gl_in_out_block) + return; + + auto *var = maybe_get<SPIRVariable>(source_id); + if (!var) + return; + + if (!is_builtin_variable(*var)) + return; + + auto &type = get_variable_data_type(*var); + auto builtin = BuiltIn(get_decoration(var->self, DecorationBuiltIn)); + if (!type.array.empty() && (builtin == BuiltInPosition || builtin == BuiltInPointSize)) + { + // We couldn't catch this case normally, because this is resolved in the OpAccessChain in most cases. + // If we load the entire array, we have no choice but to unroll here. + auto new_expr = join("_", target_id, "_unrolled"); + statement(variable_decl(type, new_expr), ";"); + string array_expr; + if (type.array_size_literal.front()) + array_expr = convert_to_string(type.array.front()); + else + array_expr = to_expression(type.array.front()); + + // The array size might be a specialization constant, so use a for-loop instead. + statement("for (int i = 0; i < int(", array_expr, "); i++)"); + begin_scope(); + statement(new_expr, "[i] = gl_in[i].", expr, ";"); + end_scope(); + + expr = move(new_expr); + } +} + void CompilerGLSL::bitcast_from_builtin_load(uint32_t source_id, std::string &expr, const spirv_cross::SPIRType &expr_type) { diff --git a/spirv_glsl.hpp b/spirv_glsl.hpp index e105c948..ed110c4e 100644 --- a/spirv_glsl.hpp +++ b/spirv_glsl.hpp @@ -619,6 +619,7 @@ protected: // Sometimes we will need to automatically perform bitcasts on load and store to make this work. virtual void bitcast_to_builtin_store(uint32_t target_id, std::string &expr, const SPIRType &expr_type); virtual void bitcast_from_builtin_load(uint32_t source_id, std::string &expr, const SPIRType &expr_type); + void unroll_array_from_builtin_load(uint32_t target_id, uint32_t source_id, std::string &expr); void handle_store_to_invariant_variable(uint32_t store_id, uint32_t value_id); void disallow_forwarding_in_expression_chain(const SPIRExpression &expr); |