OGLTexture.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 "../../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. OBJECT(Texture)
  36. BASEOBJECT(Resource)
  37. public:
  38. /// Construct.
  39. Texture(Context* context);
  40. /// Destruct.
  41. virtual ~Texture();
  42. /// Set number of requested mip levels. Needs to be called before setting size.
  43. void SetNumLevels(unsigned levels);
  44. /// Set filtering mode.
  45. void SetFilterMode(TextureFilterMode filter);
  46. /// Set addressing mode by texture coordinate.
  47. void SetAddressMode(TextureCoordinate coord, TextureAddressMode address);
  48. /// Set shadow compare mode.
  49. void SetShadowCompare(bool enable);
  50. /// Set border color for border addressing mode.
  51. void SetBorderColor(const Color& color);
  52. /// Set sRGB sampling and writing mode.
  53. void SetSRGB(bool enable);
  54. /// Set backup texture to use when rendering to this texture.
  55. void SetBackupTexture(Texture* texture);
  56. /// Set mip levels to skip on a quality setting when loading. Ensures higher quality levels do not skip more.
  57. void SetMipsToSkip(int quality, int mips);
  58. /// Dirty the parameters.
  59. void SetParametersDirty();
  60. /// Update changed parameters to OpenGL. Called by Graphics when binding the texture.
  61. void UpdateParameters();
  62. /// Return texture's OpenGL target.
  63. unsigned GetTarget() const { return target_; }
  64. /// Return texture format.
  65. unsigned GetFormat() const { return format_; }
  66. /// Return whether the texture format is compressed.
  67. bool IsCompressed() const;
  68. /// Return number of mip levels.
  69. unsigned GetLevels() const { return levels_; }
  70. /// Return width.
  71. int GetWidth() const { return width_; }
  72. /// Return height.
  73. int GetHeight() const { return height_; }
  74. /// Return height.
  75. int GetDepth() const { return depth_; }
  76. /// Return whether parameters are dirty.
  77. bool GetParametersDirty() const { return parametersDirty_; }
  78. /// Return filtering mode.
  79. TextureFilterMode GetFilterMode() const { return filterMode_; }
  80. /// Return addressing mode by texture coordinate.
  81. TextureAddressMode GetAddressMode(TextureCoordinate coord) const { return addressMode_[coord]; }
  82. /// Return whether shadow compare is enabled.
  83. bool GetShadowCompare() const { return shadowCompare_; }
  84. /// Return border color.
  85. const Color& GetBorderColor() const { return borderColor_; }
  86. /// Return whether is using sRGB sampling and writing.
  87. bool GetSRGB() const { return sRGB_; }
  88. /// Return backup texture.
  89. Texture* GetBackupTexture() const { return backupTexture_; }
  90. /// Return mip levels to skip on a quality setting when loading.
  91. int GetMipsToSkip(int quality) const;
  92. /// Return mip level width, or 0 if level does not exist.
  93. int GetLevelWidth(unsigned level) const;
  94. /// Return mip level width, or 0 if level does not exist.
  95. int GetLevelHeight(unsigned level) const;
  96. /// Return mip level depth, or 0 if level does not exist.
  97. int GetLevelDepth(unsigned level) const;
  98. /// Return texture usage type.
  99. TextureUsage GetUsage() const { return usage_; }
  100. /// Return data size in bytes for a rectangular region.
  101. unsigned GetDataSize(int width, int height) const;
  102. /// Return data size in bytes for a volume region.
  103. unsigned GetDataSize(int width, int height, int depth) const;
  104. /// Return data size in bytes for a pixel or block row.
  105. unsigned GetRowDataSize(int width) const;
  106. /// Return number of image components required to receive pixel data from GetData(), or 0 for compressed images.
  107. unsigned GetComponents() const;
  108. /// Return the non-internal texture format corresponding to an OpenGL internal format.
  109. static unsigned GetExternalFormat(unsigned format);
  110. /// Return the data type corresponding to an OpenGL internal format.
  111. static unsigned GetDataType(unsigned format);
  112. /// Set additional parameters from an XML file.
  113. void SetParameters(XMLFile* xml);
  114. /// Set additional parameters from an XML element.
  115. void SetParameters(const XMLElement& element);
  116. /// Return the corresponding SRGB texture format if supported. If not supported, return format unchanged.
  117. unsigned GetSRGBFormat(unsigned format);
  118. protected:
  119. /// Check whether texture memory budget has been exceeded. Free unused materials in that case to release the texture references.
  120. void CheckTextureBudget(StringHash type);
  121. /// Create texture.
  122. virtual bool Create() { return true; }
  123. /// Texture usage type.
  124. TextureUsage usage_;
  125. /// OpenGL target.
  126. unsigned target_;
  127. /// Texture format.
  128. unsigned format_;
  129. /// Current mip levels.
  130. unsigned levels_;
  131. /// Requested mip levels.
  132. unsigned requestedLevels_;
  133. /// Texture width.
  134. int width_;
  135. /// Texture height.
  136. int height_;
  137. /// Texture depth.
  138. int depth_;
  139. /// Shadow compare mode.
  140. bool shadowCompare_;
  141. /// Parameters dirty flag.
  142. bool parametersDirty_;
  143. /// Filtering mode.
  144. TextureFilterMode filterMode_;
  145. /// Addressing mode.
  146. TextureAddressMode addressMode_[MAX_COORDS];
  147. /// Mip levels to skip when loading per texture quality setting.
  148. unsigned mipsToSkip_[MAX_TEXTURE_QUALITY_LEVELS];
  149. /// Border color.
  150. Color borderColor_;
  151. /// sRGB sampling and writing mode flag.
  152. bool sRGB_;
  153. /// Backup texture.
  154. SharedPtr<Texture> backupTexture_;
  155. };
  156. }