Skip to content

Commit

Permalink
OpenGL Renderer: Do some minor tune-ups.
Browse files Browse the repository at this point in the history
- Remove some methods in OpenGLRenderer_2_0 and OpenGLRenderer_2_1 that have negligible contribution to either performance or code simplicity.
- OpenGLRenderer_1_2, OpenGLRenderer_2_0, and OpenGLRenderer_2_1 are now instantiated with their specific variant IDs.
- Calls to malloc_alignedCacheLine() have been replaced with the more appropriate malloc_alignedPage().
- Do some misc. code cleanup.
  • Loading branch information
rogerman committed Aug 10, 2024
1 parent c75c1ce commit ea648f7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 88 deletions.
98 changes: 14 additions & 84 deletions desmume/src/OGLRender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1290,7 +1290,7 @@ OpenGLRenderer::OpenGLRenderer()
memset(ref, 0, sizeof(OGLRenderRef));

_mappedFramebuffer = NULL;
_workingTextureUnpackBuffer = (Color4u8 *)malloc_alignedCacheLine(1024 * 1024 * sizeof(Color4u8));
_workingTextureUnpackBuffer = (Color4u8 *)malloc_alignedPage(1024 * 1024 * sizeof(Color4u8));
_pixelReadNeedsFinish = false;
_needsZeroDstAlphaPass = true;
_currentPolyIndex = 0;
Expand Down Expand Up @@ -2292,6 +2292,8 @@ Render3DError OpenGLRenderer::ApplyRenderingSettings(const GFX3D_State &renderSt

OpenGLRenderer_1_2::OpenGLRenderer_1_2()
{
_variantID = OpenGLVariantID_Legacy_1_2;

_geometryDrawBuffersEnum = GeometryDrawBuffersEnumStandard;
_geometryAttachmentWorkingBuffer = GeometryAttachmentWorkingBufferStandard;
_geometryAttachmentPolyID = GeometryAttachmentPolyIDStandard;
Expand Down Expand Up @@ -5167,7 +5169,7 @@ Render3DError OpenGLRenderer_1_2::Reset()
{
OGLRenderRef &OGLRef = *this->ref;

if(!BEGINGL())
if (!BEGINGL())
{
return OGLERROR_BEGINGL_FAILED;
}
Expand Down Expand Up @@ -5227,7 +5229,7 @@ Render3DError OpenGLRenderer_1_2::RenderPowerOff()
memset(GPU->GetEngineMain()->Get3DFramebufferMain(), 0, this->_framebufferColorSizeBytes);
memset(GPU->GetEngineMain()->Get3DFramebuffer16(), 0, this->_framebufferPixCount * sizeof(u16));

if(!BEGINGL())
if (!BEGINGL())
{
return OGLERROR_BEGINGL_FAILED;
}
Expand Down Expand Up @@ -5272,7 +5274,7 @@ Render3DError OpenGLRenderer_1_2::RenderFinish()
{
this->_pixelReadNeedsFinish = false;

if(!BEGINGL())
if (!BEGINGL())
{
return OGLERROR_BEGINGL_FAILED;
}
Expand Down Expand Up @@ -5394,7 +5396,7 @@ Render3DError OpenGLRenderer_1_2::SetFramebufferSize(size_t w, size_t h)
else
{
Color4u8 *oldFramebufferColor = this->_framebufferColor;
Color4u8 *newFramebufferColor = (Color4u8 *)malloc_alignedCacheLine(newFramebufferColorSizeBytes);
Color4u8 *newFramebufferColor = (Color4u8 *)malloc_alignedPage(newFramebufferColorSizeBytes);
this->_framebufferColor = newFramebufferColor;
free_aligned(oldFramebufferColor);
}
Expand Down Expand Up @@ -5442,6 +5444,11 @@ Render3DError OpenGLRenderer_1_2::SetFramebufferSize(size_t w, size_t h)
return error;
}

OpenGLRenderer_2_0::OpenGLRenderer_2_0()
{
_variantID = OpenGLVariantID_Legacy_2_0;
}

Render3DError OpenGLRenderer_2_0::InitFinalRenderStates(const std::set<std::string> *oglExtensionSet)
{
OGLRenderRef &OGLRef = *this->ref;
Expand Down Expand Up @@ -5674,86 +5681,9 @@ Render3DError OpenGLRenderer_2_0::BeginRender(const GFX3D_State &renderState, co
return OGLERROR_NOERR;
}

Render3DError OpenGLRenderer_2_0::SetupTexture(const POLY &thePoly, size_t polyRenderIndex)
OpenGLRenderer_2_1::OpenGLRenderer_2_1()
{
OpenGLTexture *theTexture = (OpenGLTexture *)this->_textureList[polyRenderIndex];
const NDSTextureFormat packFormat = theTexture->GetPackFormat();
const OGLRenderRef &OGLRef = *this->ref;

glUniform2f(OGLRef.uniformPolyTexScale[this->_geometryProgramFlags.value], theTexture->GetInvWidth(), theTexture->GetInvHeight());

// Check if we need to use textures
if (!theTexture->IsSamplingEnabled())
{
glUniform1i(OGLRef.uniformPolyEnableTexture[this->_geometryProgramFlags.value], GL_FALSE);
glUniform1i(OGLRef.uniformTexSingleBitAlpha[this->_geometryProgramFlags.value], GL_FALSE);
return OGLERROR_NOERR;
}

glUniform1i(OGLRef.uniformPolyEnableTexture[this->_geometryProgramFlags.value], GL_TRUE);
glUniform1i(OGLRef.uniformTexSingleBitAlpha[this->_geometryProgramFlags.value], (packFormat != TEXMODE_A3I5 && packFormat != TEXMODE_A5I3) ? GL_TRUE : GL_FALSE);

glBindTexture(GL_TEXTURE_2D, theTexture->GetID());
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, ((thePoly.texParam.RepeatS_Enable) ? ((thePoly.texParam.MirroredRepeatS_Enable) ? GL_MIRRORED_REPEAT : GL_REPEAT) : GL_CLAMP_TO_EDGE));
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, ((thePoly.texParam.RepeatT_Enable) ? ((thePoly.texParam.MirroredRepeatT_Enable) ? GL_MIRRORED_REPEAT : GL_REPEAT) : GL_CLAMP_TO_EDGE));

if (this->_enableTextureSmoothing)
{
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, (this->_textureScalingFactor > 1) ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, this->_deviceInfo.maxAnisotropy);
}
else
{
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 1.0f);
}

theTexture->ResetCacheAge();
theTexture->IncreaseCacheUsageCount(1);

return OGLERROR_NOERR;
}

Render3DError OpenGLRenderer_2_1::RenderFinish()
{
if (!this->_renderNeedsFinish)
{
return OGLERROR_NOERR;
}

if (this->_pixelReadNeedsFinish)
{
this->_pixelReadNeedsFinish = false;

if(!BEGINGL())
{
return OGLERROR_BEGINGL_FAILED;
}
this->_mappedFramebuffer = (Color4u8 *__restrict)glMapBuffer(GL_PIXEL_PACK_BUFFER, GL_READ_ONLY);
ENDGL();
}

this->_renderNeedsFlushMain = true;
this->_renderNeedsFlush16 = true;

return OGLERROR_NOERR;
}

Render3DError OpenGLRenderer_2_1::RenderFlush(bool willFlushBuffer32, bool willFlushBuffer16)
{
if (!this->_isPoweredOn)
{
return RENDER3DERROR_NOERR;
}

Color4u8 *framebufferMain = (willFlushBuffer32) ? GPU->GetEngineMain()->Get3DFramebufferMain() : NULL;
u16 *framebuffer16 = (willFlushBuffer16) ? GPU->GetEngineMain()->Get3DFramebuffer16() : NULL;

this->FlushFramebuffer(this->_mappedFramebuffer, framebufferMain, framebuffer16);

return RENDER3DERROR_NOERR;
_variantID = OpenGLVariantID_Legacy_2_1;
}

template size_t OpenGLRenderer::DrawPolygonsForIndexRange<OGLPolyDrawMode_DrawOpaquePolys>(const POLY *rawPolyList, const CPoly *clippedPolyList, const size_t clippedPolyCount, size_t firstIndex, size_t lastIndex, size_t &indexOffset, POLYGON_ATTR &lastPolyAttr);
Expand Down
8 changes: 4 additions & 4 deletions desmume/src/OGLRender.h
Original file line number Diff line number Diff line change
Expand Up @@ -1077,22 +1077,22 @@ class OpenGLRenderer_1_2 : public OpenGLRenderer

class OpenGLRenderer_2_0 : public OpenGLRenderer_1_2
{
public:
OpenGLRenderer_2_0();

protected:
virtual Render3DError InitFinalRenderStates(const std::set<std::string> *oglExtensionSet);

virtual Render3DError EnableVertexAttributes();
virtual Render3DError DisableVertexAttributes();

virtual Render3DError BeginRender(const GFX3D_State &renderState, const GFX3D_GeometryList &renderGList);

virtual Render3DError SetupTexture(const POLY &thePoly, size_t polyRenderIndex);
};

class OpenGLRenderer_2_1 : public OpenGLRenderer_2_0
{
public:
virtual Render3DError RenderFinish();
virtual Render3DError RenderFlush(bool willFlushBuffer32, bool willFlushBuffer16);
OpenGLRenderer_2_1();
};

#endif // OGLRENDER_H

0 comments on commit ea648f7

Please sign in to comment.