Sphere Rendering with Metal Mesh Shaders

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:

  1. 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?

  2. 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.

  1. 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);
    }

}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật