BsD3D9Texture.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsD3D9Prerequisites.h"
  5. #include "BsTexture.h"
  6. #include "BsRenderTexture.h"
  7. #include "BsException.h"
  8. #include "BsD3D9PixelBuffer.h"
  9. #include "BsD3D9Resource.h"
  10. namespace BansheeEngine
  11. {
  12. /**
  13. * @brief DirectX 9 implementation of a texture.
  14. */
  15. class BS_D3D9_EXPORT D3D9TextureCore : public TextureCore, public D3D9Resource
  16. {
  17. protected:
  18. /**
  19. * @brief Container for internal resources.
  20. */
  21. struct TextureResources
  22. {
  23. IDirect3DTexture9* pNormTex;
  24. IDirect3DCubeTexture9* pCubeTex;
  25. IDirect3DVolumeTexture9* pVolumeTex;
  26. IDirect3DBaseTexture9* pBaseTex;
  27. IDirect3DSurface9* pMultisampleSurface;
  28. IDirect3DSurface9* pDepthStencilSurface;
  29. };
  30. public:
  31. ~D3D9TextureCore();
  32. /**
  33. * @copydoc Texture::isBindableAsShaderResource
  34. */
  35. bool isBindableAsShaderResource() const override { return mIsBindableAsShaderResource; }
  36. /**
  37. * @brief Returns internal DirectX 9 texture object.
  38. */
  39. IDirect3DBaseTexture9* getTexture_internal();
  40. /**
  41. * @brief Returns internal DirectX 9 texture object pointing to a 1D or 2D texture.
  42. */
  43. IDirect3DTexture9* getNormTexture_internal();
  44. /**
  45. * @brief Returns internal DirectX 9 texture object pointing to a cube texture.
  46. */
  47. IDirect3DCubeTexture9* getCubeTexture_internal();
  48. /**
  49. * @brief Indicates whether the hardware gamma is enabled and supported.
  50. */
  51. bool isHardwareGammaReadToBeUsed() const { return mProperties.isHardwareGammaEnabled() && mHwGammaReadSupported; }
  52. /**
  53. * @brief Returns a hardware pixel buffer for a certain face and level of the texture.
  54. *
  55. * @param face Index of the texture face, if texture has more than one. Array index for
  56. * texture arrays and a cube face for cube textures.
  57. * @param mipmap Index of the mip level. 0 being the largest mip level.
  58. *
  59. * @note Cube face indices: +X (0), -X (1), +Y (2), -Y (3), +Z (4), -Z (5)
  60. */
  61. PixelBufferPtr getBuffer(UINT32 face, UINT32 mipmap);
  62. /**
  63. * @copydoc D3D9Resource::notifyOnDeviceCreate
  64. */
  65. virtual void notifyOnDeviceCreate(IDirect3DDevice9* d3d9Device) override;
  66. /**
  67. * @copydoc D3D9Resource::notifyOnDeviceDestroy
  68. */
  69. virtual void notifyOnDeviceDestroy(IDirect3DDevice9* d3d9Device) override;
  70. /**
  71. * @copydoc D3D9Resource::notifyOnDeviceLost
  72. */
  73. virtual void notifyOnDeviceLost(IDirect3DDevice9* d3d9Device) override;
  74. /**
  75. * @copydoc D3D9Resource::notifyOnDeviceReset
  76. */
  77. virtual void notifyOnDeviceReset(IDirect3DDevice9* d3d9Device) override;
  78. protected:
  79. friend class D3D9TextureCoreManager;
  80. friend class D3D9PixelBuffer;
  81. D3D9TextureCore(TextureType textureType, UINT32 width, UINT32 height, UINT32 depth, UINT32 numMipmaps,
  82. PixelFormat format, int usage, bool hwGamma, UINT32 multisampleCount, const PixelDataPtr& initialData);
  83. /**
  84. * @copydoc TextureCore::initialize
  85. */
  86. void initialize() override;
  87. /**
  88. * @copydoc TextureCore::lock
  89. */
  90. PixelData lockImpl(GpuLockOptions options, UINT32 mipLevel = 0, UINT32 face = 0) override;
  91. /**
  92. * @copydoc TextureCore::unlock
  93. */
  94. void unlockImpl() override;
  95. /**
  96. * @copydoc TextureCore::copy
  97. */
  98. void copyImpl(UINT32 srcFace, UINT32 srcMipLevel, UINT32 destFace, UINT32 destMipLevel, const SPtr<TextureCore>& target) override;
  99. /**
  100. * @copydoc TextureCore::readData
  101. */
  102. void readData(PixelData& dest, UINT32 mipLevel = 0, UINT32 face = 0) override;
  103. /**
  104. * @copydoc TextureCore::writeData
  105. */
  106. void writeData(const PixelData& src, UINT32 mipLevel = 0, UINT32 face = 0, bool discardWholeBuffer = false) override;
  107. /**
  108. * @brief Returns true if the texture should be allocated in the default pool.
  109. */
  110. bool useDefaultPool();
  111. /**
  112. * @brief Creates a DirectX object for blank normal 1D/2D texture.
  113. */
  114. void createNormTex(IDirect3DDevice9* d3d9Device);
  115. /**
  116. * @brief Creates a DirectX object for blank cube texture.
  117. */
  118. void createCubeTex(IDirect3DDevice9* d3d9Device);
  119. /**
  120. * @brief Creates a DirectX object for blank volume (3D) texture.
  121. */
  122. void createVolumeTex(IDirect3DDevice9* d3d9Device);
  123. /**
  124. * @brief Selects a DirectX 9 pixel format depending on requested format
  125. * and device capabilities.
  126. */
  127. D3DFORMAT chooseD3DFormat(IDirect3DDevice9* d3d9Device);
  128. /**
  129. * @copydoc Texture::calculateSize
  130. */
  131. UINT32 calculateSize() const;
  132. /**
  133. * @brief Creates internal resources for the texture for the specified device.
  134. */
  135. void createInternalResources(IDirect3DDevice9* d3d9Device);
  136. /**
  137. * @brief Updates texture attributes with final attributes provided by the API after
  138. * the texture has been created.
  139. */
  140. void setFinalAttributes(IDirect3DDevice9* d3d9Device, TextureResources* textureResources,
  141. UINT32 width, UINT32 height, UINT32 depth, PixelFormat format);
  142. /**
  143. * @brief Returns best texture filtering method.
  144. */
  145. D3DTEXTUREFILTERTYPE getBestFilterMethod(IDirect3DDevice9* d3d9Device);
  146. /**
  147. * @brief Returns if the specified combination of texture type, format and usage supports dynamic texture usage.
  148. */
  149. bool canUseDynamicTextures(IDirect3DDevice9* d3d9Device, DWORD srcUsage, D3DRESOURCETYPE srcType, D3DFORMAT srcFormat);
  150. /**
  151. * @brief Returns if the specified combination of texture type, format and usage supports automatic mipmap generation.
  152. */
  153. bool canAutoGenMipmaps(IDirect3DDevice9* d3d9Device, DWORD srcUsage, D3DRESOURCETYPE srcType, D3DFORMAT srcFormat);
  154. /**
  155. * @brief Returns if the specified combination of texture type, format and usage supports hardware gamma.
  156. */
  157. bool canUseHardwareGammaCorrection(IDirect3DDevice9* d3d9Device, DWORD srcUsage, D3DRESOURCETYPE srcType, D3DFORMAT srcFormat, bool forwriting);
  158. /**
  159. * @brief Creates a list of pixel buffers for all surfaces in the texture. This must be called after the texture
  160. * has been created.
  161. */
  162. void createSurfaceList(IDirect3DDevice9* d3d9Device, TextureResources* textureResources);
  163. /**
  164. * @brief Retrieves texture resources for the device, or null if they don't exist.
  165. */
  166. TextureResources* getTextureResources(IDirect3DDevice9* d3d9Device);
  167. /**
  168. * @brief Allocates a new set of texture resources for the device.
  169. */
  170. TextureResources* allocateTextureResources(IDirect3DDevice9* d3d9Device);
  171. /**
  172. * @brief Frees all the given texture resources.
  173. */
  174. void freeTextureResources(IDirect3DDevice9* d3d9Device, TextureResources* textureResources);
  175. /**
  176. * @brief Determines the pool in which to create the texture in.
  177. */
  178. void determinePool();
  179. protected:
  180. Map<IDirect3DDevice9*, TextureResources*> mMapDeviceToTextureResources;
  181. Vector<PixelBufferPtr> mSurfaceList;
  182. D3DPOOL mD3DPool;
  183. bool mDynamicTextures;
  184. bool mIsBindableAsShaderResource;
  185. PixelBufferPtr mLockedBuffer;
  186. bool mHwGammaReadSupported;
  187. bool mHwGammaWriteSupported;
  188. D3DMULTISAMPLE_TYPE mMultisampleType;
  189. DWORD mMultisampleQuality;
  190. };
  191. }