BsD3D9Texture.h 6.8 KB

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