2017-06-20 13:07:21 +00:00
|
|
|
// This example demonstrates how to use the compute capabilities of Vulkan.
|
|
|
|
//
|
2017-07-04 15:30:17 +00:00
|
|
|
// While graphics cards have traditionally been used for graphical operations, over time they have
|
2017-06-20 13:07:21 +00:00
|
|
|
// been more or more used for general-purpose operations as well. This is called "General-Purpose
|
|
|
|
// GPU", or *GPGPU*. This is what this example demonstrates.
|
|
|
|
|
2023-09-03 11:09:07 +00:00
|
|
|
use std::sync::Arc;
|
2022-03-06 19:30:49 +00:00
|
|
|
use vulkano::{
|
2023-04-02 13:07:16 +00:00
|
|
|
buffer::{Buffer, BufferCreateInfo, BufferUsage},
|
2022-10-05 09:09:26 +00:00
|
|
|
command_buffer::{
|
2023-12-20 12:04:01 +00:00
|
|
|
allocator::StandardCommandBufferAllocator, CommandBufferBeginInfo, CommandBufferLevel,
|
|
|
|
CommandBufferUsage, RecordingCommandBuffer,
|
2022-10-05 09:09:26 +00:00
|
|
|
},
|
|
|
|
descriptor_set::{
|
2023-11-12 16:17:37 +00:00
|
|
|
allocator::StandardDescriptorSetAllocator, DescriptorSet, WriteDescriptorSet,
|
2022-10-05 09:09:26 +00:00
|
|
|
},
|
2022-03-06 19:30:49 +00:00
|
|
|
device::{
|
2022-09-10 06:00:08 +00:00
|
|
|
physical::PhysicalDeviceType, Device, DeviceCreateInfo, DeviceExtensions, QueueCreateInfo,
|
2022-11-05 05:02:28 +00:00
|
|
|
QueueFlags,
|
2022-03-06 19:30:49 +00:00
|
|
|
},
|
2023-06-17 20:18:48 +00:00
|
|
|
instance::{Instance, InstanceCreateFlags, InstanceCreateInfo},
|
2023-07-19 09:11:17 +00:00
|
|
|
memory::allocator::{AllocationCreateInfo, MemoryTypeFilter, StandardMemoryAllocator},
|
2023-04-22 18:46:22 +00:00
|
|
|
pipeline::{
|
|
|
|
compute::ComputePipelineCreateInfo, layout::PipelineDescriptorSetLayoutCreateInfo,
|
|
|
|
ComputePipeline, Pipeline, PipelineBindPoint, PipelineLayout,
|
2023-06-25 18:08:27 +00:00
|
|
|
PipelineShaderStageCreateInfo,
|
2023-04-22 18:46:22 +00:00
|
|
|
},
|
2022-03-06 19:30:49 +00:00
|
|
|
sync::{self, GpuFuture},
|
2022-07-30 06:53:52 +00:00
|
|
|
VulkanLibrary,
|
2022-03-06 19:30:49 +00:00
|
|
|
};
|
2017-06-20 13:07:21 +00:00
|
|
|
|
|
|
|
fn main() {
|
|
|
|
// As with other examples, the first step is to create an instance.
|
2022-07-30 06:53:52 +00:00
|
|
|
let library = VulkanLibrary::new().unwrap();
|
|
|
|
let instance = Instance::new(
|
|
|
|
library,
|
|
|
|
InstanceCreateInfo {
|
2023-06-17 20:18:48 +00:00
|
|
|
flags: InstanceCreateFlags::ENUMERATE_PORTABILITY,
|
2022-07-30 06:53:52 +00:00
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
)
|
2022-07-18 13:11:43 +00:00
|
|
|
.unwrap();
|
2017-06-20 13:07:21 +00:00
|
|
|
|
|
|
|
// Choose which physical device to use.
|
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| {
|
2023-03-05 18:56:35 +00:00
|
|
|
// The Vulkan specs guarantee that a compliant implementation must provide at least one
|
|
|
|
// queue that supports compute operations.
|
2022-09-10 06:00:08 +00:00
|
|
|
p.queue_family_properties()
|
|
|
|
.iter()
|
2022-11-05 05:02:28 +00:00
|
|
|
.position(|q| q.queue_flags.intersects(QueueFlags::COMPUTE))
|
2022-09-10 06:00:08 +00:00
|
|
|
.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
|
|
|
})
|
2020-05-10 00:36:20 +00:00
|
|
|
.unwrap();
|
2017-06-20 13:07:21 +00:00
|
|
|
|
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,
|
2023-03-05 18:56:35 +00:00
|
|
|
physical_device.properties().device_type,
|
2021-06-28 06:24:44 +00:00
|
|
|
);
|
|
|
|
|
2017-06-20 13:07:21 +00:00
|
|
|
// Now initializing the device.
|
2020-05-10 00:36:20 +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()
|
|
|
|
},
|
2020-05-10 00:36:20 +00:00
|
|
|
)
|
|
|
|
.unwrap();
|
2017-06-20 13:07:21 +00:00
|
|
|
|
|
|
|
// Since we can request multiple queues, the `queues` variable is in fact an iterator. In this
|
2018-09-02 04:18:22 +00:00
|
|
|
// example we use only one queue, so we just retrieve the first and only element of the
|
2017-06-20 13:07:21 +00:00
|
|
|
// iterator and throw it away.
|
|
|
|
let queue = queues.next().unwrap();
|
|
|
|
|
|
|
|
// Now let's get to the actual example.
|
|
|
|
//
|
2023-03-05 18:56:35 +00:00
|
|
|
// What we are going to do is very basic: we are going to fill a buffer with 64k integers and
|
|
|
|
// ask the GPU to multiply each of them by 12.
|
2017-06-20 13:07:21 +00:00
|
|
|
//
|
|
|
|
// GPUs are very good at parallel computations (SIMD-like operations), and thus will do this
|
|
|
|
// much more quickly than a CPU would do. While a CPU would typically multiply them one by one
|
|
|
|
// or four by four, a GPU will do it by groups of 32 or 64.
|
|
|
|
//
|
2023-03-05 18:56:35 +00:00
|
|
|
// Note however that in a real-life situation for such a simple operation the cost of accessing
|
|
|
|
// memory usually outweighs the benefits of a faster calculation. Since both the CPU and the
|
|
|
|
// GPU will need to access data, there is no other choice but to transfer the data through the
|
|
|
|
// slow PCI express bus.
|
2017-06-20 13:07:21 +00:00
|
|
|
|
|
|
|
// We need to create the compute pipeline that describes our operation.
|
|
|
|
//
|
|
|
|
// If you are familiar with graphics pipeline, the principle is the same except that compute
|
2018-10-11 00:42:56 +00:00
|
|
|
// pipelines are much simpler to create.
|
2021-11-02 20:33:58 +00:00
|
|
|
let pipeline = {
|
2018-10-28 07:29:41 +00:00
|
|
|
mod cs {
|
2020-05-10 00:36:20 +00:00
|
|
|
vulkano_shaders::shader! {
|
2018-10-28 07:29:41 +00:00
|
|
|
ty: "compute",
|
2023-03-05 18:56:35 +00:00
|
|
|
src: r"
|
2020-01-23 07:37:12 +00:00
|
|
|
#version 450
|
2018-10-28 07:29:41 +00:00
|
|
|
|
2020-01-23 07:37:12 +00:00
|
|
|
layout(local_size_x = 64, local_size_y = 1, local_size_z = 1) in;
|
2018-10-28 07:29:41 +00:00
|
|
|
|
2020-01-23 07:37:12 +00:00
|
|
|
layout(set = 0, binding = 0) buffer Data {
|
|
|
|
uint data[];
|
2023-03-05 18:56:35 +00:00
|
|
|
};
|
2018-10-28 07:29:41 +00:00
|
|
|
|
2020-01-23 07:37:12 +00:00
|
|
|
void main() {
|
|
|
|
uint idx = gl_GlobalInvocationID.x;
|
2023-03-05 18:56:35 +00:00
|
|
|
data[idx] *= 12;
|
2020-01-23 07:37:12 +00:00
|
|
|
}
|
2023-03-05 18:56:35 +00:00
|
|
|
",
|
2018-10-28 07:29:41 +00:00
|
|
|
}
|
|
|
|
}
|
2023-04-22 18:46:22 +00:00
|
|
|
let cs = cs::load(device.clone())
|
2023-04-18 18:53:08 +00:00
|
|
|
.unwrap()
|
|
|
|
.entry_point("main")
|
|
|
|
.unwrap();
|
2023-06-25 18:08:27 +00:00
|
|
|
let stage = PipelineShaderStageCreateInfo::new(cs);
|
2023-04-22 18:46:22 +00:00
|
|
|
let layout = PipelineLayout::new(
|
|
|
|
device.clone(),
|
|
|
|
PipelineDescriptorSetLayoutCreateInfo::from_stages([&stage])
|
|
|
|
.into_pipeline_layout_create_info(device.clone())
|
|
|
|
.unwrap(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
2021-09-04 04:20:05 +00:00
|
|
|
ComputePipeline::new(
|
|
|
|
device.clone(),
|
|
|
|
None,
|
2023-04-22 18:46:22 +00:00
|
|
|
ComputePipelineCreateInfo::stage_layout(stage, layout),
|
2021-09-04 04:20:05 +00:00
|
|
|
)
|
|
|
|
.unwrap()
|
2021-11-02 20:33:58 +00:00
|
|
|
};
|
2017-06-20 13:07:21 +00:00
|
|
|
|
2023-09-03 11:09:07 +00:00
|
|
|
let memory_allocator = Arc::new(StandardMemoryAllocator::new_default(device.clone()));
|
2023-11-12 14:10:22 +00:00
|
|
|
let descriptor_set_allocator = Arc::new(StandardDescriptorSetAllocator::new(
|
|
|
|
device.clone(),
|
|
|
|
Default::default(),
|
|
|
|
));
|
2023-11-14 16:57:43 +00:00
|
|
|
let command_buffer_allocator = Arc::new(StandardCommandBufferAllocator::new(
|
|
|
|
device.clone(),
|
|
|
|
Default::default(),
|
|
|
|
));
|
2022-10-05 09:09:26 +00:00
|
|
|
|
2017-06-20 13:07:21 +00:00
|
|
|
// We start by creating the buffer that will store the data.
|
2023-03-05 18:56:35 +00:00
|
|
|
let data_buffer = Buffer::from_iter(
|
2023-09-03 11:09:07 +00:00
|
|
|
memory_allocator,
|
2023-04-02 13:07:16 +00:00
|
|
|
BufferCreateInfo {
|
|
|
|
usage: BufferUsage::STORAGE_BUFFER,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
AllocationCreateInfo {
|
2023-07-19 09:11:17 +00:00
|
|
|
memory_type_filter: MemoryTypeFilter::PREFER_DEVICE
|
|
|
|
| MemoryTypeFilter::HOST_RANDOM_ACCESS,
|
2023-03-05 18:56:35 +00:00
|
|
|
..Default::default()
|
|
|
|
},
|
2017-06-20 13:07:21 +00:00
|
|
|
// Iterator that produces the data.
|
2023-03-05 18:56:35 +00:00
|
|
|
0..65536u32,
|
|
|
|
)
|
|
|
|
.unwrap();
|
2017-06-20 13:07:21 +00:00
|
|
|
|
|
|
|
// In order to let the shader access the buffer, we need to build a *descriptor set* that
|
|
|
|
// contains the buffer.
|
|
|
|
//
|
|
|
|
// The resources that we bind to the descriptor set must match the resources expected by the
|
|
|
|
// pipeline which we pass as the first parameter.
|
|
|
|
//
|
|
|
|
// If you want to run the pipeline on multiple different buffers, you need to create multiple
|
|
|
|
// descriptor sets that each contain the buffer you want to run the shader on.
|
2023-12-28 19:32:13 +00:00
|
|
|
let layout = &pipeline.layout().set_layouts()[0];
|
2023-11-12 16:17:37 +00:00
|
|
|
let set = DescriptorSet::new(
|
2023-11-12 14:10:22 +00:00
|
|
|
descriptor_set_allocator,
|
2021-12-18 10:32:38 +00:00
|
|
|
layout.clone(),
|
|
|
|
[WriteDescriptorSet::buffer(0, data_buffer.clone())],
|
2023-06-03 12:56:27 +00:00
|
|
|
[],
|
2021-12-18 10:32:38 +00:00
|
|
|
)
|
|
|
|
.unwrap();
|
2017-06-20 13:07:21 +00:00
|
|
|
|
|
|
|
// In order to execute our operation, we have to build a command buffer.
|
2023-12-20 12:04:01 +00:00
|
|
|
let mut builder = RecordingCommandBuffer::new(
|
2023-11-14 16:57:43 +00:00
|
|
|
command_buffer_allocator,
|
2022-09-10 06:00:08 +00:00
|
|
|
queue.queue_family_index(),
|
2023-12-20 12:04:01 +00:00
|
|
|
CommandBufferLevel::Primary,
|
|
|
|
CommandBufferBeginInfo {
|
|
|
|
usage: CommandBufferUsage::OneTimeSubmit,
|
|
|
|
..Default::default()
|
|
|
|
},
|
2021-04-26 14:53:18 +00:00
|
|
|
)
|
|
|
|
.unwrap();
|
2023-12-25 03:01:16 +00:00
|
|
|
|
|
|
|
// Note that we clone the pipeline and the set. Since they are both wrapped in an `Arc`,
|
|
|
|
// this only clones the `Arc` and not the whole pipeline or set (which aren't cloneable
|
|
|
|
// anyway). In this example we would avoid cloning them since this is the last time we use
|
|
|
|
// them, but in real code you would probably need to clone them.
|
2020-06-01 14:41:42 +00:00
|
|
|
builder
|
2021-08-27 06:24:16 +00:00
|
|
|
.bind_pipeline_compute(pipeline.clone())
|
2023-08-04 18:55:16 +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,
|
2021-08-27 06:24:16 +00:00
|
|
|
)
|
2020-06-01 14:41:42 +00:00
|
|
|
.unwrap();
|
2023-03-05 18:56:35 +00:00
|
|
|
|
2023-12-25 03:01:16 +00:00
|
|
|
unsafe {
|
|
|
|
// The command buffer only does one thing: execute the compute pipeline. This is called a
|
|
|
|
// *dispatch* operation.
|
|
|
|
builder.dispatch([1024, 1, 1]).unwrap();
|
|
|
|
}
|
|
|
|
|
2020-06-01 14:41:42 +00:00
|
|
|
// Finish building the command buffer by calling `build`.
|
2023-12-09 11:24:52 +00:00
|
|
|
let command_buffer = builder.end().unwrap();
|
2017-06-20 13:07:21 +00:00
|
|
|
|
|
|
|
// Let's execute this command buffer now.
|
2022-08-12 10:18:35 +00:00
|
|
|
let future = sync::now(device)
|
|
|
|
.then_execute(queue, command_buffer)
|
2020-05-10 00:36:20 +00:00
|
|
|
.unwrap()
|
2017-06-20 13:07:21 +00:00
|
|
|
// This line instructs the GPU to signal a *fence* once the command buffer has finished
|
|
|
|
// execution. A fence is a Vulkan object that allows the CPU to know when the GPU has
|
2023-03-05 18:56:35 +00:00
|
|
|
// reached a certain point. We need to signal a fence here because below we want to block
|
|
|
|
// the CPU until the GPU has reached that point in the execution.
|
2020-05-10 00:36:20 +00:00
|
|
|
.then_signal_fence_and_flush()
|
|
|
|
.unwrap();
|
2018-10-05 07:00:02 +00:00
|
|
|
|
2017-06-20 13:07:21 +00:00
|
|
|
// Blocks execution until the GPU has finished the operation. This method only exists on the
|
|
|
|
// future that corresponds to a signalled fence. In other words, this method wouldn't be
|
2023-03-05 18:56:35 +00:00
|
|
|
// available if we didn't call `.then_signal_fence_and_flush()` earlier. The `None` parameter
|
|
|
|
// is an optional timeout.
|
2017-06-20 13:07:21 +00:00
|
|
|
//
|
|
|
|
// Note however that dropping the `future` variable (with `drop(future)` for example) would
|
|
|
|
// block execution as well, and this would be the case even if we didn't call
|
2023-03-05 18:56:35 +00:00
|
|
|
// `.then_signal_fence_and_flush()`. Therefore the actual point of calling
|
|
|
|
// `.then_signal_fence_and_flush()` and `.wait()` is to make things more explicit. In the
|
|
|
|
// future, if the Rust language gets linear types vulkano may get modified so that only
|
|
|
|
// fence-signalled futures can get destroyed like this.
|
2017-06-20 13:07:21 +00:00
|
|
|
future.wait(None).unwrap();
|
|
|
|
|
2023-03-05 18:56:35 +00:00
|
|
|
// Now that the GPU is done, the content of the buffer should have been modified. Let's check
|
|
|
|
// it out. The call to `read()` would return an error if the buffer was still in use by the
|
|
|
|
// GPU.
|
2018-10-28 03:02:29 +00:00
|
|
|
let data_buffer_content = data_buffer.read().unwrap();
|
2020-05-10 00:36:20 +00:00
|
|
|
for n in 0..65536u32 {
|
2017-06-20 13:07:21 +00:00
|
|
|
assert_eq!(data_buffer_content[n as usize], n * 12);
|
|
|
|
}
|
2022-09-17 15:37:22 +00:00
|
|
|
|
|
|
|
println!("Success");
|
2017-06-20 13:07:21 +00:00
|
|
|
}
|