Improve the ownership story of textures returned by get_current_texture

`present` and `discard` will no longer automatically remove the texture from the registry.
This commit is contained in:
teoxoy 2024-10-11 16:25:04 +02:00 committed by Teodor Tanasoaia
parent e86330977b
commit 39629d0de0
8 changed files with 36 additions and 108 deletions

View File

@ -87,7 +87,6 @@ pub fn op_webgpu_surface_get_current_texture(
let rid = state.resource_table.add(crate::texture::WebGpuTexture {
instance: instance.clone(),
id,
owned: false,
});
Ok(WebGpuResult::rid(rid))
}

View File

@ -13,7 +13,6 @@ use super::error::WebGpuResult;
pub(crate) struct WebGpuTexture {
pub(crate) instance: crate::Instance,
pub(crate) id: wgpu_core::id::TextureId,
pub(crate) owned: bool,
}
impl Resource for WebGpuTexture {
@ -22,11 +21,9 @@ impl Resource for WebGpuTexture {
}
fn close(self: Rc<Self>) {
if self.owned {
let instance = &self.instance;
instance.texture_drop(self.id);
}
}
}
pub(crate) struct WebGpuTextureView(
@ -85,7 +82,6 @@ pub fn op_webgpu_create_texture(
let rid = state.resource_table.add(WebGpuTexture {
instance: instance.clone(),
id: val,
owned: true,
});
Ok(WebGpuResult::rid_err(rid, maybe_err))

View File

@ -17,8 +17,7 @@ use crate::{
conv,
device::{Device, DeviceError, MissingDownlevelFlags, WaitIdleError},
global::Global,
hal_label, id,
resource::{self, Trackable},
hal_label, id, resource,
};
use thiserror::Error;
@ -30,7 +29,7 @@ const FRAME_TIMEOUT_MS: u32 = 1000;
pub(crate) struct Presentation {
pub(crate) device: Arc<Device>,
pub(crate) config: wgt::SurfaceConfiguration<Vec<wgt::TextureFormat>>,
pub(crate) acquired_texture: Option<id::TextureId>,
pub(crate) acquired_texture: Option<Arc<resource::Texture>>,
}
#[derive(Clone, Debug, Error)]
@ -212,12 +211,12 @@ impl Global {
.textures
.insert_single(&texture, hal::TextureUses::UNINITIALIZED);
let id = fid.assign(resource::Fallible::Valid(texture));
if present.acquired_texture.is_some() {
return Err(SurfaceError::AlreadyAcquired);
}
present.acquired_texture = Some(id);
present.acquired_texture = Some(texture.clone());
let id = fid.assign(resource::Fallible::Valid(texture));
let status = if ast.suboptimal {
Status::Suboptimal
@ -249,8 +248,6 @@ impl Global {
pub fn surface_present(&self, surface_id: id::SurfaceId) -> Result<Status, SurfaceError> {
profiling::scope!("SwapChain::present");
let hub = &self.hub;
let surface = self.surfaces.get(surface_id);
let mut presentation = surface.presentation.lock();
@ -269,35 +266,21 @@ impl Global {
device.check_is_valid()?;
let queue = device.get_queue().unwrap();
let result = {
let texture_id = present
let texture = present
.acquired_texture
.take()
.ok_or(SurfaceError::AlreadyAcquired)?;
// The texture ID got added to the device tracker by `submit()`,
// and now we are moving it away.
let texture = hub.textures.remove(texture_id).get();
if let Ok(texture) = texture {
device
.trackers
.lock()
.textures
.remove(texture.tracker_index());
let suf = surface.raw(device.backend()).unwrap();
match texture
let result = match texture
.inner
.snatch(&mut device.snatchable_lock.write())
.unwrap()
{
resource::TextureInner::Surface { raw } => unsafe {
queue.raw().present(suf, raw)
let raw_surface = surface.raw(device.backend()).unwrap();
queue.raw().present(raw_surface, raw)
},
_ => unreachable!(),
}
} else {
Err(hal::SurfaceError::Outdated) //TODO?
}
};
match result {
@ -319,8 +302,6 @@ impl Global {
pub fn surface_texture_discard(&self, surface_id: id::SurfaceId) -> Result<(), SurfaceError> {
profiling::scope!("SwapChain::discard");
let hub = &self.hub;
let surface = self.surfaces.get(surface_id);
let mut presentation = surface.presentation.lock();
let present = match presentation.as_mut() {
@ -337,35 +318,22 @@ impl Global {
device.check_is_valid()?;
{
let texture_id = present
let texture = present
.acquired_texture
.take()
.ok_or(SurfaceError::AlreadyAcquired)?;
// The texture ID got added to the device tracker by `submit()`,
// and now we are moving it away.
let texture = hub.textures.remove(texture_id).get();
if let Ok(texture) = texture {
device
.trackers
.lock()
.textures
.remove(texture.tracker_index());
let suf = surface.raw(device.backend());
match texture
.inner
.snatch(&mut device.snatchable_lock.write())
.unwrap()
{
resource::TextureInner::Surface { raw } => {
unsafe { suf.unwrap().discard_texture(raw) };
let raw_surface = surface.raw(device.backend()).unwrap();
unsafe { raw_surface.discard_texture(raw) };
}
_ => unreachable!(),
}
}
}
Ok(())
}

View File

@ -27,11 +27,6 @@ impl<T: Clone> ResourceMetadata<T> {
}
}
/// Returns the number of indices we can accommodate.
pub(super) fn size(&self) -> usize {
self.owned.len()
}
pub(super) fn set_size(&mut self, size: usize) {
self.resources.resize(size, None);
resize_bitvec(&mut self.owned, size);

View File

@ -18,7 +18,7 @@
//! is known to be in some undefined state. Any transition away from UNINITIALIZED
//! will treat the contents as junk.
use super::{range::RangedStates, PendingTransition, PendingTransitionList, TrackerIndex};
use super::{range::RangedStates, PendingTransition, PendingTransitionList};
use crate::{
resource::{Texture, TextureInner, TextureView, Trackable},
snatch::SnatchGuard,
@ -826,32 +826,6 @@ impl DeviceTextureTracker {
pending.into_hal(tex)
})
}
/// Unconditionally removes the given resource from the tracker.
///
/// Returns true if the resource was removed.
///
/// If the index is higher than the length of internal vectors,
/// false will be returned.
pub fn remove(&mut self, index: TrackerIndex) -> bool {
let index = index.as_usize();
if index >= self.metadata.size() {
return false;
}
self.tracker_assert_in_bounds(index);
unsafe {
if self.metadata.contains_unchecked(index) {
self.current_state_set.complex.remove(&index);
self.metadata.remove(index);
return true;
}
}
false
}
}
impl TextureTrackerSetSingle for DeviceTextureTracker {

View File

@ -252,7 +252,6 @@ impl Device {
Texture {
context: Arc::clone(&self.context),
data,
owned: true,
descriptor: TextureDescriptor {
label: None,
view_formats: &[],
@ -291,7 +290,6 @@ impl Device {
Texture {
context: Arc::clone(&self.context),
data: Box::new(texture),
owned: true,
descriptor: TextureDescriptor {
label: None,
view_formats: &[],

View File

@ -142,7 +142,6 @@ impl Surface<'_> {
texture: Texture {
context: Arc::clone(&self.context),
data,
owned: false,
descriptor,
},
suboptimal,

View File

@ -12,7 +12,6 @@ use crate::*;
pub struct Texture {
pub(crate) context: Arc<C>,
pub(crate) data: Box<Data>,
pub(crate) owned: bool,
pub(crate) descriptor: TextureDescriptor<'static>,
}
#[cfg(send_sync)]
@ -138,7 +137,7 @@ impl Texture {
impl Drop for Texture {
fn drop(&mut self) {
if self.owned && !thread::panicking() {
if !thread::panicking() {
self.context.texture_drop(self.data.as_ref());
}
}