🆕 Decode VA-API's pictures into rt's exported blocks
On the rt platform the VA-API arm now answers the guest: its decoders land pictures in rt's exported blocks, which rt makes as dma-bufs (new_dma_buf_buffer_of_length_padding_unobserved, export_dma_buf over new_exported_block(DmaBuf), linear, the named byte on a multiple of 256). rt's contract after an acquire is that the external side does not touch the block until its next whole overwrite, and VA reads a reference from the surface it was decoded into. So each codec's decoder now tells its accelerator whether a later picture may predict from the picture it asks storage for (PictureRole: H.264's nal_ref_idc, HEVC's sub-layer non-reference picture of the highest sub-layer), and where the platform's rules keep references apart (CachedReferences::Apart, rt's) a reference is decoded into a surface of the decoder's own and lands in its cache by video processing, while a picture no later one predicts from is decoded straight into its cache. Each session counts its pictures by role and the references it kept apart, and logs the counts at its end. The rt arm answers no encoder: rt releases no encoder's sources to the driver yet. The Vulkan platform's rules keep every picture in its cache and its encoders, as before.
This commit is contained in:
1 parent
4758a3ee22
commit
f716a52d2a
23 files changed
+516
-64
No files matched your search
@@ -51,6 +51,7 @@
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use reims_vgpu_video::codec::PictureRole;
|
||||
use reims_vgpu_video::codec::h264::nal::{AnnexB, NalUnit, access_units, nal_type};
|
||||
use reims_vgpu_video::codec::h264::pps::Pps;
|
||||
use reims_vgpu_video::codec::h264::sps::Sps;
|
||||
@@ -770,7 +771,7 @@ impl Accelerator for Recorder {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn new_picture(&mut self) -> Result<usize, h264::Error> {
|
||||
fn new_picture(&mut self, _role: PictureRole) -> Result<usize, h264::Error> {
|
||||
self.next += 1;
|
||||
Ok(self.next)
|
||||
}
|
||||
@@ -811,7 +812,7 @@ impl Accelerator for Recorder {
|
||||
|
||||
fn conceal(&mut self, from: &usize) -> Result<Option<usize>, h264::Error> {
|
||||
self.concealed.borrow_mut().push(*from);
|
||||
self.new_picture().map(Some)
|
||||
self.new_picture(PictureRole::Reference).map(Some)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -84,6 +84,7 @@
|
||||
use std::cell::{Cell, RefCell};
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
use reims_vgpu_video::codec::PictureRole;
|
||||
use reims_vgpu_video::codec::h264::nal::AnnexB;
|
||||
use reims_vgpu_video::codec::hevc::nal::{self, NalUnit, is_idr, nal_type};
|
||||
use reims_vgpu_video::codec::hevc::pps::Pps;
|
||||
@@ -975,7 +976,7 @@ impl Accelerator for Recorder {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn new_picture(&mut self) -> Result<usize, hevc::Error> {
|
||||
fn new_picture(&mut self, _role: PictureRole) -> Result<usize, hevc::Error> {
|
||||
self.next += 1;
|
||||
Ok(self.next)
|
||||
}
|
||||
|
||||
@@ -58,6 +58,18 @@ pub mod h264;
|
||||
pub mod hevc;
|
||||
mod num;
|
||||
|
||||
/// Whether a later picture of its stream may predict from a picture, as its first slice says:
|
||||
/// what each codec's decoder tells its accelerator as it asks for the picture's storage
|
||||
/// (`h264::Accelerator::new_picture`, `hevc::Accelerator::new_picture`).
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum PictureRole {
|
||||
/// A later picture may predict from it.
|
||||
Reference,
|
||||
/// No later picture predicts from it: H.264's `nal_ref_idc` 0; HEVC's sub-layer
|
||||
/// non-reference picture of the highest sub-layer the SPS has.
|
||||
NonReference,
|
||||
}
|
||||
|
||||
/// The fuzzers' harness: each codec's tests have a `fuzz` module of their
|
||||
/// own.
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -15,6 +15,7 @@ use super::pps::Pps;
|
||||
use super::scaling::ScalingLists;
|
||||
use super::slice::{SliceHeader, SliceType};
|
||||
use super::sps::Sps;
|
||||
use crate::codec::PictureRole;
|
||||
|
||||
/// The order frames come out in.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
@@ -105,8 +106,11 @@ pub trait Accelerator {
|
||||
/// what the frames need.
|
||||
fn configure(&mut self, sps: &Sps, dpb_frames: usize) -> Result<(), Self::Error>;
|
||||
|
||||
/// Storage for a new picture.
|
||||
fn new_picture(&mut self) -> Result<Self::Picture, Self::Error>;
|
||||
/// Storage for a new picture, which a later picture may predict from
|
||||
/// as `role` says: a reference picture where its `nal_ref_idc` is not
|
||||
/// 0 (fields, which alone could pair a reference with a non-reference
|
||||
/// picture, are refused at their SPS).
|
||||
fn new_picture(&mut self, role: PictureRole) -> Result<Self::Picture, Self::Error>;
|
||||
|
||||
/// Decodes one picture from all its slices into `picture`.
|
||||
fn decode_picture(
|
||||
@@ -469,7 +473,12 @@ impl<A: Accelerator> Decoder<A> {
|
||||
lists.push(pair);
|
||||
}
|
||||
|
||||
let picture = self.accelerator.new_picture()?;
|
||||
let role = if first.nal_ref_idc == 0 {
|
||||
PictureRole::NonReference
|
||||
} else {
|
||||
PictureRole::Reference
|
||||
};
|
||||
let picture = self.accelerator.new_picture(role)?;
|
||||
{
|
||||
let entries = &self.dpb.entries;
|
||||
let reference = reference_frame::<A::Picture, Output<A::Picture>>;
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use super::*;
|
||||
use crate::codec::PictureRole;
|
||||
|
||||
/// Writes syntax elements, then an escaped NAL unit.
|
||||
#[derive(Default)]
|
||||
@@ -313,6 +314,8 @@ struct Recorder {
|
||||
conceals: bool,
|
||||
/// Each copy: the picture copied, and the copy.
|
||||
concealed: Vec<(u32, u32)>,
|
||||
/// Each picture's role, as it was asked for.
|
||||
roles: Vec<PictureRole>,
|
||||
}
|
||||
|
||||
impl Accelerator for Recorder {
|
||||
@@ -324,7 +327,8 @@ impl Accelerator for Recorder {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn new_picture(&mut self) -> Result<u32, Error> {
|
||||
fn new_picture(&mut self, role: PictureRole) -> Result<u32, Error> {
|
||||
self.roles.push(role);
|
||||
self.next += 1;
|
||||
Ok(self.next - 1)
|
||||
}
|
||||
@@ -355,7 +359,8 @@ impl Accelerator for Recorder {
|
||||
if !self.conceals {
|
||||
return Ok(None);
|
||||
}
|
||||
let copy = self.new_picture()?;
|
||||
let copy = self.next;
|
||||
self.next += 1;
|
||||
self.concealed.push((*from, copy));
|
||||
Ok(Some(copy))
|
||||
}
|
||||
@@ -444,6 +449,24 @@ fn b_frames_come_out_in_display_order_and_predict_from_both_sides() -> Result<()
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn each_picture_is_asked_for_as_a_reference_or_not_as_its_nal_ref_idc_says() -> Result<(), Error> {
|
||||
let spec = SpsSpec::default();
|
||||
let mut d = decoder(OutputOrder::Display);
|
||||
// I0 P6 B2 (a reference) B4 (none), then P8.
|
||||
run(
|
||||
&mut d,
|
||||
&spec,
|
||||
&[idr(), p(1, 6), b(2, 2, true), b(3, 4, false), p(3, 8)],
|
||||
)?;
|
||||
use PictureRole::{NonReference, Reference};
|
||||
assert_eq!(
|
||||
d.accelerator().roles,
|
||||
[Reference, Reference, Reference, NonReference, Reference]
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn decode_order_emits_each_picture_as_it_is_decoded() -> Result<(), Error> {
|
||||
let spec = SpsSpec::default();
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
use std::sync::atomic::{AtomicU64, Ordering};
|
||||
|
||||
use super::*;
|
||||
use crate::codec::PictureRole;
|
||||
use crate::codec::fuzz_support::{Rng, Target, long, maybe_mutate, mutate_rounds, quick};
|
||||
|
||||
const STREAMS: Target = Target::new("h264-streams");
|
||||
@@ -84,7 +85,7 @@ impl Accelerator for Checking {
|
||||
}
|
||||
}
|
||||
|
||||
fn new_picture(&mut self) -> Result<u32, Error> {
|
||||
fn new_picture(&mut self, _role: PictureRole) -> Result<u32, Error> {
|
||||
if self.fails() {
|
||||
return Err(Error::Unsupported("the stand-in's picture failure"));
|
||||
}
|
||||
@@ -442,7 +443,7 @@ impl Accelerator for RefusingHigh {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn new_picture(&mut self) -> Result<u32, Error> {
|
||||
fn new_picture(&mut self, _role: PictureRole) -> Result<u32, Error> {
|
||||
if self.configured {
|
||||
Ok(0)
|
||||
} else {
|
||||
|
||||
@@ -20,6 +20,7 @@ use super::sps::Sps;
|
||||
use super::{Error, NalFormat, OutputOrder};
|
||||
use crate::codec::h264::ParameterSets;
|
||||
use crate::codec::h264::nal::sample_units;
|
||||
use crate::codec::PictureRole;
|
||||
|
||||
pub use super::dpb::RpsMembership;
|
||||
|
||||
@@ -113,8 +114,10 @@ pub trait Accelerator {
|
||||
/// sequence's SPS changes what the pictures need.
|
||||
fn configure(&mut self, sps: &Sps, dpb_size: usize) -> Result<(), Self::Error>;
|
||||
|
||||
/// Storage for a new picture.
|
||||
fn new_picture(&mut self) -> Result<Self::Picture, Self::Error>;
|
||||
/// Storage for a new picture, which a later picture may predict from
|
||||
/// as `role` says: all but a sub-layer non-reference picture of the
|
||||
/// SPS's highest sub-layer may be a reference.
|
||||
fn new_picture(&mut self, role: PictureRole) -> Result<Self::Picture, Self::Error>;
|
||||
|
||||
/// Decodes one picture from all its slice segments into `picture`.
|
||||
fn decode_picture(
|
||||
@@ -501,7 +504,8 @@ impl<A: Accelerator> Decoder<A> {
|
||||
|
||||
// The pictures the accelerator sees, and each slice's lists
|
||||
// (8.3.4) as indices into them.
|
||||
let picture = self.accelerator.new_picture()?;
|
||||
let role = role_of(unit_type, first.temporal_id, sps.sps_max_sub_layers_minus1);
|
||||
let picture = self.accelerator.new_picture(role)?;
|
||||
{
|
||||
let references: Vec<&Entry<_, _>> = self
|
||||
.dpb
|
||||
@@ -651,6 +655,18 @@ impl<A: Accelerator> Decoder<A> {
|
||||
}
|
||||
}
|
||||
|
||||
/// The role of a picture of NAL unit type `unit_type` and `TemporalId` `temporal_id`, in a stream
|
||||
/// whose SPS has `max_sub_layers_minus1`: no later picture predicts from a sub-layer
|
||||
/// non-reference picture (7.4.2.2) of the highest sub-layer, since only pictures of higher
|
||||
/// sub-layers may predict from one; any other picture may be a reference.
|
||||
pub(crate) fn role_of(unit_type: u8, temporal_id: u8, max_sub_layers_minus1: u8) -> PictureRole {
|
||||
if is_sub_layer_non_reference(unit_type) && temporal_id == max_sub_layers_minus1 {
|
||||
PictureRole::NonReference
|
||||
} else {
|
||||
PictureRole::Reference
|
||||
}
|
||||
}
|
||||
|
||||
/// `PicOrderCntVal` (8.3.1) of a picture with `slice_pic_order_cnt_lsb`
|
||||
/// `lsb`, after `prevTid0Pic`'s `previous`; `None` where the picture
|
||||
/// starts a coded video sequence. In 64 bits nothing overflows: the
|
||||
|
||||
@@ -10,6 +10,7 @@ use super::bits::BitReader;
|
||||
use super::nal::nal_type;
|
||||
use super::slice::SliceHeader;
|
||||
use super::*;
|
||||
use crate::codec::PictureRole;
|
||||
|
||||
/// Writes syntax elements, then raw bytes or an escaped NAL unit.
|
||||
#[derive(Default)]
|
||||
@@ -644,6 +645,8 @@ struct Recorder {
|
||||
configured: Vec<(u32, u32, u8, usize)>,
|
||||
decoded: Vec<Decoded>,
|
||||
next: u32,
|
||||
/// Each picture's role, as it was asked for.
|
||||
roles: Vec<PictureRole>,
|
||||
}
|
||||
|
||||
impl Accelerator for Recorder {
|
||||
@@ -660,7 +663,8 @@ impl Accelerator for Recorder {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn new_picture(&mut self) -> Result<u32, Error> {
|
||||
fn new_picture(&mut self, role: PictureRole) -> Result<u32, Error> {
|
||||
self.roles.push(role);
|
||||
self.next += 1;
|
||||
Ok(self.next)
|
||||
}
|
||||
@@ -766,6 +770,31 @@ fn gop_stream(order: OutputOrder) -> Result<(Decoder<Recorder>, Vec<i32>), Error
|
||||
Ok((decoder, out))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn each_picture_is_asked_for_as_a_reference_or_not_as_its_unit_type_and_sub_layer_say()
|
||||
-> Result<(), Error> {
|
||||
// IDR0, TRAIL_R 4, TRAIL_R 2, then TRAIL_N 1 and 3, in a stream of one sub-layer.
|
||||
let (decoder, _) = gop_stream(OutputOrder::Decode)?;
|
||||
use PictureRole::{NonReference, Reference};
|
||||
assert_eq!(
|
||||
decoder.accelerator().roles,
|
||||
[Reference, Reference, Reference, NonReference, NonReference]
|
||||
);
|
||||
// A sub-layer non-reference picture below the highest sub-layer may be predicted from by
|
||||
// a picture of a higher one; any other kind may be predicted from on its own sub-layer.
|
||||
let role_of = super::decoder::role_of;
|
||||
// TRAIL_N, TSA_N (2), RADL_N and RASL_N.
|
||||
for unit_type in [nal_type::TRAIL_N, 2, nal_type::RADL_N, nal_type::RASL_N] {
|
||||
assert_eq!(role_of(unit_type, 0, 0), NonReference);
|
||||
assert_eq!(role_of(unit_type, 2, 2), NonReference);
|
||||
assert_eq!(role_of(unit_type, 1, 2), Reference);
|
||||
}
|
||||
for unit_type in [nal_type::TRAIL_R, nal_type::CRA_NUT, nal_type::IDR_W_RADL, nal_type::BLA_N_LP] {
|
||||
assert_eq!(role_of(unit_type, 0, 0), Reference);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_hierarchical_gop_builds_its_sets_and_lists() -> Result<(), Error> {
|
||||
let (decoder, out) = gop_stream(OutputOrder::Decode)?;
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
use std::sync::atomic::{AtomicU64, Ordering};
|
||||
|
||||
use super::*;
|
||||
use crate::codec::PictureRole;
|
||||
use crate::codec::fuzz_support::{Rng, Target, long, maybe_mutate, mutate_rounds, quick};
|
||||
|
||||
const STREAMS: Target = Target::new("hevc-streams");
|
||||
@@ -85,7 +86,7 @@ impl Accelerator for Checking {
|
||||
}
|
||||
}
|
||||
|
||||
fn new_picture(&mut self) -> Result<u32, Error> {
|
||||
fn new_picture(&mut self, _role: PictureRole) -> Result<u32, Error> {
|
||||
if self.fails() {
|
||||
return Err(Error::Unsupported("the stand-in's picture failure"));
|
||||
}
|
||||
@@ -527,7 +528,7 @@ impl Accelerator for RefusingTenBits {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn new_picture(&mut self) -> Result<u32, Error> {
|
||||
fn new_picture(&mut self, _role: PictureRole) -> Result<u32, Error> {
|
||||
if self.configured {
|
||||
Ok(0)
|
||||
} else {
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
use std::ffi::c_int;
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::codec::PictureRole;
|
||||
use crate::codec::h264::{
|
||||
Accelerator, PictureParams, ReferenceFrame, SliceParams, slice::SliceType, sps::Sps,
|
||||
};
|
||||
@@ -67,7 +68,7 @@ impl Accelerator for NvdecH264 {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn new_picture(&mut self) -> Result<Picture, Error> {
|
||||
fn new_picture(&mut self, _role: PictureRole) -> Result<Picture, Error> {
|
||||
self.decoder()?.picture()
|
||||
}
|
||||
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
use std::ffi::{c_int, c_ushort};
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::codec::PictureRole;
|
||||
use crate::codec::hevc::nal::{is_idr, is_irap};
|
||||
use crate::codec::hevc::sps::Sps;
|
||||
use crate::codec::hevc::{Accelerator, PictureParams, SliceParams};
|
||||
@@ -71,7 +72,7 @@ impl Accelerator for NvdecHevc {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn new_picture(&mut self) -> Result<Picture, Error> {
|
||||
fn new_picture(&mut self, _role: PictureRole) -> Result<Picture, Error> {
|
||||
self.decoder()?.picture()
|
||||
}
|
||||
|
||||
|
||||
@@ -10,6 +10,7 @@ use super::frame::Frame;
|
||||
use super::surface::{Rect, Surface, SurfaceFormat, SurfacePool, end_context};
|
||||
use super::target::TargetFor;
|
||||
use super::wait::{self, Deadline};
|
||||
use crate::codec::PictureRole;
|
||||
use crate::codec::h264::{
|
||||
self, Accelerator, Decoder, NalFormat, Output, OutputOrder, PictureParams, ReferenceFrame,
|
||||
SliceParams, slice::SliceType, sps::Sps,
|
||||
@@ -287,13 +288,13 @@ impl Accelerator for VaH264 {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// The decode's target where it was handed one of the picture's shape, else a surface of the
|
||||
/// pool.
|
||||
fn new_picture(&mut self) -> Result<Surface, Error> {
|
||||
/// The decode's target where it was handed one of the picture's shape for a picture of
|
||||
/// `role`, else a surface of the pool.
|
||||
fn new_picture(&mut self, role: PictureRole) -> Result<Surface, Error> {
|
||||
let target_for = self.target_for.take();
|
||||
let pool = &self.session()?.pool;
|
||||
let lent = target_for
|
||||
.and_then(|target_for| target_for(pool.shape()))
|
||||
.and_then(|target_for| target_for(pool.shape(), role))
|
||||
.and_then(|target| pool.lend_target(target));
|
||||
match lent {
|
||||
Some(surface) => Ok(surface),
|
||||
|
||||
@@ -10,6 +10,7 @@ use super::frame::Frame;
|
||||
use super::surface::{Rect, Surface, SurfaceFormat, SurfacePool, end_context};
|
||||
use super::target::TargetFor;
|
||||
use super::wait::{self, Deadline};
|
||||
use crate::codec::PictureRole;
|
||||
use crate::codec::hevc::{
|
||||
self, Accelerator, Decoder, NalFormat, Output, OutputOrder, PictureParams, RpsMembership,
|
||||
SliceParams,
|
||||
@@ -285,13 +286,13 @@ impl Accelerator for VaHevc {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// The decode's target where it was handed one of the picture's shape, else a surface of the
|
||||
/// pool.
|
||||
fn new_picture(&mut self) -> Result<Surface, Error> {
|
||||
/// The decode's target where it was handed one of the picture's shape for a picture of
|
||||
/// `role`, else a surface of the pool.
|
||||
fn new_picture(&mut self, role: PictureRole) -> Result<Surface, Error> {
|
||||
let target_for = self.target_for.take();
|
||||
let pool = &self.session()?.pool;
|
||||
let lent = target_for
|
||||
.and_then(|target_for| target_for(pool.shape()))
|
||||
.and_then(|target_for| target_for(pool.shape(), role))
|
||||
.and_then(|target| pool.lend_target(target));
|
||||
match lent {
|
||||
Some(surface) => Ok(surface),
|
||||
|
||||
@@ -40,6 +40,7 @@ use std::os::fd::{AsRawFd as _, BorrowedFd, OwnedFd};
|
||||
use std::sync::Arc;
|
||||
|
||||
use super::display::{Display, Error, check};
|
||||
use crate::codec::PictureRole;
|
||||
use super::ffi;
|
||||
use super::surface::SurfaceFormat;
|
||||
|
||||
@@ -98,10 +99,13 @@ pub struct TargetShape {
|
||||
}
|
||||
|
||||
/// A target for a decode's picture, made as the picture's storage is: asked once, with the
|
||||
/// picture's shape, as the decoder makes the decode's picture; `None` where the caller has none
|
||||
/// for it, and the picture is decoded into a surface of the decoder's own. A target of another
|
||||
/// shape is not used, as if `None` were answered.
|
||||
pub type TargetFor = Box<dyn FnOnce(TargetShape) -> Option<Target> + Send>;
|
||||
/// picture's shape and whether a later picture may predict from it (its [`PictureRole`]), as the
|
||||
/// decoder makes the decode's picture; `None` where the caller has none for it, and the picture
|
||||
/// is decoded into a surface of the decoder's own. A target of another shape is not used, as if
|
||||
/// `None` were answered. A caller that keeps every picture it may read again apart from the
|
||||
/// driver answers `None` for a reference picture: the decoder then reads it as a reference from
|
||||
/// its own surface alone.
|
||||
pub type TargetFor = Box<dyn FnOnce(TargetShape, PictureRole) -> Option<Target> + Send>;
|
||||
|
||||
/// The height a target of a picture `height` rows tall, its planes at `luma` and `chroma`, is
|
||||
/// imported as (module docs): the least even height, from the picture's, at which the planes as
|
||||
|
||||
@@ -374,8 +374,9 @@ impl Decoder {
|
||||
/// Decodes `unit` into `target`, and answers its one frame, decoded, which must be in it.
|
||||
fn decode(&mut self, unit: &[u8], id: u64, target: Target) -> Result<Frame, String> {
|
||||
let shape = target.shape();
|
||||
// Every picture, a reference too: the check is of how the driver reads references.
|
||||
let target_for: super::target::TargetFor =
|
||||
Box::new(move |asked| (asked == shape).then_some(target));
|
||||
Box::new(move |asked, _role| (asked == shape).then_some(target));
|
||||
let decoded = match self {
|
||||
Decoder::H264(decoder) => decoder.decode_into(unit, id, Some(target_for)),
|
||||
Decoder::Hevc(decoder) => decoder.decode_into(unit, id, Some(target_for)),
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
//! The render node of the GPU a runtime device runs on, which a host video API opens the GPU by:
|
||||
//! the reims-graphics device's VA-API arm (reims-vgpu's `graphics::vaapi`) opens the runtime
|
||||
//! device's. It is the node the device's `VK_EXT_physical_device_drm` names, else the one sysfs
|
||||
//! lists for the PCI address its `VK_EXT_pci_bus_info` names; none where neither does.
|
||||
//! lists for the PCI address its `VK_EXT_pci_bus_info` names; none where neither does. The rt
|
||||
//! platform's device answers the same two, which `render_node_at` takes.
|
||||
//! On Windows the GPU is opened by its DXGI adapter's LUID instead (`adapter_luid_of`, built for
|
||||
//! Windows alone): the one Vulkan answers for the physical device of the runtime device's UUID.
|
||||
|
||||
@@ -21,6 +22,19 @@ pub fn render_node_of(device: &rt::Device) -> Option<PathBuf> {
|
||||
.or_else(|| pci_address.as_deref().and_then(node_at_pci))
|
||||
}
|
||||
|
||||
/// The render node of the GPU whose render node's numbers (`VK_EXT_physical_device_drm`) and PCI
|
||||
/// address (`VK_EXT_pci_bus_info`) are `render` and `pci`, found as [`render_node_of`] finds a
|
||||
/// runtime device's: the rt platform's device answers them (`rt_platform::HostGpu`).
|
||||
#[cfg(target_os = "linux")]
|
||||
pub fn render_node_at(
|
||||
render: Option<(u32, u32)>,
|
||||
pci: Option<(u32, u32, u32, u32)>,
|
||||
) -> Option<PathBuf> {
|
||||
render
|
||||
.and_then(|(major, minor)| node_of(major.into(), minor.into()))
|
||||
.or_else(|| pci.map(pci_address_of).as_deref().and_then(node_at_pci))
|
||||
}
|
||||
|
||||
/// The LUID of the DXGI adapter `device`'s GPU is, as Vulkan answers it for the physical device
|
||||
/// whose UUID is the runtime device's (`VkPhysicalDeviceIDProperties::deviceLUID`, read through an
|
||||
/// instance of this call's own at Vulkan 1.1); `None` where no loader, no such device, or no valid
|
||||
|
||||
@@ -995,6 +995,13 @@ impl Buffer<RtPlatform> for RtObj<RtBuffer> {
|
||||
exported.export(rt::ExportHandle::OpaqueFd, self.rt.length())
|
||||
}
|
||||
|
||||
/// A dma-buf buffer's memory as a new dma-buf (`super::export`); `Err` for any other.
|
||||
#[cfg(unix)]
|
||||
fn export_dma_buf(&self) -> Result<ExportedMemory, String> {
|
||||
let exported = (self.exported.as_ref()).ok_or("the buffer was not made exportable")?;
|
||||
exported.export(rt::ExportHandle::DmaBuf, self.rt.length())
|
||||
}
|
||||
|
||||
/// rt's acquire of what another API wrote into an exportable buffer's block
|
||||
/// (`super::export`, "The acquire"); `Err` for any other buffer, which no other API writes.
|
||||
#[cfg(unix)]
|
||||
|
||||
@@ -644,6 +644,25 @@ impl GpuDevice<RtPlatform> for RtObj<RtDevice> {
|
||||
Ok(made)
|
||||
}
|
||||
|
||||
/// A buffer of zeros of its own exported block, whose memory VA-API imports as a dma-buf
|
||||
/// (`super::export`), its byte `aligned_byte`, where given, on a multiple of 256 bytes of the
|
||||
/// memory; `None` where rt makes none, counted, and the caller makes the buffer as any other.
|
||||
/// The bytes between a texture's rows are no matter: rt makes no texture over a buffer.
|
||||
#[cfg(unix)]
|
||||
fn new_dma_buf_buffer_of_length_padding_unobserved(
|
||||
&self,
|
||||
on: OnFault,
|
||||
length: u64,
|
||||
options: u64,
|
||||
aligned_byte: Option<u64>,
|
||||
) -> RaiseResult<Option<<RtPlatform as Platform>::Buffer>> {
|
||||
let _ = on;
|
||||
let sel = "newBufferWithLength:options: (dma-buf)";
|
||||
let handle = rt::ExportHandle::DmaBuf;
|
||||
let made = super::export::exportable(self, sel, handle, length, options, aligned_byte);
|
||||
Ok(made)
|
||||
}
|
||||
|
||||
/// rt's queue, of Metal's default `maxCommandBufferCount`; nil where rt refuses it.
|
||||
fn new_command_queue(&self) -> Option<RtObj<RtQueue>> {
|
||||
match self.rt.new_command_queue(METAL_MAX_COMMAND_BUFFER_COUNT) {
|
||||
|
||||
@@ -1,24 +1,32 @@
|
||||
//! The rt platform's buffers whose memory another API imports
|
||||
//! (`GpuDevice::new_exportable_buffer_of_length`): IOSurface device caches that the host's video
|
||||
//! writes its pictures into on its own GPU work, through CUDA's import ([`exportable`]).
|
||||
//! The rt platform's buffers whose memory another API imports: IOSurface device caches that the
|
||||
//! host's video writes its pictures into on its own GPU work ([`exportable`]), through CUDA's
|
||||
//! import of an opaque fd (`GpuDevice::new_exportable_buffer_of_length`, NVDEC's) or VA-API's of
|
||||
//! a dma-buf (`GpuDevice::new_dma_buf_buffer_of_length_padding_unobserved`).
|
||||
//!
|
||||
//! **One block a buffer.** Each is a buffer of its own exported block (rt's
|
||||
//! `Device::new_exported_block`): one dedicated allocation of Private memory, exported as an
|
||||
//! opaque fd, which CUDA imports with the allocation's size, as dedicated, and the buffer's place
|
||||
//! in it ([`ExportedMemory`]). Its bytes are zeros, as `newBufferWithLength:options:`'s are (rt's
|
||||
//! GPU fill ahead of the next command buffer). Where the caller names a byte of it to lie on a
|
||||
//! multiple of 256 bytes of its memory (`aligned_byte`), the buffer starts that far into the block
|
||||
//! that the byte does.
|
||||
//! `Device::new_exported_block`): one dedicated allocation of Private memory, exported as the
|
||||
//! kind of handle its make names, which the other API imports with the allocation's size, as
|
||||
//! dedicated, and the buffer's place in it ([`ExportedMemory`]). Its bytes are zeros, as
|
||||
//! `newBufferWithLength:options:`'s are (rt's GPU fill ahead of the next command buffer). Its
|
||||
//! memory is linear: VA-API imports it as `DRM_FORMAT_MOD_LINEAR`. Where the caller names a byte
|
||||
//! of it to lie on a multiple of 256 bytes of its memory (`aligned_byte`, which the IOSurface
|
||||
//! device caches name for their first plane), the buffer starts that far into the block that the
|
||||
//! byte does: VA-API imports a cache's planes with each plane's first byte and rows on such
|
||||
//! multiples, and CUDA maps its import from the allocation's first byte and finds the buffer at
|
||||
//! its place in the mapping (reims-vgpu-video's `ImportedBuffer::import`).
|
||||
//!
|
||||
//! **Each export a new fd.** rt gives one fd of the memory as it makes the block, which the
|
||||
//! buffer keeps; each `Buffer::export_memory` answers a duplicate of it (`dup`), the caller's
|
||||
//! own, as an import takes it.
|
||||
//! buffer keeps; each `Buffer::export_memory` (an opaque fd's) or `Buffer::export_dma_buf` (a
|
||||
//! dma-buf's) answers a duplicate of it (`dup`), the caller's own, as an import takes it.
|
||||
//!
|
||||
//! **The acquire.** `Buffer::external_write_done` is rt's `Block::external_writes_done`: the
|
||||
//! block's acquire from the other API's queue family, recorded in rt's upload ahead of every
|
||||
//! command buffer committed after it, which then reads what that API wrote. No release goes back
|
||||
//! to the other API: it reads nothing of the block but what it wrote itself (rt's contract,
|
||||
//! which NVDEC's landing meets: it writes each picture's rows whole).
|
||||
//! to the other API: it touches nothing of the block between the acquire and its next write of
|
||||
//! a whole picture (rt's contract). NVDEC's landing meets it, writing each picture's rows whole;
|
||||
//! so does VA-API's, which decodes into a cache only a picture no later picture predicts from,
|
||||
//! and writes every other one there by its video processing (reims-vgpu's `graphics::vaapi`), so
|
||||
//! that no decode reads a reference from the block.
|
||||
//!
|
||||
//! **Refusals.** Where rt makes no exported block (a device that exports no opaque fd, a dma-buf
|
||||
//! without the foreign queue family, a limit) or no buffer in it, the call answers `None` and the
|
||||
@@ -239,4 +247,50 @@ mod tests {
|
||||
assert_eq!(counted(EXPORT_REFUSED_CODE), refusals);
|
||||
assert_ne!(buffer.id(), unaligned.id());
|
||||
}
|
||||
|
||||
/// Dma-buf buffers on the GPU the test holds, where its device exports a dma-buf: a Private
|
||||
/// buffer of the asked length at a GPU address, as far into its block as puts its byte 32 on
|
||||
/// a multiple of 256 bytes of the memory; each export a new dma-buf of the dedicated
|
||||
/// allocation, none as an opaque fd; the acquire taken. With no byte named, the buffer lies
|
||||
/// at its block's first byte. Options of other storage are declined uncounted. Where the
|
||||
/// device exports no dma-buf, the refusal is counted and the make answers `None`.
|
||||
#[test]
|
||||
fn dma_buf_buffers_on_vulkan() {
|
||||
use std::os::fd::AsRawFd as _;
|
||||
let platform = RtPlatform::new();
|
||||
let device = (platform.system_default_device()).expect("an rt device on the GPU it holds");
|
||||
let on = OnFault::Return;
|
||||
let length = 3840 * 2160 * 3 / 2 + 32;
|
||||
let refusals = counted(EXPORT_REFUSED_CODE);
|
||||
let made = device.new_dma_buf_buffer_of_length_padding_unobserved(on, length, 0x20, Some(32));
|
||||
let Some(buffer) = made.expect("never a raise") else {
|
||||
assert_eq!(counted(EXPORT_REFUSED_CODE), refusals + 1);
|
||||
println!("the device exports no dma-buf: refused, counted");
|
||||
return;
|
||||
};
|
||||
assert_eq!(counted(EXPORT_REFUSED_CODE), refusals);
|
||||
assert_eq!((buffer.length(), buffer.storage_mode()), (length, 2));
|
||||
assert_ne!(buffer.gpu_address(), 0);
|
||||
let first = buffer.export_dma_buf().expect("an export");
|
||||
let second = buffer.export_dma_buf().expect("another");
|
||||
assert_ne!(first.fd.as_raw_fd(), second.fd.as_raw_fd());
|
||||
assert_eq!(first.offset, 224);
|
||||
assert_eq!((first.offset + 32) % 256, 0);
|
||||
assert!(first.dedicated && first.length == length);
|
||||
assert!(first.allocation_size >= first.offset + length);
|
||||
assert_eq!(
|
||||
(second.offset, second.allocation_size),
|
||||
(first.offset, first.allocation_size)
|
||||
);
|
||||
assert!(buffer.export_memory().is_err());
|
||||
assert_eq!(buffer.external_write_done(), Ok(()));
|
||||
let unaligned = device.new_dma_buf_buffer_of_length_padding_unobserved(on, 4096, 0x20, None);
|
||||
let unaligned = unaligned
|
||||
.expect("never a raise")
|
||||
.expect("a dma-buf buffer");
|
||||
assert_eq!(unaligned.export_dma_buf().expect("an export").offset, 0);
|
||||
let shared = device.new_dma_buf_buffer_of_length_padding_unobserved(on, 4096, 0, None);
|
||||
assert!(shared.expect("never a raise").is_none());
|
||||
assert_eq!(counted(EXPORT_REFUSED_CODE), refusals);
|
||||
}
|
||||
}
|
||||
@@ -26,7 +26,13 @@
|
||||
//! last (a reference's). The picture is then there once its decode is done and the platform
|
||||
//! acquired what the driver wrote (`Buffer::external_write_done`, which the device's next
|
||||
//! submission records before its commands), and nothing is copied. Each reason a session's
|
||||
//! pictures are not, of a cache made so, is logged once.
|
||||
//! pictures are not, of a cache made so, is logged once. Where the platform's exported memory
|
||||
//! is not the driver's to read once it is acquired ([`CachedReferences::Apart`], the rt
|
||||
//! platform's), only a picture no later picture predicts from is decoded into its cache: a
|
||||
//! reference picture is decoded into a surface of the decoder's own, which the decoder reads it
|
||||
//! from as a reference, and lands in its cache by the first way below. Each session counts the
|
||||
//! pictures of each role it asked a cache for, and the references it kept apart, and logs them
|
||||
//! at its end.
|
||||
//! - **Otherwise a picture lands** in the output's planes one of three ways, each the fallback at
|
||||
//! the last's refusal, the session counting each:
|
||||
//! 1. **In the surface's pages or its device cache, by the driver's video processing** (the VA
|
||||
@@ -72,6 +78,7 @@
|
||||
#[cfg(unix)]
|
||||
use std::os::fd::{AsFd as _, OwnedFd};
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::atomic::{AtomicU64, Ordering};
|
||||
use std::sync::{Arc, LazyLock, Weak};
|
||||
|
||||
use reims_vgpu_graphics::wire::{
|
||||
@@ -80,6 +87,8 @@ use reims_vgpu_graphics::wire::{
|
||||
RGFX_VIDEO_H264_MAIN, RGFX_VIDEO_HEVC_MAIN, RGFX_VIDEO_HEVC_MAIN_10, RgfxVideoCodecCaps,
|
||||
RgfxVideoEncodeCaps, RgfxVideoOutputLayout,
|
||||
};
|
||||
#[cfg(unix)]
|
||||
use reims_vgpu_video::codec::PictureRole;
|
||||
use reims_vgpu_video::core::backend::{BackendError as VideoError, DeviceWrite};
|
||||
#[cfg(unix)]
|
||||
use reims_vgpu_video::core::device_memory::{
|
||||
@@ -147,6 +156,46 @@ const H264_TOP_LEVEL: u32 = 62;
|
||||
const H264_WIDEST: u32 = 4096;
|
||||
const HEVC_TOP_LEVEL: u32 = 186;
|
||||
|
||||
/// What the platform's device lets the VA-API arm do ([`open`]).
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub(crate) struct PlatformRules {
|
||||
/// Whether a picture a later one predicts from may be decoded into its device cache.
|
||||
pub(crate) references: CachedReferences,
|
||||
/// Whether the arm answers the driver's encoders: where the platform reads their sources for
|
||||
/// them as the Vulkan platform does (`encode`'s module doc).
|
||||
pub(crate) encodes: bool,
|
||||
}
|
||||
|
||||
impl PlatformRules {
|
||||
/// The Vulkan platform's: every picture may be decoded into its cache, and the driver's
|
||||
/// encoders are answered.
|
||||
pub(crate) const VULKAN: PlatformRules = PlatformRules {
|
||||
references: CachedReferences::InCaches,
|
||||
encodes: true,
|
||||
};
|
||||
}
|
||||
|
||||
/// Whether a reference picture is decoded into its output's device cache (module doc).
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub(crate) enum CachedReferences {
|
||||
/// Every picture, a reference too: the platform's exported memory stays the driver's to read
|
||||
/// after its acquire, and the driver's check keeps a driver that would read a reference from
|
||||
/// its target from decoding into one at all (the VA crate's `TargetChecks`).
|
||||
InCaches,
|
||||
/// Only a picture no later picture predicts from: the platform's exported memory is not the
|
||||
/// driver's to touch between its acquire and the driver's next write of a whole picture
|
||||
/// (the rt platform's `rt_platform::export`, "The acquire").
|
||||
#[cfg_attr(
|
||||
not(all(feature = "rt", unix)),
|
||||
allow(
|
||||
dead_code,
|
||||
reason = "the rt platform's arm alone keeps references apart ([`open_on_rt`]), and \
|
||||
the tests"
|
||||
)
|
||||
)]
|
||||
Apart,
|
||||
}
|
||||
|
||||
/// Where a VA display is opened ([`open`]): a render node, or on Windows a DXGI adapter by its
|
||||
/// LUID.
|
||||
pub(crate) enum DisplayAt<'a> {
|
||||
@@ -208,9 +257,46 @@ pub(crate) fn open_on(
|
||||
runtime,
|
||||
Some(copies),
|
||||
checks,
|
||||
PlatformRules::VULKAN,
|
||||
)
|
||||
}
|
||||
|
||||
/// The VA-API video of the rt platform's `device`, on its GPU's render node
|
||||
/// (`host_gpus::render_node_at`), as [`open`] opens it; `None`, logged, where its GPU has none.
|
||||
/// Its pictures land by the driver's video processing or the CPU's copy, as no GPU copier is
|
||||
/// given; a reference picture is not decoded into its cache ([`CachedReferences::Apart`]), and no
|
||||
/// encoder is answered: the platform does not yet release an encoder's sources to the driver.
|
||||
#[cfg(all(feature = "rt", unix))]
|
||||
pub(crate) fn open_on_rt(
|
||||
device: &RtDevice,
|
||||
runtime: Arc<dyn Runtime>,
|
||||
) -> Option<Arc<dyn GraphicsVideo<reims_vgpu_vulkan::RtPlatform>>> {
|
||||
let gpu = device.host_gpu();
|
||||
let Some(node) = reims_vgpu_vulkan::host_gpus::render_node_at(gpu.render, gpu.pci) else {
|
||||
log::info!("hardware video: no VA-API: the device's GPU has no render node");
|
||||
return None;
|
||||
};
|
||||
let checks = reims_vgpu_vulkan::platform::cache_directory()
|
||||
.map(|dir| dir.join("device-checks").join("va-targets"));
|
||||
let rules = PlatformRules {
|
||||
references: CachedReferences::Apart,
|
||||
encodes: false,
|
||||
};
|
||||
open::<reims_vgpu_vulkan::RtPlatform>(
|
||||
device,
|
||||
device.facts().registry_id,
|
||||
DisplayAt::Node(&node),
|
||||
runtime,
|
||||
None,
|
||||
checks,
|
||||
rules,
|
||||
)
|
||||
}
|
||||
|
||||
/// The rt platform's device.
|
||||
#[cfg(all(feature = "rt", unix))]
|
||||
type RtDevice = reims_vgpu_vulkan::rt_platform::RtObj<reims_vgpu_vulkan::rt_platform::RtDevice>;
|
||||
|
||||
/// The VA-API video of the runtime device `device` on Windows: on the DXGI adapter of its GPU, by
|
||||
/// the LUID Vulkan answers for it (`host_gpus::adapter_luid_of`), over Mesa's VA-on-D3D12 driver;
|
||||
/// `None`, logged, where its GPU names none. Its pictures land by the runtime device's GPU, from
|
||||
@@ -233,14 +319,16 @@ pub(crate) fn open_on(
|
||||
runtime,
|
||||
Some(copies),
|
||||
None,
|
||||
PlatformRules::VULKAN,
|
||||
)
|
||||
}
|
||||
|
||||
/// The VA-API video of `device`'s GPU, on its render node `at` (on Windows its adapter), its decoders' threads and waits
|
||||
/// on `runtime`, its pictures copied by the GPU through `copies` where given, the driver's check
|
||||
/// of decodes into device caches kept in `checks` where given; one per device while it lives, by
|
||||
/// `registry_id`. `None`, logged at `info` with why, where libva does not load, the node opens no
|
||||
/// display, or the driver decodes neither H.264 nor HEVC.
|
||||
/// of decodes into device caches kept in `checks` where given, as the platform's `rules` let it;
|
||||
/// one per device while it lives, by `registry_id`. `None`, logged at `info` with why, where
|
||||
/// libva does not load, the node opens no display, or the driver decodes neither H.264 nor HEVC
|
||||
/// (and, where the rules let it, encodes neither).
|
||||
pub(crate) fn open<P: Platform>(
|
||||
device: &P::Device,
|
||||
registry_id: u64,
|
||||
@@ -248,6 +336,7 @@ pub(crate) fn open<P: Platform>(
|
||||
runtime: Arc<dyn Runtime>,
|
||||
copies: Option<Arc<dyn GpuCopies<P>>>,
|
||||
checks: Option<PathBuf>,
|
||||
rules: PlatformRules,
|
||||
) -> Option<Arc<dyn GraphicsVideo<P>>> {
|
||||
type Made = Vec<(u64, Weak<dyn std::any::Any + Send + Sync>)>;
|
||||
static MADE: LazyLock<Mutex<Made>> = LazyLock::new(|| Mutex::new(Vec::new()));
|
||||
@@ -259,7 +348,8 @@ pub(crate) fn open<P: Platform>(
|
||||
if let Some(video) = kept {
|
||||
return Some(video);
|
||||
}
|
||||
let video = match VaapiVideo::<P>::new(device, &at, runtime, copies, checks.as_deref()) {
|
||||
let made = VaapiVideo::<P>::new(device, &at, runtime, copies, checks.as_deref(), rules);
|
||||
let video = match made {
|
||||
Ok(video) => Arc::new(video),
|
||||
Err(why) => {
|
||||
log::info!("hardware video: no VA-API at {at}: {why}");
|
||||
@@ -297,11 +387,20 @@ impl<P: Platform> VaapiVideo<P> {
|
||||
runtime: Arc<dyn Runtime>,
|
||||
copies: Option<Arc<dyn GpuCopies<P>>>,
|
||||
checks: Option<&Path>,
|
||||
rules: PlatformRules,
|
||||
) -> Result<Self, String> {
|
||||
let started = runtime.now();
|
||||
let display = at.open(Arc::clone(&runtime)).map_err(|e| e.to_string())?;
|
||||
let capabilities = display.capabilities().map_err(|e| e.to_string())?;
|
||||
let encoders: Arc<[EncoderSupport]> = encoder_support(&display).into();
|
||||
let encoders: Arc<[EncoderSupport]> = if rules.encodes {
|
||||
encoder_support(&display).into()
|
||||
} else {
|
||||
log::info!(
|
||||
"hardware video: {NAME} at {at}: no encoder answered: the platform reads no \
|
||||
encoder's sources for it"
|
||||
);
|
||||
Arc::new([])
|
||||
};
|
||||
let mut caps = caps_of(&capabilities);
|
||||
caps.extend(encode::encode_caps_of(&encoders, &capabilities));
|
||||
if caps.is_empty() {
|
||||
@@ -318,7 +417,8 @@ impl<P: Platform> VaapiVideo<P> {
|
||||
writes_imports.p010(),
|
||||
);
|
||||
let imports = Arc::new(GuestWrites::new(&display));
|
||||
let targets = CacheTargets::new(device, &display, &runtime, checks).map(Arc::new);
|
||||
let targets =
|
||||
CacheTargets::new(device, &display, &runtime, checks, rules.references).map(Arc::new);
|
||||
Ok(Self {
|
||||
display,
|
||||
device: device.clone_ref(),
|
||||
@@ -399,6 +499,7 @@ impl<P: Platform> GraphicsVideo<P> for VaapiVideo<P> {
|
||||
copier: self.copier(),
|
||||
said: Arc::new(Mutex::new(Vec::new())),
|
||||
targets: self.targets.clone(),
|
||||
roles: Arc::new(RoleCounts::default()),
|
||||
};
|
||||
let session = HostVideoSession::new(&self.device, codec, NAME, Box::new(decoder))?;
|
||||
Some(Arc::new(session))
|
||||
@@ -593,6 +694,8 @@ struct CacheTargets<P: Platform> {
|
||||
display: Display,
|
||||
checks: TargetChecks,
|
||||
memories: DeviceImports<CacheMemory<P>>,
|
||||
/// Whether a reference picture is decoded into its cache.
|
||||
references: CachedReferences,
|
||||
}
|
||||
|
||||
/// A device cache's memory, exported as a dma-buf, and the last target imported over it.
|
||||
@@ -618,6 +721,7 @@ impl<P: Platform> CacheTargets<P> {
|
||||
display: &Display,
|
||||
runtime: &Arc<dyn Runtime>,
|
||||
checks: Option<&Path>,
|
||||
references: CachedReferences,
|
||||
) -> Option<Self> {
|
||||
let declined = |why: &str| {
|
||||
log::info!("hardware video: {NAME}: no picture is decoded into a device cache: {why}");
|
||||
@@ -649,27 +753,34 @@ impl<P: Platform> CacheTargets<P> {
|
||||
if !passed {
|
||||
return declined(&format!("the driver's check: {}", checks.summary()));
|
||||
}
|
||||
let which = match references {
|
||||
CachedReferences::InCaches => "pictures are",
|
||||
CachedReferences::Apart => "pictures no later picture predicts from are",
|
||||
};
|
||||
log::info!(
|
||||
"hardware video: {NAME}: pictures are decoded into device caches where the driver's \
|
||||
check passed: {}",
|
||||
"hardware video: {NAME}: {which} decoded into device caches where the driver's check \
|
||||
passed: {}",
|
||||
checks.summary()
|
||||
);
|
||||
Some(Self {
|
||||
display: display.clone_ref(),
|
||||
checks,
|
||||
memories: DeviceImports::new(runtime, "VA-API's device caches", MOST_CACHES),
|
||||
references,
|
||||
})
|
||||
}
|
||||
|
||||
/// What gives a decode of `codec` into `output` its picture's target: `None` where the
|
||||
/// output's planes do not both lie in one device cache made for the host's video
|
||||
/// ([`OutputPlane::cache`]). Each reason a picture's target is not
|
||||
/// given then is logged once a session, in `said`.
|
||||
/// ([`OutputPlane::cache`]), and for a reference picture where references are kept apart
|
||||
/// (module doc). Each reason a picture's target is not given then is logged once a session,
|
||||
/// in `said`, and the picture's role is counted in `roles`.
|
||||
fn target_for(
|
||||
self: &Arc<Self>,
|
||||
codec: TargetCodec,
|
||||
output: &DecodeOutput<P>,
|
||||
said: &Arc<Mutex<Vec<String>>>,
|
||||
roles: &Arc<RoleCounts>,
|
||||
) -> Option<TargetFor> {
|
||||
let (luma, chroma) = (&output.luma, &output.chroma);
|
||||
let (Some(cache), Some(other)) = (&luma.cache, &chroma.cache) else {
|
||||
@@ -686,13 +797,17 @@ impl<P: Platform> CacheTargets<P> {
|
||||
(Room::of(luma), luma.offset, luma.rows_held),
|
||||
(Room::of(chroma), chroma.offset, chroma.rows_held),
|
||||
];
|
||||
let (targets, cache, buffer, said) = (
|
||||
let (targets, cache, buffer, said, roles) = (
|
||||
Arc::clone(self),
|
||||
cache.clone(),
|
||||
luma.buffer.clone_ref(),
|
||||
Arc::clone(said),
|
||||
Arc::clone(roles),
|
||||
);
|
||||
Some(Box::new(move |shape| {
|
||||
Some(Box::new(move |shape, role| {
|
||||
if !roles.count(role, targets.references) {
|
||||
return None;
|
||||
}
|
||||
let made = targets.target(codec, shape, &cache, &buffer, planes);
|
||||
made.map_err(|why| say(&said, why)).ok()
|
||||
}))
|
||||
@@ -864,6 +979,7 @@ impl<P: Platform> CacheTargets<P> {
|
||||
_display: &Display,
|
||||
_runtime: &Arc<dyn Runtime>,
|
||||
_checks: Option<&Path>,
|
||||
_references: CachedReferences,
|
||||
) -> Option<Self> {
|
||||
log::info!(
|
||||
"hardware video: {NAME}: no picture is decoded into a device cache: this host makes \
|
||||
@@ -877,11 +993,53 @@ impl<P: Platform> CacheTargets<P> {
|
||||
_codec: TargetCodec,
|
||||
_output: &DecodeOutput<P>,
|
||||
_said: &Arc<Mutex<Vec<String>>>,
|
||||
_roles: &Arc<RoleCounts>,
|
||||
) -> Option<TargetFor> {
|
||||
match self.never {}
|
||||
}
|
||||
}
|
||||
|
||||
/// A decode session's counts of the pictures it asked a device cache for, by role, and of the
|
||||
/// references it kept apart from their caches (module doc), logged at its end where it asked any.
|
||||
#[derive(Debug, Default)]
|
||||
struct RoleCounts {
|
||||
references: AtomicU64,
|
||||
non_references: AtomicU64,
|
||||
kept_apart: AtomicU64,
|
||||
}
|
||||
|
||||
impl RoleCounts {
|
||||
/// Counts a picture of `role` asked a cache for; whether it may be decoded into its cache,
|
||||
/// as `references` says of a reference picture.
|
||||
#[cfg(unix)]
|
||||
fn count(&self, role: PictureRole, references: CachedReferences) -> bool {
|
||||
let (counter, taken) = match (role, references) {
|
||||
(PictureRole::NonReference, _) => (&self.non_references, true),
|
||||
(PictureRole::Reference, CachedReferences::InCaches) => (&self.references, true),
|
||||
(PictureRole::Reference, CachedReferences::Apart) => {
|
||||
self.kept_apart.fetch_add(1, Ordering::Relaxed);
|
||||
(&self.references, false)
|
||||
}
|
||||
};
|
||||
counter.fetch_add(1, Ordering::Relaxed);
|
||||
taken
|
||||
}
|
||||
|
||||
/// The line a session logs at its end, where it asked a cache for any picture.
|
||||
fn line(&self) -> Option<String> {
|
||||
let references = self.references.load(Ordering::Relaxed);
|
||||
let non_references = self.non_references.load(Ordering::Relaxed);
|
||||
let kept_apart = self.kept_apart.load(Ordering::Relaxed);
|
||||
(references > 0 || non_references > 0).then(|| {
|
||||
format!(
|
||||
"hardware video: {NAME}: a session's pictures for device caches: {references} \
|
||||
references ({kept_apart} kept apart, decoded in the decoder's own surfaces), \
|
||||
{non_references} non-references"
|
||||
)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Logs `why` once in `said`'s session, and at info once per process (`kind`): why a picture was
|
||||
/// not decoded into its cache.
|
||||
#[cfg(unix)]
|
||||
@@ -915,6 +1073,16 @@ struct VaDecoder<P: Platform> {
|
||||
said: Arc<Mutex<Vec<String>>>,
|
||||
/// What decodes its pictures straight into their caches, where the video does.
|
||||
targets: Option<Arc<CacheTargets<P>>>,
|
||||
/// The roles of the pictures it asked a cache for.
|
||||
roles: Arc<RoleCounts>,
|
||||
}
|
||||
|
||||
impl<P: Platform> Drop for VaDecoder<P> {
|
||||
fn drop(&mut self) {
|
||||
if let Some(line) = self.roles.line() {
|
||||
log::info!("{line}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<P: Platform> VaDecoder<P> {
|
||||
@@ -952,7 +1120,7 @@ impl<P: Platform> HostDecoder<P> for VaDecoder<P> {
|
||||
Codec::Hevc(_) => TargetCodec::Hevc,
|
||||
};
|
||||
let target_for = (self.targets.as_ref())
|
||||
.and_then(|targets| targets.target_for(codec, output, &self.said));
|
||||
.and_then(|targets| targets.target_for(codec, output, &self.said, &self.roles));
|
||||
let mut frames = self.frames(au, target_for)?;
|
||||
let frame = match (frames.pop(), frames.len()) {
|
||||
(None, _) => return Ok(None),
|
||||
|
||||
@@ -278,6 +278,87 @@ fn the_gpu_copy_takes_the_crop_the_cpu_copy_writes() {
|
||||
);
|
||||
}
|
||||
|
||||
/// Where references are kept apart (the rt platform's rules) a reference picture is given no
|
||||
/// cache to be decoded into, and a picture no later one predicts from is; where they are not (the
|
||||
/// Vulkan platform's), every picture is. A session counts each picture by its role, and the
|
||||
/// references it kept apart, in the line it logs at its end, and logs none where it asked a cache
|
||||
/// for no picture.
|
||||
#[test]
|
||||
fn references_are_kept_apart_from_their_caches_where_the_rules_say_and_counted() {
|
||||
use PictureRole::{NonReference, Reference};
|
||||
|
||||
let apart = RoleCounts::default();
|
||||
assert_eq!(apart.line(), None);
|
||||
assert!(!apart.count(Reference, CachedReferences::Apart));
|
||||
assert!(apart.count(NonReference, CachedReferences::Apart));
|
||||
assert!(!apart.count(Reference, CachedReferences::Apart));
|
||||
let line = apart.line().expect("a line");
|
||||
assert!(
|
||||
line.ends_with(
|
||||
"2 references (2 kept apart, decoded in the decoder's own surfaces), 1 non-references"
|
||||
),
|
||||
"{line}"
|
||||
);
|
||||
|
||||
let in_caches = RoleCounts::default();
|
||||
assert!(in_caches.count(Reference, CachedReferences::InCaches));
|
||||
assert!(in_caches.count(NonReference, CachedReferences::InCaches));
|
||||
let line = in_caches.line().expect("a line");
|
||||
assert!(
|
||||
line.ends_with(
|
||||
"1 references (0 kept apart, decoded in the decoder's own surfaces), 1 non-references"
|
||||
),
|
||||
"{line}"
|
||||
);
|
||||
}
|
||||
|
||||
/// On the rt platform the VA-API arm answers what the driver decodes and no encoder (the platform
|
||||
/// releases no encoder's sources to the driver yet), and decodes into the device's caches where
|
||||
/// the driver's check passed a codec: rt's exported blocks, as dma-bufs, take the pictures no
|
||||
/// later one predicts from.
|
||||
#[cfg(feature = "rt")]
|
||||
#[test]
|
||||
#[ignore = "needs a VA-API GPU: run on the VA-API host"]
|
||||
fn the_rt_platforms_va_api_arm_decodes_alone() {
|
||||
use reims_vgpu_vulkan::RtPlatform;
|
||||
let platform = Arc::new(RtPlatform::new());
|
||||
crate::entry::run(|_| {
|
||||
let device = platform.system_default_device().expect("an rt device");
|
||||
let runtime = crate::video::runtime::PlatformRuntime::of(&platform);
|
||||
let video = open_on_rt(&device, runtime).expect("VA-API on the rt device's GPU");
|
||||
let caps = video.caps();
|
||||
assert!(!caps.is_empty());
|
||||
assert!(
|
||||
caps.iter().all(|c| c.direction == RGFX_VIDEO_DECODE),
|
||||
"{caps:?}"
|
||||
);
|
||||
assert!(video.guest_encoders().is_empty());
|
||||
assert!(video.guest_decodes());
|
||||
assert!(video.lands_in_device_caches());
|
||||
let gpu = device.host_gpu();
|
||||
let node = reims_vgpu_vulkan::host_gpus::render_node_at(gpu.render, gpu.pci);
|
||||
let display = Display::open(
|
||||
&node.expect("a render node"),
|
||||
crate::video::runtime::PlatformRuntime::of(&platform),
|
||||
)
|
||||
.expect("a VA display");
|
||||
let checks = TargetChecks::of(&display, None);
|
||||
let passed = [
|
||||
(TargetCodec::H264, TargetFormat::Nv12),
|
||||
(TargetCodec::Hevc, TargetFormat::Nv12),
|
||||
(TargetCodec::Hevc, TargetFormat::P010),
|
||||
]
|
||||
.into_iter()
|
||||
.any(|(codec, format)| checks.passed(codec, format).is_ok());
|
||||
assert_eq!(
|
||||
video.decodes_into_device_caches(),
|
||||
passed,
|
||||
"{}",
|
||||
checks.summary()
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
/// The conformance suite's streams, through the arm's decoders, each picture landed by the GPU's
|
||||
/// copy into planes laid out as CoreVideo lays a frame's (32 bytes into their pages), and by the
|
||||
/// CPU's copy into the same layout: every byte of the two must be equal, the bytes before, between
|
||||
@@ -611,7 +692,7 @@ fn cache_target(
|
||||
Arc::clone(reader),
|
||||
display.clone_ref(),
|
||||
);
|
||||
Box::new(move |shape: TargetShape| {
|
||||
Box::new(move |shape: TargetShape, _role| {
|
||||
let (width, height) = shape.coded;
|
||||
let sample = shape.format.bytes_per_sample();
|
||||
let row = u64::from(width) * sample;
|
||||
|
||||
@@ -6,10 +6,13 @@
|
||||
//! 1. NVDEC, where the device is an NVIDIA GPU that CUDA opens (Linux: `super::nvidia`).
|
||||
//! 2. VA-API, on the device's render node, whatever its vendor (Linux: `super::vaapi`), or on
|
||||
//! Windows on its DXGI adapter, over Mesa's VA-on-D3D12 driver (`super::vaapi`). The rt
|
||||
//! platform has no VA-API arm yet.
|
||||
//! platform's is Linux's alone (`super::vaapi::open_on_rt`).
|
||||
//!
|
||||
//! On the rt platform NVDEC decodes alone: NVENC reads a buffer the GPU copied a frame into,
|
||||
//! which rt's platform does not release to CUDA yet (`super::nvidia::open`'s `encodes`).
|
||||
//! On the rt platform the arms decode alone: NVENC reads a buffer the GPU copied a frame into,
|
||||
//! and VA-API's encoders a surface likewise, which rt's platform does not release to them yet
|
||||
//! (`super::nvidia::open`'s `encodes`, `super::vaapi::PlatformRules`). VA-API decodes a reference
|
||||
//! picture into a surface of its own there, not into its output's cache
|
||||
//! (`super::vaapi::CachedReferences`).
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
@@ -70,6 +73,10 @@ pub(crate) fn rt_graphics_video(
|
||||
) {
|
||||
return Some(video);
|
||||
}
|
||||
#[cfg(target_os = "linux")]
|
||||
if let Some(video) = super::vaapi::open_on_rt(device, Arc::clone(&runtime)) {
|
||||
return Some(video);
|
||||
}
|
||||
let _ = (device, runtime);
|
||||
None
|
||||
}
|
||||
|
||||
@@ -196,7 +196,7 @@ buffer of an encode session's process (reims-vgpu's `graphics/video_host/encode.
|
||||
| arm | access unit in place | a picture lands | layout asked of the guest |
|
||||
|---|---|---|---|
|
||||
| NVDEC | at any place and length | by one CUDA copy a plane: into the output's device caches, through CUDA's import of their memory, where the flag is set; into the pages, where CUDA registers their view; else read back and copied by the CPU | none |
|
||||
| VA-API | at any place and length | decoded into the output's device cache, where the driver's check of decodes into targets passed the codec and format and the cache's memory is a dma-buf; else by the driver's video processing into the pages, or into such a cache through its memory, where the import check passed for the format and the planes meet its alignment; else by the runtime device's GPU, a copy of each plane of the decode surface's export as dma-bufs into the pages or the device caches, where it imports them; else read back and copied by the CPU | granularity [2, 2], the import check's alignment |
|
||||
| VA-API | at any place and length | decoded into the output's device cache, where the driver's check of decodes into targets passed the codec and format and the cache's memory is a dma-buf (on the rt platform, only a picture no later picture predicts from: a reference is decoded into a surface of the decoder's own); else by the driver's video processing into the pages, or into such a cache through its memory, where the import check passed for the format and the planes meet its alignment; else by the runtime device's GPU, a copy of each plane of the decode surface's export as dma-bufs into the pages or the device caches, where it imports them; else read back and copied by the CPU | granularity [2, 2], the import check's alignment |
|
||||
|
||||
Both run in reims-vgpu's host video session (`graphics/video_host.rs`, [backends.md](backends.md),
|
||||
"The traits"): a plane in a device cache the arm does not write itself takes its picture staged
|
||||
|
||||
Reference in new issue
Block a user