1 /*************************************************************************/ 2 /* shader_language.h */ 3 /*************************************************************************/ 4 /* This file is part of: */ 5 /* GODOT ENGINE */ 6 /* https://godotengine.org */ 7 /*************************************************************************/ 8 /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ 9 /* Copyright (c) 2014-2019 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 33 #include "list.h" 34 #include "map.h" 35 #include "string_db.h" 36 #include "typedefs.h" 37 #include "ustring.h" 38 #include "variant.h" 39 40 class ShaderLanguage { 41 42 public: 43 enum TokenType { 44 45 TK_EMPTY, 46 TK_INDENTIFIER, 47 TK_TRUE, 48 TK_FALSE, 49 TK_REAL_CONSTANT, 50 TK_TYPE_VOID, 51 TK_TYPE_BOOL, 52 TK_TYPE_FLOAT, 53 TK_TYPE_VEC2, 54 TK_TYPE_VEC3, 55 TK_TYPE_VEC4, 56 TK_TYPE_MAT2, 57 TK_TYPE_MAT3, 58 TK_TYPE_MAT4, 59 TK_TYPE_TEXTURE, 60 TK_TYPE_CUBEMAP, 61 TK_TYPE_COLOR, 62 TK_OP_EQUAL, 63 TK_OP_NOT_EQUAL, 64 TK_OP_LESS, 65 TK_OP_LESS_EQUAL, 66 TK_OP_GREATER, 67 TK_OP_GREATER_EQUAL, 68 TK_OP_AND, 69 TK_OP_OR, 70 TK_OP_NOT, 71 TK_OP_ADD, 72 TK_OP_SUB, 73 TK_OP_MUL, 74 TK_OP_DIV, 75 TK_OP_NEG, 76 TK_OP_ASSIGN, 77 TK_OP_ASSIGN_ADD, 78 TK_OP_ASSIGN_SUB, 79 TK_OP_ASSIGN_MUL, 80 TK_OP_ASSIGN_DIV, 81 TK_CF_IF, 82 TK_CF_ELSE, 83 TK_CF_RETURN, 84 TK_BRACKET_OPEN, 85 TK_BRACKET_CLOSE, 86 TK_CURLY_BRACKET_OPEN, 87 TK_CURLY_BRACKET_CLOSE, 88 TK_PARENTHESIS_OPEN, 89 TK_PARENTHESIS_CLOSE, 90 TK_COMMA, 91 TK_SEMICOLON, 92 TK_PERIOD, 93 TK_UNIFORM, 94 TK_ERROR, 95 TK_MAX 96 }; 97 98 /* COMPILER */ 99 100 enum ShaderType { 101 SHADER_MATERIAL_VERTEX, 102 SHADER_MATERIAL_FRAGMENT, 103 SHADER_MATERIAL_LIGHT, 104 SHADER_CANVAS_ITEM_VERTEX, 105 SHADER_CANVAS_ITEM_FRAGMENT, 106 SHADER_CANVAS_ITEM_LIGHT, 107 SHADER_POST_PROCESS, 108 }; 109 110 enum DataType { 111 TYPE_VOID, 112 TYPE_BOOL, 113 TYPE_FLOAT, 114 TYPE_VEC2, 115 TYPE_VEC3, 116 TYPE_VEC4, 117 TYPE_MAT2, 118 TYPE_MAT3, 119 TYPE_MAT4, 120 TYPE_TEXTURE, 121 TYPE_CUBEMAP, 122 }; 123 124 enum Operator { 125 OP_ASSIGN, 126 OP_ADD, 127 OP_SUB, 128 OP_MUL, 129 OP_DIV, 130 OP_ASSIGN_ADD, 131 OP_ASSIGN_SUB, 132 OP_ASSIGN_MUL, 133 OP_ASSIGN_DIV, 134 OP_NEG, 135 OP_NOT, 136 OP_CMP_EQ, 137 OP_CMP_NEQ, 138 OP_CMP_LEQ, 139 OP_CMP_GEQ, 140 OP_CMP_LESS, 141 OP_CMP_GREATER, 142 OP_CMP_OR, 143 OP_CMP_AND, 144 OP_CALL, 145 OP_CONSTRUCT, 146 OP_MAX 147 }; 148 149 enum FlowOperation { 150 FLOW_OP_IF, 151 FLOW_OP_RETURN, 152 //FLOW_OP_FOR, 153 //FLOW_OP_WHILE, 154 //FLOW_OP_DO, 155 //FLOW_OP_BREAK, 156 //FLOW_OP_CONTINUE, 157 158 }; 159 160 struct Node { 161 162 enum Type { 163 TYPE_PROGRAM, 164 TYPE_FUNCTION, 165 TYPE_BLOCK, 166 TYPE_VARIABLE, 167 TYPE_CONSTANT, 168 TYPE_OPERATOR, 169 TYPE_CONTROL_FLOW, 170 TYPE_MEMBER 171 }; 172 173 Node *parent; 174 Type type; 175 get_datatypeNode176 virtual DataType get_datatype() const { return TYPE_VOID; } 177 ~NodeNode178 virtual ~Node() {} 179 }; 180 181 struct OperatorNode : public Node { 182 183 DataType return_cache; 184 Operator op; 185 Vector<Node *> arguments; get_datatypeOperatorNode186 virtual DataType get_datatype() const { return return_cache; } 187 OperatorNodeOperatorNode188 OperatorNode() { 189 type = TYPE_OPERATOR; 190 return_cache = TYPE_VOID; 191 } 192 }; 193 194 struct VariableNode : public Node { 195 bool uniform; 196 DataType datatype_cache; 197 StringName name; get_datatypeVariableNode198 virtual DataType get_datatype() const { return datatype_cache; } 199 VariableNodeVariableNode200 VariableNode() { 201 type = TYPE_VARIABLE; 202 datatype_cache = TYPE_VOID; 203 uniform = false; 204 } 205 }; 206 207 struct ConstantNode : public Node { 208 209 DataType datatype; 210 Variant value; get_datatypeConstantNode211 virtual DataType get_datatype() const { return datatype; } 212 ConstantNodeConstantNode213 ConstantNode() { type = TYPE_CONSTANT; } 214 }; 215 216 struct BlockNode : public Node { 217 218 Map<StringName, DataType> variables; 219 List<Node *> statements; BlockNodeBlockNode220 BlockNode() { type = TYPE_BLOCK; } 221 }; 222 223 struct ControlFlowNode : public Node { 224 225 FlowOperation flow_op; 226 Vector<Node *> statements; ControlFlowNodeControlFlowNode227 ControlFlowNode() { 228 type = TYPE_CONTROL_FLOW; 229 flow_op = FLOW_OP_IF; 230 } 231 }; 232 233 struct MemberNode : public Node { 234 235 DataType basetype; 236 DataType datatype; 237 StringName name; 238 Node *owner; get_datatypeMemberNode239 virtual DataType get_datatype() const { return datatype; } MemberNodeMemberNode240 MemberNode() { type = TYPE_MEMBER; } 241 }; 242 243 struct FunctionNode : public Node { 244 245 struct Argument { 246 247 StringName name; 248 DataType type; 249 }; 250 251 StringName name; 252 DataType return_type; 253 Vector<Argument> arguments; 254 BlockNode *body; 255 FunctionNodeFunctionNode256 FunctionNode() { type = TYPE_FUNCTION; } 257 }; 258 259 struct Uniform { 260 261 int order; 262 DataType type; 263 Variant default_value; 264 }; 265 266 struct ProgramNode : public Node { 267 268 struct Function { 269 StringName name; 270 FunctionNode *function; 271 }; 272 273 Map<StringName, DataType> builtin_variables; 274 Map<StringName, Uniform> uniforms; 275 276 Vector<Function> functions; 277 BlockNode *body; 278 ProgramNodeProgramNode279 ProgramNode() { type = TYPE_PROGRAM; } 280 }; 281 282 struct Expression { 283 284 bool is_op; 285 union { 286 TokenType op; 287 Node *node; 288 }; 289 }; 290 291 typedef Error (*CompileFunc)(void *, ProgramNode *); 292 293 struct VarInfo { 294 295 StringName name; 296 DataType type; 297 }; 298 299 private: 300 static const char *token_names[TK_MAX]; 301 302 struct Token { 303 304 TokenType type; 305 StringName text; 306 uint16_t line, col; 307 308 Token(TokenType p_type = TK_EMPTY, const String &p_text = String()) { 309 type = p_type; 310 text = p_text; 311 line = 0; 312 col = 0; 313 } 314 }; 315 316 static Token read_token(const CharType *p_text, int p_len, int &r_line, int &r_chars); 317 static Error tokenize(const String &p_text, Vector<Token> *p_tokens, String *r_error, int *r_err_line, int *r_err_column); 318 319 class Parser { 320 321 Vector<Token> tokens; 322 int pos; 323 String error; 324 325 public: set_error(const String & p_error)326 void set_error(const String &p_error) { error = p_error; } get_error(String * r_error,int * r_line,int * r_column)327 void get_error(String *r_error, int *r_line, int *r_column) { 328 329 *r_error = error; 330 *r_line = get_next_token(0).line; 331 *r_column = get_next_token(0).col; 332 } 333 334 Token get_next_token(int ofs = 0) const { 335 int idx = pos + ofs; 336 if (idx < 0 || idx >= tokens.size()) return Token(TK_ERROR); 337 return tokens[idx]; 338 } 339 TokenType get_next_token_type(int ofs = 0) const { 340 int idx = pos + ofs; 341 if (idx < 0 || idx >= tokens.size()) return TK_ERROR; 342 return tokens[idx].type; 343 } 344 void advance(int p_amount = 1) { pos += p_amount; } is_at_end()345 bool is_at_end() const { return pos >= tokens.size(); } 346 347 ProgramNode *program; 348 template <class T> create_node(Node * p_parent)349 T *create_node(Node *p_parent) { 350 T *n = memnew(T); 351 nodegc.push_back(n); 352 n->parent = p_parent; 353 return n; 354 } 355 List<Node *> nodegc; 356 Parser(const Vector<Token> & p_tokens)357 Parser(const Vector<Token> &p_tokens) { 358 tokens = p_tokens; 359 pos = 0; 360 } 361 }; 362 363 struct IntrinsicFuncDef { 364 365 enum { MAX_ARGS = 5 }; 366 const char *name; 367 DataType rettype; 368 const DataType args[MAX_ARGS]; 369 }; 370 371 static const IntrinsicFuncDef intrinsic_func_defs[]; 372 373 struct OperatorDef { 374 375 enum { MAX_ARGS = 2 }; 376 Operator op; 377 DataType rettype; 378 const DataType args[MAX_ARGS]; 379 }; 380 381 static const OperatorDef operator_defs[]; 382 383 struct BuiltinsDef { 384 385 const char *name; 386 DataType type; 387 }; 388 389 static const BuiltinsDef vertex_builtins_defs[]; 390 static const BuiltinsDef fragment_builtins_defs[]; 391 static const BuiltinsDef light_builtins_defs[]; 392 393 static const BuiltinsDef ci_vertex_builtins_defs[]; 394 static const BuiltinsDef ci_fragment_builtins_defs[]; 395 static const BuiltinsDef ci_light_builtins_defs[]; 396 397 static const BuiltinsDef postprocess_fragment_builtins_defs[]; 398 399 static DataType get_token_datatype(TokenType p_type); 400 static String get_datatype_name(DataType p_type); 401 static bool is_token_datatype(TokenType p_type); 402 static bool is_token_nonvoid_datatype(TokenType p_type); 403 404 static bool test_existing_identifier(Node *p_node, const StringName p_identifier, bool p_func = true, bool p_var = true, bool p_builtin = true); 405 406 static bool parser_is_at_function(Parser &parser); 407 static DataType compute_node_type(Node *p_node); 408 409 static Node *validate_function_call(Parser &parser, OperatorNode *p_func); 410 static Node *validate_operator(Parser &parser, OperatorNode *p_func); 411 static bool is_token_operator(TokenType p_type); 412 static Operator get_token_operator(TokenType p_type); 413 414 static Error parse_expression(Parser &parser, Node *p_parent, Node **r_expr); 415 416 static Error parse_variable_declaration(Parser &parser, BlockNode *p_block); 417 static Error parse_function(Parser &parser, BlockNode *p_block); 418 static Error parse_flow_if(Parser &parser, Node *p_parent, Node **r_statement); 419 static Error parse_flow_return(Parser &parser, Node *p_parent, Node **r_statement); 420 static Error parse_statement(Parser &parser, Node *p_parent, Node **r_statement); 421 static Error parse_block(Parser &parser, BlockNode *p_block); 422 423 static Error parse(const Vector<Token> &p_tokens, ShaderType p_type, CompileFunc p_compile_func, void *p_userdata, String *r_error, int *r_err_line, int *r_err_column); 424 425 ; 426 427 public: 428 static void get_keyword_list(ShaderType p_type, List<String> *p_keywords); 429 430 static Error compile(const String &p_code, ShaderType p_type, CompileFunc p_compile_func, void *p_userdata, String *r_error, int *r_err_line, int *r_err_column); 431 static String lex_debug(const String &p_code); 432 }; 433 434 #endif // SHADER_LANGUAGE_H 435