From 46b87e220cb530f95258eba419355ddb1246f5f6 Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Sat, 21 Nov 2020 10:38:20 -0500 Subject: [PATCH] Stop using Borrow --- wgpu-core/src/binding_model.rs | 15 +++++-------- wgpu-core/src/command/bundle.rs | 19 ++++------------ wgpu-core/src/pipeline.rs | 23 +++++-------------- wgpu-core/src/resource.rs | 39 +++++++-------------------------- wgpu-core/src/track/mod.rs | 33 +++++++++++++++++----------- 5 files changed, 43 insertions(+), 86 deletions(-) diff --git a/wgpu-core/src/binding_model.rs b/wgpu-core/src/binding_model.rs index e00f40dd6..cc5d270a5 100644 --- a/wgpu-core/src/binding_model.rs +++ b/wgpu-core/src/binding_model.rs @@ -4,10 +4,11 @@ use crate::{ device::{DeviceError, SHADER_STAGE_COUNT}, + hub::Resource, id::{BindGroupLayoutId, BufferId, DeviceId, SamplerId, TextureViewId, Valid}, track::{TrackerSet, DUMMY_SELECTOR}, validation::{MissingBufferUsageError, MissingTextureUsageError}, - FastHashMap, Label, LifeGuard, MultiRefCount, RefCount, Stored, MAX_BIND_GROUPS, + FastHashMap, Label, LifeGuard, MultiRefCount, Stored, MAX_BIND_GROUPS, }; use arrayvec::ArrayVec; @@ -320,7 +321,7 @@ pub struct BindGroupLayout { pub(crate) label: String, } -impl crate::hub::Resource for BindGroupLayout { +impl Resource for BindGroupLayout { const TYPE: &'static str = "BindGroupLayout"; fn life_guard(&self) -> &LifeGuard { @@ -505,7 +506,7 @@ impl PipelineLayout { } } -impl crate::hub::Resource for PipelineLayout { +impl Resource for PipelineLayout { const TYPE: &'static str = "PipelineLayout"; fn life_guard(&self) -> &LifeGuard { @@ -598,19 +599,13 @@ impl BindGroup { } } -impl Borrow for BindGroup { - fn borrow(&self) -> &RefCount { - self.life_guard.ref_count.as_ref().unwrap() - } -} - impl Borrow<()> for BindGroup { fn borrow(&self) -> &() { &DUMMY_SELECTOR } } -impl crate::hub::Resource for BindGroup { +impl Resource for BindGroup { const TYPE: &'static str = "BindGroup"; fn life_guard(&self) -> &LifeGuard { diff --git a/wgpu-core/src/command/bundle.rs b/wgpu-core/src/command/bundle.rs index be0e02f12..fd7f65a4e 100644 --- a/wgpu-core/src/command/bundle.rs +++ b/wgpu-core/src/command/bundle.rs @@ -46,21 +46,16 @@ use crate::{ device::{ AttachmentData, DeviceError, RenderPassContext, MAX_VERTEX_BUFFERS, SHADER_STAGE_COUNT, }, - hub::{GfxBackend, Global, GlobalIdentityHandlerFactory, Input, Storage, Token}, + hub::{GfxBackend, Global, GlobalIdentityHandlerFactory, Input, Resource, Storage, Token}, id, resource::BufferUse, span, track::{TrackerSet, UsageConflict}, validation::check_buffer_usage, - Label, LabelHelpers, LifeGuard, RefCount, Stored, MAX_BIND_GROUPS, + Label, LabelHelpers, LifeGuard, Stored, MAX_BIND_GROUPS, }; use arrayvec::ArrayVec; -use std::{ - borrow::{Borrow, Cow}, - iter, - marker::PhantomData, - ops::Range, -}; +use std::{borrow::Cow, iter, marker::PhantomData, ops::Range}; use thiserror::Error; /// Describes a [`RenderBundleEncoder`]. @@ -348,13 +343,7 @@ impl RenderBundle { } } -impl Borrow for RenderBundle { - fn borrow(&self) -> &RefCount { - self.life_guard.ref_count.as_ref().unwrap() - } -} - -impl crate::hub::Resource for RenderBundle { +impl Resource for RenderBundle { const TYPE: &'static str = "RenderBundle"; fn life_guard(&self) -> &LifeGuard { diff --git a/wgpu-core/src/pipeline.rs b/wgpu-core/src/pipeline.rs index 316eb1949..e47b9a941 100644 --- a/wgpu-core/src/pipeline.rs +++ b/wgpu-core/src/pipeline.rs @@ -5,11 +5,12 @@ use crate::{ binding_model::{CreateBindGroupLayoutError, CreatePipelineLayoutError}, device::{DeviceError, RenderPassContext}, + hub::Resource, id::{DeviceId, PipelineLayoutId, ShaderModuleId}, validation::StageError, - Label, LifeGuard, RefCount, Stored, + Label, LifeGuard, Stored, }; -use std::borrow::{Borrow, Cow}; +use std::borrow::Cow; use thiserror::Error; use wgt::{BufferAddress, IndexFormat, InputStepMode}; @@ -29,7 +30,7 @@ pub struct ShaderModule { pub(crate) module: Option, } -impl crate::hub::Resource for ShaderModule { +impl Resource for ShaderModule { const TYPE: &'static str = "ShaderModule"; fn life_guard(&self) -> &LifeGuard { @@ -108,13 +109,7 @@ pub struct ComputePipeline { pub(crate) life_guard: LifeGuard, } -impl Borrow for ComputePipeline { - fn borrow(&self) -> &RefCount { - self.life_guard.ref_count.as_ref().unwrap() - } -} - -impl crate::hub::Resource for ComputePipeline { +impl Resource for ComputePipeline { const TYPE: &'static str = "ComputePipeline"; fn life_guard(&self) -> &LifeGuard { @@ -233,13 +228,7 @@ pub struct RenderPipeline { pub(crate) life_guard: LifeGuard, } -impl Borrow for RenderPipeline { - fn borrow(&self) -> &RefCount { - self.life_guard.ref_count.as_ref().unwrap() - } -} - -impl crate::hub::Resource for RenderPipeline { +impl Resource for RenderPipeline { const TYPE: &'static str = "RenderPipeline"; fn life_guard(&self) -> &LifeGuard { diff --git a/wgpu-core/src/resource.rs b/wgpu-core/src/resource.rs index 8dc30deec..16319dd27 100644 --- a/wgpu-core/src/resource.rs +++ b/wgpu-core/src/resource.rs @@ -3,7 +3,8 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ use crate::{ - device::{alloc::MemoryBlock, DeviceError}, + device::{alloc::MemoryBlock, DeviceError, HostMap}, + hub::Resource, id::{DeviceId, SwapChainId, TextureId}, track::{TextureSelector, DUMMY_SELECTOR}, validation::MissingBufferUsageError, @@ -93,7 +94,7 @@ pub(crate) enum BufferMapState { Active { ptr: NonNull, sub_range: hal::buffer::SubRange, - host: crate::device::HostMap, + host: HostMap, }, /// Not mapped Idle, @@ -107,7 +108,7 @@ pub type BufferMapCallback = unsafe extern "C" fn(status: BufferMapAsyncStatus, #[repr(C)] #[derive(Debug)] pub struct BufferMapOperation { - pub host: crate::device::HostMap, + pub host: HostMap, pub callback: BufferMapCallback, pub user_data: *mut u8, } @@ -177,13 +178,7 @@ pub enum CreateBufferError { UsageMismatch(wgt::BufferUsage), } -impl Borrow for Buffer { - fn borrow(&self) -> &RefCount { - self.life_guard.ref_count.as_ref().unwrap() - } -} - -impl crate::hub::Resource for Buffer { +impl Resource for Buffer { const TYPE: &'static str = "Buffer"; fn life_guard(&self) -> &LifeGuard { @@ -243,13 +238,7 @@ pub enum CreateTextureError { MissingFeature(wgt::Features, wgt::TextureFormat), } -impl Borrow for Texture { - fn borrow(&self) -> &RefCount { - self.life_guard.ref_count.as_ref().unwrap() - } -} - -impl crate::hub::Resource for Texture { +impl Resource for Texture { const TYPE: &'static str = "Texture"; fn life_guard(&self) -> &LifeGuard { @@ -355,13 +344,7 @@ pub enum TextureViewDestroyError { SwapChainImage, } -impl Borrow for TextureView { - fn borrow(&self) -> &RefCount { - self.life_guard.ref_count.as_ref().unwrap() - } -} - -impl crate::hub::Resource for TextureView { +impl Resource for TextureView { const TYPE: &'static str = "TextureView"; fn life_guard(&self) -> &LifeGuard { @@ -441,13 +424,7 @@ pub enum CreateSamplerError { MissingFeature(wgt::Features), } -impl Borrow for Sampler { - fn borrow(&self) -> &RefCount { - self.life_guard.ref_count.as_ref().unwrap() - } -} - -impl crate::hub::Resource for Sampler { +impl Resource for Sampler { const TYPE: &'static str = "Sampler"; fn life_guard(&self) -> &LifeGuard { diff --git a/wgpu-core/src/track/mod.rs b/wgpu-core/src/track/mod.rs index e12f28aae..6d7e908ef 100644 --- a/wgpu-core/src/track/mod.rs +++ b/wgpu-core/src/track/mod.rs @@ -7,15 +7,12 @@ mod range; mod texture; use crate::{ - conv, - hub::Storage, + conv, hub, id::{self, TypedId, Valid}, resource, Epoch, FastHashMap, Index, RefCount, }; -use std::{ - borrow::Borrow, collections::hash_map::Entry, fmt, marker::PhantomData, ops, vec::Drain, -}; +use std::{collections::hash_map::Entry, fmt, marker::PhantomData, ops, vec::Drain}; use thiserror::Error; pub(crate) use buffer::BufferState; @@ -408,9 +405,9 @@ impl ResourceTracker { /// the last read-only usage, if possible. /// /// Returns the old usage as an error if there is a conflict. - pub(crate) fn use_extend<'a, T: 'a + Borrow>( + pub(crate) fn use_extend<'a, T: 'a + hub::Resource>( &mut self, - storage: &'a Storage, + storage: &'a hub::Storage, id: S::Id, selector: S::Selector, usage: S::Usage, @@ -418,24 +415,34 @@ impl ResourceTracker { let item = storage .get(id) .map_err(|_| UseExtendError::InvalidResource)?; - self.change_extend(Valid(id), item.borrow(), selector, usage) - .map(|()| item) - .map_err(|pending| UseExtendError::Conflict(pending.usage.end)) + self.change_extend( + Valid(id), + item.life_guard().ref_count.as_ref().unwrap(), + selector, + usage, + ) + .map(|()| item) + .map_err(|pending| UseExtendError::Conflict(pending.usage.end)) } /// Use a given resource provided by an `Id` with the specified usage. /// Combines storage access by 'Id' with the transition that replaces /// the last usage with a new one, returning an iterator over these /// transitions. - pub(crate) fn use_replace<'a, T: 'a + Borrow>( + pub(crate) fn use_replace<'a, T: 'a + hub::Resource>( &mut self, - storage: &'a Storage, + storage: &'a hub::Storage, id: S::Id, selector: S::Selector, usage: S::Usage, ) -> Result<(&'a T, Drain>), S::Id> { let item = storage.get(id).map_err(|_| id)?; - let drain = self.change_replace(Valid(id), item.borrow(), selector, usage); + let drain = self.change_replace( + Valid(id), + item.life_guard().ref_count.as_ref().unwrap(), + selector, + usage, + ); Ok((item, drain)) } }