remove ResourceInfo.id

This commit is contained in:
teoxoy 2024-06-26 13:58:03 +02:00 committed by Teodor Tanasoaia
parent 66777cc80d
commit a422d1cba2
9 changed files with 50 additions and 164 deletions

View File

@ -474,7 +474,7 @@ pub struct BindGroupLayout<A: HalApi> {
pub(crate) origin: bgl::Origin,
#[allow(unused)]
pub(crate) binding_count_validator: BindingTypeMaxCountValidator,
pub(crate) info: ResourceInfo<BindGroupLayout<A>>,
pub(crate) info: ResourceInfo,
}
impl<A: HalApi> Drop for BindGroupLayout<A> {
@ -497,13 +497,9 @@ impl<A: HalApi> Resource for BindGroupLayout<A> {
type Marker = crate::id::markers::BindGroupLayout;
fn as_info(&self) -> &ResourceInfo<Self> {
fn as_info(&self) -> &ResourceInfo {
&self.info
}
fn as_info_mut(&mut self) -> &mut ResourceInfo<Self> {
&mut self.info
}
}
impl<A: HalApi> ParentDevice<A> for BindGroupLayout<A> {
@ -617,7 +613,7 @@ pub struct PipelineLayoutDescriptor<'a> {
pub struct PipelineLayout<A: HalApi> {
pub(crate) raw: Option<A::PipelineLayout>,
pub(crate) device: Arc<Device<A>>,
pub(crate) info: ResourceInfo<PipelineLayout<A>>,
pub(crate) info: ResourceInfo,
pub(crate) bind_group_layouts: ArrayVec<Arc<BindGroupLayout<A>>, { hal::MAX_BIND_GROUPS }>,
pub(crate) push_constant_ranges: ArrayVec<wgt::PushConstantRange, { SHADER_STAGE_COUNT }>,
}
@ -730,13 +726,9 @@ impl<A: HalApi> Resource for PipelineLayout<A> {
type Marker = crate::id::markers::PipelineLayout;
fn as_info(&self) -> &ResourceInfo<Self> {
fn as_info(&self) -> &ResourceInfo {
&self.info
}
fn as_info_mut(&mut self) -> &mut ResourceInfo<Self> {
&mut self.info
}
}
impl<A: HalApi> ParentDevice<A> for PipelineLayout<A> {
@ -850,7 +842,7 @@ pub struct BindGroup<A: HalApi> {
pub(crate) raw: Snatchable<A::BindGroup>,
pub(crate) device: Arc<Device<A>>,
pub(crate) layout: Arc<BindGroupLayout<A>>,
pub(crate) info: ResourceInfo<BindGroup<A>>,
pub(crate) info: ResourceInfo,
pub(crate) used: BindGroupStates<A>,
pub(crate) used_buffer_ranges: Vec<BufferInitTrackerAction<A>>,
pub(crate) used_texture_ranges: Vec<TextureInitTrackerAction<A>>,
@ -948,13 +940,9 @@ impl<A: HalApi> Resource for BindGroup<A> {
type Marker = crate::id::markers::BindGroup;
fn as_info(&self) -> &ResourceInfo<Self> {
fn as_info(&self) -> &ResourceInfo {
&self.info
}
fn as_info_mut(&mut self) -> &mut ResourceInfo<Self> {
&mut self.info
}
}
impl<A: HalApi> ParentDevice<A> for BindGroup<A> {

View File

@ -972,7 +972,7 @@ pub struct RenderBundle<A: HalApi> {
pub(super) buffer_memory_init_actions: Vec<BufferInitTrackerAction<A>>,
pub(super) texture_memory_init_actions: Vec<TextureInitTrackerAction<A>>,
pub(super) context: RenderPassContext,
pub(crate) info: ResourceInfo<RenderBundle<A>>,
pub(crate) info: ResourceInfo,
discard_hal_labels: bool,
}
@ -1187,13 +1187,9 @@ impl<A: HalApi> Resource for RenderBundle<A> {
type Marker = id::markers::RenderBundle;
fn as_info(&self) -> &ResourceInfo<Self> {
fn as_info(&self) -> &ResourceInfo {
&self.info
}
fn as_info_mut(&mut self) -> &mut ResourceInfo<Self> {
&mut self.info
}
}
impl<A: HalApi> ParentDevice<A> for RenderBundle<A> {

View File

@ -311,7 +311,7 @@ impl<A: HalApi> CommandBufferMutable<A> {
pub struct CommandBuffer<A: HalApi> {
pub(crate) device: Arc<Device<A>>,
support_clear_texture: bool,
pub(crate) info: ResourceInfo<CommandBuffer<A>>,
pub(crate) info: ResourceInfo,
/// The mutable state of this command buffer.
///
@ -532,13 +532,9 @@ impl<A: HalApi> Resource for CommandBuffer<A> {
type Marker = id::markers::CommandBuffer;
fn as_info(&self) -> &ResourceInfo<Self> {
fn as_info(&self) -> &ResourceInfo {
&self.info
}
fn as_info_mut(&mut self) -> &mut ResourceInfo<Self> {
&mut self.info
}
}
impl<A: HalApi> ParentDevice<A> for CommandBuffer<A> {

View File

@ -39,7 +39,7 @@ use super::Device;
pub struct Queue<A: HalApi> {
pub(crate) device: Option<Arc<Device<A>>>,
pub(crate) raw: Option<A::Queue>,
pub(crate) info: ResourceInfo<Queue<A>>,
pub(crate) info: ResourceInfo,
}
impl<A: HalApi> Resource for Queue<A> {
@ -47,13 +47,9 @@ impl<A: HalApi> Resource for Queue<A> {
type Marker = id::markers::Queue;
fn as_info(&self) -> &ResourceInfo<Self> {
fn as_info(&self) -> &ResourceInfo {
&self.info
}
fn as_info_mut(&mut self) -> &mut ResourceInfo<Self> {
&mut self.info
}
}
impl<A: HalApi> ParentDevice<A> for Queue<A> {
@ -466,8 +462,7 @@ impl Global {
let mut pending_writes = device.pending_writes.lock();
let pending_writes = pending_writes.as_mut().unwrap();
let stage_fid = hub.staging_buffers.request();
let staging_buffer = stage_fid.init(staging_buffer);
let staging_buffer = Arc::new(staging_buffer);
if let Err(flush_error) = unsafe {
profiling::scope!("copy");
@ -862,8 +857,7 @@ impl Global {
let (staging_buffer, staging_buffer_ptr) =
prepare_staging_buffer(device, stage_size, device.instance_flags)?;
let stage_fid = hub.staging_buffers.request();
let staging_buffer = stage_fid.init(staging_buffer);
let staging_buffer = Arc::new(staging_buffer);
if stage_bytes_per_row == bytes_per_row {
profiling::scope!("copy aligned");

View File

@ -94,7 +94,7 @@ pub struct Device<A: HalApi> {
pub(crate) queue: OnceCell<Weak<Queue<A>>>,
queue_to_drop: OnceCell<A::Queue>,
pub(crate) zero_buffer: Option<A::Buffer>,
pub(crate) info: ResourceInfo<Device<A>>,
pub(crate) info: ResourceInfo,
pub(crate) command_allocator: command::CommandAllocator<A>,
//Note: The submission index here corresponds to the last submission that is done.
@ -3672,11 +3672,7 @@ impl<A: HalApi> Resource for Device<A> {
type Marker = id::markers::Device;
fn as_info(&self) -> &ResourceInfo<Self> {
fn as_info(&self) -> &ResourceInfo {
&self.info
}
fn as_info_mut(&mut self) -> &mut ResourceInfo<Self> {
&mut self.info
}
}

View File

@ -134,7 +134,7 @@ impl Instance {
pub struct Surface {
pub(crate) presentation: Mutex<Option<Presentation>>,
pub(crate) info: ResourceInfo<Surface>,
pub(crate) info: ResourceInfo,
#[cfg(vulkan)]
pub vulkan: Option<HalSurface<hal::api::Vulkan>>,
@ -151,13 +151,9 @@ impl Resource for Surface {
type Marker = markers::Surface;
fn as_info(&self) -> &ResourceInfo<Self> {
fn as_info(&self) -> &ResourceInfo {
&self.info
}
fn as_info_mut(&mut self) -> &mut ResourceInfo<Self> {
&mut self.info
}
}
impl Surface {
@ -181,7 +177,7 @@ impl Surface {
pub struct Adapter<A: HalApi> {
pub(crate) raw: hal::ExposedAdapter<A>,
pub(crate) info: ResourceInfo<Adapter<A>>,
pub(crate) info: ResourceInfo,
}
impl<A: HalApi> Adapter<A> {
@ -378,13 +374,9 @@ impl<A: HalApi> Resource for Adapter<A> {
type Marker = markers::Adapter;
fn as_info(&self) -> &ResourceInfo<Self> {
fn as_info(&self) -> &ResourceInfo {
&self.info
}
fn as_info_mut(&mut self) -> &mut ResourceInfo<Self> {
&mut self.info
}
}
#[derive(Clone, Debug, Error)]

View File

@ -50,7 +50,7 @@ pub struct ShaderModule<A: HalApi> {
pub(crate) raw: Option<A::ShaderModule>,
pub(crate) device: Arc<Device<A>>,
pub(crate) interface: Option<validation::Interface>,
pub(crate) info: ResourceInfo<ShaderModule<A>>,
pub(crate) info: ResourceInfo,
}
impl<A: HalApi> Drop for ShaderModule<A> {
@ -70,13 +70,9 @@ impl<A: HalApi> Resource for ShaderModule<A> {
type Marker = crate::id::markers::ShaderModule;
fn as_info(&self) -> &ResourceInfo<Self> {
fn as_info(&self) -> &ResourceInfo {
&self.info
}
fn as_info_mut(&mut self) -> &mut ResourceInfo<Self> {
&mut self.info
}
}
impl<A: HalApi> ParentDevice<A> for ShaderModule<A> {
@ -218,7 +214,7 @@ pub struct ComputePipeline<A: HalApi> {
pub(crate) device: Arc<Device<A>>,
pub(crate) _shader_module: Arc<ShaderModule<A>>,
pub(crate) late_sized_buffer_groups: ArrayVec<LateSizedBufferGroup, { hal::MAX_BIND_GROUPS }>,
pub(crate) info: ResourceInfo<ComputePipeline<A>>,
pub(crate) info: ResourceInfo,
}
impl<A: HalApi> Drop for ComputePipeline<A> {
@ -238,13 +234,9 @@ impl<A: HalApi> Resource for ComputePipeline<A> {
type Marker = crate::id::markers::ComputePipeline;
fn as_info(&self) -> &ResourceInfo<Self> {
fn as_info(&self) -> &ResourceInfo {
&self.info
}
fn as_info_mut(&mut self) -> &mut ResourceInfo<Self> {
&mut self.info
}
}
impl<A: HalApi> ParentDevice<A> for ComputePipeline<A> {
@ -286,7 +278,7 @@ impl From<hal::PipelineCacheError> for CreatePipelineCacheError {
pub struct PipelineCache<A: HalApi> {
pub(crate) raw: Option<A::PipelineCache>,
pub(crate) device: Arc<Device<A>>,
pub(crate) info: ResourceInfo<PipelineCache<A>>,
pub(crate) info: ResourceInfo,
}
impl<A: HalApi> Drop for PipelineCache<A> {
@ -306,13 +298,9 @@ impl<A: HalApi> Resource for PipelineCache<A> {
type Marker = crate::id::markers::PipelineCache;
fn as_info(&self) -> &ResourceInfo<Self> {
fn as_info(&self) -> &ResourceInfo {
&self.info
}
fn as_info_mut(&mut self) -> &mut ResourceInfo<Self> {
&mut self.info
}
}
impl<A: HalApi> ParentDevice<A> for PipelineCache<A> {
@ -545,7 +533,7 @@ pub struct RenderPipeline<A: HalApi> {
pub(crate) strip_index_format: Option<wgt::IndexFormat>,
pub(crate) vertex_steps: Vec<VertexStep>,
pub(crate) late_sized_buffer_groups: ArrayVec<LateSizedBufferGroup, { hal::MAX_BIND_GROUPS }>,
pub(crate) info: ResourceInfo<RenderPipeline<A>>,
pub(crate) info: ResourceInfo,
}
impl<A: HalApi> Drop for RenderPipeline<A> {
@ -565,13 +553,9 @@ impl<A: HalApi> Resource for RenderPipeline<A> {
type Marker = crate::id::markers::RenderPipeline;
fn as_info(&self) -> &ResourceInfo<Self> {
fn as_info(&self) -> &ResourceInfo {
&self.info
}
fn as_info_mut(&mut self) -> &mut ResourceInfo<Self> {
&mut self.info
}
}
impl<A: HalApi> ParentDevice<A> for RenderPipeline<A> {

View File

@ -74,25 +74,12 @@ impl<T: Resource> FutureId<'_, T> {
self.id
}
pub fn init(&self, mut value: T) -> Arc<T> {
value.as_info_mut().set_id(self.id);
Arc::new(value)
}
pub fn init_in_place(&self, mut value: Arc<T>) -> Arc<T> {
Arc::get_mut(&mut value)
.unwrap()
.as_info_mut()
.set_id(self.id);
value
}
/// Assign a new resource to this ID.
///
/// Registers it with the registry, and fills out the resource info.
/// Registers it with the registry.
pub fn assign(self, value: Arc<T>) -> (Id<T::Marker>, Arc<T>) {
let mut data = self.data.write();
data.insert(self.id, self.init_in_place(value));
data.insert(self.id, value);
(self.id, data.get(self.id).unwrap().clone())
}
@ -126,12 +113,6 @@ impl<T: Resource> Registry<T> {
}
}
pub(crate) fn request(&self) -> FutureId<T> {
FutureId {
id: self.identity.process(self.backend),
data: &self.storage,
}
}
pub(crate) fn try_get(&self, id: Id<T::Marker>) -> Result<Option<Arc<T>>, InvalidId> {
self.read().try_get(id).map(|o| o.cloned())
}
@ -152,9 +133,8 @@ impl<T: Resource> Registry<T> {
self.identity.free(id);
storage.remove(id)
}
pub(crate) fn force_replace(&self, id: Id<T::Marker>, mut value: T) {
pub(crate) fn force_replace(&self, id: Id<T::Marker>, value: T) {
let mut storage = self.storage.write();
value.as_info_mut().set_id(id);
storage.force_replace(id, value)
}
pub(crate) fn force_replace_with_error(&self, id: Id<T::Marker>, label: &str) {
@ -228,7 +208,7 @@ mod tests {
use super::Registry;
struct TestData {
info: ResourceInfo<TestData>,
info: ResourceInfo,
}
struct TestDataId;
impl Marker for TestDataId {}
@ -238,13 +218,9 @@ mod tests {
const TYPE: ResourceType = "Test data";
fn as_info(&self) -> &ResourceInfo<Self> {
fn as_info(&self) -> &ResourceInfo {
&self.info
}
fn as_info_mut(&mut self) -> &mut ResourceInfo<Self> {
&mut self.info
}
}
#[test]

View File

@ -9,7 +9,7 @@ use crate::{
global::Global,
hal_api::HalApi,
id::{
AdapterId, BufferId, CommandEncoderId, DeviceId, Id, Marker, SurfaceId, TextureId,
AdapterId, BufferId, CommandEncoderId, DeviceId, Marker, SurfaceId, TextureId,
TextureViewId,
},
init_tracker::{BufferInitTracker, TextureInitTracker},
@ -57,8 +57,7 @@ use std::{
/// [`Device`]: crate::device::resource::Device
/// [`Buffer`]: crate::resource::Buffer
#[derive(Debug)]
pub(crate) struct ResourceInfo<T: Resource> {
id: Option<Id<T::Marker>>,
pub(crate) struct ResourceInfo {
tracker_index: TrackerIndex,
tracker_indices: Option<Arc<SharedTrackerIndexAllocator>>,
/// The index of the last queue submission in which the resource
@ -74,7 +73,7 @@ pub(crate) struct ResourceInfo<T: Resource> {
label: String,
}
impl<T: Resource> Drop for ResourceInfo<T> {
impl Drop for ResourceInfo {
fn drop(&mut self) {
if let Some(indices) = &self.tracker_indices {
indices.free(self.tracker_index);
@ -82,7 +81,7 @@ impl<T: Resource> Drop for ResourceInfo<T> {
}
}
impl<T: Resource> ResourceInfo<T> {
impl ResourceInfo {
pub(crate) fn new(
label: &Label,
tracker_indices: Option<Arc<SharedTrackerIndexAllocator>>,
@ -92,7 +91,6 @@ impl<T: Resource> ResourceInfo<T> {
.map(|indices| indices.alloc())
.unwrap_or(TrackerIndex::INVALID);
Self {
id: None,
tracker_index,
tracker_indices,
submission_index: AtomicUsize::new(0),
@ -103,20 +101,11 @@ impl<T: Resource> ResourceInfo<T> {
}
}
#[allow(dead_code)]
pub(crate) fn id(&self) -> Id<T::Marker> {
self.id.unwrap()
}
pub(crate) fn tracker_index(&self) -> TrackerIndex {
debug_assert!(self.tracker_index != TrackerIndex::INVALID);
self.tracker_index
}
pub(crate) fn set_id(&mut self, id: Id<T::Marker>) {
self.id = Some(id);
}
/// Record that this resource will be used by the queue submission with the
/// given index.
pub(crate) fn use_at(&self, submit_index: SubmissionIndex) {
@ -176,8 +165,7 @@ pub(crate) type ResourceType = &'static str;
pub(crate) trait Resource: 'static + Sized + WasmNotSendSync {
type Marker: Marker;
const TYPE: ResourceType;
fn as_info(&self) -> &ResourceInfo<Self>;
fn as_info_mut(&mut self) -> &mut ResourceInfo<Self>;
fn as_info(&self) -> &ResourceInfo;
/// Returns a string identifying this resource for logging and errors.
///
@ -450,7 +438,7 @@ pub struct Buffer<A: HalApi> {
pub(crate) size: wgt::BufferAddress,
pub(crate) initialization_status: RwLock<BufferInitTracker>,
pub(crate) sync_mapped_writes: Mutex<Option<hal::MemoryRange>>,
pub(crate) info: ResourceInfo<Buffer<A>>,
pub(crate) info: ResourceInfo,
pub(crate) map_state: Mutex<BufferMapState<A>>,
pub(crate) bind_groups: Mutex<Vec<Weak<BindGroup<A>>>>,
}
@ -796,13 +784,9 @@ impl<A: HalApi> Resource for Buffer<A> {
type Marker = crate::id::markers::Buffer;
fn as_info(&self) -> &ResourceInfo<Self> {
fn as_info(&self) -> &ResourceInfo {
&self.info
}
fn as_info_mut(&mut self) -> &mut ResourceInfo<Self> {
&mut self.info
}
}
impl<A: HalApi> ParentDevice<A> for Buffer<A> {
@ -872,7 +856,7 @@ pub struct StagingBuffer<A: HalApi> {
pub(crate) device: Arc<Device<A>>,
pub(crate) size: wgt::BufferAddress,
pub(crate) is_coherent: bool,
pub(crate) info: ResourceInfo<StagingBuffer<A>>,
pub(crate) info: ResourceInfo,
}
impl<A: HalApi> Drop for StagingBuffer<A> {
@ -892,13 +876,9 @@ impl<A: HalApi> Resource for StagingBuffer<A> {
type Marker = crate::id::markers::StagingBuffer;
fn as_info(&self) -> &ResourceInfo<Self> {
fn as_info(&self) -> &ResourceInfo {
&self.info
}
fn as_info_mut(&mut self) -> &mut ResourceInfo<Self> {
&mut self.info
}
}
impl<A: HalApi> ParentDevice<A> for StagingBuffer<A> {
@ -955,7 +935,7 @@ pub struct Texture<A: HalApi> {
pub(crate) format_features: wgt::TextureFormatFeatures,
pub(crate) initialization_status: RwLock<TextureInitTracker>,
pub(crate) full_range: TextureSelector,
pub(crate) info: ResourceInfo<Texture<A>>,
pub(crate) info: ResourceInfo,
pub(crate) clear_mode: RwLock<TextureClearMode<A>>,
pub(crate) views: Mutex<Vec<Weak<TextureView<A>>>>,
pub(crate) bind_groups: Mutex<Vec<Weak<BindGroup<A>>>>,
@ -1432,13 +1412,9 @@ impl<A: HalApi> Resource for Texture<A> {
type Marker = crate::id::markers::Texture;
fn as_info(&self) -> &ResourceInfo<Self> {
fn as_info(&self) -> &ResourceInfo {
&self.info
}
fn as_info_mut(&mut self) -> &mut ResourceInfo<Self> {
&mut self.info
}
}
impl<A: HalApi> ParentDevice<A> for Texture<A> {
@ -1519,7 +1495,7 @@ pub struct TextureView<A: HalApi> {
pub(crate) render_extent: Result<wgt::Extent3d, TextureViewNotRenderableReason>,
pub(crate) samples: u32,
pub(crate) selector: TextureSelector,
pub(crate) info: ResourceInfo<TextureView<A>>,
pub(crate) info: ResourceInfo,
}
impl<A: HalApi> Drop for TextureView<A> {
@ -1607,13 +1583,9 @@ impl<A: HalApi> Resource for TextureView<A> {
type Marker = crate::id::markers::TextureView;
fn as_info(&self) -> &ResourceInfo<Self> {
fn as_info(&self) -> &ResourceInfo {
&self.info
}
fn as_info_mut(&mut self) -> &mut ResourceInfo<Self> {
&mut self.info
}
}
impl<A: HalApi> ParentDevice<A> for TextureView<A> {
@ -1655,7 +1627,7 @@ pub struct SamplerDescriptor<'a> {
pub struct Sampler<A: HalApi> {
pub(crate) raw: Option<A::Sampler>,
pub(crate) device: Arc<Device<A>>,
pub(crate) info: ResourceInfo<Self>,
pub(crate) info: ResourceInfo,
/// `true` if this is a comparison sampler
pub(crate) comparison: bool,
/// `true` if this is a filtering sampler
@ -1729,13 +1701,9 @@ impl<A: HalApi> Resource for Sampler<A> {
type Marker = crate::id::markers::Sampler;
fn as_info(&self) -> &ResourceInfo<Self> {
fn as_info(&self) -> &ResourceInfo {
&self.info
}
fn as_info_mut(&mut self) -> &mut ResourceInfo<Self> {
&mut self.info
}
}
impl<A: HalApi> ParentDevice<A> for Sampler<A> {
@ -1763,7 +1731,7 @@ pub type QuerySetDescriptor<'a> = wgt::QuerySetDescriptor<Label<'a>>;
pub struct QuerySet<A: HalApi> {
pub(crate) raw: Option<A::QuerySet>,
pub(crate) device: Arc<Device<A>>,
pub(crate) info: ResourceInfo<Self>,
pub(crate) info: ResourceInfo,
pub(crate) desc: wgt::QuerySetDescriptor<()>,
}
@ -1790,13 +1758,9 @@ impl<A: HalApi> Resource for QuerySet<A> {
type Marker = crate::id::markers::QuerySet;
fn as_info(&self) -> &ResourceInfo<Self> {
fn as_info(&self) -> &ResourceInfo {
&self.info
}
fn as_info_mut(&mut self) -> &mut ResourceInfo<Self> {
&mut self.info
}
}
impl<A: HalApi> QuerySet<A> {