Texture.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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, 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. // Sanity checks
  91. //
  92. ANKI_ASSERT(!isLoaded());
  93. ANKI_ASSERT(init.internalFormat <= 4 && "Deprecated internal format");
  94. // Create
  95. //
  96. glGenTextures(1, &glId);
  97. target = GL_TEXTURE_2D;
  98. internalFormat = init.internalFormat;
  99. format = init.format;
  100. type = init.type;
  101. width = init.width;
  102. height = init.height;
  103. // Bind
  104. glActiveTexture(GL_TEXTURE0);
  105. glBindTexture(target, glId);
  106. switch(internalFormat)
  107. {
  108. case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
  109. case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
  110. case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
  111. glCompressedTexImage2D(target, 0, internalFormat,
  112. width, height, 0, init.dataSize, init.data);
  113. break;
  114. default:
  115. glTexImage2D(target, 0, internalFormat, width,
  116. height, 0, format, type, init.data);
  117. }
  118. if(init.repeat)
  119. {
  120. glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_REPEAT);
  121. glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_REPEAT);
  122. }
  123. else
  124. {
  125. glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  126. glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  127. }
  128. if(init.mipmapping && init.data)
  129. {
  130. glGenerateMipmap(target);
  131. }
  132. // If not mipmapping then the filtering cannot be trilinear
  133. if(init.filteringType == TFT_TRILINEAR && !init.mipmapping)
  134. {
  135. setFilteringNoBind(TFT_LINEAR);
  136. }
  137. else
  138. {
  139. setFilteringNoBind(init.filteringType);
  140. }
  141. if(init.anisotropyLevel > 1)
  142. {
  143. glTexParameteri(target, GL_TEXTURE_MAX_ANISOTROPY_EXT,
  144. GLint(init.anisotropyLevel));
  145. }
  146. ANKI_CHECK_GL_ERROR();
  147. }
  148. //==============================================================================
  149. void Texture::bind() const
  150. {
  151. unit = TextureManagerSingleton::get().choseUnit(
  152. const_cast<Texture*>(this));
  153. TextureManagerSingleton::get().activateUnit(unit);
  154. glBindTexture(target, glId);
  155. }
  156. //==============================================================================
  157. void Texture::genMipmap()
  158. {
  159. bind();
  160. glGenerateMipmap(target);
  161. }
  162. //==============================================================================
  163. void Texture::setFilteringNoBind(TextureFilteringType filterType) const
  164. {
  165. switch(filterType)
  166. {
  167. case TFT_NEAREST:
  168. glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  169. glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  170. break;
  171. case TFT_LINEAR:
  172. glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  173. glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  174. break;
  175. case TFT_TRILINEAR:
  176. glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  177. glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  178. }
  179. }
  180. } // end namespace