ShaderProgramCompiler.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. // Copyright (C) 2009-2023, 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/Dxc.h>
  8. #include <AnKi/Util/Serializer.h>
  9. #include <AnKi/Util/HashMap.h>
  10. namespace anki {
  11. void freeShaderProgramBinary(ShaderProgramBinary*& binary)
  12. {
  13. if(binary == nullptr)
  14. {
  15. return;
  16. }
  17. BaseMemoryPool& mempool = ShaderCompilerMemoryPool::getSingleton();
  18. for(ShaderProgramBinaryCodeBlock& code : binary->m_codeBlocks)
  19. {
  20. mempool.free(code.m_binary.getBegin());
  21. }
  22. mempool.free(binary->m_codeBlocks.getBegin());
  23. for(ShaderProgramBinaryMutator& mutator : binary->m_mutators)
  24. {
  25. mempool.free(mutator.m_values.getBegin());
  26. }
  27. mempool.free(binary->m_mutators.getBegin());
  28. for(ShaderProgramBinaryMutation& m : binary->m_mutations)
  29. {
  30. mempool.free(m.m_values.getBegin());
  31. }
  32. mempool.free(binary->m_mutations.getBegin());
  33. for(ShaderProgramBinaryVariant& variant : binary->m_variants)
  34. {
  35. mempool.free(variant.m_techniqueCodeBlocks.getBegin());
  36. }
  37. mempool.free(binary->m_variants.getBegin());
  38. mempool.free(binary->m_techniques.getBegin());
  39. for(ShaderProgramBinaryStruct& s : binary->m_structs)
  40. {
  41. mempool.free(s.m_members.getBegin());
  42. }
  43. mempool.free(binary->m_structs.getBegin());
  44. mempool.free(binary);
  45. binary = nullptr;
  46. }
  47. /// Spin the dials. Used to compute all mutator combinations.
  48. static Bool spinDials(ShaderCompilerDynamicArray<U32>& dials, ConstWeakArray<ShaderProgramParserMutator> mutators)
  49. {
  50. ANKI_ASSERT(dials.getSize() == mutators.getSize() && dials.getSize() > 0);
  51. Bool done = true;
  52. U32 crntDial = dials.getSize() - 1;
  53. while(true)
  54. {
  55. // Turn dial
  56. ++dials[crntDial];
  57. if(dials[crntDial] >= mutators[crntDial].m_values.getSize())
  58. {
  59. if(crntDial == 0)
  60. {
  61. // Reached the 1st dial, stop spinning
  62. done = true;
  63. break;
  64. }
  65. else
  66. {
  67. dials[crntDial] = 0;
  68. --crntDial;
  69. }
  70. }
  71. else
  72. {
  73. done = false;
  74. break;
  75. }
  76. }
  77. return done;
  78. }
  79. static void compileVariantAsync(const ShaderProgramParser& parser, ShaderProgramBinaryMutation& mutation,
  80. ShaderCompilerDynamicArray<ShaderProgramBinaryVariant>& variants,
  81. ShaderCompilerDynamicArray<ShaderProgramBinaryCodeBlock>& codeBlocks,
  82. ShaderCompilerDynamicArray<U64>& sourceCodeHashes, ShaderProgramAsyncTaskInterface& taskManager, Mutex& mtx,
  83. Atomic<I32>& error)
  84. {
  85. class Ctx
  86. {
  87. public:
  88. const ShaderProgramParser* m_parser;
  89. ShaderProgramBinaryMutation* m_mutation;
  90. ShaderCompilerDynamicArray<ShaderProgramBinaryVariant>* m_variants;
  91. ShaderCompilerDynamicArray<ShaderProgramBinaryCodeBlock>* m_codeBlocks;
  92. ShaderCompilerDynamicArray<U64>* m_sourceCodeHashes;
  93. Mutex* m_mtx;
  94. Atomic<I32>* m_err;
  95. };
  96. Ctx* ctx = newInstance<Ctx>(ShaderCompilerMemoryPool::getSingleton());
  97. ctx->m_parser = &parser;
  98. ctx->m_mutation = &mutation;
  99. ctx->m_variants = &variants;
  100. ctx->m_codeBlocks = &codeBlocks;
  101. ctx->m_sourceCodeHashes = &sourceCodeHashes;
  102. ctx->m_mtx = &mtx;
  103. ctx->m_err = &error;
  104. auto callback = [](void* userData) {
  105. Ctx& ctx = *static_cast<Ctx*>(userData);
  106. class Cleanup
  107. {
  108. public:
  109. Ctx* m_ctx;
  110. ~Cleanup()
  111. {
  112. deleteInstance(ShaderCompilerMemoryPool::getSingleton(), m_ctx);
  113. }
  114. } cleanup{&ctx};
  115. if(ctx.m_err->load() != 0)
  116. {
  117. // Don't bother
  118. return;
  119. }
  120. const U32 techniqueCount = ctx.m_parser->getTechniques().getSize();
  121. // Compile the sources
  122. ShaderCompilerDynamicArray<ShaderProgramBinaryTechniqueCodeBlocks> codeBlockIndices;
  123. codeBlockIndices.resize(techniqueCount);
  124. for(auto& it : codeBlockIndices)
  125. {
  126. it.m_codeBlockIndices.fill(kMaxU32);
  127. }
  128. ShaderCompilerString compilerErrorLog;
  129. Error err = Error::kNone;
  130. U newCodeBlockCount = 0;
  131. for(U32 t = 0; t < techniqueCount && !err; ++t)
  132. {
  133. const ShaderProgramParserTechnique& technique = ctx.m_parser->getTechniques()[t];
  134. for(ShaderType shaderType : EnumBitsIterable<ShaderType, ShaderTypeBit>(technique.m_shaderTypes))
  135. {
  136. ShaderCompilerString source;
  137. ctx.m_parser->generateVariant(ctx.m_mutation->m_values, technique, shaderType, source);
  138. // Check if the source code was found before
  139. const U64 sourceCodeHash = source.computeHash();
  140. if(technique.m_activeMutators[shaderType] != kMaxU64)
  141. {
  142. LockGuard lock(*ctx.m_mtx);
  143. ANKI_ASSERT(ctx.m_sourceCodeHashes->getSize() == ctx.m_codeBlocks->getSize());
  144. Bool found = false;
  145. for(U32 i = 0; i < ctx.m_sourceCodeHashes->getSize(); ++i)
  146. {
  147. if((*ctx.m_sourceCodeHashes)[i] == sourceCodeHash)
  148. {
  149. codeBlockIndices[t].m_codeBlockIndices[shaderType] = i;
  150. found = true;
  151. break;
  152. }
  153. }
  154. if(found)
  155. {
  156. continue;
  157. }
  158. }
  159. ShaderCompilerDynamicArray<U8> spirv;
  160. err = compileHlslToSpirv(source, shaderType, ctx.m_parser->compileWith16bitTypes(), spirv, compilerErrorLog);
  161. if(err)
  162. {
  163. break;
  164. }
  165. const U64 newHash = computeHash(spirv.getBegin(), spirv.getSizeInBytes());
  166. // Add the binary if not already there
  167. {
  168. LockGuard lock(*ctx.m_mtx);
  169. Bool found = false;
  170. for(U32 j = 0; j < ctx.m_codeBlocks->getSize(); ++j)
  171. {
  172. if((*ctx.m_codeBlocks)[j].m_hash == newHash)
  173. {
  174. codeBlockIndices[t].m_codeBlockIndices[shaderType] = j;
  175. found = true;
  176. break;
  177. }
  178. }
  179. if(!found)
  180. {
  181. codeBlockIndices[t].m_codeBlockIndices[shaderType] = ctx.m_codeBlocks->getSize();
  182. auto& codeBlock = *ctx.m_codeBlocks->emplaceBack();
  183. spirv.moveAndReset(codeBlock.m_binary);
  184. codeBlock.m_hash = newHash;
  185. ctx.m_sourceCodeHashes->emplaceBack(sourceCodeHash);
  186. ANKI_ASSERT(ctx.m_sourceCodeHashes->getSize() == ctx.m_codeBlocks->getSize());
  187. ++newCodeBlockCount;
  188. }
  189. }
  190. }
  191. }
  192. if(err)
  193. {
  194. I32 expectedErr = 0;
  195. const Bool isFirstError = ctx.m_err->compareExchange(expectedErr, err._getCode());
  196. if(isFirstError)
  197. {
  198. ANKI_SHADER_COMPILER_LOGE("Shader compilation failed:\n%s", compilerErrorLog.cstr());
  199. return;
  200. }
  201. return;
  202. }
  203. // Do variant stuff
  204. {
  205. LockGuard lock(*ctx.m_mtx);
  206. Bool createVariant = true;
  207. if(newCodeBlockCount == 0)
  208. {
  209. // No new code blocks generated, search all variants to see if there is a duplicate
  210. for(U32 i = 0; i < ctx.m_variants->getSize(); ++i)
  211. {
  212. Bool same = true;
  213. for(U32 t = 0; t < techniqueCount; ++t)
  214. {
  215. const ShaderProgramBinaryTechniqueCodeBlocks& a = (*ctx.m_variants)[i].m_techniqueCodeBlocks[t];
  216. const ShaderProgramBinaryTechniqueCodeBlocks& b = codeBlockIndices[t];
  217. if(memcmp(&a, &b, sizeof(a)) != 0)
  218. {
  219. // Not the same
  220. same = false;
  221. break;
  222. }
  223. }
  224. if(same)
  225. {
  226. createVariant = false;
  227. ctx.m_mutation->m_variantIndex = i;
  228. break;
  229. }
  230. }
  231. }
  232. // Create a new variant
  233. if(createVariant)
  234. {
  235. ctx.m_mutation->m_variantIndex = ctx.m_variants->getSize();
  236. ShaderProgramBinaryVariant* variant = ctx.m_variants->emplaceBack();
  237. codeBlockIndices.moveAndReset(variant->m_techniqueCodeBlocks);
  238. }
  239. }
  240. };
  241. taskManager.enqueueTask(callback, ctx);
  242. }
  243. Error compileShaderProgramInternal(CString fname, ShaderProgramFilesystemInterface& fsystem, ShaderProgramPostParseInterface* postParseCallback,
  244. ShaderProgramAsyncTaskInterface* taskManager_, ConstWeakArray<ShaderCompilerDefine> defines,
  245. ShaderProgramBinary*& binary)
  246. {
  247. ShaderCompilerMemoryPool& memPool = ShaderCompilerMemoryPool::getSingleton();
  248. // Initialize the binary
  249. binary = newInstance<ShaderProgramBinary>(memPool);
  250. memcpy(&binary->m_magic[0], kShaderBinaryMagic, 8);
  251. // Parse source
  252. ShaderProgramParser parser(fname, &fsystem, defines);
  253. ANKI_CHECK(parser.parse());
  254. if(postParseCallback && postParseCallback->skipCompilation(parser.getHash()))
  255. {
  256. return Error::kNone;
  257. }
  258. // Get mutators
  259. U32 mutationCount = 0;
  260. if(parser.getMutators().getSize() > 0)
  261. {
  262. newArray(memPool, parser.getMutators().getSize(), binary->m_mutators);
  263. for(U32 i = 0; i < binary->m_mutators.getSize(); ++i)
  264. {
  265. ShaderProgramBinaryMutator& out = binary->m_mutators[i];
  266. const ShaderProgramParserMutator& in = parser.getMutators()[i];
  267. zeroMemory(out);
  268. newArray(memPool, in.m_values.getSize(), out.m_values);
  269. memcpy(out.m_values.getBegin(), in.m_values.getBegin(), in.m_values.getSizeInBytes());
  270. memcpy(out.m_name.getBegin(), in.m_name.cstr(), in.m_name.getLength() + 1);
  271. // Update the count
  272. mutationCount = (i == 0) ? out.m_values.getSize() : mutationCount * out.m_values.getSize();
  273. }
  274. }
  275. else
  276. {
  277. ANKI_ASSERT(binary->m_mutators.getSize() == 0);
  278. }
  279. // Create all variants
  280. Mutex mtx;
  281. Atomic<I32> errorAtomic(0);
  282. class SyncronousShaderProgramAsyncTaskInterface : public ShaderProgramAsyncTaskInterface
  283. {
  284. public:
  285. void enqueueTask(void (*callback)(void* userData), void* userData) final
  286. {
  287. callback(userData);
  288. }
  289. Error joinTasks() final
  290. {
  291. // Nothing
  292. return Error::kNone;
  293. }
  294. } syncTaskManager;
  295. ShaderProgramAsyncTaskInterface& taskManager = (taskManager_) ? *taskManager_ : syncTaskManager;
  296. if(parser.getMutators().getSize() > 0)
  297. {
  298. // Initialize
  299. ShaderCompilerDynamicArray<MutatorValue> mutationValues;
  300. mutationValues.resize(parser.getMutators().getSize());
  301. ShaderCompilerDynamicArray<U32> dials;
  302. dials.resize(parser.getMutators().getSize(), 0);
  303. ShaderCompilerDynamicArray<ShaderProgramBinaryVariant> variants;
  304. ShaderCompilerDynamicArray<ShaderProgramBinaryCodeBlock> codeBlocks;
  305. ShaderCompilerDynamicArray<U64> sourceCodeHashes;
  306. ShaderCompilerDynamicArray<ShaderProgramBinaryMutation> mutations;
  307. mutations.resize(mutationCount);
  308. ShaderCompilerHashMap<U64, U32> mutationHashToIdx;
  309. // Grow the storage of the variants array. Can't have it resize, threads will work on stale data
  310. variants.resizeStorage(mutationCount);
  311. mutationCount = 0;
  312. // Spin for all possible combinations of mutators and
  313. // - Create the spirv
  314. // - Populate the binary variant
  315. do
  316. {
  317. // Create the mutation
  318. for(U32 i = 0; i < parser.getMutators().getSize(); ++i)
  319. {
  320. mutationValues[i] = parser.getMutators()[i].m_values[dials[i]];
  321. }
  322. ShaderProgramBinaryMutation& mutation = mutations[mutationCount++];
  323. newArray(memPool, mutationValues.getSize(), mutation.m_values);
  324. memcpy(mutation.m_values.getBegin(), mutationValues.getBegin(), mutationValues.getSizeInBytes());
  325. mutation.m_hash = computeHash(mutationValues.getBegin(), mutationValues.getSizeInBytes());
  326. ANKI_ASSERT(mutation.m_hash > 0);
  327. if(parser.skipMutation(mutationValues))
  328. {
  329. mutation.m_variantIndex = kMaxU32;
  330. }
  331. else
  332. {
  333. // New and unique mutation and thus variant, add it
  334. compileVariantAsync(parser, mutation, variants, codeBlocks, sourceCodeHashes, taskManager, mtx, errorAtomic);
  335. ANKI_ASSERT(mutationHashToIdx.find(mutation.m_hash) == mutationHashToIdx.getEnd());
  336. mutationHashToIdx.emplace(mutation.m_hash, mutationCount - 1);
  337. }
  338. } while(!spinDials(dials, parser.getMutators()));
  339. ANKI_ASSERT(mutationCount == mutations.getSize());
  340. // Done, wait the threads
  341. ANKI_CHECK(taskManager.joinTasks());
  342. // Now error out
  343. ANKI_CHECK(Error(errorAtomic.getNonAtomically()));
  344. // Store temp containers to binary
  345. codeBlocks.moveAndReset(binary->m_codeBlocks);
  346. mutations.moveAndReset(binary->m_mutations);
  347. variants.moveAndReset(binary->m_variants);
  348. }
  349. else
  350. {
  351. newArray(memPool, 1, binary->m_mutations);
  352. ShaderCompilerDynamicArray<ShaderProgramBinaryVariant> variants;
  353. ShaderCompilerDynamicArray<ShaderProgramBinaryCodeBlock> codeBlocks;
  354. ShaderCompilerDynamicArray<U64> sourceCodeHashes;
  355. compileVariantAsync(parser, binary->m_mutations[0], variants, codeBlocks, sourceCodeHashes, taskManager, mtx, errorAtomic);
  356. ANKI_CHECK(taskManager.joinTasks());
  357. ANKI_CHECK(Error(errorAtomic.getNonAtomically()));
  358. ANKI_ASSERT(codeBlocks.getSize() >= parser.getTechniques().getSize());
  359. ANKI_ASSERT(binary->m_mutations[0].m_variantIndex == 0);
  360. ANKI_ASSERT(variants.getSize() == 1);
  361. binary->m_mutations[0].m_hash = 1;
  362. codeBlocks.moveAndReset(binary->m_codeBlocks);
  363. variants.moveAndReset(binary->m_variants);
  364. }
  365. // Sort the mutations
  366. std::sort(binary->m_mutations.getBegin(), binary->m_mutations.getEnd(),
  367. [](const ShaderProgramBinaryMutation& a, const ShaderProgramBinaryMutation& b) {
  368. return a.m_hash < b.m_hash;
  369. });
  370. // Techniques
  371. newArray(memPool, parser.getTechniques().getSize(), binary->m_techniques);
  372. for(U32 i = 0; i < parser.getTechniques().getSize(); ++i)
  373. {
  374. zeroMemory(binary->m_techniques[i].m_name);
  375. memcpy(binary->m_techniques[i].m_name.getBegin(), parser.getTechniques()[i].m_name.cstr(), parser.getTechniques()[i].m_name.getLength() + 1);
  376. binary->m_techniques[i].m_shaderTypes = parser.getTechniques()[i].m_shaderTypes;
  377. binary->m_shaderTypes |= parser.getTechniques()[i].m_shaderTypes;
  378. }
  379. // Structs
  380. if(parser.getGhostStructs().getSize())
  381. {
  382. newArray(memPool, parser.getGhostStructs().getSize(), binary->m_structs);
  383. }
  384. for(U32 i = 0; i < parser.getGhostStructs().getSize(); ++i)
  385. {
  386. const ShaderProgramParserGhostStruct& in = parser.getGhostStructs()[i];
  387. ShaderProgramBinaryStruct& out = binary->m_structs[i];
  388. zeroMemory(out);
  389. memcpy(out.m_name.getBegin(), in.m_name.cstr(), in.m_name.getLength() + 1);
  390. ANKI_ASSERT(in.m_members.getSize());
  391. newArray(memPool, in.m_members.getSize(), out.m_members);
  392. for(U32 j = 0; j < in.m_members.getSize(); ++j)
  393. {
  394. const ShaderProgramParserMember& inm = in.m_members[j];
  395. ShaderProgramBinaryStructMember& outm = out.m_members[j];
  396. zeroMemory(outm.m_name);
  397. memcpy(outm.m_name.getBegin(), inm.m_name.cstr(), inm.m_name.getLength() + 1);
  398. outm.m_offset = inm.m_offset;
  399. outm.m_type = inm.m_type;
  400. }
  401. out.m_size = in.m_members.getBack().m_offset + getShaderVariableDataTypeInfo(in.m_members.getBack().m_type).m_size;
  402. }
  403. return Error::kNone;
  404. }
  405. Error compileShaderProgram(CString fname, ShaderProgramFilesystemInterface& fsystem, ShaderProgramPostParseInterface* postParseCallback,
  406. ShaderProgramAsyncTaskInterface* taskManager, ConstWeakArray<ShaderCompilerDefine> defines, ShaderProgramBinary*& binary)
  407. {
  408. const Error err = compileShaderProgramInternal(fname, fsystem, postParseCallback, taskManager, defines, binary);
  409. if(err)
  410. {
  411. ANKI_SHADER_COMPILER_LOGE("Failed to compile: %s", fname.cstr());
  412. freeShaderProgramBinary(binary);
  413. }
  414. return err;
  415. }
  416. } // end namespace anki