ShaderProgramResource.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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/ShaderProgramResource.h>
  6. #include <AnKi/Resource/ResourceManager.h>
  7. #include <AnKi/Resource/ShaderProgramResourceSystem.h>
  8. #include <AnKi/Gr/ShaderProgram.h>
  9. #include <AnKi/Gr/GrManager.h>
  10. #include <AnKi/Util/Filesystem.h>
  11. #include <AnKi/Util/Functions.h>
  12. #include <AnKi/Util/CVarSet.h>
  13. namespace anki {
  14. ShaderProgramResourceVariant::ShaderProgramResourceVariant()
  15. {
  16. }
  17. ShaderProgramResourceVariant::~ShaderProgramResourceVariant()
  18. {
  19. }
  20. ShaderProgramResource::~ShaderProgramResource()
  21. {
  22. for(auto it : m_variants)
  23. {
  24. ShaderProgramResourceVariant* variant = &(*it);
  25. deleteInstance(ResourceMemoryPool::getSingleton(), variant);
  26. }
  27. ResourceMemoryPool::getSingleton().free(m_binary);
  28. }
  29. Error ShaderProgramResource::load(const ResourceFilename& filename, [[maybe_unused]] Bool async)
  30. {
  31. // Load the binary
  32. ResourceFilePtr file;
  33. ANKI_CHECK(openFile(filename, file));
  34. ANKI_CHECK(deserializeShaderBinaryFromAnyFile(*file, m_binary, ResourceMemoryPool::getSingleton()));
  35. return Error::kNone;
  36. }
  37. void ShaderProgramResource::getOrCreateVariant(const ShaderProgramResourceVariantInitInfo& info_, const ShaderProgramResourceVariant*& variant) const
  38. {
  39. ShaderProgramResourceVariantInitInfo info = info_;
  40. // Sanity checks
  41. ANKI_ASSERT(info.m_setMutators.getSetBitCount() == m_binary->m_mutators.getSize());
  42. // User didn't provided Try to guess some defaults
  43. if(!info.m_shaderTypes)
  44. {
  45. U32 techniqueIdx = kMaxU32;
  46. for(U32 i = 0; i < m_binary->m_techniques.getSize(); ++i)
  47. {
  48. if(CString("Unnamed") == m_binary->m_techniques[i].m_name.getBegin())
  49. {
  50. techniqueIdx = i;
  51. break;
  52. }
  53. }
  54. ANKI_ASSERT(techniqueIdx != kMaxU32);
  55. const ShaderTypeBit techniqueShaderTypes = m_binary->m_techniques[techniqueIdx].m_shaderTypes;
  56. if(techniqueShaderTypes == (ShaderTypeBit::kPixel | ShaderTypeBit::kVertex))
  57. {
  58. info.requestTechniqueAndTypes(techniqueShaderTypes, "Unnamed");
  59. }
  60. else if(techniqueShaderTypes == ShaderTypeBit::kCompute)
  61. {
  62. info.requestTechniqueAndTypes(techniqueShaderTypes, "Unnamed");
  63. }
  64. else
  65. {
  66. ANKI_ASSERT(!"Can't figure out a sensible default");
  67. }
  68. }
  69. // Compute variant hash
  70. U64 hash = computeHash(&info.m_shaderTypes, sizeof(info.m_shaderTypes));
  71. for(ShaderType stype : EnumBitsIterable<ShaderType, ShaderTypeBit>(info.m_shaderTypes))
  72. {
  73. const PtrSize len = strlen(info.m_techniqueNames[stype].getBegin());
  74. ANKI_ASSERT(len > 0);
  75. hash = appendHash(info.m_techniqueNames[stype].getBegin(), len, hash);
  76. }
  77. if(m_binary->m_mutators.getSize())
  78. {
  79. hash = appendHash(info.m_mutation.getBegin(), m_binary->m_mutators.getSize() * sizeof(info.m_mutation[0]), hash);
  80. }
  81. // Check if the variant is in the cache
  82. {
  83. RLockGuard lock(m_mtx);
  84. auto it = m_variants.find(hash);
  85. if(it != m_variants.getEnd())
  86. {
  87. // Done
  88. variant = *it;
  89. if(!!(info.m_shaderTypes & ShaderTypeBit::kAllGraphics))
  90. {
  91. ANKI_ASSERT(variant->m_prog->getShaderTypes() == info.m_shaderTypes);
  92. }
  93. return;
  94. }
  95. }
  96. // Create the variant
  97. WLockGuard lock(m_mtx);
  98. // Check again
  99. auto it = m_variants.find(hash);
  100. if(it != m_variants.getEnd())
  101. {
  102. // Done
  103. variant = *it;
  104. return;
  105. }
  106. // Create
  107. ShaderProgramResourceVariant* v = createNewVariant(info);
  108. if(v)
  109. {
  110. m_variants.emplace(hash, v);
  111. }
  112. variant = v;
  113. if(!!(info.m_shaderTypes & ShaderTypeBit::kAllGraphics))
  114. {
  115. ANKI_ASSERT(variant->m_prog->getShaderTypes() == info.m_shaderTypes);
  116. }
  117. }
  118. U32 ShaderProgramResource::findTechnique(CString name) const
  119. {
  120. U32 techniqueIdx = kMaxU32;
  121. for(U32 i = 0; i < m_binary->m_techniques.getSize(); ++i)
  122. {
  123. if(m_binary->m_techniques[i].m_name.getBegin() == name)
  124. {
  125. techniqueIdx = i;
  126. break;
  127. }
  128. }
  129. ANKI_ASSERT(techniqueIdx != kMaxU32 && "Technique not found");
  130. return techniqueIdx;
  131. }
  132. ShaderProgramResourceVariant* ShaderProgramResource::createNewVariant(const ShaderProgramResourceVariantInitInfo& info) const
  133. {
  134. // Get the binary program variant
  135. const ShaderBinaryVariant* binaryVariant = nullptr;
  136. U64 mutationHash = 0;
  137. if(m_binary->m_mutators.getSize())
  138. {
  139. // Create the mutation hash
  140. mutationHash = computeHash(info.m_mutation.getBegin(), m_binary->m_mutators.getSize() * sizeof(info.m_mutation[0]));
  141. // Search for the mutation in the binary
  142. // TODO optimize the search
  143. for(const ShaderBinaryMutation& mutation : m_binary->m_mutations)
  144. {
  145. if(mutation.m_hash == mutationHash)
  146. {
  147. if(mutation.m_variantIndex == kMaxU32)
  148. {
  149. // Skipped mutation, nothing to create
  150. return nullptr;
  151. }
  152. binaryVariant = &m_binary->m_variants[mutation.m_variantIndex];
  153. break;
  154. }
  155. }
  156. }
  157. else
  158. {
  159. ANKI_ASSERT(m_binary->m_variants.getSize() == 1);
  160. binaryVariant = &m_binary->m_variants[0];
  161. }
  162. ANKI_ASSERT(binaryVariant);
  163. ShaderProgramResourceVariant* variant = newInstance<ShaderProgramResourceVariant>(ResourceMemoryPool::getSingleton());
  164. // Time to init the shaders
  165. if(!!(info.m_shaderTypes & (ShaderTypeBit::kAllGraphics | ShaderTypeBit::kCompute)))
  166. {
  167. // Create the program name
  168. String fname;
  169. getFilepathFilename(getFilename(), fname);
  170. ResourceString progName = fname.cstr();
  171. for(ShaderType shaderType : EnumBitsIterable<ShaderType, ShaderTypeBit>(info.m_shaderTypes))
  172. {
  173. progName += "_";
  174. progName += info.m_techniqueNames[shaderType].getBegin();
  175. }
  176. ShaderProgramInitInfo progInf(progName);
  177. Array<ShaderPtr, U32(ShaderType::kCount)> shaderRefs; // Just for refcounting
  178. for(ShaderType shaderType : EnumBitsIterable<ShaderType, ShaderTypeBit>(info.m_shaderTypes))
  179. {
  180. const ShaderTypeBit shaderBit = ShaderTypeBit(1 << shaderType);
  181. const U32 techniqueIdx = findTechnique(info.m_techniqueNames[shaderType].getBegin());
  182. ANKI_ASSERT(!!(m_binary->m_techniques[techniqueIdx].m_shaderTypes & shaderBit));
  183. const ResourceString shaderName = (progName + "_" + m_binary->m_techniques[techniqueIdx].m_name.getBegin()).cstr();
  184. ShaderInitInfo inf(shaderName);
  185. inf.m_shaderType = shaderType;
  186. const ShaderBinaryCodeBlock& binBlock =
  187. m_binary->m_codeBlocks[binaryVariant->m_techniqueCodeBlocks[techniqueIdx].m_codeBlockIndices[shaderType]];
  188. inf.m_binary = binBlock.m_binary;
  189. inf.m_reflection = binBlock.m_reflection;
  190. ShaderPtr shader = GrManager::getSingleton().newShader(inf);
  191. shaderRefs[shaderType] = shader;
  192. if(!!(shaderBit & ShaderTypeBit::kAllGraphics))
  193. {
  194. progInf.m_graphicsShaders[shaderType] = shader.get();
  195. }
  196. else if(shaderType == ShaderType::kCompute)
  197. {
  198. progInf.m_computeShader = shader.get();
  199. }
  200. else
  201. {
  202. ANKI_ASSERT(0);
  203. }
  204. }
  205. // Create the program
  206. variant->m_prog = GrManager::getSingleton().newShaderProgram(progInf);
  207. }
  208. else
  209. {
  210. ANKI_ASSERT(!!(info.m_shaderTypes & ShaderTypeBit::kAllRayTracing));
  211. U32 techniqueIdx = kMaxU32;
  212. for(ShaderType shaderType : EnumBitsIterable<ShaderType, ShaderTypeBit>(info.m_shaderTypes))
  213. {
  214. const U32 idx = findTechnique(info.m_techniqueNames[shaderType].getBegin());
  215. if(techniqueIdx == kMaxU32)
  216. {
  217. techniqueIdx = idx;
  218. }
  219. else
  220. {
  221. ANKI_ASSERT(idx == techniqueIdx && "Can't mix and match different techniques in ray tracing");
  222. }
  223. }
  224. ANKI_ASSERT(techniqueIdx != kMaxU32);
  225. // Find the library
  226. const CString libName = m_binary->m_techniques[techniqueIdx].m_name.getBegin();
  227. ANKI_ASSERT(libName.getLength() > 0);
  228. const ShaderProgramResourceSystem& progSystem = ShaderProgramResourceSystem::getSingleton();
  229. const ShaderProgramRaytracingLibrary* foundLib = nullptr;
  230. for(const ShaderProgramRaytracingLibrary& lib : progSystem.getRayTracingLibraries())
  231. {
  232. if(lib.getLibraryName() == libName)
  233. {
  234. foundLib = &lib;
  235. break;
  236. }
  237. }
  238. ANKI_ASSERT(foundLib);
  239. variant->m_prog = foundLib->getShaderProgram();
  240. RayTracingShaderGroupType groupType;
  241. if(!!(info.m_shaderTypes & ShaderTypeBit::kRayGen))
  242. {
  243. groupType = RayTracingShaderGroupType::kRayGen;
  244. }
  245. else if(!!(info.m_shaderTypes & ShaderTypeBit::kMiss))
  246. {
  247. groupType = RayTracingShaderGroupType::kMiss;
  248. }
  249. else
  250. {
  251. ANKI_ASSERT(!!(info.m_shaderTypes & ShaderTypeBit::kAllHit));
  252. groupType = RayTracingShaderGroupType::kHit;
  253. }
  254. // Set the group handle index
  255. variant->m_shaderGroupHandleIndex = foundLib->getShaderGroupHandleIndex(getFilename(), mutationHash, groupType);
  256. }
  257. return variant;
  258. }
  259. } // end namespace anki