ShaderProgramResource.cpp 8.2 KB

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