Skip to content

Commit

Permalink
Added support for linear sampling and pixel coordinates in the metal …
Browse files Browse the repository at this point in the history
…renderer
  • Loading branch information
slouken committed Dec 9, 2017
1 parent 1c43705 commit 441d309
Show file tree
Hide file tree
Showing 4 changed files with 1,688 additions and 1,202 deletions.
67 changes: 40 additions & 27 deletions src/render/metal/SDL_render_metal.m
Expand Up @@ -113,7 +113,8 @@ @interface METAL_RenderData : NSObject
@property (nonatomic, retain) id<MTLLibrary> mtllibrary;
@property (nonatomic, retain) id<CAMetalDrawable> mtlbackbuffer;
@property (nonatomic, retain) NSMutableArray *mtlpipelineprims;
@property (nonatomic, retain) NSMutableArray *mtlpipelinecopy;
@property (nonatomic, retain) NSMutableArray *mtlpipelinecopynearest;
@property (nonatomic, retain) NSMutableArray *mtlpipelinecopylinear;
@property (nonatomic, retain) id<MTLBuffer> mtlbufclearverts;
@property (nonatomic, retain) CAMetalLayer *mtllayer;
@property (nonatomic, retain) MTLRenderPassDescriptor *mtlpassdesc;
Expand All @@ -122,6 +123,14 @@ @interface METAL_RenderData : NSObject
@implementation METAL_RenderData
@end

@interface METAL_TextureData : NSObject
@property (nonatomic, retain) id<MTLTexture> mtltexture;
@property (nonatomic, retain) NSMutableArray *mtlpipeline;
@end

@implementation METAL_TextureData
@end

static int
IsMetalAvailable(const SDL_SysWMinfo *syswm)
{
Expand Down Expand Up @@ -310,8 +319,10 @@ @implementation METAL_RenderData

data.mtlpipelineprims = [[NSMutableArray alloc] init];
MakePipelineStates(data, data.mtlpipelineprims, @"SDL primitives pipeline", @"SDL_Solid_vertex", @"SDL_Solid_fragment");
data.mtlpipelinecopy = [[NSMutableArray alloc] init];
MakePipelineStates(data, data.mtlpipelinecopy, @"SDL_RenderCopy pipeline", @"SDL_Copy_vertex", @"SDL_Copy_fragment");
data.mtlpipelinecopynearest = [[NSMutableArray alloc] init];
MakePipelineStates(data, data.mtlpipelinecopynearest, @"SDL texture pipeline (nearest)", @"SDL_Copy_vertex", @"SDL_Copy_fragment_nearest");
data.mtlpipelinecopylinear = [[NSMutableArray alloc] init];
MakePipelineStates(data, data.mtlpipelinecopylinear, @"SDL texture pipeline (linear)", @"SDL_Copy_vertex", @"SDL_Copy_fragment_linear");

static const float clearverts[] = { -1, -1, -1, 1, 1, 1, 1, -1, -1, -1 };
data.mtlbufclearverts = [data.mtldevice newBufferWithBytes:clearverts length:sizeof(clearverts) options:MTLResourceCPUCacheModeWriteCombined];
Expand Down Expand Up @@ -419,7 +430,16 @@ static void METAL_ActivateRenderer(SDL_Renderer * renderer)
return SDL_SetError("Texture allocation failed");
}

texture->driverdata = (void*)CFBridgingRetain(mtltexture);
METAL_TextureData *texturedata = [[METAL_TextureData alloc] init];
const char *hint = SDL_GetHint(SDL_HINT_RENDER_SCALE_QUALITY);
if (!hint || *hint == '0' || SDL_strcasecmp(hint, "nearest") == 0) {
texturedata.mtlpipeline = data.mtlpipelinecopynearest;
} else {
texturedata.mtlpipeline = data.mtlpipelinecopylinear;
}
texturedata.mtltexture = mtltexture;

texture->driverdata = (void*)CFBridgingRetain(texturedata);

return 0;
}}
Expand All @@ -432,7 +452,7 @@ static void METAL_ActivateRenderer(SDL_Renderer * renderer)
// !!! FIXME: Maybe move this off to a thread that marks the texture as uploaded and only stall the main thread if we try to
// !!! FIXME: use this texture before the marking is done? Is it worth it? Or will we basically always be uploading a bunch of
// !!! FIXME: stuff way ahead of time and/or using it immediately after upload?
id<MTLTexture> mtltexture = (__bridge id<MTLTexture>) texture->driverdata;
id<MTLTexture> mtltexture = ((__bridge METAL_TextureData *)texture->driverdata).mtltexture;
[mtltexture replaceRegion:MTLRegionMake2D(rect->x, rect->y, rect->w, rect->h) mipmapLevel:0 withBytes:pixels bytesPerRow:pitch];
return 0;
}}
Expand Down Expand Up @@ -471,7 +491,7 @@ static void METAL_ActivateRenderer(SDL_Renderer * renderer)
[data.mtlcmdencoder endEncoding];
[data.mtlcmdbuffer commit];

id<MTLTexture> mtltexture = texture ? (__bridge id<MTLTexture>) texture->driverdata : data.mtlbackbuffer.texture;
id<MTLTexture> mtltexture = texture ? ((__bridge METAL_TextureData *)texture->driverdata).mtltexture : data.mtlbackbuffer.texture;
data.mtlpassdesc.colorAttachments[0].texture = mtltexture;
// !!! FIXME: this can be MTLLoadActionDontCare for textures (not the backbuffer) if SDL doesn't guarantee the texture contents should survive.
data.mtlpassdesc.colorAttachments[0].loadAction = MTLLoadActionLoad;
Expand Down Expand Up @@ -577,14 +597,6 @@ static void METAL_ActivateRenderer(SDL_Renderer * renderer)
return (((val - 0.5f) / len) * 2.0f) - 1.0f;
}

// normalize a value from 0.0f to len into 0.0f to 1.0f.
static inline float
normtex(const float _val, const float len)
{
const float val = (_val < 0.0f) ? 0.0f : (_val > len) ? len : _val;
return ((val + 0.5f) / len);
}

static int
DrawVerts(SDL_Renderer * renderer, const SDL_FPoint * points, int count,
const MTLPrimitiveType primtype)
Expand Down Expand Up @@ -673,11 +685,11 @@ static void METAL_ActivateRenderer(SDL_Renderer * renderer)
{ @autoreleasepool {
METAL_ActivateRenderer(renderer);
METAL_RenderData *data = (__bridge METAL_RenderData *) renderer->driverdata;
id<MTLTexture> mtltexture = (__bridge id<MTLTexture>) texture->driverdata;
METAL_TextureData *texturedata = (__bridge METAL_TextureData *)texture->driverdata;
const float w = (float) data.mtlpassdesc.colorAttachments[0].texture.width;
const float h = (float) data.mtlpassdesc.colorAttachments[0].texture.height;
const float texw = (float) mtltexture.width;
const float texh = (float) mtltexture.height;
const float texw = (float) texturedata.mtltexture.width;
const float texh = (float) texturedata.mtltexture.height;

const float xy[] = {
normx(dstrect->x, w), normy(dstrect->y + dstrect->h, h),
Expand All @@ -688,11 +700,11 @@ static void METAL_ActivateRenderer(SDL_Renderer * renderer)
};

const float uv[] = {
normtex(srcrect->x, texw), normtex(srcrect->y + srcrect->h, texh),
normtex(srcrect->x, texw), normtex(srcrect->y, texh),
normtex(srcrect->x + srcrect->w, texw), normtex(srcrect->y, texh),
normtex(srcrect->x, texw), normtex(srcrect->y + srcrect->h, texh),
normtex(srcrect->x + srcrect->w, texw), normtex(srcrect->y + srcrect->h, texh)
srcrect->x, srcrect->y + srcrect->h,
srcrect->x, srcrect->y,
srcrect->x + srcrect->w, srcrect->y,
srcrect->x, srcrect->y + srcrect->h,
srcrect->x + srcrect->w, srcrect->y + srcrect->h
};

float color[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
Expand All @@ -703,9 +715,9 @@ static void METAL_ActivateRenderer(SDL_Renderer * renderer)
color[3] = ((float)texture->a) / 255.0f;
}

[data.mtlcmdencoder setRenderPipelineState:ChoosePipelineState(data.mtlpipelinecopy, texture->blendMode)];
[data.mtlcmdencoder setRenderPipelineState:ChoosePipelineState(texturedata.mtlpipeline, texture->blendMode)];
[data.mtlcmdencoder setFragmentBytes:color length:sizeof(color) atIndex:0];
[data.mtlcmdencoder setFragmentTexture:mtltexture atIndex:0];
[data.mtlcmdencoder setFragmentTexture:texturedata.mtltexture atIndex:0];
[data.mtlcmdencoder setVertexBytes:xy length:sizeof(xy) atIndex:0];
[data.mtlcmdencoder setVertexBytes:uv length:sizeof(uv) atIndex:1];
[data.mtlcmdencoder drawPrimitives:MTLPrimitiveTypeTriangleStrip vertexStart:0 vertexCount:5];
Expand Down Expand Up @@ -772,11 +784,12 @@ static void METAL_ActivateRenderer(SDL_Renderer * renderer)
static void
METAL_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
{ @autoreleasepool {
id<MTLTexture> mtltexture = CFBridgingRelease(texture->driverdata);
METAL_TextureData *texturedata = CFBridgingRelease(texture->driverdata);
#if __has_feature(objc_arc)
mtltexture = nil;
texturedata = nil;
#else
[mtltexture release];
[texturedata.mtltexture release];
[texturedata release];
#endif
texture->driverdata = NULL;
}}
Expand Down
12 changes: 9 additions & 3 deletions src/render/metal/SDL_shaders_metal.metal
Expand Up @@ -35,9 +35,15 @@ vertex CopyVertex SDL_Copy_vertex(constant float2 *position [[buffer(0)]], const
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_nearest(CopyVertex vert [[stage_in]], constant float4 &col [[buffer(0)]], texture2d<float> tex [[texture(0)]])
{
constexpr sampler samp; // !!! FIXME: linear sampling, etc?
return tex.sample(samp, vert.texcoord) * col;
constexpr sampler s(coord::pixel, address::clamp_to_edge, filter::nearest);
return tex.sample(s, vert.texcoord) * col;
}

fragment float4 SDL_Copy_fragment_linear(CopyVertex vert [[stage_in]], constant float4 &col [[buffer(0)]], texture2d<float> tex [[texture(0)]])
{
constexpr sampler s(coord::pixel, address::clamp_to_edge, filter::linear);
return tex.sample(s, vert.texcoord) * col;
}

0 comments on commit 441d309

Please sign in to comment.