ShaderProgramResourceSystem.cpp 12 KB

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