2
0

CmD3D9Texture.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 "CmD3D9PixelBuffer.h"
  31. #include "CmD3D9Resource.h"
  32. namespace CamelotFramework {
  33. class CM_D3D9_EXPORT D3D9Texture : public Texture, public D3D9Resource
  34. {
  35. public:
  36. ~D3D9Texture();
  37. /**
  38. * @copydoc Texture::isBindableAsShaderResource
  39. */
  40. bool isBindableAsShaderResource() const { return mIsBindableAsShaderResource; }
  41. /// retrieves a pointer to the actual texture
  42. IDirect3DBaseTexture9 *getTexture_internal();
  43. /// retrieves a pointer to the normal 1D/2D texture
  44. IDirect3DTexture9 *getNormTexture_internal();
  45. /// retrieves a pointer to the cube texture
  46. IDirect3DCubeTexture9 *getCubeTexture_internal();
  47. /** Indicates whether the hardware gamma is actually enabled and supported.
  48. @remarks
  49. Because hardware gamma might not actually be supported, we need to
  50. ignore it sometimes. Because D3D doesn't encode sRGB in the format but
  51. as a sampler state, and we don't want to change the original requested
  52. hardware gamma flag (e.g. serialisation) we need another indicator.
  53. */
  54. bool isHardwareGammaReadToBeUsed() const { return mHwGamma && mHwGammaReadSupported; }
  55. /// Will this texture need to be in the default pool?
  56. bool useDefaultPool();
  57. /** Return hardware pixel buffer for a surface. This buffer can then
  58. be used to copy data from and to a particular level of the texture.
  59. @param face Face number, in case of a cubemap texture. Must be 0
  60. for other types of textures.
  61. For cubemaps, this is one of
  62. +X (0), -X (1), +Y (2), -Y (3), +Z (4), -Z (5)
  63. @param mipmap Mipmap level. This goes from 0 for the first, largest
  64. mipmap level to getNumMipmaps()-1 for the smallest.
  65. @returns A shared pointer to a hardware pixel buffer
  66. @remarks The buffer is invalidated when the resource is unloaded or destroyed.
  67. Do not use it after the lifetime of the containing texture.
  68. */
  69. PixelBufferPtr getBuffer(UINT32 face, UINT32 mipmap);
  70. // Called immediately after the Direct3D device has been created.
  71. virtual void notifyOnDeviceCreate(IDirect3DDevice9* d3d9Device);
  72. // Called before the Direct3D device is going to be destroyed.
  73. virtual void notifyOnDeviceDestroy(IDirect3DDevice9* d3d9Device);
  74. // Called immediately after the Direct3D device has entered a lost state.
  75. virtual void notifyOnDeviceLost(IDirect3DDevice9* d3d9Device);
  76. // Called immediately after the Direct3D device has been reset
  77. virtual void notifyOnDeviceReset(IDirect3DDevice9* d3d9Device);
  78. protected:
  79. friend class D3D9TextureManager;
  80. friend class D3D9PixelBuffer;
  81. struct TextureResources
  82. {
  83. /// 1D/2D normal texture pointer
  84. IDirect3DTexture9* pNormTex;
  85. /// cubic texture pointer
  86. IDirect3DCubeTexture9* pCubeTex;
  87. /// Volume texture
  88. IDirect3DVolumeTexture9* pVolumeTex;
  89. /// actual texture pointer
  90. IDirect3DBaseTexture9* pBaseTex;
  91. /// Optional FSAA surface
  92. IDirect3DSurface9* pFSAASurface;
  93. /// Optional depth stencil surface
  94. IDirect3DSurface9* pDepthStencilSurface;
  95. };
  96. typedef Map<IDirect3DDevice9*, TextureResources*>::type DeviceToTextureResourcesMap;
  97. typedef DeviceToTextureResourcesMap::iterator DeviceToTextureResourcesIterator;
  98. /// Map between device to texture resources.
  99. DeviceToTextureResourcesMap mMapDeviceToTextureResources;
  100. /// Vector of pointers to subsurfaces
  101. typedef Vector<PixelBufferPtr>::type SurfaceList;
  102. SurfaceList mSurfaceList;
  103. /// The memory pool being used
  104. D3DPOOL mD3DPool;
  105. // Dynamic textures?
  106. bool mDynamicTextures;
  107. bool mIsBindableAsShaderResource;
  108. PixelBufferPtr mLockedBuffer;
  109. /// Is hardware gamma supported (read)?
  110. bool mHwGammaReadSupported;
  111. /// Is hardware gamma supported (write)?
  112. bool mHwGammaWriteSupported;
  113. D3DMULTISAMPLE_TYPE mFSAAType;
  114. DWORD mFSAAQuality;
  115. D3D9Texture();
  116. /**
  117. * @copydoc Texture::initialize_internal()
  118. */
  119. void initialize_internal();
  120. /**
  121. * @copydoc Texture::destroy_internal()
  122. */
  123. void destroy_internal();
  124. /**
  125. * @copydoc Texture::lock
  126. */
  127. PixelData lockImpl(GpuLockOptions options, UINT32 mipLevel = 0, UINT32 face = 0);
  128. /**
  129. * @copydoc Texture::unlock
  130. */
  131. void unlockImpl();
  132. /**
  133. * @copydoc Texture::copy
  134. */
  135. void copyImpl(TexturePtr& target);
  136. /**
  137. * @copydoc Texture::readData
  138. */
  139. void readData(PixelData& dest, UINT32 mipLevel = 0, UINT32 face = 0);
  140. /**
  141. * @copydoc Texture::writeData
  142. */
  143. void writeData(const PixelData& src, UINT32 mipLevel = 0, UINT32 face = 0, bool discardWholeBuffer = false);
  144. /// internal method, create a blank normal 1D/2D texture
  145. void _createNormTex(IDirect3DDevice9* d3d9Device);
  146. /// internal method, create a blank cube texture
  147. void _createCubeTex(IDirect3DDevice9* d3d9Device);
  148. /// internal method, create a blank cube texture
  149. void _createVolumeTex(IDirect3DDevice9* d3d9Device);
  150. /// internal method, return a D3D pixel format for texture creation
  151. D3DFORMAT _chooseD3DFormat(IDirect3DDevice9* d3d9Device);
  152. /// @copydoc Resource::calculateSize
  153. UINT32 calculateSize(void) const;
  154. /// Creates this texture resources on the specified device.
  155. void createInternalResources(IDirect3DDevice9* d3d9Device);
  156. /// internal method, set Texture class final texture protected attributes
  157. void _setFinalAttributes(IDirect3DDevice9* d3d9Device, TextureResources* textureResources,
  158. unsigned long width, unsigned long height, unsigned long depth, PixelFormat format);
  159. /// internal method, return the best by hardware supported filter method
  160. D3DTEXTUREFILTERTYPE _getBestFilterMethod(IDirect3DDevice9* d3d9Device);
  161. /// internal method, return true if the device/texture combination can use dynamic textures
  162. bool _canUseDynamicTextures(IDirect3DDevice9* d3d9Device, DWORD srcUsage, D3DRESOURCETYPE srcType, D3DFORMAT srcFormat);
  163. /// internal method, return true if the device/texture combination can auto gen. mip maps
  164. bool _canAutoGenMipmaps(IDirect3DDevice9* d3d9Device, DWORD srcUsage, D3DRESOURCETYPE srcType, D3DFORMAT srcFormat);
  165. /// internal method, return true if the device/texture combination can use hardware gamma
  166. bool _canUseHardwareGammaCorrection(IDirect3DDevice9* d3d9Device, DWORD srcUsage, D3DRESOURCETYPE srcType, D3DFORMAT srcFormat, bool forwriting);
  167. /// internal method, create D3D9HardwarePixelBuffers for every face and
  168. /// mipmap level. This method must be called after the D3D texture object was created
  169. void _createSurfaceList(IDirect3DDevice9* d3d9Device, TextureResources* textureResources);
  170. /// gets the texture resources attached to the given device.
  171. TextureResources* getTextureResources(IDirect3DDevice9* d3d9Device);
  172. /// allocates new texture resources structure attached to the given device.
  173. TextureResources* allocateTextureResources(IDirect3DDevice9* d3d9Device);
  174. /// creates this texture resources according to the current settings.
  175. void createTextureResources(IDirect3DDevice9* d3d9Device);
  176. /// frees the given texture resources.
  177. void freeTextureResources(IDirect3DDevice9* d3d9Device, TextureResources* textureResources);
  178. void determinePool();
  179. };
  180. }
  181. #endif