shader_language.h 32 KB

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