source: trunk/VUT/work/ogre_changes/RenderSystems/Direct3D9/include/OgreD3D9Texture.h @ 193

Revision 193, 12.5 KB checked in by mattausch, 19 years ago (diff)

changed to ogre 103
added readme

Line 
1/*
2-----------------------------------------------------------------------------
3This source file is part of OGRE
4    (Object-oriented Graphics Rendering Engine)
5For the latest info, see http://www.ogre3d.org/
6
7Copyright (c) 2000-2005 The OGRE Team
8Also see acknowledgements in Readme.html
9
10This program is free software; you can redistribute it and/or modify it under
11the terms of the GNU Lesser General Public License as published by the Free Software
12Foundation; either version 2 of the License, or (at your option) any later
13version.
14
15This program is distributed in the hope that it will be useful, but WITHOUT
16ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
18
19You should have received a copy of the GNU Lesser General Public License along with
20this program; if not, write to the Free Software Foundation, Inc., 59 Temple
21Place - Suite 330, Boston, MA 02111-1307, USA, or go to
22http://www.gnu.org/copyleft/lesser.txt.
23-----------------------------------------------------------------------------
24*/
25#ifndef __D3D8TEXTURE_H__
26#define __D3D8TEXTURE_H__
27
28#include "OgreD3D9Prerequisites.h"
29#include "OgreTexture.h"
30#include "OgreRenderTexture.h"
31#include "OgreImage.h"
32#include "OgreException.h"
33#include "OgreD3D9HardwarePixelBuffer.h"
34
35#include "OgreNoMemoryMacros.h"
36#include <d3d9.h>
37#include <d3dx9.h>
38#include <dxerr9.h>
39#include "OgreMemoryMacros.h"
40
41namespace Ogre {
42        class D3D9Texture : public Texture
43        {
44        protected:
45                /// D3DDevice pointer
46                IDirect3DDevice9                *mpDev;         
47                /// D3D9 pointer
48                IDirect3D9                              *mpD3D;
49                /// 1D/2D normal texture pointer
50                IDirect3DTexture9               *mpNormTex;     
51                /// cubic texture pointer
52                IDirect3DCubeTexture9   *mpCubeTex;     
53        /// Volume texture
54        IDirect3DVolumeTexture9 *mpVolumeTex;
55        /// z-buffer for the render surface pointer
56                IDirect3DSurface9               *mpZBuff;       
57                /// actual texture pointer
58                IDirect3DBaseTexture9   *mpTex;         
59
60                /// cube texture individual face names
61                String                                                  mCubeFaceNames[6];
62                /// device creation parameters
63                D3DDEVICE_CREATION_PARAMETERS   mDevCreParams;
64                /// back buffer pixel format
65                D3DFORMAT                                               mBBPixelFormat;
66                /// The memory pool being used
67                D3DPOOL                                                 mD3DPool;
68                /// device capabilities pointer
69                D3DCAPS9                                                mDevCaps;
70        // Auto-generated mipmaps?
71        bool                            mAutoGenMipmaps;
72                // Dynamic textures?
73                bool                            mDynamicTextures;
74                /// Vector of pointers to subsurfaces
75                typedef std::vector<HardwarePixelBufferSharedPtr> SurfaceList;
76                SurfaceList                                             mSurfaceList;
77       
78        /// Initialise the device and get formats
79        void _initDevice(void);
80                /// internal method, load a cube texture
81                void _loadCubeTex();
82                /// internal method, load a normal texture
83                void _loadNormTex();
84                /// internal method, load a volume texture
85                void _loadVolumeTex();
86
87                /// internal method, create a blank normal 1D/2D texture
88                void _createNormTex();
89                /// internal method, create a blank cube texture
90                void _createCubeTex();
91                /// internal method, create a blank cube texture
92                void _createVolumeTex();
93
94                /// internal method, return a D3D pixel format for texture creation
95                D3DFORMAT _chooseD3DFormat();
96
97                /// internal method, free D3D9 resources
98                void _freeResources();
99                /// internal method, set Texture class source image protected attributes
100                void _setSrcAttributes(unsigned long width, unsigned long height, unsigned long depth, PixelFormat format);
101                /// internal method, set Texture class final texture protected attributes
102                void _setFinalAttributes(unsigned long width, unsigned long height, unsigned long depth, PixelFormat format);
103                /// internal method, return the best by hardware supported filter method
104                D3DTEXTUREFILTERTYPE _getBestFilterMethod();
105                /// internal method, return true if the device/texture combination can use dynamic textures
106                bool _canUseDynamicTextures(DWORD srcUsage, D3DRESOURCETYPE srcType, D3DFORMAT srcFormat);
107                /// internal method, return true if the device/texture combination can auto gen. mip maps
108                bool _canAutoGenMipmaps(DWORD srcUsage, D3DRESOURCETYPE srcType, D3DFORMAT srcFormat);
109                /// internal method, create a depth stencil for the render target texture
110                void _createDepthStencil();
111
112                /// internal method, the cube map face name for the spec. face index
113                String _getCubeFaceName(unsigned char face) const
114                { assert(face < 6); return mCubeFaceNames[face]; }
115               
116                /// internal method, create D3D9HardwarePixelBuffers for every face and
117                /// mipmap level. This method must be called after the D3D texture object was created
118                void _createSurfaceList(bool updateOldList=false);
119
120        /// overriden from Resource
121        void loadImpl();
122        /// overriden from Resource
123        void unloadImpl();
124        public:
125                /// constructor
126        D3D9Texture(ResourceManager* creator, const String& name, ResourceHandle handle,
127            const String& group, bool isManual, ManualResourceLoader* loader,
128            IDirect3DDevice9 *pD3DDevice);
129                /// destructor
130                ~D3D9Texture();
131
132                /// overriden from Texture
133                void copyToTexture( TexturePtr& target );
134                /// overriden from Texture
135                void loadImage( const Image &img );
136
137        /// @copydoc Texture::createInternalResources
138        void createInternalResources(void);
139
140                /// @copydoc Texture::getBuffer
141                HardwarePixelBufferSharedPtr getBuffer(size_t face, size_t mipmap);
142               
143                /// retrieves a pointer to the actual texture
144                IDirect3DBaseTexture9 *getTexture()
145                { assert(mpTex); return mpTex; }
146                /// retrieves a pointer to the normal 1D/2D texture
147                IDirect3DTexture9 *getNormTexture()
148                { assert(mpNormTex); return mpNormTex; }
149                /// retrieves a pointer to the cube texture
150                IDirect3DCubeTexture9 *getCubeTexture()
151                { assert(mpCubeTex); return mpCubeTex; }
152                /// retrieves a pointer to the Depth stencil
153                IDirect3DSurface9 *getDepthStencil()
154                { assert(mpZBuff); return mpZBuff; }
155               
156                /// utility method, convert D3D9 pixel format to Ogre pixel format
157                static PixelFormat _getPF(D3DFORMAT d3dPF);
158                /// utility method, convert Ogre pixel format to D3D9 pixel format
159                static D3DFORMAT _getPF(PixelFormat ogrePF);
160                /// utility method, find closest Ogre pixel format that D3D9 can support
161                static PixelFormat _getClosestSupportedPF(PixelFormat ogrePF);
162
163                /// For dealing with lost devices - release the resource if in the default pool (and return true)
164                bool releaseIfDefaultPool(void);
165                /// For dealing with lost devices - recreate the resource if in the default pool (and return true)
166                bool recreateIfDefaultPool(LPDIRECT3DDEVICE9 pDev);
167
168    };
169
170    /** Specialisation of SharedPtr to allow SharedPtr to be assigned to D3D9TexturePtr
171    @note Has to be a subclass since we need operator=.
172    We could templatise this instead of repeating per Resource subclass,
173    except to do so requires a form VC6 does not support i.e.
174    ResourceSubclassPtr<T> : public SharedPtr<T>
175    */
176    class D3D9TexturePtr : public SharedPtr<D3D9Texture>
177    {
178    public:
179        D3D9TexturePtr() : SharedPtr<D3D9Texture>() {}
180        explicit D3D9TexturePtr(D3D9Texture* rep) : SharedPtr<D3D9Texture>(rep) {}
181        D3D9TexturePtr(const D3D9TexturePtr& r) : SharedPtr<D3D9Texture>(r) {}
182        D3D9TexturePtr(const ResourcePtr& r) : SharedPtr<D3D9Texture>()
183        {
184                        // lock & copy other mutex pointer
185                        OGRE_LOCK_MUTEX(*r.OGRE_AUTO_MUTEX_NAME)
186                        OGRE_COPY_AUTO_SHARED_MUTEX(r.OGRE_AUTO_MUTEX_NAME)
187            pRep = static_cast<D3D9Texture*>(r.getPointer());
188            pUseCount = r.useCountPointer();
189            if (pUseCount)
190            {
191                ++(*pUseCount);
192            }
193        }
194
195        /// Operator used to convert a ResourcePtr to a D3D9TexturePtr
196        D3D9TexturePtr& operator=(const ResourcePtr& r)
197        {
198            if (pRep == static_cast<D3D9Texture*>(r.getPointer()))
199                return *this;
200            release();
201                        // lock & copy other mutex pointer
202                        OGRE_LOCK_MUTEX(*r.OGRE_AUTO_MUTEX_NAME)
203                        OGRE_COPY_AUTO_SHARED_MUTEX(r.OGRE_AUTO_MUTEX_NAME)
204            pRep = static_cast<D3D9Texture*>(r.getPointer());
205            pUseCount = r.useCountPointer();
206            if (pUseCount)
207            {
208                ++(*pUseCount);
209            }
210            return *this;
211        }
212        /// Operator used to convert a TexturePtr to a D3D9TexturePtr
213        D3D9TexturePtr& operator=(const TexturePtr& r)
214        {
215            if (pRep == static_cast<D3D9Texture*>(r.getPointer()))
216                return *this;
217            release();
218                        // lock & copy other mutex pointer
219                        OGRE_LOCK_MUTEX(*r.OGRE_AUTO_MUTEX_NAME)
220                        OGRE_COPY_AUTO_SHARED_MUTEX(r.OGRE_AUTO_MUTEX_NAME)
221            pRep = static_cast<D3D9Texture*>(r.getPointer());
222            pUseCount = r.useCountPointer();
223            if (pUseCount)
224            {
225                ++(*pUseCount);
226            }
227            return *this;
228        }
229    };
230
231
232    /// RenderTexture implementation for D3D9
233    class D3D9RenderTexture : public RenderTexture
234    {
235    public:
236        D3D9RenderTexture( const String & name,
237                        unsigned int width, unsigned int height,
238                        TextureType texType, PixelFormat internalFormat,
239                        const NameValuePairList *miscParams )
240                        : RenderTexture( name, width, height, texType, internalFormat )
241        {
242            mPrivateTex = TextureManager::getSingleton().createManual
243                (mName + "_PRIVATE##", ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
244                texType, mWidth, mHeight, 0, internalFormat, TU_RENDERTARGET );
245                        mPrivateTex->load();
246        }
247               
248        ~D3D9RenderTexture()
249        {
250                        mPrivateTex->unload();
251                        TextureManager::getSingleton().remove(mPrivateTex->getName());
252                       
253        }
254
255                virtual void getCustomAttribute( const String& name, void *pData )
256        {
257                        if( name == "DDBACKBUFFER" )
258            {
259                IDirect3DSurface9 ** pSurf = (IDirect3DSurface9 **)pData;
260                                if (mPrivateTex->getTextureType() == TEX_TYPE_2D)
261                                        mPrivateTex->getNormTexture()->GetSurfaceLevel( 0, &(*pSurf) );
262                                else if (mPrivateTex->getTextureType() == TEX_TYPE_CUBE_MAP)
263                                        mPrivateTex->getCubeTexture()->GetCubeMapSurface( (D3DCUBEMAP_FACES)0, 0, &(*pSurf) );
264                                else
265                                {
266                                        OGRE_EXCEPT( Exception::UNIMPLEMENTED_FEATURE,
267                                                        "getCustomAttribute is implemented only for 2D and cube textures !!!",
268                                                        "D3D9RenderTexture::getCustomAttribute" );
269                                }
270                                // decrement reference count
271                (*pSurf)->Release();
272                return;
273            }
274            else if( name == "D3DZBUFFER" )
275            {
276                IDirect3DSurface9 ** pSurf = (IDirect3DSurface9 **)pData;
277                *pSurf = mPrivateTex->getDepthStencil();
278                return;
279            }
280            else if( name == "DDFRONTBUFFER" )
281            {
282                IDirect3DSurface9 ** pSurf = (IDirect3DSurface9 **)pData;
283                                if (mPrivateTex->getTextureType() == TEX_TYPE_2D)
284                                        mPrivateTex->getNormTexture()->GetSurfaceLevel( 0, &(*pSurf) );
285                                else if (mPrivateTex->getTextureType() == TEX_TYPE_CUBE_MAP)
286                                        mPrivateTex->getCubeTexture()->GetCubeMapSurface( (D3DCUBEMAP_FACES)0, 0, &(*pSurf) );
287                                else
288                                {
289                                        OGRE_EXCEPT( Exception::UNIMPLEMENTED_FEATURE,
290                                                        "getCustomAttribute is implemented only for 2D and cube textures !!!",
291                                                        "D3D9RenderTexture::getCustomAttribute" );
292                                }
293                (*pSurf)->Release();
294                return;
295            }
296            else if( name == "HWND" )
297            {
298                HWND *pHwnd = (HWND*)pData;
299                *pHwnd = NULL;
300                return;
301            }
302            else if( name == "isTexture" )
303            {
304                bool *b = reinterpret_cast< bool * >( pData );
305                *b = true;
306                return;
307            }
308        }
309
310                bool requiresTextureFlipping() const { return false; }
311        virtual void writeContentsToFile( const String & filename ) {}
312
313#ifdef GTP_VISIBILITY_MODIFIED_OGRE
314                uchar *getBufferContents(int &dimx, int &dimy) { dimx = dimy = 0; return NULL; };
315#endif // GTP_VISIBILITY_MODIFIED_OGRE
316
317    protected:
318        /// The texture to which rendering takes place.
319        D3D9TexturePtr mPrivateTex;
320               
321        virtual void _copyToTexture()
322        {
323            // Copy the newly-rendered data to the public texture surface.
324            mPrivateTex->copyToTexture( mTexture );
325        }
326    };
327}
328
329#endif
Note: See TracBrowser for help on using the repository browser.