chore: small optimizations

This commit is contained in:
alphafetus committed 2026-05-14 17:22:09 -04:00
1 parent 14fb61a4d5
commit 44cd6fff96
3 files changed
+16 -32

No files matched your search

+11 -27
View File
@@ -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<ShaderModule>,
frag_shader: &Arc<ShaderModule>,
render_pass: &Arc<RenderPass>,
extents: [f32; 2],
) -> Result<Arc<GraphicsPipeline>, Box<dyn Error>> {
// 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<StandardCommandBufferAllocator>,
queue: &Arc<Queue>,
@@ -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<dyn Error>> {
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<Instance>,
context: Option<VulkanContext>,
}
impl MainApplication {
impl App {
pub fn new(instance_extensions: InstanceExtensions) -> Result<Self, Box<dyn Error>> {
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 {
+3 -3
View File
@@ -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<dyn Error>> {
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)?;
+2 -2
View File
@@ -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.