ShaderProgramResource.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. U32 binaryVariantIdx = kMaxU32;
  137. U64 mutationHash = 0;
  138. if(m_binary->m_mutators.getSize())
  139. {
  140. // Create the mutation hash
  141. mutationHash = computeHash(info.m_mutation.getBegin(), m_binary->m_mutators.getSize() * sizeof(info.m_mutation[0]));
  142. // Search for the mutation in the binary
  143. // TODO optimize the search
  144. for(const ShaderBinaryMutation& mutation : m_binary->m_mutations)
  145. {
  146. if(mutation.m_hash == mutationHash)
  147. {
  148. if(mutation.m_variantIndex == kMaxU32)
  149. {
  150. // Skipped mutation, nothing to create
  151. return nullptr;
  152. }
  153. binaryVariantIdx = mutation.m_variantIndex;
  154. binaryVariant = &m_binary->m_variants[mutation.m_variantIndex];
  155. break;
  156. }
  157. }
  158. }
  159. else
  160. {
  161. ANKI_ASSERT(m_binary->m_variants.getSize() == 1);
  162. binaryVariant = &m_binary->m_variants[0];
  163. }
  164. ANKI_ASSERT(binaryVariant);
  165. ShaderProgramResourceVariant* variant = newInstance<ShaderProgramResourceVariant>(ResourceMemoryPool::getSingleton());
  166. // Time to init the shaders
  167. if(!!(info.m_shaderTypes & (ShaderTypeBit::kAllGraphics | ShaderTypeBit::kCompute)))
  168. {
  169. // Create the program name
  170. String fname;
  171. getFilepathFilename(getFilename(), fname);
  172. ResourceString progName;
  173. progName.sprintf("%s var%05u", fname.cstr(), binaryVariantIdx);
  174. for(ShaderType shaderType : EnumBitsIterable<ShaderType, ShaderTypeBit>(info.m_shaderTypes))
  175. {
  176. progName += " ";
  177. progName += info.m_techniqueNames[shaderType].getBegin();
  178. }
  179. ShaderProgramInitInfo progInf(progName);
  180. Array<ShaderPtr, U32(ShaderType::kCount)> shaderRefs; // Just for refcounting
  181. for(ShaderType shaderType : EnumBitsIterable<ShaderType, ShaderTypeBit>(info.m_shaderTypes))
  182. {
  183. const ShaderTypeBit shaderBit = ShaderTypeBit(1 << shaderType);
  184. const U32 techniqueIdx = findTechnique(info.m_techniqueNames[shaderType].getBegin());
  185. ANKI_ASSERT(!!(m_binary->m_techniques[techniqueIdx].m_shaderTypes & shaderBit));
  186. const ResourceString shaderName = (progName + " " + m_binary->m_techniques[techniqueIdx].m_name.getBegin()).cstr();
  187. ShaderInitInfo inf(shaderName);
  188. inf.m_shaderType = shaderType;
  189. const ShaderBinaryCodeBlock& binBlock =
  190. m_binary->m_codeBlocks[binaryVariant->m_techniqueCodeBlocks[techniqueIdx].m_codeBlockIndices[shaderType]];
  191. inf.m_binary = binBlock.m_binary;
  192. inf.m_reflection = binBlock.m_reflection;
  193. ShaderPtr shader = GrManager::getSingleton().newShader(inf);
  194. shaderRefs[shaderType] = shader;
  195. if(!!(shaderBit & ShaderTypeBit::kAllGraphics))
  196. {
  197. progInf.m_graphicsShaders[shaderType] = shader.get();
  198. }
  199. else if(shaderType == ShaderType::kCompute)
  200. {
  201. progInf.m_computeShader = shader.get();
  202. }
  203. else
  204. {
  205. ANKI_ASSERT(0);
  206. }
  207. }
  208. // Create the program
  209. variant->m_prog = GrManager::getSingleton().newShaderProgram(progInf);
  210. }
  211. else
  212. {
  213. ANKI_ASSERT(!!(info.m_shaderTypes & ShaderTypeBit::kAllRayTracing));
  214. U32 techniqueIdx = kMaxU32;
  215. for(ShaderType shaderType : EnumBitsIterable<ShaderType, ShaderTypeBit>(info.m_shaderTypes))
  216. {
  217. const U32 idx = findTechnique(info.m_techniqueNames[shaderType].getBegin());
  218. if(techniqueIdx == kMaxU32)
  219. {
  220. techniqueIdx = idx;
  221. }
  222. else
  223. {
  224. ANKI_ASSERT(idx == techniqueIdx && "Can't mix and match different techniques in ray tracing");
  225. }
  226. }
  227. ANKI_ASSERT(techniqueIdx != kMaxU32);
  228. // Find the library
  229. const CString libName = m_binary->m_techniques[techniqueIdx].m_name.getBegin();
  230. ANKI_ASSERT(libName.getLength() > 0);
  231. const ShaderProgramResourceSystem& progSystem = ShaderProgramResourceSystem::getSingleton();
  232. const ShaderProgramRaytracingLibrary* foundLib = nullptr;
  233. for(const ShaderProgramRaytracingLibrary& lib : progSystem.getRayTracingLibraries())
  234. {
  235. if(lib.getLibraryName() == libName)
  236. {
  237. foundLib = &lib;
  238. break;
  239. }
  240. }
  241. ANKI_ASSERT(foundLib);
  242. variant->m_prog = foundLib->getShaderProgram();
  243. RayTracingShaderGroupType groupType;
  244. if(!!(info.m_shaderTypes & ShaderTypeBit::kRayGen))
  245. {
  246. groupType = RayTracingShaderGroupType::kRayGen;
  247. }
  248. else if(!!(info.m_shaderTypes & ShaderTypeBit::kMiss))
  249. {
  250. groupType = RayTracingShaderGroupType::kMiss;
  251. }
  252. else
  253. {
  254. ANKI_ASSERT(!!(info.m_shaderTypes & ShaderTypeBit::kAllHit));
  255. groupType = RayTracingShaderGroupType::kHit;
  256. }
  257. // Set the group handle index
  258. variant->m_shaderGroupHandleIndex = foundLib->getShaderGroupHandleIndex(getFilename(), mutationHash, groupType);
  259. }
  260. return variant;
  261. }
  262. } // end namespace anki