Texture.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #pragma once
  4. #include "../GraphicsAPI/GPUObject.h"
  5. #include "../GraphicsAPI/GraphicsDefs.h"
  6. #include "../Math/Color.h"
  7. #include "../Resource/Resource.h"
  8. namespace Urho3D
  9. {
  10. static const int MAX_TEXTURE_QUALITY_LEVELS = 3;
  11. class XMLElement;
  12. class XMLFile;
  13. /// Base class for texture resources.
  14. class URHO3D_API Texture : public ResourceWithMetadata, public GPUObject
  15. {
  16. URHO3D_OBJECT(Texture, ResourceWithMetadata);
  17. public:
  18. /// Construct.
  19. explicit Texture(Context* context);
  20. /// Destruct.
  21. ~Texture() override;
  22. /// Set number of requested mip levels. Needs to be called before setting size.
  23. /** The default value (0) allocates as many mip levels as necessary to reach 1x1 size. Set value 1 to disable mipmapping.
  24. Note that rendertargets need to regenerate mips dynamically after rendering, which may cost performance. Screen buffers
  25. and shadow maps allocated by Renderer will have mipmaps disabled.
  26. */
  27. void SetNumLevels(unsigned levels);
  28. /// Set filtering mode.
  29. /// @property
  30. void SetFilterMode(TextureFilterMode mode);
  31. /// Set addressing mode by texture coordinate.
  32. /// @property
  33. void SetAddressMode(TextureCoordinate coord, TextureAddressMode mode);
  34. /// Set texture max. anisotropy level. No effect if not using anisotropic filtering. Value 0 (default) uses the default setting from Renderer.
  35. /// @property
  36. void SetAnisotropy(unsigned level);
  37. /// Set shadow compare mode. Not used on Direct3D9.
  38. void SetShadowCompare(bool enable);
  39. /// Set border color for border addressing mode.
  40. /// @property
  41. void SetBorderColor(const Color& color);
  42. /// Set sRGB sampling and writing mode.
  43. /// @property
  44. void SetSRGB(bool enable);
  45. /// Set backup texture to use when rendering to this texture.
  46. /// @property
  47. void SetBackupTexture(Texture* texture);
  48. /// Set mip levels to skip on a quality setting when loading. Ensures higher quality levels do not skip more.
  49. /// @property
  50. void SetMipsToSkip(MaterialQuality quality, int toSkip);
  51. /// Return API-specific texture format.
  52. /// @property
  53. unsigned GetFormat() const { return format_; }
  54. /// Return whether the texture format is compressed.
  55. /// @property
  56. bool IsCompressed() const;
  57. /// Return number of mip levels.
  58. /// @property
  59. unsigned GetLevels() const { return levels_; }
  60. /// Return width.
  61. /// @property
  62. int GetWidth() const { return width_; }
  63. /// Return height.
  64. /// @property
  65. int GetHeight() const { return height_; }
  66. /// Return depth.
  67. int GetDepth() const { return depth_; }
  68. /// Return filtering mode.
  69. /// @property
  70. TextureFilterMode GetFilterMode() const { return filterMode_; }
  71. /// Return addressing mode by texture coordinate.
  72. /// @property
  73. TextureAddressMode GetAddressMode(TextureCoordinate coord) const { return addressModes_[coord]; }
  74. /// Return texture max. anisotropy level. Value 0 means to use the default value from Renderer.
  75. /// @property
  76. unsigned GetAnisotropy() const { return anisotropy_; }
  77. /// Return whether shadow compare is enabled. Not used on Direct3D9.
  78. bool GetShadowCompare() const { return shadowCompare_; }
  79. /// Return border color.
  80. /// @property
  81. const Color& GetBorderColor() const { return borderColor_; }
  82. /// Return whether is using sRGB sampling and writing.
  83. /// @property
  84. bool GetSRGB() const { return sRGB_; }
  85. /// Return texture multisampling level (1 = no multisampling).
  86. /// @property
  87. int GetMultiSample() const { return multiSample_; }
  88. /// Return texture multisampling autoresolve mode. When true, the texture is resolved before being sampled on SetTexture(). When false, the texture will not be resolved and must be read as individual samples in the shader.
  89. /// @property
  90. bool GetAutoResolve() const { return autoResolve_; }
  91. /// Return whether multisampled texture needs resolve.
  92. /// @property
  93. bool IsResolveDirty() const { return resolveDirty_; }
  94. /// Return whether rendertarget mipmap levels need regenration.
  95. /// @property
  96. bool GetLevelsDirty() const { return levelsDirty_; }
  97. /// Return backup texture.
  98. /// @property
  99. Texture* GetBackupTexture() const { return backupTexture_; }
  100. /// Return mip levels to skip on a quality setting when loading.
  101. /// @property
  102. int GetMipsToSkip(MaterialQuality quality) const;
  103. /// Return mip level width, or 0 if level does not exist.
  104. /// @property
  105. int GetLevelWidth(unsigned level) const;
  106. /// Return mip level width, or 0 if level does not exist.
  107. /// @property
  108. int GetLevelHeight(unsigned level) const;
  109. /// Return mip level depth, or 0 if level does not exist.
  110. int GetLevelDepth(unsigned level) const;
  111. /// Return texture usage type.
  112. /// @property
  113. TextureUsage GetUsage() const { return usage_; }
  114. /// Return data size in bytes for a rectangular region.
  115. unsigned GetDataSize(int width, int height) const;
  116. /// Return data size in bytes for a volume region.
  117. unsigned GetDataSize(int width, int height, int depth) const;
  118. /// Return data size in bytes for a pixel or block row.
  119. unsigned GetRowDataSize(int width) const;
  120. /// Return number of image components required to receive pixel data from GetData(), or 0 for compressed images.
  121. /// @property
  122. unsigned GetComponents() const;
  123. /// Return whether the parameters are dirty.
  124. bool GetParametersDirty() const;
  125. /// Set additional parameters from an XML file.
  126. void SetParameters(XMLFile* file);
  127. /// Set additional parameters from an XML element.
  128. void SetParameters(const XMLElement& element);
  129. /// Mark parameters dirty. Called by Graphics.
  130. void SetParametersDirty();
  131. /// Update dirty parameters to the texture object. Called by Graphics when assigning the texture.
  132. void UpdateParameters();
  133. /// Return shader resource view. Only used on Direct3D11.
  134. void* GetShaderResourceView() const { return shaderResourceView_; }
  135. /// Return sampler state object. Only used on Direct3D11.
  136. void* GetSampler() const { return sampler_; }
  137. /// Return resolve texture. Only used on Direct3D11.
  138. void* GetResolveTexture() const { return resolveTexture_; }
  139. /// Return texture's target. Only used on OpenGL.
  140. unsigned GetTarget() const { return target_; }
  141. /// Set or clear the need resolve flag. Called internally by Graphics.
  142. void SetResolveDirty(bool enable) { resolveDirty_ = enable; }
  143. /// Set the mipmap levels dirty flag. Called internally by Graphics.
  144. void SetLevelsDirty();
  145. /// Regenerate mipmap levels for a rendertarget after rendering and before sampling. Called internally by Graphics. No-op on Direct3D9. On OpenGL the texture must have been bound to work properly.
  146. void RegenerateLevels();
  147. /// Check maximum allowed mip levels for a specific texture size.
  148. static unsigned CheckMaxLevels(int width, int height, unsigned requestedLevels);
  149. /// Check maximum allowed mip levels for a specific 3D texture size.
  150. static unsigned CheckMaxLevels(int width, int height, int depth, unsigned requestedLevels);
  151. #ifdef URHO3D_OPENGL
  152. /// Return the data type corresponding to an OpenGL internal format.
  153. static unsigned GetDataType_OGL(unsigned format);
  154. #endif
  155. protected:
  156. #ifdef URHO3D_OPENGL
  157. /// Convert format to sRGB. Not used on Direct3D9.
  158. unsigned GetSRGBFormat_OGL(unsigned format);
  159. /// Return the non-internal texture format corresponding to an OpenGL internal format.
  160. static unsigned GetExternalFormat_OGL(unsigned format);
  161. #endif // def URHO3D_OPENGL
  162. #ifdef URHO3D_D3D11
  163. /// Convert format to sRGB. Not used on Direct3D9.
  164. unsigned GetSRGBFormat_D3D11(unsigned format);
  165. /// Return the shader resource view format corresponding to a texture format. Handles conversion of typeless depth texture formats. Only used on Direct3D11.
  166. static unsigned GetSRVFormat_D3D11(unsigned format);
  167. /// Return the depth-stencil view format corresponding to a texture format. Handles conversion of typeless depth texture formats. Only used on Direct3D11.
  168. static unsigned GetDSVFormat_D3D11(unsigned format);
  169. #endif // def URHO3D_D3D11
  170. // For proxy functions
  171. #ifdef URHO3D_OPENGL
  172. void SetSRGB_OGL(bool enable);
  173. void UpdateParameters_OGL();
  174. bool GetParametersDirty_OGL() const;
  175. bool IsCompressed_OGL() const;
  176. unsigned GetRowDataSize_OGL(int width) const;
  177. void RegenerateLevels_OGL();
  178. #endif // def URHO3D_OPENGL
  179. #ifdef URHO3D_D3D11
  180. void SetSRGB_D3D11(bool enable);
  181. void UpdateParameters_D3D11();
  182. bool GetParametersDirty_D3D11() const;
  183. bool IsCompressed_D3D11() const;
  184. unsigned GetRowDataSize_D3D11(int width) const;
  185. void RegenerateLevels_D3D11();
  186. #endif // def URHO3D_D3D11
  187. /// Check whether texture memory budget has been exceeded. Free unused materials in that case to release the texture references.
  188. void CheckTextureBudget(StringHash type);
  189. /// Create the GPU texture. Implemented in subclasses.
  190. virtual bool Create() { return true; }
  191. /// OpenGL target.
  192. unsigned target_{};
  193. /// Direct3D11 shader resource view.
  194. void* shaderResourceView_{};
  195. /// Direct3D11 sampler state object.
  196. void* sampler_{};
  197. /// Direct3D11 resolve texture object when multisample with autoresolve is used.
  198. void* resolveTexture_{};
  199. /// Texture format.
  200. unsigned format_{};
  201. /// Texture usage type.
  202. TextureUsage usage_{TEXTURE_STATIC};
  203. /// Current mip levels.
  204. unsigned levels_{};
  205. /// Requested mip levels.
  206. unsigned requestedLevels_{};
  207. /// Texture width.
  208. int width_{};
  209. /// Texture height.
  210. int height_{};
  211. /// Texture depth.
  212. int depth_{};
  213. /// Shadow compare mode.
  214. bool shadowCompare_{};
  215. /// Filtering mode.
  216. TextureFilterMode filterMode_{FILTER_DEFAULT};
  217. /// Addressing mode.
  218. TextureAddressMode addressModes_[MAX_COORDS]{ADDRESS_WRAP, ADDRESS_WRAP, ADDRESS_WRAP};
  219. /// Texture anisotropy level.
  220. unsigned anisotropy_{};
  221. /// Mip levels to skip when loading per texture quality setting.
  222. unsigned mipsToSkip_[MAX_TEXTURE_QUALITY_LEVELS]{2, 1, 0};
  223. /// Border color.
  224. Color borderColor_;
  225. /// Multisampling level.
  226. int multiSample_{1};
  227. /// sRGB sampling and writing mode flag.
  228. bool sRGB_{};
  229. /// Parameters dirty flag.
  230. bool parametersDirty_{true};
  231. /// Multisampling autoresolve flag.
  232. bool autoResolve_{};
  233. /// Multisampling resolve needed -flag.
  234. bool resolveDirty_{};
  235. /// Mipmap levels regeneration needed -flag.
  236. bool levelsDirty_{};
  237. /// Backup texture.
  238. SharedPtr<Texture> backupTexture_;
  239. };
  240. }