Skip to content

Commit 9eb78c7

Browse files
committed
Convert texture format to the correct format before passing it to GetCopyableFootprints
1 parent ffda03a commit 9eb78c7

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

wgpu-hal/src/auxil/dxgi/conv.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,33 @@ pub fn map_texture_format_for_srv_uav(
136136
})
137137
}
138138

139+
// see https://microsoft.github.io/DirectX-Specs/d3d/PlanarDepthStencilDDISpec.html#planar-layout-for-staging-from-buffer
140+
pub fn map_texture_format_for_copy(
141+
format: wgt::TextureFormat,
142+
aspect: crate::FormatAspects,
143+
) -> Option<dxgiformat::DXGI_FORMAT> {
144+
Some(match (format, aspect) {
145+
(wgt::TextureFormat::Depth16Unorm, crate::FormatAspects::DEPTH) => {
146+
dxgiformat::DXGI_FORMAT_R16_UNORM
147+
}
148+
(
149+
wgt::TextureFormat::Depth32Float | wgt::TextureFormat::Depth32FloatStencil8,
150+
crate::FormatAspects::DEPTH,
151+
) => dxgiformat::DXGI_FORMAT_R32_FLOAT,
152+
153+
(
154+
wgt::TextureFormat::Stencil8
155+
| wgt::TextureFormat::Depth24PlusStencil8
156+
| wgt::TextureFormat::Depth32FloatStencil8,
157+
crate::FormatAspects::STENCIL,
158+
) => dxgiformat::DXGI_FORMAT_R8_UINT,
159+
160+
(format, crate::FormatAspects::COLOR) => map_texture_format(format),
161+
162+
_ => return None,
163+
})
164+
}
165+
139166
pub fn map_texture_format_for_resource(
140167
format: wgt::TextureFormat,
141168
usage: crate::TextureUses,

wgpu-hal/src/dx12/command.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,20 @@ impl crate::BufferTextureCopy {
2020
&self,
2121
device: &d3d12_ty::ID3D12Device,
2222
mut desc: d3d12_ty::D3D12_RESOURCE_DESC,
23+
format: wgt::TextureFormat,
2324
) -> d3d12_ty::D3D12_PLACED_SUBRESOURCE_FOOTPRINT {
2425
desc.Width = self.size.width as u64;
2526
desc.Height = self.size.height;
2627
desc.DepthOrArraySize = self.size.depth as u16;
2728
desc.MipLevels = 1;
29+
desc.Flags = 0;
30+
31+
if let Some(fmt) =
32+
auxil::dxgi::conv::map_texture_format_for_copy(format, self.texture_base.aspect)
33+
{
34+
desc.Format = fmt;
35+
}
36+
2837
let mut fp = d3d12_ty::D3D12_PLACED_SUBRESOURCE_FOOTPRINT::default();
2938
unsafe {
3039
device.GetCopyableFootprints(
@@ -38,6 +47,11 @@ impl crate::BufferTextureCopy {
3847
ptr::null_mut(),
3948
);
4049
}
50+
fp.Footprint.RowPitch = wgt::math::align_to(
51+
fp.Footprint.RowPitch,
52+
d3d12_ty::D3D12_TEXTURE_DATA_PITCH_ALIGNMENT,
53+
);
54+
4155
fp
4256
}
4357
}
@@ -545,7 +559,7 @@ impl crate::CommandEncoder<super::Api> for super::CommandEncoder {
545559
let src_box = make_box(&wgt::Origin3d::ZERO, &r.size);
546560
unsafe {
547561
*src_location.u.PlacedFootprint_mut() =
548-
r.to_subresource_footprint(&self.device, dst.resource.GetDesc());
562+
r.to_subresource_footprint(&self.device, dst.resource.GetDesc(), dst.format);
549563
};
550564
unsafe {
551565
*dst_location.u.SubresourceIndex_mut() =
@@ -592,7 +606,7 @@ impl crate::CommandEncoder<super::Api> for super::CommandEncoder {
592606
};
593607
unsafe {
594608
*dst_location.u.PlacedFootprint_mut() =
595-
r.to_subresource_footprint(&self.device, src.resource.GetDesc());
609+
r.to_subresource_footprint(&self.device, src.resource.GetDesc(), src.format);
596610
};
597611
unsafe { list.CopyTextureRegion(&dst_location, 0, 0, 0, &src_location, &src_box) };
598612
}

0 commit comments

Comments
 (0)