ShaderProgramResourceSystem.cpp 19 KB

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