2023-03-05 18:56:35 +00:00
|
|
|
// Multisampling anti-aliasing example, using a render pass resolve.
|
|
|
|
//
|
|
|
|
// # Introduction to multisampling
|
|
|
|
//
|
|
|
|
// When you draw an object on an image, this object occupies a certain set of pixels. Each pixel of
|
|
|
|
// the image is either fully covered by the object, or not covered at all. There is no such thing
|
|
|
|
// as a pixel that is half-covered by the object that you're drawing. What this means is that you
|
|
|
|
// will sometimes see a "staircase effect" at the border of your object, also called aliasing.
|
|
|
|
//
|
|
|
|
// The root cause of aliasing is that the resolution of the image is not high enough. If you
|
|
|
|
// increase the size of the image you're drawing to, this effect will still exist but will be much
|
|
|
|
// less visible.
|
|
|
|
//
|
|
|
|
// In order to decrease aliasing, some games and programs use what we call "SuperSample Anti-
|
|
|
|
// Aliasing" (SSAA). For example instead of drawing to an image of size 1024x1024, you draw to an
|
|
|
|
// image of size 2048x2048. Then at the end, you scale down your image to 1024x1024 by merging
|
|
|
|
// nearby pixels. Since the intermediate image is 4 times larger than the destination, this would
|
|
|
|
// be 4x SSAA.
|
|
|
|
//
|
|
|
|
// However this technique is very expensive in terms of GPU power. The fragment shader and all its
|
|
|
|
// calculations has to run four times more often.
|
|
|
|
//
|
|
|
|
// So instead of SSAA, a common alternative is MSAA (MultiSample Anti-Aliasing). The base principle
|
|
|
|
// is more or less the same: you draw to an image of a larger dimension, and then at the end you
|
|
|
|
// scale it down to the final size. The difference is that the fragment shader is only run once per
|
|
|
|
// pixel of the final size, and its value is duplicated to fill to all the pixels of the
|
|
|
|
// intermediate image that are covered by the object.
|
|
|
|
//
|
|
|
|
// For example, let's say that you use 4x MSAA, you draw to an intermediate image of size
|
|
|
|
// 2048x2048, and your object covers the whole image. With MSAA, the fragment shader will only be
|
|
|
|
// run 1,048,576 times (1024 * 1024), compared to 4,194,304 times (2048 * 2048) with 4x SSAA. Then
|
|
|
|
// the output of each fragment shader invocation is copied in each of the four pixels of the
|
|
|
|
// intermediate image that correspond to each pixel of the final image.
|
|
|
|
//
|
|
|
|
// Now, let's say that your object doesn't cover the whole image. In this situation, only the
|
|
|
|
// pixels of the intermediate image that are covered by the object will receive the output of the
|
|
|
|
// fragment shader.
|
|
|
|
//
|
|
|
|
// Because of the way it works, this technique requires direct support from the hardware, contrary
|
|
|
|
// to SSAA which can be done on any machine.
|
|
|
|
//
|
|
|
|
// # Multisampled images
|
|
|
|
//
|
|
|
|
// Using MSAA with Vulkan is done by creating a regular image, but with a number of samples per
|
|
|
|
// pixel different from 1. For example if you want to use 4x MSAA, you should create an image with
|
2023-07-06 08:52:11 +00:00
|
|
|
// 4 samples per pixel. Internally this image will have 4 times as many pixels as its extent
|
2023-03-05 18:56:35 +00:00
|
|
|
// would normally require, but this is handled transparently for you. Drawing to a multisampled
|
|
|
|
// image is exactly the same as drawing to a regular image.
|
|
|
|
//
|
|
|
|
// However multisampled images have some restrictions, for example you can't show them on the
|
|
|
|
// screen (swapchain images are always single-sampled), and you can't copy them into a buffer.
|
|
|
|
// Therefore when you have finished drawing, you have to blit your multisampled image to a
|
|
|
|
// non-multisampled image. This operation is not a regular blit (blitting a multisampled image is
|
|
|
|
// an error), instead it is called *resolving* the image.
|
2018-08-30 01:37:51 +00:00
|
|
|
|
2023-09-03 11:09:07 +00:00
|
|
|
use std::{fs::File, io::BufWriter, path::Path, sync::Arc};
|
2022-03-06 19:30:49 +00:00
|
|
|
use vulkano::{
|
2023-04-02 13:07:16 +00:00
|
|
|
buffer::{Buffer, BufferContents, BufferCreateInfo, BufferUsage},
|
2022-03-06 19:30:49 +00:00
|
|
|
command_buffer::{
|
2023-12-20 12:04:01 +00:00
|
|
|
allocator::StandardCommandBufferAllocator, CommandBufferBeginInfo, CommandBufferLevel,
|
|
|
|
CommandBufferUsage, CopyImageToBufferInfo, RecordingCommandBuffer, RenderPassBeginInfo,
|
2022-03-06 19:30:49 +00:00
|
|
|
},
|
|
|
|
device::{
|
2022-09-10 06:00:08 +00:00
|
|
|
physical::PhysicalDeviceType, Device, DeviceCreateInfo, DeviceExtensions, QueueCreateInfo,
|
2022-11-05 05:02:28 +00:00
|
|
|
QueueFlags,
|
2022-03-06 19:30:49 +00:00
|
|
|
},
|
2022-04-24 01:16:19 +00:00
|
|
|
format::Format,
|
2023-07-06 08:52:11 +00:00
|
|
|
image::{view::ImageView, Image, ImageCreateInfo, ImageType, ImageUsage, SampleCount},
|
2023-06-17 20:18:48 +00:00
|
|
|
instance::{Instance, InstanceCreateFlags, InstanceCreateInfo},
|
2023-07-19 09:11:17 +00:00
|
|
|
memory::allocator::{AllocationCreateInfo, MemoryTypeFilter, StandardMemoryAllocator},
|
2022-03-06 19:30:49 +00:00
|
|
|
pipeline::{
|
|
|
|
graphics::{
|
2023-10-07 14:46:03 +00:00
|
|
|
color_blend::{ColorBlendAttachmentState, ColorBlendState},
|
2023-04-18 18:53:08 +00:00
|
|
|
input_assembly::InputAssemblyState,
|
2022-05-29 16:53:36 +00:00
|
|
|
multisample::MultisampleState,
|
2023-04-18 18:53:08 +00:00
|
|
|
rasterization::RasterizationState,
|
2023-04-19 15:45:27 +00:00
|
|
|
vertex_input::{Vertex, VertexDefinition},
|
2022-03-06 19:30:49 +00:00
|
|
|
viewport::{Viewport, ViewportState},
|
2023-04-22 18:46:22 +00:00
|
|
|
GraphicsPipelineCreateInfo,
|
2022-03-06 19:30:49 +00:00
|
|
|
},
|
2023-04-22 18:46:22 +00:00
|
|
|
layout::PipelineDescriptorSetLayoutCreateInfo,
|
2023-10-07 14:46:03 +00:00
|
|
|
DynamicState, GraphicsPipeline, PipelineLayout, PipelineShaderStageCreateInfo,
|
2022-03-06 19:30:49 +00:00
|
|
|
},
|
|
|
|
render_pass::{Framebuffer, FramebufferCreateInfo, Subpass},
|
|
|
|
sync::GpuFuture,
|
2022-07-30 06:53:52 +00:00
|
|
|
VulkanLibrary,
|
2021-05-30 20:42:51 +00:00
|
|
|
};
|
2018-10-26 00:15:33 +00:00
|
|
|
|
2018-08-30 01:37:51 +00:00
|
|
|
fn main() {
|
|
|
|
// The usual Vulkan initialization.
|
2022-07-30 06:53:52 +00:00
|
|
|
let library = VulkanLibrary::new().unwrap();
|
|
|
|
let instance = Instance::new(
|
|
|
|
library,
|
|
|
|
InstanceCreateInfo {
|
2023-06-17 20:18:48 +00:00
|
|
|
flags: InstanceCreateFlags::ENUMERATE_PORTABILITY,
|
2022-07-30 06:53:52 +00:00
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
)
|
2022-02-14 09:32:27 +00:00
|
|
|
.unwrap();
|
2021-06-28 06:24:44 +00:00
|
|
|
|
|
|
|
let device_extensions = DeviceExtensions {
|
2022-09-05 20:16:40 +00:00
|
|
|
..DeviceExtensions::empty()
|
2021-06-28 06:24:44 +00:00
|
|
|
};
|
2022-09-10 06:00:08 +00:00
|
|
|
let (physical_device, queue_family_index) = instance
|
|
|
|
.enumerate_physical_devices()
|
|
|
|
.unwrap()
|
|
|
|
.filter(|p| p.supported_extensions().contains(&device_extensions))
|
2021-06-28 06:24:44 +00:00
|
|
|
.filter_map(|p| {
|
2022-09-10 06:00:08 +00:00
|
|
|
p.queue_family_properties()
|
|
|
|
.iter()
|
2022-11-05 05:02:28 +00:00
|
|
|
.position(|q| q.queue_flags.intersects(QueueFlags::GRAPHICS))
|
2022-09-10 06:00:08 +00:00
|
|
|
.map(|i| (p, i as u32))
|
2021-06-28 06:24:44 +00:00
|
|
|
})
|
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,
|
2022-09-05 20:16:40 +00:00
|
|
|
_ => 5,
|
2021-06-28 06:24:44 +00:00
|
|
|
})
|
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,
|
2023-03-05 18:56:35 +00:00
|
|
|
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-09-10 06:00:08 +00:00
|
|
|
queue_create_infos: vec![QueueCreateInfo {
|
|
|
|
queue_family_index,
|
|
|
|
..Default::default()
|
|
|
|
}],
|
2022-02-14 09:32:27 +00:00
|
|
|
..Default::default()
|
|
|
|
},
|
2020-05-10 00:36:20 +00:00
|
|
|
)
|
|
|
|
.unwrap();
|
2018-08-30 01:37:51 +00:00
|
|
|
let queue = queues.next().unwrap();
|
|
|
|
|
2023-09-03 11:09:07 +00:00
|
|
|
let memory_allocator = Arc::new(StandardMemoryAllocator::new_default(device.clone()));
|
2022-10-26 14:25:01 +00:00
|
|
|
|
2018-08-30 01:37:51 +00:00
|
|
|
// Creating our intermediate multisampled image.
|
|
|
|
//
|
2023-07-06 08:52:11 +00:00
|
|
|
// As explained in the introduction, we pass the same extent and format as for the final
|
2018-08-30 01:37:51 +00:00
|
|
|
// image. But we also pass the number of samples-per-pixel, which is 4 here.
|
2022-02-27 06:18:14 +00:00
|
|
|
let intermediary = ImageView::new_default(
|
2023-07-03 20:37:29 +00:00
|
|
|
Image::new(
|
2023-09-03 11:09:07 +00:00
|
|
|
memory_allocator.clone(),
|
2023-07-03 20:37:29 +00:00
|
|
|
ImageCreateInfo {
|
2023-07-06 08:52:11 +00:00
|
|
|
image_type: ImageType::Dim2d,
|
2023-07-14 14:44:34 +00:00
|
|
|
format: Format::R8G8B8A8_UNORM,
|
2023-07-06 08:52:11 +00:00
|
|
|
extent: [1024, 1024, 1],
|
2023-07-03 20:37:29 +00:00
|
|
|
usage: ImageUsage::COLOR_ATTACHMENT | ImageUsage::TRANSIENT_ATTACHMENT,
|
|
|
|
samples: SampleCount::Sample4,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
AllocationCreateInfo::default(),
|
2021-03-14 12:09:08 +00:00
|
|
|
)
|
|
|
|
.unwrap(),
|
2020-05-10 00:36:20 +00:00
|
|
|
)
|
|
|
|
.unwrap();
|
2018-08-30 01:37:51 +00:00
|
|
|
|
|
|
|
// This is the final image that will receive the anti-aliased triangle.
|
2023-07-03 20:37:29 +00:00
|
|
|
let image = Image::new(
|
2023-09-03 11:09:07 +00:00
|
|
|
memory_allocator.clone(),
|
2023-07-03 20:37:29 +00:00
|
|
|
ImageCreateInfo {
|
2023-07-06 08:52:11 +00:00
|
|
|
image_type: ImageType::Dim2d,
|
2023-07-14 14:44:34 +00:00
|
|
|
format: Format::R8G8B8A8_UNORM,
|
2023-07-06 08:52:11 +00:00
|
|
|
extent: [1024, 1024, 1],
|
2023-07-03 20:37:29 +00:00
|
|
|
usage: ImageUsage::TRANSFER_SRC
|
|
|
|
| ImageUsage::TRANSFER_DST
|
|
|
|
| ImageUsage::COLOR_ATTACHMENT
|
|
|
|
| ImageUsage::STORAGE,
|
|
|
|
..Default::default()
|
2020-05-10 00:36:20 +00:00
|
|
|
},
|
2023-07-03 20:37:29 +00:00
|
|
|
AllocationCreateInfo::default(),
|
2020-05-10 00:36:20 +00:00
|
|
|
)
|
|
|
|
.unwrap();
|
2023-07-03 20:37:29 +00:00
|
|
|
|
2022-02-27 06:18:14 +00:00
|
|
|
let view = ImageView::new_default(image.clone()).unwrap();
|
2018-08-30 01:37:51 +00:00
|
|
|
|
|
|
|
// In this example, we are going to perform the *resolve* (ie. turning a multisampled image
|
|
|
|
// into a non-multisampled one) as part of the render pass. This is the preferred method of
|
|
|
|
// doing so, as it the advantage that the Vulkan implementation doesn't have to write the
|
|
|
|
// content of the multisampled image back to memory at the end.
|
2021-11-02 20:33:58 +00:00
|
|
|
let render_pass = vulkano::single_pass_renderpass!(
|
|
|
|
device.clone(),
|
|
|
|
attachments: {
|
|
|
|
// The first framebuffer attachment is the intermediary image.
|
|
|
|
intermediary: {
|
|
|
|
format: Format::R8G8B8A8_UNORM,
|
2023-03-05 18:56:35 +00:00
|
|
|
// This has to match the image definition.
|
|
|
|
samples: 4,
|
2023-06-17 19:01:50 +00:00
|
|
|
load_op: Clear,
|
|
|
|
store_op: DontCare,
|
2018-08-30 01:37:51 +00:00
|
|
|
},
|
2021-11-02 20:33:58 +00:00
|
|
|
// The second framebuffer attachment is the final image.
|
|
|
|
color: {
|
|
|
|
format: Format::R8G8B8A8_UNORM,
|
2023-03-05 18:56:35 +00:00
|
|
|
// Same here, this has to match.
|
|
|
|
samples: 1,
|
2023-06-17 19:01:50 +00:00
|
|
|
load_op: DontCare,
|
|
|
|
store_op: Store,
|
2023-03-25 09:25:25 +00:00
|
|
|
},
|
2021-11-02 20:33:58 +00:00
|
|
|
},
|
|
|
|
pass: {
|
|
|
|
// When drawing, we have only one output which is the intermediary image.
|
2023-06-17 19:01:50 +00:00
|
|
|
//
|
|
|
|
// At the end of the pass, each color attachment will be *resolved* into the image
|
|
|
|
// given under `color_resolve`. In other words, here, at the end of the pass, the
|
|
|
|
// `intermediary` attachment will be copied to the attachment named `color`.
|
|
|
|
//
|
|
|
|
// For depth/stencil attachments, there is also a `depth_stencil_resolve` field.
|
|
|
|
// When you specify this, you must also specify at least one of the
|
|
|
|
// `depth_resolve_mode` and `stencil_resolve_mode` fields.
|
|
|
|
// We don't need that here, so it's skipped.
|
2021-11-02 20:33:58 +00:00
|
|
|
color: [intermediary],
|
2023-06-17 19:01:50 +00:00
|
|
|
color_resolve: [color],
|
2021-11-02 20:33:58 +00:00
|
|
|
depth_stencil: {},
|
2023-03-25 09:25:25 +00:00
|
|
|
},
|
2021-11-02 20:33:58 +00:00
|
|
|
)
|
|
|
|
.unwrap();
|
2018-08-30 01:37:51 +00:00
|
|
|
|
|
|
|
// Creating the framebuffer, the calls to `add` match the list of attachments in order.
|
2022-02-20 00:14:16 +00:00
|
|
|
let framebuffer = Framebuffer::new(
|
|
|
|
render_pass.clone(),
|
|
|
|
FramebufferCreateInfo {
|
2022-08-12 10:18:35 +00:00
|
|
|
attachments: vec![intermediary, view],
|
2022-02-20 00:14:16 +00:00
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.unwrap();
|
2018-08-30 01:37:51 +00:00
|
|
|
|
|
|
|
// Here is the "end" of the multisampling example, as starting from here everything is the same
|
2023-03-05 18:56:35 +00:00
|
|
|
// as in any other example. The pipeline, vertex buffer, and command buffer are created in
|
|
|
|
// exactly the same way as without multisampling. At the end of the example, we copy the
|
|
|
|
// content of `image` (ie. the final image) to a buffer, then read the content of that buffer
|
|
|
|
// and save it to a PNG file.
|
2018-08-30 01:37:51 +00:00
|
|
|
|
2018-10-28 07:29:41 +00:00
|
|
|
mod vs {
|
2020-05-10 00:36:20 +00:00
|
|
|
vulkano_shaders::shader! {
|
2023-03-05 18:56:35 +00:00
|
|
|
ty: "vertex",
|
|
|
|
src: r"
|
2020-01-23 07:37:12 +00:00
|
|
|
#version 450
|
2018-10-28 07:29:41 +00:00
|
|
|
|
2020-01-23 07:37:12 +00:00
|
|
|
layout(location = 0) in vec2 position;
|
2018-10-28 07:29:41 +00:00
|
|
|
|
2020-01-23 07:37:12 +00:00
|
|
|
void main() {
|
|
|
|
gl_Position = vec4(position, 0.0, 1.0);
|
2023-03-05 18:56:35 +00:00
|
|
|
}
|
|
|
|
",
|
|
|
|
}
|
2020-05-10 00:36:20 +00:00
|
|
|
}
|
2018-10-28 07:29:41 +00:00
|
|
|
|
2020-05-10 00:36:20 +00:00
|
|
|
mod fs {
|
|
|
|
vulkano_shaders::shader! {
|
2023-03-05 18:56:35 +00:00
|
|
|
ty: "fragment",
|
|
|
|
src: r"
|
2020-01-23 07:37:12 +00:00
|
|
|
#version 450
|
2018-10-28 07:29:41 +00:00
|
|
|
|
2020-01-23 07:37:12 +00:00
|
|
|
layout(location = 0) out vec4 f_color;
|
2018-10-28 07:29:41 +00:00
|
|
|
|
2020-01-23 07:37:12 +00:00
|
|
|
void main() {
|
|
|
|
f_color = vec4(1.0, 0.0, 0.0, 1.0);
|
|
|
|
}
|
2023-03-05 18:56:35 +00:00
|
|
|
",
|
2018-10-28 07:29:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-05 18:56:35 +00:00
|
|
|
#[derive(BufferContents, Vertex)]
|
2021-11-24 14:19:57 +00:00
|
|
|
#[repr(C)]
|
2018-08-30 01:37:51 +00:00
|
|
|
struct Vertex {
|
2022-12-28 10:23:36 +00:00
|
|
|
#[format(R32G32_SFLOAT)]
|
2018-08-30 01:37:51 +00:00
|
|
|
position: [f32; 2],
|
|
|
|
}
|
2018-10-28 03:02:29 +00:00
|
|
|
|
2022-03-06 19:30:49 +00:00
|
|
|
let vertices = [
|
|
|
|
Vertex {
|
|
|
|
position: [-0.5, -0.5],
|
|
|
|
},
|
|
|
|
Vertex {
|
|
|
|
position: [0.0, 0.5],
|
|
|
|
},
|
|
|
|
Vertex {
|
|
|
|
position: [0.5, -0.25],
|
|
|
|
},
|
|
|
|
];
|
2023-01-12 12:56:10 +00:00
|
|
|
let vertex_buffer = Buffer::from_iter(
|
2023-09-03 11:09:07 +00:00
|
|
|
memory_allocator.clone(),
|
2023-04-02 13:07:16 +00:00
|
|
|
BufferCreateInfo {
|
|
|
|
usage: BufferUsage::VERTEX_BUFFER,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
AllocationCreateInfo {
|
2023-07-19 09:11:17 +00:00
|
|
|
memory_type_filter: MemoryTypeFilter::PREFER_DEVICE
|
|
|
|
| MemoryTypeFilter::HOST_SEQUENTIAL_WRITE,
|
2023-01-12 12:56:10 +00:00
|
|
|
..Default::default()
|
|
|
|
},
|
2022-09-05 20:16:40 +00:00
|
|
|
vertices,
|
|
|
|
)
|
|
|
|
.unwrap();
|
2020-05-10 00:36:20 +00:00
|
|
|
|
2023-04-22 18:46:22 +00:00
|
|
|
let pipeline = {
|
|
|
|
let vs = vs::load(device.clone())
|
|
|
|
.unwrap()
|
|
|
|
.entry_point("main")
|
|
|
|
.unwrap();
|
|
|
|
let fs = fs::load(device.clone())
|
|
|
|
.unwrap()
|
|
|
|
.entry_point("main")
|
|
|
|
.unwrap();
|
|
|
|
let vertex_input_state = Vertex::per_vertex()
|
|
|
|
.definition(&vs.info().input_interface)
|
|
|
|
.unwrap();
|
|
|
|
let stages = [
|
2023-06-25 18:08:27 +00:00
|
|
|
PipelineShaderStageCreateInfo::new(vs),
|
|
|
|
PipelineShaderStageCreateInfo::new(fs),
|
2023-04-22 18:46:22 +00:00
|
|
|
];
|
|
|
|
let layout = PipelineLayout::new(
|
|
|
|
device.clone(),
|
|
|
|
PipelineDescriptorSetLayoutCreateInfo::from_stages(&stages)
|
|
|
|
.into_pipeline_layout_create_info(device.clone())
|
|
|
|
.unwrap(),
|
|
|
|
)
|
2021-11-02 20:33:58 +00:00
|
|
|
.unwrap();
|
2023-04-22 18:46:22 +00:00
|
|
|
let subpass = Subpass::from(render_pass, 0).unwrap();
|
2023-07-03 20:37:29 +00:00
|
|
|
|
2023-04-22 18:46:22 +00:00
|
|
|
GraphicsPipeline::new(
|
|
|
|
device.clone(),
|
|
|
|
None,
|
|
|
|
GraphicsPipelineCreateInfo {
|
|
|
|
stages: stages.into_iter().collect(),
|
|
|
|
vertex_input_state: Some(vertex_input_state),
|
|
|
|
input_assembly_state: Some(InputAssemblyState::default()),
|
2023-10-07 14:46:03 +00:00
|
|
|
viewport_state: Some(ViewportState::default()),
|
2023-04-22 18:46:22 +00:00
|
|
|
rasterization_state: Some(RasterizationState::default()),
|
|
|
|
multisample_state: Some(MultisampleState {
|
|
|
|
rasterization_samples: subpass.num_samples().unwrap(),
|
|
|
|
..Default::default()
|
|
|
|
}),
|
2023-10-07 14:46:03 +00:00
|
|
|
color_blend_state: Some(ColorBlendState::with_attachment_states(
|
|
|
|
subpass.num_color_attachments(),
|
|
|
|
ColorBlendAttachmentState::default(),
|
|
|
|
)),
|
|
|
|
dynamic_state: [DynamicState::Viewport].into_iter().collect(),
|
2023-04-22 18:46:22 +00:00
|
|
|
subpass: Some(subpass.into()),
|
|
|
|
..GraphicsPipelineCreateInfo::layout(layout)
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.unwrap()
|
|
|
|
};
|
2018-08-30 01:37:51 +00:00
|
|
|
|
2021-08-27 06:24:16 +00:00
|
|
|
let viewport = Viewport {
|
2023-06-25 18:08:27 +00:00
|
|
|
offset: [0.0, 0.0],
|
|
|
|
extent: [1024.0, 1024.0],
|
|
|
|
depth_range: 0.0..=1.0,
|
2018-08-30 01:37:51 +00:00
|
|
|
};
|
|
|
|
|
2023-11-14 16:57:43 +00:00
|
|
|
let command_buffer_allocator = Arc::new(StandardCommandBufferAllocator::new(
|
|
|
|
device,
|
|
|
|
Default::default(),
|
|
|
|
));
|
2022-10-05 09:09:26 +00:00
|
|
|
|
2023-01-12 12:56:10 +00:00
|
|
|
let buf = Buffer::from_iter(
|
2023-09-03 11:09:07 +00:00
|
|
|
memory_allocator,
|
2023-04-02 13:07:16 +00:00
|
|
|
BufferCreateInfo {
|
|
|
|
usage: BufferUsage::TRANSFER_DST,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
AllocationCreateInfo {
|
2023-07-19 09:11:17 +00:00
|
|
|
memory_type_filter: MemoryTypeFilter::PREFER_HOST
|
|
|
|
| MemoryTypeFilter::HOST_RANDOM_ACCESS,
|
2023-01-12 12:56:10 +00:00
|
|
|
..Default::default()
|
|
|
|
},
|
2020-05-10 00:36:20 +00:00
|
|
|
(0..1024 * 1024 * 4).map(|_| 0u8),
|
|
|
|
)
|
|
|
|
.unwrap();
|
2018-08-30 01:37:51 +00:00
|
|
|
|
2023-12-20 12:04:01 +00:00
|
|
|
let mut builder = RecordingCommandBuffer::new(
|
2023-11-14 16:57:43 +00:00
|
|
|
command_buffer_allocator,
|
2022-09-10 06:00:08 +00:00
|
|
|
queue.queue_family_index(),
|
2023-12-20 12:04:01 +00:00
|
|
|
CommandBufferLevel::Primary,
|
|
|
|
CommandBufferBeginInfo {
|
|
|
|
usage: CommandBufferUsage::OneTimeSubmit,
|
|
|
|
..Default::default()
|
|
|
|
},
|
2021-04-26 14:53:18 +00:00
|
|
|
)
|
|
|
|
.unwrap();
|
2023-12-25 03:01:16 +00:00
|
|
|
|
2020-06-01 14:41:42 +00:00
|
|
|
builder
|
|
|
|
.begin_render_pass(
|
2022-04-24 01:16:19 +00:00
|
|
|
RenderPassBeginInfo {
|
|
|
|
clear_values: vec![Some([0.0, 0.0, 1.0, 1.0].into()), None],
|
2022-08-12 10:18:35 +00:00
|
|
|
..RenderPassBeginInfo::framebuffer(framebuffer)
|
2022-04-24 01:16:19 +00:00
|
|
|
},
|
2023-08-04 18:55:16 +00:00
|
|
|
Default::default(),
|
2020-06-01 14:41:42 +00:00
|
|
|
)
|
|
|
|
.unwrap()
|
2023-05-18 11:03:37 +00:00
|
|
|
.set_viewport(0, [viewport].into_iter().collect())
|
2023-08-04 18:55:16 +00:00
|
|
|
.unwrap()
|
2022-08-12 10:18:35 +00:00
|
|
|
.bind_pipeline_graphics(pipeline)
|
2023-08-04 18:55:16 +00:00
|
|
|
.unwrap()
|
2021-08-27 06:24:16 +00:00
|
|
|
.bind_vertex_buffers(0, vertex_buffer.clone())
|
2023-12-25 03:01:16 +00:00
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
builder.draw(vertex_buffer.len() as u32, 1, 0, 0).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
builder
|
2023-08-04 18:55:16 +00:00
|
|
|
.end_render_pass(Default::default())
|
2020-06-01 14:41:42 +00:00
|
|
|
.unwrap()
|
2022-08-12 10:18:35 +00:00
|
|
|
.copy_image_to_buffer(CopyImageToBufferInfo::image_buffer(image, buf.clone()))
|
2020-06-01 14:41:42 +00:00
|
|
|
.unwrap();
|
2018-08-30 01:37:51 +00:00
|
|
|
|
2023-12-25 03:01:16 +00:00
|
|
|
let command_buffer = builder.end().unwrap();
|
2022-08-12 10:18:35 +00:00
|
|
|
let finished = command_buffer.execute(queue).unwrap();
|
2020-05-10 00:36:20 +00:00
|
|
|
finished
|
|
|
|
.then_signal_fence_and_flush()
|
2018-08-30 01:37:51 +00:00
|
|
|
.unwrap()
|
2020-05-10 00:36:20 +00:00
|
|
|
.wait(None)
|
2018-08-30 01:37:51 +00:00
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let buffer_content = buf.read().unwrap();
|
2023-10-29 17:46:14 +00:00
|
|
|
let path = Path::new(env!("CARGO_MANIFEST_DIR")).join("triangle.png");
|
|
|
|
let file = File::create(&path).unwrap();
|
2022-08-12 10:18:35 +00:00
|
|
|
let w = &mut BufWriter::new(file);
|
2020-01-23 07:37:12 +00:00
|
|
|
let mut encoder = png::Encoder::new(w, 1024, 1024); // Width is 2 pixels and height is 1.
|
2021-09-04 04:21:15 +00:00
|
|
|
encoder.set_color(png::ColorType::Rgba);
|
2020-01-23 07:37:12 +00:00
|
|
|
encoder.set_depth(png::BitDepth::Eight);
|
|
|
|
let mut writer = encoder.write_header().unwrap();
|
|
|
|
writer.write_image_data(&buffer_content).unwrap();
|
2022-09-17 15:37:22 +00:00
|
|
|
|
|
|
|
if let Ok(path) = path.canonicalize() {
|
|
|
|
println!("Saved to {}", path.display());
|
|
|
|
}
|
2018-08-30 01:37:51 +00:00
|
|
|
}
|