ShaderProgramCompiler.cpp 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022
  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. /// Will be stored in the binary
  271. /// @{
  272. /// [blockType][blockIdx]
  273. Array<DynamicArrayAuto<ShaderProgramBinaryBlock>, 3> m_blocks = {{m_alloc, m_alloc, m_alloc}};
  274. /// [blockType][blockIdx][varIdx]
  275. Array<DynamicArrayAuto<DynamicArrayAuto<ShaderProgramBinaryVariable>>, 3> m_vars = {
  276. {{m_alloc}, {m_alloc}, {m_alloc}}};
  277. DynamicArrayAuto<ShaderProgramBinaryOpaque> m_opaque = {m_alloc};
  278. DynamicArrayAuto<ShaderProgramBinaryConstant> m_consts = {m_alloc};
  279. /// @}
  280. /// Will be stored in a variant
  281. /// @{
  282. /// [blockType][blockInstanceIdx]
  283. Array<DynamicArrayAuto<ShaderProgramBinaryBlockInstance>, 3> m_blockInstances = {{m_alloc, m_alloc, m_alloc}};
  284. DynamicArrayAuto<ShaderProgramBinaryOpaqueInstance> m_opaqueInstances = {m_alloc};
  285. DynamicArrayAuto<ShaderProgramBinaryConstantInstance> m_constInstances = {m_alloc};
  286. Array<U32, 3> m_workgroupSizes = {MAX_U32, MAX_U32, MAX_U32};
  287. Array<U32, 3> m_workgroupSizesConstants = {MAX_U32, MAX_U32, MAX_U32};
  288. /// @}
  289. Refl(const GenericMemoryPoolAllocator<U8>& alloc)
  290. : m_alloc(alloc)
  291. {
  292. }
  293. Error setWorkgroupSizes(U32 x, U32 y, U32 z, U32 specConstMask) final
  294. {
  295. m_workgroupSizesConstants = {MAX_U32, MAX_U32, MAX_U32};
  296. m_workgroupSizes = {MAX_U32, MAX_U32, MAX_U32};
  297. const Array<U32, 3> input = {x, y, z};
  298. for(U32 i = 0; i < 3; ++i)
  299. {
  300. if(specConstMask & (1 << i))
  301. {
  302. for(const ShaderProgramBinaryConstantInstance& c : m_constInstances)
  303. {
  304. if(m_consts[c.m_index].m_constantId == input[i])
  305. {
  306. m_workgroupSizesConstants[i] = c.m_index;
  307. break;
  308. }
  309. }
  310. if(m_workgroupSizesConstants[i] == MAX_U32)
  311. {
  312. ANKI_SHADER_COMPILER_LOGE("Reflection identified workgroup size dimension %u as spec constant but "
  313. "not such spec constant was found",
  314. i);
  315. return Error::USER_DATA;
  316. }
  317. }
  318. else
  319. {
  320. m_workgroupSizes[i] = input[i];
  321. }
  322. }
  323. return Error::NONE;
  324. }
  325. Error setCounts(U32 uniformBlockCount, U32 storageBlockCount, U32 opaqueCount, Bool pushConstantBlock,
  326. U32 constCount) final
  327. {
  328. m_blockInstances[0].create(uniformBlockCount);
  329. m_blockInstances[1].create(storageBlockCount);
  330. if(pushConstantBlock)
  331. {
  332. m_blockInstances[2].create(1);
  333. }
  334. m_opaqueInstances.create(opaqueCount);
  335. m_constInstances.create(constCount);
  336. return Error::NONE;
  337. }
  338. Error visitUniformBlock(U32 idx, CString name, U32 set, U32 binding, U32 size, U32 varCount) final
  339. {
  340. return visitAnyBlock(idx, name, set, binding, size, varCount, 0);
  341. }
  342. Error visitUniformVariable(U32 blockIdx, U32 idx, CString name, ShaderVariableDataType type,
  343. const ShaderVariableBlockInfo& blockInfo) final
  344. {
  345. return visitAnyVariable(blockIdx, idx, name, type, blockInfo, 0);
  346. }
  347. Error visitStorageBlock(U32 idx, CString name, U32 set, U32 binding, U32 size, U32 varCount) final
  348. {
  349. return visitAnyBlock(idx, name, set, binding, size, varCount, 1);
  350. }
  351. Error visitStorageVariable(U32 blockIdx, U32 idx, CString name, ShaderVariableDataType type,
  352. const ShaderVariableBlockInfo& blockInfo) final
  353. {
  354. return visitAnyVariable(blockIdx, idx, name, type, blockInfo, 1);
  355. }
  356. Error visitPushConstantsBlock(CString name, U32 size, U32 varCount) final
  357. {
  358. return visitAnyBlock(0, name, 0, 0, size, varCount, 2);
  359. }
  360. Error visitPushConstant(U32 idx, CString name, ShaderVariableDataType type,
  361. const ShaderVariableBlockInfo& blockInfo) final
  362. {
  363. return visitAnyVariable(0, idx, name, type, blockInfo, 2);
  364. }
  365. Error visitOpaque(U32 instanceIdx, CString name, ShaderVariableDataType type, U32 set, U32 binding,
  366. U32 arraySize) final
  367. {
  368. // Find the opaque
  369. U32 opaqueIdx = MAX_U32;
  370. for(U32 i = 0; i < m_opaque.getSize(); ++i)
  371. {
  372. if(name == m_opaque[i].m_name.getBegin())
  373. {
  374. if(type != m_opaque[i].m_type || set != m_opaque[i].m_set || binding != m_opaque[i].m_binding)
  375. {
  376. ANKI_SHADER_COMPILER_LOGE(
  377. "The set, binding and type can't difer between shader variants for opaque: %s", name.cstr());
  378. return Error::USER_DATA;
  379. }
  380. opaqueIdx = i;
  381. break;
  382. }
  383. }
  384. // Create the opaque
  385. if(opaqueIdx == MAX_U32)
  386. {
  387. ShaderProgramBinaryOpaque& o = *m_opaque.emplaceBack();
  388. ANKI_CHECK(setName(name, o.m_name));
  389. o.m_type = type;
  390. o.m_binding = binding;
  391. o.m_set = set;
  392. opaqueIdx = m_opaque.getSize() - 1;
  393. }
  394. // Create the instance
  395. ShaderProgramBinaryOpaqueInstance& instance = m_opaqueInstances[instanceIdx];
  396. instance.m_index = opaqueIdx;
  397. instance.m_arraySize = arraySize;
  398. return Error::NONE;
  399. }
  400. Error visitConstant(U32 instanceIdx, CString name, ShaderVariableDataType type, U32 constantId) final
  401. {
  402. // Find const
  403. U32 constIdx = MAX_U32;
  404. for(U32 i = 0; i < m_consts.getSize(); ++i)
  405. {
  406. if(name == m_consts[i].m_name.getBegin())
  407. {
  408. if(type != m_consts[i].m_type || constantId != m_consts[i].m_constantId)
  409. {
  410. ANKI_SHADER_COMPILER_LOGE(
  411. "The type, constantId and stages can't difer between shader variants for const: %s",
  412. name.cstr());
  413. return Error::USER_DATA;
  414. }
  415. constIdx = i;
  416. break;
  417. }
  418. }
  419. // Create the const
  420. if(constIdx == MAX_U32)
  421. {
  422. ShaderProgramBinaryConstant& c = *m_consts.emplaceBack();
  423. ANKI_CHECK(setName(name, c.m_name));
  424. c.m_type = type;
  425. c.m_constantId = constantId;
  426. constIdx = m_consts.getSize() - 1;
  427. }
  428. // Create the instance
  429. ShaderProgramBinaryConstantInstance& instance = m_constInstances[instanceIdx];
  430. instance.m_index = constIdx;
  431. return Error::NONE;
  432. }
  433. static ANKI_USE_RESULT Error setName(CString in, Array<char, MAX_SHADER_BINARY_NAME_LENGTH + 1>& out)
  434. {
  435. if(in.getLength() + 1 > MAX_SHADER_BINARY_NAME_LENGTH)
  436. {
  437. ANKI_SHADER_COMPILER_LOGE("Name too long: %s", in.cstr());
  438. return Error::USER_DATA;
  439. }
  440. else if(in.getLength() == 0)
  441. {
  442. ANKI_SHADER_COMPILER_LOGE("Found an empty string as name");
  443. return Error::USER_DATA;
  444. }
  445. else
  446. {
  447. memcpy(out.getBegin(), in.getBegin(), in.getLength() + 1);
  448. }
  449. return Error::NONE;
  450. }
  451. static ANKI_USE_RESULT Error findBlock(CString name, U32 set, U32 binding,
  452. ConstWeakArray<ShaderProgramBinaryBlock> arr, U32& idx)
  453. {
  454. idx = MAX_U32;
  455. for(U32 i = 0; i < arr.getSize(); ++i)
  456. {
  457. const ShaderProgramBinaryBlock& block = arr[i];
  458. if(block.m_name.getBegin() == name)
  459. {
  460. if(set != block.m_set || binding != block.m_binding)
  461. {
  462. ANKI_SHADER_COMPILER_LOGE("The set and binding can't difer between shader variants for block: %s",
  463. name.cstr());
  464. return Error::USER_DATA;
  465. }
  466. idx = i;
  467. break;
  468. }
  469. }
  470. return Error::NONE;
  471. }
  472. Error visitAnyBlock(U32 blockInstanceIdx, CString name, U32 set, U32 binding, U32 size, U32 varSize, U32 blockType)
  473. {
  474. // Init the block
  475. U32 blockIdx;
  476. ANKI_CHECK(findBlock(name, set, binding, m_blocks[blockType], blockIdx));
  477. if(blockIdx == MAX_U32)
  478. {
  479. // Not found, create it
  480. ShaderProgramBinaryBlock& block = *m_blocks[blockType].emplaceBack();
  481. ANKI_CHECK(setName(name, block.m_name));
  482. block.m_set = set;
  483. block.m_binding = binding;
  484. blockIdx = m_blocks[blockType].getSize() - 1;
  485. // Create some storage for vars as well
  486. m_vars[blockType].emplaceBack(m_alloc);
  487. ANKI_ASSERT(m_vars[blockType].getSize() == m_blocks[blockType].getSize());
  488. }
  489. // Init the instance
  490. ShaderProgramBinaryBlockInstance& instance = m_blockInstances[blockType][blockInstanceIdx];
  491. instance.m_index = blockIdx;
  492. instance.m_size = size;
  493. instance.m_variables.setArray(m_alloc.newArray<ShaderProgramBinaryVariableInstance>(varSize), varSize);
  494. return Error::NONE;
  495. }
  496. Error visitAnyVariable(U32 blockInstanceIdx, U32 varInstanceIdx, CString name, ShaderVariableDataType type,
  497. const ShaderVariableBlockInfo& blockInfo, U32 blockType)
  498. {
  499. // Find the variable
  500. U32 varIdx = MAX_U32;
  501. const U32 blockIdx = m_blockInstances[blockType][blockInstanceIdx].m_index;
  502. for(U32 i = 0; i < m_vars[blockType][blockIdx].getSize(); ++i)
  503. {
  504. const ShaderProgramBinaryVariable& var = m_vars[blockType][blockIdx][i];
  505. if(var.m_name.getBegin() == name)
  506. {
  507. if(var.m_type != type)
  508. {
  509. ANKI_SHADER_COMPILER_LOGE("The type should not differ between variants for variable: %s",
  510. name.cstr());
  511. return Error::USER_DATA;
  512. }
  513. varIdx = i;
  514. break;
  515. }
  516. }
  517. // Create the variable
  518. if(varIdx == MAX_U32)
  519. {
  520. ShaderProgramBinaryVariable& var = *m_vars[blockType][blockIdx].emplaceBack();
  521. ANKI_CHECK(setName(name, var.m_name));
  522. var.m_type = type;
  523. varIdx = m_vars[blockType][blockIdx].getSize() - 1;
  524. }
  525. // Init the instance
  526. ShaderProgramBinaryVariableInstance& instance =
  527. m_blockInstances[blockType][blockInstanceIdx].m_variables[varInstanceIdx];
  528. instance.m_blockInfo = blockInfo;
  529. instance.m_index = varIdx;
  530. return Error::NONE;
  531. }
  532. };
  533. static Error doReflection(ShaderProgramBinary& binary, GenericMemoryPoolAllocator<U8>& tmpAlloc,
  534. GenericMemoryPoolAllocator<U8>& binaryAlloc)
  535. {
  536. ANKI_ASSERT(binary.m_variants.getSize() > 0);
  537. Refl refl(binaryAlloc);
  538. for(ShaderProgramBinaryVariant& variant : binary.m_variants)
  539. {
  540. Array<ConstWeakArray<U8>, U32(ShaderType::COUNT)> spirvs;
  541. for(ShaderType stage : EnumIterable<ShaderType>())
  542. {
  543. if(variant.m_codeBlockIndices[stage] != MAX_U32)
  544. {
  545. spirvs[stage] = binary.m_codeBlocks[variant.m_codeBlockIndices[stage]].m_binary;
  546. }
  547. }
  548. ANKI_CHECK(performSpirvReflection(spirvs, tmpAlloc, refl));
  549. // Store the instances
  550. if(refl.m_blockInstances[0].getSize())
  551. {
  552. ShaderProgramBinaryBlockInstance* instances;
  553. U32 size, storageSize;
  554. refl.m_blockInstances[0].moveAndReset(instances, size, storageSize);
  555. variant.m_uniformBlocks.setArray(instances, size);
  556. }
  557. if(refl.m_blockInstances[1].getSize())
  558. {
  559. ShaderProgramBinaryBlockInstance* instances;
  560. U32 size, storageSize;
  561. refl.m_blockInstances[1].moveAndReset(instances, size, storageSize);
  562. variant.m_storageBlocks.setArray(instances, size);
  563. }
  564. if(refl.m_blockInstances[2].getSize())
  565. {
  566. ShaderProgramBinaryBlockInstance* instances;
  567. U32 size, storageSize;
  568. refl.m_blockInstances[2].moveAndReset(instances, size, storageSize);
  569. ANKI_ASSERT(size == 1);
  570. variant.m_pushConstantBlock = instances;
  571. }
  572. if(refl.m_opaqueInstances.getSize())
  573. {
  574. ShaderProgramBinaryOpaqueInstance* instances;
  575. U32 size, storageSize;
  576. refl.m_opaqueInstances.moveAndReset(instances, size, storageSize);
  577. variant.m_opaques.setArray(instances, size);
  578. }
  579. if(refl.m_constInstances.getSize())
  580. {
  581. ShaderProgramBinaryConstantInstance* instances;
  582. U32 size, storageSize;
  583. refl.m_constInstances.moveAndReset(instances, size, storageSize);
  584. variant.m_constants.setArray(instances, size);
  585. }
  586. variant.m_workgroupSizes = refl.m_workgroupSizes;
  587. variant.m_workgroupSizesConstants = refl.m_workgroupSizesConstants;
  588. }
  589. if(refl.m_blocks[0].getSize())
  590. {
  591. ShaderProgramBinaryBlock* blocks;
  592. U32 size, storageSize;
  593. refl.m_blocks[0].moveAndReset(blocks, size, storageSize);
  594. binary.m_uniformBlocks.setArray(blocks, size);
  595. for(U32 i = 0; i < size; ++i)
  596. {
  597. ShaderProgramBinaryVariable* vars;
  598. U32 varSize, varStorageSize;
  599. refl.m_vars[0][i].moveAndReset(vars, varSize, varStorageSize);
  600. binary.m_uniformBlocks[i].m_variables.setArray(vars, varSize);
  601. }
  602. }
  603. if(refl.m_blocks[1].getSize())
  604. {
  605. ShaderProgramBinaryBlock* blocks;
  606. U32 size, storageSize;
  607. refl.m_blocks[1].moveAndReset(blocks, size, storageSize);
  608. binary.m_storageBlocks.setArray(blocks, size);
  609. for(U32 i = 0; i < size; ++i)
  610. {
  611. ShaderProgramBinaryVariable* vars;
  612. U32 varSize, varStorageSize;
  613. refl.m_vars[1][i].moveAndReset(vars, varSize, varStorageSize);
  614. binary.m_storageBlocks[i].m_variables.setArray(vars, varSize);
  615. }
  616. }
  617. if(refl.m_blocks[2].getSize())
  618. {
  619. ShaderProgramBinaryBlock* blocks;
  620. U32 size, storageSize;
  621. refl.m_blocks[2].moveAndReset(blocks, size, storageSize);
  622. ANKI_ASSERT(size == 1);
  623. binary.m_pushConstantBlock = blocks;
  624. ShaderProgramBinaryVariable* vars;
  625. U32 varSize, varStorageSize;
  626. refl.m_vars[2][0].moveAndReset(vars, varSize, varStorageSize);
  627. binary.m_pushConstantBlock->m_variables.setArray(vars, varSize);
  628. }
  629. if(refl.m_opaque.getSize())
  630. {
  631. ShaderProgramBinaryOpaque* opaques;
  632. U32 size, storageSize;
  633. refl.m_opaque.moveAndReset(opaques, size, storageSize);
  634. binary.m_opaques.setArray(opaques, size);
  635. }
  636. if(refl.m_consts.getSize())
  637. {
  638. ShaderProgramBinaryConstant* consts;
  639. U32 size, storageSize;
  640. refl.m_consts.moveAndReset(consts, size, storageSize);
  641. binary.m_constants.setArray(consts, size);
  642. }
  643. return Error::NONE;
  644. }
  645. Error compileShaderProgramInternal(CString fname, ShaderProgramFilesystemInterface& fsystem,
  646. ShaderProgramPostParseInterface* postParseCallback,
  647. ShaderProgramAsyncTaskInterface* taskManager_,
  648. GenericMemoryPoolAllocator<U8> tempAllocator,
  649. const ShaderCompilerOptions& compilerOptions, ShaderProgramBinaryWrapper& binaryW)
  650. {
  651. // Initialize the binary
  652. binaryW.cleanup();
  653. binaryW.m_singleAllocation = false;
  654. GenericMemoryPoolAllocator<U8> binaryAllocator = binaryW.m_alloc;
  655. binaryW.m_binary = binaryAllocator.newInstance<ShaderProgramBinary>();
  656. ShaderProgramBinary& binary = *binaryW.m_binary;
  657. binary = {};
  658. memcpy(&binary.m_magic[0], SHADER_BINARY_MAGIC, 8);
  659. // Parse source
  660. ShaderProgramParser parser(fname, &fsystem, tempAllocator, compilerOptions);
  661. ANKI_CHECK(parser.parse());
  662. if(postParseCallback && postParseCallback->skipCompilation(parser.getHash()))
  663. {
  664. return Error::NONE;
  665. }
  666. // Get mutators
  667. U32 mutationCount = 0;
  668. if(parser.getMutators().getSize() > 0)
  669. {
  670. binary.m_mutators.setArray(binaryAllocator.newArray<ShaderProgramBinaryMutator>(parser.getMutators().getSize()),
  671. parser.getMutators().getSize());
  672. for(U32 i = 0; i < binary.m_mutators.getSize(); ++i)
  673. {
  674. ShaderProgramBinaryMutator& out = binary.m_mutators[i];
  675. const ShaderProgramParserMutator& in = parser.getMutators()[i];
  676. ANKI_ASSERT(in.getName().getLength() < out.m_name.getSize());
  677. memcpy(&out.m_name[0], in.getName().cstr(), in.getName().getLength() + 1);
  678. out.m_values.setArray(binaryAllocator.newArray<I32>(in.getValues().getSize()), in.getValues().getSize());
  679. memcpy(out.m_values.getBegin(), in.getValues().getBegin(), in.getValues().getSizeInBytes());
  680. // Update the count
  681. mutationCount = (i == 0) ? out.m_values.getSize() : mutationCount * out.m_values.getSize();
  682. }
  683. }
  684. else
  685. {
  686. ANKI_ASSERT(binary.m_mutators.getSize() == 0);
  687. }
  688. // Create all variants
  689. Mutex mtx;
  690. Atomic<I32> errorAtomic(0);
  691. class SyncronousShaderProgramAsyncTaskInterface : public ShaderProgramAsyncTaskInterface
  692. {
  693. public:
  694. void enqueueTask(void (*callback)(void* userData), void* userData) final
  695. {
  696. callback(userData);
  697. }
  698. Error joinTasks() final
  699. {
  700. // Nothing
  701. return Error::NONE;
  702. }
  703. } syncTaskManager;
  704. ShaderProgramAsyncTaskInterface& taskManager = (taskManager_) ? *taskManager_ : syncTaskManager;
  705. if(parser.getMutators().getSize() > 0)
  706. {
  707. // Initialize
  708. DynamicArrayAuto<MutatorValue> originalMutationValues(tempAllocator, parser.getMutators().getSize());
  709. DynamicArrayAuto<MutatorValue> rewrittenMutationValues(tempAllocator, parser.getMutators().getSize());
  710. DynamicArrayAuto<U32> dials(tempAllocator, parser.getMutators().getSize(), 0);
  711. DynamicArrayAuto<ShaderProgramBinaryVariant> variants(binaryAllocator);
  712. DynamicArrayAuto<ShaderProgramBinaryCodeBlock> codeBlocks(binaryAllocator);
  713. DynamicArrayAuto<ShaderProgramBinaryMutation> mutations(binaryAllocator, mutationCount);
  714. DynamicArrayAuto<U64> codeBlockHashes(tempAllocator);
  715. HashMapAuto<U64, U32> mutationHashToIdx(tempAllocator);
  716. // Grow the storage of the variants array. Can't have it resize, threads will work on stale data
  717. variants.resizeStorage(mutationCount);
  718. const ShaderProgramBinaryVariant* baseVariant = nullptr;
  719. mutationCount = 0;
  720. // Spin for all possible combinations of mutators and
  721. // - Create the spirv
  722. // - Populate the binary variant
  723. do
  724. {
  725. // Create the mutation
  726. for(U32 i = 0; i < parser.getMutators().getSize(); ++i)
  727. {
  728. originalMutationValues[i] = parser.getMutators()[i].getValues()[dials[i]];
  729. rewrittenMutationValues[i] = originalMutationValues[i];
  730. }
  731. ShaderProgramBinaryMutation& mutation = mutations[mutationCount++];
  732. mutation.m_values.setArray(binaryAllocator.newArray<MutatorValue>(originalMutationValues.getSize()),
  733. originalMutationValues.getSize());
  734. memcpy(mutation.m_values.getBegin(), originalMutationValues.getBegin(),
  735. originalMutationValues.getSizeInBytes());
  736. mutation.m_hash = computeHash(originalMutationValues.getBegin(), originalMutationValues.getSizeInBytes());
  737. ANKI_ASSERT(mutation.m_hash > 0);
  738. const Bool rewritten = parser.rewriteMutation(
  739. WeakArray<MutatorValue>(rewrittenMutationValues.getBegin(), rewrittenMutationValues.getSize()));
  740. // Create the variant
  741. if(!rewritten)
  742. {
  743. // New and unique mutation and thus variant, add it
  744. ShaderProgramBinaryVariant& variant = *variants.emplaceBack();
  745. baseVariant = (baseVariant == nullptr) ? variants.getBegin() : baseVariant;
  746. compileVariantAsync(originalMutationValues, parser, variant, codeBlocks, codeBlockHashes, tempAllocator,
  747. binaryAllocator, taskManager, mtx, errorAtomic);
  748. mutation.m_variantIndex = variants.getSize() - 1;
  749. ANKI_ASSERT(mutationHashToIdx.find(mutation.m_hash) == mutationHashToIdx.getEnd());
  750. mutationHashToIdx.emplace(mutation.m_hash, mutationCount - 1);
  751. }
  752. else
  753. {
  754. // Check if the rewritten mutation exists
  755. const U64 otherMutationHash =
  756. computeHash(rewrittenMutationValues.getBegin(), rewrittenMutationValues.getSizeInBytes());
  757. auto it = mutationHashToIdx.find(otherMutationHash);
  758. ShaderProgramBinaryVariant* variant = nullptr;
  759. if(it == mutationHashToIdx.getEnd())
  760. {
  761. // Rewrite variant not found, create it
  762. variant = variants.emplaceBack();
  763. baseVariant = (baseVariant == nullptr) ? variants.getBegin() : baseVariant;
  764. compileVariantAsync(originalMutationValues, parser, *variant, codeBlocks, codeBlockHashes,
  765. tempAllocator, binaryAllocator, taskManager, mtx, errorAtomic);
  766. ShaderProgramBinaryMutation& otherMutation = mutations[mutationCount++];
  767. otherMutation.m_values.setArray(
  768. binaryAllocator.newArray<MutatorValue>(rewrittenMutationValues.getSize()),
  769. rewrittenMutationValues.getSize());
  770. memcpy(otherMutation.m_values.getBegin(), rewrittenMutationValues.getBegin(),
  771. rewrittenMutationValues.getSizeInBytes());
  772. mutation.m_hash = otherMutationHash;
  773. mutation.m_variantIndex = variants.getSize() - 1;
  774. it = mutationHashToIdx.emplace(otherMutationHash, mutationCount - 1);
  775. }
  776. // Setup the new mutation
  777. mutation.m_variantIndex = mutations[*it].m_variantIndex;
  778. mutationHashToIdx.emplace(mutation.m_hash, U32(&mutation - mutations.getBegin()));
  779. }
  780. } while(!spinDials(dials, parser.getMutators()));
  781. ANKI_ASSERT(mutationCount == mutations.getSize());
  782. ANKI_ASSERT(baseVariant == variants.getBegin() && "Can't have the variants array grow");
  783. // Done, wait the threads
  784. ANKI_CHECK(taskManager.joinTasks());
  785. ANKI_CHECK(Error(errorAtomic.getNonAtomically()));
  786. // Store temp containers to binary
  787. U32 size, storage;
  788. ShaderProgramBinaryVariant* firstVariant;
  789. variants.moveAndReset(firstVariant, size, storage);
  790. binary.m_variants.setArray(firstVariant, size);
  791. ShaderProgramBinaryCodeBlock* firstCodeBlock;
  792. codeBlocks.moveAndReset(firstCodeBlock, size, storage);
  793. binary.m_codeBlocks.setArray(firstCodeBlock, size);
  794. ShaderProgramBinaryMutation* firstMutation;
  795. mutations.moveAndReset(firstMutation, size, storage);
  796. binary.m_mutations.setArray(firstMutation, size);
  797. }
  798. else
  799. {
  800. DynamicArrayAuto<MutatorValue> mutation(tempAllocator);
  801. DynamicArrayAuto<ShaderProgramBinaryCodeBlock> codeBlocks(binaryAllocator);
  802. DynamicArrayAuto<U64> codeBlockHashes(tempAllocator);
  803. binary.m_variants.setArray(binaryAllocator.newInstance<ShaderProgramBinaryVariant>(), 1);
  804. compileVariantAsync(mutation, parser, binary.m_variants[0], codeBlocks, codeBlockHashes, tempAllocator,
  805. binaryAllocator, taskManager, mtx, errorAtomic);
  806. ANKI_CHECK(taskManager.joinTasks());
  807. ANKI_CHECK(Error(errorAtomic.getNonAtomically()));
  808. ANKI_ASSERT(codeBlocks.getSize() == U32(__builtin_popcount(U32(parser.getShaderTypes()))));
  809. ShaderProgramBinaryCodeBlock* firstCodeBlock;
  810. U32 size, storage;
  811. codeBlocks.moveAndReset(firstCodeBlock, size, storage);
  812. binary.m_codeBlocks.setArray(firstCodeBlock, size);
  813. binary.m_mutations.setArray(binaryAllocator.newInstance<ShaderProgramBinaryMutation>(), 1);
  814. binary.m_mutations[0].m_hash = 1;
  815. binary.m_mutations[0].m_variantIndex = 0;
  816. }
  817. // Sort the mutations
  818. std::sort(
  819. binary.m_mutations.getBegin(), binary.m_mutations.getEnd(),
  820. [](const ShaderProgramBinaryMutation& a, const ShaderProgramBinaryMutation& b) { return a.m_hash < b.m_hash; });
  821. // Lib name
  822. if(parser.getLibraryName().getLength() > 0)
  823. {
  824. if(parser.getLibraryName().getLength() >= sizeof(binary.m_libraryName))
  825. {
  826. ANKI_SHADER_COMPILER_LOGE("Library name too long: %s", parser.getLibraryName().cstr());
  827. return Error::USER_DATA;
  828. }
  829. memcpy(&binary.m_libraryName[0], &parser.getLibraryName()[0], parser.getLibraryName().getLength());
  830. }
  831. binary.m_rayType = parser.getRayType();
  832. // Misc
  833. binary.m_presentShaderTypes = parser.getShaderTypes();
  834. // Reflection
  835. ANKI_CHECK(doReflection(binary, tempAllocator, binaryAllocator));
  836. return Error::NONE;
  837. }
  838. Error compileShaderProgram(CString fname, ShaderProgramFilesystemInterface& fsystem,
  839. ShaderProgramPostParseInterface* postParseCallback,
  840. ShaderProgramAsyncTaskInterface* taskManager, GenericMemoryPoolAllocator<U8> tempAllocator,
  841. const ShaderCompilerOptions& compilerOptions, ShaderProgramBinaryWrapper& binaryW)
  842. {
  843. const Error err = compileShaderProgramInternal(fname, fsystem, postParseCallback, taskManager, tempAllocator,
  844. compilerOptions, binaryW);
  845. if(err)
  846. {
  847. ANKI_SHADER_COMPILER_LOGE("Failed to compile: %s", fname.cstr());
  848. }
  849. return err;
  850. }
  851. } // end namespace anki