2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* shader_language.h */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* http://www.godotengine.org */
|
|
|
|
/*************************************************************************/
|
2017-01-01 22:01:57 +01:00
|
|
|
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
|
2014-02-10 02:10:30 +01:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
|
|
|
#ifndef SHADER_LANGUAGE_H
|
|
|
|
#define SHADER_LANGUAGE_H
|
|
|
|
|
|
|
|
#include "typedefs.h"
|
|
|
|
#include "ustring.h"
|
|
|
|
#include "list.h"
|
|
|
|
#include "string_db.h"
|
|
|
|
#include "map.h"
|
|
|
|
#include "variant.h"
|
|
|
|
|
|
|
|
|
|
|
|
class ShaderLanguage {
|
2014-09-18 04:23:42 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
public:
|
|
|
|
|
2014-09-18 04:23:42 +02:00
|
|
|
enum TokenType {
|
|
|
|
|
|
|
|
TK_EMPTY,
|
2016-10-03 21:33:42 +02:00
|
|
|
TK_IDENTIFIER,
|
2014-09-18 04:23:42 +02:00
|
|
|
TK_TRUE,
|
|
|
|
TK_FALSE,
|
|
|
|
TK_REAL_CONSTANT,
|
2016-10-03 21:33:42 +02:00
|
|
|
TK_INT_CONSTANT,
|
2014-09-18 04:23:42 +02:00
|
|
|
TK_TYPE_VOID,
|
|
|
|
TK_TYPE_BOOL,
|
2016-10-03 21:33:42 +02:00
|
|
|
TK_TYPE_BVEC2,
|
|
|
|
TK_TYPE_BVEC3,
|
|
|
|
TK_TYPE_BVEC4,
|
|
|
|
TK_TYPE_INT,
|
|
|
|
TK_TYPE_IVEC2,
|
|
|
|
TK_TYPE_IVEC3,
|
|
|
|
TK_TYPE_IVEC4,
|
|
|
|
TK_TYPE_UINT,
|
|
|
|
TK_TYPE_UVEC2,
|
|
|
|
TK_TYPE_UVEC3,
|
|
|
|
TK_TYPE_UVEC4,
|
2014-09-18 04:23:42 +02:00
|
|
|
TK_TYPE_FLOAT,
|
|
|
|
TK_TYPE_VEC2,
|
|
|
|
TK_TYPE_VEC3,
|
|
|
|
TK_TYPE_VEC4,
|
2015-03-10 04:53:09 +01:00
|
|
|
TK_TYPE_MAT2,
|
2014-09-18 04:23:42 +02:00
|
|
|
TK_TYPE_MAT3,
|
|
|
|
TK_TYPE_MAT4,
|
2016-10-03 21:33:42 +02:00
|
|
|
TK_TYPE_SAMPLER2D,
|
|
|
|
TK_TYPE_ISAMPLER2D,
|
|
|
|
TK_TYPE_USAMPLER2D,
|
|
|
|
TK_TYPE_SAMPLERCUBE,
|
|
|
|
TK_PRECISION_LOW,
|
|
|
|
TK_PRECISION_MID,
|
|
|
|
TK_PRECISION_HIGH,
|
2014-09-18 04:23:42 +02:00
|
|
|
TK_OP_EQUAL,
|
|
|
|
TK_OP_NOT_EQUAL,
|
|
|
|
TK_OP_LESS,
|
|
|
|
TK_OP_LESS_EQUAL,
|
|
|
|
TK_OP_GREATER,
|
|
|
|
TK_OP_GREATER_EQUAL,
|
|
|
|
TK_OP_AND,
|
|
|
|
TK_OP_OR,
|
|
|
|
TK_OP_NOT,
|
|
|
|
TK_OP_ADD,
|
|
|
|
TK_OP_SUB,
|
|
|
|
TK_OP_MUL,
|
|
|
|
TK_OP_DIV,
|
2016-10-03 21:33:42 +02:00
|
|
|
TK_OP_MOD,
|
|
|
|
TK_OP_SHIFT_LEFT,
|
|
|
|
TK_OP_SHIFT_RIGHT,
|
2014-09-18 04:23:42 +02:00
|
|
|
TK_OP_ASSIGN,
|
|
|
|
TK_OP_ASSIGN_ADD,
|
|
|
|
TK_OP_ASSIGN_SUB,
|
|
|
|
TK_OP_ASSIGN_MUL,
|
|
|
|
TK_OP_ASSIGN_DIV,
|
2016-10-03 21:33:42 +02:00
|
|
|
TK_OP_ASSIGN_MOD,
|
|
|
|
TK_OP_ASSIGN_SHIFT_LEFT,
|
|
|
|
TK_OP_ASSIGN_SHIFT_RIGHT,
|
|
|
|
TK_OP_ASSIGN_BIT_AND,
|
|
|
|
TK_OP_ASSIGN_BIT_OR,
|
|
|
|
TK_OP_ASSIGN_BIT_XOR,
|
|
|
|
TK_OP_BIT_AND,
|
|
|
|
TK_OP_BIT_OR,
|
|
|
|
TK_OP_BIT_XOR,
|
|
|
|
TK_OP_BIT_INVERT,
|
|
|
|
TK_OP_INCREMENT,
|
|
|
|
TK_OP_DECREMENT,
|
2014-09-18 04:23:42 +02:00
|
|
|
TK_CF_IF,
|
|
|
|
TK_CF_ELSE,
|
2016-10-03 21:33:42 +02:00
|
|
|
TK_CF_FOR,
|
|
|
|
TK_CF_WHILE,
|
|
|
|
TK_CF_DO,
|
|
|
|
TK_CF_SWITCH,
|
|
|
|
TK_CF_CASE,
|
|
|
|
TK_CF_BREAK,
|
|
|
|
TK_CF_CONTINUE,
|
2014-09-18 04:23:42 +02:00
|
|
|
TK_CF_RETURN,
|
|
|
|
TK_BRACKET_OPEN,
|
|
|
|
TK_BRACKET_CLOSE,
|
|
|
|
TK_CURLY_BRACKET_OPEN,
|
|
|
|
TK_CURLY_BRACKET_CLOSE,
|
|
|
|
TK_PARENTHESIS_OPEN,
|
|
|
|
TK_PARENTHESIS_CLOSE,
|
2016-10-03 21:33:42 +02:00
|
|
|
TK_QUESTION,
|
2014-09-18 04:23:42 +02:00
|
|
|
TK_COMMA,
|
2016-10-03 21:33:42 +02:00
|
|
|
TK_COLON,
|
2014-09-18 04:23:42 +02:00
|
|
|
TK_SEMICOLON,
|
|
|
|
TK_PERIOD,
|
|
|
|
TK_UNIFORM,
|
2016-10-03 21:33:42 +02:00
|
|
|
TK_VARYING,
|
|
|
|
TK_RENDER_MODE,
|
|
|
|
TK_HINT_WHITE_TEXTURE,
|
|
|
|
TK_HINT_BLACK_TEXTURE,
|
|
|
|
TK_HINT_NORMAL_TEXTURE,
|
2016-11-21 02:49:53 +01:00
|
|
|
TK_HINT_ANISO_TEXTURE,
|
2016-10-07 16:31:18 +02:00
|
|
|
TK_HINT_ALBEDO_TEXTURE,
|
2016-11-21 02:49:53 +01:00
|
|
|
TK_HINT_BLACK_ALBEDO_TEXTURE,
|
2016-10-07 16:31:18 +02:00
|
|
|
TK_HINT_COLOR,
|
2016-10-03 21:33:42 +02:00
|
|
|
TK_HINT_RANGE,
|
2016-10-07 16:31:18 +02:00
|
|
|
TK_CURSOR,
|
2014-09-18 04:23:42 +02:00
|
|
|
TK_ERROR,
|
2016-10-03 21:33:42 +02:00
|
|
|
TK_EOF,
|
2014-09-18 04:23:42 +02:00
|
|
|
TK_MAX
|
|
|
|
};
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
/* COMPILER */
|
|
|
|
|
2017-01-04 11:56:56 +01:00
|
|
|
// lame work around to Apple defining this as a macro in 10.12 SDK
|
|
|
|
#ifdef TYPE_BOOL
|
|
|
|
#undef TYPE_BOOL
|
|
|
|
#endif
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
enum DataType {
|
|
|
|
TYPE_VOID,
|
|
|
|
TYPE_BOOL,
|
2016-10-03 21:33:42 +02:00
|
|
|
TYPE_BVEC2,
|
|
|
|
TYPE_BVEC3,
|
|
|
|
TYPE_BVEC4,
|
|
|
|
TYPE_INT,
|
|
|
|
TYPE_IVEC2,
|
|
|
|
TYPE_IVEC3,
|
|
|
|
TYPE_IVEC4,
|
|
|
|
TYPE_UINT,
|
|
|
|
TYPE_UVEC2,
|
|
|
|
TYPE_UVEC3,
|
|
|
|
TYPE_UVEC4,
|
2014-02-10 02:10:30 +01:00
|
|
|
TYPE_FLOAT,
|
|
|
|
TYPE_VEC2,
|
|
|
|
TYPE_VEC3,
|
|
|
|
TYPE_VEC4,
|
2015-03-10 04:53:09 +01:00
|
|
|
TYPE_MAT2,
|
2014-02-10 02:10:30 +01:00
|
|
|
TYPE_MAT3,
|
|
|
|
TYPE_MAT4,
|
2016-10-03 21:33:42 +02:00
|
|
|
TYPE_SAMPLER2D,
|
|
|
|
TYPE_ISAMPLER2D,
|
|
|
|
TYPE_USAMPLER2D,
|
|
|
|
TYPE_SAMPLERCUBE,
|
|
|
|
};
|
|
|
|
|
|
|
|
enum DataPrecision {
|
|
|
|
PRECISION_LOWP,
|
|
|
|
PRECISION_MEDIUMP,
|
|
|
|
PRECISION_HIGHP,
|
|
|
|
PRECISION_DEFAULT,
|
2014-02-10 02:10:30 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
enum Operator {
|
2016-10-03 21:33:42 +02:00
|
|
|
OP_EQUAL,
|
|
|
|
OP_NOT_EQUAL,
|
|
|
|
OP_LESS,
|
|
|
|
OP_LESS_EQUAL,
|
|
|
|
OP_GREATER,
|
|
|
|
OP_GREATER_EQUAL,
|
|
|
|
OP_AND,
|
|
|
|
OP_OR,
|
|
|
|
OP_NOT,
|
|
|
|
OP_NEGATE,
|
2014-02-10 02:10:30 +01:00
|
|
|
OP_ADD,
|
|
|
|
OP_SUB,
|
|
|
|
OP_MUL,
|
|
|
|
OP_DIV,
|
2016-10-03 21:33:42 +02:00
|
|
|
OP_MOD,
|
|
|
|
OP_SHIFT_LEFT,
|
|
|
|
OP_SHIFT_RIGHT,
|
|
|
|
OP_ASSIGN,
|
2014-02-10 02:10:30 +01:00
|
|
|
OP_ASSIGN_ADD,
|
|
|
|
OP_ASSIGN_SUB,
|
|
|
|
OP_ASSIGN_MUL,
|
|
|
|
OP_ASSIGN_DIV,
|
2016-10-03 21:33:42 +02:00
|
|
|
OP_ASSIGN_MOD,
|
|
|
|
OP_ASSIGN_SHIFT_LEFT,
|
|
|
|
OP_ASSIGN_SHIFT_RIGHT,
|
|
|
|
OP_ASSIGN_BIT_AND,
|
|
|
|
OP_ASSIGN_BIT_OR,
|
|
|
|
OP_ASSIGN_BIT_XOR,
|
|
|
|
OP_BIT_AND,
|
|
|
|
OP_BIT_OR,
|
|
|
|
OP_BIT_XOR,
|
|
|
|
OP_BIT_INVERT,
|
|
|
|
OP_INCREMENT,
|
|
|
|
OP_DECREMENT,
|
|
|
|
OP_SELECT_IF,
|
|
|
|
OP_SELECT_ELSE, //used only internally, then only IF appears with 3 arguments
|
|
|
|
OP_POST_INCREMENT,
|
|
|
|
OP_POST_DECREMENT,
|
2014-02-10 02:10:30 +01:00
|
|
|
OP_CALL,
|
|
|
|
OP_CONSTRUCT,
|
|
|
|
OP_MAX
|
|
|
|
};
|
|
|
|
|
|
|
|
enum FlowOperation {
|
|
|
|
FLOW_OP_IF,
|
|
|
|
FLOW_OP_RETURN,
|
2016-10-03 21:33:42 +02:00
|
|
|
FLOW_OP_FOR,
|
|
|
|
FLOW_OP_WHILE,
|
|
|
|
FLOW_OP_DO,
|
|
|
|
FLOW_OP_BREAK,
|
|
|
|
FLOW_OP_SWITCH,
|
|
|
|
FLOW_OP_CONTINUE
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Node {
|
|
|
|
|
2016-10-03 21:33:42 +02:00
|
|
|
Node *next;
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
enum Type {
|
2016-10-03 21:33:42 +02:00
|
|
|
TYPE_SHADER,
|
2014-02-10 02:10:30 +01:00
|
|
|
TYPE_FUNCTION,
|
|
|
|
TYPE_BLOCK,
|
|
|
|
TYPE_VARIABLE,
|
|
|
|
TYPE_CONSTANT,
|
|
|
|
TYPE_OPERATOR,
|
|
|
|
TYPE_CONTROL_FLOW,
|
|
|
|
TYPE_MEMBER
|
|
|
|
};
|
|
|
|
|
|
|
|
Type type;
|
|
|
|
|
|
|
|
virtual DataType get_datatype() const { return TYPE_VOID; }
|
|
|
|
|
|
|
|
virtual ~Node() {}
|
|
|
|
};
|
|
|
|
|
2016-10-03 21:33:42 +02:00
|
|
|
template<class T>
|
|
|
|
T* alloc_node() {
|
|
|
|
T* node = memnew(T);
|
|
|
|
node->next=nodes;
|
|
|
|
nodes=node;
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
Node *nodes;
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
struct OperatorNode : public Node {
|
|
|
|
|
|
|
|
DataType return_cache;
|
2016-10-03 21:33:42 +02:00
|
|
|
DataPrecision return_precision_cache;
|
2014-02-10 02:10:30 +01:00
|
|
|
Operator op;
|
|
|
|
Vector<Node*> arguments;
|
|
|
|
virtual DataType get_datatype() const { return return_cache; }
|
|
|
|
|
2016-10-03 21:33:42 +02:00
|
|
|
OperatorNode() { type=TYPE_OPERATOR; return_cache=TYPE_VOID; return_precision_cache=PRECISION_DEFAULT; }
|
2014-02-10 02:10:30 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
struct VariableNode : public Node {
|
|
|
|
DataType datatype_cache;
|
|
|
|
StringName name;
|
|
|
|
virtual DataType get_datatype() const { return datatype_cache; }
|
|
|
|
|
2016-10-03 21:33:42 +02:00
|
|
|
VariableNode() { type=TYPE_VARIABLE; datatype_cache=TYPE_VOID; }
|
2014-02-10 02:10:30 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
struct ConstantNode : public Node {
|
|
|
|
|
|
|
|
DataType datatype;
|
2016-10-07 16:31:18 +02:00
|
|
|
|
|
|
|
union Value {
|
|
|
|
bool boolean;
|
|
|
|
float real;
|
|
|
|
int32_t sint;
|
|
|
|
uint32_t uint;
|
|
|
|
};
|
|
|
|
|
|
|
|
Vector<Value> values;
|
2014-02-10 02:10:30 +01:00
|
|
|
virtual DataType get_datatype() const { return datatype; }
|
|
|
|
|
|
|
|
ConstantNode() { type=TYPE_CONSTANT; }
|
|
|
|
};
|
|
|
|
|
2016-10-03 21:33:42 +02:00
|
|
|
struct FunctionNode;
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
struct BlockNode : public Node {
|
2016-10-03 21:33:42 +02:00
|
|
|
FunctionNode *parent_function;
|
|
|
|
BlockNode *parent_block;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-10-03 21:33:42 +02:00
|
|
|
struct Variable {
|
|
|
|
DataType type;
|
|
|
|
DataPrecision precision;
|
2016-10-07 16:31:18 +02:00
|
|
|
int line; //for completion
|
2016-10-03 21:33:42 +02:00
|
|
|
};
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-10-03 21:33:42 +02:00
|
|
|
Map<StringName,Variable> variables;
|
2014-02-10 02:10:30 +01:00
|
|
|
List<Node*> statements;
|
2016-10-03 21:33:42 +02:00
|
|
|
BlockNode() { type=TYPE_BLOCK; parent_block=NULL; parent_function=NULL; }
|
2014-02-10 02:10:30 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
struct ControlFlowNode : public Node {
|
|
|
|
|
|
|
|
FlowOperation flow_op;
|
2016-10-07 16:31:18 +02:00
|
|
|
Vector<Node*> expressions;
|
2016-10-03 21:33:42 +02:00
|
|
|
Vector<BlockNode*> blocks;
|
2014-02-10 02:10:30 +01:00
|
|
|
ControlFlowNode() { type=TYPE_CONTROL_FLOW; flow_op=FLOW_OP_IF;}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct MemberNode : public Node {
|
|
|
|
|
|
|
|
DataType basetype;
|
|
|
|
DataType datatype;
|
|
|
|
StringName name;
|
|
|
|
Node* owner;
|
|
|
|
virtual DataType get_datatype() const { return datatype; }
|
|
|
|
MemberNode() { type=TYPE_MEMBER; }
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct FunctionNode : public Node {
|
|
|
|
|
|
|
|
|
|
|
|
struct Argument {
|
|
|
|
|
|
|
|
StringName name;
|
|
|
|
DataType type;
|
2016-10-03 21:33:42 +02:00
|
|
|
DataPrecision precision;
|
2014-02-10 02:10:30 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
StringName name;
|
|
|
|
DataType return_type;
|
2016-10-03 21:33:42 +02:00
|
|
|
DataPrecision return_precision;
|
2014-02-10 02:10:30 +01:00
|
|
|
Vector<Argument> arguments;
|
|
|
|
BlockNode *body;
|
|
|
|
|
2016-10-03 21:33:42 +02:00
|
|
|
FunctionNode() { type=TYPE_FUNCTION; return_precision=PRECISION_DEFAULT; }
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-10-03 21:33:42 +02:00
|
|
|
struct ShaderNode : public Node {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
struct Function {
|
|
|
|
StringName name;
|
|
|
|
FunctionNode*function;
|
2016-10-10 23:31:01 +02:00
|
|
|
Set<StringName> uses_function;
|
2016-10-03 21:33:42 +02:00
|
|
|
bool callable;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Varying {
|
|
|
|
DataType type;
|
|
|
|
DataPrecision precission;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Uniform {
|
|
|
|
enum Hint {
|
|
|
|
HINT_NONE,
|
2016-10-07 16:31:18 +02:00
|
|
|
HINT_COLOR,
|
2016-10-03 21:33:42 +02:00
|
|
|
HINT_RANGE,
|
2016-10-07 16:31:18 +02:00
|
|
|
HINT_ALBEDO,
|
2016-11-21 02:49:53 +01:00
|
|
|
HINT_BLACK_ALBEDO,
|
2016-10-07 16:31:18 +02:00
|
|
|
HINT_NORMAL,
|
|
|
|
HINT_BLACK,
|
|
|
|
HINT_WHITE,
|
2016-11-21 02:49:53 +01:00
|
|
|
HINT_ANISO,
|
2016-10-07 16:31:18 +02:00
|
|
|
HINT_MAX
|
2016-10-03 21:33:42 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
int order;
|
2016-10-10 23:31:01 +02:00
|
|
|
int texture_order;
|
2016-10-03 21:33:42 +02:00
|
|
|
DataType type;
|
|
|
|
DataPrecision precission;
|
2016-10-07 16:31:18 +02:00
|
|
|
Vector<ConstantNode::Value> default_value;
|
2016-10-03 21:33:42 +02:00
|
|
|
Hint hint;
|
|
|
|
float hint_range[3];
|
|
|
|
|
|
|
|
Uniform() { hint=HINT_NONE; hint_range[0]=0; hint_range[1]=1; hint_range[2]=0.001;}
|
2014-02-10 02:10:30 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-10-03 21:33:42 +02:00
|
|
|
Map<StringName,Varying> varyings;
|
2014-02-10 02:10:30 +01:00
|
|
|
Map<StringName,Uniform> uniforms;
|
2016-10-10 23:31:01 +02:00
|
|
|
Vector<StringName> render_modes;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
Vector<Function> functions;
|
|
|
|
|
2016-10-03 21:33:42 +02:00
|
|
|
ShaderNode() { type=TYPE_SHADER; }
|
2014-02-10 02:10:30 +01:00
|
|
|
};
|
|
|
|
|
2014-09-18 04:23:42 +02:00
|
|
|
|
|
|
|
struct Expression {
|
|
|
|
|
|
|
|
bool is_op;
|
|
|
|
union {
|
2016-10-03 21:33:42 +02:00
|
|
|
Operator op;
|
2014-09-18 04:23:42 +02:00
|
|
|
Node *node;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2016-10-03 21:33:42 +02:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
struct VarInfo {
|
|
|
|
|
|
|
|
StringName name;
|
|
|
|
DataType type;
|
|
|
|
};
|
|
|
|
|
2016-10-03 21:33:42 +02:00
|
|
|
enum CompletionType {
|
|
|
|
COMPLETION_NONE,
|
2016-10-07 16:31:18 +02:00
|
|
|
COMPLETION_RENDER_MODE,
|
|
|
|
COMPLETION_MAIN_FUNCTION,
|
2016-10-03 21:33:42 +02:00
|
|
|
COMPLETION_IDENTIFIER,
|
2016-10-07 16:31:18 +02:00
|
|
|
COMPLETION_FUNCTION_CALL,
|
2016-10-03 21:33:42 +02:00
|
|
|
COMPLETION_CALL_ARGUMENTS,
|
|
|
|
COMPLETION_INDEX,
|
|
|
|
};
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
struct Token {
|
|
|
|
|
|
|
|
TokenType type;
|
|
|
|
StringName text;
|
2016-10-03 21:33:42 +02:00
|
|
|
double constant;
|
|
|
|
uint16_t line;
|
2014-02-10 02:10:30 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-10-07 16:31:18 +02:00
|
|
|
static String get_operator_text(Operator p_op);
|
2016-10-03 21:33:42 +02:00
|
|
|
static String get_token_text(Token p_token);
|
2016-10-07 16:31:18 +02:00
|
|
|
|
2016-10-03 21:33:42 +02:00
|
|
|
static bool is_token_datatype(TokenType p_type);
|
|
|
|
static DataType get_token_datatype(TokenType p_type);
|
|
|
|
static bool is_token_precision(TokenType p_type);
|
|
|
|
static DataPrecision get_token_precision(TokenType p_type);
|
|
|
|
static String get_datatype_name(DataType p_type);
|
|
|
|
static bool is_token_nonvoid_datatype(TokenType p_type);
|
|
|
|
static bool is_token_operator(TokenType p_type);
|
|
|
|
|
2016-10-07 16:31:18 +02:00
|
|
|
static bool convert_constant(ConstantNode* p_constant, DataType p_to_type,ConstantNode::Value *p_value=NULL);
|
|
|
|
static DataType get_scalar_type(DataType p_type);
|
|
|
|
static bool is_scalar_type(DataType p_type);
|
2016-10-10 23:31:01 +02:00
|
|
|
static bool is_sampler_type(DataType p_type);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-10-07 16:31:18 +02:00
|
|
|
static void get_keyword_list(List<String> *r_keywords);
|
2016-10-10 23:31:01 +02:00
|
|
|
static void get_builtin_funcs(List<String> *r_keywords);
|
2016-10-03 21:33:42 +02:00
|
|
|
private:
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-10-07 16:31:18 +02:00
|
|
|
struct KeyWord { TokenType token; const char *text;};
|
|
|
|
static const KeyWord keyword_list[];
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-10-03 21:33:42 +02:00
|
|
|
bool error_set;
|
|
|
|
String error_str;
|
|
|
|
int error_line;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-10-03 21:33:42 +02:00
|
|
|
String code;
|
|
|
|
int char_idx;
|
|
|
|
int tk_line;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-10-10 23:31:01 +02:00
|
|
|
StringName current_function;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-10-07 16:31:18 +02:00
|
|
|
struct TkPos {
|
|
|
|
int char_idx;
|
|
|
|
int tk_line;
|
|
|
|
};
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-10-07 16:31:18 +02:00
|
|
|
TkPos _get_tkpos() {
|
|
|
|
TkPos tkp;
|
|
|
|
tkp.char_idx=char_idx;
|
|
|
|
tkp.tk_line=tk_line;
|
|
|
|
return tkp;
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
|
2016-10-07 16:31:18 +02:00
|
|
|
void _set_tkpos(TkPos p_pos) {
|
|
|
|
char_idx=p_pos.char_idx;
|
|
|
|
tk_line=p_pos.tk_line;
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-10-03 21:33:42 +02:00
|
|
|
void _set_error(const String& p_str) {
|
|
|
|
if (error_set)
|
|
|
|
return;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-10-07 16:31:18 +02:00
|
|
|
error_line=tk_line;
|
2016-10-03 21:33:42 +02:00
|
|
|
error_set=true;
|
|
|
|
error_str=p_str;
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
|
2016-10-03 21:33:42 +02:00
|
|
|
static const char * token_names[TK_MAX];
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-10-03 21:33:42 +02:00
|
|
|
Token _make_token(TokenType p_type, const StringName& p_text=StringName());
|
|
|
|
Token _get_token();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-10-03 21:33:42 +02:00
|
|
|
ShaderNode *shader;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-10-03 21:33:42 +02:00
|
|
|
enum IdentifierType {
|
|
|
|
IDENTIFIER_FUNCTION,
|
|
|
|
IDENTIFIER_UNIFORM,
|
|
|
|
IDENTIFIER_VARYING,
|
|
|
|
IDENTIFIER_FUNCTION_ARGUMENT,
|
|
|
|
IDENTIFIER_LOCAL_VAR,
|
|
|
|
IDENTIFIER_BUILTIN_VAR,
|
2014-02-10 02:10:30 +01:00
|
|
|
};
|
|
|
|
|
2016-10-03 21:33:42 +02:00
|
|
|
bool _find_identifier(const BlockNode* p_block,const Map<StringName, DataType> &p_builtin_types,const StringName& p_identifier, DataType *r_data_type=NULL, IdentifierType *r_type=NULL);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-10-03 21:33:42 +02:00
|
|
|
bool _validate_operator(OperatorNode *p_op,DataType *r_ret_type=NULL);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
|
2016-10-07 16:31:18 +02:00
|
|
|
struct BuiltinFuncDef {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
enum { MAX_ARGS=5 };
|
|
|
|
const char* name;
|
|
|
|
DataType rettype;
|
|
|
|
const DataType args[MAX_ARGS];
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2016-10-07 16:31:18 +02:00
|
|
|
CompletionType completion_type;
|
|
|
|
int completion_line;
|
|
|
|
BlockNode *completion_block;
|
|
|
|
DataType completion_base;
|
|
|
|
StringName completion_function;
|
|
|
|
int completion_argument;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
|
2016-10-07 16:31:18 +02:00
|
|
|
bool _get_completable_identifier(BlockNode *p_block, CompletionType p_type, StringName& identifier);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-10-07 16:31:18 +02:00
|
|
|
static const BuiltinFuncDef builtin_func_defs[];
|
2016-10-03 21:33:42 +02:00
|
|
|
bool _validate_function_call(BlockNode* p_block, OperatorNode *p_func,DataType *r_ret_type);
|
2015-01-11 15:43:31 +01:00
|
|
|
|
2016-10-07 16:31:18 +02:00
|
|
|
bool _parse_function_arguments(BlockNode *p_block, const Map<StringName,DataType> &p_builtin_types, OperatorNode* p_func, int *r_complete_arg=NULL);
|
2015-01-11 15:43:31 +01:00
|
|
|
|
2016-10-03 21:33:42 +02:00
|
|
|
Node* _parse_expression(BlockNode *p_block, const Map<StringName,DataType> &p_builtin_types);
|
2015-01-11 15:43:31 +01:00
|
|
|
|
2016-10-03 21:33:42 +02:00
|
|
|
ShaderLanguage::Node* _reduce_expression(BlockNode *p_block, ShaderLanguage::Node *p_node);
|
|
|
|
Node* _parse_and_reduce_expression(BlockNode *p_block,const Map<StringName,DataType> &p_builtin_types);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-10-03 21:33:42 +02:00
|
|
|
Error _parse_block(BlockNode *p_block, const Map<StringName, DataType> &p_builtin_types, bool p_just_one=false, bool p_can_break=false, bool p_can_continue=false);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-10-03 21:33:42 +02:00
|
|
|
Error _parse_shader(const Map< StringName, Map<StringName,DataType> > &p_functions, const Set<String> &p_render_modes);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
2017-01-14 12:26:56 +01:00
|
|
|
//static void get_keyword_list(ShaderType p_type,List<String> *p_keywords);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-10-03 21:33:42 +02:00
|
|
|
void clear();
|
|
|
|
Error compile(const String& p_code,const Map< StringName, Map<StringName,DataType> > &p_functions,const Set<String>& p_render_modes);
|
2016-10-07 16:31:18 +02:00
|
|
|
Error complete(const String& p_code,const Map< StringName, Map<StringName,DataType> > &p_functions,const Set<String>& p_render_modes,List<String>* r_options,String& r_call_hint);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
2016-10-03 21:33:42 +02:00
|
|
|
String get_error_text();
|
|
|
|
int get_error_line();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-10-07 16:31:18 +02:00
|
|
|
ShaderNode *get_shader();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-10-07 16:31:18 +02:00
|
|
|
String token_debug(const String& p_code);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-10-03 21:33:42 +02:00
|
|
|
ShaderLanguage();
|
|
|
|
~ShaderLanguage();
|
2014-02-10 02:10:30 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#endif // SHADER_LANGUAGE_H
|