librashader/librashader-runtime-vk/src/ubo_ring.rs

69 lines
2 KiB
Rust
Raw Normal View History

2023-01-10 14:54:54 +11:00
use crate::error;
use crate::vulkan_primitives::VulkanBuffer;
2022-12-25 17:28:49 +11:00
use ash::vk;
2022-12-29 16:50:48 +11:00
use librashader_runtime::ringbuffer::{BoxRingBuffer, InlineRingBuffer, RingBuffer};
use librashader_runtime::uniforms::UniformStorageAccess;
2022-12-25 17:28:49 +11:00
pub struct VkUboRing {
ring: BoxRingBuffer<VulkanBuffer>,
2022-12-29 16:50:48 +11:00
device: ash::Device,
2022-12-25 17:28:49 +11:00
}
impl VkUboRing {
2023-01-10 14:54:54 +11:00
pub fn new(
device: &ash::Device,
mem_props: &vk::PhysicalDeviceMemoryProperties,
ring_size: usize,
buffer_size: usize,
) -> error::Result<Self> {
2022-12-25 17:28:49 +11:00
let mut ring = Vec::new();
for _ in 0..ring_size {
2023-01-10 14:54:54 +11:00
ring.push(VulkanBuffer::new(
device,
mem_props,
vk::BufferUsageFlags::UNIFORM_BUFFER,
buffer_size,
)?);
2022-12-25 17:28:49 +11:00
}
Ok(VkUboRing {
2022-12-29 16:50:48 +11:00
ring: BoxRingBuffer::from_vec(ring),
2023-01-10 14:54:54 +11:00
device: device.clone(),
2022-12-25 17:28:49 +11:00
})
}
2022-12-29 16:50:48 +11:00
2023-01-10 14:54:54 +11:00
pub fn bind_to_descriptor_set(
&mut self,
descriptor_set: vk::DescriptorSet,
binding: u32,
storage: &impl UniformStorageAccess,
) -> error::Result<()> {
2022-12-29 16:50:48 +11:00
let mut buffer = self.ring.current_mut();
// todo: write directly to allocated buffer.
unsafe {
let mut map = buffer.map()?;
map.copy_from(0, storage.ubo_slice())
}
unsafe {
let buffer_info = vk::DescriptorBufferInfo::builder()
.buffer(buffer.handle)
.offset(0)
.range(storage.ubo_slice().len() as vk::DeviceSize)
.build();
let write_info = vk::WriteDescriptorSet::builder()
.descriptor_type(vk::DescriptorType::UNIFORM_BUFFER)
.dst_set(descriptor_set)
.dst_binding(binding)
.dst_array_element(0)
.buffer_info(&[buffer_info])
.build();
self.device.update_descriptor_sets(&[write_info], &[])
}
self.ring.next();
Ok(())
}
2022-12-25 17:28:49 +11:00
}