Skip to content

Commit

Permalink
[dx12] Fix partial texture barrier not affecting stencil aspect
Browse files Browse the repository at this point in the history
Fix clearing of D24Plus also clearing "hidden" stencil
  • Loading branch information
Wumpf committed Dec 19, 2021
1 parent 54f20be commit 6cd27f1
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
34 changes: 26 additions & 8 deletions wgpu-hal/src/dx12/command.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::FormatAspects;

use super::{conv, HResult as _};
use std::{mem, ops::Range, ptr};
use winapi::um::d3d12;
Expand Down Expand Up @@ -319,15 +321,29 @@ impl crate::CommandEncoder<super::Api> for super::CommandEncoder {
// Only one barrier if it affects the whole image.
self.temp.barriers.push(raw);
} else {
// Generate barrier for each layer/level combination.
let has_stencil = FormatAspects::from(barrier.texture.format)
.contains(FormatAspects::STENCIL);
let planes = if has_stencil {
match barrier.range.aspect {
wgt::TextureAspect::All => 0..2,
wgt::TextureAspect::StencilOnly => 1..2,
wgt::TextureAspect::DepthOnly => 0..1,
}
} else {
0..1
};

for rel_mip_level in 0..mip_level_count {
for rel_array_layer in 0..array_layer_count {
raw.u.Transition_mut().Subresource = barrier.texture.calc_subresource(
barrier.range.base_mip_level + rel_mip_level,
barrier.range.base_array_layer + rel_array_layer,
0,
);
self.temp.barriers.push(raw);
for plane in planes.clone() {
raw.u.Transition_mut().Subresource =
barrier.texture.calc_subresource(
barrier.range.base_mip_level + rel_mip_level,
barrier.range.base_array_layer + rel_array_layer,
plane,
);
self.temp.barriers.push(raw);
}
}
}
}
Expand Down Expand Up @@ -610,7 +626,9 @@ impl crate::CommandEncoder<super::Api> for super::CommandEncoder {
if !ds.depth_ops.contains(crate::AttachmentOps::LOAD) {
flags |= native::ClearFlags::DEPTH;
}
if !ds.stencil_ops.contains(crate::AttachmentOps::LOAD) {
if !ds.stencil_ops.contains(crate::AttachmentOps::LOAD)
&& ds.target.view.has_stencil_aspect
{
flags |= native::ClearFlags::STENCIL;
}

Expand Down
5 changes: 4 additions & 1 deletion wgpu-hal/src/dx12/device.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::FormatAspects;

use super::{conv, descriptor, view, HResult as _};
use parking_lot::Mutex;
use std::{ffi, mem, num::NonZeroU32, ptr, slice, sync::Arc};
Expand Down Expand Up @@ -495,6 +497,7 @@ impl crate::Device<super::Api> for super::Device {

Ok(super::TextureView {
raw_format: view_desc.format,
has_stencil_aspect: FormatAspects::from(desc.format).contains(FormatAspects::STENCIL),
target_base: (
texture.resource,
texture.calc_subresource(desc.range.base_mip_level, desc.range.base_array_layer, 0),
Expand Down Expand Up @@ -558,7 +561,7 @@ impl crate::Device<super::Api> for super::Device {
.usage
.intersects(crate::TextureUses::DEPTH_STENCIL_WRITE)
{
let raw_desc = view_desc.to_dsv(crate::FormatAspects::empty());
let raw_desc = view_desc.to_dsv(FormatAspects::empty());
let handle = self.dsv_pool.lock().alloc_handle();
self.raw.CreateDepthStencilView(
texture.resource.as_mut_ptr(),
Expand Down
1 change: 1 addition & 0 deletions wgpu-hal/src/dx12/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,7 @@ impl Texture {
#[derive(Debug)]
pub struct TextureView {
raw_format: native::Format,
has_stencil_aspect: bool,
target_base: (native::Resource, u32),
handle_srv: Option<descriptor::Handle>,
handle_uav: Option<descriptor::Handle>,
Expand Down

0 comments on commit 6cd27f1

Please sign in to comment.