2022-01-02 20:54:02 +00:00
|
|
|
// This example is a copy of `basic-compute-shaders.rs`, but initalizes half of the input buffer
|
2023-03-05 18:56:35 +00:00
|
|
|
// and then we use `copy_buffer_dimensions` to copy the first half of the input buffer to the
|
|
|
|
// second half.
|
2022-01-02 20:54:02 +00:00
|
|
|
|
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-04-16 13:02:42 +00:00
|
|
|
command_buffer::{
|
2022-10-05 09:09:26 +00:00
|
|
|
allocator::StandardCommandBufferAllocator, AutoCommandBufferBuilder, BufferCopy,
|
|
|
|
CommandBufferUsage, CopyBufferInfoTyped,
|
|
|
|
},
|
|
|
|
descriptor_set::{
|
|
|
|
allocator::StandardDescriptorSetAllocator, PersistentDescriptorSet, WriteDescriptorSet,
|
2022-04-16 13:02:42 +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
|
|
|
};
|
2022-01-02 20:54:02 +00:00
|
|
|
|
|
|
|
fn main() {
|
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();
|
2022-01-02 20:54:02 +00:00
|
|
|
|
|
|
|
let device_extensions = DeviceExtensions {
|
|
|
|
khr_storage_buffer_storage_class: true,
|
2022-09-05 20:16:40 +00:00
|
|
|
..DeviceExtensions::empty()
|
2022-01-02 20:54:02 +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))
|
2022-01-02 20:54:02 +00:00
|
|
|
.filter_map(|p| {
|
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))
|
2022-01-02 20:54:02 +00:00
|
|
|
})
|
|
|
|
.min_by_key(|(p, _)| match p.properties().device_type {
|
|
|
|
PhysicalDeviceType::DiscreteGpu => 0,
|
|
|
|
PhysicalDeviceType::IntegratedGpu => 1,
|
|
|
|
PhysicalDeviceType::VirtualGpu => 2,
|
|
|
|
PhysicalDeviceType::Cpu => 3,
|
|
|
|
PhysicalDeviceType::Other => 4,
|
2022-09-05 20:16:40 +00:00
|
|
|
_ => 5,
|
2022-01-02 20:54:02 +00:00
|
|
|
})
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
println!(
|
|
|
|
"Using device: {} (type: {:?})",
|
|
|
|
physical_device.properties().device_name,
|
2023-03-05 18:56:35 +00:00
|
|
|
physical_device.properties().device_type,
|
2022-01-02 20:54:02 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
let (device, mut queues) = Device::new(
|
|
|
|
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()
|
|
|
|
},
|
2022-01-02 20:54:02 +00:00
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let queue = queues.next().unwrap();
|
|
|
|
|
|
|
|
let pipeline = {
|
|
|
|
mod cs {
|
|
|
|
vulkano_shaders::shader! {
|
|
|
|
ty: "compute",
|
2023-03-05 18:56:35 +00:00
|
|
|
src: r"
|
2022-01-02 20:54:02 +00:00
|
|
|
#version 450
|
|
|
|
|
|
|
|
layout(local_size_x = 64, local_size_y = 1, local_size_z = 1) in;
|
|
|
|
|
|
|
|
layout(set = 0, binding = 0) buffer Data {
|
|
|
|
uint data[];
|
2023-03-05 18:56:35 +00:00
|
|
|
};
|
2022-01-02 20:54:02 +00:00
|
|
|
|
|
|
|
void main() {
|
|
|
|
uint idx = gl_GlobalInvocationID.x;
|
2023-03-05 18:56:35 +00:00
|
|
|
data[idx] *= 12;
|
2022-01-02 20:54:02 +00:00
|
|
|
}
|
2023-03-05 18:56:35 +00:00
|
|
|
",
|
2022-01-02 20:54:02 +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();
|
2022-01-02 20:54:02 +00:00
|
|
|
ComputePipeline::new(
|
|
|
|
device.clone(),
|
|
|
|
None,
|
2023-04-22 18:46:22 +00:00
|
|
|
ComputePipelineCreateInfo::stage_layout(stage, layout),
|
2022-01-02 20:54:02 +00:00
|
|
|
)
|
|
|
|
.unwrap()
|
|
|
|
};
|
|
|
|
|
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(),
|
|
|
|
));
|
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
|
|
|
|
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
|
2023-03-05 18:56:35 +00:00
|
|
|
| BufferUsage::TRANSFER_SRC
|
|
|
|
| BufferUsage::TRANSFER_DST,
|
|
|
|
..Default::default()
|
2023-04-02 13:07:16 +00:00
|
|
|
},
|
|
|
|
AllocationCreateInfo {
|
2023-07-19 09:11:17 +00:00
|
|
|
memory_type_filter: MemoryTypeFilter::PREFER_DEVICE
|
|
|
|
| MemoryTypeFilter::HOST_RANDOM_ACCESS,
|
2023-04-02 13:07:16 +00:00
|
|
|
..Default::default()
|
2023-03-05 18:56:35 +00:00
|
|
|
},
|
|
|
|
// We intitialize half of the array and leave the other half at 0, we will use the copy
|
|
|
|
// command later to fill it.
|
|
|
|
(0..65536u32).map(|n| if n < 65536 / 2 { n } else { 0 }),
|
|
|
|
)
|
|
|
|
.unwrap();
|
2022-01-02 20:54:02 +00:00
|
|
|
|
2022-02-25 22:52:44 +00:00
|
|
|
let layout = pipeline.layout().set_layouts().get(0).unwrap();
|
2022-01-02 20:54:02 +00:00
|
|
|
let set = PersistentDescriptorSet::new(
|
2023-11-12 14:10:22 +00:00
|
|
|
descriptor_set_allocator,
|
2022-01-02 20:54:02 +00:00
|
|
|
layout.clone(),
|
|
|
|
[WriteDescriptorSet::buffer(0, data_buffer.clone())],
|
2023-06-03 12:56:27 +00:00
|
|
|
[],
|
2022-01-02 20:54:02 +00:00
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
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(),
|
2022-01-02 20:54:02 +00:00
|
|
|
CommandBufferUsage::OneTimeSubmit,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
builder
|
2023-03-05 18:56:35 +00:00
|
|
|
// Copy from the first half to the second half (inside the same buffer) before we run the
|
|
|
|
// computation.
|
2022-04-16 13:02:42 +00:00
|
|
|
.copy_buffer(CopyBufferInfoTyped {
|
|
|
|
regions: [BufferCopy {
|
|
|
|
src_offset: 0,
|
|
|
|
dst_offset: 65536 / 2,
|
|
|
|
size: 65536 / 2,
|
|
|
|
..Default::default()
|
|
|
|
}]
|
|
|
|
.into(),
|
|
|
|
..CopyBufferInfoTyped::buffers(data_buffer.clone(), data_buffer.clone())
|
|
|
|
})
|
2022-01-02 20:54:02 +00:00
|
|
|
.unwrap()
|
|
|
|
.bind_pipeline_compute(pipeline.clone())
|
2023-08-04 18:55:16 +00:00
|
|
|
.unwrap()
|
2022-01-02 20:54:02 +00:00
|
|
|
.bind_descriptor_sets(
|
|
|
|
PipelineBindPoint::Compute,
|
|
|
|
pipeline.layout().clone(),
|
|
|
|
0,
|
2022-08-12 10:18:35 +00:00
|
|
|
set,
|
2022-01-02 20:54:02 +00:00
|
|
|
)
|
2023-08-04 18:55:16 +00:00
|
|
|
.unwrap()
|
2022-01-02 20:54:02 +00:00
|
|
|
.dispatch([1024, 1, 1])
|
|
|
|
.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)
|
2022-01-02 20:54:02 +00:00
|
|
|
.unwrap()
|
|
|
|
.then_signal_fence_and_flush()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
future.wait(None).unwrap();
|
|
|
|
|
|
|
|
let data_buffer_content = data_buffer.read().unwrap();
|
|
|
|
|
2023-03-05 18:56:35 +00:00
|
|
|
// Here we have the same data in the two halfs of the buffer.
|
2022-01-02 20:54:02 +00:00
|
|
|
for n in 0..65536 / 2 {
|
2023-03-05 18:56:35 +00:00
|
|
|
// The two halfs should have the same data.
|
2022-01-02 20:54:02 +00:00
|
|
|
assert_eq!(data_buffer_content[n as usize], n * 12);
|
|
|
|
assert_eq!(data_buffer_content[n as usize + 65536 / 2], n * 12);
|
|
|
|
}
|
2022-09-17 15:37:22 +00:00
|
|
|
|
|
|
|
println!("Success");
|
2022-01-02 20:54:02 +00:00
|
|
|
}
|