diff options
| author | Irene Knapp <ireneista@irenes.space> | 2026-08-08 05:32:41 -0700 |
|---|---|---|
| committer | Irene Knapp <ireneista@irenes.space> | 2026-08-08 05:32:41 -0700 |
| commit | d56eb5e4c223f5c89117cda8dfb1527e02b6a90c (patch) | |
| tree | 18e02395408c2dd12e15bdaf13af7c8a54582c9a | |
| parent | ecee10d87f5f2c8b56647ce2a2790f03880f573c (diff) | |
remove physical_device from ForReinit
Vulkanalia tracks it for us in Device. it's used in Model, so it wasn't strictly in-scope for ForReinit; this solves the problem and simplifies some stuff. Force-Push: yes Change-Id: I8e38c236b3dc4c112671382a062c9ada2f34fbb1
| -rw-r--r-- | src/graphics/model.rs | 25 | ||||
| -rw-r--r-- | src/graphics/permanent.rs | 16 | ||||
| -rw-r--r-- | src/graphics/util.rs | 29 | ||||
| -rw-r--r-- | src/graphics/window_dressing.rs | 80 | ||||
| -rw-r--r-- | src/main.rs | 3 |
5 files changed, 69 insertions, 84 deletions
diff --git a/src/graphics/model.rs b/src/graphics/model.rs index b691d99..bda064c 100644 --- a/src/graphics/model.rs +++ b/src/graphics/model.rs @@ -1,8 +1,6 @@ #![deny(unsafe_code)] use crate::error::*; -use crate::graphics::permanent::{ - PermanentGraphicsState, GraphicsStateForReinit -}; +use crate::graphics::permanent::PermanentGraphicsState; use crate::graphics::render_state::RenderState; use crate::graphics::util::init_buffer; use crate::graphics::window_dressing::WindowDressing; @@ -29,25 +27,23 @@ pub struct Model { impl Model { pub fn new(permanent: &PermanentGraphicsState, - for_reinit: &GraphicsStateForReinit, window_dressing: &WindowDressing) -> Result<Self> { let device = &permanent.device; let instance = &permanent.instance; let graphics_queue = &permanent.graphics_queue; - let physical_device = &for_reinit.physical_device; let transient_command_pool = &window_dressing.transient_command_pool; let (vertices, indices) = load_model()?; let index_count = indices.len(); let (vertex_buffer, vertex_buffer_memory) - = init_vertex_buffer(vertices, instance, physical_device, device, - graphics_queue, &transient_command_pool)?; + = init_vertex_buffer(vertices, instance, device, graphics_queue, + &transient_command_pool)?; let (index_buffer, index_buffer_memory) - = init_index_buffer(indices, instance, physical_device, device, - graphics_queue, &transient_command_pool)?; + = init_index_buffer(indices, instance, device, graphics_queue, + &transient_command_pool)?; Ok(Model { vertex_buffer, @@ -121,21 +117,20 @@ impl Model { fn init_vertex_buffer(vertices: Vec<Vertex<f32>>, instance: &Instance, - physical_device: &vk::PhysicalDevice, device: &Device, - queue: &vk::Queue, command_pool: &vk::CommandPool) + device: &Device, queue: &vk::Queue, + command_pool: &vk::CommandPool) -> Result<(vk::Buffer, vk::DeviceMemory)> { - init_buffer(instance, physical_device, device, queue, command_pool, + init_buffer(instance, device, queue, command_pool, vk::BufferUsageFlags::VERTEX_BUFFER, &vertices) } -fn init_index_buffer(indices: Vec<u32>, instance: &Instance, - physical_device: &vk::PhysicalDevice, device: &Device, +fn init_index_buffer(indices: Vec<u32>, instance: &Instance, device: &Device, queue: &vk::Queue, command_pool: &vk::CommandPool) -> Result<(vk::Buffer, vk::DeviceMemory)> { - init_buffer(instance, physical_device, device, queue, command_pool, + init_buffer(instance, device, queue, command_pool, vk::BufferUsageFlags::INDEX_BUFFER, &indices) } diff --git a/src/graphics/permanent.rs b/src/graphics/permanent.rs index 5282e21..beda3fe 100644 --- a/src/graphics/permanent.rs +++ b/src/graphics/permanent.rs @@ -123,8 +123,8 @@ impl PermanentGraphicsState { vulkanalia::window::create_surface(&instance, &window, &window) }?; - let (physical_device, device, indices, sample_count, graphics_queue, - presentation_queue, enable_anisotropy, enable_swapchain) + let (device, indices, sample_count, graphics_queue, presentation_queue, + enable_anisotropy, enable_swapchain) = init_vulkan_device(&instance, &surface, enable_validation, enable_portability)?; @@ -134,7 +134,7 @@ impl PermanentGraphicsState { window, entry, instance, debug_messager, surface, device, graphics_queue, presentation_queue }, GraphicsStateForReinit { - physical_device, indices, sample_count, descriptor_set_layout, + indices, sample_count, descriptor_set_layout, }, enable_anisotropy, enable_swapchain)) } @@ -215,7 +215,6 @@ impl PermanentGraphicsState { // initial startup, and again any time the window-dressing needs to be // reinitialized. Most notably, they are not needed when rendering. pub struct GraphicsStateForReinit { - pub physical_device: vk::PhysicalDevice, pub indices: QueueFamilyIndices, pub sample_count: vk::SampleCountFlags, pub descriptor_set_layout: vk::DescriptorSetLayout, @@ -416,9 +415,8 @@ fn init_vulkan(window: &Window) fn init_vulkan_device(instance: &Instance, surface: &vk::SurfaceKHR, enable_validation: EnableValidation, enable_portability: EnablePortability) - -> Result<(vk::PhysicalDevice, Device, QueueFamilyIndices, - vk::SampleCountFlags, vk::Queue, vk::Queue, EnableAnisotropy, - EnableSwapchain)> + -> Result<(Device, QueueFamilyIndices, vk::SampleCountFlags, vk::Queue, + vk::Queue, EnableAnisotropy, EnableSwapchain)> { let (physical_device, indices, sample_count) = pick_vulkan_device(instance, surface)?; @@ -541,8 +539,8 @@ fn init_vulkan_device(instance: &Instance, surface: &vk::SurfaceKHR, device.get_device_queue(indices.presentation, 0) }; - Ok((physical_device, device, indices, sample_count, graphics_queue, - presentation_queue, enable_anisotropy, enable_swapchain)) + Ok((device, indices, sample_count, graphics_queue, presentation_queue, + enable_anisotropy, enable_swapchain)) } diff --git a/src/graphics/util.rs b/src/graphics/util.rs index 6190542..c970d18 100644 --- a/src/graphics/util.rs +++ b/src/graphics/util.rs @@ -9,21 +9,19 @@ use vulkanalia::vk::{ self, Handle, HasBuilder, InstanceV1_0, DeviceV1_0 }; #[allow(unsafe_code)] -pub fn init_buffer<T>(instance: &Instance, - physical_device: &vk::PhysicalDevice, device: &Device, - queue: &vk::Queue, command_pool: &vk::CommandPool, +pub fn init_buffer<T>(instance: &Instance, device: &Device, queue: &vk::Queue, + command_pool: &vk::CommandPool, usage: vk::BufferUsageFlags, contents: &[T]) -> Result<(vk::Buffer, vk::DeviceMemory)> { let (staging_buffer, staging_memory, size) - = stage_in_buffer(instance, physical_device, device, contents)?; + = stage_in_buffer(instance, device, contents)?; let final_usage = vk::BufferUsageFlags::TRANSFER_DST | usage; let final_memory_flags = vk::MemoryPropertyFlags::DEVICE_LOCAL; let (final_buffer, device_memory) - = allocate_buffer(instance, physical_device, device, - size as vk::DeviceSize, final_usage, - final_memory_flags)?; + = allocate_buffer(instance, device, size as vk::DeviceSize, + final_usage, final_memory_flags)?; copy_buffer(device, queue, command_pool, &staging_buffer, &final_buffer, size as vk::DeviceSize)?; @@ -35,9 +33,8 @@ pub fn init_buffer<T>(instance: &Instance, } -pub fn stage_in_buffer<T>(instance: &Instance, - physical_device: &vk::PhysicalDevice, - device: &Device, contents: &[T]) +pub fn stage_in_buffer<T>(instance: &Instance, device: &Device, + contents: &[T]) -> Result<(vk::Buffer, vk::DeviceMemory, usize)> { let size = size_of::<T>() * contents.len(); @@ -46,9 +43,8 @@ pub fn stage_in_buffer<T>(instance: &Instance, let staging_memory_flags = vk::MemoryPropertyFlags::HOST_COHERENT | vk::MemoryPropertyFlags::HOST_VISIBLE; let (staging_buffer, staging_memory) - = allocate_buffer(instance, physical_device, device, - size as vk::DeviceSize, staging_usage, - staging_memory_flags)?; + = allocate_buffer(instance, device, size as vk::DeviceSize, + staging_usage, staging_memory_flags)?; let host_memory = unsafe { device.map_memory(staging_memory, 0, size as vk::DeviceSize, @@ -66,12 +62,13 @@ pub fn stage_in_buffer<T>(instance: &Instance, #[allow(unsafe_code)] -pub fn allocate_buffer(instance: &Instance, - physical_device: &vk::PhysicalDevice, device: &Device, +pub fn allocate_buffer(instance: &Instance, device: &Device, size: vk::DeviceSize, usage: vk::BufferUsageFlags, memory_flags: vk::MemoryPropertyFlags) -> Result<(vk::Buffer, vk::DeviceMemory)> { + let physical_device = device.physical_device(); + let buffer_info = vk::BufferCreateInfo::builder() .size(size) .usage(usage) @@ -85,7 +82,7 @@ pub fn allocate_buffer(instance: &Instance, // one to tell us about it, and we let it. let requirements = unsafe { device.get_buffer_memory_requirements(buffer) }; - let type_index = pick_memory_type(instance, physical_device, + let type_index = pick_memory_type(instance, &physical_device, &memory_flags, &requirements)?; let memory_info = vk::MemoryAllocateInfo::builder() diff --git a/src/graphics/window_dressing.rs b/src/graphics/window_dressing.rs index 065bd53..09410bc 100644 --- a/src/graphics/window_dressing.rs +++ b/src/graphics/window_dressing.rs @@ -109,33 +109,30 @@ impl WindowDressing { let surface = &permanent.surface; let device = &permanent.device; let graphics_queue = &permanent.graphics_queue; - let physical_device = &for_reinit.physical_device; let sample_count = for_reinit.sample_count; let indices = &for_reinit.indices; let swapchain = init_swapchain( - window, instance, surface, &physical_device, device, &indices)?; + window, instance, surface, device, &indices)?; let (color_image, color_image_memory, color_image_view) - = init_color(instance, &physical_device, device, - &swapchain.extent, sample_count, swapchain.format)?; + = init_color(instance, device, &swapchain.extent, sample_count, + swapchain.format)?; let (depth_image, depth_image_memory, depth_image_view, depth_format) - = init_depth(instance, &physical_device, device, - &swapchain.extent, sample_count)?; + = init_depth(instance, device, &swapchain.extent, sample_count)?; let (primary_command_pool, transient_command_pool) = init_command_pools(device, indices)?; let (texture_image, texture_image_memory, texture_image_view, mip_count) - = init_texture(instance, physical_device, device, - graphics_queue, &transient_command_pool)?; + = init_texture(instance, device, graphics_queue, + &transient_command_pool)?; let sampler = init_sampler(&device, &enable_anisotropy, mip_count)?; let (uniform_buffers, uniform_buffer_memory) - = init_uniform_buffers(instance, physical_device, device, - swapchain.images.len())?; + = init_uniform_buffers(instance, device, swapchain.images.len())?; let descriptor_pool = init_descriptor_pool(device, swapchain.images.len())?; @@ -174,7 +171,6 @@ impl WindowDressing { let instance = &permanent.instance; let surface = &permanent.surface; let device = &permanent.device; - let physical_device = &for_reinit.physical_device; let sample_count = for_reinit.sample_count; let indices = &for_reinit.indices; @@ -182,20 +178,18 @@ impl WindowDressing { self.destroy_replaceable(device); - let swapchain = init_swapchain( - window, instance, surface, &physical_device, device, &indices)?; + let swapchain + = init_swapchain(window, instance, surface, device, &indices)?; let (color_image, color_image_memory, color_image_view) - = init_color(instance, &physical_device, device, - &swapchain.extent, sample_count, swapchain.format)?; + = init_color(instance, device, &swapchain.extent, sample_count, + swapchain.format)?; let (depth_image, depth_image_memory, depth_image_view, depth_format) - = init_depth(instance, &physical_device, device, - &swapchain.extent, sample_count)?; + = init_depth(instance, device, &swapchain.extent, sample_count)?; let (uniform_buffers, uniform_buffer_memory) - = init_uniform_buffers(instance, physical_device, device, - swapchain.images.len())?; + = init_uniform_buffers(instance, device, swapchain.images.len())?; // Notice that we did NOT reuse the descriptor pool. let descriptor_pool @@ -284,14 +278,15 @@ impl WindowDressing { #[allow(unsafe_code)] fn init_swapchain(window: &Window, instance: &Instance, - surface: &vk::SurfaceKHR, - physical_device: &vk::PhysicalDevice, device: &Device, + surface: &vk::SurfaceKHR, device: &Device, indices: &QueueFamilyIndices) -> Result<Swapchain> { + let physical_device = device.physical_device(); + let (capabilities, formats, presentation_modes) = PermanentGraphicsState::find_device_swapchain_features( - instance, surface, physical_device)?.require()?; + instance, surface, &physical_device)?.require()?; let format = pick_surface_format(&formats)?; @@ -361,13 +356,12 @@ fn init_swapchain(window: &Window, instance: &Instance, #[allow(unsafe_code)] -fn init_color(instance: &Instance, physical_device: &vk::PhysicalDevice, - device: &Device, extent: &vk::Extent2D, +fn init_color(instance: &Instance, device: &Device, extent: &vk::Extent2D, sample_count: vk::SampleCountFlags, format: vk::Format) -> Result<(vk::Image, vk::DeviceMemory, vk::ImageView)> { let (image, image_memory) - = allocate_image(instance, physical_device, device, + = allocate_image(instance, device, extent.width, extent.height, 1, sample_count, format, vk::ImageTiling::OPTIMAL, @@ -383,15 +377,16 @@ fn init_color(instance: &Instance, physical_device: &vk::PhysicalDevice, #[allow(unsafe_code)] -fn init_depth(instance: &Instance, physical_device: &vk::PhysicalDevice, - device: &Device, extent: &vk::Extent2D, +fn init_depth(instance: &Instance, device: &Device, extent: &vk::Extent2D, sample_count: vk::SampleCountFlags) -> Result<(vk::Image, vk::DeviceMemory, vk::ImageView, vk::Format)> { - let format = pick_depth_format(instance, physical_device)?; + let physical_device = device.physical_device(); + + let format = pick_depth_format(instance, &physical_device)?; let (image, image_memory) - = allocate_image(instance, physical_device, device, + = allocate_image(instance, device, extent.width, extent.height, 1, sample_count, format, vk::ImageTiling::OPTIMAL, @@ -406,11 +401,12 @@ fn init_depth(instance: &Instance, physical_device: &vk::PhysicalDevice, #[allow(unsafe_code)] -fn init_texture(instance: &Instance, - physical_device: &vk::PhysicalDevice, device: &Device, - queue: &vk::Queue, command_pool: &vk::CommandPool) +fn init_texture(instance: &Instance, device: &Device, queue: &vk::Queue, + command_pool: &vk::CommandPool) -> Result<(vk::Image, vk::DeviceMemory, vk::ImageView, u32)> { + let physical_device = device.physical_device(); + let png = include_bytes!("../../textures/forest_leaves_04_diff.png"); let decoder = Decoder::new(Cursor::new(png)); @@ -419,7 +415,7 @@ fn init_texture(instance: &Instance, let (width, height) = reader.info().size(); let format_properties = unsafe { - instance.get_physical_device_format_properties(*physical_device, + instance.get_physical_device_format_properties(physical_device, vk::Format::R8G8B8A8_SRGB) }; let has_linear_filter = format_properties @@ -437,10 +433,10 @@ fn init_texture(instance: &Instance, reader.next_frame(&mut pixels)?; let (staging_buffer, staging_memory, _byte_size) - = stage_in_buffer(instance, physical_device, device, &pixels)?; + = stage_in_buffer(instance, device, &pixels)?; let (image, image_memory) - = allocate_image(instance, physical_device, device, + = allocate_image(instance, device, width, height, mip_count, vk::SampleCountFlags::_1, vk::Format::R8G8B8A8_SRGB, vk::ImageTiling::OPTIMAL, @@ -472,9 +468,7 @@ fn init_texture(instance: &Instance, } -fn init_uniform_buffers(instance: &Instance, - physical_device: &vk::PhysicalDevice, device: &Device, - count: usize) +fn init_uniform_buffers(instance: &Instance, device: &Device, count: usize) -> Result<(Vec<vk::Buffer>, Vec<vk::DeviceMemory>)> { let mut buffers = Vec::new(); @@ -482,7 +476,7 @@ fn init_uniform_buffers(instance: &Instance, for _ in 0 .. count { let (buffer, memory) = allocate_buffer( - instance, physical_device, device, + instance, device, size_of::<UniformBlock<f32>>() as vk::DeviceSize, vk::BufferUsageFlags::UNIFORM_BUFFER, vk::MemoryPropertyFlags::HOST_COHERENT @@ -721,13 +715,15 @@ fn pick_image_extent(window: &Window, #[allow(unsafe_code)] -fn allocate_image(instance: &Instance, physical_device: &vk::PhysicalDevice, - device: &Device, width: u32, height: u32, mip_count: u32, +fn allocate_image(instance: &Instance, device: &Device, width: u32, + height: u32, mip_count: u32, sample_count: vk::SampleCountFlags, format: vk::Format, tiling: vk::ImageTiling, usage: vk::ImageUsageFlags, memory_flags: vk::MemoryPropertyFlags) -> Result<(vk::Image, vk::DeviceMemory)> { + let physical_device = device.physical_device(); + let image_info = vk::ImageCreateInfo::builder() .image_type(vk::ImageType::_2D) .extent(vk::Extent3D { width, height, depth: 1 }) @@ -744,7 +740,7 @@ fn allocate_image(instance: &Instance, physical_device: &vk::PhysicalDevice, let requirements = unsafe { device.get_image_memory_requirements(image) }; - let type_index = pick_memory_type(instance, physical_device, + let type_index = pick_memory_type(instance, &physical_device, &memory_flags, &requirements)?; let image_memory_info = vk::MemoryAllocateInfo::builder() diff --git a/src/main.rs b/src/main.rs index 7bf8f5f..6382f51 100644 --- a/src/main.rs +++ b/src/main.rs @@ -73,8 +73,7 @@ impl Surreality { enable_anisotropy)?; let mut render_state = RenderState::new(&permanent, &for_reinit, &window_dressing)?; - render_state.set_model(Model::new(&permanent, &for_reinit, - &window_dressing)?); + render_state.set_model(Model::new(&permanent, &window_dressing)?); *self.window_dressing.get_mut() = Some(window_dressing); *self.render_state.get_mut() = Some(render_state); |