Skip to content

Commit

Permalink
Add vertex loader tests
Browse files Browse the repository at this point in the history
  • Loading branch information
etang-cw committed Sep 27, 2023
1 parent 020d39a commit f2889b2
Show file tree
Hide file tree
Showing 16 changed files with 567 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include <metal_stdlib>
#include <simd/simd.h>

using namespace metal;

struct main0_out
{
float3 vNormal;
float4 gl_Position;
};

struct main0_in
{
float4 aVertex [[attribute(0)]];
float3 aNormal [[attribute(1)]];
};

struct spvVertexData0
{
packed_half4 aVertex;
packed_short4 aNormal;
};
static_assert(alignof(spvVertexData0) == 2, "Unexpected alignment");
static_assert(sizeof(spvVertexData0) == 16, "Unexpected size");

main0_in spvLoadVertex(const device spvVertexData0& data0)
{
main0_in out;
out.aVertex = float4(half4(data0.aVertex));
out.aNormal = max(float4(short4(data0.aNormal)) * (1.f / 32767), -1.f).rgb;
return out;
}

kernel void main0(device const spvVertexData0* spvVertexBuffer0 [[buffer(0)]], uint3 gl_GlobalInvocationID [[thread_position_in_grid]], uint3 spvStageInputSize [[grid_size]], uint3 spvDispatchBase [[grid_origin]], device main0_out* spvOut [[buffer(28)]], const device ushort* spvIndices [[buffer(21)]])
{
device main0_out& out = spvOut[gl_GlobalInvocationID.y * spvStageInputSize.x + gl_GlobalInvocationID.x];
if (any(gl_GlobalInvocationID >= spvStageInputSize))
return;
uint gl_VertexIndex = spvIndices[gl_GlobalInvocationID.x] + spvDispatchBase.x;
uint gl_BaseVertex = spvDispatchBase.x;
uint gl_InstanceIndex = gl_GlobalInvocationID.y + spvDispatchBase.y;
uint gl_BaseInstance = spvDispatchBase.y;
main0_in in = spvLoadVertex(spvVertexBuffer0[gl_VertexIndex]);
out.gl_Position = in.aVertex;
out.vNormal = in.aNormal;
}

42 changes: 42 additions & 0 deletions reference/opt/shaders-msl/vert/basic.vertex-loader.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include <metal_stdlib>
#include <simd/simd.h>

using namespace metal;

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

struct main0_in
{
float4 aVertex [[attribute(0)]];
float3 aNormal [[attribute(1)]];
};

struct spvVertexData0
{
packed_half4 aVertex;
packed_short4 aNormal;
};
static_assert(alignof(spvVertexData0) == 2, "Unexpected alignment");
static_assert(sizeof(spvVertexData0) == 16, "Unexpected size");

main0_in spvLoadVertex(const device spvVertexData0& data0)
{
main0_in out;
out.aVertex = float4(half4(data0.aVertex));
out.aNormal = max(float4(short4(data0.aNormal)) * (1.f / 32767), -1.f).rgb;
return out;
}

vertex main0_out main0(device const spvVertexData0* spvVertexBuffer0 [[buffer(0)]], uint gl_VertexIndex [[vertex_id]], uint gl_BaseVertex [[base_vertex]], uint gl_InstanceIndex [[instance_id]], uint gl_BaseInstance [[base_instance]])
{
main0_out out = {};
main0_in in = spvLoadVertex(spvVertexBuffer0[gl_VertexIndex]);
out.gl_Position = in.aVertex;
out.vNormal = in.aNormal;
return out;
}

48 changes: 48 additions & 0 deletions reference/opt/shaders-msl/vert/pack-align.vertex-loader.msl23.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include <metal_stdlib>
#include <simd/simd.h>

using namespace metal;

struct UBO
{
float4x4 uMVP;
};

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

struct main0_in
{
float4 aVertex [[attribute(2)]];
float3 aNormal [[attribute(3)]];
};

struct alignas(4) spvVertexData1
{
rgba8unorm<float4> aVertex;
uchar spvPad4;
packed_uchar3 aNormal;
};
static_assert(alignof(spvVertexData1) == 4, "Unexpected alignment");
static_assert(sizeof(spvVertexData1) == 8, "Unexpected size");

main0_in spvLoadVertex(const device spvVertexData1& data1)
{
main0_in out;
out.aVertex = unpack_unorm4x8_to_float(reinterpret_cast<const device uint&>(data1.aVertex));
out.aNormal = float3(data1.aNormal).bgr;
return out;
}

vertex main0_out main0(device const spvVertexData1* spvVertexBuffer1 [[buffer(1)]], constant UBO& _16 [[buffer(0)]], uint gl_VertexIndex [[vertex_id]], uint gl_BaseVertex [[base_vertex]], uint gl_InstanceIndex [[instance_id]], uint gl_BaseInstance [[base_instance]])
{
main0_out out = {};
main0_in in = spvLoadVertex(spvVertexBuffer1[gl_InstanceIndex]);
out.gl_Position = _16.uMVP * in.aVertex;
out.vNormal = in.aNormal;
return out;
}

64 changes: 64 additions & 0 deletions reference/opt/shaders-msl/vert/packed-formats.vertex-loader.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include <metal_stdlib>
#include <simd/simd.h>

using namespace metal;

static half3 spvLoadVertexRG11B10Half(uint value)
{
ushort3 res = ushort3(value << 5, (value >> 6) & 0xffc0, (value >> 16) & 0xff80);
return as_type<half3>(res);
}
static float3 spvLoadVertexRGB9E5Float(uint value)
{
float exponent = exp2(float(value >> 27)) * exp2(float(-(15 + 9)));
int3 mantissa = int3(value & 0x1ff, extract_bits(value, 9, 9), extract_bits(value, 18, 9));
return float3(mantissa) * exp2(float(exponent));
}
struct UBO
{
float4x4 uMVP;
};

struct main0_out
{
float3 vNormal [[user(locn0)]];
float3 vExtra [[user(locn1)]];
float4 gl_Position [[position]];
};

struct main0_in
{
float4 aVertex [[attribute(4)]];
float3 aNormal [[attribute(5)]];
float3 aExtra [[attribute(6)]];
};

struct spvVertexData2
{
uint aVertex;
uint aNormal;
uint aExtra;
uint spvPad12;
};
static_assert(alignof(spvVertexData2) == 4, "Unexpected alignment");
static_assert(sizeof(spvVertexData2) == 16, "Unexpected size");

main0_in spvLoadVertex(const device spvVertexData2& data2)
{
main0_in out;
out.aVertex = unpack_unorm10a2_to_float(data2.aVertex);
out.aNormal = spvLoadVertexRGB9E5Float(data2.aNormal);
out.aExtra = float3(spvLoadVertexRG11B10Half(data2.aExtra));
return out;
}

vertex main0_out main0(device const spvVertexData2* spvVertexBuffer2 [[buffer(2)]], constant UBO& _16 [[buffer(0)]], uint gl_VertexIndex [[vertex_id]], uint gl_BaseVertex [[base_vertex]], uint gl_InstanceIndex [[instance_id]], uint gl_BaseInstance [[base_instance]])
{
main0_out out = {};
main0_in in = spvLoadVertex(spvVertexBuffer2[gl_BaseInstance]);
out.gl_Position = _16.uMVP * in.aVertex;
out.vNormal = in.aNormal;
out.vExtra = in.aExtra;
return out;
}

39 changes: 39 additions & 0 deletions reference/opt/shaders-msl/vert/past-stride.vertex-loader.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <metal_stdlib>
#include <simd/simd.h>

using namespace metal;

struct main0_out
{
float4 gl_Position [[position]];
};

struct main0_in
{
uint aUInt [[attribute(7)]];
float aUNorm [[attribute(8)]];
};

struct alignas(4) spvVertexData3
{
uchar data[16];
};
static_assert(alignof(spvVertexData3) == 4, "Unexpected alignment");
static_assert(sizeof(spvVertexData3) == 16, "Unexpected size");

main0_in spvLoadVertex(const device spvVertexData3& data3)
{
main0_in out;
out.aUInt = reinterpret_cast<const device uint&>(data3.data[32]);
out.aUNorm = float(data3.data[1]) * (1.f / 255);
return out;
}

vertex main0_out main0(device const spvVertexData3* spvVertexBuffer3 [[buffer(3)]], uint gl_VertexIndex [[vertex_id]], uint gl_BaseVertex [[base_vertex]], uint gl_InstanceIndex [[instance_id]], uint gl_BaseInstance [[base_instance]])
{
main0_out out = {};
main0_in in = spvLoadVertex(spvVertexBuffer3[gl_BaseInstance + (gl_InstanceIndex - gl_BaseInstance) / 4]);
out.gl_Position = float4(float(in.aUInt) * 0.015625, in.aUNorm, 0.0, 1.0);
return out;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include <metal_stdlib>
#include <simd/simd.h>

using namespace metal;

struct main0_out
{
float3 vNormal;
float4 gl_Position;
};

struct main0_in
{
float4 aVertex [[attribute(0)]];
float3 aNormal [[attribute(1)]];
};

struct spvVertexData0
{
packed_half4 aVertex;
packed_short4 aNormal;
};
static_assert(alignof(spvVertexData0) == 2, "Unexpected alignment");
static_assert(sizeof(spvVertexData0) == 16, "Unexpected size");

main0_in spvLoadVertex(const device spvVertexData0& data0)
{
main0_in out;
out.aVertex = float4(half4(data0.aVertex));
out.aNormal = max(float4(short4(data0.aNormal)) * (1.f / 32767), -1.f).rgb;
return out;
}

kernel void main0(device const spvVertexData0* spvVertexBuffer0 [[buffer(0)]], uint3 gl_GlobalInvocationID [[thread_position_in_grid]], uint3 spvStageInputSize [[grid_size]], uint3 spvDispatchBase [[grid_origin]], device main0_out* spvOut [[buffer(28)]], const device ushort* spvIndices [[buffer(21)]])
{
device main0_out& out = spvOut[gl_GlobalInvocationID.y * spvStageInputSize.x + gl_GlobalInvocationID.x];
if (any(gl_GlobalInvocationID >= spvStageInputSize))
return;
uint gl_VertexIndex = spvIndices[gl_GlobalInvocationID.x] + spvDispatchBase.x;
uint gl_BaseVertex = spvDispatchBase.x;
uint gl_InstanceIndex = gl_GlobalInvocationID.y + spvDispatchBase.y;
uint gl_BaseInstance = spvDispatchBase.y;
main0_in in = spvLoadVertex(spvVertexBuffer0[gl_VertexIndex]);
out.gl_Position = in.aVertex;
out.vNormal = in.aNormal;
}

42 changes: 42 additions & 0 deletions reference/shaders-msl/vert/basic.vertex-loader.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include <metal_stdlib>
#include <simd/simd.h>

using namespace metal;

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

struct main0_in
{
float4 aVertex [[attribute(0)]];
float3 aNormal [[attribute(1)]];
};

struct spvVertexData0
{
packed_half4 aVertex;
packed_short4 aNormal;
};
static_assert(alignof(spvVertexData0) == 2, "Unexpected alignment");
static_assert(sizeof(spvVertexData0) == 16, "Unexpected size");

main0_in spvLoadVertex(const device spvVertexData0& data0)
{
main0_in out;
out.aVertex = float4(half4(data0.aVertex));
out.aNormal = max(float4(short4(data0.aNormal)) * (1.f / 32767), -1.f).rgb;
return out;
}

vertex main0_out main0(device const spvVertexData0* spvVertexBuffer0 [[buffer(0)]], uint gl_VertexIndex [[vertex_id]], uint gl_BaseVertex [[base_vertex]], uint gl_InstanceIndex [[instance_id]], uint gl_BaseInstance [[base_instance]])
{
main0_out out = {};
main0_in in = spvLoadVertex(spvVertexBuffer0[gl_VertexIndex]);
out.gl_Position = in.aVertex;
out.vNormal = in.aNormal;
return out;
}

48 changes: 48 additions & 0 deletions reference/shaders-msl/vert/pack-align.vertex-loader.msl23.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include <metal_stdlib>
#include <simd/simd.h>

using namespace metal;

struct UBO
{
float4x4 uMVP;
};

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

struct main0_in
{
float4 aVertex [[attribute(2)]];
float3 aNormal [[attribute(3)]];
};

struct alignas(4) spvVertexData1
{
rgba8unorm<float4> aVertex;
uchar spvPad4;
packed_uchar3 aNormal;
};
static_assert(alignof(spvVertexData1) == 4, "Unexpected alignment");
static_assert(sizeof(spvVertexData1) == 8, "Unexpected size");

main0_in spvLoadVertex(const device spvVertexData1& data1)
{
main0_in out;
out.aVertex = unpack_unorm4x8_to_float(reinterpret_cast<const device uint&>(data1.aVertex));
out.aNormal = float3(data1.aNormal).bgr;
return out;
}

vertex main0_out main0(device const spvVertexData1* spvVertexBuffer1 [[buffer(1)]], constant UBO& _16 [[buffer(0)]], uint gl_VertexIndex [[vertex_id]], uint gl_BaseVertex [[base_vertex]], uint gl_InstanceIndex [[instance_id]], uint gl_BaseInstance [[base_instance]])
{
main0_out out = {};
main0_in in = spvLoadVertex(spvVertexBuffer1[gl_InstanceIndex]);
out.gl_Position = _16.uMVP * in.aVertex;
out.vNormal = in.aNormal;
return out;
}

Loading

0 comments on commit f2889b2

Please sign in to comment.