From 38b8f733d1466532a17f706e4bec94e6a40ab179 Mon Sep 17 00:00:00 2001 From: Hans-Kristian Arntzen Date: Mon, 29 Jan 2018 10:57:52 +0100 Subject: [PATCH] Fix passing arrays of arrays to functions in MSL. --- .../comp/shared-array-of-arrays.comp | 20 ++++++++++++ reference/shaders-msl/comp/functions.comp | 2 +- .../comp/shared-array-of-arrays.comp | 32 +++++++++++++++++++ shaders-msl/comp/shared-array-of-arrays.comp | 29 +++++++++++++++++ spirv_msl.cpp | 21 +++++++++--- 5 files changed, 98 insertions(+), 6 deletions(-) create mode 100644 reference/opt/shaders-msl/comp/shared-array-of-arrays.comp create mode 100644 reference/shaders-msl/comp/shared-array-of-arrays.comp create mode 100644 shaders-msl/comp/shared-array-of-arrays.comp diff --git a/reference/opt/shaders-msl/comp/shared-array-of-arrays.comp b/reference/opt/shaders-msl/comp/shared-array-of-arrays.comp new file mode 100644 index 00000000..44f05478 --- /dev/null +++ b/reference/opt/shaders-msl/comp/shared-array-of-arrays.comp @@ -0,0 +1,20 @@ +#include +#include + +using namespace metal; + +constant uint3 gl_WorkGroupSize = uint3(4u, 4u, 1u); + +struct SSBO +{ + float out_data[1]; +}; + +kernel void main0(uint3 gl_LocalInvocationID [[thread_position_in_threadgroup]], uint gl_LocalInvocationIndex [[thread_index_in_threadgroup]], device SSBO& _67 [[buffer(0)]], uint3 gl_GlobalInvocationID [[thread_position_in_grid]]) +{ + threadgroup float foo[4][4]; + foo[gl_LocalInvocationID.x][gl_LocalInvocationID.y] = float(gl_LocalInvocationIndex); + threadgroup_barrier(mem_flags::mem_threadgroup); + _67.out_data[gl_GlobalInvocationID.x] = (((0.0 + foo[gl_LocalInvocationID.x][0]) + foo[gl_LocalInvocationID.x][1]) + foo[gl_LocalInvocationID.x][2]) + foo[gl_LocalInvocationID.x][3]; +} + diff --git a/reference/shaders-msl/comp/functions.comp b/reference/shaders-msl/comp/functions.comp index 6127a39c..d8f6e55a 100644 --- a/reference/shaders-msl/comp/functions.comp +++ b/reference/shaders-msl/comp/functions.comp @@ -5,7 +5,7 @@ using namespace metal; -void myfunc(threadgroup int* foo) +void myfunc(threadgroup int (&foo)[1337]) { foo[0] = 13; } diff --git a/reference/shaders-msl/comp/shared-array-of-arrays.comp b/reference/shaders-msl/comp/shared-array-of-arrays.comp new file mode 100644 index 00000000..9ae114dd --- /dev/null +++ b/reference/shaders-msl/comp/shared-array-of-arrays.comp @@ -0,0 +1,32 @@ +#pragma clang diagnostic ignored "-Wmissing-prototypes" + +#include +#include + +using namespace metal; + +constant uint3 gl_WorkGroupSize = uint3(4u, 4u, 1u); + +struct SSBO +{ + float out_data[1]; +}; + +void work(threadgroup float (&foo)[4][4], thread uint3& gl_LocalInvocationID, thread uint& gl_LocalInvocationIndex, device SSBO& v_67, thread uint3& gl_GlobalInvocationID) +{ + foo[gl_LocalInvocationID.x][gl_LocalInvocationID.y] = float(gl_LocalInvocationIndex); + threadgroup_barrier(mem_flags::mem_threadgroup); + float x = 0.0; + x += foo[gl_LocalInvocationID.x][0]; + x += foo[gl_LocalInvocationID.x][1]; + x += foo[gl_LocalInvocationID.x][2]; + x += foo[gl_LocalInvocationID.x][3]; + v_67.out_data[gl_GlobalInvocationID.x] = x; +} + +kernel void main0(uint3 gl_LocalInvocationID [[thread_position_in_threadgroup]], uint gl_LocalInvocationIndex [[thread_index_in_threadgroup]], device SSBO& v_67 [[buffer(0)]], uint3 gl_GlobalInvocationID [[thread_position_in_grid]]) +{ + threadgroup float foo[4][4]; + work(foo, gl_LocalInvocationID, gl_LocalInvocationIndex, v_67, gl_GlobalInvocationID); +} + diff --git a/shaders-msl/comp/shared-array-of-arrays.comp b/shaders-msl/comp/shared-array-of-arrays.comp new file mode 100644 index 00000000..009b4e41 --- /dev/null +++ b/shaders-msl/comp/shared-array-of-arrays.comp @@ -0,0 +1,29 @@ +#version 310 es +layout(local_size_x = 4, local_size_y = 4) in; + +shared float foo[4][4]; + +layout(binding = 0, std430) buffer SSBO +{ + float out_data[]; +}; + +void work() +{ + foo[gl_LocalInvocationID.x][gl_LocalInvocationID.y] = float(gl_LocalInvocationIndex); + memoryBarrierShared(); + barrier(); + + float x = 0.0; + x += foo[gl_LocalInvocationID.x][0]; + x += foo[gl_LocalInvocationID.x][1]; + x += foo[gl_LocalInvocationID.x][2]; + x += foo[gl_LocalInvocationID.x][3]; + out_data[gl_GlobalInvocationID.x] = x; +} + +void main() +{ + work(); +} + diff --git a/spirv_msl.cpp b/spirv_msl.cpp index 70e9b410..1b46d28d 100644 --- a/spirv_msl.cpp +++ b/spirv_msl.cpp @@ -2837,17 +2837,28 @@ string CompilerMSL::argument_decl(const SPIRFunction::Parameter &arg) decl += "const "; if (is_builtin_variable(var)) - decl += builtin_type_decl((BuiltIn)get_decoration(arg.id, DecorationBuiltIn)); + decl += builtin_type_decl(static_cast(get_decoration(arg.id, DecorationBuiltIn))); else decl += type_to_glsl(type, arg.id); if (is_array(type)) - decl += "*"; + { + decl += " (&"; + decl += to_name(var.self); + decl += ")"; + decl += type_to_array_glsl(type); + } else if (!pointer) + { decl += "&"; - - decl += " "; - decl += to_name(var.self); + decl += " "; + decl += to_name(var.self); + } + else + { + decl += " "; + decl += to_name(var.self); + } return decl; }