BsD3D9Texture.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 Texture::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. PixelBufferPtr 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, const PixelDataPtr& initialData);
  62. /** @copydoc TextureCore::initialize */
  63. void initialize() override;
  64. /** @copydoc TextureCore::lock */
  65. PixelData lockImpl(GpuLockOptions options, UINT32 mipLevel = 0, UINT32 face = 0) override;
  66. /** @copydoc TextureCore::unlock */
  67. void unlockImpl() override;
  68. /** @copydoc TextureCore::copy */
  69. void copyImpl(UINT32 srcFace, UINT32 srcMipLevel, UINT32 destFace, UINT32 destMipLevel, const SPtr<TextureCore>& target) override;
  70. /** @copydoc TextureCore::readData */
  71. void readData(PixelData& dest, UINT32 mipLevel = 0, UINT32 face = 0) override;
  72. /** @copydoc TextureCore::writeData */
  73. void writeData(const PixelData& src, UINT32 mipLevel = 0, UINT32 face = 0, bool discardWholeBuffer = false) override;
  74. /** Returns true if the texture should be allocated in the default pool. */
  75. bool useDefaultPool();
  76. /** Creates a DirectX object for blank normal 1D/2D texture. */
  77. void createNormTex(IDirect3DDevice9* d3d9Device);
  78. /** Creates a DirectX object for blank cube texture. */
  79. void createCubeTex(IDirect3DDevice9* d3d9Device);
  80. /** Creates a DirectX object for blank volume (3D) texture. */
  81. void createVolumeTex(IDirect3DDevice9* d3d9Device);
  82. /** Selects a DirectX 9 pixel format depending on requested format and device capabilities. */
  83. D3DFORMAT chooseD3DFormat(IDirect3DDevice9* d3d9Device);
  84. /** @copydoc Texture::calculateSize */
  85. UINT32 calculateSize() const;
  86. /** Creates internal resources for the texture for the specified device. */
  87. void createInternalResources(IDirect3DDevice9* d3d9Device);
  88. /** Updates texture attributes with final attributes provided by the API after the texture has been created. */
  89. void setFinalAttributes(IDirect3DDevice9* d3d9Device, TextureResources* textureResources,
  90. UINT32 width, UINT32 height, UINT32 depth, PixelFormat format);
  91. /** Returns best texture filtering method. */
  92. D3DTEXTUREFILTERTYPE getBestFilterMethod(IDirect3DDevice9* d3d9Device);
  93. /** Returns if the specified combination of texture type, format and usage supports dynamic texture usage. */
  94. bool canUseDynamicTextures(IDirect3DDevice9* d3d9Device, DWORD srcUsage, D3DRESOURCETYPE srcType, D3DFORMAT srcFormat);
  95. /** Returns if the specified combination of texture type, format and usage supports automatic mipmap generation. */
  96. bool canAutoGenMipmaps(IDirect3DDevice9* d3d9Device, DWORD srcUsage, D3DRESOURCETYPE srcType, D3DFORMAT srcFormat);
  97. /** Returns if the specified combination of texture type, format and usage supports hardware gamma. */
  98. bool canUseHardwareGammaCorrection(IDirect3DDevice9* d3d9Device, DWORD srcUsage, D3DRESOURCETYPE srcType, D3DFORMAT srcFormat, bool forwriting);
  99. /**
  100. * Creates a list of pixel buffers for all surfaces in the texture. This must be called after the texture has been
  101. * created.
  102. */
  103. void createSurfaceList(IDirect3DDevice9* d3d9Device, TextureResources* textureResources);
  104. /** Retrieves texture resources for the device, or null if they don't exist. */
  105. TextureResources* getTextureResources(IDirect3DDevice9* d3d9Device);
  106. /** Allocates a new set of texture resources for the device. */
  107. TextureResources* allocateTextureResources(IDirect3DDevice9* d3d9Device);
  108. /** Frees all the given texture resources. */
  109. void freeTextureResources(IDirect3DDevice9* d3d9Device, TextureResources* textureResources);
  110. /** Determines the pool in which to create the texture in. */
  111. void determinePool();
  112. protected:
  113. Map<IDirect3DDevice9*, TextureResources*> mMapDeviceToTextureResources;
  114. Vector<PixelBufferPtr> mSurfaceList;
  115. D3DPOOL mD3DPool;
  116. bool mDynamicTextures;
  117. bool mIsBindableAsShaderResource;
  118. PixelBufferPtr mLockedBuffer;
  119. bool mHwGammaReadSupported;
  120. bool mHwGammaWriteSupported;
  121. D3DMULTISAMPLE_TYPE mMultisampleType;
  122. DWORD mMultisampleQuality;
  123. };
  124. /** @} */
  125. }