OGLTexture.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Oorni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #pragma once
  24. #include "ArrayPtr.h"
  25. #include "Color.h"
  26. #include "GPUObject.h"
  27. #include "GraphicsDefs.h"
  28. #include "Resource.h"
  29. namespace Urho3D
  30. {
  31. static const int MAX_TEXTURE_QUALITY_LEVELS = 3;
  32. class XMLElement;
  33. class XMLFile;
  34. /// Base class for texture resources.
  35. class Texture : public Resource, public GPUObject
  36. {
  37. public:
  38. /// Construct.
  39. Texture(Context* context);
  40. /// Destruct.
  41. virtual ~Texture();
  42. /// Set number of requested mipmap 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, OpenGL only.
  49. void SetShadowCompare(bool enable);
  50. /// Set border color for border addressing mode.
  51. void SetBorderColor(const Color& color);
  52. /// Set backup texture to use when rendering to this texture.
  53. void SetBackupTexture(Texture* texture);
  54. /// Dirty the parameters.
  55. void SetParametersDirty();
  56. /// Update changed parameters to OpenGL. Called by Graphics when binding the texture.
  57. void UpdateParameters();
  58. /// Return texture's OpenGL target.
  59. unsigned GetTarget() const { return target_; }
  60. /// Return texture format.
  61. unsigned GetFormat() const { return format_; }
  62. /// Return whether the texture format is compressed.
  63. bool IsCompressed() const;
  64. /// Return number of mipmap levels.
  65. unsigned GetLevels() const { return levels_; }
  66. /// Return width.
  67. int GetWidth() const { return width_; }
  68. /// Return height.
  69. int GetHeight() const { return height_; }
  70. /// Return whether parameters are dirty.
  71. bool GetParametersDirty() const { return parametersDirty_; }
  72. /// Return filtering mode.
  73. TextureFilterMode GetFilterMode() const { return filterMode_; }
  74. /// Return addressing mode by texture coordinate.
  75. TextureAddressMode GetAddressMode(TextureCoordinate coord) const { return addressMode_[coord]; }
  76. /// Return whether shadow compare is enabled, OpenGL only.
  77. bool GetShadowCompare() const { return shadowCompare_; }
  78. /// Return border color.
  79. const Color& GetBorderColor() const { return borderColor_; }
  80. /// Return backup texture.
  81. Texture* GetBackupTexture() const { return backupTexture_; }
  82. /// Return mip level width, or 0 if level does not exist.
  83. int GetLevelWidth(unsigned level) const;
  84. /// Return mip level width, or 0 if level does not exist.
  85. int GetLevelHeight(unsigned level) const;
  86. /// Return texture usage type.
  87. TextureUsage GetUsage() const;
  88. /// Return data size in bytes for a rectangular region.
  89. unsigned GetDataSize(int width, int height) const;
  90. /// Return data size in bytes for a pixel or block row.
  91. unsigned GetRowDataSize(int width) const;
  92. /// Return the non-internal texture format corresponding to an OpenGL internal format.
  93. static unsigned GetExternalFormat(unsigned format);
  94. /// Return the data type corresponding to an OpenGL internal format.
  95. static unsigned GetDataType(unsigned format);
  96. /// Load parameters.
  97. void LoadParameters();
  98. /// Load parameters from an XML file.
  99. void LoadParameters(XMLFile* xml);
  100. /// Load parameters from an XML element.
  101. void LoadParameters(const XMLElement& elem);
  102. protected:
  103. /// Check whether texture memory budget has been exceeded. Free unused materials in that case to release the texture references.
  104. void CheckTextureBudget(ShortStringHash type);
  105. /// OpenGL target.
  106. unsigned target_;
  107. /// Texture format.
  108. unsigned format_;
  109. /// Current mipmap levels.
  110. unsigned levels_;
  111. /// Requested mipmap levels.
  112. unsigned requestedLevels_;
  113. /// Texture width.
  114. int width_;
  115. /// Texture height.
  116. int height_;
  117. /// Dynamic flag.
  118. bool dynamic_;
  119. /// Shadow compare mode, OpenGL only.
  120. bool shadowCompare_;
  121. /// Parameters dirty flag.
  122. bool parametersDirty_;
  123. /// Filtering mode.
  124. TextureFilterMode filterMode_;
  125. /// Addressing mode.
  126. TextureAddressMode addressMode_[MAX_COORDS];
  127. /// Mipmaps to skip when loading.
  128. unsigned mipsToSkip_[MAX_TEXTURE_QUALITY_LEVELS];
  129. /// Border color.
  130. Color borderColor_;
  131. /// Backup texture.
  132. SharedPtr<Texture> backupTexture_;
  133. };
  134. }