Fix gamma handling and improve brush canvas performance
Color correctness: - Unpremultiply before the sRGB OETF on the display and export blits; encoding premultiplied color corrupted antialiased/transparent edges. - Tag exported video as full-range BT.709 (matrix/primaries/transfer). - Run perception effects (invert, brightness/contrast, hue/saturation) in gamma space to match standard editors. - Interpolate gradients in gamma space across the raster and vector paths. - Render effect thumbnails in the same linear space as the live pipeline. Brush performance: - Store the raster canvas as Rgba16Float (no shadow banding from 8-bit linear), with an incremental per-tile ping-pong sync replacing the per-frame full-canvas copy. - Do the linear->sRGB readback conversion on the GPU and reuse a cached scratch texture, dropping a ~110ms-per-stroke CPU decode. Cleanup: - Single COLOR_WGSL prelude and shared CPU sRGB scalars instead of ~8 duplicated copies of the transfer functions. - Shared compute-pipeline builder; smudge folded onto the tile-sync path.
This commit is contained in:
@@ -75,7 +75,7 @@ impl EffectRegistry {
|
||||
INVERT_ID,
|
||||
"Invert",
|
||||
EffectCategory::Color,
|
||||
include_str!("shaders/effect_invert.wgsl"),
|
||||
format!("{}\n{}", crate::gpu::COLOR_WGSL, include_str!("shaders/effect_invert.wgsl")),
|
||||
vec![
|
||||
EffectParameterDef::float_range("amount", "Amount", 1.0, 0.0, 1.0),
|
||||
],
|
||||
@@ -88,7 +88,7 @@ impl EffectRegistry {
|
||||
BRIGHTNESS_CONTRAST_ID,
|
||||
"Brightness/Contrast",
|
||||
EffectCategory::Color,
|
||||
include_str!("shaders/effect_brightness_contrast.wgsl"),
|
||||
format!("{}\n{}", crate::gpu::COLOR_WGSL, include_str!("shaders/effect_brightness_contrast.wgsl")),
|
||||
vec![
|
||||
EffectParameterDef::float_range("brightness", "Brightness", 0.0, -1.0, 1.0),
|
||||
EffectParameterDef::float_range("contrast", "Contrast", 1.0, 0.0, 3.0),
|
||||
@@ -102,7 +102,7 @@ impl EffectRegistry {
|
||||
HUE_SATURATION_ID,
|
||||
"Hue/Saturation",
|
||||
EffectCategory::Color,
|
||||
include_str!("shaders/effect_hue_saturation.wgsl"),
|
||||
format!("{}\n{}", crate::gpu::COLOR_WGSL, include_str!("shaders/effect_hue_saturation.wgsl")),
|
||||
vec![
|
||||
EffectParameterDef::angle("hue", "Hue Shift", 0.0),
|
||||
EffectParameterDef::float_range("saturation", "Saturation", 1.0, 0.0, 3.0),
|
||||
|
||||
@@ -6,6 +6,41 @@
|
||||
|
||||
use super::HDR_FORMAT;
|
||||
|
||||
/// Shared WGSL sRGB transfer functions — the single source of the sRGB OETF/EOTF
|
||||
/// used by every gamma-aware shader. Prepend it to a shader's source (it defines
|
||||
/// the functions before the body, so call order doesn't matter):
|
||||
/// `srgb_to_linear_channel` / `linear_to_srgb_channel` (scalar) and
|
||||
/// `srgb_to_linear` / `linear_to_srgb` (vec3). `linear_to_srgb_channel` clamps to
|
||||
/// [0,1] (its outputs target 8-bit / SDR display surfaces).
|
||||
pub const COLOR_WGSL: &str = r#"
|
||||
fn srgb_to_linear_channel(c: f32) -> f32 {
|
||||
return select(pow((c + 0.055) / 1.055, 2.4), c / 12.92, c <= 0.04045);
|
||||
}
|
||||
fn linear_to_srgb_channel(c: f32) -> f32 {
|
||||
let x = clamp(c, 0.0, 1.0);
|
||||
return select(1.055 * pow(x, 1.0 / 2.4) - 0.055, x * 12.92, x <= 0.0031308);
|
||||
}
|
||||
fn srgb_to_linear(c: vec3<f32>) -> vec3<f32> {
|
||||
return vec3<f32>(srgb_to_linear_channel(c.r), srgb_to_linear_channel(c.g), srgb_to_linear_channel(c.b));
|
||||
}
|
||||
fn linear_to_srgb(c: vec3<f32>) -> vec3<f32> {
|
||||
return vec3<f32>(linear_to_srgb_channel(c.r), linear_to_srgb_channel(c.g), linear_to_srgb_channel(c.b));
|
||||
}
|
||||
"#;
|
||||
|
||||
/// sRGB → linear for one channel in `[0, 1]` (CPU twin of the WGSL
|
||||
/// `srgb_to_linear_channel`). The single source of the EOTF for CPU code.
|
||||
pub fn srgb_to_linear(c: f32) -> f32 {
|
||||
if c <= 0.04045 { c / 12.92 } else { ((c + 0.055) / 1.055).powf(2.4) }
|
||||
}
|
||||
|
||||
/// linear → sRGB for one channel, clamped to `[0, 1]` (CPU twin of the WGSL
|
||||
/// `linear_to_srgb_channel`). The single source of the OETF for CPU code.
|
||||
pub fn linear_to_srgb(c: f32) -> f32 {
|
||||
let c = c.clamp(0.0, 1.0);
|
||||
if c <= 0.0031308 { c * 12.92 } else { 1.055 * c.powf(1.0 / 2.4) - 0.055 }
|
||||
}
|
||||
|
||||
/// GPU pipeline for sRGB to linear color space conversion
|
||||
///
|
||||
/// Converts Rgba8Srgb textures to Rgba16Float linear textures.
|
||||
|
||||
@@ -14,7 +14,7 @@ pub mod yuv_converter;
|
||||
|
||||
// Re-export commonly used types
|
||||
pub use buffer_pool::{BufferHandle, BufferPool, BufferSpec, BufferFormat};
|
||||
pub use color_convert::SrgbToLinearConverter;
|
||||
pub use color_convert::{SrgbToLinearConverter, COLOR_WGSL, srgb_to_linear, linear_to_srgb};
|
||||
pub use compositor::{Compositor, CompositorLayer, BlendMode};
|
||||
pub use effect_processor::{EffectProcessor, EffectUniforms};
|
||||
pub use yuv_converter::YuvConverter;
|
||||
|
||||
@@ -31,14 +31,23 @@ fn vs_main(@builtin(vertex_index) vertex_index: u32) -> VertexOutput {
|
||||
return out;
|
||||
}
|
||||
|
||||
// The HDR pipeline feeds these shaders LINEAR light, but brightness/contrast
|
||||
// (additive brightness, contrast pivoting around 0.5 perceptual mid-gray) are
|
||||
// defined in gamma/display space. Convert to sRGB, adjust there, then convert
|
||||
// back to linear so the controls behave like standard editors.
|
||||
// sRGB helpers (linear_to_srgb / srgb_to_linear) come from the prepended
|
||||
// COLOR_WGSL prelude.
|
||||
|
||||
@fragment
|
||||
fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
|
||||
let src = textureSample(source_tex, source_sampler, in.uv);
|
||||
let brightness = uniforms.params0.x; // -1 to 1
|
||||
let contrast = uniforms.params0.y; // 0 to 3
|
||||
|
||||
let src_srgb = linear_to_srgb(src.rgb);
|
||||
|
||||
// Apply brightness (additive)
|
||||
var color = src.rgb + vec3<f32>(brightness);
|
||||
var color = src_srgb + vec3<f32>(brightness);
|
||||
|
||||
// Apply contrast (multiply around midpoint 0.5)
|
||||
color = (color - vec3<f32>(0.5)) * contrast + vec3<f32>(0.5);
|
||||
@@ -46,6 +55,6 @@ fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
|
||||
// Clamp to valid range
|
||||
color = clamp(color, vec3<f32>(0.0), vec3<f32>(1.0));
|
||||
|
||||
let result = mix(src.rgb, color, uniforms.mix);
|
||||
return vec4<f32>(result, src.a);
|
||||
let result_srgb = mix(src_srgb, color, uniforms.mix);
|
||||
return vec4<f32>(srgb_to_linear(result_srgb), src.a);
|
||||
}
|
||||
|
||||
@@ -84,6 +84,12 @@ fn hsl_to_rgb(hsl: vec3<f32>) -> vec3<f32> {
|
||||
);
|
||||
}
|
||||
|
||||
// The HDR pipeline feeds this shader LINEAR light, but the HSL model (and the
|
||||
// lightness/saturation axes users expect) is defined on gamma-encoded sRGB.
|
||||
// Convert to sRGB, run the HSL adjustment there, then convert back to linear.
|
||||
// sRGB helpers (linear_to_srgb / srgb_to_linear) come from the prepended
|
||||
// COLOR_WGSL prelude.
|
||||
|
||||
@fragment
|
||||
fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
|
||||
let src = textureSample(source_tex, source_sampler, in.uv);
|
||||
@@ -91,8 +97,10 @@ fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
|
||||
let saturation = uniforms.params0.y; // Multiplier (1.0 = no change)
|
||||
let lightness = uniforms.params0.z; // Additive (-1 to 1)
|
||||
|
||||
let src_srgb = linear_to_srgb(src.rgb);
|
||||
|
||||
// Convert to HSL
|
||||
var hsl = rgb_to_hsl(src.rgb);
|
||||
var hsl = rgb_to_hsl(src_srgb);
|
||||
|
||||
// Apply adjustments
|
||||
hsl.x = fract(hsl.x + hue_shift); // Shift hue (wrapping)
|
||||
@@ -102,6 +110,6 @@ fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
|
||||
// Convert back to RGB
|
||||
let adjusted = hsl_to_rgb(hsl);
|
||||
|
||||
let result = mix(src.rgb, adjusted, uniforms.mix);
|
||||
return vec4<f32>(result, src.a);
|
||||
let result_srgb = mix(src_srgb, adjusted, uniforms.mix);
|
||||
return vec4<f32>(srgb_to_linear(result_srgb), src.a);
|
||||
}
|
||||
|
||||
@@ -33,13 +33,19 @@ fn vs_main(@builtin(vertex_index) vertex_index: u32) -> VertexOutput {
|
||||
return out;
|
||||
}
|
||||
|
||||
// The HDR pipeline feeds these shaders LINEAR light, but "invert" is a
|
||||
// perceptual operation defined in gamma/display space (Photoshop, GIMP, etc.).
|
||||
// Convert to sRGB, invert there, then convert back to linear. The sRGB helpers
|
||||
// (linear_to_srgb / srgb_to_linear) come from the prepended COLOR_WGSL prelude.
|
||||
|
||||
@fragment
|
||||
fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
|
||||
let src = textureSample(source_tex, source_sampler, in.uv);
|
||||
let amount = uniforms.params0.x; // params[0]
|
||||
|
||||
let inverted = vec3<f32>(1.0) - src.rgb;
|
||||
let result = mix(src.rgb, inverted, amount * uniforms.mix);
|
||||
let src_srgb = linear_to_srgb(src.rgb);
|
||||
let inverted = vec3<f32>(1.0) - src_srgb;
|
||||
let result_srgb = mix(src_srgb, inverted, amount * uniforms.mix);
|
||||
|
||||
return vec4<f32>(result, src.a);
|
||||
return vec4<f32>(srgb_to_linear(result_srgb), src.a);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user