Exemplo n.º 1
0
        void host_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            lock (syncObject)
            {
                if (device == null)
                {
                    return;
                }
                uint nWidth  = (uint)host.ActualWidth;
                uint nHeight = (uint)host.ActualHeight;

                device.OM.RenderTargets = new OutputMergerRenderTargets(new RenderTargetView[] { null }, null);
                //need to remove the reference to the swapchain's backbuffer to enable ResizeBuffers() call
                renderTargetView.Dispose();
                depthStencilView.Dispose();
                depthStencil.Dispose();

                device.RS.Viewports = null;

                SwapChainDescription sd = swapChain.Description;
                //Change the swap chain's back buffer size, format, and number of buffers
                swapChain.ResizeBuffers(
                    sd.BufferCount,
                    nWidth,
                    nHeight,
                    sd.BufferDescription.Format,
                    sd.Options);

                using (Texture2D pBuffer = swapChain.GetBuffer <Texture2D>(0))
                {
                    renderTargetView = device.CreateRenderTargetView(pBuffer);
                }

                InitializeDepthStencil(nWidth, nHeight);

                // bind the views to the device
                device.OM.RenderTargets = new OutputMergerRenderTargets(new[] { renderTargetView }, depthStencilView);

                SetViewport(nWidth, nHeight);

                // update the aspect ratio
                projectionMatrix = Camera.MatrixPerspectiveFovLH(
                    (float)Math.PI * 0.1f,   // fovy
                    nWidth / (float)nHeight, // aspect
                    0.1f,                    // zn
                    100.0f                   // zf
                    );
                projectionMarixVariable.Matrix = projectionMatrix;
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Sets the views that depend on background buffer dimensions
        /// </summary>
        private void SetViews()
        {
            // Create a render target view
            using (Texture2D pBuffer = swapChain.GetBuffer <Texture2D>(0))
            {
                renderTargetView = device.CreateRenderTargetView(pBuffer);
            }

            //bind the render target to the device
            device.OM.RenderTargets = new OutputMergerRenderTargets(new RenderTargetView[] { renderTargetView }, null);

            // Setup the viewport
            Viewport vp = new Viewport()
            {
                Width    = (uint)directControl.ClientSize.Width,
                Height   = (uint)directControl.ClientSize.Height,
                MinDepth = 0.0f,
                MaxDepth = 1.0f,
                TopLeftX = 0,
                TopLeftY = 0
            };

            device.RS.Viewports = new Viewport[] { vp };
        }
Exemplo n.º 3
0
        void CreateDeviceResources()
        {
            uint nWidth  = (uint)host.ActualWidth;
            uint nHeight = (uint)host.ActualHeight;

            // Create D3D device and swap chain
            SwapChainDescription swapDesc = new SwapChainDescription
            {
                BufferDescription = new ModeDescription
                {
                    Width       = nWidth, Height = nHeight,
                    Format      = Format.R8G8B8A8UNorm,
                    RefreshRate = new Rational {
                        Numerator = 60, Denominator = 1
                    }
                },
                SampleDescription = new SampleDescription {
                    Count = 1, Quality = 0
                },
                BufferUsage        = UsageOptions.RenderTargetOutput,
                BufferCount        = 1,
                OutputWindowHandle = host.Handle,
                Windowed           = true
            };

            device = D3DDevice1.CreateDeviceAndSwapChain1(
                null,
                DriverType.Hardware,
                null,
                CreateDeviceOptions.SupportBgra,
                FeatureLevel.NinePointThree,
                swapDesc
                );
            swapChain = device.SwapChain;

            using (Texture2D pBuffer = swapChain.GetBuffer <Texture2D>(0))
            {
                renderTargetView = device.CreateRenderTargetView(pBuffer);
            }

            MakeBothSidesRendered();
            InitializeDepthStencil(nWidth, nHeight);

            device.OM.RenderTargets = new OutputMergerRenderTargets(new[] { renderTargetView }, depthStencilView);

            // Set a new viewport based on the new dimensions
            SetViewport(nWidth, nHeight);

            // Load pixel shader
            shader = LoadResourceShader(device, "Microsoft.WindowsAPICodePack.DirectX.Samples.dxgisample.fxo");

            // Obtain the technique
            technique = shader.GetTechniqueByName("Render");

            // Create the input layout
            InitializeGeometryBuffers();

            // Obtain the variables
            Initialize3DTransformations(nWidth, nHeight);

            // Allocate a offscreen D3D surface for D2D to render our 2D content into
            InitializeTextureRenderTarget();

            // Create a D2D render target which can draw into the surface in the swap chain
            CreateD2DRenderTargets();
        }
Exemplo n.º 4
0
        void CreateDeviceResources()
        {
            uint nWidth = (uint)host.ActualWidth;
            uint nHeight = (uint)host.ActualHeight;

            // Create D3D device and swap chain
            SwapChainDescription swapDesc = new SwapChainDescription
            {
                BufferDescription = new ModeDescription
                {
                    Width = nWidth, Height = nHeight,
                    Format = Format.R8G8B8A8UNorm,
                    RefreshRate = new Rational { Numerator = 60, Denominator = 1 }
                },
                SampleDescription = new SampleDescription { Count = 1, Quality = 0 },
                BufferUsage = UsageOptions.RenderTargetOutput,
                BufferCount = 1,
                OutputWindowHandle = host.Handle,
                Windowed = true
            };

            device = D3DDevice1.CreateDeviceAndSwapChain1(
                null,
                DriverType.Hardware,
                null,
                CreateDeviceOptions.SupportBgra,
                FeatureLevel.NinePointThree,
                swapDesc
                );
            swapChain = device.SwapChain;

            using (Texture2D pBuffer = swapChain.GetBuffer<Texture2D>(0))
            {
                renderTargetView = device.CreateRenderTargetView(pBuffer);
            }

            MakeBothSidesRendered();
            InitializeDepthStencil(nWidth, nHeight);

            device.OM.RenderTargets = new OutputMergerRenderTargets(new[] { renderTargetView }, depthStencilView);

            // Set a new viewport based on the new dimensions
            SetViewport(nWidth, nHeight);

            // Load pixel shader
            shader = LoadResourceShader(device, "Microsoft.WindowsAPICodePack.DirectX.Samples.dxgisample.fxo");

            // Obtain the technique
            technique = shader.GetTechniqueByName("Render");

            // Create the input layout
            InitializeGeometryBuffers();

            // Obtain the variables
            Initialize3DTransformations(nWidth, nHeight);

            // Allocate a offscreen D3D surface for D2D to render our 2D content into
            InitializeTextureRenderTarget();

            // Create a D2D render target which can draw into the surface in the swap chain
            CreateD2DRenderTargets();
        }
Exemplo n.º 5
0
        void CreateDeviceResources()
        {
            uint nWidth = (uint)host.ActualWidth;
            uint nHeight = (uint)host.ActualHeight;

            // Create D3D device and swap chain
            SwapChainDescription swapDesc = new SwapChainDescription();
            swapDesc.BufferDescription.Width = nWidth;
            swapDesc.BufferDescription.Height = nHeight;
            swapDesc.BufferDescription.Format = Format.R8G8B8A8_UNORM;
            swapDesc.BufferDescription.RefreshRate.Numerator = 60;
            swapDesc.BufferDescription.RefreshRate.Denominator = 1;
            swapDesc.SampleDescription.Count = 1;
            swapDesc.SampleDescription.Quality = 0;
            swapDesc.BufferUsage = UsageOption.RenderTargetOutput;
            swapDesc.BufferCount = 1;
            swapDesc.OutputWindowHandle = host.Handle;
            swapDesc.Windowed = true;
            device = D3DDevice1.CreateDeviceAndSwapChain1(
                null,
                DriverType.Hardware,
                null,
                CreateDeviceFlag.SupportBGRA,
                FeatureLevel.FeatureLevel_10_0,
                swapDesc,
                out swapChain
                );

            using (Texture2D pBuffer = swapChain.GetBuffer<Texture2D>(0))
            {
                renderTargetView = device.CreateRenderTargetView(pBuffer);
            }

            MakeBothSidesRendered();
            InitializeDepthStencil(nWidth, nHeight);

            device.OM.SetRenderTargets(new[] { renderTargetView }, depthStencilView);

            // Set a new viewport based on the new dimensions
            SetViewport(nWidth, nHeight);

            // Load pixel shader
            shader = LoadResourceShader(device, "Microsoft.WindowsAPICodePack.DirectX.Samples.dxgisample.fxo");

            // Obtain the technique
            technique = shader.GetTechniqueByName("Render");

            // Create the input layout
            InitializeGeometryBuffers();

            // Obtain the variables
            Initialize3DTransformations(nWidth, nHeight);

            // Allocate a offscreen D3D surface for D2D to render our 2D content into
            InitializeTextureRenderTarget();

            // Create a D2D render target which can draw into the surface in the swap chain
            CreateD2DRenderTargets();
        }