Skip to content

Commit

Permalink
[mtl] dynamic buffers
Browse files Browse the repository at this point in the history
  • Loading branch information
kvark committed Jun 5, 2018
1 parent 191ce46 commit b61f215
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 24 deletions.
61 changes: 44 additions & 17 deletions src/backend/metal/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2008,18 +2008,22 @@ impl com::RawCommandBuffer<Backend> for CommandBuffer {
.pre_render_commands(commands);
}

fn bind_graphics_descriptor_sets<'a, T>(
fn bind_graphics_descriptor_sets<'a, I, J>(
&mut self,
layout: &native::PipelineLayout,
first_set: usize,
sets: T,
sets: I,
offsets: J,
) where
T: IntoIterator,
T::Item: Borrow<native::DescriptorSet>,
I: IntoIterator,
I::Item: Borrow<native::DescriptorSet>,
J: IntoIterator,
J::Item: Borrow<com::DescriptorSetOffset>,
{
use spirv_cross::{msl, spirv};

let mut commands = Vec::new(); //TODO: re-use the storage
let mut offset_iter = offsets.into_iter();

for (set_index, desc_set) in sets.into_iter().enumerate() {
let location_vs = msl::ResourceBindingLocation {
Expand Down Expand Up @@ -2089,8 +2093,17 @@ impl com::RawCommandBuffer<Backend> for CommandBuffer {
Buffer(ref buffers) => {
let start = res.buffer_id as usize;
for (i, bref) in buffers.iter().enumerate() {
let (buffer, offset) = match *bref {
Some((ref buffer, offset)) => (Some(buffer.clone()), offset),
let (buffer, offset) = match bref.base {
Some((ref buffer, mut offset)) => {
if bref.dynamic {
offset += *offset_iter
.next()
.expect("No dynamic offset provided!")
.borrow() as u64;
}
resources.add_buffer(start + i, buffer.as_ref(), offset as _);
(Some(buffer.clone()), offset)
}
None => (None, 0),
};
commands.push(soft::RenderCommand::BindBuffer {
Expand All @@ -2099,9 +2112,6 @@ impl com::RawCommandBuffer<Backend> for CommandBuffer {
buffer,
offset,
});
if let Some((ref buffer, offset)) = *bref {
resources.add_buffer(start + i, buffer.as_ref(), offset as _);
}
}
}
}
Expand Down Expand Up @@ -2156,8 +2166,14 @@ impl com::RawCommandBuffer<Backend> for CommandBuffer {
Buffer(ref buffers) => {
let start = res.buffer_id as usize;
for (i, bref) in buffers.iter().enumerate() {
let (buffer, offset) = match *bref {
Some((ref buffer, offset)) => {
let (buffer, offset) = match bref.base {
Some((ref buffer, mut offset)) => {
if bref.dynamic {
offset += *offset_iter
.next()
.expect("No dynamic offset provided!")
.borrow() as u64;
}
resources.add_buffer(start + i, buffer.as_ref(), offset as _);
(Some(buffer.clone()), offset)
},
Expand Down Expand Up @@ -2218,17 +2234,22 @@ impl com::RawCommandBuffer<Backend> for CommandBuffer {
.pre_compute_commands(iter::once(command));
}

fn bind_compute_descriptor_sets<'a, T>(
fn bind_compute_descriptor_sets<'a, I, J>(
&mut self,
layout: &native::PipelineLayout,
first_set: usize,
sets: T,
sets: I,
offsets: J,
) where
T: IntoIterator,
T::Item: Borrow<native::DescriptorSet>,
I: IntoIterator,
I::Item: Borrow<native::DescriptorSet>,
J: IntoIterator,
J::Item: Borrow<com::DescriptorSetOffset>,
{
use spirv_cross::{msl, spirv};

let mut commands = Vec::new();
let mut offset_iter = offsets.into_iter();

for (set_index, desc_set) in sets.into_iter().enumerate() {
let resources = &mut self.state.resources_cs;
Expand Down Expand Up @@ -2289,8 +2310,14 @@ impl com::RawCommandBuffer<Backend> for CommandBuffer {
Buffer(ref buffers) => {
let start = res.buffer_id as usize;
for (i, bref) in buffers.iter().enumerate() {
let (buffer, offset) = match *bref {
Some((ref buffer, offset)) => {
let (buffer, offset) = match bref.base {
Some((ref buffer, mut offset)) => {
if bref.dynamic {
offset += *offset_iter
.next()
.expect("No dynamic offset provided!")
.borrow() as u64;
}
resources.add_buffer(start + i, buffer.as_ref(), offset as _);
(Some(buffer.clone()), offset)
},
Expand Down
8 changes: 4 additions & 4 deletions src/backend/metal/src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,9 @@ impl hal::Device<Backend> for Device {
};
match set_binding.ty {
pso::DescriptorType::UniformBuffer |
pso::DescriptorType::StorageBuffer => {
pso::DescriptorType::StorageBuffer |
pso::DescriptorType::UniformBufferDynamic |
pso::DescriptorType::StorageBufferDynamic => {
res.buffer_id = counters.buffers as _;
counters.buffers += 1;
}
Expand All @@ -705,8 +707,6 @@ impl hal::Device<Backend> for Device {
counters.textures += 1;
counters.samplers += 1;
}
pso::DescriptorType::UniformBufferDynamic |
pso::DescriptorType::UniformImageDynamic => unimplemented!(),
};
assert_eq!(set_binding.count, 1); //TODO
let location = msl::ResourceBindingLocation {
Expand Down Expand Up @@ -1378,7 +1378,7 @@ impl hal::Device<Backend> for Device {
let start = range.start.unwrap_or(0);
let end = range.end.unwrap_or(buf_length);
assert!(end <= buf_length);
vec[array_offset] = Some((buffer.raw.clone(), start));
vec[array_offset].base = Some((buffer.raw.clone(), start));
}
(&pso::Descriptor::Sampler(..), _) |
(&pso::Descriptor::Image(..), _) |
Expand Down
14 changes: 11 additions & 3 deletions src/backend/metal/src/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,10 +238,12 @@ impl hal::DescriptorPool<Backend> for DescriptorPool {
}
pso::DescriptorType::UniformBuffer |
pso::DescriptorType::StorageBuffer => {
DescriptorSetBinding::Buffer(vec![None; layout.count])
DescriptorSetBinding::Buffer(vec![BufferBinding { base: None, dynamic: false }; layout.count])
}
pso::DescriptorType::UniformBufferDynamic |
pso::DescriptorType::UniformImageDynamic => unimplemented!()
pso::DescriptorType::StorageBufferDynamic => {
DescriptorSetBinding::Buffer(vec![BufferBinding { base: None, dynamic: true }; layout.count])
}
};
(layout.binding, binding)
}).collect();
Expand Down Expand Up @@ -336,12 +338,18 @@ pub struct DescriptorSetInner {
}
unsafe impl Send for DescriptorSetInner {}

#[derive(Clone, Debug)]
pub struct BufferBinding {
pub base: Option<(metal::Buffer, u64)>,
pub dynamic: bool,
}

#[derive(Debug)]
pub enum DescriptorSetBinding {
Sampler(Vec<Option<metal::SamplerState>>),
Image(Vec<Option<(metal::Texture, image::Layout)>>),
Combined(Vec<(Option<(metal::Texture, image::Layout)>, Option<metal::SamplerState>)>),
Buffer(Vec<Option<(metal::Buffer, u64)>>),
Buffer(Vec<BufferBinding>),
//InputAttachment(Vec<(metal::Texture, image::Layout)>),
}

Expand Down

0 comments on commit b61f215

Please sign in to comment.