ShaderProgramParser.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785
  1. // Copyright (C) 2009-2020, 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/shader_compiler/ShaderProgramParser.h>
  6. namespace anki
  7. {
  8. #define ANKI_PP_ERROR_MALFORMED() \
  9. ANKI_SHADER_COMPILER_LOGE("%s: Malformed expression: %s", fname.cstr(), line.cstr()); \
  10. return Error::USER_DATA
  11. #define ANKI_PP_ERROR_MALFORMED_MSG(msg_) \
  12. ANKI_SHADER_COMPILER_LOGE("%s: " msg_ ": %s", fname.cstr(), line.cstr()); \
  13. return Error::USER_DATA
  14. static const Array<CString, U32(ShaderType::COUNT)> SHADER_STAGE_NAMES = {
  15. {"VERTEX", "TESSELLATION_CONTROL", "TESSELLATION_EVALUATION", "GEOMETRY", "FRAGMENT", "COMPUTE"}};
  16. static const char* SHADER_HEADER = R"(#version 450 core
  17. #define ANKI_BACKEND_MINOR %u
  18. #define ANKI_BACKEND_MAJOR %u
  19. #define ANKI_VENDOR_%s 1
  20. #define gl_VertexID gl_VertexIndex
  21. #define gl_InstanceID gl_InstanceIndex
  22. #extension GL_EXT_control_flow_attributes : require
  23. #define ANKI_UNROLL [[unroll]]
  24. #define ANKI_LOOP [[dont_unroll]]
  25. #define ANKI_BRANCH [[branch]]
  26. #define ANKI_FLATTEN [[flatten]]
  27. #extension GL_KHR_shader_subgroup_vote : require
  28. #extension GL_KHR_shader_subgroup_ballot : require
  29. #extension GL_KHR_shader_subgroup_shuffle : require
  30. #extension GL_KHR_shader_subgroup_arithmetic : require
  31. #extension GL_EXT_samplerless_texture_functions : require
  32. #extension GL_EXT_shader_image_load_formatted : require
  33. #extension GL_EXT_nonuniform_qualifier : enable
  34. #define ANKI_MAX_BINDLESS_TEXTURES %u
  35. #define ANKI_MAX_BINDLESS_IMAGES %u
  36. #define F32 float
  37. #define Vec2 vec2
  38. #define Vec3 vec3
  39. #define Vec4 vec4
  40. #define U32 uint
  41. #define UVec2 uvec2
  42. #define UVec3 uvec3
  43. #define UVec4 uvec4
  44. #define I32 int
  45. #define IVec2 ivec2
  46. #define IVec3 ivec3
  47. #define IVec4 ivec4
  48. #define Mat3 mat3
  49. #define Mat4 mat4
  50. #define Mat3x4 mat3x4
  51. #define Bool bool
  52. #define _ANKI_CONCATENATE(a, b) a##b
  53. #define ANKI_CONCATENATE(a, b) _ANKI_CONCATENATE(a, b)
  54. #define ANKI_SPECIALIZATION_CONSTANT_I32(x, id, defltVal) layout(constant_id = id) const I32 x = defltVal
  55. #define ANKI_SPECIALIZATION_CONSTANT_IVEC2(x, id, defltVal) \
  56. layout(constant_id = id) const I32 ANKI_CONCATENATE(x, _0) = defltVal[0]; \
  57. layout(constant_id = id + 1) const I32 ANKI_CONCATENATE(x, _1) = defltVal[1]
  58. #define ANKI_SPECIALIZATION_CONSTANT_IVEC3(x, id, defltVal) \
  59. layout(constant_id = id) const I32 ANKI_CONCATENATE(x, _0) = defltVal[0]; \
  60. layout(constant_id = id + 1) const I32 ANKI_CONCATENATE(x, _1) = defltVal[1]; \
  61. layout(constant_id = id + 2) const I32 ANKI_CONCATENATE(x, _2) = defltVal[2]
  62. #define ANKI_SPECIALIZATION_CONSTANT_F32(x, id, defltVal) layout(constant_id = id) const F32 x = defltVal
  63. #define ANKI_SPECIALIZATION_CONSTANT_VEC2(x, id, defltVal) \
  64. layout(constant_id = id) const F32 ANKI_CONCATENATE(x, _0) = defltVal[0]; \
  65. layout(constant_id = id + 1) const F32 ANKI_CONCATENATE(x, _1) = defltVal[1]
  66. #define ANKI_SPECIALIZATION_CONSTANT_VEC3(x, id, defltVal) \
  67. layout(constant_id = id) const F32 ANKI_CONCATENATE(x, _0) = defltVal[0]; \
  68. layout(constant_id = id + 1) const F32 ANKI_CONCATENATE(x, _1) = defltVal[1]; \
  69. layout(constant_id = id + 2) const F32 ANKI_CONCATENATE(x, _2) = defltVal[2]
  70. )";
  71. ShaderProgramParser::ShaderProgramParser(CString fname,
  72. ShaderProgramFilesystemInterface* fsystem,
  73. GenericMemoryPoolAllocator<U8> alloc,
  74. const GpuDeviceCapabilities& gpuCapabilities,
  75. const BindlessLimits& bindlessLimits)
  76. : m_alloc(alloc)
  77. , m_fname(alloc, fname)
  78. , m_fsystem(fsystem)
  79. , m_gpuCapabilities(gpuCapabilities)
  80. , m_bindlessLimits(bindlessLimits)
  81. {
  82. }
  83. ShaderProgramParser::~ShaderProgramParser()
  84. {
  85. }
  86. void ShaderProgramParser::tokenizeLine(CString line, DynamicArrayAuto<StringAuto>& tokens) const
  87. {
  88. ANKI_ASSERT(line.getLength() > 0);
  89. StringAuto l(m_alloc, line);
  90. // Replace all tabs with spaces
  91. for(char& c : l)
  92. {
  93. if(c == '\t')
  94. {
  95. c = ' ';
  96. }
  97. }
  98. // Split
  99. StringListAuto spaceTokens(m_alloc);
  100. spaceTokens.splitString(l, ' ', false);
  101. // Create the array
  102. for(const String& s : spaceTokens)
  103. {
  104. tokens.emplaceBack(m_alloc, s);
  105. }
  106. }
  107. Error ShaderProgramParser::parsePragmaStart(const StringAuto* begin, const StringAuto* end, CString line, CString fname)
  108. {
  109. ANKI_ASSERT(begin && end);
  110. if(begin >= end)
  111. {
  112. ANKI_PP_ERROR_MALFORMED();
  113. }
  114. ShaderType shaderType = ShaderType::COUNT;
  115. if(*begin == "vert")
  116. {
  117. shaderType = ShaderType::VERTEX;
  118. }
  119. else if(*begin == "tessc")
  120. {
  121. shaderType = ShaderType::TESSELLATION_CONTROL;
  122. }
  123. else if(*begin == "tesse")
  124. {
  125. }
  126. else if(*begin == "geom")
  127. {
  128. shaderType = ShaderType::GEOMETRY;
  129. }
  130. else if(*begin == "frag")
  131. {
  132. shaderType = ShaderType::FRAGMENT;
  133. }
  134. else if(*begin == "comp")
  135. {
  136. shaderType = ShaderType::COMPUTE;
  137. }
  138. else
  139. {
  140. ANKI_PP_ERROR_MALFORMED();
  141. }
  142. m_codeLines.pushBackSprintf("#ifdef ANKI_%s", SHADER_STAGE_NAMES[shaderType].cstr());
  143. ++begin;
  144. if(begin != end)
  145. {
  146. // Should be the last token
  147. ANKI_PP_ERROR_MALFORMED();
  148. }
  149. // Set the mask
  150. ShaderTypeBit mask = ShaderTypeBit(1 << U(shaderType));
  151. if(!!(mask & m_shaderTypes))
  152. {
  153. ANKI_PP_ERROR_MALFORMED_MSG("Can't have #pragma start <shader> appearing more than once");
  154. }
  155. m_shaderTypes |= mask;
  156. // Check bounds
  157. if(m_insideShader)
  158. {
  159. ANKI_PP_ERROR_MALFORMED_MSG("Can't have #pragma start before you close the previous pragma start");
  160. }
  161. m_insideShader = true;
  162. return Error::NONE;
  163. }
  164. Error ShaderProgramParser::parsePragmaEnd(const StringAuto* begin, const StringAuto* end, CString line, CString fname)
  165. {
  166. ANKI_ASSERT(begin && end);
  167. // Check tokens
  168. if(begin != end)
  169. {
  170. ANKI_PP_ERROR_MALFORMED();
  171. }
  172. // Check bounds
  173. if(!m_insideShader)
  174. {
  175. ANKI_PP_ERROR_MALFORMED_MSG("Can't have #pragma end before you open with a pragma start");
  176. }
  177. m_insideShader = false;
  178. // Write code
  179. m_codeLines.pushBack("#endif // Shader guard");
  180. return Error::NONE;
  181. }
  182. Error ShaderProgramParser::parsePragmaMutator(
  183. const StringAuto* begin, const StringAuto* end, CString line, CString fname)
  184. {
  185. ANKI_ASSERT(begin && end);
  186. if(begin >= end)
  187. {
  188. ANKI_PP_ERROR_MALFORMED();
  189. }
  190. m_mutators.emplaceBack(m_alloc);
  191. Mutator& mutator = m_mutators.getBack();
  192. // Name
  193. {
  194. if(begin >= end)
  195. {
  196. // Need to have a name
  197. ANKI_PP_ERROR_MALFORMED();
  198. }
  199. // Check for duplicate mutators
  200. for(U32 i = 0; i < m_mutators.getSize() - 1; ++i)
  201. {
  202. if(m_mutators[i].m_name == *begin)
  203. {
  204. ANKI_PP_ERROR_MALFORMED_MSG("Duplicate mutator");
  205. }
  206. }
  207. if(begin->getLength() > MAX_SHADER_BINARY_NAME_LENGTH)
  208. {
  209. ANKI_PP_ERROR_MALFORMED_MSG("Too big name");
  210. }
  211. mutator.m_name.create(begin->toCString());
  212. ++begin;
  213. }
  214. // Values
  215. {
  216. // Gather them
  217. for(; begin < end; ++begin)
  218. {
  219. MutatorValue value = 0;
  220. if(tokenIsComment(begin->toCString()))
  221. {
  222. break;
  223. }
  224. if(begin->toNumber(value))
  225. {
  226. ANKI_PP_ERROR_MALFORMED();
  227. }
  228. mutator.m_values.emplaceBack(value);
  229. }
  230. // Check for correct count
  231. if(mutator.m_values.getSize() < 2)
  232. {
  233. ANKI_PP_ERROR_MALFORMED_MSG("Mutator with less that 2 values doesn't make sense");
  234. }
  235. std::sort(mutator.m_values.getBegin(), mutator.m_values.getEnd());
  236. // Check for duplicates
  237. for(U32 i = 1; i < mutator.m_values.getSize(); ++i)
  238. {
  239. if(mutator.m_values[i - 1] == mutator.m_values[i])
  240. {
  241. ANKI_PP_ERROR_MALFORMED_MSG("Same value appeared more than once");
  242. }
  243. }
  244. }
  245. return Error::NONE;
  246. }
  247. Error ShaderProgramParser::parsePragmaRewriteMutation(
  248. const StringAuto* begin, const StringAuto* end, CString line, CString fname)
  249. {
  250. ANKI_ASSERT(begin && end);
  251. // Some basic sanity checks
  252. const U tokenCount = end - begin;
  253. constexpr U minTokenCount = 2 + 1 + 2; // Mutator + value + "to" + mutator + value
  254. if(tokenCount < minTokenCount)
  255. {
  256. ANKI_PP_ERROR_MALFORMED();
  257. }
  258. MutationRewrite& rewrite = *m_mutationRewrites.emplaceBack(m_alloc);
  259. Bool servingFrom = true;
  260. do
  261. {
  262. if(*begin == "to")
  263. {
  264. if(servingFrom == false)
  265. {
  266. ANKI_PP_ERROR_MALFORMED();
  267. }
  268. servingFrom = false;
  269. }
  270. else
  271. {
  272. // Mutator & value
  273. // Get mutator and value
  274. const CString mutatorName = *begin;
  275. ++begin;
  276. if(begin == end)
  277. {
  278. ANKI_PP_ERROR_MALFORMED();
  279. }
  280. const CString valueStr = *begin;
  281. MutatorValue value;
  282. if(valueStr.toNumber(value))
  283. {
  284. ANKI_PP_ERROR_MALFORMED_MSG("Malformed value");
  285. }
  286. // Get or create new record
  287. if(servingFrom)
  288. {
  289. MutationRewrite::Record& rec = *rewrite.m_records.emplaceBack();
  290. for(U32 i = 0; i < m_mutators.getSize(); ++i)
  291. {
  292. if(m_mutators[i].getName() == mutatorName)
  293. {
  294. rec.m_mutatorIndex = i;
  295. break;
  296. }
  297. }
  298. if(rec.m_mutatorIndex == MAX_U32)
  299. {
  300. ANKI_PP_ERROR_MALFORMED_MSG("Mutator not found");
  301. }
  302. if(!mutatorHasValue(m_mutators[rec.m_mutatorIndex], value))
  303. {
  304. ANKI_PP_ERROR_MALFORMED_MSG("Incorect value for mutator");
  305. }
  306. rec.m_valueFrom = value;
  307. }
  308. else
  309. {
  310. Bool found = false;
  311. for(MutationRewrite::Record& rec : rewrite.m_records)
  312. {
  313. if(m_mutators[rec.m_mutatorIndex].m_name == mutatorName)
  314. {
  315. if(!mutatorHasValue(m_mutators[rec.m_mutatorIndex], value))
  316. {
  317. ANKI_PP_ERROR_MALFORMED_MSG("Incorect value for mutator");
  318. }
  319. rec.m_valueTo = value;
  320. found = true;
  321. break;
  322. }
  323. }
  324. if(!found)
  325. {
  326. ANKI_PP_ERROR_MALFORMED();
  327. }
  328. }
  329. }
  330. ++begin;
  331. } while(begin < end && !tokenIsComment(*begin));
  332. // Sort for some later cross checking
  333. std::sort(rewrite.m_records.getBegin(),
  334. rewrite.m_records.getEnd(),
  335. [](const MutationRewrite::Record& a, const MutationRewrite::Record& b) {
  336. return a.m_mutatorIndex < b.m_mutatorIndex;
  337. });
  338. // More cross checking
  339. for(U32 i = 1; i < rewrite.m_records.getSize(); ++i)
  340. {
  341. if(rewrite.m_records[i - 1].m_mutatorIndex == rewrite.m_records[i].m_mutatorIndex)
  342. {
  343. ANKI_PP_ERROR_MALFORMED_MSG("Mutator appeared more than once");
  344. }
  345. }
  346. for(U32 i = 0; i < m_mutationRewrites.getSize() - 1; ++i)
  347. {
  348. const MutationRewrite& other = m_mutationRewrites[i];
  349. if(other.m_records.getSize() != rewrite.m_records.getSize())
  350. {
  351. continue;
  352. }
  353. Bool same = true;
  354. for(U32 j = 0; j < rewrite.m_records.getSize(); ++j)
  355. {
  356. if(rewrite.m_records[j] != other.m_records[j])
  357. {
  358. same = false;
  359. break;
  360. }
  361. }
  362. if(same)
  363. {
  364. ANKI_PP_ERROR_MALFORMED_MSG("Mutation already exists");
  365. }
  366. }
  367. return Error::NONE;
  368. }
  369. Error ShaderProgramParser::parseInclude(
  370. const StringAuto* begin, const StringAuto* end, CString line, CString fname, U32 depth)
  371. {
  372. // Gather the path
  373. StringAuto path(m_alloc);
  374. for(; begin < end; ++begin)
  375. {
  376. path.append(*begin);
  377. }
  378. if(path.isEmpty())
  379. {
  380. ANKI_PP_ERROR_MALFORMED();
  381. }
  382. // Check
  383. const char firstChar = path[0];
  384. const char lastChar = path[path.getLength() - 1];
  385. if((firstChar == '\"' && lastChar == '\"') || (firstChar == '<' && lastChar == '>'))
  386. {
  387. StringAuto fname2(m_alloc);
  388. fname2.create(path.begin() + 1, path.begin() + path.getLength() - 1);
  389. if(parseFile(fname2, depth + 1))
  390. {
  391. ANKI_PP_ERROR_MALFORMED_MSG("Error parsing include. See previous errors");
  392. }
  393. }
  394. else
  395. {
  396. ANKI_PP_ERROR_MALFORMED();
  397. }
  398. return Error::NONE;
  399. }
  400. Error ShaderProgramParser::parseLine(CString line, CString fname, Bool& foundPragmaOnce, U32 depth)
  401. {
  402. // Tokenize
  403. DynamicArrayAuto<StringAuto> tokens(m_alloc);
  404. tokenizeLine(line, tokens);
  405. ANKI_ASSERT(tokens.getSize() > 0);
  406. const StringAuto* token = tokens.getBegin();
  407. const StringAuto* end = tokens.getEnd();
  408. // Skip the hash
  409. Bool foundAloneHash = false;
  410. if(*token == "#")
  411. {
  412. ++token;
  413. foundAloneHash = true;
  414. }
  415. if((token < end) && ((foundAloneHash && *token == "include") || *token == "#include"))
  416. {
  417. // We _must_ have an #include
  418. ANKI_CHECK(parseInclude(token + 1, end, line, fname, depth));
  419. }
  420. else if((token < end) && ((foundAloneHash && *token == "pragma") || *token == "#pragma"))
  421. {
  422. // We may have a #pragma once or a #pragma anki or something else
  423. ++token;
  424. if(*token == "once")
  425. {
  426. // Pragma once
  427. if(foundPragmaOnce)
  428. {
  429. ANKI_PP_ERROR_MALFORMED_MSG("Can't have more than one #pragma once per file");
  430. }
  431. if(token + 1 != end)
  432. {
  433. ANKI_PP_ERROR_MALFORMED();
  434. }
  435. // Add the guard unique for this file
  436. foundPragmaOnce = true;
  437. const U64 hash = fname.computeHash();
  438. m_codeLines.pushBackSprintf("#ifndef _ANKI_INCL_GUARD_%llu\n"
  439. "#define _ANKI_INCL_GUARD_%llu",
  440. hash,
  441. hash);
  442. }
  443. else if(*token == "anki")
  444. {
  445. // Must be a #pragma anki
  446. ++token;
  447. if(*token == "mutator")
  448. {
  449. ANKI_CHECK(parsePragmaMutator(token + 1, end, line, fname));
  450. }
  451. else if(*token == "start")
  452. {
  453. ANKI_CHECK(parsePragmaStart(token + 1, end, line, fname));
  454. }
  455. else if(*token == "end")
  456. {
  457. ANKI_CHECK(parsePragmaEnd(token + 1, end, line, fname));
  458. }
  459. else if(*token == "rewrite_mutation")
  460. {
  461. ANKI_CHECK(parsePragmaRewriteMutation(token + 1, end, line, fname));
  462. }
  463. else
  464. {
  465. ANKI_PP_ERROR_MALFORMED();
  466. }
  467. }
  468. else
  469. {
  470. // Some other pragma
  471. ANKI_SHADER_COMPILER_LOGW("Ignoring: %s", line.cstr());
  472. m_codeLines.pushBack(line);
  473. }
  474. }
  475. else
  476. {
  477. // Ignore
  478. m_codeLines.pushBack(line);
  479. }
  480. return Error::NONE;
  481. }
  482. Error ShaderProgramParser::parseFile(CString fname, U32 depth)
  483. {
  484. // First check the depth
  485. if(depth > MAX_INCLUDE_DEPTH)
  486. {
  487. ANKI_SHADER_COMPILER_LOGE("The include depth is too high. Probably circular includance");
  488. }
  489. Bool foundPragmaOnce = false;
  490. // Load file in lines
  491. StringAuto txt(m_alloc);
  492. ANKI_CHECK(m_fsystem->readAllText(fname, txt));
  493. StringListAuto lines(m_alloc);
  494. lines.splitString(txt.toCString(), '\n');
  495. if(lines.getSize() < 1)
  496. {
  497. ANKI_SHADER_COMPILER_LOGE("Source is empty");
  498. }
  499. // Parse lines
  500. for(const String& line : lines)
  501. {
  502. if(line.find("pragma") != CString::NPOS || line.find("include") != CString::NPOS)
  503. {
  504. // Possibly a preprocessor directive we care
  505. ANKI_CHECK(parseLine(line.toCString(), fname, foundPragmaOnce, depth));
  506. }
  507. else
  508. {
  509. // Just append the line
  510. m_codeLines.pushBack(line.toCString());
  511. }
  512. }
  513. if(foundPragmaOnce)
  514. {
  515. // Append the guard
  516. m_codeLines.pushBack("#endif // Include guard");
  517. }
  518. if(m_insideShader)
  519. {
  520. ANKI_SHADER_COMPILER_LOGE("Forgot a \"pragma anki end\"");
  521. return Error::USER_DATA;
  522. }
  523. return Error::NONE;
  524. }
  525. Error ShaderProgramParser::parse()
  526. {
  527. ANKI_ASSERT(!m_fname.isEmpty());
  528. ANKI_ASSERT(m_codeLines.isEmpty());
  529. const CString fname = m_fname;
  530. // Parse recursively
  531. ANKI_CHECK(parseFile(fname, 0));
  532. // Checks
  533. {
  534. if(!!(m_shaderTypes & ShaderTypeBit::COMPUTE))
  535. {
  536. if(m_shaderTypes != ShaderTypeBit::COMPUTE)
  537. {
  538. ANKI_SHADER_COMPILER_LOGE("Can't combine compute shader with other types of shaders");
  539. return Error::USER_DATA;
  540. }
  541. }
  542. else
  543. {
  544. if(!(m_shaderTypes & ShaderTypeBit::VERTEX))
  545. {
  546. ANKI_SHADER_COMPILER_LOGE("Missing vertex shader");
  547. return Error::USER_DATA;
  548. }
  549. if(!(m_shaderTypes & ShaderTypeBit::FRAGMENT))
  550. {
  551. ANKI_SHADER_COMPILER_LOGE("Missing fragment shader");
  552. return Error::USER_DATA;
  553. }
  554. }
  555. }
  556. // Create the code lines
  557. if(m_codeLines.getSize())
  558. {
  559. m_codeLines.join("\n", m_codeSource);
  560. m_codeLines.destroy();
  561. }
  562. return Error::NONE;
  563. }
  564. Error ShaderProgramParser::generateVariant(
  565. ConstWeakArray<MutatorValue> mutation, ShaderProgramParserVariant& variant) const
  566. {
  567. // Sanity checks
  568. ANKI_ASSERT(m_codeSource.getLength() > 0);
  569. ANKI_ASSERT(mutation.getSize() == m_mutators.getSize());
  570. for(U32 i = 0; i < mutation.getSize(); ++i)
  571. {
  572. ANKI_ASSERT(mutatorHasValue(m_mutators[i], mutation[i]) && "Value not found");
  573. }
  574. // Init variant
  575. ::new(&variant) ShaderProgramParserVariant();
  576. variant.m_alloc = m_alloc;
  577. // Create the mutator defines
  578. StringAuto mutatorDefines(m_alloc);
  579. for(U32 i = 0; i < mutation.getSize(); ++i)
  580. {
  581. mutatorDefines.append(StringAuto(m_alloc).sprintf("#define %s %d\n", m_mutators[i].m_name.cstr(), mutation[i]));
  582. }
  583. // Create the header
  584. StringAuto header(m_alloc);
  585. header.sprintf(SHADER_HEADER,
  586. m_gpuCapabilities.m_minorApiVersion,
  587. m_gpuCapabilities.m_majorApiVersion,
  588. GPU_VENDOR_STR[m_gpuCapabilities.m_gpuVendor].cstr(),
  589. m_bindlessLimits.m_bindlessTextureCount,
  590. m_bindlessLimits.m_bindlessImageCount);
  591. // Generate souce per stage
  592. for(ShaderType shaderType = ShaderType::FIRST; shaderType < ShaderType::COUNT; ++shaderType)
  593. {
  594. if(!((1u << ShaderTypeBit(shaderType)) & m_shaderTypes))
  595. {
  596. continue;
  597. }
  598. // Create the final source without the bindings
  599. StringAuto finalSource(m_alloc);
  600. finalSource.append(header);
  601. finalSource.append(mutatorDefines);
  602. finalSource.append(StringAuto(m_alloc).sprintf("#define ANKI_%s 1\n", SHADER_STAGE_NAMES[shaderType].cstr()));
  603. finalSource.append(m_codeSource);
  604. // Move the source
  605. variant.m_sources[shaderType] = std::move(finalSource);
  606. }
  607. return Error::NONE;
  608. }
  609. Bool ShaderProgramParser::rewriteMutation(WeakArray<MutatorValue> mutation) const
  610. {
  611. // Checks
  612. ANKI_ASSERT(mutation.getSize() == m_mutators.getSize());
  613. for(U32 i = 0; i < mutation.getSize(); ++i)
  614. {
  615. ANKI_ASSERT(mutatorHasValue(m_mutators[i], mutation[i]));
  616. }
  617. // Early exit
  618. if(mutation.getSize() == 0)
  619. {
  620. return false;
  621. }
  622. // Find if mutation exists
  623. for(const MutationRewrite& rewrite : m_mutationRewrites)
  624. {
  625. Bool found = true;
  626. for(U32 i = 0; i < rewrite.m_records.getSize(); ++i)
  627. {
  628. if(rewrite.m_records[i].m_valueFrom != mutation[rewrite.m_records[i].m_mutatorIndex])
  629. {
  630. found = false;
  631. break;
  632. }
  633. }
  634. if(found)
  635. {
  636. // Rewrite it
  637. for(U32 i = 0; i < rewrite.m_records.getSize(); ++i)
  638. {
  639. mutation[rewrite.m_records[i].m_mutatorIndex] = rewrite.m_records[i].m_valueTo;
  640. }
  641. return true;
  642. }
  643. }
  644. return false;
  645. }
  646. Bool ShaderProgramParser::mutatorHasValue(const ShaderProgramParserMutator& mutator, MutatorValue value)
  647. {
  648. for(MutatorValue v : mutator.m_values)
  649. {
  650. if(value == v)
  651. {
  652. return true;
  653. }
  654. }
  655. return false;
  656. }
  657. } // end namespace anki