ShaderProgramResourceSystem.cpp 19 KB

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