Skip to content

Commit

Permalink
metal: only 4 (instead of 5) vertices are needed to draw rectangles a…
Browse files Browse the repository at this point in the history
…nd textures, and only 3 (instead of 5) vertices are needed to cover the screen for the 'full-screen quad' when clearing.
  • Loading branch information
slime73 committed Dec 30, 2017
1 parent d027f36 commit 49df65c
Showing 1 changed file with 11 additions and 14 deletions.
25 changes: 11 additions & 14 deletions src/render/metal/SDL_render_metal.m
Expand Up @@ -324,7 +324,7 @@ @implementation METAL_TextureData
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 };
static const float clearverts[] = { -1, -1, -1, 3, 3, -1 };
data.mtlbufclearverts = [data.mtldevice newBufferWithBytes:clearverts length:sizeof(clearverts) options:MTLResourceCPUCacheModeWriteCombined];
data.mtlbufclearverts.label = @"SDL_RenderClear vertices";

Expand Down Expand Up @@ -562,12 +562,12 @@ static void METAL_ActivateRenderer(SDL_Renderer * renderer)
viewport.znear = 0.0;
viewport.zfar = 1.0;

// Draw as if we're doing a simple filled rect to the screen now.
// Draw a simple filled fullscreen triangle now.
[data.mtlcmdencoder setViewport:viewport];
[data.mtlcmdencoder setRenderPipelineState:ChoosePipelineState(data.mtlpipelineprims, renderer->blendMode)];
[data.mtlcmdencoder setVertexBuffer:data.mtlbufclearverts offset:0 atIndex:0];
[data.mtlcmdencoder setFragmentBytes:color length:sizeof(color) atIndex:0];
[data.mtlcmdencoder drawPrimitives:MTLPrimitiveTypeTriangleStrip vertexStart:0 vertexCount:5];
[data.mtlcmdencoder drawPrimitives:MTLPrimitiveTypeTriangle vertexStart:0 vertexCount:3];

// reset the viewport for the rest of our usual drawing work...
viewport.originX = renderer->viewport.x;
Expand Down Expand Up @@ -675,13 +675,12 @@ static void METAL_ActivateRenderer(SDL_Renderer * renderer)
const float verts[] = {
normx(rects->x, w), normy(rects->y + rects->h, h),
normx(rects->x, w), normy(rects->y, h),
normx(rects->x + rects->w, w), normy(rects->y, h),
normx(rects->x, w), normy(rects->y + rects->h, h),
normx(rects->x + rects->w, w), normy(rects->y + rects->h, h)
normx(rects->x + rects->w, w), normy(rects->y + rects->h, h),
normx(rects->x + rects->w, w), normy(rects->y, h)
};

[data.mtlcmdencoder setVertexBytes:verts length:sizeof(verts) atIndex:0];
[data.mtlcmdencoder drawPrimitives:MTLPrimitiveTypeTriangleStrip vertexStart:0 vertexCount:5];
[data.mtlcmdencoder drawPrimitives:MTLPrimitiveTypeTriangleStrip vertexStart:0 vertexCount:4];
}

return 0;
Expand All @@ -702,17 +701,15 @@ static void METAL_ActivateRenderer(SDL_Renderer * renderer)
const float xy[] = {
normx(dstrect->x, w), normy(dstrect->y + dstrect->h, h),
normx(dstrect->x, w), normy(dstrect->y, h),
normx(dstrect->x + dstrect->w, w), normy(dstrect->y, h),
normx(dstrect->x, w), normy(dstrect->y + dstrect->h, h),
normx(dstrect->x + dstrect->w, w), normy(dstrect->y + dstrect->h, h)
normx(dstrect->x + dstrect->w, w), normy(dstrect->y + dstrect->h, h),
normx(dstrect->x + dstrect->w, w), normy(dstrect->y, h)
};

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)
normtex(srcrect->x + srcrect->w, texw), normtex(srcrect->y + srcrect->h, texh),
normtex(srcrect->x + srcrect->w, texw), normtex(srcrect->y, texh)
};

float color[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
Expand All @@ -728,7 +725,7 @@ static void METAL_ActivateRenderer(SDL_Renderer * renderer)
[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];
[data.mtlcmdencoder drawPrimitives:MTLPrimitiveTypeTriangleStrip vertexStart:0 vertexCount:4];

return 0;
}}
Expand Down

0 comments on commit 49df65c

Please sign in to comment.