From e84e77d728af6b2147480e83a43f2dcdfce256d8 Mon Sep 17 00:00:00 2001 From: Kyle Mayes Date: Fri, 15 Sep 2023 19:35:39 -0400 Subject: [PATCH] Update shader creation in tutorial (#182) --- tutorial/book/preprocessor/main.rs | 1 + tutorial/book/src/pipeline/shader_modules.md | 20 ++++++++++--------- tutorial/src/09_shader_modules.rs | 11 ++++------ tutorial/src/10_fixed_functions.rs | 11 ++++------ tutorial/src/11_render_passes.rs | 11 ++++------ tutorial/src/12_graphics_pipeline_complete.rs | 11 ++++------ tutorial/src/13_framebuffers.rs | 11 ++++------ tutorial/src/14_command_buffers.rs | 11 ++++------ tutorial/src/15_hello_triangle.rs | 11 ++++------ tutorial/src/16_swapchain_recreation.rs | 11 ++++------ tutorial/src/17_vertex_input.rs | 11 ++++------ tutorial/src/18_vertex_buffer.rs | 11 ++++------ tutorial/src/19_staging_buffer.rs | 11 ++++------ tutorial/src/20_index_buffer.rs | 11 ++++------ tutorial/src/21_descriptor_layout.rs | 11 ++++------ tutorial/src/22_descriptor_sets.rs | 11 ++++------ tutorial/src/23_texture_image.rs | 11 ++++------ tutorial/src/24_sampler.rs | 11 ++++------ tutorial/src/25_texture_mapping.rs | 11 ++++------ tutorial/src/26_depth_buffering.rs | 11 ++++------ tutorial/src/27_model_loading.rs | 11 ++++------ tutorial/src/28_mipmapping.rs | 11 ++++------ tutorial/src/29_multisampling.rs | 11 ++++------ tutorial/src/30_push_constants.rs | 11 ++++------ tutorial/src/31_recycling_command_buffers.rs | 11 ++++------ tutorial/src/32_secondary_command_buffers.rs | 11 ++++------ 26 files changed, 108 insertions(+), 177 deletions(-) diff --git a/tutorial/book/preprocessor/main.rs b/tutorial/book/preprocessor/main.rs index 0c86b174..ab2d66f2 100644 --- a/tutorial/book/preprocessor/main.rs +++ b/tutorial/book/preprocessor/main.rs @@ -75,6 +75,7 @@ fn load_index() -> HashMap<&'static str, &'static str> { info!("Loaded index has {} entries.", index.len()); // Add entries for non-generated items. + index.insert("Bytecode", "https://docs.rs/vulkanalia/%VERSION%/vulkanalia/bytecode/struct.Bytecode.html"); index.insert("Device", "https://docs.rs/vulkanalia/%VERSION%/vulkanalia/struct.Device.html"); index.insert("Entry", "https://docs.rs/vulkanalia/%VERSION%/vulkanalia/struct.Entry.html"); index.insert("Instance", "https://docs.rs/vulkanalia/%VERSION%/vulkanalia/struct.Instance.html"); diff --git a/tutorial/book/src/pipeline/shader_modules.md b/tutorial/book/src/pipeline/shader_modules.md index 67e6a7bc..c3515cfb 100644 --- a/tutorial/book/src/pipeline/shader_modules.md +++ b/tutorial/book/src/pipeline/shader_modules.md @@ -204,24 +204,26 @@ unsafe fn create_shader_module( The function will take a slice containing the bytecode as parameter and create a `vk::ShaderModule` from it using our logical device. -Creating a shader module is simple, we only need to specify the length of our bytecode slice and the bytecode slice itself. This information is specified in a `vk::ShaderModuleCreateInfo` structure. The one catch is that the size of the bytecode is specified in bytes, but the bytecode slice expected by this struct is a `&[u32]` instead of a `&[u8]`. Therefore we will first need to convert our `&[u8]` into an `&[u32]`. We will accomplish this with [`slice::align_to`](https://doc.rust-lang.org/stable/std/primitive.slice.html#method.align_to) which can be used to convert a slice into a slice containing a type with a different size and/or alignment requirements. However, the `&[u8]` returned by `include_bytes!` may not meet our alignment requirements, so we'll first copy it into a `Vec`. +Creating a shader module is simple, we only need to specify the length of our bytecode slice and the bytecode slice itself. This information is specified in a `vk::ShaderModuleCreateInfo` structure. The one catch is that the size of the bytecode is specified in bytes, but the bytecode slice expected by this struct is a `&[u32]` instead of a `&[u8]`. Therefore we will first need to convert our `&[u8]` into an `&[u32]`. + +`vulkanalia` has a helper struct called `Bytecode` that we will use to copy the shader bytecode into a new buffer that is guaranteed to have the correct alignment for an array of `u32`s. Add an import for this helper struct: ```rust,noplaypen -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.")); -} +use vulkanalia::bytecode::Bytecode; ``` -The middle slice returned by this method (`code`) is a `&[u32]` and is guaranteed to be correctly aligned. Any `u8`s in our `bytecode` slice that fell outside this alignment guarantee will appear in the first or third slices returned (`prefix` and `suffix`). We'll require that both of these slices are empty to ensure that our entire `bytecode` slice has been converted to a `&[u32]` though you shouldn't have to worry about this failure case in practice. +Getting back to our `create_shader_module` function, `Bytecode::new` will return an error if the supplied byte slice has a length that is not a multiple of 4 or if the allocation of the aligned buffer fails. As long as you are providing valid shader bytecode this should never be a problem, so we'll just `unwrap` the result. + +```rust,noplaypen +let bytecode = Bytecode::new(bytecode).unwrap(); +``` We can then construct a `vk::ShaderModuleCreateInfo` and use it to call `create_shader_module` to create the shader module: ```rust,noplaypen let info = vk::ShaderModuleCreateInfo::builder() - .code_size(bytecode.len()) - .code(code); + .code_size(bytecode.code_size()) + .code(bytecode.code()); Ok(device.create_shader_module(&info, None)?) ``` diff --git a/tutorial/src/09_shader_modules.rs b/tutorial/src/09_shader_modules.rs index 00ad5c4a..8af48c3d 100644 --- a/tutorial/src/09_shader_modules.rs +++ b/tutorial/src/09_shader_modules.rs @@ -15,6 +15,7 @@ use std::os::raw::c_void; use anyhow::{anyhow, Result}; use log::*; use thiserror::Error; +use vulkanalia::bytecode::Bytecode; use vulkanalia::loader::{LibloadingLoader, LIBRARY}; use vulkanalia::prelude::v1_0::*; use vulkanalia::window as vk_window; @@ -517,15 +518,11 @@ unsafe fn create_pipeline(device: &Device, data: &mut AppData) -> Result<()> { } 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 bytecode = Bytecode::new(bytecode).unwrap(); let info = vk::ShaderModuleCreateInfo::builder() - .code_size(bytecode.len()) - .code(code); + .code_size(bytecode.code_size()) + .code(bytecode.code()); Ok(device.create_shader_module(&info, None)?) } diff --git a/tutorial/src/10_fixed_functions.rs b/tutorial/src/10_fixed_functions.rs index c212c0d5..2bcd6e5b 100644 --- a/tutorial/src/10_fixed_functions.rs +++ b/tutorial/src/10_fixed_functions.rs @@ -15,6 +15,7 @@ use std::os::raw::c_void; use anyhow::{anyhow, Result}; use log::*; use thiserror::Error; +use vulkanalia::bytecode::Bytecode; use vulkanalia::loader::{LibloadingLoader, LIBRARY}; use vulkanalia::prelude::v1_0::*; use vulkanalia::window as vk_window; @@ -586,15 +587,11 @@ unsafe fn create_pipeline(device: &Device, data: &mut AppData) -> Result<()> { } 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 bytecode = Bytecode::new(bytecode).unwrap(); let info = vk::ShaderModuleCreateInfo::builder() - .code_size(bytecode.len()) - .code(code); + .code_size(bytecode.code_size()) + .code(bytecode.code()); Ok(device.create_shader_module(&info, None)?) } diff --git a/tutorial/src/11_render_passes.rs b/tutorial/src/11_render_passes.rs index c0486c40..90f507e8 100644 --- a/tutorial/src/11_render_passes.rs +++ b/tutorial/src/11_render_passes.rs @@ -15,6 +15,7 @@ use std::os::raw::c_void; use anyhow::{anyhow, Result}; use log::*; use thiserror::Error; +use vulkanalia::bytecode::Bytecode; use vulkanalia::loader::{LibloadingLoader, LIBRARY}; use vulkanalia::prelude::v1_0::*; use vulkanalia::window as vk_window; @@ -626,15 +627,11 @@ unsafe fn create_pipeline(device: &Device, data: &mut AppData) -> Result<()> { } 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 bytecode = Bytecode::new(bytecode).unwrap(); let info = vk::ShaderModuleCreateInfo::builder() - .code_size(bytecode.len()) - .code(code); + .code_size(bytecode.code_size()) + .code(bytecode.code()); Ok(device.create_shader_module(&info, None)?) } diff --git a/tutorial/src/12_graphics_pipeline_complete.rs b/tutorial/src/12_graphics_pipeline_complete.rs index 46fa6431..28722e47 100644 --- a/tutorial/src/12_graphics_pipeline_complete.rs +++ b/tutorial/src/12_graphics_pipeline_complete.rs @@ -15,6 +15,7 @@ use std::os::raw::c_void; use anyhow::{anyhow, Result}; use log::*; use thiserror::Error; +use vulkanalia::bytecode::Bytecode; use vulkanalia::loader::{LibloadingLoader, LIBRARY}; use vulkanalia::prelude::v1_0::*; use vulkanalia::window as vk_window; @@ -647,15 +648,11 @@ unsafe fn create_pipeline(device: &Device, data: &mut AppData) -> Result<()> { } 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 bytecode = Bytecode::new(bytecode).unwrap(); let info = vk::ShaderModuleCreateInfo::builder() - .code_size(bytecode.len()) - .code(code); + .code_size(bytecode.code_size()) + .code(bytecode.code()); Ok(device.create_shader_module(&info, None)?) } diff --git a/tutorial/src/13_framebuffers.rs b/tutorial/src/13_framebuffers.rs index 6404186f..8e9aa81a 100644 --- a/tutorial/src/13_framebuffers.rs +++ b/tutorial/src/13_framebuffers.rs @@ -15,6 +15,7 @@ use std::os::raw::c_void; use anyhow::{anyhow, Result}; use log::*; use thiserror::Error; +use vulkanalia::bytecode::Bytecode; use vulkanalia::loader::{LibloadingLoader, LIBRARY}; use vulkanalia::prelude::v1_0::*; use vulkanalia::window as vk_window; @@ -651,15 +652,11 @@ unsafe fn create_pipeline(device: &Device, data: &mut AppData) -> Result<()> { } 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 bytecode = Bytecode::new(bytecode).unwrap(); let info = vk::ShaderModuleCreateInfo::builder() - .code_size(bytecode.len()) - .code(code); + .code_size(bytecode.code_size()) + .code(bytecode.code()); Ok(device.create_shader_module(&info, None)?) } diff --git a/tutorial/src/14_command_buffers.rs b/tutorial/src/14_command_buffers.rs index 093f8198..513d667a 100644 --- a/tutorial/src/14_command_buffers.rs +++ b/tutorial/src/14_command_buffers.rs @@ -15,6 +15,7 @@ use std::os::raw::c_void; use anyhow::{anyhow, Result}; use log::*; use thiserror::Error; +use vulkanalia::bytecode::Bytecode; use vulkanalia::loader::{LibloadingLoader, LIBRARY}; use vulkanalia::prelude::v1_0::*; use vulkanalia::window as vk_window; @@ -658,15 +659,11 @@ unsafe fn create_pipeline(device: &Device, data: &mut AppData) -> Result<()> { } 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 bytecode = Bytecode::new(bytecode).unwrap(); let info = vk::ShaderModuleCreateInfo::builder() - .code_size(bytecode.len()) - .code(code); + .code_size(bytecode.code_size()) + .code(bytecode.code()); Ok(device.create_shader_module(&info, None)?) } diff --git a/tutorial/src/15_hello_triangle.rs b/tutorial/src/15_hello_triangle.rs index d8f36290..b385cf81 100644 --- a/tutorial/src/15_hello_triangle.rs +++ b/tutorial/src/15_hello_triangle.rs @@ -15,6 +15,7 @@ use std::os::raw::c_void; use anyhow::{anyhow, Result}; use log::*; use thiserror::Error; +use vulkanalia::bytecode::Bytecode; use vulkanalia::loader::{LibloadingLoader, LIBRARY}; use vulkanalia::prelude::v1_0::*; use vulkanalia::window as vk_window; @@ -735,15 +736,11 @@ unsafe fn create_pipeline(device: &Device, data: &mut AppData) -> Result<()> { } 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 bytecode = Bytecode::new(bytecode).unwrap(); let info = vk::ShaderModuleCreateInfo::builder() - .code_size(bytecode.len()) - .code(code); + .code_size(bytecode.code_size()) + .code(bytecode.code()); Ok(device.create_shader_module(&info, None)?) } diff --git a/tutorial/src/16_swapchain_recreation.rs b/tutorial/src/16_swapchain_recreation.rs index a5bba441..2280d6ad 100644 --- a/tutorial/src/16_swapchain_recreation.rs +++ b/tutorial/src/16_swapchain_recreation.rs @@ -15,6 +15,7 @@ use std::os::raw::c_void; use anyhow::{anyhow, Result}; use log::*; use thiserror::Error; +use vulkanalia::bytecode::Bytecode; use vulkanalia::loader::{LibloadingLoader, LIBRARY}; use vulkanalia::prelude::v1_0::*; use vulkanalia::window as vk_window; @@ -780,15 +781,11 @@ unsafe fn create_pipeline(device: &Device, data: &mut AppData) -> Result<()> { } 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 bytecode = Bytecode::new(bytecode).unwrap(); let info = vk::ShaderModuleCreateInfo::builder() - .code_size(bytecode.len()) - .code(code); + .code_size(bytecode.code_size()) + .code(bytecode.code()); Ok(device.create_shader_module(&info, None)?) } diff --git a/tutorial/src/17_vertex_input.rs b/tutorial/src/17_vertex_input.rs index 7c2768f7..30476003 100644 --- a/tutorial/src/17_vertex_input.rs +++ b/tutorial/src/17_vertex_input.rs @@ -17,6 +17,7 @@ use anyhow::{anyhow, Result}; use cgmath::{vec2, vec3}; use log::*; use thiserror::Error; +use vulkanalia::bytecode::Bytecode; use vulkanalia::loader::{LibloadingLoader, LIBRARY}; use vulkanalia::prelude::v1_0::*; use vulkanalia::window as vk_window; @@ -796,15 +797,11 @@ unsafe fn create_pipeline(device: &Device, data: &mut AppData) -> Result<()> { } 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 bytecode = Bytecode::new(bytecode).unwrap(); let info = vk::ShaderModuleCreateInfo::builder() - .code_size(bytecode.len()) - .code(code); + .code_size(bytecode.code_size()) + .code(bytecode.code()); Ok(device.create_shader_module(&info, None)?) } diff --git a/tutorial/src/18_vertex_buffer.rs b/tutorial/src/18_vertex_buffer.rs index e1687154..5e769af9 100644 --- a/tutorial/src/18_vertex_buffer.rs +++ b/tutorial/src/18_vertex_buffer.rs @@ -18,6 +18,7 @@ use anyhow::{anyhow, Result}; use cgmath::{vec2, vec3}; use log::*; use thiserror::Error; +use vulkanalia::bytecode::Bytecode; use vulkanalia::loader::{LibloadingLoader, LIBRARY}; use vulkanalia::prelude::v1_0::*; use vulkanalia::window as vk_window; @@ -803,15 +804,11 @@ unsafe fn create_pipeline(device: &Device, data: &mut AppData) -> Result<()> { } 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 bytecode = Bytecode::new(bytecode).unwrap(); let info = vk::ShaderModuleCreateInfo::builder() - .code_size(bytecode.len()) - .code(code); + .code_size(bytecode.code_size()) + .code(bytecode.code()); Ok(device.create_shader_module(&info, None)?) } diff --git a/tutorial/src/19_staging_buffer.rs b/tutorial/src/19_staging_buffer.rs index cdd11402..e4cc814e 100644 --- a/tutorial/src/19_staging_buffer.rs +++ b/tutorial/src/19_staging_buffer.rs @@ -18,6 +18,7 @@ use anyhow::{anyhow, Result}; use cgmath::{vec2, vec3}; use log::*; use thiserror::Error; +use vulkanalia::bytecode::Bytecode; use vulkanalia::loader::{LibloadingLoader, LIBRARY}; use vulkanalia::prelude::v1_0::*; use vulkanalia::window as vk_window; @@ -803,15 +804,11 @@ unsafe fn create_pipeline(device: &Device, data: &mut AppData) -> Result<()> { } 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 bytecode = Bytecode::new(bytecode).unwrap(); let info = vk::ShaderModuleCreateInfo::builder() - .code_size(bytecode.len()) - .code(code); + .code_size(bytecode.code_size()) + .code(bytecode.code()); Ok(device.create_shader_module(&info, None)?) } diff --git a/tutorial/src/20_index_buffer.rs b/tutorial/src/20_index_buffer.rs index 2be2e838..8ea4096f 100644 --- a/tutorial/src/20_index_buffer.rs +++ b/tutorial/src/20_index_buffer.rs @@ -18,6 +18,7 @@ use anyhow::{anyhow, Result}; use cgmath::{vec2, vec3}; use log::*; use thiserror::Error; +use vulkanalia::bytecode::Bytecode; use vulkanalia::loader::{LibloadingLoader, LIBRARY}; use vulkanalia::prelude::v1_0::*; use vulkanalia::window as vk_window; @@ -811,15 +812,11 @@ unsafe fn create_pipeline(device: &Device, data: &mut AppData) -> Result<()> { } 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 bytecode = Bytecode::new(bytecode).unwrap(); let info = vk::ShaderModuleCreateInfo::builder() - .code_size(bytecode.len()) - .code(code); + .code_size(bytecode.code_size()) + .code(bytecode.code()); Ok(device.create_shader_module(&info, None)?) } diff --git a/tutorial/src/21_descriptor_layout.rs b/tutorial/src/21_descriptor_layout.rs index b98f75c5..885f8755 100644 --- a/tutorial/src/21_descriptor_layout.rs +++ b/tutorial/src/21_descriptor_layout.rs @@ -19,6 +19,7 @@ use anyhow::{anyhow, Result}; use cgmath::{point3, vec2, vec3, Deg}; use log::*; use thiserror::Error; +use vulkanalia::bytecode::Bytecode; use vulkanalia::loader::{LibloadingLoader, LIBRARY}; use vulkanalia::prelude::v1_0::*; use vulkanalia::window as vk_window; @@ -883,15 +884,11 @@ unsafe fn create_pipeline(device: &Device, data: &mut AppData) -> Result<()> { } 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 bytecode = Bytecode::new(bytecode).unwrap(); let info = vk::ShaderModuleCreateInfo::builder() - .code_size(bytecode.len()) - .code(code); + .code_size(bytecode.code_size()) + .code(bytecode.code()); Ok(device.create_shader_module(&info, None)?) } diff --git a/tutorial/src/22_descriptor_sets.rs b/tutorial/src/22_descriptor_sets.rs index e766c857..781f6f19 100644 --- a/tutorial/src/22_descriptor_sets.rs +++ b/tutorial/src/22_descriptor_sets.rs @@ -19,6 +19,7 @@ use anyhow::{anyhow, Result}; use cgmath::{point3, vec2, vec3, Deg}; use log::*; use thiserror::Error; +use vulkanalia::bytecode::Bytecode; use vulkanalia::loader::{LibloadingLoader, LIBRARY}; use vulkanalia::prelude::v1_0::*; use vulkanalia::window as vk_window; @@ -891,15 +892,11 @@ unsafe fn create_pipeline(device: &Device, data: &mut AppData) -> Result<()> { } 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 bytecode = Bytecode::new(bytecode).unwrap(); let info = vk::ShaderModuleCreateInfo::builder() - .code_size(bytecode.len()) - .code(code); + .code_size(bytecode.code_size()) + .code(bytecode.code()); Ok(device.create_shader_module(&info, None)?) } diff --git a/tutorial/src/23_texture_image.rs b/tutorial/src/23_texture_image.rs index 9c276176..177822b2 100644 --- a/tutorial/src/23_texture_image.rs +++ b/tutorial/src/23_texture_image.rs @@ -20,6 +20,7 @@ use anyhow::{anyhow, Result}; use cgmath::{point3, vec2, vec3, Deg}; use log::*; use thiserror::Error; +use vulkanalia::bytecode::Bytecode; use vulkanalia::loader::{LibloadingLoader, LIBRARY}; use vulkanalia::prelude::v1_0::*; use vulkanalia::window as vk_window; @@ -898,15 +899,11 @@ unsafe fn create_pipeline(device: &Device, data: &mut AppData) -> Result<()> { } 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 bytecode = Bytecode::new(bytecode).unwrap(); let info = vk::ShaderModuleCreateInfo::builder() - .code_size(bytecode.len()) - .code(code); + .code_size(bytecode.code_size()) + .code(bytecode.code()); Ok(device.create_shader_module(&info, None)?) } diff --git a/tutorial/src/24_sampler.rs b/tutorial/src/24_sampler.rs index 433330bf..8e3f80ec 100644 --- a/tutorial/src/24_sampler.rs +++ b/tutorial/src/24_sampler.rs @@ -20,6 +20,7 @@ use anyhow::{anyhow, Result}; use cgmath::{point3, vec2, vec3, Deg}; use log::*; use thiserror::Error; +use vulkanalia::bytecode::Bytecode; use vulkanalia::loader::{LibloadingLoader, LIBRARY}; use vulkanalia::prelude::v1_0::*; use vulkanalia::window as vk_window; @@ -887,15 +888,11 @@ unsafe fn create_pipeline(device: &Device, data: &mut AppData) -> Result<()> { } 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 bytecode = Bytecode::new(bytecode).unwrap(); let info = vk::ShaderModuleCreateInfo::builder() - .code_size(bytecode.len()) - .code(code); + .code_size(bytecode.code_size()) + .code(bytecode.code()); Ok(device.create_shader_module(&info, None)?) } diff --git a/tutorial/src/25_texture_mapping.rs b/tutorial/src/25_texture_mapping.rs index 5013b3c8..7eb779a2 100644 --- a/tutorial/src/25_texture_mapping.rs +++ b/tutorial/src/25_texture_mapping.rs @@ -20,6 +20,7 @@ use anyhow::{anyhow, Result}; use cgmath::{point3, vec2, vec3, Deg}; use log::*; use thiserror::Error; +use vulkanalia::bytecode::Bytecode; use vulkanalia::loader::{LibloadingLoader, LIBRARY}; use vulkanalia::prelude::v1_0::*; use vulkanalia::window as vk_window; @@ -893,15 +894,11 @@ unsafe fn create_pipeline(device: &Device, data: &mut AppData) -> Result<()> { } 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 bytecode = Bytecode::new(bytecode).unwrap(); let info = vk::ShaderModuleCreateInfo::builder() - .code_size(bytecode.len()) - .code(code); + .code_size(bytecode.code_size()) + .code(bytecode.code()); Ok(device.create_shader_module(&info, None)?) } diff --git a/tutorial/src/26_depth_buffering.rs b/tutorial/src/26_depth_buffering.rs index 6e46407d..e7f8dfa5 100644 --- a/tutorial/src/26_depth_buffering.rs +++ b/tutorial/src/26_depth_buffering.rs @@ -20,6 +20,7 @@ use anyhow::{anyhow, Result}; use cgmath::{point3, vec2, vec3, Deg}; use log::*; use thiserror::Error; +use vulkanalia::bytecode::Bytecode; use vulkanalia::loader::{LibloadingLoader, LIBRARY}; use vulkanalia::prelude::v1_0::*; use vulkanalia::window as vk_window; @@ -944,15 +945,11 @@ unsafe fn create_pipeline(device: &Device, data: &mut AppData) -> Result<()> { } 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 bytecode = Bytecode::new(bytecode).unwrap(); let info = vk::ShaderModuleCreateInfo::builder() - .code_size(bytecode.len()) - .code(code); + .code_size(bytecode.code_size()) + .code(bytecode.code()); Ok(device.create_shader_module(&info, None)?) } diff --git a/tutorial/src/27_model_loading.rs b/tutorial/src/27_model_loading.rs index 1996672b..f3a4f350 100644 --- a/tutorial/src/27_model_loading.rs +++ b/tutorial/src/27_model_loading.rs @@ -22,6 +22,7 @@ use anyhow::{anyhow, Result}; use cgmath::{point3, vec2, vec3, Deg}; use log::*; use thiserror::Error; +use vulkanalia::bytecode::Bytecode; use vulkanalia::loader::{LibloadingLoader, LIBRARY}; use vulkanalia::prelude::v1_0::*; use vulkanalia::window as vk_window; @@ -930,15 +931,11 @@ unsafe fn create_pipeline(device: &Device, data: &mut AppData) -> Result<()> { } 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 bytecode = Bytecode::new(bytecode).unwrap(); let info = vk::ShaderModuleCreateInfo::builder() - .code_size(bytecode.len()) - .code(code); + .code_size(bytecode.code_size()) + .code(bytecode.code()); Ok(device.create_shader_module(&info, None)?) } diff --git a/tutorial/src/28_mipmapping.rs b/tutorial/src/28_mipmapping.rs index 9985abb4..2fc48cd7 100644 --- a/tutorial/src/28_mipmapping.rs +++ b/tutorial/src/28_mipmapping.rs @@ -22,6 +22,7 @@ use anyhow::{anyhow, Result}; use cgmath::{point3, vec2, vec3, Deg}; use log::*; use thiserror::Error; +use vulkanalia::bytecode::Bytecode; use vulkanalia::loader::{LibloadingLoader, LIBRARY}; use vulkanalia::prelude::v1_0::*; use vulkanalia::window as vk_window; @@ -931,15 +932,11 @@ unsafe fn create_pipeline(device: &Device, data: &mut AppData) -> Result<()> { } 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 bytecode = Bytecode::new(bytecode).unwrap(); let info = vk::ShaderModuleCreateInfo::builder() - .code_size(bytecode.len()) - .code(code); + .code_size(bytecode.code_size()) + .code(bytecode.code()); Ok(device.create_shader_module(&info, None)?) } diff --git a/tutorial/src/29_multisampling.rs b/tutorial/src/29_multisampling.rs index 8f2ff2e5..0ed85146 100644 --- a/tutorial/src/29_multisampling.rs +++ b/tutorial/src/29_multisampling.rs @@ -22,6 +22,7 @@ use anyhow::{anyhow, Result}; use cgmath::{point3, vec2, vec3, Deg}; use log::*; use thiserror::Error; +use vulkanalia::bytecode::Bytecode; use vulkanalia::loader::{LibloadingLoader, LIBRARY}; use vulkanalia::prelude::v1_0::*; use vulkanalia::window as vk_window; @@ -978,15 +979,11 @@ unsafe fn create_pipeline(device: &Device, data: &mut AppData) -> Result<()> { } 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 bytecode = Bytecode::new(bytecode).unwrap(); let info = vk::ShaderModuleCreateInfo::builder() - .code_size(bytecode.len()) - .code(code); + .code_size(bytecode.code_size()) + .code(bytecode.code()); Ok(device.create_shader_module(&info, None)?) } diff --git a/tutorial/src/30_push_constants.rs b/tutorial/src/30_push_constants.rs index e1641eab..e9ba1bad 100644 --- a/tutorial/src/30_push_constants.rs +++ b/tutorial/src/30_push_constants.rs @@ -22,6 +22,7 @@ use anyhow::{anyhow, Result}; use cgmath::{point3, vec2, vec3, Deg}; use log::*; use thiserror::Error; +use vulkanalia::bytecode::Bytecode; use vulkanalia::loader::{LibloadingLoader, LIBRARY}; use vulkanalia::prelude::v1_0::*; use vulkanalia::window as vk_window; @@ -995,15 +996,11 @@ unsafe fn create_pipeline(device: &Device, data: &mut AppData) -> Result<()> { } 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 bytecode = Bytecode::new(bytecode).unwrap(); let info = vk::ShaderModuleCreateInfo::builder() - .code_size(bytecode.len()) - .code(code); + .code_size(bytecode.code_size()) + .code(bytecode.code()); Ok(device.create_shader_module(&info, None)?) } diff --git a/tutorial/src/31_recycling_command_buffers.rs b/tutorial/src/31_recycling_command_buffers.rs index 6d8f4728..ba081904 100644 --- a/tutorial/src/31_recycling_command_buffers.rs +++ b/tutorial/src/31_recycling_command_buffers.rs @@ -22,6 +22,7 @@ use anyhow::{anyhow, Result}; use cgmath::{point3, vec2, vec3, Deg}; use log::*; use thiserror::Error; +use vulkanalia::bytecode::Bytecode; use vulkanalia::loader::{LibloadingLoader, LIBRARY}; use vulkanalia::prelude::v1_0::*; use vulkanalia::window as vk_window; @@ -1082,15 +1083,11 @@ unsafe fn create_pipeline(device: &Device, data: &mut AppData) -> Result<()> { } 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 bytecode = Bytecode::new(bytecode).unwrap(); let info = vk::ShaderModuleCreateInfo::builder() - .code_size(bytecode.len()) - .code(code); + .code_size(bytecode.code_size()) + .code(bytecode.code()); Ok(device.create_shader_module(&info, None)?) } diff --git a/tutorial/src/32_secondary_command_buffers.rs b/tutorial/src/32_secondary_command_buffers.rs index 845293cc..bedce34b 100644 --- a/tutorial/src/32_secondary_command_buffers.rs +++ b/tutorial/src/32_secondary_command_buffers.rs @@ -22,6 +22,7 @@ use anyhow::{anyhow, Result}; use cgmath::{point3, vec2, vec3, Deg}; use log::*; use thiserror::Error; +use vulkanalia::bytecode::Bytecode; use vulkanalia::loader::{LibloadingLoader, LIBRARY}; use vulkanalia::prelude::v1_0::*; use vulkanalia::window as vk_window; @@ -1148,15 +1149,11 @@ unsafe fn create_pipeline(device: &Device, data: &mut AppData) -> Result<()> { } 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 bytecode = Bytecode::new(bytecode).unwrap(); let info = vk::ShaderModuleCreateInfo::builder() - .code_size(bytecode.len()) - .code(code); + .code_size(bytecode.code_size()) + .code(bytecode.code()); Ok(device.create_shader_module(&info, None)?) }