forked from cinder/Cinder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRendererImplGlMsw.cpp
More file actions
405 lines (337 loc) · 15.7 KB
/
Copy pathRendererImplGlMsw.cpp
File metadata and controls
405 lines (337 loc) · 15.7 KB
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/*
Copyright (c) 2012, The Cinder Project, All rights reserved.
This code is intended for use with the Cinder C++ library: http://libcinder.org
Redistribution and use in source and binary forms, with or without modification, are permitted provided that
the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions and
the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
the following disclaimer in the documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#if ! defined( CINDER_GL_ANGLE )
#include "cinder/gl/platform.h"
#include "cinder/app/msw/AppImplMsw.h"
#include "cinder/app/msw/RendererImplGlMsw.h"
#include "cinder/app/RendererGl.h"
#include "cinder/gl/Context.h"
#include "cinder/gl/Environment.h"
#include "glad/glad_wgl.h"
#include "cinder/app/AppBase.h"
#include "cinder/Camera.h"
#include "cinder/Log.h"
#include <windowsx.h>
namespace cinder { namespace app {
bool sMultisampleSupported = false;
int sArbMultisampleFormat;
typedef HGLRC (__stdcall * PFNWGLCREATECONTEXTATTRIBSARB) (HDC hDC, HGLRC hShareContext, const int *attribList);
typedef BOOL (__stdcall * PFNWGLCHOOSEPIXELFORMATARBPROC)(HDC hdc, const int * piAttribIList, const FLOAT * pfAttribFList, UINT nMaxFormats, int * piFormats, UINT * nNumFormats);
RendererImplGlMsw::RendererImplGlMsw( RendererGl *aRenderer )
: mRenderer( aRenderer )
{
mRC = 0;
}
void RendererImplGlMsw::prepareToggleFullScreen()
{
}
void RendererImplGlMsw::finishToggleFullScreen()
{
}
void RendererImplGlMsw::defaultResize() const
{
::RECT clientRect;
::GetClientRect( mWindowImpl->getHwnd(), &clientRect );
int widthPx = clientRect.right - clientRect.left;
int heightPx = clientRect.bottom - clientRect.top;
ivec2 sizePt = mWindowImpl->getSize();
gl::viewport( 0, 0, widthPx, heightPx );
gl::setMatricesWindow( sizePt.x, sizePt.y );
}
void RendererImplGlMsw::swapBuffers() const
{
::SwapBuffers( mWindowImpl->getDc() );
}
void RendererImplGlMsw::makeCurrentContext( bool force )
{
mCinderContext->makeCurrent( force );
}
// We can't use the normal mechanism for this test because we don't have a context yet
namespace {
HWND createDummyWindow()
{
DWORD windowExStyle, windowStyle;
WNDCLASS wc; // Windows Class Structure
RECT WindowRect; // Grabs Rectangle Upper Left / Lower Right Values
WindowRect.left = 0L;
WindowRect.right = 640L;
WindowRect.top = 0L;
WindowRect.bottom = 480L;
HINSTANCE instance = ::GetModuleHandle( NULL ); // Grab An Instance For Our Window
wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; // Redraw On Size, And Own DC For Window.
wc.lpfnWndProc = DefWindowProc; // WndProc Handles Messages
wc.cbClsExtra = 0; // No Extra Window Data
wc.cbWndExtra = 0; // No Extra Window Data
wc.hInstance = instance;
wc.hIcon = ::LoadIcon( NULL, IDI_WINLOGO ); // Load The Default Icon
wc.hCursor = ::LoadCursor( NULL, IDC_ARROW ); // Load The Arrow Pointer
wc.hbrBackground = NULL; // No Background Required For GL
wc.lpszMenuName = NULL; // We Don't Want A Menu
wc.lpszClassName = TEXT("FLINTTEMP");
if( ! ::RegisterClass( &wc ) ) { // Attempt To Register The Window Class
DWORD err = ::GetLastError();
return 0;
}
windowExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE | WS_EX_ACCEPTFILES; // Window Extended Style
windowStyle = ( WS_OVERLAPPEDWINDOW & ~WS_THICKFRAME );
::AdjustWindowRectEx( &WindowRect, windowStyle, FALSE, windowExStyle );
return ::CreateWindowEx( windowExStyle, TEXT("FLINTTEMP"), TEXT("FLINT"), windowStyle, 0, 0, WindowRect.right-WindowRect.left, WindowRect.bottom-WindowRect.top, NULL, NULL, instance, 0 );
}
bool getWglFunctionPointers( PFNWGLCREATECONTEXTATTRIBSARB *resultCreateContextAttribsFnPtr, PFNWGLCHOOSEPIXELFORMATARBPROC *resultChoosePixelFormatFnPtr )
{
static PFNWGLCREATECONTEXTATTRIBSARB cachedCreateContextAttribsFnPtr = nullptr;
static PFNWGLCHOOSEPIXELFORMATARBPROC cachedChoosePixelFormatFnPtr = nullptr;
if( ! cachedCreateContextAttribsFnPtr || ! cachedChoosePixelFormatFnPtr ) {
static PIXELFORMATDESCRIPTOR pfd = {
sizeof(PIXELFORMATDESCRIPTOR), // Size Of This Pixel Format Descriptor
1, // Version Number
PFD_DRAW_TO_WINDOW | // Format Must Support Window
PFD_SUPPORT_OPENGL | // Format Must Support OpenGL
PFD_DOUBLEBUFFER, // Must Support Double Buffering
PFD_TYPE_RGBA, // Request An RGBA Format
32, // Select Our Color Depth
0, 0, 0, 0, 0, 0, // Color Bits Ignored
0, // No Alpha Buffer
0, // Shift Bit Ignored
0, // No Accumulation Buffer
0, 0, 0, 0, // Accumulation Bits Ignored
16, // depth bits
0, // stencil bits
0, // No Auxiliary Buffer
PFD_MAIN_PLANE, // Main Drawing Layer
0, // Reserved
0, 0, 0 // Layer Masks Ignored
};
HWND tempWindow = createDummyWindow();
HDC tempDc = ::GetDC( tempWindow );
auto pixelFormat = ::ChoosePixelFormat( tempDc, &pfd );
if( pixelFormat == 0 ) {
::ReleaseDC( tempWindow, tempDc );
::DestroyWindow( tempWindow );
::UnregisterClass( TEXT("FLINTTEMP"), ::GetModuleHandle( NULL ) );
return false;
}
::SetPixelFormat( tempDc, pixelFormat, &pfd );
auto tempCtx = ::wglCreateContext( tempDc );
::wglMakeCurrent( tempDc, tempCtx );
cachedCreateContextAttribsFnPtr = (PFNWGLCREATECONTEXTATTRIBSARB) ::wglGetProcAddress( "wglCreateContextAttribsARB" );
cachedChoosePixelFormatFnPtr = (PFNWGLCHOOSEPIXELFORMATARBPROC) ::wglGetProcAddress( "wglChoosePixelFormatARB" );
*resultCreateContextAttribsFnPtr = cachedCreateContextAttribsFnPtr;
*resultChoosePixelFormatFnPtr = cachedChoosePixelFormatFnPtr;
::wglMakeCurrent( NULL, NULL );
::wglDeleteContext( tempCtx );
::ReleaseDC( tempWindow, tempDc );
::DestroyWindow( tempWindow );
::UnregisterClass( TEXT("FLINTTEMP"), ::GetModuleHandle( NULL ) );
if( ! cachedCreateContextAttribsFnPtr || ! cachedChoosePixelFormatFnPtr ) {
return false;
}
else
return true;
}
else {
*resultCreateContextAttribsFnPtr = cachedCreateContextAttribsFnPtr;
*resultChoosePixelFormatFnPtr = cachedChoosePixelFormatFnPtr;
return cachedCreateContextAttribsFnPtr && cachedChoosePixelFormatFnPtr;
}
}
HGLRC createContext( HDC dc, bool coreProfile, bool debug, bool profileSpecified, int majorVersion, int minorVersion, GLenum multigpu = 0 )
{
HGLRC result = 0;
static bool initializedLoadOGL = false; // kept from your original, in case it's used elsewhere
PFNWGLCREATECONTEXTATTRIBSARB wglCreateContextAttribsARBPtr = nullptr;
PFNWGLCHOOSEPIXELFORMATARBPROC wglChoosePixelFormatARBPtr = nullptr;
if( ! getWglFunctionPointers( &wglCreateContextAttribsARBPtr, &wglChoosePixelFormatARBPtr ) ) {
throw ExcRendererAllocation( "wglCreateContextAttribsARB / wglChoosePixelFormatARB unavailable" );
}
const int baseFlags = debug ? WGL_CONTEXT_DEBUG_BIT_ARB : 0;
auto tryCreate = [&]( int maj, int min ) -> HGLRC {
int attribList[32];
int idx = 0;
// Explicit version
attribList[idx++] = WGL_CONTEXT_MAJOR_VERSION_ARB;
attribList[idx++] = maj;
attribList[idx++] = WGL_CONTEXT_MINOR_VERSION_ARB;
attribList[idx++] = min;
// Flags (only if non-zero)
if( baseFlags != 0 ) {
attribList[idx++] = WGL_CONTEXT_FLAGS_ARB;
attribList[idx++] = baseFlags;
}
// Profile mask only if the user actually specified a profile
if( profileSpecified ) {
const int profileMask = coreProfile ? WGL_CONTEXT_CORE_PROFILE_BIT_ARB : WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB;
attribList[idx++] = WGL_CONTEXT_PROFILE_MASK_ARB;
attribList[idx++] = profileMask;
}
// Optional multigpu
if( multigpu != 0 ) {
attribList[idx++] = WGL_CONTEXT_MULTIGPU_ATTRIB_NV;
attribList[idx++] = multigpu;
}
// Terminator
attribList[idx++] = 0;
attribList[idx++] = 0;
return wglCreateContextAttribsARBPtr( dc, 0, attribList );
};
// version unspecified -> descending test of known versions
if( majorVersion == 0 && minorVersion == 0 ) {
static const int versions[][2] = { { 4, 6 }, { 4, 5 }, { 4, 4 }, { 4, 3 }, { 4, 2 }, { 4, 1 }, { 4, 0 }, { 3, 3 }, { 3, 2 } };
const int numVersions = (int)( sizeof( versions ) / sizeof( versions[0] ) );
for( int i = 0; i < numVersions; ++i ) {
HGLRC ctx = tryCreate( versions[i][0], versions[i][1] );
if( ctx )
return ctx; // first successful (highest in our list)
}
// If we get here, nothing in our list worked
throw ExcRendererAllocation( "Failed to create any modern OpenGL context (tried 4.6 down to 3.2)" );
}
else { // user specified a version - try only that one
result = tryCreate( majorVersion, minorVersion );
if( ! result )
throw ExcRendererAllocation( "Failed to create requested OpenGL context" );
}
return result;
}
bool testPixelFormat( HDC dc, int colorSamples, int depthDepth, int msaaSamples, int stencilDepth, int *resultFormat )
{
PFNWGLCREATECONTEXTATTRIBSARB wglCreateContextAttribsARBPtr = NULL;
PFNWGLCHOOSEPIXELFORMATARBPROC wglChoosePixelFormatARBPtr = NULL;
if( ! getWglFunctionPointers( &wglCreateContextAttribsARBPtr, &wglChoosePixelFormatARBPtr ) )
throw ExcRendererAllocation( "wglCreateContextAttribsARB / wglChoosePixelFormatARB unavailable" );
float fAttributes[] = {0,0};
std::vector<int> iAttributes;
iAttributes.push_back( WGL_DRAW_TO_WINDOW_ARB ); iAttributes.push_back( GL_TRUE );
iAttributes.push_back( WGL_SUPPORT_OPENGL_ARB ); iAttributes.push_back( GL_TRUE );
iAttributes.push_back( WGL_ACCELERATION_ARB ); iAttributes.push_back( WGL_FULL_ACCELERATION_ARB );
iAttributes.push_back( WGL_RED_BITS_ARB ); iAttributes.push_back( colorSamples );
iAttributes.push_back( WGL_GREEN_BITS_ARB ); iAttributes.push_back( colorSamples );
iAttributes.push_back( WGL_BLUE_BITS_ARB ); iAttributes.push_back( colorSamples );
if( colorSamples == 8 ) {
iAttributes.push_back( WGL_ALPHA_BITS_ARB ); iAttributes.push_back( colorSamples );
}
iAttributes.push_back( WGL_DEPTH_BITS_ARB ); iAttributes.push_back( depthDepth );
iAttributes.push_back( WGL_STENCIL_BITS_ARB ); iAttributes.push_back( stencilDepth );
iAttributes.push_back( WGL_DOUBLE_BUFFER_ARB ); iAttributes.push_back( GL_TRUE );
iAttributes.push_back( WGL_SAMPLES_ARB ); iAttributes.push_back( msaaSamples );
iAttributes.push_back( 0 ); iAttributes.push_back( 0 );
UINT numFormats;
int valid = (*wglChoosePixelFormatARBPtr)( dc, iAttributes.data(), fAttributes, 1, resultFormat, &numFormats );
if( valid && ( numFormats >= 1 ) )
return true;
else
return false;
}
bool setPixelFormat( HDC dc, const RendererGl::Options &options )
{
int format;
for( int colorDepth = options.getColorChannelDepth(); colorDepth >= 8; colorDepth -= 2 ) {
for( int depthDepth = options.getDepthBufferDepth(); depthDepth >= 16; depthDepth -= 8 ) {
for( int stencilDepth = options.getStencil() ? 8 : 0; stencilDepth >= 0; stencilDepth -= 8 ) {
for( int msaaSamples = options.getMsaa(); msaaSamples >= 0; msaaSamples >>= 1 ) {
if( testPixelFormat( dc, colorDepth, depthDepth, msaaSamples, stencilDepth, &format ) )
goto FOUND;
if( msaaSamples == 0 )
break;
}
}
}
}
return false;
FOUND:
PIXELFORMATDESCRIPTOR pfd;
::DescribePixelFormat( dc, format, sizeof(pfd), &pfd );
::SetPixelFormat( dc, format, &pfd );
return true;
}
GLenum getMultiGpuContextMode( const app::RendererGl::Options::MultiGpuModeNV& mode ) {
switch( mode ) {
case RendererGl::Options::MultiGpuModeNV::SINGLE: return WGL_CONTEXT_MULTIGPU_ATTRIB_SINGLE_NV; break;
case RendererGl::Options::MultiGpuModeNV::AFR: return WGL_CONTEXT_MULTIGPU_ATTRIB_AFR_NV; break;
case RendererGl::Options::MultiGpuModeNV::MULTICAST: return WGL_CONTEXT_MULTIGPU_ATTRIB_MULTICAST_NV; break;
case RendererGl::Options::MultiGpuModeNV::MULTI_DISPLAY_MULTICAST: return WGL_CONTEXT_MULTIGPU_ATTRIB_MULTI_DISPLAY_MULTICAST_NV; break;
default: return 0;
}
}
bool initializeGl( HWND /*wnd*/, HDC dc, HGLRC sharedRC, const RendererGl::Options &options, HGLRC *resultRc )
{
if( ! setPixelFormat( dc, options ) )
throw ExcRendererAllocation( "Failed to find suitable WGL pixel format" );
GLenum multigpu = 0;
if( options.isMultiGpuEnabledNV() )
multigpu = getMultiGpuContextMode( options.getMultiGpuModeNV() );
if( ! ( *resultRc = createContext( dc, options.getCoreProfile(), options.getDebug(), options.getProfileSpecified(), options.getVersion().first, options.getVersion().second, multigpu ) ) )
return false;
if( ! ::wglMakeCurrent( dc, *resultRc ) ) // Try To Activate The Rendering Context
return false;
gl::Environment::setCore();
gl::env()->initializeFunctionPointers();
gladLoadWGL( dc ); // Initialize WGL function pointers
::wglMakeCurrent( NULL, NULL );
if( sharedRC )
::wglShareLists( sharedRC, *resultRc );
::wglMakeCurrent( dc, *resultRc );
return true;
}
} // anonymous namespace
bool RendererImplGlMsw::initialize( WindowImplMsw *windowImpl, RendererRef sharedRenderer )
{
mWindowImpl = windowImpl;
RendererGl *sharedRendererGl = dynamic_cast<RendererGl*>( sharedRenderer.get() );
HGLRC sharedRC = ( sharedRenderer ) ? sharedRendererGl->mImpl->mRC : NULL;
if( ! initializeGl( mWindowImpl->getHwnd(), mWindowImpl->getDc(), sharedRC, mRenderer->getOptions(), &mRC ) ) {
return false;
}
gl::Environment::setCore();
auto platformData = std::shared_ptr<gl::Context::PlatformData>( new gl::PlatformDataMsw( mRC, mWindowImpl->getDc() ) );
platformData->mDebug = mRenderer->getOptions().getDebug();
platformData->mDebugLogSeverity = mRenderer->getOptions().getDebugLogSeverity();
platformData->mDebugBreakSeverity = mRenderer->getOptions().getDebugBreakSeverity();
platformData->mObjectTracking = mRenderer->getOptions().getObjectTracking();
platformData->mCoreProfile = mRenderer->getOptions().getCoreProfile();
// If the requested version was (0,0) meaning "highest available", resolve to
// the actual GL version. createSharedContext() passes mVersion directly to
// wglCreateContextAttribsARB, which fails on a literal 0.0.
platformData->mVersion = mRenderer->getOptions().getVersion();
if( platformData->mVersion.first == 0 && platformData->mVersion.second == 0 ) {
GLint major = 0, minor = 0;
glGetIntegerv( GL_MAJOR_VERSION, &major );
glGetIntegerv( GL_MINOR_VERSION, &minor );
platformData->mVersion = { major, minor };
}
#if ! defined( CINDER_GL_ES )
platformData->mMultiGpuEnabledNV = mRenderer->getOptions().isMultiGpuEnabledNV();
platformData->mMultiGpuModeNV = getMultiGpuContextMode( mRenderer->getOptions().getMultiGpuModeNV() );
#endif
mCinderContext = gl::Context::createFromExisting( platformData );
mCinderContext->makeCurrent();
return true;
}
void RendererImplGlMsw::kill()
{
if( mRC ) { // Do We Have A Rendering Context?
::wglMakeCurrent( NULL, NULL ); // release The DC And RC Contexts
::wglDeleteContext( mRC ); // delete The RC
}
mCinderContext.reset();
gl::Context::reflectCurrent( nullptr );
mRC = 0;
}
} } // namespace cinder::app
#endif // ! defined( CINDER_GL_ANGLE )