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
287 lines
10 KiB
C++
287 lines
10 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 "CommandEncoder.h"
|
|
|
|
#include "Buffer.h"
|
|
#include "CommandBuffer.h"
|
|
#include "ComputePassEncoder.h"
|
|
#include "Device.h"
|
|
#include "ExternalTexture.h"
|
|
#include "RenderPassEncoder.h"
|
|
#include "TextureView.h"
|
|
#include "Utility.h"
|
|
#include "mozilla/dom/WebGPUBinding.h"
|
|
#include "mozilla/webgpu/CanvasContext.h"
|
|
#include "mozilla/webgpu/ffi/wgpu.h"
|
|
|
|
namespace mozilla::webgpu {
|
|
|
|
GPU_IMPL_CYCLE_COLLECTION(CommandEncoder, mParent, mExternalTextures)
|
|
GPU_IMPL_JS_WRAP(CommandEncoder)
|
|
|
|
void CommandEncoder::ConvertTextureDataLayoutToFFI(
|
|
const dom::GPUTexelCopyBufferLayout& aLayout,
|
|
ffi::WGPUFfiTexelCopyBufferLayout* aLayoutFFI) {
|
|
*aLayoutFFI = {};
|
|
aLayoutFFI->offset = aLayout.mOffset;
|
|
|
|
if (aLayout.mBytesPerRow.WasPassed()) {
|
|
aLayoutFFI->bytes_per_row = &aLayout.mBytesPerRow.Value();
|
|
} else {
|
|
aLayoutFFI->bytes_per_row = nullptr;
|
|
}
|
|
|
|
if (aLayout.mRowsPerImage.WasPassed()) {
|
|
aLayoutFFI->rows_per_image = &aLayout.mRowsPerImage.Value();
|
|
} else {
|
|
aLayoutFFI->rows_per_image = nullptr;
|
|
}
|
|
}
|
|
|
|
void CommandEncoder::ConvertTextureCopyViewToFFI(
|
|
const dom::GPUTexelCopyTextureInfo& aCopy,
|
|
ffi::WGPUTexelCopyTextureInfo* aViewFFI) {
|
|
*aViewFFI = {};
|
|
aViewFFI->texture = aCopy.mTexture->GetId();
|
|
aViewFFI->mip_level = aCopy.mMipLevel;
|
|
const auto& origin = aCopy.mOrigin;
|
|
if (origin.IsRangeEnforcedUnsignedLongSequence()) {
|
|
const auto& seq = origin.GetAsRangeEnforcedUnsignedLongSequence();
|
|
aViewFFI->origin.x = seq.Length() > 0 ? seq[0] : 0;
|
|
aViewFFI->origin.y = seq.Length() > 1 ? seq[1] : 0;
|
|
aViewFFI->origin.z = seq.Length() > 2 ? seq[2] : 0;
|
|
} else if (origin.IsGPUOrigin3DDict()) {
|
|
const auto& dict = origin.GetAsGPUOrigin3DDict();
|
|
aViewFFI->origin.x = dict.mX;
|
|
aViewFFI->origin.y = dict.mY;
|
|
aViewFFI->origin.z = dict.mZ;
|
|
} else {
|
|
MOZ_CRASH("Unexpected origin type");
|
|
}
|
|
aViewFFI->aspect = ConvertTextureAspect(aCopy.mAspect);
|
|
}
|
|
|
|
static ffi::WGPUTexelCopyTextureInfo ConvertTextureCopyView(
|
|
const dom::GPUTexelCopyTextureInfo& aCopy) {
|
|
ffi::WGPUTexelCopyTextureInfo view = {};
|
|
CommandEncoder::ConvertTextureCopyViewToFFI(aCopy, &view);
|
|
return view;
|
|
}
|
|
|
|
CommandEncoder::CommandEncoder(Device* const aParent, RawId aId)
|
|
: ObjectBase(aParent->GetChild(), aId,
|
|
ffi::wgpu_client_drop_command_encoder),
|
|
ChildOf(aParent) {}
|
|
|
|
CommandEncoder::~CommandEncoder() = default;
|
|
|
|
void CommandEncoder::TrackPresentationContext(
|
|
WeakPtr<CanvasContext> aTargetContext) {
|
|
if (aTargetContext) {
|
|
mPresentationContexts.AppendElement(aTargetContext);
|
|
}
|
|
}
|
|
|
|
void CommandEncoder::CopyBufferToBuffer(
|
|
const Buffer& aSource, BufferAddress aSourceOffset,
|
|
const Buffer& aDestination, BufferAddress aDestinationOffset,
|
|
const dom::Optional<BufferAddress>& aSize) {
|
|
// In Javascript, `size === undefined` means "copy from source offset to end
|
|
// of buffer". wgpu_client_command_encoder_copy_buffer_to_buffer uses a value
|
|
// of UINT64_MAX to encode this. If the requested copy size was UINT64_MAX,
|
|
// fudge it to a different value that will still be rejected for misalignment
|
|
// on the device timeline.
|
|
BufferAddress size;
|
|
if (aSize.WasPassed()) {
|
|
if (aSize.Value() == std::numeric_limits<uint64_t>::max()) {
|
|
size = std::numeric_limits<uint64_t>::max() - 4;
|
|
} else {
|
|
size = aSize.Value();
|
|
}
|
|
} else {
|
|
size = std::numeric_limits<uint64_t>::max();
|
|
}
|
|
|
|
ffi::wgpu_client_command_encoder_copy_buffer_to_buffer(
|
|
GetClient(), GetId(), aSource.GetId(), aSourceOffset,
|
|
aDestination.GetId(), aDestinationOffset, size);
|
|
}
|
|
|
|
void CommandEncoder::CopyBufferToTexture(
|
|
const dom::GPUTexelCopyBufferInfo& aSource,
|
|
const dom::GPUTexelCopyTextureInfo& aDestination,
|
|
const dom::GPUExtent3D& aCopySize) {
|
|
ffi::WGPUFfiTexelCopyBufferLayout src_layout = {};
|
|
CommandEncoder::ConvertTextureDataLayoutToFFI(aSource, &src_layout);
|
|
ffi::wgpu_client_command_encoder_copy_buffer_to_texture(
|
|
GetClient(), GetId(), aSource.mBuffer->GetId(), &src_layout,
|
|
ConvertTextureCopyView(aDestination), ConvertExtent(aCopySize));
|
|
|
|
TrackPresentationContext(aDestination.mTexture->mTargetContext);
|
|
}
|
|
void CommandEncoder::CopyTextureToBuffer(
|
|
const dom::GPUTexelCopyTextureInfo& aSource,
|
|
const dom::GPUTexelCopyBufferInfo& aDestination,
|
|
const dom::GPUExtent3D& aCopySize) {
|
|
ffi::WGPUFfiTexelCopyBufferLayout dstLayout = {};
|
|
CommandEncoder::ConvertTextureDataLayoutToFFI(aDestination, &dstLayout);
|
|
ffi::wgpu_client_command_encoder_copy_texture_to_buffer(
|
|
GetClient(), GetId(), ConvertTextureCopyView(aSource),
|
|
aDestination.mBuffer->GetId(), &dstLayout, ConvertExtent(aCopySize));
|
|
}
|
|
void CommandEncoder::CopyTextureToTexture(
|
|
const dom::GPUTexelCopyTextureInfo& aSource,
|
|
const dom::GPUTexelCopyTextureInfo& aDestination,
|
|
const dom::GPUExtent3D& aCopySize) {
|
|
ffi::wgpu_client_command_encoder_copy_texture_to_texture(
|
|
GetClient(), GetId(), ConvertTextureCopyView(aSource),
|
|
ConvertTextureCopyView(aDestination), ConvertExtent(aCopySize));
|
|
|
|
TrackPresentationContext(aDestination.mTexture->mTargetContext);
|
|
}
|
|
|
|
void CommandEncoder::ClearBuffer(const Buffer& aBuffer, const uint64_t aOffset,
|
|
const dom::Optional<uint64_t>& aSize) {
|
|
uint64_t sizeVal = 0xdeaddead;
|
|
uint64_t* size = nullptr;
|
|
if (aSize.WasPassed()) {
|
|
sizeVal = aSize.Value();
|
|
size = &sizeVal;
|
|
}
|
|
|
|
ffi::wgpu_client_command_encoder_clear_buffer(GetClient(), GetId(),
|
|
aBuffer.GetId(), aOffset, size);
|
|
}
|
|
|
|
void CommandEncoder::PushDebugGroup(const nsAString& aString) {
|
|
NS_ConvertUTF16toUTF8 marker(aString);
|
|
ffi::wgpu_client_command_encoder_push_debug_group(GetClient(), GetId(),
|
|
&marker);
|
|
}
|
|
void CommandEncoder::PopDebugGroup() {
|
|
ffi::wgpu_client_command_encoder_pop_debug_group(GetClient(), GetId());
|
|
}
|
|
void CommandEncoder::InsertDebugMarker(const nsAString& aString) {
|
|
NS_ConvertUTF16toUTF8 marker(aString);
|
|
ffi::wgpu_client_command_encoder_insert_debug_marker(GetClient(), GetId(),
|
|
&marker);
|
|
}
|
|
|
|
already_AddRefed<ComputePassEncoder> CommandEncoder::BeginComputePass(
|
|
const dom::GPUComputePassDescriptor& aDesc) {
|
|
ffi::WGPUFfiComputePassDescriptor desc = {};
|
|
|
|
webgpu::StringHelper label(aDesc.mLabel);
|
|
desc.label = label.Get();
|
|
|
|
ffi::WGPUPassTimestampWrites passTimestampWrites = {};
|
|
if (aDesc.mTimestampWrites.WasPassed()) {
|
|
AssignPassTimestampWrites(aDesc.mTimestampWrites.Value(),
|
|
passTimestampWrites);
|
|
desc.timestamp_writes = &passTimestampWrites;
|
|
}
|
|
|
|
RawId id = ffi::wgpu_client_command_encoder_begin_compute_pass(
|
|
GetClient(), GetId(), &desc);
|
|
RefPtr<ComputePassEncoder> pass = new ComputePassEncoder(this, id);
|
|
pass->SetLabel(aDesc.mLabel);
|
|
return pass.forget();
|
|
}
|
|
|
|
already_AddRefed<RenderPassEncoder> CommandEncoder::BeginRenderPass(
|
|
const dom::GPURenderPassDescriptor& aDesc) {
|
|
dom::GPURenderPassDescriptor desc{aDesc};
|
|
|
|
auto coerceToViewInPlace =
|
|
[](dom::OwningGPUTextureOrGPUTextureView& texOrView)
|
|
-> RefPtr<TextureView> {
|
|
RefPtr<TextureView> view;
|
|
switch (texOrView.GetType()) {
|
|
case dom::OwningGPUTextureOrGPUTextureView::Type::eGPUTexture: {
|
|
dom::GPUTextureViewDescriptor defaultDesc{};
|
|
RefPtr<Texture> tex = texOrView.GetAsGPUTexture();
|
|
texOrView.SetAsGPUTextureView() = tex->CreateView(defaultDesc);
|
|
break;
|
|
}
|
|
|
|
case dom::OwningGPUTextureOrGPUTextureView::Type::eGPUTextureView:
|
|
// Nothing to do, great!
|
|
break;
|
|
}
|
|
view = texOrView.GetAsGPUTextureView();
|
|
return view;
|
|
};
|
|
|
|
for (auto& atOrNull : desc.mColorAttachments) {
|
|
if (atOrNull.IsNull()) {
|
|
continue;
|
|
}
|
|
auto& at = atOrNull.Value();
|
|
TrackPresentationContext(coerceToViewInPlace(at.mView)->GetTargetContext());
|
|
if (at.mResolveTarget.WasPassed()) {
|
|
TrackPresentationContext(
|
|
coerceToViewInPlace(at.mResolveTarget.Value())->GetTargetContext());
|
|
}
|
|
}
|
|
if (desc.mDepthStencilAttachment.WasPassed()) {
|
|
coerceToViewInPlace(desc.mDepthStencilAttachment.Value().mView);
|
|
}
|
|
|
|
auto id = BeginFfiRenderPass(GetClient(), GetId(), desc);
|
|
RefPtr<RenderPassEncoder> pass = new RenderPassEncoder(this, id);
|
|
pass->SetLabel(desc.mLabel);
|
|
return pass.forget();
|
|
}
|
|
|
|
void CommandEncoder::ResolveQuerySet(QuerySet& aQuerySet, uint32_t aFirstQuery,
|
|
uint32_t aQueryCount,
|
|
webgpu::Buffer& aDestination,
|
|
uint64_t aDestinationOffset) {
|
|
ffi::wgpu_client_command_encoder_resolve_query_set(
|
|
GetClient(), GetId(), aQuerySet.GetId(), aFirstQuery, aQueryCount,
|
|
aDestination.GetId(), aDestinationOffset);
|
|
}
|
|
|
|
void CommandEncoder::EndComputePass(
|
|
RawId aComputePassEncoderId, CanvasContextArray& aCanvasContexts,
|
|
Span<RefPtr<ExternalTexture>> aExternalTextures) {
|
|
for (const auto& context : aCanvasContexts) {
|
|
TrackPresentationContext(context);
|
|
}
|
|
mExternalTextures.AppendElements(aExternalTextures);
|
|
|
|
ffi::wgpu_client_compute_pass_encoder_end(GetClient(), aComputePassEncoderId);
|
|
}
|
|
|
|
void CommandEncoder::EndRenderPass(
|
|
RawId aRenderPassEncoderId, CanvasContextArray& aCanvasContexts,
|
|
Span<RefPtr<ExternalTexture>> aExternalTextures) {
|
|
for (const auto& context : aCanvasContexts) {
|
|
TrackPresentationContext(context);
|
|
}
|
|
mExternalTextures.AppendElements(aExternalTextures);
|
|
|
|
ffi::wgpu_client_render_pass_encoder_end(GetClient(), aRenderPassEncoderId);
|
|
}
|
|
|
|
already_AddRefed<CommandBuffer> CommandEncoder::Finish(
|
|
const dom::GPUCommandBufferDescriptor& aDesc) {
|
|
ffi::WGPUFfiCommandBufferDescriptor desc = {};
|
|
|
|
webgpu::StringHelper label(aDesc.mLabel);
|
|
desc.label = label.Get();
|
|
|
|
RawId command_buffer_id =
|
|
ffi::wgpu_client_command_encoder_finish(GetClient(), GetId(), &desc);
|
|
|
|
RefPtr<CommandBuffer> comb = new CommandBuffer(
|
|
mParent, command_buffer_id, std::move(mPresentationContexts),
|
|
std::move(mExternalTextures));
|
|
comb->SetLabel(aDesc.mLabel);
|
|
return comb.forget();
|
|
}
|
|
|
|
} // namespace mozilla::webgpu
|