From dc55cb436cdc4918b7f03500735b55fe02c4c177 Mon Sep 17 00:00:00 2001 From: teoxoy <28601907+teoxoy@users.noreply.github.com> Date: Tue, 2 Jul 2024 16:47:35 +0200 Subject: [PATCH] add `Texture::new` fn --- wgpu-core/src/device/global.rs | 25 ++------------- wgpu-core/src/device/resource.rs | 53 ++++++++++++++++---------------- wgpu-core/src/present.rs | 44 ++++++++------------------ wgpu-core/src/resource.rs | 38 +++++++++++++++++++++-- 4 files changed, 77 insertions(+), 83 deletions(-) diff --git a/wgpu-core/src/device/global.rs b/wgpu-core/src/device/global.rs index ae284e9f5..720a57e89 100644 --- a/wgpu-core/src/device/global.rs +++ b/wgpu-core/src/device/global.rs @@ -11,9 +11,7 @@ use crate::{ global::Global, hal_api::HalApi, id::{self, AdapterId, DeviceId, QueueId, SurfaceId}, - init_tracker::TextureInitTracker, instance::{self, Adapter, Surface}, - lock::{rank, RwLock}, pipeline::{ self, ResolvedComputePipelineDescriptor, ResolvedFragmentState, ResolvedProgrammableStageDescriptor, ResolvedRenderPipelineDescriptor, ResolvedVertexState, @@ -538,30 +536,11 @@ impl Global { trace.add(trace::Action::CreateTexture(fid.id(), desc.clone())); } - let format_features = match device - .describe_format_features(&device.adapter, desc.format) - .map_err(|error| resource::CreateTextureError::MissingFeatures(desc.format, error)) - { - Ok(features) => features, + let texture = match device.create_texture_from_hal(hal_texture, desc) { + Ok(texture) => texture, Err(error) => break 'error error, }; - let mut texture = device.create_texture_from_hal( - hal_texture, - conv::map_texture_usage(desc.usage, desc.format.into()), - desc, - format_features, - resource::TextureClearMode::None, - ); - if desc.usage.contains(wgt::TextureUsages::COPY_DST) { - texture.hal_usage |= hal::TextureUses::COPY_DST; - } - - texture.initialization_status = RwLock::new( - rank::TEXTURE_INITIALIZATION_STATUS, - TextureInitTracker::new(desc.mip_level_count, 0), - ); - let (id, resource) = fid.assign(Arc::new(texture)); api_log!("Device::create_texture({desc:?}) -> {id:?}"); diff --git a/wgpu-core/src/device/resource.rs b/wgpu-core/src/device/resource.rs index 7956baca9..41c8cfaf3 100644 --- a/wgpu-core/src/device/resource.rs +++ b/wgpu-core/src/device/resource.rs @@ -15,7 +15,7 @@ use crate::{ hal_label, init_tracker::{ BufferInitTracker, BufferInitTrackerAction, MemoryInitKind, TextureInitRange, - TextureInitTracker, TextureInitTrackerAction, + TextureInitTrackerAction, }, instance::Adapter, lock::{rank, Mutex, MutexGuard, RwLock}, @@ -734,31 +734,23 @@ impl Device { pub(crate) fn create_texture_from_hal( self: &Arc, hal_texture: A::Texture, - hal_usage: hal::TextureUses, desc: &resource::TextureDescriptor, - format_features: wgt::TextureFormatFeatures, - clear_mode: resource::TextureClearMode, - ) -> Texture { - Texture { - inner: Snatchable::new(resource::TextureInner::Native { raw: hal_texture }), - device: self.clone(), - desc: desc.map_label(|_| ()), - hal_usage, + ) -> Result, resource::CreateTextureError> { + let format_features = self + .describe_format_features(&self.adapter, desc.format) + .map_err(|error| resource::CreateTextureError::MissingFeatures(desc.format, error))?; + + let texture = Texture::new( + self, + resource::TextureInner::Native { raw: hal_texture }, + conv::map_texture_usage(desc.usage, desc.format.into()), + desc, format_features, - initialization_status: RwLock::new( - rank::TEXTURE_INITIALIZATION_STATUS, - TextureInitTracker::new(desc.mip_level_count, desc.array_layer_count()), - ), - full_range: TextureSelector { - mips: 0..desc.mip_level_count, - layers: 0..desc.array_layer_count(), - }, - label: desc.label.to_string(), - tracking_data: TrackingData::new(self.tracker_indices.textures.clone()), - clear_mode: RwLock::new(rank::TEXTURE_CLEAR_MODE, clear_mode), - views: Mutex::new(rank::TEXTURE_VIEWS, Vec::new()), - bind_groups: Mutex::new(rank::TEXTURE_BIND_GROUPS, Vec::new()), - } + resource::TextureClearMode::None, + false, + ); + + Ok(texture) } pub fn create_buffer_from_hal( @@ -1055,9 +1047,16 @@ impl Device { resource::TextureClearMode::BufferCopy }; - let mut texture = - self.create_texture_from_hal(raw_texture, hal_usage, desc, format_features, clear_mode); - texture.hal_usage = hal_usage; + let texture = Texture::new( + self, + resource::TextureInner::Native { raw: raw_texture }, + hal_usage, + desc, + format_features, + clear_mode, + true, + ); + Ok(texture) } diff --git a/wgpu-core/src/present.rs b/wgpu-core/src/present.rs index b13c4d6cd..3a5f918e2 100644 --- a/wgpu-core/src/present.rs +++ b/wgpu-core/src/present.rs @@ -20,11 +20,7 @@ use crate::{ global::Global, hal_api::HalApi, hal_label, id, - init_tracker::TextureInitTracker, - lock::{rank, Mutex, RwLock}, - resource::{self, Trackable, TrackingData}, - snatch::Snatchable, - track, + resource::{self, Trackable}, }; use hal::{Queue as _, Surface as _}; @@ -167,7 +163,7 @@ impl Global { drop(fence_guard); let texture_desc = wgt::TextureDescriptor { - label: (), + label: Some(std::borrow::Cow::Borrowed("")), size: wgt::Extent3d { width: config.width, height: config.height, @@ -207,34 +203,20 @@ impl Global { let mut presentation = surface.presentation.lock(); let present = presentation.as_mut().unwrap(); - let texture = resource::Texture { - inner: Snatchable::new(resource::TextureInner::Surface { + let texture = resource::Texture::new( + &device, + resource::TextureInner::Surface { raw: Some(ast.texture), parent_id: surface_id, - }), - device: device.clone(), - desc: texture_desc, - hal_usage, - format_features, - initialization_status: RwLock::new( - rank::TEXTURE_INITIALIZATION_STATUS, - TextureInitTracker::new(1, 1), - ), - full_range: track::TextureSelector { - layers: 0..1, - mips: 0..1, }, - label: String::from(""), - tracking_data: TrackingData::new(device.tracker_indices.textures.clone()), - clear_mode: RwLock::new( - rank::TEXTURE_CLEAR_MODE, - resource::TextureClearMode::Surface { - clear_view: Some(clear_view), - }, - ), - views: Mutex::new(rank::TEXTURE_VIEWS, Vec::new()), - bind_groups: Mutex::new(rank::TEXTURE_BIND_GROUPS, Vec::new()), - }; + hal_usage, + &texture_desc, + format_features, + resource::TextureClearMode::Surface { + clear_view: Some(clear_view), + }, + true, + ); let (id, resource) = fid.assign(Arc::new(texture)); log::debug!("Created CURRENT Surface Texture {:?}", id); diff --git a/wgpu-core/src/resource.rs b/wgpu-core/src/resource.rs index b674df17e..a7b580c93 100644 --- a/wgpu-core/src/resource.rs +++ b/wgpu-core/src/resource.rs @@ -10,11 +10,11 @@ use crate::{ hal_api::HalApi, id::{AdapterId, BufferId, CommandEncoderId, DeviceId, SurfaceId, TextureId, TextureViewId}, init_tracker::{BufferInitTracker, TextureInitTracker}, - lock::{Mutex, RwLock}, + lock::{rank, Mutex, RwLock}, resource_log, snatch::{ExclusiveSnatchGuard, SnatchGuard, Snatchable}, track::{SharedTrackerIndexAllocator, TextureSelector, TrackerIndex}, - Label, SubmissionIndex, + Label, LabelHelpers, SubmissionIndex, }; use hal::CommandEncoder; @@ -960,6 +960,40 @@ pub struct Texture { } impl Texture { + pub(crate) fn new( + device: &Arc>, + inner: TextureInner, + hal_usage: hal::TextureUses, + desc: &TextureDescriptor, + format_features: wgt::TextureFormatFeatures, + clear_mode: TextureClearMode, + init: bool, + ) -> Self { + Texture { + inner: Snatchable::new(inner), + device: device.clone(), + desc: desc.map_label(|_| ()), + hal_usage, + format_features, + initialization_status: RwLock::new( + rank::TEXTURE_INITIALIZATION_STATUS, + if init { + TextureInitTracker::new(desc.mip_level_count, desc.array_layer_count()) + } else { + TextureInitTracker::new(0, 0) + }, + ), + full_range: TextureSelector { + mips: 0..desc.mip_level_count, + layers: 0..desc.array_layer_count(), + }, + label: desc.label.to_string(), + tracking_data: TrackingData::new(device.tracker_indices.textures.clone()), + clear_mode: RwLock::new(rank::TEXTURE_CLEAR_MODE, clear_mode), + views: Mutex::new(rank::TEXTURE_VIEWS, Vec::new()), + bind_groups: Mutex::new(rank::TEXTURE_BIND_GROUPS, Vec::new()), + } + } /// Checks that the given texture usage contains the required texture usage, /// returns an error otherwise. pub(crate) fn check_usage(