From 76ff65f6949a70a5c89d926c3c3190ad4543b29c Mon Sep 17 00:00:00 2001 From: Ben Clayton Date: Tue, 4 Jan 2022 14:36:25 +0000 Subject: [PATCH] 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 Auto-Submit: Ben Clayton Reviewed-by: Corentin Wallez Commit-Queue: Corentin Wallez --- src/dawn_node/binding/GPU.cpp | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/src/dawn_node/binding/GPU.cpp b/src/dawn_node/binding/GPU.cpp index 50eb020195..a13ed3069d 100644 --- a/src/dawn_node/binding/GPU.cpp +++ b/src/dawn_node/binding/GPU.cpp @@ -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,17 +132,24 @@ 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; - for (size_t i = 0; i < adapters.size(); ++i) { - wgpu::AdapterProperties props; - adapters[i].GetProperties(&props); - if (props.backendType == targetBackendType) { - adapterIndex = i; - break; + 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; } }