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