Exemplo n.º 1
0
        /// <summary>
        /// Creates and initializes GPU resources.
        /// </summary>
        void CreateDeviceResources()
        {
            // create the Direct3D device
            D3D.FeatureLevel  actualLevel;
            D3D.DeviceContext d3dContext = null;
            var d3dDevice = new D3D.Device(IntPtr.Zero);
            var result    = HResult.Ok;

            for (int i = 0; i <= 1; i++)
            {
                // use WARP if hardware is not available
                var dt = i == 0 ? D3D.DriverType.Hardware : D3D.DriverType.Warp;
                result = D3D.D3D11.CreateDevice(null, dt, IntPtr.Zero, D3D.DeviceCreationFlags.BgraSupport | D3D.DeviceCreationFlags.SingleThreaded,
                                                null, 0, D3D.D3D11.SdkVersion, d3dDevice, out actualLevel, out d3dContext);
                if (result.Code != unchecked ((int)0x887A0004)) // DXGI_ERROR_UNSUPPORTED
                {
                    break;
                }
            }
            result.CheckError();
            d3dContext.Dispose();

            // store the DXGI device (for trimming when the application is being suspended)
            _dxgiDevice = d3dDevice.QueryInterface <DXGI.Device>();
            d3dDevice.Dispose();

            // create a RenderTarget (DeviceContext for Direct2D drawing)
            var d2dDevice = D2D.Device1.Create(_d2dFactory, _dxgiDevice);
            var rt        = D2D.DeviceContext1.Create(d2dDevice, D2D.DeviceContextOptions.None);

            d2dDevice.Dispose();
            rt.SetUnitMode(D2D.UnitMode.Pixels);
            _d2dContext = rt;

            // create built-in effects
            _shadow          = D2D.Effects.Shadow.Create(rt);
            _affineTransform = D2D.Effects.AffineTransform2D.Create(rt);
            _composite       = D2D.Effects.Composite.Create(rt);
            _displacementMap = D2D.Effects.DisplacementMap.Create(rt);
            _turbulence      = D2D.Effects.Turbulence.Create(rt);
            _tile            = D2D.Effects.Tile.Create(rt);
            _gaussianBlur    = D2D.Effects.GaussianBlur.Create(rt);
            _convolveMatrix  = D2D.Effects.ConvolveMatrix.Create(rt);
            _colorMatrix     = D2D.Effects.ColorMatrix.Create(rt);
            _flood           = D2D.Effects.Flood.Create(rt);
            _crop            = D2D.Effects.Crop.Create(rt);

            // create a brush and TextLayout for drawing text on the image
            _brush = D2D.SolidColorBrush.Create(rt, ColorF.Red);

            var format = DW.TextFormat.Create(_dwFactory, "Gabriola", DW.FontWeight.Bold, DW.FontStyle.Normal, 28f);

            _textLayout = DW.TextLayout1.Create(_dwFactory, "Direct2D Effects", format, 350f, 100f);
            format.Dispose();

            // add a font feature for more beautiful text
            var typo        = DW.Typography.Create(_dwFactory);
            var fontFeature = new DW.FontFeature(DW.FontFeatureTag.StylisticSet7, 1);

            typo.AddFontFeature(fontFeature);
            _textLayout.SetTypography(typo, new DW.TextRange(0, 100));
            typo.Dispose();
        }