Dawn sample: Apply toggles when creating adapters and devices

This CL make Dawn samples create Dawn adapters and devices with required
toggles applied.

Fix: 373474042
Change-Id: I8476eb2d89010cccfdf2b4155367a6803218ec73
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/210914
Reviewed-by: Loko Kung <lokokung@google.com>
Reviewed-by: Jiawei Shao <jiawei.shao@intel.com>
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
Commit-Queue: Zhaoming Jiang <zhaoming.jiang@intel.com>
diff --git a/src/dawn/samples/SampleUtils.cpp b/src/dawn/samples/SampleUtils.cpp
index ddafcbf..e5a469e 100644
--- a/src/dawn/samples/SampleUtils.cpp
+++ b/src/dawn/samples/SampleUtils.cpp
@@ -126,8 +126,30 @@
         angleDefaultPlatform.Set("ANGLE_DEFAULT_PLATFORM", "swiftshader");
     }
 
-    // Setup base adapter options.
+    // Create the toggles descriptor if not using emscripten.
+    wgpu::ChainedStruct* togglesChain = nullptr;
+#ifndef __EMSCRIPTEN__
+    std::vector<const char*> enableToggleNames;
+    std::vector<const char*> disabledToggleNames;
+    for (const std::string& toggle : enableToggles) {
+        enableToggleNames.push_back(toggle.c_str());
+    }
+    for (const std::string& toggle : disableToggles) {
+        disabledToggleNames.push_back(toggle.c_str());
+    }
+
+    wgpu::DawnTogglesDescriptor toggles = {};
+    toggles.enabledToggles = enableToggleNames.data();
+    toggles.enabledToggleCount = enableToggleNames.size();
+    toggles.disabledToggles = disabledToggleNames.data();
+    toggles.disabledToggleCount = disabledToggleNames.size();
+
+    togglesChain = &toggles;
+#endif  // __EMSCRIPTEN__
+
+    // Setup base adapter options with toggles.
     wgpu::RequestAdapterOptions adapterOptions = {};
+    adapterOptions.nextInChain = togglesChain;
     adapterOptions.backendType = backendType;
     if (backendType != wgpu::BackendType::Undefined) {
         adapterOptions.compatibilityMode = dawn::utils::BackendRequiresCompat(backendType);
@@ -151,23 +173,8 @@
     dawnProcSetProcs(&dawn::native::GetProcs());
 
     // Create the instance with the toggles
-    std::vector<const char*> enableToggleNames;
-    std::vector<const char*> disabledToggleNames;
-    for (const std::string& toggle : enableToggles) {
-        enableToggleNames.push_back(toggle.c_str());
-    }
-
-    for (const std::string& toggle : disableToggles) {
-        disabledToggleNames.push_back(toggle.c_str());
-    }
-    wgpu::DawnTogglesDescriptor toggles = {};
-    toggles.enabledToggles = enableToggleNames.data();
-    toggles.enabledToggleCount = enableToggleNames.size();
-    toggles.disabledToggles = disabledToggleNames.data();
-    toggles.disabledToggleCount = disabledToggleNames.size();
-
     wgpu::InstanceDescriptor instanceDescriptor = {};
-    instanceDescriptor.nextInChain = &toggles;
+    instanceDescriptor.nextInChain = togglesChain;
     instanceDescriptor.features.timedWaitAnyEnable = true;
     sample->instance = wgpu::CreateInstance(&instanceDescriptor);
 #else
@@ -194,8 +201,9 @@
     sample->adapter.GetInfo(&info);
     dawn::InfoLog() << "Using adapter \"" << info.device << "\"";
 
-    // Set device callbacks
+    // Create device descriptor with callbacks and toggles
     wgpu::DeviceDescriptor deviceDesc = {};
+    deviceDesc.nextInChain = togglesChain;
     deviceDesc.SetDeviceLostCallback(
         wgpu::CallbackMode::AllowSpontaneous,
         [](const wgpu::Device&, wgpu::DeviceLostReason reason, wgpu::StringView message) {