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