CmD3D9Texture.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. Copyright (c) 2000-2011 Torus Knot Software Ltd
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. -----------------------------------------------------------------------------
  23. */
  24. #ifndef __D3D9TEXTURE_H__
  25. #define __D3D9TEXTURE_H__
  26. #include "CmD3D9Prerequisites.h"
  27. #include "CmTexture.h"
  28. #include "CmRenderTexture.h"
  29. #include "CmException.h"
  30. #include "CmD3D9HardwarePixelBuffer.h"
  31. #include "CmD3D9Resource.h"
  32. namespace CamelotEngine {
  33. class _OgreD3D9Export D3D9Texture : public Texture, public D3D9Resource
  34. {
  35. protected:
  36. struct TextureResources
  37. {
  38. /// 1D/2D normal texture pointer
  39. IDirect3DTexture9* pNormTex;
  40. /// cubic texture pointer
  41. IDirect3DCubeTexture9* pCubeTex;
  42. /// Volume texture
  43. IDirect3DVolumeTexture9* pVolumeTex;
  44. /// actual texture pointer
  45. IDirect3DBaseTexture9* pBaseTex;
  46. /// Optional FSAA surface
  47. IDirect3DSurface9* pFSAASurface;
  48. };
  49. typedef map<IDirect3DDevice9*, TextureResources*>::type DeviceToTextureResourcesMap;
  50. typedef DeviceToTextureResourcesMap::iterator DeviceToTextureResourcesIterator;
  51. /// Map between device to texture resources.
  52. DeviceToTextureResourcesMap mMapDeviceToTextureResources;
  53. /// Vector of pointers to subsurfaces
  54. typedef vector<HardwarePixelBufferPtr>::type SurfaceList;
  55. SurfaceList mSurfaceList;
  56. /// cube texture individual face names
  57. String mCubeFaceNames[6];
  58. /// The memory pool being used
  59. D3DPOOL mD3DPool;
  60. // Dynamic textures?
  61. bool mDynamicTextures;
  62. /// Is hardware gamma supported (read)?
  63. bool mHwGammaReadSupported;
  64. /// Is hardware gamma supported (write)?
  65. bool mHwGammaWriteSupported;
  66. D3DMULTISAMPLE_TYPE mFSAAType;
  67. DWORD mFSAAQuality;
  68. // TODO PORT - Loading tex from streams not supported
  69. // needed to store data between prepareImpl and loadImpl
  70. //typedef SharedPtr<vector<MemoryDataStreamPtr>::type > LoadedStreams;
  71. /// internal method, load a cube texture
  72. //void _loadCubeTex(IDirect3DDevice9* d3d9Device, const LoadedStreams &loadedStreams);
  73. /// internal method, load a normal texture
  74. //void _loadNormTex(IDirect3DDevice9* d3d9Device, const LoadedStreams &loadedStreams);
  75. /// internal method, load a volume texture
  76. //void _loadVolumeTex(IDirect3DDevice9* d3d9Device, const LoadedStreams &loadedStreams);
  77. /// internal method, prepare a cube texture
  78. //LoadedStreams _prepareCubeTex();
  79. /// internal method, prepare a normal texture
  80. //LoadedStreams _prepareNormTex();
  81. /// internal method, prepare a volume texture
  82. //LoadedStreams _prepareVolumeTex();
  83. /// internal method, create a blank normal 1D/2D texture
  84. void _createNormTex(IDirect3DDevice9* d3d9Device);
  85. /// internal method, create a blank cube texture
  86. void _createCubeTex(IDirect3DDevice9* d3d9Device);
  87. /// internal method, create a blank cube texture
  88. void _createVolumeTex(IDirect3DDevice9* d3d9Device);
  89. /// internal method, return a D3D pixel format for texture creation
  90. D3DFORMAT _chooseD3DFormat(IDirect3DDevice9* d3d9Device);
  91. /// @copydoc Resource::calculateSize
  92. size_t calculateSize(void) const;
  93. /// @copydoc Texture::createInternalResources
  94. void createInternalResources(void);
  95. /// @copydoc Texture::freeInternalResources
  96. void freeInternalResources(void);
  97. /// @copydoc Texture::createInternalResourcesImpl
  98. void createInternalResourcesImpl(void);
  99. /// Creates this texture resources on the specified device.
  100. void createInternalResourcesImpl(IDirect3DDevice9* d3d9Device);
  101. /// free internal resources
  102. void freeInternalResourcesImpl(void);
  103. /// internal method, set Texture class source image protected attributes
  104. void _setSrcAttributes(unsigned long width, unsigned long height, unsigned long depth, PixelFormat format);
  105. /// internal method, set Texture class final texture protected attributes
  106. void _setFinalAttributes(IDirect3DDevice9* d3d9Device, TextureResources* textureResources,
  107. unsigned long width, unsigned long height, unsigned long depth, PixelFormat format);
  108. /// internal method, return the best by hardware supported filter method
  109. D3DTEXTUREFILTERTYPE _getBestFilterMethod(IDirect3DDevice9* d3d9Device);
  110. /// internal method, return true if the device/texture combination can use dynamic textures
  111. bool _canUseDynamicTextures(IDirect3DDevice9* d3d9Device, DWORD srcUsage, D3DRESOURCETYPE srcType, D3DFORMAT srcFormat);
  112. /// internal method, return true if the device/texture combination can auto gen. mip maps
  113. bool _canAutoGenMipmaps(IDirect3DDevice9* d3d9Device, DWORD srcUsage, D3DRESOURCETYPE srcType, D3DFORMAT srcFormat);
  114. /// internal method, return true if the device/texture combination can use hardware gamma
  115. bool _canUseHardwareGammaCorrection(IDirect3DDevice9* d3d9Device, DWORD srcUsage, D3DRESOURCETYPE srcType, D3DFORMAT srcFormat, bool forwriting);
  116. /// internal method, the cube map face name for the spec. face index
  117. String _getCubeFaceName(unsigned char face) const
  118. { assert(face < 6); return mCubeFaceNames[face]; }
  119. /// internal method, create D3D9HardwarePixelBuffers for every face and
  120. /// mipmap level. This method must be called after the D3D texture object was created
  121. void _createSurfaceList(IDirect3DDevice9* d3d9Device, TextureResources* textureResources);
  122. /// overriden from Resource
  123. void loadImpl();
  124. /// Loads this texture into the specified device.
  125. void loadImpl(IDirect3DDevice9* d3d9Device);
  126. /// overriden from Resource
  127. void prepareImpl();
  128. /// overriden from Resource
  129. void unprepareImpl();
  130. /// overriden from Resource
  131. void postLoadImpl();
  132. /// gets the texture resources attached to the given device.
  133. TextureResources* getTextureResources(IDirect3DDevice9* d3d9Device);
  134. /// allocates new texture resources structure attached to the given device.
  135. TextureResources* allocateTextureResources(IDirect3DDevice9* d3d9Device);
  136. /// creates this texture resources according to the current settings.
  137. void createTextureResources(IDirect3DDevice9* d3d9Device);
  138. /// frees the given texture resources.
  139. void freeTextureResources(IDirect3DDevice9* d3d9Device, TextureResources* textureResources);
  140. void determinePool();
  141. /** Vector of pointers to streams that were pulled from disk by
  142. prepareImpl but have yet to be pushed into texture memory
  143. by loadImpl. Should be cleared on load and on unprepare.
  144. */
  145. // TODO PORT - Loading tex from streams not supported
  146. //LoadedStreams mLoadedStreams;
  147. friend class D3D9HardwarePixelBuffer;
  148. public:
  149. /// constructor
  150. D3D9Texture();
  151. /// destructor
  152. ~D3D9Texture();
  153. /// overridden from Texture
  154. void copyToTexture( TexturePtr& target );
  155. /// @copydoc Texture::getBuffer
  156. HardwarePixelBufferPtr getBuffer(size_t face, size_t mipmap);
  157. /// retrieves a pointer to the actual texture
  158. IDirect3DBaseTexture9 *getTexture();
  159. /// retrieves a pointer to the normal 1D/2D texture
  160. IDirect3DTexture9 *getNormTexture();
  161. /// retrieves a pointer to the cube texture
  162. IDirect3DCubeTexture9 *getCubeTexture();
  163. /** Indicates whether the hardware gamma is actually enabled and supported.
  164. @remarks
  165. Because hardware gamma might not actually be supported, we need to
  166. ignore it sometimes. Because D3D doesn't encode sRGB in the format but
  167. as a sampler state, and we don't want to change the original requested
  168. hardware gamma flag (e.g. serialisation) we need another indicator.
  169. */
  170. bool isHardwareGammaReadToBeUsed() const { return mHwGamma && mHwGammaReadSupported; }
  171. /// Will this texture need to be in the default pool?
  172. bool useDefaultPool();
  173. // Called immediately after the Direct3D device has been created.
  174. virtual void notifyOnDeviceCreate(IDirect3DDevice9* d3d9Device);
  175. // Called before the Direct3D device is going to be destroyed.
  176. virtual void notifyOnDeviceDestroy(IDirect3DDevice9* d3d9Device);
  177. // Called immediately after the Direct3D device has entered a lost state.
  178. virtual void notifyOnDeviceLost(IDirect3DDevice9* d3d9Device);
  179. // Called immediately after the Direct3D device has been reset
  180. virtual void notifyOnDeviceReset(IDirect3DDevice9* d3d9Device);
  181. };
  182. typedef std::shared_ptr<D3D9Texture> D3D9TexturePtr;
  183. /// RenderTexture implementation for D3D9
  184. class _OgreD3D9Export D3D9RenderTexture : public RenderTexture
  185. {
  186. public:
  187. D3D9RenderTexture(const String &name, D3D9HardwarePixelBuffer *buffer, bool writeGamma, UINT32 fsaa);
  188. ~D3D9RenderTexture() {}
  189. virtual void update(bool swap);
  190. virtual void getCustomAttribute( const String& name, void *pData );
  191. bool requiresTextureFlipping() const { return false; }
  192. /// Override needed to deal with FSAA
  193. void swapBuffers(bool waitForVSync = true);
  194. };
  195. }
  196. #endif