webgpu: Update wgpu and revamp computepass (#32575)

* Do not wait on drop, but rather wake poller thread

* Update wgpu and render stuff

* Set some good expectations

* Update wgpu again

* handle IPC error as warning

* More good expectations

* Some more expectations

CTS does not match the spec: https://github.com/gpuweb/cts/issues/3806

* This expectations are due to other changes in servo

also happening on main

* Explain error_command_encoders and remove RefCell around it

* fixup

* store validness of passes

* More good expectations

* More docs

* this assert is wrong

* This is even more right per CTS/spec

Only Command encoder state errors are allowed here, but wgpu does not exposes them.

* More good expectations

* One bad expectation

* Fix my english
This commit is contained in:
Samson 2024-06-28 06:49:35 +02:00 committed by GitHub
parent fced0b4940
commit e9cf4d4971
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
16 changed files with 372 additions and 1870 deletions

View file

@ -64,7 +64,6 @@ use crate::dom::bindings::str::{DOMString, USVString};
use crate::dom::bindings::utils::WindowProxyHandler;
use crate::dom::gpubuffer::GPUBufferState;
use crate::dom::gpucanvascontext::WebGPUContextId;
use crate::dom::gpucommandencoder::GPUCommandEncoderState;
use crate::dom::htmlimageelement::SourceSet;
use crate::dom::htmlmediaelement::HTMLMediaElementFetchContext;
use crate::script_runtime::{ContextForRequestInterrupt, StreamConsumer};
@ -371,7 +370,6 @@ unsafe_no_jsmanaged_fields!(DOMString);
unsafe_no_jsmanaged_fields!(USVString);
unsafe_no_jsmanaged_fields!(WebGPUContextId);
unsafe_no_jsmanaged_fields!(GPUBufferState);
unsafe_no_jsmanaged_fields!(GPUCommandEncoderState);
unsafe_no_jsmanaged_fields!(SourceSet);
unsafe_no_jsmanaged_fields!(HTMLMediaElementFetchContext);
unsafe_no_jsmanaged_fields!(StreamConsumer);

View file

@ -8,8 +8,9 @@ use std::collections::HashSet;
use dom_struct::dom_struct;
use webgpu::wgc::command as wgpu_com;
use webgpu::{self, wgt, WebGPU, WebGPURequest};
use webgpu::{self, wgt, WebGPU, WebGPUComputePass, WebGPURequest};
use super::gpuconvert::convert_label;
use crate::dom::bindings::cell::DomRefCell;
use crate::dom::bindings::codegen::Bindings::WebGPUBinding::{
GPUCommandBufferDescriptor, GPUCommandEncoderMethods, GPUComputePassDescriptor, GPUExtent3D,
@ -31,16 +32,6 @@ use crate::dom::gpuconvert::{
use crate::dom::gpudevice::GPUDevice;
use crate::dom::gpurenderpassencoder::GPURenderPassEncoder;
// TODO(sagudev): this is different now
// https://gpuweb.github.io/gpuweb/#enumdef-encoder-state
#[derive(MallocSizeOf, PartialEq)]
pub enum GPUCommandEncoderState {
Open,
EncodingRenderPass,
EncodingComputePass,
Closed,
}
#[dom_struct]
pub struct GPUCommandEncoder {
reflector_: Reflector,
@ -51,7 +42,6 @@ pub struct GPUCommandEncoder {
#[no_trace]
encoder: webgpu::WebGPUCommandEncoder,
buffers: DomRefCell<HashSet<DomRoot<GPUBuffer>>>,
state: DomRefCell<GPUCommandEncoderState>,
device: Dom<GPUDevice>,
valid: Cell<bool>,
}
@ -70,7 +60,6 @@ impl GPUCommandEncoder {
device: Dom::from_ref(device),
encoder,
buffers: DomRefCell::new(HashSet::new()),
state: DomRefCell::new(GPUCommandEncoderState::Open),
valid: Cell::new(true),
}
}
@ -96,13 +85,8 @@ impl GPUCommandEncoder {
self.encoder
}
pub fn set_state(&self, set: GPUCommandEncoderState, expect: GPUCommandEncoderState) {
if *self.state.borrow() == expect {
*self.state.borrow_mut() = set;
} else {
self.valid.set(false);
*self.state.borrow_mut() = GPUCommandEncoderState::Closed;
}
pub fn device_id(&self) -> webgpu::WebGPUDevice {
self.device.id()
}
}
@ -122,32 +106,26 @@ impl GPUCommandEncoderMethods for GPUCommandEncoder {
&self,
descriptor: &GPUComputePassDescriptor,
) -> DomRoot<GPUComputePassEncoder> {
self.set_state(
GPUCommandEncoderState::EncodingComputePass,
GPUCommandEncoderState::Open,
);
let compute_pass_id = self
.global()
.wgpu_id_hub()
.lock()
.create_compute_pass_id(self.device.id().0.backend());
let compute_pass = if !self.valid.get() {
None
} else {
Some(wgpu_com::ComputePass::new(
self.encoder.0,
&wgpu_com::ComputePassDescriptor {
label: descriptor
.parent
.label
.as_ref()
.map(|l| Cow::Borrowed(&**l)),
timestamp_writes: None,
},
))
};
if let Err(e) = self.channel.0.send(WebGPURequest::BeginComputePass {
command_encoder_id: self.id().0,
compute_pass_id,
label: convert_label(&descriptor.parent),
device_id: self.device.id().0,
}) {
warn!("Failed to send WebGPURequest::BeginComputePass {e:?}");
}
GPUComputePassEncoder::new(
&self.global(),
self.channel.clone(),
self,
compute_pass,
WebGPUComputePass(compute_pass_id),
descriptor.parent.label.clone().unwrap_or_default(),
)
}
@ -157,11 +135,6 @@ impl GPUCommandEncoderMethods for GPUCommandEncoder {
&self,
descriptor: &GPURenderPassDescriptor,
) -> DomRoot<GPURenderPassEncoder> {
self.set_state(
GPUCommandEncoderState::EncodingRenderPass,
GPUCommandEncoderState::Open,
);
let render_pass = if !self.valid.get() {
None
} else {
@ -259,11 +232,6 @@ impl GPUCommandEncoderMethods for GPUCommandEncoder {
destination_offset: GPUSize64,
size: GPUSize64,
) {
if !(*self.state.borrow() == GPUCommandEncoderState::Open) {
self.valid.set(false);
return;
}
self.buffers.borrow_mut().insert(DomRoot::from_ref(source));
self.buffers
.borrow_mut()
@ -288,11 +256,6 @@ impl GPUCommandEncoderMethods for GPUCommandEncoder {
destination: &GPUImageCopyTexture,
copy_size: GPUExtent3D,
) {
if !(*self.state.borrow() == GPUCommandEncoderState::Open) {
self.valid.set(false);
return;
}
self.buffers
.borrow_mut()
.insert(DomRoot::from_ref(&*source.buffer));
@ -315,11 +278,6 @@ impl GPUCommandEncoderMethods for GPUCommandEncoder {
destination: &GPUImageCopyBuffer,
copy_size: GPUExtent3D,
) {
if !(*self.state.borrow() == GPUCommandEncoderState::Open) {
self.valid.set(false);
return;
}
self.buffers
.borrow_mut()
.insert(DomRoot::from_ref(&*destination.buffer));
@ -342,11 +300,6 @@ impl GPUCommandEncoderMethods for GPUCommandEncoder {
destination: &GPUImageCopyTexture,
copy_size: GPUExtent3D,
) {
if !(*self.state.borrow() == GPUCommandEncoderState::Open) {
self.valid.set(false);
return;
}
self.channel
.0
.send(WebGPURequest::CopyTextureToTexture {
@ -371,7 +324,6 @@ impl GPUCommandEncoderMethods for GPUCommandEncoder {
})
.expect("Failed to send Finish");
*self.state.borrow_mut() = GPUCommandEncoderState::Closed;
let buffer = webgpu::WebGPUCommandBuffer(self.encoder.0.into_command_buffer_id());
GPUCommandBuffer::new(
&self.global(),

View file

@ -3,8 +3,7 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use dom_struct::dom_struct;
use webgpu::wgc::command::{compute_commands as wgpu_comp, ComputePass};
use webgpu::{WebGPU, WebGPURequest};
use webgpu::{WebGPU, WebGPUComputePass, WebGPURequest};
use super::bindings::error::Fallible;
use crate::dom::bindings::cell::DomRefCell;
@ -15,7 +14,7 @@ use crate::dom::bindings::str::USVString;
use crate::dom::globalscope::GlobalScope;
use crate::dom::gpubindgroup::GPUBindGroup;
use crate::dom::gpubuffer::GPUBuffer;
use crate::dom::gpucommandencoder::{GPUCommandEncoder, GPUCommandEncoderState};
use crate::dom::gpucommandencoder::GPUCommandEncoder;
use crate::dom::gpucomputepipeline::GPUComputePipeline;
#[dom_struct]
@ -25,9 +24,8 @@ pub struct GPUComputePassEncoder {
#[no_trace]
channel: WebGPU,
label: DomRefCell<USVString>,
#[ignore_malloc_size_of = "defined in wgpu-core"]
#[no_trace]
compute_pass: DomRefCell<Option<ComputePass>>,
compute_pass: WebGPUComputePass,
command_encoder: Dom<GPUCommandEncoder>,
}
@ -35,14 +33,14 @@ impl GPUComputePassEncoder {
fn new_inherited(
channel: WebGPU,
parent: &GPUCommandEncoder,
compute_pass: Option<ComputePass>,
compute_pass: WebGPUComputePass,
label: USVString,
) -> Self {
Self {
channel,
reflector_: Reflector::new(),
label: DomRefCell::new(label),
compute_pass: DomRefCell::new(compute_pass),
compute_pass,
command_encoder: Dom::from_ref(parent),
}
}
@ -51,7 +49,7 @@ impl GPUComputePassEncoder {
global: &GlobalScope,
channel: WebGPU,
parent: &GPUCommandEncoder,
compute_pass: Option<ComputePass>,
compute_pass: WebGPUComputePass,
label: USVString,
) -> DomRoot<Self> {
reflect_dom_object(
@ -79,57 +77,83 @@ impl GPUComputePassEncoderMethods for GPUComputePassEncoder {
/// <https://gpuweb.github.io/gpuweb/#dom-gpucomputepassencoder-dispatchworkgroups>
fn DispatchWorkgroups(&self, x: u32, y: u32, z: u32) {
if let Some(compute_pass) = self.compute_pass.borrow_mut().as_mut() {
wgpu_comp::wgpu_compute_pass_dispatch_workgroups(compute_pass, x, y, z);
if let Err(e) = self
.channel
.0
.send(WebGPURequest::ComputePassDispatchWorkgroups {
compute_pass_id: self.compute_pass.0,
x,
y,
z,
device_id: self.command_encoder.device_id().0,
})
{
warn!("Error sending WebGPURequest::ComputePassDispatchWorkgroups: {e:?}")
}
}
/// <https://gpuweb.github.io/gpuweb/#dom-gpucomputepassencoder-dispatchworkgroupsindirect>
fn DispatchWorkgroupsIndirect(&self, indirect_buffer: &GPUBuffer, indirect_offset: u64) {
if let Some(compute_pass) = self.compute_pass.borrow_mut().as_mut() {
wgpu_comp::wgpu_compute_pass_dispatch_workgroups_indirect(
compute_pass,
indirect_buffer.id().0,
indirect_offset,
);
fn DispatchWorkgroupsIndirect(&self, buffer: &GPUBuffer, offset: u64) {
if let Err(e) = self
.channel
.0
.send(WebGPURequest::ComputePassDispatchWorkgroupsIndirect {
compute_pass_id: self.compute_pass.0,
buffer_id: buffer.id().0,
offset,
device_id: self.command_encoder.device_id().0,
})
{
warn!("Error sending WebGPURequest::ComputePassDispatchWorkgroupsIndirect: {e:?}")
}
}
/// <https://gpuweb.github.io/gpuweb/#dom-gpurenderpassencoder-endpass>
fn End(&self) -> Fallible<()> {
let compute_pass = self.compute_pass.borrow_mut().take();
self.channel
.0
.send(WebGPURequest::RunComputePass {
command_encoder_id: self.command_encoder.id().0,
compute_pass,
})
.expect("Failed to send RunComputePass"); //TODO: handle error
self.command_encoder.set_state(
GPUCommandEncoderState::Open,
GPUCommandEncoderState::EncodingComputePass,
);
if let Err(e) = self.channel.0.send(WebGPURequest::EndComputePass {
compute_pass_id: self.compute_pass.0,
device_id: self.command_encoder.device_id().0,
command_encoder_id: self.command_encoder.id().0,
}) {
warn!("Failed to send WebGPURequest::EndComputePass: {e:?}");
}
Ok(())
}
/// <https://gpuweb.github.io/gpuweb/#dom-gpuprogrammablepassencoder-setbindgroup>
#[allow(unsafe_code)]
fn SetBindGroup(&self, index: u32, bind_group: &GPUBindGroup, dynamic_offsets: Vec<u32>) {
if let Some(compute_pass) = self.compute_pass.borrow_mut().as_mut() {
wgpu_comp::wgpu_compute_pass_set_bind_group(
compute_pass,
index,
bind_group.id().0,
&dynamic_offsets,
)
fn SetBindGroup(&self, index: u32, bind_group: &GPUBindGroup, offsets: Vec<u32>) {
if let Err(e) = self.channel.0.send(WebGPURequest::ComputePassSetBindGroup {
compute_pass_id: self.compute_pass.0,
index,
bind_group_id: bind_group.id().0,
offsets,
device_id: self.command_encoder.device_id().0,
}) {
warn!("Error sending WebGPURequest::ComputePassSetBindGroup: {e:?}")
}
}
/// <https://gpuweb.github.io/gpuweb/#dom-gpucomputepassencoder-setpipeline>
fn SetPipeline(&self, pipeline: &GPUComputePipeline) {
if let Some(compute_pass) = self.compute_pass.borrow_mut().as_mut() {
wgpu_comp::wgpu_compute_pass_set_pipeline(compute_pass, pipeline.id().0);
if let Err(e) = self.channel.0.send(WebGPURequest::ComputePassSetPipeline {
compute_pass_id: self.compute_pass.0,
pipeline_id: pipeline.id().0,
device_id: self.command_encoder.device_id().0,
}) {
warn!("Error sending WebGPURequest::ComputePassSetPipeline: {e:?}")
}
}
}
impl Drop for GPUComputePassEncoder {
fn drop(&mut self) {
if let Err(e) = self
.channel
.0
.send(WebGPURequest::DropComputePass(self.compute_pass.0))
{
warn!("Failed to send WebGPURequest::DropComputePass with {e:?}");
}
}
}

View file

@ -586,7 +586,9 @@ impl GPUDeviceMethods for GPUDevice {
entry_point: Some(Cow::Owned(descriptor.compute.entryPoint.to_string())),
constants: Cow::Owned(HashMap::new()),
zero_initialize_workgroup_memory: true,
vertex_pulling_transform: false,
},
cache: None,
};
self.channel
@ -769,6 +771,7 @@ impl GPUDeviceMethods for GPUDevice {
Some(wgpu_pipe::RenderPipelineDescriptor {
label: convert_label(&descriptor.parent.parent),
layout,
cache: None,
vertex: wgpu_pipe::VertexState {
stage: wgpu_pipe::ProgrammableStageDescriptor {
module: descriptor.vertex.parent.module.id().0,
@ -777,6 +780,7 @@ impl GPUDeviceMethods for GPUDevice {
)),
constants: Cow::Owned(HashMap::new()),
zero_initialize_workgroup_memory: true,
vertex_pulling_transform: false,
},
buffers: Cow::Owned(
descriptor
@ -813,6 +817,7 @@ impl GPUDeviceMethods for GPUDevice {
entry_point: Some(Cow::Owned(stage.parent.entryPoint.to_string())),
constants: Cow::Owned(HashMap::new()),
zero_initialize_workgroup_memory: true,
vertex_pulling_transform: false,
},
targets: Cow::Owned(
stage

View file

@ -19,7 +19,7 @@ use crate::dom::bindings::str::USVString;
use crate::dom::globalscope::GlobalScope;
use crate::dom::gpubindgroup::GPUBindGroup;
use crate::dom::gpubuffer::GPUBuffer;
use crate::dom::gpucommandencoder::{GPUCommandEncoder, GPUCommandEncoderState};
use crate::dom::gpucommandencoder::GPUCommandEncoder;
use crate::dom::gpurenderbundle::GPURenderBundle;
use crate::dom::gpurenderpipeline::GPURenderPipeline;
@ -164,16 +164,12 @@ impl GPURenderPassEncoderMethods for GPURenderPassEncoder {
let render_pass = self.render_pass.borrow_mut().take();
self.channel
.0
.send(WebGPURequest::RunRenderPass {
command_encoder_id: self.command_encoder.id().0,
.send(WebGPURequest::EndRenderPass {
render_pass,
device_id: self.command_encoder.device_id().0,
})
.expect("Failed to send RunRenderPass");
self.command_encoder.set_state(
GPUCommandEncoderState::Open,
GPUCommandEncoderState::EncodingRenderPass,
);
Ok(())
}

View file

@ -3,6 +3,7 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use smallvec::SmallVec;
use webgpu::identity::{ComputePass, ComputePassId};
use webgpu::wgc::id::markers::{
Adapter, BindGroup, BindGroupLayout, Buffer, CommandEncoder, ComputePipeline, Device,
PipelineLayout, RenderBundle, RenderPipeline, Sampler, ShaderModule, Texture, TextureView,
@ -31,6 +32,7 @@ pub struct IdentityHub {
samplers: IdentityManager<Sampler>,
render_pipelines: IdentityManager<RenderPipeline>,
render_bundles: IdentityManager<RenderBundle>,
compute_passes: IdentityManager<ComputePass>,
}
impl IdentityHub {
@ -50,6 +52,7 @@ impl IdentityHub {
samplers: IdentityManager::new(),
render_pipelines: IdentityManager::new(),
render_bundles: IdentityManager::new(),
compute_passes: IdentityManager::new(),
}
}
}
@ -225,6 +228,14 @@ impl Identities {
pub fn kill_render_bundle_id(&mut self, id: RenderBundleId) {
self.select(id.backend()).render_bundles.free(id);
}
pub fn create_compute_pass_id(&mut self, backend: Backend) -> ComputePassId {
self.select(backend).compute_passes.process(backend)
}
pub fn kill_compute_pass_id(&mut self, id: ComputePassId) {
self.select(id.backend()).compute_passes.free(id);
}
}
impl Default for Identities {