2018-10-11 00:42:56 +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-10-11 00:42:56 +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.
|
|
|
|
|
2022-07-18 13:11:43 +00:00
|
|
|
// Push constants are a small bank of values written directly to the command buffer
|
2022-07-13 10:26:47 +00:00
|
|
|
// and accessible in shaders. They allow the application to set values used in shaders
|
|
|
|
// without creating buffers or modifying and binding descriptor sets for each update.
|
|
|
|
// As a result, they are expected to outperform such memory-backed resource updates.
|
|
|
|
|
2022-03-06 19:30:49 +00:00
|
|
|
use vulkano::{
|
|
|
|
buffer::{BufferUsage, CpuAccessibleBuffer},
|
|
|
|
command_buffer::{AutoCommandBufferBuilder, CommandBufferUsage},
|
|
|
|
descriptor_set::{PersistentDescriptorSet, WriteDescriptorSet},
|
|
|
|
device::{
|
|
|
|
physical::{PhysicalDevice, PhysicalDeviceType},
|
|
|
|
Device, DeviceCreateInfo, DeviceExtensions, QueueCreateInfo,
|
|
|
|
},
|
2022-07-18 13:11:43 +00:00
|
|
|
instance::{Instance, InstanceCreateInfo},
|
2022-03-06 19:30:49 +00:00
|
|
|
pipeline::{ComputePipeline, Pipeline, PipelineBindPoint},
|
|
|
|
sync::{self, GpuFuture},
|
|
|
|
};
|
2018-10-11 00:42:56 +00:00
|
|
|
|
2018-10-28 07:29:41 +00:00
|
|
|
fn main() {
|
2022-07-18 13:11:43 +00:00
|
|
|
let instance = Instance::new(InstanceCreateInfo {
|
|
|
|
// Enable enumerating devices that use non-conformant vulkan implementations. (ex. MoltenVK)
|
|
|
|
enumerate_portability: true,
|
|
|
|
..Default::default()
|
|
|
|
})
|
|
|
|
.unwrap();
|
2021-06-28 06:24:44 +00:00
|
|
|
|
|
|
|
let device_extensions = DeviceExtensions {
|
|
|
|
khr_storage_buffer_storage_class: true,
|
|
|
|
..DeviceExtensions::none()
|
|
|
|
};
|
|
|
|
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))
|
|
|
|
})
|
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,
|
|
|
|
})
|
2020-05-10 00:36:20 +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
|
|
|
);
|
|
|
|
|
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-02-14 09:32:27 +00:00
|
|
|
queue_create_infos: vec![QueueCreateInfo::family(queue_family)],
|
|
|
|
..Default::default()
|
|
|
|
},
|
2020-05-10 00:36:20 +00:00
|
|
|
)
|
|
|
|
.unwrap();
|
2018-10-28 07:29:41 +00:00
|
|
|
let queue = queues.next().unwrap();
|
|
|
|
|
|
|
|
mod cs {
|
2020-05-10 00:36:20 +00:00
|
|
|
vulkano_shaders::shader! {
|
2018-10-28 07:29:41 +00:00
|
|
|
ty: "compute",
|
|
|
|
src: "
|
2020-01-23 07:37:12 +00:00
|
|
|
#version 450
|
2018-10-11 00:42:56 +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-11 00:42:56 +00:00
|
|
|
|
2020-01-23 07:37:12 +00:00
|
|
|
layout(push_constant) uniform PushConstantData {
|
|
|
|
int multiple;
|
|
|
|
float addend;
|
|
|
|
bool enable;
|
|
|
|
} pc;
|
2018-10-11 00:42:56 +00:00
|
|
|
|
2020-01-23 07:37:12 +00:00
|
|
|
layout(set = 0, binding = 0) buffer Data {
|
|
|
|
uint data[];
|
|
|
|
} data;
|
2018-10-11 00:42:56 +00:00
|
|
|
|
2020-01-23 07:37:12 +00:00
|
|
|
void main() {
|
|
|
|
uint idx = gl_GlobalInvocationID.x;
|
|
|
|
if (pc.enable) {
|
|
|
|
data.data[idx] *= pc.multiple;
|
|
|
|
data.data[idx] += uint(pc.addend);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"
|
2018-10-28 07:29:41 +00:00
|
|
|
}
|
2018-10-27 23:10:29 +00:00
|
|
|
}
|
2018-10-11 00:42:56 +00:00
|
|
|
|
2021-11-13 15:06:16 +00:00
|
|
|
let shader = cs::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,
|
|
|
|
|_| {},
|
|
|
|
)
|
|
|
|
.unwrap();
|
2018-10-11 00:42:56 +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()
|
2018-10-11 00:42:56 +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(
|
|
|
|
layout.clone(),
|
|
|
|
[WriteDescriptorSet::buffer(0, data_buffer.clone())],
|
|
|
|
)
|
|
|
|
.unwrap();
|
2018-10-11 00:42:56 +00:00
|
|
|
|
2018-10-28 03:02:29 +00:00
|
|
|
// The `vulkano_shaders::shaders!` macro generates a struct with the correct representation of the push constants struct specified in the shader.
|
2018-10-11 00:42:56 +00:00
|
|
|
// Here we create an instance of the generated struct.
|
|
|
|
let push_constants = cs::ty::PushConstantData {
|
|
|
|
multiple: 1,
|
|
|
|
addend: 1.0,
|
|
|
|
enable: 1,
|
|
|
|
};
|
|
|
|
|
|
|
|
// For a compute pipeline, push constants are passed to the `dispatch` method.
|
|
|
|
// For a graphics pipeline, push constants are passed to the `draw` and `draw_indexed` methods.
|
|
|
|
// Note that there is no type safety for the push constants argument.
|
2018-10-28 03:02:29 +00:00
|
|
|
// So be careful to only pass an instance of the struct generated by the `vulkano_shaders::shaders!` macro.
|
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-08-27 06:24:16 +00:00
|
|
|
.bind_pipeline_compute(pipeline.clone())
|
|
|
|
.bind_descriptor_sets(
|
|
|
|
PipelineBindPoint::Compute,
|
|
|
|
pipeline.layout().clone(),
|
|
|
|
0,
|
|
|
|
set.clone(),
|
|
|
|
)
|
|
|
|
.push_constants(pipeline.layout().clone(), 0, push_constants)
|
|
|
|
.dispatch([1024, 1, 1])
|
2020-06-01 14:41:42 +00:00
|
|
|
.unwrap();
|
|
|
|
let command_buffer = builder.build().unwrap();
|
2018-10-11 00:42:56 +00:00
|
|
|
|
2018-10-28 03:02: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();
|
2018-10-11 00:42:56 +00:00
|
|
|
|
|
|
|
future.wait(None).unwrap();
|
|
|
|
|
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 {
|
2018-10-11 00:42:56 +00:00
|
|
|
assert_eq!(data_buffer_content[n as usize], n * 1 + 1);
|
|
|
|
}
|
|
|
|
println!("Success");
|
|
|
|
}
|