Panda3D

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 typedef LPDIRECT3DDEVICE9 DIRECT_3D_DEVICE;
00037 typedef D3DVERTEXELEMENT9 DIRECT_3D_VERTEX_ELEMENT;
00038 typedef LPDIRECT3DVERTEXDECLARATION9 DIRECT_3D_VERTEX_DECLARATION;
00039 typedef LPDIRECT3DVERTEXSHADER9 DIRECT_3D_VERTEX_SHADER;
00040 typedef LPDIRECT3DPIXELSHADER9 DIRECT_3D_PIXEL_SHADER;
00041 
00042 #include "vertexElementArray.h"
00043 #include "dxShaderContext9.h"
00044 
00045 
00046 enum GsgPageType
00047 {
00048   GPT_Texture,
00049   GPT_VertexBuffer,
00050   GPT_IndexBuffer,
00051 
00052   GPT_TotalPageTypes
00053 };
00054 
00055 class Light;
00056 
00057 class DXTextureContext9;
00058 class DXVertexBufferContext9;
00059 class DXIndexBufferContext9;
00060 
00061 ////////////////////////////////////////////////////////////////////
00062 //       Class : DXGraphicsStateGuardian9
00063 // Description : A GraphicsStateGuardian for rendering into DirectX9
00064 //               contexts.
00065 ////////////////////////////////////////////////////////////////////
00066 class EXPCL_PANDADX DXGraphicsStateGuardian9 : public GraphicsStateGuardian {
00067 public:
00068   DXGraphicsStateGuardian9(GraphicsEngine *engine, GraphicsPipe *pipe);
00069   ~DXGraphicsStateGuardian9();
00070 
00071   FrameBufferProperties
00072     calc_fb_properties(DWORD cformat, DWORD dformat,
00073                        DWORD multisampletype, DWORD multisamplequality);
00074 
00075   virtual TextureContext *prepare_texture(Texture *tex);
00076   void apply_texture(int i, TextureContext *tc);
00077   virtual bool update_texture(TextureContext *tc, bool force);
00078   bool upload_texture(DXTextureContext9 *dtc, bool force);
00079   virtual void release_texture(TextureContext *tc);
00080   virtual bool extract_texture_data(Texture *tex);
00081 
00082   ShaderContext *prepare_shader(Shader *se);
00083   void release_shader(ShaderContext *sc);
00084 
00085   virtual VertexBufferContext *prepare_vertex_buffer(GeomVertexArrayData *data);
00086   bool apply_vertex_buffer(VertexBufferContext *vbc, CLP(ShaderContext) *shader_context,
00087                            const GeomVertexArrayDataHandle *reader, bool force, string name);
00088   virtual void release_vertex_buffer(VertexBufferContext *vbc);
00089 
00090   virtual IndexBufferContext *prepare_index_buffer(GeomPrimitive *data);
00091   bool apply_index_buffer(IndexBufferContext *ibc,
00092                           const GeomPrimitivePipelineReader *reader, bool force);
00093   virtual void release_index_buffer(IndexBufferContext *ibc);
00094 
00095   virtual void begin_occlusion_query();
00096   virtual PT(OcclusionQueryContext) end_occlusion_query();
00097 
00098   virtual PT(GeomMunger) make_geom_munger(const RenderState *state,
00099                                           Thread *current_thread);
00100 
00101   virtual void clear(DrawableRegion *clearable);
00102 
00103   virtual void prepare_display_region(DisplayRegionPipelineReader *dr,
00104                                       Lens::StereoChannel stereo_channel);
00105   virtual CPT(TransformState) calc_projection_mat(const Lens *lens);
00106   virtual bool prepare_lens();
00107 
00108   virtual bool begin_frame(Thread *current_thread);
00109   virtual bool begin_scene();
00110   virtual void end_scene();
00111   virtual void end_frame(Thread *current_thread);
00112 
00113   virtual bool begin_draw_primitives(const GeomPipelineReader *geom_reader,
00114                                      const GeomMunger *munger,
00115                                      const GeomVertexDataPipelineReader *data_reader,
00116                                      bool force);
00117   virtual bool draw_triangles(const GeomPrimitivePipelineReader *reader,
00118                               bool force);
00119   virtual bool draw_tristrips(const GeomPrimitivePipelineReader *reader,
00120                               bool force);
00121   virtual bool draw_trifans(const GeomPrimitivePipelineReader *reader,
00122                             bool force);
00123   virtual bool draw_lines(const GeomPrimitivePipelineReader *reader,
00124                           bool force);
00125   virtual bool draw_linestrips(const GeomPrimitivePipelineReader *reader,
00126                                bool force);
00127   virtual bool draw_points(const GeomPrimitivePipelineReader *reader,
00128                            bool force);
00129   virtual void end_draw_primitives();
00130 
00131   virtual bool framebuffer_copy_to_texture(Texture *tex, int z, const DisplayRegion *dr,
00132                                            const RenderBuffer &rb);
00133   virtual bool framebuffer_copy_to_ram(Texture *tex, int z, const DisplayRegion *dr,
00134                                        const RenderBuffer &rb);
00135   bool do_framebuffer_copy_to_ram(Texture *tex, int z, const DisplayRegion *dr,
00136                                   const RenderBuffer &rb, bool inverted);
00137 
00138   void reset_render_states (void);
00139   virtual void reset();
00140 
00141   virtual void apply_fog(Fog *fog);
00142 
00143   virtual void bind_light(PointLight *light_obj, const NodePath &light,
00144                           int light_id);
00145   virtual void bind_light(DirectionalLight *light_obj, const NodePath &light,
00146                           int light_id);
00147   virtual void bind_light(Spotlight *light_obj, const NodePath &light,
00148                           int light_id);
00149 
00150   static D3DFORMAT get_index_type(Geom::NumericType numeric_type);
00151   INLINE static DWORD Colorf_to_D3DCOLOR(const Colorf &cColorf);
00152 
00153   virtual void set_state_and_transform(const RenderState *state,
00154                                        const TransformState *transform);
00155 
00156   bool check_dx_allocation (HRESULT result, int allocation_size, int attempts);
00157 
00158   INLINE HRESULT set_render_state (D3DRENDERSTATETYPE state, DWORD value);
00159   INLINE HRESULT set_texture_stage_state (DWORD stage, D3DTEXTURESTAGESTATETYPE type, DWORD value);
00160   INLINE HRESULT set_sampler_state (DWORD sampler, D3DSAMPLERSTATETYPE type, DWORD value);
00161 
00162   static bool get_gamma_table(void);
00163   static bool static_set_gamma(bool restore, float gamma);
00164   bool set_gamma(float 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 Colorf &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 //  void update_standard_vertex_arrays();
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 
00275   RenderBuffer::Type _cur_read_pixel_buffer;  // source for copy_pixel_buffer operation
00276   bool _auto_rescale_normal;
00277 
00278   float _material_ambient;
00279   float _material_diffuse;
00280   float _material_specular;
00281   float _material_shininess;
00282   float _material_emission;
00283 
00284   enum DxgsgFogType {
00285     None,
00286     PerVertexFog=D3DRS_FOGVERTEXMODE,
00287     PerPixelFog=D3DRS_FOGTABLEMODE
00288   };
00289   DxgsgFogType _do_fog_type;
00290 
00291   D3DVIEWPORT9 _current_viewport;
00292   bool _supports_depth_bias;
00293 
00294   DWORD _clip_plane_bits;
00295   CullFaceAttrib::Mode _cull_face_mode;
00296   RenderModeAttrib::Mode _current_fill_mode;  //point/wireframe/solid
00297 
00298   PT(Shader)  _current_shader;
00299   CLP(ShaderContext)  *_current_shader_context;
00300   PT(Shader)  _vertex_array_shader;
00301   CLP(ShaderContext)  *_vertex_array_shader_context;
00302   PT(Shader)  _texture_binding_shader;
00303   CLP(ShaderContext)  *_texture_binding_shader_context;
00304 
00305   const DXVertexBufferContext9 *_active_vbuffer;
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 
00325   // Cache the data necessary to bind each particular light each
00326   // frame, so if we bind a given light multiple times, we only have
00327   // to compute its data once.
00328   typedef pmap<NodePath, D3DLIGHT9> DirectionalLights;
00329   DirectionalLights _dlights;
00330 
00331   #define MAXIMUM_TEXTURES 16
00332 
00333 
00334   // from D3DRENDERSTATETYPE + pad
00335   #define MAXIMUM_RENDER_STATES 256
00336 
00337   // from D3DTEXTURESTAGESTATETYPE + pad
00338   #define MAXIMUM_TEXTURE_STAGE_STATES 40
00339   typedef struct {
00340     DWORD state_array [MAXIMUM_TEXTURE_STAGE_STATES];
00341   }
00342   TextureStageStates;
00343 
00344   // from D3DSAMPLERSTATETYPE + pad
00345   #define MAXIMUM_TEXTURE_RENDER_STATES 16
00346   typedef struct {
00347     DWORD state_array [MAXIMUM_TEXTURE_RENDER_STATES];
00348   }
00349   TextureRenderStates;
00350 
00351   // from D3DRENDERSTATETYPE
00352   DWORD _render_state_array [MAXIMUM_RENDER_STATES];
00353   TextureStageStates _texture_stage_states_array [D3D_MAXTEXTURESTAGES];
00354   TextureRenderStates _texture_render_states_array [MAXIMUM_TEXTURES];
00355 
00356   int _num_active_texture_stages;
00357 
00358   int _vertex_shader_version_major;
00359   int _vertex_shader_version_minor;
00360   int _pixel_shader_version_major;
00361   int _pixel_shader_version_minor;
00362 
00363   char *_vertex_shader_profile;
00364   char *_pixel_shader_profile;
00365 
00366   int _vertex_shader_maximum_constants;
00367 
00368   bool _supports_stream_offset;
00369 
00370   list <wdxGraphicsBuffer9 **> _graphics_buffer_list;
00371 
00372   int _supports_gamma_calibration;  
00373 
00374   static LPDIRECT3DDEVICE9 _cg_device;
00375 
00376 public:
00377   virtual TypeHandle get_type() const {
00378     return get_class_type();
00379   }
00380   virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
00381 
00382   static TypeHandle get_class_type() {
00383     return _type_handle;
00384   }
00385 
00386 public:
00387   static void init_type() {
00388     GraphicsStateGuardian::init_type();
00389     register_type(_type_handle, "DXGraphicsStateGuardian9",
00390                   GraphicsStateGuardian::get_class_type());
00391   }
00392 
00393 private:
00394   static TypeHandle _type_handle;
00395 
00396   friend class wdxGraphicsWindow9;
00397   friend class wdxGraphicsPipe9;
00398   friend class wdxGraphicsWindowGroup9;
00399   friend class DXTextureContext9;
00400   friend class wdxGraphicsBuffer9;
00401   friend class DXVertexBufferContext9;
00402   friend class DXIndexBufferContext9;
00403   friend class DXShaderContext9;
00404 };
00405 
00406 #include "dxGraphicsStateGuardian9.I"
00407 
00408 #endif
 All Classes Functions Variables Enumerations