metal: use a private instead of managed buffer for the renderer's non-changing constant data.
authorAlex Szpakowski <slime73@gmail.com>
Thu, 04 Jan 2018 22:16:42 -0400
changeset 118184f3cdd0a5768
parent 11817 5724c41db8fd
child 11819 3b71fc7ed851
metal: use a private instead of managed buffer for the renderer's non-changing constant data.

Recommended by Xcode's Metal frame capture analysis.
src/render/metal/SDL_render_metal.m
     1.1 --- a/src/render/metal/SDL_render_metal.m	Thu Jan 04 19:29:33 2018 -0400
     1.2 +++ b/src/render/metal/SDL_render_metal.m	Thu Jan 04 22:16:42 2018 -0400
     1.3 @@ -487,22 +487,25 @@
     1.4  
     1.5      float clearverts[6] = {0.0f, 0.0f,  0.0f, 2.0f,  2.0f, 0.0f};
     1.6  
     1.7 -    MTLResourceOptions constantsopts = 0;
     1.8 -#ifdef __MACOSX__
     1.9 -    constantsopts |= MTLResourceStorageModeManaged;
    1.10 -#endif
    1.11 +    id<MTLBuffer> mtlbufconstantstaging = [data.mtldevice newBufferWithLength:CONSTANTS_LENGTH options:MTLResourceStorageModeShared];
    1.12 +    mtlbufconstantstaging.label = @"SDL constant staging data";
    1.13  
    1.14 -    id<MTLBuffer> mtlbufconstants = [data.mtldevice newBufferWithLength:CONSTANTS_LENGTH options:constantsopts];
    1.15 +    id<MTLBuffer> mtlbufconstants = [data.mtldevice newBufferWithLength:CONSTANTS_LENGTH options:MTLResourceStorageModePrivate];
    1.16      data.mtlbufconstants = mtlbufconstants;
    1.17      data.mtlbufconstants.label = @"SDL constant data";
    1.18  
    1.19 -    char *constantdata = [data.mtlbufconstants contents];
    1.20 +    char *constantdata = [mtlbufconstantstaging contents];
    1.21      SDL_memcpy(constantdata + CONSTANTS_OFFSET_IDENTITY, identitytransform, sizeof(identitytransform));
    1.22      SDL_memcpy(constantdata + CONSTANTS_OFFSET_HALF_PIXEL_TRANSFORM, halfpixeltransform, sizeof(halfpixeltransform));
    1.23      SDL_memcpy(constantdata + CONSTANTS_OFFSET_CLEAR_VERTS, clearverts, sizeof(clearverts));
    1.24 -#ifdef __MACOSX__
    1.25 -    [data.mtlbufconstants didModifyRange:NSMakeRange(0, CONSTANTS_LENGTH)];
    1.26 -#endif
    1.27 +
    1.28 +    id<MTLCommandBuffer> cmdbuffer = [data.mtlcmdqueue commandBuffer];
    1.29 +    id<MTLBlitCommandEncoder> blitcmd = [cmdbuffer blitCommandEncoder];
    1.30 +
    1.31 +    [blitcmd copyFromBuffer:mtlbufconstantstaging sourceOffset:0 toBuffer:data.mtlbufconstants destinationOffset:0 size:CONSTANTS_LENGTH];
    1.32 +
    1.33 +    [blitcmd endEncoding];
    1.34 +    [cmdbuffer commit];
    1.35  
    1.36      // !!! FIXME: force more clears here so all the drawables are sane to start, and our static buffers are definitely flushed.
    1.37