webgpu: leverage routed_promise in calls returning promises (#35859)

Using the RoutedPromiseListener let us define a different
response type for each promise. This removes unreachable branches
that used to exist when they all shared the same WebGPUResponse.

Signed-off-by: webbeef <me@webbeef.org>
This commit is contained in:
webbeef 2025-03-23 11:52:46 -07:00 committed by GitHub
parent 4814cbdb1f
commit 1c9f486f88
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
15 changed files with 221 additions and 263 deletions

View file

@ -32,7 +32,12 @@ pub use {wgpu_core as wgc, wgpu_types as wgt};
use crate::identity::*;
use crate::render_commands::RenderCommand;
use crate::swapchain::WebGPUContextId;
use crate::{Error, ErrorFilter, Mapping, PRESENTATION_BUFFER_COUNT, WebGPUResponse};
use crate::wgc::resource::BufferAccessError;
use crate::{
Error, ErrorFilter, Mapping, PRESENTATION_BUFFER_COUNT, ShaderCompilationInfo,
WebGPUAdapterResponse, WebGPUComputePipelineResponse, WebGPUDeviceResponse,
WebGPUPoppedErrorScopeResponse, WebGPURenderPipelineResponse,
};
#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
pub struct ContextConfiguration {
@ -45,7 +50,7 @@ pub struct ContextConfiguration {
#[derive(Debug, Deserialize, Serialize)]
pub enum WebGPURequest {
BufferMapAsync {
sender: IpcSender<WebGPUResponse>,
sender: IpcSender<Result<Mapping, BufferAccessError>>,
buffer_id: id::BufferId,
device_id: id::DeviceId,
host_map: HostMap,
@ -109,7 +114,7 @@ pub enum WebGPURequest {
descriptor: ComputePipelineDescriptor<'static>,
implicit_ids: Option<(id::PipelineLayoutId, Vec<id::BindGroupLayoutId>)>,
/// present only on ASYNC versions
async_sender: Option<IpcSender<WebGPUResponse>>,
async_sender: Option<IpcSender<WebGPUComputePipelineResponse>>,
},
CreatePipelineLayout {
device_id: id::DeviceId,
@ -122,7 +127,7 @@ pub enum WebGPURequest {
descriptor: RenderPipelineDescriptor<'static>,
implicit_ids: Option<(id::PipelineLayoutId, Vec<id::BindGroupLayoutId>)>,
/// present only on ASYNC versions
async_sender: Option<IpcSender<WebGPUResponse>>,
async_sender: Option<IpcSender<WebGPURenderPipelineResponse>>,
},
CreateSampler {
device_id: id::DeviceId,
@ -134,7 +139,7 @@ pub enum WebGPURequest {
program_id: id::ShaderModuleId,
program: String,
label: Option<String>,
sender: IpcSender<WebGPUResponse>,
sender: IpcSender<Option<ShaderCompilationInfo>>,
},
/// Creates context
CreateContext {
@ -206,12 +211,12 @@ pub enum WebGPURequest {
device_id: id::DeviceId,
},
RequestAdapter {
sender: IpcSender<WebGPUResponse>,
sender: IpcSender<WebGPUAdapterResponse>,
options: RequestAdapterOptions,
adapter_id: AdapterId,
},
RequestDevice {
sender: IpcSender<WebGPUResponse>,
sender: IpcSender<WebGPUDeviceResponse>,
adapter_id: WebGPUAdapter,
descriptor: wgt::DeviceDescriptor<Option<String>>,
device_id: id::DeviceId,
@ -300,7 +305,7 @@ pub enum WebGPURequest {
data: IpcSharedMemory,
},
QueueOnSubmittedWorkDone {
sender: IpcSender<WebGPUResponse>,
sender: IpcSender<()>,
queue_id: id::QueueId,
},
PushErrorScope {
@ -313,7 +318,7 @@ pub enum WebGPURequest {
},
PopErrorScope {
device_id: id::DeviceId,
sender: IpcSender<WebGPUResponse>,
sender: IpcSender<WebGPUPoppedErrorScopeResponse>,
},
ComputeGetBindGroupLayout {
device_id: id::DeviceId,

View file

@ -12,7 +12,6 @@ use wgc::id;
use wgc::pipeline::CreateShaderModuleError;
use wgpu_core::device::HostMap;
use wgpu_core::instance::{RequestAdapterError, RequestDeviceError};
use wgpu_core::resource::BufferAccessError;
pub use {wgpu_core as wgc, wgpu_types as wgt};
use crate::identity::*;
@ -82,23 +81,14 @@ pub struct Mapping {
pub range: Range<u64>,
}
#[derive(Debug, Deserialize, Serialize)]
#[allow(clippy::large_enum_variant)]
pub enum WebGPUResponse {
/// WebGPU is disabled
None,
Adapter(Result<Adapter, RequestAdapterError>),
Device(
(
WebGPUDevice,
WebGPUQueue,
Result<wgt::DeviceDescriptor<Option<String>>, RequestDeviceError>,
),
),
BufferMapAsync(Result<Mapping, BufferAccessError>),
SubmittedWorkDone,
PoppedErrorScope(Result<Option<Error>, PopError>),
CompilationInfo(Option<ShaderCompilationInfo>),
RenderPipeline(Result<Pipeline<id::RenderPipelineId>, Error>),
ComputePipeline(Result<Pipeline<id::ComputePipelineId>, Error>),
}
pub type WebGPUDeviceResponse = (
WebGPUDevice,
WebGPUQueue,
Result<wgt::DeviceDescriptor<Option<String>>, RequestDeviceError>,
);
pub type WebGPUAdapterResponse = Option<Result<Adapter, RequestAdapterError>>;
pub type WebGPUPoppedErrorScopeResponse = Result<Option<Error>, PopError>;
pub type WebGPURenderPipelineResponse = Result<Pipeline<id::RenderPipelineId>, Error>;
pub type WebGPUComputePipelineResponse = Result<Pipeline<id::ComputePipelineId>, Error>;

View file

@ -36,7 +36,7 @@ use crate::render_commands::apply_render_command;
use crate::swapchain::{WGPUImageMap, WebGPUContextId};
use crate::{
Adapter, ComputePassId, Error, Mapping, Pipeline, PopError, RenderPassId, WebGPU,
WebGPUAdapter, WebGPUDevice, WebGPUMsg, WebGPUQueue, WebGPURequest, WebGPUResponse,
WebGPUAdapter, WebGPUDevice, WebGPUMsg, WebGPUQueue, WebGPURequest,
};
#[derive(Eq, Hash, PartialEq)]
@ -196,9 +196,7 @@ impl WGPU {
mode: host_map,
})
});
if let Err(e) =
resp_sender.send(WebGPUResponse::BufferMapAsync(response))
{
if let Err(e) = resp_sender.send(response) {
warn!("Could not send BufferMapAsync Response ({})", e);
}
});
@ -392,8 +390,8 @@ impl WGPU {
}),
Some(e) => Err(Error::from_error(e)),
};
if let Err(e) = sender.send(WebGPUResponse::ComputePipeline(res)) {
warn!("Failed sending WebGPUResponse::ComputePipeline {e:?}");
if let Err(e) = sender.send(res) {
warn!("Failed sending WebGPUComputePipelineResponse {e:?}");
}
} else {
self.maybe_dispatch_wgpu_error(device_id, error);
@ -451,8 +449,8 @@ impl WGPU {
}),
Some(e) => Err(Error::from_error(e)),
};
if let Err(e) = sender.send(WebGPUResponse::RenderPipeline(res)) {
warn!("Failed sending WebGPUResponse::RenderPipeline {e:?}");
if let Err(e) = sender.send(res) {
warn!("Failed sending WebGPURenderPipelineResponse {e:?}");
}
} else {
self.maybe_dispatch_wgpu_error(device_id, error);
@ -488,12 +486,12 @@ impl WGPU {
source,
Some(program_id),
);
if let Err(e) = sender.send(WebGPUResponse::CompilationInfo(
if let Err(e) = sender.send(
error
.as_ref()
.map(|e| crate::ShaderCompilationInfo::from(e, &program)),
)) {
warn!("Failed to send WebGPUResponse::CompilationInfo {e:?}");
) {
warn!("Failed to send CompilationInfo {e:?}");
}
self.maybe_dispatch_wgpu_error(device_id, error);
},
@ -669,7 +667,7 @@ impl WGPU {
}
});
if let Err(e) = sender.send(WebGPUResponse::Adapter(response)) {
if let Err(e) = sender.send(Some(response)) {
warn!(
"Failed to send response to WebGPURequest::RequestAdapter ({})",
e
@ -739,8 +737,7 @@ impl WGPU {
global.device_set_device_lost_closure(device_id, callback);
descriptor
});
if let Err(e) = sender.send(WebGPUResponse::Device((device, queue, result)))
{
if let Err(e) = sender.send((device, queue, result)) {
warn!(
"Failed to send response to WebGPURequest::RequestDevice ({})",
e
@ -1048,7 +1045,7 @@ impl WGPU {
let token = self.poller.token();
let callback = Box::from(move || {
drop(token);
if let Err(e) = sender.send(WebGPUResponse::SubmittedWorkDone) {
if let Err(e) = sender.send(()) {
warn!("Could not send SubmittedWorkDone Response ({})", e);
}
});
@ -1187,25 +1184,21 @@ impl WGPU {
.expect("Device should not be dropped by this point");
if let Some(error_scope_stack) = &mut device_scope.error_scope_stack {
if let Some(error_scope) = error_scope_stack.pop() {
if let Err(e) = sender.send(WebGPUResponse::PoppedErrorScope(Ok(
if let Err(e) = sender.send(Ok(
// TODO: Do actual selection instead of selecting first error
error_scope.errors.first().cloned(),
))) {
)) {
warn!(
"Unable to send {:?} to poperrorscope: {e:?}",
error_scope.errors
);
}
} else if let Err(e) =
sender.send(WebGPUResponse::PoppedErrorScope(Err(PopError::Empty)))
{
} else if let Err(e) = sender.send(Err(PopError::Empty)) {
warn!("Unable to send PopError::Empty: {e:?}");
}
} else {
// device lost
if let Err(e) =
sender.send(WebGPUResponse::PoppedErrorScope(Err(PopError::Lost)))
{
if let Err(e) = sender.send(Err(PopError::Lost)) {
warn!("Unable to send PopError::Lost due {e:?}");
}
}