OGLTexture.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 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 XMLFile;
  32. /// Base class for texture resources.
  33. class Texture : public Resource, public GPUObject
  34. {
  35. public:
  36. /// Construct.
  37. Texture(Context* context);
  38. /// Destruct.
  39. virtual ~Texture();
  40. /// Set number of requested mipmap 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 backup texture to use when rendering to this texture.
  51. void SetBackupTexture(Texture* texture);
  52. /// Dirty the parameters.
  53. void SetParametersDirty();
  54. /// Clear data lost flag. No-op on OpenGL.
  55. void ClearDataLost();
  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 number of mipmap levels.
  63. unsigned GetLevels() const { return levels_; }
  64. /// Return OpenGL depth bits.
  65. int GetDepthBits() const { return depthBits_; }
  66. /// Return width.
  67. int GetWidth() const { return width_; }
  68. /// Return height.
  69. int GetHeight() const { return height_; }
  70. /// Return whether data is lost. Always false on OpenGL.
  71. bool IsDataLost() const { return false; }
  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 backup texture.
  83. Texture* GetBackupTexture() const { return backupTexture_; }
  84. /// Return mip level width, or 0 if level does not exist.
  85. int GetLevelWidth(unsigned level) const;
  86. /// Return mip level width, or 0 if level does not exist.
  87. int GetLevelHeight(unsigned level) const;
  88. /// Return texture usage type.
  89. TextureUsage GetUsage() const;
  90. /// Return data size in bytes for a rectangular region.
  91. unsigned GetDataSize(int width, int height) const;
  92. /// Return data size in bytes for a pixel or block row.
  93. unsigned GetRowDataSize(int width) const;
  94. /// Return API-specific compressed texture format.
  95. static unsigned GetDXTFormat(CompressedFormat format);
  96. /// Return the non-internal texture format corresponding to an OpenGL internal format.
  97. static unsigned GetExternalFormat(unsigned format);
  98. /// Return the data type corresponding to an OpenGL internal format.
  99. static unsigned GetDataType(unsigned format);
  100. protected:
  101. /// Load parameters.
  102. void LoadParameters();
  103. /// Load parameters from an XML file.
  104. void LoadParameters(XMLFile* xml);
  105. /// Check whether texture memory budget has been exceeded. Free unused materials in that case to release the texture references.
  106. void CheckTextureBudget(ShortStringHash type);
  107. /// OpenGL target.
  108. unsigned target_;
  109. /// Texture format.
  110. unsigned format_;
  111. /// Current mipmap levels.
  112. unsigned levels_;
  113. /// Requested mipmap levels.
  114. unsigned requestedLevels_;
  115. /// Texture's OpenGL depth bits (depth textures only.)
  116. int depthBits_;
  117. /// Texture width.
  118. int width_;
  119. /// Texture height.
  120. int height_;
  121. /// Dynamic flag.
  122. bool dynamic_;
  123. /// Shadow compare mode, OpenGL only.
  124. bool shadowCompare_;
  125. /// Parameters dirty flag.
  126. bool parametersDirty_;
  127. /// Filtering mode.
  128. TextureFilterMode filterMode_;
  129. /// Addressing mode.
  130. TextureAddressMode addressMode_[MAX_COORDS];
  131. /// Mipmaps to skip when loading.
  132. unsigned mipsToSkip_[MAX_TEXTURE_QUALITY_LEVELS];
  133. /// Border color.
  134. Color borderColor_;
  135. /// Backup texture.
  136. SharedPtr<Texture> backupTexture_;
  137. /// Save data per mip level.
  138. Vector<SharedArrayPtr<unsigned char> > savedLevels_;
  139. };