2
0

ShaderProgramParser.cpp 32 KB

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