Beispiel #1
0
        public void Init()
        {
            if (Control == null)
            {
                return;
            }

            swapChain = Control.SwapChain;

            Device tDevice = Control.Device;

            //camera = new Camera(Vector3.Zero, Vector3.Zero, ref tDevice);
            device  = tDevice;
            context = device.ImmediateContext;

            //

            string shaderFile = $"{Path.GetDirectoryName(Application.ExecutablePath)}\\triangle.fx";

            using (var bytecode = ShaderBytecode.CompileFromFile(shaderFile, "VShader", "vs_4_0", ShaderFlags.None, EffectFlags.None))
            //effect = new Effect(device, bytecode);
            {
                inputSignature = ShaderSignature.GetInputSignature(bytecode);
                vertexShader   = new VertexShader(device, bytecode);
            }

            // load and compile the pixel shader
            using (var bytecode = ShaderBytecode.CompileFromFile(shaderFile, "PShader", "ps_4_0", ShaderFlags.None, EffectFlags.None))
                pixelShader = new PixelShader(device, bytecode);

            // create test vertex data, making sure to rewind the stream afterward

            /*var vertices = new DataStream(12 * 3, true, true);
             * vertices.Write(new Vector3(0, .25f, .5f));
             * vertices.Write(new Vector3(0, -.5f, .5f));
             * vertices.Write(new Vector3(-.5f, -.5f, .5f));
             * vertices.Position = 0;
             *
             * // create the vertex layout and buffer
             * var elements = new[] { new InputElement("POSITION", 0, Format.R32G32B32_Float, 0) };
             * var layout = new InputLayout(device, inputSignature, elements);
             * var vertexBuffer = new Buffer(device, vertices, 12 * 3, ResourceUsage.Default, BindFlags.VertexBuffer, CpuAccessFlags.None,
             *  ResourceOptionFlags.None, 0);
             *
             * // configure the Input Assembler portion of the pipeline with the vertex data
             * context.InputAssembler.InputLayout = layout;
             * context.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;
             * context.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(vertexBuffer, 12, 0));
             *
             * // set the shaders
             * context.VertexShader.Set(vertexShader);
             * context.PixelShader.Set(pixelShader);
             *
             * // render
             * Render(Color.Black);*/
        }
        protected override void Deallocate()
        {
            Destroy();

            mConstants.Reset();
            mResources.Reset();

            mShader?.Dispose();
            mShader = null;
        }
        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;
        }
Beispiel #4
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);
        }
 protected override void OnBytecodeLoaded()
 {
     mShader = new PixelShader(Device, Bytecode);
     Device.ImmediateContext.PixelShader.Set(mShader);
 }