ShaderProgramResourceSystem.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  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/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. namespace anki {
  15. U64 ShaderProgramRaytracingLibrary::generateShaderGroupGroupHash(CString resourceFilename, U64 mutationHash,
  16. GenericMemoryPoolAllocator<U8> alloc)
  17. {
  18. ANKI_ASSERT(resourceFilename.getLength() > 0);
  19. StringAuto basename(alloc);
  20. getFilepathFilename(resourceFilename, basename);
  21. const U64 hash = appendHash(basename.cstr(), basename.getLength(), mutationHash);
  22. return hash;
  23. }
  24. ShaderProgramResourceSystem::~ShaderProgramResourceSystem()
  25. {
  26. m_cacheDir.destroy(m_alloc);
  27. m_rtLibraries.destroy(m_alloc);
  28. }
  29. Error ShaderProgramResourceSystem::init()
  30. {
  31. ANKI_TRACE_SCOPED_EVENT(COMPILE_SHADERS);
  32. StringListAuto rtProgramFilenames(m_alloc);
  33. ANKI_CHECK(compileAllShaders(m_cacheDir, *m_gr, *m_fs, m_alloc, rtProgramFilenames));
  34. if(m_gr->getDeviceCapabilities().m_rayTracingEnabled)
  35. {
  36. ANKI_CHECK(createRayTracingPrograms(m_cacheDir, rtProgramFilenames, *m_gr, m_alloc, m_rtLibraries));
  37. }
  38. return Error::NONE;
  39. }
  40. Error ShaderProgramResourceSystem::compileAllShaders(CString cacheDir, GrManager& gr, ResourceFilesystem& fs,
  41. GenericMemoryPoolAllocator<U8>& alloc,
  42. StringListAuto& rtProgramFilenames)
  43. {
  44. class MetaFileData
  45. {
  46. public:
  47. U64 m_hash;
  48. ShaderTypeBit m_shaderTypes;
  49. Array<U16, 3> m_padding = {};
  50. };
  51. ANKI_RESOURCE_LOGI("Compiling shader programs");
  52. U32 shadersCompileCount = 0;
  53. U32 shadersTotalCount = 0;
  54. ThreadHive threadHive(getCpuCoresCount(), alloc, false);
  55. // Compute hash for both
  56. ShaderCompilerOptions compilerOptions;
  57. compilerOptions.m_bindlessLimits = gr.getBindlessLimits();
  58. U64 gpuHash = computeHash(&compilerOptions, sizeof(compilerOptions));
  59. gpuHash = appendHash(&SHADER_BINARY_VERSION, sizeof(SHADER_BINARY_VERSION), gpuHash);
  60. ANKI_CHECK(fs.iterateAllFilenames([&](CString fname) -> Error {
  61. // Check file extension
  62. StringAuto extension(alloc);
  63. getFilepathExtension(fname, extension);
  64. if(extension.getLength() != 8 || extension != "ankiprog")
  65. {
  66. return Error::NONE;
  67. }
  68. ++shadersTotalCount;
  69. if(fname.find("/Rt") != CString::NPOS && !gr.getDeviceCapabilities().m_rayTracingEnabled)
  70. {
  71. // Skip RT programs when RT is disabled
  72. return Error::NONE;
  73. }
  74. // Get some filenames
  75. StringAuto baseFname(alloc);
  76. getFilepathFilename(fname, baseFname);
  77. StringAuto metaFname(alloc);
  78. metaFname.sprintf("%s/%smeta", cacheDir.cstr(), baseFname.cstr());
  79. // Get the hash from the meta file
  80. U64 metafileHash = 0;
  81. ShaderTypeBit metafileShaderTypes = ShaderTypeBit::NONE;
  82. if(fileExists(metaFname))
  83. {
  84. File metaFile;
  85. ANKI_CHECK(metaFile.open(metaFname, FileOpenFlag::READ | FileOpenFlag::BINARY));
  86. MetaFileData data;
  87. ANKI_CHECK(metaFile.read(&data, sizeof(data)));
  88. if(data.m_hash == 0 || data.m_shaderTypes == ShaderTypeBit::NONE)
  89. {
  90. ANKI_RESOURCE_LOGE("Wrong data found in the metafile: %s", metaFname.cstr());
  91. return Error::USER_DATA;
  92. }
  93. metafileHash = data.m_hash;
  94. metafileShaderTypes = data.m_shaderTypes;
  95. }
  96. // Load interface
  97. class FSystem : public ShaderProgramFilesystemInterface
  98. {
  99. public:
  100. ResourceFilesystem* m_fsystem = nullptr;
  101. Error readAllText(CString filename, StringAuto& txt) final
  102. {
  103. ResourceFilePtr file;
  104. ANKI_CHECK(m_fsystem->openFile(filename, file));
  105. ANKI_CHECK(file->readAllText(txt));
  106. return Error::NONE;
  107. }
  108. } fsystem;
  109. fsystem.m_fsystem = &fs;
  110. // Skip interface
  111. class Skip : public ShaderProgramPostParseInterface
  112. {
  113. public:
  114. U64 m_metafileHash;
  115. U64 m_newHash;
  116. U64 m_gpuHash;
  117. CString m_fname;
  118. Bool skipCompilation(U64 hash)
  119. {
  120. ANKI_ASSERT(hash != 0);
  121. const Array<U64, 2> hashes = {hash, m_gpuHash};
  122. const U64 finalHash = computeHash(hashes.getBegin(), hashes.getSizeInBytes());
  123. m_newHash = finalHash;
  124. const Bool skip = finalHash == m_metafileHash;
  125. if(!skip)
  126. {
  127. ANKI_RESOURCE_LOGI("\t%s", m_fname.cstr());
  128. }
  129. return skip;
  130. };
  131. } skip;
  132. skip.m_metafileHash = metafileHash;
  133. skip.m_newHash = 0;
  134. skip.m_gpuHash = gpuHash;
  135. skip.m_fname = fname;
  136. // Threading interface
  137. class TaskManager : public ShaderProgramAsyncTaskInterface
  138. {
  139. public:
  140. ThreadHive* m_hive = nullptr;
  141. GenericMemoryPoolAllocator<U8> m_alloc;
  142. void enqueueTask(void (*callback)(void* userData), void* userData)
  143. {
  144. class Ctx
  145. {
  146. public:
  147. void (*m_callback)(void* userData);
  148. void* m_userData;
  149. GenericMemoryPoolAllocator<U8> m_alloc;
  150. };
  151. Ctx* ctx = m_alloc.newInstance<Ctx>();
  152. ctx->m_callback = callback;
  153. ctx->m_userData = userData;
  154. ctx->m_alloc = m_alloc;
  155. m_hive->submitTask(
  156. [](void* userData, U32 threadId, ThreadHive& hive, ThreadHiveSemaphore* signalSemaphore) {
  157. Ctx* ctx = static_cast<Ctx*>(userData);
  158. ctx->m_callback(ctx->m_userData);
  159. auto alloc = ctx->m_alloc;
  160. alloc.deleteInstance(ctx);
  161. },
  162. ctx);
  163. }
  164. Error joinTasks()
  165. {
  166. m_hive->waitAllTasks();
  167. return Error::NONE;
  168. }
  169. } taskManager;
  170. taskManager.m_hive = &threadHive;
  171. taskManager.m_alloc = alloc;
  172. // Compile
  173. ShaderProgramBinaryWrapper binary(alloc);
  174. ANKI_CHECK(compileShaderProgram(fname, fsystem, &skip, &taskManager, alloc, compilerOptions, binary));
  175. const Bool cachedBinIsUpToDate = metafileHash == skip.m_newHash;
  176. if(!cachedBinIsUpToDate)
  177. {
  178. ++shadersCompileCount;
  179. }
  180. // Update the meta file
  181. if(!cachedBinIsUpToDate)
  182. {
  183. File metaFile;
  184. ANKI_CHECK(metaFile.open(metaFname, FileOpenFlag::WRITE | FileOpenFlag::BINARY));
  185. MetaFileData data;
  186. data.m_hash = skip.m_newHash;
  187. data.m_shaderTypes = binary.getBinary().m_presentShaderTypes;
  188. metafileShaderTypes = data.m_shaderTypes;
  189. ANKI_CHECK(metaFile.write(&data, sizeof(data)));
  190. }
  191. // Save the binary to the cache
  192. if(!cachedBinIsUpToDate)
  193. {
  194. StringAuto storeFname(alloc);
  195. storeFname.sprintf("%s/%sbin", cacheDir.cstr(), baseFname.cstr());
  196. ANKI_CHECK(binary.serializeToFile(storeFname));
  197. }
  198. // Gather RT programs
  199. if(!!(metafileShaderTypes & ShaderTypeBit::ALL_RAY_TRACING))
  200. {
  201. rtProgramFilenames.pushBack(fname);
  202. }
  203. return Error::NONE;
  204. }));
  205. ANKI_RESOURCE_LOGI("Compiled %u shader programs out of %u", shadersCompileCount, shadersTotalCount);
  206. return Error::NONE;
  207. }
  208. Error ShaderProgramResourceSystem::createRayTracingPrograms(CString cacheDir, const StringListAuto& rtProgramFilenames,
  209. GrManager& gr, GenericMemoryPoolAllocator<U8>& alloc,
  210. DynamicArray<ShaderProgramRaytracingLibrary>& outLibs)
  211. {
  212. ANKI_RESOURCE_LOGI("Creating ray tracing programs");
  213. U32 rtProgramCount = 0;
  214. class Shader
  215. {
  216. public:
  217. ShaderPtr m_shader;
  218. U64 m_hash = 0; ///< Hash of the binary.
  219. };
  220. class ShaderGroup
  221. {
  222. public:
  223. U32 m_rayGen = MAX_U32;
  224. U32 m_miss = MAX_U32;
  225. U32 m_chit = MAX_U32;
  226. U32 m_ahit = MAX_U32;
  227. U64 m_hitGroupHash = 0;
  228. };
  229. class Lib
  230. {
  231. public:
  232. GenericMemoryPoolAllocator<U8> m_alloc;
  233. GrManager* m_gr;
  234. StringAuto m_name{m_alloc};
  235. DynamicArrayAuto<Shader> m_shaders{m_alloc};
  236. DynamicArrayAuto<ShaderGroup> m_shaderGroups{m_alloc};
  237. ShaderTypeBit m_presentStages = ShaderTypeBit::NONE;
  238. U32 m_rayTypeCount = 0;
  239. BitSet<64> m_rayTypeMask = {false};
  240. U32 m_rayGenShaderGroupCount = 0;
  241. U32 m_missShaderGroupCount = 0;
  242. U32 m_hitShaderGroupCount = 0;
  243. Lib(GenericMemoryPoolAllocator<U8> alloc, GrManager* gr)
  244. : m_alloc(alloc)
  245. , m_gr(gr)
  246. {
  247. }
  248. U32 addShader(const ShaderProgramBinaryCodeBlock& codeBlock, CString progName, ShaderType shaderType)
  249. {
  250. Shader* shader = nullptr;
  251. for(Shader& s : m_shaders)
  252. {
  253. if(s.m_hash == codeBlock.m_hash)
  254. {
  255. shader = &s;
  256. break;
  257. }
  258. }
  259. if(shader == nullptr)
  260. {
  261. shader = m_shaders.emplaceBack();
  262. ShaderInitInfo inf(progName);
  263. inf.m_shaderType = shaderType;
  264. inf.m_binary = codeBlock.m_binary;
  265. shader->m_shader = m_gr->newShader(inf);
  266. shader->m_hash = codeBlock.m_hash;
  267. m_presentStages |= ShaderTypeBit(1 << shaderType);
  268. }
  269. return U32(shader - m_shaders.getBegin());
  270. }
  271. void addGroup(CString filename, U64 mutationHash, U32 rayGen, U32 miss, U32 chit, U32 ahit)
  272. {
  273. const U64 groupHash =
  274. ShaderProgramRaytracingLibrary::generateShaderGroupGroupHash(filename, mutationHash, m_alloc);
  275. #if ANKI_ENABLE_ASSERTIONS
  276. for(const ShaderGroup& group : m_shaderGroups)
  277. {
  278. ANKI_ASSERT(group.m_hitGroupHash != groupHash && "Shouldn't find group with the same hash");
  279. }
  280. #endif
  281. ShaderGroup group;
  282. group.m_rayGen = rayGen;
  283. group.m_miss = miss;
  284. group.m_chit = chit;
  285. group.m_ahit = ahit;
  286. group.m_hitGroupHash = groupHash;
  287. m_shaderGroups.emplaceBack(group);
  288. if(rayGen < MAX_U32)
  289. {
  290. ++m_rayGenShaderGroupCount;
  291. }
  292. else if(miss < MAX_U32)
  293. {
  294. ++m_missShaderGroupCount;
  295. }
  296. else
  297. {
  298. ANKI_ASSERT(chit < MAX_U32 || ahit < MAX_U32);
  299. ++m_hitShaderGroupCount;
  300. }
  301. }
  302. };
  303. DynamicArrayAuto<Lib> libs(alloc);
  304. for(const String& filename : rtProgramFilenames)
  305. {
  306. // Get the binary
  307. StringAuto baseFilename(alloc);
  308. getFilepathFilename(filename, baseFilename);
  309. StringAuto binaryFilename(alloc);
  310. binaryFilename.sprintf("%s/%sbin", cacheDir.cstr(), baseFilename.cstr());
  311. ShaderProgramBinaryWrapper binaryw(alloc);
  312. ANKI_CHECK(binaryw.deserializeFromFile(binaryFilename));
  313. const ShaderProgramBinary& binary = binaryw.getBinary();
  314. // Checks
  315. if(binary.m_libraryName[0] == '\0')
  316. {
  317. ANKI_RESOURCE_LOGE("Library is missing from program: %s", filename.cstr());
  318. return Error::USER_DATA;
  319. }
  320. // Create the program name
  321. StringAuto progName(alloc);
  322. getFilepathFilename(filename, progName);
  323. // Find or create the lib
  324. Lib* lib = nullptr;
  325. {
  326. for(Lib& l : libs)
  327. {
  328. if(l.m_name == CString(&binary.m_libraryName[0]))
  329. {
  330. lib = &l;
  331. break;
  332. }
  333. }
  334. if(lib == nullptr)
  335. {
  336. libs.emplaceBack(alloc, &gr);
  337. lib = &libs.getBack();
  338. lib->m_name.create(CString(&binary.m_libraryName[0]));
  339. }
  340. }
  341. // Update the ray type
  342. const U32 rayTypeNumber = binary.m_rayType;
  343. if(rayTypeNumber != MAX_U32)
  344. {
  345. lib->m_rayTypeCount = max(lib->m_rayTypeCount, rayTypeNumber + 1);
  346. lib->m_rayTypeMask.set(rayTypeNumber);
  347. }
  348. // Ray gen
  349. if(!!(binary.m_presentShaderTypes & ShaderTypeBit::RAY_GEN))
  350. {
  351. if(!!(binary.m_presentShaderTypes & ~ShaderTypeBit::RAY_GEN))
  352. {
  353. ANKI_RESOURCE_LOGE("Ray gen can't co-exist with other types: %s", filename.cstr());
  354. return Error::USER_DATA;
  355. }
  356. if(binary.m_constants.getSize())
  357. {
  358. ANKI_RESOURCE_LOGE("Ray gen can't have spec constants ATM: %s", filename.cstr());
  359. return Error::USER_DATA;
  360. }
  361. // Iterate all mutations
  362. ConstWeakArray<ShaderProgramBinaryMutation> mutations;
  363. ShaderProgramBinaryMutation dummyMutation;
  364. if(binary.m_mutations.getSize() > 1)
  365. {
  366. mutations = binary.m_mutations;
  367. }
  368. else
  369. {
  370. dummyMutation.m_hash = 0;
  371. dummyMutation.m_variantIndex = 0;
  372. mutations = ConstWeakArray<ShaderProgramBinaryMutation>(&dummyMutation, 1);
  373. }
  374. for(const ShaderProgramBinaryMutation& mutation : mutations)
  375. {
  376. const ShaderProgramBinaryVariant& variant = binary.m_variants[mutation.m_variantIndex];
  377. const U32 codeBlockIndex = variant.m_codeBlockIndices[ShaderType::RAY_GEN];
  378. ANKI_ASSERT(codeBlockIndex != MAX_U32);
  379. const U32 shaderIdx =
  380. lib->addShader(binary.m_codeBlocks[codeBlockIndex], progName, ShaderType::RAY_GEN);
  381. lib->addGroup(filename, mutation.m_hash, shaderIdx, MAX_U32, MAX_U32, MAX_U32);
  382. }
  383. }
  384. // Miss shaders
  385. if(!!(binary.m_presentShaderTypes & ShaderTypeBit::MISS))
  386. {
  387. if(!!(binary.m_presentShaderTypes & ~ShaderTypeBit::MISS))
  388. {
  389. ANKI_RESOURCE_LOGE("Miss shaders can't co-exist with other types: %s", filename.cstr());
  390. return Error::USER_DATA;
  391. }
  392. if(binary.m_constants.getSize())
  393. {
  394. ANKI_RESOURCE_LOGE("Miss can't have spec constants ATM: %s", filename.cstr());
  395. return Error::USER_DATA;
  396. }
  397. if(rayTypeNumber == MAX_U32)
  398. {
  399. ANKI_RESOURCE_LOGE("Miss shader should have set the ray type: %s", filename.cstr());
  400. return Error::USER_DATA;
  401. }
  402. // Iterate all mutations
  403. ConstWeakArray<ShaderProgramBinaryMutation> mutations;
  404. ShaderProgramBinaryMutation dummyMutation;
  405. if(binary.m_mutations.getSize() > 1)
  406. {
  407. mutations = binary.m_mutations;
  408. }
  409. else
  410. {
  411. dummyMutation.m_hash = 0;
  412. dummyMutation.m_variantIndex = 0;
  413. mutations = ConstWeakArray<ShaderProgramBinaryMutation>(&dummyMutation, 1);
  414. }
  415. for(const ShaderProgramBinaryMutation& mutation : mutations)
  416. {
  417. const ShaderProgramBinaryVariant& variant = binary.m_variants[mutation.m_variantIndex];
  418. const U32 codeBlockIndex = variant.m_codeBlockIndices[ShaderType::MISS];
  419. ANKI_ASSERT(codeBlockIndex != MAX_U32);
  420. const U32 shaderIdx = lib->addShader(binary.m_codeBlocks[codeBlockIndex], progName, ShaderType::MISS);
  421. lib->addGroup(filename, mutation.m_hash, MAX_U32, shaderIdx, MAX_U32, MAX_U32);
  422. }
  423. }
  424. // Hit shaders
  425. if(!!(binary.m_presentShaderTypes & (ShaderTypeBit::ANY_HIT | ShaderTypeBit::CLOSEST_HIT)))
  426. {
  427. if(!!(binary.m_presentShaderTypes & ~(ShaderTypeBit::ANY_HIT | ShaderTypeBit::CLOSEST_HIT)))
  428. {
  429. ANKI_RESOURCE_LOGE("Hit shaders can't co-exist with other types: %s", filename.cstr());
  430. return Error::USER_DATA;
  431. }
  432. if(rayTypeNumber == MAX_U32)
  433. {
  434. ANKI_RESOURCE_LOGE("Hit shaders should have set the ray type: %s", filename.cstr());
  435. return Error::USER_DATA;
  436. }
  437. // Before you iterate the mutations do some work if there are none
  438. ConstWeakArray<ShaderProgramBinaryMutation> mutations;
  439. ShaderProgramBinaryMutation dummyMutation;
  440. if(binary.m_mutations.getSize() > 1)
  441. {
  442. mutations = binary.m_mutations;
  443. }
  444. else
  445. {
  446. dummyMutation.m_hash = 0;
  447. dummyMutation.m_variantIndex = 0;
  448. mutations = ConstWeakArray<ShaderProgramBinaryMutation>(&dummyMutation, 1);
  449. }
  450. // Iterate all mutations
  451. for(const ShaderProgramBinaryMutation& mutation : mutations)
  452. {
  453. const ShaderProgramBinaryVariant& variant = binary.m_variants[mutation.m_variantIndex];
  454. const U32 ahitCodeBlockIndex = variant.m_codeBlockIndices[ShaderType::ANY_HIT];
  455. const U32 chitCodeBlockIndex = variant.m_codeBlockIndices[ShaderType::CLOSEST_HIT];
  456. ANKI_ASSERT(ahitCodeBlockIndex != MAX_U32 || chitCodeBlockIndex != MAX_U32);
  457. const U32 ahitShaderIdx =
  458. (ahitCodeBlockIndex != MAX_U32)
  459. ? lib->addShader(binary.m_codeBlocks[ahitCodeBlockIndex], progName, ShaderType::ANY_HIT)
  460. : MAX_U32;
  461. const U32 chitShaderIdx =
  462. (chitCodeBlockIndex != MAX_U32)
  463. ? lib->addShader(binary.m_codeBlocks[chitCodeBlockIndex], progName, ShaderType::CLOSEST_HIT)
  464. : MAX_U32;
  465. lib->addGroup(filename, mutation.m_hash, MAX_U32, MAX_U32, chitShaderIdx, ahitShaderIdx);
  466. }
  467. }
  468. } // For all RT filenames
  469. // Create the libraries the value that goes to the m_resourceHashToShaderGroupHandleIndex hashmap is the index of
  470. // the shader handle inside the program. Leverage the fact that there is a predefined order between shader types.
  471. // See the ShaderProgram class for info.
  472. if(libs.getSize() != 0)
  473. {
  474. outLibs.create(alloc, libs.getSize());
  475. for(U32 libIdx = 0; libIdx < libs.getSize(); ++libIdx)
  476. {
  477. ShaderProgramRaytracingLibrary& outLib = outLibs[libIdx];
  478. const Lib& inLib = libs[libIdx];
  479. outLib.m_alloc = alloc;
  480. if(inLib.m_presentStages
  481. != (ShaderTypeBit::RAY_GEN | ShaderTypeBit::MISS | ShaderTypeBit::CLOSEST_HIT | ShaderTypeBit::ANY_HIT))
  482. {
  483. ANKI_RESOURCE_LOGE("The libray is missing shader shader types: %s", inLib.m_name.cstr());
  484. return Error::USER_DATA;
  485. }
  486. if(inLib.m_rayTypeCount != inLib.m_rayTypeMask.getEnabledBitCount())
  487. {
  488. ANKI_RESOURCE_LOGE("Ray types are not contiguous for library: %s", inLib.m_name.cstr());
  489. return Error::USER_DATA;
  490. }
  491. outLib.m_libraryName.create(alloc, inLib.m_name);
  492. outLib.m_rayTypeCount = inLib.m_rayTypeCount;
  493. DynamicArrayAuto<RayTracingHitGroup> initInfoHitGroups(alloc);
  494. DynamicArrayAuto<ShaderPtr> missShaders(alloc);
  495. DynamicArrayAuto<ShaderPtr> rayGenShaders(alloc);
  496. // Add the hitgroups to the init info
  497. for(U32 shaderGroupIdx = 0; shaderGroupIdx < inLib.m_shaderGroups.getSize(); ++shaderGroupIdx)
  498. {
  499. const ShaderGroup& inShaderGroup = inLib.m_shaderGroups[shaderGroupIdx];
  500. ANKI_ASSERT(inShaderGroup.m_hitGroupHash != 0);
  501. if(inShaderGroup.m_ahit < MAX_U32 || inShaderGroup.m_chit < MAX_U32)
  502. {
  503. // Hit shaders
  504. ANKI_ASSERT(inShaderGroup.m_miss == MAX_U32 && inShaderGroup.m_rayGen == MAX_U32);
  505. RayTracingHitGroup* infoHitGroup = initInfoHitGroups.emplaceBack();
  506. if(inShaderGroup.m_ahit < MAX_U32)
  507. {
  508. infoHitGroup->m_anyHitShader = inLib.m_shaders[inShaderGroup.m_ahit].m_shader;
  509. }
  510. if(inShaderGroup.m_chit < MAX_U32)
  511. {
  512. infoHitGroup->m_closestHitShader = inLib.m_shaders[inShaderGroup.m_chit].m_shader;
  513. }
  514. // The hit shaders are after ray gen and miss shaders
  515. const U32 idx =
  516. inLib.m_rayGenShaderGroupCount + inLib.m_missShaderGroupCount + initInfoHitGroups.getSize() - 1;
  517. outLib.m_resourceHashToShaderGroupHandleIndex.emplace(alloc, inShaderGroup.m_hitGroupHash, idx);
  518. }
  519. else if(inShaderGroup.m_miss < MAX_U32)
  520. {
  521. // Miss shader
  522. ANKI_ASSERT(inShaderGroup.m_ahit == MAX_U32 && inShaderGroup.m_chit == MAX_U32
  523. && inShaderGroup.m_rayGen == MAX_U32);
  524. missShaders.emplaceBack(inLib.m_shaders[inShaderGroup.m_miss].m_shader);
  525. // The miss shaders are after ray gen
  526. const U32 idx = inLib.m_rayGenShaderGroupCount + missShaders.getSize() - 1;
  527. outLib.m_resourceHashToShaderGroupHandleIndex.emplace(alloc, inShaderGroup.m_hitGroupHash, idx);
  528. }
  529. else
  530. {
  531. // Ray gen shader
  532. ANKI_ASSERT(inShaderGroup.m_ahit == MAX_U32 && inShaderGroup.m_chit == MAX_U32
  533. && inShaderGroup.m_miss == MAX_U32 && inShaderGroup.m_rayGen < MAX_U32);
  534. rayGenShaders.emplaceBack(inLib.m_shaders[inShaderGroup.m_rayGen].m_shader);
  535. // Ray gen shaders are first
  536. const U32 idx = rayGenShaders.getSize() - 1;
  537. outLib.m_resourceHashToShaderGroupHandleIndex.emplace(alloc, inShaderGroup.m_hitGroupHash, idx);
  538. }
  539. } // end for all groups
  540. // Create the program
  541. ShaderProgramInitInfo inf(inLib.m_name);
  542. inf.m_rayTracingShaders.m_rayGenShaders = rayGenShaders;
  543. inf.m_rayTracingShaders.m_missShaders = missShaders;
  544. inf.m_rayTracingShaders.m_hitGroups = initInfoHitGroups;
  545. outLib.m_program = gr.newShaderProgram(inf);
  546. ++rtProgramCount;
  547. }
  548. }
  549. ANKI_RESOURCE_LOGI("Created %u ray tracing programs", rtProgramCount);
  550. return Error::NONE;
  551. }
  552. } // end namespace anki