OGLTexture.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 "ArrayPtr.h"
  24. #include "Color.h"
  25. #include "GPUObject.h"
  26. #include "GraphicsDefs.h"
  27. #include "Resource.h"
  28. namespace Urho3D
  29. {
  30. static const int MAX_TEXTURE_QUALITY_LEVELS = 3;
  31. class XMLElement;
  32. class XMLFile;
  33. /// Base class for texture resources.
  34. class Texture : public Resource, public GPUObject
  35. {
  36. public:
  37. /// Construct.
  38. Texture(Context* context);
  39. /// Destruct.
  40. virtual ~Texture();
  41. /// Set number of requested mipmap 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, OpenGL only.
  48. void SetShadowCompare(bool enable);
  49. /// Set border color for border addressing mode.
  50. void SetBorderColor(const Color& color);
  51. /// Set backup texture to use when rendering to this texture.
  52. void SetBackupTexture(Texture* texture);
  53. /// Dirty the parameters.
  54. void SetParametersDirty();
  55. /// Update changed parameters to OpenGL. Called by Graphics when binding the texture.
  56. void UpdateParameters();
  57. /// Return texture's OpenGL target.
  58. unsigned GetTarget() const { return target_; }
  59. /// Return texture format.
  60. unsigned GetFormat() const { return format_; }
  61. /// Return whether the texture format is compressed.
  62. bool IsCompressed() const;
  63. /// Return number of mipmap 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 whether parameters are dirty.
  70. bool GetParametersDirty() const { return parametersDirty_; }
  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 whether shadow compare is enabled, OpenGL only.
  76. bool GetShadowCompare() const { return shadowCompare_; }
  77. /// Return border color.
  78. const Color& GetBorderColor() const { return borderColor_; }
  79. /// Return backup texture.
  80. Texture* GetBackupTexture() const { return backupTexture_; }
  81. /// Return mip level width, or 0 if level does not exist.
  82. int GetLevelWidth(unsigned level) const;
  83. /// Return mip level width, or 0 if level does not exist.
  84. int GetLevelHeight(unsigned level) const;
  85. /// Return texture usage type.
  86. TextureUsage GetUsage() const;
  87. /// Return data size in bytes for a rectangular region.
  88. unsigned GetDataSize(int width, int height) const;
  89. /// Return data size in bytes for a pixel or block row.
  90. unsigned GetRowDataSize(int width) const;
  91. /// Return the non-internal texture format corresponding to an OpenGL internal format.
  92. static unsigned GetExternalFormat(unsigned format);
  93. /// Return the data type corresponding to an OpenGL internal format.
  94. static unsigned GetDataType(unsigned format);
  95. /// Load parameters.
  96. void LoadParameters();
  97. /// Load parameters from an XML file.
  98. void LoadParameters(XMLFile* xml);
  99. /// Load parameters from an XML element.
  100. void LoadParameters(const XMLElement& elem);
  101. protected:
  102. /// Check whether texture memory budget has been exceeded. Free unused materials in that case to release the texture references.
  103. void CheckTextureBudget(ShortStringHash type);
  104. /// OpenGL target.
  105. unsigned target_;
  106. /// Texture format.
  107. unsigned format_;
  108. /// Current mipmap levels.
  109. unsigned levels_;
  110. /// Requested mipmap levels.
  111. unsigned requestedLevels_;
  112. /// Texture width.
  113. int width_;
  114. /// Texture height.
  115. int height_;
  116. /// Dynamic flag.
  117. bool dynamic_;
  118. /// Shadow compare mode, OpenGL only.
  119. bool shadowCompare_;
  120. /// Parameters dirty flag.
  121. bool parametersDirty_;
  122. /// Filtering mode.
  123. TextureFilterMode filterMode_;
  124. /// Addressing mode.
  125. TextureAddressMode addressMode_[MAX_COORDS];
  126. /// Mipmaps to skip when loading.
  127. unsigned mipsToSkip_[MAX_TEXTURE_QUALITY_LEVELS];
  128. /// Border color.
  129. Color borderColor_;
  130. /// Backup texture.
  131. SharedPtr<Texture> backupTexture_;
  132. };
  133. }