ImageResource.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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/Resource/ImageResource.h>
  6. #include <AnKi/Resource/ImageLoader.h>
  7. #include <AnKi/Resource/ResourceManager.h>
  8. #include <AnKi/Resource/AsyncLoader.h>
  9. #include <AnKi/Util/Filesystem.h>
  10. namespace anki {
  11. class ImageResource::LoadingContext
  12. {
  13. public:
  14. ImageLoader m_loader;
  15. U32 m_faces = 0;
  16. U32 m_layerCount = 0;
  17. GrManager* m_gr ANKI_DEBUG_CODE(= nullptr);
  18. TransferGpuAllocator* m_trfAlloc ANKI_DEBUG_CODE(= nullptr);
  19. TextureType m_texType;
  20. TexturePtr m_tex;
  21. LoadingContext(GenericMemoryPoolAllocator<U8> alloc)
  22. : m_loader(alloc)
  23. {
  24. }
  25. };
  26. /// Image upload async task.
  27. class ImageResource::TexUploadTask : public AsyncLoaderTask
  28. {
  29. public:
  30. ImageResource::LoadingContext m_ctx;
  31. TexUploadTask(GenericMemoryPoolAllocator<U8> alloc)
  32. : m_ctx(alloc)
  33. {
  34. }
  35. Error operator()(AsyncLoaderTaskContext& ctx) final
  36. {
  37. return ImageResource::load(m_ctx);
  38. }
  39. };
  40. ImageResource::~ImageResource()
  41. {
  42. }
  43. Error ImageResource::load(const ResourceFilename& filename, Bool async)
  44. {
  45. TexUploadTask* task;
  46. LoadingContext* ctx;
  47. LoadingContext localCtx(getTempAllocator());
  48. if(async)
  49. {
  50. task = getManager().getAsyncLoader().newTask<TexUploadTask>(getManager().getAsyncLoader().getAllocator());
  51. ctx = &task->m_ctx;
  52. }
  53. else
  54. {
  55. task = nullptr;
  56. ctx = &localCtx;
  57. }
  58. ImageLoader& loader = ctx->m_loader;
  59. StringAuto filenameExt(getTempAllocator());
  60. getFilepathFilename(filename, filenameExt);
  61. TextureInitInfo init(filenameExt);
  62. init.m_usage = TextureUsageBit::ALL_SAMPLED | TextureUsageBit::TRANSFER_DESTINATION;
  63. init.m_initialUsage = TextureUsageBit::ALL_SAMPLED;
  64. U32 faces = 0;
  65. ResourceFilePtr file;
  66. ANKI_CHECK(openFile(filename, file));
  67. ANKI_CHECK(loader.load(file, filename, getManager().getMaxImageSize()));
  68. // Various sizes
  69. init.m_width = loader.getWidth();
  70. init.m_height = loader.getHeight();
  71. switch(loader.getImageType())
  72. {
  73. case ImageBinaryType::_2D:
  74. init.m_type = TextureType::_2D;
  75. init.m_depth = 1;
  76. faces = 1;
  77. init.m_layerCount = 1;
  78. break;
  79. case ImageBinaryType::CUBE:
  80. init.m_type = TextureType::CUBE;
  81. init.m_depth = 1;
  82. faces = 6;
  83. init.m_layerCount = 1;
  84. break;
  85. case ImageBinaryType::_2D_ARRAY:
  86. init.m_type = TextureType::_2D_ARRAY;
  87. init.m_layerCount = loader.getLayerCount();
  88. init.m_depth = 1;
  89. faces = 1;
  90. break;
  91. case ImageBinaryType::_3D:
  92. init.m_type = TextureType::_3D;
  93. init.m_depth = loader.getDepth();
  94. init.m_layerCount = 1;
  95. faces = 1;
  96. break;
  97. default:
  98. ANKI_ASSERT(0);
  99. }
  100. // Internal format
  101. if(loader.getColorFormat() == ImageBinaryColorFormat::RGB8)
  102. {
  103. switch(loader.getCompression())
  104. {
  105. case ImageBinaryDataCompression::RAW:
  106. init.m_format = Format::R8G8B8_UNORM;
  107. break;
  108. case ImageBinaryDataCompression::S3TC:
  109. init.m_format = Format::BC1_RGB_UNORM_BLOCK;
  110. break;
  111. case ImageBinaryDataCompression::ASTC:
  112. if(loader.getAstcBlockSize() == UVec2(4u))
  113. {
  114. init.m_format = Format::ASTC_4x4_UNORM_BLOCK;
  115. }
  116. else
  117. {
  118. ANKI_ASSERT(loader.getAstcBlockSize() == UVec2(8u));
  119. init.m_format = Format::ASTC_8x8_UNORM_BLOCK;
  120. }
  121. break;
  122. default:
  123. ANKI_ASSERT(0);
  124. }
  125. }
  126. else if(loader.getColorFormat() == ImageBinaryColorFormat::RGBA8)
  127. {
  128. switch(loader.getCompression())
  129. {
  130. case ImageBinaryDataCompression::RAW:
  131. init.m_format = Format::R8G8B8A8_UNORM;
  132. break;
  133. case ImageBinaryDataCompression::S3TC:
  134. init.m_format = Format::BC3_UNORM_BLOCK;
  135. break;
  136. case ImageBinaryDataCompression::ASTC:
  137. if(loader.getAstcBlockSize() == UVec2(4u))
  138. {
  139. init.m_format = Format::ASTC_4x4_UNORM_BLOCK;
  140. }
  141. else
  142. {
  143. ANKI_ASSERT(loader.getAstcBlockSize() == UVec2(8u));
  144. init.m_format = Format::ASTC_8x8_UNORM_BLOCK;
  145. }
  146. break;
  147. default:
  148. ANKI_ASSERT(0);
  149. }
  150. }
  151. else
  152. {
  153. ANKI_ASSERT(0);
  154. }
  155. // mipmapsCount
  156. init.m_mipmapCount = U8(loader.getMipmapCount());
  157. // Create the texture
  158. m_tex = getManager().getGrManager().newTexture(init);
  159. // Set the context
  160. ctx->m_faces = faces;
  161. ctx->m_layerCount = init.m_layerCount;
  162. ctx->m_gr = &getManager().getGrManager();
  163. ctx->m_trfAlloc = &getManager().getTransferGpuAllocator();
  164. ctx->m_texType = init.m_type;
  165. ctx->m_tex = m_tex;
  166. // Upload the data
  167. if(async)
  168. {
  169. getManager().getAsyncLoader().submitTask(task);
  170. }
  171. else
  172. {
  173. ANKI_CHECK(load(*ctx));
  174. }
  175. m_size = UVec3(init.m_width, init.m_height, init.m_depth);
  176. m_layerCount = init.m_layerCount;
  177. // Create the texture view
  178. TextureViewInitInfo viewInit(m_tex, "Rsrc");
  179. m_texView = getManager().getGrManager().newTextureView(viewInit);
  180. return Error::NONE;
  181. }
  182. Error ImageResource::load(LoadingContext& ctx)
  183. {
  184. const U32 copyCount = ctx.m_layerCount * ctx.m_faces * ctx.m_loader.getMipmapCount();
  185. for(U32 b = 0; b < copyCount; b += MAX_COPIES_BEFORE_FLUSH)
  186. {
  187. const U32 begin = b;
  188. const U32 end = min(copyCount, b + MAX_COPIES_BEFORE_FLUSH);
  189. CommandBufferInitInfo ci;
  190. ci.m_flags = CommandBufferFlag::GENERAL_WORK | CommandBufferFlag::SMALL_BATCH;
  191. CommandBufferPtr cmdb = ctx.m_gr->newCommandBuffer(ci);
  192. // Set the barriers of the batch
  193. for(U32 i = begin; i < end; ++i)
  194. {
  195. U32 mip, layer, face;
  196. unflatten3dArrayIndex(ctx.m_layerCount, ctx.m_faces, ctx.m_loader.getMipmapCount(), i, layer, face, mip);
  197. if(ctx.m_texType == TextureType::_3D)
  198. {
  199. TextureVolumeInfo vol(mip);
  200. cmdb->setTextureVolumeBarrier(ctx.m_tex, TextureUsageBit::NONE, TextureUsageBit::TRANSFER_DESTINATION,
  201. vol);
  202. }
  203. else
  204. {
  205. TextureSurfaceInfo surf(mip, 0, face, layer);
  206. cmdb->setTextureSurfaceBarrier(ctx.m_tex, TextureUsageBit::NONE, TextureUsageBit::TRANSFER_DESTINATION,
  207. surf);
  208. }
  209. }
  210. // Do the copies
  211. Array<TransferGpuAllocatorHandle, MAX_COPIES_BEFORE_FLUSH> handles;
  212. U32 handleCount = 0;
  213. for(U32 i = begin; i < end; ++i)
  214. {
  215. U32 mip, layer, face;
  216. unflatten3dArrayIndex(ctx.m_layerCount, ctx.m_faces, ctx.m_loader.getMipmapCount(), i, layer, face, mip);
  217. PtrSize surfOrVolSize;
  218. const void* surfOrVolData;
  219. PtrSize allocationSize;
  220. if(ctx.m_texType == TextureType::_3D)
  221. {
  222. const auto& vol = ctx.m_loader.getVolume(mip);
  223. surfOrVolSize = vol.m_data.getSize();
  224. surfOrVolData = &vol.m_data[0];
  225. allocationSize = computeVolumeSize(ctx.m_tex->getWidth() >> mip, ctx.m_tex->getHeight() >> mip,
  226. ctx.m_tex->getDepth() >> mip, ctx.m_tex->getFormat());
  227. }
  228. else
  229. {
  230. const auto& surf = ctx.m_loader.getSurface(mip, face, layer);
  231. surfOrVolSize = surf.m_data.getSize();
  232. surfOrVolData = &surf.m_data[0];
  233. allocationSize = computeSurfaceSize(ctx.m_tex->getWidth() >> mip, ctx.m_tex->getHeight() >> mip,
  234. ctx.m_tex->getFormat());
  235. }
  236. ANKI_ASSERT(allocationSize >= surfOrVolSize);
  237. TransferGpuAllocatorHandle& handle = handles[handleCount++];
  238. ANKI_CHECK(ctx.m_trfAlloc->allocate(allocationSize, handle));
  239. void* data = handle.getMappedMemory();
  240. ANKI_ASSERT(data);
  241. memcpy(data, surfOrVolData, surfOrVolSize);
  242. // Create temp tex view
  243. TextureSubresourceInfo subresource;
  244. if(ctx.m_texType == TextureType::_3D)
  245. {
  246. subresource = TextureSubresourceInfo(TextureVolumeInfo(mip));
  247. }
  248. else
  249. {
  250. subresource = TextureSubresourceInfo(TextureSurfaceInfo(mip, 0, face, layer));
  251. }
  252. TextureViewPtr tmpView = ctx.m_gr->newTextureView(TextureViewInitInfo(ctx.m_tex, subresource, "RsrcTmp"));
  253. cmdb->copyBufferToTextureView(handle.getBuffer(), handle.getOffset(), handle.getRange(), tmpView);
  254. }
  255. // Set the barriers of the batch
  256. for(U32 i = begin; i < end; ++i)
  257. {
  258. U32 mip, layer, face;
  259. unflatten3dArrayIndex(ctx.m_layerCount, ctx.m_faces, ctx.m_loader.getMipmapCount(), i, layer, face, mip);
  260. if(ctx.m_texType == TextureType::_3D)
  261. {
  262. TextureVolumeInfo vol(mip);
  263. cmdb->setTextureVolumeBarrier(ctx.m_tex, TextureUsageBit::TRANSFER_DESTINATION,
  264. TextureUsageBit::SAMPLED_FRAGMENT | TextureUsageBit::SAMPLED_GEOMETRY,
  265. vol);
  266. }
  267. else
  268. {
  269. TextureSurfaceInfo surf(mip, 0, face, layer);
  270. cmdb->setTextureSurfaceBarrier(ctx.m_tex, TextureUsageBit::TRANSFER_DESTINATION,
  271. TextureUsageBit::SAMPLED_FRAGMENT | TextureUsageBit::SAMPLED_GEOMETRY,
  272. surf);
  273. }
  274. }
  275. // Flush batch
  276. FencePtr fence;
  277. cmdb->flush({}, &fence);
  278. for(U i = 0; i < handleCount; ++i)
  279. {
  280. ctx.m_trfAlloc->release(handles[i], fence);
  281. }
  282. cmdb.reset(nullptr);
  283. }
  284. return Error::NONE;
  285. }
  286. } // end namespace anki