Skip to content

Commit

Permalink
OpenGL Renderer: Add new rendering resource classes, and do some misc…
Browse files Browse the repository at this point in the history
…. refactoring.

- Using the new OpenGLGeometryResource and OpenGLRenderStatesResource classes, the 3.2 Core Profile and ES renderers now use triple-buffering for all geometry rendering resources and framebuffer constants.
- Delete the InitFinalRenderStates() method, which has been obsoleted over the years. Its functionality has been rolled into the InitExtensions() and _RenderGeometryLoopBegin() methods.
  • Loading branch information
rogerman committed Aug 15, 2024
1 parent f84a804 commit f910c61
Show file tree
Hide file tree
Showing 7 changed files with 1,062 additions and 379 deletions.
88 changes: 27 additions & 61 deletions desmume/src/OGLRender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2384,6 +2384,14 @@ Render3DError OpenGLRenderer_1_2::InitExtensions()
}
#endif

// Mirrored Repeat Mode Support
const bool isTexMirroredRepeatSupported = this->IsVersionSupported(1, 4, 0) || this->IsExtensionPresent(&oglExtensionSet, "GL_ARB_texture_mirrored_repeat");
OGLRef.stateTexMirroredRepeat = (isTexMirroredRepeatSupported) ? GL_MIRRORED_REPEAT : GL_REPEAT;

// Blending Support
this->_isBlendFuncSeparateSupported = this->IsVersionSupported(1, 4, 0) || this->IsExtensionPresent(&oglExtensionSet, "GL_EXT_blend_func_separate");
this->_isBlendEquationSeparateSupported = this->IsVersionSupported(2, 0, 0) || this->IsExtensionPresent(&oglExtensionSet, "GL_EXT_blend_equation_separate");

// Get host GPU device properties
GLfloat maxAnisotropyOGL = 1.0f;
glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &maxAnisotropyOGL);
Expand Down Expand Up @@ -2616,8 +2624,6 @@ Render3DError OpenGLRenderer_1_2::InitExtensions()

this->_enableMultisampledRendering = ((this->_selectedMultisampleSize >= 2) && this->isMultisampledFBOSupported);

this->InitFinalRenderStates(&oglExtensionSet); // This must be done last

return OGLERROR_NOERR;
}

Expand Down Expand Up @@ -3676,40 +3682,6 @@ void OpenGLRenderer_1_2::DestroyFramebufferOutput8888Programs()
OGLRef.fragmentFramebufferRGBA8888OutputShaderID = 0;
}

Render3DError OpenGLRenderer_1_2::InitFinalRenderStates(const std::set<std::string> *oglExtensionSet)
{
OGLRenderRef &OGLRef = *this->ref;

bool isTexMirroredRepeatSupported = this->IsExtensionPresent(oglExtensionSet, "GL_ARB_texture_mirrored_repeat");
bool isBlendFuncSeparateSupported = this->IsExtensionPresent(oglExtensionSet, "GL_EXT_blend_func_separate");
bool isBlendEquationSeparateSupported = this->IsExtensionPresent(oglExtensionSet, "GL_EXT_blend_equation_separate");

// Blending Support
if (isBlendFuncSeparateSupported)
{
if (isBlendEquationSeparateSupported)
{
// we want to use alpha destination blending so we can track the last-rendered alpha value
// test: new super mario brothers renders the stormclouds at the beginning
glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_SRC_ALPHA, GL_DST_ALPHA);
glBlendEquationSeparate(GL_FUNC_ADD, GL_MAX);
}
else
{
glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_DST_ALPHA);
}
}
else
{
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}

// Mirrored Repeat Mode Support
OGLRef.stateTexMirroredRepeat = (isTexMirroredRepeatSupported) ? GL_MIRRORED_REPEAT : GL_REPEAT;

return OGLERROR_NOERR;
}

Render3DError OpenGLRenderer_1_2::InitPostprocessingPrograms(const char *edgeMarkVtxShaderCString,
const char *edgeMarkFragShaderCString,
const char *framebufferOutputVtxShaderCString,
Expand Down Expand Up @@ -4384,6 +4356,25 @@ void OpenGLRenderer_1_2::_RenderGeometryLoopBegin()
if (this->_enableAlphaBlending)
{
glEnable(GL_BLEND);

if (this->_isBlendFuncSeparateSupported)
{
if (this->_isBlendEquationSeparateSupported)
{
// we want to use alpha destination blending so we can track the last-rendered alpha value
// test: new super mario brothers renders the stormclouds at the beginning
glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_SRC_ALPHA, GL_DST_ALPHA);
glBlendEquationSeparate(GL_FUNC_ADD, GL_MAX);
}
else
{
glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_DST_ALPHA);
}
}
else
{
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
}
else
{
Expand Down Expand Up @@ -4641,9 +4632,6 @@ Render3DError OpenGLRenderer_1_2::PostprocessFramebuffer()
glDisable(GL_STENCIL_TEST);
glEnable(GL_BLEND);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_SRC_ALPHA, GL_DST_ALPHA);
glBlendEquationSeparate(GL_FUNC_ADD, GL_MAX);
}

this->_FramebufferProcessVertexAttribDisable();
Expand Down Expand Up @@ -5449,28 +5437,6 @@ 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;

// we want to use alpha destination blending so we can track the last-rendered alpha value
// test: new super mario brothers renders the stormclouds at the beginning

// Blending Support
glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_SRC_ALPHA, GL_DST_ALPHA);
glBlendEquationSeparate(GL_FUNC_ADD, GL_MAX);

// Mirrored Repeat Mode Support
OGLRef.stateTexMirroredRepeat = GL_MIRRORED_REPEAT;

// Ignore our color buffer since we'll transfer the polygon alpha through a uniform.
OGLRef.position4fBuffer = NULL;
OGLRef.texCoord2fBuffer = NULL;
OGLRef.color4fBuffer = NULL;

return OGLERROR_NOERR;
}

Render3DError OpenGLRenderer_2_0::BeginRender(const GFX3D_State &renderState, const GFX3D_GeometryList &renderGList)
{
OGLRenderRef &OGLRef = *this->ref;
Expand Down
33 changes: 22 additions & 11 deletions desmume/src/OGLRender.h
Original file line number Diff line number Diff line change
Expand Up @@ -303,10 +303,15 @@ EXTERNOGLEXT(PFNGLDELETERENDERBUFFERSEXTPROC, glDeleteRenderbuffersEXT)

#endif // GL_EXT_framebuffer_object

// Some headers, such as the OpenGL ES headers, may not include this macro.
// Some headers, such as the OpenGL ES headers, may not include this token.
// Add it manually to avoid compiling issues.
#ifndef GL_BGRA
#define GL_BGRA GL_BGRA_EXT
#define GL_BGRA GL_BGRA_EXT
#endif

// OpenGL ES headers before 3.2 don't have this token, so manually define it here.
#ifndef GL_TEXTURE_BUFFER
#define GL_TEXTURE_BUFFER 0x8C2A
#endif

// OPENGL CORE EQUIVALENTS FOR LEGACY FUNCTIONS
Expand Down Expand Up @@ -375,12 +380,25 @@ enum OpenGLVariantID
OpenGLVariantID_Unknown = 0,
OpenGLVariantID_LegacyAuto = 0x1000,
OpenGLVariantID_Legacy_1_2 = 0x1012,
OpenGLVariantID_Legacy_1_3 = 0x1013,
OpenGLVariantID_Legacy_1_4 = 0x1014,
OpenGLVariantID_Legacy_1_5 = 0x1015,
OpenGLVariantID_Legacy_2_0 = 0x1020,
OpenGLVariantID_Legacy_2_1 = 0x1021,
OpenGLVariantID_CoreProfile_3_2 = 0x2032,
OpenGLVariantID_CoreProfile_3_3 = 0x2033,
OpenGLVariantID_CoreProfile_4_0 = 0x2040,
OpenGLVariantID_CoreProfile_4_1 = 0x2041,
OpenGLVariantID_CoreProfile_4_2 = 0x2042,
OpenGLVariantID_CoreProfile_4_3 = 0x2043,
OpenGLVariantID_CoreProfile_4_4 = 0x2044,
OpenGLVariantID_CoreProfile_4_5 = 0x2045,
OpenGLVariantID_CoreProfile_4_6 = 0x2046,
OpenGLVariantID_StandardAuto = 0x3000,
OpenGLVariantID_ES3_Auto = 0x4000,
OpenGLVariantID_ES3_3_0 = 0x4030,
OpenGLVariantID_ES3_3_1 = 0x4031,
OpenGLVariantID_ES3_3_2 = 0x4032,
OpenGLVariantID_ES_Auto = 0x6000
};

Expand Down Expand Up @@ -607,12 +625,6 @@ struct OGLRenderRef
// PBO
GLuint pboRenderDataID;

// UBO / TBO
GLuint uboRenderStatesID;
GLuint uboPolyStatesID;
GLuint tboPolyStatesID;
GLuint texPolyStatesID;

// FBO
GLuint texCIColorID;
GLuint texCIFogAttrID;
Expand Down Expand Up @@ -863,6 +875,8 @@ class OpenGLRenderer : public Render3D

// OpenGL Feature Support
OpenGLVariantID _variantID;
bool _isBlendFuncSeparateSupported;
bool _isBlendEquationSeparateSupported;
bool isVBOSupported;
bool isPBOSupported;
bool isFBOSupported;
Expand Down Expand Up @@ -950,7 +964,6 @@ class OpenGLRenderer : public Render3D
virtual Render3DError CreateFramebufferOutput8888Program(const char *vtxShaderCString, const char *fragShaderCString) = 0;
virtual void DestroyFramebufferOutput8888Programs() = 0;

virtual Render3DError InitFinalRenderStates(const std::set<std::string> *oglExtensionSet) = 0;
virtual Render3DError InitPostprocessingPrograms(const char *edgeMarkVtxShader,
const char *edgeMarkFragShader,
const char *framebufferOutputVtxShader,
Expand Down Expand Up @@ -1032,7 +1045,6 @@ class OpenGLRenderer_1_2 : public OpenGLRenderer
virtual Render3DError CreateFramebufferOutput8888Program(const char *vtxShaderCString, const char *fragShaderCString);
virtual void DestroyFramebufferOutput8888Programs();

virtual Render3DError InitFinalRenderStates(const std::set<std::string> *oglExtensionSet);
virtual Render3DError InitPostprocessingPrograms(const char *edgeMarkVtxShader,
const char *edgeMarkFragShader,
const char *framebufferOutputVtxShader,
Expand Down Expand Up @@ -1089,7 +1101,6 @@ class OpenGLRenderer_2_0 : public OpenGLRenderer_1_2
OpenGLRenderer_2_0();

protected:
virtual Render3DError InitFinalRenderStates(const std::set<std::string> *oglExtensionSet);
virtual Render3DError BeginRender(const GFX3D_State &renderState, const GFX3D_GeometryList &renderGList);
};

Expand Down
Loading

0 comments on commit f910c61

Please sign in to comment.