1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
|
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
#include "lib/streflop/streflop_cond.h" //! must happen before OffscreenGLContext.h, which includes agl.h
#include "System/OffscreenGLContext.h"
#include "System/Exceptions.h"
#include "System/maindefines.h"
#include "System/Platform/errorhandler.h"
#ifdef HEADLESS
/////////////////////////////////////////////////////////////////////////////////////////////////
//! Headless
COffscreenGLContext::COffscreenGLContext() {}
COffscreenGLContext::~COffscreenGLContext() {}
void COffscreenGLContext::WorkerThreadPost() {}
void COffscreenGLContext::WorkerThreadFree() {}
#elif WIN32
/////////////////////////////////////////////////////////////////////////////////////////////////
//! WINDOWS
#include <wingdi.h> //! wgl...
COffscreenGLContext::COffscreenGLContext()
{
//! this creates a 2nd OpenGL context on the >onscreen< window/HDC
//! so don't render to the the default framebuffer (always bind FBOs,DLs,...) !!!
//! get the main (onscreen) GL context
HGLRC mainRC = wglGetCurrentContext();
hdc = wglGetCurrentDC();
if (!hdc || !mainRC) {
throw opengl_error("Couldn't create an offscreen GL context: wglGetCurrentDC failed!");
}
//! create a 2nd GL context
offscreenRC = wglCreateContext(hdc);
if (!offscreenRC) {
throw opengl_error("Couldn't create an offscreen GL context: wglCreateContext failed!");
}
//! share the GL resources (textures,DLists,shaders,...)
if(!wglMakeCurrent(NULL, NULL))
throw opengl_error("Could not deactivate rendering context");
int status = wglShareLists(mainRC, offscreenRC);
if(!wglMakeCurrent(hdc, mainRC))
throw opengl_error("Could not activate rendering context");
if (!status) {
DWORD err = GetLastError();
char msg[256];
SNPRINTF(msg, 255, "Couldn't create an offscreen GL context: wglShareLists failed (error: %i)!", (int)err);
throw opengl_error(msg);
}
}
COffscreenGLContext::~COffscreenGLContext() {
if(!wglDeleteContext(offscreenRC))
throw opengl_error("Could not delete off-screen rendering context");
}
void COffscreenGLContext::WorkerThreadPost()
{
//! activate the offscreen GL context in the worker thread
if(!wglMakeCurrent(hdc, offscreenRC))
throw opengl_error("Could not activate worker rendering context");
}
void COffscreenGLContext::WorkerThreadFree()
{
//! must run in the same thread as the offscreen GL context!
if(!wglMakeCurrent(NULL, NULL))
throw opengl_error("Could not deactivate worker rendering context");
}
#elif __APPLE__
/////////////////////////////////////////////////////////////////////////////////////////////////
//! APPLE
#include <OpenGL/CGLCurrent.h>
#include <OpenGL/OpenGL.h>
COffscreenGLContext::COffscreenGLContext()
{
// Get Current OnScreen Context
CGLContextObj currentCglCtx = CGLGetCurrentContext();
if (!currentCglCtx)
throw opengl_error("Couldn't create an offscreen GL context: CGLGetCurrentContext failed!");
// Get PixelFormat
CGLPixelFormatAttribute attribs[] = {
(CGLPixelFormatAttribute)0
};
GLint numPixelFormats = 0;
CGLPixelFormatObj cglPxlfmt = NULL;
CGLChoosePixelFormat(attribs, &cglPxlfmt, &numPixelFormats);
if (!cglPxlfmt)
throw opengl_error("Couldn't create an offscreen GL context: CGLChoosePixelFmt failed!");
// Create Shared Context
CGLCreateContext(cglPxlfmt, currentCglCtx, &cglWorkerCtx);
CGLDestroyPixelFormat(cglPxlfmt);
if (!cglWorkerCtx)
throw opengl_error("Couldn't create an offscreen GL context: CGLCreateContext failed!");
}
COffscreenGLContext::~COffscreenGLContext() {
CGLDestroyContext(cglWorkerCtx);
}
void COffscreenGLContext::WorkerThreadPost()
{
CGLSetCurrentContext(cglWorkerCtx);
}
void COffscreenGLContext::WorkerThreadFree()
{
CGLSetCurrentContext(NULL);
}
#else
/////////////////////////////////////////////////////////////////////////////////////////////////
//! UNIX
#include <SDL.h>
#include <SDL_syswm.h>
COffscreenGLContext::COffscreenGLContext()
{
//! Get MainCtx & X11-Display
GLXContext mainCtx = glXGetCurrentContext();
display = glXGetCurrentDisplay();
//GLXDrawable mainDrawable = glXGetCurrentDrawable();
if(!mainCtx)
throw opengl_error("Couldn't create an offscreen GL context: glXGetCurrentContext failed!");
if (!display)
throw opengl_error("Couldn't create an offscreen GL context: Couldn't determine display!");
int scrnum = XDefaultScreen(display);
//! Create a FBConfig
int nelements = 0;
const int fbattribs[] = {
GLX_RENDER_TYPE, GLX_RGBA_BIT,
GLX_DRAWABLE_TYPE, GLX_PBUFFER_BIT,
GLX_BUFFER_SIZE, 32,
GLX_DEPTH_SIZE, 24,
GLX_STENCIL_SIZE, 8,
None
};
GLXFBConfig* fbcfg = glXChooseFBConfig(display, scrnum, (const int*)fbattribs, &nelements);
if (!fbcfg || (nelements == 0))
throw opengl_error("Couldn't create an offscreen GL context: glXChooseFBConfig failed!");
//! Create a pbuffer (each render context needs a drawable)
const int pbuf_attribs[] = {
GLX_PBUFFER_WIDTH, 1,
GLX_PBUFFER_HEIGHT, 1,
GLX_PRESERVED_CONTENTS, false,
None
};
pbuf = glXCreatePbuffer(display, fbcfg[0], (const int*)pbuf_attribs);
if (!pbuf)
throw opengl_error("Couldn't create an offscreen GL context: glXCreatePbuffer failed!");
/*
//Create a GL 3.0 context
const int ctx_attribs[] = {
//GLX_CONTEXT_MAJOR_VERSION_ARB, 2,
//GLX_CONTEXT_MINOR_VERSION_ARB, 0,
GLX_CONTEXT_FLAGS_ARB,
GLX_CONTEXT_DEBUG_BIT_ARB,
//GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB,
None
};
workerCtx = glXCreateContextAttribsARB(display, fbcfg[0], mainCtx, true, ctx_attribs);
*/
//! Create render context
workerCtx = glXCreateNewContext(display, fbcfg[0], GLX_RGBA_TYPE, mainCtx, true);
if (!workerCtx)
throw opengl_error("Couldn't create an offscreen GL context: glXCreateNewContext failed!");
XFree(fbcfg);
}
COffscreenGLContext::~COffscreenGLContext() {
glXDestroyContext(display, workerCtx);
glXDestroyPbuffer(display, pbuf);
}
void COffscreenGLContext::WorkerThreadPost()
{
glXMakeCurrent(display, pbuf, workerCtx);
}
void COffscreenGLContext::WorkerThreadFree()
{
glXMakeCurrent(display, None, NULL);
}
#endif
/******************************************************************************/
/******************************************************************************/
COffscreenGLThread::COffscreenGLThread(boost::function<void()> f) :
thread(NULL),
glOffscreenCtx() //! may trigger an opengl_error exception!
{
thread = new boost::thread( boost::bind(&COffscreenGLThread::WrapFunc, this, f) );
}
COffscreenGLThread::~COffscreenGLThread()
{
if (thread)
Join();
delete thread; thread = NULL;
}
bool COffscreenGLThread::IsFinished(boost::posix_time::time_duration wait)
{
return thread->timed_join(wait);
}
void COffscreenGLThread::Join()
{
while(thread->joinable())
if(thread->timed_join(boost::posix_time::seconds(1)))
break;
}
void COffscreenGLThread::WrapFunc(boost::function<void()> f)
{
glOffscreenCtx.WorkerThreadPost();
#ifdef STREFLOP_H
// init streflop to make it available for synced computations, too
// redundant? threads copy the FPU state of their parent.
streflop_init<streflop::Simple>();
#endif
try {
f();
} catch(boost::thread_interrupted const&) {
// do nothing
} CATCH_SPRING_ERRORS
glOffscreenCtx.WorkerThreadFree();
}
/******************************************************************************/
|