Exemple #1
0
        public static IDXGISwapChain1 CreateSwapChainForHwnd(
            ID3D11Device device,
            IntPtr hwnd)
        {
            IDXGIDevice           dxgiDevice  = device.QueryInterface <IDXGIDevice>();
            IDXGIFactory2         dxgiFactory = dxgiDevice.GetAdapter().GetParent <IDXGIFactory2>();
            SwapChainDescription1 dxgiDesc    = new()
            {
                Format            = Format.B8G8R8A8_UNorm,
                SampleDescription = new SampleDescription(1, 0),
                BufferUsage       = Usage.RenderTargetOutput,
                BufferCount       = 2,
            };

            return(dxgiFactory.CreateSwapChainForHwnd(
                       device,
                       hwnd,
                       dxgiDesc));
        }
        public D3D11GraphicsDevice(bool validation, Window window)
        {
            Window = window;
            if (CreateDXGIFactory1(out Factory).Failure)
            {
                throw new InvalidOperationException("Cannot create IDXGIFactory1");
            }

            var creationFlags = DeviceCreationFlags.BgraSupport;

            if (validation)
            {
                creationFlags |= DeviceCreationFlags.Debug;
            }

            if (D3D11CreateDevice(
                    IntPtr.Zero,
                    DriverType.Hardware,
                    creationFlags,
                    s_featureLevels,
                    out Device, out FeatureLevel, out DeviceContext).Failure)
            {
                // Remove debug flag not being supported.
                creationFlags &= ~DeviceCreationFlags.Debug;

                var result = D3D11CreateDevice(null, DriverType.Hardware,
                                               creationFlags, s_featureLevels,
                                               out Device, out FeatureLevel, out DeviceContext);
                if (result.Failure)
                {
                    // This will fail on Win 7 due to lack of 11.1, so re-try again without it
                    D3D11CreateDevice(
                        IntPtr.Zero,
                        DriverType.Hardware,
                        creationFlags,
                        s_featureLevelsNoLevel11,
                        out Device, out FeatureLevel, out DeviceContext).CheckError();
                }
            }

            using (IDXGIDevice dxgiDevice = Device.QueryInterface <IDXGIDevice>())
            {
                // Store a pointer to the DXGI adapter.
                // This is for the case of no preferred DXGI adapter, or fallback to WARP.
                dxgiDevice.GetAdapter(out Adapter).CheckError();
            }

            IntPtr hwnd = (IntPtr)window.Handle;

            var swapChainDescription = new SwapChainDescription()
            {
                BufferCount       = FrameCount,
                BufferDescription = new ModeDescription(window.Width, window.Height, Format.B8G8R8A8_UNorm),
                IsWindowed        = true,
                OutputWindow      = hwnd,
                SampleDescription = new SampleDescription(1, 0),
                SwapEffect        = SwapEffect.Discard,
                Usage             = DXGI.Usage.RenderTargetOutput
            };

            SwapChain = Factory.CreateSwapChain(Device, swapChainDescription);
            Factory.MakeWindowAssociation(hwnd, WindowAssociationFlags.IgnoreAltEnter);

            BackBuffer       = SwapChain.GetBuffer <ID3D11Texture2D>(0);
            RenderTargetView = Device.CreateRenderTargetView(BackBuffer);
        }
        public D3D11GraphicsDevice(D3D11DeviceOptions options, SwapchainDescription?swapchainDesc)
        {
            var flags = (DeviceCreationFlags)options.DeviceCreationFlags;

#if DEBUG
            flags |= DeviceCreationFlags.Debug;
#endif
            // If debug flag set but SDK layers aren't available we can't enable debug.
            if (0 != (flags & DeviceCreationFlags.Debug) && !Vortice.Direct3D11.D3D11.SdkLayersAvailable())
            {
                flags &= ~DeviceCreationFlags.Debug;
            }

            try
            {
                if (options.AdapterPtr != IntPtr.Zero)
                {
                    VorticeD3D11.D3D11CreateDevice(options.AdapterPtr,
                                                   Vortice.Direct3D.DriverType.Hardware,
                                                   flags,
                                                   new[]
                    {
                        Vortice.Direct3D.FeatureLevel.Level_11_1,
                        Vortice.Direct3D.FeatureLevel.Level_11_0,
                    },
                                                   out _device).CheckError();
                }
                else
                {
                    VorticeD3D11.D3D11CreateDevice(IntPtr.Zero,
                                                   Vortice.Direct3D.DriverType.Hardware,
                                                   flags,
                                                   new[]
                    {
                        Vortice.Direct3D.FeatureLevel.Level_11_1,
                        Vortice.Direct3D.FeatureLevel.Level_11_0,
                    },
                                                   out _device).CheckError();
                }
            }
            catch
            {
                VorticeD3D11.D3D11CreateDevice(IntPtr.Zero,
                                               Vortice.Direct3D.DriverType.Hardware,
                                               flags,
                                               null,
                                               out _device).CheckError();
            }

            using (IDXGIDevice dxgiDevice = _device.QueryInterface <IDXGIDevice>())
            {
                // Store a pointer to the DXGI adapter.
                // This is for the case of no preferred DXGI adapter, or fallback to WARP.
                dxgiDevice.GetAdapter(out _dxgiAdapter).CheckError();
                _deviceName = _dxgiAdapter.Description.Description;
            }

            if (swapchainDesc != null)
            {
                SwapchainDescription desc = swapchainDesc.Value;
                _mainSwapchain = new D3D11Swapchain(this, ref desc);
            }
            _immediateContext = _device.ImmediateContext;
            _device.CheckThreadingSupport(out _supportsConcurrentResources, out _supportsCommandLists);

            Features = new GraphicsDeviceFeatures(
                computeShader: true,
                geometryShader: true,
                tessellationShaders: true,
                multipleViewports: true,
                samplerLodBias: true,
                drawBaseVertex: true,
                drawBaseInstance: true,
                drawIndirect: true,
                drawIndirectBaseInstance: true,
                fillModeWireframe: true,
                samplerAnisotropy: true,
                depthClipDisable: true,
                texture1D: true,
                independentBlend: true,
                structuredBuffer: true,
                subsetTextureView: true,
                commandListDebugMarkers: _device.FeatureLevel >= Vortice.Direct3D.FeatureLevel.Level_11_1,
                bufferRangeBinding: _device.FeatureLevel >= Vortice.Direct3D.FeatureLevel.Level_11_1,
                shaderFloat64: _device.CheckFeatureSupport <FeatureDataDoubles>(Vortice.Direct3D11.Feature.Doubles).DoublePrecisionFloatShaderOps);

            _d3d11ResourceFactory = new D3D11ResourceFactory(this);
            _d3d11Info            = new BackendInfoD3D11(this);

            PostDeviceCreated();
        }