metal: SDL_RenderReadPixels on macOS synchronizes the render target's texture data if it's managed, before reading from it.
authorAlex Szpakowski <slime73@gmail.com>
Sun, 04 Nov 2018 14:31:56 -0400
changeset 1239480f861112154
parent 12393 c7b7abeab424
child 12395 c1443b4dea7f
metal: SDL_RenderReadPixels on macOS synchronizes the render target's texture data if it's managed, before reading from it.
src/render/metal/SDL_render_metal.m
     1.1 --- a/src/render/metal/SDL_render_metal.m	Sun Nov 04 12:31:02 2018 -0400
     1.2 +++ b/src/render/metal/SDL_render_metal.m	Sun Nov 04 14:31:56 2018 -0400
     1.3 @@ -1196,14 +1196,28 @@
     1.4      METAL_RenderData *data = (__bridge METAL_RenderData *) renderer->driverdata;
     1.5      METAL_ActivateRenderCommandEncoder(renderer, MTLLoadActionLoad, NULL);
     1.6  
     1.7 -    // Commit any current command buffer, and waitUntilCompleted, so any output is ready to be read.
     1.8      [data.mtlcmdencoder endEncoding];
     1.9 +    id<MTLTexture> mtltexture = data.mtlpassdesc.colorAttachments[0].texture;
    1.10 +
    1.11 +#ifdef __MACOSX__
    1.12 +    /* on macOS with managed-storage textures, we need to tell the driver to
    1.13 +     * update the CPU-side copy of the texture data.
    1.14 +     * NOTE: Currently all of our textures are managed on macOS. We'll need some
    1.15 +     * extra copying for any private textures. */
    1.16 +    if (mtltexture.storageMode == MTLStorageModeManaged) {
    1.17 +        id<MTLBlitCommandEncoder> blit = [data.mtlcmdbuffer blitCommandEncoder];
    1.18 +        [blit synchronizeResource:mtltexture];
    1.19 +        [blit endEncoding];
    1.20 +    }
    1.21 +#endif
    1.22 +
    1.23 +    /* Commit the current command buffer and wait until it's completed, to make
    1.24 +     * sure the GPU has finished rendering to it by the time we read it. */
    1.25      [data.mtlcmdbuffer commit];
    1.26      [data.mtlcmdbuffer waitUntilCompleted];
    1.27      data.mtlcmdencoder = nil;
    1.28      data.mtlcmdbuffer = nil;
    1.29  
    1.30 -    id<MTLTexture> mtltexture = data.mtlpassdesc.colorAttachments[0].texture;
    1.31      MTLRegion mtlregion = MTLRegionMake2D(rect->x, rect->y, rect->w, rect->h);
    1.32  
    1.33      // we only do BGRA8 or RGBA8 at the moment, so 4 will do.