From eb58f67727112bc602f98afa568fefe8d1353f66 Mon Sep 17 00:00:00 2001 From: Hans-Kristian Arntzen Date: Thu, 5 Oct 2017 16:31:52 +0200 Subject: [PATCH] Only unroll matrices for vertex input. Bandaid, might have to revisit this later. --- reference/shaders-hlsl/frag/matrix-input.frag | 26 +++++++++++++++++++ .../shaders-hlsl/vert/matrix-output.vert | 23 ++++++++++++++++ shaders-hlsl/frag/matrix-input.frag | 9 +++++++ shaders-hlsl/vert/matrix-output.vert | 9 +++++++ spirv_hlsl.cpp | 8 ++++-- 5 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 reference/shaders-hlsl/frag/matrix-input.frag create mode 100644 reference/shaders-hlsl/vert/matrix-output.vert create mode 100644 shaders-hlsl/frag/matrix-input.frag create mode 100644 shaders-hlsl/vert/matrix-output.vert diff --git a/reference/shaders-hlsl/frag/matrix-input.frag b/reference/shaders-hlsl/frag/matrix-input.frag new file mode 100644 index 00000000..92d87d39 --- /dev/null +++ b/reference/shaders-hlsl/frag/matrix-input.frag @@ -0,0 +1,26 @@ +static float4 FragColor; +static float4x4 m; + +struct SPIRV_Cross_Input +{ + float4x4 m : TEXCOORD1; +}; + +struct SPIRV_Cross_Output +{ + float4 FragColor : SV_Target0; +}; + +void frag_main() +{ + FragColor = ((m[0] + m[1]) + m[2]) + m[3]; +} + +SPIRV_Cross_Output main(SPIRV_Cross_Input stage_input) +{ + m = stage_input.m; + frag_main(); + SPIRV_Cross_Output stage_output; + stage_output.FragColor = FragColor; + return stage_output; +} diff --git a/reference/shaders-hlsl/vert/matrix-output.vert b/reference/shaders-hlsl/vert/matrix-output.vert new file mode 100644 index 00000000..f1290070 --- /dev/null +++ b/reference/shaders-hlsl/vert/matrix-output.vert @@ -0,0 +1,23 @@ +static float4 gl_Position; +static float4x4 m; + +struct SPIRV_Cross_Output +{ + float4x4 m : TEXCOORD0; + float4 gl_Position : SV_Position; +}; + +void vert_main() +{ + gl_Position = float4(1.0f, 1.0f, 1.0f, 1.0f); + m = float4x4(float4(1.0f, 0.0f, 0.0f, 0.0f), float4(0.0f, 1.0f, 0.0f, 0.0f), float4(0.0f, 0.0f, 1.0f, 0.0f), float4(0.0f, 0.0f, 0.0f, 1.0f)); +} + +SPIRV_Cross_Output main() +{ + vert_main(); + SPIRV_Cross_Output stage_output; + stage_output.gl_Position = gl_Position; + stage_output.m = m; + return stage_output; +} diff --git a/shaders-hlsl/frag/matrix-input.frag b/shaders-hlsl/frag/matrix-input.frag new file mode 100644 index 00000000..ffe242ce --- /dev/null +++ b/shaders-hlsl/frag/matrix-input.frag @@ -0,0 +1,9 @@ +#version 450 + +layout(location = 0) out vec4 FragColor; +layout(location = 1) in mat4 m; + +void main() +{ + FragColor = m[0] + m[1] + m[2] + m[3]; +} diff --git a/shaders-hlsl/vert/matrix-output.vert b/shaders-hlsl/vert/matrix-output.vert new file mode 100644 index 00000000..1151d4bd --- /dev/null +++ b/shaders-hlsl/vert/matrix-output.vert @@ -0,0 +1,9 @@ +#version 450 + +layout(location = 0) out mat4 m; + +void main() +{ + gl_Position = vec4(1.0); + m = mat4(1.0); +} diff --git a/spirv_hlsl.cpp b/spirv_hlsl.cpp index 8f2d902f..bc00235d 100644 --- a/spirv_hlsl.cpp +++ b/spirv_hlsl.cpp @@ -518,6 +518,8 @@ void CompilerHLSL::emit_interface_block_in_struct(const SPIRVariable &var, unord SPIRV_CROSS_THROW("All locations from 0 to 63 are exhausted."); }; + bool need_matrix_unroll = var.storage == StorageClassInput && execution.model == ExecutionModelVertex; + auto &m = meta[var.self].decoration; auto name = to_name(var.self); if (use_binding_number) @@ -531,7 +533,7 @@ void CompilerHLSL::emit_interface_block_in_struct(const SPIRVariable &var, unord else binding_number = get_vacant_location(); - if (type.columns > 1) + if (need_matrix_unroll && type.columns > 1) { if (!type.array.empty()) SPIRV_CROSS_THROW("Arrays of matrices used as input/output. This is not supported."); @@ -1277,12 +1279,14 @@ void CompilerHLSL::emit_hlsl_entry_point() if (var.storage != StorageClassInput) continue; + bool need_matrix_unroll = var.storage == StorageClassInput && execution.model == ExecutionModelVertex; + if (!block && !var.remapped_variable && type.pointer && !is_builtin_variable(var) && interface_variable_exists_in_entry_point(var.self)) { auto name = to_name(var.self); auto &mtype = get(var.basetype); - if (mtype.columns > 1) + if (need_matrix_unroll && mtype.columns > 1) { // Unroll matrices. for (uint32_t col = 0; col < mtype.columns; col++)