[Scummvm-git-logs] scummvm master -> 43d87cb0296eab50bee9219297ed79e57fb87581

aquadran noreply at scummvm.org
Thu Dec 30 13:04:34 UTC 2021


This automated email contains information about 1 new commit which have been
pushed to the 'scummvm' repo located at https://github.com/scummvm/scummvm .

Summary:
43d87cb029 BACKENDS: Support detecting OpenGL ES 2 at runtime for 3D games


Commit: 43d87cb0296eab50bee9219297ed79e57fb87581
    https://github.com/scummvm/scummvm/commit/43d87cb0296eab50bee9219297ed79e57fb87581
Author: Cameron Cawley (ccawley2011 at gmail.com)
Date: 2021-12-30T13:57:50+01:00

Commit Message:
BACKENDS: Support detecting OpenGL ES 2 at runtime for 3D games

Changed paths:
    backends/graphics3d/openglsdl/openglsdl-graphics3d.cpp
    backends/graphics3d/openglsdl/openglsdl-graphics3d.h
    engines/grim/gfx_opengl_shaders.cpp
    engines/stark/gfx/opengltexture.cpp


diff --git a/backends/graphics3d/openglsdl/openglsdl-graphics3d.cpp b/backends/graphics3d/openglsdl/openglsdl-graphics3d.cpp
index 468352adfde..1ee722531ec 100644
--- a/backends/graphics3d/openglsdl/openglsdl-graphics3d.cpp
+++ b/backends/graphics3d/openglsdl/openglsdl-graphics3d.cpp
@@ -65,6 +65,79 @@ OpenGLSdlGraphics3dManager::OpenGLSdlGraphics3dManager(SdlEventSource *eventSour
 
 	// Don't start at zero so that the value is never the same as the surface graphics manager
 	_screenChangeCount = 1 << (sizeof(int) * 5 - 2);
+
+	// Set up proper SDL OpenGL context creation.
+#if SDL_VERSION_ATLEAST(2, 0, 0)
+	enum {
+#ifdef USE_OPENGL_SHADERS
+		DEFAULT_GL_MAJOR = 2,
+		DEFAULT_GL_MINOR = 1,
+#else
+		DEFAULT_GL_MAJOR = 1,
+		DEFAULT_GL_MINOR = 3,
+#endif
+
+		DEFAULT_GLES2_MAJOR = 2,
+		DEFAULT_GLES2_MINOR = 0
+	};
+
+#ifdef USE_GLES2
+	_glContextType = OpenGL::kOGLContextGLES2;
+	_glContextProfileMask = SDL_GL_CONTEXT_PROFILE_ES;
+	_glContextMajor = DEFAULT_GLES2_MAJOR;
+	_glContextMinor = DEFAULT_GLES2_MINOR;
+#else
+	bool noDefaults = false;
+
+	// Obtain the default GL(ES) context SDL2 tries to setup.
+	//
+	// Please note this might not actually be SDL2's defaults when multiple
+	// instances of this object have been created. But that is no issue
+	// because then we already set up what we want to use.
+	//
+	// In case no defaults are given we prefer OpenGL over OpenGL ES.
+	if (SDL_GL_GetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, &_glContextProfileMask) != 0) {
+		_glContextProfileMask = 0;
+		noDefaults = true;
+	}
+
+	if (SDL_GL_GetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, &_glContextMajor) != 0) {
+		noDefaults = true;
+	}
+
+	if (SDL_GL_GetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, &_glContextMinor) != 0) {
+		noDefaults = true;
+	}
+
+	if (noDefaults) {
+		if (_glContextProfileMask == SDL_GL_CONTEXT_PROFILE_ES) {
+			_glContextMajor = DEFAULT_GLES2_MAJOR;
+			_glContextMinor = DEFAULT_GLES2_MINOR;
+		} else {
+			_glContextProfileMask = 0;
+			_glContextMajor = DEFAULT_GL_MAJOR;
+			_glContextMinor = DEFAULT_GL_MINOR;
+		}
+	}
+
+	if (_glContextProfileMask == SDL_GL_CONTEXT_PROFILE_ES) {
+		// TODO: Support GLES1 for games
+		_glContextType = OpenGL::kOGLContextGLES2;
+	} else if (_glContextProfileMask == SDL_GL_CONTEXT_PROFILE_CORE) {
+		_glContextType = OpenGL::kOGLContextGL;
+
+		// Core profile does not allow legacy functionality, which we use.
+		// Thus we request a standard OpenGL context.
+		_glContextProfileMask = 0;
+		_glContextMajor = DEFAULT_GL_MAJOR;
+		_glContextMinor = DEFAULT_GL_MINOR;
+	} else {
+		_glContextType = OpenGL::kOGLContextGL;
+	}
+#endif
+#else
+	_glContextType = OpenGL::kOGLContextGL;
+#endif
 }
 
 OpenGLSdlGraphics3dManager::~OpenGLSdlGraphics3dManager() {
@@ -361,15 +434,7 @@ void OpenGLSdlGraphics3dManager::notifyResize(const int width, const int height)
 }
 
 void OpenGLSdlGraphics3dManager::initializeOpenGLContext() const {
-	OpenGL::ContextOGLType type;
-
-#ifdef USE_GLES2
-	type = OpenGL::kOGLContextGLES2;
-#else
-	type = OpenGL::kOGLContextGL;
-#endif
-
-	OpenGLContext.initialize(type);
+	OpenGLContext.initialize(_glContextType);
 
 #if SDL_VERSION_ATLEAST(2, 0, 0)
 	if (SDL_GL_SetSwapInterval(_vsync ? 1 : 0)) {
@@ -423,23 +488,9 @@ bool OpenGLSdlGraphics3dManager::createOrUpdateGLContext(uint gameWidth, uint ga
 		SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, _vsync ? 1 : 0);
 #endif
 #if SDL_VERSION_ATLEAST(2, 0, 0)
-#ifdef USE_GLES2
-		SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
-		SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
-		SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
-#else
-		// The OpenGL implementation on AmigaOS4 is close to 1.3. Until that changes we need
-		// to use 1.3 as version or ScummVM will cease working at all on that platform.
-		// Profile Mask has to be 0 as well.
-		// This will be revised and removed once AmigaOS4 supports OpenGL 2.x or OpenGLES2.
-		#ifdef __amigaos4__
-			SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 1);
-			SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
-		#else
-			SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
-			SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
-		#endif
-#endif
+		SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, _glContextProfileMask);
+		SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, _glContextMajor);
+		SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, _glContextMinor);
 #endif
 
 #if SDL_VERSION_ATLEAST(2, 0, 0)
diff --git a/backends/graphics3d/openglsdl/openglsdl-graphics3d.h b/backends/graphics3d/openglsdl/openglsdl-graphics3d.h
index ba7dcb2b71c..768a46f0ca7 100644
--- a/backends/graphics3d/openglsdl/openglsdl-graphics3d.h
+++ b/backends/graphics3d/openglsdl/openglsdl-graphics3d.h
@@ -28,6 +28,8 @@
 
 #if defined(USE_OPENGL_GAME) || defined(USE_OPENGL_SHADERS)
 
+#include "graphics/opengl/context.h"
+
 namespace OpenGL {
 	class FrameBuffer;
 	class SurfaceRenderer;
@@ -117,10 +119,13 @@ public:
 
 protected:
 #if SDL_VERSION_ATLEAST(2, 0, 0)
+	int _glContextProfileMask, _glContextMajor, _glContextMinor;
 	SDL_GLContext _glContext;
 	void deinitializeRenderer();
 #endif
 
+	OpenGL::ContextOGLType _glContextType;
+
 	bool _supportsFrameBuffer;
 
 	Math::Rect2d _gameRect;
diff --git a/engines/grim/gfx_opengl_shaders.cpp b/engines/grim/gfx_opengl_shaders.cpp
index 34fd1af1dd6..645f031616c 100644
--- a/engines/grim/gfx_opengl_shaders.cpp
+++ b/engines/grim/gfx_opengl_shaders.cpp
@@ -44,6 +44,7 @@
 #if defined(USE_OPENGL_SHADERS)
 
 #include "graphics/surface.h"
+#include "graphics/opengl/context.h"
 
 #include "engines/grim/actor.h"
 #include "engines/grim/bitmap.h"
@@ -2167,27 +2168,29 @@ Bitmap *GfxOpenGLS::getScreenshot(int w, int h, bool useStored) {
 	Bitmap *bmp;
 
 	if (useStored) {
-#ifdef USE_GLES2
-		GLuint frameBuffer;
-		glGenFramebuffers(1, &frameBuffer);
-		glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer);
-		glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, _storedDisplay, 0);
+		if (OpenGLContext.type == OpenGL::kOGLContextGLES2) {
+			GLuint frameBuffer;
+			glGenFramebuffers(1, &frameBuffer);
+			glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer);
+			glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, _storedDisplay, 0);
 
-		readPixels(0, 0, _screenWidth, _screenHeight, (uint8 *)src.getPixels());
+			readPixels(0, 0, _screenWidth, _screenHeight, (uint8 *)src.getPixels());
 
-		glBindFramebuffer(GL_FRAMEBUFFER, 0);
-		glDeleteFramebuffers(1, &frameBuffer);
-#else
-		glBindTexture(GL_TEXTURE_2D, _storedDisplay);
-		char *buffer = new char[_screenWidth * _screenHeight * 4];
+			glBindFramebuffer(GL_FRAMEBUFFER, 0);
+			glDeleteFramebuffers(1, &frameBuffer);
+#ifndef USE_GLES2
+		} else {
+			glBindTexture(GL_TEXTURE_2D, _storedDisplay);
+			char *buffer = new char[_screenWidth * _screenHeight * 4];
 
-		glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
-		byte *rawBuf = (byte *)src.getPixels();
-		for (int i = 0; i < _screenHeight; i++) {
-			memcpy(&(rawBuf[(_screenHeight - i - 1) * _screenWidth * 4]), &buffer[4 * _screenWidth * i], _screenWidth * 4);
-		}
-		delete[] buffer;
+			glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
+			byte *rawBuf = (byte *)src.getPixels();
+			for (int i = 0; i < _screenHeight; i++) {
+				memcpy(&(rawBuf[(_screenHeight - i - 1) * _screenWidth * 4]), &buffer[4 * _screenWidth * i], _screenWidth * 4);
+			}
+			delete[] buffer;
 #endif
+		}
 	} else {
 		readPixels(0, 0, _screenWidth, _screenHeight, (uint8 *)src.getPixels());
 	}
diff --git a/engines/stark/gfx/opengltexture.cpp b/engines/stark/gfx/opengltexture.cpp
index 706bb8f7391..9ba1f3d92d1 100644
--- a/engines/stark/gfx/opengltexture.cpp
+++ b/engines/stark/gfx/opengltexture.cpp
@@ -27,6 +27,8 @@
 
 #if defined(USE_OPENGL_GAME) || defined(USE_OPENGL_SHADERS)
 
+#include "graphics/opengl/context.h"
+
 namespace Stark {
 namespace Gfx {
 
@@ -104,10 +106,12 @@ void OpenGlTexture::setLevelCount(uint32 count) {
 		// GLES2 does not allow setting the max provided mipmap level.
 		// It expects all the levels to be provided, which is not the case in TLJ.
 		// FIXME: Enable mipmapping on GLES2
-		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, count - 1);
+		if (OpenGLContext.type != OpenGL::kOGLContextGLES2) {
+			glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, count - 1);
 
-		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
-		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+			glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
+			glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+		}
 #endif
 
 		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);
@@ -118,10 +122,7 @@ void OpenGlTexture::setLevelCount(uint32 count) {
 void OpenGlTexture::addLevel(uint32 level, const Graphics::Surface *surface, const byte *palette) {
 	assert(level < _levelCount);
 
-#if defined(USE_GLES2)
-	if (level == 0)
-#endif
-	{
+	if (level == 0 || OpenGLContext.type != OpenGL::kOGLContextGLES2) {
 		updateLevel(level, surface, palette);
 	}
 }




More information about the Scummvm-git-logs mailing list