#pragma clang diagnostic ignored "-Wmissing-prototypes"
#pragma clang diagnostic ignored "-Wmissing-braces"

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

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];
    }
    
    object_data T& operator [] (size_t pos) object_data
    {
        return elements[pos];
    }
    constexpr const object_data T& operator [] (size_t pos) const object_data
    {
        return elements[pos];
    }
};

void spvSetMeshOutputsEXT(uint gl_LocalInvocationIndex, threadgroup uint2& spvMeshSizes, uint vertexCount, uint primitiveCount)
{
    if (gl_LocalInvocationIndex == 0)
    {
        spvMeshSizes.x = vertexCount;
        spvMeshSizes.y = primitiveCount;
    }
}

constant uint3 gl_WorkGroupSize [[maybe_unused]] = uint3(1, 1, 1);

struct spvPerVertex
{
    float4 gl_Position [[position]];
    float3 out_var_COLOR0 [[user(locn0)]];
};

using spvMesh_t = mesh<spvPerVertex, void, 3, 1, topology::triangle>;

static inline __attribute__((always_inline))
void _1(threadgroup spvUnsafeArray<uint3, 1>& gl_PrimitiveTriangleIndicesEXT, threadgroup spvUnsafeArray<float4, 3>& gl_Position, threadgroup spvUnsafeArray<float3, 3>& out_var_COLOR0, thread uint& gl_LocalInvocationIndex, threadgroup uint2& spvMeshSizes)
{
    spvSetMeshOutputsEXT(gl_LocalInvocationIndex, spvMeshSizes, 3u, 1u);
    gl_PrimitiveTriangleIndicesEXT[0] = uint3(0u, 1u, 2u);
    gl_Position[0] = float4(-0.5, -0.5, 0.0, 1.0);
    out_var_COLOR0[0] = float3(1.0, 0.0, 0.0);
    gl_Position[1] = float4(0.5, -0.5, 0.0, 1.0);
    out_var_COLOR0[1] = float3(0.0, 1.0, 0.0);
    gl_Position[2] = float4(0.0, 0.5, 0.0, 1.0);
    out_var_COLOR0[2] = float3(0.0, 0.0, 1.0);
}

[[mesh]] void main0(uint gl_LocalInvocationIndex [[thread_index_in_threadgroup]], spvMesh_t spvMesh)
{
    threadgroup uint2 spvMeshSizes;
    threadgroup spvUnsafeArray<uint3, 1> gl_PrimitiveTriangleIndicesEXT;
    threadgroup spvUnsafeArray<float4, 3> gl_Position;
    threadgroup spvUnsafeArray<float3, 3> out_var_COLOR0;
    if (gl_LocalInvocationIndex == 0) spvMeshSizes.y = 0u;
    _1(gl_PrimitiveTriangleIndicesEXT, gl_Position, out_var_COLOR0, gl_LocalInvocationIndex, spvMeshSizes);
    threadgroup_barrier(mem_flags::mem_threadgroup);
    if (spvMeshSizes.y == 0)
    {
        return;
    }
    spvMesh.set_primitive_count(spvMeshSizes.y);
    const uint spvThreadCount [[maybe_unused]] = (gl_WorkGroupSize.x * gl_WorkGroupSize.y * gl_WorkGroupSize.z);
    for (uint spvVI = gl_LocalInvocationIndex; spvVI < spvMeshSizes.x; spvVI += spvThreadCount)
    {
        spvPerVertex spvV = {};
        spvV.gl_Position = gl_Position[spvVI];
        spvV.out_var_COLOR0 = out_var_COLOR0[spvVI];
        spvMesh.set_vertex(spvVI, spvV);
    }
    const uint spvPI = gl_LocalInvocationIndex;
    if (gl_LocalInvocationIndex < spvMeshSizes.y)
    {
        spvMesh.set_index(spvPI * 3u + 0u, gl_PrimitiveTriangleIndicesEXT[spvPI].x);
        spvMesh.set_index(spvPI * 3u + 1u, gl_PrimitiveTriangleIndicesEXT[spvPI].y);
        spvMesh.set_index(spvPI * 3u + 2u, gl_PrimitiveTriangleIndicesEXT[spvPI].z);
    }
}

