From f80a13d24aa95f7fa966a0bac1ecc5709d8f9bd2 Mon Sep 17 00:00:00 2001 From: Andreas Reich Date: Tue, 28 Dec 2021 18:12:28 +0100 Subject: [PATCH] TextureInner::Surface can now be zero initialized --- wgpu-core/src/device/mod.rs | 24 +++++++++++++-------- wgpu-core/src/present.rs | 42 ++++++++++++++++++++++++++++++++++++- wgpu/examples/boids/main.rs | 4 +++- 3 files changed, 59 insertions(+), 11 deletions(-) diff --git a/wgpu-core/src/device/mod.rs b/wgpu-core/src/device/mod.rs index 097eec3f2e..f2f86fbcda 100644 --- a/wgpu-core/src/device/mod.rs +++ b/wgpu-core/src/device/mod.rs @@ -3465,17 +3465,16 @@ impl Global { let last_submit_index = texture.life_guard.life_count(); + let clear_views = + match std::mem::replace(&mut texture.clear_mode, resource::TextureClearMode::None) { + resource::TextureClearMode::BufferCopy => SmallVec::new(), + resource::TextureClearMode::RenderPass { clear_views, .. } => clear_views, + resource::TextureClearMode::None => SmallVec::new(), + }; + match texture.inner { resource::TextureInner::Native { ref mut raw } => { let raw = raw.take().ok_or(resource::DestroyError::AlreadyDestroyed)?; - let clear_views = match std::mem::replace( - &mut texture.clear_mode, - resource::TextureClearMode::None, - ) { - resource::TextureClearMode::BufferCopy => SmallVec::new(), - resource::TextureClearMode::RenderPass { clear_views, .. } => clear_views, - resource::TextureClearMode::None => SmallVec::new(), - }; let temp = queue::TempResource::Texture(raw, clear_views); if device.pending_writes.dst_textures.contains(&texture_id) { @@ -3487,7 +3486,14 @@ impl Global { .schedule_resource_destruction(temp, last_submit_index); } } - resource::TextureInner::Surface { .. } => {} //TODO + resource::TextureInner::Surface { .. } => { + for clear_view in clear_views { + unsafe { + device.raw.destroy_texture_view(clear_view); + } + } + // TODO? + } } Ok(()) diff --git a/wgpu-core/src/present.rs b/wgpu-core/src/present.rs index c13b8923cb..7421377ed7 100644 --- a/wgpu-core/src/present.rs +++ b/wgpu-core/src/present.rs @@ -9,6 +9,8 @@ When this texture is presented, we remove it from the device tracker as well as extract it from the hub. !*/ +use std::{borrow::Borrow, num::NonZeroU32}; + #[cfg(feature = "trace")] use crate::device::trace::Action; use crate::{ @@ -125,6 +127,31 @@ impl Global { let suf = A::get_surface_mut(surface); let (texture_id, status) = match unsafe { suf.raw.acquire_texture(FRAME_TIMEOUT_MS) } { Ok(Some(ast)) => { + let clear_view_desc = hal::TextureViewDescriptor { + label: Some("clear texture view"), + format: config.format, + dimension: wgt::TextureViewDimension::D2, + usage: hal::TextureUses::COLOR_TARGET, + range: wgt::ImageSubresourceRange { + aspect: wgt::TextureAspect::All, + base_mip_level: 0, + mip_level_count: NonZeroU32::new(1), + base_array_layer: 0, + array_layer_count: NonZeroU32::new(1), + }, + }; + let mut clear_views = smallvec::SmallVec::new(); + clear_views.push( + unsafe { + hal::Device::create_texture_view( + &device.raw, + &ast.texture.borrow(), + &clear_view_desc, + ) + } + .map_err(DeviceError::from)?, + ); + let present = surface.presentation.as_mut().unwrap(); let texture = resource::Texture { inner: resource::TextureInner::Surface { @@ -158,7 +185,10 @@ impl Global { levels: 0..1, }, life_guard: LifeGuard::new(""), - clear_mode: resource::TextureClearMode::None, // TODO: Shouldn't there be a view? + clear_mode: resource::TextureClearMode::RenderPass { + clear_views, + is_color: true, + }, }; let ref_count = texture.life_guard.add_ref(); @@ -240,6 +270,16 @@ impl Global { let (texture, _) = hub.textures.unregister(texture_id.value.0, &mut token); if let Some(texture) = texture { + if let resource::TextureClearMode::RenderPass { clear_views, .. } = + texture.clear_mode + { + for clear_view in clear_views { + unsafe { + hal::Device::destroy_texture_view(&device.raw, clear_view); + } + } + } + let suf = A::get_surface_mut(surface); match texture.inner { resource::TextureInner::Surface { diff --git a/wgpu/examples/boids/main.rs b/wgpu/examples/boids/main.rs index 919ba37074..043d4bc3f9 100644 --- a/wgpu/examples/boids/main.rs +++ b/wgpu/examples/boids/main.rs @@ -279,7 +279,9 @@ impl framework::Example for Example { view, resolve_target: None, ops: wgpu::Operations { - load: wgpu::LoadOp::Clear(wgpu::Color::BLACK), + // Not clearing here in order to test wgpu's zero texture initialization on a surface texture. + // Users should avoid loading uninitialized memory since this can cause additional overhead. + load: wgpu::LoadOp::Load, store: true, }, }];