| 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 VertexOutput |
| 48 |
{ |
| 49 |
float4 pos; |
| 50 |
float2 uv; |
| 51 |
}; |
| 52 |
|
| 53 |
struct HSOut |
| 54 |
{ |
| 55 |
float4 pos; |
| 56 |
float2 uv; |
| 57 |
}; |
| 58 |
|
| 59 |
struct HSConstantOut |
| 60 |
{ |
| 61 |
spvUnsafeArray<float, 3> EdgeTess; |
| 62 |
float InsideTess; |
| 63 |
}; |
| 64 |
|
| 65 |
struct VertexOutput_1 |
| 66 |
{ |
| 67 |
float2 uv; |
| 68 |
}; |
| 69 |
|
| 70 |
struct HSOut_1 |
| 71 |
{ |
| 72 |
float2 uv; |
| 73 |
}; |
| 74 |
|
| 75 |
struct main0_out |
| 76 |
{ |
| 77 |
HSOut_1 _entryPointOutput; |
| 78 |
float4 gl_Position; |
| 79 |
}; |
| 80 |
|
| 81 |
struct main0_in |
| 82 |
{ |
| 83 |
float2 VertexOutput_uv [[attribute(0)]]; |
| 84 |
float4 gl_Position [[attribute(1)]]; |
| 85 |
}; |
| 86 |
|
| 87 |
static inline __attribute__((always_inline)) |
| 88 |
HSOut _hs_main(thread const spvUnsafeArray<VertexOutput, 3> (&p), thread const uint& i) |
| 89 |
{ |
| 90 |
HSOut _output; |
| 91 |
_output.pos = p[i].pos; |
| 92 |
_output.uv = p[i].uv; |
| 93 |
return _output; |
| 94 |
} |
| 95 |
|
| 96 |
static inline __attribute__((always_inline)) |
| 97 |
HSConstantOut PatchHS(thread const spvUnsafeArray<VertexOutput, 3> (&_patch)) |
| 98 |
{ |
| 99 |
HSConstantOut _output; |
| 100 |
_output.EdgeTess[0] = (float2(1.0) + _patch[0].uv).x; |
| 101 |
_output.EdgeTess[1] = (float2(1.0) + _patch[0].uv).x; |
| 102 |
_output.EdgeTess[2] = (float2(1.0) + _patch[0].uv).x; |
| 103 |
_output.InsideTess = (float2(1.0) + _patch[0].uv).x; |
| 104 |
return _output; |
| 105 |
} |
| 106 |
|
| 107 |
kernel void main0(main0_in in [[stage_in]], uint gl_InvocationID [[thread_index_in_threadgroup]], uint gl_PrimitiveID [[threadgroup_position_in_grid]], device main0_out* spvOut [[buffer(28)]], constant uint* spvIndirectParams [[buffer(29)]], device MTLTriangleTessellationFactorsHalf* spvTessLevel [[buffer(26)]], threadgroup main0_in* gl_in [[threadgroup(0)]]) |
| 108 |
{ |
| 109 |
device main0_out* gl_out = &spvOut[gl_PrimitiveID * 3]; |
| 110 |
if (gl_InvocationID < spvIndirectParams[0]) |
| 111 |
gl_in[gl_InvocationID] = in; |
| 112 |
threadgroup_barrier(mem_flags::mem_threadgroup); |
| 113 |
if (gl_InvocationID >= 3) |
| 114 |
return; |
| 115 |
spvUnsafeArray<VertexOutput, 3> p; |
| 116 |
p[0].pos = gl_in[0].gl_Position; |
| 117 |
p[0].uv = gl_in[0].VertexOutput_uv; |
| 118 |
p[1].pos = gl_in[1].gl_Position; |
| 119 |
p[1].uv = gl_in[1].VertexOutput_uv; |
| 120 |
p[2].pos = gl_in[2].gl_Position; |
| 121 |
p[2].uv = gl_in[2].VertexOutput_uv; |
| 122 |
uint i = gl_InvocationID; |
| 123 |
spvUnsafeArray<VertexOutput, 3> param; |
| 124 |
param = p; |
| 125 |
uint param_1 = i; |
| 126 |
HSOut flattenTemp = _hs_main(param, param_1); |
| 127 |
gl_out[gl_InvocationID].gl_Position = flattenTemp.pos; |
| 128 |
gl_out[gl_InvocationID]._entryPointOutput.uv = flattenTemp.uv; |
| 129 |
threadgroup_barrier(mem_flags::mem_device | mem_flags::mem_threadgroup); |
| 130 |
if (int(gl_InvocationID) == 0) |
| 131 |
{ |
| 132 |
spvUnsafeArray<VertexOutput, 3> param_2; |
| 133 |
param_2 = p; |
| 134 |
HSConstantOut _patchConstantResult = PatchHS(param_2); |
| 135 |
spvTessLevel[gl_PrimitiveID].edgeTessellationFactor[0] = half(_patchConstantResult.EdgeTess[0]); |
| 136 |
spvTessLevel[gl_PrimitiveID].edgeTessellationFactor[1] = half(_patchConstantResult.EdgeTess[1]); |
| 137 |
spvTessLevel[gl_PrimitiveID].edgeTessellationFactor[2] = half(_patchConstantResult.EdgeTess[2]); |
| 138 |
spvTessLevel[gl_PrimitiveID].insideTessellationFactor = half(_patchConstantResult.InsideTess); |
| 139 |
} |
| 140 |
} |
| 141 |
|