Navigation Menu

Skip to content

Commit

Permalink
Fixed metal renderer pixel centers when drawing
Browse files Browse the repository at this point in the history
  • Loading branch information
slouken committed Jan 2, 2018
1 parent 07f08b4 commit fa86807
Showing 1 changed file with 40 additions and 14 deletions.
54 changes: 40 additions & 14 deletions src/render/metal/SDL_render_metal.m
Expand Up @@ -816,6 +816,18 @@ - (void)dealloc
return 0;
}}

// adjust pixel center for x and y coordinates
static inline float
adjustx(const float val)
{
return (val + 0.5f);
}
static inline float
adjusty(const float val)
{
return (val - 0.5f);
}

// normalize a value from 0.0f to len into 0.0f to 1.0f.
static inline float
normtex(const float _val, const float len)
Expand All @@ -830,16 +842,30 @@ - (void)dealloc
{ @autoreleasepool {
METAL_ActivateRenderer(renderer);

const size_t vertlen = sizeof(SDL_FPoint) * count;
const size_t vertlen = (sizeof (float) * 2) * count;
float *verts = SDL_malloc(vertlen);
if (!verts) {
return SDL_OutOfMemory();
}

METAL_RenderData *data = (__bridge METAL_RenderData *) renderer->driverdata;

// !!! FIXME: render color should live in a dedicated uniform buffer.
const float color[4] = { ((float)renderer->r) / 255.0f, ((float)renderer->g) / 255.0f, ((float)renderer->b) / 255.0f, ((float)renderer->a) / 255.0f };

[data.mtlcmdencoder setRenderPipelineState:ChoosePipelineState(data, data.mtlpipelineprims, renderer->blendMode)];
[data.mtlcmdencoder setFragmentBytes:color length:sizeof(color) atIndex:0];
[data.mtlcmdencoder setVertexBytes:points length:vertlen atIndex:0];

float *ptr = verts;
for (int i = 0; i < count; i++, points++) {
*ptr = adjustx(points->x); ptr++;
*ptr = adjusty(points->y); ptr++;
}

[data.mtlcmdencoder setVertexBytes:verts length:vertlen atIndex:0];
[data.mtlcmdencoder drawPrimitives:primtype vertexStart:0 vertexCount:count];

SDL_free(verts);
return 0;
}}

Expand Down Expand Up @@ -871,10 +897,10 @@ - (void)dealloc
if ((rects->w <= 0.0f) || (rects->h <= 0.0f)) continue;

const float verts[] = {
rects->x, rects->y + rects->h,
rects->x, rects->y,
rects->x + rects->w, rects->y + rects->h,
rects->x + rects->w, rects->y,
adjustx(rects->x), adjusty(rects->y + rects->h),
adjustx(rects->x), adjusty(rects->y),
adjustx(rects->x + rects->w), adjusty(rects->y + rects->h),
adjustx(rects->x + rects->w), adjusty(rects->y)
};

[data.mtlcmdencoder setVertexBytes:verts length:sizeof(verts) atIndex:0];
Expand All @@ -895,10 +921,10 @@ - (void)dealloc
const float texh = (float) texturedata.mtltexture.height;

const float xy[] = {
dstrect->x, dstrect->y + dstrect->h,
dstrect->x, dstrect->y,
dstrect->x + dstrect->w, dstrect->y + dstrect->h,
dstrect->x + dstrect->w, dstrect->y
adjustx(dstrect->x), adjusty(dstrect->y + dstrect->h),
adjustx(dstrect->x), adjusty(dstrect->y),
adjustx(dstrect->x + dstrect->w), adjusty(dstrect->y + dstrect->h),
adjustx(dstrect->x + dstrect->w), adjusty(dstrect->y)
};

const float uv[] = {
Expand Down Expand Up @@ -965,10 +991,10 @@ - (void)dealloc
};

const float xy[] = {
-center->x, dstrect->h - center->y,
-center->x, -center->y,
dstrect->w - center->x, dstrect->h - center->y,
dstrect->w - center->x, -center->y,
adjustx(-center->x), adjusty(dstrect->h - center->y),
adjustx(-center->x), adjusty(-center->y),
adjustx(dstrect->w - center->x), adjusty(dstrect->h - center->y),
adjustx(dstrect->w - center->x), adjusty(-center->y)
};

{
Expand Down

0 comments on commit fa86807

Please sign in to comment.