private void Destroy()
        {
            mOffScreenWindow?.Close();
            mSwapChain?.Dispose();
            mPixelShader?.Dispose();
            mVertexShader?.Dispose();
            mVertexBuffer?.Dispose();
            mVertices?.Dispose();
            mRenderTarget?.Dispose();

            mOffScreenWindow = null;
            mSwapChain       = null;
            mPixelShader     = null;
            mVertexShader    = null;
            mVertexBuffer    = null;
            mVertices        = null;
            mRenderTarget    = null;
        }
Exemple #2
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);
            }
        }
        private void CreateView(int width, int height)
        {
            if (width <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(width));
            }

            if (height <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(height));
            }

            Destroy();

            mWidth  = width;
            mHeight = height;

            Execution.ExecuteOnUiThread(() =>
            {
                mOffScreenWindow                 = new Form();
                mOffScreenWindow.Left            = -65535;
                mOffScreenWindow.Top             = -65535;
                mOffScreenWindow.FormBorderStyle = FormBorderStyle.None;
                mOffScreenWindow.Width           = 1;
                mOffScreenWindow.Height          = 1;
                mOffScreenWindow.ShowInTaskbar   = false;
                mOffScreenWindow.Show();
                mOffScreenWindow.Hide();
            });

            var description = new SwapChainDescription
            {
                BufferCount       = 1,
                IsWindowed        = true,
                ModeDescription   = new ModeDescription(0, 0, new Rational(60, 1), Format.B8G8R8A8_UNorm),
                SampleDescription = new SampleDescription(1, 0),
                Usage             = Usage.RenderTargetOutput | Usage.Shared,
                OutputHandle      = mOffScreenWindow.Handle
            };

            mSwapChain    = mDevice.CreateSwapChain(description);
            mPixelShader  = new PixelShader(Device, Bytecode);
            mVertexShader = new VertexShader(Device, mVertexShaderBytecode);
            mVertexBuffer = CreateVertexBuffer(out mVertices);

            Execution.ExecuteOnUiThread(() =>
            {
                mOffScreenWindow.Width  = mWidth;
                mOffScreenWindow.Height = mHeight;
            });

            mSwapChain.ResizeBuffers(1, mWidth, mHeight, Format.B8G8R8A8_UNorm, SwapChainFlags.None);
            Device.ImmediateContext.Rasterizer.SetViewports(new Viewport(0.0f, 0.0f, mWidth, mHeight));

            Device.ImmediateContext.Rasterizer.State =
                RasterizerState.FromDescription(Device,
                                                new RasterizerStateDescription
            {
                CullMode = CullMode.None,
                FillMode = FillMode.Solid
            });

            mRenderTarget = new HardwareRenderTarget(mDevice, mSwapChain);
        }