From 8f38fb38e8be6e236e56d61aebc02670940c49aa Mon Sep 17 00:00:00 2001 From: rogerman Date: Wed, 29 Apr 2015 09:14:32 +0000 Subject: [PATCH] OpenGL Renderer: - Clear the stencil buffer with a value of 0 instead of with the polygon ID. --- desmume/src/OGLRender.cpp | 30 +++++++++++++++--------------- desmume/src/OGLRender.h | 8 ++++---- desmume/src/OGLRender_3_2.cpp | 4 ++-- desmume/src/OGLRender_3_2.h | 2 +- desmume/src/rasterize.cpp | 6 +++--- desmume/src/rasterize.h | 2 +- desmume/src/render3D.cpp | 11 +++++++---- desmume/src/render3D.h | 5 +++-- 8 files changed, 36 insertions(+), 32 deletions(-) diff --git a/desmume/src/OGLRender.cpp b/desmume/src/OGLRender.cpp index 61837e009..c5f410e43 100644 --- a/desmume/src/OGLRender.cpp +++ b/desmume/src/OGLRender.cpp @@ -1749,24 +1749,24 @@ Render3DError OpenGLRenderer_1_2::UploadToonTable(const u16 *toonTableBuffer) return OGLERROR_NOERR; } -Render3DError OpenGLRenderer_1_2::UploadClearImage(const u16 *__restrict colorBuffer, const u32 *__restrict depthStencilBuffer, const bool *__restrict fogBuffer) +Render3DError OpenGLRenderer_1_2::UploadClearImage(const u16 *__restrict colorBuffer, const u32 *__restrict depthBuffer, const bool *__restrict fogBuffer, const u8 *__restrict polyIDBuffer) { OGLRenderRef &OGLRef = *this->ref; static GLuint depth[GFX3D_FRAMEBUFFER_WIDTH * GFX3D_FRAMEBUFFER_HEIGHT]; - static GLuint polyID[GFX3D_FRAMEBUFFER_WIDTH * GFX3D_FRAMEBUFFER_HEIGHT]; static GLuint fogAttributes[GFX3D_FRAMEBUFFER_WIDTH * GFX3D_FRAMEBUFFER_HEIGHT]; + static GLuint polyID[GFX3D_FRAMEBUFFER_WIDTH * GFX3D_FRAMEBUFFER_HEIGHT]; for (size_t i = 0; i < GFX3D_FRAMEBUFFER_WIDTH * GFX3D_FRAMEBUFFER_HEIGHT; i++) { - depth[i] = clearImageDepthStencilBuffer[i] >> 8; - polyID[i] = (depthStencilBuffer[i] & 0x0000003F) | 0xFF000000; + depth[i] = depthBuffer[i] | 0xFF000000; fogAttributes[i] = (fogBuffer[i]) ? 0xFF0000FF : 0xFF000000; + polyID[i] = (GLuint)polyIDBuffer[i] | 0xFF000000; } glActiveTextureARB(GL_TEXTURE0_ARB + OGLTextureUnitID_GColor); glBindTexture(GL_TEXTURE_2D, OGLRef.texGDepthStencilID); - glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, GFX3D_FRAMEBUFFER_WIDTH, GFX3D_FRAMEBUFFER_HEIGHT, GL_DEPTH_STENCIL_EXT, GL_UNSIGNED_INT_24_8_EXT, depthStencilBuffer); + glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, GFX3D_FRAMEBUFFER_WIDTH, GFX3D_FRAMEBUFFER_HEIGHT, GL_DEPTH_STENCIL_EXT, GL_UNSIGNED_INT_24_8_EXT, depthBuffer); glBindTexture(GL_TEXTURE_2D, OGLRef.texGColorID); glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, GFX3D_FRAMEBUFFER_WIDTH, GFX3D_FRAMEBUFFER_HEIGHT, GL_RGBA, GL_UNSIGNED_SHORT_1_5_5_5_REV, colorBuffer); @@ -2255,7 +2255,7 @@ Render3DError OpenGLRenderer_1_2::UpdateToonTable(const u16 *toonTableBuffer) return OGLERROR_NOERR; } -Render3DError OpenGLRenderer_1_2::ClearUsingImage(const u16 *__restrict colorBuffer, const u32 *__restrict depthStencilBuffer, const bool *__restrict fogBuffer) +Render3DError OpenGLRenderer_1_2::ClearUsingImage(const u16 *__restrict colorBuffer, const u32 *__restrict depthBuffer, const bool *__restrict fogBuffer, const u8 *__restrict polyIDBuffer) { if (!this->isFBOSupported) { @@ -2264,7 +2264,7 @@ Render3DError OpenGLRenderer_1_2::ClearUsingImage(const u16 *__restrict colorBuf OGLRenderRef &OGLRef = *this->ref; - this->UploadClearImage(colorBuffer, depthStencilBuffer, fogBuffer); + this->UploadClearImage(colorBuffer, depthBuffer, fogBuffer, polyIDBuffer); if (this->isMultisampledFBOSupported && OGLRef.selectedRenderingFBO == OGLRef.fboMSIntermediateRenderID) { @@ -2282,7 +2282,7 @@ Render3DError OpenGLRenderer_1_2::ClearUsingImage(const u16 *__restrict colorBuf // We do this because glBlitFramebufferEXT() for GL_STENCIL_BUFFER_BIT has been tested // to be unsupported on ATI/AMD GPUs running in compatibility mode. So we do the separate // glClear() for GL_STENCIL_BUFFER_BIT to keep these GPUs working. - glClearStencil(depthStencilBuffer[0] & 0x3F); + glClearStencil(0); glClear(GL_STENCIL_BUFFER_BIT); // Blit the working depth buffer @@ -2316,7 +2316,7 @@ Render3DError OpenGLRenderer_1_2::ClearUsingValues(const u8 r, const u8 g, const glDrawBuffer(GL_COLOR_ATTACHMENT0_EXT); // texGColorID glClearColor(divide5bitBy31_LUT[r], divide5bitBy31_LUT[g], divide5bitBy31_LUT[b], divide5bitBy31_LUT[a]); glClearDepth((GLclampd)clearDepth / (GLclampd)0x00FFFFFF); - glClearStencil(clearPolyID); + glClearStencil(0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); glDrawBuffer(GL_COLOR_ATTACHMENT1_EXT); // texGDepthID @@ -2337,7 +2337,7 @@ Render3DError OpenGLRenderer_1_2::ClearUsingValues(const u8 r, const u8 g, const { glClearColor(divide5bitBy31_LUT[r], divide5bitBy31_LUT[g], divide5bitBy31_LUT[b], divide5bitBy31_LUT[a]); glClearDepth((GLclampd)clearDepth / (GLclampd)0x00FFFFFF); - glClearStencil(clearPolyID); + glClearStencil(0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); } @@ -2633,24 +2633,24 @@ Render3DError OpenGLRenderer_1_3::UploadToonTable(const u16 *toonTableBuffer) return OGLERROR_NOERR; } -Render3DError OpenGLRenderer_1_3::UploadClearImage(const u16 *__restrict colorBuffer, const u32 *__restrict depthStencilBuffer, const bool *__restrict fogBuffer) +Render3DError OpenGLRenderer_1_3::UploadClearImage(const u16 *__restrict colorBuffer, const u32 *__restrict depthBuffer, const bool *__restrict fogBuffer, const u8 *__restrict polyIDBuffer) { OGLRenderRef &OGLRef = *this->ref; static GLuint depth[GFX3D_FRAMEBUFFER_WIDTH * GFX3D_FRAMEBUFFER_HEIGHT]; - static GLuint polyID[GFX3D_FRAMEBUFFER_WIDTH * GFX3D_FRAMEBUFFER_HEIGHT]; static GLuint fogAttributes[GFX3D_FRAMEBUFFER_WIDTH * GFX3D_FRAMEBUFFER_HEIGHT]; + static GLuint polyID[GFX3D_FRAMEBUFFER_WIDTH * GFX3D_FRAMEBUFFER_HEIGHT]; for (size_t i = 0; i < GFX3D_FRAMEBUFFER_WIDTH * GFX3D_FRAMEBUFFER_HEIGHT; i++) { - depth[i] = (depthStencilBuffer[i] >> 8) | 0xFF000000; - polyID[i] = (depthStencilBuffer[i] & 0x0000003F) | 0xFF000000; + depth[i] = depthBuffer[i] | 0xFF000000; fogAttributes[i] = (fogBuffer[i]) ? 0xFF0000FF : 0xFF000000; + polyID[i] = (GLuint)polyIDBuffer[i] | 0xFF000000; } glActiveTexture(GL_TEXTURE0 + OGLTextureUnitID_GColor); glBindTexture(GL_TEXTURE_2D, OGLRef.texGDepthStencilID); - glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, GFX3D_FRAMEBUFFER_WIDTH, GFX3D_FRAMEBUFFER_HEIGHT, GL_DEPTH_STENCIL_EXT, GL_UNSIGNED_INT_24_8_EXT, depthStencilBuffer); + glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, GFX3D_FRAMEBUFFER_WIDTH, GFX3D_FRAMEBUFFER_HEIGHT, GL_DEPTH_STENCIL_EXT, GL_UNSIGNED_INT_24_8_EXT, depthBuffer); glBindTexture(GL_TEXTURE_2D, OGLRef.texGColorID); glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, GFX3D_FRAMEBUFFER_WIDTH, GFX3D_FRAMEBUFFER_HEIGHT, GL_RGBA, GL_UNSIGNED_SHORT_1_5_5_5_REV, colorBuffer); diff --git a/desmume/src/OGLRender.h b/desmume/src/OGLRender.h index b196f328f..c188724e9 100644 --- a/desmume/src/OGLRender.h +++ b/desmume/src/OGLRender.h @@ -495,7 +495,7 @@ protected: virtual Render3DError CreateToonTable() = 0; virtual Render3DError DestroyToonTable() = 0; virtual Render3DError UploadToonTable(const u16 *toonTableBuffer) = 0; - virtual Render3DError UploadClearImage(const u16 *__restrict colorBuffer, const u32 *__restrict depthStencilBuffer, const bool *__restrict fogBuffer) = 0; + virtual Render3DError UploadClearImage(const u16 *__restrict colorBuffer, const u32 *__restrict depthBuffer, const bool *__restrict fogBuffer, const u8 *__restrict polyIDBuffer) = 0; virtual void GetExtensionSet(std::set *oglExtensionSet) = 0; virtual Render3DError ExpandFreeTextures() = 0; @@ -551,7 +551,7 @@ protected: virtual Render3DError CreateToonTable(); virtual Render3DError DestroyToonTable(); virtual Render3DError UploadToonTable(const u16 *toonTableBuffer); - virtual Render3DError UploadClearImage(const u16 *__restrict colorBuffer, const u32 *__restrict depthStencilBuffer, const bool *__restrict fogBuffer); + virtual Render3DError UploadClearImage(const u16 *__restrict colorBuffer, const u32 *__restrict depthBuffer, const bool *__restrict fogBuffer, const u8 *__restrict polyIDBuffer); virtual void GetExtensionSet(std::set *oglExtensionSet); virtual Render3DError ExpandFreeTextures(); @@ -569,7 +569,7 @@ protected: virtual Render3DError UpdateToonTable(const u16 *toonTableBuffer); - virtual Render3DError ClearUsingImage(const u16 *__restrict colorBuffer, const u32 *__restrict depthStencilBuffer, const bool *__restrict fogBuffer); + virtual Render3DError ClearUsingImage(const u16 *__restrict colorBuffer, const u32 *__restrict depthBuffer, const bool *__restrict fogBuffer, const u8 *__restrict polyIDBuffer); virtual Render3DError ClearUsingValues(const u8 r, const u8 g, const u8 b, const u8 a, const u32 clearDepth, const u8 clearPolyID, const bool enableFog) const; virtual Render3DError SetupPolygon(const POLY *thePoly); @@ -591,7 +591,7 @@ class OpenGLRenderer_1_3 : public OpenGLRenderer_1_2 { protected: virtual Render3DError UploadToonTable(const u16 *toonTableBuffer); - virtual Render3DError UploadClearImage(const u16 *__restrict colorBuffer, const u32 *__restrict depthStencilBuffer, const bool *__restrict fogBuffer); + virtual Render3DError UploadClearImage(const u16 *__restrict colorBuffer, const u32 *__restrict depthBuffer, const bool *__restrict fogBuffer, const u8 *__restrict polyIDBuffer); }; class OpenGLRenderer_1_4 : public OpenGLRenderer_1_3 diff --git a/desmume/src/OGLRender_3_2.cpp b/desmume/src/OGLRender_3_2.cpp index 7a4534169..1cb54b607 100644 --- a/desmume/src/OGLRender_3_2.cpp +++ b/desmume/src/OGLRender_3_2.cpp @@ -1007,11 +1007,11 @@ Render3DError OpenGLRenderer_3_2::RenderFog(const u8 *densityTable, const u32 co return OGLERROR_NOERR; } -Render3DError OpenGLRenderer_3_2::ClearUsingImage(const u16 *__restrict colorBuffer, const u32 *__restrict depthStencilBuffer, const bool *__restrict fogBuffer) +Render3DError OpenGLRenderer_3_2::ClearUsingImage(const u16 *__restrict colorBuffer, const u32 *__restrict depthBuffer, const bool *__restrict fogBuffer, const u8 *__restrict polyIDBuffer) { OGLRenderRef &OGLRef = *this->ref; - this->UploadClearImage(colorBuffer, depthStencilBuffer, fogBuffer); + this->UploadClearImage(colorBuffer, depthBuffer, fogBuffer, polyIDBuffer); if (OGLRef.selectedRenderingFBO == OGLRef.fboMSIntermediateRenderID) { diff --git a/desmume/src/OGLRender_3_2.h b/desmume/src/OGLRender_3_2.h index 50d8364e1..99f1983c7 100644 --- a/desmume/src/OGLRender_3_2.h +++ b/desmume/src/OGLRender_3_2.h @@ -81,7 +81,7 @@ protected: virtual Render3DError RenderEdgeMarking(const u16 *colorTable); virtual Render3DError RenderFog(const u8 *densityTable, const u32 color, const u32 offset, const u8 shift, const bool alphaOnly); - virtual Render3DError ClearUsingImage(const u16 *__restrict colorBuffer, const u32 *__restrict depthStencilBuffer, const bool *__restrict fogBuffer); + virtual Render3DError ClearUsingImage(const u16 *__restrict colorBuffer, const u32 *__restrict depthBuffer, const bool *__restrict fogBuffer, const u8 *__restrict polyIDBuffer); public: ~OpenGLRenderer_3_2(); diff --git a/desmume/src/rasterize.cpp b/desmume/src/rasterize.cpp index 576808690..aad9865b9 100644 --- a/desmume/src/rasterize.cpp +++ b/desmume/src/rasterize.cpp @@ -1654,7 +1654,7 @@ Render3DError SoftRasterizerRenderer::UpdateToonTable(const u16 *toonTableBuffer return RENDER3DERROR_NOERR; } -Render3DError SoftRasterizerRenderer::ClearUsingImage(const u16 *__restrict colorBuffer, const u32 *__restrict depthStencilBuffer, const bool *__restrict fogBuffer) +Render3DError SoftRasterizerRenderer::ClearUsingImage(const u16 *__restrict colorBuffer, const u32 *__restrict depthBuffer, const bool *__restrict fogBuffer, const u8 *__restrict polyIDBuffer) { // The clear image buffer is y-flipped, so we need to flip it back to normal here. for (size_t y = 0; y < this->_framebufferHeight; y++) @@ -1666,8 +1666,8 @@ Render3DError SoftRasterizerRenderer::ClearUsingImage(const u16 *__restrict colo ((u32 *)this->screenColor)[iw] = RGB15TO6665(colorBuffer[ir] & 0x7FFF, (colorBuffer[ir] >> 15) * 31); screenAttributes[iw].isFogged = fogBuffer[ir]; - screenAttributes[iw].depth = (depthStencilBuffer[ir] & 0xFFFFFF00) >> 8; - screenAttributes[iw].opaquePolyID = depthStencilBuffer[ir] & 0x000000FF; + screenAttributes[iw].depth = depthBuffer[ir]; + screenAttributes[iw].opaquePolyID = polyIDBuffer[ir]; screenAttributes[iw].translucentPolyID = kUnsetTranslucentPolyID; screenAttributes[iw].isTranslucentPoly = false; screenAttributes[iw].stencil = 0; diff --git a/desmume/src/rasterize.h b/desmume/src/rasterize.h index b7bdba35f..b99cc3b8b 100644 --- a/desmume/src/rasterize.h +++ b/desmume/src/rasterize.h @@ -81,7 +81,7 @@ protected: virtual Render3DError UpdateToonTable(const u16 *toonTableBuffer); - virtual Render3DError ClearUsingImage(const u16 *__restrict colorBuffer, const u32 *__restrict depthStencilBuffer, const bool *__restrict fogBuffer); + virtual Render3DError ClearUsingImage(const u16 *__restrict colorBuffer, const u32 *__restrict depthBuffer, const bool *__restrict fogBuffer, const u8 *__restrict polyIDBuffer); virtual Render3DError ClearUsingValues(const u8 r, const u8 g, const u8 b, const u8 a, const u32 clearDepth, const u8 clearPolyID, const bool enableFog) const; public: diff --git a/desmume/src/render3D.cpp b/desmume/src/render3D.cpp index 0e72c1e9e..ec2fdf218 100644 --- a/desmume/src/render3D.cpp +++ b/desmume/src/render3D.cpp @@ -194,9 +194,10 @@ Render3DError Render3D::ClearFramebuffer(const GFX3D_State *renderState) //this is tested quite well in the sonic chronicles main map mode //where depth values are used for trees etc you can walk behind - this->clearImageDepthStencilBuffer[dd] = dsDepthToD24S8_LUT[clearDepthBuffer[adr] & 0x7FFF] | polyID; + this->clearImageDepthBuffer[dd] = dsDepthToD24S8_LUT[clearDepthBuffer[adr] & 0x7FFF]; this->clearImageFogBuffer[dd] = BIT15(clearDepthBuffer[adr]); + this->clearImagePolyIDBuffer[dd] = polyID; dd++; } @@ -204,7 +205,7 @@ Render3DError Render3D::ClearFramebuffer(const GFX3D_State *renderState) dd -= GFX3D_FRAMEBUFFER_WIDTH * 2; } - error = this->ClearUsingImage(this->clearImageColor16Buffer, this->clearImageDepthStencilBuffer, this->clearImageFogBuffer); + error = this->ClearUsingImage(this->clearImageColor16Buffer, this->clearImageDepthBuffer, this->clearImageFogBuffer, this->clearImagePolyIDBuffer); if (error != RENDER3DERROR_NOERR) { error = this->ClearUsingValues(clearColor.r, clearColor.g, clearColor.b, clearColor.a, renderState->clearDepth, polyID, enableFog); @@ -218,7 +219,7 @@ Render3DError Render3D::ClearFramebuffer(const GFX3D_State *renderState) return error; } -Render3DError Render3D::ClearUsingImage(const u16 *__restrict colorBuffer, const u32 *__restrict depthStencilBuffer, const bool *__restrict fogBuffer) +Render3DError Render3D::ClearUsingImage(const u16 *__restrict colorBuffer, const u32 *__restrict depthBuffer, const bool *__restrict fogBuffer, const u8 *__restrict polyIDBuffer) { return RENDER3DERROR_NOERR; } @@ -246,7 +247,9 @@ Render3DError Render3D::SetupViewport(const u32 viewportValue) Render3DError Render3D::Reset() { memset(this->clearImageColor16Buffer, 0, sizeof(this->clearImageColor16Buffer)); - memset(this->clearImageDepthStencilBuffer, 0, sizeof(this->clearImageDepthStencilBuffer)); + memset(this->clearImageDepthBuffer, 0, sizeof(this->clearImageDepthBuffer)); + memset(this->clearImagePolyIDBuffer, 0, sizeof(this->clearImagePolyIDBuffer)); + memset(this->clearImageFogBuffer, 0, sizeof(this->clearImageFogBuffer)); return RENDER3DERROR_NOERR; } diff --git a/desmume/src/render3D.h b/desmume/src/render3D.h index df41b46ca..f43ba04d2 100644 --- a/desmume/src/render3D.h +++ b/desmume/src/render3D.h @@ -85,8 +85,9 @@ class Render3D { protected: CACHE_ALIGN u16 clearImageColor16Buffer[GFX3D_FRAMEBUFFER_WIDTH * GFX3D_FRAMEBUFFER_HEIGHT]; - CACHE_ALIGN u32 clearImageDepthStencilBuffer[GFX3D_FRAMEBUFFER_WIDTH * GFX3D_FRAMEBUFFER_HEIGHT]; + CACHE_ALIGN u32 clearImageDepthBuffer[GFX3D_FRAMEBUFFER_WIDTH * GFX3D_FRAMEBUFFER_HEIGHT]; CACHE_ALIGN bool clearImageFogBuffer[GFX3D_FRAMEBUFFER_WIDTH * GFX3D_FRAMEBUFFER_HEIGHT]; + CACHE_ALIGN u8 clearImagePolyIDBuffer[GFX3D_FRAMEBUFFER_WIDTH * GFX3D_FRAMEBUFFER_HEIGHT]; virtual Render3DError BeginRender(const GFX3D_State *renderState); virtual Render3DError RenderGeometry(const GFX3D_State *renderState, const VERTLIST *vertList, const POLYLIST *polyList, const INDEXLIST *indexList); @@ -97,7 +98,7 @@ protected: virtual Render3DError UpdateToonTable(const u16 *toonTableBuffer); virtual Render3DError ClearFramebuffer(const GFX3D_State *renderState); - virtual Render3DError ClearUsingImage(const u16 *__restrict colorBuffer, const u32 *__restrict depthStencilBuffer, const bool *__restrict fogBuffer); + virtual Render3DError ClearUsingImage(const u16 *__restrict colorBuffer, const u32 *__restrict depthBuffer, const bool *__restrict fogBuffer, const u8 *__restrict polyIDBuffer); virtual Render3DError ClearUsingValues(const u8 r, const u8 g, const u8 b, const u8 a, const u32 clearDepth, const u8 clearPolyID, const bool enableFog) const; virtual Render3DError SetupPolygon(const POLY *thePoly);