Skip to content

Commit

Permalink
OpenGL: Handle legacy functions and tokens more gracefully for OpenGL…
Browse files Browse the repository at this point in the history
… variants that may not have them.
  • Loading branch information
rogerman committed Jul 6, 2024
1 parent 76fe5f7 commit c962c55
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 18 deletions.
17 changes: 2 additions & 15 deletions desmume/src/OGLRender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,6 @@
#define OGL_TEXTURE_SRC_CI_FOG GL_UNSIGNED_BYTE
#define OGL_TEXTURE_SRC_EDGE_COLOR GL_UNSIGNED_BYTE
#define OGL_TEXTURE_SRC_TOON_TABLE GL_UNSIGNED_SHORT_5_5_5_1

#define GL_TEXTURE_1D GL_TEXTURE_2D
#define glTexSubImage1D(target, level, xoffset, width, format, type, pixels)

#define glClearDepth(depth) glClearDepthf(depth)
#else
#error Unknown OpenGL variant.
#endif
Expand Down Expand Up @@ -229,7 +224,7 @@ OGLEXT(PFNGLDELETEBUFFERSPROC, glDeleteBuffers) // Core in v1.5
OGLEXT(PFNGLBINDBUFFERPROC, glBindBuffer) // Core in v1.5
OGLEXT(PFNGLBUFFERDATAPROC, glBufferData) // Core in v1.5
OGLEXT(PFNGLBUFFERSUBDATAPROC, glBufferSubData) // Core in v1.5
#if !defined(GL_ES_VERSION_3_0)
#if defined(GL_VERSION_1_5)
OGLEXT(PFNGLMAPBUFFERPROC, glMapBuffer) // Core in v1.5
#endif
OGLEXT(PFNGLUNMAPBUFFERPROC, glUnmapBuffer) // Core in v1.5
Expand Down Expand Up @@ -304,7 +299,7 @@ static void OGLLoadEntryPoints_Legacy()
INITOGLEXT(PFNGLBINDBUFFERPROC, glBindBuffer) // Core in v1.5
INITOGLEXT(PFNGLBUFFERDATAPROC, glBufferData) // Core in v1.5
INITOGLEXT(PFNGLBUFFERSUBDATAPROC, glBufferSubData) // Core in v1.5
#if !defined(GL_ES_VERSION_3_0)
#if defined(GL_VERSION_1_5)
INITOGLEXT(PFNGLMAPBUFFERPROC, glMapBuffer) // Core in v1.5
#endif
INITOGLEXT(PFNGLUNMAPBUFFERPROC, glUnmapBuffer) // Core in v1.5
Expand Down Expand Up @@ -2795,9 +2790,7 @@ Render3DError OpenGLRenderer_1_2::CreatePBOs()
glGenBuffers(1, &OGLRef.pboRenderDataID);
glBindBuffer(GL_PIXEL_PACK_BUFFER, OGLRef.pboRenderDataID);
glBufferData(GL_PIXEL_PACK_BUFFER, this->_framebufferColorSizeBytes, NULL, GL_STREAM_READ);
#if !defined(GL_ES_VERSION_3_0)
this->_mappedFramebuffer = (Color4u8 *__restrict)glMapBuffer(GL_PIXEL_PACK_BUFFER, GL_READ_ONLY);
#endif

return OGLERROR_NOERR;
}
Expand Down Expand Up @@ -5381,9 +5374,7 @@ Render3DError OpenGLRenderer_1_2::RenderFinish()

if (this->isPBOSupported)
{
#if !defined(GL_ES_VERSION_3_0)
this->_mappedFramebuffer = (Color4u8 *__restrict)glMapBuffer(GL_PIXEL_PACK_BUFFER, GL_READ_ONLY);
#endif
}
else
{
Expand Down Expand Up @@ -5452,9 +5443,7 @@ Render3DError OpenGLRenderer_1_2::SetFramebufferSize(size_t w, size_t h)

if (this->_mappedFramebuffer != NULL)
{
#if !defined(GL_ES_VERSION_3_0)
this->_mappedFramebuffer = (Color4u8 *__restrict)glMapBuffer(GL_PIXEL_PACK_BUFFER, GL_READ_ONLY);
#endif
glFinish();
}
}
Expand Down Expand Up @@ -5795,9 +5784,7 @@ Render3DError OpenGLRenderer_2_1::RenderFinish()
{
return OGLERROR_BEGINGL_FAILED;
}
#if !defined(GL_ES_VERSION_3_0)
this->_mappedFramebuffer = (Color4u8 *__restrict)glMapBuffer(GL_PIXEL_PACK_BUFFER, GL_READ_ONLY);
#endif
ENDGL();
}

Expand Down
40 changes: 37 additions & 3 deletions desmume/src/OGLRender.h
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ EXTERNOGLEXT(PFNGLDELETEBUFFERSPROC, glDeleteBuffers) // Core in v1.5
EXTERNOGLEXT(PFNGLBINDBUFFERPROC, glBindBuffer) // Core in v1.5
EXTERNOGLEXT(PFNGLBUFFERDATAPROC, glBufferData) // Core in v1.5
EXTERNOGLEXT(PFNGLBUFFERSUBDATAPROC, glBufferSubData) // Core in v1.5
#if !defined(GL_ES_VERSION_3_0)
#if defined(GL_VERSION_1_5)
EXTERNOGLEXT(PFNGLMAPBUFFERPROC, glMapBuffer) // Core in v1.5
#endif
EXTERNOGLEXT(PFNGLUNMAPBUFFERPROC, glUnmapBuffer) // Core in v1.5
Expand All @@ -199,8 +199,10 @@ EXTERNOGLEXT(PFNGLCLEARBUFFERFVPROC, glClearBufferfv) // Core in v3.0 and ES v3.
EXTERNOGLEXT(PFNGLCLEARBUFFERFIPROC, glClearBufferfi) // Core in v3.0 and ES v3.0

// Shaders
#if !defined(GL_ES_VERSION_3_0)
#if defined(GL_VERSION_3_0)
EXTERNOGLEXT(PFNGLBINDFRAGDATALOCATIONPROC, glBindFragDataLocation) // Core in v3.0, not available in ES
#endif
#if defined(GL_VERSION_3_3) || defined(GL_ARB_blend_func_extended)
EXTERNOGLEXT(PFNGLBINDFRAGDATALOCATIONINDEXEDPROC, glBindFragDataLocationIndexed) // Core in v3.3, not available in ES
#endif

Expand All @@ -222,7 +224,7 @@ EXTERNOGLEXT(PFNGLBINDRENDERBUFFERPROC, glBindRenderbuffer) // Core in v3.0 and
EXTERNOGLEXT(PFNGLRENDERBUFFERSTORAGEPROC, glRenderbufferStorage) // Core in v3.0 and ES v2.0
EXTERNOGLEXT(PFNGLRENDERBUFFERSTORAGEMULTISAMPLEPROC, glRenderbufferStorageMultisample) // Core in v3.0 and ES v3.0
EXTERNOGLEXT(PFNGLDELETERENDERBUFFERSPROC, glDeleteRenderbuffers) // Core in v3.0 and ES v2.0
#if !defined(GL_ES_VERSION_3_0)
#if defined(GL_VERSION_3_2) || defined(GL_ARB_texture_multisample)
EXTERNOGLEXT(PFNGLTEXIMAGE2DMULTISAMPLEPROC, glTexImage2DMultisample) // Core in v3.2, not available in ES
#endif

Expand Down Expand Up @@ -304,6 +306,38 @@ EXTERNOGLEXT(PFNGLDELETERENDERBUFFERSEXTPROC, glDeleteRenderbuffersEXT)

#endif // GL_EXT_framebuffer_object

// OPENGL CORE EQUIVALENTS FOR LEGACY FUNCTIONS
// Some OpenGL variants, such as OpenGL ES, do not include certain legacy functions in their
// API. The loss of these functions will cause compile time errors when referenced, and so
// we will redeclare them to something that is supported for the sake of compiling.
//
// Do note that a redeclared function will most likely not work in exactly the same way as
// a native legacy version, and so implmentations are responsible for overriding any methods
// that use these legacy functions to whatever is available in the specific OpenGL variant.

#ifndef GL_VERSION_1_2
// These legacy functions can be promoted to later core equivalents without any further
// modification. In other words, these are one-to-one drop-in replacements.
#define glClearDepth(depth) glClearDepthf(depth)
#define glDrawBuffer(x) glDrawBuffers(1, ((GLenum[]){x}))

// 1D textures may not exist for a particular OpenGL variant, so they will be promoted to
// 2D textures instead. Implementations need to modify their GLSL shaders accordingly to
// treat any 1D textures as 2D textures instead.
#define GL_TEXTURE_1D GL_TEXTURE_2D
#define glTexImage1D(target, level, internalformat, width, border, format, type, pixels) glTexImage2D(target, level, internalformat, width, 1, border, format, type, pixels)
#define glTexSubImage1D(target, level, xoffset, width, format, type, pixels) glTexSubImage2D(target, level, xoffset, 0, width, 1, format, type, pixels)
#endif

#ifndef GL_VERSION_1_5
// Calling glMapBuffer with an OpenGL variant that forgoes legacy functions will cause a
// GL_INVALID_OPERATION error if used in practice. Implementations need to override any
// methods that would call glMapBuffer with glMapBufferRange.
#define GL_READ_ONLY GL_MAP_READ_BIT
#define GL_WRITE_ONLY GL_MAP_WRITE_BIT
#define glMapBuffer(target, access) glMapBufferRange(target, 0, 0, access)
#endif

// Define the minimum required OpenGL version for the driver to support
#if defined(OPENGL_VARIANT_STANDARD)
#define OGLRENDER_MINIMUM_DRIVER_VERSION_REQUIRED_MAJOR 1
Expand Down
5 changes: 5 additions & 0 deletions desmume/src/OGLRender_3_2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ OGLEXT(PFNGLCLEARBUFFERFIPROC, glClearBufferfi) // Core in v3.0 and ES v3.0

// Shaders
OGLEXT(PFNGLBINDFRAGDATALOCATIONPROC, glBindFragDataLocation) // Core in v3.0, not available in ES
#if defined(GL_VERSION_3_3) || defined(GL_ARB_blend_func_extended)
OGLEXT(PFNGLBINDFRAGDATALOCATIONINDEXEDPROC, glBindFragDataLocationIndexed) // Core in v3.3, not available in ES
#endif

// Buffer Objects
OGLEXT(PFNGLMAPBUFFERRANGEPROC, glMapBufferRange) // Core in v3.0 and ES v3.0
Expand Down Expand Up @@ -84,6 +86,9 @@ void OGLLoadEntryPoints_3_2()

// Shaders
INITOGLEXT(PFNGLBINDFRAGDATALOCATIONPROC, glBindFragDataLocation) // Core in v3.0, not available in ES
#if defined(GL_VERSION_3_3) || defined(GL_ARB_blend_func_extended)
INITOGLEXT(PFNGLBINDFRAGDATALOCATIONINDEXEDPROC, glBindFragDataLocationIndexed) // Core in v3.3, not available in ES
#endif

// Buffer Objects
INITOGLEXT(PFNGLMAPBUFFERRANGEPROC, glMapBufferRange) // Core in v3.0 and ES v3.0
Expand Down

0 comments on commit c962c55

Please sign in to comment.