ShaderProgramParser.cpp 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288
  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::parsePragmaSkipMutation(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. // One pair doesn't make sence so it's: mutator_name_0 + mutator_value_0 + mutator_name_1 + mutator_value_1
  475. constexpr U minTokenCount = 2 + 2;
  476. if(tokenCount < minTokenCount || (tokenCount % 2) != 0)
  477. {
  478. ANKI_PP_ERROR_MALFORMED();
  479. }
  480. PartialMutationSkip& skip = *m_skipMutations.emplaceBack(m_alloc);
  481. skip.m_partialMutation.create(m_mutators.getSize(), std::numeric_limits<MutatorValue>::max());
  482. do
  483. {
  484. // Get mutator name
  485. const CString mutatorName = *begin;
  486. U32 mutatorIndex = MAX_U32;
  487. for(U32 i = 0; i < m_mutators.getSize(); ++i)
  488. {
  489. if(m_mutators[i].m_name == mutatorName)
  490. {
  491. mutatorIndex = i;
  492. break;
  493. }
  494. }
  495. if(mutatorIndex == MAX_U32)
  496. {
  497. ANKI_PP_ERROR_MALFORMED_MSG("Mutator not found");
  498. }
  499. // Get mutator value
  500. ++begin;
  501. const CString valueStr = *begin;
  502. MutatorValue value;
  503. if(valueStr.toNumber(value))
  504. {
  505. ANKI_PP_ERROR_MALFORMED_MSG("Malformed mutator value");
  506. }
  507. if(!mutatorHasValue(m_mutators[mutatorIndex], value))
  508. {
  509. ANKI_PP_ERROR_MALFORMED_MSG("Mutator value incorrect");
  510. }
  511. skip.m_partialMutation[mutatorIndex] = value;
  512. ++begin;
  513. } while(begin < end && !tokenIsComment(*begin));
  514. return Error::NONE;
  515. }
  516. Error ShaderProgramParser::parseInclude(const StringAuto* begin, const StringAuto* end, CString line, CString fname,
  517. U32 depth)
  518. {
  519. // Gather the path
  520. StringAuto path(m_alloc);
  521. for(; begin < end; ++begin)
  522. {
  523. path.append(*begin);
  524. }
  525. if(path.isEmpty())
  526. {
  527. ANKI_PP_ERROR_MALFORMED();
  528. }
  529. // Check
  530. const char firstChar = path[0];
  531. const char lastChar = path[path.getLength() - 1];
  532. if((firstChar == '\"' && lastChar == '\"') || (firstChar == '<' && lastChar == '>'))
  533. {
  534. StringAuto fname2(m_alloc);
  535. fname2.create(path.begin() + 1, path.begin() + path.getLength() - 1);
  536. const Bool dontIgnore =
  537. fname2.find("AnKi/Shaders/") != String::NPOS || fname2.find("ThirdParty/") != String::NPOS;
  538. if(!dontIgnore)
  539. {
  540. // The shaders can't include C++ files. Ignore the include
  541. return Error::NONE;
  542. }
  543. if(parseFile(fname2, depth + 1))
  544. {
  545. ANKI_PP_ERROR_MALFORMED_MSG("Error parsing include. See previous errors");
  546. }
  547. }
  548. else
  549. {
  550. ANKI_PP_ERROR_MALFORMED();
  551. }
  552. return Error::NONE;
  553. }
  554. Error ShaderProgramParser::parseLine(CString line, CString fname, Bool& foundPragmaOnce, U32 depth)
  555. {
  556. // Tokenize
  557. DynamicArrayAuto<StringAuto> tokens(m_alloc);
  558. tokenizeLine(line, tokens);
  559. ANKI_ASSERT(tokens.getSize() > 0);
  560. const StringAuto* token = tokens.getBegin();
  561. const StringAuto* end = tokens.getEnd();
  562. // Skip the hash
  563. Bool foundAloneHash = false;
  564. if(*token == "#")
  565. {
  566. ++token;
  567. foundAloneHash = true;
  568. }
  569. if((token < end) && ((foundAloneHash && *token == "include") || *token == "#include"))
  570. {
  571. // We _must_ have an #include
  572. ANKI_CHECK(parseInclude(token + 1, end, line, fname, depth));
  573. }
  574. else if((token < end) && ((foundAloneHash && *token == "pragma") || *token == "#pragma"))
  575. {
  576. // We may have a #pragma once or a #pragma anki or something else
  577. ++token;
  578. if(*token == "once")
  579. {
  580. // Pragma once
  581. if(foundPragmaOnce)
  582. {
  583. ANKI_PP_ERROR_MALFORMED_MSG("Can't have more than one #pragma once per file");
  584. }
  585. if(token + 1 != end)
  586. {
  587. ANKI_PP_ERROR_MALFORMED();
  588. }
  589. // Add the guard unique for this file
  590. foundPragmaOnce = true;
  591. const U64 hash = fname.computeHash();
  592. m_codeLines.pushBackSprintf("#ifndef _ANKI_INCL_GUARD_%llu\n"
  593. "#define _ANKI_INCL_GUARD_%llu",
  594. hash, hash);
  595. }
  596. else if(*token == "anki")
  597. {
  598. // Must be a #pragma anki
  599. ++token;
  600. if(*token == "mutator")
  601. {
  602. ANKI_CHECK(checkNoActiveStruct());
  603. ANKI_CHECK(parsePragmaMutator(token + 1, end, line, fname));
  604. }
  605. else if(*token == "start")
  606. {
  607. ANKI_CHECK(checkNoActiveStruct());
  608. ANKI_CHECK(parsePragmaStart(token + 1, end, line, fname));
  609. }
  610. else if(*token == "end")
  611. {
  612. ANKI_CHECK(checkNoActiveStruct());
  613. ANKI_CHECK(parsePragmaEnd(token + 1, end, line, fname));
  614. }
  615. else if(*token == "skip_mutation")
  616. {
  617. ANKI_CHECK(checkNoActiveStruct());
  618. ANKI_CHECK(parsePragmaSkipMutation(token + 1, end, line, fname));
  619. }
  620. else if(*token == "library")
  621. {
  622. ANKI_CHECK(checkNoActiveStruct());
  623. ANKI_CHECK(parsePragmaLibraryName(token + 1, end, line, fname));
  624. }
  625. else if(*token == "ray_type")
  626. {
  627. ANKI_CHECK(checkNoActiveStruct());
  628. ANKI_CHECK(parsePragmaRayType(token + 1, end, line, fname));
  629. }
  630. else if(*token == "reflect")
  631. {
  632. ANKI_CHECK(checkNoActiveStruct());
  633. ANKI_CHECK(parsePragmaReflect(token + 1, end, line, fname));
  634. }
  635. else if(*token == "struct")
  636. {
  637. if(*(token + 1) == "end")
  638. {
  639. ANKI_CHECK(checkActiveStruct());
  640. ANKI_CHECK(parsePragmaStructEnd(token + 1, end, line, fname));
  641. }
  642. else
  643. {
  644. ANKI_CHECK(checkNoActiveStruct());
  645. ANKI_CHECK(parsePragmaStructBegin(token + 1, end, line, fname));
  646. }
  647. }
  648. else if(*token == "member")
  649. {
  650. ANKI_CHECK(checkActiveStruct());
  651. ANKI_CHECK(parsePragmaMember(token + 1, end, line, fname));
  652. }
  653. else
  654. {
  655. ANKI_PP_ERROR_MALFORMED();
  656. }
  657. // Add the line as a comment because of hashing of the source
  658. m_codeLines.pushBackSprintf("//%s", line.cstr());
  659. }
  660. else
  661. {
  662. // Some other pragma
  663. ANKI_SHADER_COMPILER_LOGW("Ignoring: %s", line.cstr());
  664. m_codeLines.pushBack(line);
  665. }
  666. }
  667. else
  668. {
  669. // Ignore
  670. m_codeLines.pushBack(line);
  671. }
  672. return Error::NONE;
  673. }
  674. Error ShaderProgramParser::parsePragmaStructBegin(const StringAuto* begin, const StringAuto* end, CString line,
  675. CString fname)
  676. {
  677. const U tokenCount = U(end - begin);
  678. if(tokenCount != 1)
  679. {
  680. ANKI_PP_ERROR_MALFORMED();
  681. }
  682. GhostStruct& gstruct = *m_ghostStructs.emplaceBack(m_alloc);
  683. gstruct.m_name.create(*begin);
  684. // Add a '_' to the struct name.
  685. //
  686. // Scenario:
  687. // - The shader may have a "pragma reflect" of the struct
  688. // - The SPIRV also contains the struct
  689. //
  690. // What happens:
  691. // - The struct is in SPIRV and it will be reflected
  692. // - The struct is also in ghost structs and it will be reflected
  693. //
  694. // This is undesirable because it will complicates reflection. So eliminate the struct from SPIRV by renaming it
  695. m_codeLines.pushBackSprintf("struct %s_ {", begin->cstr());
  696. ANKI_ASSERT(!m_insideStruct);
  697. m_insideStruct = true;
  698. return Error::NONE;
  699. }
  700. Error ShaderProgramParser::parsePragmaMember(const StringAuto* begin, const StringAuto* end, CString line,
  701. CString fname)
  702. {
  703. ANKI_ASSERT(m_insideStruct);
  704. const U tokenCount = U(end - begin);
  705. if(tokenCount == 0)
  706. {
  707. ANKI_PP_ERROR_MALFORMED();
  708. }
  709. Member& member = *m_ghostStructs.getBack().m_members.emplaceBack(m_alloc);
  710. // Relaxed
  711. Bool relaxed = false;
  712. if(*begin == "ANKI_RP")
  713. {
  714. relaxed = true;
  715. ++begin;
  716. }
  717. // Type
  718. if(begin == end)
  719. {
  720. ANKI_PP_ERROR_MALFORMED();
  721. }
  722. const CString typeStr = *begin;
  723. member.m_type = ShaderVariableDataType::NONE;
  724. if(typeStr == "F32")
  725. {
  726. member.m_type = ShaderVariableDataType::F32;
  727. }
  728. else if(typeStr == "Vec2")
  729. {
  730. member.m_type = ShaderVariableDataType::VEC2;
  731. }
  732. else if(typeStr == "Vec3")
  733. {
  734. member.m_type = ShaderVariableDataType::VEC3;
  735. }
  736. else if(typeStr == "Vec4")
  737. {
  738. member.m_type = ShaderVariableDataType::VEC4;
  739. }
  740. else if(typeStr == "U32")
  741. {
  742. member.m_type = ShaderVariableDataType::U32;
  743. }
  744. if(member.m_type == ShaderVariableDataType::NONE)
  745. {
  746. ANKI_PP_ERROR_MALFORMED_MSG("Unrecognized type");
  747. }
  748. ++begin;
  749. // Name
  750. if(begin == end)
  751. {
  752. ANKI_PP_ERROR_MALFORMED();
  753. }
  754. member.m_name.create(*begin);
  755. ++begin;
  756. // if MUTATOR_NAME is MUTATOR_VALUE
  757. if(begin != end)
  758. {
  759. // "if"
  760. if(*begin != "if")
  761. {
  762. ANKI_PP_ERROR_MALFORMED();
  763. }
  764. ++begin;
  765. // MUTATOR_NAME
  766. if(begin == end)
  767. {
  768. ANKI_PP_ERROR_MALFORMED();
  769. }
  770. const CString mutatorName = *begin;
  771. for(U32 i = 0; i < m_mutators.getSize(); ++i)
  772. {
  773. if(m_mutators[i].m_name == mutatorName)
  774. {
  775. member.m_dependentMutator = i;
  776. break;
  777. }
  778. }
  779. if(member.m_dependentMutator == MAX_U32)
  780. {
  781. ANKI_PP_ERROR_MALFORMED_MSG("Mutator not found");
  782. }
  783. ++begin;
  784. // "is"
  785. if(begin == end)
  786. {
  787. ANKI_PP_ERROR_MALFORMED();
  788. }
  789. if(*begin != "is")
  790. {
  791. ANKI_PP_ERROR_MALFORMED();
  792. }
  793. ++begin;
  794. // MUTATOR_VALUE
  795. if(begin == end)
  796. {
  797. ANKI_PP_ERROR_MALFORMED();
  798. }
  799. ANKI_CHECK(begin->toNumber(member.m_mutatorValue));
  800. if(!mutatorHasValue(m_mutators[member.m_dependentMutator], member.m_mutatorValue))
  801. {
  802. ANKI_PP_ERROR_MALFORMED_MSG("Wrong mutator value");
  803. }
  804. ++begin;
  805. }
  806. if(begin != end)
  807. {
  808. ANKI_PP_ERROR_MALFORMED();
  809. }
  810. // Code
  811. if(member.m_dependentMutator != MAX_U32)
  812. {
  813. m_codeLines.pushBackSprintf("#if %s == %d", m_mutators[member.m_dependentMutator].m_name.cstr(),
  814. member.m_mutatorValue);
  815. }
  816. m_codeLines.pushBackSprintf("#\tdefine %s_%s_DEFINED 1", m_ghostStructs.getBack().m_name.cstr(),
  817. member.m_name.cstr());
  818. m_codeLines.pushBackSprintf("\t%s %s %s;", (relaxed) ? "ANKI_RP" : "", typeStr.cstr(), member.m_name.cstr());
  819. if(member.m_dependentMutator != MAX_U32)
  820. {
  821. m_codeLines.pushBack("#endif");
  822. }
  823. return Error::NONE;
  824. }
  825. Error ShaderProgramParser::parsePragmaStructEnd(const StringAuto* begin, const StringAuto* end, CString line,
  826. CString fname)
  827. {
  828. ANKI_ASSERT(m_insideStruct);
  829. const U tokenCount = U(end - begin);
  830. if(tokenCount != 1)
  831. {
  832. ANKI_PP_ERROR_MALFORMED();
  833. }
  834. GhostStruct& gstruct = m_ghostStructs.getBack();
  835. const CString structName = gstruct.m_name;
  836. if(gstruct.m_members.isEmpty())
  837. {
  838. ANKI_PP_ERROR_MALFORMED_MSG("The struct doesn't have any members");
  839. }
  840. m_codeLines.pushBack("};");
  841. for(U32 i = 0; i < gstruct.m_members.getSize(); ++i)
  842. {
  843. const Member& m = gstruct.m_members[i];
  844. // #define XXX_OFFSETOF
  845. if(i == 0)
  846. {
  847. m_codeLines.pushBackSprintf("#define %s_%s_OFFSETOF 0u", gstruct.m_name.cstr(), m.m_name.cstr());
  848. }
  849. else
  850. {
  851. const Member& prev = gstruct.m_members[i - 1];
  852. m_codeLines.pushBackSprintf("#define %s_%s_OFFSETOF (%s_%s_OFFSETOF + %s_%s_SIZEOF)", structName.cstr(),
  853. m.m_name.cstr(), structName.cstr(), prev.m_name.cstr(), structName.cstr(),
  854. prev.m_name.cstr());
  855. }
  856. // #if XXX_DEFINED
  857. m_codeLines.pushBackSprintf("#if defined(%s_%s_DEFINED)", structName.cstr(), m.m_name.cstr());
  858. // # define XXX_SIZEOF
  859. m_codeLines.pushBackSprintf("#\tdefine %s_%s_SIZEOF %uu", structName.cstr(), m.m_name.cstr(),
  860. getShaderVariableDataTypeInfo(m.m_type).m_size / 4);
  861. // # define XXX_LOAD()
  862. const Bool isIntegral = getShaderVariableDataTypeInfo(m.m_type).m_isIntegral;
  863. const U32 componentCount = getShaderVariableDataTypeInfo(m.m_type).m_size / sizeof(U32);
  864. StringAuto values(m_alloc);
  865. for(U32 j = 0; j < componentCount; ++j)
  866. {
  867. StringAuto tmp(m_alloc);
  868. tmp.sprintf("%s(ssbo[%s_%s_OFFSETOF + offset + %uu])%s", (isIntegral) ? "" : "uintBitsToFloat",
  869. structName.cstr(), m.m_name.cstr(), j, (j != componentCount - 1) ? "," : "");
  870. values.append(tmp);
  871. }
  872. m_codeLines.pushBackSprintf("#\tdefine %s_%s_LOAD(ssbo, offset) %s(%s)%s", structName.cstr(), m.m_name.cstr(),
  873. getShaderVariableDataTypeInfo(m.m_type).m_name, values.cstr(),
  874. (i != gstruct.m_members.getSize() - 1) ? "," : "");
  875. // #else
  876. m_codeLines.pushBack("#else");
  877. // # define XXX_SIZEOF 0
  878. m_codeLines.pushBackSprintf("#\tdefine %s_%s_SIZEOF 0u", structName.cstr(), m.m_name.cstr());
  879. // # define XXX_LOAD()
  880. m_codeLines.pushBackSprintf("#\tdefine %s_%s_LOAD(ssbo, offset)", structName.cstr(), m.m_name.cstr());
  881. // #endif
  882. m_codeLines.pushBack("#endif");
  883. }
  884. // Now define the structure LOAD
  885. m_codeLines.pushBackSprintf("#define load%s(ssbo, offset) %s( \\", structName.cstr(), structName.cstr());
  886. for(U32 i = 0; i < gstruct.m_members.getSize(); ++i)
  887. {
  888. const Member& m = gstruct.m_members[i];
  889. m_codeLines.pushBackSprintf("\t%s_%s_LOAD(ssbo, offset) \\", structName.cstr(), m.m_name.cstr());
  890. }
  891. m_codeLines.pushBack(")");
  892. // Define the actual struct
  893. m_codeLines.pushBackSprintf("#define %s %s_", structName.cstr(), structName.cstr());
  894. m_insideStruct = false;
  895. return Error::NONE;
  896. }
  897. Error ShaderProgramParser::parseFile(CString fname, U32 depth)
  898. {
  899. // First check the depth
  900. if(depth > MAX_INCLUDE_DEPTH)
  901. {
  902. ANKI_SHADER_COMPILER_LOGE("The include depth is too high. Probably circular includance");
  903. }
  904. Bool foundPragmaOnce = false;
  905. // Load file in lines
  906. StringAuto txt(m_alloc);
  907. ANKI_CHECK(m_fsystem->readAllText(fname, txt));
  908. StringListAuto lines(m_alloc);
  909. lines.splitString(txt.toCString(), '\n');
  910. if(lines.getSize() < 1)
  911. {
  912. ANKI_SHADER_COMPILER_LOGE("Source is empty");
  913. }
  914. // Parse lines
  915. for(const String& line : lines)
  916. {
  917. if(line.find("pragma") != CString::NPOS || line.find("include") != CString::NPOS)
  918. {
  919. // Possibly a preprocessor directive we care
  920. ANKI_CHECK(parseLine(line.toCString(), fname, foundPragmaOnce, depth));
  921. }
  922. else
  923. {
  924. // Just append the line
  925. m_codeLines.pushBack(line.toCString());
  926. }
  927. }
  928. if(foundPragmaOnce)
  929. {
  930. // Append the guard
  931. m_codeLines.pushBack("#endif // Include guard");
  932. }
  933. return Error::NONE;
  934. }
  935. Error ShaderProgramParser::parse()
  936. {
  937. ANKI_ASSERT(!m_fname.isEmpty());
  938. ANKI_ASSERT(m_codeLines.isEmpty());
  939. const CString fname = m_fname;
  940. // Parse recursively
  941. ANKI_CHECK(parseFile(fname, 0));
  942. // Checks
  943. {
  944. if(!!(m_shaderTypes & ShaderTypeBit::COMPUTE))
  945. {
  946. if(m_shaderTypes != ShaderTypeBit::COMPUTE)
  947. {
  948. ANKI_SHADER_COMPILER_LOGE("Can't combine compute shader with other types of shaders");
  949. return Error::USER_DATA;
  950. }
  951. }
  952. else if(!!(m_shaderTypes & ShaderTypeBit::ALL_GRAPHICS))
  953. {
  954. if(!(m_shaderTypes & ShaderTypeBit::VERTEX))
  955. {
  956. ANKI_SHADER_COMPILER_LOGE("Missing vertex shader");
  957. return Error::USER_DATA;
  958. }
  959. if(!(m_shaderTypes & ShaderTypeBit::FRAGMENT))
  960. {
  961. ANKI_SHADER_COMPILER_LOGE("Missing fragment shader");
  962. return Error::USER_DATA;
  963. }
  964. }
  965. if(m_insideShader)
  966. {
  967. ANKI_SHADER_COMPILER_LOGE("Forgot a \"pragma anki end\"");
  968. return Error::USER_DATA;
  969. }
  970. }
  971. // Create the code lines
  972. if(m_codeLines.getSize())
  973. {
  974. m_codeLines.join("\n", m_codeSource);
  975. m_codeLines.destroy();
  976. }
  977. // Create the hash
  978. {
  979. if(m_codeSource.getLength())
  980. {
  981. m_codeSourceHash = appendHash(m_codeSource.getBegin(), m_codeSource.getLength(), SHADER_HEADER_HASH);
  982. }
  983. if(m_libName.getLength() > 0)
  984. {
  985. m_codeSourceHash = appendHash(m_libName.getBegin(), m_libName.getLength(), m_codeSourceHash);
  986. }
  987. m_codeSourceHash = appendHash(&m_rayType, sizeof(m_rayType), m_codeSourceHash);
  988. }
  989. return Error::NONE;
  990. }
  991. void ShaderProgramParser::generateAnkiShaderHeader(ShaderType shaderType, const ShaderCompilerOptions& compilerOptions,
  992. StringAuto& header)
  993. {
  994. header.sprintf(SHADER_HEADER, SHADER_STAGE_NAMES[shaderType].cstr(), compilerOptions.m_mobilePlatform,
  995. compilerOptions.m_forceFullFloatingPointPrecision, MAX_BINDLESS_TEXTURES, MAX_BINDLESS_IMAGES);
  996. }
  997. Error ShaderProgramParser::generateVariant(ConstWeakArray<MutatorValue> mutation,
  998. ShaderProgramParserVariant& variant) const
  999. {
  1000. // Sanity checks
  1001. ANKI_ASSERT(m_codeSource.getLength() > 0);
  1002. ANKI_ASSERT(mutation.getSize() == m_mutators.getSize());
  1003. for(U32 i = 0; i < mutation.getSize(); ++i)
  1004. {
  1005. ANKI_ASSERT(mutatorHasValue(m_mutators[i], mutation[i]) && "Value not found");
  1006. }
  1007. // Init variant
  1008. ::new(&variant) ShaderProgramParserVariant();
  1009. variant.m_alloc = m_alloc;
  1010. // Create the mutator defines
  1011. StringAuto mutatorDefines(m_alloc);
  1012. for(U32 i = 0; i < mutation.getSize(); ++i)
  1013. {
  1014. mutatorDefines.append(StringAuto(m_alloc).sprintf("#define %s %d\n", m_mutators[i].m_name.cstr(), mutation[i]));
  1015. }
  1016. // Generate souce per stage
  1017. for(ShaderType shaderType : EnumIterable<ShaderType>())
  1018. {
  1019. if(!(ShaderTypeBit(1u << shaderType) & m_shaderTypes))
  1020. {
  1021. continue;
  1022. }
  1023. // Create the header
  1024. StringAuto header(m_alloc);
  1025. generateAnkiShaderHeader(shaderType, m_compilerOptions, header);
  1026. // Create the final source without the bindings
  1027. StringAuto finalSource(m_alloc);
  1028. finalSource.append(header);
  1029. finalSource.append(mutatorDefines);
  1030. finalSource.append(m_codeSource);
  1031. // Move the source
  1032. variant.m_sources[shaderType] = std::move(finalSource);
  1033. }
  1034. return Error::NONE;
  1035. }
  1036. Bool ShaderProgramParser::mutatorHasValue(const ShaderProgramParserMutator& mutator, MutatorValue value)
  1037. {
  1038. for(MutatorValue v : mutator.m_values)
  1039. {
  1040. if(value == v)
  1041. {
  1042. return true;
  1043. }
  1044. }
  1045. return false;
  1046. }
  1047. Bool ShaderProgramParser::skipMutation(ConstWeakArray<MutatorValue> mutation) const
  1048. {
  1049. ANKI_ASSERT(mutation.getSize() == m_mutators.getSize());
  1050. for(const PartialMutationSkip& skip : m_skipMutations)
  1051. {
  1052. Bool doSkip = true;
  1053. for(U32 i = 0; i < m_mutators.getSize(); ++i)
  1054. {
  1055. if(skip.m_partialMutation[i] == std::numeric_limits<MutatorValue>::max())
  1056. {
  1057. // Don't care
  1058. continue;
  1059. }
  1060. if(skip.m_partialMutation[i] != mutation[i])
  1061. {
  1062. doSkip = false;
  1063. break;
  1064. }
  1065. }
  1066. if(doSkip)
  1067. {
  1068. return true;
  1069. }
  1070. }
  1071. return false;
  1072. }
  1073. } // end namespace anki