Skip to content

Commit

Permalink
MSL: Update reference shaders.
Browse files Browse the repository at this point in the history
  • Loading branch information
js6i committed Oct 5, 2023
1 parent f01cb63 commit 1ca83d8
Show file tree
Hide file tree
Showing 7 changed files with 399 additions and 4 deletions.
159 changes: 159 additions & 0 deletions reference/shaders-msl/geom/basic.msl31.geom
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
#pragma clang diagnostic ignored "-Wmissing-prototypes"
#pragma clang diagnostic ignored "-Wmissing-braces"

#include <metal_stdlib>
#include <simd/simd.h>
#include <metal_mesh>

using namespace metal;

template<typename T, size_t Num>
struct spvUnsafeArray
{
T elements[Num ? Num : 1];

thread T& operator [] (size_t pos) thread
{
return elements[pos];
}
constexpr const thread T& operator [] (size_t pos) const thread
{
return elements[pos];
}

device T& operator [] (size_t pos) device
{
return elements[pos];
}
constexpr const device T& operator [] (size_t pos) const device
{
return elements[pos];
}

constexpr const constant T& operator [] (size_t pos) const constant
{
return elements[pos];
}

threadgroup T& operator [] (size_t pos) threadgroup
{
return elements[pos];
}
constexpr const threadgroup T& operator [] (size_t pos) const threadgroup
{
return elements[pos];
}
};

template<typename V, typename P, int MaxV, int MaxP, metal::topology T>
struct spvMeshStream
{
using mesh_t = metal::mesh<V, P, MaxV, MaxP, T>;
thread mesh_t &meshOut;
int currentVertex = 0;
int currentIndex = 0;
int currentVertexInPrimitive = 0;
int currentPrimitive = 0;
thread P &primitiveData;
thread V &vertexData;
spvMeshStream(thread mesh_t &_meshOut, thread V &_v, thread P &_p) : meshOut(_meshOut), primitiveData(_p), vertexData(_v)
{
}
~spvMeshStream()
{
meshOut.set_primitive_count(currentPrimitive);
}
int VperP()
{
if (T == metal::topology::triangle) return 3;
else if (T == metal::topology::line) return 2;
else /* if (T == metal::topology::point) */ return 1;
}
void EndPrimitive()
{
currentVertexInPrimitive = 0;
}
void EmitVertex()
{
meshOut.set_vertex(currentVertex++, vertexData);
currentVertexInPrimitive++;
if (currentVertexInPrimitive >= VperP())
{
if (T == metal::topology::triangle) meshOut.set_index(currentIndex++, currentVertex-3);
if (T == metal::topology::triangle || T == metal::topology::line) meshOut.set_index(currentIndex++, currentVertex-2);
meshOut.set_index(currentIndex++, currentVertex-1);
meshOut.set_primitive(currentPrimitive++, primitiveData);
}
}
};
struct VertexData
{
float3 normal;
float4 pos;
};

struct main0_out_2
{
};
struct main0_out_1
{
float3 vNormal [[user(locn0)]];
float4 gl_Position [[position]];
};

struct main0_out_2_1
{
};
struct main0_in
{
spvUnsafeArray<VertexData, 3> vin;
spvUnsafeArray<float4, 3> pos;
};

enum { VERTEX_COUNT = 3, PRIMITIVE_COUNT = 1 };
using mesh_stream_t = spvMeshStream<main0_out_1, main0_out_2_1, VERTEX_COUNT, PRIMITIVE_COUNT, metal::topology::triangle>;
void main0(mesh_stream_t::mesh_t spvMeshOut, main0_in in)
{
main0_out_1 out = {};
main0_out_2_1 out_1 = {};
mesh_stream_t meshStream(spvMeshOut, out, out_1);
out.gl_Position = in.pos[0];
out.vNormal = in.vin[0].normal;
meshStream.EmitVertex();
out.gl_Position = in.pos[1];
out.vNormal = in.vin[1].normal;
meshStream.EmitVertex();
out.gl_Position = in.pos[2];
out.vNormal = in.vin[2].normal;
meshStream.EmitVertex();
meshStream.EndPrimitive();
}

struct Payload
{
struct
{
struct
{
VertexData vin [[user(locn0)]];
float4 pos [[user(locn2)]];
} in;
} vertices[3];
};
[[mesh]] void main0(mesh_stream_t::mesh_t outputMesh, const object_data Payload &payload [[payload]],

uint lid [[thread_index_in_threadgroup]], uint tid [[threadgroup_position_in_grid]])
{
main0_in in;
const unsigned long vertexCount = 3;
for (unsigned long i = 0; i < vertexCount; ++i)
{
auto out = payload.vertices[i];
if (i < sizeof(in.pos) / sizeof(in.pos[0]))
in.pos[i] = out.in.pos;
if (i < sizeof(in.vin) / sizeof(in.vin[0]))
in.vin[i] = out.in.vin;
}
main0(outputMesh, in
);
}
152 changes: 152 additions & 0 deletions reference/shaders-msl/geom/lines.msl31.geom
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
#pragma clang diagnostic ignored "-Wmissing-prototypes"
#pragma clang diagnostic ignored "-Wmissing-braces"

#include <metal_stdlib>
#include <simd/simd.h>
#include <metal_mesh>

using namespace metal;

template<typename T, size_t Num>
struct spvUnsafeArray
{
T elements[Num ? Num : 1];

thread T& operator [] (size_t pos) thread
{
return elements[pos];
}
constexpr const thread T& operator [] (size_t pos) const thread
{
return elements[pos];
}

device T& operator [] (size_t pos) device
{
return elements[pos];
}
constexpr const device T& operator [] (size_t pos) const device
{
return elements[pos];
}

constexpr const constant T& operator [] (size_t pos) const constant
{
return elements[pos];
}

threadgroup T& operator [] (size_t pos) threadgroup
{
return elements[pos];
}
constexpr const threadgroup T& operator [] (size_t pos) const threadgroup
{
return elements[pos];
}
};

template<typename V, typename P, int MaxV, int MaxP, metal::topology T>
struct spvMeshStream
{
using mesh_t = metal::mesh<V, P, MaxV, MaxP, T>;
thread mesh_t &meshOut;
int currentVertex = 0;
int currentIndex = 0;
int currentVertexInPrimitive = 0;
int currentPrimitive = 0;
thread P &primitiveData;
thread V &vertexData;
spvMeshStream(thread mesh_t &_meshOut, thread V &_v, thread P &_p) : meshOut(_meshOut), primitiveData(_p), vertexData(_v)
{
}
~spvMeshStream()
{
meshOut.set_primitive_count(currentPrimitive);
}
int VperP()
{
if (T == metal::topology::triangle) return 3;
else if (T == metal::topology::line) return 2;
else /* if (T == metal::topology::point) */ return 1;
}
void EndPrimitive()
{
currentVertexInPrimitive = 0;
}
void EmitVertex()
{
meshOut.set_vertex(currentVertex++, vertexData);
currentVertexInPrimitive++;
if (currentVertexInPrimitive >= VperP())
{
if (T == metal::topology::triangle) meshOut.set_index(currentIndex++, currentVertex-3);
if (T == metal::topology::triangle || T == metal::topology::line) meshOut.set_index(currentIndex++, currentVertex-2);
meshOut.set_index(currentIndex++, currentVertex-1);
meshOut.set_primitive(currentPrimitive++, primitiveData);
}
}
};
struct VertexData
{
float3 normal;
float4 position;
};

struct main0_out_2
{
};
struct main0_out_1
{
float3 vNormal [[user(locn0)]];
float4 gl_Position [[position]];
};

struct main0_out_2_1
{
};
struct main0_in
{
spvUnsafeArray<VertexData, 2> vin;
};

enum { VERTEX_COUNT = 2, PRIMITIVE_COUNT = 1 };
using mesh_stream_t = spvMeshStream<main0_out_1, main0_out_2_1, VERTEX_COUNT, PRIMITIVE_COUNT, metal::topology::line>;
void main0(mesh_stream_t::mesh_t spvMeshOut, main0_in in)
{
main0_out_1 out = {};
main0_out_2_1 out_1 = {};
mesh_stream_t meshStream(spvMeshOut, out, out_1);
out.gl_Position = in.vin[0].position;
out.vNormal = in.vin[0].normal;
meshStream.EmitVertex();
out.gl_Position = in.vin[1].position;
out.vNormal = in.vin[1].normal;
meshStream.EmitVertex();
meshStream.EndPrimitive();
}

struct Payload
{
struct
{
struct
{
VertexData vin [[user(locn0)]];
} in;
} vertices[2];
};
[[mesh]] void main0(mesh_stream_t::mesh_t outputMesh, const object_data Payload &payload [[payload]],

uint lid [[thread_index_in_threadgroup]], uint tid [[threadgroup_position_in_grid]])
{
main0_in in;
const unsigned long vertexCount = 2;
for (unsigned long i = 0; i < vertexCount; ++i)
{
auto out = payload.vertices[i];
if (i < sizeof(in.vin) / sizeof(in.vin[0]))
in.vin[i] = out.in.vin;
}
main0(outputMesh, in
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ struct main0_patchOut
struct main0_in
{
float3 in_tc_attr;
ushort2 m_179;
ushort2 m_182;
};

kernel void main0(uint3 gl_GlobalInvocationID [[thread_position_in_grid]], constant uint* spvIndirectParams [[buffer(29)]], device main0_patchOut* spvPatchOut [[buffer(27)]], device MTLQuadTessellationFactorsHalf* spvTessLevel [[buffer(26)]], device main0_in* spvIn [[buffer(22)]])
Expand Down
2 changes: 1 addition & 1 deletion reference/shaders-msl/tesc/matrix-output.multi-patch.tesc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ struct main0_out
struct main0_in
{
float3 in_tc_attr;
ushort2 m_103;
ushort2 m_106;
};

kernel void main0(uint3 gl_GlobalInvocationID [[thread_position_in_grid]], device main0_out* spvOut [[buffer(28)]], constant uint* spvIndirectParams [[buffer(29)]], device MTLQuadTessellationFactorsHalf* spvTessLevel [[buffer(26)]], device main0_in* spvIn [[buffer(22)]])
Expand Down
2 changes: 1 addition & 1 deletion reference/shaders-msl/tesc/struct-output.multi-patch.tesc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ struct main0_out
struct main0_in
{
float3 in_tc_attr;
ushort2 m_107;
ushort2 m_110;
};

kernel void main0(uint3 gl_GlobalInvocationID [[thread_position_in_grid]], device main0_out* spvOut [[buffer(28)]], constant uint* spvIndirectParams [[buffer(29)]], device MTLQuadTessellationFactorsHalf* spvTessLevel [[buffer(26)]], device main0_in* spvIn [[buffer(22)]])
Expand Down
2 changes: 1 addition & 1 deletion reference/shaders-msl/tesc/water_tess.multi-patch.tesc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ struct main0_patchOut
struct main0_in
{
float3 vPatchPosBase;
ushort2 m_430;
ushort2 m_433;
};

static inline __attribute__((always_inline))
Expand Down
Loading

0 comments on commit 1ca83d8

Please sign in to comment.