mirror of
https://github.com/gfx-rs/wgpu.git
synced 2025-12-08 21:26:17 +00:00
[core] Replace id transmute method with explicit functions. (#5509)
Replace the `wgpu_core:🆔:Id::transmute` method, the `transmute`
private module, and the `Transmute` sealed trait with some associated
functions with obvious names.
This commit is contained in:
parent
17ef6cac90
commit
03db77cb8c
@ -87,7 +87,7 @@ fn main() {
|
|||||||
&desc,
|
&desc,
|
||||||
None,
|
None,
|
||||||
Some(id),
|
Some(id),
|
||||||
Some(id.transmute())
|
Some(id.into_queue_id())
|
||||||
));
|
));
|
||||||
if let Some(e) = error {
|
if let Some(e) = error {
|
||||||
panic!("{:?}", e);
|
panic!("{:?}", e);
|
||||||
|
|||||||
@ -336,7 +336,7 @@ impl GlobalPlay for wgc::global::Global {
|
|||||||
let bin = std::fs::read(dir.join(data)).unwrap();
|
let bin = std::fs::read(dir.join(data)).unwrap();
|
||||||
let size = (range.end - range.start) as usize;
|
let size = (range.end - range.start) as usize;
|
||||||
if queued {
|
if queued {
|
||||||
self.queue_write_buffer::<A>(device.transmute(), id, range.start, &bin)
|
self.queue_write_buffer::<A>(device.into_queue_id(), id, range.start, &bin)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
} else {
|
} else {
|
||||||
self.device_wait_for_buffer::<A>(device, id).unwrap();
|
self.device_wait_for_buffer::<A>(device, id).unwrap();
|
||||||
@ -351,23 +351,27 @@ impl GlobalPlay for wgc::global::Global {
|
|||||||
size,
|
size,
|
||||||
} => {
|
} => {
|
||||||
let bin = std::fs::read(dir.join(data)).unwrap();
|
let bin = std::fs::read(dir.join(data)).unwrap();
|
||||||
self.queue_write_texture::<A>(device.transmute(), &to, &bin, &layout, &size)
|
self.queue_write_texture::<A>(device.into_queue_id(), &to, &bin, &layout, &size)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
}
|
}
|
||||||
Action::Submit(_index, ref commands) if commands.is_empty() => {
|
Action::Submit(_index, ref commands) if commands.is_empty() => {
|
||||||
self.queue_submit::<A>(device.transmute(), &[]).unwrap();
|
self.queue_submit::<A>(device.into_queue_id(), &[]).unwrap();
|
||||||
}
|
}
|
||||||
Action::Submit(_index, commands) => {
|
Action::Submit(_index, commands) => {
|
||||||
let (encoder, error) = self.device_create_command_encoder::<A>(
|
let (encoder, error) = self.device_create_command_encoder::<A>(
|
||||||
device,
|
device,
|
||||||
&wgt::CommandEncoderDescriptor { label: None },
|
&wgt::CommandEncoderDescriptor { label: None },
|
||||||
Some(comb_manager.process(device.backend()).transmute()),
|
Some(
|
||||||
|
comb_manager
|
||||||
|
.process(device.backend())
|
||||||
|
.into_command_encoder_id(),
|
||||||
|
),
|
||||||
);
|
);
|
||||||
if let Some(e) = error {
|
if let Some(e) = error {
|
||||||
panic!("{e}");
|
panic!("{e}");
|
||||||
}
|
}
|
||||||
let cmdbuf = self.encode_commands::<A>(encoder, commands);
|
let cmdbuf = self.encode_commands::<A>(encoder, commands);
|
||||||
self.queue_submit::<A>(device.transmute(), &[cmdbuf])
|
self.queue_submit::<A>(device.into_queue_id(), &[cmdbuf])
|
||||||
.unwrap();
|
.unwrap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -115,7 +115,7 @@ impl Test<'_> {
|
|||||||
},
|
},
|
||||||
None,
|
None,
|
||||||
Some(device_id),
|
Some(device_id),
|
||||||
Some(device_id.transmute())
|
Some(device_id.into_queue_id())
|
||||||
));
|
));
|
||||||
if let Some(e) = error {
|
if let Some(e) = error {
|
||||||
panic!("{:?}", e);
|
panic!("{:?}", e);
|
||||||
|
|||||||
@ -253,7 +253,7 @@ impl<A: HalApi> CommandBuffer<A> {
|
|||||||
id: id::CommandEncoderId,
|
id: id::CommandEncoderId,
|
||||||
) -> Result<Arc<Self>, CommandEncoderError> {
|
) -> Result<Arc<Self>, CommandEncoderError> {
|
||||||
let storage = hub.command_buffers.read();
|
let storage = hub.command_buffers.read();
|
||||||
match storage.get(id.transmute()) {
|
match storage.get(id.into_command_buffer_id()) {
|
||||||
Ok(cmd_buf) => match cmd_buf.data.lock().as_ref().unwrap().status {
|
Ok(cmd_buf) => match cmd_buf.data.lock().as_ref().unwrap().status {
|
||||||
CommandEncoderStatus::Recording => Ok(cmd_buf.clone()),
|
CommandEncoderStatus::Recording => Ok(cmd_buf.clone()),
|
||||||
CommandEncoderStatus::Finished => Err(CommandEncoderError::NotRecording),
|
CommandEncoderStatus::Finished => Err(CommandEncoderError::NotRecording),
|
||||||
@ -418,7 +418,7 @@ impl Global {
|
|||||||
|
|
||||||
let hub = A::hub(self);
|
let hub = A::hub(self);
|
||||||
|
|
||||||
let error = match hub.command_buffers.get(encoder_id.transmute()) {
|
let error = match hub.command_buffers.get(encoder_id.into_command_buffer_id()) {
|
||||||
Ok(cmd_buf) => {
|
Ok(cmd_buf) => {
|
||||||
let mut cmd_buf_data = cmd_buf.data.lock();
|
let mut cmd_buf_data = cmd_buf.data.lock();
|
||||||
let cmd_buf_data = cmd_buf_data.as_mut().unwrap();
|
let cmd_buf_data = cmd_buf_data.as_mut().unwrap();
|
||||||
@ -444,7 +444,7 @@ impl Global {
|
|||||||
Err(_) => Some(CommandEncoderError::Invalid),
|
Err(_) => Some(CommandEncoderError::Invalid),
|
||||||
};
|
};
|
||||||
|
|
||||||
(encoder_id.transmute(), error)
|
(encoder_id.into_command_buffer_id(), error)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn command_encoder_push_debug_group<A: HalApi>(
|
pub fn command_encoder_push_debug_group<A: HalApi>(
|
||||||
@ -700,7 +700,7 @@ impl PrettyError for PassErrorScope {
|
|||||||
// This error is not in the error chain, only notes are needed
|
// This error is not in the error chain, only notes are needed
|
||||||
match *self {
|
match *self {
|
||||||
Self::Pass(id) => {
|
Self::Pass(id) => {
|
||||||
fmt.command_buffer_label(&id.transmute());
|
fmt.command_buffer_label(&id.into_command_buffer_id());
|
||||||
}
|
}
|
||||||
Self::SetBindGroup(id) => {
|
Self::SetBindGroup(id) => {
|
||||||
fmt.bind_group_label(&id);
|
fmt.bind_group_label(&id);
|
||||||
|
|||||||
@ -2409,7 +2409,10 @@ impl Global {
|
|||||||
(trackers, pending_discard_init_fixups)
|
(trackers, pending_discard_init_fixups)
|
||||||
};
|
};
|
||||||
|
|
||||||
let cmd_buf = hub.command_buffers.get(encoder_id.transmute()).unwrap();
|
let cmd_buf = hub
|
||||||
|
.command_buffers
|
||||||
|
.get(encoder_id.into_command_buffer_id())
|
||||||
|
.unwrap();
|
||||||
let mut cmd_buf_data = cmd_buf.data.lock();
|
let mut cmd_buf_data = cmd_buf.data.lock();
|
||||||
let cmd_buf_data = cmd_buf_data.as_mut().unwrap();
|
let cmd_buf_data = cmd_buf_data.as_mut().unwrap();
|
||||||
|
|
||||||
|
|||||||
@ -1334,7 +1334,9 @@ impl Global {
|
|||||||
profiling::scope!("Device::create_command_encoder");
|
profiling::scope!("Device::create_command_encoder");
|
||||||
|
|
||||||
let hub = A::hub(self);
|
let hub = A::hub(self);
|
||||||
let fid = hub.command_buffers.prepare(id_in.map(|id| id.transmute()));
|
let fid = hub
|
||||||
|
.command_buffers
|
||||||
|
.prepare(id_in.map(|id| id.into_command_buffer_id()));
|
||||||
|
|
||||||
let error = loop {
|
let error = loop {
|
||||||
let device = match hub.devices.get(device_id) {
|
let device = match hub.devices.get(device_id) {
|
||||||
@ -1369,11 +1371,11 @@ impl Global {
|
|||||||
|
|
||||||
let (id, _) = fid.assign(Arc::new(command_buffer));
|
let (id, _) = fid.assign(Arc::new(command_buffer));
|
||||||
api_log!("Device::create_command_encoder -> {id:?}");
|
api_log!("Device::create_command_encoder -> {id:?}");
|
||||||
return (id.transmute(), None);
|
return (id.into_command_encoder_id(), None);
|
||||||
};
|
};
|
||||||
|
|
||||||
let id = fid.assign_error(desc.label.borrow_or_default());
|
let id = fid.assign_error(desc.label.borrow_or_default());
|
||||||
(id.transmute(), Some(error))
|
(id.into_command_encoder_id(), Some(error))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn command_buffer_label<A: HalApi>(&self, id: id::CommandBufferId) -> String {
|
pub fn command_buffer_label<A: HalApi>(&self, id: id::CommandBufferId) -> String {
|
||||||
@ -1388,7 +1390,7 @@ impl Global {
|
|||||||
|
|
||||||
if let Some(cmd_buf) = hub
|
if let Some(cmd_buf) = hub
|
||||||
.command_buffers
|
.command_buffers
|
||||||
.unregister(command_encoder_id.transmute())
|
.unregister(command_encoder_id.into_command_buffer_id())
|
||||||
{
|
{
|
||||||
cmd_buf.data.lock().as_mut().unwrap().encoder.discard();
|
cmd_buf.data.lock().as_mut().unwrap().encoder.discard();
|
||||||
cmd_buf
|
cmd_buf
|
||||||
@ -1400,7 +1402,7 @@ impl Global {
|
|||||||
pub fn command_buffer_drop<A: HalApi>(&self, command_buffer_id: id::CommandBufferId) {
|
pub fn command_buffer_drop<A: HalApi>(&self, command_buffer_id: id::CommandBufferId) {
|
||||||
profiling::scope!("CommandBuffer::drop");
|
profiling::scope!("CommandBuffer::drop");
|
||||||
api_log!("CommandBuffer::drop {command_buffer_id:?}");
|
api_log!("CommandBuffer::drop {command_buffer_id:?}");
|
||||||
self.command_encoder_drop::<A>(command_buffer_id.transmute())
|
self.command_encoder_drop::<A>(command_buffer_id.into_command_encoder_id())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn device_create_render_bundle_encoder(
|
pub fn device_create_render_bundle_encoder(
|
||||||
@ -2121,7 +2123,7 @@ impl Global {
|
|||||||
.map_err(|_| DeviceError::Invalid)?;
|
.map_err(|_| DeviceError::Invalid)?;
|
||||||
|
|
||||||
if let wgt::Maintain::WaitForSubmissionIndex(submission_index) = maintain {
|
if let wgt::Maintain::WaitForSubmissionIndex(submission_index) = maintain {
|
||||||
if submission_index.queue_id != device_id.transmute() {
|
if submission_index.queue_id != device_id.into_queue_id() {
|
||||||
return Err(WaitIdleError::WrongSubmissionIndex(
|
return Err(WaitIdleError::WrongSubmissionIndex(
|
||||||
submission_index.queue_id,
|
submission_index.queue_id,
|
||||||
device_id,
|
device_id,
|
||||||
|
|||||||
@ -707,7 +707,7 @@ impl Global {
|
|||||||
.get(destination.texture)
|
.get(destination.texture)
|
||||||
.map_err(|_| TransferError::InvalidTexture(destination.texture))?;
|
.map_err(|_| TransferError::InvalidTexture(destination.texture))?;
|
||||||
|
|
||||||
if dst.device.as_info().id() != queue_id.transmute() {
|
if dst.device.as_info().id().into_queue_id() != queue_id {
|
||||||
return Err(DeviceError::WrongDevice.into());
|
return Err(DeviceError::WrongDevice.into());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1191,7 +1191,7 @@ impl Global {
|
|||||||
Err(_) => continue,
|
Err(_) => continue,
|
||||||
};
|
};
|
||||||
|
|
||||||
if cmdbuf.device.as_info().id() != queue_id.transmute() {
|
if cmdbuf.device.as_info().id().into_queue_id() != queue_id {
|
||||||
return Err(DeviceError::WrongDevice.into());
|
return Err(DeviceError::WrongDevice.into());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -182,15 +182,6 @@ where
|
|||||||
self.0.backend()
|
self.0.backend()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Transmute this identifier to one with a different marker trait.
|
|
||||||
///
|
|
||||||
/// Legal use is governed through a sealed trait, however it's correctness
|
|
||||||
/// depends on the current implementation of `wgpu-core`.
|
|
||||||
#[inline]
|
|
||||||
pub const fn transmute<U: self::transmute::Transmute<T>>(self) -> Id<U> {
|
|
||||||
Id(self.0, PhantomData)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn zip(index: Index, epoch: Epoch, backend: Backend) -> Self {
|
pub fn zip(index: Index, epoch: Epoch, backend: Backend) -> Self {
|
||||||
Id(RawId::zip(index, epoch, backend), PhantomData)
|
Id(RawId::zip(index, epoch, backend), PhantomData)
|
||||||
@ -202,20 +193,6 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) mod transmute {
|
|
||||||
// This trait is effectively sealed to prevent illegal transmutes.
|
|
||||||
pub trait Transmute<U>: super::Marker {}
|
|
||||||
|
|
||||||
// Self-transmute is always legal.
|
|
||||||
impl<T> Transmute<T> for T where T: super::Marker {}
|
|
||||||
|
|
||||||
// TODO: Remove these once queues have their own identifiers.
|
|
||||||
impl Transmute<super::markers::Queue> for super::markers::Device {}
|
|
||||||
impl Transmute<super::markers::Device> for super::markers::Queue {}
|
|
||||||
impl Transmute<super::markers::CommandBuffer> for super::markers::CommandEncoder {}
|
|
||||||
impl Transmute<super::markers::CommandEncoder> for super::markers::CommandBuffer {}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T> Copy for Id<T> where T: Marker {}
|
impl<T> Copy for Id<T> where T: Marker {}
|
||||||
|
|
||||||
impl<T> Clone for Id<T>
|
impl<T> Clone for Id<T>
|
||||||
@ -349,6 +326,24 @@ ids! {
|
|||||||
pub type QuerySetId QuerySet;
|
pub type QuerySetId QuerySet;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl CommandEncoderId {
|
||||||
|
pub fn into_command_buffer_id(self) -> CommandBufferId {
|
||||||
|
Id(self.0, PhantomData)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl CommandBufferId {
|
||||||
|
pub fn into_command_encoder_id(self) -> CommandEncoderId {
|
||||||
|
Id(self.0, PhantomData)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl DeviceId {
|
||||||
|
pub fn into_queue_id(self) -> QueueId {
|
||||||
|
Id(self.0, PhantomData)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_id_backend() {
|
fn test_id_backend() {
|
||||||
for &b in &[
|
for &b in &[
|
||||||
|
|||||||
@ -1043,7 +1043,10 @@ impl Global {
|
|||||||
profiling::scope!("CommandEncoder::as_hal");
|
profiling::scope!("CommandEncoder::as_hal");
|
||||||
|
|
||||||
let hub = A::hub(self);
|
let hub = A::hub(self);
|
||||||
let cmd_buf = hub.command_buffers.get(id.transmute()).unwrap();
|
let cmd_buf = hub
|
||||||
|
.command_buffers
|
||||||
|
.get(id.into_command_buffer_id())
|
||||||
|
.unwrap();
|
||||||
let mut cmd_buf_data = cmd_buf.data.lock();
|
let mut cmd_buf_data = cmd_buf.data.lock();
|
||||||
let cmd_buf_data = cmd_buf_data.as_mut().unwrap();
|
let cmd_buf_data = cmd_buf_data.as_mut().unwrap();
|
||||||
let cmd_buf_raw = cmd_buf_data.encoder.open().ok();
|
let cmd_buf_raw = cmd_buf_data.encoder.open().ok();
|
||||||
|
|||||||
@ -631,7 +631,7 @@ impl crate::Context for ContextWgpuCore {
|
|||||||
id: queue_id,
|
id: queue_id,
|
||||||
error_sink,
|
error_sink,
|
||||||
};
|
};
|
||||||
ready(Ok((device_id, device, device_id.transmute(), queue)))
|
ready(Ok((device_id, device, device_id.into_queue_id(), queue)))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn instance_poll_all_devices(&self, force_wait: bool) -> bool {
|
fn instance_poll_all_devices(&self, force_wait: bool) -> bool {
|
||||||
@ -1839,8 +1839,7 @@ impl crate::Context for ContextWgpuCore {
|
|||||||
if let Err(cause) = wgc::gfx_select!(
|
if let Err(cause) = wgc::gfx_select!(
|
||||||
encoder => self.0.command_encoder_run_compute_pass(*encoder, pass_data)
|
encoder => self.0.command_encoder_run_compute_pass(*encoder, pass_data)
|
||||||
) {
|
) {
|
||||||
let name =
|
let name = wgc::gfx_select!(encoder => self.0.command_buffer_label(encoder.into_command_buffer_id()));
|
||||||
wgc::gfx_select!(encoder => self.0.command_buffer_label(encoder.transmute()));
|
|
||||||
self.handle_error(
|
self.handle_error(
|
||||||
&encoder_data.error_sink,
|
&encoder_data.error_sink,
|
||||||
cause,
|
cause,
|
||||||
@ -1923,8 +1922,7 @@ impl crate::Context for ContextWgpuCore {
|
|||||||
if let Err(cause) =
|
if let Err(cause) =
|
||||||
wgc::gfx_select!(encoder => self.0.command_encoder_run_render_pass(*encoder, pass_data))
|
wgc::gfx_select!(encoder => self.0.command_encoder_run_render_pass(*encoder, pass_data))
|
||||||
{
|
{
|
||||||
let name =
|
let name = wgc::gfx_select!(encoder => self.0.command_buffer_label(encoder.into_command_buffer_id()));
|
||||||
wgc::gfx_select!(encoder => self.0.command_buffer_label(encoder.transmute()));
|
|
||||||
self.handle_error(
|
self.handle_error(
|
||||||
&encoder_data.error_sink,
|
&encoder_data.error_sink,
|
||||||
cause,
|
cause,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user