示例#1
0
        protected void Render()
        {
            while (true)
            {
                Timer.Instance.Tick();

                if (Resize)
                {
                    DeviceManager.Instance.Resize();
                    Resize = false;
                }

                frameCounter.Count();

                DeviceManager deviceManager = DeviceManager.Instance;
                deviceManager.context.ClearDepthStencilView(deviceManager.depthStencil, DepthStencilClearFlags.Depth | DepthStencilClearFlags.Stencil, 1.0f, 0);
                deviceManager.context.ClearRenderTargetView(deviceManager.renderTarget, new Color4(BackgroundColour));

                CameraManager.Instance.UpdateFrameCamera();

                SlimDX.Direct3D11.DepthStencilState    oldDSState         = DeviceManager.Instance.context.OutputMerger.DepthStencilState;
                SlimDX.Direct3D11.BlendState           oldBlendState      = DeviceManager.Instance.context.OutputMerger.BlendState;
                SlimDX.Direct3D11.RasterizerState      oldRasterizerState = DeviceManager.Instance.context.Rasterizer.State;
                SlimDX.Direct3D11.VertexShader         oldVertexShader    = DeviceManager.Instance.context.VertexShader.Get();
                SlimDX.Direct3D11.Buffer[]             oldVSCBuffers      = DeviceManager.Instance.context.VertexShader.GetConstantBuffers(0, 10);
                SlimDX.Direct3D11.PixelShader          oldPixelShader     = DeviceManager.Instance.context.PixelShader.Get();
                SlimDX.Direct3D11.Buffer[]             oldPSCBuffers      = DeviceManager.Instance.context.PixelShader.GetConstantBuffers(0, 10);
                SlimDX.Direct3D11.ShaderResourceView[] oldShaderResources = DeviceManager.Instance.context.PixelShader.GetShaderResources(0, 10);
                SlimDX.Direct3D11.GeometryShader       oldGeometryShader  = DeviceManager.Instance.context.GeometryShader.Get();

                if (ViewMode == ViewMode.Scene)
                {
                    if (CurrentScene != null)
                    {
                        CurrentScene.Render();
                    }
                }
                else if (ViewMode == ViewMode.Object)
                {
                    if (CurrentObject != null)
                    {
                        CurrentObject.Render();
                    }
                }

                DeviceManager.Instance.context.OutputMerger.DepthStencilState = oldDSState;
                DeviceManager.Instance.context.OutputMerger.BlendState        = oldBlendState;
                DeviceManager.Instance.context.Rasterizer.State = oldRasterizerState;
                DeviceManager.Instance.context.VertexShader.Set(oldVertexShader);
                DeviceManager.Instance.context.VertexShader.SetConstantBuffers(oldVSCBuffers, 0, 10);
                DeviceManager.Instance.context.PixelShader.Set(oldPixelShader);
                DeviceManager.Instance.context.PixelShader.SetConstantBuffers(oldPSCBuffers, 0, 10);
                DeviceManager.Instance.context.PixelShader.SetShaderResources(oldShaderResources, 0, 10);
                DeviceManager.Instance.context.GeometryShader.Set(oldGeometryShader);

                Monitor.Enter(DeviceManager.Instance.device);
                _spriteRenderer.RefreshViewport();
                _spriteRenderer.Flush();
                Monitor.Exit(DeviceManager.Instance.device);

                DeviceManager.Instance.context.OutputMerger.DepthStencilState = oldDSState;
                DeviceManager.Instance.context.OutputMerger.BlendState        = oldBlendState;
                DeviceManager.Instance.context.Rasterizer.State = oldRasterizerState;
                DeviceManager.Instance.context.VertexShader.Set(oldVertexShader);
                DeviceManager.Instance.context.VertexShader.SetConstantBuffers(oldVSCBuffers, 0, 10);
                DeviceManager.Instance.context.PixelShader.Set(oldPixelShader);
                DeviceManager.Instance.context.PixelShader.SetConstantBuffers(oldPSCBuffers, 0, 10);
                DeviceManager.Instance.context.PixelShader.SetShaderResources(oldShaderResources, 0, 10);
                DeviceManager.Instance.context.GeometryShader.Set(oldGeometryShader);

                // syncInterval can be 0
                deviceManager.swapChain.Present(syncInterval, PresentFlags.None);
            }
        }