Texture.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. //
  2. // Copyright (c) 2008-2020 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "../Graphics/GPUObject.h"
  24. #include "../Graphics/GraphicsDefs.h"
  25. #include "../Math/Color.h"
  26. #include "../Resource/Resource.h"
  27. namespace Urho3D
  28. {
  29. static const int MAX_TEXTURE_QUALITY_LEVELS = 3;
  30. class XMLElement;
  31. class XMLFile;
  32. /// Base class for texture resources.
  33. class URHO3D_API Texture : public ResourceWithMetadata, public GPUObject
  34. {
  35. URHO3D_OBJECT(Texture, ResourceWithMetadata);
  36. public:
  37. /// Construct.
  38. explicit Texture(Context* context);
  39. /// Destruct.
  40. ~Texture() override;
  41. /// Set number of requested mip levels. Needs to be called before setting size.
  42. /** The default value (0) allocates as many mip levels as necessary to reach 1x1 size. Set value 1 to disable mipmapping.
  43. Note that rendertargets need to regenerate mips dynamically after rendering, which may cost performance. Screen buffers
  44. and shadow maps allocated by Renderer will have mipmaps disabled.
  45. */
  46. void SetNumLevels(unsigned levels);
  47. /// Set filtering mode.
  48. /// @property
  49. void SetFilterMode(TextureFilterMode mode);
  50. /// Set addressing mode by texture coordinate.
  51. /// @property
  52. void SetAddressMode(TextureCoordinate coord, TextureAddressMode mode);
  53. /// Set texture max. anisotropy level. No effect if not using anisotropic filtering. Value 0 (default) uses the default setting from Renderer.
  54. /// @property
  55. void SetAnisotropy(unsigned level);
  56. /// Set shadow compare mode. Not used on Direct3D9.
  57. void SetShadowCompare(bool enable);
  58. /// Set border color for border addressing mode.
  59. /// @property
  60. void SetBorderColor(const Color& color);
  61. /// Set sRGB sampling and writing mode.
  62. /// @property
  63. void SetSRGB(bool enable);
  64. /// Set backup texture to use when rendering to this texture.
  65. /// @property
  66. void SetBackupTexture(Texture* texture);
  67. /// Set mip levels to skip on a quality setting when loading. Ensures higher quality levels do not skip more.
  68. /// @property
  69. void SetMipsToSkip(MaterialQuality quality, int toSkip);
  70. /// Return API-specific texture format.
  71. /// @property
  72. unsigned GetFormat() const { return format_; }
  73. /// Return whether the texture format is compressed.
  74. /// @property
  75. bool IsCompressed() const;
  76. /// Return number of mip levels.
  77. /// @property
  78. unsigned GetLevels() const { return levels_; }
  79. /// Return width.
  80. /// @property
  81. int GetWidth() const { return width_; }
  82. /// Return height.
  83. /// @property
  84. int GetHeight() const { return height_; }
  85. /// Return depth.
  86. int GetDepth() const { return depth_; }
  87. /// Return filtering mode.
  88. /// @property
  89. TextureFilterMode GetFilterMode() const { return filterMode_; }
  90. /// Return addressing mode by texture coordinate.
  91. /// @property
  92. TextureAddressMode GetAddressMode(TextureCoordinate coord) const { return addressModes_[coord]; }
  93. /// Return texture max. anisotropy level. Value 0 means to use the default value from Renderer.
  94. /// @property
  95. unsigned GetAnisotropy() const { return anisotropy_; }
  96. /// Return whether shadow compare is enabled. Not used on Direct3D9.
  97. bool GetShadowCompare() const { return shadowCompare_; }
  98. /// Return border color.
  99. /// @property
  100. const Color& GetBorderColor() const { return borderColor_; }
  101. /// Return whether is using sRGB sampling and writing.
  102. /// @property
  103. bool GetSRGB() const { return sRGB_; }
  104. /// Return texture multisampling level (1 = no multisampling).
  105. /// @property
  106. int GetMultiSample() const { return multiSample_; }
  107. /// 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.
  108. /// @property
  109. bool GetAutoResolve() const { return autoResolve_; }
  110. /// Return whether multisampled texture needs resolve.
  111. /// @property
  112. bool IsResolveDirty() const { return resolveDirty_; }
  113. /// Return whether rendertarget mipmap levels need regenration.
  114. /// @property
  115. bool GetLevelsDirty() const { return levelsDirty_; }
  116. /// Return backup texture.
  117. /// @property
  118. Texture* GetBackupTexture() const { return backupTexture_; }
  119. /// Return mip levels to skip on a quality setting when loading.
  120. /// @property
  121. int GetMipsToSkip(MaterialQuality quality) const;
  122. /// Return mip level width, or 0 if level does not exist.
  123. /// @property
  124. int GetLevelWidth(unsigned level) const;
  125. /// Return mip level width, or 0 if level does not exist.
  126. /// @property
  127. int GetLevelHeight(unsigned level) const;
  128. /// Return mip level depth, or 0 if level does not exist.
  129. int GetLevelDepth(unsigned level) const;
  130. /// Return texture usage type.
  131. /// @property
  132. TextureUsage GetUsage() const { return usage_; }
  133. /// Return data size in bytes for a rectangular region.
  134. unsigned GetDataSize(int width, int height) const;
  135. /// Return data size in bytes for a volume region.
  136. unsigned GetDataSize(int width, int height, int depth) const;
  137. /// Return data size in bytes for a pixel or block row.
  138. unsigned GetRowDataSize(int width) const;
  139. /// Return number of image components required to receive pixel data from GetData(), or 0 for compressed images.
  140. /// @property
  141. unsigned GetComponents() const;
  142. /// Return whether the parameters are dirty.
  143. bool GetParametersDirty() const;
  144. /// Set additional parameters from an XML file.
  145. void SetParameters(XMLFile* file);
  146. /// Set additional parameters from an XML element.
  147. void SetParameters(const XMLElement& element);
  148. /// Mark parameters dirty. Called by Graphics.
  149. void SetParametersDirty();
  150. /// Update dirty parameters to the texture object. Called by Graphics when assigning the texture.
  151. void UpdateParameters();
  152. /// Return shader resource view. Only used on Direct3D11.
  153. void* GetShaderResourceView() const { return shaderResourceView_; }
  154. /// Return sampler state object. Only used on Direct3D11.
  155. void* GetSampler() const { return sampler_; }
  156. /// Return resolve texture. Only used on Direct3D11.
  157. void* GetResolveTexture() const { return resolveTexture_; }
  158. /// Return texture's target. Only used on OpenGL.
  159. unsigned GetTarget() const { return target_; }
  160. /// Convert format to sRGB. Not used on Direct3D9.
  161. /// @nobind
  162. unsigned GetSRGBFormat(unsigned format);
  163. /// Set or clear the need resolve flag. Called internally by Graphics.
  164. void SetResolveDirty(bool enable) { resolveDirty_ = enable; }
  165. /// Set the mipmap levels dirty flag. Called internally by Graphics.
  166. void SetLevelsDirty();
  167. /// 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.
  168. void RegenerateLevels();
  169. /// Check maximum allowed mip levels for a specific texture size.
  170. static unsigned CheckMaxLevels(int width, int height, unsigned requestedLevels);
  171. /// Check maximum allowed mip levels for a specific 3D texture size.
  172. static unsigned CheckMaxLevels(int width, int height, int depth, unsigned requestedLevels);
  173. /// Return the shader resource view format corresponding to a texture format. Handles conversion of typeless depth texture formats. Only used on Direct3D11.
  174. /// @nobind
  175. static unsigned GetSRVFormat(unsigned format);
  176. /// Return the depth-stencil view format corresponding to a texture format. Handles conversion of typeless depth texture formats. Only used on Direct3D11.
  177. /// @nobind
  178. static unsigned GetDSVFormat(unsigned format);
  179. /// Return the non-internal texture format corresponding to an OpenGL internal format.
  180. /// @nobind
  181. static unsigned GetExternalFormat(unsigned format);
  182. /// Return the data type corresponding to an OpenGL internal format.
  183. /// @nobind
  184. static unsigned GetDataType(unsigned format);
  185. protected:
  186. /// Check whether texture memory budget has been exceeded. Free unused materials in that case to release the texture references.
  187. void CheckTextureBudget(StringHash type);
  188. /// Create the GPU texture. Implemented in subclasses.
  189. virtual bool Create() { return true; }
  190. /// OpenGL target.
  191. unsigned target_{};
  192. /// Direct3D11 shader resource view.
  193. void* shaderResourceView_{};
  194. /// Direct3D11 sampler state object.
  195. void* sampler_{};
  196. /// Direct3D11 resolve texture object when multisample with autoresolve is used.
  197. void* resolveTexture_{};
  198. /// Texture format.
  199. unsigned format_{};
  200. /// Texture usage type.
  201. TextureUsage usage_{TEXTURE_STATIC};
  202. /// Current mip levels.
  203. unsigned levels_{};
  204. /// Requested mip levels.
  205. unsigned requestedLevels_{};
  206. /// Texture width.
  207. int width_{};
  208. /// Texture height.
  209. int height_{};
  210. /// Texture depth.
  211. int depth_{};
  212. /// Shadow compare mode.
  213. bool shadowCompare_{};
  214. /// Filtering mode.
  215. TextureFilterMode filterMode_{FILTER_DEFAULT};
  216. /// Addressing mode.
  217. TextureAddressMode addressModes_[MAX_COORDS]{ADDRESS_WRAP, ADDRESS_WRAP, ADDRESS_WRAP};
  218. /// Texture anisotropy level.
  219. unsigned anisotropy_{};
  220. /// Mip levels to skip when loading per texture quality setting.
  221. unsigned mipsToSkip_[MAX_TEXTURE_QUALITY_LEVELS]{2, 1, 0};
  222. /// Border color.
  223. Color borderColor_;
  224. /// Multisampling level.
  225. int multiSample_{1};
  226. /// sRGB sampling and writing mode flag.
  227. bool sRGB_{};
  228. /// Parameters dirty flag.
  229. bool parametersDirty_{true};
  230. /// Multisampling autoresolve flag.
  231. bool autoResolve_{};
  232. /// Multisampling resolve needed -flag.
  233. bool resolveDirty_{};
  234. /// Mipmap levels regeneration needed -flag.
  235. bool levelsDirty_{};
  236. /// Backup texture.
  237. SharedPtr<Texture> backupTexture_;
  238. };
  239. }