Back to index

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