|
| 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 | +#include <metal_mesh> |
| 7 | + |
| 8 | +using namespace metal; |
| 9 | + |
| 10 | +template<typename T, size_t Num> |
| 11 | +struct spvUnsafeArray |
| 12 | +{ |
| 13 | + T elements[Num ? Num : 1]; |
| 14 | + |
| 15 | + thread T& operator [] (size_t pos) thread |
| 16 | + { |
| 17 | + return elements[pos]; |
| 18 | + } |
| 19 | + constexpr const thread T& operator [] (size_t pos) const thread |
| 20 | + { |
| 21 | + return elements[pos]; |
| 22 | + } |
| 23 | + |
| 24 | + device T& operator [] (size_t pos) device |
| 25 | + { |
| 26 | + return elements[pos]; |
| 27 | + } |
| 28 | + constexpr const device T& operator [] (size_t pos) const device |
| 29 | + { |
| 30 | + return elements[pos]; |
| 31 | + } |
| 32 | + |
| 33 | + constexpr const constant T& operator [] (size_t pos) const constant |
| 34 | + { |
| 35 | + return elements[pos]; |
| 36 | + } |
| 37 | + |
| 38 | + threadgroup T& operator [] (size_t pos) threadgroup |
| 39 | + { |
| 40 | + return elements[pos]; |
| 41 | + } |
| 42 | + constexpr const threadgroup T& operator [] (size_t pos) const threadgroup |
| 43 | + { |
| 44 | + return elements[pos]; |
| 45 | + } |
| 46 | +}; |
| 47 | + |
| 48 | +template<typename V, typename P, int MaxV, int MaxP, metal::topology T> |
| 49 | +struct spvMeshStream |
| 50 | +{ |
| 51 | + using mesh_t = metal::mesh<V, P, MaxV, MaxP, T>; |
| 52 | + thread mesh_t &meshOut; |
| 53 | + int currentVertex = 0; |
| 54 | + int currentIndex = 0; |
| 55 | + int currentVertexInPrimitive = 0; |
| 56 | + int currentPrimitive = 0; |
| 57 | + thread P &primitiveData; |
| 58 | + thread V &vertexData; |
| 59 | + spvMeshStream(thread mesh_t &_meshOut, thread V &_v, thread P &_p) : meshOut(_meshOut), primitiveData(_p), vertexData(_v) |
| 60 | + { |
| 61 | + } |
| 62 | + ~spvMeshStream() |
| 63 | + { |
| 64 | + meshOut.set_primitive_count(currentPrimitive); |
| 65 | + } |
| 66 | + int VperP() |
| 67 | + { |
| 68 | + if (T == metal::topology::triangle) return 3; |
| 69 | + else if (T == metal::topology::line) return 2; |
| 70 | + else /* if (T == metal::topology::point) */ return 1; |
| 71 | + } |
| 72 | + void EndPrimitive() |
| 73 | + { |
| 74 | + currentVertexInPrimitive = 0; |
| 75 | + } |
| 76 | + void EmitVertex() |
| 77 | + { |
| 78 | + meshOut.set_vertex(currentVertex++, vertexData); |
| 79 | + currentVertexInPrimitive++; |
| 80 | + if (currentVertexInPrimitive >= VperP()) |
| 81 | + { |
| 82 | + if (T == metal::topology::triangle) meshOut.set_index(currentIndex++, currentVertex-3); |
| 83 | + if (T == metal::topology::triangle || T == metal::topology::line) meshOut.set_index(currentIndex++, currentVertex-2); |
| 84 | + meshOut.set_index(currentIndex++, currentVertex-1); |
| 85 | + meshOut.set_primitive(currentPrimitive++, primitiveData); |
| 86 | + } |
| 87 | + } |
| 88 | +}; |
| 89 | +struct VertexData |
| 90 | +{ |
| 91 | + float3 normal; |
| 92 | + float4 pos; |
| 93 | +}; |
| 94 | + |
| 95 | +struct main0_out_2 |
| 96 | +{ |
| 97 | +}; |
| 98 | +struct main0_out_1 |
| 99 | +{ |
| 100 | + float3 vNormal [[user(locn0)]]; |
| 101 | + float4 gl_Position [[position]]; |
| 102 | +}; |
| 103 | + |
| 104 | +struct main0_out_2_1 |
| 105 | +{ |
| 106 | +}; |
| 107 | +struct main0_in |
| 108 | +{ |
| 109 | + spvUnsafeArray<VertexData, 3> vin; |
| 110 | + spvUnsafeArray<float4, 3> pos; |
| 111 | +}; |
| 112 | + |
| 113 | +enum { VERTEX_COUNT = 3, PRIMITIVE_COUNT = 1 }; |
| 114 | +using mesh_stream_t = spvMeshStream<main0_out_1, main0_out_2_1, VERTEX_COUNT, PRIMITIVE_COUNT, metal::topology::triangle>; |
| 115 | +void main0(mesh_stream_t::mesh_t spvMeshOut, main0_in in) |
| 116 | +{ |
| 117 | + main0_out_1 out = {}; |
| 118 | + main0_out_2_1 out_1 = {}; |
| 119 | + mesh_stream_t meshStream(spvMeshOut, out, out_1); |
| 120 | + out.gl_Position = in.pos[0]; |
| 121 | + out.vNormal = in.vin[0].normal; |
| 122 | + meshStream.EmitVertex(); |
| 123 | + out.gl_Position = in.pos[1]; |
| 124 | + out.vNormal = in.vin[1].normal; |
| 125 | + meshStream.EmitVertex(); |
| 126 | + out.gl_Position = in.pos[2]; |
| 127 | + out.vNormal = in.vin[2].normal; |
| 128 | + meshStream.EmitVertex(); |
| 129 | + meshStream.EndPrimitive(); |
| 130 | +} |
| 131 | + |
| 132 | +struct Payload |
| 133 | +{ |
| 134 | + struct |
| 135 | + { |
| 136 | + struct |
| 137 | + { |
| 138 | + VertexData vin [[user(locn0)]]; |
| 139 | + float4 pos [[user(locn2)]]; |
| 140 | + } in; |
| 141 | + } vertices[3]; |
| 142 | +}; |
| 143 | +[[mesh]] void main0(mesh_stream_t::mesh_t outputMesh, const object_data Payload &payload [[payload]], |
| 144 | + |
| 145 | +uint lid [[thread_index_in_threadgroup]], uint tid [[threadgroup_position_in_grid]]) |
| 146 | +{ |
| 147 | + main0_in in; |
| 148 | + const unsigned long vertexCount = 3; |
| 149 | + for (unsigned long i = 0; i < vertexCount; ++i) |
| 150 | + { |
| 151 | + auto out = payload.vertices[i]; |
| 152 | + if (i < sizeof(in.pos) / sizeof(in.pos[0])) |
| 153 | + in.pos[i] = out.in.pos; |
| 154 | + if (i < sizeof(in.vin) / sizeof(in.vin[0])) |
| 155 | + in.vin[i] = out.in.vin; |
| 156 | + } |
| 157 | + main0(outputMesh, in |
| 158 | + ); |
| 159 | +} |
0 commit comments