SurvivalMachine
SurvivalMachine

Reputation: 8356

Different vertex formats in a Metal shader

My renderer supports 2 vertex formats:

typedef struct
{
    packed_float3 position;
    packed_float2 texcoord;
    packed_float3 normal;
    packed_float4 tangent;
    packed_float4 color;
} vertex_ptntc;

typedef struct
{
    packed_float3 position;
    packed_float2 texcoord;
    packed_float4 color;
} vertex_ptc;

One of my shader library's vertex shader signature is as follows:

vertex ColorInOut unlit_vertex(device vertex_ptc* vertex_array [[ buffer(0) ]],
                                constant uniforms_t& uniforms [[ buffer(1) ]],
                                unsigned int vid [[ vertex_id ]])

Some of the meshes rendered by this shader will use one format and some will use the other. How do I support both formats? This shader only uses the attributes in vertex_ptc. Do I have to write another vertex shader?

Upvotes: 1

Views: 660

Answers (1)

Bill Hollings
Bill Hollings

Reputation: 2374

When defining the shader function argument as an array of structures (as you're doing), the structure definition in the shader vertex function must match the exact shape and size of the actual structures in the buffer (including padding).

Have you considered defining the input in terms of the [[stage_in]] qualifier, and vertex descriptors? This will allow you to massage the vertex input on a shader-by-shader basis, by using an [[attribute(n)]] qualifier on each element of the structure declared for each shader function. You would define a vertex descriptor for each structure.

Upvotes: 2

Related Questions