[Bf-blender-cvs] [8529b2f] master: BGE: alpha on frame buffer and precedence of MSAA over swap.

Benoit Bolsee noreply at git.blender.org
Thu Jun 9 22:15:37 CEST 2016


Commit: 8529b2f925c2a0f0cfa3ffe9218519b55223107e
Author: Benoit Bolsee
Date:   Thu Jun 9 21:07:58 2016 +0200
Branches: master
https://developer.blender.org/rB8529b2f925c2a0f0cfa3ffe9218519b55223107e

BGE: alpha on frame buffer and precedence of MSAA over swap.

A new option '-a' can be passed to the blenderplayer. It forces the
framebuffer to have an alpha channel.
This can be used in VideoTexture to return a image with alpha channel
with ImageViewport (provided alpha is set to True on the ImageViewport
object and that the background color alpha channel is 0, which is the
default).
Without the -a option, the frame buffer has no alpha channel and
ImageViewport always returns an opaque image, no matter what.
In Linux, the player window will be rendered transparently over
the desktop.
In Windows, the player window is still rendered opaque because
transparency of the window is only possible using the 'compositing'
functions of Windows. The code is there but not enabled  (look for
WIN32_COMPOSITING) because 1) it doesn't work so well 2) it requires
a DLL that is only available on Vista and up.

give precedence to AA over Swap copy:

Certain GPU (intel) will not allow MSAA together with swap copy.
Previously, swap copy had priority over MSAA: fewer AA samples would be
chosen if it was the condition to get swap copy. This patch reverse the
logic: swap copy will be abandonned if another swap method (undefined or
exchange) will provide the number of AA samples requested. If no AA
samples is requested, swap copy still has the priority of course.

===================================================================

M	CMakeLists.txt
M	intern/ghost/CMakeLists.txt
M	intern/ghost/GHOST_ISystem.h
M	intern/ghost/GHOST_Types.h
M	intern/ghost/intern/GHOST_ContextGLX.cpp
M	intern/ghost/intern/GHOST_ContextGLX.h
M	intern/ghost/intern/GHOST_ContextWGL.cpp
M	intern/ghost/intern/GHOST_ContextWGL.h
M	intern/ghost/intern/GHOST_System.cpp
M	intern/ghost/intern/GHOST_System.h
M	intern/ghost/intern/GHOST_SystemWin32.cpp
M	intern/ghost/intern/GHOST_SystemX11.cpp
M	intern/ghost/intern/GHOST_WindowWin32.cpp
M	intern/ghost/intern/GHOST_WindowWin32.h
M	intern/ghost/intern/GHOST_WindowX11.cpp
M	intern/ghost/intern/GHOST_WindowX11.h
M	source/gameengine/GamePlayer/ghost/GPG_Application.cpp
M	source/gameengine/GamePlayer/ghost/GPG_Application.h
M	source/gameengine/GamePlayer/ghost/GPG_ghost.cpp

===================================================================

diff --git a/CMakeLists.txt b/CMakeLists.txt
index b558fe1..15ee834 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -273,6 +273,7 @@ endif()
 if(WITH_X11)
 	option(WITH_X11_XINPUT    "Enable X11 Xinput (tablet support and unicode input)"  ON)
 	option(WITH_X11_XF86VMODE "Enable X11 video mode switching"                       ON)
+	option(WITH_X11_ALPHA     "Enable X11 transparent background"                     ON)
 endif()
 
 if(UNIX AND NOT APPLE)
@@ -723,6 +724,7 @@ if(WITH_GHOST_SDL OR WITH_HEADLESS)
 	set(WITH_X11           OFF)
 	set(WITH_X11_XINPUT    OFF)
 	set(WITH_X11_XF86VMODE OFF)
+	set(WITH_X11_ALPHA     OFF)
 	set(WITH_GHOST_XDND    OFF)
 	set(WITH_INPUT_IME     OFF)
 endif()
@@ -862,6 +864,16 @@ if(WITH_X11)
 		endif()
 	endif()
 
+	if(WITH_X11_ALPHA)
+		find_library(X11_Xrender_LIB Xrender  ${X11_LIB_SEARCH_PATH})
+		mark_as_advanced(X11_Xrender_LIB)
+		if (X11_Xrender_LIB)
+			list(APPEND PLATFORM_LINKLIBS ${X11_Xrender_LIB})
+		else()
+			set(WITH_X11_ALPHA OFF)
+		endif()
+	endif()
+
 endif()
 
 
@@ -3199,6 +3211,7 @@ if(FIRST_RUN)
 
 	info_cfg_text("System Options:")
 	info_cfg_option(WITH_INSTALL_PORTABLE)
+	info_cfg_option(WITH_X11_ALPHA)
 	info_cfg_option(WITH_X11_XF86VMODE)
 	info_cfg_option(WITH_X11_XINPUT)
 	info_cfg_option(WITH_MEM_JEMALLOC)
diff --git a/intern/ghost/CMakeLists.txt b/intern/ghost/CMakeLists.txt
index d31e947..e549a48 100644
--- a/intern/ghost/CMakeLists.txt
+++ b/intern/ghost/CMakeLists.txt
@@ -223,6 +223,10 @@ elseif(WITH_X11)
 		)
 	endif()
 
+	if(WITH_X11_ALPHA)
+		add_definitions(-DWITH_X11_ALPHA)
+	endif()
+
 	if(WITH_INPUT_NDOF)
 		list(APPEND SRC
 			intern/GHOST_NDOFManagerUnix.cpp
diff --git a/intern/ghost/GHOST_ISystem.h b/intern/ghost/GHOST_ISystem.h
index 4c48473..08045b9 100644
--- a/intern/ghost/GHOST_ISystem.h
+++ b/intern/ghost/GHOST_ISystem.h
@@ -277,7 +277,7 @@ public:
 	 */
 	virtual GHOST_TSuccess beginFullScreen(
 	        const GHOST_DisplaySetting& setting, GHOST_IWindow **window,
-	        const bool stereoVisual, const GHOST_TUns16 numOfAASamples = 0) = 0;
+	        const bool stereoVisual, const bool alphaBackground = 0, const GHOST_TUns16 numOfAASamples = 0) = 0;
 
 	/**
 	 * Updates the resolution while in fullscreen mode.
diff --git a/intern/ghost/GHOST_Types.h b/intern/ghost/GHOST_Types.h
index b3e560a..7e77ba3 100644
--- a/intern/ghost/GHOST_Types.h
+++ b/intern/ghost/GHOST_Types.h
@@ -57,7 +57,8 @@ typedef struct {
 
 typedef enum {
 	GHOST_glStereoVisual = (1 << 0),
-	GHOST_glDebugContext = (1 << 1)
+	GHOST_glDebugContext = (1 << 1),
+	GHOST_glAlphaBackground = (1 << 2),
 } GHOST_GLFlags;
 
 
diff --git a/intern/ghost/intern/GHOST_ContextGLX.cpp b/intern/ghost/intern/GHOST_ContextGLX.cpp
index 90d810b..4b735bb 100644
--- a/intern/ghost/intern/GHOST_ContextGLX.cpp
+++ b/intern/ghost/intern/GHOST_ContextGLX.cpp
@@ -62,6 +62,7 @@ GHOST_ContextGLX::GHOST_ContextGLX(
         Window window,
         Display *display,
         XVisualInfo *visualInfo,
+        GLXFBConfig fbconfig,
         int contextProfileMask,
         int contextMajorVersion,
         int contextMinorVersion,
@@ -70,6 +71,7 @@ GHOST_ContextGLX::GHOST_ContextGLX(
     : GHOST_Context(stereoVisual, numOfAASamples),
       m_display(display),
       m_visualInfo(visualInfo),
+      m_fbconfig(fbconfig),
       m_window(window),
       m_contextProfileMask(contextProfileMask),
       m_contextMajorVersion(contextMajorVersion),
@@ -285,19 +287,25 @@ const bool GLXEW_ARB_create_context_robustness =
 		attribs[i++] = 0;
 
 		/* Create a GL 3.x context */
-		GLXFBConfig *framebuffer_config = NULL;
+		if (m_fbconfig)
 		{
-			int glx_attribs[64];
-			int fbcount = 0;
+			m_context = glXCreateContextAttribsARB(m_display, m_fbconfig, s_sharedContext, true, attribs);
+		}
+		else {
+			GLXFBConfig *framebuffer_config = NULL;
+			{
+				int glx_attribs[64];
+				int fbcount = 0;
 
-			GHOST_X11_GL_GetAttributes(glx_attribs, 64, m_numOfAASamples, m_stereoVisual, true);
+				GHOST_X11_GL_GetAttributes(glx_attribs, 64, m_numOfAASamples, m_stereoVisual, false, true);
 
-			framebuffer_config = glXChooseFBConfig(m_display, DefaultScreen(m_display), glx_attribs, &fbcount);
-		}
+				framebuffer_config = glXChooseFBConfig(m_display, DefaultScreen(m_display), glx_attribs, &fbcount);
+			}
 
-		if (framebuffer_config) {
-			m_context = glXCreateContextAttribsARB(m_display, framebuffer_config[0], s_sharedContext, True, attribs);
-			XFree(framebuffer_config);
+			if (framebuffer_config) {
+				m_context = glXCreateContextAttribsARB(m_display, framebuffer_config[0], s_sharedContext, True, attribs);
+				XFree(framebuffer_config);
+			}
 		}
 	}
 	else {
@@ -401,16 +409,11 @@ GHOST_TSuccess GHOST_ContextGLX::getSwapInterval(int &intervalOut)
 int GHOST_X11_GL_GetAttributes(
         int *attribs, int attribs_max,
         int samples, bool is_stereo_visual,
+        bool need_alpha,
         bool for_fb_config)
 {
 	int i = 0;
 
-#ifdef GHOST_OPENGL_ALPHA
-	const bool need_alpha = true;
-#else
-	const bool need_alpha = false;
-#endif
-
 #ifdef GHOST_OPENGL_STENCIL
 	const bool need_stencil = true;
 #else
diff --git a/intern/ghost/intern/GHOST_ContextGLX.h b/intern/ghost/intern/GHOST_ContextGLX.h
index 8c2231a..f0f010d 100644
--- a/intern/ghost/intern/GHOST_ContextGLX.h
+++ b/intern/ghost/intern/GHOST_ContextGLX.h
@@ -66,6 +66,7 @@ public:
 	        Window window,
 	        Display *display,
 	        XVisualInfo *visualInfo,
+	        GLXFBConfig fbconfig,
 	        int contextProfileMask,
 	        int contextMajorVersion,
 	        int contextMinorVersion,
@@ -128,6 +129,7 @@ private:
 
 	Display *m_display;
 	XVisualInfo *m_visualInfo;
+	GLXFBConfig m_fbconfig;
 	Window   m_window;
 
 	const int m_contextProfileMask;
@@ -151,6 +153,7 @@ private:
 int GHOST_X11_GL_GetAttributes(
         int *attribs, int attribs_max,
         int samples, bool is_stereo_visual,
+	bool need_alpha,
         bool for_fb_config);
 
 #endif // __GHOST_CONTEXTGLX_H__
diff --git a/intern/ghost/intern/GHOST_ContextWGL.cpp b/intern/ghost/intern/GHOST_ContextWGL.cpp
index cb580f6..75e2f94 100644
--- a/intern/ghost/intern/GHOST_ContextWGL.cpp
+++ b/intern/ghost/intern/GHOST_ContextWGL.cpp
@@ -63,6 +63,7 @@ static bool is_crappy_intel_card()
 
 GHOST_ContextWGL::GHOST_ContextWGL(
         bool stereoVisual,
+		bool alphaBackground,
         GHOST_TUns16 numOfAASamples,
         HWND hWnd,
         HDC hDC,
@@ -78,6 +79,7 @@ GHOST_ContextWGL::GHOST_ContextWGL(
       m_contextMajorVersion(contextMajorVersion),
       m_contextMinorVersion(contextMinorVersion),
       m_contextFlags(contextFlags),
+	  m_alphaBackground(alphaBackground),
       m_contextResetNotificationStrategy(contextResetNotificationStrategy),
       m_hGLRC(NULL)
 #ifdef WITH_GLEW_MX
@@ -168,7 +170,7 @@ GHOST_TSuccess GHOST_ContextWGL::activateDrawingContext()
 /* Ron Fosner's code for weighting pixel formats and forcing software.
  * See http://www.opengl.org/resources/faq/technical/weight.cpp
  */
-static int weight_pixel_format(PIXELFORMATDESCRIPTOR &pfd)
+static int weight_pixel_format(PIXELFORMATDESCRIPTOR &pfd, PIXELFORMATDESCRIPTOR &preferredPFD)
 {
 	int weight = 0;
 
@@ -194,11 +196,12 @@ static int weight_pixel_format(PIXELFORMATDESCRIPTOR &pfd)
 
 	weight += pfd.cColorBits -  8;
 
-#ifdef GHOST_OPENGL_ALPHA
-	if (pfd.cAlphaBits > 0)
+	if (preferredPFD.cAlphaBits > 0 && pfd.cAlphaBits > 0)
+		weight++;
+#ifdef WIN32_COMPOSITING
+	if ((preferredPFD.dwFlags & PFD_SUPPORT_COMPOSITION) && (pfd.dwFlags & PFD_SUPPORT_COMPOSITION))
 		weight++;
 #endif
-
 #ifdef GHOST_OPENGL_STENCIL
 	if (pfd.cStencilBits >= 8)
 		weight++;
@@ -239,7 +242,7 @@ static int choose_pixel_format_legacy(HDC hDC, PIXELFORMATDESCRIPTOR &preferredP
 
 		WIN32_CHK(check == lastPFD);
 
-		int w = weight_pixel_format(pfd);
+		int w = weight_pixel_format(pfd, preferredPFD);
 
 		if (w > weight) {
 			weight = w;
@@ -496,7 +499,10 @@ int GHOST_ContextWGL::_choose_pixel_format_arb_2(
 {
 	std::vector<int> iAttributes;
 
+#define _MAX_PIXEL_FORMATS 32
+
 	int iPixelFormat = 0;
+	int iPixelFormats[_MAX_PIXEL_FORMATS];
 
 	int samples;
 
@@ -521,8 +527,31 @@ int GHOST_ContextWGL::_choose_pixel_format_arb_2(
 		        sRGB);
 
 		UINT nNumFormats;
-		WIN32_CHK(wglChoosePixelFormatARB(m_hDC, &(iAttributes[0]), NULL, 1, &iPixelFormat, &nNumFormats));
-
+		WIN32_CHK(wglChoosePixelFormatARB(m_hDC, &(iAttributes[0]), NULL, _MAX_PIXEL_FORMATS, iPixelFormats, &nNumFormats));
+
+#ifdef WIN32_COMPOSITING
+		if (needAlpha && nNumFormats) {
+			// scan through all pixel format to make sure one supports compositing
+			PIXELFORMATDESCRIPTOR pfd;
+			int i;
+
+			for (i = 0; i < nNumFormats; i++) {
+				if (DescribePixelFormat(m_hDC, iPixelFormats[i], sizeof(PIXELFORMATDESCRIPTOR), &pfd)) {
+					if (pfd.dwFlags & PFD_SUPPORT_COMPOSITION) {
+						iPixelFormat = iPixelFormats[i];
+						break;
+					}
+				}
+			}
+			if (i == nNumFormats) {
+				fprintf(stderr,
+						"Warning! Unable to find a pixel format with compositing capability.\n");
+				iPixelFormat = iPixelFormats[0];
+			}
+		}
+		else
+#endif
+			iPixelFormat = iPixelFormats[0];
 		/* total number of formats that match (regardless of size of iPixelFormat array)
 		 * see: WGL_ARB_pixel_format extension spec */
 		if (nNumFormats > 0)
@@ -538,7 +567,7 @@ int GHOST_ContextWGL::_choose_pixel_format_arb_2(
 	// check how many samples were actually gotten
 	if (iPixelFormat != 0) {
 		int iQuery[] = { WGL_SAMPLES_ARB };
-		int actualSamples;
+		int actualSamples, alphaBits;
 		wglGetPixelFormatAttribivARB(m_hDC, iPixelFormat, 0, 1, iQuery, &actualSamples);
 
 		if (actualSamples != *numOfAASamples) {
@@ -549,6 +578,14 @@ int GHOST_ContextWGL::_choose_pixel_format_arb_2(
 
 			*numOfAASamples = actualSamples; // set context property to actual value
 		}
+		if (needAlpha) {
+			iQuery[0] = WGL_ALPHA_BITS_ARB;
+			wglGetPixelFormatAttribivARB(m_hDC, iPixelFormat, 0, 1, iQuery, &alphaBits);
+			if (alphaBits == 0) {
+				fprintf(stderr,
+						"Warning! Unable to find a frame buffer with alpha channel.\n");
+			}
+		}
 	}
 	else {
 		*numOfAASamples = 0;
@@ -674,9 +711,15 @@ int GHOST_ContextWGL::choose_pixel_format(
 		PFD_DRAW_TO_WINDOW |
 		PFD_SWAP_COPY      |        

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list