-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvk_ctx.rs
More file actions
127 lines (108 loc) · 4.06 KB
/
vk_ctx.rs
File metadata and controls
127 lines (108 loc) · 4.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
use log::info;
use ash::extensions::ext::DebugUtils;
use ash::extensions::khr::Surface;
use ash::vk;
use ash::{self, extensions::khr::PushDescriptor};
use super::*;
use crate::app_timer::FrameIdx;
use crate::vk_utils::{execute_setup_cmd_buf, WithSetupCmdBuffer};
// TODO [CRITICAL] Check Vulkan validation layers: best practices, synchronization, portability, nsight-gpu-trace log
/** Kitchen sink for Vulkan stuff */
pub struct VkCtx {
// do not move these fields around, order matters for `Drop` trait
pub allocator: vma::Allocator,
pub device: VkCtxDevice,
pub instance: ash::Instance,
pub entry: ash::Entry,
// rest of fields
pub swapchain: VkCtxSwapchain,
pub swapchain_images: Vec<VkCtxSwapchainImage>,
pub command_pool: vk::CommandPool,
// Special command buffer used for resource init
pub setup_cb: vk::CommandBuffer,
pub pipeline_cache: vk::PipelineCache,
pub push_descriptor: PushDescriptor,
/// C'mon you will not use non-linear/nearest sampling anyway, can just create global objects..
pub default_texture_sampler_linear: vk::Sampler,
pub default_texture_sampler_nearest: vk::Sampler,
// surface
pub surface_loader: Surface,
pub surface_khr: vk::SurfaceKHR,
// debug
pub debug_utils: Option<(DebugUtils, vk::DebugUtilsMessengerEXT)>,
}
impl VkCtx {
pub fn swapchain_images_count(&self) -> usize {
self.swapchain_images.len()
}
pub fn window_size(&self) -> vk::Extent2D {
self.swapchain.size
}
pub fn vk_device(&self) -> &ash::Device {
&self.device.device
}
pub fn with_debug_loader(&self, callback: impl FnOnce(&DebugUtils)) {
self.debug_utils.as_ref().map(|dbg| callback(&dbg.0));
}
/// get next swapchain image
/// https://themaister.net/blog/2023/11/12/my-scuffed-game-streaming-adventure-pyrofling/
pub fn acquire_next_swapchain_image(&self, frame_idx: FrameIdx) -> (usize, &VkCtxSwapchainImage) {
// TODO [CRITICAL] handle image_index that is out of order.
// i.e. the image_view should be based on `swapchain_image_index`,
// not `frame_idx % (self.swapchain_images.len() as u64)`
let idx = frame_idx % (self.swapchain_images.len() as u64);
let swapchain_image = &self.swapchain_images[idx as usize];
let swapchain = &self.swapchain;
let swapchain_image_index: usize = unsafe {
// We *should* check the result for `VK_ERROR_OUT_OF_DATE_KHR`.
// Recreate swapchain if that happens (usually after window resize/minimize).
// Current code works on my PC so..
swapchain
.swapchain_loader
.acquire_next_image(
swapchain.swapchain,
u64::MAX,
swapchain_image.acquire_semaphore,
vk::Fence::null(),
)
.expect("Failed to acquire next swapchain image")
.0 as _
};
(swapchain_image_index, swapchain_image)
}
pub unsafe fn destroy(&mut self) {
let device = &self.device.device;
for obj in &self.swapchain_images {
obj.destroy(device);
}
device.destroy_command_pool(self.command_pool, None);
self.swapchain.destroy();
device.destroy_pipeline_cache(self.pipeline_cache, None);
self.surface_loader.destroy_surface(self.surface_khr, None);
device.destroy_sampler(self.default_texture_sampler_linear, None);
device.destroy_sampler(self.default_texture_sampler_nearest, None);
info!("VkCtx::destroy() finished. All app resources should be deleted. Only Device, Allocator and Instance remain.");
}
}
impl Drop for VkCtx {
fn drop(&mut self) {
unsafe {
match &self.debug_utils {
Some((loader, messanger)) => {
loader.destroy_debug_utils_messenger(*messanger, None);
}
_ => (),
}
// allocator and device are removed through `Drop` trait
// self.instance.destroy_instance(None); // ?
}
}
}
impl WithSetupCmdBuffer for VkCtx {
fn with_setup_cb(&self, callback: impl FnOnce(&ash::Device, vk::CommandBuffer)) {
let device = &self.device.device;
let queue = self.device.queue;
let cmd_buf = self.setup_cb;
unsafe { execute_setup_cmd_buf(device, queue, cmd_buf, callback) };
}
}