ShaderProgramParser.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299
  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. class ShaderProgramParser::PartialMutationSkip
  232. {
  233. public:
  234. DynamicArrayAuto<MutatorValue> m_partialMutation;
  235. PartialMutationSkip(const GenericMemoryPoolAllocator<U8>& alloc)
  236. : m_partialMutation(alloc)
  237. {
  238. }
  239. };
  240. ShaderProgramParser::ShaderProgramParser(CString fname, ShaderProgramFilesystemInterface* fsystem,
  241. GenericMemoryPoolAllocator<U8> alloc,
  242. const ShaderCompilerOptions& compilerOptions)
  243. : m_alloc(alloc)
  244. , m_fname(alloc, fname)
  245. , m_fsystem(fsystem)
  246. , m_compilerOptions(compilerOptions)
  247. {
  248. }
  249. ShaderProgramParser::~ShaderProgramParser()
  250. {
  251. }
  252. void ShaderProgramParser::tokenizeLine(CString line, DynamicArrayAuto<StringAuto>& tokens) const
  253. {
  254. ANKI_ASSERT(line.getLength() > 0);
  255. StringAuto l(m_alloc, line);
  256. // Replace all tabs with spaces
  257. for(char& c : l)
  258. {
  259. if(c == '\t')
  260. {
  261. c = ' ';
  262. }
  263. }
  264. // Split
  265. StringListAuto spaceTokens(m_alloc);
  266. spaceTokens.splitString(l, ' ', false);
  267. // Create the array
  268. for(const String& s : spaceTokens)
  269. {
  270. tokens.emplaceBack(m_alloc, s);
  271. }
  272. }
  273. Error ShaderProgramParser::parsePragmaStart(const StringAuto* begin, const StringAuto* end, CString line, CString fname)
  274. {
  275. ANKI_ASSERT(begin && end);
  276. if(begin >= end)
  277. {
  278. ANKI_PP_ERROR_MALFORMED();
  279. }
  280. ShaderType shaderType = ShaderType::COUNT;
  281. if(*begin == "vert")
  282. {
  283. shaderType = ShaderType::VERTEX;
  284. }
  285. else if(*begin == "tessc")
  286. {
  287. shaderType = ShaderType::TESSELLATION_CONTROL;
  288. }
  289. else if(*begin == "tesse")
  290. {
  291. }
  292. else if(*begin == "geom")
  293. {
  294. shaderType = ShaderType::GEOMETRY;
  295. }
  296. else if(*begin == "frag")
  297. {
  298. shaderType = ShaderType::FRAGMENT;
  299. }
  300. else if(*begin == "comp")
  301. {
  302. shaderType = ShaderType::COMPUTE;
  303. }
  304. else if(*begin == "rgen")
  305. {
  306. shaderType = ShaderType::RAY_GEN;
  307. }
  308. else if(*begin == "ahit")
  309. {
  310. shaderType = ShaderType::ANY_HIT;
  311. }
  312. else if(*begin == "chit")
  313. {
  314. shaderType = ShaderType::CLOSEST_HIT;
  315. }
  316. else if(*begin == "miss")
  317. {
  318. shaderType = ShaderType::MISS;
  319. }
  320. else if(*begin == "int")
  321. {
  322. shaderType = ShaderType::INTERSECTION;
  323. }
  324. else if(*begin == "call")
  325. {
  326. shaderType = ShaderType::CALLABLE;
  327. }
  328. else
  329. {
  330. ANKI_PP_ERROR_MALFORMED();
  331. }
  332. m_codeLines.pushBackSprintf("#ifdef ANKI_%s_SHADER", SHADER_STAGE_NAMES[shaderType].cstr());
  333. ++begin;
  334. if(begin != end)
  335. {
  336. // Should be the last token
  337. ANKI_PP_ERROR_MALFORMED();
  338. }
  339. // Set the mask
  340. const ShaderTypeBit mask = ShaderTypeBit(1 << shaderType);
  341. if(!!(mask & m_shaderTypes))
  342. {
  343. ANKI_PP_ERROR_MALFORMED_MSG("Can't have #pragma start <shader> appearing more than once");
  344. }
  345. m_shaderTypes |= mask;
  346. // Check bounds
  347. if(m_insideShader)
  348. {
  349. ANKI_PP_ERROR_MALFORMED_MSG("Can't have #pragma start before you close the previous pragma start");
  350. }
  351. m_insideShader = true;
  352. return Error::NONE;
  353. }
  354. Error ShaderProgramParser::parsePragmaEnd(const StringAuto* begin, const StringAuto* end, CString line, CString fname)
  355. {
  356. ANKI_ASSERT(begin && end);
  357. // Check tokens
  358. if(begin != end)
  359. {
  360. ANKI_PP_ERROR_MALFORMED();
  361. }
  362. // Check bounds
  363. if(!m_insideShader)
  364. {
  365. ANKI_PP_ERROR_MALFORMED_MSG("Can't have #pragma end before you open with a pragma start");
  366. }
  367. m_insideShader = false;
  368. // Write code
  369. m_codeLines.pushBack("#endif // Shader guard");
  370. return Error::NONE;
  371. }
  372. Error ShaderProgramParser::parsePragmaMutator(const StringAuto* begin, const StringAuto* end, CString line,
  373. CString fname)
  374. {
  375. ANKI_ASSERT(begin && end);
  376. if(begin >= end)
  377. {
  378. ANKI_PP_ERROR_MALFORMED();
  379. }
  380. m_mutators.emplaceBack(m_alloc);
  381. Mutator& mutator = m_mutators.getBack();
  382. // Name
  383. {
  384. if(begin >= end)
  385. {
  386. // Need to have a name
  387. ANKI_PP_ERROR_MALFORMED();
  388. }
  389. // Check for duplicate mutators
  390. for(U32 i = 0; i < m_mutators.getSize() - 1; ++i)
  391. {
  392. if(m_mutators[i].m_name == *begin)
  393. {
  394. ANKI_PP_ERROR_MALFORMED_MSG("Duplicate mutator");
  395. }
  396. }
  397. if(begin->getLength() > MAX_SHADER_BINARY_NAME_LENGTH)
  398. {
  399. ANKI_PP_ERROR_MALFORMED_MSG("Too big name");
  400. }
  401. mutator.m_name.create(begin->toCString());
  402. ++begin;
  403. }
  404. // Values
  405. {
  406. // Gather them
  407. for(; begin < end; ++begin)
  408. {
  409. MutatorValue value = 0;
  410. if(tokenIsComment(begin->toCString()))
  411. {
  412. break;
  413. }
  414. if(begin->toNumber(value))
  415. {
  416. ANKI_PP_ERROR_MALFORMED();
  417. }
  418. mutator.m_values.emplaceBack(value);
  419. }
  420. std::sort(mutator.m_values.getBegin(), mutator.m_values.getEnd());
  421. // Check for duplicates
  422. for(U32 i = 1; i < mutator.m_values.getSize(); ++i)
  423. {
  424. if(mutator.m_values[i - 1] == mutator.m_values[i])
  425. {
  426. ANKI_PP_ERROR_MALFORMED_MSG("Same value appeared more than once");
  427. }
  428. }
  429. }
  430. return Error::NONE;
  431. }
  432. Error ShaderProgramParser::parsePragmaLibraryName(const StringAuto* begin, const StringAuto* end, CString line,
  433. CString fname)
  434. {
  435. ANKI_ASSERT(begin && end);
  436. if(begin >= end)
  437. {
  438. ANKI_PP_ERROR_MALFORMED();
  439. }
  440. if(m_libName.getLength() > 0)
  441. {
  442. ANKI_PP_ERROR_MALFORMED_MSG("Library name already set");
  443. }
  444. m_libName = *begin;
  445. return Error::NONE;
  446. }
  447. Error ShaderProgramParser::parsePragmaRayType(const StringAuto* begin, const StringAuto* end, CString line,
  448. CString fname)
  449. {
  450. ANKI_ASSERT(begin && end);
  451. if(begin >= end)
  452. {
  453. ANKI_PP_ERROR_MALFORMED();
  454. }
  455. if(m_rayType != MAX_U32)
  456. {
  457. ANKI_PP_ERROR_MALFORMED_MSG("Ray type already set");
  458. }
  459. ANKI_CHECK(begin->toNumber(m_rayType));
  460. if(m_rayType > 128)
  461. {
  462. ANKI_PP_ERROR_MALFORMED_MSG("Ray type has a very large value");
  463. }
  464. return Error::NONE;
  465. }
  466. Error ShaderProgramParser::parsePragmaReflect(const StringAuto* begin, const StringAuto* end, CString line,
  467. CString fname)
  468. {
  469. ANKI_ASSERT(begin && end);
  470. if(begin >= end)
  471. {
  472. ANKI_PP_ERROR_MALFORMED();
  473. }
  474. m_symbolsToReflect.pushBack(*begin);
  475. return Error::NONE;
  476. }
  477. Error ShaderProgramParser::parsePragmaSkipMutation(const StringAuto* begin, const StringAuto* end, CString line,
  478. CString fname)
  479. {
  480. ANKI_ASSERT(begin && end);
  481. // Some basic sanity checks
  482. const U tokenCount = U(end - begin);
  483. // One pair doesn't make sence so it's: mutator_name_0 + mutator_value_0 + mutator_name_1 + mutator_value_1
  484. constexpr U minTokenCount = 2 + 2;
  485. if(tokenCount < minTokenCount || (tokenCount % 2) != 0)
  486. {
  487. ANKI_PP_ERROR_MALFORMED();
  488. }
  489. PartialMutationSkip& skip = *m_skipMutations.emplaceBack(m_alloc);
  490. skip.m_partialMutation.create(m_mutators.getSize(), std::numeric_limits<MutatorValue>::max());
  491. do
  492. {
  493. // Get mutator name
  494. const CString mutatorName = *begin;
  495. U32 mutatorIndex = MAX_U32;
  496. for(U32 i = 0; i < m_mutators.getSize(); ++i)
  497. {
  498. if(m_mutators[i].m_name == mutatorName)
  499. {
  500. mutatorIndex = i;
  501. break;
  502. }
  503. }
  504. if(mutatorIndex == MAX_U32)
  505. {
  506. ANKI_PP_ERROR_MALFORMED_MSG("Mutator not found");
  507. }
  508. // Get mutator value
  509. ++begin;
  510. const CString valueStr = *begin;
  511. MutatorValue value;
  512. if(valueStr.toNumber(value))
  513. {
  514. ANKI_PP_ERROR_MALFORMED_MSG("Malformed mutator value");
  515. }
  516. if(!mutatorHasValue(m_mutators[mutatorIndex], value))
  517. {
  518. ANKI_PP_ERROR_MALFORMED_MSG("Mutator value incorrect");
  519. }
  520. skip.m_partialMutation[mutatorIndex] = value;
  521. ++begin;
  522. } while(begin < end && !tokenIsComment(*begin));
  523. return Error::NONE;
  524. }
  525. Error ShaderProgramParser::parseInclude(const StringAuto* begin, const StringAuto* end, CString line, CString fname,
  526. U32 depth)
  527. {
  528. // Gather the path
  529. StringAuto path(m_alloc);
  530. for(; begin < end; ++begin)
  531. {
  532. path.append(*begin);
  533. }
  534. if(path.isEmpty())
  535. {
  536. ANKI_PP_ERROR_MALFORMED();
  537. }
  538. // Check
  539. const char firstChar = path[0];
  540. const char lastChar = path[path.getLength() - 1];
  541. if((firstChar == '\"' && lastChar == '\"') || (firstChar == '<' && lastChar == '>'))
  542. {
  543. StringAuto fname2(m_alloc);
  544. fname2.create(path.begin() + 1, path.begin() + path.getLength() - 1);
  545. const Bool dontIgnore =
  546. fname2.find("AnKi/Shaders/") != String::NPOS || fname2.find("ThirdParty/") != String::NPOS;
  547. if(!dontIgnore)
  548. {
  549. // The shaders can't include C++ files. Ignore the include
  550. return Error::NONE;
  551. }
  552. if(parseFile(fname2, depth + 1))
  553. {
  554. ANKI_PP_ERROR_MALFORMED_MSG("Error parsing include. See previous errors");
  555. }
  556. }
  557. else
  558. {
  559. ANKI_PP_ERROR_MALFORMED();
  560. }
  561. return Error::NONE;
  562. }
  563. Error ShaderProgramParser::parseLine(CString line, CString fname, Bool& foundPragmaOnce, U32 depth)
  564. {
  565. // Tokenize
  566. DynamicArrayAuto<StringAuto> tokens(m_alloc);
  567. tokenizeLine(line, tokens);
  568. ANKI_ASSERT(tokens.getSize() > 0);
  569. const StringAuto* token = tokens.getBegin();
  570. const StringAuto* end = tokens.getEnd();
  571. // Skip the hash
  572. Bool foundAloneHash = false;
  573. if(*token == "#")
  574. {
  575. ++token;
  576. foundAloneHash = true;
  577. }
  578. if((token < end) && ((foundAloneHash && *token == "include") || *token == "#include"))
  579. {
  580. // We _must_ have an #include
  581. ANKI_CHECK(parseInclude(token + 1, end, line, fname, depth));
  582. }
  583. else if((token < end) && ((foundAloneHash && *token == "pragma") || *token == "#pragma"))
  584. {
  585. // We may have a #pragma once or a #pragma anki or something else
  586. ++token;
  587. if(*token == "once")
  588. {
  589. // Pragma once
  590. if(foundPragmaOnce)
  591. {
  592. ANKI_PP_ERROR_MALFORMED_MSG("Can't have more than one #pragma once per file");
  593. }
  594. if(token + 1 != end)
  595. {
  596. ANKI_PP_ERROR_MALFORMED();
  597. }
  598. // Add the guard unique for this file
  599. foundPragmaOnce = true;
  600. const U64 hash = fname.computeHash();
  601. m_codeLines.pushBackSprintf("#ifndef _ANKI_INCL_GUARD_%llu\n"
  602. "#define _ANKI_INCL_GUARD_%llu",
  603. hash, hash);
  604. }
  605. else if(*token == "anki")
  606. {
  607. // Must be a #pragma anki
  608. ++token;
  609. if(*token == "mutator")
  610. {
  611. ANKI_CHECK(checkNoActiveStruct());
  612. ANKI_CHECK(parsePragmaMutator(token + 1, end, line, fname));
  613. }
  614. else if(*token == "start")
  615. {
  616. ANKI_CHECK(checkNoActiveStruct());
  617. ANKI_CHECK(parsePragmaStart(token + 1, end, line, fname));
  618. }
  619. else if(*token == "end")
  620. {
  621. ANKI_CHECK(checkNoActiveStruct());
  622. ANKI_CHECK(parsePragmaEnd(token + 1, end, line, fname));
  623. }
  624. else if(*token == "skip_mutation")
  625. {
  626. ANKI_CHECK(checkNoActiveStruct());
  627. ANKI_CHECK(parsePragmaSkipMutation(token + 1, end, line, fname));
  628. }
  629. else if(*token == "library")
  630. {
  631. ANKI_CHECK(checkNoActiveStruct());
  632. ANKI_CHECK(parsePragmaLibraryName(token + 1, end, line, fname));
  633. }
  634. else if(*token == "ray_type")
  635. {
  636. ANKI_CHECK(checkNoActiveStruct());
  637. ANKI_CHECK(parsePragmaRayType(token + 1, end, line, fname));
  638. }
  639. else if(*token == "reflect")
  640. {
  641. ANKI_CHECK(checkNoActiveStruct());
  642. ANKI_CHECK(parsePragmaReflect(token + 1, end, line, fname));
  643. }
  644. else if(*token == "struct")
  645. {
  646. if(*(token + 1) == "end")
  647. {
  648. ANKI_CHECK(checkActiveStruct());
  649. ANKI_CHECK(parsePragmaStructEnd(token + 1, end, line, fname));
  650. }
  651. else
  652. {
  653. ANKI_CHECK(checkNoActiveStruct());
  654. ANKI_CHECK(parsePragmaStructBegin(token + 1, end, line, fname));
  655. }
  656. }
  657. else if(*token == "member")
  658. {
  659. ANKI_CHECK(checkActiveStruct());
  660. ANKI_CHECK(parsePragmaMember(token + 1, end, line, fname));
  661. }
  662. else
  663. {
  664. ANKI_PP_ERROR_MALFORMED();
  665. }
  666. // Add the line as a comment because of hashing of the source
  667. m_codeLines.pushBackSprintf("//%s", line.cstr());
  668. }
  669. else
  670. {
  671. // Some other pragma
  672. ANKI_SHADER_COMPILER_LOGW("Ignoring: %s", line.cstr());
  673. m_codeLines.pushBack(line);
  674. }
  675. }
  676. else
  677. {
  678. // Ignore
  679. m_codeLines.pushBack(line);
  680. }
  681. return Error::NONE;
  682. }
  683. Error ShaderProgramParser::parsePragmaStructBegin(const StringAuto* begin, const StringAuto* end, CString line,
  684. CString fname)
  685. {
  686. const U tokenCount = U(end - begin);
  687. if(tokenCount != 1)
  688. {
  689. ANKI_PP_ERROR_MALFORMED();
  690. }
  691. GhostStruct& gstruct = *m_ghostStructs.emplaceBack(m_alloc);
  692. gstruct.m_name.create(*begin);
  693. // Add a '_' to the struct name.
  694. //
  695. // Scenario:
  696. // - The shader may have a "pragma reflect" of the struct
  697. // - The SPIRV also contains the struct
  698. //
  699. // What happens:
  700. // - The struct is in SPIRV and it will be reflected
  701. // - The struct is also in ghost structs and it will be reflected
  702. //
  703. // This is undesirable because it will complicates reflection. So eliminate the struct from SPIRV by renaming it
  704. m_codeLines.pushBackSprintf("struct %s_ {", begin->cstr());
  705. ANKI_ASSERT(!m_insideStruct);
  706. m_insideStruct = true;
  707. return Error::NONE;
  708. }
  709. Error ShaderProgramParser::parsePragmaMember(const StringAuto* begin, const StringAuto* end, CString line,
  710. CString fname)
  711. {
  712. ANKI_ASSERT(m_insideStruct);
  713. const U tokenCount = U(end - begin);
  714. if(tokenCount == 0)
  715. {
  716. ANKI_PP_ERROR_MALFORMED();
  717. }
  718. Member& member = *m_ghostStructs.getBack().m_members.emplaceBack(m_alloc);
  719. // Relaxed
  720. Bool relaxed = false;
  721. if(*begin == "ANKI_RP")
  722. {
  723. relaxed = true;
  724. ++begin;
  725. }
  726. // Type
  727. if(begin == end)
  728. {
  729. ANKI_PP_ERROR_MALFORMED();
  730. }
  731. const CString typeStr = *begin;
  732. member.m_type = ShaderVariableDataType::NONE;
  733. if(typeStr == "F32")
  734. {
  735. member.m_type = ShaderVariableDataType::F32;
  736. }
  737. else if(typeStr == "Vec2")
  738. {
  739. member.m_type = ShaderVariableDataType::VEC2;
  740. }
  741. else if(typeStr == "Vec3")
  742. {
  743. member.m_type = ShaderVariableDataType::VEC3;
  744. }
  745. else if(typeStr == "Vec4")
  746. {
  747. member.m_type = ShaderVariableDataType::VEC4;
  748. }
  749. else if(typeStr == "U32")
  750. {
  751. member.m_type = ShaderVariableDataType::U32;
  752. }
  753. if(member.m_type == ShaderVariableDataType::NONE)
  754. {
  755. ANKI_PP_ERROR_MALFORMED_MSG("Unrecognized type");
  756. }
  757. ++begin;
  758. // Name
  759. if(begin == end)
  760. {
  761. ANKI_PP_ERROR_MALFORMED();
  762. }
  763. member.m_name.create(*begin);
  764. ++begin;
  765. // if MUTATOR_NAME is MUTATOR_VALUE
  766. if(begin != end)
  767. {
  768. // "if"
  769. if(*begin != "if")
  770. {
  771. ANKI_PP_ERROR_MALFORMED();
  772. }
  773. ++begin;
  774. // MUTATOR_NAME
  775. if(begin == end)
  776. {
  777. ANKI_PP_ERROR_MALFORMED();
  778. }
  779. const CString mutatorName = *begin;
  780. for(U32 i = 0; i < m_mutators.getSize(); ++i)
  781. {
  782. if(m_mutators[i].m_name == mutatorName)
  783. {
  784. member.m_dependentMutator = i;
  785. break;
  786. }
  787. }
  788. if(member.m_dependentMutator == MAX_U32)
  789. {
  790. ANKI_PP_ERROR_MALFORMED_MSG("Mutator not found");
  791. }
  792. ++begin;
  793. // "is"
  794. if(begin == end)
  795. {
  796. ANKI_PP_ERROR_MALFORMED();
  797. }
  798. if(*begin != "is")
  799. {
  800. ANKI_PP_ERROR_MALFORMED();
  801. }
  802. ++begin;
  803. // MUTATOR_VALUE
  804. if(begin == end)
  805. {
  806. ANKI_PP_ERROR_MALFORMED();
  807. }
  808. ANKI_CHECK(begin->toNumber(member.m_mutatorValue));
  809. if(!mutatorHasValue(m_mutators[member.m_dependentMutator], member.m_mutatorValue))
  810. {
  811. ANKI_PP_ERROR_MALFORMED_MSG("Wrong mutator value");
  812. }
  813. ++begin;
  814. }
  815. if(begin != end)
  816. {
  817. ANKI_PP_ERROR_MALFORMED();
  818. }
  819. // Code
  820. if(member.m_dependentMutator != MAX_U32)
  821. {
  822. m_codeLines.pushBackSprintf("#if %s == %d", m_mutators[member.m_dependentMutator].m_name.cstr(),
  823. member.m_mutatorValue);
  824. }
  825. m_codeLines.pushBackSprintf("#\tdefine %s_%s_DEFINED 1", m_ghostStructs.getBack().m_name.cstr(),
  826. member.m_name.cstr());
  827. m_codeLines.pushBackSprintf("\t%s %s %s;", (relaxed) ? "ANKI_RP" : "", typeStr.cstr(), member.m_name.cstr());
  828. if(member.m_dependentMutator != MAX_U32)
  829. {
  830. m_codeLines.pushBack("#endif");
  831. }
  832. return Error::NONE;
  833. }
  834. Error ShaderProgramParser::parsePragmaStructEnd(const StringAuto* begin, const StringAuto* end, CString line,
  835. CString fname)
  836. {
  837. ANKI_ASSERT(m_insideStruct);
  838. const U tokenCount = U(end - begin);
  839. if(tokenCount != 1)
  840. {
  841. ANKI_PP_ERROR_MALFORMED();
  842. }
  843. GhostStruct& gstruct = m_ghostStructs.getBack();
  844. const CString structName = gstruct.m_name;
  845. if(gstruct.m_members.isEmpty())
  846. {
  847. ANKI_PP_ERROR_MALFORMED_MSG("The struct doesn't have any members");
  848. }
  849. m_codeLines.pushBack("};");
  850. for(U32 i = 0; i < gstruct.m_members.getSize(); ++i)
  851. {
  852. const Member& m = gstruct.m_members[i];
  853. // #define XXX_OFFSETOF
  854. if(i == 0)
  855. {
  856. m_codeLines.pushBackSprintf("#define %s_%s_OFFSETOF 0u", gstruct.m_name.cstr(), m.m_name.cstr());
  857. }
  858. else
  859. {
  860. const Member& prev = gstruct.m_members[i - 1];
  861. m_codeLines.pushBackSprintf("#define %s_%s_OFFSETOF (%s_%s_OFFSETOF + %s_%s_SIZEOF)", structName.cstr(),
  862. m.m_name.cstr(), structName.cstr(), prev.m_name.cstr(), structName.cstr(),
  863. prev.m_name.cstr());
  864. }
  865. // #if XXX_DEFINED
  866. m_codeLines.pushBackSprintf("#if defined(%s_%s_DEFINED)", structName.cstr(), m.m_name.cstr());
  867. // # define XXX_SIZEOF
  868. m_codeLines.pushBackSprintf("#\tdefine %s_%s_SIZEOF %uu", structName.cstr(), m.m_name.cstr(),
  869. getShaderVariableDataTypeInfo(m.m_type).m_size / 4);
  870. // # define XXX_LOAD()
  871. const Bool isIntegral = getShaderVariableDataTypeInfo(m.m_type).m_isIntegral;
  872. const U32 componentCount = getShaderVariableDataTypeInfo(m.m_type).m_size / sizeof(U32);
  873. StringAuto values(m_alloc);
  874. for(U32 j = 0; j < componentCount; ++j)
  875. {
  876. StringAuto tmp(m_alloc);
  877. tmp.sprintf("%s(ssbo[%s_%s_OFFSETOF + offset + %uu])%s", (isIntegral) ? "" : "uintBitsToFloat",
  878. structName.cstr(), m.m_name.cstr(), j, (j != componentCount - 1) ? "," : "");
  879. values.append(tmp);
  880. }
  881. m_codeLines.pushBackSprintf("#\tdefine %s_%s_LOAD(ssbo, offset) %s(%s)%s", structName.cstr(), m.m_name.cstr(),
  882. getShaderVariableDataTypeInfo(m.m_type).m_name, values.cstr(),
  883. (i != gstruct.m_members.getSize() - 1) ? "," : "");
  884. // #else
  885. m_codeLines.pushBack("#else");
  886. // # define XXX_SIZEOF 0
  887. m_codeLines.pushBackSprintf("#\tdefine %s_%s_SIZEOF 0u", structName.cstr(), m.m_name.cstr());
  888. // # define XXX_LOAD()
  889. m_codeLines.pushBackSprintf("#\tdefine %s_%s_LOAD(ssbo, offset)", structName.cstr(), m.m_name.cstr());
  890. // #endif
  891. m_codeLines.pushBack("#endif");
  892. }
  893. // Now define the structure LOAD
  894. m_codeLines.pushBackSprintf("#define load%s(ssbo, offset) %s( \\", structName.cstr(), structName.cstr());
  895. for(U32 i = 0; i < gstruct.m_members.getSize(); ++i)
  896. {
  897. const Member& m = gstruct.m_members[i];
  898. m_codeLines.pushBackSprintf("\t%s_%s_LOAD(ssbo, offset) \\", structName.cstr(), m.m_name.cstr());
  899. }
  900. m_codeLines.pushBack(")");
  901. // Define the actual struct
  902. m_codeLines.pushBackSprintf("#define %s %s_", structName.cstr(), structName.cstr());
  903. m_insideStruct = false;
  904. return Error::NONE;
  905. }
  906. Error ShaderProgramParser::parseFile(CString fname, U32 depth)
  907. {
  908. // First check the depth
  909. if(depth > MAX_INCLUDE_DEPTH)
  910. {
  911. ANKI_SHADER_COMPILER_LOGE("The include depth is too high. Probably circular includance");
  912. }
  913. Bool foundPragmaOnce = false;
  914. // Load file in lines
  915. StringAuto txt(m_alloc);
  916. ANKI_CHECK(m_fsystem->readAllText(fname, txt));
  917. StringListAuto lines(m_alloc);
  918. lines.splitString(txt.toCString(), '\n');
  919. if(lines.getSize() < 1)
  920. {
  921. ANKI_SHADER_COMPILER_LOGE("Source is empty");
  922. }
  923. // Parse lines
  924. for(const String& line : lines)
  925. {
  926. if(line.find("pragma") != CString::NPOS || line.find("include") != CString::NPOS)
  927. {
  928. // Possibly a preprocessor directive we care
  929. ANKI_CHECK(parseLine(line.toCString(), fname, foundPragmaOnce, depth));
  930. }
  931. else
  932. {
  933. // Just append the line
  934. m_codeLines.pushBack(line.toCString());
  935. }
  936. }
  937. if(foundPragmaOnce)
  938. {
  939. // Append the guard
  940. m_codeLines.pushBack("#endif // Include guard");
  941. }
  942. return Error::NONE;
  943. }
  944. Error ShaderProgramParser::parse()
  945. {
  946. ANKI_ASSERT(!m_fname.isEmpty());
  947. ANKI_ASSERT(m_codeLines.isEmpty());
  948. const CString fname = m_fname;
  949. // Parse recursively
  950. ANKI_CHECK(parseFile(fname, 0));
  951. // Checks
  952. {
  953. if(!!(m_shaderTypes & ShaderTypeBit::COMPUTE))
  954. {
  955. if(m_shaderTypes != ShaderTypeBit::COMPUTE)
  956. {
  957. ANKI_SHADER_COMPILER_LOGE("Can't combine compute shader with other types of shaders");
  958. return Error::USER_DATA;
  959. }
  960. }
  961. else if(!!(m_shaderTypes & ShaderTypeBit::ALL_GRAPHICS))
  962. {
  963. if(!(m_shaderTypes & ShaderTypeBit::VERTEX))
  964. {
  965. ANKI_SHADER_COMPILER_LOGE("Missing vertex shader");
  966. return Error::USER_DATA;
  967. }
  968. if(!(m_shaderTypes & ShaderTypeBit::FRAGMENT))
  969. {
  970. ANKI_SHADER_COMPILER_LOGE("Missing fragment shader");
  971. return Error::USER_DATA;
  972. }
  973. }
  974. if(m_insideShader)
  975. {
  976. ANKI_SHADER_COMPILER_LOGE("Forgot a \"pragma anki end\"");
  977. return Error::USER_DATA;
  978. }
  979. }
  980. // Create the code lines
  981. if(m_codeLines.getSize())
  982. {
  983. m_codeLines.join("\n", m_codeSource);
  984. m_codeLines.destroy();
  985. }
  986. // Create the hash
  987. {
  988. if(m_codeSource.getLength())
  989. {
  990. m_codeSourceHash = appendHash(m_codeSource.getBegin(), m_codeSource.getLength(), SHADER_HEADER_HASH);
  991. }
  992. if(m_libName.getLength() > 0)
  993. {
  994. m_codeSourceHash = appendHash(m_libName.getBegin(), m_libName.getLength(), m_codeSourceHash);
  995. }
  996. m_codeSourceHash = appendHash(&m_rayType, sizeof(m_rayType), m_codeSourceHash);
  997. }
  998. return Error::NONE;
  999. }
  1000. void ShaderProgramParser::generateAnkiShaderHeader(ShaderType shaderType, const ShaderCompilerOptions& compilerOptions,
  1001. StringAuto& header)
  1002. {
  1003. header.sprintf(SHADER_HEADER, SHADER_STAGE_NAMES[shaderType].cstr(), compilerOptions.m_mobilePlatform,
  1004. compilerOptions.m_forceFullFloatingPointPrecision, MAX_BINDLESS_TEXTURES, MAX_BINDLESS_IMAGES);
  1005. }
  1006. Error ShaderProgramParser::generateVariant(ConstWeakArray<MutatorValue> mutation,
  1007. ShaderProgramParserVariant& variant) const
  1008. {
  1009. // Sanity checks
  1010. ANKI_ASSERT(m_codeSource.getLength() > 0);
  1011. ANKI_ASSERT(mutation.getSize() == m_mutators.getSize());
  1012. for(U32 i = 0; i < mutation.getSize(); ++i)
  1013. {
  1014. ANKI_ASSERT(mutatorHasValue(m_mutators[i], mutation[i]) && "Value not found");
  1015. }
  1016. // Init variant
  1017. ::new(&variant) ShaderProgramParserVariant();
  1018. variant.m_alloc = m_alloc;
  1019. // Create the mutator defines
  1020. StringAuto mutatorDefines(m_alloc);
  1021. for(U32 i = 0; i < mutation.getSize(); ++i)
  1022. {
  1023. mutatorDefines.append(StringAuto(m_alloc).sprintf("#define %s %d\n", m_mutators[i].m_name.cstr(), mutation[i]));
  1024. }
  1025. // Generate souce per stage
  1026. for(ShaderType shaderType : EnumIterable<ShaderType>())
  1027. {
  1028. if(!(ShaderTypeBit(1u << shaderType) & m_shaderTypes))
  1029. {
  1030. continue;
  1031. }
  1032. // Create the header
  1033. StringAuto header(m_alloc);
  1034. generateAnkiShaderHeader(shaderType, m_compilerOptions, header);
  1035. // Create the final source without the bindings
  1036. StringAuto finalSource(m_alloc);
  1037. finalSource.append(header);
  1038. finalSource.append(mutatorDefines);
  1039. finalSource.append(m_codeSource);
  1040. // Move the source
  1041. variant.m_sources[shaderType] = std::move(finalSource);
  1042. }
  1043. return Error::NONE;
  1044. }
  1045. Bool ShaderProgramParser::mutatorHasValue(const ShaderProgramParserMutator& mutator, MutatorValue value)
  1046. {
  1047. for(MutatorValue v : mutator.m_values)
  1048. {
  1049. if(value == v)
  1050. {
  1051. return true;
  1052. }
  1053. }
  1054. return false;
  1055. }
  1056. Bool ShaderProgramParser::skipMutation(ConstWeakArray<MutatorValue> mutation) const
  1057. {
  1058. ANKI_ASSERT(mutation.getSize() == m_mutators.getSize());
  1059. for(const PartialMutationSkip& skip : m_skipMutations)
  1060. {
  1061. Bool doSkip = true;
  1062. for(U32 i = 0; i < m_mutators.getSize(); ++i)
  1063. {
  1064. if(skip.m_partialMutation[i] == std::numeric_limits<MutatorValue>::max())
  1065. {
  1066. // Don't care
  1067. continue;
  1068. }
  1069. if(skip.m_partialMutation[i] != mutation[i])
  1070. {
  1071. doSkip = false;
  1072. break;
  1073. }
  1074. }
  1075. if(doSkip)
  1076. {
  1077. return true;
  1078. }
  1079. }
  1080. return false;
  1081. }
  1082. } // end namespace anki