-
Notifications
You must be signed in to change notification settings - Fork 1.1k
DX12: Align copies b/w textures and buffers when D3D12_FEATURE_DATA_D3D12_OPTIONS13.UnrestrictedBufferTextureCopyPitchSupported
is false
#7721
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ErichDonGubler
wants to merge
5
commits into
gfx-rs:trunk
Choose a base branch
from
erichdongubler-mozilla:dx12-aligned-texbuf-copy-offset
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7c73b70
refactor(dx12): detect `UnrestrictedBufferTextureCopyPitchSupported`
ErichDonGubler 2a8e0ff
refactor(dx12): lower `list` extraction for copies between textures a…
ErichDonGubler 98bf7e0
refactor(hal): impl. `From` conversions b/w `wgh::CopyExtent` and `wg…
ErichDonGubler fec2dba
refactor(dx12): extract `copy_aligned` in `copy_texture_to_buffer`
ErichDonGubler 698316c
fix(dx12): align tex. <-> buf. copies via intermediate buffer if `!Un…
ErichDonGubler File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,7 @@ use crate::{ | |
dxgi::{name::ObjectExt, result::HResult as _}, | ||
}, | ||
dx12::borrow_interface_temporarily, | ||
AccelerationStructureEntries, | ||
AccelerationStructureEntries, CommandEncoder as _, | ||
}; | ||
|
||
fn make_box(origin: &wgt::Origin3d, size: &crate::CopyExtent) -> Direct3D12::D3D12_BOX { | ||
|
@@ -312,6 +312,78 @@ impl super::CommandEncoder { | |
} | ||
} | ||
} | ||
|
||
unsafe fn buf_tex_intermediate<T>( | ||
&mut self, | ||
region: crate::BufferTextureCopy, | ||
tex_fmt: wgt::TextureFormat, | ||
copy_op: impl FnOnce(&mut Self, &super::Buffer, wgt::BufferSize, crate::BufferTextureCopy) -> T, | ||
) -> Option<(T, super::Buffer)> { | ||
let size = { | ||
let copy_info = region.buffer_layout.get_buffer_texture_copy_info( | ||
tex_fmt, | ||
region.texture_base.aspect.map(), | ||
®ion.size.into(), | ||
); | ||
copy_info.unwrap().bytes_in_copy | ||
}; | ||
|
||
let size = wgt::BufferSize::new(size)?; | ||
|
||
let buffer = { | ||
let (resource, allocation) = | ||
super::suballocation::DeviceAllocationContext::from(&*self) | ||
.create_buffer(&crate::BufferDescriptor { | ||
label: None, | ||
size: size.get(), | ||
usage: wgt::BufferUses::COPY_SRC | wgt::BufferUses::COPY_DST, | ||
memory_flags: crate::MemoryFlags::empty(), | ||
}) | ||
.expect(concat!( | ||
"internal error: ", | ||
"failed to allocate intermediate buffer ", | ||
"for offset alignment" | ||
)); | ||
super::Buffer { | ||
resource, | ||
size: size.get(), | ||
allocation, | ||
} | ||
}; | ||
|
||
let mut region = region; | ||
region.buffer_layout.offset = 0; | ||
|
||
unsafe { | ||
self.transition_buffers( | ||
[crate::BufferBarrier { | ||
buffer: &buffer, | ||
usage: crate::StateTransition { | ||
from: wgt::BufferUses::empty(), | ||
to: wgt::BufferUses::COPY_DST, | ||
}, | ||
}] | ||
.into_iter(), | ||
) | ||
}; | ||
|
||
let t = copy_op(self, &buffer, size, region); | ||
|
||
unsafe { | ||
self.transition_buffers( | ||
[crate::BufferBarrier { | ||
buffer: &buffer, | ||
usage: crate::StateTransition { | ||
from: wgt::BufferUses::COPY_DST, | ||
to: wgt::BufferUses::COPY_SRC, | ||
}, | ||
}] | ||
.into_iter(), | ||
) | ||
}; | ||
|
||
Some((t, buffer)) | ||
} | ||
} | ||
|
||
impl crate::CommandEncoder for super::CommandEncoder { | ||
|
@@ -366,6 +438,7 @@ impl crate::CommandEncoder for super::CommandEncoder { | |
Ok(super::CommandBuffer { raw }) | ||
} | ||
unsafe fn reset_all<I: Iterator<Item = super::CommandBuffer>>(&mut self, command_buffers: I) { | ||
self.intermediate_copy_bufs.clear(); | ||
for cmd_buf in command_buffers { | ||
self.free_lists.push(cmd_buf.raw); | ||
} | ||
|
@@ -612,30 +685,61 @@ impl crate::CommandEncoder for super::CommandEncoder { | |
) where | ||
T: Iterator<Item = crate::BufferTextureCopy>, | ||
{ | ||
let list = self.list.as_ref().unwrap(); | ||
for r in regions { | ||
let offset_alignment = self.shared.private_caps.texture_data_placement_alignment(); | ||
|
||
for naive_copy_region in regions { | ||
let is_offset_aligned = naive_copy_region.buffer_layout.offset % offset_alignment == 0; | ||
let (final_copy_region, src) = if is_offset_aligned { | ||
(naive_copy_region, src) | ||
} else { | ||
let Some((intermediate_to_dst_region, intermediate_buf)) = (unsafe { | ||
let src_offset = naive_copy_region.buffer_layout.offset; | ||
self.buf_tex_intermediate( | ||
naive_copy_region, | ||
dst.format, | ||
|this, buf, size, intermediate_to_dst_region| { | ||
let layout = crate::BufferCopy { | ||
src_offset, | ||
dst_offset: 0, | ||
size, | ||
}; | ||
this.copy_buffer_to_buffer(src, buf, [layout].into_iter()); | ||
intermediate_to_dst_region | ||
}, | ||
) | ||
}) else { | ||
continue; | ||
}; | ||
self.intermediate_copy_bufs.push(intermediate_buf); | ||
let intermediate_buf = self.intermediate_copy_bufs.last().unwrap(); | ||
(intermediate_to_dst_region, intermediate_buf) | ||
}; | ||
|
||
let list = self.list.as_ref().unwrap(); | ||
|
||
let src_location = Direct3D12::D3D12_TEXTURE_COPY_LOCATION { | ||
pResource: unsafe { borrow_interface_temporarily(&src.resource) }, | ||
Type: Direct3D12::D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT, | ||
Anonymous: Direct3D12::D3D12_TEXTURE_COPY_LOCATION_0 { | ||
PlacedFootprint: r.to_subresource_footprint(dst.format), | ||
PlacedFootprint: final_copy_region.to_subresource_footprint(dst.format), | ||
}, | ||
}; | ||
let dst_location = Direct3D12::D3D12_TEXTURE_COPY_LOCATION { | ||
pResource: unsafe { borrow_interface_temporarily(&dst.resource) }, | ||
Type: Direct3D12::D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX, | ||
Anonymous: Direct3D12::D3D12_TEXTURE_COPY_LOCATION_0 { | ||
SubresourceIndex: dst.calc_subresource_for_copy(&r.texture_base), | ||
SubresourceIndex: dst | ||
.calc_subresource_for_copy(&final_copy_region.texture_base), | ||
}, | ||
}; | ||
|
||
let src_box = make_box(&wgt::Origin3d::ZERO, &r.size); | ||
let src_box = make_box(&wgt::Origin3d::ZERO, &final_copy_region.size); | ||
unsafe { | ||
list.CopyTextureRegion( | ||
&dst_location, | ||
r.texture_base.origin.x, | ||
r.texture_base.origin.y, | ||
r.texture_base.origin.z, | ||
final_copy_region.texture_base.origin.x, | ||
final_copy_region.texture_base.origin.y, | ||
final_copy_region.texture_base.origin.z, | ||
&src_location, | ||
Some(&src_box), | ||
) | ||
|
@@ -652,8 +756,12 @@ impl crate::CommandEncoder for super::CommandEncoder { | |
) where | ||
T: Iterator<Item = crate::BufferTextureCopy>, | ||
{ | ||
let list = self.list.as_ref().unwrap(); | ||
for r in regions { | ||
let copy_aligned = |this: &mut Self, | ||
src: &super::Texture, | ||
dst: &super::Buffer, | ||
r: crate::BufferTextureCopy| { | ||
let list = this.list.as_ref().unwrap(); | ||
|
||
let src_location = Direct3D12::D3D12_TEXTURE_COPY_LOCATION { | ||
pResource: unsafe { borrow_interface_temporarily(&src.resource) }, | ||
Type: Direct3D12::D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX, | ||
|
@@ -673,6 +781,39 @@ impl crate::CommandEncoder for super::CommandEncoder { | |
unsafe { | ||
list.CopyTextureRegion(&dst_location, 0, 0, 0, &src_location, Some(&src_box)) | ||
}; | ||
}; | ||
|
||
let offset_alignment = self.shared.private_caps.texture_data_placement_alignment(); | ||
|
||
for r in regions { | ||
let is_offset_aligned = r.buffer_layout.offset % offset_alignment == 0; | ||
if is_offset_aligned { | ||
copy_aligned(self, src, dst, r) | ||
} else { | ||
let orig_offset = r.buffer_layout.offset; | ||
let Some((intermediate_to_dst_region, src)) = (unsafe { | ||
self.buf_tex_intermediate( | ||
r, | ||
src.format, | ||
|this, buf, size, intermediate_region| { | ||
copy_aligned(this, src, buf, intermediate_region); | ||
crate::BufferCopy { | ||
src_offset: orig_offset, | ||
dst_offset: 0, | ||
Comment on lines
+801
to
+802
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These seem swapped. |
||
size, | ||
} | ||
}, | ||
) | ||
}) else { | ||
continue; | ||
}; | ||
|
||
unsafe { | ||
self.copy_buffer_to_buffer(&src, dst, [intermediate_to_dst_region].into_iter()); | ||
} | ||
|
||
self.intermediate_copy_bufs.push(src); | ||
}; | ||
} | ||
} | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can
unwrap
here, since it would simplify the code and 0 sized copies shouldn't appear at the hal level.