separate Queue&Device Id (#32966)

Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com>
This commit is contained in:
Samson 2024-08-08 09:53:17 +02:00 committed by GitHub
parent a5df51ea56
commit f989d3776e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 54 additions and 32 deletions

View file

@ -138,10 +138,14 @@ impl GPUAdapterMethods for GPUAdapter {
} }
} }
} }
let id = self let device_id = self
.global() .global()
.wgpu_id_hub() .wgpu_id_hub()
.create_device_id(self.adapter.0.backend()); .create_device_id(self.adapter.0.backend());
let queue_id = self
.global()
.wgpu_id_hub()
.create_queue_id(self.adapter.0.backend());
let pipeline_id = self.global().pipeline_id(); let pipeline_id = self.global().pipeline_id();
if self if self
.channel .channel
@ -150,7 +154,8 @@ impl GPUAdapterMethods for GPUAdapter {
sender, sender,
adapter_id: self.adapter, adapter_id: self.adapter,
descriptor: desc, descriptor: desc,
device_id: id, device_id,
queue_id,
pipeline_id, pipeline_id,
}) })
.is_err() .is_err()

View file

@ -272,6 +272,7 @@ impl GPUCanvasContextMethods for GPUCanvasContext {
.0 .0
.send(WebGPURequest::CreateSwapChain { .send(WebGPURequest::CreateSwapChain {
device_id: descriptor.device.id().0, device_id: descriptor.device.id().0,
queue_id: descriptor.device.GetQueue().id().0,
buffer_ids, buffer_ids,
external_id: self.context_id.0, external_id: self.context_id.0,
sender, sender,

View file

@ -61,6 +61,10 @@ impl GPUQueue {
pub fn set_device(&self, device: &GPUDevice) { pub fn set_device(&self, device: &GPUDevice) {
*self.device.borrow_mut() = Some(Dom::from_ref(device)); *self.device.borrow_mut() = Some(Dom::from_ref(device));
} }
pub fn id(&self) -> WebGPUQueue {
self.queue
}
} }
impl GPUQueueMethods for GPUQueue { impl GPUQueueMethods for GPUQueue {
@ -95,6 +99,7 @@ impl GPUQueueMethods for GPUQueue {
self.channel self.channel
.0 .0
.send(WebGPURequest::Submit { .send(WebGPURequest::Submit {
device_id: self.device.borrow().as_ref().unwrap().id().0,
queue_id: self.queue.0, queue_id: self.queue.0,
command_buffers, command_buffers,
}) })
@ -133,6 +138,7 @@ impl GPUQueueMethods for GPUQueue {
&bytes[data_offset as usize..(data_offset + content_size) as usize], &bytes[data_offset as usize..(data_offset + content_size) as usize],
); );
if let Err(e) = self.channel.0.send(WebGPURequest::WriteBuffer { if let Err(e) = self.channel.0.send(WebGPURequest::WriteBuffer {
device_id: self.device.borrow().as_ref().unwrap().id().0,
queue_id: self.queue.0, queue_id: self.queue.0,
buffer_id: buffer.id().0, buffer_id: buffer.id().0,
buffer_offset, buffer_offset,
@ -169,6 +175,7 @@ impl GPUQueueMethods for GPUQueue {
let final_data = IpcSharedMemory::from_bytes(&bytes); let final_data = IpcSharedMemory::from_bytes(&bytes);
if let Err(e) = self.channel.0.send(WebGPURequest::WriteTexture { if let Err(e) = self.channel.0.send(WebGPURequest::WriteTexture {
device_id: self.device.borrow().as_ref().unwrap().id().0,
queue_id: self.queue.0, queue_id: self.queue.0,
texture_cv, texture_cv,
data_layout: texture_layout, data_layout: texture_layout,
@ -197,6 +204,7 @@ impl GPUQueueMethods for GPUQueue {
.send(WebGPURequest::QueueOnSubmittedWorkDone { .send(WebGPURequest::QueueOnSubmittedWorkDone {
sender, sender,
queue_id: self.queue.0, queue_id: self.queue.0,
device_id: self.device.borrow().as_ref().unwrap().id().0,
}) })
{ {
warn!("QueueOnSubmittedWorkDone failed with {e}") warn!("QueueOnSubmittedWorkDone failed with {e}")

View file

@ -6,12 +6,13 @@ use smallvec::SmallVec;
use webgpu::identity::{ComputePass, ComputePassId, RenderPass, RenderPassId}; use webgpu::identity::{ComputePass, ComputePassId, RenderPass, RenderPassId};
use webgpu::wgc::id::markers::{ use webgpu::wgc::id::markers::{
Adapter, BindGroup, BindGroupLayout, Buffer, CommandEncoder, ComputePipeline, Device, Adapter, BindGroup, BindGroupLayout, Buffer, CommandEncoder, ComputePipeline, Device,
PipelineLayout, RenderBundle, RenderPipeline, Sampler, ShaderModule, Texture, TextureView, PipelineLayout, Queue, RenderBundle, RenderPipeline, Sampler, ShaderModule, Texture,
TextureView,
}; };
use webgpu::wgc::id::{ use webgpu::wgc::id::{
AdapterId, BindGroupId, BindGroupLayoutId, BufferId, CommandEncoderId, ComputePipelineId, AdapterId, BindGroupId, BindGroupLayoutId, BufferId, CommandEncoderId, ComputePipelineId,
DeviceId, PipelineLayoutId, RenderBundleId, RenderPipelineId, SamplerId, ShaderModuleId, DeviceId, PipelineLayoutId, QueueId, RenderBundleId, RenderPipelineId, SamplerId,
TextureId, TextureViewId, ShaderModuleId, TextureId, TextureViewId,
}; };
use webgpu::wgc::identity::IdentityManager; use webgpu::wgc::identity::IdentityManager;
use webgpu::wgt::Backend; use webgpu::wgt::Backend;
@ -20,6 +21,7 @@ use webgpu::wgt::Backend;
pub struct IdentityHub { pub struct IdentityHub {
adapters: IdentityManager<Adapter>, adapters: IdentityManager<Adapter>,
devices: IdentityManager<Device>, devices: IdentityManager<Device>,
queues: IdentityManager<Queue>,
buffers: IdentityManager<Buffer>, buffers: IdentityManager<Buffer>,
bind_groups: IdentityManager<BindGroup>, bind_groups: IdentityManager<BindGroup>,
bind_group_layouts: IdentityManager<BindGroupLayout>, bind_group_layouts: IdentityManager<BindGroupLayout>,
@ -41,6 +43,7 @@ impl IdentityHub {
IdentityHub { IdentityHub {
adapters: IdentityManager::new(), adapters: IdentityManager::new(),
devices: IdentityManager::new(), devices: IdentityManager::new(),
queues: IdentityManager::new(),
buffers: IdentityManager::new(), buffers: IdentityManager::new(),
bind_groups: IdentityManager::new(), bind_groups: IdentityManager::new(),
bind_group_layouts: IdentityManager::new(), bind_group_layouts: IdentityManager::new(),
@ -123,6 +126,14 @@ impl Identities {
self.select(id.backend()).devices.free(id); self.select(id.backend()).devices.free(id);
} }
pub fn create_queue_id(&self, backend: Backend) -> QueueId {
self.select(backend).queues.process(backend)
}
pub fn free_queue_id(&self, id: QueueId) {
self.select(id.backend()).queues.free(id);
}
pub fn create_adapter_ids(&self) -> SmallVec<[AdapterId; 4]> { pub fn create_adapter_ids(&self) -> SmallVec<[AdapterId; 4]> {
let mut ids = SmallVec::new(); let mut ids = SmallVec::new();
for hubs in self.hubs() { for hubs in self.hubs() {

View file

@ -130,6 +130,7 @@ pub enum WebGPURequest {
}, },
CreateSwapChain { CreateSwapChain {
device_id: id::DeviceId, device_id: id::DeviceId,
queue_id: id::QueueId,
buffer_ids: ArrayVec<id::BufferId, PRESENTATION_BUFFER_COUNT>, buffer_ids: ArrayVec<id::BufferId, PRESENTATION_BUFFER_COUNT>,
external_id: u64, external_id: u64,
sender: IpcSender<ImageKey>, sender: IpcSender<ImageKey>,
@ -191,6 +192,7 @@ pub enum WebGPURequest {
adapter_id: WebGPUAdapter, adapter_id: WebGPUAdapter,
descriptor: wgt::DeviceDescriptor<Option<String>>, descriptor: wgt::DeviceDescriptor<Option<String>>,
device_id: id::DeviceId, device_id: id::DeviceId,
queue_id: id::QueueId,
pipeline_id: PipelineId, pipeline_id: PipelineId,
}, },
// Compute Pass // Compute Pass
@ -250,6 +252,7 @@ pub enum WebGPURequest {
command_encoder_id: id::CommandEncoderId, command_encoder_id: id::CommandEncoderId,
}, },
Submit { Submit {
device_id: id::DeviceId,
queue_id: id::QueueId, queue_id: id::QueueId,
command_buffers: Vec<id::CommandBufferId>, command_buffers: Vec<id::CommandBufferId>,
}, },
@ -267,12 +270,14 @@ pub enum WebGPURequest {
size: u64, size: u64,
}, },
WriteBuffer { WriteBuffer {
device_id: id::DeviceId,
queue_id: id::QueueId, queue_id: id::QueueId,
buffer_id: id::BufferId, buffer_id: id::BufferId,
buffer_offset: u64, buffer_offset: u64,
data: IpcSharedMemory, data: IpcSharedMemory,
}, },
WriteTexture { WriteTexture {
device_id: id::DeviceId,
queue_id: id::QueueId, queue_id: id::QueueId,
texture_cv: ImageCopyTexture, texture_cv: ImageCopyTexture,
data_layout: wgt::ImageDataLayout, data_layout: wgt::ImageDataLayout,
@ -282,6 +287,7 @@ pub enum WebGPURequest {
QueueOnSubmittedWorkDone { QueueOnSubmittedWorkDone {
sender: IpcSender<WebGPUResponse>, sender: IpcSender<WebGPUResponse>,
queue_id: id::QueueId, queue_id: id::QueueId,
device_id: id::DeviceId,
}, },
PushErrorScope { PushErrorScope {
device_id: id::DeviceId, device_id: id::DeviceId,

View file

@ -143,21 +143,3 @@ pub struct PresentationData {
image_desc: ImageDescriptor, image_desc: ImageDescriptor,
image_data: ImageData, 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()) }
}
}

View file

@ -38,8 +38,8 @@ use crate::gpu_error::ErrorScope;
use crate::poll_thread::Poller; use crate::poll_thread::Poller;
use crate::render_commands::apply_render_command; use crate::render_commands::apply_render_command;
use crate::{ use crate::{
Adapter, ComputePassId, Error, PopError, PresentationData, RenderPassId, Transmute, WebGPU, Adapter, ComputePassId, Error, PopError, PresentationData, RenderPassId, WebGPU, WebGPUAdapter,
WebGPUAdapter, WebGPUDevice, WebGPUMsg, WebGPUQueue, WebGPURequest, WebGPUResponse, WebGPUDevice, WebGPUMsg, WebGPUQueue, WebGPURequest, WebGPUResponse,
}; };
pub const PRESENTATION_BUFFER_COUNT: usize = 10; pub const PRESENTATION_BUFFER_COUNT: usize = 10;
@ -491,6 +491,7 @@ impl WGPU {
}, },
WebGPURequest::CreateSwapChain { WebGPURequest::CreateSwapChain {
device_id, device_id,
queue_id,
buffer_ids, buffer_ids,
external_id, external_id,
sender, sender,
@ -510,7 +511,7 @@ impl WGPU {
external_id, external_id,
PresentationData { PresentationData {
device_id, device_id,
queue_id: device_id.transmute(), queue_id,
data: vec![255; (buffer_stride * height as u32) as usize], data: vec![255; (buffer_stride * height as u32) as usize],
size: Size2D::new(width, height), size: Size2D::new(width, height),
unassigned_buffer_ids: buffer_ids, unassigned_buffer_ids: buffer_ids,
@ -704,6 +705,7 @@ impl WGPU {
adapter_id, adapter_id,
descriptor, descriptor,
device_id, device_id,
queue_id,
pipeline_id, pipeline_id,
} => { } => {
let desc = DeviceDescriptor { let desc = DeviceDescriptor {
@ -718,7 +720,7 @@ impl WGPU {
&desc, &desc,
None, None,
Some(device_id), Some(device_id),
Some(device_id.transmute()), Some(queue_id),
)); ));
let device = WebGPUDevice(device_id); let device = WebGPUDevice(device_id);
let queue = WebGPUQueue(queue_id); let queue = WebGPUQueue(queue_id);
@ -991,6 +993,7 @@ impl WGPU {
}; };
}, },
WebGPURequest::Submit { WebGPURequest::Submit {
device_id,
queue_id, queue_id,
command_buffers, command_buffers,
} => { } => {
@ -1008,7 +1011,7 @@ impl WGPU {
gfx_select!(queue_id => global.queue_submit(queue_id, &command_buffers)) gfx_select!(queue_id => global.queue_submit(queue_id, &command_buffers))
.map_err(Error::from_error) .map_err(Error::from_error)
}; };
self.maybe_dispatch_error(queue_id.transmute(), result.err()); self.maybe_dispatch_error(device_id, result.err());
}, },
WebGPURequest::SwapChainPresent { WebGPURequest::SwapChainPresent {
external_id, external_id,
@ -1195,6 +1198,7 @@ impl WGPU {
self.maybe_dispatch_wgpu_error(device_id, result.err()); self.maybe_dispatch_wgpu_error(device_id, result.err());
}, },
WebGPURequest::WriteBuffer { WebGPURequest::WriteBuffer {
device_id,
queue_id, queue_id,
buffer_id, buffer_id,
buffer_offset, buffer_offset,
@ -1208,9 +1212,10 @@ impl WGPU {
buffer_offset as wgt::BufferAddress, buffer_offset as wgt::BufferAddress,
&data &data
)); ));
self.maybe_dispatch_wgpu_error(queue_id.transmute(), result.err()); self.maybe_dispatch_wgpu_error(device_id, result.err());
}, },
WebGPURequest::WriteTexture { WebGPURequest::WriteTexture {
device_id,
queue_id, queue_id,
texture_cv, texture_cv,
data_layout, data_layout,
@ -1228,9 +1233,13 @@ impl WGPU {
&size &size
)); ));
drop(_guard); drop(_guard);
self.maybe_dispatch_wgpu_error(queue_id.transmute(), result.err()); self.maybe_dispatch_wgpu_error(device_id, result.err());
}, },
WebGPURequest::QueueOnSubmittedWorkDone { sender, queue_id } => { WebGPURequest::QueueOnSubmittedWorkDone {
sender,
queue_id,
device_id,
} => {
let global = &self.global; let global = &self.global;
let token = self.poller.token(); let token = self.poller.token();
let callback = SubmittedWorkDoneClosure::from_rust(Box::from(move || { let callback = SubmittedWorkDoneClosure::from_rust(Box::from(move || {
@ -1241,7 +1250,7 @@ impl WGPU {
})); }));
let result = gfx_select!(queue_id => global.queue_on_submitted_work_done(queue_id, callback)); let result = gfx_select!(queue_id => global.queue_on_submitted_work_done(queue_id, callback));
self.poller.wake(); self.poller.wake();
self.maybe_dispatch_wgpu_error(queue_id.transmute(), result.err()); self.maybe_dispatch_wgpu_error(device_id, result.err());
}, },
WebGPURequest::DropTexture(id) => { WebGPURequest::DropTexture(id) => {
let global = &self.global; let global = &self.global;