2016-03-26 09:17:37 +00:00
|
|
|
// Copyright (c) 2016 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.
|
|
|
|
|
2018-10-28 03:02:29 +00:00
|
|
|
use vulkano::buffer::{BufferUsage, CpuAccessibleBuffer};
|
|
|
|
use vulkano::command_buffer::{AutoCommandBufferBuilder, DynamicState};
|
|
|
|
use vulkano::descriptor::descriptor_set::PersistentDescriptorSet;
|
|
|
|
use vulkano::device::{Device, DeviceExtensions};
|
|
|
|
use vulkano::format::Format;
|
|
|
|
use vulkano::framebuffer::{Framebuffer, FramebufferAbstract, Subpass, RenderPassAbstract};
|
|
|
|
use vulkano::image::{SwapchainImage, ImmutableImage, Dimensions};
|
|
|
|
use vulkano::instance::{Instance, PhysicalDevice};
|
|
|
|
use vulkano::pipeline::GraphicsPipeline;
|
2018-10-27 21:16:30 +00:00
|
|
|
use vulkano::pipeline::viewport::Viewport;
|
2018-10-28 03:02:29 +00:00
|
|
|
use vulkano::sampler::{Sampler, SamplerAddressMode, Filter, MipmapMode};
|
|
|
|
use vulkano::swapchain::{AcquireError, PresentMode, SurfaceTransform, Swapchain, SwapchainCreationError};
|
|
|
|
use vulkano::swapchain;
|
|
|
|
use vulkano::sync::{GpuFuture, FlushError};
|
|
|
|
use vulkano::sync;
|
|
|
|
|
|
|
|
use vulkano_win::VkSurfaceBuild;
|
2018-10-27 21:16:30 +00:00
|
|
|
|
2018-10-28 03:02:29 +00:00
|
|
|
use winit::{EventsLoop, Window, WindowBuilder, Event, WindowEvent};
|
|
|
|
|
|
|
|
use image::ImageFormat;
|
2016-02-24 09:47:58 +00:00
|
|
|
|
2017-04-04 07:00:11 +00:00
|
|
|
use std::sync::Arc;
|
2016-02-24 09:47:58 +00:00
|
|
|
|
|
|
|
fn main() {
|
|
|
|
// The start of this example is exactly the same as `triangle`. You should read the
|
|
|
|
// `triangle` example if you haven't done so yet.
|
|
|
|
|
2016-05-04 05:10:00 +00:00
|
|
|
let extensions = vulkano_win::required_extensions();
|
2018-10-28 03:02:29 +00:00
|
|
|
let instance = Instance::new(None, &extensions, None).unwrap();
|
2016-02-24 09:47:58 +00:00
|
|
|
|
2018-10-28 03:02:29 +00:00
|
|
|
let physical = PhysicalDevice::enumerate(&instance).next().unwrap();
|
2016-02-24 09:47:58 +00:00
|
|
|
println!("Using device: {} (type: {:?})", physical.name(), physical.ty());
|
|
|
|
|
2018-10-28 03:02:29 +00:00
|
|
|
let mut events_loop = EventsLoop::new();
|
|
|
|
let surface = WindowBuilder::new().build_vk_surface(&events_loop, instance.clone()).unwrap();
|
2018-10-27 21:16:30 +00:00
|
|
|
let window = surface.window();
|
2017-07-26 15:58:40 +00:00
|
|
|
|
2018-10-28 03:02:29 +00:00
|
|
|
let queue_family = physical.queue_families().find(|&q|
|
|
|
|
q.supports_graphics() && surface.is_supported(q).unwrap_or(false)
|
|
|
|
).unwrap();
|
2016-02-24 09:47:58 +00:00
|
|
|
|
2018-10-28 03:02:29 +00:00
|
|
|
let device_ext = DeviceExtensions { khr_swapchain: true, .. DeviceExtensions::none() };
|
|
|
|
let (device, mut queues) = Device::new(physical, physical.supported_features(), &device_ext,
|
|
|
|
[(queue_family, 0.5)].iter().cloned()).unwrap();
|
2016-05-12 15:40:31 +00:00
|
|
|
let queue = queues.next().unwrap();
|
2016-02-24 09:47:58 +00:00
|
|
|
|
2018-10-27 21:16:30 +00:00
|
|
|
let (mut swapchain, images) = {
|
2018-10-28 03:02:29 +00:00
|
|
|
let caps = surface.capabilities(physical).unwrap();
|
2016-02-24 09:47:58 +00:00
|
|
|
|
2018-10-28 03:02:29 +00:00
|
|
|
let usage = caps.supported_usage_flags;
|
|
|
|
let alpha = caps.supported_composite_alpha.iter().next().unwrap();
|
|
|
|
let format = caps.supported_formats[0].0;
|
|
|
|
|
|
|
|
let initial_dimensions = if let Some(dimensions) = window.get_inner_size() {
|
2018-10-27 21:16:30 +00:00
|
|
|
// convert to physical pixels
|
|
|
|
let dimensions: (u32, u32) = dimensions.to_physical(window.get_hidpi_factor()).into();
|
|
|
|
[dimensions.0, dimensions.1]
|
|
|
|
} else {
|
|
|
|
// The window no longer exists so exit the application.
|
|
|
|
return;
|
|
|
|
};
|
2016-02-24 09:47:58 +00:00
|
|
|
|
2018-10-28 03:02:29 +00:00
|
|
|
Swapchain::new(device.clone(), surface.clone(), caps.min_image_count, format,
|
|
|
|
initial_dimensions, 1, usage, &queue, SurfaceTransform::Identity, alpha,
|
|
|
|
PresentMode::Fifo, true, None).unwrap()
|
2016-02-24 09:47:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2019-07-01 21:02:48 +00:00
|
|
|
#[derive(Default, Debug, Clone)]
|
2016-02-24 09:47:58 +00:00
|
|
|
struct Vertex { position: [f32; 2] }
|
2018-12-07 12:35:19 +00:00
|
|
|
vulkano::impl_vertex!(Vertex, position);
|
2016-02-24 09:47:58 +00:00
|
|
|
|
2018-10-28 03:02:29 +00:00
|
|
|
let vertex_buffer = CpuAccessibleBuffer::<[Vertex]>::from_iter(
|
|
|
|
device.clone(),
|
|
|
|
BufferUsage::all(),
|
|
|
|
[
|
|
|
|
Vertex { position: [-0.5, -0.5 ] },
|
|
|
|
Vertex { position: [-0.5, 0.5 ] },
|
|
|
|
Vertex { position: [ 0.5, -0.5 ] },
|
|
|
|
Vertex { position: [ 0.5, 0.5 ] },
|
|
|
|
].iter().cloned()
|
|
|
|
).unwrap();
|
2016-02-24 09:47:58 +00:00
|
|
|
|
2018-10-28 03:02:29 +00:00
|
|
|
let vs = vs::Shader::load(device.clone()).unwrap();
|
|
|
|
let fs = fs::Shader::load(device.clone()).unwrap();
|
2016-02-24 09:47:58 +00:00
|
|
|
|
2018-10-27 21:16:30 +00:00
|
|
|
let render_pass = Arc::new(
|
2018-12-07 12:35:19 +00:00
|
|
|
vulkano::single_pass_renderpass!(device.clone(),
|
2016-02-26 15:21:06 +00:00
|
|
|
attachments: {
|
|
|
|
color: {
|
|
|
|
load: Clear,
|
|
|
|
store: Store,
|
2017-05-26 11:12:21 +00:00
|
|
|
format: swapchain.format(),
|
2017-04-04 07:00:11 +00:00
|
|
|
samples: 1,
|
2016-02-26 15:21:06 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
pass: {
|
|
|
|
color: [color],
|
|
|
|
depth_stencil: {}
|
2016-02-24 12:14:51 +00:00
|
|
|
}
|
2017-04-04 07:00:11 +00:00
|
|
|
).unwrap()
|
|
|
|
);
|
2016-02-24 09:47:58 +00:00
|
|
|
|
2017-07-22 18:08:52 +00:00
|
|
|
let (texture, tex_future) = {
|
2016-02-24 14:00:06 +00:00
|
|
|
let image = image::load_from_memory_with_format(include_bytes!("image_img.png"),
|
2018-10-28 03:02:29 +00:00
|
|
|
ImageFormat::PNG).unwrap().to_rgba();
|
2016-02-24 14:00:06 +00:00
|
|
|
let image_data = image.into_raw().clone();
|
|
|
|
|
2018-10-28 03:02:29 +00:00
|
|
|
ImmutableImage::from_iter(
|
2017-07-22 18:08:52 +00:00
|
|
|
image_data.iter().cloned(),
|
2018-10-28 03:02:29 +00:00
|
|
|
Dimensions::Dim2d { width: 93, height: 93 },
|
|
|
|
Format::R8G8B8A8Srgb,
|
|
|
|
queue.clone()
|
|
|
|
).unwrap()
|
2016-02-24 14:00:06 +00:00
|
|
|
};
|
|
|
|
|
2018-10-28 03:02:29 +00:00
|
|
|
let sampler = Sampler::new(device.clone(), Filter::Linear, Filter::Linear,
|
|
|
|
MipmapMode::Nearest, SamplerAddressMode::Repeat, SamplerAddressMode::Repeat,
|
|
|
|
SamplerAddressMode::Repeat, 0.0, 1.0, 0.0, 0.0).unwrap();
|
2016-02-24 14:00:06 +00:00
|
|
|
|
2018-10-28 03:02:29 +00:00
|
|
|
let pipeline = Arc::new(GraphicsPipeline::start()
|
2017-06-10 16:55:05 +00:00
|
|
|
.vertex_input_single_buffer::<Vertex>()
|
2017-06-13 13:21:19 +00:00
|
|
|
.vertex_shader(vs.main_entry_point(), ())
|
2017-06-10 16:55:05 +00:00
|
|
|
.triangle_strip()
|
2017-07-26 15:58:40 +00:00
|
|
|
.viewports_dynamic_scissors_irrelevant(1)
|
2017-06-13 13:21:19 +00:00
|
|
|
.fragment_shader(fs.main_entry_point(), ())
|
2017-07-22 18:08:52 +00:00
|
|
|
.blend_alpha_blending()
|
2018-10-28 03:02:29 +00:00
|
|
|
.render_pass(Subpass::from(render_pass.clone(), 0).unwrap())
|
2017-06-10 16:55:05 +00:00
|
|
|
.build(device.clone())
|
|
|
|
.unwrap());
|
2017-04-04 07:00:11 +00:00
|
|
|
|
2018-10-28 03:02:29 +00:00
|
|
|
let set = Arc::new(PersistentDescriptorSet::start(pipeline.clone(), 0)
|
2017-07-06 19:57:28 +00:00
|
|
|
.add_sampled_image(texture.clone(), sampler.clone()).unwrap()
|
|
|
|
.build().unwrap()
|
2017-07-06 12:53:23 +00:00
|
|
|
);
|
2016-02-24 09:47:58 +00:00
|
|
|
|
2018-10-27 21:16:30 +00:00
|
|
|
let mut dynamic_state = DynamicState { line_width: None, viewports: None, scissors: None };
|
|
|
|
let mut framebuffers = window_size_dependent_setup(&images, render_pass.clone(), &mut dynamic_state);
|
2017-07-26 15:58:40 +00:00
|
|
|
|
|
|
|
let mut recreate_swapchain = false;
|
2017-07-22 18:08:52 +00:00
|
|
|
let mut previous_frame_end = Box::new(tex_future) as Box<GpuFuture>;
|
2016-02-24 09:47:58 +00:00
|
|
|
|
|
|
|
loop {
|
2017-05-21 17:04:14 +00:00
|
|
|
previous_frame_end.cleanup_finished();
|
2017-07-26 15:58:40 +00:00
|
|
|
if recreate_swapchain {
|
2018-10-27 21:16:30 +00:00
|
|
|
let dimensions = if let Some(dimensions) = window.get_inner_size() {
|
|
|
|
let dimensions: (u32, u32) = dimensions.to_physical(window.get_hidpi_factor()).into();
|
|
|
|
[dimensions.0, dimensions.1]
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
};
|
2018-08-11 06:48:03 +00:00
|
|
|
|
2017-07-26 15:58:40 +00:00
|
|
|
let (new_swapchain, new_images) = match swapchain.recreate_with_dimension(dimensions) {
|
|
|
|
Ok(r) => r,
|
2018-10-28 03:02:29 +00:00
|
|
|
Err(SwapchainCreationError::UnsupportedDimensions) => continue,
|
2017-07-26 15:58:40 +00:00
|
|
|
Err(err) => panic!("{:?}", err)
|
|
|
|
};
|
|
|
|
|
2018-08-04 12:38:57 +00:00
|
|
|
swapchain = new_swapchain;
|
2018-10-27 21:16:30 +00:00
|
|
|
framebuffers = window_size_dependent_setup(&new_images, render_pass.clone(), &mut dynamic_state);
|
2018-08-04 12:38:33 +00:00
|
|
|
|
2017-07-26 15:58:40 +00:00
|
|
|
recreate_swapchain = false;
|
|
|
|
}
|
|
|
|
|
2018-10-28 03:02:29 +00:00
|
|
|
let (image_num, future) = match swapchain::acquire_next_image(swapchain.clone(), None) {
|
2017-07-26 15:58:40 +00:00
|
|
|
Ok(r) => r,
|
2018-10-28 03:02:29 +00:00
|
|
|
Err(AcquireError::OutOfDate) => {
|
2017-07-26 15:58:40 +00:00
|
|
|
recreate_swapchain = true;
|
|
|
|
continue;
|
2018-10-27 21:16:30 +00:00
|
|
|
}
|
2017-07-26 15:58:40 +00:00
|
|
|
Err(err) => panic!("{:?}", err)
|
|
|
|
};
|
2017-04-04 07:00:11 +00:00
|
|
|
|
2018-10-28 03:02:29 +00:00
|
|
|
let clear_values = vec!([0.0, 0.0, 1.0, 1.0].into());
|
|
|
|
let cb = AutoCommandBufferBuilder::primary_one_time_submit(device.clone(), queue.family())
|
2017-04-04 07:00:11 +00:00
|
|
|
.unwrap()
|
2018-10-28 03:02:29 +00:00
|
|
|
.begin_render_pass(framebuffers[image_num].clone(), false, clear_values).unwrap()
|
|
|
|
.draw(pipeline.clone(), &dynamic_state, vertex_buffer.clone(), set.clone(), ()).unwrap()
|
2017-04-20 07:43:57 +00:00
|
|
|
.end_render_pass().unwrap()
|
2017-04-04 15:41:51 +00:00
|
|
|
.build().unwrap();
|
2017-04-04 07:00:11 +00:00
|
|
|
|
2017-05-21 17:04:14 +00:00
|
|
|
let future = previous_frame_end.join(future)
|
2017-05-15 09:02:00 +00:00
|
|
|
.then_execute(queue.clone(), cb).unwrap()
|
2017-04-04 07:00:11 +00:00
|
|
|
.then_swapchain_present(queue.clone(), swapchain.clone(), image_num)
|
2018-02-14 07:51:52 +00:00
|
|
|
.then_signal_fence_and_flush();
|
|
|
|
|
|
|
|
match future {
|
|
|
|
Ok(future) => {
|
|
|
|
previous_frame_end = Box::new(future) as Box<_>;
|
|
|
|
}
|
2018-10-28 03:02:29 +00:00
|
|
|
Err(FlushError::OutOfDate) => {
|
2018-02-14 07:51:52 +00:00
|
|
|
recreate_swapchain = true;
|
2018-10-28 03:02:29 +00:00
|
|
|
previous_frame_end = Box::new(sync::now(device.clone())) as Box<_>;
|
2018-02-14 07:51:52 +00:00
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
println!("{:?}", e);
|
2018-10-28 03:02:29 +00:00
|
|
|
previous_frame_end = Box::new(sync::now(device.clone())) as Box<_>;
|
2018-02-14 07:51:52 +00:00
|
|
|
}
|
|
|
|
}
|
2016-02-24 09:47:58 +00:00
|
|
|
|
2017-05-08 01:57:25 +00:00
|
|
|
let mut done = false;
|
|
|
|
events_loop.poll_events(|ev| {
|
2016-02-24 09:47:58 +00:00
|
|
|
match ev {
|
2018-10-28 03:02:29 +00:00
|
|
|
Event::WindowEvent { event: WindowEvent::CloseRequested, .. } => done = true,
|
|
|
|
Event::WindowEvent { event: WindowEvent::Resized(_), .. } => recreate_swapchain = true,
|
2016-02-24 09:47:58 +00:00
|
|
|
_ => ()
|
|
|
|
}
|
2017-05-08 01:57:25 +00:00
|
|
|
});
|
|
|
|
if done { return; }
|
2016-02-24 09:47:58 +00:00
|
|
|
}
|
|
|
|
}
|
2017-06-03 10:49:50 +00:00
|
|
|
|
2018-10-28 03:02:29 +00:00
|
|
|
/// This method is called once during initialization, then again whenever the window is resized
|
2018-10-27 21:16:30 +00:00
|
|
|
fn window_size_dependent_setup(
|
|
|
|
images: &[Arc<SwapchainImage<Window>>],
|
|
|
|
render_pass: Arc<RenderPassAbstract + Send + Sync>,
|
|
|
|
dynamic_state: &mut DynamicState
|
|
|
|
) -> Vec<Arc<FramebufferAbstract + Send + Sync>> {
|
|
|
|
let dimensions = images[0].dimensions();
|
|
|
|
|
|
|
|
let viewport = Viewport {
|
|
|
|
origin: [0.0, 0.0],
|
|
|
|
dimensions: [dimensions[0] as f32, dimensions[1] as f32],
|
|
|
|
depth_range: 0.0 .. 1.0,
|
|
|
|
};
|
|
|
|
dynamic_state.viewports = Some(vec!(viewport));
|
|
|
|
|
|
|
|
images.iter().map(|image| {
|
|
|
|
Arc::new(
|
|
|
|
Framebuffer::start(render_pass.clone())
|
|
|
|
.add(image.clone()).unwrap()
|
|
|
|
.build().unwrap()
|
|
|
|
) as Arc<FramebufferAbstract + Send + Sync>
|
|
|
|
}).collect::<Vec<_>>()
|
|
|
|
}
|
|
|
|
|
2018-10-27 23:10:29 +00:00
|
|
|
mod vs {
|
|
|
|
vulkano_shaders::shader!{
|
|
|
|
ty: "vertex",
|
|
|
|
src: "
|
2017-06-03 10:49:50 +00:00
|
|
|
#version 450
|
|
|
|
|
|
|
|
layout(location = 0) in vec2 position;
|
|
|
|
layout(location = 0) out vec2 tex_coords;
|
|
|
|
|
|
|
|
void main() {
|
|
|
|
gl_Position = vec4(position, 0.0, 1.0);
|
|
|
|
tex_coords = position + vec2(0.5);
|
2018-10-26 00:15:33 +00:00
|
|
|
}"
|
2018-10-27 23:10:29 +00:00
|
|
|
}
|
2017-06-03 10:49:50 +00:00
|
|
|
}
|
|
|
|
|
2018-10-27 23:10:29 +00:00
|
|
|
mod fs {
|
|
|
|
vulkano_shaders::shader!{
|
|
|
|
ty: "fragment",
|
|
|
|
src: "
|
2017-06-03 10:49:50 +00:00
|
|
|
#version 450
|
|
|
|
|
|
|
|
layout(location = 0) in vec2 tex_coords;
|
|
|
|
layout(location = 0) out vec4 f_color;
|
|
|
|
|
|
|
|
layout(set = 0, binding = 0) uniform sampler2D tex;
|
|
|
|
|
|
|
|
void main() {
|
|
|
|
f_color = texture(tex, tex_coords);
|
2018-10-26 00:15:33 +00:00
|
|
|
}"
|
2018-10-27 23:10:29 +00:00
|
|
|
}
|
2017-06-03 10:49:50 +00:00
|
|
|
}
|