private static void RestoreState(BackupDx11State?oldState) { if (!_backupState || !oldState.HasValue) { return; } BackupDx11State old = oldState.Value; _deviceContext.Rasterizer.SetScissorRectangles(old.ScissorRects); _deviceContext.Rasterizer.SetViewports(old.Viewports); _deviceContext.Rasterizer.State = new RasterizerState(old.RasterizerState); _deviceContext.OutputMerger.SetBlendState(new BlendState(old.BlendState), old.BlendFactor, old.SampleMask); _deviceContext.OutputMerger.SetDepthStencilState(new DepthStencilState(old.DepthStencilState)); _deviceContext.PixelShader.SetShaderResource(0, new ShaderResourceView(old.PSShaderResource)); _deviceContext.PixelShader.SetSampler(0, new SamplerState(old.PSSampler)); _deviceContext.PixelShader.Set(new PixelShader(old.PS), old.PSInstances); _deviceContext.VertexShader.Set(new VertexShader(old.VS), old.VSInstances); _deviceContext.VertexShader.SetConstantBuffer(0, new Buffer(old.VSConstantBuffer)); _deviceContext.GeometryShader.Set(new GeometryShader(old.GS), old.GSInstances); _deviceContext.InputAssembler.PrimitiveTopology = old.PrimitiveTopology; _deviceContext.InputAssembler.SetIndexBuffer(new Buffer(old.IndexBuffer), old.IndexBufferFormat, old.IndexBufferOffset); _deviceContext.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding { Buffer = new Buffer(old.VertexBuffer), Stride = old.VertexBufferStride, Offset = old.VertexBufferOffset }); _deviceContext.InputAssembler.InputLayout = new InputLayout(old.InputLayout); }
// This is pretty awful in the main code, and SharpDX makes it worse // Best to only use this when actually necessary private static BackupDx11State?BackupState() { if (_backupState) { // (unfortunately this is very ugly looking and verbose. Close your eyes!) BackupDx11State old = new BackupDx11State(); old.ScissorRects = new Rectangle[16]; old.Viewports = new RawViewportF[16]; _deviceContext.Rasterizer.GetScissorRectangles(old.ScissorRects); _deviceContext.Rasterizer.GetViewports(old.Viewports); old.RasterizerState = _deviceContext.Rasterizer.State?.NativePointer ?? IntPtr.Zero; old.BlendState = _deviceContext.OutputMerger.GetBlendState(out old.BlendFactor, out old.SampleMask)?.NativePointer ?? IntPtr.Zero; old.DepthStencilState = _deviceContext.OutputMerger.GetDepthStencilState(out old.StencilRef)?.NativePointer ?? IntPtr.Zero; old.PSShaderResource = _deviceContext.PixelShader.GetShaderResources(0, 1)[0]?.NativePointer ?? IntPtr.Zero; old.PSSampler = _deviceContext.PixelShader.GetSamplers(0, 1)[0]?.NativePointer ?? IntPtr.Zero; // this is really poorly done in SharpDX... they hide the methods that would return the count old.PS = GetShaderInstances(_deviceContext.PixelShader, out old.PSInstances)?.NativePointer ?? IntPtr.Zero; old.VS = GetShaderInstances(_deviceContext.VertexShader, out old.VSInstances)?.NativePointer ?? IntPtr.Zero; old.VSConstantBuffer = _deviceContext.VertexShader.GetConstantBuffers(0, 1)[0]?.NativePointer ?? IntPtr.Zero; old.GS = GetShaderInstances(_deviceContext.GeometryShader, out old.GSInstances)?.NativePointer ?? IntPtr.Zero; old.PrimitiveTopology = _deviceContext.InputAssembler.PrimitiveTopology; _deviceContext.InputAssembler.GetIndexBuffer(out Buffer indexBufferRef, out old.IndexBufferFormat, out old.IndexBufferOffset); old.IndexBuffer = indexBufferRef?.NativePointer ?? IntPtr.Zero; var vertexBuffersOut = new Buffer[1]; var stridesRef = new int[1]; var offsetsRef = new int[1]; _deviceContext.InputAssembler.GetVertexBuffers(0, 1, vertexBuffersOut, stridesRef, offsetsRef); old.VertexBuffer = vertexBuffersOut[0]?.NativePointer ?? IntPtr.Zero; old.VertexBufferStride = stridesRef[0]; old.VertexBufferOffset = offsetsRef[0]; old.InputLayout = _deviceContext.InputAssembler.InputLayout?.NativePointer ?? IntPtr.Zero; return(old); } return(null); }