Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
104 lines (84 sloc) 3.45 KB
// Copyright (c) 2017 The vulkano developers
// Licensed under the Apache License, Version 2.0
// <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT
// license <LICENSE-MIT or http://opensource.org/licenses/MIT>,
// 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 contains the source code of the second part of the guide at http://vulkano.rs.
//!
//! It is not commented, as the explanations can be found in the guide itself.
extern crate vulkano;
#[macro_use]
extern crate vulkano_shader_derive;
use std::sync::Arc;
use vulkano::buffer::BufferUsage;
use vulkano::buffer::CpuAccessibleBuffer;
use vulkano::command_buffer::AutoCommandBufferBuilder;
use vulkano::command_buffer::CommandBuffer;
use vulkano::descriptor::descriptor_set::PersistentDescriptorSet;
use vulkano::device::Device;
use vulkano::device::DeviceExtensions;
use vulkano::instance::Features;
use vulkano::instance::Instance;
use vulkano::instance::InstanceExtensions;
use vulkano::instance::PhysicalDevice;
use vulkano::pipeline::ComputePipeline;
use vulkano::sync::GpuFuture;
fn main() {
let instance = Instance::new(None, &InstanceExtensions::none(), None)
.expect("failed to create instance");
let physical = PhysicalDevice::enumerate(&instance).next().expect("no device available");
let queue_family = physical.queue_families()
.find(|&q| q.supports_compute())
.expect("couldn't find a compute queue family");
let (device, mut queues) = {
Device::new(physical, &Features::none(), &DeviceExtensions::none(),
[(queue_family, 0.5)].iter().cloned()).expect("failed to create device")
};
let queue = queues.next().unwrap();
// Introduction to compute operations
let data_iter = 0 .. 65536;
let data_buffer = CpuAccessibleBuffer::from_iter(device.clone(), BufferUsage::all(),
data_iter).expect("failed to create buffer");
// Compute pipelines
#[allow(dead_code)]
mod cs {
#[derive(VulkanoShader)]
#[ty = "compute"]
#[src = "
#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[];
} buf;
void main() {
uint idx = gl_GlobalInvocationID.x;
buf.data[idx] *= 12;
}"
]
struct Dummy;
}
let shader = cs::Shader::load(device.clone())
.expect("failed to create shader module");
let compute_pipeline = Arc::new(ComputePipeline::new(device.clone(), &shader.main_entry_point(), &())
.expect("failed to create compute pipeline"));
// Descriptor sets
let set = Arc::new(PersistentDescriptorSet::start(compute_pipeline.clone(), 0)
.add_buffer(data_buffer.clone()).unwrap()
.build().unwrap()
);
// Dispatch
let command_buffer = AutoCommandBufferBuilder::new(device.clone(), queue.family()).unwrap()
.dispatch([1024, 1, 1], compute_pipeline.clone(), set.clone(), ()).unwrap()
.build().unwrap();
let finished = command_buffer.execute(queue.clone()).unwrap();
finished.then_signal_fence_and_flush().unwrap()
.wait(None).unwrap();
let content = data_buffer.read().unwrap();
for (n, val) in content.iter().enumerate() {
assert_eq!(*val, n as u32 * 12);
}
println!("Everything succeeded!");
}