OGLTexture.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. //
  2. // Copyright (c) 2008-2013 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 whether parameters are dirty.
  73. bool GetParametersDirty() const { return parametersDirty_; }
  74. /// Return filtering mode.
  75. TextureFilterMode GetFilterMode() const { return filterMode_; }
  76. /// Return addressing mode by texture coordinate.
  77. TextureAddressMode GetAddressMode(TextureCoordinate coord) const { return addressMode_[coord]; }
  78. /// Return whether shadow compare is enabled, OpenGL only.
  79. bool GetShadowCompare() const { return shadowCompare_; }
  80. /// Return border color.
  81. const Color& GetBorderColor() const { return borderColor_; }
  82. /// Return whether is using sRGB sampling and writing.
  83. bool GetSRGB() const { return sRGB_; }
  84. /// Return backup texture.
  85. Texture* GetBackupTexture() const { return backupTexture_; }
  86. /// Return mip levels to skip on a quality setting when loading.
  87. int GetMipsToSkip(int quality) const;
  88. /// Return mip level width, or 0 if level does not exist.
  89. int GetLevelWidth(unsigned level) const;
  90. /// Return mip level width, or 0 if level does not exist.
  91. int GetLevelHeight(unsigned level) const;
  92. /// Return texture usage type.
  93. TextureUsage GetUsage() const { return usage_; }
  94. /// Return data size in bytes for a rectangular region.
  95. unsigned GetDataSize(int width, int height) const;
  96. /// Return data size in bytes for a pixel or block row.
  97. unsigned GetRowDataSize(int width) const;
  98. /// Return the non-internal texture format corresponding to an OpenGL internal format.
  99. static unsigned GetExternalFormat(unsigned format);
  100. /// Return the data type corresponding to an OpenGL internal format.
  101. static unsigned GetDataType(unsigned format);
  102. /// Load parameters.
  103. void LoadParameters();
  104. /// Load parameters from an XML file.
  105. void LoadParameters(XMLFile* xml);
  106. /// Load parameters from an XML element.
  107. void LoadParameters(const XMLElement& element);
  108. /// Return the corresponding SRGB texture format if supported. If not supported, return format unchanged.
  109. unsigned GetSRGBFormat(unsigned format);
  110. protected:
  111. /// Check whether texture memory budget has been exceeded. Free unused materials in that case to release the texture references.
  112. void CheckTextureBudget(ShortStringHash type);
  113. /// Create texture.
  114. virtual bool Create() { return true; }
  115. /// Texture usage type.
  116. TextureUsage usage_;
  117. /// OpenGL target.
  118. unsigned target_;
  119. /// Texture format.
  120. unsigned format_;
  121. /// Current mip levels.
  122. unsigned levels_;
  123. /// Requested mip levels.
  124. unsigned requestedLevels_;
  125. /// Texture width.
  126. int width_;
  127. /// Texture height.
  128. int height_;
  129. /// Shadow compare mode, OpenGL only.
  130. bool shadowCompare_;
  131. /// Parameters dirty flag.
  132. bool parametersDirty_;
  133. /// Filtering mode.
  134. TextureFilterMode filterMode_;
  135. /// Addressing mode.
  136. TextureAddressMode addressMode_[MAX_COORDS];
  137. /// Mip levels to skip when loading per texture quality setting.
  138. unsigned mipsToSkip_[MAX_TEXTURE_QUALITY_LEVELS];
  139. /// Border color.
  140. Color borderColor_;
  141. /// sRGB sampling and writing mode flag.
  142. bool sRGB_;
  143. /// Backup texture.
  144. SharedPtr<Texture> backupTexture_;
  145. };
  146. }