ShaderProgramParser.cpp 32 KB

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