Browse Source

support red-blue stereo in dx8/9

David Rose 20 years ago
parent
commit
82c0011f87

+ 37 - 39
panda/src/dxgsg8/dxGraphicsStateGuardian8.cxx

@@ -558,41 +558,40 @@ do_clear(const RenderBuffer &buffer) {
 //       scissor region and viewport)
 //       scissor region and viewport)
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 void DXGraphicsStateGuardian8::
 void DXGraphicsStateGuardian8::
-prepare_display_region() {
-  if (_current_display_region == (DisplayRegion*)0L) {
+prepare_display_region(DisplayRegion *dr, Lens::StereoChannel stereo_channel) {
+  nassertv(dr != (DisplayRegion *)NULL);
+  GraphicsStateGuardian::prepare_display_region(dr, stereo_channel);
+
+  int l, u, w, h;
+  _current_display_region->get_region_pixels_i(l, u, w, h);
+  
+  // Create the viewport
+  D3DVIEWPORT8 vp = { l, u, w, h, 0.0f, 1.0f };
+  HRESULT hr = _d3d_device->SetViewport(&vp);
+  if (FAILED(hr)) {
     dxgsg8_cat.error()
     dxgsg8_cat.error()
-      << "Invalid NULL display region in prepare_display_region()\n";
-
-  } else if (_current_display_region != _actual_display_region) {
-    _actual_display_region = _current_display_region;
-
-    int l, u, w, h;
-    _actual_display_region->get_region_pixels_i(l, u, w, h);
-
-    // Create the viewport
-    D3DVIEWPORT8 vp = { l, u, w, h, 0.0f, 1.0f };
-    HRESULT hr = _d3d_device->SetViewport(&vp);
+      << "_screen->_swap_chain = " << _screen->_swap_chain << " _swap_chain = " << _swap_chain << "\n";
+    dxgsg8_cat.error()
+      << "SetViewport(" << l << ", " << u << ", " << w << ", " << h
+      << ") failed" << D3DERRORSTRING(hr);
+    
+    D3DVIEWPORT8 vp_old;
+    _d3d_device->GetViewport(&vp_old);
+    dxgsg8_cat.error()
+      << "GetViewport(" << vp_old.X << ", " << vp_old.Y << ", " << vp_old.Width << ", "
+      << vp_old.Height << ") returned: Trying to set that vp---->\n";
+    hr = _d3d_device->SetViewport(&vp_old);
+    
     if (FAILED(hr)) {
     if (FAILED(hr)) {
-      dxgsg8_cat.error()
-        << "_screen->_swap_chain = " << _screen->_swap_chain << " _swap_chain = " << _swap_chain << "\n";
-      dxgsg8_cat.error()
-        << "SetViewport(" << l << ", " << u << ", " << w << ", " << h
-        << ") failed" << D3DERRORSTRING(hr);
-
-      D3DVIEWPORT8 vp_old;
-      _d3d_device->GetViewport(&vp_old);
-      dxgsg8_cat.error()
-        << "GetViewport(" << vp_old.X << ", " << vp_old.Y << ", " << vp_old.Width << ", "
-        << vp_old.Height << ") returned: Trying to set that vp---->\n";
-      hr = _d3d_device->SetViewport(&vp_old);
-
-      if (FAILED(hr)) {
-        dxgsg8_cat.error() << "Failed again\n";
-        throw_event("panda3d-render-error");
-        nassertv(false);
-      }
+      dxgsg8_cat.error() << "Failed again\n";
+      throw_event("panda3d-render-error");
+      nassertv(false);
     }
     }
-    // Note: for DX9, also change scissor clipping state here
+  }
+  // Note: for DX9, also change scissor clipping state here
+
+  if (_screen->_can_direct_disable_color_writes) {
+    _d3d_device->SetRenderState(D3DRS_COLORWRITEENABLE, _color_write_mask);
   }
   }
 }
 }
 
 
@@ -609,7 +608,7 @@ prepare_display_region() {
 //               false if it is not.
 //               false if it is not.
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 bool DXGraphicsStateGuardian8::
 bool DXGraphicsStateGuardian8::
-prepare_lens(Lens::StereoChannel stereo_channel) {
+prepare_lens() {
   if (_current_lens == (Lens *)NULL) {
   if (_current_lens == (Lens *)NULL) {
     return false;
     return false;
   }
   }
@@ -619,7 +618,7 @@ prepare_lens(Lens::StereoChannel stereo_channel) {
   }
   }
 
 
   // Start with the projection matrix from the lens.
   // Start with the projection matrix from the lens.
-  const LMatrix4f &lens_mat = _current_lens->get_projection_mat(stereo_channel);
+  const LMatrix4f &lens_mat = _current_lens->get_projection_mat(_current_stereo_channel);
 
 
   // The projection matrix must always be left-handed Y-up internally,
   // The projection matrix must always be left-handed Y-up internally,
   // to match DirectX's convention, even if our coordinate system of
   // to match DirectX's convention, even if our coordinate system of
@@ -1715,9 +1714,6 @@ reset() {
 
 
   _d3d_device->SetRenderState(D3DRS_CLIPPING, true);
   _d3d_device->SetRenderState(D3DRS_CLIPPING, true);
 
 
-  // these both reflect d3d defaults
-  _color_writemask = 0xFFFFFFFF;
-
   _d3d_device->SetRenderState(D3DRS_SHADEMODE, D3DSHADE_GOURAUD);
   _d3d_device->SetRenderState(D3DRS_SHADEMODE, D3DSHADE_GOURAUD);
 
 
   _d3d_device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
   _d3d_device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
@@ -2788,7 +2784,9 @@ do_issue_blending() {
   // all the other blending-related stuff doesn't matter.  If the
   // all the other blending-related stuff doesn't matter.  If the
   // device doesn't support color-write, we use blending tricks
   // device doesn't support color-write, we use blending tricks
   // to effectively disable color write.
   // to effectively disable color write.
-  if (_target._color_write->get_channels() == ColorWriteAttrib::C_off) {
+  unsigned int color_channels = 
+    _target._color_write->get_channels() & _color_write_mask;
+  if (color_channels == ColorWriteAttrib::C_off) {
     if (_target._color_write != _state._color_write) {
     if (_target._color_write != _state._color_write) {
       if (_screen->_can_direct_disable_color_writes) {
       if (_screen->_can_direct_disable_color_writes) {
         _d3d_device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
         _d3d_device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
@@ -2803,7 +2801,7 @@ do_issue_blending() {
   } else {
   } else {
     if (_target._color_write != _state._color_write) {
     if (_target._color_write != _state._color_write) {
       if (_screen->_can_direct_disable_color_writes) {
       if (_screen->_can_direct_disable_color_writes) {
-        _d3d_device->SetRenderState(D3DRS_COLORWRITEENABLE, _target._color_write->get_channels());
+        _d3d_device->SetRenderState(D3DRS_COLORWRITEENABLE, color_channels);
       }
       }
     }
     }
   }
   }

+ 2 - 4
panda/src/dxgsg8/dxGraphicsStateGuardian8.h

@@ -66,8 +66,8 @@ public:
 
 
   virtual void do_clear(const RenderBuffer &buffer);
   virtual void do_clear(const RenderBuffer &buffer);
 
 
-  virtual void prepare_display_region();
-  virtual bool prepare_lens(Lens::StereoChannel stereo_channel);
+  virtual void prepare_display_region(DisplayRegion *dr, Lens::StereoChannel stereo_channel);
+  virtual bool prepare_lens();
 
 
   virtual bool begin_frame();
   virtual bool begin_frame();
   virtual bool begin_scene();
   virtual bool begin_scene();
@@ -201,7 +201,6 @@ protected:
   bool _auto_rescale_normal;
   bool _auto_rescale_normal;
 
 
   D3DCOLOR _d3dcolor_clear_value;
   D3DCOLOR _d3dcolor_clear_value;
-  UINT _color_writemask;
 
 
   float _material_ambient;
   float _material_ambient;
   float _material_diffuse;
   float _material_diffuse;
@@ -222,7 +221,6 @@ protected:
 
 
   LMatrix4f _projection_mat;
   LMatrix4f _projection_mat;
 
 
-  CPT(DisplayRegion) _actual_display_region;
   const DXVertexBufferContext8 *_active_vbuffer;
   const DXVertexBufferContext8 *_active_vbuffer;
   const DXIndexBufferContext8 *_active_ibuffer;
   const DXIndexBufferContext8 *_active_ibuffer;
 
 

+ 8 - 6
panda/src/dxgsg9/dxGraphicsStateGuardian9.cxx

@@ -804,7 +804,10 @@ prepare_display_region(DisplayRegion *dr, Lens::StereoChannel stereo_channel) {
       nassertv(false);
       nassertv(false);
     }
     }
   }
   }
-  // Note: for DX9, also change scissor clipping state here
+
+  if (_screen->_can_direct_disable_color_writes) {
+    set_render_state(D3DRS_COLORWRITEENABLE, _color_write_mask);
+  }
 }
 }
 
 
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
@@ -2560,9 +2563,6 @@ reset() {
 
 
   set_render_state(D3DRS_CLIPPING, true);
   set_render_state(D3DRS_CLIPPING, true);
 
 
-  // these both reflect d3d defaults
-  _color_writemask = 0xFFFFFFFF;
-
   set_render_state(D3DRS_SHADEMODE, D3DSHADE_GOURAUD);
   set_render_state(D3DRS_SHADEMODE, D3DSHADE_GOURAUD);
 
 
   set_render_state(D3DRS_ZWRITEENABLE, TRUE);
   set_render_state(D3DRS_ZWRITEENABLE, TRUE);
@@ -3682,7 +3682,9 @@ do_issue_blending() {
   // all the other blending-related stuff doesn't matter.  If the
   // all the other blending-related stuff doesn't matter.  If the
   // device doesn't support color-write, we use blending tricks
   // device doesn't support color-write, we use blending tricks
   // to effectively disable color write.
   // to effectively disable color write.
-  if (_target._color_write->get_channels() == ColorWriteAttrib::C_off) {
+  unsigned int color_channels = 
+    _target._color_write->get_channels() & _color_write_mask;
+  if (color_channels == ColorWriteAttrib::C_off) {
     if (_target._color_write != _state._color_write) {
     if (_target._color_write != _state._color_write) {
       if (_screen->_can_direct_disable_color_writes) {
       if (_screen->_can_direct_disable_color_writes) {
         set_render_state(D3DRS_ALPHABLENDENABLE, FALSE);
         set_render_state(D3DRS_ALPHABLENDENABLE, FALSE);
@@ -3697,7 +3699,7 @@ do_issue_blending() {
   } else {
   } else {
     if (_target._color_write != _state._color_write) {
     if (_target._color_write != _state._color_write) {
       if (_screen->_can_direct_disable_color_writes) {
       if (_screen->_can_direct_disable_color_writes) {
-        set_render_state(D3DRS_COLORWRITEENABLE, _target._color_write->get_channels());
+        set_render_state(D3DRS_COLORWRITEENABLE, color_channels);
       }
       }
     }
     }
   }
   }

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

@@ -258,7 +258,6 @@ protected:
   bool _auto_rescale_normal;
   bool _auto_rescale_normal;
 
 
   D3DCOLOR _d3dcolor_clear_value;
   D3DCOLOR _d3dcolor_clear_value;
-  UINT _color_writemask;
 
 
   float _material_ambient;
   float _material_ambient;
   float _material_diffuse;
   float _material_diffuse;
@@ -286,7 +285,6 @@ protected:
   PT(ShaderExpansion)  _texture_binding_shader_expansion;
   PT(ShaderExpansion)  _texture_binding_shader_expansion;
   CLP(ShaderContext)  *_texture_binding_shader_context;
   CLP(ShaderContext)  *_texture_binding_shader_context;
 
 
-  CPT(DisplayRegion) _actual_display_region;
   const DXVertexBufferContext9 *_active_vbuffer;
   const DXVertexBufferContext9 *_active_vbuffer;
   const DXIndexBufferContext9 *_active_ibuffer;
   const DXIndexBufferContext9 *_active_ibuffer;