Skip to content

Commit

Permalink
TextureInner::Surface can now be zero initialized
Browse files Browse the repository at this point in the history
  • Loading branch information
Wumpf committed Dec 28, 2021
1 parent 4b74c4f commit f80a13d
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 11 deletions.
24 changes: 15 additions & 9 deletions wgpu-core/src/device/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3465,17 +3465,16 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {

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) {
Expand All @@ -3487,7 +3486,14 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
.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(())
Expand Down
42 changes: 41 additions & 1 deletion wgpu-core/src/present.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -125,6 +127,31 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
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 {
Expand Down Expand Up @@ -158,7 +185,10 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
levels: 0..1,
},
life_guard: LifeGuard::new("<Surface>"),
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();
Expand Down Expand Up @@ -240,6 +270,16 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {

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 {
Expand Down
4 changes: 3 additions & 1 deletion wgpu/examples/boids/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
}];
Expand Down

0 comments on commit f80a13d

Please sign in to comment.