Exemplo n.º 1
0
        /// <summary>
        /// Creates device resources.
        /// </summary>
        /// <remarks>
        /// This method is called at the initialization of this instance.
        /// </remarks>
        protected virtual void CreateDeviceResources()
        {
            // Dispose previous references and set to null
            SafeDispose(ref d3dDevice);
            SafeDispose(ref d3dContext);
            SafeDispose(ref d2dDevice);
            SafeDispose(ref d2dContext);

            // Allocate new references
            // Enable compatibility with Direct2D
            // Retrieve the Direct3D 11.1 device amd device context
            var creationFlags = SharpDX.Direct3D11.DeviceCreationFlags.VideoSupport | SharpDX.Direct3D11.DeviceCreationFlags.BgraSupport;

#if DEBUG
            creationFlags |= SharpDX.Direct3D11.DeviceCreationFlags.Debug;
#endif
            using (var defaultDevice = new SharpDX.Direct3D11.Device(DriverType.Hardware, creationFlags))
                d3dDevice = defaultDevice.QueryInterface <SharpDX.Direct3D11.Device1>();
            featureLevel = d3dDevice.FeatureLevel;

            // Get Direct3D 11.1 context
            d3dContext = ToDispose(d3dDevice.ImmediateContext.QueryInterface <SharpDX.Direct3D11.DeviceContext1>());

            // Create Direct2D device
            using (var dxgiDevice = d3dDevice.QueryInterface <SharpDX.DXGI.Device>())
                d2dDevice = ToDispose(new SharpDX.Direct2D1.Device(d2dFactory, dxgiDevice));

            // Create Direct2D context
            d2dContext = ToDispose(new SharpDX.Direct2D1.DeviceContext(d2dDevice, SharpDX.Direct2D1.DeviceContextOptions.None));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Creates device resources.
        /// </summary>
        /// <remarks>
        /// This method is called at the initialization of this instance.
        /// </remarks>
        protected virtual void CreateDeviceResources()
        {
            // Dispose previous references and set to null
            if (d3dDevice != null)
            {
                RemoveAndDispose(ref d3dDevice);
            }

            if (d3dContext != null)
            {
                RemoveAndDispose(ref d3dContext);
            }

            if (d2dDevice != null)
            {
                RemoveAndDispose(ref d2dDevice);
            }

            if (d2dContext != null)
            {
                RemoveAndDispose(ref d2dContext);
            }

            // Allocate new references
            // Enable compatibility with Direct2D
            // Retrieve the Direct3D 11.1 device amd device context
            var creationFlags = SharpDX.Direct3D11.DeviceCreationFlags.VideoSupport | SharpDX.Direct3D11.DeviceCreationFlags.BgraSupport;

            // Decomment this line to have Debug. Unfortunately, debug is sometimes crashing applications, so it is disable by default
            try
            {
                // Try to create it with Video Support
                // If it is not working, we just use BGRA
                // Force to FeatureLevel.Level_9_1
                using (var defaultDevice = new SharpDX.Direct3D11.Device(DriverType.Hardware, creationFlags))
                    d3dDevice = defaultDevice.QueryInterface <SharpDX.Direct3D11.Device1>();
            } catch (Exception)
            {
                creationFlags = SharpDX.Direct3D11.DeviceCreationFlags.BgraSupport;
                using (var defaultDevice = new SharpDX.Direct3D11.Device(DriverType.Hardware, creationFlags))
                    d3dDevice = defaultDevice.QueryInterface <SharpDX.Direct3D11.Device1>();
            }
            featureLevel = d3dDevice.FeatureLevel;

            // Get Direct3D 11.1 context
            d3dContext = Collect(d3dDevice.ImmediateContext.QueryInterface <SharpDX.Direct3D11.DeviceContext1>());

            // Create Direct2D device
            using (var dxgiDevice = d3dDevice.QueryInterface <SharpDX.DXGI.Device>())
                d2dDevice = Collect(new SharpDX.Direct2D1.Device(d2dFactory, dxgiDevice));

            // Create Direct2D context
            d2dContext = Collect(new SharpDX.Direct2D1.DeviceContext(d2dDevice, SharpDX.Direct2D1.DeviceContextOptions.None));
        }
Exemplo n.º 3
0
        private void OnLoaded(object sender, RoutedEventArgs e)
        {
            ManipulationMode = ManipulationModes.TranslateX | ManipulationModes.TranslateY | ManipulationModes.TranslateInertia | ManipulationModes.Scale;

            tileBackground.ImageSource = "Assets/bgline.JPG";

            onePointWait.Visibility = Visibility.Collapsed;
            twoPointWait.Visibility = Visibility.Collapsed;

            double val = -Math.PI / 2;

            foreach (UIElement child in colorSelect.Children)
            {
                child.SetValue(Canvas.LeftProperty, Math.Cos(val) * 70);
                child.SetValue(Canvas.TopProperty, Math.Sin(val) * 70);
                val += Math.PI / 5;
            }

            var debugLevel = SharpDX.Direct2D1.DebugLevel.None;

            d2dFactory = new SharpDX.Direct2D1.Factory1(SharpDX.Direct2D1.FactoryType.SingleThreaded, debugLevel);

            var creationFlags = SharpDX.Direct3D11.DeviceCreationFlags.BgraSupport;

            using (var defaultDevice = new SharpDX.Direct3D11.Device(SharpDX.Direct3D.DriverType.Hardware, creationFlags))
                d3dDevice = defaultDevice.QueryInterface <SharpDX.Direct3D11.Device1>();

            d3dContext = d3dDevice.ImmediateContext.QueryInterface <SharpDX.Direct3D11.DeviceContext1>();

            using (dxgiDevice = d3dDevice.QueryInterface <SharpDX.DXGI.Device2>())
                d2dDevice = new SharpDX.Direct2D1.Device(d2dFactory, dxgiDevice);

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

            DisplayProperties.LogicalDpiChanged += LogicalDpiChanged;
        }
        /// <summary>
        /// Creates device resources. 
        /// </summary>
        /// <remarks>
        /// This method is called at the initialization of this instance.
        /// </remarks>
        protected virtual void CreateDeviceResources()
        {
            // Dispose previous references and set to null
            RemoveAndDispose(ref d3dDevice);
            RemoveAndDispose(ref d3dContext);
            RemoveAndDispose(ref d2dDevice);
            RemoveAndDispose(ref d2dContext);

            // Allocate new references
            // Enable compatibility with Direct2D
            // Retrieve the Direct3D 11.1 device amd device context
            var creationFlags = SharpDX.Direct3D11.DeviceCreationFlags.VideoSupport | SharpDX.Direct3D11.DeviceCreationFlags.BgraSupport;

            // Decomment this line to have Debug. Unfortunately, debug is sometimes crashing applications, so it is disable by default
            try
            {
                // Try to create it with Video Support
                // If it is not working, we just use BGRA
                // Force to FeatureLevel.Level_9_1
                using (var defaultDevice = new SharpDX.Direct3D11.Device(DriverType.Hardware, creationFlags))
                    d3dDevice = defaultDevice.QueryInterface<SharpDX.Direct3D11.Device1>();
            } catch (Exception)
            {
                creationFlags = SharpDX.Direct3D11.DeviceCreationFlags.BgraSupport;
                using (var defaultDevice = new SharpDX.Direct3D11.Device(DriverType.Hardware, creationFlags))
                    d3dDevice = defaultDevice.QueryInterface<SharpDX.Direct3D11.Device1>();
            }
            featureLevel = d3dDevice.FeatureLevel;

            // Get Direct3D 11.1 context
            d3dContext = ToDispose(d3dDevice.ImmediateContext.QueryInterface<SharpDX.Direct3D11.DeviceContext1>());

            // Create Direct2D device
            using (var dxgiDevice = d3dDevice.QueryInterface<SharpDX.DXGI.Device>())
                d2dDevice = ToDispose(new SharpDX.Direct2D1.Device(d2dFactory, dxgiDevice));

            // Create Direct2D context
            d2dContext = ToDispose(new SharpDX.Direct2D1.DeviceContext(d2dDevice, SharpDX.Direct2D1.DeviceContextOptions.None));
        }
Exemplo n.º 5
0
        private void OnLoaded(object sender, RoutedEventArgs e)
        {
            ManipulationMode = ManipulationModes.TranslateX | ManipulationModes.TranslateY | ManipulationModes.TranslateInertia | ManipulationModes.Scale;

            tileBackground.ImageSource = "Assets/bgline.JPG";

            onePointWait.Visibility = Visibility.Collapsed;
            twoPointWait.Visibility = Visibility.Collapsed;

            double val = -Math.PI / 2;
            foreach (UIElement child in colorSelect.Children)
            {
                child.SetValue(Canvas.LeftProperty, Math.Cos(val) * 70);
                child.SetValue(Canvas.TopProperty, Math.Sin(val) * 70);
                val += Math.PI / 5;
            }

            var debugLevel = SharpDX.Direct2D1.DebugLevel.None;
            d2dFactory = new SharpDX.Direct2D1.Factory1(SharpDX.Direct2D1.FactoryType.SingleThreaded, debugLevel);

            var creationFlags = SharpDX.Direct3D11.DeviceCreationFlags.BgraSupport;

            using (var defaultDevice = new SharpDX.Direct3D11.Device(SharpDX.Direct3D.DriverType.Hardware, creationFlags))
                d3dDevice = defaultDevice.QueryInterface<SharpDX.Direct3D11.Device1>();

            d3dContext = d3dDevice.ImmediateContext.QueryInterface<SharpDX.Direct3D11.DeviceContext1>();

            using (dxgiDevice = d3dDevice.QueryInterface<SharpDX.DXGI.Device2>())
                d2dDevice = new SharpDX.Direct2D1.Device(d2dFactory, dxgiDevice);

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

            DisplayProperties.LogicalDpiChanged += LogicalDpiChanged;
        }
Exemplo n.º 6
0
        /// <summary>
        /// Creates device resources. 
        /// </summary>
        /// <remarks>
        /// This method is called at the initialization of this instance.
        /// </remarks>
        protected virtual void CreateDeviceResources()
        {
            // Dispose previous references and set to null
            SafeDispose(ref d3dDevice);
            SafeDispose(ref d3dContext);
            SafeDispose(ref d2dDevice);
            SafeDispose(ref d2dContext);

            // Allocate new references
            // Enable compatibility with Direct2D
            // Retrieve the Direct3D 11.1 device amd device context
            var creationFlags = SharpDX.Direct3D11.DeviceCreationFlags.VideoSupport | SharpDX.Direct3D11.DeviceCreationFlags.BgraSupport;
            #if DEBUG
            creationFlags |= SharpDX.Direct3D11.DeviceCreationFlags.Debug;
            #endif
            using (var defaultDevice = new SharpDX.Direct3D11.Device(DriverType.Hardware, creationFlags))
                d3dDevice = defaultDevice.QueryInterface<SharpDX.Direct3D11.Device1>();
            featureLevel = d3dDevice.FeatureLevel;

            // Get Direct3D 11.1 context
            d3dContext = ToDispose(d3dDevice.ImmediateContext.QueryInterface<SharpDX.Direct3D11.DeviceContext1>());

            // Create Direct2D device
            using (var dxgiDevice = d3dDevice.QueryInterface<SharpDX.DXGI.Device>())
                d2dDevice = ToDispose(new SharpDX.Direct2D1.Device(d2dFactory, dxgiDevice));

            // Create Direct2D context
            d2dContext = ToDispose(new SharpDX.Direct2D1.DeviceContext(d2dDevice, SharpDX.Direct2D1.DeviceContextOptions.None));
        }
Exemplo n.º 7
0
            // This method creates all application resources that depend on
            // the application window size.  It is called at app initialization,
            // and whenever the application window size changes.
            void CreateWindowSizeDependentResources()
            {
                if (m_swapChain != null)
                {
                    // If the swap chain already exists, resize it.
                    m_swapChain.ResizeBuffers(
                        2,
                        0,
                        0,
                        SharpDX.DXGI.Format.B8G8R8A8_UNorm,
                        0
                        );
                }
                else
                {
                    // If the swap chain does not exist, create it.
                    var swapChainDesc = new SharpDX.DXGI.SwapChainDescription1
                    {
                        Stereo  = false,
                        Usage   = SharpDX.DXGI.Usage.RenderTargetOutput,
                        Scaling = SharpDX.DXGI.Scaling.None,
                        Flags   = 0,
                    };

                    // Use automatic sizing.
                    swapChainDesc.Width  = 0;
                    swapChainDesc.Height = 0;

                    // This is the most common swap chain format.
                    swapChainDesc.Format = SharpDX.DXGI.Format.B8G8R8A8_UNorm;

                    // Don't use multi-sampling.
                    swapChainDesc.SampleDescription.Count   = 1;
                    swapChainDesc.SampleDescription.Quality = 0;

                    // Use two buffers to enable flip effect.
                    swapChainDesc.BufferCount = 2;

                    // We recommend using this swap effect for all applications.
                    swapChainDesc.SwapEffect = SharpDX.DXGI.SwapEffect.FlipSequential;

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

                    // First, retrieve the underlying DXGI Device from the D3D Device.
                    using (var dxgiDevice = m_d3dDevice.QueryInterface <SharpDX.DXGI.Device2>())
                    {
                        // 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.
                        dxgiDevice.MaximumFrameLatency = 1;

                        // Next, get the parent factory from the DXGI Device.
                        using (var dxgiAdapter = dxgiDevice.Adapter)
                            using (var dxgiFactory = dxgiAdapter.GetParent <SharpDX.DXGI.Factory2>())
                                // Finally, create the swap chain.
                                using (var coreWindow = new SharpDX.ComObject(m_window))
                                {
                                    m_swapChain = new SharpDX.DXGI.SwapChain1(dxgiFactory
                                                                              , m_d3dDevice, coreWindow, ref swapChainDesc);
                                }
                    }
                }

                // Once the swap chain is created, create a render target view.  This will
                // allow Direct3D to render graphics to the window.
                using (var backBuffer = m_swapChain.GetBackBuffer <SharpDX.Direct3D11.Texture2D>(0))
                {
                    m_renderTargetView = new SharpDX.Direct3D11.RenderTargetView(m_d3dDevice, backBuffer);

                    // After the render target view is created, specify that the viewport,
                    // which describes what portion of the window to draw to, should cover
                    // the entire window.

                    var backBufferDesc = backBuffer.Description;

                    var viewport = new SharpDX.ViewportF
                    {
                        X        = 0.0f,
                        Y        = 0.0f,
                        Width    = backBufferDesc.Width,
                        Height   = backBufferDesc.Height,
                        MinDepth = 0,
                        MaxDepth = 1,
                    };

                    m_d3dDeviceContext.Rasterizer.SetViewport(viewport);
                }
            }
Exemplo n.º 8
0
        private void DirectXRender(List <UIElement> points)
        {
            if (points.Count <= 0)
            {
                return;
            }

            Line initLine = points[0] as Line;

            Point
                p1 = new Point
                     (
                Math.Min(initLine.X1 - initLine.StrokeThickness / 2, initLine.X2 - initLine.StrokeThickness / 2),
                Math.Min(initLine.Y1 - initLine.StrokeThickness / 2, initLine.Y2 - initLine.StrokeThickness / 2)
                     ),
                p2 = new Point
                     (
                Math.Max(initLine.X1 + initLine.StrokeThickness / 2, initLine.X2 + initLine.StrokeThickness / 2),
                Math.Max(initLine.Y1 + initLine.StrokeThickness / 2, initLine.Y2 + initLine.StrokeThickness / 2)
                     );

            foreach (var child in points)
            {
                var line = child as Line;

                if (line == null)
                {
                    continue;
                }

                if (p1.X > line.X1 - line.StrokeThickness / 2)
                {
                    p1.X = line.X1 - line.StrokeThickness / 2;
                }
                if (p1.X > line.X2 - line.StrokeThickness / 2)
                {
                    p1.X = line.X2 - line.StrokeThickness / 2;
                }

                if (p2.X < line.X1 + line.StrokeThickness / 2)
                {
                    p2.X = line.X1 + line.StrokeThickness / 2;
                }
                if (p2.X < line.X2 + line.StrokeThickness / 2)
                {
                    p2.X = line.X2 + line.StrokeThickness / 2;
                }

                if (p1.Y > line.Y1 - line.StrokeThickness / 2)
                {
                    p1.Y = line.Y1 - line.StrokeThickness / 2;
                }
                if (p1.Y > line.Y2 - line.StrokeThickness / 2)
                {
                    p1.Y = line.Y2 - line.StrokeThickness / 2;
                }

                if (p2.Y < line.Y1 + line.StrokeThickness / 2)
                {
                    p2.Y = line.Y1 + line.StrokeThickness / 2;
                }
                if (p2.Y < line.Y2 + line.StrokeThickness / 2)
                {
                    p2.Y = line.Y2 + line.StrokeThickness / 2;
                }
            }

            var bndRect = new Rect(p1, p2);

            var dxTarget = new SurfaceImageSource
                           (
                (int)(bndRect.Width * DisplayProperties.LogicalDpi / 96.0 + 1),
                (int)(bndRect.Height * DisplayProperties.LogicalDpi / 96.0 + 1)
                           );

            SharpDX.DXGI.ISurfaceImageSourceNative dxTargetNative = SharpDX.ComObject.As <SharpDX.DXGI.ISurfaceImageSourceNative>(dxTarget);
            dxTargetNative.Device = d3dDevice.QueryInterface <SharpDX.DXGI.Device>();

            /*
             * Draw Logic
             */
            SharpDX.DrawingPoint drawingPoint;
            var surface = dxTargetNative.BeginDraw(new SharpDX.Rectangle(0, 0,
                                                                         (int)(bndRect.Width * DisplayProperties.LogicalDpi / 96.0 + 1),
                                                                         (int)(bndRect.Height * DisplayProperties.LogicalDpi / 96.0 + 1)),
                                                   out drawingPoint);

            var dxRenderTarget = new SharpDX.Direct2D1.RenderTarget(d2dFactory, surface, new SharpDX.Direct2D1.RenderTargetProperties()
            {
                DpiX        = DisplayProperties.LogicalDpi,
                DpiY        = DisplayProperties.LogicalDpi,
                PixelFormat = new SharpDX.Direct2D1.PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Premultiplied),
                Type        = SharpDX.Direct2D1.RenderTargetType.Default,
                Usage       = SharpDX.Direct2D1.RenderTargetUsage.None
            });

            dxRenderTarget.BeginDraw();
            dxRenderTarget.Clear(SharpDX.Color.Transparent);

            foreach (var child in points)
            {
                var line = child as Line;

                if (line == null)
                {
                    continue;
                }

                Color c     = (line.Stroke as SolidColorBrush).Color;
                var   brush = new SharpDX.Direct2D1.SolidColorBrush(dxRenderTarget, new SharpDX.Color(c.R, c.G, c.B, c.A));

                var style = new SharpDX.Direct2D1.StrokeStyleProperties();
                style.LineJoin = SharpDX.Direct2D1.LineJoin.Round;
                style.StartCap = SharpDX.Direct2D1.CapStyle.Round;
                style.EndCap   = SharpDX.Direct2D1.CapStyle.Round;
                var stroke = new SharpDX.Direct2D1.StrokeStyle(d2dFactory, style);

                dxRenderTarget.DrawLine(
                    new SharpDX.DrawingPointF((float)(line.X1 - bndRect.Left), (float)(line.Y1 - bndRect.Top)),
                    new SharpDX.DrawingPointF((float)(line.X2 - bndRect.Left), (float)(line.Y2 - bndRect.Top)),
                    brush, (float)line.StrokeThickness, stroke);
            }

            dxRenderTarget.EndDraw();
            dxTargetNative.EndDraw();

            var dxImage = new Image();

            dxImage.Source = dxTarget;
            canvas.Children.Add(dxImage);
            Canvas.SetLeft(dxImage, bndRect.X);
            Canvas.SetTop(dxImage, bndRect.Y);
        }
Exemplo n.º 9
0
        void CreateDevice()
        {
            // d3d11
            var flags = SharpDX.Direct3D11.DeviceCreationFlags.BgraSupport;
            #if DEBUG
            // error on Windows10(Geforce435M)
            //flags |= SharpDX.Direct3D11.DeviceCreationFlags.Debug;
            #endif

            using (var device = new SharpDX.Direct3D11.Device(SharpDX.Direct3D.DriverType.Hardware
                , flags
                , SharpDX.Direct3D.FeatureLevel.Level_10_0
                ))
            {
                D3DDevice = device.QueryInterfaceOrNull<SharpDX.Direct3D11.Device1>();
            }

            // DXGIDevice
            DXGIDevice = D3DDevice.QueryInterface<SharpDX.DXGI.Device2>();
            DXGIDevice.MaximumFrameLatency = 1;
        }