mod frame;
mod gles;
mod vulkan;
pub use frame::{DRM_FORMAT_MOD_INVALID, DmaBufFormat, DmaBufFrame, DmaBufLease, DmaBufPlane};
use gles::GlesInterop;
use vulkan::ImportedVulkanImage;
#[derive(Debug)]
enum Backend {
Vulkan,
Gles(Box<GlesInterop>),
}
fn create_backend(backend: wgpu::Backend, device: &wgpu::Device) -> Backend {
match backend {
wgpu::Backend::Vulkan => Backend::Vulkan,
wgpu::Backend::Gl => Backend::Gles(Box::new(GlesInterop::new(device))),
backend => {
panic!("DMA-BUF import requires a Vulkan or EGL/GLES wgpu device, received {backend:?}")
}
}
}
#[derive(Debug)]
pub struct DmaBufImportGuard {
#[expect(
dead_code,
reason = "the import is held only so that dropping the guard destroys it, which dead-code analysis does not count as a read"
)]
imported: Option<ImportedVulkanImage>,
}
#[derive(Debug)]
pub struct DmaBufImport {
pub command_buffers: Vec<wgpu::CommandBuffer>,
pub encoder: wgpu::CommandEncoder,
pub guard: DmaBufImportGuard,
}
#[derive(Debug)]
pub struct DmaBufImporter {
device: wgpu::Device,
queue: wgpu::Queue,
backend: Backend,
}
impl DmaBufImporter {
#[must_use]
pub fn new(device: &wgpu::Device, queue: &wgpu::Queue, adapter: &wgpu::Adapter) -> Self {
Self {
device: device.clone(),
queue: queue.clone(),
backend: create_backend(adapter.get_info().backend, device),
}
}
#[must_use]
pub fn copy_to_texture(&self, mut frame: DmaBufFrame) -> wgpu::Texture {
assert!(
frame.is_render_ready(),
"a DMA-BUF must be ready before a synchronous GPU copy"
);
let (visible_width, visible_height) = frame.visible_size();
let size = wgpu::Extent3d {
width: visible_width,
height: visible_height,
depth_or_array_layers: 1,
};
let destination = self.device.create_texture(&wgpu::TextureDescriptor {
label: Some("wgpu_external_frame_owned_dma_buf"),
size,
mip_level_count: 1,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format: frame.format.texture_format(),
usage: wgpu::TextureUsages::COPY_DST | wgpu::TextureUsages::TEXTURE_BINDING,
view_formats: &[],
});
let import = self.copy_into(&mut frame, &destination);
let submission = self.queue.submit(
import
.command_buffers
.into_iter()
.chain([import.encoder.finish()]),
);
match &self.backend {
Backend::Vulkan => {
self.device
.poll(wgpu::PollType::Wait {
submission_index: Some(submission),
timeout: None,
})
.expect("the Vulkan DMA-BUF copy failed");
}
Backend::Gles(gles) => {
gles.finish();
}
}
frame.presented();
drop(import.guard);
frame.release(None);
destination
}
#[must_use]
pub fn copy_into(&self, frame: &mut DmaBufFrame, destination: &wgpu::Texture) -> DmaBufImport {
let (visible_width, visible_height) = frame.visible_size();
let size = wgpu::Extent3d {
width: visible_width,
height: visible_height,
depth_or_array_layers: 1,
};
let mut command_buffers = Vec::with_capacity(3);
let imported = match &self.backend {
Backend::Vulkan => {
let imported = vulkan::import_dma_buf(&self.device, frame);
let mut acquire =
self.device
.create_command_encoder(&wgpu::CommandEncoderDescriptor {
label: Some("wgpu_external_frame_dma_buf_acquire"),
});
imported.record_acquire(&mut acquire);
let mut copy =
self.device
.create_command_encoder(&wgpu::CommandEncoderDescriptor {
label: Some("wgpu_external_frame_dma_buf_copy"),
});
copy.copy_texture_to_texture(
wgpu::TexelCopyTextureInfo {
texture: imported.texture(),
mip_level: 0,
origin: wgpu::Origin3d::ZERO,
aspect: wgpu::TextureAspect::All,
},
wgpu::TexelCopyTextureInfo {
texture: destination,
mip_level: 0,
origin: wgpu::Origin3d::ZERO,
aspect: wgpu::TextureAspect::All,
},
size,
);
let mut release =
self.device
.create_command_encoder(&wgpu::CommandEncoderDescriptor {
label: Some("wgpu_external_frame_dma_buf_release"),
});
imported.record_release(&mut release);
command_buffers.extend([acquire.finish(), copy.finish(), release.finish()]);
Some(imported)
}
Backend::Gles(gles) => {
let texel_size = destination
.format()
.block_copy_size(None)
.expect("the DMA-BUF texture format must be block-copyable");
let source = self.device.create_buffer(&wgpu::BufferDescriptor {
label: Some("wgpu_external_frame_dma_buf_init_marker"),
size: u64::from(texel_size),
usage: wgpu::BufferUsages::COPY_SRC,
mapped_at_creation: false,
});
let mut marker =
self.device
.create_command_encoder(&wgpu::CommandEncoderDescriptor {
label: Some("wgpu_external_frame_dma_buf_init"),
});
marker.copy_buffer_to_texture(
wgpu::TexelCopyBufferInfo {
buffer: &source,
layout: wgpu::TexelCopyBufferLayout {
offset: 0,
bytes_per_row: None,
rows_per_image: None,
},
},
wgpu::TexelCopyTextureInfo {
texture: destination,
mip_level: 0,
origin: wgpu::Origin3d::ZERO,
aspect: wgpu::TextureAspect::All,
},
wgpu::Extent3d {
width: 1,
height: 1,
depth_or_array_layers: 1,
},
);
self.queue.submit([marker.finish()]);
gles.copy_dma_buf(frame, destination);
None
}
};
DmaBufImport {
command_buffers,
encoder: self
.device
.create_command_encoder(&wgpu::CommandEncoderDescriptor {
label: Some("wgpu_external_frame_dma_buf_follow_up"),
}),
guard: DmaBufImportGuard { imported },
}
}
}