add Texture::new fn

This commit is contained in:
teoxoy 2024-07-02 16:47:35 +02:00 committed by Teodor Tanasoaia
parent 53c79bdf7a
commit dc55cb436c
4 changed files with 77 additions and 83 deletions

View File

@ -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:?}");

View File

@ -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<A: HalApi> Device<A> {
pub(crate) fn create_texture_from_hal(
self: &Arc<Self>,
hal_texture: A::Texture,
hal_usage: hal::TextureUses,
desc: &resource::TextureDescriptor,
format_features: wgt::TextureFormatFeatures,
clear_mode: resource::TextureClearMode<A>,
) -> Texture<A> {
Texture {
inner: Snatchable::new(resource::TextureInner::Native { raw: hal_texture }),
device: self.clone(),
desc: desc.map_label(|_| ()),
hal_usage,
) -> Result<Texture<A>, 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<A: HalApi> Device<A> {
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)
}

View File

@ -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("<Surface Texture>")),
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("<Surface Texture>"),
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);

View File

@ -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<A: HalApi> {
}
impl<A: HalApi> Texture<A> {
pub(crate) fn new(
device: &Arc<Device<A>>,
inner: TextureInner<A>,
hal_usage: hal::TextureUses,
desc: &TextureDescriptor,
format_features: wgt::TextureFormatFeatures,
clear_mode: TextureClearMode<A>,
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(