shader_graph.h
1 /*************************************************************************/
2 /* shader_graph.h */
3 /*************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* http://www.godotengine.org */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
9 /* */
10 /* Permission is hereby granted, free of charge, to any person obtaining */
11 /* a copy of this software and associated documentation files (the */
12 /* "Software"), to deal in the Software without restriction, including */
13 /* without limitation the rights to use, copy, modify, merge, publish, */
14 /* distribute, sublicense, and/or sell copies of the Software, and to */
15 /* permit persons to whom the Software is furnished to do so, subject to */
16 /* the following conditions: */
17 /* */
18 /* The above copyright notice and this permission notice shall be */
19 /* included in all copies or substantial portions of the Software. */
20 /* */
21 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
22 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
23 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
24 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
25 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
26 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
27 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
28 /*************************************************************************/
29 #ifndef SHADER_GRAPH_H
30 #define SHADER_GRAPH_H
31 
32 
33 
34 #include "map.h"
35 #include "scene/resources/shader.h"
36 
37 class ShaderGraph : public Shader {
38 
39  OBJ_TYPE( ShaderGraph, Shader );
40  RES_BASE_EXTENSION("sgp");
41 
42 public:
43 
44  enum NodeType {
45  NODE_INPUT, // all inputs (shader type dependent)
46  NODE_SCALAR_CONST, //scalar constant
47  NODE_VEC_CONST, //vec3 constant
48  NODE_RGB_CONST, //rgb constant (shows a color picker instead)
49  NODE_XFORM_CONST, // 4x4 matrix constant
50  NODE_TIME, // time in seconds
51  NODE_SCREEN_TEX, // screen texture sampler (takes UV) (only usable in fragment shader)
52  NODE_SCALAR_OP, // scalar vs scalar op (mul, add, div, etc)
53  NODE_VEC_OP, // vec3 vs vec3 op (mul,ad,div,crossprod,etc)
54  NODE_VEC_SCALAR_OP, // vec3 vs scalar op (mul, add, div, etc)
55  NODE_RGB_OP, // vec3 vs vec3 rgb op (with scalar amount), like brighten, darken, burn, dodge, multiply, etc.
56  NODE_XFORM_MULT, // mat4 x mat4
57  NODE_XFORM_VEC_MULT, // mat4 x vec3 mult (with no-translation option)
58  NODE_XFORM_VEC_INV_MULT, // mat4 x vec3 inverse mult (with no-translation option)
59  NODE_SCALAR_FUNC, // scalar function (sin, cos, etc)
60  NODE_VEC_FUNC, // vector function (normalize, negate, reciprocal, rgb2hsv, hsv2rgb, etc, etc)
61  NODE_VEC_LEN, // vec3 length
62  NODE_DOT_PROD, // vec3 . vec3 (dot product -> scalar output)
63  NODE_VEC_TO_SCALAR, // 1 vec3 input, 3 scalar outputs
64  NODE_SCALAR_TO_VEC, // 3 scalar input, 1 vec3 output
65  NODE_XFORM_TO_VEC, // 3 vec input, 1 xform output
66  NODE_VEC_TO_XFORM, // 3 vec input, 1 xform output
67  NODE_SCALAR_INTERP, // scalar interpolation (with optional curve)
68  NODE_VEC_INTERP, // vec3 interpolation (with optional curve)
69  NODE_COLOR_RAMP, //take scalar, output vec3
70  NODE_CURVE_MAP, //take scalar, otput scalar
71  NODE_SCALAR_INPUT, // scalar uniform (assignable in material)
72  NODE_VEC_INPUT, // vec3 uniform (assignable in material)
73  NODE_RGB_INPUT, // color uniform (assignable in material)
74  NODE_XFORM_INPUT, // mat4 uniform (assignable in material)
75  NODE_TEXTURE_INPUT, // texture input (assignable in material)
76  NODE_CUBEMAP_INPUT, // cubemap input (assignable in material)
77  NODE_DEFAULT_TEXTURE,
78  NODE_OUTPUT, // output (shader type dependent)
79  NODE_COMMENT, // comment
80  NODE_TYPE_MAX
81  };
82 
83 
84  struct Connection {
85 
86  int src_id;
87  int src_slot;
88  int dst_id;
89  int dst_slot;
90  };
91 
92  enum SlotType {
93 
94  SLOT_TYPE_SCALAR,
95  SLOT_TYPE_VEC,
96  SLOT_TYPE_XFORM,
97  SLOT_TYPE_TEXTURE,
98  SLOT_MAX
99  };
100 
101  enum ShaderType {
102  SHADER_TYPE_VERTEX,
103  SHADER_TYPE_FRAGMENT,
104  SHADER_TYPE_LIGHT,
105  SHADER_TYPE_MAX
106  };
107 
108  enum SlotDir {
109  SLOT_IN,
110  SLOT_OUT
111  };
112 
113  enum GraphError {
114  GRAPH_OK,
115  GRAPH_ERROR_CYCLIC,
116  GRAPH_ERROR_MISSING_CONNECTIONS
117  };
118 
119 private:
120 
121  String _find_unique_name(const String& p_base);
122 
123  enum {SLOT_DEFAULT_VALUE = 0x7FFFFFFF};
124  struct SourceSlot {
125 
126  int id;
127  int slot;
128  bool operator==(const SourceSlot& p_slot) const {
129  return id==p_slot.id && slot==p_slot.slot;
130  }
131  };
132 
133  struct Node {
134 
135  Vector2 pos;
136  NodeType type;
137  Variant param1;
138  Variant param2;
139  Map<int, Variant> defaults;
140  int id;
141  mutable int order; // used for sorting
142  int sort_order;
143  Map<int,SourceSlot> connections;
144 
145  };
146 
147  struct ShaderData {
148  Map<int,Node> node_map;
149  GraphError error;
150  } shader[3];
151 
152 
153 
154  struct InOutParamInfo {
155  Mode shader_mode;
156  ShaderType shader_type;
157  const char *name;
158  const char *variable;
159  const char *postfix;
160  SlotType slot_type;
161  SlotDir dir;
162  };
163 
164  static const InOutParamInfo inout_param_info[];
165 
166  struct NodeSlotInfo {
167 
168  enum { MAX_INS=3, MAX_OUTS=3 };
169  NodeType type;
170  const SlotType ins[MAX_INS];
171  const SlotType outs[MAX_OUTS];
172  };
173 
174  static const NodeSlotInfo node_slot_info[];
175 
176  bool _pending_update_shader;
177  void _update_shader();
178  void _request_update();
179 
180  void _plot_curve(const Vector2& p_a,const Vector2& p_b,const Vector2& p_c,const Vector2& p_d,uint8_t* p_heights,bool *p_useds);
181  void _add_node_code(ShaderType p_type,Node *p_node,const Vector<String>& p_inputs,String& code);
182 
183  Array _get_node_list(ShaderType p_type) const;
184  Array _get_connections(ShaderType p_type) const;
185 
186  void _set_data(const Dictionary& p_data);
187  Dictionary _get_data() const;
188 protected:
189 
190  static void _bind_methods();
191 
192 public:
193 
194 
195  void node_add(ShaderType p_type, NodeType p_node_type, int p_id);
196  void node_remove(ShaderType p_which,int p_id);
197  void node_set_pos(ShaderType p_which,int p_id,const Point2& p_pos);
198  Point2 node_get_pos(ShaderType p_which,int p_id) const;
199 
200  void get_node_list(ShaderType p_which,List<int> *p_node_list) const;
201  NodeType node_get_type(ShaderType p_which,int p_id) const;
202 
203  void scalar_const_node_set_value(ShaderType p_which,int p_id,float p_value);
204  float scalar_const_node_get_value(ShaderType p_which,int p_id) const;
205 
206  void vec_const_node_set_value(ShaderType p_which,int p_id,const Vector3& p_value);
207  Vector3 vec_const_node_get_value(ShaderType p_which,int p_id) const;
208 
209  void rgb_const_node_set_value(ShaderType p_which,int p_id,const Color& p_value);
210  Color rgb_const_node_get_value(ShaderType p_which,int p_id) const;
211 
212  void xform_const_node_set_value(ShaderType p_which,int p_id,const Transform& p_value);
213  Transform xform_const_node_get_value(ShaderType p_which,int p_id) const;
214 
215  void texture_node_set_filter_size(ShaderType p_which,int p_id,int p_size);
216  int texture_node_get_filter_size(ShaderType p_which,int p_id) const;
217 
218  void texture_node_set_filter_strength(ShaderType p_which,float p_id,float p_strength);
219  float texture_node_get_filter_strength(ShaderType p_which,float p_id) const;
220 
221  void duplicate_nodes(ShaderType p_which, List<int> &p_nodes);
222 
223  List<int> generate_ids(ShaderType p_type, int count);
224 
225  enum ScalarOp {
226  SCALAR_OP_ADD,
227  SCALAR_OP_SUB,
228  SCALAR_OP_MUL,
229  SCALAR_OP_DIV,
230  SCALAR_OP_MOD,
231  SCALAR_OP_POW,
232  SCALAR_OP_MAX,
233  SCALAR_OP_MIN,
234  SCALAR_OP_ATAN2,
235  SCALAR_MAX_OP
236  };
237 
238  void scalar_op_node_set_op(ShaderType p_which,float p_id,ScalarOp p_op);
239  ScalarOp scalar_op_node_get_op(ShaderType p_which,float p_id) const;
240 
241  enum VecOp {
242  VEC_OP_ADD,
243  VEC_OP_SUB,
244  VEC_OP_MUL,
245  VEC_OP_DIV,
246  VEC_OP_MOD,
247  VEC_OP_POW,
248  VEC_OP_MAX,
249  VEC_OP_MIN,
250  VEC_OP_CROSS,
251  VEC_MAX_OP
252  };
253 
254  void vec_op_node_set_op(ShaderType p_which,float p_id,VecOp p_op);
255  VecOp vec_op_node_get_op(ShaderType p_which,float p_id) const;
256 
257  enum VecScalarOp {
258  VEC_SCALAR_OP_MUL,
259  VEC_SCALAR_OP_DIV,
260  VEC_SCALAR_OP_POW,
261  VEC_SCALAR_MAX_OP
262  };
263 
264  void vec_scalar_op_node_set_op(ShaderType p_which,float p_id,VecScalarOp p_op);
265  VecScalarOp vec_scalar_op_node_get_op(ShaderType p_which,float p_id) const;
266 
267  enum RGBOp {
268  RGB_OP_SCREEN,
269  RGB_OP_DIFFERENCE,
270  RGB_OP_DARKEN,
271  RGB_OP_LIGHTEN,
272  RGB_OP_OVERLAY,
273  RGB_OP_DODGE,
274  RGB_OP_BURN,
275  RGB_OP_SOFT_LIGHT,
276  RGB_OP_HARD_LIGHT,
277  RGB_MAX_OP
278  };
279 
280  void rgb_op_node_set_op(ShaderType p_which,float p_id,RGBOp p_op);
281  RGBOp rgb_op_node_get_op(ShaderType p_which,float p_id) const;
282 
283  void xform_vec_mult_node_set_no_translation(ShaderType p_which,int p_id,bool p_no_translation);
284  bool xform_vec_mult_node_get_no_translation(ShaderType p_which,int p_id) const;
285 
286  enum ScalarFunc {
287  SCALAR_FUNC_SIN,
288  SCALAR_FUNC_COS,
289  SCALAR_FUNC_TAN,
290  SCALAR_FUNC_ASIN,
291  SCALAR_FUNC_ACOS,
292  SCALAR_FUNC_ATAN,
293  SCALAR_FUNC_SINH,
294  SCALAR_FUNC_COSH,
295  SCALAR_FUNC_TANH,
296  SCALAR_FUNC_LOG,
297  SCALAR_FUNC_EXP,
298  SCALAR_FUNC_SQRT,
299  SCALAR_FUNC_ABS,
300  SCALAR_FUNC_SIGN,
301  SCALAR_FUNC_FLOOR,
302  SCALAR_FUNC_ROUND,
303  SCALAR_FUNC_CEIL,
304  SCALAR_FUNC_FRAC,
305  SCALAR_FUNC_SATURATE,
306  SCALAR_FUNC_NEGATE,
307  SCALAR_MAX_FUNC
308  };
309 
310  void scalar_func_node_set_function(ShaderType p_which,int p_id,ScalarFunc p_func);
311  ScalarFunc scalar_func_node_get_function(ShaderType p_which,int p_id) const;
312 
313  enum VecFunc {
314  VEC_FUNC_NORMALIZE,
315  VEC_FUNC_SATURATE,
316  VEC_FUNC_NEGATE,
317  VEC_FUNC_RECIPROCAL,
318  VEC_FUNC_RGB2HSV,
319  VEC_FUNC_HSV2RGB,
320  VEC_MAX_FUNC
321  };
322 
323  void default_set_value(ShaderType p_which,int p_id,int p_param, const Variant& p_value);
324  Variant default_get_value(ShaderType p_which,int p_id,int p_param);
325 
326  void vec_func_node_set_function(ShaderType p_which,int p_id,VecFunc p_func);
327  VecFunc vec_func_node_get_function(ShaderType p_which,int p_id) const;
328 
329  void color_ramp_node_set_ramp(ShaderType p_which,int p_id,const DVector<Color>& p_colors, const DVector<real_t>& p_offsets);
330  DVector<Color> color_ramp_node_get_colors(ShaderType p_which,int p_id) const;
331  DVector<real_t> color_ramp_node_get_offsets(ShaderType p_which,int p_id) const;
332 
333  void curve_map_node_set_points(ShaderType p_which, int p_id, const DVector<Vector2>& p_points);
334  DVector<Vector2> curve_map_node_get_points(ShaderType p_which,int p_id) const;
335 
336  void input_node_set_name(ShaderType p_which,int p_id,const String& p_name);
337  String input_node_get_name(ShaderType p_which,int p_id);
338 
339  void scalar_input_node_set_value(ShaderType p_which,int p_id,float p_value);
340  float scalar_input_node_get_value(ShaderType p_which,int p_id) const;
341 
342  void vec_input_node_set_value(ShaderType p_which,int p_id,const Vector3& p_value);
343  Vector3 vec_input_node_get_value(ShaderType p_which,int p_id) const;
344 
345  void rgb_input_node_set_value(ShaderType p_which,int p_id,const Color& p_value);
346  Color rgb_input_node_get_value(ShaderType p_which,int p_id) const;
347 
348  void xform_input_node_set_value(ShaderType p_which,int p_id,const Transform& p_value);
349  Transform xform_input_node_get_value(ShaderType p_which,int p_id) const;
350 
351  void texture_input_node_set_value(ShaderType p_which,int p_id,const Ref<Texture>& p_texture);
352  Ref<Texture> texture_input_node_get_value(ShaderType p_which,int p_id) const;
353 
354  void cubemap_input_node_set_value(ShaderType p_which,int p_id,const Ref<CubeMap>& p_cubemap);
355  Ref<CubeMap> cubemap_input_node_get_value(ShaderType p_which,int p_id) const;
356 
357  void comment_node_set_text(ShaderType p_which,int p_id,const String& p_comment);
358  String comment_node_get_text(ShaderType p_which,int p_id) const;
359 
360  Error connect_node(ShaderType p_which,int p_src_id,int p_src_slot, int p_dst_id,int p_dst_slot);
361  bool is_node_connected(ShaderType p_which,int p_src_id,int p_src_slot, int p_dst_id,int p_dst_slot) const;
362  void disconnect_node(ShaderType p_which,int p_src_id,int p_src_slot, int p_dst_id,int p_dst_slot);
363 
364  void get_node_connections(ShaderType p_which,List<Connection> *p_connections) const;
365 
366  bool is_slot_connected(ShaderType p_which,int p_dst_id,int slot_id);
367 
368  void clear(ShaderType p_which);
369 
370  Variant node_get_state(ShaderType p_type, int p_node) const;
371  void node_set_state(ShaderType p_type, int p_id, const Variant& p_state);
372 
373  GraphError get_graph_error(ShaderType p_type) const;
374 
375  int node_count(ShaderType p_which, int p_type);
376 
377  static int get_type_input_count(NodeType p_type);
378  static int get_type_output_count(NodeType p_type);
379  static SlotType get_type_input_type(NodeType p_type,int p_idx);
380  static SlotType get_type_output_type(NodeType p_type,int p_idx);
381  static bool is_type_valid(Mode p_mode,ShaderType p_type);
382 
383 
384  struct SlotInfo {
385  String name;
386  SlotType type;
387  SlotDir dir;
388  };
389 
390  static void get_input_output_node_slot_info(Mode p_mode, ShaderType p_type, List<SlotInfo> *r_slots);
391 
392  static int get_node_input_slot_count(Mode p_mode, ShaderType p_shader_type,NodeType p_type);
393  static int get_node_output_slot_count(Mode p_mode, ShaderType p_shader_type,NodeType p_type);
394  static SlotType get_node_input_slot_type(Mode p_mode, ShaderType p_shader_type,NodeType p_type,int p_idx);
395  static SlotType get_node_output_slot_type(Mode p_mode, ShaderType p_shader_type,NodeType p_type,int p_idx);
396 
397 
398  ShaderGraph(Mode p_mode);
399  ~ShaderGraph();
400 };
401 
402 //helper functions
403 
404 
405 
406 
407 VARIANT_ENUM_CAST( ShaderGraph::NodeType );
408 VARIANT_ENUM_CAST( ShaderGraph::ShaderType );
409 VARIANT_ENUM_CAST( ShaderGraph::SlotType );
410 VARIANT_ENUM_CAST( ShaderGraph::ScalarOp );
411 VARIANT_ENUM_CAST( ShaderGraph::VecOp );
412 VARIANT_ENUM_CAST( ShaderGraph::VecScalarOp );
413 VARIANT_ENUM_CAST( ShaderGraph::RGBOp );
414 VARIANT_ENUM_CAST( ShaderGraph::ScalarFunc );
415 VARIANT_ENUM_CAST( ShaderGraph::VecFunc );
416 VARIANT_ENUM_CAST( ShaderGraph::GraphError );
417 
418 
420 
421  OBJ_TYPE( MaterialShaderGraph, ShaderGraph );
422 
423 public:
424 
425 
426  MaterialShaderGraph() : ShaderGraph(MODE_MATERIAL) {
427 
428  }
429 };
430 
432 
433  OBJ_TYPE( CanvasItemShaderGraph, ShaderGraph );
434 
435 public:
436 
437 
438  CanvasItemShaderGraph() : ShaderGraph(MODE_CANVAS_ITEM) {
439 
440  }
441 };
442 
443 
444 #endif // SHADER_GRAPH_H
Definition: array.h:38
Definition: shader_graph.h:84
Definition: variant.h:74
Definition: color.h:37
Definition: shader_graph.h:431
Definition: vector3.h:38
Definition: transform.h:38
Definition: shader_graph.h:419
Definition: dictionary.h:42
Definition: math_2d.h:65
Definition: shader_graph.h:384
Definition: shader_graph.h:37
Definition: shader.h:35
Definition: dvector.h:43
Definition: ustring.h:64