webgpu: Update to wgpu 0.20 (#32173)

* Update wgpu to 0.20

* good expectations

* Throw TypeError in configure on unsupported format instead of panic

* Expect

* `into_command_buffer_id`,`into_command_encoder_id`
This commit is contained in:
Samson 2024-05-08 07:38:11 +02:00 committed by GitHub
parent 5298ccb0eb
commit c4f8599404
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
21 changed files with 1552 additions and 852 deletions

View file

@ -268,7 +268,8 @@ impl GPUBufferMethods for GPUBuffer {
buffer_id: self.buffer.0,
device_id: self.device.id().0,
host_map,
map_range: map_range.clone(),
offset,
size: Some(range_size),
},
)) {
warn!(

View file

@ -208,14 +208,19 @@ impl GPUCanvasContextMethods for GPUCanvasContext {
}
/// <https://gpuweb.github.io/gpuweb/#dom-gpucanvascontext-configure>
fn Configure(&self, descriptor: &GPUCanvasConfiguration) {
fn Configure(&self, descriptor: &GPUCanvasConfiguration) -> Fallible<()> {
// Step 1 is let
// Step 2
// TODO: device features
let format = match descriptor.format {
GPUTextureFormat::Rgba8unorm | GPUTextureFormat::Rgba8unorm_srgb => ImageFormat::RGBA8,
GPUTextureFormat::Bgra8unorm | GPUTextureFormat::Bgra8unorm_srgb => ImageFormat::BGRA8,
_ => panic!("SwapChain format({:?}) not supported", descriptor.format), // TODO: Better handling
_ => {
return Err(Error::Type(format!(
"SwapChain format({:?}) not supported",
descriptor.format
)))
},
};
// Step 3
@ -287,6 +292,7 @@ impl GPUCanvasContextMethods for GPUCanvasContext {
.set(Some(&descriptor.device.CreateTexture(&text_desc).unwrap()));
self.webrender_image.set(Some(receiver.recv().unwrap()));
Ok(())
}
/// <https://gpuweb.github.io/gpuweb/#dom-gpucanvascontext-unconfigure>

View file

@ -393,7 +393,7 @@ impl GPUCommandEncoderMethods for GPUCommandEncoder {
.expect("Failed to send Finish");
*self.state.borrow_mut() = GPUCommandEncoderState::Closed;
let buffer = webgpu::WebGPUCommandBuffer(self.encoder.0.transmute());
let buffer = webgpu::WebGPUCommandBuffer(self.encoder.0.into_command_buffer_id());
GPUCommandBuffer::new(
&self.global(),
self.channel.clone(),

View file

@ -3,7 +3,7 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use dom_struct::dom_struct;
use webgpu::wgpu::command::{compute_ffi as wgpu_comp, ComputePass};
use webgpu::wgpu::command::{compute_commands as wgpu_comp, ComputePass};
use webgpu::{WebGPU, WebGPURequest};
use super::bindings::error::Fallible;
@ -120,15 +120,12 @@ impl GPUComputePassEncoderMethods for GPUComputePassEncoder {
#[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() {
unsafe {
wgpu_comp::wgpu_compute_pass_set_bind_group(
compute_pass,
index,
bind_group.id().0,
dynamic_offsets.as_ptr(),
dynamic_offsets.len(),
)
};
wgpu_comp::wgpu_compute_pass_set_bind_group(
compute_pass,
index,
bind_group.id().0,
&dynamic_offsets,
)
}
}

View file

@ -718,7 +718,9 @@ impl GPUDeviceMethods for GPUDevice {
layout,
stage: wgpu_pipe::ProgrammableStageDescriptor {
module: descriptor.compute.module.id().0,
entry_point: Cow::Owned(descriptor.compute.entryPoint.to_string()),
entry_point: Some(Cow::Owned(descriptor.compute.entryPoint.to_string())),
constants: Cow::Owned(HashMap::new()),
zero_initialize_workgroup_memory: true,
},
};
@ -921,7 +923,11 @@ impl GPUDeviceMethods for GPUDevice {
vertex: wgpu_pipe::VertexState {
stage: wgpu_pipe::ProgrammableStageDescriptor {
module: descriptor.vertex.parent.module.id().0,
entry_point: Cow::Owned(descriptor.vertex.parent.entryPoint.to_string()),
entry_point: Some(Cow::Owned(
descriptor.vertex.parent.entryPoint.to_string(),
)),
constants: Cow::Owned(HashMap::new()),
zero_initialize_workgroup_memory: true,
},
buffers: Cow::Owned(
descriptor
@ -955,7 +961,9 @@ impl GPUDeviceMethods for GPUDevice {
.map(|stage| wgpu_pipe::FragmentState {
stage: wgpu_pipe::ProgrammableStageDescriptor {
module: stage.parent.module.id().0,
entry_point: Cow::Owned(stage.parent.entryPoint.to_string()),
entry_point: Some(Cow::Owned(stage.parent.entryPoint.to_string())),
constants: Cow::Owned(HashMap::new()),
zero_initialize_workgroup_memory: true,
},
targets: Cow::Owned(
stage

View file

@ -3,7 +3,7 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use dom_struct::dom_struct;
use webgpu::wgpu::command::{render_ffi as wgpu_render, RenderPass};
use webgpu::wgpu::command::{render_commands as wgpu_render, RenderPass};
use webgpu::{wgt, WebGPU, WebGPURequest};
use super::bindings::codegen::Bindings::WebGPUBinding::GPUIndexFormat;
@ -86,15 +86,12 @@ impl GPURenderPassEncoderMethods for GPURenderPassEncoder {
#[allow(unsafe_code)]
fn SetBindGroup(&self, index: u32, bind_group: &GPUBindGroup, dynamic_offsets: Vec<u32>) {
if let Some(render_pass) = self.render_pass.borrow_mut().as_mut() {
unsafe {
wgpu_render::wgpu_render_pass_set_bind_group(
render_pass,
index,
bind_group.id().0,
dynamic_offsets.as_ptr(),
dynamic_offsets.len(),
)
};
wgpu_render::wgpu_render_pass_set_bind_group(
render_pass,
index,
bind_group.id().0,
&dynamic_offsets,
)
}
}
@ -286,13 +283,7 @@ impl GPURenderPassEncoderMethods for GPURenderPassEncoder {
fn ExecuteBundles(&self, bundles: Vec<DomRoot<GPURenderBundle>>) {
let bundle_ids = bundles.iter().map(|b| b.id().0).collect::<Vec<_>>();
if let Some(render_pass) = self.render_pass.borrow_mut().as_mut() {
unsafe {
wgpu_render::wgpu_render_pass_execute_bundles(
render_pass,
bundle_ids.as_ptr(),
bundle_ids.len(),
)
};
wgpu_render::wgpu_render_pass_execute_bundles(render_pass, &bundle_ids)
}
}
}

View file

@ -1048,6 +1048,7 @@ interface GPUCanvasContext {
// Calling configure() a second time invalidates the previous one,
// and all of the textures it's produced.
[Throws]
undefined configure(GPUCanvasConfiguration descriptor);
undefined unconfigure();

View file

@ -2125,7 +2125,7 @@ impl ScriptThread {
WebGPUMsg::FreeCommandBuffer(id) => self
.gpu_id_hub
.lock()
.kill_command_buffer_id(id.transmute()),
.kill_command_buffer_id(id.into_command_encoder_id()),
WebGPUMsg::FreeSampler(id) => self.gpu_id_hub.lock().kill_sampler_id(id),
WebGPUMsg::FreeShaderModule(id) => self.gpu_id_hub.lock().kill_shader_module_id(id),
WebGPUMsg::FreeRenderBundle(id) => self.gpu_id_hub.lock().kill_render_bundle_id(id),

View file

@ -81,7 +81,8 @@ pub enum WebGPURequest {
buffer_id: id::BufferId,
device_id: id::DeviceId,
host_map: HostMap,
map_range: std::ops::Range<u64>,
offset: u64,
size: Option<u64>,
},
CommandEncoderFinish {
command_encoder_id: id::CommandEncoderId,
@ -407,7 +408,8 @@ impl WGPU {
buffer_id,
device_id,
host_map,
map_range,
offset,
size,
} => {
let glob = Arc::clone(&self.global);
let resp_sender = sender.clone();
@ -447,7 +449,12 @@ impl WGPU {
callback: Some(callback),
};
let global = &self.global;
let result = gfx_select!(buffer_id => global.buffer_map_async(buffer_id, map_range, operation));
let result = gfx_select!(buffer_id => global.buffer_map_async(
buffer_id,
offset,
size,
operation
));
if let Err(ref e) = result {
if let Err(w) = sender.send(Some(Err(format!("{:?}", e)))) {
warn!("Failed to send BufferMapAsync Response ({:?})", w);
@ -839,7 +846,7 @@ impl WGPU {
WebGPURequest::DropCommandBuffer(id) => {
self.error_command_encoders
.borrow_mut()
.remove(&id.transmute());
.remove(&id.into_command_encoder_id());
let global = &self.global;
gfx_select!(id => global.command_buffer_drop(id));
if let Err(e) = self.script_sender.send(WebGPUMsg::FreeCommandBuffer(id)) {
@ -1004,7 +1011,7 @@ impl WGPU {
let cmd_id = command_buffers.iter().find(|id| {
self.error_command_encoders
.borrow()
.contains_key(&id.transmute())
.contains_key(&id.into_command_encoder_id())
});
let result = if cmd_id.is_some() {
Err(String::from("Invalid command buffer submitted"))
@ -1108,7 +1115,7 @@ impl WGPU {
));
let _ = gfx_select!(queue_id => global.queue_submit(
queue_id,
&[encoder_id.transmute()]
&[encoder_id.into_command_buffer_id()]
));
let glob = Arc::clone(&self.global);
@ -1161,7 +1168,8 @@ impl WGPU {
host: HostMap::Read,
callback: Some(callback),
};
let _ = gfx_select!(buffer_id => global.buffer_map_async(buffer_id, 0..buffer_size, map_op));
let _ = gfx_select!(buffer_id
=> global.buffer_map_async(buffer_id, 0, Some(buffer_size), map_op));
},
WebGPURequest::UnmapBuffer {
buffer_id,
@ -1458,3 +1466,21 @@ pub struct PresentationData {
image_desc: ImageDescriptor,
image_data: ImageData,
}
pub trait Transmute<U: id::Marker> {
fn transmute(self) -> id::Id<U>;
}
impl Transmute<id::markers::Queue> for id::Id<id::markers::Device> {
fn transmute(self) -> id::Id<id::markers::Queue> {
// if this is removed next one should be removed too.
self.into_queue_id()
}
}
impl Transmute<id::markers::Device> for id::Id<id::markers::Queue> {
fn transmute(self) -> id::Id<id::markers::Device> {
// SAFETY: This is safe because queue_id = device_id in wgpu
unsafe { id::Id::from_raw(self.into_raw()) }
}
}