source: OGRE/trunk/ogre_changes/RenderSystems/Direct3D9/include/OgreD3D9Texture.h @ 657

Revision 657, 12.3 KB checked in by mattausch, 19 years ago (diff)

added ogre dependencies and patched ogre sources

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                // Dynamic textures?
71                bool                            mDynamicTextures;
72                /// Vector of pointers to subsurfaces
73                typedef std::vector<HardwarePixelBufferSharedPtr> SurfaceList;
74                SurfaceList                                             mSurfaceList;
75       
76        /// Initialise the device and get formats
77        void _initDevice(void);
78                /// internal method, load a cube texture
79                void _loadCubeTex();
80                /// internal method, load a normal texture
81                void _loadNormTex();
82                /// internal method, load a volume texture
83                void _loadVolumeTex();
84
85                /// internal method, create a blank normal 1D/2D texture
86                void _createNormTex();
87                /// internal method, create a blank cube texture
88                void _createCubeTex();
89                /// internal method, create a blank cube texture
90                void _createVolumeTex();
91
92                /// internal method, return a D3D pixel format for texture creation
93                D3DFORMAT _chooseD3DFormat();
94
95                /// @copydoc Texture::createInternalResourcesImpl
96                void createInternalResourcesImpl(void);
97                /// free internal resources
98                void freeInternalResourcesImpl(void);
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(void);
119
120        /// overriden from Resource
121        void loadImpl();
122        public:
123                /// constructor
124        D3D9Texture(ResourceManager* creator, const String& name, ResourceHandle handle,
125            const String& group, bool isManual, ManualResourceLoader* loader,
126            IDirect3DDevice9 *pD3DDevice);
127                /// destructor
128                ~D3D9Texture();
129
130                /// overriden from Texture
131                void copyToTexture( TexturePtr& target );
132                /// overriden from Texture
133                void loadImage( const Image &img );
134
135
136                /// @copydoc Texture::getBuffer
137                HardwarePixelBufferSharedPtr getBuffer(size_t face, size_t mipmap);
138               
139                /// retrieves a pointer to the actual texture
140                IDirect3DBaseTexture9 *getTexture()
141                { assert(mpTex); return mpTex; }
142                /// retrieves a pointer to the normal 1D/2D texture
143                IDirect3DTexture9 *getNormTexture()
144                { assert(mpNormTex); return mpNormTex; }
145                /// retrieves a pointer to the cube texture
146                IDirect3DCubeTexture9 *getCubeTexture()
147                { assert(mpCubeTex); return mpCubeTex; }
148                /// retrieves a pointer to the Depth stencil
149                IDirect3DSurface9 *getDepthStencil()
150                { assert(mpZBuff); return mpZBuff; }
151               
152                /// utility method, convert D3D9 pixel format to Ogre pixel format
153                static PixelFormat _getPF(D3DFORMAT d3dPF);
154                /// utility method, convert Ogre pixel format to D3D9 pixel format
155                static D3DFORMAT _getPF(PixelFormat ogrePF);
156                /// utility method, find closest Ogre pixel format that D3D9 can support
157                static PixelFormat _getClosestSupportedPF(PixelFormat ogrePF);
158
159                /// For dealing with lost devices - release the resource if in the default pool (and return true)
160                bool releaseIfDefaultPool(void);
161                /// For dealing with lost devices - recreate the resource if in the default pool (and return true)
162                bool recreateIfDefaultPool(LPDIRECT3DDEVICE9 pDev);
163
164    };
165
166    /** Specialisation of SharedPtr to allow SharedPtr to be assigned to D3D9TexturePtr
167    @note Has to be a subclass since we need operator=.
168    We could templatise this instead of repeating per Resource subclass,
169    except to do so requires a form VC6 does not support i.e.
170    ResourceSubclassPtr<T> : public SharedPtr<T>
171    */
172    class D3D9TexturePtr : public SharedPtr<D3D9Texture>
173    {
174    public:
175        D3D9TexturePtr() : SharedPtr<D3D9Texture>() {}
176        explicit D3D9TexturePtr(D3D9Texture* rep) : SharedPtr<D3D9Texture>(rep) {}
177        D3D9TexturePtr(const D3D9TexturePtr& r) : SharedPtr<D3D9Texture>(r) {}
178        D3D9TexturePtr(const ResourcePtr& r) : SharedPtr<D3D9Texture>()
179        {
180                        // lock & copy other mutex pointer
181                        OGRE_LOCK_MUTEX(*r.OGRE_AUTO_MUTEX_NAME)
182                        OGRE_COPY_AUTO_SHARED_MUTEX(r.OGRE_AUTO_MUTEX_NAME)
183            pRep = static_cast<D3D9Texture*>(r.getPointer());
184            pUseCount = r.useCountPointer();
185            if (pUseCount)
186            {
187                ++(*pUseCount);
188            }
189        }
190
191        /// Operator used to convert a ResourcePtr to a D3D9TexturePtr
192        D3D9TexturePtr& operator=(const ResourcePtr& r)
193        {
194            if (pRep == static_cast<D3D9Texture*>(r.getPointer()))
195                return *this;
196            release();
197                        // lock & copy other mutex pointer
198                        OGRE_LOCK_MUTEX(*r.OGRE_AUTO_MUTEX_NAME)
199                        OGRE_COPY_AUTO_SHARED_MUTEX(r.OGRE_AUTO_MUTEX_NAME)
200            pRep = static_cast<D3D9Texture*>(r.getPointer());
201            pUseCount = r.useCountPointer();
202            if (pUseCount)
203            {
204                ++(*pUseCount);
205            }
206            return *this;
207        }
208        /// Operator used to convert a TexturePtr to a D3D9TexturePtr
209        D3D9TexturePtr& operator=(const TexturePtr& r)
210        {
211            if (pRep == static_cast<D3D9Texture*>(r.getPointer()))
212                return *this;
213            release();
214                        // lock & copy other mutex pointer
215                        OGRE_LOCK_MUTEX(*r.OGRE_AUTO_MUTEX_NAME)
216                        OGRE_COPY_AUTO_SHARED_MUTEX(r.OGRE_AUTO_MUTEX_NAME)
217            pRep = static_cast<D3D9Texture*>(r.getPointer());
218            pUseCount = r.useCountPointer();
219            if (pUseCount)
220            {
221                ++(*pUseCount);
222            }
223            return *this;
224        }
225    };
226
227
228    /// RenderTexture implementation for D3D9
229    class D3D9RenderTexture : public RenderTexture
230    {
231    public:
232        D3D9RenderTexture( const String & name,
233                        unsigned int width, unsigned int height,
234                        TextureType texType, PixelFormat internalFormat,
235                        const NameValuePairList *miscParams )
236                        : RenderTexture( name, width, height, texType, internalFormat )
237        {
238            mPrivateTex = TextureManager::getSingleton().createManual
239                (mName + "_PRIVATE##", ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
240                texType, mWidth, mHeight, 0, internalFormat, TU_RENDERTARGET );
241                        mPrivateTex->load();
242        }
243               
244        ~D3D9RenderTexture()
245        {
246                        mPrivateTex->unload();
247                        TextureManager::getSingleton().remove(mPrivateTex->getName());
248                       
249        }
250
251                virtual void getCustomAttribute( const String& name, void *pData )
252        {
253                        if( name == "DDBACKBUFFER" )
254            {
255                IDirect3DSurface9 ** pSurf = (IDirect3DSurface9 **)pData;
256                                if (mPrivateTex->getTextureType() == TEX_TYPE_2D)
257                                        mPrivateTex->getNormTexture()->GetSurfaceLevel( 0, &(*pSurf) );
258                                else if (mPrivateTex->getTextureType() == TEX_TYPE_CUBE_MAP)
259                                        mPrivateTex->getCubeTexture()->GetCubeMapSurface( (D3DCUBEMAP_FACES)0, 0, &(*pSurf) );
260                                else
261                                {
262                                        OGRE_EXCEPT( Exception::UNIMPLEMENTED_FEATURE,
263                                                        "getCustomAttribute is implemented only for 2D and cube textures !!!",
264                                                        "D3D9RenderTexture::getCustomAttribute" );
265                                }
266                                // decrement reference count
267                (*pSurf)->Release();
268                return;
269            }
270            else if( name == "D3DZBUFFER" )
271            {
272                IDirect3DSurface9 ** pSurf = (IDirect3DSurface9 **)pData;
273                *pSurf = mPrivateTex->getDepthStencil();
274                return;
275            }
276            else if( name == "DDFRONTBUFFER" )
277            {
278                IDirect3DSurface9 ** pSurf = (IDirect3DSurface9 **)pData;
279                                if (mPrivateTex->getTextureType() == TEX_TYPE_2D)
280                                        mPrivateTex->getNormTexture()->GetSurfaceLevel( 0, &(*pSurf) );
281                                else if (mPrivateTex->getTextureType() == TEX_TYPE_CUBE_MAP)
282                                        mPrivateTex->getCubeTexture()->GetCubeMapSurface( (D3DCUBEMAP_FACES)0, 0, &(*pSurf) );
283                                else
284                                {
285                                        OGRE_EXCEPT( Exception::UNIMPLEMENTED_FEATURE,
286                                                        "getCustomAttribute is implemented only for 2D and cube textures !!!",
287                                                        "D3D9RenderTexture::getCustomAttribute" );
288                                }
289                (*pSurf)->Release();
290                return;
291            }
292            else if( name == "HWND" )
293            {
294                HWND *pHwnd = (HWND*)pData;
295                *pHwnd = NULL;
296                return;
297            }
298            else if( name == "isTexture" )
299            {
300                bool *b = reinterpret_cast< bool * >( pData );
301                *b = true;
302                return;
303            }
304        }
305
306                bool requiresTextureFlipping() const { return false; }
307        virtual void writeContentsToFile( const String & filename ) {}
308
309#ifdef GTP_VISIBILITY_MODIFIED_OGRE
310                uchar *getBufferContents(int &dimx, int &dimy) { dimx = dimy = 0; return NULL; };
311#endif // GTP_VISIBILITY_MODIFIED_OGRE
312
313    protected:
314        /// The texture to which rendering takes place.
315        D3D9TexturePtr mPrivateTex;
316               
317        virtual void _copyToTexture()
318        {
319            // Copy the newly-rendered data to the public texture surface.
320            mPrivateTex->copyToTexture( mTexture );
321        }
322    };
323}
324
325#endif
Note: See TracBrowser for help on using the repository browser.