Бранимир Караџић 5 years ago
parent
commit
8f4750b199
6 changed files with 116 additions and 73 deletions
  1. 1 1
      .appveyor.yml
  2. 0 2
      src/bgfx_p.h
  3. 27 16
      src/glcontext_egl.cpp
  4. 37 18
      src/glcontext_glx.cpp
  5. 20 11
      src/glcontext_html5.cpp
  6. 31 25
      src/glcontext_wgl.cpp

+ 1 - 1
.appveyor.yml

@@ -6,7 +6,7 @@ os:
 environment:
   matrix:
   - TOOLSET: vs2017
-  - TOOLSET: vs2019
+#  - TOOLSET: vs2019
 
 configuration:
   - Debug

+ 0 - 2
src/bgfx_p.h

@@ -44,8 +44,6 @@
 		, _handleAlloc.getMaxHandles()                             \
 		)
 
-#define BGFX_CAST_FUNCTION(proto, func) ((proto)(void(*)(void))func)
-
 #if BGFX_CONFIG_MULTITHREADED
 #	define BGFX_MUTEX_SCOPE(_mutex) bx::MutexScope BX_CONCATENATE(mutexScope, __LINE__)(_mutex)
 #else

+ 27 - 16
src/glcontext_egl.cpp

@@ -444,30 +444,41 @@ EGL_IMPORT
 	void GlContext::import()
 	{
 		BX_TRACE("Import:");
+
 #	if BX_PLATFORM_WINDOWS || BX_PLATFORM_LINUX
 		void* glesv2 = bx::dlopen("libGLESv2." BX_DL_EXT);
-#		define GL_EXTENSION(_optional, _proto, _func, _import)                                                                                                   \
-			{                                                                                                                                                    \
-				if (NULL == _func)                                                                                                                               \
-				{                                                                                                                                                \
-					_func = BGFX_CAST_FUNCTION(_proto, bx::dlsym(glesv2, #_import));                                                                             \
-					BX_TRACE("\t%p " #_func " (" #_import ")", _func);                                                                                           \
-					BGFX_FATAL(_optional || NULL != _func, Fatal::UnableToInitialize, "Failed to create OpenGLES context. eglGetProcAddress(\"%s\")", #_import); \
-				}                                                                                                                                                \
+
+#		define GL_EXTENSION(_optional, _proto, _func, _import)                           \
+			{                                                                            \
+				if (NULL == _func)                                                       \
+				{                                                                        \
+					_func = bx::functionCast<_proto>(bx::dlsym(glesv2, #_import));       \
+					BX_TRACE("\t%p " #_func " (" #_import ")", _func);                   \
+					BGFX_FATAL(_optional || NULL != _func                                \
+						, Fatal::UnableToInitialize                                      \
+						, "Failed to create OpenGLES context. eglGetProcAddress(\"%s\")" \
+						, #_import);                                                     \
+				}                                                                        \
 			}
 #	else
-#		define GL_EXTENSION(_optional, _proto, _func, _import)                                                                                                   \
-			{                                                                                                                                                    \
-				if (NULL == _func)                                                                                                                               \
-				{                                                                                                                                                \
-					_func = BGFX_CAST_FUNCTION(_proto , eglGetProcAddress(#_import));                                                                            \
-					BX_TRACE("\t%p " #_func " (" #_import ")", _func);                                                                                           \
-					BGFX_FATAL(_optional || NULL != _func, Fatal::UnableToInitialize, "Failed to create OpenGLES context. eglGetProcAddress(\"%s\")", #_import); \
-				}                                                                                                                                                \
+#		define GL_EXTENSION(_optional, _proto, _func, _import)                           \
+			{                                                                            \
+				if (NULL == _func)                                                       \
+				{                                                                        \
+					_func = bx::functionCast<_proto>(eglGetProcAddress(#_import) );      \
+					BX_TRACE("\t%p " #_func " (" #_import ")", _func);                   \
+					BGFX_FATAL(_optional || NULL != _func                                \
+						, Fatal::UnableToInitialize                                      \
+						, "Failed to create OpenGLES context. eglGetProcAddress(\"%s\")" \
+						, #_import);                                                     \
+				}                                                                        \
 			}
+
 #	endif // BX_PLATFORM_
 
 #	include "glimports.h"
+
+#	undef GL_EXTENSION
 	}
 
 } /* namespace gl */ } // namespace bgfx

+ 37 - 18
src/glcontext_glx.cpp

@@ -66,7 +66,7 @@ namespace bgfx { namespace gl
 		for(;;)
 		{
 			bx::StringView found = bx::strFind(searchStart, _ext);
-			if (found.isEmpty())
+			if (found.isEmpty() )
 			{
 				return false;
 			}
@@ -74,7 +74,7 @@ namespace bgfx { namespace gl
 			// We found the substring, but need an exact match, with a word
 			// boundary at both the front and back of the found spot.
 			if ((found.getPtr() == _extList || *(found.getPtr() - 1) == ' ')
-			&&  (found.getTerm() == end || *found.getTerm() == ' '))
+			&&  (found.getTerm() == end || *found.getTerm() == ' ') )
 			{
 				return true;
 			}
@@ -222,12 +222,16 @@ namespace bgfx { namespace gl
 		glXMakeCurrent(m_display, (::Window)g_platformData.nwh, m_context);
 		m_current = NULL;
 
-		const char* extensions = glXQueryExtensionsString(m_display, DefaultScreen(m_display));
+		const char* extensions = glXQueryExtensionsString(m_display, DefaultScreen(m_display) );
+
 		if (NULL != extensions)
 		{
 			bool foundSwapControl = false;
-			if (haveGlxExtension("GLX_EXT_swap_control", extensions)) {
-				glXSwapIntervalEXT = (PFNGLXSWAPINTERVALEXTPROC)glXGetProcAddress( (const GLubyte*)"glXSwapIntervalEXT");
+
+			if (haveGlxExtension("GLX_EXT_swap_control", extensions) )
+			{
+				glXSwapIntervalEXT = bx::functionCast<PFNGLXSWAPINTERVALEXTPROC>(glXGetProcAddress( (const GLubyte*)"glXSwapIntervalEXT") );
+
 				if (NULL != glXSwapIntervalEXT)
 				{
 					BX_TRACE("Using glXSwapIntervalEXT.");
@@ -235,8 +239,12 @@ namespace bgfx { namespace gl
 					foundSwapControl = true;
 				}
 			}
-			if (!foundSwapControl && haveGlxExtension("GLX_MESA_swap_control", extensions)) {
-				glXSwapIntervalMESA = (PFNGLXSWAPINTERVALMESAPROC)glXGetProcAddress( (const GLubyte*)"glXSwapIntervalMESA");
+
+			if (!foundSwapControl
+			&&  haveGlxExtension("GLX_MESA_swap_control", extensions) )
+			{
+				glXSwapIntervalMESA = bx::functionCast<PFNGLXSWAPINTERVALMESAPROC>(glXGetProcAddress( (const GLubyte*)"glXSwapIntervalMESA") );
+
 				if (NULL != glXSwapIntervalMESA)
 				{
 					BX_TRACE("Using glXSwapIntervalMESA.");
@@ -244,8 +252,12 @@ namespace bgfx { namespace gl
 					foundSwapControl = true;
 				}
 			}
-			if (!foundSwapControl && haveGlxExtension("GLX_SGI_swap_control", extensions)) {
-				glXSwapIntervalSGI = (PFNGLXSWAPINTERVALSGIPROC)glXGetProcAddress( (const GLubyte*)"glXSwapIntervalSGI");
+
+			if (!foundSwapControl
+			&&  haveGlxExtension("GLX_SGI_swap_control", extensions) )
+			{
+				glXSwapIntervalSGI = bx::functionCast<PFNGLXSWAPINTERVALSGIPROC>(glXGetProcAddress( (const GLubyte*)"glXSwapIntervalSGI") );
+
 				if (NULL != glXSwapIntervalSGI)
 				{
 					BX_TRACE("Using glXSwapIntervalSGI.");
@@ -348,16 +360,23 @@ namespace bgfx { namespace gl
 
 	void GlContext::import()
 	{
-#	define GL_EXTENSION(_optional, _proto, _func, _import) \
-				{ \
-					if (NULL == _func) \
-					{ \
-						_func = BGFX_CAST_FUNCTION(_proto, glXGetProcAddress( (const GLubyte*)#_import)); \
-						BX_TRACE("%p " #_func " (" #_import ")", _func); \
-						BGFX_FATAL(_optional || NULL != _func, Fatal::UnableToInitialize, "Failed to create OpenGL context. glXGetProcAddress %s", #_import); \
-					} \
-				}
+		BX_TRACE("Import:");
+
+#	define GL_EXTENSION(_optional, _proto, _func, _import)                                       \
+		{                                                                                        \
+			if (NULL == _func)                                                                   \
+			{                                                                                    \
+				_func = bx::functionCast<_proto>(glXGetProcAddress( (const GLubyte*)#_import) ); \
+				BX_TRACE("%p " #_func " (" #_import ")", _func);                                 \
+				BGFX_FATAL(_optional || NULL != _func                                            \
+					, Fatal::UnableToInitialize                                                  \
+					, "Failed to create OpenGL context. glXGetProcAddress %s", #_import);        \
+			}                                                                                    \
+		}
+
 #	include "glimports.h"
+
+#	undef GL_EXTENSION
 	}
 
 } /* namespace gl */ } // namespace bgfx

+ 20 - 11
src/glcontext_html5.cpp

@@ -182,19 +182,28 @@ namespace bgfx { namespace gl
 	void GlContext::import(int webGLVersion)
 	{
 		BX_TRACE("Import:");
-#		define GL_EXTENSION(_optional, _proto, _func, _import)                                                                                                   \
-			{                                                                                                                                                    \
-				if (NULL == _func)                                                                                                                               \
-				{                                                                                                                                                \
-					_func = (_proto)emscripten_webgl1_get_proc_address(#_import);                                                                                \
-					if (!_func && webGLVersion >= 2)                                                                                                             \
-					    _func = BGFX_CAST_FUNCTION(_proto, emscripten_webgl2_get_proc_address(#_import));                                                        \
-					BX_TRACE("\t%p " #_func " (" #_import ")", _func);                                                                                           \
-					BGFX_FATAL(_optional || NULL != _func, Fatal::UnableToInitialize, "Failed to create WebGL/OpenGLES context. GetProcAddress(\"%s\")", #_import); \
-				}                                                                                                                                                \
-			}
+
+#	define GL_EXTENSION(_optional, _proto, _func, _import)                                     \
+	{                                                                                          \
+		if (NULL == _func)                                                                     \
+		{                                                                                      \
+			_func = (_proto)emscripten_webgl1_get_proc_address(#_import);                      \
+			if (!_func && webGLVersion >= 2)                                                   \
+			{                                                                                  \
+				_func = bx::functionCast<_proto>(emscripten_webgl2_get_proc_address(#_import); \
+			}                                                                                  \
+			BX_TRACE("\t%p " #_func " (" #_import ")", _func);                                 \
+			BGFX_FATAL(_optional || NULL != _func, Fatal::UnableToInitialize                   \
+				, "Failed to create WebGL/OpenGLES context. GetProcAddress(\"%s\")"            \
+				, #_import                                                                     \
+				);                                                                             \
+		}                                                                                      \
+	}
 
 #	include "glimports.h"
+
+#	undef GL_EXTENSION
+
 	}
 
 } /* namespace gl */ } // namespace bgfx

+ 31 - 25
src/glcontext_wgl.cpp

@@ -106,7 +106,7 @@ namespace bgfx { namespace gl
 		m_opengl32dll = bx::dlopen("opengl32.dll");
 		BGFX_FATAL(NULL != m_opengl32dll, Fatal::UnableToInitialize, "Failed to load opengl32.dll.");
 
-		wglGetProcAddress = BGFX_CAST_FUNCTION(PFNWGLGETPROCADDRESSPROC, bx::dlsym(m_opengl32dll, "wglGetProcAddress"));
+		wglGetProcAddress = bx::functionCast<PFNWGLGETPROCADDRESSPROC>(bx::dlsym(m_opengl32dll, "wglGetProcAddress");
 		BGFX_FATAL(NULL != wglGetProcAddress, Fatal::UnableToInitialize, "Failed get wglGetProcAddress.");
 
 
@@ -136,13 +136,13 @@ namespace bgfx { namespace gl
 
 		if (NULL != g_platformData.nwh && NULL == g_platformData.context )
 		{
-			wglMakeCurrent = BGFX_CAST_FUNCTION(PFNWGLMAKECURRENTPROC, bx::dlsym(m_opengl32dll, "wglMakeCurrent"));
+			wglMakeCurrent = bx::functionCast<PFNWGLMAKECURRENTPROC>(bx::dlsym(m_opengl32dll, "wglMakeCurrent") );
 			BGFX_FATAL(NULL != wglMakeCurrent, Fatal::UnableToInitialize, "Failed get wglMakeCurrent.");
 
-			wglCreateContext = BGFX_CAST_FUNCTION(PFNWGLCREATECONTEXTPROC, bx::dlsym(m_opengl32dll, "wglCreateContext"));
+			wglCreateContext = bx::functionCast<PFNWGLCREATECONTEXTPROC>(bx::dlsym(m_opengl32dll, "wglCreateContext") );
 			BGFX_FATAL(NULL != wglCreateContext, Fatal::UnableToInitialize, "Failed get wglCreateContext.");
 
-			wglDeleteContext = BGFX_CAST_FUNCTION(PFNWGLDELETECONTEXTPROC, bx::dlsym(m_opengl32dll, "wglDeleteContext"));
+			wglDeleteContext = bx::functionCast<PFNWGLDELETECONTEXTPROC>(bx::dlsym(m_opengl32dll, "wglDeleteContext") );
 			BGFX_FATAL(NULL != wglDeleteContext, Fatal::UnableToInitialize, "Failed get wglDeleteContext.");
 
 			m_hdc = GetDC( (HWND)g_platformData.nwh);
@@ -171,10 +171,10 @@ namespace bgfx { namespace gl
 
 			HGLRC context = createContext(hdc);
 
-			wglGetExtensionsStringARB  = BGFX_CAST_FUNCTION(PFNWGLGETEXTENSIONSSTRINGARBPROC, wglGetProcAddress("wglGetExtensionsStringARB"));
-			wglChoosePixelFormatARB    = BGFX_CAST_FUNCTION(PFNWGLCHOOSEPIXELFORMATARBPROC, wglGetProcAddress("wglChoosePixelFormatARB"));
-			wglCreateContextAttribsARB = BGFX_CAST_FUNCTION(PFNWGLCREATECONTEXTATTRIBSARBPROC, wglGetProcAddress("wglCreateContextAttribsARB"));
-			wglSwapIntervalEXT         = BGFX_CAST_FUNCTION(PFNWGLSWAPINTERVALEXTPROC, wglGetProcAddress("wglSwapIntervalEXT"));
+			wglGetExtensionsStringARB  = bx::functionCast<PFNWGLGETEXTENSIONSSTRINGARBPROC>(wglGetProcAddress("wglGetExtensionsStringARB") );
+			wglChoosePixelFormatARB    = bx::functionCast<PFNWGLCHOOSEPIXELFORMATARBPROC>(wglGetProcAddress("wglChoosePixelFormatARB") );
+			wglCreateContextAttribsARB = bx::functionCast<PFNWGLCREATECONTEXTATTRIBSARBPROC>(wglGetProcAddress("wglCreateContextAttribsARB") );
+			wglSwapIntervalEXT         = bx::functionCast<PFNWGLSWAPINTERVALEXTPROC>(wglGetProcAddress("wglSwapIntervalEXT") );
 
 			if (NULL != wglGetExtensionsStringARB)
 			{
@@ -387,24 +387,30 @@ namespace bgfx { namespace gl
 	void GlContext::import()
 	{
 		BX_TRACE("Import:");
-#	define GL_EXTENSION(_optional, _proto, _func, _import) \
-				{ \
-					if (NULL == _func) \
-					{ \
-						_func = BGFX_CAST_FUNCTION(_proto, wglGetProcAddress(#_import)); \
-						if (_func == NULL) \
-						{ \
-							_func = BGFX_CAST_FUNCTION(_proto, bx::dlsym(m_opengl32dll, #_import)); \
-							BX_TRACE("    %p " #_func " (" #_import ")", _func); \
-						} \
-						else \
-						{ \
-							BX_TRACE("wgl %p " #_func " (" #_import ")", _func); \
-						} \
-						BGFX_FATAL(BX_IGNORE_C4127(_optional) || NULL != _func, Fatal::UnableToInitialize, "Failed to create OpenGL context. wglGetProcAddress(\"%s\")", #_import); \
-					} \
-				}
+
+#	define GL_EXTENSION(_optional, _proto, _func, _import)                                     \
+		{                                                                                      \
+			if (NULL == _func)                                                                 \
+			{                                                                                  \
+				_func = bx::functionCast<_proto>(wglGetProcAddress(#_import);                  \
+				if (_func == NULL)                                                             \
+				{                                                                              \
+					_func = bx::functionCast<_proto>(bx::dlsym(m_opengl32dll, #_import);       \
+					BX_TRACE("    %p " #_func " (" #_import ")", _func);                       \
+				}                                                                              \
+				else                                                                           \
+				{                                                                              \
+					BX_TRACE("wgl %p " #_func " (" #_import ")", _func);                       \
+				}                                                                              \
+				BGFX_FATAL(BX_IGNORE_C4127(_optional) || NULL != _func                         \
+					, Fatal::UnableToInitialize                                                \
+					, "Failed to create OpenGL context. wglGetProcAddress(\"%s\")", #_import); \
+			}                                                                                  \
+		}
+
 #	include "glimports.h"
+
+#	undef GL_EXTENSION
 	}
 
 } } // namespace bgfx