#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;
    }
}

struct gl_MeshPerVertexEXT
{
    float4 gl_Position [[position]];
    float gl_PointSize;
    float gl_ClipDistance [[clip_distance]] [1];
};

struct BlockOut
{
    float4 a;
    float4 b;
};

struct BlockOutPrim
{
    float4 a;
    float4 b;
};

struct TaskPayload
{
    float a;
    float b;
    int c;
};

struct gl_MeshPerPrimitiveEXT
{
    uint gl_PrimitiveID [[primitive_id]];
    uint gl_Layer [[render_target_array_index]];
    uint gl_ViewportIndex [[viewport_array_index]];
    bool gl_CullPrimitiveEXT [[primitive_culled]];
};

constant uint3 gl_WorkGroupSize [[maybe_unused]] = uint3(2u, 3u, 4u);

struct spvPerVertex
{
    float4 gl_Position [[position]];
    float gl_PointSize;
    float gl_ClipDistance [[clip_distance]] [1];
    float gl_ClipDistance_0 [[user(clip0)]];
    float4 vOut [[user(locn0)]];
    float4 outputs_a [[user(locn2)]];
    float4 outputs_b [[user(locn3)]];
};

struct spvPerPrimitive
{
    float4 vPrim [[user(locn1)]];
    float4 prim_outputs_a [[user(locn4)]];
    float4 prim_outputs_b [[user(locn5)]];
    uint gl_PrimitiveID [[primitive_id]];
    uint gl_Layer [[render_target_array_index]];
    uint gl_ViewportIndex [[viewport_array_index]];
    bool gl_CullPrimitiveEXT [[primitive_culled]];
};

using spvMesh_t = mesh<spvPerVertex, spvPerPrimitive, 24, 22, topology::triangle>;

static inline __attribute__((always_inline))
void _4(threadgroup spvUnsafeArray<gl_MeshPerVertexEXT, 24>& gl_MeshVerticesEXT, thread uint& gl_LocalInvocationIndex, thread uint3& gl_GlobalInvocationID, threadgroup spvUnsafeArray<float4, 24>& vOut, threadgroup spvUnsafeArray<BlockOut, 24>& outputs, threadgroup spvUnsafeArray<float4, 22>& vPrim, thread uint3& gl_WorkGroupID, threadgroup spvUnsafeArray<BlockOutPrim, 22>& prim_outputs, const object_data TaskPayload& payload, threadgroup spvUnsafeArray<uint3, 22>& gl_PrimitiveTriangleIndicesEXT, threadgroup spvUnsafeArray<gl_MeshPerPrimitiveEXT, 22>& gl_MeshPrimitivesEXT, threadgroup uint2& spvMeshSizes)
{
    spvSetMeshOutputsEXT(gl_LocalInvocationIndex, spvMeshSizes, 24u, 22u);
    gl_MeshVerticesEXT[gl_LocalInvocationIndex].gl_Position = float4(float3(gl_GlobalInvocationID), 1.0);
    gl_MeshVerticesEXT[gl_LocalInvocationIndex].gl_PointSize = 2.0;
    gl_MeshVerticesEXT[gl_LocalInvocationIndex].gl_ClipDistance[0] = 4.0;
    vOut[gl_LocalInvocationIndex] = float4(float3(gl_GlobalInvocationID), 2.0);
    outputs[gl_LocalInvocationIndex].a = float4(5.0);
    outputs[gl_LocalInvocationIndex].b = float4(6.0);
    threadgroup_barrier(mem_flags::mem_threadgroup);
    if (gl_LocalInvocationIndex < 22u)
    {
        vPrim[gl_LocalInvocationIndex] = float4(float3(gl_WorkGroupID), 3.0);
        prim_outputs[gl_LocalInvocationIndex].a = float4(payload.a);
        prim_outputs[gl_LocalInvocationIndex].b = float4(payload.b);
        gl_PrimitiveTriangleIndicesEXT[gl_LocalInvocationIndex] = uint3(0u, 1u, 2u) + uint3(gl_LocalInvocationIndex);
        gl_MeshPrimitivesEXT[gl_LocalInvocationIndex].gl_PrimitiveID = int(gl_GlobalInvocationID.x);
        gl_MeshPrimitivesEXT[gl_LocalInvocationIndex].gl_Layer = int(gl_GlobalInvocationID.x) + 1;
        gl_MeshPrimitivesEXT[gl_LocalInvocationIndex].gl_ViewportIndex = int(gl_GlobalInvocationID.x) + 2;
        gl_MeshPrimitivesEXT[gl_LocalInvocationIndex].gl_CullPrimitiveEXT = short((gl_GlobalInvocationID.x & 1u) != 0u);
    }
}

[[mesh]] void main0(uint gl_LocalInvocationIndex [[thread_index_in_threadgroup]], uint3 gl_GlobalInvocationID [[thread_position_in_grid]], uint3 gl_WorkGroupID [[threadgroup_position_in_grid]], spvMesh_t spvMesh, const object_data TaskPayload& payload [[payload]])
{
    threadgroup uint2 spvMeshSizes;
    threadgroup spvUnsafeArray<gl_MeshPerVertexEXT, 24> gl_MeshVerticesEXT;
    threadgroup spvUnsafeArray<float4, 24> vOut;
    threadgroup spvUnsafeArray<BlockOut, 24> outputs;
    threadgroup spvUnsafeArray<float4, 22> vPrim;
    threadgroup spvUnsafeArray<BlockOutPrim, 22> prim_outputs;
    threadgroup spvUnsafeArray<uint3, 22> gl_PrimitiveTriangleIndicesEXT;
    threadgroup spvUnsafeArray<gl_MeshPerPrimitiveEXT, 22> gl_MeshPrimitivesEXT;
    threadgroup spvUnsafeArray<float, 16> shared_float;
    if (gl_LocalInvocationIndex == 0) spvMeshSizes.y = 0u;
    _4(gl_MeshVerticesEXT, gl_LocalInvocationIndex, gl_GlobalInvocationID, vOut, outputs, vPrim, gl_WorkGroupID, prim_outputs, payload, gl_PrimitiveTriangleIndicesEXT, gl_MeshPrimitivesEXT, 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);
    const uint spvVI = gl_LocalInvocationIndex;
    if (gl_LocalInvocationIndex < spvMeshSizes.x)
    {
        spvPerVertex spvV = {};
        spvV.gl_Position = gl_MeshVerticesEXT[spvVI].gl_Position;
        spvV.gl_PointSize = gl_MeshVerticesEXT[spvVI].gl_PointSize;
        spvV.gl_ClipDistance[0] = gl_MeshVerticesEXT[spvVI].gl_ClipDistance[0];
        spvV.gl_ClipDistance_0 = gl_MeshVerticesEXT[spvVI].gl_ClipDistance[0];
        spvV.vOut = vOut[spvVI];
        spvV.outputs_a = outputs[spvVI].a;
        spvV.outputs_b = outputs[spvVI].b;
        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);
        spvPerPrimitive spvP = {};
        spvP.vPrim = vPrim[spvPI];
        spvP.prim_outputs_a = prim_outputs[spvPI].a;
        spvP.prim_outputs_b = prim_outputs[spvPI].b;
        spvP.gl_PrimitiveID = gl_MeshPrimitivesEXT[spvPI].gl_PrimitiveID;
        spvP.gl_Layer = gl_MeshPrimitivesEXT[spvPI].gl_Layer;
        spvP.gl_ViewportIndex = gl_MeshPrimitivesEXT[spvPI].gl_ViewportIndex;
        spvP.gl_CullPrimitiveEXT = gl_MeshPrimitivesEXT[spvPI].gl_CullPrimitiveEXT;
        spvMesh.set_primitive(spvPI, spvP);
    }
}

