ShaderProgramCompiler.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038
  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) 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. static ANKI_USE_RESULT Error setName(CString in, Array<char, MAX_SHADER_BINARY_NAME_LENGTH + 1>& out)
  449. {
  450. if(in.getLength() + 1 > MAX_SHADER_BINARY_NAME_LENGTH)
  451. {
  452. ANKI_SHADER_COMPILER_LOGE("Name too long: %s", in.cstr());
  453. return Error::USER_DATA;
  454. }
  455. else if(in.getLength() == 0)
  456. {
  457. ANKI_SHADER_COMPILER_LOGE("Found an empty string as name");
  458. return Error::USER_DATA;
  459. }
  460. else
  461. {
  462. memcpy(out.getBegin(), in.getBegin(), in.getLength() + 1);
  463. }
  464. return Error::NONE;
  465. }
  466. static ANKI_USE_RESULT Error findBlock(CString name, U32 set, U32 binding,
  467. ConstWeakArray<ShaderProgramBinaryBlock> arr, U32& idx)
  468. {
  469. idx = MAX_U32;
  470. for(U32 i = 0; i < arr.getSize(); ++i)
  471. {
  472. const ShaderProgramBinaryBlock& block = arr[i];
  473. if(block.m_name.getBegin() == name)
  474. {
  475. if(set != block.m_set || binding != block.m_binding)
  476. {
  477. ANKI_SHADER_COMPILER_LOGE("The set and binding can't difer between shader variants for block: %s",
  478. name.cstr());
  479. return Error::USER_DATA;
  480. }
  481. idx = i;
  482. break;
  483. }
  484. }
  485. return Error::NONE;
  486. }
  487. Error visitAnyBlock(U32 blockInstanceIdx, CString name, U32 set, U32 binding, U32 size, U32 varSize, U32 blockType)
  488. {
  489. // Init the block
  490. U32 blockIdx;
  491. ANKI_CHECK(findBlock(name, set, binding, m_blocks[blockType], blockIdx));
  492. if(blockIdx == MAX_U32)
  493. {
  494. // Not found, create it
  495. ShaderProgramBinaryBlock& block = *m_blocks[blockType].emplaceBack();
  496. ANKI_CHECK(setName(name, block.m_name));
  497. block.m_set = set;
  498. block.m_binding = binding;
  499. blockIdx = m_blocks[blockType].getSize() - 1;
  500. // Create some storage for vars as well
  501. m_vars[blockType].emplaceBack(m_alloc);
  502. ANKI_ASSERT(m_vars[blockType].getSize() == m_blocks[blockType].getSize());
  503. }
  504. // Init the instance
  505. ShaderProgramBinaryBlockInstance& instance = m_blockInstances[blockType][blockInstanceIdx];
  506. instance.m_index = blockIdx;
  507. instance.m_size = size;
  508. instance.m_variables.setArray(m_alloc.newArray<ShaderProgramBinaryVariableInstance>(varSize), varSize);
  509. return Error::NONE;
  510. }
  511. Error visitAnyVariable(U32 blockInstanceIdx, U32 varInstanceIdx, CString name, ShaderVariableDataType type,
  512. const ShaderVariableBlockInfo& blockInfo, U32 blockType)
  513. {
  514. // Find the variable
  515. U32 varIdx = MAX_U32;
  516. const U32 blockIdx = m_blockInstances[blockType][blockInstanceIdx].m_index;
  517. for(U32 i = 0; i < m_vars[blockType][blockIdx].getSize(); ++i)
  518. {
  519. const ShaderProgramBinaryVariable& var = m_vars[blockType][blockIdx][i];
  520. if(var.m_name.getBegin() == name)
  521. {
  522. if(var.m_type != type)
  523. {
  524. ANKI_SHADER_COMPILER_LOGE("The type should not differ between variants for variable: %s",
  525. name.cstr());
  526. return Error::USER_DATA;
  527. }
  528. varIdx = i;
  529. break;
  530. }
  531. }
  532. // Create the variable
  533. if(varIdx == MAX_U32)
  534. {
  535. ShaderProgramBinaryVariable& var = *m_vars[blockType][blockIdx].emplaceBack();
  536. ANKI_CHECK(setName(name, var.m_name));
  537. var.m_type = type;
  538. varIdx = m_vars[blockType][blockIdx].getSize() - 1;
  539. }
  540. // Init the instance
  541. ShaderProgramBinaryVariableInstance& instance =
  542. m_blockInstances[blockType][blockInstanceIdx].m_variables[varInstanceIdx];
  543. instance.m_blockInfo = blockInfo;
  544. instance.m_index = varIdx;
  545. return Error::NONE;
  546. }
  547. };
  548. static Error doReflection(const StringList& symbolsToReflect, ShaderProgramBinary& binary,
  549. GenericMemoryPoolAllocator<U8>& tmpAlloc, GenericMemoryPoolAllocator<U8>& binaryAlloc)
  550. {
  551. ANKI_ASSERT(binary.m_variants.getSize() > 0);
  552. Refl refl(binaryAlloc, &symbolsToReflect);
  553. for(ShaderProgramBinaryVariant& variant : binary.m_variants)
  554. {
  555. Array<ConstWeakArray<U8>, U32(ShaderType::COUNT)> spirvs;
  556. for(ShaderType stage : EnumIterable<ShaderType>())
  557. {
  558. if(variant.m_codeBlockIndices[stage] != MAX_U32)
  559. {
  560. spirvs[stage] = binary.m_codeBlocks[variant.m_codeBlockIndices[stage]].m_binary;
  561. }
  562. }
  563. ANKI_CHECK(performSpirvReflection(spirvs, tmpAlloc, refl));
  564. // Store the instances
  565. if(refl.m_blockInstances[0].getSize())
  566. {
  567. ShaderProgramBinaryBlockInstance* instances;
  568. U32 size, storageSize;
  569. refl.m_blockInstances[0].moveAndReset(instances, size, storageSize);
  570. variant.m_uniformBlocks.setArray(instances, size);
  571. }
  572. if(refl.m_blockInstances[1].getSize())
  573. {
  574. ShaderProgramBinaryBlockInstance* instances;
  575. U32 size, storageSize;
  576. refl.m_blockInstances[1].moveAndReset(instances, size, storageSize);
  577. variant.m_storageBlocks.setArray(instances, size);
  578. }
  579. if(refl.m_blockInstances[2].getSize())
  580. {
  581. ShaderProgramBinaryBlockInstance* instances;
  582. U32 size, storageSize;
  583. refl.m_blockInstances[2].moveAndReset(instances, size, storageSize);
  584. ANKI_ASSERT(size == 1);
  585. variant.m_pushConstantBlock = instances;
  586. }
  587. if(refl.m_opaqueInstances.getSize())
  588. {
  589. ShaderProgramBinaryOpaqueInstance* instances;
  590. U32 size, storageSize;
  591. refl.m_opaqueInstances.moveAndReset(instances, size, storageSize);
  592. variant.m_opaques.setArray(instances, size);
  593. }
  594. if(refl.m_constInstances.getSize())
  595. {
  596. ShaderProgramBinaryConstantInstance* instances;
  597. U32 size, storageSize;
  598. refl.m_constInstances.moveAndReset(instances, size, storageSize);
  599. variant.m_constants.setArray(instances, size);
  600. }
  601. variant.m_workgroupSizes = refl.m_workgroupSizes;
  602. variant.m_workgroupSizesConstants = refl.m_workgroupSizesConstants;
  603. }
  604. if(refl.m_blocks[0].getSize())
  605. {
  606. ShaderProgramBinaryBlock* blocks;
  607. U32 size, storageSize;
  608. refl.m_blocks[0].moveAndReset(blocks, size, storageSize);
  609. binary.m_uniformBlocks.setArray(blocks, size);
  610. for(U32 i = 0; i < size; ++i)
  611. {
  612. ShaderProgramBinaryVariable* vars;
  613. U32 varSize, varStorageSize;
  614. refl.m_vars[0][i].moveAndReset(vars, varSize, varStorageSize);
  615. binary.m_uniformBlocks[i].m_variables.setArray(vars, varSize);
  616. }
  617. }
  618. if(refl.m_blocks[1].getSize())
  619. {
  620. ShaderProgramBinaryBlock* blocks;
  621. U32 size, storageSize;
  622. refl.m_blocks[1].moveAndReset(blocks, size, storageSize);
  623. binary.m_storageBlocks.setArray(blocks, size);
  624. for(U32 i = 0; i < size; ++i)
  625. {
  626. ShaderProgramBinaryVariable* vars;
  627. U32 varSize, varStorageSize;
  628. refl.m_vars[1][i].moveAndReset(vars, varSize, varStorageSize);
  629. binary.m_storageBlocks[i].m_variables.setArray(vars, varSize);
  630. }
  631. }
  632. if(refl.m_blocks[2].getSize())
  633. {
  634. ShaderProgramBinaryBlock* blocks;
  635. U32 size, storageSize;
  636. refl.m_blocks[2].moveAndReset(blocks, size, storageSize);
  637. ANKI_ASSERT(size == 1);
  638. binary.m_pushConstantBlock = blocks;
  639. ShaderProgramBinaryVariable* vars;
  640. U32 varSize, varStorageSize;
  641. refl.m_vars[2][0].moveAndReset(vars, varSize, varStorageSize);
  642. binary.m_pushConstantBlock->m_variables.setArray(vars, varSize);
  643. }
  644. if(refl.m_opaque.getSize())
  645. {
  646. ShaderProgramBinaryOpaque* opaques;
  647. U32 size, storageSize;
  648. refl.m_opaque.moveAndReset(opaques, size, storageSize);
  649. binary.m_opaques.setArray(opaques, size);
  650. }
  651. if(refl.m_consts.getSize())
  652. {
  653. ShaderProgramBinaryConstant* consts;
  654. U32 size, storageSize;
  655. refl.m_consts.moveAndReset(consts, size, storageSize);
  656. binary.m_constants.setArray(consts, size);
  657. }
  658. return Error::NONE;
  659. }
  660. Error compileShaderProgramInternal(CString fname, ShaderProgramFilesystemInterface& fsystem,
  661. ShaderProgramPostParseInterface* postParseCallback,
  662. ShaderProgramAsyncTaskInterface* taskManager_,
  663. GenericMemoryPoolAllocator<U8> tempAllocator,
  664. const ShaderCompilerOptions& compilerOptions, ShaderProgramBinaryWrapper& binaryW)
  665. {
  666. // Initialize the binary
  667. binaryW.cleanup();
  668. binaryW.m_singleAllocation = false;
  669. GenericMemoryPoolAllocator<U8> binaryAllocator = binaryW.m_alloc;
  670. binaryW.m_binary = binaryAllocator.newInstance<ShaderProgramBinary>();
  671. ShaderProgramBinary& binary = *binaryW.m_binary;
  672. binary = {};
  673. memcpy(&binary.m_magic[0], SHADER_BINARY_MAGIC, 8);
  674. // Parse source
  675. ShaderProgramParser parser(fname, &fsystem, tempAllocator, compilerOptions);
  676. ANKI_CHECK(parser.parse());
  677. if(postParseCallback && postParseCallback->skipCompilation(parser.getHash()))
  678. {
  679. return Error::NONE;
  680. }
  681. // Get mutators
  682. U32 mutationCount = 0;
  683. if(parser.getMutators().getSize() > 0)
  684. {
  685. binary.m_mutators.setArray(binaryAllocator.newArray<ShaderProgramBinaryMutator>(parser.getMutators().getSize()),
  686. parser.getMutators().getSize());
  687. for(U32 i = 0; i < binary.m_mutators.getSize(); ++i)
  688. {
  689. ShaderProgramBinaryMutator& out = binary.m_mutators[i];
  690. const ShaderProgramParserMutator& in = parser.getMutators()[i];
  691. ANKI_ASSERT(in.getName().getLength() < out.m_name.getSize());
  692. memcpy(&out.m_name[0], in.getName().cstr(), in.getName().getLength() + 1);
  693. out.m_values.setArray(binaryAllocator.newArray<I32>(in.getValues().getSize()), in.getValues().getSize());
  694. memcpy(out.m_values.getBegin(), in.getValues().getBegin(), in.getValues().getSizeInBytes());
  695. // Update the count
  696. mutationCount = (i == 0) ? out.m_values.getSize() : mutationCount * out.m_values.getSize();
  697. }
  698. }
  699. else
  700. {
  701. ANKI_ASSERT(binary.m_mutators.getSize() == 0);
  702. }
  703. // Create all variants
  704. Mutex mtx;
  705. Atomic<I32> errorAtomic(0);
  706. class SyncronousShaderProgramAsyncTaskInterface : public ShaderProgramAsyncTaskInterface
  707. {
  708. public:
  709. void enqueueTask(void (*callback)(void* userData), void* userData) final
  710. {
  711. callback(userData);
  712. }
  713. Error joinTasks() final
  714. {
  715. // Nothing
  716. return Error::NONE;
  717. }
  718. } syncTaskManager;
  719. ShaderProgramAsyncTaskInterface& taskManager = (taskManager_) ? *taskManager_ : syncTaskManager;
  720. if(parser.getMutators().getSize() > 0)
  721. {
  722. // Initialize
  723. DynamicArrayAuto<MutatorValue> originalMutationValues(tempAllocator, parser.getMutators().getSize());
  724. DynamicArrayAuto<MutatorValue> rewrittenMutationValues(tempAllocator, parser.getMutators().getSize());
  725. DynamicArrayAuto<U32> dials(tempAllocator, parser.getMutators().getSize(), 0);
  726. DynamicArrayAuto<ShaderProgramBinaryVariant> variants(binaryAllocator);
  727. DynamicArrayAuto<ShaderProgramBinaryCodeBlock> codeBlocks(binaryAllocator);
  728. DynamicArrayAuto<ShaderProgramBinaryMutation> mutations(binaryAllocator, mutationCount);
  729. DynamicArrayAuto<U64> codeBlockHashes(tempAllocator);
  730. HashMapAuto<U64, U32> mutationHashToIdx(tempAllocator);
  731. // Grow the storage of the variants array. Can't have it resize, threads will work on stale data
  732. variants.resizeStorage(mutationCount);
  733. const ShaderProgramBinaryVariant* baseVariant = nullptr;
  734. mutationCount = 0;
  735. // Spin for all possible combinations of mutators and
  736. // - Create the spirv
  737. // - Populate the binary variant
  738. do
  739. {
  740. // Create the mutation
  741. for(U32 i = 0; i < parser.getMutators().getSize(); ++i)
  742. {
  743. originalMutationValues[i] = parser.getMutators()[i].getValues()[dials[i]];
  744. rewrittenMutationValues[i] = originalMutationValues[i];
  745. }
  746. ShaderProgramBinaryMutation& mutation = mutations[mutationCount++];
  747. mutation.m_values.setArray(binaryAllocator.newArray<MutatorValue>(originalMutationValues.getSize()),
  748. originalMutationValues.getSize());
  749. memcpy(mutation.m_values.getBegin(), originalMutationValues.getBegin(),
  750. originalMutationValues.getSizeInBytes());
  751. mutation.m_hash = computeHash(originalMutationValues.getBegin(), originalMutationValues.getSizeInBytes());
  752. ANKI_ASSERT(mutation.m_hash > 0);
  753. const Bool rewritten = parser.rewriteMutation(
  754. WeakArray<MutatorValue>(rewrittenMutationValues.getBegin(), rewrittenMutationValues.getSize()));
  755. // Create the variant
  756. if(!rewritten)
  757. {
  758. // New and unique mutation and thus variant, add it
  759. ShaderProgramBinaryVariant& variant = *variants.emplaceBack();
  760. baseVariant = (baseVariant == nullptr) ? variants.getBegin() : baseVariant;
  761. compileVariantAsync(originalMutationValues, parser, variant, codeBlocks, codeBlockHashes, tempAllocator,
  762. binaryAllocator, taskManager, mtx, errorAtomic);
  763. mutation.m_variantIndex = variants.getSize() - 1;
  764. ANKI_ASSERT(mutationHashToIdx.find(mutation.m_hash) == mutationHashToIdx.getEnd());
  765. mutationHashToIdx.emplace(mutation.m_hash, mutationCount - 1);
  766. }
  767. else
  768. {
  769. // Check if the rewritten mutation exists
  770. const U64 otherMutationHash =
  771. computeHash(rewrittenMutationValues.getBegin(), rewrittenMutationValues.getSizeInBytes());
  772. auto it = mutationHashToIdx.find(otherMutationHash);
  773. ShaderProgramBinaryVariant* variant = nullptr;
  774. if(it == mutationHashToIdx.getEnd())
  775. {
  776. // Rewrite variant not found, create it
  777. variant = variants.emplaceBack();
  778. baseVariant = (baseVariant == nullptr) ? variants.getBegin() : baseVariant;
  779. compileVariantAsync(originalMutationValues, parser, *variant, codeBlocks, codeBlockHashes,
  780. tempAllocator, binaryAllocator, taskManager, mtx, errorAtomic);
  781. ShaderProgramBinaryMutation& otherMutation = mutations[mutationCount++];
  782. otherMutation.m_values.setArray(
  783. binaryAllocator.newArray<MutatorValue>(rewrittenMutationValues.getSize()),
  784. rewrittenMutationValues.getSize());
  785. memcpy(otherMutation.m_values.getBegin(), rewrittenMutationValues.getBegin(),
  786. rewrittenMutationValues.getSizeInBytes());
  787. mutation.m_hash = otherMutationHash;
  788. mutation.m_variantIndex = variants.getSize() - 1;
  789. it = mutationHashToIdx.emplace(otherMutationHash, mutationCount - 1);
  790. }
  791. // Setup the new mutation
  792. mutation.m_variantIndex = mutations[*it].m_variantIndex;
  793. mutationHashToIdx.emplace(mutation.m_hash, U32(&mutation - mutations.getBegin()));
  794. }
  795. } while(!spinDials(dials, parser.getMutators()));
  796. ANKI_ASSERT(mutationCount == mutations.getSize());
  797. ANKI_ASSERT(baseVariant == variants.getBegin() && "Can't have the variants array grow");
  798. // Done, wait the threads
  799. ANKI_CHECK(taskManager.joinTasks());
  800. ANKI_CHECK(Error(errorAtomic.getNonAtomically()));
  801. // Store temp containers to binary
  802. U32 size, storage;
  803. ShaderProgramBinaryVariant* firstVariant;
  804. variants.moveAndReset(firstVariant, size, storage);
  805. binary.m_variants.setArray(firstVariant, size);
  806. ShaderProgramBinaryCodeBlock* firstCodeBlock;
  807. codeBlocks.moveAndReset(firstCodeBlock, size, storage);
  808. binary.m_codeBlocks.setArray(firstCodeBlock, size);
  809. ShaderProgramBinaryMutation* firstMutation;
  810. mutations.moveAndReset(firstMutation, size, storage);
  811. binary.m_mutations.setArray(firstMutation, size);
  812. }
  813. else
  814. {
  815. DynamicArrayAuto<MutatorValue> mutation(tempAllocator);
  816. DynamicArrayAuto<ShaderProgramBinaryCodeBlock> codeBlocks(binaryAllocator);
  817. DynamicArrayAuto<U64> codeBlockHashes(tempAllocator);
  818. binary.m_variants.setArray(binaryAllocator.newInstance<ShaderProgramBinaryVariant>(), 1);
  819. compileVariantAsync(mutation, parser, binary.m_variants[0], codeBlocks, codeBlockHashes, tempAllocator,
  820. binaryAllocator, taskManager, mtx, errorAtomic);
  821. ANKI_CHECK(taskManager.joinTasks());
  822. ANKI_CHECK(Error(errorAtomic.getNonAtomically()));
  823. ANKI_ASSERT(codeBlocks.getSize() == U32(__builtin_popcount(U32(parser.getShaderTypes()))));
  824. ShaderProgramBinaryCodeBlock* firstCodeBlock;
  825. U32 size, storage;
  826. codeBlocks.moveAndReset(firstCodeBlock, size, storage);
  827. binary.m_codeBlocks.setArray(firstCodeBlock, size);
  828. binary.m_mutations.setArray(binaryAllocator.newInstance<ShaderProgramBinaryMutation>(), 1);
  829. binary.m_mutations[0].m_hash = 1;
  830. binary.m_mutations[0].m_variantIndex = 0;
  831. }
  832. // Sort the mutations
  833. std::sort(
  834. binary.m_mutations.getBegin(), binary.m_mutations.getEnd(),
  835. [](const ShaderProgramBinaryMutation& a, const ShaderProgramBinaryMutation& b) { return a.m_hash < b.m_hash; });
  836. // Lib name
  837. if(parser.getLibraryName().getLength() > 0)
  838. {
  839. if(parser.getLibraryName().getLength() >= sizeof(binary.m_libraryName))
  840. {
  841. ANKI_SHADER_COMPILER_LOGE("Library name too long: %s", parser.getLibraryName().cstr());
  842. return Error::USER_DATA;
  843. }
  844. memcpy(&binary.m_libraryName[0], &parser.getLibraryName()[0], parser.getLibraryName().getLength());
  845. }
  846. binary.m_rayType = parser.getRayType();
  847. // Misc
  848. binary.m_presentShaderTypes = parser.getShaderTypes();
  849. // Reflection
  850. ANKI_CHECK(doReflection(parser.getSymbolsToReflect(), binary, tempAllocator, binaryAllocator));
  851. return Error::NONE;
  852. }
  853. Error compileShaderProgram(CString fname, ShaderProgramFilesystemInterface& fsystem,
  854. ShaderProgramPostParseInterface* postParseCallback,
  855. ShaderProgramAsyncTaskInterface* taskManager, GenericMemoryPoolAllocator<U8> tempAllocator,
  856. const ShaderCompilerOptions& compilerOptions, ShaderProgramBinaryWrapper& binaryW)
  857. {
  858. const Error err = compileShaderProgramInternal(fname, fsystem, postParseCallback, taskManager, tempAllocator,
  859. compilerOptions, binaryW);
  860. if(err)
  861. {
  862. ANKI_SHADER_COMPILER_LOGE("Failed to compile: %s", fname.cstr());
  863. }
  864. return err;
  865. }
  866. } // end namespace anki