TextureImpl.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. // Copyright (C) 2009-2021, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include <AnKi/Gr/gl/TextureImpl.h>
  6. #include <AnKi/Gr/Texture.h>
  7. #include <AnKi/Gr/gl/Error.h>
  8. #include <AnKi/Util/Functions.h>
  9. #include <AnKi/Gr/GrManager.h>
  10. #include <AnKi/Gr/gl/GrManagerImpl.h>
  11. #include <AnKi/Gr/gl/TextureViewImpl.h>
  12. #include <AnKi/Gr/gl/RenderingThread.h>
  13. #include <AnKi/Gr/gl/CommandBufferImpl.h>
  14. namespace anki
  15. {
  16. static GLenum convertTextureType(TextureType type)
  17. {
  18. GLenum out = GL_NONE;
  19. switch(type)
  20. {
  21. case TextureType::_1D:
  22. out = GL_TEXTURE_1D;
  23. break;
  24. case TextureType::_2D:
  25. out = GL_TEXTURE_2D;
  26. break;
  27. case TextureType::_3D:
  28. out = GL_TEXTURE_3D;
  29. break;
  30. case TextureType::_2D_ARRAY:
  31. out = GL_TEXTURE_2D_ARRAY;
  32. break;
  33. case TextureType::CUBE:
  34. out = GL_TEXTURE_CUBE_MAP;
  35. break;
  36. case TextureType::CUBE_ARRAY:
  37. out = GL_TEXTURE_CUBE_MAP_ARRAY;
  38. break;
  39. default:
  40. ANKI_ASSERT(0);
  41. };
  42. return out;
  43. }
  44. class DeleteTextureCommand final : public GlCommand
  45. {
  46. public:
  47. GLuint m_tex;
  48. HashMap<TextureSubresourceInfo, MicroTextureView> m_views;
  49. GrAllocator<U8> m_alloc;
  50. DeleteTextureCommand(GLuint tex, HashMap<TextureSubresourceInfo, MicroTextureView>& views, GrAllocator<U8> alloc)
  51. : m_tex(tex)
  52. , m_views(std::move(views))
  53. , m_alloc(alloc)
  54. {
  55. }
  56. Error operator()(GlState& state)
  57. {
  58. // Delete views
  59. auto it = m_views.getBegin();
  60. auto end = m_views.getEnd();
  61. while(it != end)
  62. {
  63. const MicroTextureView& view = *it;
  64. glDeleteTextures(1, &view.m_glName);
  65. ++it;
  66. }
  67. m_views.destroy(m_alloc);
  68. // Delete texture
  69. if(m_tex != 0)
  70. {
  71. glDeleteTextures(1, &m_tex);
  72. }
  73. return Error::NONE;
  74. }
  75. };
  76. TextureImpl::~TextureImpl()
  77. {
  78. GrManager& manager = getManager();
  79. RenderingThread& thread = static_cast<GrManagerImpl&>(manager).getRenderingThread();
  80. if(!thread.isServerThread())
  81. {
  82. CommandBufferPtr commands;
  83. commands = manager.newCommandBuffer(CommandBufferInitInfo());
  84. static_cast<CommandBufferImpl&>(*commands).pushBackNewCommand<DeleteTextureCommand>(m_glName, m_viewsMap,
  85. getAllocator());
  86. static_cast<CommandBufferImpl&>(*commands).flush();
  87. }
  88. else
  89. {
  90. DeleteTextureCommand cmd(m_glName, m_viewsMap, getAllocator());
  91. cmd(static_cast<GrManagerImpl&>(manager).getState());
  92. }
  93. m_glName = 0;
  94. }
  95. void TextureImpl::bind() const
  96. {
  97. glActiveTexture(GL_TEXTURE0);
  98. glBindTexture(m_target, m_glName);
  99. }
  100. void TextureImpl::preInit(const TextureInitInfo& init)
  101. {
  102. ANKI_ASSERT(init.isValid());
  103. m_width = init.m_width;
  104. m_height = init.m_height;
  105. m_depth = init.m_depth;
  106. m_layerCount = init.m_layerCount;
  107. m_target = convertTextureType(init.m_type);
  108. m_texType = init.m_type;
  109. m_format = init.m_format;
  110. m_usage = init.m_usage;
  111. convertTextureInformation(init.m_format, m_compressed, m_glFormat, m_internalFormat, m_glType, m_aspect);
  112. if(m_target != GL_TEXTURE_3D)
  113. {
  114. m_mipCount = min<U>(init.m_mipmapCount, computeMaxMipmapCount2d(m_width, m_height));
  115. }
  116. else
  117. {
  118. m_mipCount = min<U>(init.m_mipmapCount, computeMaxMipmapCount3d(m_width, m_height, m_depth));
  119. }
  120. // Surface count
  121. switch(m_target)
  122. {
  123. case GL_TEXTURE_1D:
  124. case GL_TEXTURE_2D:
  125. case GL_TEXTURE_2D_MULTISAMPLE:
  126. m_surfaceCountPerLevel = 1;
  127. m_faceCount = 1;
  128. break;
  129. case GL_TEXTURE_CUBE_MAP:
  130. m_surfaceCountPerLevel = 6;
  131. m_faceCount = 6;
  132. break;
  133. case GL_TEXTURE_CUBE_MAP_ARRAY:
  134. m_surfaceCountPerLevel = m_layerCount * 6;
  135. m_faceCount = 6;
  136. break;
  137. case GL_TEXTURE_2D_ARRAY:
  138. m_surfaceCountPerLevel = m_layerCount;
  139. m_faceCount = 1;
  140. break;
  141. case GL_TEXTURE_3D:
  142. m_surfaceCountPerLevel = m_depth;
  143. m_faceCount = 1;
  144. break;
  145. default:
  146. ANKI_ASSERT(0);
  147. }
  148. }
  149. void TextureImpl::init(const TextureInitInfo& init)
  150. {
  151. ANKI_ASSERT(!isCreated());
  152. GrAllocator<U8> alloc = getAllocator();
  153. // Create
  154. //
  155. glGenTextures(1, &m_glName);
  156. ANKI_ASSERT(m_glName != 0);
  157. bind();
  158. // Create storage
  159. switch(m_target)
  160. {
  161. case GL_TEXTURE_2D:
  162. case GL_TEXTURE_CUBE_MAP:
  163. glTexStorage2D(m_target, m_mipCount, m_internalFormat, m_width, m_height);
  164. break;
  165. case GL_TEXTURE_CUBE_MAP_ARRAY:
  166. glTexStorage3D(m_target, m_mipCount, m_internalFormat, m_width, m_height, m_layerCount * 6);
  167. break;
  168. case GL_TEXTURE_2D_ARRAY:
  169. glTexStorage3D(m_target, m_mipCount, m_internalFormat, m_width, m_height, m_layerCount);
  170. break;
  171. case GL_TEXTURE_3D:
  172. glTexStorage3D(m_target, m_mipCount, m_internalFormat, m_width, m_height, m_depth);
  173. break;
  174. case GL_TEXTURE_2D_MULTISAMPLE:
  175. glTexStorage2DMultisample(m_target, init.m_samples, m_internalFormat, m_width, m_height, GL_FALSE);
  176. break;
  177. default:
  178. ANKI_ASSERT(0);
  179. }
  180. // Make sure that the texture is complete
  181. glTexParameteri(m_target, GL_TEXTURE_MAX_LEVEL, m_mipCount - 1);
  182. ANKI_CHECK_GL_ERROR();
  183. }
  184. void TextureImpl::copyFromBuffer(const TextureSubresourceInfo& subresource, GLuint pbo, PtrSize offset,
  185. PtrSize dataSize) const
  186. {
  187. ANKI_ASSERT(isSubresourceGoodForCopyFromBuffer(subresource));
  188. ANKI_ASSERT(dataSize > 0);
  189. const U mipmap = subresource.m_firstMipmap;
  190. const U w = m_width >> mipmap;
  191. const U h = m_height >> mipmap;
  192. const U d = m_depth >> mipmap;
  193. ANKI_ASSERT(w > 0);
  194. ANKI_ASSERT(h > 0);
  195. bind();
  196. glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbo);
  197. const void* ptrOffset = numberToPtr<const void*>(offset);
  198. switch(m_target)
  199. {
  200. case GL_TEXTURE_2D:
  201. if(!m_compressed)
  202. {
  203. glTexSubImage2D(m_target, mipmap, 0, 0, w, h, m_glFormat, m_glType, ptrOffset);
  204. }
  205. else
  206. {
  207. glCompressedTexSubImage2D(m_target, mipmap, 0, 0, w, h, m_glFormat, dataSize, ptrOffset);
  208. }
  209. break;
  210. case GL_TEXTURE_CUBE_MAP:
  211. {
  212. const U surfIdx = computeSurfaceIdx(TextureSurfaceInfo(mipmap, 0, subresource.m_firstFace, 0));
  213. if(!m_compressed)
  214. {
  215. glTexSubImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + surfIdx, mipmap, 0, 0, w, h, m_glFormat, m_glType,
  216. ptrOffset);
  217. }
  218. else
  219. {
  220. glCompressedTexSubImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + surfIdx, mipmap, 0, 0, w, h, m_glFormat,
  221. dataSize, ptrOffset);
  222. }
  223. break;
  224. }
  225. case GL_TEXTURE_2D_ARRAY:
  226. {
  227. const U surfIdx = computeSurfaceIdx(TextureSurfaceInfo(mipmap, 0, 0, subresource.m_firstLayer));
  228. if(!m_compressed)
  229. {
  230. glTexSubImage3D(m_target, mipmap, 0, 0, surfIdx, w, h, 1, m_glFormat, m_glType, ptrOffset);
  231. }
  232. else
  233. {
  234. glCompressedTexSubImage3D(m_target, mipmap, 0, 0, surfIdx, w, h, 1, m_glFormat, dataSize, ptrOffset);
  235. }
  236. break;
  237. }
  238. case GL_TEXTURE_3D:
  239. ANKI_ASSERT(d > 0);
  240. if(!m_compressed)
  241. {
  242. glTexSubImage3D(m_target, mipmap, 0, 0, 0, w, h, d, m_glFormat, m_glType, ptrOffset);
  243. }
  244. else
  245. {
  246. glCompressedTexSubImage3D(m_target, mipmap, 0, 0, 0, w, h, d, m_glFormat, dataSize, ptrOffset);
  247. }
  248. break;
  249. default:
  250. ANKI_ASSERT(0);
  251. }
  252. glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
  253. ANKI_CHECK_GL_ERROR();
  254. }
  255. void TextureImpl::generateMipmaps2d(const TextureViewImpl& view) const
  256. {
  257. ANKI_ASSERT(view.m_tex.get() == this);
  258. ANKI_ASSERT(isSubresourceGoodForMipmapGeneration(view.getSubresource()));
  259. ANKI_ASSERT(!m_compressed);
  260. if(m_surfaceCountPerLevel > 1)
  261. {
  262. glGenerateTextureMipmap(view.m_view.m_glName);
  263. }
  264. else
  265. {
  266. glGenerateTextureMipmap(m_glName);
  267. }
  268. }
  269. void TextureImpl::clear(const TextureSubresourceInfo& subresource, const ClearValue& clearValue) const
  270. {
  271. ANKI_ASSERT(isCreated());
  272. ANKI_ASSERT(isSubresourceValid(subresource));
  273. ANKI_ASSERT(m_texType != TextureType::_3D && "TODO");
  274. // Find the aspect to clear
  275. const DepthStencilAspectBit aspect = subresource.m_depthStencilAspect;
  276. GLenum format;
  277. if(aspect == DepthStencilAspectBit::DEPTH)
  278. {
  279. ANKI_ASSERT(m_glFormat == GL_DEPTH_COMPONENT || m_glFormat == GL_DEPTH_STENCIL);
  280. format = GL_DEPTH_COMPONENT;
  281. }
  282. else if(aspect == DepthStencilAspectBit::STENCIL)
  283. {
  284. ANKI_ASSERT(m_glFormat == GL_STENCIL_INDEX || m_glFormat == GL_DEPTH_STENCIL);
  285. format = GL_STENCIL_INDEX;
  286. }
  287. else if(aspect == DepthStencilAspectBit::DEPTH_STENCIL)
  288. {
  289. ANKI_ASSERT(m_glFormat == GL_DEPTH_STENCIL);
  290. format = GL_DEPTH_STENCIL;
  291. }
  292. else
  293. {
  294. format = m_glFormat;
  295. }
  296. for(U mip = subresource.m_firstMipmap; mip < subresource.m_firstMipmap + subresource.m_mipmapCount; ++mip)
  297. {
  298. for(U face = subresource.m_firstFace; face < subresource.m_firstFace + subresource.m_faceCount; ++face)
  299. {
  300. for(U layer = subresource.m_firstLayer; layer < subresource.m_firstLayer + subresource.m_layerCount;
  301. ++layer)
  302. {
  303. const U surfaceIdx = computeSurfaceIdx(TextureSurfaceInfo(mip, 0, face, layer));
  304. const U width = m_width >> mip;
  305. const U height = m_height >> mip;
  306. glClearTexSubImage(m_glName, mip, 0, 0, surfaceIdx, width, height, 1, format, GL_FLOAT,
  307. &clearValue.m_colorf[0]);
  308. }
  309. }
  310. }
  311. }
  312. U TextureImpl::computeSurfaceIdx(const TextureSurfaceInfo& surf) const
  313. {
  314. U out;
  315. if(m_target == GL_TEXTURE_3D)
  316. {
  317. // Check depth for this level
  318. ANKI_ASSERT(surf.m_depth < (m_depth >> surf.m_level));
  319. out = surf.m_depth;
  320. }
  321. else
  322. {
  323. out = m_faceCount * surf.m_layer + surf.m_face;
  324. }
  325. ANKI_ASSERT(out < m_surfaceCountPerLevel);
  326. return out;
  327. }
  328. MicroTextureView TextureImpl::getOrCreateView(const TextureSubresourceInfo& subresource) const
  329. {
  330. // Quick opt: Check if the subresource refers to the whole tex
  331. TextureSubresourceInfo wholeTexSubresource;
  332. wholeTexSubresource.m_mipmapCount = getMipmapCount();
  333. wholeTexSubresource.m_faceCount = textureTypeIsCube(getTextureType()) ? 6 : 1;
  334. wholeTexSubresource.m_layerCount = getLayerCount();
  335. wholeTexSubresource.m_depthStencilAspect = getDepthStencilAspect();
  336. if(subresource == wholeTexSubresource)
  337. {
  338. MicroTextureView view{getGlName(), wholeTexSubresource.m_depthStencilAspect};
  339. return view;
  340. }
  341. // Continue with the regular init
  342. LockGuard<Mutex> lock(m_viewsMapMtx);
  343. auto it = m_viewsMap.find(subresource);
  344. if(it != m_viewsMap.getEnd())
  345. {
  346. return *it;
  347. }
  348. else
  349. {
  350. // Create a new view
  351. // Compute the new target if needed
  352. const TextureType newTexType = computeNewTexTypeOfSubresource(subresource);
  353. GLenum glTarget = m_target;
  354. if(newTexType == TextureType::_2D)
  355. {
  356. // Change that anyway
  357. glTarget = GL_TEXTURE_2D;
  358. }
  359. const U firstSurf = computeSurfaceIdx(
  360. TextureSurfaceInfo(subresource.m_firstMipmap, 0, subresource.m_firstFace, subresource.m_firstLayer));
  361. const U lastSurf = computeSurfaceIdx(
  362. TextureSurfaceInfo(subresource.m_firstMipmap, 0, subresource.m_firstFace + subresource.m_faceCount - 1,
  363. subresource.m_firstLayer + subresource.m_layerCount - 1));
  364. ANKI_ASSERT(firstSurf <= lastSurf);
  365. MicroTextureView view;
  366. view.m_aspect = subresource.m_depthStencilAspect;
  367. glGenTextures(1, &view.m_glName);
  368. glTextureView(view.m_glName, glTarget, m_glName, m_internalFormat, subresource.m_firstMipmap,
  369. subresource.m_mipmapCount, firstSurf, lastSurf - firstSurf + 1);
  370. m_viewsMap.emplace(getAllocator(), subresource, view);
  371. return view;
  372. }
  373. }
  374. } // end namespace anki