dawn_node: Error if the specified backend is not found

Defaulting to the 0'th adapter can silently use the Null adapter, which is no fun for anyone.

Bug tint:1354

Change-Id: I14e2379175cb90a48753bd81d096391d15d2dc22
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/75070
Commit-Queue: Ben Clayton <bclayton@google.com>
Auto-Submit: Ben Clayton <bclayton@google.com>
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
Commit-Queue: Corentin Wallez <cwallez@chromium.org>
This commit is contained in:
Ben Clayton 2022-01-04 14:36:25 +00:00 committed by Dawn LUCI CQ
parent 85b7e50c52
commit 76ff65f694
1 changed files with 19 additions and 9 deletions

View File

@ -112,6 +112,9 @@ namespace wgpu { namespace binding {
std::transform(forceBackend.begin(), forceBackend.end(), forceBackend.begin(), std::transform(forceBackend.begin(), forceBackend.end(), forceBackend.begin(),
[](char c) { return std::tolower(c); }); [](char c) { return std::tolower(c); });
// Default to first adapter if a backend is not specified
size_t adapterIndex = 0;
if (!forceBackend.empty()) { if (!forceBackend.empty()) {
if (forceBackend == "null") { if (forceBackend == "null") {
targetBackendType = wgpu::BackendType::Null; targetBackendType = wgpu::BackendType::Null;
@ -129,19 +132,26 @@ namespace wgpu { namespace binding {
targetBackendType = wgpu::BackendType::OpenGL; targetBackendType = wgpu::BackendType::OpenGL;
} else if (forceBackend == "opengles" || forceBackend == "gles") { } else if (forceBackend == "opengles" || forceBackend == "gles") {
targetBackendType = wgpu::BackendType::OpenGLES; targetBackendType = wgpu::BackendType::OpenGLES;
} } else {
promise.Reject("unknown backend '" + forceBackend + "'");
return promise;
} }
// Default to first adapter if we don't find a match bool found = false;
size_t adapterIndex = 0;
for (size_t i = 0; i < adapters.size(); ++i) { for (size_t i = 0; i < adapters.size(); ++i) {
wgpu::AdapterProperties props; wgpu::AdapterProperties props;
adapters[i].GetProperties(&props); adapters[i].GetProperties(&props);
if (props.backendType == targetBackendType) { if (props.backendType == targetBackendType) {
adapterIndex = i; adapterIndex = i;
found = true;
break; break;
} }
} }
if (!found) {
promise.Reject("backend '" + forceBackend + "' not found");
return promise;
}
}
auto adapter = GPUAdapter::Create<GPUAdapter>(env, adapters[adapterIndex], flags_); auto adapter = GPUAdapter::Create<GPUAdapter>(env, adapters[adapterIndex], flags_);
promise.Resolve(std::optional<interop::Interface<interop::GPUAdapter>>(adapter)); promise.Resolve(std::optional<interop::Interface<interop::GPUAdapter>>(adapter));