Texture.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. #include "anki/gl/Texture.h"
  2. #include "anki/gl/GlException.h"
  3. #include "anki/util/Exception.h"
  4. #include <boost/lexical_cast.hpp>
  5. namespace anki {
  6. //==============================================================================
  7. // TextureManager =
  8. //==============================================================================
  9. //==============================================================================
  10. TextureManager::TextureManager()
  11. {
  12. GLint tmp;
  13. glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &tmp);
  14. units.resize(tmp, Unit{nullptr, 0});
  15. activeUnit = -1;
  16. mipmapping = true;
  17. anisotropyLevel = 8;
  18. compression = true;
  19. }
  20. //==============================================================================
  21. void TextureManager::activateUnit(uint unit)
  22. {
  23. ANKI_ASSERT(unit < units.size());
  24. if(activeUnit == unit)
  25. {
  26. return;
  27. }
  28. activeUnit = unit;
  29. glActiveTexture(GL_TEXTURE0 + activeUnit);
  30. }
  31. //==============================================================================
  32. uint TextureManager::choseUnit(Texture* tex)
  33. {
  34. // Increase life for all
  35. //
  36. for(Unit& unit : units)
  37. {
  38. ++unit.life;
  39. }
  40. // Already binded
  41. //
  42. if(tex->unit != -1)
  43. {
  44. ANKI_ASSERT(units[tex->unit].tex == tex);
  45. units[tex->unit].life = 0;
  46. return tex->unit;
  47. }
  48. // Find an empty slot for it
  49. //
  50. for(uint i = 0; i < units.size(); i++)
  51. {
  52. if(units[i].tex == nullptr)
  53. {
  54. units[i].tex = tex;
  55. units[i].life = 0;
  56. return i;
  57. }
  58. }
  59. // If all units occupied chose the older
  60. //
  61. // Find the unit with the max life
  62. uint umaxlife = 0;
  63. for(uint i = 1; i < units.size(); ++i)
  64. {
  65. if(units[umaxlife].life < units[i].life)
  66. {
  67. umaxlife = i;
  68. }
  69. }
  70. units[umaxlife].tex->unit = -1;
  71. units[umaxlife].tex = tex;
  72. units[umaxlife].life = 0;
  73. return umaxlife;
  74. }
  75. //==============================================================================
  76. void TextureManager::textureDeleted(Texture* tex)
  77. {
  78. for(auto it = units.begin(); it != units.end(); ++it)
  79. {
  80. if(it->tex == tex)
  81. {
  82. it->tex = nullptr;
  83. return;
  84. }
  85. }
  86. ANKI_ASSERT(0 && "Pointer not found");
  87. }
  88. //==============================================================================
  89. // Texture =
  90. //==============================================================================
  91. //==============================================================================
  92. Texture::Texture()
  93. : glId(std::numeric_limits<uint>::max()), target(GL_TEXTURE_2D), unit(-1)
  94. {}
  95. //==============================================================================
  96. Texture::~Texture()
  97. {
  98. if(isLoaded())
  99. {
  100. glDeleteTextures(1, &glId);
  101. }
  102. TextureManagerSingleton::get().textureDeleted(this);
  103. }
  104. //==============================================================================
  105. void Texture::create(const Initializer& init)
  106. {
  107. // Sanity checks
  108. //
  109. ANKI_ASSERT(!isLoaded());
  110. ANKI_ASSERT(init.internalFormat <= 4 && "Deprecated internal format");
  111. // Create
  112. //
  113. glGenTextures(1, &glId);
  114. target = GL_TEXTURE_2D;
  115. internalFormat = init.internalFormat;
  116. format = init.format;
  117. type = init.type;
  118. width = init.width;
  119. height = init.height;
  120. // Bind
  121. glActiveTexture(GL_TEXTURE0);
  122. glBindTexture(target, glId);
  123. switch(internalFormat)
  124. {
  125. case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
  126. case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
  127. case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
  128. glCompressedTexImage2D(target, 0, internalFormat,
  129. width, height, 0, init.dataSize, init.data);
  130. break;
  131. default:
  132. glTexImage2D(target, 0, internalFormat, width,
  133. height, 0, format, type, init.data);
  134. }
  135. if(init.repeat)
  136. {
  137. glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_REPEAT);
  138. glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_REPEAT);
  139. }
  140. else
  141. {
  142. glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  143. glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  144. }
  145. if(init.mipmapping && init.data)
  146. {
  147. glGenerateMipmap(target);
  148. }
  149. // If not mipmapping then the filtering cannot be trilinear
  150. if(init.filteringType == TFT_TRILINEAR && !init.mipmapping)
  151. {
  152. setFilteringNoBind(TFT_LINEAR);
  153. }
  154. else
  155. {
  156. setFilteringNoBind(init.filteringType);
  157. }
  158. if(init.anisotropyLevel > 1)
  159. {
  160. glTexParameteri(target, GL_TEXTURE_MAX_ANISOTROPY_EXT,
  161. GLint(init.anisotropyLevel));
  162. }
  163. ANKI_CHECK_GL_ERROR();
  164. }
  165. //==============================================================================
  166. void Texture::bind() const
  167. {
  168. unit = TextureManagerSingleton::get().choseUnit(
  169. const_cast<Texture*>(this));
  170. TextureManagerSingleton::get().activateUnit(unit);
  171. glBindTexture(target, glId);
  172. }
  173. //==============================================================================
  174. void Texture::genMipmap()
  175. {
  176. bind();
  177. glGenerateMipmap(target);
  178. }
  179. //==============================================================================
  180. void Texture::setFilteringNoBind(TextureFilteringType filterType) const
  181. {
  182. switch(filterType)
  183. {
  184. case TFT_NEAREST:
  185. glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  186. glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  187. break;
  188. case TFT_LINEAR:
  189. glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  190. glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  191. break;
  192. case TFT_TRILINEAR:
  193. glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  194. glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  195. }
  196. }
  197. } // end namespace