ShaderProgramResourceSystem.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. // Copyright (C) 2009-2022, 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/ShaderProgramResourceSystem.h>
  6. #include <AnKi/Resource/ResourceFilesystem.h>
  7. #include <AnKi/Util/Tracer.h>
  8. #include <AnKi/Gr/GrManager.h>
  9. #include <AnKi/ShaderCompiler/ShaderProgramCompiler.h>
  10. #include <AnKi/Util/Filesystem.h>
  11. #include <AnKi/Util/ThreadHive.h>
  12. #include <AnKi/Util/System.h>
  13. #include <AnKi/Util/BitSet.h>
  14. #include <AnKi/Core/ConfigSet.h>
  15. namespace anki {
  16. U64 ShaderProgramRaytracingLibrary::generateShaderGroupGroupHash(CString resourceFilename, U64 mutationHash,
  17. GenericMemoryPoolAllocator<U8> alloc)
  18. {
  19. ANKI_ASSERT(resourceFilename.getLength() > 0);
  20. StringAuto basename(alloc);
  21. getFilepathFilename(resourceFilename, basename);
  22. const U64 hash = appendHash(basename.cstr(), basename.getLength(), mutationHash);
  23. return hash;
  24. }
  25. ShaderProgramResourceSystem::~ShaderProgramResourceSystem()
  26. {
  27. m_rtLibraries.destroy(m_alloc);
  28. }
  29. Error ShaderProgramResourceSystem::init(ResourceFilesystem& fs, GrManager& gr)
  30. {
  31. if(!gr.getDeviceCapabilities().m_rayTracingEnabled)
  32. {
  33. return Error::NONE;
  34. }
  35. // Create RT pipeline libraries
  36. const Error err = createRayTracingPrograms(fs, gr, m_alloc, m_rtLibraries);
  37. if(err)
  38. {
  39. ANKI_RESOURCE_LOGE("Failed to create ray tracing programs");
  40. }
  41. return err;
  42. }
  43. Error ShaderProgramResourceSystem::createRayTracingPrograms(ResourceFilesystem& fs, GrManager& gr,
  44. GenericMemoryPoolAllocator<U8>& alloc,
  45. DynamicArray<ShaderProgramRaytracingLibrary>& outLibs)
  46. {
  47. ANKI_RESOURCE_LOGI("Creating ray tracing programs");
  48. U32 rtProgramCount = 0;
  49. class Shader
  50. {
  51. public:
  52. ShaderPtr m_shader;
  53. U64 m_hash = 0; ///< Hash of the binary.
  54. };
  55. class ShaderGroup
  56. {
  57. public:
  58. U32 m_rayGen = MAX_U32;
  59. U32 m_miss = MAX_U32;
  60. U32 m_chit = MAX_U32;
  61. U32 m_ahit = MAX_U32;
  62. U64 m_hitGroupHash = 0;
  63. };
  64. class Lib
  65. {
  66. public:
  67. GenericMemoryPoolAllocator<U8> m_alloc;
  68. GrManager* m_gr;
  69. StringAuto m_name{m_alloc};
  70. DynamicArrayAuto<Shader> m_shaders{m_alloc};
  71. DynamicArrayAuto<ShaderGroup> m_shaderGroups{m_alloc};
  72. ShaderTypeBit m_presentStages = ShaderTypeBit::NONE;
  73. U32 m_rayTypeCount = 0;
  74. BitSet<64> m_rayTypeMask = {false};
  75. U32 m_rayGenShaderGroupCount = 0;
  76. U32 m_missShaderGroupCount = 0;
  77. U32 m_hitShaderGroupCount = 0;
  78. Lib(GenericMemoryPoolAllocator<U8> alloc, GrManager* gr)
  79. : m_alloc(alloc)
  80. , m_gr(gr)
  81. {
  82. }
  83. U32 addShader(const ShaderProgramBinaryCodeBlock& codeBlock, CString progName, ShaderType shaderType)
  84. {
  85. Shader* shader = nullptr;
  86. for(Shader& s : m_shaders)
  87. {
  88. if(s.m_hash == codeBlock.m_hash)
  89. {
  90. shader = &s;
  91. break;
  92. }
  93. }
  94. if(shader == nullptr)
  95. {
  96. shader = m_shaders.emplaceBack();
  97. ShaderInitInfo inf(progName);
  98. inf.m_shaderType = shaderType;
  99. inf.m_binary = codeBlock.m_binary;
  100. shader->m_shader = m_gr->newShader(inf);
  101. shader->m_hash = codeBlock.m_hash;
  102. m_presentStages |= ShaderTypeBit(1 << shaderType);
  103. }
  104. return U32(shader - m_shaders.getBegin());
  105. }
  106. void addGroup(CString filename, U64 mutationHash, U32 rayGen, U32 miss, U32 chit, U32 ahit)
  107. {
  108. const U64 groupHash =
  109. ShaderProgramRaytracingLibrary::generateShaderGroupGroupHash(filename, mutationHash, m_alloc);
  110. #if ANKI_ENABLE_ASSERTIONS
  111. for(const ShaderGroup& group : m_shaderGroups)
  112. {
  113. ANKI_ASSERT(group.m_hitGroupHash != groupHash && "Shouldn't find group with the same hash");
  114. }
  115. #endif
  116. ShaderGroup group;
  117. group.m_rayGen = rayGen;
  118. group.m_miss = miss;
  119. group.m_chit = chit;
  120. group.m_ahit = ahit;
  121. group.m_hitGroupHash = groupHash;
  122. m_shaderGroups.emplaceBack(group);
  123. if(rayGen < MAX_U32)
  124. {
  125. ++m_rayGenShaderGroupCount;
  126. }
  127. else if(miss < MAX_U32)
  128. {
  129. ++m_missShaderGroupCount;
  130. }
  131. else
  132. {
  133. ANKI_ASSERT(chit < MAX_U32 || ahit < MAX_U32);
  134. ++m_hitShaderGroupCount;
  135. }
  136. }
  137. };
  138. DynamicArrayAuto<Lib> libs(alloc);
  139. ANKI_CHECK(fs.iterateAllFilenames([&](CString filename) -> Error {
  140. // Check file extension
  141. StringAuto extension(alloc);
  142. getFilepathExtension(filename, extension);
  143. const Char binExtension[] = "ankiprogbin";
  144. if(extension.getLength() != sizeof(binExtension) - 1 || extension != binExtension)
  145. {
  146. return Error::NONE;
  147. }
  148. if(filename.find("ShaderBinaries/Rt") != 0)
  149. {
  150. // Doesn't start with the expected path, skip it
  151. return Error::NONE;
  152. }
  153. // Get the binary
  154. ResourceFilePtr file;
  155. ANKI_CHECK(fs.openFile(filename, file));
  156. ShaderProgramBinaryWrapper binaryw(alloc);
  157. ANKI_CHECK(binaryw.deserializeFromAnyFile(*file));
  158. const ShaderProgramBinary& binary = binaryw.getBinary();
  159. if(!(binary.m_presentShaderTypes & ShaderTypeBit::ALL_RAY_TRACING))
  160. {
  161. return Error::NONE;
  162. }
  163. // Checks
  164. if(binary.m_libraryName[0] == '\0')
  165. {
  166. ANKI_RESOURCE_LOGE("Library is missing from program: %s", filename.cstr());
  167. return Error::USER_DATA;
  168. }
  169. // Create the program name
  170. StringAuto progName(alloc);
  171. getFilepathFilename(filename, progName);
  172. // Find or create the lib
  173. Lib* lib = nullptr;
  174. {
  175. for(Lib& l : libs)
  176. {
  177. if(l.m_name == CString(&binary.m_libraryName[0]))
  178. {
  179. lib = &l;
  180. break;
  181. }
  182. }
  183. if(lib == nullptr)
  184. {
  185. libs.emplaceBack(alloc, &gr);
  186. lib = &libs.getBack();
  187. lib->m_name.create(CString(&binary.m_libraryName[0]));
  188. }
  189. }
  190. // Update the ray type
  191. const U32 rayTypeNumber = binary.m_rayType;
  192. if(rayTypeNumber != MAX_U32)
  193. {
  194. lib->m_rayTypeCount = max(lib->m_rayTypeCount, rayTypeNumber + 1);
  195. lib->m_rayTypeMask.set(rayTypeNumber);
  196. }
  197. // Ray gen
  198. if(!!(binary.m_presentShaderTypes & ShaderTypeBit::RAY_GEN))
  199. {
  200. if(!!(binary.m_presentShaderTypes & ~ShaderTypeBit::RAY_GEN))
  201. {
  202. ANKI_RESOURCE_LOGE("Ray gen can't co-exist with other types: %s", filename.cstr());
  203. return Error::USER_DATA;
  204. }
  205. if(binary.m_constants.getSize())
  206. {
  207. ANKI_RESOURCE_LOGE("Ray gen can't have spec constants ATM: %s", filename.cstr());
  208. return Error::USER_DATA;
  209. }
  210. // Iterate all mutations
  211. ConstWeakArray<ShaderProgramBinaryMutation> mutations;
  212. ShaderProgramBinaryMutation dummyMutation;
  213. if(binary.m_mutations.getSize() > 1)
  214. {
  215. mutations = binary.m_mutations;
  216. }
  217. else
  218. {
  219. dummyMutation.m_hash = 0;
  220. dummyMutation.m_variantIndex = 0;
  221. mutations = ConstWeakArray<ShaderProgramBinaryMutation>(&dummyMutation, 1);
  222. }
  223. for(const ShaderProgramBinaryMutation& mutation : mutations)
  224. {
  225. const ShaderProgramBinaryVariant& variant = binary.m_variants[mutation.m_variantIndex];
  226. const U32 codeBlockIndex = variant.m_codeBlockIndices[ShaderType::RAY_GEN];
  227. ANKI_ASSERT(codeBlockIndex != MAX_U32);
  228. const U32 shaderIdx =
  229. lib->addShader(binary.m_codeBlocks[codeBlockIndex], progName, ShaderType::RAY_GEN);
  230. lib->addGroup(filename, mutation.m_hash, shaderIdx, MAX_U32, MAX_U32, MAX_U32);
  231. }
  232. }
  233. // Miss shaders
  234. if(!!(binary.m_presentShaderTypes & ShaderTypeBit::MISS))
  235. {
  236. if(!!(binary.m_presentShaderTypes & ~ShaderTypeBit::MISS))
  237. {
  238. ANKI_RESOURCE_LOGE("Miss shaders can't co-exist with other types: %s", filename.cstr());
  239. return Error::USER_DATA;
  240. }
  241. if(binary.m_constants.getSize())
  242. {
  243. ANKI_RESOURCE_LOGE("Miss can't have spec constants ATM: %s", filename.cstr());
  244. return Error::USER_DATA;
  245. }
  246. if(rayTypeNumber == MAX_U32)
  247. {
  248. ANKI_RESOURCE_LOGE("Miss shader should have set the ray type: %s", filename.cstr());
  249. return Error::USER_DATA;
  250. }
  251. // Iterate all mutations
  252. ConstWeakArray<ShaderProgramBinaryMutation> mutations;
  253. ShaderProgramBinaryMutation dummyMutation;
  254. if(binary.m_mutations.getSize() > 1)
  255. {
  256. mutations = binary.m_mutations;
  257. }
  258. else
  259. {
  260. dummyMutation.m_hash = 0;
  261. dummyMutation.m_variantIndex = 0;
  262. mutations = ConstWeakArray<ShaderProgramBinaryMutation>(&dummyMutation, 1);
  263. }
  264. for(const ShaderProgramBinaryMutation& mutation : mutations)
  265. {
  266. const ShaderProgramBinaryVariant& variant = binary.m_variants[mutation.m_variantIndex];
  267. const U32 codeBlockIndex = variant.m_codeBlockIndices[ShaderType::MISS];
  268. ANKI_ASSERT(codeBlockIndex != MAX_U32);
  269. const U32 shaderIdx = lib->addShader(binary.m_codeBlocks[codeBlockIndex], progName, ShaderType::MISS);
  270. lib->addGroup(filename, mutation.m_hash, MAX_U32, shaderIdx, MAX_U32, MAX_U32);
  271. }
  272. }
  273. // Hit shaders
  274. if(!!(binary.m_presentShaderTypes & (ShaderTypeBit::ANY_HIT | ShaderTypeBit::CLOSEST_HIT)))
  275. {
  276. if(!!(binary.m_presentShaderTypes & ~(ShaderTypeBit::ANY_HIT | ShaderTypeBit::CLOSEST_HIT)))
  277. {
  278. ANKI_RESOURCE_LOGE("Hit shaders can't co-exist with other types: %s", filename.cstr());
  279. return Error::USER_DATA;
  280. }
  281. if(rayTypeNumber == MAX_U32)
  282. {
  283. ANKI_RESOURCE_LOGE("Hit shaders should have set the ray type: %s", filename.cstr());
  284. return Error::USER_DATA;
  285. }
  286. // Before you iterate the mutations do some work if there are none
  287. ConstWeakArray<ShaderProgramBinaryMutation> mutations;
  288. ShaderProgramBinaryMutation dummyMutation;
  289. if(binary.m_mutations.getSize() > 1)
  290. {
  291. mutations = binary.m_mutations;
  292. }
  293. else
  294. {
  295. dummyMutation.m_hash = 0;
  296. dummyMutation.m_variantIndex = 0;
  297. mutations = ConstWeakArray<ShaderProgramBinaryMutation>(&dummyMutation, 1);
  298. }
  299. // Iterate all mutations
  300. for(const ShaderProgramBinaryMutation& mutation : mutations)
  301. {
  302. const ShaderProgramBinaryVariant& variant = binary.m_variants[mutation.m_variantIndex];
  303. const U32 ahitCodeBlockIndex = variant.m_codeBlockIndices[ShaderType::ANY_HIT];
  304. const U32 chitCodeBlockIndex = variant.m_codeBlockIndices[ShaderType::CLOSEST_HIT];
  305. ANKI_ASSERT(ahitCodeBlockIndex != MAX_U32 || chitCodeBlockIndex != MAX_U32);
  306. const U32 ahitShaderIdx =
  307. (ahitCodeBlockIndex != MAX_U32)
  308. ? lib->addShader(binary.m_codeBlocks[ahitCodeBlockIndex], progName, ShaderType::ANY_HIT)
  309. : MAX_U32;
  310. const U32 chitShaderIdx =
  311. (chitCodeBlockIndex != MAX_U32)
  312. ? lib->addShader(binary.m_codeBlocks[chitCodeBlockIndex], progName, ShaderType::CLOSEST_HIT)
  313. : MAX_U32;
  314. lib->addGroup(filename, mutation.m_hash, MAX_U32, MAX_U32, chitShaderIdx, ahitShaderIdx);
  315. }
  316. }
  317. return Error::NONE;
  318. })); // For all RT filenames
  319. // Create the libraries the value that goes to the m_resourceHashToShaderGroupHandleIndex hashmap is the index of
  320. // the shader handle inside the program. Leverage the fact that there is a predefined order between shader types.
  321. // See the ShaderProgram class for info.
  322. if(libs.getSize() != 0)
  323. {
  324. outLibs.create(alloc, libs.getSize());
  325. for(U32 libIdx = 0; libIdx < libs.getSize(); ++libIdx)
  326. {
  327. ShaderProgramRaytracingLibrary& outLib = outLibs[libIdx];
  328. const Lib& inLib = libs[libIdx];
  329. outLib.m_alloc = alloc;
  330. if(inLib.m_presentStages
  331. != (ShaderTypeBit::RAY_GEN | ShaderTypeBit::MISS | ShaderTypeBit::CLOSEST_HIT | ShaderTypeBit::ANY_HIT))
  332. {
  333. ANKI_RESOURCE_LOGE("The libray is missing shader shader types: %s", inLib.m_name.cstr());
  334. return Error::USER_DATA;
  335. }
  336. if(inLib.m_rayTypeCount != inLib.m_rayTypeMask.getEnabledBitCount())
  337. {
  338. ANKI_RESOURCE_LOGE("Ray types are not contiguous for library: %s", inLib.m_name.cstr());
  339. return Error::USER_DATA;
  340. }
  341. outLib.m_libraryName.create(alloc, inLib.m_name);
  342. outLib.m_rayTypeCount = inLib.m_rayTypeCount;
  343. DynamicArrayAuto<RayTracingHitGroup> initInfoHitGroups(alloc);
  344. DynamicArrayAuto<ShaderPtr> missShaders(alloc);
  345. DynamicArrayAuto<ShaderPtr> rayGenShaders(alloc);
  346. // Add the hitgroups to the init info
  347. for(U32 shaderGroupIdx = 0; shaderGroupIdx < inLib.m_shaderGroups.getSize(); ++shaderGroupIdx)
  348. {
  349. const ShaderGroup& inShaderGroup = inLib.m_shaderGroups[shaderGroupIdx];
  350. ANKI_ASSERT(inShaderGroup.m_hitGroupHash != 0);
  351. if(inShaderGroup.m_ahit < MAX_U32 || inShaderGroup.m_chit < MAX_U32)
  352. {
  353. // Hit shaders
  354. ANKI_ASSERT(inShaderGroup.m_miss == MAX_U32 && inShaderGroup.m_rayGen == MAX_U32);
  355. RayTracingHitGroup* infoHitGroup = initInfoHitGroups.emplaceBack();
  356. if(inShaderGroup.m_ahit < MAX_U32)
  357. {
  358. infoHitGroup->m_anyHitShader = inLib.m_shaders[inShaderGroup.m_ahit].m_shader;
  359. }
  360. if(inShaderGroup.m_chit < MAX_U32)
  361. {
  362. infoHitGroup->m_closestHitShader = inLib.m_shaders[inShaderGroup.m_chit].m_shader;
  363. }
  364. // The hit shaders are after ray gen and miss shaders
  365. const U32 idx =
  366. inLib.m_rayGenShaderGroupCount + inLib.m_missShaderGroupCount + initInfoHitGroups.getSize() - 1;
  367. outLib.m_resourceHashToShaderGroupHandleIndex.emplace(alloc, inShaderGroup.m_hitGroupHash, idx);
  368. }
  369. else if(inShaderGroup.m_miss < MAX_U32)
  370. {
  371. // Miss shader
  372. ANKI_ASSERT(inShaderGroup.m_ahit == MAX_U32 && inShaderGroup.m_chit == MAX_U32
  373. && inShaderGroup.m_rayGen == MAX_U32);
  374. missShaders.emplaceBack(inLib.m_shaders[inShaderGroup.m_miss].m_shader);
  375. // The miss shaders are after ray gen
  376. const U32 idx = inLib.m_rayGenShaderGroupCount + missShaders.getSize() - 1;
  377. outLib.m_resourceHashToShaderGroupHandleIndex.emplace(alloc, inShaderGroup.m_hitGroupHash, idx);
  378. }
  379. else
  380. {
  381. // Ray gen shader
  382. ANKI_ASSERT(inShaderGroup.m_ahit == MAX_U32 && inShaderGroup.m_chit == MAX_U32
  383. && inShaderGroup.m_miss == MAX_U32 && inShaderGroup.m_rayGen < MAX_U32);
  384. rayGenShaders.emplaceBack(inLib.m_shaders[inShaderGroup.m_rayGen].m_shader);
  385. // Ray gen shaders are first
  386. const U32 idx = rayGenShaders.getSize() - 1;
  387. outLib.m_resourceHashToShaderGroupHandleIndex.emplace(alloc, inShaderGroup.m_hitGroupHash, idx);
  388. }
  389. } // end for all groups
  390. // Create the program
  391. ShaderProgramInitInfo inf(inLib.m_name);
  392. inf.m_rayTracingShaders.m_rayGenShaders = rayGenShaders;
  393. inf.m_rayTracingShaders.m_missShaders = missShaders;
  394. inf.m_rayTracingShaders.m_hitGroups = initInfoHitGroups;
  395. outLib.m_program = gr.newShaderProgram(inf);
  396. ++rtProgramCount;
  397. }
  398. }
  399. ANKI_RESOURCE_LOGI("Created %u ray tracing programs", rtProgramCount);
  400. return Error::NONE;
  401. }
  402. } // end namespace anki