SPIRV-Cross/reference/shaders-msl-no-opt/packing/matrix-4x2-std430.comp
Hans-Kristian Arntzen fa5b206d97 MSL: Workaround broken vector -> scalar access chain in MSL.
On MSL, the compiler refuses to allow access chains into a normal vector type.
What happens in practice instead is a read-modify-write where a vector type is
loaded, modified and written back.

The workaround is to convert a vector into a pointer-to-scalar before
the access chain continues to add the scalar index.
2020-07-06 10:03:44 +02:00

87 lines
2.3 KiB
Plaintext

#pragma clang diagnostic ignored "-Wmissing-prototypes"
#include <metal_stdlib>
#include <simd/simd.h>
using namespace metal;
struct SSBOCol
{
float4x2 col_major0;
float4x2 col_major1;
};
struct SSBORow
{
float2x4 row_major0;
float2x4 row_major1;
};
constant uint3 gl_WorkGroupSize [[maybe_unused]] = uint3(1u);
static inline __attribute__((always_inline))
void load_store_to_variable_col_major(device SSBOCol& v_29)
{
float4x2 loaded = v_29.col_major0;
v_29.col_major1 = loaded;
}
static inline __attribute__((always_inline))
void load_store_to_variable_row_major(device SSBORow& v_41)
{
float4x2 loaded = transpose(v_41.row_major0);
v_41.row_major0 = transpose(loaded);
}
static inline __attribute__((always_inline))
void copy_col_major_to_col_major(device SSBOCol& v_29)
{
v_29.col_major0 = v_29.col_major1;
}
static inline __attribute__((always_inline))
void copy_col_major_to_row_major(device SSBOCol& v_29, device SSBORow& v_41)
{
v_41.row_major0 = transpose(v_29.col_major0);
}
static inline __attribute__((always_inline))
void copy_row_major_to_col_major(device SSBOCol& v_29, device SSBORow& v_41)
{
v_29.col_major0 = transpose(v_41.row_major0);
}
static inline __attribute__((always_inline))
void copy_row_major_to_row_major(device SSBORow& v_41)
{
v_41.row_major0 = v_41.row_major1;
}
static inline __attribute__((always_inline))
void copy_columns(device SSBOCol& v_29, device SSBORow& v_41)
{
v_29.col_major0[1] = float2(v_41.row_major0[0][1], v_41.row_major0[1][1]);
v_41.row_major0[0][1] = v_29.col_major0[1].x;
v_41.row_major0[1][1] = v_29.col_major0[1].y;
}
static inline __attribute__((always_inline))
void copy_elements(device SSBOCol& v_29, device SSBORow& v_41)
{
((device float*)&v_29.col_major0[0])[1u] = ((device float*)&v_41.row_major0[1u])[0];
((device float*)&v_41.row_major0[1u])[0] = ((device float*)&v_29.col_major0[0])[1u];
}
kernel void main0(device SSBOCol& v_29 [[buffer(0)]], device SSBORow& v_41 [[buffer(1)]])
{
load_store_to_variable_col_major(v_29);
load_store_to_variable_row_major(v_41);
copy_col_major_to_col_major(v_29);
copy_col_major_to_row_major(v_29, v_41);
copy_row_major_to_col_major(v_29, v_41);
copy_row_major_to_row_major(v_41);
copy_columns(v_29, v_41);
copy_elements(v_29, v_41);
}