-
Notifications
You must be signed in to change notification settings - Fork 61
Add support for presser
#138
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
127ffee
Add `presser` support for vulkan Allocation
fu5ha d3db3bb
ignore tmp dir
fu5ha 2b5e060
fmt + slight doc update
fu5ha 75e2e8c
user presser in imgui example renderer
fu5ha 293c3d9
make it prettier and add presser to dev-dependencies
fu5ha be71ac2
fix example code a bit
fu5ha 1b577ca
add presser feature to ci
fu5ha 2c6e4b9
Update src/vulkan/presser.rs
fu5ha b19754a
> to greater than
fu5ha 7575efa
add "presser" to vulkan-visualization required features
fu5ha 8376990
remove ```rust
fu5ha 710fa71
make presser not optional
fu5ha df1894d
remove redundant presser mod
fu5ha bea8b3b
impl Slab on Allocation directly
fu5ha 4a26a32
revert gitignore
fu5ha 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 |
---|---|---|
@@ -0,0 +1,83 @@ | ||
use super::Allocation; | ||
use core::convert::TryFrom; | ||
|
||
impl Allocation { | ||
/// Borrow the CPU-mapped memory that backs this allocation as a [`presser::Slab`], which you can then | ||
/// use to safely copy data into the raw, potentially-uninitialized buffer. | ||
/// | ||
/// # Example | ||
/// | ||
/// ```rust,ignore | ||
fu5ha marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
/// #[repr(C, align(16))] | ||
/// #[derive(Clone, Copy)] | ||
/// struct MyGpuVector { | ||
/// x: f32, | ||
/// y: f32, | ||
/// z: f32, | ||
/// } | ||
/// | ||
/// // Create some data to be sent to the GPU. Note this must be formatted correctly in terms of | ||
/// // alignment of individual items and etc, as usual. | ||
/// let my_gpu_data: &[MyGpuVector] = get_vertex_data(); | ||
/// | ||
/// // Get a `presser::Slab` from our gpu_allocator::Allocation | ||
/// let alloc_slab = my_allocation.as_mapped_slab(); | ||
/// | ||
/// // depending on the type of data you're copying, your vulkan device may have a minimum | ||
/// // alignment requirement for that data | ||
/// let min_gpu_align = my_vulkan_device_specifications.min_alignment_thing; | ||
/// | ||
/// let copy_record = presser::copy_from_slice_to_offset_with_align( | ||
/// my_gpu_data, | ||
/// alloc_slab, | ||
/// 0, // start as close to the beginning of the allocation as possible | ||
/// min_gpu_align, | ||
/// ); | ||
/// | ||
/// // the data may not have actually been copied starting at the requested start offset | ||
/// // depending on the alignment of the underlying allocation, as well as the alignment requirements of | ||
/// // `MyGpuVector` and the `min_gpu_align` we passed in | ||
/// let actual_data_start_offset = copy_record.copy_start_offset; | ||
/// ``` | ||
/// | ||
/// # Safety | ||
/// | ||
/// This is technically not fully safe because we can't validate that the | ||
/// GPU is not using the data in the buffer while `self` is borrowed, however trying | ||
/// to validate this statically is really hard and the community has basically decided | ||
/// that just calling stuff like this is fine. So, as would always be the case, ensure the GPU | ||
/// is not using the data in `self` before calling this function. | ||
pub fn as_mapped_slab(&mut self) -> Option<MappedAllocationSlab<'_>> { | ||
let mapped_ptr = self.mapped_ptr()?.cast().as_ptr(); | ||
// size > isize::MAX is disallowed by `Slab` for safety reasons | ||
let size = isize::try_from(self.size()).ok()?; | ||
// this will always succeed since size can only be positive and < isize::MAX | ||
let size = size as usize; | ||
|
||
Some(MappedAllocationSlab { _borrowed_alloc: self, mapped_ptr, size }) | ||
} | ||
} | ||
|
||
/// A wrapper struct over a borrowed [`Allocation`] that implements [`presser::Slab`]. | ||
/// | ||
/// This type should be acquired by calling [`Allocation::as_mapped_slab`]. | ||
pub struct MappedAllocationSlab<'a> { | ||
_borrowed_alloc: &'a mut Allocation, | ||
mapped_ptr: *mut u8, | ||
size: usize, | ||
} | ||
|
||
// SAFETY: See the safety comment of Allocation::as_mapped_slab above. | ||
unsafe impl<'a> presser::Slab for MappedAllocationSlab<'a> { | ||
fn base_ptr(&self) -> *const u8 { | ||
self.mapped_ptr | ||
} | ||
|
||
fn base_ptr_mut(&mut self) -> *mut u8 { | ||
self.mapped_ptr | ||
} | ||
|
||
fn size(&self) -> usize { | ||
self.size | ||
} | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.