diff options
| -rw-r--r-- | src/graphics/mod.rs | 10 | ||||
| -rw-r--r-- | src/graphics/model.rs | 11 | ||||
| -rw-r--r-- | src/graphics/permanent.rs | 33 | ||||
| -rw-r--r-- | src/graphics/render.rs (renamed from src/graphics/render_state.rs) | 53 | ||||
| -rw-r--r-- | src/graphics/scene.rs | 18 | ||||
| -rw-r--r-- | src/graphics/texture.rs | 4 | ||||
| -rw-r--r-- | src/graphics/window_dressing.rs | 18 | ||||
| -rw-r--r-- | src/main.rs | 37 |
8 files changed, 86 insertions, 98 deletions
diff --git a/src/graphics/mod.rs b/src/graphics/mod.rs index 3d03f9c..bb12c80 100644 --- a/src/graphics/mod.rs +++ b/src/graphics/mod.rs @@ -1,9 +1,15 @@ #![deny(unsafe_code)] - pub mod model; pub mod permanent; -pub mod render_state; +pub mod render; pub mod texture; pub mod scene; pub mod util; pub mod window_dressing; + +pub use crate::graphics::model::Model; +pub use crate::graphics::texture::Texture; +pub use crate::graphics::permanent::{ Permanent, ForReinit }; +pub use crate::graphics::render::Render; +pub use crate::graphics::window_dressing::WindowDressing; + diff --git a/src/graphics/model.rs b/src/graphics/model.rs index 7b912b6..9c62d51 100644 --- a/src/graphics/model.rs +++ b/src/graphics/model.rs @@ -1,7 +1,6 @@ #![deny(unsafe_code)] use crate::error::*; -use crate::graphics::permanent::PermanentGraphicsState; -use crate::graphics::render_state::RenderState; +use crate::graphics::{ Permanent, Render }; use crate::graphics::util::init_buffer; use crate::linear_algebra::{ Vec3, Vec4, Transformation }; use crate::shader_data::{ Vertex, VertexPushBlock }; @@ -25,7 +24,7 @@ pub struct Model { impl Model { pub fn new(vertices: Vec<Vertex<f32>>, indices: Vec<u32>, - permanent: &PermanentGraphicsState) + permanent: &Permanent) -> Result<Self> { let device = &permanent.device; @@ -61,7 +60,7 @@ impl Model { } #[allow(unsafe_code)] - pub fn render(&self, time: f32, device: &Device, render_state: &RenderState, + pub fn render(&self, time: f32, device: &Device, render: &Render, command_buffer: &vk::CommandBuffer, descriptor_set: &vk::DescriptorSet) { @@ -78,7 +77,7 @@ impl Model { unsafe { device.cmd_bind_descriptor_sets(*command_buffer, vk::PipelineBindPoint::GRAPHICS, - render_state.pipeline_layout, + render.pipeline_layout, 0, &[*descriptor_set], &[]) @@ -99,7 +98,7 @@ impl Model { }; unsafe { - device.cmd_push_constants(*command_buffer, render_state.pipeline_layout, + device.cmd_push_constants(*command_buffer, render.pipeline_layout, vk::ShaderStageFlags::VERTEX, 0, push_block_bytes) diff --git a/src/graphics/permanent.rs b/src/graphics/permanent.rs index f359dbd..df86284 100644 --- a/src/graphics/permanent.rs +++ b/src/graphics/permanent.rs @@ -19,10 +19,10 @@ use winit::window::{ Window, WindowAttributes }; const VULKAN_FIRST_PORTABILITY_VERSION: Version = Version::new(1, 3, 216); -// The PermanentGraphicsState collects the various windowing-system and -// Vulkan objects which never need to be regenerated once they're created. The -// ones which do need that are collected below, in WindowDressing. -pub struct PermanentGraphicsState { +// Permanent is a state object which collects the various windowing-system +// and Vulkan objects which never need to be regenerated once they're created. +// The ones which do need that are collected below, in WindowDressing. +pub struct Permanent { // The "window" is the usual operating-system concept of a window; it's // provided by winit, and may be X11, Wayland, or some more curious thing. // The way we initialize Vulkan requires us to have at least one of these; @@ -100,11 +100,10 @@ pub struct EnableAnisotropy(pub bool); pub struct EnableSwapchain(pub bool); -impl PermanentGraphicsState { +impl Permanent { #[allow(unsafe_code)] pub fn new(event_loop: &ActiveEventLoop) - -> Result<(Self, GraphicsStateForReinit, EnableAnisotropy, - EnableSwapchain)> + -> Result<(Self, ForReinit, EnableAnisotropy, EnableSwapchain)> { let window = init_window(event_loop)?; @@ -141,11 +140,11 @@ impl PermanentGraphicsState { let (primary_command_pool, transient_command_pool) = init_command_pools(&device, &indices)?; - Ok((PermanentGraphicsState { + Ok((Permanent { window, entry, instance, debug_messager, surface, device, graphics_queue, presentation_queue, primary_command_pool, transient_command_pool, - }, GraphicsStateForReinit { + }, ForReinit { indices, sample_count, descriptor_set_layout, }, enable_anisotropy, enable_swapchain)) } @@ -230,18 +229,18 @@ impl PermanentGraphicsState { } -// The GraphicsStateForReinit connects Vulkan objects which are only needed -// during the creation of the window-dressing objects. They are used during -// initial startup, and again any time the window-dressing needs to be -// reinitialized. Most notably, they are not needed when rendering. -pub struct GraphicsStateForReinit { +// ForReinit is a state object that collects Vulkan objects which are only +// needed during the creation of the window-dressing objects. They are used +// during initial startup, and again any time the window-dressing needs to be +// reinitialized. Most notably, they are not needed when rendering. +pub struct ForReinit { pub indices: QueueFamilyIndices, pub sample_count: vk::SampleCountFlags, pub descriptor_set_layout: vk::DescriptorSetLayout, } -impl GraphicsStateForReinit { +impl ForReinit { #[allow(unsafe_code)] pub fn destroy(self, device: &Device) -> () { unsafe { @@ -494,7 +493,7 @@ fn init_vulkan_device(instance: &Instance, surface: &vk::SurfaceKHR, // we are discarding its results a second time. We'll do it for the // third and last time in swapchain creation. if let Acceptable::Accepted(_) - = PermanentGraphicsState::find_device_swapchain_features( + = Permanent::find_device_swapchain_features( &instance, &surface, &physical_device)? { extensions.push(swapchain_extension_name.as_ptr()); @@ -752,7 +751,7 @@ fn score_vulkan_device(instance: &Instance, surface: &vk::SurfaceKHR, // With that said, however, it only counts if we're able to actually // use it on the surface we have. Let's find out... if let Acceptable::Accepted(_) - = PermanentGraphicsState::find_device_swapchain_features( + = Permanent::find_device_swapchain_features( instance, surface, physical_device)? { // We don't count it for enough points to override a device type diff --git a/src/graphics/render_state.rs b/src/graphics/render.rs index 5848ac8..0b4a12a 100644 --- a/src/graphics/render_state.rs +++ b/src/graphics/render.rs @@ -1,11 +1,6 @@ #![deny(unsafe_code)] use crate::error::*; -use crate::graphics::permanent::{ - PermanentGraphicsState, GraphicsStateForReinit -}; -use crate::graphics::model::Model; -use crate::graphics::texture::Texture; -use crate::graphics::window_dressing::WindowDressing; +use crate::graphics::{ Permanent, ForReinit, WindowDressing, Model, Texture }; use crate::shader_data::{ Vertex, UniformBlock, VertexPushBlock }; use std::mem::size_of; @@ -14,11 +9,11 @@ use vulkanalia::Device; use vulkanalia::vk::{ self, Handle, HasBuilder, DeviceV1_0 }; -// The RenderState collects the Vulkan graphics objects which need to be -// regenerated or modified when the window changes, as with WindowDressing, -// and which are also used as part of rendering. +// Render is a state object that collects the Vulkan graphics objects which +// need to be regenerated or modified when the window changes, as with +// WindowDressing, and which are also used as part of rendering. #[derive(Debug)] -pub struct RenderState { +pub struct Render { pub render_pass: vk::RenderPass, pub pipeline: vk::Pipeline, @@ -32,9 +27,8 @@ pub struct RenderState { } -impl RenderState { - pub fn new(permanent: &PermanentGraphicsState, - for_reinit: &GraphicsStateForReinit, +impl Render { + pub fn new(permanent: &Permanent, for_reinit: &ForReinit, window_dressing: &WindowDressing, texture: &Texture) -> Result<Self> { @@ -72,7 +66,7 @@ impl RenderState { let model = None; - Ok(RenderState { + Ok(Render { render_pass, pipeline, pipeline_layout, @@ -85,8 +79,8 @@ impl RenderState { // This relies on its caller to have already waited for the device to be // idle. - pub fn reinit(&mut self, permanent: &PermanentGraphicsState, - for_reinit: &GraphicsStateForReinit, + pub fn reinit(&mut self, + permanent: &Permanent, for_reinit: &ForReinit, window_dressing: &WindowDressing, texture: &Texture) -> Result<()> { @@ -138,8 +132,7 @@ impl RenderState { // This relies on its caller to have already waited for the device to be // idle. #[allow(unsafe_code)] - pub fn destroy(mut self, permanent: &PermanentGraphicsState) - { + pub fn destroy(mut self, permanent: &Permanent) { self.destroy_replaceable(permanent); if let Some(model) = self.model { @@ -148,8 +141,7 @@ impl RenderState { } #[allow(unsafe_code)] - fn destroy_replaceable(&mut self, permanent: &PermanentGraphicsState) - { + fn destroy_replaceable(&mut self, permanent: &Permanent) { let device = &permanent.device; for framebuffer in &self.framebuffers { @@ -273,9 +265,9 @@ fn init_pipeline(device: &Device, let fragment_binary = include_bytes!( concat!(env!("OUT_DIR"), "/shader.frag.spv")); - let vertex_module = PermanentGraphicsState::load_spirv_shader_module( + let vertex_module = Permanent::load_spirv_shader_module( device, vertex_binary)?; - let fragment_module = PermanentGraphicsState::load_spirv_shader_module( + let fragment_module = Permanent::load_spirv_shader_module( device, fragment_binary)?; let vertex_stage_info = vk::PipelineShaderStageCreateInfo::builder() @@ -318,14 +310,15 @@ fn init_pipeline(device: &Device, .viewports(&viewports) .scissors(&scissor_list); - let rasterizer_state_info = vk::PipelineRasterizationStateCreateInfo::builder() - .depth_clamp_enable(false) - .rasterizer_discard_enable(false) - .polygon_mode(vk::PolygonMode::FILL) - .line_width(1.0) - .cull_mode(vk::CullModeFlags::BACK) - .front_face(vk::FrontFace::CLOCKWISE) - .depth_bias_enable(false); + let rasterizer_state_info + = vk::PipelineRasterizationStateCreateInfo::builder() + .depth_clamp_enable(false) + .rasterizer_discard_enable(false) + .polygon_mode(vk::PolygonMode::FILL) + .line_width(1.0) + .cull_mode(vk::CullModeFlags::BACK) + .front_face(vk::FrontFace::CLOCKWISE) + .depth_bias_enable(false); let multisample_state_info = vk::PipelineMultisampleStateCreateInfo::builder() diff --git a/src/graphics/scene.rs b/src/graphics/scene.rs index f1399b2..874e9ea 100644 --- a/src/graphics/scene.rs +++ b/src/graphics/scene.rs @@ -1,6 +1,6 @@ #![deny(unsafe_code)] use crate::error::*; -use crate::graphics::render_state::RenderState; +use crate::graphics::Render; use vulkanalia::Device; use vulkanalia::vk::{ self, HasBuilder, DeviceV1_0 }; @@ -8,13 +8,13 @@ use vulkanalia::vk::{ self, HasBuilder, DeviceV1_0 }; #[allow(unsafe_code)] pub fn generate_scene_commands<'a>(image_index: usize, time: f32, - render_state: &'a RenderState, + render: &'a Render, device: &Device, extent: &vk::Extent2D) -> Result<&'a vk::CommandBuffer> { - let command_buffer = &render_state.command_buffers[image_index]; - let framebuffer = &render_state.framebuffers[image_index]; - let descriptor_set = &render_state.descriptor_sets[image_index]; + let command_buffer = &render.command_buffers[image_index]; + let framebuffer = &render.framebuffers[image_index]; + let descriptor_set = &render.descriptor_sets[image_index]; let inheritance_info = vk::CommandBufferInheritanceInfo::builder(); @@ -44,7 +44,7 @@ pub fn generate_scene_commands<'a>(image_index: usize, time: f32, let clear_values = [color_clear_value, depth_clear_value]; let begin_pass_info = vk::RenderPassBeginInfo::builder() - .render_pass(render_state.render_pass) + .render_pass(render.render_pass) .framebuffer(*framebuffer) .render_area(render_area) .clear_values(&clear_values); @@ -57,11 +57,11 @@ pub fn generate_scene_commands<'a>(image_index: usize, time: f32, unsafe { device.cmd_bind_pipeline(*command_buffer, vk::PipelineBindPoint::GRAPHICS, - render_state.pipeline) + render.pipeline) }; - if let Some(model) = &render_state.model { - model.render(time, device, render_state, command_buffer, descriptor_set); + if let Some(model) = &render.model { + model.render(time, device, render, command_buffer, descriptor_set); } unsafe { device.cmd_end_render_pass(*command_buffer) }; diff --git a/src/graphics/texture.rs b/src/graphics/texture.rs index cd4dd7a..a6085e3 100644 --- a/src/graphics/texture.rs +++ b/src/graphics/texture.rs @@ -1,6 +1,6 @@ #![deny(unsafe_code)] use crate::error::*; -use crate::graphics::permanent::PermanentGraphicsState; +use crate::graphics::Permanent; use crate::graphics::util::{ stage_in_buffer, allocate_image, init_image_view, begin_transient_commands, end_transient_commands @@ -22,7 +22,7 @@ pub struct Texture { impl Texture { - pub fn new(permanent: &PermanentGraphicsState) -> Result<(Self, u32)> { + pub fn new(permanent: &Permanent) -> Result<(Self, u32)> { let graphics_queue = &permanent.graphics_queue; let instance = &permanent.instance; let device = &permanent.device; diff --git a/src/graphics/window_dressing.rs b/src/graphics/window_dressing.rs index 9afb03e..cca0dc8 100644 --- a/src/graphics/window_dressing.rs +++ b/src/graphics/window_dressing.rs @@ -1,9 +1,7 @@ #![deny(unsafe_code)] use crate::error::*; -use crate::graphics::permanent::{ - PermanentGraphicsState, GraphicsStateForReinit, QueueFamilyIndices, - EnableAnisotropy -}; +use crate::graphics::{ Permanent, ForReinit }; +use crate::graphics::permanent::{ QueueFamilyIndices, EnableAnisotropy }; use crate::graphics::util::{ allocate_buffer, allocate_image, init_image_view }; @@ -27,8 +25,8 @@ pub const N_SIMULTANEOUS_FRAMES: usize = 5; // The WindowDressing collects the Vulkan graphics objects which need to be // regenerated or modified when the window changes in certain ways, such as // resizing, but are not needed during rendering. The ones which don't need to -// be regenerated are collected in PermanentGraphicsState. The ones which are -// needed during rendering are collected in RenderState, below. +// be regenerated are collected in Permanent. The ones which are needed during +// rendering are collected in RenderState, below. #[derive(Debug)] pub struct WindowDressing { pub swapchain: Swapchain, @@ -89,8 +87,7 @@ pub struct Concurrency { impl WindowDressing { - pub fn new(permanent: &PermanentGraphicsState, - for_reinit: &GraphicsStateForReinit, + pub fn new(permanent: &Permanent, for_reinit: &ForReinit, enable_anisotropy: EnableAnisotropy, mip_count: u32) -> Result<Self> { @@ -140,8 +137,7 @@ impl WindowDressing { #[allow(unsafe_code)] - pub fn reinit(&mut self, permanent: &PermanentGraphicsState, - for_reinit: &GraphicsStateForReinit) + pub fn reinit(&mut self, permanent: &Permanent, for_reinit: &ForReinit) -> Result<()> { let window = &permanent.window; @@ -254,7 +250,7 @@ fn init_swapchain(window: &Window, instance: &Instance, let physical_device = device.physical_device(); let (capabilities, formats, presentation_modes) - = PermanentGraphicsState::find_device_swapchain_features( + = Permanent::find_device_swapchain_features( instance, surface, &physical_device)?.require()?; let format = pick_surface_format(&formats)?; diff --git a/src/main.rs b/src/main.rs index 6601982..3e8fb0b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,11 +1,6 @@ #![deny(unsafe_code)] use crate::error::*; -use crate::graphics::model::Model; -use crate::graphics::texture::Texture; -use crate::graphics::permanent::{ - PermanentGraphicsState, GraphicsStateForReinit -}; -use crate::graphics::render_state::RenderState; +use crate::graphics::{ Permanent, ForReinit, Render, Model, Texture }; use crate::graphics::scene::generate_scene_commands; use crate::graphics::window_dressing::{ WindowDressing, N_SIMULTANEOUS_FRAMES @@ -34,10 +29,10 @@ mod shader_data; struct Surreality { - permanent: RefCell<Option<PermanentGraphicsState>>, - for_reinit: RefCell<Option<GraphicsStateForReinit>>, + permanent: RefCell<Option<Permanent>>, + for_reinit: RefCell<Option<ForReinit>>, window_dressing: RefCell<Option<WindowDressing>>, - render_state: RefCell<Option<RenderState>>, + render: RefCell<Option<Render>>, texture: RefCell<Option<Texture>>, is_minimized: bool, is_reinit_queued: bool, @@ -55,7 +50,7 @@ impl Surreality { permanent: RefCell::new(None), for_reinit: RefCell::new(None), window_dressing: RefCell::new(None), - render_state: RefCell::new(None), + render: RefCell::new(None), texture: RefCell::new(None), is_minimized: false, is_reinit_queued: false, @@ -70,20 +65,20 @@ impl Surreality { #[allow(unsafe_code)] fn init(&mut self, event_loop: &ActiveEventLoop) -> Result<()> { let (permanent, for_reinit, enable_anisotropy, enable_swapchain) - = PermanentGraphicsState::new(event_loop)?; + = Permanent::new(event_loop)?; let (texture, mip_count) = Texture::new(&permanent)?; if enable_swapchain.0 { let window_dressing = WindowDressing::new(&permanent, &for_reinit, enable_anisotropy, mip_count)?; - let mut render_state = RenderState::new(&permanent, &for_reinit, - &window_dressing, &texture)?; + let mut render = Render::new(&permanent, &for_reinit, &window_dressing, + &texture)?; let (vertices, indices) = load_obj()?; - render_state.set_model(Model::new(vertices, indices, &permanent)?); + render.set_model(Model::new(vertices, indices, &permanent)?); *self.window_dressing.get_mut() = Some(window_dressing); - *self.render_state.get_mut() = Some(render_state); + *self.render.get_mut() = Some(render); } *self.texture.get_mut() = Some(texture); @@ -98,11 +93,11 @@ impl Surreality { && let Some(for_reinit) = self.for_reinit.borrow().as_ref() && let Some(window_dressing) = self.window_dressing.borrow_mut().as_mut() - && let Some(render_state) = self.render_state.borrow_mut().as_mut() + && let Some(render) = self.render.borrow_mut().as_mut() && let Some(texture) = self.texture.borrow_mut().as_mut() { window_dressing.reinit(permanent, for_reinit)?; - render_state.reinit(permanent, for_reinit, &window_dressing, &texture)?; + render.reinit(permanent, for_reinit, &window_dressing, &texture)?; } Ok(()) @@ -128,7 +123,7 @@ impl Surreality { if let Some(permanent) = self.permanent.borrow().as_ref() && let Some(window_dressing) = self.window_dressing.borrow_mut().as_mut() - && let Some(render_state) = self.render_state.borrow_mut().as_mut() + && let Some(render) = self.render.borrow_mut().as_mut() && window_id == permanent.window.id() { let device = &permanent.device; @@ -162,7 +157,7 @@ impl Surreality { let time = self.simulation_start.elapsed().as_secs_f32(); let command_buffer = generate_scene_commands( - image_index, time, &render_state, device, + image_index, time, &render, device, &window_dressing.swapchain.extent)?; render_uniforms(device, @@ -226,8 +221,8 @@ impl Drop for Surreality { unsafe { permanent.device.device_wait_idle() }.unwrap(); if let Some(window_dressing) = self.window_dressing.replace(None) { - if let Some(render_state) = self.render_state.replace(None) { - render_state.destroy(&permanent); + if let Some(render) = self.render.replace(None) { + render.destroy(&permanent); } window_dressing.destroy(&permanent.device); |