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.
|
|
|
|
|
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;
|
2016-04-23 11:32:31 +00:00
|
|
|
extern crate vulkano_win;
|
|
|
|
|
|
|
|
use vulkano_win::VkSurfaceBuild;
|
2016-10-06 10:03:22 +00:00
|
|
|
use vulkano::command_buffer::CommandsList;
|
2016-02-20 09:55:51 +00:00
|
|
|
|
2016-03-23 10:15:43 +00:00
|
|
|
use std::sync::Arc;
|
2016-04-10 11:31:04 +00:00
|
|
|
use std::time::Duration;
|
2016-02-20 09:55:51 +00:00
|
|
|
|
2016-04-23 11:24:56 +00:00
|
|
|
mod vs { include!{concat!(env!("OUT_DIR"), "/shaders/src/bin/teapot_vs.glsl")} }
|
|
|
|
mod fs { include!{concat!(env!("OUT_DIR"), "/shaders/src/bin/teapot_fs.glsl")} }
|
2016-03-01 20:10:59 +00:00
|
|
|
|
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());
|
|
|
|
|
2016-04-23 11:32:31 +00:00
|
|
|
let window = winit::WindowBuilder::new().build_vk_surface(&instance).unwrap();
|
2016-02-20 09:55:51 +00:00
|
|
|
|
|
|
|
let queue = physical.queue_families().find(|q| q.supports_graphics() &&
|
2016-04-23 11:32:31 +00:00
|
|
|
window.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
|
|
|
};
|
|
|
|
|
2016-05-12 15:40:31 +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
|
|
|
|
|
|
|
let (swapchain, images) = {
|
2016-04-23 11:32:31 +00:00
|
|
|
let caps = window.surface().get_capabilities(&physical).expect("failed to get surface capabilities");
|
2016-02-20 09:55:51 +00:00
|
|
|
|
|
|
|
let dimensions = caps.current_extent.unwrap_or([1280, 1024]);
|
2016-05-03 08:54:50 +00:00
|
|
|
let present = caps.present_modes.iter().next().unwrap();
|
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;
|
2016-02-20 09:55:51 +00:00
|
|
|
|
2016-04-23 11:32:31 +00:00
|
|
|
vulkano::swapchain::Swapchain::new(&device, &window.surface(), 3, format, dimensions, 1,
|
2016-02-20 09:55:51 +00:00
|
|
|
&usage, &queue, vulkano::swapchain::SurfaceTransform::Identity,
|
|
|
|
vulkano::swapchain::CompositeAlpha::Opaque,
|
2016-04-14 18:33:44 +00:00
|
|
|
present, true, None).expect("failed to create swapchain")
|
2016-02-20 09:55:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-04-02 07:38:12 +00:00
|
|
|
let depth_buffer = vulkano::image::attachment::AttachmentImage::transient(&device, images[0].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
|
2016-09-11 12:11:45 +00:00
|
|
|
::from_iter(&device, &vulkano::buffer::BufferUsage::all(), Some(queue.family()), examples::VERTICES.iter().cloned())
|
|
|
|
.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
|
2016-09-11 12:11:45 +00:00
|
|
|
::from_iter(&device, &vulkano::buffer::BufferUsage::all(), Some(queue.family()), examples::NORMALS.iter().cloned())
|
|
|
|
.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
|
2016-09-11 12:11:45 +00:00
|
|
|
::from_iter(&device, &vulkano::buffer::BufferUsage::all(), Some(queue.family()), examples::INDICES.iter().cloned())
|
|
|
|
.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
|
2016-03-31 17:43:12 +00:00
|
|
|
let proj = cgmath::perspective(cgmath::rad(3.141592 / 2.0), { let d = images[0].dimensions(); d[0] as f32 / d[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);
|
|
|
|
|
2016-03-23 10:15:43 +00:00
|
|
|
let uniform_buffer = vulkano::buffer::cpu_access::CpuAccessibleBuffer::<vs::ty::Data>
|
2016-09-11 12:11:45 +00:00
|
|
|
::from_data(&device, &vulkano::buffer::BufferUsage::all(), Some(queue.family()),
|
|
|
|
vs::ty::Data {
|
|
|
|
world : <cgmath::Matrix4<f32> as cgmath::SquareMatrix>::identity().into(),
|
|
|
|
view : (view * scale).into(),
|
|
|
|
proj : proj.into(),
|
|
|
|
})
|
2016-02-20 13:40:50 +00:00
|
|
|
.expect("failed to create buffer");
|
|
|
|
|
2016-02-25 07:52:22 +00:00
|
|
|
let vs = vs::Shader::load(&device).expect("failed to create shader module");
|
|
|
|
let fs = fs::Shader::load(&device).expect("failed to create shader module");
|
2016-02-20 09:55:51 +00:00
|
|
|
|
2016-02-26 15:21:06 +00:00
|
|
|
mod renderpass {
|
|
|
|
single_pass_renderpass!{
|
|
|
|
attachments: {
|
|
|
|
color: {
|
|
|
|
load: Clear,
|
|
|
|
store: Store,
|
2016-04-10 09:07:07 +00:00
|
|
|
format: ::vulkano::format::Format,
|
2016-02-26 15:21:06 +00:00
|
|
|
},
|
|
|
|
depth: {
|
|
|
|
load: Clear,
|
|
|
|
store: DontCare,
|
2016-04-10 09:07:07 +00:00
|
|
|
format: ::vulkano::format::D16Unorm,
|
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
|
|
|
}
|
2016-02-20 09:55:51 +00:00
|
|
|
}
|
2016-02-26 15:21:06 +00:00
|
|
|
}
|
|
|
|
|
2016-04-10 09:07:07 +00:00
|
|
|
let renderpass = renderpass::CustomRenderPass::new(&device, &renderpass::Formats {
|
|
|
|
color: (images[0].format(), 1),
|
|
|
|
depth: (vulkano::format::D16Unorm, 1)
|
2016-04-23 06:27:47 +00:00
|
|
|
}).unwrap();
|
2016-02-20 09:55:51 +00:00
|
|
|
|
2016-04-14 21:45:10 +00:00
|
|
|
let descriptor_pool = vulkano::descriptor::descriptor_set::DescriptorPool::new(&device);
|
2016-02-20 12:05:44 +00:00
|
|
|
|
2016-04-03 18:31:24 +00:00
|
|
|
mod pipeline_layout {
|
|
|
|
pipeline_layout!{
|
|
|
|
set0: {
|
|
|
|
uniforms: UniformBuffer<::vs::ty::Data>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-02-22 14:09:55 +00:00
|
|
|
|
2016-04-03 18:31:24 +00:00
|
|
|
let pipeline_layout = pipeline_layout::CustomPipeline::new(&device).unwrap();
|
|
|
|
let set = pipeline_layout::set0::Set::new(&descriptor_pool, &pipeline_layout, &pipeline_layout::set0::Descriptors {
|
|
|
|
uniforms: &uniform_buffer
|
2016-04-14 21:45:10 +00:00
|
|
|
});
|
2016-02-22 14:09:55 +00:00
|
|
|
|
2016-04-07 15:40:08 +00:00
|
|
|
let pipeline = vulkano::pipeline::GraphicsPipeline::new(&device, vulkano::pipeline::GraphicsPipelineParams {
|
2016-04-07 19:37:57 +00:00
|
|
|
vertex_input: vulkano::pipeline::vertex::TwoBuffersDefinition::new(),
|
2016-04-07 15:40:08 +00:00
|
|
|
vertex_shader: vs.main_entry_point(),
|
|
|
|
input_assembly: vulkano::pipeline::input_assembly::InputAssembly::triangle_list(),
|
2016-05-20 17:30:30 +00:00
|
|
|
tessellation: None,
|
2016-04-08 09:04:16 +00:00
|
|
|
geometry_shader: None,
|
2016-04-07 15:40:08 +00:00
|
|
|
viewport: vulkano::pipeline::viewport::ViewportsState::Fixed {
|
2016-02-20 09:55:51 +00:00
|
|
|
data: vec![(
|
|
|
|
vulkano::pipeline::viewport::Viewport {
|
|
|
|
origin: [0.0, 0.0],
|
2016-03-31 17:43:12 +00:00
|
|
|
depth_range: 0.0 .. 1.0,
|
|
|
|
dimensions: [images[0].dimensions()[0] as f32, images[0].dimensions()[1] as f32],
|
2016-02-20 09:55:51 +00:00
|
|
|
},
|
2016-03-24 08:27:04 +00:00
|
|
|
vulkano::pipeline::viewport::Scissor::irrelevant()
|
2016-02-20 09:55:51 +00:00
|
|
|
)],
|
2016-04-07 15:40:08 +00:00
|
|
|
},
|
|
|
|
raster: Default::default(),
|
|
|
|
multisample: vulkano::pipeline::multisample::Multisample::disabled(),
|
2016-04-07 15:45:53 +00:00
|
|
|
fragment_shader: fs.main_entry_point(),
|
2016-04-07 18:05:15 +00:00
|
|
|
depth_stencil: vulkano::pipeline::depth_stencil::DepthStencil::simple_depth_test(),
|
2016-04-08 18:31:52 +00:00
|
|
|
blend: vulkano::pipeline::blend::Blend::pass_through(),
|
2016-04-07 15:40:08 +00:00
|
|
|
layout: &pipeline_layout,
|
|
|
|
render_pass: vulkano::framebuffer::Subpass::from(&renderpass, 0).unwrap(),
|
|
|
|
}).unwrap();
|
2016-02-20 09:55:51 +00:00
|
|
|
|
|
|
|
let framebuffers = images.iter().map(|image| {
|
2016-03-25 14:08:48 +00:00
|
|
|
let attachments = renderpass::AList {
|
2016-09-21 11:45:06 +00:00
|
|
|
color: image.clone(),
|
|
|
|
depth: depth_buffer.clone(),
|
2016-03-25 14:08:48 +00:00
|
|
|
};
|
|
|
|
|
2016-09-21 11:45:06 +00:00
|
|
|
vulkano::framebuffer::StdFramebuffer::new(&renderpass, [image.dimensions()[0], image.dimensions()[1], 1], attachments).unwrap()
|
2016-02-20 09:55:51 +00:00
|
|
|
}).collect::<Vec<_>>();
|
|
|
|
|
2016-02-20 12:05:44 +00:00
|
|
|
|
2016-02-20 09:55:51 +00:00
|
|
|
let command_buffers = framebuffers.iter().map(|framebuffer| {
|
2016-09-21 11:45:06 +00:00
|
|
|
vulkano::command_buffer::std::PrimaryCbBuilder::new(&device, queue.family())
|
|
|
|
.begin_render_pass(framebuffer.clone(), false, renderpass::ClearValues {
|
2016-04-23 10:01:53 +00:00
|
|
|
color: [0.0, 0.0, 1.0, 1.0],
|
2016-03-13 13:14:56 +00:00
|
|
|
depth: 1.0,
|
|
|
|
})
|
2016-04-03 18:31:24 +00:00
|
|
|
.draw_indexed(&pipeline, (&vertex_buffer, &normals_buffer), &index_buffer,
|
2016-04-08 19:52:40 +00:00
|
|
|
&vulkano::command_buffer::DynamicState::none(), &set, &())
|
2016-09-21 11:45:06 +00:00
|
|
|
.end_render_pass()
|
2016-04-14 21:45:10 +00:00
|
|
|
.build()
|
2016-02-20 09:55:51 +00:00
|
|
|
}).collect::<Vec<_>>();
|
|
|
|
|
2016-09-21 11:45:06 +00:00
|
|
|
let mut submissions: Vec<vulkano::command_buffer::Submission> = Vec::new();
|
2016-03-02 07:59:02 +00:00
|
|
|
|
2016-09-11 12:11:45 +00:00
|
|
|
|
2016-02-20 09:55:51 +00:00
|
|
|
loop {
|
2016-03-28 17:42:26 +00:00
|
|
|
submissions.retain(|s| s.destroying_would_block());
|
2016-03-02 07:59:02 +00:00
|
|
|
|
2016-09-11 12:11:45 +00:00
|
|
|
{
|
|
|
|
// aquiring write lock for the uniform buffer
|
|
|
|
let mut buffer_content = uniform_buffer.write(Duration::new(1, 0)).unwrap();
|
|
|
|
|
|
|
|
let rotation = cgmath::Matrix3::from_angle_y(cgmath::rad(time::precise_time_ns() as f32 * 0.000000001));
|
|
|
|
|
|
|
|
// since write lock implementd Deref and DerefMut traits,
|
|
|
|
// we can update content directly
|
|
|
|
buffer_content.world = cgmath::Matrix4::from(rotation).into();
|
|
|
|
}
|
|
|
|
|
2016-04-10 11:31:04 +00:00
|
|
|
let image_num = swapchain.acquire_next_image(Duration::from_millis(1)).unwrap();
|
2016-09-21 11:45:06 +00:00
|
|
|
submissions.push(&command_buffers[image_num].submit(&queue).unwrap());
|
2016-02-24 16:32:06 +00:00
|
|
|
swapchain.present(&queue, image_num).unwrap();
|
2016-02-20 09:55:51 +00:00
|
|
|
|
2016-04-23 11:32:31 +00:00
|
|
|
for ev in window.window().poll_events() {
|
2016-02-23 12:28:46 +00:00
|
|
|
match ev {
|
2016-03-26 15:34:51 +00:00
|
|
|
winit::Event::Closed => return,
|
2016-02-23 12:28:46 +00:00
|
|
|
_ => ()
|
2016-02-20 09:55:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|