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.
|
|
|
|
|
2021-04-26 14:53:18 +00:00
|
|
|
use std::sync::Arc;
|
2018-11-30 02:09:13 +00:00
|
|
|
use vulkano::buffer::{BufferUsage, CpuAccessibleBuffer};
|
2021-04-26 14:53:18 +00:00
|
|
|
use vulkano::command_buffer::{AutoCommandBufferBuilder, CommandBufferUsage};
|
2021-06-28 08:28:09 +00:00
|
|
|
use vulkano::descriptor_set::PersistentDescriptorSet;
|
2021-06-28 08:04:28 +00:00
|
|
|
use vulkano::device::physical::{PhysicalDevice, PhysicalDeviceType};
|
2021-06-28 06:24:44 +00:00
|
|
|
use vulkano::device::{Device, DeviceExtensions, Features};
|
2021-06-28 08:04:28 +00:00
|
|
|
use vulkano::instance::{Instance, InstanceExtensions};
|
2021-05-23 15:33:25 +00:00
|
|
|
use vulkano::pipeline::{ComputePipeline, ComputePipelineAbstract};
|
2018-11-30 02:09:13 +00:00
|
|
|
use vulkano::sync;
|
2020-05-10 00:36:20 +00:00
|
|
|
use vulkano::sync::GpuFuture;
|
2021-05-23 16:09:50 +00:00
|
|
|
use vulkano::Version;
|
2018-11-30 02:09:13 +00:00
|
|
|
|
|
|
|
fn main() {
|
2021-06-28 06:24:44 +00:00
|
|
|
let instance = Instance::new(None, Version::V1_1, &InstanceExtensions::none(), None).unwrap();
|
2020-05-10 00:36:20 +00:00
|
|
|
let device_extensions = DeviceExtensions {
|
|
|
|
khr_storage_buffer_storage_class: true,
|
|
|
|
..DeviceExtensions::none()
|
2020-01-23 07:37:12 +00:00
|
|
|
};
|
2021-06-28 06:24:44 +00:00
|
|
|
let (physical_device, queue_family) = PhysicalDevice::enumerate(&instance)
|
2021-06-28 09:07:54 +00:00
|
|
|
.filter(|&p| p.supported_extensions().is_superset_of(&device_extensions))
|
2021-06-28 06:24:44 +00:00
|
|
|
.filter_map(|p| {
|
|
|
|
p.queue_families()
|
|
|
|
.find(|&q| q.supports_compute())
|
|
|
|
.map(|q| (p, q))
|
|
|
|
})
|
|
|
|
.min_by_key(|(p, _)| match p.properties().device_type.unwrap() {
|
|
|
|
PhysicalDeviceType::DiscreteGpu => 0,
|
|
|
|
PhysicalDeviceType::IntegratedGpu => 1,
|
|
|
|
PhysicalDeviceType::VirtualGpu => 2,
|
|
|
|
PhysicalDeviceType::Cpu => 3,
|
|
|
|
PhysicalDeviceType::Other => 4,
|
|
|
|
})
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
println!(
|
|
|
|
"Using device: {} (type: {:?})",
|
|
|
|
physical_device.properties().device_name.as_ref().unwrap(),
|
|
|
|
physical_device.properties().device_type.unwrap()
|
|
|
|
);
|
|
|
|
|
2020-05-10 00:36:20 +00:00
|
|
|
let (device, mut queues) = Device::new(
|
2021-06-28 06:24:44 +00:00
|
|
|
physical_device,
|
|
|
|
&Features::none(),
|
2021-06-28 08:04:28 +00:00
|
|
|
&physical_device
|
|
|
|
.required_extensions()
|
|
|
|
.union(&device_extensions),
|
2020-05-10 00:36:20 +00:00
|
|
|
[(queue_family, 0.5)].iter().cloned(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
2020-01-23 07:37:12 +00:00
|
|
|
let queue = queues.next().unwrap();
|
2019-10-23 08:04:49 +00:00
|
|
|
|
2020-01-23 07:37:12 +00:00
|
|
|
let pipeline = Arc::new({
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
let shader = cs::Shader::load(device.clone()).unwrap();
|
2020-11-29 08:40:44 +00:00
|
|
|
ComputePipeline::new(device.clone(), &shader.main_entry_point(), &(), None).unwrap()
|
2020-05-10 00:36:20 +00:00
|
|
|
});
|
2019-10-23 08:04:49 +00:00
|
|
|
|
2019-11-24 09:07:29 +00:00
|
|
|
let data_buffer = {
|
2020-05-10 00:36:20 +00:00
|
|
|
let data_iter = (0..65536u32).map(|n| n);
|
|
|
|
CpuAccessibleBuffer::from_iter(device.clone(), BufferUsage::all(), false, data_iter)
|
|
|
|
.unwrap()
|
2019-11-24 09:07:29 +00:00
|
|
|
};
|
|
|
|
let layout = pipeline.layout().descriptor_set_layout(0).unwrap();
|
2020-05-10 00:36:20 +00:00
|
|
|
let set = Arc::new(
|
|
|
|
PersistentDescriptorSet::start(layout.clone())
|
|
|
|
.add_buffer(data_buffer.clone())
|
|
|
|
.unwrap()
|
|
|
|
.build()
|
|
|
|
.unwrap(),
|
2019-11-24 09:07:29 +00:00
|
|
|
);
|
2021-04-26 14:53:18 +00:00
|
|
|
let mut builder = AutoCommandBufferBuilder::primary(
|
|
|
|
device.clone(),
|
|
|
|
queue.family(),
|
|
|
|
CommandBufferUsage::OneTimeSubmit,
|
|
|
|
)
|
|
|
|
.unwrap();
|
2020-06-01 14:41:42 +00:00
|
|
|
builder
|
2021-02-05 15:38:36 +00:00
|
|
|
.dispatch([1024, 1, 1], pipeline.clone(), set.clone(), (), vec![])
|
2020-06-01 14:41:42 +00:00
|
|
|
.unwrap();
|
|
|
|
let command_buffer = builder.build().unwrap();
|
2019-11-24 09:07:29 +00:00
|
|
|
let future = sync::now(device.clone())
|
2020-05-10 00:36:20 +00:00
|
|
|
.then_execute(queue.clone(), command_buffer)
|
|
|
|
.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);
|
|
|
|
}
|
2018-11-30 02:09:13 +00:00
|
|
|
}
|