2021-02-05 15:38:36 +00:00
|
|
|
// Copyright (c) 2021 The vulkano developers
|
|
|
|
// Licensed under the Apache License, Version 2.0
|
|
|
|
// <LICENSE-APACHE or
|
|
|
|
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT
|
|
|
|
// license <LICENSE-MIT or https://opensource.org/licenses/MIT>,
|
|
|
|
// at your option. All files in the project carrying such
|
|
|
|
// notice may not be copied, modified, or distributed except
|
|
|
|
// according to those terms.
|
|
|
|
|
|
|
|
// This example demonstrates how to use dynamic uniform buffers.
|
|
|
|
//
|
|
|
|
// Dynamic uniform and storage buffers store buffer data for different
|
|
|
|
// calls in one large buffer. Each draw or dispatch call can specify an
|
|
|
|
// offset into the buffer to read object data from, without having to
|
|
|
|
// rebind descriptor sets.
|
|
|
|
|
2022-08-12 10:18:35 +00:00
|
|
|
use std::{iter::repeat, mem::size_of};
|
2022-03-06 19:30:49 +00:00
|
|
|
use vulkano::{
|
|
|
|
buffer::{BufferUsage, CpuAccessibleBuffer},
|
2022-10-05 09:09:26 +00:00
|
|
|
command_buffer::{
|
|
|
|
allocator::StandardCommandBufferAllocator, AutoCommandBufferBuilder, CommandBufferUsage,
|
|
|
|
},
|
2022-03-06 19:30:49 +00:00
|
|
|
descriptor_set::{
|
2022-10-05 09:09:26 +00:00
|
|
|
allocator::StandardDescriptorSetAllocator, layout::DescriptorType, DescriptorSet,
|
|
|
|
PersistentDescriptorSet, WriteDescriptorSet,
|
2022-03-06 19:30:49 +00:00
|
|
|
},
|
|
|
|
device::{
|
2022-09-10 06:00:08 +00:00
|
|
|
physical::PhysicalDeviceType, Device, DeviceCreateInfo, DeviceExtensions, QueueCreateInfo,
|
2022-03-06 19:30:49 +00:00
|
|
|
},
|
2022-07-18 13:11:43 +00:00
|
|
|
instance::{Instance, InstanceCreateInfo},
|
2022-10-26 14:25:01 +00:00
|
|
|
memory::allocator::StandardMemoryAllocator,
|
2022-03-06 19:30:49 +00:00
|
|
|
pipeline::{ComputePipeline, Pipeline, PipelineBindPoint},
|
|
|
|
sync::{self, GpuFuture},
|
2022-07-30 06:53:52 +00:00
|
|
|
VulkanLibrary,
|
2022-03-06 19:30:49 +00:00
|
|
|
};
|
2021-02-05 15:38:36 +00:00
|
|
|
|
|
|
|
fn main() {
|
2022-07-30 06:53:52 +00:00
|
|
|
let library = VulkanLibrary::new().unwrap();
|
|
|
|
let instance = Instance::new(
|
|
|
|
library,
|
|
|
|
InstanceCreateInfo {
|
|
|
|
// Enable enumerating devices that use non-conformant vulkan implementations. (ex. MoltenVK)
|
|
|
|
enumerate_portability: true,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
)
|
2022-07-18 13:11:43 +00:00
|
|
|
.unwrap();
|
2021-02-05 15:38:36 +00:00
|
|
|
|
2021-06-28 06:24:44 +00:00
|
|
|
let device_extensions = DeviceExtensions {
|
|
|
|
khr_storage_buffer_storage_class: true,
|
2022-09-05 20:16:40 +00:00
|
|
|
..DeviceExtensions::empty()
|
2021-06-28 06:24:44 +00:00
|
|
|
};
|
2022-09-10 06:00:08 +00:00
|
|
|
let (physical_device, queue_family_index) = instance
|
|
|
|
.enumerate_physical_devices()
|
|
|
|
.unwrap()
|
|
|
|
.filter(|p| p.supported_extensions().contains(&device_extensions))
|
2021-06-28 06:24:44 +00:00
|
|
|
.filter_map(|p| {
|
2022-09-10 06:00:08 +00:00
|
|
|
p.queue_family_properties()
|
|
|
|
.iter()
|
|
|
|
.position(|q| q.queue_flags.compute)
|
|
|
|
.map(|i| (p, i as u32))
|
2021-06-28 06:24:44 +00:00
|
|
|
})
|
2021-08-09 13:44:58 +00:00
|
|
|
.min_by_key(|(p, _)| match p.properties().device_type {
|
2021-06-28 06:24:44 +00:00
|
|
|
PhysicalDeviceType::DiscreteGpu => 0,
|
|
|
|
PhysicalDeviceType::IntegratedGpu => 1,
|
|
|
|
PhysicalDeviceType::VirtualGpu => 2,
|
|
|
|
PhysicalDeviceType::Cpu => 3,
|
|
|
|
PhysicalDeviceType::Other => 4,
|
2022-09-05 20:16:40 +00:00
|
|
|
_ => 5,
|
2021-06-28 06:24:44 +00:00
|
|
|
})
|
2021-02-05 15:38:36 +00:00
|
|
|
.unwrap();
|
2021-06-28 06:24:44 +00:00
|
|
|
|
|
|
|
println!(
|
|
|
|
"Using device: {} (type: {:?})",
|
2021-08-09 13:44:58 +00:00
|
|
|
physical_device.properties().device_name,
|
|
|
|
physical_device.properties().device_type
|
2021-06-28 06:24:44 +00:00
|
|
|
);
|
|
|
|
|
2021-02-05 15:38:36 +00:00
|
|
|
let (device, mut queues) = Device::new(
|
2021-06-28 06:24:44 +00:00
|
|
|
physical_device,
|
2022-02-14 09:32:27 +00:00
|
|
|
DeviceCreateInfo {
|
2022-07-18 13:11:43 +00:00
|
|
|
enabled_extensions: device_extensions,
|
2022-09-10 06:00:08 +00:00
|
|
|
queue_create_infos: vec![QueueCreateInfo {
|
|
|
|
queue_family_index,
|
|
|
|
..Default::default()
|
|
|
|
}],
|
2022-02-14 09:32:27 +00:00
|
|
|
..Default::default()
|
|
|
|
},
|
2021-02-05 15:38:36 +00:00
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let queue = queues.next().unwrap();
|
|
|
|
|
|
|
|
mod shader {
|
|
|
|
vulkano_shaders::shader! {
|
|
|
|
ty: "compute",
|
|
|
|
src: "
|
|
|
|
#version 450
|
|
|
|
|
|
|
|
layout(local_size_x = 12) in;
|
|
|
|
|
|
|
|
// Uniform Buffer Object
|
2022-01-11 02:06:22 +00:00
|
|
|
layout(set = 0, binding = 0) uniform InData {
|
2021-02-05 15:38:36 +00:00
|
|
|
uint data;
|
|
|
|
} ubo;
|
|
|
|
|
|
|
|
// Output Buffer
|
|
|
|
layout(set = 0, binding = 1) buffer OutData {
|
|
|
|
uint data[];
|
|
|
|
} data;
|
|
|
|
|
|
|
|
// Toy shader that only runs for the index specified in `ubo`.
|
|
|
|
void main() {
|
|
|
|
uint index = gl_GlobalInvocationID.x;
|
|
|
|
if(index == ubo.data) {
|
|
|
|
data.data[index] = index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-13 15:06:16 +00:00
|
|
|
let shader = shader::load(device.clone()).unwrap();
|
2021-11-02 20:33:58 +00:00
|
|
|
let pipeline = ComputePipeline::new(
|
|
|
|
device.clone(),
|
2021-11-13 15:06:16 +00:00
|
|
|
shader.entry_point("main").unwrap(),
|
2021-11-02 20:33:58 +00:00
|
|
|
&(),
|
|
|
|
None,
|
2022-02-25 22:52:44 +00:00
|
|
|
|layout_create_infos| {
|
|
|
|
let binding = layout_create_infos[0].bindings.get_mut(&0).unwrap();
|
|
|
|
binding.descriptor_type = DescriptorType::UniformBufferDynamic;
|
2021-11-02 20:33:58 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
.unwrap();
|
2021-02-05 15:38:36 +00:00
|
|
|
|
2022-10-26 14:25:01 +00:00
|
|
|
let memory_allocator = StandardMemoryAllocator::new_default(device.clone());
|
2022-10-05 09:09:26 +00:00
|
|
|
let descriptor_set_allocator = StandardDescriptorSetAllocator::new(device.clone());
|
2022-10-29 17:34:07 +00:00
|
|
|
let command_buffer_allocator =
|
|
|
|
StandardCommandBufferAllocator::new(device.clone(), Default::default());
|
2022-10-05 09:09:26 +00:00
|
|
|
|
2021-02-05 15:38:36 +00:00
|
|
|
// Declare input buffer.
|
|
|
|
// Data in a dynamic buffer **MUST** be aligned to min_uniform_buffer_offset_align
|
|
|
|
// or min_storage_buffer_offset_align, depending on the type of buffer.
|
|
|
|
let data: Vec<u8> = vec![3, 11, 7];
|
|
|
|
let min_dynamic_align = device
|
|
|
|
.physical_device()
|
2021-06-13 19:47:10 +00:00
|
|
|
.properties()
|
2021-08-09 13:44:58 +00:00
|
|
|
.min_uniform_buffer_offset_alignment as usize;
|
2021-02-05 15:38:36 +00:00
|
|
|
println!(
|
|
|
|
"Minimum uniform buffer offset alignment: {}",
|
|
|
|
min_dynamic_align
|
|
|
|
);
|
|
|
|
println!("Input: {:?}", data);
|
|
|
|
// Round size up to the next multiple of align.
|
2022-03-06 19:30:49 +00:00
|
|
|
let align = (size_of::<u32>() + min_dynamic_align - 1) & !(min_dynamic_align - 1);
|
2021-02-05 15:38:36 +00:00
|
|
|
let aligned_data = {
|
|
|
|
let mut aligned_data = Vec::with_capacity(align * data.len());
|
2022-08-12 10:18:35 +00:00
|
|
|
for elem in data {
|
|
|
|
let bytes = elem.to_ne_bytes();
|
2021-02-05 15:38:36 +00:00
|
|
|
// Fill up the buffer with data
|
2022-08-12 10:18:35 +00:00
|
|
|
for b in bytes {
|
|
|
|
aligned_data.push(b);
|
2021-02-05 15:38:36 +00:00
|
|
|
}
|
|
|
|
// Zero out any padding needed for alignment
|
2022-08-12 10:18:35 +00:00
|
|
|
aligned_data.extend(repeat(0).take(align - bytes.len()));
|
2021-02-05 15:38:36 +00:00
|
|
|
}
|
|
|
|
aligned_data
|
|
|
|
};
|
|
|
|
|
|
|
|
let input_buffer = CpuAccessibleBuffer::from_iter(
|
2022-10-26 14:25:01 +00:00
|
|
|
&memory_allocator,
|
2022-09-05 20:16:40 +00:00
|
|
|
BufferUsage {
|
|
|
|
uniform_buffer: true,
|
|
|
|
..BufferUsage::empty()
|
|
|
|
},
|
2021-02-05 15:38:36 +00:00
|
|
|
false,
|
|
|
|
aligned_data.into_iter(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let output_buffer = CpuAccessibleBuffer::from_iter(
|
2022-10-26 14:25:01 +00:00
|
|
|
&memory_allocator,
|
2022-09-05 20:16:40 +00:00
|
|
|
BufferUsage {
|
|
|
|
storage_buffer: true,
|
|
|
|
..BufferUsage::empty()
|
|
|
|
},
|
2021-02-05 15:38:36 +00:00
|
|
|
false,
|
|
|
|
(0..12).map(|_| 0u32),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
2022-02-25 22:52:44 +00:00
|
|
|
let layout = pipeline.layout().set_layouts().get(0).unwrap();
|
2021-12-18 10:32:38 +00:00
|
|
|
let set = PersistentDescriptorSet::new(
|
2022-10-05 09:09:26 +00:00
|
|
|
&descriptor_set_allocator,
|
2021-12-18 10:32:38 +00:00
|
|
|
layout.clone(),
|
|
|
|
[
|
2022-08-12 10:18:35 +00:00
|
|
|
WriteDescriptorSet::buffer(0, input_buffer),
|
2021-12-18 10:32:38 +00:00
|
|
|
WriteDescriptorSet::buffer(1, output_buffer.clone()),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
.unwrap();
|
2021-02-05 15:38:36 +00:00
|
|
|
|
|
|
|
// Build the command buffer, using different offsets for each call.
|
2021-04-26 14:53:18 +00:00
|
|
|
let mut builder = AutoCommandBufferBuilder::primary(
|
2022-10-05 09:09:26 +00:00
|
|
|
&command_buffer_allocator,
|
2022-09-10 06:00:08 +00:00
|
|
|
queue.queue_family_index(),
|
2021-04-26 14:53:18 +00:00
|
|
|
CommandBufferUsage::OneTimeSubmit,
|
|
|
|
)
|
|
|
|
.unwrap();
|
2022-08-12 10:18:35 +00:00
|
|
|
|
|
|
|
#[allow(clippy::erasing_op, clippy::identity_op)]
|
2021-02-05 15:38:36 +00:00
|
|
|
builder
|
2021-08-27 06:24:16 +00:00
|
|
|
.bind_pipeline_compute(pipeline.clone())
|
|
|
|
.bind_descriptor_sets(
|
|
|
|
PipelineBindPoint::Compute,
|
|
|
|
pipeline.layout().clone(),
|
|
|
|
0,
|
2021-07-20 17:06:22 +00:00
|
|
|
set.clone().offsets([0 * align as u32]),
|
2021-02-05 15:38:36 +00:00
|
|
|
)
|
2021-08-27 06:24:16 +00:00
|
|
|
.dispatch([12, 1, 1])
|
2021-02-05 15:38:36 +00:00
|
|
|
.unwrap()
|
2021-08-27 06:24:16 +00:00
|
|
|
.bind_descriptor_sets(
|
|
|
|
PipelineBindPoint::Compute,
|
|
|
|
pipeline.layout().clone(),
|
|
|
|
0,
|
2021-07-20 17:06:22 +00:00
|
|
|
set.clone().offsets([1 * align as u32]),
|
2021-02-05 15:38:36 +00:00
|
|
|
)
|
2021-08-27 06:24:16 +00:00
|
|
|
.dispatch([12, 1, 1])
|
2021-02-05 15:38:36 +00:00
|
|
|
.unwrap()
|
2021-08-27 06:24:16 +00:00
|
|
|
.bind_descriptor_sets(
|
|
|
|
PipelineBindPoint::Compute,
|
|
|
|
pipeline.layout().clone(),
|
|
|
|
0,
|
2022-08-12 10:18:35 +00:00
|
|
|
set.offsets([2 * align as u32]),
|
2021-02-05 15:38:36 +00:00
|
|
|
)
|
2021-08-27 06:24:16 +00:00
|
|
|
.dispatch([12, 1, 1])
|
2021-02-05 15:38:36 +00:00
|
|
|
.unwrap();
|
|
|
|
let command_buffer = builder.build().unwrap();
|
|
|
|
|
2022-08-12 10:18:35 +00:00
|
|
|
let future = sync::now(device)
|
|
|
|
.then_execute(queue, command_buffer)
|
2021-02-05 15:38:36 +00:00
|
|
|
.unwrap()
|
|
|
|
.then_signal_fence_and_flush()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
future.wait(None).unwrap();
|
|
|
|
|
|
|
|
let output_content = output_buffer.read().unwrap();
|
2022-03-06 19:30:49 +00:00
|
|
|
println!("Output: {:?}", &*output_content);
|
2021-02-05 15:38:36 +00:00
|
|
|
}
|