nux-1.14.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 #include "GLResource.h" 00024 #include "GpuDevice.h" 00025 #include "GLDeviceObjects.h" 00026 #include "GLResourceManager.h" 00027 00028 #include "GLTextureResourceManager.h" 00029 #include "GLVertexResourceManager.h" 00030 #include "GLDeviceFrameBufferObject.h" 00031 #include "GLTemplatePrimitiveBuffer.h" 00032 #include "GraphicsEngine.h" 00033 00034 namespace nux 00035 { 00036 ObjectPtr<IOpenGLTexture2D> GpuDevice::CreateTexture ( 00037 int Width 00038 , int Height 00039 , int Levels 00040 , BitmapFormat PixelFormat) 00041 { 00042 IOpenGLTexture2D *ptr; 00043 CreateTexture (Width, Height, Levels, PixelFormat, (IOpenGLTexture2D **) &ptr); 00044 ObjectPtr<IOpenGLTexture2D> h = ObjectPtr<IOpenGLTexture2D> (ptr); 00045 ptr->UnReference (); 00046 return h; 00047 } 00048 00049 int GpuDevice::CreateTexture ( 00050 t_u32 Width 00051 , t_u32 Height 00052 , t_u32 Levels 00053 //, DWORD Usage // no use 00054 , BitmapFormat PixelFormat 00055 , IOpenGLTexture2D **ppTexture 00056 //, HANDLE* pSharedHandle // no use 00057 ) 00058 { 00059 // From : http://oss.sgi.com/projects/ogl-sample/registry/ARB/texture_non_power_of_two.txt 00060 // The "floor" convention has a relatively straightforward way to 00061 // evaluate (with integer math) means to determine how many mipmap 00062 // levels are required for a complete pyramid: 00063 // numLevels = 1 + floor(log2(max(w, h, d))) 00064 t_u32 NumTotalMipLevel = 1 + floorf (Log2 (Max (Width, Height) ) ); 00065 00066 // Levels 00067 // [in] Number of levels in the texture. If this is zero, generate all texture sublevels 00068 // down to 1 by 1 pixels for hardware that supports mip-maps textures. Call GetNumMipLevel to see the 00069 // number of levels generated. 00070 t_u32 NumMipLevel = 0; 00071 00072 if (Levels == 0) 00073 { 00074 NumMipLevel = NumTotalMipLevel; 00075 } 00076 else if (Levels > NumTotalMipLevel) 00077 { 00078 NumMipLevel = NumTotalMipLevel; 00079 } 00080 else 00081 { 00082 NumMipLevel = Levels; 00083 } 00084 00085 00086 // The "floor" convention can be evaluated incrementally with the 00087 // following recursion: 00088 // 00089 // nextLODdim = max(1, currentLODdim >> 1) 00090 // 00091 // where currentLODdim is the dimension of a level N and nextLODdim 00092 // is the dimension of level N+1. The recursion stops when level 00093 // numLevels-1 is reached. 00094 00095 *ppTexture = new IOpenGLTexture2D (Width, Height, NumMipLevel, PixelFormat, false, NUX_TRACKER_LOCATION); 00096 00097 return 1; 00098 } 00099 00100 ObjectPtr<IOpenGLTexture2D> GpuDevice::CreateTexture2DFromID(int id, 00101 int width, 00102 int height, 00103 int levels, 00104 BitmapFormat pixel_format) 00105 { 00106 IOpenGLTexture2D *ptr; 00107 ptr = new IOpenGLTexture2D (width, height, levels, pixel_format, true, NUX_TRACKER_LOCATION); // ref count = 1; 00108 ptr->_OpenGLID = id; 00109 ObjectPtr<IOpenGLTexture2D> h = ObjectPtr<IOpenGLTexture2D> (ptr); // ref count = 2 00110 ptr->UnReference (); // ref count = 1 00111 return h; 00112 } 00113 00114 ObjectPtr<IOpenGLRectangleTexture> GpuDevice::CreateRectangleTexture ( 00115 int Width 00116 , int Height 00117 , int Levels 00118 , BitmapFormat PixelFormat) 00119 { 00120 IOpenGLRectangleTexture *ptr; 00121 CreateRectangleTexture (Width, Height, Levels, PixelFormat, (IOpenGLRectangleTexture **) &ptr); 00122 ObjectPtr<IOpenGLRectangleTexture> h = ObjectPtr<IOpenGLRectangleTexture> (ptr); 00123 ptr->UnReference (); 00124 return h; 00125 } 00126 00127 int GpuDevice::CreateRectangleTexture ( 00128 t_u32 Width 00129 , t_u32 Height 00130 , t_u32 Levels 00131 //, DWORD Usage // no use 00132 , BitmapFormat PixelFormat 00133 , IOpenGLRectangleTexture **ppTexture 00134 //, HANDLE* pSharedHandle // no use 00135 ) 00136 { 00137 00138 // From : http://oss.sgi.com/projects/ogl-sample/registry/ARB/texture_non_power_of_two.txt 00139 // The "floor" convention has a relatively straightforward way to 00140 // evaluate (with integer math) means to determine how many mipmap 00141 // levels are required for a complete pyramid: 00142 // numLevels = 1 + floor(log2(max(w, h, d))) 00143 t_u32 NumTotalMipLevel = 1 + floorf (Log2 (Max (Width, Height) ) ); 00144 00145 // Levels 00146 // [in] Number of levels in the texture. If this is zero, generate all texture sublevels 00147 // down to 1 by 1 pixels for hardware that supports mip-maps textures. Call GetNumMipLevel to see the 00148 // number of levels generated. 00149 t_u32 NumMipLevel = 0; 00150 00151 if (Levels == 0) 00152 { 00153 //Rectangle texture texture don't support mipmaps 00154 NumMipLevel = 1; 00155 } 00156 else if (Levels > NumTotalMipLevel) 00157 { 00158 NumMipLevel = 1; 00159 } 00160 else 00161 { 00162 NumMipLevel = 1; 00163 } 00164 00165 00166 // The "floor" convention can be evaluated incrementally with the 00167 // following recursion: 00168 // 00169 // nextLODdim = max(1, currentLODdim >> 1) 00170 // 00171 // where currentLODdim is the dimension of a level N and nextLODdim 00172 // is the dimension of level N+1. The recursion stops when level 00173 // numLevels-1 is reached. 00174 00175 *ppTexture = new IOpenGLRectangleTexture (Width, Height, NumMipLevel, PixelFormat, false, NUX_TRACKER_LOCATION); 00176 00177 00178 return 1; 00179 } 00180 00181 ObjectPtr<IOpenGLCubeTexture> GpuDevice::CreateCubeTexture ( 00182 int EdgeLength 00183 , int Levels 00184 , BitmapFormat PixelFormat) 00185 { 00186 IOpenGLCubeTexture *ptr; 00187 CreateCubeTexture (EdgeLength, Levels, PixelFormat, (IOpenGLCubeTexture **) &ptr); 00188 ObjectPtr<IOpenGLCubeTexture> h = ObjectPtr<IOpenGLCubeTexture> (ptr); 00189 ptr->UnReference (); 00190 return h; 00191 } 00192 00193 int GpuDevice::CreateCubeTexture ( 00194 t_u32 EdgeLength 00195 , t_u32 Levels 00196 //, DWORD Usage // no use 00197 , BitmapFormat PixelFormat 00198 , IOpenGLCubeTexture **ppCubeTexture 00199 //, HANDLE* pSharedHandle // no use 00200 ) 00201 { 00202 t_u32 NumTotalMipLevel = 1 + floorf (Log2 (EdgeLength) ); 00203 // Levels 00204 // [in] Number of levels in the texture. If this is zero, Direct3D will generate all texture sublevels 00205 // down to 1 by 1 pixels for hardware that supports mipmapped textures. Call GetNumMipLevel to see the 00206 // number of levels generated. 00207 t_u32 NumMipLevel = 0; 00208 00209 if (Levels == 0) 00210 { 00211 NumMipLevel = NumTotalMipLevel; 00212 } 00213 else if (Levels > NumTotalMipLevel) 00214 { 00215 NumMipLevel = NumTotalMipLevel; 00216 } 00217 else 00218 { 00219 NumMipLevel = Levels; 00220 } 00221 00222 *ppCubeTexture = new IOpenGLCubeTexture (EdgeLength, NumMipLevel, PixelFormat); 00223 00224 return 1; 00225 } 00226 00227 ObjectPtr<IOpenGLVolumeTexture> GpuDevice::CreateVolumeTexture ( 00228 int Width 00229 , int Height 00230 , int Depth 00231 , int Levels 00232 , BitmapFormat PixelFormat) 00233 { 00234 IOpenGLVolumeTexture *ptr; 00235 CreateVolumeTexture (Width, Height, Depth, Levels, PixelFormat, (IOpenGLVolumeTexture **) &ptr); 00236 ObjectPtr<IOpenGLVolumeTexture> h = ObjectPtr<IOpenGLVolumeTexture> (ptr); 00237 ptr->UnReference (); 00238 return h; 00239 } 00240 00241 int GpuDevice::CreateVolumeTexture ( 00242 t_u32 Width 00243 , t_u32 Height 00244 , t_u32 Depth 00245 , t_u32 Levels 00246 //, DWORD Usage // no use 00247 , BitmapFormat PixelFormat 00248 , IOpenGLVolumeTexture **ppVolumeTexture 00249 //, HANDLE* pSharedHandle // no use 00250 ) 00251 { 00252 t_u32 NumTotalMipLevel = 1 + floorf (Log2 (Max (Max (Width, Height), Depth) ) ); 00253 // Levels 00254 // [in] Number of levels in the texture. If this is zero, Direct3D will generate all texture sublevels 00255 // down to 1 by 1 pixels for hardware that supports mipmapped textures. Call GetNumMipLevel to see the 00256 // number of levels generated. 00257 t_u32 NumMipLevel = 0; 00258 00259 if (Levels == 0) 00260 { 00261 NumMipLevel = NumTotalMipLevel; 00262 } 00263 else if (Levels > NumTotalMipLevel) 00264 { 00265 NumMipLevel = NumTotalMipLevel; 00266 } 00267 else 00268 { 00269 NumMipLevel = Levels; 00270 } 00271 00272 *ppVolumeTexture = new IOpenGLVolumeTexture (Width, Height, Depth, NumMipLevel, PixelFormat); 00273 00274 return OGL_OK; 00275 } 00276 00277 ObjectPtr<IOpenGLAnimatedTexture> GpuDevice::CreateAnimatedTexture ( 00278 int Width 00279 , int Height 00280 , int Depth 00281 , BitmapFormat PixelFormat) 00282 { 00283 IOpenGLAnimatedTexture *ptr; 00284 CreateAnimatedTexture (Width, Height, Depth, PixelFormat, (IOpenGLAnimatedTexture **) &ptr); 00285 ObjectPtr<IOpenGLAnimatedTexture> h = ObjectPtr<IOpenGLAnimatedTexture> (ptr); 00286 ptr->UnReference (); 00287 return h; 00288 } 00289 00290 int GpuDevice::CreateAnimatedTexture (t_u32 Width, 00291 t_u32 Height, 00292 t_u32 Depth, 00293 BitmapFormat PixelFormat, 00294 IOpenGLAnimatedTexture **ppAnimatedTexture) 00295 { 00296 *ppAnimatedTexture = new IOpenGLAnimatedTexture (Width, Height, Depth, PixelFormat); 00297 00298 return OGL_OK; 00299 } 00300 00301 ObjectPtr<IOpenGLQuery> GpuDevice::CreateQuery (QUERY_TYPE Type) 00302 { 00303 IOpenGLQuery *ptr; 00304 CreateQuery (Type, (IOpenGLQuery **) &ptr); 00305 ObjectPtr<IOpenGLQuery> h = ObjectPtr<IOpenGLQuery> (ptr); 00306 ptr->UnReference (); 00307 return h; 00308 } 00309 00310 int GpuDevice::CreateQuery (QUERY_TYPE Type, IOpenGLQuery **ppQuery) 00311 { 00312 *ppQuery = new IOpenGLQuery (Type); 00313 00314 return OGL_OK; 00315 } 00316 }