editor: wire hardware video decode + NV12 preview compositing (Stage 3c-preview)
Make the dormant core HW-decode engine live for the preview path: - hw_video.rs: editor's HwVideoImporter — maps a decoded VAAPI surface to a DRM-PRIME DMA-BUF and imports it as wgpu NV12 plane textures on the *shared* device (the only one with the import extensions). install() creates the VAAPI device and injects it + the importer into the VideoManager. - main.rs: track whether the shared device is actually in use; only then (Linux, not LB_NO_SHARED_DEVICE) install hardware decode, using the CreationContext's shared device + adapter. - nv12_blit.rs + nv12_blit.wgsl: NV12 plane textures → BT.709 → sRGB-encoded → linear, written straight into the Rgba16Float HDR layer (no CPU upload). Colour math mirrors the software path so HW/SW video match; honours full_range. - stage.rs: the preview Video arm branches on inst.gpu (NV12 blit) vs rgba_data (existing upload+blit_straight); sets render_hardware_ok = !cpu_renderer so the CPU fallback still gets software frames. - video_exporter.rs: sets render_hardware_ok(false) before both compositing passes — export composites on the encoder's separate device, so a hardware decoder downloads to CPU instead (export stays software, correct). - dmabuf.rs: imported plane textures now also carry SAMPLED/TEXTURE_BINDING so they can be sampled by the NV12 blit (they were render-target-only); into_planes hands the textures to the longer-lived GpuVideoFrame. - video.rs: cache-key the GPU/CPU representation on want_gpu (HW-configured AND render_hardware_ok) so software-only decode keeps a single cache entry. Preview only this pass; export GPU-residency is the 3c-export follow-up. Untested at runtime here (no GPU/display in container) — both crates compile.
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
// NV12 → linear-RGB blit shader.
|
||||
//
|
||||
// Samples a hardware-decoded video frame stored as two planes — Y (R8Unorm) and
|
||||
// interleaved CbCr (Rg8Unorm, half-res) — converts BT.709 Y'CbCr → gamma-encoded
|
||||
// R'G'B', then sRGB→linear, and writes straight-alpha linear into the Rgba16Float
|
||||
// HDR layer. This mirrors the software path (swscale → sRGB RGBA8 → sampled as
|
||||
// Rgba8UnormSrgb → linear) so hardware- and software-decoded video match.
|
||||
//
|
||||
// The affine transform (viewport UV → frame UV) is the same packing as
|
||||
// canvas_blit.wgsl's BlitTransform; `full_range` selects full vs. studio-swing
|
||||
// de-quantization.
|
||||
|
||||
struct Nv12Params {
|
||||
col0: vec4<f32>,
|
||||
col1: vec4<f32>,
|
||||
col2: vec4<f32>,
|
||||
full_range: u32,
|
||||
_pad: vec3<u32>,
|
||||
}
|
||||
|
||||
@group(0) @binding(0) var y_tex: texture_2d<f32>;
|
||||
@group(0) @binding(1) var samp: sampler;
|
||||
@group(0) @binding(2) var<uniform> params: Nv12Params;
|
||||
@group(0) @binding(3) var uv_tex: texture_2d<f32>;
|
||||
|
||||
struct VertexOutput {
|
||||
@builtin(position) position: vec4<f32>,
|
||||
@location(0) uv: vec2<f32>,
|
||||
}
|
||||
|
||||
@vertex
|
||||
fn vs_main(@builtin(vertex_index) vertex_index: u32) -> VertexOutput {
|
||||
var out: VertexOutput;
|
||||
let x = f32((vertex_index & 1u) << 1u);
|
||||
let y = f32(vertex_index & 2u);
|
||||
out.position = vec4<f32>(x * 2.0 - 1.0, 1.0 - y * 2.0, 0.0, 1.0);
|
||||
out.uv = vec2<f32>(x, y);
|
||||
return out;
|
||||
}
|
||||
|
||||
fn srgb_to_linear(c: vec3<f32>) -> vec3<f32> {
|
||||
let lo = c / 12.92;
|
||||
let hi = pow((c + vec3<f32>(0.055)) / 1.055, vec3<f32>(2.4));
|
||||
return select(lo, hi, c > vec3<f32>(0.04045));
|
||||
}
|
||||
|
||||
@fragment
|
||||
fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
|
||||
let m = mat3x3<f32>(params.col0.xyz, params.col1.xyz, params.col2.xyz);
|
||||
let frame_uv = (m * vec3<f32>(in.uv.x, in.uv.y, 1.0)).xy;
|
||||
|
||||
if frame_uv.x < 0.0 || frame_uv.x > 1.0
|
||||
|| frame_uv.y < 0.0 || frame_uv.y > 1.0 {
|
||||
return vec4<f32>(0.0, 0.0, 0.0, 0.0);
|
||||
}
|
||||
|
||||
let yv = textureSample(y_tex, samp, frame_uv).r;
|
||||
let cbcr = textureSample(uv_tex, samp, frame_uv).rg;
|
||||
|
||||
var Y: f32;
|
||||
var Cb: f32;
|
||||
var Cr: f32;
|
||||
if params.full_range != 0u {
|
||||
// Full ("JPEG") range: [0,255] luma, chroma centered at 128.
|
||||
Y = yv;
|
||||
Cb = cbcr.r - 0.5;
|
||||
Cr = cbcr.g - 0.5;
|
||||
} else {
|
||||
// Studio swing: Y'∈[16,235], Cb/Cr∈[16,240].
|
||||
Y = (yv * 255.0 - 16.0) / 219.0;
|
||||
Cb = (cbcr.r * 255.0 - 128.0) / 224.0;
|
||||
Cr = (cbcr.g * 255.0 - 128.0) / 224.0;
|
||||
}
|
||||
|
||||
// BT.709 Y'CbCr → gamma-encoded R'G'B'.
|
||||
let r = Y + 1.5748 * Cr;
|
||||
let g = Y - 0.1873 * Cb - 0.4681 * Cr;
|
||||
let b = Y + 1.8556 * Cb;
|
||||
let rgb_gamma = clamp(vec3<f32>(r, g, b), vec3<f32>(0.0), vec3<f32>(1.0));
|
||||
|
||||
// R'G'B' is gamma-encoded; the HDR target is linear → undo the transfer.
|
||||
let rgb_lin = srgb_to_linear(rgb_gamma);
|
||||
return vec4<f32>(rgb_lin, 1.0);
|
||||
}
|
||||
@@ -152,6 +152,8 @@ struct SharedVelloResources {
|
||||
gpu_brush: Mutex<crate::gpu_brush::GpuBrushEngine>,
|
||||
/// Canvas blit pipeline (renders GPU canvas to layer sRGB buffer)
|
||||
canvas_blit: crate::gpu_brush::CanvasBlitPipeline,
|
||||
/// NV12→linear blit for hardware-decoded video frames (GPU plane textures → HDR layer).
|
||||
nv12_blit: crate::nv12_blit::Nv12BlitPipeline,
|
||||
/// True when Vello is running its CPU software renderer (either forced or GPU fallback).
|
||||
/// Used to select cheaper antialiasing — Msaa16 on CPU costs 16× as much as Area.
|
||||
is_cpu_renderer: bool,
|
||||
@@ -372,6 +374,7 @@ impl SharedVelloResources {
|
||||
// Initialize GPU raster brush engine
|
||||
let gpu_brush = crate::gpu_brush::GpuBrushEngine::new(device);
|
||||
let canvas_blit = crate::gpu_brush::CanvasBlitPipeline::new(device);
|
||||
let nv12_blit = crate::nv12_blit::Nv12BlitPipeline::new(device);
|
||||
|
||||
println!("✅ Vello shared resources initialized (renderer, shaders, HDR compositor, effect processor, color converter, and GPU brush engine)");
|
||||
|
||||
@@ -389,6 +392,7 @@ impl SharedVelloResources {
|
||||
srgb_to_linear,
|
||||
gpu_brush: Mutex::new(gpu_brush),
|
||||
canvas_blit,
|
||||
nv12_blit,
|
||||
is_cpu_renderer: use_cpu || is_cpu_renderer,
|
||||
gpu_timer: Mutex::new(None),
|
||||
video_frame_cache: Mutex::new(VideoFrameTexCache::new()),
|
||||
@@ -1062,6 +1066,12 @@ impl egui_wgpu::CallbackTrait for VelloCallback {
|
||||
// Let the cache page image bytes from the project container on a decode miss.
|
||||
image_cache.set_container_path(self.ctx.container_path.clone());
|
||||
|
||||
// Preview composites on the shared device, so it can consume hardware-decoded GPU
|
||||
// frames — but only the GPU renderer; the CPU fallback needs software frames.
|
||||
if let Ok(mut vm) = shared.video_manager.lock() {
|
||||
vm.set_render_hardware_ok(!shared.is_cpu_renderer);
|
||||
}
|
||||
|
||||
let composite_result = if shared.is_cpu_renderer {
|
||||
lightningbeam_core::renderer::render_document_for_compositing_cpu(
|
||||
&self.ctx.document,
|
||||
@@ -1678,25 +1688,33 @@ impl egui_wgpu::CallbackTrait for VelloCallback {
|
||||
RenderedLayerType::Video { instances } => {
|
||||
// Video layer — per-instance: (cached) frame texture → blit → composite.
|
||||
for inst in instances {
|
||||
if inst.rgba_data.is_empty() { continue; }
|
||||
if inst.gpu.is_none() && inst.rgba_data.is_empty() { continue; }
|
||||
let hdr_layer_handle = buffer_pool.acquire(device, hdr_spec);
|
||||
if let (Some(hdr_layer_view), Some(hdr_view)) = (
|
||||
buffer_pool.get_view(hdr_layer_handle),
|
||||
&instance_resources.hdr_texture_view,
|
||||
) {
|
||||
// Reuse the GPU texture for this frame if it's unchanged (a
|
||||
// static/paused video → no CPU conversion, alloc, or upload).
|
||||
// Timed into `blit_ms` (incl the cache lookup + per-frame view).
|
||||
let _t = std::time::Instant::now();
|
||||
let tex_view = shared
|
||||
.video_frame_cache
|
||||
.lock()
|
||||
.unwrap()
|
||||
.texture_view(device, queue, &inst.rgba_data, inst.width, inst.height);
|
||||
let bt = crate::gpu_brush::BlitTransform::new(
|
||||
inst.transform, inst.width, inst.height, width, height,
|
||||
);
|
||||
shared.canvas_blit.blit_straight(device, queue, &tex_view, hdr_layer_view, &bt, None);
|
||||
if let Some(gpu) = &inst.gpu {
|
||||
// Hardware-decoded NV12 plane textures → linear RGB, no CPU upload.
|
||||
let y_view = gpu.y.create_view(&Default::default());
|
||||
let uv_view = gpu.uv.create_view(&Default::default());
|
||||
shared.nv12_blit.blit(
|
||||
device, queue, &y_view, &uv_view, hdr_layer_view, &bt, gpu.full_range,
|
||||
);
|
||||
} else {
|
||||
// Reuse the GPU texture for this frame if it's unchanged (a
|
||||
// static/paused video → no CPU conversion, alloc, or upload).
|
||||
let tex_view = shared
|
||||
.video_frame_cache
|
||||
.lock()
|
||||
.unwrap()
|
||||
.texture_view(device, queue, &inst.rgba_data, inst.width, inst.height);
|
||||
shared.canvas_blit.blit_straight(device, queue, &tex_view, hdr_layer_view, &bt, None);
|
||||
}
|
||||
cput.blit_ms += _t.elapsed().as_secs_f64() * 1000.0;
|
||||
|
||||
let compositor_layer = lightningbeam_core::gpu::CompositorLayer::new(
|
||||
|
||||
Reference in New Issue
Block a user