Merge branch 'rust-ui' of https://git.skyler.io/skyler/Lightningbeam into rust-ui

This commit is contained in:
Skyler Lehmkuhl
2026-03-09 13:41:48 -04:00
66 changed files with 13393 additions and 1439 deletions
@@ -32,22 +32,41 @@ impl CustomCursor {
/// Convert a Tool enum to the corresponding custom cursor
pub fn from_tool(tool: Tool) -> Self {
match tool {
Tool::Select => CustomCursor::Select,
Tool::Draw => CustomCursor::Draw,
Tool::Transform => CustomCursor::Transform,
Tool::Rectangle => CustomCursor::Rectangle,
Tool::Ellipse => CustomCursor::Ellipse,
Tool::PaintBucket => CustomCursor::PaintBucket,
Tool::Eyedropper => CustomCursor::Eyedropper,
Tool::Line => CustomCursor::Line,
Tool::Polygon => CustomCursor::Polygon,
Tool::BezierEdit => CustomCursor::BezierEdit,
Tool::Text => CustomCursor::Text,
Tool::RegionSelect => CustomCursor::Select, // Reuse select cursor for now
Tool::Split => CustomCursor::Select, // Reuse select cursor for now
Tool::Erase => CustomCursor::Draw, // Reuse draw cursor for raster erase
Tool::Smudge => CustomCursor::Draw, // Reuse draw cursor for raster smudge
Tool::SelectLasso => CustomCursor::Select, // Reuse select cursor for lasso
Tool::Select => CustomCursor::Select,
Tool::Draw => CustomCursor::Draw,
Tool::Transform => CustomCursor::Transform,
Tool::Rectangle => CustomCursor::Rectangle,
Tool::Ellipse => CustomCursor::Ellipse,
Tool::PaintBucket => CustomCursor::PaintBucket,
Tool::Eyedropper => CustomCursor::Eyedropper,
Tool::Line => CustomCursor::Line,
Tool::Polygon => CustomCursor::Polygon,
Tool::BezierEdit => CustomCursor::BezierEdit,
Tool::Text => CustomCursor::Text,
Tool::RegionSelect => CustomCursor::Select,
Tool::Split => CustomCursor::Select,
Tool::Erase => CustomCursor::Draw,
Tool::Smudge => CustomCursor::Draw,
Tool::SelectLasso => CustomCursor::Select,
// Raster brush tools — use draw cursor until implemented
Tool::Pencil
| Tool::Pen
| Tool::Airbrush
| Tool::CloneStamp
| Tool::HealingBrush
| Tool::PatternStamp
| Tool::DodgeBurn
| Tool::Sponge
| Tool::BlurSharpen => CustomCursor::Draw,
// Selection tools — use select cursor until implemented
Tool::SelectEllipse
| Tool::MagicWand
| Tool::QuickSelect => CustomCursor::Select,
// Other tools — use select cursor until implemented
Tool::Gradient
| Tool::CustomShape
| Tool::Warp
| Tool::Liquify => CustomCursor::Select,
}
}
@@ -5,9 +5,40 @@
use eframe::egui;
use std::collections::VecDeque;
use std::sync::{Mutex, OnceLock};
use std::time::{Duration, Instant};
const FRAME_HISTORY_SIZE: usize = 60; // Track last 60 frames for FPS stats
/// Timing breakdown for the GPU prepare() pass, written by the render thread.
#[derive(Debug, Clone, Default)]
pub struct PrepareTiming {
pub total_ms: f64,
pub removals_ms: f64,
pub gpu_dispatches_ms: f64,
pub scene_build_ms: f64,
pub composite_ms: f64,
}
static LAST_PREPARE_TIMING: OnceLock<Mutex<PrepareTiming>> = OnceLock::new();
/// Called from `VelloCallback::prepare()` every frame to update the timing snapshot.
pub fn update_prepare_timing(
total_ms: f64,
removals_ms: f64,
gpu_dispatches_ms: f64,
scene_build_ms: f64,
composite_ms: f64,
) {
let cell = LAST_PREPARE_TIMING.get_or_init(|| Mutex::new(PrepareTiming::default()));
if let Ok(mut t) = cell.lock() {
t.total_ms = total_ms;
t.removals_ms = removals_ms;
t.gpu_dispatches_ms = gpu_dispatches_ms;
t.scene_build_ms = scene_build_ms;
t.composite_ms = composite_ms;
}
}
const DEVICE_REFRESH_INTERVAL: Duration = Duration::from_secs(2); // Refresh devices every 2 seconds
const MEMORY_REFRESH_INTERVAL: Duration = Duration::from_millis(500); // Refresh memory every 500ms
@@ -28,6 +59,9 @@ pub struct DebugStats {
pub audio_input_devices: Vec<String>,
pub has_pointer: bool,
// GPU prepare() timing breakdown (from render thread)
pub prepare_timing: PrepareTiming,
// Performance metrics for each section
pub timing_memory_us: u64,
pub timing_gpu_us: u64,
@@ -170,6 +204,12 @@ impl DebugStatsCollector {
let timing_total_us = collection_start.elapsed().as_micros() as u64;
let prepare_timing = LAST_PREPARE_TIMING
.get()
.and_then(|m| m.lock().ok())
.map(|t| t.clone())
.unwrap_or_default();
DebugStats {
fps_current,
fps_min,
@@ -184,6 +224,7 @@ impl DebugStatsCollector {
midi_devices,
audio_input_devices,
has_pointer,
prepare_timing,
timing_memory_us,
timing_gpu_us,
timing_midi_us,
@@ -231,6 +272,16 @@ pub fn render_debug_overlay(ctx: &egui::Context, stats: &DebugStats) {
ui.add_space(8.0);
// GPU prepare() timing section
let pt = &stats.prepare_timing;
ui.colored_label(egui::Color32::YELLOW, format!("GPU prepare: {:.2} ms", pt.total_ms));
ui.label(format!(" removals: {:.2} ms", pt.removals_ms));
ui.label(format!(" gpu_dispatch: {:.2} ms", pt.gpu_dispatches_ms));
ui.label(format!(" scene_build: {:.2} ms", pt.scene_build_ms));
ui.label(format!(" composite: {:.2} ms", pt.composite_ms));
ui.add_space(8.0);
// Memory section with timing
ui.colored_label(egui::Color32::YELLOW, format!("Memory: ({}µs)", stats.timing_memory_us));
ui.label(format!("Physical: {} MB", stats.memory_physical_mb));
@@ -3,13 +3,29 @@
//! Provides a user interface for configuring and starting audio/video exports.
use eframe::egui;
use lightningbeam_core::export::{AudioExportSettings, AudioFormat, VideoExportSettings, VideoCodec, VideoQuality};
use lightningbeam_core::export::{
AudioExportSettings, AudioFormat,
ImageExportSettings, ImageFormat,
VideoExportSettings, VideoCodec, VideoQuality,
};
use std::path::PathBuf;
/// Hint about document content, used to pick a smart default export type.
pub struct DocumentHint {
pub has_video: bool,
pub has_audio: bool,
pub has_raster: bool,
pub has_vector: bool,
pub current_time: f64,
pub doc_width: u32,
pub doc_height: u32,
}
/// Export type selection
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ExportType {
Audio,
Image,
Video,
}
@@ -17,6 +33,7 @@ pub enum ExportType {
#[derive(Debug, Clone)]
pub enum ExportResult {
AudioOnly(AudioExportSettings, PathBuf),
Image(ImageExportSettings, PathBuf),
VideoOnly(VideoExportSettings, PathBuf),
VideoWithAudio(VideoExportSettings, AudioExportSettings, PathBuf),
}
@@ -32,6 +49,9 @@ pub struct ExportDialog {
/// Audio export settings
pub audio_settings: AudioExportSettings,
/// Image export settings
pub image_settings: ImageExportSettings,
/// Video export settings
pub video_settings: VideoExportSettings,
@@ -55,6 +75,15 @@ pub struct ExportDialog {
/// Output directory
pub output_dir: PathBuf,
/// Project name from the last `open()` call — used to detect file switches.
current_project: String,
/// Export type used the last time the user actually clicked Export for `current_project`.
last_export_type: Option<ExportType>,
/// Full path of the most recent successful export. Restored as the default on next open.
last_exported_path: Option<PathBuf>,
}
impl Default for ExportDialog {
@@ -71,6 +100,7 @@ impl Default for ExportDialog {
open: false,
export_type: ExportType::Audio,
audio_settings: AudioExportSettings::standard_mp3(),
image_settings: ImageExportSettings::default(),
video_settings: VideoExportSettings::default(),
include_audio: true,
output_path: None,
@@ -78,23 +108,56 @@ impl Default for ExportDialog {
show_advanced: false,
selected_video_preset: 0,
output_filename: String::new(),
current_project: String::new(),
last_export_type: None,
last_exported_path: None,
output_dir: music_dir,
}
}
}
impl ExportDialog {
/// Open the dialog with default settings
pub fn open(&mut self, timeline_duration: f64, project_name: &str) {
/// Open the dialog with default settings, using `hint` to pick a smart default tab.
pub fn open(&mut self, timeline_duration: f64, project_name: &str, hint: &DocumentHint) {
self.open = true;
self.audio_settings.end_time = timeline_duration;
self.video_settings.end_time = timeline_duration;
self.image_settings.time = hint.current_time;
// Propagate document dimensions as defaults (None means "use doc size").
self.image_settings.width = None;
self.image_settings.height = None;
self.error_message = None;
// Pre-populate filename from project name if not already set
if self.output_filename.is_empty() || !self.output_filename.contains(project_name) {
let ext = self.audio_settings.format.extension();
self.output_filename = format!("{}.{}", project_name, ext);
// Determine export type: prefer the type used last time for this file,
// then fall back to document-content hints.
let same_project = self.current_project == project_name;
self.export_type = if same_project && self.last_export_type.is_some() {
self.last_export_type.unwrap()
} else {
let only_audio = hint.has_audio && !hint.has_video && !hint.has_raster && !hint.has_vector;
let only_raster = hint.has_raster && !hint.has_video && !hint.has_audio && !hint.has_vector;
if hint.has_video { ExportType::Video }
else if only_audio { ExportType::Audio }
else if only_raster { ExportType::Image }
else { self.export_type } // keep current as fallback
};
self.current_project = project_name.to_owned();
// Restore the last exported path if available; otherwise default to project name.
if let Some(ref last) = self.last_exported_path.clone() {
if let Some(dir) = last.parent() { self.output_dir = dir.to_path_buf(); }
if let Some(name) = last.file_name() { self.output_filename = name.to_string_lossy().into_owned(); }
} else if self.output_filename.is_empty() || !self.output_filename.contains(project_name) {
self.output_filename = format!("{}.{}", project_name, self.current_extension());
}
}
/// Extension for the currently selected export type.
fn current_extension(&self) -> &'static str {
match self.export_type {
ExportType::Audio => self.audio_settings.format.extension(),
ExportType::Image => self.image_settings.format.extension(),
ExportType::Video => self.video_settings.codec.container_format(),
}
}
@@ -106,10 +169,7 @@ impl ExportDialog {
/// Update the filename extension to match the current format
fn update_filename_extension(&mut self) {
let ext = match self.export_type {
ExportType::Audio => self.audio_settings.format.extension(),
ExportType::Video => self.video_settings.codec.container_format(),
};
let ext = self.current_extension();
// Replace extension in filename
if let Some(dot_pos) = self.output_filename.rfind('.') {
self.output_filename.truncate(dot_pos + 1);
@@ -138,6 +198,7 @@ impl ExportDialog {
let window_title = match self.export_type {
ExportType::Audio => "Export Audio",
ExportType::Image => "Export Image",
ExportType::Video => "Export Video",
};
@@ -156,11 +217,14 @@ impl ExportDialog {
// Export type selection (tabs)
ui.horizontal(|ui| {
if ui.selectable_value(&mut self.export_type, ExportType::Audio, "Audio").clicked() {
self.update_filename_extension();
}
if ui.selectable_value(&mut self.export_type, ExportType::Video, "Video").clicked() {
self.update_filename_extension();
for (variant, label) in [
(ExportType::Audio, "Audio"),
(ExportType::Image, "Image"),
(ExportType::Video, "Video"),
] {
if ui.selectable_value(&mut self.export_type, variant, label).clicked() {
self.update_filename_extension();
}
}
});
@@ -171,6 +235,7 @@ impl ExportDialog {
// Basic settings
match self.export_type {
ExportType::Audio => self.render_audio_basic(ui),
ExportType::Image => self.render_image_settings(ui),
ExportType::Video => self.render_video_basic(ui),
}
@@ -188,6 +253,7 @@ impl ExportDialog {
ui.add_space(8.0);
match self.export_type {
ExportType::Audio => self.render_audio_advanced(ui),
ExportType::Image => self.render_image_advanced(ui),
ExportType::Video => self.render_video_advanced(ui),
}
}
@@ -260,6 +326,62 @@ impl ExportDialog {
});
}
/// Render basic image export settings (format, quality, transparency).
fn render_image_settings(&mut self, ui: &mut egui::Ui) {
// Format
ui.horizontal(|ui| {
ui.label("Format:");
let prev = self.image_settings.format;
egui::ComboBox::from_id_salt("image_format")
.selected_text(self.image_settings.format.name())
.show_ui(ui, |ui| {
ui.selectable_value(&mut self.image_settings.format, ImageFormat::Png, "PNG");
ui.selectable_value(&mut self.image_settings.format, ImageFormat::Jpeg, "JPEG");
ui.selectable_value(&mut self.image_settings.format, ImageFormat::WebP, "WebP");
});
if self.image_settings.format != prev {
self.update_filename_extension();
}
});
// Quality (JPEG / WebP only)
if self.image_settings.format.has_quality() {
ui.horizontal(|ui| {
ui.label("Quality:");
ui.add(egui::Slider::new(&mut self.image_settings.quality, 1..=100));
});
}
// Transparency (PNG / WebP only — JPEG has no alpha)
if self.image_settings.format != ImageFormat::Jpeg {
ui.checkbox(&mut self.image_settings.allow_transparency, "Allow transparency");
}
}
/// Render advanced image export settings (time, resolution override).
fn render_image_advanced(&mut self, ui: &mut egui::Ui) {
// Time (which frame to export)
ui.horizontal(|ui| {
ui.label("Time:");
ui.add(egui::DragValue::new(&mut self.image_settings.time)
.speed(0.01)
.range(0.0..=f64::MAX)
.suffix(" s"));
});
// Resolution override (None = use document size; 0 means "use doc size")
ui.horizontal(|ui| {
ui.label("Size:");
let mut w = self.image_settings.width.unwrap_or(0);
let mut h = self.image_settings.height.unwrap_or(0);
let changed_w = ui.add(egui::DragValue::new(&mut w).range(0..=u32::MAX).prefix("W ")).changed();
let changed_h = ui.add(egui::DragValue::new(&mut h).range(0..=u32::MAX).prefix("H ")).changed();
if changed_w { self.image_settings.width = if w == 0 { None } else { Some(w) }; }
if changed_h { self.image_settings.height = if h == 0 { None } else { Some(h) }; }
ui.weak("(0 = document size)");
});
}
/// Render advanced audio settings (sample rate, channels, bit depth, bitrate, time range)
fn render_audio_advanced(&mut self, ui: &mut egui::Ui) {
ui.horizontal(|ui| {
@@ -419,6 +541,7 @@ impl ExportDialog {
fn render_time_range(&mut self, ui: &mut egui::Ui) {
let (start_time, end_time) = match self.export_type {
ExportType::Audio => (&mut self.audio_settings.start_time, &mut self.audio_settings.end_time),
ExportType::Image => return, // image uses a single time field, not a range
ExportType::Video => (&mut self.video_settings.start_time, &mut self.video_settings.end_time),
};
@@ -440,26 +563,35 @@ impl ExportDialog {
ui.label(format!("Duration: {:.2} seconds", duration));
}
/// Render output file selection UI
/// Render output file selection UI — single OS save-file dialog.
fn render_output_selection(&mut self, ui: &mut egui::Ui) {
ui.horizontal(|ui| {
// Show the current path (truncated if long).
let full_path = self.build_output_path();
let path_str = full_path.display().to_string();
ui.label("Save to:");
let dir_text = self.output_dir.display().to_string();
ui.label(&dir_text);
if ui.button("Change...").clicked() {
if let Some(dir) = rfd::FileDialog::new()
.set_directory(&self.output_dir)
.pick_folder()
{
self.output_dir = dir;
}
}
ui.add(egui::Label::new(
egui::RichText::new(&path_str).weak()
).truncate());
});
ui.horizontal(|ui| {
ui.label("Filename:");
ui.text_edit_singleline(&mut self.output_filename);
});
if ui.button("Choose location...").clicked() {
let ext = self.current_extension();
let mut dialog = rfd::FileDialog::new()
.set_directory(&self.output_dir)
.set_file_name(&self.output_filename)
.add_filter(ext.to_uppercase(), &[ext]);
if let Some(path) = dialog.save_file() {
if let Some(dir) = path.parent() {
self.output_dir = dir.to_path_buf();
}
if let Some(name) = path.file_name() {
self.output_filename = name.to_string_lossy().into_owned();
// Ensure the extension matches the selected format.
self.update_filename_extension();
}
}
}
}
/// Handle export button click
@@ -471,7 +603,18 @@ impl ExportDialog {
let output_path = self.output_path.clone().unwrap();
// Remember this export type and path for next time the dialog is opened.
self.last_export_type = Some(self.export_type);
self.last_exported_path = Some(output_path.clone());
let result = match self.export_type {
ExportType::Image => {
if let Err(err) = self.image_settings.validate() {
self.error_message = Some(err);
return None;
}
Some(ExportResult::Image(self.image_settings.clone(), output_path))
}
ExportType::Audio => {
// Validate audio settings
if let Err(err) = self.audio_settings.validate() {
@@ -0,0 +1,70 @@
//! Image encoding — save raw RGBA bytes as PNG / JPEG / WebP.
use lightningbeam_core::export::ImageFormat;
use std::path::Path;
/// Encode `pixels` (raw RGBA8, top-left origin) and write to `path`.
///
/// * `allow_transparency` — when true the alpha channel is preserved (PNG/WebP);
/// when false each pixel is composited onto black before encoding.
pub fn save_rgba_image(
pixels: &[u8],
width: u32,
height: u32,
format: ImageFormat,
quality: u8,
allow_transparency: bool,
path: &Path,
) -> Result<(), String> {
use image::{ImageBuffer, Rgba};
let img = ImageBuffer::<Rgba<u8>, _>::from_raw(width, height, pixels.to_vec())
.ok_or_else(|| "Pixel buffer size mismatch".to_string())?;
match format {
ImageFormat::Png => {
if allow_transparency {
img.save(path).map_err(|e| format!("PNG save failed: {e}"))
} else {
let flat = flatten_alpha(img);
flat.save(path).map_err(|e| format!("PNG save failed: {e}"))
}
}
ImageFormat::Jpeg => {
use image::codecs::jpeg::JpegEncoder;
use image::DynamicImage;
use std::fs::File;
use std::io::BufWriter;
// Flatten alpha onto black before JPEG encoding (JPEG has no alpha).
let flat = flatten_alpha(img);
let rgb_img = DynamicImage::ImageRgb8(flat).to_rgb8();
let file = File::create(path).map_err(|e| format!("Cannot create file: {e}"))?;
let writer = BufWriter::new(file);
let mut encoder = JpegEncoder::new_with_quality(writer, quality);
encoder.encode_image(&rgb_img).map_err(|e| format!("JPEG encode failed: {e}"))
}
ImageFormat::WebP => {
if allow_transparency {
img.save(path).map_err(|e| format!("WebP save failed: {e}"))
} else {
let flat = flatten_alpha(img);
flat.save(path).map_err(|e| format!("WebP save failed: {e}"))
}
}
}
}
/// Composite RGBA pixels onto an opaque black background, returning an RGB image.
fn flatten_alpha(img: image::ImageBuffer<image::Rgba<u8>, Vec<u8>>) -> image::ImageBuffer<image::Rgb<u8>, Vec<u8>> {
use image::{ImageBuffer, Rgb};
ImageBuffer::from_fn(img.width(), img.height(), |x, y| {
let p = img.get_pixel(x, y);
let a = p[3] as f32 / 255.0;
Rgb([
(p[0] as f32 * a) as u8,
(p[1] as f32 * a) as u8,
(p[2] as f32 * a) as u8,
])
})
}
@@ -5,12 +5,13 @@
pub mod audio_exporter;
pub mod dialog;
pub mod image_exporter;
pub mod video_exporter;
pub mod readback_pipeline;
pub mod perf_metrics;
pub mod cpu_yuv_converter;
use lightningbeam_core::export::{AudioExportSettings, VideoExportSettings, ExportProgress};
use lightningbeam_core::export::{AudioExportSettings, ImageExportSettings, VideoExportSettings, ExportProgress};
use lightningbeam_core::document::Document;
use lightningbeam_core::renderer::ImageCache;
use lightningbeam_core::video::VideoManager;
@@ -66,6 +67,25 @@ pub struct VideoExportState {
perf_metrics: Option<perf_metrics::ExportMetrics>,
}
/// State for a single-frame image export (runs on the GPU render thread, one frame per update).
pub struct ImageExportState {
pub settings: ImageExportSettings,
pub output_path: PathBuf,
/// Resolved pixel dimensions (after applying any width/height overrides).
pub width: u32,
pub height: u32,
/// True once rendering has been submitted; the next call reads back and encodes.
pub rendered: bool,
/// GPU resources allocated on the first render call.
pub gpu_resources: Option<video_exporter::ExportGpuResources>,
/// Output RGBA texture — kept separate from gpu_resources to avoid split-borrow issues.
pub output_texture: Option<wgpu::Texture>,
/// View for output_texture.
pub output_texture_view: Option<wgpu::TextureView>,
/// Staging buffer for synchronous GPU→CPU readback.
pub staging_buffer: Option<wgpu::Buffer>,
}
/// Export orchestrator that manages the export process
pub struct ExportOrchestrator {
/// Channel for receiving progress updates (video or audio-only export)
@@ -82,6 +102,9 @@ pub struct ExportOrchestrator {
/// Parallel audio+video export state
parallel_export: Option<ParallelExportState>,
/// Single-frame image export state
image_state: Option<ImageExportState>,
}
/// State for parallel audio+video export
@@ -115,6 +138,7 @@ impl ExportOrchestrator {
cancel_flag: Arc::new(AtomicBool::new(false)),
video_state: None,
parallel_export: None,
image_state: None,
}
}
@@ -446,12 +470,8 @@ impl ExportOrchestrator {
/// Check if an export is in progress
pub fn is_exporting(&self) -> bool {
// Check parallel export first
if self.parallel_export.is_some() {
return true;
}
// Check single export
if self.parallel_export.is_some() { return true; }
if self.image_state.is_some() { return true; }
if let Some(handle) = &self.thread_handle {
!handle.is_finished()
} else {
@@ -459,6 +479,171 @@ impl ExportOrchestrator {
}
}
/// Enqueue a single-frame image export. Call `render_image_frame()` from the
/// egui update loop (where the wgpu device/queue are available) to complete it.
pub fn start_image_export(
&mut self,
settings: ImageExportSettings,
output_path: PathBuf,
doc_width: u32,
doc_height: u32,
) {
self.cancel_flag.store(false, Ordering::Relaxed);
let width = settings.width.unwrap_or(doc_width).max(1);
let height = settings.height.unwrap_or(doc_height).max(1);
self.image_state = Some(ImageExportState {
settings,
output_path,
width,
height,
rendered: false,
gpu_resources: None,
output_texture: None,
output_texture_view: None,
staging_buffer: None,
});
}
/// Drive the single-frame image export. Returns `Ok(true)` when done (success or
/// cancelled), `Ok(false)` if another call is needed next frame.
pub fn render_image_frame(
&mut self,
document: &mut Document,
device: &wgpu::Device,
queue: &wgpu::Queue,
renderer: &mut vello::Renderer,
image_cache: &mut ImageCache,
video_manager: &Arc<std::sync::Mutex<VideoManager>>,
floating_selection: Option<&lightningbeam_core::selection::RasterFloatingSelection>,
) -> Result<bool, String> {
if self.cancel_flag.load(Ordering::Relaxed) {
self.image_state = None;
return Ok(true);
}
let state = match self.image_state.as_mut() {
Some(s) => s,
None => return Ok(true),
};
if !state.rendered {
// ── First call: render the frame to the GPU output texture ────────
let w = state.width;
let h = state.height;
if state.gpu_resources.is_none() {
state.gpu_resources = Some(video_exporter::ExportGpuResources::new(device, w, h));
}
if state.output_texture.is_none() {
let tex = device.create_texture(&wgpu::TextureDescriptor {
label: Some("image_export_output"),
size: wgpu::Extent3d { width: w, height: h, depth_or_array_layers: 1 },
mip_level_count: 1,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format: wgpu::TextureFormat::Rgba8Unorm,
usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::COPY_SRC,
view_formats: &[],
});
state.output_texture_view = Some(tex.create_view(&wgpu::TextureViewDescriptor::default()));
state.output_texture = Some(tex);
}
// Borrow separately to avoid a split-borrow conflict (gpu mutably, view immutably).
let gpu = state.gpu_resources.as_mut().unwrap();
let output_view = state.output_texture_view.as_ref().unwrap();
let mut encoder = video_exporter::render_frame_to_gpu_rgba(
document,
state.settings.time,
w, h,
device, queue, renderer, image_cache, video_manager,
gpu,
output_view,
floating_selection,
state.settings.allow_transparency,
)?;
queue.submit(Some(encoder.finish()));
// Create a staging buffer for synchronous readback.
// wgpu requires bytes_per_row to be a multiple of 256.
let align = wgpu::COPY_BYTES_PER_ROW_ALIGNMENT;
let bytes_per_row = (w * 4 + align - 1) / align * align;
let staging = device.create_buffer(&wgpu::BufferDescriptor {
label: Some("image_export_staging"),
size: (bytes_per_row * h) as u64,
usage: wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::MAP_READ,
mapped_at_creation: false,
});
let mut copy_enc = device.create_command_encoder(&wgpu::CommandEncoderDescriptor {
label: Some("image_export_copy"),
});
let output_tex = state.output_texture.as_ref().unwrap();
copy_enc.copy_texture_to_buffer(
wgpu::TexelCopyTextureInfo {
texture: output_tex,
mip_level: 0,
origin: wgpu::Origin3d::ZERO,
aspect: wgpu::TextureAspect::All,
},
wgpu::TexelCopyBufferInfo {
buffer: &staging,
layout: wgpu::TexelCopyBufferLayout {
offset: 0,
bytes_per_row: Some(bytes_per_row),
rows_per_image: Some(h),
},
},
wgpu::Extent3d { width: w, height: h, depth_or_array_layers: 1 },
);
queue.submit(Some(copy_enc.finish()));
state.staging_buffer = Some(staging);
state.rendered = true;
return Ok(false); // Come back next frame to read the result.
}
// ── Second call: map the staging buffer, encode, and save ─────────────
let staging = match state.staging_buffer.as_ref() {
Some(b) => b,
None => { self.image_state = None; return Ok(true); }
};
// Map synchronously.
let slice = staging.slice(..);
slice.map_async(wgpu::MapMode::Read, |_| {});
let _ = device.poll(wgpu::PollType::wait_indefinitely());
let w = state.width;
let h = state.height;
let align = wgpu::COPY_BYTES_PER_ROW_ALIGNMENT;
let bytes_per_row = (w * 4 + align - 1) / align * align;
let pixels: Vec<u8> = {
let mapped = slice.get_mapped_range();
// Strip row padding: copy only w*4 bytes from each bytes_per_row-wide row.
let mut out = Vec::with_capacity((w * h * 4) as usize);
for row in 0..h {
let start = (row * bytes_per_row) as usize;
out.extend_from_slice(&mapped[start..start + (w * 4) as usize]);
}
out
};
staging.unmap();
let result = image_exporter::save_rgba_image(
&pixels, w, h,
state.settings.format,
state.settings.quality,
state.settings.allow_transparency,
&state.output_path,
);
self.image_state = None;
result.map(|_| true)
}
/// Wait for the export to complete
///
/// This blocks until the export thread finishes.
@@ -924,6 +1109,8 @@ impl ExportOrchestrator {
document, timestamp, width, height,
device, queue, renderer, image_cache, video_manager,
gpu_resources, &acquired.rgba_texture_view,
None, // No floating selection during video export
false, // Video export is never transparent
)?;
let render_end = Instant::now();
@@ -79,6 +79,10 @@ pub struct ExportGpuResources {
pub linear_to_srgb_bind_group_layout: wgpu::BindGroupLayout,
/// Sampler for linear to sRGB conversion
pub linear_to_srgb_sampler: wgpu::Sampler,
/// Canvas blit pipeline for raster/video/float layers (bypasses Vello).
pub canvas_blit: crate::gpu_brush::CanvasBlitPipeline,
/// Per-keyframe GPU texture cache for raster layers during export.
pub raster_cache: std::collections::HashMap<uuid::Uuid, crate::gpu_brush::CanvasPair>,
}
impl ExportGpuResources {
@@ -235,6 +239,8 @@ impl ExportGpuResources {
..Default::default()
});
let canvas_blit = crate::gpu_brush::CanvasBlitPipeline::new(device);
Self {
buffer_pool,
compositor,
@@ -251,6 +257,8 @@ impl ExportGpuResources {
linear_to_srgb_pipeline,
linear_to_srgb_bind_group_layout,
linear_to_srgb_sampler,
canvas_blit,
raster_cache: std::collections::HashMap::new(),
}
}
@@ -702,6 +710,233 @@ pub fn render_frame_to_rgba(
Ok(())
}
/// Composite all layers from `composite_result` into `gpu_resources.hdr_texture_view`.
///
/// Shared by both export functions. Handles every layer type:
/// - Vector/Group: Vello scene → sRGB → linear → composite
/// - Raster: upload pixels to `raster_cache` (if needed) → GPU blit → composite
/// - Video: sRGB straight-alpha → linear premultiplied → transient GPU texture → blit → composite
/// - Float: sRGB-premultiplied → linear → transient GPU texture → blit → composite
/// - Effect: apply post-process on the HDR accumulator
fn composite_document_to_hdr(
composite_result: &lightningbeam_core::renderer::CompositeRenderResult,
document: &Document,
device: &wgpu::Device,
queue: &wgpu::Queue,
renderer: &mut vello::Renderer,
gpu_resources: &mut ExportGpuResources,
width: u32,
height: u32,
allow_transparency: bool,
) -> Result<(), String> {
use vello::kurbo::Affine;
let layer_spec = BufferSpec::new(width, height, BufferFormat::Rgba8Srgb);
let hdr_spec = BufferSpec::new(width, height, BufferFormat::Rgba16Float);
let layer_render_params = vello::RenderParams {
base_color: vello::peniko::Color::TRANSPARENT,
width, height,
antialiasing_method: vello::AaConfig::Area,
};
// --- Background ---
let bg_srgb = gpu_resources.buffer_pool.acquire(device, layer_spec);
let bg_hdr = gpu_resources.buffer_pool.acquire(device, hdr_spec);
if let (Some(bg_srgb_view), Some(bg_hdr_view)) = (
gpu_resources.buffer_pool.get_view(bg_srgb),
gpu_resources.buffer_pool.get_view(bg_hdr),
) {
renderer.render_to_texture(device, queue, &composite_result.background, bg_srgb_view, &layer_render_params)
.map_err(|e| format!("Failed to render background: {e}"))?;
let mut enc = device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("export_bg_srgb_to_linear") });
gpu_resources.srgb_to_linear.convert(device, &mut enc, bg_srgb_view, bg_hdr_view);
queue.submit(Some(enc.finish()));
let bg_layer = CompositorLayer::normal(bg_hdr, 1.0);
let mut enc = device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("export_bg_composite") });
// When transparency is allowed, start from transparent black so the background's
// native alpha is preserved. Otherwise force an opaque black underlay.
let clear = if allow_transparency { [0.0, 0.0, 0.0, 0.0] } else { [0.0, 0.0, 0.0, 1.0] };
gpu_resources.compositor.composite(device, queue, &mut enc, &[bg_layer],
&gpu_resources.buffer_pool, &gpu_resources.hdr_texture_view, Some(clear));
queue.submit(Some(enc.finish()));
}
gpu_resources.buffer_pool.release(bg_srgb);
gpu_resources.buffer_pool.release(bg_hdr);
// --- Layers ---
for rendered_layer in &composite_result.layers {
if !rendered_layer.has_content { continue; }
match &rendered_layer.layer_type {
RenderedLayerType::Vector => {
let srgb_handle = gpu_resources.buffer_pool.acquire(device, layer_spec);
let hdr_layer_handle = gpu_resources.buffer_pool.acquire(device, hdr_spec);
if let (Some(srgb_view), Some(hdr_layer_view)) = (
gpu_resources.buffer_pool.get_view(srgb_handle),
gpu_resources.buffer_pool.get_view(hdr_layer_handle),
) {
renderer.render_to_texture(device, queue, &rendered_layer.scene, srgb_view, &layer_render_params)
.map_err(|e| format!("Failed to render layer: {e}"))?;
let mut enc = device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("export_layer_srgb_to_linear") });
gpu_resources.srgb_to_linear.convert(device, &mut enc, srgb_view, hdr_layer_view);
queue.submit(Some(enc.finish()));
let compositor_layer = CompositorLayer::new(hdr_layer_handle, rendered_layer.opacity, rendered_layer.blend_mode);
let mut enc = device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("export_layer_composite") });
gpu_resources.compositor.composite(device, queue, &mut enc, &[compositor_layer], &gpu_resources.buffer_pool, &gpu_resources.hdr_texture_view, None);
queue.submit(Some(enc.finish()));
}
gpu_resources.buffer_pool.release(srgb_handle);
gpu_resources.buffer_pool.release(hdr_layer_handle);
}
RenderedLayerType::Raster { kf_id, width: cw, height: ch, transform: layer_transform, dirty: _ } => {
let raw_pixels = document.get_layer(&rendered_layer.layer_id)
.and_then(|l| match l {
lightningbeam_core::layer::AnyLayer::Raster(rl) => rl.keyframe_at(document.current_time),
_ => None,
})
.filter(|kf| !kf.raw_pixels.is_empty())
.map(|kf| kf.raw_pixels.clone());
if let Some(pixels) = raw_pixels {
if !gpu_resources.raster_cache.contains_key(kf_id) {
let canvas = crate::gpu_brush::CanvasPair::new(device, *cw, *ch);
canvas.upload(queue, &pixels);
gpu_resources.raster_cache.insert(*kf_id, canvas);
}
if let Some(canvas) = gpu_resources.raster_cache.get(kf_id) {
let hdr_layer_handle = gpu_resources.buffer_pool.acquire(device, hdr_spec);
if let Some(hdr_layer_view) = gpu_resources.buffer_pool.get_view(hdr_layer_handle) {
let bt = crate::gpu_brush::BlitTransform::new(*layer_transform, *cw, *ch, width, height);
gpu_resources.canvas_blit.blit(device, queue, canvas.src_view(), hdr_layer_view, &bt, None);
let compositor_layer = CompositorLayer::new(hdr_layer_handle, rendered_layer.opacity, rendered_layer.blend_mode);
let mut enc = device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("export_raster_composite") });
gpu_resources.compositor.composite(device, queue, &mut enc, &[compositor_layer], &gpu_resources.buffer_pool, &gpu_resources.hdr_texture_view, None);
queue.submit(Some(enc.finish()));
}
gpu_resources.buffer_pool.release(hdr_layer_handle);
}
}
}
RenderedLayerType::Video { instances } => {
for inst in instances {
if inst.rgba_data.is_empty() { continue; }
let hdr_layer_handle = gpu_resources.buffer_pool.acquire(device, hdr_spec);
if let Some(hdr_layer_view) = gpu_resources.buffer_pool.get_view(hdr_layer_handle) {
// sRGB straight-alpha → linear premultiplied
let linear: Vec<u8> = inst.rgba_data.chunks_exact(4).flat_map(|p| {
let a = p[3] as f32 / 255.0;
let lin = |c: u8| -> f32 {
let f = c as f32 / 255.0;
if f <= 0.04045 { f / 12.92 } else { ((f + 0.055) / 1.055).powf(2.4) }
};
let r = (lin(p[0]) * a * 255.0 + 0.5) as u8;
let g = (lin(p[1]) * a * 255.0 + 0.5) as u8;
let b = (lin(p[2]) * a * 255.0 + 0.5) as u8;
[r, g, b, p[3]]
}).collect();
let tex = upload_transient_texture(device, queue, &linear, inst.width, inst.height, Some("export_video_frame_tex"));
let tex_view = tex.create_view(&Default::default());
let bt = crate::gpu_brush::BlitTransform::new(inst.transform, inst.width, inst.height, width, height);
gpu_resources.canvas_blit.blit(device, queue, &tex_view, hdr_layer_view, &bt, None);
let compositor_layer = CompositorLayer::new(hdr_layer_handle, inst.opacity, lightningbeam_core::gpu::BlendMode::Normal);
let mut enc = device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("export_video_composite") });
gpu_resources.compositor.composite(device, queue, &mut enc, &[compositor_layer], &gpu_resources.buffer_pool, &gpu_resources.hdr_texture_view, None);
queue.submit(Some(enc.finish()));
}
gpu_resources.buffer_pool.release(hdr_layer_handle);
}
}
RenderedLayerType::Float { x: float_x, y: float_y, width: fw, height: fh, transform: layer_transform, pixels, .. } => {
if !pixels.is_empty() {
// sRGB-premultiplied → linear-premultiplied
let linear: Vec<u8> = pixels.chunks_exact(4).flat_map(|p| {
let lin = |c: u8| -> u8 {
let f = c as f32 / 255.0;
let l = if f <= 0.04045 { f / 12.92 } else { ((f + 0.055) / 1.055).powf(2.4) };
(l * 255.0 + 0.5) as u8
};
[lin(p[0]), lin(p[1]), lin(p[2]), p[3]]
}).collect();
let tex = upload_transient_texture(device, queue, &linear, *fw, *fh, Some("export_float_tex"));
let tex_view = tex.create_view(&Default::default());
let hdr_layer_handle = gpu_resources.buffer_pool.acquire(device, hdr_spec);
if let Some(hdr_layer_view) = gpu_resources.buffer_pool.get_view(hdr_layer_handle) {
let float_to_vp = *layer_transform * Affine::translate((*float_x as f64, *float_y as f64));
let bt = crate::gpu_brush::BlitTransform::new(float_to_vp, *fw, *fh, width, height);
gpu_resources.canvas_blit.blit(device, queue, &tex_view, hdr_layer_view, &bt, None);
let compositor_layer = CompositorLayer::normal(hdr_layer_handle, 1.0);
let mut enc = device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("export_float_composite") });
gpu_resources.compositor.composite(device, queue, &mut enc, &[compositor_layer], &gpu_resources.buffer_pool, &gpu_resources.hdr_texture_view, None);
queue.submit(Some(enc.finish()));
}
gpu_resources.buffer_pool.release(hdr_layer_handle);
}
}
RenderedLayerType::Effect { effect_instances } => {
let current_time = document.current_time;
for effect_instance in effect_instances {
let Some(effect_def) = document.get_effect_definition(&effect_instance.clip_id) else { continue; };
if !gpu_resources.effect_processor.is_compiled(&effect_def.id) {
let success = gpu_resources.effect_processor.compile_effect(device, effect_def);
if !success { eprintln!("Failed to compile effect: {}", effect_def.name); continue; }
}
let effect_inst = lightningbeam_core::effect::EffectInstance::new(
effect_def,
effect_instance.timeline_start,
effect_instance.timeline_start + effect_instance.effective_duration(lightningbeam_core::effect::EFFECT_DURATION),
);
let effect_output_handle = gpu_resources.buffer_pool.acquire(device, hdr_spec);
if let Some(effect_output_view) = gpu_resources.buffer_pool.get_view(effect_output_handle) {
let mut enc = device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("export_effect") });
let applied = gpu_resources.effect_processor.apply_effect(
device, queue, &mut enc, effect_def, &effect_inst,
&gpu_resources.hdr_texture_view, effect_output_view, width, height, current_time,
);
if applied {
queue.submit(Some(enc.finish()));
let effect_layer = CompositorLayer::normal(effect_output_handle, rendered_layer.opacity);
let mut copy_enc = device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("export_effect_copy") });
// Replace the accumulator with the processed result.
gpu_resources.compositor.composite(device, queue, &mut copy_enc, &[effect_layer], &gpu_resources.buffer_pool, &gpu_resources.hdr_texture_view, Some([0.0, 0.0, 0.0, 0.0]));
queue.submit(Some(copy_enc.finish()));
}
}
gpu_resources.buffer_pool.release(effect_output_handle);
}
}
}
}
gpu_resources.buffer_pool.next_frame();
Ok(())
}
/// Upload `pixels` to a transient `Rgba8Unorm` GPU texture (TEXTURE_BINDING | COPY_DST).
fn upload_transient_texture(
device: &wgpu::Device,
queue: &wgpu::Queue,
pixels: &[u8],
width: u32,
height: u32,
label: Option<&'static str>,
) -> wgpu::Texture {
let tex = device.create_texture(&wgpu::TextureDescriptor {
label,
size: wgpu::Extent3d { width, height, depth_or_array_layers: 1 },
mip_level_count: 1, sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format: wgpu::TextureFormat::Rgba8Unorm,
usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST,
view_formats: &[],
});
queue.write_texture(
wgpu::TexelCopyTextureInfo { texture: &tex, mip_level: 0, origin: wgpu::Origin3d::ZERO, aspect: wgpu::TextureAspect::All },
pixels,
wgpu::TexelCopyBufferLayout { offset: 0, bytes_per_row: Some(width * 4), rows_per_image: Some(height) },
wgpu::Extent3d { width, height, depth_or_array_layers: 1 },
);
tex
}
/// Render a document frame using the HDR compositing pipeline with effects
///
/// This function uses the same rendering pipeline as the stage preview,
@@ -748,193 +983,12 @@ pub fn render_frame_to_rgba_hdr(
image_cache,
video_manager,
None, // No webcam during export
None, // No floating selection during export
false, // No checkerboard in export
);
// Buffer specs for layer rendering
let layer_spec = BufferSpec::new(width, height, BufferFormat::Rgba8Srgb);
let hdr_spec = BufferSpec::new(width, height, BufferFormat::Rgba16Float);
// Render parameters for Vello (transparent background for layers)
let layer_render_params = vello::RenderParams {
base_color: vello::peniko::Color::TRANSPARENT,
width,
height,
antialiasing_method: vello::AaConfig::Area,
};
// First, render background and composite it
let bg_srgb_handle = gpu_resources.buffer_pool.acquire(device, layer_spec);
let bg_hdr_handle = gpu_resources.buffer_pool.acquire(device, hdr_spec);
if let (Some(bg_srgb_view), Some(bg_hdr_view)) = (
gpu_resources.buffer_pool.get_view(bg_srgb_handle),
gpu_resources.buffer_pool.get_view(bg_hdr_handle),
) {
// Render background scene
renderer.render_to_texture(device, queue, &composite_result.background, bg_srgb_view, &layer_render_params)
.map_err(|e| format!("Failed to render background: {}", e))?;
// Convert sRGB to linear HDR
let mut convert_encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor {
label: Some("export_bg_srgb_to_linear_encoder"),
});
gpu_resources.srgb_to_linear.convert(device, &mut convert_encoder, bg_srgb_view, bg_hdr_view);
queue.submit(Some(convert_encoder.finish()));
// Composite background onto HDR texture (first layer, clears to black for export)
let bg_compositor_layer = CompositorLayer::normal(bg_hdr_handle, 1.0);
let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor {
label: Some("export_bg_composite_encoder"),
});
// Clear to black for export (unlike stage preview which has gray background)
gpu_resources.compositor.composite(
device,
queue,
&mut encoder,
&[bg_compositor_layer],
&gpu_resources.buffer_pool,
&gpu_resources.hdr_texture_view,
Some([0.0, 0.0, 0.0, 1.0]),
);
queue.submit(Some(encoder.finish()));
}
gpu_resources.buffer_pool.release(bg_srgb_handle);
gpu_resources.buffer_pool.release(bg_hdr_handle);
// Now render and composite each layer incrementally
for rendered_layer in &composite_result.layers {
if !rendered_layer.has_content {
continue;
}
match &rendered_layer.layer_type {
RenderedLayerType::Content => {
// Regular content layer - render to sRGB, convert to linear, then composite
let srgb_handle = gpu_resources.buffer_pool.acquire(device, layer_spec);
let hdr_layer_handle = gpu_resources.buffer_pool.acquire(device, hdr_spec);
if let (Some(srgb_view), Some(hdr_layer_view)) = (
gpu_resources.buffer_pool.get_view(srgb_handle),
gpu_resources.buffer_pool.get_view(hdr_layer_handle),
) {
// Render layer scene to sRGB buffer
renderer.render_to_texture(device, queue, &rendered_layer.scene, srgb_view, &layer_render_params)
.map_err(|e| format!("Failed to render layer: {}", e))?;
// Convert sRGB to linear HDR
let mut convert_encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor {
label: Some("export_layer_srgb_to_linear_encoder"),
});
gpu_resources.srgb_to_linear.convert(device, &mut convert_encoder, srgb_view, hdr_layer_view);
queue.submit(Some(convert_encoder.finish()));
// Composite this layer onto the HDR accumulator with its opacity
let compositor_layer = CompositorLayer::new(
hdr_layer_handle,
rendered_layer.opacity,
rendered_layer.blend_mode,
);
let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor {
label: Some("export_layer_composite_encoder"),
});
gpu_resources.compositor.composite(
device,
queue,
&mut encoder,
&[compositor_layer],
&gpu_resources.buffer_pool,
&gpu_resources.hdr_texture_view,
None, // Don't clear - blend onto existing content
);
queue.submit(Some(encoder.finish()));
}
gpu_resources.buffer_pool.release(srgb_handle);
gpu_resources.buffer_pool.release(hdr_layer_handle);
}
RenderedLayerType::Effect { effect_instances } => {
// Effect layer - apply effects to the current HDR accumulator
let current_time = document.current_time;
for effect_instance in effect_instances {
// Get effect definition from document
let Some(effect_def) = document.get_effect_definition(&effect_instance.clip_id) else {
continue;
};
// Compile effect if needed
if !gpu_resources.effect_processor.is_compiled(&effect_def.id) {
let success = gpu_resources.effect_processor.compile_effect(device, effect_def);
if !success {
eprintln!("Failed to compile effect: {}", effect_def.name);
continue;
}
}
// Create EffectInstance from ClipInstance for the processor
let effect_inst = lightningbeam_core::effect::EffectInstance::new(
effect_def,
effect_instance.timeline_start,
effect_instance.timeline_start + effect_instance.effective_duration(lightningbeam_core::effect::EFFECT_DURATION),
);
// Acquire temp buffer for effect output (HDR format)
let effect_output_handle = gpu_resources.buffer_pool.acquire(device, hdr_spec);
if let Some(effect_output_view) = gpu_resources.buffer_pool.get_view(effect_output_handle) {
let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor {
label: Some("export_effect_encoder"),
});
// Apply effect: HDR accumulator → effect output buffer
let applied = gpu_resources.effect_processor.apply_effect(
device,
queue,
&mut encoder,
effect_def,
&effect_inst,
&gpu_resources.hdr_texture_view,
effect_output_view,
width,
height,
current_time,
);
if applied {
queue.submit(Some(encoder.finish()));
// Copy effect output back to HDR accumulator
let mut copy_encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor {
label: Some("export_effect_copy_encoder"),
});
// Use compositor to copy (replacing content)
let effect_layer = CompositorLayer::normal(
effect_output_handle,
rendered_layer.opacity, // Apply effect layer opacity
);
gpu_resources.compositor.composite(
device,
queue,
&mut copy_encoder,
&[effect_layer],
&gpu_resources.buffer_pool,
&gpu_resources.hdr_texture_view,
Some([0.0, 0.0, 0.0, 0.0]), // Clear with transparent (we're replacing)
);
queue.submit(Some(copy_encoder.finish()));
}
}
gpu_resources.buffer_pool.release(effect_output_handle);
}
}
}
}
// Advance frame counter for buffer cleanup
gpu_resources.buffer_pool.next_frame();
// Video export is never transparent.
composite_document_to_hdr(&composite_result, document, device, queue, renderer, gpu_resources, width, height, false)?;
// Use persistent output texture (already created in ExportGpuResources)
let output_view = &gpu_resources.output_texture_view;
@@ -1118,6 +1172,8 @@ pub fn render_frame_to_gpu_rgba(
video_manager: &Arc<std::sync::Mutex<VideoManager>>,
gpu_resources: &mut ExportGpuResources,
rgba_texture_view: &wgpu::TextureView,
floating_selection: Option<&lightningbeam_core::selection::RasterFloatingSelection>,
allow_transparency: bool,
) -> Result<wgpu::CommandEncoder, String> {
use vello::kurbo::Affine;
@@ -1134,176 +1190,11 @@ pub fn render_frame_to_gpu_rgba(
image_cache,
video_manager,
None, // No webcam during export
floating_selection,
false, // No checkerboard in export
);
// Buffer specs for layer rendering
let layer_spec = BufferSpec::new(width, height, BufferFormat::Rgba8Srgb);
let hdr_spec = BufferSpec::new(width, height, BufferFormat::Rgba16Float);
// Render parameters for Vello (transparent background for layers)
let layer_render_params = vello::RenderParams {
base_color: vello::peniko::Color::TRANSPARENT,
width,
height,
antialiasing_method: vello::AaConfig::Area,
};
// Render background and composite it
let bg_srgb_handle = gpu_resources.buffer_pool.acquire(device, layer_spec);
let bg_hdr_handle = gpu_resources.buffer_pool.acquire(device, hdr_spec);
if let (Some(bg_srgb_view), Some(bg_hdr_view)) = (
gpu_resources.buffer_pool.get_view(bg_srgb_handle),
gpu_resources.buffer_pool.get_view(bg_hdr_handle),
) {
renderer.render_to_texture(device, queue, &composite_result.background, bg_srgb_view, &layer_render_params)
.map_err(|e| format!("Failed to render background: {}", e))?;
let mut convert_encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor {
label: Some("export_bg_srgb_to_linear_encoder"),
});
gpu_resources.srgb_to_linear.convert(device, &mut convert_encoder, bg_srgb_view, bg_hdr_view);
queue.submit(Some(convert_encoder.finish()));
let bg_compositor_layer = CompositorLayer::normal(bg_hdr_handle, 1.0);
let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor {
label: Some("export_bg_composite_encoder"),
});
gpu_resources.compositor.composite(
device,
queue,
&mut encoder,
&[bg_compositor_layer],
&gpu_resources.buffer_pool,
&gpu_resources.hdr_texture_view,
Some([0.0, 0.0, 0.0, 1.0]),
);
queue.submit(Some(encoder.finish()));
}
gpu_resources.buffer_pool.release(bg_srgb_handle);
gpu_resources.buffer_pool.release(bg_hdr_handle);
// Render and composite each layer incrementally
for rendered_layer in &composite_result.layers {
if !rendered_layer.has_content {
continue;
}
match &rendered_layer.layer_type {
RenderedLayerType::Content => {
let srgb_handle = gpu_resources.buffer_pool.acquire(device, layer_spec);
let hdr_layer_handle = gpu_resources.buffer_pool.acquire(device, hdr_spec);
if let (Some(srgb_view), Some(hdr_layer_view)) = (
gpu_resources.buffer_pool.get_view(srgb_handle),
gpu_resources.buffer_pool.get_view(hdr_layer_handle),
) {
renderer.render_to_texture(device, queue, &rendered_layer.scene, srgb_view, &layer_render_params)
.map_err(|e| format!("Failed to render layer: {}", e))?;
let mut convert_encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor {
label: Some("export_layer_srgb_to_linear_encoder"),
});
gpu_resources.srgb_to_linear.convert(device, &mut convert_encoder, srgb_view, hdr_layer_view);
queue.submit(Some(convert_encoder.finish()));
let compositor_layer = CompositorLayer::normal(hdr_layer_handle, rendered_layer.opacity);
let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor {
label: Some("export_layer_composite_encoder"),
});
gpu_resources.compositor.composite(
device,
queue,
&mut encoder,
&[compositor_layer],
&gpu_resources.buffer_pool,
&gpu_resources.hdr_texture_view,
None,
);
queue.submit(Some(encoder.finish()));
}
gpu_resources.buffer_pool.release(srgb_handle);
gpu_resources.buffer_pool.release(hdr_layer_handle);
}
RenderedLayerType::Effect { effect_instances } => {
// Effect layer - apply effects to the current HDR accumulator
let current_time = document.current_time;
for effect_instance in effect_instances {
// Get effect definition from document
let Some(effect_def) = document.get_effect_definition(&effect_instance.clip_id) else {
continue;
};
// Compile effect if needed
if !gpu_resources.effect_processor.is_compiled(&effect_def.id) {
let success = gpu_resources.effect_processor.compile_effect(device, effect_def);
if !success {
eprintln!("Failed to compile effect: {}", effect_def.name);
continue;
}
}
// Create EffectInstance from ClipInstance for the processor
let effect_inst = lightningbeam_core::effect::EffectInstance::new(
effect_def,
effect_instance.timeline_start,
effect_instance.timeline_start + effect_instance.effective_duration(lightningbeam_core::effect::EFFECT_DURATION),
);
// Acquire temp buffer for effect output (HDR format)
let effect_output_handle = gpu_resources.buffer_pool.acquire(device, hdr_spec);
if let Some(effect_output_view) = gpu_resources.buffer_pool.get_view(effect_output_handle) {
let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor {
label: Some("export_effect_encoder"),
});
// Apply effect: HDR accumulator → effect output buffer
let applied = gpu_resources.effect_processor.apply_effect(
device,
queue,
&mut encoder,
effect_def,
&effect_inst,
&gpu_resources.hdr_texture_view,
effect_output_view,
width,
height,
current_time,
);
if applied {
// Copy effect output back to HDR accumulator
encoder.copy_texture_to_texture(
wgpu::TexelCopyTextureInfo {
texture: gpu_resources.buffer_pool.get_texture(effect_output_handle).unwrap(),
mip_level: 0,
origin: wgpu::Origin3d::ZERO,
aspect: wgpu::TextureAspect::All,
},
wgpu::TexelCopyTextureInfo {
texture: &gpu_resources.hdr_texture,
mip_level: 0,
origin: wgpu::Origin3d::ZERO,
aspect: wgpu::TextureAspect::All,
},
wgpu::Extent3d {
width,
height,
depth_or_array_layers: 1,
},
);
}
queue.submit(Some(encoder.finish()));
}
gpu_resources.buffer_pool.release(effect_output_handle);
}
}
}
}
composite_document_to_hdr(&composite_result, document, device, queue, renderer, gpu_resources, width, height, allow_transparency)?;
// Convert HDR to sRGB (linear → sRGB), render directly to external RGBA texture
let output_view = rgba_texture_view;
File diff suppressed because it is too large Load Diff
@@ -429,24 +429,26 @@ impl AppAction {
/// `Tool::Split` has no tool-shortcut action (it's triggered via the menu).
pub fn tool_app_action(tool: lightningbeam_core::tool::Tool) -> Option<AppAction> {
use lightningbeam_core::tool::Tool;
Some(match tool {
Tool::Select => AppAction::ToolSelect,
Tool::Draw => AppAction::ToolDraw,
Tool::Transform => AppAction::ToolTransform,
Tool::Rectangle => AppAction::ToolRectangle,
Tool::Ellipse => AppAction::ToolEllipse,
Tool::PaintBucket => AppAction::ToolPaintBucket,
Tool::Eyedropper => AppAction::ToolEyedropper,
Tool::Line => AppAction::ToolLine,
Tool::Polygon => AppAction::ToolPolygon,
Tool::BezierEdit => AppAction::ToolBezierEdit,
Tool::Text => AppAction::ToolText,
Tool::RegionSelect => AppAction::ToolRegionSelect,
Tool::Erase => AppAction::ToolErase,
Tool::Smudge => AppAction::ToolSmudge,
Tool::SelectLasso => AppAction::ToolSelectLasso,
Tool::Split => AppAction::ToolSplit,
})
match tool {
Tool::Select => Some(AppAction::ToolSelect),
Tool::Draw => Some(AppAction::ToolDraw),
Tool::Transform => Some(AppAction::ToolTransform),
Tool::Rectangle => Some(AppAction::ToolRectangle),
Tool::Ellipse => Some(AppAction::ToolEllipse),
Tool::PaintBucket => Some(AppAction::ToolPaintBucket),
Tool::Eyedropper => Some(AppAction::ToolEyedropper),
Tool::Line => Some(AppAction::ToolLine),
Tool::Polygon => Some(AppAction::ToolPolygon),
Tool::BezierEdit => Some(AppAction::ToolBezierEdit),
Tool::Text => Some(AppAction::ToolText),
Tool::RegionSelect => Some(AppAction::ToolRegionSelect),
Tool::Erase => Some(AppAction::ToolErase),
Tool::Smudge => Some(AppAction::ToolSmudge),
Tool::SelectLasso => Some(AppAction::ToolSelectLasso),
Tool::Split => Some(AppAction::ToolSplit),
// New tools have no keybinding yet
_ => None,
}
}
// === Default bindings ===
+105 -35
View File
@@ -13,6 +13,8 @@ use uuid::Uuid;
mod panes;
use panes::{PaneInstance, PaneRenderer};
mod tools;
mod widgets;
mod menu;
@@ -26,6 +28,8 @@ mod waveform_gpu;
mod cqt_gpu;
mod gpu_brush;
mod raster_tool;
mod config;
use config::AppConfig;
@@ -332,6 +336,7 @@ mod tool_icons {
pub static ERASE: &[u8] = include_bytes!("../../../src/assets/erase.svg");
pub static SMUDGE: &[u8] = include_bytes!("../../../src/assets/smudge.svg");
pub static LASSO: &[u8] = include_bytes!("../../../src/assets/lasso.svg");
pub static TODO: &[u8] = include_bytes!("../../../src/assets/todo.svg");
}
/// Embedded focus icon SVGs
@@ -399,11 +404,28 @@ impl ToolIconCache {
Tool::Polygon => tool_icons::POLYGON,
Tool::BezierEdit => tool_icons::BEZIER_EDIT,
Tool::Text => tool_icons::TEXT,
Tool::RegionSelect => tool_icons::SELECT, // Reuse select icon for now
Tool::RegionSelect => tool_icons::SELECT,
Tool::Split => tool_icons::SPLIT,
Tool::Erase => tool_icons::ERASE,
Tool::Smudge => tool_icons::SMUDGE,
Tool::SelectLasso => tool_icons::LASSO,
// Not yet implemented — use placeholder icon
Tool::Pencil
| Tool::Pen
| Tool::Airbrush
| Tool::CloneStamp
| Tool::HealingBrush
| Tool::PatternStamp
| Tool::DodgeBurn
| Tool::Sponge
| Tool::BlurSharpen
| Tool::Gradient
| Tool::CustomShape
| Tool::SelectEllipse
| Tool::MagicWand
| Tool::QuickSelect
| Tool::Warp
| Tool::Liquify => tool_icons::TODO,
};
if let Some(texture) = rasterize_svg(svg_data, tool.icon_file(), 180, ctx) {
self.icons.insert(tool, texture);
@@ -766,12 +788,10 @@ struct EditorApp {
draw_simplify_mode: lightningbeam_core::tool::SimplifyMode, // Current simplification mode for draw tool
rdp_tolerance: f64, // RDP simplification tolerance (default: 10.0)
schneider_max_error: f64, // Schneider curve fitting max error (default: 30.0)
// Raster brush settings
brush_radius: f32, // brush radius in pixels
brush_opacity: f32, // brush opacity 0.0–1.0
brush_hardness: f32, // brush hardness 0.0–1.0
brush_spacing: f32, // dabs_per_radius (fraction of radius per dab)
brush_use_fg: bool, // true = paint with FG (stroke) color, false = BG (fill) color
/// All per-tool raster paint settings (brush, eraser, smudge, clone, pattern, dodge/burn, sponge).
raster_settings: tools::RasterToolSettings,
/// GPU-rendered brush preview pixel buffers, shared with VelloCallback::prepare().
brush_preview_pixels: std::sync::Arc<std::sync::Mutex<Vec<(u32, u32, Vec<u8>)>>>,
// Audio engine integration
#[allow(dead_code)] // Must be kept alive to maintain audio output
audio_stream: Option<cpal::Stream>,
@@ -841,6 +861,7 @@ struct EditorApp {
// Region select state
region_selection: Option<lightningbeam_core::selection::RegionSelection>,
region_select_mode: lightningbeam_core::tool::RegionSelectMode,
lasso_mode: lightningbeam_core::tool::LassoMode,
// VU meter levels
input_level: f32,
@@ -935,6 +956,9 @@ impl EditorApp {
#[cfg(debug_assertions)]
cc.egui_ctx.style_mut(|style| style.debug.show_unaligned = false);
// Disable egui's built-in Ctrl+Plus/Minus zoom — we handle zoom ourselves.
cc.egui_ctx.options_mut(|o| o.zoom_with_keyboard = false);
// Load application config
let config = AppConfig::load();
@@ -1049,11 +1073,8 @@ impl EditorApp {
draw_simplify_mode: lightningbeam_core::tool::SimplifyMode::Smooth, // Default to smooth curves
rdp_tolerance: 10.0, // Default RDP tolerance
schneider_max_error: 30.0, // Default Schneider max error
brush_radius: 10.0,
brush_opacity: 1.0,
brush_hardness: 0.5,
brush_spacing: 0.1,
brush_use_fg: true,
raster_settings: tools::RasterToolSettings::default(),
brush_preview_pixels: std::sync::Arc::new(std::sync::Mutex::new(Vec::new())),
audio_stream,
audio_controller,
audio_event_rx,
@@ -1097,6 +1118,7 @@ impl EditorApp {
polygon_sides: 5, // Default to pentagon
region_selection: None,
region_select_mode: lightningbeam_core::tool::RegionSelectMode::default(),
lasso_mode: lightningbeam_core::tool::LassoMode::default(),
input_level: 0.0,
output_level: (0.0, 0.0),
track_levels: HashMap::new(),
@@ -1852,7 +1874,8 @@ impl EditorApp {
let cy = y0 + row;
let inside = match sel {
RasterSelection::Rect(..) => true,
RasterSelection::Lasso(_) => sel.contains_pixel(cx as i32, cy as i32),
RasterSelection::Lasso(_) | RasterSelection::Mask { .. } =>
sel.contains_pixel(cx as i32, cy as i32),
};
if inside {
let src = ((cy * canvas_w + cx) * 4) as usize;
@@ -1976,7 +1999,8 @@ impl EditorApp {
let action = RasterStrokeAction::new(
float.layer_id, float.time,
float.canvas_before, canvas_after,
std::sync::Arc::try_unwrap(float.canvas_before).unwrap_or_else(|a| (*a).clone()),
canvas_after,
w, h,
);
if let Err(e) = self.action_executor.execute(Box::new(action)) {
@@ -1995,7 +2019,7 @@ impl EditorApp {
let document = self.action_executor.document_mut();
let Some(AnyLayer::Raster(rl)) = document.get_layer_mut(&float.layer_id) else { return };
let Some(kf) = rl.keyframe_at_mut(float.time) else { return };
kf.raw_pixels = float.canvas_before;
kf.raw_pixels = std::sync::Arc::try_unwrap(float.canvas_before).unwrap_or_else(|a| (*a).clone());
}
/// Drop (discard) the floating selection keeping the hole punched in the
@@ -2015,7 +2039,8 @@ impl EditorApp {
let (w, h) = (kf.width, kf.height);
let action = RasterStrokeAction::new(
float.layer_id, float.time,
float.canvas_before, canvas_after,
std::sync::Arc::try_unwrap(float.canvas_before).unwrap_or_else(|a| (*a).clone()),
canvas_after,
w, h,
);
if let Err(e) = self.action_executor.execute(Box::new(action)) {
@@ -2036,7 +2061,7 @@ impl EditorApp {
if matches!(document.get_layer(&layer_id), Some(AnyLayer::Raster(_))) {
if let Some(float) = &self.selection.raster_floating {
self.clipboard_manager.copy(ClipboardContent::RasterPixels {
pixels: float.pixels.clone(),
pixels: (*float.pixels).clone(),
width: float.width,
height: float.height,
});
@@ -2516,14 +2541,14 @@ impl EditorApp {
use lightningbeam_core::selection::{RasterFloatingSelection, RasterSelection};
self.selection.raster_floating = Some(RasterFloatingSelection {
pixels,
pixels: std::sync::Arc::new(pixels),
width,
height,
x: paste_x,
y: paste_y,
layer_id,
time: self.playback_time,
canvas_before,
canvas_before: std::sync::Arc::new(canvas_before),
canvas_id: uuid::Uuid::new_v4(),
});
// Update the marquee to show the floating selection bounds.
@@ -2942,14 +2967,42 @@ impl EditorApp {
}
MenuAction::Export => {
println!("Menu: Export");
// Open export dialog with calculated timeline endpoint
let timeline_endpoint = self.action_executor.document().calculate_timeline_endpoint();
// Derive project name from the .beam file path, falling back to document name
let project_name = self.current_file_path.as_ref()
.and_then(|p| p.file_stem())
.map(|s| s.to_string_lossy().into_owned())
.unwrap_or_else(|| self.action_executor.document().name.clone());
self.export_dialog.open(timeline_endpoint, &project_name);
// Build document hint for smart export-type defaulting.
let hint = {
use lightningbeam_core::layer::AnyLayer;
use export::dialog::DocumentHint;
fn scan(layers: &[AnyLayer], hint: &mut DocumentHint) {
for l in layers {
match l {
AnyLayer::Video(_) => hint.has_video = true,
AnyLayer::Audio(_) => hint.has_audio = true,
AnyLayer::Raster(_) => hint.has_raster = true,
AnyLayer::Vector(_) | AnyLayer::Effect(_) => hint.has_vector = true,
AnyLayer::Group(g) => scan(&g.children, hint),
}
}
}
let doc = self.action_executor.document();
let mut h = DocumentHint {
has_video: false,
has_audio: false,
has_raster: false,
has_vector: false,
current_time: doc.current_time,
doc_width: doc.width as u32,
doc_height: doc.height as u32,
};
scan(&doc.root.children, &mut h);
h
};
self.export_dialog.open(timeline_endpoint, &project_name, &hint);
}
MenuAction::Quit => {
println!("Menu: Quit");
@@ -4535,16 +4588,9 @@ impl eframe::App for EditorApp {
fn update(&mut self, ctx: &egui::Context, frame: &mut eframe::Frame) {
let _frame_start = std::time::Instant::now();
// Disable egui's built-in Ctrl+Plus/Minus zoom behavior
// We handle zoom ourselves for the Stage pane
ctx.options_mut(|o| {
o.zoom_with_keyboard = false;
});
// Force continuous repaint if we have pending waveform updates
// This ensures thumbnails update immediately when waveform data arrives
if !self.audio_pools_with_new_waveforms.is_empty() {
println!("🔄 [UPDATE] Pending waveform updates for pools: {:?}", self.audio_pools_with_new_waveforms);
ctx.request_repaint();
}
@@ -5264,6 +5310,17 @@ impl eframe::App for EditorApp {
let export_started = if let Some(orchestrator) = &mut self.export_orchestrator {
match export_result {
ExportResult::Image(settings, output_path) => {
println!("🖼 [MAIN] Starting image export: {}", output_path.display());
let doc = self.action_executor.document();
orchestrator.start_image_export(
settings,
output_path,
doc.width as u32,
doc.height as u32,
);
false // image export is silent (no progress dialog)
}
ExportResult::AudioOnly(settings, output_path) => {
println!("🎵 [MAIN] Starting audio-only export: {}", output_path.display());
@@ -5374,6 +5431,7 @@ impl eframe::App for EditorApp {
let mut temp_image_cache = lightningbeam_core::renderer::ImageCache::new();
if let Some(renderer) = &mut temp_renderer {
// Drive incremental video export.
if let Ok(has_more) = orchestrator.render_next_video_frame(
self.action_executor.document_mut(),
device,
@@ -5383,10 +5441,24 @@ impl eframe::App for EditorApp {
&self.video_manager,
) {
if has_more {
// More frames to render - request repaint for next frame
ctx.request_repaint();
}
}
// Drive single-frame image export (two-frame async: render then readback).
match orchestrator.render_image_frame(
self.action_executor.document_mut(),
device,
queue,
renderer,
&mut temp_image_cache,
&self.video_manager,
self.selection.raster_floating.as_ref(),
) {
Ok(false) => { ctx.request_repaint(); } // readback pending
Ok(true) => {} // done or cancelled
Err(e) => { eprintln!("Image export failed: {e}"); }
}
}
}
}
@@ -5605,11 +5677,7 @@ impl eframe::App for EditorApp {
draw_simplify_mode: &mut self.draw_simplify_mode,
rdp_tolerance: &mut self.rdp_tolerance,
schneider_max_error: &mut self.schneider_max_error,
brush_radius: &mut self.brush_radius,
brush_opacity: &mut self.brush_opacity,
brush_hardness: &mut self.brush_hardness,
brush_spacing: &mut self.brush_spacing,
brush_use_fg: &mut self.brush_use_fg,
raster_settings: &mut self.raster_settings,
audio_controller: self.audio_controller.as_ref(),
video_manager: &self.video_manager,
playback_time: &mut self.playback_time,
@@ -5650,6 +5718,7 @@ impl eframe::App for EditorApp {
script_saved: &mut self.script_saved,
region_selection: &mut self.region_selection,
region_select_mode: &mut self.region_select_mode,
lasso_mode: &mut self.lasso_mode,
pending_graph_loads: &self.pending_graph_loads,
clipboard_consumed: &mut clipboard_consumed,
keymap: &self.keymap,
@@ -5660,6 +5729,7 @@ impl eframe::App for EditorApp {
test_mode: &mut self.test_mode,
#[cfg(debug_assertions)]
synthetic_input: &mut synthetic_input_storage,
brush_preview_pixels: &self.brush_preview_pixels,
},
pane_instances: &mut self.pane_instances,
};
@@ -0,0 +1,261 @@
//! Gradient stop editor widget.
//!
//! Call [`gradient_stop_editor`] inside any egui layout; it returns `true` when
//! `gradient` was modified.
use eframe::egui::{self, Color32, DragValue, Painter, Rect, Sense, Stroke, Vec2};
use lightningbeam_core::gradient::{GradientExtend, GradientStop, GradientType, ShapeGradient};
use lightningbeam_core::shape::ShapeColor;
// ── Public entry point ───────────────────────────────────────────────────────
/// Render an inline gradient editor.
///
/// * `gradient` – the gradient being edited (mutated in place).
/// * `selected_stop` – index of the currently selected stop (persisted by caller).
///
/// Returns `true` if anything changed.
pub fn gradient_stop_editor(
ui: &mut egui::Ui,
gradient: &mut ShapeGradient,
selected_stop: &mut Option<usize>,
) -> bool {
let mut changed = false;
// ── Row 1: Kind + angle ───────────────────────────────────────────────
ui.horizontal(|ui| {
let was_linear = gradient.kind == GradientType::Linear;
if ui.selectable_label(was_linear, "Linear").clicked() && !was_linear {
gradient.kind = GradientType::Linear;
changed = true;
}
if ui.selectable_label(!was_linear, "Radial").clicked() && was_linear {
gradient.kind = GradientType::Radial;
changed = true;
}
if gradient.kind == GradientType::Linear {
ui.add_space(8.0);
ui.label("Angle:");
if ui.add(
DragValue::new(&mut gradient.angle)
.speed(1.0)
.range(-360.0..=360.0)
.suffix("°"),
).changed() {
changed = true;
}
}
});
// ── Gradient bar + handles ────────────────────────────────────────────
let bar_height = 22.0_f32;
let handle_h = 14.0_f32;
let total_height = bar_height + handle_h + 4.0;
let avail_w = ui.available_width();
let (bar_rect, bar_resp) = ui.allocate_exact_size(
Vec2::new(avail_w, total_height),
Sense::click(),
);
let painter = ui.painter_at(bar_rect);
let bar = Rect::from_min_size(bar_rect.min, Vec2::new(avail_w, bar_height));
let track = Rect::from_min_size(
egui::pos2(bar_rect.min.x, bar_rect.min.y + bar_height + 2.0),
Vec2::new(avail_w, handle_h),
);
// Draw checkerboard background (transparent indicator).
draw_checker(&painter, bar);
// Draw gradient bar as N segments.
let seg = 128_usize;
for i in 0..seg {
let t0 = i as f32 / seg as f32;
let t1 = (i + 1) as f32 / seg as f32;
let t = (t0 + t1) * 0.5;
let [r, g, b, a] = gradient.eval(t);
let col = Color32::from_rgba_unmultiplied(r, g, b, a);
let x0 = bar.min.x + t0 * bar.width();
let x1 = bar.min.x + t1 * bar.width();
let seg_rect = Rect::from_min_max(
egui::pos2(x0, bar.min.y),
egui::pos2(x1, bar.max.y),
);
painter.rect_filled(seg_rect, 0.0, col);
}
// Outline.
painter.rect_stroke(bar, 2.0, Stroke::new(1.0, Color32::from_gray(60)), eframe::egui::StrokeKind::Middle);
// Click on bar → add stop.
if bar_resp.clicked() {
if let Some(pos) = bar_resp.interact_pointer_pos() {
if bar.contains(pos) {
let t = ((pos.x - bar.min.x) / bar.width()).clamp(0.0, 1.0);
let [r, g, b, a] = gradient.eval(t);
gradient.stops.push(GradientStop {
position: t,
color: ShapeColor::rgba(r, g, b, a),
});
gradient.stops.sort_by(|a, b| a.position.partial_cmp(&b.position).unwrap());
*selected_stop = gradient.stops.iter().position(|s| s.position == t);
changed = true;
}
}
}
// Draw stop handles.
// We need to detect drags per-handle, so allocate individual rects with the
// regular egui input model. To avoid borrow conflicts we collect interactions
// before mutating.
let handle_w = 10.0_f32;
let n_stops = gradient.stops.len();
let mut drag_idx: Option<usize> = None;
let mut drag_delta: f32 = 0.0;
let mut click_idx: Option<usize> = None;
// To render handles after collecting, remember their rects.
let handle_rects: Vec<Rect> = (0..n_stops).map(|i| {
let cx = track.min.x + gradient.stops[i].position * track.width();
Rect::from_center_size(
egui::pos2(cx, track.center().y),
Vec2::new(handle_w, handle_h),
)
}).collect();
for (i, &h_rect) in handle_rects.iter().enumerate() {
let resp = ui.interact(h_rect, ui.id().with(("grad_handle", i)), Sense::click_and_drag());
if resp.dragged() {
drag_idx = Some(i);
drag_delta = resp.drag_delta().x / track.width();
}
if resp.clicked() {
click_idx = Some(i);
}
}
// Apply drag.
if let (Some(i), delta) = (drag_idx, drag_delta) {
if delta != 0.0 {
let new_pos = (gradient.stops[i].position + delta).clamp(0.0, 1.0);
gradient.stops[i].position = new_pos;
// Re-sort and track the moved stop.
gradient.stops.sort_by(|a, b| a.position.partial_cmp(&b.position).unwrap());
// Find new index of the moved stop (closest position match).
if let Some(ref mut sel) = *selected_stop {
// Re-find by position proximity.
*sel = gradient.stops.iter().enumerate()
.min_by(|(_, a), (_, b)| {
let pa = (a.position - (gradient.stops.get(i).map_or(0.0, |s| s.position))).abs();
let pb = (b.position - (gradient.stops.get(i).map_or(0.0, |s| s.position))).abs();
pa.partial_cmp(&pb).unwrap()
})
.map(|(idx, _)| idx)
.unwrap_or(0);
}
changed = true;
}
}
if let Some(i) = click_idx {
*selected_stop = Some(i);
}
// Paint handles on top (after interaction so they visually react).
for (i, h_rect) in handle_rects.iter().enumerate() {
let col = ShapeColor_to_Color32(gradient.stops[i].color);
let is_selected = *selected_stop == Some(i);
// Draw a downward-pointing triangle.
let cx = h_rect.center().x;
let top = h_rect.min.y;
let bot = h_rect.max.y;
let hw = h_rect.width() * 0.5;
let tri = vec![
egui::pos2(cx, bot),
egui::pos2(cx - hw, top),
egui::pos2(cx + hw, top),
];
painter.add(egui::Shape::convex_polygon(
tri,
col,
Stroke::new(if is_selected { 2.0 } else { 1.0 },
if is_selected { Color32::WHITE } else { Color32::from_gray(100) }),
));
}
// ── Selected stop detail ──────────────────────────────────────────────
if let Some(i) = *selected_stop {
if i < gradient.stops.len() {
ui.separator();
ui.horizontal(|ui| {
let stop = &mut gradient.stops[i];
let mut rgba = [stop.color.r, stop.color.g, stop.color.b, stop.color.a];
if ui.color_edit_button_srgba_unmultiplied(&mut rgba).changed() {
stop.color = ShapeColor::rgba(rgba[0], rgba[1], rgba[2], rgba[3]);
changed = true;
}
ui.label("Position:");
if ui.add(
DragValue::new(&mut stop.position)
.speed(0.005)
.range(0.0..=1.0),
).changed() {
gradient.stops.sort_by(|a, b| a.position.partial_cmp(&b.position).unwrap());
changed = true;
}
let can_remove = gradient.stops.len() > 2;
if ui.add_enabled(can_remove, egui::Button::new("− Remove")).clicked() {
gradient.stops.remove(i);
*selected_stop = None;
changed = true;
}
});
} else {
*selected_stop = None;
}
}
// ── Extend mode ───────────────────────────────────────────────────────
ui.horizontal(|ui| {
ui.label("Extend:");
if ui.selectable_label(gradient.extend == GradientExtend::Pad, "Pad").clicked() {
gradient.extend = GradientExtend::Pad; changed = true;
}
if ui.selectable_label(gradient.extend == GradientExtend::Reflect, "Reflect").clicked() {
gradient.extend = GradientExtend::Reflect; changed = true;
}
if ui.selectable_label(gradient.extend == GradientExtend::Repeat, "Repeat").clicked() {
gradient.extend = GradientExtend::Repeat; changed = true;
}
});
changed
}
// ── Helpers ──────────────────────────────────────────────────────────────────
fn ShapeColor_to_Color32(c: ShapeColor) -> Color32 {
Color32::from_rgba_unmultiplied(c.r, c.g, c.b, c.a)
}
/// Draw a small grey/white checkerboard inside `rect`.
fn draw_checker(painter: &Painter, rect: Rect) {
let cell = 6.0_f32;
let cols = ((rect.width() / cell).ceil() as u32).max(1);
let rows = ((rect.height() / cell).ceil() as u32).max(1);
for row in 0..rows {
for col in 0..cols {
let light = (row + col) % 2 == 0;
let col32 = if light { Color32::from_gray(200) } else { Color32::from_gray(140) };
let x = rect.min.x + col as f32 * cell;
let y = rect.min.y + row as f32 * cell;
let r = Rect::from_min_size(
egui::pos2(x, y),
Vec2::splat(cell),
).intersect(rect);
painter.rect_filled(r, 0.0, col32);
}
}
}
@@ -11,12 +11,15 @@
/// - Document settings (when nothing is focused)
use eframe::egui::{self, DragValue, Ui};
use lightningbeam_core::actions::{SetDocumentPropertiesAction, SetShapePropertiesAction};
use lightningbeam_core::brush_settings::{bundled_brushes, BrushSettings};
use lightningbeam_core::actions::{SetDocumentPropertiesAction, SetShapePropertiesAction, SetFillPaintAction};
use lightningbeam_core::gradient::ShapeGradient;
use lightningbeam_core::layer::{AnyLayer, LayerTrait};
use lightningbeam_core::selection::FocusSelection;
use lightningbeam_core::shape::ShapeColor;
use lightningbeam_core::tool::{SimplifyMode, Tool};
use super::{NodePath, PaneRenderer, SharedPaneState};
use super::gradient_editor::gradient_stop_editor;
use uuid::Uuid;
/// Info panel pane state
@@ -25,13 +28,36 @@ pub struct InfopanelPane {
tool_section_open: bool,
/// Whether the shape properties section is expanded
shape_section_open: bool,
/// Index of the selected paint brush preset (None = custom / unset)
selected_brush_preset: Option<usize>,
/// Whether the paint brush picker is expanded
brush_picker_expanded: bool,
/// Index of the selected eraser brush preset
selected_eraser_preset: Option<usize>,
/// Whether the eraser brush picker is expanded
eraser_picker_expanded: bool,
/// Cached preview textures, one per preset (populated lazily).
brush_preview_textures: Vec<egui::TextureHandle>,
/// Selected stop index for gradient editor in shape section.
selected_shape_gradient_stop: Option<usize>,
/// Selected stop index for gradient editor in tool section (gradient tool).
selected_tool_gradient_stop: Option<usize>,
}
impl InfopanelPane {
pub fn new() -> Self {
let presets = bundled_brushes();
let default_eraser_idx = presets.iter().position(|p| p.name == "Brush");
Self {
tool_section_open: true,
shape_section_open: true,
selected_brush_preset: None,
brush_picker_expanded: false,
selected_eraser_preset: default_eraser_idx,
eraser_picker_expanded: false,
brush_preview_textures: Vec::new(),
selected_shape_gradient_stop: None,
selected_tool_gradient_stop: None,
}
}
}
@@ -47,6 +73,8 @@ struct SelectionInfo {
// Shape property values (None = mixed)
fill_color: Option<Option<ShapeColor>>,
/// None = mixed across selection; Some(None) = no gradient; Some(Some(g)) = all same gradient
fill_gradient: Option<Option<ShapeGradient>>,
stroke_color: Option<Option<ShapeColor>>,
stroke_width: Option<f64>,
}
@@ -58,6 +86,7 @@ impl Default for SelectionInfo {
dcel_count: 0,
layer_id: None,
fill_color: None,
fill_gradient: None,
stroke_color: None,
stroke_width: None,
}
@@ -120,21 +149,32 @@ impl InfopanelPane {
// Gather fill properties from selected faces
let mut first_fill_color: Option<Option<ShapeColor>> = None;
let mut fill_color_mixed = false;
let mut first_fill_gradient: Option<Option<ShapeGradient>> = None;
let mut fill_gradient_mixed = false;
for &fid in shared.selection.selected_faces() {
let face = dcel.face(fid);
let fc = face.fill_color;
let fg = face.gradient_fill.clone();
match first_fill_color {
None => first_fill_color = Some(fc),
Some(prev) if prev != fc => fill_color_mixed = true,
_ => {}
}
match &first_fill_gradient {
None => first_fill_gradient = Some(fg),
Some(prev) if *prev != fg => fill_gradient_mixed = true,
_ => {}
}
}
if !fill_color_mixed {
info.fill_color = first_fill_color;
}
if !fill_gradient_mixed {
info.fill_gradient = first_fill_gradient;
}
}
}
}
@@ -151,7 +191,8 @@ impl InfopanelPane {
.and_then(|id| shared.action_executor.document().get_layer(&id))
.map_or(false, |l| matches!(l, AnyLayer::Raster(_)));
let is_raster_paint_tool = active_is_raster && matches!(tool, Tool::Draw | Tool::Erase | Tool::Smudge);
let raster_tool_def = active_is_raster.then(|| crate::tools::raster_tool_def(&tool)).flatten();
let is_raster_paint_tool = raster_tool_def.is_some();
// Only show tool options for tools that have options
let is_vector_tool = !active_is_raster && matches!(
@@ -159,23 +200,30 @@ impl InfopanelPane {
Tool::Select | Tool::BezierEdit | Tool::Draw | Tool::Rectangle
| Tool::Ellipse | Tool::Line | Tool::Polygon
);
let has_options = is_vector_tool || is_raster_paint_tool || matches!(
let is_raster_transform = active_is_raster
&& matches!(tool, Tool::Transform)
&& shared.selection.raster_floating.is_some();
let is_raster_select = active_is_raster && matches!(tool, Tool::Select);
let is_raster_shape = active_is_raster && matches!(
tool,
Tool::PaintBucket | Tool::RegionSelect
Tool::Rectangle | Tool::Ellipse | Tool::Line | Tool::Polygon
);
let has_options = is_vector_tool || is_raster_paint_tool || is_raster_transform
|| is_raster_select || is_raster_shape || matches!(
tool,
Tool::PaintBucket | Tool::RegionSelect | Tool::MagicWand | Tool::QuickSelect
| Tool::Warp | Tool::Liquify | Tool::Gradient
);
if !has_options {
return;
}
let header_label = if is_raster_paint_tool {
match tool {
Tool::Erase => "Eraser",
Tool::Smudge => "Smudge",
_ => "Brush",
}
let header_label = if is_raster_transform {
"Raster Transform"
} else {
"Tool Options"
raster_tool_def.map(|d| d.header_label()).unwrap_or("Tool Options")
};
egui::CollapsingHeader::new(header_label)
@@ -190,6 +238,23 @@ impl InfopanelPane {
ui.add_space(2.0);
}
// Raster transform tool hint.
if is_raster_transform {
ui.label("Drag handles to move, scale, or rotate.");
ui.add_space(4.0);
ui.label("Enter — apply Esc — cancel");
ui.add_space(4.0);
return;
}
// Raster paint tool: delegate to per-tool impl.
if let Some(def) = raster_tool_def {
def.render_ui(ui, shared.raster_settings);
if def.show_brush_preset_picker() {
self.render_raster_tool_options(ui, shared, def.is_eraser());
}
}
match tool {
Tool::Draw if !is_raster_paint_tool => {
// Stroke width
@@ -242,17 +307,192 @@ impl InfopanelPane {
}
Tool::PaintBucket => {
// Gap tolerance
if active_is_raster {
use crate::tools::FillThresholdMode;
ui.horizontal(|ui| {
ui.label("Threshold:");
ui.add(
egui::Slider::new(
&mut shared.raster_settings.fill_threshold,
0.0_f32..=255.0,
)
.step_by(1.0),
);
});
ui.horizontal(|ui| {
ui.label("Softness:");
ui.add(
egui::Slider::new(
&mut shared.raster_settings.fill_softness,
0.0_f32..=100.0,
)
.custom_formatter(|v, _| format!("{:.0}%", v)),
);
});
ui.horizontal(|ui| {
ui.label("Mode:");
ui.selectable_value(
&mut shared.raster_settings.fill_threshold_mode,
FillThresholdMode::Absolute,
"Absolute",
);
ui.selectable_value(
&mut shared.raster_settings.fill_threshold_mode,
FillThresholdMode::Relative,
"Relative",
);
});
} else {
// Vector: gap tolerance
ui.horizontal(|ui| {
ui.label("Gap Tolerance:");
ui.add(
DragValue::new(shared.paint_bucket_gap_tolerance)
.speed(0.1)
.range(0.0..=50.0),
);
});
}
}
Tool::Select if is_raster_select => {
use crate::tools::SelectionShape;
ui.horizontal(|ui| {
ui.label("Gap Tolerance:");
ui.add(
DragValue::new(shared.paint_bucket_gap_tolerance)
.speed(0.1)
.range(0.0..=50.0),
ui.label("Shape:");
ui.selectable_value(
&mut shared.raster_settings.select_shape,
SelectionShape::Rect,
"Rectangle",
);
ui.selectable_value(
&mut shared.raster_settings.select_shape,
SelectionShape::Ellipse,
"Ellipse",
);
});
}
Tool::MagicWand => {
use crate::tools::FillThresholdMode;
ui.horizontal(|ui| {
ui.label("Threshold:");
ui.add(
egui::Slider::new(
&mut shared.raster_settings.wand_threshold,
0.0_f32..=255.0,
)
.step_by(1.0),
);
});
ui.horizontal(|ui| {
ui.label("Mode:");
ui.selectable_value(
&mut shared.raster_settings.wand_mode,
FillThresholdMode::Absolute,
"Absolute",
);
ui.selectable_value(
&mut shared.raster_settings.wand_mode,
FillThresholdMode::Relative,
"Relative",
);
});
ui.checkbox(&mut shared.raster_settings.wand_contiguous, "Contiguous");
}
Tool::QuickSelect => {
use crate::tools::FillThresholdMode;
ui.horizontal(|ui| {
ui.label("Radius:");
ui.add(
egui::Slider::new(
&mut shared.raster_settings.quick_select_radius,
1.0_f32..=200.0,
)
.step_by(1.0),
);
});
ui.horizontal(|ui| {
ui.label("Threshold:");
ui.add(
egui::Slider::new(
&mut shared.raster_settings.wand_threshold,
0.0_f32..=255.0,
)
.step_by(1.0),
);
});
ui.horizontal(|ui| {
ui.label("Mode:");
ui.selectable_value(
&mut shared.raster_settings.wand_mode,
FillThresholdMode::Absolute,
"Absolute",
);
ui.selectable_value(
&mut shared.raster_settings.wand_mode,
FillThresholdMode::Relative,
"Relative",
);
});
}
Tool::Warp => {
ui.horizontal(|ui| {
ui.label("Grid:");
let cols = shared.raster_settings.warp_grid_cols;
let rows = shared.raster_settings.warp_grid_rows;
for (label, c, r) in [("3×3", 3u32, 3u32), ("4×4", 4, 4), ("5×5", 5, 5), ("8×8", 8, 8)] {
let selected = cols == c && rows == r;
if ui.selectable_label(selected, label).clicked() {
shared.raster_settings.warp_grid_cols = c;
shared.raster_settings.warp_grid_rows = r;
}
}
});
ui.small("Enter to commit · Escape to cancel");
}
Tool::Liquify => {
use crate::tools::LiquifyMode;
ui.horizontal(|ui| {
ui.label("Mode:");
for (label, mode) in [
("Push", LiquifyMode::Push),
("Pucker", LiquifyMode::Pucker),
("Bloat", LiquifyMode::Bloat),
("Smooth", LiquifyMode::Smooth),
("Reconstruct", LiquifyMode::Reconstruct),
] {
let selected = shared.raster_settings.liquify_mode == mode;
if ui.selectable_label(selected, label).clicked() {
shared.raster_settings.liquify_mode = mode;
}
}
});
ui.horizontal(|ui| {
ui.label("Radius:");
ui.add(
egui::Slider::new(
&mut shared.raster_settings.liquify_radius,
5.0_f32..=500.0,
)
.step_by(1.0),
);
});
ui.horizontal(|ui| {
ui.label("Strength:");
ui.add(
egui::Slider::new(
&mut shared.raster_settings.liquify_strength,
0.01_f32..=1.0,
)
.step_by(0.01),
);
});
ui.small("Enter to commit · Escape to cancel");
}
Tool::Polygon => {
// Number of sides
ui.horizontal(|ui| {
@@ -300,48 +540,20 @@ impl InfopanelPane {
});
}
// Raster paint tools
Tool::Draw | Tool::Erase | Tool::Smudge if is_raster_paint_tool => {
// Color source toggle (Draw tool only)
if matches!(tool, Tool::Draw) {
ui.horizontal(|ui| {
ui.label("Color:");
ui.selectable_value(shared.brush_use_fg, true, "FG");
ui.selectable_value(shared.brush_use_fg, false, "BG");
});
}
Tool::Gradient if active_is_raster => {
ui.horizontal(|ui| {
ui.label("Size:");
ui.add(
egui::Slider::new(shared.brush_radius, 1.0_f32..=200.0)
.logarithmic(true)
.suffix(" px"),
);
});
if !matches!(tool, Tool::Smudge) {
ui.horizontal(|ui| {
ui.label("Opacity:");
ui.add(
egui::Slider::new(shared.brush_opacity, 0.0_f32..=1.0)
.custom_formatter(|v, _| format!("{:.0}%", v * 100.0)),
);
});
}
ui.horizontal(|ui| {
ui.label("Hardness:");
ui.add(
egui::Slider::new(shared.brush_hardness, 0.0_f32..=1.0)
.custom_formatter(|v, _| format!("{:.0}%", v * 100.0)),
);
});
ui.horizontal(|ui| {
ui.label("Spacing:");
ui.add(
egui::Slider::new(shared.brush_spacing, 0.01_f32..=1.0)
.logarithmic(true)
.custom_formatter(|v, _| format!("{:.0}%", v * 100.0)),
);
ui.label("Opacity:");
ui.add(egui::Slider::new(
&mut shared.raster_settings.gradient_opacity,
0.0_f32..=1.0,
).custom_formatter(|v, _| format!("{:.0}%", v * 100.0)));
});
ui.add_space(4.0);
gradient_stop_editor(
ui,
&mut shared.raster_settings.gradient,
&mut self.selected_tool_gradient_stop,
);
}
_ => {}
@@ -351,6 +563,192 @@ impl InfopanelPane {
});
}
/// Render all options for a raster paint tool (brush picker + sliders).
/// `is_eraser` drives which shared state is read/written.
fn render_raster_tool_options(
&mut self,
ui: &mut Ui,
shared: &mut SharedPaneState,
is_eraser: bool,
) {
self.render_brush_preset_grid(ui, shared, is_eraser);
ui.add_space(2.0);
let rs = &mut shared.raster_settings;
if !is_eraser {
ui.horizontal(|ui| {
ui.label("Color:");
ui.selectable_value(&mut rs.brush_use_fg, true, "FG");
ui.selectable_value(&mut rs.brush_use_fg, false, "BG");
});
}
macro_rules! field {
($eraser:ident, $brush:ident) => {
if is_eraser { &mut rs.$eraser } else { &mut rs.$brush }
}
}
ui.horizontal(|ui| {
ui.label("Size:");
ui.add(egui::Slider::new(field!(eraser_radius, brush_radius), 1.0_f32..=200.0).logarithmic(true).suffix(" px"));
});
ui.horizontal(|ui| {
ui.label("Opacity:");
ui.add(egui::Slider::new(field!(eraser_opacity, brush_opacity), 0.0_f32..=1.0)
.custom_formatter(|v, _| format!("{:.0}%", v * 100.0)));
});
ui.horizontal(|ui| {
ui.label("Hardness:");
ui.add(egui::Slider::new(field!(eraser_hardness, brush_hardness), 0.0_f32..=1.0)
.custom_formatter(|v, _| format!("{:.0}%", v * 100.0)));
});
ui.horizontal(|ui| {
ui.label("Spacing:");
ui.add(egui::Slider::new(field!(eraser_spacing, brush_spacing), 0.01_f32..=1.0)
.logarithmic(true)
.custom_formatter(|v, _| format!("{:.0}%", v * 100.0)));
});
}
/// Render the brush preset thumbnail grid (collapsible).
/// `is_eraser` drives which picker state and which shared settings are updated.
fn render_brush_preset_grid(&mut self, ui: &mut Ui, shared: &mut SharedPaneState, is_eraser: bool) {
let presets = bundled_brushes();
if presets.is_empty() { return; }
// Build preview TextureHandles from GPU-rendered pixel data when available.
if self.brush_preview_textures.len() != presets.len() {
if let Ok(previews) = shared.brush_preview_pixels.try_lock() {
if previews.len() == presets.len() {
self.brush_preview_textures.clear();
for (idx, (w, h, pixels)) in previews.iter().enumerate() {
let image = egui::ColorImage::from_rgba_premultiplied(
[*w as usize, *h as usize],
pixels,
);
let handle = ui.ctx().load_texture(
format!("brush_preview_{}", presets[idx].name),
image,
egui::TextureOptions::LINEAR,
);
self.brush_preview_textures.push(handle);
}
}
}
}
// Read picker state into locals to avoid multiple &mut self borrows.
let mut expanded = if is_eraser { self.eraser_picker_expanded } else { self.brush_picker_expanded };
let mut selected = if is_eraser { self.selected_eraser_preset } else { self.selected_brush_preset };
let gap = 3.0;
let cols = 2usize;
let avail_w = ui.available_width();
let cell_w = ((avail_w - gap * (cols as f32 - 1.0)) / cols as f32).max(50.0);
let cell_h = 80.0;
if !expanded {
// Collapsed: show just the currently selected preset as a single wide cell.
let show_idx = selected.unwrap_or(0);
if let Some(preset) = presets.get(show_idx) {
let full_w = avail_w.max(50.0);
let (rect, resp) = ui.allocate_exact_size(egui::vec2(full_w, cell_h), egui::Sense::click());
let painter = ui.painter();
let bg = if resp.hovered() {
egui::Color32::from_rgb(50, 56, 70)
} else {
egui::Color32::from_rgb(45, 65, 95)
};
painter.rect_filled(rect, 4.0, bg);
painter.rect_stroke(rect, 4.0, egui::Stroke::new(1.5, egui::Color32::from_rgb(80, 140, 220)), egui::StrokeKind::Middle);
let preview_rect = egui::Rect::from_min_size(
rect.min + egui::vec2(4.0, 4.0),
egui::vec2(rect.width() - 8.0, cell_h - 22.0),
);
if let Some(tex) = self.brush_preview_textures.get(show_idx) {
painter.image(tex.id(), preview_rect,
egui::Rect::from_min_max(egui::pos2(0.0, 0.0), egui::pos2(1.0, 1.0)),
egui::Color32::WHITE);
}
painter.text(egui::pos2(rect.center().x, rect.max.y - 9.0),
egui::Align2::CENTER_CENTER, preset.name,
egui::FontId::proportional(9.5), egui::Color32::from_rgb(140, 190, 255));
if resp.clicked() { expanded = true; }
}
} else {
// Expanded: full grid; clicking a preset selects it and collapses.
for (row_idx, chunk) in presets.chunks(cols).enumerate() {
ui.horizontal(|ui| {
ui.spacing_mut().item_spacing.x = gap;
for (col_idx, preset) in chunk.iter().enumerate() {
let idx = row_idx * cols + col_idx;
let is_sel = selected == Some(idx);
let (rect, resp) = ui.allocate_exact_size(egui::vec2(cell_w, cell_h), egui::Sense::click());
let painter = ui.painter();
let bg = if is_sel {
egui::Color32::from_rgb(45, 65, 95)
} else if resp.hovered() {
egui::Color32::from_rgb(45, 50, 62)
} else {
egui::Color32::from_rgb(32, 36, 44)
};
painter.rect_filled(rect, 4.0, bg);
if is_sel {
painter.rect_stroke(rect, 4.0, egui::Stroke::new(1.5, egui::Color32::from_rgb(80, 140, 220)), egui::StrokeKind::Middle);
}
let preview_rect = egui::Rect::from_min_size(
rect.min + egui::vec2(4.0, 4.0),
egui::vec2(cell_w - 8.0, cell_h - 22.0),
);
if let Some(tex) = self.brush_preview_textures.get(idx) {
painter.image(tex.id(), preview_rect,
egui::Rect::from_min_max(egui::pos2(0.0, 0.0), egui::pos2(1.0, 1.0)),
egui::Color32::WHITE);
}
painter.text(egui::pos2(rect.center().x, rect.max.y - 9.0),
egui::Align2::CENTER_CENTER, preset.name,
egui::FontId::proportional(9.5),
if is_sel { egui::Color32::from_rgb(140, 190, 255) } else { egui::Color32::from_gray(160) });
if resp.clicked() {
selected = Some(idx);
expanded = false;
let s = &preset.settings;
let rs = &mut shared.raster_settings;
if is_eraser {
rs.eraser_opacity = s.opaque.clamp(0.0, 1.0);
rs.eraser_hardness = s.hardness.clamp(0.0, 1.0);
rs.eraser_spacing = s.dabs_per_radius;
rs.active_eraser_settings = s.clone();
} else {
rs.brush_opacity = s.opaque.clamp(0.0, 1.0);
rs.brush_hardness = s.hardness.clamp(0.0, 1.0);
rs.brush_spacing = s.dabs_per_radius;
rs.active_brush_settings = s.clone();
// If the user was on a preset-backed tool (Pencil/Pen/Airbrush)
// and manually picked a different brush, revert to the generic tool.
if matches!(*shared.selected_tool, Tool::Pencil | Tool::Pen | Tool::Airbrush) {
*shared.selected_tool = Tool::Draw;
}
}
}
}
});
ui.add_space(gap);
}
}
// Write back picker state.
if is_eraser {
self.eraser_picker_expanded = expanded;
self.selected_eraser_preset = selected;
} else {
self.brush_picker_expanded = expanded;
self.selected_brush_preset = selected;
}
}
// Transform section: deferred to Phase 2 (DCEL elements don't have instance transforms)
/// Render shape properties section (fill/stroke)
@@ -377,28 +775,72 @@ impl InfopanelPane {
self.shape_section_open = true;
ui.add_space(4.0);
// Fill color
// Fill — determine current fill type
let has_gradient = matches!(&info.fill_gradient, Some(Some(_)));
let has_solid = matches!(&info.fill_color, Some(Some(_)));
let fill_is_none = matches!(&info.fill_color, Some(None))
&& matches!(&info.fill_gradient, Some(None));
let fill_mixed = info.fill_color.is_none() && info.fill_gradient.is_none();
// Fill type toggle row
ui.horizontal(|ui| {
ui.label("Fill:");
match info.fill_color {
Some(Some(color)) => {
if fill_mixed {
ui.label("--");
} else {
if ui.selectable_label(fill_is_none, "None").clicked() && !fill_is_none {
let action = SetFillPaintAction::solid(
layer_id, time, face_ids.clone(), None,
);
shared.pending_actions.push(Box::new(action));
}
if ui.selectable_label(has_solid || (!has_gradient && !fill_is_none), "Solid").clicked() && !has_solid {
// Switch to solid: use existing color or default to black
let color = info.fill_color.flatten()
.unwrap_or(ShapeColor::rgba(0, 0, 0, 255));
let action = SetFillPaintAction::solid(
layer_id, time, face_ids.clone(), Some(color),
);
shared.pending_actions.push(Box::new(action));
}
if ui.selectable_label(has_gradient, "Gradient").clicked() && !has_gradient {
let grad = info.fill_gradient.clone().flatten()
.unwrap_or_default();
let action = SetFillPaintAction::gradient(
layer_id, time, face_ids.clone(), Some(grad),
);
shared.pending_actions.push(Box::new(action));
}
}
});
// Solid fill color editor
if !fill_mixed && has_solid {
if let Some(Some(color)) = info.fill_color {
ui.horizontal(|ui| {
let mut rgba = [color.r, color.g, color.b, color.a];
if ui.color_edit_button_srgba_unmultiplied(&mut rgba).changed() {
let new_color = ShapeColor::rgba(rgba[0], rgba[1], rgba[2], rgba[3]);
let action = SetShapePropertiesAction::set_fill_color(
let action = SetFillPaintAction::solid(
layer_id, time, face_ids.clone(), Some(new_color),
);
shared.pending_actions.push(Box::new(action));
}
}
Some(None) => {
ui.label("None");
}
None => {
ui.label("--");
});
}
}
// Gradient fill editor
if !fill_mixed && has_gradient {
if let Some(Some(mut grad)) = info.fill_gradient.clone() {
if gradient_stop_editor(ui, &mut grad, &mut self.selected_shape_gradient_stop) {
let action = SetFillPaintAction::gradient(
layer_id, time, face_ids.clone(), Some(grad),
);
shared.pending_actions.push(Box::new(action));
}
}
});
}
// Stroke color
ui.horizontal(|ui| {
@@ -864,6 +1306,40 @@ impl InfopanelPane {
}
}
/// Draw a brush dab preview into `rect` approximating the brush falloff shape.
///
/// Renders N concentric filled circles from outermost to innermost. Because each
/// inner circle overwrites the pixels of all outer circles beneath it, the visible
/// alpha at distance `d` from the centre equals the alpha of the innermost circle
/// whose radius ≥ `d`. This step-approximates the actual brush falloff formula:
/// `opa = ((1 − r) / (1 − hardness))²` for `r > hardness`, 1 inside the hard core.
fn paint_brush_dab(painter: &egui::Painter, rect: egui::Rect, s: &BrushSettings) {
let center = rect.center();
let max_r = (rect.width().min(rect.height()) / 2.0 - 2.0).max(1.0);
let h = s.hardness;
let a = s.opaque;
const N: usize = 12;
for i in 0..N {
// t: normalized radial position of this ring, 1.0 = outermost edge
let t = 1.0 - i as f32 / N as f32;
let r = max_r * t;
let opa_weight = if h >= 1.0 || t <= h {
1.0f32
} else {
let x = (1.0 - t) / (1.0 - h).max(1e-4);
(x * x).min(1.0)
};
let alpha = (opa_weight * a * 220.0).min(220.0) as u8;
painter.circle_filled(
center, r,
egui::Color32::from_rgba_unmultiplied(200, 200, 220, alpha),
);
}
}
/// Convert MIDI note number to note name (e.g. 60 -> "C4")
fn midi_note_name(note: u8) -> String {
const NAMES: [&str; 12] = ["C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"];
@@ -68,6 +68,7 @@ pub enum WebcamRecordCommand {
pub mod toolbar;
pub mod stage;
pub mod gradient_editor;
pub mod timeline;
pub mod infopanel;
pub mod outliner;
@@ -187,13 +188,8 @@ pub struct SharedPaneState<'a> {
pub draw_simplify_mode: &'a mut lightningbeam_core::tool::SimplifyMode,
pub rdp_tolerance: &'a mut f64,
pub schneider_max_error: &'a mut f64,
/// Raster brush settings
pub brush_radius: &'a mut f32,
pub brush_opacity: &'a mut f32,
pub brush_hardness: &'a mut f32,
pub brush_spacing: &'a mut f32,
/// Whether the brush paints with the foreground (fill) color (true) or background (stroke) color (false)
pub brush_use_fg: &'a mut bool,
/// All per-tool raster paint settings (replaces 20+ individual fields).
pub raster_settings: &'a mut crate::tools::RasterToolSettings,
/// Audio engine controller for playback control (wrapped in Arc<Mutex<>> for thread safety)
pub audio_controller: Option<&'a std::sync::Arc<std::sync::Mutex<daw_backend::EngineController>>>,
/// Video manager for video decoding and frame caching
@@ -269,6 +265,8 @@ pub struct SharedPaneState<'a> {
pub region_selection: &'a mut Option<lightningbeam_core::selection::RegionSelection>,
/// Region select mode (Rectangle or Lasso)
pub region_select_mode: &'a mut lightningbeam_core::tool::RegionSelectMode,
/// Lasso select sub-mode (Freehand / Polygonal / Magnetic)
pub lasso_mode: &'a mut lightningbeam_core::tool::LassoMode,
/// Counter for in-flight graph preset loads — increment when sending a
/// GraphLoadPreset command so the repaint loop stays alive until the
/// audio thread sends GraphPresetLoaded back
@@ -291,6 +289,10 @@ pub struct SharedPaneState<'a> {
/// Synthetic input from test mode replay (debug builds only)
#[cfg(debug_assertions)]
pub synthetic_input: &'a mut Option<crate::test_mode::SyntheticInput>,
/// GPU-rendered brush preview thumbnails. Populated by `VelloCallback::prepare()`
/// on the first frame; panes (e.g. infopanel) convert the pixel data to egui
/// TextureHandles. Each entry is `(width, height, sRGB-premultiplied RGBA bytes)`.
pub brush_preview_pixels: &'a std::sync::Arc<std::sync::Mutex<Vec<(u32, u32, Vec<u8>)>>>,
}
/// Trait for pane rendering
@@ -0,0 +1,27 @@
// Alpha composite compute shader.
//
// Composites the accumulated-dab scratch buffer C on top of the source buffer A,
// writing the result into the output buffer B:
//
// B[px] = C[px] + A[px] * (1 − C[px].a) (Porter-Duff src-over, C over A)
//
// All textures are Rgba8Unorm, linear premultiplied RGBA.
// Dispatch: ceil(w/8) × ceil(h/8) × 1.
@group(0) @binding(0) var tex_a: texture_2d<f32>; // source (A)
@group(0) @binding(1) var tex_c: texture_2d<f32>; // accumulated dabs (C)
@group(0) @binding(2) var tex_b: texture_storage_2d<rgba8unorm, write>; // output (B)
@compute @workgroup_size(8, 8)
fn main(@builtin(global_invocation_id) gid: vec3<u32>) {
let dims = textureDimensions(tex_a);
if gid.x >= dims.x || gid.y >= dims.y { return; }
let coord = vec2<i32>(i32(gid.x), i32(gid.y));
let a = textureLoad(tex_a, coord, 0);
let c = textureLoad(tex_c, coord, 0);
// Porter-Duff src-over: C is the foreground (dabs), A is the background.
// out = c + a * (1 - c.a)
textureStore(tex_b, coord, c + a * (1.0 - c.a));
}
@@ -20,7 +20,7 @@ struct GpuDab {
x: f32, y: f32, radius: f32, hardness: f32, // bytes 0–15
opacity: f32, color_r: f32, color_g: f32, color_b: f32, // bytes 16–31
color_a: f32, ndx: f32, ndy: f32, smudge_dist: f32, // bytes 32–47
blend_mode: u32, _pad0: u32, _pad1: u32, _pad2: u32, // bytes 48–63
blend_mode: u32, elliptical_dab_ratio: f32, elliptical_dab_angle: f32, lock_alpha: f32, // bytes 48–63
}
struct Params {
@@ -76,7 +76,20 @@ fn bilinear_sample(px: f32, py: f32) -> vec4<f32> {
fn apply_dab(current: vec4<f32>, dab: GpuDab, px: i32, py: i32) -> vec4<f32> {
let dx = f32(px) + 0.5 - dab.x;
let dy = f32(py) + 0.5 - dab.y;
let rr = (dx * dx + dy * dy) / (dab.radius * dab.radius);
// Normalised squared distance — supports circular and elliptical dabs.
var rr: f32;
if dab.elliptical_dab_ratio > 1.001 {
// Rotate into the dab's local frame.
// Major axis is along dab.elliptical_dab_angle; minor axis is compressed by ratio.
let c = cos(dab.elliptical_dab_angle);
let s = sin(dab.elliptical_dab_angle);
let dx_r = dx * c + dy * s; // along major axis
let dy_r = (-dx * s + dy * c) * dab.elliptical_dab_ratio; // minor axis compressed
rr = (dx_r * dx_r + dy_r * dy_r) / (dab.radius * dab.radius);
} else {
rr = (dx * dx + dy * dy) / (dab.radius * dab.radius);
}
if rr > 1.0 { return current; }
// Quadratic falloff: flat inner core, smooth quadratic outer zone.
@@ -94,15 +107,17 @@ fn apply_dab(current: vec4<f32>, dab: GpuDab, px: i32, py: i32) -> vec4<f32> {
}
if dab.blend_mode == 0u {
// Normal: "over" operator
// Normal: "over" operator on premultiplied RGBA.
// If lock_alpha > 0.5, preserve the destination alpha unchanged.
let dab_a = opa_weight * dab.opacity * dab.color_a;
if dab_a <= 0.0 { return current; }
let ba = 1.0 - dab_a;
let out_a = select(dab_a + ba * current.a, current.a, dab.lock_alpha > 0.5);
return vec4<f32>(
dab_a * dab.color_r + ba * current.r,
dab_a * dab.color_g + ba * current.g,
dab_a * dab.color_b + ba * current.b,
dab_a + ba * current.a,
out_a,
);
} else if dab.blend_mode == 1u {
// Erase: multiplicative alpha reduction
@@ -111,7 +126,7 @@ fn apply_dab(current: vec4<f32>, dab: GpuDab, px: i32, py: i32) -> vec4<f32> {
let new_a = current.a * (1.0 - dab_a);
let scale = select(0.0, new_a / current.a, current.a > 1e-6);
return vec4<f32>(current.r * scale, current.g * scale, current.b * scale, new_a);
} else {
} else if dab.blend_mode == 2u {
// Smudge: directional warp — sample from position behind the stroke direction
let alpha = opa_weight * dab.opacity;
if alpha <= 0.0 { return current; }
@@ -125,6 +140,192 @@ fn apply_dab(current: vec4<f32>, dab: GpuDab, px: i32, py: i32) -> vec4<f32> {
alpha * src.b + da * current.b,
alpha * src.a + da * current.a,
);
} else if dab.blend_mode == 3u {
// Clone stamp: sample from (this_pixel + offset) in the source canvas.
// color_r/color_g store the world-space offset (source_world - drag_start_world)
// computed once when the stroke begins. Each pixel samples its own source texel.
let alpha = opa_weight * dab.opacity;
if alpha <= 0.0 { return current; }
let src_x = f32(px) + 0.5 + dab.color_r;
let src_y = f32(py) + 0.5 + dab.color_g;
let src = bilinear_sample(src_x, src_y);
let ba = 1.0 - alpha;
return vec4<f32>(
alpha * src.r + ba * current.r,
alpha * src.g + ba * current.g,
alpha * src.b + ba * current.b,
alpha * src.a + ba * current.a,
);
} else if dab.blend_mode == 5u {
// Pattern stamp: procedural tiling pattern using brush color.
// ndx = pattern_type (0=Checker, 1=Dots, 2=H-Lines, 3=V-Lines, 4=Diagonal, 5=Crosshatch)
// ndy = pattern_scale (tile size in pixels, >= 1.0)
let scale = max(dab.ndy, 1.0);
let pt = u32(dab.ndx);
// Fractional position within the tile [0.0, 1.0)
let tx = fract(f32(px) / scale);
let ty = fract(f32(py) / scale);
var on: bool;
if pt == 0u { // Checkerboard
let cx = u32(floor(f32(px) / scale));
let cy = u32(floor(f32(py) / scale));
on = (cx + cy) % 2u == 0u;
} else if pt == 1u { // Polka dots (r ≈ 0.35 of cell radius)
let ddx = tx - 0.5; let ddy = ty - 0.5;
on = ddx * ddx + ddy * ddy < 0.1225;
} else if pt == 2u { // Horizontal lines (50% duty)
on = ty < 0.5;
} else if pt == 3u { // Vertical lines (50% duty)
on = tx < 0.5;
} else if pt == 4u { // Diagonal \ (top-left → bottom-right)
on = fract((f32(px) + f32(py)) / scale) < 0.5;
} else if pt == 5u { // Diagonal / (top-right → bottom-left)
on = fract((f32(px) - f32(py)) / scale) < 0.5;
} else { // Crosshatch (type 6+)
on = tx < 0.4 || ty < 0.4;
}
if !on { return current; }
// Paint with brush color — same compositing as Normal blend
let dab_a = opa_weight * dab.opacity * dab.color_a;
if dab_a <= 0.0 { return current; }
let ba = 1.0 - dab_a;
return vec4<f32>(
dab_a * dab.color_r + ba * current.r,
dab_a * dab.color_g + ba * current.g,
dab_a * dab.color_b + ba * current.b,
dab_a + ba * current.a,
);
} else if dab.blend_mode == 4u {
// Healing brush: per-pixel color-corrected clone stamp.
// color_r/color_g = source offset (ox, oy), same as clone stamp.
// For each pixel: result = src_pixel + (local_dest_mean - local_src_mean)
// Means are computed from 4 cardinal neighbors at ±half-radius — per-pixel, no banding.
let alpha = opa_weight * dab.opacity;
if alpha <= 0.0 { return current; }
let cw = i32(params.canvas_w);
let ch = i32(params.canvas_h);
let ox = dab.color_r;
let oy = dab.color_g;
let hr = max(dab.radius * 0.5, 1.0);
let ihr = i32(hr);
// Per-pixel DESTINATION mean: 4 cardinal neighbors from canvas_src (pre-batch state)
let d_n = textureLoad(canvas_src, vec2<i32>(px, clamp(py - ihr, 0, ch - 1)), 0);
let d_s = textureLoad(canvas_src, vec2<i32>(px, clamp(py + ihr, 0, ch - 1)), 0);
let d_w = textureLoad(canvas_src, vec2<i32>(clamp(px - ihr, 0, cw - 1), py ), 0);
let d_e = textureLoad(canvas_src, vec2<i32>(clamp(px + ihr, 0, cw - 1), py ), 0);
let d_mean = (d_n + d_s + d_w + d_e) * 0.25;
// Per-pixel SOURCE mean: 4 cardinal neighbors at offset position (bilinear for sub-pixel offsets)
let spx = f32(px) + 0.5 + ox;
let spy = f32(py) + 0.5 + oy;
let s_mean = (bilinear_sample(spx, spy - hr)
+ bilinear_sample(spx, spy + hr)
+ bilinear_sample(spx - hr, spy )
+ bilinear_sample(spx + hr, spy )) * 0.25;
// Source pixel + color correction
let s_pixel = bilinear_sample(spx, spy);
let corrected = clamp(s_pixel + (d_mean - s_mean), vec4<f32>(0.0), vec4<f32>(1.0));
let ba = 1.0 - alpha;
return vec4<f32>(
alpha * corrected.r + ba * current.r,
alpha * corrected.g + ba * current.g,
alpha * corrected.b + ba * current.b,
alpha * corrected.a + ba * current.a,
);
} else if dab.blend_mode == 6u {
// Dodge / Burn: power-curve exposure adjustment.
// color_r: 0.0 = dodge, 1.0 = burn
// Uses pow(channel, gamma) which is asymmetric across channels:
// burn (gamma > 1): low channels compressed toward 0 faster than high ones → saturation increases
// dodge (gamma < 1): low channels lifted faster than high ones → saturation decreases
// This matches the behaviour of GIMP / Photoshop dodge-burn tools.
let s = opa_weight * dab.opacity;
if s <= 0.0 { return current; }
let rgb = max(current.rgb, vec3<f32>(0.0));
var adjusted: vec3<f32>;
if dab.color_r < 0.5 {
// Dodge: gamma < 1 → brightens
adjusted = pow(rgb, vec3<f32>(max(1.0 - s, 0.001)));
} else {
// Burn: gamma > 1 → darkens and increases saturation
adjusted = pow(rgb, vec3<f32>(1.0 + s));
}
return vec4<f32>(clamp(adjusted, vec3<f32>(0.0), vec3<f32>(1.0)), current.a);
} else if dab.blend_mode == 7u {
// Sponge: saturate or desaturate existing pixels.
// color_r: 0.0 = saturate, 1.0 = desaturate
// Computes luminance, then moves RGB toward (desaturate) or away from (saturate) it.
let s = opa_weight * dab.opacity;
if s <= 0.0 { return current; }
let luma = dot(current.rgb, vec3<f32>(0.2126, 0.7152, 0.0722));
let luma_vec = vec3<f32>(luma);
var adjusted: vec3<f32>;
if dab.color_r < 0.5 {
// Saturate: push RGB away from luma (increase chroma)
adjusted = clamp(current.rgb + s * (current.rgb - luma_vec), vec3<f32>(0.0), vec3<f32>(1.0));
} else {
// Desaturate: blend RGB toward luma
adjusted = mix(current.rgb, luma_vec, s);
}
return vec4<f32>(adjusted, current.a);
} else if dab.blend_mode == 8u {
// Blur / Sharpen: 5×5 separable Gaussian kernel.
// color_r: 0.0 = blur, 1.0 = sharpen
// ndx: kernel radius in canvas pixels (> 0)
//
// Samples are placed on a grid at ±step and ±2*step per axis, where step = kr/2.
// Weights are exp(-x²/2σ²) with σ = step, factored as a separable product.
// This gives a true Gaussian falloff rather than a flat ring, so edges blend
// into a smooth gradient rather than a flat averaged zone.
let s = opa_weight * dab.opacity;
if s <= 0.0 { return current; }
let kr = max(dab.ndx, 1.0);
let cx2 = f32(px) + 0.5;
let cy2 = f32(py) + 0.5;
let step = kr * 0.5;
// 1-D Gaussian weights at distances 0, ±step, ±2*step (σ = step):
// exp(0) = 1.0, exp(-0.5) ≈ 0.6065, exp(-2.0) ≈ 0.1353
var gauss = array<f32, 5>(0.1353, 0.6065, 1.0, 0.6065, 0.1353);
var blur_sum = vec4<f32>(0.0);
var blur_w = 0.0;
for (var iy = 0; iy < 5; iy++) {
for (var ix = 0; ix < 5; ix++) {
let w = gauss[ix] * gauss[iy];
let spx = cx2 + (f32(ix) - 2.0) * step;
let spy = cy2 + (f32(iy) - 2.0) * step;
blur_sum += bilinear_sample(spx, spy) * w;
blur_w += w;
}
}
let blurred = blur_sum / blur_w;
let c = textureLoad(canvas_src, vec2<i32>(px, py), 0);
var result: vec4<f32>;
if dab.color_r < 0.5 {
// Blur: blend current toward the Gaussian-weighted local average.
result = mix(current, blurred, s);
} else {
// Sharpen: unsharp mask — push pixel away from the local average.
// sharpened = 2*src - blurred → highlights diverge, shadows diverge.
let sharpened = clamp(c * 2.0 - blurred, vec4<f32>(0.0), vec4<f32>(1.0));
result = mix(current, sharpened, s);
}
return result;
} else {
return current;
}
}
@@ -1,30 +1,31 @@
// Canvas blit shader.
//
// Renders a GPU raster canvas (at document resolution) into an Rgba16Float HDR
// buffer (at viewport resolution), applying the camera transform (pan + zoom)
// to map document-space pixels to viewport-space pixels.
// buffer (at viewport resolution), applying a general affine transform that maps
// viewport UV [0,1]² directly to canvas UV [0,1]².
//
// The combined inverse transform (viewport UV → canvas UV) is pre-computed on the
// CPU and uploaded as a column-major 3×3 matrix packed into three vec4 uniforms.
//
// The canvas stores premultiplied linear RGBA. We output it as-is so the HDR
// compositor sees the same premultiplied-linear format it always works with,
// bypassing the sRGB intermediate used for Vello layers.
//
// Any viewport pixel whose corresponding document coordinate falls outside
// [0, canvas_w) × [0, canvas_h) outputs transparent black.
// Any viewport pixel whose corresponding canvas coordinate falls outside [0,1)²
// outputs transparent black.
struct CameraParams {
pan_x: f32,
pan_y: f32,
zoom: f32,
canvas_w: f32,
canvas_h: f32,
viewport_w: f32,
viewport_h: f32,
_pad: f32,
struct BlitTransform {
/// Column 0 of the viewport_uv → canvas_uv affine matrix (+ padding).
col0: vec4<f32>,
/// Column 1 (+ padding).
col1: vec4<f32>,
/// Column 2: translation column — col2.xy = translation, col2.z = 1 (+ padding).
col2: vec4<f32>,
}
@group(0) @binding(0) var canvas_tex: texture_2d<f32>;
@group(0) @binding(1) var canvas_sampler: sampler;
@group(0) @binding(2) var<uniform> camera: CameraParams;
@group(0) @binding(2) var<uniform> transform: BlitTransform;
/// Selection mask: R8Unorm, 255 = inside selection (keep), 0 = outside (discard).
/// A 1×1 all-white texture is bound when no selection is active.
@group(0) @binding(3) var mask_tex: texture_2d<f32>;
@@ -48,14 +49,10 @@ fn vs_main(@builtin(vertex_index) vertex_index: u32) -> VertexOutput {
@fragment
fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
// Map viewport UV [0,1] → viewport pixel
let vp = in.uv * vec2<f32>(camera.viewport_w, camera.viewport_h);
// Map viewport pixel → document pixel (inverse camera transform)
let doc = (vp - vec2<f32>(camera.pan_x, camera.pan_y)) / camera.zoom;
// Map document pixel → canvas UV [0,1]
let canvas_uv = doc / vec2<f32>(camera.canvas_w, camera.canvas_h);
// Apply the combined inverse transform: viewport UV → canvas UV.
let m = mat3x3<f32>(transform.col0.xyz, transform.col1.xyz, transform.col2.xyz);
let canvas_uv_h = m * vec3<f32>(in.uv.x, in.uv.y, 1.0);
let canvas_uv = canvas_uv_h.xy;
// Out-of-bounds → transparent
if canvas_uv.x < 0.0 || canvas_uv.x > 1.0
@@ -0,0 +1,137 @@
// GPU gradient fill shader.
//
// Reads the anchor canvas (before_pixels), composites a gradient over it, and
// writes the result to the display canvas. All color values in the canvas are
// linear premultiplied RGBA. The stop colors passed via `stops` are linear
// straight-alpha [0..1] (sRGB→linear conversion is done on the CPU).
//
// Dispatch: ceil(canvas_w / 8) × ceil(canvas_h / 8) × 1
struct Params {
canvas_w: u32,
canvas_h: u32,
start_x: f32,
start_y: f32,
end_x: f32,
end_y: f32,
opacity: f32,
extend_mode: u32, // 0 = Pad, 1 = Reflect, 2 = Repeat
num_stops: u32,
kind: u32, // 0 = Linear, 1 = Radial
_pad1: u32,
_pad2: u32,
}
// 32 bytes per stop (8 × f32), matching `GpuGradientStop` on the Rust side.
struct GradientStop {
position: f32,
r: f32, // linear [0..1], straight-alpha
g: f32,
b: f32,
a: f32,
_pad0: f32,
_pad1: f32,
_pad2: f32,
}
@group(0) @binding(0) var<uniform> params: Params;
@group(0) @binding(1) var src: texture_2d<f32>;
@group(0) @binding(2) var<storage, read> stops: array<GradientStop>;
@group(0) @binding(3) var dst: texture_storage_2d<rgba8unorm, write>;
fn apply_extend(t: f32) -> f32 {
if params.extend_mode == 0u {
// Pad: clamp to [0, 1]
return clamp(t, 0.0, 1.0);
} else if params.extend_mode == 1u {
// Reflect: 0→1→0→1→...
let t_abs = abs(t);
let period = floor(t_abs);
let frac = t_abs - period;
if (u32(period) & 1u) == 0u {
return frac;
} else {
return 1.0 - frac;
}
} else {
// Repeat: tile [0, 1)
return t - floor(t);
}
}
fn eval_gradient(t: f32) -> vec4<f32> {
let n = params.num_stops;
if n == 0u { return vec4<f32>(0.0); }
let s0 = stops[0];
if t <= s0.position {
return vec4<f32>(s0.r, s0.g, s0.b, s0.a);
}
let sn = stops[n - 1u];
if t >= sn.position {
return vec4<f32>(sn.r, sn.g, sn.b, sn.a);
}
for (var i = 0u; i < n - 1u; i++) {
let sa = stops[i];
let sb = stops[i + 1u];
if t >= sa.position && t <= sb.position {
let span = sb.position - sa.position;
let f = select(0.0, (t - sa.position) / span, span > 0.0001);
return mix(
vec4<f32>(sa.r, sa.g, sa.b, sa.a),
vec4<f32>(sb.r, sb.g, sb.b, sb.a),
f,
);
}
}
return vec4<f32>(sn.r, sn.g, sn.b, sn.a);
}
@compute @workgroup_size(8, 8)
fn main(@builtin(global_invocation_id) gid: vec3<u32>) {
if gid.x >= params.canvas_w || gid.y >= params.canvas_h { return; }
// Anchor pixel (linear premultiplied RGBA).
let src_px = textureLoad(src, vec2<i32>(i32(gid.x), i32(gid.y)), 0);
let dx = params.end_x - params.start_x;
let dy = params.end_y - params.start_y;
let px = f32(gid.x) + 0.5;
let py = f32(gid.y) + 0.5;
var t_raw: f32 = 0.0;
if params.kind == 1u {
// Radial: center at start point, radius = |end-start|.
let radius = sqrt(dx * dx + dy * dy);
if radius >= 0.5 {
let pdx = px - params.start_x;
let pdy = py - params.start_y;
t_raw = sqrt(pdx * pdx + pdy * pdy) / radius;
}
} else {
// Linear: project pixel centre onto gradient axis (start → end).
let len2 = dx * dx + dy * dy;
if len2 >= 1.0 {
let fx = px - params.start_x;
let fy = py - params.start_y;
t_raw = (fx * dx + fy * dy) / len2;
}
}
let t = apply_extend(t_raw);
let grad = eval_gradient(t); // straight-alpha linear RGBA
// Effective alpha: gradient alpha × tool opacity.
let a = grad.a * params.opacity;
// Alpha-over composite.
// src_px.rgb is premultiplied (= straight_rgb * src_a).
// Output is also premultiplied.
let out_a = a + src_px.a * (1.0 - a);
let out_rgb = grad.rgb * a + src_px.rgb * (1.0 - a);
textureStore(dst, vec2<i32>(i32(gid.x), i32(gid.y)), vec4<f32>(out_rgb, out_a));
}
@@ -0,0 +1,92 @@
// GPU liquify-brush shader.
//
// Updates a per-pixel displacement map (array of vec2f) for one brush step.
// Each pixel within the brush radius receives a displacement contribution
// weighted by a Gaussian falloff.
//
// Modes:
// 0 = Push — displace in brush-drag direction (dx, dy)
// 1 = Pucker — pull toward brush center
// 2 = Bloat — push away from brush center
// 3 = Smooth — blend toward average of 4 cardinal neighbours
// 4 = Reconstruct — blend toward zero (gradually undo)
//
// Dispatch: ceil((2*radius+1) / 8) × ceil((2*radius+1) / 8) × 1
// The CPU clips invocation IDs to the valid map range.
struct Params {
cx: f32, // brush center x (canvas pixels)
cy: f32, // brush center y
radius: f32, // brush radius (canvas pixels)
strength: f32, // effect strength [0..1]
dx: f32, // push direction x (normalised by caller, Push mode only)
dy: f32, // push direction y
mode: u32, // 0=Push 1=Pucker 2=Bloat 3=Smooth 4=Reconstruct
map_w: u32,
map_h: u32,
_pad0: u32,
_pad1: u32,
}
@group(0) @binding(0) var<uniform> params: Params;
@group(0) @binding(1) var<storage, read_write> disp: array<vec2f>;
@compute @workgroup_size(8, 8)
fn main(@builtin(global_invocation_id) gid: vec3<u32>) {
// Offset invocation into the brush bounding box so gid(0,0) = (cx-r, cy-r).
let base_x = floor(params.cx - params.radius);
let base_y = floor(params.cy - params.radius);
let px = base_x + f32(gid.x);
let py = base_y + f32(gid.y);
// Clip to displacement map bounds.
if px < 0.0 || py < 0.0 { return; }
let map_x = u32(px);
let map_y = u32(py);
if map_x >= params.map_w || map_y >= params.map_h { return; }
let ddx = px - params.cx;
let ddy = py - params.cy;
let dist2 = ddx * ddx + ddy * ddy;
let r2 = params.radius * params.radius;
if dist2 > r2 { return; }
// Gaussian influence: 1 at center, ~0.01 at edge (sigma = radius/2.15)
let influence = params.strength * exp(-dist2 / (r2 * 0.2));
let idx = map_y * params.map_w + map_x;
var d = disp[idx];
switch params.mode {
case 0u: { // Push
d = d + vec2f(params.dx, params.dy) * influence * params.radius;
}
case 1u: { // Pucker — toward center
let len = sqrt(dist2) + 0.0001;
d = d + vec2f(-ddx / len, -ddy / len) * influence * params.radius;
}
case 2u: { // Bloat — away from center
let len = sqrt(dist2) + 0.0001;
d = d + vec2f(ddx / len, ddy / len) * influence * params.radius;
}
case 3u: { // Smooth — blend toward average of 4 neighbours
let xi = i32(map_x);
let yi = i32(map_y);
let w = i32(params.map_w);
let h = i32(params.map_h);
let l = disp[u32(clamp(yi, 0, h-1)) * params.map_w + u32(clamp(xi - 1, 0, w-1))];
let r = disp[u32(clamp(yi, 0, h-1)) * params.map_w + u32(clamp(xi + 1, 0, w-1))];
let u = disp[u32(clamp(yi - 1, 0, h-1)) * params.map_w + u32(clamp(xi, 0, w-1))];
let dn = disp[u32(clamp(yi + 1, 0, h-1)) * params.map_w + u32(clamp(xi, 0, w-1))];
let avg = (l + r + u + dn) * 0.25;
d = mix(d, avg, influence * 0.5);
}
case 4u: { // Reconstruct — blend toward zero
d = mix(d, vec2f(0.0), influence * 0.5);
}
default: {}
}
disp[idx] = d;
}
@@ -0,0 +1,78 @@
// GPU affine-transform resample shader.
//
// For each output pixel, computes the corresponding source pixel via an inverse
// 2D affine transform (no perspective) and bilinear-samples from the source texture.
//
// Used by the raster selection transform tool: the source is the immutable "anchor"
// canvas (original float pixels), the destination is the current float canvas.
//
// CPU precomputes the inverse affine matrix components and the output bounding box.
// The shader just does the per-pixel mapping and bilinear interpolation.
//
// Dispatch: ceil(dst_w / 8) × ceil(dst_h / 8) × 1
struct Params {
// Inverse affine: src_pixel = A * out_pixel + b
// For output pixel center (ox, oy), source pixel is:
// sx = a00*ox + a01*oy + b0
// sy = a10*ox + a11*oy + b1
a00: f32, a01: f32,
a10: f32, a11: f32,
b0: f32, b1: f32,
src_w: u32, src_h: u32,
dst_w: u32, dst_h: u32,
_pad0: u32, _pad1: u32, // pad to 48 bytes (3 × 16)
}
@group(0) @binding(0) var<uniform> params: Params;
@group(0) @binding(1) var src: texture_2d<f32>;
@group(0) @binding(2) var dst: texture_storage_2d<rgba8unorm, write>;
// Manual bilinear sample with clamp-to-edge (textureSample forbidden in compute shaders).
fn bilinear_sample(px: f32, py: f32) -> vec4<f32> {
let sw = i32(params.src_w);
let sh = i32(params.src_h);
let ix = i32(floor(px - 0.5));
let iy = i32(floor(py - 0.5));
let fx = fract(px - 0.5);
let fy = fract(py - 0.5);
let x0 = clamp(ix, 0, sw - 1);
let x1 = clamp(ix + 1, 0, sw - 1);
let y0 = clamp(iy, 0, sh - 1);
let y1 = clamp(iy + 1, 0, sh - 1);
let s00 = textureLoad(src, vec2<i32>(x0, y0), 0);
let s10 = textureLoad(src, vec2<i32>(x1, y0), 0);
let s01 = textureLoad(src, vec2<i32>(x0, y1), 0);
let s11 = textureLoad(src, vec2<i32>(x1, y1), 0);
return mix(mix(s00, s10, fx), mix(s01, s11, fx), fy);
}
@compute @workgroup_size(8, 8)
fn main(@builtin(global_invocation_id) gid: vec3<u32>) {
if gid.x >= params.dst_w || gid.y >= params.dst_h { return; }
let ox = f32(gid.x);
let oy = f32(gid.y);
// Map output pixel index → source pixel position via inverse affine.
// We use pixel centers (ox + 0.5, oy + 0.5) in the forward transform, but the
// b0/b1 precomputation on the CPU already accounts for the +0.5 offset, so ox/oy
// are used directly here (the CPU bakes +0.5 into b).
let sx = params.a00 * ox + params.a01 * oy + params.b0;
let sy = params.a10 * ox + params.a11 * oy + params.b1;
var color: vec4<f32>;
if sx < 0.0 || sy < 0.0 || sx >= f32(params.src_w) || sy >= f32(params.src_h) {
// Outside source bounds → transparent
color = vec4<f32>(0.0);
} else {
// Bilinear sample at pixel center
color = bilinear_sample(sx + 0.5, sy + 0.5);
}
textureStore(dst, vec2<i32>(i32(gid.x), i32(gid.y)), color);
}
@@ -0,0 +1,103 @@
// GPU warp-apply shader.
//
// Two modes selected by grid_cols / grid_rows:
//
// grid_cols == 0 (Liquify / per-pixel mode)
// disp[] is a full canvas-sized array<vec2f>. Each pixel reads its own entry.
//
// grid_cols > 0 (Warp control-point mode)
// disp[] contains only grid_cols * grid_rows vec2f displacements (one per
// control point). The shader bilinearly interpolates them so the CPU never
// needs to build or upload the full per-pixel buffer.
//
// Dispatch: ceil(dst_w / 8) × ceil(dst_h / 8) × 1
struct Params {
src_w: u32,
src_h: u32,
dst_w: u32,
dst_h: u32,
grid_cols: u32, // 0 = per-pixel mode
grid_rows: u32,
_pad0: u32,
_pad1: u32,
}
@group(0) @binding(0) var<uniform> params: Params;
@group(0) @binding(1) var src: texture_2d<f32>;
@group(0) @binding(2) var<storage, read> disp: array<vec2f>;
@group(0) @binding(3) var dst: texture_storage_2d<rgba8unorm, write>;
// Manual bilinear sample with clamp-to-edge (textureSample forbidden in compute shaders).
fn bilinear_sample(px: f32, py: f32) -> vec4<f32> {
let sw = i32(params.src_w);
let sh = i32(params.src_h);
let ix = i32(floor(px - 0.5));
let iy = i32(floor(py - 0.5));
let fx = fract(px - 0.5);
let fy = fract(py - 0.5);
let x0 = clamp(ix, 0, sw - 1);
let x1 = clamp(ix + 1, 0, sw - 1);
let y0 = clamp(iy, 0, sh - 1);
let y1 = clamp(iy + 1, 0, sh - 1);
let s00 = textureLoad(src, vec2<i32>(x0, y0), 0);
let s10 = textureLoad(src, vec2<i32>(x1, y0), 0);
let s01 = textureLoad(src, vec2<i32>(x0, y1), 0);
let s11 = textureLoad(src, vec2<i32>(x1, y1), 0);
return mix(mix(s00, s10, fx), mix(s01, s11, fx), fy);
}
// Bilinearly interpolate the control-point displacement grid.
fn grid_displacement(px: u32, py: u32) -> vec2f {
let cols = params.grid_cols;
let rows = params.grid_rows;
// Normalised position in grid space [0 .. cols-1] × [0 .. rows-1].
let gx = f32(px) / f32(params.dst_w - 1u) * f32(cols - 1u);
let gy = f32(py) / f32(params.dst_h - 1u) * f32(rows - 1u);
let col0 = u32(floor(gx));
let row0 = u32(floor(gy));
let col1 = min(col0 + 1u, cols - 1u);
let row1 = min(row0 + 1u, rows - 1u);
let fx = gx - floor(gx);
let fy = gy - floor(gy);
let d00 = disp[row0 * cols + col0];
let d10 = disp[row0 * cols + col1];
let d01 = disp[row1 * cols + col0];
let d11 = disp[row1 * cols + col1];
return d00 * (1.0 - fx) * (1.0 - fy)
+ d10 * fx * (1.0 - fy)
+ d01 * (1.0 - fx) * fy
+ d11 * fx * fy;
}
@compute @workgroup_size(8, 8)
fn main(@builtin(global_invocation_id) gid: vec3<u32>) {
if gid.x >= params.dst_w || gid.y >= params.dst_h { return; }
var d: vec2f;
if params.grid_cols > 0u {
d = grid_displacement(gid.x, gid.y);
} else {
d = disp[gid.y * params.dst_w + gid.x];
}
let sx = f32(gid.x) + d.x;
let sy = f32(gid.y) + d.y;
var color: vec4<f32>;
if sx < 0.0 || sy < 0.0 || sx >= f32(params.src_w) || sy >= f32(params.src_h) {
color = vec4<f32>(0.0);
} else {
color = bilinear_sample(sx + 0.5, sy + 0.5);
}
textureStore(dst, vec2<i32>(i32(gid.x), i32(gid.y)), color);
}
File diff suppressed because it is too large Load Diff
@@ -5,7 +5,8 @@
use eframe::egui;
use lightningbeam_core::layer::{AnyLayer, LayerType};
use lightningbeam_core::tool::{Tool, RegionSelectMode};
use lightningbeam_core::tool::{Tool, RegionSelectMode, LassoMode};
use lightningbeam_core::brush_settings::bundled_brushes;
use crate::keymap::tool_app_action;
use super::{NodePath, PaneRenderer, SharedPaneState};
@@ -101,7 +102,7 @@ impl PaneRenderer for ToolbarPane {
}
// Draw sub-tool arrow indicator for tools with modes
let has_sub_tools = matches!(tool, Tool::RegionSelect);
let has_sub_tools = matches!(tool, Tool::RegionSelect | Tool::SelectLasso);
if has_sub_tools {
let arrow_size = 6.0;
let margin = 4.0;
@@ -125,6 +126,22 @@ impl PaneRenderer for ToolbarPane {
// Check for click first
if response.clicked() {
*shared.selected_tool = *tool;
// Preset-backed tools: auto-select the matching bundled brush.
let preset_name = match tool {
Tool::Pencil => Some("Pencil"),
Tool::Pen => Some("Pen"),
Tool::Airbrush => Some("Airbrush"),
_ => None,
};
if let Some(name) = preset_name {
if let Some(preset) = bundled_brushes().iter().find(|p| p.name == name) {
let s = &preset.settings;
shared.raster_settings.brush_opacity = s.opaque.clamp(0.0, 1.0);
shared.raster_settings.brush_hardness = s.hardness.clamp(0.0, 1.0);
shared.raster_settings.brush_spacing = s.dabs_per_radius;
shared.raster_settings.active_brush_settings = s.clone();
}
}
}
// Right-click context menu for tools with sub-options
@@ -150,6 +167,33 @@ impl PaneRenderer for ToolbarPane {
ui.close();
}
}
Tool::SelectLasso => {
ui.set_min_width(130.0);
if ui.selectable_label(
*shared.lasso_mode == LassoMode::Freehand,
"Freehand",
).clicked() {
*shared.lasso_mode = LassoMode::Freehand;
*shared.selected_tool = Tool::SelectLasso;
ui.close();
}
if ui.selectable_label(
*shared.lasso_mode == LassoMode::Polygonal,
"Polygonal",
).clicked() {
*shared.lasso_mode = LassoMode::Polygonal;
*shared.selected_tool = Tool::SelectLasso;
ui.close();
}
if ui.selectable_label(
*shared.lasso_mode == LassoMode::Magnetic,
"Magnetic",
).clicked() {
*shared.lasso_mode = LassoMode::Magnetic;
*shared.selected_tool = Tool::SelectLasso;
ui.close();
}
}
_ => {}
}
});
@@ -176,6 +220,13 @@ impl PaneRenderer for ToolbarPane {
RegionSelectMode::Lasso => "Lasso",
};
format!("{} - {}{}\nRight-click for options", tool.display_name(), mode, hint)
} else if *tool == Tool::SelectLasso {
let mode = match *shared.lasso_mode {
LassoMode::Freehand => "Freehand",
LassoMode::Polygonal => "Polygonal",
LassoMode::Magnetic => "Magnetic",
};
format!("{} - {}{}\nRight-click for options", tool.display_name(), mode, hint)
} else {
format!("{}{}", tool.display_name(), hint)
};
@@ -0,0 +1,758 @@
//! Unified raster tool interface.
//!
//! Every raster tool operates on three GPU textures of identical dimensions:
//!
//! | Buffer | Access | Purpose |
//! |--------|--------|---------|
//! | **A** | Read-only | Source pixels, uploaded from layer/float at mousedown. |
//! | **B** | Write-only | Output / display. Compositor shows B while the tool is active. |
//! | **C** | Read+Write | Scratch. Dabs accumulate here across the stroke; composite A+C→B each frame. |
//!
//! All three are `Rgba8Unorm` with the same pixel dimensions. The framework
//! allocates and validates them in [`begin_raster_workspace`]; tools only
//! dispatch shaders.
use std::sync::Arc;
use uuid::Uuid;
use eframe::egui;
// ── WorkspaceSource ──────────────────────────────────────────────────────────
/// Describes whether the tool is operating on a raster layer or a floating selection.
#[derive(Clone, Debug)]
pub enum WorkspaceSource {
/// Operating on the full raster layer.
Layer {
layer_id: Uuid,
time: f64,
/// The keyframe's own UUID (the A-canvas key in `GpuBrushEngine`).
kf_id: Uuid,
/// Full canvas dimensions (may differ from workspace dims for floating selections).
canvas_w: u32,
canvas_h: u32,
},
/// Operating on the floating selection.
Float,
}
// ── RasterWorkspace ───────────────────────────────────────────────────────────
/// GPU buffer IDs and metadata for a single tool operation.
///
/// Created by [`begin_raster_workspace`] on mousedown. All three canvas UUIDs
/// index into `GpuBrushEngine::canvases` and are valid for the lifetime of the
/// active tool. They are queued for removal in `pending_canvas_removals` after
/// commit or cancel.
#[derive(Debug)]
pub struct RasterWorkspace {
/// A canvas (Rgba8Unorm) — source pixels, uploaded at mousedown, read-only for tools.
pub a_canvas_id: Uuid,
/// B canvas (Rgba8Unorm) — output / display; compositor shows this while active.
pub b_canvas_id: Uuid,
/// C canvas (Rgba8Unorm) — scratch; tools accumulate dabs here across the stroke.
pub c_canvas_id: Uuid,
/// Optional R8Unorm selection mask (same pixel dimensions as A/B/C).
/// `None` means the entire workspace is selected.
pub mask_texture: Option<Arc<wgpu::Texture>>,
/// Pixel dimensions. A, B, C, and mask are all guaranteed to be this size.
pub width: u32,
pub height: u32,
/// Top-left position in document-pixel space.
/// `(0, 0)` for a layer workspace; `(float.x, float.y)` for a float workspace.
pub x: i32,
pub y: i32,
/// Where the workspace came from — drives commit behaviour.
pub source: WorkspaceSource,
/// CPU snapshot taken at mousedown for undo / cancel.
/// Length is always `width * height * 4` (sRGB premultiplied RGBA).
pub before_pixels: Vec<u8>,
}
impl RasterWorkspace {
/// Panic-safe bounds check. Asserts that every GPU canvas exists and has
/// the dimensions declared by this workspace. Called by the framework
/// before `begin()` and before each `update()`.
pub fn validate(&self, gpu: &crate::gpu_brush::GpuBrushEngine) {
for (name, id) in [
("A", self.a_canvas_id),
("B", self.b_canvas_id),
("C", self.c_canvas_id),
] {
let canvas = gpu.canvases.get(&id).unwrap_or_else(|| {
panic!(
"RasterWorkspace::validate: buffer '{}' (id={}) not found in GpuBrushEngine",
name, id
)
});
assert_eq!(
canvas.width, self.width,
"RasterWorkspace::validate: buffer '{}' width {} != workspace width {}",
name, canvas.width, self.width
);
assert_eq!(
canvas.height, self.height,
"RasterWorkspace::validate: buffer '{}' height {} != workspace height {}",
name, canvas.height, self.height
);
}
let expected = (self.width * self.height * 4) as usize;
assert_eq!(
self.before_pixels.len(), expected,
"RasterWorkspace::validate: before_pixels.len()={} != expected {}",
self.before_pixels.len(), expected
);
}
/// Returns the three canvas UUIDs as an array (convenient for bulk removal).
pub fn canvas_ids(&self) -> [Uuid; 3] {
[self.a_canvas_id, self.b_canvas_id, self.c_canvas_id]
}
}
// ── WorkspaceInitPacket ───────────────────────────────────────────────────────
/// Data sent to `prepare()` on the first frame to create and upload the A/B/C canvases.
///
/// The canvas UUIDs are pre-allocated in `begin_raster_workspace()` (UI thread).
/// The actual `wgpu::Texture` creation and pixel upload happens in `prepare()`.
pub struct WorkspaceInitPacket {
/// A canvas UUID (already in `RasterWorkspace::a_canvas_id`).
pub a_canvas_id: Uuid,
/// Pixel data to upload to A. Length must equal `width * height * 4`.
pub a_pixels: Vec<u8>,
/// B canvas UUID.
pub b_canvas_id: Uuid,
/// C canvas UUID.
pub c_canvas_id: Uuid,
pub width: u32,
pub height: u32,
}
// ── ActiveToolRender ──────────────────────────────────────────────────────────
/// Passed to `VelloRenderContext` so the compositor can blit the tool's B output
/// in the correct position in the layer stack.
///
/// While an `ActiveToolRender` is set:
/// - If `layer_id == Some(id)`: blit B at that layer's compositor slot.
/// - If `layer_id == None`: blit B at the float's compositor slot.
#[derive(Clone, Debug)]
pub struct ActiveToolRender {
/// B canvas to blit.
pub b_canvas_id: Uuid,
/// Position of the B canvas in document space.
pub x: i32,
pub y: i32,
/// Pixel dimensions of the B canvas.
pub width: u32,
pub height: u32,
/// `Some(layer_id)` → B replaces this layer's render slot.
/// `None` → B replaces the float render slot.
pub layer_id: Option<Uuid>,
}
// ── PendingGpuWork ────────────────────────────────────────────────────────────
/// GPU work to execute in `VelloCallback::prepare()`.
///
/// Tools compute dab lists and other CPU-side data in `update()` (UI thread),
/// store them as a `Box<dyn PendingGpuWork>`, and return that work through
/// `RasterTool::take_pending_gpu_work()` each frame. `prepare()` then calls
/// `execute()` with the render-thread `device`/`queue`/`gpu`.
///
/// `execute()` takes `&self` so the work object need not be consumed; it lives
/// in the `VelloRenderContext` (which is immutable in `prepare()`).
pub trait PendingGpuWork: Send + Sync {
fn execute(
&self,
device: &wgpu::Device,
queue: &wgpu::Queue,
gpu: &mut crate::gpu_brush::GpuBrushEngine,
);
}
// ── RasterTool trait ──────────────────────────────────────────────────────────
/// Unified interface for all raster tools.
///
/// All methods run on the UI thread. They update the tool's internal state
/// and store pending GPU op descriptors in fields that `StagePane` forwards
/// to `VelloRenderContext` for execution by `VelloCallback::prepare()`.
pub trait RasterTool: Send + Sync {
/// Called on **mousedown** after [`begin_raster_workspace`] has allocated and
/// validated A, B, and C. The tool should initialise its internal state and
/// optionally queue an initial GPU dispatch (e.g. identity composite for
/// transform so the handle frame appears immediately).
fn begin(
&mut self,
ws: &RasterWorkspace,
pos: egui::Vec2,
dt: f32,
settings: &crate::tools::RasterToolSettings,
);
/// Called every frame while the pointer is held (including the first drag frame).
/// The tool should accumulate new work into C and queue a composite A+C→B pass.
/// `dt` is the elapsed time in seconds since the previous call; used by time-based
/// brushes (airbrush, etc.) to fire dabs at the correct rate when stationary.
fn update(
&mut self,
ws: &RasterWorkspace,
pos: egui::Vec2,
dt: f32,
settings: &crate::tools::RasterToolSettings,
);
/// Called on **pointer release**. Returns `true` if a GPU readback of B should
/// be performed and the result committed to the document. Returns `false` if
/// the operation was a no-op (e.g. the pointer never moved).
fn finish(&mut self, ws: &RasterWorkspace) -> bool;
/// Called on **Escape** or tool switch mid-stroke. The caller restores the
/// source pixels from `ws.before_pixels` without creating an undo entry; the
/// tool just cleans up internal state.
fn cancel(&mut self, ws: &RasterWorkspace);
/// Called once per frame (in the VelloCallback construction, UI thread) to
/// extract pending GPU work accumulated by `begin()` / `update()`.
///
/// The tool clears its internal pending work and returns it. `prepare()` on
/// the render thread then calls `work.execute()`. Default: no GPU work.
fn take_pending_gpu_work(&mut self) -> Option<Box<dyn PendingGpuWork>> {
None
}
}
// ── BrushRasterTool ───────────────────────────────────────────────────────────
use lightningbeam_core::brush_engine::{BrushEngine, GpuDab, StrokeState};
use lightningbeam_core::brush_settings::BrushSettings;
use lightningbeam_core::raster_layer::{RasterBlendMode, StrokePoint, StrokeRecord};
/// GPU work for one frame of a brush stroke: dispatch dabs into C, then composite A+C→B.
struct PendingBrushWork {
dabs: Vec<GpuDab>,
bbox: (i32, i32, i32, i32),
a_id: Uuid,
b_id: Uuid,
c_id: Uuid,
canvas_w: u32,
canvas_h: u32,
}
impl PendingGpuWork for PendingBrushWork {
fn execute(
&self,
device: &wgpu::Device,
queue: &wgpu::Queue,
gpu: &mut crate::gpu_brush::GpuBrushEngine,
) {
// 1. Accumulate this frame's dabs into C (if any).
if !self.dabs.is_empty() {
gpu.render_dabs(device, queue, self.c_id, &self.dabs, self.bbox, self.canvas_w, self.canvas_h);
}
// 2. Always composite A + C → B so B shows A's content even with no dabs this frame.
// On begin() with empty C this initialises B = A, avoiding a transparent flash.
gpu.composite_a_c_to_b(device, queue, self.a_id, self.c_id, self.b_id, self.canvas_w, self.canvas_h);
}
}
/// Raster tool for paint brushes (Normal blend mode).
///
/// Each `update()` call computes new dabs for that frame and stores them as
/// `PendingBrushWork`. `take_pending_gpu_work()` hands the work to `prepare()`
/// which dispatches the dab and composite shaders on the render thread.
pub struct BrushRasterTool {
color: [f32; 4],
brush: BrushSettings,
blend_mode: RasterBlendMode,
stroke_state: StrokeState,
last_point: Option<StrokePoint>,
pending: Option<Box<PendingBrushWork>>,
/// True after at least one non-empty frame (so finish() knows a commit is needed).
has_dabs: bool,
/// Offset to convert world coordinates to canvas-local coordinates.
canvas_offset_x: i32,
canvas_offset_y: i32,
}
impl BrushRasterTool {
/// Create a new brush tool.
///
/// `color` — linear premultiplied RGBA, matches the format expected by `GpuDab`.
pub fn new(
color: [f32; 4],
brush: BrushSettings,
blend_mode: RasterBlendMode,
) -> Self {
Self {
color,
brush,
blend_mode,
stroke_state: StrokeState::new(),
last_point: None,
pending: None,
has_dabs: false,
canvas_offset_x: 0,
canvas_offset_y: 0,
}
}
fn make_stroke_point(pos: egui::Vec2, off_x: i32, off_y: i32) -> StrokePoint {
StrokePoint {
x: pos.x - off_x as f32,
y: pos.y - off_y as f32,
pressure: 1.0,
tilt_x: 0.0,
tilt_y: 0.0,
timestamp: 0.0,
}
}
fn dispatch_dabs(
&mut self,
ws: &RasterWorkspace,
pt: StrokePoint,
dt: f32,
) {
// Use a 2-point segment when we have a previous point so the engine
// interpolates dabs along the path. First mousedown uses a single point.
let points = match self.last_point.take() {
Some(prev) => vec![prev, pt.clone()],
None => vec![pt.clone()],
};
let record = StrokeRecord {
brush_settings: self.brush.clone(),
color: self.color,
blend_mode: self.blend_mode,
tool_params: [0.0; 4],
points,
};
let (dabs, bbox) = BrushEngine::compute_dabs(&record, &mut self.stroke_state, dt);
if !dabs.is_empty() {
self.has_dabs = true;
self.pending = Some(Box::new(PendingBrushWork {
dabs,
bbox,
a_id: ws.a_canvas_id,
b_id: ws.b_canvas_id,
c_id: ws.c_canvas_id,
canvas_w: ws.width,
canvas_h: ws.height,
}));
}
self.last_point = Some(pt);
}
}
impl RasterTool for BrushRasterTool {
fn begin(&mut self, ws: &RasterWorkspace, pos: egui::Vec2, _dt: f32, _settings: &crate::tools::RasterToolSettings) {
self.canvas_offset_x = ws.x;
self.canvas_offset_y = ws.y;
let pt = Self::make_stroke_point(pos, ws.x, ws.y);
self.dispatch_dabs(ws, pt, 0.0);
// Always ensure a composite is queued on begin() so B is initialised from A
// on the first frame even if no dabs fired (large spacing, etc.).
if self.pending.is_none() {
self.pending = Some(Box::new(PendingBrushWork {
dabs: vec![],
bbox: (0, 0, ws.width as i32, ws.height as i32),
a_id: ws.a_canvas_id,
b_id: ws.b_canvas_id,
c_id: ws.c_canvas_id,
canvas_w: ws.width,
canvas_h: ws.height,
}));
}
}
fn update(&mut self, ws: &RasterWorkspace, pos: egui::Vec2, dt: f32, _settings: &crate::tools::RasterToolSettings) {
let pt = Self::make_stroke_point(pos, ws.x, ws.y);
self.dispatch_dabs(ws, pt, dt);
}
fn finish(&mut self, _ws: &RasterWorkspace) -> bool {
self.has_dabs
}
fn cancel(&mut self, _ws: &RasterWorkspace) {
self.pending = None;
self.has_dabs = false;
}
fn take_pending_gpu_work(&mut self) -> Option<Box<dyn PendingGpuWork>> {
self.pending.take().map(|w| w as Box<dyn PendingGpuWork>)
}
}
// ── EffectBrushTool ───────────────────────────────────────────────────────────
/// Raster tool for effect brushes (Blur, Sharpen, Dodge, Burn, Sponge, Desaturate).
///
/// C accumulates a per-pixel influence weight (R channel, 0–255).
/// The composite pass applies the effect to A, scaled by C.r, writing to B:
/// `B = lerp(A, effect(A), C.r)`
///
/// Using C as an influence map (rather than accumulating modified pixels) prevents
/// overlapping dabs from compounding the effect beyond the C.r cap (255).
///
/// # GPU implementation (TODO)
/// Requires a dedicated `effect_brush_composite.wgsl` shader that reads A and C,
/// applies the blend-mode-specific filter to A, and blends by C.r → B.
pub struct EffectBrushTool {
brush: BrushSettings,
blend_mode: RasterBlendMode,
has_dabs: bool,
}
impl EffectBrushTool {
pub fn new(brush: BrushSettings, blend_mode: RasterBlendMode) -> Self {
Self { brush, blend_mode, has_dabs: false }
}
}
impl RasterTool for EffectBrushTool {
fn begin(&mut self, _ws: &RasterWorkspace, _pos: egui::Vec2, _dt: f32, _settings: &crate::tools::RasterToolSettings) {}
fn update(&mut self, _ws: &RasterWorkspace, _pos: egui::Vec2, _dt: f32, _settings: &crate::tools::RasterToolSettings) {
self.has_dabs = true; // placeholder
}
fn finish(&mut self, _ws: &RasterWorkspace) -> bool { self.has_dabs }
fn cancel(&mut self, _ws: &RasterWorkspace) { self.has_dabs = false; }
// GPU shaders not yet implemented; take_pending_gpu_work returns None (default).
}
// ── SmudgeTool ────────────────────────────────────────────────────────────────
/// Raster tool for the smudge brush.
///
/// `begin()`: copy A → C so C starts with the source pixels for color pickup.
/// `update()`: dispatch smudge dabs using `blend_mode=2` (reads C as source,
/// writes smear to C); then composite C over A → B.
/// Because the smudge shader reads from `canvas_src` (C.src) and writes to
/// `canvas_dst` (C.dst), existing dabs are preserved in the smear history.
///
/// # GPU implementation (TODO)
/// Requires an initial A → C copy in `begin()` (via GPU copy command).
/// The smudge dab dispatch then uses `render_dabs(c_id, smudge_dabs, ...)`.
/// The composite pass is `composite_a_c_to_b` (same as BrushRasterTool).
pub struct SmudgeTool {
brush: BrushSettings,
has_dabs: bool,
}
impl SmudgeTool {
pub fn new(brush: BrushSettings) -> Self {
Self { brush, has_dabs: false }
}
}
impl RasterTool for SmudgeTool {
fn begin(&mut self, _ws: &RasterWorkspace, _pos: egui::Vec2, _dt: f32, _settings: &crate::tools::RasterToolSettings) {}
fn update(&mut self, _ws: &RasterWorkspace, _pos: egui::Vec2, _dt: f32, _settings: &crate::tools::RasterToolSettings) {
self.has_dabs = true; // placeholder
}
fn finish(&mut self, _ws: &RasterWorkspace) -> bool { self.has_dabs }
fn cancel(&mut self, _ws: &RasterWorkspace) { self.has_dabs = false; }
// GPU shaders not yet implemented; take_pending_gpu_work returns None (default).
}
// ── GradientRasterTool ────────────────────────────────────────────────────────
use crate::gpu_brush::GpuGradientStop;
use lightningbeam_core::gradient::{GradientExtend, GradientType, ShapeGradient};
fn gradient_stops_to_gpu(gradient: &ShapeGradient) -> Vec<GpuGradientStop> {
gradient.stops.iter().map(|s| {
GpuGradientStop::from_srgb_u8(s.position, s.color.r, s.color.g, s.color.b, s.color.a)
}).collect()
}
fn gradient_extend_to_u32(extend: GradientExtend) -> u32 {
match extend {
GradientExtend::Pad => 0,
GradientExtend::Reflect => 1,
GradientExtend::Repeat => 2,
}
}
fn gradient_kind_to_u32(kind: GradientType) -> u32 {
match kind {
GradientType::Linear => 0,
GradientType::Radial => 1,
}
}
struct PendingGradientWork {
a_id: Uuid,
b_id: Uuid,
stops: Vec<GpuGradientStop>,
start: (f32, f32),
end: (f32, f32),
opacity: f32,
extend_mode: u32,
kind: u32,
}
impl PendingGpuWork for PendingGradientWork {
fn execute(&self, device: &wgpu::Device, queue: &wgpu::Queue, gpu: &mut crate::gpu_brush::GpuBrushEngine) {
gpu.apply_gradient_fill(
device, queue,
&self.a_id, &self.b_id,
&self.stops,
self.start, self.end,
self.opacity, self.extend_mode, self.kind,
);
}
}
/// Raster tool for gradient fills.
///
/// `begin()` records the canvas-local start position.
/// `update()` recomputes gradient parameters from settings and queues a
/// `PendingGradientWork` that calls `apply_gradient_fill` in `prepare()`.
/// `finish()` returns whether any gradient was dispatched.
pub struct GradientRasterTool {
start_canvas: egui::Vec2,
end_canvas: egui::Vec2,
pending: Option<Box<PendingGradientWork>>,
has_dispatched: bool,
}
impl GradientRasterTool {
pub fn new() -> Self {
Self {
start_canvas: egui::Vec2::ZERO,
end_canvas: egui::Vec2::ZERO,
pending: None,
has_dispatched: false,
}
}
}
impl RasterTool for GradientRasterTool {
fn begin(&mut self, ws: &RasterWorkspace, pos: egui::Vec2, _dt: f32, _settings: &crate::tools::RasterToolSettings) {
let canvas_pos = pos - egui::vec2(ws.x as f32, ws.y as f32);
self.start_canvas = canvas_pos;
self.end_canvas = canvas_pos;
}
fn update(&mut self, ws: &RasterWorkspace, pos: egui::Vec2, _dt: f32, settings: &crate::tools::RasterToolSettings) {
self.end_canvas = pos - egui::vec2(ws.x as f32, ws.y as f32);
let gradient = &settings.gradient;
self.pending = Some(Box::new(PendingGradientWork {
a_id: ws.a_canvas_id,
b_id: ws.b_canvas_id,
stops: gradient_stops_to_gpu(gradient),
start: (self.start_canvas.x, self.start_canvas.y),
end: (self.end_canvas.x, self.end_canvas.y),
opacity: settings.gradient_opacity,
extend_mode: gradient_extend_to_u32(gradient.extend),
kind: gradient_kind_to_u32(gradient.kind),
}));
self.has_dispatched = true;
}
fn finish(&mut self, _ws: &RasterWorkspace) -> bool { self.has_dispatched }
fn cancel(&mut self, _ws: &RasterWorkspace) {
self.pending = None;
self.has_dispatched = false;
}
fn take_pending_gpu_work(&mut self) -> Option<Box<dyn PendingGpuWork>> {
self.pending.take().map(|w| w as Box<dyn PendingGpuWork>)
}
}
// ── TransformRasterTool ───────────────────────────────────────────────────────
use crate::gpu_brush::RasterTransformGpuParams;
struct PendingTransformWork {
a_id: Uuid,
b_id: Uuid,
params: RasterTransformGpuParams,
}
impl PendingGpuWork for PendingTransformWork {
fn execute(&self, device: &wgpu::Device, queue: &wgpu::Queue, gpu: &mut crate::gpu_brush::GpuBrushEngine) {
gpu.render_transform(device, queue, &self.a_id, &self.b_id, self.params);
}
}
/// Raster tool for affine transforms (move, scale, rotate, shear).
///
/// `begin()` stores the initial canvas dimensions and queues an identity
/// transform so B is initialised on the first frame.
/// `update()` recomputes the inverse affine matrix from the current handle
/// positions and queues a new `PendingTransformWork`.
///
/// The inverse matrix maps output pixel coordinates back to source pixel
/// coordinates: `src = M_inv * dst + b`
/// where `M_inv = [[a00, a01], [a10, a11]]` and `b = [b0, b1]`.
///
/// # GPU implementation
/// Fully wired — uses `GpuBrushEngine::render_transform`. Handle interaction
/// logic (drag, rotate, scale) is handled by the tool's `update()` caller in
/// `stage.rs` which computes and passes in the `RasterTransformGpuParams`.
pub struct TransformRasterTool {
pending: Option<Box<PendingTransformWork>>,
has_dispatched: bool,
canvas_w: u32,
canvas_h: u32,
}
impl TransformRasterTool {
pub fn new() -> Self {
Self {
pending: None,
has_dispatched: false,
canvas_w: 0,
canvas_h: 0,
}
}
/// Queue a transform with the given inverse-affine matrix.
/// Called by the stage handler after computing handle positions.
pub fn set_transform(
&mut self,
ws: &RasterWorkspace,
params: RasterTransformGpuParams,
) {
self.pending = Some(Box::new(PendingTransformWork {
a_id: ws.a_canvas_id,
b_id: ws.b_canvas_id,
params,
}));
self.has_dispatched = true;
}
}
impl RasterTool for TransformRasterTool {
fn begin(&mut self, ws: &RasterWorkspace, _pos: egui::Vec2, _dt: f32, _settings: &crate::tools::RasterToolSettings) {
self.canvas_w = ws.width;
self.canvas_h = ws.height;
// Queue identity transform so B shows the source immediately.
let identity = RasterTransformGpuParams {
a00: 1.0, a01: 0.0,
a10: 0.0, a11: 1.0,
b0: 0.0, b1: 0.0,
src_w: ws.width, src_h: ws.height,
dst_w: ws.width, dst_h: ws.height,
_pad0: 0, _pad1: 0,
};
self.set_transform(ws, identity);
}
fn update(&mut self, _ws: &RasterWorkspace, _pos: egui::Vec2, _dt: f32, _settings: &crate::tools::RasterToolSettings) {
// Handle interaction and matrix updates are driven from stage.rs via set_transform().
}
fn finish(&mut self, _ws: &RasterWorkspace) -> bool { self.has_dispatched }
fn cancel(&mut self, _ws: &RasterWorkspace) {
self.pending = None;
self.has_dispatched = false;
}
fn take_pending_gpu_work(&mut self) -> Option<Box<dyn PendingGpuWork>> {
self.pending.take().map(|w| w as Box<dyn PendingGpuWork>)
}
}
// ── WarpRasterTool ────────────────────────────────────────────────────────────
/// Raster tool for warp / mesh deformation.
///
/// Uses a displacement buffer (managed by `GpuBrushEngine`) that maps each
/// output pixel to a source offset. The displacement grid is updated by
/// dragging control points; the warp shader reads anchor pixels + displacement
/// → B each frame.
///
/// # GPU implementation (TODO)
/// Requires: `create_displacement_buf`, `apply_warp` already exist in
/// `GpuBrushEngine`. Wire brush-drag interaction to update displacement
/// entries and call `apply_warp`.
pub struct WarpRasterTool {
has_dispatched: bool,
}
impl WarpRasterTool {
pub fn new() -> Self { Self { has_dispatched: false } }
}
impl RasterTool for WarpRasterTool {
fn begin(&mut self, _ws: &RasterWorkspace, _pos: egui::Vec2, _dt: f32, _settings: &crate::tools::RasterToolSettings) {}
fn update(&mut self, _ws: &RasterWorkspace, _pos: egui::Vec2, _dt: f32, _settings: &crate::tools::RasterToolSettings) {
self.has_dispatched = true; // placeholder
}
fn finish(&mut self, _ws: &RasterWorkspace) -> bool { self.has_dispatched }
fn cancel(&mut self, _ws: &RasterWorkspace) { self.has_dispatched = false; }
// take_pending_gpu_work: default (None) — full GPU wiring is TODO.
}
// ── LiquifyRasterTool ─────────────────────────────────────────────────────────
/// Raster tool for liquify (per-pixel displacement painting).
///
/// Similar to `WarpRasterTool` but uses a full per-pixel displacement map
/// (grid_cols = grid_rows = 0 in `apply_warp`) painted by brush strokes.
/// Each dab accumulates displacement in the push/pull/swirl direction.
///
/// # GPU implementation (TODO)
/// Requires: a dab-to-displacement shader that accumulates per-pixel offsets
/// into the displacement buffer, then `apply_warp` reads it → B.
pub struct LiquifyRasterTool {
has_dispatched: bool,
}
impl LiquifyRasterTool {
pub fn new() -> Self { Self { has_dispatched: false } }
}
impl RasterTool for LiquifyRasterTool {
fn begin(&mut self, _ws: &RasterWorkspace, _pos: egui::Vec2, _dt: f32, _settings: &crate::tools::RasterToolSettings) {}
fn update(&mut self, _ws: &RasterWorkspace, _pos: egui::Vec2, _dt: f32, _settings: &crate::tools::RasterToolSettings) {
self.has_dispatched = true; // placeholder
}
fn finish(&mut self, _ws: &RasterWorkspace) -> bool { self.has_dispatched }
fn cancel(&mut self, _ws: &RasterWorkspace) { self.has_dispatched = false; }
// take_pending_gpu_work: default (None) — full GPU wiring is TODO.
}
// ── SelectionTool ─────────────────────────────────────────────────────────────
/// Raster selection tool (Magic Wand / Quick Select).
///
/// C (RGBA8) acts as the growing selection; C.r = mask value (0 or 255).
/// Each `update()` frame a flood-fill / region-grow shader extends C.r.
/// The composite pass draws A + a tinted overlay from C.r → B so the user
/// sees the growing selection boundary.
///
/// `finish()` returns false (commit does not write pixels back to the layer;
/// instead the caller extracts C.r into the standalone `R8Unorm` selection
/// texture via `shared.raster_selection`).
///
/// # GPU implementation (TODO)
/// Requires: a flood-fill compute shader seeded by the click position that
/// grows the selection in C.r; and a composite shader that tints selected
/// pixels blue/cyan for preview.
pub struct SelectionTool {
has_selection: bool,
}
impl SelectionTool {
pub fn new() -> Self { Self { has_selection: false } }
}
impl RasterTool for SelectionTool {
fn begin(&mut self, _ws: &RasterWorkspace, _pos: egui::Vec2, _dt: f32, _settings: &crate::tools::RasterToolSettings) {}
fn update(&mut self, _ws: &RasterWorkspace, _pos: egui::Vec2, _dt: f32, _settings: &crate::tools::RasterToolSettings) {
self.has_selection = true; // placeholder
}
/// Selection tools never trigger a pixel readback/commit on mouseup.
/// The caller reads C.r directly into the selection mask texture.
fn finish(&mut self, _ws: &RasterWorkspace) -> bool { false }
fn cancel(&mut self, _ws: &RasterWorkspace) { self.has_selection = false; }
// take_pending_gpu_work: default (None) — full GPU wiring is TODO.
}
@@ -0,0 +1,60 @@
use super::{BrushParams, RasterToolDef, RasterToolSettings};
use eframe::egui;
use lightningbeam_core::{brush_settings::BrushSettings, raster_layer::RasterBlendMode};
pub struct BlurSharpenTool;
pub static BLUR_SHARPEN: BlurSharpenTool = BlurSharpenTool;
impl RasterToolDef for BlurSharpenTool {
fn blend_mode(&self) -> RasterBlendMode { RasterBlendMode::BlurSharpen }
fn header_label(&self) -> &'static str { "Blur / Sharpen" }
fn brush_params(&self, s: &RasterToolSettings) -> BrushParams {
BrushParams {
base_settings: BrushSettings::default(),
radius: s.blur_sharpen_radius,
opacity: s.blur_sharpen_strength,
hardness: s.blur_sharpen_hardness,
spacing: s.blur_sharpen_spacing,
}
}
fn tool_params(&self, s: &RasterToolSettings) -> [f32; 4] {
[s.blur_sharpen_mode as f32, s.blur_sharpen_kernel, 0.0, 0.0]
}
fn show_brush_preset_picker(&self) -> bool { false }
fn render_ui(&self, ui: &mut egui::Ui, s: &mut RasterToolSettings) {
ui.horizontal(|ui| {
if ui.selectable_label(s.blur_sharpen_mode == 0, "Blur").clicked() {
s.blur_sharpen_mode = 0;
}
if ui.selectable_label(s.blur_sharpen_mode == 1, "Sharpen").clicked() {
s.blur_sharpen_mode = 1;
}
});
ui.horizontal(|ui| {
ui.label("Size:");
ui.add(egui::Slider::new(&mut s.blur_sharpen_radius, 1.0_f32..=500.0).logarithmic(true).suffix(" px"));
});
ui.horizontal(|ui| {
ui.label("Strength:");
ui.add(egui::Slider::new(&mut s.blur_sharpen_strength, 0.0_f32..=1.0)
.custom_formatter(|v, _| format!("{:.0}%", v * 100.0)));
});
ui.horizontal(|ui| {
ui.label("Hardness:");
ui.add(egui::Slider::new(&mut s.blur_sharpen_hardness, 0.0_f32..=1.0)
.custom_formatter(|v, _| format!("{:.0}%", v * 100.0)));
});
ui.horizontal(|ui| {
ui.label("Kernel:");
ui.add(egui::Slider::new(&mut s.blur_sharpen_kernel, 1.0_f32..=20.0)
.logarithmic(true)
.custom_formatter(|v, _| format!("{:.1} px", v)));
});
ui.horizontal(|ui| {
ui.label("Spacing:");
ui.add(egui::Slider::new(&mut s.blur_sharpen_spacing, 0.5_f32..=20.0)
.logarithmic(true)
.custom_formatter(|v, _| format!("{:.1}", v)));
});
}
}
@@ -0,0 +1,29 @@
use super::{BrushParams, RasterToolDef, RasterToolSettings};
use eframe::egui;
use lightningbeam_core::raster_layer::RasterBlendMode;
pub struct CloneStampTool;
pub static CLONE_STAMP: CloneStampTool = CloneStampTool;
impl RasterToolDef for CloneStampTool {
fn blend_mode(&self) -> RasterBlendMode { RasterBlendMode::CloneStamp }
fn header_label(&self) -> &'static str { "Clone Stamp" }
fn brush_params(&self, s: &RasterToolSettings) -> BrushParams {
BrushParams {
base_settings: s.active_brush_settings.clone(),
radius: s.brush_radius,
opacity: s.brush_opacity,
hardness: s.brush_hardness,
spacing: s.brush_spacing,
}
}
/// For Clone Stamp, tool_params are filled by stage.rs at stroke-start time
/// (offset = clone_source - stroke_start), not from settings directly.
fn tool_params(&self, _s: &RasterToolSettings) -> [f32; 4] { [0.0; 4] }
fn uses_alt_click(&self) -> bool { true }
fn render_ui(&self, ui: &mut egui::Ui, s: &mut RasterToolSettings) {
if s.clone_source.is_none() {
ui.label("Alt+click to set source point.");
}
}
}
@@ -0,0 +1,54 @@
use super::{BrushParams, RasterToolDef, RasterToolSettings};
use eframe::egui;
use lightningbeam_core::{brush_settings::BrushSettings, raster_layer::RasterBlendMode};
pub struct DodgeBurnTool;
pub static DODGE_BURN: DodgeBurnTool = DodgeBurnTool;
impl RasterToolDef for DodgeBurnTool {
fn blend_mode(&self) -> RasterBlendMode { RasterBlendMode::DodgeBurn }
fn header_label(&self) -> &'static str { "Dodge / Burn" }
fn brush_params(&self, s: &RasterToolSettings) -> BrushParams {
BrushParams {
base_settings: BrushSettings::default(),
radius: s.dodge_burn_radius,
opacity: s.dodge_burn_exposure,
hardness: s.dodge_burn_hardness,
spacing: s.dodge_burn_spacing,
}
}
fn tool_params(&self, s: &RasterToolSettings) -> [f32; 4] {
[s.dodge_burn_mode as f32, 0.0, 0.0, 0.0]
}
fn show_brush_preset_picker(&self) -> bool { false }
fn render_ui(&self, ui: &mut egui::Ui, s: &mut RasterToolSettings) {
ui.horizontal(|ui| {
if ui.selectable_label(s.dodge_burn_mode == 0, "Dodge").clicked() {
s.dodge_burn_mode = 0;
}
if ui.selectable_label(s.dodge_burn_mode == 1, "Burn").clicked() {
s.dodge_burn_mode = 1;
}
});
ui.horizontal(|ui| {
ui.label("Size:");
ui.add(egui::Slider::new(&mut s.dodge_burn_radius, 1.0_f32..=500.0).logarithmic(true).suffix(" px"));
});
ui.horizontal(|ui| {
ui.label("Exposure:");
ui.add(egui::Slider::new(&mut s.dodge_burn_exposure, 0.0_f32..=1.0)
.custom_formatter(|v, _| format!("{:.0}%", v * 100.0)));
});
ui.horizontal(|ui| {
ui.label("Hardness:");
ui.add(egui::Slider::new(&mut s.dodge_burn_hardness, 0.0_f32..=1.0)
.custom_formatter(|v, _| format!("{:.0}%", v * 100.0)));
});
ui.horizontal(|ui| {
ui.label("Spacing:");
ui.add(egui::Slider::new(&mut s.dodge_burn_spacing, 0.5_f32..=20.0)
.logarithmic(true)
.custom_formatter(|v, _| format!("{:.1}", v)));
});
}
}
@@ -0,0 +1,23 @@
use super::{BrushParams, RasterToolDef, RasterToolSettings};
use eframe::egui;
use lightningbeam_core::raster_layer::RasterBlendMode;
pub struct EraseTool;
pub static ERASE: EraseTool = EraseTool;
impl RasterToolDef for EraseTool {
fn blend_mode(&self) -> RasterBlendMode { RasterBlendMode::Erase }
fn header_label(&self) -> &'static str { "Eraser" }
fn brush_params(&self, s: &RasterToolSettings) -> BrushParams {
BrushParams {
base_settings: s.active_eraser_settings.clone(),
radius: s.eraser_radius,
opacity: s.eraser_opacity,
hardness: s.eraser_hardness,
spacing: s.eraser_spacing,
}
}
fn tool_params(&self, _s: &RasterToolSettings) -> [f32; 4] { [0.0; 4] }
fn is_eraser(&self) -> bool { true }
fn render_ui(&self, _ui: &mut egui::Ui, _s: &mut RasterToolSettings) {}
}
@@ -0,0 +1,28 @@
use super::{BrushParams, RasterToolDef, RasterToolSettings};
use eframe::egui;
use lightningbeam_core::raster_layer::RasterBlendMode;
pub struct HealingBrushTool;
pub static HEALING_BRUSH: HealingBrushTool = HealingBrushTool;
impl RasterToolDef for HealingBrushTool {
fn blend_mode(&self) -> RasterBlendMode { RasterBlendMode::Healing }
fn header_label(&self) -> &'static str { "Healing Brush" }
fn brush_params(&self, s: &RasterToolSettings) -> BrushParams {
BrushParams {
base_settings: s.active_brush_settings.clone(),
radius: s.brush_radius,
opacity: s.brush_opacity,
hardness: s.brush_hardness,
spacing: s.brush_spacing,
}
}
/// tool_params are filled by stage.rs at stroke-start time (clone offset).
fn tool_params(&self, _s: &RasterToolSettings) -> [f32; 4] { [0.0; 4] }
fn uses_alt_click(&self) -> bool { true }
fn render_ui(&self, ui: &mut egui::Ui, s: &mut RasterToolSettings) {
if s.clone_source.is_none() {
ui.label("Alt+click to set source point.");
}
}
}
@@ -0,0 +1,268 @@
/// Per-tool module for raster painting tools.
///
/// Each tool implements `RasterToolDef`. Adding a new tool requires:
/// 1. A new file in this directory implementing `RasterToolDef`.
/// 2. One entry in `raster_tool_def()` below.
/// 3. Core changes: `RasterBlendMode` variant, `brush_engine.rs` constant, WGSL branch.
use eframe::egui;
use lightningbeam_core::{
brush_settings::BrushSettings,
raster_layer::RasterBlendMode,
tool::Tool,
};
pub mod paint;
pub mod erase;
pub mod smudge;
pub mod clone_stamp;
pub mod healing_brush;
pub mod pattern_stamp;
pub mod dodge_burn;
pub mod sponge;
pub mod blur_sharpen;
// ---------------------------------------------------------------------------
// Shared settings struct (replaces 20+ individual SharedPaneState / EditorApp fields)
// ---------------------------------------------------------------------------
/// All per-tool settings for raster painting. Owned by `EditorApp`; borrowed
/// by `SharedPaneState` as a single `&'a mut RasterToolSettings`.
pub struct RasterToolSettings {
// --- Paint brush ---
pub brush_radius: f32,
pub brush_opacity: f32,
pub brush_hardness: f32,
pub brush_spacing: f32,
/// true = paint with FG (stroke) color, false = BG (fill) color
pub brush_use_fg: bool,
pub active_brush_settings: BrushSettings,
// --- Eraser ---
pub eraser_radius: f32,
pub eraser_opacity: f32,
pub eraser_hardness: f32,
pub eraser_spacing: f32,
pub active_eraser_settings: BrushSettings,
// --- Smudge ---
pub smudge_radius: f32,
pub smudge_hardness: f32,
pub smudge_spacing: f32,
pub smudge_strength: f32,
// --- Clone / Healing ---
/// World-space source point set by Alt+click.
pub clone_source: Option<egui::Vec2>,
// --- Pattern stamp ---
pub pattern_type: u32,
pub pattern_scale: f32,
// --- Dodge / Burn ---
pub dodge_burn_radius: f32,
pub dodge_burn_hardness: f32,
pub dodge_burn_spacing: f32,
pub dodge_burn_exposure: f32,
/// 0 = dodge (lighten), 1 = burn (darken)
pub dodge_burn_mode: u32,
// --- Sponge ---
pub sponge_radius: f32,
pub sponge_hardness: f32,
pub sponge_spacing: f32,
pub sponge_flow: f32,
/// 0 = saturate, 1 = desaturate
pub sponge_mode: u32,
// --- Blur / Sharpen ---
pub blur_sharpen_radius: f32,
pub blur_sharpen_hardness: f32,
pub blur_sharpen_spacing: f32,
pub blur_sharpen_strength: f32,
/// Neighborhood kernel radius in canvas pixels (1–20)
pub blur_sharpen_kernel: f32,
/// 0 = blur, 1 = sharpen
pub blur_sharpen_mode: u32,
// --- Magic wand (raster) ---
/// Color-distance threshold for magic wand selection (same scale as fill_threshold).
pub wand_threshold: f32,
/// Absolute = compare to seed pixel; Relative = compare to BFS parent.
pub wand_mode: FillThresholdMode,
/// true = BFS from click (contiguous region only); false = global color scan.
pub wand_contiguous: bool,
// --- Quick Select ---
/// Brush radius in canvas pixels for the quick-select tool.
pub quick_select_radius: f32,
// --- Flood fill (Paint Bucket, raster) ---
/// Color-distance threshold (Euclidean RGBA, 0–510). Pixels within this
/// distance of the comparison color are included in the fill.
pub fill_threshold: f32,
/// Soft-edge width as a percentage of the threshold (0 = hard, 100 = full fade).
pub fill_softness: f32,
/// Whether to compare each pixel to the seed pixel (Absolute) or to its BFS
/// parent pixel (Relative, spreads across gradients).
pub fill_threshold_mode: FillThresholdMode,
// --- Marquee select shape ---
/// Whether the rectangular select tool draws a rect or an ellipse.
pub select_shape: SelectionShape,
// --- Warp ---
pub warp_grid_cols: u32,
pub warp_grid_rows: u32,
// --- Liquify ---
pub liquify_mode: LiquifyMode,
pub liquify_radius: f32,
pub liquify_strength: f32,
// --- Gradient ---
pub gradient: lightningbeam_core::gradient::ShapeGradient,
pub gradient_opacity: f32,
}
/// Brush mode for the Liquify tool.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum LiquifyMode {
#[default]
Push,
Pucker,
Bloat,
Smooth,
Reconstruct,
}
impl LiquifyMode {
pub fn as_u32(self) -> u32 {
match self {
LiquifyMode::Push => 0,
LiquifyMode::Pucker => 1,
LiquifyMode::Bloat => 2,
LiquifyMode::Smooth => 3,
LiquifyMode::Reconstruct => 4,
}
}
}
/// Shape mode for the rectangular-select tool.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum SelectionShape {
#[default]
Rect,
Ellipse,
}
/// Threshold comparison mode for the raster flood fill.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum FillThresholdMode {
/// Compare each candidate pixel to the original seed pixel (default).
#[default]
Absolute,
/// Compare each candidate pixel to the pixel it was reached from.
Relative,
}
impl Default for RasterToolSettings {
fn default() -> Self {
Self {
brush_radius: 10.0,
brush_opacity: 1.0,
brush_hardness: 0.5,
brush_spacing: 0.1,
brush_use_fg: true,
active_brush_settings: BrushSettings::default(),
eraser_radius: 10.0,
eraser_opacity: 1.0,
eraser_hardness: 0.5,
eraser_spacing: 0.1,
active_eraser_settings: lightningbeam_core::brush_settings::bundled_brushes()
.iter()
.find(|p| p.name == "Brush")
.map(|p| p.settings.clone())
.unwrap_or_default(),
smudge_radius: 15.0,
smudge_hardness: 0.8,
smudge_spacing: 8.0,
smudge_strength: 1.0,
clone_source: None,
pattern_type: 0,
pattern_scale: 32.0,
dodge_burn_radius: 30.0,
dodge_burn_hardness: 0.5,
dodge_burn_spacing: 3.0,
dodge_burn_exposure: 0.5,
dodge_burn_mode: 0,
sponge_radius: 30.0,
sponge_hardness: 0.5,
sponge_spacing: 3.0,
sponge_flow: 0.5,
sponge_mode: 0,
blur_sharpen_radius: 30.0,
blur_sharpen_hardness: 0.5,
blur_sharpen_spacing: 3.0,
blur_sharpen_strength: 0.5,
blur_sharpen_kernel: 5.0,
blur_sharpen_mode: 0,
wand_threshold: 15.0,
wand_mode: FillThresholdMode::Absolute,
wand_contiguous: true,
fill_threshold: 15.0,
fill_softness: 0.0,
fill_threshold_mode: FillThresholdMode::Absolute,
quick_select_radius: 20.0,
select_shape: SelectionShape::Rect,
warp_grid_cols: 4,
warp_grid_rows: 4,
liquify_mode: LiquifyMode::Push,
liquify_radius: 50.0,
liquify_strength: 0.5,
gradient: lightningbeam_core::gradient::ShapeGradient::default(),
gradient_opacity: 1.0,
}
}
}
// ---------------------------------------------------------------------------
// Brush parameters extracted per-tool
// ---------------------------------------------------------------------------
pub struct BrushParams {
pub base_settings: BrushSettings,
pub radius: f32,
pub opacity: f32,
pub hardness: f32,
pub spacing: f32,
}
// ---------------------------------------------------------------------------
// RasterToolDef trait
// ---------------------------------------------------------------------------
pub trait RasterToolDef: Send + Sync {
fn blend_mode(&self) -> RasterBlendMode;
fn header_label(&self) -> &'static str;
fn brush_params(&self, s: &RasterToolSettings) -> BrushParams;
/// Encode tool-specific state into the 4-float `StrokeRecord::tool_params`.
fn tool_params(&self, s: &RasterToolSettings) -> [f32; 4];
/// Cursor display radius (world pixels).
fn cursor_radius(&self, s: &RasterToolSettings) -> f32 {
self.brush_params(s).radius
}
/// Render tool-specific controls in the infopanel (called before preset picker if any).
fn render_ui(&self, ui: &mut egui::Ui, s: &mut RasterToolSettings);
/// Whether to show the brush preset picker after `render_ui`.
fn show_brush_preset_picker(&self) -> bool { true }
/// Whether this tool is the eraser (drives preset picker + color UI visibility).
fn is_eraser(&self) -> bool { false }
/// Whether Alt+click sets a source point for this tool.
fn uses_alt_click(&self) -> bool { false }
}
// ---------------------------------------------------------------------------
// Lookup: Tool → &'static dyn RasterToolDef
// ---------------------------------------------------------------------------
pub fn raster_tool_def(tool: &Tool) -> Option<&'static dyn RasterToolDef> {
match tool {
Tool::Draw | Tool::Pencil | Tool::Pen | Tool::Airbrush => Some(&paint::PAINT),
Tool::Erase => Some(&erase::ERASE),
Tool::Smudge => Some(&smudge::SMUDGE),
Tool::CloneStamp => Some(&clone_stamp::CLONE_STAMP),
Tool::HealingBrush => Some(&healing_brush::HEALING_BRUSH),
Tool::PatternStamp => Some(&pattern_stamp::PATTERN_STAMP),
Tool::DodgeBurn => Some(&dodge_burn::DODGE_BURN),
Tool::Sponge => Some(&sponge::SPONGE),
Tool::BlurSharpen => Some(&blur_sharpen::BLUR_SHARPEN),
_ => None,
}
}
@@ -0,0 +1,22 @@
use super::{BrushParams, RasterToolDef, RasterToolSettings};
use eframe::egui;
use lightningbeam_core::raster_layer::RasterBlendMode;
pub struct PaintTool;
pub static PAINT: PaintTool = PaintTool;
impl RasterToolDef for PaintTool {
fn blend_mode(&self) -> RasterBlendMode { RasterBlendMode::Normal }
fn header_label(&self) -> &'static str { "Brush" }
fn brush_params(&self, s: &RasterToolSettings) -> BrushParams {
BrushParams {
base_settings: s.active_brush_settings.clone(),
radius: s.brush_radius,
opacity: s.brush_opacity,
hardness: s.brush_hardness,
spacing: s.brush_spacing,
}
}
fn tool_params(&self, _s: &RasterToolSettings) -> [f32; 4] { [0.0; 4] }
fn render_ui(&self, _ui: &mut egui::Ui, _s: &mut RasterToolSettings) {}
}
@@ -0,0 +1,49 @@
use super::{BrushParams, RasterToolDef, RasterToolSettings};
use eframe::egui;
use lightningbeam_core::raster_layer::RasterBlendMode;
pub struct PatternStampTool;
pub static PATTERN_STAMP: PatternStampTool = PatternStampTool;
const PATTERN_NAMES: &[&str] = &[
"Checkerboard", "Dots", "H-Lines", "V-Lines", "Diagonal \\", "Diagonal /", "Crosshatch",
];
impl RasterToolDef for PatternStampTool {
fn blend_mode(&self) -> RasterBlendMode { RasterBlendMode::PatternStamp }
fn header_label(&self) -> &'static str { "Pattern Stamp" }
fn brush_params(&self, s: &RasterToolSettings) -> BrushParams {
BrushParams {
base_settings: s.active_brush_settings.clone(),
radius: s.brush_radius,
opacity: s.brush_opacity,
hardness: s.brush_hardness,
spacing: s.brush_spacing,
}
}
fn tool_params(&self, s: &RasterToolSettings) -> [f32; 4] {
[s.pattern_type as f32, s.pattern_scale, 0.0, 0.0]
}
fn render_ui(&self, ui: &mut egui::Ui, s: &mut RasterToolSettings) {
let selected_name = PATTERN_NAMES
.get(s.pattern_type as usize)
.copied()
.unwrap_or("Checkerboard");
ui.horizontal(|ui| {
ui.label("Pattern:");
egui::ComboBox::from_id_salt("pattern_type")
.selected_text(selected_name)
.show_ui(ui, |ui| {
for (i, name) in PATTERN_NAMES.iter().enumerate() {
ui.selectable_value(&mut s.pattern_type, i as u32, *name);
}
});
});
ui.horizontal(|ui| {
ui.label("Scale:");
ui.add(egui::Slider::new(&mut s.pattern_scale, 4.0_f32..=256.0)
.logarithmic(true).suffix(" px"));
});
ui.add_space(4.0);
}
}
@@ -0,0 +1,44 @@
use super::{BrushParams, RasterToolDef, RasterToolSettings};
use eframe::egui;
use lightningbeam_core::{brush_settings::BrushSettings, raster_layer::RasterBlendMode};
pub struct SmudgeTool;
pub static SMUDGE: SmudgeTool = SmudgeTool;
impl RasterToolDef for SmudgeTool {
fn blend_mode(&self) -> RasterBlendMode { RasterBlendMode::Smudge }
fn header_label(&self) -> &'static str { "Smudge" }
fn brush_params(&self, s: &RasterToolSettings) -> BrushParams {
BrushParams {
base_settings: BrushSettings::default(),
radius: s.smudge_radius,
opacity: 1.0, // strength is a separate smudge_dist multiplier
hardness: s.smudge_hardness,
spacing: s.smudge_spacing,
}
}
fn tool_params(&self, _s: &RasterToolSettings) -> [f32; 4] { [0.0; 4] }
fn show_brush_preset_picker(&self) -> bool { false }
fn render_ui(&self, ui: &mut egui::Ui, s: &mut RasterToolSettings) {
ui.horizontal(|ui| {
ui.label("Size:");
ui.add(egui::Slider::new(&mut s.smudge_radius, 1.0_f32..=200.0).logarithmic(true).suffix(" px"));
});
ui.horizontal(|ui| {
ui.label("Strength:");
ui.add(egui::Slider::new(&mut s.smudge_strength, 0.0_f32..=1.0)
.custom_formatter(|v, _| format!("{:.0}%", v * 100.0)));
});
ui.horizontal(|ui| {
ui.label("Hardness:");
ui.add(egui::Slider::new(&mut s.smudge_hardness, 0.0_f32..=1.0)
.custom_formatter(|v, _| format!("{:.0}%", v * 100.0)));
});
ui.horizontal(|ui| {
ui.label("Spacing:");
ui.add(egui::Slider::new(&mut s.smudge_spacing, 0.5_f32..=20.0)
.logarithmic(true)
.custom_formatter(|v, _| format!("{:.1}", v)));
});
}
}
@@ -0,0 +1,54 @@
use super::{BrushParams, RasterToolDef, RasterToolSettings};
use eframe::egui;
use lightningbeam_core::{brush_settings::BrushSettings, raster_layer::RasterBlendMode};
pub struct SpongeTool;
pub static SPONGE: SpongeTool = SpongeTool;
impl RasterToolDef for SpongeTool {
fn blend_mode(&self) -> RasterBlendMode { RasterBlendMode::Sponge }
fn header_label(&self) -> &'static str { "Sponge" }
fn brush_params(&self, s: &RasterToolSettings) -> BrushParams {
BrushParams {
base_settings: BrushSettings::default(),
radius: s.sponge_radius,
opacity: s.sponge_flow,
hardness: s.sponge_hardness,
spacing: s.sponge_spacing,
}
}
fn tool_params(&self, s: &RasterToolSettings) -> [f32; 4] {
[s.sponge_mode as f32, 0.0, 0.0, 0.0]
}
fn show_brush_preset_picker(&self) -> bool { false }
fn render_ui(&self, ui: &mut egui::Ui, s: &mut RasterToolSettings) {
ui.horizontal(|ui| {
if ui.selectable_label(s.sponge_mode == 0, "Saturate").clicked() {
s.sponge_mode = 0;
}
if ui.selectable_label(s.sponge_mode == 1, "Desaturate").clicked() {
s.sponge_mode = 1;
}
});
ui.horizontal(|ui| {
ui.label("Size:");
ui.add(egui::Slider::new(&mut s.sponge_radius, 1.0_f32..=500.0).logarithmic(true).suffix(" px"));
});
ui.horizontal(|ui| {
ui.label("Flow:");
ui.add(egui::Slider::new(&mut s.sponge_flow, 0.0_f32..=1.0)
.custom_formatter(|v, _| format!("{:.0}%", v * 100.0)));
});
ui.horizontal(|ui| {
ui.label("Hardness:");
ui.add(egui::Slider::new(&mut s.sponge_hardness, 0.0_f32..=1.0)
.custom_formatter(|v, _| format!("{:.0}%", v * 100.0)));
});
ui.horizontal(|ui| {
ui.label("Spacing:");
ui.add(egui::Slider::new(&mut s.sponge_spacing, 0.5_f32..=20.0)
.logarithmic(true)
.custom_formatter(|v, _| format!("{:.1}", v)));
});
}
}