OGLTexture.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Öörni
  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 "Image.h"
  29. #include "Resource.h"
  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. /// Clear data lost flag. No-op on OpenGL.
  56. void ClearDataLost();
  57. /// Update changed parameters to OpenGL. Called by Graphics when binding the texture.
  58. void UpdateParameters();
  59. /// Return texture's OpenGL target.
  60. unsigned GetTarget() const { return target_; }
  61. /// Return texture format.
  62. unsigned GetFormat() const { return format_; }
  63. /// Return whether the texture format is compressed.
  64. bool IsCompressed() const;
  65. /// Return number of mipmap levels.
  66. unsigned GetLevels() const { return levels_; }
  67. /// Return OpenGL depth bits.
  68. int GetDepthBits() const { return depthBits_; }
  69. /// Return width.
  70. int GetWidth() const { return width_; }
  71. /// Return height.
  72. int GetHeight() const { return height_; }
  73. /// Return whether data is lost. Always false on OpenGL.
  74. bool IsDataLost() const { return false; }
  75. /// Return whether parameters are dirty.
  76. bool GetParametersDirty() const { return parametersDirty_; }
  77. /// Return filtering mode.
  78. TextureFilterMode GetFilterMode() const { return filterMode_; }
  79. /// Return addressing mode by texture coordinate.
  80. TextureAddressMode GetAddressMode(TextureCoordinate coord) const { return addressMode_[coord]; }
  81. /// Return whether shadow compare is enabled, OpenGL only.
  82. bool GetShadowCompare() const { return shadowCompare_; }
  83. /// Return border color.
  84. const Color& GetBorderColor() const { return borderColor_; }
  85. /// Return backup texture.
  86. Texture* GetBackupTexture() const { return backupTexture_; }
  87. /// Return mip level width, or 0 if level does not exist.
  88. int GetLevelWidth(unsigned level) const;
  89. /// Return mip level width, or 0 if level does not exist.
  90. int GetLevelHeight(unsigned level) const;
  91. /// Return texture usage type.
  92. TextureUsage GetUsage() const;
  93. /// Return data size in bytes for a rectangular region.
  94. unsigned GetDataSize(int width, int height) const;
  95. /// Return data size in bytes for a pixel or block row.
  96. unsigned GetRowDataSize(int width) const;
  97. /// Return API-specific compressed texture format.
  98. static unsigned GetDXTFormat(CompressedFormat format);
  99. /// Return the non-internal texture format corresponding to an OpenGL internal format.
  100. static unsigned GetExternalFormat(unsigned format);
  101. /// Return the data type corresponding to an OpenGL internal format.
  102. static unsigned GetDataType(unsigned format);
  103. /// Load parameters.
  104. void LoadParameters();
  105. /// Load parameters from an XML file.
  106. void LoadParameters(XMLFile* xml);
  107. /// Load parameters from an XML element.
  108. void LoadParameters(const XMLElement& elem);
  109. protected:
  110. /// Check whether texture memory budget has been exceeded. Free unused materials in that case to release the texture references.
  111. void CheckTextureBudget(ShortStringHash type);
  112. /// OpenGL target.
  113. unsigned target_;
  114. /// Texture format.
  115. unsigned format_;
  116. /// Current mipmap levels.
  117. unsigned levels_;
  118. /// Requested mipmap levels.
  119. unsigned requestedLevels_;
  120. /// Texture's OpenGL depth bits (depth textures only.)
  121. int depthBits_;
  122. /// Texture width.
  123. int width_;
  124. /// Texture height.
  125. int height_;
  126. /// Dynamic flag.
  127. bool dynamic_;
  128. /// Shadow compare mode, OpenGL only.
  129. bool shadowCompare_;
  130. /// Parameters dirty flag.
  131. bool parametersDirty_;
  132. /// Filtering mode.
  133. TextureFilterMode filterMode_;
  134. /// Addressing mode.
  135. TextureAddressMode addressMode_[MAX_COORDS];
  136. /// Mipmaps to skip when loading.
  137. unsigned mipsToSkip_[MAX_TEXTURE_QUALITY_LEVELS];
  138. /// Border color.
  139. Color borderColor_;
  140. /// Backup texture.
  141. SharedPtr<Texture> backupTexture_;
  142. /// Save data per mip level.
  143. Vector<SharedArrayPtr<unsigned char> > savedLevels_;
  144. };