Пример #1
0
        public override void Resize(int width, int height)
        {
            swapChain.ResizeBuffers(2, width, height, Format.R8G8B8A8_UNorm, SwapChainFlags.None);

            using (var backBuffer = swapChain.GetBackBuffer <Texture2D>(0)) {
                RenderTarget = new RenderTargetView(D3DDevice, backBuffer);
            }
        }
Пример #2
0
        public void Resize(float w, float h)
        {
            var width  = (int)w;
            var height = (int)h;

            renderTargetView.Dispose();
            depthStencilView.Dispose();

            swapChain.ResizeBuffers(2, width, height, Format.R8G8B8A8_UNorm, SwapChainFlags.None);

            CreateBuffers(width, height);
        }
        internal void Resize()
        {
            try
            {
                if (device == null)
                {
                    return;
                }

                float aspectRatio = (float)form.Width / (float)form.Height;
                CameraManager.Instance.currentCamera.SetPerspective((float)Math.PI / 4, aspectRatio, CameraManager.znearPlane, CameraManager.zfarPlane);

                // Dispose before resizing.
                if (renderTarget != null)
                {
                    renderTarget.Dispose();
                }

                if (resource != null)
                {
                    resource.Dispose();
                }

                if (depthStencil != null)
                {
                    depthStencil.Dispose();
                }

                swapChain.ResizeBuffers(1,
                                        form.ClientSize.Width,
                                        form.ClientSize.Height,
                                        Format.R8G8B8A8_UNorm,
                                        SwapChainFlags.AllowModeSwitch);

                resource     = Texture2D.FromSwapChain <Texture2D>(swapChain, 0);
                renderTarget = new RenderTargetView(device, resource);

                CreateDepthStencilBuffer(form);

                viewport = new Viewport(0, 0, form.ClientSize.Width, form.ClientSize.Height);
                context.Rasterizer.SetViewport(viewport);
                context.OutputMerger.SetTargets(depthStencil, renderTarget);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
Пример #4
0
        protected override SwapChain CreateSwapChainResources()
        {
            IntPtr?handle  = null;
            var    control = Description.DeviceWindowHandle as Control;

            if (control != null)
            {
                handle = control.Handle;
            }
            else if (Description.DeviceWindowHandle is IntPtr)
            {
                handle = (IntPtr)Description.DeviceWindowHandle;
            }

            if (!handle.HasValue)
            {
                throw new NotSupportedException(
                          string.Format("DeviceWindowHandle of type [{0}] is not supported. Only System.Windows.Control or IntPtr are supported",
                                        Description.DeviceWindowHandle != null ? Description.DeviceWindowHandle.GetType().Name : "null"));
            }

            int requestedMultiSampleCount = (int)Description.MultiSampleCount;
            int multiSampleCount          = Math.Min((int)DirectXDevice.Features[Description.BackBufferFormat].MSAALevelMax, requestedMultiSampleCount);

            if (multiSampleCount < (int)Description.MultiSampleCount)
            {
                LogEvent.Engine.Warning(string.Format("Requested MultiSampleCount of {0} not supported, using {1} instead", requestedMultiSampleCount, multiSampleCount));
            }

            BufferCount = 1;
            var description = new SwapChainDescription
            {
                ModeDescription = new ModeDescription(Description.BackBufferWidth, Description.BackBufferHeight, Description.RefreshRate,
                                                      Description.BackBufferFormat),
                BufferCount       = BufferCount, // TODO: Do we really need this to be configurable by the user?
                OutputHandle      = handle.Value,
                SampleDescription = new SampleDescription(multiSampleCount, 0),
                SwapEffect        = SwapEffect.Discard,
                Usage             = Description.RenderTargetUsage,
                IsWindowed        = true,
                Flags             = Description.Flags,
            };

            var newSwapChain = new SwapChain(GraphicsAdapter.Factory, (Device)DirectXDevice, description);

            if (Description.IsFullScreen)
            {
                // Before fullscreen switch
                newSwapChain.ResizeTarget(ref description.ModeDescription);

                // Switch to full screen
                newSwapChain.IsFullScreen = true;

                // This is really important to call ResizeBuffers AFTER switching to IsFullScreen
                newSwapChain.ResizeBuffers(BufferCount, Description.BackBufferWidth, Description.BackBufferHeight,
                                           Description.BackBufferFormat, SwapChainFlags.AllowModeSwitch);
            }

            return(newSwapChain);
        }
Пример #5
0
        protected override void ResizeBackBuffer(int width, int height, PixelFormat format)
        {
            // Manually update back buffer texture
            backBuffer.OnDestroyed();

#if SILICONSTUDIO_PLATFORM_WINDOWS_RUNTIME
            var swapChainPanel = Description.DeviceWindowHandle.NativeHandle as Windows.UI.Xaml.Controls.SwapChainPanel;
            if (swapChainPanel != null)
            {
                var swapChain2 = swapChain.QueryInterface <SwapChain2>();
                if (swapChain2 != null)
                {
                    swapChain2.MatrixTransform = new RawMatrix3x2 {
                        M11 = 1f / swapChainPanel.CompositionScaleX, M22 = 1f / swapChainPanel.CompositionScaleY
                    };
                    swapChain2.Dispose();
                }
            }
#endif

            // If format is same as before, using Unknown (None) will keep the current
            // We do that because on Win10/RT, actual format might be the non-srgb one and we don't want to switch to srgb one by mistake (or need #ifdef)
            if (format == backBuffer.Format)
            {
                format = PixelFormat.None;
            }

            swapChain.ResizeBuffers(bufferCount, width, height, (SharpDX.DXGI.Format)format, SwapChainFlags.None);

            // Get newly created native texture
            var backBufferTexture = swapChain.GetBackBuffer <BackBufferResourceType>(0);

            // Put it in our back buffer texture
            backBuffer.InitializeFrom(backBufferTexture, Description.BackBufferFormat.IsSRgb());
        }
        public void UpdateBufferSize(Renderer renderer)
        {
            Rectangle rect = new Rectangle();

            Win32Helper.GetWindowRect(_hWnd, out rect);
            int newWidth  = System.Math.Max(1, rect.Width - rect.Left);
            int newHeight = System.Math.Max(1, rect.Height - rect.Top);

            SwapChain swapChain = _swapChain.Get(renderer);

            if (swapChain == null)
            {
                _width  = newWidth;
                _height = newHeight;
            }
            else if (newWidth != this.Width || newHeight != this.Height)
            {
                _width  = newWidth;
                _height = newHeight;

                _renderTargetView.Release(renderer);

                Logger.LogInfo(this, string.Format("Resizing SwapChain to {0}x{1}", _width, _height));

                swapChain.ResizeBuffers(1, newWidth, newHeight, Format.R8G8B8A8_UNorm, SwapChainFlags.AllowModeSwitch);
                using (var resource = SharpDX.Direct3D11.Resource.FromSwapChain <SharpDX.Direct3D11.Texture2D>(swapChain, 0))
                {
                    _renderTargetView.Set(renderer, new RenderTargetView(renderer.Device, resource), Width * Height * 4);
                }
            }
        }
Пример #7
0
        void Resize(Common.Size ReqSize)
        {
            var            w    = (int)d2dRenderTarget.Size.Width;
            var            h    = (int)d2dRenderTarget.Size.Height;
            var            nbb  = new Texture2D(device, backBuffer.Description);
            ResourceRegion rrgn = new ResourceRegion()
            {
                Front = 0, Back = 1, Top = 0, Left = 0, Right = w, Bottom = h
            };

            device.CopySubresourceRegion(backBuffer, 0, rrgn, nbb, 0, 0, 0, 0);

            d2dRenderTarget.Dispose();
            renderView.Dispose();
            surface.Dispose();
            backBuffer.Dispose();

            swapchain.ResizeBuffers(0, (int)ReqSize.width, (int)ReqSize.height, Format.B8G8R8A8_UNorm, SwapChainFlags.None);
            backBuffer      = Texture2D.FromSwapChain <Texture2D>(swapchain, 0);
            renderView      = new RenderTargetView(device, backBuffer);
            surface         = backBuffer.QueryInterface <Surface1>();
            d2dRenderTarget = new RenderTarget(d2dFactory, surface, new RenderTargetProperties(new PixelFormat(Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied)));

            device.CopySubresourceRegion(nbb, 0, rrgn, backBuffer, 0, 0, 0, 0);
            nbb.Dispose();
        }
        protected override void ResizeBackBuffer(int width, int height, PixelFormat format)
        {
            // Manually update back buffer texture
            backBuffer.OnDestroyed();

#if SILICONSTUDIO_PLATFORM_WINDOWS_RUNTIME
            var swapChainPanel = Description.DeviceWindowHandle.NativeHandle as Windows.UI.Xaml.Controls.SwapChainPanel;
            if (swapChainPanel != null)
            {
                var swapChain2 = swapChain.QueryInterface <SwapChain2>();
                if (swapChain2 != null)
                {
                    swapChain2.MatrixTransform = Matrix3x2.Scaling(1f / swapChainPanel.CompositionScaleX, 1f / swapChainPanel.CompositionScaleY);
                    swapChain2.Dispose();
                }
            }
#endif

            swapChain.ResizeBuffers(bufferCount, width, height, (SharpDX.DXGI.Format)format, SwapChainFlags.None);

            // Get newly created native texture
            var backBufferTexture = swapChain.GetBackBuffer <SharpDX.Direct3D11.Texture2D>(0);

            // Put it in our back buffer texture
            backBuffer.InitializeFrom(backBufferTexture);
        }
Пример #9
0
        public override bool Resize(int width, int height, Format format, Rational?refreshRate = null)
        {
            if (!base.Resize(width, height, format, refreshRate))
            {
                return(false);
            }

            RemoveAndDispose(ref backBuffer);

#if DIRECTX11_2 && (WIN8METRO || WINDOWS_UWP)
            var swapChainPanel = Description.DeviceWindowHandle as SwapChainPanel;
            if (swapChainPanel != null && swapChain2 != null)
            {
                swapChain2.MatrixTransform = Matrix3x2.Scaling(1f / swapChainPanel.CompositionScaleX, 1f / swapChainPanel.CompositionScaleY);
            }
#endif

            swapChain.ResizeBuffers(bufferCount, width, height, format, Description.Flags);

            // Recreate the back buffer
            backBuffer = ToDispose(RenderTarget2D.New(GraphicsDevice, swapChain.GetBackBuffer <Direct3D11.Texture2D>(0)));

            // Reinit the Viewport
            DefaultViewport = new ViewportF(0, 0, backBuffer.Width, backBuffer.Height);

            return(true);
        }
Пример #10
0
        //============================================================
        public void ConfigureBackBuffer(int width, int height)
        {
            _size.Set(width, height);
            // 释放渲染目标
            if (null != _nativeTarget)
            {
                _nativeTarget.Dispose();
                _nativeTarget = null;
            }
            if (null != _nativeDepth)
            {
                _nativeDepth.Dispose();
                _nativeDepth = null;
            }
            // 改变缓冲大小
            SwapChainDescription description = _nativeSwap.Description;

            _nativeSwap.ResizeBuffers(description.BufferCount, width, height, Format.R8G8B8A8_UNorm, SwapChainFlags.None);
            // 重新创建渲染目标
            CreatePrimaryRenderTarget();
            CreateDepthBuffer();
            // 设置视角
            _nativeViewport        = new Viewport(0, 0, width, height, 0.0f, 1.0f);
            _target.NativeViewport = _nativeViewport;
            _nativeTargetActive    = _nativeTarget;
            _nativeDevice.OutputMerger.SetTargets(_nativeDepth, _nativeTargetActive);
            _nativeDevice.Rasterizer.SetViewports(_nativeViewport);
        }
Пример #11
0
        public void Resize(int w, int h)
        {
            ReleaseRTV();
            var desc = m_swapChain.Description;

            m_swapChain.ResizeBuffers(desc.BufferCount, w, h, desc.ModeDescription.Format, desc.Flags);
        }
Пример #12
0
        /// <summary>
        /// Rebuilds the swap chain and render targets.
        /// </summary>
        protected void UpdatePresenter()
        {
            PresenterReady = false;

            // release our buffers since we are updating the swap chain
            RenderTarget2D.Dispose();
            DxgiSurface.Dispose();
            RenderTargetView.Dispose();
            _backBuffer.Dispose();

            // resize swapchain
            _swapChain.ResizeBuffers(1, Size.Width, Size.Height, Format.R8G8B8A8_UNorm, SwapChainFlags.None);

            // recreate the buffers
            _backBuffer                  = Resource.FromSwapChain <Texture2D>(_swapChain, 0);
            RenderTargetView             = new RenderTargetView(_device, _backBuffer);
            DxgiSurface                  = _backBuffer.QueryInterface <Surface>();
            RenderTarget2D               = new RenderTarget(Factory2D, DxgiSurface, new RenderTargetProperties(new PixelFormat(Format.Unknown, AlphaMode.Premultiplied)));
            RenderTarget2D.AntialiasMode = AntialiasMode;
            PresenterReady               = true;

            Brushes.Cleanup();
            Fonts.Cleanup();
            IsDirty = true;
            foreach (IRenderComponent component in Layers)
            {
                component.PresenterReset();
            }
        }
Пример #13
0
        /// <summary>
        /// Render the frame
        /// </summary>
        protected void RenderScene()
        {
            if (needsResizing)
            {
                needsResizing = false;
                renderTargetView.Dispose();
                SwapChainDescription sd = swapChain.Description;
                swapChain.ResizeBuffers(sd.BufferCount, (uint)host.ActualWidth, (uint)host.ActualHeight, sd.BufferDescription.Format, sd.Options);
                SetViews();
                // Update the projection matrix
                InitMatrices();
            }
            t = (Environment.TickCount - dwTimeStart) / 1000.0f;

            //WPF transforms used here use degrees as opposed to D3DX which uses radians in the native tutorial
            //360 degrees == 2 * Math.PI
            //world matrix rotates the first cube by t degrees
            RotateTransform3D rt1 = new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(0, 1, 0), t * 60));

            // Clear the backbuffer
            device.ClearRenderTargetView(renderTargetView, backColor);

            // Clear the depth buffer to 1.0 (max depth)
            device.ClearDepthStencilView(depthStencilView, ClearOptions.Depth, 1.0f, (byte)0);

            mesh.Render(rt1.Value.ToMatrix4x4F());

            Microsoft.WindowsAPICodePack.DirectX.ErrorCode error;
            swapChain.TryPresent(1, PresentOptions.None, out error);
        }
Пример #14
0
        public virtual void OnResize()
        {
            Debug.Assert(ImmediateContext != null);
            Debug.Assert(Device != null);
            Debug.Assert(SwapChain != null);
            Util.ReleaseCom(ref RenderTargetView);
            Util.ReleaseCom(ref DepthStencilView);
            Util.ReleaseCom(ref DepthStencilBuffer);
            SwapChain.ResizeBuffers(1, ClientWidth, ClientHeight, DXGI.Format.R8G8B8A8_UNorm, DXGI.SwapChainFlags.None);
            using (var resource = D3D11.Resource.FromSwapChain <D3D11.Texture2D>(SwapChain, 0))
            {
                RenderTargetView = new D3D11.RenderTargetView(Device, resource);
            }
            var depthStencilDesc = new D3D11.Texture2DDescription()
            {
                Width             = ClientWidth,
                Height            = ClientHeight,
                MipLevels         = 1,
                ArraySize         = 1,
                Format            = DXGI.Format.D24_UNorm_S8_UInt,
                SampleDescription = (Enable4XMsaa) ? new DXGI.SampleDescription(4, Msaa4XQuality - 1) : new DXGI.SampleDescription(1, 0),
                Usage             = D3D11.ResourceUsage.Default,
                BindFlags         = D3D11.BindFlags.DepthStencil,
                CpuAccessFlags    = D3D11.CpuAccessFlags.None,
                OptionFlags       = D3D11.ResourceOptionFlags.None
            };

            DepthStencilBuffer = new D3D11.Texture2D(Device, depthStencilDesc);
            DepthStencilView   = new D3D11.DepthStencilView(Device, DepthStencilBuffer);
            ImmediateContext.OutputMerger.SetTargets(DepthStencilView, RenderTargetView);
            Viewport = new Viewport(0, 0, ClientWidth, ClientHeight, 0.0f, 1.0f);
            ImmediateContext.Rasterizer.SetViewport(Viewport);
        }
Пример #15
0
        public unsafe void ChangeResolution(int width, int height)
        {
            _width  = width;
            _height = height;

            //Clear the current render target
            DeviceContext.OMSetRenderTargets(_context, 0, null, IntPtr.Zero);

            //Release all target objects, including _defaultRenderTarget
            ReleaseRenderTargets?.Invoke();

            //Release the default render view (containing reference to the back buffer)
            NativeHelper.Dispose(ref _defaultRenderView);

            //Resize swapchain
            SwapChain.ResizeBuffers(_swapchain, 1, (uint)width, (uint)height, 28 /*R8G8B8A8_UNorm*/, 0).Check();

            //Get the new back buffer and create default view
            RebuildBackBuffer();

            //Rebuild all target objects
            RebuildRenderTargets?.Invoke();

            //Apply current RenderTarget
            CurrentTarget.Apply();

            //Invoke external event
            ResolutionChanged?.Invoke(this, EventArgs.Empty);
        }
Пример #16
0
        private SwapChain CreateSwapChainForDesktop(IntPtr handle)
        {
            bufferCount = 1;
            var description = new SwapChainDescription
            {
                ModeDescription   = new ModeDescription(Description.BackBufferWidth, Description.BackBufferHeight, Description.RefreshRate.ToSharpDX(), (SharpDX.DXGI.Format)Description.BackBufferFormat),
                BufferCount       = bufferCount, // TODO: Do we really need this to be configurable by the user?
                OutputHandle      = handle,
                SampleDescription = new SampleDescription((int)Description.MultiSampleCount, 0),
                SwapEffect        = SwapEffect.Discard,
                Usage             = SharpDX.DXGI.Usage.BackBuffer | SharpDX.DXGI.Usage.RenderTargetOutput,
                IsWindowed        = true,
                Flags             = Description.IsFullScreen ? SwapChainFlags.AllowModeSwitch : SwapChainFlags.None,
            };

            var newSwapChain = new SwapChain(GraphicsAdapterFactory.NativeFactory, GraphicsDevice.NativeDevice, description);

            if (Description.IsFullScreen)
            {
                // Before fullscreen switch
                newSwapChain.ResizeTarget(ref description.ModeDescription);

                // Switch to full screen
                newSwapChain.IsFullScreen = true;

                // This is really important to call ResizeBuffers AFTER switching to IsFullScreen
                newSwapChain.ResizeBuffers(bufferCount, Description.BackBufferWidth, Description.BackBufferHeight, (SharpDX.DXGI.Format)Description.BackBufferFormat, SwapChainFlags.AllowModeSwitch);
            }

            return(newSwapChain);
        }
Пример #17
0
        private void CustomSwapChainPanel_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            lock (d2d1DC)
            {
                Utilities.Dispose(ref d2d1DC);
                Utilities.Dispose(ref backBufferTexture);

                try
                {
                    swapChain.ResizeBuffers(0, (int)ActualWidth, (int)ActualHeight, Format.Unknown, SwapChainFlags.None);
                }
                catch (SharpDXException ex)
                {
                    Debug.WriteLine(ex.ToString());
                }

                backBufferTexture = SharpDX.Direct3D11.Resource.FromSwapChain <SharpDX.Direct3D11.Texture2D>(swapChain, 0);

                using (var surface = backBufferTexture.QueryInterface <Surface2>())
                {
                    d2d1DC = new DeviceContext(surface);
                }
            }

            AdjustFontSize();
            DrawPTT();
        }
Пример #18
0
        public D3DRenderTarget ResizeSwapChain(SwapChain swapChain, int width, int height)
        {
            swapChain.ResizeBuffers(BackBufferCount, width, height, Format.Unknown, SwapChainFlags.None);
            var texture = Texture2D.FromSwapChain <Texture2D>(swapChain, 0);
            var rtv     = new RenderTargetView(device, texture);

            return(new D3DRenderTarget(texture, rtv, null, null));
        }
Пример #19
0
 private static void RebuildBackBuffer(RenderForm form, Device device, ref RenderTargetView rtv, ref Texture2D buffer, ref SwapChain swapChain)
 {
     rtv.Dispose();
     buffer.Dispose();
     swapChain.ResizeBuffers(3, form.ClientSize.Width, form.ClientSize.Height, Format.Unknown, SwapChainFlags.AllowModeSwitch);
     buffer = Texture2D.FromSwapChain <Texture2D>(swapChain, 0);
     rtv    = new RenderTargetView(device, buffer);
 }
Пример #20
0
        // end of init... and whatever is required to handel window resize
        public void HandleResize(int viewWidth, int viewHeight, DXConfigClass DXConfig)
        {
            // happens when window is minimized
            if (viewWidth * viewHeight == 0)
            {
                return;
            }

            _viewWidth  = viewWidth;
            _viewHeight = viewHeight;

            // dispose whatever needs to be disposed first
            if (_renderTargetView != null)
            {
                _renderTargetView.Dispose();
            }
            if (_depthStencilBuffer != null)
            {
                _depthStencilBuffer.Dispose();
            }
            if (_depthStencilView != null)
            {
                _depthStencilView.Dispose();
            }

            // Resize the backbuffer
            _swapChain.ResizeBuffers(1, viewWidth, viewHeight, Format.R8G8B8A8_UNorm, SwapChainFlags.None);

            // Get the backbuffer from the swapchain
            var backBuffer = Texture2D.FromSwapChain <Texture2D>(_swapChain, 0);

            // RenderTargetView on the backbuffer
            _renderTargetView           = new RenderTargetView(_device, backBuffer);
            _renderTargetView.DebugName = "MainRenderView";

            backBuffer.Dispose();

            // Create the depth buffer
            _depthStencilBuffer = new Texture2D(_device, new Texture2DDescription()
            {
                Format            = Format.D32_Float,
                ArraySize         = 1,
                MipLevels         = 1,
                Width             = viewWidth,
                Height            = viewHeight,
                SampleDescription = new SampleDescription(DXConfig.MSAA_SampleCount, DXConfig.MSAA_SampleDesc),
                Usage             = ResourceUsage.Default,
                BindFlags         = BindFlags.DepthStencil,
                CpuAccessFlags    = CpuAccessFlags.None,
                OptionFlags       = ResourceOptionFlags.None
            });
            _depthStencilBuffer.DebugName = "MainDepthBuffer";

            _depthStencilView = new DepthStencilView(_device, _depthStencilBuffer);

            _context.OutputMerger.SetTargets(_depthStencilView, _renderTargetView);
            _context.Rasterizer.SetViewport(new Viewport(0, 0, viewWidth, viewHeight, 0.0f, 1.0f));
        }
Пример #21
0
 protected override Texture2D CreateRenderTarget(int width, int height)
 {
     if (_IsResizing)
     {
         _SwapChain.ResizeBuffers(1, width, height, Format.Unknown, SwapChainFlags.None);
     }
     _IsResizing = false;
     return(Texture2D.FromSwapChain <Texture2D>(_SwapChain, 0));
 }
Пример #22
0
        protected void Resize()
        {
            var width  = Width;
            var height = Height;

            if (width == 0 || height == 0)
            {
                return;
            }

            DisposeHelper.Dispose(ref _renderBuffer);
            DisposeHelper.Dispose(ref _renderView);
            DisposeHelper.Dispose(ref _depthBuffer);
            DisposeHelper.Dispose(ref _depthView);

            if (_swapChain != null)
            {
                _swapChain.ResizeBuffers(_swapChainDescription.BufferCount, ActualWidth, ActualHeight,
                                         Format.Unknown, SwapChainFlags.None);
                _renderBuffer = Resource.FromSwapChain <Texture2D>(_swapChain, 0);
            }
            else
            {
                _renderBuffer = new Texture2D(Device, new Texture2DDescription {
                    Width             = ActualWidth,
                    Height            = ActualHeight,
                    MipLevels         = 1,
                    ArraySize         = 1,
                    Format            = WpfMode ? Format.B8G8R8A8_UNorm : Format.R8G8B8A8_UNorm,
                    SampleDescription = SampleDescription,
                    Usage             = ResourceUsage.Default,
                    BindFlags         = BindFlags.ShaderResource | BindFlags.RenderTarget,
                    CpuAccessFlags    = CpuAccessFlags.None,
                    OptionFlags       = WpfMode ? ResourceOptionFlags.Shared : ResourceOptionFlags.None
                });
            }

            _renderView = new RenderTargetView(Device, _renderBuffer);

            var depth = CreateDepthBuffer(width, height);

            _depthBuffer = depth?.Item1;
            _depthView   = depth?.Item2;

            DeviceContext.Rasterizer.SetViewports(OutputViewport);
            Sprite?.RefreshViewport();

            DeviceContext.Rasterizer.SetViewports(Viewport);

            ResetTargets();
            DeviceContext.OutputMerger.DepthStencilState = null;

            ResizeInner();
            DeviceContextHolder.OnResize(width, height);

            InitiallyResized = true;
        }
Пример #23
0
 private void ClientResize(object sender, EventArgs args)
 {
     RenderTarget.Dispose();
     DepthStencil.Dispose();
     System.Drawing.Size size = Client.ClientSize;
     SwapChain.ResizeBuffers(1, size.Width, size.Height, Format.R8G8B8A8_UNorm, SwapChainFlags.None);
     initRenderTarget();
     initDepthStencil();
 }
Пример #24
0
        public void ResizeRenderTargets(int width, int height)
        {
            RenderTargetView.Dispose();
            DepthStencilView.Dispose();

            SwapChain.ResizeBuffers(3, 0, 0, Format.R8G8B8A8_UNorm, SwapChainFlags.AllowModeSwitch);
            CreateRenderTargets(width, height);
            Device.ImmediateContext.OutputMerger.SetTargets(DepthStencilView, RenderTargetView);
        }
Пример #25
0
        private void Resize()
        {
            // Dispose all previous allocated resources
            Canvas.Release();
            Utilities.Dispose(ref backBufferView);
            Utilities.Dispose(ref depthStencilView);


            if (View.ClientSize.Width == 0 || View.ClientSize.Height == 0)
            {
                return;
            }

            // Resize the backbuffer
            swapChain.ResizeBuffers(1, View.ClientSize.Width, View.ClientSize.Height, Format.B8G8R8A8_UNorm, SwapChainFlags.None);

            // Get the backbuffer from the swapchain
            var backBufferTexture = swapChain.GetBackBuffer <Texture2D11>(0);

            //update font
            Canvas.UpdateResources(backBufferTexture);

            // Backbuffer
            backBufferView = new RenderTargetView(NativeDevice, backBufferTexture);
            backBufferTexture.Dispose();

            // Depth buffer

            var depthStencilTexture = new Texture2D11(NativeDevice, new Texture2DDescription()
            {
                Format            = Format.D16_UNorm,
                ArraySize         = 1,
                MipLevels         = 1,
                Width             = View.ClientSize.Width,
                Height            = View.ClientSize.Height,
                SampleDescription = new SampleDescription(1, 0),
                Usage             = ResourceUsage.Default,
                BindFlags         = BindFlags.DepthStencil,
                CpuAccessFlags    = CpuAccessFlags.None,
                OptionFlags       = ResourceOptionFlags.None
            });


            // Create the depth buffer view
            depthStencilView = new DepthStencilView(NativeDevice, depthStencilTexture);
            depthStencilTexture.Dispose();

            //SetDefaultTargers();

            // Set Default Targets
            NativeDeviceContext.Rasterizer.SetViewport(0, 0, View.ClientSize.Width, View.ClientSize.Height);
            NativeDeviceContext.OutputMerger.SetTargets(depthStencilView, backBufferView);

            // End resize
            //MustResize = false;
        }
    private void OnUserResized(object sender, EventArgs eventArgs)
    {
        backBufferView.Dispose();

        SwapChainDescription currentDesc = swapChain.Description;

        swapChain.ResizeBuffers(currentDesc.BufferCount, form.ClientSize.Width, form.ClientSize.Height, currentDesc.ModeDescription.Format, currentDesc.Flags);

        SetupBackbufferAndViewport();
    }
Пример #27
0
        void Resize(Size size)
        {
            Utilities.Dispose(ref _d2dRenderTarget);
            Utilities.Dispose(ref backBuffer);
            Utilities.Dispose(ref renderView);
            Utilities.Dispose(ref depthBuffer);
            Utilities.Dispose(ref depthView);
            var context = _d3dDevice.ImmediateContext;

            // Resize the backbuffer
            _swapChain.ResizeBuffers(_desc.BufferCount, (int)size.Width, (int)size.Height, Format.Unknown, SwapChainFlags.None);

            // Get the backbuffer from the swapchain
            backBuffer = Texture2D.FromSwapChain <Texture2D>(_swapChain, 0);

            // Renderview on the backbuffer
            renderView = new RenderTargetView(_d3dDevice, backBuffer);

            // Create the depth buffer
            depthBuffer = new Texture2D(_d3dDevice, new Texture2DDescription()
            {
                Format            = Format.D32_Float_S8X24_UInt,
                ArraySize         = 1,
                MipLevels         = 1,
                Width             = (int)size.Width,
                Height            = (int)size.Height,
                SampleDescription = new SampleDescription(1, 0),
                Usage             = ResourceUsage.Default,
                BindFlags         = BindFlags.DepthStencil,
                CpuAccessFlags    = CpuAccessFlags.None,
                OptionFlags       = ResourceOptionFlags.None
            });

            // Create the depth buffer view
            depthView = new DepthStencilView(_d3dDevice, depthBuffer);

            // Setup targets and viewport for rendering
            context.Rasterizer.SetViewport(new Viewport(0, 0, (int)size.Width, (int)size.Height, 0.0f, 1.0f));
            context.OutputMerger.SetTargets(depthView, renderView);

            // Setup new projection matrix with correct aspect ratio
            _proj = Matrix.PerspectiveFovLH((float)Math.PI / 4.0f, (float)(size.Width / size.Height), 0.1f, 100.0f);

            using (var dxgiBackBuffer = _swapChain.GetBackBuffer <Surface>(0))
            {
                _d2dRenderTarget = new RenderTarget(AvaloniaLocator.Current.GetService <SharpDX.Direct2D1.Factory>()
                                                    , dxgiBackBuffer, new RenderTargetProperties
                {
                    DpiX        = 96,
                    DpiY        = 96,
                    Type        = RenderTargetType.Default,
                    PixelFormat = new PixelFormat(Format.Unknown, AlphaMode.Premultiplied)
                });
            }
        }
Пример #28
0
        public void Resize(int width, int height)
        {
            if (m_swapChain == null)
            {
                return;
            }
            var desc = m_swapChain.Description;

            m_swapChain.ResizeBuffers(desc.BufferCount, width, height,
                                      desc.ModeDescription.Format, desc.Flags);
        }
Пример #29
0
 protected override void OnSizeChanged(EventArgs e)
 {
     if (renderTargetView != null)
     {
         renderTargetView.Dispose();
         SwapChainDescription sd = swapChain.Description;
         swapChain.ResizeBuffers(sd.BufferCount, (uint)this.ClientSize.Width, (uint)this.ClientSize.Height, sd.BufferDescription.Format, sd.Options);
         SetViews();
         Invalidate();
     }
     base.OnSizeChanged(e);
 }
Пример #30
0
        private void ResizeBackBuffer()
        {
            // Release the old views, as they hold references to the buffers we
            // will be destroying.  Also release the old depth/stencil buffer

            if (_renderTargetView != null)
            {
                _renderTargetView.Dispose();
            }

            if (_depthStencilView != null)
            {
                _depthStencilView.Dispose();
            }

            if (_depthStencilBuffer != null)
            {
                _depthStencilBuffer.Dispose();
            }

            _swapChain.ResizeBuffers(1, ClientSize.Width, ClientSize.Height, Format.R8G8B8A8_UNorm, SwapChainFlags.None);

            Texture2D backBuffer = Texture2D.FromSwapChain <Texture2D>(_swapChain, 0);

            _renderTargetView = new RenderTargetView(_d3dDevice, backBuffer);
            backBuffer.Dispose();

            // Create the depth/stencil buffer and view
            Texture2DDescription depthStencilDesc = new Texture2DDescription
            {
                Width             = ClientSize.Width,
                Height            = ClientSize.Height,
                MipLevels         = 1,
                ArraySize         = 1,
                Format            = Format.D24_UNorm_S8_UInt,
                SampleDescription = new SampleDescription(1, 0),
                Usage             = ResourceUsage.Default,
                BindFlags         = BindFlags.DepthStencil,
                CpuAccessFlags    = 0,
                OptionFlags       = ResourceOptionFlags.None
            };

            _depthStencilBuffer = new Texture2D(_d3dDevice, depthStencilDesc);
            _depthStencilView   = new DepthStencilView(_d3dDevice, _depthStencilBuffer);

            // Bind the render target view and depth/stencil view to the pipeline
            _d3dDevice.OutputMerger.SetTargets(_depthStencilView, _renderTargetView);

            // Set the viewport transform
            Viewport vp = new Viewport(0, 0, ClientSize.Width, ClientSize.Height, 0.0f, 1.0f);

            _d3dDevice.Rasterizer.SetViewports(vp);
        }
        private SwapChain CreateSwapChainForDesktop(IntPtr handle)
        {
            bufferCount = 1;
            var description = new SwapChainDescription
                {
                    ModeDescription = new ModeDescription(Description.BackBufferWidth, Description.BackBufferHeight, Description.RefreshRate.ToSharpDX(), (SharpDX.DXGI.Format)Description.BackBufferFormat), 
                    BufferCount = bufferCount, // TODO: Do we really need this to be configurable by the user?
                    OutputHandle = handle,
                    SampleDescription = new SampleDescription((int)Description.MultiSampleCount, 0), 
                    SwapEffect = SwapEffect.Discard,
                    Usage = SharpDX.DXGI.Usage.BackBuffer | SharpDX.DXGI.Usage.RenderTargetOutput,
                    IsWindowed = true,
                    Flags = Description.IsFullScreen ? SwapChainFlags.AllowModeSwitch : SwapChainFlags.None, 
                };

            var newSwapChain = new SwapChain(GraphicsAdapterFactory.NativeFactory, GraphicsDevice.NativeDevice, description);
            if (Description.IsFullScreen)
            {
                // Before fullscreen switch
                newSwapChain.ResizeTarget(ref description.ModeDescription);

                // Switch to full screen
                newSwapChain.IsFullScreen = true;

                // This is really important to call ResizeBuffers AFTER switching to IsFullScreen 
                newSwapChain.ResizeBuffers(bufferCount, Description.BackBufferWidth, Description.BackBufferHeight, (SharpDX.DXGI.Format)Description.BackBufferFormat, SwapChainFlags.AllowModeSwitch);
            }

            return newSwapChain;
        }
Пример #32
0
        public void Resize(Device device, SwapChain swapChain, DeviceContext context)
        {
            renderTarget.Dispose();
            //DepthStencilView.Dispose();

            swapChain.ResizeBuffers(2, 0, 0, Format.R8G8B8A8_UNorm, SwapChainFlags.AllowModeSwitch);
            using (var resource = Resource.FromSwapChain<Texture2D>(swapChain, 0))
                renderTarget = new RenderTargetView(device, resource);

            context.OutputMerger.SetTargets(renderTarget);
        }
        private SwapChain CreateSwapChainForDesktop(IntPtr handle)
        {
            bufferCount = 1;
            var backbufferFormat = Description.BackBufferFormat;
#if SILICONSTUDIO_XENKO_GRAPHICS_API_DIRECT3D12
            // TODO D3D12 (check if this setting make sense on D3D11 too?)
            backbufferFormat = backbufferFormat.ToNonSRgb();
            // TODO D3D12 Can we make it work with something else after?
            bufferCount = 2;
#endif
            var description = new SwapChainDescription
                {
                    ModeDescription = new ModeDescription(Description.BackBufferWidth, Description.BackBufferHeight, Description.RefreshRate.ToSharpDX(), (SharpDX.DXGI.Format)backbufferFormat), 
                    BufferCount = bufferCount, // TODO: Do we really need this to be configurable by the user?
                    OutputHandle = handle,
                    SampleDescription = new SampleDescription((int)Description.MultiSampleLevel, 0),
#if SILICONSTUDIO_XENKO_GRAPHICS_API_DIRECT3D11
                    SwapEffect = SwapEffect.Discard,
#elif SILICONSTUDIO_XENKO_GRAPHICS_API_DIRECT3D12
                    SwapEffect = SwapEffect.FlipDiscard,
#endif
                    Usage = SharpDX.DXGI.Usage.BackBuffer | SharpDX.DXGI.Usage.RenderTargetOutput,
                    IsWindowed = true,
                    Flags = Description.IsFullScreen ? SwapChainFlags.AllowModeSwitch : SwapChainFlags.None, 
                };

#if SILICONSTUDIO_XENKO_GRAPHICS_API_DIRECT3D11
            var newSwapChain = new SwapChain(GraphicsAdapterFactory.NativeFactory, GraphicsDevice.NativeDevice, description);
#elif SILICONSTUDIO_XENKO_GRAPHICS_API_DIRECT3D12
            var newSwapChain = new SwapChain(GraphicsAdapterFactory.NativeFactory, GraphicsDevice.NativeCommandQueue, description);
#endif

            //prevent normal alt-tab
            GraphicsAdapterFactory.NativeFactory.MakeWindowAssociation(handle, WindowAssociationFlags.IgnoreAltEnter);

            if (Description.IsFullScreen)
            {
                // Before fullscreen switch
                newSwapChain.ResizeTarget(ref description.ModeDescription);

                // Switch to full screen
                newSwapChain.IsFullScreen = true;

                // This is really important to call ResizeBuffers AFTER switching to IsFullScreen 
                newSwapChain.ResizeBuffers(bufferCount, Description.BackBufferWidth, Description.BackBufferHeight, (SharpDX.DXGI.Format)Description.BackBufferFormat, SwapChainFlags.AllowModeSwitch);
            }

            return newSwapChain;
        }
        private SwapChain CreateSwapChainForDesktop()
        {
            var control = Description.DeviceWindowHandle.NativeHandle as Control;
            if (control == null)
            {
                throw new NotSupportedException(string.Format("Form of type [{0}] is not supported. Only System.Windows.Control are supported", Description.DeviceWindowHandle != null ? Description.DeviceWindowHandle.GetType().Name : "null"));
            }

            bufferCount = 1;
            var description = new SwapChainDescription
                {
                    ModeDescription = new ModeDescription(Description.BackBufferWidth, Description.BackBufferHeight, Description.RefreshRate.ToSharpDX(), (SharpDX.DXGI.Format)Description.BackBufferFormat), 
                    BufferCount = bufferCount, // TODO: Do we really need this to be configurable by the user?
                    OutputHandle = control.Handle, 
                    SampleDescription = new SampleDescription((int)Description.MultiSampleCount, 0), 
                    SwapEffect = SwapEffect.Discard,
                    Usage = SharpDX.DXGI.Usage.BackBuffer | SharpDX.DXGI.Usage.RenderTargetOutput,
                    IsWindowed = true,
                    Flags = Description.IsFullScreen ? SwapChainFlags.AllowModeSwitch : SwapChainFlags.None, 
                };

            var newSwapChain = new SwapChain(GraphicsAdapterFactory.NativeFactory, GraphicsDevice.NativeDevice, description);
            if (Description.IsFullScreen)
            {
                // Before fullscreen switch
                newSwapChain.ResizeTarget(ref description.ModeDescription);

                // Switch to full screen
                newSwapChain.IsFullScreen = true;

                // This is really important to call ResizeBuffers AFTER switching to IsFullScreen 
                newSwapChain.ResizeBuffers(bufferCount, Description.BackBufferWidth, Description.BackBufferHeight, (SharpDX.DXGI.Format)Description.BackBufferFormat, SwapChainFlags.AllowModeSwitch);
            }

            return newSwapChain;
        }
Пример #35
0
        private SwapChain CreateSwapChainForDesktop()
        {
            IntPtr? handle = null;
            var control = Description.DeviceWindowHandle as Control;
            if (control != null) handle = control.Handle;
            else if (Description.DeviceWindowHandle is IntPtr) handle = (IntPtr)Description.DeviceWindowHandle;

            if (!handle.HasValue)
            {
                throw new NotSupportedException(string.Format("DeviceWindowHandle of type [{0}] is not supported. Only System.Windows.Control or IntPtr are supported", Description.DeviceWindowHandle != null ? Description.DeviceWindowHandle.GetType().Name : "null"));
            }

            bufferCount = 1;
            var description = new SwapChainDescription
                {
                    ModeDescription = new ModeDescription(Description.BackBufferWidth, Description.BackBufferHeight, Description.RefreshRate, Description.BackBufferFormat),
                    BufferCount = bufferCount, // TODO: Do we really need this to be configurable by the user?
                    OutputHandle = handle.Value,
                    SampleDescription = new SampleDescription((int)Description.MultiSampleCount, 0),
                    SwapEffect = SwapEffect.Discard,
                    Usage = Description.RenderTargetUsage,
                    IsWindowed = true,
                    Flags = Description.Flags,
                };

            var newSwapChain = new SwapChain(GraphicsAdapter.Factory, (Direct3D11.Device)GraphicsDevice, description);
            if (Description.IsFullScreen)
            {
                // Before fullscreen switch
                newSwapChain.ResizeTarget(ref description.ModeDescription);

                // Switch to full screen
                newSwapChain.IsFullScreen = true;

                // This is really important to call ResizeBuffers AFTER switching to IsFullScreen 
                newSwapChain.ResizeBuffers(bufferCount, Description.BackBufferWidth, Description.BackBufferHeight, Description.BackBufferFormat, SwapChainFlags.AllowModeSwitch);
            }

            return newSwapChain;
        }