I'm stuck with a problem where and I want to render just two triangles (each one is stored in separated buffer) and Metal API rejects attempts to render second vertex buffer. I suspect this is about alignment. The assertion message is failed assertion `(length - offset)(0) must be >= 32 at buffer binding at index 0 for vertexArray[0].' Here the code:
Vertex and constants structs:
struct VertexPositionColor
{
VertexPositionColor(const simd::float4& pos,
const simd::float4& col)
: position(pos), color(col) {}
simd::float4 position;
simd::float4 color;
};
typedef struct
{
simd::float4x4 model_view_projection;
} constants_t;
This is how I store and add new buffers (the function gets called twice):
NSMutableArray<id<MTLBuffer>> *_vertexBuffer;
NSMutableArray<id<MTLBuffer>> *_uniformBuffer;
NSMutableArray<id<MTLBuffer>> *_indexBuffer;
- (void)linkGeometry:(metalGeometry*)geometry
{
[_vertexBuffer addObject:[_device newBufferWithBytes:[geometry vertices]
length:[geometry vertices_length]
options:0]
];
[_uniformBuffer addObject:[_device newBufferWithLength:[geometry uniforms_length]
options:0]
];
RCB::constants_t* guts = (RCB::constants_t*) [[_uniformBuffer lastObject] contents];
guts->model_view_projection = [geometry uniforms]->model_view_projection;
[geometry linkTransformation:(RCB::constants_t *)[[_uniformBuffer lastObject] contents]];
}
And next are the lines where assert fails (the very last one):
[render setVertexBuffer:_vertexBuffer[0] offset:0 atIndex:0];
[render setVertexBuffer:_uniformBuffer[0] offset:0 atIndex:1];
[render drawPrimitives:MTLPrimitiveTypeTriangle vertexStart:0 vertexCount:3];
[render setVertexBuffer:_vertexBuffer[1] offset:3*sizeof(VertexPositionColor) atIndex:0];
[render setVertexBuffer:_uniformBuffer[1] offset:sizeof(constants_t) atIndex:1];
[render drawPrimitives:MTLPrimitiveTypeTriangle vertexStart:3 vertexCount:3];
So, we just make offsets equal to memory size taken by previous buffer. Note that the first triangle will be rendered as expected if we comment the last line out.
Could anyone understand what I've missed? I would really appreciate that.
Regards