From 6f9d56d37021497be7964008b3f6966bedf02aa4 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Thu, 6 Apr 2023 15:28:09 -0400 Subject: [PATCH] Create pipeline and shader modules --- justfile | 8 ++++++ shaders/frag.spv | Bin 0 -> 572 bytes shaders/shader.frag | 9 +++++++ shaders/shader.vert | 20 +++++++++++++++ shaders/vert.spv | Bin 0 -> 1504 bytes src/app.rs | 17 ++++++++---- src/app/functions.rs | 60 ++++++++++++++++++++++++++++++++----------- 7 files changed, 94 insertions(+), 20 deletions(-) create mode 100644 justfile create mode 100644 shaders/frag.spv create mode 100644 shaders/shader.frag create mode 100644 shaders/shader.vert create mode 100644 shaders/vert.spv diff --git a/justfile b/justfile new file mode 100644 index 0000000..9d33e13 --- /dev/null +++ b/justfile @@ -0,0 +1,8 @@ +# Lists the available actions +default: + @just --list + +# Convert the human-readable shader code to Vulkan bytecode +compile-shaders: + glslc shaders/shader.vert -o shaders/vert.spv + glslc shaders/shader.frag -o shaders/frag.spv diff --git a/shaders/frag.spv b/shaders/frag.spv new file mode 100644 index 0000000000000000000000000000000000000000..da37f7ede672fe162b5322c32f5938a7836409a7 GIT binary patch literal 572 zcmYk2PfNo<6vUrx)7IAhv!FMrcod2U6+zU4NG^dYet=MtD1q3NHWj@2+5A*q1n0LV z(uK*}H#=`OEEHA71Yeo8+BI6MPEqd}+{a8uKdhAl0+aGA( z6gLqLrRPob_)qk0tMXUiuge|}IP@J=^z`Vvs`?d4G32BH#;R0mT<^MFnDF)EG3?WD_tXer-10N+zk@OW-Q!$6U%CCwBbK=EWe5;z^DZB6pD_lbols z;A~j*!x>3aHS;kY(h;+8ZeOy&`7ymK{vghmXU$b^!qFcb-s$KIjvgFG49B?z%J}l= z0x|SK?oo}|e?!JvmI~dbYWN26s-mvEFI!W+9#DzNtqAiXd>L!E#+MXRzb-s0PYs+e z%-(zt{$-81PdMuF^D>sh3l;pmr?cT(i!wCuPIE=E)*Gi;5`e=TcU3Vp71u3$SQh4c z=*ctVHF^4QGps0vb2F^gd3a_3^ZuGMTo*Rxm-^|JwKs&|=o@qV%^JI$<8L*zhH;ugQp(6Q*-1WD5ekY5ZsgJ+w=n7uQ6v$J(%~2hWvAR>d7DJE${}JnT6)B z7*GDS5O0dRCFX88w^vfr+kF{*y_K4t9?H(LSvj4QYY=-~< literal 0 HcmV?d00001 diff --git a/src/app.rs b/src/app.rs index 1814200..377a9df 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1,7 +1,7 @@ mod functions; use functions::{ - create_instance, create_logical_device, create_swapchain, create_swapchain_image_views, - pick_physical_device, + create_instance, create_logical_device, create_pipeline, create_swapchain, + create_swapchain_image_views, pick_physical_device, }; use crate::VALIDATION_ENABLED; @@ -32,13 +32,17 @@ impl App { pub unsafe fn create(window: &Window) -> Result { let loader = LibloadingLoader::new(LIBRARY)?; let entry = Entry::new(loader).map_err(|b| anyhow!("{}", b))?; + let mut data = AppData::default(); let instance = create_instance(window, &entry, &mut data)?; data.surface = vk_window::create_surface(&instance, window)?; + pick_physical_device(&instance, &mut data)?; let device = create_logical_device(&instance, &mut data)?; + create_swapchain(window, &instance, &device, &mut data)?; create_swapchain_image_views(&device, &mut data)?; + create_pipeline(&device, &mut data)?; Ok(Self { entry, @@ -61,13 +65,13 @@ impl App { .for_each(|v| self.device.destroy_image_view(*v, None)); self.device.destroy_swapchain_khr(self.data.swapchain, None); self.device.destroy_device(None); + self.instance.destroy_surface_khr(self.data.surface, None); if VALIDATION_ENABLED { self.instance .destroy_debug_utils_messenger_ext(self.data.messenger, None); } - self.instance.destroy_surface_khr(self.data.surface, None); self.instance.destroy_instance(None); } } @@ -83,6 +87,7 @@ pub struct AppData { physical_device: vk::PhysicalDevice, graphics_queue: vk::Queue, present_queue: vk::Queue, + // Swapchain swapchain_format: vk::Format, swapchain_extent: vk::Extent2D, swapchain: vk::SwapchainKHR, @@ -90,17 +95,19 @@ pub struct AppData { swapchain_image_views: Vec, } +impl AppData {} + #[derive(Debug, Error)] #[error("Missing {0}.")] pub struct SuitabilityError(pub &'static str); #[derive(Copy, Clone, Debug)] -pub(crate) struct QueueFamilyIndicies { +pub(crate) struct QueueFamilyIndices { graphics: u32, present: u32, } -impl QueueFamilyIndicies { +impl QueueFamilyIndices { unsafe fn get( instance: &Instance, data: &AppData, diff --git a/src/app/functions.rs b/src/app/functions.rs index 697b273..370af08 100644 --- a/src/app/functions.rs +++ b/src/app/functions.rs @@ -133,12 +133,12 @@ pub(super) unsafe fn pick_physical_device(instance: &Instance, data: &mut AppDat Err(anyhow!("Failed to find suitable physical device.")) } -pub(super) unsafe fn check_physical_device( +unsafe fn check_physical_device( instance: &Instance, data: &AppData, physical_device: vk::PhysicalDevice, ) -> Result<()> { - QueueFamilyIndicies::get(instance, data, physical_device)?; + QueueFamilyIndices::get(instance, data, physical_device)?; check_physical_device_extensions(instance, physical_device)?; let support = SwapchainSupport::get(instance, data, physical_device)?; @@ -149,7 +149,7 @@ pub(super) unsafe fn check_physical_device( Ok(()) } -pub(super) unsafe fn check_physical_device_extensions( +unsafe fn check_physical_device_extensions( instance: &Instance, physical_device: vk::PhysicalDevice, ) -> Result<()> { @@ -173,7 +173,7 @@ pub(super) unsafe fn create_logical_device( data: &mut AppData, ) -> Result { // Queue Create Infos - let indices = QueueFamilyIndicies::get(instance, data, data.physical_device)?; + let indices = QueueFamilyIndices::get(instance, data, data.physical_device)?; let mut unique_indices = HashSet::new(); unique_indices.insert(indices.graphics); @@ -230,9 +230,7 @@ pub(super) unsafe fn create_logical_device( Ok(device) } -pub(super) fn get_swapchain_surface_format( - formats: &[vk::SurfaceFormatKHR], -) -> vk::SurfaceFormatKHR { +fn get_swapchain_surface_format(formats: &[vk::SurfaceFormatKHR]) -> vk::SurfaceFormatKHR { formats .iter() .cloned() @@ -243,9 +241,7 @@ pub(super) fn get_swapchain_surface_format( .unwrap_or_else(|| formats[0]) } -pub(super) fn get_swapchain_present_mode( - present_modes: &[vk::PresentModeKHR], -) -> vk::PresentModeKHR { +fn get_swapchain_present_mode(present_modes: &[vk::PresentModeKHR]) -> vk::PresentModeKHR { present_modes .iter() .cloned() @@ -253,10 +249,7 @@ pub(super) fn get_swapchain_present_mode( .unwrap_or(vk::PresentModeKHR::FIFO) } -pub(super) fn get_swapchain_extent( - window: &Window, - capabilities: vk::SurfaceCapabilitiesKHR, -) -> vk::Extent2D { +fn get_swapchain_extent(window: &Window, capabilities: vk::SurfaceCapabilitiesKHR) -> vk::Extent2D { if capabilities.current_extent.width != u32::MAX { capabilities.current_extent } else { @@ -283,7 +276,7 @@ pub(super) unsafe fn create_swapchain( device: &Device, data: &mut AppData, ) -> Result<()> { - let indices = QueueFamilyIndicies::get(instance, data, data.physical_device)?; + let indices = QueueFamilyIndices::get(instance, data, data.physical_device)?; let support = SwapchainSupport::get(instance, data, data.physical_device)?; let surface_format = get_swapchain_surface_format(&support.formats); @@ -364,3 +357,40 @@ pub(super) unsafe fn create_swapchain_image_views( Ok(()) } + +pub(super) unsafe fn create_pipeline(device: &Device, data: &mut AppData) -> Result<()> { + let vert = include_bytes!("../../shaders/vert.spv"); + let frag = include_bytes!("../../shaders/frag.spv"); + + let vert_shader_module = create_shader_module(device, &vert[..])?; + let frag_shader_module = create_shader_module(device, &frag[..])?; + + let vert_stage = vk::PipelineShaderStageCreateInfo::builder() + .stage(vk::ShaderStageFlags::VERTEX) + .module(vert_shader_module) + .name(b"main\0"); + + let frag_stage = vk::PipelineShaderStageCreateInfo::builder() + .stage(vk::ShaderStageFlags::FRAGMENT) + .module(frag_shader_module) + .name(b"main\0"); + + device.destroy_shader_module(vert_shader_module, None); + device.destroy_shader_module(frag_shader_module, None); + + Ok(()) +} + +unsafe fn create_shader_module(device: &Device, bytecode: &[u8]) -> Result { + let bytecode = Vec::::from(bytecode); + let (prefix, code, suffix) = bytecode.align_to::(); + if !prefix.is_empty() || !suffix.is_empty() { + return Err(anyhow!("Shader bytecode is not properly aligned.")); + } + + let info = vk::ShaderModuleCreateInfo::builder() + .code_size(bytecode.len()) + .code(code); + + Ok(device.create_shader_module(&info, None)?) +}