use ::wgpu;
#[cfg(all(feature = "gpu-interop", target_os = "windows"))]
pub(crate) mod dx12;
#[cfg(all(feature = "gpu-interop", any(target_os = "macos", target_os = "ios")))]
pub(crate) mod metal_iosurface;
#[cfg(all(feature = "gpu-interop", any(target_os = "linux", target_os = "android")))]
pub(crate) mod vk_dmabuf;
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum ExportedHandle {
Win32Dx12NtHandle {
nt_handle: u64,
dxgi_format: u32,
adapter_luid: [u8; 8],
},
LinuxDmaBuf {
fd: i32,
stride: u32,
offset: u32,
drm_format: u32,
modifier: u64,
},
AppleIoSurface {
iosurface_ref: u64,
iosurface_id: u32,
},
AndroidHardwareBuffer {
ahb_ptr: u64,
},
InternalOnly,
}
impl ExportedHandle {
#[inline(always)]
pub const fn is_external_shared(&self) -> bool {
!matches!(self, Self::InternalOnly)
}
#[inline(always)]
pub const fn raw_u64(&self) -> u64 {
match *self {
Self::Win32Dx12NtHandle { nt_handle, .. } => nt_handle,
Self::LinuxDmaBuf { fd, .. } => fd as u64,
Self::AppleIoSurface { iosurface_ref, .. } => iosurface_ref,
Self::AndroidHardwareBuffer { ahb_ptr } => ahb_ptr,
Self::InternalOnly => 0,
}
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, Default)]
pub enum ExternalSyncHandle {
Win32Dx12FenceNtHandle(u64),
LinuxSemaphoreFd(i32),
AppleMetalSharedEvent(u64),
AndroidSyncFd(i32),
#[default]
None,
}
impl ExternalSyncHandle {
#[inline(always)]
pub const fn raw_u64(&self) -> u64 {
match *self {
Self::Win32Dx12FenceNtHandle(h) => h,
Self::LinuxSemaphoreFd(fd) => fd as u64,
Self::AppleMetalSharedEvent(p) => p,
Self::AndroidSyncFd(fd) => fd as u64,
Self::None => 0,
}
}
}
#[allow(dead_code)]
pub(crate) enum PlatformTextureGuard {
None,
#[cfg(all(feature = "gpu-interop", target_os = "windows"))]
Dx12(dx12::Dx12SharedTextureGuard),
#[cfg(all(feature = "gpu-interop", any(target_os = "linux", target_os = "android")))]
VkDmaBuf(vk_dmabuf::VkSharedTextureGuard),
#[cfg(all(feature = "gpu-interop", any(target_os = "macos", target_os = "ios")))]
MetalIoSurface(metal_iosurface::MetalIoSurfaceGuard),
}
pub(crate) struct ExportedTextureAlloc {
pub(crate) texture: wgpu::Texture,
pub(crate) view: wgpu::TextureView,
pub(crate) handle: ExportedHandle,
pub(crate) _guard: PlatformTextureGuard,
}
pub(crate) enum PlatformSharedFence {
Fallback(core::sync::atomic::AtomicU64),
#[cfg(all(feature = "gpu-interop", target_os = "windows"))]
Dx12(dx12::Dx12SharedFence),
#[cfg(all(feature = "gpu-interop", any(target_os = "linux", target_os = "android")))]
Vk(vk_dmabuf::VkSharedFence),
#[cfg(all(feature = "gpu-interop", any(target_os = "macos", target_os = "ios")))]
Metal(metal_iosurface::MetalSharedFence),
}
impl PlatformSharedFence {
pub(crate) fn export_handle(&self) -> ExternalSyncHandle {
match self {
Self::Fallback(_) => ExternalSyncHandle::None,
#[cfg(all(feature = "gpu-interop", target_os = "windows"))]
Self::Dx12(f) => f.export_handle(),
#[cfg(all(feature = "gpu-interop", any(target_os = "linux", target_os = "android")))]
Self::Vk(f) => f.export_handle(),
#[cfg(all(feature = "gpu-interop", any(target_os = "macos", target_os = "ios")))]
Self::Metal(f) => f.export_handle(),
}
}
pub(crate) fn signal_on_queue(&self, device: &wgpu::Device, value: u64) {
match self {
Self::Fallback(v) => {
let _ = device.poll(wgpu::PollType::Poll);
v.store(value, core::sync::atomic::Ordering::Release);
}
#[cfg(all(feature = "gpu-interop", target_os = "windows"))]
Self::Dx12(f) => f.signal_on_device(device, value),
#[cfg(all(feature = "gpu-interop", any(target_os = "linux", target_os = "android")))]
Self::Vk(f) => f.signal_on_device(device, value),
#[cfg(all(feature = "gpu-interop", any(target_os = "macos", target_os = "ios")))]
Self::Metal(f) => f.signal_on_device(device, value),
}
}
pub(crate) fn completed_value(&self) -> u64 {
match self {
Self::Fallback(v) => v.load(core::sync::atomic::Ordering::Acquire),
#[cfg(all(feature = "gpu-interop", target_os = "windows"))]
Self::Dx12(f) => f.completed_value(),
#[cfg(all(feature = "gpu-interop", any(target_os = "linux", target_os = "android")))]
Self::Vk(f) => f.completed_value(),
#[cfg(all(feature = "gpu-interop", any(target_os = "macos", target_os = "ios")))]
Self::Metal(f) => f.completed_value(),
}
}
#[allow(unused_variables)]
pub(crate) fn wait_cpu(&self, device: &wgpu::Device, value: u64, timeout_ms: u32) -> bool {
match self {
Self::Fallback(v) => {
let _ = device.poll(wgpu::PollType::wait_indefinitely());
v.load(core::sync::atomic::Ordering::Acquire) >= value
}
#[cfg(all(feature = "gpu-interop", target_os = "windows"))]
Self::Dx12(f) => f.wait_cpu(value, timeout_ms),
#[cfg(all(feature = "gpu-interop", any(target_os = "linux", target_os = "android")))]
Self::Vk(f) => f.wait_cpu(device, value, timeout_ms),
#[cfg(all(feature = "gpu-interop", any(target_os = "macos", target_os = "ios")))]
Self::Metal(f) => f.wait_cpu(device, value, timeout_ms),
}
}
}
#[cfg(feature = "gpu-interop")]
pub(crate) fn init_aligned_wgpu_context(
adapter_luid: Option<[u8; 8]>,
device_uuid: Option<[u8; 16]>,
) -> Option<super::PreparedWgpuContext> {
#[cfg(target_os = "windows")]
{
let _ = device_uuid;
if let Some(ctx) = dx12::init_aligned_dx12_context(adapter_luid) {
return Some(ctx);
}
}
#[cfg(any(target_os = "linux", target_os = "android"))]
{
let _ = adapter_luid;
if let Some(ctx) = vk_dmabuf::init_aligned_vk_context(device_uuid) {
return Some(ctx);
}
}
#[cfg(any(target_os = "macos", target_os = "ios"))]
{
let _ = (adapter_luid, device_uuid);
if let Some(ctx) = metal_iosurface::init_aligned_metal_context() {
return Some(ctx);
}
}
let _ = (adapter_luid, device_uuid);
None
}
pub(crate) fn allocate_shared_texture(
device: &wgpu::Device,
width: u32,
height: u32,
format: wgpu::TextureFormat,
adapter_luid: [u8; 8],
) -> ExportedTextureAlloc {
let w = width.max(1);
let h = height.max(1);
#[cfg(all(feature = "gpu-interop", target_os = "windows"))]
{
if let Some(alloc) = dx12::allocate_dx12_shared_texture(device, w, h, format, adapter_luid) {
return alloc;
}
}
#[cfg(all(feature = "gpu-interop", any(target_os = "linux", target_os = "android")))]
{
let _ = adapter_luid;
if let Some(alloc) = vk_dmabuf::allocate_vk_shared_texture(device, w, h, format) {
return alloc;
}
}
#[cfg(all(feature = "gpu-interop", any(target_os = "macos", target_os = "ios")))]
{
let _ = adapter_luid;
if let Some(alloc) = metal_iosurface::allocate_metal_shared_texture(device, w, h, format) {
return alloc;
}
}
let _ = adapter_luid;
let texture = device.create_texture(&wgpu::TextureDescriptor {
label: Some("velox_shared_slot_fallback"),
size: wgpu::Extent3d {
width: w,
height: h,
depth_or_array_layers: 1,
},
mip_level_count: 1,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format,
usage: wgpu::TextureUsages::RENDER_ATTACHMENT
| wgpu::TextureUsages::TEXTURE_BINDING
| wgpu::TextureUsages::COPY_SRC
| wgpu::TextureUsages::COPY_DST,
view_formats: &[],
});
let view = texture.create_view(&wgpu::TextureViewDescriptor::default());
ExportedTextureAlloc {
texture,
view,
handle: ExportedHandle::InternalOnly,
_guard: PlatformTextureGuard::None,
}
}
pub(crate) fn create_shared_fence(device: &wgpu::Device) -> PlatformSharedFence {
#[cfg(all(feature = "gpu-interop", target_os = "windows"))]
{
if let Some(f) = dx12::Dx12SharedFence::new(device) {
return PlatformSharedFence::Dx12(f);
}
}
#[cfg(all(feature = "gpu-interop", any(target_os = "linux", target_os = "android")))]
{
if let Some(f) = vk_dmabuf::VkSharedFence::new(device) {
return PlatformSharedFence::Vk(f);
}
}
#[cfg(all(feature = "gpu-interop", any(target_os = "macos", target_os = "ios")))]
{
if let Some(f) = metal_iosurface::MetalSharedFence::new(device) {
return PlatformSharedFence::Metal(f);
}
}
let _ = device;
PlatformSharedFence::Fallback(core::sync::atomic::AtomicU64::new(0))
}
#[cfg(all(feature = "gpu-interop", target_os = "windows"))]
pub fn verify_dx12_exported_handles(
device: &wgpu::Device,
tex_nt_handle: u64,
fence_nt_handle: u64,
) -> Option<(u64, u32, u64)> {
dx12::verify_dx12_exported_handles(device, tex_nt_handle, fence_nt_handle)
}