Texture.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. //
  2. // Copyright (c) 2008-2016 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 Atomic
  28. {
  29. static const int MAX_TEXTURE_QUALITY_LEVELS = 3;
  30. class XMLElement;
  31. class XMLFile;
  32. /// Base class for texture resources.
  33. class ATOMIC_API Texture : public Resource, public GPUObject
  34. {
  35. ATOMIC_OBJECT(Texture, Resource)
  36. public:
  37. /// Construct.
  38. Texture(Context* context);
  39. /// Destruct.
  40. virtual ~Texture();
  41. /// Set number of requested mip levels. Needs to be called before setting size.
  42. void SetNumLevels(unsigned levels);
  43. /// Set filtering mode.
  44. void SetFilterMode(TextureFilterMode filter);
  45. /// Set addressing mode by texture coordinate.
  46. void SetAddressMode(TextureCoordinate coord, TextureAddressMode address);
  47. /// Set texture max. anisotropy level. No effect if not using anisotropic filtering. Value 0 (default) uses the default setting from Renderer.
  48. void SetAnisotropy(unsigned level);
  49. /// Set shadow compare mode. Not used on Direct3D9.
  50. void SetShadowCompare(bool enable);
  51. /// Set border color for border addressing mode.
  52. void SetBorderColor(const Color& color);
  53. /// Set sRGB sampling and writing mode.
  54. void SetSRGB(bool enable);
  55. /// Set backup texture to use when rendering to this texture.
  56. void SetBackupTexture(Texture* texture);
  57. /// Set mip levels to skip on a quality setting when loading. Ensures higher quality levels do not skip more.
  58. void SetMipsToSkip(int quality, int toSkip);
  59. /// Return API-specific texture format.
  60. unsigned GetFormat() const { return format_; }
  61. /// Return whether the texture format is compressed.
  62. bool IsCompressed() const;
  63. /// Return number of mip levels.
  64. unsigned GetLevels() const { return levels_; }
  65. /// Return width.
  66. int GetWidth() const { return width_; }
  67. /// Return height.
  68. int GetHeight() const { return height_; }
  69. /// Return height.
  70. int GetDepth() const { return depth_; }
  71. /// Return filtering mode.
  72. TextureFilterMode GetFilterMode() const { return filterMode_; }
  73. /// Return addressing mode by texture coordinate.
  74. TextureAddressMode GetAddressMode(TextureCoordinate coord) const { return addressMode_[coord]; }
  75. /// Return texture max. anisotropy level. Value 0 means to use the default value from Renderer.
  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. const Color& GetBorderColor() const { return borderColor_; }
  81. /// Return whether is using sRGB sampling and writing.
  82. bool GetSRGB() const { return sRGB_; }
  83. /// Return backup texture.
  84. Texture* GetBackupTexture() const { return backupTexture_; }
  85. /// Return mip levels to skip on a quality setting when loading.
  86. int GetMipsToSkip(int quality) const;
  87. /// Return mip level width, or 0 if level does not exist.
  88. int GetLevelWidth(unsigned level) const;
  89. /// Return mip level width, or 0 if level does not exist.
  90. int GetLevelHeight(unsigned level) const;
  91. /// Return mip level depth, or 0 if level does not exist.
  92. int GetLevelDepth(unsigned level) const;
  93. /// Return texture usage type.
  94. TextureUsage GetUsage() const { return usage_; }
  95. /// Return data size in bytes for a rectangular region.
  96. unsigned GetDataSize(int width, int height) const;
  97. /// Return data size in bytes for a volume region.
  98. unsigned GetDataSize(int width, int height, int depth) const;
  99. /// Return data size in bytes for a pixel or block row.
  100. unsigned GetRowDataSize(int width) const;
  101. /// Return number of image components required to receive pixel data from GetData(), or 0 for compressed images.
  102. unsigned GetComponents() const;
  103. /// Return whether the parameters are dirty.
  104. bool GetParametersDirty() const;
  105. /// Set additional parameters from an XML file.
  106. void SetParameters(XMLFile* xml);
  107. /// Set additional parameters from an XML element.
  108. void SetParameters(const XMLElement& element);
  109. /// Mark parameters dirty. Called by Graphics.
  110. void SetParametersDirty();
  111. /// Update dirty parameters to the texture object. Called by Graphics when assigning the texture.
  112. void UpdateParameters();
  113. /// Return shader resource view. Only used on Direct3D11.
  114. void* GetShaderResourceView() const { return shaderResourceView_; }
  115. /// Return sampler state object. Only used on Direct3D11.
  116. void* GetSampler() const { return sampler_; }
  117. /// Return texture's target. Only used on OpenGL.
  118. unsigned GetTarget() const { return target_; }
  119. /// Convert format to sRGB. Not used on Direct3D9.
  120. unsigned GetSRGBFormat(unsigned format);
  121. /// Check maximum allowed mip levels for a specific texture size.
  122. static unsigned CheckMaxLevels(int width, int height, unsigned requestedLevels);
  123. /// Check maximum allowed mip levels for a specific 3D texture size.
  124. static unsigned CheckMaxLevels(int width, int height, int depth, unsigned requestedLevels);
  125. /// Return the shader resource view format corresponding to a texture format. Handles conversion of typeless depth texture formats. Only used on Direct3D11.
  126. static unsigned GetSRVFormat(unsigned format);
  127. /// Return the depth-stencil view format corresponding to a texture format. Handles conversion of typeless depth texture formats. Only used on Direct3D11.
  128. static unsigned GetDSVFormat(unsigned format);
  129. /// Return the non-internal texture format corresponding to an OpenGL internal format.
  130. static unsigned GetExternalFormat(unsigned format);
  131. /// Return the data type corresponding to an OpenGL internal format.
  132. static unsigned GetDataType(unsigned format);
  133. protected:
  134. /// Check whether texture memory budget has been exceeded. Free unused materials in that case to release the texture references.
  135. void CheckTextureBudget(StringHash type);
  136. /// Create the GPU texture. Implemented in subclasses.
  137. virtual bool Create() { return true; }
  138. union
  139. {
  140. /// Direct3D11 shader resource view.
  141. void* shaderResourceView_;
  142. /// OpenGL target.
  143. unsigned target_;
  144. };
  145. /// Direct3D11 sampler state object.
  146. void* sampler_;
  147. /// Texture format.
  148. unsigned format_;
  149. /// Texture usage type.
  150. TextureUsage usage_;
  151. /// Current mip levels.
  152. unsigned levels_;
  153. /// Requested mip levels.
  154. unsigned requestedLevels_;
  155. /// Texture width.
  156. int width_;
  157. /// Texture height.
  158. int height_;
  159. /// Texture depth.
  160. int depth_;
  161. /// Shadow compare mode.
  162. bool shadowCompare_;
  163. /// Filtering mode.
  164. TextureFilterMode filterMode_;
  165. /// Addressing mode.
  166. TextureAddressMode addressMode_[MAX_COORDS];
  167. /// Texture anisotropy level.
  168. unsigned anisotropy_;
  169. /// Mip levels to skip when loading per texture quality setting.
  170. unsigned mipsToSkip_[MAX_TEXTURE_QUALITY_LEVELS];
  171. /// Border color.
  172. Color borderColor_;
  173. /// sRGB sampling and writing mode flag.
  174. bool sRGB_;
  175. /// Parameters dirty flag.
  176. bool parametersDirty_;
  177. /// Backup texture.
  178. SharedPtr<Texture> backupTexture_;
  179. };
  180. }