ShaderProgramResource.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. // Copyright (C) 2009-2023, 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/Core/ConfigSet.h>
  13. namespace anki {
  14. ShaderProgramResourceVariant::ShaderProgramResourceVariant()
  15. {
  16. }
  17. ShaderProgramResourceVariant::~ShaderProgramResourceVariant()
  18. {
  19. }
  20. ShaderProgramResource::ShaderProgramResource()
  21. : m_binary(&ResourceMemoryPool::getSingleton())
  22. {
  23. }
  24. ShaderProgramResource::~ShaderProgramResource()
  25. {
  26. for(auto it : m_variants)
  27. {
  28. ShaderProgramResourceVariant* variant = &(*it);
  29. deleteInstance(ResourceMemoryPool::getSingleton(), variant);
  30. }
  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(m_binary.deserializeFromAnyFile(*file));
  38. const ShaderProgramBinary& binary = m_binary.getBinary();
  39. // Create the mutators
  40. if(binary.m_mutators.getSize() > 0)
  41. {
  42. m_mutators.resize(binary.m_mutators.getSize());
  43. for(U32 i = 0; i < binary.m_mutators.getSize(); ++i)
  44. {
  45. m_mutators[i].m_name = binary.m_mutators[i].m_name.getBegin();
  46. ANKI_ASSERT(m_mutators[i].m_name.getLength() > 0);
  47. m_mutators[i].m_values = binary.m_mutators[i].m_values;
  48. }
  49. }
  50. // Create the constants
  51. for(const ShaderProgramBinaryConstant& c : binary.m_constants)
  52. {
  53. U32 componentIdx;
  54. U32 componentCount;
  55. CString name;
  56. ANKI_CHECK(parseConst(c.m_name.getBegin(), componentIdx, componentCount, name));
  57. // Do the mapping
  58. ConstMapping mapping;
  59. mapping.m_component = componentIdx;
  60. if(componentIdx > 0)
  61. {
  62. const ShaderProgramResourceConstant* other = tryFindConstant(name);
  63. ANKI_ASSERT(other);
  64. mapping.m_constsIdx = U32(other - m_consts.getBegin());
  65. }
  66. else
  67. {
  68. mapping.m_constsIdx = m_consts.getSize();
  69. }
  70. m_constBinaryMapping.emplaceBack(mapping);
  71. // Skip if const is there
  72. if(componentIdx > 0)
  73. {
  74. continue;
  75. }
  76. // Create new one
  77. ShaderProgramResourceConstant& in = *m_consts.emplaceBack();
  78. in.m_name = name;
  79. in.m_index = m_consts.getSize() - 1;
  80. if(componentCount == 1)
  81. {
  82. in.m_dataType = c.m_type;
  83. }
  84. else if(componentCount == 2)
  85. {
  86. if(c.m_type == ShaderVariableDataType::kU32)
  87. {
  88. in.m_dataType = ShaderVariableDataType::kUVec2;
  89. }
  90. else if(c.m_type == ShaderVariableDataType::kI32)
  91. {
  92. in.m_dataType = ShaderVariableDataType::kIVec2;
  93. }
  94. else
  95. {
  96. ANKI_ASSERT(c.m_type == ShaderVariableDataType::kF32);
  97. in.m_dataType = ShaderVariableDataType::kVec2;
  98. }
  99. }
  100. else if(componentCount == 3)
  101. {
  102. if(c.m_type == ShaderVariableDataType::kU32)
  103. {
  104. in.m_dataType = ShaderVariableDataType::kUVec3;
  105. }
  106. else if(c.m_type == ShaderVariableDataType::kI32)
  107. {
  108. in.m_dataType = ShaderVariableDataType::kIVec3;
  109. }
  110. else
  111. {
  112. ANKI_ASSERT(c.m_type == ShaderVariableDataType::kF32);
  113. in.m_dataType = ShaderVariableDataType::kVec3;
  114. }
  115. }
  116. else if(componentCount == 4)
  117. {
  118. if(c.m_type == ShaderVariableDataType::kU32)
  119. {
  120. in.m_dataType = ShaderVariableDataType::kUVec4;
  121. }
  122. else if(c.m_type == ShaderVariableDataType::kI32)
  123. {
  124. in.m_dataType = ShaderVariableDataType::kIVec4;
  125. }
  126. else
  127. {
  128. ANKI_ASSERT(c.m_type == ShaderVariableDataType::kF32);
  129. in.m_dataType = ShaderVariableDataType::kVec4;
  130. }
  131. }
  132. else
  133. {
  134. ANKI_ASSERT(0);
  135. }
  136. }
  137. m_shaderStages = binary.m_presentShaderTypes;
  138. // Do some RT checks
  139. if(!!(m_shaderStages & ShaderTypeBit::kAllRayTracing))
  140. {
  141. if(m_shaderStages != (ShaderTypeBit::kAnyHit | ShaderTypeBit::kClosestHit) && m_shaderStages != ShaderTypeBit::kMiss
  142. && m_shaderStages != ShaderTypeBit::kRayGen)
  143. {
  144. ANKI_RESOURCE_LOGE("Any and closest hit shaders shouldn't coexist with other stages. Miss can't coexist "
  145. "with other stages. Raygen can't coexist with other stages as well");
  146. return Error::kUserData;
  147. }
  148. }
  149. return Error::kNone;
  150. }
  151. Error ShaderProgramResource::parseConst(CString constName, U32& componentIdx, U32& componentCount, CString& name)
  152. {
  153. const CString prefixName = "_anki_const_";
  154. const PtrSize prefix = constName.find(prefixName);
  155. if(prefix != 0)
  156. {
  157. // Simple name
  158. componentIdx = 0;
  159. componentCount = 1;
  160. name = constName;
  161. return Error::kNone;
  162. }
  163. Array<char, 2> number;
  164. number[0] = constName[prefixName.getLength()];
  165. number[1] = '\0';
  166. ANKI_CHECK(CString(number.getBegin()).toNumber(componentIdx));
  167. number[0] = constName[prefixName.getLength() + 2];
  168. ANKI_CHECK(CString(number.getBegin()).toNumber(componentCount));
  169. name = constName.getBegin() + prefixName.getLength() + 4;
  170. return Error::kNone;
  171. }
  172. void ShaderProgramResource::getOrCreateVariant(const ShaderProgramResourceVariantInitInfo& info, const ShaderProgramResourceVariant*& variant) const
  173. {
  174. // Sanity checks
  175. ANKI_ASSERT(info.m_setMutators.getEnabledBitCount() == m_mutators.getSize());
  176. ANKI_ASSERT(info.m_setConstants.getEnabledBitCount() == m_consts.getSize());
  177. // Compute variant hash
  178. U64 hash = 0;
  179. if(m_mutators.getSize())
  180. {
  181. hash = computeHash(info.m_mutation.getBegin(), m_mutators.getSize() * sizeof(info.m_mutation[0]));
  182. }
  183. if(m_consts.getSize())
  184. {
  185. hash = appendHash(info.m_constantValues.getBegin(), m_consts.getSize() * sizeof(info.m_constantValues[0]), hash);
  186. }
  187. // Check if the variant is in the cache
  188. {
  189. RLockGuard<RWMutex> lock(m_mtx);
  190. auto it = m_variants.find(hash);
  191. if(it != m_variants.getEnd())
  192. {
  193. // Done
  194. variant = *it;
  195. return;
  196. }
  197. }
  198. // Create the variant
  199. WLockGuard<RWMutex> lock(m_mtx);
  200. // Check again
  201. auto it = m_variants.find(hash);
  202. if(it != m_variants.getEnd())
  203. {
  204. // Done
  205. variant = *it;
  206. return;
  207. }
  208. // Create
  209. ShaderProgramResourceVariant* v = createNewVariant(info);
  210. if(v)
  211. {
  212. m_variants.emplace(hash, v);
  213. }
  214. variant = v;
  215. }
  216. ShaderProgramResourceVariant* ShaderProgramResource::createNewVariant(const ShaderProgramResourceVariantInitInfo& info) const
  217. {
  218. const ShaderProgramBinary& binary = m_binary.getBinary();
  219. // Get the binary program variant
  220. const ShaderProgramBinaryVariant* binaryVariant = nullptr;
  221. U64 mutationHash = 0;
  222. if(m_mutators.getSize())
  223. {
  224. // Create the mutation hash
  225. mutationHash = computeHash(info.m_mutation.getBegin(), m_mutators.getSize() * sizeof(info.m_mutation[0]));
  226. // Search for the mutation in the binary
  227. // TODO optimize the search
  228. for(const ShaderProgramBinaryMutation& mutation : binary.m_mutations)
  229. {
  230. if(mutation.m_hash == mutationHash)
  231. {
  232. if(mutation.m_variantIndex == kMaxU32)
  233. {
  234. // Skipped mutation, nothing to create
  235. return nullptr;
  236. }
  237. binaryVariant = &binary.m_variants[mutation.m_variantIndex];
  238. break;
  239. }
  240. }
  241. }
  242. else
  243. {
  244. ANKI_ASSERT(binary.m_variants.getSize() == 1);
  245. binaryVariant = &binary.m_variants[0];
  246. }
  247. ANKI_ASSERT(binaryVariant);
  248. ShaderProgramResourceVariant* variant = newInstance<ShaderProgramResourceVariant>(ResourceMemoryPool::getSingleton());
  249. variant->m_binaryVariant = binaryVariant;
  250. // Set the constant values
  251. Array<ShaderSpecializationConstValue, 64> constValues;
  252. U32 constValueCount = 0;
  253. for(const ShaderProgramBinaryConstantInstance& instance : binaryVariant->m_constants)
  254. {
  255. const ShaderProgramBinaryConstant& c = binary.m_constants[instance.m_index];
  256. const U32 inputIdx = m_constBinaryMapping[instance.m_index].m_constsIdx;
  257. const U32 component = m_constBinaryMapping[instance.m_index].m_component;
  258. // Get value
  259. const ShaderProgramResourceConstantValue* value = nullptr;
  260. for(U32 i = 0; i < m_consts.getSize(); ++i)
  261. {
  262. if(info.m_constantValues[i].m_constantIndex == inputIdx)
  263. {
  264. value = &info.m_constantValues[i];
  265. break;
  266. }
  267. }
  268. ANKI_ASSERT(value && "Forgot to set the value of a constant");
  269. constValues[constValueCount].m_constantId = c.m_constantId;
  270. constValues[constValueCount].m_dataType = c.m_type;
  271. constValues[constValueCount].m_int = value->m_ivec4[component];
  272. ++constValueCount;
  273. }
  274. // Get the workgroup sizes
  275. if(!!(m_shaderStages & ShaderTypeBit::kCompute))
  276. {
  277. for(U32 i = 0; i < 3; ++i)
  278. {
  279. if(binaryVariant->m_workgroupSizes[i] != kMaxU32)
  280. {
  281. // Size didn't come from specialization const
  282. variant->m_workgroupSizes[i] = binaryVariant->m_workgroupSizes[i];
  283. }
  284. else
  285. {
  286. // Size is specialization const
  287. ANKI_ASSERT(binaryVariant->m_workgroupSizesConstants[i] != kMaxU32);
  288. const U32 binaryConstIdx = binaryVariant->m_workgroupSizesConstants[i];
  289. const U32 constIdx = m_constBinaryMapping[binaryConstIdx].m_constsIdx;
  290. const U32 component = m_constBinaryMapping[binaryConstIdx].m_component;
  291. [[maybe_unused]] const Const& c = m_consts[constIdx];
  292. ANKI_ASSERT(c.m_dataType == ShaderVariableDataType::kU32 || c.m_dataType == ShaderVariableDataType::kUVec2
  293. || c.m_dataType == ShaderVariableDataType::kUVec3 || c.m_dataType == ShaderVariableDataType::kUVec4);
  294. // Find the value
  295. for(U32 i = 0; i < m_consts.getSize(); ++i)
  296. {
  297. if(info.m_constantValues[i].m_constantIndex == constIdx)
  298. {
  299. const I32 value = info.m_constantValues[i].m_ivec4[component];
  300. ANKI_ASSERT(value > 0);
  301. variant->m_workgroupSizes[i] = U32(value);
  302. break;
  303. }
  304. }
  305. }
  306. ANKI_ASSERT(variant->m_workgroupSizes[i] != kMaxU32);
  307. }
  308. }
  309. // Time to init the shaders
  310. if(!!(m_shaderStages & (ShaderTypeBit::kAllGraphics | ShaderTypeBit::kCompute)))
  311. {
  312. // Create the program name
  313. String progName;
  314. getFilepathFilename(getFilename(), progName);
  315. char* cprogName = const_cast<char*>(progName.cstr());
  316. if(progName.getLength() > kMaxGrObjectNameLength)
  317. {
  318. cprogName[kMaxGrObjectNameLength] = '\0';
  319. }
  320. ShaderProgramInitInfo progInf(cprogName);
  321. Array<ShaderPtr, U32(ShaderType::kCount)> shaderRefs; // Just for refcounting
  322. for(ShaderType shaderType : EnumBitsIterable<ShaderType, ShaderTypeBit>(m_shaderStages))
  323. {
  324. ShaderInitInfo inf(cprogName);
  325. inf.m_shaderType = shaderType;
  326. inf.m_binary = binary.m_codeBlocks[binaryVariant->m_codeBlockIndices[shaderType]].m_binary;
  327. inf.m_constValues.setArray((constValueCount) ? constValues.getBegin() : nullptr, constValueCount);
  328. ShaderPtr shader = GrManager::getSingleton().newShader(inf);
  329. shaderRefs[shaderType] = shader;
  330. const ShaderTypeBit shaderBit = ShaderTypeBit(1 << shaderType);
  331. if(!!(shaderBit & ShaderTypeBit::kAllGraphics))
  332. {
  333. progInf.m_graphicsShaders[shaderType] = shader.get();
  334. }
  335. else if(shaderType == ShaderType::kCompute)
  336. {
  337. progInf.m_computeShader = shader.get();
  338. }
  339. else
  340. {
  341. ANKI_ASSERT(0);
  342. }
  343. }
  344. // Create the program
  345. variant->m_prog = GrManager::getSingleton().newShaderProgram(progInf);
  346. }
  347. else
  348. {
  349. ANKI_ASSERT(!!(m_shaderStages & ShaderTypeBit::kAllRayTracing));
  350. // Find the library
  351. CString libName = &binary.m_libraryName[0];
  352. ANKI_ASSERT(libName.getLength() > 0);
  353. const ShaderProgramResourceSystem& progSystem = ResourceManager::getSingleton().getShaderProgramResourceSystem();
  354. const ShaderProgramRaytracingLibrary* foundLib = nullptr;
  355. for(const ShaderProgramRaytracingLibrary& lib : progSystem.getRayTracingLibraries())
  356. {
  357. if(lib.getLibraryName() == libName)
  358. {
  359. foundLib = &lib;
  360. break;
  361. }
  362. }
  363. ANKI_ASSERT(foundLib);
  364. variant->m_prog = foundLib->getShaderProgram();
  365. // Set the group handle index
  366. variant->m_shaderGroupHandleIndex = foundLib->getShaderGroupHandleIndex(getFilename(), mutationHash);
  367. }
  368. return variant;
  369. }
  370. } // end namespace anki