use std::{
sync::{
Arc,
atomic::{AtomicBool, Ordering},
},
time::{Duration, Instant},
};
use parking_lot::Mutex;
use windows::Win32::{
Foundation::HANDLE,
Graphics::{
Direct3D11::ID3D11Fence,
Direct3D12::{ID3D12Fence, ID3D12Resource},
Dxgi::{
Common::{
DXGI_FORMAT, DXGI_FORMAT_B8G8R8A8_UNORM, DXGI_FORMAT_NV12, DXGI_FORMAT_R8_UNORM, DXGI_FORMAT_R8G8_UNORM,
DXGI_FORMAT_R8G8B8A8_UNORM,
},
CreateDXGIFactory1, IDXGIFactory1,
},
},
};
use super::card_copies::{Copied, Readers};
use crate::images::{CardFrame, CardFrames, CardFramesWay, CardLease, CardSurface};
#[derive(Clone, Default)]
pub(super) struct Leases {
kept: Arc<Mutex<Vec<CardLease>>>,
}
impl Leases {
fn keep(&self, lease: Option<CardLease>) {
if let Some(lease) = lease {
self.kept.lock().push(lease);
}
}
pub(super) fn let_go_after(&self, queue: &wgpu::Queue) {
let kept = std::mem::take(&mut *self.kept.lock());
if !kept.is_empty() {
queue.on_submitted_work_done(move || drop(kept));
}
}
}
pub(super) struct Slot {
pub bind_group: wgpu::BindGroup,
pub first: wgpu::TextureView,
pub second: Option<wgpu::TextureView>,
pub(super) size: (u32, u32),
pub(super) textures: Vec<wgpu::Texture>,
}
impl Slot {
#[cfg(test)]
pub(super) fn textures(&self) -> &[wgpu::Texture] {
&self.textures
}
}
pub(super) fn announce(device: &wgpu::Device) -> bool {
if !device.features().contains(wgpu::Features::TEXTURE_FORMAT_NV12) {
return false;
}
let Some(hal) = (unsafe { device.as_hal::<wgpu::hal::api::Dx12>() }) else {
return false;
};
let card = unsafe { hal.raw_device().GetAdapterLuid() };
crate::images::set_card_frames_adapter(Some((card.LowPart, card.HighPart)));
crate::images::set_card_frames_nv12(true);
true
}
const AMD: u32 = 0x1002;
const LOST_FOR_NV12_WITHIN: Duration = Duration::from_secs(5);
static NV12_GIVEN: Mutex<Option<Instant>> = Mutex::new(None);
static NV12_LOST: AtomicBool = AtomicBool::new(false);
fn vulkan_takes_nv12(device: &wgpu::Device) -> bool {
device.features().contains(wgpu::Features::TEXTURE_FORMAT_NV12)
&& nv12_is_tried_on(device.adapter_info().vendor)
&& !NV12_LOST.load(Ordering::Acquire)
}
pub(super) fn nv12_is_tried_on(vendor: u32) -> bool {
vendor == AMD
}
pub(super) fn a_device_is_lost() {
if NV12_GIVEN
.lock()
.is_some_and(|given| given.elapsed() < LOST_FOR_NV12_WITHIN)
{
NV12_LOST.store(true, Ordering::Release);
crate::images::set_card_frames_nv12(false);
}
}
#[cfg(test)]
pub(super) fn nv12_was_given(at: Option<Instant>) {
*NV12_GIVEN.lock() = at;
}
#[cfg(test)]
pub(super) fn nv12_is_lost() -> bool {
NV12_LOST.load(Ordering::Acquire)
}
#[cfg(test)]
pub(super) fn forget_the_lost_nv12() {
NV12_LOST.store(false, Ordering::Release);
*NV12_GIVEN.lock() = None;
}
pub(super) fn announce_vulkan(device: &wgpu::Device, adapter: &wgpu::Adapter) -> bool {
if !device.features().contains(wgpu::Features::VULKAN_EXTERNAL_MEMORY_WIN32) {
return false;
}
let info = adapter.get_info();
if info.backend != wgpu::Backend::Vulkan {
return false;
}
let card = unsafe {
let Ok(cards) = CreateDXGIFactory1::<IDXGIFactory1>() else {
return false;
};
(0..)
.map_while(|index| cards.EnumAdapters1(index).ok())
.filter_map(|card| card.GetDesc1().ok())
.find(|said| said.VendorId == info.vendor && said.DeviceId == info.device)
.map(|said| said.AdapterLuid)
};
let Some(card) = card else {
return false;
};
crate::images::set_card_frames_adapter(Some((card.LowPart, card.HighPart)));
crate::images::set_card_frames_nv12(vulkan_takes_nv12(device));
true
}
fn format_of(said: DXGI_FORMAT) -> Option<wgpu::TextureFormat> {
Some(match said {
DXGI_FORMAT_NV12 => wgpu::TextureFormat::NV12,
DXGI_FORMAT_R8_UNORM => wgpu::TextureFormat::R8Unorm,
DXGI_FORMAT_R8G8_UNORM => wgpu::TextureFormat::Rg8Unorm,
DXGI_FORMAT_R8G8B8A8_UNORM => wgpu::TextureFormat::Rgba8Unorm,
DXGI_FORMAT_B8G8R8A8_UNORM => wgpu::TextureFormat::Bgra8Unorm,
_ => return None,
})
}
fn described(format: wgpu::TextureFormat, size: wgpu::Extent3d) -> wgpu::TextureDescriptor<'static> {
wgpu::TextureDescriptor {
label: Some("lurq_card_frame"),
size,
mip_level_count: 1,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format,
usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_SRC,
view_formats: &[],
}
}
fn open_in_vulkan(
device: &wgpu::Device,
readers: &mut Readers,
handle: isize,
formats: &[wgpu::TextureFormat],
) -> Option<(wgpu::Texture, (u32, u32))> {
if !device.features().contains(wgpu::Features::VULKAN_EXTERNAL_MEMORY_WIN32) {
return None;
}
let hal = unsafe { device.as_hal::<wgpu::hal::api::Vulkan>() }?;
let (said, width, height) = readers.said_of(handle)?;
let format = format_of(said)?;
let nv12 = format == wgpu::TextureFormat::NV12;
if !formats.contains(&format) || (nv12 && !vulkan_takes_nv12(device)) {
return None;
}
if nv12 {
*NV12_GIVEN.lock() = Some(Instant::now());
}
let size = wgpu::Extent3d {
width,
height,
depth_or_array_layers: 1,
};
let texture = unsafe {
hal.texture_from_d3d11_shared_handle(
HANDLE(handle as _),
&wgpu::hal::TextureDescriptor {
label: Some("lurq_card_frame"),
size,
mip_level_count: 1,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format,
usage: wgpu::TextureUses::RESOURCE | wgpu::TextureUses::COPY_SRC,
memory_flags: wgpu::hal::MemoryFlags::empty(),
view_formats: Vec::new(),
},
)
}
.ok()?;
drop(hal);
let texture = unsafe { device.create_texture_from_hal::<wgpu::hal::api::Vulkan>(texture, &described(format, size)) };
Some((texture, (width, height)))
}
fn opened(
device: &wgpu::Device,
readers: &mut Readers,
handle: isize,
formats: &[wgpu::TextureFormat],
) -> Option<(wgpu::Texture, (u32, u32))> {
open(device, handle, formats).or_else(|| open_in_vulkan(device, readers, handle, formats))
}
enum Fence {
Own(ID3D12Fence),
Read(ID3D11Fence),
}
impl Fence {
fn reached(&self) -> u64 {
match self {
Self::Own(fence) => unsafe { fence.GetCompletedValue() },
Self::Read(fence) => unsafe { fence.GetCompletedValue() },
}
}
}
pub(super) fn of(image: &crate::images::ImageCmd) -> Option<&CardFrames> {
image.native.as_ref()?.payload::<CardFrames>()
}
pub(super) fn open(
device: &wgpu::Device,
handle: isize,
formats: &[wgpu::TextureFormat],
) -> Option<(wgpu::Texture, (u32, u32))> {
let hal = unsafe { device.as_hal::<wgpu::hal::api::Dx12>() }?;
let mut resource: Option<ID3D12Resource> = None;
unsafe { hal.raw_device().OpenSharedHandle(HANDLE(handle as _), &mut resource) }.ok()?;
let resource = resource?;
let said = unsafe { resource.GetDesc() };
let format = format_of(said.Format)?;
if !formats.contains(&format) {
return None;
}
let (width, height) = (u32::try_from(said.Width).ok()?, said.Height);
let size = wgpu::Extent3d {
width,
height,
depth_or_array_layers: 1,
};
let texture =
unsafe { wgpu::hal::dx12::Device::texture_from_raw(resource, format, wgpu::TextureDimension::D2, size, 1, 1) };
drop(hal);
let texture = unsafe { device.create_texture_from_hal::<wgpu::hal::api::Dx12>(texture, &described(format, size)) };
Some((texture, (width, height)))
}
pub(super) fn planes(texture: &wgpu::Texture) -> (wgpu::TextureView, wgpu::TextureView) {
let plane = |aspect, format| {
texture.create_view(&wgpu::TextureViewDescriptor {
aspect,
format: Some(format),
..Default::default()
})
};
(
plane(wgpu::TextureAspect::Plane0, wgpu::TextureFormat::R8Unorm),
plane(wgpu::TextureAspect::Plane1, wgpu::TextureFormat::Rg8Unorm),
)
}
fn fence(device: &wgpu::Device, handle: isize) -> Option<ID3D12Fence> {
let hal = unsafe { device.as_hal::<wgpu::hal::api::Dx12>() }?;
let mut fence: Option<ID3D12Fence> = None;
unsafe { hal.raw_device().OpenSharedHandle(HANDLE(handle as _), &mut fence) }.ok()?;
fence
}
enum Way {
Card { slots: Vec<Slot>, fence: Option<Fence> },
Memory(Copied),
Nowhere,
}
fn on_the_card(
device: &wgpu::Device,
frames: &CardFrames,
readers: &mut Readers,
bind: &impl Fn(&wgpu::TextureView, Option<&wgpu::TextureView>) -> wgpu::BindGroup,
) -> Option<Way> {
let slots = frames
.surfaces()
.iter()
.map(|surface| {
let (first, second, size, textures) = match *surface {
CardSurface::Nv12(handle) => {
let (texture, size) = opened(device, readers, handle, &[wgpu::TextureFormat::NV12])?;
let (gray, color) = planes(&texture);
(gray, Some(color), size, vec![texture])
}
CardSurface::Planes { gray, color } => {
let (gray, size) = opened(device, readers, gray, &[wgpu::TextureFormat::R8Unorm])?;
let (color, _) = opened(device, readers, color, &[wgpu::TextureFormat::Rg8Unorm])?;
(
gray.create_view(&Default::default()),
Some(color.create_view(&Default::default())),
size,
vec![gray, color],
)
}
CardSurface::Rgba(handle) => {
let (texture, size) = opened(
device,
readers,
handle,
&[wgpu::TextureFormat::Rgba8Unorm, wgpu::TextureFormat::Bgra8Unorm],
)?;
(texture.create_view(&Default::default()), None, size, vec![texture])
}
};
Some(Slot {
bind_group: bind(&first, second.as_ref()),
first,
second,
size,
textures,
})
})
.collect::<Option<Vec<_>>>()?;
let fence = match frames.fence() {
Some(handle) => Some(match fence(device, handle) {
Some(own) => Fence::Own(own),
None => Fence::Read(readers.fence_of(frames.surfaces().first()?.handles().0, handle)?),
}),
None => None,
};
Some(Way::Card { slots, fence })
}
pub(super) struct Opened {
way: Way,
frames: CardFrames,
drawn: Option<(u64, CardFrame)>,
leases: Leases,
width: u32,
height: u32,
}
impl Drop for Opened {
fn drop(&mut self) {
if let Some((_, frame)) = self.drawn.take() {
self.leases.keep(frame.lease);
}
}
}
impl Opened {
pub(super) fn open(
device: &wgpu::Device,
frames: &CardFrames,
picture: (u32, u32),
leases: &Leases,
readers: &mut Readers,
bind: impl Fn(&wgpu::TextureView, Option<&wgpu::TextureView>) -> wgpu::BindGroup,
) -> Self {
let way = on_the_card(device, frames, readers, &bind)
.or_else(|| Copied::open(device, frames, readers, &bind).map(Way::Memory))
.unwrap_or(Way::Nowhere);
Self::by(way, frames, picture, leases)
}
#[cfg(test)]
pub(super) fn through_memory(
device: &wgpu::Device,
frames: &CardFrames,
picture: (u32, u32),
leases: &Leases,
readers: &mut Readers,
bind: impl Fn(&wgpu::TextureView, Option<&wgpu::TextureView>) -> wgpu::BindGroup,
) -> Self {
let way = Copied::open(device, frames, readers, &bind).map_or(Way::Nowhere, Way::Memory);
Self::by(way, frames, picture, leases)
}
fn by(way: Way, frames: &CardFrames, (width, height): (u32, u32), leases: &Leases) -> Self {
frames.found(match way {
Way::Card { .. } => CardFramesWay::Card,
Way::Memory(_) => CardFramesWay::Memory,
Way::Nowhere => CardFramesWay::Nowhere,
});
Self {
way,
frames: frames.clone(),
drawn: None,
leases: leases.clone(),
width,
height,
}
}
pub(super) fn is_of(&self, frames: &CardFrames, (width, height): (u32, u32)) -> bool {
self.frames.same(frames) && (self.width, self.height) == (width, height)
}
pub(super) fn is_rgba(&self) -> bool {
self.frames.is_rgba()
}
pub(super) fn pick(&mut self, queue: &wgpu::Queue) {
if let Way::Memory(copied) = &mut self.way {
if let Some((count, mut frame)) = copied.arrived(queue) {
drop(frame.lease.take());
self.drawn = Some((count, frame));
}
if copied.is_copying() {
return;
}
}
let Some((count, frame)) = self.frames.latest() else {
return;
};
if self.drawn.as_ref().is_some_and(|(drawn, _)| *drawn == count) || frame.surface >= self.frames.surfaces().len() {
return;
}
let reached = match &self.way {
Way::Card { fence, .. } => fence.as_ref().map(Fence::reached),
Way::Memory(copied) => copied.reached(),
Way::Nowhere => return,
};
let written = match (frame.ready, reached) {
(Some(value), Some(reached)) => reached >= value,
_ => true,
};
if !written {
return;
}
match &mut self.way {
Way::Card { .. } => {
if let Some((_, before)) = self.drawn.replace((count, frame)) {
self.leases.keep(before.lease);
}
}
Way::Memory(copied) => copied.copy(count, frame),
Way::Nowhere => {}
}
}
pub(super) fn drawn(&self) -> Option<(usize, &Slot)> {
let (_, frame) = self.drawn.as_ref()?;
match &self.way {
Way::Card { slots, .. } => Some((frame.surface, slots.get(frame.surface)?)),
Way::Memory(copied) => Some((0, copied.slot())),
Way::Nowhere => None,
}
}
pub(super) fn colors(&self) -> crate::images::YuvColors {
self
.drawn
.as_ref()
.map_or(crate::images::YuvColors::BT709, |(_, frame)| frame.colors)
}
pub(super) fn part(&self, min: [f32; 2], max: [f32; 2]) -> ([f32; 2], [f32; 2]) {
let Some(((_, frame), (_, slot))) = self.drawn.as_ref().zip(self.drawn()) else {
return (min, max);
};
crate::images::part_of(
frame.crop,
(self.width, self.height),
slot.size,
self.is_rgba(),
min,
max,
)
}
}