Skip to content

Commit

Permalink
remove dummy id, let direct have Unused
Browse files Browse the repository at this point in the history
  • Loading branch information
i509VCB committed Dec 19, 2022
1 parent 491db69 commit 8f13df2
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
39 changes: 31 additions & 8 deletions wgpu/src/backend/direct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ use std::{
error::Error,
fmt,
future::{ready, Ready},
num::NonZeroU128,
ops::Range,
slice,
sync::Arc,
};
use wgc::command::{bundle_ffi::*, compute_ffi::*, render_ffi::*};
use wgc::id::TypedId;
use wgt::strict_assert_eq;

const LABEL: &str = "label";

Expand Down Expand Up @@ -450,6 +452,27 @@ pub struct CommandEncoder {
open: bool,
}

/// Representation of an object id that is not used.
///
/// This may be used as the id type when only a the data associated type is used for a specific type of object.
#[derive(Debug, Clone, Copy)]
pub struct Unused;

const UNUSED_SENTINEL: Option<NonZeroU128> = NonZeroU128::new(u128::MAX);

impl From<ObjectId> for Unused {
fn from(id: ObjectId) -> Self {
strict_assert_eq!(Some(NonZeroU128::from(id)), UNUSED_SENTINEL);
Self
}
}

impl From<Unused> for ObjectId {
fn from(_: Unused) -> Self {
ObjectId::from(UNUSED_SENTINEL.expect("This should never panic"))
}
}

impl crate::Context for Context {
type AdapterId = wgc::id::AdapterId;
type AdapterData = ();
Expand Down Expand Up @@ -481,21 +504,21 @@ impl crate::Context for Context {
type ComputePipelineData = ();
type CommandEncoderId = wgc::id::CommandEncoderId;
type CommandEncoderData = CommandEncoder;
type ComputePassId = ObjectId;
type ComputePassId = Unused;
type ComputePassData = wgc::command::ComputePass;
type RenderPassId = ObjectId;
type RenderPassId = Unused;
type RenderPassData = wgc::command::RenderPass;
type CommandBufferId = wgc::id::CommandBufferId;
type CommandBufferData = ();
type RenderBundleEncoderId = ObjectId;
type RenderBundleEncoderId = Unused;
type RenderBundleEncoderData = wgc::command::RenderBundleEncoder;
type RenderBundleId = wgc::id::RenderBundleId;
type RenderBundleData = ();

type SurfaceId = wgc::id::SurfaceId;
type SurfaceData = Surface;
type SurfaceOutputDetail = SurfaceOutputDetail;
type SubmissionIndex = ObjectId;
type SubmissionIndex = Unused;
type SubmissionIndexData = wgc::device::queue::WrappedSubmissionIndex;

type RequestAdapterFuture = Ready<Option<(Self::AdapterId, Self::AdapterData)>>;
Expand Down Expand Up @@ -1359,7 +1382,7 @@ impl crate::Context for Context {
multiview: desc.multiview,
};
match wgc::command::RenderBundleEncoder::new(&descriptor, *device, None) {
Ok(encoder) => (ObjectId::dummy(), encoder),
Ok(encoder) => (Unused, encoder),
Err(e) => panic!("Error in Device::create_render_bundle_encoder: {}", e),
}
}
Expand Down Expand Up @@ -1781,7 +1804,7 @@ impl crate::Context for Context {
desc: &ComputePassDescriptor,
) -> (Self::ComputePassId, Self::ComputePassData) {
(
ObjectId::dummy(),
Unused,
wgc::command::ComputePass::new(
*encoder,
&wgc::command::ComputePassDescriptor {
Expand Down Expand Up @@ -1841,7 +1864,7 @@ impl crate::Context for Context {
});

(
ObjectId::dummy(),
Unused,
wgc::command::RenderPass::new(
*encoder,
&wgc::command::RenderPassDescriptor {
Expand Down Expand Up @@ -2175,7 +2198,7 @@ impl crate::Context for Context {
Ok(index) => index,
Err(err) => self.handle_error_fatal(err, "Queue::submit"),
};
(ObjectId::dummy(), index)
(Unused, index)
}

fn queue_get_timestamp_period(
Expand Down
5 changes: 0 additions & 5 deletions wgpu/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -986,11 +986,6 @@ pub trait Context: Debug + Send + Sized + Sync {
pub struct ObjectId(NonZeroU128);

impl ObjectId {
// DO NOT MERGE: This is definitely not ideal
pub fn dummy() -> Self {
Self(NonZeroU128::new(u128::MAX).unwrap())
}

pub fn global_id(&self) -> u64 {
(self.0.get() >> 64) as u64
}
Expand Down

0 comments on commit 8f13df2

Please sign in to comment.