Example #1
0
        protected unsafe DXGISwapchain(
            GraphicsDevice device,
            PresentationParameters presentationParameters,
            ComObject deviceOrCommandQueue,
            int bufferCount,
            int frameCount) : base(device)
        {
            _frameCount = frameCount;
            var width  = Math.Max(presentationParameters.BackBufferWidth, 1);
            var height = Math.Max(presentationParameters.BackBufferHeight, 1);

            switch (presentationParameters.DeviceWindowHandle)
            {
            case IntPtr hwnd:
            {
                using (var dxgiDevice = deviceOrCommandQueue.QueryInterface <SharpDX.DXGI.Device>())
                {
                    using (var dxgiFactory = dxgiDevice.Adapter.GetParent <Factory2>())
                    {
                        // Check tearing support.
                        RawBool allowTearing = false;
                        using (var factory5 = dxgiFactory.QueryInterfaceOrNull <DXGI.Factory5>())
                        {
                            factory5.CheckFeatureSupport(DXGI.Feature.PresentAllowTearing,
                                                         new IntPtr(&allowTearing), sizeof(RawBool)
                                                         );

                            // Recommended to always use tearing if supported when using a sync interval of 0.
                            _syncInterval  = 0;
                            _presentFlags |= DXGI.PresentFlags.AllowTearing;
                        }

                        var swapchainDesc = new SharpDX.DXGI.SwapChainDescription1()
                        {
                            Width             = width,
                            Height            = height,
                            Format            = Format.B8G8R8A8_UNorm,
                            Stereo            = false,
                            SampleDescription = new DXGI.SampleDescription(1, 0),
                            Usage             = DXGI.Usage.RenderTargetOutput,
                            BufferCount       = bufferCount,
                            Scaling           = Scaling.Stretch,
                            SwapEffect        = allowTearing ? SwapEffect.FlipDiscard : DXGI.SwapEffect.Discard,
                            AlphaMode         = AlphaMode.Ignore,
                            Flags             = allowTearing ? SwapChainFlags.AllowTearing : DXGI.SwapChainFlags.None,
                        };

                        var fullscreenDescription = new DXGI.SwapChainFullScreenDescription
                        {
                            Windowed = true
                        };

                        _swapChain = new DXGI.SwapChain1(dxgiFactory,
                                                         deviceOrCommandQueue,
                                                         hwnd,
                                                         ref swapchainDesc,
                                                         fullscreenDescription);
                        dxgiFactory.MakeWindowAssociation(hwnd, DXGI.WindowAssociationFlags.IgnoreAll);
                    }
                }
            }
            break;

                //case CoreWindow coreWindowHandle:
                //    {
                //        var coreWindow = coreWindowHandle.CoreWindow;

                //        var swapchainDesc = new DXGI.SwapChainDescription1()
                //        {
                //            Width = width,
                //            Height = height,
                //            Format = DXGI.Format.B8G8R8A8_UNorm,
                //            Stereo = false,
                //            SampleDescription = new DXGI.SampleDescription(1, 0),
                //            Usage = DXGI.Usage.RenderTargetOutput,
                //            BufferCount = FrameCount,
                //            Scaling = DXGI.Scaling.AspectRatioStretch,
                //            SwapEffect = DXGI.SwapEffect.FlipDiscard,
                //            AlphaMode = DXGI.AlphaMode.Ignore,
                //            Flags = DXGI.SwapChainFlags.None,
                //        };

                //        using (var comCoreWindow = new ComObject(coreWindow))
                //        {
                //            _swapChain = new DXGI.SwapChain1(
                //                factory,
                //                device.D3DDevice,
                //                comCoreWindow,
                //                ref swapchainDesc);
                //        }
                //    }
                //    break;
            }
        }
Example #2
0
        /// <summary>
        /// Create new instance of <see cref="GraphicsDevice"/>
        /// </summary>
        /// <param name="backend">The type of <see cref="GraphicsBackend"/></param>
        /// <param name="validation">Whether to enable validation if supported.</param>
        /// <param name="presentationParameters">The main swap chain parameters.</param>
        /// <returns>New instance of <see cref="GraphicsDevice"/>.</returns>
        public static GraphicsDevice Create(GraphicsBackend backend, bool validation, PresentationParameters presentationParameters)
        {
            Guard.NotNull(presentationParameters, nameof(presentationParameters));

            if (backend == GraphicsBackend.Default)
            {
                backend = GetDefaultGraphicsPlatform(Platform.PlatformType);
            }

            if (!IsSupported(backend))
            {
                throw new GraphicsException($"Backend {backend} is not supported");
            }

            switch (backend)
            {
            case GraphicsBackend.Direct3D11:
#if !VORTICE_NO_D3D11
                return(new D3D11.D3D11GraphicsDevice(validation, presentationParameters));
#else
                throw new GraphicsException($"{GraphicsBackend.Direct3D11} Backend is not supported");
#endif

            case GraphicsBackend.Direct3D12:
#if !VORTICE_NO_D3D12
                return(new D3D12.D3D12GraphicsDevice(validation, presentationParameters));
#else
                throw new GraphicsException($"{GraphicsBackend.Direct3D12} Backend is not supported");
#endif

            case GraphicsBackend.Vulkan:
#if !VORTICE_NO_D3D12
                return(new Vulkan.VulkanGraphicsDevice(validation, presentationParameters));
#else
                throw new GraphicsException($"{GraphicsBackend.Vulkan} Backend is not supported");
#endif

            default:
                throw new GraphicsException($"Invalid {backend} backend");
            }
        }