sync
This commit is contained in:
28
src/main.rs
28
src/main.rs
@@ -3,7 +3,6 @@
|
|||||||
#![allow(unused_mut)]
|
#![allow(unused_mut)]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
extern crate cgmath;
|
extern crate cgmath;
|
||||||
extern crate image;
|
extern crate image;
|
||||||
extern crate nalgebra as na;
|
extern crate nalgebra as na;
|
||||||
@@ -35,6 +34,7 @@ use winit::dpi::LogicalSize;
|
|||||||
use winit::event_loop::EventLoop;
|
use winit::event_loop::EventLoop;
|
||||||
use winit::event::{Event, WindowEvent, DeviceEvent, VirtualKeyCode, ElementState};
|
use winit::event::{Event, WindowEvent, DeviceEvent, VirtualKeyCode, ElementState};
|
||||||
use winit::platform::unix::WindowBuilderExtUnix;
|
use winit::platform::unix::WindowBuilderExtUnix;
|
||||||
|
use crate::vkprocessor::VkProcessor;
|
||||||
|
|
||||||
|
|
||||||
pub mod util;
|
pub mod util;
|
||||||
@@ -43,24 +43,32 @@ pub mod drawables;
|
|||||||
pub mod canvas;
|
pub mod canvas;
|
||||||
pub mod compute;
|
pub mod compute;
|
||||||
|
|
||||||
|
|
||||||
pub fn main() {
|
pub fn main() {
|
||||||
hprof::start_frame();
|
hprof::start_frame();
|
||||||
|
|
||||||
let q1 = hprof::enter("setup");
|
let q1 = hprof::enter("setup");
|
||||||
|
|
||||||
|
|
||||||
let instance = {
|
let instance = {
|
||||||
let extensions = vulkano_win::required_extensions();
|
let extensions = vulkano_win::required_extensions();
|
||||||
Instance::new(None, &extensions, None).unwrap()
|
Instance::new(None, &extensions, None).unwrap()
|
||||||
};
|
};
|
||||||
|
|
||||||
{
|
let _callback = DebugCallback::errors_and_warnings(&instance, |msg| {
|
||||||
let mut processor = vkprocessor::VkProcessor::new(instance.clone());
|
println!("Debug callback: {:?}", msg.description);
|
||||||
|
}).ok();
|
||||||
|
|
||||||
|
let mut events_loop = EventLoop::new();
|
||||||
|
|
||||||
|
let mut surface = WindowBuilder::new()
|
||||||
|
.with_inner_size(LogicalSize::new(800, 800));
|
||||||
|
// Some weird namespacing issue here
|
||||||
|
let mut surface = VkSurfaceBuild::build_vk_surface(surface.clone(), &events_loop, instance.clone()).unwrap();
|
||||||
|
|
||||||
|
let mut processor = VkProcessor::new(&instance, surface.clone());
|
||||||
|
|
||||||
{
|
{
|
||||||
let g = hprof::enter("vulkan preload");
|
let g = hprof::enter("vulkan preload");
|
||||||
processor.create_swapchain();
|
processor.create_swapchain(surface.clone());
|
||||||
|
|
||||||
processor.preload_kernels();
|
processor.preload_kernels();
|
||||||
processor.preload_shaders();
|
processor.preload_shaders();
|
||||||
@@ -139,7 +147,7 @@ pub fn main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Events loop is borrowed from the surface
|
// Events loop is borrowed from the surface
|
||||||
processor.event_loop().clone().run(move |event, _, control_flow| {
|
events_loop.run(move |event, _, control_flow| {
|
||||||
match event {
|
match event {
|
||||||
Event::WindowEvent { event: WindowEvent::CloseRequested, .. } =>
|
Event::WindowEvent { event: WindowEvent::CloseRequested, .. } =>
|
||||||
{
|
{
|
||||||
@@ -181,19 +189,17 @@ pub fn main() {
|
|||||||
|
|
||||||
{
|
{
|
||||||
let g = hprof::enter("Run");
|
let g = hprof::enter("Run");
|
||||||
processor.run(canvas_frame,
|
processor.run(&surface.clone(),
|
||||||
|
canvas_frame,
|
||||||
compu_frame);
|
compu_frame);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
drop(l);
|
drop(l);
|
||||||
|
|
||||||
|
|
||||||
hprof::end_frame();
|
hprof::end_frame();
|
||||||
hprof::profiler().print_timing();
|
hprof::profiler().print_timing();
|
||||||
|
|
||||||
drop(processor);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -32,7 +32,6 @@ use winit::event_loop::EventLoop;
|
|||||||
/// and the compute and canvas states
|
/// and the compute and canvas states
|
||||||
pub struct VkProcessor<'a> {
|
pub struct VkProcessor<'a> {
|
||||||
// Vulkan state fields
|
// Vulkan state fields
|
||||||
pub instance: Arc<Instance>,
|
|
||||||
pub physical: PhysicalDevice<'a>,
|
pub physical: PhysicalDevice<'a>,
|
||||||
pub device: Arc<Device>,
|
pub device: Arc<Device>,
|
||||||
pub queues: QueuesIter,
|
pub queues: QueuesIter,
|
||||||
@@ -43,9 +42,6 @@ pub struct VkProcessor<'a> {
|
|||||||
|
|
||||||
pub swapchain_recreate_needed: bool,
|
pub swapchain_recreate_needed: bool,
|
||||||
|
|
||||||
pub surface: Arc<Surface<Window>>,
|
|
||||||
pub event_loop: Arc<EventLoop<()>>,
|
|
||||||
|
|
||||||
/// State holding textures, images, and their related vertex buffers
|
/// State holding textures, images, and their related vertex buffers
|
||||||
canvas_state: CanvasState,
|
canvas_state: CanvasState,
|
||||||
/// State holding
|
/// State holding
|
||||||
@@ -59,22 +55,9 @@ pub struct VkProcessor<'a> {
|
|||||||
impl<'a> VkProcessor<'a> {
|
impl<'a> VkProcessor<'a> {
|
||||||
/// Creates a new VkProcessor from an instance and surface
|
/// Creates a new VkProcessor from an instance and surface
|
||||||
/// This includes the physical device, queues, compute and canvas state
|
/// This includes the physical device, queues, compute and canvas state
|
||||||
//pub fn new(instance: &'a Arc<Instance>, surface: &'a Arc<Surface<Window>>) -> VkProcessor<'a> {
|
pub fn new(instance: &'a Arc<Instance>, surface: Arc<Surface<Window>>) -> VkProcessor<'a> {
|
||||||
pub fn new(instance: Arc<Instance>) -> VkProcessor<'a> {
|
|
||||||
|
|
||||||
let _callback = DebugCallback::errors_and_warnings(&instance, |msg| {
|
let physical = PhysicalDevice::enumerate(instance).next().unwrap();
|
||||||
println!("Debug callback: {:?}", msg.description);
|
|
||||||
}).ok();
|
|
||||||
|
|
||||||
let mut events_loop = Arc::new(EventLoop::new());
|
|
||||||
|
|
||||||
let mut surface = WindowBuilder::new()
|
|
||||||
.with_inner_size(LogicalSize::new(800, 800));
|
|
||||||
|
|
||||||
// Some weird namespacing issue here
|
|
||||||
let mut surface = VkSurfaceBuild::build_vk_surface(surface, &events_loop, instance.clone()).unwrap();
|
|
||||||
|
|
||||||
let physical = PhysicalDevice::enumerate(&instance).next().unwrap();
|
|
||||||
|
|
||||||
let queue_family = physical.queue_families().find(|&q| {
|
let queue_family = physical.queue_families().find(|&q| {
|
||||||
// We take the first queue that supports drawing to our window.
|
// We take the first queue that supports drawing to our window.
|
||||||
@@ -93,8 +76,8 @@ impl<'a> VkProcessor<'a> {
|
|||||||
|
|
||||||
let capabilities = surface.capabilities(physical).unwrap();
|
let capabilities = surface.capabilities(physical).unwrap();
|
||||||
|
|
||||||
|
|
||||||
VkProcessor {
|
VkProcessor {
|
||||||
instance: instance.clone(),
|
|
||||||
physical: physical.clone(),
|
physical: physical.clone(),
|
||||||
device: device.clone(),
|
device: device.clone(),
|
||||||
queue: queue.clone(),
|
queue: queue.clone(),
|
||||||
@@ -105,8 +88,6 @@ impl<'a> VkProcessor<'a> {
|
|||||||
compute_state: CompuState::new(),
|
compute_state: CompuState::new(),
|
||||||
capabilities: capabilities.clone(),
|
capabilities: capabilities.clone(),
|
||||||
canvas_state: CanvasState::new(queue, device, physical, capabilities),
|
canvas_state: CanvasState::new(queue, device, physical, capabilities),
|
||||||
surface: surface,
|
|
||||||
event_loop: events_loop,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -116,24 +97,19 @@ impl<'a> VkProcessor<'a> {
|
|||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn event_loop(&mut self) -> Arc<EventLoop<()>> {
|
|
||||||
self.event_loop()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Using the surface, we calculate the surface capabilities and create the swapchain and swapchain images
|
/// Using the surface, we calculate the surface capabilities and create the swapchain and swapchain images
|
||||||
pub fn create_swapchain(&mut self) {
|
pub fn create_swapchain(&mut self, surface: Arc<Surface<Window>>) {
|
||||||
|
|
||||||
let (mut swapchain, images) = {
|
let (mut swapchain, images) = {
|
||||||
let capabilities = self.surface.capabilities(self.physical).unwrap();
|
let capabilities = surface.capabilities(self.physical).unwrap();
|
||||||
let usage = capabilities.supported_usage_flags;
|
let usage = capabilities.supported_usage_flags;
|
||||||
let alpha = capabilities.supported_composite_alpha.iter().next().unwrap();
|
let alpha = capabilities.supported_composite_alpha.iter().next().unwrap();
|
||||||
// Choosing the internal format that the images will have.
|
// Choosing the internal format that the images will have.
|
||||||
let format = capabilities.supported_formats[0].0;
|
let format = capabilities.supported_formats[0].0;
|
||||||
|
|
||||||
// Set the swapchains window dimensions
|
// Set the swapchains window dimensions
|
||||||
let initial_dimensions = if let dimensions = self.surface.window().inner_size() {
|
let initial_dimensions = if let dimensions = surface.window().inner_size() {
|
||||||
// convert to physical pixels
|
// convert to physical pixels
|
||||||
let dimensions: (u32, u32) = dimensions.to_logical::<u32>(self.surface.window().scale_factor()).into();
|
let dimensions: (u32, u32) = dimensions.to_logical::<u32>(surface.window().scale_factor()).into();
|
||||||
[dimensions.0, dimensions.1]
|
[dimensions.0, dimensions.1]
|
||||||
} else {
|
} else {
|
||||||
// The window no longer exists so exit the application.
|
// The window no longer exists so exit the application.
|
||||||
@@ -141,7 +117,7 @@ impl<'a> VkProcessor<'a> {
|
|||||||
};
|
};
|
||||||
|
|
||||||
Swapchain::new(self.device.clone(),
|
Swapchain::new(self.device.clone(),
|
||||||
self.surface.clone(),
|
surface.clone(),
|
||||||
capabilities.min_image_count, // number of attachment images
|
capabilities.min_image_count, // number of attachment images
|
||||||
format,
|
format,
|
||||||
initial_dimensions,
|
initial_dimensions,
|
||||||
@@ -160,9 +136,9 @@ impl<'a> VkProcessor<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// On screen resizes, the swapchain and images must be recreated
|
/// On screen resizes, the swapchain and images must be recreated
|
||||||
pub fn recreate_swapchain(&mut self) {
|
pub fn recreate_swapchain(&mut self, surface: &'a Arc<Surface<Window>>) {
|
||||||
let dimensions = if let dimensions = self.surface.window().inner_size() {
|
let dimensions = if let dimensions = surface.window().inner_size() {
|
||||||
let dimensions: (u32, u32) = dimensions.to_logical::<u32>(self.surface.window().scale_factor()).into();
|
let dimensions: (u32, u32) = dimensions.to_logical::<u32>(surface.window().scale_factor()).into();
|
||||||
[dimensions.0, dimensions.1]
|
[dimensions.0, dimensions.1]
|
||||||
} else {
|
} else {
|
||||||
return;
|
return;
|
||||||
@@ -254,6 +230,7 @@ impl<'a> VkProcessor<'a> {
|
|||||||
|
|
||||||
/// Run the VKprocessor for a single frame, consuming the Canvas/Compu Frames
|
/// Run the VKprocessor for a single frame, consuming the Canvas/Compu Frames
|
||||||
pub fn run(&mut self,
|
pub fn run(&mut self,
|
||||||
|
surface: &'a Arc<Surface<Window>>,
|
||||||
canvas_frame: CanvasFrame,
|
canvas_frame: CanvasFrame,
|
||||||
compute_frame: CompuFrame,
|
compute_frame: CompuFrame,
|
||||||
) {
|
) {
|
||||||
@@ -269,7 +246,7 @@ impl<'a> VkProcessor<'a> {
|
|||||||
// Whenever the window resizes we need to recreate everything dependent on the window size.
|
// Whenever the window resizes we need to recreate everything dependent on the window size.
|
||||||
// In this example that includes the swapchain, the framebuffers and the dynamic state viewport.
|
// In this example that includes the swapchain, the framebuffers and the dynamic state viewport.
|
||||||
if self.swapchain_recreate_needed {
|
if self.swapchain_recreate_needed {
|
||||||
self.recreate_swapchain();
|
self.recreate_swapchain(surface);
|
||||||
framebuffers =
|
framebuffers =
|
||||||
self.canvas_state.window_size_dependent_setup(&self.swapchain_images.clone().unwrap().clone());
|
self.canvas_state.window_size_dependent_setup(&self.swapchain_images.clone().unwrap().clone());
|
||||||
self.swapchain_recreate_needed = false;
|
self.swapchain_recreate_needed = false;
|
||||||
|
|||||||
Reference in New Issue
Block a user