From e55ab9be4656d3cc867c1360ef995937bad3fcf5 Mon Sep 17 00:00:00 2001 From: Yun Hsiao Wu Date: Wed, 20 Oct 2021 11:13:16 +0800 Subject: [PATCH] fix scissor update strat (#3995) --- .../renderer/gfx-gles2/GLES2Commands.cpp | 35 +++++++++++++----- .../renderer/gfx-gles2/GLES2GPUObjects.h | 37 +++++++++++++------ .../renderer/gfx-gles2/GLES2Swapchain.cpp | 2 + .../renderer/gfx-gles3/GLES3Commands.cpp | 35 +++++++++++++----- .../renderer/gfx-gles3/GLES3GPUObjects.h | 30 +++++++++++---- .../renderer/gfx-gles3/GLES3Swapchain.cpp | 2 + 6 files changed, 104 insertions(+), 37 deletions(-) diff --git a/native/cocos/renderer/gfx-gles2/GLES2Commands.cpp b/native/cocos/renderer/gfx-gles2/GLES2Commands.cpp index 98ff63644c..f4749e749a 100644 --- a/native/cocos/renderer/gfx-gles2/GLES2Commands.cpp +++ b/native/cocos/renderer/gfx-gles2/GLES2Commands.cpp @@ -1282,20 +1282,20 @@ void cmdFuncGLES2DestroyInputAssembler(GLES2Device *device, GLES2GPUInputAssembl gpuInputAssembler->glVAOs.clear(); } -static GLuint doCreateFramebuffer(GLES2Device * device, +static GLES2GPUFramebuffer::GLFramebufferInfo doCreateFramebuffer(GLES2Device * device, const vector &attachments, const uint32_t *colors, size_t colorCount, const GLES2GPUTexture *depthStencil, const uint32_t * resolves = nullptr, const GLES2GPUTexture *depthStencilResolve = nullptr, GLbitfield * resolveMask = nullptr) { static vector drawBuffers; - GLuint glFramebuffer{0U}; GLES2GPUStateCache * cache = device->stateCache(); + GLES2GPUFramebuffer::GLFramebufferInfo res; - GL_CHECK(glGenFramebuffers(1, &glFramebuffer)); - if (cache->glFramebuffer != glFramebuffer) { - GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, glFramebuffer)); - cache->glFramebuffer = glFramebuffer; + GL_CHECK(glGenFramebuffers(1, &res.glFramebuffer)); + if (cache->glFramebuffer != res.glFramebuffer) { + GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, res.glFramebuffer)); + cache->glFramebuffer = res.glFramebuffer; } drawBuffers.clear(); @@ -1324,6 +1324,8 @@ static GLuint doCreateFramebuffer(GLES2Device * device, GL_CHECK(glFramebufferRenderbuffer(GL_FRAMEBUFFER, static_cast(GL_COLOR_ATTACHMENT0 + j), gpuColorTexture->glTarget, gpuColorTexture->glRenderbuffer)); } + res.width = std::min(res.width, gpuColorTexture->width); + res.height = std::min(res.height, gpuColorTexture->height); } if (depthStencil) { bool hasStencil = GFX_FORMAT_INFOS[static_cast(depthStencil->format)].hasStencil; @@ -1337,11 +1339,13 @@ static GLuint doCreateFramebuffer(GLES2Device * device, // fallback to blit-based manual resolve if (depthStencilResolve) *resolveMask |= hasStencil ? GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT : GL_DEPTH_BUFFER_BIT; + res.width = std::min(res.width, depthStencil->width); + res.height = std::min(res.height, depthStencil->height); } // register to framebuffer caches - if (colorCount == 1) device->framebufferCacheMap()->registerExternal(glFramebuffer, attachments[colors[0]]); - if (depthStencil) device->framebufferCacheMap()->registerExternal(glFramebuffer, depthStencil); + if (colorCount == 1) device->framebufferCacheMap()->registerExternal(res.glFramebuffer, attachments[colors[0]]); + if (depthStencil) device->framebufferCacheMap()->registerExternal(res.glFramebuffer, depthStencil); if (device->hasFeature(Feature::MULTIPLE_RENDER_TARGETS)) { GL_CHECK(glDrawBuffersEXT(utils::toUint(drawBuffers.size()), drawBuffers.data())); @@ -1369,7 +1373,7 @@ static GLuint doCreateFramebuffer(GLES2Device * device, } } - return glFramebuffer; + return res; } static GLES2GPUSwapchain *getSwapchainIfExists(const vector &textures, const uint32_t *indices, size_t count) { @@ -1534,6 +1538,19 @@ void cmdFuncGLES2BeginRenderPass(GLES2Device *device, uint32_t subpassIdx, GLES2 cache->viewport.width = renderArea->width; cache->viewport.height = renderArea->height; } + + uint32_t fboWidth = instance.framebuffer.getWidth(); + uint32_t fboHeight = instance.framebuffer.getHeight(); + if (cache->scissor.x != 0 || + cache->scissor.y != 0 || + cache->scissor.width != fboWidth || + cache->scissor.height != fboHeight) { + GL_CHECK(glScissor(0, 0, fboWidth, fboHeight)); + cache->scissor.x = 0; + cache->scissor.y = 0; + cache->scissor.width = fboWidth; + cache->scissor.height = fboHeight; + } } GLbitfield glClears = 0; diff --git a/native/cocos/renderer/gfx-gles2/GLES2GPUObjects.h b/native/cocos/renderer/gfx-gles2/GLES2GPUObjects.h index 2c8826ed24..721e8fcb0c 100644 --- a/native/cocos/renderer/gfx-gles2/GLES2GPUObjects.h +++ b/native/cocos/renderer/gfx-gles2/GLES2GPUObjects.h @@ -92,13 +92,6 @@ private: StringArray _extensions; }; -class GLES2GPUSwapchain final : public Object { -public: - EGLSurface eglSurface{EGL_NO_SURFACE}; - EGLint eglSwapInterval{0}; - GLuint glFramebuffer{0}; -}; - class GLES2GPUBuffer final : public Object { public: BufferUsage usage = BufferUsage::NONE; @@ -152,6 +145,14 @@ public: using GLES2GPUTextureList = vector; +class GLES2GPUSwapchain final : public Object { +public: + EGLSurface eglSurface{EGL_NO_SURFACE}; + EGLint eglSwapInterval{0}; + GLuint glFramebuffer{0}; + GLES2GPUTexture *gpuColorTexture{nullptr}; +}; + class GLES2GPUSampler final : public Object { public: Filter minFilter = Filter::NONE; @@ -285,17 +286,31 @@ public: GLES2GPUTexture * gpuDepthStencilTexture{nullptr}; bool usesFBF{false}; + struct GLFramebufferInfo { + GLuint glFramebuffer{0U}; + uint32_t width{UINT_MAX}; + uint32_t height{UINT_MAX}; + }; + struct GLFramebuffer { - inline void initialize(GLES2GPUSwapchain *sc) { swapchain = sc; } - inline void initialize(GLuint framebuffer) { _glFramebuffer = framebuffer; } - inline GLuint getFramebuffer() const { return swapchain ? swapchain->glFramebuffer : _glFramebuffer; } + inline void initialize(GLES2GPUSwapchain *sc) { swapchain = sc; } + inline void initialize(const GLFramebufferInfo &info) { + _glFramebuffer = info.glFramebuffer; + _width = info.width; + _height = info.height; + } + inline GLuint getFramebuffer() const { return swapchain ? swapchain->glFramebuffer : _glFramebuffer; } + inline uint32_t getWidth() const { return swapchain ? swapchain->gpuColorTexture->width : _width; } + inline uint32_t getHeight() const { return swapchain ? swapchain->gpuColorTexture->height : _height; } void destroy(GLES2GPUStateCache *cache, GLES2GPUFramebufferCacheMap *framebufferCacheMap); GLES2GPUSwapchain *swapchain{nullptr}; private: - GLuint _glFramebuffer{0U}; + GLuint _glFramebuffer{0U}; + uint32_t _width{0U}; + uint32_t _height{0U}; }; struct Framebuffer { diff --git a/native/cocos/renderer/gfx-gles2/GLES2Swapchain.cpp b/native/cocos/renderer/gfx-gles2/GLES2Swapchain.cpp index 65ebcf6cd4..33be123929 100644 --- a/native/cocos/renderer/gfx-gles2/GLES2Swapchain.cpp +++ b/native/cocos/renderer/gfx-gles2/GLES2Swapchain.cpp @@ -98,6 +98,8 @@ void GLES2Swapchain::doInit(const SwapchainInfo& info) { textureInfo.format = Format::DEPTH_STENCIL; initTexture(textureInfo, _depthStencilTexture); + + _gpuSwapchain->gpuColorTexture = static_cast(_colorTexture)->gpuTexture(); } void GLES2Swapchain::doDestroy() { diff --git a/native/cocos/renderer/gfx-gles3/GLES3Commands.cpp b/native/cocos/renderer/gfx-gles3/GLES3Commands.cpp index 15c07ce3d5..2fbe09bef8 100644 --- a/native/cocos/renderer/gfx-gles3/GLES3Commands.cpp +++ b/native/cocos/renderer/gfx-gles3/GLES3Commands.cpp @@ -1478,20 +1478,20 @@ void cmdFuncGLES3DestroyInputAssembler(GLES3Device *device, GLES3GPUInputAssembl gpuInputAssembler->glVAOs.clear(); } -static GLuint doCreateFramebuffer(GLES3Device * device, +static GLES3GPUFramebuffer::GLFramebufferInfo doCreateFramebuffer(GLES3Device * device, const vector &attachments, const uint32_t *colors, size_t colorCount, const GLES3GPUTexture *depthStencil, const uint32_t * resolves = nullptr, const GLES3GPUTexture *depthStencilResolve = nullptr, GLbitfield * resolveMask = nullptr) { static vector drawBuffers; - GLuint glFramebuffer{0U}; GLES3GPUStateCache * cache = device->stateCache(); + GLES3GPUFramebuffer::GLFramebufferInfo res; - GL_CHECK(glGenFramebuffers(1, &glFramebuffer)); - if (cache->glDrawFramebuffer != glFramebuffer) { - GL_CHECK(glBindFramebuffer(GL_DRAW_FRAMEBUFFER, glFramebuffer)); - cache->glDrawFramebuffer = glFramebuffer; + GL_CHECK(glGenFramebuffers(1, &res.glFramebuffer)); + if (cache->glDrawFramebuffer != res.glFramebuffer) { + GL_CHECK(glBindFramebuffer(GL_DRAW_FRAMEBUFFER, res.glFramebuffer)); + cache->glDrawFramebuffer = res.glFramebuffer; } drawBuffers.clear(); @@ -1520,6 +1520,8 @@ static GLuint doCreateFramebuffer(GLES3Device * device, GL_CHECK(glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER, static_cast(GL_COLOR_ATTACHMENT0 + j), gpuColorTexture->glTarget, gpuColorTexture->glRenderbuffer)); } + res.width = std::min(res.width, gpuColorTexture->width); + res.height = std::min(res.height, gpuColorTexture->height); } if (depthStencil) { bool hasStencil = GFX_FORMAT_INFOS[static_cast(depthStencil->format)].hasStencil; @@ -1532,11 +1534,13 @@ static GLuint doCreateFramebuffer(GLES3Device * device, // fallback to blit-based manual resolve if (depthStencilResolve) *resolveMask |= hasStencil ? GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT : GL_DEPTH_BUFFER_BIT; + res.width = std::min(res.width, depthStencil->width); + res.height = std::min(res.height, depthStencil->height); } // register to framebuffer caches - if (colorCount == 1) device->framebufferCacheMap()->registerExternal(glFramebuffer, attachments[colors[0]], 0); - if (depthStencil) device->framebufferCacheMap()->registerExternal(glFramebuffer, depthStencil, 0); + if (colorCount == 1) device->framebufferCacheMap()->registerExternal(res.glFramebuffer, attachments[colors[0]], 0); + if (depthStencil) device->framebufferCacheMap()->registerExternal(res.glFramebuffer, depthStencil, 0); GL_CHECK(glDrawBuffers(utils::toUint(drawBuffers.size()), drawBuffers.data())); @@ -1565,7 +1569,7 @@ static GLuint doCreateFramebuffer(GLES3Device * device, } } - return glFramebuffer; + return res; } static GLES3GPUSwapchain *getSwapchainIfExists(const vector &textures, const uint32_t *indices, size_t count) { @@ -1884,6 +1888,19 @@ void cmdFuncGLES3BeginRenderPass(GLES3Device *device, uint32_t subpassIdx, GLES3 cache->viewport.width = renderArea->width; cache->viewport.height = renderArea->height; } + + uint32_t fboWidth = instance.framebuffer.getWidth(); + uint32_t fboHeight = instance.framebuffer.getHeight(); + if (cache->scissor.x != 0 || + cache->scissor.y != 0 || + cache->scissor.width != fboWidth || + cache->scissor.height != fboHeight) { + GL_CHECK(glScissor(0, 0, fboWidth, fboHeight)); + cache->scissor.x = 0; + cache->scissor.y = 0; + cache->scissor.width = fboWidth; + cache->scissor.height = fboHeight; + } } GLbitfield glClears = 0; diff --git a/native/cocos/renderer/gfx-gles3/GLES3GPUObjects.h b/native/cocos/renderer/gfx-gles3/GLES3GPUObjects.h index 2ff2ecb14e..e49b295bbb 100644 --- a/native/cocos/renderer/gfx-gles3/GLES3GPUObjects.h +++ b/native/cocos/renderer/gfx-gles3/GLES3GPUObjects.h @@ -90,13 +90,6 @@ private: StringArray _extensions; }; -class GLES3GPUSwapchain final : public Object { -public: - EGLSurface eglSurface{EGL_NO_SURFACE}; - EGLint eglSwapInterval{0}; - GLuint glFramebuffer{0}; -}; - class GLES3GPUQueryPool final : public Object { public: QueryType type{QueryType::OCCLUSION}; @@ -159,6 +152,14 @@ public: using GLES3GPUTextureList = vector; +class GLES3GPUSwapchain final : public Object { +public: + EGLSurface eglSurface{EGL_NO_SURFACE}; + EGLint eglSwapInterval{0}; + GLuint glFramebuffer{0}; + GLES3GPUTexture *gpuColorTexture{nullptr}; +}; + class GLES3GPUSampler final : public Object { public: Filter minFilter = Filter::NONE; @@ -328,10 +329,21 @@ public: GLES3GPUTexture * gpuDepthStencilTexture{nullptr}; bool usesFBF{false}; + struct GLFramebufferInfo { + GLuint glFramebuffer{0U}; + uint32_t width{UINT_MAX}; + uint32_t height{UINT_MAX}; + }; struct GLFramebuffer { inline void initialize(GLES3GPUSwapchain *sc) { swapchain = sc; } - inline void initialize(GLuint framebuffer) { _glFramebuffer = framebuffer; } + inline void initialize(const GLFramebufferInfo &info) { + _glFramebuffer = info.glFramebuffer; + _width = info.width; + _height = info.height; + } inline GLuint getFramebuffer() const { return swapchain ? swapchain->glFramebuffer : _glFramebuffer; } + inline uint32_t getWidth() const { return swapchain ? swapchain->gpuColorTexture->width : _width; } + inline uint32_t getHeight() const { return swapchain ? swapchain->gpuColorTexture->height : _height; } void destroy(GLES3GPUStateCache *cache, GLES3GPUFramebufferCacheMap *framebufferCacheMap); @@ -339,6 +351,8 @@ public: private: GLuint _glFramebuffer{0U}; + uint32_t _width{0U}; + uint32_t _height{0U}; }; struct Framebuffer { diff --git a/native/cocos/renderer/gfx-gles3/GLES3Swapchain.cpp b/native/cocos/renderer/gfx-gles3/GLES3Swapchain.cpp index a02e6b0c6c..cfc86fb49f 100644 --- a/native/cocos/renderer/gfx-gles3/GLES3Swapchain.cpp +++ b/native/cocos/renderer/gfx-gles3/GLES3Swapchain.cpp @@ -98,6 +98,8 @@ void GLES3Swapchain::doInit(const SwapchainInfo& info) { textureInfo.format = Format::DEPTH_STENCIL; initTexture(textureInfo, _depthStencilTexture); + + _gpuSwapchain->gpuColorTexture = static_cast(_colorTexture)->gpuTexture(); } void GLES3Swapchain::doDestroy() {