diff --git a/desmume/src/OGLRender.cpp b/desmume/src/OGLRender.cpp index ab10f2b4a..e16a13a34 100644 --- a/desmume/src/OGLRender.cpp +++ b/desmume/src/OGLRender.cpp @@ -1302,6 +1302,34 @@ FragmentColor* OpenGLRenderer::GetFramebuffer() return (this->willConvertFramebufferOnGPU) ? this->_mappedFramebuffer : GPU->GetEngineMain()->Get3DFramebufferRGBA6665(); } +OpenGLTexture* OpenGLRenderer::GetLoadedTextureFromPolygon(const POLY &thePoly, bool enableTexturing) +{ + OpenGLTexture *theTexture = (OpenGLTexture *)texCache.GetTexture(thePoly.texParam, thePoly.texPalette); + const bool isNewTexture = (theTexture == NULL); + + if (isNewTexture) + { + theTexture = new OpenGLTexture(thePoly.texParam, thePoly.texPalette); + theTexture->SetUnpackBuffer(this->_workingTextureUnpackBuffer); + theTexture->SetDeposterizeBuffer(this->_workingTextureUnpackBuffer, this->_textureDeposterizeDstSurface.workingSurface[0]); + theTexture->SetUpscalingBuffer(this->_textureUpscaleBuffer); + + texCache.Add(theTexture); + } + + const NDSTextureFormat packFormat = theTexture->GetPackFormat(); + const bool isTextureEnabled = ( (packFormat != TEXMODE_NONE) && enableTexturing ); + + if (theTexture->IsLoadNeeded() && isTextureEnabled) + { + theTexture->SetUseDeposterize(this->_textureDeposterize); + theTexture->SetScalingFactor(this->_textureScalingFactor); + theTexture->Load(isNewTexture); + } + + return theTexture; +} + OpenGLRenderer_1_2::~OpenGLRenderer_1_2() { glFinish(); @@ -2630,6 +2658,8 @@ Render3DError OpenGLRenderer_1_2::BeginRender(const GFX3D &engine) } } } + + this->_textureList[i] = this->GetLoadedTextureFromPolygon(*thePoly, engine.renderState.enableTexturing); } if (this->isVBOSupported) @@ -2699,7 +2729,7 @@ Render3DError OpenGLRenderer_1_2::RenderGeometry(const GFX3D_State &renderState, u32 lastViewport = firstPoly.viewport; this->SetupPolygon(firstPoly); - this->SetupTexture(firstPoly, renderState.enableTexturing); + this->SetupTexture(firstPoly, 0, renderState.enableTexturing); this->SetupViewport(lastViewport); GLsizei vertIndexCount = 0; @@ -2722,7 +2752,7 @@ Render3DError OpenGLRenderer_1_2::RenderGeometry(const GFX3D_State &renderState, { lastTexParams = thePoly.texParam; lastTexPalette = thePoly.texPalette; - this->SetupTexture(thePoly, renderState.enableTexturing); + this->SetupTexture(thePoly, i, renderState.enableTexturing); } // Set up the viewport if it changed @@ -3022,13 +3052,14 @@ Render3DError OpenGLRenderer_1_2::SetupPolygon(const POLY &thePoly) return OGLERROR_NOERR; } -Render3DError OpenGLRenderer_1_2::SetupTexture(const POLY &thePoly, bool enableTexturing) +Render3DError OpenGLRenderer_1_2::SetupTexture(const POLY &thePoly, size_t polyRenderIndex, bool enableTexturing) { - OGLRenderRef &OGLRef = *this->ref; - const PolygonTexParams params = thePoly.getTexParams(); + OpenGLTexture *theTexture = this->_textureList[polyRenderIndex]; + const NDSTextureFormat packFormat = theTexture->GetPackFormat(); + const OGLRenderRef &OGLRef = *this->ref; // Check if we need to use textures - if (params.texFormat == TEXMODE_NONE || !enableTexturing) + if (packFormat == TEXMODE_NONE || !enableTexturing) { if (this->isShaderSupported) { @@ -3043,27 +3074,7 @@ Render3DError OpenGLRenderer_1_2::SetupTexture(const POLY &thePoly, bool enableT return OGLERROR_NOERR; } - OpenGLTexture *theTexture = (OpenGLTexture *)texCache.GetTexture(thePoly.texParam, thePoly.texPalette); - const bool isNewTexture = (theTexture == NULL); - - if (isNewTexture) - { - theTexture = new OpenGLTexture(thePoly.texParam, thePoly.texPalette); - theTexture->SetUnpackBuffer(this->_workingTextureUnpackBuffer); - theTexture->SetDeposterizeBuffer(this->_workingTextureUnpackBuffer, this->_textureDeposterizeDstSurface.workingSurface[0]); - theTexture->SetUpscalingBuffer(this->_textureUpscaleBuffer); - - texCache.Add(theTexture); - } - - if (theTexture->IsLoadNeeded()) - { - theTexture->SetUseDeposterize(this->_textureDeposterize); - theTexture->SetScalingFactor(this->_textureScalingFactor); - theTexture->Load(isNewTexture); - } - - const NDSTextureFormat packFormat = theTexture->GetPackFormat(); + const PolygonTexParams texParams = thePoly.getTexParams(); // Enable textures if they weren't already enabled if (this->isShaderSupported) @@ -3081,8 +3092,8 @@ Render3DError OpenGLRenderer_1_2::SetupTexture(const POLY &thePoly, bool enableT } glBindTexture(GL_TEXTURE_2D, theTexture->GetID()); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, (params.enableRepeatS ? (params.enableMirroredRepeatS ? OGLRef.stateTexMirroredRepeat : GL_REPEAT) : GL_CLAMP_TO_EDGE)); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, (params.enableRepeatT ? (params.enableMirroredRepeatT ? OGLRef.stateTexMirroredRepeat : GL_REPEAT) : GL_CLAMP_TO_EDGE)); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, (texParams.enableRepeatS ? (texParams.enableMirroredRepeatS ? OGLRef.stateTexMirroredRepeat : GL_REPEAT) : GL_CLAMP_TO_EDGE)); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, (texParams.enableRepeatT ? (texParams.enableMirroredRepeatT ? OGLRef.stateTexMirroredRepeat : GL_REPEAT) : GL_CLAMP_TO_EDGE)); if (this->_textureSmooth) { @@ -3737,6 +3748,8 @@ Render3DError OpenGLRenderer_1_5::BeginRender(const GFX3D &engine) } } } + + this->_textureList[i] = this->GetLoadedTextureFromPolygon(*thePoly, engine.renderState.enableTexturing); } glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER); @@ -4394,6 +4407,8 @@ Render3DError OpenGLRenderer_2_0::BeginRender(const GFX3D &engine) } } } + + this->_textureList[i] = this->GetLoadedTextureFromPolygon(*thePoly, engine.renderState.enableTexturing); } glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER); @@ -4629,48 +4644,29 @@ Render3DError OpenGLRenderer_2_0::SetupPolygon(const POLY &thePoly) return OGLERROR_NOERR; } -Render3DError OpenGLRenderer_2_0::SetupTexture(const POLY &thePoly, bool enableTexturing) +Render3DError OpenGLRenderer_2_0::SetupTexture(const POLY &thePoly, size_t polyRenderIndex, bool enableTexturing) { - OGLRenderRef &OGLRef = *this->ref; - const PolygonTexParams params = thePoly.getTexParams(); + OpenGLTexture *theTexture = this->_textureList[polyRenderIndex]; + const NDSTextureFormat packFormat = theTexture->GetPackFormat(); + const OGLRenderRef &OGLRef = *this->ref; // Check if we need to use textures - if (params.texFormat == TEXMODE_NONE || !enableTexturing) + if (packFormat == TEXMODE_NONE || !enableTexturing) { glUniform1i(OGLRef.uniformPolyEnableTexture, GL_FALSE); glUniform1i(OGLRef.uniformTexSingleBitAlpha, GL_FALSE); return OGLERROR_NOERR; } - OpenGLTexture *theTexture = (OpenGLTexture *)texCache.GetTexture(thePoly.texParam, thePoly.texPalette); - const bool isNewTexture = (theTexture == NULL); - - if (isNewTexture) - { - theTexture = new OpenGLTexture(thePoly.texParam, thePoly.texPalette); - theTexture->SetUnpackBuffer(this->_workingTextureUnpackBuffer); - theTexture->SetDeposterizeBuffer(this->_workingTextureUnpackBuffer, this->_textureDeposterizeDstSurface.workingSurface[0]); - theTexture->SetUpscalingBuffer(this->_textureUpscaleBuffer); - - texCache.Add(theTexture); - } - - if (theTexture->IsLoadNeeded()) - { - theTexture->SetUseDeposterize(this->_textureDeposterize); - theTexture->SetScalingFactor(this->_textureScalingFactor); - theTexture->Load(isNewTexture); - } - - const NDSTextureFormat packFormat = theTexture->GetPackFormat(); + const PolygonTexParams texParams = thePoly.getTexParams(); glUniform1i(OGLRef.uniformPolyEnableTexture, GL_TRUE); glUniform1i(OGLRef.uniformTexSingleBitAlpha, (packFormat != TEXMODE_A3I5 && packFormat != TEXMODE_A5I3) ? GL_TRUE : GL_FALSE); glUniform2f(OGLRef.uniformPolyTexScale, theTexture->GetInvWidth(), theTexture->GetInvHeight()); glBindTexture(GL_TEXTURE_2D, theTexture->GetID()); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, (params.enableRepeatS ? (params.enableMirroredRepeatS ? GL_MIRRORED_REPEAT : GL_REPEAT) : GL_CLAMP_TO_EDGE)); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, (params.enableRepeatT ? (params.enableMirroredRepeatT ? GL_MIRRORED_REPEAT : GL_REPEAT) : GL_CLAMP_TO_EDGE)); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, (texParams.enableRepeatS ? (texParams.enableMirroredRepeatS ? GL_MIRRORED_REPEAT : GL_REPEAT) : GL_CLAMP_TO_EDGE)); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, (texParams.enableRepeatT ? (texParams.enableMirroredRepeatT ? GL_MIRRORED_REPEAT : GL_REPEAT) : GL_CLAMP_TO_EDGE)); if (this->_textureSmooth) { diff --git a/desmume/src/OGLRender.h b/desmume/src/OGLRender.h index 1693bba63..fef00892c 100644 --- a/desmume/src/OGLRender.h +++ b/desmume/src/OGLRender.h @@ -611,8 +611,10 @@ protected: bool _pixelReadNeedsFinish; size_t _currentPolyIndex; std::vector _shadowPolyID; + OpenGLTexture *_textureList[POLYLIST_SIZE]; Render3DError FlushFramebuffer(const FragmentColor *__restrict srcFramebuffer, FragmentColor *__restrict dstFramebuffer, u16 *__restrict dstRGBA5551); + OpenGLTexture* GetLoadedTextureFromPolygon(const POLY &thePoly, bool enableTexturing); // OpenGL-specific methods virtual Render3DError CreateVBOs() = 0; @@ -731,7 +733,7 @@ protected: virtual void SetPolygonIndex(const size_t index); virtual Render3DError SetupPolygon(const POLY &thePoly); - virtual Render3DError SetupTexture(const POLY &thePoly, bool enableTexturing); + virtual Render3DError SetupTexture(const POLY &thePoly, size_t polyRenderIndex, bool enableTexturing); virtual Render3DError SetupViewport(const u32 viewportValue); public: @@ -804,7 +806,7 @@ protected: virtual Render3DError RenderFog(const u8 *densityTable, const u32 color, const u32 offset, const u8 shift, const bool alphaOnly); virtual Render3DError SetupPolygon(const POLY &thePoly); - virtual Render3DError SetupTexture(const POLY &thePoly, bool enableTexturing); + virtual Render3DError SetupTexture(const POLY &thePoly, size_t polyRenderIndex, bool enableTexturing); }; class OpenGLRenderer_2_1 : public OpenGLRenderer_2_0 diff --git a/desmume/src/OGLRender_3_2.cpp b/desmume/src/OGLRender_3_2.cpp index 485fecb59..d77c4b0af 100644 --- a/desmume/src/OGLRender_3_2.cpp +++ b/desmume/src/OGLRender_3_2.cpp @@ -1400,19 +1400,6 @@ Render3DError OpenGLRenderer_3_2::BeginRender(const GFX3D &engine) { const POLY *thePoly = &engine.polylist->list[engine.indexlist.list[i]]; const size_t polyType = thePoly->type; - PolygonAttributes polyAttr = thePoly->getAttributes(); - PolygonTexParams texParams = thePoly->getTexParams(); - - polyStates[i].enableTexture = (texParams.texFormat != TEXMODE_NONE && engine.renderState.enableTexturing) ? GL_TRUE : GL_FALSE; - polyStates[i].enableFog = (polyAttr.enableRenderFog && !(polyAttr.polygonMode == POLYGON_MODE_SHADOW && polyAttr.polygonID == 0)) ? GL_TRUE : GL_FALSE; - polyStates[i].enableDepthWrite = !(polyAttr.polygonMode == POLYGON_MODE_SHADOW && polyAttr.polygonID == 0) ? GL_TRUE : GL_FALSE; - polyStates[i].setNewDepthForTranslucent = (polyAttr.enableAlphaDepthWrite) ? GL_TRUE : GL_FALSE; - polyStates[i].polyAlpha = (!polyAttr.isWireframe && polyAttr.isTranslucent) ? polyAttr.alpha : 0x1F; - polyStates[i].polyMode = polyAttr.polygonMode; - polyStates[i].polyID = polyAttr.polygonID; - polyStates[i].texSizeS = texParams.sizeS; - polyStates[i].texSizeT = texParams.sizeT; - polyStates[i].texSingleBitAlpha = (texParams.texFormat != TEXMODE_A3I5 && texParams.texFormat != TEXMODE_A5I3) ? GL_TRUE : GL_FALSE; for (size_t j = 0; j < polyType; j++) { @@ -1435,6 +1422,23 @@ Render3DError OpenGLRenderer_3_2::BeginRender(const GFX3D &engine) } } } + + this->_textureList[i] = this->GetLoadedTextureFromPolygon(*thePoly, engine.renderState.enableTexturing); + + const NDSTextureFormat packFormat = this->_textureList[i]->GetPackFormat(); + const PolygonAttributes polyAttr = thePoly->getAttributes(); + const PolygonTexParams texParams = thePoly->getTexParams(); + + polyStates[i].enableTexture = (packFormat != TEXMODE_NONE && engine.renderState.enableTexturing) ? GL_TRUE : GL_FALSE; + polyStates[i].enableFog = (polyAttr.enableRenderFog && !(polyAttr.polygonMode == POLYGON_MODE_SHADOW && polyAttr.polygonID == 0)) ? GL_TRUE : GL_FALSE; + polyStates[i].enableDepthWrite = !(polyAttr.polygonMode == POLYGON_MODE_SHADOW && polyAttr.polygonID == 0) ? GL_TRUE : GL_FALSE; + polyStates[i].setNewDepthForTranslucent = (polyAttr.enableAlphaDepthWrite) ? GL_TRUE : GL_FALSE; + polyStates[i].polyAlpha = (!polyAttr.isWireframe && polyAttr.isTranslucent) ? polyAttr.alpha : 0x1F; + polyStates[i].polyMode = polyAttr.polygonMode; + polyStates[i].polyID = polyAttr.polygonID; + polyStates[i].texSizeS = texParams.sizeS; // Note that we are using the packed version of sizeS + polyStates[i].texSizeT = texParams.sizeT; // Note that we are using the packed version of sizeT + polyStates[i].texSingleBitAlpha = (packFormat != TEXMODE_A3I5 && packFormat != TEXMODE_A5I3) ? GL_TRUE : GL_FALSE; } glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER); @@ -1681,39 +1685,21 @@ Render3DError OpenGLRenderer_3_2::SetupPolygon(const POLY &thePoly) return OGLERROR_NOERR; } -Render3DError OpenGLRenderer_3_2::SetupTexture(const POLY &thePoly, bool enableTexturing) +Render3DError OpenGLRenderer_3_2::SetupTexture(const POLY &thePoly, size_t polyRenderIndex, bool enableTexturing) { - const PolygonTexParams params = thePoly.getTexParams(); + OpenGLTexture *theTexture = this->_textureList[polyRenderIndex]; // Check if we need to use textures - if (params.texFormat == TEXMODE_NONE || !enableTexturing) + if (theTexture->GetPackFormat() == TEXMODE_NONE || !enableTexturing) { return OGLERROR_NOERR; } - OpenGLTexture *theTexture = (OpenGLTexture *)texCache.GetTexture(thePoly.texParam, thePoly.texPalette); - const bool isNewTexture = (theTexture == NULL); - - if (isNewTexture) - { - theTexture = new OpenGLTexture(thePoly.texParam, thePoly.texPalette); - theTexture->SetUnpackBuffer(this->_workingTextureUnpackBuffer); - theTexture->SetDeposterizeBuffer(this->_workingTextureUnpackBuffer, this->_textureDeposterizeDstSurface.workingSurface[0]); - theTexture->SetUpscalingBuffer(this->_textureUpscaleBuffer); - - texCache.Add(theTexture); - } - - if (theTexture->IsLoadNeeded()) - { - theTexture->SetUseDeposterize(this->_textureDeposterize); - theTexture->SetScalingFactor(this->_textureScalingFactor); - theTexture->Load(isNewTexture); - } + PolygonTexParams texParams = thePoly.getTexParams(); glBindTexture(GL_TEXTURE_2D, theTexture->GetID()); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, (params.enableRepeatS ? (params.enableMirroredRepeatS ? GL_MIRRORED_REPEAT : GL_REPEAT) : GL_CLAMP_TO_EDGE)); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, (params.enableRepeatT ? (params.enableMirroredRepeatT ? GL_MIRRORED_REPEAT : GL_REPEAT) : GL_CLAMP_TO_EDGE)); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, (texParams.enableRepeatS ? (texParams.enableMirroredRepeatS ? GL_MIRRORED_REPEAT : GL_REPEAT) : GL_CLAMP_TO_EDGE)); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, (texParams.enableRepeatT ? (texParams.enableMirroredRepeatT ? GL_MIRRORED_REPEAT : GL_REPEAT) : GL_CLAMP_TO_EDGE)); if (this->_textureSmooth) { diff --git a/desmume/src/OGLRender_3_2.h b/desmume/src/OGLRender_3_2.h index 2cee2a163..33fdc9a9d 100644 --- a/desmume/src/OGLRender_3_2.h +++ b/desmume/src/OGLRender_3_2.h @@ -98,7 +98,7 @@ protected: virtual void SetPolygonIndex(const size_t index); virtual Render3DError SetupPolygon(const POLY &thePoly); - virtual Render3DError SetupTexture(const POLY &thePoly, bool enableTexturing); + virtual Render3DError SetupTexture(const POLY &thePoly, size_t polyRenderIndex, bool enableTexturing); virtual Render3DError SetFramebufferSize(size_t w, size_t h); public: diff --git a/desmume/src/render3D.cpp b/desmume/src/render3D.cpp index e45139dfb..f6ad44db4 100644 --- a/desmume/src/render3D.cpp +++ b/desmume/src/render3D.cpp @@ -607,7 +607,7 @@ Render3DError Render3D::SetupPolygon(const POLY &thePoly) return RENDER3DERROR_NOERR; } -Render3DError Render3D::SetupTexture(const POLY &thePoly, bool enableTexturing) +Render3DError Render3D::SetupTexture(const POLY &thePoly, size_t polyRenderIndex, bool enableTexturing) { return RENDER3DERROR_NOERR; } diff --git a/desmume/src/render3D.h b/desmume/src/render3D.h index ea433cc77..cf61ad9f0 100644 --- a/desmume/src/render3D.h +++ b/desmume/src/render3D.h @@ -175,7 +175,7 @@ protected: virtual Render3DError ClearUsingValues(const FragmentColor &clearColor6665, const FragmentAttributes &clearAttributes) const; virtual Render3DError SetupPolygon(const POLY &thePoly); - virtual Render3DError SetupTexture(const POLY &thePoly, bool enableTexturing); + virtual Render3DError SetupTexture(const POLY &thePoly, size_t polyRenderIndex, bool enableTexturing); virtual Render3DError SetupViewport(const u32 viewportValue); public: