shader.h
1 /*************************************************************************/
2 /* shader.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_H
30 #define SHADER_H
31 
32 #include "resource.h"
33 #include "io/resource_loader.h"
34 #include "scene/resources/texture.h"
35 class Shader : public Resource {
36 
37  OBJ_TYPE(Shader,Resource);
38  OBJ_SAVE_TYPE( Shader );
39  RES_BASE_EXTENSION("shd");
40 
41 public:
42  enum Mode {
43 
44  MODE_MATERIAL,
45  MODE_CANVAS_ITEM,
46  MODE_POST_PROCESS,
47  MODE_MAX
48  };
49 private:
50  RID shader;
51  Mode mode;
52  Dictionary _get_code();
53  void _set_code(const Dictionary& p_string);
54 
55 
56  // hack the name of performance
57  // shaders keep a list of ShaderMaterial -> VisualServer name translations, to make
58  // convertion fast and save memory.
59  mutable bool params_cache_dirty;
60  mutable Map<StringName,StringName> params_cache; //map a shader param to a material param..
61  Map<StringName,Ref<Texture> > default_textures;
62 
63 
64 
65 protected:
66 
67 
68 
69  static void _bind_methods();
70 public:
71 
72 
73  //void set_mode(Mode p_mode);
74  Mode get_mode() const;
75 
76  void set_code( const String& p_vertex, const String& p_fragment, const String& p_light,int p_fragment_ofs=0,int p_light_ofs=0);
77  String get_vertex_code() const;
78  String get_fragment_code() const;
79  String get_light_code() const;
80 
81  void get_param_list(List<PropertyInfo> *p_params) const;
82  bool has_param(const StringName& p_param) const;
83 
84  void set_default_texture_param(const StringName& p_param, const Ref<Texture> &p_texture);
85  Ref<Texture> get_default_texture_param(const StringName& p_param) const;
86  void get_default_texture_param_list(List<StringName>* r_textures) const;
87 
88  _FORCE_INLINE_ StringName remap_param(const StringName& p_param) const {
89  if (params_cache_dirty)
90  get_param_list(NULL);
91 
92  const Map<StringName,StringName>::Element *E=params_cache.find(p_param);
93  if (E)
94  return E->get();
95  return StringName();
96  }
97 
98  virtual RID get_rid() const;
99 
100  Shader(Mode p_mode);
101  ~Shader();
102 
103 };
104 
105 VARIANT_ENUM_CAST( Shader::Mode );
106 
107 class MaterialShader : public Shader {
108 
109  OBJ_TYPE(MaterialShader,Shader);
110 
111 public:
112 
113  MaterialShader() : Shader(MODE_MATERIAL) {};
114 };
115 
116 class CanvasItemShader : public Shader {
117 
118  OBJ_TYPE(CanvasItemShader,Shader);
119 
120 public:
121 
122  CanvasItemShader() : Shader(MODE_CANVAS_ITEM) {};
123 };
124 
125 
126 
128 public:
129  virtual RES load(const String &p_path,const String& p_original_path="",Error *r_error=NULL);
130  virtual void get_recognized_extensions(List<String> *p_extensions) const;
131  virtual bool handles_type(const String& p_type) const;
132  virtual String get_resource_type(const String &p_path) const;
133 };
134 
135 
136 
137 #endif // SHADER_H
Definition: string_db.h:48
Definition: resource.h:89
Definition: resource_loader.h:57
Definition: rid.h:47
Definition: dictionary.h:42
Definition: shader.h:116
Definition: shader.h:35
Definition: shader.h:127
Definition: ustring.h:64
Definition: shader.h:107