ShaderProgramResource.cpp 12 KB

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