Fixed pixel positioning and size for the Metal renderer

This commit is contained in:
Sam Lantinga 2017-12-09 12:58:41 -08:00
parent 7dac177cd1
commit f55c998891
4 changed files with 1744 additions and 1713 deletions

View File

@ -96,8 +96,12 @@ SDL_RenderDriver METAL_RenderDriver = {
// (the weakest GPU supported by Metal on iOS has 4k texture max, and // (the weakest GPU supported by Metal on iOS has 4k texture max, and
// other models might be 2x or 4x more. On macOS, it's 16k across the // other models might be 2x or 4x more. On macOS, it's 16k across the
// board right now.) // board right now.)
4096, #ifdef __MACOSX__
4096} 16384, 16384
#else
4096, 4096
#endif
}
}; };
@interface METAL_RenderData : NSObject @interface METAL_RenderData : NSObject
@ -305,7 +309,7 @@ METAL_CreateRenderer(SDL_Window * window, Uint32 flags)
data.mtllibrary.label = @"SDL Metal renderer shader library"; data.mtllibrary.label = @"SDL Metal renderer shader library";
data.mtlpipelineprims = [[NSMutableArray alloc] init]; data.mtlpipelineprims = [[NSMutableArray alloc] init];
MakePipelineStates(data, data.mtlpipelineprims, @"SDL primitives pipeline", @"SDL_Simple_vertex", @"SDL_Simple_fragment"); MakePipelineStates(data, data.mtlpipelineprims, @"SDL primitives pipeline", @"SDL_Solid_vertex", @"SDL_Solid_fragment");
data.mtlpipelinecopy = [[NSMutableArray alloc] init]; data.mtlpipelinecopy = [[NSMutableArray alloc] init];
MakePipelineStates(data, data.mtlpipelinecopy, @"SDL_RenderCopy pipeline", @"SDL_Copy_vertex", @"SDL_Copy_fragment"); MakePipelineStates(data, data.mtlpipelinecopy, @"SDL_RenderCopy pipeline", @"SDL_Copy_vertex", @"SDL_Copy_fragment");
@ -559,17 +563,18 @@ METAL_RenderClear(SDL_Renderer * renderer)
// normalize a value from 0.0f to len into -1.0f to 1.0f. // normalize a value from 0.0f to len into -1.0f to 1.0f.
static inline float static inline float
norm(const float _val, const float len) normx(const float _val, const float len)
{ {
const float val = (_val < 0.0f) ? 0.0f : (_val > len) ? len : _val; const float val = (_val < 0.0f) ? 0.0f : (_val > len) ? len : _val;
return ((val / len) * 2.0f) - 1.0f; // !!! FIXME: is this right? return (((val + 0.5f) / len) * 2.0f) - 1.0f;
} }
// normalize a value from 0.0f to len into -1.0f to 1.0f. // normalize a value from 0.0f to len into -1.0f to 1.0f.
static inline float static inline float
normy(const float _val, const float len) normy(const float _val, const float len)
{ {
return norm(len - ((_val < 0.0f) ? 0.0f : (_val > len) ? len : _val), len); const float val = (_val <= 0.0f) ? len : (_val >= len) ? 0.0f : (len - _val);
return (((val - 0.5f) / len) * 2.0f) - 1.0f;
} }
// normalize a value from 0.0f to len into 0.0f to 1.0f. // normalize a value from 0.0f to len into 0.0f to 1.0f.
@ -577,7 +582,7 @@ static inline float
normtex(const float _val, const float len) normtex(const float _val, const float len)
{ {
const float val = (_val < 0.0f) ? 0.0f : (_val > len) ? len : _val; const float val = (_val < 0.0f) ? 0.0f : (_val > len) ? len : _val;
return (val / len); return ((val + 0.5f) / len);
} }
static int static int
@ -606,7 +611,7 @@ DrawVerts(SDL_Renderer * renderer, const SDL_FPoint * points, int count,
// !!! FIXME: we can convert this in the shader. This will save the malloc and for-loop, but we still need to upload. // !!! FIXME: we can convert this in the shader. This will save the malloc and for-loop, but we still need to upload.
float *ptr = verts; float *ptr = verts;
for (int i = 0; i < count; i++, points++) { for (int i = 0; i < count; i++, points++) {
*ptr = norm(points->x, w); ptr++; *ptr = normx(points->x, w); ptr++;
*ptr = normy(points->y, h); ptr++; *ptr = normy(points->y, h); ptr++;
} }
@ -648,11 +653,11 @@ METAL_RenderFillRects(SDL_Renderer * renderer, const SDL_FRect * rects, int coun
if ((rects->w <= 0.0f) || (rects->h <= 0.0f)) continue; if ((rects->w <= 0.0f) || (rects->h <= 0.0f)) continue;
const float verts[] = { const float verts[] = {
norm(rects->x, w), normy(rects->y + rects->h, h), normx(rects->x, w), normy(rects->y + rects->h, h),
norm(rects->x, w), normy(rects->y, h), normx(rects->x, w), normy(rects->y, h),
norm(rects->x + rects->w, w), normy(rects->y, h), normx(rects->x + rects->w, w), normy(rects->y, h),
norm(rects->x, w), normy(rects->y + rects->h, h), normx(rects->x, w), normy(rects->y + rects->h, h),
norm(rects->x + rects->w, w), normy(rects->y + rects->h, h) normx(rects->x + rects->w, w), normy(rects->y + rects->h, h)
}; };
[data.mtlcmdencoder setVertexBytes:verts length:sizeof(verts) atIndex:0]; [data.mtlcmdencoder setVertexBytes:verts length:sizeof(verts) atIndex:0];
@ -675,11 +680,11 @@ METAL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
const float texh = (float) mtltexture.height; const float texh = (float) mtltexture.height;
const float xy[] = { const float xy[] = {
norm(dstrect->x, w), normy(dstrect->y + dstrect->h, h), normx(dstrect->x, w), normy(dstrect->y + dstrect->h, h),
norm(dstrect->x, w), normy(dstrect->y, h), normx(dstrect->x, w), normy(dstrect->y, h),
norm(dstrect->x + dstrect->w, w), normy(dstrect->y, h), normx(dstrect->x + dstrect->w, w), normy(dstrect->y, h),
norm(dstrect->x, w), normy(dstrect->y + dstrect->h, h), normx(dstrect->x, w), normy(dstrect->y + dstrect->h, h),
norm(dstrect->x + dstrect->w, w), normy(dstrect->y + dstrect->h, h) normx(dstrect->x + dstrect->w, w), normy(dstrect->y + dstrect->h, h)
}; };
const float uv[] = { const float uv[] = {
@ -768,7 +773,9 @@ static void
METAL_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture) METAL_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
{ @autoreleasepool { { @autoreleasepool {
id<MTLTexture> mtltexture = CFBridgingRelease(texture->driverdata); id<MTLTexture> mtltexture = CFBridgingRelease(texture->driverdata);
#if !__has_feature(objc_arc) #if __has_feature(objc_arc)
mtltexture = nil;
#else
[mtltexture release]; [mtltexture release];
#endif #endif
texture->driverdata = NULL; texture->driverdata = NULL;

View File

@ -2,12 +2,21 @@
using namespace metal; using namespace metal;
vertex float4 SDL_Simple_vertex(constant float2 *position [[buffer(0)]], uint vid [[vertex_id]]) struct SolidVertex
{ {
return float4(position[vid].x, position[vid].y, 0.0f, 1.0f); float4 position [[position]];
float pointSize [[point_size]];
};
vertex SolidVertex SDL_Solid_vertex(constant float2 *position [[buffer(0)]], uint vid [[vertex_id]])
{
SolidVertex v;
v.position = float4(position[vid].x, position[vid].y, 0.0f, 1.0f);
v.pointSize = 0.5f;
return v;
} }
fragment float4 SDL_Simple_fragment(constant float4 &col [[buffer(0)]]) fragment float4 SDL_Solid_fragment(constant float4 &col [[buffer(0)]])
{ {
return col; return col;
} }
@ -20,10 +29,10 @@ struct CopyVertex
vertex CopyVertex SDL_Copy_vertex(constant float2 *position [[buffer(0)]], constant float2 *texcoords [[buffer(1)]], uint vid [[vertex_id]]) vertex CopyVertex SDL_Copy_vertex(constant float2 *position [[buffer(0)]], constant float2 *texcoords [[buffer(1)]], uint vid [[vertex_id]])
{ {
CopyVertex retval; CopyVertex v;
retval.position = float4(position[vid].x, position[vid].y, 0.0f, 1.0f); v.position = float4(position[vid].x, position[vid].y, 0.0f, 1.0f);
retval.texcoord = texcoords[vid]; v.texcoord = texcoords[vid];
return retval; return v;
} }
fragment float4 SDL_Copy_fragment(CopyVertex vert [[stage_in]], constant float4 &col [[buffer(0)]], texture2d<float> tex [[texture(0)]]) fragment float4 SDL_Copy_fragment(CopyVertex vert [[stage_in]], constant float4 &col [[buffer(0)]], texture2d<float> tex [[texture(0)]])

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff