Skip to content

Commit

Permalink
metal: CopyEx transform matrix must be aligned for constant buffer ac…
Browse files Browse the repository at this point in the history
…cess.
  • Loading branch information
icculus committed Oct 5, 2018
1 parent 638d624 commit 208c4b0
Showing 1 changed file with 23 additions and 17 deletions.
40 changes: 23 additions & 17 deletions src/render/metal/SDL_render_metal.m
Expand Up @@ -871,14 +871,33 @@ - (void)dealloc
const float rads = (float)(M_PI * (float) angle / 180.0f);
const float c = cosf(rads), s = sinf(rads);
float minu, maxu, minv, maxv;
// !!! FIXME: use an index buffer
const size_t vertlen = (sizeof (float) * 32);
float *verts = (float *) SDL_AllocateRenderVertices(renderer, vertlen, 0, &cmd->data.draw.first);
float *verts;

// cheat and store this offset in (count) because it needs to be aligned in ways other fields don't and we aren't using count otherwise.
verts = (float *) SDL_AllocateRenderVertices(renderer, vertlen, CONSTANT_ALIGN, &cmd->data.draw.count);
if (!verts) {
return -1;
}

cmd->data.draw.count = 1;
// transform matrix
SDL_memset(verts, '\0', sizeof (*verts) * 16);
verts[10] = verts[15] = 1.0f;
// rotation
verts[0] = c;
verts[1] = s;
verts[4] = -s;
verts[5] = c;

// translation
verts[12] = dstrect->x + center->x;
verts[13] = dstrect->y + center->y;

// rest of the vertices don't need the aggressive alignment. Pack them in.
verts = (float *) SDL_AllocateRenderVertices(renderer, vertlen, 0, &cmd->data.draw.first);
if (!verts) {
return -1;
}

minu = normtex(srcquad->x, texw);
maxu = normtex(srcquad->x + srcquad->w, texw);
Expand Down Expand Up @@ -916,19 +935,6 @@ - (void)dealloc
*(verts++) = maxu;
*(verts++) = minv;

// transform matrix
SDL_memset(verts, '\0', sizeof (*verts) * 16);
verts[10] = verts[15] = 1.0f;
// rotation
verts[0] = c;
verts[1] = s;
verts[4] = -s;
verts[5] = c;

// translation
verts[12] = dstrect->x + center->x;
verts[13] = dstrect->y + center->y;

return 0;
}

Expand Down Expand Up @@ -1172,7 +1178,7 @@ - (void)dealloc

case SDL_RENDERCMD_COPY_EX: {
SetCopyState(renderer, cmd, CONSTANTS_OFFSET_INVALID, mtlbufvertex, &statecache);
[data.mtlcmdencoder setVertexBuffer:mtlbufvertex offset:cmd->data.draw.first+(16*sizeof (float)) atIndex:3]; // transform
[data.mtlcmdencoder setVertexBuffer:mtlbufvertex offset:cmd->data.draw.count atIndex:3]; // transform
[data.mtlcmdencoder drawPrimitives:MTLPrimitiveTypeTriangleStrip vertexStart:0 vertexCount:4];
break;
}
Expand Down

0 comments on commit 208c4b0

Please sign in to comment.