2020-05-09 15:46:12 +00:00
|
|
|
// Copyright (c) 2020 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>,
|
2020-05-09 15:46:12 +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 compute and load Compute Shader local size
|
|
|
|
// layout in runtime through specialization constants using Physical Device metadata.
|
|
|
|
//
|
|
|
|
// Workgroup parallelism capabilities are varying between GPUs and setting them
|
|
|
|
// properly is important to achieve maximal performance that particular device
|
|
|
|
// can provide.
|
|
|
|
|
2022-03-06 19:30:49 +00:00
|
|
|
use std::{fs::File, io::BufWriter, path::Path};
|
|
|
|
use vulkano::{
|
|
|
|
buffer::{BufferUsage, CpuAccessibleBuffer},
|
2022-04-16 13:02:42 +00:00
|
|
|
command_buffer::{AutoCommandBufferBuilder, CommandBufferUsage, CopyImageToBufferInfo},
|
2022-03-06 19:30:49 +00:00
|
|
|
descriptor_set::{PersistentDescriptorSet, WriteDescriptorSet},
|
|
|
|
device::{
|
|
|
|
physical::{PhysicalDevice, PhysicalDeviceType},
|
|
|
|
Device, DeviceCreateInfo, DeviceExtensions, QueueCreateInfo,
|
|
|
|
},
|
|
|
|
format::Format,
|
|
|
|
image::{view::ImageView, ImageDimensions, StorageImage},
|
|
|
|
instance::{Instance, InstanceCreateInfo, InstanceExtensions},
|
|
|
|
pipeline::{ComputePipeline, Pipeline, PipelineBindPoint},
|
|
|
|
sync::{self, GpuFuture},
|
2022-07-30 06:53:52 +00:00
|
|
|
VulkanLibrary,
|
2022-03-06 19:30:49 +00:00
|
|
|
};
|
2020-05-09 15:46:12 +00:00
|
|
|
|
|
|
|
fn main() {
|
2022-07-30 06:53:52 +00:00
|
|
|
let library = VulkanLibrary::new().unwrap();
|
|
|
|
let instance = Instance::new(
|
|
|
|
library,
|
|
|
|
InstanceCreateInfo {
|
|
|
|
enabled_extensions: InstanceExtensions {
|
|
|
|
// This extension is required to obtain physical device metadata
|
|
|
|
// about the device workgroup size limits
|
|
|
|
khr_get_physical_device_properties2: true,
|
2020-05-10 00:36:20 +00:00
|
|
|
|
2022-07-30 06:53:52 +00:00
|
|
|
..InstanceExtensions::none()
|
|
|
|
},
|
|
|
|
// Enable enumerating devices that use non-conformant vulkan implementations. (ex. MoltenVK)
|
|
|
|
enumerate_portability: true,
|
|
|
|
..Default::default()
|
2020-05-10 00:36:20 +00:00
|
|
|
},
|
2022-07-30 06:53:52 +00:00
|
|
|
)
|
2020-05-10 00:36:20 +00:00
|
|
|
.unwrap();
|
2020-05-09 15:46:12 +00:00
|
|
|
|
2021-06-28 06:24:44 +00:00
|
|
|
let device_extensions = DeviceExtensions {
|
|
|
|
..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();
|
2020-05-09 15:46:12 +00:00
|
|
|
let queue = queues.next().unwrap();
|
|
|
|
|
|
|
|
mod cs {
|
2020-05-10 00:36:20 +00:00
|
|
|
vulkano_shaders::shader! {
|
2020-05-09 15:46:12 +00:00
|
|
|
ty: "compute",
|
|
|
|
src: "
|
|
|
|
#version 450
|
|
|
|
|
|
|
|
// We set local_size_x and local_size_y to be variable configurable
|
|
|
|
// values through Specialization Constants. Values 1 and 2 define
|
|
|
|
// constant_id (1 and 2 correspondingly) and default values of
|
|
|
|
// the constants both. The `local_size_z = 1` here is an ordinary
|
|
|
|
// built-in value of the local size in Z axis.
|
|
|
|
//
|
|
|
|
// Unfortunately current GLSL language capabilities doesn't let us
|
|
|
|
// define exact names of the constants so we will have to use
|
|
|
|
// anonymous constants instead. See below on how to provide their
|
|
|
|
// values in run time.
|
|
|
|
//
|
|
|
|
// Please NOTE that the constant_id in local_size layout must be
|
|
|
|
// positive values. Zero value lead to runtime failure on nVidia
|
|
|
|
// devices due to a known bug in nVidia driver.
|
|
|
|
layout(local_size_x_id = 1, local_size_y_id = 2, local_size_z = 1) in;
|
|
|
|
|
|
|
|
// We can still define more constants in the Shader
|
|
|
|
layout(constant_id = 0) const float red = 0.0;
|
|
|
|
layout(constant_id = 3) const float green = 0.0;
|
|
|
|
layout(constant_id = 4) const float blue = 0.0;
|
|
|
|
|
|
|
|
layout(set = 0, binding = 0, rgba8) uniform writeonly image2D img;
|
|
|
|
|
|
|
|
void main() {
|
|
|
|
// Colorful Mandelbrot fractal
|
|
|
|
|
|
|
|
vec2 norm_coordinates = (gl_GlobalInvocationID.xy + vec2(0.5)) / vec2(imageSize(img));
|
|
|
|
vec2 c = (norm_coordinates - vec2(0.5)) * 2.0 - vec2(1.0, 0.0);
|
|
|
|
|
|
|
|
vec2 z = vec2(0.0, 0.0);
|
|
|
|
float i;
|
|
|
|
for (i = 0.0; i < 1.0; i += 0.005) {
|
|
|
|
z = vec2(
|
|
|
|
z.x * z.x - z.y * z.y + c.x,
|
|
|
|
z.y * z.x + z.x * z.y + c.y
|
|
|
|
);
|
|
|
|
|
|
|
|
if (length(z) > 4.0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
vec4 to_write = vec4(vec3(red, green, blue) * i, 1.0);
|
|
|
|
|
|
|
|
imageStore(img, ivec2(gl_GlobalInvocationID.xy), to_write);
|
|
|
|
}
|
|
|
|
"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-13 15:06:16 +00:00
|
|
|
let shader = cs::load(device.clone()).unwrap();
|
2020-05-09 15:46:12 +00:00
|
|
|
|
|
|
|
// Fetching subgroup size from the Physical Device metadata to compute appropriate
|
|
|
|
// Compute Shader local size properties.
|
|
|
|
//
|
|
|
|
// Most of the drivers provide this metadata, but some of the drivers don't.
|
|
|
|
// In this case we can find appropriate value in this table: https://vulkan.gpuinfo.org/
|
|
|
|
// or just use fallback constant for simplicity, but failure to set proper
|
|
|
|
// local size can lead to significant performance penalty.
|
2021-06-28 06:24:44 +00:00
|
|
|
let (local_size_x, local_size_y) = match physical_device.properties().subgroup_size {
|
2020-05-09 15:46:12 +00:00
|
|
|
Some(subgroup_size) => {
|
2021-06-28 06:24:44 +00:00
|
|
|
println!("Subgroup size is {}", subgroup_size);
|
2020-05-09 15:46:12 +00:00
|
|
|
|
|
|
|
// Most of the subgroup values are divisors of 8
|
|
|
|
(8, subgroup_size / 8)
|
2020-05-10 00:36:20 +00:00
|
|
|
}
|
2020-05-09 15:46:12 +00:00
|
|
|
None => {
|
|
|
|
println!("This Vulkan driver doesn't provide physical device Subgroup information");
|
|
|
|
|
|
|
|
// Using fallback constant
|
|
|
|
(8, 8)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-05-10 00:36:20 +00:00
|
|
|
println!(
|
|
|
|
"Local size will be set to: ({}, {}, 1)",
|
|
|
|
local_size_x, local_size_y
|
|
|
|
);
|
2020-05-09 15:46:12 +00:00
|
|
|
|
|
|
|
let spec_consts = cs::SpecializationConstants {
|
|
|
|
red: 0.2,
|
|
|
|
green: 0.5,
|
|
|
|
blue: 1.0,
|
|
|
|
constant_1: local_size_x, // specifying local size constants
|
|
|
|
constant_2: local_size_y,
|
|
|
|
};
|
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
|
|
|
&spec_consts,
|
|
|
|
None,
|
|
|
|
|_| {},
|
|
|
|
)
|
|
|
|
.unwrap();
|
2020-05-09 15:46:12 +00:00
|
|
|
|
2020-05-10 00:36:20 +00:00
|
|
|
let image = StorageImage::new(
|
|
|
|
device.clone(),
|
2021-03-14 12:09:08 +00:00
|
|
|
ImageDimensions::Dim2d {
|
2020-05-10 00:36:20 +00:00
|
|
|
width: 1024,
|
|
|
|
height: 1024,
|
2021-03-14 12:09:08 +00:00
|
|
|
array_layers: 1,
|
2020-05-10 00:36:20 +00:00
|
|
|
},
|
2021-09-02 09:05:23 +00:00
|
|
|
Format::R8G8B8A8_UNORM,
|
2020-05-10 00:36:20 +00:00
|
|
|
Some(queue.family()),
|
|
|
|
)
|
|
|
|
.unwrap();
|
2022-02-27 06:18:14 +00:00
|
|
|
let view = ImageView::new_default(image.clone()).unwrap();
|
2020-05-09 15:46:12 +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::image_view(0, view.clone())],
|
|
|
|
)
|
|
|
|
.unwrap();
|
2020-05-09 15:46:12 +00:00
|
|
|
|
2020-05-10 00:36:20 +00:00
|
|
|
let buf = CpuAccessibleBuffer::from_iter(
|
|
|
|
device.clone(),
|
|
|
|
BufferUsage::all(),
|
|
|
|
false,
|
|
|
|
(0..1024 * 1024 * 4).map(|_| 0u8),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
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,
|
2020-06-01 14:41:42 +00:00
|
|
|
set.clone(),
|
|
|
|
)
|
2021-08-27 06:24:16 +00:00
|
|
|
.dispatch([
|
|
|
|
1024 / local_size_x, // Note that dispatch dimensions must be
|
|
|
|
1024 / local_size_y, // proportional to local size
|
|
|
|
1,
|
|
|
|
])
|
2020-06-01 14:41:42 +00:00
|
|
|
.unwrap()
|
2022-04-16 13:02:42 +00:00
|
|
|
.copy_image_to_buffer(CopyImageToBufferInfo::image_buffer(
|
|
|
|
image.clone(),
|
|
|
|
buf.clone(),
|
|
|
|
))
|
2020-06-01 14:41:42 +00:00
|
|
|
.unwrap();
|
|
|
|
let command_buffer = builder.build().unwrap();
|
2020-05-09 15:46:12 +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();
|
2020-05-09 15:46:12 +00:00
|
|
|
|
|
|
|
future.wait(None).unwrap();
|
|
|
|
|
|
|
|
println!("Success");
|
|
|
|
|
|
|
|
let buffer_content = buf.read().unwrap();
|
|
|
|
let path = Path::new("mandelbrot.png");
|
|
|
|
let file = File::create(path).unwrap();
|
|
|
|
let ref mut w = BufWriter::new(file);
|
|
|
|
let mut encoder = png::Encoder::new(w, 1024, 1024);
|
2021-09-04 04:21:15 +00:00
|
|
|
encoder.set_color(png::ColorType::Rgba);
|
2020-05-09 15:46:12 +00:00
|
|
|
encoder.set_depth(png::BitDepth::Eight);
|
|
|
|
let mut writer = encoder.write_header().unwrap();
|
|
|
|
writer.write_image_data(&buffer_content).unwrap();
|
|
|
|
}
|