Fixed metal renderer pixel centers when drawing
authorSam Lantinga <slouken@libsdl.org>
Tue, 02 Jan 2018 14:11:10 -0800
changeset 11806aa740c67a96a
parent 11805 ccf4e7af3583
child 11807 6b3d9e08c586
Fixed metal renderer pixel centers when drawing
src/render/metal/SDL_render_metal.m
     1.1 --- a/src/render/metal/SDL_render_metal.m	Mon Jan 01 23:06:08 2018 -0400
     1.2 +++ b/src/render/metal/SDL_render_metal.m	Tue Jan 02 14:11:10 2018 -0800
     1.3 @@ -816,6 +816,18 @@
     1.4      return 0;
     1.5  }}
     1.6  
     1.7 +// adjust pixel center for x and y coordinates
     1.8 +static inline float
     1.9 +adjustx(const float val)
    1.10 +{
    1.11 +	return (val + 0.5f);
    1.12 +}
    1.13 +static inline float
    1.14 +adjusty(const float val)
    1.15 +{
    1.16 +	return (val - 0.5f);
    1.17 +}
    1.18 +
    1.19  // normalize a value from 0.0f to len into 0.0f to 1.0f.
    1.20  static inline float
    1.21  normtex(const float _val, const float len)
    1.22 @@ -830,7 +842,12 @@
    1.23  { @autoreleasepool {
    1.24      METAL_ActivateRenderer(renderer);
    1.25  
    1.26 -    const size_t vertlen = sizeof(SDL_FPoint) * count;
    1.27 +    const size_t vertlen = (sizeof (float) * 2) * count;
    1.28 +    float *verts = SDL_malloc(vertlen);
    1.29 +    if (!verts) {
    1.30 +        return SDL_OutOfMemory();
    1.31 +    }
    1.32 +
    1.33      METAL_RenderData *data = (__bridge METAL_RenderData *) renderer->driverdata;
    1.34  
    1.35      // !!! FIXME: render color should live in a dedicated uniform buffer.
    1.36 @@ -838,8 +855,17 @@
    1.37  
    1.38      [data.mtlcmdencoder setRenderPipelineState:ChoosePipelineState(data, data.mtlpipelineprims, renderer->blendMode)];
    1.39      [data.mtlcmdencoder setFragmentBytes:color length:sizeof(color) atIndex:0];
    1.40 -    [data.mtlcmdencoder setVertexBytes:points length:vertlen atIndex:0];
    1.41 +
    1.42 +    float *ptr = verts;
    1.43 +    for (int i = 0; i < count; i++, points++) {
    1.44 +        *ptr = adjustx(points->x); ptr++;
    1.45 +        *ptr = adjusty(points->y); ptr++;
    1.46 +    }
    1.47 +
    1.48 +    [data.mtlcmdencoder setVertexBytes:verts length:vertlen atIndex:0];
    1.49      [data.mtlcmdencoder drawPrimitives:primtype vertexStart:0 vertexCount:count];
    1.50 +
    1.51 +    SDL_free(verts);
    1.52      return 0;
    1.53  }}
    1.54  
    1.55 @@ -871,10 +897,10 @@
    1.56          if ((rects->w <= 0.0f) || (rects->h <= 0.0f)) continue;
    1.57  
    1.58          const float verts[] = {
    1.59 -            rects->x, rects->y + rects->h,
    1.60 -            rects->x, rects->y,
    1.61 -            rects->x + rects->w, rects->y + rects->h,
    1.62 -            rects->x + rects->w, rects->y,
    1.63 +            adjustx(rects->x), adjusty(rects->y + rects->h),
    1.64 +            adjustx(rects->x), adjusty(rects->y),
    1.65 +            adjustx(rects->x + rects->w), adjusty(rects->y + rects->h),
    1.66 +            adjustx(rects->x + rects->w), adjusty(rects->y)
    1.67          };
    1.68  
    1.69          [data.mtlcmdencoder setVertexBytes:verts length:sizeof(verts) atIndex:0];
    1.70 @@ -895,10 +921,10 @@
    1.71      const float texh = (float) texturedata.mtltexture.height;
    1.72  
    1.73      const float xy[] = {
    1.74 -        dstrect->x, dstrect->y + dstrect->h,
    1.75 -        dstrect->x, dstrect->y,
    1.76 -        dstrect->x + dstrect->w, dstrect->y + dstrect->h,
    1.77 -        dstrect->x + dstrect->w, dstrect->y
    1.78 +        adjustx(dstrect->x), adjusty(dstrect->y + dstrect->h),
    1.79 +        adjustx(dstrect->x), adjusty(dstrect->y),
    1.80 +        adjustx(dstrect->x + dstrect->w), adjusty(dstrect->y + dstrect->h),
    1.81 +        adjustx(dstrect->x + dstrect->w), adjusty(dstrect->y)
    1.82      };
    1.83  
    1.84      const float uv[] = {
    1.85 @@ -965,10 +991,10 @@
    1.86      };
    1.87  
    1.88      const float xy[] = {
    1.89 -        -center->x, dstrect->h - center->y,
    1.90 -        -center->x, -center->y,
    1.91 -        dstrect->w - center->x, dstrect->h - center->y,
    1.92 -        dstrect->w - center->x, -center->y,
    1.93 +        adjustx(-center->x), adjusty(dstrect->h - center->y),
    1.94 +        adjustx(-center->x), adjusty(-center->y),
    1.95 +        adjustx(dstrect->w - center->x), adjusty(dstrect->h - center->y),
    1.96 +        adjustx(dstrect->w - center->x), adjusty(-center->y)
    1.97      };
    1.98  
    1.99      {