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);
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                                       Lens::StereoChannel stereo_channel);
00076   virtual CPT(TransformState) calc_projection_mat(const Lens *lens);
00077   virtual bool prepare_lens();
00078 
00079   virtual bool begin_frame(Thread *current_thread);
00080   virtual bool begin_scene();
00081   virtual void end_scene();
00082   virtual void end_frame(Thread *current_thread);
00083 
00084   virtual bool begin_draw_primitives(const GeomPipelineReader *geom_reader,
00085                                      const GeomMunger *munger,
00086                                      const GeomVertexDataPipelineReader *data_reader,
00087                                      bool force);
00088   virtual bool draw_triangles(const GeomPrimitivePipelineReader *reader,
00089                               bool force);
00090   virtual bool draw_tristrips(const GeomPrimitivePipelineReader *reader,
00091                               bool force);
00092   virtual bool draw_trifans(const GeomPrimitivePipelineReader *reader,
00093                             bool force);
00094   virtual bool draw_lines(const GeomPrimitivePipelineReader *reader,
00095                           bool force);
00096   virtual bool draw_linestrips(const GeomPrimitivePipelineReader *reader,
00097                                bool force);
00098   virtual bool draw_points(const GeomPrimitivePipelineReader *reader,
00099                            bool force);
00100   virtual void end_draw_primitives();
00101 
00102   virtual bool framebuffer_copy_to_texture(Texture *tex, int z, const DisplayRegion *dr,
00103                                            const RenderBuffer &rb);
00104   virtual bool framebuffer_copy_to_ram(Texture *tex, int z, const DisplayRegion *dr,
00105                                        const RenderBuffer &rb);
00106   bool do_framebuffer_copy_to_ram(Texture *tex, int z, const DisplayRegion *dr,
00107                                   const RenderBuffer &rb, bool inverted);
00108 
00109   virtual void reset();
00110 
00111   virtual void apply_fog(Fog *fog);
00112 
00113   virtual void bind_light(PointLight *light_obj, const NodePath &light,
00114                           int light_id);
00115   virtual void bind_light(DirectionalLight *light_obj, const NodePath &light,
00116                           int light_id);
00117   virtual void bind_light(Spotlight *light_obj, const NodePath &light,
00118                           int light_id);
00119 
00120   static D3DFORMAT get_index_type(Geom::NumericType numeric_type);
00121   INLINE static DWORD Colorf_to_D3DCOLOR(const Colorf &cColorf);
00122 
00123   virtual void set_state_and_transform(const RenderState *state,
00124                                        const TransformState *transform);
00125   LPDIRECT3DDEVICE8 get_d3d_device();
00126 
00127   static bool get_gamma_table(void);
00128   static bool static_set_gamma(bool restore, float gamma);
00129   bool set_gamma(float 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(float left, float right, float bottom, float top);
00153 
00154   virtual void reissue_transforms();
00155 
00156   virtual void enable_lighting(bool enable);
00157   virtual void set_ambient_light(const Colorf &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 
00230   RenderBuffer::Type _cur_read_pixel_buffer;  // source for copy_pixel_buffer operation
00231   bool _auto_rescale_normal;
00232 
00233   float _material_ambient;
00234   float _material_diffuse;
00235   float _material_specular;
00236   float _material_shininess;
00237   float _material_emission;
00238 
00239   enum DxgsgFogType {
00240     None,
00241     PerVertexFog=D3DRS_FOGVERTEXMODE,
00242     PerPixelFog=D3DRS_FOGTABLEMODE
00243   };
00244   DxgsgFogType _do_fog_type;
00245 
00246   D3DVIEWPORT8 _current_viewport;
00247 
00248   DWORD _clip_plane_bits;
00249   CullFaceAttrib::Mode _cull_face_mode;
00250   RenderModeAttrib::Mode _current_fill_mode;  //point/wireframe/solid
00251 
00252   const DXVertexBufferContext8 *_active_vbuffer;
00253   const DXIndexBufferContext8 *_active_ibuffer;
00254 
00255   int _num_active_texture_stages;
00256   
00257   // Cache the data necessary to bind each particular light each
00258   // frame, so if we bind a given light multiple times, we only have
00259   // to compute its data once.
00260   typedef pmap<NodePath, D3DLIGHT8> DirectionalLights;
00261   DirectionalLights _dlights;
00262 
00263   bool _overlay_windows_supported;
00264   bool _tex_stats_retrieval_impossible;
00265 
00266   static D3DMATRIX _d3d_ident_mat;
00267   CPT(TransformState) _scissor_mat;
00268 
00269   static unsigned char *_temp_buffer;
00270   static unsigned char *_safe_buffer_start;
00271 
00272   list <wdxGraphicsBuffer8 **> _graphics_buffer_list;
00273 
00274 public:
00275   virtual TypeHandle get_type() const {
00276     return get_class_type();
00277   }
00278   virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
00279 
00280   static TypeHandle get_class_type() {
00281     return _type_handle;
00282   }
00283 
00284 public:
00285   static void init_type() {
00286     GraphicsStateGuardian::init_type();
00287     register_type(_type_handle, "DXGraphicsStateGuardian8",
00288                   GraphicsStateGuardian::get_class_type());
00289   }
00290   
00291 private:
00292   static TypeHandle _type_handle;
00293 
00294   friend class wdxGraphicsWindow8;
00295   friend class wdxGraphicsBuffer8;
00296   friend class wdxGraphicsPipe8;
00297   friend class wdxGraphicsWindowGroup8;
00298   friend class DXTextureContext8;
00299 };
00300 
00301 #include "dxGraphicsStateGuardian8.I"
00302 
00303 #endif
00304 
 All Classes Functions Variables Enumerations