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.
|
|
|
|
|
2017-06-20 03:56:51 +00:00
|
|
|
// For the purpose of this example all unused code is allowed.
|
|
|
|
#![allow(dead_code)]
|
|
|
|
|
2016-04-23 11:24:56 +00:00
|
|
|
extern crate examples;
|
2016-02-21 07:50:15 +00:00
|
|
|
extern crate cgmath;
|
2016-02-23 12:28:46 +00:00
|
|
|
extern crate winit;
|
2016-09-11 12:11:45 +00:00
|
|
|
extern crate time;
|
2016-02-23 12:28:46 +00:00
|
|
|
|
2016-02-20 09:55:51 +00:00
|
|
|
#[macro_use]
|
|
|
|
extern crate vulkano;
|
2017-06-03 10:49:50 +00:00
|
|
|
#[macro_use]
|
|
|
|
extern crate vulkano_shader_derive;
|
2016-04-23 11:32:31 +00:00
|
|
|
extern crate vulkano_win;
|
|
|
|
|
|
|
|
use vulkano_win::VkSurfaceBuild;
|
2017-03-16 13:55:01 +00:00
|
|
|
use vulkano::sync::GpuFuture;
|
2016-02-20 09:55:51 +00:00
|
|
|
|
2016-03-23 10:15:43 +00:00
|
|
|
use std::sync::Arc;
|
2016-02-20 09:55:51 +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();
|
2016-05-21 07:47:52 +00:00
|
|
|
let instance = vulkano::instance::Instance::new(None, &extensions, None).expect("failed to create instance");
|
2016-02-20 09:55:51 +00:00
|
|
|
|
|
|
|
let physical = vulkano::instance::PhysicalDevice::enumerate(&instance)
|
|
|
|
.next().expect("no device available");
|
|
|
|
println!("Using device: {} (type: {:?})", physical.name(), physical.ty());
|
|
|
|
|
2017-07-04 11:53:32 +00:00
|
|
|
let mut events_loop = winit::EventsLoop::new();
|
2018-02-13 13:29:36 +00:00
|
|
|
let surface = winit::WindowBuilder::new().build_vk_surface(&events_loop, instance.clone()).unwrap();
|
2016-02-20 09:55:51 +00:00
|
|
|
|
2017-09-26 08:29:26 +00:00
|
|
|
let mut dimensions;
|
2017-07-26 15:58:40 +00:00
|
|
|
|
2017-05-26 08:42:03 +00:00
|
|
|
let queue = physical.queue_families().find(|&q| q.supports_graphics() &&
|
2018-02-13 13:29:36 +00:00
|
|
|
surface.is_supported(q).unwrap_or(false))
|
2016-02-20 09:55:51 +00:00
|
|
|
.expect("couldn't find a graphical queue family");
|
|
|
|
|
2016-03-10 06:47:45 +00:00
|
|
|
let device_ext = vulkano::device::DeviceExtensions {
|
|
|
|
khr_swapchain: true,
|
2016-03-30 11:44:13 +00:00
|
|
|
.. vulkano::device::DeviceExtensions::none()
|
2016-03-10 06:47:45 +00:00
|
|
|
};
|
|
|
|
|
2017-07-02 03:52:18 +00:00
|
|
|
let (device, mut queues) = vulkano::device::Device::new(physical, physical.supported_features(),
|
2016-05-21 21:22:36 +00:00
|
|
|
&device_ext, [(queue, 0.5)].iter().cloned())
|
|
|
|
.expect("failed to create device");
|
2016-05-12 15:40:31 +00:00
|
|
|
let queue = queues.next().unwrap();
|
2016-02-20 09:55:51 +00:00
|
|
|
|
2017-07-26 15:58:40 +00:00
|
|
|
let (mut swapchain, mut images) = {
|
2018-02-13 13:29:36 +00:00
|
|
|
let caps = surface.capabilities(physical).expect("failed to get surface capabilities");
|
2016-02-20 09:55:51 +00:00
|
|
|
|
2017-09-26 08:29:26 +00:00
|
|
|
dimensions = caps.current_extent.unwrap_or([1024, 768]);
|
|
|
|
|
2016-02-20 09:55:51 +00:00
|
|
|
let usage = caps.supported_usage_flags;
|
2016-04-10 09:07:07 +00:00
|
|
|
let format = caps.supported_formats[0].0;
|
2017-07-22 14:44:52 +00:00
|
|
|
let alpha = caps.supported_composite_alpha.iter().next().unwrap();
|
2016-02-20 09:55:51 +00:00
|
|
|
|
2018-02-13 13:29:36 +00:00
|
|
|
vulkano::swapchain::Swapchain::new(device.clone(), surface.clone(), caps.min_image_count, format, dimensions, 1,
|
2017-05-20 12:06:19 +00:00
|
|
|
usage, &queue, vulkano::swapchain::SurfaceTransform::Identity,
|
2017-07-22 14:44:52 +00:00
|
|
|
alpha,
|
2017-06-11 17:01:00 +00:00
|
|
|
vulkano::swapchain::PresentMode::Fifo, true, None).expect("failed to create swapchain")
|
2016-02-20 09:55:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-07-26 15:58:40 +00:00
|
|
|
let mut depth_buffer = vulkano::image::attachment::AttachmentImage::transient(device.clone(), dimensions, vulkano::format::D16Unorm).unwrap();
|
2016-02-21 10:02:51 +00:00
|
|
|
|
2016-03-23 10:15:43 +00:00
|
|
|
let vertex_buffer = vulkano::buffer::cpu_access::CpuAccessibleBuffer
|
2017-08-17 09:59:59 +00:00
|
|
|
::from_iter(device.clone(), vulkano::buffer::BufferUsage::all(), examples::VERTICES.iter().cloned())
|
2016-09-11 12:11:45 +00:00
|
|
|
.expect("failed to create buffer");
|
2016-02-21 07:38:36 +00:00
|
|
|
|
2016-03-23 10:15:43 +00:00
|
|
|
let normals_buffer = vulkano::buffer::cpu_access::CpuAccessibleBuffer
|
2017-08-17 09:59:59 +00:00
|
|
|
::from_iter(device.clone(), vulkano::buffer::BufferUsage::all(), examples::NORMALS.iter().cloned())
|
2016-09-11 12:11:45 +00:00
|
|
|
.expect("failed to create buffer");
|
2016-02-21 08:39:49 +00:00
|
|
|
|
2016-03-23 10:15:43 +00:00
|
|
|
let index_buffer = vulkano::buffer::cpu_access::CpuAccessibleBuffer
|
2017-08-17 09:59:59 +00:00
|
|
|
::from_iter(device.clone(), vulkano::buffer::BufferUsage::all(), examples::INDICES.iter().cloned())
|
2016-09-11 12:11:45 +00:00
|
|
|
.expect("failed to create buffer");
|
2016-02-20 09:55:51 +00:00
|
|
|
|
2016-02-21 08:27:39 +00:00
|
|
|
// note: this teapot was meant for OpenGL where the origin is at the lower left
|
|
|
|
// instead the origin is at the upper left in vulkan, so we reverse the Y axis
|
2017-07-26 15:58:40 +00:00
|
|
|
let mut proj = cgmath::perspective(cgmath::Rad(std::f32::consts::FRAC_PI_2), { dimensions[0] as f32 / dimensions[1] as f32 }, 0.01, 100.0);
|
2016-02-21 20:25:41 +00:00
|
|
|
let view = cgmath::Matrix4::look_at(cgmath::Point3::new(0.3, 0.3, 1.0), cgmath::Point3::new(0.0, 0.0, 0.0), cgmath::Vector3::new(0.0, -1.0, 0.0));
|
2016-02-21 07:50:15 +00:00
|
|
|
let scale = cgmath::Matrix4::from_scale(0.01);
|
|
|
|
|
2017-07-19 17:19:40 +00:00
|
|
|
let uniform_buffer = vulkano::buffer::cpu_pool::CpuBufferPool::<vs::ty::Data>
|
2017-08-17 09:59:59 +00:00
|
|
|
::new(device.clone(), vulkano::buffer::BufferUsage::all());
|
2016-02-20 13:40:50 +00:00
|
|
|
|
2017-07-03 14:53:44 +00:00
|
|
|
let vs = vs::Shader::load(device.clone()).expect("failed to create shader module");
|
|
|
|
let fs = fs::Shader::load(device.clone()).expect("failed to create shader module");
|
2016-02-20 09:55:51 +00:00
|
|
|
|
2017-03-16 13:55:01 +00:00
|
|
|
let renderpass = Arc::new(
|
|
|
|
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-03-16 13:55:01 +00:00
|
|
|
samples: 1,
|
2016-02-26 15:21:06 +00:00
|
|
|
},
|
|
|
|
depth: {
|
|
|
|
load: Clear,
|
|
|
|
store: DontCare,
|
2017-06-02 18:36:43 +00:00
|
|
|
format: vulkano::format::Format::D16Unorm,
|
2017-03-16 13:55:01 +00:00
|
|
|
samples: 1,
|
2016-02-26 15:21:06 +00:00
|
|
|
}
|
2016-02-24 12:14:51 +00:00
|
|
|
},
|
2016-02-26 15:21:06 +00:00
|
|
|
pass: {
|
|
|
|
color: [color],
|
|
|
|
depth_stencil: {depth}
|
2016-02-24 12:14:51 +00:00
|
|
|
}
|
2017-03-16 13:55:01 +00:00
|
|
|
).unwrap()
|
|
|
|
);
|
2016-02-26 15:21:06 +00:00
|
|
|
|
2017-06-10 16:55:05 +00:00
|
|
|
let pipeline = Arc::new(vulkano::pipeline::GraphicsPipeline::start()
|
|
|
|
.vertex_input(vulkano::pipeline::vertex::TwoBuffersDefinition::new())
|
2017-06-13 13:21:19 +00:00
|
|
|
.vertex_shader(vs.main_entry_point(), ())
|
2017-06-10 16:55:05 +00:00
|
|
|
.triangle_list()
|
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-06-10 16:55:05 +00:00
|
|
|
.depth_stencil_simple_depth()
|
|
|
|
.render_pass(vulkano::framebuffer::Subpass::from(renderpass.clone(), 0).unwrap())
|
|
|
|
.build(device.clone())
|
2017-07-26 15:58:40 +00:00
|
|
|
.unwrap());
|
|
|
|
let mut framebuffers: Option<Vec<Arc<vulkano::framebuffer::Framebuffer<_,_>>>> = None;
|
2016-02-20 09:55:51 +00:00
|
|
|
|
2017-07-26 15:58:40 +00:00
|
|
|
let mut recreate_swapchain = false;
|
2016-02-20 12:05:44 +00:00
|
|
|
|
2017-05-21 17:04:14 +00:00
|
|
|
let mut previous_frame = Box::new(vulkano::sync::now(device.clone())) as Box<GpuFuture>;
|
2017-07-08 10:54:23 +00:00
|
|
|
let rotation_start = std::time::Instant::now();
|
2016-09-11 12:11:45 +00:00
|
|
|
|
2016-02-20 09:55:51 +00:00
|
|
|
loop {
|
2017-05-21 17:04:14 +00:00
|
|
|
previous_frame.cleanup_finished();
|
2016-03-02 07:59:02 +00:00
|
|
|
|
2017-07-26 15:58:40 +00:00
|
|
|
if recreate_swapchain {
|
2017-09-26 08:29:26 +00:00
|
|
|
|
2018-02-13 13:29:36 +00:00
|
|
|
dimensions = surface.capabilities(physical)
|
2017-09-26 08:29:26 +00:00
|
|
|
.expect("failed to get surface capabilities")
|
|
|
|
.current_extent.unwrap_or([1024, 768]);
|
2017-07-26 15:58:40 +00:00
|
|
|
|
|
|
|
let (new_swapchain, new_images) = match swapchain.recreate_with_dimension(dimensions) {
|
|
|
|
Ok(r) => r,
|
|
|
|
Err(vulkano::swapchain::SwapchainCreationError::UnsupportedDimensions) => {
|
|
|
|
continue;
|
|
|
|
},
|
|
|
|
Err(err) => panic!("{:?}", err)
|
|
|
|
};
|
|
|
|
|
|
|
|
std::mem::replace(&mut swapchain, new_swapchain);
|
|
|
|
std::mem::replace(&mut images, new_images);
|
|
|
|
|
|
|
|
let new_depth_buffer = vulkano::image::attachment::AttachmentImage::transient(device.clone(), dimensions, vulkano::format::D16Unorm).unwrap();
|
|
|
|
std::mem::replace(&mut depth_buffer, new_depth_buffer);
|
|
|
|
|
|
|
|
framebuffers = None;
|
|
|
|
|
|
|
|
proj = cgmath::perspective(cgmath::Rad(std::f32::consts::FRAC_PI_2), { dimensions[0] as f32 / dimensions[1] as f32 }, 0.01, 100.0);
|
|
|
|
|
|
|
|
recreate_swapchain = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if framebuffers.is_none() {
|
|
|
|
let new_framebuffers = Some(images.iter().map(|image| {
|
|
|
|
Arc::new(vulkano::framebuffer::Framebuffer::start(renderpass.clone())
|
|
|
|
.add(image.clone()).unwrap()
|
|
|
|
.add(depth_buffer.clone()).unwrap()
|
|
|
|
.build().unwrap())
|
|
|
|
}).collect::<Vec<_>>());
|
|
|
|
std::mem::replace(&mut framebuffers, new_framebuffers);
|
|
|
|
}
|
|
|
|
|
2017-07-19 17:19:40 +00:00
|
|
|
let uniform_buffer_subbuffer = {
|
2017-07-08 10:54:23 +00:00
|
|
|
let elapsed = rotation_start.elapsed();
|
|
|
|
let rotation = elapsed.as_secs() as f64 + elapsed.subsec_nanos() as f64 / 1_000_000_000.0;
|
|
|
|
let rotation = cgmath::Matrix3::from_angle_y(cgmath::Rad(rotation as f32));
|
2016-09-11 12:11:45 +00:00
|
|
|
|
2017-07-19 17:19:40 +00:00
|
|
|
let uniform_data = vs::ty::Data {
|
|
|
|
world : cgmath::Matrix4::from(rotation).into(),
|
|
|
|
view : (view * scale).into(),
|
|
|
|
proj : proj.into(),
|
|
|
|
};
|
|
|
|
|
2017-09-09 06:55:20 +00:00
|
|
|
uniform_buffer.next(uniform_data).unwrap()
|
2017-07-19 17:19:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let set = Arc::new(vulkano::descriptor::descriptor_set::PersistentDescriptorSet::start(pipeline.clone(), 0)
|
|
|
|
.add_buffer(uniform_buffer_subbuffer).unwrap()
|
|
|
|
.build().unwrap()
|
|
|
|
);
|
2016-09-11 12:11:45 +00:00
|
|
|
|
2017-07-26 15:58:40 +00:00
|
|
|
let (image_num, acquire_future) = match vulkano::swapchain::acquire_next_image(swapchain.clone(),
|
|
|
|
None) {
|
|
|
|
Ok(r) => r,
|
|
|
|
Err(vulkano::swapchain::AcquireError::OutOfDate) => {
|
|
|
|
recreate_swapchain = true;
|
|
|
|
continue;
|
|
|
|
},
|
|
|
|
Err(err) => panic!("{:?}", err)
|
|
|
|
};
|
2017-03-16 13:55:01 +00:00
|
|
|
|
2017-07-11 09:33:35 +00:00
|
|
|
let command_buffer = vulkano::command_buffer::AutoCommandBufferBuilder::primary_one_time_submit(device.clone(), queue.family()).unwrap()
|
2017-03-16 13:55:01 +00:00
|
|
|
.begin_render_pass(
|
2017-07-26 15:58:40 +00:00
|
|
|
framebuffers.as_ref().unwrap()[image_num].clone(), false,
|
2017-06-02 18:36:43 +00:00
|
|
|
vec![
|
|
|
|
[0.0, 0.0, 1.0, 1.0].into(),
|
|
|
|
1f32.into()
|
|
|
|
]).unwrap()
|
2017-03-16 13:55:01 +00:00
|
|
|
.draw_indexed(
|
2017-07-26 15:58:40 +00:00
|
|
|
pipeline.clone(),
|
|
|
|
vulkano::command_buffer::DynamicState {
|
|
|
|
line_width: None,
|
|
|
|
viewports: Some(vec![vulkano::pipeline::viewport::Viewport {
|
|
|
|
origin: [0.0, 0.0],
|
|
|
|
dimensions: [dimensions[0] as f32, dimensions[1] as f32],
|
|
|
|
depth_range: 0.0 .. 1.0,
|
|
|
|
}]),
|
|
|
|
scissors: None,
|
|
|
|
},
|
2017-03-16 13:55:01 +00:00
|
|
|
(vertex_buffer.clone(), normals_buffer.clone()),
|
2017-04-20 07:43:57 +00:00
|
|
|
index_buffer.clone(), set.clone(), ()).unwrap()
|
|
|
|
.end_render_pass().unwrap()
|
2017-04-04 15:41:51 +00:00
|
|
|
.build().unwrap();
|
2017-03-16 13:55:01 +00:00
|
|
|
|
2017-05-21 17:04:14 +00:00
|
|
|
let future = previous_frame.join(acquire_future)
|
2017-05-15 09:02:00 +00:00
|
|
|
.then_execute(queue.clone(), command_buffer).unwrap()
|
2017-03-16 13:55:01 +00:00
|
|
|
.then_swapchain_present(queue.clone(), swapchain.clone(), image_num)
|
2017-05-06 18:10:16 +00:00
|
|
|
.then_signal_fence_and_flush().unwrap();
|
2017-05-21 17:04:14 +00:00
|
|
|
previous_frame = Box::new(future) as Box<_>;
|
2016-02-20 09:55:51 +00:00
|
|
|
|
2017-05-08 01:57:25 +00:00
|
|
|
let mut done = false;
|
|
|
|
events_loop.poll_events(|ev| {
|
2016-02-23 12:28:46 +00:00
|
|
|
match ev {
|
2017-05-08 01:57:25 +00:00
|
|
|
winit::Event::WindowEvent { event: winit::WindowEvent::Closed, .. } => done = true,
|
2016-02-23 12:28:46 +00:00
|
|
|
_ => ()
|
2016-02-20 09:55:51 +00:00
|
|
|
}
|
2017-05-08 01:57:25 +00:00
|
|
|
});
|
|
|
|
if done { return; }
|
2016-02-20 09:55:51 +00:00
|
|
|
}
|
|
|
|
}
|
2017-06-03 10:49:50 +00:00
|
|
|
|
|
|
|
mod vs {
|
|
|
|
#[derive(VulkanoShader)]
|
|
|
|
#[ty = "vertex"]
|
|
|
|
#[src = "
|
|
|
|
#version 450
|
|
|
|
|
|
|
|
layout(location = 0) in vec3 position;
|
|
|
|
layout(location = 1) in vec3 normal;
|
|
|
|
|
|
|
|
layout(location = 0) out vec3 v_normal;
|
|
|
|
|
|
|
|
layout(set = 0, binding = 0) uniform Data {
|
|
|
|
mat4 world;
|
|
|
|
mat4 view;
|
|
|
|
mat4 proj;
|
|
|
|
} uniforms;
|
|
|
|
|
|
|
|
void main() {
|
|
|
|
mat4 worldview = uniforms.view * uniforms.world;
|
|
|
|
v_normal = transpose(inverse(mat3(worldview))) * normal;
|
|
|
|
gl_Position = uniforms.proj * worldview * vec4(position, 1.0);
|
|
|
|
}
|
|
|
|
"]
|
|
|
|
struct Dummy;
|
|
|
|
}
|
|
|
|
|
|
|
|
mod fs {
|
|
|
|
#[derive(VulkanoShader)]
|
|
|
|
#[ty = "fragment"]
|
|
|
|
#[src = "
|
|
|
|
#version 450
|
|
|
|
|
|
|
|
layout(location = 0) in vec3 v_normal;
|
|
|
|
layout(location = 0) out vec4 f_color;
|
|
|
|
|
|
|
|
const vec3 LIGHT = vec3(0.0, 0.0, 1.0);
|
|
|
|
|
|
|
|
void main() {
|
|
|
|
float brightness = dot(normalize(v_normal), normalize(LIGHT));
|
|
|
|
vec3 dark_color = vec3(0.6, 0.0, 0.0);
|
|
|
|
vec3 regular_color = vec3(1.0, 0.0, 0.0);
|
|
|
|
|
|
|
|
f_color = vec4(mix(dark_color, regular_color, brightness), 1.0);
|
|
|
|
}
|
|
|
|
"]
|
|
|
|
struct Dummy;
|
|
|
|
}
|