D3D11Texture.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. //
  2. // Copyright (c) 2008-2015 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 "../../Container/ArrayPtr.h"
  24. #include "../../Graphics/GPUObject.h"
  25. #include "../../Graphics/GraphicsDefs.h"
  26. #include "../../Math/Color.h"
  27. #include "../../Resource/Resource.h"
  28. namespace Atomic
  29. {
  30. static const int MAX_TEXTURE_QUALITY_LEVELS = 3;
  31. class XMLElement;
  32. class XMLFile;
  33. /// Base class for texture resources.
  34. class ATOMIC_API Texture : public Resource, public GPUObject
  35. {
  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 shadow compare mode.
  48. void SetShadowCompare(bool enable);
  49. /// Set border color for border addressing mode.
  50. void SetBorderColor(const Color& color);
  51. /// Set sRGB sampling and writing mode.
  52. void SetSRGB(bool enable);
  53. /// Set backup texture to use when rendering to this texture.
  54. void SetBackupTexture(Texture* texture);
  55. /// Set mip levels to skip on a quality setting when loading. Ensures higher quality levels do not skip more.
  56. void SetMipsToSkip(int quality, int mips);
  57. /// Return texture format.
  58. unsigned GetFormat() const { return format_; }
  59. /// Return whether the texture format is compressed.
  60. bool IsCompressed() const;
  61. /// Return number of mip levels.
  62. unsigned GetLevels() const { return levels_; }
  63. /// Return width.
  64. int GetWidth() const { return width_; }
  65. /// Return height.
  66. int GetHeight() const { return height_; }
  67. /// Return height.
  68. int GetDepth() const { return depth_; }
  69. /// Return filtering mode.
  70. TextureFilterMode GetFilterMode() const { return filterMode_; }
  71. /// Return addressing mode by texture coordinate.
  72. TextureAddressMode GetAddressMode(TextureCoordinate coord) const { return addressMode_[coord]; }
  73. /// Return whether shadow compare is enabled.
  74. bool GetShadowCompare() const { return shadowCompare_; }
  75. /// Return border color.
  76. const Color& GetBorderColor() const { return borderColor_; }
  77. /// Return whether is using sRGB sampling and writing.
  78. bool GetSRGB() const { return sRGB_; }
  79. /// Return backup texture.
  80. Texture* GetBackupTexture() const { return backupTexture_; }
  81. /// Return mip levels to skip on a quality setting when loading.
  82. int GetMipsToSkip(int quality) const;
  83. /// Return mip level width, or 0 if level does not exist.
  84. int GetLevelWidth(unsigned level) const;
  85. /// Return mip level width, or 0 if level does not exist.
  86. int GetLevelHeight(unsigned level) const;
  87. /// Return mip level depth, or 0 if level does not exist.
  88. int GetLevelDepth(unsigned level) const;
  89. /// Return texture usage type.
  90. TextureUsage GetUsage() const { return usage_; }
  91. /// Return data size in bytes for a rectangular region.
  92. unsigned GetDataSize(int width, int height) const;
  93. /// Return data size in bytes for a volume region.
  94. unsigned GetDataSize(int width, int height, int depth) const;
  95. /// Return data size in bytes for a pixel or block row.
  96. unsigned GetRowDataSize(int width) const;
  97. /// Return number of image components required to receive pixel data from GetData(), or 0 for compressed images.
  98. unsigned GetComponents() const;
  99. /// Return whether the parameters are dirty.
  100. bool GetParametersDirty() const { return parametersDirty_ || !sampler_; }
  101. /// Set additional parameters from an XML file.
  102. void SetParameters(XMLFile* xml);
  103. /// Set additional parameters from an XML element.
  104. void SetParameters(const XMLElement& element);
  105. /// Mark parameters dirty. Called by Graphics.
  106. void SetParametersDirty();
  107. /// Create sampler state object after parameters have been changed. Called by Graphics when assigning the texture.
  108. void UpdateParameters();
  109. /// Return shader resource view.
  110. void* GetShaderResourceView() const { return shaderResourceView_; }
  111. /// Return sampler state object.
  112. void* GetSampler() const { return sampler_; }
  113. /// Check maximum allowed mip levels for a specific texture size.
  114. static unsigned CheckMaxLevels(int width, int height, unsigned requestedLevels);
  115. /// Check maximum allowed mip levels for a specific 3D texture size.
  116. static unsigned CheckMaxLevels(int width, int height, int depth, unsigned requestedLevels);
  117. /// Return the shader resource view format corresponding to a texture format. Handles conversion of typeless depth texture formats.
  118. static unsigned GetSRVFormat(unsigned format);
  119. /// Return the depth-stencil view format corresponding to a texture format. Handles conversion of typeless depth texture formats.
  120. static unsigned GetDSVFormat(unsigned format);
  121. /// Convert format to sRGB.
  122. static unsigned GetSRGBFormat(unsigned format);
  123. protected:
  124. /// Check whether texture memory budget has been exceeded. Free unused materials in that case to release the texture references.
  125. void CheckTextureBudget(StringHash type);
  126. /// Shader resource view.
  127. void* shaderResourceView_;
  128. /// Sampler state object.
  129. void* sampler_;
  130. /// Texture format.
  131. unsigned format_;
  132. /// Texture usage type.
  133. TextureUsage usage_;
  134. /// Current mip levels.
  135. unsigned levels_;
  136. /// Requested mip levels.
  137. unsigned requestedLevels_;
  138. /// Texture width.
  139. int width_;
  140. /// Texture height.
  141. int height_;
  142. /// Texture depth.
  143. int depth_;
  144. /// Shadow compare mode.
  145. bool shadowCompare_;
  146. /// Filtering mode.
  147. TextureFilterMode filterMode_;
  148. /// Addressing mode.
  149. TextureAddressMode addressMode_[MAX_COORDS];
  150. /// Mip levels to skip when loading per texture quality setting.
  151. unsigned mipsToSkip_[MAX_TEXTURE_QUALITY_LEVELS];
  152. /// Border color.
  153. Color borderColor_;
  154. /// sRGB sampling and writing mode flag.
  155. bool sRGB_;
  156. /// Parameters dirty flag.
  157. bool parametersDirty_;
  158. /// Backup texture.
  159. SharedPtr<Texture> backupTexture_;
  160. };
  161. }