vulkano/examples/src/bin/teapot.rs

210 lines
9.4 KiB
Rust
Raw Normal View History

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.
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;
use vulkano::command_buffer::CommandBufferBuilder;
use vulkano::sync::GpuFuture;
2017-04-15 10:55:54 +00:00
use vulkano::image::ImageView;
2016-02-20 09:55:51 +00:00
use std::sync::Arc;
2016-02-20 09:55:51 +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
};
let (device, mut queues) = vulkano::device::Device::new(&physical, physical.supported_features(),
&device_ext, [(queue, 0.5)].iter().cloned())
.expect("failed to create device");
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]);
let present = caps.present_modes.iter().next().unwrap();
2016-02-20 09:55:51 +00:00
let usage = caps.supported_usage_flags;
let format = caps.supported_formats[0].0;
2016-02-20 09:55:51 +00:00
vulkano::swapchain::Swapchain::new(&device, &window.surface(), caps.min_image_count, 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
};
2017-04-17 07:15:43 +00:00
let depth_buffer = vulkano::image::attachment::AttachmentImage::transient(&device, images[0].dimensions(), vulkano::format::D16Unorm).unwrap().access();
2016-02-21 10:02:51 +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
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
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
let proj = cgmath::perspective(cgmath::Rad(std::f32::consts::FRAC_PI_2), { 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);
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
let renderpass = Arc::new(
single_pass_renderpass!(device.clone(),
attachments: {
color: {
load: Clear,
store: Store,
format: images[0].format(),
samples: 1,
},
depth: {
load: Clear,
store: DontCare,
2017-04-15 10:55:54 +00:00
format: vulkano::image::ImageAccess::format(&depth_buffer),
samples: 1,
}
},
pass: {
color: [color],
depth_stencil: {depth}
}
).unwrap()
);
let pipeline = Arc::new(vulkano::pipeline::GraphicsPipeline::new(&device, vulkano::pipeline::GraphicsPipelineParams {
2016-04-07 19:37:57 +00:00
vertex_input: vulkano::pipeline::vertex::TwoBuffersDefinition::new(),
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,
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
)],
},
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(),
render_pass: vulkano::framebuffer::Subpass::from(renderpass.clone(), 0).unwrap(),
}).unwrap());
2016-02-20 09:55:51 +00:00
let set = Arc::new(simple_descriptor_set!(pipeline.clone(), 0, {
uniforms: uniform_buffer.clone()
}));
2016-10-16 18:37:32 +00:00
2016-02-20 09:55:51 +00:00
let framebuffers = images.iter().map(|image| {
let attachments = renderpass.desc().start_attachments()
.color(image.clone()).depth(depth_buffer.clone());
let dimensions = [image.dimensions()[0], image.dimensions()[1], 1];
vulkano::framebuffer::Framebuffer::new(renderpass.clone(), dimensions, attachments).unwrap()
2016-02-20 09:55:51 +00:00
}).collect::<Vec<_>>();
2016-02-20 12:05:44 +00:00
let mut submissions: Vec<Box<GpuFuture>> = Vec::new();
2016-09-11 12:11:45 +00:00
2016-02-20 09:55:51 +00:00
loop {
while submissions.len() >= 4 {
submissions.remove(0);
}
2016-09-11 12:11:45 +00:00
{
// aquiring write lock for the uniform buffer
let mut buffer_content = uniform_buffer.write().unwrap();
2016-09-11 12:11:45 +00:00
let rotation = cgmath::Matrix3::from_angle_y(cgmath::Rad(time::precise_time_ns() as f32 * 0.000000001));
2016-09-11 12:11:45 +00:00
// since write lock implementd Deref and DerefMut traits,
// we can update content directly
buffer_content.world = cgmath::Matrix4::from(rotation).into();
}
let (image_num, future) = swapchain.acquire_next_image(std::time::Duration::new(1, 0)).unwrap();
let command_buffer = vulkano::command_buffer::AutoCommandBufferBuilder::new(device.clone(), queue.family()).unwrap()
.begin_render_pass(
framebuffers[image_num].clone(), false,
renderpass.desc().start_clear_values()
.color([0.0, 0.0, 1.0, 1.0]).depth((1f32)))
.draw_indexed(
pipeline.clone(), vulkano::command_buffer::DynamicState::none(),
(vertex_buffer.clone(), normals_buffer.clone()),
index_buffer.clone(), set.clone(), ())
.end_render_pass()
.build().unwrap();
let future = future
.then_execute(queue.clone(), command_buffer)
.then_swapchain_present(queue.clone(), swapchain.clone(), image_num)
.then_signal_fence();
future.flush().unwrap();
submissions.push(Box::new(future) as Box<_>);
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 {
winit::Event::Closed => return,
2016-02-23 12:28:46 +00:00
_ => ()
2016-02-20 09:55:51 +00:00
}
}
}
}