ShaderProgramParser.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272
  1. // Copyright (C) 2009-2022, 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/ShaderProgramParser.h>
  6. namespace anki {
  7. #define ANKI_PP_ERROR_MALFORMED() \
  8. ANKI_SHADER_COMPILER_LOGE("%s: Malformed expression: %s", fname.cstr(), line.cstr()); \
  9. return Error::USER_DATA
  10. #define ANKI_PP_ERROR_MALFORMED_MSG(msg_) \
  11. ANKI_SHADER_COMPILER_LOGE("%s: " msg_ ": %s", fname.cstr(), line.cstr()); \
  12. return Error::USER_DATA
  13. static const Array<CString, U32(ShaderType::COUNT)> SHADER_STAGE_NAMES = {
  14. {"VERTEX", "TESSELLATION_CONTROL", "TESSELLATION_EVALUATION", "GEOMETRY", "FRAGMENT", "COMPUTE", "RAY_GEN",
  15. "ANY_HIT", "CLOSEST_HIT", "MISS", "INTERSECTION", "CALLABLE"}};
  16. static const char SHADER_HEADER[] = R"(#version 460 core
  17. #define ANKI_%s_SHADER 1
  18. #define ANKI_PLATFORM_MOBILE %d
  19. #define ANKI_FORCE_FULL_FP_PRECISION %d
  20. #define _ANKI_SUPPORTS_64BIT !ANKI_PLATFORM_MOBILE
  21. #define gl_VertexID gl_VertexIndex
  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. #extension GL_EXT_buffer_reference : enable
  35. #extension GL_EXT_buffer_reference2 : enable
  36. #extension GL_EXT_shader_explicit_arithmetic_types : enable
  37. #extension GL_EXT_shader_explicit_arithmetic_types_int8 : enable
  38. #extension GL_EXT_shader_explicit_arithmetic_types_int16 : enable
  39. #extension GL_EXT_shader_explicit_arithmetic_types_int32 : enable
  40. #extension GL_EXT_shader_explicit_arithmetic_types_float16 : enable
  41. #extension GL_EXT_shader_explicit_arithmetic_types_float32 : enable
  42. #if _ANKI_SUPPORTS_64BIT
  43. #extension GL_EXT_shader_explicit_arithmetic_types_int64 : enable
  44. #extension GL_EXT_shader_explicit_arithmetic_types_float64 : enable
  45. #extension GL_EXT_shader_atomic_int64 : enable
  46. #extension GL_EXT_shader_subgroup_extended_types_int64 : enable
  47. #endif
  48. #extension GL_EXT_nonuniform_qualifier : enable
  49. #extension GL_EXT_scalar_block_layout : enable
  50. #define ANKI_MAX_BINDLESS_TEXTURES %uu
  51. #define ANKI_MAX_BINDLESS_IMAGES %uu
  52. #if defined(ANKI_RAY_GEN_SHADER) || defined(ANKI_ANY_HIT_SHADER) || defined(ANKI_CLOSEST_HIT_SHADER) || defined(ANKI_MISS_SHADER) || defined(ANKI_INTERSECTION_SHADER) || defined(ANKI_CALLABLE_SHADER)
  53. # extension GL_EXT_ray_tracing : enable
  54. #endif
  55. #define ANKI_BINDLESS_SET(set_) \
  56. layout(set = set_, binding = 0) uniform utexture2D u_bindlessTextures2dU32[ANKI_MAX_BINDLESS_TEXTURES]; \
  57. layout(set = set_, binding = 0) uniform itexture2D u_bindlessTextures2dI32[ANKI_MAX_BINDLESS_TEXTURES]; \
  58. layout(set = set_, binding = 0) uniform texture2D u_bindlessTextures2dF32[ANKI_MAX_BINDLESS_TEXTURES]; \
  59. layout(set = set_, binding = 1) uniform readonly uimage2D u_bindlessImages2dU32[ANKI_MAX_BINDLESS_IMAGES]; \
  60. layout(set = set_, binding = 1) uniform readonly iimage2D u_bindlessImages2dI32[ANKI_MAX_BINDLESS_IMAGES]; \
  61. layout(set = set_, binding = 1) uniform readonly image2D u_bindlessImages2dF32[ANKI_MAX_BINDLESS_IMAGES]
  62. #define F32 float
  63. #define _ANKI_SIZEOF_float 4u
  64. #define Vec2 vec2
  65. #define _ANKI_SIZEOF_vec2 8u
  66. #define Vec3 vec3
  67. #define _ANKI_SIZEOF_vec3 12u
  68. #define Vec4 vec4
  69. #define _ANKI_SIZEOF_vec4 16u
  70. #define F16 float16_t
  71. #define _ANKI_SIZEOF_float16_t 2u
  72. #define HVec2 f16vec2
  73. #define _ANKI_SIZEOF_f16vec2 4u
  74. #define HVec3 f16vec3
  75. #define _ANKI_SIZEOF_f16vec3 6u
  76. #define HVec4 f16vec4
  77. #define _ANKI_SIZEOF_f16vec4 8u
  78. #define U8 uint8_t
  79. #define _ANKI_SIZEOF_uint8_t 1u
  80. #define U8Vec2 u8vec2
  81. #define _ANKI_SIZEOF_u8vec2 2u
  82. #define U8Vec3 u8vec3
  83. #define _ANKI_SIZEOF_u8vec3 3u
  84. #define U8Vec4 u8vec4
  85. #define _ANKI_SIZEOF_u8vec4 4u
  86. #define I8 int8_t
  87. #define _ANKI_SIZEOF_int8_t 1u
  88. #define I8Vec2 i8vec2
  89. #define _ANKI_SIZEOF_i8vec2 2u
  90. #define I8Vec3 i8vec3
  91. #define _ANKI_SIZEOF_i8vec3 3u
  92. #define I8Vec4 i8vec4
  93. #define _ANKI_SIZEOF_i8vec4 4u
  94. #define U16 uint16_t
  95. #define _ANKI_SIZEOF_uint16_t 2u
  96. #define U16Vec2 u16vec2
  97. #define _ANKI_SIZEOF_u16vec2 4u
  98. #define U16Vec3 u16vec3
  99. #define _ANKI_SIZEOF_u16vec3 6u
  100. #define U16Vec4 u16vec4
  101. #define _ANKI_SIZEOF_u16vec4 8u
  102. #define I16 int16_t
  103. #define _ANKI_SIZEOF_int16_t 2u
  104. #define I16Vec2 i16vec2
  105. #define _ANKI_SIZEOF_i16vec2 4u
  106. #define I16Vec3 i16vec3
  107. #define _ANKI_SIZEOF_i16vec3 6u
  108. #define i16Vec4 i16vec4
  109. #define _ANKI_SIZEOF_i16vec4 8u
  110. #define U32 uint
  111. #define _ANKI_SIZEOF_uint 4u
  112. #define UVec2 uvec2
  113. #define _ANKI_SIZEOF_uvec2 8u
  114. #define UVec3 uvec3
  115. #define _ANKI_SIZEOF_uvec3 12u
  116. #define UVec4 uvec4
  117. #define _ANKI_SIZEOF_uvec4 16u
  118. #define I32 int
  119. #define _ANKI_SIZEOF_int 4u
  120. #define IVec2 ivec2
  121. #define _ANKI_SIZEOF_ivec2 8u
  122. #define IVec3 ivec3
  123. #define _ANKI_SIZEOF_ivec3 12u
  124. #define IVec4 ivec4
  125. #define _ANKI_SIZEOF_ivec4 16u
  126. #if _ANKI_SUPPORTS_64BIT
  127. # define U64 uint64_t
  128. # define _ANKI_SIZEOF_uint64_t 8u
  129. # define U64Vec2 u64vec2
  130. # define _ANKI_SIZEOF_u64vec2 16u
  131. # define U64Vec3 u64vec3
  132. # define _ANKI_SIZEOF_u64vec3 24u
  133. # define U64Vec4 u64vec4
  134. # define _ANKI_SIZEOF_u64vec4 32u
  135. # define I64 int64_t
  136. # define _ANKI_SIZEOF_int64_t 8u
  137. # define I64Vec2 i64vec2
  138. # define _ANKI_SIZEOF_i64vec2 16u
  139. # define I64Vec3 i64vec3
  140. # define _ANKI_SIZEOF_i64vec3 24u
  141. # define I64Vec4 i64vec4
  142. # define _ANKI_SIZEOF_i64vec4 32u
  143. #endif
  144. #define Mat3 mat3
  145. #define _ANKI_SIZEOF_mat3 36u
  146. #define Mat4 mat4
  147. #define _ANKI_SIZEOF_mat4 64u
  148. #define Mat3x4 mat4x3 // GLSL has the column number first and then the rows
  149. #define _ANKI_SIZEOF_mat3x4 48u
  150. #define Bool bool
  151. #if _ANKI_SUPPORTS_64BIT
  152. # define Address U64
  153. #else
  154. # define Address UVec2
  155. #endif
  156. #define _ANKI_SIZEOF_Address 8u
  157. #define _ANKI_CONCATENATE(a, b) a##b
  158. #define ANKI_CONCATENATE(a, b) _ANKI_CONCATENATE(a, b)
  159. #define ANKI_SIZEOF(type) _ANKI_CONCATENATE(_ANKI_SIZEOF_, type)
  160. #define ANKI_ALIGNOF(type) _ANKI_CONCATENATE(_ANKI_ALIGNOF_, type)
  161. #define _ANKI_SCONST_X(type, n, id) \
  162. layout(constant_id = id) const type n = type(1); \
  163. const U32 ANKI_CONCATENATE(n, _CONST_ID) = id
  164. #define _ANKI_SCONST_X2(type, componentType, n, id, constWorkaround) \
  165. layout(constant_id = id + 0u) const componentType ANKI_CONCATENATE(_anki_const_0_2_, n) = componentType(1); \
  166. layout(constant_id = id + 1u) const componentType ANKI_CONCATENATE(_anki_const_1_2_, n) = componentType(1); \
  167. constWorkaround type n = type(ANKI_CONCATENATE(_anki_const_0_2_, n), ANKI_CONCATENATE(_anki_const_1_2_, n))
  168. #define _ANKI_SCONST_X3(type, componentType, n, id, constWorkaround) \
  169. layout(constant_id = id + 0u) const componentType ANKI_CONCATENATE(_anki_const_0_3_, n) = componentType(1); \
  170. layout(constant_id = id + 1u) const componentType ANKI_CONCATENATE(_anki_const_1_3_, n) = componentType(1); \
  171. layout(constant_id = id + 2u) const componentType ANKI_CONCATENATE(_anki_const_2_3_, n) = componentType(1); \
  172. constWorkaround type n = type(ANKI_CONCATENATE(_anki_const_0_3_, n), ANKI_CONCATENATE(_anki_const_1_3_, n), \
  173. ANKI_CONCATENATE(_anki_const_2_3_, n))
  174. #define _ANKI_SCONST_X4(type, componentType, n, id, constWorkaround) \
  175. layout(constant_id = id + 0u) const componentType ANKI_CONCATENATE(_anki_const_0_4_, n) = componentType(1); \
  176. layout(constant_id = id + 1u) const componentType ANKI_CONCATENATE(_anki_const_1_4_, n) = componentType(1); \
  177. layout(constant_id = id + 2u) const componentType ANKI_CONCATENATE(_anki_const_2_4_, n) = componentType(1); \
  178. layout(constant_id = id + 3u) const componentType ANKI_CONCATENATE(_anki_const_3_4_, n) = componentType(1); \
  179. constWorkaround type n = type(ANKI_CONCATENATE(_anki_const_0_4_, n), ANKI_CONCATENATE(_anki_const_1_4_, n), \
  180. ANKI_CONCATENATE(_anki_const_2_4_, n), ANKI_CONCATENATE(_anki_const_2_4_, n))
  181. #define ANKI_SPECIALIZATION_CONSTANT_I32(n, id) _ANKI_SCONST_X(I32, n, id)
  182. #define ANKI_SPECIALIZATION_CONSTANT_IVEC2(n, id) _ANKI_SCONST_X2(IVec2, I32, n, id, const)
  183. #define ANKI_SPECIALIZATION_CONSTANT_IVEC3(n, id) _ANKI_SCONST_X3(IVec3, I32, n, id, const)
  184. #define ANKI_SPECIALIZATION_CONSTANT_IVEC4(n, id) _ANKI_SCONST_X4(IVec4, I32, n, id, const)
  185. #define ANKI_SPECIALIZATION_CONSTANT_U32(n, id) _ANKI_SCONST_X(U32, n, id)
  186. #define ANKI_SPECIALIZATION_CONSTANT_UVEC2(n, id) _ANKI_SCONST_X2(UVec2, U32, n, id, const)
  187. #define ANKI_SPECIALIZATION_CONSTANT_UVEC3(n, id) _ANKI_SCONST_X3(UVec3, U32, n, id, const)
  188. #define ANKI_SPECIALIZATION_CONSTANT_UVEC4(n, id) _ANKI_SCONST_X4(UVec4, U32, n, id, const)
  189. #define ANKI_SPECIALIZATION_CONSTANT_F32(n, id) _ANKI_SCONST_X(F32, n, id)
  190. #define ANKI_SPECIALIZATION_CONSTANT_VEC2(n, id) _ANKI_SCONST_X2(Vec2, F32, n, id,)
  191. #define ANKI_SPECIALIZATION_CONSTANT_VEC3(n, id) _ANKI_SCONST_X3(Vec3, F32, n, id,)
  192. #define ANKI_SPECIALIZATION_CONSTANT_VEC4(n, id) _ANKI_SCONST_X4(Vec4, F32, n, id,)
  193. #define ANKI_DEFINE_LOAD_STORE(type, alignment) \
  194. layout(buffer_reference, scalar, buffer_reference_align = (alignment)) buffer _Ref##type \
  195. { \
  196. type m_value; \
  197. }; \
  198. void load(U64 address, out type o) \
  199. { \
  200. o = _Ref##type(address).m_value; \
  201. } \
  202. void store(U64 address, type i) \
  203. { \
  204. _Ref##type(address).m_value = i; \
  205. }
  206. #define ANKI_PADDING(bytes) U8 _padding_ ## __LINE__[bytes]
  207. layout(std140, row_major) uniform;
  208. layout(std140, row_major) buffer;
  209. #if ANKI_FORCE_FULL_FP_PRECISION
  210. # define ANKI_RP
  211. #else
  212. # define ANKI_RP mediump
  213. #endif
  214. #define ANKI_FP highp
  215. precision highp int;
  216. precision highp float;
  217. Vec2 pow(Vec2 a, F32 b)
  218. {
  219. return pow(a, Vec2(b));
  220. }
  221. Vec3 pow(Vec3 a, F32 b)
  222. {
  223. return pow(a, Vec3(b));
  224. }
  225. Vec4 pow(Vec4 a, F32 b)
  226. {
  227. return pow(a, Vec4(b));
  228. }
  229. )";
  230. static const U64 SHADER_HEADER_HASH = computeHash(SHADER_HEADER, sizeof(SHADER_HEADER));
  231. ShaderProgramParser::ShaderProgramParser(CString fname, ShaderProgramFilesystemInterface* fsystem,
  232. GenericMemoryPoolAllocator<U8> alloc,
  233. const ShaderCompilerOptions& compilerOptions)
  234. : m_alloc(alloc)
  235. , m_fname(alloc, fname)
  236. , m_fsystem(fsystem)
  237. , m_compilerOptions(compilerOptions)
  238. {
  239. }
  240. ShaderProgramParser::~ShaderProgramParser()
  241. {
  242. }
  243. void ShaderProgramParser::tokenizeLine(CString line, DynamicArrayAuto<StringAuto>& tokens) const
  244. {
  245. ANKI_ASSERT(line.getLength() > 0);
  246. StringAuto l(m_alloc, line);
  247. // Replace all tabs with spaces
  248. for(char& c : l)
  249. {
  250. if(c == '\t')
  251. {
  252. c = ' ';
  253. }
  254. }
  255. // Split
  256. StringListAuto spaceTokens(m_alloc);
  257. spaceTokens.splitString(l, ' ', false);
  258. // Create the array
  259. for(const String& s : spaceTokens)
  260. {
  261. tokens.emplaceBack(m_alloc, s);
  262. }
  263. }
  264. Error ShaderProgramParser::parsePragmaStart(const StringAuto* begin, const StringAuto* end, CString line, CString fname)
  265. {
  266. ANKI_ASSERT(begin && end);
  267. if(begin >= end)
  268. {
  269. ANKI_PP_ERROR_MALFORMED();
  270. }
  271. ShaderType shaderType = ShaderType::COUNT;
  272. if(*begin == "vert")
  273. {
  274. shaderType = ShaderType::VERTEX;
  275. }
  276. else if(*begin == "tessc")
  277. {
  278. shaderType = ShaderType::TESSELLATION_CONTROL;
  279. }
  280. else if(*begin == "tesse")
  281. {
  282. }
  283. else if(*begin == "geom")
  284. {
  285. shaderType = ShaderType::GEOMETRY;
  286. }
  287. else if(*begin == "frag")
  288. {
  289. shaderType = ShaderType::FRAGMENT;
  290. }
  291. else if(*begin == "comp")
  292. {
  293. shaderType = ShaderType::COMPUTE;
  294. }
  295. else if(*begin == "rgen")
  296. {
  297. shaderType = ShaderType::RAY_GEN;
  298. }
  299. else if(*begin == "ahit")
  300. {
  301. shaderType = ShaderType::ANY_HIT;
  302. }
  303. else if(*begin == "chit")
  304. {
  305. shaderType = ShaderType::CLOSEST_HIT;
  306. }
  307. else if(*begin == "miss")
  308. {
  309. shaderType = ShaderType::MISS;
  310. }
  311. else if(*begin == "int")
  312. {
  313. shaderType = ShaderType::INTERSECTION;
  314. }
  315. else if(*begin == "call")
  316. {
  317. shaderType = ShaderType::CALLABLE;
  318. }
  319. else
  320. {
  321. ANKI_PP_ERROR_MALFORMED();
  322. }
  323. m_codeLines.pushBackSprintf("#ifdef ANKI_%s_SHADER", SHADER_STAGE_NAMES[shaderType].cstr());
  324. ++begin;
  325. if(begin != end)
  326. {
  327. // Should be the last token
  328. ANKI_PP_ERROR_MALFORMED();
  329. }
  330. // Set the mask
  331. const ShaderTypeBit mask = ShaderTypeBit(1 << shaderType);
  332. if(!!(mask & m_shaderTypes))
  333. {
  334. ANKI_PP_ERROR_MALFORMED_MSG("Can't have #pragma start <shader> appearing more than once");
  335. }
  336. m_shaderTypes |= mask;
  337. // Check bounds
  338. if(m_insideShader)
  339. {
  340. ANKI_PP_ERROR_MALFORMED_MSG("Can't have #pragma start before you close the previous pragma start");
  341. }
  342. m_insideShader = true;
  343. return Error::NONE;
  344. }
  345. Error ShaderProgramParser::parsePragmaEnd(const StringAuto* begin, const StringAuto* end, CString line, CString fname)
  346. {
  347. ANKI_ASSERT(begin && end);
  348. // Check tokens
  349. if(begin != end)
  350. {
  351. ANKI_PP_ERROR_MALFORMED();
  352. }
  353. // Check bounds
  354. if(!m_insideShader)
  355. {
  356. ANKI_PP_ERROR_MALFORMED_MSG("Can't have #pragma end before you open with a pragma start");
  357. }
  358. m_insideShader = false;
  359. // Write code
  360. m_codeLines.pushBack("#endif // Shader guard");
  361. return Error::NONE;
  362. }
  363. Error ShaderProgramParser::parsePragmaMutator(const StringAuto* begin, const StringAuto* end, CString line,
  364. CString fname)
  365. {
  366. ANKI_ASSERT(begin && end);
  367. if(begin >= end)
  368. {
  369. ANKI_PP_ERROR_MALFORMED();
  370. }
  371. m_mutators.emplaceBack(m_alloc);
  372. Mutator& mutator = m_mutators.getBack();
  373. // Name
  374. {
  375. if(begin >= end)
  376. {
  377. // Need to have a name
  378. ANKI_PP_ERROR_MALFORMED();
  379. }
  380. // Check for duplicate mutators
  381. for(U32 i = 0; i < m_mutators.getSize() - 1; ++i)
  382. {
  383. if(m_mutators[i].m_name == *begin)
  384. {
  385. ANKI_PP_ERROR_MALFORMED_MSG("Duplicate mutator");
  386. }
  387. }
  388. if(begin->getLength() > MAX_SHADER_BINARY_NAME_LENGTH)
  389. {
  390. ANKI_PP_ERROR_MALFORMED_MSG("Too big name");
  391. }
  392. mutator.m_name.create(begin->toCString());
  393. ++begin;
  394. }
  395. // Values
  396. {
  397. // Gather them
  398. for(; begin < end; ++begin)
  399. {
  400. MutatorValue value = 0;
  401. if(tokenIsComment(begin->toCString()))
  402. {
  403. break;
  404. }
  405. if(begin->toNumber(value))
  406. {
  407. ANKI_PP_ERROR_MALFORMED();
  408. }
  409. mutator.m_values.emplaceBack(value);
  410. }
  411. std::sort(mutator.m_values.getBegin(), mutator.m_values.getEnd());
  412. // Check for duplicates
  413. for(U32 i = 1; i < mutator.m_values.getSize(); ++i)
  414. {
  415. if(mutator.m_values[i - 1] == mutator.m_values[i])
  416. {
  417. ANKI_PP_ERROR_MALFORMED_MSG("Same value appeared more than once");
  418. }
  419. }
  420. }
  421. return Error::NONE;
  422. }
  423. Error ShaderProgramParser::parsePragmaLibraryName(const StringAuto* begin, const StringAuto* end, CString line,
  424. CString fname)
  425. {
  426. ANKI_ASSERT(begin && end);
  427. if(begin >= end)
  428. {
  429. ANKI_PP_ERROR_MALFORMED();
  430. }
  431. if(m_libName.getLength() > 0)
  432. {
  433. ANKI_PP_ERROR_MALFORMED_MSG("Library name already set");
  434. }
  435. m_libName = *begin;
  436. return Error::NONE;
  437. }
  438. Error ShaderProgramParser::parsePragmaRayType(const StringAuto* begin, const StringAuto* end, CString line,
  439. CString fname)
  440. {
  441. ANKI_ASSERT(begin && end);
  442. if(begin >= end)
  443. {
  444. ANKI_PP_ERROR_MALFORMED();
  445. }
  446. if(m_rayType != MAX_U32)
  447. {
  448. ANKI_PP_ERROR_MALFORMED_MSG("Ray type already set");
  449. }
  450. ANKI_CHECK(begin->toNumber(m_rayType));
  451. if(m_rayType > 128)
  452. {
  453. ANKI_PP_ERROR_MALFORMED_MSG("Ray type has a very large value");
  454. }
  455. return Error::NONE;
  456. }
  457. Error ShaderProgramParser::parsePragmaReflect(const StringAuto* begin, const StringAuto* end, CString line,
  458. CString fname)
  459. {
  460. ANKI_ASSERT(begin && end);
  461. if(begin >= end)
  462. {
  463. ANKI_PP_ERROR_MALFORMED();
  464. }
  465. m_symbolsToReflect.pushBack(*begin);
  466. return Error::NONE;
  467. }
  468. Error ShaderProgramParser::parsePragmaRewriteMutation(const StringAuto* begin, const StringAuto* end, CString line,
  469. CString fname)
  470. {
  471. ANKI_ASSERT(begin && end);
  472. // Some basic sanity checks
  473. const U tokenCount = U(end - begin);
  474. constexpr U minTokenCount = 2 + 1 + 2; // Mutator + value + "to" + mutator + value
  475. if(tokenCount < minTokenCount)
  476. {
  477. ANKI_PP_ERROR_MALFORMED();
  478. }
  479. MutationRewrite& rewrite = *m_mutationRewrites.emplaceBack(m_alloc);
  480. Bool servingFrom = true;
  481. do
  482. {
  483. if(*begin == "to")
  484. {
  485. if(servingFrom == false)
  486. {
  487. ANKI_PP_ERROR_MALFORMED();
  488. }
  489. servingFrom = false;
  490. }
  491. else
  492. {
  493. // Mutator & value
  494. // Get mutator and value
  495. const CString mutatorName = *begin;
  496. ++begin;
  497. if(begin == end)
  498. {
  499. ANKI_PP_ERROR_MALFORMED();
  500. }
  501. const CString valueStr = *begin;
  502. MutatorValue value;
  503. if(valueStr.toNumber(value))
  504. {
  505. ANKI_PP_ERROR_MALFORMED_MSG("Malformed value");
  506. }
  507. // Get or create new record
  508. if(servingFrom)
  509. {
  510. MutationRewrite::Record& rec = *rewrite.m_records.emplaceBack();
  511. for(U32 i = 0; i < m_mutators.getSize(); ++i)
  512. {
  513. if(m_mutators[i].getName() == mutatorName)
  514. {
  515. rec.m_mutatorIndex = i;
  516. break;
  517. }
  518. }
  519. if(rec.m_mutatorIndex == MAX_U32)
  520. {
  521. ANKI_PP_ERROR_MALFORMED_MSG("Mutator not found");
  522. }
  523. if(!mutatorHasValue(m_mutators[rec.m_mutatorIndex], value))
  524. {
  525. ANKI_PP_ERROR_MALFORMED_MSG("Incorect value for mutator");
  526. }
  527. rec.m_valueFrom = value;
  528. }
  529. else
  530. {
  531. Bool found = false;
  532. for(MutationRewrite::Record& rec : rewrite.m_records)
  533. {
  534. if(m_mutators[rec.m_mutatorIndex].m_name == mutatorName)
  535. {
  536. if(!mutatorHasValue(m_mutators[rec.m_mutatorIndex], value))
  537. {
  538. ANKI_PP_ERROR_MALFORMED_MSG("Incorect value for mutator");
  539. }
  540. rec.m_valueTo = value;
  541. found = true;
  542. break;
  543. }
  544. }
  545. if(!found)
  546. {
  547. ANKI_PP_ERROR_MALFORMED();
  548. }
  549. }
  550. }
  551. ++begin;
  552. } while(begin < end && !tokenIsComment(*begin));
  553. // Sort for some later cross checking
  554. std::sort(rewrite.m_records.getBegin(), rewrite.m_records.getEnd(),
  555. [](const MutationRewrite::Record& a, const MutationRewrite::Record& b) {
  556. return a.m_mutatorIndex < b.m_mutatorIndex;
  557. });
  558. // More cross checking
  559. for(U32 i = 1; i < rewrite.m_records.getSize(); ++i)
  560. {
  561. if(rewrite.m_records[i - 1].m_mutatorIndex == rewrite.m_records[i].m_mutatorIndex)
  562. {
  563. ANKI_PP_ERROR_MALFORMED_MSG("Mutator appeared more than once");
  564. }
  565. }
  566. for(U32 i = 0; i < m_mutationRewrites.getSize() - 1; ++i)
  567. {
  568. const MutationRewrite& other = m_mutationRewrites[i];
  569. if(other.m_records.getSize() != rewrite.m_records.getSize())
  570. {
  571. continue;
  572. }
  573. Bool same = true;
  574. for(U32 j = 0; j < rewrite.m_records.getSize(); ++j)
  575. {
  576. if(rewrite.m_records[j] != other.m_records[j])
  577. {
  578. same = false;
  579. break;
  580. }
  581. }
  582. if(same)
  583. {
  584. ANKI_PP_ERROR_MALFORMED_MSG("Mutation already exists");
  585. }
  586. }
  587. return Error::NONE;
  588. }
  589. Error ShaderProgramParser::parseInclude(const StringAuto* begin, const StringAuto* end, CString line, CString fname,
  590. U32 depth)
  591. {
  592. // Gather the path
  593. StringAuto path(m_alloc);
  594. for(; begin < end; ++begin)
  595. {
  596. path.append(*begin);
  597. }
  598. if(path.isEmpty())
  599. {
  600. ANKI_PP_ERROR_MALFORMED();
  601. }
  602. // Check
  603. const char firstChar = path[0];
  604. const char lastChar = path[path.getLength() - 1];
  605. if((firstChar == '\"' && lastChar == '\"') || (firstChar == '<' && lastChar == '>'))
  606. {
  607. StringAuto fname2(m_alloc);
  608. fname2.create(path.begin() + 1, path.begin() + path.getLength() - 1);
  609. const Bool dontIgnore =
  610. fname2.find("AnKi/Shaders/") != String::NPOS || fname2.find("ThirdParty/") != String::NPOS;
  611. if(!dontIgnore)
  612. {
  613. // The shaders can't include C++ files. Ignore the include
  614. return Error::NONE;
  615. }
  616. if(parseFile(fname2, depth + 1))
  617. {
  618. ANKI_PP_ERROR_MALFORMED_MSG("Error parsing include. See previous errors");
  619. }
  620. }
  621. else
  622. {
  623. ANKI_PP_ERROR_MALFORMED();
  624. }
  625. return Error::NONE;
  626. }
  627. Error ShaderProgramParser::parseLine(CString line, CString fname, Bool& foundPragmaOnce, U32 depth)
  628. {
  629. // Tokenize
  630. DynamicArrayAuto<StringAuto> tokens(m_alloc);
  631. tokenizeLine(line, tokens);
  632. ANKI_ASSERT(tokens.getSize() > 0);
  633. const StringAuto* token = tokens.getBegin();
  634. const StringAuto* end = tokens.getEnd();
  635. // Skip the hash
  636. Bool foundAloneHash = false;
  637. if(*token == "#")
  638. {
  639. ++token;
  640. foundAloneHash = true;
  641. }
  642. if((token < end) && ((foundAloneHash && *token == "include") || *token == "#include"))
  643. {
  644. // We _must_ have an #include
  645. ANKI_CHECK(parseInclude(token + 1, end, line, fname, depth));
  646. }
  647. else if((token < end) && ((foundAloneHash && *token == "pragma") || *token == "#pragma"))
  648. {
  649. // We may have a #pragma once or a #pragma anki or something else
  650. ++token;
  651. if(*token == "once")
  652. {
  653. // Pragma once
  654. if(foundPragmaOnce)
  655. {
  656. ANKI_PP_ERROR_MALFORMED_MSG("Can't have more than one #pragma once per file");
  657. }
  658. if(token + 1 != end)
  659. {
  660. ANKI_PP_ERROR_MALFORMED();
  661. }
  662. // Add the guard unique for this file
  663. foundPragmaOnce = true;
  664. const U64 hash = fname.computeHash();
  665. m_codeLines.pushBackSprintf("#ifndef _ANKI_INCL_GUARD_%llu\n"
  666. "#define _ANKI_INCL_GUARD_%llu",
  667. hash, hash);
  668. }
  669. else if(*token == "anki")
  670. {
  671. // Must be a #pragma anki
  672. ++token;
  673. if(*token == "mutator")
  674. {
  675. ANKI_CHECK(checkNoActiveStruct());
  676. ANKI_CHECK(parsePragmaMutator(token + 1, end, line, fname));
  677. }
  678. else if(*token == "start")
  679. {
  680. ANKI_CHECK(checkNoActiveStruct());
  681. ANKI_CHECK(parsePragmaStart(token + 1, end, line, fname));
  682. }
  683. else if(*token == "end")
  684. {
  685. ANKI_CHECK(checkNoActiveStruct());
  686. ANKI_CHECK(parsePragmaEnd(token + 1, end, line, fname));
  687. }
  688. else if(*token == "rewrite_mutation")
  689. {
  690. ANKI_CHECK(checkNoActiveStruct());
  691. ANKI_CHECK(parsePragmaRewriteMutation(token + 1, end, line, fname));
  692. }
  693. else if(*token == "library")
  694. {
  695. ANKI_CHECK(checkNoActiveStruct());
  696. ANKI_CHECK(parsePragmaLibraryName(token + 1, end, line, fname));
  697. }
  698. else if(*token == "ray_type")
  699. {
  700. ANKI_CHECK(checkNoActiveStruct());
  701. ANKI_CHECK(parsePragmaRayType(token + 1, end, line, fname));
  702. }
  703. else if(*token == "reflect")
  704. {
  705. ANKI_CHECK(checkNoActiveStruct());
  706. ANKI_CHECK(parsePragmaReflect(token + 1, end, line, fname));
  707. }
  708. else if(*token == "struct")
  709. {
  710. if(*(token + 1) == "end")
  711. {
  712. ANKI_CHECK(checkActiveStruct());
  713. ANKI_CHECK(parsePragmaStructEnd(token + 1, end, line, fname));
  714. }
  715. else
  716. {
  717. ANKI_CHECK(checkNoActiveStruct());
  718. ANKI_CHECK(parsePragmaStructBegin(token + 1, end, line, fname));
  719. }
  720. }
  721. else if(*token == "member")
  722. {
  723. ANKI_CHECK(checkActiveStruct());
  724. ANKI_CHECK(parsePragmaMember(token + 1, end, line, fname));
  725. }
  726. else
  727. {
  728. ANKI_PP_ERROR_MALFORMED();
  729. }
  730. // Add the line as a comment because of hashing of the source
  731. m_codeLines.pushBackSprintf("//%s", line.cstr());
  732. }
  733. else
  734. {
  735. // Some other pragma
  736. ANKI_SHADER_COMPILER_LOGW("Ignoring: %s", line.cstr());
  737. m_codeLines.pushBack(line);
  738. }
  739. }
  740. else
  741. {
  742. // Ignore
  743. m_codeLines.pushBack(line);
  744. }
  745. return Error::NONE;
  746. }
  747. Error ShaderProgramParser::parsePragmaStructBegin(const StringAuto* begin, const StringAuto* end, CString line,
  748. CString fname)
  749. {
  750. const U tokenCount = U(end - begin);
  751. if(tokenCount != 1)
  752. {
  753. ANKI_PP_ERROR_MALFORMED();
  754. }
  755. m_activeStruct.create(*begin);
  756. m_codeLines.pushBackSprintf("struct %s {", begin->cstr());
  757. return Error::NONE;
  758. }
  759. Error ShaderProgramParser::parsePragmaMember(const StringAuto* begin, const StringAuto* end, CString line,
  760. CString fname)
  761. {
  762. const U tokenCount = U(end - begin);
  763. if(tokenCount != 2 && tokenCount != 3)
  764. {
  765. ANKI_PP_ERROR_MALFORMED();
  766. }
  767. Bool relaxed = false;
  768. if(*begin == "ANKI_RP")
  769. {
  770. if(tokenCount != 3)
  771. {
  772. ANKI_PP_ERROR_MALFORMED();
  773. }
  774. relaxed = true;
  775. ++begin;
  776. }
  777. const CString typeStr = *begin;
  778. ShaderVariableDataType type = ShaderVariableDataType::NONE;
  779. if(typeStr == "F32")
  780. {
  781. type = ShaderVariableDataType::F32;
  782. }
  783. else if(typeStr == "Vec2")
  784. {
  785. type = ShaderVariableDataType::VEC2;
  786. }
  787. else if(typeStr == "Vec3")
  788. {
  789. type = ShaderVariableDataType::VEC3;
  790. }
  791. else if(typeStr == "Vec4")
  792. {
  793. type = ShaderVariableDataType::VEC4;
  794. }
  795. if(type == ShaderVariableDataType::NONE)
  796. {
  797. ANKI_PP_ERROR_MALFORMED_MSG("Unrecognized type");
  798. }
  799. const CString memberName = begin[1];
  800. // Code
  801. m_codeLines.pushBackSprintf("#define %s_%s_DEFINED 1", m_activeStruct.cstr(), memberName.cstr());
  802. m_codeLines.pushBackSprintf("\t%s %s %s;", (relaxed) ? "ANKI_RP" : "", typeStr.cstr(), memberName.cstr());
  803. // Add it
  804. Member& m = *m_members.emplaceBack(m_alloc);
  805. m.m_name.create(memberName);
  806. m.m_type = type;
  807. return Error::NONE;
  808. }
  809. Error ShaderProgramParser::parsePragmaStructEnd(const StringAuto* begin, const StringAuto* end, CString line,
  810. CString fname)
  811. {
  812. ANKI_ASSERT(!m_activeStruct.isEmpty());
  813. const U tokenCount = U(end - begin);
  814. if(tokenCount != 1)
  815. {
  816. ANKI_PP_ERROR_MALFORMED();
  817. }
  818. if(m_members.isEmpty())
  819. {
  820. ANKI_PP_ERROR_MALFORMED_MSG("The struct doesn't have any members");
  821. }
  822. m_codeLines.pushBack("};");
  823. for(U32 i = 0; i < m_members.getSize(); ++i)
  824. {
  825. const Member& m = m_members[i];
  826. // #define XXX_OFFSETOF
  827. if(i == 0)
  828. {
  829. m_codeLines.pushBackSprintf("#define %s_%s_OFFSETOF 0u", m_activeStruct.cstr(), m.m_name.cstr());
  830. }
  831. else
  832. {
  833. const Member& prev = m_members[i - 1];
  834. m_codeLines.pushBackSprintf("#define %s_%s_OFFSETOF (%s_%s_OFFSETOF + %s_%s_SIZEOF)", m_activeStruct.cstr(),
  835. m.m_name.cstr(), m_activeStruct.cstr(), prev.m_name.cstr(),
  836. m_activeStruct.cstr(), prev.m_name.cstr());
  837. }
  838. // #if XXX_DEFINED
  839. m_codeLines.pushBackSprintf("#if defined(%s_%s_DEFINED)", m_activeStruct.cstr(), m.m_name.cstr());
  840. // # define XXX_SIZEOF
  841. m_codeLines.pushBackSprintf("#\tdefine %s_%s_SIZEOF %uu", m_activeStruct.cstr(), m.m_name.cstr(),
  842. getShaderVariableDataTypeInfo(m.m_type).m_size / 4);
  843. // # define XXX_LOAD()
  844. const Bool isIntegral = getShaderVariableDataTypeInfo(m.m_type).m_isIntegral;
  845. const U32 componentCount = getShaderVariableDataTypeInfo(m.m_type).m_size / sizeof(U32);
  846. StringAuto values(m_alloc);
  847. for(U32 j = 0; j < componentCount; ++j)
  848. {
  849. StringAuto tmp(m_alloc);
  850. tmp.sprintf("%s(ssbo[%s_%s_OFFSETOF + offset + %uu])%s", (isIntegral) ? "" : "uintBitsToFloat",
  851. m_activeStruct.cstr(), m.m_name.cstr(), j, (j != componentCount - 1) ? "," : "");
  852. values.append(tmp);
  853. }
  854. m_codeLines.pushBackSprintf("#\tdefine %s_%s_LOAD(ssbo, offset) %s(%s)%s", m_activeStruct.cstr(),
  855. m.m_name.cstr(), getShaderVariableDataTypeInfo(m.m_type).m_name, values.cstr(),
  856. (i != m_members.getSize() - 1) ? "," : "");
  857. // #else
  858. m_codeLines.pushBack("#else");
  859. // # define XXX_SIZEOF 0
  860. m_codeLines.pushBackSprintf("#\tdefine %s_%s_SIZEOF 0u", m_activeStruct.cstr(), m.m_name.cstr());
  861. // # define XXX_LOAD()
  862. m_codeLines.pushBackSprintf("#\tdefine %s_%s_LOAD(ssbo, offset)", m_activeStruct.cstr(), m.m_name.cstr());
  863. // #endif
  864. m_codeLines.pushBack("#endif");
  865. }
  866. // Now define the structure LOAD
  867. m_codeLines.pushBackSprintf("#define load%s(ssbo, offset) %s( \\", m_activeStruct.cstr(), m_activeStruct.cstr());
  868. for(U32 i = 0; i < m_members.getSize(); ++i)
  869. {
  870. const Member& m = m_members[i];
  871. m_codeLines.pushBackSprintf("\t%s_%s_LOAD(ssbo, offset) \\", m_activeStruct.cstr(), m.m_name.cstr());
  872. }
  873. m_codeLines.pushBack(")");
  874. // Cleanup
  875. m_activeStruct.destroy();
  876. m_members.destroy();
  877. return Error::NONE;
  878. }
  879. Error ShaderProgramParser::parseFile(CString fname, U32 depth)
  880. {
  881. // First check the depth
  882. if(depth > MAX_INCLUDE_DEPTH)
  883. {
  884. ANKI_SHADER_COMPILER_LOGE("The include depth is too high. Probably circular includance");
  885. }
  886. Bool foundPragmaOnce = false;
  887. // Load file in lines
  888. StringAuto txt(m_alloc);
  889. ANKI_CHECK(m_fsystem->readAllText(fname, txt));
  890. StringListAuto lines(m_alloc);
  891. lines.splitString(txt.toCString(), '\n');
  892. if(lines.getSize() < 1)
  893. {
  894. ANKI_SHADER_COMPILER_LOGE("Source is empty");
  895. }
  896. // Parse lines
  897. for(const String& line : lines)
  898. {
  899. if(line.find("pragma") != CString::NPOS || line.find("include") != CString::NPOS)
  900. {
  901. // Possibly a preprocessor directive we care
  902. ANKI_CHECK(parseLine(line.toCString(), fname, foundPragmaOnce, depth));
  903. }
  904. else
  905. {
  906. // Just append the line
  907. m_codeLines.pushBack(line.toCString());
  908. }
  909. }
  910. if(foundPragmaOnce)
  911. {
  912. // Append the guard
  913. m_codeLines.pushBack("#endif // Include guard");
  914. }
  915. return Error::NONE;
  916. }
  917. Error ShaderProgramParser::parse()
  918. {
  919. ANKI_ASSERT(!m_fname.isEmpty());
  920. ANKI_ASSERT(m_codeLines.isEmpty());
  921. const CString fname = m_fname;
  922. // Parse recursively
  923. ANKI_CHECK(parseFile(fname, 0));
  924. // Checks
  925. {
  926. if(!!(m_shaderTypes & ShaderTypeBit::COMPUTE))
  927. {
  928. if(m_shaderTypes != ShaderTypeBit::COMPUTE)
  929. {
  930. ANKI_SHADER_COMPILER_LOGE("Can't combine compute shader with other types of shaders");
  931. return Error::USER_DATA;
  932. }
  933. }
  934. else if(!!(m_shaderTypes & ShaderTypeBit::ALL_GRAPHICS))
  935. {
  936. if(!(m_shaderTypes & ShaderTypeBit::VERTEX))
  937. {
  938. ANKI_SHADER_COMPILER_LOGE("Missing vertex shader");
  939. return Error::USER_DATA;
  940. }
  941. if(!(m_shaderTypes & ShaderTypeBit::FRAGMENT))
  942. {
  943. ANKI_SHADER_COMPILER_LOGE("Missing fragment shader");
  944. return Error::USER_DATA;
  945. }
  946. }
  947. if(m_insideShader)
  948. {
  949. ANKI_SHADER_COMPILER_LOGE("Forgot a \"pragma anki end\"");
  950. return Error::USER_DATA;
  951. }
  952. }
  953. // Create the code lines
  954. if(m_codeLines.getSize())
  955. {
  956. m_codeLines.join("\n", m_codeSource);
  957. m_codeLines.destroy();
  958. }
  959. // Create the hash
  960. {
  961. if(m_codeSource.getLength())
  962. {
  963. m_codeSourceHash = appendHash(m_codeSource.getBegin(), m_codeSource.getLength(), SHADER_HEADER_HASH);
  964. }
  965. if(m_libName.getLength() > 0)
  966. {
  967. m_codeSourceHash = appendHash(m_libName.getBegin(), m_libName.getLength(), m_codeSourceHash);
  968. }
  969. m_codeSourceHash = appendHash(&m_rayType, sizeof(m_rayType), m_codeSourceHash);
  970. }
  971. return Error::NONE;
  972. }
  973. void ShaderProgramParser::generateAnkiShaderHeader(ShaderType shaderType, const ShaderCompilerOptions& compilerOptions,
  974. StringAuto& header)
  975. {
  976. header.sprintf(SHADER_HEADER, SHADER_STAGE_NAMES[shaderType].cstr(), compilerOptions.m_mobilePlatform,
  977. compilerOptions.m_forceFullFloatingPointPrecision, MAX_BINDLESS_TEXTURES, MAX_BINDLESS_IMAGES);
  978. }
  979. Error ShaderProgramParser::generateVariant(ConstWeakArray<MutatorValue> mutation,
  980. ShaderProgramParserVariant& variant) const
  981. {
  982. // Sanity checks
  983. ANKI_ASSERT(m_codeSource.getLength() > 0);
  984. ANKI_ASSERT(mutation.getSize() == m_mutators.getSize());
  985. for(U32 i = 0; i < mutation.getSize(); ++i)
  986. {
  987. ANKI_ASSERT(mutatorHasValue(m_mutators[i], mutation[i]) && "Value not found");
  988. }
  989. // Init variant
  990. ::new(&variant) ShaderProgramParserVariant();
  991. variant.m_alloc = m_alloc;
  992. // Create the mutator defines
  993. StringAuto mutatorDefines(m_alloc);
  994. for(U32 i = 0; i < mutation.getSize(); ++i)
  995. {
  996. mutatorDefines.append(StringAuto(m_alloc).sprintf("#define %s %d\n", m_mutators[i].m_name.cstr(), mutation[i]));
  997. }
  998. // Generate souce per stage
  999. for(ShaderType shaderType : EnumIterable<ShaderType>())
  1000. {
  1001. if(!(ShaderTypeBit(1u << shaderType) & m_shaderTypes))
  1002. {
  1003. continue;
  1004. }
  1005. // Create the header
  1006. StringAuto header(m_alloc);
  1007. generateAnkiShaderHeader(shaderType, m_compilerOptions, header);
  1008. // Create the final source without the bindings
  1009. StringAuto finalSource(m_alloc);
  1010. finalSource.append(header);
  1011. finalSource.append(mutatorDefines);
  1012. finalSource.append(m_codeSource);
  1013. // Move the source
  1014. variant.m_sources[shaderType] = std::move(finalSource);
  1015. }
  1016. return Error::NONE;
  1017. }
  1018. Bool ShaderProgramParser::rewriteMutation(WeakArray<MutatorValue> mutation) const
  1019. {
  1020. // Checks
  1021. ANKI_ASSERT(mutation.getSize() == m_mutators.getSize());
  1022. for(U32 i = 0; i < mutation.getSize(); ++i)
  1023. {
  1024. ANKI_ASSERT(mutatorHasValue(m_mutators[i], mutation[i]));
  1025. }
  1026. // Early exit
  1027. if(mutation.getSize() == 0)
  1028. {
  1029. return false;
  1030. }
  1031. // Find if mutation exists
  1032. for(const MutationRewrite& rewrite : m_mutationRewrites)
  1033. {
  1034. Bool found = true;
  1035. for(U32 i = 0; i < rewrite.m_records.getSize(); ++i)
  1036. {
  1037. if(rewrite.m_records[i].m_valueFrom != mutation[rewrite.m_records[i].m_mutatorIndex])
  1038. {
  1039. found = false;
  1040. break;
  1041. }
  1042. }
  1043. if(found)
  1044. {
  1045. // Rewrite it
  1046. for(U32 i = 0; i < rewrite.m_records.getSize(); ++i)
  1047. {
  1048. mutation[rewrite.m_records[i].m_mutatorIndex] = rewrite.m_records[i].m_valueTo;
  1049. }
  1050. return true;
  1051. }
  1052. }
  1053. return false;
  1054. }
  1055. Bool ShaderProgramParser::mutatorHasValue(const ShaderProgramParserMutator& mutator, MutatorValue value)
  1056. {
  1057. for(MutatorValue v : mutator.m_values)
  1058. {
  1059. if(value == v)
  1060. {
  1061. return true;
  1062. }
  1063. }
  1064. return false;
  1065. }
  1066. } // end namespace anki