Exemple #1
0
        /// <summary>
        /// Configures resources that don't depend on the Direct3D device.
        /// </summary>
        private void CreateDeviceIndependentResources()
        {
            // Dispose previous references and set to null
            this.RemoveAndDispose(ref d2dFactory);
            this.RemoveAndDispose(ref dwriteFactory);
            this.RemoveAndDispose(ref wicFactory);

            // Initialize Direct2D resources.
            var debugLevel = SharpDX.Direct2D1.DebugLevel.None;

#if DEBUG
            if (DirectXHelper.SdkLayersAvailable())
            {
                debugLevel = SharpDX.Direct2D1.DebugLevel.Information;
            }
#endif

            // Initialize the Direct2D Factory.
            d2dFactory = this.ToDispose(
                new SharpDX.Direct2D1.Factory2(
                    SharpDX.Direct2D1.FactoryType.SingleThreaded,
                    debugLevel
                    )
                );

            // Initialize the DirectWrite Factory.
            dwriteFactory = this.ToDispose(
                new SharpDX.DirectWrite.Factory1(SharpDX.DirectWrite.FactoryType.Shared)
                );

            // Initialize the Windows Imaging Component (WIC) Factory.
            wicFactory = this.ToDispose(
                new SharpDX.WIC.ImagingFactory2()
                );
        }
Exemple #2
0
        /// <summary>
        /// Handles the <see cref="IGraphicsDeviceService.DeviceCreated"/> event.
        /// Initializes the <see cref="Direct2DService.Device"/> and <see cref="Direct2DService.Context"/>.
        /// </summary>
        /// <param name="sender">Ignored.</param>
        /// <param name="e">Ignored.</param>
        private void HandleDeviceCreated(object sender, EventArgs e)
        {
            var d3D11Device = (SharpDX.Direct3D11.Device)_graphicsDeviceService.GraphicsDevice;

            using (var dxgiDevice = d3D11Device.QueryInterface <SharpDX.DXGI.Device>())
            {
                _device = new Device(dxgiDevice, new CreationProperties {
                    DebugLevel = D2DDebugLevel
                });
                _deviceContext = new DeviceContext(_device, DeviceContextOptions.EnableMultithreadedOptimizations);
            }

            _dwFactory = new SharpDX.DirectWrite.Factory1();
        }
Exemple #3
0
        public Direct2DDevice(IServiceRegistry services, SharpDX.Direct3D11.Device d3dDevice, DebugLevel debugLevel)
        {
            this.services = services;
            using (var dxgiDevice = d3dDevice.QueryInterface <SharpDX.DXGI.Device>())
            {
                factory       = ToDispose(new SharpDX.Direct2D1.Factory1(FactoryType.SingleThreaded, debugLevel));
                device        = ToDispose(new Device(factory, dxgiDevice));
                deviceContext = ToDispose(new DeviceContext(device, DeviceContextOptions.None));
            }

            backBuffer = ToDispose(BackBufferSurface.New(this));
            backBuffer.Initialize();

            directWriteFactory = ToDispose(new DWFactory());
        }
Exemple #4
0
        private void Initialize()
        {
            _d2dFactory   = new SharpDX.Direct2D1.Factory1();
            DWriteFactory = new SharpDX.DirectWrite.Factory1();
            WicFactory    = new ImagingFactory2();

            DevicePixelFormat = new SharpDX.Direct2D1.PixelFormat(SharpDX.DXGI.Format.R8G8B8A8_UNorm, AlphaMode.Premultiplied);
            ImagePixelFormat  = SharpDX.WIC.PixelFormat.Format32bppPRGBA;
            using (var defaultDevice = new SharpDX.Direct3D11.Device(DriverType.Hardware, DeviceCreationFlags.BgraSupport))
                using (var d3dDevice = defaultDevice.QueryInterface <SharpDX.Direct3D11.Device1>())
                    using (var dxgiDevice = d3dDevice.QueryInterface <SharpDX.DXGI.Device2>())
                    {
                        _d2dDevice    = new SharpDX.Direct2D1.Device(_d2dFactory, dxgiDevice);
                        DeviceContext = new SharpDX.Direct2D1.DeviceContext(_d2dDevice, new DeviceContextOptions());
                    }

            WicImageEncoder = new ImageEncoder(WicFactory, _d2dDevice);
            DeviceContext.TextAntialiasMode = SharpDX.Direct2D1.TextAntialiasMode.Grayscale;
        }
Exemple #5
0
        internal static void Init()
        {
            Logger.Info("GraphicsManager initializing.");

            // Direct3D11 Init ---
            ResizeNextFrame = true;

            Factory        = new FactoryDXGI();
            ImagingFactory = new FactoryWIC();
            Factory2D      = new FactoryD2D(FactoryType.MultiThreaded,
                                            Engine.IsDebug ? DebugLevel.Error : DebugLevel.None);
            DirectWriteFactory = new FactoryDW(SharpDX.DirectWrite.FactoryType.Shared);

            CreateDevices(RenderSettings.GraphicsAdapter);

            Brushes =
                new MemoizingMRUCache <Colour, SolidColorBrush>(
                    (colour, _) => new SolidColorBrush(Context2D, (Color4)colour)
            {
                Opacity = colour.A
            }, int.MaxValue,
                    brush => brush.Dispose());
            // ------------------------------------

            Engine.HandleSet += hwnd =>
            {
                var camera = Game.Workspace.CurrentCamera;
                camera.RenderHandle = hwnd;
            };

            if (Engine.Handle != IntPtr.Zero)
            {
                var camera = Game.Workspace.CurrentCamera;
                camera.RenderHandle = Engine.Handle;
            }

            SamplerStates.Load();
            Shaders.Init();
            BlendStates.Load();
            DepthStencilStates.Load();
            RasterizerStates.Load();

            //StandardConstants = new ConstantBuffer<StandardConstantData>();

            StandardShader = Shaders.Get("Standard");
            MainPass       = StandardShader.GetPass();
            LightingShader = Shaders.Get("Lighting");
            LightingPass   = LightingShader.GetPass();

            PostProcessShader = Shaders.Get("PostProcess");

            SkyboxShader = Shaders.Get("Skybox");
            SkyboxPass   = SkyboxShader.GetPass();

            AdornShader      = Shaders.Get("Adorn");
            AALinePass       = AdornShader.GetPass("AALine");
            AdornSelfLitPass = AdornShader.GetPass("AdornSelfLit");

            Shadows.Init();

            // ------------------

            IsInitialized = true;
            Initialized?.Invoke();
            Initialized = null;

            //PixHelper.AllowProfiling(Engine.IsDebug);

            Logger.Info("Renderer initialized.");
        }