gloo
gloo

Reputation: 2580

Metal shader not working as expected

If I run the following vertex shader in Metal/Swift I get a nice rectangle on the screen:

vertex Vertex vertexShader(uint k [[ vertex_id ]],
                           device float2* position [[buffer(1)]]){
    Vertex output;
    float2 pos = position[k];
    output.position = float4(pos,0,1);
    return output;
};
//position [0.0, 0.0, 0.5, 0.0, 0.0, 0.5, 0.5, 0.5]
//indexList [0, 1, 2, 2, 1, 3] 

Now if I run the following I get a blank screen:

vertex Vertex vertexShader(uint k [[ vertex_id ]],
                           device float3* position [[buffer(1)]]){
    Vertex output;
    float3 pos = position[k];
    output.position = float4(pos,1);
    return output;
};
//position [0.0, 0.0, 0.0, 0.5, 0.0, 0.0, 0.0, 0.5, 0.0, 0.5, 0.5, 0.0]
//indexList [0, 1, 2, 2, 1, 3] 

It seems to me these should produce identical results. What am I missing?

Upvotes: 1

Views: 573

Answers (1)

Ken Thomases
Ken Thomases

Reputation: 90521

How exactly are you filling the buffer associated with index 1 in your app code?

I suspect you're just supplying an array of floats. Well, float3 is not packed. Its layout is not the same as 3 floats. There's padding. Its size is actually the same as float4 or 4 floats.

Probably, the simplest fix is to declare position as a pointer to packed_float3.

Upvotes: 1

Related Questions