ShaderProgramParser.cpp 21 KB

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