I’m currently learning rendering and Metal, and I’m experimenting with different methods to draw a bunch of spheres. I aim to replace my instancing render method with mesh shaders, hoping it might improve performance. However, I’m encountering some challenges and would appreciate any guidance.
Questions:
-
Setting up threadsPerGrid, threadsPerObjectThreadgroup, and
threadsPerMeshThreadgroup:• How should I configure these parameters to optimize performance for my use case? and what does lid, tid do?
-
Mesh Generation Stage:
• Which stage is more appropriate for building my mesh: the [[object]] stage or the [[mesh]] stage?
My current thought process is to calculate the level of detail (LoD) in the object stage and set the segment counts based on the distance for different payloads. However, I’ve seen examples where vertices are set during the object stage, and when I tried generating them in the mesh stage, the performance degraded significantly.
-
Using Mesh Shaders for Quad Generation and Ray-Sphere Intersection:
• If I use mesh shaders to generate quads and then perform ray-sphere intersection for rendering, would this approach offer any performance benefits?
I’ve attached my shader below, and full Xcode project here
Despite my efforts, the performance is not as expected. I suspect there might be inefficiencies in how I’m setting up the mesh shaders or distributing the workload among threads.
Any advice or code on how to correctly do this process or insights into better practices would be greatly appreciated. Thank you!
#include <metal_stdlib>
using namespace metal;
struct Vertex {
float3 position;
float3 normal;
Vertex() {}
Vertex(float3 pos, float3 norm) {
position = pos;
normal = norm;
}
};
struct VertexOut
{
float4 position [[position]];
float3 normal;
};
struct PrimeOut
{
float4 color;
};
struct fragmentIn
{
VertexOut v;
PrimeOut p;
};
struct InstanceConstants {
float4x4 modelMatrix;
float4x4 normalMatrix;
float4 color;
};
constant int stackCount = 10;
constant int sectorCount = 10;
constant float PI = 3.14159265358979323846;
static constexpr constant uint32_t MaxVertexCount = (stackCount + 1) * (sectorCount + 1);
static constexpr constant uint32_t MaxPrimitiveCount = stackCount * sectorCount * 2;
using TriangleMeshType = metal::mesh<VertexOut, PrimeOut, MaxVertexCount, MaxPrimitiveCount, topology::triangle>;
struct MeshPayload {
float4x4 transform;
float4x4 normalMatrix;
float4 color;
float3 cameraPosition;
int lodLevel;
};
[[object]]
void objectStage(object_data MeshPayload& payload [[payload]],
constant InstanceConstants* instance [[buffer(1)]],
constant float4x4 &viewProjectionMatrix [[buffer(2)]],
constant float3 &cameraPosition [[buffer(3)]],
mesh_grid_properties props,
uint index [[thread_position_in_grid]]){
uint threadIndex = index;
// Initialize payload with instance-specific data
payload.transform = viewProjectionMatrix * instance[threadIndex].modelMatrix;
payload.normalMatrix = instance[threadIndex].normalMatrix;
payload.color = instance[threadIndex].color;
// Set the threadgroups per grid
props.set_threadgroups_per_grid(uint3(1, 1, 1));
}
[[mesh]]
void meshStage(TriangleMeshType output,
const object_data MeshPayload& payload [[payload]],
uint lid [[thread_index_in_threadgroup]],
uint tid [[threadgroup_position_in_grid]]){
// Set primitive count
if (lid == 0) {
output.set_primitive_count(MaxPrimitiveCount);
}
// Generate vertices
if (lid < MaxVertexCount) {
int stack = lid / (sectorCount + 1);
int sector = lid % (sectorCount + 1);
float stackAngle = PI / 2 - stack * (PI / stackCount);
float sectorAngle = sector * (2 * PI / sectorCount);
float xy = cos(stackAngle);
float z = sin(stackAngle);
float x = xy * cos(sectorAngle);
float y = xy * sin(sectorAngle);
float3 position = float3(x, y, z);
float3 normal = normalize(position);
VertexOut v;
v.position = payload.transform * float4(position, 1.0);
v.normal = (payload.normalMatrix * float4(normal, 0.0)).xyz;
output.set_vertex(lid, v);
}
// Generate indices
if (lid < MaxPrimitiveCount) {
int primitive = lid;
int stack = primitive / (sectorCount * 2);
int sector = (primitive % (sectorCount * 2)) / 2;
int triIndex = primitive % 2;
int k1 = stack * (sectorCount + 1) + sector;
int k2 = k1 + sectorCount + 1;
uint3 indices;
if (triIndex == 0) {
indices = uint3(k1, k2, k1 + 1);
} else {
indices = uint3(k1 + 1, k2, k2 + 1);
}
output.set_index(primitive * 3, indices.x);
output.set_index(primitive * 3 + 1, indices.y);
output.set_index(primitive * 3 + 2, indices.z);
PrimeOut p;
p.color = payload.color;
output.set_primitive(primitive, p);
}
}
fragment float4 fragmShader(fragmentIn in [[stage_in]])
{
float3 N = normalize(in.v.normal);
float3 L = float3(1,1,1);
float NdotL = 0.75 + 0.25*dot(N, L);
return float4(mix(in.p.color.xyz * NdotL, N*0.5+0.5, 0.2), 1.0f);
}
here’s another stuff I tried
#include <metal_stdlib>
using namespace metal;
struct Vertex {
float3 position;
float3 normal;
// Constructor
Vertex() {}
Vertex(float3 pos, float3 norm) {
position = pos;
normal = norm;
}
};
struct VertexOut
{
float4 position [[position]];
float3 normal;
};
// Per-vertex primitive data.
struct PrimeOut
{
float4 color;
};
struct fragmentIn
{
VertexOut v;
PrimeOut p;
};
struct InstanceConstants {
float4x4 modelMatrix;
float4x4 normalMatrix;
float4 color;
};
constant int stackCount = 10;
constant int sectorCount = 10;
constant float PI = 3.14159265358979323846;
static constexpr constant uint32_t MaxVertexCount = (stackCount + 1) * (sectorCount + 1);
static constexpr constant uint32_t MaxPrimitiveCount = stackCount * sectorCount * 2;
using TriangleMeshType = metal::mesh<VertexOut, PrimeOut, MaxVertexCount, MaxPrimitiveCount, topology::triangle>;
struct MeshPayload {
Vertex vertices[MaxVertexCount];
float4x4 transform;
float4x4 normalMatrix;
float4 color;
uint32_t primitiveCount;
uint32_t vertexCount;
};
[[object]]
void objectStage(object_data MeshPayload& payload [[payload]],
constant InstanceConstants* instance [[buffer(1)]],
constant float4x4 &viewProjectionMatrix [[buffer(2)]],
mesh_grid_properties props,
uint3 positionInGrid [[threadgroup_position_in_grid]]){
uint threadIndex = positionInGrid.x;
// Initialize payload with instance-specific data
payload.transform = viewProjectionMatrix * instance[threadIndex].modelMatrix;
payload.normalMatrix = instance[threadIndex].normalMatrix;
payload.color = instance[threadIndex].color;
payload.vertexCount = MaxVertexCount;
payload.primitiveCount = MaxPrimitiveCount;
// Initialize vertices
float stackStep = PI / stackCount;
float sectorStep = 2 * PI / sectorCount;
float radius = 1.0;
float xy, z;
uint index = 0;
for (int i = 0; i <= stackCount; ++i) {
float stackAngle = PI / 2 - i * stackStep; // starting from pi/2 to -pi/2
xy = radius * cos(stackAngle); // r * cos(u)
z = radius * sin(stackAngle); // r * sin(u)
// add (sectorCount+1) vertices per stack
for (int j = 0; j <= sectorCount; ++j) {
float sectorAngle = j * sectorStep; // starting from 0 to 2pi
// vertex position (x, y, z)
float x = xy * cos(sectorAngle); // r * cos(u) * cos(v)
float y = xy * sin(sectorAngle); // r * cos(u) * sin(v)
payload.vertices[index++] = Vertex(float3(x, y, z), normalize(float3(x, y, z)));
}
}
// Set the threadgroups per grid
props.set_threadgroups_per_grid(uint3(1, 1, 1));
}
[[mesh]]
void meshStage(TriangleMeshType output,
const object_data MeshPayload& payload [[payload]],
uint lid [[thread_index_in_threadgroup]],
uint tid [[threadgroup_position_in_grid]]){
if (lid == 0)
{
output.set_primitive_count(payload.primitiveCount);
int k1, k2;
uint index = 0;
for (int i = 0; i < stackCount; ++i) {
k1 = i * (sectorCount + 1); // beginning of current stack
k2 = k1 + sectorCount + 1; // beginning of next stack
for (int j = 0; j < sectorCount; ++j, ++k1, ++k2) {
// 2 triangles per sector excluding first and last stacks
if (i != 0) {
output.set_index(index++, k1);
output.set_index(index++, k2);
output.set_index(index++, k1 + 1);
}
if (i != (stackCount - 1)) {
output.set_index(index++, k1 + 1);
output.set_index(index++, k2);
output.set_index(index++, k2 + 1);
}
}
}
}
if (lid < payload.vertexCount) {
VertexOut v;
float4 position = float4(payload.vertices[lid].position, 1.0);
v.position = payload.transform * position;
v.normal = (payload.normalMatrix * float4(payload.vertices[lid].normal, 0.0)).xyz;
output.set_vertex(lid, v);
}
if (lid < payload.primitiveCount) {
PrimeOut p;
p.color = payload.color;
output.set_primitive(lid, p);
}
}