From 0be5636497cd4e42076645afb323b91ed6b31ada Mon Sep 17 00:00:00 2001 From: ICEYSELF Date: Mon, 9 Sep 2024 20:26:37 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E5=BC=80=E5=A7=8B=E5=90=8C=E6=AD=A5?= =?UTF-8?q?=E4=B8=8A=E6=B8=B8=E5=B7=A5=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.toml | 8 ++++---- book/preprocessor/Cargo.toml | 2 +- book/preprocessor/main.rs | 2 +- book/src/development_environment.md | 30 ++++++++++++++++++----------- 4 files changed, 25 insertions(+), 17 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 6b314fc..1583cd3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ publish = false -edition = "2018" +edition = "2021" name = "tutorial" authors = ["Kyle Mayes "] @@ -20,11 +20,11 @@ anyhow = "1" log = "0.4" cgmath = "0.18" png = "0.17" -pretty_env_logger = "0.4" +pretty_env_logger = "0.5" thiserror = "1" tobj = { version = "3", features = ["log"] } -vulkanalia = { version = "=0.22.0", features = ["libloading", "provisional", "window"] } -winit = "0.28" +vulkanalia = { version = "=0.25.0", features = ["libloading", "provisional", "window"] } +winit = "0.29" [[bin]] diff --git a/book/preprocessor/Cargo.toml b/book/preprocessor/Cargo.toml index d13ec67..77847a2 100644 --- a/book/preprocessor/Cargo.toml +++ b/book/preprocessor/Cargo.toml @@ -22,7 +22,7 @@ anyhow = "1" clap = "2" log = "0.4" mdbook = { version = "=0.4.21", default-features = false } -pretty_env_logger = "0.4" +pretty_env_logger = "0.5" pulldown-cmark = "0.8" pulldown-cmark-to-cmark = "6" serde_json = "1" diff --git a/book/preprocessor/main.rs b/book/preprocessor/main.rs index ab2d66f..37a6622 100644 --- a/book/preprocessor/main.rs +++ b/book/preprocessor/main.rs @@ -16,7 +16,7 @@ use pulldown_cmark_to_cmark::cmark; /// The Vulkan API Registry index. const INDEX: &str = include_str!("../../../index.txt"); /// The version of `vulkanalia` used by the tutorial. -const VERSION: &str = "0.22.0"; +const VERSION: &str = "0.25.0"; /// The number of documentation link replacements made. static REPLACEMENTS: AtomicUsize = AtomicUsize::new(0); diff --git a/book/src/development_environment.md b/book/src/development_environment.md index 5b49c1d..7c425d0 100644 --- a/book/src/development_environment.md +++ b/book/src/development_environment.md @@ -2,9 +2,9 @@ > 原文链接: > -> Commit Hash: ceb4a3fc6d8ca565af4f8679c4889bcad7941338 +> Commit Hash: 7becee96b0029bf721f833039c00ea2a417714dd -在这个章节中,我们将会安装 Vulkan SDK 并搭建开发 Vulkan 应用所需的环境。此教程假设你已经有一个搭建好的 Rust(1.51+)开发环境。 +在这个章节中,我们将会安装 Vulkan SDK 并搭建开发 Vulkan 应用所需的环境。此教程假设你已经有一个搭建好的 Rust(1.70+)开发环境。 ## Cargo 项目 @@ -22,11 +22,11 @@ log = "0.4" cgmath = "0.18" nalgebra-glm = "0.18" png = "0.17" -pretty_env_logger = "0.4" +pretty_env_logger = "0.5" thiserror = "1" tobj = { version = "3", features = ["log"] } -vulkanalia = { version = "=0.22.0", features = ["libloading", "provisional", "window"] } -winit = "0.28" +vulkanalia = { version = "=0.25.0", features = ["libloading", "provisional", "window"] } +winit = "0.29" ``` * `anyhow` – 用于简单的错误处理 @@ -35,7 +35,7 @@ winit = "0.28" * `png` – 用于将 PNG 图片文件加载到纹理 * `pretty_env_logger` – 用于打印日志到控制台 * `thiserror` – 用于在自定义错误类型时减少样板代码 -* `tobj` – 用于加载 [Wavefront .obj 格式](https:/en.wikipedia.org/wiki/Wavefront_.obj_file) 的 3D 模型 +* `tobj` – 用于加载 [Wavefront .obj 格式](https://en.wikipedia.org/wiki/Wavefront_.obj_file) 的 3D 模型 * `vulkanalia` – 用于调用 Vulkan API * `winit` – 用于创建将进行渲染的窗口 @@ -75,16 +75,24 @@ SDK 能在 [LunarG 网站](https://vulkan.lunarg.com/)下载。创建账户不 如果你收到了一条错误信息,那你需要确保你的显卡驱动是最新的,包含 Vulkan 运行时,并且你的显卡支持 Vulkan。主流品牌的驱动下载链接详见[介绍章节](introduction.html)。 -### macOS +### MacOS -以下指引将假设你在使用 [Homebrew 包管理器](https://brew.sh/)。并且注意你需要 macOS 10.11 或更新的版本。你的设备也需要支持 [Metal API](https://en.wikipedia.org/wiki/Metal_(API)#Supported_GPUs)。 - -SDK 能在 [LunarG 网站](https://vulkan.lunarg.com/) 下载。创建账户不是必须的,但它会给你阅读一些或许对你有用的额外文档的权限。 +SDK 可在 [LunarG 网站](https://vulkan.lunarg.com/) 下载。创建账户不是必须的,但它会给你阅读一些或许对你有用的额外文档的权限。 ![](./images/vulkan_sdk_download_buttons.png) -macOS 版本的 SDK 在内部使用了 [MoltenVK](https://moltengl.com/)。Vulkan 在 macOS 上没有原生支持,所以 MoltenVK 会作为中间层把 Vulkan API 的调用翻译至苹果的 Metal 图形框架。这样你就可以享受到苹果的 Metal 框架在调试与性能上的优点。 +MacOS 版本的 SDK 在内部使用了 [MoltenVK](https://moltengl.com/)。Vulkan 在 MacOS 上没有原生支持,所以 MoltenVK 会作为中间层把 Vulkan API 的调用翻译至苹果的 Metal 图形框架。这样你就可以享受到苹果的 Metal 框架在调试与性能上的优点。 下载完成之后,将其解压到你自己选择的文件夹。在解压后的文件夹内,你可以在 `Applications` 文件夹中找到一些使用 SDK 运行的示例应用的可执行文件。运行 `vkcube` 示例应用,你会看到这个画面: ![](./images/cube_demo_mac.png) + +#### 环境配置 + +当在 Vulkan SDK 目录以外的地方运行 Vulkan 应用程序时,你可能会需要运行由 Vulkan SDK 提供的 `setup-env.sh` 脚本,以避免找不到 Vulkan 库(例如 `libvulkan.dylib`)导致的问题。如果你把 Vulkan SDK 安装到了默认位置,脚本应该可以在如下目录中找到:`~/VulkanSDK/1.3.280.1/setup-env.sh`(用你实际安装的版本号替换 `1.3.280.1` 以匹配你的 Vulkan 安装)。 + +你也可以把这个脚本添加到你 Shell 的启动脚本中,这样它就能自动运行了。例如你可以在 `~/.zshrc` 文件中添加这样一行: + +``` +source ~/VulkanSDK/1.3.280.1/setup-env.sh +``` From ffa70d7ff422a2dc7e301cc11cd24cddaccaa9cd Mon Sep 17 00:00:00 2001 From: ICEYSELF Date: Mon, 9 Sep 2024 21:24:46 +0800 Subject: [PATCH 2/3] Update --- book/src/dynamic/push_constants.md | 4 +- book/src/dynamic/secondary_command_buffers.md | 17 ++-- book/src/faq.md | 4 +- book/src/introduction.md | 4 +- book/src/pipeline/fixed_functions.md | 30 +++--- book/src/pipeline/shader_modules.md | 11 ++- book/src/presentation/swapchain.md | 14 +-- book/src/presentation/window_surface.md | 3 +- book/src/setup/base_code.md | 34 ++++--- book/src/setup/validation_layers.md | 25 ++++- book/src/swapchain/recreation.md | 29 +++--- book/src/vertex/vertex_buffer_creation.md | 4 +- src/00_base_code.rs | 30 +++--- src/01_instance_creation.rs | 30 +++--- src/02_validation_layers.rs | 36 ++++--- src/03_physical_device_selection.rs | 36 ++++--- src/04_logical_device.rs | 36 ++++--- src/05_window_surface.rs | 36 ++++--- src/06_swapchain_creation.rs | 47 +++++---- src/07_image_views.rs | 47 +++++---- src/08_graphics_pipeline.rs | 47 +++++---- src/09_shader_modules.rs | 47 +++++---- src/10_fixed_functions.rs | 47 +++++---- src/11_render_passes.rs | 47 +++++---- src/12_graphics_pipeline_complete.rs | 47 +++++---- src/13_framebuffers.rs | 47 +++++---- src/14_command_buffers.rs | 47 +++++---- src/15_hello_triangle.rs | 55 ++++++----- src/16_swapchain_recreation.rs | 73 +++++++------- src/17_vertex_input.rs | 73 +++++++------- src/18_vertex_buffer.rs | 73 +++++++------- src/19_staging_buffer.rs | 73 +++++++------- src/20_index_buffer.rs | 73 +++++++------- src/21_descriptor_set_layout.rs | 73 +++++++------- src/22_descriptor_sets.rs | 73 +++++++------- src/23_texture_image.rs | 73 +++++++------- src/24_sampler.rs | 73 +++++++------- src/25_texture_mapping.rs | 73 +++++++------- src/26_depth_buffering.rs | 73 +++++++------- src/27_model_loading.rs | 73 +++++++------- src/28_mipmapping.rs | 73 +++++++------- src/29_multisampling.rs | 73 +++++++------- src/30_push_constants.rs | 77 ++++++++------- src/31_recycling_command_buffers.rs | 80 +++++++-------- src/32_secondary_command_buffers.rs | 99 ++++++++++--------- src/manage.py | 2 +- 46 files changed, 1176 insertions(+), 965 deletions(-) diff --git a/book/src/dynamic/push_constants.md b/book/src/dynamic/push_constants.md index 4d2054f..226a0d2 100644 --- a/book/src/dynamic/push_constants.md +++ b/book/src/dynamic/push_constants.md @@ -2,7 +2,7 @@ > 原文链接: > -> Commit Hash: ceb4a3fc6d8ca565af4f8679c4889bcad7941338 +> Commit Hash: 7becee96b0029bf721f833039c00ea2a417714dd > 前面没有这个声明的章节都是直接从 改编而来。

这一章和后面的章节都是原创,作者并不是 Vulkan 的专家。作者尽力保持了权威的语气,但是这些章节应该被视为一个 Vulkan 初学者的“尽力而为”。

如果你有问题、建议或者修正,请[提交 issue](https://github.com/KyleMayes/vulkanalia/issues)! @@ -124,7 +124,7 @@ let model = Mat4::from_axis_angle( Deg(0.0) ); -let model_bytes = &*std::slice::from_raw_parts( +let model_bytes = std::slice::from_raw_parts( &model as *const Mat4 as *const u8, size_of::() ); diff --git a/book/src/dynamic/secondary_command_buffers.md b/book/src/dynamic/secondary_command_buffers.md index 8dea468..3c3cc00 100644 --- a/book/src/dynamic/secondary_command_buffers.md +++ b/book/src/dynamic/secondary_command_buffers.md @@ -2,7 +2,7 @@ > 原文链接: > -> Commit Hash: ceb4a3fc6d8ca565af4f8679c4889bcad7941338 +> Commit Hash: 7becee96b0029bf721f833039c00ea2a417714dd > 前面没有这个声明的章节都是直接从 改编而来。

这一章和后面的章节都是原创,作者并不是 Vulkan 的专家。作者尽力保持了权威的语气,但是这些章节应该被视为一个 Vulkan 初学者的“尽力而为”。

如果你有问题、建议或者修正,请[提交 issue](https://github.com/KyleMayes/vulkanalia/issues)! @@ -296,12 +296,15 @@ use winit::event::{ElementState, VirtualKeyCode}; ```rust,noplaypen match event { // ... - Event::WindowEvent { event: WindowEvent::KeyboardInput { input, .. }, .. } => { - if input.state == ElementState::Pressed { - match input.virtual_keycode { - Some(VirtualKeyCode::Left) if app.models > 1 => app.models -= 1, - Some(VirtualKeyCode::Right) if app.models < 4 => app.models += 1, - _ => { } + Event::WindowEvent { event, .. } => match event { + // ... + WindowEvent::KeyboardInput { event, .. } => { + if event.state == ElementState::Pressed { + match event.physical_key { + PhysicalKey::Code(KeyCode::ArrowLeft) if app.models > 1 => app.models -= 1, + PhysicalKey::Code(KeyCode::ArrowRight) if app.models < 4 => app.models += 1, + _ => { } + } } } } diff --git a/book/src/faq.md b/book/src/faq.md index 1740398..430d96e 100644 --- a/book/src/faq.md +++ b/book/src/faq.md @@ -2,10 +2,12 @@ > 原文链接: > -> Commit Hash: ceb4a3fc6d8ca565af4f8679c4889bcad7941338 +> Commit Hash: 7becee96b0029bf721f833039c00ea2a417714dd 本页面列举了在开发 Vulkan 应用时可能遇到的常见问题及其解决方案。 +* **(MacOS)** 我安装了 Vulkan SDK,但我运行 Vulkan 应用程序的时候遇到了找不到 `libvulkan.dylib` 的错误 —— 参见[MacOS Vulkan SDK 安装说明中的`环境配置`章节](./development_environment.md#setup-environment)。 + * **我在核心校验层中遇到了访问冲突(access violation)错误** – 确保未运行 MSI Afterburner / RivaTuner Statistics Server,因为它们和 Vulkan 之间存在一些兼容性问题。 * **我看不到任何来自校验层的消息/校验层不可用** – 首先确保校验层有机会打印错误信息,请在程序退出后保持终端窗口打开。在 Visual Studio 中,你可以通过使用 Ctrl-F5 而不是 F5 来运行程序;在 Linux 中,你可以从终端窗口执行程序。如果仍然没有消息,并且你确信校验层已启用,那么你应该按照[此页面上的“Verify the Installation”说明](https://vulkan.lunarg.com/doc/view/1.2.135.0/windows/getting_started.html)来确保 Vulkan SDK 已正确安装。同时确保你的 SDK 版本至少为 1.1.106.0,以支持 `VK_LAYER_KHRONOS_validation` 校验层。 diff --git a/book/src/introduction.md b/book/src/introduction.md index b471e3e..6cbe301 100644 --- a/book/src/introduction.md +++ b/book/src/introduction.md @@ -2,7 +2,7 @@ > 原文链接: > -> Commit Hash: ceb4a3fc6d8ca565af4f8679c4889bcad7941338 +> Commit Hash: 7becee96b0029bf721f833039c00ea2a417714dd 本教程是用 Rust 对 的改写版本,应当归功于原教程的作者 ([Alexander Overvoorde](https://github.com/Overv)) 及[其他贡献者们](https://github.com/Overv/VulkanTutorial/graphs/contributors)。 @@ -20,7 +20,7 @@ * 一张支持 Vulkan 的显卡和驱动程序([NVIDIA](https://developer.nvidia.com/vulkan-driver),[AMD](http://www.amd.com/en-us/innovations/software-technologies/technologies-gaming/vulkan),[Intel](https://software.intel.com/en-us/blogs/2016/03/14/new-intel-vulkan-beta-1540204404-graphics-driver-for-windows-78110-1540)) * 使用 Rust 的经验 -* Rust 1.51 或更高版本 +* Rust 1.70 或更高版本 * 一些的 3D 计算机图形学知识 本教程不会要求你有 OpenGL 或者 Direct3D 的知识储备,但要求你必须理解 3D 计算机图形学的基础。例如,教程中不会解释透视投影背后的数学原理。[这本在线书籍](https://paroj.github.io/gltut/)是一个很好的计算机图形学入门资源。其他一些很好的计算机图形学资源包括: diff --git a/book/src/pipeline/fixed_functions.md b/book/src/pipeline/fixed_functions.md index ba97949..265b1b7 100644 --- a/book/src/pipeline/fixed_functions.md +++ b/book/src/pipeline/fixed_functions.md @@ -2,7 +2,7 @@ > 原文链接: > -> Commit Hash: ceb4a3fc6d8ca565af4f8679c4889bcad7941338 +> Commit Hash: 7becee96b0029bf721f833039c00ea2a417714dd **本章代码:**[main.rs](https://github.com/chuigda/Vulkan-Tutorial-Rust-CN/tree/master/src/10_fixed_functions.rs) @@ -15,18 +15,17 @@ * 绑定(Bindings) – 数据之间的间隔,以及数据是逐顶点(per-vertex)还是逐个实例(per-instance)的(参见[实例化](https://en.wikipedia.org/wiki/Geometry_instancing)) * 属性描述(Attribute descriptions) – 顶点着色器接收的属性的类型,从哪个绑定中加载数据,以及从哪个偏移量开始加载 -因为我们已经在顶点着色器中硬编码了数据,我们会将这个结构体的所有字段都保留默认值,表明我们不需要加载数据。我们会在顶点缓冲章节重新审视这个结构体。 +因为我们已经在顶点着色器中硬编码了数据,我们会将这个结构体的所有字段都保留默认值,表明我们不需要加载数据。我们会在顶点缓冲章节重新审视这个结构体。在 `create_pipeline` 函数中 `vk::PipelineShaderStageCreateInfo` 的后面添加以内容: ```rust,noplaypen -let vertex_input_state = vk::PipelineVertexInputStateCreateInfo::builder(); -``` +unsafe fn create_pipeline(device: &Device, data: &mut AppData) -> Result<()> { + // ... -在 `create_pipeline` 函数中,我们会在 `vk::PipelineShaderStageCreateInfo` 结构体之后添加这个结构体。 + let vertex_input_state = vk::PipelineVertexInputStateCreateInfo::builder(); +``` - ## 输入装配 @@ -231,7 +230,7 @@ let color_blend_state = vk::PipelineColorBlendStateCreateInfo::builder() 如果你想使用第二种混合方式(位运算结合),你应该把 `logic_op_enable` 设为 `true`。位运算操作可由 `logic_op` 字段指定。注意这会自动禁用第一种混合方式,就好像你把所有帧缓冲的 `blend_enable` 都置为了 `false`!`color_write_mask` 在这种模式下也会被用到,以决定哪些通道会被实际地传递到帧缓冲中。你也可以同时禁用这两种模式,就像我们这里做的一样,这样片元颜色就会原封不动地传递到帧缓冲中。 -## 动态状态 +## 动态状态(只是示例,不要添加到代码里) 有一部分状态是可以在不重新创建管线的情况下改变的。例如视口的大小、线宽和混合常量。如果你想这么做,那么你需要填充一个 `vk::PipelineDynamicStateCreateInfo` 结构体: @@ -263,9 +262,18 @@ struct AppData { 然后在 `create_pipeline` 函数中,在调用 `destroy_shader_module` 之前创建这个对象: ```rust,noplaypen -let layout_info = vk::PipelineLayoutCreateInfo::builder(); ++unsafe fn create_pipeline(device: &Device, data: &mut AppData) -> Result<()> { + // ... -data.pipeline_layout = device.create_pipeline_layout(&layout_info, None)?; + let layout_info = vk::PipelineLayoutCreateInfo::builder(); + + data.pipeline_layout = device.create_pipeline_layout(&layout_info, None)?; + + device.destroy_shader_module(vert_shader_module, None); + device.destroy_shader_module(frag_shader_module, None); + + Ok(()) +} ``` 这个结构体也用于指定*推式常量* —— 另一种给着色器传递动态值的方式,我们会在后面的章节中介绍。 diff --git a/book/src/pipeline/shader_modules.md b/book/src/pipeline/shader_modules.md index 74bd026..6a0b0b1 100644 --- a/book/src/pipeline/shader_modules.md +++ b/book/src/pipeline/shader_modules.md @@ -2,7 +2,7 @@ > 原文链接: > -> Commit Hash: ceb4a3fc6d8ca565af4f8679c4889bcad7941338 +> Commit Hash: 7becee96b0029bf721f833039c00ea2a417714dd **本章代码:**[main.rs](https://github.com/chuigda/Vulkan-Tutorial-Rust-CN/tree/master/src/09_shader_modules.rs) | [shader.vert](https://github.com/chuigda/Vulkan-Tutorial-Rust-CN/tree/master/shaders/09/shader.vert) | [shader.frag](https://github.com/chuigda/Vulkan-Tutorial-Rust-CN/tree/master/shaders/09/shader.frag) @@ -177,6 +177,15 @@ pause 将 `glslc` 的路径替换为你安装 Vulkan SDK 的路径。使用 `chmod +x compile.sh` 命令将这个脚本变得可执行,然后运行它。 +**MacOS** + +创建一个包含以下命令的 `compile.sh` 文件: + +```bash +/User/user/VulkanSDK/x.x.x.x/macOS/bin/glslc shaders/shader.vert -o vert.spv +/User/user/VulkanSDK/x.x.x.x/macOS/bin/glslc shaders/shader.frag -o frag.spv +``` +
我是一条可爱的分割线,诶嘿诶嘿地爬 _(ÒωÓ๑ゝ∠)_
diff --git a/book/src/presentation/swapchain.md b/book/src/presentation/swapchain.md index 7132d98..465ed2a 100644 --- a/book/src/presentation/swapchain.md +++ b/book/src/presentation/swapchain.md @@ -2,7 +2,7 @@ > 原文链接: > -> Commit Hash: ceb4a3fc6d8ca565af4f8679c4889bcad7941338 +> Commit Hash: 7becee96b0029bf721f833039c00ea2a417714dd **本章代码:**[main.rs](https://github.com/chuigda/Vulkan-Tutorial-Rust-CN/tree/master/src/06_swapchain_creation.rs) @@ -246,25 +246,21 @@ fn get_swapchain_extent( if capabilities.current_extent.width != u32::MAX { capabilities.current_extent } else { - let size = window.inner_size(); - let clamp = |min: u32, max: u32, v: u32| min.max(max.min(v)); vk::Extent2D::builder() - .width(clamp( + .width(window.inner_size().width.clamp( capabilities.min_image_extent.width, capabilities.max_image_extent.width, - size.width, )) - .height(clamp( + .height(window.inner_size().height.clamp( capabilities.min_image_extent.height, capabilities.max_image_extent.height, - size.height, )) .build() } } ``` -我们使用 `clamp` 函数来限制窗口的实际大小在 Vulkan 设备支持的范围内。 +我们使用 [`clamp`](https://doc.rust-lang.org/std/cmp/trait.Ord.html#method.clamp) 函数来限制窗口的实际大小在 Vulkan 设备支持的范围内。 ## 创建交换链 @@ -445,7 +441,7 @@ data.swapchain_images = device.get_swapchain_images_khr(data.swapchain)?; 还有一件事,我们需要保存交换链中图像的格式和交换范围,因为我们将在后面的章节中用到它们。在 `AppData` 中添加两个字段: ```rust,noplaypen -impl AppData { +struct AppData { // ... swapchain_format: vk::Format, swapchain_extent: vk::Extent2D, diff --git a/book/src/presentation/window_surface.md b/book/src/presentation/window_surface.md index 7c45f5a..4c5dc31 100644 --- a/book/src/presentation/window_surface.md +++ b/book/src/presentation/window_surface.md @@ -2,7 +2,7 @@ > 原文链接: > -> Commit Hash: ceb4a3fc6d8ca565af4f8679c4889bcad7941338 +> Commit Hash: 7becee96b0029bf721f833039c00ea2a417714dd **本章代码:**[main.rs](https://github.com/chuigda/Vulkan-Tutorial-Rust-CN/tree/master/src/05_window_surface.rs) @@ -159,6 +159,7 @@ let queue_infos = unique_indices let info = vk::DeviceCreateInfo::builder() .queue_create_infos(&queue_infos) .enabled_layer_names(&layers) + .enabled_extension_names(&extensions) .enabled_features(&features); ``` diff --git a/book/src/setup/base_code.md b/book/src/setup/base_code.md index cdfa7cc..6fbaf73 100644 --- a/book/src/setup/base_code.md +++ b/book/src/setup/base_code.md @@ -2,7 +2,7 @@ > 原文链接: > -> Commit Hash: ceb4a3fc6d8ca565af4f8679c4889bcad7941338 +> Commit Hash: 7becee96b0029bf721f833039c00ea2a417714dd **本章代码:**[main.rs](https://github.com/chuigda/Vulkan-Tutorial-Rust-CN/tree/master/src/00_base_code.rs) @@ -19,7 +19,7 @@ use anyhow::Result; use winit::dpi::LogicalSize; use winit::event::{Event, WindowEvent}; -use winit::event_loop::{ControlFlow, EventLoop}; +use winit::event_loop::, EventLoop; use winit::window::{Window, WindowBuilder}; fn main() -> Result<()> { @@ -27,7 +27,7 @@ fn main() -> Result<()> { // 创建窗口 - let event_loop = EventLoop::new(); + let event_loop = EventLoop::new()?; let window = WindowBuilder::new() .with_title("Vulkan Tutorial (Rust)") .with_inner_size(LogicalSize::new(1024, 768)) @@ -36,22 +36,26 @@ fn main() -> Result<()> { // 初始化应用程序 let mut app = unsafe { App::create(&window)? }; - let mut destroying = false; - event_loop.run(move |event, _, control_flow| { - *control_flow = ControlFlow::Poll; + let mut app = unsafe { App::create(&window)? }; + event_loop.run(move |event, elwt| { match event { - // Render a frame if our Vulkan app is not being destroyed. - Event::MainEventsCleared if !destroying => - unsafe { app.render(&window) }.unwrap(), - // Destroy our Vulkan app. - Event::WindowEvent { event: WindowEvent::CloseRequested, .. } => { - destroying = true; - *control_flow = ControlFlow::Exit; - unsafe { app.destroy(); } + // Request a redraw when all events were processed. + Event::AboutToWait => window.request_redraw(), + Event::WindowEvent { event, .. } => match event { + // Render a frame if our Vulkan app is not being destroyed. + WindowEvent::RedrawRequested if !elwt.exiting() => unsafe { app.render(&window) }.unwrap(), + // Destroy our Vulkan app. + WindowEvent::CloseRequested => { + elwt.exit(); + unsafe { app.destroy(); } + } + _ => {} } _ => {} } - }); + })?; + + Ok(()) } /// 我们的 Vulkan 应用程序 diff --git a/book/src/setup/validation_layers.md b/book/src/setup/validation_layers.md index 7f0e663..fdd704a 100644 --- a/book/src/setup/validation_layers.md +++ b/book/src/setup/validation_layers.md @@ -2,7 +2,7 @@ > 原文链接: > -> Commit Hash: ceb4a3fc6d8ca565af4f8679c4889bcad7941338 +> Commit Hash: 7becee96b0029bf721f833039c00ea2a417714dd **本章代码:**[main.rs](https://github.com/chuigda/Vulkan-Tutorial-Rust-CN/tree/master/src/02_validation_layers.rs) @@ -196,7 +196,11 @@ unsafe fn create_instance( if VALIDATION_ENABLED { let debug_info = vk::DebugUtilsMessengerCreateInfoEXT::builder() .message_severity(vk::DebugUtilsMessageSeverityFlagsEXT::all()) - .message_type(vk::DebugUtilsMessageTypeFlagsEXT::all()) + .message_type( + vk::DebugUtilsMessageTypeFlagsEXT::GENERAL + | vk::DebugUtilsMessageTypeFlagsEXT::VALIDATION + | vk::DebugUtilsMessageTypeFlagsEXT::PERFORMANCE, + ) .user_callback(Some(debug_callback)); data.messenger = instance.create_debug_utils_messenger_ext(&debug_info, None)?; @@ -206,8 +210,11 @@ unsafe fn create_instance( } ``` - -> **注:**在一组 Vulkan 标志(例如上面的例子中的 `vk::DebugUtilsMessageTypeFlagsEXT::all()`)上调用 `all` 静态方法会返回一系列 `vulkanalia` 可识别的标志。这会导致一个问题,如果应用程序使用的 Vulkan 实现版本比 `vulkanalia` 支持的 Vulkan 版本旧,那么这个标志集可能包含应用程序使用的 Vulkan 实现不认识的标志。这不会影响应用程序的功能,但是你可能会看到一些校验层错误。如果你因为这些调试标志遇到了未知标志的警告,你可以通过升级你的 Vulkan SDK 到最新版本(或者直接指定支持的标志)来避免这些警告。 +> **注:** 对 Vulkan 标志类型调用静态方法 `all`(例如以上代码中的 `vk::DebugUtilsMessageSeverityFlagsEXT::all()`)会返回 `vulkanalia` 已知的所有标志。完整的标志集合中可能包括仅当特定扩展启用时才有效的标志,或是在比你所用/所面向的 Vulkan 版本更高的版本中才可用的标志。 +> +> 在以上代码中,我们明确列出了我们需要使用的 `vk::DebugUtilsMessageTypeFlagsEXT` 标志,因为这个标志集包含了一个标志(`vk::DebugUtilsMessageTypeFlagsEXT::DEVICE_ADDRESS_BINDING`),它只有在[启用了某个特定扩展](https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/VK_EXT_device_address_binding_report.html) 时才有效。 +> +> 多数情况下,使用不支持的标志不会导致任何错误或改变应用程序的行为,但如果你启用了校验层(正如我们在本章中所做的那样),那么它肯定会导致校验错误。 首先,我们从返回表达式中提取出 Vulkan 实例,这样我们就可以用它来添加我们的调试回调函数。 @@ -281,7 +288,11 @@ let mut info = vk::InstanceCreateInfo::builder() let mut debug_info = vk::DebugUtilsMessengerCreateInfoEXT::builder() .message_severity(vk::DebugUtilsMessageSeverityFlagsEXT::all()) - .message_type(vk::DebugUtilsMessageTypeFlagsEXT::all()) + .message_type( + vk::DebugUtilsMessageTypeFlagsEXT::GENERAL + | vk::DebugUtilsMessageTypeFlagsEXT::VALIDATION + | vk::DebugUtilsMessageTypeFlagsEXT::PERFORMANCE, + ) .user_callback(Some(debug_callback)); if VALIDATION_ENABLED { @@ -289,6 +300,10 @@ if VALIDATION_ENABLED { } ``` + + +> **注:**在这里选择同样的调试信息,包括相同的严重程度、类型和回调函数,既调用 `create_debug_utils_messenger_ext` *又*添加到 `vk::InstanceCreateInfo` 实例的扩展中,可能看起来有些多余。然而,这两种用法有不同的目的。这里的用法(将调试信息添加到 `vk::InstanceCreateInfo`)设置了在创建和销毁实例期间的调试。调用 `create_debug_utils_messenger_ext` 为其他一切设置了持久性调试。请参阅 Vulkan 规范中[相关章节](https://registry.khronos.org/vulkan/specs/1.3-extensions/html/chap4.html#VkInstanceCreateInfo)中以 "To capture events that occur while creating or destroying an instance" 开头的段落。 + `debug_info` 需要在条件语句之外定义,因为它需要一直存活到我们调用完 `create_instance` 之后。幸运的是,我们可以依赖 Rust 编译器来保护我们:因为 `vulkanalia` 生成器定义的生命周期,我们无法将一个活得不够长的结构体推到指针链上。 现在我们可以运行程序,观察调试回调函数打印的日志了。不过我们要先设置 `RUST_LOG` 环境变量,这样 `pretty_env_logger` 就会启用我们感兴趣的日志级别。我们先把日志级别设置为 `debug`,以确定这些东西工作正常。下面是在 Windows(PowerShell)上的一个例子: diff --git a/book/src/swapchain/recreation.md b/book/src/swapchain/recreation.md index 6de29a4..4d2dba9 100644 --- a/book/src/swapchain/recreation.md +++ b/book/src/swapchain/recreation.md @@ -2,7 +2,7 @@ > 原文链接: > -> Commit Hash: ceb4a3fc6d8ca565af4f8679c4889bcad7941338 +> Commit Hash: 7becee96b0029bf721f833039c00ea2a417714dd **本章代码:**[main.rs](https://github.com/chuigda/Vulkan-Tutorial-Rust-CN/tree/master/src/16_swapchain_recreation.rs) @@ -175,24 +175,27 @@ match event { ```rust,noplaypen let mut app = unsafe { App::create(&window)? }; -let mut destroying = false; let mut minimized = false; -event_loop.run(move |event, _, control_flow| { - *control_flow = ControlFlow::Poll; +event_loop.run(move |event,elwt| { match event { - Event::MainEventsCleared if !destroying && !minimized => - unsafe { app.render(&window) }.unwrap(), - Event::WindowEvent { event: WindowEvent::Resized(size), .. } => { - if size.width == 0 || size.height == 0 { - minimized = true; - } else { - minimized = false; - app.resized = true; + // ... + Event::WindowEvent { event, .. } => match event { + WindowEvent::RedrawRequested if !elwt.exiting() && !minimized => { + unsafe { app.render(&window) }.unwrap(); + }, + WindowEvent::Resized(size) => { + if size.width == 0 || size.height == 0 { + minimized = true; + } else { + minimized = false; + app.resized = true; + } } + // ... } // ... } -}); +})?; ``` 恭喜你,你已经完成了你的第一个行为良好的 Vulkan 程序!在下一章中,我们会避免在顶点着色器中硬编码顶点,并实际地使用一个顶点缓冲。 diff --git a/book/src/vertex/vertex_buffer_creation.md b/book/src/vertex/vertex_buffer_creation.md index 84ef630..12d4cb9 100644 --- a/book/src/vertex/vertex_buffer_creation.md +++ b/book/src/vertex/vertex_buffer_creation.md @@ -2,12 +2,14 @@ > 原文链接: > -> Commit Hash: ceb4a3fc6d8ca565af4f8679c4889bcad7941338 +> Commit Hash: 7becee96b0029bf721f833039c00ea2a417714dd **本章代码:**[main.rs](https://github.com/chuigda/Vulkan-Tutorial-Rust-CN/tree/master/src/18_vertex_buffer.rs) 在 Vulkan 中,缓冲是用于存储可被显卡读取的任意数据的内存区域。我们会在本章中用它们来存储顶点数据,但它们也可以用于许多其他目的,这些将在以后的章节中探讨。与我们到目前为止见过的 Vulkan 对象不同,缓冲不会自动为自己分配内存。前面章节中的工作已经表明,Vulkan API 将几乎所有事物置于程序员的控制下,内存管理就是其中之一。 +> 尽管本教程只使用 Vulkan API 管理内存, 但现实世界中的许多 Vulkan 应用程序使用更高级的抽象,例如 [Vulkan 内存分配器](https://github.com/GPUOpen-LibrariesAndSDKs/VulkanMemoryAllocator)(VMA)。VMA 是一个库,它封装了 Vulkan API,并使内存管理变得更加简单和容易。[`vulkanalia-vma` crate](https://docs.rs/vulkanalia-vma)(`vulkanalia` 项目的一部分)提供了 VMA 与 `vulkanalia` 的集成。 + ## 创建缓冲 首先,我们创建一个名为 `create_vertex_buffer` 的新函数,并在 `App::create` 函数中,在 `create_command_buffers` 之前调用它。 diff --git a/src/00_base_code.rs b/src/00_base_code.rs index 2ea5772..a8edc2e 100644 --- a/src/00_base_code.rs +++ b/src/00_base_code.rs @@ -11,7 +11,7 @@ use anyhow::Result; use winit::dpi::LogicalSize; use winit::event::{Event, WindowEvent}; -use winit::event_loop::{ControlFlow, EventLoop}; +use winit::event_loop::EventLoop; use winit::window::{Window, WindowBuilder}; #[rustfmt::skip] @@ -20,7 +20,7 @@ fn main() -> Result<()> { // Window - let event_loop = EventLoop::new(); + let event_loop = EventLoop::new()?; let window = WindowBuilder::new() .with_title("Vulkan Tutorial (Rust)") .with_inner_size(LogicalSize::new(1024, 768)) @@ -29,21 +29,25 @@ fn main() -> Result<()> { // App let mut app = unsafe { App::create(&window)? }; - let mut destroying = false; - event_loop.run(move |event, _, control_flow| { - *control_flow = ControlFlow::Poll; + event_loop.run(move |event, elwt| { match event { - // Render a frame if our Vulkan app is not being destroyed. - Event::MainEventsCleared if !destroying => unsafe { app.render(&window) }.unwrap(), - // Destroy our Vulkan app. - Event::WindowEvent { event: WindowEvent::CloseRequested, .. } => { - destroying = true; - *control_flow = ControlFlow::Exit; - unsafe { app.destroy(); } + // Request a redraw when all events were processed. + Event::AboutToWait => window.request_redraw(), + Event::WindowEvent { event, .. } => match event { + // Render a frame if our Vulkan app is not being destroyed. + WindowEvent::RedrawRequested if !elwt.exiting() => unsafe { app.render(&window) }.unwrap(), + // Destroy our Vulkan app. + WindowEvent::CloseRequested => { + elwt.exit(); + unsafe { app.destroy(); } + } + _ => {} } _ => {} } - }); + })?; + + Ok(()) } /// Our Vulkan app. diff --git a/src/01_instance_creation.rs b/src/01_instance_creation.rs index 1f4b7db..f55e1c4 100644 --- a/src/01_instance_creation.rs +++ b/src/01_instance_creation.rs @@ -16,7 +16,7 @@ use vulkanalia::window as vk_window; use vulkanalia::Version; use winit::dpi::LogicalSize; use winit::event::{Event, WindowEvent}; -use winit::event_loop::{ControlFlow, EventLoop}; +use winit::event_loop::EventLoop; use winit::window::{Window, WindowBuilder}; /// The Vulkan SDK version that started requiring the portability subset extension for macOS. @@ -28,7 +28,7 @@ fn main() -> Result<()> { // Window - let event_loop = EventLoop::new(); + let event_loop = EventLoop::new()?; let window = WindowBuilder::new() .with_title("Vulkan Tutorial (Rust)") .with_inner_size(LogicalSize::new(1024, 768)) @@ -37,21 +37,25 @@ fn main() -> Result<()> { // App let mut app = unsafe { App::create(&window)? }; - let mut destroying = false; - event_loop.run(move |event, _, control_flow| { - *control_flow = ControlFlow::Poll; + event_loop.run(move |event, elwt| { match event { - // Render a frame if our Vulkan app is not being destroyed. - Event::MainEventsCleared if !destroying => unsafe { app.render(&window) }.unwrap(), - // Destroy our Vulkan app. - Event::WindowEvent { event: WindowEvent::CloseRequested, .. } => { - destroying = true; - *control_flow = ControlFlow::Exit; - unsafe { app.destroy(); } + // Request a redraw when all events were processed. + Event::AboutToWait => window.request_redraw(), + Event::WindowEvent { event, .. } => match event { + // Render a frame if our Vulkan app is not being destroyed. + WindowEvent::RedrawRequested if !elwt.exiting() => unsafe { app.render(&window) }.unwrap(), + // Destroy our Vulkan app. + WindowEvent::CloseRequested => { + elwt.exit(); + unsafe { app.destroy(); } + } + _ => {} } _ => {} } - }); + })?; + + Ok(()) } /// Our Vulkan app. diff --git a/src/02_validation_layers.rs b/src/02_validation_layers.rs index 33296a6..d2014f4 100644 --- a/src/02_validation_layers.rs +++ b/src/02_validation_layers.rs @@ -20,7 +20,7 @@ use vulkanalia::window as vk_window; use vulkanalia::Version; use winit::dpi::LogicalSize; use winit::event::{Event, WindowEvent}; -use winit::event_loop::{ControlFlow, EventLoop}; +use winit::event_loop::EventLoop; use winit::window::{Window, WindowBuilder}; use vulkanalia::vk::ExtDebugUtilsExtension; @@ -39,7 +39,7 @@ fn main() -> Result<()> { // Window - let event_loop = EventLoop::new(); + let event_loop = EventLoop::new()?; let window = WindowBuilder::new() .with_title("Vulkan Tutorial (Rust)") .with_inner_size(LogicalSize::new(1024, 768)) @@ -48,21 +48,25 @@ fn main() -> Result<()> { // App let mut app = unsafe { App::create(&window)? }; - let mut destroying = false; - event_loop.run(move |event, _, control_flow| { - *control_flow = ControlFlow::Poll; + event_loop.run(move |event, elwt| { match event { - // Render a frame if our Vulkan app is not being destroyed. - Event::MainEventsCleared if !destroying => unsafe { app.render(&window) }.unwrap(), - // Destroy our Vulkan app. - Event::WindowEvent { event: WindowEvent::CloseRequested, .. } => { - destroying = true; - *control_flow = ControlFlow::Exit; - unsafe { app.destroy(); } + // Request a redraw when all events were processed. + Event::AboutToWait => window.request_redraw(), + Event::WindowEvent { event, .. } => match event { + // Render a frame if our Vulkan app is not being destroyed. + WindowEvent::RedrawRequested if !elwt.exiting() => unsafe { app.render(&window) }.unwrap(), + // Destroy our Vulkan app. + WindowEvent::CloseRequested => { + elwt.exit(); + unsafe { app.destroy(); } + } + _ => {} } _ => {} } - }); + })?; + + Ok(()) } /// Our Vulkan app. @@ -169,7 +173,11 @@ unsafe fn create_instance(window: &Window, entry: &Entry, data: &mut AppData) -> let mut debug_info = vk::DebugUtilsMessengerCreateInfoEXT::builder() .message_severity(vk::DebugUtilsMessageSeverityFlagsEXT::all()) - .message_type(vk::DebugUtilsMessageTypeFlagsEXT::all()) + .message_type( + vk::DebugUtilsMessageTypeFlagsEXT::GENERAL + | vk::DebugUtilsMessageTypeFlagsEXT::VALIDATION + | vk::DebugUtilsMessageTypeFlagsEXT::PERFORMANCE, + ) .user_callback(Some(debug_callback)); if VALIDATION_ENABLED { diff --git a/src/03_physical_device_selection.rs b/src/03_physical_device_selection.rs index 0964d11..88cb14b 100644 --- a/src/03_physical_device_selection.rs +++ b/src/03_physical_device_selection.rs @@ -21,7 +21,7 @@ use vulkanalia::window as vk_window; use vulkanalia::Version; use winit::dpi::LogicalSize; use winit::event::{Event, WindowEvent}; -use winit::event_loop::{ControlFlow, EventLoop}; +use winit::event_loop::EventLoop; use winit::window::{Window, WindowBuilder}; use vulkanalia::vk::ExtDebugUtilsExtension; @@ -40,7 +40,7 @@ fn main() -> Result<()> { // Window - let event_loop = EventLoop::new(); + let event_loop = EventLoop::new()?; let window = WindowBuilder::new() .with_title("Vulkan Tutorial (Rust)") .with_inner_size(LogicalSize::new(1024, 768)) @@ -49,21 +49,25 @@ fn main() -> Result<()> { // App let mut app = unsafe { App::create(&window)? }; - let mut destroying = false; - event_loop.run(move |event, _, control_flow| { - *control_flow = ControlFlow::Poll; + event_loop.run(move |event, elwt| { match event { - // Render a frame if our Vulkan app is not being destroyed. - Event::MainEventsCleared if !destroying => unsafe { app.render(&window) }.unwrap(), - // Destroy our Vulkan app. - Event::WindowEvent { event: WindowEvent::CloseRequested, .. } => { - destroying = true; - *control_flow = ControlFlow::Exit; - unsafe { app.destroy(); } + // Request a redraw when all events were processed. + Event::AboutToWait => window.request_redraw(), + Event::WindowEvent { event, .. } => match event { + // Render a frame if our Vulkan app is not being destroyed. + WindowEvent::RedrawRequested if !elwt.exiting() => unsafe { app.render(&window) }.unwrap(), + // Destroy our Vulkan app. + WindowEvent::CloseRequested => { + elwt.exit(); + unsafe { app.destroy(); } + } + _ => {} } _ => {} } - }); + })?; + + Ok(()) } /// Our Vulkan app. @@ -173,7 +177,11 @@ unsafe fn create_instance(window: &Window, entry: &Entry, data: &mut AppData) -> let mut debug_info = vk::DebugUtilsMessengerCreateInfoEXT::builder() .message_severity(vk::DebugUtilsMessageSeverityFlagsEXT::all()) - .message_type(vk::DebugUtilsMessageTypeFlagsEXT::all()) + .message_type( + vk::DebugUtilsMessageTypeFlagsEXT::GENERAL + | vk::DebugUtilsMessageTypeFlagsEXT::VALIDATION + | vk::DebugUtilsMessageTypeFlagsEXT::PERFORMANCE, + ) .user_callback(Some(debug_callback)); if VALIDATION_ENABLED { diff --git a/src/04_logical_device.rs b/src/04_logical_device.rs index d2dde39..63787ce 100644 --- a/src/04_logical_device.rs +++ b/src/04_logical_device.rs @@ -21,7 +21,7 @@ use vulkanalia::window as vk_window; use vulkanalia::Version; use winit::dpi::LogicalSize; use winit::event::{Event, WindowEvent}; -use winit::event_loop::{ControlFlow, EventLoop}; +use winit::event_loop::EventLoop; use winit::window::{Window, WindowBuilder}; use vulkanalia::vk::ExtDebugUtilsExtension; @@ -40,7 +40,7 @@ fn main() -> Result<()> { // Window - let event_loop = EventLoop::new(); + let event_loop = EventLoop::new()?; let window = WindowBuilder::new() .with_title("Vulkan Tutorial (Rust)") .with_inner_size(LogicalSize::new(1024, 768)) @@ -49,21 +49,25 @@ fn main() -> Result<()> { // App let mut app = unsafe { App::create(&window)? }; - let mut destroying = false; - event_loop.run(move |event, _, control_flow| { - *control_flow = ControlFlow::Poll; + event_loop.run(move |event, elwt| { match event { - // Render a frame if our Vulkan app is not being destroyed. - Event::MainEventsCleared if !destroying => unsafe { app.render(&window) }.unwrap(), - // Destroy our Vulkan app. - Event::WindowEvent { event: WindowEvent::CloseRequested, .. } => { - destroying = true; - *control_flow = ControlFlow::Exit; - unsafe { app.destroy(); } + // Request a redraw when all events were processed. + Event::AboutToWait => window.request_redraw(), + Event::WindowEvent { event, .. } => match event { + // Render a frame if our Vulkan app is not being destroyed. + WindowEvent::RedrawRequested if !elwt.exiting() => unsafe { app.render(&window) }.unwrap(), + // Destroy our Vulkan app. + WindowEvent::CloseRequested => { + elwt.exit(); + unsafe { app.destroy(); } + } + _ => {} } _ => {} } - }); + })?; + + Ok(()) } /// Our Vulkan app. @@ -183,7 +187,11 @@ unsafe fn create_instance(window: &Window, entry: &Entry, data: &mut AppData) -> let mut debug_info = vk::DebugUtilsMessengerCreateInfoEXT::builder() .message_severity(vk::DebugUtilsMessageSeverityFlagsEXT::all()) - .message_type(vk::DebugUtilsMessageTypeFlagsEXT::all()) + .message_type( + vk::DebugUtilsMessageTypeFlagsEXT::GENERAL + | vk::DebugUtilsMessageTypeFlagsEXT::VALIDATION + | vk::DebugUtilsMessageTypeFlagsEXT::PERFORMANCE, + ) .user_callback(Some(debug_callback)); if VALIDATION_ENABLED { diff --git a/src/05_window_surface.rs b/src/05_window_surface.rs index b8ee2c3..9b64769 100644 --- a/src/05_window_surface.rs +++ b/src/05_window_surface.rs @@ -21,7 +21,7 @@ use vulkanalia::window as vk_window; use vulkanalia::Version; use winit::dpi::LogicalSize; use winit::event::{Event, WindowEvent}; -use winit::event_loop::{ControlFlow, EventLoop}; +use winit::event_loop::EventLoop; use winit::window::{Window, WindowBuilder}; use vulkanalia::vk::ExtDebugUtilsExtension; @@ -41,7 +41,7 @@ fn main() -> Result<()> { // Window - let event_loop = EventLoop::new(); + let event_loop = EventLoop::new()?; let window = WindowBuilder::new() .with_title("Vulkan Tutorial (Rust)") .with_inner_size(LogicalSize::new(1024, 768)) @@ -50,21 +50,25 @@ fn main() -> Result<()> { // App let mut app = unsafe { App::create(&window)? }; - let mut destroying = false; - event_loop.run(move |event, _, control_flow| { - *control_flow = ControlFlow::Poll; + event_loop.run(move |event, elwt| { match event { - // Render a frame if our Vulkan app is not being destroyed. - Event::MainEventsCleared if !destroying => unsafe { app.render(&window) }.unwrap(), - // Destroy our Vulkan app. - Event::WindowEvent { event: WindowEvent::CloseRequested, .. } => { - destroying = true; - *control_flow = ControlFlow::Exit; - unsafe { app.destroy(); } + // Request a redraw when all events were processed. + Event::AboutToWait => window.request_redraw(), + Event::WindowEvent { event, .. } => match event { + // Render a frame if our Vulkan app is not being destroyed. + WindowEvent::RedrawRequested if !elwt.exiting() => unsafe { app.render(&window) }.unwrap(), + // Destroy our Vulkan app. + WindowEvent::CloseRequested => { + elwt.exit(); + unsafe { app.destroy(); } + } + _ => {} } _ => {} } - }); + })?; + + Ok(()) } /// Our Vulkan app. @@ -189,7 +193,11 @@ unsafe fn create_instance(window: &Window, entry: &Entry, data: &mut AppData) -> let mut debug_info = vk::DebugUtilsMessengerCreateInfoEXT::builder() .message_severity(vk::DebugUtilsMessageSeverityFlagsEXT::all()) - .message_type(vk::DebugUtilsMessageTypeFlagsEXT::all()) + .message_type( + vk::DebugUtilsMessageTypeFlagsEXT::GENERAL + | vk::DebugUtilsMessageTypeFlagsEXT::VALIDATION + | vk::DebugUtilsMessageTypeFlagsEXT::PERFORMANCE, + ) .user_callback(Some(debug_callback)); if VALIDATION_ENABLED { diff --git a/src/06_swapchain_creation.rs b/src/06_swapchain_creation.rs index 98b50da..0d6a7c3 100644 --- a/src/06_swapchain_creation.rs +++ b/src/06_swapchain_creation.rs @@ -21,7 +21,7 @@ use vulkanalia::window as vk_window; use vulkanalia::Version; use winit::dpi::LogicalSize; use winit::event::{Event, WindowEvent}; -use winit::event_loop::{ControlFlow, EventLoop}; +use winit::event_loop::EventLoop; use winit::window::{Window, WindowBuilder}; use vulkanalia::vk::ExtDebugUtilsExtension; @@ -44,7 +44,7 @@ fn main() -> Result<()> { // Window - let event_loop = EventLoop::new(); + let event_loop = EventLoop::new()?; let window = WindowBuilder::new() .with_title("Vulkan Tutorial (Rust)") .with_inner_size(LogicalSize::new(1024, 768)) @@ -53,21 +53,25 @@ fn main() -> Result<()> { // App let mut app = unsafe { App::create(&window)? }; - let mut destroying = false; - event_loop.run(move |event, _, control_flow| { - *control_flow = ControlFlow::Poll; + event_loop.run(move |event, elwt| { match event { - // Render a frame if our Vulkan app is not being destroyed. - Event::MainEventsCleared if !destroying => unsafe { app.render(&window) }.unwrap(), - // Destroy our Vulkan app. - Event::WindowEvent { event: WindowEvent::CloseRequested, .. } => { - destroying = true; - *control_flow = ControlFlow::Exit; - unsafe { app.destroy(); } + // Request a redraw when all events were processed. + Event::AboutToWait => window.request_redraw(), + Event::WindowEvent { event, .. } => match event { + // Render a frame if our Vulkan app is not being destroyed. + WindowEvent::RedrawRequested if !elwt.exiting() => unsafe { app.render(&window) }.unwrap(), + // Destroy our Vulkan app. + WindowEvent::CloseRequested => { + elwt.exit(); + unsafe { app.destroy(); } + } + _ => {} } _ => {} } - }); + })?; + + Ok(()) } /// Our Vulkan app. @@ -199,7 +203,11 @@ unsafe fn create_instance(window: &Window, entry: &Entry, data: &mut AppData) -> let mut debug_info = vk::DebugUtilsMessengerCreateInfoEXT::builder() .message_severity(vk::DebugUtilsMessageSeverityFlagsEXT::all()) - .message_type(vk::DebugUtilsMessageTypeFlagsEXT::all()) + .message_type( + vk::DebugUtilsMessageTypeFlagsEXT::GENERAL + | vk::DebugUtilsMessageTypeFlagsEXT::VALIDATION + | vk::DebugUtilsMessageTypeFlagsEXT::PERFORMANCE, + ) .user_callback(Some(debug_callback)); if VALIDATION_ENABLED { @@ -428,22 +436,19 @@ fn get_swapchain_present_mode(present_modes: &[vk::PresentModeKHR]) -> vk::Prese .unwrap_or(vk::PresentModeKHR::FIFO) } +#[rustfmt::skip] fn get_swapchain_extent(window: &Window, capabilities: vk::SurfaceCapabilitiesKHR) -> vk::Extent2D { - if capabilities.current_extent.width != u32::max_value() { + if capabilities.current_extent.width != u32::MAX { capabilities.current_extent } else { - let size = window.inner_size(); - let clamp = |min: u32, max: u32, v: u32| min.max(max.min(v)); vk::Extent2D::builder() - .width(clamp( + .width(window.inner_size().width.clamp( capabilities.min_image_extent.width, capabilities.max_image_extent.width, - size.width, )) - .height(clamp( + .height(window.inner_size().height.clamp( capabilities.min_image_extent.height, capabilities.max_image_extent.height, - size.height, )) .build() } diff --git a/src/07_image_views.rs b/src/07_image_views.rs index 3457cc0..2d54d3c 100644 --- a/src/07_image_views.rs +++ b/src/07_image_views.rs @@ -21,7 +21,7 @@ use vulkanalia::window as vk_window; use vulkanalia::Version; use winit::dpi::LogicalSize; use winit::event::{Event, WindowEvent}; -use winit::event_loop::{ControlFlow, EventLoop}; +use winit::event_loop::EventLoop; use winit::window::{Window, WindowBuilder}; use vulkanalia::vk::ExtDebugUtilsExtension; @@ -44,7 +44,7 @@ fn main() -> Result<()> { // Window - let event_loop = EventLoop::new(); + let event_loop = EventLoop::new()?; let window = WindowBuilder::new() .with_title("Vulkan Tutorial (Rust)") .with_inner_size(LogicalSize::new(1024, 768)) @@ -53,21 +53,25 @@ fn main() -> Result<()> { // App let mut app = unsafe { App::create(&window)? }; - let mut destroying = false; - event_loop.run(move |event, _, control_flow| { - *control_flow = ControlFlow::Poll; + event_loop.run(move |event, elwt| { match event { - // Render a frame if our Vulkan app is not being destroyed. - Event::MainEventsCleared if !destroying => unsafe { app.render(&window) }.unwrap(), - // Destroy our Vulkan app. - Event::WindowEvent { event: WindowEvent::CloseRequested, .. } => { - destroying = true; - *control_flow = ControlFlow::Exit; - unsafe { app.destroy(); } + // Request a redraw when all events were processed. + Event::AboutToWait => window.request_redraw(), + Event::WindowEvent { event, .. } => match event { + // Render a frame if our Vulkan app is not being destroyed. + WindowEvent::RedrawRequested if !elwt.exiting() => unsafe { app.render(&window) }.unwrap(), + // Destroy our Vulkan app. + WindowEvent::CloseRequested => { + elwt.exit(); + unsafe { app.destroy(); } + } + _ => {} } _ => {} } - }); + })?; + + Ok(()) } /// Our Vulkan app. @@ -202,7 +206,11 @@ unsafe fn create_instance(window: &Window, entry: &Entry, data: &mut AppData) -> let mut debug_info = vk::DebugUtilsMessengerCreateInfoEXT::builder() .message_severity(vk::DebugUtilsMessageSeverityFlagsEXT::all()) - .message_type(vk::DebugUtilsMessageTypeFlagsEXT::all()) + .message_type( + vk::DebugUtilsMessageTypeFlagsEXT::GENERAL + | vk::DebugUtilsMessageTypeFlagsEXT::VALIDATION + | vk::DebugUtilsMessageTypeFlagsEXT::PERFORMANCE, + ) .user_callback(Some(debug_callback)); if VALIDATION_ENABLED { @@ -431,22 +439,19 @@ fn get_swapchain_present_mode(present_modes: &[vk::PresentModeKHR]) -> vk::Prese .unwrap_or(vk::PresentModeKHR::FIFO) } +#[rustfmt::skip] fn get_swapchain_extent(window: &Window, capabilities: vk::SurfaceCapabilitiesKHR) -> vk::Extent2D { - if capabilities.current_extent.width != u32::max_value() { + if capabilities.current_extent.width != u32::MAX { capabilities.current_extent } else { - let size = window.inner_size(); - let clamp = |min: u32, max: u32, v: u32| min.max(max.min(v)); vk::Extent2D::builder() - .width(clamp( + .width(window.inner_size().width.clamp( capabilities.min_image_extent.width, capabilities.max_image_extent.width, - size.width, )) - .height(clamp( + .height(window.inner_size().height.clamp( capabilities.min_image_extent.height, capabilities.max_image_extent.height, - size.height, )) .build() } diff --git a/src/08_graphics_pipeline.rs b/src/08_graphics_pipeline.rs index c01aa29..6b41b79 100644 --- a/src/08_graphics_pipeline.rs +++ b/src/08_graphics_pipeline.rs @@ -21,7 +21,7 @@ use vulkanalia::window as vk_window; use vulkanalia::Version; use winit::dpi::LogicalSize; use winit::event::{Event, WindowEvent}; -use winit::event_loop::{ControlFlow, EventLoop}; +use winit::event_loop::EventLoop; use winit::window::{Window, WindowBuilder}; use vulkanalia::vk::ExtDebugUtilsExtension; @@ -44,7 +44,7 @@ fn main() -> Result<()> { // Window - let event_loop = EventLoop::new(); + let event_loop = EventLoop::new()?; let window = WindowBuilder::new() .with_title("Vulkan Tutorial (Rust)") .with_inner_size(LogicalSize::new(1024, 768)) @@ -53,21 +53,25 @@ fn main() -> Result<()> { // App let mut app = unsafe { App::create(&window)? }; - let mut destroying = false; - event_loop.run(move |event, _, control_flow| { - *control_flow = ControlFlow::Poll; + event_loop.run(move |event, elwt| { match event { - // Render a frame if our Vulkan app is not being destroyed. - Event::MainEventsCleared if !destroying => unsafe { app.render(&window) }.unwrap(), - // Destroy our Vulkan app. - Event::WindowEvent { event: WindowEvent::CloseRequested, .. } => { - destroying = true; - *control_flow = ControlFlow::Exit; - unsafe { app.destroy(); } + // Request a redraw when all events were processed. + Event::AboutToWait => window.request_redraw(), + Event::WindowEvent { event, .. } => match event { + // Render a frame if our Vulkan app is not being destroyed. + WindowEvent::RedrawRequested if !elwt.exiting() => unsafe { app.render(&window) }.unwrap(), + // Destroy our Vulkan app. + WindowEvent::CloseRequested => { + elwt.exit(); + unsafe { app.destroy(); } + } + _ => {} } _ => {} } - }); + })?; + + Ok(()) } /// Our Vulkan app. @@ -203,7 +207,11 @@ unsafe fn create_instance(window: &Window, entry: &Entry, data: &mut AppData) -> let mut debug_info = vk::DebugUtilsMessengerCreateInfoEXT::builder() .message_severity(vk::DebugUtilsMessageSeverityFlagsEXT::all()) - .message_type(vk::DebugUtilsMessageTypeFlagsEXT::all()) + .message_type( + vk::DebugUtilsMessageTypeFlagsEXT::GENERAL + | vk::DebugUtilsMessageTypeFlagsEXT::VALIDATION + | vk::DebugUtilsMessageTypeFlagsEXT::PERFORMANCE, + ) .user_callback(Some(debug_callback)); if VALIDATION_ENABLED { @@ -432,22 +440,19 @@ fn get_swapchain_present_mode(present_modes: &[vk::PresentModeKHR]) -> vk::Prese .unwrap_or(vk::PresentModeKHR::FIFO) } +#[rustfmt::skip] fn get_swapchain_extent(window: &Window, capabilities: vk::SurfaceCapabilitiesKHR) -> vk::Extent2D { - if capabilities.current_extent.width != u32::max_value() { + if capabilities.current_extent.width != u32::MAX { capabilities.current_extent } else { - let size = window.inner_size(); - let clamp = |min: u32, max: u32, v: u32| min.max(max.min(v)); vk::Extent2D::builder() - .width(clamp( + .width(window.inner_size().width.clamp( capabilities.min_image_extent.width, capabilities.max_image_extent.width, - size.width, )) - .height(clamp( + .height(window.inner_size().height.clamp( capabilities.min_image_extent.height, capabilities.max_image_extent.height, - size.height, )) .build() } diff --git a/src/09_shader_modules.rs b/src/09_shader_modules.rs index 8af48c3..f1376cf 100644 --- a/src/09_shader_modules.rs +++ b/src/09_shader_modules.rs @@ -22,7 +22,7 @@ use vulkanalia::window as vk_window; use vulkanalia::Version; use winit::dpi::LogicalSize; use winit::event::{Event, WindowEvent}; -use winit::event_loop::{ControlFlow, EventLoop}; +use winit::event_loop::EventLoop; use winit::window::{Window, WindowBuilder}; use vulkanalia::vk::ExtDebugUtilsExtension; @@ -45,7 +45,7 @@ fn main() -> Result<()> { // Window - let event_loop = EventLoop::new(); + let event_loop = EventLoop::new()?; let window = WindowBuilder::new() .with_title("Vulkan Tutorial (Rust)") .with_inner_size(LogicalSize::new(1024, 768)) @@ -54,21 +54,25 @@ fn main() -> Result<()> { // App let mut app = unsafe { App::create(&window)? }; - let mut destroying = false; - event_loop.run(move |event, _, control_flow| { - *control_flow = ControlFlow::Poll; + event_loop.run(move |event, elwt| { match event { - // Render a frame if our Vulkan app is not being destroyed. - Event::MainEventsCleared if !destroying => unsafe { app.render(&window) }.unwrap(), - // Destroy our Vulkan app. - Event::WindowEvent { event: WindowEvent::CloseRequested, .. } => { - destroying = true; - *control_flow = ControlFlow::Exit; - unsafe { app.destroy(); } + // Request a redraw when all events were processed. + Event::AboutToWait => window.request_redraw(), + Event::WindowEvent { event, .. } => match event { + // Render a frame if our Vulkan app is not being destroyed. + WindowEvent::RedrawRequested if !elwt.exiting() => unsafe { app.render(&window) }.unwrap(), + // Destroy our Vulkan app. + WindowEvent::CloseRequested => { + elwt.exit(); + unsafe { app.destroy(); } + } + _ => {} } _ => {} } - }); + })?; + + Ok(()) } /// Our Vulkan app. @@ -204,7 +208,11 @@ unsafe fn create_instance(window: &Window, entry: &Entry, data: &mut AppData) -> let mut debug_info = vk::DebugUtilsMessengerCreateInfoEXT::builder() .message_severity(vk::DebugUtilsMessageSeverityFlagsEXT::all()) - .message_type(vk::DebugUtilsMessageTypeFlagsEXT::all()) + .message_type( + vk::DebugUtilsMessageTypeFlagsEXT::GENERAL + | vk::DebugUtilsMessageTypeFlagsEXT::VALIDATION + | vk::DebugUtilsMessageTypeFlagsEXT::PERFORMANCE, + ) .user_callback(Some(debug_callback)); if VALIDATION_ENABLED { @@ -433,22 +441,19 @@ fn get_swapchain_present_mode(present_modes: &[vk::PresentModeKHR]) -> vk::Prese .unwrap_or(vk::PresentModeKHR::FIFO) } +#[rustfmt::skip] fn get_swapchain_extent(window: &Window, capabilities: vk::SurfaceCapabilitiesKHR) -> vk::Extent2D { - if capabilities.current_extent.width != u32::max_value() { + if capabilities.current_extent.width != u32::MAX { capabilities.current_extent } else { - let size = window.inner_size(); - let clamp = |min: u32, max: u32, v: u32| min.max(max.min(v)); vk::Extent2D::builder() - .width(clamp( + .width(window.inner_size().width.clamp( capabilities.min_image_extent.width, capabilities.max_image_extent.width, - size.width, )) - .height(clamp( + .height(window.inner_size().height.clamp( capabilities.min_image_extent.height, capabilities.max_image_extent.height, - size.height, )) .build() } diff --git a/src/10_fixed_functions.rs b/src/10_fixed_functions.rs index 2bcd6e5..50d0a6e 100644 --- a/src/10_fixed_functions.rs +++ b/src/10_fixed_functions.rs @@ -22,7 +22,7 @@ use vulkanalia::window as vk_window; use vulkanalia::Version; use winit::dpi::LogicalSize; use winit::event::{Event, WindowEvent}; -use winit::event_loop::{ControlFlow, EventLoop}; +use winit::event_loop::EventLoop; use winit::window::{Window, WindowBuilder}; use vulkanalia::vk::ExtDebugUtilsExtension; @@ -45,7 +45,7 @@ fn main() -> Result<()> { // Window - let event_loop = EventLoop::new(); + let event_loop = EventLoop::new()?; let window = WindowBuilder::new() .with_title("Vulkan Tutorial (Rust)") .with_inner_size(LogicalSize::new(1024, 768)) @@ -54,21 +54,25 @@ fn main() -> Result<()> { // App let mut app = unsafe { App::create(&window)? }; - let mut destroying = false; - event_loop.run(move |event, _, control_flow| { - *control_flow = ControlFlow::Poll; + event_loop.run(move |event, elwt| { match event { - // Render a frame if our Vulkan app is not being destroyed. - Event::MainEventsCleared if !destroying => unsafe { app.render(&window) }.unwrap(), - // Destroy our Vulkan app. - Event::WindowEvent { event: WindowEvent::CloseRequested, .. } => { - destroying = true; - *control_flow = ControlFlow::Exit; - unsafe { app.destroy(); } + // Request a redraw when all events were processed. + Event::AboutToWait => window.request_redraw(), + Event::WindowEvent { event, .. } => match event { + // Render a frame if our Vulkan app is not being destroyed. + WindowEvent::RedrawRequested if !elwt.exiting() => unsafe { app.render(&window) }.unwrap(), + // Destroy our Vulkan app. + WindowEvent::CloseRequested => { + elwt.exit(); + unsafe { app.destroy(); } + } + _ => {} } _ => {} } - }); + })?; + + Ok(()) } /// Our Vulkan app. @@ -207,7 +211,11 @@ unsafe fn create_instance(window: &Window, entry: &Entry, data: &mut AppData) -> let mut debug_info = vk::DebugUtilsMessengerCreateInfoEXT::builder() .message_severity(vk::DebugUtilsMessageSeverityFlagsEXT::all()) - .message_type(vk::DebugUtilsMessageTypeFlagsEXT::all()) + .message_type( + vk::DebugUtilsMessageTypeFlagsEXT::GENERAL + | vk::DebugUtilsMessageTypeFlagsEXT::VALIDATION + | vk::DebugUtilsMessageTypeFlagsEXT::PERFORMANCE, + ) .user_callback(Some(debug_callback)); if VALIDATION_ENABLED { @@ -436,22 +444,19 @@ fn get_swapchain_present_mode(present_modes: &[vk::PresentModeKHR]) -> vk::Prese .unwrap_or(vk::PresentModeKHR::FIFO) } +#[rustfmt::skip] fn get_swapchain_extent(window: &Window, capabilities: vk::SurfaceCapabilitiesKHR) -> vk::Extent2D { - if capabilities.current_extent.width != u32::max_value() { + if capabilities.current_extent.width != u32::MAX { capabilities.current_extent } else { - let size = window.inner_size(); - let clamp = |min: u32, max: u32, v: u32| min.max(max.min(v)); vk::Extent2D::builder() - .width(clamp( + .width(window.inner_size().width.clamp( capabilities.min_image_extent.width, capabilities.max_image_extent.width, - size.width, )) - .height(clamp( + .height(window.inner_size().height.clamp( capabilities.min_image_extent.height, capabilities.max_image_extent.height, - size.height, )) .build() } diff --git a/src/11_render_passes.rs b/src/11_render_passes.rs index 90f507e..465efcc 100644 --- a/src/11_render_passes.rs +++ b/src/11_render_passes.rs @@ -22,7 +22,7 @@ use vulkanalia::window as vk_window; use vulkanalia::Version; use winit::dpi::LogicalSize; use winit::event::{Event, WindowEvent}; -use winit::event_loop::{ControlFlow, EventLoop}; +use winit::event_loop::EventLoop; use winit::window::{Window, WindowBuilder}; use vulkanalia::vk::ExtDebugUtilsExtension; @@ -45,7 +45,7 @@ fn main() -> Result<()> { // Window - let event_loop = EventLoop::new(); + let event_loop = EventLoop::new()?; let window = WindowBuilder::new() .with_title("Vulkan Tutorial (Rust)") .with_inner_size(LogicalSize::new(1024, 768)) @@ -54,21 +54,25 @@ fn main() -> Result<()> { // App let mut app = unsafe { App::create(&window)? }; - let mut destroying = false; - event_loop.run(move |event, _, control_flow| { - *control_flow = ControlFlow::Poll; + event_loop.run(move |event, elwt| { match event { - // Render a frame if our Vulkan app is not being destroyed. - Event::MainEventsCleared if !destroying => unsafe { app.render(&window) }.unwrap(), - // Destroy our Vulkan app. - Event::WindowEvent { event: WindowEvent::CloseRequested, .. } => { - destroying = true; - *control_flow = ControlFlow::Exit; - unsafe { app.destroy(); } + // Request a redraw when all events were processed. + Event::AboutToWait => window.request_redraw(), + Event::WindowEvent { event, .. } => match event { + // Render a frame if our Vulkan app is not being destroyed. + WindowEvent::RedrawRequested if !elwt.exiting() => unsafe { app.render(&window) }.unwrap(), + // Destroy our Vulkan app. + WindowEvent::CloseRequested => { + elwt.exit(); + unsafe { app.destroy(); } + } + _ => {} } _ => {} } - }); + })?; + + Ok(()) } /// Our Vulkan app. @@ -210,7 +214,11 @@ unsafe fn create_instance(window: &Window, entry: &Entry, data: &mut AppData) -> let mut debug_info = vk::DebugUtilsMessengerCreateInfoEXT::builder() .message_severity(vk::DebugUtilsMessageSeverityFlagsEXT::all()) - .message_type(vk::DebugUtilsMessageTypeFlagsEXT::all()) + .message_type( + vk::DebugUtilsMessageTypeFlagsEXT::GENERAL + | vk::DebugUtilsMessageTypeFlagsEXT::VALIDATION + | vk::DebugUtilsMessageTypeFlagsEXT::PERFORMANCE, + ) .user_callback(Some(debug_callback)); if VALIDATION_ENABLED { @@ -439,22 +447,19 @@ fn get_swapchain_present_mode(present_modes: &[vk::PresentModeKHR]) -> vk::Prese .unwrap_or(vk::PresentModeKHR::FIFO) } +#[rustfmt::skip] fn get_swapchain_extent(window: &Window, capabilities: vk::SurfaceCapabilitiesKHR) -> vk::Extent2D { - if capabilities.current_extent.width != u32::max_value() { + if capabilities.current_extent.width != u32::MAX { capabilities.current_extent } else { - let size = window.inner_size(); - let clamp = |min: u32, max: u32, v: u32| min.max(max.min(v)); vk::Extent2D::builder() - .width(clamp( + .width(window.inner_size().width.clamp( capabilities.min_image_extent.width, capabilities.max_image_extent.width, - size.width, )) - .height(clamp( + .height(window.inner_size().height.clamp( capabilities.min_image_extent.height, capabilities.max_image_extent.height, - size.height, )) .build() } diff --git a/src/12_graphics_pipeline_complete.rs b/src/12_graphics_pipeline_complete.rs index 28722e4..90e3a69 100644 --- a/src/12_graphics_pipeline_complete.rs +++ b/src/12_graphics_pipeline_complete.rs @@ -22,7 +22,7 @@ use vulkanalia::window as vk_window; use vulkanalia::Version; use winit::dpi::LogicalSize; use winit::event::{Event, WindowEvent}; -use winit::event_loop::{ControlFlow, EventLoop}; +use winit::event_loop::EventLoop; use winit::window::{Window, WindowBuilder}; use vulkanalia::vk::ExtDebugUtilsExtension; @@ -45,7 +45,7 @@ fn main() -> Result<()> { // Window - let event_loop = EventLoop::new(); + let event_loop = EventLoop::new()?; let window = WindowBuilder::new() .with_title("Vulkan Tutorial (Rust)") .with_inner_size(LogicalSize::new(1024, 768)) @@ -54,21 +54,25 @@ fn main() -> Result<()> { // App let mut app = unsafe { App::create(&window)? }; - let mut destroying = false; - event_loop.run(move |event, _, control_flow| { - *control_flow = ControlFlow::Poll; + event_loop.run(move |event, elwt| { match event { - // Render a frame if our Vulkan app is not being destroyed. - Event::MainEventsCleared if !destroying => unsafe { app.render(&window) }.unwrap(), - // Destroy our Vulkan app. - Event::WindowEvent { event: WindowEvent::CloseRequested, .. } => { - destroying = true; - *control_flow = ControlFlow::Exit; - unsafe { app.destroy(); } + // Request a redraw when all events were processed. + Event::AboutToWait => window.request_redraw(), + Event::WindowEvent { event, .. } => match event { + // Render a frame if our Vulkan app is not being destroyed. + WindowEvent::RedrawRequested if !elwt.exiting() => unsafe { app.render(&window) }.unwrap(), + // Destroy our Vulkan app. + WindowEvent::CloseRequested => { + elwt.exit(); + unsafe { app.destroy(); } + } + _ => {} } _ => {} } - }); + })?; + + Ok(()) } /// Our Vulkan app. @@ -212,7 +216,11 @@ unsafe fn create_instance(window: &Window, entry: &Entry, data: &mut AppData) -> let mut debug_info = vk::DebugUtilsMessengerCreateInfoEXT::builder() .message_severity(vk::DebugUtilsMessageSeverityFlagsEXT::all()) - .message_type(vk::DebugUtilsMessageTypeFlagsEXT::all()) + .message_type( + vk::DebugUtilsMessageTypeFlagsEXT::GENERAL + | vk::DebugUtilsMessageTypeFlagsEXT::VALIDATION + | vk::DebugUtilsMessageTypeFlagsEXT::PERFORMANCE, + ) .user_callback(Some(debug_callback)); if VALIDATION_ENABLED { @@ -441,22 +449,19 @@ fn get_swapchain_present_mode(present_modes: &[vk::PresentModeKHR]) -> vk::Prese .unwrap_or(vk::PresentModeKHR::FIFO) } +#[rustfmt::skip] fn get_swapchain_extent(window: &Window, capabilities: vk::SurfaceCapabilitiesKHR) -> vk::Extent2D { - if capabilities.current_extent.width != u32::max_value() { + if capabilities.current_extent.width != u32::MAX { capabilities.current_extent } else { - let size = window.inner_size(); - let clamp = |min: u32, max: u32, v: u32| min.max(max.min(v)); vk::Extent2D::builder() - .width(clamp( + .width(window.inner_size().width.clamp( capabilities.min_image_extent.width, capabilities.max_image_extent.width, - size.width, )) - .height(clamp( + .height(window.inner_size().height.clamp( capabilities.min_image_extent.height, capabilities.max_image_extent.height, - size.height, )) .build() } diff --git a/src/13_framebuffers.rs b/src/13_framebuffers.rs index 8e9aa81..0457e03 100644 --- a/src/13_framebuffers.rs +++ b/src/13_framebuffers.rs @@ -22,7 +22,7 @@ use vulkanalia::window as vk_window; use vulkanalia::Version; use winit::dpi::LogicalSize; use winit::event::{Event, WindowEvent}; -use winit::event_loop::{ControlFlow, EventLoop}; +use winit::event_loop::EventLoop; use winit::window::{Window, WindowBuilder}; use vulkanalia::vk::ExtDebugUtilsExtension; @@ -45,7 +45,7 @@ fn main() -> Result<()> { // Window - let event_loop = EventLoop::new(); + let event_loop = EventLoop::new()?; let window = WindowBuilder::new() .with_title("Vulkan Tutorial (Rust)") .with_inner_size(LogicalSize::new(1024, 768)) @@ -54,21 +54,25 @@ fn main() -> Result<()> { // App let mut app = unsafe { App::create(&window)? }; - let mut destroying = false; - event_loop.run(move |event, _, control_flow| { - *control_flow = ControlFlow::Poll; + event_loop.run(move |event, elwt| { match event { - // Render a frame if our Vulkan app is not being destroyed. - Event::MainEventsCleared if !destroying => unsafe { app.render(&window) }.unwrap(), - // Destroy our Vulkan app. - Event::WindowEvent { event: WindowEvent::CloseRequested, .. } => { - destroying = true; - *control_flow = ControlFlow::Exit; - unsafe { app.destroy(); } + // Request a redraw when all events were processed. + Event::AboutToWait => window.request_redraw(), + Event::WindowEvent { event, .. } => match event { + // Render a frame if our Vulkan app is not being destroyed. + WindowEvent::RedrawRequested if !elwt.exiting() => unsafe { app.render(&window) }.unwrap(), + // Destroy our Vulkan app. + WindowEvent::CloseRequested => { + elwt.exit(); + unsafe { app.destroy(); } + } + _ => {} } _ => {} } - }); + })?; + + Ok(()) } /// Our Vulkan app. @@ -216,7 +220,11 @@ unsafe fn create_instance(window: &Window, entry: &Entry, data: &mut AppData) -> let mut debug_info = vk::DebugUtilsMessengerCreateInfoEXT::builder() .message_severity(vk::DebugUtilsMessageSeverityFlagsEXT::all()) - .message_type(vk::DebugUtilsMessageTypeFlagsEXT::all()) + .message_type( + vk::DebugUtilsMessageTypeFlagsEXT::GENERAL + | vk::DebugUtilsMessageTypeFlagsEXT::VALIDATION + | vk::DebugUtilsMessageTypeFlagsEXT::PERFORMANCE, + ) .user_callback(Some(debug_callback)); if VALIDATION_ENABLED { @@ -445,22 +453,19 @@ fn get_swapchain_present_mode(present_modes: &[vk::PresentModeKHR]) -> vk::Prese .unwrap_or(vk::PresentModeKHR::FIFO) } +#[rustfmt::skip] fn get_swapchain_extent(window: &Window, capabilities: vk::SurfaceCapabilitiesKHR) -> vk::Extent2D { - if capabilities.current_extent.width != u32::max_value() { + if capabilities.current_extent.width != u32::MAX { capabilities.current_extent } else { - let size = window.inner_size(); - let clamp = |min: u32, max: u32, v: u32| min.max(max.min(v)); vk::Extent2D::builder() - .width(clamp( + .width(window.inner_size().width.clamp( capabilities.min_image_extent.width, capabilities.max_image_extent.width, - size.width, )) - .height(clamp( + .height(window.inner_size().height.clamp( capabilities.min_image_extent.height, capabilities.max_image_extent.height, - size.height, )) .build() } diff --git a/src/14_command_buffers.rs b/src/14_command_buffers.rs index 513d667..1bf750a 100644 --- a/src/14_command_buffers.rs +++ b/src/14_command_buffers.rs @@ -22,7 +22,7 @@ use vulkanalia::window as vk_window; use vulkanalia::Version; use winit::dpi::LogicalSize; use winit::event::{Event, WindowEvent}; -use winit::event_loop::{ControlFlow, EventLoop}; +use winit::event_loop::EventLoop; use winit::window::{Window, WindowBuilder}; use vulkanalia::vk::ExtDebugUtilsExtension; @@ -45,7 +45,7 @@ fn main() -> Result<()> { // Window - let event_loop = EventLoop::new(); + let event_loop = EventLoop::new()?; let window = WindowBuilder::new() .with_title("Vulkan Tutorial (Rust)") .with_inner_size(LogicalSize::new(1024, 768)) @@ -54,21 +54,25 @@ fn main() -> Result<()> { // App let mut app = unsafe { App::create(&window)? }; - let mut destroying = false; - event_loop.run(move |event, _, control_flow| { - *control_flow = ControlFlow::Poll; + event_loop.run(move |event, elwt| { match event { - // Render a frame if our Vulkan app is not being destroyed. - Event::MainEventsCleared if !destroying => unsafe { app.render(&window) }.unwrap(), - // Destroy our Vulkan app. - Event::WindowEvent { event: WindowEvent::CloseRequested, .. } => { - destroying = true; - *control_flow = ControlFlow::Exit; - unsafe { app.destroy(); } + // Request a redraw when all events were processed. + Event::AboutToWait => window.request_redraw(), + Event::WindowEvent { event, .. } => match event { + // Render a frame if our Vulkan app is not being destroyed. + WindowEvent::RedrawRequested if !elwt.exiting() => unsafe { app.render(&window) }.unwrap(), + // Destroy our Vulkan app. + WindowEvent::CloseRequested => { + elwt.exit(); + unsafe { app.destroy(); } + } + _ => {} } _ => {} } - }); + })?; + + Ok(()) } /// Our Vulkan app. @@ -223,7 +227,11 @@ unsafe fn create_instance(window: &Window, entry: &Entry, data: &mut AppData) -> let mut debug_info = vk::DebugUtilsMessengerCreateInfoEXT::builder() .message_severity(vk::DebugUtilsMessageSeverityFlagsEXT::all()) - .message_type(vk::DebugUtilsMessageTypeFlagsEXT::all()) + .message_type( + vk::DebugUtilsMessageTypeFlagsEXT::GENERAL + | vk::DebugUtilsMessageTypeFlagsEXT::VALIDATION + | vk::DebugUtilsMessageTypeFlagsEXT::PERFORMANCE, + ) .user_callback(Some(debug_callback)); if VALIDATION_ENABLED { @@ -452,22 +460,19 @@ fn get_swapchain_present_mode(present_modes: &[vk::PresentModeKHR]) -> vk::Prese .unwrap_or(vk::PresentModeKHR::FIFO) } +#[rustfmt::skip] fn get_swapchain_extent(window: &Window, capabilities: vk::SurfaceCapabilitiesKHR) -> vk::Extent2D { - if capabilities.current_extent.width != u32::max_value() { + if capabilities.current_extent.width != u32::MAX { capabilities.current_extent } else { - let size = window.inner_size(); - let clamp = |min: u32, max: u32, v: u32| min.max(max.min(v)); vk::Extent2D::builder() - .width(clamp( + .width(window.inner_size().width.clamp( capabilities.min_image_extent.width, capabilities.max_image_extent.width, - size.width, )) - .height(clamp( + .height(window.inner_size().height.clamp( capabilities.min_image_extent.height, capabilities.max_image_extent.height, - size.height, )) .build() } diff --git a/src/15_hello_triangle.rs b/src/15_hello_triangle.rs index b385cf8..5ed0701 100644 --- a/src/15_hello_triangle.rs +++ b/src/15_hello_triangle.rs @@ -22,7 +22,7 @@ use vulkanalia::window as vk_window; use vulkanalia::Version; use winit::dpi::LogicalSize; use winit::event::{Event, WindowEvent}; -use winit::event_loop::{ControlFlow, EventLoop}; +use winit::event_loop::EventLoop; use winit::window::{Window, WindowBuilder}; use vulkanalia::vk::ExtDebugUtilsExtension; @@ -48,7 +48,7 @@ fn main() -> Result<()> { // Window - let event_loop = EventLoop::new(); + let event_loop = EventLoop::new()?; let window = WindowBuilder::new() .with_title("Vulkan Tutorial (Rust)") .with_inner_size(LogicalSize::new(1024, 768)) @@ -57,21 +57,25 @@ fn main() -> Result<()> { // App let mut app = unsafe { App::create(&window)? }; - let mut destroying = false; - event_loop.run(move |event, _, control_flow| { - *control_flow = ControlFlow::Poll; + event_loop.run(move |event, elwt| { match event { - // Render a frame if our Vulkan app is not being destroyed. - Event::MainEventsCleared if !destroying => unsafe { app.render(&window) }.unwrap(), - // Destroy our Vulkan app. - Event::WindowEvent { event: WindowEvent::CloseRequested, .. } => { - destroying = true; - *control_flow = ControlFlow::Exit; - unsafe { app.destroy(); } + // Request a redraw when all events were processed. + Event::AboutToWait => window.request_redraw(), + Event::WindowEvent { event, .. } => match event { + // Render a frame if our Vulkan app is not being destroyed. + WindowEvent::RedrawRequested if !elwt.exiting() => unsafe { app.render(&window) }.unwrap(), + // Destroy our Vulkan app. + WindowEvent::CloseRequested => { + elwt.exit(); + unsafe { app.destroy(); } + } + _ => {} } _ => {} } - }); + })?; + + Ok(()) } /// Our Vulkan app. @@ -115,14 +119,13 @@ impl App { unsafe fn render(&mut self, window: &Window) -> Result<()> { let in_flight_fence = self.data.in_flight_fences[self.frame]; - self.device - .wait_for_fences(&[in_flight_fence], true, u64::max_value())?; + self.device.wait_for_fences(&[in_flight_fence], true, u64::MAX)?; let image_index = self .device .acquire_next_image_khr( self.data.swapchain, - u64::max_value(), + u64::MAX, self.data.image_available_semaphores[self.frame], vk::Fence::null(), )? @@ -130,8 +133,7 @@ impl App { let image_in_flight = self.data.images_in_flight[image_index]; if !image_in_flight.is_null() { - self.device - .wait_for_fences(&[image_in_flight], true, u64::max_value())?; + self.device.wait_for_fences(&[image_in_flight], true, u64::MAX)?; } self.data.images_in_flight[image_index] = in_flight_fence; @@ -288,7 +290,11 @@ unsafe fn create_instance(window: &Window, entry: &Entry, data: &mut AppData) -> let mut debug_info = vk::DebugUtilsMessengerCreateInfoEXT::builder() .message_severity(vk::DebugUtilsMessageSeverityFlagsEXT::all()) - .message_type(vk::DebugUtilsMessageTypeFlagsEXT::all()) + .message_type( + vk::DebugUtilsMessageTypeFlagsEXT::GENERAL + | vk::DebugUtilsMessageTypeFlagsEXT::VALIDATION + | vk::DebugUtilsMessageTypeFlagsEXT::PERFORMANCE, + ) .user_callback(Some(debug_callback)); if VALIDATION_ENABLED { @@ -517,22 +523,19 @@ fn get_swapchain_present_mode(present_modes: &[vk::PresentModeKHR]) -> vk::Prese .unwrap_or(vk::PresentModeKHR::FIFO) } +#[rustfmt::skip] fn get_swapchain_extent(window: &Window, capabilities: vk::SurfaceCapabilitiesKHR) -> vk::Extent2D { - if capabilities.current_extent.width != u32::max_value() { + if capabilities.current_extent.width != u32::MAX { capabilities.current_extent } else { - let size = window.inner_size(); - let clamp = |min: u32, max: u32, v: u32| min.max(max.min(v)); vk::Extent2D::builder() - .width(clamp( + .width(window.inner_size().width.clamp( capabilities.min_image_extent.width, capabilities.max_image_extent.width, - size.width, )) - .height(clamp( + .height(window.inner_size().height.clamp( capabilities.min_image_extent.height, capabilities.max_image_extent.height, - size.height, )) .build() } diff --git a/src/16_swapchain_recreation.rs b/src/16_swapchain_recreation.rs index 2280d6a..6e652c6 100644 --- a/src/16_swapchain_recreation.rs +++ b/src/16_swapchain_recreation.rs @@ -22,7 +22,7 @@ use vulkanalia::window as vk_window; use vulkanalia::Version; use winit::dpi::LogicalSize; use winit::event::{Event, WindowEvent}; -use winit::event_loop::{ControlFlow, EventLoop}; +use winit::event_loop::EventLoop; use winit::window::{Window, WindowBuilder}; use vulkanalia::vk::ExtDebugUtilsExtension; @@ -48,7 +48,7 @@ fn main() -> Result<()> { // Window - let event_loop = EventLoop::new(); + let event_loop = EventLoop::new()?; let window = WindowBuilder::new() .with_title("Vulkan Tutorial (Rust)") .with_inner_size(LogicalSize::new(1024, 768)) @@ -57,31 +57,37 @@ fn main() -> Result<()> { // App let mut app = unsafe { App::create(&window)? }; - let mut destroying = false; let mut minimized = false; - event_loop.run(move |event, _, control_flow| { - *control_flow = ControlFlow::Poll; + event_loop.run(move |event, elwt| { match event { - // Render a frame if our Vulkan app is not being destroyed. - Event::MainEventsCleared if !destroying && !minimized => unsafe { app.render(&window) }.unwrap(), - // Mark the window as having been resized. - Event::WindowEvent { event: WindowEvent::Resized(size), .. } => { - if size.width == 0 || size.height == 0 { - minimized = true; - } else { - minimized = false; - app.resized = true; + // Request a redraw when all events were processed. + Event::AboutToWait => window.request_redraw(), + Event::WindowEvent { event, .. } => match event { + // Render a frame if our Vulkan app is not being destroyed. + WindowEvent::RedrawRequested if !elwt.exiting() && !minimized => { + unsafe { app.render(&window) }.unwrap(); + }, + // Mark the window as having been resized. + WindowEvent::Resized(size) => { + if size.width == 0 || size.height == 0 { + minimized = true; + } else { + minimized = false; + app.resized = true; + } } - } - // Destroy our Vulkan app. - Event::WindowEvent { event: WindowEvent::CloseRequested, .. } => { - destroying = true; - *control_flow = ControlFlow::Exit; - unsafe { app.destroy(); } + // Destroy our Vulkan app. + WindowEvent::CloseRequested => { + elwt.exit(); + unsafe { app.destroy(); } + } + _ => {} } _ => {} } - }); + })?; + + Ok(()) } /// Our Vulkan app. @@ -127,12 +133,11 @@ impl App { unsafe fn render(&mut self, window: &Window) -> Result<()> { let in_flight_fence = self.data.in_flight_fences[self.frame]; - self.device - .wait_for_fences(&[in_flight_fence], true, u64::max_value())?; + self.device.wait_for_fences(&[in_flight_fence], true, u64::MAX)?; let result = self.device.acquire_next_image_khr( self.data.swapchain, - u64::max_value(), + u64::MAX, self.data.image_available_semaphores[self.frame], vk::Fence::null(), ); @@ -145,8 +150,7 @@ impl App { let image_in_flight = self.data.images_in_flight[image_index]; if !image_in_flight.is_null() { - self.device - .wait_for_fences(&[image_in_flight], true, u64::max_value())?; + self.device.wait_for_fences(&[image_in_flight], true, u64::MAX)?; } self.data.images_in_flight[image_index] = in_flight_fence; @@ -333,7 +337,11 @@ unsafe fn create_instance(window: &Window, entry: &Entry, data: &mut AppData) -> let mut debug_info = vk::DebugUtilsMessengerCreateInfoEXT::builder() .message_severity(vk::DebugUtilsMessageSeverityFlagsEXT::all()) - .message_type(vk::DebugUtilsMessageTypeFlagsEXT::all()) + .message_type( + vk::DebugUtilsMessageTypeFlagsEXT::GENERAL + | vk::DebugUtilsMessageTypeFlagsEXT::VALIDATION + | vk::DebugUtilsMessageTypeFlagsEXT::PERFORMANCE, + ) .user_callback(Some(debug_callback)); if VALIDATION_ENABLED { @@ -562,22 +570,19 @@ fn get_swapchain_present_mode(present_modes: &[vk::PresentModeKHR]) -> vk::Prese .unwrap_or(vk::PresentModeKHR::FIFO) } +#[rustfmt::skip] fn get_swapchain_extent(window: &Window, capabilities: vk::SurfaceCapabilitiesKHR) -> vk::Extent2D { - if capabilities.current_extent.width != u32::max_value() { + if capabilities.current_extent.width != u32::MAX { capabilities.current_extent } else { - let size = window.inner_size(); - let clamp = |min: u32, max: u32, v: u32| min.max(max.min(v)); vk::Extent2D::builder() - .width(clamp( + .width(window.inner_size().width.clamp( capabilities.min_image_extent.width, capabilities.max_image_extent.width, - size.width, )) - .height(clamp( + .height(window.inner_size().height.clamp( capabilities.min_image_extent.height, capabilities.max_image_extent.height, - size.height, )) .build() } diff --git a/src/17_vertex_input.rs b/src/17_vertex_input.rs index 3047600..374f668 100644 --- a/src/17_vertex_input.rs +++ b/src/17_vertex_input.rs @@ -24,7 +24,7 @@ use vulkanalia::window as vk_window; use vulkanalia::Version; use winit::dpi::LogicalSize; use winit::event::{Event, WindowEvent}; -use winit::event_loop::{ControlFlow, EventLoop}; +use winit::event_loop::EventLoop; use winit::window::{Window, WindowBuilder}; use vulkanalia::vk::ExtDebugUtilsExtension; @@ -60,7 +60,7 @@ fn main() -> Result<()> { // Window - let event_loop = EventLoop::new(); + let event_loop = EventLoop::new()?; let window = WindowBuilder::new() .with_title("Vulkan Tutorial (Rust)") .with_inner_size(LogicalSize::new(1024, 768)) @@ -69,31 +69,37 @@ fn main() -> Result<()> { // App let mut app = unsafe { App::create(&window)? }; - let mut destroying = false; let mut minimized = false; - event_loop.run(move |event, _, control_flow| { - *control_flow = ControlFlow::Poll; + event_loop.run(move |event, elwt| { match event { - // Render a frame if our Vulkan app is not being destroyed. - Event::MainEventsCleared if !destroying && !minimized => unsafe { app.render(&window) }.unwrap(), - // Mark the window as having been resized. - Event::WindowEvent { event: WindowEvent::Resized(size), .. } => { - if size.width == 0 || size.height == 0 { - minimized = true; - } else { - minimized = false; - app.resized = true; + // Request a redraw when all events were processed. + Event::AboutToWait => window.request_redraw(), + Event::WindowEvent { event, .. } => match event { + // Render a frame if our Vulkan app is not being destroyed. + WindowEvent::RedrawRequested if !elwt.exiting() && !minimized => { + unsafe { app.render(&window) }.unwrap(); + }, + // Mark the window as having been resized. + WindowEvent::Resized(size) => { + if size.width == 0 || size.height == 0 { + minimized = true; + } else { + minimized = false; + app.resized = true; + } } - } - // Destroy our Vulkan app. - Event::WindowEvent { event: WindowEvent::CloseRequested, .. } => { - destroying = true; - *control_flow = ControlFlow::Exit; - unsafe { app.destroy(); } + // Destroy our Vulkan app. + WindowEvent::CloseRequested => { + elwt.exit(); + unsafe { app.destroy(); } + } + _ => {} } _ => {} } - }); + })?; + + Ok(()) } /// Our Vulkan app. @@ -139,12 +145,11 @@ impl App { unsafe fn render(&mut self, window: &Window) -> Result<()> { let in_flight_fence = self.data.in_flight_fences[self.frame]; - self.device - .wait_for_fences(&[in_flight_fence], true, u64::max_value())?; + self.device.wait_for_fences(&[in_flight_fence], true, u64::MAX)?; let result = self.device.acquire_next_image_khr( self.data.swapchain, - u64::max_value(), + u64::MAX, self.data.image_available_semaphores[self.frame], vk::Fence::null(), ); @@ -157,8 +162,7 @@ impl App { let image_in_flight = self.data.images_in_flight[image_index]; if !image_in_flight.is_null() { - self.device - .wait_for_fences(&[image_in_flight], true, u64::max_value())?; + self.device.wait_for_fences(&[image_in_flight], true, u64::MAX)?; } self.data.images_in_flight[image_index] = in_flight_fence; @@ -345,7 +349,11 @@ unsafe fn create_instance(window: &Window, entry: &Entry, data: &mut AppData) -> let mut debug_info = vk::DebugUtilsMessengerCreateInfoEXT::builder() .message_severity(vk::DebugUtilsMessageSeverityFlagsEXT::all()) - .message_type(vk::DebugUtilsMessageTypeFlagsEXT::all()) + .message_type( + vk::DebugUtilsMessageTypeFlagsEXT::GENERAL + | vk::DebugUtilsMessageTypeFlagsEXT::VALIDATION + | vk::DebugUtilsMessageTypeFlagsEXT::PERFORMANCE, + ) .user_callback(Some(debug_callback)); if VALIDATION_ENABLED { @@ -574,22 +582,19 @@ fn get_swapchain_present_mode(present_modes: &[vk::PresentModeKHR]) -> vk::Prese .unwrap_or(vk::PresentModeKHR::FIFO) } +#[rustfmt::skip] fn get_swapchain_extent(window: &Window, capabilities: vk::SurfaceCapabilitiesKHR) -> vk::Extent2D { - if capabilities.current_extent.width != u32::max_value() { + if capabilities.current_extent.width != u32::MAX { capabilities.current_extent } else { - let size = window.inner_size(); - let clamp = |min: u32, max: u32, v: u32| min.max(max.min(v)); vk::Extent2D::builder() - .width(clamp( + .width(window.inner_size().width.clamp( capabilities.min_image_extent.width, capabilities.max_image_extent.width, - size.width, )) - .height(clamp( + .height(window.inner_size().height.clamp( capabilities.min_image_extent.height, capabilities.max_image_extent.height, - size.height, )) .build() } diff --git a/src/18_vertex_buffer.rs b/src/18_vertex_buffer.rs index 5e769af..b6edb53 100644 --- a/src/18_vertex_buffer.rs +++ b/src/18_vertex_buffer.rs @@ -25,7 +25,7 @@ use vulkanalia::window as vk_window; use vulkanalia::Version; use winit::dpi::LogicalSize; use winit::event::{Event, WindowEvent}; -use winit::event_loop::{ControlFlow, EventLoop}; +use winit::event_loop::EventLoop; use winit::window::{Window, WindowBuilder}; use vulkanalia::vk::ExtDebugUtilsExtension; @@ -61,7 +61,7 @@ fn main() -> Result<()> { // Window - let event_loop = EventLoop::new(); + let event_loop = EventLoop::new()?; let window = WindowBuilder::new() .with_title("Vulkan Tutorial (Rust)") .with_inner_size(LogicalSize::new(1024, 768)) @@ -70,31 +70,37 @@ fn main() -> Result<()> { // App let mut app = unsafe { App::create(&window)? }; - let mut destroying = false; let mut minimized = false; - event_loop.run(move |event, _, control_flow| { - *control_flow = ControlFlow::Poll; + event_loop.run(move |event, elwt| { match event { - // Render a frame if our Vulkan app is not being destroyed. - Event::MainEventsCleared if !destroying && !minimized => unsafe { app.render(&window) }.unwrap(), - // Mark the window as having been resized. - Event::WindowEvent { event: WindowEvent::Resized(size), .. } => { - if size.width == 0 || size.height == 0 { - minimized = true; - } else { - minimized = false; - app.resized = true; + // Request a redraw when all events were processed. + Event::AboutToWait => window.request_redraw(), + Event::WindowEvent { event, .. } => match event { + // Render a frame if our Vulkan app is not being destroyed. + WindowEvent::RedrawRequested if !elwt.exiting() && !minimized => { + unsafe { app.render(&window) }.unwrap(); + }, + // Mark the window as having been resized. + WindowEvent::Resized(size) => { + if size.width == 0 || size.height == 0 { + minimized = true; + } else { + minimized = false; + app.resized = true; + } } - } - // Destroy our Vulkan app. - Event::WindowEvent { event: WindowEvent::CloseRequested, .. } => { - destroying = true; - *control_flow = ControlFlow::Exit; - unsafe { app.destroy(); } + // Destroy our Vulkan app. + WindowEvent::CloseRequested => { + elwt.exit(); + unsafe { app.destroy(); } + } + _ => {} } _ => {} } - }); + })?; + + Ok(()) } /// Our Vulkan app. @@ -141,12 +147,11 @@ impl App { unsafe fn render(&mut self, window: &Window) -> Result<()> { let in_flight_fence = self.data.in_flight_fences[self.frame]; - self.device - .wait_for_fences(&[in_flight_fence], true, u64::max_value())?; + self.device.wait_for_fences(&[in_flight_fence], true, u64::MAX)?; let result = self.device.acquire_next_image_khr( self.data.swapchain, - u64::max_value(), + u64::MAX, self.data.image_available_semaphores[self.frame], vk::Fence::null(), ); @@ -159,8 +164,7 @@ impl App { let image_in_flight = self.data.images_in_flight[image_index]; if !image_in_flight.is_null() { - self.device - .wait_for_fences(&[image_in_flight], true, u64::max_value())?; + self.device.wait_for_fences(&[image_in_flight], true, u64::MAX)?; } self.data.images_in_flight[image_index] = in_flight_fence; @@ -352,7 +356,11 @@ unsafe fn create_instance(window: &Window, entry: &Entry, data: &mut AppData) -> let mut debug_info = vk::DebugUtilsMessengerCreateInfoEXT::builder() .message_severity(vk::DebugUtilsMessageSeverityFlagsEXT::all()) - .message_type(vk::DebugUtilsMessageTypeFlagsEXT::all()) + .message_type( + vk::DebugUtilsMessageTypeFlagsEXT::GENERAL + | vk::DebugUtilsMessageTypeFlagsEXT::VALIDATION + | vk::DebugUtilsMessageTypeFlagsEXT::PERFORMANCE, + ) .user_callback(Some(debug_callback)); if VALIDATION_ENABLED { @@ -581,22 +589,19 @@ fn get_swapchain_present_mode(present_modes: &[vk::PresentModeKHR]) -> vk::Prese .unwrap_or(vk::PresentModeKHR::FIFO) } +#[rustfmt::skip] fn get_swapchain_extent(window: &Window, capabilities: vk::SurfaceCapabilitiesKHR) -> vk::Extent2D { - if capabilities.current_extent.width != u32::max_value() { + if capabilities.current_extent.width != u32::MAX { capabilities.current_extent } else { - let size = window.inner_size(); - let clamp = |min: u32, max: u32, v: u32| min.max(max.min(v)); vk::Extent2D::builder() - .width(clamp( + .width(window.inner_size().width.clamp( capabilities.min_image_extent.width, capabilities.max_image_extent.width, - size.width, )) - .height(clamp( + .height(window.inner_size().height.clamp( capabilities.min_image_extent.height, capabilities.max_image_extent.height, - size.height, )) .build() } diff --git a/src/19_staging_buffer.rs b/src/19_staging_buffer.rs index e4cc814..30c0bbb 100644 --- a/src/19_staging_buffer.rs +++ b/src/19_staging_buffer.rs @@ -25,7 +25,7 @@ use vulkanalia::window as vk_window; use vulkanalia::Version; use winit::dpi::LogicalSize; use winit::event::{Event, WindowEvent}; -use winit::event_loop::{ControlFlow, EventLoop}; +use winit::event_loop::EventLoop; use winit::window::{Window, WindowBuilder}; use vulkanalia::vk::ExtDebugUtilsExtension; @@ -61,7 +61,7 @@ fn main() -> Result<()> { // Window - let event_loop = EventLoop::new(); + let event_loop = EventLoop::new()?; let window = WindowBuilder::new() .with_title("Vulkan Tutorial (Rust)") .with_inner_size(LogicalSize::new(1024, 768)) @@ -70,31 +70,37 @@ fn main() -> Result<()> { // App let mut app = unsafe { App::create(&window)? }; - let mut destroying = false; let mut minimized = false; - event_loop.run(move |event, _, control_flow| { - *control_flow = ControlFlow::Poll; + event_loop.run(move |event, elwt| { match event { - // Render a frame if our Vulkan app is not being destroyed. - Event::MainEventsCleared if !destroying && !minimized => unsafe { app.render(&window) }.unwrap(), - // Mark the window as having been resized. - Event::WindowEvent { event: WindowEvent::Resized(size), .. } => { - if size.width == 0 || size.height == 0 { - minimized = true; - } else { - minimized = false; - app.resized = true; + // Request a redraw when all events were processed. + Event::AboutToWait => window.request_redraw(), + Event::WindowEvent { event, .. } => match event { + // Render a frame if our Vulkan app is not being destroyed. + WindowEvent::RedrawRequested if !elwt.exiting() && !minimized => { + unsafe { app.render(&window) }.unwrap(); + }, + // Mark the window as having been resized. + WindowEvent::Resized(size) => { + if size.width == 0 || size.height == 0 { + minimized = true; + } else { + minimized = false; + app.resized = true; + } } - } - // Destroy our Vulkan app. - Event::WindowEvent { event: WindowEvent::CloseRequested, .. } => { - destroying = true; - *control_flow = ControlFlow::Exit; - unsafe { app.destroy(); } + // Destroy our Vulkan app. + WindowEvent::CloseRequested => { + elwt.exit(); + unsafe { app.destroy(); } + } + _ => {} } _ => {} } - }); + })?; + + Ok(()) } /// Our Vulkan app. @@ -141,12 +147,11 @@ impl App { unsafe fn render(&mut self, window: &Window) -> Result<()> { let in_flight_fence = self.data.in_flight_fences[self.frame]; - self.device - .wait_for_fences(&[in_flight_fence], true, u64::max_value())?; + self.device.wait_for_fences(&[in_flight_fence], true, u64::MAX)?; let result = self.device.acquire_next_image_khr( self.data.swapchain, - u64::max_value(), + u64::MAX, self.data.image_available_semaphores[self.frame], vk::Fence::null(), ); @@ -159,8 +164,7 @@ impl App { let image_in_flight = self.data.images_in_flight[image_index]; if !image_in_flight.is_null() { - self.device - .wait_for_fences(&[image_in_flight], true, u64::max_value())?; + self.device.wait_for_fences(&[image_in_flight], true, u64::MAX)?; } self.data.images_in_flight[image_index] = in_flight_fence; @@ -352,7 +356,11 @@ unsafe fn create_instance(window: &Window, entry: &Entry, data: &mut AppData) -> let mut debug_info = vk::DebugUtilsMessengerCreateInfoEXT::builder() .message_severity(vk::DebugUtilsMessageSeverityFlagsEXT::all()) - .message_type(vk::DebugUtilsMessageTypeFlagsEXT::all()) + .message_type( + vk::DebugUtilsMessageTypeFlagsEXT::GENERAL + | vk::DebugUtilsMessageTypeFlagsEXT::VALIDATION + | vk::DebugUtilsMessageTypeFlagsEXT::PERFORMANCE, + ) .user_callback(Some(debug_callback)); if VALIDATION_ENABLED { @@ -581,22 +589,19 @@ fn get_swapchain_present_mode(present_modes: &[vk::PresentModeKHR]) -> vk::Prese .unwrap_or(vk::PresentModeKHR::FIFO) } +#[rustfmt::skip] fn get_swapchain_extent(window: &Window, capabilities: vk::SurfaceCapabilitiesKHR) -> vk::Extent2D { - if capabilities.current_extent.width != u32::max_value() { + if capabilities.current_extent.width != u32::MAX { capabilities.current_extent } else { - let size = window.inner_size(); - let clamp = |min: u32, max: u32, v: u32| min.max(max.min(v)); vk::Extent2D::builder() - .width(clamp( + .width(window.inner_size().width.clamp( capabilities.min_image_extent.width, capabilities.max_image_extent.width, - size.width, )) - .height(clamp( + .height(window.inner_size().height.clamp( capabilities.min_image_extent.height, capabilities.max_image_extent.height, - size.height, )) .build() } diff --git a/src/20_index_buffer.rs b/src/20_index_buffer.rs index 8ea4096..e361075 100644 --- a/src/20_index_buffer.rs +++ b/src/20_index_buffer.rs @@ -25,7 +25,7 @@ use vulkanalia::window as vk_window; use vulkanalia::Version; use winit::dpi::LogicalSize; use winit::event::{Event, WindowEvent}; -use winit::event_loop::{ControlFlow, EventLoop}; +use winit::event_loop::EventLoop; use winit::window::{Window, WindowBuilder}; use vulkanalia::vk::ExtDebugUtilsExtension; @@ -64,7 +64,7 @@ fn main() -> Result<()> { // Window - let event_loop = EventLoop::new(); + let event_loop = EventLoop::new()?; let window = WindowBuilder::new() .with_title("Vulkan Tutorial (Rust)") .with_inner_size(LogicalSize::new(1024, 768)) @@ -73,31 +73,37 @@ fn main() -> Result<()> { // App let mut app = unsafe { App::create(&window)? }; - let mut destroying = false; let mut minimized = false; - event_loop.run(move |event, _, control_flow| { - *control_flow = ControlFlow::Poll; + event_loop.run(move |event, elwt| { match event { - // Render a frame if our Vulkan app is not being destroyed. - Event::MainEventsCleared if !destroying && !minimized => unsafe { app.render(&window) }.unwrap(), - // Mark the window as having been resized. - Event::WindowEvent { event: WindowEvent::Resized(size), .. } => { - if size.width == 0 || size.height == 0 { - minimized = true; - } else { - minimized = false; - app.resized = true; + // Request a redraw when all events were processed. + Event::AboutToWait => window.request_redraw(), + Event::WindowEvent { event, .. } => match event { + // Render a frame if our Vulkan app is not being destroyed. + WindowEvent::RedrawRequested if !elwt.exiting() && !minimized => { + unsafe { app.render(&window) }.unwrap(); + }, + // Mark the window as having been resized. + WindowEvent::Resized(size) => { + if size.width == 0 || size.height == 0 { + minimized = true; + } else { + minimized = false; + app.resized = true; + } } - } - // Destroy our Vulkan app. - Event::WindowEvent { event: WindowEvent::CloseRequested, .. } => { - destroying = true; - *control_flow = ControlFlow::Exit; - unsafe { app.destroy(); } + // Destroy our Vulkan app. + WindowEvent::CloseRequested => { + elwt.exit(); + unsafe { app.destroy(); } + } + _ => {} } _ => {} } - }); + })?; + + Ok(()) } /// Our Vulkan app. @@ -145,12 +151,11 @@ impl App { unsafe fn render(&mut self, window: &Window) -> Result<()> { let in_flight_fence = self.data.in_flight_fences[self.frame]; - self.device - .wait_for_fences(&[in_flight_fence], true, u64::max_value())?; + self.device.wait_for_fences(&[in_flight_fence], true, u64::MAX)?; let result = self.device.acquire_next_image_khr( self.data.swapchain, - u64::max_value(), + u64::MAX, self.data.image_available_semaphores[self.frame], vk::Fence::null(), ); @@ -163,8 +168,7 @@ impl App { let image_in_flight = self.data.images_in_flight[image_index]; if !image_in_flight.is_null() { - self.device - .wait_for_fences(&[image_in_flight], true, u64::max_value())?; + self.device.wait_for_fences(&[image_in_flight], true, u64::MAX)?; } self.data.images_in_flight[image_index] = in_flight_fence; @@ -360,7 +364,11 @@ unsafe fn create_instance(window: &Window, entry: &Entry, data: &mut AppData) -> let mut debug_info = vk::DebugUtilsMessengerCreateInfoEXT::builder() .message_severity(vk::DebugUtilsMessageSeverityFlagsEXT::all()) - .message_type(vk::DebugUtilsMessageTypeFlagsEXT::all()) + .message_type( + vk::DebugUtilsMessageTypeFlagsEXT::GENERAL + | vk::DebugUtilsMessageTypeFlagsEXT::VALIDATION + | vk::DebugUtilsMessageTypeFlagsEXT::PERFORMANCE, + ) .user_callback(Some(debug_callback)); if VALIDATION_ENABLED { @@ -589,22 +597,19 @@ fn get_swapchain_present_mode(present_modes: &[vk::PresentModeKHR]) -> vk::Prese .unwrap_or(vk::PresentModeKHR::FIFO) } +#[rustfmt::skip] fn get_swapchain_extent(window: &Window, capabilities: vk::SurfaceCapabilitiesKHR) -> vk::Extent2D { - if capabilities.current_extent.width != u32::max_value() { + if capabilities.current_extent.width != u32::MAX { capabilities.current_extent } else { - let size = window.inner_size(); - let clamp = |min: u32, max: u32, v: u32| min.max(max.min(v)); vk::Extent2D::builder() - .width(clamp( + .width(window.inner_size().width.clamp( capabilities.min_image_extent.width, capabilities.max_image_extent.width, - size.width, )) - .height(clamp( + .height(window.inner_size().height.clamp( capabilities.min_image_extent.height, capabilities.max_image_extent.height, - size.height, )) .build() } diff --git a/src/21_descriptor_set_layout.rs b/src/21_descriptor_set_layout.rs index 885f875..2230fe1 100644 --- a/src/21_descriptor_set_layout.rs +++ b/src/21_descriptor_set_layout.rs @@ -26,7 +26,7 @@ use vulkanalia::window as vk_window; use vulkanalia::Version; use winit::dpi::LogicalSize; use winit::event::{Event, WindowEvent}; -use winit::event_loop::{ControlFlow, EventLoop}; +use winit::event_loop::EventLoop; use winit::window::{Window, WindowBuilder}; use vulkanalia::vk::ExtDebugUtilsExtension; @@ -66,7 +66,7 @@ fn main() -> Result<()> { // Window - let event_loop = EventLoop::new(); + let event_loop = EventLoop::new()?; let window = WindowBuilder::new() .with_title("Vulkan Tutorial (Rust)") .with_inner_size(LogicalSize::new(1024, 768)) @@ -75,31 +75,37 @@ fn main() -> Result<()> { // App let mut app = unsafe { App::create(&window)? }; - let mut destroying = false; let mut minimized = false; - event_loop.run(move |event, _, control_flow| { - *control_flow = ControlFlow::Poll; + event_loop.run(move |event, elwt| { match event { - // Render a frame if our Vulkan app is not being destroyed. - Event::MainEventsCleared if !destroying && !minimized => unsafe { app.render(&window) }.unwrap(), - // Mark the window as having been resized. - Event::WindowEvent { event: WindowEvent::Resized(size), .. } => { - if size.width == 0 || size.height == 0 { - minimized = true; - } else { - minimized = false; - app.resized = true; + // Request a redraw when all events were processed. + Event::AboutToWait => window.request_redraw(), + Event::WindowEvent { event, .. } => match event { + // Render a frame if our Vulkan app is not being destroyed. + WindowEvent::RedrawRequested if !elwt.exiting() && !minimized => { + unsafe { app.render(&window) }.unwrap(); + }, + // Mark the window as having been resized. + WindowEvent::Resized(size) => { + if size.width == 0 || size.height == 0 { + minimized = true; + } else { + minimized = false; + app.resized = true; + } } - } - // Destroy our Vulkan app. - Event::WindowEvent { event: WindowEvent::CloseRequested, .. } => { - destroying = true; - *control_flow = ControlFlow::Exit; - unsafe { app.destroy(); } + // Destroy our Vulkan app. + WindowEvent::CloseRequested => { + elwt.exit(); + unsafe { app.destroy(); } + } + _ => {} } _ => {} } - }); + })?; + + Ok(()) } /// Our Vulkan app. @@ -151,12 +157,11 @@ impl App { unsafe fn render(&mut self, window: &Window) -> Result<()> { let in_flight_fence = self.data.in_flight_fences[self.frame]; - self.device - .wait_for_fences(&[in_flight_fence], true, u64::max_value())?; + self.device.wait_for_fences(&[in_flight_fence], true, u64::MAX)?; let result = self.device.acquire_next_image_khr( self.data.swapchain, - u64::max_value(), + u64::MAX, self.data.image_available_semaphores[self.frame], vk::Fence::null(), ); @@ -169,8 +174,7 @@ impl App { let image_in_flight = self.data.images_in_flight[image_index]; if !image_in_flight.is_null() { - self.device - .wait_for_fences(&[image_in_flight], true, u64::max_value())?; + self.device.wait_for_fences(&[image_in_flight], true, u64::MAX)?; } self.data.images_in_flight[image_index] = in_flight_fence; @@ -416,7 +420,11 @@ unsafe fn create_instance(window: &Window, entry: &Entry, data: &mut AppData) -> let mut debug_info = vk::DebugUtilsMessengerCreateInfoEXT::builder() .message_severity(vk::DebugUtilsMessageSeverityFlagsEXT::all()) - .message_type(vk::DebugUtilsMessageTypeFlagsEXT::all()) + .message_type( + vk::DebugUtilsMessageTypeFlagsEXT::GENERAL + | vk::DebugUtilsMessageTypeFlagsEXT::VALIDATION + | vk::DebugUtilsMessageTypeFlagsEXT::PERFORMANCE, + ) .user_callback(Some(debug_callback)); if VALIDATION_ENABLED { @@ -645,22 +653,19 @@ fn get_swapchain_present_mode(present_modes: &[vk::PresentModeKHR]) -> vk::Prese .unwrap_or(vk::PresentModeKHR::FIFO) } +#[rustfmt::skip] fn get_swapchain_extent(window: &Window, capabilities: vk::SurfaceCapabilitiesKHR) -> vk::Extent2D { - if capabilities.current_extent.width != u32::max_value() { + if capabilities.current_extent.width != u32::MAX { capabilities.current_extent } else { - let size = window.inner_size(); - let clamp = |min: u32, max: u32, v: u32| min.max(max.min(v)); vk::Extent2D::builder() - .width(clamp( + .width(window.inner_size().width.clamp( capabilities.min_image_extent.width, capabilities.max_image_extent.width, - size.width, )) - .height(clamp( + .height(window.inner_size().height.clamp( capabilities.min_image_extent.height, capabilities.max_image_extent.height, - size.height, )) .build() } diff --git a/src/22_descriptor_sets.rs b/src/22_descriptor_sets.rs index 781f6f1..7bee658 100644 --- a/src/22_descriptor_sets.rs +++ b/src/22_descriptor_sets.rs @@ -26,7 +26,7 @@ use vulkanalia::window as vk_window; use vulkanalia::Version; use winit::dpi::LogicalSize; use winit::event::{Event, WindowEvent}; -use winit::event_loop::{ControlFlow, EventLoop}; +use winit::event_loop::EventLoop; use winit::window::{Window, WindowBuilder}; use vulkanalia::vk::ExtDebugUtilsExtension; @@ -66,7 +66,7 @@ fn main() -> Result<()> { // Window - let event_loop = EventLoop::new(); + let event_loop = EventLoop::new()?; let window = WindowBuilder::new() .with_title("Vulkan Tutorial (Rust)") .with_inner_size(LogicalSize::new(1024, 768)) @@ -75,31 +75,37 @@ fn main() -> Result<()> { // App let mut app = unsafe { App::create(&window)? }; - let mut destroying = false; let mut minimized = false; - event_loop.run(move |event, _, control_flow| { - *control_flow = ControlFlow::Poll; + event_loop.run(move |event, elwt| { match event { - // Render a frame if our Vulkan app is not being destroyed. - Event::MainEventsCleared if !destroying && !minimized => unsafe { app.render(&window) }.unwrap(), - // Mark the window as having been resized. - Event::WindowEvent { event: WindowEvent::Resized(size), .. } => { - if size.width == 0 || size.height == 0 { - minimized = true; - } else { - minimized = false; - app.resized = true; + // Request a redraw when all events were processed. + Event::AboutToWait => window.request_redraw(), + Event::WindowEvent { event, .. } => match event { + // Render a frame if our Vulkan app is not being destroyed. + WindowEvent::RedrawRequested if !elwt.exiting() && !minimized => { + unsafe { app.render(&window) }.unwrap(); + }, + // Mark the window as having been resized. + WindowEvent::Resized(size) => { + if size.width == 0 || size.height == 0 { + minimized = true; + } else { + minimized = false; + app.resized = true; + } } - } - // Destroy our Vulkan app. - Event::WindowEvent { event: WindowEvent::CloseRequested, .. } => { - destroying = true; - *control_flow = ControlFlow::Exit; - unsafe { app.destroy(); } + // Destroy our Vulkan app. + WindowEvent::CloseRequested => { + elwt.exit(); + unsafe { app.destroy(); } + } + _ => {} } _ => {} } - }); + })?; + + Ok(()) } /// Our Vulkan app. @@ -153,12 +159,11 @@ impl App { unsafe fn render(&mut self, window: &Window) -> Result<()> { let in_flight_fence = self.data.in_flight_fences[self.frame]; - self.device - .wait_for_fences(&[in_flight_fence], true, u64::max_value())?; + self.device.wait_for_fences(&[in_flight_fence], true, u64::MAX)?; let result = self.device.acquire_next_image_khr( self.data.swapchain, - u64::max_value(), + u64::MAX, self.data.image_available_semaphores[self.frame], vk::Fence::null(), ); @@ -171,8 +176,7 @@ impl App { let image_in_flight = self.data.images_in_flight[image_index]; if !image_in_flight.is_null() { - self.device - .wait_for_fences(&[image_in_flight], true, u64::max_value())?; + self.device.wait_for_fences(&[image_in_flight], true, u64::MAX)?; } self.data.images_in_flight[image_index] = in_flight_fence; @@ -424,7 +428,11 @@ unsafe fn create_instance(window: &Window, entry: &Entry, data: &mut AppData) -> let mut debug_info = vk::DebugUtilsMessengerCreateInfoEXT::builder() .message_severity(vk::DebugUtilsMessageSeverityFlagsEXT::all()) - .message_type(vk::DebugUtilsMessageTypeFlagsEXT::all()) + .message_type( + vk::DebugUtilsMessageTypeFlagsEXT::GENERAL + | vk::DebugUtilsMessageTypeFlagsEXT::VALIDATION + | vk::DebugUtilsMessageTypeFlagsEXT::PERFORMANCE, + ) .user_callback(Some(debug_callback)); if VALIDATION_ENABLED { @@ -653,22 +661,19 @@ fn get_swapchain_present_mode(present_modes: &[vk::PresentModeKHR]) -> vk::Prese .unwrap_or(vk::PresentModeKHR::FIFO) } +#[rustfmt::skip] fn get_swapchain_extent(window: &Window, capabilities: vk::SurfaceCapabilitiesKHR) -> vk::Extent2D { - if capabilities.current_extent.width != u32::max_value() { + if capabilities.current_extent.width != u32::MAX { capabilities.current_extent } else { - let size = window.inner_size(); - let clamp = |min: u32, max: u32, v: u32| min.max(max.min(v)); vk::Extent2D::builder() - .width(clamp( + .width(window.inner_size().width.clamp( capabilities.min_image_extent.width, capabilities.max_image_extent.width, - size.width, )) - .height(clamp( + .height(window.inner_size().height.clamp( capabilities.min_image_extent.height, capabilities.max_image_extent.height, - size.height, )) .build() } diff --git a/src/23_texture_image.rs b/src/23_texture_image.rs index 177822b..997871d 100644 --- a/src/23_texture_image.rs +++ b/src/23_texture_image.rs @@ -27,7 +27,7 @@ use vulkanalia::window as vk_window; use vulkanalia::Version; use winit::dpi::LogicalSize; use winit::event::{Event, WindowEvent}; -use winit::event_loop::{ControlFlow, EventLoop}; +use winit::event_loop::EventLoop; use winit::window::{Window, WindowBuilder}; use vulkanalia::vk::ExtDebugUtilsExtension; @@ -67,7 +67,7 @@ fn main() -> Result<()> { // Window - let event_loop = EventLoop::new(); + let event_loop = EventLoop::new()?; let window = WindowBuilder::new() .with_title("Vulkan Tutorial (Rust)") .with_inner_size(LogicalSize::new(1024, 768)) @@ -76,31 +76,37 @@ fn main() -> Result<()> { // App let mut app = unsafe { App::create(&window)? }; - let mut destroying = false; let mut minimized = false; - event_loop.run(move |event, _, control_flow| { - *control_flow = ControlFlow::Poll; + event_loop.run(move |event, elwt| { match event { - // Render a frame if our Vulkan app is not being destroyed. - Event::MainEventsCleared if !destroying && !minimized => unsafe { app.render(&window) }.unwrap(), - // Mark the window as having been resized. - Event::WindowEvent { event: WindowEvent::Resized(size), .. } => { - if size.width == 0 || size.height == 0 { - minimized = true; - } else { - minimized = false; - app.resized = true; + // Request a redraw when all events were processed. + Event::AboutToWait => window.request_redraw(), + Event::WindowEvent { event, .. } => match event { + // Render a frame if our Vulkan app is not being destroyed. + WindowEvent::RedrawRequested if !elwt.exiting() && !minimized => { + unsafe { app.render(&window) }.unwrap(); + }, + // Mark the window as having been resized. + WindowEvent::Resized(size) => { + if size.width == 0 || size.height == 0 { + minimized = true; + } else { + minimized = false; + app.resized = true; + } } - } - // Destroy our Vulkan app. - Event::WindowEvent { event: WindowEvent::CloseRequested, .. } => { - destroying = true; - *control_flow = ControlFlow::Exit; - unsafe { app.destroy(); } + // Destroy our Vulkan app. + WindowEvent::CloseRequested => { + elwt.exit(); + unsafe { app.destroy(); } + } + _ => {} } _ => {} } - }); + })?; + + Ok(()) } /// Our Vulkan app. @@ -155,12 +161,11 @@ impl App { unsafe fn render(&mut self, window: &Window) -> Result<()> { let in_flight_fence = self.data.in_flight_fences[self.frame]; - self.device - .wait_for_fences(&[in_flight_fence], true, u64::max_value())?; + self.device.wait_for_fences(&[in_flight_fence], true, u64::MAX)?; let result = self.device.acquire_next_image_khr( self.data.swapchain, - u64::max_value(), + u64::MAX, self.data.image_available_semaphores[self.frame], vk::Fence::null(), ); @@ -173,8 +178,7 @@ impl App { let image_in_flight = self.data.images_in_flight[image_index]; if !image_in_flight.is_null() { - self.device - .wait_for_fences(&[image_in_flight], true, u64::max_value())?; + self.device.wait_for_fences(&[image_in_flight], true, u64::MAX)?; } self.data.images_in_flight[image_index] = in_flight_fence; @@ -431,7 +435,11 @@ unsafe fn create_instance(window: &Window, entry: &Entry, data: &mut AppData) -> let mut debug_info = vk::DebugUtilsMessengerCreateInfoEXT::builder() .message_severity(vk::DebugUtilsMessageSeverityFlagsEXT::all()) - .message_type(vk::DebugUtilsMessageTypeFlagsEXT::all()) + .message_type( + vk::DebugUtilsMessageTypeFlagsEXT::GENERAL + | vk::DebugUtilsMessageTypeFlagsEXT::VALIDATION + | vk::DebugUtilsMessageTypeFlagsEXT::PERFORMANCE, + ) .user_callback(Some(debug_callback)); if VALIDATION_ENABLED { @@ -660,22 +668,19 @@ fn get_swapchain_present_mode(present_modes: &[vk::PresentModeKHR]) -> vk::Prese .unwrap_or(vk::PresentModeKHR::FIFO) } +#[rustfmt::skip] fn get_swapchain_extent(window: &Window, capabilities: vk::SurfaceCapabilitiesKHR) -> vk::Extent2D { - if capabilities.current_extent.width != u32::max_value() { + if capabilities.current_extent.width != u32::MAX { capabilities.current_extent } else { - let size = window.inner_size(); - let clamp = |min: u32, max: u32, v: u32| min.max(max.min(v)); vk::Extent2D::builder() - .width(clamp( + .width(window.inner_size().width.clamp( capabilities.min_image_extent.width, capabilities.max_image_extent.width, - size.width, )) - .height(clamp( + .height(window.inner_size().height.clamp( capabilities.min_image_extent.height, capabilities.max_image_extent.height, - size.height, )) .build() } diff --git a/src/24_sampler.rs b/src/24_sampler.rs index 8e3f80e..ec1a2bb 100644 --- a/src/24_sampler.rs +++ b/src/24_sampler.rs @@ -27,7 +27,7 @@ use vulkanalia::window as vk_window; use vulkanalia::Version; use winit::dpi::LogicalSize; use winit::event::{Event, WindowEvent}; -use winit::event_loop::{ControlFlow, EventLoop}; +use winit::event_loop::EventLoop; use winit::window::{Window, WindowBuilder}; use vulkanalia::vk::ExtDebugUtilsExtension; @@ -67,7 +67,7 @@ fn main() -> Result<()> { // Window - let event_loop = EventLoop::new(); + let event_loop = EventLoop::new()?; let window = WindowBuilder::new() .with_title("Vulkan Tutorial (Rust)") .with_inner_size(LogicalSize::new(1024, 768)) @@ -76,31 +76,37 @@ fn main() -> Result<()> { // App let mut app = unsafe { App::create(&window)? }; - let mut destroying = false; let mut minimized = false; - event_loop.run(move |event, _, control_flow| { - *control_flow = ControlFlow::Poll; + event_loop.run(move |event, elwt| { match event { - // Render a frame if our Vulkan app is not being destroyed. - Event::MainEventsCleared if !destroying && !minimized => unsafe { app.render(&window) }.unwrap(), - // Mark the window as having been resized. - Event::WindowEvent { event: WindowEvent::Resized(size), .. } => { - if size.width == 0 || size.height == 0 { - minimized = true; - } else { - minimized = false; - app.resized = true; + // Request a redraw when all events were processed. + Event::AboutToWait => window.request_redraw(), + Event::WindowEvent { event, .. } => match event { + // Render a frame if our Vulkan app is not being destroyed. + WindowEvent::RedrawRequested if !elwt.exiting() && !minimized => { + unsafe { app.render(&window) }.unwrap(); + }, + // Mark the window as having been resized. + WindowEvent::Resized(size) => { + if size.width == 0 || size.height == 0 { + minimized = true; + } else { + minimized = false; + app.resized = true; + } } - } - // Destroy our Vulkan app. - Event::WindowEvent { event: WindowEvent::CloseRequested, .. } => { - destroying = true; - *control_flow = ControlFlow::Exit; - unsafe { app.destroy(); } + // Destroy our Vulkan app. + WindowEvent::CloseRequested => { + elwt.exit(); + unsafe { app.destroy(); } + } + _ => {} } _ => {} } - }); + })?; + + Ok(()) } /// Our Vulkan app. @@ -157,12 +163,11 @@ impl App { unsafe fn render(&mut self, window: &Window) -> Result<()> { let in_flight_fence = self.data.in_flight_fences[self.frame]; - self.device - .wait_for_fences(&[in_flight_fence], true, u64::max_value())?; + self.device.wait_for_fences(&[in_flight_fence], true, u64::MAX)?; let result = self.device.acquire_next_image_khr( self.data.swapchain, - u64::max_value(), + u64::MAX, self.data.image_available_semaphores[self.frame], vk::Fence::null(), ); @@ -175,8 +180,7 @@ impl App { let image_in_flight = self.data.images_in_flight[image_index]; if !image_in_flight.is_null() { - self.device - .wait_for_fences(&[image_in_flight], true, u64::max_value())?; + self.device.wait_for_fences(&[image_in_flight], true, u64::MAX)?; } self.data.images_in_flight[image_index] = in_flight_fence; @@ -437,7 +441,11 @@ unsafe fn create_instance(window: &Window, entry: &Entry, data: &mut AppData) -> let mut debug_info = vk::DebugUtilsMessengerCreateInfoEXT::builder() .message_severity(vk::DebugUtilsMessageSeverityFlagsEXT::all()) - .message_type(vk::DebugUtilsMessageTypeFlagsEXT::all()) + .message_type( + vk::DebugUtilsMessageTypeFlagsEXT::GENERAL + | vk::DebugUtilsMessageTypeFlagsEXT::VALIDATION + | vk::DebugUtilsMessageTypeFlagsEXT::PERFORMANCE, + ) .user_callback(Some(debug_callback)); if VALIDATION_ENABLED { @@ -671,22 +679,19 @@ fn get_swapchain_present_mode(present_modes: &[vk::PresentModeKHR]) -> vk::Prese .unwrap_or(vk::PresentModeKHR::FIFO) } +#[rustfmt::skip] fn get_swapchain_extent(window: &Window, capabilities: vk::SurfaceCapabilitiesKHR) -> vk::Extent2D { - if capabilities.current_extent.width != u32::max_value() { + if capabilities.current_extent.width != u32::MAX { capabilities.current_extent } else { - let size = window.inner_size(); - let clamp = |min: u32, max: u32, v: u32| min.max(max.min(v)); vk::Extent2D::builder() - .width(clamp( + .width(window.inner_size().width.clamp( capabilities.min_image_extent.width, capabilities.max_image_extent.width, - size.width, )) - .height(clamp( + .height(window.inner_size().height.clamp( capabilities.min_image_extent.height, capabilities.max_image_extent.height, - size.height, )) .build() } diff --git a/src/25_texture_mapping.rs b/src/25_texture_mapping.rs index 7eb779a..86d2376 100644 --- a/src/25_texture_mapping.rs +++ b/src/25_texture_mapping.rs @@ -27,7 +27,7 @@ use vulkanalia::window as vk_window; use vulkanalia::Version; use winit::dpi::LogicalSize; use winit::event::{Event, WindowEvent}; -use winit::event_loop::{ControlFlow, EventLoop}; +use winit::event_loop::EventLoop; use winit::window::{Window, WindowBuilder}; use vulkanalia::vk::ExtDebugUtilsExtension; @@ -67,7 +67,7 @@ fn main() -> Result<()> { // Window - let event_loop = EventLoop::new(); + let event_loop = EventLoop::new()?; let window = WindowBuilder::new() .with_title("Vulkan Tutorial (Rust)") .with_inner_size(LogicalSize::new(1024, 768)) @@ -76,31 +76,37 @@ fn main() -> Result<()> { // App let mut app = unsafe { App::create(&window)? }; - let mut destroying = false; let mut minimized = false; - event_loop.run(move |event, _, control_flow| { - *control_flow = ControlFlow::Poll; + event_loop.run(move |event, elwt| { match event { - // Render a frame if our Vulkan app is not being destroyed. - Event::MainEventsCleared if !destroying && !minimized => unsafe { app.render(&window) }.unwrap(), - // Mark the window as having been resized. - Event::WindowEvent { event: WindowEvent::Resized(size), .. } => { - if size.width == 0 || size.height == 0 { - minimized = true; - } else { - minimized = false; - app.resized = true; + // Request a redraw when all events were processed. + Event::AboutToWait => window.request_redraw(), + Event::WindowEvent { event, .. } => match event { + // Render a frame if our Vulkan app is not being destroyed. + WindowEvent::RedrawRequested if !elwt.exiting() && !minimized => { + unsafe { app.render(&window) }.unwrap(); + }, + // Mark the window as having been resized. + WindowEvent::Resized(size) => { + if size.width == 0 || size.height == 0 { + minimized = true; + } else { + minimized = false; + app.resized = true; + } } - } - // Destroy our Vulkan app. - Event::WindowEvent { event: WindowEvent::CloseRequested, .. } => { - destroying = true; - *control_flow = ControlFlow::Exit; - unsafe { app.destroy(); } + // Destroy our Vulkan app. + WindowEvent::CloseRequested => { + elwt.exit(); + unsafe { app.destroy(); } + } + _ => {} } _ => {} } - }); + })?; + + Ok(()) } /// Our Vulkan app. @@ -157,12 +163,11 @@ impl App { unsafe fn render(&mut self, window: &Window) -> Result<()> { let in_flight_fence = self.data.in_flight_fences[self.frame]; - self.device - .wait_for_fences(&[in_flight_fence], true, u64::max_value())?; + self.device.wait_for_fences(&[in_flight_fence], true, u64::MAX)?; let result = self.device.acquire_next_image_khr( self.data.swapchain, - u64::max_value(), + u64::MAX, self.data.image_available_semaphores[self.frame], vk::Fence::null(), ); @@ -175,8 +180,7 @@ impl App { let image_in_flight = self.data.images_in_flight[image_index]; if !image_in_flight.is_null() { - self.device - .wait_for_fences(&[image_in_flight], true, u64::max_value())?; + self.device.wait_for_fences(&[image_in_flight], true, u64::MAX)?; } self.data.images_in_flight[image_index] = in_flight_fence; @@ -437,7 +441,11 @@ unsafe fn create_instance(window: &Window, entry: &Entry, data: &mut AppData) -> let mut debug_info = vk::DebugUtilsMessengerCreateInfoEXT::builder() .message_severity(vk::DebugUtilsMessageSeverityFlagsEXT::all()) - .message_type(vk::DebugUtilsMessageTypeFlagsEXT::all()) + .message_type( + vk::DebugUtilsMessageTypeFlagsEXT::GENERAL + | vk::DebugUtilsMessageTypeFlagsEXT::VALIDATION + | vk::DebugUtilsMessageTypeFlagsEXT::PERFORMANCE, + ) .user_callback(Some(debug_callback)); if VALIDATION_ENABLED { @@ -671,22 +679,19 @@ fn get_swapchain_present_mode(present_modes: &[vk::PresentModeKHR]) -> vk::Prese .unwrap_or(vk::PresentModeKHR::FIFO) } +#[rustfmt::skip] fn get_swapchain_extent(window: &Window, capabilities: vk::SurfaceCapabilitiesKHR) -> vk::Extent2D { - if capabilities.current_extent.width != u32::max_value() { + if capabilities.current_extent.width != u32::MAX { capabilities.current_extent } else { - let size = window.inner_size(); - let clamp = |min: u32, max: u32, v: u32| min.max(max.min(v)); vk::Extent2D::builder() - .width(clamp( + .width(window.inner_size().width.clamp( capabilities.min_image_extent.width, capabilities.max_image_extent.width, - size.width, )) - .height(clamp( + .height(window.inner_size().height.clamp( capabilities.min_image_extent.height, capabilities.max_image_extent.height, - size.height, )) .build() } diff --git a/src/26_depth_buffering.rs b/src/26_depth_buffering.rs index e7f8dfa..b4a4b87 100644 --- a/src/26_depth_buffering.rs +++ b/src/26_depth_buffering.rs @@ -27,7 +27,7 @@ use vulkanalia::window as vk_window; use vulkanalia::Version; use winit::dpi::LogicalSize; use winit::event::{Event, WindowEvent}; -use winit::event_loop::{ControlFlow, EventLoop}; +use winit::event_loop::EventLoop; use winit::window::{Window, WindowBuilder}; use vulkanalia::vk::ExtDebugUtilsExtension; @@ -77,7 +77,7 @@ fn main() -> Result<()> { // Window - let event_loop = EventLoop::new(); + let event_loop = EventLoop::new()?; let window = WindowBuilder::new() .with_title("Vulkan Tutorial (Rust)") .with_inner_size(LogicalSize::new(1024, 768)) @@ -86,31 +86,37 @@ fn main() -> Result<()> { // App let mut app = unsafe { App::create(&window)? }; - let mut destroying = false; let mut minimized = false; - event_loop.run(move |event, _, control_flow| { - *control_flow = ControlFlow::Poll; + event_loop.run(move |event, elwt| { match event { - // Render a frame if our Vulkan app is not being destroyed. - Event::MainEventsCleared if !destroying && !minimized => unsafe { app.render(&window) }.unwrap(), - // Mark the window as having been resized. - Event::WindowEvent { event: WindowEvent::Resized(size), .. } => { - if size.width == 0 || size.height == 0 { - minimized = true; - } else { - minimized = false; - app.resized = true; + // Request a redraw when all events were processed. + Event::AboutToWait => window.request_redraw(), + Event::WindowEvent { event, .. } => match event { + // Render a frame if our Vulkan app is not being destroyed. + WindowEvent::RedrawRequested if !elwt.exiting() && !minimized => { + unsafe { app.render(&window) }.unwrap(); + }, + // Mark the window as having been resized. + WindowEvent::Resized(size) => { + if size.width == 0 || size.height == 0 { + minimized = true; + } else { + minimized = false; + app.resized = true; + } } - } - // Destroy our Vulkan app. - Event::WindowEvent { event: WindowEvent::CloseRequested, .. } => { - destroying = true; - *control_flow = ControlFlow::Exit; - unsafe { app.destroy(); } + // Destroy our Vulkan app. + WindowEvent::CloseRequested => { + elwt.exit(); + unsafe { app.destroy(); } + } + _ => {} } _ => {} } - }); + })?; + + Ok(()) } /// Our Vulkan app. @@ -168,12 +174,11 @@ impl App { unsafe fn render(&mut self, window: &Window) -> Result<()> { let in_flight_fence = self.data.in_flight_fences[self.frame]; - self.device - .wait_for_fences(&[in_flight_fence], true, u64::max_value())?; + self.device.wait_for_fences(&[in_flight_fence], true, u64::MAX)?; let result = self.device.acquire_next_image_khr( self.data.swapchain, - u64::max_value(), + u64::MAX, self.data.image_available_semaphores[self.frame], vk::Fence::null(), ); @@ -186,8 +191,7 @@ impl App { let image_in_flight = self.data.images_in_flight[image_index]; if !image_in_flight.is_null() { - self.device - .wait_for_fences(&[image_in_flight], true, u64::max_value())?; + self.device.wait_for_fences(&[image_in_flight], true, u64::MAX)?; } self.data.images_in_flight[image_index] = in_flight_fence; @@ -463,7 +467,11 @@ unsafe fn create_instance(window: &Window, entry: &Entry, data: &mut AppData) -> let mut debug_info = vk::DebugUtilsMessengerCreateInfoEXT::builder() .message_severity(vk::DebugUtilsMessageSeverityFlagsEXT::all()) - .message_type(vk::DebugUtilsMessageTypeFlagsEXT::all()) + .message_type( + vk::DebugUtilsMessageTypeFlagsEXT::GENERAL + | vk::DebugUtilsMessageTypeFlagsEXT::VALIDATION + | vk::DebugUtilsMessageTypeFlagsEXT::PERFORMANCE, + ) .user_callback(Some(debug_callback)); if VALIDATION_ENABLED { @@ -697,22 +705,19 @@ fn get_swapchain_present_mode(present_modes: &[vk::PresentModeKHR]) -> vk::Prese .unwrap_or(vk::PresentModeKHR::FIFO) } +#[rustfmt::skip] fn get_swapchain_extent(window: &Window, capabilities: vk::SurfaceCapabilitiesKHR) -> vk::Extent2D { - if capabilities.current_extent.width != u32::max_value() { + if capabilities.current_extent.width != u32::MAX { capabilities.current_extent } else { - let size = window.inner_size(); - let clamp = |min: u32, max: u32, v: u32| min.max(max.min(v)); vk::Extent2D::builder() - .width(clamp( + .width(window.inner_size().width.clamp( capabilities.min_image_extent.width, capabilities.max_image_extent.width, - size.width, )) - .height(clamp( + .height(window.inner_size().height.clamp( capabilities.min_image_extent.height, capabilities.max_image_extent.height, - size.height, )) .build() } diff --git a/src/27_model_loading.rs b/src/27_model_loading.rs index f3a4f35..3ca21f9 100644 --- a/src/27_model_loading.rs +++ b/src/27_model_loading.rs @@ -29,7 +29,7 @@ use vulkanalia::window as vk_window; use vulkanalia::Version; use winit::dpi::LogicalSize; use winit::event::{Event, WindowEvent}; -use winit::event_loop::{ControlFlow, EventLoop}; +use winit::event_loop::EventLoop; use winit::window::{Window, WindowBuilder}; use vulkanalia::vk::ExtDebugUtilsExtension; @@ -59,7 +59,7 @@ fn main() -> Result<()> { // Window - let event_loop = EventLoop::new(); + let event_loop = EventLoop::new()?; let window = WindowBuilder::new() .with_title("Vulkan Tutorial (Rust)") .with_inner_size(LogicalSize::new(1024, 768)) @@ -68,31 +68,37 @@ fn main() -> Result<()> { // App let mut app = unsafe { App::create(&window)? }; - let mut destroying = false; let mut minimized = false; - event_loop.run(move |event, _, control_flow| { - *control_flow = ControlFlow::Poll; + event_loop.run(move |event, elwt| { match event { - // Render a frame if our Vulkan app is not being destroyed. - Event::MainEventsCleared if !destroying && !minimized => unsafe { app.render(&window) }.unwrap(), - // Mark the window as having been resized. - Event::WindowEvent { event: WindowEvent::Resized(size), .. } => { - if size.width == 0 || size.height == 0 { - minimized = true; - } else { - minimized = false; - app.resized = true; + // Request a redraw when all events were processed. + Event::AboutToWait => window.request_redraw(), + Event::WindowEvent { event, .. } => match event { + // Render a frame if our Vulkan app is not being destroyed. + WindowEvent::RedrawRequested if !elwt.exiting() && !minimized => { + unsafe { app.render(&window) }.unwrap(); + }, + // Mark the window as having been resized. + WindowEvent::Resized(size) => { + if size.width == 0 || size.height == 0 { + minimized = true; + } else { + minimized = false; + app.resized = true; + } } - } - // Destroy our Vulkan app. - Event::WindowEvent { event: WindowEvent::CloseRequested, .. } => { - destroying = true; - *control_flow = ControlFlow::Exit; - unsafe { app.destroy(); } + // Destroy our Vulkan app. + WindowEvent::CloseRequested => { + elwt.exit(); + unsafe { app.destroy(); } + } + _ => {} } _ => {} } - }); + })?; + + Ok(()) } /// Our Vulkan app. @@ -151,12 +157,11 @@ impl App { unsafe fn render(&mut self, window: &Window) -> Result<()> { let in_flight_fence = self.data.in_flight_fences[self.frame]; - self.device - .wait_for_fences(&[in_flight_fence], true, u64::max_value())?; + self.device.wait_for_fences(&[in_flight_fence], true, u64::MAX)?; let result = self.device.acquire_next_image_khr( self.data.swapchain, - u64::max_value(), + u64::MAX, self.data.image_available_semaphores[self.frame], vk::Fence::null(), ); @@ -169,8 +174,7 @@ impl App { let image_in_flight = self.data.images_in_flight[image_index]; if !image_in_flight.is_null() { - self.device - .wait_for_fences(&[image_in_flight], true, u64::max_value())?; + self.device.wait_for_fences(&[image_in_flight], true, u64::MAX)?; } self.data.images_in_flight[image_index] = in_flight_fence; @@ -449,7 +453,11 @@ unsafe fn create_instance(window: &Window, entry: &Entry, data: &mut AppData) -> let mut debug_info = vk::DebugUtilsMessengerCreateInfoEXT::builder() .message_severity(vk::DebugUtilsMessageSeverityFlagsEXT::all()) - .message_type(vk::DebugUtilsMessageTypeFlagsEXT::all()) + .message_type( + vk::DebugUtilsMessageTypeFlagsEXT::GENERAL + | vk::DebugUtilsMessageTypeFlagsEXT::VALIDATION + | vk::DebugUtilsMessageTypeFlagsEXT::PERFORMANCE, + ) .user_callback(Some(debug_callback)); if VALIDATION_ENABLED { @@ -683,22 +691,19 @@ fn get_swapchain_present_mode(present_modes: &[vk::PresentModeKHR]) -> vk::Prese .unwrap_or(vk::PresentModeKHR::FIFO) } +#[rustfmt::skip] fn get_swapchain_extent(window: &Window, capabilities: vk::SurfaceCapabilitiesKHR) -> vk::Extent2D { - if capabilities.current_extent.width != u32::max_value() { + if capabilities.current_extent.width != u32::MAX { capabilities.current_extent } else { - let size = window.inner_size(); - let clamp = |min: u32, max: u32, v: u32| min.max(max.min(v)); vk::Extent2D::builder() - .width(clamp( + .width(window.inner_size().width.clamp( capabilities.min_image_extent.width, capabilities.max_image_extent.width, - size.width, )) - .height(clamp( + .height(window.inner_size().height.clamp( capabilities.min_image_extent.height, capabilities.max_image_extent.height, - size.height, )) .build() } diff --git a/src/28_mipmapping.rs b/src/28_mipmapping.rs index 2fc48cd..f76212e 100644 --- a/src/28_mipmapping.rs +++ b/src/28_mipmapping.rs @@ -29,7 +29,7 @@ use vulkanalia::window as vk_window; use vulkanalia::Version; use winit::dpi::LogicalSize; use winit::event::{Event, WindowEvent}; -use winit::event_loop::{ControlFlow, EventLoop}; +use winit::event_loop::EventLoop; use winit::window::{Window, WindowBuilder}; use vulkanalia::vk::ExtDebugUtilsExtension; @@ -59,7 +59,7 @@ fn main() -> Result<()> { // Window - let event_loop = EventLoop::new(); + let event_loop = EventLoop::new()?; let window = WindowBuilder::new() .with_title("Vulkan Tutorial (Rust)") .with_inner_size(LogicalSize::new(1024, 768)) @@ -68,31 +68,37 @@ fn main() -> Result<()> { // App let mut app = unsafe { App::create(&window)? }; - let mut destroying = false; let mut minimized = false; - event_loop.run(move |event, _, control_flow| { - *control_flow = ControlFlow::Poll; + event_loop.run(move |event, elwt| { match event { - // Render a frame if our Vulkan app is not being destroyed. - Event::MainEventsCleared if !destroying && !minimized => unsafe { app.render(&window) }.unwrap(), - // Mark the window as having been resized. - Event::WindowEvent { event: WindowEvent::Resized(size), .. } => { - if size.width == 0 || size.height == 0 { - minimized = true; - } else { - minimized = false; - app.resized = true; + // Request a redraw when all events were processed. + Event::AboutToWait => window.request_redraw(), + Event::WindowEvent { event, .. } => match event { + // Render a frame if our Vulkan app is not being destroyed. + WindowEvent::RedrawRequested if !elwt.exiting() && !minimized => { + unsafe { app.render(&window) }.unwrap(); + }, + // Mark the window as having been resized. + WindowEvent::Resized(size) => { + if size.width == 0 || size.height == 0 { + minimized = true; + } else { + minimized = false; + app.resized = true; + } } - } - // Destroy our Vulkan app. - Event::WindowEvent { event: WindowEvent::CloseRequested, .. } => { - destroying = true; - *control_flow = ControlFlow::Exit; - unsafe { app.destroy(); } + // Destroy our Vulkan app. + WindowEvent::CloseRequested => { + elwt.exit(); + unsafe { app.destroy(); } + } + _ => {} } _ => {} } - }); + })?; + + Ok(()) } /// Our Vulkan app. @@ -151,12 +157,11 @@ impl App { unsafe fn render(&mut self, window: &Window) -> Result<()> { let in_flight_fence = self.data.in_flight_fences[self.frame]; - self.device - .wait_for_fences(&[in_flight_fence], true, u64::max_value())?; + self.device.wait_for_fences(&[in_flight_fence], true, u64::MAX)?; let result = self.device.acquire_next_image_khr( self.data.swapchain, - u64::max_value(), + u64::MAX, self.data.image_available_semaphores[self.frame], vk::Fence::null(), ); @@ -169,8 +174,7 @@ impl App { let image_in_flight = self.data.images_in_flight[image_index]; if !image_in_flight.is_null() { - self.device - .wait_for_fences(&[image_in_flight], true, u64::max_value())?; + self.device.wait_for_fences(&[image_in_flight], true, u64::MAX)?; } self.data.images_in_flight[image_index] = in_flight_fence; @@ -450,7 +454,11 @@ unsafe fn create_instance(window: &Window, entry: &Entry, data: &mut AppData) -> let mut debug_info = vk::DebugUtilsMessengerCreateInfoEXT::builder() .message_severity(vk::DebugUtilsMessageSeverityFlagsEXT::all()) - .message_type(vk::DebugUtilsMessageTypeFlagsEXT::all()) + .message_type( + vk::DebugUtilsMessageTypeFlagsEXT::GENERAL + | vk::DebugUtilsMessageTypeFlagsEXT::VALIDATION + | vk::DebugUtilsMessageTypeFlagsEXT::PERFORMANCE, + ) .user_callback(Some(debug_callback)); if VALIDATION_ENABLED { @@ -684,22 +692,19 @@ fn get_swapchain_present_mode(present_modes: &[vk::PresentModeKHR]) -> vk::Prese .unwrap_or(vk::PresentModeKHR::FIFO) } +#[rustfmt::skip] fn get_swapchain_extent(window: &Window, capabilities: vk::SurfaceCapabilitiesKHR) -> vk::Extent2D { - if capabilities.current_extent.width != u32::max_value() { + if capabilities.current_extent.width != u32::MAX { capabilities.current_extent } else { - let size = window.inner_size(); - let clamp = |min: u32, max: u32, v: u32| min.max(max.min(v)); vk::Extent2D::builder() - .width(clamp( + .width(window.inner_size().width.clamp( capabilities.min_image_extent.width, capabilities.max_image_extent.width, - size.width, )) - .height(clamp( + .height(window.inner_size().height.clamp( capabilities.min_image_extent.height, capabilities.max_image_extent.height, - size.height, )) .build() } diff --git a/src/29_multisampling.rs b/src/29_multisampling.rs index 0ed8514..cc39f1b 100644 --- a/src/29_multisampling.rs +++ b/src/29_multisampling.rs @@ -29,7 +29,7 @@ use vulkanalia::window as vk_window; use vulkanalia::Version; use winit::dpi::LogicalSize; use winit::event::{Event, WindowEvent}; -use winit::event_loop::{ControlFlow, EventLoop}; +use winit::event_loop::EventLoop; use winit::window::{Window, WindowBuilder}; use vulkanalia::vk::ExtDebugUtilsExtension; @@ -59,7 +59,7 @@ fn main() -> Result<()> { // Window - let event_loop = EventLoop::new(); + let event_loop = EventLoop::new()?; let window = WindowBuilder::new() .with_title("Vulkan Tutorial (Rust)") .with_inner_size(LogicalSize::new(1024, 768)) @@ -68,31 +68,37 @@ fn main() -> Result<()> { // App let mut app = unsafe { App::create(&window)? }; - let mut destroying = false; let mut minimized = false; - event_loop.run(move |event, _, control_flow| { - *control_flow = ControlFlow::Poll; + event_loop.run(move |event, elwt| { match event { - // Render a frame if our Vulkan app is not being destroyed. - Event::MainEventsCleared if !destroying && !minimized => unsafe { app.render(&window) }.unwrap(), - // Mark the window as having been resized. - Event::WindowEvent { event: WindowEvent::Resized(size), .. } => { - if size.width == 0 || size.height == 0 { - minimized = true; - } else { - minimized = false; - app.resized = true; + // Request a redraw when all events were processed. + Event::AboutToWait => window.request_redraw(), + Event::WindowEvent { event, .. } => match event { + // Render a frame if our Vulkan app is not being destroyed. + WindowEvent::RedrawRequested if !elwt.exiting() && !minimized => { + unsafe { app.render(&window) }.unwrap(); + }, + // Mark the window as having been resized. + WindowEvent::Resized(size) => { + if size.width == 0 || size.height == 0 { + minimized = true; + } else { + minimized = false; + app.resized = true; + } } - } - // Destroy our Vulkan app. - Event::WindowEvent { event: WindowEvent::CloseRequested, .. } => { - destroying = true; - *control_flow = ControlFlow::Exit; - unsafe { app.destroy(); } + // Destroy our Vulkan app. + WindowEvent::CloseRequested => { + elwt.exit(); + unsafe { app.destroy(); } + } + _ => {} } _ => {} } - }); + })?; + + Ok(()) } /// Our Vulkan app. @@ -152,12 +158,11 @@ impl App { unsafe fn render(&mut self, window: &Window) -> Result<()> { let in_flight_fence = self.data.in_flight_fences[self.frame]; - self.device - .wait_for_fences(&[in_flight_fence], true, u64::max_value())?; + self.device.wait_for_fences(&[in_flight_fence], true, u64::MAX)?; let result = self.device.acquire_next_image_khr( self.data.swapchain, - u64::max_value(), + u64::MAX, self.data.image_available_semaphores[self.frame], vk::Fence::null(), ); @@ -170,8 +175,7 @@ impl App { let image_in_flight = self.data.images_in_flight[image_index]; if !image_in_flight.is_null() { - self.device - .wait_for_fences(&[image_in_flight], true, u64::max_value())?; + self.device.wait_for_fences(&[image_in_flight], true, u64::MAX)?; } self.data.images_in_flight[image_index] = in_flight_fence; @@ -460,7 +464,11 @@ unsafe fn create_instance(window: &Window, entry: &Entry, data: &mut AppData) -> let mut debug_info = vk::DebugUtilsMessengerCreateInfoEXT::builder() .message_severity(vk::DebugUtilsMessageSeverityFlagsEXT::all()) - .message_type(vk::DebugUtilsMessageTypeFlagsEXT::all()) + .message_type( + vk::DebugUtilsMessageTypeFlagsEXT::GENERAL + | vk::DebugUtilsMessageTypeFlagsEXT::VALIDATION + | vk::DebugUtilsMessageTypeFlagsEXT::PERFORMANCE, + ) .user_callback(Some(debug_callback)); if VALIDATION_ENABLED { @@ -714,22 +722,19 @@ fn get_swapchain_present_mode(present_modes: &[vk::PresentModeKHR]) -> vk::Prese .unwrap_or(vk::PresentModeKHR::FIFO) } +#[rustfmt::skip] fn get_swapchain_extent(window: &Window, capabilities: vk::SurfaceCapabilitiesKHR) -> vk::Extent2D { - if capabilities.current_extent.width != u32::max_value() { + if capabilities.current_extent.width != u32::MAX { capabilities.current_extent } else { - let size = window.inner_size(); - let clamp = |min: u32, max: u32, v: u32| min.max(max.min(v)); vk::Extent2D::builder() - .width(clamp( + .width(window.inner_size().width.clamp( capabilities.min_image_extent.width, capabilities.max_image_extent.width, - size.width, )) - .height(clamp( + .height(window.inner_size().height.clamp( capabilities.min_image_extent.height, capabilities.max_image_extent.height, - size.height, )) .build() } diff --git a/src/30_push_constants.rs b/src/30_push_constants.rs index e9ba1ba..9f5a7d7 100644 --- a/src/30_push_constants.rs +++ b/src/30_push_constants.rs @@ -15,7 +15,7 @@ use std::hash::{Hash, Hasher}; use std::io::BufReader; use std::mem::size_of; use std::os::raw::c_void; -use std::ptr::{copy_nonoverlapping as memcpy, slice_from_raw_parts}; +use std::ptr::copy_nonoverlapping as memcpy; use std::time::Instant; use anyhow::{anyhow, Result}; @@ -29,7 +29,7 @@ use vulkanalia::window as vk_window; use vulkanalia::Version; use winit::dpi::LogicalSize; use winit::event::{Event, WindowEvent}; -use winit::event_loop::{ControlFlow, EventLoop}; +use winit::event_loop::EventLoop; use winit::window::{Window, WindowBuilder}; use vulkanalia::vk::ExtDebugUtilsExtension; @@ -59,7 +59,7 @@ fn main() -> Result<()> { // Window - let event_loop = EventLoop::new(); + let event_loop = EventLoop::new()?; let window = WindowBuilder::new() .with_title("Vulkan Tutorial (Rust)") .with_inner_size(LogicalSize::new(1024, 768)) @@ -68,31 +68,37 @@ fn main() -> Result<()> { // App let mut app = unsafe { App::create(&window)? }; - let mut destroying = false; let mut minimized = false; - event_loop.run(move |event, _, control_flow| { - *control_flow = ControlFlow::Poll; + event_loop.run(move |event, elwt| { match event { - // Render a frame if our Vulkan app is not being destroyed. - Event::MainEventsCleared if !destroying && !minimized => unsafe { app.render(&window) }.unwrap(), - // Mark the window as having been resized. - Event::WindowEvent { event: WindowEvent::Resized(size), .. } => { - if size.width == 0 || size.height == 0 { - minimized = true; - } else { - minimized = false; - app.resized = true; + // Request a redraw when all events were processed. + Event::AboutToWait => window.request_redraw(), + Event::WindowEvent { event, .. } => match event { + // Render a frame if our Vulkan app is not being destroyed. + WindowEvent::RedrawRequested if !elwt.exiting() && !minimized => { + unsafe { app.render(&window) }.unwrap(); + }, + // Mark the window as having been resized. + WindowEvent::Resized(size) => { + if size.width == 0 || size.height == 0 { + minimized = true; + } else { + minimized = false; + app.resized = true; + } } - } - // Destroy our Vulkan app. - Event::WindowEvent { event: WindowEvent::CloseRequested, .. } => { - destroying = true; - *control_flow = ControlFlow::Exit; - unsafe { app.destroy(); } + // Destroy our Vulkan app. + WindowEvent::CloseRequested => { + elwt.exit(); + unsafe { app.destroy(); } + } + _ => {} } _ => {} } - }); + })?; + + Ok(()) } /// Our Vulkan app. @@ -152,12 +158,11 @@ impl App { unsafe fn render(&mut self, window: &Window) -> Result<()> { let in_flight_fence = self.data.in_flight_fences[self.frame]; - self.device - .wait_for_fences(&[in_flight_fence], true, u64::max_value())?; + self.device.wait_for_fences(&[in_flight_fence], true, u64::MAX)?; let result = self.device.acquire_next_image_khr( self.data.swapchain, - u64::max_value(), + u64::MAX, self.data.image_available_semaphores[self.frame], vk::Fence::null(), ); @@ -170,8 +175,7 @@ impl App { let image_in_flight = self.data.images_in_flight[image_index]; if !image_in_flight.is_null() { - self.device - .wait_for_fences(&[image_in_flight], true, u64::max_value())?; + self.device.wait_for_fences(&[image_in_flight], true, u64::MAX)?; } self.data.images_in_flight[image_index] = in_flight_fence; @@ -456,7 +460,11 @@ unsafe fn create_instance(window: &Window, entry: &Entry, data: &mut AppData) -> let mut debug_info = vk::DebugUtilsMessengerCreateInfoEXT::builder() .message_severity(vk::DebugUtilsMessageSeverityFlagsEXT::all()) - .message_type(vk::DebugUtilsMessageTypeFlagsEXT::all()) + .message_type( + vk::DebugUtilsMessageTypeFlagsEXT::GENERAL + | vk::DebugUtilsMessageTypeFlagsEXT::VALIDATION + | vk::DebugUtilsMessageTypeFlagsEXT::PERFORMANCE, + ) .user_callback(Some(debug_callback)); if VALIDATION_ENABLED { @@ -710,22 +718,19 @@ fn get_swapchain_present_mode(present_modes: &[vk::PresentModeKHR]) -> vk::Prese .unwrap_or(vk::PresentModeKHR::FIFO) } +#[rustfmt::skip] fn get_swapchain_extent(window: &Window, capabilities: vk::SurfaceCapabilitiesKHR) -> vk::Extent2D { - if capabilities.current_extent.width != u32::max_value() { + if capabilities.current_extent.width != u32::MAX { capabilities.current_extent } else { - let size = window.inner_size(); - let clamp = |min: u32, max: u32, v: u32| min.max(max.min(v)); vk::Extent2D::builder() - .width(clamp( + .width(window.inner_size().width.clamp( capabilities.min_image_extent.width, capabilities.max_image_extent.width, - size.width, )) - .height(clamp( + .height(window.inner_size().height.clamp( capabilities.min_image_extent.height, capabilities.max_image_extent.height, - size.height, )) .build() } @@ -1685,7 +1690,7 @@ unsafe fn create_command_buffers(device: &Device, data: &mut AppData) -> Result< let model = Mat4::from_axis_angle(vec3(0.0, 0.0, 1.0), Deg(0.0)); - let model_bytes = &*slice_from_raw_parts(&model as *const Mat4 as *const u8, size_of::()); + let model_bytes = std::slice::from_raw_parts(&model as *const Mat4 as *const u8, size_of::()); let opacity = 0.25f32; let opacity_bytes = &opacity.to_ne_bytes()[..]; diff --git a/src/31_recycling_command_buffers.rs b/src/31_recycling_command_buffers.rs index ba08190..6b45b04 100644 --- a/src/31_recycling_command_buffers.rs +++ b/src/31_recycling_command_buffers.rs @@ -15,7 +15,7 @@ use std::hash::{Hash, Hasher}; use std::io::BufReader; use std::mem::size_of; use std::os::raw::c_void; -use std::ptr::{copy_nonoverlapping as memcpy, slice_from_raw_parts}; +use std::ptr::copy_nonoverlapping as memcpy; use std::time::Instant; use anyhow::{anyhow, Result}; @@ -29,7 +29,7 @@ use vulkanalia::window as vk_window; use vulkanalia::Version; use winit::dpi::LogicalSize; use winit::event::{Event, WindowEvent}; -use winit::event_loop::{ControlFlow, EventLoop}; +use winit::event_loop::EventLoop; use winit::window::{Window, WindowBuilder}; use vulkanalia::vk::ExtDebugUtilsExtension; @@ -59,7 +59,7 @@ fn main() -> Result<()> { // Window - let event_loop = EventLoop::new(); + let event_loop = EventLoop::new()?; let window = WindowBuilder::new() .with_title("Vulkan Tutorial (Rust)") .with_inner_size(LogicalSize::new(1024, 768)) @@ -68,31 +68,37 @@ fn main() -> Result<()> { // App let mut app = unsafe { App::create(&window)? }; - let mut destroying = false; let mut minimized = false; - event_loop.run(move |event, _, control_flow| { - *control_flow = ControlFlow::Poll; + event_loop.run(move |event, elwt| { match event { - // Render a frame if our Vulkan app is not being destroyed. - Event::MainEventsCleared if !destroying && !minimized => unsafe { app.render(&window) }.unwrap(), - // Mark the window as having been resized. - Event::WindowEvent { event: WindowEvent::Resized(size), .. } => { - if size.width == 0 || size.height == 0 { - minimized = true; - } else { - minimized = false; - app.resized = true; + // Request a redraw when all events were processed. + Event::AboutToWait => window.request_redraw(), + Event::WindowEvent { event, .. } => match event { + // Render a frame if our Vulkan app is not being destroyed. + WindowEvent::RedrawRequested if !elwt.exiting() && !minimized => { + unsafe { app.render(&window) }.unwrap(); + }, + // Mark the window as having been resized. + WindowEvent::Resized(size) => { + if size.width == 0 || size.height == 0 { + minimized = true; + } else { + minimized = false; + app.resized = true; + } } - } - // Destroy our Vulkan app. - Event::WindowEvent { event: WindowEvent::CloseRequested, .. } => { - destroying = true; - *control_flow = ControlFlow::Exit; - unsafe { app.destroy(); } + // Destroy our Vulkan app. + WindowEvent::CloseRequested => { + elwt.exit(); + unsafe { app.destroy(); } + } + _ => {} } _ => {} } - }); + })?; + + Ok(()) } /// Our Vulkan app. @@ -152,12 +158,11 @@ impl App { unsafe fn render(&mut self, window: &Window) -> Result<()> { let in_flight_fence = self.data.in_flight_fences[self.frame]; - self.device - .wait_for_fences(&[in_flight_fence], true, u64::max_value())?; + self.device.wait_for_fences(&[in_flight_fence], true, u64::MAX)?; let result = self.device.acquire_next_image_khr( self.data.swapchain, - u64::max_value(), + u64::MAX, self.data.image_available_semaphores[self.frame], vk::Fence::null(), ); @@ -170,8 +175,7 @@ impl App { let image_in_flight = self.data.images_in_flight[image_index]; if !image_in_flight.is_null() { - self.device - .wait_for_fences(&[image_in_flight], true, u64::max_value())?; + self.device.wait_for_fences(&[image_in_flight], true, u64::MAX)?; } self.data.images_in_flight[image_index] = in_flight_fence; @@ -231,10 +235,7 @@ impl App { let model = Mat4::from_axis_angle(vec3(0.0, 0.0, 1.0), Deg(90.0) * time); - let model_bytes = &*slice_from_raw_parts( - &model as *const Mat4 as *const u8, - size_of::() - ); + let model_bytes = std::slice::from_raw_parts(&model as *const Mat4 as *const u8, size_of::()); let opacity = 0.25f32; let opacity_bytes = &opacity.to_ne_bytes()[..]; @@ -543,7 +544,11 @@ unsafe fn create_instance(window: &Window, entry: &Entry, data: &mut AppData) -> let mut debug_info = vk::DebugUtilsMessengerCreateInfoEXT::builder() .message_severity(vk::DebugUtilsMessageSeverityFlagsEXT::all()) - .message_type(vk::DebugUtilsMessageTypeFlagsEXT::all()) + .message_type( + vk::DebugUtilsMessageTypeFlagsEXT::GENERAL + | vk::DebugUtilsMessageTypeFlagsEXT::VALIDATION + | vk::DebugUtilsMessageTypeFlagsEXT::PERFORMANCE, + ) .user_callback(Some(debug_callback)); if VALIDATION_ENABLED { @@ -797,22 +802,19 @@ fn get_swapchain_present_mode(present_modes: &[vk::PresentModeKHR]) -> vk::Prese .unwrap_or(vk::PresentModeKHR::FIFO) } +#[rustfmt::skip] fn get_swapchain_extent(window: &Window, capabilities: vk::SurfaceCapabilitiesKHR) -> vk::Extent2D { - if capabilities.current_extent.width != u32::max_value() { + if capabilities.current_extent.width != u32::MAX { capabilities.current_extent } else { - let size = window.inner_size(); - let clamp = |min: u32, max: u32, v: u32| min.max(max.min(v)); vk::Extent2D::builder() - .width(clamp( + .width(window.inner_size().width.clamp( capabilities.min_image_extent.width, capabilities.max_image_extent.width, - size.width, )) - .height(clamp( + .height(window.inner_size().height.clamp( capabilities.min_image_extent.height, capabilities.max_image_extent.height, - size.height, )) .build() } diff --git a/src/32_secondary_command_buffers.rs b/src/32_secondary_command_buffers.rs index bedce34..2bcd2bf 100644 --- a/src/32_secondary_command_buffers.rs +++ b/src/32_secondary_command_buffers.rs @@ -15,7 +15,7 @@ use std::hash::{Hash, Hasher}; use std::io::BufReader; use std::mem::size_of; use std::os::raw::c_void; -use std::ptr::{copy_nonoverlapping as memcpy, slice_from_raw_parts}; +use std::ptr::copy_nonoverlapping as memcpy; use std::time::Instant; use anyhow::{anyhow, Result}; @@ -28,8 +28,9 @@ use vulkanalia::prelude::v1_0::*; use vulkanalia::window as vk_window; use vulkanalia::Version; use winit::dpi::LogicalSize; -use winit::event::{ElementState, Event, VirtualKeyCode, WindowEvent}; -use winit::event_loop::{ControlFlow, EventLoop}; +use winit::event::{ElementState, Event, WindowEvent}; +use winit::event_loop::EventLoop; +use winit::keyboard::{KeyCode, PhysicalKey}; use winit::window::{Window, WindowBuilder}; use vulkanalia::vk::ExtDebugUtilsExtension; @@ -59,7 +60,7 @@ fn main() -> Result<()> { // Window - let event_loop = EventLoop::new(); + let event_loop = EventLoop::new()?; let window = WindowBuilder::new() .with_title("Vulkan Tutorial (Rust)") .with_inner_size(LogicalSize::new(1024, 768)) @@ -68,41 +69,47 @@ fn main() -> Result<()> { // App let mut app = unsafe { App::create(&window)? }; - let mut destroying = false; let mut minimized = false; - event_loop.run(move |event, _, control_flow| { - *control_flow = ControlFlow::Poll; + event_loop.run(move |event, elwt| { match event { - // Render a frame if our Vulkan app is not being destroyed. - Event::MainEventsCleared if !destroying && !minimized => unsafe { app.render(&window) }.unwrap(), - // Mark the window as having been resized. - Event::WindowEvent { event: WindowEvent::Resized(size), .. } => { - if size.width == 0 || size.height == 0 { - minimized = true; - } else { - minimized = false; - app.resized = true; + // Request a redraw when all events were processed. + Event::AboutToWait => window.request_redraw(), + Event::WindowEvent { event, .. } => match event { + // Render a frame if our Vulkan app is not being destroyed. + WindowEvent::RedrawRequested if !elwt.exiting() && !minimized => { + unsafe { app.render(&window) }.unwrap(); + }, + // Mark the window as having been resized. + WindowEvent::Resized(size) => { + if size.width == 0 || size.height == 0 { + minimized = true; + } else { + minimized = false; + app.resized = true; + } } - } - // Destroy our Vulkan app. - Event::WindowEvent { event: WindowEvent::CloseRequested, .. } => { - destroying = true; - *control_flow = ControlFlow::Exit; - unsafe { app.destroy(); } - } - // Handle keyboard events. - Event::WindowEvent { event: WindowEvent::KeyboardInput { input, .. }, .. } => { - if input.state == ElementState::Pressed { - match input.virtual_keycode { - Some(VirtualKeyCode::Left) if app.models > 1 => app.models -= 1, - Some(VirtualKeyCode::Right) if app.models < 4 => app.models += 1, - _ => { } + // Destroy our Vulkan app. + WindowEvent::CloseRequested => { + elwt.exit(); + unsafe { app.destroy(); } + } + // Handle keyboard events. + WindowEvent::KeyboardInput { event, .. } => { + if event.state == ElementState::Pressed { + match event.physical_key { + PhysicalKey::Code(KeyCode::ArrowLeft) if app.models > 1 => app.models -= 1, + PhysicalKey::Code(KeyCode::ArrowRight) if app.models < 4 => app.models += 1, + _ => { } + } } } + _ => {} } _ => {} } - }); + })?; + + Ok(()) } /// Our Vulkan app. @@ -164,12 +171,11 @@ impl App { unsafe fn render(&mut self, window: &Window) -> Result<()> { let in_flight_fence = self.data.in_flight_fences[self.frame]; - self.device - .wait_for_fences(&[in_flight_fence], true, u64::max_value())?; + self.device.wait_for_fences(&[in_flight_fence], true, u64::MAX)?; let result = self.device.acquire_next_image_khr( self.data.swapchain, - u64::max_value(), + u64::MAX, self.data.image_available_semaphores[self.frame], vk::Fence::null(), ); @@ -182,8 +188,7 @@ impl App { let image_in_flight = self.data.images_in_flight[image_index]; if !image_in_flight.is_null() { - self.device - .wait_for_fences(&[image_in_flight], true, u64::max_value())?; + self.device.wait_for_fences(&[image_in_flight], true, u64::MAX)?; } self.data.images_in_flight[image_index] = in_flight_fence; @@ -312,10 +317,7 @@ impl App { Deg(90.0) * time ); - let model_bytes = &*slice_from_raw_parts( - &model as *const Mat4 as *const u8, - size_of::() - ); + let model_bytes = std::slice::from_raw_parts(&model as *const Mat4 as *const u8, size_of::()); let opacity = (model_index + 1) as f32 * 0.25; let opacity_bytes = &opacity.to_ne_bytes()[..]; @@ -609,7 +611,11 @@ unsafe fn create_instance(window: &Window, entry: &Entry, data: &mut AppData) -> let mut debug_info = vk::DebugUtilsMessengerCreateInfoEXT::builder() .message_severity(vk::DebugUtilsMessageSeverityFlagsEXT::all()) - .message_type(vk::DebugUtilsMessageTypeFlagsEXT::all()) + .message_type( + vk::DebugUtilsMessageTypeFlagsEXT::GENERAL + | vk::DebugUtilsMessageTypeFlagsEXT::VALIDATION + | vk::DebugUtilsMessageTypeFlagsEXT::PERFORMANCE, + ) .user_callback(Some(debug_callback)); if VALIDATION_ENABLED { @@ -863,22 +869,19 @@ fn get_swapchain_present_mode(present_modes: &[vk::PresentModeKHR]) -> vk::Prese .unwrap_or(vk::PresentModeKHR::FIFO) } +#[rustfmt::skip] fn get_swapchain_extent(window: &Window, capabilities: vk::SurfaceCapabilitiesKHR) -> vk::Extent2D { - if capabilities.current_extent.width != u32::max_value() { + if capabilities.current_extent.width != u32::MAX { capabilities.current_extent } else { - let size = window.inner_size(); - let clamp = |min: u32, max: u32, v: u32| min.max(max.min(v)); vk::Extent2D::builder() - .width(clamp( + .width(window.inner_size().width.clamp( capabilities.min_image_extent.width, capabilities.max_image_extent.width, - size.width, )) - .height(clamp( + .height(window.inner_size().height.clamp( capabilities.min_image_extent.height, capabilities.max_image_extent.height, - size.height, )) .build() } diff --git a/src/manage.py b/src/manage.py index 76de227..42bc992 100755 --- a/src/manage.py +++ b/src/manage.py @@ -42,7 +42,7 @@ def apply_patch(patch, target): except: pass try: os.remove(f"{target}.rej") except: pass - + if code != 0: sys.exit(code) From d73c570a906b62a46edb6200dbbd4ff7ceb72583 Mon Sep 17 00:00:00 2001 From: ICEYSELF Date: Mon, 9 Sep 2024 21:33:12 +0800 Subject: [PATCH 3/3] Update rest commit hashes --- book/src/conclusion.md | 2 +- book/src/drawing/command_buffers.md | 2 +- book/src/drawing/framebuffers.md | 2 +- book/src/drawing/rendering_and_presentation.md | 2 +- book/src/dynamic/recycling_command_buffers.md | 2 +- book/src/model/depth_buffering.md | 2 +- book/src/model/loading_models.md | 2 +- book/src/overview.md | 2 +- book/src/pipeline/conclusion.md | 2 +- book/src/pipeline/introduction.md | 2 +- book/src/pipeline/render_passes.md | 2 +- book/src/presentation/image_views.md | 2 +- book/src/quality/generating_mipmaps.md | 2 +- book/src/quality/multisampling.md | 2 +- book/src/setup/instance.md | 2 +- book/src/setup/logical_device_and_queues.md | 2 +- book/src/setup/physical_devices_and_queue_families.md | 2 +- book/src/texture/combined_image_sampler.md | 2 +- book/src/texture/image_view_and_sampler.md | 2 +- book/src/texture/images.md | 2 +- book/src/uniform/descriptor_pool_and_sets.md | 2 +- book/src/uniform/descriptor_set_layout_and_buffer.md | 2 +- book/src/vertex/index_buffer.md | 2 +- book/src/vertex/staging_buffer.md | 2 +- book/src/vertex/vertex_input_description.md | 2 +- 25 files changed, 25 insertions(+), 25 deletions(-) diff --git a/book/src/conclusion.md b/book/src/conclusion.md index 1c612af..8644f78 100644 --- a/book/src/conclusion.md +++ b/book/src/conclusion.md @@ -2,7 +2,7 @@ > 原文链接: > -> Commit Hash: ceb4a3fc6d8ca565af4f8679c4889bcad7941338 +> Commit Hash: 7becee96b0029bf721f833039c00ea2a417714dd 一路千辛万苦,我们终于有了一个基本的 Vulkan 程序。你现在已经掌握了 Vulkan 的基本原理,可以开始探索更多的特性了,比如: * 实例化渲染 diff --git a/book/src/drawing/command_buffers.md b/book/src/drawing/command_buffers.md index ff010d4..172b7e4 100644 --- a/book/src/drawing/command_buffers.md +++ b/book/src/drawing/command_buffers.md @@ -2,7 +2,7 @@ > 原文链接: > -> Commit Hash: ceb4a3fc6d8ca565af4f8679c4889bcad7941338 +> Commit Hash: 7becee96b0029bf721f833039c00ea2a417714dd **本章代码:**[main.rs](https://github.com/chuigda/Vulkan-Tutorial-Rust-CN/tree/master/src/14_command_buffers.rs) diff --git a/book/src/drawing/framebuffers.md b/book/src/drawing/framebuffers.md index 50d34af..bc1a55f 100644 --- a/book/src/drawing/framebuffers.md +++ b/book/src/drawing/framebuffers.md @@ -2,7 +2,7 @@ > 原文链接: > -> Commit Hash: ceb4a3fc6d8ca565af4f8679c4889bcad7941338 +> Commit Hash: 7becee96b0029bf721f833039c00ea2a417714dd **本章代码:**[main.rs](https://github.com/chuigda/Vulkan-Tutorial-Rust-CN/tree/master/src/13_framebuffers.rs) diff --git a/book/src/drawing/rendering_and_presentation.md b/book/src/drawing/rendering_and_presentation.md index 3e06a5b..3957072 100644 --- a/book/src/drawing/rendering_and_presentation.md +++ b/book/src/drawing/rendering_and_presentation.md @@ -2,7 +2,7 @@ > 原文链接: > -> Commit Hash: ceb4a3fc6d8ca565af4f8679c4889bcad7941338 +> Commit Hash: 7becee96b0029bf721f833039c00ea2a417714dd **本章代码:**[main.rs](https://github.com/chuigda/Vulkan-Tutorial-Rust-CN/tree/master/src/15_hello_triangle.rs) diff --git a/book/src/dynamic/recycling_command_buffers.md b/book/src/dynamic/recycling_command_buffers.md index cb1cd05..72a1a5e 100644 --- a/book/src/dynamic/recycling_command_buffers.md +++ b/book/src/dynamic/recycling_command_buffers.md @@ -2,7 +2,7 @@ > 原文链接: > -> Commit Hash: ceb4a3fc6d8ca565af4f8679c4889bcad7941338 +> Commit Hash: 7becee96b0029bf721f833039c00ea2a417714dd > 前面没有这个声明的章节都是直接从 改编而来。

这一章和后面的章节都是原创,作者并不是 Vulkan 的专家。作者尽力保持了权威的语气,但是这些章节应该被视为一个 Vulkan 初学者的“尽力而为”。

如果你有问题、建议或者修正,请[提交 issue](https://github.com/KyleMayes/vulkanalia/issues)! diff --git a/book/src/model/depth_buffering.md b/book/src/model/depth_buffering.md index 5e3bf63..e4b0195 100644 --- a/book/src/model/depth_buffering.md +++ b/book/src/model/depth_buffering.md @@ -2,7 +2,7 @@ > 原文链接: > -> Commit Hash: ceb4a3fc6d8ca565af4f8679c4889bcad7941338 +> Commit Hash: 7becee96b0029bf721f833039c00ea2a417714dd **本章代码:**[main.rs](https://github.com/chuigda/Vulkan-Tutorial-Rust-CN/tree/master/src/26_depth_buffering.rs) | [shader.vert](https://github.com/chuigda/Vulkan-Tutorial-Rust-CN/tree/master/shaders/26/shader.vert) | [shader.frag](https://github.com/chuigda/Vulkan-Tutorial-Rust-CN/tree/master/shaders/26/shader.frag) diff --git a/book/src/model/loading_models.md b/book/src/model/loading_models.md index f18b80a..7380642 100644 --- a/book/src/model/loading_models.md +++ b/book/src/model/loading_models.md @@ -2,7 +2,7 @@ > 原文链接: > -> Commit Hash: ceb4a3fc6d8ca565af4f8679c4889bcad7941338 +> Commit Hash: 7becee96b0029bf721f833039c00ea2a417714dd **本章代码:**[main.rs](https://github.com/chuigda/Vulkan-Tutorial-Rust-CN/tree/master/src/27_model_loading.rs) diff --git a/book/src/overview.md b/book/src/overview.md index 7d724cf..523f3a1 100644 --- a/book/src/overview.md +++ b/book/src/overview.md @@ -2,7 +2,7 @@ > 原文链接: > -> Commit Hash: ceb4a3fc6d8ca565af4f8679c4889bcad7941338 +> Commit Hash: 7becee96b0029bf721f833039c00ea2a417714dd 本章会以介绍 Vulkan 和它所解决的问题开始。之后,我们会看到绘制第一个三角形所需的所有组件。这会给你一个总体的蓝图,以便你将每个后续章节放在正确的位置。之后我们会讨论 `vulkanalia` 提供的 Vulkan API。 diff --git a/book/src/pipeline/conclusion.md b/book/src/pipeline/conclusion.md index 5094406..77c0867 100644 --- a/book/src/pipeline/conclusion.md +++ b/book/src/pipeline/conclusion.md @@ -2,7 +2,7 @@ > 原文链接: > -> Commit Hash: ceb4a3fc6d8ca565af4f8679c4889bcad7941338 +> Commit Hash: 7becee96b0029bf721f833039c00ea2a417714dd **本章代码:**[main.rs](https://github.com/chuigda/Vulkan-Tutorial-Rust-CN/tree/master/src/12_graphics_pipeline_complete.rs) diff --git a/book/src/pipeline/introduction.md b/book/src/pipeline/introduction.md index 1e59034..92d22b6 100644 --- a/book/src/pipeline/introduction.md +++ b/book/src/pipeline/introduction.md @@ -2,7 +2,7 @@ > 原文链接: > -> Commit Hash: ceb4a3fc6d8ca565af4f8679c4889bcad7941338 +> Commit Hash: 7becee96b0029bf721f833039c00ea2a417714dd **本章代码:**[main.rs](https://github.com/chuigda/Vulkan-Tutorial-Rust-CN/tree/master/src/08_graphics_pipeline.rs) diff --git a/book/src/pipeline/render_passes.md b/book/src/pipeline/render_passes.md index 32ffa70..c322280 100644 --- a/book/src/pipeline/render_passes.md +++ b/book/src/pipeline/render_passes.md @@ -2,7 +2,7 @@ > 原文链接: > -> Commit Hash: ceb4a3fc6d8ca565af4f8679c4889bcad7941338 +> Commit Hash: 7becee96b0029bf721f833039c00ea2a417714dd **本章代码** [main.rs](https://github.com/chuigda/Vulkan-Tutorial-Rust-CN/tree/master/src/11_render_passes.rs) diff --git a/book/src/presentation/image_views.md b/book/src/presentation/image_views.md index 1548308..80377a7 100644 --- a/book/src/presentation/image_views.md +++ b/book/src/presentation/image_views.md @@ -2,7 +2,7 @@ > 原文链接: > -> Commit Hash: ceb4a3fc6d8ca565af4f8679c4889bcad7941338 +> Commit Hash: 7becee96b0029bf721f833039c00ea2a417714dd **本章代码:**[main.rs](https://github.com/chuigda/Vulkan-Tutorial-Rust-CN/tree/master/src/07_image_views.rs) diff --git a/book/src/quality/generating_mipmaps.md b/book/src/quality/generating_mipmaps.md index 1d10632..fdff6fe 100644 --- a/book/src/quality/generating_mipmaps.md +++ b/book/src/quality/generating_mipmaps.md @@ -2,7 +2,7 @@ > 原文链接: > -> Commit Hash: ceb4a3fc6d8ca565af4f8679c4889bcad7941338 +> Commit Hash: 7becee96b0029bf721f833039c00ea2a417714dd **本章代码:**[main.rs](https://github.com/chuigda/Vulkan-Tutorial-Rust-CN/tree/master/src/28_mipmapping.rs) diff --git a/book/src/quality/multisampling.md b/book/src/quality/multisampling.md index 691537b..eac5189 100644 --- a/book/src/quality/multisampling.md +++ b/book/src/quality/multisampling.md @@ -2,7 +2,7 @@ > 原文链接: > -> Commit Hash: ceb4a3fc6d8ca565af4f8679c4889bcad7941338 +> Commit Hash: 7becee96b0029bf721f833039c00ea2a417714dd **本章代码:**[main.rs](https://github.com/chuigda/Vulkan-Tutorial-Rust-CN/tree/master/src/29_multisampling.rs) diff --git a/book/src/setup/instance.md b/book/src/setup/instance.md index 06468db..5303603 100644 --- a/book/src/setup/instance.md +++ b/book/src/setup/instance.md @@ -2,7 +2,7 @@ > 原文链接: > -> Commit Hash: ceb4a3fc6d8ca565af4f8679c4889bcad7941338 +> Commit Hash: 7becee96b0029bf721f833039c00ea2a417714dd **本章代码:**[main.rs](https://github.com/chuigda/Vulkan-Tutorial-Rust-CN/tree/master/src/01_instance_creation.rs) diff --git a/book/src/setup/logical_device_and_queues.md b/book/src/setup/logical_device_and_queues.md index f09ab4c..6fc4dc1 100644 --- a/book/src/setup/logical_device_and_queues.md +++ b/book/src/setup/logical_device_and_queues.md @@ -2,7 +2,7 @@ > 原文链接: > -> Commit Hash: ceb4a3fc6d8ca565af4f8679c4889bcad7941338 +> Commit Hash: 7becee96b0029bf721f833039c00ea2a417714dd **本章代码:**[main.rs](https://github.com/chuigda/Vulkan-Tutorial-Rust-CN/tree/master/src/04_logical_device.rs) diff --git a/book/src/setup/physical_devices_and_queue_families.md b/book/src/setup/physical_devices_and_queue_families.md index 2fcecfb..8e93aed 100644 --- a/book/src/setup/physical_devices_and_queue_families.md +++ b/book/src/setup/physical_devices_and_queue_families.md @@ -2,7 +2,7 @@ > 原文链接: > -> Commit Hash: ceb4a3fc6d8ca565af4f8679c4889bcad7941338 +> Commit Hash: 7becee96b0029bf721f833039c00ea2a417714dd **本章代码:**[main.rs](https://github.com/chuigda/Vulkan-Tutorial-Rust-CN/tree/master/src/03_physical_device_selection.rs) diff --git a/book/src/texture/combined_image_sampler.md b/book/src/texture/combined_image_sampler.md index a9d6e17..5f2057b 100644 --- a/book/src/texture/combined_image_sampler.md +++ b/book/src/texture/combined_image_sampler.md @@ -2,7 +2,7 @@ > 原文链接: > -> Commit Hash: ceb4a3fc6d8ca565af4f8679c4889bcad7941338 +> Commit Hash: 7becee96b0029bf721f833039c00ea2a417714dd **本章代码:**[main.rs](https://github.com/chuigda/Vulkan-Tutorial-Rust-CN/tree/master/src/25_texture_mapping.rs) | [shader.vert](https://github.com/chuigda/Vulkan-Tutorial-Rust-CN/tree/master/shaders/25/shader.vert) | [shader.frag](https://github.com/chuigda/Vulkan-Tutorial-Rust-CN/tree/master/shaders/25/shader.frag) diff --git a/book/src/texture/image_view_and_sampler.md b/book/src/texture/image_view_and_sampler.md index f67e4ad..40081fa 100644 --- a/book/src/texture/image_view_and_sampler.md +++ b/book/src/texture/image_view_and_sampler.md @@ -2,7 +2,7 @@ > 原文链接: > -> Commit Hash: ceb4a3fc6d8ca565af4f8679c4889bcad7941338 +> Commit Hash: 7becee96b0029bf721f833039c00ea2a417714dd **本章代码:**[main.rs](https://github.com/chuigda/Vulkan-Tutorial-Rust-CN/tree/master/src/24_sampler.rs) diff --git a/book/src/texture/images.md b/book/src/texture/images.md index b6a51d2..2bf0207 100644 --- a/book/src/texture/images.md +++ b/book/src/texture/images.md @@ -2,7 +2,7 @@ > 原文链接: > -> Commit Hash: ceb4a3fc6d8ca565af4f8679c4889bcad7941338 +> Commit Hash: 7becee96b0029bf721f833039c00ea2a417714dd **本章代码:**[main.rs](https://github.com/chuigda/Vulkan-Tutorial-Rust-CN/tree/master/src/23_texture_image.rs) diff --git a/book/src/uniform/descriptor_pool_and_sets.md b/book/src/uniform/descriptor_pool_and_sets.md index 239097f..8c609b6 100644 --- a/book/src/uniform/descriptor_pool_and_sets.md +++ b/book/src/uniform/descriptor_pool_and_sets.md @@ -2,7 +2,7 @@ > 原文链接: > -> Commit Hash: ceb4a3fc6d8ca565af4f8679c4889bcad7941338 +> Commit Hash: 7becee96b0029bf721f833039c00ea2a417714dd **本章代码:**[main.rs](https://github.com/chuigda/Vulkan-Tutorial-Rust-CN/tree/master/src/22_descriptor_sets.rs) diff --git a/book/src/uniform/descriptor_set_layout_and_buffer.md b/book/src/uniform/descriptor_set_layout_and_buffer.md index a1a75c4..70bff6c 100644 --- a/book/src/uniform/descriptor_set_layout_and_buffer.md +++ b/book/src/uniform/descriptor_set_layout_and_buffer.md @@ -2,7 +2,7 @@ > 原文链接: > -> Commit Hash: ceb4a3fc6d8ca565af4f8679c4889bcad7941338 +> Commit Hash: 7becee96b0029bf721f833039c00ea2a417714dd **本章代码:**[main.rs](https://github.com/chuigda/Vulkan-Tutorial-Rust-CN/tree/master/src/21_descriptor_set_layout.rs) | [shader.vert](https://github.com/chuigda/Vulkan-Tutorial-Rust-CN/tree/master/shaders/21/shader.vert) | [shader.frag](https://github.com/chuigda/Vulkan-Tutorial-Rust-CN/tree/master/shaders/21/shader.frag) diff --git a/book/src/vertex/index_buffer.md b/book/src/vertex/index_buffer.md index 3f0646f..eb0ddd0 100644 --- a/book/src/vertex/index_buffer.md +++ b/book/src/vertex/index_buffer.md @@ -2,7 +2,7 @@ > 原文链接: > -> Commit Hash: ceb4a3fc6d8ca565af4f8679c4889bcad7941338 +> Commit Hash: 7becee96b0029bf721f833039c00ea2a417714dd **本章代码:**[main.rs](https://github.com/chuigda/Vulkan-Tutorial-Rust-CN/tree/master/src/20_index_buffer.rs) diff --git a/book/src/vertex/staging_buffer.md b/book/src/vertex/staging_buffer.md index 74df1a6..ff07f07 100644 --- a/book/src/vertex/staging_buffer.md +++ b/book/src/vertex/staging_buffer.md @@ -2,7 +2,7 @@ > 原文链接: > -> Commit Hash: ceb4a3fc6d8ca565af4f8679c4889bcad7941338 +> Commit Hash: 7becee96b0029bf721f833039c00ea2a417714dd **本章代码:**[main.rs](https://github.com/chuigda/Vulkan-Tutorial-Rust-CN/tree/master/src/19_staging_buffer.rs) diff --git a/book/src/vertex/vertex_input_description.md b/book/src/vertex/vertex_input_description.md index 0ef4188..bf44c0a 100644 --- a/book/src/vertex/vertex_input_description.md +++ b/book/src/vertex/vertex_input_description.md @@ -2,7 +2,7 @@ > 原文链接: > -> Commit Hash: ceb4a3fc6d8ca565af4f8679c4889bcad7941338 +> Commit Hash: 7becee96b0029bf721f833039c00ea2a417714dd **本章代码:**[main.rs](https://github.com/chuigda/Vulkan-Tutorial-Rust-CN/tree/master/src/17_vertex_input.rs) | [shader.vert](https://github.com/chuigda/Vulkan-Tutorial-Rust-CN/tree/master/shaders/17/shader.vert) | [shader.frag](https://github.com/chuigda/Vulkan-Tutorial-Rust-CN/tree/master/shaders/17/shader.frag)