Compare commits
2 commits
dd8a5a97ea
...
a669247406
Author | SHA1 | Date | |
---|---|---|---|
a669247406 | |||
001547dbc2 |
21 changed files with 83 additions and 85 deletions
|
@ -1,5 +1,5 @@
|
||||||
use crate::display::window::Window;
|
use crate::display::window::Window;
|
||||||
use crate::vulkan::VkRenderContext;
|
use crate::renderer::vulkan::VkRenderContext;
|
||||||
use winit::application::ApplicationHandler;
|
use winit::application::ApplicationHandler;
|
||||||
use winit::event::WindowEvent;
|
use winit::event::WindowEvent;
|
||||||
use winit::event_loop::ActiveEventLoop;
|
use winit::event_loop::ActiveEventLoop;
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use winit::event_loop::{ControlFlow, EventLoop};
|
use winit::event_loop::{ControlFlow, EventLoop};
|
||||||
|
|
||||||
mod display;
|
mod display;
|
||||||
mod vulkan;
|
mod renderer;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
env_logger::init();
|
env_logger::init();
|
||||||
|
|
1
src/renderer/mod.rs
Normal file
1
src/renderer/mod.rs
Normal file
|
@ -0,0 +1 @@
|
||||||
|
pub mod vulkan;
|
41
src/renderer/vulkan/mod.rs
Normal file
41
src/renderer/vulkan/mod.rs
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
mod vk_render_context;
|
||||||
|
pub use vk_render_context::VkRenderContext;
|
||||||
|
|
||||||
|
mod vk_instance;
|
||||||
|
pub use vk_instance::VkInstance;
|
||||||
|
|
||||||
|
mod vk_surface;
|
||||||
|
pub use vk_surface::{SwapchainSupportDetails, VkSurface};
|
||||||
|
|
||||||
|
mod vk_physical_device;
|
||||||
|
pub use vk_physical_device::VkPhysicalDevice;
|
||||||
|
|
||||||
|
mod vk_device;
|
||||||
|
pub use vk_device::VkDevice;
|
||||||
|
|
||||||
|
mod vk_swapchain;
|
||||||
|
pub use vk_swapchain::VkSwapchain;
|
||||||
|
|
||||||
|
mod vk_shader_module;
|
||||||
|
pub use vk_shader_module::VkShaderModule;
|
||||||
|
|
||||||
|
mod vk_graphics_pipeline;
|
||||||
|
pub use vk_graphics_pipeline::VkGraphicsPipeline;
|
||||||
|
|
||||||
|
mod vk_render_pass;
|
||||||
|
pub use vk_render_pass::VkRenderPass;
|
||||||
|
|
||||||
|
mod vk_semaphore;
|
||||||
|
pub use vk_semaphore::VkSemaphore;
|
||||||
|
|
||||||
|
mod vk_command_pool;
|
||||||
|
pub use vk_command_pool::VkCommandPool;
|
||||||
|
|
||||||
|
mod vk_framebuffer;
|
||||||
|
pub use vk_framebuffer::VkFramebuffer;
|
||||||
|
|
||||||
|
mod vk_fence;
|
||||||
|
pub use vk_fence::VkFence;
|
||||||
|
|
||||||
|
mod utils;
|
||||||
|
mod vertex;
|
|
@ -1,4 +1,4 @@
|
||||||
use crate::vulkan::VkDevice;
|
use super::VkDevice;
|
||||||
use ash::prelude::VkResult;
|
use ash::prelude::VkResult;
|
||||||
use ash::vk;
|
use ash::vk;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
|
@ -1,4 +1,4 @@
|
||||||
use crate::vulkan::{VkInstance, VkPhysicalDevice};
|
use super::{VkInstance, VkPhysicalDevice};
|
||||||
use ash::prelude::VkResult;
|
use ash::prelude::VkResult;
|
||||||
use ash::vk;
|
use ash::vk;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
|
@ -1,4 +1,4 @@
|
||||||
use crate::vulkan::VkDevice;
|
use super::VkDevice;
|
||||||
use ash::vk;
|
use ash::vk;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
use crate::vulkan::vk_render_pass::VkRenderPass;
|
use super::{VkDevice, VkRenderPass, VkSwapchain};
|
||||||
use crate::vulkan::{VkDevice, VkSwapchain};
|
|
||||||
use ash::vk;
|
use ash::vk;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
use crate::vulkan::vk_render_pass::VkRenderPass;
|
use super::{VkDevice, VkRenderPass, VkShaderModule, VkSwapchain};
|
||||||
use crate::vulkan::{VkDevice, VkShaderModule, VkSwapchain};
|
|
||||||
use ash::vk;
|
use ash::vk;
|
||||||
use std::ffi::CStr;
|
use std::ffi::CStr;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
@ -22,7 +21,7 @@ impl VkGraphicsPipeline {
|
||||||
let shader_entry_name = CStr::from_bytes_with_nul(b"main\0")?;
|
let shader_entry_name = CStr::from_bytes_with_nul(b"main\0")?;
|
||||||
|
|
||||||
let vert_shader_module =
|
let vert_shader_module =
|
||||||
VkShaderModule::from_spv_file(device.clone(), "res/shaders/main.vert.spv")?;
|
VkShaderModule::from_spv_file(device, "res/shaders/main.vert.spv")?;
|
||||||
|
|
||||||
let vert_shader_info = vk::PipelineShaderStageCreateInfo::default()
|
let vert_shader_info = vk::PipelineShaderStageCreateInfo::default()
|
||||||
.module(vert_shader_module.handle)
|
.module(vert_shader_module.handle)
|
||||||
|
@ -30,7 +29,7 @@ impl VkGraphicsPipeline {
|
||||||
.stage(vk::ShaderStageFlags::VERTEX);
|
.stage(vk::ShaderStageFlags::VERTEX);
|
||||||
|
|
||||||
let frag_shader_module =
|
let frag_shader_module =
|
||||||
VkShaderModule::from_spv_file(device.clone(), "res/shaders/main.frag.spv")?;
|
VkShaderModule::from_spv_file(device, "res/shaders/main.frag.spv")?;
|
||||||
|
|
||||||
let frag_shader_info = vk::PipelineShaderStageCreateInfo::default()
|
let frag_shader_info = vk::PipelineShaderStageCreateInfo::default()
|
||||||
.module(frag_shader_module.handle)
|
.module(frag_shader_module.handle)
|
|
@ -1,5 +1,7 @@
|
||||||
use crate::vulkan::utils::layers::{use_layers, LayersSelector};
|
use crate::renderer::vulkan::{
|
||||||
use crate::vulkan::VkPhysicalDevice;
|
utils::layers::{use_layers, LayersSelector},
|
||||||
|
VkPhysicalDevice,
|
||||||
|
};
|
||||||
use ash::khr::surface;
|
use ash::khr::surface;
|
||||||
use ash::{vk, Entry, Instance};
|
use ash::{vk, Entry, Instance};
|
||||||
use std::ffi::{c_char, CStr, CString};
|
use std::ffi::{c_char, CStr, CString};
|
||||||
|
@ -26,7 +28,7 @@ impl VkInstance {
|
||||||
layer_property.layer_name_as_c_str().ok(),
|
layer_property.layer_name_as_c_str().ok(),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
.unwrap_or_default();
|
.unwrap_or_default();
|
||||||
log::debug!("{layer_property:#?} {layer_extensions:#?}");
|
log::debug!("{layer_property:#?} {layer_extensions:#?}");
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
use crate::vulkan::vk_surface::VkSurface;
|
use super::VkSurface;
|
||||||
use ash::vk;
|
use ash::vk;
|
||||||
|
|
||||||
pub struct VkPhysicalDevice {
|
pub struct VkPhysicalDevice {
|
|
@ -1,4 +1,4 @@
|
||||||
use crate::vulkan::{
|
use super::{
|
||||||
VkCommandPool, VkDevice, VkFence, VkFramebuffer, VkGraphicsPipeline, VkInstance, VkPhysicalDevice,
|
VkCommandPool, VkDevice, VkFence, VkFramebuffer, VkGraphicsPipeline, VkInstance, VkPhysicalDevice,
|
||||||
VkRenderPass, VkSemaphore, VkSurface, VkSwapchain,
|
VkRenderPass, VkSemaphore, VkSurface, VkSwapchain,
|
||||||
};
|
};
|
||||||
|
@ -187,7 +187,7 @@ impl VkRenderContext {
|
||||||
.queue_submit(*queue, &[submit_info], self.in_flight_fence.handle)?
|
.queue_submit(*queue, &[submit_info], self.in_flight_fence.handle)?
|
||||||
};
|
};
|
||||||
|
|
||||||
let swapchains = [self.swapchain.swapchain.unwrap()];
|
let swapchains = [self.swapchain.handle.unwrap()];
|
||||||
let indices = [index];
|
let indices = [index];
|
||||||
let present_info = vk::PresentInfoKHR::default()
|
let present_info = vk::PresentInfoKHR::default()
|
||||||
.wait_semaphores(&signal_semaphores)
|
.wait_semaphores(&signal_semaphores)
|
|
@ -1,4 +1,4 @@
|
||||||
use crate::vulkan::{VkDevice, VkSwapchain};
|
use super::{VkDevice, VkSwapchain};
|
||||||
use ash::prelude::VkResult;
|
use ash::prelude::VkResult;
|
||||||
use ash::vk;
|
use ash::vk;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
|
@ -1,4 +1,4 @@
|
||||||
use crate::vulkan::VkDevice;
|
use super::VkDevice;
|
||||||
use ash::vk;
|
use ash::vk;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use crate::vulkan::VkDevice;
|
use super::VkDevice;
|
||||||
use ash::vk;
|
use ash::vk;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
@ -10,7 +10,7 @@ pub struct VkShaderModule {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl VkShaderModule {
|
impl VkShaderModule {
|
||||||
pub fn from_spv_file<P: AsRef<Path>>(device: Arc<VkDevice>, path: P) -> anyhow::Result<Self> {
|
pub fn from_spv_file<P: AsRef<Path>>(device: &Arc<VkDevice>, path: P) -> anyhow::Result<Self> {
|
||||||
let mut file = std::fs::File::open(&path)?;
|
let mut file = std::fs::File::open(&path)?;
|
||||||
let frag_shader_str = ash::util::read_spv(&mut file)?;
|
let frag_shader_str = ash::util::read_spv(&mut file)?;
|
||||||
|
|
||||||
|
@ -26,7 +26,7 @@ impl VkShaderModule {
|
||||||
);
|
);
|
||||||
|
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
device,
|
device: device.clone(),
|
||||||
handle: shader_module,
|
handle: shader_module,
|
||||||
})
|
})
|
||||||
}
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
use crate::vulkan::{VkInstance, VkPhysicalDevice};
|
use super::{VkInstance, VkPhysicalDevice};
|
||||||
use ash::prelude::VkResult;
|
use ash::prelude::VkResult;
|
||||||
use ash::vk;
|
use ash::vk;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
@ -13,7 +13,7 @@ pub struct SwapchainSupportDetails(
|
||||||
pub struct VkSurface {
|
pub struct VkSurface {
|
||||||
instance: Arc<VkInstance>,
|
instance: Arc<VkInstance>,
|
||||||
|
|
||||||
pub(super) surface: vk::SurfaceKHR,
|
pub(super) handle: vk::SurfaceKHR,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl VkSurface {
|
impl VkSurface {
|
||||||
|
@ -34,7 +34,7 @@ impl VkSurface {
|
||||||
|
|
||||||
log::debug!("Surface created ({:?})", surface);
|
log::debug!("Surface created ({:?})", surface);
|
||||||
|
|
||||||
Ok(Self { instance, surface })
|
Ok(Self { instance, handle: surface })
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn physical_device_queue_supported(
|
pub fn physical_device_queue_supported(
|
||||||
|
@ -48,7 +48,7 @@ impl VkSurface {
|
||||||
.get_physical_device_surface_support(
|
.get_physical_device_surface_support(
|
||||||
physical_device.handle,
|
physical_device.handle,
|
||||||
queue_index,
|
queue_index,
|
||||||
self.surface,
|
self.handle,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -61,17 +61,17 @@ impl VkSurface {
|
||||||
let formats = self
|
let formats = self
|
||||||
.instance
|
.instance
|
||||||
.surface_loader
|
.surface_loader
|
||||||
.get_physical_device_surface_formats(physical_device.handle, self.surface)?;
|
.get_physical_device_surface_formats(physical_device.handle, self.handle)?;
|
||||||
|
|
||||||
let capabilities = self
|
let capabilities = self
|
||||||
.instance
|
.instance
|
||||||
.surface_loader
|
.surface_loader
|
||||||
.get_physical_device_surface_capabilities(physical_device.handle, self.surface)?;
|
.get_physical_device_surface_capabilities(physical_device.handle, self.handle)?;
|
||||||
|
|
||||||
let present_modes = self
|
let present_modes = self
|
||||||
.instance
|
.instance
|
||||||
.surface_loader
|
.surface_loader
|
||||||
.get_physical_device_surface_present_modes(physical_device.handle, self.surface)?;
|
.get_physical_device_surface_present_modes(physical_device.handle, self.handle)?;
|
||||||
|
|
||||||
Ok(SwapchainSupportDetails(
|
Ok(SwapchainSupportDetails(
|
||||||
formats,
|
formats,
|
||||||
|
@ -87,8 +87,8 @@ impl Drop for VkSurface {
|
||||||
unsafe {
|
unsafe {
|
||||||
self.instance
|
self.instance
|
||||||
.surface_loader
|
.surface_loader
|
||||||
.destroy_surface(self.surface, None);
|
.destroy_surface(self.handle, None);
|
||||||
}
|
}
|
||||||
log::debug!("Surface destroyed ({:?})", self.surface);
|
log::debug!("Surface destroyed ({:?})", self.handle);
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,8 +1,5 @@
|
||||||
|
use super::{SwapchainSupportDetails, VkDevice, VkFramebuffer, VkPhysicalDevice, VkRenderPass, VkSemaphore, VkSurface};
|
||||||
use crate::display::Window;
|
use crate::display::Window;
|
||||||
use crate::vulkan::vk_render_pass::VkRenderPass;
|
|
||||||
use crate::vulkan::vk_semaphore::VkSemaphore;
|
|
||||||
use crate::vulkan::vk_surface::SwapchainSupportDetails;
|
|
||||||
use crate::vulkan::{VkDevice, VkFramebuffer, VkPhysicalDevice, VkSurface};
|
|
||||||
use ash::prelude::VkResult;
|
use ash::prelude::VkResult;
|
||||||
use ash::vk;
|
use ash::vk;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
@ -11,7 +8,7 @@ pub struct VkSwapchain {
|
||||||
surface: Arc<VkSurface>,
|
surface: Arc<VkSurface>,
|
||||||
device: Arc<VkDevice>,
|
device: Arc<VkDevice>,
|
||||||
|
|
||||||
pub(super) swapchain: Option<vk::SwapchainKHR>,
|
pub(super) handle: Option<vk::SwapchainKHR>,
|
||||||
swapchain_support_details: SwapchainSupportDetails,
|
swapchain_support_details: SwapchainSupportDetails,
|
||||||
|
|
||||||
pub(super) desired_image_count: u32,
|
pub(super) desired_image_count: u32,
|
||||||
|
@ -64,7 +61,7 @@ impl VkSwapchain {
|
||||||
surface: surface.clone(),
|
surface: surface.clone(),
|
||||||
device: device.clone(),
|
device: device.clone(),
|
||||||
|
|
||||||
swapchain: None,
|
handle: None,
|
||||||
new_requested_surface_resolution: None,
|
new_requested_surface_resolution: None,
|
||||||
swapchain_support_details,
|
swapchain_support_details,
|
||||||
desired_image_count,
|
desired_image_count,
|
||||||
|
@ -89,7 +86,7 @@ impl VkSwapchain {
|
||||||
|
|
||||||
let mut swapchain_create_info = self.create_swapchain_info(&self.surface);
|
let mut swapchain_create_info = self.create_swapchain_info(&self.surface);
|
||||||
|
|
||||||
if let Some(old_swapchain) = self.swapchain {
|
if let Some(old_swapchain) = self.handle {
|
||||||
swapchain_create_info.old_swapchain = old_swapchain;
|
swapchain_create_info.old_swapchain = old_swapchain;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -114,14 +111,14 @@ impl VkSwapchain {
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
if log::log_enabled!(log::Level::Debug) {
|
if log::log_enabled!(log::Level::Debug) {
|
||||||
let label = match self.swapchain {
|
let label = match self.handle {
|
||||||
None => "Swapchain created",
|
None => "Swapchain created",
|
||||||
Some(_) => "Swapchain updated",
|
Some(_) => "Swapchain updated",
|
||||||
};
|
};
|
||||||
log::debug!("{label} ({swapchain:?}) : {swapchain_create_info:#?}");
|
log::debug!("{label} ({swapchain:?}) : {swapchain_create_info:#?}");
|
||||||
}
|
}
|
||||||
|
|
||||||
self.swapchain = Some(swapchain);
|
self.handle = Some(swapchain);
|
||||||
self.present_image_views = Some(present_images_view);
|
self.present_image_views = Some(present_images_view);
|
||||||
self.present_images = Some(present_images);
|
self.present_images = Some(present_images);
|
||||||
|
|
||||||
|
@ -177,7 +174,7 @@ impl VkSwapchain {
|
||||||
pub(super) fn acquire_next_image(&self, semaphore: &VkSemaphore) -> VkResult<(u32, bool)> {
|
pub(super) fn acquire_next_image(&self, semaphore: &VkSemaphore) -> VkResult<(u32, bool)> {
|
||||||
unsafe {
|
unsafe {
|
||||||
self.device.swapchain_loader.acquire_next_image(
|
self.device.swapchain_loader.acquire_next_image(
|
||||||
self.swapchain.unwrap(),
|
self.handle.unwrap(),
|
||||||
u64::MAX,
|
u64::MAX,
|
||||||
semaphore.handle,
|
semaphore.handle,
|
||||||
vk::Fence::null(),
|
vk::Fence::null(),
|
||||||
|
@ -191,7 +188,7 @@ impl VkSwapchain {
|
||||||
|
|
||||||
fn create_swapchain_info(&self, surface: &VkSurface) -> vk::SwapchainCreateInfoKHR {
|
fn create_swapchain_info(&self, surface: &VkSurface) -> vk::SwapchainCreateInfoKHR {
|
||||||
vk::SwapchainCreateInfoKHR::default()
|
vk::SwapchainCreateInfoKHR::default()
|
||||||
.surface(surface.surface)
|
.surface(surface.handle)
|
||||||
.min_image_count(self.desired_image_count)
|
.min_image_count(self.desired_image_count)
|
||||||
.image_color_space(self.surface_format.color_space)
|
.image_color_space(self.surface_format.color_space)
|
||||||
.image_format(self.surface_format.format)
|
.image_format(self.surface_format.format)
|
||||||
|
@ -287,13 +284,13 @@ impl VkSwapchain {
|
||||||
|
|
||||||
impl Drop for VkSwapchain {
|
impl Drop for VkSwapchain {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
if let Some(swapchain) = self.swapchain {
|
if let Some(swapchain) = self.handle {
|
||||||
unsafe {
|
unsafe {
|
||||||
self.device
|
self.device
|
||||||
.swapchain_loader
|
.swapchain_loader
|
||||||
.destroy_swapchain(swapchain, None);
|
.destroy_swapchain(swapchain, None);
|
||||||
}
|
}
|
||||||
self.swapchain = None;
|
self.handle = None;
|
||||||
log::debug!("Swapchain destroyed ({swapchain:?})");
|
log::debug!("Swapchain destroyed ({swapchain:?})");
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,41 +0,0 @@
|
||||||
pub(self) mod vk_render_context;
|
|
||||||
pub use vk_render_context::VkRenderContext;
|
|
||||||
|
|
||||||
pub(self) mod vk_instance;
|
|
||||||
pub use vk_instance::VkInstance;
|
|
||||||
|
|
||||||
pub(self) mod vk_surface;
|
|
||||||
pub use vk_surface::VkSurface;
|
|
||||||
|
|
||||||
pub(self) mod vk_physical_device;
|
|
||||||
pub use vk_physical_device::VkPhysicalDevice;
|
|
||||||
|
|
||||||
pub(self) mod vk_device;
|
|
||||||
pub use vk_device::VkDevice;
|
|
||||||
|
|
||||||
pub(self) mod vk_swapchain;
|
|
||||||
pub use vk_swapchain::VkSwapchain;
|
|
||||||
|
|
||||||
pub(self) mod vk_shader_module;
|
|
||||||
pub use vk_shader_module::VkShaderModule;
|
|
||||||
|
|
||||||
pub(self) mod vk_graphics_pipeline;
|
|
||||||
pub use vk_graphics_pipeline::VkGraphicsPipeline;
|
|
||||||
|
|
||||||
mod vk_render_pass;
|
|
||||||
pub(self) use vk_render_pass::VkRenderPass;
|
|
||||||
|
|
||||||
mod vk_semaphore;
|
|
||||||
pub(self) use vk_semaphore::VkSemaphore;
|
|
||||||
|
|
||||||
mod vk_command_pool;
|
|
||||||
pub(self) use vk_command_pool::VkCommandPool;
|
|
||||||
|
|
||||||
mod vk_framebuffer;
|
|
||||||
pub(self) use vk_framebuffer::VkFramebuffer;
|
|
||||||
|
|
||||||
mod vk_fence;
|
|
||||||
pub(self) use vk_fence::VkFence;
|
|
||||||
|
|
||||||
mod utils;
|
|
||||||
mod vertex;
|
|
Loading…
Reference in a new issue