mirror of
https://github.com/storytold/wgpu.git
synced 2026-11-25 00:41:07 +01:00
More validation of the surface texture lifetime
This commit is contained in:
1 parent
06316c1bac
commit
53d262c8ee
5 files changed
+44
-16
No files matched your search
@@ -411,6 +411,8 @@ pub enum RenderPassErrorInner {
|
||||
InvalidResolveSourceSampleCount,
|
||||
#[error("resolve target must have a sample count of 1")]
|
||||
InvalidResolveTargetSampleCount,
|
||||
#[error("surface texture is dropped before the render pass is finished")]
|
||||
SurfaceTextureDropped,
|
||||
#[error("not enough memory left")]
|
||||
OutOfMemory,
|
||||
#[error("unable to clear non-present/read-only depth")]
|
||||
@@ -730,6 +732,9 @@ impl<'a, A: HalApi> RenderPassInfo<'a, A> {
|
||||
}
|
||||
|
||||
for ra in self.render_attachments {
|
||||
if !texture_guard.contains(ra.texture_id.value.0) {
|
||||
return Err(RenderPassErrorInner::SurfaceTextureDropped);
|
||||
}
|
||||
let texture = &texture_guard[ra.texture_id.value];
|
||||
check_texture_usage(texture.desc.usage, TextureUsages::RENDER_ATTACHMENT)?;
|
||||
|
||||
|
||||
@@ -550,7 +550,8 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
|
||||
let (compute_pipe_guard, mut token) = hub.compute_pipelines.read(&mut token);
|
||||
let (render_pipe_guard, mut token) = hub.render_pipelines.read(&mut token);
|
||||
let (mut buffer_guard, mut token) = hub.buffers.write(&mut token);
|
||||
let (texture_guard, mut token) = hub.textures.write(&mut token);
|
||||
// This could be made immutable. It's only mutated for the `has_work` flag.
|
||||
let (mut texture_guard, mut token) = hub.textures.write(&mut token);
|
||||
let (texture_view_guard, mut token) = hub.texture_views.read(&mut token);
|
||||
let (sampler_guard, mut token) = hub.samplers.read(&mut token);
|
||||
let (query_set_guard, _) = hub.query_sets.read(&mut token);
|
||||
@@ -610,14 +611,18 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
|
||||
}
|
||||
}
|
||||
for id in cmdbuf.trackers.textures.used() {
|
||||
let texture = &texture_guard[id];
|
||||
let texture = &mut texture_guard[id];
|
||||
match texture.inner {
|
||||
TextureInner::Native { raw: None } => {
|
||||
return Err(QueueSubmitError::DestroyedTexture(id.0));
|
||||
}
|
||||
TextureInner::Native { raw: Some(_) } => {}
|
||||
TextureInner::Surface { .. } => {
|
||||
TextureInner::Surface {
|
||||
ref mut has_work, ..
|
||||
} => {
|
||||
use track::ResourceState as _;
|
||||
|
||||
*has_work = true;
|
||||
let ref_count = cmdbuf.trackers.textures.get_ref_count(id);
|
||||
//TODO: better error handling here?
|
||||
{
|
||||
|
||||
@@ -110,7 +110,7 @@ impl<T, I: id::TypedId> ops::IndexMut<id::Valid<I>> for Storage<T, I> {
|
||||
}
|
||||
|
||||
impl<T, I: id::TypedId> Storage<T, I> {
|
||||
pub(crate) fn _contains(&self, id: I) -> bool {
|
||||
pub(crate) fn contains(&self, id: I) -> bool {
|
||||
let (index, epoch, _) = id.unzip();
|
||||
match self.map[index as usize] {
|
||||
Element::Vacant => false,
|
||||
@@ -648,9 +648,14 @@ impl<A: HalApi, F: GlobalIdentityHandlerFactory> Hub<A, F> {
|
||||
let textures = self.textures.data.read();
|
||||
for element in self.texture_views.data.write().map.drain(..) {
|
||||
if let Element::Occupied(texture_view, _) = element {
|
||||
let device = &devices[textures[texture_view.parent_id.value].device_id.value];
|
||||
unsafe {
|
||||
device.raw.destroy_texture_view(texture_view.raw);
|
||||
// the texture should generally be present, unless it's a surface
|
||||
// texture, and we are in emergency shutdown.
|
||||
if textures.contains(texture_view.parent_id.value.0) {
|
||||
let texture = &textures[texture_view.parent_id.value];
|
||||
let device = &devices[texture.device_id.value];
|
||||
unsafe {
|
||||
device.raw.destroy_texture_view(texture_view.raw);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -128,6 +128,7 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
|
||||
inner: resource::TextureInner::Surface {
|
||||
raw: ast.texture,
|
||||
parent_id: Valid(surface_id),
|
||||
has_work: false,
|
||||
},
|
||||
device_id: present.device_id.clone(),
|
||||
desc: wgt::TextureDescriptor {
|
||||
@@ -235,12 +236,26 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
|
||||
|
||||
let (texture, _) = hub.textures.unregister(texture_id.value.0, &mut token);
|
||||
if let Some(texture) = texture {
|
||||
let suf_texture = match texture.inner {
|
||||
resource::TextureInner::Surface { raw, .. } => raw,
|
||||
resource::TextureInner::Native { .. } => unreachable!(),
|
||||
};
|
||||
let suf = A::get_surface_mut(surface);
|
||||
unsafe { device.queue.present(&mut suf.raw, suf_texture) }
|
||||
match texture.inner {
|
||||
resource::TextureInner::Surface {
|
||||
raw,
|
||||
parent_id,
|
||||
has_work,
|
||||
} => {
|
||||
if surface_id != parent_id.0 {
|
||||
log::error!("Presented frame is from a different surface");
|
||||
Err(hal::SurfaceError::Lost)
|
||||
} else if !has_work {
|
||||
log::error!("No work has been submitted for this frame");
|
||||
unsafe { suf.raw.discard_texture(raw) };
|
||||
Err(hal::SurfaceError::Outdated)
|
||||
} else {
|
||||
unsafe { device.queue.present(&mut suf.raw, raw) }
|
||||
}
|
||||
}
|
||||
resource::TextureInner::Native { .. } => unreachable!(),
|
||||
}
|
||||
} else {
|
||||
Err(hal::SurfaceError::Outdated) //TODO?
|
||||
}
|
||||
|
||||
@@ -164,6 +164,7 @@ pub(crate) enum TextureInner<A: hal::Api> {
|
||||
Surface {
|
||||
raw: A::SurfaceTexture,
|
||||
parent_id: Valid<SurfaceId>,
|
||||
has_work: bool,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -172,10 +173,7 @@ impl<A: hal::Api> TextureInner<A> {
|
||||
match *self {
|
||||
Self::Native { raw: Some(ref tex) } => Some(tex),
|
||||
Self::Native { raw: None } => None,
|
||||
Self::Surface {
|
||||
ref raw,
|
||||
parent_id: _,
|
||||
} => Some(std::borrow::Borrow::borrow(raw)),
|
||||
Self::Surface { ref raw, .. } => Some(raw.borrow()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user