ShaderProgramCompiler.cpp 13 KB

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