private void OnViewportRender(object sender, RenderEventArgs e) { foreach (IRenderBlock block in this._Model.Blocks) { using (var stateBlock = new StateBlock(e.Device, StateBlockMask.EnableAll())) { stateBlock.Capture(); var dssd = new DepthStencilStateDescription(); dssd.IsDepthEnabled = true; dssd.DepthWriteMask = DepthWriteMask.All; dssd.DepthComparison = Comparison.Less; dssd.IsStencilEnabled = true; dssd.StencilReadMask = 0xFF; dssd.StencilWriteMask = 0xFF; var frontFace = new DepthStencilOperationDescription(); frontFace.FailOperation = StencilOperation.Keep; frontFace.DepthFailOperation = StencilOperation.Increment; frontFace.PassOperation = StencilOperation.Keep; frontFace.Comparison = Comparison.Always; dssd.FrontFace = frontFace; var backFace = new DepthStencilOperationDescription(); backFace.FailOperation = StencilOperation.Keep; backFace.DepthFailOperation = StencilOperation.Decrement; backFace.PassOperation = StencilOperation.Keep; backFace.Comparison = Comparison.Always; dssd.BackFace = backFace; e.Device.OutputMerger.DepthStencilState = DepthStencilState.FromDescription(e.Device, dssd); var oldState = e.Device.Rasterizer.State.Description; var state = e.Device.Rasterizer.State.Description; state.FillMode = _SelectedBlocks.Contains(block) == true ? FillMode.Wireframe : FillMode.Solid; state.CullMode = CullMode.None; e.Device.Rasterizer.State = RasterizerState.FromDescription(e.Device, state); if (this._BlockRenderers.ContainsKey(block) == false) { var renderer = RendererTypes.Instantiate(block); if (renderer == null) { continue; } renderer.Setup(e.Device, block, this._ShaderBundle, this._ModelPath); this._BlockRenderers.Add(block, renderer); this._BlockRenderers[block].Render(e.Device, e.ViewProjectionMatrix); } else { this._BlockRenderers[block].Render(e.Device, e.ViewProjectionMatrix); } e.Device.Rasterizer.State = RasterizerState.FromDescription(e.Device, oldState); stateBlock.Apply(); } } }
/// <summary> /// Create a state block. /// </summary> /// <remarks> /// A state block is a collection of device state, and is used for saving and restoring device state. Use a state-block mask to enable subsets of state for saving and restoring. The <see cref="SharpDX.Direct3D10.StateBlockMask"/> structure can be filled manually or by using any of the D3D10StateBlockMaskXXX APIs. A state block mask can also be obtained by calling <see cref="SharpDX.Direct3D10.EffectTechnique.ComputeStateBlockMask"/> or <see cref="SharpDX.Direct3D10.EffectPass.ComputeStateBlockMask"/>. Differences between Direct3D 9 and Direct3D 10: In Direct3D 10, a state block object does not contain any valid information about the state of the device until <see cref="SharpDX.Direct3D10.StateBlock.Capture"/> is called. In Direct3D 9, state is saved in a state block object, when it is created. ? /// </remarks> /// <param name="device">The device for which the state block will be created. </param> /// <param name="mask">Indicates which parts of the device state will be captured when calling <see cref="SharpDX.Direct3D10.StateBlock.Capture"/> and reapplied when calling <see cref="SharpDX.Direct3D10.StateBlock.Apply"/>. See remarks. </param> /// <unmanaged>HRESULT D3D10CreateStateBlock([None] ID3D10Device* pDevice,[None] D3D10_STATE_BLOCK_MASK* pStateBlockMask,[None] ID3D10StateBlock** ppStateBlock)</unmanaged> public StateBlock(Device device, StateBlockMask mask) { D3D10.CreateStateBlock(device, ref mask, this); }