shader_language.h 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040
  1. /*************************************************************************/
  2. /* shader_language.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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/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_TYPE_VOID,
  56. TK_TYPE_BOOL,
  57. TK_TYPE_BVEC2,
  58. TK_TYPE_BVEC3,
  59. TK_TYPE_BVEC4,
  60. TK_TYPE_INT,
  61. TK_TYPE_IVEC2,
  62. TK_TYPE_IVEC3,
  63. TK_TYPE_IVEC4,
  64. TK_TYPE_UINT,
  65. TK_TYPE_UVEC2,
  66. TK_TYPE_UVEC3,
  67. TK_TYPE_UVEC4,
  68. TK_TYPE_FLOAT,
  69. TK_TYPE_VEC2,
  70. TK_TYPE_VEC3,
  71. TK_TYPE_VEC4,
  72. TK_TYPE_MAT2,
  73. TK_TYPE_MAT3,
  74. TK_TYPE_MAT4,
  75. TK_TYPE_SAMPLER2D,
  76. TK_TYPE_ISAMPLER2D,
  77. TK_TYPE_USAMPLER2D,
  78. TK_TYPE_SAMPLER2DARRAY,
  79. TK_TYPE_ISAMPLER2DARRAY,
  80. TK_TYPE_USAMPLER2DARRAY,
  81. TK_TYPE_SAMPLER3D,
  82. TK_TYPE_ISAMPLER3D,
  83. TK_TYPE_USAMPLER3D,
  84. TK_TYPE_SAMPLERCUBE,
  85. TK_TYPE_SAMPLERCUBEARRAY,
  86. TK_INTERPOLATION_FLAT,
  87. TK_INTERPOLATION_SMOOTH,
  88. TK_CONST,
  89. TK_STRUCT,
  90. TK_PRECISION_LOW,
  91. TK_PRECISION_MID,
  92. TK_PRECISION_HIGH,
  93. TK_OP_EQUAL,
  94. TK_OP_NOT_EQUAL,
  95. TK_OP_LESS,
  96. TK_OP_LESS_EQUAL,
  97. TK_OP_GREATER,
  98. TK_OP_GREATER_EQUAL,
  99. TK_OP_AND,
  100. TK_OP_OR,
  101. TK_OP_NOT,
  102. TK_OP_ADD,
  103. TK_OP_SUB,
  104. TK_OP_MUL,
  105. TK_OP_DIV,
  106. TK_OP_MOD,
  107. TK_OP_SHIFT_LEFT,
  108. TK_OP_SHIFT_RIGHT,
  109. TK_OP_ASSIGN,
  110. TK_OP_ASSIGN_ADD,
  111. TK_OP_ASSIGN_SUB,
  112. TK_OP_ASSIGN_MUL,
  113. TK_OP_ASSIGN_DIV,
  114. TK_OP_ASSIGN_MOD,
  115. TK_OP_ASSIGN_SHIFT_LEFT,
  116. TK_OP_ASSIGN_SHIFT_RIGHT,
  117. TK_OP_ASSIGN_BIT_AND,
  118. TK_OP_ASSIGN_BIT_OR,
  119. TK_OP_ASSIGN_BIT_XOR,
  120. TK_OP_BIT_AND,
  121. TK_OP_BIT_OR,
  122. TK_OP_BIT_XOR,
  123. TK_OP_BIT_INVERT,
  124. TK_OP_INCREMENT,
  125. TK_OP_DECREMENT,
  126. TK_CF_IF,
  127. TK_CF_ELSE,
  128. TK_CF_FOR,
  129. TK_CF_WHILE,
  130. TK_CF_DO,
  131. TK_CF_SWITCH,
  132. TK_CF_CASE,
  133. TK_CF_DEFAULT,
  134. TK_CF_BREAK,
  135. TK_CF_CONTINUE,
  136. TK_CF_RETURN,
  137. TK_CF_DISCARD,
  138. TK_BRACKET_OPEN,
  139. TK_BRACKET_CLOSE,
  140. TK_CURLY_BRACKET_OPEN,
  141. TK_CURLY_BRACKET_CLOSE,
  142. TK_PARENTHESIS_OPEN,
  143. TK_PARENTHESIS_CLOSE,
  144. TK_QUESTION,
  145. TK_COMMA,
  146. TK_COLON,
  147. TK_SEMICOLON,
  148. TK_PERIOD,
  149. TK_UNIFORM,
  150. TK_INSTANCE,
  151. TK_GLOBAL,
  152. TK_VARYING,
  153. TK_ARG_IN,
  154. TK_ARG_OUT,
  155. TK_ARG_INOUT,
  156. TK_RENDER_MODE,
  157. TK_HINT_WHITE_TEXTURE,
  158. TK_HINT_BLACK_TEXTURE,
  159. TK_HINT_NORMAL_TEXTURE,
  160. TK_HINT_ROUGHNESS_NORMAL_TEXTURE,
  161. TK_HINT_ROUGHNESS_R,
  162. TK_HINT_ROUGHNESS_G,
  163. TK_HINT_ROUGHNESS_B,
  164. TK_HINT_ROUGHNESS_A,
  165. TK_HINT_ROUGHNESS_GRAY,
  166. TK_HINT_ANISO_TEXTURE,
  167. TK_HINT_ALBEDO_TEXTURE,
  168. TK_HINT_BLACK_ALBEDO_TEXTURE,
  169. TK_HINT_COLOR,
  170. TK_HINT_RANGE,
  171. TK_HINT_INSTANCE_INDEX,
  172. TK_FILTER_NEAREST,
  173. TK_FILTER_LINEAR,
  174. TK_FILTER_NEAREST_MIPMAP,
  175. TK_FILTER_LINEAR_MIPMAP,
  176. TK_FILTER_NEAREST_MIPMAP_ANISO,
  177. TK_FILTER_LINEAR_MIPMAP_ANISO,
  178. TK_REPEAT_ENABLE,
  179. TK_REPEAT_DISABLE,
  180. TK_SHADER_TYPE,
  181. TK_CURSOR,
  182. TK_ERROR,
  183. TK_EOF,
  184. TK_MAX
  185. };
  186. /* COMPILER */
  187. // lame work around to Apple defining this as a macro in 10.12 SDK
  188. #ifdef TYPE_BOOL
  189. #undef TYPE_BOOL
  190. #endif
  191. enum DataType {
  192. TYPE_VOID,
  193. TYPE_BOOL,
  194. TYPE_BVEC2,
  195. TYPE_BVEC3,
  196. TYPE_BVEC4,
  197. TYPE_INT,
  198. TYPE_IVEC2,
  199. TYPE_IVEC3,
  200. TYPE_IVEC4,
  201. TYPE_UINT,
  202. TYPE_UVEC2,
  203. TYPE_UVEC3,
  204. TYPE_UVEC4,
  205. TYPE_FLOAT,
  206. TYPE_VEC2,
  207. TYPE_VEC3,
  208. TYPE_VEC4,
  209. TYPE_MAT2,
  210. TYPE_MAT3,
  211. TYPE_MAT4,
  212. TYPE_SAMPLER2D,
  213. TYPE_ISAMPLER2D,
  214. TYPE_USAMPLER2D,
  215. TYPE_SAMPLER2DARRAY,
  216. TYPE_ISAMPLER2DARRAY,
  217. TYPE_USAMPLER2DARRAY,
  218. TYPE_SAMPLER3D,
  219. TYPE_ISAMPLER3D,
  220. TYPE_USAMPLER3D,
  221. TYPE_SAMPLERCUBE,
  222. TYPE_SAMPLERCUBEARRAY,
  223. TYPE_STRUCT,
  224. TYPE_MAX
  225. };
  226. enum DataPrecision {
  227. PRECISION_LOWP,
  228. PRECISION_MEDIUMP,
  229. PRECISION_HIGHP,
  230. PRECISION_DEFAULT,
  231. };
  232. enum DataInterpolation {
  233. INTERPOLATION_FLAT,
  234. INTERPOLATION_SMOOTH,
  235. };
  236. enum Operator {
  237. OP_EQUAL,
  238. OP_NOT_EQUAL,
  239. OP_LESS,
  240. OP_LESS_EQUAL,
  241. OP_GREATER,
  242. OP_GREATER_EQUAL,
  243. OP_AND,
  244. OP_OR,
  245. OP_NOT,
  246. OP_NEGATE,
  247. OP_ADD,
  248. OP_SUB,
  249. OP_MUL,
  250. OP_DIV,
  251. OP_MOD,
  252. OP_SHIFT_LEFT,
  253. OP_SHIFT_RIGHT,
  254. OP_ASSIGN,
  255. OP_ASSIGN_ADD,
  256. OP_ASSIGN_SUB,
  257. OP_ASSIGN_MUL,
  258. OP_ASSIGN_DIV,
  259. OP_ASSIGN_MOD,
  260. OP_ASSIGN_SHIFT_LEFT,
  261. OP_ASSIGN_SHIFT_RIGHT,
  262. OP_ASSIGN_BIT_AND,
  263. OP_ASSIGN_BIT_OR,
  264. OP_ASSIGN_BIT_XOR,
  265. OP_BIT_AND,
  266. OP_BIT_OR,
  267. OP_BIT_XOR,
  268. OP_BIT_INVERT,
  269. OP_INCREMENT,
  270. OP_DECREMENT,
  271. OP_SELECT_IF,
  272. OP_SELECT_ELSE, //used only internally, then only IF appears with 3 arguments
  273. OP_POST_INCREMENT,
  274. OP_POST_DECREMENT,
  275. OP_CALL,
  276. OP_CONSTRUCT,
  277. OP_STRUCT,
  278. OP_INDEX,
  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_ANISO,
  309. FILTER_LINEAR_MIPMAP_ANISO,
  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_DECLARATION,
  344. TYPE_ARRAY_CONSTRUCT,
  345. TYPE_STRUCT,
  346. };
  347. Type type;
  348. virtual DataType get_datatype() const { return TYPE_VOID; }
  349. virtual String get_datatype_name() const { return ""; }
  350. virtual int get_array_size() const { return 0; }
  351. virtual bool is_indexed() const { return false; }
  352. Node(Type t) :
  353. type(t) {}
  354. virtual ~Node() {}
  355. };
  356. template <class T>
  357. T *alloc_node() {
  358. T *node = memnew(T);
  359. node->next = nodes;
  360. nodes = node;
  361. return node;
  362. }
  363. Node *nodes;
  364. struct OperatorNode : public Node {
  365. DataType return_cache = TYPE_VOID;
  366. DataPrecision return_precision_cache = PRECISION_DEFAULT;
  367. int return_array_size = 0;
  368. Operator op = OP_EQUAL;
  369. StringName struct_name;
  370. Vector<Node *> arguments;
  371. virtual DataType get_datatype() const override { return return_cache; }
  372. virtual String get_datatype_name() const override { return String(struct_name); }
  373. virtual int get_array_size() const override { return return_array_size; }
  374. virtual bool is_indexed() const override { return op == OP_INDEX; }
  375. OperatorNode() :
  376. Node(TYPE_OPERATOR) {}
  377. };
  378. struct VariableNode : public Node {
  379. DataType datatype_cache = TYPE_VOID;
  380. StringName name;
  381. StringName struct_name;
  382. bool is_const = false;
  383. bool is_local = false;
  384. virtual DataType get_datatype() const override { return datatype_cache; }
  385. virtual String get_datatype_name() const override { return String(struct_name); }
  386. VariableNode() :
  387. Node(TYPE_VARIABLE) {}
  388. };
  389. struct VariableDeclarationNode : public Node {
  390. DataPrecision precision = PRECISION_DEFAULT;
  391. DataType datatype = TYPE_VOID;
  392. String struct_name;
  393. bool is_const = false;
  394. struct Declaration {
  395. StringName name;
  396. Node *initializer;
  397. };
  398. Vector<Declaration> declarations;
  399. virtual DataType get_datatype() const override { return datatype; }
  400. VariableDeclarationNode() :
  401. Node(TYPE_VARIABLE_DECLARATION) {}
  402. };
  403. struct ArrayNode : public Node {
  404. DataType datatype_cache = TYPE_VOID;
  405. StringName struct_name;
  406. StringName name;
  407. Node *index_expression = nullptr;
  408. Node *call_expression = nullptr;
  409. Node *assign_expression = nullptr;
  410. bool is_const = false;
  411. int array_size = 0;
  412. bool is_local = false;
  413. virtual DataType get_datatype() const override { return datatype_cache; }
  414. virtual String get_datatype_name() const override { return String(struct_name); }
  415. virtual int get_array_size() const override { return (index_expression || call_expression) ? 0 : array_size; }
  416. virtual bool is_indexed() const override { return index_expression != nullptr; }
  417. ArrayNode() :
  418. Node(TYPE_ARRAY) {}
  419. };
  420. struct ArrayConstructNode : public Node {
  421. DataType datatype = TYPE_VOID;
  422. String struct_name;
  423. Vector<Node *> initializer;
  424. virtual DataType get_datatype() const override { return datatype; }
  425. virtual String get_datatype_name() const override { return struct_name; }
  426. virtual int get_array_size() const override { return initializer.size(); }
  427. ArrayConstructNode() :
  428. Node(TYPE_ARRAY_CONSTRUCT) {}
  429. };
  430. struct ArrayDeclarationNode : public Node {
  431. DataPrecision precision = PRECISION_DEFAULT;
  432. DataType datatype = TYPE_VOID;
  433. String struct_name;
  434. bool is_const = false;
  435. Node *size_expression = nullptr;
  436. struct Declaration {
  437. StringName name;
  438. uint32_t size;
  439. Vector<Node *> initializer;
  440. bool single_expression;
  441. };
  442. Vector<Declaration> declarations;
  443. virtual DataType get_datatype() const override { return datatype; }
  444. ArrayDeclarationNode() :
  445. Node(TYPE_ARRAY_DECLARATION) {}
  446. };
  447. struct ConstantNode : public Node {
  448. DataType datatype = TYPE_VOID;
  449. String struct_name = "";
  450. int array_size = 0;
  451. union Value {
  452. bool boolean;
  453. float real;
  454. int32_t sint;
  455. uint32_t uint;
  456. };
  457. Vector<Value> values;
  458. Vector<ArrayDeclarationNode::Declaration> array_declarations;
  459. virtual DataType get_datatype() const override { return datatype; }
  460. virtual String get_datatype_name() const override { return struct_name; }
  461. virtual int get_array_size() const override { return array_size; }
  462. ConstantNode() :
  463. Node(TYPE_CONSTANT) {}
  464. };
  465. struct FunctionNode;
  466. struct BlockNode : public Node {
  467. FunctionNode *parent_function = nullptr;
  468. BlockNode *parent_block = nullptr;
  469. enum BlockType {
  470. BLOCK_TYPE_STANDART,
  471. BLOCK_TYPE_FOR,
  472. BLOCK_TYPE_SWITCH,
  473. BLOCK_TYPE_CASE,
  474. BLOCK_TYPE_DEFAULT,
  475. };
  476. int block_type = BLOCK_TYPE_STANDART;
  477. SubClassTag block_tag = SubClassTag::TAG_GLOBAL;
  478. struct Variable {
  479. DataType type;
  480. StringName struct_name;
  481. DataPrecision precision;
  482. int line; //for completion
  483. int array_size;
  484. bool is_const;
  485. ConstantNode::Value value;
  486. };
  487. Map<StringName, Variable> variables;
  488. List<Node *> statements;
  489. bool single_statement = false;
  490. BlockNode() :
  491. Node(TYPE_BLOCK) {}
  492. };
  493. struct ControlFlowNode : public Node {
  494. FlowOperation flow_op = FLOW_OP_IF;
  495. Vector<Node *> expressions;
  496. Vector<BlockNode *> blocks;
  497. ControlFlowNode() :
  498. Node(TYPE_CONTROL_FLOW) {}
  499. };
  500. struct MemberNode : public Node {
  501. DataType basetype = TYPE_VOID;
  502. bool basetype_const = false;
  503. StringName base_struct_name;
  504. DataPrecision precision = PRECISION_DEFAULT;
  505. DataType datatype = TYPE_VOID;
  506. int array_size = 0;
  507. StringName struct_name;
  508. StringName name;
  509. Node *owner = nullptr;
  510. Node *index_expression = nullptr;
  511. Node *assign_expression = nullptr;
  512. Node *call_expression = nullptr;
  513. bool has_swizzling_duplicates = false;
  514. virtual DataType get_datatype() const override { return datatype; }
  515. virtual String get_datatype_name() const override { return String(struct_name); }
  516. virtual int get_array_size() const override { return array_size; }
  517. virtual bool is_indexed() const override { return index_expression != nullptr || call_expression != nullptr; }
  518. MemberNode() :
  519. Node(TYPE_MEMBER) {}
  520. };
  521. struct StructNode : public Node {
  522. List<MemberNode *> members;
  523. StructNode() :
  524. Node(TYPE_STRUCT) {}
  525. };
  526. struct FunctionNode : public Node {
  527. struct Argument {
  528. ArgumentQualifier qualifier;
  529. StringName name;
  530. DataType type;
  531. StringName type_str;
  532. DataPrecision precision;
  533. //for passing textures as arguments
  534. bool tex_argument_check;
  535. TextureFilter tex_argument_filter;
  536. TextureRepeat tex_argument_repeat;
  537. bool tex_builtin_check;
  538. StringName tex_builtin;
  539. bool is_const;
  540. int array_size;
  541. Map<StringName, Set<int>> tex_argument_connect;
  542. };
  543. StringName name;
  544. DataType return_type = TYPE_VOID;
  545. StringName return_struct_name;
  546. DataPrecision return_precision = PRECISION_DEFAULT;
  547. int return_array_size = 0;
  548. Vector<Argument> arguments;
  549. BlockNode *body = nullptr;
  550. bool can_discard = false;
  551. virtual DataType get_datatype() const override { return return_type; }
  552. virtual String get_datatype_name() const override { return String(return_struct_name); }
  553. virtual int get_array_size() const override { return return_array_size; }
  554. FunctionNode() :
  555. Node(TYPE_FUNCTION) {}
  556. };
  557. struct ShaderNode : public Node {
  558. struct Constant {
  559. StringName name;
  560. DataType type;
  561. StringName type_str;
  562. DataPrecision precision;
  563. ConstantNode *initializer;
  564. int array_size;
  565. };
  566. struct Function {
  567. StringName name;
  568. FunctionNode *function;
  569. Set<StringName> uses_function;
  570. bool callable;
  571. };
  572. struct Struct {
  573. StringName name;
  574. StructNode *shader_struct;
  575. };
  576. struct Varying {
  577. enum Stage {
  578. STAGE_UNKNOWN,
  579. STAGE_VERTEX, // transition stage to STAGE_VERTEX_TO_FRAGMENT_LIGHT, emits warning if it's not used
  580. STAGE_FRAGMENT, // transition stage to STAGE_FRAGMENT_TO_LIGHT, emits warning if it's not used
  581. STAGE_VERTEX_TO_FRAGMENT_LIGHT,
  582. STAGE_FRAGMENT_TO_LIGHT,
  583. };
  584. Stage stage = STAGE_UNKNOWN;
  585. DataType type = TYPE_VOID;
  586. DataInterpolation interpolation = INTERPOLATION_FLAT;
  587. DataPrecision precision = PRECISION_DEFAULT;
  588. int array_size = 0;
  589. TkPos tkpos;
  590. Varying() {}
  591. };
  592. struct Uniform {
  593. enum Hint {
  594. HINT_NONE,
  595. HINT_COLOR,
  596. HINT_RANGE,
  597. HINT_ALBEDO,
  598. HINT_BLACK_ALBEDO,
  599. HINT_NORMAL,
  600. HINT_ROUGHNESS_NORMAL,
  601. HINT_ROUGHNESS_R,
  602. HINT_ROUGHNESS_G,
  603. HINT_ROUGHNESS_B,
  604. HINT_ROUGHNESS_A,
  605. HINT_ROUGHNESS_GRAY,
  606. HINT_BLACK,
  607. HINT_WHITE,
  608. HINT_ANISO,
  609. HINT_MAX
  610. };
  611. enum Scope {
  612. SCOPE_LOCAL,
  613. SCOPE_INSTANCE,
  614. SCOPE_GLOBAL,
  615. };
  616. int order = 0;
  617. int texture_order = 0;
  618. int texture_binding = 0;
  619. DataType type = TYPE_VOID;
  620. DataPrecision precision = PRECISION_DEFAULT;
  621. int array_size = 0;
  622. Vector<ConstantNode::Value> default_value;
  623. Scope scope = SCOPE_LOCAL;
  624. Hint hint = HINT_NONE;
  625. TextureFilter filter = FILTER_DEFAULT;
  626. TextureRepeat repeat = REPEAT_DEFAULT;
  627. float hint_range[3];
  628. int instance_index = 0;
  629. Uniform() {
  630. hint_range[0] = 0.0f;
  631. hint_range[1] = 1.0f;
  632. hint_range[2] = 0.001f;
  633. }
  634. };
  635. Map<StringName, Constant> constants;
  636. Map<StringName, Varying> varyings;
  637. Map<StringName, Uniform> uniforms;
  638. Map<StringName, Struct> structs;
  639. Vector<StringName> render_modes;
  640. Vector<Function> functions;
  641. Vector<Constant> vconstants;
  642. Vector<Struct> vstructs;
  643. ShaderNode() :
  644. Node(TYPE_SHADER) {}
  645. };
  646. struct Expression {
  647. bool is_op;
  648. union {
  649. Operator op;
  650. Node *node;
  651. };
  652. };
  653. struct VarInfo {
  654. StringName name;
  655. DataType type;
  656. };
  657. enum CompletionType {
  658. COMPLETION_NONE,
  659. COMPLETION_RENDER_MODE,
  660. COMPLETION_MAIN_FUNCTION,
  661. COMPLETION_IDENTIFIER,
  662. COMPLETION_FUNCTION_CALL,
  663. COMPLETION_CALL_ARGUMENTS,
  664. COMPLETION_INDEX,
  665. COMPLETION_STRUCT,
  666. };
  667. struct Token {
  668. TokenType type;
  669. StringName text;
  670. double constant;
  671. uint16_t line;
  672. };
  673. static String get_operator_text(Operator p_op);
  674. static String get_token_text(Token p_token);
  675. static bool is_token_datatype(TokenType p_type);
  676. static bool is_token_variable_datatype(TokenType p_type);
  677. static DataType get_token_datatype(TokenType p_type);
  678. static bool is_token_interpolation(TokenType p_type);
  679. static DataInterpolation get_token_interpolation(TokenType p_type);
  680. static bool is_token_precision(TokenType p_type);
  681. static DataPrecision get_token_precision(TokenType p_type);
  682. static String get_precision_name(DataPrecision p_type);
  683. static String get_datatype_name(DataType p_type);
  684. static bool is_token_nonvoid_datatype(TokenType p_type);
  685. static bool is_token_operator(TokenType p_type);
  686. static bool is_token_operator_assign(TokenType p_type);
  687. static bool convert_constant(ConstantNode *p_constant, DataType p_to_type, ConstantNode::Value *p_value = nullptr);
  688. static DataType get_scalar_type(DataType p_type);
  689. static int get_cardinality(DataType p_type);
  690. static bool is_scalar_type(DataType p_type);
  691. static bool is_float_type(DataType p_type);
  692. static bool is_sampler_type(DataType p_type);
  693. 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);
  694. static PropertyInfo uniform_to_property_info(const ShaderNode::Uniform &p_uniform);
  695. static uint32_t get_type_size(DataType p_type);
  696. static void get_keyword_list(List<String> *r_keywords);
  697. static bool is_control_flow_keyword(String p_keyword);
  698. static void get_builtin_funcs(List<String> *r_keywords);
  699. struct BuiltInInfo {
  700. DataType type = TYPE_VOID;
  701. bool constant = false;
  702. BuiltInInfo() {}
  703. BuiltInInfo(DataType p_type, bool p_constant = false) :
  704. type(p_type),
  705. constant(p_constant) {}
  706. };
  707. struct StageFunctionInfo {
  708. struct Argument {
  709. StringName name;
  710. DataType type;
  711. Argument(const StringName &p_name = StringName(), DataType p_type = TYPE_VOID) {
  712. name = p_name;
  713. type = p_type;
  714. }
  715. };
  716. Vector<Argument> arguments;
  717. DataType return_type = TYPE_VOID;
  718. };
  719. struct FunctionInfo {
  720. Map<StringName, BuiltInInfo> built_ins;
  721. Map<StringName, StageFunctionInfo> stage_functions;
  722. bool can_discard = false;
  723. bool main_function = false;
  724. };
  725. static bool has_builtin(const Map<StringName, ShaderLanguage::FunctionInfo> &p_functions, const StringName &p_name);
  726. typedef DataType (*GlobalVariableGetTypeFunc)(const StringName &p_name);
  727. private:
  728. struct KeyWord {
  729. TokenType token;
  730. const char *text;
  731. };
  732. static const KeyWord keyword_list[];
  733. GlobalVariableGetTypeFunc global_var_get_type_func;
  734. bool error_set;
  735. String error_str;
  736. int error_line;
  737. #ifdef DEBUG_ENABLED
  738. struct Usage {
  739. int decl_line;
  740. bool used = false;
  741. Usage(int p_decl_line = -1) {
  742. decl_line = p_decl_line;
  743. }
  744. };
  745. Map<StringName, Usage> used_constants;
  746. Map<StringName, Usage> used_varyings;
  747. Map<StringName, Usage> used_uniforms;
  748. Map<StringName, Usage> used_functions;
  749. Map<StringName, Usage> used_structs;
  750. Map<ShaderWarning::Code, Map<StringName, Usage> *> warnings_check_map;
  751. Map<StringName, Map<StringName, Usage>> used_local_vars;
  752. Map<ShaderWarning::Code, Map<StringName, Map<StringName, Usage>> *> warnings_check_map2;
  753. List<ShaderWarning> warnings;
  754. bool check_warnings = false;
  755. uint32_t warning_flags;
  756. void _add_line_warning(ShaderWarning::Code p_code, const StringName &p_subject = "") {
  757. warnings.push_back(ShaderWarning(p_code, tk_line, p_subject));
  758. }
  759. void _add_warning(ShaderWarning::Code p_code, int p_line, const StringName &p_subject = "") {
  760. warnings.push_back(ShaderWarning(p_code, p_line, p_subject));
  761. }
  762. void _check_warning_accums();
  763. #endif // DEBUG_ENABLED
  764. String code;
  765. int char_idx;
  766. int tk_line;
  767. StringName current_function;
  768. bool last_const = false;
  769. StringName last_name;
  770. VaryingFunctionNames varying_function_names;
  771. struct VaryingUsage {
  772. ShaderNode::Varying *var;
  773. int line;
  774. };
  775. List<VaryingUsage> unknown_varying_usages;
  776. bool _check_varying_usages(int *r_error_line, String *r_error_message) const;
  777. TkPos _get_tkpos() {
  778. TkPos tkp;
  779. tkp.char_idx = char_idx;
  780. tkp.tk_line = tk_line;
  781. return tkp;
  782. }
  783. void _set_tkpos(TkPos p_pos) {
  784. char_idx = p_pos.char_idx;
  785. tk_line = p_pos.tk_line;
  786. }
  787. void _set_error(const String &p_str) {
  788. if (error_set) {
  789. return;
  790. }
  791. error_line = tk_line;
  792. error_set = true;
  793. error_str = p_str;
  794. }
  795. static const char *token_names[TK_MAX];
  796. Token _make_token(TokenType p_type, const StringName &p_text = StringName());
  797. Token _get_token();
  798. ShaderNode *shader;
  799. enum IdentifierType {
  800. IDENTIFIER_FUNCTION,
  801. IDENTIFIER_UNIFORM,
  802. IDENTIFIER_VARYING,
  803. IDENTIFIER_FUNCTION_ARGUMENT,
  804. IDENTIFIER_LOCAL_VAR,
  805. IDENTIFIER_BUILTIN_VAR,
  806. IDENTIFIER_CONSTANT,
  807. IDENTIFIER_MAX,
  808. };
  809. IdentifierType last_type = IDENTIFIER_MAX;
  810. 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);
  811. #ifdef DEBUG_ENABLED
  812. void _parse_used_identifier(const StringName &p_identifier, IdentifierType p_type, const StringName &p_function);
  813. #endif // DEBUG_ENABLED
  814. bool _is_operator_assign(Operator p_op) const;
  815. bool _validate_assign(Node *p_node, const FunctionInfo &p_function_info, String *r_message = nullptr);
  816. bool _validate_operator(OperatorNode *p_op, DataType *r_ret_type = nullptr, int *r_ret_size = nullptr);
  817. struct BuiltinFuncDef {
  818. enum { MAX_ARGS = 5 };
  819. const char *name;
  820. DataType rettype;
  821. const DataType args[MAX_ARGS];
  822. const char *args_names[MAX_ARGS];
  823. SubClassTag tag;
  824. bool high_end;
  825. };
  826. struct BuiltinFuncOutArgs { //arguments used as out in built in functions
  827. enum { MAX_ARGS = 2 };
  828. const char *name;
  829. const int arguments[MAX_ARGS];
  830. };
  831. struct BuiltinFuncConstArgs {
  832. const char *name;
  833. int arg;
  834. int min;
  835. int max;
  836. };
  837. CompletionType completion_type;
  838. int completion_line;
  839. BlockNode *completion_block;
  840. DataType completion_base;
  841. SubClassTag completion_class;
  842. StringName completion_function;
  843. StringName completion_struct;
  844. int completion_argument;
  845. const Map<StringName, FunctionInfo> *stages = nullptr;
  846. bool _get_completable_identifier(BlockNode *p_block, CompletionType p_type, StringName &identifier);
  847. static const BuiltinFuncDef builtin_func_defs[];
  848. static const BuiltinFuncOutArgs builtin_func_out_args[];
  849. static const BuiltinFuncConstArgs builtin_func_const_args[];
  850. Error _validate_datatype(DataType p_type);
  851. 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);
  852. bool _compare_datatypes_in_nodes(Node *a, Node *b);
  853. bool _validate_function_call(BlockNode *p_block, const FunctionInfo &p_function_info, OperatorNode *p_func, DataType *r_ret_type, StringName *r_ret_type_str);
  854. bool _parse_function_arguments(BlockNode *p_block, const FunctionInfo &p_function_info, OperatorNode *p_func, int *r_complete_arg = nullptr);
  855. bool _propagate_function_call_sampler_uniform_settings(StringName p_name, int p_argument, TextureFilter p_filter, TextureRepeat p_repeat);
  856. bool _propagate_function_call_sampler_builtin_reference(StringName p_name, int p_argument, const StringName &p_builtin);
  857. bool _validate_varying_assign(ShaderNode::Varying &p_varying, String *r_message);
  858. bool _validate_varying_using(ShaderNode::Varying &p_varying, String *r_message);
  859. bool _check_node_constness(const Node *p_node) const;
  860. Node *_parse_array_size(BlockNode *p_block, const FunctionInfo &p_function_info, int &r_array_size);
  861. Error _parse_global_array_size(int &r_array_size);
  862. Error _parse_local_array_size(BlockNode *p_block, const FunctionInfo &p_function_info, ArrayDeclarationNode *p_node, ArrayDeclarationNode::Declaration *p_decl, int &r_array_size, bool &r_is_unknown_size);
  863. Node *_parse_expression(BlockNode *p_block, const FunctionInfo &p_function_info);
  864. Node *_parse_array_constructor(BlockNode *p_block, const FunctionInfo &p_function_info);
  865. Node *_parse_array_constructor(BlockNode *p_block, const FunctionInfo &p_function_info, DataType p_type, const StringName &p_struct_name, int p_array_size);
  866. ShaderLanguage::Node *_reduce_expression(BlockNode *p_block, ShaderLanguage::Node *p_node);
  867. Node *_parse_and_reduce_expression(BlockNode *p_block, const FunctionInfo &p_function_info);
  868. 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);
  869. String _get_shader_type_list(const Set<String> &p_shader_types) const;
  870. String _get_qualifier_str(ArgumentQualifier p_qualifier) const;
  871. Error _parse_shader(const Map<StringName, FunctionInfo> &p_functions, const Vector<StringName> &p_render_modes, const Set<String> &p_shader_types);
  872. Error _find_last_flow_op_in_block(BlockNode *p_block, FlowOperation p_op);
  873. Error _find_last_flow_op_in_op(ControlFlowNode *p_flow, FlowOperation p_op);
  874. public:
  875. #ifdef DEBUG_ENABLED
  876. List<ShaderWarning>::Element *get_warnings_ptr();
  877. void enable_warning_checking(bool p_enabled);
  878. bool is_warning_checking_enabled() const;
  879. void set_warning_flags(uint32_t p_flags);
  880. uint32_t get_warning_flags() const;
  881. #endif // DEBUG_ENABLED
  882. //static void get_keyword_list(ShaderType p_type,List<String> *p_keywords);
  883. void clear();
  884. static String get_shader_type(const String &p_code);
  885. Error compile(const String &p_code, const Map<StringName, FunctionInfo> &p_functions, const Vector<StringName> &p_render_modes, const VaryingFunctionNames &p_varying_function_names, const Set<String> &p_shader_types, GlobalVariableGetTypeFunc p_global_variable_type_func);
  886. Error complete(const String &p_code, const Map<StringName, FunctionInfo> &p_functions, const Vector<StringName> &p_render_modes, const VaryingFunctionNames &p_varying_function_names, const Set<String> &p_shader_types, GlobalVariableGetTypeFunc p_global_variable_type_func, List<ScriptCodeCompletionOption> *r_options, String &r_call_hint);
  887. String get_error_text();
  888. int get_error_line();
  889. ShaderNode *get_shader();
  890. String token_debug(const String &p_code);
  891. ShaderLanguage();
  892. ~ShaderLanguage();
  893. };
  894. #endif // SHADER_LANGUAGE_H