ShaderProgramResource.cpp 12 KB

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