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"
22 #include "shaderAttrib.h"
23 #include "renderState.h"
24 
25 #ifdef HAVE_CG
26 
27 #include "graphicsOutputBase.h"
28 #include "nodePath.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_map_emission = 0x001000000,
111  TF_uses_color = 0x200,
112  TF_uses_primary_color = 0x400,
113  TF_uses_last_saved_result = 0x800,
114 
115  TF_rgb_scale_2 = 0x1000,
116  TF_rgb_scale_4 = 0x2000,
117  TF_alpha_scale_2 = 0x4000,
118  TF_alpha_scale_4 = 0x8000,
119 
120  TF_COMBINE_RGB_MODE_SHIFT = 16,
121  TF_COMBINE_RGB_MODE_MASK = 0x0000f0000,
122  TF_COMBINE_ALPHA_MODE_SHIFT = 20,
123  TF_COMBINE_ALPHA_MODE_MASK = 0x000f00000,
124  };
125 
126  ColorAttrib::Type _color_type;
127  int _material_flags;
128  int _texture_flags;
129 
130  struct TextureInfo {
131  CPT_InternalName _texcoord_name;
132  Texture::TextureType _type;
133  TextureStage::Mode _mode;
134  TexGenAttrib::Mode _gen_mode;
135  int _flags;
136  uint16_t _combine_rgb;
137  uint16_t _combine_alpha;
138  };
139  pvector<TextureInfo> _textures;
140 
141  enum LightFlags {
142  LF_has_shadows = 1,
143  LF_has_specular_color = 2,
144  };
145 
146  struct LightInfo {
147  TypeHandle _type;
148  int _flags;
149  };
150  pvector<LightInfo> _lights;
151  bool _lighting;
152  bool _have_separate_ambient;
153 
154  int _fog_mode;
155 
156  int _outputs;
157  bool _calc_primary_alpha;
158  bool _disable_alpha_write;
159  RenderAttrib::PandaCompareFunc _alpha_test_mode;
160  PN_stdfloat _alpha_test_ref;
161 
162  int _num_clip_planes;
163 
164  CPT(LightRampAttrib) _light_ramp;
165  };
166 
167  typedef phash_map<ShaderKey, CPT(ShaderAttrib)> GeneratedShaders;
168  GeneratedShaders _generated_shaders;
169 
170  void analyze_renderstate(ShaderKey &key, const RenderState *rs);
171 
172  static std::string combine_mode_as_string(const ShaderKey::TextureInfo &info,
173  TextureStage::CombineMode c_mode, bool alpha, short texindex);
174  static std::string combine_source_as_string(const ShaderKey::TextureInfo &info,
175  short num, bool alpha, short texindex);
176  static const char *texture_type_as_string(Texture::TextureType ttype);
177 
178 public:
179  static TypeHandle get_class_type() {
180  return _type_handle;
181  }
182  static void init_type() {
183  TypedReferenceCount::init_type();
184  register_type(_type_handle, "ShaderGenerator",
185  TypedReferenceCount::get_class_type());
186  }
187  virtual TypeHandle get_type() const {
188  return get_class_type();
189  }
190  virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
191 
192 private:
193  static TypeHandle _type_handle;
194 };
195 
196 #else
197 
198 // If we don't have Cg, let's replace this with a stub.
199 class EXPCL_PANDA_PGRAPHNODES ShaderGenerator : public TypedReferenceCount {
200 PUBLISHED:
202  virtual ~ShaderGenerator();
203 
204  virtual CPT(ShaderAttrib) synthesize_shader(const RenderState *rs,
205  const GeomVertexAnimationSpec &anim);
206 
207  void rehash_generated_shaders();
208  void clear_generated_shaders();
209 
210 public:
211  static TypeHandle get_class_type() {
212  return _type_handle;
213  }
214  static void init_type() {
215  TypedReferenceCount::init_type();
216  register_type(_type_handle, "ShaderGenerator",
217  TypedReferenceCount::get_class_type());
218  }
219  virtual TypeHandle get_type() const {
220  return get_class_type();
221  }
222  virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
223 
224 private:
225  static TypeHandle _type_handle;
226 };
227 
228 #include "shaderGenerator.I"
229 
230 #endif // HAVE_CG
231 
232 #endif // SHADERGENERATOR_H
nodePath.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
pvector
This is our own Panda specialization on the default STL vector.
Definition: pvector.h:42
pandabase.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
register_type
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
PointLight
A light originating from a single point in space, and shining in all directions.
Definition: pointLight.h:25
colorAttrib.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
renderAttrib.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
ShaderAttrib
Definition: shaderAttrib.h:39
TypedReferenceCount
A base class for things which need to inherit from both TypedObject and from ReferenceCount.
Definition: typedReferenceCount.h:31
RenderState
This represents a unique collection of RenderAttrib objects that correspond to a particular renderabl...
Definition: renderState.h:47
renderState.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
AmbientLight
A light source that seems to illuminate all points in space at once.
Definition: ambientLight.h:26
TypeHandle
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
texGenAttrib.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
GeomVertexAnimationSpec
This object describes how the vertex animation, if any, represented in a GeomVertexData is encoded.
Definition: geomVertexAnimationSpec.h:38
ShaderGenerator
Definition: shaderGenerator.h:199
LightRampAttrib
A Light Ramp is any unary operator that takes a rendered pixel as input, and adjusts the brightness o...
Definition: lightRampAttrib.h:28
Spotlight
A light originating from a single point in space, and shining in a particular direction,...
Definition: spotlight.h:32
shaderAttrib.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
CPT_InternalName
This is a const pointer to an InternalName, and should be used in lieu of a CPT(InternalName) in func...
Definition: internalName.h:193
DirectionalLight
A light shining from infinitely far away in a particular direction, like sunlight.
Definition: directionalLight.h:25
lightRampAttrib.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
textureAttrib.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
shaderGenerator.I
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
GraphicsStateGuardianBase
This is a base class for the GraphicsStateGuardian class, which is itself a base class for the variou...
Definition: graphicsStateGuardianBase.h:110
typedReferenceCount.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
LightAttrib
Indicates which set of lights should be considered "on" to illuminate geometry at this level and belo...
Definition: lightAttrib.h:30
graphicsStateGuardianBase.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
graphicsOutputBase.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.