ShaderProgramResourceSystem.cpp 19 KB

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