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         // Dynamic textures?
00071         bool                            mDynamicTextures;
00073         typedef std::vector<HardwarePixelBufferSharedPtr> SurfaceList;
00074         SurfaceList                     mSurfaceList;
00075     
00077         void _initDevice(void);
00079         void _loadCubeTex();
00081         void _loadNormTex();
00083         void _loadVolumeTex();
00084 
00086         void _createNormTex();
00088         void _createCubeTex();
00090         void _createVolumeTex();
00091 
00093         D3DFORMAT _chooseD3DFormat();
00094 
00096         void createInternalResourcesImpl(void);
00098         void freeInternalResourcesImpl(void);
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(void);
00119 
00121         void loadImpl();
00122     public:
00124         D3D9Texture(ResourceManager* creator, const String& name, ResourceHandle handle,
00125             const String& group, bool isManual, ManualResourceLoader* loader, 
00126             IDirect3DDevice9 *pD3DDevice);
00128         ~D3D9Texture();
00129 
00131         void copyToTexture( TexturePtr& target );
00133         void loadImage( const Image &img );
00134 
00135 
00137         HardwarePixelBufferSharedPtr getBuffer(size_t face, size_t mipmap);
00138         
00140         IDirect3DBaseTexture9 *getTexture() 
00141         { assert(mpTex); return mpTex; }
00143         IDirect3DTexture9 *getNormTexture()
00144         { assert(mpNormTex); return mpNormTex; }
00146         IDirect3DCubeTexture9 *getCubeTexture()
00147         { assert(mpCubeTex); return mpCubeTex; }
00149         IDirect3DSurface9 *getDepthStencil() 
00150         { assert(mpZBuff); return mpZBuff; }
00151         
00153         static PixelFormat _getPF(D3DFORMAT d3dPF);
00155         static D3DFORMAT _getPF(PixelFormat ogrePF);
00157         static PixelFormat _getClosestSupportedPF(PixelFormat ogrePF);
00158 
00160         bool releaseIfDefaultPool(void);
00162         bool recreateIfDefaultPool(LPDIRECT3DDEVICE9 pDev);
00163 
00164     };
00165 
00172     class D3D9TexturePtr : public SharedPtr<D3D9Texture> 
00173     {
00174     public:
00175         D3D9TexturePtr() : SharedPtr<D3D9Texture>() {}
00176         explicit D3D9TexturePtr(D3D9Texture* rep) : SharedPtr<D3D9Texture>(rep) {}
00177         D3D9TexturePtr(const D3D9TexturePtr& r) : SharedPtr<D3D9Texture>(r) {} 
00178         D3D9TexturePtr(const ResourcePtr& r) : SharedPtr<D3D9Texture>()
00179         {
00180             // lock & copy other mutex pointer
00181             OGRE_LOCK_MUTEX(*r.OGRE_AUTO_MUTEX_NAME)
00182             OGRE_COPY_AUTO_SHARED_MUTEX(r.OGRE_AUTO_MUTEX_NAME)
00183             pRep = static_cast<D3D9Texture*>(r.getPointer());
00184             pUseCount = r.useCountPointer();
00185             if (pUseCount)
00186             {
00187                 ++(*pUseCount);
00188             }
00189         }
00190 
00192         D3D9TexturePtr& operator=(const ResourcePtr& r)
00193         {
00194             if (pRep == static_cast<D3D9Texture*>(r.getPointer()))
00195                 return *this;
00196             release();
00197             // lock & copy other mutex pointer
00198             OGRE_LOCK_MUTEX(*r.OGRE_AUTO_MUTEX_NAME)
00199             OGRE_COPY_AUTO_SHARED_MUTEX(r.OGRE_AUTO_MUTEX_NAME)
00200             pRep = static_cast<D3D9Texture*>(r.getPointer());
00201             pUseCount = r.useCountPointer();
00202             if (pUseCount)
00203             {
00204                 ++(*pUseCount);
00205             }
00206             return *this;
00207         }
00209         D3D9TexturePtr& operator=(const TexturePtr& r)
00210         {
00211             if (pRep == static_cast<D3D9Texture*>(r.getPointer()))
00212                 return *this;
00213             release();
00214             // lock & copy other mutex pointer
00215             OGRE_LOCK_MUTEX(*r.OGRE_AUTO_MUTEX_NAME)
00216             OGRE_COPY_AUTO_SHARED_MUTEX(r.OGRE_AUTO_MUTEX_NAME)
00217             pRep = static_cast<D3D9Texture*>(r.getPointer());
00218             pUseCount = r.useCountPointer();
00219             if (pUseCount)
00220             {
00221                 ++(*pUseCount);
00222             }
00223             return *this;
00224         }
00225     };
00226 
00227 
00229     class D3D9RenderTexture : public RenderTexture
00230     {
00231     public:
00232         D3D9RenderTexture( const String & name, 
00233             unsigned int width, unsigned int height,
00234             TextureType texType, PixelFormat internalFormat, 
00235             const NameValuePairList *miscParams ) 
00236             : RenderTexture( name, width, height, texType, internalFormat )
00237         {
00238             mPrivateTex = TextureManager::getSingleton().createManual
00239                 (mName + "_PRIVATE##", ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, 
00240                 texType, mWidth, mHeight, 0, internalFormat, TU_RENDERTARGET );
00241             mPrivateTex->load();
00242         }
00243         
00244         ~D3D9RenderTexture()
00245         {
00246             mPrivateTex->unload();
00247             TextureManager::getSingleton().remove(mPrivateTex->getName());
00248             
00249         }
00250 
00251         virtual void getCustomAttribute( const String& name, void *pData )
00252         {
00253             if( name == "DDBACKBUFFER" )
00254             {
00255                 IDirect3DSurface9 ** pSurf = (IDirect3DSurface9 **)pData;
00256                 if (mPrivateTex->getTextureType() == TEX_TYPE_2D)
00257                     mPrivateTex->getNormTexture()->GetSurfaceLevel( 0, &(*pSurf) );
00258                 else if (mPrivateTex->getTextureType() == TEX_TYPE_CUBE_MAP)
00259                     mPrivateTex->getCubeTexture()->GetCubeMapSurface( (D3DCUBEMAP_FACES)0, 0, &(*pSurf) );
00260                 else
00261                 {
00262                     OGRE_EXCEPT( Exception::UNIMPLEMENTED_FEATURE, 
00263                             "getCustomAttribute is implemented only for 2D and cube textures !!!", 
00264                             "D3D9RenderTexture::getCustomAttribute" );
00265                 }
00266                 // decrement reference count
00267                 (*pSurf)->Release();
00268                 return;
00269             }
00270             else if( name == "D3DZBUFFER" )
00271             {
00272                 IDirect3DSurface9 ** pSurf = (IDirect3DSurface9 **)pData;
00273                 *pSurf = mPrivateTex->getDepthStencil();
00274                 return;
00275             }
00276             else if( name == "DDFRONTBUFFER" )
00277             {
00278                 IDirect3DSurface9 ** pSurf = (IDirect3DSurface9 **)pData;
00279                 if (mPrivateTex->getTextureType() == TEX_TYPE_2D)
00280                     mPrivateTex->getNormTexture()->GetSurfaceLevel( 0, &(*pSurf) );
00281                 else if (mPrivateTex->getTextureType() == TEX_TYPE_CUBE_MAP)
00282                     mPrivateTex->getCubeTexture()->GetCubeMapSurface( (D3DCUBEMAP_FACES)0, 0, &(*pSurf) );
00283                 else
00284                 {
00285                     OGRE_EXCEPT( Exception::UNIMPLEMENTED_FEATURE, 
00286                             "getCustomAttribute is implemented only for 2D and cube textures !!!", 
00287                             "D3D9RenderTexture::getCustomAttribute" );
00288                 }
00289                 (*pSurf)->Release();
00290                 return;
00291             }
00292             else if( name == "HWND" )
00293             {
00294                 HWND *pHwnd = (HWND*)pData;
00295                 *pHwnd = NULL;
00296                 return;
00297             }
00298             else if( name == "isTexture" )
00299             {
00300                 bool *b = reinterpret_cast< bool * >( pData );
00301                 *b = true;
00302                 return;
00303             }
00304         }
00305 
00306         bool requiresTextureFlipping() const { return false; }
00307         virtual void writeContentsToFile( const String & filename ) {}
00308 
00309     protected:
00311         D3D9TexturePtr mPrivateTex;
00312         
00313         virtual void _copyToTexture()
00314         {
00315             // Copy the newly-rendered data to the public texture surface.
00316             mPrivateTex->copyToTexture( mTexture );
00317         }
00318     };
00319 }
00320 
00321 #endif

Copyright © 2000-2005 by The OGRE Team
Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 2.5 License.
Last modified Sun Feb 12 12:59:44 2006