ShaderProgramCompiler.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050
  1. // Copyright (C) 2009-2021, 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/ShaderCompiler/ShaderProgramCompiler.h>
  6. #include <AnKi/ShaderCompiler/ShaderProgramParser.h>
  7. #include <AnKi/ShaderCompiler/Glslang.h>
  8. #include <AnKi/ShaderCompiler/ShaderProgramReflection.h>
  9. #include <AnKi/Util/Serializer.h>
  10. #include <AnKi/Util/HashMap.h>
  11. namespace anki
  12. {
  13. static const char* SHADER_BINARY_MAGIC = "ANKISDR5"; ///< @warning If changed change SHADER_BINARY_VERSION
  14. const U32 SHADER_BINARY_VERSION = 5;
  15. Error ShaderProgramBinaryWrapper::serializeToFile(CString fname) const
  16. {
  17. ANKI_ASSERT(m_binary);
  18. File file;
  19. ANKI_CHECK(file.open(fname, FileOpenFlag::WRITE | FileOpenFlag::BINARY));
  20. BinarySerializer serializer;
  21. HeapAllocator<U8> tmpAlloc(m_alloc.getMemoryPool().getAllocationCallback(),
  22. m_alloc.getMemoryPool().getAllocationCallbackUserData());
  23. ANKI_CHECK(serializer.serialize(*m_binary, tmpAlloc, file));
  24. return Error::NONE;
  25. }
  26. Error ShaderProgramBinaryWrapper::deserializeFromFile(CString fname)
  27. {
  28. cleanup();
  29. File file;
  30. ANKI_CHECK(file.open(fname, FileOpenFlag::READ | FileOpenFlag::BINARY));
  31. BinaryDeserializer deserializer;
  32. ANKI_CHECK(deserializer.deserialize(m_binary, m_alloc, file));
  33. m_singleAllocation = true;
  34. if(memcmp(SHADER_BINARY_MAGIC, &m_binary->m_magic[0], strlen(SHADER_BINARY_MAGIC)) != 0)
  35. {
  36. ANKI_SHADER_COMPILER_LOGE("Corrupted or wrong version of shader binary: %s. Clean the shader cache",
  37. fname.cstr());
  38. return Error::USER_DATA;
  39. }
  40. return Error::NONE;
  41. }
  42. void ShaderProgramBinaryWrapper::cleanup()
  43. {
  44. if(m_binary == nullptr)
  45. {
  46. return;
  47. }
  48. if(!m_singleAllocation)
  49. {
  50. for(ShaderProgramBinaryMutator& mutator : m_binary->m_mutators)
  51. {
  52. m_alloc.getMemoryPool().free(mutator.m_values.getBegin());
  53. }
  54. m_alloc.getMemoryPool().free(m_binary->m_mutators.getBegin());
  55. for(ShaderProgramBinaryCodeBlock& code : m_binary->m_codeBlocks)
  56. {
  57. m_alloc.getMemoryPool().free(code.m_binary.getBegin());
  58. }
  59. m_alloc.getMemoryPool().free(m_binary->m_codeBlocks.getBegin());
  60. for(ShaderProgramBinaryMutation& m : m_binary->m_mutations)
  61. {
  62. m_alloc.getMemoryPool().free(m.m_values.getBegin());
  63. }
  64. m_alloc.getMemoryPool().free(m_binary->m_mutations.getBegin());
  65. for(ShaderProgramBinaryBlock& block : m_binary->m_uniformBlocks)
  66. {
  67. m_alloc.getMemoryPool().free(block.m_variables.getBegin());
  68. }
  69. m_alloc.getMemoryPool().free(m_binary->m_uniformBlocks.getBegin());
  70. for(ShaderProgramBinaryBlock& block : m_binary->m_storageBlocks)
  71. {
  72. m_alloc.getMemoryPool().free(block.m_variables.getBegin());
  73. }
  74. m_alloc.getMemoryPool().free(m_binary->m_storageBlocks.getBegin());
  75. if(m_binary->m_pushConstantBlock)
  76. {
  77. m_alloc.getMemoryPool().free(m_binary->m_pushConstantBlock->m_variables.getBegin());
  78. m_alloc.getMemoryPool().free(m_binary->m_pushConstantBlock);
  79. }
  80. m_alloc.getMemoryPool().free(m_binary->m_opaques.getBegin());
  81. m_alloc.getMemoryPool().free(m_binary->m_constants.getBegin());
  82. for(ShaderProgramBinaryVariant& variant : m_binary->m_variants)
  83. {
  84. for(ShaderProgramBinaryBlockInstance& block : variant.m_uniformBlocks)
  85. {
  86. m_alloc.getMemoryPool().free(block.m_variables.getBegin());
  87. }
  88. for(ShaderProgramBinaryBlockInstance& block : variant.m_storageBlocks)
  89. {
  90. m_alloc.getMemoryPool().free(block.m_variables.getBegin());
  91. }
  92. if(variant.m_pushConstantBlock)
  93. {
  94. m_alloc.getMemoryPool().free(variant.m_pushConstantBlock->m_variables.getBegin());
  95. }
  96. m_alloc.getMemoryPool().free(variant.m_uniformBlocks.getBegin());
  97. m_alloc.getMemoryPool().free(variant.m_storageBlocks.getBegin());
  98. m_alloc.getMemoryPool().free(variant.m_pushConstantBlock);
  99. m_alloc.getMemoryPool().free(variant.m_constants.getBegin());
  100. m_alloc.getMemoryPool().free(variant.m_opaques.getBegin());
  101. }
  102. m_alloc.getMemoryPool().free(m_binary->m_variants.getBegin());
  103. }
  104. m_alloc.getMemoryPool().free(m_binary);
  105. m_binary = nullptr;
  106. m_singleAllocation = false;
  107. }
  108. /// Spin the dials. Used to compute all mutator combinations.
  109. static Bool spinDials(DynamicArrayAuto<U32>& dials, ConstWeakArray<ShaderProgramParserMutator> mutators)
  110. {
  111. ANKI_ASSERT(dials.getSize() == mutators.getSize() && dials.getSize() > 0);
  112. Bool done = true;
  113. U32 crntDial = dials.getSize() - 1;
  114. while(true)
  115. {
  116. // Turn dial
  117. ++dials[crntDial];
  118. if(dials[crntDial] >= mutators[crntDial].getValues().getSize())
  119. {
  120. if(crntDial == 0)
  121. {
  122. // Reached the 1st dial, stop spinning
  123. done = true;
  124. break;
  125. }
  126. else
  127. {
  128. dials[crntDial] = 0;
  129. --crntDial;
  130. }
  131. }
  132. else
  133. {
  134. done = false;
  135. break;
  136. }
  137. }
  138. return done;
  139. }
  140. static Error compileSpirv(ConstWeakArray<MutatorValue> mutation, const ShaderProgramParser& parser,
  141. GenericMemoryPoolAllocator<U8>& tmpAlloc,
  142. Array<DynamicArrayAuto<U8>, U32(ShaderType::COUNT)>& spirv)
  143. {
  144. // Generate the source and the rest for the variant
  145. ShaderProgramParserVariant parserVariant;
  146. ANKI_CHECK(parser.generateVariant(mutation, parserVariant));
  147. // Compile stages
  148. for(ShaderType shaderType : EnumIterable<ShaderType>())
  149. {
  150. if(!(ShaderTypeBit(1 << shaderType) & parser.getShaderTypes()))
  151. {
  152. continue;
  153. }
  154. // Compile
  155. ANKI_CHECK(compilerGlslToSpirv(parserVariant.getSource(shaderType), shaderType, tmpAlloc, spirv[shaderType]));
  156. ANKI_ASSERT(spirv[shaderType].getSize() > 0);
  157. }
  158. return Error::NONE;
  159. }
  160. static void compileVariantAsync(ConstWeakArray<MutatorValue> mutation, const ShaderProgramParser& parser,
  161. ShaderProgramBinaryVariant& variant,
  162. DynamicArrayAuto<ShaderProgramBinaryCodeBlock>& codeBlocks,
  163. DynamicArrayAuto<U64>& codeBlockHashes, GenericMemoryPoolAllocator<U8>& tmpAlloc,
  164. GenericMemoryPoolAllocator<U8>& binaryAlloc,
  165. ShaderProgramAsyncTaskInterface& taskManager, Mutex& mtx, Atomic<I32>& error)
  166. {
  167. variant = {};
  168. class Ctx
  169. {
  170. public:
  171. GenericMemoryPoolAllocator<U8> m_tmpAlloc;
  172. GenericMemoryPoolAllocator<U8> m_binaryAlloc;
  173. DynamicArrayAuto<MutatorValue> m_mutation = {m_tmpAlloc};
  174. const ShaderProgramParser* m_parser;
  175. ShaderProgramBinaryVariant* m_variant;
  176. DynamicArrayAuto<ShaderProgramBinaryCodeBlock>* m_codeBlocks;
  177. DynamicArrayAuto<U64>* m_codeBlockHashes;
  178. Mutex* m_mtx;
  179. Atomic<I32>* m_err;
  180. Ctx(GenericMemoryPoolAllocator<U8> tmpAlloc)
  181. : m_tmpAlloc(tmpAlloc)
  182. {
  183. }
  184. };
  185. Ctx* ctx = tmpAlloc.newInstance<Ctx>(tmpAlloc);
  186. ctx->m_binaryAlloc = binaryAlloc;
  187. ctx->m_mutation.create(mutation.getSize());
  188. memcpy(ctx->m_mutation.getBegin(), mutation.getBegin(), mutation.getSizeInBytes());
  189. ctx->m_parser = &parser;
  190. ctx->m_variant = &variant;
  191. ctx->m_codeBlocks = &codeBlocks;
  192. ctx->m_codeBlockHashes = &codeBlockHashes;
  193. ctx->m_mtx = &mtx;
  194. ctx->m_err = &error;
  195. auto callback = [](void* userData) {
  196. Ctx& ctx = *static_cast<Ctx*>(userData);
  197. GenericMemoryPoolAllocator<U8>& tmpAlloc = ctx.m_tmpAlloc;
  198. if(ctx.m_err->load() != 0)
  199. {
  200. // Cleanup and return
  201. tmpAlloc.deleteInstance(&ctx);
  202. return;
  203. }
  204. // All good, compile the variant
  205. Array<DynamicArrayAuto<U8>, U32(ShaderType::COUNT)> spirvs = {{{tmpAlloc},
  206. {tmpAlloc},
  207. {tmpAlloc},
  208. {tmpAlloc},
  209. {tmpAlloc},
  210. {tmpAlloc},
  211. {tmpAlloc},
  212. {tmpAlloc},
  213. {tmpAlloc},
  214. {tmpAlloc},
  215. {tmpAlloc},
  216. {tmpAlloc}}};
  217. const Error err = compileSpirv(ctx.m_mutation, *ctx.m_parser, tmpAlloc, spirvs);
  218. if(!err)
  219. {
  220. // No error, check if the spirvs are common with some other variant and store it
  221. LockGuard<Mutex> lock(*ctx.m_mtx);
  222. for(ShaderType shaderType : EnumIterable<ShaderType>())
  223. {
  224. DynamicArrayAuto<U8>& spirv = spirvs[shaderType];
  225. if(spirv.isEmpty())
  226. {
  227. ctx.m_variant->m_codeBlockIndices[shaderType] = MAX_U32;
  228. continue;
  229. }
  230. // Check if the spirv is already generated
  231. const U64 newHash = computeHash(&spirv[0], spirv.getSize());
  232. Bool found = false;
  233. for(U32 i = 0; i < ctx.m_codeBlockHashes->getSize(); ++i)
  234. {
  235. if((*ctx.m_codeBlockHashes)[i] == newHash)
  236. {
  237. // Found it
  238. ctx.m_variant->m_codeBlockIndices[shaderType] = i;
  239. found = true;
  240. break;
  241. }
  242. }
  243. // Create it if not found
  244. if(!found)
  245. {
  246. U8* code = ctx.m_binaryAlloc.allocate(spirv.getSizeInBytes());
  247. memcpy(code, &spirv[0], spirv.getSizeInBytes());
  248. ShaderProgramBinaryCodeBlock block;
  249. block.m_binary.setArray(code, U32(spirv.getSizeInBytes()));
  250. block.m_hash = newHash;
  251. ctx.m_codeBlocks->emplaceBack(block);
  252. ctx.m_codeBlockHashes->emplaceBack(newHash);
  253. ctx.m_variant->m_codeBlockIndices[shaderType] = ctx.m_codeBlocks->getSize() - 1;
  254. }
  255. }
  256. }
  257. else
  258. {
  259. ctx.m_err->store(err._getCode());
  260. }
  261. // Cleanup
  262. tmpAlloc.deleteInstance(&ctx);
  263. };
  264. taskManager.enqueueTask(callback, ctx);
  265. }
  266. class Refl final : public ShaderReflectionVisitorInterface
  267. {
  268. public:
  269. GenericMemoryPoolAllocator<U8> m_alloc;
  270. const StringList* m_symbolsToReflect = nullptr;
  271. /// Will be stored in the binary
  272. /// @{
  273. /// [blockType][blockIdx]
  274. Array<DynamicArrayAuto<ShaderProgramBinaryBlock>, 3> m_blocks = {{m_alloc, m_alloc, m_alloc}};
  275. /// [blockType][blockIdx][varIdx]
  276. Array<DynamicArrayAuto<DynamicArrayAuto<ShaderProgramBinaryVariable>>, 3> m_vars = {
  277. {{m_alloc}, {m_alloc}, {m_alloc}}};
  278. DynamicArrayAuto<ShaderProgramBinaryOpaque> m_opaque = {m_alloc};
  279. DynamicArrayAuto<ShaderProgramBinaryConstant> m_consts = {m_alloc};
  280. /// @}
  281. /// Will be stored in a variant
  282. /// @{
  283. /// [blockType][blockInstanceIdx]
  284. Array<DynamicArrayAuto<ShaderProgramBinaryBlockInstance>, 3> m_blockInstances = {{m_alloc, m_alloc, m_alloc}};
  285. DynamicArrayAuto<ShaderProgramBinaryOpaqueInstance> m_opaqueInstances = {m_alloc};
  286. DynamicArrayAuto<ShaderProgramBinaryConstantInstance> m_constInstances = {m_alloc};
  287. Array<U32, 3> m_workgroupSizes = {MAX_U32, MAX_U32, MAX_U32};
  288. Array<U32, 3> m_workgroupSizesConstants = {MAX_U32, MAX_U32, MAX_U32};
  289. /// @}
  290. Refl(const GenericMemoryPoolAllocator<U8>& alloc, const StringList* symbolsToReflect)
  291. : m_alloc(alloc)
  292. , m_symbolsToReflect(symbolsToReflect)
  293. {
  294. }
  295. Error setWorkgroupSizes(U32 x, U32 y, U32 z, U32 specConstMask) final
  296. {
  297. m_workgroupSizesConstants = {MAX_U32, MAX_U32, MAX_U32};
  298. m_workgroupSizes = {MAX_U32, MAX_U32, MAX_U32};
  299. const Array<U32, 3> input = {x, y, z};
  300. for(U32 i = 0; i < 3; ++i)
  301. {
  302. if(specConstMask & (1 << i))
  303. {
  304. for(const ShaderProgramBinaryConstantInstance& c : m_constInstances)
  305. {
  306. if(m_consts[c.m_index].m_constantId == input[i])
  307. {
  308. m_workgroupSizesConstants[i] = c.m_index;
  309. break;
  310. }
  311. }
  312. if(m_workgroupSizesConstants[i] == MAX_U32)
  313. {
  314. ANKI_SHADER_COMPILER_LOGE("Reflection identified workgroup size dimension %u as spec constant but "
  315. "not such spec constant was found",
  316. i);
  317. return Error::USER_DATA;
  318. }
  319. }
  320. else
  321. {
  322. m_workgroupSizes[i] = input[i];
  323. }
  324. }
  325. return Error::NONE;
  326. }
  327. Error setCounts(U32 uniformBlockCount, U32 storageBlockCount, U32 opaqueCount, Bool pushConstantBlock,
  328. U32 constCount, U32 structCount) final
  329. {
  330. m_blockInstances[0].create(uniformBlockCount);
  331. m_blockInstances[1].create(storageBlockCount);
  332. if(pushConstantBlock)
  333. {
  334. m_blockInstances[2].create(1);
  335. }
  336. m_opaqueInstances.create(opaqueCount);
  337. m_constInstances.create(constCount);
  338. return Error::NONE;
  339. }
  340. Error visitUniformBlock(U32 idx, CString name, U32 set, U32 binding, U32 size, U32 varCount) final
  341. {
  342. return visitAnyBlock(idx, name, set, binding, size, varCount, 0);
  343. }
  344. Error visitUniformVariable(U32 blockIdx, U32 idx, CString name, ShaderVariableDataType type,
  345. const ShaderVariableBlockInfo& blockInfo) final
  346. {
  347. return visitAnyVariable(blockIdx, idx, name, type, blockInfo, 0);
  348. }
  349. Error visitStorageBlock(U32 idx, CString name, U32 set, U32 binding, U32 size, U32 varCount) final
  350. {
  351. return visitAnyBlock(idx, name, set, binding, size, varCount, 1);
  352. }
  353. Error visitStorageVariable(U32 blockIdx, U32 idx, CString name, ShaderVariableDataType type,
  354. const ShaderVariableBlockInfo& blockInfo) final
  355. {
  356. return visitAnyVariable(blockIdx, idx, name, type, blockInfo, 1);
  357. }
  358. Error visitPushConstantsBlock(CString name, U32 size, U32 varCount) final
  359. {
  360. return visitAnyBlock(0, name, 0, 0, size, varCount, 2);
  361. }
  362. Error visitPushConstant(U32 idx, CString name, ShaderVariableDataType type,
  363. const ShaderVariableBlockInfo& blockInfo) final
  364. {
  365. return visitAnyVariable(0, idx, name, type, blockInfo, 2);
  366. }
  367. Error visitOpaque(U32 instanceIdx, CString name, ShaderVariableDataType type, U32 set, U32 binding,
  368. U32 arraySize) final
  369. {
  370. // Find the opaque
  371. U32 opaqueIdx = MAX_U32;
  372. for(U32 i = 0; i < m_opaque.getSize(); ++i)
  373. {
  374. if(name == m_opaque[i].m_name.getBegin())
  375. {
  376. if(type != m_opaque[i].m_type || set != m_opaque[i].m_set || binding != m_opaque[i].m_binding)
  377. {
  378. ANKI_SHADER_COMPILER_LOGE(
  379. "The set, binding and type can't difer between shader variants for opaque: %s", name.cstr());
  380. return Error::USER_DATA;
  381. }
  382. opaqueIdx = i;
  383. break;
  384. }
  385. }
  386. // Create the opaque
  387. if(opaqueIdx == MAX_U32)
  388. {
  389. ShaderProgramBinaryOpaque& o = *m_opaque.emplaceBack();
  390. ANKI_CHECK(setName(name, o.m_name));
  391. o.m_type = type;
  392. o.m_binding = binding;
  393. o.m_set = set;
  394. opaqueIdx = m_opaque.getSize() - 1;
  395. }
  396. // Create the instance
  397. ShaderProgramBinaryOpaqueInstance& instance = m_opaqueInstances[instanceIdx];
  398. instance.m_index = opaqueIdx;
  399. instance.m_arraySize = arraySize;
  400. return Error::NONE;
  401. }
  402. Bool skipSymbol(CString symbol) const final
  403. {
  404. Bool skip = true;
  405. for(const String& s : *m_symbolsToReflect)
  406. {
  407. if(symbol == s)
  408. {
  409. skip = false;
  410. break;
  411. }
  412. }
  413. return skip;
  414. }
  415. Error visitConstant(U32 instanceIdx, CString name, ShaderVariableDataType type, U32 constantId) final
  416. {
  417. // Find const
  418. U32 constIdx = MAX_U32;
  419. for(U32 i = 0; i < m_consts.getSize(); ++i)
  420. {
  421. if(name == m_consts[i].m_name.getBegin())
  422. {
  423. if(type != m_consts[i].m_type || constantId != m_consts[i].m_constantId)
  424. {
  425. ANKI_SHADER_COMPILER_LOGE(
  426. "The type, constantId and stages can't difer between shader variants for const: %s",
  427. name.cstr());
  428. return Error::USER_DATA;
  429. }
  430. constIdx = i;
  431. break;
  432. }
  433. }
  434. // Create the const
  435. if(constIdx == MAX_U32)
  436. {
  437. ShaderProgramBinaryConstant& c = *m_consts.emplaceBack();
  438. ANKI_CHECK(setName(name, c.m_name));
  439. c.m_type = type;
  440. c.m_constantId = constantId;
  441. constIdx = m_consts.getSize() - 1;
  442. }
  443. // Create the instance
  444. ShaderProgramBinaryConstantInstance& instance = m_constInstances[instanceIdx];
  445. instance.m_index = constIdx;
  446. return Error::NONE;
  447. }
  448. Error visitStruct(U32 idx, CString name, U32 memberCount) final
  449. {
  450. // TODO
  451. return Error::NONE;
  452. }
  453. Error visitStructMember(U32 memberIdx, CString name, ShaderVariableDataType type) final
  454. {
  455. // TODO
  456. return Error::NONE;
  457. }
  458. static ANKI_USE_RESULT Error setName(CString in, Array<char, MAX_SHADER_BINARY_NAME_LENGTH + 1>& out)
  459. {
  460. if(in.getLength() + 1 > MAX_SHADER_BINARY_NAME_LENGTH)
  461. {
  462. ANKI_SHADER_COMPILER_LOGE("Name too long: %s", in.cstr());
  463. return Error::USER_DATA;
  464. }
  465. else if(in.getLength() == 0)
  466. {
  467. ANKI_SHADER_COMPILER_LOGE("Found an empty string as name");
  468. return Error::USER_DATA;
  469. }
  470. else
  471. {
  472. memcpy(out.getBegin(), in.getBegin(), in.getLength() + 1);
  473. }
  474. return Error::NONE;
  475. }
  476. static ANKI_USE_RESULT Error findBlock(CString name, U32 set, U32 binding,
  477. ConstWeakArray<ShaderProgramBinaryBlock> arr, U32& idx)
  478. {
  479. idx = MAX_U32;
  480. for(U32 i = 0; i < arr.getSize(); ++i)
  481. {
  482. const ShaderProgramBinaryBlock& block = arr[i];
  483. if(block.m_name.getBegin() == name)
  484. {
  485. if(set != block.m_set || binding != block.m_binding)
  486. {
  487. ANKI_SHADER_COMPILER_LOGE("The set and binding can't difer between shader variants for block: %s",
  488. name.cstr());
  489. return Error::USER_DATA;
  490. }
  491. idx = i;
  492. break;
  493. }
  494. }
  495. return Error::NONE;
  496. }
  497. Error visitAnyBlock(U32 blockInstanceIdx, CString name, U32 set, U32 binding, U32 size, U32 varSize, U32 blockType)
  498. {
  499. // Init the block
  500. U32 blockIdx;
  501. ANKI_CHECK(findBlock(name, set, binding, m_blocks[blockType], blockIdx));
  502. if(blockIdx == MAX_U32)
  503. {
  504. // Not found, create it
  505. ShaderProgramBinaryBlock& block = *m_blocks[blockType].emplaceBack();
  506. ANKI_CHECK(setName(name, block.m_name));
  507. block.m_set = set;
  508. block.m_binding = binding;
  509. blockIdx = m_blocks[blockType].getSize() - 1;
  510. // Create some storage for vars as well
  511. m_vars[blockType].emplaceBack(m_alloc);
  512. ANKI_ASSERT(m_vars[blockType].getSize() == m_blocks[blockType].getSize());
  513. }
  514. // Init the instance
  515. ShaderProgramBinaryBlockInstance& instance = m_blockInstances[blockType][blockInstanceIdx];
  516. instance.m_index = blockIdx;
  517. instance.m_size = size;
  518. instance.m_variables.setArray(m_alloc.newArray<ShaderProgramBinaryVariableInstance>(varSize), varSize);
  519. return Error::NONE;
  520. }
  521. Error visitAnyVariable(U32 blockInstanceIdx, U32 varInstanceIdx, CString name, ShaderVariableDataType type,
  522. const ShaderVariableBlockInfo& blockInfo, U32 blockType)
  523. {
  524. // Find the variable
  525. U32 varIdx = MAX_U32;
  526. const U32 blockIdx = m_blockInstances[blockType][blockInstanceIdx].m_index;
  527. for(U32 i = 0; i < m_vars[blockType][blockIdx].getSize(); ++i)
  528. {
  529. const ShaderProgramBinaryVariable& var = m_vars[blockType][blockIdx][i];
  530. if(var.m_name.getBegin() == name)
  531. {
  532. if(var.m_type != type)
  533. {
  534. ANKI_SHADER_COMPILER_LOGE("The type should not differ between variants for variable: %s",
  535. name.cstr());
  536. return Error::USER_DATA;
  537. }
  538. varIdx = i;
  539. break;
  540. }
  541. }
  542. // Create the variable
  543. if(varIdx == MAX_U32)
  544. {
  545. ShaderProgramBinaryVariable& var = *m_vars[blockType][blockIdx].emplaceBack();
  546. ANKI_CHECK(setName(name, var.m_name));
  547. var.m_type = type;
  548. varIdx = m_vars[blockType][blockIdx].getSize() - 1;
  549. }
  550. // Init the instance
  551. ShaderProgramBinaryVariableInstance& instance =
  552. m_blockInstances[blockType][blockInstanceIdx].m_variables[varInstanceIdx];
  553. instance.m_blockInfo = blockInfo;
  554. instance.m_index = varIdx;
  555. return Error::NONE;
  556. }
  557. };
  558. static Error doReflection(const StringList& symbolsToReflect, ShaderProgramBinary& binary,
  559. GenericMemoryPoolAllocator<U8>& tmpAlloc, GenericMemoryPoolAllocator<U8>& binaryAlloc)
  560. {
  561. ANKI_ASSERT(binary.m_variants.getSize() > 0);
  562. Refl refl(binaryAlloc, &symbolsToReflect);
  563. for(ShaderProgramBinaryVariant& variant : binary.m_variants)
  564. {
  565. Array<ConstWeakArray<U8>, U32(ShaderType::COUNT)> spirvs;
  566. for(ShaderType stage : EnumIterable<ShaderType>())
  567. {
  568. if(variant.m_codeBlockIndices[stage] != MAX_U32)
  569. {
  570. spirvs[stage] = binary.m_codeBlocks[variant.m_codeBlockIndices[stage]].m_binary;
  571. }
  572. }
  573. ANKI_CHECK(performSpirvReflection(spirvs, tmpAlloc, refl));
  574. // Store the instances
  575. if(refl.m_blockInstances[0].getSize())
  576. {
  577. ShaderProgramBinaryBlockInstance* instances;
  578. U32 size, storageSize;
  579. refl.m_blockInstances[0].moveAndReset(instances, size, storageSize);
  580. variant.m_uniformBlocks.setArray(instances, size);
  581. }
  582. if(refl.m_blockInstances[1].getSize())
  583. {
  584. ShaderProgramBinaryBlockInstance* instances;
  585. U32 size, storageSize;
  586. refl.m_blockInstances[1].moveAndReset(instances, size, storageSize);
  587. variant.m_storageBlocks.setArray(instances, size);
  588. }
  589. if(refl.m_blockInstances[2].getSize())
  590. {
  591. ShaderProgramBinaryBlockInstance* instances;
  592. U32 size, storageSize;
  593. refl.m_blockInstances[2].moveAndReset(instances, size, storageSize);
  594. ANKI_ASSERT(size == 1);
  595. variant.m_pushConstantBlock = instances;
  596. }
  597. if(refl.m_opaqueInstances.getSize())
  598. {
  599. ShaderProgramBinaryOpaqueInstance* instances;
  600. U32 size, storageSize;
  601. refl.m_opaqueInstances.moveAndReset(instances, size, storageSize);
  602. variant.m_opaques.setArray(instances, size);
  603. }
  604. if(refl.m_constInstances.getSize())
  605. {
  606. ShaderProgramBinaryConstantInstance* instances;
  607. U32 size, storageSize;
  608. refl.m_constInstances.moveAndReset(instances, size, storageSize);
  609. variant.m_constants.setArray(instances, size);
  610. }
  611. variant.m_workgroupSizes = refl.m_workgroupSizes;
  612. variant.m_workgroupSizesConstants = refl.m_workgroupSizesConstants;
  613. }
  614. if(refl.m_blocks[0].getSize())
  615. {
  616. ShaderProgramBinaryBlock* blocks;
  617. U32 size, storageSize;
  618. refl.m_blocks[0].moveAndReset(blocks, size, storageSize);
  619. binary.m_uniformBlocks.setArray(blocks, size);
  620. for(U32 i = 0; i < size; ++i)
  621. {
  622. ShaderProgramBinaryVariable* vars;
  623. U32 varSize, varStorageSize;
  624. refl.m_vars[0][i].moveAndReset(vars, varSize, varStorageSize);
  625. binary.m_uniformBlocks[i].m_variables.setArray(vars, varSize);
  626. }
  627. }
  628. if(refl.m_blocks[1].getSize())
  629. {
  630. ShaderProgramBinaryBlock* blocks;
  631. U32 size, storageSize;
  632. refl.m_blocks[1].moveAndReset(blocks, size, storageSize);
  633. binary.m_storageBlocks.setArray(blocks, size);
  634. for(U32 i = 0; i < size; ++i)
  635. {
  636. ShaderProgramBinaryVariable* vars;
  637. U32 varSize, varStorageSize;
  638. refl.m_vars[1][i].moveAndReset(vars, varSize, varStorageSize);
  639. binary.m_storageBlocks[i].m_variables.setArray(vars, varSize);
  640. }
  641. }
  642. if(refl.m_blocks[2].getSize())
  643. {
  644. ShaderProgramBinaryBlock* blocks;
  645. U32 size, storageSize;
  646. refl.m_blocks[2].moveAndReset(blocks, size, storageSize);
  647. ANKI_ASSERT(size == 1);
  648. binary.m_pushConstantBlock = blocks;
  649. ShaderProgramBinaryVariable* vars;
  650. U32 varSize, varStorageSize;
  651. refl.m_vars[2][0].moveAndReset(vars, varSize, varStorageSize);
  652. binary.m_pushConstantBlock->m_variables.setArray(vars, varSize);
  653. }
  654. if(refl.m_opaque.getSize())
  655. {
  656. ShaderProgramBinaryOpaque* opaques;
  657. U32 size, storageSize;
  658. refl.m_opaque.moveAndReset(opaques, size, storageSize);
  659. binary.m_opaques.setArray(opaques, size);
  660. }
  661. if(refl.m_consts.getSize())
  662. {
  663. ShaderProgramBinaryConstant* consts;
  664. U32 size, storageSize;
  665. refl.m_consts.moveAndReset(consts, size, storageSize);
  666. binary.m_constants.setArray(consts, size);
  667. }
  668. return Error::NONE;
  669. }
  670. Error compileShaderProgramInternal(CString fname, ShaderProgramFilesystemInterface& fsystem,
  671. ShaderProgramPostParseInterface* postParseCallback,
  672. ShaderProgramAsyncTaskInterface* taskManager_,
  673. GenericMemoryPoolAllocator<U8> tempAllocator,
  674. const ShaderCompilerOptions& compilerOptions, ShaderProgramBinaryWrapper& binaryW)
  675. {
  676. // Initialize the binary
  677. binaryW.cleanup();
  678. binaryW.m_singleAllocation = false;
  679. GenericMemoryPoolAllocator<U8> binaryAllocator = binaryW.m_alloc;
  680. binaryW.m_binary = binaryAllocator.newInstance<ShaderProgramBinary>();
  681. ShaderProgramBinary& binary = *binaryW.m_binary;
  682. binary = {};
  683. memcpy(&binary.m_magic[0], SHADER_BINARY_MAGIC, 8);
  684. // Parse source
  685. ShaderProgramParser parser(fname, &fsystem, tempAllocator, compilerOptions);
  686. ANKI_CHECK(parser.parse());
  687. if(postParseCallback && postParseCallback->skipCompilation(parser.getHash()))
  688. {
  689. return Error::NONE;
  690. }
  691. // Get mutators
  692. U32 mutationCount = 0;
  693. if(parser.getMutators().getSize() > 0)
  694. {
  695. binary.m_mutators.setArray(binaryAllocator.newArray<ShaderProgramBinaryMutator>(parser.getMutators().getSize()),
  696. parser.getMutators().getSize());
  697. for(U32 i = 0; i < binary.m_mutators.getSize(); ++i)
  698. {
  699. ShaderProgramBinaryMutator& out = binary.m_mutators[i];
  700. const ShaderProgramParserMutator& in = parser.getMutators()[i];
  701. ANKI_ASSERT(in.getName().getLength() < out.m_name.getSize());
  702. memcpy(&out.m_name[0], in.getName().cstr(), in.getName().getLength() + 1);
  703. out.m_values.setArray(binaryAllocator.newArray<I32>(in.getValues().getSize()), in.getValues().getSize());
  704. memcpy(out.m_values.getBegin(), in.getValues().getBegin(), in.getValues().getSizeInBytes());
  705. // Update the count
  706. mutationCount = (i == 0) ? out.m_values.getSize() : mutationCount * out.m_values.getSize();
  707. }
  708. }
  709. else
  710. {
  711. ANKI_ASSERT(binary.m_mutators.getSize() == 0);
  712. }
  713. // Create all variants
  714. Mutex mtx;
  715. Atomic<I32> errorAtomic(0);
  716. class SyncronousShaderProgramAsyncTaskInterface : public ShaderProgramAsyncTaskInterface
  717. {
  718. public:
  719. void enqueueTask(void (*callback)(void* userData), void* userData) final
  720. {
  721. callback(userData);
  722. }
  723. Error joinTasks() final
  724. {
  725. // Nothing
  726. return Error::NONE;
  727. }
  728. } syncTaskManager;
  729. ShaderProgramAsyncTaskInterface& taskManager = (taskManager_) ? *taskManager_ : syncTaskManager;
  730. if(parser.getMutators().getSize() > 0)
  731. {
  732. // Initialize
  733. DynamicArrayAuto<MutatorValue> originalMutationValues(tempAllocator, parser.getMutators().getSize());
  734. DynamicArrayAuto<MutatorValue> rewrittenMutationValues(tempAllocator, parser.getMutators().getSize());
  735. DynamicArrayAuto<U32> dials(tempAllocator, parser.getMutators().getSize(), 0);
  736. DynamicArrayAuto<ShaderProgramBinaryVariant> variants(binaryAllocator);
  737. DynamicArrayAuto<ShaderProgramBinaryCodeBlock> codeBlocks(binaryAllocator);
  738. DynamicArrayAuto<ShaderProgramBinaryMutation> mutations(binaryAllocator, mutationCount);
  739. DynamicArrayAuto<U64> codeBlockHashes(tempAllocator);
  740. HashMapAuto<U64, U32> mutationHashToIdx(tempAllocator);
  741. // Grow the storage of the variants array. Can't have it resize, threads will work on stale data
  742. variants.resizeStorage(mutationCount);
  743. const ShaderProgramBinaryVariant* baseVariant = nullptr;
  744. mutationCount = 0;
  745. // Spin for all possible combinations of mutators and
  746. // - Create the spirv
  747. // - Populate the binary variant
  748. do
  749. {
  750. // Create the mutation
  751. for(U32 i = 0; i < parser.getMutators().getSize(); ++i)
  752. {
  753. originalMutationValues[i] = parser.getMutators()[i].getValues()[dials[i]];
  754. rewrittenMutationValues[i] = originalMutationValues[i];
  755. }
  756. ShaderProgramBinaryMutation& mutation = mutations[mutationCount++];
  757. mutation.m_values.setArray(binaryAllocator.newArray<MutatorValue>(originalMutationValues.getSize()),
  758. originalMutationValues.getSize());
  759. memcpy(mutation.m_values.getBegin(), originalMutationValues.getBegin(),
  760. originalMutationValues.getSizeInBytes());
  761. mutation.m_hash = computeHash(originalMutationValues.getBegin(), originalMutationValues.getSizeInBytes());
  762. ANKI_ASSERT(mutation.m_hash > 0);
  763. const Bool rewritten = parser.rewriteMutation(
  764. WeakArray<MutatorValue>(rewrittenMutationValues.getBegin(), rewrittenMutationValues.getSize()));
  765. // Create the variant
  766. if(!rewritten)
  767. {
  768. // New and unique mutation and thus variant, add it
  769. ShaderProgramBinaryVariant& variant = *variants.emplaceBack();
  770. baseVariant = (baseVariant == nullptr) ? variants.getBegin() : baseVariant;
  771. compileVariantAsync(originalMutationValues, parser, variant, codeBlocks, codeBlockHashes, tempAllocator,
  772. binaryAllocator, taskManager, mtx, errorAtomic);
  773. mutation.m_variantIndex = variants.getSize() - 1;
  774. ANKI_ASSERT(mutationHashToIdx.find(mutation.m_hash) == mutationHashToIdx.getEnd());
  775. mutationHashToIdx.emplace(mutation.m_hash, mutationCount - 1);
  776. }
  777. else
  778. {
  779. // Check if the rewritten mutation exists
  780. const U64 otherMutationHash =
  781. computeHash(rewrittenMutationValues.getBegin(), rewrittenMutationValues.getSizeInBytes());
  782. auto it = mutationHashToIdx.find(otherMutationHash);
  783. ShaderProgramBinaryVariant* variant = nullptr;
  784. if(it == mutationHashToIdx.getEnd())
  785. {
  786. // Rewrite variant not found, create it
  787. variant = variants.emplaceBack();
  788. baseVariant = (baseVariant == nullptr) ? variants.getBegin() : baseVariant;
  789. compileVariantAsync(originalMutationValues, parser, *variant, codeBlocks, codeBlockHashes,
  790. tempAllocator, binaryAllocator, taskManager, mtx, errorAtomic);
  791. ShaderProgramBinaryMutation& otherMutation = mutations[mutationCount++];
  792. otherMutation.m_values.setArray(
  793. binaryAllocator.newArray<MutatorValue>(rewrittenMutationValues.getSize()),
  794. rewrittenMutationValues.getSize());
  795. memcpy(otherMutation.m_values.getBegin(), rewrittenMutationValues.getBegin(),
  796. rewrittenMutationValues.getSizeInBytes());
  797. mutation.m_hash = otherMutationHash;
  798. mutation.m_variantIndex = variants.getSize() - 1;
  799. it = mutationHashToIdx.emplace(otherMutationHash, mutationCount - 1);
  800. }
  801. // Setup the new mutation
  802. mutation.m_variantIndex = mutations[*it].m_variantIndex;
  803. mutationHashToIdx.emplace(mutation.m_hash, U32(&mutation - mutations.getBegin()));
  804. }
  805. } while(!spinDials(dials, parser.getMutators()));
  806. ANKI_ASSERT(mutationCount == mutations.getSize());
  807. ANKI_ASSERT(baseVariant == variants.getBegin() && "Can't have the variants array grow");
  808. // Done, wait the threads
  809. ANKI_CHECK(taskManager.joinTasks());
  810. ANKI_CHECK(Error(errorAtomic.getNonAtomically()));
  811. // Store temp containers to binary
  812. U32 size, storage;
  813. ShaderProgramBinaryVariant* firstVariant;
  814. variants.moveAndReset(firstVariant, size, storage);
  815. binary.m_variants.setArray(firstVariant, size);
  816. ShaderProgramBinaryCodeBlock* firstCodeBlock;
  817. codeBlocks.moveAndReset(firstCodeBlock, size, storage);
  818. binary.m_codeBlocks.setArray(firstCodeBlock, size);
  819. ShaderProgramBinaryMutation* firstMutation;
  820. mutations.moveAndReset(firstMutation, size, storage);
  821. binary.m_mutations.setArray(firstMutation, size);
  822. }
  823. else
  824. {
  825. DynamicArrayAuto<MutatorValue> mutation(tempAllocator);
  826. DynamicArrayAuto<ShaderProgramBinaryCodeBlock> codeBlocks(binaryAllocator);
  827. DynamicArrayAuto<U64> codeBlockHashes(tempAllocator);
  828. binary.m_variants.setArray(binaryAllocator.newInstance<ShaderProgramBinaryVariant>(), 1);
  829. compileVariantAsync(mutation, parser, binary.m_variants[0], codeBlocks, codeBlockHashes, tempAllocator,
  830. binaryAllocator, taskManager, mtx, errorAtomic);
  831. ANKI_CHECK(taskManager.joinTasks());
  832. ANKI_CHECK(Error(errorAtomic.getNonAtomically()));
  833. ANKI_ASSERT(codeBlocks.getSize() == U32(__builtin_popcount(U32(parser.getShaderTypes()))));
  834. ShaderProgramBinaryCodeBlock* firstCodeBlock;
  835. U32 size, storage;
  836. codeBlocks.moveAndReset(firstCodeBlock, size, storage);
  837. binary.m_codeBlocks.setArray(firstCodeBlock, size);
  838. binary.m_mutations.setArray(binaryAllocator.newInstance<ShaderProgramBinaryMutation>(), 1);
  839. binary.m_mutations[0].m_hash = 1;
  840. binary.m_mutations[0].m_variantIndex = 0;
  841. }
  842. // Sort the mutations
  843. std::sort(
  844. binary.m_mutations.getBegin(), binary.m_mutations.getEnd(),
  845. [](const ShaderProgramBinaryMutation& a, const ShaderProgramBinaryMutation& b) { return a.m_hash < b.m_hash; });
  846. // Lib name
  847. if(parser.getLibraryName().getLength() > 0)
  848. {
  849. if(parser.getLibraryName().getLength() >= sizeof(binary.m_libraryName))
  850. {
  851. ANKI_SHADER_COMPILER_LOGE("Library name too long: %s", parser.getLibraryName().cstr());
  852. return Error::USER_DATA;
  853. }
  854. memcpy(&binary.m_libraryName[0], &parser.getLibraryName()[0], parser.getLibraryName().getLength());
  855. }
  856. binary.m_rayType = parser.getRayType();
  857. // Misc
  858. binary.m_presentShaderTypes = parser.getShaderTypes();
  859. // Reflection
  860. ANKI_CHECK(doReflection(parser.getSymbolsToReflect(), binary, tempAllocator, binaryAllocator));
  861. return Error::NONE;
  862. }
  863. Error compileShaderProgram(CString fname, ShaderProgramFilesystemInterface& fsystem,
  864. ShaderProgramPostParseInterface* postParseCallback,
  865. ShaderProgramAsyncTaskInterface* taskManager, GenericMemoryPoolAllocator<U8> tempAllocator,
  866. const ShaderCompilerOptions& compilerOptions, ShaderProgramBinaryWrapper& binaryW)
  867. {
  868. const Error err = compileShaderProgramInternal(fname, fsystem, postParseCallback, taskManager, tempAllocator,
  869. compilerOptions, binaryW);
  870. if(err)
  871. {
  872. ANKI_SHADER_COMPILER_LOGE("Failed to compile: %s", fname.cstr());
  873. }
  874. return err;
  875. }
  876. } // end namespace anki