OGLTexture.h 6.6 KB

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