2
0

ShaderProgramResource.cpp 12 KB

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