ShaderProgramResource.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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.create(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.create(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)
  142. && m_shaderStages != ShaderTypeBit::kMiss && 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,
  173. const ShaderProgramResourceVariant*& variant) const
  174. {
  175. // Sanity checks
  176. ANKI_ASSERT(info.m_setMutators.getEnabledBitCount() == m_mutators.getSize());
  177. ANKI_ASSERT(info.m_setConstants.getEnabledBitCount() == m_consts.getSize());
  178. // Compute variant hash
  179. U64 hash = 0;
  180. if(m_mutators.getSize())
  181. {
  182. hash = computeHash(info.m_mutation.getBegin(), m_mutators.getSize() * sizeof(info.m_mutation[0]));
  183. }
  184. if(m_consts.getSize())
  185. {
  186. hash =
  187. appendHash(info.m_constantValues.getBegin(), m_consts.getSize() * sizeof(info.m_constantValues[0]), hash);
  188. }
  189. // Check if the variant is in the cache
  190. {
  191. RLockGuard<RWMutex> lock(m_mtx);
  192. auto it = m_variants.find(hash);
  193. if(it != m_variants.getEnd())
  194. {
  195. // Done
  196. variant = *it;
  197. return;
  198. }
  199. }
  200. // Create the variant
  201. WLockGuard<RWMutex> lock(m_mtx);
  202. // Check again
  203. auto it = m_variants.find(hash);
  204. if(it != m_variants.getEnd())
  205. {
  206. // Done
  207. variant = *it;
  208. return;
  209. }
  210. // Create
  211. ShaderProgramResourceVariant* v = createNewVariant(info);
  212. if(v)
  213. {
  214. m_variants.emplace(hash, v);
  215. }
  216. variant = v;
  217. }
  218. ShaderProgramResourceVariant*
  219. ShaderProgramResource::createNewVariant(const ShaderProgramResourceVariantInitInfo& info) const
  220. {
  221. const ShaderProgramBinary& binary = m_binary.getBinary();
  222. // Get the binary program variant
  223. const ShaderProgramBinaryVariant* binaryVariant = nullptr;
  224. U64 mutationHash = 0;
  225. if(m_mutators.getSize())
  226. {
  227. // Create the mutation hash
  228. mutationHash = computeHash(info.m_mutation.getBegin(), m_mutators.getSize() * sizeof(info.m_mutation[0]));
  229. // Search for the mutation in the binary
  230. // TODO optimize the search
  231. for(const ShaderProgramBinaryMutation& mutation : binary.m_mutations)
  232. {
  233. if(mutation.m_hash == mutationHash)
  234. {
  235. if(mutation.m_variantIndex == kMaxU32)
  236. {
  237. // Skipped mutation, nothing to create
  238. return nullptr;
  239. }
  240. binaryVariant = &binary.m_variants[mutation.m_variantIndex];
  241. break;
  242. }
  243. }
  244. }
  245. else
  246. {
  247. ANKI_ASSERT(binary.m_variants.getSize() == 1);
  248. binaryVariant = &binary.m_variants[0];
  249. }
  250. ANKI_ASSERT(binaryVariant);
  251. ShaderProgramResourceVariant* variant =
  252. newInstance<ShaderProgramResourceVariant>(ResourceMemoryPool::getSingleton());
  253. variant->m_binaryVariant = binaryVariant;
  254. // Set the constant values
  255. Array<ShaderSpecializationConstValue, 64> constValues;
  256. U32 constValueCount = 0;
  257. for(const ShaderProgramBinaryConstantInstance& instance : binaryVariant->m_constants)
  258. {
  259. const ShaderProgramBinaryConstant& c = binary.m_constants[instance.m_index];
  260. const U32 inputIdx = m_constBinaryMapping[instance.m_index].m_constsIdx;
  261. const U32 component = m_constBinaryMapping[instance.m_index].m_component;
  262. // Get value
  263. const ShaderProgramResourceConstantValue* value = nullptr;
  264. for(U32 i = 0; i < m_consts.getSize(); ++i)
  265. {
  266. if(info.m_constantValues[i].m_constantIndex == inputIdx)
  267. {
  268. value = &info.m_constantValues[i];
  269. break;
  270. }
  271. }
  272. ANKI_ASSERT(value && "Forgot to set the value of a constant");
  273. constValues[constValueCount].m_constantId = c.m_constantId;
  274. constValues[constValueCount].m_dataType = c.m_type;
  275. constValues[constValueCount].m_int = value->m_ivec4[component];
  276. ++constValueCount;
  277. }
  278. // Get the workgroup sizes
  279. if(!!(m_shaderStages & ShaderTypeBit::kCompute))
  280. {
  281. for(U32 i = 0; i < 3; ++i)
  282. {
  283. if(binaryVariant->m_workgroupSizes[i] != kMaxU32)
  284. {
  285. // Size didn't come from specialization const
  286. variant->m_workgroupSizes[i] = binaryVariant->m_workgroupSizes[i];
  287. }
  288. else
  289. {
  290. // Size is specialization const
  291. ANKI_ASSERT(binaryVariant->m_workgroupSizesConstants[i] != kMaxU32);
  292. const U32 binaryConstIdx = binaryVariant->m_workgroupSizesConstants[i];
  293. const U32 constIdx = m_constBinaryMapping[binaryConstIdx].m_constsIdx;
  294. const U32 component = m_constBinaryMapping[binaryConstIdx].m_component;
  295. [[maybe_unused]] const Const& c = m_consts[constIdx];
  296. ANKI_ASSERT(c.m_dataType == ShaderVariableDataType::kU32
  297. || c.m_dataType == ShaderVariableDataType::kUVec2
  298. || c.m_dataType == ShaderVariableDataType::kUVec3
  299. || c.m_dataType == ShaderVariableDataType::kUVec4);
  300. // Find the value
  301. for(U32 i = 0; i < m_consts.getSize(); ++i)
  302. {
  303. if(info.m_constantValues[i].m_constantIndex == constIdx)
  304. {
  305. const I32 value = info.m_constantValues[i].m_ivec4[component];
  306. ANKI_ASSERT(value > 0);
  307. variant->m_workgroupSizes[i] = U32(value);
  308. break;
  309. }
  310. }
  311. }
  312. ANKI_ASSERT(variant->m_workgroupSizes[i] != kMaxU32);
  313. }
  314. }
  315. // Time to init the shaders
  316. if(!!(m_shaderStages & (ShaderTypeBit::kAllGraphics | ShaderTypeBit::kCompute)))
  317. {
  318. // Create the program name
  319. StringRaii progName(&ResourceMemoryPool::getSingleton());
  320. getFilepathFilename(getFilename(), progName);
  321. char* cprogName = const_cast<char*>(progName.cstr());
  322. if(progName.getLength() > kMaxGrObjectNameLength)
  323. {
  324. cprogName[kMaxGrObjectNameLength] = '\0';
  325. }
  326. ShaderProgramInitInfo progInf(cprogName);
  327. for(ShaderType shaderType : EnumIterable<ShaderType>())
  328. {
  329. if(!(ShaderTypeBit(1 << shaderType) & m_shaderStages))
  330. {
  331. continue;
  332. }
  333. ShaderInitInfo inf(cprogName);
  334. inf.m_shaderType = shaderType;
  335. inf.m_binary = binary.m_codeBlocks[binaryVariant->m_codeBlockIndices[shaderType]].m_binary;
  336. inf.m_constValues.setArray((constValueCount) ? constValues.getBegin() : nullptr, constValueCount);
  337. ShaderPtr shader = ResourceManager::getSingleton().getExternalSubsystems().m_grManager->newShader(inf);
  338. const ShaderTypeBit shaderBit = ShaderTypeBit(1 << shaderType);
  339. if(!!(shaderBit & ShaderTypeBit::kAllGraphics))
  340. {
  341. progInf.m_graphicsShaders[shaderType] = shader;
  342. }
  343. else if(shaderType == ShaderType::kCompute)
  344. {
  345. progInf.m_computeShader = std::move(shader);
  346. }
  347. else
  348. {
  349. ANKI_ASSERT(0);
  350. }
  351. }
  352. // Create the program
  353. variant->m_prog =
  354. ResourceManager::getSingleton().getExternalSubsystems().m_grManager->newShaderProgram(progInf);
  355. }
  356. else
  357. {
  358. ANKI_ASSERT(!!(m_shaderStages & ShaderTypeBit::kAllRayTracing));
  359. // Find the library
  360. CString libName = &binary.m_libraryName[0];
  361. ANKI_ASSERT(libName.getLength() > 0);
  362. const ShaderProgramResourceSystem& progSystem =
  363. ResourceManager::getSingleton().getShaderProgramResourceSystem();
  364. const ShaderProgramRaytracingLibrary* foundLib = nullptr;
  365. for(const ShaderProgramRaytracingLibrary& lib : progSystem.getRayTracingLibraries())
  366. {
  367. if(lib.getLibraryName() == libName)
  368. {
  369. foundLib = &lib;
  370. break;
  371. }
  372. }
  373. ANKI_ASSERT(foundLib);
  374. variant->m_prog = foundLib->getShaderProgram();
  375. // Set the group handle index
  376. variant->m_shaderGroupHandleIndex = foundLib->getShaderGroupHandleIndex(getFilename(), mutationHash);
  377. }
  378. return variant;
  379. }
  380. } // end namespace anki