From 44cd6fff966163aeb64679c596ba78b04ba31229 Mon Sep 17 00:00:00 2001 From: Lucas Vas Date: Thu, 14 May 2026 17:22:09 -0400 Subject: [PATCH] chore: small optimizations --- src/app.rs | 38 ++++++++++------------------------ src/main.rs | 6 +++--- src/shaders/triangle_frag.glsl | 4 ++-- 3 files changed, 16 insertions(+), 32 deletions(-) diff --git a/src/app.rs b/src/app.rs index 7f11707..d0ba248 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1,4 +1,3 @@ -use std::alloc::Layout; use crate::common::mesh::MeshRegistry; use crate::gpu::vertex3::Vertex3; use crate::triangle; @@ -16,10 +15,10 @@ use vulkano::device::physical::{PhysicalDevice, PhysicalDeviceType}; use vulkano::device::{ Device, DeviceCreateInfo, DeviceExtensions, Queue, QueueCreateInfo, QueueFlags, }; -use vulkano::image::view::{ImageView, ImageViewCreateInfo}; -use vulkano::image::{Image, ImageAspects, ImageCreateInfo, ImageLayout, ImageSubresourceRange, ImageType, ImageUsage}; +use vulkano::image::view::ImageView; +use vulkano::image::{Image, ImageCreateInfo, ImageType, ImageUsage}; use vulkano::instance::{Instance, InstanceCreateFlags, InstanceCreateInfo, InstanceExtensions}; -use vulkano::memory::allocator::{AllocationCreateInfo, MemoryAllocator, MemoryTypeFilter, StandardMemoryAllocator}; +use vulkano::memory::allocator::{AllocationCreateInfo, MemoryAllocator, StandardMemoryAllocator}; use vulkano::pipeline::graphics::GraphicsPipelineCreateInfo; use vulkano::pipeline::graphics::color_blend::{ColorBlendAttachmentState, ColorBlendState}; use vulkano::pipeline::graphics::input_assembly::InputAssemblyState; @@ -34,8 +33,7 @@ use vulkano::shader::ShaderModule; use vulkano::swapchain::{Surface, Swapchain, SwapchainCreateInfo, SwapchainPresentInfo}; use vulkano::sync::GpuFuture; use vulkano::{Validated, VulkanError, VulkanLibrary, swapchain, sync}; -use vulkano::format::{ClearValue, Format}; -use vulkano::memory::MemoryType; +use vulkano::format::Format; use vulkano::pipeline::graphics::depth_stencil::{DepthState, DepthStencilState}; use winit::application::ApplicationHandler; use winit::event::WindowEvent; @@ -183,7 +181,6 @@ impl VulkanContext { &vert_shader, &frag_shader, &render_pass, - [image_extents[0] as f32, image_extents[1] as f32], )?; // Get descriptor set layout @@ -206,13 +203,6 @@ impl VulkanContext { &scene )); - // Construct render batches each frame, starting with once here. - let batches = RenderBatch::build_batches( - mem_allocator.clone(), - mesh_registry.clone(), - instance_registry.clone() - ); - Ok(Self { window, device, @@ -389,7 +379,6 @@ impl VulkanContext { vert_shader: &Arc, frag_shader: &Arc, render_pass: &Arc, - extents: [f32; 2], ) -> Result, Box> { // Get shader stages let vs = vert_shader @@ -447,7 +436,8 @@ impl VulkanContext { )?) } - /// Creates a [`PrimaryAutoCommandBuffer`] for each swapchain image. + /// Creates a [`PrimaryAutoCommandBuffer`] for the given framebuffer, render pass, and queue. + /// This is used to pass instructions to draw a frame. fn get_command_buffer( cmd_allocator: &Arc, queue: &Arc, @@ -518,6 +508,7 @@ impl VulkanContext { /// bounds. Will also handle resizing logic, as this requires extra information to be updated, /// specifically the [`GraphicsPipeline`] and [`PrimaryAutoCommandBuffer`]. All information is /// automatically set within the `VulkanContext`. + // TODO Define a function that handles creation of all information related to the size of the window. pub fn recreate_swapchain(&mut self, resized: bool) -> Result<(), Box> { unsafe { self.device.wait_idle()?; @@ -549,17 +540,10 @@ impl VulkanContext { &self.vert_shader, &self.frag_shader, &self.render_pass, - [image_extents.width as f32, image_extents.height as f32], )?; self.camera.extents([image_extents.width, image_extents.height]); - let batches = RenderBatch::build_batches( - self.mem_allocator.clone(), - self.mesh_registry.clone(), - self.instance_registry.clone() - ); - self.pipeline = new_pipeline; } @@ -567,12 +551,12 @@ impl VulkanContext { } } -pub struct MainApplication { +pub struct App { instance: Arc, context: Option, } -impl MainApplication { +impl App { pub fn new(instance_extensions: InstanceExtensions) -> Result> { Ok(Self { instance: Self::get_instance(instance_extensions)?, @@ -608,7 +592,7 @@ impl MainApplication { } } -impl ApplicationHandler for MainApplication { +impl ApplicationHandler for App { fn resumed(&mut self, event_loop: &ActiveEventLoop) { self.context.is_none().then(|| { // Initialization logic @@ -622,7 +606,7 @@ impl ApplicationHandler for MainApplication { fn window_event( &mut self, event_loop: &ActiveEventLoop, - window_id: WindowId, + _window_id: WindowId, event: WindowEvent, ) { match event { diff --git a/src/main.rs b/src/main.rs index 0c15f7d..62a8170 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,12 +3,12 @@ mod common; pub mod gpu; // pub mod math; -use crate::app::MainApplication; +use crate::app::App; use std::error::Error; use vulkano::swapchain::Surface; use winit::event_loop::{ControlFlow, EventLoop}; -// Compute shader compilation. Happens at runtime, but file contents are included during compilation. +// Shader compilation. Happens at runtime, but file contents are included during compilation. mod cs { vulkano_shaders::shader! { ty: "compute", @@ -46,7 +46,7 @@ fn main() -> Result<(), Box> { let required_extensions = Surface::required_extensions(&event_loop)?; // Create and start the window. - let mut app = MainApplication::new(required_extensions)?; + let mut app = App::new(required_extensions)?; event_loop.set_control_flow(ControlFlow::Wait); event_loop.run_app(&mut app)?; diff --git a/src/shaders/triangle_frag.glsl b/src/shaders/triangle_frag.glsl index 8e66835..ef2cdb7 100644 --- a/src/shaders/triangle_frag.glsl +++ b/src/shaders/triangle_frag.glsl @@ -15,9 +15,9 @@ layout(location = 0) out vec4 f_color; void main() { // Default mesh color - no textures for now. - vec3 objectColor = vec3(0.1, 0.1, 0.1); + vec3 objectColor = vec3(0.5, 0.5, 0.5); // Default light information - we aren't going to edit light information until we know more about it. - vec3 lightPosition = vec3(0.0, 10000.0, 0.0); + vec3 lightPosition = vec3(-100.0, 200.0, 200.0); vec3 lightColor = vec3(1.0, 1.0, 1.0); // Ambient lighting - literally just a base color percentage.