hdr: per-document Clip vs Highlight-rolloff output mode (Stage A pt 2)

Add a per-document HDR→SDR mapping applied at the final linear→sRGB encode, so
super-white (HDR) video highlights can be recovered instead of hard-clipped:

- core: HdrOutputMode {Clip (default), HighlightRolloff} on Document (serde
  default), with a SetDocumentPropertiesAction variant for undoable edits.
- shaders: a fs_main_rolloff entry point (preview linear_to_srgb.wgsl + the export
  inline shader) applying a C1 highlight knee — identity below 0.8, smooth rolloff
  [0.8,∞)→[0.8,1). SDR below the knee is untouched; Clip stays the historical path.
- preview (stage.rs) and both export encodes (video_exporter.rs) pick the pipeline
  variant from document.hdr_output_mode — one value per frame, so no per-pixel
  uniform; mirrors the existing fs_main_straight pattern.
- UI: an "HDR output" dropdown in the Document section of the info panel.

Default (Clip) is bit-identical to previous behaviour. Completes Stage A:
HDR-correct input (pt 1) + SDR-safe output mapping. HDR export (10-bit P010/PQ)
and HDR display remain Stages B/C.
This commit is contained in:
Skyler Lehmkuhl
2026-06-26 03:01:15 -04:00
parent ff490ab9ae
commit b76eefb404
6 changed files with 210 additions and 6 deletions
@@ -980,10 +980,10 @@ impl InfopanelPane {
// Extract all needed values up front, then drop the borrow before closures
// that need mutable access to shared or self.
let (mut width, mut height, mut duration, mut framerate, layer_count, background_color) = {
let (mut width, mut height, mut duration, mut framerate, layer_count, background_color, mut hdr_mode) = {
let document = shared.action_executor.document();
(document.width, document.height, document.duration, document.framerate,
document.root.children.len(), document.background_color)
document.root.children.len(), document.background_color, document.hdr_output_mode)
};
// Canvas width
@@ -1087,6 +1087,23 @@ impl InfopanelPane {
}
});
// HDR output mode (how super-white video highlights map to SDR output)
ui.horizontal(|ui| {
use lightningbeam_core::document::HdrOutputMode;
ui.label("HDR output:");
egui::ComboBox::from_id_salt(("hdr_output_mode", path))
.selected_text(hdr_mode.name())
.show_ui(ui, |ui| {
let mut changed = false;
changed |= ui.selectable_value(&mut hdr_mode, HdrOutputMode::Clip, HdrOutputMode::Clip.name()).changed();
changed |= ui.selectable_value(&mut hdr_mode, HdrOutputMode::HighlightRolloff, HdrOutputMode::HighlightRolloff.name()).changed();
if changed {
let action = SetDocumentPropertiesAction::set_hdr_output_mode(hdr_mode);
shared.pending_actions.push(Box::new(action));
}
});
});
// Layer count (read-only)
ui.horizontal(|ui| {
ui.label("Layers:");
@@ -50,3 +50,34 @@ fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
a
);
}
// Highlight rolloff: identity below the knee, then a smooth C1 rolloff that maps [knee, ∞) → [knee, 1)
// so super-white (HDR) detail is recovered instead of hard-clipped. SDR below the knee is untouched.
fn highlight_rolloff(x: f32) -> f32 {
let knee = 0.8;
if x <= knee {
return x;
}
let headroom = 1.0 - knee;
return knee + headroom * (1.0 - exp(-(x - knee) / headroom));
}
// Variant of fs_main with highlight rolloff (document HDR output mode = Highlight rolloff).
@fragment
fn fs_main_rolloff(in: VertexOutput) -> @location(0) vec4<f32> {
let linear = textureSample(input_tex, input_sampler, in.uv);
let a = linear.a;
let straight = select(linear.rgb / a, vec3<f32>(0.0), a <= 0.0);
let rolled = vec3<f32>(
highlight_rolloff(straight.r),
highlight_rolloff(straight.g),
highlight_rolloff(straight.b),
);
return vec4<f32>(
linear_to_srgb_channel(rolled.r),
linear_to_srgb_channel(rolled.g),
linear_to_srgb_channel(rolled.b),
a
);
}
@@ -135,6 +135,8 @@ struct SharedVelloResources {
blit_bind_group_layout: wgpu::BindGroupLayout,
/// HDR to sRGB blit pipeline (linear→sRGB conversion for display)
hdr_blit_pipeline: wgpu::RenderPipeline,
/// Variant of `hdr_blit_pipeline` with highlight rolloff (document HDR output mode).
hdr_blit_pipeline_rolloff: wgpu::RenderPipeline,
sampler: wgpu::Sampler,
/// Shared image cache for avoiding re-decoding images every frame
image_cache: Mutex<lightningbeam_core::renderer::ImageCache>,
@@ -346,6 +348,41 @@ impl SharedVelloResources {
cache: None,
});
// Variant with highlight rolloff (document HDR output mode); identical but `fs_main_rolloff`.
let hdr_blit_pipeline_rolloff = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
label: Some("hdr_blit_pipeline_rolloff"),
layout: Some(&pipeline_layout),
vertex: wgpu::VertexState {
module: &hdr_shader,
entry_point: Some("vs_main"),
buffers: &[],
compilation_options: Default::default(),
},
fragment: Some(wgpu::FragmentState {
module: &hdr_shader,
entry_point: Some("fs_main_rolloff"),
targets: &[Some(wgpu::ColorTargetState {
format: wgpu::TextureFormat::Rgba8Unorm,
blend: None,
write_mask: wgpu::ColorWrites::ALL,
})],
compilation_options: Default::default(),
}),
primitive: wgpu::PrimitiveState {
topology: wgpu::PrimitiveTopology::TriangleStrip,
strip_index_format: None,
front_face: wgpu::FrontFace::Ccw,
cull_mode: None,
unclipped_depth: false,
polygon_mode: wgpu::PolygonMode::Fill,
conservative: false,
},
depth_stencil: None,
multisample: wgpu::MultisampleState::default(),
multiview: None,
cache: None,
});
// Create sampler
let sampler = device.create_sampler(&wgpu::SamplerDescriptor {
label: Some("vello_blit_sampler"),
@@ -383,6 +420,7 @@ impl SharedVelloResources {
blit_pipeline,
blit_bind_group_layout,
hdr_blit_pipeline,
hdr_blit_pipeline_rolloff,
sampler,
image_cache: Mutex::new(lightningbeam_core::renderer::ImageCache::new()),
video_manager,
@@ -2947,7 +2985,11 @@ impl egui_wgpu::CallbackTrait for VelloCallback {
occlusion_query_set: None,
});
render_pass.set_pipeline(&shared.hdr_blit_pipeline);
let hdr_pipeline = match self.ctx.document.hdr_output_mode {
lightningbeam_core::document::HdrOutputMode::HighlightRolloff => &shared.hdr_blit_pipeline_rolloff,
lightningbeam_core::document::HdrOutputMode::Clip => &shared.hdr_blit_pipeline,
};
render_pass.set_pipeline(hdr_pipeline);
render_pass.set_bind_group(0, hdr_bind_group, &[]);
render_pass.draw(0..3, 0..1); // Full-screen triangle (3 vertices)
}