pub(in crate::runtime) mod features;
pub(in crate::runtime) mod physical;
mod configuration;
pub(super) mod info;
pub(super) mod logical;
pub(in crate::runtime) trait NativeFlagBits:
Copy + PartialEq + std::ops::BitAnd<Output = Self>
{
fn contains(self, member: Self) -> bool {
(self & member) == member
}
}
macro_rules! impl_native_flag_bits {
($($ty:ty),* $(,)?) => {
$(impl NativeFlagBits for $ty {})*
};
}
impl_native_flag_bits! {
vk::FormatFeatureFlagBits,
vk::ImageCreateFlagBits,
vk::ImageUsageFlagBits,
vk::QueueFlagBits,
vk::ShaderStageFlagBits,
vk::CompositeAlphaFlagBitsKHR,
vk::SampleCountFlagBits,
vk::VideoCapabilityFlagBitsKHR,
vk::VideoChromaSubsamplingFlagBitsKHR,
vk::VideoCodingControlFlagBitsKHR,
vk::VideoComponentBitDepthFlagBitsKHR,
vk::VideoDecodeCapabilityFlagBitsKHR,
vk::VideoDecodeH264PictureLayoutFlagBitsKHR,
vk::VideoEncodeFeedbackFlagBitsKHR,
vk::VideoEncodeH265CtbSizeFlagBitsKHR,
vk::VideoEncodeH265TransformBlockSizeFlagBitsKHR,
vk::VideoEncodeRateControlModeFlagBitsKHR,
vk::AccessFlagBits2,
vk::PipelineStageFlagBits2,
vk::BufferUsageFlagBits,
vk::QueryResultFlagBits,
}
use std::sync::{Arc, Mutex, OnceLock};
use crate::{
Error, Result,
runtime::{
Buffer, Instance,
dna::{ir::elementwise::Program, kernel::KernelId, tuning::ObservedRoute},
graph::executable::ExecutableGraph,
},
};
use configuration::DeviceConfiguration;
use features::ExecutionProfile;
use info::log_compute_device;
use logical::{
DeviceLogical, FrameDraw, MAX_RECYCLED_STORAGE_BUFFERS, MAX_RECYCLED_STORAGE_BYTES,
PipelineCacheGuard, SceneDrawCmd, TileReduceOrder,
buffer::{MeshBuffer, RecycledBuffer},
command::{CommandPool, CommandPoolKind, RecordedCommandBuffer, ReusableCommandBuffer},
commands::{RenderingCommands, SyncCommands},
image::RenderTarget,
pipeline::{self, ComputePipeline},
pqc, secret_buffer, specialization,
timestamp::{RouteTimings, TimestampPair},
video, vkcache,
};
pub(in crate::runtime) use physical::DevicePhysical;
pub(crate) struct Device {
logical: DeviceLogical,
physical: DevicePhysical,
configuration: DeviceConfiguration,
instance: super::instance::Instance,
}
impl Device {
pub(in crate::runtime) fn new(
instance: &super::instance::Instance,
physical: DevicePhysical,
) -> crate::Result<Arc<Self>> {
let configuration = DeviceConfiguration::new(instance, &physical);
let logical = DeviceLogical::new(instance, &physical, &configuration)?;
Ok(Arc::new(Self {
logical,
physical,
configuration,
instance: instance.clone(),
}))
}
pub(in crate::runtime) fn logical(&self) -> &DeviceLogical {
&self.logical
}
pub(in crate::runtime) fn compute_shared_memory_bytes(&self) -> u32 {
self.physical.limits.max_compute_shared_memory_size
}
pub(in crate::runtime) const fn max_compute_work_group_count(&self) -> [u32; 3] {
self.physical.limits.max_compute_work_group_count
}
pub(in crate::runtime) fn linear_dispatch_grid(
&self,
count: u32,
group_size: u32,
) -> crate::Result<[u32; 3]> {
let limits = self.physical.limits;
if group_size == 0
|| group_size > limits.max_compute_work_group_size[0]
|| group_size > limits.max_compute_work_group_invocations
{
return Err(crate::Error::invalid_argument(
"linear dispatch group size exceeds device limits",
));
}
let groups = count.div_ceil(group_size).max(1);
let x = groups.min(limits.max_compute_work_group_count[0]);
if x == 0 || limits.max_compute_work_group_count[1] == 0 {
return Err(crate::Error::missing_capability(
"device has no usable compute dispatch grid",
));
}
let y = groups.div_ceil(x);
if y > limits.max_compute_work_group_count[1] {
return Err(crate::Error::invalid_argument(
"linear work exceeds queried compute dispatch grid",
));
}
Ok([x, y, 1])
}
pub(in crate::runtime) fn supports_clock_calibration(&self) -> bool {
self.physical.limits.compute_timestamp_valid_bits != 0
&& (self
.configuration
.extension_enabled(vk::KHR_CALIBRATED_TIMESTAMPS_EXTENSION_NAME)
|| self
.configuration
.extension_enabled(vk::EXT_CALIBRATED_TIMESTAMPS_EXTENSION_NAME))
}
pub(in crate::runtime) fn calibrate_clock(
&self,
sample_count: u32,
) -> crate::Result<crate::ClockCalibration> {
if !(1..=1024).contains(&sample_count) {
return Err(crate::Error::invalid_argument(
"clock calibration sample count must be in [1, 1024]",
));
}
if !self.supports_clock_calibration() {
return Err(crate::Error::unavailable(
"selected device does not expose calibrated timestamps",
));
}
let use_khr = self
.configuration
.extension_enabled(vk::KHR_CALIBRATED_TIMESTAMPS_EXTENSION_NAME);
let instance = self.instance.raw();
let query_domains = |count: *mut u32, domains: *mut vk::TimeDomainKHR| unsafe {
if use_khr {
instance.get_physical_device_calibrateable_time_domains_khr(
self.physical.handle,
count,
domains,
)
} else {
instance.get_physical_device_calibrateable_time_domains_ext(
self.physical.handle,
count,
domains,
)
}
};
let mut domain_count = 0_u32;
let count_result = query_domains(&mut domain_count, std::ptr::null_mut());
match count_result {
Ok(vk::Result::SUCCESS) if domain_count > 0 => {}
Err(vk::CallError::MissingCommand(_)) => {
return Err(crate::Error::unavailable(
"calibrated timestamp entry points are unavailable",
));
}
_ => {
return Err(crate::Error::backend_dispatch_failure(
"Vulkan",
"calibrated timestamp domain query",
count_result,
));
}
}
let mut domains = vec![vk::TimeDomainKHR::DEVICE_KHR; domain_count as usize];
let list_result = query_domains(&mut domain_count, domains.as_mut_ptr());
if let Err(vk::CallError::MissingCommand(_)) = list_result {
return Err(crate::Error::unavailable(
"calibrated timestamp entry points are unavailable",
));
}
if list_result != Ok(vk::Result::SUCCESS) {
return Err(crate::Error::backend_dispatch_failure(
"Vulkan",
"calibrated timestamp domain enumeration",
list_result,
));
}
let has_device = domains.contains(&vk::TimeDomainKHR::DEVICE_KHR);
let has_monotonic = domains.contains(&vk::TimeDomainKHR::CLOCK_MONOTONIC_KHR);
let has_monotonic_raw = domains.contains(&vk::TimeDomainKHR::CLOCK_MONOTONIC_RAW_KHR);
if !has_device || !(has_monotonic || has_monotonic_raw) {
return Err(crate::Error::unavailable(
"device and POSIX monotonic clock domains cannot be calibrated together",
));
}
let host_domain = if has_monotonic_raw {
vk::TimeDomainKHR::CLOCK_MONOTONIC_RAW_KHR
} else {
vk::TimeDomainKHR::CLOCK_MONOTONIC_KHR
};
let infos = [
vk::CalibratedTimestampInfoKHR::new().set_time_domain(vk::TimeDomainKHR::DEVICE_KHR),
vk::CalibratedTimestampInfoKHR::new().set_time_domain(host_domain),
];
let device = self.raw();
let mut best = crate::ClockCalibration::default();
let mut have_best = false;
for _ in 0..sample_count {
let mut timestamps = [0_u64; 2];
let mut maximum_deviation = 0_u64;
let sample_result = unsafe {
if use_khr {
device.get_calibrated_timestamps_khr(
2,
infos.as_ptr(),
timestamps.as_mut_ptr(),
&mut maximum_deviation,
)
} else {
device.get_calibrated_timestamps_ext(
2,
infos.as_ptr(),
timestamps.as_mut_ptr(),
&mut maximum_deviation,
)
}
};
if let Err(vk::CallError::MissingCommand(_)) = sample_result {
return Err(crate::Error::unavailable(
"calibrated timestamp entry points are unavailable",
));
}
if sample_result != Ok(vk::Result::SUCCESS) {
return Err(crate::Error::backend_dispatch_failure(
"Vulkan",
"calibrated timestamp sample",
sample_result,
));
}
if have_best && maximum_deviation >= best.maximum_deviation_nanoseconds {
continue;
}
let valid_bits = self.physical.limits.compute_timestamp_valid_bits;
if valid_bits < 64 {
timestamps[0] &= (1_u64 << valid_bits) - 1;
}
best = crate::ClockCalibration {
device_timestamp_ticks: timestamps[0],
host_timestamp_nanoseconds: timestamps[1],
maximum_deviation_nanoseconds: maximum_deviation,
device_nanoseconds_per_tick: self.physical.limits.timestamp_period_ns,
device_timestamp_valid_bits: valid_bits,
host_clock_domain: if has_monotonic_raw {
crate::HostClockDomain::MonotonicRaw
} else {
crate::HostClockDomain::Monotonic
},
};
have_best = true;
}
Ok(best)
}
pub(in crate::runtime) fn device_info(&self) -> crate::DeviceInfo {
let mut info = self.physical.device_info();
let (buffers, images) = self.descriptor_capacities();
info.hardware.storage_buffer_descriptors = buffers;
info.hardware.storage_image_descriptors = images;
info
}
pub(in crate::runtime) fn configuration(&self) -> &DeviceConfiguration {
&self.configuration
}
pub(super) fn descriptor_capacities(&self) -> (u32, u32) {
self.logical.descriptors.capacities()
}
pub(in crate::runtime) fn execution_profile_name(&self) -> &'static str {
match self.physical.profile {
ExecutionProfile::Core => "Core (Vulkan 1.3+)",
ExecutionProfile::Compatibility => "Compatibility (Vulkan 1.2)",
}
}
pub(in crate::runtime) fn log_identity(&self) {
crate::log_info!(
crate::LogComponent::ENGINE,
"oa::Engine · v{} · Vulkan · 1 device {{",
env!("CARGO_PKG_VERSION")
);
log_compute_device(self, " ", true);
crate::log_info!(crate::LogComponent::RUNTIME, "}}");
}
pub(in crate::runtime) fn video_device_capabilities(
&self,
) -> Result<crate::video::VideoDeviceCapabilities> {
let video = self.physical.video;
let decoder_available = self.logical.video_decode_queue.is_some()
&& video.decode_result_status_queries
&& (video.h264_decode || video.h265_decode || video.av1_decode || video.vp9_decode);
let encoder_available =
self.logical.video_encode_queue.is_some() && (video.h264_encode || video.h265_encode);
Ok(crate::video::VideoDeviceCapabilities {
decode_queue_family: video.decode_queue_family,
encode_queue_family: video.encode_queue_family,
decode_result_status_queries: video.decode_result_status_queries,
encode_result_status_queries: video.encode_result_status_queries,
h264_decode: video.h264_decode,
h265_decode: video.h265_decode,
av1_decode: video.av1_decode,
vp9_decode: video.vp9_decode,
h264_encode: video.h264_encode,
h265_encode: video.h265_encode,
av1_encode: video.av1_encode,
video_queues_enabled: self.logical.video_decode_queue.is_some()
|| self.logical.video_encode_queue.is_some(),
decoder_sessions_available: decoder_available,
encoder_sessions_available: encoder_available,
})
}
pub(in crate::runtime) fn has_graphics_queue(&self) -> bool {
self.logical.graphics_queue.is_some()
&& self.configuration.features().dynamic_rendering
&& self.configuration.features().shader_draw_parameters
}
pub(in crate::runtime) fn require_ui_bindings(&self) -> Result<()> {
if !self.optimal_format_supports(
vk::Format::R8G8B8A8_UNORM,
vk::FormatFeatureFlagBits::COLOR_ATTACHMENT_BIT
| vk::FormatFeatureFlagBits::TRANSFER_SRC_BIT
| vk::FormatFeatureFlagBits::TRANSFER_DST_BIT,
) {
return Err(Error::missing_capability(
"UI requires RGBA8 color-attachment and transfer image support",
));
}
if !self.has_graphics_queue() {
return Err(Error::missing_capability(
"UI requires a graphics queue with dynamic rendering",
));
}
Ok(())
}
pub(in crate::runtime) fn supports_swapchain(&self) -> bool {
self
.configuration
.extension_enabled(vk::KHR_SWAPCHAIN_EXTENSION_NAME)
}
pub(in crate::runtime) fn optimal_format_supports(
&self,
format: vk::Format,
required: vk::FormatFeatureFlags,
) -> bool {
let mut properties = vk::FormatProperties::new();
let result = unsafe {
self.instance.raw().get_physical_device_format_properties(
self.physical.handle,
format,
&mut properties,
)
};
matches!(result, Ok(())) && properties.optimal_tiling_features.contains(required)
}
pub(in crate::runtime) fn pipeline_cache(&self) -> Result<PipelineCacheGuard<'_>> {
let guard = self
.logical
.pipeline_creation
.lock()
.map_err(|_| Error::failed_precondition("Vulkan pipeline creation lock is poisoned"))?;
let raw = guard
.cache
.as_ref()
.map_or(vk::PipelineCache::NULL, pipeline::PipelineCache::raw);
Ok(PipelineCacheGuard { _guard: guard, raw })
}
pub(in crate::runtime) fn create_graphics_pipeline(&self) -> Result<pipeline::GraphicsPipeline> {
let cache = self.pipeline_cache()?;
pipeline::GraphicsPipeline::new(
std::sync::Arc::clone(&self.logical.handle),
self.physical.limits,
cache.raw(),
)
}
pub(in crate::runtime) fn create_flat_color_pipeline(
&self,
) -> Result<pipeline::FlatColorPipeline> {
let cache = self.pipeline_cache()?;
pipeline::FlatColorPipeline::new(
std::sync::Arc::clone(&self.logical.handle),
self.physical.limits,
cache.raw(),
)
}
pub(in crate::runtime) fn create_canvas_grid_pipeline(
&self,
) -> Result<pipeline::CanvasGridPipeline> {
let cache = self.pipeline_cache()?;
pipeline::CanvasGridPipeline::new(
std::sync::Arc::clone(&self.logical.handle),
self.physical.limits,
cache.raw(),
)
}
#[cfg(oa_profile = "core")]
pub(in crate::runtime) fn create_compute_pipeline(
&self,
artifact: &'static crate::runtime::dna::kernel::ShaderArtifact,
) -> Result<pipeline::ComputePipeline> {
let cache = self.pipeline_cache()?;
pipeline::ComputePipeline::new(
std::sync::Arc::clone(&self.logical.handle),
self.descriptor_layout(),
artifact,
self.physical.limits,
cache.raw(),
)
}
pub(in crate::runtime) fn create_unlit_descriptor_layouts(
&self,
) -> Result<pipeline::UnlitDescriptorLayouts> {
pipeline::UnlitDescriptorLayouts::new(std::sync::Arc::clone(&self.logical.handle))
}
pub(in crate::runtime) fn create_unlit_descriptor_arena(
&self,
layout: vk::DescriptorSetLayout,
set_count: u32,
) -> Result<pipeline::UnlitDescriptorArena> {
pipeline::UnlitDescriptorArena::new(
std::sync::Arc::clone(&self.logical.handle),
layout,
set_count,
)
}
pub(in crate::runtime) fn create_surface_descriptor_layouts(
&self,
) -> Result<pipeline::SurfaceDescriptorLayouts> {
pipeline::SurfaceDescriptorLayouts::new(std::sync::Arc::clone(&self.logical.handle))
}
pub(in crate::runtime) fn create_surface_descriptor_arena(
&self,
scene_layout: vk::DescriptorSetLayout,
material_layout: vk::DescriptorSetLayout,
set_count: u32,
max_materials_per_slot: u32,
) -> Result<pipeline::SurfaceDescriptorArena> {
pipeline::SurfaceDescriptorArena::new(
std::sync::Arc::clone(&self.logical.handle),
scene_layout,
material_layout,
set_count,
max_materials_per_slot,
)
}
pub(in crate::runtime) fn create_unlit_pipeline(
&self,
sampler_layout: vk::DescriptorSetLayout,
) -> Result<pipeline::UnlitPipeline> {
let cache = self.pipeline_cache()?;
pipeline::UnlitPipeline::new(
std::sync::Arc::clone(&self.logical.handle),
sampler_layout,
self.physical.limits,
cache.raw(),
)
}
pub(in crate::runtime) fn create_surface_pipeline(
&self,
scene_layout: vk::DescriptorSetLayout,
material_layout: vk::DescriptorSetLayout,
) -> Result<pipeline::SurfacePipeline> {
let cache = self.pipeline_cache()?;
pipeline::SurfacePipeline::new(
std::sync::Arc::clone(&self.logical.handle),
scene_layout,
material_layout,
self.physical.limits,
cache.raw(),
)
}
pub(in crate::runtime) fn create_surface_blend_pipeline(
&self,
layout: vk::PipelineLayout,
) -> Result<pipeline::SurfaceBlendPipeline> {
let cache = self.pipeline_cache()?;
pipeline::SurfaceBlendPipeline::new(
std::sync::Arc::clone(&self.logical.handle),
layout,
cache.raw(),
)
}
pub(in crate::runtime) fn allocate_mesh_buffer(
self: &Arc<Self>,
size: usize,
usage: vk::BufferUsageFlags,
) -> Result<MeshBuffer> {
MeshBuffer::new(self, size, usage)
}
pub(in crate::runtime) fn video_decode_capabilities(
&self,
profile: crate::video::VideoDecodeProfile,
) -> Result<crate::video::VideoDecodeCapabilities> {
video::query_decode_capabilities(&self.instance, &self.physical, profile)
}
pub(in crate::runtime) fn video_decode_formats(
&self,
profile: crate::video::VideoDecodeProfile,
) -> Result<crate::video::VideoDecodeFormats> {
video::query_decode_formats(&self.instance, &self.physical, profile)
}
pub(in crate::runtime) fn video_encode_capabilities(
&self,
profile: crate::video::VideoEncodeProfile,
) -> Result<crate::video::VideoEncodeCapabilities> {
video::query_encode_capabilities(&self.instance, &self.physical, profile)
}
pub(in crate::runtime) fn video_encode_formats(
&self,
profile: crate::video::VideoEncodeProfile,
) -> Result<crate::video::VideoEncodeFormats> {
video::query_encode_formats(&self.instance, &self.physical, profile)
}
pub(in crate::runtime) fn physical(&self) -> &DevicePhysical {
&self.physical
}
pub(in crate::runtime) fn pipeline_cache_data(&self) -> Option<Vec<u8>> {
self.logical.pipeline_cache_data()
}
pub(in crate::runtime) fn fusion_census(
&self,
) -> &crate::runtime::dna::fusion::census::FusionCensus {
&self.logical.fusion_census
}
pub(in crate::runtime) fn tape_bound_elements(&self) -> u32 {
self.logical.tape_bound_elements
}
pub(in crate::runtime) fn single_head_sdpa_route(&self) -> bool {
self.logical.single_head_sdpa_route
}
pub(in crate::runtime) fn submission_budget_ns(&self) -> u64 {
self.logical.submission_budget_ns
}
pub(in crate::runtime) fn jit_fusion_enabled(&self) -> bool {
crate::EnvFlag::is_set("OA_JIT_FUSION")
}
pub(in crate::runtime) fn jit_admit(
&self,
program: &Program,
tape_eligible: bool,
) -> Option<Vec<u8>> {
if !self.jit_fusion_enabled() {
self
.fusion_census()
.jit(crate::runtime::dna::fusion::census::JitOutcome::Disabled);
return None;
}
self
.logical
.jit
.lock()
.ok()?
.admit(self, program, tape_eligible)
}
pub(in crate::runtime) fn tile_gemm_enabled(&self) -> bool {
crate::EnvFlag::is_set("OA_TILE_GEMM")
}
pub(in crate::runtime) fn tile_signature(
&self,
program: &crate::runtime::dna::ir::tile::BlockProgram,
) -> u64 {
specialization::block_signature(program)
}
pub(in crate::runtime) fn jit_admit_tile(
&self,
program: &crate::runtime::dna::ir::tile::BlockProgram,
) -> Option<Vec<u8>> {
self.logical.jit.lock().ok()?.admit_block(self, program)
}
pub(in crate::runtime) fn tile_route_stats(&self, signature: u64) -> (Option<u64>, usize, u64) {
self
.logical
.jit
.lock()
.map(|mut jit| jit.route_stats(self, signature))
.unwrap_or((None, 0, 0))
}
pub(in crate::runtime) fn jit_admit_tile_gemm(
&self,
program: &crate::runtime::dna::ir::tile::BlockProgram,
) -> Option<Vec<u8>> {
if !self.tile_gemm_enabled() {
return None;
}
self.jit_admit_tile(program)
}
pub(in crate::runtime) fn tile_softmax_enabled(&self) -> bool {
crate::EnvFlag::is_set("OA_TILE_SOFTMAX")
}
pub(in crate::runtime) fn tile_video_enabled(&self) -> bool {
crate::EnvFlag::is_set("OA_TILE_VIDEO")
}
pub(in crate::runtime) fn jit_admit_tile_video(
&self,
program: &crate::runtime::dna::ir::tile::BlockProgram,
) -> Option<Vec<u8>> {
if !self.tile_video_enabled() {
return None;
}
self.jit_admit_tile(program)
}
pub(in crate::runtime) fn tile_image_enabled(&self) -> bool {
crate::EnvFlag::is_set("OA_TILE_IMAGE")
}
pub(in crate::runtime) fn jit_admit_tile_image(
&self,
program: &crate::runtime::dna::ir::tile::BlockProgram,
) -> Option<Vec<u8>> {
if !self.tile_image_enabled() {
return None;
}
self.jit_admit_tile(program)
}
pub(in crate::runtime) fn tile_audio_enabled(&self) -> bool {
crate::EnvFlag::is_set("OA_TILE_AUDIO")
}
pub(in crate::runtime) fn jit_admit_tile_audio(
&self,
program: &crate::runtime::dna::ir::tile::BlockProgram,
) -> Option<Vec<u8>> {
if !self.tile_audio_enabled() {
return None;
}
self.jit_admit_tile(program)
}
pub(in crate::runtime) fn jit_admit_tile_softmax(
&self,
program: &crate::runtime::dna::ir::tile::BlockProgram,
) -> Option<Vec<u8>> {
if !self.tile_softmax_enabled() {
return None;
}
self.jit_admit_tile(program)
}
pub(in crate::runtime) fn tile_gemv_enabled(&self) -> bool {
crate::EnvFlag::is_set("OA_TILE_GEMV")
}
pub(in crate::runtime) fn jit_admit_tile_gemv(
&self,
program: &crate::runtime::dna::ir::tile::BlockProgram,
) -> Option<Vec<u8>> {
if !self.tile_gemv_enabled() {
return None;
}
self.jit_admit_tile(program)
}
pub(in crate::runtime) fn tile_log_softmax_enabled(&self) -> bool {
crate::EnvFlag::is_set("OA_TILE_LOG_SOFTMAX")
}
pub(in crate::runtime) fn tile_rms_norm_enabled(&self) -> bool {
crate::EnvFlag::is_set("OA_TILE_RMS_NORM")
}
pub(in crate::runtime) fn tile_layer_norm_enabled(&self) -> bool {
crate::EnvFlag::is_set("OA_TILE_LAYER_NORM")
}
pub(in crate::runtime) fn tile_softmax_backward_enabled(&self) -> bool {
crate::EnvFlag::is_set("OA_TILE_SOFTMAX_BACKWARD")
}
pub(in crate::runtime) fn tile_log_softmax_backward_enabled(&self) -> bool {
crate::EnvFlag::is_set("OA_TILE_LOG_SOFTMAX_BACKWARD")
}
pub(in crate::runtime) fn tile_layer_norm_backward_enabled(&self) -> bool {
crate::EnvFlag::is_set("OA_TILE_LAYER_NORM_BACKWARD")
}
pub(in crate::runtime) fn tile_rms_norm_backward_enabled(&self) -> bool {
crate::EnvFlag::is_set("OA_TILE_RMS_NORM_BACKWARD")
}
pub(in crate::runtime) fn tile_linear_enabled(&self) -> bool {
crate::EnvFlag::is_set("OA_TILE_LINEAR")
}
pub(in crate::runtime) fn tile_linear_act_enabled(&self) -> bool {
crate::EnvFlag::is_set("OA_TILE_LINEAR_ACT")
}
pub(in crate::runtime) fn tile_sum_enabled(&self) -> bool {
crate::EnvFlag::is_set("OA_TILE_SUM")
}
pub(in crate::runtime) fn tile_sum_axis_enabled(&self) -> bool {
crate::EnvFlag::is_set("OA_TILE_SUM_AXIS")
}
pub(in crate::runtime) fn tile_linear_triple_enabled(&self) -> bool {
crate::EnvFlag::is_set("OA_TILE_LINEAR_TRIPLE")
}
pub(in crate::runtime) fn tile_linear_concat_enabled(&self) -> bool {
crate::EnvFlag::is_set("OA_TILE_LINEAR_CONCAT")
}
pub(in crate::runtime) fn tile_residual_norm_enabled(&self) -> bool {
crate::EnvFlag::is_set("OA_TILE_RESIDUAL_NORM")
}
pub(in crate::runtime) fn tile_qkv_enabled(&self) -> bool {
crate::EnvFlag::is_set("OA_TILE_QKV")
}
pub(in crate::runtime) fn tile_gate_up_enabled(&self) -> bool {
crate::EnvFlag::is_set("OA_TILE_GATE_UP")
}
pub(in crate::runtime) fn tile_grouped_linear_act_enabled(&self) -> bool {
crate::EnvFlag::is_set("OA_TILE_GROUPED_LINEAR_ACT")
}
pub(in crate::runtime) fn tile_grouped_linear_enabled(&self) -> bool {
crate::EnvFlag::is_set("OA_TILE_GROUPED_LINEAR")
}
pub(in crate::runtime) fn tile_grouped_gemm_enabled(&self) -> bool {
crate::EnvFlag::is_set("OA_TILE_GROUPED_GEMM")
}
pub(in crate::runtime) fn tile_grouped_backward_enabled(&self) -> bool {
crate::EnvFlag::is_set("OA_TILE_GROUPED_BACKWARD")
}
pub(in crate::runtime) fn tile_moe_enabled(&self) -> bool {
crate::EnvFlag::is_set("OA_TILE_MOE")
}
pub(in crate::runtime) fn tile_conv_enabled(&self) -> bool {
crate::EnvFlag::is_set("OA_TILE_CONV")
}
pub(in crate::runtime) fn tile_pool_enabled(&self) -> bool {
crate::EnvFlag::is_set("OA_TILE_POOL")
}
pub(in crate::runtime) fn tile_cross_entropy_enabled(&self) -> bool {
crate::EnvFlag::is_set("OA_TILE_CROSS_ENTROPY")
}
pub(in crate::runtime) fn tile_embedding_enabled(&self) -> bool {
crate::EnvFlag::is_set("OA_TILE_EMBEDDING")
}
pub(in crate::runtime) fn tile_max_enabled(&self) -> bool {
crate::EnvFlag::is_set("OA_TILE_MAX")
}
pub(in crate::runtime) fn tile_max_wave_enabled(&self) -> bool {
crate::EnvFlag::is_set("OA_TILE_MAX_WAVE")
}
pub(in crate::runtime) fn tile_attention_enabled(&self) -> bool {
crate::EnvFlag::is_set("OA_TILE_ATTENTION")
}
pub(in crate::runtime) fn tile_linear_backward_enabled(&self) -> bool {
crate::EnvFlag::is_set("OA_TILE_LINEAR_BACKWARD")
}
pub(in crate::runtime) fn tile_sum_wave_enabled(&self) -> bool {
crate::EnvFlag::is_set("OA_TILE_SUM_WAVE")
}
pub(in crate::runtime) fn tile_reduce_order(&self) -> TileReduceOrder {
match crate::EnvFlag::get_string("OA_TILE_REDUCE_ORDER", "").as_str() {
"throughput" => TileReduceOrder::Throughput,
"deterministic" => TileReduceOrder::Deterministic,
_ => TileReduceOrder::Default,
}
}
pub(in crate::runtime) fn tile_reduce_wave_offered(&self, per_kernel_flag: bool) -> bool {
let offered = match self.tile_reduce_order() {
TileReduceOrder::Deterministic => false,
TileReduceOrder::Throughput => true,
TileReduceOrder::Default => per_kernel_flag,
};
offered
&& (self.physical().limits.subgroup_supported_stages & vk::ShaderStageFlagBits::COMPUTE_BIT)
== vk::ShaderStageFlagBits::COMPUTE_BIT
&& (self.physical().limits.subgroup_supported_operations
& vk::SubgroupFeatureFlagBits::ARITHMETIC_BIT)
== vk::SubgroupFeatureFlagBits::ARITHMETIC_BIT
}
pub(in crate::runtime) fn tile_gemm_bias_enabled(&self) -> bool {
crate::EnvFlag::is_set("OA_TILE_GEMM_BIAS")
}
pub(in crate::runtime) fn tile_gemm_search_enabled(&self) -> bool {
crate::EnvFlag::is_set("OA_TILE_GEMM_SEARCH")
}
pub(in crate::runtime) fn jit_pipeline(
&self,
key: &[u8],
) -> Option<std::sync::Arc<pipeline::ComputePipeline>> {
if let Ok(extensions) = self.logical.extensions.lock()
&& let Some(pipeline) = extensions.get(key)
{
return Some(pipeline.clone());
}
self.logical.jit.lock().ok()?.pipeline(self, key)
}
pub(crate) fn admit_extension_pipeline(
&self,
key: Vec<u8>,
words: &[u32],
workgroup_size: [u32; 3],
) -> Result<Arc<pipeline::ComputePipeline>> {
let mut extensions = self
.logical
.extensions
.lock()
.map_err(|_| Error::internal("extension pipeline table lock poisoned"))?;
if let Some(pipeline) = extensions.get(&key) {
return Ok(pipeline.clone());
}
let pipeline = pipeline::ComputePipeline::from_words(
self.raw_arc(),
self.descriptor_layout(),
words,
workgroup_size,
self.physical().limits,
self.pipeline_cache()?.raw(),
)?;
let pipeline = Arc::new(pipeline);
extensions.insert(key, pipeline.clone());
Ok(pipeline)
}
pub(in crate::runtime) fn jit_section_updates(&self) -> Vec<vkcache::SectionUpdate> {
let census = self.fusion_census();
self
.logical
.jit
.lock()
.map(|mut fusion| fusion.section_updates(census))
.unwrap_or_default()
}
pub(in crate::runtime) fn route_timings(
self: &Arc<Self>,
entries: Vec<(u64, ObservedRoute)>,
) -> Result<Arc<RouteTimings>> {
RouteTimings::new(
self,
entries,
self.logical.timestamp_period_ns,
self.logical.compute_timestamp_valid_bits,
)
}
pub(in crate::runtime) fn jit_observe_timings(&self, timings: Arc<RouteTimings>) {
if let Ok(mut fusion) = self.logical.jit.lock() {
fusion.observe(timings);
}
}
pub(in crate::runtime) fn instance(&self) -> &Instance {
&self.instance
}
pub(in crate::runtime) fn graphics_queue_handle(&self) -> Result<vk::Queue> {
self
.logical
.graphics_queue
.as_ref()
.copied()
.ok_or_else(|| Error::missing_capability("no Vulkan graphics queue is enabled"))
}
pub(in crate::runtime) fn create_video_decode_session(
self: &Arc<Self>,
profile: crate::video::VideoDecodeProfile,
coded_extent: crate::video::VideoExtent,
max_dpb_slots: u32,
max_active_references: u32,
) -> Result<video::DecodeSession> {
video::create_decode_session(
self,
profile,
coded_extent,
max_dpb_slots,
max_active_references,
)
}
pub(in crate::runtime) fn create_video_encode_session(
self: &Arc<Self>,
settings: &crate::video::VideoEncodeConfig,
plan: crate::video::VideoEncodePlan,
) -> Result<video::EncodeSession> {
video::create_encode_session(self, settings, plan)
}
pub(in crate::runtime) fn allocator(&self) -> &vk::mem::SynchronizedAllocator<'static> {
&self.logical.allocator
}
pub(in crate::runtime) fn bind_storage_buffer(
&self,
buffer: vk::Buffer,
size: vk::DeviceSize,
) -> Result<u32> {
self
.logical
.descriptors
.bind_storage_buffer(&self.logical.handle, buffer, size)
}
pub(in crate::runtime) fn release_storage_buffer(&self, index: u32) {
self.logical.descriptors.release_storage_buffer(index);
}
pub(in crate::runtime) fn descriptor_write_count(&self) -> u64 {
self.logical.descriptors.write_count()
}
pub(in crate::runtime) fn descriptor_set(&self) -> vk::DescriptorSet {
self.logical.descriptors.set()
}
pub(in crate::runtime) fn descriptor_layout(&self) -> vk::DescriptorSetLayout {
self.logical.descriptors.layout()
}
pub(in crate::runtime) fn bounded_descriptor_layout(
&self,
count: u32,
) -> Option<vk::DescriptorSetLayout> {
self.logical.descriptors.bounded_layout(count)
}
pub(in crate::runtime) fn bind_storage_image(&self, view: vk::ImageView) -> Result<u32> {
self
.logical
.descriptors
.bind_storage_image(&self.logical.handle, view)
}
pub(in crate::runtime) fn release_storage_image(&self, index: u32) {
self.logical.descriptors.release_storage_image(index);
}
pub(super) fn take_recycled_storage_buffer(&self, size: usize) -> Option<RecycledBuffer> {
let mut pool = match self.logical.storage_pool.lock() {
Ok(pool) => pool,
Err(poisoned) => poisoned.into_inner(),
};
let index = pool.free.iter().rposition(|buffer| buffer.size == size)?;
let buffer = pool.free.swap_remove(index);
pool.bytes = pool.bytes.saturating_sub(buffer.size);
Some(buffer)
}
pub(super) fn recycle_storage_buffer(&self, buffer: RecycledBuffer) -> Option<RecycledBuffer> {
let mut pool = match self.logical.storage_pool.lock() {
Ok(pool) => pool,
Err(poisoned) => poisoned.into_inner(),
};
let Some(bytes) = pool.bytes.checked_add(buffer.size) else {
return Some(buffer);
};
if pool.free.len() >= MAX_RECYCLED_STORAGE_BUFFERS || bytes > MAX_RECYCLED_STORAGE_BYTES {
return Some(buffer);
}
pool.bytes = bytes;
pool.free.push(buffer);
None
}
pub(in crate::runtime) fn discard_one_recycled_storage_buffer(&self) -> bool {
let buffer = {
let mut pool = match self.logical.storage_pool.lock() {
Ok(pool) => pool,
Err(poisoned) => poisoned.into_inner(),
};
let Some(buffer) = pool.free.pop() else {
return false;
};
pool.bytes = pool.bytes.saturating_sub(buffer.size);
buffer
};
self.destroy_recycled_storage_buffer(buffer);
true
}
fn destroy_recycled_storage_buffer(&self, mut buffer: RecycledBuffer) {
self.release_storage_buffer(buffer.descriptor_index);
unsafe {
let _ = self
.allocator()
.destroy_buffer(buffer.handle, &mut buffer.allocation);
}
}
pub(in crate::runtime) fn same_as(&self, other: &Self) -> bool {
std::ptr::eq(self, other)
}
pub(in crate::runtime) fn prepare_mlkem_keygen<'a>(
self: &'a Arc<Self>,
seed: secret_buffer::SecretBinding<'a>,
private: secret_buffer::SecretBinding<'a>,
public: &Buffer,
k: u32,
) -> Result<pqc::PreparedSecretKeygen<'a>> {
let operands = pqc::SecretKeygenOperands::mlkem(self, seed, private, public, k)?;
self.prepare_secret_keygen(operands)
}
pub(in crate::runtime) fn prepare_mldsa_keygen<'a>(
self: &'a Arc<Self>,
seed: secret_buffer::SecretBinding<'a>,
private: secret_buffer::SecretBinding<'a>,
public: &Buffer,
parameter: u32,
) -> Result<pqc::PreparedSecretKeygen<'a>> {
let operands = pqc::SecretKeygenOperands::mldsa(self, seed, private, public, parameter)?;
self.prepare_secret_keygen(operands)
}
fn prepare_secret_keygen<'a>(
self: &'a Arc<Self>,
operands: pqc::SecretKeygenOperands<'a>,
) -> Result<pqc::PreparedSecretKeygen<'a>> {
let kernel = operands.kernel();
self.require_kernel(kernel)?;
let pipeline = self
.logical
.pipelines
.get(kernel.index())
.and_then(Option::as_ref)
.and_then(OnceLock::get)
.ok_or_else(|| Error::missing_capability("PQC keygen pipeline unavailable"))?;
operands.prepare(pipeline)
}
pub(super) fn prepare_secret_consumer<'a>(
self: &'a Arc<Self>,
operands: pqc::SecretConsumerOperands<'a>,
) -> Result<pqc::PreparedSecretConsumer<'a>> {
if !operands.belongs_to(self) {
return Err(Error::invalid_argument(
"PQC operands require their originating Device",
));
}
let kernel = operands.kernel();
self.require_kernel(kernel)?;
let pipeline = self
.logical
.pipelines
.get(kernel.index())
.and_then(Option::as_ref)
.and_then(OnceLock::get)
.ok_or_else(|| Error::missing_capability("PQC consumer pipeline unavailable"))?;
operands.prepare(pipeline)
}
pub(in crate::runtime) fn require_kernel(&self, kernel: KernelId) -> Result<()> {
let pipeline = self.logical.pipelines.get(kernel.index()).ok_or_else(|| {
Error::internal(format!(
"{} has no generated pipeline-table slot",
kernel.report_name()
))
})?;
if let Some(slot) = pipeline {
if slot.get().is_none() {
let mut creation = self
.logical
.pipeline_creation
.lock()
.map_err(|_| Error::failed_precondition("Vulkan pipeline creation lock is poisoned"))?;
if slot.get().is_none() {
let artifact = kernel.artifact();
let layout = if self.physical.profile == ExecutionProfile::Compatibility {
self
.logical
.descriptors
.bounded_layout(kernel.bounded_buffer_count())
.ok_or_else(|| {
Error::missing_capability("bounded pipeline descriptor layout is unavailable")
})?
} else {
self.logical.descriptors.layout()
};
let cache = creation
.cache
.as_ref()
.map_or(vk::PipelineCache::NULL, pipeline::PipelineCache::raw);
let created = match ComputePipeline::new(
std::sync::Arc::clone(&self.logical.handle),
layout,
artifact,
self.physical.limits,
cache,
) {
Ok(pipeline) => pipeline,
Err(first_error) => {
if creation.cache.is_none() {
return Err(first_error);
}
let scope = vkcache::VkCacheScope::new(&self.physical);
let scope_path = scope.path();
crate::log_warn!(
crate::LogComponent::COMPUTE,
"pipeline cache: creation failed for {}; dropping {}",
kernel.report_name(),
scope_path.display()
);
creation.cache = None;
let _ = vkcache::update(
&scope_path,
&scope,
&[vkcache::SectionUpdate::remove(
vkcache::SECTION_DRIVER_PIPELINE_CACHE,
)],
);
ComputePipeline::new(
std::sync::Arc::clone(&self.logical.handle),
layout,
artifact,
self.physical.limits,
vk::PipelineCache::NULL,
)?
}
};
if let Err(mut duplicate) = slot.set(created) {
duplicate.destroy(&self.logical.handle);
return Err(Error::internal(
"Vulkan pipeline was published outside its creation lock",
));
}
}
}
return Ok(());
}
if self.physical.profile == ExecutionProfile::Compatibility {
return Err(Error::missing_capability(format!(
"{} has no device-admitted bounded SPIR-V 1.5 pipeline on the Compatibility profile",
kernel.report_name()
)));
}
let requirements = kernel.artifact().requirements()?;
let missing = self.configuration.missing_shader_requirements(
self.physical.limits.subgroup_supported_stages,
self.physical.limits.subgroup_supported_operations,
requirements,
);
if missing.is_empty() {
return Err(Error::missing_capability(format!(
"{} has no qualified Vulkan pipeline",
kernel.report_name()
)));
}
Err(Error::missing_capability(format!(
"{} requires Vulkan capabilities not enabled on this device: {}",
kernel.report_name(),
missing.join(", ")
)))
}
pub(in crate::runtime) fn preload_pipelines(&self) -> Result<()> {
let mut jobs = Vec::new();
for kernel in KernelId::ALL {
let Some(Some(slot)) = self.logical.pipelines.get(kernel.index()) else {
continue;
};
if slot.get().is_some() {
continue;
}
let artifact = kernel.artifact();
let layout = if self.physical.profile == ExecutionProfile::Compatibility {
self
.logical
.descriptors
.bounded_layout(kernel.bounded_buffer_count())
.ok_or_else(|| {
Error::missing_capability("bounded pipeline descriptor layout is unavailable")
})?
} else {
self.logical.descriptors.layout()
};
jobs.push((kernel, artifact, layout));
}
if jobs.is_empty() {
return Ok(());
}
let (initial, warm) = {
let creation = self
.logical
.pipeline_creation
.lock()
.map_err(|_| Error::failed_precondition("Vulkan pipeline creation lock is poisoned"))?;
let initial = creation
.cache
.as_ref()
.and_then(|cache| cache.data().ok())
.unwrap_or_default();
let warm = creation
.cache
.as_ref()
.is_some_and(|cache| cache.initial_data_bytes() != 0);
(initial, warm)
};
let configured = crate::EnvFlag::get_int("OA_SHADER_LOAD_THREADS", 0);
let hardware = std::thread::available_parallelism().map_or(1, |count| count.get());
let workers = if configured > 0 {
usize::try_from(configured).unwrap_or(64).min(64)
} else if warm {
1
} else {
(hardware / 2).clamp(1, 8)
}
.clamp(1, jobs.len());
crate::log_info!(
crate::LogComponent::COMPUTE,
"Loading {} shader pipeline{} on demand ({} thread{}, {} cache)",
jobs.len(),
if jobs.len() == 1 { "" } else { "s" },
workers,
if workers == 1 { "" } else { "s" },
if warm { "warm" } else { "cold" }
);
let load_begin = std::time::Instant::now();
let mut failures: Vec<(KernelId, Error)> = Vec::new();
if workers == 1 {
for (kernel, _, _) in &jobs {
if let Err(error) = self.require_kernel(*kernel) {
failures.push((*kernel, error));
}
}
} else {
let device = Arc::clone(&self.logical.handle);
let worker_caches = (0..workers)
.map(|_| pipeline::PipelineCache::new(Arc::clone(&device), &initial).ok())
.collect::<Vec<_>>();
let next = std::sync::atomic::AtomicUsize::new(0);
let worker_failures = Mutex::new(Vec::<(KernelId, Error)>::new());
let next_ref = &next;
let failures_ref = &worker_failures;
let jobs = &jobs;
let pipelines = &self.logical.pipelines;
let limits = self.physical.limits;
let device = &device;
std::thread::scope(|scope| {
for worker_cache in &worker_caches {
scope.spawn(move || {
let cache = worker_cache
.as_ref()
.map_or(vk::PipelineCache::NULL, pipeline::PipelineCache::raw);
loop {
let index = next_ref.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
let Some((kernel, artifact, layout)) = jobs.get(index) else {
break;
};
let Some(Some(slot)) = pipelines.get(kernel.index()) else {
continue;
};
if slot.get().is_some() {
continue;
}
let created =
ComputePipeline::new(Arc::clone(device), *layout, artifact, limits, cache).or_else(
|_| {
ComputePipeline::new(
Arc::clone(device),
*layout,
artifact,
limits,
vk::PipelineCache::NULL,
)
},
);
match created {
Ok(pipeline) => {
if let Err(mut duplicate) = slot.set(pipeline) {
duplicate.destroy(device);
}
}
Err(error) => {
let _ = failures_ref
.lock()
.map(|mut failures| failures.push((*kernel, error)));
}
}
}
});
}
});
let creation = self
.logical
.pipeline_creation
.lock()
.map_err(|_| Error::failed_precondition("Vulkan pipeline creation lock is poisoned"))?;
if let Some(cache) = creation.cache.as_ref() {
let sources = worker_caches
.iter()
.flatten()
.map(pipeline::PipelineCache::raw)
.collect::<Vec<_>>();
if let Err(error) = cache.merge(&sources) {
crate::log_warn!(
crate::LogComponent::COMPUTE,
"pipeline cache: worker merge failed: {error}"
);
}
}
failures.append(&mut worker_failures.into_inner().unwrap_or_default());
}
let load_ms = load_begin.elapsed().as_secs_f64() * 1000.0;
for (kernel, error) in &failures {
crate::log_warn!(
crate::LogComponent::COMPUTE,
"Failed to load shader '{}' on demand: {}",
kernel.report_name(),
error
);
}
crate::log_info!(
crate::LogComponent::COMPUTE,
"Loaded {}/{} shader pipeline{} on demand (failed={}, threads={}, {:.2} ms)",
jobs.len() - failures.len(),
jobs.len(),
if jobs.len() == 1 { "" } else { "s" },
failures.len(),
workers,
load_ms
);
match failures.pop() {
Some((_, error)) => Err(error),
None => Ok(()),
}
}
pub(in crate::runtime) fn raw(&self) -> &vk::Device {
&self.logical().handle
}
pub(in crate::runtime) fn raw_arc(&self) -> Arc<vk::Device> {
(*self.logical().handle).clone()
}
pub(in crate::runtime) fn sync_commands(&self) -> &SyncCommands {
&self.logical.sync_commands
}
pub(in crate::runtime) fn rendering_commands(&self) -> RenderingCommands {
self.logical.rendering_commands.clone()
}
pub(in crate::runtime) unsafe fn pipeline_barrier(
&self,
command: vk::CommandBuffer,
dependency: &vk::DependencyInfo<'_>,
) -> Result<()> {
unsafe {
self
.logical
.sync_commands
.pipeline_barrier(&self.logical.handle, command, dependency)
.map_err(Error::from)
}
}
pub(in crate::runtime) unsafe fn queue_submit2(
&self,
queue: vk::Queue,
submits: &[vk::SubmitInfo2<'_>],
fence: vk::Fence,
) -> Result<()> {
unsafe {
self
.logical
.sync_commands
.submit(&self.logical.handle, queue, submits, fence)
.map_err(Error::from)
}
}
pub(in crate::runtime) unsafe fn write_timestamp(
&self,
command: vk::CommandBuffer,
stage: vk::PipelineStageFlags2,
pool: vk::QueryPool,
query: u32,
) -> Result<()> {
unsafe {
self
.logical
.sync_commands
.write_timestamp(&self.logical.handle, command, stage, pool, query)
.map_err(Error::from)
}
}
pub(in crate::runtime) fn record_empty(&self) -> Result<RecordedCommandBuffer> {
let mut command_pool = match self.logical.command_pool.lock() {
Ok(command_pool) => command_pool,
Err(poisoned) => poisoned.into_inner(),
};
command_pool.record_empty(&self.logical.handle)
}
pub(in crate::runtime) fn record_present_clear(
&self,
image: vk::Image,
color: [f32; 4],
) -> Result<RecordedCommandBuffer> {
let family = self
.physical
.graphics_queue_family
.ok_or_else(|| Error::missing_capability("no Vulkan graphics queue is enabled"))?;
let mut slot = self
.logical
.graphics_command_pool
.lock()
.unwrap_or_else(|poisoned| poisoned.into_inner());
if slot.is_none() {
*slot = Some(CommandPool::new(
&self.logical.handle,
family,
CommandPoolKind::Graphics,
)?);
}
slot
.as_mut()
.expect("graphics command pool initialized")
.record_custom(&self.logical.handle, |command| {
let range = vk::ImageSubresourceRange::new()
.set_aspect_mask(vk::ImageAspectFlagBits::COLOR_BIT)
.set_level_count(1)
.set_layer_count(1);
let barrier = vk::ImageMemoryBarrier2::new()
.set_src_stage_mask(vk::PipelineStageFlagBits2::NONE)
.set_dst_stage_mask(vk::PipelineStageFlagBits2::CLEAR_BIT)
.set_old_layout(vk::ImageLayout::UNDEFINED)
.set_new_layout(vk::ImageLayout::TRANSFER_DST_OPTIMAL)
.set_image(image)
.set_subresource_range(range);
let barriers = [barrier];
let dependency = vk::DependencyInfo::new().set_image_memory_barriers(&barriers);
unsafe { self.pipeline_barrier(command, &(dependency).map_err(Error::from)?) }?;
let clear = vk::ClearColorValue { float32: color };
unsafe {
self.cmd_clear_color_image(
command,
image,
vk::ImageLayout::TRANSFER_DST_OPTIMAL,
&clear,
&[range],
)
}?;
let barrier = vk::ImageMemoryBarrier2::new()
.set_src_stage_mask(vk::PipelineStageFlagBits2::CLEAR_BIT)
.set_src_access_mask(vk::AccessFlagBits2::TRANSFER_WRITE_BIT)
.set_dst_stage_mask(vk::PipelineStageFlagBits2::NONE)
.set_old_layout(vk::ImageLayout::TRANSFER_DST_OPTIMAL)
.set_new_layout(vk::ImageLayout::PRESENT_SRC_KHR)
.set_image(image)
.set_subresource_range(range);
let barriers = [barrier];
let dependency = vk::DependencyInfo::new().set_image_memory_barriers(&barriers);
unsafe { self.pipeline_barrier(command, &(dependency).map_err(Error::from)?) }?;
Ok(())
})
}
pub(in crate::runtime) fn record_present_blit(
&self,
source: vk::Image,
source_extent: [u32; 2],
target: vk::Image,
target_extent: [u32; 2],
) -> Result<RecordedCommandBuffer> {
let family = self
.physical
.graphics_queue_family
.ok_or_else(|| Error::missing_capability("no Vulkan graphics queue is enabled"))?;
let mut slot = self
.logical
.graphics_command_pool
.lock()
.unwrap_or_else(|poisoned| poisoned.into_inner());
if slot.is_none() {
*slot = Some(CommandPool::new(
&self.logical.handle,
family,
CommandPoolKind::Graphics,
)?);
}
slot
.as_mut()
.expect("graphics command pool initialized")
.record_custom(&self.logical.handle, |command| {
let range = vk::ImageSubresourceRange::new()
.set_aspect_mask(vk::ImageAspectFlagBits::COLOR_BIT)
.set_level_count(1)
.set_layer_count(1);
let barriers = [
vk::ImageMemoryBarrier2::new()
.set_src_stage_mask(vk::PipelineStageFlagBits2::ALL_COMMANDS_BIT)
.set_src_access_mask(vk::AccessFlagBits2::SHADER_SAMPLED_READ_BIT)
.set_dst_stage_mask(vk::PipelineStageFlagBits2::BLIT_BIT)
.set_dst_access_mask(vk::AccessFlagBits2::TRANSFER_READ_BIT)
.set_old_layout(vk::ImageLayout::SHADER_READ_ONLY_OPTIMAL)
.set_new_layout(vk::ImageLayout::TRANSFER_SRC_OPTIMAL)
.set_image(source)
.set_subresource_range(range),
vk::ImageMemoryBarrier2::new()
.set_src_stage_mask(vk::PipelineStageFlagBits2::NONE)
.set_dst_stage_mask(vk::PipelineStageFlagBits2::BLIT_BIT)
.set_dst_access_mask(vk::AccessFlagBits2::TRANSFER_WRITE_BIT)
.set_old_layout(vk::ImageLayout::UNDEFINED)
.set_new_layout(vk::ImageLayout::TRANSFER_DST_OPTIMAL)
.set_image(target)
.set_subresource_range(range),
];
unsafe {
self
.logical
.sync_commands
.pipeline_barrier(
&self.logical.handle,
command,
&(vk::DependencyInfo::new().set_image_memory_barriers(&barriers))
.map_err(Error::from)?,
)
.map_err(Error::from)?
};
let region = vk::ImageBlit::new()
.set_src_subresource(
vk::ImageSubresourceLayers::new()
.set_aspect_mask(vk::ImageAspectFlagBits::COLOR_BIT)
.set_layer_count(1),
)
.set_src_offsets([
vk::Offset3D::new(),
vk::Offset3D {
x: source_extent[0] as i32,
y: source_extent[1] as i32,
z: 1,
},
])
.set_dst_subresource(
vk::ImageSubresourceLayers::new()
.set_aspect_mask(vk::ImageAspectFlagBits::COLOR_BIT)
.set_layer_count(1),
)
.set_dst_offsets([
vk::Offset3D::new(),
vk::Offset3D {
x: target_extent[0] as i32,
y: target_extent[1] as i32,
z: 1,
},
]);
unsafe {
self.cmd_blit_image(
command,
source,
vk::ImageLayout::TRANSFER_SRC_OPTIMAL,
target,
vk::ImageLayout::TRANSFER_DST_OPTIMAL,
&[region],
vk::Filter::LINEAR,
)
}?;
let barriers = [
vk::ImageMemoryBarrier2::new()
.set_src_stage_mask(vk::PipelineStageFlagBits2::BLIT_BIT)
.set_src_access_mask(vk::AccessFlagBits2::TRANSFER_READ_BIT)
.set_dst_stage_mask(vk::PipelineStageFlagBits2::ALL_COMMANDS_BIT)
.set_dst_access_mask(vk::AccessFlagBits2::SHADER_SAMPLED_READ_BIT)
.set_old_layout(vk::ImageLayout::TRANSFER_SRC_OPTIMAL)
.set_new_layout(vk::ImageLayout::SHADER_READ_ONLY_OPTIMAL)
.set_image(source)
.set_subresource_range(range),
vk::ImageMemoryBarrier2::new()
.set_src_stage_mask(vk::PipelineStageFlagBits2::BLIT_BIT)
.set_src_access_mask(vk::AccessFlagBits2::TRANSFER_WRITE_BIT)
.set_dst_stage_mask(vk::PipelineStageFlagBits2::NONE)
.set_old_layout(vk::ImageLayout::TRANSFER_DST_OPTIMAL)
.set_new_layout(vk::ImageLayout::PRESENT_SRC_KHR)
.set_image(target)
.set_subresource_range(range),
];
unsafe {
self
.logical
.sync_commands
.pipeline_barrier(
&self.logical.handle,
command,
&(vk::DependencyInfo::new().set_image_memory_barriers(&barriers))
.map_err(Error::from)?,
)
.map_err(Error::from)?
};
Ok(())
})
}
pub(in crate::runtime) fn submit_present(
&self,
command: &RecordedCommandBuffer,
wait: vk::Semaphore,
signal: vk::Semaphore,
fence: vk::Fence,
) -> Result<()> {
let queue = *self
.logical
.graphics_queue
.as_ref()
.ok_or_else(|| Error::missing_capability("no Vulkan graphics queue is enabled"))?;
let waits = [vk::SemaphoreSubmitInfo::new()
.set_semaphore(wait)
.set_stage_mask(vk::PipelineStageFlagBits2::ALL_COMMANDS_BIT)];
let commands = [vk::CommandBufferSubmitInfo::new().set_command_buffer(command.raw())];
let signals = [vk::SemaphoreSubmitInfo::new()
.set_semaphore(signal)
.set_stage_mask(vk::PipelineStageFlagBits2::ALL_COMMANDS_BIT)];
let submits = [vk::SubmitInfo2::new()
.set_wait_semaphore_infos(&waits)
.map_err(Error::from)?
.set_command_buffer_infos(&commands)
.map_err(Error::from)?
.set_signal_semaphore_infos(&signals)
.map_err(Error::from)?];
unsafe {
self
.logical
.sync_commands
.submit(&self.logical.handle, queue, &submits, fence)
.map_err(|source| Error::backend_failure("Vulkan", "presentation queue submission", source))
}
}
#[allow(clippy::too_many_arguments)]
pub(in crate::runtime) fn record_mesh_frame(
&self,
target: &mut RenderTarget,
vertex_buffer: vk::Buffer,
index_buffer: vk::Buffer,
index_count: u32,
clear_color: [f32; 4],
push_data: &[u8],
push_stages: vk::ShaderStageFlags,
pipeline: vk::Pipeline,
pipeline_layout: vk::PipelineLayout,
descriptor_sets: &[vk::DescriptorSet],
) -> Result<RecordedCommandBuffer> {
self.record_draw_frame(
target,
FrameDraw::Indexed {
vertex_buffer,
index_buffer,
index_count,
},
clear_color,
push_data,
push_stages,
pipeline,
pipeline_layout,
descriptor_sets,
)
}
#[allow(clippy::too_many_arguments)]
pub(in crate::runtime) fn record_vertexless_frame(
&self,
target: &mut RenderTarget,
vertex_count: u32,
clear_color: [f32; 4],
push_data: &[u8],
push_stages: vk::ShaderStageFlags,
pipeline: vk::Pipeline,
pipeline_layout: vk::PipelineLayout,
descriptor_sets: &[vk::DescriptorSet],
) -> Result<RecordedCommandBuffer> {
self.record_draw_frame(
target,
FrameDraw::Vertexless { vertex_count },
clear_color,
push_data,
push_stages,
pipeline,
pipeline_layout,
descriptor_sets,
)
}
#[allow(clippy::too_many_arguments)]
fn record_draw_frame(
&self,
target: &mut RenderTarget,
draw: FrameDraw,
clear_color: [f32; 4],
push_data: &[u8],
push_stages: vk::ShaderStageFlags,
pipeline: vk::Pipeline,
pipeline_layout: vk::PipelineLayout,
descriptor_sets: &[vk::DescriptorSet],
) -> Result<RecordedCommandBuffer> {
let family = self
.physical
.graphics_queue_family
.ok_or_else(|| Error::missing_capability("no Vulkan graphics queue is enabled"))?;
let mut slot = self
.logical
.graphics_command_pool
.lock()
.unwrap_or_else(|p| p.into_inner());
if slot.is_none() {
*slot = Some(CommandPool::new(
&self.logical.handle,
family,
CommandPoolKind::Graphics,
)?);
}
let color_image = target.color_image;
let depth_image = target.depth_image;
let color_view = target.color_view;
let depth_view = target.depth_view;
let readback_buf = target.readback_buffer();
let width = target.width;
let height = target.height;
let old_color_layout = target.color_layout;
let color_range = vk::ImageSubresourceRange::new()
.set_aspect_mask(vk::ImageAspectFlagBits::COLOR_BIT)
.set_level_count(1)
.set_layer_count(1);
let depth_range = vk::ImageSubresourceRange::new()
.set_aspect_mask(vk::ImageAspectFlagBits::DEPTH_BIT)
.set_level_count(1)
.set_layer_count(1);
let result = slot
.as_mut()
.expect("graphics command pool initialized")
.record_custom(&self.logical.handle, |cmd| {
let to_attachment = [
vk::ImageMemoryBarrier2::new()
.set_src_stage_mask(
if old_color_layout == vk::ImageLayout::SHADER_READ_ONLY_OPTIMAL {
vk::PipelineStageFlagBits2::ALL_COMMANDS_BIT
} else {
vk::PipelineStageFlags2::empty()
},
)
.set_src_access_mask(
if old_color_layout == vk::ImageLayout::SHADER_READ_ONLY_OPTIMAL {
vk::AccessFlagBits2::SHADER_SAMPLED_READ_BIT
} else {
vk::AccessFlags2::empty()
},
)
.set_dst_stage_mask(vk::PipelineStageFlagBits2::COLOR_ATTACHMENT_OUTPUT_BIT)
.set_dst_access_mask(vk::AccessFlagBits2::COLOR_ATTACHMENT_WRITE_BIT)
.set_old_layout(old_color_layout)
.set_new_layout(vk::ImageLayout::COLOR_ATTACHMENT_OPTIMAL)
.set_image(color_image)
.set_subresource_range(color_range),
vk::ImageMemoryBarrier2::new()
.set_src_stage_mask(vk::PipelineStageFlags2::empty())
.set_dst_stage_mask(
vk::PipelineStageFlagBits2::EARLY_FRAGMENT_TESTS_BIT
| vk::PipelineStageFlagBits2::LATE_FRAGMENT_TESTS_BIT,
)
.set_dst_access_mask(
vk::AccessFlagBits2::DEPTH_STENCIL_ATTACHMENT_READ_BIT
| vk::AccessFlagBits2::DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
)
.set_old_layout(vk::ImageLayout::UNDEFINED)
.set_new_layout(vk::ImageLayout::DEPTH_STENCIL_ATTACHMENT_OPTIMAL)
.set_image(depth_image)
.set_subresource_range(depth_range),
];
unsafe {
self
.logical
.sync_commands
.pipeline_barrier(
&self.logical.handle,
cmd,
&(vk::DependencyInfo::new()
.set_image_memory_barriers(&to_attachment)
.map_err(Error::from)?),
)
.map_err(Error::from)?
};
let clear_cv = vk::ClearColorValue {
float32: clear_color,
};
let clear_dv = vk::ClearDepthStencilValue {
depth: 1.0,
stencil: 0,
};
let color_att = vk::RenderingAttachmentInfo::new()
.set_image_view(color_view)
.set_image_layout(vk::ImageLayout::COLOR_ATTACHMENT_OPTIMAL)
.set_load_op(vk::AttachmentLoadOp::CLEAR)
.set_store_op(vk::AttachmentStoreOp::STORE)
.set_clear_value(vk::ClearValue { color: clear_cv });
let depth_att = vk::RenderingAttachmentInfo::new()
.set_image_view(depth_view)
.set_image_layout(vk::ImageLayout::DEPTH_STENCIL_ATTACHMENT_OPTIMAL)
.set_load_op(vk::AttachmentLoadOp::CLEAR)
.set_store_op(vk::AttachmentStoreOp::DONT_CARE)
.set_clear_value(vk::ClearValue {
depth_stencil: clear_dv,
});
let rendering_info = vk::RenderingInfo::new()
.set_render_area(vk::Rect2D {
offset: vk::Offset2D::new(),
extent: vk::Extent2D { width, height },
})
.set_layer_count(1)
.set_color_attachments(std::slice::from_ref(&color_att))
.map_err(Error::from)?
.set_depth_attachment(&depth_att);
unsafe {
self
.logical
.rendering_commands
.begin(&self.logical.handle, cmd, &rendering_info)
}?;
let viewport = vk::Viewport {
x: 0.0,
y: 0.0,
width: width as f32,
height: height as f32,
min_depth: 0.0,
max_depth: 1.0,
};
let scissor = vk::Rect2D {
offset: vk::Offset2D::new(),
extent: vk::Extent2D { width, height },
};
unsafe {
self.cmd_set_viewport(cmd, 0, &[viewport])?;
self.cmd_set_scissor(cmd, 0, &[scissor])?;
};
unsafe {
self.cmd_bind_pipeline(cmd, vk::PipelineBindPoint::GRAPHICS, pipeline)?;
self.cmd_push_constants(cmd, pipeline_layout, push_stages, 0, push_data)?;
if !descriptor_sets.is_empty() {
self.cmd_bind_descriptor_sets(
cmd,
vk::PipelineBindPoint::GRAPHICS,
pipeline_layout,
0,
descriptor_sets,
&[],
)?;
}
};
unsafe {
match draw {
FrameDraw::Indexed {
vertex_buffer,
index_buffer,
index_count,
} => {
self.cmd_bind_vertex_buffers(cmd, 0, &[vertex_buffer], &[0])?;
self.cmd_bind_index_buffer(cmd, index_buffer, 0, vk::IndexType::UINT32)?;
self.cmd_draw_indexed(cmd, index_count, 1, 0, 0, 0)?;
}
FrameDraw::Vertexless { vertex_count } => {
self.cmd_draw(cmd, vertex_count, 1, 0, 0)?;
}
}
};
unsafe {
self
.logical
.rendering_commands
.end(&self.logical.handle, cmd)
}?;
let to_transfer = vk::ImageMemoryBarrier2::new()
.set_src_stage_mask(vk::PipelineStageFlagBits2::COLOR_ATTACHMENT_OUTPUT_BIT)
.set_src_access_mask(vk::AccessFlagBits2::COLOR_ATTACHMENT_WRITE_BIT)
.set_dst_stage_mask(vk::PipelineStageFlagBits2::COPY_BIT)
.set_dst_access_mask(vk::AccessFlagBits2::TRANSFER_READ_BIT)
.set_old_layout(vk::ImageLayout::COLOR_ATTACHMENT_OPTIMAL)
.set_new_layout(vk::ImageLayout::TRANSFER_SRC_OPTIMAL)
.set_image(color_image)
.set_subresource_range(color_range);
unsafe {
self
.logical
.sync_commands
.pipeline_barrier(
&self.logical.handle,
cmd,
&(vk::DependencyInfo::new())
.set_image_memory_barriers(std::slice::from_ref(&to_transfer))
.map_err(Error::from)?,
)
.map_err(Error::from)?
};
let copy = vk::BufferImageCopy::new()
.set_image_subresource(
vk::ImageSubresourceLayers::new()
.set_aspect_mask(vk::ImageAspectFlagBits::COLOR_BIT)
.set_layer_count(1),
)
.set_image_extent(vk::Extent3D {
width,
height,
depth: 1,
});
unsafe {
self.cmd_copy_image_to_buffer(
cmd,
color_image,
vk::ImageLayout::TRANSFER_SRC_OPTIMAL,
readback_buf,
&[copy],
)
}?;
let to_sampled = vk::ImageMemoryBarrier2::new()
.set_src_stage_mask(vk::PipelineStageFlagBits2::COPY_BIT)
.set_src_access_mask(vk::AccessFlagBits2::TRANSFER_READ_BIT)
.set_dst_stage_mask(vk::PipelineStageFlagBits2::ALL_COMMANDS_BIT)
.set_dst_access_mask(vk::AccessFlagBits2::SHADER_SAMPLED_READ_BIT)
.set_old_layout(vk::ImageLayout::TRANSFER_SRC_OPTIMAL)
.set_new_layout(vk::ImageLayout::SHADER_READ_ONLY_OPTIMAL)
.set_image(color_image)
.set_subresource_range(color_range);
unsafe {
self
.logical
.sync_commands
.pipeline_barrier(
&self.logical.handle,
cmd,
&(vk::DependencyInfo::new())
.set_image_memory_barriers(std::slice::from_ref(&to_sampled))
.map_err(Error::from)?,
)
.map_err(Error::from)?
};
Ok(())
});
if result.is_ok() {
target.color_layout = vk::ImageLayout::SHADER_READ_ONLY_OPTIMAL;
}
result
}
#[allow(clippy::too_many_arguments)]
pub(in crate::runtime) fn record_scene_frame(
&self,
target: &mut RenderTarget,
vertex_buffer: vk::Buffer,
index_buffer: vk::Buffer,
clear_color: [f32; 4],
push_stages: vk::ShaderStageFlags,
pipeline: vk::Pipeline,
blend_pipeline: vk::Pipeline,
pipeline_layout: vk::PipelineLayout,
scene_set: vk::DescriptorSet,
draws: &[SceneDrawCmd],
) -> Result<RecordedCommandBuffer> {
let family = self
.physical
.graphics_queue_family
.ok_or_else(|| Error::missing_capability("no Vulkan graphics queue is enabled"))?;
let mut slot = self
.logical
.graphics_command_pool
.lock()
.unwrap_or_else(|p| p.into_inner());
if slot.is_none() {
*slot = Some(CommandPool::new(
&self.logical.handle,
family,
CommandPoolKind::Graphics,
)?);
}
let color_image = target.color_image;
let depth_image = target.depth_image;
let color_view = target.color_view;
let depth_view = target.depth_view;
let readback_buf = target.readback_buffer();
let width = target.width;
let height = target.height;
let old_color_layout = target.color_layout;
let color_range = vk::ImageSubresourceRange::new()
.set_aspect_mask(vk::ImageAspectFlagBits::COLOR_BIT)
.set_level_count(1)
.set_layer_count(1);
let depth_range = vk::ImageSubresourceRange::new()
.set_aspect_mask(vk::ImageAspectFlagBits::DEPTH_BIT)
.set_level_count(1)
.set_layer_count(1);
let result = slot
.as_mut()
.expect("graphics command pool initialized")
.record_custom(&self.logical.handle, |cmd| {
let to_attachment = [
vk::ImageMemoryBarrier2::new()
.set_src_stage_mask(
if old_color_layout == vk::ImageLayout::SHADER_READ_ONLY_OPTIMAL {
vk::PipelineStageFlagBits2::ALL_COMMANDS_BIT
} else {
vk::PipelineStageFlags2::empty()
},
)
.set_src_access_mask(
if old_color_layout == vk::ImageLayout::SHADER_READ_ONLY_OPTIMAL {
vk::AccessFlagBits2::SHADER_SAMPLED_READ_BIT
} else {
vk::AccessFlags2::empty()
},
)
.set_dst_stage_mask(vk::PipelineStageFlagBits2::COLOR_ATTACHMENT_OUTPUT_BIT)
.set_dst_access_mask(vk::AccessFlagBits2::COLOR_ATTACHMENT_WRITE_BIT)
.set_old_layout(old_color_layout)
.set_new_layout(vk::ImageLayout::COLOR_ATTACHMENT_OPTIMAL)
.set_image(color_image)
.set_subresource_range(color_range),
vk::ImageMemoryBarrier2::new()
.set_src_stage_mask(vk::PipelineStageFlags2::empty())
.set_dst_stage_mask(
vk::PipelineStageFlagBits2::EARLY_FRAGMENT_TESTS_BIT
| vk::PipelineStageFlagBits2::LATE_FRAGMENT_TESTS_BIT,
)
.set_dst_access_mask(
vk::AccessFlagBits2::DEPTH_STENCIL_ATTACHMENT_READ_BIT
| vk::AccessFlagBits2::DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
)
.set_old_layout(vk::ImageLayout::UNDEFINED)
.set_new_layout(vk::ImageLayout::DEPTH_STENCIL_ATTACHMENT_OPTIMAL)
.set_image(depth_image)
.set_subresource_range(depth_range),
];
unsafe {
self
.logical
.sync_commands
.pipeline_barrier(
&self.logical.handle,
cmd,
&(vk::DependencyInfo::new()
.set_image_memory_barriers(&to_attachment)
.map_err(Error::from)?),
)
.map_err(Error::from)?
};
let clear_cv = vk::ClearColorValue {
float32: clear_color,
};
let clear_dv = vk::ClearDepthStencilValue {
depth: 1.0,
stencil: 0,
};
let color_att = vk::RenderingAttachmentInfo::new()
.set_image_view(color_view)
.set_image_layout(vk::ImageLayout::COLOR_ATTACHMENT_OPTIMAL)
.set_load_op(vk::AttachmentLoadOp::CLEAR)
.set_store_op(vk::AttachmentStoreOp::STORE)
.set_clear_value(vk::ClearValue { color: clear_cv });
let depth_att = vk::RenderingAttachmentInfo::new()
.set_image_view(depth_view)
.set_image_layout(vk::ImageLayout::DEPTH_STENCIL_ATTACHMENT_OPTIMAL)
.set_load_op(vk::AttachmentLoadOp::CLEAR)
.set_store_op(vk::AttachmentStoreOp::DONT_CARE)
.set_clear_value(vk::ClearValue {
depth_stencil: clear_dv,
});
let rendering_info = vk::RenderingInfo::new()
.set_render_area(vk::Rect2D {
offset: vk::Offset2D::new(),
extent: vk::Extent2D { width, height },
})
.set_layer_count(1)
.set_color_attachments(std::slice::from_ref(&color_att))
.map_err(Error::from)?
.set_depth_attachment(&depth_att);
unsafe {
self
.logical
.rendering_commands
.begin(&self.logical.handle, cmd, &rendering_info)
}?;
let viewport = vk::Viewport {
x: 0.0,
y: 0.0,
width: width as f32,
height: height as f32,
min_depth: 0.0,
max_depth: 1.0,
};
let scissor = vk::Rect2D {
offset: vk::Offset2D::new(),
extent: vk::Extent2D { width, height },
};
unsafe {
self.cmd_set_viewport(cmd, 0, &[viewport])?;
self.cmd_set_scissor(cmd, 0, &[scissor])?;
};
unsafe {
self.cmd_bind_vertex_buffers(cmd, 0, &[vertex_buffer], &[0])?;
self.cmd_bind_index_buffer(cmd, index_buffer, 0, vk::IndexType::UINT32)?;
self.cmd_bind_descriptor_sets(
cmd,
vk::PipelineBindPoint::GRAPHICS,
pipeline_layout,
0,
&[scene_set],
&[],
)?;
};
unsafe {
self.cmd_bind_pipeline(cmd, vk::PipelineBindPoint::GRAPHICS, pipeline)?;
}
for draw in draws.iter().filter(|d| !d.is_blend) {
unsafe {
self.cmd_push_constants(cmd, pipeline_layout, push_stages, 0, &draw.push_data)?;
self.cmd_bind_descriptor_sets(
cmd,
vk::PipelineBindPoint::GRAPHICS,
pipeline_layout,
1,
&[draw.material_set],
&[],
)?;
self.cmd_draw_indexed(cmd, draw.index_count, 1, draw.index_start, 0, 0)?;
}
}
let has_blend = draws.iter().any(|d| d.is_blend);
if has_blend {
unsafe {
self.cmd_bind_pipeline(cmd, vk::PipelineBindPoint::GRAPHICS, blend_pipeline)?;
}
for draw in draws.iter().filter(|d| d.is_blend) {
unsafe {
self.cmd_push_constants(cmd, pipeline_layout, push_stages, 0, &draw.push_data)?;
self.cmd_bind_descriptor_sets(
cmd,
vk::PipelineBindPoint::GRAPHICS,
pipeline_layout,
1,
&[draw.material_set],
&[],
)?;
self.cmd_draw_indexed(cmd, draw.index_count, 1, draw.index_start, 0, 0)?;
}
}
}
unsafe {
self
.logical
.rendering_commands
.end(&self.logical.handle, cmd)
}?;
let to_transfer = vk::ImageMemoryBarrier2::new()
.set_src_stage_mask(vk::PipelineStageFlagBits2::COLOR_ATTACHMENT_OUTPUT_BIT)
.set_src_access_mask(vk::AccessFlagBits2::COLOR_ATTACHMENT_WRITE_BIT)
.set_dst_stage_mask(vk::PipelineStageFlagBits2::COPY_BIT)
.set_dst_access_mask(vk::AccessFlagBits2::TRANSFER_READ_BIT)
.set_old_layout(vk::ImageLayout::COLOR_ATTACHMENT_OPTIMAL)
.set_new_layout(vk::ImageLayout::TRANSFER_SRC_OPTIMAL)
.set_image(color_image)
.set_subresource_range(color_range);
unsafe {
self
.logical
.sync_commands
.pipeline_barrier(
&self.logical.handle,
cmd,
&(vk::DependencyInfo::new())
.set_image_memory_barriers(std::slice::from_ref(&to_transfer))
.map_err(Error::from)?,
)
.map_err(Error::from)?
};
let copy = vk::BufferImageCopy::new()
.set_image_subresource(
vk::ImageSubresourceLayers::new()
.set_aspect_mask(vk::ImageAspectFlagBits::COLOR_BIT)
.set_layer_count(1),
)
.set_image_extent(vk::Extent3D {
width,
height,
depth: 1,
});
unsafe {
self.cmd_copy_image_to_buffer(
cmd,
color_image,
vk::ImageLayout::TRANSFER_SRC_OPTIMAL,
readback_buf,
&[copy],
)
}?;
let to_sampled = vk::ImageMemoryBarrier2::new()
.set_src_stage_mask(vk::PipelineStageFlagBits2::COPY_BIT)
.set_src_access_mask(vk::AccessFlagBits2::TRANSFER_READ_BIT)
.set_dst_stage_mask(vk::PipelineStageFlagBits2::ALL_COMMANDS_BIT)
.set_dst_access_mask(vk::AccessFlagBits2::SHADER_SAMPLED_READ_BIT)
.set_old_layout(vk::ImageLayout::TRANSFER_SRC_OPTIMAL)
.set_new_layout(vk::ImageLayout::SHADER_READ_ONLY_OPTIMAL)
.set_image(color_image)
.set_subresource_range(color_range);
unsafe {
self
.logical
.sync_commands
.pipeline_barrier(
&self.logical.handle,
cmd,
&(vk::DependencyInfo::new())
.set_image_memory_barriers(std::slice::from_ref(&to_sampled))
.map_err(Error::from)?,
)
.map_err(Error::from)?
};
Ok(())
});
if result.is_ok() {
target.color_layout = vk::ImageLayout::SHADER_READ_ONLY_OPTIMAL;
}
result
}
pub(in crate::runtime) fn record_presentation_commands(
&self,
record: impl FnOnce(vk::CommandBuffer) -> Result<()>,
) -> Result<RecordedCommandBuffer> {
let family = self
.physical
.graphics_queue_family
.ok_or_else(|| Error::missing_capability("no graphics queue is enabled"))?;
let mut slot = self
.logical
.graphics_command_pool
.lock()
.unwrap_or_else(|p| p.into_inner());
if slot.is_none() {
*slot = Some(CommandPool::new(
&self.logical.handle,
family,
CommandPoolKind::Graphics,
)?);
}
slot
.as_mut()
.expect("graphics command pool initialized")
.record_custom(&self.logical.handle, record)
}
pub(in crate::runtime) fn record_compute_commands(
&self,
record: impl FnOnce(vk::CommandBuffer) -> Result<()>,
) -> Result<RecordedCommandBuffer> {
let mut command_pool = match self.logical.command_pool.lock() {
Ok(command_pool) => command_pool,
Err(poisoned) => poisoned.into_inner(),
};
command_pool.record_custom(&self.logical.handle, record)
}
pub(in crate::runtime) fn record_video_decode(
&self,
record: impl FnOnce(vk::CommandBuffer) -> Result<()>,
) -> Result<RecordedCommandBuffer> {
let family = self
.physical
.video
.decode_queue_family
.ok_or_else(|| Error::missing_capability("no Vulkan Video decode queue is enabled"))?;
let mut slot = match self.logical.video_decode_command_pool.lock() {
Ok(slot) => slot,
Err(poisoned) => poisoned.into_inner(),
};
if slot.is_none() {
*slot = Some(CommandPool::new(
&self.logical.handle,
family,
CommandPoolKind::VideoDecode,
)?);
}
slot
.as_mut()
.expect("video decode command pool was initialized")
.record_custom(&self.logical.handle, record)
}
pub(in crate::runtime) fn record_video_encode(
&self,
record: impl FnOnce(vk::CommandBuffer) -> Result<()>,
) -> Result<RecordedCommandBuffer> {
let family = self
.physical
.video
.encode_queue_family
.ok_or_else(|| Error::missing_capability("no Vulkan Video encode queue is enabled"))?;
let mut slot = match self.logical.video_encode_command_pool.lock() {
Ok(slot) => slot,
Err(poisoned) => poisoned.into_inner(),
};
if slot.is_none() {
*slot = Some(CommandPool::new(
&self.logical.handle,
family,
CommandPoolKind::VideoEncode,
)?);
}
slot
.as_mut()
.expect("video encode command pool was initialized")
.record_custom(&self.logical.handle, record)
}
fn family_for_pool_kind(&self, kind: CommandPoolKind) -> Result<u32> {
let physical = &self.physical;
match kind {
CommandPoolKind::Compute => Ok(physical.compute_queue_family),
CommandPoolKind::Graphics => physical
.graphics_queue_family
.ok_or_else(|| Error::missing_capability("no Vulkan graphics queue is enabled")),
CommandPoolKind::Transfer => physical
.transfer_queue_family
.filter(|family| *family != physical.compute_queue_family)
.ok_or_else(|| Error::missing_capability("no dedicated Vulkan transfer queue is enabled")),
CommandPoolKind::VideoDecode => physical
.video
.decode_queue_family
.ok_or_else(|| Error::missing_capability("no Vulkan Video decode queue is enabled")),
CommandPoolKind::VideoEncode => physical
.video
.encode_queue_family
.ok_or_else(|| Error::missing_capability("no Vulkan Video encode queue is enabled")),
}
}
fn with_pool_for_family<R>(
&self,
family: u32,
record: impl FnOnce(&mut CommandPool) -> Result<R>,
) -> Result<R> {
let physical = &self.physical;
if family == physical.compute_queue_family {
let mut pool = self
.logical
.command_pool
.lock()
.unwrap_or_else(|poisoned| poisoned.into_inner());
return record(&mut pool);
}
let (slot, kind) = if physical.transfer_queue_family == Some(family) {
(
&self.logical.transfer_command_pool,
CommandPoolKind::Transfer,
)
} else if physical.graphics_queue_family == Some(family) {
(
&self.logical.graphics_command_pool,
CommandPoolKind::Graphics,
)
} else if physical.video.decode_queue_family == Some(family) {
(
&self.logical.video_decode_command_pool,
CommandPoolKind::VideoDecode,
)
} else if physical.video.encode_queue_family == Some(family) {
(
&self.logical.video_encode_command_pool,
CommandPoolKind::VideoEncode,
)
} else {
return Err(Error::missing_capability(format!(
"no queue or command pool is enabled for queue family {family}"
)));
};
let mut slot = slot.lock().unwrap_or_else(|poisoned| poisoned.into_inner());
if slot.is_none() {
*slot = Some(CommandPool::new(&self.logical.handle, family, kind)?);
}
record(slot.as_mut().expect("command pool was initialized"))
}
fn record_graph_range(
self: &Arc<Self>,
graph: &ExecutableGraph,
nodes: std::ops::Range<usize>,
timing: Option<TimestampPair>,
reusable_device: Option<Arc<Device>>,
) -> Result<RecordedCommandBuffer> {
let family = graph
.node_families()
.get(nodes.start)
.copied()
.ok_or_else(|| Error::invalid_argument("execution graph range is empty"))?;
self.with_pool_for_family(family, |pool| {
pool.record_compute_graph(
self,
&self.logical.pipelines,
self.logical.descriptors.set(),
graph,
nodes.clone(),
timing.clone(),
reusable_device.clone(),
)
})
}
pub(in crate::runtime) fn record_fresh_graph_range(
self: &Arc<Self>,
graph: &ExecutableGraph,
nodes: std::ops::Range<usize>,
timed: bool,
) -> Result<RecordedCommandBuffer> {
let timing = if timed {
let family = graph
.node_families()
.get(nodes.start)
.copied()
.ok_or_else(|| Error::invalid_argument("execution graph range is empty"))?;
Some(self.timestamp_pair_for(family)?)
} else {
None
};
self.record_graph_range(graph, nodes, timing, None)
}
pub(in crate::runtime) fn record_compute_graph(
self: &Arc<Self>,
graph: &ExecutableGraph,
) -> Result<RecordedCommandBuffer> {
self.record_graph_range(graph, 0..graph.nodes().len(), None, None)
}
pub(in crate::runtime) fn record_reusable_compute_graph(
self: &Arc<Self>,
graph: &ExecutableGraph,
) -> Result<ReusableCommandBuffer> {
let command =
self.record_graph_range(graph, 0..graph.nodes().len(), None, Some(self.clone()))?;
command.reusable().ok_or_else(|| {
Error::backend_failure(
"Vulkan",
"reusable command-buffer recording",
std::io::Error::other("recording did not retain reusable ownership"),
)
})
}
pub(in crate::runtime) fn record_reusable_graph_range(
self: &Arc<Self>,
graph: &ExecutableGraph,
nodes: std::ops::Range<usize>,
) -> Result<ReusableCommandBuffer> {
let command = self.record_graph_range(graph, nodes, None, Some(self.clone()))?;
command.reusable().ok_or_else(|| {
Error::backend_failure(
"Vulkan",
"reusable command-buffer recording",
std::io::Error::other("recording did not retain reusable ownership"),
)
})
}
pub(in crate::runtime) fn new_timestamp_pair(self: &Arc<Self>) -> Result<TimestampPair> {
self.timestamp_pair_for(self.physical.compute_queue_family)
}
pub(in crate::runtime) fn timestamp_pair_for(
self: &Arc<Self>,
family: u32,
) -> Result<TimestampPair> {
let valid_bits = self.physical.timestamp_valid_bits(family);
if valid_bits == 0 {
return Err(Error::missing_capability(format!(
"queue family {family} reports no timestamp support"
)));
}
TimestampPair::new(self, self.logical.timestamp_period_ns, valid_bits)
}
pub(in crate::runtime) fn record_timed_compute_graph(
self: &Arc<Self>,
graph: &ExecutableGraph,
) -> Result<RecordedCommandBuffer> {
let timing = self.new_timestamp_pair()?;
self.record_timed_compute_graph_with(graph, timing)
}
pub(in crate::runtime) fn record_timed_compute_graph_with(
self: &Arc<Self>,
graph: &ExecutableGraph,
timing: TimestampPair,
) -> Result<RecordedCommandBuffer> {
self.record_graph_range(graph, 0..graph.nodes().len(), Some(timing), None)
}
pub(in crate::runtime) fn time_reusable_command(
self: &Arc<Self>,
command: RecordedCommandBuffer,
) -> Result<RecordedCommandBuffer> {
let timing = self.timestamp_pair_for(self.family_for_pool_kind(command.pool_kind())?)?;
self.time_reusable_command_with(command, timing)
}
pub(in crate::runtime) fn time_reusable_command_with(
self: &Arc<Self>,
command: RecordedCommandBuffer,
timing: TimestampPair,
) -> Result<RecordedCommandBuffer> {
let family = self.family_for_pool_kind(command.pool_kind())?;
self.with_pool_for_family(family, |pool| {
let before = pool.record_custom(self.raw(), |buffer| timing.record_begin(buffer))?;
let after = match pool.record_custom(self.raw(), |buffer| timing.record_end(buffer)) {
Ok(after) => after,
Err(error) => {
pool.free_handle(self.raw(), before.raw());
return Err(error);
}
};
Ok(command.with_timing_brackets(before, after, timing))
})
}
pub(in crate::runtime) fn submit(
&self,
command: &RecordedCommandBuffer,
epoch: u64,
) -> Result<()> {
let command_infos = command
.submission_handles()
.into_iter()
.map(|handle| vk::CommandBufferSubmitInfo::new().set_command_buffer(handle))
.collect::<Vec<_>>();
let signal_infos = [vk::SemaphoreSubmitInfo::new()
.set_semaphore(self.logical.timeline.raw())
.set_value(epoch)
.set_stage_mask(vk::PipelineStageFlagBits2::ALL_COMMANDS_BIT)];
let wait_info = vk::SemaphoreSubmitInfo::new()
.set_semaphore(self.logical.timeline.raw())
.set_value(epoch.saturating_sub(1))
.set_stage_mask(vk::PipelineStageFlagBits2::ALL_COMMANDS_BIT);
let wait_infos = if epoch > 1 {
std::slice::from_ref(&wait_info)
} else {
&[]
};
let submit_infos = [vk::SubmitInfo2::new()
.set_wait_semaphore_infos(wait_infos)
.map_err(Error::from)?
.set_command_buffer_infos(&command_infos)
.map_err(Error::from)?
.set_signal_semaphore_infos(&signal_infos)
.map_err(Error::from)?];
let queue = match command.pool_kind() {
CommandPoolKind::Compute => self.logical.compute_queue,
CommandPoolKind::Graphics => *self
.logical
.graphics_queue
.as_ref()
.ok_or_else(|| Error::missing_capability("no Vulkan graphics queue is enabled"))?,
CommandPoolKind::Transfer => *self.logical.transfer_queue.as_ref().ok_or_else(|| {
Error::missing_capability("no dedicated Vulkan transfer queue is enabled")
})?,
CommandPoolKind::VideoDecode => *self
.logical
.video_decode_queue
.as_ref()
.ok_or_else(|| Error::missing_capability("no Vulkan Video decode queue is enabled"))?,
CommandPoolKind::VideoEncode => *self
.logical
.video_encode_queue
.as_ref()
.ok_or_else(|| Error::missing_capability("no Vulkan Video encode queue is enabled"))?,
};
unsafe { self.queue_submit2(queue, &submit_infos, vk::Fence::default()) }
.map_err(|source| Error::backend_failure("Vulkan", "queue submission", source))
}
pub(in crate::runtime) fn free(&self, mut command: RecordedCommandBuffer) {
if let Some(pair) = command.take_brackets() {
for bracket in *pair {
self.free(bracket);
}
}
let Some((handle, pool, _bounded_sets)) = command.into_owned_handle() else {
return;
};
self.free_command_buffer_handle(handle, pool);
}
pub(super) fn free_command_buffer_handle(
&self,
handle: vk::CommandBuffer,
pool: CommandPoolKind,
) {
match pool {
CommandPoolKind::Compute => {
let mut command_pool = match self.logical.command_pool.lock() {
Ok(command_pool) => command_pool,
Err(poisoned) => poisoned.into_inner(),
};
command_pool.free_handle(&self.logical.handle, handle);
}
CommandPoolKind::Graphics => {
let mut slot = match self.logical.graphics_command_pool.lock() {
Ok(pool) => pool,
Err(poisoned) => poisoned.into_inner(),
};
if let Some(command_pool) = slot.as_mut() {
command_pool.free_handle(&self.logical.handle, handle);
}
}
CommandPoolKind::Transfer => {
let mut slot = match self.logical.transfer_command_pool.lock() {
Ok(pool) => pool,
Err(poisoned) => poisoned.into_inner(),
};
if let Some(command_pool) = slot.as_mut() {
command_pool.free_handle(&self.logical.handle, handle);
}
}
CommandPoolKind::VideoDecode => {
let mut slot = match self.logical.video_decode_command_pool.lock() {
Ok(slot) => slot,
Err(poisoned) => poisoned.into_inner(),
};
if let Some(command_pool) = slot.as_mut() {
command_pool.free_handle(&self.logical.handle, handle);
}
}
CommandPoolKind::VideoEncode => {
let mut slot = match self.logical.video_encode_command_pool.lock() {
Ok(slot) => slot,
Err(poisoned) => poisoned.into_inner(),
};
if let Some(command_pool) = slot.as_mut() {
command_pool.free_handle(&self.logical.handle, handle);
}
}
}
}
pub(in crate::runtime) fn is_complete(&self, epoch: u64) -> Result<bool> {
self
.logical
.timeline
.is_complete(&self.logical.handle, epoch)
}
pub(in crate::runtime) fn wait(&self, epoch: u64) -> Result<()> {
self.logical.timeline.wait(&self.logical.handle, epoch)
}
#[inline]
pub(crate) unsafe fn cmd_dispatch(
&self,
command_buffer: vk::CommandBuffer,
group_count_x: u32,
group_count_y: u32,
group_count_z: u32,
) -> std::result::Result<(), vk::CallError> {
unsafe {
self
.raw()
.cmd_dispatch(command_buffer, group_count_x, group_count_y, group_count_z)
}
}
#[inline]
pub(crate) unsafe fn cmd_dispatch_indirect(
&self,
command_buffer: vk::CommandBuffer,
buffer: vk::Buffer,
offset: vk::DeviceSize,
) -> std::result::Result<(), vk::CallError> {
unsafe {
self
.raw()
.cmd_dispatch_indirect(command_buffer, buffer, offset)
}
}
#[inline]
pub(crate) unsafe fn cmd_bind_pipeline(
&self,
command_buffer: vk::CommandBuffer,
pipeline_bind_point: vk::PipelineBindPoint,
pipeline: vk::Pipeline,
) -> std::result::Result<(), vk::CallError> {
unsafe {
self
.raw()
.cmd_bind_pipeline(command_buffer, pipeline_bind_point, pipeline)
}
}
#[inline]
pub(crate) unsafe fn cmd_bind_descriptor_sets(
&self,
command_buffer: vk::CommandBuffer,
pipeline_bind_point: vk::PipelineBindPoint,
layout: vk::PipelineLayout,
first_set: u32,
descriptor_sets: &[vk::DescriptorSet],
dynamic_offsets: &[u32],
) -> std::result::Result<(), vk::CallError> {
unsafe {
self.raw().cmd_bind_descriptor_sets(
command_buffer,
pipeline_bind_point,
layout,
first_set,
descriptor_sets,
dynamic_offsets,
)
}
}
#[inline]
pub(crate) unsafe fn cmd_push_constants(
&self,
command_buffer: vk::CommandBuffer,
layout: vk::PipelineLayout,
stage_flags: vk::ShaderStageFlags,
offset: u32,
values: &[u8],
) -> std::result::Result<(), vk::CallError> {
unsafe {
self
.raw()
.cmd_push_constants(command_buffer, layout, stage_flags, offset, values)
}
}
#[inline]
pub(crate) unsafe fn cmd_copy_buffer(
&self,
command_buffer: vk::CommandBuffer,
src_buffer: vk::Buffer,
dst_buffer: vk::Buffer,
regions: &[vk::BufferCopy],
) -> std::result::Result<(), vk::CallError> {
unsafe {
self
.raw()
.cmd_copy_buffer(command_buffer, src_buffer, dst_buffer, regions)
}
}
#[inline]
pub(crate) unsafe fn cmd_copy_buffer_to_image(
&self,
command_buffer: vk::CommandBuffer,
src_buffer: vk::Buffer,
dst_image: vk::Image,
dst_image_layout: vk::ImageLayout,
regions: &[vk::BufferImageCopy],
) -> std::result::Result<(), vk::CallError> {
unsafe {
self.raw().cmd_copy_buffer_to_image(
command_buffer,
src_buffer,
dst_image,
dst_image_layout,
regions,
)
}
}
#[inline]
pub(crate) unsafe fn cmd_copy_image_to_buffer(
&self,
command_buffer: vk::CommandBuffer,
src_image: vk::Image,
src_image_layout: vk::ImageLayout,
dst_buffer: vk::Buffer,
regions: &[vk::BufferImageCopy],
) -> std::result::Result<(), vk::CallError> {
unsafe {
self.raw().cmd_copy_image_to_buffer(
command_buffer,
src_image,
src_image_layout,
dst_buffer,
regions,
)
}
}
#[inline]
#[allow(clippy::too_many_arguments)]
pub(crate) unsafe fn cmd_blit_image(
&self,
command_buffer: vk::CommandBuffer,
src_image: vk::Image,
src_image_layout: vk::ImageLayout,
dst_image: vk::Image,
dst_image_layout: vk::ImageLayout,
regions: &[vk::ImageBlit],
filter: vk::Filter,
) -> std::result::Result<(), vk::CallError> {
unsafe {
self.raw().cmd_blit_image(
command_buffer,
src_image,
src_image_layout,
dst_image,
dst_image_layout,
regions,
filter,
)
}
}
#[inline]
pub(crate) unsafe fn cmd_fill_buffer(
&self,
command_buffer: vk::CommandBuffer,
buffer: vk::Buffer,
offset: vk::DeviceSize,
size: vk::DeviceSize,
data: u32,
) -> std::result::Result<(), vk::CallError> {
unsafe {
self
.raw()
.cmd_fill_buffer(command_buffer, buffer, offset, size, data)
}
}
#[inline]
pub(crate) unsafe fn cmd_clear_color_image(
&self,
command_buffer: vk::CommandBuffer,
image: vk::Image,
image_layout: vk::ImageLayout,
color: &vk::ClearColorValue,
ranges: &[vk::ImageSubresourceRange],
) -> std::result::Result<(), vk::CallError> {
unsafe {
self
.raw()
.cmd_clear_color_image(command_buffer, image, image_layout, color, ranges)
}
}
#[inline]
pub(crate) unsafe fn cmd_pipeline_barrier2(
&self,
command_buffer: vk::CommandBuffer,
dependency_info: &vk::DependencyInfo<'_>,
) -> std::result::Result<(), vk::CallError> {
unsafe {
self
.raw()
.cmd_pipeline_barrier2(command_buffer, dependency_info)
}
}
#[inline]
pub(crate) unsafe fn cmd_reset_query_pool(
&self,
command_buffer: vk::CommandBuffer,
query_pool: vk::QueryPool,
first_query: u32,
query_count: u32,
) -> std::result::Result<(), vk::CallError> {
unsafe {
self
.raw()
.cmd_reset_query_pool(command_buffer, query_pool, first_query, query_count)
}
}
#[inline]
pub(crate) unsafe fn cmd_begin_query(
&self,
command_buffer: vk::CommandBuffer,
query_pool: vk::QueryPool,
query: u32,
flags: vk::QueryControlFlags,
) -> std::result::Result<(), vk::CallError> {
unsafe {
self
.raw()
.cmd_begin_query(command_buffer, query_pool, query, flags)
}
}
#[inline]
pub(crate) unsafe fn cmd_end_query(
&self,
command_buffer: vk::CommandBuffer,
query_pool: vk::QueryPool,
query: u32,
) -> std::result::Result<(), vk::CallError> {
unsafe { self.raw().cmd_end_query(command_buffer, query_pool, query) }
}
#[inline]
pub(crate) unsafe fn cmd_set_viewport(
&self,
command_buffer: vk::CommandBuffer,
first_viewport: u32,
viewports: &[vk::Viewport],
) -> std::result::Result<(), vk::CallError> {
unsafe {
self
.raw()
.cmd_set_viewport(command_buffer, first_viewport, viewports)
}
}
#[inline]
pub(crate) unsafe fn cmd_set_scissor(
&self,
command_buffer: vk::CommandBuffer,
first_scissor: u32,
scissors: &[vk::Rect2D],
) -> std::result::Result<(), vk::CallError> {
unsafe {
self
.raw()
.cmd_set_scissor(command_buffer, first_scissor, scissors)
}
}
#[inline]
pub(crate) unsafe fn cmd_bind_vertex_buffers(
&self,
command_buffer: vk::CommandBuffer,
first_binding: u32,
buffers: &[vk::Buffer],
offsets: &[vk::DeviceSize],
) -> std::result::Result<(), vk::CallError> {
unsafe {
self
.raw()
.cmd_bind_vertex_buffers(command_buffer, first_binding, buffers, offsets)
}
}
#[inline]
pub(crate) unsafe fn cmd_bind_index_buffer(
&self,
command_buffer: vk::CommandBuffer,
buffer: vk::Buffer,
offset: vk::DeviceSize,
index_type: vk::IndexType,
) -> std::result::Result<(), vk::CallError> {
unsafe {
self
.raw()
.cmd_bind_index_buffer(command_buffer, buffer, offset, index_type)
}
}
#[inline]
pub(crate) unsafe fn cmd_draw(
&self,
command_buffer: vk::CommandBuffer,
vertex_count: u32,
instance_count: u32,
first_vertex: u32,
first_instance: u32,
) -> std::result::Result<(), vk::CallError> {
unsafe {
self.raw().cmd_draw(
command_buffer,
vertex_count,
instance_count,
first_vertex,
first_instance,
)
}
}
#[inline]
pub(crate) unsafe fn cmd_draw_indexed(
&self,
command_buffer: vk::CommandBuffer,
index_count: u32,
instance_count: u32,
first_index: u32,
vertex_offset: i32,
first_instance: u32,
) -> std::result::Result<(), vk::CallError> {
unsafe {
self.raw().cmd_draw_indexed(
command_buffer,
index_count,
instance_count,
first_index,
vertex_offset,
first_instance,
)
}
}
#[inline]
pub(crate) unsafe fn cmd_begin_video_coding_khr(
&self,
command_buffer: vk::CommandBuffer,
p_begin_info: *const vk::VideoBeginCodingInfoKHR<'_>,
) -> std::result::Result<(), vk::CallError> {
unsafe {
self
.raw()
.cmd_begin_video_coding_khr(command_buffer, p_begin_info)
}
}
#[inline]
pub(crate) unsafe fn cmd_decode_video_khr(
&self,
command_buffer: vk::CommandBuffer,
p_decode_info: *const vk::VideoDecodeInfoKHR<'_>,
) -> std::result::Result<(), vk::CallError> {
unsafe {
self
.raw()
.cmd_decode_video_khr(command_buffer, p_decode_info)
}
}
#[inline]
pub(crate) unsafe fn cmd_encode_video_khr(
&self,
command_buffer: vk::CommandBuffer,
p_encode_info: *const vk::VideoEncodeInfoKHR<'_>,
) -> std::result::Result<(), vk::CallError> {
unsafe {
self
.raw()
.cmd_encode_video_khr(command_buffer, p_encode_info)
}
}
#[inline]
pub(crate) unsafe fn cmd_control_video_coding_khr(
&self,
command_buffer: vk::CommandBuffer,
p_coding_control_info: *const vk::VideoCodingControlInfoKHR<'_>,
) -> std::result::Result<(), vk::CallError> {
unsafe {
self
.raw()
.cmd_control_video_coding_khr(command_buffer, p_coding_control_info)
}
}
#[inline]
pub(crate) unsafe fn cmd_end_video_coding_khr(
&self,
command_buffer: vk::CommandBuffer,
p_end_coding_info: *const vk::VideoEndCodingInfoKHR<'_>,
) -> std::result::Result<(), vk::CallError> {
unsafe {
self
.raw()
.cmd_end_video_coding_khr(command_buffer, p_end_coding_info)
}
}
#[inline]
pub(crate) unsafe fn queue_present_khr(
&self,
queue: vk::Queue,
p_present_info: *const vk::PresentInfoKHR<'_>,
) -> std::result::Result<vk::Result, vk::CallError> {
unsafe { self.raw().queue_present_khr(queue, p_present_info) }
}
#[inline]
pub(crate) unsafe fn acquire_next_image_khr(
&self,
swapchain: vk::SwapchainKHR,
timeout: u64,
semaphore: vk::Semaphore,
fence: vk::Fence,
p_image_index: *mut u32,
) -> std::result::Result<vk::Result, vk::CallError> {
unsafe {
self
.raw()
.acquire_next_image_khr(swapchain, timeout, semaphore, fence, p_image_index)
}
}
#[inline]
pub(crate) unsafe fn wait_for_fences(
&self,
fences: &[vk::Fence],
wait_all: bool,
timeout_ns: u64,
) -> std::result::Result<bool, vk::CallError> {
unsafe { self.raw().wait_for_fences(fences, wait_all, timeout_ns) }
}
#[inline]
pub(crate) unsafe fn reset_fences(
&self,
fences: &[vk::Fence],
) -> std::result::Result<(), vk::CallError> {
unsafe { self.raw().reset_fences(fences) }
}
#[inline]
pub(crate) unsafe fn device_wait_idle(&self) -> std::result::Result<(), vk::CallError> {
unsafe { self.raw().device_wait_idle() }
}
#[inline]
pub(crate) unsafe fn get_query_pool_results(
&self,
query_pool: vk::QueryPool,
first_query: u32,
query_count: u32,
data: &mut [u8],
stride: vk::DeviceSize,
flags: vk::QueryResultFlags,
) -> std::result::Result<vk::Result, vk::CallError> {
unsafe {
self
.raw()
.get_query_pool_results(query_pool, first_query, query_count, data, stride, flags)
}
}
#[inline]
pub(crate) unsafe fn get_video_session_memory_requirements_khr(
&self,
video_session: vk::VideoSessionKHR,
p_memory_requirements_count: *mut u32,
p_memory_requirements: *mut vk::VideoSessionMemoryRequirementsKHR<'_>,
) -> std::result::Result<vk::Result, vk::CallError> {
unsafe {
self.raw().get_video_session_memory_requirements_khr(
video_session,
p_memory_requirements_count,
p_memory_requirements,
)
}
}
#[inline]
pub(crate) unsafe fn get_encoded_video_session_parameters_khr(
&self,
p_video_session_parameters_info: *const vk::VideoEncodeSessionParametersGetInfoKHR<'_>,
p_feedback_info: *mut vk::VideoEncodeSessionParametersFeedbackInfoKHR<'_>,
p_data_size: *mut usize,
p_data: *mut std::ffi::c_void,
) -> std::result::Result<vk::Result, vk::CallError> {
unsafe {
self.raw().get_encoded_video_session_parameters_khr(
p_video_session_parameters_info,
p_feedback_info,
p_data_size,
p_data,
)
}
}
#[inline]
pub(crate) unsafe fn create_image_view(
&self,
create_info: &vk::ImageViewCreateInfo<'_>,
allocator: Option<&vk::AllocationCallbacks>,
) -> std::result::Result<vk::ImageView, vk::CallError> {
unsafe { self.raw().create_image_view(create_info, allocator) }
}
#[inline]
pub(crate) unsafe fn destroy_image_view(
&self,
image_view: vk::ImageView,
allocator: Option<&vk::AllocationCallbacks>,
) -> std::result::Result<(), vk::CallError> {
unsafe { self.raw().destroy_image_view(image_view, allocator) }
}
#[inline]
pub(crate) unsafe fn create_semaphore(
&self,
create_info: &vk::SemaphoreCreateInfo<'_>,
allocator: Option<&vk::AllocationCallbacks>,
) -> std::result::Result<vk::Semaphore, vk::CallError> {
unsafe { self.raw().create_semaphore(create_info, allocator) }
}
#[inline]
pub(crate) unsafe fn destroy_semaphore(
&self,
semaphore: vk::Semaphore,
allocator: Option<&vk::AllocationCallbacks>,
) -> std::result::Result<(), vk::CallError> {
unsafe { self.raw().destroy_semaphore(semaphore, allocator) }
}
#[inline]
pub(crate) unsafe fn create_fence(
&self,
create_info: &vk::FenceCreateInfo,
allocator: Option<&vk::AllocationCallbacks>,
) -> std::result::Result<vk::Fence, vk::CallError> {
unsafe { self.raw().create_fence(create_info, allocator) }
}
#[inline]
pub(crate) unsafe fn destroy_fence(
&self,
fence: vk::Fence,
allocator: Option<&vk::AllocationCallbacks>,
) -> std::result::Result<(), vk::CallError> {
unsafe { self.raw().destroy_fence(fence, allocator) }
}
#[inline]
pub(crate) unsafe fn create_query_pool(
&self,
create_info: &vk::QueryPoolCreateInfo<'_>,
allocator: Option<&vk::AllocationCallbacks>,
) -> std::result::Result<vk::QueryPool, vk::CallError> {
unsafe { self.raw().create_query_pool(create_info, allocator) }
}
#[inline]
pub(crate) unsafe fn destroy_query_pool(
&self,
query_pool: vk::QueryPool,
allocator: Option<&vk::AllocationCallbacks>,
) -> std::result::Result<(), vk::CallError> {
unsafe { self.raw().destroy_query_pool(query_pool, allocator) }
}
#[inline]
pub(crate) unsafe fn create_swapchain_khr(
&self,
p_create_info: *const vk::SwapchainCreateInfoKHR<'_>,
p_allocator: *const vk::AllocationCallbacks,
p_swapchain: *mut vk::SwapchainKHR,
) -> std::result::Result<vk::Result, vk::CallError> {
unsafe {
self
.raw()
.create_swapchain_khr(p_create_info, p_allocator, p_swapchain)
}
}
#[inline]
pub(crate) unsafe fn destroy_swapchain_khr(
&self,
swapchain: vk::SwapchainKHR,
p_allocator: *const vk::AllocationCallbacks,
) -> std::result::Result<(), vk::CallError> {
unsafe { self.raw().destroy_swapchain_khr(swapchain, p_allocator) }
}
#[inline]
pub(crate) unsafe fn create_video_session_khr(
&self,
p_create_info: *const vk::VideoSessionCreateInfoKHR<'_>,
p_allocator: *const vk::AllocationCallbacks,
p_video_session: *mut vk::VideoSessionKHR,
) -> std::result::Result<vk::Result, vk::CallError> {
unsafe {
self
.raw()
.create_video_session_khr(p_create_info, p_allocator, p_video_session)
}
}
#[inline]
pub(crate) unsafe fn destroy_video_session_khr(
&self,
video_session: vk::VideoSessionKHR,
p_allocator: *const vk::AllocationCallbacks,
) -> std::result::Result<(), vk::CallError> {
unsafe {
self
.raw()
.destroy_video_session_khr(video_session, p_allocator)
}
}
#[inline]
pub(crate) unsafe fn create_video_session_parameters_khr(
&self,
p_create_info: *const vk::VideoSessionParametersCreateInfoKHR<'_>,
p_allocator: *const vk::AllocationCallbacks,
p_video_session_parameters: *mut vk::VideoSessionParametersKHR,
) -> std::result::Result<vk::Result, vk::CallError> {
unsafe {
self.raw().create_video_session_parameters_khr(
p_create_info,
p_allocator,
p_video_session_parameters,
)
}
}
#[inline]
pub(crate) unsafe fn destroy_video_session_parameters_khr(
&self,
video_session_parameters: vk::VideoSessionParametersKHR,
p_allocator: *const vk::AllocationCallbacks,
) -> std::result::Result<(), vk::CallError> {
unsafe {
self
.raw()
.destroy_video_session_parameters_khr(video_session_parameters, p_allocator)
}
}
#[inline]
pub(crate) unsafe fn bind_video_session_memory_khr(
&self,
video_session: vk::VideoSessionKHR,
bind_session_memory_info_count: u32,
p_bind_session_memory_infos: *const vk::BindVideoSessionMemoryInfoKHR<'_>,
) -> std::result::Result<vk::Result, vk::CallError> {
unsafe {
self.raw().bind_video_session_memory_khr(
video_session,
bind_session_memory_info_count,
p_bind_session_memory_infos,
)
}
}
}
pub use info::{DeviceKind, DeviceSummary};