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
166 lines
6.6 KiB
C++
166 lines
6.6 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 "RenderBundleEncoder.h"
|
|
|
|
#include "BindGroup.h"
|
|
#include "Buffer.h"
|
|
#include "RenderBundle.h"
|
|
#include "RenderPipeline.h"
|
|
#include "Utility.h"
|
|
#include "mozilla/dom/WebGPUBinding.h"
|
|
#include "mozilla/webgpu/ffi/wgpu.h"
|
|
|
|
namespace mozilla::webgpu {
|
|
|
|
GPU_IMPL_CYCLE_COLLECTION(RenderBundleEncoder, mParent)
|
|
GPU_IMPL_JS_WRAP(RenderBundleEncoder)
|
|
|
|
RenderBundleEncoder::RenderBundleEncoder(Device* const aParent, RawId aId)
|
|
: ObjectBase(aParent->GetChild(), aId,
|
|
ffi::wgpu_client_drop_render_bundle_encoder),
|
|
ChildOf(aParent) {}
|
|
|
|
RenderBundleEncoder::~RenderBundleEncoder() = default;
|
|
|
|
void RenderBundleEncoder::SetBindGroup(uint32_t aSlot,
|
|
BindGroup* const aBindGroup,
|
|
const uint32_t* aDynamicOffsets,
|
|
size_t aDynamicOffsetsLength) {
|
|
RawId bindGroup = 0;
|
|
if (aBindGroup) {
|
|
mUsedCanvasContexts.AppendElements(aBindGroup->GetCanvasContexts());
|
|
mExternalTextures.AppendElements(aBindGroup->GetExternalTextures());
|
|
bindGroup = aBindGroup->GetId();
|
|
}
|
|
const ffi::WGPUFfiSlice_DynamicOffset dynamicOffsets{
|
|
.data = aDynamicOffsets,
|
|
.length = aDynamicOffsetsLength,
|
|
};
|
|
ffi::wgpu_client_render_bundle_encoder_set_bind_group(
|
|
GetClient(), GetId(), aSlot, bindGroup, dynamicOffsets);
|
|
}
|
|
|
|
void RenderBundleEncoder::SetBindGroup(
|
|
uint32_t aSlot, BindGroup* const aBindGroup,
|
|
const dom::Sequence<uint32_t>& aDynamicOffsets, ErrorResult& aRv) {
|
|
this->SetBindGroup(aSlot, aBindGroup, aDynamicOffsets.Elements(),
|
|
aDynamicOffsets.Length());
|
|
}
|
|
|
|
void RenderBundleEncoder::SetBindGroup(
|
|
uint32_t aSlot, BindGroup* const aBindGroup,
|
|
const dom::Uint32Array& aDynamicOffsetsData,
|
|
uint64_t aDynamicOffsetsDataStart, uint64_t aDynamicOffsetsDataLength,
|
|
ErrorResult& aRv) {
|
|
auto dynamicOffsets =
|
|
GetDynamicOffsetsFromArray(aDynamicOffsetsData, aDynamicOffsetsDataStart,
|
|
aDynamicOffsetsDataLength, aRv);
|
|
|
|
if (dynamicOffsets.isSome()) {
|
|
this->SetBindGroup(aSlot, aBindGroup, dynamicOffsets->Elements(),
|
|
dynamicOffsets->Length());
|
|
}
|
|
}
|
|
|
|
void RenderBundleEncoder::SetPipeline(const RenderPipeline& aPipeline) {
|
|
ffi::wgpu_client_render_bundle_encoder_set_pipeline(GetClient(), GetId(),
|
|
aPipeline.GetId());
|
|
}
|
|
|
|
void RenderBundleEncoder::SetIndexBuffer(
|
|
const Buffer& aBuffer, const dom::GPUIndexFormat& aIndexFormat,
|
|
uint64_t aOffset, const dom::Optional<uint64_t>& aSize) {
|
|
const auto iformat = aIndexFormat == dom::GPUIndexFormat::Uint32
|
|
? ffi::WGPUIndexFormat_Uint32
|
|
: ffi::WGPUIndexFormat_Uint16;
|
|
ffi::WGPUFfiOption_BufferAddress bufferSize = {};
|
|
if (aSize.WasPassed()) {
|
|
bufferSize.tag = ffi::WGPUFfiOption_BufferAddress_Some_BufferAddress;
|
|
bufferSize.some = aSize.Value();
|
|
} else {
|
|
bufferSize.tag = ffi::WGPUFfiOption_BufferAddress_None_BufferAddress;
|
|
}
|
|
ffi::wgpu_client_render_bundle_encoder_set_index_buffer(
|
|
GetClient(), GetId(), aBuffer.GetId(), iformat, aOffset, bufferSize);
|
|
}
|
|
|
|
void RenderBundleEncoder::SetVertexBuffer(
|
|
uint32_t aSlot, const Buffer* const aBuffer, uint64_t aOffset,
|
|
const dom::Optional<uint64_t>& aSize) {
|
|
RawId bufferId = 0;
|
|
if (aBuffer) {
|
|
bufferId = aBuffer->GetId();
|
|
}
|
|
ffi::WGPUFfiOption_BufferAddress bufferSize = {};
|
|
if (aSize.WasPassed()) {
|
|
bufferSize.tag = ffi::WGPUFfiOption_BufferAddress_Some_BufferAddress;
|
|
bufferSize.some = aSize.Value();
|
|
} else {
|
|
bufferSize.tag = ffi::WGPUFfiOption_BufferAddress_None_BufferAddress;
|
|
}
|
|
ffi::wgpu_client_render_bundle_encoder_set_vertex_buffer(
|
|
GetClient(), GetId(), aSlot, bufferId, aOffset, bufferSize);
|
|
}
|
|
|
|
void RenderBundleEncoder::Draw(uint32_t aVertexCount, uint32_t aInstanceCount,
|
|
uint32_t aFirstVertex, uint32_t aFirstInstance) {
|
|
ffi::wgpu_client_render_bundle_encoder_draw(GetClient(), GetId(),
|
|
aVertexCount, aInstanceCount,
|
|
aFirstVertex, aFirstInstance);
|
|
}
|
|
|
|
void RenderBundleEncoder::DrawIndexed(uint32_t aIndexCount,
|
|
uint32_t aInstanceCount,
|
|
uint32_t aFirstIndex, int32_t aBaseVertex,
|
|
uint32_t aFirstInstance) {
|
|
ffi::wgpu_client_render_bundle_encoder_draw_indexed(
|
|
GetClient(), GetId(), aIndexCount, aInstanceCount, aFirstIndex,
|
|
aBaseVertex, aFirstInstance);
|
|
}
|
|
|
|
void RenderBundleEncoder::DrawIndirect(const Buffer& aIndirectBuffer,
|
|
uint64_t aIndirectOffset) {
|
|
ffi::wgpu_client_render_bundle_encoder_draw_indirect(
|
|
GetClient(), GetId(), aIndirectBuffer.GetId(), aIndirectOffset);
|
|
}
|
|
|
|
void RenderBundleEncoder::DrawIndexedIndirect(const Buffer& aIndirectBuffer,
|
|
uint64_t aIndirectOffset) {
|
|
ffi::wgpu_client_render_bundle_encoder_draw_indexed_indirect(
|
|
GetClient(), GetId(), aIndirectBuffer.GetId(), aIndirectOffset);
|
|
}
|
|
|
|
void RenderBundleEncoder::PushDebugGroup(const nsAString& aString) {
|
|
const NS_ConvertUTF16toUTF8 utf8(aString);
|
|
ffi::wgpu_client_render_bundle_encoder_push_debug_group(GetClient(), GetId(),
|
|
utf8.get());
|
|
}
|
|
void RenderBundleEncoder::PopDebugGroup() {
|
|
ffi::wgpu_client_render_bundle_encoder_pop_debug_group(GetClient(), GetId());
|
|
}
|
|
void RenderBundleEncoder::InsertDebugMarker(const nsAString& aString) {
|
|
const NS_ConvertUTF16toUTF8 utf8(aString);
|
|
ffi::wgpu_client_render_bundle_encoder_insert_debug_marker(
|
|
GetClient(), GetId(), utf8.get());
|
|
}
|
|
|
|
already_AddRefed<RenderBundle> RenderBundleEncoder::Finish(
|
|
const dom::GPURenderBundleDescriptor& aDesc) {
|
|
ffi::WGPUFfiRenderBundleDescriptor desc = {};
|
|
webgpu::StringHelper label(aDesc.mLabel);
|
|
desc.label = label.Get();
|
|
|
|
RawId id = ffi::wgpu_client_render_bundle_encoder_finish(GetClient(), GetId(),
|
|
&desc);
|
|
|
|
auto canvasContexts = mUsedCanvasContexts.Clone();
|
|
auto externalTextures = mExternalTextures.Clone();
|
|
RefPtr<RenderBundle> bundle = new RenderBundle(
|
|
mParent, id, std::move(canvasContexts), std::move(externalTextures));
|
|
return bundle.forget();
|
|
}
|
|
|
|
} // namespace mozilla::webgpu
|