Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

OgreD3D9Texture.h

Go to the documentation of this file.
00001 /*
00002 -----------------------------------------------------------------------------
00003 This source file is part of OGRE
00004     (Object-oriented Graphics Rendering Engine)
00005 For the latest info, see http://www.ogre3d.org/
00006 
00007 Copyright (c) 2000-2005 The OGRE Team
00008 Also see acknowledgements in Readme.html
00009 
00010 This program is free software; you can redistribute it and/or modify it under
00011 the terms of the GNU Lesser General Public License as published by the Free Software
00012 Foundation; either version 2 of the License, or (at your option) any later
00013 version.
00014 
00015 This program is distributed in the hope that it will be useful, but WITHOUT
00016 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00017 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
00018 
00019 You should have received a copy of the GNU Lesser General Public License along with
00020 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
00021 Place - Suite 330, Boston, MA 02111-1307, USA, or go to
00022 http://www.gnu.org/copyleft/lesser.txt.
00023 -----------------------------------------------------------------------------
00024 */
00025 #ifndef __D3D8TEXTURE_H__
00026 #define __D3D8TEXTURE_H__
00027 
00028 #include "OgreD3D9Prerequisites.h"
00029 #include "OgreTexture.h"
00030 #include "OgreRenderTexture.h"
00031 #include "OgreImage.h"
00032 #include "OgreException.h"
00033 #include "OgreD3D9HardwarePixelBuffer.h"
00034 
00035 #include "OgreNoMemoryMacros.h"
00036 #include <d3d9.h>
00037 #include <d3dx9.h>
00038 #include <dxerr9.h>
00039 #include "OgreMemoryMacros.h"
00040 
00041 namespace Ogre {
00042     class D3D9Texture : public Texture
00043     {
00044     protected:
00046         IDirect3DDevice9        *mpDev;     
00048         IDirect3D9              *mpD3D;
00050         IDirect3DTexture9       *mpNormTex; 
00052         IDirect3DCubeTexture9   *mpCubeTex; 
00054         IDirect3DVolumeTexture9 *mpVolumeTex;
00056         IDirect3DSurface9       *mpZBuff;   
00058         IDirect3DBaseTexture9   *mpTex;     
00059 
00061         String                          mCubeFaceNames[6];
00063         D3DDEVICE_CREATION_PARAMETERS   mDevCreParams;
00065         D3DFORMAT                       mBBPixelFormat;
00067         D3DPOOL                         mD3DPool;
00069         D3DCAPS9                        mDevCaps;
00070         // Auto-generated mipmaps?
00071         bool                            mAutoGenMipmaps;
00072         // Dynamic textures?
00073         bool                            mDynamicTextures;
00075         typedef std::vector<HardwarePixelBufferSharedPtr> SurfaceList;
00076         SurfaceList                     mSurfaceList;
00077     
00079         void _initDevice(void);
00081         void _loadCubeTex();
00083         void _loadNormTex();
00085         void _loadVolumeTex();
00086 
00088         void _createNormTex();
00090         void _createCubeTex();
00092         void _createVolumeTex();
00093 
00095         D3DFORMAT _chooseD3DFormat();
00096 
00098         void _freeResources();
00100         void _setSrcAttributes(unsigned long width, unsigned long height, unsigned long depth, PixelFormat format);
00102         void _setFinalAttributes(unsigned long width, unsigned long height, unsigned long depth, PixelFormat format);
00104         D3DTEXTUREFILTERTYPE _getBestFilterMethod();
00106         bool _canUseDynamicTextures(DWORD srcUsage, D3DRESOURCETYPE srcType, D3DFORMAT srcFormat);
00108         bool _canAutoGenMipmaps(DWORD srcUsage, D3DRESOURCETYPE srcType, D3DFORMAT srcFormat);
00110         void _createDepthStencil();
00111 
00113         String _getCubeFaceName(unsigned char face) const
00114         { assert(face < 6); return mCubeFaceNames[face]; }
00115         
00118         void _createSurfaceList(bool updateOldList=false);
00119 
00121         void loadImpl();
00123         void unloadImpl();
00124     public:
00126         D3D9Texture(ResourceManager* creator, const String& name, ResourceHandle handle,
00127             const String& group, bool isManual, ManualResourceLoader* loader, 
00128             IDirect3DDevice9 *pD3DDevice);
00130         ~D3D9Texture();
00131 
00133         void copyToTexture( TexturePtr& target );
00135         void loadImage( const Image &img );
00136 
00138         void createInternalResources(void);
00139 
00141         HardwarePixelBufferSharedPtr getBuffer(size_t face, size_t mipmap);
00142         
00144         IDirect3DBaseTexture9 *getTexture() 
00145         { assert(mpTex); return mpTex; }
00147         IDirect3DTexture9 *getNormTexture()
00148         { assert(mpNormTex); return mpNormTex; }
00150         IDirect3DCubeTexture9 *getCubeTexture()
00151         { assert(mpCubeTex); return mpCubeTex; }
00153         IDirect3DSurface9 *getDepthStencil() 
00154         { assert(mpZBuff); return mpZBuff; }
00155         
00157         static PixelFormat _getPF(D3DFORMAT d3dPF);
00159         static D3DFORMAT _getPF(PixelFormat ogrePF);
00161         static PixelFormat _getClosestSupportedPF(PixelFormat ogrePF);
00162 
00164         void releaseIfDefaultPool(void);
00166         void recreateIfDefaultPool(LPDIRECT3DDEVICE9 pDev);
00167 
00168     };
00169 
00176     class D3D9TexturePtr : public SharedPtr<D3D9Texture> 
00177     {
00178     public:
00179         D3D9TexturePtr() : SharedPtr<D3D9Texture>() {}
00180         explicit D3D9TexturePtr(D3D9Texture* rep) : SharedPtr<D3D9Texture>(rep) {}
00181         D3D9TexturePtr(const D3D9TexturePtr& r) : SharedPtr<D3D9Texture>(r) {} 
00182         D3D9TexturePtr(const ResourcePtr& r) : SharedPtr<D3D9Texture>()
00183         {
00184             // lock & copy other mutex pointer
00185             OGRE_LOCK_MUTEX(*r.OGRE_AUTO_MUTEX_NAME)
00186             OGRE_COPY_AUTO_SHARED_MUTEX(r.OGRE_AUTO_MUTEX_NAME)
00187             pRep = static_cast<D3D9Texture*>(r.getPointer());
00188             pUseCount = r.useCountPointer();
00189             if (pUseCount)
00190             {
00191                 ++(*pUseCount);
00192             }
00193         }
00194 
00196         D3D9TexturePtr& operator=(const ResourcePtr& r)
00197         {
00198             if (pRep == static_cast<D3D9Texture*>(r.getPointer()))
00199                 return *this;
00200             release();
00201             // lock & copy other mutex pointer
00202             OGRE_LOCK_MUTEX(*r.OGRE_AUTO_MUTEX_NAME)
00203             OGRE_COPY_AUTO_SHARED_MUTEX(r.OGRE_AUTO_MUTEX_NAME)
00204             pRep = static_cast<D3D9Texture*>(r.getPointer());
00205             pUseCount = r.useCountPointer();
00206             if (pUseCount)
00207             {
00208                 ++(*pUseCount);
00209             }
00210             return *this;
00211         }
00213         D3D9TexturePtr& operator=(const TexturePtr& r)
00214         {
00215             if (pRep == static_cast<D3D9Texture*>(r.getPointer()))
00216                 return *this;
00217             release();
00218             // lock & copy other mutex pointer
00219             OGRE_LOCK_MUTEX(*r.OGRE_AUTO_MUTEX_NAME)
00220             OGRE_COPY_AUTO_SHARED_MUTEX(r.OGRE_AUTO_MUTEX_NAME)
00221             pRep = static_cast<D3D9Texture*>(r.getPointer());
00222             pUseCount = r.useCountPointer();
00223             if (pUseCount)
00224             {
00225                 ++(*pUseCount);
00226             }
00227             return *this;
00228         }
00229     };
00230 
00231 
00233     class D3D9RenderTexture : public RenderTexture
00234     {
00235     public:
00236         D3D9RenderTexture( const String & name, 
00237             unsigned int width, unsigned int height,
00238             TextureType texType, PixelFormat internalFormat, 
00239             const NameValuePairList *miscParams ) 
00240             : RenderTexture( name, width, height, texType, internalFormat )
00241         {
00242             mPrivateTex = TextureManager::getSingleton().createManual
00243                 (mName + "_PRIVATE##", ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, 
00244                 texType, mWidth, mHeight, 0, internalFormat, TU_RENDERTARGET );
00245         }
00246         
00247         ~D3D9RenderTexture()
00248         {
00249             mPrivateTex->unload();
00250             TextureManager::getSingleton().remove(mPrivateTex->getName());
00251             
00252         }
00253 
00254         virtual void getCustomAttribute( const String& name, void *pData )
00255         {
00256             if( name == "DDBACKBUFFER" )
00257             {
00258                 IDirect3DSurface9 ** pSurf = (IDirect3DSurface9 **)pData;
00259                 if (mPrivateTex->getTextureType() == TEX_TYPE_2D)
00260                     mPrivateTex->getNormTexture()->GetSurfaceLevel( 0, &(*pSurf) );
00261                 else if (mPrivateTex->getTextureType() == TEX_TYPE_CUBE_MAP)
00262                     mPrivateTex->getCubeTexture()->GetCubeMapSurface( (D3DCUBEMAP_FACES)0, 0, &(*pSurf) );
00263                 else
00264                 {
00265                     OGRE_EXCEPT( Exception::UNIMPLEMENTED_FEATURE, 
00266                             "getCustomAttribute is implemented only for 2D and cube textures !!!", 
00267                             "D3D9RenderTexture::getCustomAttribute" );
00268                 }
00269                 // decrement reference count
00270                 (*pSurf)->Release();
00271                 return;
00272             }
00273             else if( name == "D3DZBUFFER" )
00274             {
00275                 IDirect3DSurface9 ** pSurf = (IDirect3DSurface9 **)pData;
00276                 *pSurf = mPrivateTex->getDepthStencil();
00277                 return;
00278             }
00279             else if( name == "DDFRONTBUFFER" )
00280             {
00281                 IDirect3DSurface9 ** pSurf = (IDirect3DSurface9 **)pData;
00282                 if (mPrivateTex->getTextureType() == TEX_TYPE_2D)
00283                     mPrivateTex->getNormTexture()->GetSurfaceLevel( 0, &(*pSurf) );
00284                 else if (mPrivateTex->getTextureType() == TEX_TYPE_CUBE_MAP)
00285                     mPrivateTex->getCubeTexture()->GetCubeMapSurface( (D3DCUBEMAP_FACES)0, 0, &(*pSurf) );
00286                 else
00287                 {
00288                     OGRE_EXCEPT( Exception::UNIMPLEMENTED_FEATURE, 
00289                             "getCustomAttribute is implemented only for 2D and cube textures !!!", 
00290                             "D3D9RenderTexture::getCustomAttribute" );
00291                 }
00292                 (*pSurf)->Release();
00293                 return;
00294             }
00295             else if( name == "HWND" )
00296             {
00297                 HWND *pHwnd = (HWND*)pData;
00298                 *pHwnd = NULL;
00299                 return;
00300             }
00301             else if( name == "isTexture" )
00302             {
00303                 bool *b = reinterpret_cast< bool * >( pData );
00304                 *b = true;
00305                 return;
00306             }
00307         }
00308 
00309         bool requiresTextureFlipping() const { return false; }
00310         virtual void writeContentsToFile( const String & filename ) {}
00311 
00312     protected:
00314         D3D9TexturePtr mPrivateTex;
00315         
00316         virtual void _copyToTexture()
00317         {
00318             // Copy the newly-rendered data to the public texture surface.
00319             mPrivateTex->copyToTexture( mTexture );
00320         }
00321     };
00322 }
00323 
00324 #endif

Copyright © 2000-2005 by The OGRE Team
Last modified Wed Feb 23 00:19:05 2005