Skip to content

Commit d76c2cc

Browse files
committed
Add a new hint SDL_HINT_VIDEO_WINDOW_SHARE_PIXEL_FORMAT that allows SDL_CreateWindowFrom() to set the pixel format of another SDL_Window (and also will set the SDL_WINDOW_OPENGL flag on the window created with SDL_CreateWindowFrom()).
The reasoning behind this change is that source2 in -tools mode has a single OpenGL context that is used with multiple different windows. Some of those windows are created outside the engine (i.e. with Qt) and therefore we need to use SDL_CreateWindowFrom() to get an SDL_Window for those. The requirement for sharing an OpenGL context across multiple different windows is that each window has the same pixel format. To facilitate this, I now set SDL_HINT_VIDEO_WINDOW_SHARE_PIXEL_FORMAT for the main window before calling SDL_CreateWindowFrom(). When I do this, SDL_CreateWindowFrom() will: 1. Set the pixel format of the returned window to the same pixel format as this SDL_Window passed in with the hint 2. The flag SDL_WINDOW_OPENGL will be set on the new window so it can be used for OpenGL rendering. I only currently implemented this for Win32/WGL so implementing it for other platforms (i.e. X11) remains a TODO. CR: SamL Some pseudocode that shows how this is used in Source2: HWND hExternalHwnd; // HWND that was established outside of SDL // Create main window (happens inside platwindow.cpp) SDL_Window *mainWindow = SDL_CreateWindow( , SDL_WINDOW_OPENGL .. ); // Create GL context, happens inside rendersystemgl SDL_GLContext onlyContext = SDL_GL_CreateContext( mainWindow ); // Now I need to create another window from hEternalHwnd for my swap chain that will have the same pixel format as mainWindow, so set the hint SDL_SetHint( SDL_HINT_VIDEO_WINDOW_SHARE_PIXEL_FORMAT, CFmtStr( %p, mainWindow) ); // Create the secondary window. This returned window will have SDL_WINDOW_OPENGL set and share a pixel format with mainWindow from the hint SDL_Window *secondaryWindow = SDL_CreateWindowFrom( hExternalHwnd ); // To render to the main window: SDL_GL_MakeCurrent( mainWindow, onlyContext ); // Do some rendering to main window // To render to the secondary window: SDL_GLMakeCurrent( secondaryWindow, onlyContext ); // Do some rendering to secondary window
1 parent dbd4a91 commit d76c2cc

File tree

4 files changed

+66
-0
lines changed

4 files changed

+66
-0
lines changed

include/SDL_hints.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,25 @@ extern "C" {
328328
*/
329329
#define SDL_HINT_VIDEO_WIN_D3DCOMPILER "SDL_VIDEO_WIN_D3DCOMPILER"
330330

331+
/**
332+
* \brief A variable that is the address of another SDL_Window* (as a hex string formatted with "%p").
333+
*
334+
* If this hint is set before SDL_CreateWindowFrom() and the SDL_Window* it is set to has
335+
* SDL_WINDOW_OPENGL set (and running on WGL only, currently), then two things will occur on the newly
336+
* created SDL_Window:
337+
338+
* 1. Its pixel format will be set to the same pixel format as this SDL_Window. This is
339+
* needed for example when sharing an OpenGL context across multiple windows.
340+
*
341+
* 2. The flag SDL_WINDOW_OPENGL will be set on the new window so it can be used for
342+
* OpenGL rendering.
343+
*
344+
* This variable can be set to the following values:
345+
* The address (as a string "%p") of the SDL_Window* that new windows created with SDL_CreateWindowFrom() should
346+
* share a pixel format with.
347+
*/
348+
#define SDL_HINT_VIDEO_WINDOW_SHARE_PIXEL_FORMAT "SDL_VIDEO_WINDOW_SHARE_PIXEL_FORMAT"
349+
331350
/**
332351
* \brief An enumeration of hint priorities
333352
*/

src/video/windows/SDL_windowsopengl.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -759,6 +759,26 @@ WIN_GL_DeleteContext(_THIS, SDL_GLContext context)
759759
_this->gl_data->wglDeleteContext((HGLRC) context);
760760
}
761761

762+
763+
SDL_bool
764+
WIN_GL_SetPixelFormatFrom(_THIS, SDL_Window * fromWindow, SDL_Window * toWindow)
765+
{
766+
HDC hfromdc = ((SDL_WindowData *) fromWindow->driverdata)->hdc;
767+
HDC htodc = ((SDL_WindowData *) toWindow->driverdata)->hdc;
768+
BOOL result;
769+
770+
/* get the pixel format of the fromWindow */
771+
int pixel_format = GetPixelFormat(hfromdc);
772+
PIXELFORMATDESCRIPTOR pfd;
773+
SDL_memset(&pfd, 0, sizeof(pfd));
774+
DescribePixelFormat(hfromdc, pixel_format, sizeof(pfd), &pfd);
775+
776+
/* set the pixel format of the toWindow */
777+
result = SetPixelFormat(htodc, pixel_format, &pfd);
778+
779+
return result ? SDL_TRUE : SDL_FALSE;
780+
}
781+
762782
#endif /* SDL_VIDEO_OPENGL_WGL */
763783

764784
#endif /* SDL_VIDEO_DRIVER_WINDOWS */

src/video/windows/SDL_windowsopengl.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ extern int WIN_GL_GetSwapInterval(_THIS);
6464
extern void WIN_GL_SwapWindow(_THIS, SDL_Window * window);
6565
extern void WIN_GL_DeleteContext(_THIS, SDL_GLContext context);
6666
extern void WIN_GL_InitExtensions(_THIS);
67+
extern SDL_bool WIN_GL_SetPixelFormatFrom(_THIS, SDL_Window * fromWindow, SDL_Window * toWindow);
6768

6869
#ifndef WGL_ARB_pixel_format
6970
#define WGL_NUMBER_PIXEL_FORMATS_ARB 0x2000

src/video/windows/SDL_windowswindow.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
#include "SDL_windowsvideo.h"
3434
#include "SDL_windowswindow.h"
35+
#include "SDL_hints.h"
3536

3637
/* Dropfile support */
3738
#include <shellapi.h>
@@ -337,6 +338,31 @@ WIN_CreateWindowFrom(_THIS, SDL_Window * window, const void *data)
337338
if (SetupWindowData(_this, window, hwnd, SDL_FALSE) < 0) {
338339
return -1;
339340
}
341+
342+
#if SDL_VIDEO_OPENGL_WGL
343+
{
344+
const char *hint = SDL_GetHint(SDL_HINT_VIDEO_WINDOW_SHARE_PIXEL_FORMAT);
345+
if (hint) {
346+
// This hint is a pointer (in string form) of the address of
347+
// the window to share a pixel format with
348+
SDL_Window *otherWindow = NULL;
349+
SDL_sscanf(hint, "%p", (void**)&otherWindow);
350+
351+
// Do some error checking on the pointer
352+
if (otherWindow != NULL && otherWindow->magic == &_this->window_magic)
353+
{
354+
// If the otherWindow has SDL_WINDOW_OPENGL set, set it for the new window as well
355+
if (otherWindow->flags & SDL_WINDOW_OPENGL)
356+
{
357+
window->flags |= SDL_WINDOW_OPENGL;
358+
if(!WIN_GL_SetPixelFormatFrom(_this, otherWindow, window)) {
359+
return -1;
360+
}
361+
}
362+
}
363+
}
364+
}
365+
#endif
340366
return 0;
341367
}
342368

0 commit comments

Comments
 (0)