| 1 | /* | 
|---|
| 2 | ----------------------------------------------------------------------------- | 
|---|
| 3 | This source file is part of OGRE | 
|---|
| 4 |     (Object-oriented Graphics Rendering Engine) | 
|---|
| 5 | For the latest info, see http://www.ogre3d.org/ | 
|---|
| 6 |  | 
|---|
| 7 | Copyright (c) 2000-2006 Torus Knot Software Ltd | 
|---|
| 8 | Also see acknowledgements in Readme.html | 
|---|
| 9 |  | 
|---|
| 10 | This program is free software; you can redistribute it and/or modify it under | 
|---|
| 11 | the terms of the GNU Lesser General Public License as published by the Free Software | 
|---|
| 12 | Foundation; either version 2 of the License, or (at your option) any later | 
|---|
| 13 | version. | 
|---|
| 14 |  | 
|---|
| 15 | This program is distributed in the hope that it will be useful, but WITHOUT | 
|---|
| 16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | 
|---|
| 17 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. | 
|---|
| 18 |  | 
|---|
| 19 | You should have received a copy of the GNU Lesser General Public License along with | 
|---|
| 20 | this program; if not, write to the Free Software Foundation, Inc., 59 Temple | 
|---|
| 21 | Place - Suite 330, Boston, MA 02111-1307, USA, or go to | 
|---|
| 22 | http://www.gnu.org/copyleft/lesser.txt. | 
|---|
| 23 |  | 
|---|
| 24 | You may alternatively use this source under the terms of a specific version of | 
|---|
| 25 | the OGRE Unrestricted License provided you have obtained such a license from | 
|---|
| 26 | Torus Knot Software Ltd. | 
|---|
| 27 | ----------------------------------------------------------------------------- | 
|---|
| 28 | */ | 
|---|
| 29 | #ifndef __D3D8TEXTURE_H__ | 
|---|
| 30 | #define __D3D8TEXTURE_H__ | 
|---|
| 31 |  | 
|---|
| 32 | #include "OgreD3D9Prerequisites.h" | 
|---|
| 33 | #include "OgreTexture.h" | 
|---|
| 34 | #include "OgreRenderTexture.h" | 
|---|
| 35 | #include "OgreImage.h" | 
|---|
| 36 | #include "OgreException.h" | 
|---|
| 37 | #include "OgreD3D9HardwarePixelBuffer.h" | 
|---|
| 38 |  | 
|---|
| 39 | #include "OgreNoMemoryMacros.h" | 
|---|
| 40 | #include <d3d9.h> | 
|---|
| 41 | #include <d3dx9.h> | 
|---|
| 42 | #include <dxerr9.h> | 
|---|
| 43 | #include "OgreMemoryMacros.h" | 
|---|
| 44 |  | 
|---|
| 45 | namespace Ogre { | 
|---|
| 46 |         class D3D9Texture : public Texture | 
|---|
| 47 |         { | 
|---|
| 48 |         protected: | 
|---|
| 49 |                 /// D3DDevice pointer | 
|---|
| 50 |                 IDirect3DDevice9                *mpDev;          | 
|---|
| 51 |                 /// D3D9 pointer | 
|---|
| 52 |                 IDirect3D9                              *mpD3D; | 
|---|
| 53 |                 /// 1D/2D normal texture pointer | 
|---|
| 54 |                 IDirect3DTexture9               *mpNormTex;      | 
|---|
| 55 |                 /// cubic texture pointer | 
|---|
| 56 |                 IDirect3DCubeTexture9   *mpCubeTex;      | 
|---|
| 57 |         /// Volume texture | 
|---|
| 58 |         IDirect3DVolumeTexture9 *mpVolumeTex; | 
|---|
| 59 |         /// actual texture pointer | 
|---|
| 60 |                 IDirect3DBaseTexture9   *mpTex;          | 
|---|
| 61 |  | 
|---|
| 62 |                 /// cube texture individual face names | 
|---|
| 63 |                 String                                                  mCubeFaceNames[6]; | 
|---|
| 64 |                 /// device creation parameters | 
|---|
| 65 |                 D3DDEVICE_CREATION_PARAMETERS   mDevCreParams; | 
|---|
| 66 |                 /// back buffer pixel format | 
|---|
| 67 |                 D3DFORMAT                                               mBBPixelFormat; | 
|---|
| 68 |                 /// The memory pool being used | 
|---|
| 69 |                 D3DPOOL                                                 mD3DPool; | 
|---|
| 70 |                 /// device capabilities pointer | 
|---|
| 71 |                 D3DCAPS9                                                mDevCaps; | 
|---|
| 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 |                 /// @copydoc Texture::createInternalResourcesImpl | 
|---|
| 98 |                 void createInternalResourcesImpl(void); | 
|---|
| 99 |                 /// free internal resources | 
|---|
| 100 |                 void freeInternalResourcesImpl(void); | 
|---|
| 101 |                 /// internal method, set Texture class source image protected attributes | 
|---|
| 102 |                 void _setSrcAttributes(unsigned long width, unsigned long height, unsigned long depth, PixelFormat format); | 
|---|
| 103 |                 /// internal method, set Texture class final texture protected attributes | 
|---|
| 104 |                 void _setFinalAttributes(unsigned long width, unsigned long height, unsigned long depth, PixelFormat format); | 
|---|
| 105 |                 /// internal method, return the best by hardware supported filter method | 
|---|
| 106 |                 D3DTEXTUREFILTERTYPE _getBestFilterMethod(); | 
|---|
| 107 |                 /// internal method, return true if the device/texture combination can use dynamic textures | 
|---|
| 108 |                 bool _canUseDynamicTextures(DWORD srcUsage, D3DRESOURCETYPE srcType, D3DFORMAT srcFormat); | 
|---|
| 109 |                 /// internal method, return true if the device/texture combination can auto gen. mip maps | 
|---|
| 110 |                 bool _canAutoGenMipmaps(DWORD srcUsage, D3DRESOURCETYPE srcType, D3DFORMAT srcFormat); | 
|---|
| 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 |  | 
|---|
| 133 |  | 
|---|
| 134 |                 /// @copydoc Texture::getBuffer | 
|---|
| 135 |                 HardwarePixelBufferSharedPtr getBuffer(size_t face, size_t mipmap); | 
|---|
| 136 |                  | 
|---|
| 137 |                 /// retrieves a pointer to the actual texture | 
|---|
| 138 |                 IDirect3DBaseTexture9 *getTexture()  | 
|---|
| 139 |                 { assert(mpTex); return mpTex; } | 
|---|
| 140 |                 /// retrieves a pointer to the normal 1D/2D texture | 
|---|
| 141 |                 IDirect3DTexture9 *getNormTexture() | 
|---|
| 142 |                 { assert(mpNormTex); return mpNormTex; } | 
|---|
| 143 |                 /// retrieves a pointer to the cube texture | 
|---|
| 144 |                 IDirect3DCubeTexture9 *getCubeTexture() | 
|---|
| 145 |                 { assert(mpCubeTex); return mpCubeTex; } | 
|---|
| 146 |                  | 
|---|
| 147 |                  | 
|---|
| 148 |  | 
|---|
| 149 |                 /// For dealing with lost devices - release the resource if in the default pool (and return true) | 
|---|
| 150 |                 bool releaseIfDefaultPool(void); | 
|---|
| 151 |                 /// For dealing with lost devices - recreate the resource if in the default pool (and return true) | 
|---|
| 152 |                 bool recreateIfDefaultPool(LPDIRECT3DDEVICE9 pDev); | 
|---|
| 153 |  | 
|---|
| 154 |     }; | 
|---|
| 155 |  | 
|---|
| 156 |     /** Specialisation of SharedPtr to allow SharedPtr to be assigned to D3D9TexturePtr  | 
|---|
| 157 |     @note Has to be a subclass since we need operator=. | 
|---|
| 158 |     We could templatise this instead of repeating per Resource subclass,  | 
|---|
| 159 |     except to do so requires a form VC6 does not support i.e. | 
|---|
| 160 |     ResourceSubclassPtr<T> : public SharedPtr<T> | 
|---|
| 161 |     */ | 
|---|
| 162 |     class D3D9TexturePtr : public SharedPtr<D3D9Texture>  | 
|---|
| 163 |     { | 
|---|
| 164 |     public: | 
|---|
| 165 |         D3D9TexturePtr() : SharedPtr<D3D9Texture>() {} | 
|---|
| 166 |         explicit D3D9TexturePtr(D3D9Texture* rep) : SharedPtr<D3D9Texture>(rep) {} | 
|---|
| 167 |         D3D9TexturePtr(const D3D9TexturePtr& r) : SharedPtr<D3D9Texture>(r) {}  | 
|---|
| 168 |         D3D9TexturePtr(const ResourcePtr& r) : SharedPtr<D3D9Texture>() | 
|---|
| 169 |         { | 
|---|
| 170 |                         // lock & copy other mutex pointer | 
|---|
| 171 |             OGRE_MUTEX_CONDITIONAL(r.OGRE_AUTO_MUTEX_NAME) | 
|---|
| 172 |             { | 
|---|
| 173 |                             OGRE_LOCK_MUTEX(*r.OGRE_AUTO_MUTEX_NAME) | 
|---|
| 174 |                             OGRE_COPY_AUTO_SHARED_MUTEX(r.OGRE_AUTO_MUTEX_NAME) | 
|---|
| 175 |                 pRep = static_cast<D3D9Texture*>(r.getPointer()); | 
|---|
| 176 |                 pUseCount = r.useCountPointer(); | 
|---|
| 177 |                 if (pUseCount) | 
|---|
| 178 |                 { | 
|---|
| 179 |                     ++(*pUseCount); | 
|---|
| 180 |                 } | 
|---|
| 181 |             } | 
|---|
| 182 |         } | 
|---|
| 183 |                 D3D9TexturePtr(const TexturePtr& r) : SharedPtr<D3D9Texture>() | 
|---|
| 184 |                 { | 
|---|
| 185 |                         *this = r; | 
|---|
| 186 |                 } | 
|---|
| 187 |  | 
|---|
| 188 |         /// Operator used to convert a ResourcePtr to a D3D9TexturePtr | 
|---|
| 189 |         D3D9TexturePtr& operator=(const ResourcePtr& r) | 
|---|
| 190 |         { | 
|---|
| 191 |             if (pRep == static_cast<D3D9Texture*>(r.getPointer())) | 
|---|
| 192 |                 return *this; | 
|---|
| 193 |             release(); | 
|---|
| 194 |                         // lock & copy other mutex pointer | 
|---|
| 195 |             OGRE_MUTEX_CONDITIONAL(r.OGRE_AUTO_MUTEX_NAME) | 
|---|
| 196 |             { | 
|---|
| 197 |                             OGRE_LOCK_MUTEX(*r.OGRE_AUTO_MUTEX_NAME) | 
|---|
| 198 |                             OGRE_COPY_AUTO_SHARED_MUTEX(r.OGRE_AUTO_MUTEX_NAME) | 
|---|
| 199 |                 pRep = static_cast<D3D9Texture*>(r.getPointer()); | 
|---|
| 200 |                 pUseCount = r.useCountPointer(); | 
|---|
| 201 |                 if (pUseCount) | 
|---|
| 202 |                 { | 
|---|
| 203 |                     ++(*pUseCount); | 
|---|
| 204 |                 } | 
|---|
| 205 |             } | 
|---|
| 206 |                         else | 
|---|
| 207 |                         { | 
|---|
| 208 |                                 // RHS must be a null pointer | 
|---|
| 209 |                                 assert(r.isNull() && "RHS must be null if it has no mutex!"); | 
|---|
| 210 |                                 setNull(); | 
|---|
| 211 |                         } | 
|---|
| 212 |             return *this; | 
|---|
| 213 |         } | 
|---|
| 214 |         /// Operator used to convert a TexturePtr to a D3D9TexturePtr | 
|---|
| 215 |         D3D9TexturePtr& operator=(const TexturePtr& r) | 
|---|
| 216 |         { | 
|---|
| 217 |             if (pRep == static_cast<D3D9Texture*>(r.getPointer())) | 
|---|
| 218 |                 return *this; | 
|---|
| 219 |             release(); | 
|---|
| 220 |                         // lock & copy other mutex pointer | 
|---|
| 221 |             OGRE_MUTEX_CONDITIONAL(r.OGRE_AUTO_MUTEX_NAME) | 
|---|
| 222 |             { | 
|---|
| 223 |                             OGRE_LOCK_MUTEX(*r.OGRE_AUTO_MUTEX_NAME) | 
|---|
| 224 |                             OGRE_COPY_AUTO_SHARED_MUTEX(r.OGRE_AUTO_MUTEX_NAME) | 
|---|
| 225 |                 pRep = static_cast<D3D9Texture*>(r.getPointer()); | 
|---|
| 226 |                 pUseCount = r.useCountPointer(); | 
|---|
| 227 |                 if (pUseCount) | 
|---|
| 228 |                 { | 
|---|
| 229 |                     ++(*pUseCount); | 
|---|
| 230 |                 } | 
|---|
| 231 |             } | 
|---|
| 232 |                         else | 
|---|
| 233 |                         { | 
|---|
| 234 |                                 // RHS must be a null pointer | 
|---|
| 235 |                                 assert(r.isNull() && "RHS must be null if it has no mutex!"); | 
|---|
| 236 |                                 setNull(); | 
|---|
| 237 |                         } | 
|---|
| 238 |             return *this; | 
|---|
| 239 |         } | 
|---|
| 240 |     }; | 
|---|
| 241 |  | 
|---|
| 242 |     /// RenderTexture implementation for D3D9 | 
|---|
| 243 |     class D3D9RenderTexture : public RenderTexture | 
|---|
| 244 |     { | 
|---|
| 245 |     public: | 
|---|
| 246 |                 D3D9RenderTexture(const String &name, D3D9HardwarePixelBuffer *buffer): | 
|---|
| 247 |                         RenderTexture(buffer, 0) | 
|---|
| 248 |                 {  | 
|---|
| 249 |                         mName = name; | 
|---|
| 250 |                 } | 
|---|
| 251 |         ~D3D9RenderTexture() {} | 
|---|
| 252 |  | 
|---|
| 253 |         void rebind(D3D9HardwarePixelBuffer *buffer) | 
|---|
| 254 |         { | 
|---|
| 255 |             mBuffer = buffer; | 
|---|
| 256 |             mWidth = mBuffer->getWidth(); | 
|---|
| 257 |             mHeight = mBuffer->getHeight(); | 
|---|
| 258 |             mColourDepth = Ogre::PixelUtil::getNumElemBits(mBuffer->getFormat()); | 
|---|
| 259 |         } | 
|---|
| 260 |  | 
|---|
| 261 |         virtual void update(void); | 
|---|
| 262 |  | 
|---|
| 263 |                 virtual void getCustomAttribute( const String& name, void *pData ) | 
|---|
| 264 |         { | 
|---|
| 265 |                         if(name == "DDBACKBUFFER") | 
|---|
| 266 |             { | 
|---|
| 267 |                 IDirect3DSurface9 ** pSurf = (IDirect3DSurface9 **)pData; | 
|---|
| 268 |                                 *pSurf = static_cast<D3D9HardwarePixelBuffer*>(mBuffer)->getSurface(); | 
|---|
| 269 |                                 return; | 
|---|
| 270 |             } | 
|---|
| 271 |             else if(name == "HWND") | 
|---|
| 272 |             { | 
|---|
| 273 |                 HWND *pHwnd = (HWND*)pData; | 
|---|
| 274 |                 *pHwnd = NULL; | 
|---|
| 275 |                 return; | 
|---|
| 276 |             } | 
|---|
| 277 |                         else if(name == "BUFFER") | 
|---|
| 278 |                         { | 
|---|
| 279 |                                 *static_cast<HardwarePixelBuffer**>(pData) = mBuffer; | 
|---|
| 280 |                                 return; | 
|---|
| 281 |                         } | 
|---|
| 282 |                 } | 
|---|
| 283 |  | 
|---|
| 284 |                 bool requiresTextureFlipping() const { return false; } | 
|---|
| 285 |         }; | 
|---|
| 286 |  | 
|---|
| 287 | } | 
|---|
| 288 |  | 
|---|
| 289 | #endif | 
|---|