#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(128u, 1u, 1u);

struct spvPerPrimitive
{
    float4 m_17 [[user(locn0)]];
};

using spvMesh_t = mesh<float4, spvPerPrimitive, 256, 256, topology::triangle>;

static inline __attribute__((always_inline))
void _4(thread uint& gl_LocalInvocationIndex, threadgroup uint2& spvMeshSizes)
{
    spvSetMeshOutputsEXT(gl_LocalInvocationIndex, spvMeshSizes, 0u, 0u);
}

[[mesh]] void main0(uint gl_LocalInvocationIndex [[thread_index_in_threadgroup]], spvMesh_t spvMesh)
{
    threadgroup uint2 spvMeshSizes;
    threadgroup spvUnsafeArray<float4, 256> _17;
    if (gl_LocalInvocationIndex == 0) spvMeshSizes.y = 0u;
    _4(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 spvPI = gl_LocalInvocationIndex; spvPI < spvMeshSizes.y; spvPI += spvThreadCount)
    {
        spvPerPrimitive spvP = {};
        spvP.m_17 = _17[spvPI];
        spvMesh.set_primitive(spvPI, spvP);
    }
}

