Example #1
0
        /// <summary>
        ///
        /// </summary>
        protected void InitializeD3D10(int pWidth, int pHeight)
        {
            // SwapChain description
            var desc = new DXGI.SwapChainDescription()
            {
                BufferCount       = 1,
                ModeDescription   = new DXGI.ModeDescription(pWidth, pHeight, new DXGI.Rational(60, 1), DXGI.Format.R8G8B8A8_UNorm),
                IsWindowed        = true,
                OutputHandle      = DisplayHandle,
                SampleDescription = new DXGI.SampleDescription(1, 0),
                SwapEffect        = DXGI.SwapEffect.Discard,
                Usage             = DXGI.Usage.RenderTargetOutput
            };

            // Create Device and SwapChain
            SharpDX.Direct3D10.Device1.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.BgraSupport, desc, SharpDX.Direct3D10.FeatureLevel.Level_10_0, out mDevice, out mSwapChain);

            // Ignore all windows events
            DXGI.Factory factory = mSwapChain.GetParent <DXGI.Factory>();
            factory.MakeWindowAssociation(DisplayHandle, DXGI.WindowAssociationFlags.IgnoreAll);

            // New RenderTargetView from the backbuffer
            mBackBuffer     = Texture2D.FromSwapChain <Texture2D>(mSwapChain, 0);
            mBackBufferView = new RenderTargetView(mDevice, mBackBuffer);
        }
Example #2
0
        protected void InitialiseComponents()
        {
            SharpDX.Configuration.EnableObjectTracking = true;

            var description = new DXGI.SwapChainDescription {
                BufferCount     = 1,
                ModeDescription =
                    new DXGI.ModeDescription(Width, Height, new DXGI.Rational(60, 1), DXGI.Format.B8G8R8A8_UNorm),
                IsWindowed        = true,
                OutputHandle      = Handle,
                SampleDescription = new DXGI.SampleDescription(1, 0),
                SwapEffect        = DXGI.SwapEffect.Discard,
                Usage             = DXGI.Usage.RenderTargetOutput
            };

            Direct3D.Device1.CreateWithSwapChain(Direct3D.DriverType.Hardware,
                                                 Direct3D.DeviceCreationFlags.BgraSupport, description, Direct3D.FeatureLevel.Level_10_0,
                                                 out device, out swapChain);

            var dxgiFactory = swapChain.GetParent <DXGI.Factory>();

            dxgiFactory.MakeWindowAssociation(Handle, DXGI.WindowAssociationFlags.IgnoreAll);

            directWriteFactory = new DirectWrite.Factory();

            CreateSizeDependentComponents();
            children.ForEach(c => c.SetContext(this));
        }
Example #3
0
        public SwapChain(Form form, Renderer renderer, int fullScreenWidth, int fullScreenHeight)
        {
            if (fullScreenWidth == 0 || fullScreenHeight == 0)
            {
                fullScreenWidth  = SystemInformation.VirtualScreen.Width;
                fullScreenHeight = SystemInformation.VirtualScreen.Height;
            }
            this.fullScreenWidth  = fullScreenWidth;
            this.fullScreenHeight = fullScreenHeight;

            formWidth  = form.ClientSize.Width;
            formHeight = form.ClientSize.Height;

            var swapChainDescriptor = new SwapChainDescription()
            {
                BufferCount       = 1,
                Usage             = Usage.RenderTargetOutput,
                Flags             = SwapChainFlags.AllowModeSwitch,
                IsWindowed        = true,
                ModeDescription   = new ModeDescription(form.ClientSize.Width, form.ClientSize.Height, new Rational(0, 1), Format.R8G8B8A8_UNorm),
                OutputHandle      = form.Handle,
                SampleDescription = new SampleDescription(1, 0),
                SwapEffect        = SwapEffect.Discard
            };

            using (var factory = new Factory1()) {
                swapChain = new DXSwapChain(factory, renderer.Device, swapChainDescriptor);
            }
            using (var resource = Resource.FromSwapChain <Texture2D>(swapChain, 0)) {
                RenderTarget = new RenderTargetGroup(resource);
            }

            using (var fac = swapChain.GetParent <Factory>()) {
                fac.MakeWindowAssociation(form.Handle, WindowAssociationFlags.IgnoreAltEnter);
            }

            form.ResizeBegin += (o, e) => {
                formHeight = ((Form)o).Height;
                formWidth  = ((Form)o).Width;
            };
            form.ResizeBegin += (o, e) => {
                isResizing = true;
            };
            form.ResizeEnd += (o, e) => {
                isResizing = false;
                HandleResize(o, e);
            };
            form.KeyDown += HandleKeyDown;

            form.SizeChanged += HandleResize;
        }
        private void InitializeDevices()
        {
            try
            {
                SwapChainDescription                   = new DXGI.SwapChainDescription();
                SwapChainDescription.BufferCount       = 2;
                SwapChainDescription.SampleDescription = new DXGI.SampleDescription(1, 0);
                SwapChainDescription.SwapEffect        = DXGI.SwapEffect.Discard;
                SwapChainDescription.Usage             = DXGI.Usage.BackBuffer | DXGI.Usage.RenderTargetOutput;
                SwapChainDescription.IsWindowed        = true;
                SwapChainDescription.ModeDescription   = new DXGI.ModeDescription(GameWindow.Current.WindowParameters.Width, GameWindow.Current.WindowParameters.Height, new DXGI.Rational(60, 1), DXGI.Format.B8G8R8A8_UNorm);
                SwapChainDescription.OutputHandle      = GameWindowHandle;

                D3D11.Device.CreateWithSwapChain(DriverType.Hardware, D3D11.DeviceCreationFlags.BgraSupport, featureLevels, SwapChainDescription, out D3DDefaultDevice, out SwapChain);

                DXGI.Factory factory = SwapChain.GetParent <DXGI.Factory>();
                factory.MakeWindowAssociation(GameWindowHandle, DXGI.WindowAssociationFlags.IgnoreAll);

                D3DDevice = D3DDefaultDevice.QueryInterface <D3D11.Device1>();

                Backbuffer       = D3D11.Texture2D.FromSwapChain <D3D11.Texture2D>(SwapChain, 0);
                RenderTargetView = new D3D11.RenderTargetView(D3DDevice, Backbuffer);
                D3DDevice.ImmediateContext.Rasterizer.SetViewport(0, 0, GameWindow.Current.WindowParameters.Width, GameWindow.Current.WindowParameters.Height);
                D3DDevice.ImmediateContext.OutputMerger.SetTargets(RenderTargetView);

                DXGIDevice = D3DDevice.QueryInterface <DXGI.Device>();

                D2DFactory       = new D2D1.Factory1(D2D1.FactoryType.MultiThreaded);
                D2DDevice        = new D2D1.Device(D2DFactory, DXGIDevice);
                D2DDeviceContext = new D2D1.DeviceContext(D2DDevice, D2D1.DeviceContextOptions.None);

                RenderTargetSurface        = Backbuffer.QueryInterface <DXGI.Surface>();
                RenderTarget               = new D2D1.RenderTarget(D2DFactory, RenderTargetSurface, new D2D1.RenderTargetProperties(new D2D1.PixelFormat(DXGI.Format.Unknown, D2D1.AlphaMode.Premultiplied)));
                RenderTarget.AntialiasMode = D2D1.AntialiasMode.PerPrimitive;

                // Initialize debug drawings brushes
                DrawingBoundsBrush  = new D2D1.SolidColorBrush(RenderTarget, new SharpDX.Color(1f, 1f, 0f));
                CollisionBoxesBrush = new D2D1.SolidColorBrush(RenderTarget, new SharpDX.Color(1f, 0f, 0f));

                RenderFrame = new RenderFrame(RenderTarget);

                Clock = Stopwatch.StartNew();
            }
            catch (Exception ex)
            {
                throw new DeviceInitializationException("Unable to initialize DirectX device!", ex);
            }
        }
        private void ConfigureAltEnter()
        {
            // https://katyscode.wordpress.com/2013/08/24/c-directx-api-face-off-slimdx-vs-sharpdx-which-should-you-choose/
            // Disable automatic ALT+Enter processing because it doesn't work properly with WinForms
            using (var factory = swapChain.GetParent <SharpDX.DXGI.Factory1>())
                factory.MakeWindowAssociation(renderForm.Handle, WindowAssociationFlags.IgnoreAltEnter);

            // Add event handler for ALT+Enter
            renderForm.KeyDown += (o, e) =>
            {
                if (e.Alt && e.KeyCode == Keys.Enter)
                {
                    swapChain.IsFullScreen = !swapChain.IsFullScreen;
                }
            };

            // Set window size
            renderForm.Size = new System.Drawing.Size(RenderWidth, RenderHeight);

            // Prevent window from being re-sized
            renderForm.AutoSizeMode = AutoSizeMode.GrowAndShrink;
        }
Example #6
0
        void CreateDevice(System.Drawing.Size size, IntPtr hwnd)
        {
            RenderViewSize = size;
            ControlHandle = hwnd;

            //create device and swapchain
            DriverType driverType = DriverType.Hardware;
            DeviceCreationFlags flags = DeviceCreationFlags.None;
            if (Config.DebugMode) flags |= DeviceCreationFlags.Debug;

            FeatureLevel[] levels = new FeatureLevel[] { FeatureLevel.Level_11_0, FeatureLevel.Level_10_1, FeatureLevel.Level_10_0 };

            SwapChainDescription desc = new SwapChainDescription();
            desc.BufferCount = 1;
            desc.Flags = SharpDX.DXGI.SwapChainFlags.None;
            desc.IsWindowed = true;
            desc.ModeDescription = new ModeDescription(RenderViewSize.Width, RenderViewSize.Height, new Rational(60, 1), Format.R8G8B8A8_UNorm);
            desc.OutputHandle = ControlHandle;
            desc.SampleDescription = new SampleDescription(Config.MSAASampleCount, Config.MSAAQuality);
            desc.SwapEffect = SwapEffect.Discard;
            desc.Usage = Usage.RenderTargetOutput;

            SharpDX.Direct3D11.Device createdDevice;
            SharpDX.DXGI.SwapChain createdSwapChain;
            SharpDX.Direct3D11.Device.CreateWithSwapChain(driverType, flags, levels, desc, out createdDevice, out createdSwapChain);

            // Store references
            this.SwapChain = createdSwapChain;
            this.Device = createdDevice;
            this.DeviceContext = createdDevice.ImmediateContext;

            this.SwapChain.DebugName = "Lilium";
            this.Device.DebugName = "Lilium";
            this.DeviceContext.DebugName = "Lilium";

            // Ignore all windows events
            var factory = SwapChain.GetParent<Factory>();
            factory.MakeWindowAssociation(ControlHandle, WindowAssociationFlags.IgnoreAll);

            ResizeBuffers();

            renderTimer = new Timer();
            renderTimer.Tick += renderTimer_TickUpdate;
            renderTimer.Interval = 1;
            renderTimer.Start();
        }