00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef DXGRAPHICSSTATEGUARDIAN_H
00020 #define DXGRAPHICSSTATEGUARDIAN_H
00021
00022
00023
00024 #include "dxgsg8base.h"
00025 #include "dxGeomNodeContext8.h"
00026 #include "dxTextureContext8.h"
00027 #include "d3dfont8.h"
00028
00029 #include "graphicsStateGuardian.h"
00030 #include "geomprimitives.h"
00031 #include "texture.h"
00032 #include "pixelBuffer.h"
00033 #include "displayRegion.h"
00034 #include "material.h"
00035 #include "depthTestAttrib.h"
00036 #include "renderModeAttrib.h"
00037 #include "textureApplyAttrib.h"
00038 #include "fog.h"
00039 #include "pointerToArray.h"
00040
00041
00042
00043 class Light;
00044
00045
00046
00047
00048 #if 0
00049
00050
00051
00052
00053
00054 extern void dbgPrintVidMem(LPDIRECTDRAW7 pDD, LPDDSCAPS2 lpddsCaps,const char *pMsg);
00055 #define PRINTVIDMEM(pDD,pCaps,pMsg) dbgPrintVidMem(pDD,pCaps,pMsg)
00056 #else
00057 #define PRINTVIDMEM(pDD,pCaps,pMsg)
00058 #endif
00059
00060
00061
00062
00063
00064
00065
00066 class EXPCL_PANDADX DXGraphicsStateGuardian8 : public GraphicsStateGuardian {
00067 friend class wdxGraphicsWindow8;
00068 friend class wdxGraphicsPipe8;
00069 friend class wdxGraphicsWindowGroup8;
00070 friend class DXTextureContext8;
00071
00072 public:
00073 DXGraphicsStateGuardian8(const FrameBufferProperties &properties);
00074 ~DXGraphicsStateGuardian8();
00075
00076 virtual void reset();
00077
00078 virtual void do_clear(const RenderBuffer &buffer);
00079
00080 virtual void prepare_display_region();
00081 virtual bool prepare_lens();
00082
00083 virtual void draw_point(GeomPoint *geom, GeomContext *gc);
00084 virtual void draw_line(GeomLine *geom, GeomContext *gc);
00085 virtual void draw_linestrip(GeomLinestrip *geom, GeomContext *gc);
00086 void draw_linestrip_base(Geom *geom, GeomContext *gc, bool bConnectEnds);
00087 virtual void draw_sprite(GeomSprite *geom, GeomContext *gc);
00088 virtual void draw_polygon(GeomPolygon *geom, GeomContext *gc);
00089 virtual void draw_quad(GeomQuad *geom, GeomContext *gc);
00090 virtual void draw_tri(GeomTri *geom, GeomContext *gc);
00091 virtual void draw_tristrip(GeomTristrip *geom, GeomContext *gc);
00092 virtual void draw_trifan(GeomTrifan *geom, GeomContext *gc);
00093 virtual void draw_sphere(GeomSphere *geom, GeomContext *gc);
00094
00095 virtual GeomNodeContext *prepare_geom_node(GeomNode *node);
00096 virtual void draw_geom_node(GeomNode *node, const RenderState *state,
00097 GeomNodeContext *gnc);
00098 virtual void release_geom_node(GeomNodeContext *gnc);
00099
00100 virtual TextureContext *prepare_texture(Texture *tex);
00101 virtual void apply_texture(TextureContext *tc);
00102 virtual void release_texture(TextureContext *tc);
00103
00104 virtual void copy_texture(TextureContext *tc, const DisplayRegion *dr);
00105 virtual void copy_texture(TextureContext *tc, const DisplayRegion *dr,
00106 const RenderBuffer &rb);
00107
00108 virtual void texture_to_pixel_buffer(TextureContext *tc, PixelBuffer *pb);
00109 virtual void texture_to_pixel_buffer(TextureContext *tc, PixelBuffer *pb,
00110 const DisplayRegion *dr);
00111
00112 virtual void copy_pixel_buffer(PixelBuffer *pb, const DisplayRegion *dr);
00113 virtual void copy_pixel_buffer(PixelBuffer *pb, const DisplayRegion *dr,
00114 const RenderBuffer &rb);
00115
00116 virtual void apply_material(const Material *material);
00117 virtual void apply_fog(Fog *fog);
00118
00119 virtual void issue_transform(const TransformState *transform);
00120 virtual void issue_tex_matrix(const TexMatrixAttrib *attrib);
00121 virtual void issue_texture(const TextureAttrib *attrib);
00122 virtual void issue_material(const MaterialAttrib *attrib);
00123 virtual void issue_render_mode(const RenderModeAttrib *attrib);
00124 virtual void issue_texture_apply(const TextureApplyAttrib *attrib);
00125 virtual void issue_alpha_test(const AlphaTestAttrib *attrib);
00126 virtual void issue_depth_test(const DepthTestAttrib *attrib);
00127 virtual void issue_depth_write(const DepthWriteAttrib *attrib);
00128 virtual void issue_color_write(const ColorWriteAttrib *attrib);
00129 virtual void issue_cull_face(const CullFaceAttrib *attrib);
00130 virtual void issue_fog(const FogAttrib *attrib);
00131 virtual void issue_depth_offset(const DepthOffsetAttrib *attrib);
00132
00133 virtual void bind_light(PointLight *light, int light_id);
00134 virtual void bind_light(DirectionalLight *light, int light_id);
00135 virtual void bind_light(Spotlight *light, int light_id);
00136
00137 virtual bool begin_frame();
00138 virtual bool begin_scene();
00139 virtual void end_scene();
00140 virtual void end_frame();
00141
00142 virtual bool wants_normals(void) const;
00143 virtual bool wants_texcoords(void) const;
00144
00145 virtual bool depth_offset_decals();
00146
00147 virtual CoordinateSystem get_internal_coordinate_system() const;
00148 INLINE float compute_distance_to(const LPoint3f &point) const;
00149 virtual void set_color_clear_value(const Colorf& value);
00150
00151 public:
00152
00153 DXScreenData *_pScrn;
00154 LPDIRECT3DDEVICE8 _pD3DDevice;
00155
00156 protected:
00157 virtual void enable_lighting(bool enable);
00158 virtual void set_ambient_light(const Colorf &color);
00159 virtual void enable_light(int light_id, bool enable);
00160
00161 virtual bool slot_new_clip_plane(int plane_id);
00162 virtual void enable_clip_plane(int plane_id, bool enable);
00163 virtual void bind_clip_plane(PlaneNode *plane, int plane_id);
00164
00165 virtual void set_blend_mode(ColorWriteAttrib::Mode color_write_mode,
00166 ColorBlendAttrib::Mode color_blend_mode,
00167 TransparencyAttrib::Mode transparency_mode);
00168
00169 void free_nondx_resources();
00170 void free_d3d_device(void);
00171 virtual PT(SavedFrameBuffer) save_frame_buffer(const RenderBuffer &buffer,
00172 CPT(DisplayRegion) dr);
00173 virtual void restore_frame_buffer(SavedFrameBuffer *frame_buffer);
00174
00175 void set_draw_buffer(const RenderBuffer &rb);
00176 void set_read_buffer(const RenderBuffer &rb);
00177
00178 INLINE void add_to_FVFBuf(void *data, size_t bytes) ;
00179
00180 bool _bDXisReady;
00181 HRESULT _last_testcooplevel_result;
00182 DXTextureContext8 *_pCurTexContext;
00183
00184 bool _bTransformIssued;
00185 D3DMATRIX _SavedTransform;
00186
00187 RenderBuffer::Type _cur_read_pixel_buffer;
00188
00189 void GenerateSphere(void *pVertexSpace,DWORD dwVertSpaceByteSize,
00190 void *pIndexSpace,DWORD dwIndexSpaceByteSize,
00191 D3DXVECTOR3 *pCenter, float fRadius,
00192 DWORD wNumRings, DWORD wNumSections, float sx, float sy, float sz,
00193 DWORD *pNumVertices,DWORD *pNumTris,DWORD fvfFlags,DWORD dwVertSize);
00194 HRESULT ReleaseAllDeviceObjects(void);
00195 HRESULT RecreateAllDeviceObjects(void);
00196 HRESULT DeleteAllDeviceObjects(void);
00197
00198
00199
00200
00201
00202
00203
00204 INLINE void enable_color_material(bool val);
00205 INLINE void enable_fog(bool val);
00206 INLINE void enable_zwritemask(bool val);
00207 INLINE void set_color_writemask(UINT color_writemask);
00208 INLINE void enable_gouraud_shading(bool val);
00209 INLINE void set_vertex_format(DWORD NewFvfType);
00210
00211 INLINE D3DTEXTUREADDRESS get_texture_wrap_mode(Texture::WrapMode wm) const;
00212 INLINE D3DFOGMODE get_fog_mode_type(Fog::Mode m) const;
00213
00214 INLINE void enable_primitive_clipping(bool val);
00215 INLINE void enable_alpha_test(bool val);
00216 INLINE void enable_line_smooth(bool val);
00217 INLINE void enable_blend(bool val);
00218 INLINE void enable_point_smooth(bool val);
00219 INLINE void enable_texturing(bool val);
00220 INLINE void call_dxLightModelAmbient(const Colorf& color);
00221 INLINE void call_dxAlphaFunc(D3DCMPFUNC func, float refval);
00222 INLINE void call_dxBlendFunc(D3DBLEND sfunc, D3DBLEND dfunc);
00223 INLINE void enable_dither(bool val);
00224 INLINE void enable_stencil_test(bool val);
00225 void report_texmgr_stats();
00226 void draw_multitri(Geom *geom, D3DPRIMITIVETYPE tri_id);
00227
00228 void draw_prim_inner_loop(int nVerts, const Geom *geom, ushort perFlags);
00229 void draw_prim_inner_loop_coordtexonly(int nVerts, const Geom *geom);
00230 size_t draw_prim_setup(const Geom *geom) ;
00231
00232
00233 Normalf p_normal;
00234 TexCoordf p_texcoord;
00235 D3DCOLOR _curD3Dcolor;
00236 DWORD _perPrim,_perVertex,_perComp;
00237 DWORD _CurFVFType;
00238
00239 BYTE *_pCurFvfBufPtr,*_pFvfBufBasePtr;
00240 WORD *_index_buf;
00241
00242 D3DCOLOR _scene_graph_color_D3DCOLOR;
00243 D3DCOLOR _d3dcolor_clear_value;
00244
00245 bool _bGouraudShadingOn;
00246 UINT _color_writemask;
00247 bool _bDrawPrimDoSetupVertexBuffer;
00248 DXGeomNodeContext8 *_pCurrentGeomContext;
00249
00250
00251 Geom::VertexIterator vi;
00252 Geom::NormalIterator ni;
00253 Geom::TexCoordIterator ti;
00254 Geom::ColorIterator ci;
00255
00256
00257
00258 Vertexf *_coord_array,*_pCurCoord;
00259 ushort *_coordindex_array,*_pCurCoordIndex;
00260
00261 TexCoordf *_texcoord_array,*_pCurTexCoord;
00262 ushort *_texcoordindex_array,*_pCurTexCoordIndex;
00263
00264
00265
00266
00267
00268
00269
00270 Colorf _lmodel_ambient;
00271 float _material_ambient;
00272 float _material_diffuse;
00273 float _material_specular;
00274 float _material_shininess;
00275 float _material_emission;
00276
00277 typedef enum {None,
00278 PerVertexFog=D3DRS_FOGVERTEXMODE,
00279 PerPixelFog=D3DRS_FOGTABLEMODE
00280 } DxgsgFogType;
00281 DxgsgFogType _doFogType;
00282 bool _fog_enabled;
00283
00284
00285
00286
00287
00288 float _alpha_func_refval;
00289 D3DCMPFUNC _alpha_func;
00290
00291 D3DBLEND _blend_source_func;
00292 D3DBLEND _blend_dest_func;
00293
00294 bool _line_smooth_enabled;
00295 bool _color_material_enabled;
00296 bool _texturing_enabled;
00297 bool _clipping_enabled;
00298 bool _dither_enabled;
00299 bool _stencil_test_enabled;
00300 bool _blend_enabled;
00301 bool _depth_test_enabled;
00302 bool _depth_write_enabled;
00303 bool _alpha_test_enabled;
00304 DWORD _clip_plane_bits;
00305
00306 RenderModeAttrib::Mode _current_fill_mode;
00307
00308
00309
00310
00311
00312 TextureApplyAttrib::Mode _CurTexBlendMode;
00313 D3DTEXTUREFILTERTYPE _CurTexMagFilter,_CurTexMinFilter,_CurTexMipFilter;
00314 DWORD _CurTexAnisoDegree;
00315 Texture::WrapMode _CurTexWrapModeU,_CurTexWrapModeV;
00316 LMatrix4f _current_projection_mat;
00317 int _projection_mat_stack_count;
00318
00319 CPT(DisplayRegion) _actual_display_region;
00320
00321
00322 INLINE void transform_color(Colorf &InColor,D3DCOLOR &OutColor);
00323
00324 bool _overlay_windows_supported;
00325
00326 #if 0
00327
00328
00329
00330
00331
00332
00333 GraphicsWindow *_win;
00334 #endif
00335
00336 public:
00337 static GraphicsStateGuardian*
00338 make_DXGraphicsStateGuardian8(const FactoryParams ¶ms);
00339 void set_context(DXScreenData *pNewContextData);
00340
00341 static TypeHandle get_class_type(void);
00342 static void init_type(void);
00343 virtual TypeHandle get_type(void) const;
00344 virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
00345 INLINE void SetDXReady(bool status) { _bDXisReady = status; }
00346 INLINE bool GetDXReady(void) { return _bDXisReady;}
00347 void DXGraphicsStateGuardian8::SetTextureBlendMode(TextureApplyAttrib::Mode TexBlendMode,bool bJustEnable);
00348
00349
00350 DXShaderHandle read_vertex_shader(string &filename);
00351 DXShaderHandle read_pixel_shader(string &filename);
00352 void init_shader(ShaderType stype,DXShaderHandle &hShader,string *pFname);
00353 IDirect3DTexture8 *_pGlobalTexture;
00354 DXShaderHandle _CurVertexShader,_CurPixelShader;
00355
00356 void dx_cleanup(bool bRestoreDisplayMode,bool bAtExitFnCalled);
00357 void reset_panda_gsg(void);
00358 HRESULT reset_d3d_device(D3DPRESENT_PARAMETERS *pPresParams);
00359
00360 #define DO_REACTIVATE_WINDOW true
00361 bool CheckCooperativeLevel(bool bDoReactivateWindow = false);
00362
00363 void show_frame(bool bNoNewFrameDrawn = false);
00364 void dx_init(void);
00365
00366 void support_overlay_window(bool flag);
00367
00368 private:
00369 static TypeHandle _type_handle;
00370 };
00371
00372 HRESULT CreateDX8Cursor(LPDIRECT3DDEVICE8 pd3dDevice, HCURSOR hCursor,BOOL bAddWatermark);
00373
00374 #include "DXGraphicsStateGuardian8.I"
00375 #endif
00376