Browse Source

metal: fix love.graphics.captureScreenshot

slime 3 years ago
parent
commit
bba1717130
2 changed files with 9 additions and 8 deletions
  1. 1 1
      src/modules/graphics/metal/Buffer.mm
  2. 8 7
      src/modules/graphics/metal/Graphics.mm

+ 1 - 1
src/modules/graphics/metal/Buffer.mm

@@ -123,7 +123,7 @@ void *Buffer::map(MapType map, size_t offset, size_t size)
 		return nullptr;
 
 	if (map == MAP_READ_ONLY && dataUsage != BUFFERDATAUSAGE_READBACK)
-		return  nullptr;
+		return nullptr;
 
 	Range r(offset, size);
 

+ 8 - 7
src/modules/graphics/metal/Graphics.mm

@@ -1520,14 +1520,17 @@ void Graphics::present(void *screenshotCallbackData)
 
 	deprecations.draw(this);
 
+	// endPass calls useRenderEncoder, which makes sure activeDrawable is set
+	// when possible.
 	endPass();
 
 	id<MTLBuffer> screenshotbuffer = nil;
 
+	int w = activeDrawable.texture.width;
+	int h = activeDrawable.texture.height;
+
 	if (!pendingScreenshotCallbacks.empty())
 	{
-		int w = activeDrawable.texture.width;
-		int h = activeDrawable.texture.height;
 		size_t size = w * h * 4;
 
 		screenshotbuffer = [device newBufferWithLength:size options:MTLResourceStorageModeShared];
@@ -1540,7 +1543,7 @@ void Graphics::present(void *screenshotCallbackData)
 						 sourceSlice:0
 						 sourceLevel:0
 						sourceOrigin:MTLOriginMake(0, 0, 0)
-						  sourceSize:MTLSizeMake(w, h, 0)
+						  sourceSize:MTLSizeMake(w, h, 1)
 							toBuffer:screenshotbuffer
 				   destinationOffset:0
 			  destinationBytesPerRow:w * 4
@@ -1564,12 +1567,12 @@ void Graphics::present(void *screenshotCallbackData)
 
 	submitCommandBuffer(SUBMIT_DONE);
 
+	activeDrawable = nil;
+
 	if (!pendingScreenshotCallbacks.empty())
 	{
 		[cmd waitUntilCompleted];
 
-		int w = activeDrawable.texture.width;
-		int h = activeDrawable.texture.height;
 		size_t size = w * h * 4;
 
 		auto imagemodule = Module::getInstance<love::image::Image>(M_IMAGE);
@@ -1620,8 +1623,6 @@ void Graphics::present(void *screenshotCallbackData)
 	// This is set to NO when there are pending screen captures.
 	metalLayer.framebufferOnly = YES;
 
-	activeDrawable = nil;
-
 	// Reset the per-frame stat counts.
 	drawCalls = 0;
 	shaderSwitches = 0;