Panda3D
shaderGenerator.h
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file shaderGenerator.h
10  * @author jyelon
11  * @date 2007-12-15
12  * @author weifengh, PandaSE
13  * @date 2010-04-15
14  */
15 
16 #ifndef SHADERGENERATOR_H
17 #define SHADERGENERATOR_H
18 
19 #include "pandabase.h"
20 #include "typedReferenceCount.h"
21 
22 #ifdef HAVE_CG
23 
25 #include "graphicsOutputBase.h"
26 #include "nodePath.h"
27 #include "shaderAttrib.h"
28 #include "renderState.h"
29 #include "renderAttrib.h"
30 
31 #include "colorAttrib.h"
32 #include "lightRampAttrib.h"
33 #include "texGenAttrib.h"
34 #include "textureAttrib.h"
35 
36 class AmbientLight;
37 class DirectionalLight;
38 class PointLight;
39 class Spotlight;
40 class LightAttrib;
42 
43 /**
44  * The ShaderGenerator is a device that effectively replaces the classic fixed
45  * function pipeline with a 'next-gen' fixed function pipeline. The next-gen
46  * fixed function pipeline supports features like normal mapping, gloss
47  * mapping, cartoon lighting, and so forth. It works by automatically
48  * generating a shader from a given RenderState.
49  *
50  * Currently, there is one ShaderGenerator object per GraphicsStateGuardian.
51  * It is our intent that in time, people will write classes that derive from
52  * ShaderGenerator but which yield slightly different results.
53  *
54  * The ShaderGenerator owes its existence to the 'Bamboo Team' at Carnegie
55  * Mellon's Entertainment Technology Center. This is a group of students who,
56  * as a semester project, decided that next-gen graphics should be accessible
57  * to everyone, even if they don't know shader programming. The group
58  * consisted of:
59  *
60  * Aaron Lo, Programmer Heegun Lee, Programmer Erin Fernandez, Artist/Tester
61  * Joe Grubb, Artist/Tester Ivan Ortega, Technical Artist/Tester
62  *
63  * Thanks to them!
64  *
65  */
66 class EXPCL_PANDA_PGRAPHNODES ShaderGenerator : public TypedReferenceCount {
67 PUBLISHED:
69  virtual ~ShaderGenerator();
70  virtual CPT(ShaderAttrib) synthesize_shader(const RenderState *rs,
71  const GeomVertexAnimationSpec &anim);
72 
73  void rehash_generated_shaders();
74  void clear_generated_shaders();
75 
76 protected:
77  // Shader register allocation:
78 
79  bool _use_generic_attr;
80  int _vcregs_used;
81  int _fcregs_used;
82  int _vtregs_used;
83  int _ftregs_used;
84  void reset_register_allocator();
85  const char *alloc_vreg();
86  const char *alloc_freg();
87 
88  bool _use_shadow_filter;
89 
90  // RenderState analysis information. Created by analyze_renderstate:
91 
92  CPT(RenderState) _state;
93  struct ShaderKey {
94  ShaderKey();
95  bool operator < (const ShaderKey &other) const;
96  bool operator == (const ShaderKey &other) const;
97  bool operator != (const ShaderKey &other) const { return !operator ==(other); }
98 
99  GeomVertexAnimationSpec _anim_spec;
100  enum TextureFlags {
101  TF_has_rgb = 0x001,
102  TF_has_alpha = 0x002,
103  TF_has_texscale = 0x004,
104  TF_has_texmat = 0x008,
105  TF_saved_result = 0x010,
106  TF_map_normal = 0x020,
107  TF_map_height = 0x040,
108  TF_map_glow = 0x080,
109  TF_map_gloss = 0x100,
110  TF_uses_color = 0x200,
111  TF_uses_primary_color = 0x400,
112  TF_uses_last_saved_result = 0x800,
113 
114  TF_rgb_scale_2 = 0x1000,
115  TF_rgb_scale_4 = 0x2000,
116  TF_alpha_scale_2 = 0x4000,
117  TF_alpha_scale_4 = 0x8000,
118 
119  TF_COMBINE_RGB_MODE_SHIFT = 16,
120  TF_COMBINE_RGB_MODE_MASK = 0x0000f0000,
121  TF_COMBINE_ALPHA_MODE_SHIFT = 20,
122  TF_COMBINE_ALPHA_MODE_MASK = 0x000f00000,
123  };
124 
125  ColorAttrib::Type _color_type;
126  int _material_flags;
127  int _texture_flags;
128 
129  struct TextureInfo {
130  CPT_InternalName _texcoord_name;
131  Texture::TextureType _type;
132  TextureStage::Mode _mode;
133  TexGenAttrib::Mode _gen_mode;
134  int _flags;
135  uint16_t _combine_rgb;
136  uint16_t _combine_alpha;
137  };
138  pvector<TextureInfo> _textures;
139 
140  enum LightFlags {
141  LF_has_shadows = 1,
142  LF_has_specular_color = 2,
143  };
144 
145  struct LightInfo {
146  TypeHandle _type;
147  int _flags;
148  };
149  pvector<LightInfo> _lights;
150  bool _lighting;
151  bool _have_separate_ambient;
152 
153  int _fog_mode;
154 
155  int _outputs;
156  bool _calc_primary_alpha;
157  bool _disable_alpha_write;
158  RenderAttrib::PandaCompareFunc _alpha_test_mode;
159  PN_stdfloat _alpha_test_ref;
160 
161  int _num_clip_planes;
162 
163  CPT(LightRampAttrib) _light_ramp;
164  };
165 
166  typedef phash_map<ShaderKey, CPT(ShaderAttrib)> GeneratedShaders;
167  GeneratedShaders _generated_shaders;
168 
169  void analyze_renderstate(ShaderKey &key, const RenderState *rs);
170 
171  static std::string combine_mode_as_string(const ShaderKey::TextureInfo &info,
172  TextureStage::CombineMode c_mode, bool alpha, short texindex);
173  static std::string combine_source_as_string(const ShaderKey::TextureInfo &info,
174  short num, bool alpha, short texindex);
175  static const char *texture_type_as_string(Texture::TextureType ttype);
176 
177 public:
178  static TypeHandle get_class_type() {
179  return _type_handle;
180  }
181  static void init_type() {
182  TypedReferenceCount::init_type();
183  register_type(_type_handle, "ShaderGenerator",
184  TypedReferenceCount::get_class_type());
185  }
186  virtual TypeHandle get_type() const {
187  return get_class_type();
188  }
189  virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
190 
191 private:
192  static TypeHandle _type_handle;
193 };
194 
195 #else
196 
197 // If we don't have Cg, let's replace this with a stub.
198 class EXPCL_PANDA_PGRAPHNODES ShaderGenerator : public TypedReferenceCount {
199 public:
200  static TypeHandle get_class_type() {
201  return _type_handle;
202  }
203  static void init_type() {
204  TypedReferenceCount::init_type();
205  register_type(_type_handle, "ShaderGenerator",
206  TypedReferenceCount::get_class_type());
207  }
208  virtual TypeHandle get_type() const {
209  return get_class_type();
210  }
211  virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
212 
213 private:
214  static TypeHandle _type_handle;
215 };
216 
217 #include "shaderGenerator.I"
218 
219 #endif // HAVE_CG
220 
221 #endif // SHADERGENERATOR_H
A light shining from infinitely far away in a particular direction, like sunlight.
This is a const pointer to an InternalName, and should be used in lieu of a CPT(InternalName) in func...
Definition: internalName.h:193
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This object describes how the vertex animation, if any, represented in a GeomVertexData is encoded.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void register_type(TypeHandle &type_handle, const std::string &name)
This inline function is just a convenient way to call TypeRegistry::register_type(),...
Definition: register_type.I:22
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
A base class for things which need to inherit from both TypedObject and from ReferenceCount.
A light source that seems to illuminate all points in space at once.
Definition: ambientLight.h:26
A Light Ramp is any unary operator that takes a rendered pixel as input, and adjusts the brightness o...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This is our own Panda specialization on the default STL vector.
Definition: pvector.h:42
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
A light originating from a single point in space, and shining in a particular direction,...
Definition: spotlight.h:32
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This represents a unique collection of RenderAttrib objects that correspond to a particular renderabl...
Definition: renderState.h:47
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This is a base class for the GraphicsStateGuardian class, which is itself a base class for the variou...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
A light originating from a single point in space, and shining in all directions.
Definition: pointLight.h:25
Indicates which set of lights should be considered "on" to illuminate geometry at this level and belo...
Definition: lightAttrib.h:30