Browse Source

fix crash at exit on dx9

David Rose 17 years ago
parent
commit
f29f5297f6

+ 0 - 1
panda/src/dxgsg9/dxGraphicsDevice9.cxx

@@ -37,5 +37,4 @@ DXGraphicsDevice9(wdxGraphicsPipe9 *pipe) :
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 DXGraphicsDevice9::
 DXGraphicsDevice9::
 ~DXGraphicsDevice9() {
 ~DXGraphicsDevice9() {
-
 }
 }

+ 21 - 4
panda/src/dxgsg9/dxGraphicsStateGuardian9.cxx

@@ -81,6 +81,8 @@ D3DMATRIX DXGraphicsStateGuardian9::_d3d_ident_mat;
 unsigned char *DXGraphicsStateGuardian9::_temp_buffer = NULL;
 unsigned char *DXGraphicsStateGuardian9::_temp_buffer = NULL;
 unsigned char *DXGraphicsStateGuardian9::_safe_buffer_start = NULL;
 unsigned char *DXGraphicsStateGuardian9::_safe_buffer_start = NULL;
 
 
+LPDIRECT3DDEVICE9 DXGraphicsStateGuardian9::_cg_device = NULL;
+
 #define __D3DLIGHT_RANGE_MAX ((float)sqrt(FLT_MAX))  //for some reason this is missing in dx9 hdrs
 #define __D3DLIGHT_RANGE_MAX ((float)sqrt(FLT_MAX))  //for some reason this is missing in dx9 hdrs
 
 
 #define MY_D3DRGBA(r, g, b, a) ((D3DCOLOR) D3DCOLOR_COLORVALUE(r, g, b, a))
 #define MY_D3DRGBA(r, g, b, a) ((D3DCOLOR) D3DCOLOR_COLORVALUE(r, g, b, a))
@@ -173,9 +175,6 @@ DXGraphicsStateGuardian9::
   if (IS_VALID_PTR(_d3d_device)) {
   if (IS_VALID_PTR(_d3d_device)) {
     _d3d_device->SetTexture(0, NULL);  // this frees reference to the old texture
     _d3d_device->SetTexture(0, NULL);  // this frees reference to the old texture
   }
   }
-#ifdef HAVE_CG
-  cgD3D9SetDevice(NULL);
-#endif // HAVE_CG
 
 
   free_nondx_resources();
   free_nondx_resources();
 }
 }
@@ -2432,7 +2431,7 @@ reset() {
   _auto_detect_shader_model = _shader_model;
   _auto_detect_shader_model = _shader_model;
 
 
 #ifdef HAVE_CG
 #ifdef HAVE_CG
-  cgD3D9SetDevice (_d3d_device);
+  set_cg_device(_d3d_device);
 
 
   if (cgD3D9IsProfileSupported(CG_PROFILE_PS_2_0) &&
   if (cgD3D9IsProfileSupported(CG_PROFILE_PS_2_0) &&
       cgD3D9IsProfileSupported(CG_PROFILE_VS_2_0)) {
       cgD3D9IsProfileSupported(CG_PROFILE_VS_2_0)) {
@@ -4416,6 +4415,7 @@ set_context(DXScreenData *new_context) {
   _swap_chain = _screen->_swap_chain;   //copy this one field for speed of deref
   _swap_chain = _screen->_swap_chain;   //copy this one field for speed of deref
 
 
   _screen->_dxgsg9 = this;
   _screen->_dxgsg9 = this;
+  set_cg_device(_d3d_device);
 }
 }
 
 
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
@@ -5663,9 +5663,26 @@ restore_gamma() {
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 void DXGraphicsStateGuardian9::
 void DXGraphicsStateGuardian9::
 atexit_function(void) {
 atexit_function(void) {
+  set_cg_device(NULL);
   static_set_gamma(true, 1.0f);
   static_set_gamma(true, 1.0f);
 }
 }
 
 
+////////////////////////////////////////////////////////////////////
+//     Function: DXGraphicsStateGuardian9::set_cg_device
+//       Access: Protected, Static
+//  Description: Sets the global Cg device pointer.  TODO: make this
+//               thread-safe somehow.  Maybe Cg is inherently not
+//               thread-safe.
+////////////////////////////////////////////////////////////////////
+void DXGraphicsStateGuardian9::
+set_cg_device(LPDIRECT3DDEVICE9 cg_device) {
+#ifdef HAVE_CG
+  if (_cg_device != cg_device) {
+    cgD3D9SetDevice(cg_device);
+    _cg_device = cg_device;
+  }
+#endif // HAVE_CG
+}
 
 
 
 
 
 

+ 4 - 0
panda/src/dxgsg9/dxGraphicsStateGuardian9.h

@@ -164,6 +164,8 @@ public:
   void restore_gamma();
   void restore_gamma();
   static void atexit_function(void);
   static void atexit_function(void);
 
 
+  static void set_cg_device(LPDIRECT3DDEVICE9 cg_device);
+
 protected:
 protected:
   void do_issue_transform();
   void do_issue_transform();
   void do_issue_alpha_test();
   void do_issue_alpha_test();
@@ -366,6 +368,8 @@ protected:
 
 
   int _supports_gamma_calibration;  
   int _supports_gamma_calibration;  
 
 
+  static LPDIRECT3DDEVICE9 _cg_device;
+
 public:
 public:
   virtual TypeHandle get_type() const {
   virtual TypeHandle get_type() const {
     return get_class_type();
     return get_class_type();

+ 3 - 0
panda/src/dxgsg9/wdxGraphicsWindow9.cxx

@@ -256,6 +256,9 @@ close_window() {
     _gsg.clear();
     _gsg.clear();
     _active = false;
     _active = false;
   }
   }
+
+  DXGraphicsStateGuardian9::set_cg_device(NULL);
+
   _dxgsg->release_swap_chain(&_wcontext);
   _dxgsg->release_swap_chain(&_wcontext);
   WinGraphicsWindow::close_window();
   WinGraphicsWindow::close_window();
 }
 }