OGLTexture.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 "Color.h"
  25. #include "GPUObject.h"
  26. #include "Image.h"
  27. #include "GraphicsDefs.h"
  28. #include "Resource.h"
  29. static const int MAX_TEXTURE_QUALITY_LEVELS = 3;
  30. class XMLFile;
  31. /// Base class for texture resources
  32. class Texture : public Resource, public GPUObject
  33. {
  34. public:
  35. /// Construct
  36. Texture(Context* context);
  37. /// Destruct
  38. virtual ~Texture();
  39. /// Set number of requested mipmap levels. Needs to be called before setting size
  40. void SetNumLevels(unsigned levels);
  41. /// Set filtering mode
  42. void SetFilterMode(TextureFilterMode filter);
  43. /// Set addressing mode by texture coordinate
  44. void SetAddressMode(TextureCoordinate coord, TextureAddressMode address);
  45. /// Set shadow compare mode, OpenGL only
  46. void SetShadowCompare(bool enable);
  47. /// Set border color for border addressing mode
  48. void SetBorderColor(const Color& color);
  49. /// Set backup texture to use when rendering to this texture
  50. void SetBackupTexture(Texture* texture);
  51. /// Dirty the parameters
  52. void SetParametersDirty();
  53. /// Clear data lost flag. No-op on OpenGL
  54. void ClearDataLost();
  55. /// Update changed parameters to OpenGL. Called by Graphics when binding the texture
  56. void UpdateParameters();
  57. /// Return texture usage type
  58. TextureUsage GetUsage() const;
  59. /// Return texture's OpenGL type
  60. unsigned GetTextureType() const { return textureType_; }
  61. /// Return texture format
  62. unsigned GetFormat() const { return format_; }
  63. /// Return number of mipmap levels
  64. unsigned GetLevels() const { return levels_; }
  65. /// Return OpenGL depth bits
  66. int GetDepthBits() const { return depthBits_; }
  67. /// Return width
  68. int GetWidth() const { return width_; }
  69. /// Return height
  70. int GetHeight() const { return height_; }
  71. /// Return whether data is lost. Always false on OpenGL
  72. bool IsDataLost() const { return false; }
  73. /// Return whether parameters are dirty
  74. bool GetParametersDirty() const { return parametersDirty_; }
  75. /// Return filtering mode
  76. TextureFilterMode GetFilterMode() const { return filterMode_; }
  77. /// Return addressing mode by texture coordinate
  78. TextureAddressMode GetAddressMode(TextureCoordinate coord) const { return addressMode_[coord]; }
  79. /// Return whether shadow compare is enabled, OpenGL only
  80. bool GetShadowCompare() const { return shadowCompare_; }
  81. /// Return border color
  82. const Color& GetBorderColor() const { return borderColor_; }
  83. /// Return backup texture
  84. Texture* GetBackupTexture() const { return backupTexture_; }
  85. /// Return API-specific compressed texture format
  86. static unsigned GetDXTFormat(CompressedFormat format);
  87. /// Return the non-internal texture format corresponding to an OpenGL internal format
  88. static unsigned GetExternalFormat(unsigned format);
  89. /// Return the data type corresponding to an OpenGL internal format
  90. static unsigned GetDataType(unsigned format);
  91. protected:
  92. /// Load parameters
  93. void LoadParameters();
  94. /// Load parameters from an XML file
  95. void LoadParameters(XMLFile* xml);
  96. /// Check whether texture memory budget has been exceeded. Free unused materials in that case to release the texture references
  97. void CheckTextureBudget(ShortStringHash type);
  98. /// Texture's OpenGL type
  99. unsigned textureType_;
  100. /// Texture format
  101. unsigned format_;
  102. /// Current mipmap levels
  103. unsigned levels_;
  104. /// Requested mipmap levels
  105. unsigned requestedLevels_;
  106. /// Texture's OpenGL depth bits (depth textures only)
  107. int depthBits_;
  108. /// Texture width
  109. int width_;
  110. /// Texture height
  111. int height_;
  112. /// Dynamic flag
  113. bool dynamic_;
  114. /// Shadow compare mode, OpenGL only
  115. bool shadowCompare_;
  116. /// Parameters dirty flag
  117. bool parametersDirty_;
  118. /// Filtering mode
  119. TextureFilterMode filterMode_;
  120. /// Addressing mode
  121. TextureAddressMode addressMode_[MAX_COORDS];
  122. /// Mipmaps to skip when loading
  123. unsigned mipsToSkip_[MAX_TEXTURE_QUALITY_LEVELS];
  124. /// Border color
  125. Color borderColor_;
  126. /// Backup texture
  127. SharedPtr<Texture> backupTexture_;
  128. };