ShaderProgramResourceSystem.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. // Copyright (C) 2009-present, 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/Resource/ResourceManager.h>
  8. #include <AnKi/Util/Tracer.h>
  9. #include <AnKi/Gr/GrManager.h>
  10. #include <AnKi/ShaderCompiler/ShaderCompiler.h>
  11. #include <AnKi/Util/Filesystem.h>
  12. #include <AnKi/Util/System.h>
  13. #include <AnKi/Util/BitSet.h>
  14. #include <AnKi/Util/CVarSet.h>
  15. namespace anki {
  16. class ShaderProgramResourceSystem::ShaderH
  17. {
  18. public:
  19. ShaderPtr m_shader;
  20. U64 m_hash = 0; ///< Hash of the binary.
  21. };
  22. class ShaderProgramResourceSystem::ShaderGroup
  23. {
  24. public:
  25. U32 m_rayGen = kMaxU32;
  26. U32 m_miss = kMaxU32;
  27. U32 m_chit = kMaxU32;
  28. U32 m_ahit = kMaxU32;
  29. ResourceDynamicArray<U64> m_groupHashes;
  30. };
  31. class ShaderProgramResourceSystem::Lib
  32. {
  33. public:
  34. ResourceString m_name;
  35. ResourceDynamicArray<ShaderH> m_shaders;
  36. ResourceDynamicArray<ShaderGroup> m_rayGenGroups;
  37. ResourceDynamicArray<ShaderGroup> m_missGroups;
  38. ResourceDynamicArray<ShaderGroup> m_hitGroups;
  39. ShaderTypeBit m_presentStages = ShaderTypeBit::kNone;
  40. U32 addShader(const ShaderBinaryCodeBlock& codeBlock, CString progName, ShaderType shaderType)
  41. {
  42. ShaderH* shader = nullptr;
  43. for(ShaderH& s : m_shaders)
  44. {
  45. if(s.m_hash == codeBlock.m_hash)
  46. {
  47. shader = &s;
  48. break;
  49. }
  50. }
  51. if(shader == nullptr)
  52. {
  53. shader = m_shaders.emplaceBack();
  54. ShaderInitInfo inf(progName);
  55. inf.m_shaderType = shaderType;
  56. inf.m_binary = codeBlock.m_binary;
  57. shader->m_shader = GrManager::getSingleton().newShader(inf);
  58. shader->m_hash = codeBlock.m_hash;
  59. m_presentStages |= ShaderTypeBit(1 << shaderType);
  60. }
  61. return U32(shader - m_shaders.getBegin());
  62. }
  63. void addGroup(CString filename, U64 mutationHash, U32 rayGen, U32 miss, U32 chit, U32 ahit)
  64. {
  65. ResourceDynamicArray<ShaderGroup>* groupArray = nullptr;
  66. RayTracingShaderGroupType groupType;
  67. if(rayGen < kMaxU32)
  68. {
  69. groupType = RayTracingShaderGroupType::kRayGen;
  70. groupArray = &m_rayGenGroups;
  71. }
  72. else if(miss < kMaxU32)
  73. {
  74. groupType = RayTracingShaderGroupType::kMiss;
  75. groupArray = &m_missGroups;
  76. }
  77. else
  78. {
  79. ANKI_ASSERT(chit < kMaxU32 || ahit < kMaxU32);
  80. groupType = RayTracingShaderGroupType::kHit;
  81. groupArray = &m_hitGroups;
  82. }
  83. ShaderGroup* pgroup = nullptr;
  84. for(ShaderGroup& s : *groupArray)
  85. {
  86. if(s.m_rayGen == rayGen && s.m_miss == miss && s.m_ahit == ahit && s.m_chit == chit)
  87. {
  88. pgroup = &s;
  89. break;
  90. }
  91. }
  92. if(!pgroup)
  93. {
  94. pgroup = groupArray->emplaceBack();
  95. pgroup->m_rayGen = rayGen;
  96. pgroup->m_miss = miss;
  97. pgroup->m_chit = chit;
  98. pgroup->m_ahit = ahit;
  99. }
  100. const U64 groupHash = ShaderProgramRaytracingLibrary::generateShaderGroupGroupHash(filename, mutationHash, groupType);
  101. #if ANKI_ASSERTIONS_ENABLED
  102. for(U64 hash : pgroup->m_groupHashes)
  103. {
  104. ANKI_ASSERT(hash != groupHash && "Shouldn't find group with the same hash");
  105. }
  106. #endif
  107. pgroup->m_groupHashes.emplaceBack(groupHash);
  108. }
  109. };
  110. U64 ShaderProgramRaytracingLibrary::generateShaderGroupGroupHash(CString resourceFilename, U64 mutationHash, RayTracingShaderGroupType groupType)
  111. {
  112. ANKI_ASSERT(resourceFilename.getLength() > 0);
  113. ANKI_ASSERT(groupType < RayTracingShaderGroupType::kCount);
  114. String basename;
  115. getFilepathFilename(resourceFilename, basename);
  116. U64 hash = appendHash(basename.cstr(), basename.getLength(), mutationHash);
  117. hash = appendHash(&groupType, sizeof(groupType), hash);
  118. return hash;
  119. }
  120. Error ShaderProgramResourceSystem::init()
  121. {
  122. if(!GrManager::getSingleton().getDeviceCapabilities().m_rayTracingEnabled)
  123. {
  124. return Error::kNone;
  125. }
  126. // Create RT pipeline libraries
  127. const Error err = createRayTracingPrograms(m_rtLibraries);
  128. if(err)
  129. {
  130. ANKI_RESOURCE_LOGE("Failed to create ray tracing programs");
  131. }
  132. return err;
  133. }
  134. Error ShaderProgramResourceSystem::createRayTracingPrograms(ResourceDynamicArray<ShaderProgramRaytracingLibrary>& outLibs)
  135. {
  136. ANKI_RESOURCE_LOGI("Creating ray tracing programs");
  137. U32 rtProgramCount = 0;
  138. ResourceDynamicArray<Lib> libs;
  139. const Error err = ResourceManager::getSingleton().getFilesystem().iterateAllFilenames([&](CString filename) -> Error {
  140. // Check file extension
  141. String extension;
  142. getFilepathExtension(filename, extension);
  143. const Char binExtension[] = "ankiprogbin";
  144. if(extension.getLength() != sizeof(binExtension) - 1 || extension != binExtension)
  145. {
  146. return Error::kNone;
  147. }
  148. // Get the binary
  149. ResourceFilePtr file;
  150. ANKI_CHECK(ResourceManager::getSingleton().getFilesystem().openFile(filename, file));
  151. ShaderBinary* binary;
  152. ANKI_CHECK(deserializeShaderBinaryFromAnyFile(*file, binary, ResourceMemoryPool::getSingleton()));
  153. class Dummy
  154. {
  155. public:
  156. ShaderBinary* m_binary;
  157. ~Dummy()
  158. {
  159. ResourceMemoryPool::getSingleton().free(m_binary);
  160. }
  161. } dummy{binary};
  162. if(!(binary->m_shaderTypes & ShaderTypeBit::kAllRayTracing))
  163. {
  164. return Error::kNone;
  165. }
  166. // Create the program name
  167. String progName;
  168. getFilepathFilename(filename, progName);
  169. for(const ShaderBinaryTechnique& technique : binary->m_techniques)
  170. {
  171. if(!(technique.m_shaderTypes & ShaderTypeBit::kAllRayTracing))
  172. {
  173. continue;
  174. }
  175. const U32 techniqueIdx = U32(&technique - binary->m_techniques.getBegin());
  176. // Find or create the lib
  177. Lib* lib = nullptr;
  178. {
  179. for(Lib& l : libs)
  180. {
  181. if(l.m_name == technique.m_name.getBegin())
  182. {
  183. lib = &l;
  184. break;
  185. }
  186. }
  187. if(lib == nullptr)
  188. {
  189. libs.emplaceBack();
  190. lib = &libs.getBack();
  191. lib->m_name = technique.m_name.getBegin();
  192. }
  193. }
  194. // Ray gen
  195. if(!!(technique.m_shaderTypes & ShaderTypeBit::kRayGen))
  196. {
  197. // Iterate all mutations
  198. ConstWeakArray<ShaderBinaryMutation> mutations;
  199. ShaderBinaryMutation dummyMutation;
  200. if(binary->m_mutations.getSize() > 1)
  201. {
  202. mutations = binary->m_mutations;
  203. }
  204. else
  205. {
  206. dummyMutation.m_hash = 0;
  207. dummyMutation.m_variantIndex = 0;
  208. mutations = ConstWeakArray<ShaderBinaryMutation>(&dummyMutation, 1);
  209. }
  210. for(const ShaderBinaryMutation& mutation : mutations)
  211. {
  212. const ShaderBinaryVariant& variant = binary->m_variants[mutation.m_variantIndex];
  213. const U32 codeBlockIndex = variant.m_techniqueCodeBlocks[techniqueIdx].m_codeBlockIndices[ShaderType::kRayGen];
  214. const U32 shaderIdx = lib->addShader(binary->m_codeBlocks[codeBlockIndex], progName, ShaderType::kRayGen);
  215. lib->addGroup(filename, mutation.m_hash, shaderIdx, kMaxU32, kMaxU32, kMaxU32);
  216. }
  217. }
  218. // Miss
  219. if(!!(technique.m_shaderTypes & ShaderTypeBit::kMiss))
  220. {
  221. // Iterate all mutations
  222. ConstWeakArray<ShaderBinaryMutation> mutations;
  223. ShaderBinaryMutation dummyMutation;
  224. if(binary->m_mutations.getSize() > 1)
  225. {
  226. mutations = binary->m_mutations;
  227. }
  228. else
  229. {
  230. dummyMutation.m_hash = 0;
  231. dummyMutation.m_variantIndex = 0;
  232. mutations = ConstWeakArray<ShaderBinaryMutation>(&dummyMutation, 1);
  233. }
  234. for(const ShaderBinaryMutation& mutation : mutations)
  235. {
  236. const ShaderBinaryVariant& variant = binary->m_variants[mutation.m_variantIndex];
  237. const U32 codeBlockIndex = variant.m_techniqueCodeBlocks[techniqueIdx].m_codeBlockIndices[ShaderType::kMiss];
  238. ANKI_ASSERT(codeBlockIndex < kMaxU32);
  239. const U32 shaderIdx = lib->addShader(binary->m_codeBlocks[codeBlockIndex], progName, ShaderType::kMiss);
  240. lib->addGroup(filename, mutation.m_hash, kMaxU32, shaderIdx, kMaxU32, kMaxU32);
  241. }
  242. }
  243. // Hit shaders
  244. if(!!(technique.m_shaderTypes & ShaderTypeBit::kAllHit))
  245. {
  246. // Before you iterate the mutations do some work if there are none
  247. ConstWeakArray<ShaderBinaryMutation> mutations;
  248. ShaderBinaryMutation dummyMutation;
  249. if(binary->m_mutations.getSize() > 1)
  250. {
  251. mutations = binary->m_mutations;
  252. }
  253. else
  254. {
  255. dummyMutation.m_hash = 0;
  256. dummyMutation.m_variantIndex = 0;
  257. mutations = ConstWeakArray<ShaderBinaryMutation>(&dummyMutation, 1);
  258. }
  259. // Iterate all mutations
  260. for(const ShaderBinaryMutation& mutation : mutations)
  261. {
  262. if(mutation.m_variantIndex == kMaxU32)
  263. {
  264. continue;
  265. }
  266. const ShaderBinaryVariant& variant = binary->m_variants[mutation.m_variantIndex];
  267. const U32 ahitCodeBlockIndex = variant.m_techniqueCodeBlocks[techniqueIdx].m_codeBlockIndices[ShaderType::kAnyHit];
  268. const U32 chitCodeBlockIndex = variant.m_techniqueCodeBlocks[techniqueIdx].m_codeBlockIndices[ShaderType::kClosestHit];
  269. ANKI_ASSERT(ahitCodeBlockIndex != kMaxU32 || chitCodeBlockIndex != kMaxU32);
  270. const U32 ahitShaderIdx = (ahitCodeBlockIndex != kMaxU32)
  271. ? lib->addShader(binary->m_codeBlocks[ahitCodeBlockIndex], progName, ShaderType::kAnyHit)
  272. : kMaxU32;
  273. const U32 chitShaderIdx = (chitCodeBlockIndex != kMaxU32)
  274. ? lib->addShader(binary->m_codeBlocks[chitCodeBlockIndex], progName, ShaderType::kClosestHit)
  275. : kMaxU32;
  276. lib->addGroup(filename, mutation.m_hash, kMaxU32, kMaxU32, chitShaderIdx, ahitShaderIdx);
  277. }
  278. }
  279. } // iterate techniques
  280. return Error::kNone;
  281. }); // For all RT filenames
  282. ANKI_CHECK(err);
  283. // Create the libraries. The value that goes to the m_resourceHashToShaderGroupHandleIndex hashmap is the index of the shader handle inside the
  284. // program. Leverage the fact that there is a predefined order between group types (ray gen first, miss and hit follows). See the ShaderProgram
  285. // class for info.
  286. if(libs.getSize() != 0)
  287. {
  288. outLibs.resize(libs.getSize());
  289. for(U32 libIdx = 0; libIdx < libs.getSize(); ++libIdx)
  290. {
  291. ShaderProgramRaytracingLibrary& outLib = outLibs[libIdx];
  292. const Lib& inLib = libs[libIdx];
  293. if(inLib.m_presentStages != (ShaderTypeBit::kRayGen | ShaderTypeBit::kMiss | ShaderTypeBit::kClosestHit | ShaderTypeBit::kAnyHit))
  294. {
  295. ANKI_RESOURCE_LOGE("The libray is missing shader shader types: %s", inLib.m_name.cstr());
  296. return Error::kUserData;
  297. }
  298. outLib.m_libraryName = inLib.m_name;
  299. ResourceDynamicArray<RayTracingHitGroup> initInfoHitGroups;
  300. ResourceDynamicArray<Shader*> missShaders;
  301. ResourceDynamicArray<Shader*> rayGenShaders;
  302. U32 groupCount = 0;
  303. // Ray gen
  304. for(const ShaderGroup& group : inLib.m_rayGenGroups)
  305. {
  306. rayGenShaders.emplaceBack(inLib.m_shaders[group.m_rayGen].m_shader.get());
  307. for(U64 hash : group.m_groupHashes)
  308. {
  309. outLib.m_resourceHashToShaderGroupHandleIndex.emplace(hash, groupCount);
  310. }
  311. ++groupCount;
  312. }
  313. // Miss
  314. for(const ShaderGroup& group : inLib.m_missGroups)
  315. {
  316. missShaders.emplaceBack(inLib.m_shaders[group.m_miss].m_shader.get());
  317. for(U64 hash : group.m_groupHashes)
  318. {
  319. outLib.m_resourceHashToShaderGroupHandleIndex.emplace(hash, groupCount);
  320. }
  321. ++groupCount;
  322. }
  323. // Hit
  324. for(const ShaderGroup& group : inLib.m_hitGroups)
  325. {
  326. RayTracingHitGroup* infoHitGroup = initInfoHitGroups.emplaceBack();
  327. if(group.m_ahit < kMaxU32)
  328. {
  329. infoHitGroup->m_anyHitShader = inLib.m_shaders[group.m_ahit].m_shader.get();
  330. }
  331. if(group.m_chit < kMaxU32)
  332. {
  333. infoHitGroup->m_closestHitShader = inLib.m_shaders[group.m_chit].m_shader.get();
  334. }
  335. for(U64 hash : group.m_groupHashes)
  336. {
  337. outLib.m_resourceHashToShaderGroupHandleIndex.emplace(hash, groupCount);
  338. }
  339. ++groupCount;
  340. }
  341. // Create the program
  342. ShaderProgramInitInfo inf(inLib.m_name);
  343. inf.m_rayTracingShaders.m_rayGenShaders = rayGenShaders;
  344. inf.m_rayTracingShaders.m_missShaders = missShaders;
  345. inf.m_rayTracingShaders.m_hitGroups = initInfoHitGroups;
  346. outLib.m_program = GrManager::getSingleton().newShaderProgram(inf);
  347. ++rtProgramCount;
  348. }
  349. }
  350. ANKI_RESOURCE_LOGI("Created %u ray tracing programs", rtProgramCount);
  351. return Error::kNone;
  352. }
  353. } // end namespace anki