Texture.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 - 1, nullptr);
  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. // Already binded
  35. //
  36. if(tex->unit != -1)
  37. {
  38. ANKI_ASSERT(units[tex->unit] == tex);
  39. return tex->unit;
  40. }
  41. // Find an empty slot for it
  42. //
  43. for(uint i = 0; i < units.size(); i++)
  44. {
  45. if(units[i] == nullptr)
  46. {
  47. units[i] = tex;
  48. return i;
  49. }
  50. }
  51. // If all units occupied chose a random for now
  52. //
  53. int tmp = rand() % units.size();
  54. units[tmp]->unit = -1;
  55. units[tmp] = tex;
  56. return tmp;
  57. }
  58. //==============================================================================
  59. void TextureManager::textureDeleted(Texture* tex)
  60. {
  61. for(auto it = units.begin(); it != units.end(); ++it)
  62. {
  63. if(*it == tex)
  64. {
  65. *it = nullptr;
  66. return;
  67. }
  68. }
  69. ANKI_ASSERT(0 && "Pointer not found");
  70. }
  71. //==============================================================================
  72. // Texture =
  73. //==============================================================================
  74. //==============================================================================
  75. Texture::Texture()
  76. : glId(std::numeric_limits<uint>::max()), target(GL_TEXTURE_2D), unit(-1)
  77. {}
  78. //==============================================================================
  79. Texture::~Texture()
  80. {
  81. if(isLoaded())
  82. {
  83. glDeleteTextures(1, &glId);
  84. }
  85. TextureManagerSingleton::get().textureDeleted(this);
  86. }
  87. //==============================================================================
  88. void Texture::create(const Initializer& init)
  89. {
  90. TextureManagerSingleton::get().lock();
  91. // Sanity checks
  92. //
  93. ANKI_ASSERT(!isLoaded());
  94. ANKI_ASSERT(init.internalFormat <= 4 && "Deprecated internal format");
  95. // Create
  96. //
  97. glGenTextures(1, &glId);
  98. target = GL_TEXTURE_2D;
  99. internalFormat = init.internalFormat;
  100. format = init.format;
  101. type = init.type;
  102. width = init.width;
  103. height = init.height;
  104. // Bind
  105. glActiveTexture(GL_TEXTURE0
  106. + TextureManagerSingleton::get().getMaxUnitsCount() - 1);
  107. glBindTexture(target, glId);
  108. switch(internalFormat)
  109. {
  110. case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
  111. case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
  112. case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
  113. glCompressedTexImage2D(target, 0, internalFormat,
  114. width, height, 0, init.dataSize, init.data);
  115. break;
  116. default:
  117. glTexImage2D(target, 0, internalFormat, width,
  118. height, 0, format, type, init.data);
  119. }
  120. if(init.repeat)
  121. {
  122. glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_REPEAT);
  123. glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_REPEAT);
  124. }
  125. else
  126. {
  127. glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  128. glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  129. }
  130. if(init.mipmapping && init.data)
  131. {
  132. glGenerateMipmap(target);
  133. }
  134. // If not mipmapping then the filtering cannot be trilinear
  135. if(init.filteringType == TFT_TRILINEAR && !init.mipmapping)
  136. {
  137. setFilteringNoBind(TFT_LINEAR);
  138. }
  139. else
  140. {
  141. setFilteringNoBind(init.filteringType);
  142. }
  143. if(init.anisotropyLevel > 1)
  144. {
  145. glTexParameteri(target, GL_TEXTURE_MAX_ANISOTROPY_EXT,
  146. GLint(init.anisotropyLevel));
  147. }
  148. TextureManagerSingleton::get().unlock();
  149. ANKI_CHECK_GL_ERROR();
  150. }
  151. //==============================================================================
  152. void Texture::bind() const
  153. {
  154. unit = TextureManagerSingleton::get().choseUnit(
  155. const_cast<Texture*>(this));
  156. TextureManagerSingleton::get().activateUnit(unit);
  157. glBindTexture(target, glId);
  158. }
  159. //==============================================================================
  160. void Texture::genMipmap()
  161. {
  162. bind();
  163. glGenerateMipmap(target);
  164. }
  165. //==============================================================================
  166. void Texture::setFilteringNoBind(TextureFilteringType filterType) const
  167. {
  168. switch(filterType)
  169. {
  170. case TFT_NEAREST:
  171. glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  172. glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  173. break;
  174. case TFT_LINEAR:
  175. glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  176. glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  177. break;
  178. case TFT_TRILINEAR:
  179. glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  180. glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  181. }
  182. }
  183. } // end namespace