graphics: clean up some shared backbuffer settings code.

This commit is contained in:
Sasha Szpakowski
2026-06-14 19:31:40 -03:00
parent 6b144d3494
commit 8884fb3f10
9 changed files with 133 additions and 153 deletions
+19 -14
View File
@@ -201,12 +201,7 @@ Graphics::DisplayState::DisplayState()
Graphics::Graphics(const char *name) Graphics::Graphics(const char *name)
: Module(M_GRAPHICS, name) : Module(M_GRAPHICS, name)
, width(0) , backbufferSettings()
, height(0)
, pixelWidth(0)
, pixelHeight(0)
, backbufferHasStencil(false)
, backbufferHasDepth(false)
, created(false) , created(false)
, active(true) , active(true)
, batchedDrawState() , batchedDrawState()
@@ -736,7 +731,7 @@ void Graphics::validateStencilState(const StencilState &s) const
const auto &rts = states.back().renderTargets; const auto &rts = states.back().renderTargets;
love::graphics::Texture *dstexture = rts.depthStencil.texture.get(); love::graphics::Texture *dstexture = rts.depthStencil.texture.get();
if (!isRenderTargetActive() && !backbufferHasStencil) if (!isRenderTargetActive() && !backbufferSettings.stencil)
throw love::Exception("The window must have stenciling enabled to draw to the main screen's stencil buffer."); throw love::Exception("The window must have stenciling enabled to draw to the main screen's stencil buffer.");
else if (isRenderTargetActive() && (rts.temporaryRTFlags & TEMPORARY_RT_STENCIL) == 0 && (dstexture == nullptr || !isPixelFormatStencil(dstexture->getPixelFormat()))) else if (isRenderTargetActive() && (rts.temporaryRTFlags & TEMPORARY_RT_STENCIL) == 0 && (dstexture == nullptr || !isPixelFormatStencil(dstexture->getPixelFormat())))
throw love::Exception("Drawing to the stencil buffer with a Canvas active requires either stencil=true or a custom stencil-type Canvas to be used, in setCanvas."); throw love::Exception("Drawing to the stencil buffer with a Canvas active requires either stencil=true or a custom stencil-type Canvas to be used, in setCanvas.");
@@ -750,7 +745,7 @@ void Graphics::validateDepthState(bool depthwrite) const
const auto &rts = states.back().renderTargets; const auto &rts = states.back().renderTargets;
love::graphics::Texture *dstexture = rts.depthStencil.texture.get(); love::graphics::Texture *dstexture = rts.depthStencil.texture.get();
if (!isRenderTargetActive() && !backbufferHasDepth) if (!isRenderTargetActive() && !backbufferSettings.depth)
throw love::Exception("The window must have depth enabled to draw to the main screen's depth buffer."); throw love::Exception("The window must have depth enabled to draw to the main screen's depth buffer.");
else if (isRenderTargetActive() && (rts.temporaryRTFlags & TEMPORARY_RT_DEPTH) == 0 && (dstexture == nullptr || !isPixelFormatDepth(dstexture->getPixelFormat()))) else if (isRenderTargetActive() && (rts.temporaryRTFlags & TEMPORARY_RT_DEPTH) == 0 && (dstexture == nullptr || !isPixelFormatDepth(dstexture->getPixelFormat())))
throw love::Exception("Drawing to the depth buffer with a Canvas active requires either depth=true or a custom depth-type Canvas to be used, in setCanvas."); throw love::Exception("Drawing to the depth buffer with a Canvas active requires either depth=true or a custom depth-type Canvas to be used, in setCanvas.");
@@ -759,22 +754,22 @@ void Graphics::validateDepthState(bool depthwrite) const
int Graphics::getWidth() const int Graphics::getWidth() const
{ {
return width; return backbufferSettings.width;
} }
int Graphics::getHeight() const int Graphics::getHeight() const
{ {
return height; return backbufferSettings.height;
} }
int Graphics::getPixelWidth() const int Graphics::getPixelWidth() const
{ {
return pixelWidth; return backbufferSettings.pixelWidth;
} }
int Graphics::getPixelHeight() const int Graphics::getPixelHeight() const
{ {
return pixelHeight; return backbufferSettings.pixelHeight;
} }
double Graphics::getCurrentDPIScale() const double Graphics::getCurrentDPIScale() const
@@ -791,6 +786,11 @@ double Graphics::getScreenDPIScale() const
return (double) getPixelHeight() / (double) getHeight(); return (double) getPixelHeight() / (double) getHeight();
} }
int Graphics::getRequestedBackbufferMSAA() const
{
return backbufferSettings.msaa;
}
bool Graphics::isCreated() const bool Graphics::isCreated() const
{ {
return created; return created;
@@ -813,7 +813,12 @@ void Graphics::reset()
void Graphics::backbufferChanged(int width, int height, int pixelwidth, int pixelheight) void Graphics::backbufferChanged(int width, int height, int pixelwidth, int pixelheight)
{ {
backbufferChanged(width, height, pixelwidth, pixelheight, backbufferHasStencil, backbufferHasDepth, getRequestedBackbufferMSAA()); BackbufferSettings s = backbufferSettings;
s.width = width;
s.height = height;
s.pixelWidth = pixelwidth;
s.pixelHeight = pixelheight;
backbufferChanged(s);
} }
/** /**
@@ -1235,7 +1240,7 @@ void Graphics::setRenderTarget()
const RenderTargetsStrongRef prevRTs = state.renderTargets; const RenderTargetsStrongRef prevRTs = state.renderTargets;
flushBatchedDraws(); flushBatchedDraws();
setRenderTargetsInternal(RenderTargets(), pixelWidth, pixelHeight, isGammaCorrect()); setRenderTargetsInternal(RenderTargets(), backbufferSettings.pixelWidth, backbufferSettings.pixelHeight, isGammaCorrect());
state.renderTargets = RenderTargetsStrongRef(); state.renderTargets = RenderTargetsStrongRef();
renderTargetSwitchCount++; renderTargetSwitchCount++;
+28 -10
View File
@@ -448,6 +448,30 @@ public:
} }
}; };
struct BackbufferSettings
{
int width = 0;
int height = 0;
int pixelWidth = 0;
int pixelHeight = 0;
bool stencil = false;
bool depth = false;
int msaa = 0;
bool operator == (const BackbufferSettings &other) const
{
return width == other.width && height == other.height
&& pixelWidth == other.pixelWidth && pixelHeight == other.pixelHeight
&& stencil == other.stencil && depth == other.depth
&& msaa == other.msaa;
}
bool operator != (const BackbufferSettings &other) const
{
return !(operator == (other));
}
};
Graphics(const char *name); Graphics(const char *name);
virtual ~Graphics(); virtual ~Graphics();
@@ -505,13 +529,13 @@ public:
/** /**
* Called when the backbuffer changes. * Called when the backbuffer changes.
**/ **/
virtual void backbufferChanged(int width, int height, int pixelwidth, int pixelheight, bool backbufferstencil, bool backbufferdepth, int msaa) = 0; virtual void backbufferChanged(const BackbufferSettings &settings) = 0;
void backbufferChanged(int width, int height, int pixelwidth, int pixelheight); void backbufferChanged(int width, int height, int pixelwidth, int pixelheight);
/** /**
* Sets the current graphics display viewport and initializes the renderer. * Sets the current graphics display viewport and initializes the renderer.
**/ **/
virtual bool setMode(void *context, int width, int height, int pixelwidth, int pixelheight, bool backbufferstencil, bool backbufferdepth, int msaa) = 0; virtual bool setMode(void *context, const BackbufferSettings &settings) = 0;
/** /**
* Un-sets the current graphics display mode (uninitializing objects if * Un-sets the current graphics display mode (uninitializing objects if
@@ -546,7 +570,7 @@ public:
double getCurrentDPIScale() const; double getCurrentDPIScale() const;
double getScreenDPIScale() const; double getScreenDPIScale() const;
virtual int getRequestedBackbufferMSAA() const = 0; int getRequestedBackbufferMSAA() const;
virtual int getBackbufferMSAA() const = 0; virtual int getBackbufferMSAA() const = 0;
Buffer *getQuadIndexBuffer() const { return quadIndexBuffer; } Buffer *getQuadIndexBuffer() const { return quadIndexBuffer; }
@@ -1051,13 +1075,7 @@ protected:
void updateDeviceProjection(const Matrix4 &projection); void updateDeviceProjection(const Matrix4 &projection);
int width; BackbufferSettings backbufferSettings;
int height;
int pixelWidth;
int pixelHeight;
bool backbufferHasStencil;
bool backbufferHasDepth;
bool created; bool created;
bool active; bool active;
+2 -4
View File
@@ -64,8 +64,8 @@ public:
love::graphics::Texture *newTextureView(love::graphics::Texture *base, const Texture::ViewSettings &viewsettings) override; love::graphics::Texture *newTextureView(love::graphics::Texture *base, const Texture::ViewSettings &viewsettings) override;
love::graphics::Buffer *newBuffer(const Buffer::Settings &settings, const std::vector<Buffer::DataDeclaration> &format, const void *data, size_t size, size_t arraylength) override; love::graphics::Buffer *newBuffer(const Buffer::Settings &settings, const std::vector<Buffer::DataDeclaration> &format, const void *data, size_t size, size_t arraylength) override;
void backbufferChanged(int width, int height, int pixelwidth, int pixelheight, bool backbufferstencil, bool backbufferdepth, int msaa) override; void backbufferChanged(const BackbufferSettings &settings) override;
bool setMode(void *context, int width, int height, int pixelwidth, int pixelheight, bool backbufferstencil, bool backbufferdepth, int msaa) override; bool setMode(void *context, const BackbufferSettings &settings) override;
void unSetMode() override; void unSetMode() override;
void setActive(bool active) override; void setActive(bool active) override;
@@ -84,7 +84,6 @@ public:
void present(void *screenshotCallbackData) override; void present(void *screenshotCallbackData) override;
int getRequestedBackbufferMSAA() const override;
int getBackbufferMSAA() const override; int getBackbufferMSAA() const override;
void setColor(Colorf c) override; void setColor(Colorf c) override;
@@ -231,7 +230,6 @@ private:
StrongRef<love::graphics::Texture> backbufferMSAA; StrongRef<love::graphics::Texture> backbufferMSAA;
StrongRef<love::graphics::Texture> backbufferDepthStencil; StrongRef<love::graphics::Texture> backbufferDepthStencil;
int requestedBackbufferMSAA;
AttachmentStoreActions attachmentStoreActions; AttachmentStoreActions attachmentStoreActions;
+26 -41
View File
@@ -278,7 +278,6 @@ Graphics::Graphics()
, lastCullMode(CULL_MAX_ENUM) , lastCullMode(CULL_MAX_ENUM)
, lastRenderPipelineKey() , lastRenderPipelineKey()
, shaderSwitches(0) , shaderSwitches(0)
, requestedBackbufferMSAA(0)
, attachmentStoreActions() , attachmentStoreActions()
, renderBindings() , renderBindings()
, uniformBufferOffset(0) , uniformBufferOffset(0)
@@ -486,22 +485,15 @@ love::graphics::GraphicsReadback *Graphics::newReadbackInternal(ReadbackMethod m
return new GraphicsReadback(this, method, texture, slice, mipmap, rect, dest, destx, desty); return new GraphicsReadback(this, method, texture, slice, mipmap, rect, dest, destx, desty);
} }
void Graphics::backbufferChanged(int width, int height, int pixelwidth, int pixelheight, bool backbufferstencil, bool backbufferdepth, int msaa) void Graphics::backbufferChanged(const BackbufferSettings &settings)
{ {
bool sizechanged = width != this->width || height != this->height bool sizechanged = settings.width != backbufferSettings.width || settings.height != backbufferSettings.height
|| pixelwidth != this->pixelWidth || pixelheight != this->pixelHeight; || settings.pixelWidth != backbufferSettings.pixelWidth || settings.pixelHeight != backbufferSettings.pixelHeight;
bool dschanged = backbufferstencil != this->backbufferHasStencil || backbufferdepth != this->backbufferHasDepth; bool dschanged = settings.stencil != backbufferSettings.stencil || settings.depth != backbufferSettings.depth;
bool msaachanged = msaa != this->requestedBackbufferMSAA; bool msaachanged = settings.msaa != backbufferSettings.msaa;
this->width = width; backbufferSettings = settings;
this->height = height;
this->pixelWidth = pixelwidth;
this->pixelHeight = pixelheight;
this->backbufferHasStencil = backbufferstencil;
this->backbufferHasDepth = backbufferdepth;
this->requestedBackbufferMSAA = msaa;
if (!isRenderTargetActive()) if (!isRenderTargetActive())
{ {
@@ -509,44 +501,42 @@ void Graphics::backbufferChanged(int width, int height, int pixelwidth, int pixe
resetProjection(); resetProjection();
} }
Texture::Settings settings; Texture::Settings ts;
settings.width = width; ts.width = settings.width;
settings.height = height; ts.height = settings.height;
settings.dpiScale = (float)pixelheight / (float)height; ts.dpiScale = (float)settings.pixelHeight / (float)settings.height;
settings.msaa = getRequestedBackbufferMSAA(); ts.msaa = getRequestedBackbufferMSAA();
settings.renderTarget = true; ts.renderTarget = true;
settings.readable.set(false); ts.readable.set(false);
if (sizechanged || msaachanged) if (sizechanged || msaachanged)
{ {
backbufferMSAA.set(nullptr); backbufferMSAA.set(nullptr);
if (settings.msaa > 1) if (ts.msaa > 1)
{ {
settings.format = isGammaCorrect() ? PIXELFORMAT_BGRA8_sRGB : PIXELFORMAT_BGRA8_UNORM; ts.format = isGammaCorrect() ? PIXELFORMAT_BGRA8_sRGB : PIXELFORMAT_BGRA8_UNORM;
backbufferMSAA.set(newTexture(settings), Acquire::NORETAIN); backbufferMSAA.set(newTexture(ts), Acquire::NORETAIN);
} }
} }
if (sizechanged || msaachanged || dschanged) if (sizechanged || msaachanged || dschanged)
{ {
backbufferDepthStencil.set(nullptr); backbufferDepthStencil.set(nullptr);
if (backbufferstencil || backbufferdepth) if (settings.stencil || settings.depth)
{ {
if (backbufferstencil && backbufferdepth) if (settings.stencil && settings.depth)
settings.format = PIXELFORMAT_DEPTH24_UNORM_STENCIL8; ts.format = PIXELFORMAT_DEPTH24_UNORM_STENCIL8;
else if (backbufferstencil) else if (settings.stencil)
settings.format = PIXELFORMAT_STENCIL8; ts.format = PIXELFORMAT_STENCIL8;
else if (backbufferdepth) else if (settings.depth)
settings.format = PIXELFORMAT_DEPTH24_UNORM; ts.format = PIXELFORMAT_DEPTH24_UNORM;
backbufferDepthStencil.set(newTexture(settings), Acquire::NORETAIN); backbufferDepthStencil.set(newTexture(ts), Acquire::NORETAIN);
} }
} }
} }
bool Graphics::setMode(void *context, int width, int height, int pixelwidth, int pixelheight, bool backbufferstencil, bool backbufferdepth, int msaa) bool Graphics::setMode(void *context, const BackbufferSettings &settings)
{ @autoreleasepool { { @autoreleasepool {
this->width = width;
this->height = height;
this->metalLayer = (__bridge CAMetalLayer *) context; this->metalLayer = (__bridge CAMetalLayer *) context;
metalLayer.device = device; metalLayer.device = device;
@@ -560,7 +550,7 @@ bool Graphics::setMode(void *context, int width, int height, int pixelwidth, int
metalLayer.magnificationFilter = kCAFilterNearest; metalLayer.magnificationFilter = kCAFilterNearest;
#endif #endif
backbufferChanged(width, height, pixelwidth, pixelheight, backbufferstencil, backbufferdepth, msaa); backbufferChanged(settings);
created = true; created = true;
@@ -1786,11 +1776,6 @@ void Graphics::present(void *screenshotCallbackData)
processCompletedCommandBuffers(); processCompletedCommandBuffers();
}} }}
int Graphics::getRequestedBackbufferMSAA() const
{
return requestedBackbufferMSAA;
}
int Graphics::getBackbufferMSAA() const int Graphics::getBackbufferMSAA() const
{ {
return backbufferMSAA.get() ? backbufferMSAA->getMSAA() : 0; return backbufferMSAA.get() ? backbufferMSAA->getMSAA() : 0;
+24 -42
View File
@@ -110,7 +110,6 @@ Graphics::Graphics()
, windowHasStencil(false) , windowHasStencil(false)
, mainVAO(0) , mainVAO(0)
, internalBackbufferFBO(0) , internalBackbufferFBO(0)
, requestedBackbufferMSAA(0)
, bufferMapMemory(nullptr) , bufferMapMemory(nullptr)
, bufferMapMemorySize(2 * 1024 * 1024) , bufferMapMemorySize(2 * 1024 * 1024)
, pixelFormatUsage() , pixelFormatUsage()
@@ -188,27 +187,15 @@ love::graphics::GraphicsReadback *Graphics::newReadbackInternal(ReadbackMethod m
return new GraphicsReadback(this, method, texture, slice, mipmap, rect, dest, destx, desty); return new GraphicsReadback(this, method, texture, slice, mipmap, rect, dest, destx, desty);
} }
void Graphics::backbufferChanged(int width, int height, int pixelwidth, int pixelheight, bool backbufferstencil, bool backbufferdepth, int msaa) void Graphics::backbufferChanged(const BackbufferSettings &settings)
{ {
bool changed = width != this->width || height != this->height bool changed = settings != backbufferSettings;
|| pixelwidth != this->pixelWidth || pixelheight != this->pixelHeight; backbufferSettings = settings;
changed |= backbufferstencil != this->backbufferHasStencil || backbufferdepth != this->backbufferHasDepth;
changed |= msaa != this->requestedBackbufferMSAA;
this->width = width;
this->height = height;
this->pixelWidth = pixelwidth;
this->pixelHeight = pixelheight;
this->backbufferHasStencil = backbufferstencil;
this->backbufferHasDepth = backbufferdepth;
this->requestedBackbufferMSAA = msaa;
if (!isRenderTargetActive()) if (!isRenderTargetActive())
{ {
// Set the viewport to top-left corner. // Set the viewport to top-left corner.
gl.setViewport({0, 0, pixelwidth, pixelheight}); gl.setViewport({0, 0, settings.pixelWidth, settings.pixelHeight});
// Re-apply the scissor if it was active, since the rectangle passed to // Re-apply the scissor if it was active, since the rectangle passed to
// glScissor is affected by the viewport dimensions. // glScissor is affected by the viewport dimensions.
@@ -222,7 +209,7 @@ void Graphics::backbufferChanged(int width, int height, int pixelwidth, int pixe
return; return;
bool useinternalbackbuffer = false; bool useinternalbackbuffer = false;
if (msaa > 1) if (settings.msaa > 1)
useinternalbackbuffer = true; useinternalbackbuffer = true;
GLuint prevFBO = gl.getFramebuffer(OpenGL::FRAMEBUFFER_ALL); GLuint prevFBO = gl.getFramebuffer(OpenGL::FRAMEBUFFER_ALL);
@@ -230,27 +217,27 @@ void Graphics::backbufferChanged(int width, int height, int pixelwidth, int pixe
if (useinternalbackbuffer) if (useinternalbackbuffer)
{ {
Texture::Settings settings; Texture::Settings ts;
settings.width = width; ts.width = settings.width;
settings.height = height; ts.height = settings.height;
settings.dpiScale = (float)pixelheight / (float)height; ts.dpiScale = (float)settings.pixelHeight / (float)settings.height;
settings.msaa = msaa; ts.msaa = settings.msaa;
settings.renderTarget = true; ts.renderTarget = true;
settings.readable.set(false); ts.readable.set(false);
settings.format = isGammaCorrect() ? PIXELFORMAT_RGBA8_sRGB : PIXELFORMAT_RGBA8_UNORM; ts.format = isGammaCorrect() ? PIXELFORMAT_RGBA8_sRGB : PIXELFORMAT_RGBA8_UNORM;
internalBackbuffer.set(newTexture(settings), Acquire::NORETAIN); internalBackbuffer.set(newTexture(ts), Acquire::NORETAIN);
internalBackbufferDepthStencil.set(nullptr); internalBackbufferDepthStencil.set(nullptr);
if (backbufferstencil || backbufferdepth) if (settings.stencil || settings.depth)
{ {
if (backbufferstencil && backbufferdepth) if (settings.stencil && settings.depth)
settings.format = PIXELFORMAT_DEPTH24_UNORM_STENCIL8; ts.format = PIXELFORMAT_DEPTH24_UNORM_STENCIL8;
else if (backbufferstencil) else if (settings.stencil)
settings.format = PIXELFORMAT_STENCIL8; ts.format = PIXELFORMAT_STENCIL8;
else if (backbufferdepth) else if (settings.depth)
settings.format = PIXELFORMAT_DEPTH24_UNORM; ts.format = PIXELFORMAT_DEPTH24_UNORM;
internalBackbufferDepthStencil.set(newTexture(settings), Acquire::NORETAIN); internalBackbufferDepthStencil.set(newTexture(ts), Acquire::NORETAIN);
} }
RenderTargets rts; RenderTargets rts;
@@ -293,7 +280,7 @@ GLuint Graphics::getSystemBackbufferFBO() const
#endif #endif
} }
bool Graphics::setMode(void */*context*/, int width, int height, int pixelwidth, int pixelheight, bool backbufferstencil, bool backbufferdepth, int msaa) bool Graphics::setMode(void */*context*/, const BackbufferSettings &settings)
{ {
// Okay, setup OpenGL. // Okay, setup OpenGL.
gl.initContext(); gl.initContext();
@@ -350,7 +337,7 @@ bool Graphics::setMode(void */*context*/, int width, int height, int pixelwidth,
setDebug(isDebugEnabled()); setDebug(isDebugEnabled());
backbufferChanged(width, height, pixelwidth, pixelheight, backbufferstencil, backbufferdepth, msaa); backbufferChanged(settings);
if (batchedDrawState.vb[0] == nullptr) if (batchedDrawState.vb[0] == nullptr)
{ {
@@ -1314,11 +1301,6 @@ void Graphics::present(void *screenshotCallbackData)
updateTemporaryResources(); updateTemporaryResources();
} }
int Graphics::getRequestedBackbufferMSAA() const
{
return requestedBackbufferMSAA;
}
int Graphics::getBackbufferMSAA() const int Graphics::getBackbufferMSAA() const
{ {
return internalBackbuffer.get() ? internalBackbuffer->getMSAA() : 0; return internalBackbuffer.get() ? internalBackbuffer->getMSAA() : 0;
+2 -4
View File
@@ -60,8 +60,8 @@ public:
love::graphics::Texture *newTextureView(love::graphics::Texture *base, const Texture::ViewSettings &viewsettings) override; love::graphics::Texture *newTextureView(love::graphics::Texture *base, const Texture::ViewSettings &viewsettings) override;
love::graphics::Buffer *newBuffer(const Buffer::Settings &settings, const std::vector<Buffer::DataDeclaration> &format, const void *data, size_t size, size_t arraylength) override; love::graphics::Buffer *newBuffer(const Buffer::Settings &settings, const std::vector<Buffer::DataDeclaration> &format, const void *data, size_t size, size_t arraylength) override;
void backbufferChanged(int width, int height, int pixelwidth, int pixelheight, bool backbufferstencil, bool backbufferdepth, int msaa) override; void backbufferChanged(const BackbufferSettings &settings) override;
bool setMode(void *context, int width, int height, int pixelwidth, int pixelheight, bool backbufferstencil, bool backbufferdepth, int msaa) override; bool setMode(void *context, const BackbufferSettings &settings) override;
void unSetMode() override; void unSetMode() override;
void setActive(bool active) override; void setActive(bool active) override;
@@ -80,7 +80,6 @@ public:
void present(void *screenshotCallbackData) override; void present(void *screenshotCallbackData) override;
int getRequestedBackbufferMSAA() const override;
int getBackbufferMSAA() const override; int getBackbufferMSAA() const override;
void setColor(Colorf c) override; void setColor(Colorf c) override;
@@ -166,7 +165,6 @@ private:
StrongRef<love::graphics::Texture> internalBackbuffer; StrongRef<love::graphics::Texture> internalBackbuffer;
StrongRef<love::graphics::Texture> internalBackbufferDepthStencil; StrongRef<love::graphics::Texture> internalBackbufferDepthStencil;
GLuint internalBackbufferFBO; GLuint internalBackbufferFBO;
int requestedBackbufferMSAA;
char *bufferMapMemory; char *bufferMapMemory;
size_t bufferMapMemorySize; size_t bufferMapMemorySize;
+19 -32
View File
@@ -265,7 +265,7 @@ void Graphics::clear(const std::vector<OptionalColorD> &colors, OptionalInt sten
if (stencil.hasValue) if (stencil.hasValue)
{ {
if ((!rtactive && backbufferHasStencil) if ((!rtactive && backbufferSettings.stencil)
|| (dstexture && isPixelFormatStencil(dstexture->getPixelFormat())) || (rts.temporaryRTFlags & TEMPORARY_RT_STENCIL) != 0) || (dstexture && isPixelFormatStencil(dstexture->getPixelFormat())) || (rts.temporaryRTFlags & TEMPORARY_RT_STENCIL) != 0)
{ {
depthStencilAttachment.aspectMask |= VK_IMAGE_ASPECT_STENCIL_BIT; depthStencilAttachment.aspectMask |= VK_IMAGE_ASPECT_STENCIL_BIT;
@@ -274,7 +274,7 @@ void Graphics::clear(const std::vector<OptionalColorD> &colors, OptionalInt sten
} }
if (depth.hasValue) if (depth.hasValue)
{ {
if ((!rtactive && backbufferHasDepth) if ((!rtactive && backbufferSettings.depth)
|| (dstexture && isPixelFormatDepth(dstexture->getPixelFormat())) || (rts.temporaryRTFlags & TEMPORARY_RT_DEPTH) != 0) || (dstexture && isPixelFormatDepth(dstexture->getPixelFormat())) || (rts.temporaryRTFlags & TEMPORARY_RT_DEPTH) != 0)
{ {
depthStencilAttachment.aspectMask |= VK_IMAGE_ASPECT_DEPTH_BIT; depthStencilAttachment.aspectMask |= VK_IMAGE_ASPECT_DEPTH_BIT;
@@ -617,26 +617,18 @@ void Graphics::present(void *screenshotCallbackdata)
beginFrame(); beginFrame();
} }
void Graphics::backbufferChanged(int width, int height, int pixelwidth, int pixelheight, bool backbufferstencil, bool backbufferdepth, int msaa) void Graphics::backbufferChanged(const BackbufferSettings &settings)
{ {
if (swapChain != VK_NULL_HANDLE && (pixelwidth != this->pixelWidth || pixelheight != this->pixelHeight || width != this->width || height != this->height if (swapChain != VK_NULL_HANDLE && settings != backbufferSettings)
|| backbufferstencil != this->backbufferHasStencil || backbufferdepth != this->backbufferHasDepth || msaa != requestedMsaa))
requestSwapchainRecreation(); requestSwapchainRecreation();
this->width = width; backbufferSettings = settings;
this->height = height;
this->pixelWidth = pixelwidth;
this->pixelHeight = pixelheight;
this->backbufferHasStencil = backbufferstencil;
this->backbufferHasDepth = backbufferdepth;
this->requestedMsaa = msaa;
if (!isRenderTargetActive()) if (!isRenderTargetActive())
resetProjection(); resetProjection();
if (swapChain != VK_NULL_HANDLE) if (swapChain != VK_NULL_HANDLE)
msaaSamples = getMsaaCount(requestedMsaa); msaaSamples = getMsaaCount(settings.msaa);
// Don't wait until the next frame starts to recreate the swapchain - doing so // Don't wait until the next frame starts to recreate the swapchain - doing so
// will cause a 1 frame delay in the backbuffer size on resize, and it can cause // will cause a 1 frame delay in the backbuffer size on resize, and it can cause
@@ -650,10 +642,10 @@ void Graphics::backbufferChanged(int width, int height, int pixelwidth, int pixe
} }
} }
bool Graphics::setMode(void *context, int width, int height, int pixelwidth, int pixelheight, bool backbufferstencil, bool backbufferdepth, int msaa) bool Graphics::setMode(void *context, const BackbufferSettings &settings)
{ {
// Must be called before the swapchain is created. // Must be called before the swapchain is created.
backbufferChanged(width, height, pixelwidth, pixelheight, backbufferstencil, backbufferdepth, msaa); backbufferChanged(settings);
cleanUpFunctions.clear(); cleanUpFunctions.clear();
cleanUpFunctions.resize(MAX_FRAMES_IN_FLIGHT); cleanUpFunctions.resize(MAX_FRAMES_IN_FLIGHT);
@@ -674,7 +666,7 @@ bool Graphics::setMode(void *context, int width, int height, int pixelwidth, int
initCapabilities(); initCapabilities();
} }
msaaSamples = getMsaaCount(requestedMsaa); msaaSamples = getMsaaCount(settings.msaa);
createSwapChain(); createSwapChain();
createImageViews(); createImageViews();
@@ -830,11 +822,6 @@ void Graphics::setActive(bool enable)
active = enable; active = enable;
} }
int Graphics::getRequestedBackbufferMSAA() const
{
return requestedMsaa;
}
int Graphics::getBackbufferMSAA() const int Graphics::getBackbufferMSAA() const
{ {
return static_cast<int>(msaaSamples); return static_cast<int>(msaaSamples);
@@ -2076,8 +2063,8 @@ void Graphics::createSwapChain()
// because newTexture needs an active command buffer to do its initial // because newTexture needs an active command buffer to do its initial
// layout transitions. // layout transitions.
swapChainImages.clear(); swapChainImages.clear();
extent.width = std::max(1, pixelWidth); extent.width = std::max(1, backbufferSettings.pixelWidth);
extent.height = std::max(1, pixelHeight); extent.height = std::max(1, backbufferSettings.pixelHeight);
if (isGammaCorrect()) if (isGammaCorrect())
surfaceFormat.format = VK_FORMAT_R8G8B8A8_SRGB; surfaceFormat.format = VK_FORMAT_R8G8B8A8_SRGB;
@@ -2190,8 +2177,8 @@ VkExtent2D Graphics::chooseSwapExtent(const VkSurfaceCapabilitiesKHR &capabiliti
else else
{ {
VkExtent2D actualExtent = { VkExtent2D actualExtent = {
static_cast<uint32_t>(pixelWidth), static_cast<uint32_t>(backbufferSettings.pixelWidth),
static_cast<uint32_t>(pixelHeight) static_cast<uint32_t>(backbufferSettings.pixelHeight)
}; };
actualExtent.width = clampuint32_t(actualExtent.width, capabilities.minImageExtent.width, capabilities.maxImageExtent.width); actualExtent.width = clampuint32_t(actualExtent.width, capabilities.minImageExtent.width, capabilities.maxImageExtent.width);
@@ -2696,7 +2683,7 @@ void Graphics::setDefaultRenderPass()
RenderPassConfiguration renderPassConfiguration{}; RenderPassConfiguration renderPassConfiguration{};
VkFormat dsformat = backbufferHasDepth || backbufferHasStencil ? depthStencilFormat : VK_FORMAT_UNDEFINED; VkFormat dsformat = backbufferSettings.depth || backbufferSettings.stencil ? depthStencilFormat : VK_FORMAT_UNDEFINED;
renderPassConfiguration.staticData.depthStencilAttachment = { dsformat, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, VK_ATTACHMENT_LOAD_OP_LOAD, VK_ATTACHMENT_LOAD_OP_LOAD, msaaSamples }; renderPassConfiguration.staticData.depthStencilAttachment = { dsformat, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, VK_ATTACHMENT_LOAD_OP_LOAD, VK_ATTACHMENT_LOAD_OP_LOAD, msaaSamples };
if (msaaSamples & VK_SAMPLE_COUNT_1_BIT) if (msaaSamples & VK_SAMPLE_COUNT_1_BIT)
renderPassConfiguration.staticData.resolve = false; renderPassConfiguration.staticData.resolve = false;
@@ -2741,13 +2728,13 @@ void Graphics::setDefaultRenderPass()
renderPassState.clearColors[0].color = Texture::getClearColor(nullptr, renderPassState.mainWindowClearColorValue.value); renderPassState.clearColors[0].color = Texture::getClearColor(nullptr, renderPassState.mainWindowClearColorValue.value);
} }
if (renderPassState.mainWindowClearDepthValue.hasValue && backbufferHasDepth) if (renderPassState.mainWindowClearDepthValue.hasValue && backbufferSettings.depth)
{ {
renderPassState.renderPassConfiguration.staticData.depthStencilAttachment.depthLoadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; renderPassState.renderPassConfiguration.staticData.depthStencilAttachment.depthLoadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
renderPassState.clearColors[1].depthStencil.depth = static_cast<float>(renderPassState.mainWindowClearDepthValue.value); renderPassState.clearColors[1].depthStencil.depth = static_cast<float>(renderPassState.mainWindowClearDepthValue.value);
} }
if (renderPassState.mainWindowClearStencilValue.hasValue && backbufferHasStencil) if (renderPassState.mainWindowClearStencilValue.hasValue && backbufferSettings.stencil)
{ {
renderPassState.renderPassConfiguration.staticData.depthStencilAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; renderPassState.renderPassConfiguration.staticData.depthStencilAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
renderPassState.clearColors[1].depthStencil.stencil = static_cast<uint32_t>(renderPassState.mainWindowClearStencilValue.value); renderPassState.clearColors[1].depthStencil.stencil = static_cast<uint32_t>(renderPassState.mainWindowClearStencilValue.value);
@@ -3304,7 +3291,7 @@ VkFormat Graphics::findDepthFormat()
void Graphics::createDepthResources() void Graphics::createDepthResources()
{ {
if (!backbufferHasDepth && !backbufferHasStencil) if (!backbufferSettings.depth && !backbufferSettings.stencil)
{ {
depthImage = VK_NULL_HANDLE; depthImage = VK_NULL_HANDLE;
depthImageView = VK_NULL_HANDLE; depthImageView = VK_NULL_HANDLE;
@@ -3343,9 +3330,9 @@ void Graphics::createDepthResources()
imageViewInfo.components.g = VK_COMPONENT_SWIZZLE_IDENTITY; imageViewInfo.components.g = VK_COMPONENT_SWIZZLE_IDENTITY;
imageViewInfo.components.b = VK_COMPONENT_SWIZZLE_IDENTITY; imageViewInfo.components.b = VK_COMPONENT_SWIZZLE_IDENTITY;
imageViewInfo.components.a = VK_COMPONENT_SWIZZLE_IDENTITY; imageViewInfo.components.a = VK_COMPONENT_SWIZZLE_IDENTITY;
if (backbufferHasDepth) if (backbufferSettings.depth)
imageViewInfo.subresourceRange.aspectMask |= VK_IMAGE_ASPECT_DEPTH_BIT; imageViewInfo.subresourceRange.aspectMask |= VK_IMAGE_ASPECT_DEPTH_BIT;
if (backbufferHasStencil) if (backbufferSettings.stencil)
imageViewInfo.subresourceRange.aspectMask |= VK_IMAGE_ASPECT_STENCIL_BIT; imageViewInfo.subresourceRange.aspectMask |= VK_IMAGE_ASPECT_STENCIL_BIT;
imageViewInfo.subresourceRange.baseMipLevel = 0; imageViewInfo.subresourceRange.baseMipLevel = 0;
imageViewInfo.subresourceRange.levelCount = 1; imageViewInfo.subresourceRange.levelCount = 1;
+2 -4
View File
@@ -240,11 +240,10 @@ public:
void clear(const std::vector<OptionalColorD> &colors, OptionalInt stencil, OptionalDouble depth) override; void clear(const std::vector<OptionalColorD> &colors, OptionalInt stencil, OptionalDouble depth) override;
void discard(const std::vector<bool>& colorbuffers, bool depthstencil) override; void discard(const std::vector<bool>& colorbuffers, bool depthstencil) override;
void present(void *screenshotCallbackdata) override; void present(void *screenshotCallbackdata) override;
void backbufferChanged(int width, int height, int pixelwidth, int pixelheight, bool backbufferstencil, bool backbufferdepth, int msaa) override; void backbufferChanged(const BackbufferSettings &settings) override;
bool setMode(void *context, int width, int height, int pixelwidth, int pixelheight, bool backbufferstencil, bool backbufferdepth, int msaa) override; bool setMode(void *context, const BackbufferSettings &settings) override;
void unSetMode() override; void unSetMode() override;
void setActive(bool active) override; void setActive(bool active) override;
int getRequestedBackbufferMSAA() const override;
int getBackbufferMSAA() const override; int getBackbufferMSAA() const override;
void setColor(Colorf c) override; void setColor(Colorf c) override;
void setScissor(const Rect &rect) override; void setScissor(const Rect &rect) override;
@@ -364,7 +363,6 @@ private:
VkInstance instance = VK_NULL_HANDLE; VkInstance instance = VK_NULL_HANDLE;
VkPhysicalDevice physicalDevice = VK_NULL_HANDLE; VkPhysicalDevice physicalDevice = VK_NULL_HANDLE;
uint32_t deviceApiVersion = VK_API_VERSION_1_0; uint32_t deviceApiVersion = VK_API_VERSION_1_0;
int requestedMsaa = 0;
VkDevice device = VK_NULL_HANDLE; VkDevice device = VK_NULL_HANDLE;
OptionalInstanceExtensions optionalInstanceExtensions; OptionalInstanceExtensions optionalInstanceExtensions;
OptionalDeviceExtensions optionalDeviceExtensions; OptionalDeviceExtensions optionalDeviceExtensions;
+11 -2
View File
@@ -658,6 +658,15 @@ bool Window::setWindow(int width, int height, WindowSettings *settings)
double scaledw, scaledh; double scaledw, scaledh;
fromPixels((double) pixelWidth, (double) pixelHeight, scaledw, scaledh); fromPixels((double) pixelWidth, (double) pixelHeight, scaledw, scaledh);
graphics::Graphics::BackbufferSettings backbufferSettings;
backbufferSettings.width = (int)scaledw;
backbufferSettings.height = (int)scaledh;
backbufferSettings.pixelWidth = pixelWidth;
backbufferSettings.pixelHeight = pixelHeight;
backbufferSettings.stencil = f.stencil;
backbufferSettings.depth = f.depth;
backbufferSettings.msaa = f.msaa;
if (needsetmode) if (needsetmode)
{ {
void *context = nullptr; void *context = nullptr;
@@ -669,11 +678,11 @@ bool Window::setWindow(int width, int height, WindowSettings *settings)
#endif #endif
// TODO: try/catch // TODO: try/catch
graphics->setMode(context, (int) scaledw, (int) scaledh, pixelWidth, pixelHeight, f.stencil, f.depth, f.msaa); graphics->setMode(context, backbufferSettings);
} }
else else
{ {
graphics->backbufferChanged((int) scaledw, (int) scaledh, pixelWidth, pixelHeight, f.stencil, f.depth, f.msaa); graphics->backbufferChanged(backbufferSettings);
} }
this->settings.msaa = graphics->getBackbufferMSAA(); this->settings.msaa = graphics->getBackbufferMSAA();