BsD3D9Texture.h 6.8 KB

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