ShaderProgramResourceSystem.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  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. ThreadHive threadHive(getCpuCoresCount(), alloc, false);
  55. // Compute hash for both
  56. const GpuDeviceCapabilities caps = gr.getDeviceCapabilities();
  57. const BindlessLimits limits = gr.getBindlessLimits();
  58. U64 gpuHash = computeHash(&caps, sizeof(caps));
  59. gpuHash = appendHash(&limits, sizeof(limits), gpuHash);
  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. 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, caps, limits, 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", shadersCompileCount);
  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. for(const ShaderGroup& group : m_shaderGroups)
  276. {
  277. ANKI_ASSERT(group.m_hitGroupHash != groupHash && "Shouldn't find group with the same hash");
  278. }
  279. ShaderGroup group;
  280. group.m_rayGen = rayGen;
  281. group.m_miss = miss;
  282. group.m_chit = chit;
  283. group.m_ahit = ahit;
  284. group.m_hitGroupHash = groupHash;
  285. m_shaderGroups.emplaceBack(group);
  286. if(rayGen < MAX_U32)
  287. {
  288. ++m_rayGenShaderGroupCount;
  289. }
  290. else if(miss < MAX_U32)
  291. {
  292. ++m_missShaderGroupCount;
  293. }
  294. else
  295. {
  296. ANKI_ASSERT(chit < MAX_U32 || ahit < MAX_U32);
  297. ++m_hitShaderGroupCount;
  298. }
  299. }
  300. };
  301. DynamicArrayAuto<Lib> libs(alloc);
  302. for(const String& filename : rtProgramFilenames)
  303. {
  304. // Get the binary
  305. StringAuto baseFilename(alloc);
  306. getFilepathFilename(filename, baseFilename);
  307. StringAuto binaryFilename(alloc);
  308. binaryFilename.sprintf("%s/%sbin", cacheDir.cstr(), baseFilename.cstr());
  309. ShaderProgramBinaryWrapper binaryw(alloc);
  310. ANKI_CHECK(binaryw.deserializeFromFile(binaryFilename));
  311. const ShaderProgramBinary& binary = binaryw.getBinary();
  312. // Checks
  313. if(binary.m_libraryName[0] == '\0')
  314. {
  315. ANKI_RESOURCE_LOGE("Library is missing from program: %s", filename.cstr());
  316. return Error::USER_DATA;
  317. }
  318. // Create the program name
  319. StringAuto progName(alloc);
  320. getFilepathFilename(filename, progName);
  321. // Find or create the lib
  322. Lib* lib = nullptr;
  323. {
  324. for(Lib& l : libs)
  325. {
  326. if(l.m_name == CString(&binary.m_libraryName[0]))
  327. {
  328. lib = &l;
  329. break;
  330. }
  331. }
  332. if(lib == nullptr)
  333. {
  334. libs.emplaceBack(alloc, &gr);
  335. lib = &libs.getBack();
  336. lib->m_name.create(CString(&binary.m_libraryName[0]));
  337. }
  338. }
  339. // Update the ray type
  340. const U32 rayTypeNumber = binary.m_rayType;
  341. if(rayTypeNumber != MAX_U32)
  342. {
  343. lib->m_rayTypeCount = max(lib->m_rayTypeCount, rayTypeNumber + 1);
  344. lib->m_rayTypeMask.set(rayTypeNumber);
  345. }
  346. // Ray gen
  347. if(!!(binary.m_presentShaderTypes & ShaderTypeBit::RAY_GEN))
  348. {
  349. if(!!(binary.m_presentShaderTypes & ~ShaderTypeBit::RAY_GEN))
  350. {
  351. ANKI_RESOURCE_LOGE("Ray gen can't co-exist with other types: %s", filename.cstr());
  352. return Error::USER_DATA;
  353. }
  354. if(binary.m_constants.getSize())
  355. {
  356. ANKI_RESOURCE_LOGE("Ray gen can't have spec constants ATM: %s", filename.cstr());
  357. return Error::USER_DATA;
  358. }
  359. // Iterate all mutations
  360. ConstWeakArray<ShaderProgramBinaryMutation> mutations;
  361. ShaderProgramBinaryMutation dummyMutation;
  362. if(binary.m_mutations.getSize() > 1)
  363. {
  364. mutations = binary.m_mutations;
  365. }
  366. else
  367. {
  368. dummyMutation.m_hash = 0;
  369. dummyMutation.m_variantIndex = 0;
  370. mutations = ConstWeakArray<ShaderProgramBinaryMutation>(&dummyMutation, 1);
  371. }
  372. for(const ShaderProgramBinaryMutation& mutation : mutations)
  373. {
  374. const ShaderProgramBinaryVariant& variant = binary.m_variants[mutation.m_variantIndex];
  375. const U32 codeBlockIndex = variant.m_codeBlockIndices[ShaderType::RAY_GEN];
  376. ANKI_ASSERT(codeBlockIndex != MAX_U32);
  377. const U32 shaderIdx =
  378. lib->addShader(binary.m_codeBlocks[codeBlockIndex], progName, ShaderType::RAY_GEN);
  379. lib->addGroup(filename, mutation.m_hash, shaderIdx, MAX_U32, MAX_U32, MAX_U32);
  380. }
  381. }
  382. // Miss shaders
  383. if(!!(binary.m_presentShaderTypes & ShaderTypeBit::MISS))
  384. {
  385. if(!!(binary.m_presentShaderTypes & ~ShaderTypeBit::MISS))
  386. {
  387. ANKI_RESOURCE_LOGE("Miss shaders can't co-exist with other types: %s", filename.cstr());
  388. return Error::USER_DATA;
  389. }
  390. if(binary.m_constants.getSize())
  391. {
  392. ANKI_RESOURCE_LOGE("Miss can't have spec constants ATM: %s", filename.cstr());
  393. return Error::USER_DATA;
  394. }
  395. if(rayTypeNumber == MAX_U32)
  396. {
  397. ANKI_RESOURCE_LOGE("Miss shader should have set the ray type: %s", filename.cstr());
  398. return Error::USER_DATA;
  399. }
  400. // Iterate all mutations
  401. ConstWeakArray<ShaderProgramBinaryMutation> mutations;
  402. ShaderProgramBinaryMutation dummyMutation;
  403. if(binary.m_mutations.getSize() > 1)
  404. {
  405. mutations = binary.m_mutations;
  406. }
  407. else
  408. {
  409. dummyMutation.m_hash = 0;
  410. dummyMutation.m_variantIndex = 0;
  411. mutations = ConstWeakArray<ShaderProgramBinaryMutation>(&dummyMutation, 1);
  412. }
  413. for(const ShaderProgramBinaryMutation& mutation : mutations)
  414. {
  415. const ShaderProgramBinaryVariant& variant = binary.m_variants[mutation.m_variantIndex];
  416. const U32 codeBlockIndex = variant.m_codeBlockIndices[ShaderType::MISS];
  417. ANKI_ASSERT(codeBlockIndex != MAX_U32);
  418. const U32 shaderIdx = lib->addShader(binary.m_codeBlocks[codeBlockIndex], progName, ShaderType::MISS);
  419. lib->addGroup(filename, mutation.m_hash, MAX_U32, shaderIdx, MAX_U32, MAX_U32);
  420. }
  421. }
  422. // Hit shaders
  423. if(!!(binary.m_presentShaderTypes & (ShaderTypeBit::ANY_HIT | ShaderTypeBit::CLOSEST_HIT)))
  424. {
  425. if(!!(binary.m_presentShaderTypes & ~(ShaderTypeBit::ANY_HIT | ShaderTypeBit::CLOSEST_HIT)))
  426. {
  427. ANKI_RESOURCE_LOGE("Hit shaders can't co-exist with other types: %s", filename.cstr());
  428. return Error::USER_DATA;
  429. }
  430. if(rayTypeNumber == MAX_U32)
  431. {
  432. ANKI_RESOURCE_LOGE("Hit shaders should have set the ray type: %s", filename.cstr());
  433. return Error::USER_DATA;
  434. }
  435. // Before you iterate the mutations do some work if there are none
  436. ConstWeakArray<ShaderProgramBinaryMutation> mutations;
  437. ShaderProgramBinaryMutation dummyMutation;
  438. if(binary.m_mutations.getSize() > 1)
  439. {
  440. mutations = binary.m_mutations;
  441. }
  442. else
  443. {
  444. dummyMutation.m_hash = 0;
  445. dummyMutation.m_variantIndex = 0;
  446. mutations = ConstWeakArray<ShaderProgramBinaryMutation>(&dummyMutation, 1);
  447. }
  448. // Iterate all mutations
  449. for(const ShaderProgramBinaryMutation& mutation : mutations)
  450. {
  451. const ShaderProgramBinaryVariant& variant = binary.m_variants[mutation.m_variantIndex];
  452. const U32 ahitCodeBlockIndex = variant.m_codeBlockIndices[ShaderType::ANY_HIT];
  453. const U32 chitCodeBlockIndex = variant.m_codeBlockIndices[ShaderType::CLOSEST_HIT];
  454. ANKI_ASSERT(ahitCodeBlockIndex != MAX_U32 || chitCodeBlockIndex != MAX_U32);
  455. const U32 ahitShaderIdx =
  456. (ahitCodeBlockIndex != MAX_U32)
  457. ? lib->addShader(binary.m_codeBlocks[ahitCodeBlockIndex], progName, ShaderType::ANY_HIT)
  458. : MAX_U32;
  459. const U32 chitShaderIdx =
  460. (chitCodeBlockIndex != MAX_U32)
  461. ? lib->addShader(binary.m_codeBlocks[chitCodeBlockIndex], progName, ShaderType::CLOSEST_HIT)
  462. : MAX_U32;
  463. lib->addGroup(filename, mutation.m_hash, MAX_U32, MAX_U32, chitShaderIdx, ahitShaderIdx);
  464. }
  465. }
  466. } // For all RT filenames
  467. // Create the libraries the value that goes to the m_resourceHashToShaderGroupHandleIndex hashmap is the index of
  468. // the shader handle inside the program. Leverage the fact that there is a predefined order between shader types.
  469. // See the ShaderProgram class for info.
  470. if(libs.getSize() != 0)
  471. {
  472. outLibs.create(alloc, libs.getSize());
  473. for(U32 libIdx = 0; libIdx < libs.getSize(); ++libIdx)
  474. {
  475. ShaderProgramRaytracingLibrary& outLib = outLibs[libIdx];
  476. const Lib& inLib = libs[libIdx];
  477. outLib.m_alloc = alloc;
  478. if(inLib.m_presentStages
  479. != (ShaderTypeBit::RAY_GEN | ShaderTypeBit::MISS | ShaderTypeBit::CLOSEST_HIT | ShaderTypeBit::ANY_HIT))
  480. {
  481. ANKI_RESOURCE_LOGE("The libray is missing shader shader types: %s", inLib.m_name.cstr());
  482. return Error::USER_DATA;
  483. }
  484. if(inLib.m_rayTypeCount != inLib.m_rayTypeMask.getEnabledBitCount())
  485. {
  486. ANKI_RESOURCE_LOGE("Ray types are not contiguous for library: %s", inLib.m_name.cstr());
  487. return Error::USER_DATA;
  488. }
  489. outLib.m_libraryName.create(alloc, inLib.m_name);
  490. outLib.m_rayTypeCount = inLib.m_rayTypeCount;
  491. DynamicArrayAuto<RayTracingHitGroup> initInfoHitGroups(alloc);
  492. DynamicArrayAuto<ShaderPtr> missShaders(alloc);
  493. DynamicArrayAuto<ShaderPtr> rayGenShaders(alloc);
  494. // Add the hitgroups to the init info
  495. for(U32 shaderGroupIdx = 0; shaderGroupIdx < inLib.m_shaderGroups.getSize(); ++shaderGroupIdx)
  496. {
  497. const ShaderGroup& inShaderGroup = inLib.m_shaderGroups[shaderGroupIdx];
  498. ANKI_ASSERT(inShaderGroup.m_hitGroupHash != 0);
  499. if(inShaderGroup.m_ahit < MAX_U32 || inShaderGroup.m_chit < MAX_U32)
  500. {
  501. // Hit shaders
  502. ANKI_ASSERT(inShaderGroup.m_miss == MAX_U32 && inShaderGroup.m_rayGen == MAX_U32);
  503. RayTracingHitGroup* infoHitGroup = initInfoHitGroups.emplaceBack();
  504. if(inShaderGroup.m_ahit < MAX_U32)
  505. {
  506. infoHitGroup->m_anyHitShader = inLib.m_shaders[inShaderGroup.m_ahit].m_shader;
  507. }
  508. if(inShaderGroup.m_chit < MAX_U32)
  509. {
  510. infoHitGroup->m_closestHitShader = inLib.m_shaders[inShaderGroup.m_chit].m_shader;
  511. }
  512. // The hit shaders are after ray gen and miss shaders
  513. const U32 idx =
  514. inLib.m_rayGenShaderGroupCount + inLib.m_missShaderGroupCount + initInfoHitGroups.getSize() - 1;
  515. outLib.m_resourceHashToShaderGroupHandleIndex.emplace(alloc, inShaderGroup.m_hitGroupHash, idx);
  516. }
  517. else if(inShaderGroup.m_miss < MAX_U32)
  518. {
  519. // Miss shader
  520. ANKI_ASSERT(inShaderGroup.m_ahit == MAX_U32 && inShaderGroup.m_chit == MAX_U32
  521. && inShaderGroup.m_rayGen == MAX_U32);
  522. missShaders.emplaceBack(inLib.m_shaders[inShaderGroup.m_miss].m_shader);
  523. // The miss shaders are after ray gen
  524. const U32 idx = inLib.m_rayGenShaderGroupCount + missShaders.getSize() - 1;
  525. outLib.m_resourceHashToShaderGroupHandleIndex.emplace(alloc, inShaderGroup.m_hitGroupHash, idx);
  526. }
  527. else
  528. {
  529. // Ray gen shader
  530. ANKI_ASSERT(inShaderGroup.m_ahit == MAX_U32 && inShaderGroup.m_chit == MAX_U32
  531. && inShaderGroup.m_miss == MAX_U32 && inShaderGroup.m_rayGen < MAX_U32);
  532. rayGenShaders.emplaceBack(inLib.m_shaders[inShaderGroup.m_rayGen].m_shader);
  533. // Ray gen shaders are first
  534. const U32 idx = rayGenShaders.getSize() - 1;
  535. outLib.m_resourceHashToShaderGroupHandleIndex.emplace(alloc, inShaderGroup.m_hitGroupHash, idx);
  536. }
  537. } // end for all groups
  538. // Create the program
  539. ShaderProgramInitInfo inf(inLib.m_name);
  540. inf.m_rayTracingShaders.m_rayGenShaders = rayGenShaders;
  541. inf.m_rayTracingShaders.m_missShaders = missShaders;
  542. inf.m_rayTracingShaders.m_hitGroups = initInfoHitGroups;
  543. outLib.m_program = gr.newShaderProgram(inf);
  544. ++rtProgramCount;
  545. }
  546. }
  547. ANKI_RESOURCE_LOGI("Created %u ray tracing programs", rtProgramCount);
  548. return Error::NONE;
  549. }
  550. } // end namespace anki