00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef GRAPHICSSTATEGUARDIAN_H
00020 #define GRAPHICSSTATEGUARDIAN_H
00021
00022 #include "pandabase.h"
00023
00024 #include "savedFrameBuffer.h"
00025 #include "frameBufferStack.h"
00026 #include "frameBufferProperties.h"
00027 #include "displayRegionStack.h"
00028 #include "lensStack.h"
00029
00030 #include "graphicsStateGuardianBase.h"
00031 #include "graphicsThreadingModel.h"
00032 #include "graphicsPipe.h"
00033 #include "sceneSetup.h"
00034 #include "luse.h"
00035 #include "coordinateSystem.h"
00036 #include "factory.h"
00037 #include "pStatCollector.h"
00038 #include "transformState.h"
00039 #include "renderState.h"
00040 #include "light.h"
00041 #include "planeNode.h"
00042 #include "colorWriteAttrib.h"
00043 #include "colorBlendAttrib.h"
00044 #include "transparencyAttrib.h"
00045 #include "config_display.h"
00046
00047 #include "notify.h"
00048 #include "pvector.h"
00049
00050 class ClearableRegion;
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062 class EXPCL_PANDA GraphicsStateGuardian : public GraphicsStateGuardianBase {
00063
00064
00065
00066 public:
00067 GraphicsStateGuardian(const FrameBufferProperties &properties);
00068 virtual ~GraphicsStateGuardian();
00069
00070 PUBLISHED:
00071 void release_all_textures();
00072 void release_all_geoms();
00073
00074 public:
00075 INLINE const FrameBufferProperties &get_properties() const;
00076 INLINE GraphicsPipe *get_pipe() const;
00077 INLINE const GraphicsThreadingModel &get_threading_model() const;
00078
00079 INLINE void set_scene(SceneSetup *scene_setup);
00080 INLINE SceneSetup *get_scene() const;
00081
00082 virtual TextureContext *prepare_texture(Texture *tex);
00083 virtual void apply_texture(TextureContext *tc);
00084 virtual void release_texture(TextureContext *tc);
00085
00086 virtual GeomNodeContext *prepare_geom_node(GeomNode *node);
00087 virtual void draw_geom_node(GeomNode *node, const RenderState *state,
00088 GeomNodeContext *gnc);
00089 virtual void release_geom_node(GeomNodeContext *gnc);
00090
00091 virtual GeomContext *prepare_geom(Geom *geom);
00092 virtual void release_geom(GeomContext *gc);
00093
00094 virtual void set_state_and_transform(const RenderState *state,
00095 const TransformState *transform);
00096
00097 virtual void set_color_clear_value(const Colorf &value);
00098 virtual void set_depth_clear_value(const float value);
00099 virtual void do_clear(const RenderBuffer &buffer)=0;
00100
00101 void clear(ClearableRegion *clearable);
00102 INLINE void clear(DisplayRegion *dr);
00103
00104 virtual void prepare_display_region()=0;
00105 virtual bool prepare_lens();
00106
00107 INLINE void enable_normals(bool val) { _normals_enabled = val; }
00108
00109 virtual bool begin_frame();
00110 virtual bool begin_scene();
00111 virtual void end_scene();
00112 virtual void end_frame();
00113
00114
00115
00116
00117 virtual bool wants_normals() const;
00118 virtual bool wants_texcoords() const;
00119 virtual bool wants_colors() const;
00120
00121 virtual bool depth_offset_decals();
00122 virtual CPT(RenderState) begin_decal_base_first();
00123 virtual CPT(RenderState) begin_decal_nested();
00124 virtual CPT(RenderState) begin_decal_base_second();
00125 virtual void finish_decal();
00126
00127 INLINE void reset_if_new();
00128 virtual void reset();
00129
00130 INLINE void modify_state(const RenderState *state);
00131 INLINE void set_state(const RenderState *state);
00132 INLINE void set_transform(const TransformState *transform);
00133
00134 RenderBuffer get_render_buffer(int buffer_type);
00135
00136 INLINE const DisplayRegion *get_current_display_region(void) const;
00137 INLINE const Lens *get_current_lens() const;
00138
00139 INLINE DisplayRegionStack push_display_region(const DisplayRegion *dr);
00140 INLINE void pop_display_region(DisplayRegionStack &node);
00141 INLINE FrameBufferStack push_frame_buffer(const RenderBuffer &buffer,
00142 const DisplayRegion *dr);
00143 INLINE void pop_frame_buffer(FrameBufferStack &node);
00144
00145 INLINE LensStack push_lens(const Lens *lens);
00146 INLINE void pop_lens(LensStack &stack);
00147 INLINE bool set_lens(const Lens *lens);
00148
00149 INLINE void set_coordinate_system(CoordinateSystem cs);
00150 INLINE CoordinateSystem get_coordinate_system() const;
00151 virtual CoordinateSystem get_internal_coordinate_system() const;
00152
00153 virtual void issue_transform(const TransformState *transform);
00154 virtual void issue_color_scale(const ColorScaleAttrib *attrib);
00155 virtual void issue_color(const ColorAttrib *attrib);
00156 virtual void issue_light(const LightAttrib *attrib);
00157 virtual void issue_color_write(const ColorWriteAttrib *attrib);
00158 virtual void issue_transparency(const TransparencyAttrib *attrib);
00159 virtual void issue_color_blend(const ColorBlendAttrib *attrib);
00160 virtual void issue_clip_plane(const ClipPlaneAttrib *attrib);
00161
00162 virtual void bind_light(PointLight *light, int light_id);
00163 virtual void bind_light(DirectionalLight *light, int light_id);
00164 virtual void bind_light(Spotlight *light, int light_id);
00165
00166 protected:
00167 INLINE Light *get_light(int light_id) const;
00168 virtual bool slot_new_light(int light_id);
00169 virtual void enable_lighting(bool enable);
00170 virtual void set_ambient_light(const Colorf &color);
00171 virtual void enable_light(int light_id, bool enable);
00172 virtual void begin_bind_lights();
00173 virtual void end_bind_lights();
00174
00175 INLINE PlaneNode *get_clip_plane(int plane_id) const;
00176 virtual bool slot_new_clip_plane(int plane_id);
00177 virtual void enable_clip_planes(bool enable);
00178 virtual void enable_clip_plane(int plane_id, bool enable);
00179 virtual void begin_bind_clip_planes();
00180 virtual void bind_clip_plane(PlaneNode *plane, int pane_id);
00181 virtual void end_bind_clip_planes();
00182
00183 virtual void set_blend_mode(ColorWriteAttrib::Mode color_write_mode,
00184 ColorBlendAttrib::Mode color_blend_mode,
00185 TransparencyAttrib::Mode transparency_mode);
00186
00187 virtual PT(SavedFrameBuffer) save_frame_buffer(const RenderBuffer &buffer,
00188 CPT(DisplayRegion) dr)=0;
00189 virtual void restore_frame_buffer(SavedFrameBuffer *frame_buffer)=0;
00190
00191 bool mark_prepared_texture(TextureContext *tc);
00192 bool unmark_prepared_texture(TextureContext *tc);
00193 bool mark_prepared_geom(GeomContext *gc);
00194 bool unmark_prepared_geom(GeomContext *gc);
00195 bool mark_prepared_geom_node(GeomNodeContext *gnc);
00196 bool unmark_prepared_geom_node(GeomNodeContext *gnc);
00197
00198 virtual void free_pointers();
00199 virtual void close_gsg();
00200
00201 #ifdef DO_PSTATS
00202
00203
00204 void init_frame_pstats();
00205 void add_to_texture_record(TextureContext *tc);
00206 void add_to_geom_record(GeomContext *gc);
00207 void add_to_geom_node_record(GeomNodeContext *gnc);
00208 void record_state_change(TypeHandle type);
00209 pset<TextureContext *> _current_textures;
00210 pset<GeomContext *> _current_geoms;
00211 pset<GeomNodeContext *> _current_geom_nodes;
00212 #else
00213 INLINE void init_frame_pstats() { }
00214 INLINE void add_to_texture_record(TextureContext *) { }
00215 INLINE void add_to_geom_record(GeomContext *) { }
00216 INLINE void add_to_geom_node_record(GeomNodeContext *) { }
00217 INLINE void record_state_change(TypeHandle) { }
00218 #endif
00219
00220 static CPT(RenderState) get_unlit_state();
00221 static CPT(RenderState) get_unclipped_state();
00222 static CPT(RenderState) get_untextured_state();
00223
00224 protected:
00225 PT(SceneSetup) _scene_setup;
00226
00227 CPT(RenderState) _state;
00228 CPT(TransformState) _transform;
00229
00230 int _buffer_mask;
00231 Colorf _color_clear_value;
00232 float _depth_clear_value;
00233 bool _stencil_clear_value;
00234 Colorf _accum_clear_value;
00235 int _clear_buffer_type;
00236
00237 int _display_region_stack_level;
00238 int _frame_buffer_stack_level;
00239 int _lens_stack_level;
00240
00241 CPT(DisplayRegion) _current_display_region;
00242 CPT(Lens) _current_lens;
00243
00244
00245 bool _normals_enabled;
00246
00247 CoordinateSystem _coordinate_system;
00248
00249 Colorf _scene_graph_color;
00250 bool _has_scene_graph_color;
00251 bool _scene_graph_color_stale;
00252 bool _vertex_colors_enabled;
00253 bool _lighting_enabled;
00254 bool _clip_planes_enabled;
00255
00256 enum ColorTransform {
00257 CT_offset = 0x01,
00258 CT_scale = 0x02,
00259 };
00260 int _color_transform_enabled;
00261 LVecBase4f _current_color_offset;
00262 LVecBase4f _current_color_scale;
00263
00264 ColorWriteAttrib::Mode _color_write_mode;
00265 ColorBlendAttrib::Mode _color_blend_mode;
00266 TransparencyAttrib::Mode _transparency_mode;
00267
00268 bool _needs_reset;
00269 bool _closing_gsg;
00270
00271 public:
00272
00273 static PStatCollector _total_texusage_pcollector;
00274 static PStatCollector _active_texusage_pcollector;
00275 static PStatCollector _total_geom_pcollector;
00276 static PStatCollector _active_geom_pcollector;
00277 static PStatCollector _total_geom_node_pcollector;
00278 static PStatCollector _active_geom_node_pcollector;
00279 static PStatCollector _total_texmem_pcollector;
00280 static PStatCollector _used_texmem_pcollector;
00281 static PStatCollector _texmgrmem_total_pcollector;
00282 static PStatCollector _texmgrmem_resident_pcollector;
00283 static PStatCollector _vertices_pcollector;
00284 static PStatCollector _vertices_tristrip_pcollector;
00285 static PStatCollector _vertices_trifan_pcollector;
00286 static PStatCollector _vertices_tri_pcollector;
00287 static PStatCollector _vertices_other_pcollector;
00288 static PStatCollector _state_changes_pcollector;
00289 static PStatCollector _transform_state_pcollector;
00290 static PStatCollector _texture_state_pcollector;
00291 static PStatCollector _nodes_pcollector;
00292 static PStatCollector _geom_nodes_pcollector;
00293 static PStatCollector _frustum_cull_volumes_pcollector;
00294 static PStatCollector _frustum_cull_transforms_pcollector;
00295 static PStatCollector _set_state_pcollector;
00296 static PStatCollector _draw_primitive_pcollector;
00297 static PStatCollector _transform_states_pcollector;
00298 static PStatCollector _transform_states_unused_pcollector;
00299 static PStatCollector _render_states_pcollector;
00300 static PStatCollector _render_states_unused_pcollector;
00301
00302 private:
00303 class LightInfo {
00304 public:
00305 INLINE LightInfo();
00306 PT(Light) _light;
00307 bool _enabled;
00308 bool _next_enabled;
00309 };
00310
00311 pvector<LightInfo> _light_info;
00312 bool _lighting_enabled_this_frame;
00313
00314 class ClipPlaneInfo {
00315 public:
00316 INLINE ClipPlaneInfo();
00317 PT(PlaneNode) _plane;
00318 bool _enabled;
00319 bool _next_enabled;
00320 };
00321
00322 pvector<ClipPlaneInfo> _clip_plane_info;
00323 bool _clip_planes_enabled_this_frame;
00324
00325
00326
00327 typedef pset<TextureContext *> Textures;
00328 Textures _prepared_textures;
00329 typedef pset<GeomContext *> Geoms;
00330 Geoms _prepared_geoms;
00331 typedef pset<GeomNodeContext *> GeomNodes;
00332 GeomNodes _prepared_geom_nodes;
00333
00334 FrameBufferProperties _properties;
00335 PT(GraphicsPipe) _pipe;
00336 GraphicsThreadingModel _threading_model;
00337
00338 public:
00339 void traverse_prepared_textures(bool (*pertex_callbackfn)(TextureContext *,void *),void *callback_arg);
00340
00341 public:
00342 static TypeHandle get_class_type() {
00343 return _type_handle;
00344 }
00345
00346 public:
00347 static void init_type() {
00348 GraphicsStateGuardianBase::init_type();
00349 register_type(_type_handle, "GraphicsStateGuardian",
00350 GraphicsStateGuardianBase::get_class_type());
00351 }
00352 virtual TypeHandle get_type() const {
00353 return get_class_type();
00354 }
00355 virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
00356
00357 private:
00358 static TypeHandle _type_handle;
00359
00360 friend class GraphicsPipe;
00361 friend class GraphicsWindow;
00362 friend class GraphicsEngine;
00363 };
00364
00365 #include "graphicsStateGuardian.I"
00366
00367 #endif