ShaderProgramResourceSystem.cpp 19 KB

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