From 464b4537488baf2fe90c9746262f5c5ff47c1118 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sat, 28 Jun 2014 01:19:07 -0700 Subject: [PATCH 1/3] Use a temp FBO at 1x to upload stencil. This improves performance at > 1x resolutions. --- GPU/GLES/StencilBuffer.cpp | 48 +++++++++++++++++++++++++++++++++----- 1 file changed, 42 insertions(+), 6 deletions(-) diff --git a/GPU/GLES/StencilBuffer.cpp b/GPU/GLES/StencilBuffer.cpp index 555c4cd4f6..908c9bcd0a 100644 --- a/GPU/GLES/StencilBuffer.cpp +++ b/GPU/GLES/StencilBuffer.cpp @@ -112,10 +112,37 @@ bool FramebufferManager::NotifyStencilUpload(u32 addr, int size) { break; } - if (dstBuffer->fbo) { + bool useBlit = false; + bool useNV = false; + +#ifndef USING_GLES2 + if (gl_extensions.FBO_ARB) { + useNV = false; + useBlit = true; + } +#else + if (gl_extensions.GLES3 || gl_extensions.NV_framebuffer_blit) { + useNV = !gl_extensions.GLES3; + useBlit = true; + } +#endif + + // Our fragment shader (and discard) is slow. Since the source is 1x, we can stencil to 1x. + // Then after we're done, we'll just blit it across and stretch it there. + if (dstBuffer->bufferWidth == dstBuffer->renderWidth || !dstBuffer->fbo) { + useBlit = false; + } + u16 w = useBlit ? dstBuffer->bufferWidth : dstBuffer->renderWidth; + u16 h = useBlit ? dstBuffer->bufferHeight : dstBuffer->renderHeight; + + FBO *blitFBO = NULL; + if (useBlit) { + blitFBO = GetTempFBO(w, h, FBO_8888); + fbo_bind_as_render_target(blitFBO); + } else if (dstBuffer->fbo) { fbo_bind_as_render_target(dstBuffer->fbo); } - glViewport(0, 0, dstBuffer->renderWidth, dstBuffer->renderHeight); + glViewport(0, 0, w, h); glClearStencil(0); glClear(GL_STENCIL_BUFFER_BIT); @@ -140,11 +167,20 @@ bool FramebufferManager::NotifyStencilUpload(u32 addr, int size) { } glStencilMask(0xFF); - if (currentRenderVfb_) { - RebindFramebuffer(); - } else { - fbo_unbind(); + if (useBlit) { + fbo_bind_as_render_target(dstBuffer->fbo); + fbo_bind_for_read(blitFBO); + if (!useNV) { + glBlitFramebuffer(0, 0, w, h, 0, 0, dstBuffer->renderWidth, dstBuffer->renderHeight, GL_STENCIL_BUFFER_BIT, GL_NEAREST); + } else { +#if defined(USING_GLES2) && defined(ANDROID) // We only support this extension on Android, it's not even available on PC. + glBlitFramebufferNV(0, 0, w, h, 0, 0, dstBuffer->renderWidth, dstBuffer->renderHeight, GL_STENCIL_BUFFER_BIT, GL_NEAREST); +#endif // defined(USING_GLES2) && defined(ANDROID) + } } + + fbo_unbind(); + RebindFramebuffer(); glstate.viewport.restore(); return true; } \ No newline at end of file From a33589b3e9ddede1ca752d9ce743a5c6f744a2ed Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sat, 28 Jun 2014 01:39:18 -0700 Subject: [PATCH 2/3] Optimize away bits in the stencil upload. Should be faster than sending to the GPU this way, if we can help it. Should improve delays on FBO creation at high render resolutions. --- GPU/GLES/Framebuffer.cpp | 2 +- GPU/GLES/Framebuffer.h | 2 +- GPU/GLES/StencilBuffer.cpp | 100 +++++++++++++++++++++++++++++-------- 3 files changed, 80 insertions(+), 24 deletions(-) diff --git a/GPU/GLES/Framebuffer.cpp b/GPU/GLES/Framebuffer.cpp index 1d6e31ca95..e17e03ca68 100644 --- a/GPU/GLES/Framebuffer.cpp +++ b/GPU/GLES/Framebuffer.cpp @@ -1005,7 +1005,7 @@ void FramebufferManager::DoSetRenderFrameBuffer() { ClearBuffer(); if (useBufferedRendering_ && !updateVRAM_) { gpu->PerformMemoryUpload(fb_address_mem, byteSize); - gpu->PerformStencilUpload(fb_address_mem, byteSize); + NotifyStencilUpload(fb_address_mem, byteSize, true); // TODO: Is it worth trying to upload the depth buffer? } diff --git a/GPU/GLES/Framebuffer.h b/GPU/GLES/Framebuffer.h index ec4e2df9aa..f6231b359e 100644 --- a/GPU/GLES/Framebuffer.h +++ b/GPU/GLES/Framebuffer.h @@ -226,7 +226,7 @@ public: inline bool ShouldDownloadFramebuffer(const VirtualFramebuffer *vfb) const; bool NotifyFramebufferCopy(u32 src, u32 dest, int size, bool isMemset = false); - bool NotifyStencilUpload(u32 addr, int size); + bool NotifyStencilUpload(u32 addr, int size, bool skipZero = false); void DestroyFramebuf(VirtualFramebuffer *vfb); void ResizeFramebufFBO(VirtualFramebuffer *vfb, u16 w, u16 h, bool force = false); diff --git a/GPU/GLES/StencilBuffer.cpp b/GPU/GLES/StencilBuffer.cpp index 908c9bcd0a..d6d357ad3d 100644 --- a/GPU/GLES/StencilBuffer.cpp +++ b/GPU/GLES/StencilBuffer.cpp @@ -50,7 +50,41 @@ static const char *stencil_vs = " gl_Position = a_position;\n" "}\n"; -bool FramebufferManager::NotifyStencilUpload(u32 addr, int size) { +static u8 StencilBits5551(const u8 *ptr8, u32 numPixels) { + const u16 *ptr = (const u16 *)ptr8; + + for (u32 i = 0; i < numPixels; ++i) { + if (ptr[i] & 0x8000) { + return 1; + } + } + + return 0; +} + +static u8 StencilBits4444(const u8 *ptr8, u32 numPixels) { + const u16 *ptr = (const u16 *)ptr8; + u8 bits = 0; + + for (u32 i = 0; i < numPixels; ++i) { + bits |= ptr[i] >> 12; + } + + return bits; +} + +static u8 StencilBits8888(const u8 *ptr8, u32 numPixels) { + const u32 *ptr = (const u32 *)ptr8; + u8 bits = 0; + + for (u32 i = 0; i < numPixels; ++i) { + bits |= ptr[i] >> 24; + } + + return bits; +} + +bool FramebufferManager::NotifyStencilUpload(u32 addr, int size, bool skipZero) { if (!MayIntersectFramebuffer(addr)) { return false; } @@ -66,6 +100,45 @@ bool FramebufferManager::NotifyStencilUpload(u32 addr, int size) { return false; } + int values = 0; + u8 usedBits = 0; + + switch (dstBuffer->format) { + case GE_FORMAT_565: + // Well, this doesn't make much sense. + return false; + case GE_FORMAT_5551: + usedBits = StencilBits5551(Memory::GetPointer(addr), dstBuffer->fb_stride * dstBuffer->bufferHeight); + values = 2; + break; + case GE_FORMAT_4444: + usedBits = StencilBits4444(Memory::GetPointer(addr), dstBuffer->fb_stride * dstBuffer->bufferHeight); + values = 16; + break; + case GE_FORMAT_8888: + usedBits = StencilBits8888(Memory::GetPointer(addr), dstBuffer->fb_stride * dstBuffer->bufferHeight); + values = 256; + break; + case GE_FORMAT_INVALID: + // Impossible. + break; + } + + if (usedBits == 0) { + if (skipZero) { + // Common when creating buffers, it's already 0. We're done. + return false; + } + + // Let's not bother with the shader if it's just zero. + glstate.scissorTest.disable(); + glstate.colorMask.set(GL_FALSE, GL_FALSE, GL_FALSE, GL_TRUE); + glClearColor(0, 0, 0, 0); + glClearStencil(0); + glClear(GL_STENCIL_BUFFER_BIT | GL_COLOR_BUFFER_BIT); + return true; + } + GLSLProgram *program = 0; if (!stencilUploadProgram_) { std::string errorString; @@ -91,27 +164,6 @@ bool FramebufferManager::NotifyStencilUpload(u32 addr, int size) { glstate.stencilTest.enable(); glstate.stencilOp.set(GL_REPLACE, GL_REPLACE, GL_REPLACE); - // TODO: Doing it the slow way for now. - int values = 0; - - switch (dstBuffer->format) { - case GE_FORMAT_565: - // Well, this doesn't make much sense. - return false; - case GE_FORMAT_5551: - values = 2; - break; - case GE_FORMAT_4444: - values = 16; - break; - case GE_FORMAT_8888: - values = 256; - break; - case GE_FORMAT_INVALID: - // Impossible. - break; - } - bool useBlit = false; bool useNV = false; @@ -151,6 +203,10 @@ bool FramebufferManager::NotifyStencilUpload(u32 addr, int size) { GLint u_stencilValue = glsl_uniform_loc(stencilUploadProgram_, "u_stencilValue"); for (int i = 1; i < values; i += i) { + if (!(usedBits & i)) { + // It's already zero, let's skip it. + continue; + } // DrawActiveTexture unbinds it, so rebind here before setting uniforms. glsl_bind(stencilUploadProgram_); if (dstBuffer->format == GE_FORMAT_4444) { From e57ed7d2f40703f47524c69edb4d45efb6cc7e83 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sat, 28 Jun 2014 02:00:14 -0700 Subject: [PATCH 3/3] Small optimizations to stencil scanning. --- GPU/GLES/StencilBuffer.cpp | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/GPU/GLES/StencilBuffer.cpp b/GPU/GLES/StencilBuffer.cpp index d6d357ad3d..bba59b2549 100644 --- a/GPU/GLES/StencilBuffer.cpp +++ b/GPU/GLES/StencilBuffer.cpp @@ -51,10 +51,10 @@ static const char *stencil_vs = "}\n"; static u8 StencilBits5551(const u8 *ptr8, u32 numPixels) { - const u16 *ptr = (const u16 *)ptr8; + const u32 *ptr = (const u32 *)ptr8; - for (u32 i = 0; i < numPixels; ++i) { - if (ptr[i] & 0x8000) { + for (u32 i = 0; i < numPixels / 2; ++i) { + if (ptr[i] & 0x80008000) { return 1; } } @@ -63,25 +63,25 @@ static u8 StencilBits5551(const u8 *ptr8, u32 numPixels) { } static u8 StencilBits4444(const u8 *ptr8, u32 numPixels) { - const u16 *ptr = (const u16 *)ptr8; - u8 bits = 0; + const u32 *ptr = (const u32 *)ptr8; + u32 bits = 0; - for (u32 i = 0; i < numPixels; ++i) { - bits |= ptr[i] >> 12; + for (u32 i = 0; i < numPixels / 2; ++i) { + bits |= ptr[i]; } - return bits; + return ((bits >> 12) & 0xF) | (bits >> 28); } static u8 StencilBits8888(const u8 *ptr8, u32 numPixels) { const u32 *ptr = (const u32 *)ptr8; - u8 bits = 0; + u32 bits = 0; for (u32 i = 0; i < numPixels; ++i) { - bits |= ptr[i] >> 24; + bits |= ptr[i]; } - return bits; + return bits >> 24; } bool FramebufferManager::NotifyStencilUpload(u32 addr, int size, bool skipZero) {