Migrate instance options to DawnInstanceDescriptor

Bug: None
Change-Id: If2517545fe1ee20faa2fe7b03736b85495d79636
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/166142
Commit-Queue: Corentin Wallez <cwallez@chromium.org>
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: Austin Eng <enga@chromium.org>
diff --git a/include/dawn/native/DawnNative.h b/include/dawn/native/DawnNative.h
index fe8ab48..ce2d076 100644
--- a/include/dawn/native/DawnNative.h
+++ b/include/dawn/native/DawnNative.h
@@ -137,6 +137,10 @@
     const char* const* additionalRuntimeSearchPaths;
     dawn::platform::Platform* platform = nullptr;
 
+    BackendValidationLevel backendValidationLevel = BackendValidationLevel::Disabled;
+    bool beginCaptureOnStartup = false;
+    bool enableAdapterBlocklist = false;
+
     // Equality operators, mostly for testing. Note that this tests
     // strict pointer-pointer equality if the struct contains member pointers.
     bool operator==(const DawnInstanceDescriptor& rhs) const;
diff --git a/src/dawn/native/DawnNative.cpp b/src/dawn/native/DawnNative.cpp
index 844859b..f856df1 100644
--- a/src/dawn/native/DawnNative.cpp
+++ b/src/dawn/native/DawnNative.cpp
@@ -151,9 +151,11 @@
 
 bool DawnInstanceDescriptor::operator==(const DawnInstanceDescriptor& rhs) const {
     return (nextInChain == rhs.nextInChain) &&
-           std::tie(additionalRuntimeSearchPathsCount, additionalRuntimeSearchPaths, platform) ==
+           std::tie(additionalRuntimeSearchPathsCount, additionalRuntimeSearchPaths, platform,
+                    backendValidationLevel, beginCaptureOnStartup, enableAdapterBlocklist) ==
                std::tie(rhs.additionalRuntimeSearchPathsCount, rhs.additionalRuntimeSearchPaths,
-                        rhs.platform);
+                        rhs.platform, rhs.backendValidationLevel, rhs.beginCaptureOnStartup,
+                        rhs.enableAdapterBlocklist);
 }
 
 // Instance
diff --git a/src/dawn/native/Instance.cpp b/src/dawn/native/Instance.cpp
index 660c73c..36a50ee 100644
--- a/src/dawn/native/Instance.cpp
+++ b/src/dawn/native/Instance.cpp
@@ -225,6 +225,10 @@
             mRuntimeSearchPaths.push_back(dawnDesc->additionalRuntimeSearchPaths[i]);
         }
         SetPlatform(dawnDesc->platform);
+
+        mBackendValidationLevel = dawnDesc->backendValidationLevel;
+        mBeginCaptureOnStartup = dawnDesc->beginCaptureOnStartup;
+        mEnableAdapterBlocklist = dawnDesc->enableAdapterBlocklist;
     }
 
     // Default paths to search are next to the shared library, next to the executable, and
diff --git a/src/dawn/node/binding/GPU.cpp b/src/dawn/node/binding/GPU.cpp
index 3b2f5a6..411446c 100644
--- a/src/dawn/node/binding/GPU.cpp
+++ b/src/dawn/node/binding/GPU.cpp
@@ -113,15 +113,21 @@
 // wgpu::bindings::GPU
 ////////////////////////////////////////////////////////////////////////////////
 GPU::GPU(Flags flags) : flags_(std::move(flags)) {
-    if (auto validate = flags_.Get("validate"); validate == "1" || validate == "true") {
-        instance_.EnableBackendValidation(true);
-        instance_.SetBackendValidationLevel(dawn::native::BackendValidationLevel::Full);
-    }
-
     // Setting the DllDir changes where we load adapter DLLs from (e.g. d3dcompiler_47.dll)
     if (auto dir = flags_.Get("dlldir")) {
         SetDllDir(dir->c_str());
     }
+
+    // Set up the chained descriptor for the various instance extensions.
+    dawn::native::DawnInstanceDescriptor dawnDesc;
+    if (auto validate = flags_.Get("validate"); validate == "1" || validate == "true") {
+        dawnDesc.backendValidationLevel = dawn::native::BackendValidationLevel::Full;
+    }
+
+    wgpu::InstanceDescriptor desc;
+    desc.nextInChain = &dawnDesc;
+    instance_ = std::make_unique<dawn::native::Instance>(
+        reinterpret_cast<const WGPUInstanceDescriptor*>(&desc));
 }
 
 interop::Promise<std::optional<interop::Interface<interop::GPUAdapter>>> GPU::requestAdapter(
@@ -192,7 +198,7 @@
     DawnTogglesDescriptor togglesDescriptor = togglesLoader.GetDescriptor();
     nativeOptions.nextInChain = &togglesDescriptor;
 
-    auto adapters = instance_.EnumerateAdapters(&nativeOptions);
+    auto adapters = instance_->EnumerateAdapters(&nativeOptions);
     if (adapters.empty()) {
         promise.Resolve({});
         return promise;
@@ -289,7 +295,7 @@
         InteropWGSLFeatureSet features_;
     };
 
-    wgpu::Instance instance = instance_.Get();
+    wgpu::Instance instance = instance_->Get();
     size_t count = instance.EnumerateWGSLLanguageFeatures(nullptr);
 
     std::vector<wgpu::WGSLFeatureName> features(count);
diff --git a/src/dawn/node/binding/GPU.h b/src/dawn/node/binding/GPU.h
index 8953305..bec07cd 100644
--- a/src/dawn/node/binding/GPU.h
+++ b/src/dawn/node/binding/GPU.h
@@ -28,6 +28,8 @@
 #ifndef SRC_DAWN_NODE_BINDING_GPU_H_
 #define SRC_DAWN_NODE_BINDING_GPU_H_
 
+#include <memory>
+
 #include "dawn/native/DawnNative.h"
 #include "dawn/webgpu_cpp.h"
 
@@ -50,7 +52,7 @@
 
   private:
     const Flags flags_;
-    dawn::native::Instance instance_;
+    std::unique_ptr<dawn::native::Instance> instance_;
 };
 
 }  // namespace wgpu::binding
diff --git a/src/dawn/tests/DawnNativeTest.cpp b/src/dawn/tests/DawnNativeTest.cpp
index 6665665..3009641 100644
--- a/src/dawn/tests/DawnNativeTest.cpp
+++ b/src/dawn/tests/DawnNativeTest.cpp
@@ -73,10 +73,8 @@
 
     wgpu::InstanceDescriptor instanceDesc;
     instanceDesc.nextInChain = &dawnInstanceDesc;
-
     instance = std::make_unique<dawn::native::Instance>(
         reinterpret_cast<const WGPUInstanceDescriptor*>(&instanceDesc));
-    instance->EnableAdapterBlocklist(false);
 
     wgpu::RequestAdapterOptions options = {};
     options.backendType = wgpu::BackendType::Null;
diff --git a/src/dawn/tests/DawnTest.cpp b/src/dawn/tests/DawnTest.cpp
index 79aacb3..bc9389a 100644
--- a/src/dawn/tests/DawnTest.cpp
+++ b/src/dawn/tests/DawnTest.cpp
@@ -411,6 +411,8 @@
 
     dawn::native::DawnInstanceDescriptor dawnInstanceDesc;
     dawnInstanceDesc.platform = platform;
+    dawnInstanceDesc.beginCaptureOnStartup = mBeginCaptureOnStartup;
+    dawnInstanceDesc.backendValidationLevel = mBackendValidationLevel;
     dawnInstanceDesc.nextInChain = &instanceToggles;
 
     wgpu::InstanceDescriptor instanceDesc{};
@@ -419,9 +421,6 @@
 
     auto instance = std::make_unique<native::Instance>(
         reinterpret_cast<const WGPUInstanceDescriptor*>(&instanceDesc));
-    instance->EnableBeginCaptureOnStartup(mBeginCaptureOnStartup);
-    instance->SetBackendValidationLevel(mBackendValidationLevel);
-    instance->EnableAdapterBlocklist(false);
 
 #ifdef DAWN_ENABLE_BACKEND_OPENGLES
     if (GetEnvironmentVar("ANGLE_DEFAULT_PLATFORM").first.empty()) {
diff --git a/src/dawn/tests/end2end/DeviceInitializationTests.cpp b/src/dawn/tests/end2end/DeviceInitializationTests.cpp
index eb0a030..9e3f1e1 100644
--- a/src/dawn/tests/end2end/DeviceInitializationTests.cpp
+++ b/src/dawn/tests/end2end/DeviceInitializationTests.cpp
@@ -90,7 +90,6 @@
     std::vector<wgpu::AdapterProperties> availableAdapterProperties;
     {
         auto instance = std::make_unique<native::Instance>();
-        instance->EnableAdapterBlocklist(false);
         for (const native::Adapter& adapter : instance->EnumerateAdapters()) {
             wgpu::AdapterProperties properties;
             adapter.GetProperties(&properties);
@@ -107,7 +106,6 @@
         wgpu::Device device;
 
         auto instance = std::make_unique<native::Instance>();
-        instance->EnableAdapterBlocklist(false);
         for (native::Adapter& adapter : instance->EnumerateAdapters()) {
             wgpu::AdapterProperties properties;
             adapter.GetProperties(&properties);
@@ -137,7 +135,6 @@
     std::vector<wgpu::AdapterProperties> availableAdapterProperties;
     {
         auto instance = std::make_unique<native::Instance>();
-        instance->EnableAdapterBlocklist(false);
         for (const native::Adapter& adapter : instance->EnumerateAdapters()) {
             wgpu::AdapterProperties properties;
             adapter.GetProperties(&properties);
@@ -157,7 +154,6 @@
         wgpu::Adapter adapter;
 
         auto instance = std::make_unique<native::Instance>();
-        instance->EnableAdapterBlocklist(false);
         // Save a pointer to the instance.
         // It will only be valid as long as the instance is alive.
         WGPUInstance unsafeInstancePtr = instance->Get();