From c3ebfde3022fe63de798cd46221c29f8287de103 Mon Sep 17 00:00:00 2001 From: teoxoy <28601907+teoxoy@users.noreply.github.com> Date: Thu, 27 Jun 2024 16:50:45 +0200 Subject: [PATCH] remove `Resource` trait --- wgpu-core/src/binding_model.rs | 8 +----- wgpu-core/src/command/bind.rs | 2 +- wgpu-core/src/command/bundle.rs | 4 +-- wgpu-core/src/command/compute.rs | 2 +- wgpu-core/src/command/memory_init.rs | 2 +- wgpu-core/src/command/mod.rs | 4 +-- wgpu-core/src/command/render.rs | 1 - wgpu-core/src/device/queue.rs | 6 ++--- wgpu-core/src/device/resource.rs | 4 +-- wgpu-core/src/instance.rs | 6 +---- wgpu-core/src/pipeline.rs | 10 +------- wgpu-core/src/resource.rs | 37 ++++++++-------------------- 12 files changed, 21 insertions(+), 65 deletions(-) diff --git a/wgpu-core/src/binding_model.rs b/wgpu-core/src/binding_model.rs index 44eecf027..a0885e27e 100644 --- a/wgpu-core/src/binding_model.rs +++ b/wgpu-core/src/binding_model.rs @@ -8,7 +8,7 @@ use crate::{ init_tracker::{BufferInitTrackerAction, TextureInitTrackerAction}, resource::{ DestroyedResourceError, Labeled, MissingBufferUsageError, MissingTextureUsageError, - ParentDevice, Resource, ResourceErrorIdent, TrackingData, + ParentDevice, ResourceErrorIdent, TrackingData, }, resource_log, snatch::{SnatchGuard, Snatchable}, @@ -499,8 +499,6 @@ crate::impl_labeled!(BindGroupLayout); crate::impl_storage_item!(BindGroupLayout); crate::impl_trackable!(BindGroupLayout); -impl Resource for BindGroupLayout {} - impl ParentDevice for BindGroupLayout { fn device(&self) -> &Arc> { &self.device @@ -727,8 +725,6 @@ crate::impl_labeled!(PipelineLayout); crate::impl_storage_item!(PipelineLayout); crate::impl_trackable!(PipelineLayout); -impl Resource for PipelineLayout {} - impl ParentDevice for PipelineLayout { fn device(&self) -> &Arc> { &self.device @@ -940,8 +936,6 @@ crate::impl_labeled!(BindGroup); crate::impl_storage_item!(BindGroup); crate::impl_trackable!(BindGroup); -impl Resource for BindGroup {} - impl ParentDevice for BindGroup { fn device(&self) -> &Arc> { &self.device diff --git a/wgpu-core/src/command/bind.rs b/wgpu-core/src/command/bind.rs index d8091cbfe..8830ec00e 100644 --- a/wgpu-core/src/command/bind.rs +++ b/wgpu-core/src/command/bind.rs @@ -19,7 +19,7 @@ mod compat { binding_model::BindGroupLayout, device::bgl, hal_api::HalApi, - resource::{Labeled, Resource}, + resource::{Labeled, ParentDevice}, }; use std::{ops::Range, sync::Arc}; diff --git a/wgpu-core/src/command/bundle.rs b/wgpu-core/src/command/bundle.rs index 3f928a3ca..59a254008 100644 --- a/wgpu-core/src/command/bundle.rs +++ b/wgpu-core/src/command/bundle.rs @@ -95,7 +95,7 @@ use crate::{ id, init_tracker::{BufferInitTrackerAction, MemoryInitKind, TextureInitTrackerAction}, pipeline::{PipelineFlags, RenderPipeline, VertexStep}, - resource::{Buffer, DestroyedResourceError, Labeled, ParentDevice, Resource, TrackingData}, + resource::{Buffer, DestroyedResourceError, Labeled, ParentDevice, TrackingData}, resource_log, snatch::SnatchGuard, track::RenderBundleScope, @@ -1188,8 +1188,6 @@ crate::impl_labeled!(RenderBundle); crate::impl_storage_item!(RenderBundle); crate::impl_trackable!(RenderBundle); -impl Resource for RenderBundle {} - impl ParentDevice for RenderBundle { fn device(&self) -> &Arc> { &self.device diff --git a/wgpu-core/src/command/compute.rs b/wgpu-core/src/command/compute.rs index 70685f4c1..7b8c853b4 100644 --- a/wgpu-core/src/command/compute.rs +++ b/wgpu-core/src/command/compute.rs @@ -20,7 +20,7 @@ use crate::{ pipeline::ComputePipeline, resource::{ self, Buffer, DestroyedResourceError, Labeled, MissingBufferUsageError, ParentDevice, - Resource, ResourceErrorIdent, Trackable, + ResourceErrorIdent, Trackable, }, snatch::SnatchGuard, track::{ResourceUsageCompatibilityError, Tracker, TrackerIndex, UsageScope}, diff --git a/wgpu-core/src/command/memory_init.rs b/wgpu-core/src/command/memory_init.rs index 7c0871683..bf31aba1a 100644 --- a/wgpu-core/src/command/memory_init.rs +++ b/wgpu-core/src/command/memory_init.rs @@ -6,7 +6,7 @@ use crate::{ device::Device, hal_api::HalApi, init_tracker::*, - resource::{DestroyedResourceError, Resource, Texture, Trackable}, + resource::{DestroyedResourceError, ParentDevice, Texture, Trackable}, snatch::SnatchGuard, track::{TextureTracker, Tracker}, FastHashMap, diff --git a/wgpu-core/src/command/mod.rs b/wgpu-core/src/command/mod.rs index 89692d65b..bcef4e8e6 100644 --- a/wgpu-core/src/command/mod.rs +++ b/wgpu-core/src/command/mod.rs @@ -36,7 +36,7 @@ use crate::lock::{rank, Mutex}; use crate::snatch::SnatchGuard; use crate::init_tracker::BufferInitTrackerAction; -use crate::resource::{Labeled, ParentDevice, Resource}; +use crate::resource::{Labeled, ParentDevice}; use crate::track::{Tracker, UsageScope}; use crate::LabelHelpers; use crate::{api_log, global::Global, hal_api::HalApi, id, resource_log, Label}; @@ -532,8 +532,6 @@ crate::impl_resource_type!(CommandBuffer); crate::impl_labeled!(CommandBuffer); crate::impl_storage_item!(CommandBuffer); -impl Resource for CommandBuffer {} - impl ParentDevice for CommandBuffer { fn device(&self) -> &Arc> { &self.device diff --git a/wgpu-core/src/command/render.rs b/wgpu-core/src/command/render.rs index 104f055aa..91b6ba22d 100644 --- a/wgpu-core/src/command/render.rs +++ b/wgpu-core/src/command/render.rs @@ -4,7 +4,6 @@ use crate::command::{ }; use crate::init_tracker::BufferInitTrackerAction; use crate::pipeline::RenderPipeline; -use crate::resource::Resource; use crate::snatch::SnatchGuard; use crate::{ api_log, diff --git a/wgpu-core/src/device/queue.rs b/wgpu-core/src/device/queue.rs index dd5dbb5b8..30d8f20be 100644 --- a/wgpu-core/src/device/queue.rs +++ b/wgpu-core/src/device/queue.rs @@ -17,8 +17,8 @@ use crate::{ lock::{rank, Mutex, RwLockWriteGuard}, resource::{ Buffer, BufferAccessError, BufferMapState, DestroyedBuffer, DestroyedResourceError, - DestroyedTexture, Labeled, ParentDevice, Resource, ResourceErrorIdent, StagingBuffer, - Texture, TextureInner, Trackable, TrackingData, + DestroyedTexture, Labeled, ParentDevice, ResourceErrorIdent, StagingBuffer, Texture, + TextureInner, Trackable, TrackingData, }, resource_log, track::{self, TrackerIndex}, @@ -50,8 +50,6 @@ impl Labeled for Queue { } crate::impl_storage_item!(Queue); -impl Resource for Queue {} - impl ParentDevice for Queue { fn device(&self) -> &Arc> { self.device.as_ref().unwrap() diff --git a/wgpu-core/src/device/resource.rs b/wgpu-core/src/device/resource.rs index 9699a610f..36d885260 100644 --- a/wgpu-core/src/device/resource.rs +++ b/wgpu-core/src/device/resource.rs @@ -24,7 +24,7 @@ use crate::{ pool::ResourcePool, registry::Registry, resource::{ - self, Buffer, Labeled, ParentDevice, QuerySet, Resource, Sampler, Texture, TextureView, + self, Buffer, Labeled, ParentDevice, QuerySet, Sampler, Texture, TextureView, TextureViewNotRenderableReason, Trackable, TrackingData, }, resource_log, @@ -3668,5 +3668,3 @@ impl Device { crate::impl_resource_type!(Device); crate::impl_labeled!(Device); crate::impl_storage_item!(Device); - -impl Resource for Device {} diff --git a/wgpu-core/src/instance.rs b/wgpu-core/src/instance.rs index 53f15d3f0..5e407b8fa 100644 --- a/wgpu-core/src/instance.rs +++ b/wgpu-core/src/instance.rs @@ -9,7 +9,7 @@ use crate::{ id::{markers, AdapterId, DeviceId, Id, Marker, QueueId, SurfaceId}, lock::{rank, Mutex}, present::Presentation, - resource::{Labeled, Resource, ResourceType}, + resource::{Labeled, ResourceType}, resource_log, LabelHelpers, DOWNLEVEL_WARNING_MESSAGE, }; @@ -157,8 +157,6 @@ impl crate::storage::StorageItem for Surface { type Marker = markers::Surface; } -impl Resource for Surface {} - impl Surface { pub fn get_capabilities( &self, @@ -376,8 +374,6 @@ impl Labeled for Adapter { } crate::impl_storage_item!(Adapter); -impl Resource for Adapter {} - #[derive(Clone, Debug, Error)] #[non_exhaustive] pub enum IsSurfaceSupportedError { diff --git a/wgpu-core/src/pipeline.rs b/wgpu-core/src/pipeline.rs index 1b91382a8..e22dc46f0 100644 --- a/wgpu-core/src/pipeline.rs +++ b/wgpu-core/src/pipeline.rs @@ -5,7 +5,7 @@ use crate::{ device::{Device, DeviceError, MissingDownlevelFlags, MissingFeatures, RenderPassContext}, hal_api::HalApi, id::{PipelineCacheId, PipelineLayoutId, ShaderModuleId}, - resource::{Labeled, ParentDevice, Resource, TrackingData}, + resource::{Labeled, ParentDevice, TrackingData}, resource_log, validation, Label, }; use arrayvec::ArrayVec; @@ -70,8 +70,6 @@ crate::impl_resource_type!(ShaderModule); crate::impl_labeled!(ShaderModule); crate::impl_storage_item!(ShaderModule); -impl Resource for ShaderModule {} - impl ParentDevice for ShaderModule { fn device(&self) -> &Arc> { &self.device @@ -233,8 +231,6 @@ crate::impl_labeled!(ComputePipeline); crate::impl_storage_item!(ComputePipeline); crate::impl_trackable!(ComputePipeline); -impl Resource for ComputePipeline {} - impl ParentDevice for ComputePipeline { fn device(&self) -> &Arc> { &self.device @@ -296,8 +292,6 @@ crate::impl_labeled!(PipelineCache); crate::impl_storage_item!(PipelineCache); crate::impl_trackable!(PipelineCache); -impl Resource for PipelineCache {} - impl ParentDevice for PipelineCache { fn device(&self) -> &Arc> { &self.device @@ -550,8 +544,6 @@ crate::impl_labeled!(RenderPipeline); crate::impl_storage_item!(RenderPipeline); crate::impl_trackable!(RenderPipeline); -impl Resource for RenderPipeline {} - impl ParentDevice for RenderPipeline { fn device(&self) -> &Arc> { &self.device diff --git a/wgpu-core/src/resource.rs b/wgpu-core/src/resource.rs index a757b0e4a..193775cd4 100644 --- a/wgpu-core/src/resource.rs +++ b/wgpu-core/src/resource.rs @@ -20,7 +20,6 @@ use crate::{ use hal::CommandEncoder; use smallvec::SmallVec; use thiserror::Error; -use wgt::WasmNotSendSync; use std::{ borrow::Borrow, @@ -113,8 +112,12 @@ impl std::fmt::Display for ResourceErrorIdent { pub(crate) trait ParentDevice: Labeled { fn device(&self) -> &Arc>; + fn is_equal(self: &Arc, other: &Arc) -> bool { + Arc::ptr_eq(self, other) + } + fn same_device_as>(&self, other: &O) -> Result<(), DeviceError> { - if self.device().is_equal(other.device()) { + if Arc::ptr_eq(self.device(), other.device()) { Ok(()) } else { Err(DeviceError::DeviceMismatch(Box::new(DeviceMismatch { @@ -127,7 +130,7 @@ pub(crate) trait ParentDevice: Labeled { } fn same_device(&self, device: &Arc>) -> Result<(), DeviceError> { - if self.device().is_equal(device) { + if Arc::ptr_eq(self.device(), device) { Ok(()) } else { Err(DeviceError::DeviceMismatch(Box::new(DeviceMismatch { @@ -186,6 +189,10 @@ pub(crate) trait Trackable: Labeled { /// given index. fn use_at(&self, submit_index: SubmissionIndex); fn submission_index(&self) -> SubmissionIndex; + + fn is_unique(self: &Arc) -> bool { + Arc::strong_count(self) == 1 + } } #[macro_export] @@ -205,18 +212,6 @@ macro_rules! impl_trackable { }; } -pub(crate) trait Resource: 'static + Sized + WasmNotSendSync + Labeled { - fn ref_count(self: &Arc) -> usize { - Arc::strong_count(self) - } - fn is_unique(self: &Arc) -> bool { - self.ref_count() == 1 - } - fn is_equal(self: &Arc, other: &Arc) -> bool { - Arc::ptr_eq(self, other) - } -} - /// The status code provided to the buffer mapping callback. /// /// This is very similar to `BufferAccessResult`, except that this is FFI-friendly. @@ -809,8 +804,6 @@ crate::impl_labeled!(Buffer); crate::impl_storage_item!(Buffer); crate::impl_trackable!(Buffer); -impl Resource for Buffer {} - impl ParentDevice for Buffer { fn device(&self) -> &Arc> { &self.device @@ -903,8 +896,6 @@ impl Labeled for StagingBuffer { crate::impl_storage_item!(StagingBuffer); crate::impl_trackable!(StagingBuffer); -impl Resource for StagingBuffer {} - impl ParentDevice for StagingBuffer { fn device(&self) -> &Arc> { &self.device @@ -1438,8 +1429,6 @@ crate::impl_labeled!(Texture); crate::impl_storage_item!(Texture); crate::impl_trackable!(Texture); -impl Resource for Texture {} - impl ParentDevice for Texture { fn device(&self) -> &Arc> { &self.device @@ -1608,8 +1597,6 @@ crate::impl_labeled!(TextureView); crate::impl_storage_item!(TextureView); crate::impl_trackable!(TextureView); -impl Resource for TextureView {} - impl ParentDevice for TextureView { fn device(&self) -> &Arc> { &self.device @@ -1725,8 +1712,6 @@ crate::impl_labeled!(Sampler); crate::impl_storage_item!(Sampler); crate::impl_trackable!(Sampler); -impl Resource for Sampler {} - impl ParentDevice for Sampler { fn device(&self) -> &Arc> { &self.device @@ -1781,8 +1766,6 @@ crate::impl_labeled!(QuerySet); crate::impl_storage_item!(QuerySet); crate::impl_trackable!(QuerySet); -impl Resource for QuerySet {} - impl QuerySet { pub(crate) fn raw(&self) -> &A::QuerySet { self.raw.as_ref().unwrap()