00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef GRAPHICSSTATEGUARDIAN_H
00018 #define GRAPHICSSTATEGUARDIAN_H
00019
00020 #include "pandabase.h"
00021
00022 #include "frameBufferProperties.h"
00023 #include "preparedGraphicsObjects.h"
00024 #include "lens.h"
00025 #include "graphicsStateGuardianBase.h"
00026 #include "graphicsThreadingModel.h"
00027 #include "graphicsPipe.h"
00028 #include "sceneSetup.h"
00029 #include "displayRegion.h"
00030 #include "luse.h"
00031 #include "coordinateSystem.h"
00032 #include "factory.h"
00033 #include "pStatCollector.h"
00034 #include "transformState.h"
00035 #include "renderState.h"
00036 #include "light.h"
00037 #include "planeNode.h"
00038 #include "config_display.h"
00039 #include "geomMunger.h"
00040 #include "geomVertexData.h"
00041 #include "pnotify.h"
00042 #include "pvector.h"
00043 #include "shaderContext.h"
00044 #include "bitMask.h"
00045 #include "texture.h"
00046 #include "occlusionQueryContext.h"
00047 #include "stencilRenderStates.h"
00048 #include "loader.h"
00049 #include "textureAttrib.h"
00050 #include "texGenAttrib.h"
00051 #include "shaderAttrib.h"
00052
00053 class DrawableRegion;
00054 class GraphicsEngine;
00055 class ShaderGenerator;
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067 class EXPCL_PANDA_DISPLAY GraphicsStateGuardian : public GraphicsStateGuardianBase {
00068
00069
00070
00071 public:
00072 GraphicsStateGuardian(CoordinateSystem internal_coordinate_system,
00073 GraphicsEngine *engine, GraphicsPipe *pipe);
00074 virtual ~GraphicsStateGuardian();
00075
00076 PUBLISHED:
00077
00078 enum ShaderModel
00079 {
00080 SM_00,
00081 SM_11,
00082 SM_20,
00083 SM_2X,
00084 SM_30,
00085 SM_40,
00086 };
00087
00088 INLINE void release_all();
00089 INLINE int release_all_textures();
00090 INLINE int release_all_geoms();
00091 INLINE int release_all_vertex_buffers();
00092 INLINE int release_all_index_buffers();
00093
00094 INLINE void set_active(bool active);
00095 INLINE bool is_active() const;
00096 INLINE bool is_valid() const;
00097 INLINE bool needs_reset() const;
00098
00099 INLINE void set_incomplete_render(bool incomplete_render);
00100 virtual INLINE bool get_incomplete_render() const;
00101 virtual INLINE bool get_effective_incomplete_render() const;
00102
00103 INLINE void set_loader(Loader *loader);
00104 INLINE Loader *get_loader() const;
00105
00106 INLINE GraphicsPipe *get_pipe() const;
00107 GraphicsEngine *get_engine() const;
00108 INLINE const GraphicsThreadingModel &get_threading_model() const;
00109
00110 INLINE bool is_hardware() const;
00111 virtual INLINE bool prefers_triangle_strips() const;
00112 virtual INLINE int get_max_vertices_per_array() const;
00113 virtual INLINE int get_max_vertices_per_primitive() const;
00114
00115 INLINE int get_max_texture_stages() const;
00116 virtual INLINE int get_max_texture_dimension() const;
00117 INLINE int get_max_3d_texture_dimension() const;
00118 INLINE int get_max_2d_texture_array_layers() const;
00119 INLINE int get_max_cube_map_dimension() const;
00120
00121 INLINE bool get_supports_texture_combine() const;
00122 INLINE bool get_supports_texture_saved_result() const;
00123 INLINE bool get_supports_texture_dot3() const;
00124
00125 INLINE bool get_supports_3d_texture() const;
00126 INLINE bool get_supports_2d_texture_array() const;
00127 INLINE bool get_supports_cube_map() const;
00128 INLINE bool get_supports_tex_non_pow2() const;
00129
00130 INLINE bool get_supports_compressed_texture() const;
00131 virtual INLINE bool get_supports_compressed_texture_format(int compression_mode) const;
00132
00133 INLINE int get_max_lights() const;
00134 INLINE int get_max_clip_planes() const;
00135
00136 INLINE int get_max_vertex_transforms() const;
00137 INLINE int get_max_vertex_transform_indices() const;
00138
00139 INLINE bool get_copy_texture_inverted() const;
00140 virtual bool get_supports_multisample() const;
00141 INLINE bool get_supports_generate_mipmap() const;
00142 INLINE bool get_supports_depth_texture() const;
00143 INLINE bool get_supports_depth_stencil() const;
00144 INLINE bool get_supports_shadow_filter() const;
00145 INLINE bool get_supports_basic_shaders() const;
00146 INLINE bool get_supports_glsl() const;
00147 INLINE bool get_supports_stencil() const;
00148 INLINE bool get_supports_two_sided_stencil() const;
00149 INLINE bool get_supports_geometry_instancing() const;
00150
00151 INLINE int get_maximum_simultaneous_render_targets() const;
00152
00153 INLINE int get_shader_model() const;
00154 INLINE void set_shader_model(int shader_model);
00155
00156 virtual int get_supported_geom_rendering() const;
00157 virtual bool get_supports_cg_profile(const string &name) const;
00158
00159
00160 INLINE bool get_color_scale_via_lighting() const;
00161 INLINE bool get_alpha_scale_via_texture() const;
00162 INLINE bool get_alpha_scale_via_texture(const TextureAttrib *tex_attrib) const;
00163 INLINE bool get_runtime_color_scale() const;
00164
00165 INLINE static TextureStage *get_alpha_scale_texture_stage();
00166
00167 void set_coordinate_system(CoordinateSystem cs);
00168 INLINE CoordinateSystem get_coordinate_system() const;
00169 virtual CoordinateSystem get_internal_coordinate_system() const;
00170
00171 virtual PreparedGraphicsObjects *get_prepared_objects();
00172
00173 virtual bool set_gamma(PN_stdfloat gamma);
00174 PN_stdfloat get_gamma(PN_stdfloat gamma);
00175 virtual void restore_gamma();
00176
00177 INLINE void set_texture_quality_override(Texture::QualityLevel quality_level);
00178 INLINE Texture::QualityLevel get_texture_quality_override() const;
00179
00180 #ifdef HAVE_PYTHON
00181 PyObject *get_prepared_textures() const;
00182 #endif
00183 typedef bool TextureCallback(TextureContext *tc, void *callback_arg);
00184 void traverse_prepared_textures(TextureCallback *func, void *callback_arg);
00185
00186 #ifndef NDEBUG
00187 void set_flash_texture(Texture *tex);
00188 void clear_flash_texture();
00189 Texture *get_flash_texture() const;
00190 #endif
00191
00192 PUBLISHED:
00193
00194 virtual string get_driver_vendor();
00195 virtual string get_driver_renderer();
00196 virtual string get_driver_version();
00197 virtual int get_driver_version_major();
00198 virtual int get_driver_version_minor();
00199 virtual int get_driver_shader_version_major();
00200 virtual int get_driver_shader_version_minor();
00201
00202 bool set_scene(SceneSetup *scene_setup);
00203 virtual SceneSetup *get_scene() const;
00204
00205 public:
00206 virtual TextureContext *prepare_texture(Texture *tex);
00207 virtual bool update_texture(TextureContext *tc, bool force);
00208 virtual void release_texture(TextureContext *tc);
00209 virtual bool extract_texture_data(Texture *tex);
00210
00211 virtual GeomContext *prepare_geom(Geom *geom);
00212 virtual void release_geom(GeomContext *gc);
00213
00214 virtual ShaderContext *prepare_shader(Shader *shader);
00215 virtual void release_shader(ShaderContext *sc);
00216
00217 virtual VertexBufferContext *prepare_vertex_buffer(GeomVertexArrayData *data);
00218 virtual void release_vertex_buffer(VertexBufferContext *vbc);
00219
00220 virtual IndexBufferContext *prepare_index_buffer(GeomPrimitive *data);
00221 virtual void release_index_buffer(IndexBufferContext *ibc);
00222
00223 virtual bool get_supports_occlusion_query() const;
00224 virtual void begin_occlusion_query();
00225 virtual PT(OcclusionQueryContext) end_occlusion_query();
00226
00227 virtual PT(GeomMunger) get_geom_munger(const RenderState *state,
00228 Thread *current_thread);
00229 virtual PT(GeomMunger) make_geom_munger(const RenderState *state,
00230 Thread *current_thread);
00231
00232 virtual void set_state_and_transform(const RenderState *state,
00233 const TransformState *transform);
00234
00235 virtual PN_stdfloat compute_distance_to(const LPoint3 &point) const;
00236
00237 virtual void clear(DrawableRegion *clearable);
00238
00239 const LMatrix4 *fetch_specified_value(Shader::ShaderMatSpec &spec, int altered);
00240 const LMatrix4 *fetch_specified_part(Shader::ShaderMatInput input, InternalName *name, LMatrix4 &t);
00241 const Shader::ShaderPtrData *fetch_ptr_parameter(const Shader::ShaderPtrSpec& spec);
00242
00243 virtual void prepare_display_region(DisplayRegionPipelineReader *dr);
00244 virtual void clear_before_callback();
00245 virtual void clear_state_and_transform();
00246
00247 virtual void remove_window(GraphicsOutputBase *window);
00248
00249 virtual CPT(TransformState) calc_projection_mat(const Lens *lens);
00250 virtual bool prepare_lens();
00251
00252 virtual bool begin_frame(Thread *current_thread);
00253 PUBLISHED:
00254 virtual bool begin_scene();
00255 virtual void end_scene();
00256 public:
00257 virtual void end_frame(Thread *current_thread);
00258
00259 void set_current_properties(const FrameBufferProperties *properties);
00260
00261 virtual bool depth_offset_decals();
00262 virtual CPT(RenderState) begin_decal_base_first();
00263 virtual CPT(RenderState) begin_decal_nested();
00264 virtual CPT(RenderState) begin_decal_base_second();
00265 virtual void finish_decal();
00266
00267 virtual bool begin_draw_primitives(const GeomPipelineReader *geom_reader,
00268 const GeomMunger *munger,
00269 const GeomVertexDataPipelineReader *data_reader,
00270 bool force);
00271 virtual bool draw_triangles(const GeomPrimitivePipelineReader *reader,
00272 bool force);
00273 virtual bool draw_tristrips(const GeomPrimitivePipelineReader *reader,
00274 bool force);
00275 virtual bool draw_trifans(const GeomPrimitivePipelineReader *reader,
00276 bool force);
00277 virtual bool draw_lines(const GeomPrimitivePipelineReader *reader,
00278 bool force);
00279 virtual bool draw_linestrips(const GeomPrimitivePipelineReader *reader,
00280 bool force);
00281 virtual bool draw_points(const GeomPrimitivePipelineReader *reader,
00282 bool force);
00283 virtual void end_draw_primitives();
00284
00285 INLINE bool reset_if_new();
00286 INLINE void mark_new();
00287 virtual void reset();
00288
00289 INLINE CPT(TransformState) get_external_transform() const;
00290 INLINE CPT(TransformState) get_internal_transform() const;
00291
00292 RenderBuffer get_render_buffer(int buffer_type, const FrameBufferProperties &prop);
00293
00294 INLINE const DisplayRegion *get_current_display_region() const;
00295 INLINE Lens::StereoChannel get_current_stereo_channel() const;
00296 INLINE int get_current_tex_view_offset() const;
00297 INLINE const Lens *get_current_lens() const;
00298
00299 virtual const TransformState *get_cs_transform() const;
00300 INLINE const TransformState *get_inv_cs_transform() const;
00301
00302 void do_issue_clip_plane();
00303 void do_issue_color();
00304 void do_issue_color_scale();
00305 virtual void do_issue_light();
00306
00307 virtual bool framebuffer_copy_to_texture
00308 (Texture *tex, int z, const DisplayRegion *dr, const RenderBuffer &rb);
00309 virtual bool framebuffer_copy_to_ram
00310 (Texture *tex, int z, const DisplayRegion *dr, const RenderBuffer &rb);
00311
00312 virtual void bind_light(PointLight *light_obj, const NodePath &light,
00313 int light_id);
00314 virtual void bind_light(DirectionalLight *light_obj, const NodePath &light,
00315 int light_id);
00316 virtual void bind_light(Spotlight *light_obj, const NodePath &light,
00317 int light_id);
00318
00319 static void create_gamma_table (PN_stdfloat gamma, unsigned short *red_table, unsigned short *green_table, unsigned short *blue_table);
00320
00321 virtual PT(Texture) make_shadow_buffer(const NodePath &light_np, GraphicsOutputBase *host);
00322
00323 #ifdef DO_PSTATS
00324 static void init_frame_pstats();
00325 #endif
00326
00327 protected:
00328 virtual void reissue_transforms();
00329
00330 virtual void enable_lighting(bool enable);
00331 virtual void set_ambient_light(const LColor &color);
00332 virtual void enable_light(int light_id, bool enable);
00333 virtual void begin_bind_lights();
00334 virtual void end_bind_lights();
00335
00336 virtual void enable_clip_planes(bool enable);
00337 virtual void enable_clip_plane(int plane_id, bool enable);
00338 virtual void begin_bind_clip_planes();
00339 virtual void bind_clip_plane(const NodePath &plane, int plane_id);
00340 virtual void end_bind_clip_planes();
00341
00342 void determine_target_texture();
00343
00344 virtual void free_pointers();
00345 virtual void close_gsg();
00346 void panic_deactivate();
00347
00348 void determine_light_color_scale();
00349
00350 static CPT(RenderState) get_unlit_state();
00351 static CPT(RenderState) get_unclipped_state();
00352 static CPT(RenderState) get_untextured_state();
00353
00354 void async_reload_texture(TextureContext *tc);
00355
00356 protected:
00357 PT(SceneSetup) _scene_null;
00358 PT(SceneSetup) _scene_setup;
00359
00360
00361
00362 CPT(RenderState) _state_rs;
00363
00364
00365
00366 CPT(RenderState) _target_rs;
00367
00368
00369
00370
00371
00372
00373
00374 RenderState::SlotMask _state_mask;
00375 RenderState::SlotMask _inv_state_mask;
00376
00377
00378
00379 CPT(TransformState) _internal_transform;
00380
00381
00382
00383
00384
00385 CPT(TextureAttrib) _target_texture;
00386 CPT(TextureAttrib) _state_texture;
00387 CPT(TexGenAttrib) _target_tex_gen;
00388 CPT(TexGenAttrib) _state_tex_gen;
00389
00390
00391
00392 CPT(ShaderAttrib) _state_shader;
00393 CPT(ShaderAttrib) _target_shader;
00394
00395
00396
00397 CPT(GeomMunger) _munger;
00398 const GeomVertexDataPipelineReader *_data_reader;
00399
00400 unsigned int _color_write_mask;
00401
00402 CPT(DisplayRegion) _current_display_region;
00403 Lens::StereoChannel _current_stereo_channel;
00404 int _current_tex_view_offset;
00405 CPT(Lens) _current_lens;
00406 CPT(TransformState) _projection_mat;
00407 CPT(TransformState) _projection_mat_inv;
00408 const FrameBufferProperties *_current_properties;
00409
00410 CoordinateSystem _coordinate_system;
00411 CoordinateSystem _internal_coordinate_system;
00412 CPT(TransformState) _cs_transform;
00413 CPT(TransformState) _inv_cs_transform;
00414
00415 LColor _scene_graph_color;
00416 bool _has_scene_graph_color;
00417 bool _transform_stale;
00418 bool _color_blend_involves_color_scale;
00419 bool _texture_involves_color_scale;
00420 bool _vertex_colors_enabled;
00421 bool _lighting_enabled;
00422 bool _clip_planes_enabled;
00423 bool _color_scale_enabled;
00424 LVecBase4 _current_color_scale;
00425
00426 bool _has_material_force_color;
00427 LColor _material_force_color;
00428 LVecBase4 _light_color_scale;
00429 bool _has_texture_alpha_scale;
00430
00431 bool _tex_gen_modifies_mat;
00432 bool _tex_gen_point_sprite;
00433 int _last_max_stage_index;
00434
00435 bool _needs_reset;
00436 bool _is_valid;
00437 bool _closing_gsg;
00438 bool _active;
00439 bool _incomplete_render;
00440 bool _effective_incomplete_render;
00441 PT(Loader) _loader;
00442
00443 PT(PreparedGraphicsObjects) _prepared_objects;
00444
00445 bool _is_hardware;
00446 bool _prefers_triangle_strips;
00447 int _max_vertices_per_array;
00448 int _max_vertices_per_primitive;
00449
00450 int _max_texture_stages;
00451 int _max_texture_dimension;
00452 int _max_3d_texture_dimension;
00453 int _max_2d_texture_array_layers;
00454 int _max_cube_map_dimension;
00455
00456 bool _supports_texture_combine;
00457 bool _supports_texture_saved_result;
00458 bool _supports_texture_dot3;
00459
00460 bool _supports_3d_texture;
00461 bool _supports_2d_texture_array;
00462 bool _supports_cube_map;
00463 bool _supports_tex_non_pow2;
00464
00465 bool _supports_compressed_texture;
00466 BitMask32 _compressed_texture_formats;
00467
00468 int _max_lights;
00469 int _max_clip_planes;
00470
00471 int _max_vertex_transforms;
00472 int _max_vertex_transform_indices;
00473
00474 bool _supports_occlusion_query;
00475 PT(OcclusionQueryContext) _current_occlusion_query;
00476
00477 bool _copy_texture_inverted;
00478 bool _supports_multisample;
00479 bool _supports_generate_mipmap;
00480 bool _supports_depth_texture;
00481 bool _supports_depth_stencil;
00482 bool _supports_shadow_filter;
00483 bool _supports_basic_shaders;
00484 bool _supports_glsl;
00485 bool _supports_framebuffer_multisample;
00486 bool _supports_framebuffer_blit;
00487
00488 bool _supports_stencil;
00489 bool _supports_stencil_wrap;
00490 bool _supports_two_sided_stencil;
00491 bool _supports_geometry_instancing;
00492
00493 int _maximum_simultaneous_render_targets;
00494
00495 int _supported_geom_rendering;
00496 bool _color_scale_via_lighting;
00497 bool _alpha_scale_via_texture;
00498 bool _runtime_color_scale;
00499
00500 int _stereo_buffer_mask;
00501
00502 StencilRenderStates *_stencil_render_states;
00503
00504 int _auto_detect_shader_model;
00505 int _shader_model;
00506
00507 static PT(TextureStage) _alpha_scale_texture_stage;
00508
00509 Shader::ShaderCaps _shader_caps;
00510
00511 PN_stdfloat _gamma;
00512 Texture::QualityLevel _texture_quality_override;
00513
00514 ShaderGenerator* _shader_generator;
00515
00516 #ifndef NDEBUG
00517 PT(Texture) _flash_texture;
00518 #endif
00519
00520 public:
00521
00522 static PStatCollector _vertex_buffer_switch_pcollector;
00523 static PStatCollector _index_buffer_switch_pcollector;
00524 static PStatCollector _load_vertex_buffer_pcollector;
00525 static PStatCollector _load_index_buffer_pcollector;
00526 static PStatCollector _create_vertex_buffer_pcollector;
00527 static PStatCollector _create_index_buffer_pcollector;
00528 static PStatCollector _load_texture_pcollector;
00529 static PStatCollector _data_transferred_pcollector;
00530 static PStatCollector _texmgrmem_total_pcollector;
00531 static PStatCollector _texmgrmem_resident_pcollector;
00532 static PStatCollector _primitive_batches_pcollector;
00533 static PStatCollector _primitive_batches_tristrip_pcollector;
00534 static PStatCollector _primitive_batches_trifan_pcollector;
00535 static PStatCollector _primitive_batches_tri_pcollector;
00536 static PStatCollector _primitive_batches_other_pcollector;
00537 static PStatCollector _vertices_tristrip_pcollector;
00538 static PStatCollector _vertices_trifan_pcollector;
00539 static PStatCollector _vertices_tri_pcollector;
00540 static PStatCollector _vertices_other_pcollector;
00541 static PStatCollector _vertices_indexed_tristrip_pcollector;
00542 static PStatCollector _state_pcollector;
00543 static PStatCollector _transform_state_pcollector;
00544 static PStatCollector _texture_state_pcollector;
00545 static PStatCollector _draw_primitive_pcollector;
00546 static PStatCollector _draw_set_state_pcollector;
00547 static PStatCollector _clear_pcollector;
00548 static PStatCollector _flush_pcollector;
00549 static PStatCollector _wait_occlusion_pcollector;
00550
00551
00552
00553 static PStatCollector _draw_set_state_transform_pcollector;
00554 static PStatCollector _draw_set_state_alpha_test_pcollector;
00555 static PStatCollector _draw_set_state_antialias_pcollector;
00556 static PStatCollector _draw_set_state_clip_plane_pcollector;
00557 static PStatCollector _draw_set_state_color_pcollector;
00558 static PStatCollector _draw_set_state_cull_face_pcollector;
00559 static PStatCollector _draw_set_state_depth_offset_pcollector;
00560 static PStatCollector _draw_set_state_depth_test_pcollector;
00561 static PStatCollector _draw_set_state_depth_write_pcollector;
00562 static PStatCollector _draw_set_state_render_mode_pcollector;
00563 static PStatCollector _draw_set_state_rescale_normal_pcollector;
00564 static PStatCollector _draw_set_state_shade_model_pcollector;
00565 static PStatCollector _draw_set_state_blending_pcollector;
00566 static PStatCollector _draw_set_state_shader_pcollector;
00567 static PStatCollector _draw_set_state_shader_parameters_pcollector;
00568 static PStatCollector _draw_set_state_texture_pcollector;
00569 static PStatCollector _draw_set_state_tex_matrix_pcollector;
00570 static PStatCollector _draw_set_state_tex_gen_pcollector;
00571 static PStatCollector _draw_set_state_material_pcollector;
00572 static PStatCollector _draw_set_state_light_pcollector;
00573 static PStatCollector _draw_set_state_stencil_pcollector;
00574 static PStatCollector _draw_set_state_fog_pcollector;
00575 static PStatCollector _draw_set_state_scissor_pcollector;
00576
00577 private:
00578 int _num_lights_enabled;
00579 int _num_clip_planes_enabled;
00580
00581 PT(GraphicsPipe) _pipe;
00582 GraphicsEngine *_engine;
00583 GraphicsThreadingModel _threading_model;
00584
00585 public:
00586 static TypeHandle get_class_type() {
00587 return _type_handle;
00588 }
00589
00590 public:
00591 static void init_type() {
00592 GraphicsStateGuardianBase::init_type();
00593 register_type(_type_handle, "GraphicsStateGuardian",
00594 GraphicsStateGuardianBase::get_class_type());
00595 }
00596 virtual TypeHandle get_type() const {
00597 return get_class_type();
00598 }
00599 virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
00600
00601 private:
00602 static TypeHandle _type_handle;
00603
00604 friend class GraphicsPipe;
00605 friend class GraphicsWindow;
00606 friend class GraphicsEngine;
00607 };
00608
00609 #include "graphicsStateGuardian.I"
00610
00611 #endif