Пример #1
0
        private void _updateDimensions(double width, double height)
        {
            _appWidth  = (float)width + 5;
            _appHeight = (float)height;

            if (_deviceManager == null)
            {
                return;
            }
            if (_stagingTexture2D != null)
            {
                _stagingTexture2D.Dispose();
            }
            if (_stagingBitmap != null)
            {
                _stagingBitmap.Dispose();
            }
            if (_stagingBitmapSourceEffect != null)
            {
                _stagingBitmapSourceEffect.Dispose();
            }

            _stagingTexture2D          = AllocateTextureReturnSurface((int)_appWidth, (int)_appHeight);
            _stagingBitmap             = new SharpDX.Direct2D1.Bitmap1(_deviceManager.ContextDirect2D, _stagingTexture2D.QueryInterface <SharpDX.DXGI.Surface>());
            _stagingBitmapSourceEffect = new SharpDX.Direct2D1.Effects.BitmapSource(_deviceManager.ContextDirect2D);
        }
Пример #2
0
 public override void Resize(Size size)
 {
     this.deviceContext3.Target = null;
     this.swapChainBuffer.Dispose();
     this.targetBitmap.Dispose();
     this.swapChain.ResizeBuffers(1, 0, 0, SharpDX.DXGI.Format.B8G8R8A8_UNorm, SharpDX.DXGI.SwapChainFlags.None);
     this.swapChainBuffer       = SharpDX.DXGI.Surface.FromSwapChain(this.swapChain, 0);
     this.targetBitmap          = new SharpDX.Direct2D1.Bitmap1(this.deviceContext3, this.swapChainBuffer);
     this.deviceContext3.Target = targetBitmap;
 }
Пример #3
0
        public SharpDX.Direct2D1.Bitmap GetD2DBitmap(SharpDX.Direct2D1.DeviceContext dc)
        {
            BitmapSource src = Frame;

            // PixelFormat settings/conversion
            if (src.Format != System.Windows.Media.PixelFormats.Bgra32)
            {
                // Convert BitmapSource
                FormatConvertedBitmap fcb = new FormatConvertedBitmap();
                fcb.BeginInit();
                fcb.Source            = src;
                fcb.DestinationFormat = PixelFormats.Bgra32;
                fcb.EndInit();
                src = fcb;
            }

            if (src.PixelHeight > _maxImageSize || src.PixelWidth > _maxImageSize)
            {
                double            scale = (src.PixelWidth > src.PixelHeight) ? _maxImageSize / src.PixelWidth : _maxImageSize / src.PixelHeight;
                TransformedBitmap tb    = new TransformedBitmap();
                tb.BeginInit();
                tb.Source    = src;
                tb.Transform = new ScaleTransform(scale, scale);
                tb.EndInit();
                src = tb;
            }

            SharpDX.Direct2D1.Bitmap retval = null;
            GCHandle pinnedArray            = GCHandle.Alloc(null);

            try
            {
                int    stride     = src.PixelWidth * (src.Format.BitsPerPixel + 7) / 8;
                int    bufferSize = stride * src.PixelHeight;
                byte[] buffer     = new byte[bufferSize];
                src.CopyPixels(System.Windows.Int32Rect.Empty, buffer, stride, 0);
                pinnedArray = GCHandle.Alloc(buffer, GCHandleType.Pinned);
                using (SharpDX.DataStream datastream = new SharpDX.DataStream(pinnedArray.AddrOfPinnedObject(), bufferSize, true, false))
                {
                    var bmpProps1 = new SharpDX.Direct2D1.BitmapProperties1(dc.PixelFormat, dc.Factory.DesktopDpi.Width, dc.Factory.DesktopDpi.Height);
                    retval = new SharpDX.Direct2D1.Bitmap1(dc, new SharpDX.Size2(src.PixelWidth, src.PixelHeight), datastream, stride, bmpProps1);
                }
            }
            catch
            {
            }
            finally
            {
                if (pinnedArray.IsAllocated)
                {
                    pinnedArray.Free();
                }
            }
            return(retval);
        }
Пример #4
0
        /// <summary>
        /// Called from parent when it closes to ensure this asset closes properly and leaves
        /// not possible memory leaks
        /// </summary>
        public void Unload()
        {
            WindowLayoutService.OnWindowLayoutRaised -= WindowLayoutService_OnWindowLayoutRaised;

            _root       = null;
            _rootParent = null;

            _clearRenderTree();
            ClearAssets();

            _clock   = null;
            _gt      = null;
            _tweener = null;

            _deviceManager = null;
            _d2dContext    = null;

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

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

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

            _pathD2DConverter = null;



            //_graphicsDevice.Dispose();
            //_graphicsDevice = null;
        }
Пример #5
0
 public DUIDeviceContext3(IntPtr handle)
 {
     this.d3d11Device    = new SharpDX.Direct3D11.Device(SharpDX.Direct3D.DriverType.Hardware, SharpDX.Direct3D11.DeviceCreationFlags.BgraSupport);
     this.dxgiDevice     = d3d11Device.QueryInterface <SharpDX.Direct3D11.Device1>().QueryInterface <SharpDX.DXGI.Device>();
     this.d2dDevice3     = new SharpDX.Direct2D1.Device3(dxgiDevice);
     this.deviceContext3 = new SharpDX.Direct2D1.DeviceContext3(d2dDevice3, SharpDX.Direct2D1.DeviceContextOptions.None);
     // 创建 DXGI SwapChain。
     SharpDX.DXGI.SwapChainDescription swapChainDescription = new SharpDX.DXGI.SwapChainDescription()
     {
         BufferCount  = 1,
         Usage        = SharpDX.DXGI.Usage.RenderTargetOutput,
         OutputHandle = handle,
         IsWindowed   = true,
         // 这里宽度和高度都是 0,表示自动获取。
         ModeDescription   = new SharpDX.DXGI.ModeDescription(0, 0, new SharpDX.DXGI.Rational(60, 1), SharpDX.DXGI.Format.B8G8R8A8_UNorm),
         SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0),
         SwapEffect        = SharpDX.DXGI.SwapEffect.Discard
     };
     this.swapChain             = new SharpDX.DXGI.SwapChain(dxgiDevice.GetParent <SharpDX.DXGI.Adapter>().GetParent <SharpDX.DXGI.Factory>(), d3d11Device, swapChainDescription);
     this.swapChainBuffer       = SharpDX.DXGI.Surface.FromSwapChain(this.swapChain, 0);
     this.targetBitmap          = new SharpDX.Direct2D1.Bitmap1(this.deviceContext3, this.swapChainBuffer);
     this.deviceContext3.Target = targetBitmap;
 }
Пример #6
0
        void InitText(SwapChain3 tempSwapChain)
        {
            init       = true;
            device     = tempSwapChain.GetDevice <SharpDX.Direct3D11.Device1>();
            d3dContext = device.ImmediateContext.QueryInterface <SharpDX.Direct3D11.DeviceContext1>();
            var texture2d = tempSwapChain.GetBackBuffer <Texture2D>(0);

            SharpDX.DXGI.Device2  dxgiDevice2  = device.QueryInterface <SharpDX.DXGI.Device2>();
            SharpDX.DXGI.Adapter  dxgiAdapter  = dxgiDevice2.Adapter;
            SharpDX.DXGI.Factory2 dxgiFactory2 = dxgiAdapter.GetParent <SharpDX.DXGI.Factory2>();

            SharpDX.Direct2D1.Device d2dDevice = new SharpDX.Direct2D1.Device(dxgiDevice2);
            d2dContext = new SharpDX.Direct2D1.DeviceContext(d2dDevice, SharpDX.Direct2D1.DeviceContextOptions.None);

            SharpDX.Direct2D1.BitmapProperties1 properties = new SharpDX.Direct2D1.BitmapProperties1(
                new SharpDX.Direct2D1.PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm,
                                                  SharpDX.Direct2D1.AlphaMode.Premultiplied),
                96, 96, SharpDX.Direct2D1.BitmapOptions.Target | SharpDX.Direct2D1.BitmapOptions.CannotDraw);

            Surface backBuffer = tempSwapChain.GetBackBuffer <Surface>(0);

            d2dTarget = new SharpDX.Direct2D1.Bitmap1(d2dContext, new Size2(800, 600), properties);

            solidBrush = new SharpDX.Direct2D1.SolidColorBrush(d2dContext, Color.Coral);

            // Create a linear gradient brush.
            // Note that the StartPoint and EndPoint values are set as absolute coordinates of the surface you are drawing to,
            // NOT the geometry we will apply the brush.
            linearGradientBrush = new SharpDX.Direct2D1.LinearGradientBrush(d2dContext, new SharpDX.Direct2D1.LinearGradientBrushProperties()
            {
                StartPoint = new Vector2(50, 0),
                EndPoint   = new Vector2(450, 0),
            },
                                                                            new SharpDX.Direct2D1.GradientStopCollection(d2dContext, new SharpDX.Direct2D1.GradientStop[]
            {
                new SharpDX.Direct2D1.GradientStop()
                {
                    Color    = Color.Blue,
                    Position = 0,
                },
                new SharpDX.Direct2D1.GradientStop()
                {
                    Color    = Color.Green,
                    Position = 1,
                }
            }));

            SharpDX.Direct2D1.RadialGradientBrushProperties rgb = new SharpDX.Direct2D1.RadialGradientBrushProperties()
            {
                Center  = new Vector2(250, 525),
                RadiusX = 100,
                RadiusY = 100,
            };
            // Create a radial gradient brush.
            // The center is specified in absolute coordinates, too.
            radialGradientBrush = new SharpDX.Direct2D1.RadialGradientBrush(d2dContext, ref rgb
                                                                            ,
                                                                            new SharpDX.Direct2D1.GradientStopCollection(d2dContext, new SharpDX.Direct2D1.GradientStop[]
            {
                new SharpDX.Direct2D1.GradientStop()
                {
                    Color    = Color.Yellow,
                    Position = 0,
                },
                new SharpDX.Direct2D1.GradientStop()
                {
                    Color    = Color.Red,
                    Position = 1,
                }
            }));
        }
Пример #7
0
 /// <summary>
 /// Creates an <see cref="SharpDX.Direct2D1.BitmapBrush"/> from the specified bitmap.
 /// </summary>
 /// <param name="deviceContext">an instance of <see cref = "DeviceContext" /></param>
 /// <param name="bitmap">The bitmap contents of the new brush.</param>
 /// <param name="bitmapBrushProperties">The extend modes and interpolation mode of the new brush, or NULL. If this parameter is NULL, the brush defaults to the <see cref="SharpDX.Direct2D1.ExtendMode.Clamp"/> horizontal and vertical extend modes and the <see cref="SharpDX.Direct2D1.BitmapInterpolationMode.Linear"/> interpolation mode. </param>
 /// <param name="brushProperties">The opacity and transform of the new brush, or NULL. If this parameter is NULL, the brush defaults to an opacity of 1.0f and its transform is the identity matrix.</param>
 /// <unmanaged>HRESULT ID2D1DeviceContext::CreateBitmapBrush([In] ID2D1Bitmap* bitmap,[In, Optional] const D2D1_BITMAP_BRUSH_PROPERTIES1* bitmapBrushProperties,[In, Optional] const D2D1_BRUSH_PROPERTIES* brushProperties,[Out, Fast] ID2D1BitmapBrush1** bitmapBrush)</unmanaged>
 public BitmapBrush1(DeviceContext deviceContext, SharpDX.Direct2D1.Bitmap1 bitmap, SharpDX.Direct2D1.BitmapBrushProperties1?bitmapBrushProperties, SharpDX.Direct2D1.BrushProperties?brushProperties)
     : base(IntPtr.Zero)
 {
     deviceContext.CreateBitmapBrush(bitmap, bitmapBrushProperties, brushProperties, this);
 }
Пример #8
0
 /// <summary>
 /// Creates an <see cref="SharpDX.Direct2D1.BitmapBrush"/> from the specified bitmap.
 /// </summary>
 /// <param name="deviceContext">an instance of <see cref = "DeviceContext" /></param>
 /// <param name="bitmap">The bitmap contents of the new brush.</param>
 /// <param name="brushProperties">The opacity and transform of the new brush, or NULL. If this parameter is NULL, the brush defaults to an opacity of 1.0f and its transform is the identity matrix.</param>
 /// <unmanaged>HRESULT ID2D1DeviceContext::CreateBitmapBrush([In] ID2D1Bitmap* bitmap,[In, Optional] const D2D1_BITMAP_BRUSH_PROPERTIES1* bitmapBrushProperties,[In, Optional] const D2D1_BRUSH_PROPERTIES* brushProperties,[Out, Fast] ID2D1BitmapBrush1** bitmapBrush)</unmanaged>
 public BitmapBrush1(DeviceContext deviceContext, SharpDX.Direct2D1.Bitmap1 bitmap, SharpDX.Direct2D1.BrushProperties brushProperties)
     : this(deviceContext, bitmap, null, brushProperties)
 {
 }
Пример #9
0
 /// <summary>
 /// Creates an <see cref="SharpDX.Direct2D1.BitmapBrush"/> from the specified bitmap.
 /// </summary>
 /// <param name="deviceContext">an instance of <see cref = "DeviceContext" /></param>
 /// <param name="bitmap">The bitmap contents of the new brush.</param>
 /// <unmanaged>HRESULT ID2D1DeviceContext::CreateBitmapBrush([In] ID2D1Bitmap* bitmap,[In, Optional] const D2D1_BITMAP_BRUSH_PROPERTIES1* bitmapBrushProperties,[In, Optional] const D2D1_BRUSH_PROPERTIES* brushProperties,[Out, Fast] ID2D1BitmapBrush1** bitmapBrush)</unmanaged>
 public BitmapBrush1(DeviceContext deviceContext, SharpDX.Direct2D1.Bitmap1 bitmap)
     : this(deviceContext, bitmap, null, null)
 {
 }
Пример #10
0
 /// <inheritdoc/>
 public void Draw(SharpDX.Direct2D1.Bitmap1 target, SharpDX.Rectangle drawRect, SharpDX.Point targetOrigin)
 {
     Draw_(target, drawRect, targetOrigin);
 }
        /// <summary>
        /// Called from parent when it closes to ensure this asset closes properly and leaves
        /// not possible memory leaks
        /// </summary>
        public void Unload()
        {

            WindowLayoutService.OnWindowLayoutRaised -= WindowLayoutService_OnWindowLayoutRaised;

            _root = null;
            _rootParent = null;

            _clearRenderTree();
            ClearAssets();

            _clock = null;
            _gt = null;
            _tweener = null;

            _deviceManager = null;
            _d2dContext = null;

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

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

            if (_stagingTexture2D != null) {
                _stagingTexture2D.Dispose();
                _stagingTexture2D = null;
            }
            
            _pathD2DConverter = null;

           

            //_graphicsDevice.Dispose();
            //_graphicsDevice = null;

        }
        private void _updateDimensions(double width, double height)
        {
            _appWidth = (float)width + 5;
            _appHeight = (float)height;

            if (_deviceManager == null) return ;
            if (_stagingTexture2D != null) _stagingTexture2D.Dispose();
            if (_stagingBitmap != null) _stagingBitmap.Dispose();
            if (_stagingBitmapSourceEffect != null) _stagingBitmapSourceEffect.Dispose();

            _stagingTexture2D = AllocateTextureReturnSurface((int)_appWidth, (int)_appHeight);
            _stagingBitmap = new SharpDX.Direct2D1.Bitmap1(_deviceManager.ContextDirect2D, _stagingTexture2D.QueryInterface<SharpDX.DXGI.Surface>());
            _stagingBitmapSourceEffect = new SharpDX.Direct2D1.Effects.BitmapSource(_deviceManager.ContextDirect2D);


        }
        protected virtual void CreateSizeDependentResources(TargetBase renderBase)
        {
            var d3dDevice = DeviceManager.DeviceDirect3D;
            var d3dContext = DeviceManager.ContextDirect3D;
            var d2dContext = DeviceManager.ContextDirect2D;

            d2dContext.Target = null;
            Utilities.Dispose(ref renderTargetView);
            Utilities.Dispose(ref depthStencilView);
            Utilities.Dispose(ref bitmapTarget);
            Utilities.Dispose(ref backBuffer);

            // If the swap chain already exists, resize it.
            if (swapChain != null)
            {
                swapChain.ResizeBuffers(2, Width, Height, SharpDX.DXGI.Format.B8G8R8A8_UNorm, SharpDX.DXGI.SwapChainFlags.None);
            }
            // Otherwise, create a new one.
            else
            {
                // SwapChain description
                var desc = CreateSwapChainDescription();

                // Once the desired swap chain description is configured, it must be created on the same adapter as our D3D Device

                // First, retrieve the underlying DXGI Device from the D3D Device.
                // Creates the swap chain 
                using (var dxgiDevice2 = d3dDevice.QueryInterface<SharpDX.DXGI.Device2>())
                using (var dxgiAdapter = dxgiDevice2.Adapter)
                using (var dxgiFactory2 = dxgiAdapter.GetParent<SharpDX.DXGI.Factory2>())
                {
                    swapChain = CreateSwapChain(dxgiFactory2, d3dDevice, desc);

                    // Ensure that DXGI does not queue more than one frame at a time. This both reduces 
                    // latency and ensures that the application will only render after each VSync, minimizing 
                    // power consumption.
                    dxgiDevice2.MaximumFrameLatency = 1;
                }
            }

            // Obtain the backbuffer for this window which will be the final 3D rendertarget.
            backBuffer = SharpDX.Direct3D11.Texture2D.FromSwapChain<SharpDX.Direct3D11.Texture2D>(swapChain, 0);
            {
                // Create a view interface on the rendertarget to use on bind.
                renderTargetView = new SharpDX.Direct3D11.RenderTargetView(d3dDevice, BackBuffer);

                // Cache the rendertarget dimensions in our helper class for convenient use.
                var backBufferDesc = BackBuffer.Description;
                RenderTargetBounds = new Windows.Foundation.Rect(0, 0, backBufferDesc.Width, backBufferDesc.Height);
            }

            // Create a descriptor for the depth/stencil buffer.
            // Allocate a 2-D surface as the depth/stencil buffer.
            // Create a DepthStencil view on this surface to use on bind.
            using (var depthBuffer = new SharpDX.Direct3D11.Texture2D(d3dDevice, new SharpDX.Direct3D11.Texture2DDescription()
            {
                Format = SharpDX.DXGI.Format.D24_UNorm_S8_UInt,
                ArraySize = 1,
                MipLevels = 1,
                Width = (int)RenderTargetSize.Width,
                Height = (int)RenderTargetSize.Height,
                SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0),
                BindFlags = SharpDX.Direct3D11.BindFlags.DepthStencil,
            }))
                depthStencilView = new SharpDX.Direct3D11.DepthStencilView(d3dDevice, depthBuffer, new SharpDX.Direct3D11.DepthStencilViewDescription() { Dimension = SharpDX.Direct3D11.DepthStencilViewDimension.Texture2D });

            // Create a viewport descriptor of the full window size.
            var viewport = new SharpDX.Mathematics.ViewportF((float)RenderTargetBounds.X, (float)RenderTargetBounds.Y, (float)RenderTargetBounds.Width, (float)RenderTargetBounds.Height, 0.0f, 1.0f);

            // Set the current viewport using the descriptor.
            d3dContext.Rasterizer.SetViewport(viewport);

            // Now we set up the Direct2D render target bitmap linked to the swapchain. 
            // Whenever we render to this bitmap, it will be directly rendered to the 
            // swapchain associated with the window.
            var bitmapProperties = new SharpDX.Direct2D1.BitmapProperties1(
                new SharpDX.Direct2D1.PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Premultiplied),
                DeviceManager.Dpi,
                DeviceManager.Dpi,
                SharpDX.Direct2D1.BitmapOptions.Target | SharpDX.Direct2D1.BitmapOptions.CannotDraw);

            // Direct2D needs the dxgi version of the backbuffer surface pointer.
            // Get a D2D surface from the DXGI back buffer to use as the D2D render target.
            using (var dxgiBackBuffer = swapChain.GetBackBuffer<SharpDX.DXGI.Surface>(0))
                bitmapTarget = new SharpDX.Direct2D1.Bitmap1(d2dContext, dxgiBackBuffer, bitmapProperties);

            // So now we can set the Direct2D render target.
            d2dContext.Target = BitmapTarget2D;

            // Set D2D text anti-alias mode to Grayscale to ensure proper rendering of text on intermediate surfaces.
            d2dContext.TextAntialiasMode = SharpDX.Direct2D1.TextAntialiasMode.Grayscale;            
        }
Пример #14
0
        protected virtual void CreateSizeDependentResources(TargetBase renderBase)
        {
            var d3dDevice  = DeviceManager.DeviceDirect3D;
            var d3dContext = DeviceManager.ContextDirect3D;
            var d2dContext = DeviceManager.ContextDirect2D;

            d2dContext.Target = null;
            Utilities.Dispose(ref renderTargetView);
            Utilities.Dispose(ref depthStencilView);
            Utilities.Dispose(ref bitmapTarget);
            Utilities.Dispose(ref backBuffer);

            // If the swap chain already exists, resize it.
            if (swapChain != null)
            {
                swapChain.ResizeBuffers(2, Width, Height, SharpDX.DXGI.Format.B8G8R8A8_UNorm, SharpDX.DXGI.SwapChainFlags.None);
            }
            // Otherwise, create a new one.
            else
            {
                // SwapChain description
                var desc = CreateSwapChainDescription();

                // Once the desired swap chain description is configured, it must be created on the same adapter as our D3D Device

                // First, retrieve the underlying DXGI Device from the D3D Device.
                // Creates the swap chain
                using (var dxgiDevice2 = d3dDevice.QueryInterface <SharpDX.DXGI.Device2>())
                    using (var dxgiAdapter = dxgiDevice2.Adapter)
                        using (var dxgiFactory2 = dxgiAdapter.GetParent <SharpDX.DXGI.Factory2>())
                        {
                            swapChain = CreateSwapChain(dxgiFactory2, d3dDevice, desc);

                            // Ensure that DXGI does not queue more than one frame at a time. This both reduces
                            // latency and ensures that the application will only render after each VSync, minimizing
                            // power consumption.
                            dxgiDevice2.MaximumFrameLatency = 1;
                        }
            }

            // Obtain the backbuffer for this window which will be the final 3D rendertarget.
            backBuffer = SharpDX.Direct3D11.Texture2D.FromSwapChain <SharpDX.Direct3D11.Texture2D>(swapChain, 0);
            {
                // Create a view interface on the rendertarget to use on bind.
                renderTargetView = new SharpDX.Direct3D11.RenderTargetView(d3dDevice, BackBuffer);

                // Cache the rendertarget dimensions in our helper class for convenient use.
                var backBufferDesc = BackBuffer.Description;
                RenderTargetBounds = new Windows.Foundation.Rect(0, 0, backBufferDesc.Width, backBufferDesc.Height);
            }

            // Create a descriptor for the depth/stencil buffer.
            // Allocate a 2-D surface as the depth/stencil buffer.
            // Create a DepthStencil view on this surface to use on bind.
            using (var depthBuffer = new SharpDX.Direct3D11.Texture2D(d3dDevice, new SharpDX.Direct3D11.Texture2DDescription()
            {
                Format = SharpDX.DXGI.Format.D24_UNorm_S8_UInt,
                ArraySize = 1,
                MipLevels = 1,
                Width = (int)RenderTargetSize.Width,
                Height = (int)RenderTargetSize.Height,
                SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0),
                BindFlags = SharpDX.Direct3D11.BindFlags.DepthStencil,
            }))
                depthStencilView = new SharpDX.Direct3D11.DepthStencilView(d3dDevice, depthBuffer, new SharpDX.Direct3D11.DepthStencilViewDescription()
                {
                    Dimension = SharpDX.Direct3D11.DepthStencilViewDimension.Texture2D
                });

            // Create a viewport descriptor of the full window size.
            var viewport = new SharpDX.Mathematics.ViewportF((float)RenderTargetBounds.X, (float)RenderTargetBounds.Y, (float)RenderTargetBounds.Width, (float)RenderTargetBounds.Height, 0.0f, 1.0f);

            // Set the current viewport using the descriptor.
            d3dContext.Rasterizer.SetViewport(viewport);

            // Now we set up the Direct2D render target bitmap linked to the swapchain.
            // Whenever we render to this bitmap, it will be directly rendered to the
            // swapchain associated with the window.
            var bitmapProperties = new SharpDX.Direct2D1.BitmapProperties1(
                new SharpDX.Direct2D1.PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Premultiplied),
                DeviceManager.Dpi,
                DeviceManager.Dpi,
                SharpDX.Direct2D1.BitmapOptions.Target | SharpDX.Direct2D1.BitmapOptions.CannotDraw);

            // Direct2D needs the dxgi version of the backbuffer surface pointer.
            // Get a D2D surface from the DXGI back buffer to use as the D2D render target.
            using (var dxgiBackBuffer = swapChain.GetBackBuffer <SharpDX.DXGI.Surface>(0))
                bitmapTarget = new SharpDX.Direct2D1.Bitmap1(d2dContext, dxgiBackBuffer, bitmapProperties);

            // So now we can set the Direct2D render target.
            d2dContext.Target = BitmapTarget2D;

            // Set D2D text anti-alias mode to Grayscale to ensure proper rendering of text on intermediate surfaces.
            d2dContext.TextAntialiasMode = SharpDX.Direct2D1.TextAntialiasMode.Grayscale;
        }
Пример #15
0
        /// <summary>
        /// Create size dependent resources, in this case the swap chain and render targets
        /// </summary>
        /// <param name="app"></param>
        protected virtual void CreateSizeDependentResources(D3DApplicationBase app)
        {
            // Retrieve references to device and context
            var device  = DeviceManager.Direct3DDevice;
            var context = DeviceManager.Direct3DContext;
            // Retrieve Direct2D context (for use with text rendering etc)
            var d2dContext = DeviceManager.Direct2DContext;

            // Before the swapchain can resize all the buffers must be released
            RemoveAndDispose(ref _backBuffer);
            RemoveAndDispose(ref _renderTargetView);
            RemoveAndDispose(ref _depthStencilView);
            RemoveAndDispose(ref _depthBuffer);
            RemoveAndDispose(ref _bitmapTarget);
            d2dContext.Target = null;

            #region Initialize Direct3D swap chain and render target

            // If the swap chain already exists, resize it.
            if (_swapChain != null)
            {
                _swapChain.ResizeBuffers(
                    _swapChain.Description1.BufferCount,
                    Width,
                    Height,
                    _swapChain.Description.ModeDescription.Format,
                    _swapChain.Description.Flags);
            }
            // Otherwise, create a new one.
            else
            {
                // SwapChain description
                var desc = CreateSwapChainDescription();

                // Rather than create a new DXGI Factory we should reuse
                // the one that has been used internally to create the device

                // First, retrieve the underlying DXGI Device from the D3D Device.
                // access the adapter used for that device and then create the swap chain
                using (var dxgiDevice2 = device.QueryInterface <SharpDX.DXGI.Device2>())
                    using (var dxgiAdapter = dxgiDevice2.Adapter)
                        using (var dxgiFactory2 = dxgiAdapter.GetParent <SharpDX.DXGI.Factory2>())
                            using (var output = dxgiAdapter.Outputs.First())
                            {
                                // The CreateSwapChain method is used so we can descend
                                // from this class and implement a swapchain for a desktop
                                // or a Windows 8 AppStore app
                                _swapChain = ToDispose(CreateSwapChain(dxgiFactory2, device, desc));

#if !NETFX_CORE
                                // Retrieve the list of supported display modes
                                DisplayModeList = output.GetDisplayModeList(desc.Format, DisplayModeEnumerationFlags.Scaling);
#endif
                            }
            }

            // Obtain the backbuffer for this window which will be the final 3D rendertarget.
            BackBuffer = ToDispose(Texture2D.FromSwapChain <Texture2D>(_swapChain, 0));
            // Create a view interface on the rendertarget to use on bind.
            RenderTargetView = ToDispose(new RenderTargetView(device, BackBuffer));

            // Cache the rendertarget dimensions in our helper class for convenient use.
            var backBufferDesc = BackBuffer.Description;
            RenderTargetBounds = new SharpDX.Rectangle(0, 0, backBufferDesc.Width, backBufferDesc.Height);

            // Create a viewport descriptor of the render size.
            this.Viewport = new SharpDX.ViewportF(
                (float)RenderTargetBounds.X,
                (float)RenderTargetBounds.Y,
                (float)RenderTargetBounds.Width,
                (float)RenderTargetBounds.Height,
                0.0f,   // min depth
                1.0f);  // max depth

            // Set the current viewport for the rasterizer.
            context.Rasterizer.SetViewport(Viewport);

            // Create a descriptor for the depth/stencil buffer.
            // Allocate a 2-D texture as the depth/stencil buffer.
            // Create a DSV to use on bind.
            this.DepthBuffer = ToDispose(new Texture2D(device, new Texture2DDescription()
            {
                Format            = SharpDX.DXGI.Format.D32_Float_S8X24_UInt,
                ArraySize         = 1,
                MipLevels         = 1,
                Width             = RenderTargetSize.Width,
                Height            = RenderTargetSize.Height,
                SampleDescription = SwapChain.Description.SampleDescription,
                BindFlags         = BindFlags.DepthStencil,
            }));
            this.DepthStencilView = ToDispose(
                new DepthStencilView(
                    device,
                    DepthBuffer,
                    new DepthStencilViewDescription()
            {
                Dimension = (SwapChain.Description.SampleDescription.Count > 1 || SwapChain.Description.SampleDescription.Quality > 0) ? DepthStencilViewDimension.Texture2DMultisampled : DepthStencilViewDimension.Texture2D
            }));

            // Set the OutputMerger targets
            context.OutputMerger.SetTargets(DepthStencilView, RenderTargetView);

            #endregion

            #region Initialize Direct2D render target

            // Now we set up the Direct2D render target bitmap linked to the swapchain.
            // Whenever we render to this bitmap, it will be directly rendered to the
            // swapchain associated with the window.
            var bitmapProperties = new SharpDX.Direct2D1.BitmapProperties1(
                new SharpDX.Direct2D1.PixelFormat(_swapChain.Description.ModeDescription.Format, SharpDX.Direct2D1.AlphaMode.Premultiplied),
                DeviceManager.Dpi,
                DeviceManager.Dpi,
                SharpDX.Direct2D1.BitmapOptions.Target | SharpDX.Direct2D1.BitmapOptions.CannotDraw);

            // Direct2D needs the dxgi version of the backbuffer surface pointer.
            // Get a D2D surface from the DXGI back buffer to use as the D2D render target.
            using (var dxgiBackBuffer = _swapChain.GetBackBuffer <SharpDX.DXGI.Surface>(0))
                BitmapTarget2D = ToDispose(new SharpDX.Direct2D1.Bitmap1(d2dContext, dxgiBackBuffer, bitmapProperties));

            // So now we can set the Direct2D render target.
            d2dContext.Target = BitmapTarget2D;

            // Set D2D text anti-alias mode to Grayscale to ensure proper rendering of text on intermediate surfaces.
            d2dContext.TextAntialiasMode = SharpDX.Direct2D1.TextAntialiasMode.Grayscale;

            #endregion
        }