utahwithak
utahwithak

Reputation: 6335

Beginning Metal Shader vertex overflow

I have been banging my head on this for days. I am just learning metal and graphics programing, I have generated valid terrain data and the triangles for it, set up some transforms and have things actually showing up on screen (no small feat)! But it will only draw a certain number of triangles and then stop.

After loads of debugging I found that it always stops between buffer offset 0xFFF0 and 0x10008 Which happens to be where uint16's end. I have no idea why this would be the case but that is the only thing I can think of.

My buffer is made up of structs defined as:

struct Vertex {
    float2 position [[ attribute(0) ]];
    float2 tex      [[ attribute(1) ]];
    float shadow    [[ attribute(2) ]];
};

I have my vertex shader set up like:

vertex FragmentIn terrainVertex(constant MetalVertex* verts [[ buffer(0) ]],
                             constant Constants &mvp [[buffer(1)]],
                            constant ModelMatrix &modelMat [[buffer(2)]],
                             uint v_id [[ vertex_id ]]) {
MetalVertex vert = verts[v_id];

FragmentIn outVertex;
outVertex.position = mvp.viewProjectionMatrix * modelMat.modelMatrix * float4(vert.position.x,vert.position.y,0,1);
outVertex.shadow  = vert.shadow;
outVertex.uv = vert.tex;
return outVertex;

}

I have the vertex_id declared as a uint which I assume would likely be 32 bits. I'm not sure why this would be happening.

I have debugged the buffers and all data is there and correct it just stops drawing triangles after that point.

Debugging the triangles it always ends like this:

broken triangle

The data for that broken triangle in the buffer is not what is being drawn. The draw calls are simple drawPrimitives, I've been debugging but they look like:

       renderEncoder.drawPrimitives(type: .triangle, vertexStart: 0, vertexCount: 1500, instanceCount: 500)
    let count = 411
    renderEncoder.drawPrimitives(type: .triangle, vertexStart: 1500, vertexCount: count * 3, instanceCount: count)

Has anyone seen this and know whats going on?

Upvotes: 2

Views: 449

Answers (1)

utahwithak
utahwithak

Reputation: 6335

I have found the solution. Apparently the problem was declaring the MetalVertex* as constant in the vertex shader

Declaring it as device and everything magically works.

Upvotes: 3

Related Questions