nux-1.16.0
|
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 #ifndef GLDEVICEFACTORY_H 00024 #define GLDEVICEFACTORY_H 00025 00026 #include "GLResource.h" 00027 #include "GLDeviceFrameBufferObject.h" 00028 #include "GLDeviceObjects.h" 00029 #include "GLTextureStates.h" 00030 #include "GLTemplatePrimitiveBuffer.h" 00031 00032 namespace nux 00033 { 00034 class GpuRenderStates; 00035 00037 typedef enum 00038 { 00039 GPU_VENDOR_UNKNOWN = 0, 00040 GPU_BRAND_AMD, 00041 GPU_BRAND_NVIDIA, 00042 GPU_BRAND_INTEL, 00043 } GpuBrand; 00044 00045 template<typename T> class ObjectPtr; 00046 00047 struct STREAMSOURCE 00048 { 00049 WORD Stream; 00050 ObjectPtr<IOpenGLVertexBuffer> VertexBuffer; 00051 t_u16 StreamOffset; 00052 t_u16 StreamStride; 00053 00054 STREAMSOURCE() 00055 { 00056 Stream = 0; 00057 //VertexBuffer = 0; 00058 StreamOffset = 0; 00059 StreamStride = 0; 00060 } 00061 00062 void ResetStreamSource() 00063 { 00064 Stream = 0; 00065 StreamOffset = 0; 00066 VertexBuffer = ObjectPtr<IOpenGLVertexBuffer> (0); 00067 StreamStride = 0; 00068 } 00069 }; 00070 00071 // GPU Graphics information. 00072 class GpuInfo 00073 { 00074 public: 00075 00076 GpuInfo(); 00077 00078 bool SupportOpenGL11() const {return _support_opengl_version_11;} 00079 bool SupportOpenGL12() const {return _support_opengl_version_12;} 00080 bool SupportOpenGL13() const {return _support_opengl_version_13;} 00081 bool SupportOpenGL14() const {return _support_opengl_version_14;} 00082 bool SupportOpenGL15() const {return _support_opengl_version_15;} 00083 bool SupportOpenGL20() const {return _support_opengl_version_20;} 00084 bool SupportOpenGL21() const {return _support_opengl_version_21;} 00085 bool SupportOpenGL30() const {return _support_opengl_version_30;} 00086 bool SupportOpenGL31() const {return _support_opengl_version_31;} 00087 bool SupportOpenGL33() const {return _support_opengl_version_33;} 00088 bool SupportOpenGL32() const {return _support_opengl_version_32;} 00089 bool SupportOpenGL40() const {return _support_opengl_version_40;} 00090 bool SupportOpenGL41() const {return _support_opengl_version_41;} 00091 00092 bool Support_EXT_Swap_Control () const {return _support_ext_swap_control;} 00093 bool Support_ARB_Texture_Rectangle () const {return _support_arb_texture_rectangle;} 00094 bool Support_ARB_Vertex_Program () const {return _support_arb_vertex_program;} 00095 bool Support_ARB_Fragment_Program () const {return _support_arb_fragment_program;} 00096 bool Support_ARB_Shader_Objects () const {return _support_arb_shader_objects;} 00097 bool Support_ARB_Vertex_Shader () const {return _support_arb_vertex_shader;} 00098 bool Support_ARB_Fragment_Shader () const {return _support_arb_fragment_shader;} 00099 bool Support_ARB_Vertex_Buffer_Object () const {return _support_arb_vertex_buffer_object;} 00100 bool Support_ARB_Texture_Non_Power_Of_Two () const {return _support_arb_texture_non_power_of_two;} 00101 bool Support_EXT_Framebuffer_Object () const {return _support_ext_framebuffer_object;} 00102 bool Support_EXT_Draw_Range_Elements () const {return _support_ext_draw_range_elements;} 00103 bool Support_EXT_Stencil_Two_Side () const {return _support_ext_stencil_two_side;} 00104 bool Support_EXT_Texture_Rectangle () const {return _support_ext_texture_rectangle;} 00105 bool Support_NV_Texture_Rectangle () const {return _support_nv_texture_rectangle;} 00106 bool Support_ARB_Pixel_Buffer_Object () const {return _support_arb_pixel_buffer_object;} 00107 bool Support_EXT_Blend_Equation_Separate () const {return _support_ext_blend_equation_separate;} 00108 00109 #ifndef NUX_OPENGLES_20 00110 bool Support_EXT_Texture_sRGB () const {return _support_ext_texture_srgb;} 00111 bool Support_EXT_Texture_Decode () const {return _support_ext_texture_srgb_decode;} 00112 bool Support_EXT_Framebuffer_sRGB () const {return _support_ext_framebuffer_srgb;} 00113 bool Support_ARB_Framebuffer_sRGB () const {return _support_arb_framebuffer_srgb;} 00114 #endif 00115 00116 int GetMaxFboAttachment () {return _opengl_max_fb_attachment;} 00117 00118 00119 private: 00120 void Setup (); 00121 00122 bool _support_opengl_version_11; 00123 bool _support_opengl_version_12; 00124 bool _support_opengl_version_13; 00125 bool _support_opengl_version_14; 00126 bool _support_opengl_version_15; 00127 bool _support_opengl_version_20; 00128 bool _support_opengl_version_21; 00129 bool _support_opengl_version_30; 00130 bool _support_opengl_version_31; 00131 bool _support_opengl_version_32; 00132 bool _support_opengl_version_33; 00133 bool _support_opengl_version_40; 00134 bool _support_opengl_version_41; 00135 00136 int _opengl_max_texture_units; 00137 int _opengl_max_texture_coords; 00138 int _opengl_max_texture_image_units; 00139 int _opengl_max_fb_attachment; 00140 int _opengl_max_vertex_attributes; 00141 00142 bool _support_ext_swap_control; 00143 bool _support_arb_vertex_program; 00144 bool _support_arb_fragment_program; 00145 bool _support_arb_shader_objects; 00146 bool _support_arb_vertex_shader; 00147 bool _support_arb_fragment_shader; 00148 bool _support_arb_vertex_buffer_object; 00149 bool _support_arb_texture_non_power_of_two; 00150 bool _support_ext_framebuffer_object; 00151 bool _support_ext_draw_range_elements; 00152 bool _support_ext_stencil_two_side; 00153 bool _support_ext_texture_rectangle; 00154 bool _support_arb_texture_rectangle; 00155 bool _support_nv_texture_rectangle; 00156 bool _support_arb_pixel_buffer_object; 00157 bool _support_ext_blend_equation_separate; 00158 00159 #ifndef NUX_OPENGLES_20 00160 bool _support_ext_texture_srgb; 00161 bool _support_ext_texture_srgb_decode; 00162 bool _support_ext_framebuffer_srgb; 00163 bool _support_arb_framebuffer_srgb; 00164 #endif 00165 00166 friend class GpuDevice; 00167 }; 00168 00169 class GpuDevice 00170 { 00171 private: 00172 static STREAMSOURCE _StreamSource[MAX_NUM_STREAM]; 00173 00174 int CreateTexture ( 00175 unsigned int Width 00176 , unsigned int Height 00177 , unsigned int Levels 00178 , BitmapFormat PixelFormat 00179 , IOpenGLTexture2D **ppTexture 00180 ); 00181 00182 int CreateRectangleTexture ( 00183 unsigned int Width 00184 , unsigned int Height 00185 , unsigned int Levels 00186 , BitmapFormat PixelFormat 00187 , IOpenGLRectangleTexture **ppTexture 00188 ); 00189 00190 int CreateCubeTexture ( 00191 unsigned int EdgeLength 00192 , unsigned int Levels 00193 , BitmapFormat PixelFormat 00194 , IOpenGLCubeTexture **ppCubeTexture 00195 ); 00196 00197 int CreateVolumeTexture ( 00198 unsigned int Width 00199 , unsigned int Height 00200 , unsigned int Depth 00201 , unsigned int Levels 00202 , BitmapFormat PixelFormat 00203 , IOpenGLVolumeTexture **ppVolumeTexture 00204 ); 00205 00206 int CreateAnimatedTexture ( 00207 unsigned int Width 00208 , unsigned int Height 00209 , unsigned int Depth 00210 , BitmapFormat PixelFormat 00211 , IOpenGLAnimatedTexture **ppAnimatedTexture 00212 ); 00213 00214 int CreateVertexBuffer ( 00215 unsigned int Length 00216 , VBO_USAGE Usage // Dynamic or WriteOnly 00217 , IOpenGLVertexBuffer **ppVertexBuffer 00218 ); 00219 00220 int CreateIndexBuffer ( 00221 unsigned int Length 00222 , VBO_USAGE Usage // Dynamic or WriteOnly 00223 , INDEX_FORMAT Format 00224 , IOpenGLIndexBuffer **ppIndexBuffer 00225 ); 00226 00227 int CreatePixelBufferObject (int Size, VBO_USAGE Usage, // Dynamic or WriteOnly 00228 IOpenGLPixelBufferObject **ppPixelBufferObject 00229 ); 00230 00231 int CreateQuery ( 00232 QUERY_TYPE Type, 00233 IOpenGLQuery **ppQuery); 00234 00235 int CreateVertexDeclaration ( 00236 const VERTEXELEMENT *pVertexElements, 00237 IOpenGLVertexDeclaration **ppDecl); 00238 00239 int CreateFrameBufferObject ( 00240 IOpenGLFrameBufferObject **ppFrameBufferObject); 00241 00242 int CreateShaderProgram ( 00243 IOpenGLShaderProgram **ppShaderProgram); 00244 00245 int CreateVertexShader ( 00246 IOpenGLVertexShader **ppVertexShader); 00247 00248 int CreatePixelShader ( 00249 IOpenGLPixelShader **ppPixelShader); 00250 00251 int CreateAsmShaderProgram ( 00252 IOpenGLAsmShaderProgram **ppAsmShaderProgram); 00253 00254 int CreateAsmVertexShader ( 00255 IOpenGLAsmVertexShader **ppAsmVertexShader); 00256 00257 int CreateAsmPixelShader ( 00258 IOpenGLAsmPixelShader **ppAsmPixelShader); 00259 00260 #if (NUX_ENABLE_CG_SHADERS) 00261 int CreateCGVertexShader ( 00262 ICgVertexShader **ppCgVertexShader); 00263 00264 int CreateCGPixelShader ( 00265 ICgPixelShader **ppCgPixelShader); 00266 #endif 00267 00268 public: 00269 ObjectPtr<IOpenGLTexture2D> CreateTexture ( 00270 int Width, 00271 int Height, 00272 int Levels, 00273 BitmapFormat PixelFormat); 00274 00275 ObjectPtr<IOpenGLTexture2D> CreateTexture2DFromID(int id, 00276 int Width, 00277 int Height, 00278 int Levels, 00279 BitmapFormat PixelFormat); 00280 00281 ObjectPtr<IOpenGLRectangleTexture> CreateRectangleTexture ( 00282 int Width 00283 , int Height 00284 , int Levels 00285 , BitmapFormat PixelFormat); 00286 00287 ObjectPtr<IOpenGLCubeTexture> CreateCubeTexture ( 00288 int EdgeLength 00289 , int Levels 00290 , BitmapFormat PixelFormat); 00291 00292 ObjectPtr<IOpenGLVolumeTexture> CreateVolumeTexture ( 00293 int Width 00294 , int Height 00295 , int Depth 00296 , int Levels 00297 , BitmapFormat PixelFormat); 00298 00299 ObjectPtr<IOpenGLAnimatedTexture> CreateAnimatedTexture ( 00300 int Width 00301 , int Height 00302 , int Depth 00303 , BitmapFormat PixelFormat); 00304 00305 ObjectPtr<IOpenGLVertexBuffer> CreateVertexBuffer ( 00306 int Length 00307 , VBO_USAGE Usage); 00308 00309 ObjectPtr<IOpenGLIndexBuffer> CreateIndexBuffer ( 00310 int Length 00311 , VBO_USAGE Usage // Dynamic or WriteOnly 00312 , INDEX_FORMAT Format); 00313 00314 ObjectPtr<IOpenGLPixelBufferObject> CreatePixelBufferObject (int Size, VBO_USAGE Usage); 00315 00316 ObjectPtr<IOpenGLQuery> CreateQuery ( 00317 QUERY_TYPE Type); 00318 00319 ObjectPtr<IOpenGLVertexDeclaration> CreateVertexDeclaration ( 00320 const VERTEXELEMENT *pVertexElements); 00321 00322 ObjectPtr<IOpenGLFrameBufferObject> CreateFrameBufferObject(); 00323 00324 ObjectPtr<IOpenGLShaderProgram> CreateShaderProgram(); 00325 ObjectPtr<IOpenGLVertexShader> CreateVertexShader(); 00326 ObjectPtr<IOpenGLPixelShader> CreatePixelShader(); 00327 ObjectPtr<IOpenGLAsmShaderProgram> CreateAsmShaderProgram(); 00328 ObjectPtr<IOpenGLAsmVertexShader> CreateAsmVertexShader(); 00329 ObjectPtr<IOpenGLAsmPixelShader> CreateAsmPixelShader(); 00330 00331 #if (NUX_ENABLE_CG_SHADERS) 00332 ObjectPtr<ICgVertexShader> CreateCGVertexShader(); 00333 ObjectPtr<ICgPixelShader> CreateCGPixelShader(); 00334 #endif 00335 00336 // This is for the fixed pipeline 00337 // When using shaders see how a shader sampler parameter links to a texture with a call to setTexture. 00338 int SetTexture (unsigned int TextureUnit, IOpenGLBaseTexture *texture); 00339 00340 int DrawIndexedPrimitive ( 00341 ObjectPtr<IOpenGLIndexBuffer> IndexBuffer, 00342 ObjectPtr<IOpenGLVertexDeclaration> VertexDeclaration, 00343 PRIMITIVE_TYPE PrimitiveType, 00344 int PrimitiveCount 00345 ); 00346 00347 // Draw Primitive without index buffer 00348 int DrawPrimitive ( 00349 ObjectPtr<IOpenGLVertexDeclaration> VertexDeclaration, 00350 PRIMITIVE_TYPE pt_, 00351 unsigned vtx_start_, 00352 unsigned num_prims_); 00353 00354 // Draw Primitive without index buffer, and use a user pointer for the source of the stream. 00355 int DrawPrimitiveUP ( 00356 ObjectPtr<IOpenGLVertexDeclaration> VertexDeclaration, 00357 PRIMITIVE_TYPE PrimitiveType, 00358 unsigned int PrimitiveCount, 00359 const void *pVertexStreamZeroData, 00360 unsigned int VertexStreamZeroStride 00361 ); 00362 00363 int SetStreamSource ( 00364 unsigned int StreamNumber, 00365 ObjectPtr<IOpenGLVertexBuffer> pStreamData, 00366 unsigned int OffsetInBytes, 00367 unsigned int Stride); 00368 00370 void InvalidateVertexBuffer(); 00372 void InvalidateIndexBuffer(); 00374 void InvalidateTextureUnit (int TextureUnitIndex); 00375 00376 unsigned int GetPixelStoreAlignment() 00377 { 00378 return _PixelStoreAlignment; 00379 } 00380 00381 int AllocateUnpackPixelBufferIndex (int *index); 00382 int FreeUnpackPixelBufferIndex (const int index); 00383 int BindUnpackPixelBufferIndex (const int index); 00384 int BindPackPixelBufferIndex (const int index); 00385 void *LockUnpackPixelBufferIndex (const int index, const int Size); 00386 void *LockPackPixelBufferIndex (const int index, const int Size); 00387 00388 void UnlockUnpackPixelBufferIndex (const int index); 00389 void UnlockPackPixelBufferIndex (const int index); 00390 00391 // All these operations are done on the default frame buffer object: _FrameBufferObject. 00392 int FormatFrameBufferObject (unsigned int Width, unsigned int Height, BitmapFormat PixelFormat); 00393 int SetColorRenderTargetSurface (unsigned int ColorAttachmentIndex, ObjectPtr<IOpenGLSurface> pRenderTargetSurface); 00394 int SetDepthRenderTargetSurface (ObjectPtr<IOpenGLSurface> pDepthSurface); 00395 ObjectPtr<IOpenGLSurface> GetColorRenderTargetSurface (unsigned int ColorAttachmentIndex); 00396 ObjectPtr<IOpenGLSurface> GetDepthRenderTargetSurface(); 00397 // Activate and Deactivate the default framebuffer: _FrameBufferObject. 00398 void ActivateFrameBuffer(); 00399 00401 void DeactivateFrameBuffer (); 00402 00403 public: 00404 void SetCurrentFrameBufferObject (ObjectPtr<IOpenGLFrameBufferObject> fbo); 00405 ObjectPtr<IOpenGLFrameBufferObject> GetCurrentFrameBufferObject(); 00406 00407 int GetOpenGLMajorVersion () const; 00408 int GetOpenGLMinorVersion () const; 00409 private: 00410 // Default FrameBufferobject 00411 ObjectPtr<IOpenGLFrameBufferObject> _FrameBufferObject; 00412 ObjectPtr<IOpenGLFrameBufferObject> _CurrentFrameBufferObject; 00413 00414 struct PixelBufferObject 00415 { 00416 ObjectPtr<IOpenGLPixelBufferObject> PBO; 00417 bool IsReserved; 00418 }; 00419 00420 unsigned int _PixelStoreAlignment; 00421 00422 std::vector<PixelBufferObject> _PixelBufferArray; 00423 00424 public: 00425 00426 #if (NUX_ENABLE_CG_SHADERS) 00427 CGcontext GetCgContext() 00428 { 00429 return m_Cgcontext; 00430 } 00431 CGcontext m_Cgcontext; 00432 #endif 00433 00434 inline bool UsePixelBufferObjects () const 00435 { 00436 return _UsePixelBufferObject; 00437 } 00438 00439 GpuBrand GetGPUBrand () const; 00440 00441 GpuRenderStates &GetRenderStates (); 00442 00443 GpuInfo &GetGpuInfo (); 00444 00445 void ResetRenderStates (); 00446 00447 void VerifyRenderStates (); 00448 00450 00457 ObjectPtr<IOpenGLBaseTexture> CreateSystemCapableDeviceTexture ( 00458 int Width 00459 , int Height 00460 , int Levels 00461 , BitmapFormat PixelFormat); 00462 00464 00467 BaseTexture* CreateSystemCapableTexture (); 00468 00469 bool SUPPORT_GL_ARB_TEXTURE_NON_POWER_OF_TWO() const 00470 { 00471 return _gpu_info->Support_ARB_Texture_Non_Power_Of_Two (); 00472 } 00473 00474 bool SUPPORT_GL_EXT_TEXTURE_RECTANGLE() const 00475 { 00476 return _gpu_info->Support_EXT_Texture_Rectangle (); 00477 } 00478 00479 bool SUPPORT_GL_ARB_TEXTURE_RECTANGLE() const 00480 { 00481 return _gpu_info->Support_ARB_Texture_Rectangle (); 00482 } 00483 00484 private: 00485 00486 // 00487 int _opengl_major; 00488 int _opengl_minor; 00489 int _glsl_version_major; 00490 int _glsl_version_minor; 00491 00492 NString _board_vendor_string; 00493 NString _board_renderer_string; 00494 NString _openGL_version_string; 00495 NString _glsl_version_string; 00496 GpuBrand _gpu_brand; 00497 00498 bool _UsePixelBufferObject; 00499 00500 bool OGL_EXT_SWAP_CONTROL; 00501 bool GL_ARB_VERTEX_PROGRAM; 00502 bool GL_ARB_FRAGMENT_PROGRAM; 00503 bool GL_ARB_SHADER_OBJECTS; 00504 bool GL_ARB_VERTEX_SHADER; 00505 bool GL_ARB_FRAGMENT_SHADER; 00506 bool GL_ARB_VERTEX_BUFFER_OBJECT; 00507 bool GL_ARB_TEXTURE_NON_POWER_OF_TWO; 00508 bool GL_EXT_FRAMEBUFFER_OBJECT; 00509 bool GL_EXT_DRAW_RANGE_ELEMENTS; 00510 bool GL_EXT_STENCIL_TWO_SIDE; 00511 bool GL_EXT_TEXTURE_RECTANGLE; 00512 bool GL_ARB_TEXTURE_RECTANGLE; 00513 bool GL_NV_TEXTURE_RECTANGLE; 00514 00515 GpuRenderStates *_gpu_render_states; 00516 GpuInfo *_gpu_info; 00517 00518 public: 00519 00520 ObjectPtr<IOpenGLTexture2D> backup_texture0_; 00521 00522 #if defined (NUX_OS_WINDOWS) 00523 GpuDevice (unsigned int DeviceWidth, unsigned int DeviceHeight, BitmapFormat DeviceFormat, 00524 HDC device_context, 00525 HGLRC &opengl_rendering_context, 00526 int req_opengl_major = 1, // requested opengl major version. 00527 int req_opengl_minor = 0, // requested opengl minor version. 00528 bool opengl_es_20 = false); 00529 00530 #elif defined (NUX_OS_LINUX) 00531 GpuDevice (t_u32 DeviceWidth, t_u32 DeviceHeight, BitmapFormat DeviceFormat, 00532 Display *display, 00533 Window window, 00534 bool has_new_glx_support, 00535 GLXFBConfig fb_config, 00536 GLXContext &opengl_rendering_context, 00537 int req_opengl_major = 1, // requested opengl major version. 00538 int req_opengl_minor = 0, // requested opengl minor version. 00539 bool opengl_es_20 = false); 00540 #endif 00541 00542 ~GpuDevice(); 00543 friend class IOpenGLSurface; 00544 friend class GraphicsEngine; 00545 }; 00546 00547 } 00548 00549 #endif // GLDEVICEFACTORY_H 00550