2018-11-30 02:09:13 +00:00
|
|
|
// Copyright (c) 2017 The vulkano developers
|
|
|
|
// Licensed under the Apache License, Version 2.0
|
|
|
|
// <LICENSE-APACHE or
|
2020-11-10 17:03:50 +00:00
|
|
|
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT
|
|
|
|
// license <LICENSE-MIT or https://opensource.org/licenses/MIT>,
|
2018-11-30 02:09:13 +00:00
|
|
|
// 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 the standard and relative include directives within
|
|
|
|
// shader source code. The boilerplate is taken from the "basic-compute-shader.rs" example, where
|
|
|
|
// most of the boilerplate is explained.
|
|
|
|
|
2022-03-06 19:30:49 +00:00
|
|
|
use vulkano::{
|
2023-01-12 12:56:10 +00:00
|
|
|
buffer::{Buffer, BufferAllocateInfo, BufferUsage},
|
2022-10-05 09:09:26 +00:00
|
|
|
command_buffer::{
|
|
|
|
allocator::StandardCommandBufferAllocator, AutoCommandBufferBuilder, CommandBufferUsage,
|
|
|
|
},
|
|
|
|
descriptor_set::{
|
|
|
|
allocator::StandardDescriptorSetAllocator, 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-11-05 05:02:28 +00:00
|
|
|
QueueFlags,
|
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
|
|
|
};
|
2018-11-30 02:09:13 +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();
|
|
|
|
|
2020-05-10 00:36:20 +00:00
|
|
|
let device_extensions = DeviceExtensions {
|
|
|
|
khr_storage_buffer_storage_class: true,
|
2022-09-05 20:16:40 +00:00
|
|
|
..DeviceExtensions::empty()
|
2020-01-23 07:37:12 +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()
|
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
|
|
|
})
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
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
|
|
|
);
|
|
|
|
|
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();
|
2020-01-23 07:37:12 +00:00
|
|
|
let queue = queues.next().unwrap();
|
2019-10-23 08:04:49 +00:00
|
|
|
|
2021-11-02 20:33:58 +00:00
|
|
|
let pipeline = {
|
2020-01-23 07:37:12 +00:00
|
|
|
mod cs {
|
2020-05-10 00:36:20 +00:00
|
|
|
vulkano_shaders::shader! {
|
|
|
|
ty: "compute",
|
|
|
|
// We declare what directories to search for when using the `#include <...>`
|
|
|
|
// syntax. Specified directories have descending priorities based on their order.
|
|
|
|
include: [ "src/bin/shader-include/standard-shaders" ],
|
|
|
|
src: "
|
2020-01-23 07:37:12 +00:00
|
|
|
#version 450
|
|
|
|
// Substitutes this line with the contents of the file `common.glsl` found in one of the standard
|
|
|
|
// `include` directories specified above.
|
|
|
|
// Note, that relative inclusion (`#include \"...\"`), although it falls back to standard
|
|
|
|
// inclusion, should not be used for **embedded** shader source, as it may be misleading and/or
|
|
|
|
// confusing.
|
|
|
|
#include <common.glsl>
|
2019-10-23 08:04:49 +00:00
|
|
|
|
2020-01-23 07:37:12 +00:00
|
|
|
layout(local_size_x = 64, local_size_y = 1, local_size_z = 1) in;
|
2019-10-23 08:04:49 +00:00
|
|
|
|
2020-01-23 07:37:12 +00:00
|
|
|
layout(set = 0, binding = 0) buffer Data {
|
|
|
|
uint data[];
|
|
|
|
} data;
|
2019-10-23 08:04:49 +00:00
|
|
|
|
2020-01-23 07:37:12 +00:00
|
|
|
void main() {
|
|
|
|
uint idx = gl_GlobalInvocationID.x;
|
|
|
|
data.data[idx] = multiply_by_12(data.data[idx]);
|
|
|
|
}
|
|
|
|
"
|
2020-05-10 00:36:20 +00:00
|
|
|
}
|
|
|
|
}
|
2021-11-13 15:06:16 +00:00
|
|
|
let shader = cs::load(device.clone()).unwrap();
|
2021-09-04 04:20:05 +00:00
|
|
|
ComputePipeline::new(
|
|
|
|
device.clone(),
|
2021-11-13 15:06:16 +00:00
|
|
|
shader.entry_point("main").unwrap(),
|
2021-09-04 04:20:05 +00:00
|
|
|
&(),
|
|
|
|
None,
|
|
|
|
|_| {},
|
|
|
|
)
|
|
|
|
.unwrap()
|
2021-11-02 20:33:58 +00:00
|
|
|
};
|
2019-10-23 08:04:49 +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
|
|
|
|
2019-11-24 09:07:29 +00:00
|
|
|
let data_buffer = {
|
2022-08-12 10:18:35 +00:00
|
|
|
let data_iter = 0..65536u32;
|
2023-01-12 12:56:10 +00:00
|
|
|
Buffer::from_iter(
|
2022-10-26 14:25:01 +00:00
|
|
|
&memory_allocator,
|
2023-01-12 12:56:10 +00:00
|
|
|
BufferAllocateInfo {
|
|
|
|
buffer_usage: BufferUsage::STORAGE_BUFFER,
|
|
|
|
..Default::default()
|
|
|
|
},
|
2022-09-05 20:16:40 +00:00
|
|
|
data_iter,
|
|
|
|
)
|
|
|
|
.unwrap()
|
2019-11-24 09:07:29 +00:00
|
|
|
};
|
2021-09-02 08:29:27 +00:00
|
|
|
|
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(),
|
|
|
|
[WriteDescriptorSet::buffer(0, data_buffer.clone())],
|
|
|
|
)
|
|
|
|
.unwrap();
|
2021-09-02 08:29:27 +00:00
|
|
|
|
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();
|
2020-06-01 14:41:42 +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,
|
2022-08-12 10:18:35 +00:00
|
|
|
set,
|
2021-08-27 06:24:16 +00:00
|
|
|
)
|
|
|
|
.dispatch([1024, 1, 1])
|
2020-06-01 14:41:42 +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)
|
2020-05-10 00:36:20 +00:00
|
|
|
.unwrap()
|
|
|
|
.then_signal_fence_and_flush()
|
|
|
|
.unwrap();
|
2019-10-23 08:04:49 +00:00
|
|
|
|
2020-05-10 00:36:20 +00:00
|
|
|
future.wait(None).unwrap();
|
2019-10-23 08:04:49 +00:00
|
|
|
|
2020-05-10 00:36:20 +00:00
|
|
|
let data_buffer_content = data_buffer.read().unwrap();
|
|
|
|
for n in 0..65536u32 {
|
|
|
|
assert_eq!(data_buffer_content[n as usize], n * 12);
|
|
|
|
}
|
2022-09-17 15:37:22 +00:00
|
|
|
|
|
|
|
println!("Success");
|
2018-11-30 02:09:13 +00:00
|
|
|
}
|