| 1 |
#pragma clang diagnostic ignored "-Wmissing-prototypes" |
| 2 |
#pragma clang diagnostic ignored "-Wmissing-braces" |
| 3 |
|
| 4 |
#include <metal_stdlib> |
| 5 |
#include <simd/simd.h> |
| 6 |
|
| 7 |
using namespace metal; |
| 8 |
|
| 9 |
template<typename T, size_t Num> |
| 10 |
struct spvUnsafeArray |
| 11 |
{ |
| 12 |
T elements[Num ? Num : 1]; |
| 13 |
|
| 14 |
thread T& operator [] (size_t pos) thread |
| 15 |
{ |
| 16 |
return elements[pos]; |
| 17 |
} |
| 18 |
constexpr const thread T& operator [] (size_t pos) const thread |
| 19 |
{ |
| 20 |
return elements[pos]; |
| 21 |
} |
| 22 |
|
| 23 |
device T& operator [] (size_t pos) device |
| 24 |
{ |
| 25 |
return elements[pos]; |
| 26 |
} |
| 27 |
constexpr const device T& operator [] (size_t pos) const device |
| 28 |
{ |
| 29 |
return elements[pos]; |
| 30 |
} |
| 31 |
|
| 32 |
constexpr const constant T& operator [] (size_t pos) const constant |
| 33 |
{ |
| 34 |
return elements[pos]; |
| 35 |
} |
| 36 |
|
| 37 |
threadgroup T& operator [] (size_t pos) threadgroup |
| 38 |
{ |
| 39 |
return elements[pos]; |
| 40 |
} |
| 41 |
constexpr const threadgroup T& operator [] (size_t pos) const threadgroup |
| 42 |
{ |
| 43 |
return elements[pos]; |
| 44 |
} |
| 45 |
}; |
| 46 |
|
| 47 |
struct Matrices |
| 48 |
{ |
| 49 |
float4x4 vpMatrix; |
| 50 |
float4x4 wMatrix; |
| 51 |
float4x3 wMatrix4x3; |
| 52 |
float3x4 wMatrix3x4; |
| 53 |
}; |
| 54 |
|
| 55 |
struct main0_out |
| 56 |
{ |
| 57 |
float3 OutNormal [[user(locn0)]]; |
| 58 |
float4 OutWorldPos_0 [[user(locn1)]]; |
| 59 |
float4 OutWorldPos_1 [[user(locn2)]]; |
| 60 |
float4 OutWorldPos_2 [[user(locn3)]]; |
| 61 |
float4 OutWorldPos_3 [[user(locn4)]]; |
| 62 |
float4 gl_Position [[position]]; |
| 63 |
}; |
| 64 |
|
| 65 |
struct main0_in |
| 66 |
{ |
| 67 |
float3 InPos [[attribute(0)]]; |
| 68 |
float3 InNormal [[attribute(1)]]; |
| 69 |
}; |
| 70 |
|
| 71 |
template<typename T> |
| 72 |
T spvFMul(T l, T r) |
| 73 |
{ |
| 74 |
return fma(l, r, T(0)); |
| 75 |
} |
| 76 |
|
| 77 |
template<typename T, int Cols, int Rows> |
| 78 |
vec<T, Cols> spvFMulVectorMatrix(vec<T, Rows> v, matrix<T, Cols, Rows> m) |
| 79 |
{ |
| 80 |
vec<T, Cols> res = vec<T, Cols>(0); |
| 81 |
for (uint i = Rows; i > 0; --i) |
| 82 |
{ |
| 83 |
vec<T, Cols> tmp(0); |
| 84 |
for (uint j = 0; j < Cols; ++j) |
| 85 |
{ |
| 86 |
tmp[j] = m[j][i - 1]; |
| 87 |
} |
| 88 |
res = fma(tmp, vec<T, Cols>(v[i - 1]), res); |
| 89 |
} |
| 90 |
return res; |
| 91 |
} |
| 92 |
|
| 93 |
template<typename T, int Cols, int Rows> |
| 94 |
vec<T, Rows> spvFMulMatrixVector(matrix<T, Cols, Rows> m, vec<T, Cols> v) |
| 95 |
{ |
| 96 |
vec<T, Rows> res = vec<T, Rows>(0); |
| 97 |
for (uint i = Cols; i > 0; --i) |
| 98 |
{ |
| 99 |
res = fma(m[i - 1], vec<T, Rows>(v[i - 1]), res); |
| 100 |
} |
| 101 |
return res; |
| 102 |
} |
| 103 |
|
| 104 |
template<typename T, int LCols, int LRows, int RCols, int RRows> |
| 105 |
matrix<T, RCols, LRows> spvFMulMatrixMatrix(matrix<T, LCols, LRows> l, matrix<T, RCols, RRows> r) |
| 106 |
{ |
| 107 |
matrix<T, RCols, LRows> res; |
| 108 |
for (uint i = 0; i < RCols; i++) |
| 109 |
{ |
| 110 |
vec<T, RCols> tmp(0); |
| 111 |
for (uint j = 0; j < LCols; j++) |
| 112 |
{ |
| 113 |
tmp = fma(vec<T, RCols>(r[i][j]), l[j], tmp); |
| 114 |
} |
| 115 |
res[i] = tmp; |
| 116 |
} |
| 117 |
return res; |
| 118 |
} |
| 119 |
|
| 120 |
vertex main0_out main0(main0_in in [[stage_in]], constant Matrices& _22 [[buffer(0)]]) |
| 121 |
{ |
| 122 |
main0_out out = {}; |
| 123 |
spvUnsafeArray<float4, 4> OutWorldPos = {}; |
| 124 |
float4 _37 = float4(in.InPos, 1.0); |
| 125 |
out.gl_Position = spvFMulMatrixVector(spvFMulMatrixMatrix(_22.vpMatrix, _22.wMatrix), _37); |
| 126 |
OutWorldPos[0] = spvFMulMatrixVector(_22.wMatrix, _37); |
| 127 |
OutWorldPos[1] = spvFMulVectorMatrix(_37, _22.wMatrix); |
| 128 |
OutWorldPos[2] = spvFMulMatrixVector(_22.wMatrix3x4, in.InPos); |
| 129 |
OutWorldPos[3] = spvFMulVectorMatrix(in.InPos, _22.wMatrix4x3); |
| 130 |
out.OutNormal = spvFMulMatrixVector(_22.wMatrix, float4(in.InNormal, 0.0)).xyz; |
| 131 |
out.OutWorldPos_0 = OutWorldPos[0]; |
| 132 |
out.OutWorldPos_1 = OutWorldPos[1]; |
| 133 |
out.OutWorldPos_2 = OutWorldPos[2]; |
| 134 |
out.OutWorldPos_3 = OutWorldPos[3]; |
| 135 |
return out; |
| 136 |
} |
| 137 |
|