Panda3D
Loading...
Searching...
No Matches
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
36class AmbientLight;
38class PointLight;
39class Spotlight;
40class 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 */
66class EXPCL_PANDA_PGRAPHNODES ShaderGenerator : public TypedReferenceCount {
67public:
68 ShaderGenerator(const Shader::ShaderCaps &caps, bool use_shadow_filter);
69
70PUBLISHED:
72 virtual ~ShaderGenerator();
73 virtual CPT(ShaderAttrib) synthesize_shader(const RenderState *rs,
74 const GeomVertexAnimationSpec &anim);
75
76 void rehash_generated_shaders();
77 void clear_generated_shaders();
78
79protected:
80 // Shader register allocation:
81
82 bool _use_generic_attr : 1;
83 bool _use_pointcoord : 1;
84 int _vcregs_used;
85 int _fcregs_used;
86 int _vtregs_used;
87 int _ftregs_used;
88 void reset_register_allocator();
89 const char *alloc_vreg();
90 const char *alloc_freg();
91
92 bool _use_shadow_filter;
93
94 // RenderState analysis information. Created by analyze_renderstate:
95
96 CPT(RenderState) _state;
97 struct ShaderKey {
98 ShaderKey();
99 bool operator < (const ShaderKey &other) const;
100 bool operator == (const ShaderKey &other) const;
101 bool operator != (const ShaderKey &other) const { return !operator ==(other); }
102
103 GeomVertexAnimationSpec _anim_spec;
104 enum TextureFlags {
105 TF_has_rgb = 0x001,
106 TF_has_alpha = 0x002,
107 TF_has_texscale = 0x004,
108 TF_has_texmat = 0x008,
109 TF_saved_result = 0x010,
110 TF_map_normal = 0x020,
111 TF_map_height = 0x040,
112 TF_map_glow = 0x080,
113 TF_map_gloss = 0x100,
114 TF_map_emission = 0x001000000,
115 TF_uses_color = 0x200,
116 TF_uses_primary_color = 0x400,
117 TF_uses_last_saved_result = 0x800,
118
119 TF_rgb_scale_2 = 0x1000,
120 TF_rgb_scale_4 = 0x2000,
121 TF_alpha_scale_2 = 0x4000,
122 TF_alpha_scale_4 = 0x8000,
123
124 TF_COMBINE_RGB_MODE_SHIFT = 16,
125 TF_COMBINE_RGB_MODE_MASK = 0x0000f0000,
126 TF_COMBINE_ALPHA_MODE_SHIFT = 20,
127 TF_COMBINE_ALPHA_MODE_MASK = 0x000f00000,
128 };
129
130 ColorAttrib::Type _color_type;
131 int _material_flags;
132 int _texture_flags;
133
134 struct TextureInfo {
135 CPT_InternalName _texcoord_name;
136 Texture::TextureType _type;
137 TextureStage::Mode _mode;
138 TexGenAttrib::Mode _gen_mode;
139 int _flags;
140 uint16_t _combine_rgb;
141 uint16_t _combine_alpha;
142 };
143 pvector<TextureInfo> _textures;
144
145 enum LightFlags {
146 LF_has_shadows = 1,
147 LF_has_specular_color = 2,
148 };
149
150 struct LightInfo {
151 TypeHandle _type;
152 int _flags;
153 };
154 pvector<LightInfo> _lights;
155 bool _lighting;
156 bool _have_separate_ambient;
157
158 // Also contains bit 0x10000 indicating perspective point mode
159 int _fog_mode;
160
161 int _outputs;
162 bool _calc_primary_alpha;
163 bool _disable_alpha_write;
164 RenderAttrib::PandaCompareFunc _alpha_test_mode;
165 PN_stdfloat _alpha_test_ref;
166
167 int _num_clip_planes;
168
169 CPT(LightRampAttrib) _light_ramp;
170 };
171
172 typedef phash_map<ShaderKey, CPT(ShaderAttrib)> GeneratedShaders;
173 GeneratedShaders _generated_shaders;
174
175 void analyze_renderstate(ShaderKey &key, const RenderState *rs);
176
177 static std::string combine_mode_as_string(const ShaderKey::TextureInfo &info,
178 TextureStage::CombineMode c_mode, bool alpha, short texindex);
179 static std::string combine_source_as_string(const ShaderKey::TextureInfo &info,
180 short num, bool alpha, short texindex);
181 static const char *texture_type_as_string(Texture::TextureType ttype);
182
183public:
184 static TypeHandle get_class_type() {
185 return _type_handle;
186 }
187 static void init_type() {
188 TypedReferenceCount::init_type();
189 register_type(_type_handle, "ShaderGenerator",
190 TypedReferenceCount::get_class_type());
191 }
192 virtual TypeHandle get_type() const {
193 return get_class_type();
194 }
195 virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
196
197private:
198 static TypeHandle _type_handle;
199};
200
201#else
202
203// If we don't have Cg, let's replace this with a stub.
204class EXPCL_PANDA_PGRAPHNODES ShaderGenerator : public TypedReferenceCount {
205PUBLISHED:
207 virtual ~ShaderGenerator();
208
209 virtual CPT(ShaderAttrib) synthesize_shader(const RenderState *rs,
210 const GeomVertexAnimationSpec &anim);
211
212 void rehash_generated_shaders();
213 void clear_generated_shaders();
214
215public:
216 static TypeHandle get_class_type() {
217 return _type_handle;
218 }
219 static void init_type() {
220 TypedReferenceCount::init_type();
221 register_type(_type_handle, "ShaderGenerator",
222 TypedReferenceCount::get_class_type());
223 }
224 virtual TypeHandle get_type() const {
225 return get_class_type();
226 }
227 virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
228
229private:
230 static TypeHandle _type_handle;
231};
232
233#include "shaderGenerator.I"
234
235#endif // HAVE_CG
236
237#endif // SHADERGENERATOR_H
A light source that seems to illuminate all points in space at once.
This is a const pointer to an InternalName, and should be used in lieu of a CPT(InternalName) in func...
A light shining from infinitely far away in a particular direction, like sunlight.
This object describes how the vertex animation, if any, represented in a GeomVertexData is encoded.
This is a base class for the GraphicsStateGuardian class, which is itself a base class for the variou...
Indicates which set of lights should be considered "on" to illuminate geometry at this level and belo...
Definition lightAttrib.h:30
A Light Ramp is any unary operator that takes a rendered pixel as input, and adjusts the brightness o...
A light originating from a single point in space, and shining in all directions.
Definition pointLight.h:25
This represents a unique collection of RenderAttrib objects that correspond to a particular renderabl...
Definition renderState.h:47
A light originating from a single point in space, and shining in a particular direction,...
Definition spotlight.h:32
TypeHandle is the identifier used to differentiate C++ class types.
Definition typeHandle.h:81
A base class for things which need to inherit from both TypedObject and from ReferenceCount.
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.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
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(),...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.