Add initial docs
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,523 @@
|
||||
# Building Lightningbeam
|
||||
|
||||
This guide provides detailed instructions for building Lightningbeam on different platforms, including dependency installation, troubleshooting, and advanced build configurations.
|
||||
|
||||
## Table of Contents
|
||||
|
||||
- [Quick Start](#quick-start)
|
||||
- [Platform-Specific Instructions](#platform-specific-instructions)
|
||||
- [Dependencies](#dependencies)
|
||||
- [Build Configurations](#build-configurations)
|
||||
- [Troubleshooting](#troubleshooting)
|
||||
- [Development Builds](#development-builds)
|
||||
|
||||
## Quick Start
|
||||
|
||||
```bash
|
||||
# Clone the repository
|
||||
git clone https://github.com/skykooler/lightningbeam.git
|
||||
cd lightningbeam/lightningbeam-ui
|
||||
|
||||
# Build and run
|
||||
cargo build
|
||||
cargo run
|
||||
```
|
||||
|
||||
## Platform-Specific Instructions
|
||||
|
||||
### Linux
|
||||
|
||||
#### Ubuntu/Debian
|
||||
|
||||
**Important**: Lightningbeam requires FFmpeg 8, which may not be in the default repositories.
|
||||
|
||||
```bash
|
||||
# Install basic dependencies
|
||||
sudo apt update
|
||||
sudo apt install -y \
|
||||
build-essential \
|
||||
pkg-config \
|
||||
libasound2-dev \
|
||||
clang \
|
||||
libclang-dev
|
||||
|
||||
# Install FFmpeg 8 from PPA (Ubuntu)
|
||||
sudo add-apt-repository ppa:ubuntuhandbook1/ffmpeg7
|
||||
sudo apt update
|
||||
sudo apt install -y \
|
||||
ffmpeg \
|
||||
libavcodec-dev \
|
||||
libavformat-dev \
|
||||
libavutil-dev \
|
||||
libswscale-dev \
|
||||
libswresample-dev
|
||||
|
||||
# Verify FFmpeg version (should be 8.x)
|
||||
ffmpeg -version
|
||||
|
||||
# Install Rust if needed
|
||||
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
|
||||
|
||||
# Build
|
||||
cd lightningbeam-ui
|
||||
cargo build --release
|
||||
```
|
||||
|
||||
**Note**: If the PPA doesn't provide FFmpeg 8, you may need to compile FFmpeg from source or find an alternative PPA. See [FFmpeg Issues](#ffmpeg-issues) for details.
|
||||
|
||||
#### Arch Linux/Manjaro
|
||||
|
||||
```bash
|
||||
# Install system dependencies
|
||||
sudo pacman -S --needed \
|
||||
base-devel \
|
||||
rust \
|
||||
alsa-lib \
|
||||
ffmpeg \
|
||||
clang
|
||||
|
||||
# Build
|
||||
cd lightningbeam-ui
|
||||
cargo build --release
|
||||
```
|
||||
|
||||
#### Fedora/RHEL
|
||||
|
||||
```bash
|
||||
# Install system dependencies
|
||||
sudo dnf install -y \
|
||||
gcc \
|
||||
gcc-c++ \
|
||||
make \
|
||||
pkg-config \
|
||||
alsa-lib-devel \
|
||||
ffmpeg \
|
||||
ffmpeg-devel \
|
||||
clang \
|
||||
clang-devel
|
||||
|
||||
# Install Rust if needed
|
||||
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
|
||||
|
||||
# Build
|
||||
cd lightningbeam-ui
|
||||
cargo build --release
|
||||
```
|
||||
|
||||
### macOS
|
||||
|
||||
```bash
|
||||
# Install Homebrew if needed
|
||||
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
|
||||
|
||||
# Install dependencies
|
||||
brew install rust ffmpeg pkg-config
|
||||
|
||||
# Build
|
||||
cd lightningbeam-ui
|
||||
cargo build --release
|
||||
```
|
||||
|
||||
**Note**: macOS uses CoreAudio for audio I/O (via cpal), so no additional audio libraries are needed.
|
||||
|
||||
### Windows
|
||||
|
||||
#### Using Visual Studio
|
||||
|
||||
1. Install [Visual Studio 2022](https://visualstudio.microsoft.com/) with "Desktop development with C++" workload
|
||||
2. Install [Rust](https://rustup.rs/)
|
||||
3. Install [FFmpeg](https://ffmpeg.org/download.html#build-windows):
|
||||
- Download a shared build from https://www.gyan.dev/ffmpeg/builds/
|
||||
- Extract to `C:\ffmpeg`
|
||||
- Add `C:\ffmpeg\bin` to PATH
|
||||
- Set environment variables:
|
||||
```cmd
|
||||
set FFMPEG_DIR=C:\ffmpeg
|
||||
set PKG_CONFIG_PATH=C:\ffmpeg\lib\pkgconfig
|
||||
```
|
||||
|
||||
4. Build:
|
||||
```cmd
|
||||
cd lightningbeam-ui
|
||||
cargo build --release
|
||||
```
|
||||
|
||||
#### Using MSYS2/MinGW
|
||||
|
||||
```bash
|
||||
# In MSYS2 shell
|
||||
pacman -S mingw-w64-x86_64-rust \
|
||||
mingw-w64-x86_64-ffmpeg \
|
||||
mingw-w64-x86_64-pkg-config
|
||||
|
||||
cd lightningbeam-ui
|
||||
cargo build --release
|
||||
```
|
||||
|
||||
**Note**: Windows uses WASAPI for audio I/O (via cpal), which is built into Windows.
|
||||
|
||||
## Dependencies
|
||||
|
||||
### Required Dependencies
|
||||
|
||||
#### Rust Toolchain
|
||||
- **Version**: Stable (1.70+)
|
||||
- **Install**: https://rustup.rs/
|
||||
- **Components**: Default installation includes everything needed
|
||||
|
||||
#### Audio I/O (ALSA on Linux)
|
||||
- **Ubuntu/Debian**: `libasound2-dev`
|
||||
- **Arch**: `alsa-lib`
|
||||
- **Fedora**: `alsa-lib-devel`
|
||||
- **macOS**: CoreAudio (built-in)
|
||||
- **Windows**: WASAPI (built-in)
|
||||
|
||||
#### FFmpeg
|
||||
**Version Required**: FFmpeg 8.x
|
||||
|
||||
Required for video encoding/decoding. Note that many distribution repositories may have older versions.
|
||||
|
||||
- **Ubuntu/Debian**: Use PPA for FFmpeg 8 (see [Ubuntu/Debian instructions](#ubuntudebian))
|
||||
- **Arch**: `ffmpeg` (usually up-to-date)
|
||||
- **Fedora**: `ffmpeg ffmpeg-devel` (check version with `ffmpeg -version`)
|
||||
- **macOS**: `brew install ffmpeg` (Homebrew usually has latest)
|
||||
- **Windows**: Download FFmpeg 8 from https://ffmpeg.org/download.html
|
||||
|
||||
#### Build Tools
|
||||
- **Linux**: `build-essential` (Ubuntu), `base-devel` (Arch)
|
||||
- **macOS**: Xcode Command Line Tools (`xcode-select --install`)
|
||||
- **Windows**: Visual Studio with C++ tools or MinGW
|
||||
|
||||
#### pkg-config
|
||||
Required for finding system libraries.
|
||||
|
||||
- **Linux**: Usually included with build tools
|
||||
- **macOS**: `brew install pkg-config`
|
||||
- **Windows**: Included with MSYS2/MinGW, or use vcpkg
|
||||
|
||||
### Optional Dependencies
|
||||
|
||||
#### GPU Drivers
|
||||
Vello requires a GPU with Vulkan (Linux/Windows) or Metal (macOS) support:
|
||||
|
||||
- **Linux Vulkan**:
|
||||
- NVIDIA: Install proprietary drivers
|
||||
- AMD: `mesa-vulkan-drivers` (Ubuntu) or `vulkan-radeon` (Arch)
|
||||
- Intel: `mesa-vulkan-drivers` (Ubuntu) or `vulkan-intel` (Arch)
|
||||
|
||||
- **macOS Metal**: Built-in (macOS 10.13+)
|
||||
|
||||
- **Windows Vulkan**:
|
||||
- Usually included with GPU drivers
|
||||
- Manual install: https://vulkan.lunarg.com/
|
||||
|
||||
## Build Configurations
|
||||
|
||||
### Release Build (Optimized)
|
||||
|
||||
```bash
|
||||
cargo build --release
|
||||
```
|
||||
|
||||
- Optimizations: Level 3
|
||||
- LTO: Enabled
|
||||
- Debug info: None
|
||||
- Build time: Slower (~5-10 minutes)
|
||||
- Runtime: Fast
|
||||
|
||||
Binary location: `target/release/lightningbeam-editor`
|
||||
|
||||
### Debug Build (Default)
|
||||
|
||||
```bash
|
||||
cargo build
|
||||
```
|
||||
|
||||
- Optimizations: Level 1 (Level 2 for audio code)
|
||||
- LTO: Disabled
|
||||
- Debug info: Full
|
||||
- Build time: Faster (~2-5 minutes)
|
||||
- Runtime: Slower (but audio is still optimized)
|
||||
|
||||
Binary location: `target/debug/lightningbeam-editor`
|
||||
|
||||
**Note**: Audio code is always compiled with `opt-level = 2` even in debug builds to meet real-time deadlines. This is configured in `lightningbeam-ui/Cargo.toml`:
|
||||
|
||||
```toml
|
||||
[profile.dev.package.daw-backend]
|
||||
opt-level = 2
|
||||
```
|
||||
|
||||
### Check Without Building
|
||||
|
||||
Quickly check for compilation errors without producing binaries:
|
||||
|
||||
```bash
|
||||
cargo check
|
||||
```
|
||||
|
||||
Useful for rapid feedback during development.
|
||||
|
||||
### Build Specific Package
|
||||
|
||||
```bash
|
||||
# Check only the audio backend
|
||||
cargo check -p daw-backend
|
||||
|
||||
# Build only the core library
|
||||
cargo build -p lightningbeam-core
|
||||
```
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
### Audio Issues
|
||||
|
||||
#### "ALSA lib cannot find card" or similar errors
|
||||
|
||||
**Solution**: Install ALSA development files:
|
||||
```bash
|
||||
# Ubuntu/Debian
|
||||
sudo apt install libasound2-dev
|
||||
|
||||
# Arch
|
||||
sudo pacman -S alsa-lib
|
||||
```
|
||||
|
||||
#### Audio dropouts or crackling
|
||||
|
||||
**Symptoms**: Console shows "Audio overrun" or timing warnings.
|
||||
|
||||
**Solutions**:
|
||||
1. Increase buffer size in `daw-backend/src/lib.rs` (default: 256 frames)
|
||||
2. Enable audio debug logging:
|
||||
```bash
|
||||
DAW_AUDIO_DEBUG=1 cargo run
|
||||
```
|
||||
3. Make sure audio code is optimized (check `Cargo.toml` profile settings)
|
||||
4. Close other audio applications
|
||||
|
||||
#### "PulseAudio" or "JACK" errors in container
|
||||
|
||||
**Note**: This is expected in containerized environments without audio support. These errors don't occur on native systems.
|
||||
|
||||
### FFmpeg Issues
|
||||
|
||||
#### "Could not find FFmpeg libraries" or linking errors
|
||||
|
||||
**Version Check First**:
|
||||
```bash
|
||||
ffmpeg -version
|
||||
# Should show version 8.x
|
||||
```
|
||||
|
||||
**Linux**:
|
||||
```bash
|
||||
# Ubuntu/Debian - requires FFmpeg 8 from PPA
|
||||
sudo add-apt-repository ppa:ubuntuhandbook1/ffmpeg7
|
||||
sudo apt update
|
||||
sudo apt install libavcodec-dev libavformat-dev libavutil-dev libswscale-dev libswresample-dev
|
||||
|
||||
# Arch (usually has latest)
|
||||
sudo pacman -S ffmpeg
|
||||
|
||||
# Check installation
|
||||
pkg-config --modversion libavcodec
|
||||
# Should show 61.x or higher (FFmpeg 8)
|
||||
```
|
||||
|
||||
If the PPA doesn't work or doesn't have FFmpeg 8, you may need to compile from source:
|
||||
```bash
|
||||
# Download and compile FFmpeg 8
|
||||
wget https://ffmpeg.org/releases/ffmpeg-8.0.tar.xz
|
||||
tar xf ffmpeg-8.0.tar.xz
|
||||
cd ffmpeg-8.0
|
||||
./configure --enable-shared --disable-static
|
||||
make -j$(nproc)
|
||||
sudo make install
|
||||
sudo ldconfig
|
||||
```
|
||||
|
||||
**macOS**:
|
||||
```bash
|
||||
brew install ffmpeg
|
||||
export PKG_CONFIG_PATH="/opt/homebrew/opt/ffmpeg/lib/pkgconfig:$PKG_CONFIG_PATH"
|
||||
```
|
||||
|
||||
**Windows**:
|
||||
Set environment variables:
|
||||
```cmd
|
||||
set FFMPEG_DIR=C:\path\to\ffmpeg
|
||||
set PKG_CONFIG_PATH=C:\path\to\ffmpeg\lib\pkgconfig
|
||||
```
|
||||
|
||||
#### "Unsupported codec" or video not playing
|
||||
|
||||
Make sure FFmpeg was compiled with the necessary codecs:
|
||||
```bash
|
||||
ffmpeg -codecs | grep h264 # Check for H.264
|
||||
ffmpeg -codecs | grep vp9 # Check for VP9
|
||||
```
|
||||
|
||||
### GPU/Rendering Issues
|
||||
|
||||
#### Black screen or no rendering
|
||||
|
||||
**Check GPU support**:
|
||||
```bash
|
||||
# Linux - check Vulkan
|
||||
vulkaninfo | grep deviceName
|
||||
|
||||
# macOS - Metal is built-in on 10.13+
|
||||
system_profiler SPDisplaysDataType
|
||||
```
|
||||
|
||||
**Solutions**:
|
||||
1. Update GPU drivers
|
||||
2. Install Vulkan runtime (Linux)
|
||||
3. Check console for wgpu errors
|
||||
|
||||
#### "No suitable GPU adapter found"
|
||||
|
||||
This usually means missing Vulkan/Metal support.
|
||||
|
||||
**Linux**: Install Vulkan drivers (see [Optional Dependencies](#optional-dependencies))
|
||||
|
||||
**macOS**: Requires macOS 10.13+ (Metal support)
|
||||
|
||||
**Windows**: Update GPU drivers
|
||||
|
||||
### Build Performance
|
||||
|
||||
#### Slow compilation times
|
||||
|
||||
**Solutions**:
|
||||
1. Use `cargo check` instead of `cargo build` during development
|
||||
2. Enable incremental compilation (enabled by default)
|
||||
3. Use `mold` linker (Linux):
|
||||
```bash
|
||||
# Install mold
|
||||
sudo apt install mold # Ubuntu 22.04+
|
||||
|
||||
# Use mold
|
||||
mold -run cargo build
|
||||
```
|
||||
4. Increase parallel jobs:
|
||||
```bash
|
||||
cargo build -j 8 # Use 8 parallel jobs
|
||||
```
|
||||
|
||||
#### Out of memory during compilation
|
||||
|
||||
**Solution**: Reduce parallel jobs:
|
||||
```bash
|
||||
cargo build -j 2 # Use only 2 parallel jobs
|
||||
```
|
||||
|
||||
### Linker Errors
|
||||
|
||||
#### "undefined reference to..." or "cannot find -l..."
|
||||
|
||||
**Cause**: Missing system libraries.
|
||||
|
||||
**Solution**: Install all dependencies listed in [Platform-Specific Instructions](#platform-specific-instructions).
|
||||
|
||||
#### Windows: "LNK1181: cannot open input file"
|
||||
|
||||
**Cause**: FFmpeg libraries not found.
|
||||
|
||||
**Solution**:
|
||||
1. Download FFmpeg shared build
|
||||
2. Set `FFMPEG_DIR` environment variable
|
||||
3. Add FFmpeg bin directory to PATH
|
||||
|
||||
## Development Builds
|
||||
|
||||
### Enable Audio Debug Logging
|
||||
|
||||
```bash
|
||||
DAW_AUDIO_DEBUG=1 cargo run
|
||||
```
|
||||
|
||||
Output includes:
|
||||
- Buffer sizes
|
||||
- Average/worst-case processing times
|
||||
- Audio overruns/underruns
|
||||
- Playhead position updates
|
||||
|
||||
### Disable Optimizations for Specific Crates
|
||||
|
||||
Edit `lightningbeam-ui/Cargo.toml`:
|
||||
|
||||
```toml
|
||||
[profile.dev.package.my-crate]
|
||||
opt-level = 0 # No optimizations
|
||||
```
|
||||
|
||||
**Warning**: Do not disable optimizations for `daw-backend` or audio-related crates, as this will cause audio dropouts.
|
||||
|
||||
### Build with Specific Features
|
||||
|
||||
```bash
|
||||
# Build with all features
|
||||
cargo build --all-features
|
||||
|
||||
# Build with no default features
|
||||
cargo build --no-default-features
|
||||
```
|
||||
|
||||
### Clean Build
|
||||
|
||||
Remove all build artifacts and start fresh:
|
||||
|
||||
```bash
|
||||
cargo clean
|
||||
cargo build
|
||||
```
|
||||
|
||||
Useful when dependencies change or build cache becomes corrupted.
|
||||
|
||||
### Cross-Compilation
|
||||
|
||||
Cross-compiling is not currently documented but should be possible using `cross`:
|
||||
|
||||
```bash
|
||||
cargo install cross
|
||||
cross build --target x86_64-unknown-linux-gnu
|
||||
```
|
||||
|
||||
See [cross documentation](https://github.com/cross-rs/cross) for details.
|
||||
|
||||
## Running Tests
|
||||
|
||||
```bash
|
||||
# Run all tests
|
||||
cargo test
|
||||
|
||||
# Run tests for specific package
|
||||
cargo test -p lightningbeam-core
|
||||
|
||||
# Run with output
|
||||
cargo test -- --nocapture
|
||||
|
||||
# Run specific test
|
||||
cargo test test_name
|
||||
```
|
||||
|
||||
## Building Documentation
|
||||
|
||||
Generate and open Rust API documentation:
|
||||
|
||||
```bash
|
||||
cargo doc --open
|
||||
```
|
||||
|
||||
This generates HTML documentation from code comments and opens it in your browser.
|
||||
|
||||
## Next Steps
|
||||
|
||||
After building successfully:
|
||||
|
||||
- See [CONTRIBUTING.md](../CONTRIBUTING.md) for development workflow
|
||||
- See [ARCHITECTURE.md](../ARCHITECTURE.md) for system architecture
|
||||
- See [docs/AUDIO_SYSTEM.md](AUDIO_SYSTEM.md) for audio engine details
|
||||
- See [docs/UI_SYSTEM.md](UI_SYSTEM.md) for UI development
|
||||
@@ -0,0 +1,812 @@
|
||||
# GPU Rendering Architecture
|
||||
|
||||
This document describes Lightningbeam's GPU rendering pipeline, including Vello integration for vector graphics, custom WGSL shaders for waveforms, and wgpu integration patterns.
|
||||
|
||||
## Table of Contents
|
||||
|
||||
- [Overview](#overview)
|
||||
- [Rendering Pipeline](#rendering-pipeline)
|
||||
- [Vello Integration](#vello-integration)
|
||||
- [Waveform Rendering](#waveform-rendering)
|
||||
- [WGSL Shaders](#wgsl-shaders)
|
||||
- [Uniform Buffer Alignment](#uniform-buffer-alignment)
|
||||
- [Custom wgpu Integration](#custom-wgpu-integration)
|
||||
- [Performance Optimization](#performance-optimization)
|
||||
- [Debugging Rendering Issues](#debugging-rendering-issues)
|
||||
|
||||
## Overview
|
||||
|
||||
Lightningbeam uses GPU-accelerated rendering for high-performance 2D graphics:
|
||||
|
||||
- **Vello**: Compute shader-based 2D vector rendering
|
||||
- **wgpu 27**: Cross-platform GPU API (Vulkan, Metal, D3D12)
|
||||
- **egui-wgpu**: Integration layer between egui and wgpu
|
||||
- **Custom WGSL shaders**: For specialized rendering (waveforms, effects)
|
||||
|
||||
### Supported Backends
|
||||
|
||||
- **Linux**: Vulkan (primary), OpenGL (fallback)
|
||||
- **macOS**: Metal
|
||||
- **Windows**: Vulkan, DirectX 12
|
||||
|
||||
## Rendering Pipeline
|
||||
|
||||
### High-Level Flow
|
||||
|
||||
```
|
||||
┌─────────────────────────────────────────────────────────────┐
|
||||
│ Application Frame │
|
||||
├─────────────────────────────────────────────────────────────┤
|
||||
│ │
|
||||
│ 1. egui Layout Phase │
|
||||
│ - Build UI tree │
|
||||
│ - Collect paint primitives │
|
||||
│ - Register wgpu callbacks │
|
||||
│ │
|
||||
│ 2. Custom GPU Rendering (via egui_wgpu::Callback) │
|
||||
│ ┌────────────────────────────────────────────────┐ │
|
||||
│ │ prepare(): │ │
|
||||
│ │ - Build Vello scene from document │ │
|
||||
│ │ - Update uniform buffers │ │
|
||||
│ │ - Generate waveform mipmaps (if needed) │ │
|
||||
│ └────────────────────────────────────────────────┘ │
|
||||
│ ┌────────────────────────────────────────────────┐ │
|
||||
│ │ paint(): │ │
|
||||
│ │ - Render Vello scene to texture │ │
|
||||
│ │ - Render waveforms │ │
|
||||
│ │ - Composite layers │ │
|
||||
│ └────────────────────────────────────────────────┘ │
|
||||
│ │
|
||||
│ 3. egui Paint │
|
||||
│ - Render egui UI elements │
|
||||
│ - Composite with custom rendering │
|
||||
│ │
|
||||
│ 4. Present to Screen │
|
||||
│ │
|
||||
└─────────────────────────────────────────────────────────────┘
|
||||
```
|
||||
|
||||
### Render Pass Structure
|
||||
|
||||
```
|
||||
Main Render Pass
|
||||
├─> Clear screen
|
||||
├─> Custom wgpu callbacks (Stage pane, etc.)
|
||||
│ ├─> Vello vector rendering
|
||||
│ └─> Waveform rendering
|
||||
└─> egui UI rendering (text, widgets, overlays)
|
||||
```
|
||||
|
||||
## Vello Integration
|
||||
|
||||
Vello is a GPU-accelerated 2D rendering engine that uses compute shaders for high-performance vector graphics.
|
||||
|
||||
### Vello Architecture
|
||||
|
||||
```
|
||||
Document Shapes
|
||||
↓
|
||||
Convert to kurbo paths
|
||||
↓
|
||||
Build Vello Scene
|
||||
↓
|
||||
Vello Renderer (compute shaders)
|
||||
↓
|
||||
Render to GPU texture
|
||||
↓
|
||||
Composite with UI
|
||||
```
|
||||
|
||||
### Building a Vello Scene
|
||||
|
||||
```rust
|
||||
use vello::{Scene, SceneBuilder, kurbo::{Affine, BezPath}};
|
||||
use peniko::{Color, Fill, Brush};
|
||||
|
||||
fn build_vello_scene(document: &Document) -> Scene {
|
||||
let mut scene = Scene::new();
|
||||
let mut builder = SceneBuilder::for_scene(&mut scene);
|
||||
|
||||
for layer in &document.layers {
|
||||
if let Layer::VectorLayer { clips, visible, .. } = layer {
|
||||
if !visible {
|
||||
continue;
|
||||
}
|
||||
|
||||
for clip in clips {
|
||||
for shape_instance in &clip.shapes {
|
||||
// Get transform for this shape
|
||||
let transform = shape_instance.compute_world_transform();
|
||||
let affine = to_vello_affine(transform);
|
||||
|
||||
// Convert shape to kurbo path
|
||||
let path = shape_to_kurbo_path(&shape_instance.shape);
|
||||
|
||||
// Fill
|
||||
if let Some(fill_color) = shape_instance.shape.fill {
|
||||
let brush = Brush::Solid(to_peniko_color(fill_color));
|
||||
builder.fill(
|
||||
Fill::NonZero,
|
||||
affine,
|
||||
&brush,
|
||||
None,
|
||||
&path,
|
||||
);
|
||||
}
|
||||
|
||||
// Stroke
|
||||
if let Some(stroke) = &shape_instance.shape.stroke {
|
||||
let brush = Brush::Solid(to_peniko_color(stroke.color));
|
||||
let stroke_style = vello::kurbo::Stroke::new(stroke.width);
|
||||
builder.stroke(
|
||||
&stroke_style,
|
||||
affine,
|
||||
&brush,
|
||||
None,
|
||||
&path,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
scene
|
||||
}
|
||||
```
|
||||
|
||||
### Shape to Kurbo Path Conversion
|
||||
|
||||
```rust
|
||||
use kurbo::{BezPath, PathEl, Point};
|
||||
|
||||
fn shape_to_kurbo_path(shape: &Shape) -> BezPath {
|
||||
let mut path = BezPath::new();
|
||||
|
||||
if shape.curves.is_empty() {
|
||||
return path;
|
||||
}
|
||||
|
||||
// Start at first point
|
||||
path.move_to(Point::new(
|
||||
shape.curves[0].start.x as f64,
|
||||
shape.curves[0].start.y as f64,
|
||||
));
|
||||
|
||||
// Add curves
|
||||
for curve in &shape.curves {
|
||||
match curve.curve_type {
|
||||
CurveType::Linear => {
|
||||
path.line_to(Point::new(
|
||||
curve.end.x as f64,
|
||||
curve.end.y as f64,
|
||||
));
|
||||
}
|
||||
CurveType::Quadratic => {
|
||||
path.quad_to(
|
||||
Point::new(curve.control1.x as f64, curve.control1.y as f64),
|
||||
Point::new(curve.end.x as f64, curve.end.y as f64),
|
||||
);
|
||||
}
|
||||
CurveType::Cubic => {
|
||||
path.curve_to(
|
||||
Point::new(curve.control1.x as f64, curve.control1.y as f64),
|
||||
Point::new(curve.control2.x as f64, curve.control2.y as f64),
|
||||
Point::new(curve.end.x as f64, curve.end.y as f64),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Close path if needed
|
||||
if shape.closed {
|
||||
path.close_path();
|
||||
}
|
||||
|
||||
path
|
||||
}
|
||||
```
|
||||
|
||||
### Vello Renderer Setup
|
||||
|
||||
```rust
|
||||
use vello::{Renderer, RendererOptions, RenderParams};
|
||||
use wgpu;
|
||||
|
||||
pub struct VelloRenderer {
|
||||
renderer: Renderer,
|
||||
surface_format: wgpu::TextureFormat,
|
||||
}
|
||||
|
||||
impl VelloRenderer {
|
||||
pub fn new(device: &wgpu::Device, surface_format: wgpu::TextureFormat) -> Self {
|
||||
let renderer = Renderer::new(
|
||||
device,
|
||||
RendererOptions {
|
||||
surface_format: Some(surface_format),
|
||||
use_cpu: false,
|
||||
antialiasing_support: vello::AaSupport::all(),
|
||||
num_init_threads: None,
|
||||
},
|
||||
).expect("Failed to create Vello renderer");
|
||||
|
||||
Self {
|
||||
renderer,
|
||||
surface_format,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn render(
|
||||
&mut self,
|
||||
device: &wgpu::Device,
|
||||
queue: &wgpu::Queue,
|
||||
scene: &Scene,
|
||||
texture: &wgpu::TextureView,
|
||||
width: u32,
|
||||
height: u32,
|
||||
) {
|
||||
let params = RenderParams {
|
||||
base_color: peniko::Color::TRANSPARENT,
|
||||
width,
|
||||
height,
|
||||
antialiasing_method: vello::AaConfig::Msaa16,
|
||||
};
|
||||
|
||||
self.renderer
|
||||
.render_to_texture(device, queue, scene, texture, ¶ms)
|
||||
.expect("Failed to render Vello scene");
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Waveform Rendering
|
||||
|
||||
Audio waveforms are rendered on the GPU using custom WGSL shaders with mipmapping for efficient zooming.
|
||||
|
||||
### Waveform GPU Resources
|
||||
|
||||
```rust
|
||||
pub struct WaveformGPU {
|
||||
// Waveform data texture (min/max per sample)
|
||||
texture: wgpu::Texture,
|
||||
texture_view: wgpu::TextureView,
|
||||
|
||||
// Mipmap chain for level-of-detail
|
||||
mip_levels: Vec<wgpu::TextureView>,
|
||||
|
||||
// Render pipeline
|
||||
pipeline: wgpu::RenderPipeline,
|
||||
|
||||
// Uniform buffer for view parameters
|
||||
uniform_buffer: wgpu::Buffer,
|
||||
bind_group: wgpu::BindGroup,
|
||||
}
|
||||
```
|
||||
|
||||
### Waveform Texture Format
|
||||
|
||||
Each texel stores min/max amplitude for a sample range:
|
||||
|
||||
```
|
||||
Texture Format: Rgba16Float (4 channels, 16-bit float each)
|
||||
- R channel: Left channel minimum amplitude in range [-1, 1]
|
||||
- G channel: Left channel maximum amplitude in range [-1, 1]
|
||||
- B channel: Right channel minimum amplitude in range [-1, 1]
|
||||
- A channel: Right channel maximum amplitude in range [-1, 1]
|
||||
|
||||
Mip level 0: Per-sample min/max (1x)
|
||||
Mip level 1: Per-4-sample min/max (1/4x)
|
||||
Mip level 2: Per-16-sample min/max (1/16x)
|
||||
Mip level 3: Per-64-sample min/max (1/64x)
|
||||
...
|
||||
|
||||
Each mip level reduces by 4x, not 2x, for efficient zooming.
|
||||
```
|
||||
|
||||
### Generating Waveform Texture
|
||||
|
||||
```rust
|
||||
fn generate_waveform_texture(
|
||||
device: &wgpu::Device,
|
||||
queue: &wgpu::Queue,
|
||||
audio_samples: &[f32],
|
||||
) -> wgpu::Texture {
|
||||
// Calculate mip levels
|
||||
let width = audio_samples.len() as u32;
|
||||
let mip_levels = (width as f32).log2().floor() as u32 + 1;
|
||||
|
||||
// Create texture
|
||||
let texture = device.create_texture(&wgpu::TextureDescriptor {
|
||||
label: Some("Waveform Texture"),
|
||||
size: wgpu::Extent3d {
|
||||
width,
|
||||
height: 1,
|
||||
depth_or_array_layers: 1,
|
||||
},
|
||||
mip_level_count: mip_levels,
|
||||
sample_count: 1,
|
||||
dimension: wgpu::TextureDimension::D1,
|
||||
format: wgpu::TextureFormat::Rg32Float,
|
||||
usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST,
|
||||
view_formats: &[],
|
||||
});
|
||||
|
||||
// Upload base level (per-sample min/max)
|
||||
let mut data: Vec<f32> = Vec::with_capacity(width as usize * 2);
|
||||
for &sample in audio_samples {
|
||||
data.push(sample); // min
|
||||
data.push(sample); // max
|
||||
}
|
||||
|
||||
queue.write_texture(
|
||||
wgpu::ImageCopyTexture {
|
||||
texture: &texture,
|
||||
mip_level: 0,
|
||||
origin: wgpu::Origin3d::ZERO,
|
||||
aspect: wgpu::TextureAspect::All,
|
||||
},
|
||||
bytemuck::cast_slice(&data),
|
||||
wgpu::ImageDataLayout {
|
||||
offset: 0,
|
||||
bytes_per_row: Some(width * 8), // 2 floats * 4 bytes
|
||||
rows_per_image: None,
|
||||
},
|
||||
wgpu::Extent3d {
|
||||
width,
|
||||
height: 1,
|
||||
depth_or_array_layers: 1,
|
||||
},
|
||||
);
|
||||
|
||||
texture
|
||||
}
|
||||
```
|
||||
|
||||
### Mipmap Generation (Compute Shader)
|
||||
|
||||
```rust
|
||||
// Compute shader generates mipmaps by taking min/max of 4 parent samples
|
||||
// Each mip level is 4x smaller than the previous level
|
||||
fn generate_mipmaps(
|
||||
device: &wgpu::Device,
|
||||
queue: &wgpu::Queue,
|
||||
texture: &wgpu::Texture,
|
||||
base_width: u32,
|
||||
base_height: u32,
|
||||
mip_count: u32,
|
||||
base_sample_count: u32,
|
||||
) -> Vec<wgpu::CommandBuffer> {
|
||||
if mip_count <= 1 {
|
||||
return Vec::new();
|
||||
}
|
||||
|
||||
let mut encoder = device.create_command_encoder(&Default::default());
|
||||
|
||||
let mut src_width = base_width;
|
||||
let mut src_height = base_height;
|
||||
let mut src_sample_count = base_sample_count;
|
||||
|
||||
for level in 1..mip_count {
|
||||
// Dimensions halve (2x2 texels -> 1 texel)
|
||||
let dst_width = (src_width / 2).max(1);
|
||||
let dst_height = (src_height / 2).max(1);
|
||||
// But sample count reduces by 4x (4 samples -> 1)
|
||||
let dst_sample_count = (src_sample_count + 3) / 4;
|
||||
|
||||
let src_view = texture.create_view(&wgpu::TextureViewDescriptor {
|
||||
base_mip_level: level - 1,
|
||||
mip_level_count: Some(1),
|
||||
..Default::default()
|
||||
});
|
||||
|
||||
let dst_view = texture.create_view(&wgpu::TextureViewDescriptor {
|
||||
base_mip_level: level,
|
||||
mip_level_count: Some(1),
|
||||
..Default::default()
|
||||
});
|
||||
|
||||
let params = MipgenParams {
|
||||
src_width,
|
||||
dst_width,
|
||||
src_sample_count,
|
||||
_pad: 0,
|
||||
};
|
||||
let params_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
||||
contents: bytemuck::cast_slice(&[params]),
|
||||
usage: wgpu::BufferUsages::UNIFORM,
|
||||
});
|
||||
|
||||
let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
|
||||
layout: &mipgen_bind_group_layout,
|
||||
entries: &[
|
||||
wgpu::BindGroupEntry {
|
||||
binding: 0,
|
||||
resource: wgpu::BindingResource::TextureView(&src_view),
|
||||
},
|
||||
wgpu::BindGroupEntry {
|
||||
binding: 1,
|
||||
resource: wgpu::BindingResource::TextureView(&dst_view),
|
||||
},
|
||||
wgpu::BindGroupEntry {
|
||||
binding: 2,
|
||||
resource: params_buffer.as_entire_binding(),
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
// Dispatch compute shader
|
||||
let total_dst_texels = dst_width * dst_height;
|
||||
let workgroup_count = (total_dst_texels + 63) / 64;
|
||||
|
||||
let mut pass = encoder.begin_compute_pass(&Default::default());
|
||||
pass.set_pipeline(&mipgen_pipeline);
|
||||
pass.set_bind_group(0, &bind_group, &[]);
|
||||
pass.dispatch_workgroups(workgroup_count, 1, 1);
|
||||
drop(pass);
|
||||
|
||||
src_width = dst_width;
|
||||
src_height = dst_height;
|
||||
src_sample_count = dst_sample_count;
|
||||
}
|
||||
|
||||
vec![encoder.finish()]
|
||||
}
|
||||
```
|
||||
|
||||
## WGSL Shaders
|
||||
|
||||
### Waveform Render Shader
|
||||
|
||||
```wgsl
|
||||
// waveform.wgsl
|
||||
|
||||
struct WaveformParams {
|
||||
view_matrix: mat4x4<f32>, // 64 bytes
|
||||
viewport_size: vec2<f32>, // 8 bytes
|
||||
zoom: f32, // 4 bytes
|
||||
_pad1: f32, // 4 bytes (padding)
|
||||
tint_color: vec4<f32>, // 16 bytes (requires 16-byte alignment)
|
||||
// Total: 96 bytes
|
||||
}
|
||||
|
||||
@group(0) @binding(0) var<uniform> params: WaveformParams;
|
||||
@group(0) @binding(1) var waveform_texture: texture_1d<f32>;
|
||||
@group(0) @binding(2) var waveform_sampler: sampler;
|
||||
|
||||
struct VertexOutput {
|
||||
@builtin(position) position: vec4<f32>,
|
||||
@location(0) uv: vec2<f32>,
|
||||
}
|
||||
|
||||
@vertex
|
||||
fn vs_main(@builtin(vertex_index) vertex_index: u32) -> VertexOutput {
|
||||
// Generate fullscreen quad
|
||||
var positions = array<vec2<f32>, 6>(
|
||||
vec2(-1.0, -1.0),
|
||||
vec2( 1.0, -1.0),
|
||||
vec2( 1.0, 1.0),
|
||||
vec2(-1.0, -1.0),
|
||||
vec2( 1.0, 1.0),
|
||||
vec2(-1.0, 1.0),
|
||||
);
|
||||
|
||||
var output: VertexOutput;
|
||||
output.position = vec4(positions[vertex_index], 0.0, 1.0);
|
||||
output.uv = (positions[vertex_index] + 1.0) * 0.5;
|
||||
return output;
|
||||
}
|
||||
|
||||
@fragment
|
||||
fn fs_main(input: VertexOutput) -> @location(0) vec4<f32> {
|
||||
// Sample waveform texture
|
||||
let sample_pos = input.uv.x;
|
||||
let waveform = textureSample(waveform_texture, waveform_sampler, sample_pos);
|
||||
|
||||
// waveform.r = min amplitude, waveform.g = max amplitude
|
||||
let min_amp = waveform.r;
|
||||
let max_amp = waveform.g;
|
||||
|
||||
// Map amplitude to vertical position
|
||||
let center_y = 0.5;
|
||||
let min_y = center_y - min_amp * 0.5;
|
||||
let max_y = center_y + max_amp * 0.5;
|
||||
|
||||
// Check if pixel is within waveform range
|
||||
if (input.uv.y >= min_y && input.uv.y <= max_y) {
|
||||
return params.tint_color;
|
||||
} else {
|
||||
return vec4(0.0, 0.0, 0.0, 0.0); // Transparent
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Mipmap Generation Shader
|
||||
|
||||
```wgsl
|
||||
// waveform_mipgen.wgsl
|
||||
|
||||
struct MipgenParams {
|
||||
src_width: u32,
|
||||
dst_width: u32,
|
||||
src_sample_count: u32,
|
||||
}
|
||||
|
||||
@group(0) @binding(0) var src_texture: texture_2d<f32>;
|
||||
@group(0) @binding(1) var dst_texture: texture_storage_2d<rgba16float, write>;
|
||||
@group(0) @binding(2) var<uniform> params: MipgenParams;
|
||||
|
||||
@compute @workgroup_size(64)
|
||||
fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {
|
||||
let linear_index = global_id.x;
|
||||
|
||||
// Convert linear index to 2D coordinates
|
||||
let dst_x = linear_index % params.dst_width;
|
||||
let dst_y = linear_index / params.dst_width;
|
||||
|
||||
// Each dst texel corresponds to 4 src samples (not 4 src texels)
|
||||
// But 2D texture layout halves in each dimension
|
||||
let src_x = dst_x * 2u;
|
||||
let src_y = dst_y * 2u;
|
||||
|
||||
// Sample 4 texels from parent level (2x2 block)
|
||||
let s00 = textureLoad(src_texture, vec2<i32>(i32(src_x), i32(src_y)), 0);
|
||||
let s10 = textureLoad(src_texture, vec2<i32>(i32(src_x + 1u), i32(src_y)), 0);
|
||||
let s01 = textureLoad(src_texture, vec2<i32>(i32(src_x), i32(src_y + 1u)), 0);
|
||||
let s11 = textureLoad(src_texture, vec2<i32>(i32(src_x + 1u), i32(src_y + 1u)), 0);
|
||||
|
||||
// Compute min/max across all 4 samples for each channel
|
||||
let left_min = min(min(s00.r, s10.r), min(s01.r, s11.r));
|
||||
let left_max = max(max(s00.g, s10.g), max(s01.g, s11.g));
|
||||
let right_min = min(min(s00.b, s10.b), min(s01.b, s11.b));
|
||||
let right_max = max(max(s00.a, s10.a), max(s01.a, s11.a));
|
||||
|
||||
// Write to destination mip level
|
||||
textureStore(dst_texture, vec2<i32>(i32(dst_x), i32(dst_y)),
|
||||
vec4(left_min, left_max, right_min, right_max));
|
||||
}
|
||||
```
|
||||
|
||||
## Uniform Buffer Alignment
|
||||
|
||||
WGSL has strict alignment requirements. The most common issue is `vec4<f32>` requiring 16-byte alignment.
|
||||
|
||||
### Alignment Rules
|
||||
|
||||
```rust
|
||||
// ❌ Bad: tint_color not aligned to 16 bytes
|
||||
#[repr(C)]
|
||||
struct WaveformParams {
|
||||
view_matrix: [f32; 16], // 64 bytes (offset 0)
|
||||
viewport_size: [f32; 2], // 8 bytes (offset 64)
|
||||
zoom: f32, // 4 bytes (offset 72)
|
||||
tint_color: [f32; 4], // 16 bytes (offset 76) ❌ Not 16-byte aligned!
|
||||
}
|
||||
|
||||
// ✅ Good: explicit padding for alignment
|
||||
#[repr(C)]
|
||||
#[derive(Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
|
||||
struct WaveformParams {
|
||||
view_matrix: [f32; 16], // 64 bytes (offset 0)
|
||||
viewport_size: [f32; 2], // 8 bytes (offset 64)
|
||||
zoom: f32, // 4 bytes (offset 72)
|
||||
_pad1: f32, // 4 bytes (offset 76) - padding
|
||||
tint_color: [f32; 4], // 16 bytes (offset 80) ✅ 16-byte aligned!
|
||||
}
|
||||
// Total size: 96 bytes
|
||||
```
|
||||
|
||||
### Common Alignment Requirements
|
||||
|
||||
| WGSL Type | Size | Alignment |
|
||||
|-----------|------|-----------|
|
||||
| `f32` | 4 bytes | 4 bytes |
|
||||
| `vec2<f32>` | 8 bytes | 8 bytes |
|
||||
| `vec3<f32>` | 12 bytes | 16 bytes ⚠️ |
|
||||
| `vec4<f32>` | 16 bytes | 16 bytes |
|
||||
| `mat4x4<f32>` | 64 bytes | 16 bytes |
|
||||
| Struct | Sum of members | 16 bytes (uniform buffers) |
|
||||
|
||||
### Debug Alignment Issues
|
||||
|
||||
```rust
|
||||
// Use static_assertions to catch alignment bugs at compile time
|
||||
use static_assertions::const_assert_eq;
|
||||
|
||||
const_assert_eq!(std::mem::size_of::<WaveformParams>(), 96);
|
||||
const_assert_eq!(std::mem::align_of::<WaveformParams>(), 16);
|
||||
|
||||
// Runtime validation
|
||||
fn validate_uniform_buffer<T: bytemuck::Pod>(data: &T) {
|
||||
let size = std::mem::size_of::<T>();
|
||||
let align = std::mem::align_of::<T>();
|
||||
|
||||
assert!(size % 16 == 0, "Uniform buffer size must be multiple of 16");
|
||||
assert!(align >= 16, "Uniform buffer must be 16-byte aligned");
|
||||
}
|
||||
```
|
||||
|
||||
## Custom wgpu Integration
|
||||
|
||||
### egui-wgpu Callback Pattern
|
||||
|
||||
```rust
|
||||
use egui_wgpu::CallbackTrait;
|
||||
|
||||
struct CustomRenderCallback {
|
||||
// Data needed for rendering
|
||||
scene: Scene,
|
||||
params: UniformData,
|
||||
}
|
||||
|
||||
impl CallbackTrait for CustomRenderCallback {
|
||||
fn prepare(
|
||||
&self,
|
||||
device: &wgpu::Device,
|
||||
queue: &wgpu::Queue,
|
||||
_screen_descriptor: &egui_wgpu::ScreenDescriptor,
|
||||
_encoder: &mut wgpu::CommandEncoder,
|
||||
resources: &mut egui_wgpu::CallbackResources,
|
||||
) -> Vec<wgpu::CommandBuffer> {
|
||||
// Update GPU resources (buffers, textures, etc.)
|
||||
// This runs before rendering
|
||||
|
||||
// Get or create renderer
|
||||
let renderer: &mut MyRenderer = resources.get_or_insert_with(|| {
|
||||
MyRenderer::new(device)
|
||||
});
|
||||
|
||||
// Update uniform buffer
|
||||
queue.write_buffer(&renderer.uniform_buffer, 0, bytemuck::bytes_of(&self.params));
|
||||
|
||||
vec![] // Return additional command buffers if needed
|
||||
}
|
||||
|
||||
fn paint<'a>(
|
||||
&'a self,
|
||||
_info: egui::PaintCallbackInfo,
|
||||
render_pass: &mut wgpu::RenderPass<'a>,
|
||||
resources: &'a egui_wgpu::CallbackResources,
|
||||
) {
|
||||
// Actual rendering
|
||||
let renderer: &MyRenderer = resources.get().unwrap();
|
||||
|
||||
render_pass.set_pipeline(&renderer.pipeline);
|
||||
render_pass.set_bind_group(0, &renderer.bind_group, &[]);
|
||||
render_pass.draw(0..6, 0..1); // Draw fullscreen quad
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Registering Callback in egui
|
||||
|
||||
```rust
|
||||
// In Stage pane render method
|
||||
let callback = egui_wgpu::Callback::new_paint_callback(
|
||||
rect,
|
||||
CustomRenderCallback {
|
||||
scene: self.build_scene(document),
|
||||
params: self.compute_params(),
|
||||
},
|
||||
);
|
||||
|
||||
ui.painter().add(callback);
|
||||
```
|
||||
|
||||
## Performance Optimization
|
||||
|
||||
### Minimize GPU↔CPU Transfer
|
||||
|
||||
```rust
|
||||
// ❌ Bad: Update uniform buffer every frame
|
||||
for frame in frames {
|
||||
queue.write_buffer(&uniform_buffer, 0, ¶ms);
|
||||
render();
|
||||
}
|
||||
|
||||
// ✅ Good: Only update when changed
|
||||
if params_changed {
|
||||
queue.write_buffer(&uniform_buffer, 0, ¶ms);
|
||||
}
|
||||
render();
|
||||
```
|
||||
|
||||
### Reuse GPU Resources
|
||||
|
||||
```rust
|
||||
// ✅ Good: Reuse textures and buffers
|
||||
struct WaveformCache {
|
||||
textures: HashMap<Uuid, wgpu::Texture>,
|
||||
}
|
||||
|
||||
impl WaveformCache {
|
||||
fn get_or_create(&mut self, clip_id: Uuid, audio_data: &[f32]) -> &wgpu::Texture {
|
||||
self.textures.entry(clip_id).or_insert_with(|| {
|
||||
generate_waveform_texture(device, queue, audio_data)
|
||||
})
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Batch Draw Calls
|
||||
|
||||
```rust
|
||||
// ❌ Bad: One draw call per shape
|
||||
for shape in shapes {
|
||||
render_pass.set_bind_group(0, &shape.bind_group, &[]);
|
||||
render_pass.draw(0..shape.vertex_count, 0..1);
|
||||
}
|
||||
|
||||
// ✅ Good: Batch into single draw call
|
||||
let batched_vertices = batch_shapes(shapes);
|
||||
render_pass.set_bind_group(0, &batched_bind_group, &[]);
|
||||
render_pass.draw(0..batched_vertices.len(), 0..1);
|
||||
```
|
||||
|
||||
### Use Mipmaps for Zooming
|
||||
|
||||
```rust
|
||||
// ✅ Good: Select appropriate mip level based on zoom
|
||||
let mip_level = ((1.0 / zoom).log2().floor() as u32).min(max_mip_level);
|
||||
let texture_view = texture.create_view(&wgpu::TextureViewDescriptor {
|
||||
base_mip_level: mip_level,
|
||||
mip_level_count: Some(1),
|
||||
..Default::default()
|
||||
});
|
||||
```
|
||||
|
||||
## Debugging Rendering Issues
|
||||
|
||||
### Enable wgpu Validation
|
||||
|
||||
```rust
|
||||
let instance = wgpu::Instance::new(wgpu::InstanceDescriptor {
|
||||
backends: wgpu::Backends::all(),
|
||||
dx12_shader_compiler: Default::default(),
|
||||
flags: wgpu::InstanceFlags::validation(), // Enable validation
|
||||
gles_minor_version: wgpu::Gles3MinorVersion::Automatic,
|
||||
});
|
||||
```
|
||||
|
||||
### Check for Errors
|
||||
|
||||
```rust
|
||||
// Set error handler
|
||||
device.on_uncaptured_error(Box::new(|error| {
|
||||
eprintln!("wgpu error: {:?}", error);
|
||||
}));
|
||||
```
|
||||
|
||||
### Capture GPU Frame
|
||||
|
||||
**Linux** (RenderDoc):
|
||||
```bash
|
||||
renderdoccmd capture ./lightningbeam-editor
|
||||
```
|
||||
|
||||
**macOS** (Xcode):
|
||||
- Run with GPU Frame Capture enabled
|
||||
- Trigger capture with Cmd+Option+G
|
||||
|
||||
### Common Issues
|
||||
|
||||
#### Black Screen
|
||||
- Check that vertex shader outputs correct clip-space coordinates
|
||||
- Verify texture bindings are correct
|
||||
- Check that render pipeline format matches surface format
|
||||
|
||||
#### Validation Errors
|
||||
- Check uniform buffer alignment (see [Uniform Buffer Alignment](#uniform-buffer-alignment))
|
||||
- Verify texture formats match shader expectations
|
||||
- Ensure bind groups match pipeline layout
|
||||
|
||||
#### Performance Issues
|
||||
- Use GPU profiler (RenderDoc, Xcode)
|
||||
- Check for redundant buffer uploads
|
||||
- Profile shader performance
|
||||
- Reduce draw call count via batching
|
||||
|
||||
## Related Documentation
|
||||
|
||||
- [ARCHITECTURE.md](../ARCHITECTURE.md) - Overall system architecture
|
||||
- [docs/UI_SYSTEM.md](UI_SYSTEM.md) - UI and pane integration
|
||||
- [CONTRIBUTING.md](../CONTRIBUTING.md) - Development workflow
|
||||
@@ -0,0 +1,848 @@
|
||||
# UI System Architecture
|
||||
|
||||
This document describes Lightningbeam's UI architecture, including the pane system, tool system, GPU integration, and patterns for extending the UI with new features.
|
||||
|
||||
## Table of Contents
|
||||
|
||||
- [Overview](#overview)
|
||||
- [Pane System](#pane-system)
|
||||
- [Shared State](#shared-state)
|
||||
- [Two-Phase Dispatch](#two-phase-dispatch)
|
||||
- [ID Collision Avoidance](#id-collision-avoidance)
|
||||
- [Tool System](#tool-system)
|
||||
- [GPU Integration](#gpu-integration)
|
||||
- [Adding New Panes](#adding-new-panes)
|
||||
- [Adding New Tools](#adding-new-tools)
|
||||
- [Event Handling](#event-handling)
|
||||
- [Best Practices](#best-practices)
|
||||
|
||||
## Overview
|
||||
|
||||
Lightningbeam's UI is built with **egui**, an immediate-mode GUI framework. Unlike retained-mode frameworks (Qt, GTK), immediate-mode rebuilds the UI every frame by running code that describes what should be displayed.
|
||||
|
||||
### Key Technologies
|
||||
|
||||
- **egui 0.33.3**: Immediate-mode GUI framework
|
||||
- **eframe**: Application framework wrapping egui
|
||||
- **winit**: Cross-platform windowing
|
||||
- **Vello**: GPU-accelerated 2D vector rendering
|
||||
- **wgpu**: Low-level GPU API
|
||||
- **egui-wgpu**: Integration layer between egui and wgpu
|
||||
|
||||
### Immediate Mode Overview
|
||||
|
||||
```rust
|
||||
// Immediate mode: UI is described every frame
|
||||
fn render(&mut self, ui: &mut egui::Ui) {
|
||||
if ui.button("Click me").clicked() {
|
||||
self.counter += 1;
|
||||
}
|
||||
ui.label(format!("Count: {}", self.counter));
|
||||
}
|
||||
```
|
||||
|
||||
**Benefits**:
|
||||
- Simple mental model (just describe what you see)
|
||||
- No manual synchronization between state and UI
|
||||
- Easy to compose and reuse components
|
||||
|
||||
**Considerations**:
|
||||
- Must avoid expensive operations in render code
|
||||
- IDs needed for stateful widgets (handled automatically in most cases)
|
||||
|
||||
## Pane System
|
||||
|
||||
Lightningbeam uses a flexible pane system where the UI is composed of independent, reusable panes (Stage, Timeline, Asset Library, etc.).
|
||||
|
||||
### Pane Architecture
|
||||
|
||||
```
|
||||
┌─────────────────────────────────────────────────────────┐
|
||||
│ Main Application │
|
||||
│ (LightningbeamApp) │
|
||||
├─────────────────────────────────────────────────────────┤
|
||||
│ │
|
||||
│ ┌────────────────────────────────────────────────┐ │
|
||||
│ │ Pane Tree (egui_tiles) │ │
|
||||
│ │ │ │
|
||||
│ │ ┌──────────┐ ┌──────────┐ ┌──────────┐ │ │
|
||||
│ │ │ Stage │ │ Timeline │ │ Asset │ │ │
|
||||
│ │ │ Pane │ │ Pane │ │ Library │ │ │
|
||||
│ │ └──────────┘ └──────────┘ └──────────┘ │ │
|
||||
│ │ │ │
|
||||
│ │ Each pane: │ │
|
||||
│ │ - Renders its UI │ │
|
||||
│ │ - Registers actions with SharedPaneState │ │
|
||||
│ │ - Accesses shared document state │ │
|
||||
│ └────────────────────────────────────────────────┘ │
|
||||
│ │
|
||||
│ ┌────────────────────────────────────────────────┐ │
|
||||
│ │ SharedPaneState │ │
|
||||
│ │ - Document │ │
|
||||
│ │ - Selected tool │ │
|
||||
│ │ - Pending actions │ │
|
||||
│ │ - Audio system │ │
|
||||
│ └────────────────────────────────────────────────┘ │
|
||||
│ │
|
||||
│ After all panes render: │
|
||||
│ - Execute pending actions │
|
||||
│ - Update undo/redo stacks │
|
||||
│ - Synchronize with audio engine │
|
||||
│ │
|
||||
└─────────────────────────────────────────────────────────┘
|
||||
```
|
||||
|
||||
### PaneInstance Enum
|
||||
|
||||
All panes are variants of the `PaneInstance` enum:
|
||||
|
||||
```rust
|
||||
// In lightningbeam-editor/src/panes/mod.rs
|
||||
pub enum PaneInstance {
|
||||
Stage(Stage),
|
||||
Timeline(Timeline),
|
||||
AssetLibrary(AssetLibrary),
|
||||
InfoPanel(InfoPanel),
|
||||
VirtualPiano(VirtualPiano),
|
||||
Toolbar(Toolbar),
|
||||
NodeEditor(NodeEditor),
|
||||
PianoRoll(PianoRoll),
|
||||
Outliner(Outliner),
|
||||
PresetBrowser(PresetBrowser),
|
||||
}
|
||||
|
||||
impl PaneInstance {
|
||||
pub fn render(&mut self, ui: &mut Ui, shared_state: &mut SharedPaneState) {
|
||||
match self {
|
||||
PaneInstance::Stage(stage) => stage.render(ui, shared_state),
|
||||
PaneInstance::Timeline(timeline) => timeline.render(ui, shared_state),
|
||||
PaneInstance::AssetLibrary(lib) => lib.render(ui, shared_state),
|
||||
// ... dispatch to specific pane
|
||||
}
|
||||
}
|
||||
|
||||
pub fn title(&self) -> &str {
|
||||
match self {
|
||||
PaneInstance::Stage(_) => "Stage",
|
||||
PaneInstance::Timeline(_) => "Timeline",
|
||||
// ...
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Individual Pane Structure
|
||||
|
||||
Each pane is a struct with its own state and a `render` method:
|
||||
|
||||
```rust
|
||||
pub struct MyPane {
|
||||
// Pane-specific state
|
||||
scroll_offset: f32,
|
||||
selected_item: Option<usize>,
|
||||
// ... other state
|
||||
}
|
||||
|
||||
impl MyPane {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
scroll_offset: 0.0,
|
||||
selected_item: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn render(&mut self, ui: &mut Ui, shared_state: &mut SharedPaneState) {
|
||||
// Render pane UI
|
||||
ui.heading("My Pane");
|
||||
|
||||
// Access shared state
|
||||
let document = &shared_state.document;
|
||||
|
||||
// Create actions
|
||||
if ui.button("Do something").clicked() {
|
||||
let action = Box::new(MyAction { /* ... */ });
|
||||
shared_state.pending_actions.push(action);
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Key Panes
|
||||
|
||||
Located in `lightningbeam-editor/src/panes/`:
|
||||
|
||||
- **stage.rs** (214KB): Main canvas for drawing and transform tools
|
||||
- **timeline.rs** (84KB): Multi-track timeline with clip editing
|
||||
- **asset_library.rs** (70KB): Asset browser with drag-to-timeline
|
||||
- **infopanel.rs** (31KB): Context-sensitive property editor
|
||||
- **virtual_piano.rs** (31KB): On-screen MIDI keyboard
|
||||
- **toolbar.rs** (9KB): Tool palette
|
||||
|
||||
## Shared State
|
||||
|
||||
`SharedPaneState` is passed to all panes during rendering to share data and coordinate actions.
|
||||
|
||||
### SharedPaneState Structure
|
||||
|
||||
```rust
|
||||
pub struct SharedPaneState {
|
||||
// Document state
|
||||
pub document: Document,
|
||||
pub undo_stack: Vec<Box<dyn Action>>,
|
||||
pub redo_stack: Vec<Box<dyn Action>>,
|
||||
|
||||
// Tool state
|
||||
pub selected_tool: Tool,
|
||||
pub tool_state: ToolState,
|
||||
|
||||
// Actions to execute after rendering
|
||||
pub pending_actions: Vec<Box<dyn Action>>,
|
||||
|
||||
// Audio engine
|
||||
pub audio_system: AudioSystem,
|
||||
pub playhead_position: f64,
|
||||
pub is_playing: bool,
|
||||
|
||||
// Selection state
|
||||
pub selected_clips: HashSet<Uuid>,
|
||||
pub selected_shapes: HashSet<Uuid>,
|
||||
|
||||
// Clipboard
|
||||
pub clipboard: Option<ClipboardData>,
|
||||
|
||||
// UI state
|
||||
pub show_grid: bool,
|
||||
pub snap_to_grid: bool,
|
||||
pub grid_size: f32,
|
||||
}
|
||||
```
|
||||
|
||||
### Accessing Shared State
|
||||
|
||||
```rust
|
||||
impl MyPane {
|
||||
pub fn render(&mut self, ui: &mut Ui, shared_state: &mut SharedPaneState) {
|
||||
// Read from document
|
||||
let layer_count = shared_state.document.layers.len();
|
||||
ui.label(format!("Layers: {}", layer_count));
|
||||
|
||||
// Check tool state
|
||||
if shared_state.selected_tool == Tool::Select {
|
||||
// ... render selection-specific UI
|
||||
}
|
||||
|
||||
// Check playback state
|
||||
if shared_state.is_playing {
|
||||
ui.label("▶ Playing");
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Two-Phase Dispatch
|
||||
|
||||
Panes cannot directly mutate shared state during rendering due to Rust's borrowing rules. Instead, they register actions to be executed after all panes have rendered.
|
||||
|
||||
### Why Two-Phase?
|
||||
|
||||
```rust
|
||||
// This doesn't work: can't borrow shared_state as mutable twice
|
||||
pub fn render(&mut self, ui: &mut Ui, shared_state: &mut SharedPaneState) {
|
||||
if ui.button("Add layer").clicked() {
|
||||
// ❌ Can't mutate document while borrowed by render
|
||||
shared_state.document.layers.push(Layer::new());
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Solution: Pending Actions
|
||||
|
||||
```rust
|
||||
// Phase 1: Register action during render
|
||||
pub fn render(&mut self, ui: &mut Ui, shared_state: &mut SharedPaneState) {
|
||||
if ui.button("Add layer").clicked() {
|
||||
let action = Box::new(AddLayerAction::new());
|
||||
shared_state.pending_actions.push(action);
|
||||
}
|
||||
}
|
||||
|
||||
// Phase 2: Execute after all panes rendered (in main app)
|
||||
for action in shared_state.pending_actions.drain(..) {
|
||||
action.apply(&mut shared_state.document);
|
||||
shared_state.undo_stack.push(action);
|
||||
}
|
||||
```
|
||||
|
||||
### Action Trait
|
||||
|
||||
All actions implement the `Action` trait:
|
||||
|
||||
```rust
|
||||
pub trait Action: Send {
|
||||
fn apply(&mut self, document: &mut Document);
|
||||
fn undo(&mut self, document: &mut Document);
|
||||
fn redo(&mut self, document: &mut Document);
|
||||
}
|
||||
```
|
||||
|
||||
Example action:
|
||||
|
||||
```rust
|
||||
pub struct AddLayerAction {
|
||||
layer_id: Uuid,
|
||||
layer_type: LayerType,
|
||||
}
|
||||
|
||||
impl Action for AddLayerAction {
|
||||
fn apply(&mut self, document: &mut Document) {
|
||||
let layer = Layer::new(self.layer_id, self.layer_type);
|
||||
document.layers.push(layer);
|
||||
}
|
||||
|
||||
fn undo(&mut self, document: &mut Document) {
|
||||
document.layers.retain(|l| l.id != self.layer_id);
|
||||
}
|
||||
|
||||
fn redo(&mut self, document: &mut Document) {
|
||||
self.apply(document);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## ID Collision Avoidance
|
||||
|
||||
egui uses IDs to track widget state across frames (e.g., scroll position, collapse state). When multiple instances of the same pane exist, IDs can collide.
|
||||
|
||||
### The Problem
|
||||
|
||||
```rust
|
||||
// If two Timeline panes exist, they'll share the same ID
|
||||
ui.collapsing("Track 1", |ui| {
|
||||
// ... content
|
||||
}); // ID is derived from label "Track 1"
|
||||
```
|
||||
|
||||
Both timeline instances would have the same "Track 1" ID, causing state conflicts.
|
||||
|
||||
### Solution: Salt IDs with Node Path
|
||||
|
||||
Each pane has a unique node path (e.g., `"root/0/1/2"`). Salt all IDs with this path:
|
||||
|
||||
```rust
|
||||
pub struct Timeline {
|
||||
node_path: String, // Unique path for this pane instance
|
||||
}
|
||||
|
||||
impl Timeline {
|
||||
pub fn render(&mut self, ui: &mut Ui, shared_state: &mut SharedPaneState) {
|
||||
// Salt IDs with node path
|
||||
ui.push_id(&self.node_path, |ui| {
|
||||
// Now all IDs within this closure are unique to this instance
|
||||
ui.collapsing("Track 1", |ui| {
|
||||
// ... content
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Alternative: Per-Widget Salting
|
||||
|
||||
For individual widgets:
|
||||
|
||||
```rust
|
||||
ui.collapsing("Track 1", |ui| {
|
||||
// ... content
|
||||
}).id.with(&self.node_path); // Salt this specific ID
|
||||
```
|
||||
|
||||
### Best Practice
|
||||
|
||||
**Always salt IDs in new panes** to support multiple instances:
|
||||
|
||||
```rust
|
||||
impl NewPane {
|
||||
pub fn render(&mut self, ui: &mut Ui, shared_state: &mut SharedPaneState) {
|
||||
ui.push_id(&self.node_path, |ui| {
|
||||
// All rendering code goes here
|
||||
});
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Tool System
|
||||
|
||||
Tools handle user input on the Stage pane (drawing, selection, transforms, etc.).
|
||||
|
||||
### Tool Enum
|
||||
|
||||
```rust
|
||||
pub enum Tool {
|
||||
Select,
|
||||
Draw,
|
||||
Rectangle,
|
||||
Ellipse,
|
||||
Line,
|
||||
PaintBucket,
|
||||
Transform,
|
||||
Eyedropper,
|
||||
}
|
||||
```
|
||||
|
||||
### Tool State
|
||||
|
||||
```rust
|
||||
pub struct ToolState {
|
||||
// Generic tool state
|
||||
pub mouse_pos: Pos2,
|
||||
pub mouse_down: bool,
|
||||
pub drag_start: Option<Pos2>,
|
||||
|
||||
// Tool-specific state
|
||||
pub draw_points: Vec<Pos2>,
|
||||
pub transform_mode: TransformMode,
|
||||
pub paint_bucket_tolerance: f32,
|
||||
}
|
||||
```
|
||||
|
||||
### Tool Implementation
|
||||
|
||||
Tools implement the `ToolBehavior` trait:
|
||||
|
||||
```rust
|
||||
pub trait ToolBehavior {
|
||||
fn on_mouse_down(&mut self, pos: Pos2, shared_state: &mut SharedPaneState);
|
||||
fn on_mouse_move(&mut self, pos: Pos2, shared_state: &mut SharedPaneState);
|
||||
fn on_mouse_up(&mut self, pos: Pos2, shared_state: &mut SharedPaneState);
|
||||
fn on_key(&mut self, key: Key, shared_state: &mut SharedPaneState);
|
||||
fn render_overlay(&self, painter: &Painter);
|
||||
}
|
||||
```
|
||||
|
||||
Example: Rectangle tool:
|
||||
|
||||
```rust
|
||||
pub struct RectangleTool {
|
||||
start_pos: Option<Pos2>,
|
||||
}
|
||||
|
||||
impl ToolBehavior for RectangleTool {
|
||||
fn on_mouse_down(&mut self, pos: Pos2, _shared_state: &mut SharedPaneState) {
|
||||
self.start_pos = Some(pos);
|
||||
}
|
||||
|
||||
fn on_mouse_move(&mut self, pos: Pos2, _shared_state: &mut SharedPaneState) {
|
||||
// Visual feedback handled in render_overlay
|
||||
}
|
||||
|
||||
fn on_mouse_up(&mut self, pos: Pos2, shared_state: &mut SharedPaneState) {
|
||||
if let Some(start) = self.start_pos.take() {
|
||||
// Create rectangle shape
|
||||
let rect = Rect::from_two_pos(start, pos);
|
||||
let action = Box::new(AddShapeAction::rectangle(rect));
|
||||
shared_state.pending_actions.push(action);
|
||||
}
|
||||
}
|
||||
|
||||
fn render_overlay(&self, painter: &Painter) {
|
||||
if let Some(start) = self.start_pos {
|
||||
let current = painter.mouse_pos();
|
||||
let rect = Rect::from_two_pos(start, current);
|
||||
painter.rect_stroke(rect, 0.0, Stroke::new(2.0, Color32::WHITE));
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Tool Selection
|
||||
|
||||
```rust
|
||||
// In Toolbar pane
|
||||
if ui.button("✏ Draw").clicked() {
|
||||
shared_state.selected_tool = Tool::Draw;
|
||||
}
|
||||
|
||||
// In Stage pane
|
||||
match shared_state.selected_tool {
|
||||
Tool::Draw => self.draw_tool.on_mouse_move(pos, shared_state),
|
||||
Tool::Select => self.select_tool.on_mouse_move(pos, shared_state),
|
||||
// ...
|
||||
}
|
||||
```
|
||||
|
||||
## GPU Integration
|
||||
|
||||
The Stage pane uses custom wgpu rendering for vector graphics and waveforms.
|
||||
|
||||
### egui-wgpu Callbacks
|
||||
|
||||
```rust
|
||||
// In Stage::render()
|
||||
ui.painter().add(egui_wgpu::Callback::new_paint_callback(
|
||||
rect,
|
||||
StageCallback {
|
||||
document: shared_state.document.clone(),
|
||||
vello_renderer: self.vello_renderer.clone(),
|
||||
waveform_renderer: self.waveform_renderer.clone(),
|
||||
},
|
||||
));
|
||||
```
|
||||
|
||||
### Callback Implementation
|
||||
|
||||
```rust
|
||||
struct StageCallback {
|
||||
document: Document,
|
||||
vello_renderer: Arc<Mutex<VelloRenderer>>,
|
||||
waveform_renderer: Arc<Mutex<WaveformRenderer>>,
|
||||
}
|
||||
|
||||
impl egui_wgpu::CallbackTrait for StageCallback {
|
||||
fn prepare(
|
||||
&self,
|
||||
device: &wgpu::Device,
|
||||
queue: &wgpu::Queue,
|
||||
encoder: &mut wgpu::CommandEncoder,
|
||||
resources: &egui_wgpu::CallbackResources,
|
||||
) -> Vec<wgpu::CommandBuffer> {
|
||||
// Prepare GPU resources
|
||||
let mut vello = self.vello_renderer.lock().unwrap();
|
||||
vello.prepare_scene(&self.document);
|
||||
|
||||
vec![]
|
||||
}
|
||||
|
||||
fn paint<'a>(
|
||||
&'a self,
|
||||
info: egui::PaintCallbackInfo,
|
||||
render_pass: &mut wgpu::RenderPass<'a>,
|
||||
resources: &'a egui_wgpu::CallbackResources,
|
||||
) {
|
||||
// Render vector graphics
|
||||
let vello = self.vello_renderer.lock().unwrap();
|
||||
vello.render(render_pass);
|
||||
|
||||
// Render waveforms
|
||||
let waveforms = self.waveform_renderer.lock().unwrap();
|
||||
waveforms.render(render_pass);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Vello Integration
|
||||
|
||||
Vello renders 2D vector graphics using GPU compute shaders:
|
||||
|
||||
```rust
|
||||
use vello::{Scene, SceneBuilder, kurbo};
|
||||
|
||||
fn build_vello_scene(document: &Document) -> Scene {
|
||||
let mut scene = Scene::new();
|
||||
let mut builder = SceneBuilder::for_scene(&mut scene);
|
||||
|
||||
for layer in &document.layers {
|
||||
if let Layer::VectorLayer { clips, .. } = layer {
|
||||
for clip in clips {
|
||||
for shape in &clip.shapes {
|
||||
// Convert shape to kurbo path
|
||||
let path = shape.to_kurbo_path();
|
||||
|
||||
// Add to scene with fill/stroke
|
||||
builder.fill(
|
||||
Fill::NonZero,
|
||||
Affine::IDENTITY,
|
||||
&shape.fill_color,
|
||||
None,
|
||||
&path,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
scene
|
||||
}
|
||||
```
|
||||
|
||||
## Adding New Panes
|
||||
|
||||
### Step 1: Create Pane Struct
|
||||
|
||||
```rust
|
||||
// In lightningbeam-editor/src/panes/my_pane.rs
|
||||
pub struct MyPane {
|
||||
node_path: String,
|
||||
// Pane-specific state
|
||||
selected_index: usize,
|
||||
scroll_offset: f32,
|
||||
}
|
||||
|
||||
impl MyPane {
|
||||
pub fn new(node_path: String) -> Self {
|
||||
Self {
|
||||
node_path,
|
||||
selected_index: 0,
|
||||
scroll_offset: 0.0,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn render(&mut self, ui: &mut Ui, shared_state: &mut SharedPaneState) {
|
||||
// IMPORTANT: Salt IDs with node path
|
||||
ui.push_id(&self.node_path, |ui| {
|
||||
ui.heading("My Pane");
|
||||
|
||||
// Render pane content
|
||||
// ...
|
||||
});
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Step 2: Add to PaneInstance Enum
|
||||
|
||||
```rust
|
||||
// In lightningbeam-editor/src/panes/mod.rs
|
||||
pub enum PaneInstance {
|
||||
// ... existing variants
|
||||
MyPane(MyPane),
|
||||
}
|
||||
|
||||
impl PaneInstance {
|
||||
pub fn render(&mut self, ui: &mut Ui, shared_state: &mut SharedPaneState) {
|
||||
match self {
|
||||
// ... existing cases
|
||||
PaneInstance::MyPane(pane) => pane.render(ui, shared_state),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn title(&self) -> &str {
|
||||
match self {
|
||||
// ... existing cases
|
||||
PaneInstance::MyPane(_) => "My Pane",
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Step 3: Add to Menu
|
||||
|
||||
```rust
|
||||
// In main application
|
||||
if ui.button("My Pane").clicked() {
|
||||
let pane = PaneInstance::MyPane(MyPane::new(generate_node_path()));
|
||||
app.add_pane(pane);
|
||||
}
|
||||
```
|
||||
|
||||
## Adding New Tools
|
||||
|
||||
### Step 1: Add to Tool Enum
|
||||
|
||||
```rust
|
||||
pub enum Tool {
|
||||
// ... existing tools
|
||||
MyTool,
|
||||
}
|
||||
```
|
||||
|
||||
### Step 2: Implement Tool Behavior
|
||||
|
||||
```rust
|
||||
pub struct MyToolState {
|
||||
// Tool-specific state
|
||||
start_pos: Option<Pos2>,
|
||||
}
|
||||
|
||||
impl MyToolState {
|
||||
pub fn handle_input(
|
||||
&mut self,
|
||||
response: &Response,
|
||||
shared_state: &mut SharedPaneState,
|
||||
) {
|
||||
if response.clicked() {
|
||||
self.start_pos = response.interact_pointer_pos();
|
||||
}
|
||||
|
||||
if response.drag_released() {
|
||||
if let Some(start) = self.start_pos.take() {
|
||||
// Create action
|
||||
let action = Box::new(MyAction { /* ... */ });
|
||||
shared_state.pending_actions.push(action);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn render_overlay(&self, painter: &Painter) {
|
||||
// Draw tool-specific overlay
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Step 3: Add to Toolbar
|
||||
|
||||
```rust
|
||||
// In Toolbar pane
|
||||
if ui.button("🔧 My Tool").clicked() {
|
||||
shared_state.selected_tool = Tool::MyTool;
|
||||
}
|
||||
```
|
||||
|
||||
### Step 4: Handle in Stage Pane
|
||||
|
||||
```rust
|
||||
// In Stage pane
|
||||
match shared_state.selected_tool {
|
||||
// ... existing tools
|
||||
Tool::MyTool => self.my_tool_state.handle_input(&response, shared_state),
|
||||
}
|
||||
|
||||
// Render overlay
|
||||
match shared_state.selected_tool {
|
||||
// ... existing tools
|
||||
Tool::MyTool => self.my_tool_state.render_overlay(&painter),
|
||||
}
|
||||
```
|
||||
|
||||
## Event Handling
|
||||
|
||||
### Mouse Events
|
||||
|
||||
```rust
|
||||
let response = ui.allocate_rect(rect, Sense::click_and_drag());
|
||||
|
||||
if response.clicked() {
|
||||
let pos = response.interact_pointer_pos().unwrap();
|
||||
// Handle click at pos
|
||||
}
|
||||
|
||||
if response.dragged() {
|
||||
let delta = response.drag_delta();
|
||||
// Handle drag by delta
|
||||
}
|
||||
|
||||
if response.drag_released() {
|
||||
// Handle drag end
|
||||
}
|
||||
```
|
||||
|
||||
### Keyboard Events
|
||||
|
||||
```rust
|
||||
ui.input(|i| {
|
||||
if i.key_pressed(Key::Delete) {
|
||||
// Delete selected items
|
||||
}
|
||||
|
||||
if i.modifiers.ctrl && i.key_pressed(Key::Z) {
|
||||
// Undo
|
||||
}
|
||||
|
||||
if i.modifiers.ctrl && i.key_pressed(Key::Y) {
|
||||
// Redo
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
### Drag and Drop
|
||||
|
||||
```rust
|
||||
// Source (Asset Library)
|
||||
let response = ui.label("Audio Clip");
|
||||
if response.dragged() {
|
||||
let payload = DragPayload::AudioClip(clip_id);
|
||||
ui.memory_mut(|mem| {
|
||||
mem.data.insert_temp(Id::new("drag_payload"), payload);
|
||||
});
|
||||
}
|
||||
|
||||
// Target (Timeline)
|
||||
let response = ui.allocate_rect(rect, Sense::hover());
|
||||
if response.hovered() {
|
||||
if let Some(payload) = ui.memory(|mem| mem.data.get_temp::<DragPayload>(Id::new("drag_payload"))) {
|
||||
// Handle drop
|
||||
let action = Box::new(AddClipAction { clip_id: payload.clip_id(), position });
|
||||
shared_state.pending_actions.push(action);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Best Practices
|
||||
|
||||
### 1. Always Salt IDs
|
||||
|
||||
```rust
|
||||
// ✅ Good
|
||||
ui.push_id(&self.node_path, |ui| {
|
||||
// All rendering here
|
||||
});
|
||||
|
||||
// ❌ Bad (ID collisions if multiple instances)
|
||||
ui.collapsing("Settings", |ui| {
|
||||
// ...
|
||||
});
|
||||
```
|
||||
|
||||
### 2. Use Pending Actions
|
||||
|
||||
```rust
|
||||
// ✅ Good
|
||||
shared_state.pending_actions.push(Box::new(action));
|
||||
|
||||
// ❌ Bad (borrowing conflicts)
|
||||
shared_state.document.layers.push(layer);
|
||||
```
|
||||
|
||||
### 3. Split Borrows with std::mem::take
|
||||
|
||||
```rust
|
||||
// ✅ Good
|
||||
let mut clips = std::mem::take(&mut self.clips);
|
||||
for clip in &mut clips {
|
||||
self.render_clip(ui, clip); // Can borrow self immutably
|
||||
}
|
||||
self.clips = clips;
|
||||
|
||||
// ❌ Bad (can't borrow self while iterating clips)
|
||||
for clip in &mut self.clips {
|
||||
self.render_clip(ui, clip); // Error!
|
||||
}
|
||||
```
|
||||
|
||||
### 4. Avoid Expensive Operations in Render
|
||||
|
||||
```rust
|
||||
// ❌ Bad (heavy computation every frame)
|
||||
pub fn render(&mut self, ui: &mut Ui, shared_state: &mut SharedPaneState) {
|
||||
let thumbnail = self.generate_thumbnail(); // Expensive!
|
||||
ui.image(thumbnail);
|
||||
}
|
||||
|
||||
// ✅ Good (cache result)
|
||||
pub fn render(&mut self, ui: &mut Ui, shared_state: &mut SharedPaneState) {
|
||||
if self.thumbnail_cache.is_none() {
|
||||
self.thumbnail_cache = Some(self.generate_thumbnail());
|
||||
}
|
||||
ui.image(self.thumbnail_cache.as_ref().unwrap());
|
||||
}
|
||||
```
|
||||
|
||||
### 5. Handle Missing State Gracefully
|
||||
|
||||
```rust
|
||||
// ✅ Good
|
||||
if let Some(layer) = document.layers.get(layer_index) {
|
||||
// Render layer
|
||||
} else {
|
||||
ui.label("Layer not found");
|
||||
}
|
||||
|
||||
// ❌ Bad (panics if layer missing)
|
||||
let layer = &document.layers[layer_index]; // May panic!
|
||||
```
|
||||
|
||||
## Related Documentation
|
||||
|
||||
- [ARCHITECTURE.md](../ARCHITECTURE.md) - Overall system architecture
|
||||
- [docs/AUDIO_SYSTEM.md](AUDIO_SYSTEM.md) - Audio engine integration
|
||||
- [docs/RENDERING.md](RENDERING.md) - GPU rendering details
|
||||
- [CONTRIBUTING.md](../CONTRIBUTING.md) - Development workflow
|
||||
Reference in New Issue
Block a user