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.
|
|
|
|
|
|
|
|
// TODO: Give a paragraph about what push constants are and what problems they solve
|
|
|
|
|
2018-10-28 03:02:29 +00:00
|
|
|
use vulkano::buffer::{BufferUsage, CpuAccessibleBuffer};
|
2018-10-11 00:42:56 +00:00
|
|
|
use vulkano::command_buffer::AutoCommandBufferBuilder;
|
|
|
|
use vulkano::descriptor::descriptor_set::PersistentDescriptorSet;
|
2019-11-24 09:07:29 +00:00
|
|
|
use vulkano::descriptor::PipelineLayoutAbstract;
|
2018-10-28 03:02:29 +00:00
|
|
|
use vulkano::device::{Device, DeviceExtensions};
|
|
|
|
use vulkano::instance::{Instance, InstanceExtensions, PhysicalDevice};
|
2018-10-11 00:42:56 +00:00
|
|
|
use vulkano::pipeline::ComputePipeline;
|
2018-10-28 03:02:29 +00:00
|
|
|
use vulkano::sync;
|
2020-05-10 00:36:20 +00:00
|
|
|
use vulkano::sync::GpuFuture;
|
2018-10-11 00:42:56 +00:00
|
|
|
|
2018-10-26 00:15:33 +00:00
|
|
|
use std::sync::Arc;
|
2018-10-11 00:42:56 +00:00
|
|
|
|
2018-10-28 07:29:41 +00:00
|
|
|
fn main() {
|
|
|
|
let instance = Instance::new(None, &InstanceExtensions::none(), None).unwrap();
|
|
|
|
let physical = PhysicalDevice::enumerate(&instance).next().unwrap();
|
2020-05-10 00:36:20 +00:00
|
|
|
let queue_family = physical
|
|
|
|
.queue_families()
|
|
|
|
.find(|&q| q.supports_compute())
|
|
|
|
.unwrap();
|
|
|
|
let (device, mut queues) = Device::new(
|
|
|
|
physical,
|
|
|
|
physical.supported_features(),
|
|
|
|
&DeviceExtensions {
|
|
|
|
khr_storage_buffer_storage_class: true,
|
|
|
|
..DeviceExtensions::none()
|
|
|
|
},
|
|
|
|
[(queue_family, 0.5)].iter().cloned(),
|
|
|
|
)
|
|
|
|
.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
|
|
|
|
2018-10-28 03:02:29 +00:00
|
|
|
let shader = cs::Shader::load(device.clone()).unwrap();
|
2020-11-29 08:40:44 +00:00
|
|
|
let pipeline = Arc::new(
|
|
|
|
ComputePipeline::new(device.clone(), &shader.main_entry_point(), &(), 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
|
|
|
};
|
|
|
|
|
2019-11-24 09:07:29 +00:00
|
|
|
let layout = pipeline.layout().descriptor_set_layout(0).unwrap();
|
2018-10-28 03:02:29 +00:00
|
|
|
let set = Arc::new(
|
2019-11-24 09:07:29 +00:00
|
|
|
PersistentDescriptorSet::start(layout.clone())
|
2020-05-10 00:36:20 +00:00
|
|
|
.add_buffer(data_buffer.clone())
|
|
|
|
.unwrap()
|
|
|
|
.build()
|
|
|
|
.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.
|
2020-06-01 14:41:42 +00:00
|
|
|
let mut builder =
|
|
|
|
AutoCommandBufferBuilder::primary_one_time_submit(device.clone(), queue.family()).unwrap();
|
|
|
|
builder
|
2021-02-08 11:43:56 +00:00
|
|
|
.dispatch(
|
|
|
|
[1024, 1, 1],
|
|
|
|
pipeline.clone(),
|
|
|
|
set.clone(),
|
|
|
|
push_constants,
|
|
|
|
vec![],
|
|
|
|
)
|
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");
|
|
|
|
}
|