diff --git a/shaders/line_frag.spv b/shaders/line_frag.spv deleted file mode 100644 index 0970cb5..0000000 Binary files a/shaders/line_frag.spv and /dev/null differ diff --git a/shaders/line_vert.spv b/shaders/line_vert.spv deleted file mode 100644 index 147d70a..0000000 Binary files a/shaders/line_vert.spv and /dev/null differ diff --git a/shaders/triangle.vert b/shaders/triangle.vert index 1eca8d6..aafc3f2 100644 --- a/shaders/triangle.vert +++ b/shaders/triangle.vert @@ -1,7 +1,7 @@ #version 450 #extension GL_ARB_separate_shader_objects : enable -layout(binding = 0) uniform UniformBufferObject { +layout(binding = 0) uniform ObjectUniformData { mat4 view; mat4 projection; float time; @@ -14,8 +14,6 @@ layout(location = 0) out vec2 tex_coords; layout(push_constant) uniform PushConstants { mat4 model; - mat4 view; - mat4 projection; } push; out gl_PerVertex { @@ -23,6 +21,6 @@ out gl_PerVertex { }; void main() { - gl_Position = push.projection * push.view * push.model * vec4(position, 1.0); + gl_Position = ubo.projection * ubo.view * push.model * vec4(position, 1.0); tex_coords = uv; } \ No newline at end of file diff --git a/shaders/triangle_frag.spv b/shaders/triangle_frag.spv deleted file mode 100644 index c93e87a..0000000 Binary files a/shaders/triangle_frag.spv and /dev/null differ diff --git a/shaders/triangle_vert.spv b/shaders/triangle_vert.spv deleted file mode 100644 index 0243574..0000000 Binary files a/shaders/triangle_vert.spv and /dev/null differ diff --git a/src/main.rs b/src/main.rs index 18fa4dc..ee45083 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,7 +4,7 @@ use winit::event::Event; use crate::config::LogConfig; use crate::input::InputState; use crate::vulkan::{Game, GameObject, GameObjectHandle, LinePoint, MeshHandle, VulkanRenderer}; -use crate::vulkan::vs::ty::UniformBufferObject; +use crate::vulkan::vs::ty::ObjectUniformData; mod vulkan; mod input; @@ -25,7 +25,7 @@ impl Game for TestGame { self.input.on_window_event(event); } - fn update(self: &mut Self, renderer: &mut VulkanRenderer) -> UniformBufferObject { + fn update(self: &mut Self, renderer: &mut VulkanRenderer) -> ObjectUniformData { self.input.frame_start(); let time = (renderer.game_data.start_time.elapsed().unwrap().as_micros() as f64 / 1000000.0) as f32; @@ -68,12 +68,10 @@ impl Game for TestGame { renderer.game_data.line_push_constants.view = view.into(); renderer.game_data.line_push_constants.projection = proj.into(); - renderer.game_data.push_constants.view = view.into(); - renderer.game_data.push_constants.projection = proj.into(); self.input.frame_end(); - UniformBufferObject { + ObjectUniformData { view: view.into(), projection: proj.into(), time, diff --git a/src/vulkan.rs b/src/vulkan.rs index 1c162ba..e73a35e 100644 --- a/src/vulkan.rs +++ b/src/vulkan.rs @@ -1,4 +1,4 @@ -use std::sync::{Arc}; +use std::sync::Arc; use std::time::SystemTime; use cgmath::{Matrix4, SquareMatrix}; @@ -6,7 +6,7 @@ use image::{ImageBuffer, ImageFormat, Rgb, Rgba}; use image::buffer::ConvertBuffer; use vulkano::buffer::{BufferUsage, CpuAccessibleBuffer}; use vulkano::command_buffer::{AutoCommandBuffer, AutoCommandBufferBuilder, DynamicState}; -use vulkano::descriptor::descriptor_set::{PersistentDescriptorSet}; +use vulkano::descriptor::descriptor_set::PersistentDescriptorSet; use vulkano::descriptor::DescriptorSet; use vulkano::device::{Device, DeviceExtensions, Queue}; use vulkano::format::{ClearValue, Format}; @@ -24,7 +24,7 @@ use vulkano::sync::{FlushError, GpuFuture}; use vulkano::sync; use vulkano_win::VkSurfaceBuild; use winit::event::{Event, WindowEvent}; -use winit::event_loop::{EventLoop, ControlFlow}; +use winit::event_loop::{ControlFlow, EventLoop}; use winit::window::{Window, WindowBuilder}; use line_vs::ty::LinePushConstants; @@ -54,7 +54,7 @@ pub trait Game { /// Returns true if event should be ignored by the vulkan handler fn on_window_event(self: &mut Self, event: &Event<()>); - fn update(self: &mut Self, renderer: &mut VulkanRenderer) -> vs::ty::UniformBufferObject; + fn update(self: &mut Self, renderer: &mut VulkanRenderer) -> vs::ty::ObjectUniformData; } pub struct Mesh { @@ -84,7 +84,7 @@ pub struct GameData { pub textures: Vec>>, } -type Yeet = dyn DescriptorSet + Send + Sync; +type RendererDescriptorSets = dyn DescriptorSet + Send + Sync; pub struct VulkanRenderer { pub game_data: GameData, @@ -102,8 +102,8 @@ pub struct VulkanRenderer { pub recreate_swapchain: bool, pub debug_callback: Option, pub previous_frame_end: Option>, - pub uniform_buffers: Vec>>, - pub descriptor_sets: Vec>, + pub uniform_buffers: Vec>>, + pub descriptor_sets: Vec>, } impl VulkanRenderer { @@ -111,8 +111,6 @@ impl VulkanRenderer { let mut data = GameData { push_constants: PushConstants { model: Matrix4::identity().into(), - view: Matrix4::identity().into(), - projection: Matrix4::identity().into(), }, line_push_constants: LinePushConstants { model: Matrix4::identity().into(), @@ -284,7 +282,7 @@ impl VulkanRenderer { let framebuffers = window_size_dependent_setup(device.clone(), &images, render_pass.clone(), &mut dynamic_state); let mut uniform_buffers = Vec::new(); - let uniform_buffer = vs::ty::UniformBufferObject { view: Matrix4::identity().into(), projection: Matrix4::identity().into(), time: 0.0 }; + let uniform_buffer = vs::ty::ObjectUniformData { view: Matrix4::identity().into(), projection: Matrix4::identity().into(), time: 0.0 }; for _ in 0..swapchain.num_images() { uniform_buffers.push(CpuAccessibleBuffer::from_data( @@ -299,7 +297,7 @@ impl VulkanRenderer { let descriptor_sets = uniform_buffers.iter().map(|uniform_buffer| { let builder = PersistentDescriptorSet::start(descriptor_set_layout.clone()); - let result: Arc = Arc::new(builder + let result: Arc = Arc::new(builder .add_buffer(uniform_buffer.clone()).unwrap() .add_sampled_image(default_tex.clone(), sampler.clone()).unwrap() .build().unwrap()); @@ -322,9 +320,9 @@ impl VulkanRenderer { recreate_swapchain: false, debug_callback, previous_frame_end }, events_loop) } - fn create_command_buffer(self: &mut Self, fb_index: usize, ubo: vs::ty::UniformBufferObject) -> Arc { + fn create_command_buffer(self: &mut Self, fb_index: usize, uniform_buffer_data: vs::ty::ObjectUniformData) -> Arc { let mut builder = AutoCommandBufferBuilder::primary_simultaneous_use(self.device.clone(), self.queue.family()).unwrap(); - builder.update_buffer(self.uniform_buffers[fb_index].clone(), ubo).unwrap(); + builder.update_buffer(self.uniform_buffers[fb_index].clone(), uniform_buffer_data).unwrap(); builder.begin_render_pass(self.framebuffers[fb_index].clone(), false, vec![ClearValue::Float([0.0, 0.0, 0.0, 1.0]), ClearValue::Depth(1.0)]).unwrap(); for i in 0..self.game_data.game_objects.len() { @@ -347,13 +345,11 @@ impl VulkanRenderer { Arc::new(builder.build().unwrap()) } - pub fn render_loop(self: &mut Self, new_ubo: vs::ty::UniformBufferObject) { - // It is important to call this function from time to time, otherwise resources will keep - // accumulating and you will eventually reach an out of memory error. - // Calling this function polls various fences in order to determine what the GPU has - // already processed, and frees the resources that are no longer needed. + pub fn render_loop(self: &mut Self, new_ubo: vs::ty::ObjectUniformData) { + // cleanup previous frame self.previous_frame_end.as_mut().unwrap().cleanup_finished(); + // recreate swapchain if window size changed if self.recreate_swapchain { let window = self.surface.window(); let inner_size = window.inner_size(); @@ -378,6 +374,7 @@ impl VulkanRenderer { self.recreate_swapchain = false; } + // recreate pipeline if requested if self.game_data.recreate_pipeline { if let Some(pipeline_ok) = create_pipeline::(self.device.clone(), self.render_pass.clone(), false) { self.pipeline = pipeline_ok; @@ -468,7 +465,7 @@ impl VulkanRenderer { pub fn start_event_loop(mut renderer: VulkanRenderer, mut game: Box, event_loop: EventLoop<()>) { let mut recreate_swapchain = false; - let ubo = game.update(&mut renderer); + let mut ubo = game.update(&mut renderer); event_loop.run(move |event, _, control_flow| { game.on_window_event(&event); @@ -480,7 +477,7 @@ pub fn start_event_loop(mut renderer: VulkanRenderer, mut game: Box, e recreate_swapchain = true; }, Event::RedrawEventsCleared => { - game.update(&mut renderer); + ubo = game.update(&mut renderer); renderer.render_loop(ubo); }, _ => {}