metal: CopyEx transform matrix must be aligned for constant buffer access. SDL-ryan-batching-renderer
authorRyan C. Gordon <icculus@icculus.org>
Thu, 04 Oct 2018 20:21:58 -0400
branchSDL-ryan-batching-renderer
changeset 1229486b7b00ea2b8
parent 12293 d027a83b1386
child 12295 96b57a48057c
metal: CopyEx transform matrix must be aligned for constant buffer access.
src/render/metal/SDL_render_metal.m
     1.1 --- a/src/render/metal/SDL_render_metal.m	Thu Oct 04 20:21:23 2018 -0400
     1.2 +++ b/src/render/metal/SDL_render_metal.m	Thu Oct 04 20:21:58 2018 -0400
     1.3 @@ -871,14 +871,33 @@
     1.4      const float rads = (float)(M_PI * (float) angle / 180.0f);
     1.5      const float c = cosf(rads), s = sinf(rads);
     1.6      float minu, maxu, minv, maxv;
     1.7 -    // !!! FIXME: use an index buffer
     1.8      const size_t vertlen = (sizeof (float) * 32);
     1.9 -    float *verts = (float *) SDL_AllocateRenderVertices(renderer, vertlen, 0, &cmd->data.draw.first);
    1.10 +    float *verts;
    1.11 +
    1.12 +    // 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.
    1.13 +    verts = (float *) SDL_AllocateRenderVertices(renderer, vertlen, CONSTANT_ALIGN, &cmd->data.draw.count);
    1.14      if (!verts) {
    1.15          return -1;
    1.16      }
    1.17  
    1.18 -    cmd->data.draw.count = 1;
    1.19 +    // transform matrix
    1.20 +    SDL_memset(verts, '\0', sizeof (*verts) * 16);
    1.21 +    verts[10] = verts[15] = 1.0f;
    1.22 +    // rotation
    1.23 +    verts[0] = c;
    1.24 +    verts[1] = s;
    1.25 +    verts[4] = -s;
    1.26 +    verts[5] = c;
    1.27 +
    1.28 +    // translation
    1.29 +    verts[12] = dstrect->x + center->x;
    1.30 +    verts[13] = dstrect->y + center->y;
    1.31 +
    1.32 +    // rest of the vertices don't need the aggressive alignment. Pack them in.
    1.33 +    verts = (float *) SDL_AllocateRenderVertices(renderer, vertlen, 0, &cmd->data.draw.first);
    1.34 +    if (!verts) {
    1.35 +        return -1;
    1.36 +    }
    1.37  
    1.38      minu = normtex(srcquad->x, texw);
    1.39      maxu = normtex(srcquad->x + srcquad->w, texw);
    1.40 @@ -916,19 +935,6 @@
    1.41      *(verts++) = maxu;
    1.42      *(verts++) = minv;
    1.43  
    1.44 -    // transform matrix
    1.45 -    SDL_memset(verts, '\0', sizeof (*verts) * 16);
    1.46 -    verts[10] = verts[15] = 1.0f;
    1.47 -    // rotation
    1.48 -    verts[0] = c;
    1.49 -    verts[1] = s;
    1.50 -    verts[4] = -s;
    1.51 -    verts[5] = c;
    1.52 -
    1.53 -    // translation
    1.54 -    verts[12] = dstrect->x + center->x;
    1.55 -    verts[13] = dstrect->y + center->y;
    1.56 -
    1.57      return 0;
    1.58  }
    1.59  
    1.60 @@ -1172,7 +1178,7 @@
    1.61  
    1.62              case SDL_RENDERCMD_COPY_EX: {
    1.63                  SetCopyState(renderer, cmd, CONSTANTS_OFFSET_INVALID, mtlbufvertex, &statecache);
    1.64 -                [data.mtlcmdencoder setVertexBuffer:mtlbufvertex offset:cmd->data.draw.first+(16*sizeof (float)) atIndex:3];  // transform
    1.65 +                [data.mtlcmdencoder setVertexBuffer:mtlbufvertex offset:cmd->data.draw.count atIndex:3];  // transform
    1.66                  [data.mtlcmdencoder drawPrimitives:MTLPrimitiveTypeTriangleStrip vertexStart:0 vertexCount:4];
    1.67                  break;
    1.68              }