| 1 |
/*! |
| 2 |
# Vulkan API internals. |
| 3 |
|
| 4 |
## Stack memory |
| 5 |
|
| 6 |
Ash expects slices, which we don't generally have available. |
| 7 |
We cope with this requirement by the combination of the following ways: |
| 8 |
- temporarily allocating `Vec` on heap, where overhead is permitted |
| 9 |
- growing temporary local storage |
| 10 |
|
| 11 |
## Framebuffers and Render passes |
| 12 |
|
| 13 |
Render passes are cached on the device and kept forever. |
| 14 |
|
| 15 |
Framebuffers are also cached on the device, but they are removed when |
| 16 |
any of the image views (they have) gets removed. |
| 17 |
If Vulkan supports image-less framebuffers, |
| 18 |
then the actual views are excluded from the framebuffer key. |
| 19 |
|
| 20 |
## Fences |
| 21 |
|
| 22 |
If timeline semaphores are available, they are used 1:1 with wgpu-hal fences. |
| 23 |
Otherwise, we manage a pool of `VkFence` objects behind each `hal::Fence`. |
| 24 |
|
| 25 |
!*/ |
| 26 |
|
| 27 |
mod adapter; |
| 28 |
mod command; |
| 29 |
mod conv; |
| 30 |
mod device; |
| 31 |
mod drm; |
| 32 |
mod instance; |
| 33 |
mod sampler; |
| 34 |
|
| 35 |
use alloc::{boxed::Box, ffi::CString, sync::Arc, vec::Vec}; |
| 36 |
use core::{borrow::Borrow, ffi::CStr, fmt, mem, num::NonZeroU32, ops::DerefMut}; |
| 37 |
|
| 38 |
use arrayvec::ArrayVec; |
| 39 |
use ash::{ext, khr, vk}; |
| 40 |
use bytemuck::{Pod, Zeroable}; |
| 41 |
use hashbrown::HashSet; |
| 42 |
use parking_lot::{Mutex, RwLock}; |
| 43 |
|
| 44 |
use naga::FastHashMap; |
| 45 |
use wgt::InternalCounter; |
| 46 |
|
| 47 |
const MILLIS_TO_NANOS: u64 = 1_000_000; |
| 48 |
const MAX_TOTAL_ATTACHMENTS: usize = crate::MAX_COLOR_ATTACHMENTS * 2 + 1; |
| 49 |
|
| 50 |
#[derive(Clone, Debug)] |
| 51 |
pub struct Api; |
| 52 |
|
| 53 |
impl crate::Api for Api { |
| 54 |
type Instance = Instance; |
| 55 |
type Surface = Surface; |
| 56 |
type Adapter = Adapter; |
| 57 |
type Device = Device; |
| 58 |
|
| 59 |
type Queue = Queue; |
| 60 |
type CommandEncoder = CommandEncoder; |
| 61 |
type CommandBuffer = CommandBuffer; |
| 62 |
|
| 63 |
type Buffer = Buffer; |
| 64 |
type Texture = Texture; |
| 65 |
type SurfaceTexture = SurfaceTexture; |
| 66 |
type TextureView = TextureView; |
| 67 |
type Sampler = Sampler; |
| 68 |
type QuerySet = QuerySet; |
| 69 |
type Fence = Fence; |
| 70 |
type AccelerationStructure = AccelerationStructure; |
| 71 |
type PipelineCache = PipelineCache; |
| 72 |
|
| 73 |
type BindGroupLayout = BindGroupLayout; |
| 74 |
type BindGroup = BindGroup; |
| 75 |
type PipelineLayout = PipelineLayout; |
| 76 |
type ShaderModule = ShaderModule; |
| 77 |
type RenderPipeline = RenderPipeline; |
| 78 |
type ComputePipeline = ComputePipeline; |
| 79 |
} |
| 80 |
|
| 81 |
crate::impl_dyn_resource!( |
| 82 |
Adapter, |
| 83 |
AccelerationStructure, |
| 84 |
BindGroup, |
| 85 |
BindGroupLayout, |
| 86 |
Buffer, |
| 87 |
CommandBuffer, |
| 88 |
CommandEncoder, |
| 89 |
ComputePipeline, |
| 90 |
Device, |
| 91 |
Fence, |
| 92 |
Instance, |
| 93 |
PipelineCache, |
| 94 |
PipelineLayout, |
| 95 |
QuerySet, |
| 96 |
Queue, |
| 97 |
RenderPipeline, |
| 98 |
Sampler, |
| 99 |
ShaderModule, |
| 100 |
Surface, |
| 101 |
SurfaceTexture, |
| 102 |
Texture, |
| 103 |
TextureView |
| 104 |
); |
| 105 |
|
| 106 |
struct DebugUtils { |
| 107 |
extension: ext::debug_utils::Instance, |
| 108 |
messenger: vk::DebugUtilsMessengerEXT, |
| 109 |
|
| 110 |
/// Owning pointer to the debug messenger callback user data. |
| 111 |
/// |
| 112 |
/// `InstanceShared::drop` destroys the debug messenger before |
| 113 |
/// dropping this, so the callback should never receive a dangling |
| 114 |
/// user data pointer. |
| 115 |
#[allow(dead_code)] |
| 116 |
callback_data: Box<DebugUtilsMessengerUserData>, |
| 117 |
} |
| 118 |
|
| 119 |
pub struct DebugUtilsCreateInfo { |
| 120 |
severity: vk::DebugUtilsMessageSeverityFlagsEXT, |
| 121 |
message_type: vk::DebugUtilsMessageTypeFlagsEXT, |
| 122 |
callback_data: Box<DebugUtilsMessengerUserData>, |
| 123 |
} |
| 124 |
|
| 125 |
#[derive(Debug)] |
| 126 |
/// The properties related to the validation layer needed for the |
| 127 |
/// DebugUtilsMessenger for their workarounds |
| 128 |
struct ValidationLayerProperties { |
| 129 |
/// Validation layer description, from `vk::LayerProperties`. |
| 130 |
layer_description: CString, |
| 131 |
|
| 132 |
/// Validation layer specification version, from `vk::LayerProperties`. |
| 133 |
layer_spec_version: u32, |
| 134 |
} |
| 135 |
|
| 136 |
/// User data needed by `instance::debug_utils_messenger_callback`. |
| 137 |
/// |
| 138 |
/// When we create the [`vk::DebugUtilsMessengerEXT`], the `pUserData` |
| 139 |
/// pointer refers to one of these values. |
| 140 |
#[derive(Debug)] |
| 141 |
pub struct DebugUtilsMessengerUserData { |
| 142 |
/// The properties related to the validation layer, if present |
| 143 |
validation_layer_properties: Option<ValidationLayerProperties>, |
| 144 |
|
| 145 |
/// If the OBS layer is present. OBS never increments the version of their layer, |
| 146 |
/// so there's no reason to have the version. |
| 147 |
has_obs_layer: bool, |
| 148 |
} |
| 149 |
|
| 150 |
pub struct InstanceShared { |
| 151 |
raw: ash::Instance, |
| 152 |
extensions: Vec<&'static CStr>, |
| 153 |
drop_guard: Option<crate::DropGuard>, |
| 154 |
flags: wgt::InstanceFlags, |
| 155 |
memory_budget_thresholds: wgt::MemoryBudgetThresholds, |
| 156 |
debug_utils: Option<DebugUtils>, |
| 157 |
get_physical_device_properties: Option<khr::get_physical_device_properties2::Instance>, |
| 158 |
entry: ash::Entry, |
| 159 |
has_nv_optimus: bool, |
| 160 |
android_sdk_version: u32, |
| 161 |
/// The instance API version. |
| 162 |
/// |
| 163 |
/// Which is the version of Vulkan supported for instance-level functionality. |
| 164 |
/// |
| 165 |
/// It is associated with a `VkInstance` and its children, |
| 166 |
/// except for a `VkPhysicalDevice` and its children. |
| 167 |
instance_api_version: u32, |
| 168 |
} |
| 169 |
|
| 170 |
pub struct Instance { |
| 171 |
shared: Arc<InstanceShared>, |
| 172 |
} |
| 173 |
|
| 174 |
/// The semaphores needed to use one image in a swapchain. |
| 175 |
#[derive(Debug)] |
| 176 |
struct SwapchainImageSemaphores { |
| 177 |
/// A semaphore that is signaled when this image is safe for us to modify. |
| 178 |
/// |
| 179 |
/// When [`vkAcquireNextImageKHR`] returns the index of the next swapchain |
| 180 |
/// image that we should use, that image may actually still be in use by the |
| 181 |
/// presentation engine, and is not yet safe to modify. However, that |
| 182 |
/// function does accept a semaphore that it will signal when the image is |
| 183 |
/// indeed safe to begin messing with. |
| 184 |
/// |
| 185 |
/// This semaphore is: |
| 186 |
/// |
| 187 |
/// - waited for by the first queue submission to operate on this image |
| 188 |
/// since it was acquired, and |
| 189 |
/// |
| 190 |
/// - signaled by [`vkAcquireNextImageKHR`] when the acquired image is ready |
| 191 |
/// for us to use. |
| 192 |
/// |
| 193 |
/// [`vkAcquireNextImageKHR`]: https://registry.khronos.org/vulkan/specs/1.3-extensions/html/vkspec.html#vkAcquireNextImageKHR |
| 194 |
acquire: vk::Semaphore, |
| 195 |
|
| 196 |
/// True if the next command submission operating on this image should wait |
| 197 |
/// for [`acquire`]. |
| 198 |
/// |
| 199 |
/// We must wait for `acquire` before drawing to this swapchain image, but |
| 200 |
/// because `wgpu-hal` queue submissions are always strongly ordered, only |
| 201 |
/// the first submission that works with a swapchain image actually needs to |
| 202 |
/// wait. We set this flag when this image is acquired, and clear it the |
| 203 |
/// first time it's passed to [`Queue::submit`] as a surface texture. |
| 204 |
/// |
| 205 |
/// [`acquire`]: SwapchainImageSemaphores::acquire |
| 206 |
/// [`Queue::submit`]: crate::Queue::submit |
| 207 |
should_wait_for_acquire: bool, |
| 208 |
|
| 209 |
/// A pool of semaphores for ordering presentation after drawing. |
| 210 |
/// |
| 211 |
/// The first [`present_index`] semaphores in this vector are: |
| 212 |
/// |
| 213 |
/// - all waited on by the call to [`vkQueuePresentKHR`] that presents this |
| 214 |
/// image, and |
| 215 |
/// |
| 216 |
/// - each signaled by some [`vkQueueSubmit`] queue submission that draws to |
| 217 |
/// this image, when the submission finishes execution. |
| 218 |
/// |
| 219 |
/// This vector accumulates one semaphore per submission that writes to this |
| 220 |
/// image. This is awkward, but hard to avoid: [`vkQueuePresentKHR`] |
| 221 |
/// requires a semaphore to order it with respect to drawing commands, and |
| 222 |
/// we can't attach new completion semaphores to a command submission after |
| 223 |
/// it's been submitted. This means that, at submission time, we must create |
| 224 |
/// the semaphore we might need if the caller's next action is to enqueue a |
| 225 |
/// presentation of this image. |
| 226 |
/// |
| 227 |
/// An alternative strategy would be for presentation to enqueue an empty |
| 228 |
/// submit, ordered relative to other submits in the usual way, and |
| 229 |
/// signaling a single presentation semaphore. But we suspect that submits |
| 230 |
/// are usually expensive enough, and semaphores usually cheap enough, that |
| 231 |
/// performance-sensitive users will avoid making many submits, so that the |
| 232 |
/// cost of accumulated semaphores will usually be less than the cost of an |
| 233 |
/// additional submit. |
| 234 |
/// |
| 235 |
/// Only the first [`present_index`] semaphores in the vector are actually |
| 236 |
/// going to be signalled by submitted commands, and need to be waited for |
| 237 |
/// by the next present call. Any semaphores beyond that index were created |
| 238 |
/// for prior presents and are simply being retained for recycling. |
| 239 |
/// |
| 240 |
/// [`present_index`]: SwapchainImageSemaphores::present_index |
| 241 |
/// [`vkQueuePresentKHR`]: https://registry.khronos.org/vulkan/specs/1.3-extensions/html/vkspec.html#vkQueuePresentKHR |
| 242 |
/// [`vkQueueSubmit`]: https://registry.khronos.org/vulkan/specs/1.3-extensions/html/vkspec.html#vkQueueSubmit |
| 243 |
present: Vec<vk::Semaphore>, |
| 244 |
|
| 245 |
/// The number of semaphores in [`present`] to be signalled for this submission. |
| 246 |
/// |
| 247 |
/// [`present`]: SwapchainImageSemaphores::present |
| 248 |
present_index: usize, |
| 249 |
|
| 250 |
/// The fence value of the last command submission that wrote to this image. |
| 251 |
/// |
| 252 |
/// The next time we try to acquire this image, we'll block until |
| 253 |
/// this submission finishes, proving that [`acquire`] is ready to |
| 254 |
/// pass to `vkAcquireNextImageKHR` again. |
| 255 |
/// |
| 256 |
/// [`acquire`]: SwapchainImageSemaphores::acquire |
| 257 |
previously_used_submission_index: crate::FenceValue, |
| 258 |
} |
| 259 |
|
| 260 |
impl SwapchainImageSemaphores { |
| 261 |
fn new(device: &DeviceShared) -> Result<Self, crate::DeviceError> { |
| 262 |
Ok(Self { |
| 263 |
acquire: device.new_binary_semaphore()?, |
| 264 |
should_wait_for_acquire: true, |
| 265 |
present: Vec::new(), |
| 266 |
present_index: 0, |
| 267 |
previously_used_submission_index: 0, |
| 268 |
}) |
| 269 |
} |
| 270 |
|
| 271 |
fn set_used_fence_value(&mut self, value: crate::FenceValue) { |
| 272 |
self.previously_used_submission_index = value; |
| 273 |
} |
| 274 |
|
| 275 |
/// Return the semaphore that commands drawing to this image should wait for, if any. |
| 276 |
/// |
| 277 |
/// This only returns `Some` once per acquisition; see |
| 278 |
/// [`SwapchainImageSemaphores::should_wait_for_acquire`] for details. |
| 279 |
fn get_acquire_wait_semaphore(&mut self) -> Option<vk::Semaphore> { |
| 280 |
if self.should_wait_for_acquire { |
| 281 |
self.should_wait_for_acquire = false; |
| 282 |
Some(self.acquire) |
| 283 |
} else { |
| 284 |
None |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
/// Return a semaphore that a submission that writes to this image should |
| 289 |
/// signal when it's done. |
| 290 |
/// |
| 291 |
/// See [`SwapchainImageSemaphores::present`] for details. |
| 292 |
fn get_submit_signal_semaphore( |
| 293 |
&mut self, |
| 294 |
device: &DeviceShared, |
| 295 |
) -> Result<vk::Semaphore, crate::DeviceError> { |
| 296 |
// Try to recycle a semaphore we created for a previous presentation. |
| 297 |
let sem = match self.present.get(self.present_index) { |
| 298 |
Some(sem) => *sem, |
| 299 |
None => { |
| 300 |
let sem = device.new_binary_semaphore()?; |
| 301 |
self.present.push(sem); |
| 302 |
sem |
| 303 |
} |
| 304 |
}; |
| 305 |
|
| 306 |
self.present_index += 1; |
| 307 |
|
| 308 |
Ok(sem) |
| 309 |
} |
| 310 |
|
| 311 |
/// Return the semaphores that a presentation of this image should wait on. |
| 312 |
/// |
| 313 |
/// Return a slice of semaphores that the call to [`vkQueueSubmit`] that |
| 314 |
/// ends this image's acquisition should wait for. See |
| 315 |
/// [`SwapchainImageSemaphores::present`] for details. |
| 316 |
/// |
| 317 |
/// Reset `self` to be ready for the next acquisition cycle. |
| 318 |
/// |
| 319 |
/// [`vkQueueSubmit`]: https://registry.khronos.org/vulkan/specs/1.3-extensions/html/vkspec.html#vkQueueSubmit |
| 320 |
fn get_present_wait_semaphores(&mut self) -> &[vk::Semaphore] { |
| 321 |
let old_index = self.present_index; |
| 322 |
|
| 323 |
// Since this marks the end of this acquire/draw/present cycle, take the |
| 324 |
// opportunity to reset `self` in preparation for the next acquisition. |
| 325 |
self.present_index = 0; |
| 326 |
self.should_wait_for_acquire = true; |
| 327 |
|
| 328 |
&self.present[0..old_index] |
| 329 |
} |
| 330 |
|
| 331 |
unsafe fn destroy(&self, device: &ash::Device) { |
| 332 |
unsafe { |
| 333 |
device.destroy_semaphore(self.acquire, None); |
| 334 |
for sem in &self.present { |
| 335 |
device.destroy_semaphore(*sem, None); |
| 336 |
} |
| 337 |
} |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
struct Swapchain { |
| 342 |
raw: vk::SwapchainKHR, |
| 343 |
functor: khr::swapchain::Device, |
| 344 |
device: Arc<DeviceShared>, |
| 345 |
images: Vec<vk::Image>, |
| 346 |
config: crate::SurfaceConfiguration, |
| 347 |
/// One wait semaphore per swapchain image. This will be associated with the |
| 348 |
/// surface texture, and later collected during submission. |
| 349 |
/// |
| 350 |
/// We need this to be `Arc<Mutex<>>` because we need to be able to pass this |
| 351 |
/// data into the surface texture, so submit/present can use it. |
| 352 |
surface_semaphores: Vec<Arc<Mutex<SwapchainImageSemaphores>>>, |
| 353 |
/// The index of the next semaphore to use. Ideally we would use the same |
| 354 |
/// index as the image index, but we need to specify the semaphore as an argument |
| 355 |
/// to the acquire_next_image function which is what tells us which image to use. |
| 356 |
next_semaphore_index: usize, |
| 357 |
/// The present timing information which will be set in the next call to [`present()`](crate::Queue::present()). |
| 358 |
/// |
| 359 |
/// # Safety |
| 360 |
/// |
| 361 |
/// This must only be set if [`wgt::Features::VULKAN_GOOGLE_DISPLAY_TIMING`] is enabled, and |
| 362 |
/// so the VK_GOOGLE_display_timing extension is present. |
| 363 |
next_present_time: Option<vk::PresentTimeGOOGLE>, |
| 364 |
} |
| 365 |
|
| 366 |
impl Swapchain { |
| 367 |
fn advance_surface_semaphores(&mut self) { |
| 368 |
let semaphore_count = self.surface_semaphores.len(); |
| 369 |
self.next_semaphore_index = (self.next_semaphore_index + 1) % semaphore_count; |
| 370 |
} |
| 371 |
|
| 372 |
fn get_surface_semaphores(&self) -> Arc<Mutex<SwapchainImageSemaphores>> { |
| 373 |
self.surface_semaphores[self.next_semaphore_index].clone() |
| 374 |
} |
| 375 |
} |
| 376 |
|
| 377 |
pub struct Surface { |
| 378 |
raw: vk::SurfaceKHR, |
| 379 |
functor: khr::surface::Instance, |
| 380 |
instance: Arc<InstanceShared>, |
| 381 |
swapchain: RwLock<Option<Swapchain>>, |
| 382 |
} |
| 383 |
|
| 384 |
impl Surface { |
| 385 |
/// Get the raw Vulkan swapchain associated with this surface. |
| 386 |
/// |
| 387 |
/// Returns [`None`] if the surface is not configured. |
| 388 |
pub fn raw_swapchain(&self) -> Option<vk::SwapchainKHR> { |
| 389 |
let read = self.swapchain.read(); |
| 390 |
read.as_ref().map(|it| it.raw) |
| 391 |
} |
| 392 |
|
| 393 |
/// Set the present timing information which will be used for the next [presentation](crate::Queue::present()) of this surface, |
| 394 |
/// using [VK_GOOGLE_display_timing]. |
| 395 |
/// |
| 396 |
/// This can be used to give an id to presentations, for future use of [`vk::PastPresentationTimingGOOGLE`]. |
| 397 |
/// Note that `wgpu-hal` does *not* provide a way to use that API - you should manually access this through [`ash`]. |
| 398 |
/// |
| 399 |
/// This can also be used to add a "not before" timestamp to the presentation. |
| 400 |
/// |
| 401 |
/// The exact semantics of the fields are also documented in the [specification](https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/VkPresentTimeGOOGLE.html) for the extension. |
| 402 |
/// |
| 403 |
/// # Panics |
| 404 |
/// |
| 405 |
/// - If the surface hasn't been configured. |
| 406 |
/// - If the device doesn't [support present timing](wgt::Features::VULKAN_GOOGLE_DISPLAY_TIMING). |
| 407 |
/// |
| 408 |
/// [VK_GOOGLE_display_timing]: https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/VK_GOOGLE_display_timing.html |
| 409 |
#[track_caller] |
| 410 |
pub fn set_next_present_time(&self, present_timing: vk::PresentTimeGOOGLE) { |
| 411 |
let mut swapchain = self.swapchain.write(); |
| 412 |
let swapchain = swapchain |
| 413 |
.as_mut() |
| 414 |
.expect("Surface should have been configured"); |
| 415 |
let features = wgt::Features::VULKAN_GOOGLE_DISPLAY_TIMING; |
| 416 |
if swapchain.device.features.contains(features) { |
| 417 |
swapchain.next_present_time = Some(present_timing); |
| 418 |
} else { |
| 419 |
// Ideally we'd use something like `device.required_features` here, but that's in `wgpu-core`, which we are a dependency of |
| 420 |
panic!( |
| 421 |
concat!( |
| 422 |
"Tried to set display timing properties ", |
| 423 |
"without the corresponding feature ({:?}) enabled." |
| 424 |
), |
| 425 |
features |
| 426 |
); |
| 427 |
} |
| 428 |
} |
| 429 |
} |
| 430 |
|
| 431 |
#[derive(Debug)] |
| 432 |
pub struct SurfaceTexture { |
| 433 |
index: u32, |
| 434 |
texture: Texture, |
| 435 |
surface_semaphores: Arc<Mutex<SwapchainImageSemaphores>>, |
| 436 |
} |
| 437 |
|
| 438 |
impl crate::DynSurfaceTexture for SurfaceTexture {} |
| 439 |
|
| 440 |
impl Borrow<Texture> for SurfaceTexture { |
| 441 |
fn borrow(&self) -> &Texture { |
| 442 |
&self.texture |
| 443 |
} |
| 444 |
} |
| 445 |
|
| 446 |
impl Borrow<dyn crate::DynTexture> for SurfaceTexture { |
| 447 |
fn borrow(&self) -> &dyn crate::DynTexture { |
| 448 |
&self.texture |
| 449 |
} |
| 450 |
} |
| 451 |
|
| 452 |
pub struct Adapter { |
| 453 |
raw: vk::PhysicalDevice, |
| 454 |
instance: Arc<InstanceShared>, |
| 455 |
//queue_families: Vec<vk::QueueFamilyProperties>, |
| 456 |
known_memory_flags: vk::MemoryPropertyFlags, |
| 457 |
phd_capabilities: adapter::PhysicalDeviceProperties, |
| 458 |
phd_features: adapter::PhysicalDeviceFeatures, |
| 459 |
downlevel_flags: wgt::DownlevelFlags, |
| 460 |
private_caps: PrivateCapabilities, |
| 461 |
workarounds: Workarounds, |
| 462 |
} |
| 463 |
|
| 464 |
// TODO there's no reason why this can't be unified--the function pointers should all be the same--it's not clear how to do this with `ash`. |
| 465 |
enum ExtensionFn<T> { |
| 466 |
/// The loaded function pointer struct for an extension. |
| 467 |
Extension(T), |
| 468 |
/// The extension was promoted to a core version of Vulkan and the functions on `ash`'s `DeviceV1_x` traits should be used. |
| 469 |
Promoted, |
| 470 |
} |
| 471 |
|
| 472 |
struct DeviceExtensionFunctions { |
| 473 |
debug_utils: Option<ext::debug_utils::Device>, |
| 474 |
draw_indirect_count: Option<khr::draw_indirect_count::Device>, |
| 475 |
timeline_semaphore: Option<ExtensionFn<khr::timeline_semaphore::Device>>, |
| 476 |
ray_tracing: Option<RayTracingDeviceExtensionFunctions>, |
| 477 |
mesh_shading: Option<ext::mesh_shader::Device>, |
| 478 |
} |
| 479 |
|
| 480 |
struct RayTracingDeviceExtensionFunctions { |
| 481 |
acceleration_structure: khr::acceleration_structure::Device, |
| 482 |
buffer_device_address: khr::buffer_device_address::Device, |
| 483 |
} |
| 484 |
|
| 485 |
/// Set of internal capabilities, which don't show up in the exposed |
| 486 |
/// device geometry, but affect the code paths taken internally. |
| 487 |
#[derive(Clone, Debug)] |
| 488 |
struct PrivateCapabilities { |
| 489 |
image_view_usage: bool, |
| 490 |
timeline_semaphores: bool, |
| 491 |
texture_d24: bool, |
| 492 |
texture_d24_s8: bool, |
| 493 |
texture_s8: bool, |
| 494 |
/// Ability to present contents to any screen. Only needed to work around broken platform configurations. |
| 495 |
can_present: bool, |
| 496 |
non_coherent_map_mask: wgt::BufferAddress, |
| 497 |
|
| 498 |
/// True if this adapter advertises the [`robustBufferAccess`][vrba] feature. |
| 499 |
/// |
| 500 |
/// Note that Vulkan's `robustBufferAccess` is not sufficient to implement |
| 501 |
/// `wgpu_hal`'s guarantee that shaders will not access buffer contents via |
| 502 |
/// a given bindgroup binding outside that binding's [accessible |
| 503 |
/// region][ar]. Enabling `robustBufferAccess` does ensure that |
| 504 |
/// out-of-bounds reads and writes are not undefined behavior (that's good), |
| 505 |
/// but still permits out-of-bounds reads to return data from anywhere |
| 506 |
/// within the buffer, not just the accessible region. |
| 507 |
/// |
| 508 |
/// [ar]: ../struct.BufferBinding.html#accessible-region |
| 509 |
/// [vrba]: https://registry.khronos.org/vulkan/specs/1.3-extensions/html/vkspec.html#features-robustBufferAccess |
| 510 |
robust_buffer_access: bool, |
| 511 |
|
| 512 |
robust_image_access: bool, |
| 513 |
|
| 514 |
/// True if this adapter supports the [`VK_EXT_robustness2`] extension's |
| 515 |
/// [`robustBufferAccess2`] feature. |
| 516 |
/// |
| 517 |
/// This is sufficient to implement `wgpu_hal`'s [required bounds-checking][ar] of |
| 518 |
/// shader accesses to buffer contents. If this feature is not available, |
| 519 |
/// this backend must have Naga inject bounds checks in the generated |
| 520 |
/// SPIR-V. |
| 521 |
/// |
| 522 |
/// [`VK_EXT_robustness2`]: https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/VK_EXT_robustness2.html |
| 523 |
/// [`robustBufferAccess2`]: https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/VkPhysicalDeviceRobustness2FeaturesEXT.html#features-robustBufferAccess2 |
| 524 |
/// [ar]: ../struct.BufferBinding.html#accessible-region |
| 525 |
robust_buffer_access2: bool, |
| 526 |
|
| 527 |
robust_image_access2: bool, |
| 528 |
zero_initialize_workgroup_memory: bool, |
| 529 |
image_format_list: bool, |
| 530 |
maximum_samplers: u32, |
| 531 |
|
| 532 |
/// True if this adapter supports the [`VK_KHR_shader_integer_dot_product`] extension |
| 533 |
/// (promoted to Vulkan 1.3). |
| 534 |
/// |
| 535 |
/// This is used to generate optimized code for WGSL's `dot4{I, U}8Packed`. |
| 536 |
/// |
| 537 |
/// [`VK_KHR_shader_integer_dot_product`]: https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/VK_KHR_shader_integer_dot_product.html |
| 538 |
shader_integer_dot_product: bool, |
| 539 |
} |
| 540 |
|
| 541 |
bitflags::bitflags!( |
| 542 |
/// Workaround flags. |
| 543 |
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] |
| 544 |
pub struct Workarounds: u32 { |
| 545 |
/// Only generate SPIR-V for one entry point at a time. |
| 546 |
const SEPARATE_ENTRY_POINTS = 0x1; |
| 547 |
/// Qualcomm OOMs when there are zero color attachments but a non-null pointer |
| 548 |
/// to a subpass resolve attachment array. This nulls out that pointer in that case. |
| 549 |
const EMPTY_RESOLVE_ATTACHMENT_LISTS = 0x2; |
| 550 |
/// If the following code returns false, then nvidia will end up filling the wrong range. |
| 551 |
/// |
| 552 |
/// ```skip |
| 553 |
/// fn nvidia_succeeds() -> bool { |
| 554 |
/// # let (copy_length, start_offset) = (0, 0); |
| 555 |
/// if copy_length >= 4096 { |
| 556 |
/// if start_offset % 16 != 0 { |
| 557 |
/// if copy_length == 4096 { |
| 558 |
/// return true; |
| 559 |
/// } |
| 560 |
/// if copy_length % 16 == 0 { |
| 561 |
/// return false; |
| 562 |
/// } |
| 563 |
/// } |
| 564 |
/// } |
| 565 |
/// true |
| 566 |
/// } |
| 567 |
/// ``` |
| 568 |
/// |
| 569 |
/// As such, we need to make sure all calls to vkCmdFillBuffer are aligned to 16 bytes |
| 570 |
/// if they cover a range of 4096 bytes or more. |
| 571 |
const FORCE_FILL_BUFFER_WITH_SIZE_GREATER_4096_ALIGNED_OFFSET_16 = 0x4; |
| 572 |
} |
| 573 |
); |
| 574 |
|
| 575 |
#[derive(Clone, Debug, Eq, Hash, PartialEq)] |
| 576 |
struct AttachmentKey { |
| 577 |
format: vk::Format, |
| 578 |
layout: vk::ImageLayout, |
| 579 |
ops: crate::AttachmentOps, |
| 580 |
} |
| 581 |
|
| 582 |
impl AttachmentKey { |
| 583 |
/// Returns an attachment key for a compatible attachment. |
| 584 |
fn compatible(format: vk::Format, layout: vk::ImageLayout) -> Self { |
| 585 |
Self { |
| 586 |
format, |
| 587 |
layout, |
| 588 |
ops: crate::AttachmentOps::all(), |
| 589 |
} |
| 590 |
} |
| 591 |
} |
| 592 |
|
| 593 |
#[derive(Clone, Eq, Hash, PartialEq)] |
| 594 |
struct ColorAttachmentKey { |
| 595 |
base: AttachmentKey, |
| 596 |
resolve: Option<AttachmentKey>, |
| 597 |
} |
| 598 |
|
| 599 |
#[derive(Clone, Eq, Hash, PartialEq)] |
| 600 |
struct DepthStencilAttachmentKey { |
| 601 |
base: AttachmentKey, |
| 602 |
stencil_ops: crate::AttachmentOps, |
| 603 |
} |
| 604 |
|
| 605 |
#[derive(Clone, Eq, Default, Hash, PartialEq)] |
| 606 |
struct RenderPassKey { |
| 607 |
colors: ArrayVec<Option<ColorAttachmentKey>, { crate::MAX_COLOR_ATTACHMENTS }>, |
| 608 |
depth_stencil: Option<DepthStencilAttachmentKey>, |
| 609 |
sample_count: u32, |
| 610 |
multiview: Option<NonZeroU32>, |
| 611 |
} |
| 612 |
|
| 613 |
struct DeviceShared { |
| 614 |
raw: ash::Device, |
| 615 |
family_index: u32, |
| 616 |
queue_index: u32, |
| 617 |
raw_queue: vk::Queue, |
| 618 |
drop_guard: Option<crate::DropGuard>, |
| 619 |
instance: Arc<InstanceShared>, |
| 620 |
physical_device: vk::PhysicalDevice, |
| 621 |
enabled_extensions: Vec<&'static CStr>, |
| 622 |
extension_fns: DeviceExtensionFunctions, |
| 623 |
vendor_id: u32, |
| 624 |
pipeline_cache_validation_key: [u8; 16], |
| 625 |
timestamp_period: f32, |
| 626 |
private_caps: PrivateCapabilities, |
| 627 |
workarounds: Workarounds, |
| 628 |
features: wgt::Features, |
| 629 |
render_passes: Mutex<FastHashMap<RenderPassKey, vk::RenderPass>>, |
| 630 |
sampler_cache: Mutex<sampler::SamplerCache>, |
| 631 |
memory_allocations_counter: InternalCounter, |
| 632 |
} |
| 633 |
|
| 634 |
impl Drop for DeviceShared { |
| 635 |
fn drop(&mut self) { |
| 636 |
for &raw in self.render_passes.lock().values() { |
| 637 |
unsafe { self.raw.destroy_render_pass(raw, None) }; |
| 638 |
} |
| 639 |
if self.drop_guard.is_none() { |
| 640 |
unsafe { self.raw.destroy_device(None) }; |
| 641 |
} |
| 642 |
} |
| 643 |
} |
| 644 |
|
| 645 |
pub struct Device { |
| 646 |
shared: Arc<DeviceShared>, |
| 647 |
mem_allocator: Mutex<gpu_alloc::GpuAllocator<vk::DeviceMemory>>, |
| 648 |
desc_allocator: |
| 649 |
Mutex<gpu_descriptor::DescriptorAllocator<vk::DescriptorPool, vk::DescriptorSet>>, |
| 650 |
valid_ash_memory_types: u32, |
| 651 |
naga_options: naga::back::spv::Options<'static>, |
| 652 |
#[cfg(feature = "renderdoc")] |
| 653 |
render_doc: crate::auxil::renderdoc::RenderDoc, |
| 654 |
counters: Arc<wgt::HalCounters>, |
| 655 |
} |
| 656 |
|
| 657 |
impl Drop for Device { |
| 658 |
fn drop(&mut self) { |
| 659 |
unsafe { self.mem_allocator.lock().cleanup(&*self.shared) }; |
| 660 |
unsafe { self.desc_allocator.lock().cleanup(&*self.shared) }; |
| 661 |
} |
| 662 |
} |
| 663 |
|
| 664 |
/// Semaphores for forcing queue submissions to run in order. |
| 665 |
/// |
| 666 |
/// The [`wgpu_hal::Queue`] trait promises that if two calls to [`submit`] are |
| 667 |
/// ordered, then the first submission will finish on the GPU before the second |
| 668 |
/// submission begins. To get this behavior on Vulkan we need to pass semaphores |
| 669 |
/// to [`vkQueueSubmit`] for the commands to wait on before beginning execution, |
| 670 |
/// and to signal when their execution is done. |
| 671 |
/// |
| 672 |
/// Normally this can be done with a single semaphore, waited on and then |
| 673 |
/// signalled for each submission. At any given time there's exactly one |
| 674 |
/// submission that would signal the semaphore, and exactly one waiting on it, |
| 675 |
/// as Vulkan requires. |
| 676 |
/// |
| 677 |
/// However, as of Oct 2021, bug [#5508] in the Mesa ANV drivers caused them to |
| 678 |
/// hang if we use a single semaphore. The workaround is to alternate between |
| 679 |
/// two semaphores. The bug has been fixed in Mesa, but we should probably keep |
| 680 |
/// the workaround until, say, Oct 2026. |
| 681 |
/// |
| 682 |
/// [`wgpu_hal::Queue`]: crate::Queue |
| 683 |
/// [`submit`]: crate::Queue::submit |
| 684 |
/// [`vkQueueSubmit`]: https://registry.khronos.org/vulkan/specs/1.3-extensions/html/vkspec.html#vkQueueSubmit |
| 685 |
/// [#5508]: https://gitlab.freedesktop.org/mesa/mesa/-/issues/5508 |
| 686 |
#[derive(Clone)] |
| 687 |
struct RelaySemaphores { |
| 688 |
/// The semaphore the next submission should wait on before beginning |
| 689 |
/// execution on the GPU. This is `None` for the first submission, which |
| 690 |
/// should not wait on anything at all. |
| 691 |
wait: Option<vk::Semaphore>, |
| 692 |
|
| 693 |
/// The semaphore the next submission should signal when it has finished |
| 694 |
/// execution on the GPU. |
| 695 |
signal: vk::Semaphore, |
| 696 |
} |
| 697 |
|
| 698 |
impl RelaySemaphores { |
| 699 |
fn new(device: &DeviceShared) -> Result<Self, crate::DeviceError> { |
| 700 |
Ok(Self { |
| 701 |
wait: None, |
| 702 |
signal: device.new_binary_semaphore()?, |
| 703 |
}) |
| 704 |
} |
| 705 |
|
| 706 |
/// Advances the semaphores, returning the semaphores that should be used for a submission. |
| 707 |
fn advance(&mut self, device: &DeviceShared) -> Result<Self, crate::DeviceError> { |
| 708 |
let old = self.clone(); |
| 709 |
|
| 710 |
// Build the state for the next submission. |
| 711 |
match self.wait { |
| 712 |
None => { |
| 713 |
// The `old` values describe the first submission to this queue. |
| 714 |
// The second submission should wait on `old.signal`, and then |
| 715 |
// signal a new semaphore which we'll create now. |
| 716 |
self.wait = Some(old.signal); |
| 717 |
self.signal = device.new_binary_semaphore()?; |
| 718 |
} |
| 719 |
Some(ref mut wait) => { |
| 720 |
// What this submission signals, the next should wait. |
| 721 |
mem::swap(wait, &mut self.signal); |
| 722 |
} |
| 723 |
}; |
| 724 |
|
| 725 |
Ok(old) |
| 726 |
} |
| 727 |
|
| 728 |
/// Destroys the semaphores. |
| 729 |
unsafe fn destroy(&self, device: &ash::Device) { |
| 730 |
unsafe { |
| 731 |
if let Some(wait) = self.wait { |
| 732 |
device.destroy_semaphore(wait, None); |
| 733 |
} |
| 734 |
device.destroy_semaphore(self.signal, None); |
| 735 |
} |
| 736 |
} |
| 737 |
} |
| 738 |
|
| 739 |
pub struct Queue { |
| 740 |
raw: vk::Queue, |
| 741 |
swapchain_fn: khr::swapchain::Device, |
| 742 |
device: Arc<DeviceShared>, |
| 743 |
family_index: u32, |
| 744 |
relay_semaphores: Mutex<RelaySemaphores>, |
| 745 |
signal_semaphores: Mutex<(Vec<vk::Semaphore>, Vec<u64>)>, |
| 746 |
} |
| 747 |
|
| 748 |
impl Queue { |
| 749 |
pub fn as_raw(&self) -> vk::Queue { |
| 750 |
self.raw |
| 751 |
} |
| 752 |
} |
| 753 |
|
| 754 |
impl Drop for Queue { |
| 755 |
fn drop(&mut self) { |
| 756 |
unsafe { self.relay_semaphores.lock().destroy(&self.device.raw) }; |
| 757 |
} |
| 758 |
} |
| 759 |
|
| 760 |
#[derive(Debug)] |
| 761 |
pub struct Buffer { |
| 762 |
raw: vk::Buffer, |
| 763 |
block: Option<Mutex<gpu_alloc::MemoryBlock<vk::DeviceMemory>>>, |
| 764 |
} |
| 765 |
|
| 766 |
impl crate::DynBuffer for Buffer {} |
| 767 |
|
| 768 |
#[derive(Debug)] |
| 769 |
pub struct AccelerationStructure { |
| 770 |
raw: vk::AccelerationStructureKHR, |
| 771 |
buffer: vk::Buffer, |
| 772 |
block: Mutex<gpu_alloc::MemoryBlock<vk::DeviceMemory>>, |
| 773 |
compacted_size_query: Option<vk::QueryPool>, |
| 774 |
} |
| 775 |
|
| 776 |
impl crate::DynAccelerationStructure for AccelerationStructure {} |
| 777 |
|
| 778 |
#[derive(Debug)] |
| 779 |
pub struct Texture { |
| 780 |
raw: vk::Image, |
| 781 |
drop_guard: Option<crate::DropGuard>, |
| 782 |
external_memory: Option<vk::DeviceMemory>, |
| 783 |
block: Option<gpu_alloc::MemoryBlock<vk::DeviceMemory>>, |
| 784 |
format: wgt::TextureFormat, |
| 785 |
copy_size: crate::CopyExtent, |
| 786 |
} |
| 787 |
|
| 788 |
impl crate::DynTexture for Texture {} |
| 789 |
|
| 790 |
impl Texture { |
| 791 |
/// # Safety |
| 792 |
/// |
| 793 |
/// - The image handle must not be manually destroyed |
| 794 |
pub unsafe fn raw_handle(&self) -> vk::Image { |
| 795 |
self.raw |
| 796 |
} |
| 797 |
} |
| 798 |
|
| 799 |
#[derive(Debug)] |
| 800 |
pub struct TextureView { |
| 801 |
raw_texture: vk::Image, |
| 802 |
raw: vk::ImageView, |
| 803 |
layers: NonZeroU32, |
| 804 |
format: wgt::TextureFormat, |
| 805 |
raw_format: vk::Format, |
| 806 |
base_mip_level: u32, |
| 807 |
dimension: wgt::TextureViewDimension, |
| 808 |
} |
| 809 |
|
| 810 |
impl crate::DynTextureView for TextureView {} |
| 811 |
|
| 812 |
impl TextureView { |
| 813 |
/// # Safety |
| 814 |
/// |
| 815 |
/// - The image view handle must not be manually destroyed |
| 816 |
pub unsafe fn raw_handle(&self) -> vk::ImageView { |
| 817 |
self.raw |
| 818 |
} |
| 819 |
} |
| 820 |
|
| 821 |
#[derive(Debug)] |
| 822 |
pub struct Sampler { |
| 823 |
raw: vk::Sampler, |
| 824 |
create_info: vk::SamplerCreateInfo<'static>, |
| 825 |
} |
| 826 |
|
| 827 |
impl crate::DynSampler for Sampler {} |
| 828 |
|
| 829 |
#[derive(Debug)] |
| 830 |
pub struct BindGroupLayout { |
| 831 |
raw: vk::DescriptorSetLayout, |
| 832 |
desc_count: gpu_descriptor::DescriptorTotalCount, |
| 833 |
types: Box<[(vk::DescriptorType, u32)]>, |
| 834 |
/// Map of binding index to size, |
| 835 |
binding_arrays: Vec<(u32, NonZeroU32)>, |
| 836 |
} |
| 837 |
|
| 838 |
impl crate::DynBindGroupLayout for BindGroupLayout {} |
| 839 |
|
| 840 |
#[derive(Debug)] |
| 841 |
pub struct PipelineLayout { |
| 842 |
raw: vk::PipelineLayout, |
| 843 |
binding_arrays: naga::back::spv::BindingMap, |
| 844 |
} |
| 845 |
|
| 846 |
impl crate::DynPipelineLayout for PipelineLayout {} |
| 847 |
|
| 848 |
#[derive(Debug)] |
| 849 |
pub struct BindGroup { |
| 850 |
set: gpu_descriptor::DescriptorSet<vk::DescriptorSet>, |
| 851 |
} |
| 852 |
|
| 853 |
impl crate::DynBindGroup for BindGroup {} |
| 854 |
|
| 855 |
/// Miscellaneous allocation recycling pool for `CommandAllocator`. |
| 856 |
#[derive(Default)] |
| 857 |
struct Temp { |
| 858 |
marker: Vec<u8>, |
| 859 |
buffer_barriers: Vec<vk::BufferMemoryBarrier<'static>>, |
| 860 |
image_barriers: Vec<vk::ImageMemoryBarrier<'static>>, |
| 861 |
} |
| 862 |
|
| 863 |
impl Temp { |
| 864 |
fn clear(&mut self) { |
| 865 |
self.marker.clear(); |
| 866 |
self.buffer_barriers.clear(); |
| 867 |
self.image_barriers.clear(); |
| 868 |
} |
| 869 |
|
| 870 |
fn make_c_str(&mut self, name: &str) -> &CStr { |
| 871 |
self.marker.clear(); |
| 872 |
self.marker.extend_from_slice(name.as_bytes()); |
| 873 |
self.marker.push(0); |
| 874 |
unsafe { CStr::from_bytes_with_nul_unchecked(&self.marker) } |
| 875 |
} |
| 876 |
} |
| 877 |
|
| 878 |
#[derive(Clone, Eq, Hash, PartialEq)] |
| 879 |
struct FramebufferKey { |
| 880 |
raw_pass: vk::RenderPass, |
| 881 |
attachments: ArrayVec<vk::ImageView, { MAX_TOTAL_ATTACHMENTS }>, |
| 882 |
extent: wgt::Extent3d, |
| 883 |
} |
| 884 |
|
| 885 |
#[derive(Clone, Eq, Hash, PartialEq)] |
| 886 |
struct TempTextureViewKey { |
| 887 |
texture: vk::Image, |
| 888 |
format: vk::Format, |
| 889 |
mip_level: u32, |
| 890 |
depth_slice: u32, |
| 891 |
} |
| 892 |
|
| 893 |
pub struct CommandEncoder { |
| 894 |
raw: vk::CommandPool, |
| 895 |
device: Arc<DeviceShared>, |
| 896 |
|
| 897 |
/// The current command buffer, if `self` is in the ["recording"] |
| 898 |
/// state. |
| 899 |
/// |
| 900 |
/// ["recording"]: crate::CommandEncoder |
| 901 |
/// |
| 902 |
/// If non-`null`, the buffer is in the Vulkan "recording" state. |
| 903 |
active: vk::CommandBuffer, |
| 904 |
|
| 905 |
/// What kind of pass we are currently within: compute or render. |
| 906 |
bind_point: vk::PipelineBindPoint, |
| 907 |
|
| 908 |
/// Allocation recycling pool for this encoder. |
| 909 |
temp: Temp, |
| 910 |
|
| 911 |
/// A pool of available command buffers. |
| 912 |
/// |
| 913 |
/// These are all in the Vulkan "initial" state. |
| 914 |
free: Vec<vk::CommandBuffer>, |
| 915 |
|
| 916 |
/// A pool of discarded command buffers. |
| 917 |
/// |
| 918 |
/// These could be in any Vulkan state except "pending". |
| 919 |
discarded: Vec<vk::CommandBuffer>, |
| 920 |
|
| 921 |
/// If this is true, the active renderpass enabled a debug span, |
| 922 |
/// and needs to be disabled on renderpass close. |
| 923 |
rpass_debug_marker_active: bool, |
| 924 |
|
| 925 |
/// If set, the end of the next render/compute pass will write a timestamp at |
| 926 |
/// the given pool & location. |
| 927 |
end_of_pass_timer_query: Option<(vk::QueryPool, u32)>, |
| 928 |
|
| 929 |
framebuffers: FastHashMap<FramebufferKey, vk::Framebuffer>, |
| 930 |
temp_texture_views: FastHashMap<TempTextureViewKey, vk::ImageView>, |
| 931 |
|
| 932 |
counters: Arc<wgt::HalCounters>, |
| 933 |
} |
| 934 |
|
| 935 |
impl Drop for CommandEncoder { |
| 936 |
fn drop(&mut self) { |
| 937 |
// SAFETY: |
| 938 |
// |
| 939 |
// VUID-vkDestroyCommandPool-commandPool-00041: wgpu_hal requires that a |
| 940 |
// `CommandBuffer` must live until its execution is complete, and that a |
| 941 |
// `CommandBuffer` must not outlive the `CommandEncoder` that built it. |
| 942 |
// Thus, we know that none of our `CommandBuffers` are in the "pending" |
| 943 |
// state. |
| 944 |
// |
| 945 |
// The other VUIDs are pretty obvious. |
| 946 |
unsafe { |
| 947 |
// `vkDestroyCommandPool` also frees any command buffers allocated |
| 948 |
// from that pool, so there's no need to explicitly call |
| 949 |
// `vkFreeCommandBuffers` on `cmd_encoder`'s `free` and `discarded` |
| 950 |
// fields. |
| 951 |
self.device.raw.destroy_command_pool(self.raw, None); |
| 952 |
} |
| 953 |
|
| 954 |
for (_, fb) in self.framebuffers.drain() { |
| 955 |
unsafe { self.device.raw.destroy_framebuffer(fb, None) }; |
| 956 |
} |
| 957 |
|
| 958 |
for (_, view) in self.temp_texture_views.drain() { |
| 959 |
unsafe { self.device.raw.destroy_image_view(view, None) }; |
| 960 |
} |
| 961 |
|
| 962 |
self.counters.command_encoders.sub(1); |
| 963 |
} |
| 964 |
} |
| 965 |
|
| 966 |
impl CommandEncoder { |
| 967 |
/// # Safety |
| 968 |
/// |
| 969 |
/// - The command buffer handle must not be manually destroyed |
| 970 |
pub unsafe fn raw_handle(&self) -> vk::CommandBuffer { |
| 971 |
self.active |
| 972 |
} |
| 973 |
} |
| 974 |
|
| 975 |
impl fmt::Debug for CommandEncoder { |
| 976 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 977 |
f.debug_struct("CommandEncoder") |
| 978 |
.field("raw", &self.raw) |
| 979 |
.finish() |
| 980 |
} |
| 981 |
} |
| 982 |
|
| 983 |
#[derive(Debug)] |
| 984 |
pub struct CommandBuffer { |
| 985 |
raw: vk::CommandBuffer, |
| 986 |
} |
| 987 |
|
| 988 |
impl crate::DynCommandBuffer for CommandBuffer {} |
| 989 |
|
| 990 |
#[derive(Debug)] |
| 991 |
#[allow(clippy::large_enum_variant)] |
| 992 |
pub enum ShaderModule { |
| 993 |
Raw(vk::ShaderModule), |
| 994 |
Intermediate { |
| 995 |
naga_shader: crate::NagaShader, |
| 996 |
runtime_checks: wgt::ShaderRuntimeChecks, |
| 997 |
}, |
| 998 |
} |
| 999 |
|
| 1000 |
impl crate::DynShaderModule for ShaderModule {} |
| 1001 |
|
| 1002 |
#[derive(Debug)] |
| 1003 |
pub struct RenderPipeline { |
| 1004 |
raw: vk::Pipeline, |
| 1005 |
} |
| 1006 |
|
| 1007 |
impl crate::DynRenderPipeline for RenderPipeline {} |
| 1008 |
|
| 1009 |
#[derive(Debug)] |
| 1010 |
pub struct ComputePipeline { |
| 1011 |
raw: vk::Pipeline, |
| 1012 |
} |
| 1013 |
|
| 1014 |
impl crate::DynComputePipeline for ComputePipeline {} |
| 1015 |
|
| 1016 |
#[derive(Debug)] |
| 1017 |
pub struct PipelineCache { |
| 1018 |
raw: vk::PipelineCache, |
| 1019 |
} |
| 1020 |
|
| 1021 |
impl crate::DynPipelineCache for PipelineCache {} |
| 1022 |
|
| 1023 |
#[derive(Debug)] |
| 1024 |
pub struct QuerySet { |
| 1025 |
raw: vk::QueryPool, |
| 1026 |
} |
| 1027 |
|
| 1028 |
impl crate::DynQuerySet for QuerySet {} |
| 1029 |
|
| 1030 |
/// The [`Api::Fence`] type for [`vulkan::Api`]. |
| 1031 |
/// |
| 1032 |
/// This is an `enum` because there are two possible implementations of |
| 1033 |
/// `wgpu-hal` fences on Vulkan: Vulkan fences, which work on any version of |
| 1034 |
/// Vulkan, and Vulkan timeline semaphores, which are easier and cheaper but |
| 1035 |
/// require non-1.0 features. |
| 1036 |
/// |
| 1037 |
/// [`Device::create_fence`] returns a [`TimelineSemaphore`] if |
| 1038 |
/// [`VK_KHR_timeline_semaphore`] is available and enabled, and a [`FencePool`] |
| 1039 |
/// otherwise. |
| 1040 |
/// |
| 1041 |
/// [`Api::Fence`]: crate::Api::Fence |
| 1042 |
/// [`vulkan::Api`]: Api |
| 1043 |
/// [`Device::create_fence`]: crate::Device::create_fence |
| 1044 |
/// [`TimelineSemaphore`]: Fence::TimelineSemaphore |
| 1045 |
/// [`VK_KHR_timeline_semaphore`]: https://registry.khronos.org/vulkan/specs/1.3-extensions/html/vkspec.html#VK_KHR_timeline_semaphore |
| 1046 |
/// [`FencePool`]: Fence::FencePool |
| 1047 |
#[derive(Debug)] |
| 1048 |
pub enum Fence { |
| 1049 |
/// A Vulkan [timeline semaphore]. |
| 1050 |
/// |
| 1051 |
/// These are simpler to use than Vulkan fences, since timeline semaphores |
| 1052 |
/// work exactly the way [`wpgu_hal::Api::Fence`] is specified to work. |
| 1053 |
/// |
| 1054 |
/// [timeline semaphore]: https://registry.khronos.org/vulkan/specs/1.3-extensions/html/vkspec.html#synchronization-semaphores |
| 1055 |
/// [`wpgu_hal::Api::Fence`]: crate::Api::Fence |
| 1056 |
TimelineSemaphore(vk::Semaphore), |
| 1057 |
|
| 1058 |
/// A collection of Vulkan [fence]s, each associated with a [`FenceValue`]. |
| 1059 |
/// |
| 1060 |
/// The effective [`FenceValue`] of this variant is the greater of |
| 1061 |
/// `last_completed` and the maximum value associated with a signalled fence |
| 1062 |
/// in `active`. |
| 1063 |
/// |
| 1064 |
/// Fences are available in all versions of Vulkan, but since they only have |
| 1065 |
/// two states, "signaled" and "unsignaled", we need to use a separate fence |
| 1066 |
/// for each queue submission we might want to wait for, and remember which |
| 1067 |
/// [`FenceValue`] each one represents. |
| 1068 |
/// |
| 1069 |
/// [fence]: https://registry.khronos.org/vulkan/specs/1.3-extensions/html/vkspec.html#synchronization-fences |
| 1070 |
/// [`FenceValue`]: crate::FenceValue |
| 1071 |
FencePool { |
| 1072 |
last_completed: crate::FenceValue, |
| 1073 |
/// The pending fence values have to be ascending. |
| 1074 |
active: Vec<(crate::FenceValue, vk::Fence)>, |
| 1075 |
free: Vec<vk::Fence>, |
| 1076 |
}, |
| 1077 |
} |
| 1078 |
|
| 1079 |
impl crate::DynFence for Fence {} |
| 1080 |
|
| 1081 |
impl Fence { |
| 1082 |
/// Return the highest [`FenceValue`] among the signalled fences in `active`. |
| 1083 |
/// |
| 1084 |
/// As an optimization, assume that we already know that the fence has |
| 1085 |
/// reached `last_completed`, and don't bother checking fences whose values |
| 1086 |
/// are less than that: those fences remain in the `active` array only |
| 1087 |
/// because we haven't called `maintain` yet to clean them up. |
| 1088 |
/// |
| 1089 |
/// [`FenceValue`]: crate::FenceValue |
| 1090 |
fn check_active( |
| 1091 |
device: &ash::Device, |
| 1092 |
mut last_completed: crate::FenceValue, |
| 1093 |
active: &[(crate::FenceValue, vk::Fence)], |
| 1094 |
) -> Result<crate::FenceValue, crate::DeviceError> { |
| 1095 |
for &(value, raw) in active.iter() { |
| 1096 |
unsafe { |
| 1097 |
if value > last_completed |
| 1098 |
&& device |
| 1099 |
.get_fence_status(raw) |
| 1100 |
.map_err(map_host_device_oom_and_lost_err)? |
| 1101 |
{ |
| 1102 |
last_completed = value; |
| 1103 |
} |
| 1104 |
} |
| 1105 |
} |
| 1106 |
Ok(last_completed) |
| 1107 |
} |
| 1108 |
|
| 1109 |
/// Return the highest signalled [`FenceValue`] for `self`. |
| 1110 |
/// |
| 1111 |
/// [`FenceValue`]: crate::FenceValue |
| 1112 |
fn get_latest( |
| 1113 |
&self, |
| 1114 |
device: &ash::Device, |
| 1115 |
extension: Option<&ExtensionFn<khr::timeline_semaphore::Device>>, |
| 1116 |
) -> Result<crate::FenceValue, crate::DeviceError> { |
| 1117 |
match *self { |
| 1118 |
Self::TimelineSemaphore(raw) => unsafe { |
| 1119 |
Ok(match *extension.unwrap() { |
| 1120 |
ExtensionFn::Extension(ref ext) => ext |
| 1121 |
.get_semaphore_counter_value(raw) |
| 1122 |
.map_err(map_host_device_oom_and_lost_err)?, |
| 1123 |
ExtensionFn::Promoted => device |
| 1124 |
.get_semaphore_counter_value(raw) |
| 1125 |
.map_err(map_host_device_oom_and_lost_err)?, |
| 1126 |
}) |
| 1127 |
}, |
| 1128 |
Self::FencePool { |
| 1129 |
last_completed, |
| 1130 |
ref active, |
| 1131 |
free: _, |
| 1132 |
} => Self::check_active(device, last_completed, active), |
| 1133 |
} |
| 1134 |
} |
| 1135 |
|
| 1136 |
/// Trim the internal state of this [`Fence`]. |
| 1137 |
/// |
| 1138 |
/// This function has no externally visible effect, but you should call it |
| 1139 |
/// periodically to keep this fence's resource consumption under control. |
| 1140 |
/// |
| 1141 |
/// For fences using the [`FencePool`] implementation, this function |
| 1142 |
/// recycles fences that have been signaled. If you don't call this, |
| 1143 |
/// [`Queue::submit`] will just keep allocating a new Vulkan fence every |
| 1144 |
/// time it's called. |
| 1145 |
/// |
| 1146 |
/// [`FencePool`]: Fence::FencePool |
| 1147 |
/// [`Queue::submit`]: crate::Queue::submit |
| 1148 |
fn maintain(&mut self, device: &ash::Device) -> Result<(), crate::DeviceError> { |
| 1149 |
match *self { |
| 1150 |
Self::TimelineSemaphore(_) => {} |
| 1151 |
Self::FencePool { |
| 1152 |
ref mut last_completed, |
| 1153 |
ref mut active, |
| 1154 |
ref mut free, |
| 1155 |
} => { |
| 1156 |
let latest = Self::check_active(device, *last_completed, active)?; |
| 1157 |
let base_free = free.len(); |
| 1158 |
for &(value, raw) in active.iter() { |
| 1159 |
if value <= latest { |
| 1160 |
free.push(raw); |
| 1161 |
} |
| 1162 |
} |
| 1163 |
if free.len() != base_free { |
| 1164 |
active.retain(|&(value, _)| value > latest); |
| 1165 |
unsafe { device.reset_fences(&free[base_free..]) } |
| 1166 |
.map_err(map_device_oom_err)? |
| 1167 |
} |
| 1168 |
*last_completed = latest; |
| 1169 |
} |
| 1170 |
} |
| 1171 |
Ok(()) |
| 1172 |
} |
| 1173 |
} |
| 1174 |
|
| 1175 |
impl crate::Queue for Queue { |
| 1176 |
type A = Api; |
| 1177 |
|
| 1178 |
unsafe fn submit( |
| 1179 |
&self, |
| 1180 |
command_buffers: &[&CommandBuffer], |
| 1181 |
surface_textures: &[&SurfaceTexture], |
| 1182 |
(signal_fence, signal_value): (&mut Fence, crate::FenceValue), |
| 1183 |
) -> Result<(), crate::DeviceError> { |
| 1184 |
let mut fence_raw = vk::Fence::null(); |
| 1185 |
|
| 1186 |
let mut wait_stage_masks = Vec::new(); |
| 1187 |
let mut wait_semaphores = Vec::new(); |
| 1188 |
let mut signal_semaphores = Vec::new(); |
| 1189 |
let mut signal_values = Vec::new(); |
| 1190 |
|
| 1191 |
// Double check that the same swapchain image isn't being given to us multiple times, |
| 1192 |
// as that will deadlock when we try to lock them all. |
| 1193 |
debug_assert!( |
| 1194 |
{ |
| 1195 |
let mut check = HashSet::with_capacity(surface_textures.len()); |
| 1196 |
// We compare the Arcs by pointer, as Eq isn't well defined for SurfaceSemaphores. |
| 1197 |
for st in surface_textures { |
| 1198 |
check.insert(Arc::as_ptr(&st.surface_semaphores)); |
| 1199 |
} |
| 1200 |
check.len() == surface_textures.len() |
| 1201 |
}, |
| 1202 |
"More than one surface texture is being used from the same swapchain. This will cause a deadlock in release." |
| 1203 |
); |
| 1204 |
|
| 1205 |
let locked_swapchain_semaphores = surface_textures |
| 1206 |
.iter() |
| 1207 |
.map(|st| { |
| 1208 |
st.surface_semaphores |
| 1209 |
.try_lock() |
| 1210 |
.expect("Failed to lock surface semaphore.") |
| 1211 |
}) |
| 1212 |
.collect::<Vec<_>>(); |
| 1213 |
|
| 1214 |
for mut swapchain_semaphore in locked_swapchain_semaphores { |
| 1215 |
swapchain_semaphore.set_used_fence_value(signal_value); |
| 1216 |
|
| 1217 |
// If we're the first submission to operate on this image, wait on |
| 1218 |
// its acquire semaphore, to make sure the presentation engine is |
| 1219 |
// done with it. |
| 1220 |
if let Some(sem) = swapchain_semaphore.get_acquire_wait_semaphore() { |
| 1221 |
wait_stage_masks.push(vk::PipelineStageFlags::TOP_OF_PIPE); |
| 1222 |
wait_semaphores.push(sem); |
| 1223 |
} |
| 1224 |
|
| 1225 |
// Get a semaphore to signal when we're done writing to this surface |
| 1226 |
// image. Presentation of this image will wait for this. |
| 1227 |
let signal_semaphore = swapchain_semaphore.get_submit_signal_semaphore(&self.device)?; |
| 1228 |
signal_semaphores.push(signal_semaphore); |
| 1229 |
signal_values.push(!0); |
| 1230 |
} |
| 1231 |
|
| 1232 |
let mut guards = self.signal_semaphores.lock(); |
| 1233 |
let (ref mut pending_signal_semaphores, ref mut pending_signal_semaphore_values) = |
| 1234 |
guards.deref_mut(); |
| 1235 |
assert!(pending_signal_semaphores.len() == pending_signal_semaphore_values.len()); |
| 1236 |
if !pending_signal_semaphores.is_empty() { |
| 1237 |
signal_semaphores.append(pending_signal_semaphores); |
| 1238 |
signal_values.append(pending_signal_semaphore_values); |
| 1239 |
} |
| 1240 |
|
| 1241 |
// In order for submissions to be strictly ordered, we encode a dependency between each submission |
| 1242 |
// using a pair of semaphores. This adds a wait if it is needed, and signals the next semaphore. |
| 1243 |
let semaphore_state = self.relay_semaphores.lock().advance(&self.device)?; |
| 1244 |
|
| 1245 |
if let Some(sem) = semaphore_state.wait { |
| 1246 |
wait_stage_masks.push(vk::PipelineStageFlags::TOP_OF_PIPE); |
| 1247 |
wait_semaphores.push(sem); |
| 1248 |
} |
| 1249 |
|
| 1250 |
signal_semaphores.push(semaphore_state.signal); |
| 1251 |
signal_values.push(!0); |
| 1252 |
|
| 1253 |
// We need to signal our wgpu::Fence if we have one, this adds it to the signal list. |
| 1254 |
signal_fence.maintain(&self.device.raw)?; |
| 1255 |
match *signal_fence { |
| 1256 |
Fence::TimelineSemaphore(raw) => { |
| 1257 |
signal_semaphores.push(raw); |
| 1258 |
signal_values.push(signal_value); |
| 1259 |
} |
| 1260 |
Fence::FencePool { |
| 1261 |
ref mut active, |
| 1262 |
ref mut free, |
| 1263 |
.. |
| 1264 |
} => { |
| 1265 |
fence_raw = match free.pop() { |
| 1266 |
Some(raw) => raw, |
| 1267 |
None => unsafe { |
| 1268 |
self.device |
| 1269 |
.raw |
| 1270 |
.create_fence(&vk::FenceCreateInfo::default(), None) |
| 1271 |
.map_err(map_host_device_oom_err)? |
| 1272 |
}, |
| 1273 |
}; |
| 1274 |
active.push((signal_value, fence_raw)); |
| 1275 |
} |
| 1276 |
} |
| 1277 |
|
| 1278 |
let vk_cmd_buffers = command_buffers |
| 1279 |
.iter() |
| 1280 |
.map(|cmd| cmd.raw) |
| 1281 |
.collect::<Vec<_>>(); |
| 1282 |
|
| 1283 |
let mut vk_info = vk::SubmitInfo::default().command_buffers(&vk_cmd_buffers); |
| 1284 |
|
| 1285 |
vk_info = vk_info |
| 1286 |
.wait_semaphores(&wait_semaphores) |
| 1287 |
.wait_dst_stage_mask(&wait_stage_masks) |
| 1288 |
.signal_semaphores(&signal_semaphores); |
| 1289 |
|
| 1290 |
let mut vk_timeline_info; |
| 1291 |
|
| 1292 |
if self.device.private_caps.timeline_semaphores { |
| 1293 |
vk_timeline_info = |
| 1294 |
vk::TimelineSemaphoreSubmitInfo::default().signal_semaphore_values(&signal_values); |
| 1295 |
vk_info = vk_info.push_next(&mut vk_timeline_info); |
| 1296 |
} |
| 1297 |
|
| 1298 |
profiling::scope!("vkQueueSubmit"); |
| 1299 |
unsafe { |
| 1300 |
self.device |
| 1301 |
.raw |
| 1302 |
.queue_submit(self.raw, &[vk_info], fence_raw) |
| 1303 |
.map_err(map_host_device_oom_and_lost_err)? |
| 1304 |
}; |
| 1305 |
Ok(()) |
| 1306 |
} |
| 1307 |
|
| 1308 |
unsafe fn present( |
| 1309 |
&self, |
| 1310 |
surface: &Surface, |
| 1311 |
texture: SurfaceTexture, |
| 1312 |
) -> Result<(), crate::SurfaceError> { |
| 1313 |
let mut swapchain = surface.swapchain.write(); |
| 1314 |
let ssc = swapchain.as_mut().unwrap(); |
| 1315 |
let mut swapchain_semaphores = texture.surface_semaphores.lock(); |
| 1316 |
|
| 1317 |
let swapchains = [ssc.raw]; |
| 1318 |
let image_indices = [texture.index]; |
| 1319 |
let vk_info = vk::PresentInfoKHR::default() |
| 1320 |
.swapchains(&swapchains) |
| 1321 |
.image_indices(&image_indices) |
| 1322 |
.wait_semaphores(swapchain_semaphores.get_present_wait_semaphores()); |
| 1323 |
|
| 1324 |
let mut display_timing; |
| 1325 |
let present_times; |
| 1326 |
let vk_info = if let Some(present_time) = ssc.next_present_time.take() { |
| 1327 |
debug_assert!( |
| 1328 |
ssc.device |
| 1329 |
.features |
| 1330 |
.contains(wgt::Features::VULKAN_GOOGLE_DISPLAY_TIMING), |
| 1331 |
"`next_present_time` should only be set if `VULKAN_GOOGLE_DISPLAY_TIMING` is enabled" |
| 1332 |
); |
| 1333 |
present_times = [present_time]; |
| 1334 |
display_timing = vk::PresentTimesInfoGOOGLE::default().times(&present_times); |
| 1335 |
// SAFETY: We know that VK_GOOGLE_display_timing is present because of the safety contract on `next_present_time`. |
| 1336 |
vk_info.push_next(&mut display_timing) |
| 1337 |
} else { |
| 1338 |
vk_info |
| 1339 |
}; |
| 1340 |
|
| 1341 |
let suboptimal = { |
| 1342 |
profiling::scope!("vkQueuePresentKHR"); |
| 1343 |
unsafe { self.swapchain_fn.queue_present(self.raw, &vk_info) }.map_err(|error| { |
| 1344 |
match error { |
| 1345 |
vk::Result::ERROR_OUT_OF_DATE_KHR => crate::SurfaceError::Outdated, |
| 1346 |
vk::Result::ERROR_SURFACE_LOST_KHR => crate::SurfaceError::Lost, |
| 1347 |
// We don't use VK_EXT_full_screen_exclusive |
| 1348 |
// VK_ERROR_FULL_SCREEN_EXCLUSIVE_MODE_LOST_EXT |
| 1349 |
_ => map_host_device_oom_and_lost_err(error).into(), |
| 1350 |
} |
| 1351 |
})? |
| 1352 |
}; |
| 1353 |
if suboptimal { |
| 1354 |
// We treat `VK_SUBOPTIMAL_KHR` as `VK_SUCCESS` on Android. |
| 1355 |
// On Android 10+, libvulkan's `vkQueuePresentKHR` implementation returns `VK_SUBOPTIMAL_KHR` if not doing pre-rotation |
| 1356 |
// (i.e `VkSwapchainCreateInfoKHR::preTransform` not being equal to the current device orientation). |
| 1357 |
// This is always the case when the device orientation is anything other than the identity one, as we unconditionally use `VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR`. |
| 1358 |
#[cfg(not(target_os = "android"))] |
| 1359 |
log::warn!("Suboptimal present of frame {}", texture.index); |
| 1360 |
} |
| 1361 |
Ok(()) |
| 1362 |
} |
| 1363 |
|
| 1364 |
unsafe fn get_timestamp_period(&self) -> f32 { |
| 1365 |
self.device.timestamp_period |
| 1366 |
} |
| 1367 |
} |
| 1368 |
|
| 1369 |
impl Queue { |
| 1370 |
pub fn raw_device(&self) -> &ash::Device { |
| 1371 |
&self.device.raw |
| 1372 |
} |
| 1373 |
|
| 1374 |
pub fn add_signal_semaphore(&self, semaphore: vk::Semaphore, semaphore_value: Option<u64>) { |
| 1375 |
let mut guards = self.signal_semaphores.lock(); |
| 1376 |
let (ref mut semaphores, ref mut semaphore_values) = guards.deref_mut(); |
| 1377 |
semaphores.push(semaphore); |
| 1378 |
semaphore_values.push(semaphore_value.unwrap_or(!0)); |
| 1379 |
} |
| 1380 |
} |
| 1381 |
|
| 1382 |
/// Maps |
| 1383 |
/// |
| 1384 |
/// - VK_ERROR_OUT_OF_HOST_MEMORY |
| 1385 |
/// - VK_ERROR_OUT_OF_DEVICE_MEMORY |
| 1386 |
fn map_host_device_oom_err(err: vk::Result) -> crate::DeviceError { |
| 1387 |
match err { |
| 1388 |
vk::Result::ERROR_OUT_OF_HOST_MEMORY | vk::Result::ERROR_OUT_OF_DEVICE_MEMORY => { |
| 1389 |
get_oom_err(err) |
| 1390 |
} |
| 1391 |
e => get_unexpected_err(e), |
| 1392 |
} |
| 1393 |
} |
| 1394 |
|
| 1395 |
/// Maps |
| 1396 |
/// |
| 1397 |
/// - VK_ERROR_OUT_OF_HOST_MEMORY |
| 1398 |
/// - VK_ERROR_OUT_OF_DEVICE_MEMORY |
| 1399 |
/// - VK_ERROR_DEVICE_LOST |
| 1400 |
fn map_host_device_oom_and_lost_err(err: vk::Result) -> crate::DeviceError { |
| 1401 |
match err { |
| 1402 |
vk::Result::ERROR_DEVICE_LOST => get_lost_err(), |
| 1403 |
other => map_host_device_oom_err(other), |
| 1404 |
} |
| 1405 |
} |
| 1406 |
|
| 1407 |
/// Maps |
| 1408 |
/// |
| 1409 |
/// - VK_ERROR_OUT_OF_HOST_MEMORY |
| 1410 |
/// - VK_ERROR_OUT_OF_DEVICE_MEMORY |
| 1411 |
/// - VK_ERROR_INVALID_OPAQUE_CAPTURE_ADDRESS_KHR |
| 1412 |
fn map_host_device_oom_and_ioca_err(err: vk::Result) -> crate::DeviceError { |
| 1413 |
// We don't use VK_KHR_buffer_device_address |
| 1414 |
// VK_ERROR_INVALID_OPAQUE_CAPTURE_ADDRESS_KHR |
| 1415 |
map_host_device_oom_err(err) |
| 1416 |
} |
| 1417 |
|
| 1418 |
/// Maps |
| 1419 |
/// |
| 1420 |
/// - VK_ERROR_OUT_OF_HOST_MEMORY |
| 1421 |
fn map_host_oom_err(err: vk::Result) -> crate::DeviceError { |
| 1422 |
match err { |
| 1423 |
vk::Result::ERROR_OUT_OF_HOST_MEMORY => get_oom_err(err), |
| 1424 |
e => get_unexpected_err(e), |
| 1425 |
} |
| 1426 |
} |
| 1427 |
|
| 1428 |
/// Maps |
| 1429 |
/// |
| 1430 |
/// - VK_ERROR_OUT_OF_DEVICE_MEMORY |
| 1431 |
fn map_device_oom_err(err: vk::Result) -> crate::DeviceError { |
| 1432 |
match err { |
| 1433 |
vk::Result::ERROR_OUT_OF_DEVICE_MEMORY => get_oom_err(err), |
| 1434 |
e => get_unexpected_err(e), |
| 1435 |
} |
| 1436 |
} |
| 1437 |
|
| 1438 |
/// Maps |
| 1439 |
/// |
| 1440 |
/// - VK_ERROR_OUT_OF_HOST_MEMORY |
| 1441 |
/// - VK_ERROR_INVALID_OPAQUE_CAPTURE_ADDRESS_KHR |
| 1442 |
fn map_host_oom_and_ioca_err(err: vk::Result) -> crate::DeviceError { |
| 1443 |
// We don't use VK_KHR_buffer_device_address |
| 1444 |
// VK_ERROR_INVALID_OPAQUE_CAPTURE_ADDRESS_KHR |
| 1445 |
map_host_oom_err(err) |
| 1446 |
} |
| 1447 |
|
| 1448 |
/// Maps |
| 1449 |
/// |
| 1450 |
/// - VK_ERROR_OUT_OF_HOST_MEMORY |
| 1451 |
/// - VK_ERROR_OUT_OF_DEVICE_MEMORY |
| 1452 |
/// - VK_PIPELINE_COMPILE_REQUIRED_EXT |
| 1453 |
/// - VK_ERROR_INVALID_SHADER_NV |
| 1454 |
fn map_pipeline_err(err: vk::Result) -> crate::DeviceError { |
| 1455 |
// We don't use VK_EXT_pipeline_creation_cache_control |
| 1456 |
// VK_PIPELINE_COMPILE_REQUIRED_EXT |
| 1457 |
// We don't use VK_NV_glsl_shader |
| 1458 |
// VK_ERROR_INVALID_SHADER_NV |
| 1459 |
map_host_device_oom_err(err) |
| 1460 |
} |
| 1461 |
|
| 1462 |
/// Returns [`crate::DeviceError::Unexpected`] or panics if the `internal_error_panic` |
| 1463 |
/// feature flag is enabled. |
| 1464 |
fn get_unexpected_err(_err: vk::Result) -> crate::DeviceError { |
| 1465 |
#[cfg(feature = "internal_error_panic")] |
| 1466 |
panic!("Unexpected Vulkan error: {_err:?}"); |
| 1467 |
|
| 1468 |
#[allow(unreachable_code)] |
| 1469 |
crate::DeviceError::Unexpected |
| 1470 |
} |
| 1471 |
|
| 1472 |
/// Returns [`crate::DeviceError::OutOfMemory`]. |
| 1473 |
fn get_oom_err(_err: vk::Result) -> crate::DeviceError { |
| 1474 |
crate::DeviceError::OutOfMemory |
| 1475 |
} |
| 1476 |
|
| 1477 |
/// Returns [`crate::DeviceError::Lost`] or panics if the `device_lost_panic` |
| 1478 |
/// feature flag is enabled. |
| 1479 |
fn get_lost_err() -> crate::DeviceError { |
| 1480 |
#[cfg(feature = "device_lost_panic")] |
| 1481 |
panic!("Device lost"); |
| 1482 |
|
| 1483 |
#[allow(unreachable_code)] |
| 1484 |
crate::DeviceError::Lost |
| 1485 |
} |
| 1486 |
|
| 1487 |
#[derive(Clone, Copy, Pod, Zeroable)] |
| 1488 |
#[repr(C)] |
| 1489 |
struct RawTlasInstance { |
| 1490 |
transform: [f32; 12], |
| 1491 |
custom_data_and_mask: u32, |
| 1492 |
shader_binding_table_record_offset_and_flags: u32, |
| 1493 |
acceleration_structure_reference: u64, |
| 1494 |
} |