Subversion Repository Public Repository

Divide-Framework

This repository has no backups
This repository's network speed is throttled to 100KB/sec

Diff Revisions 167 vs 168 for /trunk/Source Code/Hardware/Video/OpenGL/GLFWWrapper.cpp

Diff revisions: vs.
  @@ -47,7 +47,7 @@
47 47 ///GLEW_MX requirement
48 48 static boost::thread_specific_ptr<GLEWContext> _GLEWContextPtr;
49 49 GLEWContext * glewGetContext(){ return _GLEWContextPtr.get(); }
50 - #if defined(_WIN32)
50 + #if defined( OS_WINDOWS )
51 51 static boost::thread_specific_ptr<WGLEWContext> _WGLEWContextPtr;
52 52 WGLEWContext* wglewGetContext(){ return _WGLEWContextPtr.get(); }
53 53 #else
  @@ -80,7 +80,7 @@
80 80 exit(GLEW_INIT_ERROR);
81 81 }
82 82 #ifdef GLEW_MX
83 - #if defined(_WIN32)
83 + #if defined( OS_WINDOWS )
84 84
85 85 WGLEWContext * currentWGLEWContextsPtr = _WGLEWContextPtr.get();
86 86 if (currentWGLEWContextsPtr == NULL) {
  @@ -139,14 +139,13 @@
139 139
140 140 if(_msaaSamples > 1 && _useMSAA) glfwWindowHint(GLFW_SAMPLES, _msaaSamples);
141 141
142 - if(par.getParam<bool>("rendering.overrideRefreshRate",false)){
143 - //glfwWindowHint(GLFW_REFRESH_RATE,par.getParam<U8>("rendering.targetRefreshRate",75));
142 + if(par.getParam<bool>("runtime.overrideRefreshRate",false)){
143 + //glfwWindowHint(GLFW_REFRESH_RATE,par.getParam<U8>("runtime.targetRefreshRate",75));
144 144 }
145 145
146 - GLint glMinorVer = static_cast<GLint>(par.getParam<GLubyte>("runtime.GLminorVer",2));
147 - glfwWindowHint(GLFW_RESIZABLE,!par.getParam<bool>("runtime.allowWindowResize",true));
146 + glfwWindowHint(GLFW_RESIZABLE,par.getParam<bool>("runtime.allowWindowResize",false));
148 147 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR,3);
149 - glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR,glMinorVer);
148 + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR,3);
150 149 ///32Bit RGBA (R8G8B8A8), 24bit Depth, 8bit Stencil
151 150 glfwWindowHint(GLFW_RED_BITS,8);
152 151 glfwWindowHint(GLFW_GREEN_BITS,8);
  @@ -154,13 +153,8 @@
154 153 glfwWindowHint(GLFW_ALPHA_BITS,8);
155 154 glfwWindowHint(GLFW_DEPTH_BITS,24);
156 155 glfwWindowHint(GLFW_STENCIL_BITS,8);
156 + glfwWindowHint(GLFW_OPENGL_PROFILE, par.getParam<bool>("runtime.useGLCompatProfile",true) ? GLFW_OPENGL_COMPAT_PROFILE : GLFW_OPENGL_CORE_PROFILE);
157 157
158 - if(glMinorVer > 1 && par.getParam<bool>("runtime.useGLCompatProfile",true)){
159 - glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_COMPAT_PROFILE);
160 - }else{
161 - glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
162 - }
163 -
164 158 //Store the main window ID for future reference
165 159 // Open an OpenGL window; resolution is specified in the external XML files
166 160 bool window = par.getParam<bool>("runtime.windowedMode",true);
  @@ -178,6 +172,12 @@
178 172 glfwMakeContextCurrent(Divide::GL::_mainWindow);
179 173 //Init glew for main context
180 174 Divide::GL::initGlew();
175 +
176 + //Geometry shaders became core in version 3.3
177 + if(!GLEW_VERSION_3_3){
178 + ERROR_FN(Locale::get("ERROR_GFX_DEVICE"),"The OpenGL version supported by the current GPU is too old!");
179 + return GLEW_OLD_HARDWARE;
180 + }
181 181
182 182 glfwWindowHint(GLFW_VISIBLE, GL_FALSE);
183 183 Divide::GL::_loaderWindow = glfwCreateWindow(1,1,"divide-res-loader",NULL, Divide::GL::_mainWindow);
  @@ -186,18 +186,6 @@
186 186 return( GLFW_WINDOW_INIT_ERROR );
187 187 }
188 188
189 - bool coreGeomShaders = (glewIsSupported("ARB_geometry_shader4") == GL_TRUE);
190 - bool arbGeomShaders = (glewIsSupported("GL_ARB_geometry_shader4") == GL_TRUE);
191 - bool extGeomShaders = (glewIsSupported("GL_EXT_geometry_shader4") == GL_TRUE);
192 -
193 - if(!coreGeomShaders && !extGeomShaders && !arbGeomShaders){
194 - ERROR_FN(Locale::get("ERROR_GFX_DEVICE"),"geometry shaders are not supported by the current GPU!");
195 - return GLEW_OLD_HARDWARE;
196 - }
197 - GL_API::_coreGeomShadersAvailable = coreGeomShaders;
198 - GL_API::_arbGeomShadersAvailable = arbGeomShaders;
199 - GL_API::_extGeomShadersAvailable = extGeomShaders;
200 -
201 189 #ifdef _DEBUG
202 190 if(GLEW_ARB_debug_output) {
203 191 glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB);
  @@ -229,10 +217,10 @@
229 217 I32 width = return_struct->width;
230 218 glfwSetWindowPos(Divide::GL::_mainWindow, (width - resolution.width)*0.5f,(height - resolution.height)*0.5f);
231 219
232 - #if defined( __WIN32__ ) || defined( _WIN32 )
220 + #if defined( OS_WINDOWS )
233 221 _hwnd = FindWindow(NULL,par.getParam<std::string>("appTitle").c_str());
234 222 _hdc = GetDC(_hwnd);
235 - #elif defined( __APPLE_CC__ ) // Apple OS X
223 + #elif defined( OS_APPLE ) // Apple OS X
236 224 ///??
237 225 #else //Linux
238 226 _dpy = XOpenDisplay();
  @@ -243,7 +231,7 @@
243 231 GLint max_frag_uniform = 0, max_varying_floats = 0;
244 232 GLint max_vertex_uniform = 0, max_vertex_attrib = 0,max_texture_units = 0;
245 233 GLint buffers = 0,samplesEffective = 0;
246 - GLint major = 0, minor = 0;
234 + GLint major = 0, minor = 0, maxMinor = 0;
247 235 GLint maxAnisotrophy = 0;
248 236 GLint maxUBOBindings = 0, maxUBOBlockSize = 0;
249 237
  @@ -273,6 +261,9 @@
273 261 gpuVendorByte = "Unknown GPU Vendor";
274 262 }
275 263
264 + //Shader selection based on detail
265 + par.setParam("shaderDetailToken",par.getParam<GLubyte>("rendering.detailLevel"));
266 + par.setParam("GFX_DEVICE.maxTextureCombinedUnits",max_texture_units);
276 267 //Cap max aniso to what the hardware supports
277 268 if(maxAnisotrophy < par.getParam<U8>("rendering.anisotropicFilteringLevel",1)){
278 269 par.setParam("rendering.anisotropicFilteringLevel",maxAnisotrophy);
  @@ -284,31 +275,31 @@
284 275 PRINT_FN(Locale::get("WARN_SWITCH_D3D"));
285 276 PRINT_FN(Locale::get("WARN_APPLICATION_CLOSE"));
286 277 return GLEW_OLD_HARDWARE;
287 - }else {
288 - //OpenGL 3.1 introduced v140 for GLSL, so use a maximum of "medium" shaders
289 - //some "high" shaders need v150
290 - if(minor < 2){
291 - //If settings are set to low, use "low" shaders else cap at "medium" level
292 - par.setParam("shaderDetailToken",(par.getParam<GLubyte>("rendering.detailLevel") == DETAIL_LOW) ? DETAIL_LOW : DETAIL_MEDIUM);
293 - }else{ //If we got here, full OpenGL 3.2/3.3 is supported so use full shaders
294 - //Same shader selection based on detail level as above
295 - par.setParam("shaderDetailToken",par.getParam<GLubyte>("rendering.detailLevel"));
296 - }
297 - major == 3 ? setVersionId(OpenGL3x) : setVersionId(OpenGL4x);
278 + }else if(major == 3){
279 + setVersionId(OpenGL3x);
280 + maxMinor = 3;
281 + }else{
282 + setVersionId(OpenGL4x);
283 + /* if(GLEW_VERSION_4_4) maxMinor = 4;
284 + else*/if(GLEW_VERSION_4_3) maxMinor = 3;
285 + else if(GLEW_VERSION_4_2) maxMinor = 2;
286 + else if(GLEW_VERSION_4_1) maxMinor = 1;
298 287 }
299 - par.setParam("GFX_DEVICE.maxTextureCombinedUnits",max_texture_units);
288 +
300 289 //Print all of the OpenGL functionality info to the console
301 290 PRINT_FN(Locale::get("GL_MAX_UNIFORM"),max_frag_uniform);
302 291 PRINT_FN(Locale::get("GL_MAX_FRAG_VARYING"),max_varying_floats);
303 292 PRINT_FN(Locale::get("GL_MAX_VERT_UNIFORM"),max_vertex_uniform);
304 293 PRINT_FN(Locale::get("GL_MAX_VERT_ATTRIB"),max_vertex_attrib);
305 294 PRINT_FN(Locale::get("GL_MAX_TEX_UNITS"),max_texture_units);
306 - PRINT_FN(Locale::get("GL_MAX_VERSION"),major,minor);
295 + PRINT_FN(Locale::get("GL_MAX_VERSION"),major,maxMinor);
307 296 PRINT_FN(Locale::get("GL_GLSL_SUPPORT"),glslVersionSupported);
308 297 PRINT_FN(Locale::get("GL_VENDOR_STRING"),gpuVendorByte.c_str(), glGetString(GL_RENDERER));
309 298 PRINT_FN(Locale::get("GL_MULTI_SAMPLE_INFO"),samplesEffective,buffers);
310 299 PRINT_FN(Locale::get("GL_UBO_INFO"),maxUBOBindings, maxUBOBlockSize);
300 +
311 301 GL_ENUM_TABLE::fill();
302 +
312 303 //Set the clear color to a nice blue
313 304 GL_API::clearColor(DIVIDE_BLUE());
314 305 if(glewIsSupported("GL_ARB_seamless_cube_map")){
  @@ -348,13 +339,17 @@
348 339 _uniformBufferObjects[Matrices_UBO]->ReserveBuffer(2 * 16, sizeof(F32)); //View and Projection 2 x 16 float values
349 340 _uniformBufferObjects[Lights_UBO] = New glUniformBufferObject();
350 341 _uniformBufferObjects[Lights_UBO]->Create(Lights_UBO,true,false);
351 - _uniformBufferObjects[Lights_UBO]->ReserveBuffer(MAX_LIGHTS_PER_SCENE_NODE, sizeof(LightProperties)); //Usually less or equal to 4
342 + _uniformBufferObjects[Lights_UBO]->ReserveBuffer(Config::MAX_LIGHTS_PER_SCENE_NODE, sizeof(LightProperties)); //Usually less or equal to 4
352 343
353 344 /*
354 345 _uniformBufferObjects[Material_UBO] = New glUniformBufferObject();
355 346 _uniformBufferObjects[Material_UBO]->Create(Material_UBO,true,false);
356 347 _uniformBufferObjects[Material_UBO]->ReserveBuffer(num materials here, sizeof(_mat->getShaderData()) );
357 348 */
349 + for(GLuint index = 0; index < Config::MAX_CLIP_PLANES; ){
350 + _activeClipPlanes[index++] = false;
351 + }
352 +
358 353 //That's it. Everything should be ready for draw calls
359 354 PRINT_FN(Locale::get("START_OGL_API_OK"));
360 355
  @@ -389,16 +384,20 @@
389 384 }
390 385
391 386 void GL_API::exitRenderLoop(const bool killCommand) {
392 - Divide::GL::_applicationClosing = killCommand;
387 + Divide::GL::_applicationClosing = true;
393 388 glfwSetWindowShouldClose(Divide::GL::_mainWindow,true);
394 389 glfwSetWindowShouldClose(Divide::GL::_loaderWindow,true);
395 - _closeLoadingThread = true;
396 - _loaderThread->join();
397 390 }
398 391
399 392 ///clear up stuff ...
400 393 void GL_API::closeRenderingApi(){
401 - SAFE_DELETE(_loaderThread);
394 + _closeLoadingThread = true;
395 + _loaderThread->join();
396 +
397 + Divide::GL::_applicationClosing = true;
398 + glfwSetWindowShouldClose(Divide::GL::_mainWindow,true);
399 + glfwSetWindowShouldClose(Divide::GL::_loaderWindow,true);
400 +
402 401 try{
403 402 CEGUI::System::destroy();
404 403 }
  @@ -420,6 +419,12 @@
420 419 _glimInterfaces.clear(); //<Should call all destructors
421 420 SAFE_DELETE(_prevPointString);
422 421 SAFE_DELETE(_state2DRendering);
422 +
423 + glfwDestroyWindow(Divide::GL::_mainWindow);
424 + glfwDestroyWindow(Divide::GL::_loaderWindow);
425 + glfwTerminate();
426 +
427 + SAFE_DELETE(_loaderThread);
423 428 }
424 429
425 430 void GL_API::initDevice(GLuint targetFrameRate){
  @@ -427,46 +432,40 @@
427 432 while(!glfwWindowShouldClose(Divide::GL::_mainWindow)) {
428 433 Kernel::MainLoopStatic();
429 434 }
430 -
431 - glfwDestroyWindow(Divide::GL::_mainWindow);
432 - glfwDestroyWindow(Divide::GL::_loaderWindow);
433 - glfwTerminate();
434 435 }
435 436
436 437 bool GL_API::initShaders(){
437 438 //Init glsw library
438 439 I32 glswState = glswInit();
439 440 glswAddDirectiveToken("","#version 130\n/*“Copyright 2009-2013 DIVIDE-Studio”*/");
440 - glswAddDirectiveToken("","#extension GL_EXT_texture_array : require");
441 + glswAddDirectiveToken("Fragment","#extension GL_EXT_texture_array : require");
441 442 glswAddDirectiveToken("","#extension GL_ARB_uniform_buffer_object : require");
442 - if(!GL_API::_coreGeomShadersAvailable){
443 - if(GL_API::_arbGeomShadersAvailable){
444 - glswAddDirectiveToken("","#extension GL_ARB_geometry_shader4 : enable");
445 - }else{
446 - glswAddDirectiveToken("","#extension GL_EXT_geometry_shader4 : enable");
447 - }
443 + glswAddDirectiveToken("","#extension GL_EXT_gpu_shader4 : enable");
444 +
445 + if(getGPUVendor() == GPU_VENDOR_NVIDIA){ //nVidia specific
446 + glswAddDirectiveToken("","#pragma optionNV(fastmath on)");
447 + glswAddDirectiveToken("","#pragma optionNV(fastprecision on)");
448 + glswAddDirectiveToken("","#pragma optionNV(inline all)");
449 + glswAddDirectiveToken("","#pragma optionNV(strict on)");
450 + glswAddDirectiveToken("","#pragma optionNV(unroll all)");
448 451 }
449 -
450 - if(GFX_DEVICE.getGPUVendor() == GPU_VENDOR_NVIDIA || GFX_DEVICE.getGPUVendor() == GPU_VENDOR_AMD){
451 - glswAddDirectiveToken("","#extension GL_EXT_gpu_shader4 : enable");
452 452
453 - if(GFX_DEVICE.getGPUVendor() == GPU_VENDOR_NVIDIA){ //nVidia specific
454 - glswAddDirectiveToken("","#pragma optionNV(fastmath on)");
455 - glswAddDirectiveToken("","#pragma optionNV(fastprecision on)");
456 - glswAddDirectiveToken("","#pragma optionNV(inline all)");
457 - glswAddDirectiveToken("","#pragma optionNV(strict on)");
458 - glswAddDirectiveToken("","#pragma optionNV(unroll all)");
459 - }else{//AMD specific
460 - }
453 + if(getGPUVendor() == GPU_VENDOR_AMD){
454 + glswAddDirectiveToken("","#define SKIP_HARDWARE_CLIPPING");
461 455 }
462 -
463 - std::string lightCount("#define MAX_LIGHT_COUNT " + Util::toString(MAX_LIGHTS_PER_SCENE_NODE));
464 - std::string shadowCount("#define MAX_SHADOW_CASTING_LIGHTS " + Util::toString(MAX_SHADOW_CASTING_LIGHTS_PER_NODE));
456 +
457 + std::string clipPlanes("#define MAX_CLIP_PLANES " + Util::toString(Config::MAX_CLIP_PLANES));
458 + std::string lightCount("#define MAX_LIGHT_COUNT " + Util::toString(Config::MAX_LIGHTS_PER_SCENE_NODE));
459 + std::string shadowCount("#define MAX_SHADOW_CASTING_LIGHTS " + Util::toString(Config::MAX_SHADOW_CASTING_LIGHTS_PER_NODE));
460 + glswAddDirectiveToken("", clipPlanes.c_str());
465 461 glswAddDirectiveToken("", lightCount.c_str());
466 462 glswAddDirectiveToken("", shadowCount.c_str());
467 463 glswAddDirectiveToken("","#define Z_TEST_SIGMA 0.0001");
468 464 glswAddDirectiveToken("","#define ALPHA_DISCARD_THRESHOLD 0.2");
469 - glswAddDirectiveToken("","__CUSTOM_DEFINES__");
465 + glswAddDirectiveToken("","//__CUSTOM_DEFINES__");
466 + glswAddDirectiveToken("Fragment","//__CUSTOM_FRAGMENT_UNIFORMS__");
467 + glswAddDirectiveToken("Vertex","//__CUSTOM_VERTEX_UNIFORMS__");
468 +
470 469 GL_API::_GLSLOptContex = glslopt_initialize(GFX_DEVICE.getApi() == OpenGLES);
471 470 if(glswState == 1 && GL_API::_GLSLOptContex != NULL){
472 471 return true;
  @@ -493,7 +492,7 @@
493 492 Divide::GL::_matrixMode(PROJECTION_MATRIX);
494 493 Divide::GL::_loadIdentity();
495 494 // Set the viewport to be the entire window
496 - GL_API::setViewport(0,0,w,h,true);
495 + GL_API::setViewport(vec4<U32>(0,0,w,h),true);
497 496 // Set the clipping volume
498 497 Divide::GL::_perspective(fov,ratio,zNear,zFar);
499 498
  @@ -508,16 +507,21 @@
508 507 Kernel::updateResolutionCallback(w,h);
509 508 }
510 509
511 - void GL_API::setWindowPos(GLushort w, GLushort h){
510 + void GL_API::setWindowPos(GLushort w, GLushort h) const {
512 511 glfwSetWindowPos(Divide::GL::_mainWindow,w,h);
513 512 }
514 513
514 + void GL_API::setMousePosition(D32 x, D32 y) const {
515 + glfwSetCursorPos(Divide::GL::_mainWindow,x,y);
516 + }
517 +
515 518 void GL_API::idle(){
516 519 glfwPollEvents();
517 520 }
518 521
519 522 bool GL_API::loadInContext(const CurrentContext& context, boost::function0<GLvoid> callback) {
520 - if(callback.empty()) return false;
523 + if(callback.empty())
524 + return false;
521 525
522 526 if(context == GFX_LOADING_CONTEXT){
523 527 while(!_loadQueue.push(callback));
  @@ -534,7 +538,7 @@
534 538 #endif
535 539 while(!_closeLoadingThread){
536 540 if(_loadQueue.empty()){
537 - boost::this_thread::sleep(boost::posix_time::milliseconds(5));//<Avoid burning the CPU - Ionut
541 + boost::this_thread::sleep(boost::posix_time::milliseconds(10));//<Avoid burning the CPU - Ionut
538 542 continue;
539 543 }
540 544 boost::function0<GLvoid> callback;