Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
metal: Implement SDL_LockTexture for non-YUV textures.
  • Loading branch information
slime73 committed Dec 18, 2018
1 parent c0c8f2d commit ce8c716
Showing 1 changed file with 66 additions and 6 deletions.
72 changes: 66 additions & 6 deletions src/render/metal/SDL_render_metal.m
Expand Up @@ -155,6 +155,9 @@ @interface METAL_TextureData : NSObject
@property (nonatomic, assign) BOOL nv12;
@property (nonatomic, assign) size_t conversionBufferOffset;
@property (nonatomic, assign) BOOL hasdata;

@property (nonatomic, retain) id<MTLBuffer> lockedbuffer;
@property (nonatomic, assign) SDL_Rect lockedrect;
@end

@implementation METAL_TextureData
Expand Down Expand Up @@ -783,15 +786,72 @@ - (void)dealloc
static int
METAL_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * rect, void **pixels, int *pitch)
{
return SDL_Unsupported(); // !!! FIXME: write me
}
{ @autoreleasepool {
METAL_RenderData *data = (__bridge METAL_RenderData *) renderer->driverdata;
METAL_TextureData *texturedata = (__bridge METAL_TextureData *)texture->driverdata;

if (texturedata.yuv || texturedata.nv12) {
/* FIXME: write me */
return SDL_Unsupported();
}

*pitch = SDL_BYTESPERPIXEL(texture->format) * rect->w;

texturedata.lockedrect = *rect;
texturedata.lockedbuffer = [data.mtldevice newBufferWithLength:(*pitch)*rect->h options:MTLResourceStorageModeShared];
if (texturedata.lockedbuffer == nil) {
return SDL_OutOfMemory();
}

*pixels = [texturedata.lockedbuffer contents];

return 0;
}}

static void
METAL_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
{
// !!! FIXME: write me
}
{ @autoreleasepool {
METAL_RenderData *data = (__bridge METAL_RenderData *) renderer->driverdata;
METAL_TextureData *texturedata = (__bridge METAL_TextureData *)texture->driverdata;
SDL_Rect rect = texturedata.lockedrect;

if (texturedata.lockedbuffer == nil) {
return;
}

if (data.mtlcmdencoder != nil) {
[data.mtlcmdencoder endEncoding];
data.mtlcmdencoder = nil;
}

if (data.mtlcmdbuffer == nil) {
data.mtlcmdbuffer = [data.mtlcmdqueue commandBuffer];
}

id<MTLBlitCommandEncoder> blitcmd = [data.mtlcmdbuffer blitCommandEncoder];

[blitcmd copyFromBuffer:texturedata.lockedbuffer
sourceOffset:0
sourceBytesPerRow:SDL_BYTESPERPIXEL(texture->format) * rect.w
sourceBytesPerImage:0
sourceSize:MTLSizeMake(rect.w, rect.h, 1)
toTexture:texturedata.mtltexture
destinationSlice:0
destinationLevel:0
destinationOrigin:MTLOriginMake(rect.x, rect.y, 0)];

[blitcmd endEncoding];

[data.mtlcmdbuffer commit];
data.mtlcmdbuffer = nil;

#if !__has_feature(objc_arc)
[texturedata.lockedbuffer release];
#endif

texturedata.lockedbuffer = nil;
texturedata.hasdata = YES;
}}

static int
METAL_SetRenderTarget(SDL_Renderer * renderer, SDL_Texture * texture)
Expand Down

0 comments on commit ce8c716

Please sign in to comment.