Panda3D

dxGraphicsStateGuardian8.h

00001 // Filename: dxGraphicsStateGuardian8.h
00002 // Created by:  mike (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 DXGRAPHICSSTATEGUARDIAN8_H
00016 #define DXGRAPHICSSTATEGUARDIAN8_H
00017 
00018 #include "dxgsg8base.h"
00019 #include "dxTextureContext8.h"
00020 #include "config_dxgsg8.h"
00021 
00022 #include "graphicsStateGuardian.h"
00023 #include "texture.h"
00024 #include "displayRegion.h"
00025 #include "material.h"
00026 #include "depthTestAttrib.h"
00027 #include "cullFaceAttrib.h"
00028 #include "renderModeAttrib.h"
00029 #include "colorBlendAttrib.h"
00030 #include "fog.h"
00031 #include "pointerToArray.h"
00032 
00033 class Light;
00034 
00035 class DXTextureContext8;
00036 class DXVertexBufferContext8;
00037 class DXIndexBufferContext8;
00038 
00039 ////////////////////////////////////////////////////////////////////
00040 //       Class : DXGraphicsStateGuardian8
00041 // Description : A GraphicsStateGuardian for rendering into DirectX8
00042 //               contexts.
00043 ////////////////////////////////////////////////////////////////////
00044 class EXPCL_PANDADX DXGraphicsStateGuardian8 : public GraphicsStateGuardian {
00045 public:
00046   DXGraphicsStateGuardian8(GraphicsEngine *engine, GraphicsPipe *pipe);
00047   ~DXGraphicsStateGuardian8();
00048 
00049   FrameBufferProperties
00050     calc_fb_properties(DWORD cformat, DWORD dformat, DWORD multisampletype);
00051 
00052   virtual TextureContext *prepare_texture(Texture *tex, int view);
00053   void apply_texture(int i, TextureContext *tc);
00054   virtual bool update_texture(TextureContext *tc, bool force);
00055   bool upload_texture(DXTextureContext8 *dtc, bool force);
00056   virtual void release_texture(TextureContext *tc);
00057   virtual bool extract_texture_data(Texture *tex);
00058 
00059   virtual VertexBufferContext *prepare_vertex_buffer(GeomVertexArrayData *data);
00060   bool apply_vertex_buffer(VertexBufferContext *vbc,
00061                            const GeomVertexArrayDataHandle *reader, bool force);
00062   virtual void release_vertex_buffer(VertexBufferContext *vbc);
00063 
00064   virtual IndexBufferContext *prepare_index_buffer(GeomPrimitive *data);
00065   bool apply_index_buffer(IndexBufferContext *ibc,
00066                           const GeomPrimitivePipelineReader *reader, bool force);
00067   virtual void release_index_buffer(IndexBufferContext *ibc);
00068 
00069   virtual PT(GeomMunger) make_geom_munger(const RenderState *state,
00070                                           Thread *current_thread);
00071 
00072   virtual void clear(DrawableRegion *region);
00073 
00074   virtual void prepare_display_region(DisplayRegionPipelineReader *dr);
00075   virtual CPT(TransformState) calc_projection_mat(const Lens *lens);
00076   virtual bool prepare_lens();
00077 
00078   virtual bool begin_frame(Thread *current_thread);
00079   virtual bool begin_scene();
00080   virtual void end_scene();
00081   virtual void end_frame(Thread *current_thread);
00082 
00083   virtual bool begin_draw_primitives(const GeomPipelineReader *geom_reader,
00084                                      const GeomMunger *munger,
00085                                      const GeomVertexDataPipelineReader *data_reader,
00086                                      bool force);
00087   virtual bool draw_triangles(const GeomPrimitivePipelineReader *reader,
00088                               bool force);
00089   virtual bool draw_tristrips(const GeomPrimitivePipelineReader *reader,
00090                               bool force);
00091   virtual bool draw_trifans(const GeomPrimitivePipelineReader *reader,
00092                             bool force);
00093   virtual bool draw_lines(const GeomPrimitivePipelineReader *reader,
00094                           bool force);
00095   virtual bool draw_linestrips(const GeomPrimitivePipelineReader *reader,
00096                                bool force);
00097   virtual bool draw_points(const GeomPrimitivePipelineReader *reader,
00098                            bool force);
00099   virtual void end_draw_primitives();
00100 
00101   virtual bool framebuffer_copy_to_texture(Texture *tex, int z, const DisplayRegion *dr,
00102                                            const RenderBuffer &rb);
00103   virtual bool framebuffer_copy_to_ram(Texture *tex, int z, const DisplayRegion *dr,
00104                                        const RenderBuffer &rb);
00105   bool do_framebuffer_copy_to_ram(Texture *tex, int z, const DisplayRegion *dr,
00106                                   const RenderBuffer &rb, bool inverted);
00107 
00108   virtual void reset();
00109 
00110   virtual void apply_fog(Fog *fog);
00111 
00112   virtual void bind_light(PointLight *light_obj, const NodePath &light,
00113                           int light_id);
00114   virtual void bind_light(DirectionalLight *light_obj, const NodePath &light,
00115                           int light_id);
00116   virtual void bind_light(Spotlight *light_obj, const NodePath &light,
00117                           int light_id);
00118 
00119   static D3DFORMAT get_index_type(Geom::NumericType numeric_type);
00120   INLINE static DWORD LColor_to_D3DCOLOR(const LColor &cLColor);
00121 
00122   virtual void set_state_and_transform(const RenderState *state,
00123                                        const TransformState *transform);
00124   LPDIRECT3DDEVICE8 get_d3d_device();
00125   INLINE bool get_supports_render_texture() const;
00126 
00127   static bool get_gamma_table(void);
00128   static bool static_set_gamma(bool restore, PN_stdfloat gamma);
00129   bool set_gamma(PN_stdfloat gamma);
00130   void restore_gamma();
00131   static void atexit_function(void);
00132 
00133 protected:
00134   void do_issue_transform();
00135   void do_issue_alpha_test();
00136   void do_issue_render_mode();
00137   void do_issue_rescale_normal();
00138   void do_issue_color_write();
00139   void do_issue_depth_test();
00140   void do_issue_depth_write();
00141   void do_issue_cull_face();
00142   void do_issue_fog();
00143   void do_issue_depth_offset();
00144   void do_issue_tex_gen();
00145   void do_issue_shade_model();
00146   void do_issue_material();
00147   void do_issue_texture();
00148   void do_issue_blending();
00149   void do_issue_stencil();
00150   void do_issue_scissor();
00151 
00152   void set_scissor(PN_stdfloat left, PN_stdfloat right, PN_stdfloat bottom, PN_stdfloat top);
00153 
00154   virtual void reissue_transforms();
00155 
00156   virtual void enable_lighting(bool enable);
00157   virtual void set_ambient_light(const LColor &color);
00158   virtual void enable_light(int light_id, bool enable);
00159 
00160   virtual void enable_clip_plane(int plane_id, bool enable);
00161   virtual void bind_clip_plane(const NodePath &plane, int plane_id);
00162 
00163   virtual void close_gsg();
00164   void free_nondx_resources();
00165   void free_d3d_device();
00166 
00167   void set_draw_buffer(const RenderBuffer &rb);
00168   void set_read_buffer(const RenderBuffer &rb);
00169 
00170   void do_auto_rescale_normal();
00171 
00172 protected:
00173   INLINE static D3DTEXTUREADDRESS get_texture_wrap_mode(Texture::WrapMode wm);
00174   INLINE static D3DFOGMODE get_fog_mode_type(Fog::Mode m);
00175   const D3DCOLORVALUE &get_light_color(Light *light) const;
00176   INLINE static D3DTRANSFORMSTATETYPE get_tex_mat_sym(int stage_index);
00177 
00178   static D3DBLEND get_blend_func(ColorBlendAttrib::Operand operand);
00179   void report_texmgr_stats();
00180 
00181   void set_context(DXScreenData *new_context);
00182   void set_render_target();
00183 
00184   void set_texture_blend_mode(int i, const TextureStage *stage);
00185 
00186   void dx_cleanup();
00187   HRESULT reset_d3d_device(D3DPRESENT_PARAMETERS *p_presentation_params,
00188                            DXScreenData **screen = NULL);
00189 
00190   bool check_cooperative_level();
00191 
00192   void show_frame();
00193 
00194   bool create_swap_chain (DXScreenData *new_context);
00195   bool release_swap_chain (DXScreenData *new_context);
00196   void copy_pres_reset(DXScreenData *new_context);
00197 
00198   static D3DTEXTUREFILTERTYPE get_d3d_min_type(Texture::FilterType filter_type);
00199   static D3DTEXTUREFILTERTYPE get_d3d_mip_type(Texture::FilterType filter_type);
00200   static D3DTEXTUREOP get_texture_operation(TextureStage::CombineMode mode, int scale);
00201   static DWORD get_texture_argument(TextureStage::CombineSource source,
00202                                     TextureStage::CombineOperand operand);
00203   static DWORD get_texture_argument_modifier(TextureStage::CombineOperand operand);
00204 
00205   void draw_primitive_up(D3DPRIMITIVETYPE primitive_type,
00206        unsigned int primitive_count,
00207        unsigned int first_vertex,
00208        unsigned int num_vertices,
00209        const unsigned char *buffer, size_t stride);
00210   void draw_indexed_primitive_up(D3DPRIMITIVETYPE primitive_type,
00211          unsigned int min_index, unsigned int max_index,
00212          unsigned int num_primitives,
00213          const unsigned char *index_data,
00214          D3DFORMAT index_type,
00215          const unsigned char *buffer, size_t stride);
00216 
00217   INLINE static unsigned char *get_safe_buffer_start();
00218 
00219 protected:
00220   DXScreenData *_screen;
00221   LPDIRECT3DDEVICE8 _d3d_device;  // same as _screen->_d3d_device, cached for spd
00222   IDirect3DSwapChain8 *_swap_chain;
00223   D3DPRESENT_PARAMETERS _presentation_reset;  // This is built during reset device
00224 
00225   bool _dx_is_ready;
00226   HRESULT _last_testcooplevel_result;
00227 
00228   bool _vertex_blending_enabled;
00229   bool _supports_render_texture;
00230 
00231   RenderBuffer::Type _cur_read_pixel_buffer;  // source for copy_pixel_buffer operation
00232   bool _auto_rescale_normal;
00233 
00234   PN_stdfloat _material_ambient;
00235   PN_stdfloat _material_diffuse;
00236   PN_stdfloat _material_specular;
00237   PN_stdfloat _material_shininess;
00238   PN_stdfloat _material_emission;
00239 
00240   enum DxgsgFogType {
00241     None,
00242     PerVertexFog=D3DRS_FOGVERTEXMODE,
00243     PerPixelFog=D3DRS_FOGTABLEMODE
00244   };
00245   DxgsgFogType _do_fog_type;
00246 
00247   D3DVIEWPORT8 _current_viewport;
00248 
00249   DWORD _clip_plane_bits;
00250   CullFaceAttrib::Mode _cull_face_mode;
00251   RenderModeAttrib::Mode _current_fill_mode;  //point/wireframe/solid
00252 
00253   const DXVertexBufferContext8 *_active_vbuffer;
00254   const DXIndexBufferContext8 *_active_ibuffer;
00255 
00256   int _num_active_texture_stages;
00257   
00258   // Cache the data necessary to bind each particular light each
00259   // frame, so if we bind a given light multiple times, we only have
00260   // to compute its data once.
00261   typedef pmap<NodePath, D3DLIGHT8> DirectionalLights;
00262   DirectionalLights _dlights;
00263 
00264   bool _overlay_windows_supported;
00265   bool _tex_stats_retrieval_impossible;
00266 
00267   static D3DMATRIX _d3d_ident_mat;
00268   CPT(TransformState) _scissor_mat;
00269 
00270   static unsigned char *_temp_buffer;
00271   static unsigned char *_safe_buffer_start;
00272 
00273   list <wdxGraphicsBuffer8 **> _graphics_buffer_list;
00274 
00275 public:
00276   virtual TypeHandle get_type() const {
00277     return get_class_type();
00278   }
00279   virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
00280 
00281   static TypeHandle get_class_type() {
00282     return _type_handle;
00283   }
00284 
00285 public:
00286   static void init_type() {
00287     GraphicsStateGuardian::init_type();
00288     register_type(_type_handle, "DXGraphicsStateGuardian8",
00289                   GraphicsStateGuardian::get_class_type());
00290   }
00291   
00292 private:
00293   static TypeHandle _type_handle;
00294 
00295   friend class wdxGraphicsWindow8;
00296   friend class wdxGraphicsBuffer8;
00297   friend class wdxGraphicsPipe8;
00298   friend class wdxGraphicsWindowGroup8;
00299   friend class DXTextureContext8;
00300 };
00301 
00302 #include "dxGraphicsStateGuardian8.I"
00303 
00304 #endif
00305 
 All Classes Functions Variables Enumerations