ShaderProgramParser.cpp 20 KB

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