nux-0.9.46
|
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 int GetMaxFboAttachment () {return _opengl_max_fb_attachment;} 00110 00111 00112 private: 00113 void Setup (); 00114 00115 bool _support_opengl_version_11; 00116 bool _support_opengl_version_12; 00117 bool _support_opengl_version_13; 00118 bool _support_opengl_version_14; 00119 bool _support_opengl_version_15; 00120 bool _support_opengl_version_20; 00121 bool _support_opengl_version_21; 00122 bool _support_opengl_version_30; 00123 bool _support_opengl_version_31; 00124 bool _support_opengl_version_32; 00125 bool _support_opengl_version_33; 00126 bool _support_opengl_version_40; 00127 bool _support_opengl_version_41; 00128 00129 int _opengl_max_texture_units; 00130 int _opengl_max_texture_coords; 00131 int _opengl_max_texture_image_units; 00132 int _opengl_max_fb_attachment; 00133 int _opengl_max_vertex_attributes; 00134 00135 bool _support_ext_swap_control; 00136 bool _support_arb_vertex_program; 00137 bool _support_arb_fragment_program; 00138 bool _support_arb_shader_objects; 00139 bool _support_arb_vertex_shader; 00140 bool _support_arb_fragment_shader; 00141 bool _support_arb_vertex_buffer_object; 00142 bool _support_arb_texture_non_power_of_two; 00143 bool _support_ext_framebuffer_object; 00144 bool _support_ext_draw_range_elements; 00145 bool _support_ext_stencil_two_side; 00146 bool _support_ext_texture_rectangle; 00147 bool _support_arb_texture_rectangle; 00148 bool _support_nv_texture_rectangle; 00149 bool _support_arb_pixel_buffer_object; 00150 bool _support_ext_blend_equation_separate; 00151 00152 friend class GpuDevice; 00153 }; 00154 00155 class GpuDevice 00156 { 00157 private: 00158 static STREAMSOURCE _StreamSource[MAX_NUM_STREAM]; 00159 00160 int CreateTexture ( 00161 unsigned int Width 00162 , unsigned int Height 00163 , unsigned int Levels 00164 , BitmapFormat PixelFormat 00165 , IOpenGLTexture2D **ppTexture 00166 ); 00167 00168 int CreateRectangleTexture ( 00169 unsigned int Width 00170 , unsigned int Height 00171 , unsigned int Levels 00172 , BitmapFormat PixelFormat 00173 , IOpenGLRectangleTexture **ppTexture 00174 ); 00175 00176 int CreateCubeTexture ( 00177 unsigned int EdgeLength 00178 , unsigned int Levels 00179 , BitmapFormat PixelFormat 00180 , IOpenGLCubeTexture **ppCubeTexture 00181 ); 00182 00183 int CreateVolumeTexture ( 00184 unsigned int Width 00185 , unsigned int Height 00186 , unsigned int Depth 00187 , unsigned int Levels 00188 , BitmapFormat PixelFormat 00189 , IOpenGLVolumeTexture **ppVolumeTexture 00190 ); 00191 00192 int CreateAnimatedTexture ( 00193 unsigned int Width 00194 , unsigned int Height 00195 , unsigned int Depth 00196 , BitmapFormat PixelFormat 00197 , IOpenGLAnimatedTexture **ppAnimatedTexture 00198 ); 00199 00200 int CreateVertexBuffer ( 00201 unsigned int Length 00202 , VBO_USAGE Usage // Dynamic or WriteOnly 00203 , IOpenGLVertexBuffer **ppVertexBuffer 00204 ); 00205 00206 int CreateIndexBuffer ( 00207 unsigned int Length 00208 , VBO_USAGE Usage // Dynamic or WriteOnly 00209 , INDEX_FORMAT Format 00210 , IOpenGLIndexBuffer **ppIndexBuffer 00211 ); 00212 00213 int CreatePixelBufferObject (int Size, VBO_USAGE Usage, // Dynamic or WriteOnly 00214 IOpenGLPixelBufferObject **ppPixelBufferObject 00215 ); 00216 00217 int CreateQuery ( 00218 QUERY_TYPE Type, 00219 IOpenGLQuery **ppQuery); 00220 00221 int CreateVertexDeclaration ( 00222 const VERTEXELEMENT *pVertexElements, 00223 IOpenGLVertexDeclaration **ppDecl); 00224 00225 int CreateFrameBufferObject ( 00226 IOpenGLFrameBufferObject **ppFrameBufferObject); 00227 00228 int CreateShaderProgram ( 00229 IOpenGLShaderProgram **ppShaderProgram); 00230 00231 int CreateVertexShader ( 00232 IOpenGLVertexShader **ppVertexShader); 00233 00234 int CreatePixelShader ( 00235 IOpenGLPixelShader **ppPixelShader); 00236 00237 int CreateAsmShaderProgram ( 00238 IOpenGLAsmShaderProgram **ppAsmShaderProgram); 00239 00240 int CreateAsmVertexShader ( 00241 IOpenGLAsmVertexShader **ppAsmVertexShader); 00242 00243 int CreateAsmPixelShader ( 00244 IOpenGLAsmPixelShader **ppAsmPixelShader); 00245 00246 #if (NUX_ENABLE_CG_SHADERS) 00247 int CreateCGVertexShader ( 00248 ICgVertexShader **ppCgVertexShader); 00249 00250 int CreateCGPixelShader ( 00251 ICgPixelShader **ppCgPixelShader); 00252 #endif 00253 00254 public: 00255 ObjectPtr<IOpenGLTexture2D> CreateTexture ( 00256 int Width 00257 , int Height 00258 , int Levels 00259 , BitmapFormat PixelFormat); 00260 00261 ObjectPtr<IOpenGLRectangleTexture> CreateRectangleTexture ( 00262 int Width 00263 , int Height 00264 , int Levels 00265 , BitmapFormat PixelFormat); 00266 00267 ObjectPtr<IOpenGLCubeTexture> CreateCubeTexture ( 00268 int EdgeLength 00269 , int Levels 00270 , BitmapFormat PixelFormat); 00271 00272 ObjectPtr<IOpenGLVolumeTexture> CreateVolumeTexture ( 00273 int Width 00274 , int Height 00275 , int Depth 00276 , int Levels 00277 , BitmapFormat PixelFormat); 00278 00279 ObjectPtr<IOpenGLAnimatedTexture> CreateAnimatedTexture ( 00280 int Width 00281 , int Height 00282 , int Depth 00283 , BitmapFormat PixelFormat); 00284 00285 ObjectPtr<IOpenGLVertexBuffer> CreateVertexBuffer ( 00286 int Length 00287 , VBO_USAGE Usage); 00288 00289 ObjectPtr<IOpenGLIndexBuffer> CreateIndexBuffer ( 00290 int Length 00291 , VBO_USAGE Usage // Dynamic or WriteOnly 00292 , INDEX_FORMAT Format); 00293 00294 ObjectPtr<IOpenGLPixelBufferObject> CreatePixelBufferObject (int Size, VBO_USAGE Usage); 00295 00296 ObjectPtr<IOpenGLQuery> CreateQuery ( 00297 QUERY_TYPE Type); 00298 00299 ObjectPtr<IOpenGLVertexDeclaration> CreateVertexDeclaration ( 00300 const VERTEXELEMENT *pVertexElements); 00301 00302 ObjectPtr<IOpenGLFrameBufferObject> CreateFrameBufferObject(); 00303 00304 ObjectPtr<IOpenGLShaderProgram> CreateShaderProgram(); 00305 ObjectPtr<IOpenGLVertexShader> CreateVertexShader(); 00306 ObjectPtr<IOpenGLPixelShader> CreatePixelShader(); 00307 ObjectPtr<IOpenGLAsmShaderProgram> CreateAsmShaderProgram(); 00308 ObjectPtr<IOpenGLAsmVertexShader> CreateAsmVertexShader(); 00309 ObjectPtr<IOpenGLAsmPixelShader> CreateAsmPixelShader(); 00310 00311 #if (NUX_ENABLE_CG_SHADERS) 00312 ObjectPtr<ICgVertexShader> CreateCGVertexShader(); 00313 ObjectPtr<ICgPixelShader> CreateCGPixelShader(); 00314 #endif 00315 00316 // This is for the fixed pipeline 00317 // When using shaders see how a shader sampler parameter links to a texture with a call to setTexture. 00318 int SetTexture (unsigned int TextureUnit, IOpenGLBaseTexture *texture); 00319 00320 int DrawIndexedPrimitive ( 00321 ObjectPtr<IOpenGLIndexBuffer> IndexBuffer, 00322 ObjectPtr<IOpenGLVertexDeclaration> VertexDeclaration, 00323 PRIMITIVE_TYPE PrimitiveType, 00324 int BaseVertexIndex, 00325 int MinIndex, 00326 int NumVertices, 00327 int StartIndex, 00328 int PrimitiveCount 00329 ); 00330 00331 // Draw Primitive without index buffer 00332 int DrawPrimitive ( 00333 ObjectPtr<IOpenGLVertexDeclaration> VertexDeclaration, 00334 PRIMITIVE_TYPE pt_, 00335 unsigned vtx_start_, 00336 unsigned num_prims_); 00337 00338 // Draw Primitive without index buffer, and use a user pointer for the source of the stream. 00339 int DrawPrimitiveUP ( 00340 ObjectPtr<IOpenGLVertexDeclaration> VertexDeclaration, 00341 PRIMITIVE_TYPE PrimitiveType, 00342 unsigned int PrimitiveCount, 00343 const void *pVertexStreamZeroData, 00344 unsigned int VertexStreamZeroStride 00345 ); 00346 00347 int SetStreamSource ( 00348 unsigned int StreamNumber, 00349 ObjectPtr<IOpenGLVertexBuffer> pStreamData, 00350 unsigned int OffsetInBytes, 00351 unsigned int Stride, 00352 unsigned int NumComponent = 0, 00353 unsigned int ComponentFormat = 0); 00354 00356 void InvalidateVertexBuffer(); 00358 void InvalidateIndexBuffer(); 00360 void InvalidateTextureUnit (int TextureUnitIndex); 00361 00362 unsigned int GetPixelStoreAlignment() 00363 { 00364 return _PixelStoreAlignment; 00365 } 00366 00367 int AllocateUnpackPixelBufferIndex (int *index); 00368 int FreeUnpackPixelBufferIndex (const int index); 00369 int BindUnpackPixelBufferIndex (const int index); 00370 int BindPackPixelBufferIndex (const int index); 00371 void *LockUnpackPixelBufferIndex (const int index, const int Size); 00372 void *LockPackPixelBufferIndex (const int index, const int Size); 00373 00374 void UnlockUnpackPixelBufferIndex (const int index); 00375 void UnlockPackPixelBufferIndex (const int index); 00376 00377 // All these operations are done on the default frame buffer object: _FrameBufferObject. 00378 int FormatFrameBufferObject (unsigned int Width, unsigned int Height, BitmapFormat PixelFormat); 00379 int SetColorRenderTargetSurface (unsigned int ColorAttachmentIndex, ObjectPtr<IOpenGLSurface> pRenderTargetSurface); 00380 int SetDepthRenderTargetSurface (ObjectPtr<IOpenGLSurface> pDepthSurface); 00381 ObjectPtr<IOpenGLSurface> GetColorRenderTargetSurface (unsigned int ColorAttachmentIndex); 00382 ObjectPtr<IOpenGLSurface> GetDepthRenderTargetSurface(); 00383 // Activate and Deactivate the default framebuffer: _FrameBufferObject. 00384 void ActivateFrameBuffer(); 00385 00387 void DeactivateFrameBuffer (); 00388 00389 public: 00390 void SetCurrentFrameBufferObject (ObjectPtr<IOpenGLFrameBufferObject> fbo); 00391 ObjectPtr<IOpenGLFrameBufferObject> GetCurrentFrameBufferObject(); 00392 00393 int GetOpenGLMajorVersion () const; 00394 int GetOpenGLMinorVersion () const; 00395 private: 00396 // Default FrameBufferobject 00397 ObjectPtr<IOpenGLFrameBufferObject> _FrameBufferObject; 00398 ObjectPtr<IOpenGLFrameBufferObject> _CurrentFrameBufferObject; 00399 00400 struct PixelBufferObject 00401 { 00402 ObjectPtr<IOpenGLPixelBufferObject> PBO; 00403 bool IsReserved; 00404 }; 00405 00406 unsigned int _PixelStoreAlignment; 00407 00408 std::vector<PixelBufferObject> _PixelBufferArray; 00409 00410 public: 00411 00412 #if (NUX_ENABLE_CG_SHADERS) 00413 CGcontext GetCgContext() 00414 { 00415 return m_Cgcontext; 00416 } 00417 CGcontext m_Cgcontext; 00418 #endif 00419 00420 inline bool UsePixelBufferObjects () const 00421 { 00422 return _UsePixelBufferObject; 00423 } 00424 00425 GpuBrand GetGPUBrand () const; 00426 00427 GpuRenderStates &GetRenderStates (); 00428 00429 GpuInfo &GetGpuInfo (); 00430 00431 void ResetRenderStates (); 00432 00433 void VerifyRenderStates (); 00434 00436 00443 ObjectPtr<IOpenGLBaseTexture> CreateSystemCapableDeviceTexture ( 00444 int Width 00445 , int Height 00446 , int Levels 00447 , BitmapFormat PixelFormat); 00448 00450 00453 BaseTexture* CreateSystemCapableTexture (); 00454 00455 bool SUPPORT_GL_ARB_TEXTURE_NON_POWER_OF_TWO() const 00456 { 00457 return _gpu_info->Support_ARB_Texture_Non_Power_Of_Two (); 00458 } 00459 00460 bool SUPPORT_GL_EXT_TEXTURE_RECTANGLE() const 00461 { 00462 return _gpu_info->Support_EXT_Texture_Rectangle (); 00463 } 00464 00465 bool SUPPORT_GL_ARB_TEXTURE_RECTANGLE() const 00466 { 00467 return _gpu_info->Support_ARB_Texture_Rectangle (); 00468 } 00469 00470 private: 00471 00472 // 00473 int _opengl_major; 00474 int _opengl_minor; 00475 int _glsl_version_major; 00476 int _glsl_version_minor; 00477 00478 NString _board_vendor_string; 00479 NString _board_renderer_string; 00480 NString _openGL_version_string; 00481 NString _glsl_version_string; 00482 GpuBrand _gpu_brand; 00483 00484 bool _UsePixelBufferObject; 00485 00486 bool OGL_EXT_SWAP_CONTROL; 00487 bool GL_ARB_VERTEX_PROGRAM; 00488 bool GL_ARB_FRAGMENT_PROGRAM; 00489 bool GL_ARB_SHADER_OBJECTS; 00490 bool GL_ARB_VERTEX_SHADER; 00491 bool GL_ARB_FRAGMENT_SHADER; 00492 bool GL_ARB_VERTEX_BUFFER_OBJECT; 00493 bool GL_ARB_TEXTURE_NON_POWER_OF_TWO; 00494 bool GL_EXT_FRAMEBUFFER_OBJECT; 00495 bool GL_EXT_DRAW_RANGE_ELEMENTS; 00496 bool GL_EXT_STENCIL_TWO_SIDE; 00497 bool GL_EXT_TEXTURE_RECTANGLE; 00498 bool GL_ARB_TEXTURE_RECTANGLE; 00499 bool GL_NV_TEXTURE_RECTANGLE; 00500 00501 GpuRenderStates *_gpu_render_states; 00502 GpuInfo *_gpu_info; 00503 00504 public: 00505 00506 #if defined (NUX_OS_WINDOWS) 00507 GpuDevice (unsigned int DeviceWidth, unsigned int DeviceHeight, BitmapFormat DeviceFormat, 00508 HDC device_context, 00509 HGLRC &opengl_rendering_context, 00510 int req_opengl_major = 1, // requested opengl major version. 00511 int req_opengl_minor = 0, // requested opengl minor version. 00512 bool opengl_es_20 = false); 00513 00514 #elif defined (NUX_OS_LINUX) 00515 GpuDevice (t_u32 DeviceWidth, t_u32 DeviceHeight, BitmapFormat DeviceFormat, 00516 Display *display, 00517 Window window, 00518 bool has_new_glx_support, 00519 GLXFBConfig fb_config, 00520 GLXContext &opengl_rendering_context, 00521 int req_opengl_major = 1, // requested opengl major version. 00522 int req_opengl_minor = 0, // requested opengl minor version. 00523 bool opengl_es_20 = false); 00524 #endif 00525 00526 ~GpuDevice(); 00527 friend class IOpenGLSurface; 00528 friend class GraphicsEngine; 00529 }; 00530 00531 } 00532 00533 #endif // GLDEVICEFACTORY_H 00534