1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
|
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/blink/renderer/modules/webgpu/gpu_adapter.h"
#include "services/metrics/public/cpp/ukm_builders.h"
#include "third_party/blink/renderer/bindings/core/v8/script_promise_resolver.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_object_builder.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_gpu_device_descriptor.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_gpu_queue_descriptor.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_gpu_request_adapter_options.h"
#include "third_party/blink/renderer/core/dom/dom_exception.h"
#include "third_party/blink/renderer/core/inspector/console_message.h"
#include "third_party/blink/renderer/modules/webgpu/dawn_enum_conversions.h"
#include "third_party/blink/renderer/modules/webgpu/gpu.h"
#include "third_party/blink/renderer/modules/webgpu/gpu_adapter_info.h"
#include "third_party/blink/renderer/modules/webgpu/gpu_device.h"
#include "third_party/blink/renderer/modules/webgpu/gpu_device_lost_info.h"
#include "third_party/blink/renderer/modules/webgpu/gpu_memory_heap_info.h"
#include "third_party/blink/renderer/modules/webgpu/gpu_subgroup_matrix_config.h"
#include "third_party/blink/renderer/modules/webgpu/gpu_supported_features.h"
#include "third_party/blink/renderer/modules/webgpu/gpu_supported_limits.h"
#include "third_party/blink/renderer/modules/webgpu/string_utils.h"
#include "third_party/blink/renderer/platform/heap/garbage_collected.h"
namespace blink {
namespace {
GPUSupportedFeatures* MakeFeatureNameSet(wgpu::Adapter adapter) {
GPUSupportedFeatures* features = MakeGarbageCollected<GPUSupportedFeatures>();
DCHECK(features->FeatureNameSet().empty());
wgpu::SupportedFeatures supported_features;
adapter.GetFeatures(&supported_features);
// SAFETY: Required from caller
const auto features_span = UNSAFE_BUFFERS(base::span<const wgpu::FeatureName>(
supported_features.features, supported_features.featureCount));
for (const auto& f : features_span) {
auto feature_name_enum_optional =
GPUSupportedFeatures::ToV8FeatureNameEnum(f);
if (feature_name_enum_optional) {
features->AddFeatureName(
V8GPUFeatureName(feature_name_enum_optional.value()));
}
}
return features;
}
} // anonymous namespace
GPUAdapter::GPUAdapter(
GPU* gpu,
wgpu::Adapter handle,
scoped_refptr<DawnControlClientHolder> dawn_control_client,
const GPURequestAdapterOptions* options)
: DawnObject(dawn_control_client, std::move(handle), String()), gpu_(gpu) {
wgpu::AdapterInfo info = {};
wgpu::ChainedStructOut** propertiesChain = &info.nextInChain;
if (GetHandle().HasFeature(wgpu::FeatureName::AdapterPropertiesMemoryHeaps)) {
*propertiesChain = &memory_heaps_;
propertiesChain = &(*propertiesChain)->nextInChain;
}
if (GetHandle().HasFeature(
wgpu::FeatureName::ChromiumExperimentalSubgroupMatrix)) {
*propertiesChain = &subgroup_matrix_configs_;
propertiesChain = &(*propertiesChain)->nextInChain;
}
wgpu::AdapterPropertiesD3D d3dProperties = {};
bool supportsPropertiesD3D =
GetHandle().HasFeature(wgpu::FeatureName::AdapterPropertiesD3D);
if (supportsPropertiesD3D) {
*propertiesChain = &d3dProperties;
propertiesChain = &(*propertiesChain)->nextInChain;
}
wgpu::AdapterPropertiesVk vkProperties = {};
bool supportsPropertiesVk =
GetHandle().HasFeature(wgpu::FeatureName::AdapterPropertiesVk);
if (supportsPropertiesVk) {
*propertiesChain = &vkProperties;
propertiesChain = &(*propertiesChain)->nextInChain;
}
wgpu::DawnAdapterPropertiesPowerPreference powerProperties{};
*propertiesChain = &powerProperties;
propertiesChain = &(*propertiesChain)->nextInChain;
GetHandle().GetInfo(&info);
is_fallback_adapter_ = info.adapterType == wgpu::AdapterType::CPU;
adapter_type_ = info.adapterType;
backend_type_ = info.backendType;
// TODO(crbug.com/359418629): Report xr compatibility in GetInfo()
is_xr_compatible_ = options->xrCompatible();
vendor_ = String::FromUTF8(info.vendor);
architecture_ = String::FromUTF8(info.architecture);
if (info.deviceID <= 0xffff) {
device_ = String::Format("0x%04x", info.deviceID);
} else {
device_ = String::Format("0x%08x", info.deviceID);
}
description_ = String::FromUTF8(info.device);
driver_ = String::FromUTF8(info.description);
if (supportsPropertiesD3D) {
d3d_shader_model_ = d3dProperties.shaderModel;
}
if (supportsPropertiesVk) {
vk_driver_version_ = vkProperties.driverVersion;
}
subgroup_min_size_ = info.subgroupMinSize;
subgroup_max_size_ = info.subgroupMaxSize;
power_preference_ = powerProperties.powerPreference;
features_ = MakeFeatureNameSet(GetHandle());
wgpu::Limits limits = {};
GetHandle().GetLimits(&limits);
limits_ = MakeGarbageCollected<GPUSupportedLimits>(limits);
info_ = CreateAdapterInfoForAdapter();
}
GPUAdapterInfo* GPUAdapter::CreateAdapterInfoForAdapter() {
GPUAdapterInfo* info;
if (RuntimeEnabledFeatures::WebGPUDeveloperFeaturesEnabled()) {
// If WebGPU developer features have been enabled then provide all available
// adapter info values.
info = MakeGarbageCollected<GPUAdapterInfo>(
vendor_, architecture_, subgroup_min_size_, subgroup_max_size_,
is_fallback_adapter_, device_, description_, driver_,
FromDawnEnum(backend_type_), FromDawnEnum(adapter_type_),
d3d_shader_model_, vk_driver_version_, FromDawnEnum(power_preference_));
// SAFETY: Required from caller
const auto memory_heaps_span =
UNSAFE_BUFFERS(base::span<const wgpu::MemoryHeapInfo>(
memory_heaps_.heapInfo, memory_heaps_.heapCount));
for (const auto& m : memory_heaps_span) {
info->AppendMemoryHeapInfo(MakeGarbageCollected<GPUMemoryHeapInfo>(m));
}
} else {
info = MakeGarbageCollected<GPUAdapterInfo>(
vendor_, architecture_, subgroup_min_size_, subgroup_max_size_,
is_fallback_adapter_);
}
// SAFETY: Required from caller
const auto subgroup_matrix_configs_span =
UNSAFE_BUFFERS(base::span<const wgpu::SubgroupMatrixConfig>(
subgroup_matrix_configs_.configs,
subgroup_matrix_configs_.configCount));
for (const auto& c : subgroup_matrix_configs_span) {
info->AppendSubgroupMatrixConfig(
MakeGarbageCollected<GPUSubgroupMatrixConfig>(c));
}
return info;
}
void GPUAdapter::AddConsoleWarning(ExecutionContext* execution_context,
const char* message) {
if (execution_context && allowed_console_warnings_remaining_ > 0) {
auto* console_message = MakeGarbageCollected<ConsoleMessage>(
mojom::blink::ConsoleMessageSource::kRendering,
mojom::blink::ConsoleMessageLevel::kWarning,
StringFromASCIIAndUTF8(message));
execution_context->AddConsoleMessage(console_message);
allowed_console_warnings_remaining_--;
if (allowed_console_warnings_remaining_ == 0) {
auto* final_message = MakeGarbageCollected<ConsoleMessage>(
mojom::blink::ConsoleMessageSource::kRendering,
mojom::blink::ConsoleMessageLevel::kWarning,
"WebGPU: too many warnings, no more warnings will be reported to the "
"console for this GPUAdapter.");
execution_context->AddConsoleMessage(final_message);
}
}
}
GPUSupportedFeatures* GPUAdapter::features() const {
return features_.Get();
}
GPUAdapterInfo* GPUAdapter::info() const {
return info_.Get();
}
bool GPUAdapter::isFallbackAdapter() const {
return is_fallback_adapter_;
}
wgpu::BackendType GPUAdapter::backendType() const {
return backend_type_;
}
bool GPUAdapter::SupportsMultiPlanarFormats() const {
return GetHandle().HasFeature(wgpu::FeatureName::DawnMultiPlanarFormats);
}
void GPUAdapter::OnRequestDeviceCallback(
GPUDevice* device,
const GPUDeviceDescriptor* descriptor,
ScriptPromiseResolver<GPUDevice>* resolver,
wgpu::RequestDeviceStatus status,
wgpu::Device dawn_device,
wgpu::StringView error_message) {
switch (status) {
case wgpu::RequestDeviceStatus::Success: {
DCHECK(dawn_device);
GPUDeviceLostInfo* device_lost_info = nullptr;
if (is_consumed_) {
// Immediately force the device to be lost.
// TODO: Ideally this should be handled in Dawn, which can return an
// error device.
device_lost_info = MakeGarbageCollected<GPUDeviceLostInfo>(
wgpu::DeviceLostReason::Unknown,
StringFromASCIIAndUTF8(
"The adapter is invalid because it has already been used to "
"create a device. A lost device has been returned."));
}
is_consumed_ = true;
device->Initialize(dawn_device, descriptor, device_lost_info);
if (device_lost_info) {
// Ensure the Dawn device is marked as lost as well.
device->InjectError(
wgpu::ErrorType::Internal,
"Device was marked as lost due to a stale adapter.");
}
resolver->Resolve(device);
ukm::builders::ClientRenderingAPI(
device->GetExecutionContext()->UkmSourceID())
.SetGPUDevice(static_cast<int>(true))
.Record(device->GetExecutionContext()->UkmRecorder());
break;
}
case wgpu::RequestDeviceStatus::Error:
case wgpu::RequestDeviceStatus::CallbackCancelled:
if (dawn_device) {
// A device provided with an error is already a lost device on the Dawn
// side, reflect that by resolving the lost property immediately.
device->Initialize(dawn_device, descriptor,
MakeGarbageCollected<GPUDeviceLostInfo>(
wgpu::DeviceLostReason::Unknown,
StringFromASCIIAndUTF8(error_message)));
// Resolve with the lost device.
resolver->Resolve(device);
} else {
// If a device is not returned, that means that an error occurred while
// validating features or limits, and as a result the promise should be
// rejected with an OperationError.
resolver->RejectWithDOMException(DOMExceptionCode::kOperationError,
StringFromASCIIAndUTF8(error_message));
}
break;
}
}
ScriptPromise<GPUDevice> GPUAdapter::requestDevice(
ScriptState* script_state,
GPUDeviceDescriptor* descriptor) {
auto* resolver = MakeGarbageCollected<ScriptPromiseResolver<GPUDevice>>(
script_state, ExceptionContext(v8::ExceptionContext::kOperation,
"GPUAdapter", "requestDevice"));
auto promise = resolver->Promise();
wgpu::DeviceDescriptor dawn_desc = {};
wgpu::Limits required_limits = {};
if (descriptor->hasRequiredLimits()) {
dawn_desc.requiredLimits = &required_limits;
GPUSupportedLimits::MakeUndefined(&required_limits);
if (!GPUSupportedLimits::Populate(&required_limits,
descriptor->requiredLimits(), resolver)) {
return promise;
}
}
Vector<wgpu::FeatureName> required_features;
// The ShaderModuleCompilationOptions feature is required only if the adapter
// has the ShaderModuleCompilationOptions feature and the user has enabled the
// WebGPUDeveloperFeatures flag. It is needed to control
// strict math during shader module compilation.
if (RuntimeEnabledFeatures::WebGPUDeveloperFeaturesEnabled() &&
GetHandle().HasFeature(
wgpu::FeatureName::ShaderModuleCompilationOptions)) {
required_features.push_back(
wgpu::FeatureName::ShaderModuleCompilationOptions);
}
if (descriptor->hasRequiredFeatures()) {
// Insert features into a set to dedup them.
HashSet<wgpu::FeatureName> required_features_set;
for (const V8GPUFeatureName& f : descriptor->requiredFeatures()) {
// If the feature is not a valid feature reject with a type error.
if (!features_->Has(f.AsEnum())) {
resolver->RejectWithTypeError(
String::Format("Unsupported feature: %s", f.AsCStr()));
return promise;
}
required_features_set.insert(AsDawnEnum(f));
}
// Then, push the deduped features into a vector.
required_features.AppendRange(required_features_set.begin(),
required_features_set.end());
dawn_desc.requiredFeatures = required_features.data();
dawn_desc.requiredFeatureCount = required_features.size();
}
std::string label = descriptor->label().Utf8();
if (!label.empty()) {
dawn_desc.label = label.c_str();
}
std::string queueLabel = descriptor->defaultQueue()->label().Utf8();
if (!queueLabel.empty()) {
dawn_desc.defaultQueue.label = queueLabel.c_str();
}
// Create a GPUDevice without the handle, so that we can set up its callbacks
// in the wgpu::DeviceDescriptor.
ExecutionContext* execution_context = ExecutionContext::From(script_state);
auto* device = MakeGarbageCollected<GPUDevice>(
execution_context, GetDawnControlClient(), this, descriptor->label());
device->SetDescriptorCallbacks(dawn_desc);
auto* callback = MakeWGPUOnceCallback(resolver->WrapCallbackInScriptScope(
WTF::BindOnce(&GPUAdapter::OnRequestDeviceCallback, WrapPersistent(this),
WrapPersistent(device), WrapPersistent(descriptor))));
GetHandle().RequestDevice(&dawn_desc, wgpu::CallbackMode::AllowSpontaneous,
callback->UnboundCallback(),
callback->AsUserdata());
EnsureFlush(ToEventLoop(script_state));
return promise;
}
void GPUAdapter::Trace(Visitor* visitor) const {
visitor->Trace(gpu_);
visitor->Trace(features_);
visitor->Trace(limits_);
visitor->Trace(info_);
ScriptWrappable::Trace(visitor);
}
} // namespace blink
|