Ejemplo n.º 1
0
            /// <inheritdoc/>
            public bool MoveNext()
            {
                if (!this.isInitialized)
                {
                    this.isInitialized = true;

                    fixed(IDXGIFactory6 **dxgiFactory6 = this.dxgiFactory6)
                    {
                        CreateDXGIFactory6(dxgiFactory6);
                    }
                }

                if (this.isCompleted)
                {
                    return(false);
                }

                while (true)
                {
                    using ComPtr <IDXGIAdapter1> dxgiAdapter1 = default;

                    HRESULT enumAdapters1Result = this.dxgiFactory6.Get()->EnumAdapterByGpuPreference(
                        this.index,
                        DXGI_GPU_PREFERENCE_HIGH_PERFORMANCE,
                        Windows.__uuidof <IDXGIAdapter1>(),
                        dxgiAdapter1.GetVoidAddressOf());

                    if (enumAdapters1Result == DXGI.DXGI_ERROR_NOT_FOUND)
                    {
                        this.dxgiFactory6.Get()->EnumWarpAdapter(Windows.__uuidof <IDXGIAdapter1>(), dxgiAdapter1.GetVoidAddressOf()).Assert();

                        DXGI_ADAPTER_DESC1 dxgiDescription1;

                        dxgiAdapter1.Get()->GetDesc1(&dxgiDescription1).Assert();

                        HRESULT createDeviceResult = DirectX.D3D12CreateDevice(
                            dxgiAdapter1.AsIUnknown().Get(),
                            D3D_FEATURE_LEVEL_11_0,
                            Windows.__uuidof <ID3D12Device>(),
                            null);

                        if (Windows.SUCCEEDED(createDeviceResult) &&
                            this.predicate?.Invoke(new GraphicsDeviceInfo(&dxgiDescription1)) != false)
                        {
                            using ComPtr <ID3D12Device> d3D12Device = default;

                            DirectX.D3D12CreateDevice(
                                dxgiAdapter1.AsIUnknown().Get(),
                                D3D_FEATURE_LEVEL_11_0,
                                Windows.__uuidof <ID3D12Device>(),
                                d3D12Device.GetVoidAddressOf()).Assert();

                            this.graphicsDevice = GetOrCreateDevice(d3D12Device.Get(), (IDXGIAdapter *)dxgiAdapter1.Get(), &dxgiDescription1);
                            this.isCompleted    = true;

                            return(true);
                        }

                        return(false);
                    }
                    else
                    {
                        enumAdapters1Result.Assert();

                        this.index++;

                        DXGI_ADAPTER_DESC1 dxgiDescription1;

                        dxgiAdapter1.Get()->GetDesc1(&dxgiDescription1).Assert();

                        if (dxgiDescription1.VendorId == MicrosoftVendorId &&
                            dxgiDescription1.DeviceId == WarpDeviceId)
                        {
                            continue;
                        }

                        HRESULT createDeviceResult = DirectX.D3D12CreateDevice(
                            dxgiAdapter1.AsIUnknown().Get(),
                            D3D_FEATURE_LEVEL_11_0,
                            Windows.__uuidof <ID3D12Device>(),
                            null);

                        if (Windows.SUCCEEDED(createDeviceResult) &&
                            this.predicate?.Invoke(new GraphicsDeviceInfo(&dxgiDescription1)) != false)
                        {
                            using ComPtr <ID3D12Device> d3D12Device = default;

                            DirectX.D3D12CreateDevice(
                                dxgiAdapter1.AsIUnknown().Get(),
                                D3D_FEATURE_LEVEL_11_0,
                                Windows.__uuidof <ID3D12Device>(),
                                d3D12Device.GetVoidAddressOf()).Assert();

                            if (d3D12Device.Get()->IsShaderModelSupported(D3D_SHADER_MODEL_6_0))
                            {
                                this.graphicsDevice = GetOrCreateDevice(d3D12Device.Get(), (IDXGIAdapter *)dxgiAdapter1.Get(), &dxgiDescription1);

                                return(true);
                            }
                        }
                    }
                }
            }