shader_language.h 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114
  1. /*************************************************************************/
  2. /* shader_language.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef SHADER_LANGUAGE_H
  31. #define SHADER_LANGUAGE_H
  32. #include "core/object/script_language.h"
  33. #include "core/string/string_name.h"
  34. #include "core/string/ustring.h"
  35. #include "core/templates/list.h"
  36. #include "core/templates/rb_map.h"
  37. #include "core/typedefs.h"
  38. #include "core/variant/variant.h"
  39. #ifdef DEBUG_ENABLED
  40. #include "shader_warnings.h"
  41. #endif // DEBUG_ENABLED
  42. class ShaderLanguage {
  43. public:
  44. struct TkPos {
  45. int char_idx;
  46. int tk_line;
  47. };
  48. enum TokenType {
  49. TK_EMPTY,
  50. TK_IDENTIFIER,
  51. TK_TRUE,
  52. TK_FALSE,
  53. TK_FLOAT_CONSTANT,
  54. TK_INT_CONSTANT,
  55. TK_UINT_CONSTANT,
  56. TK_TYPE_VOID,
  57. TK_TYPE_BOOL,
  58. TK_TYPE_BVEC2,
  59. TK_TYPE_BVEC3,
  60. TK_TYPE_BVEC4,
  61. TK_TYPE_INT,
  62. TK_TYPE_IVEC2,
  63. TK_TYPE_IVEC3,
  64. TK_TYPE_IVEC4,
  65. TK_TYPE_UINT,
  66. TK_TYPE_UVEC2,
  67. TK_TYPE_UVEC3,
  68. TK_TYPE_UVEC4,
  69. TK_TYPE_FLOAT,
  70. TK_TYPE_VEC2,
  71. TK_TYPE_VEC3,
  72. TK_TYPE_VEC4,
  73. TK_TYPE_MAT2,
  74. TK_TYPE_MAT3,
  75. TK_TYPE_MAT4,
  76. TK_TYPE_SAMPLER2D,
  77. TK_TYPE_ISAMPLER2D,
  78. TK_TYPE_USAMPLER2D,
  79. TK_TYPE_SAMPLER2DARRAY,
  80. TK_TYPE_ISAMPLER2DARRAY,
  81. TK_TYPE_USAMPLER2DARRAY,
  82. TK_TYPE_SAMPLER3D,
  83. TK_TYPE_ISAMPLER3D,
  84. TK_TYPE_USAMPLER3D,
  85. TK_TYPE_SAMPLERCUBE,
  86. TK_TYPE_SAMPLERCUBEARRAY,
  87. TK_INTERPOLATION_FLAT,
  88. TK_INTERPOLATION_SMOOTH,
  89. TK_CONST,
  90. TK_STRUCT,
  91. TK_PRECISION_LOW,
  92. TK_PRECISION_MID,
  93. TK_PRECISION_HIGH,
  94. TK_OP_EQUAL,
  95. TK_OP_NOT_EQUAL,
  96. TK_OP_LESS,
  97. TK_OP_LESS_EQUAL,
  98. TK_OP_GREATER,
  99. TK_OP_GREATER_EQUAL,
  100. TK_OP_AND,
  101. TK_OP_OR,
  102. TK_OP_NOT,
  103. TK_OP_ADD,
  104. TK_OP_SUB,
  105. TK_OP_MUL,
  106. TK_OP_DIV,
  107. TK_OP_MOD,
  108. TK_OP_SHIFT_LEFT,
  109. TK_OP_SHIFT_RIGHT,
  110. TK_OP_ASSIGN,
  111. TK_OP_ASSIGN_ADD,
  112. TK_OP_ASSIGN_SUB,
  113. TK_OP_ASSIGN_MUL,
  114. TK_OP_ASSIGN_DIV,
  115. TK_OP_ASSIGN_MOD,
  116. TK_OP_ASSIGN_SHIFT_LEFT,
  117. TK_OP_ASSIGN_SHIFT_RIGHT,
  118. TK_OP_ASSIGN_BIT_AND,
  119. TK_OP_ASSIGN_BIT_OR,
  120. TK_OP_ASSIGN_BIT_XOR,
  121. TK_OP_BIT_AND,
  122. TK_OP_BIT_OR,
  123. TK_OP_BIT_XOR,
  124. TK_OP_BIT_INVERT,
  125. TK_OP_INCREMENT,
  126. TK_OP_DECREMENT,
  127. TK_CF_IF,
  128. TK_CF_ELSE,
  129. TK_CF_FOR,
  130. TK_CF_WHILE,
  131. TK_CF_DO,
  132. TK_CF_SWITCH,
  133. TK_CF_CASE,
  134. TK_CF_DEFAULT,
  135. TK_CF_BREAK,
  136. TK_CF_CONTINUE,
  137. TK_CF_RETURN,
  138. TK_CF_DISCARD,
  139. TK_BRACKET_OPEN,
  140. TK_BRACKET_CLOSE,
  141. TK_CURLY_BRACKET_OPEN,
  142. TK_CURLY_BRACKET_CLOSE,
  143. TK_PARENTHESIS_OPEN,
  144. TK_PARENTHESIS_CLOSE,
  145. TK_QUESTION,
  146. TK_COMMA,
  147. TK_COLON,
  148. TK_SEMICOLON,
  149. TK_PERIOD,
  150. TK_UNIFORM,
  151. TK_INSTANCE,
  152. TK_GLOBAL,
  153. TK_VARYING,
  154. TK_ARG_IN,
  155. TK_ARG_OUT,
  156. TK_ARG_INOUT,
  157. TK_RENDER_MODE,
  158. TK_HINT_DEFAULT_WHITE_TEXTURE,
  159. TK_HINT_DEFAULT_BLACK_TEXTURE,
  160. TK_HINT_NORMAL_TEXTURE,
  161. TK_HINT_ROUGHNESS_NORMAL_TEXTURE,
  162. TK_HINT_ROUGHNESS_R,
  163. TK_HINT_ROUGHNESS_G,
  164. TK_HINT_ROUGHNESS_B,
  165. TK_HINT_ROUGHNESS_A,
  166. TK_HINT_ROUGHNESS_GRAY,
  167. TK_HINT_ANISOTROPY_TEXTURE,
  168. TK_HINT_SOURCE_COLOR,
  169. TK_HINT_RANGE,
  170. TK_HINT_INSTANCE_INDEX,
  171. TK_FILTER_NEAREST,
  172. TK_FILTER_LINEAR,
  173. TK_FILTER_NEAREST_MIPMAP,
  174. TK_FILTER_LINEAR_MIPMAP,
  175. TK_FILTER_NEAREST_MIPMAP_ANISOTROPIC,
  176. TK_FILTER_LINEAR_MIPMAP_ANISOTROPIC,
  177. TK_REPEAT_ENABLE,
  178. TK_REPEAT_DISABLE,
  179. TK_SHADER_TYPE,
  180. TK_CURSOR,
  181. TK_ERROR,
  182. TK_EOF,
  183. TK_MAX
  184. };
  185. /* COMPILER */
  186. // lame work around to Apple defining this as a macro in 10.12 SDK
  187. #ifdef TYPE_BOOL
  188. #undef TYPE_BOOL
  189. #endif
  190. enum DataType {
  191. TYPE_VOID,
  192. TYPE_BOOL,
  193. TYPE_BVEC2,
  194. TYPE_BVEC3,
  195. TYPE_BVEC4,
  196. TYPE_INT,
  197. TYPE_IVEC2,
  198. TYPE_IVEC3,
  199. TYPE_IVEC4,
  200. TYPE_UINT,
  201. TYPE_UVEC2,
  202. TYPE_UVEC3,
  203. TYPE_UVEC4,
  204. TYPE_FLOAT,
  205. TYPE_VEC2,
  206. TYPE_VEC3,
  207. TYPE_VEC4,
  208. TYPE_MAT2,
  209. TYPE_MAT3,
  210. TYPE_MAT4,
  211. TYPE_SAMPLER2D,
  212. TYPE_ISAMPLER2D,
  213. TYPE_USAMPLER2D,
  214. TYPE_SAMPLER2DARRAY,
  215. TYPE_ISAMPLER2DARRAY,
  216. TYPE_USAMPLER2DARRAY,
  217. TYPE_SAMPLER3D,
  218. TYPE_ISAMPLER3D,
  219. TYPE_USAMPLER3D,
  220. TYPE_SAMPLERCUBE,
  221. TYPE_SAMPLERCUBEARRAY,
  222. TYPE_STRUCT,
  223. TYPE_MAX
  224. };
  225. enum DataPrecision {
  226. PRECISION_LOWP,
  227. PRECISION_MEDIUMP,
  228. PRECISION_HIGHP,
  229. PRECISION_DEFAULT,
  230. };
  231. enum DataInterpolation {
  232. INTERPOLATION_FLAT,
  233. INTERPOLATION_SMOOTH,
  234. };
  235. enum Operator {
  236. OP_EQUAL,
  237. OP_NOT_EQUAL,
  238. OP_LESS,
  239. OP_LESS_EQUAL,
  240. OP_GREATER,
  241. OP_GREATER_EQUAL,
  242. OP_AND,
  243. OP_OR,
  244. OP_NOT,
  245. OP_NEGATE,
  246. OP_ADD,
  247. OP_SUB,
  248. OP_MUL,
  249. OP_DIV,
  250. OP_MOD,
  251. OP_SHIFT_LEFT,
  252. OP_SHIFT_RIGHT,
  253. OP_ASSIGN,
  254. OP_ASSIGN_ADD,
  255. OP_ASSIGN_SUB,
  256. OP_ASSIGN_MUL,
  257. OP_ASSIGN_DIV,
  258. OP_ASSIGN_MOD,
  259. OP_ASSIGN_SHIFT_LEFT,
  260. OP_ASSIGN_SHIFT_RIGHT,
  261. OP_ASSIGN_BIT_AND,
  262. OP_ASSIGN_BIT_OR,
  263. OP_ASSIGN_BIT_XOR,
  264. OP_BIT_AND,
  265. OP_BIT_OR,
  266. OP_BIT_XOR,
  267. OP_BIT_INVERT,
  268. OP_INCREMENT,
  269. OP_DECREMENT,
  270. OP_SELECT_IF,
  271. OP_SELECT_ELSE, //used only internally, then only IF appears with 3 arguments
  272. OP_POST_INCREMENT,
  273. OP_POST_DECREMENT,
  274. OP_CALL,
  275. OP_CONSTRUCT,
  276. OP_STRUCT,
  277. OP_INDEX,
  278. OP_EMPTY,
  279. OP_MAX
  280. };
  281. enum FlowOperation {
  282. FLOW_OP_IF,
  283. FLOW_OP_RETURN,
  284. FLOW_OP_FOR,
  285. FLOW_OP_WHILE,
  286. FLOW_OP_DO,
  287. FLOW_OP_BREAK,
  288. FLOW_OP_SWITCH,
  289. FLOW_OP_CASE,
  290. FLOW_OP_DEFAULT,
  291. FLOW_OP_CONTINUE,
  292. FLOW_OP_DISCARD
  293. };
  294. enum ArgumentQualifier {
  295. ARGUMENT_QUALIFIER_IN,
  296. ARGUMENT_QUALIFIER_OUT,
  297. ARGUMENT_QUALIFIER_INOUT,
  298. };
  299. enum SubClassTag {
  300. TAG_GLOBAL,
  301. TAG_ARRAY,
  302. };
  303. enum TextureFilter {
  304. FILTER_NEAREST,
  305. FILTER_LINEAR,
  306. FILTER_NEAREST_MIPMAP,
  307. FILTER_LINEAR_MIPMAP,
  308. FILTER_NEAREST_MIPMAP_ANISOTROPIC,
  309. FILTER_LINEAR_MIPMAP_ANISOTROPIC,
  310. FILTER_DEFAULT,
  311. };
  312. enum TextureRepeat {
  313. REPEAT_DISABLE,
  314. REPEAT_ENABLE,
  315. REPEAT_DEFAULT,
  316. };
  317. enum {
  318. MAX_INSTANCE_UNIFORM_INDICES = 16
  319. };
  320. struct VaryingFunctionNames {
  321. StringName fragment;
  322. StringName vertex;
  323. StringName light;
  324. VaryingFunctionNames() {
  325. fragment = "fragment";
  326. vertex = "vertex";
  327. light = "light";
  328. }
  329. };
  330. struct Node {
  331. Node *next = nullptr;
  332. enum Type {
  333. TYPE_SHADER,
  334. TYPE_FUNCTION,
  335. TYPE_BLOCK,
  336. TYPE_VARIABLE,
  337. TYPE_VARIABLE_DECLARATION,
  338. TYPE_CONSTANT,
  339. TYPE_OPERATOR,
  340. TYPE_CONTROL_FLOW,
  341. TYPE_MEMBER,
  342. TYPE_ARRAY,
  343. TYPE_ARRAY_CONSTRUCT,
  344. TYPE_STRUCT,
  345. };
  346. Type type;
  347. virtual DataType get_datatype() const { return TYPE_VOID; }
  348. virtual String get_datatype_name() const { return ""; }
  349. virtual int get_array_size() const { return 0; }
  350. virtual bool is_indexed() const { return false; }
  351. Node(Type t) :
  352. type(t) {}
  353. virtual ~Node() {}
  354. };
  355. template <class T>
  356. T *alloc_node() {
  357. T *node = memnew(T);
  358. node->next = nodes;
  359. nodes = node;
  360. return node;
  361. }
  362. Node *nodes = nullptr;
  363. struct OperatorNode : public Node {
  364. DataType return_cache = TYPE_VOID;
  365. DataPrecision return_precision_cache = PRECISION_DEFAULT;
  366. int return_array_size = 0;
  367. Operator op = OP_EQUAL;
  368. StringName struct_name;
  369. Vector<Node *> arguments;
  370. virtual DataType get_datatype() const override { return return_cache; }
  371. virtual String get_datatype_name() const override { return String(struct_name); }
  372. virtual int get_array_size() const override { return return_array_size; }
  373. virtual bool is_indexed() const override { return op == OP_INDEX; }
  374. OperatorNode() :
  375. Node(TYPE_OPERATOR) {}
  376. };
  377. struct VariableNode : public Node {
  378. DataType datatype_cache = TYPE_VOID;
  379. StringName name;
  380. StringName struct_name;
  381. bool is_const = false;
  382. bool is_local = false;
  383. virtual DataType get_datatype() const override { return datatype_cache; }
  384. virtual String get_datatype_name() const override { return String(struct_name); }
  385. VariableNode() :
  386. Node(TYPE_VARIABLE) {}
  387. };
  388. struct VariableDeclarationNode : public Node {
  389. DataPrecision precision = PRECISION_DEFAULT;
  390. DataType datatype = TYPE_VOID;
  391. String struct_name;
  392. bool is_const = false;
  393. struct Declaration {
  394. StringName name;
  395. uint32_t size = 0U;
  396. Node *size_expression = nullptr;
  397. Vector<Node *> initializer;
  398. bool single_expression = false;
  399. };
  400. Vector<Declaration> declarations;
  401. virtual DataType get_datatype() const override { return datatype; }
  402. VariableDeclarationNode() :
  403. Node(TYPE_VARIABLE_DECLARATION) {}
  404. };
  405. struct ArrayNode : public Node {
  406. DataType datatype_cache = TYPE_VOID;
  407. StringName struct_name;
  408. StringName name;
  409. Node *index_expression = nullptr;
  410. Node *call_expression = nullptr;
  411. Node *assign_expression = nullptr;
  412. bool is_const = false;
  413. int array_size = 0;
  414. bool is_local = false;
  415. virtual DataType get_datatype() const override { return call_expression ? call_expression->get_datatype() : datatype_cache; }
  416. virtual String get_datatype_name() const override { return call_expression ? call_expression->get_datatype_name() : String(struct_name); }
  417. virtual int get_array_size() const override { return (index_expression || call_expression) ? 0 : array_size; }
  418. virtual bool is_indexed() const override { return index_expression != nullptr; }
  419. ArrayNode() :
  420. Node(TYPE_ARRAY) {}
  421. };
  422. struct ArrayConstructNode : public Node {
  423. DataType datatype = TYPE_VOID;
  424. String struct_name;
  425. Vector<Node *> initializer;
  426. virtual DataType get_datatype() const override { return datatype; }
  427. virtual String get_datatype_name() const override { return struct_name; }
  428. virtual int get_array_size() const override { return initializer.size(); }
  429. ArrayConstructNode() :
  430. Node(TYPE_ARRAY_CONSTRUCT) {}
  431. };
  432. struct ConstantNode : public Node {
  433. DataType datatype = TYPE_VOID;
  434. String struct_name = "";
  435. int array_size = 0;
  436. union Value {
  437. bool boolean;
  438. float real;
  439. int32_t sint;
  440. uint32_t uint;
  441. };
  442. Vector<Value> values;
  443. Vector<VariableDeclarationNode::Declaration> array_declarations;
  444. virtual DataType get_datatype() const override { return datatype; }
  445. virtual String get_datatype_name() const override { return struct_name; }
  446. virtual int get_array_size() const override { return array_size; }
  447. ConstantNode() :
  448. Node(TYPE_CONSTANT) {}
  449. };
  450. struct FunctionNode;
  451. struct BlockNode : public Node {
  452. FunctionNode *parent_function = nullptr;
  453. BlockNode *parent_block = nullptr;
  454. enum BlockType {
  455. BLOCK_TYPE_STANDARD,
  456. BLOCK_TYPE_FOR_INIT,
  457. BLOCK_TYPE_FOR_CONDITION,
  458. BLOCK_TYPE_FOR_EXPRESSION,
  459. BLOCK_TYPE_SWITCH,
  460. BLOCK_TYPE_CASE,
  461. BLOCK_TYPE_DEFAULT,
  462. };
  463. int block_type = BLOCK_TYPE_STANDARD;
  464. SubClassTag block_tag = SubClassTag::TAG_GLOBAL;
  465. struct Variable {
  466. DataType type;
  467. StringName struct_name;
  468. DataPrecision precision;
  469. int line; //for completion
  470. int array_size;
  471. bool is_const;
  472. ConstantNode::Value value;
  473. };
  474. HashMap<StringName, Variable> variables;
  475. List<Node *> statements;
  476. bool single_statement = false;
  477. bool use_comma_between_statements = false;
  478. BlockNode() :
  479. Node(TYPE_BLOCK) {}
  480. };
  481. struct ControlFlowNode : public Node {
  482. FlowOperation flow_op = FLOW_OP_IF;
  483. Vector<Node *> expressions;
  484. Vector<BlockNode *> blocks;
  485. ControlFlowNode() :
  486. Node(TYPE_CONTROL_FLOW) {}
  487. };
  488. struct MemberNode : public Node {
  489. DataType basetype = TYPE_VOID;
  490. bool basetype_const = false;
  491. StringName base_struct_name;
  492. DataPrecision precision = PRECISION_DEFAULT;
  493. DataType datatype = TYPE_VOID;
  494. int array_size = 0;
  495. StringName struct_name;
  496. StringName name;
  497. Node *owner = nullptr;
  498. Node *index_expression = nullptr;
  499. Node *assign_expression = nullptr;
  500. Node *call_expression = nullptr;
  501. bool has_swizzling_duplicates = false;
  502. virtual DataType get_datatype() const override { return call_expression ? call_expression->get_datatype() : datatype; }
  503. virtual String get_datatype_name() const override { return call_expression ? call_expression->get_datatype_name() : String(struct_name); }
  504. virtual int get_array_size() const override { return (index_expression || call_expression) ? 0 : array_size; }
  505. virtual bool is_indexed() const override { return index_expression != nullptr || call_expression != nullptr; }
  506. MemberNode() :
  507. Node(TYPE_MEMBER) {}
  508. };
  509. struct StructNode : public Node {
  510. List<MemberNode *> members;
  511. StructNode() :
  512. Node(TYPE_STRUCT) {}
  513. };
  514. struct FunctionNode : public Node {
  515. struct Argument {
  516. ArgumentQualifier qualifier;
  517. StringName name;
  518. DataType type;
  519. StringName type_str;
  520. DataPrecision precision;
  521. //for passing textures as arguments
  522. bool tex_argument_check;
  523. TextureFilter tex_argument_filter;
  524. TextureRepeat tex_argument_repeat;
  525. bool tex_builtin_check;
  526. StringName tex_builtin;
  527. bool is_const;
  528. int array_size;
  529. HashMap<StringName, HashSet<int>> tex_argument_connect;
  530. };
  531. StringName name;
  532. DataType return_type = TYPE_VOID;
  533. StringName return_struct_name;
  534. DataPrecision return_precision = PRECISION_DEFAULT;
  535. int return_array_size = 0;
  536. Vector<Argument> arguments;
  537. BlockNode *body = nullptr;
  538. bool can_discard = false;
  539. virtual DataType get_datatype() const override { return return_type; }
  540. virtual String get_datatype_name() const override { return String(return_struct_name); }
  541. virtual int get_array_size() const override { return return_array_size; }
  542. FunctionNode() :
  543. Node(TYPE_FUNCTION) {}
  544. };
  545. struct ShaderNode : public Node {
  546. struct Constant {
  547. StringName name;
  548. DataType type;
  549. StringName type_str;
  550. DataPrecision precision;
  551. ConstantNode *initializer = nullptr;
  552. int array_size;
  553. };
  554. struct Function {
  555. StringName name;
  556. FunctionNode *function = nullptr;
  557. HashSet<StringName> uses_function;
  558. bool callable;
  559. };
  560. struct Struct {
  561. StringName name;
  562. StructNode *shader_struct = nullptr;
  563. };
  564. struct Varying {
  565. enum Stage {
  566. STAGE_UNKNOWN,
  567. STAGE_VERTEX, // transition stage to STAGE_VERTEX_TO_FRAGMENT_LIGHT, emits warning if it's not used
  568. STAGE_FRAGMENT, // transition stage to STAGE_FRAGMENT_TO_LIGHT, emits warning if it's not used
  569. STAGE_VERTEX_TO_FRAGMENT_LIGHT,
  570. STAGE_FRAGMENT_TO_LIGHT,
  571. };
  572. Stage stage = STAGE_UNKNOWN;
  573. DataType type = TYPE_VOID;
  574. DataInterpolation interpolation = INTERPOLATION_FLAT;
  575. DataPrecision precision = PRECISION_DEFAULT;
  576. int array_size = 0;
  577. TkPos tkpos;
  578. Varying() {}
  579. };
  580. struct Uniform {
  581. enum Hint {
  582. HINT_NONE,
  583. HINT_RANGE,
  584. HINT_SOURCE_COLOR,
  585. HINT_NORMAL,
  586. HINT_ROUGHNESS_NORMAL,
  587. HINT_ROUGHNESS_R,
  588. HINT_ROUGHNESS_G,
  589. HINT_ROUGHNESS_B,
  590. HINT_ROUGHNESS_A,
  591. HINT_ROUGHNESS_GRAY,
  592. HINT_DEFAULT_BLACK,
  593. HINT_DEFAULT_WHITE,
  594. HINT_ANISOTROPY,
  595. HINT_MAX
  596. };
  597. enum Scope {
  598. SCOPE_LOCAL,
  599. SCOPE_INSTANCE,
  600. SCOPE_GLOBAL,
  601. };
  602. int order = 0;
  603. int texture_order = 0;
  604. int texture_binding = 0;
  605. DataType type = TYPE_VOID;
  606. DataPrecision precision = PRECISION_DEFAULT;
  607. int array_size = 0;
  608. Vector<ConstantNode::Value> default_value;
  609. Scope scope = SCOPE_LOCAL;
  610. Hint hint = HINT_NONE;
  611. TextureFilter filter = FILTER_DEFAULT;
  612. TextureRepeat repeat = REPEAT_DEFAULT;
  613. float hint_range[3];
  614. int instance_index = 0;
  615. Uniform() {
  616. hint_range[0] = 0.0f;
  617. hint_range[1] = 1.0f;
  618. hint_range[2] = 0.001f;
  619. }
  620. };
  621. HashMap<StringName, Constant> constants;
  622. HashMap<StringName, Varying> varyings;
  623. HashMap<StringName, Uniform> uniforms;
  624. HashMap<StringName, Struct> structs;
  625. Vector<StringName> render_modes;
  626. Vector<Function> functions;
  627. Vector<Constant> vconstants;
  628. Vector<Struct> vstructs;
  629. ShaderNode() :
  630. Node(TYPE_SHADER) {}
  631. };
  632. struct Expression {
  633. bool is_op;
  634. union {
  635. Operator op;
  636. Node *node = nullptr;
  637. };
  638. };
  639. struct VarInfo {
  640. StringName name;
  641. DataType type;
  642. };
  643. enum CompletionType {
  644. COMPLETION_NONE,
  645. COMPLETION_SHADER_TYPE,
  646. COMPLETION_RENDER_MODE,
  647. COMPLETION_MAIN_FUNCTION,
  648. COMPLETION_IDENTIFIER,
  649. COMPLETION_FUNCTION_CALL,
  650. COMPLETION_CALL_ARGUMENTS,
  651. COMPLETION_INDEX,
  652. COMPLETION_STRUCT,
  653. COMPLETION_HINT,
  654. };
  655. struct Token {
  656. TokenType type;
  657. StringName text;
  658. double constant;
  659. uint16_t line;
  660. bool is_integer_constant() const {
  661. return type == TK_INT_CONSTANT || type == TK_UINT_CONSTANT;
  662. }
  663. };
  664. static String get_operator_text(Operator p_op);
  665. static String get_token_text(Token p_token);
  666. static bool is_token_datatype(TokenType p_type);
  667. static bool is_token_variable_datatype(TokenType p_type);
  668. static DataType get_token_datatype(TokenType p_type);
  669. static bool is_token_interpolation(TokenType p_type);
  670. static DataInterpolation get_token_interpolation(TokenType p_type);
  671. static bool is_token_precision(TokenType p_type);
  672. static bool is_token_arg_qual(TokenType p_type);
  673. static DataPrecision get_token_precision(TokenType p_type);
  674. static String get_precision_name(DataPrecision p_type);
  675. static String get_datatype_name(DataType p_type);
  676. static bool is_token_nonvoid_datatype(TokenType p_type);
  677. static bool is_token_operator(TokenType p_type);
  678. static bool is_token_operator_assign(TokenType p_type);
  679. static bool is_token_hint(TokenType p_type);
  680. static bool convert_constant(ConstantNode *p_constant, DataType p_to_type, ConstantNode::Value *p_value = nullptr);
  681. static DataType get_scalar_type(DataType p_type);
  682. static int get_cardinality(DataType p_type);
  683. static bool is_scalar_type(DataType p_type);
  684. static bool is_float_type(DataType p_type);
  685. static bool is_sampler_type(DataType p_type);
  686. static Variant constant_value_to_variant(const Vector<ShaderLanguage::ConstantNode::Value> &p_value, DataType p_type, int p_array_size, ShaderLanguage::ShaderNode::Uniform::Hint p_hint = ShaderLanguage::ShaderNode::Uniform::HINT_NONE);
  687. static PropertyInfo uniform_to_property_info(const ShaderNode::Uniform &p_uniform);
  688. static uint32_t get_datatype_size(DataType p_type);
  689. static void get_keyword_list(List<String> *r_keywords);
  690. static bool is_control_flow_keyword(String p_keyword);
  691. static void get_builtin_funcs(List<String> *r_keywords);
  692. struct BuiltInInfo {
  693. DataType type = TYPE_VOID;
  694. bool constant = false;
  695. BuiltInInfo() {}
  696. BuiltInInfo(DataType p_type, bool p_constant = false) :
  697. type(p_type),
  698. constant(p_constant) {}
  699. };
  700. struct StageFunctionInfo {
  701. struct Argument {
  702. StringName name;
  703. DataType type;
  704. Argument(const StringName &p_name = StringName(), DataType p_type = TYPE_VOID) {
  705. name = p_name;
  706. type = p_type;
  707. }
  708. };
  709. Vector<Argument> arguments;
  710. DataType return_type = TYPE_VOID;
  711. };
  712. struct ModeInfo {
  713. StringName name;
  714. Vector<StringName> options;
  715. ModeInfo() {}
  716. ModeInfo(const StringName &p_name) :
  717. name(p_name) {
  718. }
  719. ModeInfo(const StringName &p_name, const StringName &p_arg1, const StringName &p_arg2) :
  720. name(p_name) {
  721. options.push_back(p_arg1);
  722. options.push_back(p_arg2);
  723. }
  724. ModeInfo(const StringName &p_name, const StringName &p_arg1, const StringName &p_arg2, const StringName &p_arg3) :
  725. name(p_name) {
  726. options.push_back(p_arg1);
  727. options.push_back(p_arg2);
  728. options.push_back(p_arg3);
  729. }
  730. ModeInfo(const StringName &p_name, const StringName &p_arg1, const StringName &p_arg2, const StringName &p_arg3, const StringName &p_arg4) :
  731. name(p_name) {
  732. options.push_back(p_arg1);
  733. options.push_back(p_arg2);
  734. options.push_back(p_arg3);
  735. options.push_back(p_arg4);
  736. }
  737. ModeInfo(const StringName &p_name, const StringName &p_arg1, const StringName &p_arg2, const StringName &p_arg3, const StringName &p_arg4, const StringName &p_arg5) :
  738. name(p_name) {
  739. options.push_back(p_arg1);
  740. options.push_back(p_arg2);
  741. options.push_back(p_arg3);
  742. options.push_back(p_arg4);
  743. options.push_back(p_arg5);
  744. }
  745. ModeInfo(const StringName &p_name, const StringName &p_arg1, const StringName &p_arg2, const StringName &p_arg3, const StringName &p_arg4, const StringName &p_arg5, const StringName &p_arg6) :
  746. name(p_name) {
  747. options.push_back(p_arg1);
  748. options.push_back(p_arg2);
  749. options.push_back(p_arg3);
  750. options.push_back(p_arg4);
  751. options.push_back(p_arg5);
  752. options.push_back(p_arg6);
  753. }
  754. };
  755. struct FunctionInfo {
  756. HashMap<StringName, BuiltInInfo> built_ins;
  757. HashMap<StringName, StageFunctionInfo> stage_functions;
  758. bool can_discard = false;
  759. bool main_function = false;
  760. };
  761. static bool has_builtin(const HashMap<StringName, ShaderLanguage::FunctionInfo> &p_functions, const StringName &p_name);
  762. typedef DataType (*GlobalVariableGetTypeFunc)(const StringName &p_name);
  763. private:
  764. struct KeyWord {
  765. TokenType token;
  766. const char *text;
  767. uint32_t flags;
  768. const Vector<String> excluded_shader_types;
  769. const Vector<String> functions;
  770. };
  771. static const KeyWord keyword_list[];
  772. GlobalVariableGetTypeFunc global_var_get_type_func = nullptr;
  773. bool error_set = false;
  774. String error_str;
  775. int error_line = 0;
  776. #ifdef DEBUG_ENABLED
  777. struct Usage {
  778. int decl_line;
  779. bool used = false;
  780. Usage(int p_decl_line = -1) {
  781. decl_line = p_decl_line;
  782. }
  783. };
  784. HashMap<StringName, Usage> used_constants;
  785. HashMap<StringName, Usage> used_varyings;
  786. HashMap<StringName, Usage> used_uniforms;
  787. HashMap<StringName, Usage> used_functions;
  788. HashMap<StringName, Usage> used_structs;
  789. HashMap<ShaderWarning::Code, HashMap<StringName, Usage> *> warnings_check_map;
  790. HashMap<StringName, HashMap<StringName, Usage>> used_local_vars;
  791. HashMap<ShaderWarning::Code, HashMap<StringName, HashMap<StringName, Usage>> *> warnings_check_map2;
  792. List<ShaderWarning> warnings;
  793. bool check_warnings = false;
  794. uint32_t warning_flags = 0;
  795. void _add_line_warning(ShaderWarning::Code p_code, const StringName &p_subject = "", const Vector<Variant> &p_extra_args = Vector<Variant>()) {
  796. warnings.push_back(ShaderWarning(p_code, tk_line, p_subject, p_extra_args));
  797. }
  798. void _add_global_warning(ShaderWarning::Code p_code, const StringName &p_subject = "", const Vector<Variant> &p_extra_args = Vector<Variant>()) {
  799. warnings.push_back(ShaderWarning(p_code, -1, p_subject, p_extra_args));
  800. }
  801. void _add_warning(ShaderWarning::Code p_code, int p_line, const StringName &p_subject = "", const Vector<Variant> &p_extra_args = Vector<Variant>()) {
  802. warnings.push_back(ShaderWarning(p_code, p_line, p_subject, p_extra_args));
  803. }
  804. void _check_warning_accums();
  805. #endif // DEBUG_ENABLED
  806. String code;
  807. int char_idx = 0;
  808. int tk_line = 0;
  809. StringName shader_type_identifier;
  810. StringName current_function;
  811. bool last_const = false;
  812. StringName last_name;
  813. VaryingFunctionNames varying_function_names;
  814. TkPos _get_tkpos() {
  815. TkPos tkp;
  816. tkp.char_idx = char_idx;
  817. tkp.tk_line = tk_line;
  818. return tkp;
  819. }
  820. void _set_tkpos(TkPos p_pos) {
  821. char_idx = p_pos.char_idx;
  822. tk_line = p_pos.tk_line;
  823. }
  824. void _set_error(const String &p_str) {
  825. if (error_set) {
  826. return;
  827. }
  828. error_line = tk_line;
  829. error_set = true;
  830. error_str = p_str;
  831. }
  832. void _set_expected_error(const String &p_what) {
  833. _set_error(vformat(RTR("Expected a '%s'."), p_what));
  834. }
  835. void _set_expected_error(const String &p_first, const String p_second) {
  836. _set_error(vformat(RTR("Expected a '%s' or '%s'."), p_first, p_second));
  837. }
  838. void _set_expected_after_error(const String &p_what, const String &p_after) {
  839. _set_error(vformat(RTR("Expected a '%s' after '%s'."), p_what, p_after));
  840. }
  841. void _set_redefinition_error(const String &p_what) {
  842. _set_error(vformat(RTR("Redefinition of '%s'."), p_what));
  843. }
  844. void _set_parsing_error() {
  845. _set_error("Parser bug.");
  846. }
  847. static const char *token_names[TK_MAX];
  848. Token _make_token(TokenType p_type, const StringName &p_text = StringName());
  849. Token _get_token();
  850. bool _lookup_next(Token &r_tk);
  851. ShaderNode *shader = nullptr;
  852. enum IdentifierType {
  853. IDENTIFIER_FUNCTION,
  854. IDENTIFIER_UNIFORM,
  855. IDENTIFIER_VARYING,
  856. IDENTIFIER_FUNCTION_ARGUMENT,
  857. IDENTIFIER_LOCAL_VAR,
  858. IDENTIFIER_BUILTIN_VAR,
  859. IDENTIFIER_CONSTANT,
  860. IDENTIFIER_MAX,
  861. };
  862. IdentifierType last_type = IDENTIFIER_MAX;
  863. bool _find_identifier(const BlockNode *p_block, bool p_allow_reassign, const FunctionInfo &p_function_info, const StringName &p_identifier, DataType *r_data_type = nullptr, IdentifierType *r_type = nullptr, bool *r_is_const = nullptr, int *r_array_size = nullptr, StringName *r_struct_name = nullptr, ConstantNode::Value *r_constant_value = nullptr);
  864. #ifdef DEBUG_ENABLED
  865. void _parse_used_identifier(const StringName &p_identifier, IdentifierType p_type, const StringName &p_function);
  866. #endif // DEBUG_ENABLED
  867. bool _is_operator_assign(Operator p_op) const;
  868. bool _validate_assign(Node *p_node, const FunctionInfo &p_function_info, String *r_message = nullptr);
  869. bool _validate_operator(OperatorNode *p_op, DataType *r_ret_type = nullptr, int *r_ret_size = nullptr);
  870. struct BuiltinFuncDef {
  871. enum { MAX_ARGS = 5 };
  872. const char *name;
  873. DataType rettype;
  874. const DataType args[MAX_ARGS];
  875. const char *args_names[MAX_ARGS];
  876. SubClassTag tag;
  877. bool high_end;
  878. };
  879. struct BuiltinFuncOutArgs { //arguments used as out in built in functions
  880. enum { MAX_ARGS = 2 };
  881. const char *name;
  882. const int arguments[MAX_ARGS];
  883. };
  884. struct BuiltinFuncConstArgs {
  885. const char *name;
  886. int arg;
  887. int min;
  888. int max;
  889. };
  890. CompletionType completion_type;
  891. int completion_line = 0;
  892. BlockNode *completion_block = nullptr;
  893. DataType completion_base;
  894. bool completion_base_array = false;
  895. SubClassTag completion_class;
  896. StringName completion_function;
  897. StringName completion_struct;
  898. int completion_argument = 0;
  899. #ifdef DEBUG_ENABLED
  900. uint32_t keyword_completion_context;
  901. #endif // DEBUG_ENABLED
  902. const HashMap<StringName, FunctionInfo> *stages = nullptr;
  903. bool _get_completable_identifier(BlockNode *p_block, CompletionType p_type, StringName &identifier);
  904. static const BuiltinFuncDef builtin_func_defs[];
  905. static const BuiltinFuncOutArgs builtin_func_out_args[];
  906. static const BuiltinFuncConstArgs builtin_func_const_args[];
  907. static bool is_const_suffix_lut_initialized;
  908. Error _validate_precision(DataType p_type, DataPrecision p_precision);
  909. Error _validate_datatype(DataType p_type);
  910. bool _compare_datatypes(DataType p_datatype_a, String p_datatype_name_a, int p_array_size_a, DataType p_datatype_b, String p_datatype_name_b, int p_array_size_b);
  911. bool _compare_datatypes_in_nodes(Node *a, Node *b);
  912. bool _validate_function_call(BlockNode *p_block, const FunctionInfo &p_function_info, OperatorNode *p_func, DataType *r_ret_type, StringName *r_ret_type_str);
  913. bool _parse_function_arguments(BlockNode *p_block, const FunctionInfo &p_function_info, OperatorNode *p_func, int *r_complete_arg = nullptr);
  914. bool _propagate_function_call_sampler_uniform_settings(StringName p_name, int p_argument, TextureFilter p_filter, TextureRepeat p_repeat);
  915. bool _propagate_function_call_sampler_builtin_reference(StringName p_name, int p_argument, const StringName &p_builtin);
  916. bool _validate_varying_assign(ShaderNode::Varying &p_varying, String *r_message);
  917. bool _check_node_constness(const Node *p_node) const;
  918. Node *_parse_expression(BlockNode *p_block, const FunctionInfo &p_function_info);
  919. Error _parse_array_size(BlockNode *p_block, const FunctionInfo &p_function_info, bool p_forbid_unknown_size, Node **r_size_expression, int *r_array_size, bool *r_unknown_size);
  920. Node *_parse_array_constructor(BlockNode *p_block, const FunctionInfo &p_function_info);
  921. Node *_parse_array_constructor(BlockNode *p_block, const FunctionInfo &p_function_info, DataType p_type, const StringName &p_struct_name, int p_array_size);
  922. ShaderLanguage::Node *_reduce_expression(BlockNode *p_block, ShaderLanguage::Node *p_node);
  923. Node *_parse_and_reduce_expression(BlockNode *p_block, const FunctionInfo &p_function_info);
  924. Error _parse_block(BlockNode *p_block, const FunctionInfo &p_function_info, bool p_just_one = false, bool p_can_break = false, bool p_can_continue = false);
  925. String _get_shader_type_list(const HashSet<String> &p_shader_types) const;
  926. String _get_qualifier_str(ArgumentQualifier p_qualifier) const;
  927. Error _parse_shader(const HashMap<StringName, FunctionInfo> &p_functions, const Vector<ModeInfo> &p_render_modes, const HashSet<String> &p_shader_types);
  928. Error _find_last_flow_op_in_block(BlockNode *p_block, FlowOperation p_op);
  929. Error _find_last_flow_op_in_op(ControlFlowNode *p_flow, FlowOperation p_op);
  930. public:
  931. #ifdef DEBUG_ENABLED
  932. List<ShaderWarning>::Element *get_warnings_ptr();
  933. void enable_warning_checking(bool p_enabled);
  934. bool is_warning_checking_enabled() const;
  935. void set_warning_flags(uint32_t p_flags);
  936. uint32_t get_warning_flags() const;
  937. #endif // DEBUG_ENABLED
  938. //static void get_keyword_list(ShaderType p_type,List<String> *p_keywords);
  939. void clear();
  940. static String get_shader_type(const String &p_code);
  941. struct ShaderCompileInfo {
  942. HashMap<StringName, FunctionInfo> functions;
  943. Vector<ModeInfo> render_modes;
  944. VaryingFunctionNames varying_function_names = VaryingFunctionNames();
  945. HashSet<String> shader_types;
  946. GlobalVariableGetTypeFunc global_variable_type_func = nullptr;
  947. };
  948. Error compile(const String &p_code, const ShaderCompileInfo &p_info);
  949. Error complete(const String &p_code, const ShaderCompileInfo &p_info, List<ScriptLanguage::CodeCompletionOption> *r_options, String &r_call_hint);
  950. String get_error_text();
  951. int get_error_line();
  952. ShaderNode *get_shader();
  953. String token_debug(const String &p_code);
  954. ShaderLanguage();
  955. ~ShaderLanguage();
  956. };
  957. #endif // SHADER_LANGUAGE_H