mirror of
https://github.com/servo/servo.git
synced 2025-08-06 22:15:33 +01:00
Upgrade wgpu-core version to 0.5.0 and implement server-side logic for wgpu id recycling
Remove current implementation of MapReadAsync
This commit is contained in:
parent
1a74382603
commit
a4f911699a
15 changed files with 504 additions and 339 deletions
|
@ -15,7 +15,8 @@ embedder_traits = {path = "../embedder_traits"}
|
|||
ipc-channel = "0.14"
|
||||
log = "0.4"
|
||||
malloc_size_of = { path = "../malloc_size_of" }
|
||||
serde = "1.0"
|
||||
serde = { version = "1.0", features = ["serde_derive"] }
|
||||
servo_config = {path = "../config"}
|
||||
smallvec = { version = "0.6", features = ["serde"] }
|
||||
wgpu-core = { version = "0.1.0", git = "https://github.com/gfx-rs/wgpu", features = ["serde"] }
|
||||
wgpu-core = { version = "0.5.0", git = "https://github.com/gfx-rs/wgpu", features = ["trace", "replay"] }
|
||||
wgpu-types = { version = "0.5.0", git = "https://github.com/gfx-rs/wgpu", features = ["trace", "replay"] }
|
||||
|
|
114
components/webgpu/identity.rs
Normal file
114
components/webgpu/identity.rs
Normal file
|
@ -0,0 +1,114 @@
|
|||
/* 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 https://mozilla.org/MPL/2.0/. */
|
||||
|
||||
use ipc_channel::ipc::IpcSender;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use wgpu::{
|
||||
hub::{GlobalIdentityHandlerFactory, IdentityHandler, IdentityHandlerFactory},
|
||||
id::{
|
||||
AdapterId, BindGroupId, BindGroupLayoutId, BufferId, CommandBufferId, ComputePipelineId,
|
||||
DeviceId, PipelineLayoutId, RenderPipelineId, SamplerId, ShaderModuleId, SurfaceId,
|
||||
SwapChainId, TextureId, TextureViewId, TypedId,
|
||||
},
|
||||
};
|
||||
use wgt::Backend;
|
||||
|
||||
#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
|
||||
pub enum WebGPUMsg {
|
||||
FreeAdapter(AdapterId),
|
||||
FreeDevice(DeviceId),
|
||||
FreeBuffer(BufferId),
|
||||
FreeSwapChain(SwapChainId),
|
||||
FreePipelineLayout(PipelineLayoutId),
|
||||
FreeComputePipeline(ComputePipelineId),
|
||||
FreeRenderPipeline(RenderPipelineId),
|
||||
FreeBindGroup(BindGroupId),
|
||||
FreeBindGroupLayout(BindGroupLayoutId),
|
||||
FreeCommandBuffer(CommandBufferId),
|
||||
FreeTexture(TextureId),
|
||||
FreeTextureView(TextureViewId),
|
||||
FreeSampler(SamplerId),
|
||||
FreeSurface(SurfaceId),
|
||||
FreeShaderModule(ShaderModuleId),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct IdentityRecycler {
|
||||
sender: IpcSender<WebGPUMsg>,
|
||||
}
|
||||
|
||||
pub struct IdentityRecyclerFactory {
|
||||
pub sender: IpcSender<WebGPUMsg>,
|
||||
}
|
||||
|
||||
macro_rules! impl_identity_handler {
|
||||
($id:ty, $st:tt, $($var:tt)*) => {
|
||||
impl IdentityHandler<$id> for IdentityRecycler {
|
||||
type Input = $id;
|
||||
fn process(&self, id: $id, _backend: Backend) -> $id {
|
||||
log::debug!("process {} {:?}", $st, id);
|
||||
//debug_assert_eq!(id.unzip().2, backend);
|
||||
id
|
||||
}
|
||||
fn free(&self, id: $id) {
|
||||
log::debug!("free {} {:?}", $st, id);
|
||||
let msg = $($var)*(id);
|
||||
if self.sender.send(msg).is_err() {
|
||||
log::error!("Failed to send {:?}", msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
impl_identity_handler!(AdapterId, "adapter", WebGPUMsg::FreeAdapter);
|
||||
impl_identity_handler!(DeviceId, "device", WebGPUMsg::FreeDevice);
|
||||
impl_identity_handler!(SurfaceId, "surface", WebGPUMsg::FreeSurface);
|
||||
impl_identity_handler!(SamplerId, "sampler", WebGPUMsg::FreeSampler);
|
||||
impl_identity_handler!(TextureId, "texture", WebGPUMsg::FreeTexture);
|
||||
impl_identity_handler!(TextureViewId, "texture_view", WebGPUMsg::FreeTextureView);
|
||||
impl_identity_handler!(BufferId, "buffer", WebGPUMsg::FreeBuffer);
|
||||
impl_identity_handler!(BindGroupId, "bind_group", WebGPUMsg::FreeBindGroup);
|
||||
impl_identity_handler!(SwapChainId, "swap_chain", WebGPUMsg::FreeSwapChain);
|
||||
impl_identity_handler!(ShaderModuleId, "shader_module", WebGPUMsg::FreeShaderModule);
|
||||
impl_identity_handler!(
|
||||
RenderPipelineId,
|
||||
"render_pipeline",
|
||||
WebGPUMsg::FreeRenderPipeline
|
||||
);
|
||||
impl_identity_handler!(
|
||||
ComputePipelineId,
|
||||
"compute_pipeline",
|
||||
WebGPUMsg::FreeComputePipeline
|
||||
);
|
||||
impl_identity_handler!(
|
||||
CommandBufferId,
|
||||
"command_buffer",
|
||||
WebGPUMsg::FreeCommandBuffer
|
||||
);
|
||||
impl_identity_handler!(
|
||||
BindGroupLayoutId,
|
||||
"bind_group_layout",
|
||||
WebGPUMsg::FreeBindGroupLayout
|
||||
);
|
||||
impl_identity_handler!(
|
||||
PipelineLayoutId,
|
||||
"pipeline_layout",
|
||||
WebGPUMsg::FreePipelineLayout
|
||||
);
|
||||
|
||||
impl<I: TypedId + Clone + std::fmt::Debug> IdentityHandlerFactory<I> for IdentityRecyclerFactory
|
||||
where
|
||||
I: TypedId + Clone + std::fmt::Debug,
|
||||
IdentityRecycler: IdentityHandler<I>,
|
||||
{
|
||||
type Filter = IdentityRecycler;
|
||||
fn spawn(&self, _min_index: u32) -> Self::Filter {
|
||||
IdentityRecycler {
|
||||
sender: self.sender.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl GlobalIdentityHandlerFactory for IdentityRecyclerFactory {}
|
|
@ -6,21 +6,28 @@
|
|||
extern crate log;
|
||||
#[macro_use]
|
||||
pub extern crate wgpu_core as wgpu;
|
||||
pub extern crate wgpu_types as wgt;
|
||||
|
||||
use ipc_channel::ipc::{self, IpcReceiver, IpcSender, IpcSharedMemory};
|
||||
mod identity;
|
||||
|
||||
use identity::{IdentityRecyclerFactory, WebGPUMsg};
|
||||
use ipc_channel::ipc::{self, IpcReceiver, IpcSender};
|
||||
use malloc_size_of::{MallocSizeOf, MallocSizeOfOps};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use servo_config::pref;
|
||||
use smallvec::SmallVec;
|
||||
use std::ptr;
|
||||
use wgpu::{
|
||||
binding_model::{BindGroupBinding, BindGroupLayoutBinding},
|
||||
binding_model::{
|
||||
BindGroupDescriptor, BindGroupEntry, BindGroupLayoutDescriptor, BindGroupLayoutEntry,
|
||||
},
|
||||
id::{
|
||||
AdapterId, BindGroupId, BindGroupLayoutId, BufferId, CommandBufferId, CommandEncoderId,
|
||||
ComputePipelineId, DeviceId, PipelineLayoutId, QueueId, ShaderModuleId,
|
||||
},
|
||||
instance::{DeviceDescriptor, RequestAdapterOptions},
|
||||
resource::BufferDescriptor,
|
||||
BufferAddress,
|
||||
instance::RequestAdapterOptions,
|
||||
};
|
||||
use wgt::{BufferAddress, BufferDescriptor, CommandBufferDescriptor, DeviceDescriptor};
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
pub enum WebGPUResponse {
|
||||
|
@ -34,7 +41,6 @@ pub enum WebGPUResponse {
|
|||
queue_id: WebGPUQueue,
|
||||
_descriptor: DeviceDescriptor,
|
||||
},
|
||||
MapReadAsync(IpcSharedMemory),
|
||||
}
|
||||
|
||||
pub type WebGPUResponseResult = Result<WebGPUResponse, String>;
|
||||
|
@ -60,25 +66,25 @@ pub enum WebGPURequest {
|
|||
device_id: DeviceId,
|
||||
bind_group_id: BindGroupId,
|
||||
bind_group_layout_id: BindGroupLayoutId,
|
||||
bindings: Vec<BindGroupBinding>,
|
||||
bindings: Vec<BindGroupEntry>,
|
||||
},
|
||||
CreateBindGroupLayout {
|
||||
sender: IpcSender<WebGPUBindGroupLayout>,
|
||||
device_id: DeviceId,
|
||||
bind_group_layout_id: BindGroupLayoutId,
|
||||
bindings: Vec<BindGroupLayoutBinding>,
|
||||
bindings: Vec<BindGroupLayoutEntry>,
|
||||
},
|
||||
CreateBuffer {
|
||||
sender: IpcSender<WebGPUBuffer>,
|
||||
device_id: DeviceId,
|
||||
buffer_id: BufferId,
|
||||
descriptor: BufferDescriptor,
|
||||
descriptor: BufferDescriptor<String>,
|
||||
},
|
||||
CreateBufferMapped {
|
||||
sender: IpcSender<WebGPUBuffer>,
|
||||
device_id: DeviceId,
|
||||
buffer_id: BufferId,
|
||||
descriptor: BufferDescriptor,
|
||||
descriptor: BufferDescriptor<String>,
|
||||
},
|
||||
CreateCommandEncoder {
|
||||
sender: IpcSender<WebGPUCommandEncoder>,
|
||||
|
@ -109,13 +115,6 @@ pub enum WebGPURequest {
|
|||
},
|
||||
DestroyBuffer(BufferId),
|
||||
Exit(IpcSender<()>),
|
||||
MapReadAsync {
|
||||
sender: IpcSender<WebGPUResponseResult>,
|
||||
buffer_id: BufferId,
|
||||
device_id: DeviceId,
|
||||
usage: u32,
|
||||
size: u64,
|
||||
},
|
||||
RequestAdapter {
|
||||
sender: IpcSender<WebGPUResponseResult>,
|
||||
options: RequestAdapterOptions,
|
||||
|
@ -154,7 +153,7 @@ impl WebGPU {
|
|||
Ok(sender_and_receiver) => sender_and_receiver,
|
||||
Err(e) => {
|
||||
warn!(
|
||||
"Failed to create sender and receiciver for WGPU thread ({})",
|
||||
"Failed to create sender and receiver for WGPU thread ({})",
|
||||
e
|
||||
);
|
||||
return None;
|
||||
|
@ -162,10 +161,21 @@ impl WebGPU {
|
|||
};
|
||||
let sender_clone = sender.clone();
|
||||
|
||||
let (script_sender, _script_recv) = match ipc::channel() {
|
||||
Ok(sender_and_receiver) => sender_and_receiver,
|
||||
Err(e) => {
|
||||
warn!(
|
||||
"Failed to create receiver and sender for WGPU thread ({})",
|
||||
e
|
||||
);
|
||||
return None;
|
||||
},
|
||||
};
|
||||
|
||||
if let Err(e) = std::thread::Builder::new()
|
||||
.name("WGPU".to_owned())
|
||||
.spawn(move || {
|
||||
WGPU::new(receiver, sender_clone).run();
|
||||
WGPU::new(receiver, sender_clone, script_sender).run();
|
||||
})
|
||||
{
|
||||
warn!("Failed to spwan WGPU thread ({})", e);
|
||||
|
@ -184,7 +194,7 @@ impl WebGPU {
|
|||
struct WGPU {
|
||||
receiver: IpcReceiver<WebGPURequest>,
|
||||
sender: IpcSender<WebGPURequest>,
|
||||
global: wgpu::hub::Global<()>,
|
||||
global: wgpu::hub::Global<IdentityRecyclerFactory>,
|
||||
adapters: Vec<WebGPUAdapter>,
|
||||
devices: Vec<WebGPUDevice>,
|
||||
// Track invalid adapters https://gpuweb.github.io/gpuweb/#invalid
|
||||
|
@ -192,21 +202,24 @@ struct WGPU {
|
|||
}
|
||||
|
||||
impl WGPU {
|
||||
fn new(receiver: IpcReceiver<WebGPURequest>, sender: IpcSender<WebGPURequest>) -> Self {
|
||||
fn new(
|
||||
receiver: IpcReceiver<WebGPURequest>,
|
||||
sender: IpcSender<WebGPURequest>,
|
||||
script_sender: IpcSender<WebGPUMsg>,
|
||||
) -> Self {
|
||||
let factory = IdentityRecyclerFactory {
|
||||
sender: script_sender,
|
||||
};
|
||||
WGPU {
|
||||
receiver,
|
||||
sender,
|
||||
global: wgpu::hub::Global::new("wgpu-core"),
|
||||
global: wgpu::hub::Global::new("wgpu-core", factory),
|
||||
adapters: Vec::new(),
|
||||
devices: Vec::new(),
|
||||
_invalid_adapters: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
fn deinit(self) {
|
||||
self.global.delete()
|
||||
}
|
||||
|
||||
fn run(mut self) {
|
||||
while let Ok(msg) = self.receiver.recv() {
|
||||
match msg {
|
||||
|
@ -217,7 +230,7 @@ impl WGPU {
|
|||
let global = &self.global;
|
||||
let command_buffer_id = gfx_select!(command_encoder_id => global.command_encoder_finish(
|
||||
command_encoder_id,
|
||||
&wgpu::command::CommandBufferDescriptor::default()
|
||||
&CommandBufferDescriptor::default()
|
||||
));
|
||||
if let Err(e) = sender.send(WebGPUCommandBuffer(command_buffer_id)) {
|
||||
warn!(
|
||||
|
@ -252,10 +265,11 @@ impl WGPU {
|
|||
bindings,
|
||||
} => {
|
||||
let global = &self.global;
|
||||
let descriptor = wgpu_core::binding_model::BindGroupDescriptor {
|
||||
let descriptor = BindGroupDescriptor {
|
||||
layout: bind_group_layout_id,
|
||||
bindings: bindings.as_ptr(),
|
||||
bindings_length: bindings.len(),
|
||||
entries: bindings.as_ptr(),
|
||||
entries_length: bindings.len(),
|
||||
label: ptr::null(),
|
||||
};
|
||||
let bg_id = gfx_select!(bind_group_id =>
|
||||
global.device_create_bind_group(device_id, &descriptor, bind_group_id));
|
||||
|
@ -275,9 +289,10 @@ impl WGPU {
|
|||
bindings,
|
||||
} => {
|
||||
let global = &self.global;
|
||||
let descriptor = wgpu_core::binding_model::BindGroupLayoutDescriptor {
|
||||
bindings: bindings.as_ptr(),
|
||||
bindings_length: bindings.len(),
|
||||
let descriptor = BindGroupLayoutDescriptor {
|
||||
entries: bindings.as_ptr(),
|
||||
entries_length: bindings.len(),
|
||||
label: ptr::null(),
|
||||
};
|
||||
let bgl_id = gfx_select!(bind_group_layout_id =>
|
||||
global.device_create_bind_group_layout(device_id, &descriptor, bind_group_layout_id));
|
||||
|
@ -297,7 +312,12 @@ impl WGPU {
|
|||
descriptor,
|
||||
} => {
|
||||
let global = &self.global;
|
||||
let id = gfx_select!(buffer_id => global.device_create_buffer(device_id, &descriptor, buffer_id));
|
||||
let desc = BufferDescriptor {
|
||||
size: descriptor.size,
|
||||
usage: descriptor.usage,
|
||||
label: ptr::null(),
|
||||
};
|
||||
let id = gfx_select!(buffer_id => global.device_create_buffer(device_id, &desc, buffer_id));
|
||||
let buffer = WebGPUBuffer(id);
|
||||
if let Err(e) = sender.send(buffer) {
|
||||
warn!(
|
||||
|
@ -313,8 +333,13 @@ impl WGPU {
|
|||
descriptor,
|
||||
} => {
|
||||
let global = &self.global;
|
||||
let desc = BufferDescriptor {
|
||||
size: descriptor.size,
|
||||
usage: descriptor.usage,
|
||||
label: ptr::null(),
|
||||
};
|
||||
let (buffer_id, _arr_buff_ptr) = gfx_select!(buffer_id =>
|
||||
global.device_create_buffer_mapped(device_id, &descriptor, buffer_id));
|
||||
global.device_create_buffer_mapped(device_id, &desc, buffer_id));
|
||||
let buffer = WebGPUBuffer(buffer_id);
|
||||
|
||||
if let Err(e) = sender.send(buffer) {
|
||||
|
@ -418,55 +443,12 @@ impl WGPU {
|
|||
gfx_select!(buffer => global.buffer_destroy(buffer));
|
||||
},
|
||||
WebGPURequest::Exit(sender) => {
|
||||
self.deinit();
|
||||
drop(self.global);
|
||||
if let Err(e) = sender.send(()) {
|
||||
warn!("Failed to send response to WebGPURequest::Exit ({})", e)
|
||||
}
|
||||
return;
|
||||
},
|
||||
WebGPURequest::MapReadAsync {
|
||||
sender,
|
||||
buffer_id,
|
||||
device_id,
|
||||
usage,
|
||||
size,
|
||||
} => {
|
||||
let global = &self.global;
|
||||
let on_read = move |status: wgpu::resource::BufferMapAsyncStatus,
|
||||
ptr: *const u8| {
|
||||
match status {
|
||||
wgpu::resource::BufferMapAsyncStatus::Success => {
|
||||
let array_buffer =
|
||||
unsafe { std::slice::from_raw_parts(ptr, size as usize) };
|
||||
if let Err(e) = sender.send(Ok(WebGPUResponse::MapReadAsync(
|
||||
IpcSharedMemory::from_bytes(array_buffer),
|
||||
))) {
|
||||
warn!(
|
||||
"Failed to send response to WebGPURequest::MapReadAsync ({})",
|
||||
e
|
||||
)
|
||||
}
|
||||
},
|
||||
_ => {
|
||||
if let Err(e) = sender
|
||||
.send(Err("MapReadAsync: Failed to map buffer".to_owned()))
|
||||
{
|
||||
warn!(
|
||||
"Failed to send response to WebGPURequest::MapReadAsync ({})",
|
||||
e
|
||||
)
|
||||
}
|
||||
},
|
||||
}
|
||||
};
|
||||
gfx_select!(buffer_id => global.buffer_map_async(
|
||||
buffer_id,
|
||||
wgpu::resource::BufferUsage::from_bits(usage).unwrap(),
|
||||
0..size,
|
||||
wgpu::resource::BufferMapOperation::Read(Box::new(on_read))
|
||||
));
|
||||
gfx_select!(device_id => global.device_poll(device_id, true));
|
||||
},
|
||||
WebGPURequest::RequestAdapter {
|
||||
sender,
|
||||
options,
|
||||
|
@ -523,6 +505,7 @@ impl WGPU {
|
|||
let id = gfx_select!(device_id => global.adapter_request_device(
|
||||
adapter_id.0,
|
||||
&descriptor,
|
||||
None,
|
||||
device_id
|
||||
));
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue