BsD3D9Texture.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. class BS_D3D9_EXPORT D3D9Texture : public Texture, public D3D9Resource
  11. {
  12. protected:
  13. struct TextureResources
  14. {
  15. /// 1D/2D normal texture pointer
  16. IDirect3DTexture9* pNormTex;
  17. /// cubic texture pointer
  18. IDirect3DCubeTexture9* pCubeTex;
  19. /// Volume texture
  20. IDirect3DVolumeTexture9* pVolumeTex;
  21. /// actual texture pointer
  22. IDirect3DBaseTexture9* pBaseTex;
  23. /// Optional multisample surface
  24. IDirect3DSurface9* pMultisampleSurface;
  25. /// Optional depth stencil surface
  26. IDirect3DSurface9* pDepthStencilSurface;
  27. };
  28. public:
  29. ~D3D9Texture();
  30. /**
  31. * @copydoc Texture::isBindableAsShaderResource
  32. */
  33. bool isBindableAsShaderResource() const { return mIsBindableAsShaderResource; }
  34. /// retrieves a pointer to the actual texture
  35. IDirect3DBaseTexture9 *getTexture_internal();
  36. /// retrieves a pointer to the normal 1D/2D texture
  37. IDirect3DTexture9 *getNormTexture_internal();
  38. /// retrieves a pointer to the cube texture
  39. IDirect3DCubeTexture9 *getCubeTexture_internal();
  40. /** Indicates whether the hardware gamma is actually enabled and supported.
  41. @remarks
  42. Because hardware gamma might not actually be supported, we need to
  43. ignore it sometimes. Because D3D doesn't encode sRGB in the format but
  44. as a sampler state, and we don't want to change the original requested
  45. hardware gamma flag (e.g. serialisation) we need another indicator.
  46. */
  47. bool isHardwareGammaReadToBeUsed() const { return mHwGamma && mHwGammaReadSupported; }
  48. /// Will this texture need to be in the default pool?
  49. bool useDefaultPool();
  50. /** Return hardware pixel buffer for a surface. This buffer can then
  51. be used to copy data from and to a particular level of the texture.
  52. @param face Face number, in case of a cubemap texture. Must be 0
  53. for other types of textures.
  54. For cubemaps, this is one of
  55. +X (0), -X (1), +Y (2), -Y (3), +Z (4), -Z (5)
  56. @param mipmap Mipmap level. This goes from 0 for the first, largest
  57. mipmap level to getNumMipmaps()-1 for the smallest.
  58. @returns A shared pointer to a hardware pixel buffer
  59. @remarks The buffer is invalidated when the resource is unloaded or destroyed.
  60. Do not use it after the lifetime of the containing texture.
  61. */
  62. PixelBufferPtr getBuffer(UINT32 face, UINT32 mipmap);
  63. /**
  64. * @copydoc D3D9Resource::notifyOnDeviceCreate
  65. */
  66. virtual void notifyOnDeviceCreate(IDirect3DDevice9* d3d9Device);
  67. /**
  68. * @copydoc D3D9Resource::notifyOnDeviceDestroy
  69. */
  70. virtual void notifyOnDeviceDestroy(IDirect3DDevice9* d3d9Device);
  71. /**
  72. * @copydoc D3D9Resource::notifyOnDeviceLost
  73. */
  74. virtual void notifyOnDeviceLost(IDirect3DDevice9* d3d9Device);
  75. /**
  76. * @copydoc D3D9Resource::notifyOnDeviceReset
  77. */
  78. virtual void notifyOnDeviceReset(IDirect3DDevice9* d3d9Device);
  79. protected:
  80. friend class D3D9TextureManager;
  81. friend class D3D9PixelBuffer;
  82. D3D9Texture();
  83. /**
  84. * @copydoc Texture::initialize_internal()
  85. */
  86. void initialize_internal();
  87. /**
  88. * @copydoc Texture::destroy_internal()
  89. */
  90. void destroy_internal();
  91. /**
  92. * @copydoc Texture::lock
  93. */
  94. PixelData lockImpl(GpuLockOptions options, UINT32 mipLevel = 0, UINT32 face = 0);
  95. /**
  96. * @copydoc Texture::unlock
  97. */
  98. void unlockImpl();
  99. /**
  100. * @copydoc Texture::copy
  101. */
  102. void copyImpl(TexturePtr& target);
  103. /**
  104. * @copydoc Texture::readData
  105. */
  106. void readData(PixelData& dest, UINT32 mipLevel = 0, UINT32 face = 0);
  107. /**
  108. * @copydoc Texture::writeData
  109. */
  110. void writeData(const PixelData& src, UINT32 mipLevel = 0, UINT32 face = 0, bool discardWholeBuffer = false);
  111. /// internal method, create a blank normal 1D/2D texture
  112. void createNormTex(IDirect3DDevice9* d3d9Device);
  113. /// internal method, create a blank cube texture
  114. void createCubeTex(IDirect3DDevice9* d3d9Device);
  115. /// internal method, create a blank cube texture
  116. void createVolumeTex(IDirect3DDevice9* d3d9Device);
  117. /// internal method, return a D3D pixel format for texture creation
  118. D3DFORMAT chooseD3DFormat(IDirect3DDevice9* d3d9Device);
  119. /// @copydoc Resource::calculateSize
  120. UINT32 calculateSize() const;
  121. /// Creates this texture resources on the specified device.
  122. void createInternalResources(IDirect3DDevice9* d3d9Device);
  123. /// internal method, set Texture class final texture protected attributes
  124. void setFinalAttributes(IDirect3DDevice9* d3d9Device, TextureResources* textureResources,
  125. UINT32 width, UINT32 height, UINT32 depth, PixelFormat format);
  126. /// internal method, return the best by hardware supported filter method
  127. D3DTEXTUREFILTERTYPE getBestFilterMethod(IDirect3DDevice9* d3d9Device);
  128. /// internal method, return true if the device/texture combination can use dynamic textures
  129. bool canUseDynamicTextures(IDirect3DDevice9* d3d9Device, DWORD srcUsage, D3DRESOURCETYPE srcType, D3DFORMAT srcFormat);
  130. /// internal method, return true if the device/texture combination can auto gen. mip maps
  131. bool canAutoGenMipmaps(IDirect3DDevice9* d3d9Device, DWORD srcUsage, D3DRESOURCETYPE srcType, D3DFORMAT srcFormat);
  132. /// internal method, return true if the device/texture combination can use hardware gamma
  133. bool canUseHardwareGammaCorrection(IDirect3DDevice9* d3d9Device, DWORD srcUsage, D3DRESOURCETYPE srcType, D3DFORMAT srcFormat, bool forwriting);
  134. /// internal method, create D3D9HardwarePixelBuffers for every face and
  135. /// mipmap level. This method must be called after the D3D texture object was created
  136. void createSurfaceList(IDirect3DDevice9* d3d9Device, TextureResources* textureResources);
  137. /// gets the texture resources attached to the given device.
  138. TextureResources* getTextureResources(IDirect3DDevice9* d3d9Device);
  139. /// allocates new texture resources structure attached to the given device.
  140. TextureResources* allocateTextureResources(IDirect3DDevice9* d3d9Device);
  141. /// frees the given texture resources.
  142. void freeTextureResources(IDirect3DDevice9* d3d9Device, TextureResources* textureResources);
  143. void determinePool();
  144. protected:
  145. /// Map between device to texture resources.
  146. Map<IDirect3DDevice9*, TextureResources*> mMapDeviceToTextureResources;
  147. /// Vector of pointers to subsurfaces
  148. Vector<PixelBufferPtr> mSurfaceList;
  149. /// The memory pool being used
  150. D3DPOOL mD3DPool;
  151. // Dynamic textures?
  152. bool mDynamicTextures;
  153. bool mIsBindableAsShaderResource;
  154. PixelBufferPtr mLockedBuffer;
  155. /// Is hardware gamma supported (read)?
  156. bool mHwGammaReadSupported;
  157. /// Is hardware gamma supported (write)?
  158. bool mHwGammaWriteSupported;
  159. D3DMULTISAMPLE_TYPE mMultisampleType;
  160. DWORD mMultisampleQuality;
  161. };
  162. }