[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [58676] branches/soc-2013-viewport_fx: Enabled GLEW_MX and got it working with GHOST_ContextWGL

Jason Wilkins Jason.A.Wilkins at gmail.com
Sat Jul 27 22:32:11 CEST 2013


Revision: 58676
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=58676
Author:   jwilkins
Date:     2013-07-27 20:32:10 +0000 (Sat, 27 Jul 2013)
Log Message:
-----------
Enabled GLEW_MX and got it working with GHOST_ContextWGL

GLEW_MX puts each set of GL entry points into its own independent context.  This should reduce the possibility of WGL and GL getting mismatched.

It should also allow desktop and embedded GL to live in the same program at the same time, which isn't really the end goal, but if multiple versions of OpenGL could run in the same instance of Blender it would mean that GL contexts are well managed and encapsulated.

The biggest chore is probably sharing textures between alien contexts, however, I believe that problem can be solved by the same mechanism that will be needed to allow Blender to recover from power management events on mobile devices (and probably also the desktop when using ES).

Not going to go too far down that road now, but this is the foundation.

Modified Paths:
--------------
    branches/soc-2013-viewport_fx/CMakeLists.txt
    branches/soc-2013-viewport_fx/extern/glew-es/include/GL/glew.h
    branches/soc-2013-viewport_fx/intern/ghost/intern/GHOST_Context.cpp
    branches/soc-2013-viewport_fx/intern/ghost/intern/GHOST_Context.h
    branches/soc-2013-viewport_fx/intern/ghost/intern/GHOST_ContextWGL.cpp
    branches/soc-2013-viewport_fx/intern/ghost/intern/GHOST_ContextWGL.h
    branches/soc-2013-viewport_fx/source/blender/gpu/intern/gpu_extensions.c
    branches/soc-2013-viewport_fx/source/blender/gpu/intern/gpu_glew.h
    branches/soc-2013-viewport_fx/source/blender/gpu/intern/gpu_simple_shader.c
    branches/soc-2013-viewport_fx/source/blender/python/generic/bgl.c

Modified: branches/soc-2013-viewport_fx/CMakeLists.txt
===================================================================
--- branches/soc-2013-viewport_fx/CMakeLists.txt	2013-07-27 19:31:33 UTC (rev 58675)
+++ branches/soc-2013-viewport_fx/CMakeLists.txt	2013-07-27 20:32:10 UTC (rev 58676)
@@ -2069,7 +2069,7 @@
 		GLEW_INCLUDE_PATH
 	)
 else()
-	set(GL_DEFINITIONS "${GL_DEFINITIONS} -DGLEW_STATIC")
+	set(GL_DEFINITIONS "${GL_DEFINITIONS} -DGLEW_STATIC -DGLEW_MX")
 
 	if(WITH_GLEW_ES)
 		set(GLEW_INCLUDE_PATH "${CMAKE_SOURCE_DIR}/extern/glew-es/include")

Modified: branches/soc-2013-viewport_fx/extern/glew-es/include/GL/glew.h
===================================================================
--- branches/soc-2013-viewport_fx/extern/glew-es/include/GL/glew.h	2013-07-27 19:31:33 UTC (rev 58675)
+++ branches/soc-2013-viewport_fx/extern/glew-es/include/GL/glew.h	2013-07-27 20:32:10 UTC (rev 58676)
@@ -19008,7 +19008,7 @@
 GLEW_FUN_EXPORT PFNGLGETDEBUGMESSAGELOGPROC __glewGetDebugMessageLog;
 GLEW_FUN_EXPORT PFNGLGETOBJECTLABELPROC __glewGetObjectLabel;
 GLEW_FUN_EXPORT PFNGLGETOBJECTPTRLABELPROC __glewGetObjectPtrLabel;
-GLEW_FUN_EXPORT PFNGLGETPOINTERVPROC __glewGetPointerv;
+//GLEW_FUN_EXPORT PFNGLGETPOINTERVPROC __glewGetPointerv; // XXX jwilkins redefinition?
 GLEW_FUN_EXPORT PFNGLOBJECTLABELPROC __glewObjectLabel;
 GLEW_FUN_EXPORT PFNGLOBJECTPTRLABELPROC __glewObjectPtrLabel;
 GLEW_FUN_EXPORT PFNGLPOPDEBUGGROUPPROC __glewPopDebugGroup;

Modified: branches/soc-2013-viewport_fx/intern/ghost/intern/GHOST_Context.cpp
===================================================================
--- branches/soc-2013-viewport_fx/intern/ghost/intern/GHOST_Context.cpp	2013-07-27 19:31:33 UTC (rev 58675)
+++ branches/soc-2013-viewport_fx/intern/ghost/intern/GHOST_Context.cpp	2013-07-27 20:32:10 UTC (rev 58676)
@@ -30,3 +30,26 @@
  * Definition of GHOST_Context class.
  */
 
+#include "GHOST_Context.h"
+
+
+
+GLEWContext* glewContext = NULL;
+
+
+
+bool GHOST_Context::initGlew()
+{
+	if (m_glewContext == NULL) {
+		glewContext = new GLEWContext;
+		
+		if (glewInit() != GLEW_OK) {
+			delete glewContext;
+			glewContext = NULL;
+		}
+
+		m_glewContext = glewContext;
+	}
+	
+	return  m_glewContext != NULL ? GHOST_kSuccess : GHOST_kFailure;
+}

Modified: branches/soc-2013-viewport_fx/intern/ghost/intern/GHOST_Context.h
===================================================================
--- branches/soc-2013-viewport_fx/intern/ghost/intern/GHOST_Context.h	2013-07-27 19:31:33 UTC (rev 58675)
+++ branches/soc-2013-viewport_fx/intern/ghost/intern/GHOST_Context.h	2013-07-27 20:32:10 UTC (rev 58676)
@@ -35,16 +35,31 @@
 
 #include "GHOST_Types.h"
 
+#define glewGetContext() glewContext
+#include <GL/glew.h>
+extern "C" GLEWContext* glewContext;
 
+#include <cstdlib> // for NULL
 
+
+
 class GHOST_Context
 {
 public:
 	/**
+	 * Constructor.
+	 */
+	GHOST_Context()
+		: m_glewContext(NULL)
+	{}
+
+	/**
 	 * Destructor.
 	 */
 	virtual ~GHOST_Context()
-	{}
+	{
+		delete m_glewContext;
+	}
 
 	/**
 	 * Swaps front and back buffers of a window.
@@ -71,6 +86,17 @@
 	 * \return Indication as to whether removal has succeeded.
 	 */
 	virtual GHOST_TSuccess releaseNativeHandles() = 0;
+
+protected:
+	bool GHOST_Context::initGlew();
+
+	void GHOST_Context::activateGlew()
+	{
+		glewContext = m_glewContext;
+	}
+
+private:
+	GLEWContext* m_glewContext;
 };
 
 

Modified: branches/soc-2013-viewport_fx/intern/ghost/intern/GHOST_ContextWGL.cpp
===================================================================
--- branches/soc-2013-viewport_fx/intern/ghost/intern/GHOST_ContextWGL.cpp	2013-07-27 19:31:33 UTC (rev 58675)
+++ branches/soc-2013-viewport_fx/intern/ghost/intern/GHOST_ContextWGL.cpp	2013-07-27 20:32:10 UTC (rev 58676)
@@ -40,6 +40,10 @@
 
 
 
+extern "C" WGLEWContext* wglewContext = NULL;
+
+
+
 HGLRC GHOST_ContextWGL::s_sharedHGLRC = NULL;
 HDC   GHOST_ContextWGL::s_sharedHDC   = NULL;
 int   GHOST_ContextWGL::s_sharedCount = 0;
@@ -127,6 +131,7 @@
 	, m_contextFlags(contextFlags)
 	, m_contextMajorVersion(contextMajorVersion)
 	, m_contextMinorVersion(contextMinorVersion)
+	, m_wglewContext(NULL)
 {
 	assert(m_hWnd);
 	assert(m_hDC);
@@ -136,6 +141,8 @@
 
 GHOST_ContextWGL::~GHOST_ContextWGL()
 {
+	wglewContext = m_wglewContext;
+
 	if (m_hGLRC != NULL) {
 		if (m_hGLRC == ::wglGetCurrentContext())
 			WIN32_CHK(::wglMakeCurrent(NULL, NULL));
@@ -153,6 +160,8 @@
 			WIN32_CHK(::wglDeleteContext(m_hGLRC));
 		}
 	}
+
+	delete m_wglewContext;
 }
 
 
@@ -166,6 +175,8 @@
 
 GHOST_TSuccess GHOST_ContextWGL::activateDrawingContext()
 {
+	activateGlew();
+
 	return WIN32_CHK(::wglMakeCurrent(m_hDC, m_hGLRC)) ? GHOST_kSuccess : GHOST_kFailure;
 }
 
@@ -244,7 +255,7 @@
 
 		WIN32_CHK(check == lastPFD);
 
-		float w = weight_pixel_format(pfd);
+		int w = weight_pixel_format(pfd);
 
 		if (w > weight) {
 			weight = w;
@@ -323,11 +334,9 @@
 
 
 
-static bool init_wglew(HWND hWnd, HDC hDC, PIXELFORMATDESCRIPTOR& preferredPFD)
+bool GHOST_ContextWGL::initWGlew(PIXELFORMATDESCRIPTOR& preferredPFD)
 {
-	static bool wglewInitialized = false;
-
-	if (!wglewInitialized) {
+	if (m_wglewContext == NULL) {
 		HWND  dummyHWND  = NULL;
 		HDC   dummyHDC   = NULL;
 		HGLRC dummyHGLRC = NULL;
@@ -340,7 +349,7 @@
 		HGLRC prevHGLRC = ::wglGetCurrentContext();
 		WIN32_CHK(GetLastError() == NO_ERROR);
 
-		dummyHWND = clone_window(hWnd, NULL);
+		dummyHWND = clone_window(m_hWnd, NULL);
 
 		if (!WIN32_CHK(dummyHWND != NULL))
 			goto finalize;
@@ -370,9 +379,15 @@
 		if (!WIN32_CHK(::wglMakeCurrent(dummyHDC, dummyHGLRC)))
 			goto finalize;
 
-		if (wglewInit() == GLEW_OK)
-			wglewInitialized = true;
+		wglewContext = new WGLEWContext;
 
+		if (wglewInit() != GLEW_OK) {
+			delete wglewContext;
+			wglewContext = NULL;
+		}
+
+		m_wglewContext = wglewContext;
+
 		// the following are not technially WGLEW, but they also require a context to work
 
 #ifndef NDEBUG
@@ -396,12 +411,12 @@
 		}
 	}
 
-	return wglewInitialized;
+	return m_wglewContext != NULL;
 }
 
 
 
-static int _choose_pixel_format_arb_2(HDC hDC, bool stereoVisual, int numOfAASamples, int swapMethod)
+int GHOST_ContextWGL::_choose_pixel_format_arb_2(bool stereoVisual, int numOfAASamples, int swapMethod)
 {
 #define SAMPLES        iAttributes[1]
 #define SAMPLE_BUFFERS iAttributes[3]
@@ -435,7 +450,7 @@
 		SAMPLE_BUFFERS = SAMPLES > 0 ? 1 : 0;
 
 		UINT nNumFormats;
-		WIN32_CHK(wglChoosePixelFormatARB(hDC, iAttributes, NULL, 1, &iPixelFormat, &nNumFormats));
+		WIN32_CHK(wglChoosePixelFormatARB(m_hDC, iAttributes, NULL, 1, &iPixelFormat, &nNumFormats));
 
 		if (nNumFormats > 0) // total number of formats that match (regardless of size of iPixelFormat array)
 			break;
@@ -448,7 +463,7 @@
 	if (iPixelFormat != 0 && SAMPLES != numOfAASamples) {
 		fprintf(
 			stderr,
-			"Warning! Unable to find a multisample pixel format that supports %d samples.  Substituting one that uses %d samples.\n",
+			"Warning! Unable to find a multisample pixel format that supports %d samples. Substituting one that uses %d samples.\n",
 			numOfAASamples,
 			SAMPLES);
 	}
@@ -461,21 +476,21 @@
 
 
 
-static int _choose_pixel_format_arb_1(HDC hDC, bool stereoVisual, int numOfAASamples, int& swapMethodOut)
+int GHOST_ContextWGL::_choose_pixel_format_arb_1(bool stereoVisual, int numOfAASamples, int& swapMethodOut)
 {
 	int iPixelFormat;
 
 	swapMethodOut   = WGL_SWAP_COPY_ARB;
-	iPixelFormat = _choose_pixel_format_arb_2(hDC, stereoVisual, numOfAASamples, swapMethodOut);
+	iPixelFormat = _choose_pixel_format_arb_2(stereoVisual, numOfAASamples, swapMethodOut);
 
 	if (iPixelFormat == 0) {
 		swapMethodOut = WGL_SWAP_UNDEFINED_ARB;
-		iPixelFormat = _choose_pixel_format_arb_2(hDC, stereoVisual, numOfAASamples, swapMethodOut);
+		iPixelFormat = _choose_pixel_format_arb_2(stereoVisual, numOfAASamples, swapMethodOut);
 	}
 
 	if (iPixelFormat == 0) {
 		swapMethodOut   = WGL_SWAP_EXCHANGE_ARB;
-		iPixelFormat = _choose_pixel_format_arb_2(hDC, stereoVisual, numOfAASamples, swapMethodOut);
+		iPixelFormat = _choose_pixel_format_arb_2(stereoVisual, numOfAASamples, swapMethodOut);
 	}
 
 	return iPixelFormat;
@@ -483,23 +498,23 @@
 
 
 
-static int choose_pixel_format_arb(HDC hDC, bool stereoVisual, int numOfAASamples)
+int GHOST_ContextWGL::choose_pixel_format_arb(bool stereoVisual, int numOfAASamples)
 {
 	int iPixelFormat;
 	int swapMethod;
 
-	iPixelFormat = _choose_pixel_format_arb_1(hDC, stereoVisual, numOfAASamples, swapMethod);
+	iPixelFormat = _choose_pixel_format_arb_1(stereoVisual, numOfAASamples, swapMethod);
 
 	if (iPixelFormat == 0 && stereoVisual) {
 		fprintf(stderr, "Warning! Unable to find a stereo pixel format.\n");
 
-		iPixelFormat = _choose_pixel_format_arb_1(hDC, false, numOfAASamples, swapMethod);
+		iPixelFormat = _choose_pixel_format_arb_1(false, numOfAASamples, swapMethod);
 	}
 
 	if (swapMethod != WGL_SWAP_COPY_ARB) {
 		fprintf(
 			stderr,
-			"Warning! Unable to find a pixel format that supports WGL_SWAP_COPY_ARB.  Substituting one that uses %s.\n",
+			"Warning! Unable to find a pixel format that supports WGL_SWAP_COPY_ARB. Substituting one that uses %s.\n",
 			swapMethod == WGL_SWAP_UNDEFINED_ARB ? "WGL_SWAP_UNDEFINED_ARB" : "WGL_SWAP_EXCHANGE_ARB");
 	}
 
@@ -508,7 +523,7 @@
 
 
 
-static int choose_pixel_format(HWND hWnd, HDC hDC, bool stereoVisual, int numOfAASamples)
+int GHOST_ContextWGL::choose_pixel_format(bool stereoVisual, int numOfAASamples)
 {
 	/*
 	 * Color and depth bit values are not to be trusted.
@@ -547,18 +562,24 @@
 		0, 0, 0                          /* no layer, visible, damage masks */
 	};
 
-	if (!init_wglew(hWnd, hDC, preferredPFD))
+	if (!initWGlew(preferredPFD))
 		fprintf(stderr, "WGLEW failed to initialize.\n");
 
-	if (WGLEW_ARB_pixel_format) {
-		return choose_pixel_format_arb(hDC, stereoVisual, numOfAASamples);
+	wglewContext = m_wglewContext;
+
+	int iPixelFormat;
+
+	if (wglewContext != NULL && WGLEW_ARB_pixel_format) {
+		iPixelFormat = choose_pixel_format_arb(stereoVisual, numOfAASamples);
 	}
 	else {
 		if (numOfAASamples > 0)
-			return 0;
+			iPixelFormat = 0;
+		else
+			iPixelFormat = choose_pixel_format_legacy(m_hDC, preferredPFD);
+	}
 
-		return choose_pixel_format_legacy(hDC, preferredPFD);
-	}
+	return iPixelFormat;
 }
 
 
@@ -572,7 +593,7 @@
 	WIN32_CHK(GetLastError() == NO_ERROR);
 
 	if (m_needSetPixelFormat) {
-		int iPixelFormat = choose_pixel_format(m_hWnd, m_hDC, stereoVisual, numOfAASamples);
+		int iPixelFormat = choose_pixel_format(stereoVisual, numOfAASamples);
 
 		if (iPixelFormat == 0)
 			goto error;

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list