metal: only 4 (instead of 5) vertices are needed to draw rectangles and textures, and only 3 (instead of 5) vertices are needed to cover the screen for the 'full-screen quad' when clearing.
authorAlex Szpakowski <slime73@gmail.com>
Sat, 30 Dec 2017 18:48:07 -0400
changeset 11788d16d4c766611
parent 11787 feaf1b45ae88
child 11789 01f9851b2c9d
metal: only 4 (instead of 5) vertices are needed to draw rectangles and textures, and only 3 (instead of 5) vertices are needed to cover the screen for the 'full-screen quad' when clearing.
src/render/metal/SDL_render_metal.m
     1.1 --- a/src/render/metal/SDL_render_metal.m	Sat Dec 30 14:40:39 2017 -0500
     1.2 +++ b/src/render/metal/SDL_render_metal.m	Sat Dec 30 18:48:07 2017 -0400
     1.3 @@ -324,7 +324,7 @@
     1.4      data.mtlpipelinecopylinear = [[NSMutableArray alloc] init];
     1.5      MakePipelineStates(data, data.mtlpipelinecopylinear, @"SDL texture pipeline (linear)", @"SDL_Copy_vertex", @"SDL_Copy_fragment_linear");
     1.6  
     1.7 -    static const float clearverts[] = { -1, -1, -1, 1, 1, 1, 1, -1, -1, -1 };
     1.8 +    static const float clearverts[] = { -1, -1,  -1, 3,  3, -1 };
     1.9      data.mtlbufclearverts = [data.mtldevice newBufferWithBytes:clearverts length:sizeof(clearverts) options:MTLResourceCPUCacheModeWriteCombined];
    1.10      data.mtlbufclearverts.label = @"SDL_RenderClear vertices";
    1.11  
    1.12 @@ -562,12 +562,12 @@
    1.13      viewport.znear = 0.0;
    1.14      viewport.zfar = 1.0;
    1.15  
    1.16 -    // Draw as if we're doing a simple filled rect to the screen now.
    1.17 +    // Draw a simple filled fullscreen triangle now.
    1.18      [data.mtlcmdencoder setViewport:viewport];
    1.19      [data.mtlcmdencoder setRenderPipelineState:ChoosePipelineState(data.mtlpipelineprims, renderer->blendMode)];
    1.20      [data.mtlcmdencoder setVertexBuffer:data.mtlbufclearverts offset:0 atIndex:0];
    1.21      [data.mtlcmdencoder setFragmentBytes:color length:sizeof(color) atIndex:0];
    1.22 -    [data.mtlcmdencoder drawPrimitives:MTLPrimitiveTypeTriangleStrip vertexStart:0 vertexCount:5];
    1.23 +    [data.mtlcmdencoder drawPrimitives:MTLPrimitiveTypeTriangle vertexStart:0 vertexCount:3];
    1.24  
    1.25      // reset the viewport for the rest of our usual drawing work...
    1.26      viewport.originX = renderer->viewport.x;
    1.27 @@ -675,13 +675,12 @@
    1.28          const float verts[] = {
    1.29              normx(rects->x, w), normy(rects->y + rects->h, h),
    1.30              normx(rects->x, w), normy(rects->y, h),
    1.31 -            normx(rects->x + rects->w, w), normy(rects->y, h),
    1.32 -            normx(rects->x, w), normy(rects->y + rects->h, h),
    1.33 -            normx(rects->x + rects->w, w), normy(rects->y + rects->h, h)
    1.34 +            normx(rects->x + rects->w, w), normy(rects->y + rects->h, h),
    1.35 +            normx(rects->x + rects->w, w), normy(rects->y, h)
    1.36          };
    1.37  
    1.38          [data.mtlcmdencoder setVertexBytes:verts length:sizeof(verts) atIndex:0];
    1.39 -        [data.mtlcmdencoder drawPrimitives:MTLPrimitiveTypeTriangleStrip vertexStart:0 vertexCount:5];
    1.40 +        [data.mtlcmdencoder drawPrimitives:MTLPrimitiveTypeTriangleStrip vertexStart:0 vertexCount:4];
    1.41      }
    1.42  
    1.43      return 0;
    1.44 @@ -702,17 +701,15 @@
    1.45      const float xy[] = {
    1.46          normx(dstrect->x, w), normy(dstrect->y + dstrect->h, h),
    1.47          normx(dstrect->x, w), normy(dstrect->y, h),
    1.48 -        normx(dstrect->x + dstrect->w, w), normy(dstrect->y, h),
    1.49 -        normx(dstrect->x, w), normy(dstrect->y + dstrect->h, h),
    1.50 -        normx(dstrect->x + dstrect->w, w), normy(dstrect->y + dstrect->h, h)
    1.51 +        normx(dstrect->x + dstrect->w, w), normy(dstrect->y + dstrect->h, h),
    1.52 +        normx(dstrect->x + dstrect->w, w), normy(dstrect->y, h)
    1.53      };
    1.54  
    1.55      const float uv[] = {
    1.56          normtex(srcrect->x, texw), normtex(srcrect->y + srcrect->h, texh),
    1.57          normtex(srcrect->x, texw), normtex(srcrect->y, texh),
    1.58 -        normtex(srcrect->x + srcrect->w, texw), normtex(srcrect->y, texh),
    1.59 -        normtex(srcrect->x, texw), normtex(srcrect->y + srcrect->h, texh),
    1.60 -        normtex(srcrect->x + srcrect->w, texw), normtex(srcrect->y + srcrect->h, texh)
    1.61 +        normtex(srcrect->x + srcrect->w, texw), normtex(srcrect->y + srcrect->h, texh),
    1.62 +        normtex(srcrect->x + srcrect->w, texw), normtex(srcrect->y, texh)
    1.63      };
    1.64  
    1.65      float color[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
    1.66 @@ -728,7 +725,7 @@
    1.67      [data.mtlcmdencoder setFragmentTexture:texturedata.mtltexture atIndex:0];
    1.68      [data.mtlcmdencoder setVertexBytes:xy length:sizeof(xy) atIndex:0];
    1.69      [data.mtlcmdencoder setVertexBytes:uv length:sizeof(uv) atIndex:1];
    1.70 -    [data.mtlcmdencoder drawPrimitives:MTLPrimitiveTypeTriangleStrip vertexStart:0 vertexCount:5];
    1.71 +    [data.mtlcmdencoder drawPrimitives:MTLPrimitiveTypeTriangleStrip vertexStart:0 vertexCount:4];
    1.72  
    1.73      return 0;
    1.74  }}