nux-1.14.0
GpuDevice.cpp
00001 /*
00002  * Copyright 2010 Inalogic® Inc.
00003  *
00004  * This program is free software: you can redistribute it and/or modify it
00005  * under the terms of the GNU Lesser General Public License, as
00006  * published by the  Free Software Foundation; either version 2.1 or 3.0
00007  * of the License.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranties of
00011  * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
00012  * PURPOSE.  See the applicable version of the GNU Lesser General Public
00013  * License for more details.
00014  *
00015  * You should have received a copy of both the GNU Lesser General Public
00016  * License along with this program. If not, see <http://www.gnu.org/licenses/>
00017  *
00018  * Authored by: Jay Taoko <jaytaoko@inalogic.com>
00019  *
00020  */
00021 
00022 
00023 #include "GLResource.h"
00024 #include "NuxGraphics.h"
00025 #include "GpuDevice.h"
00026 #include "GLDeviceObjects.h"
00027 #include "GLResourceManager.h"
00028 
00029 #include "GLTextureResourceManager.h"
00030 #include "GLVertexResourceManager.h"
00031 #include "GLDeviceFrameBufferObject.h"
00032 #include "GLTemplatePrimitiveBuffer.h"
00033 #include "GraphicsEngine.h"
00034 
00035 namespace nux
00036 {
00037 #if (NUX_ENABLE_CG_SHADERS)
00038   extern void cgErrorCallback (void);
00039 #endif
00040 
00041   typedef struct
00042   {
00043     int major;
00044     int minor;
00045   } OpenGLVersion;
00046 
00047   static OpenGLVersion OpenGLVersionTable [] =
00048   {
00049     {1, 0},
00050     {1, 1},
00051     {1, 2},
00052     {1, 3},
00053     {1, 4},
00054     {2, 0},
00055     {2, 1},
00056     {3, 0},
00057     {3, 1},
00058     {3, 2},
00059     {3, 3},
00060     {4, 0},
00061     {4, 1},
00062     {0, 0}
00063   };
00064 
00065   // Pixel buffer object seems to corrupt textures. This can be seen when the window is moved
00066   // to the second screen in a multi-display configuration...
00067   // Happens on geforce 6600. Does not happens on geforce 6800.
00068 
00069   // ATI Radeon 4670 has problems loading textures from pixel buffer object. PBO should be deactivated if the
00070   // graphics card is made by AMD/ATI
00071   #define NUX_USE_PBO     1
00072 
00073   extern PixelFormatInfo GPixelFormats[];
00074 
00075   static void InitTextureFormats()
00076   {
00077 #ifndef NUX_OPENGLES_20
00078     GPixelFormats[ BITFMT_UNKNOWN               ].PlatformFormat        = GL_NONE;                                                      // Not supported for rendering.
00079 
00080     // Data in PC system memory: R(LSB) G B A(MSB) ---> GL Format:GL_RGBA - GL Type:GL_UNSIGNED_INT_8_8_8_8_REV
00081     GPixelFormats[ BITFMT_R8G8B8A8     ].PlatformFormat = GL_RGBA8;
00082     GPixelFormats[ BITFMT_R8G8B8A8     ].Format         = GL_RGBA;
00083     GPixelFormats[ BITFMT_R8G8B8A8     ].type               = GL_UNSIGNED_INT_8_8_8_8_REV;
00084 
00085     // Data in PC system memory: A(LSB) B G R(MSB) ---> GL Format:GL_RGBA - GL Type:GL_UNSIGNED_INT_8_8_8_8
00086     GPixelFormats[ BITFMT_A8B8G8R8     ].PlatformFormat = GL_RGBA8;
00087     GPixelFormats[ BITFMT_A8B8G8R8     ].Format         = GL_RGBA;
00088     GPixelFormats[ BITFMT_A8B8G8R8     ].type               = GL_UNSIGNED_INT_8_8_8_8;
00089 
00090     // Data in PC system memory: B(LSB) G R A(MSB) ---> GL Format:GL_BGRA - GL Type:GL_UNSIGNED_INT_8_8_8_8_REV
00091     GPixelFormats[ BITFMT_B8G8R8A8     ].PlatformFormat = GL_RGBA8;
00092     GPixelFormats[ BITFMT_B8G8R8A8     ].Format         = GL_BGRA;
00093     GPixelFormats[ BITFMT_B8G8R8A8     ].type               = GL_UNSIGNED_INT_8_8_8_8_REV;
00094 
00095     // Data in PC system memory: A(LSB) R G B(MSB) ---> GL Format:GL_BGRA - GL Type:GL_UNSIGNED_INT_8_8_8_8
00096     GPixelFormats[ BITFMT_A8R8G8B8     ].PlatformFormat = GL_RGBA8;
00097     GPixelFormats[ BITFMT_A8R8G8B8     ].Format         = GL_BGRA;
00098     GPixelFormats[ BITFMT_A8R8G8B8     ].type               = GL_UNSIGNED_INT_8_8_8_8;
00099 
00100     // Data in PC system memory: R(LSB) G B(MSB) ---> GL Format:GL_RGB - GL Type:GL_UNSIGNED
00101     GPixelFormats[ BITFMT_R8G8B8                ].PlatformFormat        = GL_RGB8;
00102     GPixelFormats[ BITFMT_R8G8B8                ].Format                = GL_RGB;
00103     GPixelFormats[ BITFMT_R8G8B8                ].type              = GL_UNSIGNED_BYTE;
00104 
00105     GPixelFormats[ BITFMT_B8G8R8                ].PlatformFormat        = GL_RGB8;
00106     GPixelFormats[ BITFMT_B8G8R8                ].Format                = GL_BGR;
00107     GPixelFormats[ BITFMT_B8G8R8                ].type              = GL_UNSIGNED_BYTE;
00108 
00109     GPixelFormats[ BITFMT_R5G6B5       ].PlatformFormat = GL_RGB5;
00110     GPixelFormats[ BITFMT_R5G6B5       ].Format         = GL_RGB;
00111     GPixelFormats[ BITFMT_R5G6B5       ].type               = GL_UNSIGNED_SHORT_5_6_5;
00112 
00113     GPixelFormats[ BITFMT_RGBA16F               ].PlatformFormat        = GL_RGBA16F_ARB;
00114     GPixelFormats[ BITFMT_RGBA16F               ].Format                = GL_RGBA;
00115     GPixelFormats[ BITFMT_RGBA16F               ].type              = GL_HALF_FLOAT_ARB;
00116 
00117     GPixelFormats[ BITFMT_RGB32F                ].PlatformFormat        = GL_RGB;
00118     GPixelFormats[ BITFMT_RGB32F                ].Format                = GL_RGB;
00119     GPixelFormats[ BITFMT_RGB32F                ].type              = GL_FLOAT;
00120 
00121     GPixelFormats[ BITFMT_RGBA32F               ].PlatformFormat        = GL_RGBA32F_ARB;
00122     GPixelFormats[ BITFMT_RGBA32F               ].Format                = GL_RGBA;
00123     GPixelFormats[ BITFMT_RGBA32F               ].type              = GL_FLOAT;
00124 
00125     // Note: Using GL_DEPTH_COMPONENT24 or GL_DEPTH_COMPONENT for PlatformFormat generate error GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT.
00126     GPixelFormats[ BITFMT_D24S8         ].PlatformFormat        = GL_DEPTH24_STENCIL8_EXT;
00127     GPixelFormats[ BITFMT_D24S8         ].Format                = GL_DEPTH_STENCIL_EXT;     // or GL_DEPTH_STENCIL_NV;
00128     GPixelFormats[ BITFMT_D24S8         ].type              = GL_UNSIGNED_INT_24_8_EXT; // or GL_UNSIGNED_INT_24_8_NV;
00129 
00130     GPixelFormats[ BITFMT_DXT1                  ].PlatformFormat        = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
00131     GPixelFormats[ BITFMT_DXT2                  ].PlatformFormat        = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
00132     GPixelFormats[ BITFMT_DXT3                  ].PlatformFormat        = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
00133     GPixelFormats[ BITFMT_DXT4                  ].PlatformFormat        = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
00134     GPixelFormats[ BITFMT_DXT5                  ].PlatformFormat        = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
00135 
00136     GPixelFormats[ BITFMT_R10G10B10A2  ].PlatformFormat = GL_RGB10_A2;
00137     GPixelFormats[ BITFMT_R10G10B10A2  ].Format         = GL_RGBA;
00138     GPixelFormats[ BITFMT_R10G10B10A2  ].type               = GL_UNSIGNED_INT_10_10_10_2;
00139 
00140     GPixelFormats[ BITFMT_A2B10G10R10  ].PlatformFormat = GL_RGB10_A2;
00141     GPixelFormats[ BITFMT_A2B10G10R10  ].Format         = GL_RGBA;
00142     GPixelFormats[ BITFMT_A2B10G10R10  ].type               = GL_UNSIGNED_INT_2_10_10_10_REV;
00143 
00144     GPixelFormats[ BITFMT_B10G10R10A2  ].PlatformFormat = GL_RGB10_A2;
00145     GPixelFormats[ BITFMT_B10G10R10A2  ].Format         = GL_BGRA;
00146     GPixelFormats[ BITFMT_B10G10R10A2  ].type               = GL_UNSIGNED_INT_10_10_10_2;
00147 
00148     GPixelFormats[ BITFMT_A2R10G10B10  ].PlatformFormat = GL_RGB10_A2;
00149     GPixelFormats[ BITFMT_A2R10G10B10  ].Format         = GL_BGRA;
00150     GPixelFormats[ BITFMT_A2R10G10B10  ].type               = GL_UNSIGNED_INT_2_10_10_10_REV;
00151 
00152     GPixelFormats[ BITFMT_A8           ].PlatformFormat = GL_RGBA8;
00153     GPixelFormats[ BITFMT_A8           ].Format         = GL_LUMINANCE;
00154     GPixelFormats[ BITFMT_A8           ].type               = GL_UNSIGNED_BYTE;
00155 #else
00156     GPixelFormats[ BITFMT_UNKNOWN               ].PlatformFormat        = GL_NONE;                                                      // Not supported for rendering.
00157 
00158     // Data in PC system memory: R(LSB) G B A(MSB) ---> GL Format:GL_RGBA - GL Type:GL_UNSIGNED_INT_8_8_8_8_REV
00159     GPixelFormats[ BITFMT_R8G8B8A8     ].PlatformFormat = GL_RGBA;
00160     GPixelFormats[ BITFMT_R8G8B8A8     ].Format         = GL_RGBA;
00161     GPixelFormats[ BITFMT_R8G8B8A8     ].type             = GL_UNSIGNED_BYTE;
00162 
00163     // Data in PC system memory: A(LSB) B G R(MSB) ---> GL Format:GL_RGBA - GL Type:GL_UNSIGNED_INT_8_8_8_8
00164     GPixelFormats[ BITFMT_A8B8G8R8     ].PlatformFormat = GL_RGBA;
00165     GPixelFormats[ BITFMT_A8B8G8R8     ].Format         = GL_RGBA;
00166     GPixelFormats[ BITFMT_A8B8G8R8     ].type             = GL_UNSIGNED_BYTE;
00167 
00168     // Data in PC system memory: B(LSB) G R A(MSB) ---> GL Format:GL_BGRA - GL Type:GL_UNSIGNED_INT_8_8_8_8_REV
00169     GPixelFormats[ BITFMT_B8G8R8A8     ].PlatformFormat = GL_RGBA;
00170     GPixelFormats[ BITFMT_B8G8R8A8     ].Format         = GL_RGBA;
00171     GPixelFormats[ BITFMT_B8G8R8A8     ].type             = GL_UNSIGNED_BYTE;
00172 
00173     // Data in PC system memory: A(LSB) R G B(MSB) ---> GL Format:GL_BGRA - GL Type:GL_UNSIGNED_INT_8_8_8_8
00174     GPixelFormats[ BITFMT_A8R8G8B8     ].PlatformFormat = GL_RGBA;
00175     GPixelFormats[ BITFMT_A8R8G8B8     ].Format         = GL_RGBA;
00176     GPixelFormats[ BITFMT_A8R8G8B8     ].type             = GL_UNSIGNED_BYTE;
00177 
00178     // Data in PC system memory: R(LSB) G B(MSB) ---> GL Format:GL_RGB - GL Type:GL_UNSIGNED
00179     GPixelFormats[ BITFMT_R8G8B8                ].PlatformFormat        = GL_RGB;
00180     GPixelFormats[ BITFMT_R8G8B8                ].Format                = GL_RGB;
00181     GPixelFormats[ BITFMT_R8G8B8                ].type            = GL_UNSIGNED_BYTE;
00182 
00183     GPixelFormats[ BITFMT_B8G8R8                ].PlatformFormat        = GL_RGB;
00184     GPixelFormats[ BITFMT_B8G8R8                ].Format                = GL_RGB;
00185     GPixelFormats[ BITFMT_B8G8R8                ].type            = GL_UNSIGNED_BYTE;
00186 
00187     GPixelFormats[ BITFMT_R5G6B5       ].PlatformFormat = GL_RGB;
00188     GPixelFormats[ BITFMT_R5G6B5       ].Format         = GL_RGB;
00189     GPixelFormats[ BITFMT_R5G6B5       ].type             = GL_UNSIGNED_SHORT_5_6_5;
00190 
00191     GPixelFormats[ BITFMT_RGBA16F               ].PlatformFormat        = GL_NONE;
00192     GPixelFormats[ BITFMT_RGBA16F               ].Format                = GL_NONE;
00193     GPixelFormats[ BITFMT_RGBA16F               ].type            = GL_NONE;
00194 
00195     GPixelFormats[ BITFMT_RGB32F                ].PlatformFormat        = GL_NONE;
00196     GPixelFormats[ BITFMT_RGB32F                ].Format                = GL_NONE;
00197     GPixelFormats[ BITFMT_RGB32F                ].type            = GL_NONE;
00198 
00199     GPixelFormats[ BITFMT_RGBA32F               ].PlatformFormat        = GL_NONE;
00200     GPixelFormats[ BITFMT_RGBA32F               ].Format                = GL_NONE;
00201     GPixelFormats[ BITFMT_RGBA32F               ].type            = GL_NONE;
00202 
00203     // Note: Using GL_DEPTH_COMPONENT24 or GL_DEPTH_COMPONENT for PlatformFormat generate error GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT.
00204     GPixelFormats[ BITFMT_D24S8         ].PlatformFormat          = GL_NONE;
00205     GPixelFormats[ BITFMT_D24S8         ].Format                  = GL_NONE;     // or GL_DEPTH_STENCIL_NV;
00206     GPixelFormats[ BITFMT_D24S8         ].type              = GL_NONE; // or GL_UNSIGNED_INT_24_8_NV;
00207 
00208     GPixelFormats[ BITFMT_DXT1                  ].PlatformFormat        = GL_NONE;
00209     GPixelFormats[ BITFMT_DXT2                  ].PlatformFormat        = GL_NONE;
00210     GPixelFormats[ BITFMT_DXT3                  ].PlatformFormat        = GL_NONE;
00211     GPixelFormats[ BITFMT_DXT4                  ].PlatformFormat        = GL_NONE;
00212     GPixelFormats[ BITFMT_DXT5                  ].PlatformFormat        = GL_NONE;
00213 
00214     GPixelFormats[ BITFMT_R10G10B10A2  ].PlatformFormat = GL_NONE;
00215     GPixelFormats[ BITFMT_R10G10B10A2  ].Format         = GL_NONE;
00216     GPixelFormats[ BITFMT_R10G10B10A2  ].type             = GL_NONE;
00217 
00218     GPixelFormats[ BITFMT_A2B10G10R10  ].PlatformFormat = GL_NONE;
00219     GPixelFormats[ BITFMT_A2B10G10R10  ].Format         = GL_NONE;
00220     GPixelFormats[ BITFMT_A2B10G10R10  ].type             = GL_NONE;
00221 
00222     GPixelFormats[ BITFMT_B10G10R10A2  ].PlatformFormat = GL_NONE;
00223     GPixelFormats[ BITFMT_B10G10R10A2  ].Format         = GL_NONE;
00224     GPixelFormats[ BITFMT_B10G10R10A2  ].type             = GL_NONE;
00225 
00226     GPixelFormats[ BITFMT_A2R10G10B10  ].PlatformFormat = GL_NONE;
00227     GPixelFormats[ BITFMT_A2R10G10B10  ].Format         = GL_NONE;
00228     GPixelFormats[ BITFMT_A2R10G10B10  ].type             = GL_NONE;
00229 
00230     GPixelFormats[ BITFMT_A8           ].PlatformFormat = GL_ALPHA;
00231     GPixelFormats[ BITFMT_A8           ].Format         = GL_ALPHA;
00232     GPixelFormats[ BITFMT_A8           ].type             = GL_UNSIGNED_BYTE;
00233 #endif
00234   }
00235 
00236   STREAMSOURCE GpuDevice::_StreamSource[MAX_NUM_STREAM];
00237 
00238   GpuInfo::GpuInfo ()
00239   {
00240     _support_opengl_version_11 = false;
00241     _support_opengl_version_12 = false;
00242     _support_opengl_version_13 = false;
00243     _support_opengl_version_14 = false;
00244     _support_opengl_version_15 = false;
00245     _support_opengl_version_20 = false;
00246     _support_opengl_version_21 = false;
00247     _support_opengl_version_30 = false;
00248     _support_opengl_version_31 = false;
00249     _support_opengl_version_32 = false;
00250     _support_opengl_version_33 = false;
00251     _support_opengl_version_40 = false;
00252     _support_opengl_version_41 = false;
00253   }
00254 
00255   void GpuInfo::Setup()
00256   {
00257     _support_opengl_version_11 = GLEW_VERSION_1_1 ? true : false;
00258     _support_opengl_version_12 = GLEW_VERSION_1_2 ? true : false;
00259     _support_opengl_version_13 = GLEW_VERSION_1_3 ? true : false;
00260     _support_opengl_version_14 = GLEW_VERSION_1_4 ? true : false;
00261     _support_opengl_version_15 = GLEW_VERSION_1_5 ? true : false;
00262     _support_opengl_version_20 = GLEW_VERSION_2_0 ? true : false;
00263     _support_opengl_version_21 = GLEW_VERSION_2_1 ? true : false;
00264     _support_opengl_version_30 = GLEW_VERSION_3_0 ? true : false;
00265     _support_opengl_version_31 = GLEW_VERSION_3_1 ? true : false;
00266     _support_opengl_version_32 = GLEW_VERSION_3_2 ? true : false;
00267 //     _support_opengl_version_33 = GLEW_VERSION_3_3 ? true : false;
00268 //     _support_opengl_version_40 = GLEW_VERSION_4_0 ? true : false;
00269 //     _support_opengl_version_41 = GLEW_VERSION_4_1 ? true : false;
00270 
00271 #ifndef NUX_OPENGLES_20
00272     // See: http://developer.nvidia.com/object/General_FAQ.html
00273     // The value of GL_MAX_TEXTURE_UNITS is 4 for GeForce FX and GeForce 6 Series GPUs. Why is that, since those GPUs have 16 texture units?
00274     CHECKGL (glGetIntegerv (GL_MAX_TEXTURE_UNITS, &_opengl_max_texture_units));
00275     CHECKGL (glGetIntegerv (GL_MAX_TEXTURE_COORDS, &_opengl_max_texture_coords));
00276     CHECKGL (glGetIntegerv (GL_MAX_TEXTURE_IMAGE_UNITS_ARB, &_opengl_max_texture_image_units));
00277     CHECKGL (glGetIntegerv (GL_MAX_VERTEX_ATTRIBS, &_opengl_max_vertex_attributes));
00278     CHECKGL (glGetIntegerv (GL_MAX_COLOR_ATTACHMENTS_EXT, &_opengl_max_fb_attachment));
00279 #else
00280     _opengl_max_fb_attachment = 1;
00281 #endif
00282 
00283 #if defined(NUX_OS_WINDOWS)
00284     _support_ext_swap_control                 = WGLEW_EXT_swap_control ? true : false;
00285 #elif defined(NUX_OS_LINUX)
00286     _support_ext_swap_control                 = GLXEW_SGI_swap_control ? true : false;
00287 #elif defined(NUX_OS_LINUX)
00288     _support_ext_swap_control                 = GLXEW_SGI_swap_control ? true : false;
00289 #endif
00290     _support_arb_vertex_program               = GLEW_ARB_vertex_program ? true : false;
00291     _support_arb_fragment_program             = GLEW_ARB_fragment_program ? true : false;
00292     _support_ext_framebuffer_object           = GLEW_EXT_framebuffer_object ? true : false;
00293     _support_arb_shader_objects               = GLEW_ARB_shader_objects ? true : false;
00294     _support_arb_vertex_shader                = GLEW_ARB_vertex_shader ? true : false;
00295     _support_arb_fragment_shader              = GLEW_ARB_fragment_shader ? true : false;
00296     _support_arb_vertex_buffer_object         = GLEW_ARB_vertex_buffer_object ? true : false;
00297     _support_arb_texture_non_power_of_two     = GLEW_ARB_texture_non_power_of_two ? true : false;
00298     _support_ext_draw_range_elements          = GLEW_EXT_draw_range_elements ? true : false;
00299     _support_ext_stencil_two_side             = GLEW_EXT_stencil_two_side ? true : false;
00300     _support_ext_texture_rectangle            = GLEW_EXT_texture_rectangle ? true : false;
00301     _support_arb_texture_rectangle            = GLEW_ARB_texture_rectangle ? true : false;
00302     _support_nv_texture_rectangle             = GLEW_NV_texture_rectangle ? true : false;
00303     _support_arb_pixel_buffer_object          = GLEW_ARB_pixel_buffer_object ? true : false;
00304     _support_ext_blend_equation_separate      = GLEW_EXT_blend_equation_separate ? true : false;
00305 #ifndef NUX_OPENGLES_20
00306     _support_ext_texture_srgb                 = GLEW_EXT_texture_sRGB ? true : false;
00307     _support_ext_texture_srgb_decode          = false; //GLEW_EXT_texture_sRGB_decode ? true : false;
00308     _support_ext_framebuffer_srgb             = GLEW_EXT_framebuffer_sRGB ? true : false;
00309     _support_arb_framebuffer_srgb             = GLEW_ARB_framebuffer_sRGB ? true : false;
00310 #endif
00311   }
00312 
00313 #if defined (NUX_OS_WINDOWS)
00314   GpuDevice::GpuDevice (t_u32 DeviceWidth, t_u32 DeviceHeight, BitmapFormat DeviceFormat,
00315     HDC device_context,
00316     HGLRC &opengl_rendering_context,
00317     int req_opengl_major,
00318     int req_opengl_minor,
00319     bool opengl_es_20)
00320 #elif defined (NUX_OS_LINUX)
00321   GpuDevice::GpuDevice (t_u32 DeviceWidth, t_u32 DeviceHeight, BitmapFormat DeviceFormat,
00322     Display *display,
00323     Window window,
00324     bool has_glx_13_support,
00325     GLXFBConfig fb_config,
00326     GLXContext &opengl_rendering_context,
00327     int req_opengl_major,
00328     int req_opengl_minor,
00329     bool opengl_es_20)
00330 #endif
00331   {
00332     _PixelStoreAlignment  = 4;
00333     _UsePixelBufferObject = false;
00334     _gpu_info             = NULL;
00335     _gpu_brand            = GPU_VENDOR_UNKNOWN;
00336     
00337     // OpenGL extension initialization
00338     GLenum Glew_Ok = 0;
00339 #ifdef GLEW_MX
00340     Glew_Ok = glewContextInit (glewGetContext() );
00341     nuxAssertMsg (Glew_Ok == GLEW_OK, TEXT ("[GpuDevice::GpuDevice] GL Extensions failed to initialize."));
00342 
00343 #if defined(NUX_OS_WINDOWS)
00344     Glew_Ok = wglewContextInit (wglewGetContext() );
00345 #elif defined(NUX_OS_LINUX)
00346     Glew_Ok = glxewContextInit (glxewGetContext() );
00347 #elif defined(NUX_OS_MACOSX)
00348     Glew_Ok = glxewContextInit (glxewGetContext() );
00349 #endif
00350     nuxAssertMsg (Glew_Ok == GLEW_OK, TEXT ("[GpuDevice::GpuDevice] OpenGL Extensions failed to initialize."));
00351 #else
00352     Glew_Ok = glewInit();
00353 #endif
00354 
00355 #ifndef NUX_OPENGLES_20
00356     CHECKGL (glGetIntegerv (GL_MAJOR_VERSION, &_opengl_major));
00357     CHECKGL (glGetIntegerv (GL_MINOR_VERSION, &_opengl_minor));
00358 #else
00359     _opengl_major = 2;
00360     _opengl_minor = 0;
00361 #endif
00362 
00363     bool opengl_es_context_created = false;
00364 
00365 #if defined (NUX_OS_WINDOWS)
00366     if (((_opengl_major >= 3) && (req_opengl_major >= 3)) || (_opengl_major >= 3) || opengl_es_20)
00367 #elif defined (NUX_OS_LINUX)
00368     if (has_glx_13_support && (((_opengl_major >= 3) && (req_opengl_major >= 3)) || ((_opengl_major >= 3) && opengl_es_20)))
00369 #endif
00370     {
00371       // Create a new Opengl Rendering Context
00372       bool requested_profile_is_supported = false;
00373       int index = 0;
00374       for (index = 0; OpenGLVersionTable [index].major != 0; index++)
00375       {
00376         if ((OpenGLVersionTable[index].major == req_opengl_major) &&
00377           (OpenGLVersionTable[index].minor == req_opengl_minor))
00378         {
00379           if (_opengl_major == 1)
00380           {
00381             if ((req_opengl_major == 1) && (req_opengl_minor >= 0) && (req_opengl_minor <= 5))
00382               requested_profile_is_supported = true;
00383           }
00384           else if (_opengl_major == 2)
00385           {
00386             if ((req_opengl_major == 2) && (req_opengl_minor >= 0) && (req_opengl_minor <= 1))
00387               requested_profile_is_supported = true;
00388           }
00389           else if (_opengl_major == 3)
00390           {
00391             if ((req_opengl_major == 3) && (req_opengl_minor >= 0) && (req_opengl_minor <= 3))
00392               requested_profile_is_supported = true;
00393           }
00394           else if (_opengl_major == 4)
00395           {
00396             if ((req_opengl_major == 4) && (req_opengl_minor >= 0) && (req_opengl_minor <= 1))
00397               requested_profile_is_supported = true;
00398           }
00399           break;
00400         }
00401       }
00402 
00403       if (opengl_es_20)
00404       {
00405 #if defined (NUX_OS_WINDOWS)
00406         int attribs[] =
00407         {
00408           WGL_CONTEXT_MAJOR_VERSION_ARB,  2,
00409           WGL_CONTEXT_MINOR_VERSION_ARB,  0,
00410           WGL_CONTEXT_PROFILE_MASK_ARB,   //WGL_CONTEXT_ES2_PROFILE_BIT_EXT,
00411           0
00412         };
00413 
00414         HGLRC new_opengl_rendering_context = wglCreateContextAttribsARB(device_context,0, attribs);
00415 
00416         if (new_opengl_rendering_context == 0)
00417         {
00418           nuxDebugMsg (TEXT("[GpuDevice::GpuDevice] OpenGL ES 2.0 context creation has failed."));
00419         }
00420         else
00421         {
00422           wglMakeCurrent (NULL, NULL);
00423           wglDeleteContext (opengl_rendering_context);
00424           opengl_rendering_context = new_opengl_rendering_context;
00425           wglMakeCurrent (device_context, opengl_rendering_context);
00426           opengl_es_context_created = true;
00427         }
00428 #elif defined (NUX_OS_LINUX)
00429         int attribs[] =
00430         {
00431           GLX_CONTEXT_MAJOR_VERSION_ARB,  2,
00432           GLX_CONTEXT_MINOR_VERSION_ARB,  0,
00433           //GLX_CONTEXT_PROFILE_MASK_ARB,   GLX_CONTEXT_ES2_PROFILE_BIT_EXT,
00434           0
00435         };
00436 
00437         GLXContext new_opengl_rendering_context = glXCreateContextAttribsARB(display, fb_config, 0, true, attribs);
00438 
00439         if (new_opengl_rendering_context == 0)
00440         {
00441           nuxDebugMsg (TEXT("[GpuDevice::GpuDevice] OpenGL ES 2.0 context creation has failed."));
00442         }
00443         else
00444         {
00445           opengl_rendering_context = new_opengl_rendering_context;
00446           glXMakeCurrent (display, window, opengl_rendering_context);
00447           opengl_es_context_created = true;
00448         }
00449 #endif
00450       }
00451       else if (requested_profile_is_supported)
00452       {
00453         int profile_mask = 0;
00454         int profile_value = 0;
00455         int flag_mask = 0;
00456         int flag_value = 0;
00457 
00458 #if defined (NUX_OS_WINDOWS)
00459         if (((req_opengl_major == 3) && (req_opengl_minor >= 3)) || (req_opengl_major >= 4))
00460         {
00461           profile_mask = WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB;
00462           profile_value = WGL_CONTEXT_PROFILE_MASK_ARB;
00463           flag_mask = WGL_CONTEXT_FLAGS_ARB;
00464           flag_value = 0;
00465         }
00466 
00467         int attribs[] =
00468         {
00469           WGL_CONTEXT_MAJOR_VERSION_ARB,  req_opengl_major,
00470           WGL_CONTEXT_MINOR_VERSION_ARB,  req_opengl_minor,
00471           profile_mask,                   profile_value,
00472           flag_mask,                      flag_value,
00473           0
00474         };
00475 
00476         HGLRC new_opengl_rendering_context = wglCreateContextAttribsARB(device_context,0, attribs);
00477 
00478         if (new_opengl_rendering_context == 0)
00479         {
00480           nuxDebugMsg (TEXT("[GpuDevice::GpuDevice] OpenGL version %d.%d context creation has failed."), req_opengl_major, req_opengl_minor);
00481         }
00482         else
00483         {
00484           wglMakeCurrent (NULL, NULL);
00485           wglDeleteContext (opengl_rendering_context);
00486           opengl_rendering_context = new_opengl_rendering_context;
00487           wglMakeCurrent (device_context, opengl_rendering_context);
00488         }
00489 #elif defined (NUX_OS_LINUX)
00490         if (((req_opengl_major == 3) && (req_opengl_minor >= 3)) || (req_opengl_major >= 4))
00491         {
00492           profile_mask  = GLX_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB;
00493           profile_value = GLX_CONTEXT_PROFILE_MASK_ARB;
00494           flag_mask     = GLX_CONTEXT_FLAGS_ARB;
00495           flag_value    = 0;
00496         }
00497 
00498         int attribs[] =
00499         {
00500           GLX_CONTEXT_MAJOR_VERSION_ARB,  req_opengl_major,
00501           GLX_CONTEXT_MINOR_VERSION_ARB,  req_opengl_minor,
00502           profile_mask,                   profile_value,
00503           flag_mask,                      flag_value,
00504           0
00505         };
00506 
00507         GLXContext new_opengl_rendering_context = glXCreateContextAttribsARB(display, fb_config, 0, true, attribs);
00508 
00509         if (new_opengl_rendering_context == 0)
00510         {
00511           nuxDebugMsg (TEXT("[GpuDevice::GpuDevice] OpenGL version %d.%d context creation has failed."), req_opengl_major, req_opengl_minor);
00512           attribs[0] = 1; // major version
00513           attribs[1] = 0; // minor version
00514           attribs[2] = 0;
00515           new_opengl_rendering_context = glXCreateContextAttribsARB(display, fb_config, 0, true, attribs);
00516 
00517           opengl_rendering_context = new_opengl_rendering_context;
00518           glXMakeCurrent (display, window, opengl_rendering_context);
00519         }
00520         else
00521         {
00522           opengl_rendering_context = new_opengl_rendering_context;
00523           glXMakeCurrent (display, window, opengl_rendering_context);
00524         }
00525 #endif
00526       }
00527       else
00528       {
00529         nuxDebugMsg (TEXT("[GpuDevice::GpuDevice] Using highest default OpenGL version."));
00530       }
00531     }
00532     else
00533     {
00534       opengl_rendering_context = 0;
00535     }
00536 
00537     _board_vendor_string = ANSI_TO_TCHAR (NUX_REINTERPRET_CAST (const char *, glGetString (GL_VENDOR) ) );
00538     CHECKGL_MSG (glGetString (GL_VENDOR) );
00539     _board_renderer_string = ANSI_TO_TCHAR (NUX_REINTERPRET_CAST (const char *, glGetString (GL_RENDERER) ) );
00540     CHECKGL_MSG (glGetString (GL_RENDERER) );
00541     _openGL_version_string = ANSI_TO_TCHAR (NUX_REINTERPRET_CAST (const char *, glGetString (GL_VERSION) ) );
00542     CHECKGL_MSG (glGetString (GL_VERSION) );
00543     if (GLEW_VERSION_2_0)
00544     {
00545       _glsl_version_string = ANSI_TO_TCHAR (NUX_REINTERPRET_CAST (const char *, glGetString (GL_SHADING_LANGUAGE_VERSION) ) );
00546       CHECKGL_MSG (glGetString (GL_SHADING_LANGUAGE_VERSION) );
00547 
00548       NString glsl_major;
00549       NString glsl_minor;
00550       TCHAR split = TEXT('.');
00551       _glsl_version_string.SplitAtFirstOccurenceOf(split, glsl_major, glsl_minor);
00552 
00553     }
00554 
00555     nuxDebugMsg (TEXT ("Gpu Vendor: %s"), _board_vendor_string.GetTCharPtr() );
00556     nuxDebugMsg (TEXT ("Gpu Renderer: %s"), _board_renderer_string.GetTCharPtr() );
00557     nuxDebugMsg (TEXT ("Gpu OpenGL Version: %s"), _openGL_version_string.GetTCharPtr() );
00558     nuxDebugMsg (TEXT ("Gpu GLSL Version: %s"), _glsl_version_string.GetTCharPtr() );
00559 
00560     // Get the version supported by the context that was set.
00561 
00562     if (opengl_es_20 && opengl_es_context_created)
00563     {
00564       
00565     }
00566     else
00567     {
00568       int new_opengl_major;
00569       int new_opengl_minor;
00570       CHECKGL (glGetIntegerv (GL_MAJOR_VERSION, &new_opengl_major));
00571       CHECKGL (glGetIntegerv (GL_MINOR_VERSION, &new_opengl_minor));
00572 
00573       if ((new_opengl_major != _opengl_major) || (new_opengl_minor != _opengl_minor))
00574       {
00575         nuxDebugMsg (TEXT ("The Gpu supports OpenGL %d.%d but version %d.%d has been requested."), _opengl_major, _opengl_minor, new_opengl_major, new_opengl_minor);
00576       }
00577     }
00578 
00579     NString TempStr = (const TCHAR *) TCharToUpperCase (_board_vendor_string.GetTCharPtr() );
00580 
00581     if (TempStr.FindFirstOccurence (TEXT ("NVIDIA") ) != tstring::npos)
00582     {
00583       _gpu_brand = GPU_BRAND_NVIDIA;
00584     }
00585     else if (TempStr.FindFirstOccurence (TEXT ("ATI") ) != tstring::npos)
00586     {
00587       _gpu_brand = GPU_BRAND_AMD;
00588     }
00589     else if (TempStr.FindFirstOccurence (TEXT ("TUNGSTEN") ) != tstring::npos)
00590     {
00591       _gpu_brand = GPU_BRAND_INTEL;
00592     }
00593 
00594     if (0)
00595     {
00596       if (GetGPUBrand() == GPU_BRAND_AMD)
00597         _UsePixelBufferObject = false;
00598       else
00599         _UsePixelBufferObject = true;
00600     }
00601     else
00602     {
00603       _UsePixelBufferObject = false;
00604     }
00605 
00606     _gpu_info = new GpuInfo ();
00607     _gpu_info->Setup ();
00608     _gpu_render_states = new GpuRenderStates (_gpu_brand, _gpu_info);
00609 
00610 #if defined(NUX_OS_WINDOWS)
00611     OGL_EXT_SWAP_CONTROL                = WGLEW_EXT_swap_control ? true : false;
00612 #elif defined(NUX_OS_LINUX)
00613     OGL_EXT_SWAP_CONTROL                = GLXEW_SGI_swap_control ? true : false;
00614 #elif defined(NUX_OS_LINUX)
00615     OGL_EXT_SWAP_CONTROL                = GLXEW_SGI_swap_control ? true : false;
00616 #endif
00617 
00618     InitTextureFormats();
00619 
00620     // See Avoiding 16 Common OpenGL Pitfalls
00621     // 7. Watch Your Pixel Store Alignment
00622     // http://www.opengl.org/resources/features/KilgardTechniques/oglpitfall/
00623     // We use a pack /unpack alignment to 1 so we don't have any padding at the end of row.
00624 
00625     CHECKGL (glPixelStorei (GL_UNPACK_ALIGNMENT, _PixelStoreAlignment));
00626     CHECKGL (glPixelStorei (GL_PACK_ALIGNMENT, _PixelStoreAlignment));
00627 
00628 //     _DeviceWidth = DeviceWidth;
00629 //     _DeviceHeight = DeviceHeight;
00630 // 
00631 //     _ViewportX = 0;
00632 //     _ViewportY = 0;
00633 //     _ViewportWidth = DeviceWidth;
00634 //     _ViewportHeight = DeviceHeight;
00635 
00636     for (int i = 0; i < MAX_NUM_STREAM; i++)
00637     {
00638       _StreamSource[i].ResetStreamSource();
00639     }
00640 
00641     // Configure NVidia CG
00642 #if (NUX_ENABLE_CG_SHADERS)
00643     {
00644       m_Cgcontext = 0;
00645 
00646       // Create Cg context and set profile.
00647       CHECKGL ( cgSetErrorCallback ( cgErrorCallback ) );
00648       m_Cgcontext = cgCreateContext();
00649       nuxAssert (m_Cgcontext);
00650       //CHECKGL( cgGLEnableProfile( CG_PROFILE_VP40 ) );
00651       //CHECKGL( cgGLEnableProfile( CG_PROFILE_FP40 ) );
00652       CHECKGL ( cgGLSetManageTextureParameters ( m_Cgcontext, CG_FALSE ) );
00653     }
00654 #endif
00655 
00656     if (GetGpuInfo ().Support_EXT_Framebuffer_Object ())
00657     {
00658       _FrameBufferObject = CreateFrameBufferObject ();
00659       _FrameBufferObject->Deactivate ();
00660     }
00661   }
00662 
00663   GpuDevice::~GpuDevice()
00664   {
00665     NUX_SAFE_DELETE (_gpu_info);
00666     NUX_SAFE_DELETE (_gpu_render_states);
00667 
00668     _FrameBufferObject.Release ();
00669     _CurrentFrameBufferObject.Release ();
00670 
00671     _PixelBufferArray.clear ();
00672 
00673     for (int i = 0; i < MAX_NUM_STREAM; i++)
00674     {
00675       _StreamSource[i].ResetStreamSource();
00676     }
00677     // NVidia CG
00678 #if (NUX_ENABLE_CG_SHADERS)
00679     cgDestroyContext (m_Cgcontext);
00680 #endif
00681 
00682   }
00683 
00684   ObjectPtr<IOpenGLFrameBufferObject> GpuDevice::CreateFrameBufferObject()
00685   {
00686     IOpenGLFrameBufferObject *ptr;
00687     CreateFrameBufferObject ( (IOpenGLFrameBufferObject **) &ptr);
00688     ObjectPtr<IOpenGLFrameBufferObject> h = ObjectPtr<IOpenGLFrameBufferObject> (ptr);
00689     ptr->UnReference ();
00690     return h;
00691   }
00692 
00693   int GpuDevice::CreateFrameBufferObject (IOpenGLFrameBufferObject **ppFrameBufferObject)
00694   {
00695     *ppFrameBufferObject = new IOpenGLFrameBufferObject(NUX_TRACKER_LOCATION);
00696 
00697     return OGL_OK;
00698   }
00699 
00700   int GpuDevice::GetOpenGLMajorVersion () const
00701   {
00702     return _opengl_major;
00703   }
00704 
00705   int GpuDevice::GetOpenGLMinorVersion () const
00706   {
00707     return _opengl_minor;
00708   }
00709 
00710   GpuBrand GpuDevice::GetGPUBrand() const
00711   {
00712     return _gpu_brand;
00713   }
00714 
00715   GpuRenderStates &GpuDevice::GetRenderStates()
00716   {
00717     return *_gpu_render_states;
00718   }
00719 
00720   GpuInfo &GpuDevice::GetGpuInfo()
00721   {
00722     return *_gpu_info;
00723   }
00724 
00725   void GpuDevice::ResetRenderStates()
00726   {
00727     _gpu_render_states->ResetStateChangeToDefault();
00728   }
00729 
00730   void GpuDevice::VerifyRenderStates()
00731   {
00732     _gpu_render_states->CheckStateChange();
00733   }
00734 
00735   void GpuDevice::InvalidateTextureUnit (int TextureUnitIndex)
00736   {
00737     CHECKGL (glActiveTextureARB (TextureUnitIndex) );
00738 
00739     CHECKGL (glBindTexture (GL_TEXTURE_1D, 0) );
00740     CHECKGL (glBindTexture (GL_TEXTURE_2D, 0) );
00741     CHECKGL (glBindTexture (GL_TEXTURE_CUBE_MAP, 0) );
00742     CHECKGL (glBindTexture (GL_TEXTURE_3D, 0) );
00743     CHECKGL (glBindTexture (GL_TEXTURE_RECTANGLE_ARB, 0) );
00744 
00745     // From lowest priority to highest priority:
00746     //      GL_TEXTURE_1D,
00747     //      GL_TEXTURE_2D,
00748     //      GL_TEXTURE_RECTANGLE_ARB,
00749     //      GL_TEXTURE_3D,
00750     //      GL_TEXTURE_CUBE_MAP.
00751 
00752     CHECKGL (glDisable (GL_TEXTURE_1D) );
00753     CHECKGL (glDisable (GL_TEXTURE_2D) );
00754     CHECKGL (glDisable (GL_TEXTURE_RECTANGLE_ARB) );
00755     CHECKGL (glDisable (GL_TEXTURE_3D) );
00756     CHECKGL (glDisable (GL_TEXTURE_CUBE_MAP) );
00757   }
00758 
00759   int GpuDevice::AllocateUnpackPixelBufferIndex (int *index)
00760   {
00761     t_u32 num = (t_u32) _PixelBufferArray.size();
00762 
00763     for (t_u32 i = 0; i < num; i++)
00764     {
00765       if (_PixelBufferArray[i].IsReserved == FALSE)
00766       {
00767         _PixelBufferArray[i].IsReserved = TRUE;
00768         *index = i;
00769         return OGL_OK;
00770       }
00771     }
00772 
00773     // Not enough free pbo
00774     PixelBufferObject pbo;
00775     pbo.PBO = CreatePixelBufferObject (4, (VBO_USAGE) GL_STATIC_DRAW);
00776     pbo.IsReserved = TRUE;
00777     _PixelBufferArray.push_back (pbo);
00778     *index = (int) _PixelBufferArray.size() - 1;
00779     return OGL_OK;
00780   }
00781 
00782   int GpuDevice::FreeUnpackPixelBufferIndex (const int index)
00783   {
00784     t_s32 num = (t_s32) _PixelBufferArray.size();
00785     nuxAssertMsg ( (index >= 0) && (index < num), TEXT ("[GpuDevice::FreeUnpackPixelBufferIndex] Trying to Free a pixel buffer index that does not exist.") );
00786 
00787     if ( (index < 0) || (index >= num) )
00788     {
00789       return OGL_ERROR;
00790     }
00791 
00792     _PixelBufferArray[index].IsReserved = false;
00793 
00794 // //     if(0)
00795 // //     {
00796 // //         // Can we realloc the memory used by the buffer with much less memory (4x4bytes)???
00797 // //         CHECKGL( glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, OpenGLID) );
00798 // //         CHECKGL( glBufferDataARB(GL_PIXEL_UNPACK_BUFFER_ARB, 4*4, NULL, GL_STREAM_DRAW_ARB) );
00799 // //     }
00800     return OGL_OK;
00801   }
00802 
00803   void *GpuDevice::LockUnpackPixelBufferIndex (const int index, int Size)
00804   {
00805     BindUnpackPixelBufferIndex (index);
00806     CHECKGL ( glBufferDataARB (GL_PIXEL_UNPACK_BUFFER_ARB, Size, NULL, GL_STREAM_DRAW) );
00807     void *pBits = glMapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB, GL_WRITE_ONLY_ARB);
00808     CHECKGL_MSG (glMapBufferARB );
00809     CHECKGL ( glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB, 0) );
00810     return pBits;
00811   }
00812 
00813   void* GpuDevice::LockPackPixelBufferIndex (const int index, int Size)
00814   {
00815     BindPackPixelBufferIndex (index);
00816     CHECKGL ( glBufferDataARB (GL_PIXEL_PACK_BUFFER_ARB, Size, NULL, GL_STREAM_DRAW) );
00817     void *pBits = glMapBufferARB (GL_PIXEL_PACK_BUFFER_ARB, GL_WRITE_ONLY_ARB);
00818     CHECKGL_MSG (glMapBufferARB );
00819     CHECKGL ( glBindBufferARB (GL_PIXEL_PACK_BUFFER_ARB, 0) );
00820     return pBits;
00821   }
00822 
00823   void GpuDevice::UnlockUnpackPixelBufferIndex (const int index)
00824   {
00825     BindUnpackPixelBufferIndex (index);
00826     CHECKGL ( glUnmapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB) );
00827     CHECKGL ( glBindBufferARB (GL_PIXEL_PACK_BUFFER_ARB, 0) );
00828   }
00829 
00830   void GpuDevice::UnlockPackPixelBufferIndex (const int index)
00831   {
00832     BindPackPixelBufferIndex (index);
00833     CHECKGL ( glUnmapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB) );
00834     CHECKGL ( glBindBufferARB (GL_PIXEL_PACK_BUFFER_ARB, 0) );
00835   }
00836 
00837   int GpuDevice::BindUnpackPixelBufferIndex (const int index)
00838   {
00839     t_s32 num = (t_s32) _PixelBufferArray.size();
00840     nuxAssertMsg ( (index >= 0) && (index < num), TEXT ("[GpuDevice::BindUnpackPixelBufferIndex] Trying to bind an invalid pixel buffer index.") );
00841 
00842     if ( (index < 0) || (index >= num) )
00843     {
00844       return OGL_ERROR;
00845     }
00846 
00847     nuxAssertMsg (_PixelBufferArray[index].IsReserved == true, TEXT ("[GpuDevice::BindUnpackPixelBufferIndex] Trying to reserved pixel buffer index.") );
00848 
00849     if (_PixelBufferArray[index].IsReserved == false)
00850     {
00851       return OGL_ERROR;
00852     }
00853 
00854     _PixelBufferArray[index].PBO->BindUnpackPixelBufferObject();
00855     return OGL_OK;
00856   }
00857 
00858   int GpuDevice::BindPackPixelBufferIndex (const int index)
00859   {
00860     t_s32 num = (t_s32) _PixelBufferArray.size();
00861     nuxAssertMsg ( (index >= 0) && (index < num), TEXT ("[GpuDevice::BindPackPixelBufferIndex] Trying to bind an invalid pixel buffer index.") );
00862 
00863     if ( (index < 0) || (index >= num) )
00864     {
00865       return OGL_ERROR;
00866     }
00867 
00868     nuxAssertMsg (_PixelBufferArray[index].IsReserved == true, TEXT ("[GpuDevice::BindPackPixelBufferIndex] Trying to reserved pixel buffer index.") );
00869 
00870     if (_PixelBufferArray[index].IsReserved == false)
00871     {
00872       return OGL_ERROR;
00873     }
00874 
00875     _PixelBufferArray[index].PBO->BindPackPixelBufferObject();
00876     return OGL_OK;
00877   }
00878 
00879   int GpuDevice::FormatFrameBufferObject (t_u32 Width, t_u32 Height, BitmapFormat PixelFormat)
00880   {
00881     if (!GetGpuInfo ().Support_EXT_Framebuffer_Object ())
00882     {
00883       nuxDebugMsg (TEXT ("[GpuDevice::FormatFrameBufferObject] No support for OpenGL framebuffer extension.") );
00884       return 0;
00885     }
00886 
00887     return _FrameBufferObject->FormatFrameBufferObject (Width, Height, PixelFormat);
00888   }
00889 
00890   int GpuDevice::SetColorRenderTargetSurface (t_u32 ColorAttachmentIndex, ObjectPtr<IOpenGLSurface> pRenderTargetSurface)
00891   {
00892     if (!GetGpuInfo ().Support_EXT_Framebuffer_Object ())
00893     {
00894       nuxDebugMsg (TEXT ("[GpuDevice::SetColorRenderTargetSurface] No support for OpenGL framebuffer extension.") );
00895       return 0;
00896     }
00897 
00898     return _FrameBufferObject->SetRenderTarget (ColorAttachmentIndex, pRenderTargetSurface);
00899   }
00900 
00901   int GpuDevice::SetDepthRenderTargetSurface (ObjectPtr<IOpenGLSurface> pDepthSurface)
00902   {
00903     if (!GetGpuInfo ().Support_EXT_Framebuffer_Object ())
00904     {
00905       nuxDebugMsg (TEXT ("[GpuDevice::SetDepthRenderTargetSurface] No support for OpenGL framebuffer extension.") );
00906       return 0;
00907     }
00908 
00909     return _FrameBufferObject->SetDepthSurface (pDepthSurface);
00910   }
00911 
00912   ObjectPtr<IOpenGLSurface> GpuDevice::GetColorRenderTargetSurface (t_u32 ColorAttachmentIndex)
00913   {
00914     if (!GetGpuInfo ().Support_EXT_Framebuffer_Object ())
00915     {
00916       nuxDebugMsg (TEXT ("[GpuDevice::GetColorRenderTargetSurface] No support for OpenGL framebuffer extension.") );
00917       return ObjectPtr<IOpenGLSurface> (0);
00918     }
00919 
00920     return _FrameBufferObject->GetRenderTarget (ColorAttachmentIndex);
00921   }
00922 
00923   ObjectPtr<IOpenGLSurface> GpuDevice::GetDepthRenderTargetSurface()
00924   {
00925     if (!GetGpuInfo ().Support_EXT_Framebuffer_Object ())
00926     {
00927       nuxDebugMsg (TEXT ("[GpuDevice::GetDepthRenderTargetSurface] No support for OpenGL framebuffer extension.") );
00928       return ObjectPtr<IOpenGLSurface> (0);
00929     }
00930 
00931     return _FrameBufferObject->GetDepthRenderTarget();
00932   }
00933 
00934   void GpuDevice::ActivateFrameBuffer()
00935   {
00936     if (!GetGpuInfo ().Support_EXT_Framebuffer_Object ())
00937     {
00938       nuxDebugMsg (TEXT ("[GpuDevice::ActivateFrameBuffer] No support for OpenGL framebuffer extension.") );
00939       return;
00940     }
00941 
00942     _FrameBufferObject->Activate();
00943   }
00944 
00945   void GpuDevice::SetCurrentFrameBufferObject (ObjectPtr<IOpenGLFrameBufferObject> fbo)
00946   {
00947     _CurrentFrameBufferObject = fbo;
00948   }
00949 
00950   ObjectPtr<IOpenGLFrameBufferObject> GpuDevice::GetCurrentFrameBufferObject()
00951   {
00952     return _CurrentFrameBufferObject;
00953   }
00954 
00955   void GpuDevice::DeactivateFrameBuffer ()
00956   {
00957     if (!GetGpuInfo ().Support_EXT_Framebuffer_Object () )
00958     {
00959       nuxDebugMsg (TEXT ("[GpuDevice::DeactivateFrameBuffer] No support for OpenGL framebuffer extension.") );
00960       return;
00961     }
00962 
00963     _CurrentFrameBufferObject.Release ();
00964     CHECKGL ( glBindFramebufferEXT (GL_FRAMEBUFFER_EXT, 0));
00965     CHECKGL ( glBindRenderbufferEXT (GL_RENDERBUFFER_EXT, 0));
00966   }
00967 
00968   ObjectPtr<IOpenGLBaseTexture> GpuDevice::CreateSystemCapableDeviceTexture (
00969     int Width
00970     , int Height
00971     , int Levels
00972     , BitmapFormat PixelFormat)
00973   {
00974     if(GetGpuInfo().Support_ARB_Texture_Non_Power_Of_Two())
00975     {
00976       return CreateTexture (Width, Height, Levels, PixelFormat);
00977     }
00978 
00979     if(GetGpuInfo().Support_EXT_Texture_Rectangle () || GetGpuInfo().Support_ARB_Texture_Rectangle ())
00980     {
00981       return CreateRectangleTexture (Width, Height, Levels, PixelFormat);
00982     }
00983 
00984     nuxAssertMsg(0, TEXT("[NuxGraphicsResources::CreateSystemCapableDeviceTexture] No support for non power of two textures or rectangle textures"));
00985 
00986     return ObjectPtr<IOpenGLBaseTexture>();
00987   }
00988 
00989   BaseTexture* GpuDevice::CreateSystemCapableTexture ()
00990   {
00991     if(GetGpuInfo().Support_ARB_Texture_Non_Power_Of_Two())
00992     {
00993       return new Texture2D (NUX_TRACKER_LOCATION);
00994     }
00995 
00996     if(GetGpuInfo().Support_EXT_Texture_Rectangle () || GetGpuInfo().Support_ARB_Texture_Rectangle ())
00997     {
00998       return new TextureRectangle (NUX_TRACKER_LOCATION);
00999     }
01000 
01001     nuxAssertMsg(0, TEXT("[NuxGraphicsResources::CreateSystemCapableTexture] No support for non power of two textures or rectangle textures"));
01002 
01003     return 0;
01004   }
01005 }
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends