Files
Teodor Tanasoaia 81ee9d2ca8 Bug 2066780 - Update wgpu to revision 9b095f553c0701329b92a347f5ab02ec02e133fc. r=webgpu-reviewers,supply-chain-reviewers,nical
Upstream has split the id/registry ("remoting") machinery out of
wgpu-core into two new crates, `wgpu-core-remote` and
`wgpu-core-remote-types`. wgpu-core is now a direct `Arc`-based API, and
only browser-style consumers that have to mediate between an untrusted
content process and the GPU process layer the remoting crates on top.
Most of this patch is adapting Gecko to that split.

The split buys several things:
 - wgpu-core gets simpler for everyone else. wgpu itself and Deno hold
   `Arc`s directly and no longer pay for id indirection they never
   needed.
 - The remoting layer doesn't need to be multi-threaded, so the hub
   sheds the locking wgpu-core's registry needed and is now a plain
   `RefCell<Hub>`.
 - Security hardening. The IPC types Gecko deserialized in the GPU
   process used to be wgpu-core's own public types, so the untrusted
   content process could name native, non-standard wgpu features
   (raytracing, native-only feature bits, arbitrary descriptor fields)
   that WebGPU never exposes. `wgpu-core-remote-types` defines the wire
   types entirely separately, restricted to what standard WebGPU can
   express, so serde deserialization rejects those values outright
   instead of relying on Gecko to never construct them.
 - Firefox and Servo share one implementation of the per-command
   handling instead of each maintaining its own on top of wgpu-core.
 - Owning the hub outside wgpu-core opens the door to a real
   `device_destroy`: remove every child of the device from the hub and
   leave a "lost" sentinel behind, so memory is reclaimed as soon as any
   `Arc`s still held elsewhere are dropped when outstanding submissions
   finish.

gfx/wgpu_bindings:
 - Depend on the two new crates and run cbindgen over those instead of
   `wgpu-core`/`wgpu-hal`, since the FFI-visible types are now the
   purpose-built remoting types rather than wgpu-core internals.
 - The command enums, serializable descriptors and `IdentityHub` were a
   Gecko-local version of what `wgpu_core_remote_types` now provides,
   and the per-command server dispatch is now
   `Global::handle_*_command`. Both are deleted here, along with
   `src/client/render_pass.rs`. Only the `ExternalTextureSource` id
   space, which has no upstream equivalent, stays local.
 - `device_id` is dropped from the encoder, pass and queue-write entry
   points: the remote `Global` resolves an encoder's owning device
   itself, so content no longer needs to be trusted to supply it.
 - Resource teardown moves from `*_drop` to `*_remove` and backend
   handles come from `resolve_*_id().as_hal::<B>()`, matching the new
   hub ownership model.
 - wgpu-core now implements spec-conformant error scopes on `Device`, so
   the parallel Gecko implementation and the `ErrorBuffer` out-parameter
   protocol built around it are replaced by a serializable `GPUError`
   and `Global::device_{push,pop}_error_scope`. Parent-internal
   operations that still need a success/failure answer wrap themselves
   in a single validation error scope and return `bool`; hence the new
   `wgpu_server_submit_copy_texture_to_buffer`, which fuses
   create-encoder/copy/finish/submit/drop into one call and one error
   scope.
 - Uncaptured errors and device loss are raised by wgpu-core callbacks
   now rather than synthesised in C++, so they are reported from Rust
   over the existing `ServerMessage` stream. `WebGPUParentWeakPtr` wraps
   a C++ `WeakPtr<WebGPUParent>` so those async tasks cannot use a
   destroyed parent.

dom/webgpu:
 - The dedicated `PWebGPU::UncapturedError` and `PWebGPU::DeviceLost`
   messages are gone now that both travel in the `ServerMessage` stream;
   the child re-enters via
   `wgpu_child_handle_uncaptured_error`/`_device_lost`. With their last
   consumers gone, `WebGPUSerialize.h` and `PWebGPUTypes.ipdlh` are
   deleted, and `WebGPUParent` sheds the error-scope and device-lost
   machinery that duplicated state that's aleardy in wgpu-core.
 - Zero-sized buffer mappings are allowed, which makes mapped state
   awkward to infer from the mapped range, so `BufferMapData` tracks it
   directly in `mIsMapped`.
 - `SwapChainPresent` now returns the staging buffer id to the available
   pool via a scope guard on every error path, matching what the other
   readback path already did.
 - External texture import no longer raises `GPUInternalError` for
   failures such as a missing VideoBridge, an unsupported surface format
   or a failed fence wait. Per spec `GPUInternalError` is only ever
   generated by pipeline creation, so these were incorrect; they now log
   via `gfxCriticalError`/`NoteOnce`. The usability check in
   `ImportExternalTexture` still raises a validation error, as the spec
   requires.
 - Mechanical fallout from the type move: descriptors picked up an `Ffi`
   prefix, `device_id` is gone from the client-side encoder calls, and
   cbindgen no longer emits `_Sentinel` enum variants so the matching
   switch cases and assertions are dropped.

Differential Revision: https://phabricator.services.mozilla.com/D322889
2026-09-04 17:30:10 +00:00

625 lines
22 KiB
C++

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "Adapter.h"
#include <algorithm>
#include <bit>
#include "Device.h"
#include "Instance.h"
#include "SupportedFeatures.h"
#include "SupportedLimits.h"
#include "ipc/WebGPUChild.h"
#include "mozilla/dom/BindingDeclarations.h"
#include "mozilla/dom/Promise.h"
#include "mozilla/dom/WebGPUBinding.h"
#include "mozilla/webgpu/ffi/wgpu.h"
namespace mozilla::webgpu {
GPU_IMPL_CYCLE_COLLECTION(AdapterInfo, mParent)
GPU_IMPL_JS_WRAP(AdapterInfo)
uint32_t AdapterInfo::SubgroupMinSize() const {
// From the spec. at
// <https://www.w3.org/TR/2025/CRD-webgpu-20250319/#dom-gpuadapterinfo-subgroupminsize>:
//
// > If `["subgroups"](https://www.w3.org/TR/webgpu/#subgroups)` is supported,
// > set `subgroupMinSize` to the smallest supported subgroup size. Otherwise,
// > set this value to 4.
// >
// > Note: To preserve privacy, the user agent may choose to not support some
// > features or provide values for the property which do not distinguish
// > different devices, but are still usable (e.g. use the default value of
// > 4 for all devices).
if (GetParentObject()->ShouldResistFingerprinting(
RFPTarget::WebGPUSubgroupSizes)) {
return 4;
}
// TODO: When we support `subgroups`, use the supported amount instead:
// <https://bugzilla.mozilla.org/show_bug.cgi?id=1955417>
return 4;
}
uint32_t AdapterInfo::SubgroupMaxSize() const {
// From the spec. at
// <https://www.w3.org/TR/2025/CRD-webgpu-20250319/#dom-gpuadapterinfo-subgroupmaxsize>:
//
// > If `["subgroups"](https://www.w3.org/TR/webgpu/#subgroups)` is supported,
// > set `subgroupMaxSize` to the largest supported subgroup size. Otherwise,
// > set this value to 128.
// >
// > Note: To preserve privacy, the user agent may choose to not support some
// > features or provide values for the property which do not distinguish
// > different devices, but are still usable (e.g. use the default value of
// > 128 for all devices).
if (GetParentObject()->ShouldResistFingerprinting(
RFPTarget::WebGPUSubgroupSizes)) {
return 128;
}
// TODO: When we support `subgroups`, use the supported amount instead:
// <https://bugzilla.mozilla.org/show_bug.cgi?id=1955417>
return 128;
}
bool AdapterInfo::IsFallbackAdapter() const {
if (GetParentObject()->ShouldResistFingerprinting(
RFPTarget::WebGPUIsFallbackAdapter)) {
// Always report hardware support for WebGPU.
// This behaviour matches with media capabilities API.
return false;
}
return mAboutSupportInfo->device_type ==
ffi::WGPUDeviceType::WGPUDeviceType_Cpu;
}
void AdapterInfo::GetWgpuName(nsString& s) const {
s = mAboutSupportInfo->name;
}
uint32_t AdapterInfo::WgpuVendor() const { return mAboutSupportInfo->vendor; }
uint32_t AdapterInfo::WgpuDevice() const { return mAboutSupportInfo->device; }
void AdapterInfo::GetWgpuDeviceType(nsString& s) const {
switch (mAboutSupportInfo->device_type) {
case ffi::WGPUDeviceType_Cpu:
s.AssignLiteral("Cpu");
return;
case ffi::WGPUDeviceType_DiscreteGpu:
s.AssignLiteral("DiscreteGpu");
return;
case ffi::WGPUDeviceType_IntegratedGpu:
s.AssignLiteral("IntegratedGpu");
return;
case ffi::WGPUDeviceType_VirtualGpu:
s.AssignLiteral("VirtualGpu");
return;
case ffi::WGPUDeviceType_Other:
s.AssignLiteral("Other");
return;
}
MOZ_CRASH("Bad `ffi::WGPUDeviceType`");
}
void AdapterInfo::GetWgpuDriver(nsString& s) const {
s = mAboutSupportInfo->driver;
}
void AdapterInfo::GetWgpuDriverInfo(nsString& s) const {
s = mAboutSupportInfo->driver_info;
}
void AdapterInfo::GetWgpuBackend(nsString& s) const {
switch (mAboutSupportInfo->backend) {
case ffi::WGPUBackend_Noop:
s.AssignLiteral("No-op");
return;
case ffi::WGPUBackend_Vulkan:
s.AssignLiteral("Vulkan");
return;
case ffi::WGPUBackend_Metal:
s.AssignLiteral("Metal");
return;
case ffi::WGPUBackend_Dx12:
s.AssignLiteral("Dx12");
return;
case ffi::WGPUBackend_Gl:
s.AssignLiteral("Gl");
return;
case ffi::WGPUBackend_BrowserWebGpu:
break; // This should never happen, because
// we _are_ the browser.
}
MOZ_CRASH("Bad `ffi::WGPUBackend`");
}
// -
GPU_IMPL_CYCLE_COLLECTION(Adapter, mParent, mFeatures, mLimits, mInfo)
GPU_IMPL_JS_WRAP(Adapter)
enum class FeatureImplementationStatusTag {
Implemented,
NotImplemented,
};
struct FeatureImplementationStatus {
FeatureImplementationStatusTag tag =
FeatureImplementationStatusTag::NotImplemented;
union {
struct {
ffi::WGPUFeaturesWebGPU wgpuBit;
} implemented;
struct {
const char* bugzillaUrlAscii;
} unimplemented;
} value = {
.unimplemented = {
.bugzillaUrlAscii =
"https://bugzilla.mozilla.org/"
"enter_bug.cgi?product=Core&component=Graphics%3A+WebGPU"}};
static FeatureImplementationStatus fromDomFeature(
const dom::GPUFeatureName aFeature) {
auto implemented = [](const ffi::WGPUFeaturesWebGPU aBit) {
FeatureImplementationStatus feat;
feat.tag = FeatureImplementationStatusTag::Implemented;
feat.value.implemented.wgpuBit = aBit;
return feat;
};
auto unimplemented = [](const char* aBugzillaUrl) {
FeatureImplementationStatus feat;
feat.tag = FeatureImplementationStatusTag::NotImplemented;
feat.value.unimplemented.bugzillaUrlAscii = aBugzillaUrl;
return feat;
};
switch (aFeature) {
case dom::GPUFeatureName::Depth_clip_control:
return implemented(WGPUWEBGPU_FEATURE_DEPTH_CLIP_CONTROL);
case dom::GPUFeatureName::Depth32float_stencil8:
return implemented(WGPUWEBGPU_FEATURE_DEPTH32FLOAT_STENCIL8);
case dom::GPUFeatureName::Texture_compression_bc:
return implemented(WGPUWEBGPU_FEATURE_TEXTURE_COMPRESSION_BC);
case dom::GPUFeatureName::Texture_compression_bc_sliced_3d:
return implemented(WGPUWEBGPU_FEATURE_TEXTURE_COMPRESSION_BC_SLICED_3D);
case dom::GPUFeatureName::Texture_compression_etc2:
return implemented(WGPUWEBGPU_FEATURE_TEXTURE_COMPRESSION_ETC2);
case dom::GPUFeatureName::Texture_compression_astc:
return implemented(WGPUWEBGPU_FEATURE_TEXTURE_COMPRESSION_ASTC);
case dom::GPUFeatureName::Texture_compression_astc_sliced_3d:
return implemented(
WGPUWEBGPU_FEATURE_TEXTURE_COMPRESSION_ASTC_SLICED_3D);
case dom::GPUFeatureName::Timestamp_query:
return implemented(WGPUWEBGPU_FEATURE_TIMESTAMP_QUERY);
case dom::GPUFeatureName::Indirect_first_instance:
return implemented(WGPUWEBGPU_FEATURE_INDIRECT_FIRST_INSTANCE);
case dom::GPUFeatureName::Shader_f16:
return implemented(WGPUWEBGPU_FEATURE_SHADER_F16);
case dom::GPUFeatureName::Rg11b10ufloat_renderable:
return implemented(WGPUWEBGPU_FEATURE_RG11B10UFLOAT_RENDERABLE);
case dom::GPUFeatureName::Bgra8unorm_storage:
return implemented(WGPUWEBGPU_FEATURE_BGRA8UNORM_STORAGE);
case dom::GPUFeatureName::Float32_filterable:
return implemented(WGPUWEBGPU_FEATURE_FLOAT32_FILTERABLE);
case dom::GPUFeatureName::Float32_blendable:
return unimplemented(
"https://bugzilla.mozilla.org/show_bug.cgi?id=1931630");
case dom::GPUFeatureName::Clip_distances:
return unimplemented(
"https://bugzilla.mozilla.org/show_bug.cgi?id=1931629");
case dom::GPUFeatureName::Dual_source_blending:
return implemented(WGPUWEBGPU_FEATURE_DUAL_SOURCE_BLENDING);
case dom::GPUFeatureName::Subgroups:
// return implemented(WGPUWEBGPU_FEATURE_SUBGROUPS);
return unimplemented(
"https://bugzilla.mozilla.org/show_bug.cgi?id=1955417");
case dom::GPUFeatureName::Primitive_index:
// return implemented(WGPUWEBGPU_FEATURE_PRIMITIVE_INDEX);
return unimplemented(
"https://bugzilla.mozilla.org/show_bug.cgi?id=1989116");
case dom::GPUFeatureName::Core_features_and_limits:
// NOTE: `0` means that no bits are set in calling code, but this is on
// purpose. We currently _always_ return this feature elsewhere. If this
// actually corresponds to a value in the future, remove the
// unconditional setting of this feature!
return implemented(0);
}
MOZ_CRASH("Bad GPUFeatureName.");
}
};
Adapter::Adapter(Instance* const aParent, WebGPUChild* const aChild,
const std::shared_ptr<ffi::WGPUAdapterInformation>& aInfo)
: ObjectBase(aChild, aInfo->id, ffi::wgpu_client_drop_adapter),
ChildOf(aParent),
mFeatures(new SupportedFeatures(this)),
mLimits(new SupportedLimits(this, aInfo->limits)),
mInfo(new AdapterInfo(this, aInfo)),
mInfoInner(aInfo) {
ErrorResult ignoredRv; // It's onerous to plumb this in from outside in this
// case, and we don't really need to.
static const auto FEATURE_BY_BIT = []() {
auto ret =
std::unordered_map<ffi::WGPUFeaturesWebGPU, dom::GPUFeatureName>{};
for (const auto feature :
dom::MakeWebIDLEnumeratedRange<dom::GPUFeatureName>()) {
const auto status = FeatureImplementationStatus::fromDomFeature(feature);
switch (status.tag) {
case FeatureImplementationStatusTag::Implemented:
ret[status.value.implemented.wgpuBit] = feature;
break;
case FeatureImplementationStatusTag::NotImplemented:
break;
}
}
return ret;
}();
auto remainingFeatureBits = aInfo->features;
auto bitMask = decltype(remainingFeatureBits){0};
while (remainingFeatureBits) {
if (bitMask) {
bitMask <<= 1;
} else {
bitMask = 1;
}
const auto bit = remainingFeatureBits & bitMask;
remainingFeatureBits &= ~bitMask; // Clear bit.
if (!bit) {
continue;
}
const auto featureForBit = FEATURE_BY_BIT.find(bit);
if (featureForBit != FEATURE_BY_BIT.end()) {
mFeatures->Add(featureForBit->second, ignoredRv);
} else {
// One of two cases:
//
// 1. WGPU claims to implement this, but we've explicitly marked this as
// not implemented.
// 2. We don't recognize that bit, but maybe it's a wpgu-native-only
// feature.
}
}
// TODO: Once we implement compat mode (see
// <https://bugzilla.mozilla.org/show_bug.cgi?id=1905951>), do not report this
// unconditionally.
//
// Meanwhile, the current spec. proposal's `Initialization` section (see
// <https://github.com/gpuweb/gpuweb/blob/main/proposals/compatibility-mode.md#initialization>)
// says:
//
// > Core-defaulting adapters *always* support the
// > `"core-features-and-limits"` feature. It is *automatically enabled* on
// > devices created from such adapters.
mFeatures->Add(dom::GPUFeatureName::Core_features_and_limits, ignoredRv);
// We clamp limits to defaults when requestDevice is called, but
// we return the actual limits when only requestAdapter is called.
// So, we should clamp the limits here too if we should RFP.
if (GetParentObject()->ShouldResistFingerprinting(RFPTarget::WebGPULimits)) {
ffi::wgpu_client_fill_default_limits(mLimits->mFfi.get());
}
}
Adapter::~Adapter() = default;
const RefPtr<SupportedFeatures>& Adapter::Features() const { return mFeatures; }
const RefPtr<SupportedLimits>& Adapter::Limits() const { return mLimits; }
const RefPtr<AdapterInfo>& Adapter::Info() const { return mInfo; }
bool Adapter::SupportSharedTextureInSwapChain() const {
return mInfoInner->support_use_shared_texture_in_swap_chain;
}
static std::string_view ToJsKey(const Limit limit) {
switch (limit) {
case Limit::MaxTextureDimension1D:
return "maxTextureDimension1D";
case Limit::MaxTextureDimension2D:
return "maxTextureDimension2D";
case Limit::MaxTextureDimension3D:
return "maxTextureDimension3D";
case Limit::MaxTextureArrayLayers:
return "maxTextureArrayLayers";
case Limit::MaxBindGroups:
return "maxBindGroups";
case Limit::MaxBindGroupsPlusVertexBuffers:
return "maxBindGroupsPlusVertexBuffers";
case Limit::MaxBindingsPerBindGroup:
return "maxBindingsPerBindGroup";
case Limit::MaxDynamicUniformBuffersPerPipelineLayout:
return "maxDynamicUniformBuffersPerPipelineLayout";
case Limit::MaxDynamicStorageBuffersPerPipelineLayout:
return "maxDynamicStorageBuffersPerPipelineLayout";
case Limit::MaxSampledTexturesPerShaderStage:
return "maxSampledTexturesPerShaderStage";
case Limit::MaxSamplersPerShaderStage:
return "maxSamplersPerShaderStage";
case Limit::MaxStorageBuffersInVertexStage:
return "maxStorageBuffersInVertexStage";
case Limit::MaxStorageBuffersInFragmentStage:
return "maxStorageBuffersInFragmentStage";
case Limit::MaxStorageBuffersPerShaderStage:
return "maxStorageBuffersPerShaderStage";
case Limit::MaxStorageTexturesInVertexStage:
return "maxStorageTexturesInVertexStage";
case Limit::MaxStorageTexturesInFragmentStage:
return "maxStorageTexturesInFragmentStage";
case Limit::MaxStorageTexturesPerShaderStage:
return "maxStorageTexturesPerShaderStage";
case Limit::MaxUniformBuffersPerShaderStage:
return "maxUniformBuffersPerShaderStage";
case Limit::MaxUniformBufferBindingSize:
return "maxUniformBufferBindingSize";
case Limit::MaxStorageBufferBindingSize:
return "maxStorageBufferBindingSize";
case Limit::MinUniformBufferOffsetAlignment:
return "minUniformBufferOffsetAlignment";
case Limit::MinStorageBufferOffsetAlignment:
return "minStorageBufferOffsetAlignment";
case Limit::MaxVertexBuffers:
return "maxVertexBuffers";
case Limit::MaxBufferSize:
return "maxBufferSize";
case Limit::MaxVertexAttributes:
return "maxVertexAttributes";
case Limit::MaxVertexBufferArrayStride:
return "maxVertexBufferArrayStride";
case Limit::MaxInterStageShaderVariables:
return "maxInterStageShaderVariables";
case Limit::MaxColorAttachments:
return "maxColorAttachments";
case Limit::MaxColorAttachmentBytesPerSample:
return "maxColorAttachmentBytesPerSample";
case Limit::MaxComputeWorkgroupStorageSize:
return "maxComputeWorkgroupStorageSize";
case Limit::MaxComputeInvocationsPerWorkgroup:
return "maxComputeInvocationsPerWorkgroup";
case Limit::MaxComputeWorkgroupSizeX:
return "maxComputeWorkgroupSizeX";
case Limit::MaxComputeWorkgroupSizeY:
return "maxComputeWorkgroupSizeY";
case Limit::MaxComputeWorkgroupSizeZ:
return "maxComputeWorkgroupSizeZ";
case Limit::MaxComputeWorkgroupsPerDimension:
return "maxComputeWorkgroupsPerDimension";
}
MOZ_CRASH("Bad Limit");
}
uint64_t Adapter::MissingFeatures() const {
uint64_t missingFeatures = 0;
// Turn on all implemented features.
for (const auto feature :
dom::MakeWebIDLEnumeratedRange<dom::GPUFeatureName>()) {
const auto status = FeatureImplementationStatus::fromDomFeature(feature);
switch (status.tag) {
case FeatureImplementationStatusTag::Implemented:
missingFeatures |= status.value.implemented.wgpuBit;
break;
case FeatureImplementationStatusTag::NotImplemented:
break;
}
}
// Turn off features that are supported by the adapter.
for (auto feature : mFeatures->Features()) {
const auto status = FeatureImplementationStatus::fromDomFeature(feature);
switch (status.tag) {
case FeatureImplementationStatusTag::Implemented:
missingFeatures &= ~status.value.implemented.wgpuBit;
break;
case FeatureImplementationStatusTag::NotImplemented:
break;
}
}
return missingFeatures;
}
// -
// String helpers
static auto ToACString(const nsAString& s) { return NS_ConvertUTF16toUTF8(s); }
// -
// Adapter::RequestDevice
already_AddRefed<dom::Promise> Adapter::RequestDevice(
const dom::GPUDeviceDescriptor& aDesc, ErrorResult& aRv) {
RefPtr<dom::Promise> promise = dom::Promise::Create(GetParentObject(), aRv);
if (NS_WARN_IF(aRv.Failed())) {
return nullptr;
}
RefPtr<dom::Promise> lost_promise =
dom::Promise::Create(GetParentObject(), aRv);
if (NS_WARN_IF(aRv.Failed())) {
return nullptr;
}
ffi::WGPULimits deviceLimits = {};
ffi::wgpu_client_fill_default_limits(&deviceLimits);
// -
[&]() { // So that we can `return;` instead of `return promise.forget();`.
// -
// Validate Features
ffi::WGPUFeaturesWebGPU featureBits = 0;
for (const auto requested : aDesc.mRequiredFeatures) {
auto status = FeatureImplementationStatus::fromDomFeature(requested);
switch (status.tag) {
case FeatureImplementationStatusTag::Implemented:
featureBits |= status.value.implemented.wgpuBit;
break;
case FeatureImplementationStatusTag::NotImplemented: {
const auto featureStr = dom::GetEnumString(requested);
(void)featureStr;
nsPrintfCString msg(
"`GPUAdapter.requestDevice`: '%s' was requested in "
"`requiredFeatures`, but it is not supported by Firefox. "
"Follow <%s> for updates.",
featureStr.get(), status.value.unimplemented.bugzillaUrlAscii);
promise->MaybeRejectWithTypeError(msg);
return;
}
}
const bool supportedByAdapter = mFeatures->Features().count(requested);
if (!supportedByAdapter) {
const auto fstr = dom::GetEnumString(requested);
const auto astr = this->LabelOrId();
nsPrintfCString msg(
"`GPUAdapter.requestDevice`: '%s' was requested in "
"`requiredFeatures`, but it is not supported by adapter %s.",
fstr.get(), astr.get());
promise->MaybeRejectWithTypeError(msg);
return;
}
}
// -
// Validate Limits
if (aDesc.mRequiredLimits.WasPassed()) {
static const auto LIMIT_BY_JS_KEY = []() {
std::unordered_map<std::string_view, Limit> ret;
for (const auto limit : MakeInclusiveEnumeratedRange(Limit::_LAST)) {
const auto jsKeyU8 = ToJsKey(limit);
ret[jsKeyU8] = limit;
}
return ret;
}();
for (const auto& entry : aDesc.mRequiredLimits.Value().Entries()) {
const auto& keyU16 = entry.mKey;
const nsCString keyU8 = ToACString(keyU16);
const auto itr = LIMIT_BY_JS_KEY.find(keyU8.get());
if (itr == LIMIT_BY_JS_KEY.end()) {
nsPrintfCString msg("requestDevice: Limit '%s' not recognized.",
keyU8.get());
promise->MaybeRejectWithOperationError(msg);
return;
}
const auto& limit = itr->second;
uint64_t requestedValue = entry.mValue;
const auto supportedValue = GetLimit(*mLimits->mFfi, limit);
if (StringBeginsWith(keyU8, "max"_ns)) {
if (requestedValue > supportedValue) {
nsPrintfCString msg(
"requestDevice: Request for limit '%s' must be <= supported "
"%s, was %s.",
keyU8.get(), std::to_string(supportedValue).c_str(),
std::to_string(requestedValue).c_str());
promise->MaybeRejectWithOperationError(msg);
return;
}
// Clamp to default if lower than default
requestedValue =
std::max(requestedValue, GetLimit(deviceLimits, limit));
} else {
MOZ_ASSERT(StringBeginsWith(keyU8, "min"_ns));
if (requestedValue < supportedValue) {
nsPrintfCString msg(
"requestDevice: Request for limit '%s' must be >= supported "
"%s, was %s.",
keyU8.get(), std::to_string(supportedValue).c_str(),
std::to_string(requestedValue).c_str());
promise->MaybeRejectWithOperationError(msg);
return;
}
if (StringEndsWith(keyU8, "Alignment"_ns)) {
if (!std::has_single_bit(requestedValue)) {
nsPrintfCString msg(
"requestDevice: Request for limit '%s' must be a power of "
"two, "
"was %s.",
keyU8.get(), std::to_string(requestedValue).c_str());
promise->MaybeRejectWithOperationError(msg);
return;
}
}
/// Clamp to default if higher than default
/// Changing implementation in a way that increases fingerprinting
/// surface? Please create a bug in [Core::Privacy: Anti
/// Tracking](https://bugzilla.mozilla.org/enter_bug.cgi?product=Core&component=Privacy%3A%20Anti-Tracking)
requestedValue =
std::min(requestedValue, GetLimit(deviceLimits, limit));
}
SetLimit(&deviceLimits, limit, requestedValue);
}
}
// -
RefPtr<SupportedFeatures> features = new SupportedFeatures(this);
for (const auto& feature : aDesc.mRequiredFeatures) {
features->Add(feature, aRv);
}
// TODO: Once we implement compat mode (see
// <https://bugzilla.mozilla.org/show_bug.cgi?id=1905951>), do not report
// this unconditionally.
//
// Meanwhile, the current spec. proposal's `Initialization` section (see
// <https://github.com/gpuweb/gpuweb/blob/main/proposals/compatibility-mode.md#initialization>)
// says:
//
// > Core-defaulting adapters *always* support the
// > `"core-features-and-limits"` feature. It is *automatically enabled* on
// > devices created from such adapters.
features->Add(dom::GPUFeatureName::Core_features_and_limits, aRv);
RefPtr<SupportedLimits> limits = new SupportedLimits(this, deviceLimits);
ffi::WGPUFfiDeviceDescriptor ffiDesc = {};
ffiDesc.required_features = featureBits;
ffiDesc.required_limits = deviceLimits;
ffi::WGPUDeviceQueueId ids =
ffi::wgpu_client_request_device(GetClient(), GetId(), &ffiDesc);
GetChild()->EnqueueRequestDevicePromise(PendingRequestDevicePromise{
promise, ids.device, ids.queue, aDesc.mLabel, this, std::move(features),
std::move(limits), mInfo, std::move(lost_promise)});
}();
return promise.forget();
}
} // namespace mozilla::webgpu