// ReSharper restore InconsistentNaming

        static CompositionEngine()
        {
            _wicFactory = new SharpDX.WIC.ImagingFactory();
            _dWriteFactory = new SharpDX.DirectWrite.Factory();

            var d3DDevice = new SharpDX.Direct3D11.Device(
                DriverType.Hardware,
                DeviceCreationFlags.BgraSupport
#if DEBUG
                | DeviceCreationFlags.Debug
#endif
,
                FeatureLevel.Level_11_1,
                FeatureLevel.Level_11_0,
                FeatureLevel.Level_10_1,
                FeatureLevel.Level_10_0,
                FeatureLevel.Level_9_3,
                FeatureLevel.Level_9_2,
                FeatureLevel.Level_9_1
                );

            var dxgiDevice = ComObject.As<SharpDX.DXGI.Device>(d3DDevice.NativePointer);
                //new SharpDX.DXGI.Device2(d3DDevice.NativePointer);
            var d2DDevice = new SharpDX.Direct2D1.Device(dxgiDevice);
            _d2DFactory = d2DDevice.Factory;
            _d2DDeviceContext = new SharpDX.Direct2D1.DeviceContext(d2DDevice, D2D.DeviceContextOptions.None);
            _d2DDeviceContext.DotsPerInch = new Size2F(LogicalDpi, LogicalDpi);
        }
예제 #2
0
        /// <summary>
        /// Gets the font names.
        /// </summary>
        /// <returns>IEnumerable&lt;System.String&gt;.</returns>
        public IEnumerable<string> GetFontNames()
        {
            var fontList = new List<string>();
#if NETFX_CORE || WINDOWS_PHONE_APP
            var factory = new SharpDX.DirectWrite.Factory();
            var fontCollection = factory.GetSystemFontCollection(false);
            var familyCount = fontCollection.FontFamilyCount;

            for (int i = 0; i < familyCount; i++)
            {
                var fontFamily = fontCollection.GetFontFamily(i);
                var familyNames = fontFamily.FamilyNames;
                int index;

                if (!familyNames.FindLocaleName(System.Globalization.CultureInfo.CurrentCulture.Name, out index))
                {
                    familyNames.FindLocaleName("en-us", out index);
                }

                string name = familyNames.GetString(index);
                fontList.Add(name);
            }
#endif

            return fontList;
        }
예제 #3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="DrawingContext"/> class.
 /// </summary>
 /// <param name="renderTarget">The render target to draw to.</param>
 /// <param name="directWriteFactory">The DirectWrite factory.</param>
 public DrawingContext(
     SharpDX.Direct2D1.RenderTarget renderTarget,
     SharpDX.DirectWrite.Factory directWriteFactory)
 {
     _renderTarget = renderTarget;
     _directWriteFactory = directWriteFactory;
     _renderTarget.BeginDraw();
 }
예제 #4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="DrawingContext"/> class.
 /// </summary>
 /// <param name="renderTarget">The render target to draw to.</param>
 /// <param name="directWriteFactory">The DirectWrite factory.</param>
 public DrawingContext(
     RenderTarget renderTarget,
     SharpDX.DirectWrite.Factory directWriteFactory)
 {
     this.renderTarget = renderTarget;
     this.directWriteFactory = directWriteFactory;
     this.renderTarget.BeginDraw();
 }
예제 #5
0
 public D2DRenderTargetBitmapImpl(
     ImagingFactory imagingFactory,
     DirectWriteFactory dwriteFactory,
     BitmapRenderTarget target)
     : base(imagingFactory, target.Bitmap)
 {
     _dwriteFactory = dwriteFactory;
     _target        = target;
 }
예제 #6
0
        internal static void InitializeDirect2D()
        {
            lock (s_initLock)
            {
                if (s_initialized)
                {
                    return;
                }
#if DEBUG
                try
                {
                    Direct2D1Factory = new SharpDX.Direct2D1.Factory1(
                        SharpDX.Direct2D1.FactoryType.MultiThreaded,
                        SharpDX.Direct2D1.DebugLevel.Error);
                }
                catch
                {
                    //
                }
#endif
                if (Direct2D1Factory == null)
                {
                    Direct2D1Factory = new SharpDX.Direct2D1.Factory1(
                        SharpDX.Direct2D1.FactoryType.MultiThreaded,
                        SharpDX.Direct2D1.DebugLevel.None);
                }

                using (var factory = new SharpDX.DirectWrite.Factory())
                {
                    DirectWriteFactory = factory.QueryInterface <SharpDX.DirectWrite.Factory1>();
                }

                ImagingFactory = new SharpDX.WIC.ImagingFactory();

                var featureLevels = new[]
                {
                    SharpDX.Direct3D.FeatureLevel.Level_11_1,
                    SharpDX.Direct3D.FeatureLevel.Level_11_0,
                    SharpDX.Direct3D.FeatureLevel.Level_10_1,
                    SharpDX.Direct3D.FeatureLevel.Level_10_0,
                    SharpDX.Direct3D.FeatureLevel.Level_9_3,
                    SharpDX.Direct3D.FeatureLevel.Level_9_2,
                    SharpDX.Direct3D.FeatureLevel.Level_9_1,
                };

                Direct3D11Device = new SharpDX.Direct3D11.Device(
                    SharpDX.Direct3D.DriverType.Hardware,
                    SharpDX.Direct3D11.DeviceCreationFlags.BgraSupport | SharpDX.Direct3D11.DeviceCreationFlags.VideoSupport,
                    featureLevels);

                DxgiDevice = Direct3D11Device.QueryInterface <SharpDX.DXGI.Device1>();

                Direct2D1Device = new SharpDX.Direct2D1.Device(Direct2D1Factory, DxgiDevice);

                s_initialized = true;
            }
        }
예제 #7
0
 public ExternalRenderTarget(
     IExternalDirect2DRenderTargetSurface externalRenderTargetProvider,
     DirectWriteFactory dwFactory,
     SharpDX.WIC.ImagingFactory wicFactory)
 {
     _externalRenderTargetProvider = externalRenderTargetProvider;
     _dwFactory  = dwFactory;
     _wicFactory = wicFactory;
 }
예제 #8
0
 public CompositionEngine()
 {
     _dxgiFactory = new SharpDX.DXGI.Factory1();
     //_dxgiFactory.Adapters1[0].Description1.
     //_dxgiFactory = new SharpDX.DXGI.Factory();
     //new
     _wicFactory    = new SharpDX.WIC.ImagingFactory();
     _d2DFactory    = new SharpDX.Direct2D1.Factory();
     _dWriteFactory = new SharpDX.DirectWrite.Factory();
 }
 public CompositionEngine()
 {
     _dxgiFactory = new SharpDX.DXGI.Factory1();
     //_dxgiFactory.Adapters1[0].Description1.
     //_dxgiFactory = new SharpDX.DXGI.Factory();
     //new 
     _wicFactory = new SharpDX.WIC.ImagingFactory();
     _d2DFactory = new SharpDX.Direct2D1.Factory();
     _dWriteFactory = new SharpDX.DirectWrite.Factory();
 }
예제 #10
0
        public void LoadResources()
        {
            bsToolBarBox    = D3DTextureLoader.LoadBitmap(new SharpDX.WIC.ImagingFactory2(), "Resources/GUI/ToolBarBox.jpg");
            bsToolBarSelect = D3DTextureLoader.LoadBitmap(new SharpDX.WIC.ImagingFactory2(), "Resources/GUI/ToolBarSelect.png");

            var textFactory = new SharpDX.DirectWrite.Factory();

            textFormat = new SharpDX.DirectWrite.TextFormat(textFactory, "Consolas", 12.0f);
            textFactory.Dispose();
        }
예제 #11
0
 /// <summary>
 /// Initializes a new instance of the <see cref="DrawingContextImpl"/> class.
 /// </summary>
 /// <param name="renderTarget">The render target to draw to.</param>
 /// <param name="directWriteFactory">The DirectWrite factory.</param>
 /// <param name="swapChain">An optional swap chain associated with this drawing context.</param>
 public DrawingContextImpl(
     SharpDX.Direct2D1.RenderTarget renderTarget,
     SharpDX.DirectWrite.Factory directWriteFactory,
     SharpDX.DXGI.SwapChain1 swapChain = null)
 {
     _renderTarget       = renderTarget;
     _directWriteFactory = directWriteFactory;
     _swapChain          = swapChain;
     _renderTarget.BeginDraw();
 }
예제 #12
0
 private void MainWindow_Loaded(object sender, RoutedEventArgs e)
 {
     SharpDX.DirectWrite.Factory    factory    = new SharpDX.DirectWrite.Factory(SharpDX.DirectWrite.FactoryType.Isolated);
     SharpDX.DirectWrite.TextFormat textFormat = new SharpDX.DirectWrite.TextFormat(factory, "Arial", 60);
     textLayout = new SharpDX.DirectWrite.TextLayout(factory, "Test", textFormat, 0, 0);
     MainWindow_SizeChanged(null, null);
     InteropImage.WindowOwner     = (new System.Windows.Interop.WindowInteropHelper(this)).Handle;
     InteropImage.OnRender        = OnRender;
     CompositionTarget.Rendering += CompositionTarget_Rendering;
 }
예제 #13
0
        private void Create()
        {
            var d3dCreationFlags = DeviceCreationFlags.BgraSupport | DeviceCreationFlags.SingleThreaded;

            this.m_d3D = this.m_compVariant > 0
                ? D3D11MetaFactory.CreateForComposition(creationFlags: d3dCreationFlags)
                : D3D11MetaFactory.CreateForWindowTarget(creationFlags: d3dCreationFlags);
            this.m_d2D           = D2D1MetaFactory.CreateForSwapChain();
            this.m_dWriteFactory = new Factory(FactoryType.Shared);
            this.Compositor      = new WindowSwapChainCompositor(this.m_compVariant);
        }
예제 #14
0
        /// <summary>
        /// Create device manager objects
        /// </summary>
        /// <remarks>
        /// This method is called at the initialization of this instance.
        /// </remarks>
        protected virtual void CreateInstance()
        {
            // Dispose previous references and set to null
            RemoveAndDispose(ref d3dDevice);
            RemoveAndDispose(ref d3dContext);
            RemoveAndDispose(ref d2dDevice);
            RemoveAndDispose(ref d2dContext);
            RemoveAndDispose(ref d2dFactory);
            RemoveAndDispose(ref dwriteFactory);
            RemoveAndDispose(ref wicFactory);


            #region Create Direct3D 11.1 device and retrieve device context

            // BGRA performs better especially with Direct2D software render targets
            var creationFlags = SharpDX.Direct3D11.DeviceCreationFlags.BgraSupport;
#if DEBUG
            // Enble D3D device debug layer
            creationFlags |= SharpDX.Direct3D11.DeviceCreationFlags.Debug;
#endif
            // Retrieve the Direct3D 11.1 device and device context
            using (var device = new SharpDX.Direct3D11.Device(DriverType.Hardware,
                                                              creationFlags,
                                                              D3DFeatureLevel))
            {
                d3dDevice = ToDispose(device.QueryInterface <Device1>());
            }

            // Get Direct3D 11.1 immediate context
            d3dContext = ToDispose(d3dDevice.ImmediateContext.QueryInterface <DeviceContext1>());
            #endregion

            #region Create Direct2D device and context

#if DEBUG
            var debugLevel = SharpDX.Direct2D1.DebugLevel.Information;
#endif
            // Allocate new references
            d2dFactory    = ToDispose(new SharpDX.Direct2D1.Factory1(SharpDX.Direct2D1.FactoryType.SingleThreaded, debugLevel));
            dwriteFactory = ToDispose(new SharpDX.DirectWrite.Factory(SharpDX.DirectWrite.FactoryType.Shared));
            wicFactory    = ToDispose(new SharpDX.WIC.ImagingFactory2());

            // 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));

            #endregion
        }
예제 #15
0
        public static void InitializeDirectX()
        {
            renderForm = new RenderForm(title);

            renderForm.Size          = new System.Drawing.Size(800, 600);
            renderForm.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;

            var swapChainDesc = new SwapChainDescription()
            {
                BufferCount       = 4,
                Flags             = SwapChainFlags.AllowModeSwitch,
                IsWindowed        = true,
                ModeDescription   = new ModeDescription(800, 600, new Rational(1, 60), Format.R8G8B8A8_UNorm),
                OutputHandle      = renderForm.Handle,
                SampleDescription = new SampleDescription(1, 0),
                SwapEffect        = SwapEffect.Discard,
                Usage             = Usage.RenderTargetOutput
            };

            D3D.FeatureLevel[] featureLevels = { D3D.FeatureLevel.Level_9_3, D3D.FeatureLevel.Level_10_1 };
            Device.CreateWithSwapChain(D3D.DriverType.Hardware, DeviceCreationFlags.BgraSupport, featureLevels, swapChainDesc, out device, out swapChain);

            Surface backBuffer = Surface.FromSwapChain(swapChain, 0);

            using (var factory2D = new Factory2D(FactoryType.MultiThreaded))
            {
                var dpi = factory2D.DesktopDpi;

                InitializeRenderTarget(backBuffer, factory2D, dpi);

                renderForm.AutoSizeMode        = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
                renderTarget.AntialiasMode     = AntialiasMode.Aliased;
                renderTarget.TextAntialiasMode = TextAntialiasMode.Aliased;

                using (SharpDX.DirectWrite.Factory textFactory = new SharpDX.DirectWrite.Factory(SharpDX.DirectWrite.FactoryType.Shared))
                {
                    textFormat = new Text(textFactory,
                                          "MS Sans Serif",
                                          SharpDX.DirectWrite.FontWeight.SemiBold,
                                          SharpDX.DirectWrite.FontStyle.Normal,
                                          SharpDX.DirectWrite.FontStretch.Medium,
                                          16.0f
                                          );
                }

                renderForm.KeyDown += (o, e) =>
                {
                    if (e.Alt && e.KeyCode == System.Windows.Forms.Keys.Enter)
                    {
                        swapChain.IsFullScreen = !swapChain.IsFullScreen;
                    }
                };
            }
        }
예제 #16
0
        public static D2DRenderTargetBitmapImpl CreateCompatible(
            ImagingFactory imagingFactory,
            DirectWriteFactory dwriteFactory,
            SharpDX.Direct2D1.RenderTarget renderTarget,
            Size size)
        {
            var bitmapRenderTarget = new BitmapRenderTarget(
                renderTarget,
                CompatibleRenderTargetOptions.None,
                new Size2F((float)size.Width, (float)size.Height));

            return(new D2DRenderTargetBitmapImpl(imagingFactory, dwriteFactory, bitmapRenderTarget));
        }
예제 #17
0
        /// <summary>
        /// 设备独立资源
        /// </summary>
        /// <returns></returns>
        bool CreateDeviceIndependentResource()
        {
            if (d2dFactory == null)
            {
                d2dFactory = new SharpDX.Direct2D1.Factory(SharpDX.Direct2D1.FactoryType.MultiThreaded);
            }

            if (dwriteFactory == null)
            {
                dwriteFactory = new SharpDX.DirectWrite.Factory(SharpDX.DirectWrite.FactoryType.Shared);
            }

            return(true);
        }
예제 #18
0
 public TextWriter(IDrawing.IDevice device, System.Drawing.Font font)// float FontSize = 10, String FontName = "Arial")
 { 
     this.device = ((Device)device);
     this.FontName = font.Name;
     this.FontSize = font.Size;
    
     renderTarget =this.device.renderTarget;
     SharpDX.DirectWrite.Factory factory = new SharpDX.DirectWrite.Factory();
     SharpDX.DirectWrite.FontWeight fontW = SharpDX.DirectWrite.FontWeight.Normal;
     if(font.Style==System.Drawing.FontStyle.Bold)
         fontW = SharpDX.DirectWrite.FontWeight.Heavy;
     textFormat = new SharpDX.DirectWrite.TextFormat(factory, FontName,fontW, SharpDX.DirectWrite.FontStyle.Normal, FontSize); 
    // this.device.ChangeRenderTarget += TextWriter_ChangeRenderTarget;
 }
예제 #19
0
 /// <summary>
 /// Initializes a new instance of the <see cref="DrawingContextImpl"/> class.
 /// </summary>
 /// <param name="visualBrushRenderer">The visual brush renderer.</param>
 /// <param name="renderTarget">The render target to draw to.</param>
 /// <param name="directWriteFactory">The DirectWrite factory.</param>
 /// <param name="swapChain">An optional swap chain associated with this drawing context.</param>
 /// <param name="finishedCallback">An optional delegate to be called when context is disposed.</param>
 public DrawingContextImpl(
     IVisualBrushRenderer visualBrushRenderer,
     SharpDX.Direct2D1.RenderTarget renderTarget,
     SharpDX.DirectWrite.Factory directWriteFactory,
     SharpDX.DXGI.SwapChain1 swapChain = null,
     Action finishedCallback           = null)
 {
     _visualBrushRenderer = visualBrushRenderer;
     _renderTarget        = renderTarget;
     _swapChain           = swapChain;
     _finishedCallback    = finishedCallback;
     _directWriteFactory  = directWriteFactory;
     _swapChain           = swapChain;
     _renderTarget.BeginDraw();
 }
예제 #20
0
        private void InitializeDirectXResources()
        {
            var clientSize     = ClientSize;
            var backBufferDesc = new SharpDX.DXGI.ModeDescription(clientSize.Width, clientSize.Height,
                                                                  new SharpDX.DXGI.Rational(60, 1), SharpDX.DXGI.Format.R8G8B8A8_UNorm);

            var swapChainDesc = new SharpDX.DXGI.SwapChainDescription()
            {
                ModeDescription   = backBufferDesc,
                SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0),
                Usage             = SharpDX.DXGI.Usage.RenderTargetOutput,
                BufferCount       = 1,
                OutputHandle      = Handle,
                SwapEffect        = SharpDX.DXGI.SwapEffect.Discard,
                IsWindowed        = false
            };

            SharpDX.Direct3D11.Device.CreateWithSwapChain(SharpDX.Direct3D.DriverType.Hardware, SharpDX.Direct3D11.DeviceCreationFlags.BgraSupport,
                                                          new[] { SharpDX.Direct3D.FeatureLevel.Level_10_0 }, swapChainDesc,
                                                          out _d3DDevice, out var swapChain);
            _d3DDeviceContext = _d3DDevice.ImmediateContext;

            _swapChain = new SharpDX.DXGI.SwapChain1(swapChain.NativePointer);

            _d2DFactory = new SharpDX.Direct2D1.Factory();

            using (var backBuffer = _swapChain.GetBackBuffer <SharpDX.Direct3D11.Texture2D>(0))
            {
                _renderTargetView = new SharpDX.Direct3D11.RenderTargetView(_d3DDevice, backBuffer);
                _renderTarget     = new SharpDX.Direct2D1.RenderTarget(_d2DFactory, backBuffer.QueryInterface <SharpDX.DXGI.Surface>(),
                                                                       new SharpDX.Direct2D1.RenderTargetProperties(new SharpDX.Direct2D1.PixelFormat(SharpDX.DXGI.Format.Unknown, SharpDX.Direct2D1.AlphaMode.Premultiplied)))
                {
                    TextAntialiasMode = SharpDX.Direct2D1.TextAntialiasMode.Cleartype
                };
            }

            _solidColorBrush = new SharpDX.Direct2D1.SolidColorBrush(_renderTarget, Color.White);

            _dwFactory  = new SharpDX.DirectWrite.Factory(SharpDX.DirectWrite.FactoryType.Shared);
            _textFormat = new SharpDX.DirectWrite.TextFormat(_dwFactory, "Arial", SharpDX.DirectWrite.FontWeight.Bold,
                                                             SharpDX.DirectWrite.FontStyle.Normal, SharpDX.DirectWrite.FontStretch.Normal, 84 * (float)GraphicsUtils.Scale)
            {
                TextAlignment      = SharpDX.DirectWrite.TextAlignment.Center,
                ParagraphAlignment = SharpDX.DirectWrite.ParagraphAlignment.Center
            };
            //                var rectangleGeometry = new D2D1.RoundedRectangleGeometry(_d2DFactory,
            //                    new D2D1.RoundedRectangle() { RadiusX = 32, RadiusY = 32, Rect = new RectangleF(128, 128, Width - 128 * 2, Height - 128 * 2) });
        }
예제 #21
0
        public Renderer(IntPtr hwnd, Bounds b)
        {
            this.hwnd          = hwnd;
            this.currentBounds = b;
            d2dFactory         = new SharpDX.Direct2D1.Factory(FactoryType.SingleThreaded);
            fontFactory        = new SharpDX.DirectWrite.Factory(SharpDX.DirectWrite.FactoryType.Shared);

            var properties = new RenderTargetProperties(new PixelFormat(Format.R8G8B8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Premultiplied));

            d2dTarget = new SharpDX.Direct2D1.WindowRenderTarget(d2dFactory, properties, new HwndRenderTargetProperties
            {
                Hwnd           = hwnd,
                PixelSize      = new Size2(currentBounds.Width, currentBounds.Height),
                PresentOptions = PresentOptions.Immediately
            });
        }
예제 #22
0
        private void Form1_Load(object sender, EventArgs e)
        {
            SetLayeredWindowAttributes(Handle, (uint)0, 255, 0x00000002);

            DoubleBuffered = true;
            Width          = 64;
            Height         = 64;
            Location       = new System.Drawing.Point(0, 0);
            SetStyle(ControlStyles.OptimizedDoubleBuffer |
                     ControlStyles.AllPaintingInWmPaint |
                     ControlStyles.DoubleBuffer |
                     ControlStyles.UserPaint |
                     ControlStyles.Opaque |
                     ControlStyles.ResizeRedraw |
                     ControlStyles.SupportsTransparentBackColor, true);
            TopMost = true;
            Visible = true;

            DXFactory     = new Factory();
            DXFontFactory = new FontFactory();

            while (!IsRunning)
            {
                IsRunning = GetProcessAndHandles();
                Thread.Sleep(1);
            }

            DeviceRenderProperties = new HwndRenderTargetProperties()
            {
                Hwnd           = this.Handle,
                PixelSize      = new Size2(1920, 1080),
                PresentOptions = PresentOptions.None
            };

            //Init DirectX
            RenderDevice = new WindowRenderTarget(DXFactory, new RenderTargetProperties(new PixelFormat(Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied)), DeviceRenderProperties);

            LoopThread = new Thread(new ParameterizedThreadStart(SDXThread));

            LoopThread.Priority     = ThreadPriority.Highest;
            LoopThread.IsBackground = true;
            LoopThread.Start();

            MainForm mf = new MainForm(this);

            mf.ShowDialog();
        }
예제 #23
0
        public RenderController(IntPtr windowHandle)
        {
            writeFactory = new SharpDX.DirectWrite.Factory();

            SharpDX.Direct3D11.Device defaultDevice = new SharpDX.Direct3D11.Device
                                                      (
                DriverType.Hardware,
                DeviceCreationFlags.Debug | DeviceCreationFlags.BgraSupport
                                                      );

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

            SwapChainDescription1 description = new SwapChainDescription1()
            {
                Width             = 0,
                Height            = 0,
                Format            = Format.B8G8R8A8_UNorm,
                Stereo            = false,
                SampleDescription = new SampleDescription(1, 0),
                Usage             = Usage.RenderTargetOutput,
                BufferCount       = 2,
                Scaling           = Scaling.None,
                SwapEffect        = SwapEffect.FlipSequential,
            };

            swapChain  = new SwapChain1(dxgiFactory2, device, windowHandle, ref description);
            backBuffer = Surface.FromSwapChain(swapChain, 0);

            d2dDevice  = new SharpDX.Direct2D1.Device(dxgiDevice2);
            d2dContext = new SharpDX.Direct2D1.DeviceContext(d2dDevice, SharpDX.Direct2D1.DeviceContextOptions.None);
            properties = new BitmapProperties1
                         (
                new SharpDX.Direct2D1.PixelFormat
                (
                    SharpDX.DXGI.Format.B8G8R8A8_UNorm,
                    SharpDX.Direct2D1.AlphaMode.Premultiplied
                ),
                0, 0, BitmapOptions.Target | BitmapOptions.CannotDraw
                         );
            d2dTarget         = new Bitmap1(d2dContext, backBuffer, properties);
            d2dContext.Target = d2dTarget;

            canDraw = true;
        }
예제 #24
0
        private void RecreateResources(IVisualContainerElement root, out RenderContext context)
        {
            _dxgiFactory?.Dispose();
            _swapChain?.Dispose();
            _direct3DDevice?.Dispose();
            _directWriteFactory?.Dispose();

            CreateSwapChainAndDevice(out _swapChainDescription, out _swapChain, out _direct3DDevice);
            _dxgiDevice  = _direct3DDevice.QueryInterface <SharpDX.DXGI.Device>();
            _dxgiFactory = _swapChain.GetParent <Factory>();

            // Video (EVR) initialization.
            var multithread = _direct3DDevice.QueryInterface <DeviceMultithread>();

            multithread.SetMultithreadProtected(true);
            if (_dxgiDeviceManager == null)
            {
                try {
                    _dxgiDeviceManager = new DXGIDeviceManager();
                } catch (System.EntryPointNotFoundException) {
                    // Windows 7
                }
            }
            _dxgiDeviceManager?.ResetDevice(_direct3DDevice);

            _directWriteFactory = new SharpDX.DirectWrite.Factory();

            context        = new RenderContext(this, new Size(_swapChainDescription.ModeDescription.Width, _swapChainDescription.ModeDescription.Height));
            _renderContext = context;

            root.OnLayout();

            root.OnGotContext(context);

            _isSizeChanged = false;

            ++_controlResizeCounter;
            if (_controlResizeCounter == StartupControlResizeCount)
            {
                // WTF...
                // Look into MediaEngine and DXGIDeviceManager?
                Game.Window.Invoke(new Action(() => Game.Window.RaiseStageReady(EventArgs.Empty)));

                root.OnStageReady(context);
            }
        }
예제 #25
0
        /// <summary>
        /// Creates device independent resources.
        /// </summary>
        /// <remarks>
        /// This method is called at the initialization of this instance.
        /// </remarks>
        protected virtual void CreateDeviceIndependentResources()
        {
#if DEBUG
            var debugLevel = SharpDX.Direct2D1.DebugLevel.Information;
#else
            var debugLevel = SharpDX.Direct2D1.DebugLevel.None;
#endif
            // Dispose previous references and set to null
            Utilities.Dispose(ref d2dFactory);
            Utilities.Dispose(ref dwriteFactory);
            Utilities.Dispose(ref wicFactory);

            // Allocate new references
            d2dFactory    = new SharpDX.Direct2D1.Factory1(SharpDX.Direct2D1.FactoryType.SingleThreaded, debugLevel);
            dwriteFactory = new SharpDX.DirectWrite.Factory(SharpDX.DirectWrite.FactoryType.Shared);
            wicFactory    = new SharpDX.WIC.ImagingFactory2();
        }
예제 #26
0
        private void PlatformConstruct(GraphicsDevice graphicsDevice)
        {
            var debugLevel = DebugLevel.None;

#if DEBUG
            debugLevel = DebugLevel.Information;
#endif

            using (var dxgiDevice = graphicsDevice.Device.QueryInterface <SharpDX.DXGI.Device>())
                using (var factory = new Factory1(FactoryType.SingleThreaded, debugLevel))
                    using (var device = new Device(factory, dxgiDevice))
                    {
                        DeviceContext = AddDisposable(new DeviceContext(device, DeviceContextOptions.None));
                    }

            DirectWriteFactory = AddDisposable(new SharpDX.DirectWrite.Factory(SharpDX.DirectWrite.FactoryType.Isolated));
        }
예제 #27
0
        private GraphicCore()
        {
            form                 = new RenderForm("");
            form.Width           = System.Windows.Forms.SystemInformation.PrimaryMonitorSize.Width;
            form.Height          = System.Windows.Forms.SystemInformation.PrimaryMonitorSize.Height;
            form.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Sizable;
            form.ControlBox      = false;
            form.MaximizeBox     = false;
            var desc = new SwapChainDescription()
            {
                BufferCount     = 1,
                ModeDescription =
                    new ModeDescription(form.Width, form.Height, new Rational(frame_rate, 1), Format.R8G8B8A8_UNorm),
                IsWindowed        = true,
                OutputHandle      = form.Handle,
                SampleDescription = new SampleDescription(1, 0),
                SwapEffect        = SwapEffect.Discard,
                Usage             = Usage.RenderTargetOutput
            };

            Camera.width  = form.Width;
            Camera.height = form.Height;

            Device1.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.BgraSupport, desc,
                                        SharpDX.Direct3D10.FeatureLevel.Level_10_0, out device, out swapChain);

            factory2d   = new SharpDX.Direct2D1.Factory();
            factoryText = new SharpDX.DirectWrite.Factory();

            // Ignore all windows events
            factoryDX = swapChain.GetParent <SharpDX.DXGI.Factory>();
            //factoryDX.MakeWindowAssociation(form.Handle, WindowAssociationFlags.IgnoreAll);

            Texture2D backBuffer = Texture2D.FromSwapChain <Texture2D>(swapChain, 0);
            var       renderView = new RenderTargetView(device, backBuffer);

            Surface surface = backBuffer.QueryInterface <Surface>();

            this.render2d = new RenderTarget(factory2d, surface,
                                             new RenderTargetProperties(new SharpDX.Direct2D1.PixelFormat(Format.Unknown, AlphaMode.Premultiplied)));

            brush      = new SolidColorBrush(this.render2d, Color.White);
            _isRunning = true;
        }
예제 #28
0
        /// <summary>
        ///
        /// </summary>
        private void InitializeD2D()
        {
            mFactory2D = new SharpDX.Direct2D1.Factory();
            using (var surface = BackBuffer.QueryInterface <DXGI.Surface>())
            {
                mRenderTarget2D = new RenderTarget(mFactory2D, surface, new RenderTargetProperties(new PixelFormat(DXGI.Format.Unknown, AlphaMode.Premultiplied)));
            }
            mRenderTarget2D.AntialiasMode = AntialiasMode.PerPrimitive;

            mFactoryDWrite = new SharpDX.DirectWrite.Factory();

            SceneColorBrush = new SolidColorBrush(mRenderTarget2D, Color.White);

            // Initialize fonts
            foreach (eTextSize size in Enum.GetValues(typeof(eTextSize)))
            {
                mTextFormats.Add(size, new SharpDX.DirectWrite.TextFormat(mFactoryDWrite, "Arial", (int)size));
            }
        }
예제 #29
0
 public static async Task <IList <Font> > LoadAsync()
 {
     using (var dwFactory = new SharpDX.DirectWrite.Factory())
     {
         using (var fontCollection = dwFactory.GetSystemFontCollection(new RawBool(true)))
         {
             return(await Task.WhenAll(fontCollection.GetFontFamilies().Select(family =>
             {
                 return Task.Run(() =>
                 {
                     var familyName = family.GetFontFamilyName();
                     var result = CreateFont(familyName, family);
                     family.Dispose();
                     return result;
                 });
             })));
         }
     }
 }
예제 #30
0
    async Task LoadCustomFonts()
    {
        await Task.Run(() =>
        {
            // Font Families
            FontFamilyNames = new List <string> {
                "Aileron", "Grundschrift"
            };
            // Character codes to check for:
            int[] codes               = { 0x41, 0x6f, 0x7c, 0xc2aa, 0xD7A3 };
            FactoryDWrite             = new SharpDX.DirectWrite.Factory();
            CurrentResourceFontLoader = new ResourceFontLoader(FactoryDWrite, customFontStreams);
            CurrentFontCollection     = new FontCollection(FactoryDWrite, CurrentResourceFontLoader, CurrentResourceFontLoader.Key);

            foreach (var fontfamilyname in FontFamilyNames)
            {
                int familyIndex;
                CurrentFontCollection.FindFamilyName(fontfamilyname, out familyIndex);

                using (var fontFamily = CurrentFontCollection.GetFontFamily(familyIndex))
                {
                    var font = fontFamily.GetFont(0);

                    using (var fontface = new FontFace(font))
                    {
                        var results = fontface.GetGlyphIndices(codes);
                        for (int i = 0; i < codes.Length - 1; i++)
                        {
                            if (results[i] > 0)
                            {
                                Debug.WriteLine("Contains the unicode character " + codes[i]);
                            }
                            else
                            {
                                Debug.WriteLine("Does not contain the unicode character " + codes[i]);
                            }
                        }
                    }
                }
            }
        });
    }
예제 #31
0
        void InitializeComponent()
        {
            Factory2D     = new SharpDX.Direct2D1.Factory();
            FactoryDWrite = new SharpDX.DirectWrite.Factory();
            HwndRenderTargetProperties properties = new HwndRenderTargetProperties();

            properties.Hwnd           = this.Handle;
            properties.PixelSize      = new SharpDX.Size2(this.ClientSize.Width, this.ClientSize.Height);
            properties.PresentOptions = PresentOptions.RetainContents;



            RenderTarget2D = new WindowRenderTarget(Factory2D, new RenderTargetProperties(new PixelFormat(Format.B8G8R8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Premultiplied)), properties);

            RenderTarget2D.AntialiasMode = AntialiasMode.PerPrimitive;


            SceneColorBrush = new SolidColorBrush(RenderTarget2D, new RawColor4(1.0f, 0.0f, 0.0f, 1.0f));
            //NOT WORKING
        }
예제 #32
0
        public DeviceResources(IntPtr handle)
        {
            Factory2D      = new Factory1_2D1();
            ImagingFactory = new ImagingFactory();
            FontFactory    = new FactoryDW();
            PixelFormat    = new PixelFormat(Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied);

            var modeDescription = new ModeDescription(PixelFormat.Format);

            var swapChainDescription = new SwapChainDescription
            {
                BufferCount       = 1,
                Flags             = SwapChainFlags.AllowModeSwitch,
                IsWindowed        = false,
                ModeDescription   = modeDescription,
                OutputHandle      = handle,
                SampleDescription = new SampleDescription(1, 0),
                SwapEffect        = SwapEffect.Discard,
                Usage             = Usage.RenderTargetOutput
            };

            Device.CreateWithSwapChain(
                DriverType.Hardware,
                DeviceCreationFlags.BgraSupport,
                swapChainDescription,
                out Device device3D,
                out SwapChain swapChain
                );
            SwapChain = swapChain;

            using (var dxgiDevice = device3D.QueryInterface <SharpDX.DXGI.Device>())
            {
                Device2D = new Device2D1(Factory2D, dxgiDevice);
            }

            device3D.Dispose();

            CreateDeviceContext();
        }
예제 #33
0
        public OverlayDX(AutoIt autoIt)
        {
            this.autoIt = autoIt;
            formWidth   = autoIt.window.Width;
            formHeight  = autoIt.window.Height;

            overlayForm = new RenderForm("SharpDX")
            {
                TransparencyKey = System.Drawing.Color.Black,
                TopMost         = true,
                Enabled         = true,
                ShowIcon        = false,
                FormBorderStyle = FormBorderStyle.None,
                //FormBorderStyle = FormBorderStyle.FixedToolWindow,
                Width  = formWidth,
                Height = formHeight,
            };


            Factory factory2D = new Factory();

            HwndRenderTargetProperties properties = new HwndRenderTargetProperties
            {
                Hwnd           = overlayForm.Handle,
                PixelSize      = new SharpDX.Size2(overlayForm.Width, overlayForm.Height),
                PresentOptions = PresentOptions.Immediately
            };

            renderTarget = new WindowRenderTarget(factory2D, new RenderTargetProperties(new PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied)), properties);
            renderTarget.AntialiasMode     = AntialiasMode.PerPrimitive;
            renderTarget.TextAntialiasMode = TextAntialiasMode.Cleartype;
            renderTarget.StrokeWidth       = 1.5f;

            brush = new SolidColorBrush(renderTarget, new RawColor4(0, 1, 0, 0.8f));

            DWfactory  = new SharpDX.DirectWrite.Factory();
            textFormat = new SharpDX.DirectWrite.TextFormat(DWfactory, "Calibri", 16);
        }
예제 #34
0
        public RenderTargetBitmapImpl(
            ImagingFactory imagingFactory,
            Factory d2dFactory,
            DirectWriteFactory dwriteFactory,
            int width,
            int height,
            double dpiX,
            double dpiY)
            : base(imagingFactory, width, height)
        {
            var props = new RenderTargetProperties
            {
                DpiX = (float)dpiX,
                DpiY = (float)dpiY,
            };

            _target = new WicRenderTarget(
                d2dFactory,
                WicImpl,
                props);

            _dwriteFactory = dwriteFactory;
        }
        private void Initialize()
        {
            // 创建 Direct2D 单线程工厂。
            Factory factory = new Factory(FactoryType.MultiThreaded);

            FactoryDWrite = new SharpDX.DirectWrite.Factory();

            // 渲染参数。

            var d2PixelFormat = new PixelFormat(SharpDX.DXGI.Format.R8G8B8A8_UNorm, AlphaMode.Premultiplied);
            RenderTargetProperties renderProps = new RenderTargetProperties
            {
                PixelFormat = d2PixelFormat,
                Usage       = RenderTargetUsage.None,
                Type        = RenderTargetType.Default
            };
            // 渲染目标属性。
            HwndRenderTargetProperties hwndProps = new HwndRenderTargetProperties()
            {
                // 承载控件的句柄。
                Hwnd = this.Handle,
                // 控件的尺寸。
                PixelSize      = new SharpDX.Size2(this.ClientSize.Width, this.ClientSize.Height),
                PresentOptions = PresentOptions.None
            };

            // 渲染目标。

            RenderTarget2D = new WindowRenderTarget(factory, renderProps, hwndProps);
            RenderTarget2D.AntialiasMode = AntialiasMode.Aliased;
            if (Antialias)
            {
                RenderTarget2D.AntialiasMode = AntialiasMode.PerPrimitive;
            }

            g = new D2DGraphics(RenderTarget2D, FactoryDWrite);
        }
 public Direct2DFactoryManager()
 {
   wicFactory = new SharpDX.WIC.ImagingFactory();
   d2DFactory = new SharpDX.Direct2D1.Factory();
   dwFactory = new SharpDX.DirectWrite.Factory();
 }
예제 #37
0
		public D3D11_2D1(Device11 drawdevice, Device10 textdevice)
			: base(false) // nothing!
		{
			if (drawdevice == null || textdevice == null)
			{
				using (var dg = new DisposeGroup())
				{
					if (drawdevice == null && textdevice == null)
					{
						Adapter a = null;
						foreach (var item in DeviceUtil.GetAdapters(dg))
						{
							if (!item.IsInterfaceSupported<Device10>())
								continue;
							if (Device11.GetSupportedFeatureLevel(item) < Direct3D.FeatureLevel.Level_10_1)
								continue;
							a = item;
							break;
						}
						device = new Device11(a, DeviceCreationFlags.BgraSupport | DeviceCreationFlags.SingleThreaded | DeviceCreationFlags.Debug);
						device10 = new Device10(a, Direct3D10.DeviceCreationFlags.BgraSupport | Direct3D10.DeviceCreationFlags.Singlethreaded | Direct3D10.DeviceCreationFlags.Debug);
					}
					else
					{
						if (drawdevice == null)
						{
							using (var xgidtext = textdevice.QueryInterface<DeviceXGI>())
								device = new Device11(xgidtext.Adapter, DeviceCreationFlags.BgraSupport | DeviceCreationFlags.SingleThreaded | DeviceCreationFlags.Debug);
							textdevice.AddReference();
							device10 = textdevice;
						}
						else
						{
							using (var xgiddraw = drawdevice.QueryInterface<DeviceXGI>())
								device10 = new Device10(xgiddraw.Adapter, Direct3D10.DeviceCreationFlags.BgraSupport | Direct3D10.DeviceCreationFlags.Singlethreaded | Direct3D10.DeviceCreationFlags.Debug);
							drawdevice.AddReference();
							device = drawdevice;
						}
					}
				}
			}
			else
			{
				using (var xgidev10 = device10.QueryInterface<DeviceXGI>())
				using (var xgidev11 = device.QueryInterface<DeviceXGI>())
				{
					if (xgidev10.Adapter.NativePointer != xgidev11.Adapter.NativePointer)
						throw new ArgumentException("drawdevice.Adapter.NativePointer != textdevice.Adapter.NativePointer");
				}
				textdevice.AddReference();
				drawdevice.AddReference();
				device = drawdevice;
				device10 = textdevice;
			}

			factory2D = new SharpDX.Direct2D1.Factory();
			factoryDW = new FactoryDW();
		}
예제 #38
0
        void InitD2D()
        {
            _factoryD2D = new SharpDX.Direct2D1.Factory();
            _factoryDWrite = new SharpDX.DirectWrite.Factory();

            HwndRenderTargetProperties properties = new HwndRenderTargetProperties();
            properties.Hwnd = this.Handle;
            properties.PixelSize = new SharpDX.Size2(this.Width, this.Height);
            properties.PresentOptions = PresentOptions.Immediately;

            _renderTarget = new WindowRenderTarget(_factoryD2D,
                new RenderTargetProperties(_renderTargetType,
                    new PixelFormat(Format.Unknown, AlphaMode.Premultiplied), 96.0f, 96.0f,
                    RenderTargetUsage.GdiCompatible, FeatureLevel.Level_DEFAULT), properties);
        }
예제 #39
0
 public static void LoadFactories()
 {
     Factory2D = new Factory(FactoryType.MultiThreaded);
     FactoryWrite = new SharpDX.DirectWrite.Factory();
 }
예제 #40
0
        /* DEVICE MANAGER METHODS */
        /// <summary>
        /// Initialize resources and trigger an initialization event for all registered listeners
        /// </summary>
        public void Initialize(Tesseract gameEngine)
        {
            // Release any pre-exisitng references
            ReleaseResources();

            // Retrieve the Direct3D 11.1 device
            using (var device = new Device(DriverType.Hardware, DeviceCreationFlags.BgraSupport, FeatureLevel))
            {
                Device3D = ToDispose(device.QueryInterface<Device1>());
            }

            // Get the Direct3D 11.1 context
            Context3D = ToDispose(Device3D.ImmediateContext.QueryInterface<DeviceContext1>());

            // Create the remaining references
            Factory2D = ToDispose(new SharpDX.Direct2D1.Factory1(SharpDX.Direct2D1.FactoryType.SingleThreaded));
            FactoryDW = ToDispose(new SharpDX.DirectWrite.Factory(SharpDX.DirectWrite.FactoryType.Shared));
            FactoryWIC = ToDispose(new SharpDX.WIC.ImagingFactory2());

            // Create the Direct2D device
            using (var device = Device3D.QueryInterface<SharpDX.DXGI.Device>())
            {
                Device2D = ToDispose(new SharpDX.Direct2D1.Device(Factory2D, device));
            }

            // Create the Direct2D context
            Context2D = ToDispose(new SharpDX.Direct2D1.DeviceContext(Device2D, SharpDX.Direct2D1.DeviceContextOptions.None));
        }
        /// <summary>
        /// Creates device manager objects
        /// </summary>
        /// <remarks>
        /// This method is called at the initialization of this instance.
        /// </remarks>
        protected virtual void CreateInstances()
        {
            // Dispose previous references and set to null
            RemoveAndDispose(ref d3dDevice);
            RemoveAndDispose(ref d3dContext);
            RemoveAndDispose(ref d2dDevice);
            RemoveAndDispose(ref d2dContext);
            RemoveAndDispose(ref d2dFactory);
            RemoveAndDispose(ref dwriteFactory);
            RemoveAndDispose(ref wicFactory);

            #region Create Direct3D 11.1 device and retrieve device context

            // Bgra performs better especially with Direct2D software
            // render targets
            var creationFlags = DeviceCreationFlags.BgraSupport;
            #if DEBUG
            // Enable D3D device debug layer
            creationFlags |= DeviceCreationFlags.Debug;
            #endif

            // Retrieve the Direct3D 11.1 device and device context
            using (var device = new Device(DriverType.Hardware, creationFlags, Direct3DFeatureLevels))
            {
                d3dDevice = ToDispose(device.QueryInterface<Device1>());
            }

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

            #region Create Direct2D device and context

            #if DEBUG
            var debugLevel = SharpDX.Direct2D1.DebugLevel.Information;
            #else
            var debugLevel = SharpDX.Direct2D1.DebugLevel.None;
            #endif

            // Allocate new references
            d2dFactory = ToDispose(new SharpDX.Direct2D1.Factory1(SharpDX.Direct2D1.FactoryType.SingleThreaded, debugLevel));
            dwriteFactory = ToDispose(new SharpDX.DirectWrite.Factory(SharpDX.DirectWrite.FactoryType.Shared));
            wicFactory = ToDispose(new SharpDX.WIC.ImagingFactory2());

            // 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));
            #endregion
        }
예제 #42
0
파일: GameForm.cs 프로젝트: DarkLotus/TD
        /// <summary>
        /// Setup DirectX Renderer.
        /// </summary>
        private void SetupDX()
        {
            d2dFactory = new SharpDX.Direct2D1.Factory();
            fontFactory = new SharpDX.DirectWrite.Factory();

            HwndRenderTargetProperties properties = new HwndRenderTargetProperties();
            properties.Hwnd = this.Handle;
            properties.PixelSize = new System.Drawing.Size(this.Width, this.Height);
            properties.PresentOptions = PresentOptions.None;

            d2dRenderTarget = new WindowRenderTarget(d2dFactory, new RenderTargetProperties(new PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm, AlphaMode.Ignore)), properties);
            d2dRenderTarget.AntialiasMode = AntialiasMode.Aliased;
            d2dRenderTarget.TextAntialiasMode = TextAntialiasMode.Cleartype;

            EmptyTileBrush = new SolidColorBrush(d2dRenderTarget, Colors.Aquamarine);
            solidColorBrush = new SolidColorBrush(d2dRenderTarget, Colors.White);
            MonsterBrush = new SolidColorBrush(d2dRenderTarget, Colors.Wheat);
            TowerBrush = new SolidColorBrush(d2dRenderTarget, Colors.Purple);
        }
예제 #43
0
파일: D2D1.cs 프로젝트: rookboom/Flow
 /// <summary>
 /// 
 /// </summary>
 /// <param name="device"></param>
 public D2D1(SharpDX.Direct3D10.Device1 device)
     : base(device)
 {
     m_factory2D = new SharpDX.Direct2D1.Factory();
     m_factoryDW = new FactoryDW();
 }
        /// <summary>
        /// Creates device independent resources.
        /// </summary>
        /// <remarks>
        /// This method is called at the initialization of this instance.
        /// </remarks>
        protected virtual void CreateDeviceIndependentResources()
        {
            #if DEBUG
            var debugLevel = SharpDX.Direct2D1.DebugLevel.Information;
            #else
            var debugLevel = SharpDX.Direct2D1.DebugLevel.None;
            #endif
            // Dispose previous references and set to null
            RemoveAndDispose(ref d2dFactory);
            RemoveAndDispose(ref dwriteFactory);
            RemoveAndDispose(ref wicFactory);

            // Allocate new references
            d2dFactory = ToDispose(new SharpDX.Direct2D1.Factory1(SharpDX.Direct2D1.FactoryType.SingleThreaded, debugLevel));
            dwriteFactory = ToDispose(new SharpDX.DirectWrite.Factory(SharpDX.DirectWrite.FactoryType.Shared));
            wicFactory = ToDispose(new SharpDX.WIC.ImagingFactory2());
        }
예제 #45
0
        private void LoadPipeline(RenderForm form)
        {
            int width = form.ClientSize.Width;
            int height = form.ClientSize.Height;

            viewport.Width = width;
            viewport.Height = height;
            viewport.MaxDepth = 1.0f;

            scissorRect.Right = width;
            scissorRect.Bottom = height;

            #if DEBUG
            // Enable the D3D12 debug layer.
            {
                DebugInterface.Get().EnableDebugLayer();
            }
            #endif
            device = new Device(null, SharpDX.Direct3D.FeatureLevel.Level_12_0);
            using (var factory = new Factory4())
            {

                // Describe and create the command queue.
                CommandQueueDescription queueDesc = new CommandQueueDescription(CommandListType.Direct);
                commandQueue = device.CreateCommandQueue(queueDesc);

                // Describe and create the swap chain.
                SwapChainDescription swapChainDesc = new SwapChainDescription()
                {
                    BufferCount = FrameCount,
                    ModeDescription = new ModeDescription(width, height, new Rational(60, 1), Format.R8G8B8A8_UNorm),
                    Usage = Usage.RenderTargetOutput,
                    SwapEffect = SwapEffect.FlipDiscard,
                    OutputHandle = form.Handle,
                    //Flags = SwapChainFlags.None,
                    SampleDescription = new SampleDescription(1, 0),
                    IsWindowed = true
                };

                SwapChain tempSwapChain = new SwapChain(factory, commandQueue, swapChainDesc);
                swapChain = tempSwapChain.QueryInterface<SwapChain3>();
                tempSwapChain.Dispose();
                frameIndex = swapChain.CurrentBackBufferIndex;
            }

            // Create descriptor heaps.
            // Describe and create a render target view (RTV) descriptor heap.
            DescriptorHeapDescription rtvHeapDesc = new DescriptorHeapDescription()
            {
                DescriptorCount = FrameCount,
                Flags = DescriptorHeapFlags.None,
                Type = DescriptorHeapType.RenderTargetView
            };

            renderTargetViewHeap = device.CreateDescriptorHeap(rtvHeapDesc);

            rtvDescriptorSize = device.GetDescriptorHandleIncrementSize(DescriptorHeapType.RenderTargetView);

            //Init Direct3D11 device from Direct3D12 device
            device11 = SharpDX.Direct3D11.Device.CreateFromDirect3D12(device, SharpDX.Direct3D11.DeviceCreationFlags.BgraSupport, null, null, commandQueue);
            deviceContext11 = device11.ImmediateContext;
            device11on12 = device11.QueryInterface<SharpDX.Direct3D11.ID3D11On12Device>();
            var d2dFactory = new SharpDX.Direct2D1.Factory(SharpDX.Direct2D1.FactoryType.MultiThreaded);

            // Create frame resources.
            CpuDescriptorHandle rtvHandle = renderTargetViewHeap.CPUDescriptorHandleForHeapStart;
            for (int n = 0; n < FrameCount; n++)
            {
                renderTargets[n] = swapChain.GetBackBuffer<Resource>(n);
                device.CreateRenderTargetView(renderTargets[n], null, rtvHandle);
                rtvHandle += rtvDescriptorSize;

                //init Direct2D surfaces
                SharpDX.Direct3D11.D3D11ResourceFlags format = new SharpDX.Direct3D11.D3D11ResourceFlags()
                {
                    BindFlags = (int)SharpDX.Direct3D11.BindFlags.RenderTarget,
                    CPUAccessFlags = (int)SharpDX.Direct3D11.CpuAccessFlags.None
                };

                device11on12.CreateWrappedResource(
                    renderTargets[n], format,
                    (int)ResourceStates.Present,
                    (int)ResourceStates.RenderTarget,
                    typeof(SharpDX.Direct3D11.Resource).GUID,
                    out wrappedBackBuffers[n]);

                //Init direct2D surface
                var d2dSurface = wrappedBackBuffers[n].QueryInterface<Surface>();
                direct2DRenderTarget[n] = new SharpDX.Direct2D1.RenderTarget(d2dFactory, d2dSurface, new SharpDX.Direct2D1.RenderTargetProperties(new SharpDX.Direct2D1.PixelFormat(Format.Unknown, SharpDX.Direct2D1.AlphaMode.Premultiplied)));
                d2dSurface.Dispose();
            }

            commandAllocator = device.CreateCommandAllocator(CommandListType.Direct);

            d2dFactory.Dispose();

            //Init font
            var directWriteFactory = new SharpDX.DirectWrite.Factory();
            textFormat = new SharpDX.DirectWrite.TextFormat(directWriteFactory, "Arial", SharpDX.DirectWrite.FontWeight.Bold, SharpDX.DirectWrite.FontStyle.Normal, 48) { TextAlignment = SharpDX.DirectWrite.TextAlignment.Leading, ParagraphAlignment = SharpDX.DirectWrite.ParagraphAlignment.Near };
            textBrush = new SharpDX.Direct2D1.SolidColorBrush(direct2DRenderTarget[0], Color.White);
            directWriteFactory.Dispose();
        }