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-02-24 09:47:58 +00:00
|
|
|
extern crate cgmath;
|
2016-02-24 14:00:06 +00:00
|
|
|
extern crate image;
|
2016-02-24 09:47:58 +00:00
|
|
|
extern crate winit;
|
|
|
|
|
|
|
|
#[macro_use]
|
|
|
|
extern crate vulkano;
|
2016-04-23 11:32:31 +00:00
|
|
|
extern crate vulkano_win;
|
|
|
|
|
|
|
|
use vulkano_win::VkSurfaceBuild;
|
2017-04-04 07:00:11 +00:00
|
|
|
use vulkano::command_buffer::CommandBufferBuilder;
|
|
|
|
use vulkano::sync::GpuFuture;
|
2016-02-24 09:47:58 +00:00
|
|
|
|
2017-04-04 07:00:11 +00:00
|
|
|
use std::sync::Arc;
|
2016-04-15 16:05:58 +00:00
|
|
|
use std::time::Duration;
|
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();
|
2016-05-21 07:47:52 +00:00
|
|
|
let instance = vulkano::instance::Instance::new(None, &extensions, &[]).expect("failed to create instance");
|
2016-02-24 09:47:58 +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-24 09:47:58 +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-24 09:47:58 +00:00
|
|
|
.expect("couldn't find a graphical queue family");
|
|
|
|
|
2016-03-12 08:08:06 +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-12 08:08:06 +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-24 09:47:58 +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-24 09:47:58 +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-24 09:47:58 +00:00
|
|
|
let usage = caps.supported_usage_flags;
|
|
|
|
|
2017-01-03 21:47:33 +00:00
|
|
|
vulkano::swapchain::Swapchain::new(&device, &window.surface(), caps.min_image_count,
|
2016-02-24 20:03:04 +00:00
|
|
|
vulkano::format::B8G8R8A8Srgb, dimensions, 1,
|
2016-02-24 09:47:58 +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-24 09:47:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-12-22 00:01:37 +00:00
|
|
|
#[derive(Debug, Clone)]
|
2016-02-24 09:47:58 +00:00
|
|
|
struct Vertex { position: [f32; 2] }
|
|
|
|
impl_vertex!(Vertex, position);
|
|
|
|
|
2016-12-22 00:01:37 +00:00
|
|
|
let vertex_buffer = vulkano::buffer::cpu_access::CpuAccessibleBuffer::<[Vertex]>
|
|
|
|
::from_iter(&device, &vulkano::buffer::BufferUsage::all(),
|
|
|
|
Some(queue.family()), [
|
|
|
|
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()).expect("failed to create buffer");
|
2016-02-24 09:47:58 +00:00
|
|
|
|
2016-04-23 11:24:56 +00:00
|
|
|
mod vs { include!{concat!(env!("OUT_DIR"), "/shaders/src/bin/image_vs.glsl")} }
|
2016-02-25 07:52:22 +00:00
|
|
|
let vs = vs::Shader::load(&device).expect("failed to create shader module");
|
2016-04-23 11:24:56 +00:00
|
|
|
mod fs { include!{concat!(env!("OUT_DIR"), "/shaders/src/bin/image_fs.glsl")} }
|
2016-02-25 07:52:22 +00:00
|
|
|
let fs = fs::Shader::load(&device).expect("failed to create shader module");
|
2016-02-24 09:47:58 +00:00
|
|
|
|
2017-04-04 07:00:11 +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-04-04 07:00:11 +00:00
|
|
|
format: images[0].format(),
|
|
|
|
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
|
|
|
|
2016-09-11 19:43:06 +00:00
|
|
|
let texture = vulkano::image::immutable::ImmutableImage::new(&device, vulkano::image::Dimensions::Dim2d { width: 93, height: 93 },
|
2016-03-25 16:29:05 +00:00
|
|
|
vulkano::format::R8G8B8A8Unorm, Some(queue.family())).unwrap();
|
2016-02-24 09:47:58 +00:00
|
|
|
|
2016-02-24 14:00:06 +00:00
|
|
|
|
|
|
|
let pixel_buffer = {
|
|
|
|
let image = image::load_from_memory_with_format(include_bytes!("image_img.png"),
|
|
|
|
image::ImageFormat::PNG).unwrap().to_rgba();
|
|
|
|
let image_data = image.into_raw().clone();
|
|
|
|
|
2016-12-22 00:01:37 +00:00
|
|
|
let image_data_chunks = image_data.chunks(4).map(|c| [c[0], c[1], c[2], c[3]]);
|
2016-02-24 14:00:06 +00:00
|
|
|
|
2016-12-22 00:01:37 +00:00
|
|
|
// TODO: staging buffer instead
|
|
|
|
vulkano::buffer::cpu_access::CpuAccessibleBuffer::<[[u8; 4]]>
|
|
|
|
::from_iter(&device, &vulkano::buffer::BufferUsage::all(),
|
|
|
|
Some(queue.family()), image_data_chunks)
|
|
|
|
.expect("failed to create buffer")
|
2016-02-24 14:00:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-02-24 09:47:58 +00:00
|
|
|
let sampler = vulkano::sampler::Sampler::new(&device, vulkano::sampler::Filter::Linear,
|
2016-02-24 14:00:06 +00:00
|
|
|
vulkano::sampler::Filter::Linear, vulkano::sampler::MipmapMode::Nearest,
|
2016-02-24 09:47:58 +00:00
|
|
|
vulkano::sampler::SamplerAddressMode::Repeat,
|
|
|
|
vulkano::sampler::SamplerAddressMode::Repeat,
|
|
|
|
vulkano::sampler::SamplerAddressMode::Repeat,
|
|
|
|
0.0, 1.0, 0.0, 0.0).unwrap();
|
|
|
|
|
2017-04-04 07:00:11 +00:00
|
|
|
let pipeline = Arc::new(vulkano::pipeline::GraphicsPipeline::new(&device, vulkano::pipeline::GraphicsPipelineParams {
|
2016-04-15 16:05:58 +00:00
|
|
|
vertex_input: vulkano::pipeline::vertex::SingleBufferDefinition::new(),
|
|
|
|
vertex_shader: vs.main_entry_point(),
|
|
|
|
input_assembly: vulkano::pipeline::input_assembly::InputAssembly {
|
2016-02-24 09:47:58 +00:00
|
|
|
topology: vulkano::pipeline::input_assembly::PrimitiveTopology::TriangleStrip,
|
|
|
|
primitive_restart_enable: false,
|
2016-04-15 16:05:58 +00:00
|
|
|
},
|
2016-05-20 17:30:30 +00:00
|
|
|
tessellation: None,
|
2016-04-15 16:05:58 +00:00
|
|
|
geometry_shader: None,
|
|
|
|
viewport: vulkano::pipeline::viewport::ViewportsState::Fixed {
|
2016-02-24 09:47:58 +00:00
|
|
|
data: vec![(
|
|
|
|
vulkano::pipeline::viewport::Viewport {
|
|
|
|
origin: [0.0, 0.0],
|
2016-04-15 16:05:58 +00:00
|
|
|
depth_range: 0.0 .. 1.0,
|
|
|
|
dimensions: [images[0].dimensions()[0] as f32, images[0].dimensions()[1] as f32],
|
2016-02-24 09:47:58 +00:00
|
|
|
},
|
2016-04-15 16:05:58 +00:00
|
|
|
vulkano::pipeline::viewport::Scissor::irrelevant()
|
2016-02-24 09:47:58 +00:00
|
|
|
)],
|
2016-04-15 16:05:58 +00:00
|
|
|
},
|
|
|
|
raster: Default::default(),
|
|
|
|
multisample: vulkano::pipeline::multisample::Multisample::disabled(),
|
|
|
|
fragment_shader: fs.main_entry_point(),
|
2016-04-23 11:33:58 +00:00
|
|
|
depth_stencil: vulkano::pipeline::depth_stencil::DepthStencil::disabled(),
|
2016-04-15 16:05:58 +00:00
|
|
|
blend: vulkano::pipeline::blend::Blend::pass_through(),
|
2017-04-04 07:00:11 +00:00
|
|
|
render_pass: vulkano::framebuffer::Subpass::from(renderpass.clone(), 0).unwrap(),
|
|
|
|
}).unwrap());
|
|
|
|
|
|
|
|
let set = Arc::new(simple_descriptor_set!(pipeline.clone(), 0, {
|
|
|
|
tex: (texture.clone(), sampler.clone())
|
|
|
|
}));
|
2016-02-24 09:47:58 +00:00
|
|
|
|
|
|
|
let framebuffers = images.iter().map(|image| {
|
2017-04-04 07:00:11 +00:00
|
|
|
let attachments = renderpass.desc().start_attachments()
|
|
|
|
.color(image.clone());
|
|
|
|
let dimensions = [image.dimensions()[0], image.dimensions()[1], 1];
|
2016-03-25 16:29:05 +00:00
|
|
|
|
2017-04-04 07:00:11 +00:00
|
|
|
vulkano::framebuffer::Framebuffer::new(renderpass.clone(), dimensions, attachments).unwrap()
|
2016-02-24 09:47:58 +00:00
|
|
|
}).collect::<Vec<_>>();
|
|
|
|
|
2017-04-04 07:00:11 +00:00
|
|
|
let mut submissions: Vec<Box<GpuFuture>> = Vec::new();
|
2016-02-24 09:47:58 +00:00
|
|
|
|
|
|
|
loop {
|
2017-04-04 07:00:11 +00:00
|
|
|
while submissions.len() >= 4 {
|
|
|
|
submissions.remove(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
let (image_num, future) = swapchain.acquire_next_image(Duration::new(10, 0)).unwrap();
|
|
|
|
|
|
|
|
let cb = vulkano::command_buffer::AutoCommandBufferBuilder::new(device.clone(), queue.family())
|
|
|
|
.unwrap()
|
|
|
|
.copy_buffer_to_image(pixel_buffer.clone(), texture.clone())
|
|
|
|
.unwrap()
|
|
|
|
//.clear_color_image(&texture, [0.0, 1.0, 0.0, 1.0])
|
|
|
|
.begin_render_pass(
|
|
|
|
framebuffers[image_num].clone(), false,
|
|
|
|
renderpass.desc().start_clear_values()
|
|
|
|
.color([0.0, 0.0, 1.0, 1.0]))
|
|
|
|
.draw(pipeline.clone(), vulkano::command_buffer::DynamicState::none(), vertex_buffer.clone(),
|
|
|
|
set.clone(), ())
|
|
|
|
.end_render_pass()
|
|
|
|
.build();
|
|
|
|
|
|
|
|
let future = future
|
|
|
|
.then_execute(queue.clone(), cb)
|
|
|
|
.then_swapchain_present(queue.clone(), swapchain.clone(), image_num)
|
|
|
|
.then_signal_fence();
|
|
|
|
future.flush().unwrap();
|
|
|
|
submissions.push(Box::new(future) as Box<_>);
|
2016-02-24 09:47:58 +00:00
|
|
|
|
2016-04-23 11:32:31 +00:00
|
|
|
for ev in window.window().poll_events() {
|
2016-02-24 09:47:58 +00:00
|
|
|
match ev {
|
2016-03-26 15:34:51 +00:00
|
|
|
winit::Event::Closed => return,
|
2016-02-24 09:47:58 +00:00
|
|
|
_ => ()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|