Improve D3D12 adapter initialization.

Gather device info at device initialization rather than adapter initialization.

BUG=dawn:144

Change-Id: I07e114731a37f3738daa585afa99675f2fd36289
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/7700
Commit-Queue: Bryan Bernhart <bryan.bernhart@intel.com>
Reviewed-by: Kai Ninomiya <kainino@chromium.org>
diff --git a/BUILD.gn b/BUILD.gn
index 1e04c69..c859ff8 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -176,6 +176,8 @@
       "src/dawn_native/d3d12/CommandBufferD3D12.h",
       "src/dawn_native/d3d12/ComputePipelineD3D12.cpp",
       "src/dawn_native/d3d12/ComputePipelineD3D12.h",
+      "src/dawn_native/d3d12/D3D12Info.cpp",
+      "src/dawn_native/d3d12/D3D12Info.h",
       "src/dawn_native/d3d12/DescriptorHeapAllocator.cpp",
       "src/dawn_native/d3d12/DescriptorHeapAllocator.h",
       "src/dawn_native/d3d12/DeviceD3D12.cpp",
diff --git a/src/dawn_native/d3d12/AdapterD3D12.cpp b/src/dawn_native/d3d12/AdapterD3D12.cpp
index 16fc617..e2c92cc 100644
--- a/src/dawn_native/d3d12/AdapterD3D12.cpp
+++ b/src/dawn_native/d3d12/AdapterD3D12.cpp
@@ -38,6 +38,36 @@
         : AdapterBase(backend->GetInstance(), BackendType::D3D12),
           mHardwareAdapter(hardwareAdapter),
           mBackend(backend) {
+    }
+
+    const D3D12DeviceInfo& Adapter::GetDeviceInfo() const {
+        return mDeviceInfo;
+    }
+
+    IDXGIAdapter1* Adapter::GetHardwareAdapter() const {
+        return mHardwareAdapter.Get();
+    }
+
+    Backend* Adapter::GetBackend() const {
+        return mBackend;
+    }
+
+    ComPtr<ID3D12Device> Adapter::GetDevice() const {
+        return mD3d12Device;
+    }
+
+    MaybeError Adapter::Initialize() {
+        // D3D12 cannot check for feature support without a device.
+        // Create the device to populate the adapter properties then reuse it when needed for actual
+        // rendering.
+        const PlatformFunctions* functions = GetBackend()->GetFunctions();
+        if (FAILED(functions->d3d12CreateDevice(GetHardwareAdapter(), D3D_FEATURE_LEVEL_11_0,
+                                                _uuidof(ID3D12Device), &mD3d12Device))) {
+            return DAWN_CONTEXT_LOST_ERROR("D3D12CreateDevice failed");
+        }
+
+        DAWN_TRY_ASSIGN(mDeviceInfo, GatherDeviceInfo(*this));
+
         DXGI_ADAPTER_DESC1 adapterDesc;
         mHardwareAdapter->GetDesc1(&adapterDesc);
 
@@ -47,43 +77,20 @@
         if (adapterDesc.Flags & DXGI_ADAPTER_FLAG_SOFTWARE) {
             mDeviceType = DeviceType::CPU;
         } else {
-            // Using DXGI_ADAPTER_DESC1 approach to determine integrated vs dedicated is
-            // vendor-specific.
-            switch (mPCIInfo.vendorId) {
-                case kVendorID_Intel: {
-                    // On Intel GPUs, dedicated video memory is always set to 128MB when the GPU is
-                    // integrated.
-                    static constexpr uint64_t kDedicatedVideoMemory = 128 * 1024 * 1024;
-                    mDeviceType = (adapterDesc.DedicatedVideoMemory == kDedicatedVideoMemory)
-                                      ? DeviceType::IntegratedGPU
-                                      : DeviceType::DiscreteGPU;
-                    break;
-                }
-                default:
-                    // TODO: Support additional GPU vendors.
-                    mDeviceType = DeviceType::Unknown;
-                    break;
-            }
+            mDeviceType = (mDeviceInfo.isUMA) ? DeviceType::IntegratedGPU : DeviceType::DiscreteGPU;
         }
 
         std::wstring_convert<DeletableFacet<std::codecvt<wchar_t, char, std::mbstate_t>>> converter(
             "Error converting");
         mPCIInfo.name = converter.to_bytes(adapterDesc.Description);
-    }
 
-    Backend* Adapter::GetBackend() const {
-        return mBackend;
+        return {};
     }
 
     ResultOrError<DeviceBase*> Adapter::CreateDeviceImpl(const DeviceDescriptor* descriptor) {
-        ComPtr<ID3D12Device> d3d12Device;
-        if (FAILED(mBackend->GetFunctions()->d3d12CreateDevice(
-                mHardwareAdapter.Get(), D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(&d3d12Device)))) {
-            return DAWN_CONTEXT_LOST_ERROR("D3D12CreateDevice failed");
-        }
-
-        ASSERT(d3d12Device != nullptr);
-        return new Device(this, d3d12Device, descriptor);
+        std::unique_ptr<Device> device = std::make_unique<Device>(this, descriptor);
+        DAWN_TRY(device->Initialize());
+        return device.release();
     }
 
 }}  // namespace dawn_native::d3d12
diff --git a/src/dawn_native/d3d12/AdapterD3D12.h b/src/dawn_native/d3d12/AdapterD3D12.h
index 24f3dfc..b5a726b 100644
--- a/src/dawn_native/d3d12/AdapterD3D12.h
+++ b/src/dawn_native/d3d12/AdapterD3D12.h
@@ -17,6 +17,7 @@
 
 #include "dawn_native/Adapter.h"
 
+#include "dawn_native/d3d12/D3D12Info.h"
 #include "dawn_native/d3d12/d3d12_platform.h"
 
 namespace dawn_native { namespace d3d12 {
@@ -28,13 +29,21 @@
         Adapter(Backend* backend, ComPtr<IDXGIAdapter1> hardwareAdapter);
         virtual ~Adapter() = default;
 
+        const D3D12DeviceInfo& GetDeviceInfo() const;
+        IDXGIAdapter1* GetHardwareAdapter() const;
         Backend* GetBackend() const;
+        ComPtr<ID3D12Device> GetDevice() const;
+
+        MaybeError Initialize();
 
       private:
         ResultOrError<DeviceBase*> CreateDeviceImpl(const DeviceDescriptor* descriptor) override;
 
         ComPtr<IDXGIAdapter1> mHardwareAdapter;
+        ComPtr<ID3D12Device> mD3d12Device;
+
         Backend* mBackend;
+        D3D12DeviceInfo mDeviceInfo = {};
     };
 
 }}  // namespace dawn_native::d3d12
diff --git a/src/dawn_native/d3d12/BackendD3D12.cpp b/src/dawn_native/d3d12/BackendD3D12.cpp
index 29db46e1..b702fe4 100644
--- a/src/dawn_native/d3d12/BackendD3D12.cpp
+++ b/src/dawn_native/d3d12/BackendD3D12.cpp
@@ -92,10 +92,13 @@
             }
 
             ASSERT(dxgiAdapter != nullptr);
-            if (SUCCEEDED(mFunctions->d3d12CreateDevice(dxgiAdapter.Get(), D3D_FEATURE_LEVEL_11_0,
-                                                        _uuidof(ID3D12Device), nullptr))) {
-                adapters.push_back(std::make_unique<Adapter>(this, dxgiAdapter));
+
+            std::unique_ptr<Adapter> adapter = std::make_unique<Adapter>(this, dxgiAdapter);
+            if (GetInstance()->ConsumedError(adapter->Initialize())) {
+                continue;
             }
+
+            adapters.push_back(std::move(adapter));
         }
 
         return adapters;
diff --git a/src/dawn_native/d3d12/D3D12Info.cpp b/src/dawn_native/d3d12/D3D12Info.cpp
new file mode 100644
index 0000000..ac5e526
--- /dev/null
+++ b/src/dawn_native/d3d12/D3D12Info.cpp
@@ -0,0 +1,44 @@
+// Copyright 2019 The Dawn Authors
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "dawn_native/d3d12/D3D12Info.h"
+
+#include "dawn_native/D3D12/AdapterD3D12.h"
+#include "dawn_native/D3D12/BackendD3D12.h"
+
+#include "dawn_native/d3d12/PlatformFunctions.h"
+
+namespace dawn_native { namespace d3d12 {
+
+    ResultOrError<D3D12DeviceInfo> GatherDeviceInfo(const Adapter& adapter) {
+        D3D12DeviceInfo info = {};
+
+        // Gather info about device memory
+        {
+            // Newer builds replace D3D_FEATURE_DATA_ARCHITECTURE with
+            // D3D_FEATURE_DATA_ARCHITECTURE1. However, D3D_FEATURE_DATA_ARCHITECTURE can be used
+            // for backwards compat.
+            // https://docs.microsoft.com/en-us/windows/desktop/api/d3d12/ne-d3d12-d3d12_feature
+            D3D12_FEATURE_DATA_ARCHITECTURE arch = {};
+            if (FAILED(adapter.GetDevice()->CheckFeatureSupport(D3D12_FEATURE_ARCHITECTURE, &arch,
+                                                                sizeof(arch)))) {
+                return DAWN_CONTEXT_LOST_ERROR("CheckFeatureSupport failed");
+            }
+
+            info.isUMA = arch.UMA;
+        }
+
+        return info;
+    }
+}}  // namespace dawn_native::d3d12
\ No newline at end of file
diff --git a/src/dawn_native/d3d12/D3D12Info.h b/src/dawn_native/d3d12/D3D12Info.h
new file mode 100644
index 0000000..11be2d3
--- /dev/null
+++ b/src/dawn_native/d3d12/D3D12Info.h
@@ -0,0 +1,32 @@
+// Copyright 2019 The Dawn Authors
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef DAWNNATIVE_D3D12_D3D12INFO_H_
+#define DAWNNATIVE_D3D12_D3D12INFO_H_
+
+#include "dawn_native/Error.h"
+#include "dawn_native/d3d12/d3d12_platform.h"
+
+namespace dawn_native { namespace d3d12 {
+
+    class Adapter;
+
+    struct D3D12DeviceInfo {
+        bool isUMA;
+    };
+
+    ResultOrError<D3D12DeviceInfo> GatherDeviceInfo(const Adapter& adapter);
+}}  // namespace dawn_native::d3d12
+
+#endif  // DAWNNATIVE_D3D12_D3D12INFO_H_
\ No newline at end of file
diff --git a/src/dawn_native/d3d12/DeviceD3D12.cpp b/src/dawn_native/d3d12/DeviceD3D12.cpp
index 651323e..8097169 100644
--- a/src/dawn_native/d3d12/DeviceD3D12.cpp
+++ b/src/dawn_native/d3d12/DeviceD3D12.cpp
@@ -43,13 +43,18 @@
         ASSERT(SUCCEEDED(hr));
     }
 
-    Device::Device(Adapter* adapter,
-                   ComPtr<ID3D12Device> d3d12Device,
-                   const DeviceDescriptor* descriptor)
-        : DeviceBase(adapter, descriptor), mD3d12Device(d3d12Device) {
+    Device::Device(Adapter* adapter, const DeviceDescriptor* descriptor)
+        : DeviceBase(adapter, descriptor) {
         if (descriptor != nullptr) {
             ApplyToggleOverrides(descriptor);
         }
+    }
+
+    MaybeError Device::Initialize() {
+        mD3d12Device = ToBackend(GetAdapter())->GetDevice();
+
+        ASSERT(mD3d12Device != nullptr);
+
         // Create device-global objects
         D3D12_COMMAND_QUEUE_DESC queueDesc = {};
         queueDesc.Flags = D3D12_COMMAND_QUEUE_FLAG_NONE;
@@ -96,6 +101,8 @@
             ->GetD3D12Device()
             ->CreateCommandSignature(&programDesc, NULL,
                                      IID_PPV_ARGS(&mDrawIndexedIndirectSignature));
+
+        return {};
     }
 
     Device::~Device() {
diff --git a/src/dawn_native/d3d12/DeviceD3D12.h b/src/dawn_native/d3d12/DeviceD3D12.h
index 18efaab..311f178 100644
--- a/src/dawn_native/d3d12/DeviceD3D12.h
+++ b/src/dawn_native/d3d12/DeviceD3D12.h
@@ -37,11 +37,11 @@
     // Definition of backend types
     class Device : public DeviceBase {
       public:
-        Device(Adapter* adapter,
-               ComPtr<ID3D12Device> d3d12Device,
-               const DeviceDescriptor* descriptor);
+        Device(Adapter* adapter, const DeviceDescriptor* descriptor);
         ~Device();
 
+        MaybeError Initialize();
+
         CommandBufferBase* CreateCommandBuffer(CommandEncoderBase* encoder) override;
 
         Serial GetCompletedCommandSerial() const final override;
@@ -108,7 +108,7 @@
         ComPtr<ID3D12Fence> mFence;
         HANDLE mFenceEvent;
 
-        ComPtr<ID3D12Device> mD3d12Device;
+        ComPtr<ID3D12Device> mD3d12Device;  // Device is owned by adapter and will not be outlived.
         ComPtr<ID3D12CommandQueue> mCommandQueue;
 
         ComPtr<ID3D12CommandSignature> mDispatchIndirectSignature;