Panda3D
 All Classes Functions Variables Enumerations
dxGraphicsStateGuardian9.h
00001 // Filename: dxGraphicsStateGuardian9.h
00002 // Created by:  mike (02Feb99)
00003 // Updated by: fperazzi, PandaSE (05May10) (added get_supports_cg_profile)
00004 //
00005 ////////////////////////////////////////////////////////////////////
00006 //
00007 // PANDA 3D SOFTWARE
00008 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00009 //
00010 // All use of this software is subject to the terms of the revised BSD
00011 // license.  You should have received a copy of this license along
00012 // with this source code in a file named "LICENSE."
00013 //
00014 ////////////////////////////////////////////////////////////////////
00015 
00016 #ifndef DXGRAPHICSSTATEGUARDIAN9_H
00017 #define DXGRAPHICSSTATEGUARDIAN9_H
00018 
00019 #include "dxgsg9base.h"
00020 #include "dxTextureContext9.h"
00021 #include "config_dxgsg9.h"
00022 
00023 #include "graphicsStateGuardian.h"
00024 #include "texture.h"
00025 #include "displayRegion.h"
00026 #include "material.h"
00027 #include "depthTestAttrib.h"
00028 #include "cullFaceAttrib.h"
00029 #include "renderModeAttrib.h"
00030 #include "colorBlendAttrib.h"
00031 #include "fog.h"
00032 #include "pointerToArray.h"
00033 
00034 #include "lru.h"
00035 
00036 #include "vertexElementArray.h"
00037 #include "dxShaderContext9.h"
00038 
00039 
00040 enum GsgPageType
00041 {
00042   GPT_Texture,
00043   GPT_VertexBuffer,
00044   GPT_IndexBuffer,
00045 
00046   GPT_TotalPageTypes
00047 };
00048 
00049 class Light;
00050 
00051 class DXTextureContext9;
00052 class DXVertexBufferContext9;
00053 class DXIndexBufferContext9;
00054 
00055 ////////////////////////////////////////////////////////////////////
00056 //       Class : DXGraphicsStateGuardian9
00057 // Description : A GraphicsStateGuardian for rendering into DirectX9
00058 //               contexts.
00059 ////////////////////////////////////////////////////////////////////
00060 class EXPCL_PANDADX DXGraphicsStateGuardian9 : public GraphicsStateGuardian {
00061 public:
00062   DXGraphicsStateGuardian9(GraphicsEngine *engine, GraphicsPipe *pipe);
00063   ~DXGraphicsStateGuardian9();
00064 
00065   FrameBufferProperties
00066     calc_fb_properties(DWORD cformat, DWORD dformat,
00067                        DWORD multisampletype, DWORD multisamplequality);
00068 
00069   virtual TextureContext *prepare_texture(Texture *tex, int view);
00070   void apply_texture(int i, TextureContext *tc);
00071   virtual bool update_texture(TextureContext *tc, bool force);
00072   bool upload_texture(DXTextureContext9 *dtc, bool force);
00073   virtual void release_texture(TextureContext *tc);
00074   virtual bool extract_texture_data(Texture *tex);
00075 
00076   ShaderContext *prepare_shader(Shader *se);
00077   void release_shader(ShaderContext *sc);
00078 
00079   virtual VertexBufferContext *prepare_vertex_buffer(GeomVertexArrayData *data);
00080   bool apply_vertex_buffer(VertexBufferContext *vbc,
00081                            const GeomVertexArrayDataHandle *reader,
00082                            bool force);
00083   virtual void release_vertex_buffer(VertexBufferContext *vbc);
00084 
00085   bool setup_array_data(CLP(VertexBufferContext)*& vbc,
00086                         const GeomVertexArrayDataHandle* data,
00087                         bool force);
00088 
00089   virtual IndexBufferContext *prepare_index_buffer(GeomPrimitive *data);
00090   bool apply_index_buffer(IndexBufferContext *ibc,
00091                           const GeomPrimitivePipelineReader *reader, bool force);
00092   virtual void release_index_buffer(IndexBufferContext *ibc);
00093 
00094   virtual void begin_occlusion_query();
00095   virtual PT(OcclusionQueryContext) end_occlusion_query();
00096 
00097   virtual PT(GeomMunger) make_geom_munger(const RenderState *state,
00098                                           Thread *current_thread);
00099 
00100   virtual void clear(DrawableRegion *clearable);
00101 
00102   virtual void prepare_display_region(DisplayRegionPipelineReader *dr);
00103   virtual CPT(TransformState) calc_projection_mat(const Lens *lens);
00104   virtual bool prepare_lens();
00105 
00106   virtual bool begin_frame(Thread *current_thread);
00107   virtual bool begin_scene();
00108   virtual void end_scene();
00109   virtual void end_frame(Thread *current_thread);
00110 
00111   virtual bool begin_draw_primitives(const GeomPipelineReader *geom_reader,
00112                                      const GeomMunger *munger,
00113                                      const GeomVertexDataPipelineReader *data_reader,
00114                                      bool force);
00115   virtual bool draw_triangles(const GeomPrimitivePipelineReader *reader,
00116                               bool force);
00117   virtual bool draw_tristrips(const GeomPrimitivePipelineReader *reader,
00118                               bool force);
00119   virtual bool draw_trifans(const GeomPrimitivePipelineReader *reader,
00120                             bool force);
00121   virtual bool draw_lines(const GeomPrimitivePipelineReader *reader,
00122                           bool force);
00123   virtual bool draw_linestrips(const GeomPrimitivePipelineReader *reader,
00124                                bool force);
00125   virtual bool draw_points(const GeomPrimitivePipelineReader *reader,
00126                            bool force);
00127   virtual void end_draw_primitives();
00128 
00129   virtual bool framebuffer_copy_to_texture(Texture *tex, int z, const DisplayRegion *dr,
00130                                            const RenderBuffer &rb);
00131   virtual bool framebuffer_copy_to_ram(Texture *tex, int z, const DisplayRegion *dr,
00132                                        const RenderBuffer &rb);
00133   bool do_framebuffer_copy_to_ram(Texture *tex, int z, const DisplayRegion *dr,
00134                                   const RenderBuffer &rb, bool inverted);
00135 
00136   void reset_render_states (void);
00137   virtual void reset();
00138 
00139   virtual void apply_fog(Fog *fog);
00140 
00141   virtual void bind_light(PointLight *light_obj, const NodePath &light,
00142                           int light_id);
00143   virtual void bind_light(DirectionalLight *light_obj, const NodePath &light,
00144                           int light_id);
00145   virtual void bind_light(Spotlight *light_obj, const NodePath &light,
00146                           int light_id);
00147 
00148   static D3DFORMAT get_index_type(Geom::NumericType numeric_type);
00149   INLINE static DWORD LColor_to_D3DCOLOR(const LColor &cLColor);
00150 
00151   virtual void set_state_and_transform(const RenderState *state,
00152                                        const TransformState *transform);
00153 
00154   bool check_dx_allocation (HRESULT result, int allocation_size, int attempts);
00155 
00156   INLINE HRESULT set_render_state (D3DRENDERSTATETYPE state, DWORD value);
00157   INLINE HRESULT set_texture_stage_state (DWORD stage, D3DTEXTURESTAGESTATETYPE type, DWORD value);
00158   INLINE HRESULT set_sampler_state (DWORD sampler, D3DSAMPLERSTATETYPE type, DWORD value);
00159 
00160   INLINE bool get_supports_render_texture() const;
00161 
00162   static bool get_gamma_table(void);
00163   static bool static_set_gamma(bool restore, PN_stdfloat gamma);
00164   bool set_gamma(PN_stdfloat gamma);
00165   void restore_gamma();
00166   static void atexit_function(void);
00167 
00168   static void set_cg_device(LPDIRECT3DDEVICE9 cg_device);
00169   virtual bool get_supports_cg_profile(const string &name) const;
00170 
00171 
00172 protected:
00173   void do_issue_transform();
00174   void do_issue_alpha_test();
00175   void do_issue_shader();
00176   void do_issue_render_mode();
00177   void do_issue_rescale_normal();
00178   void do_issue_color_write();
00179   void do_issue_depth_test();
00180   void do_issue_depth_write();
00181   void do_issue_cull_face();
00182   void do_issue_fog();
00183   void do_issue_depth_offset();
00184   void do_issue_tex_gen();
00185   void do_issue_shade_model();
00186   void do_issue_material();
00187   void do_issue_texture();
00188   void do_issue_blending();
00189   void do_issue_stencil();
00190   void do_issue_scissor();
00191 
00192   virtual void reissue_transforms();
00193 
00194   virtual void enable_lighting(bool enable);
00195   virtual void set_ambient_light(const LColor &color);
00196   virtual void enable_light(int light_id, bool enable);
00197 
00198   virtual void enable_clip_plane(int plane_id, bool enable);
00199   virtual void bind_clip_plane(const NodePath &plane, int plane_id);
00200 
00201   virtual void close_gsg();
00202   void free_nondx_resources();
00203   void free_d3d_device();
00204 
00205   void set_draw_buffer(const RenderBuffer &rb);
00206   void set_read_buffer(const RenderBuffer &rb);
00207 
00208   void do_auto_rescale_normal();
00209 
00210   void disable_standard_vertex_arrays();
00211   bool update_standard_vertex_arrays(bool force);
00212   void disable_standard_texture_bindings();
00213   void update_standard_texture_bindings();
00214 
00215 protected:
00216   INLINE static D3DTEXTUREADDRESS get_texture_wrap_mode(Texture::WrapMode wm);
00217   INLINE static D3DFOGMODE get_fog_mode_type(Fog::Mode m);
00218   const D3DCOLORVALUE &get_light_color(Light *light) const;
00219   INLINE static D3DTRANSFORMSTATETYPE get_tex_mat_sym(int stage_index);
00220 
00221   static D3DBLEND get_blend_func(ColorBlendAttrib::Operand operand);
00222   void report_texmgr_stats();
00223 
00224   void set_context(DXScreenData *new_context);
00225   void set_render_target();
00226 
00227   void set_texture_blend_mode(int i, const TextureStage *stage);
00228 
00229   void dx_cleanup();
00230   HRESULT reset_d3d_device(D3DPRESENT_PARAMETERS *p_presentation_params,
00231                            DXScreenData **screen = NULL);
00232 
00233   bool check_cooperative_level();
00234 
00235   void show_frame();
00236 
00237   bool create_swap_chain (DXScreenData *new_context);
00238   bool release_swap_chain (DXScreenData *new_context);
00239   void copy_pres_reset(DXScreenData *new_context);
00240 
00241   static D3DTEXTUREFILTERTYPE get_d3d_min_type(Texture::FilterType filter_type);
00242   static D3DTEXTUREFILTERTYPE get_d3d_mip_type(Texture::FilterType filter_type);
00243   static D3DTEXTUREOP get_texture_operation(TextureStage::CombineMode mode, int scale);
00244   DWORD get_texture_argument(TextureStage::CombineSource source,
00245            TextureStage::CombineOperand operand) const;
00246   static DWORD get_texture_argument_modifier(TextureStage::CombineOperand operand);
00247 
00248   void draw_primitive_up(D3DPRIMITIVETYPE primitive_type,
00249        unsigned int primitive_count,
00250        unsigned int first_vertex,
00251        unsigned int num_vertices,
00252        const unsigned char *buffer, size_t stride);
00253   void draw_indexed_primitive_up(D3DPRIMITIVETYPE primitive_type,
00254          unsigned int min_index, unsigned int max_index,
00255          unsigned int num_primitives,
00256          const unsigned char *index_data,
00257          D3DFORMAT index_type,
00258          const unsigned char *buffer, size_t stride);
00259 
00260   INLINE static unsigned char *get_safe_buffer_start();
00261 
00262 public:
00263   DXScreenData *_screen;
00264   
00265 protected:
00266   LPDIRECT3DDEVICE9 _d3d_device;  // same as _screen->_d3d_device, cached for spd
00267   IDirect3DSwapChain9 *_swap_chain;
00268   D3DPRESENT_PARAMETERS _presentation_reset;  // This is built during reset device
00269 
00270   bool _dx_is_ready;
00271   HRESULT _last_testcooplevel_result;
00272 
00273   bool _vertex_blending_enabled;
00274   bool _supports_render_texture;
00275 
00276   RenderBuffer::Type _cur_read_pixel_buffer;  // source for copy_pixel_buffer operation
00277   bool _auto_rescale_normal;
00278 
00279   PN_stdfloat _material_ambient;
00280   PN_stdfloat _material_diffuse;
00281   PN_stdfloat _material_specular;
00282   PN_stdfloat _material_shininess;
00283   PN_stdfloat _material_emission;
00284 
00285   enum DxgsgFogType {
00286     None,
00287     PerVertexFog=D3DRS_FOGVERTEXMODE,
00288     PerPixelFog=D3DRS_FOGTABLEMODE
00289   };
00290   DxgsgFogType _do_fog_type;
00291 
00292   D3DVIEWPORT9 _current_viewport;
00293   bool _supports_depth_bias;
00294 
00295   DWORD _clip_plane_bits;
00296   CullFaceAttrib::Mode _cull_face_mode;
00297   RenderModeAttrib::Mode _current_fill_mode;  //point/wireframe/solid
00298 
00299   PT(Shader)  _current_shader;
00300   CLP(ShaderContext)  *_current_shader_context;
00301   PT(Shader)  _vertex_array_shader;
00302   CLP(ShaderContext)  *_vertex_array_shader_context;
00303   PT(Shader)  _texture_binding_shader;
00304   CLP(ShaderContext)  *_texture_binding_shader_context;
00305 
00306   const DXIndexBufferContext9 *_active_ibuffer;
00307 
00308   bool _overlay_windows_supported;
00309   bool _tex_stats_retrieval_impossible;
00310   bool _supports_texture_constant_color;
00311   DWORD _constant_color_operand;
00312 
00313   static D3DMATRIX _d3d_ident_mat;
00314 
00315   static unsigned char *_temp_buffer;
00316   static unsigned char *_safe_buffer_start;
00317 
00318   int _gsg_managed_textures;
00319   int _gsg_managed_vertex_buffers;
00320   int _gsg_managed_index_buffers;
00321   UINT _available_texture_memory;
00322 
00323   DWORD _last_fvf;
00324   int _num_bound_streams;
00325 
00326   // Cache the data necessary to bind each particular light each
00327   // frame, so if we bind a given light multiple times, we only have
00328   // to compute its data once.
00329   typedef pmap<NodePath, D3DLIGHT9> DirectionalLights;
00330   DirectionalLights _dlights;
00331 
00332   #define MAXIMUM_TEXTURES 16
00333 
00334 
00335   // from D3DRENDERSTATETYPE + pad
00336   #define MAXIMUM_RENDER_STATES 256
00337 
00338   // from D3DTEXTURESTAGESTATETYPE + pad
00339   #define MAXIMUM_TEXTURE_STAGE_STATES 40
00340   typedef struct {
00341     DWORD state_array [MAXIMUM_TEXTURE_STAGE_STATES];
00342   }
00343   TextureStageStates;
00344 
00345   // from D3DSAMPLERSTATETYPE + pad
00346   #define MAXIMUM_TEXTURE_RENDER_STATES 16
00347   typedef struct {
00348     DWORD state_array [MAXIMUM_TEXTURE_RENDER_STATES];
00349   }
00350   TextureRenderStates;
00351 
00352   // from D3DRENDERSTATETYPE
00353   DWORD _render_state_array [MAXIMUM_RENDER_STATES];
00354   TextureStageStates _texture_stage_states_array [D3D_MAXTEXTURESTAGES];
00355   TextureRenderStates _texture_render_states_array [MAXIMUM_TEXTURES];
00356 
00357   int _num_active_texture_stages;
00358 
00359   int _vertex_shader_version_major;
00360   int _vertex_shader_version_minor;
00361   int _pixel_shader_version_major;
00362   int _pixel_shader_version_minor;
00363 
00364   char *_vertex_shader_profile;
00365   char *_pixel_shader_profile;
00366 
00367   int _vertex_shader_maximum_constants;
00368 
00369   bool _supports_stream_offset;
00370 
00371   list <wdxGraphicsBuffer9 **> _graphics_buffer_list;
00372 
00373   int _supports_gamma_calibration;  
00374 
00375   static LPDIRECT3DDEVICE9 _cg_device;
00376 
00377 public:
00378   virtual TypeHandle get_type() const {
00379     return get_class_type();
00380   }
00381   virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
00382 
00383   static TypeHandle get_class_type() {
00384     return _type_handle;
00385   }
00386 
00387 public:
00388   static void init_type() {
00389     GraphicsStateGuardian::init_type();
00390     register_type(_type_handle, "DXGraphicsStateGuardian9",
00391                   GraphicsStateGuardian::get_class_type());
00392   }
00393 
00394 private:
00395   static TypeHandle _type_handle;
00396 
00397   friend class wdxGraphicsWindow9;
00398   friend class wdxGraphicsPipe9;
00399   friend class wdxGraphicsWindowGroup9;
00400   friend class DXTextureContext9;
00401   friend class wdxGraphicsBuffer9;
00402   friend class DXVertexBufferContext9;
00403   friend class DXIndexBufferContext9;
00404   friend class DXShaderContext9;
00405 };
00406 
00407 #include "dxGraphicsStateGuardian9.I"
00408 
00409 #endif
 All Classes Functions Variables Enumerations