2017-07-22 19:47:53 +00:00
|
|
|
// 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.
|
|
|
|
//
|
2018-09-02 04:18:22 +00:00
|
|
|
// This example demonstrates one way of preparing data structures and loading
|
2017-07-20 13:58:26 +00:00
|
|
|
// SPIRV shaders from external source (file system).
|
|
|
|
//
|
|
|
|
// Note that you will need to do all correctness checking by yourself.
|
|
|
|
//
|
2018-08-30 01:37:51 +00:00
|
|
|
// vert.glsl and frag.glsl must be built by yourself.
|
2017-07-20 13:58:26 +00:00
|
|
|
// One way of building them is to build Khronos' glslang and use
|
|
|
|
// glslangValidator tool:
|
2018-08-30 01:37:51 +00:00
|
|
|
// $ glslangValidator vert.glsl -V -S vert -o vert.spv
|
|
|
|
// $ glslangValidator frag.glsl -V -S frag -o frag.spv
|
2017-07-20 13:58:26 +00:00
|
|
|
// Vulkano uses glslangValidator to build your shaders internally.
|
|
|
|
|
|
|
|
use vulkano as vk;
|
|
|
|
use vulkano::buffer::BufferUsage;
|
|
|
|
use vulkano::buffer::cpu_access::CpuAccessibleBuffer;
|
|
|
|
use vulkano::command_buffer::AutoCommandBufferBuilder;
|
|
|
|
use vulkano::command_buffer::DynamicState;
|
|
|
|
use vulkano::descriptor::descriptor::DescriptorDesc;
|
|
|
|
use vulkano::descriptor::descriptor::ShaderStages;
|
|
|
|
use vulkano::descriptor::pipeline_layout::PipelineLayoutDesc;
|
|
|
|
use vulkano::descriptor::pipeline_layout::PipelineLayoutDescPcRange;
|
|
|
|
use vulkano::device::Device;
|
|
|
|
use vulkano::device::DeviceExtensions;
|
2018-10-28 03:02:29 +00:00
|
|
|
use vulkano::format::Format;
|
2018-10-27 21:16:30 +00:00
|
|
|
use vulkano::framebuffer::{Framebuffer, FramebufferAbstract, Subpass, RenderPassAbstract};
|
|
|
|
use vulkano::image::SwapchainImage;
|
2017-07-20 13:58:26 +00:00
|
|
|
use vulkano::pipeline::GraphicsPipeline;
|
2018-10-27 21:16:30 +00:00
|
|
|
use vulkano::pipeline::shader::{GraphicsShaderType, ShaderInterfaceDef, ShaderInterfaceDefEntry, ShaderModule};
|
2017-07-20 13:58:26 +00:00
|
|
|
use vulkano::pipeline::vertex::SingleBufferDefinition;
|
|
|
|
use vulkano::pipeline::viewport::Viewport;
|
2019-11-05 10:03:46 +00:00
|
|
|
use vulkano::swapchain::{AcquireError, PresentMode, SurfaceTransform, Swapchain, SwapchainCreationError, ColorSpace};
|
2018-10-27 21:16:30 +00:00
|
|
|
use vulkano::swapchain;
|
2020-01-23 07:37:12 +00:00
|
|
|
use vulkano::sync::{GpuFuture, FlushError};
|
2018-10-27 21:16:30 +00:00
|
|
|
use vulkano::sync;
|
2020-01-23 07:37:12 +00:00
|
|
|
use vulkano::instance::Instance;
|
2017-07-20 13:58:26 +00:00
|
|
|
|
|
|
|
use vulkano_win::VkSurfaceBuild;
|
2020-01-23 07:37:12 +00:00
|
|
|
use winit::window::{WindowBuilder, Window};
|
|
|
|
use winit::event_loop::{EventLoop, ControlFlow};
|
|
|
|
use winit::event::{Event, WindowEvent};
|
2018-10-27 21:16:30 +00:00
|
|
|
|
2017-07-20 13:58:26 +00:00
|
|
|
use std::borrow::Cow;
|
|
|
|
use std::ffi::CStr;
|
|
|
|
use std::fs::File;
|
|
|
|
use std::io::Read;
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
2019-07-01 21:02:48 +00:00
|
|
|
#[derive(Default, Copy, Clone)]
|
2017-07-20 13:58:26 +00:00
|
|
|
pub struct Vertex {
|
|
|
|
pub position: [f32; 2],
|
|
|
|
pub color: [f32; 3],
|
|
|
|
}
|
|
|
|
|
2018-12-07 12:35:19 +00:00
|
|
|
vulkano::impl_vertex!(Vertex, position, color);
|
2017-07-20 13:58:26 +00:00
|
|
|
|
|
|
|
fn main() {
|
2020-01-23 07:37:12 +00:00
|
|
|
let required_extensions = vulkano_win::required_extensions();
|
|
|
|
let instance = Instance::new(None, &required_extensions, None).unwrap();
|
2018-10-28 03:02:29 +00:00
|
|
|
let physical = vk::instance::PhysicalDevice::enumerate(&instance).next().unwrap();
|
|
|
|
|
2020-01-23 07:37:12 +00:00
|
|
|
let event_loop = EventLoop::new();
|
|
|
|
let surface = WindowBuilder::new().build_vk_surface(&event_loop, instance.clone()).unwrap();
|
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();
|
2018-10-27 21:16:30 +00:00
|
|
|
let (device, mut queues) = {
|
2018-10-28 03:02:29 +00:00
|
|
|
let device_ext = DeviceExtensions { khr_swapchain: true, .. DeviceExtensions::none() };
|
|
|
|
Device::new(physical, physical.supported_features(), &device_ext,
|
|
|
|
[(queue_family, 0.5)].iter().cloned()).unwrap()
|
|
|
|
};
|
|
|
|
let queue = queues.next().unwrap();
|
|
|
|
|
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();
|
|
|
|
let usage = caps.supported_usage_flags;
|
2018-10-27 21:16:30 +00:00
|
|
|
let alpha = caps.supported_composite_alpha.iter().next().unwrap();
|
2017-07-20 13:58:26 +00:00
|
|
|
let format = caps.supported_formats[0].0;
|
2020-01-23 07:37:12 +00:00
|
|
|
let dimensions: [u32; 2] = surface.window().inner_size().into();
|
2017-07-20 13:58:26 +00:00
|
|
|
|
2020-01-23 07:37:12 +00:00
|
|
|
Swapchain::new(device.clone(), surface.clone(), caps.min_image_count, format, dimensions,
|
2019-11-05 10:03:46 +00:00
|
|
|
1, usage, &queue, SurfaceTransform::Identity, alpha, PresentMode::Fifo, true, ColorSpace::SrgbNonLinear).unwrap()
|
2017-07-20 13:58:26 +00:00
|
|
|
};
|
|
|
|
|
2018-12-07 12:35:19 +00:00
|
|
|
let render_pass = Arc::new(vulkano::single_pass_renderpass!(
|
2018-10-28 03:02:29 +00:00
|
|
|
device.clone(),
|
|
|
|
attachments: {
|
|
|
|
color: {
|
|
|
|
load: Clear,
|
|
|
|
store: Store,
|
|
|
|
format: swapchain.format(),
|
|
|
|
samples: 1,
|
2017-07-20 13:58:26 +00:00
|
|
|
}
|
2018-10-28 03:02:29 +00:00
|
|
|
},
|
|
|
|
pass: {
|
|
|
|
color: [color],
|
|
|
|
depth_stencil: {}
|
|
|
|
}
|
|
|
|
).unwrap());
|
2017-07-20 13:58:26 +00:00
|
|
|
|
|
|
|
let vs = {
|
2018-08-30 01:37:51 +00:00
|
|
|
let mut f = File::open("src/bin/runtime-shader/vert.spv")
|
|
|
|
.expect("Can't find file src/bin/runtime-shader/vert.spv This example needs to be run from the root of the example crate.");
|
2017-07-20 13:58:26 +00:00
|
|
|
let mut v = vec![];
|
|
|
|
f.read_to_end(&mut v).unwrap();
|
|
|
|
// Create a ShaderModule on a device the same Shader::load does it.
|
|
|
|
// NOTE: You will have to verify correctness of the data by yourself!
|
2018-10-27 21:16:30 +00:00
|
|
|
unsafe { ShaderModule::new(device.clone(), &v) }.unwrap()
|
2017-07-20 13:58:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let fs = {
|
2018-08-30 01:37:51 +00:00
|
|
|
let mut f = File::open("src/bin/runtime-shader/frag.spv")
|
|
|
|
.expect("Can't find file src/bin/runtime-shader/frag.spv");
|
2017-07-20 13:58:26 +00:00
|
|
|
let mut v = vec![];
|
|
|
|
f.read_to_end(&mut v).unwrap();
|
2018-10-27 21:16:30 +00:00
|
|
|
unsafe { ShaderModule::new(device.clone(), &v) }.unwrap()
|
2017-07-20 13:58:26 +00:00
|
|
|
};
|
|
|
|
|
2018-10-28 03:02:29 +00:00
|
|
|
// This structure will tell Vulkan how input entries of our vertex shader look like
|
2017-07-20 13:58:26 +00:00
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
|
|
|
|
struct VertInput;
|
2018-10-28 03:02:29 +00:00
|
|
|
|
2017-07-20 13:58:26 +00:00
|
|
|
unsafe impl ShaderInterfaceDef for VertInput {
|
|
|
|
type Iter = VertInputIter;
|
|
|
|
|
|
|
|
fn elements(&self) -> VertInputIter {
|
|
|
|
VertInputIter(0)
|
|
|
|
}
|
|
|
|
}
|
2018-10-28 03:02:29 +00:00
|
|
|
|
2017-07-20 13:58:26 +00:00
|
|
|
#[derive(Debug, Copy, Clone)]
|
|
|
|
struct VertInputIter(u16);
|
2018-10-28 03:02:29 +00:00
|
|
|
|
2017-07-20 13:58:26 +00:00
|
|
|
impl Iterator for VertInputIter {
|
|
|
|
type Item = ShaderInterfaceDefEntry;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
// There are things to consider when giving out entries:
|
|
|
|
// * There must be only one entry per one location, you can't have
|
|
|
|
// `color' and `position' entries both at 0..1 locations. They also
|
|
|
|
// should not overlap.
|
|
|
|
// * Format of each element must be no larger than 128 bits.
|
|
|
|
if self.0 == 0 {
|
|
|
|
self.0 += 1;
|
|
|
|
return Some(ShaderInterfaceDefEntry {
|
|
|
|
location: 1..2,
|
2018-10-28 03:02:29 +00:00
|
|
|
format: Format::R32G32B32Sfloat,
|
2017-07-20 13:58:26 +00:00
|
|
|
name: Some(Cow::Borrowed("color"))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if self.0 == 1 {
|
|
|
|
self.0 += 1;
|
|
|
|
return Some(ShaderInterfaceDefEntry {
|
|
|
|
location: 0..1,
|
2018-10-28 03:02:29 +00:00
|
|
|
format: Format::R32G32Sfloat,
|
2017-07-20 13:58:26 +00:00
|
|
|
name: Some(Cow::Borrowed("position"))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
2018-10-28 03:02:29 +00:00
|
|
|
|
2017-07-20 13:58:26 +00:00
|
|
|
#[inline]
|
|
|
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
|
|
|
// We must return exact number of entries left in iterator.
|
|
|
|
let len = (2 - self.0) as usize;
|
|
|
|
(len, Some(len))
|
|
|
|
}
|
|
|
|
}
|
2018-10-28 03:02:29 +00:00
|
|
|
|
|
|
|
impl ExactSizeIterator for VertInputIter { }
|
|
|
|
|
2017-07-20 13:58:26 +00:00
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
|
|
|
|
struct VertOutput;
|
2018-10-28 03:02:29 +00:00
|
|
|
|
2017-07-20 13:58:26 +00:00
|
|
|
unsafe impl ShaderInterfaceDef for VertOutput {
|
|
|
|
type Iter = VertOutputIter;
|
|
|
|
|
|
|
|
fn elements(&self) -> VertOutputIter {
|
|
|
|
VertOutputIter(0)
|
|
|
|
}
|
|
|
|
}
|
2018-10-28 03:02:29 +00:00
|
|
|
|
2017-07-20 13:58:26 +00:00
|
|
|
// This structure will tell Vulkan how output entries (those passed to next
|
|
|
|
// stage) of our vertex shader look like.
|
|
|
|
#[derive(Debug, Copy, Clone)]
|
|
|
|
struct VertOutputIter(u16);
|
2018-10-28 03:02:29 +00:00
|
|
|
|
2017-07-20 13:58:26 +00:00
|
|
|
impl Iterator for VertOutputIter {
|
|
|
|
type Item = ShaderInterfaceDefEntry;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
if self.0 == 0 {
|
|
|
|
self.0 += 1;
|
|
|
|
return Some(ShaderInterfaceDefEntry {
|
|
|
|
location: 0..1,
|
2018-10-28 03:02:29 +00:00
|
|
|
format: Format::R32G32B32Sfloat,
|
2017-07-20 13:58:26 +00:00
|
|
|
name: Some(Cow::Borrowed("v_color"))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
2018-10-28 03:02:29 +00:00
|
|
|
|
2017-07-20 13:58:26 +00:00
|
|
|
#[inline]
|
|
|
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
|
|
|
let len = (1 - self.0) as usize;
|
|
|
|
(len, Some(len))
|
|
|
|
}
|
|
|
|
}
|
2018-10-28 03:02:29 +00:00
|
|
|
|
|
|
|
impl ExactSizeIterator for VertOutputIter { }
|
|
|
|
|
2017-07-20 13:58:26 +00:00
|
|
|
// This structure describes layout of this stage.
|
|
|
|
#[derive(Debug, Copy, Clone)]
|
|
|
|
struct VertLayout(ShaderStages);
|
|
|
|
unsafe impl PipelineLayoutDesc for VertLayout {
|
|
|
|
// Number of descriptor sets it takes.
|
2017-08-23 13:45:37 +00:00
|
|
|
fn num_sets(&self) -> usize { 0 }
|
2017-07-20 13:58:26 +00:00
|
|
|
// Number of entries (bindings) in each set.
|
2018-10-28 03:02:29 +00:00
|
|
|
fn num_bindings_in_set(&self, _set: usize) -> Option<usize> { None }
|
2017-07-20 13:58:26 +00:00
|
|
|
// Descriptor descriptions.
|
2018-10-28 03:02:29 +00:00
|
|
|
fn descriptor(&self, _set: usize, _binding: usize) -> Option<DescriptorDesc> { None }
|
2017-07-20 13:58:26 +00:00
|
|
|
// Number of push constants ranges (think: number of push constants).
|
|
|
|
fn num_push_constants_ranges(&self) -> usize { 0 }
|
|
|
|
// Each push constant range in memory.
|
2018-10-28 03:02:29 +00:00
|
|
|
fn push_constants_range(&self, _num: usize) -> Option<PipelineLayoutDescPcRange> { None }
|
2017-07-20 13:58:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Same as with our vertex shader, but for fragment one instead.
|
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
|
|
|
|
struct FragInput;
|
|
|
|
unsafe impl ShaderInterfaceDef for FragInput {
|
|
|
|
type Iter = FragInputIter;
|
|
|
|
|
|
|
|
fn elements(&self) -> FragInputIter {
|
|
|
|
FragInputIter(0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#[derive(Debug, Copy, Clone)]
|
|
|
|
struct FragInputIter(u16);
|
2018-10-28 03:02:29 +00:00
|
|
|
|
2017-07-20 13:58:26 +00:00
|
|
|
impl Iterator for FragInputIter {
|
|
|
|
type Item = ShaderInterfaceDefEntry;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
if self.0 == 0 {
|
|
|
|
self.0 += 1;
|
|
|
|
return Some(ShaderInterfaceDefEntry {
|
|
|
|
location: 0..1,
|
2018-10-28 03:02:29 +00:00
|
|
|
format: Format::R32G32B32Sfloat,
|
2017-07-20 13:58:26 +00:00
|
|
|
name: Some(Cow::Borrowed("v_color"))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
2018-10-28 03:02:29 +00:00
|
|
|
|
2017-07-20 13:58:26 +00:00
|
|
|
#[inline]
|
|
|
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
|
|
|
let len = (1 - self.0) as usize;
|
|
|
|
(len, Some(len))
|
|
|
|
}
|
|
|
|
}
|
2018-10-28 03:02:29 +00:00
|
|
|
|
|
|
|
impl ExactSizeIterator for FragInputIter { }
|
|
|
|
|
2017-07-20 13:58:26 +00:00
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
|
|
|
|
struct FragOutput;
|
|
|
|
unsafe impl ShaderInterfaceDef for FragOutput {
|
|
|
|
type Iter = FragOutputIter;
|
|
|
|
|
|
|
|
fn elements(&self) -> FragOutputIter {
|
|
|
|
FragOutputIter(0)
|
|
|
|
}
|
|
|
|
}
|
2018-10-28 03:02:29 +00:00
|
|
|
|
2017-07-20 13:58:26 +00:00
|
|
|
#[derive(Debug, Copy, Clone)]
|
|
|
|
struct FragOutputIter(u16);
|
2018-10-28 03:02:29 +00:00
|
|
|
|
2017-07-20 13:58:26 +00:00
|
|
|
impl Iterator for FragOutputIter {
|
|
|
|
type Item = ShaderInterfaceDefEntry;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
// Note that color fragment color entry will be determined
|
|
|
|
// automatically by Vulkano.
|
|
|
|
if self.0 == 0 {
|
|
|
|
self.0 += 1;
|
|
|
|
return Some(ShaderInterfaceDefEntry {
|
|
|
|
location: 0..1,
|
2018-10-28 03:02:29 +00:00
|
|
|
format: Format::R32G32B32A32Sfloat,
|
2017-07-20 13:58:26 +00:00
|
|
|
name: Some(Cow::Borrowed("f_color"))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
#[inline]
|
|
|
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
|
|
|
let len = (1 - self.0) as usize;
|
|
|
|
(len, Some(len))
|
|
|
|
}
|
|
|
|
}
|
2018-10-28 03:02:29 +00:00
|
|
|
|
|
|
|
impl ExactSizeIterator for FragOutputIter { }
|
|
|
|
|
2017-07-20 13:58:26 +00:00
|
|
|
// Layout same as with vertex shader.
|
|
|
|
#[derive(Debug, Copy, Clone)]
|
|
|
|
struct FragLayout(ShaderStages);
|
|
|
|
unsafe impl PipelineLayoutDesc for FragLayout {
|
2017-08-23 13:45:37 +00:00
|
|
|
fn num_sets(&self) -> usize { 0 }
|
2018-10-28 03:02:29 +00:00
|
|
|
fn num_bindings_in_set(&self, _set: usize) -> Option<usize> { None }
|
|
|
|
fn descriptor(&self, _set: usize, _binding: usize) -> Option<DescriptorDesc> { None }
|
2017-07-20 13:58:26 +00:00
|
|
|
fn num_push_constants_ranges(&self) -> usize { 0 }
|
2018-10-28 03:02:29 +00:00
|
|
|
fn push_constants_range(&self, _num: usize) -> Option<PipelineLayoutDescPcRange> { None }
|
2017-07-20 13:58:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: ShaderModule::*_shader_entry_point calls do not do any error
|
|
|
|
// checking and you have to verify correctness of what you are doing by
|
|
|
|
// yourself.
|
|
|
|
//
|
|
|
|
// You must be extra careful to specify correct entry point, or program will
|
|
|
|
// crash at runtime outside of rust and you will get NO meaningful error
|
|
|
|
// information!
|
2017-08-02 08:42:30 +00:00
|
|
|
let vert_main = unsafe { vs.graphics_entry_point(
|
2017-07-20 13:58:26 +00:00
|
|
|
CStr::from_bytes_with_nul_unchecked(b"main\0"),
|
|
|
|
VertInput,
|
|
|
|
VertOutput,
|
2017-08-02 08:42:30 +00:00
|
|
|
VertLayout(ShaderStages { vertex: true, ..ShaderStages::none() }),
|
|
|
|
GraphicsShaderType::Vertex
|
2017-07-20 13:58:26 +00:00
|
|
|
) };
|
|
|
|
|
2017-08-02 08:42:30 +00:00
|
|
|
let frag_main = unsafe { fs.graphics_entry_point(
|
2017-07-20 13:58:26 +00:00
|
|
|
CStr::from_bytes_with_nul_unchecked(b"main\0"),
|
|
|
|
FragInput,
|
|
|
|
FragOutput,
|
2017-08-02 08:42:30 +00:00
|
|
|
FragLayout(ShaderStages { fragment: true, ..ShaderStages::none() }),
|
|
|
|
GraphicsShaderType::Fragment
|
2017-07-20 13:58:26 +00:00
|
|
|
) };
|
|
|
|
|
|
|
|
let graphics_pipeline = Arc::new(
|
|
|
|
GraphicsPipeline::start()
|
|
|
|
.vertex_input(SingleBufferDefinition::<Vertex>::new())
|
|
|
|
.vertex_shader(vert_main, ())
|
|
|
|
.triangle_list()
|
2018-10-27 21:16:30 +00:00
|
|
|
.viewports_dynamic_scissors_irrelevant(1)
|
2017-07-20 13:58:26 +00:00
|
|
|
.fragment_shader(frag_main, ())
|
|
|
|
.cull_mode_front()
|
|
|
|
.front_face_counter_clockwise()
|
|
|
|
.depth_stencil_disabled()
|
2018-10-27 21:16:30 +00:00
|
|
|
.render_pass(Subpass::from(render_pass.clone(), 0).unwrap())
|
|
|
|
.build(device.clone())
|
2017-07-20 13:58:26 +00:00
|
|
|
.unwrap(),
|
|
|
|
);
|
|
|
|
|
2018-10-27 21:16:30 +00:00
|
|
|
let mut recreate_swapchain = false;
|
|
|
|
|
2017-07-20 13:58:26 +00:00
|
|
|
let vertex_buffer = CpuAccessibleBuffer::from_iter(
|
2018-10-27 21:16:30 +00:00
|
|
|
device.clone(),
|
2017-07-20 13:58:26 +00:00
|
|
|
BufferUsage::all(),
|
|
|
|
[
|
|
|
|
Vertex { position: [-1.0, 1.0], color: [1.0, 0.0, 0.0] },
|
|
|
|
Vertex { position: [ 0.0, -1.0], color: [0.0, 1.0, 0.0] },
|
|
|
|
Vertex { position: [ 1.0, 1.0], color: [0.0, 0.0, 1.0] },
|
|
|
|
].iter().cloned()
|
2018-10-28 03:02:29 +00:00
|
|
|
).unwrap();
|
2017-07-20 13:58:26 +00:00
|
|
|
|
|
|
|
// NOTE: We don't create any descriptor sets in this example, but you should
|
|
|
|
// note that passing wrong types, providing sets at wrong indexes will cause
|
|
|
|
// descriptor set builder to return Err!
|
|
|
|
|
2019-10-18 15:23:05 +00:00
|
|
|
let mut dynamic_state = DynamicState { line_width: None, viewports: None, scissors: None, compare_mask: None, write_mask: None, reference: None };
|
2018-10-27 21:16:30 +00:00
|
|
|
let mut framebuffers = window_size_dependent_setup(&images, render_pass.clone(), &mut dynamic_state);
|
2020-01-23 07:37:12 +00:00
|
|
|
let mut previous_frame_end = Some(Box::new(sync::now(device.clone())) as Box<dyn GpuFuture>);
|
2018-10-27 21:16:30 +00:00
|
|
|
|
2020-01-23 07:37:12 +00:00
|
|
|
event_loop.run(move |event, _, control_flow| {
|
|
|
|
match event {
|
|
|
|
Event::WindowEvent { event: WindowEvent::CloseRequested, .. } => {
|
|
|
|
*control_flow = ControlFlow::Exit;
|
2018-10-27 21:16:30 +00:00
|
|
|
},
|
2020-01-23 07:37:12 +00:00
|
|
|
Event::WindowEvent { event: WindowEvent::Resized(_), .. } => {
|
2018-10-27 21:16:30 +00:00
|
|
|
recreate_swapchain = true;
|
2020-01-23 07:37:12 +00:00
|
|
|
},
|
|
|
|
Event::RedrawEventsCleared => {
|
|
|
|
previous_frame_end.as_mut().unwrap().cleanup_finished();
|
|
|
|
|
|
|
|
if recreate_swapchain {
|
|
|
|
let dimensions: [u32; 2] = surface.window().inner_size().into();
|
|
|
|
let (new_swapchain, new_images) = match swapchain.recreate_with_dimension(dimensions) {
|
|
|
|
Ok(r) => r,
|
|
|
|
Err(SwapchainCreationError::UnsupportedDimensions) => return,
|
|
|
|
Err(e) => panic!("Failed to recreate swapchain: {:?}", e)
|
|
|
|
};
|
|
|
|
|
|
|
|
swapchain = new_swapchain;
|
|
|
|
framebuffers = window_size_dependent_setup(&new_images, render_pass.clone(), &mut dynamic_state);
|
|
|
|
recreate_swapchain = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
let (image_num, acquire_future) = match swapchain::acquire_next_image(swapchain.clone(), None) {
|
|
|
|
Ok(r) => r,
|
|
|
|
Err(AcquireError::OutOfDate) => {
|
|
|
|
recreate_swapchain = true;
|
|
|
|
return;
|
|
|
|
},
|
|
|
|
Err(e) => panic!("Failed to acquire next image: {:?}", e)
|
|
|
|
};
|
|
|
|
|
|
|
|
let clear_values = vec!([0.0, 0.0, 0.0, 1.0].into());
|
|
|
|
let command_buffer = AutoCommandBufferBuilder::new(device.clone(), queue.family()).unwrap()
|
|
|
|
.begin_render_pass(framebuffers[image_num].clone(), false, clear_values).unwrap()
|
|
|
|
.draw(graphics_pipeline.clone(), &dynamic_state, vertex_buffer.clone(), (), ()).unwrap()
|
|
|
|
.end_render_pass().unwrap()
|
|
|
|
.build().unwrap();
|
|
|
|
|
|
|
|
let future = previous_frame_end.take().unwrap()
|
|
|
|
.join(acquire_future)
|
|
|
|
.then_execute(queue.clone(), command_buffer).unwrap()
|
|
|
|
.then_swapchain_present(queue.clone(), swapchain.clone(), image_num)
|
|
|
|
.then_signal_fence_and_flush();
|
|
|
|
|
|
|
|
match future {
|
|
|
|
Ok(future) => {
|
|
|
|
previous_frame_end = Some(Box::new(future) as Box<_>);
|
|
|
|
},
|
|
|
|
Err(FlushError::OutOfDate) => {
|
|
|
|
recreate_swapchain = true;
|
|
|
|
previous_frame_end = Some(Box::new(sync::now(device.clone())) as Box<_>);
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
println!("Failed to flush future: {:?}", e);
|
|
|
|
previous_frame_end = Some(Box::new(sync::now(device.clone())) as Box<_>);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => ()
|
2018-10-27 21:16:30 +00:00
|
|
|
}
|
2020-01-23 07:37:12 +00:00
|
|
|
});
|
2017-07-20 13:58:26 +00:00
|
|
|
}
|
2018-10-27 21:16:30 +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>>],
|
2019-07-02 08:25:58 +00:00
|
|
|
render_pass: Arc<dyn RenderPassAbstract + Send + Sync>,
|
2018-10-27 21:16:30 +00:00
|
|
|
dynamic_state: &mut DynamicState
|
2019-07-02 08:25:58 +00:00
|
|
|
) -> Vec<Arc<dyn FramebufferAbstract + Send + Sync>> {
|
2018-10-27 21:16:30 +00:00
|
|
|
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()
|
2019-07-02 08:25:58 +00:00
|
|
|
) as Arc<dyn FramebufferAbstract + Send + Sync>
|
2018-10-27 21:16:30 +00:00
|
|
|
}).collect::<Vec<_>>()
|
|
|
|
}
|