BsD3D9Texture.h 6.7 KB

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