dawn_node: add "dlldir=<path>" flag

Only used on Windows builds for now, this flag adds the input path to
the DLL search paths. The main purpose is to ensure we load the right
version of D3D dlls, such as d3dcompiler_47.dll from the Chrome output
dir, rather than the default one in the Systems directory.

Bug: dawn:1163
Change-Id: I8e696dd877ec715e1e54d8589af8275e62c90937
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/66962
Commit-Queue: Antonio Maiorano <amaiorano@google.com>
Reviewed-by: Ben Clayton <bclayton@google.com>
diff --git a/src/dawn_node/binding/GPU.cpp b/src/dawn_node/binding/GPU.cpp
index e6e03c4..5f1d4e7 100644
--- a/src/dawn_node/binding/GPU.cpp
+++ b/src/dawn_node/binding/GPU.cpp
@@ -18,6 +18,10 @@
 
 #include <cstdlib>
 
+#if defined(_WIN32)
+#    include <Windows.h>
+#endif
+
 namespace {
     std::string GetEnvVar(const char* varName) {
 #if defined(_WIN32)
@@ -37,6 +41,14 @@
         return "";
 #endif
     }
+
+    void SetDllDir(const char* dir) {
+        (void)dir;
+#if defined(_WIN32)
+        ::SetDllDirectory(dir);
+#endif
+    }
+
 }  // namespace
 
 namespace wgpu { namespace binding {
@@ -49,6 +61,10 @@
         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());
+        }
         instance_.DiscoverDefaultAdapters();
     }