Skip to content
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

Heap-less descriptor sets in Metal #2183

Merged
merged 5 commits into from
Jun 28, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/backend/auxil/range_alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ where
pub fn new(range: Range<T>) -> Self {
RangeAllocator {
initial_range: range.clone(),
free_ranges: vec![range.clone()],
free_ranges: vec![range],
}
}

pub fn allocate_range(&mut self, length: T) -> Option<Range<T>> {
assert_ne!(length + length, length);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the purpose of this assertion? Seems it's just checking for zero?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, but T doesn't have a Zero bound :)

let mut best_fit: Option<(usize, Range<T>)> = None;
for (index, range) in self.free_ranges.iter().cloned().enumerate() {
let range_length = range.end - range.start;
Expand Down
70 changes: 44 additions & 26 deletions src/backend/metal/src/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,29 +322,47 @@ impl hal::DescriptorPool<Backend> for DescriptorPool {

// step[2]: try to allocate the ranges from the pool
let mut inner = pool_inner.write().unwrap();
let sampler_range = match inner.sampler_alloc.allocate_range(total_samplers as _) {
Some(range) => range,
None => {
warn!("Not enough samplers for {}", total_samplers);
return Err(pso::AllocationError::FragmentedPool);
let sampler_range = if total_samplers != 0 {
match inner.sampler_alloc.allocate_range(total_samplers as _) {
Some(range) => range,
None => {
warn!("Not enough samplers for {}", total_samplers);
return Err(pso::AllocationError::FragmentedPool);
}
}
} else {
0 .. 0
};
let texture_range = match inner.texture_alloc.allocate_range(total_textures as _) {
Some(range) => range,
None => {
inner.sampler_alloc.free_range(sampler_range);
warn!("Not enough images for {}", total_textures);
return Err(pso::AllocationError::FragmentedPool);
let texture_range = if total_textures != 0 {
match inner.texture_alloc.allocate_range(total_textures as _) {
Some(range) => range,
None => {
if sampler_range.end != 0 {
inner.sampler_alloc.free_range(sampler_range);
}
warn!("Not enough images for {}", total_textures);
return Err(pso::AllocationError::FragmentedPool);
}
}
} else {
0 .. 0
};
let buffer_range = match inner.buffer_alloc.allocate_range(total_buffers as _) {
Some(range) => range,
None => {
inner.sampler_alloc.free_range(sampler_range);
inner.texture_alloc.free_range(texture_range);
warn!("Not enough buffers for {}", total_buffers);
return Err(pso::AllocationError::FragmentedPool);
let buffer_range = if total_buffers != 0 {
match inner.buffer_alloc.allocate_range(total_buffers as _) {
Some(range) => range,
None => {
if sampler_range.end != 0 {
inner.sampler_alloc.free_range(sampler_range);
}
if texture_range.end != 0 {
inner.texture_alloc.free_range(texture_range);
}
warn!("Not enough buffers for {}", total_buffers);
return Err(pso::AllocationError::FragmentedPool);
}
}
} else {
0 .. 0
};

// step[3]: fill out immutable samplers
Expand Down Expand Up @@ -399,25 +417,25 @@ impl hal::DescriptorPool<Backend> for DescriptorPool {
let mut inner = pool_inner.write().unwrap();
for descriptor_set in descriptor_sets {
match descriptor_set {
DescriptorSet::Emulated { ref sampler_range, ref texture_range, ref buffer_range, .. } => {
if sampler_range.start != sampler_range.end {
inner.sampler_alloc.free_range(sampler_range.clone());
}
DescriptorSet::Emulated { sampler_range, texture_range, buffer_range, .. } => {
for sampler in &mut inner.samplers[sampler_range.start as usize .. sampler_range.end as usize] {
*sampler = None;
}
if texture_range.start != texture_range.end {
inner.texture_alloc.free_range(texture_range.clone());
if sampler_range.start != sampler_range.end {
inner.sampler_alloc.free_range(sampler_range);
}
for image in &mut inner.textures[texture_range.start as usize .. texture_range.end as usize] {
*image = None;
}
if buffer_range.start != buffer_range.end {
inner.buffer_alloc.free_range(buffer_range.clone());
if texture_range.start != texture_range.end {
inner.texture_alloc.free_range(texture_range);
}
for buffer in &mut inner.buffers[buffer_range.start as usize .. buffer_range.end as usize] {
buffer.base = None;
}
if buffer_range.start != buffer_range.end {
inner.buffer_alloc.free_range(buffer_range);
}
}
DescriptorSet::ArgumentBuffer{..} => {
panic!("Tried to free a DescriptorSet not given out by this DescriptorPool!")
Expand Down