Skip to content
This repository has been archived by the owner on May 13, 2024. It is now read-only.

Commit

Permalink
Migrate OpenGL cache and texture handler to GL pointers
Browse files Browse the repository at this point in the history
this makes the OpenGL 3 driver free of GL symbol references!
  • Loading branch information
sfan5 committed Jan 16, 2024
1 parent 88ca26c commit 16c462c
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 63 deletions.
1 change: 0 additions & 1 deletion source/Irrlicht/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,6 @@ if(ENABLE_OPENGL3)
if (NOT USE_SDL2)
message(FATAL_ERROR "OpenGL 3 driver requires SDL2")
endif()
set(OPENGL_DIRECT_LINK TRUE) # TODO
set(USE_SDLGL ON)
set(USE_SDLGL3 ON)
endif()
Expand Down
62 changes: 32 additions & 30 deletions source/Irrlicht/COpenGLCoreCacheHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include "SMaterial.h"
#include "ITexture.h"

#include "mt_opengl.h"

namespace irr
{
namespace video
Expand Down Expand Up @@ -87,19 +89,19 @@ class COpenGLCoreCacheHandler

if (curTextureType != prevTextureType)
{
glBindTexture(prevTextureType, 0);
GL.BindTexture(prevTextureType, 0);

#if ( defined(IRR_COMPILE_GL_COMMON) || defined(IRR_COMPILE_GLES_COMMON) )
glDisable(prevTextureType);
glEnable(curTextureType);
GL.Disable(prevTextureType);
GL.Enable(curTextureType);
#endif
}
#if ( defined(IRR_COMPILE_GL_COMMON) || defined(IRR_COMPILE_GLES_COMMON) )
else if (!prevTexture)
glEnable(curTextureType);
GL.Enable(curTextureType);
#endif

glBindTexture(curTextureType, static_cast<const TOpenGLTexture*>(texture)->getOpenGLTextureName());
GL.BindTexture(curTextureType, static_cast<const TOpenGLTexture*>(texture)->getOpenGLTextureName());
}
else
{
Expand All @@ -115,10 +117,10 @@ class COpenGLCoreCacheHandler
{
const GLenum prevTextureType = prevTexture->getOpenGLTextureType();

glBindTexture(prevTextureType, 0);
GL.BindTexture(prevTextureType, 0);

#if ( defined(IRR_COMPILE_GL_COMMON) || defined(IRR_COMPILE_GLES_COMMON) )
glDisable(prevTextureType);
GL.Disable(prevTextureType);
#endif
}

Expand Down Expand Up @@ -222,28 +224,28 @@ class COpenGLCoreCacheHandler
ColorMask[i] = ECP_ALL;
}

glBlendFunc(GL_ONE, GL_ZERO);
glDisable(GL_BLEND);
GL.BlendFunc(GL_ONE, GL_ZERO);
GL.Disable(GL_BLEND);

glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
GL.ColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);

glCullFace(CullFaceMode);
glDisable(GL_CULL_FACE);
GL.CullFace(CullFaceMode);
GL.Disable(GL_CULL_FACE);

glDepthFunc(DepthFunc);
glDepthMask(GL_TRUE);
glDisable(GL_DEPTH_TEST);
GL.DepthFunc(DepthFunc);
GL.DepthMask(GL_TRUE);
GL.Disable(GL_DEPTH_TEST);

Driver->irrGlActiveTexture(ActiveTexture);

#if ( defined(IRR_COMPILE_GL_COMMON) || defined(IRR_COMPILE_GLES_COMMON) )
glDisable(GL_TEXTURE_2D);
GL.Disable(GL_TEXTURE_2D);
#endif

const core::dimension2d<u32> ScreenSize = Driver->getScreenSize();
ViewportWidth = ScreenSize.Width;
ViewportHeight = ScreenSize.Height;
glViewport(ViewportX, ViewportY, ViewportWidth, ViewportHeight);
GL.Viewport(ViewportX, ViewportY, ViewportWidth, ViewportHeight);
}

virtual ~COpenGLCoreCacheHandler()
Expand Down Expand Up @@ -300,7 +302,7 @@ class COpenGLCoreCacheHandler
BlendSourceAlpha[0] != source || BlendDestinationAlpha[0] != destination ||
BlendFuncInvalid)
{
glBlendFunc(source, destination);
GL.BlendFunc(source, destination);

for (GLuint i = 0; i < FrameBufferCount; ++i)
{
Expand Down Expand Up @@ -383,9 +385,9 @@ class COpenGLCoreCacheHandler
if (Blend[0] != enable || BlendInvalid)
{
if (enable)
glEnable(GL_BLEND);
GL.Enable(GL_BLEND);
else
glDisable(GL_BLEND);
GL.Disable(GL_BLEND);

for (GLuint i = 0; i < FrameBufferCount; ++i)
Blend[i] = enable;
Expand Down Expand Up @@ -419,7 +421,7 @@ class COpenGLCoreCacheHandler
{
if (ColorMask[0] != mask || ColorMaskInvalid)
{
glColorMask((mask & ECP_RED) ? GL_TRUE : GL_FALSE, (mask & ECP_GREEN) ? GL_TRUE : GL_FALSE, (mask & ECP_BLUE) ? GL_TRUE : GL_FALSE, (mask & ECP_ALPHA) ? GL_TRUE : GL_FALSE);
GL.ColorMask((mask & ECP_RED) ? GL_TRUE : GL_FALSE, (mask & ECP_GREEN) ? GL_TRUE : GL_FALSE, (mask & ECP_BLUE) ? GL_TRUE : GL_FALSE, (mask & ECP_ALPHA) ? GL_TRUE : GL_FALSE);

for (GLuint i = 0; i < FrameBufferCount; ++i)
ColorMask[i] = mask;
Expand All @@ -445,7 +447,7 @@ class COpenGLCoreCacheHandler
{
if (CullFaceMode != mode)
{
glCullFace(mode);
GL.CullFace(mode);
CullFaceMode = mode;
}
}
Expand All @@ -455,9 +457,9 @@ class COpenGLCoreCacheHandler
if (CullFace != enable)
{
if (enable)
glEnable(GL_CULL_FACE);
GL.Enable(GL_CULL_FACE);
else
glDisable(GL_CULL_FACE);
GL.Disable(GL_CULL_FACE);

CullFace = enable;
}
Expand All @@ -469,7 +471,7 @@ class COpenGLCoreCacheHandler
{
if (DepthFunc != mode)
{
glDepthFunc(mode);
GL.DepthFunc(mode);
DepthFunc = mode;
}
}
Expand All @@ -484,9 +486,9 @@ class COpenGLCoreCacheHandler
if (DepthMask != enable)
{
if (enable)
glDepthMask(GL_TRUE);
GL.DepthMask(GL_TRUE);
else
glDepthMask(GL_FALSE);
GL.DepthMask(GL_FALSE);

DepthMask = enable;
}
Expand All @@ -502,9 +504,9 @@ class COpenGLCoreCacheHandler
if (DepthTest != enable)
{
if (enable)
glEnable(GL_DEPTH_TEST);
GL.Enable(GL_DEPTH_TEST);
else
glDisable(GL_DEPTH_TEST);
GL.Disable(GL_DEPTH_TEST);

DepthTest = enable;
}
Expand Down Expand Up @@ -572,7 +574,7 @@ class COpenGLCoreCacheHandler
{
if (ViewportX != viewportX || ViewportY != viewportY || ViewportWidth != viewportWidth || ViewportHeight != viewportHeight)
{
glViewport(viewportX, viewportY, viewportWidth, viewportHeight);
GL.Viewport(viewportX, viewportY, viewportWidth, viewportHeight);
ViewportX = viewportX;
ViewportY = viewportY;
ViewportWidth = viewportWidth;
Expand Down
59 changes: 27 additions & 32 deletions source/Irrlicht/COpenGLCoreTexture.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@
#include "CImage.h"
#include "CColorConverter.h"

// Check if GL version we compile with should have the glGenerateMipmap function.
#if defined(GL_VERSION_3_0) || defined(GL_ES_VERSION_2_0)
#define IRR_OPENGL_HAS_glGenerateMipmap
#endif
#include "mt_opengl.h"

namespace irr
{
Expand Down Expand Up @@ -90,23 +87,23 @@ class COpenGLCoreTexture : public ITexture
tmpImages = &Images;
}

glGenTextures(1, &TextureName);
GL.GenTextures(1, &TextureName);

const COpenGLCoreTexture* prevTexture = Driver->getCacheHandler()->getTextureCache().get(0);
Driver->getCacheHandler()->getTextureCache().set(0, this);

glTexParameteri(TextureType, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(TextureType, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
GL.TexParameteri(TextureType, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
GL.TexParameteri(TextureType, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

#ifdef GL_GENERATE_MIPMAP_HINT
if (HasMipMaps)
{
if (Driver->getTextureCreationFlag(ETCF_OPTIMIZED_FOR_SPEED))
glHint(GL_GENERATE_MIPMAP_HINT, GL_FASTEST);
GL.Hint(GL_GENERATE_MIPMAP_HINT, GL_FASTEST);
else if (Driver->getTextureCreationFlag(ETCF_OPTIMIZED_FOR_QUALITY))
glHint(GL_GENERATE_MIPMAP_HINT, GL_NICEST);
GL.Hint(GL_GENERATE_MIPMAP_HINT, GL_NICEST);
else
glHint(GL_GENERATE_MIPMAP_HINT, GL_DONT_CARE);
GL.Hint(GL_GENERATE_MIPMAP_HINT, GL_DONT_CARE);
}
#endif

Expand All @@ -115,7 +112,7 @@ class COpenGLCoreTexture : public ITexture
{
LegacyAutoGenerateMipMaps = Driver->getTextureCreationFlag(ETCF_AUTO_GENERATE_MIP_MAPS) &&
Driver->queryFeature(EVDF_MIP_MAP_AUTO_UPDATE);
glTexParameteri(TextureType, GL_GENERATE_MIPMAP, LegacyAutoGenerateMipMaps ? GL_TRUE : GL_FALSE);
GL.TexParameteri(TextureType, GL_GENERATE_MIPMAP, LegacyAutoGenerateMipMaps ? GL_TRUE : GL_FALSE);
}
#endif

Expand Down Expand Up @@ -174,19 +171,19 @@ class COpenGLCoreTexture : public ITexture
os::Printer::log("COpenGLCoreTexture: Color format is not supported", ColorFormatNames[ColorFormat < ECF_UNKNOWN?ColorFormat:ECF_UNKNOWN], ELL_ERROR);
}

glGenTextures(1, &TextureName);
GL.GenTextures(1, &TextureName);

const COpenGLCoreTexture* prevTexture = Driver->getCacheHandler()->getTextureCache().get(0);
Driver->getCacheHandler()->getTextureCache().set(0, this);


glTexParameteri(TextureType, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(TextureType, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(TextureType, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(TextureType, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
GL.TexParameteri(TextureType, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
GL.TexParameteri(TextureType, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
GL.TexParameteri(TextureType, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
GL.TexParameteri(TextureType, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

#if defined(GL_VERSION_1_2)
glTexParameteri(TextureType, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
GL.TexParameteri(TextureType, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
#endif

StatesCache.WrapU = ETC_CLAMP_TO_EDGE;
Expand All @@ -196,15 +193,15 @@ class COpenGLCoreTexture : public ITexture
switch (Type)
{
case ETT_2D:
glTexImage2D(GL_TEXTURE_2D, 0, InternalFormat, Size.Width, Size.Height, 0, PixelFormat, PixelType, 0);
GL.TexImage2D(GL_TEXTURE_2D, 0, InternalFormat, Size.Width, Size.Height, 0, PixelFormat, PixelType, 0);
break;
case ETT_CUBEMAP:
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, InternalFormat, Size.Width, Size.Height, 0, PixelFormat, PixelType, 0);
glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0, InternalFormat, Size.Width, Size.Height, 0, PixelFormat, PixelType, 0);
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0, InternalFormat, Size.Width, Size.Height, 0, PixelFormat, PixelType, 0);
glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, 0, InternalFormat, Size.Width, Size.Height, 0, PixelFormat, PixelType, 0);
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0, InternalFormat, Size.Width, Size.Height, 0, PixelFormat, PixelType, 0);
glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, InternalFormat, Size.Width, Size.Height, 0, PixelFormat, PixelType, 0);
GL.TexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, InternalFormat, Size.Width, Size.Height, 0, PixelFormat, PixelType, 0);
GL.TexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0, InternalFormat, Size.Width, Size.Height, 0, PixelFormat, PixelType, 0);
GL.TexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0, InternalFormat, Size.Width, Size.Height, 0, PixelFormat, PixelType, 0);
GL.TexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, 0, InternalFormat, Size.Width, Size.Height, 0, PixelFormat, PixelType, 0);
GL.TexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0, InternalFormat, Size.Width, Size.Height, 0, PixelFormat, PixelType, 0);
GL.TexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, InternalFormat, Size.Width, Size.Height, 0, PixelFormat, PixelType, 0);
break;
}

Expand All @@ -220,7 +217,7 @@ class COpenGLCoreTexture : public ITexture
virtual ~COpenGLCoreTexture()
{
if (TextureName)
glDeleteTextures(1, &TextureName);
GL.DeleteTextures(1, &TextureName);

if (LockImage)
LockImage->drop();
Expand Down Expand Up @@ -278,7 +275,7 @@ class COpenGLCoreTexture : public ITexture
tmpTextureType = GL_TEXTURE_CUBE_MAP_POSITIVE_X + layer;
}

glGetTexImage(tmpTextureType, MipLevelStored, PixelFormat, PixelType, tmpImage->getData());
GL.GetTexImage(tmpTextureType, MipLevelStored, PixelFormat, PixelType, tmpImage->getData());
Driver->testGLError(__LINE__);

if (IsRenderTarget && lockFlags == ETLF_FLIP_Y_UP_RTT)
Expand Down Expand Up @@ -321,12 +318,12 @@ class COpenGLCoreTexture : public ITexture

Driver->irrGlFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tmpTexture->getOpenGLTextureName(), 0);

glClear(GL_COLOR_BUFFER_BIT);
GL.Clear(GL_COLOR_BUFFER_BIT);

Driver->draw2DImage(this, layer, true);

IImage* tmpImage = Driver->createImage(ECF_A8R8G8B8, Size);
glReadPixels(0, 0, Size.Width, Size.Height, GL_RGBA, GL_UNSIGNED_BYTE, tmpImage->getData());
GL.ReadPixels(0, 0, Size.Width, Size.Height, GL_RGBA, GL_UNSIGNED_BYTE, tmpImage->getData());

Driver->getCacheHandler()->setFBO(prevFBO);
Driver->getCacheHandler()->setViewport(prevViewportX, prevViewportY, prevViewportWidth, prevViewportHeight);
Expand Down Expand Up @@ -428,9 +425,7 @@ class COpenGLCoreTexture : public ITexture
}
else
{
#ifdef IRR_OPENGL_HAS_glGenerateMipmap
Driver->irrGlGenerateMipmap(TextureType);
#endif
}

Driver->getCacheHandler()->getTextureCache().set(0, prevTexture);
Expand Down Expand Up @@ -595,9 +590,9 @@ class COpenGLCoreTexture : public ITexture
case GL_TEXTURE_2D:
case GL_TEXTURE_CUBE_MAP:
if (initTexture)
glTexImage2D(tmpTextureType, level, InternalFormat, width, height, 0, PixelFormat, PixelType, tmpData);
GL.TexImage2D(tmpTextureType, level, InternalFormat, width, height, 0, PixelFormat, PixelType, tmpData);
else
glTexSubImage2D(tmpTextureType, level, 0, 0, width, height, PixelFormat, PixelType, tmpData);
GL.TexSubImage2D(tmpTextureType, level, 0, 0, width, height, PixelFormat, PixelType, tmpData);
Driver->testGLError(__LINE__);
break;
default:
Expand Down

0 comments on commit 16c462c

Please sign in to comment.