#version 460

#define MAX_VER  81
#define MAX_PRIM 32

#define BARRIER() \
    memoryBarrierShared(); \
    barrier();

#extension GL_EXT_mesh_shader : enable

layout(local_size_x = 32, local_size_y=1, local_size_z=1) in;

layout(max_vertices=MAX_VER) out;
layout(max_primitives=MAX_PRIM) out;
layout(triangles) out;

// test use of builtins in mesh shaders:

void main()
{
    uint iid = gl_LocalInvocationID.x;
    uint gid = gl_WorkGroupID.x;
    uint vertexCount = MAX_VER; // vertexCount <= max_vertices
    uint primitiveCount = MAX_PRIM; // primitiveCount <= max_primtives
    SetMeshOutputsEXT(vertexCount, primitiveCount);

    gl_MeshVerticesEXT[iid].gl_Position = vec4(1.0);
    gl_MeshVerticesEXT[iid].gl_PointSize = 2.0;
    gl_MeshVerticesEXT[iid].gl_ClipDistance[3] = 3.0;
    gl_MeshVerticesEXT[iid].gl_CullDistance[2] = 4.0;

    BARRIER();

    gl_MeshVerticesEXT[iid+1].gl_Position = gl_MeshVerticesEXT[iid].gl_Position;
    gl_MeshVerticesEXT[iid+1].gl_PointSize = gl_MeshVerticesEXT[iid].gl_PointSize;
    gl_MeshVerticesEXT[iid+1].gl_ClipDistance[3] = gl_MeshVerticesEXT[iid].gl_ClipDistance[3];
    gl_MeshVerticesEXT[iid+1].gl_CullDistance[2] = gl_MeshVerticesEXT[iid].gl_CullDistance[2];

    BARRIER();

    gl_MeshPrimitivesEXT[iid].gl_PrimitiveID = 6;
    gl_MeshPrimitivesEXT[iid].gl_Layer = 7;
    gl_MeshPrimitivesEXT[iid].gl_ViewportIndex = 8;
    gl_MeshPrimitivesEXT[iid].gl_CullPrimitiveEXT = false;

    BARRIER();

    gl_MeshPrimitivesEXT[iid+1].gl_PrimitiveID = gl_MeshPrimitivesEXT[iid].gl_PrimitiveID;
    gl_MeshPrimitivesEXT[iid+1].gl_Layer = gl_MeshPrimitivesEXT[iid].gl_Layer;
    gl_MeshPrimitivesEXT[iid+1].gl_ViewportIndex = gl_MeshPrimitivesEXT[iid].gl_ViewportIndex;
    gl_MeshPrimitivesEXT[iid+1].gl_CullPrimitiveEXT = false;

    BARRIER();

    // check bound limits
    gl_PrimitiveTriangleIndicesEXT[0] = uvec3(1, 1, 1); // range is between [0, vertexCount-1]
    gl_PrimitiveTriangleIndicesEXT[primitiveCount - 1] = uvec3(2, 2, 2); // array size is primitiveCount*3 for triangle
    gl_PrimitiveTriangleIndicesEXT[gid] = gl_PrimitiveTriangleIndicesEXT[gid-1];


    BARRIER();
}

#extension GL_KHR_shader_subgroup_basic: enable
void basic_works (void)
{
  gl_SubgroupSize;
  gl_SubgroupInvocationID;
  subgroupBarrier();
  subgroupMemoryBarrier();
  subgroupMemoryBarrierBuffer();
  subgroupMemoryBarrierImage();
  subgroupElect();
  gl_NumSubgroups;                  // allowed in mesh
  gl_SubgroupID;                    // allowed in mesh
  subgroupMemoryBarrierShared();    // allowed in mesh
}

#extension GL_KHR_shader_subgroup_ballot: enable
void ballot_works(vec4 f4) {
  gl_SubgroupEqMask;
  gl_SubgroupGeMask;
  gl_SubgroupGtMask;
  gl_SubgroupLeMask;
  gl_SubgroupLtMask;
  subgroupBroadcast(f4, 0);
  subgroupBroadcastFirst(f4);
  uvec4 ballot = subgroupBallot(false);
  subgroupInverseBallot(uvec4(0x1));
  subgroupBallotBitExtract(ballot, 0);
  subgroupBallotBitCount(ballot);
  subgroupBallotInclusiveBitCount(ballot);
  subgroupBallotExclusiveBitCount(ballot);
  subgroupBallotFindLSB(ballot);
  subgroupBallotFindMSB(ballot);
}

#extension GL_KHR_shader_subgroup_vote: enable
void vote_works(vec4 f4)
{
  subgroupAll(true);
  subgroupAny(false);
  subgroupAllEqual(f4);
}

#extension GL_KHR_shader_subgroup_shuffle: enable
#extension GL_KHR_shader_subgroup_shuffle_relative: enable
void shuffle_works(vec4 f4)
{
  subgroupShuffle(f4, 0);
  subgroupShuffleXor(f4, 0x1);
  subgroupShuffleUp(f4, 1);
  subgroupShuffleDown(f4, 1);
}

#extension GL_KHR_shader_subgroup_arithmetic: enable
void arith_works(vec4 f4)
{
  uvec4 ballot;
  subgroupAdd(f4);
  subgroupMul(f4);
  subgroupMin(f4);
  subgroupMax(f4);
  subgroupAnd(ballot);
  subgroupOr(ballot);
  subgroupXor(ballot);
  subgroupInclusiveAdd(f4);
  subgroupInclusiveMul(f4);
  subgroupInclusiveMin(f4);
  subgroupInclusiveMax(f4);
  subgroupInclusiveAnd(ballot);
  subgroupInclusiveOr(ballot);
  subgroupInclusiveXor(ballot);
  subgroupExclusiveAdd(f4);
  subgroupExclusiveMul(f4);
  subgroupExclusiveMin(f4);
  subgroupExclusiveMax(f4);
  subgroupExclusiveAnd(ballot);
  subgroupExclusiveOr(ballot);
  subgroupExclusiveXor(ballot);
}

#extension GL_KHR_shader_subgroup_clustered: enable
void clustered_works(vec4 f4)
{
  uvec4 ballot = uvec4(0x55,0,0,0);
  subgroupClusteredAdd(f4, 2);
  subgroupClusteredMul(f4, 2);
  subgroupClusteredMin(f4, 2);
  subgroupClusteredMax(f4, 2);
  subgroupClusteredAnd(ballot, 2);
  subgroupClusteredOr(ballot, 2);
  subgroupClusteredXor(ballot, 2);
}

#extension GL_KHR_shader_subgroup_quad: enable
void quad_works(vec4 f4)
{
  subgroupQuadBroadcast(f4, 0);
  subgroupQuadSwapHorizontal(f4);
  subgroupQuadSwapVertical(f4);
  subgroupQuadSwapDiagonal(f4);
}
