mirror of
https://github.com/love2d/love.git
synced 2026-08-17 19:23:38 +02:00
metal: hook up the backbuffer
This commit is contained in:
@@ -23,6 +23,9 @@
|
||||
#include "graphics/Graphics.h"
|
||||
#include "Metal.h"
|
||||
|
||||
@class CAMetalLayer;
|
||||
@protocol CAMetalDrawable;
|
||||
|
||||
namespace love
|
||||
{
|
||||
namespace graphics
|
||||
@@ -177,6 +180,8 @@ private:
|
||||
id<MTLRenderCommandEncoder> renderEncoder;
|
||||
id<MTLBlitCommandEncoder> blitEncoder;
|
||||
|
||||
CAMetalLayer *metalLayer;
|
||||
id<CAMetalDrawable> activeDrawable;
|
||||
MTLRenderPassDescriptor *passDesc;
|
||||
|
||||
uint32 dirtyRenderState;
|
||||
|
||||
@@ -155,6 +155,8 @@ Graphics::Graphics()
|
||||
, commandBuffer(nil)
|
||||
, renderEncoder(nil)
|
||||
, blitEncoder(nil)
|
||||
, metalLayer(nil)
|
||||
, activeDrawable(nil)
|
||||
, passDesc(nil)
|
||||
, dirtyRenderState(STATEBIT_ALL)
|
||||
, windowHasStencil(false)
|
||||
@@ -243,9 +245,12 @@ bool Graphics::setMode(void *context, int width, int height, int pixelwidth, int
|
||||
{ @autoreleasepool {
|
||||
this->width = width;
|
||||
this->height = height;
|
||||
this->metalLayer = (__bridge CAMetalLayer *) context;
|
||||
|
||||
this->windowHasStencil = windowhasstencil;
|
||||
|
||||
metalLayer.device = device;
|
||||
|
||||
setViewportSize(width, height, pixelwidth, pixelheight);
|
||||
|
||||
created = true;
|
||||
@@ -286,7 +291,7 @@ bool Graphics::setMode(void *context, int width, int height, int pixelwidth, int
|
||||
}}
|
||||
|
||||
void Graphics::unSetMode()
|
||||
{
|
||||
{ @autoreleasepool {
|
||||
if (!isCreated())
|
||||
return;
|
||||
|
||||
@@ -300,7 +305,9 @@ void Graphics::unSetMode()
|
||||
temporaryTextures.clear();
|
||||
|
||||
created = false;
|
||||
}
|
||||
metalLayer = nil;
|
||||
activeDrawable = nil;
|
||||
}}
|
||||
|
||||
void Graphics::setActive(bool enable)
|
||||
{
|
||||
@@ -333,6 +340,19 @@ id<MTLRenderCommandEncoder> Graphics::useRenderEncoder()
|
||||
if (renderEncoder == nil)
|
||||
{
|
||||
submitBlitEncoder();
|
||||
|
||||
const auto &rts = states.back().renderTargets;
|
||||
if (rts.getFirstTarget().texture.get() != nullptr)
|
||||
{
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
if (activeDrawable == nil)
|
||||
activeDrawable = [metalLayer nextDrawable];
|
||||
passDesc.colorAttachments[0].texture = activeDrawable.texture;
|
||||
}
|
||||
|
||||
renderEncoder = [useCommandBuffer() renderCommandEncoderWithDescriptor:passDesc];
|
||||
dirtyRenderState = STATEBIT_ALL;
|
||||
}
|
||||
@@ -346,6 +366,8 @@ void Graphics::submitRenderEncoder()
|
||||
{
|
||||
[renderEncoder endEncoding];
|
||||
renderEncoder = nil;
|
||||
|
||||
passDesc.colorAttachments[0].texture = nil;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -778,7 +800,7 @@ void Graphics::discard(const std::vector<bool> &colorbuffers, bool depthstencil)
|
||||
}
|
||||
|
||||
void Graphics::present(void *screenshotCallbackData)
|
||||
{
|
||||
{ @autoreleasepool {
|
||||
if (!isActive())
|
||||
return;
|
||||
|
||||
@@ -851,12 +873,19 @@ void Graphics::present(void *screenshotCallbackData)
|
||||
buffer->nextFrame();
|
||||
batchedDrawState.indexBuffer->nextFrame();
|
||||
|
||||
id<MTLCommandBuffer> cmd = getCommandBuffer();
|
||||
|
||||
if (cmd != nil && activeDrawable != nil)
|
||||
[cmd presentDrawable:activeDrawable];
|
||||
|
||||
submitCommandBuffer();
|
||||
|
||||
auto window = Module::getInstance<love::window::Window>(M_WINDOW);
|
||||
if (window != nullptr)
|
||||
window->swapBuffers();
|
||||
|
||||
activeDrawable = nil;
|
||||
|
||||
// Reset the per-frame stat counts.
|
||||
drawCalls = 0;
|
||||
//gl.stats.shaderSwitches = 0;
|
||||
@@ -875,7 +904,7 @@ void Graphics::present(void *screenshotCallbackData)
|
||||
else
|
||||
temporaryTextures[i].framesSinceUse++;
|
||||
}
|
||||
}
|
||||
}}
|
||||
|
||||
void Graphics::setColor(Colorf c)
|
||||
{
|
||||
|
||||
@@ -81,7 +81,10 @@ MTLPixelFormat Metal::convertPixelFormat(PixelFormat format, bool &isSRGB)
|
||||
break;
|
||||
|
||||
case PIXELFORMAT_LA8_UNORM:
|
||||
mtlformat = MTLPixelFormatRGBA8Unorm;
|
||||
// TODO: Swizzle
|
||||
// TODO: fall back to RGBA8 when swizzle isn't available. Pixel format
|
||||
// size calculation will need to be adjusted as well
|
||||
mtlformat = MTLPixelFormatRG8Unorm;
|
||||
break;
|
||||
|
||||
case PIXELFORMAT_RGBA4_UNORM:
|
||||
|
||||
@@ -51,7 +51,7 @@ public:
|
||||
data = (uint8 *) buffer.contents;
|
||||
|
||||
for (int i = 0; i < BUFFER_FRAMES; i++)
|
||||
frameSemaphores[i] = dispatch_semaphore_create(0);
|
||||
frameSemaphores[i] = dispatch_semaphore_create(1);
|
||||
}}
|
||||
|
||||
virtual ~StreamBuffer()
|
||||
|
||||
Reference in New Issue
Block a user