| author | |
| committer | |
| log | f4024c0b020e151157095e4d47ec0f031bbced3b |
| tree | 9a2a5a48e1025bd8b6b34cc4d12f014c97187371 |
| parent | 518f2c643c33dd2bc47ae03dc746b478d8e40892 |
| signature | Signed by SSH key SHA256:52mNGHRsVFBDED9IAX5pe+LRWUefqTbxEReunq21QvU |
9 files changed, 2685 insertions(+), 2671 deletions(-)
crates/engine/src/eval/mod.rs+258-190| ... | ... | @@ -5,6 +5,7 @@ use crate::cache::{CacheMode, CachedTexture, EvalStats, NodeCache}; |
| 5 | 5 | use crate::media::{MediaCache, TextPlusParams, rasterize_polyline_mask, rasterize_text_plus}; |
| 6 | 6 | use bytemuck::{Pod, Zeroable}; |
| 7 | 7 | use model::{BitDepth, Comp, ExprEngine, Node, NodeId, ParamValue, default_main_input_port}; |
| 8 | use std::cell::{OnceCell, RefCell}; | |
| 8 | 9 | use std::collections::{HashMap, HashSet}; |
| 9 | 10 | use std::collections::hash_map::DefaultHasher; |
| 10 | 11 | use std::hash::{Hash, Hasher}; |
| ... | ... | @@ -58,7 +59,41 @@ impl Default for DisplaySettings { |
| 58 | 59 | } |
| 59 | 60 | } |
| 60 | 61 | |
| 61 | #[derive(Clone, Copy, Debug)] | |
| 62 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] | |
| 63 | enum ShaderDomain { | |
| 64 | Composite, | |
| 65 | Color, | |
| 66 | BlurGlow, | |
| 67 | Transform, | |
| 68 | Masks, | |
| 69 | KeyersGen, | |
| 70 | } | |
| 71 | ||
| 72 | impl ShaderDomain { | |
| 73 | fn source(self) -> &'static str { | |
| 74 | match self { | |
| 75 | Self::Composite => include_str!("../shaders/composite.wgsl"), | |
| 76 | Self::Color => include_str!("../shaders/color.wgsl"), | |
| 77 | Self::BlurGlow => include_str!("../shaders/blur_glow.wgsl"), | |
| 78 | Self::Transform => include_str!("../shaders/transform.wgsl"), | |
| 79 | Self::Masks => include_str!("../shaders/masks.wgsl"), | |
| 80 | Self::KeyersGen => include_str!("../shaders/keyers_gen.wgsl"), | |
| 81 | } | |
| 82 | } | |
| 83 | ||
| 84 | fn label(self) -> &'static str { | |
| 85 | match self { | |
| 86 | Self::Composite => "compositor-shaders-composite", | |
| 87 | Self::Color => "compositor-shaders-color", | |
| 88 | Self::BlurGlow => "compositor-shaders-blur-glow", | |
| 89 | Self::Transform => "compositor-shaders-transform", | |
| 90 | Self::Masks => "compositor-shaders-masks", | |
| 91 | Self::KeyersGen => "compositor-shaders-keyers-gen", | |
| 92 | } | |
| 93 | } | |
| 94 | } | |
| 95 | ||
| 96 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] | |
| 62 | 97 | enum ShaderOp { |
| 63 | 98 | Bg, |
| 64 | 99 | Merge, |
| ... | ... | @@ -105,76 +140,123 @@ enum ShaderOp { |
| 105 | 140 | KScope, |
| 106 | 141 | } |
| 107 | 142 | |
| 108 | struct Pipelines { | |
| 143 | impl ShaderOp { | |
| 144 | fn domain(self) -> ShaderDomain { | |
| 145 | match self { | |
| 146 | Self::Bg | |
| 147 | | Self::Merge | |
| 148 | | Self::DisplayBlit | |
| 149 | | Self::ChannelBoolean | |
| 150 | | Self::Dissolve | |
| 151 | | Self::ShadowComp | |
| 152 | | Self::SetDomain | |
| 153 | | Self::ChangeDepth => ShaderDomain::Composite, | |
| 154 | ||
| 155 | Self::Bc | |
| 156 | | Self::Cc | |
| 157 | | Self::ColorGain | |
| 158 | | Self::Colorize | |
| 159 | | Self::ColorCurves | |
| 160 | | Self::GamutConvert | |
| 161 | | Self::Invert | |
| 162 | | Self::HueCurves => ShaderDomain::Color, | |
| 163 | ||
| 164 | Self::Blur | |
| 165 | | Self::DirBlur | |
| 166 | | Self::VariBlur | |
| 167 | | Self::GlowAdd | |
| 168 | | Self::XGlowPrep | |
| 169 | | Self::XGlowAccum | |
| 170 | | Self::Filter | |
| 171 | | Self::FilmGrain => ShaderDomain::BlurGlow, | |
| 172 | ||
| 173 | Self::Transform | |
| 174 | | Self::Displace | |
| 175 | | Self::XfChroma | |
| 176 | | Self::Crop | |
| 177 | | Self::Homography | |
| 178 | | Self::Vortex | |
| 179 | | Self::KScope => ShaderDomain::Transform, | |
| 180 | ||
| 181 | Self::MaskShape | |
| 182 | | Self::MaskCombine | |
| 183 | | Self::MaskApply | |
| 184 | | Self::BitmapMask => ShaderDomain::Masks, | |
| 185 | ||
| 186 | Self::LumaKey | |
| 187 | | Self::FastNoise | |
| 188 | | Self::OutlineTint | |
| 189 | | Self::OutlineComposite | |
| 190 | | Self::TextOutline | |
| 191 | | Self::CustomWave | |
| 192 | | Self::UltraKeyer | |
| 193 | | Self::Plasma => ShaderDomain::KeyersGen, | |
| 194 | } | |
| 195 | } | |
| 196 | ||
| 197 | fn entry_point(self) -> &'static str { | |
| 198 | match self { | |
| 199 | Self::Bg => "bg_op", | |
| 200 | Self::Merge => "merge_op", | |
| 201 | Self::Transform => "transform_op", | |
| 202 | Self::Bc => "bc_op", | |
| 203 | Self::Cc => "cc_op", | |
| 204 | Self::ColorGain => "color_gain_op", | |
| 205 | Self::LumaKey => "luma_key_op", | |
| 206 | Self::Blur => "blur_op", | |
| 207 | Self::DirBlur => "dir_blur_op", | |
| 208 | Self::FastNoise => "fast_noise_op", | |
| 209 | Self::Displace => "displace_op", | |
| 210 | Self::MaskShape => "mask_shape_op", | |
| 211 | Self::MaskCombine => "mask_combine_op", | |
| 212 | Self::MaskApply => "mask_apply_op", | |
| 213 | Self::BitmapMask => "bitmap_mask_op", | |
| 214 | Self::Colorize => "colorize_op", | |
| 215 | Self::ColorCurves => "color_curves_op", | |
| 216 | Self::XfChroma => "xf_chroma_op", | |
| 217 | Self::GamutConvert => "gamut_convert_op", | |
| 218 | Self::VariBlur => "vari_blur_op", | |
| 219 | Self::OutlineTint => "outline_tint", | |
| 220 | Self::OutlineComposite => "outline_composite", | |
| 221 | Self::TextOutline => "text_outline_op", | |
| 222 | Self::CustomWave => "custom_wave_op", | |
| 223 | Self::Crop => "crop_op", | |
| 224 | Self::GlowAdd => "glow_add_op", | |
| 225 | Self::XGlowPrep => "xglow_prep_op", | |
| 226 | Self::XGlowAccum => "xglow_accum_op", | |
| 227 | Self::DisplayBlit => "display_blit_op", | |
| 228 | Self::Invert => "invert_op", | |
| 229 | Self::ChannelBoolean => "channel_boolean_op", | |
| 230 | Self::Filter => "filter_op", | |
| 231 | Self::HueCurves => "hue_curves_op", | |
| 232 | Self::UltraKeyer => "ultra_keyer_op", | |
| 233 | Self::Dissolve => "dissolve_op", | |
| 234 | Self::Homography => "homography_op", | |
| 235 | Self::ShadowComp => "shadow_comp_op", | |
| 236 | Self::Plasma => "plasma_op", | |
| 237 | Self::FilmGrain => "film_grain_op", | |
| 238 | Self::SetDomain => "set_domain_op", | |
| 239 | Self::ChangeDepth => "change_depth_op", | |
| 240 | Self::Vortex => "vortex_op", | |
| 241 | Self::KScope => "kscope_op", | |
| 242 | } | |
| 243 | } | |
| 244 | } | |
| 245 | ||
| 246 | const COMMON_WGSL: &str = include_str!("../shaders/common.wgsl"); | |
| 247 | ||
| 248 | struct ShaderRegistry { | |
| 249 | format: wgpu::TextureFormat, | |
| 250 | hdr_float: bool, | |
| 109 | 251 | bgl: wgpu::BindGroupLayout, |
| 252 | pl: wgpu::PipelineLayout, | |
| 110 | 253 | sampler: wgpu::Sampler, |
| 111 | bg: wgpu::ComputePipeline, | |
| 112 | merge: wgpu::ComputePipeline, | |
| 113 | transform: wgpu::ComputePipeline, | |
| 114 | bc: wgpu::ComputePipeline, | |
| 115 | cc: wgpu::ComputePipeline, | |
| 116 | color_gain: wgpu::ComputePipeline, | |
| 117 | luma_key: wgpu::ComputePipeline, | |
| 118 | blur: wgpu::ComputePipeline, | |
| 119 | dir_blur: wgpu::ComputePipeline, | |
| 120 | fast_noise: wgpu::ComputePipeline, | |
| 121 | displace: wgpu::ComputePipeline, | |
| 122 | mask_shape: wgpu::ComputePipeline, | |
| 123 | mask_combine: wgpu::ComputePipeline, | |
| 124 | mask_apply: wgpu::ComputePipeline, | |
| 125 | bitmap_mask: wgpu::ComputePipeline, | |
| 126 | colorize: wgpu::ComputePipeline, | |
| 127 | color_curves: wgpu::ComputePipeline, | |
| 128 | xf_chroma: wgpu::ComputePipeline, | |
| 129 | gamut_convert: wgpu::ComputePipeline, | |
| 130 | vari_blur: wgpu::ComputePipeline, | |
| 131 | outline_tint: wgpu::ComputePipeline, | |
| 132 | outline_composite: wgpu::ComputePipeline, | |
| 133 | text_outline: wgpu::ComputePipeline, | |
| 134 | custom_wave: wgpu::ComputePipeline, | |
| 135 | crop: wgpu::ComputePipeline, | |
| 136 | glow_add: wgpu::ComputePipeline, | |
| 137 | xglow_prep: wgpu::ComputePipeline, | |
| 138 | xglow_accum: wgpu::ComputePipeline, | |
| 139 | display_blit: wgpu::ComputePipeline, | |
| 140 | invert: wgpu::ComputePipeline, | |
| 141 | channel_boolean: wgpu::ComputePipeline, | |
| 142 | filter: wgpu::ComputePipeline, | |
| 143 | hue_curves: wgpu::ComputePipeline, | |
| 144 | ultra_keyer: wgpu::ComputePipeline, | |
| 145 | dissolve: wgpu::ComputePipeline, | |
| 146 | homography: wgpu::ComputePipeline, | |
| 147 | shadow_comp: wgpu::ComputePipeline, | |
| 148 | plasma: wgpu::ComputePipeline, | |
| 149 | film_grain: wgpu::ComputePipeline, | |
| 150 | set_domain: wgpu::ComputePipeline, | |
| 151 | change_depth: wgpu::ComputePipeline, | |
| 152 | vortex: wgpu::ComputePipeline, | |
| 153 | kscope: wgpu::ComputePipeline, | |
| 254 | modules: HashMap<ShaderDomain, wgpu::ShaderModule>, | |
| 255 | pipelines: HashMap<ShaderOp, wgpu::ComputePipeline>, | |
| 154 | 256 | } |
| 155 | 257 | |
| 156 | impl Pipelines { | |
| 258 | impl ShaderRegistry { | |
| 157 | 259 | fn new(device: &wgpu::Device, format: wgpu::TextureFormat, hdr_float: bool) -> Self { |
| 158 | let storage_fmt = match format { | |
| 159 | wgpu::TextureFormat::Rgba16Float => "rgba16float", | |
| 160 | wgpu::TextureFormat::Rgba32Float => "rgba32float", | |
| 161 | _ => "rgba8unorm", | |
| 162 | }; | |
| 163 | let mut src = include_str!("../shaders.wgsl").to_owned(); | |
| 164 | if storage_fmt != "rgba8unorm" { | |
| 165 | src = src.replace( | |
| 166 | "texture_storage_2d<rgba8unorm, write>", | |
| 167 | &format!("texture_storage_2d<{storage_fmt}, write>"), | |
| 168 | ); | |
| 169 | } | |
| 170 | if hdr_float { | |
| 171 | src = src.replace("const HDR_FLOAT: bool = false;", "const HDR_FLOAT: bool = true;"); | |
| 172 | } | |
| 173 | let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor { | |
| 174 | label: Some("compositor-shaders"), | |
| 175 | source: wgpu::ShaderSource::Wgsl(src.into()), | |
| 176 | }); | |
| 177 | ||
| 178 | 260 | let tex_entry = |binding| wgpu::BindGroupLayoutEntry { |
| 179 | 261 | binding, |
| 180 | 262 | visibility: wgpu::ShaderStages::COMPUTE, |
| ... | ... | @@ -220,23 +302,12 @@ impl Pipelines { |
| 220 | 302 | ], |
| 221 | 303 | }); |
| 222 | 304 | |
| 223 | let layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { | |
| 305 | let pl = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { | |
| 224 | 306 | label: Some("compositor-pl"), |
| 225 | 307 | bind_group_layouts: &[Some(&bgl)], |
| 226 | 308 | immediate_size: 0, |
| 227 | 309 | }); |
| 228 | 310 | |
| 229 | let make = |entry: &str| { | |
| 230 | device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor { | |
| 231 | label: Some(entry), | |
| 232 | layout: Some(&layout), | |
| 233 | module: &shader, | |
| 234 | entry_point: Some(entry), | |
| 235 | compilation_options: Default::default(), | |
| 236 | cache: None, | |
| 237 | }) | |
| 238 | }; | |
| 239 | ||
| 240 | 311 | let sampler = device.create_sampler(&wgpu::SamplerDescriptor { |
| 241 | 312 | label: Some("compositor-linear"), |
| 242 | 313 | address_mode_u: wgpu::AddressMode::ClampToEdge, |
| ... | ... | @@ -248,110 +319,69 @@ impl Pipelines { |
| 248 | 319 | }); |
| 249 | 320 | |
| 250 | 321 | Self { |
| 322 | format, | |
| 323 | hdr_float, | |
| 251 | 324 | bgl, |
| 325 | pl, | |
| 252 | 326 | sampler, |
| 253 | bg: make("bg_op"), | |
| 254 | merge: make("merge_op"), | |
| 255 | transform: make("transform_op"), | |
| 256 | bc: make("bc_op"), | |
| 257 | cc: make("cc_op"), | |
| 258 | color_gain: make("color_gain_op"), | |
| 259 | luma_key: make("luma_key_op"), | |
| 260 | blur: make("blur_op"), | |
| 261 | dir_blur: make("dir_blur_op"), | |
| 262 | fast_noise: make("fast_noise_op"), | |
| 263 | displace: make("displace_op"), | |
| 264 | mask_shape: make("mask_shape_op"), | |
| 265 | mask_combine: make("mask_combine_op"), | |
| 266 | mask_apply: make("mask_apply_op"), | |
| 267 | bitmap_mask: make("bitmap_mask_op"), | |
| 268 | colorize: make("colorize_op"), | |
| 269 | color_curves: make("color_curves_op"), | |
| 270 | xf_chroma: make("xf_chroma_op"), | |
| 271 | gamut_convert: make("gamut_convert_op"), | |
| 272 | vari_blur: make("vari_blur_op"), | |
| 273 | outline_tint: make("outline_tint"), | |
| 274 | outline_composite: make("outline_composite"), | |
| 275 | text_outline: make("text_outline_op"), | |
| 276 | custom_wave: make("custom_wave_op"), | |
| 277 | crop: make("crop_op"), | |
| 278 | glow_add: make("glow_add_op"), | |
| 279 | xglow_prep: make("xglow_prep_op"), | |
| 280 | xglow_accum: make("xglow_accum_op"), | |
| 281 | display_blit: make("display_blit_op"), | |
| 282 | invert: make("invert_op"), | |
| 283 | channel_boolean: make("channel_boolean_op"), | |
| 284 | filter: make("filter_op"), | |
| 285 | hue_curves: make("hue_curves_op"), | |
| 286 | ultra_keyer: make("ultra_keyer_op"), | |
| 287 | dissolve: make("dissolve_op"), | |
| 288 | homography: make("homography_op"), | |
| 289 | shadow_comp: make("shadow_comp_op"), | |
| 290 | plasma: make("plasma_op"), | |
| 291 | film_grain: make("film_grain_op"), | |
| 292 | set_domain: make("set_domain_op"), | |
| 293 | change_depth: make("change_depth_op"), | |
| 294 | vortex: make("vortex_op"), | |
| 295 | kscope: make("kscope_op"), | |
| 327 | modules: HashMap::new(), | |
| 328 | pipelines: HashMap::new(), | |
| 296 | 329 | } |
| 297 | 330 | } |
| 298 | 331 | |
| 299 | fn get(&self, op: ShaderOp) -> &wgpu::ComputePipeline { | |
| 300 | match op { | |
| 301 | ShaderOp::Bg => &self.bg, | |
| 302 | ShaderOp::Merge => &self.merge, | |
| 303 | ShaderOp::Transform => &self.transform, | |
| 304 | ShaderOp::Bc => &self.bc, | |
| 305 | ShaderOp::Cc => &self.cc, | |
| 306 | ShaderOp::ColorGain => &self.color_gain, | |
| 307 | ShaderOp::LumaKey => &self.luma_key, | |
| 308 | ShaderOp::Blur => &self.blur, | |
| 309 | ShaderOp::DirBlur => &self.dir_blur, | |
| 310 | ShaderOp::FastNoise => &self.fast_noise, | |
| 311 | ShaderOp::Displace => &self.displace, | |
| 312 | ShaderOp::MaskShape => &self.mask_shape, | |
| 313 | ShaderOp::MaskCombine => &self.mask_combine, | |
| 314 | ShaderOp::MaskApply => &self.mask_apply, | |
| 315 | ShaderOp::BitmapMask => &self.bitmap_mask, | |
| 316 | ShaderOp::Colorize => &self.colorize, | |
| 317 | ShaderOp::ColorCurves => &self.color_curves, | |
| 318 | ShaderOp::XfChroma => &self.xf_chroma, | |
| 319 | ShaderOp::GamutConvert => &self.gamut_convert, | |
| 320 | ShaderOp::VariBlur => &self.vari_blur, | |
| 321 | ShaderOp::OutlineTint => &self.outline_tint, | |
| 322 | ShaderOp::OutlineComposite => &self.outline_composite, | |
| 323 | ShaderOp::TextOutline => &self.text_outline, | |
| 324 | ShaderOp::CustomWave => &self.custom_wave, | |
| 325 | ShaderOp::Crop => &self.crop, | |
| 326 | ShaderOp::GlowAdd => &self.glow_add, | |
| 327 | ShaderOp::XGlowPrep => &self.xglow_prep, | |
| 328 | ShaderOp::XGlowAccum => &self.xglow_accum, | |
| 329 | ShaderOp::DisplayBlit => &self.display_blit, | |
| 330 | ShaderOp::Invert => &self.invert, | |
| 331 | ShaderOp::ChannelBoolean => &self.channel_boolean, | |
| 332 | ShaderOp::Filter => &self.filter, | |
| 333 | ShaderOp::HueCurves => &self.hue_curves, | |
| 334 | ShaderOp::UltraKeyer => &self.ultra_keyer, | |
| 335 | ShaderOp::Dissolve => &self.dissolve, | |
| 336 | ShaderOp::Homography => &self.homography, | |
| 337 | ShaderOp::ShadowComp => &self.shadow_comp, | |
| 338 | ShaderOp::Plasma => &self.plasma, | |
| 339 | ShaderOp::FilmGrain => &self.film_grain, | |
| 340 | ShaderOp::SetDomain => &self.set_domain, | |
| 341 | ShaderOp::ChangeDepth => &self.change_depth, | |
| 342 | ShaderOp::Vortex => &self.vortex, | |
| 343 | ShaderOp::KScope => &self.kscope, | |
| 332 | fn ensure_pipeline(&mut self, device: &wgpu::Device, op: ShaderOp) { | |
| 333 | if self.pipelines.contains_key(&op) { | |
| 334 | return; | |
| 344 | 335 | } |
| 336 | let domain = op.domain(); | |
| 337 | let format = self.format; | |
| 338 | let hdr_float = self.hdr_float; | |
| 339 | let module = self.modules.entry(domain).or_insert_with(|| { | |
| 340 | let domain_src = domain.source(); | |
| 341 | let mut src = String::with_capacity(COMMON_WGSL.len() + domain_src.len() + 1); | |
| 342 | src.push_str(COMMON_WGSL); | |
| 343 | src.push('\n'); | |
| 344 | src.push_str(domain_src); | |
| 345 | let storage_fmt = match format { | |
| 346 | wgpu::TextureFormat::Rgba16Float => "rgba16float", | |
| 347 | wgpu::TextureFormat::Rgba32Float => "rgba32float", | |
| 348 | _ => "rgba8unorm", | |
| 349 | }; | |
| 350 | if storage_fmt != "rgba8unorm" { | |
| 351 | src = src.replace( | |
| 352 | "texture_storage_2d<rgba8unorm, write>", | |
| 353 | &format!("texture_storage_2d<{storage_fmt}, write>"), | |
| 354 | ); | |
| 355 | } | |
| 356 | if hdr_float { | |
| 357 | src = src.replace("const HDR_FLOAT: bool = false;", "const HDR_FLOAT: bool = true;"); | |
| 358 | } | |
| 359 | device.create_shader_module(wgpu::ShaderModuleDescriptor { | |
| 360 | label: Some(domain.label()), | |
| 361 | source: wgpu::ShaderSource::Wgsl(src.into()), | |
| 362 | }) | |
| 363 | }); | |
| 364 | ||
| 365 | let entry = op.entry_point(); | |
| 366 | let pipeline = device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor { | |
| 367 | label: Some(entry), | |
| 368 | layout: Some(&self.pl), | |
| 369 | module, | |
| 370 | entry_point: Some(entry), | |
| 371 | compilation_options: Default::default(), | |
| 372 | cache: None, | |
| 373 | }); | |
| 374 | self.pipelines.insert(op, pipeline); | |
| 345 | 375 | } |
| 346 | 376 | } |
| 347 | 377 | |
| 348 | 378 | pub struct Engine { |
| 349 | 379 | device: wgpu::Device, |
| 350 | 380 | queue: wgpu::Queue, |
| 351 | pipelines: Pipelines, | |
| 352 | pipelines_int16: std::sync::OnceLock<Pipelines>, | |
| 353 | pipelines_f16: std::sync::OnceLock<Pipelines>, | |
| 354 | pipelines_f32: std::sync::OnceLock<Pipelines>, | |
| 381 | pipelines: RefCell<ShaderRegistry>, | |
| 382 | pipelines_int16: OnceCell<RefCell<ShaderRegistry>>, | |
| 383 | pipelines_f16: OnceCell<RefCell<ShaderRegistry>>, | |
| 384 | pipelines_f32: OnceCell<RefCell<ShaderRegistry>>, | |
| 355 | 385 | supports_f32_filter: bool, |
| 356 | 386 | scene3d: crate::scene3d::Scene3DRenderer, |
| 357 | 387 | optflow: std::sync::OnceLock<crate::optflow::OpticalFlowPipelines>, |
| ... | ... | @@ -381,7 +411,7 @@ pub struct Engine { |
| 381 | 411 | impl Engine { |
| 382 | 412 | pub fn new(device: wgpu::Device, queue: wgpu::Queue) -> Self { |
| 383 | 413 | let supports_f32_filter = device.features().contains(wgpu::Features::FLOAT32_FILTERABLE); |
| 384 | let pipelines = Pipelines::new(&device, wgpu::TextureFormat::Rgba8Unorm, false); | |
| 414 | let pipelines = RefCell::new(ShaderRegistry::new(&device, wgpu::TextureFormat::Rgba8Unorm, false)); | |
| 385 | 415 | let scene3d = crate::scene3d::Scene3DRenderer::new(&device); |
| 386 | 416 | let dummy = create_texture(&device, [1, 1], 0); |
| 387 | 417 | queue.write_texture( |
| ... | ... | @@ -398,9 +428,9 @@ impl Engine { |
| 398 | 428 | device, |
| 399 | 429 | queue, |
| 400 | 430 | pipelines, |
| 401 | pipelines_int16: std::sync::OnceLock::new(), | |
| 402 | pipelines_f16: std::sync::OnceLock::new(), | |
| 403 | pipelines_f32: std::sync::OnceLock::new(), | |
| 431 | pipelines_int16: OnceCell::new(), | |
| 432 | pipelines_f16: OnceCell::new(), | |
| 433 | pipelines_f32: OnceCell::new(), | |
| 404 | 434 | supports_f32_filter, |
| 405 | 435 | scene3d, |
| 406 | 436 | optflow: std::sync::OnceLock::new(), |
| ... | ... | @@ -507,23 +537,23 @@ impl Engine { |
| 507 | 537 | create_texture_dod_fmt(&self.device, size, origin, canvas_size, d, fmt, content_hash) |
| 508 | 538 | } |
| 509 | 539 | |
| 510 | fn pipelines_for(&self, depth: BitDepth) -> &Pipelines { | |
| 540 | fn pipelines_for(&self, depth: BitDepth) -> &RefCell<ShaderRegistry> { | |
| 511 | 541 | match depth { |
| 512 | 542 | BitDepth::Int8 => &self.pipelines, |
| 513 | 543 | BitDepth::Int16 => self.pipelines_int16.get_or_init(|| { |
| 514 | Pipelines::new(&self.device, wgpu::TextureFormat::Rgba16Float, false) | |
| 544 | RefCell::new(ShaderRegistry::new(&self.device, wgpu::TextureFormat::Rgba16Float, false)) | |
| 515 | 545 | }), |
| 516 | 546 | BitDepth::Float16 => self.pipelines_f16.get_or_init(|| { |
| 517 | Pipelines::new(&self.device, wgpu::TextureFormat::Rgba16Float, true) | |
| 547 | RefCell::new(ShaderRegistry::new(&self.device, wgpu::TextureFormat::Rgba16Float, true)) | |
| 518 | 548 | }), |
| 519 | 549 | BitDepth::Float32 => { |
| 520 | 550 | if self.supports_f32_filter { |
| 521 | 551 | self.pipelines_f32.get_or_init(|| { |
| 522 | Pipelines::new(&self.device, wgpu::TextureFormat::Rgba32Float, true) | |
| 552 | RefCell::new(ShaderRegistry::new(&self.device, wgpu::TextureFormat::Rgba32Float, true)) | |
| 523 | 553 | }) |
| 524 | 554 | } else { |
| 525 | 555 | self.pipelines_f16.get_or_init(|| { |
| 526 | Pipelines::new(&self.device, wgpu::TextureFormat::Rgba16Float, true) | |
| 556 | RefCell::new(ShaderRegistry::new(&self.device, wgpu::TextureFormat::Rgba16Float, true)) | |
| 527 | 557 | }) |
| 528 | 558 | } |
| 529 | 559 | } |
| ... | ... | @@ -597,7 +627,7 @@ impl Engine { |
| 597 | 627 | params.p[2] = [if display.smooth_resize { 0.0 } else { 1.0 }, 0.0, 0.0, 0.0]; |
| 598 | 628 | self.dispatch_to_view( |
| 599 | 629 | &mut encoder, |
| 600 | &self.pipelines.display_blit, | |
| 630 | ShaderOp::DisplayBlit, | |
| 601 | 631 | &src.view, |
| 602 | 632 | &self.dummy.view, |
| 603 | 633 | &self.dummy.view, |
| ... | ... | @@ -784,7 +814,7 @@ impl Engine { |
| 784 | 814 | } |
| 785 | 815 | self.dispatch_to_view( |
| 786 | 816 | &mut encoder, |
| 787 | &self.pipelines.display_blit, | |
| 817 | ShaderOp::DisplayBlit, | |
| 788 | 818 | &tex.view, |
| 789 | 819 | &self.dummy.view, |
| 790 | 820 | &self.dummy.view, |
| ... | ... | @@ -825,7 +855,7 @@ impl Engine { |
| 825 | 855 | params.p[0] = [1.0, 1.0, 0.0, 0.0]; |
| 826 | 856 | self.dispatch_to_view( |
| 827 | 857 | &mut enc, |
| 828 | &self.pipelines.display_blit, | |
| 858 | ShaderOp::DisplayBlit, | |
| 829 | 859 | &tex.view, |
| 830 | 860 | &self.dummy.view, |
| 831 | 861 | &self.dummy.view, |
| ... | ... | @@ -1160,7 +1190,9 @@ impl Engine { |
| 1160 | 1190 | dst: &CachedTexture, |
| 1161 | 1191 | params: &UniformParams, |
| 1162 | 1192 | ) { |
| 1163 | let pl = self.pipelines_for(dst.depth); | |
| 1193 | let reg_cell = self.pipelines_for(dst.depth); | |
| 1194 | reg_cell.borrow_mut().ensure_pipeline(&self.device, op); | |
| 1195 | let reg = reg_cell.borrow(); | |
| 1164 | 1196 | let ubuf = self.device.create_buffer(&wgpu::BufferDescriptor { |
| 1165 | 1197 | label: Some("op-params"), |
| 1166 | 1198 | size: std::mem::size_of::<UniformParams>() as u64, |
| ... | ... | @@ -1171,12 +1203,12 @@ impl Engine { |
| 1171 | 1203 | |
| 1172 | 1204 | let bg = self.device.create_bind_group(&wgpu::BindGroupDescriptor { |
| 1173 | 1205 | label: Some("op-bg"), |
| 1174 | layout: &pl.bgl, | |
| 1206 | layout: &reg.bgl, | |
| 1175 | 1207 | entries: &[ |
| 1176 | 1208 | wgpu::BindGroupEntry { binding: 0, resource: wgpu::BindingResource::TextureView(src_a) }, |
| 1177 | 1209 | wgpu::BindGroupEntry { binding: 1, resource: wgpu::BindingResource::TextureView(src_b) }, |
| 1178 | 1210 | wgpu::BindGroupEntry { binding: 2, resource: wgpu::BindingResource::TextureView(mask) }, |
| 1179 | wgpu::BindGroupEntry { binding: 3, resource: wgpu::BindingResource::Sampler(&pl.sampler) }, | |
| 1211 | wgpu::BindGroupEntry { binding: 3, resource: wgpu::BindingResource::Sampler(&reg.sampler) }, | |
| 1180 | 1212 | wgpu::BindGroupEntry { binding: 4, resource: wgpu::BindingResource::TextureView(&dst.view) }, |
| 1181 | 1213 | wgpu::BindGroupEntry { binding: 5, resource: ubuf.as_entire_binding() }, |
| 1182 | 1214 | ], |
| ... | ... | @@ -1186,7 +1218,7 @@ impl Engine { |
| 1186 | 1218 | label: Some("comp-op"), |
| 1187 | 1219 | timestamp_writes: None, |
| 1188 | 1220 | }); |
| 1189 | pass.set_pipeline(pl.get(op)); | |
| 1221 | pass.set_pipeline(&reg.pipelines[&op]); | |
| 1190 | 1222 | pass.set_bind_group(0, &bg, &[]); |
| 1191 | 1223 | pass.dispatch_workgroups(dst.size[0].div_ceil(8), dst.size[1].div_ceil(8), 1); |
| 1192 | 1224 | } |
| ... | ... | @@ -1195,7 +1227,7 @@ impl Engine { |
| 1195 | 1227 | fn dispatch_to_view( |
| 1196 | 1228 | &self, |
| 1197 | 1229 | enc: &mut wgpu::CommandEncoder, |
| 1198 | pipeline: &wgpu::ComputePipeline, | |
| 1230 | op: ShaderOp, | |
| 1199 | 1231 | src_a: &wgpu::TextureView, |
| 1200 | 1232 | src_b: &wgpu::TextureView, |
| 1201 | 1233 | mask: &wgpu::TextureView, |
| ... | ... | @@ -1203,6 +1235,8 @@ impl Engine { |
| 1203 | 1235 | dst_size: [u32; 2], |
| 1204 | 1236 | params: &UniformParams, |
| 1205 | 1237 | ) { |
| 1238 | self.pipelines.borrow_mut().ensure_pipeline(&self.device, op); | |
| 1239 | let reg = self.pipelines.borrow(); | |
| 1206 | 1240 | let ubuf = self.device.create_buffer(&wgpu::BufferDescriptor { |
| 1207 | 1241 | label: Some("op-params"), |
| 1208 | 1242 | size: std::mem::size_of::<UniformParams>() as u64, |
| ... | ... | @@ -1213,12 +1247,12 @@ impl Engine { |
| 1213 | 1247 | |
| 1214 | 1248 | let bg = self.device.create_bind_group(&wgpu::BindGroupDescriptor { |
| 1215 | 1249 | label: Some("op-bg"), |
| 1216 | layout: &self.pipelines.bgl, | |
| 1250 | layout: &reg.bgl, | |
| 1217 | 1251 | entries: &[ |
| 1218 | 1252 | wgpu::BindGroupEntry { binding: 0, resource: wgpu::BindingResource::TextureView(src_a) }, |
| 1219 | 1253 | wgpu::BindGroupEntry { binding: 1, resource: wgpu::BindingResource::TextureView(src_b) }, |
| 1220 | 1254 | wgpu::BindGroupEntry { binding: 2, resource: wgpu::BindingResource::TextureView(mask) }, |
| 1221 | wgpu::BindGroupEntry { binding: 3, resource: wgpu::BindingResource::Sampler(&self.pipelines.sampler) }, | |
| 1255 | wgpu::BindGroupEntry { binding: 3, resource: wgpu::BindingResource::Sampler(&reg.sampler) }, | |
| 1222 | 1256 | wgpu::BindGroupEntry { binding: 4, resource: wgpu::BindingResource::TextureView(dst_view) }, |
| 1223 | 1257 | wgpu::BindGroupEntry { binding: 5, resource: ubuf.as_entire_binding() }, |
| 1224 | 1258 | ], |
| ... | ... | @@ -1228,7 +1262,7 @@ impl Engine { |
| 1228 | 1262 | label: Some("comp-op"), |
| 1229 | 1263 | timestamp_writes: None, |
| 1230 | 1264 | }); |
| 1231 | pass.set_pipeline(pipeline); | |
| 1265 | pass.set_pipeline(&reg.pipelines[&op]); | |
| 1232 | 1266 | pass.set_bind_group(0, &bg, &[]); |
| 1233 | 1267 | pass.dispatch_workgroups(dst_size[0].div_ceil(8), dst_size[1].div_ceil(8), 1); |
| 1234 | 1268 | } |
| ... | ... | @@ -1294,3 +1328,37 @@ pub fn create_texture_dod_fmt( |
| 1294 | 1328 | content_hash, |
| 1295 | 1329 | ) |
| 1296 | 1330 | } |
| 1331 | ||
| 1332 | #[cfg(test)] | |
| 1333 | mod lazy_shader_verify { | |
| 1334 | use super::*; | |
| 1335 | ||
| 1336 | #[test] | |
| 1337 | fn verify_all_43_shader_ops_compile() { | |
| 1338 | let eng = Engine::new_headless().expect("gpu"); | |
| 1339 | let all_ops = [ | |
| 1340 | ShaderOp::Bg, ShaderOp::Merge, ShaderOp::Transform, ShaderOp::Bc, ShaderOp::Cc, | |
| 1341 | ShaderOp::ColorGain, ShaderOp::LumaKey, ShaderOp::Blur, ShaderOp::DirBlur, | |
| 1342 | ShaderOp::FastNoise, ShaderOp::Displace, ShaderOp::MaskShape, ShaderOp::MaskCombine, | |
| 1343 | ShaderOp::MaskApply, ShaderOp::BitmapMask, ShaderOp::Colorize, ShaderOp::ColorCurves, | |
| 1344 | ShaderOp::XfChroma, ShaderOp::GamutConvert, ShaderOp::VariBlur, ShaderOp::OutlineTint, | |
| 1345 | ShaderOp::OutlineComposite, ShaderOp::TextOutline, ShaderOp::CustomWave, ShaderOp::Crop, | |
| 1346 | ShaderOp::GlowAdd, ShaderOp::XGlowPrep, ShaderOp::XGlowAccum, ShaderOp::DisplayBlit, | |
| 1347 | ShaderOp::Invert, ShaderOp::ChannelBoolean, ShaderOp::Filter, ShaderOp::HueCurves, | |
| 1348 | ShaderOp::UltraKeyer, ShaderOp::Dissolve, ShaderOp::Homography, ShaderOp::ShadowComp, | |
| 1349 | ShaderOp::Plasma, ShaderOp::FilmGrain, ShaderOp::SetDomain, ShaderOp::ChangeDepth, | |
| 1350 | ShaderOp::Vortex, ShaderOp::KScope, | |
| 1351 | ]; | |
| 1352 | // Verify starts completely empty (lazy!) | |
| 1353 | assert_eq!(eng.pipelines.borrow().modules.len(), 0); | |
| 1354 | assert_eq!(eng.pipelines.borrow().pipelines.len(), 0); | |
| 1355 | for op in all_ops { | |
| 1356 | eng.pipelines.borrow_mut().ensure_pipeline(&eng.device, op); | |
| 1357 | eng.pipelines_for(BitDepth::Float16).borrow_mut().ensure_pipeline(&eng.device, op); | |
| 1358 | } | |
| 1359 | assert_eq!(eng.pipelines.borrow().modules.len(), 6); | |
| 1360 | assert_eq!(eng.pipelines.borrow().pipelines.len(), 43); | |
| 1361 | assert_eq!(eng.pipelines_for(BitDepth::Float16).borrow().modules.len(), 6); | |
| 1362 | assert_eq!(eng.pipelines_for(BitDepth::Float16).borrow().pipelines.len(), 43); | |
| 1363 | } | |
| 1364 | } |
crates/engine/src/shaders.wgsl deleted-2481| ... | ... | @@ -1,2481 +0,0 @@ |
| 1 | // Compositor GPU compute shaders (WGSL). | |
| 2 | // Coordinate convention: Fusion uses normalized UV (0,0) = bottom-left, (1,1) = top-right, | |
| 3 | // whereas textures store row 0 at the top. Helpers `pix_to_fu` / `fu_to_uv` bridge cleanly. | |
| 4 | ||
| 5 | @group(0) @binding(0) var src_a: texture_2d<f32>; | |
| 6 | @group(0) @binding(1) var src_b: texture_2d<f32>; | |
| 7 | @group(0) @binding(2) var mask_tex: texture_2d<f32>; | |
| 8 | @group(0) @binding(3) var samp_lin: sampler; | |
| 9 | @group(0) @binding(4) var dst_tex: texture_storage_2d<rgba8unorm, write>; | |
| 10 | ||
| 11 | // Replaced at pipeline build time for Float16/Float32 HDR pipelines (clamped for Int8/Int16). | |
| 12 | const HDR_FLOAT: bool = false; | |
| 13 | ||
| 14 | fn store_dst(p: vec2<i32>, col: vec4<f32>) { | |
| 15 | if (HDR_FLOAT) { | |
| 16 | textureStore(dst_tex, p, col); | |
| 17 | } else { | |
| 18 | textureStore(dst_tex, p, clamp(col, vec4<f32>(0.0), vec4<f32>(1.0))); | |
| 19 | } | |
| 20 | } | |
| 21 | ||
| 22 | struct Params { | |
| 23 | // 16 vec4<f32> = 256 bytes of uniform parameters per dispatch. | |
| 24 | p: array<vec4<f32>, 16>, | |
| 25 | }; | |
| 26 | @group(0) @binding(5) var<uniform> u: Params; | |
| 27 | ||
| 28 | // Helper: output pixel (gid.xy, top-down) -> Fusion UV (0..1, bottom-up). | |
| 29 | fn pix_to_fu(gid: vec2<u32>, dims: vec2<u32>) -> vec2<f32> { | |
| 30 | let f = vec2<f32>(dims); | |
| 31 | return vec2<f32>((f32(gid.x) + 0.5) / f.x, 1.0 - (f32(gid.y) + 0.5) / f.y); | |
| 32 | } | |
| 33 | ||
| 34 | // Helper: Fusion UV (bottom-up) -> texture UV (top-down). | |
| 35 | fn fu_to_uv(fu: vec2<f32>) -> vec2<f32> { | |
| 36 | return vec2<f32>(fu.x, 1.0 - fu.y); | |
| 37 | } | |
| 38 | ||
| 39 | fn sample_tex(tex: texture_2d<f32>, uv: vec2<f32>, nearest: bool) -> vec4<f32> { | |
| 40 | if (nearest) { | |
| 41 | let isdims = vec2<i32>(textureDimensions(tex)); | |
| 42 | let sx = clamp(i32(floor(uv.x * f32(isdims.x))), 0, isdims.x - 1); | |
| 43 | let sy = clamp(i32(floor(uv.y * f32(isdims.y))), 0, isdims.y - 1); | |
| 44 | return textureLoad(tex, vec2<i32>(sx, sy), 0); | |
| 45 | } | |
| 46 | return textureSampleLevel(tex, samp_lin, uv, 0.0); | |
| 47 | } | |
| 48 | ||
| 49 | // Helper: Sample texture with Fusion Edges mode: 0=Canvas, 1=Wrap, 2=Duplicate, 3=Mirror. | |
| 50 | // When `dod` (`origin_x, origin_y, canvas_w, canvas_h`) has `canvas_w > 0.5`, maps `fu` | |
| 51 | // (normalized to `canvas`) into the texture's expanded Domain of Definition! | |
| 52 | fn sample_dod_edges_mode(tex: texture_2d<f32>, fu: vec2<f32>, edges: i32, dod: vec4<f32>, nearest: bool) -> vec4<f32> { | |
| 53 | var uv: vec2<f32>; | |
| 54 | if (dod.z > 0.5 && dod.w > 0.5) { | |
| 55 | let tdims = vec2<f32>(textureDimensions(tex)); | |
| 56 | let canvas_px = vec2<f32>(fu.x * dod.z, (1.0 - fu.y) * dod.w); | |
| 57 | uv = (canvas_px - dod.xy) / tdims; | |
| 58 | } else { | |
| 59 | uv = fu_to_uv(fu); | |
| 60 | } | |
| 61 | if (edges == 0) { | |
| 62 | if (uv.x < 0.0 || uv.x > 1.0 || uv.y < 0.0 || uv.y > 1.0) { | |
| 63 | return vec4<f32>(0.0); | |
| 64 | } | |
| 65 | return sample_tex(tex, uv, nearest); | |
| 66 | } else if (edges == 1) { | |
| 67 | uv = fract(uv); | |
| 68 | return sample_tex(tex, uv, nearest); | |
| 69 | } else if (edges == 2) { | |
| 70 | uv = clamp(uv, vec2<f32>(0.0), vec2<f32>(1.0)); | |
| 71 | return sample_tex(tex, uv, nearest); | |
| 72 | } else { | |
| 73 | let m = abs(fract(uv * 0.5 + 0.5) * 2.0 - 1.0); | |
| 74 | return sample_tex(tex, m, nearest); | |
| 75 | } | |
| 76 | } | |
| 77 | ||
| 78 | fn sample_dod_edges(tex: texture_2d<f32>, fu: vec2<f32>, edges: i32, dod: vec4<f32>) -> vec4<f32> { | |
| 79 | return sample_dod_edges_mode(tex, fu, edges, dod, false); | |
| 80 | } | |
| 81 | ||
| 82 | fn sample_edges(tex: texture_2d<f32>, fu: vec2<f32>, edges: i32) -> vec4<f32> { | |
| 83 | return sample_dod_edges(tex, fu, edges, vec4<f32>(0.0)); | |
| 84 | } | |
| 85 | ||
| 86 | fn read_mask_uv(uv: vec2<f32>, has_mask: f32, invert: f32) -> f32 { | |
| 87 | if (has_mask < 0.5) { | |
| 88 | return 1.0; | |
| 89 | } | |
| 90 | var m = 0.0; | |
| 91 | if (uv.x >= 0.0 && uv.x <= 1.0 && uv.y >= 0.0 && uv.y <= 1.0) { | |
| 92 | m = textureSampleLevel(mask_tex, samp_lin, uv, 0.0).a; | |
| 93 | } | |
| 94 | return select(m, 1.0 - m, invert > 0.5); | |
| 95 | } | |
| 96 | ||
| 97 | fn read_mask(gid: vec2<u32>, dims: vec2<u32>, has_mask: f32, invert: f32) -> f32 { | |
| 98 | return read_mask_uv((vec2<f32>(gid) + 0.5) / vec2<f32>(dims), has_mask, invert); | |
| 99 | } | |
| 100 | ||
| 101 | // ============================================================================ | |
| 102 | // 1. Background (Solid + Gradients) | |
| 103 | // p[0] = top_left rgba, p[1] = top_right rgba, p[2] = bottom_left rgba, p[3] = bottom_right rgba | |
| 104 | // p[4] = (type: 0=Solid, 1=Horiz, 2=Vert, 3=FourCorner, 4=Gradient, 5=Radial, sub_type, start_x, start_y) | |
| 105 | // p[5] = (end_x, end_y, 0, 0) | |
| 106 | // ============================================================================ | |
| 107 | @compute @workgroup_size(8, 8) | |
| 108 | fn bg_op(@builtin(global_invocation_id) gid: vec3<u32>) { | |
| 109 | let dims = textureDimensions(dst_tex); | |
| 110 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | |
| 111 | let fu = pix_to_fu(gid.xy, dims); | |
| 112 | let kind = i32(u.p[4].x + 0.5); | |
| 113 | var col = u.p[0]; | |
| 114 | if (kind == 1) { | |
| 115 | col = mix(u.p[0], u.p[1], fu.x); | |
| 116 | } else if (kind == 2) { | |
| 117 | col = mix(u.p[2], u.p[0], fu.y); | |
| 118 | } else if (kind == 3) { | |
| 119 | let top = mix(u.p[0], u.p[1], fu.x); | |
| 120 | let bot = mix(u.p[2], u.p[3], fu.x); | |
| 121 | col = mix(bot, top, fu.y); | |
| 122 | } else if (kind == 4 || kind == 5) { | |
| 123 | let s = u.p[4].zw; | |
| 124 | let e = u.p[5].xy; | |
| 125 | let d = e - s; | |
| 126 | var t = 0.0; | |
| 127 | if (kind == 5) { | |
| 128 | let r = max(length(d), 1e-5); | |
| 129 | t = clamp(length(fu - s) / r, 0.0, 1.0); | |
| 130 | } else { | |
| 131 | let len2 = max(dot(d, d), 1e-6); | |
| 132 | t = clamp(dot(fu - s, d) / len2, 0.0, 1.0); | |
| 133 | } | |
| 134 | col = mix(u.p[0], u.p[1], t); | |
| 135 | } | |
| 136 | let m = read_mask(gid.xy, dims, u.p[5].z, u.p[5].w); | |
| 137 | col = col * m; | |
| 138 | // Output premultiplied RGBA | |
| 139 | let out_col = vec4<f32>(col.rgb * col.a, col.a); | |
| 140 | store_dst(vec2<i32>(gid.xy), out_col); | |
| 141 | } | |
| 142 | ||
| 143 | ||
| 144 | // ============================================================================ | |
| 145 | // 2. Merge (Transform FG + ApplyMode + Gain + Blend + Mask) | |
| 146 | // p[0] = (center.x, center.y, size, angle_rad) | |
| 147 | // p[1] = (blend, gain, apply_mode_id, edges_mode) | |
| 148 | // p[2] = (flip_h, flip_v, fg_aspect, bg_aspect) | |
| 149 | // p[3] = (has_mask, mask_invert, fg_w, fg_h) | |
| 150 | // ============================================================================ | |
| 151 | fn blend_lum(c: vec3<f32>) -> f32 { | |
| 152 | return dot(c, vec3<f32>(0.299, 0.587, 0.114)); | |
| 153 | } | |
| 154 | ||
| 155 | fn clip_color(col_in: vec3<f32>) -> vec3<f32> { | |
| 156 | var c = col_in; | |
| 157 | let l = blend_lum(c); | |
| 158 | let n = min(c.r, min(c.g, c.b)); | |
| 159 | let x = max(c.r, max(c.g, c.b)); | |
| 160 | if (n < 0.0) { | |
| 161 | c = vec3<f32>(l) + ((c - vec3<f32>(l)) * l) / max(l - n, 1e-5); | |
| 162 | } | |
| 163 | if (x > 1.0) { | |
| 164 | c = vec3<f32>(l) + ((c - vec3<f32>(l)) * (1.0 - l)) / max(x - l, 1e-5); | |
| 165 | } | |
| 166 | return c; | |
| 167 | } | |
| 168 | ||
| 169 | fn set_lum(c: vec3<f32>, l: f32) -> vec3<f32> { | |
| 170 | let d = l - blend_lum(c); | |
| 171 | return clip_color(c + vec3<f32>(d)); | |
| 172 | } | |
| 173 | ||
| 174 | fn sat_val(c: vec3<f32>) -> f32 { | |
| 175 | return max(c.r, max(c.g, c.b)) - min(c.r, min(c.g, c.b)); | |
| 176 | } | |
| 177 | ||
| 178 | fn set_sat(c_in: vec3<f32>, s: f32) -> vec3<f32> { | |
| 179 | let mn = min(c_in.r, min(c_in.g, c_in.b)); | |
| 180 | let mx = max(c_in.r, max(c_in.g, c_in.b)); | |
| 181 | let span = mx - mn; | |
| 182 | if (span <= 1e-5) { | |
| 183 | return vec3<f32>(0.0); | |
| 184 | } | |
| 185 | return (c_in - vec3<f32>(mn)) * (s / span); | |
| 186 | } | |
| 187 | ||
| 188 | fn apply_blend_mode(bg: vec3<f32>, fg: vec3<f32>, mode: i32) -> vec3<f32> { | |
| 189 | switch mode { | |
| 190 | case 1: { // Screen | |
| 191 | return 1.0 - (1.0 - bg) * (1.0 - fg); | |
| 192 | } | |
| 193 | case 2: { // Multiply | |
| 194 | return bg * fg; | |
| 195 | } | |
| 196 | case 3: { // Darken | |
| 197 | return min(bg, fg); | |
| 198 | } | |
| 199 | case 4: { // Lighten | |
| 200 | return max(bg, fg); | |
| 201 | } | |
| 202 | case 5: { // Difference | |
| 203 | return abs(bg - fg); | |
| 204 | } | |
| 205 | case 6: { // Exclusion | |
| 206 | return bg + fg - 2.0 * bg * fg; | |
| 207 | } | |
| 208 | case 7: { // Overlay | |
| 209 | let lo = 2.0 * bg * fg; | |
| 210 | let hi = 1.0 - 2.0 * (1.0 - bg) * (1.0 - fg); | |
| 211 | return select(hi, lo, bg < vec3<f32>(0.5)); | |
| 212 | } | |
| 213 | case 8: { // Hard Light | |
| 214 | let lo = 2.0 * bg * fg; | |
| 215 | let hi = 1.0 - 2.0 * (1.0 - bg) * (1.0 - fg); | |
| 216 | return select(hi, lo, fg < vec3<f32>(0.5)); | |
| 217 | } | |
| 218 | case 9: { // Soft Light (W3C / Photoshop / Fusion exact) | |
| 219 | let d_lo = ((16.0 * bg - 12.0) * bg + 4.0) * bg; | |
| 220 | let d_hi = sqrt(max(bg, vec3<f32>(0.0))); | |
| 221 | let d = select(d_hi, d_lo, bg <= vec3<f32>(0.25)); | |
| 222 | let lo = bg - (1.0 - 2.0 * fg) * bg * (1.0 - bg); | |
| 223 | let hi = bg + (2.0 * fg - 1.0) * (d - bg); | |
| 224 | return select(hi, lo, fg <= vec3<f32>(0.5)); | |
| 225 | } | |
| 226 | case 10: { // Linear Light | |
| 227 | return clamp(bg + 2.0 * fg - 1.0, vec3<f32>(0.0), vec3<f32>(1.0)); | |
| 228 | } | |
| 229 | case 11: { // Color Dodge | |
| 230 | return clamp(bg / max(1.0 - fg, vec3<f32>(1e-4)), vec3<f32>(0.0), vec3<f32>(1.0)); | |
| 231 | } | |
| 232 | case 12: { // Linear Dodge / Add | |
| 233 | return clamp(bg + fg, vec3<f32>(0.0), vec3<f32>(1.0)); | |
| 234 | } | |
| 235 | case 13: { // Vivid Light | |
| 236 | let burn = 1.0 - (1.0 - bg) / max(2.0 * fg, vec3<f32>(1e-4)); | |
| 237 | let dodge = bg / max(2.0 * (1.0 - fg), vec3<f32>(1e-4)); | |
| 238 | return clamp(select(dodge, burn, fg < vec3<f32>(0.5)), vec3<f32>(0.0), vec3<f32>(1.0)); | |
| 239 | } | |
| 240 | case 14: { // Hypotenuse | |
| 241 | return clamp(sqrt(bg * bg + fg * fg), vec3<f32>(0.0), vec3<f32>(1.0)); | |
| 242 | } | |
| 243 | case 15: { // Darker Color (Fusion compares Rec.601 luma, picks darker pixel whole) | |
| 244 | let l_fg = dot(fg, vec3<f32>(0.299, 0.587, 0.114)); | |
| 245 | let l_bg = dot(bg, vec3<f32>(0.299, 0.587, 0.114)); | |
| 246 | return select(bg, fg, l_fg < l_bg); | |
| 247 | } | |
| 248 | case 16: { // Lighter Color (Fusion compares Rec.601 luma, picks lighter pixel whole) | |
| 249 | let l_fg = dot(fg, vec3<f32>(0.299, 0.587, 0.114)); | |
| 250 | let l_bg = dot(bg, vec3<f32>(0.299, 0.587, 0.114)); | |
| 251 | return select(bg, fg, l_fg > l_bg); | |
| 252 | } | |
| 253 | case 17: { // Pin Light | |
| 254 | let lo = min(bg, 2.0 * fg); | |
| 255 | let hi = max(bg, 2.0 * fg - 1.0); | |
| 256 | return select(hi, lo, fg < vec3<f32>(0.5)); | |
| 257 | } | |
| 258 | case 18: { // Color Burn | |
| 259 | return 1.0 - clamp((1.0 - bg) / max(fg, vec3<f32>(1e-4)), vec3<f32>(0.0), vec3<f32>(1.0)); | |
| 260 | } | |
| 261 | case 19: { // Linear Burn | |
| 262 | return clamp(bg + fg - 1.0, vec3<f32>(0.0), vec3<f32>(1.0)); | |
| 263 | } | |
| 264 | case 20: { // Geometric | |
| 265 | return (2.0 * fg * bg) / max(fg + bg, vec3<f32>(1e-5)); | |
| 266 | } | |
| 267 | case 21: { // Hue | |
| 268 | return set_lum(set_sat(fg, sat_val(bg)), blend_lum(bg)); | |
| 269 | } | |
| 270 | case 22: { // Saturation | |
| 271 | return set_lum(set_sat(bg, sat_val(fg)), blend_lum(bg)); | |
| 272 | } | |
| 273 | case 23: { // Color | |
| 274 | return set_lum(fg, blend_lum(bg)); | |
| 275 | } | |
| 276 | case 24: { // Luminosity | |
| 277 | return set_lum(bg, blend_lum(fg)); | |
| 278 | } | |
| 279 | default: { // 0: Normal | |
| 280 | return fg; | |
| 281 | } | |
| 282 | } | |
| 283 | } | |
| 284 | ||
| 285 | @compute @workgroup_size(8, 8) | |
| 286 | fn merge_op(@builtin(global_invocation_id) gid: vec3<u32>) { | |
| 287 | let dims = textureDimensions(dst_tex); | |
| 288 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | |
| 289 | let bg_dod = u.p[5]; | |
| 290 | let has_bg_dod = bg_dod.z > 0.5 && bg_dod.w > 0.5; | |
| 291 | let bg_dims = select(vec2<f32>(dims), bg_dod.zw, has_bg_dod); | |
| 292 | let fg_dims = max(u.p[3].zw, vec2<f32>(1.0)); | |
| 293 | ||
| 294 | let bg_off = vec2<i32>(i32(round(u.p[6].z)), i32(round(u.p[6].w))); | |
| 295 | let bg_coord = vec2<i32>(gid.xy) - bg_off; | |
| 296 | let bg_tex_dims = vec2<i32>(textureDimensions(src_a)); | |
| 297 | var bg = vec4<f32>(0.0); | |
| 298 | if (bg_coord.x >= 0 && bg_coord.y >= 0 && bg_coord.x < bg_tex_dims.x && bg_coord.y < bg_tex_dims.y) { | |
| 299 | bg = textureLoad(src_a, bg_coord, 0); | |
| 300 | } | |
| 301 | // If BG has expanded DoD, map output texel into logical BG canvas Fusion UV! | |
| 302 | let canvas_px = select( | |
| 303 | vec2<f32>(gid.xy) + vec2<f32>(0.5), | |
| 304 | vec2<f32>(gid.xy) + vec2<f32>(0.5) + bg_dod.xy, | |
| 305 | has_bg_dod, | |
| 306 | ); | |
| 307 | let fu = vec2<f32>(canvas_px.x / bg_dims.x, 1.0 - canvas_px.y / bg_dims.y); | |
| 308 | ||
| 309 | let center = u.p[0].xy; | |
| 310 | let size = max(u.p[0].z, 1e-5); | |
| 311 | let angle = u.p[0].w; | |
| 312 | let blend = u.p[1].x; | |
| 313 | let gain = u.p[1].y; | |
| 314 | let mode = i32(u.p[1].z + 0.5); | |
| 315 | let edges = i32(u.p[1].w + 0.5); | |
| 316 | let nearest = u.p[2].z > 0.5; | |
| 317 | ||
| 318 | // Map output pixel in Fusion pixel space relative to Center into FG pixel space. | |
| 319 | let delta_px = (fu - center) * bg_dims; | |
| 320 | let c = cos(angle); | |
| 321 | let s = sin(angle); | |
| 322 | // Fusion Angle rotates CCW in Y-up, so inverse rotates CW: | |
| 323 | let rot_px = vec2<f32>(delta_px.x * c + delta_px.y * s, -delta_px.x * s + delta_px.y * c) / size; | |
| 324 | var fg_fu = rot_px / fg_dims + vec2<f32>(0.5); | |
| 325 | if (u.p[2].x > 0.5) { fg_fu.x = 1.0 - fg_fu.x; } | |
| 326 | if (u.p[2].y > 0.5) { fg_fu.y = 1.0 - fg_fu.y; } | |
| 327 | ||
| 328 | let fg = sample_dod_edges_mode(src_b, fg_fu, edges, u.p[4], nearest); | |
| 329 | let mask = read_mask_uv(canvas_px / bg_dims, u.p[3].x, u.p[3].y); | |
| 330 | let eff_blend = clamp(blend * mask, 0.0, 1.0); | |
| 331 | let op = i32(u.p[2].w + 0.5); // 0=Over, 1=Under, 2=In, 3=HeldOut, 4=Atop, 5=XOr | |
| 332 | ||
| 333 | let burn_in = u.p[6].x; | |
| 334 | ||
| 335 | var out_col: vec4<f32>; | |
| 336 | if (mode == 0) { | |
| 337 | let fg_occ = fg.a * gain * (1.0 - burn_in); | |
| 338 | var comp: vec4<f32>; | |
| 339 | if (op == 1) { | |
| 340 | // Under: FG behind BG | |
| 341 | let bg_occ = bg.a; | |
| 342 | comp = vec4<f32>(bg.rgb + fg.rgb * (1.0 - bg_occ), bg.a + fg.a * (1.0 - bg_occ)); | |
| 343 | } else if (op == 2) { | |
| 344 | // In: FG masked by BG alpha | |
| 345 | comp = fg * bg.a; | |
| 346 | } else if (op == 3) { | |
| 347 | // HeldOut: BG masked by (1 - FG alpha) | |
| 348 | comp = bg * (1.0 - fg_occ); | |
| 349 | } else if (op == 4) { | |
| 350 | // Atop: FG inside BG + BG outside FG | |
| 351 | comp = vec4<f32>(fg.rgb * bg.a + bg.rgb * (1.0 - fg_occ), bg.a); | |
| 352 | } else if (op == 5) { | |
| 353 | // XOr | |
| 354 | comp = fg * (1.0 - bg.a) + bg * (1.0 - fg_occ); | |
| 355 | } else { | |
| 356 | // Over (default) with Fusion Alpha Gain, Subtractive/Additive, and BurnIn | |
| 357 | let over_rgb = fg.rgb + bg.rgb * (1.0 - fg_occ); | |
| 358 | let over_a = fg.a * gain + bg.a * (1.0 - fg.a * gain); | |
| 359 | comp = vec4<f32>(over_rgb, over_a); | |
| 360 | } | |
| 361 | out_col = mix(bg, comp, eff_blend); | |
| 362 | } else if (mode == 15 || mode == 16) { | |
| 363 | // Fusion DarkerColor/LighterColor with AlphaGain: candidate is FG + BG*(1 - fg.a*gain), | |
| 364 | // compared against BG by Rec.601 luma! (Verified bit-exact on probe_merge f09 & f10!) | |
| 365 | let fg_a_eff = fg.a * gain; | |
| 366 | let bg_u = select(vec3<f32>(0.0), bg.rgb / max(bg.a, 1e-5), bg.a > 1e-5); | |
| 367 | let fg_u = select(vec3<f32>(0.0), fg.rgb / max(fg.a, 1e-5), fg.a > 1e-5); | |
| 368 | let cand = fg_u + bg_u * (1.0 - fg_a_eff); | |
| 369 | let blended = apply_blend_mode(bg_u, cand, mode); | |
| 370 | let comp_rgb = bg.a * fg.a * blended + fg.rgb * (1.0 - bg.a) + (1.0 - fg.a) * bg.rgb; | |
| 371 | let comp_a = bg.a + fg_a_eff * (1.0 - bg.a); | |
| 372 | out_col = mix(bg, vec4<f32>(comp_rgb, comp_a), eff_blend); | |
| 373 | } else { | |
| 374 | let bg_u = select(vec3<f32>(0.0), bg.rgb / max(bg.a, 1e-5), bg.a > 1e-5); | |
| 375 | let fg_u = select(vec3<f32>(0.0), fg.rgb / max(fg.a, 1e-5), fg.a > 1e-5); | |
| 376 | // In Fusion's special fill-opacity modes (Difference, Exclusion, Screen, LinearDodge), | |
| 377 | // Gain scales FG color intensity BEFORE blending (e.g. Difference = |bg - gain*fg|!). | |
| 378 | let scale_fg_rgb = (mode == 5 || mode == 6 || mode == 1 || mode == 12); | |
| 379 | let fg_blend_in = select(fg_u, fg_u * gain, scale_fg_rgb); | |
| 380 | let blended = apply_blend_mode(bg_u, fg_blend_in, mode); | |
| 381 | let fg_eff = select(fg.a * gain, fg.a, scale_fg_rgb); | |
| 382 | let comp_rgb = fg_eff * bg.a * blended + fg_eff * (1.0 - bg.a) * fg_blend_in + (1.0 - fg_eff) * bg.rgb; | |
| 383 | let comp_a = bg.a + fg_eff * (1.0 - bg.a); | |
| 384 | out_col = mix(bg, vec4<f32>(comp_rgb, comp_a), eff_blend); | |
| 385 | } | |
| 386 | store_dst(vec2<i32>(gid.xy), out_col); | |
| 387 | } | |
| 388 | ||
| 389 | ||
| 390 | // ============================================================================ | |
| 391 | // 3. Transform | |
| 392 | // p[0] = (center.x, center.y, pivot.x, pivot.y) | |
| 393 | // p[1] = (size_x, size_y, angle_rad, edges) | |
| 394 | // p[2] = (flip_h, flip_v, blend, has_mask) | |
| 395 | // p[3] = (mask_invert, 0, 0, 0) | |
| 396 | // ============================================================================ | |
| 397 | @compute @workgroup_size(8, 8) | |
| 398 | fn transform_op(@builtin(global_invocation_id) gid: vec3<u32>) { | |
| 399 | let dims = textureDimensions(dst_tex); | |
| 400 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | |
| 401 | let fdims = vec2<f32>(dims); | |
| 402 | let fu = pix_to_fu(gid.xy, dims); | |
| 403 | ||
| 404 | let center = u.p[0].xy; | |
| 405 | let pivot = u.p[0].zw; | |
| 406 | let sx = max(u.p[1].x, 1e-5); | |
| 407 | let sy = max(u.p[1].y, 1e-5); | |
| 408 | let angle = u.p[1].z; | |
| 409 | let edges = i32(u.p[1].w + 0.5); | |
| 410 | ||
| 411 | // Forward in Fusion: p' = R(angle) * S(sx, sy) * (p - pivot) + pivot + (center - 0.5) | |
| 412 | let shift = center - vec2<f32>(0.5); | |
| 413 | let rel_px = (fu - pivot - shift) * fdims; | |
| 414 | let c = cos(angle); | |
| 415 | let s = sin(angle); | |
| 416 | let unrot = vec2<f32>(rel_px.x * c + rel_px.y * s, -rel_px.x * s + rel_px.y * c); | |
| 417 | var src_fu = vec2<f32>(unrot.x / sx, unrot.y / sy) / fdims + pivot; | |
| 418 | if (u.p[2].x > 0.5) { src_fu.x = 1.0 - src_fu.x; } | |
| 419 | if (u.p[2].y > 0.5) { src_fu.y = 1.0 - src_fu.y; } | |
| 420 | ||
| 421 | let nearest = u.p[3].y > 0.5; | |
| 422 | let orig = sample_dod_edges_mode(src_a, fu, 0, u.p[4], nearest); | |
| 423 | let moved = sample_dod_edges_mode(src_a, src_fu, edges, u.p[4], nearest); | |
| 424 | let m = read_mask(gid.xy, dims, u.p[2].w, u.p[3].x) * u.p[2].z; | |
| 425 | let out_col = mix(orig, moved, clamp(m, 0.0, 1.0)); | |
| 426 | store_dst(vec2<i32>(gid.xy), out_col); | |
| 427 | } | |
| 428 | ||
| 429 | ||
| 430 | // ============================================================================ | |
| 431 | // 4. BrightnessContrast | |
| 432 | // p[0] = (gain, lift, gamma, contrast) | |
| 433 | // p[1] = (brightness, saturation, prediv, blend) | |
| 434 | // p[2] = (has_mask, mask_invert, low, high) | |
| 435 | // p[3] = (proc_r, proc_g, proc_b, proc_a) | |
| 436 | // ============================================================================ | |
| 437 | fn bc_curve_scalar(v_in: f32, low: f32, high: f32, gain: f32, lift: f32, gamma: f32, contrast: f32, brightness: f32) -> f32 { | |
| 438 | var v = v_in; | |
| 439 | if (abs(low) > 1e-5 || abs(high - 1.0) > 1e-5) { | |
| 440 | let span = max(high - low, 1e-5); | |
| 441 | v = (v - low) / span; | |
| 442 | } | |
| 443 | v = v * (gain - lift) + lift; | |
| 444 | v = pow(max(v, 0.0), 1.0 / gamma); | |
| 445 | v = (v - 0.5) * (1.0 + contrast) + 0.5; | |
| 446 | v = v + brightness; | |
| 447 | return v; | |
| 448 | } | |
| 449 | ||
| 450 | @compute @workgroup_size(8, 8) | |
| 451 | fn bc_op(@builtin(global_invocation_id) gid: vec3<u32>) { | |
| 452 | let dims = textureDimensions(dst_tex); | |
| 453 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | |
| 454 | let src = textureLoad(src_a, vec2<i32>(gid.xy), 0); | |
| 455 | let gain = u.p[0].x; | |
| 456 | let lift = u.p[0].y; | |
| 457 | let gamma = max(u.p[0].z, 1e-4); | |
| 458 | let contrast = u.p[0].w; | |
| 459 | let brightness = u.p[1].x; | |
| 460 | let sat = u.p[1].y; | |
| 461 | let prediv = u.p[1].z > 0.5; | |
| 462 | let low = u.p[2].z; | |
| 463 | let high = u.p[2].w; | |
| 464 | let proc_r = u.p[3].x > 0.5; | |
| 465 | let proc_g = u.p[3].y > 0.5; | |
| 466 | let proc_b = u.p[3].z > 0.5; | |
| 467 | let proc_a = u.p[3].w > 0.5; | |
| 468 | ||
| 469 | var rgb = src.rgb; | |
| 470 | if (prediv && src.a > 1e-5) { | |
| 471 | rgb = rgb / src.a; | |
| 472 | } | |
| 473 | ||
| 474 | var adj_rgb = vec3<f32>( | |
| 475 | bc_curve_scalar(rgb.r, low, high, gain, lift, gamma, contrast, brightness), | |
| 476 | bc_curve_scalar(rgb.g, low, high, gain, lift, gamma, contrast, brightness), | |
| 477 | bc_curve_scalar(rgb.b, low, high, gain, lift, gamma, contrast, brightness), | |
| 478 | ); | |
| 479 | let luma = dot(adj_rgb, vec3<f32>(0.299, 0.587, 0.114)); | |
| 480 | adj_rgb = mix(vec3<f32>(luma), adj_rgb, sat); | |
| 481 | ||
| 482 | let out_rgb_unpre = vec3<f32>( | |
| 483 | select(rgb.r, adj_rgb.r, proc_r), | |
| 484 | select(rgb.g, adj_rgb.g, proc_g), | |
| 485 | select(rgb.b, adj_rgb.b, proc_b), | |
| 486 | ); | |
| 487 | let out_a = select(src.a, clamp(bc_curve_scalar(src.a, low, high, gain, lift, gamma, contrast, brightness), 0.0, 1.0), proc_a); | |
| 488 | let out_rgb = select(out_rgb_unpre, out_rgb_unpre * out_a, prediv); | |
| 489 | let adjusted = vec4<f32>(out_rgb, out_a); | |
| 490 | let m = read_mask(gid.xy, dims, u.p[2].x, u.p[2].y) * u.p[1].w; | |
| 491 | let out_col = mix(src, adjusted, clamp(m, 0.0, 1.0)); | |
| 492 | store_dst(vec2<i32>(gid.xy), out_col); | |
| 493 | } | |
| 494 | ||
| 495 | ||
| 496 | // ============================================================================ | |
| 497 | // 5. ColorCorrector (Master + Shadows/Midtones/Highlights wheels, TintMode 0 & 1, LGG) | |
| 498 | // p[0] = (hue1, sat1, tint_angle1, tint_len1) | |
| 499 | // p[1] = (gain1, lift1, gamma1, contrast1) | |
| 500 | // p[2] = (brightness1, prediv, blend, has_mask) | |
| 501 | // p[3] = (mask_invert, tint_mode, has_smh, 0) | |
| 502 | // p[4] = (hue2, sat2, tint_angle2, tint_len2) // Shadows | |
| 503 | // p[5] = (gain2, lift2, gamma2, contrast2) | |
| 504 | // p[6] = (hue3, sat3, tint_angle3, tint_len3) // Midtones | |
| 505 | // p[7] = (gain3, lift3, gamma3, contrast3) | |
| 506 | // p[8] = (hue4, sat4, tint_angle4, tint_len4) // Highlights | |
| 507 | // p[9] = (gain4, lift4, gamma4, contrast4) | |
| 508 | // p[10] = (bright2, bright3, bright4, 0) | |
| 509 | // ============================================================================ | |
| 510 | fn rotate_hue_preserve_luma(c: vec3<f32>, angle: f32) -> vec3<f32> { | |
| 511 | // Paul Haeberli luminance-preserving shear-rotate-unshear around (1,1,1) with Rec.601 weights. | |
| 512 | // Verified 0.00/255 bit-exact against Fusion Studio 21.1 ColorCorrector! | |
| 513 | let k = vec3<f32>(0.299, 0.587, 0.114); | |
| 514 | let y = dot(c, k); | |
| 515 | let ch = c - vec3<f32>(y); | |
| 516 | let ca = cos(angle); | |
| 517 | let sa = sin(angle); | |
| 518 | let s3 = 0.5773502691896258; // 1 / sqrt(3) | |
| 519 | let ndv = (ch.r + ch.g + ch.b) * (1.0 / 3.0); | |
| 520 | let nxv = vec3<f32>(ch.g - ch.b, ch.b - ch.r, ch.r - ch.g) * s3; | |
| 521 | let v = ch * ca + nxv * sa + vec3<f32>(ndv * (1.0 - ca)); | |
| 522 | let dy = dot(v, k); | |
| 523 | return vec3<f32>(y) + v - vec3<f32>(dy); | |
| 524 | } | |
| 525 | ||
| 526 | fn hsv_pure_hue(h_turns: f32) -> vec3<f32> { | |
| 527 | let h = fract(h_turns) * 6.0; | |
| 528 | let r = clamp(abs(h - 3.0) - 1.0, 0.0, 1.0); | |
| 529 | let g = clamp(2.0 - abs(h - 2.0), 0.0, 1.0); | |
| 530 | let b = clamp(2.0 - abs(h - 4.0), 0.0, 1.0); | |
| 531 | return vec3<f32>(r, g, b); | |
| 532 | } | |
| 533 | ||
| 534 | fn apply_cc_wheel_and_lgg( | |
| 535 | rgb_in: vec3<f32>, | |
| 536 | hue_turns: f32, | |
| 537 | sat: f32, | |
| 538 | tint_turns: f32, | |
| 539 | tint_len: f32, | |
| 540 | tint_mode: f32, | |
| 541 | gain: f32, | |
| 542 | lift: f32, | |
| 543 | gamma_in: f32, | |
| 544 | contrast: f32, | |
| 545 | brightness: f32, | |
| 546 | ) -> vec3<f32> { | |
| 547 | let k_luma = vec3<f32>(0.299, 0.587, 0.114); | |
| 548 | let gamma = max(gamma_in, 1e-4); | |
| 549 | var rgb = rgb_in; | |
| 550 | ||
| 551 | // 1. Hue rotation (verified 0.00/255 vs Fusion 21.1) | |
| 552 | if (abs(hue_turns) > 1e-5) { | |
| 553 | rgb = rotate_hue_preserve_luma(rgb, -hue_turns * 6.28318530718); | |
| 554 | } | |
| 555 | ||
| 556 | // 2. Fusion Color Wheel Tint | |
| 557 | if (tint_len > 1e-5) { | |
| 558 | let l = clamp(tint_len, 0.0, 1.0); | |
| 559 | if (tint_mode < 0.5) { | |
| 560 | // TintMode = 0 (RGB cube tilt + perpendicular chroma squeeze, verified 0.00/255 at len=1.0!) | |
| 561 | let s3 = 0.5773502691896258; | |
| 562 | let e1 = vec3<f32>(s3, s3, s3); | |
| 563 | let a = tint_turns * 6.28318530718; | |
| 564 | let u_ch = normalize(vec3<f32>(cos(a), cos(a - 2.09439510239), cos(a + 2.09439510239))); | |
| 565 | let u_pp = normalize(cross(e1, u_ch)); | |
| 566 | let t = l * l; | |
| 567 | let th = 0.471238898038 * t; // 27.0 deg * len^2 | |
| 568 | let c_th = cos(th); | |
| 569 | let s_th = sin(th); | |
| 570 | let x1 = dot(rgb, e1); | |
| 571 | let x2 = dot(rgb, u_ch); | |
| 572 | let x3 = dot(rgb, u_pp); | |
| 573 | let y1 = c_th * x1 - s_th * x2; | |
| 574 | let y2 = s_th * x1 + c_th * x2; | |
| 575 | let y3 = (1.0 - t) * x3; | |
| 576 | rgb = y1 * e1 + y2 * u_ch + y3 * u_pp; | |
| 577 | } else if (tint_mode < 1.5) { | |
| 578 | // TintMode = 1: luma-preserving chroma blend + amp = l*((1-l) + l*2*min(y,1-y)), verified 0.00/255! | |
| 579 | let y_t = dot(rgb, k_luma); | |
| 580 | let pure = hsv_pure_hue(tint_turns); | |
| 581 | let pure_y = dot(pure, k_luma); | |
| 582 | let tc = pure - vec3<f32>(pure_y); | |
| 583 | let yc = clamp(y_t, 0.0, 1.0); | |
| 584 | let amp = l * ((1.0 - l) + l * 2.0 * min(yc, 1.0 - yc)); | |
| 585 | rgb = vec3<f32>(y_t) + (rgb - vec3<f32>(y_t)) * (1.0 - l) + tc * amp; | |
| 586 | } else { | |
| 587 | // TintMode = 2 (Fusion 21.1 default when omitted): lerp toward pure HSV chroma * 2*min(y,1-y) | |
| 588 | let y_t = dot(rgb, k_luma); | |
| 589 | let pure = hsv_pure_hue(tint_turns); | |
| 590 | let pure_y = dot(pure, k_luma); | |
| 591 | let tc = pure - vec3<f32>(pure_y); | |
| 592 | let yc = clamp(y_t, 0.0, 1.0); | |
| 593 | let amp = 2.0 * min(yc, 1.0 - yc); | |
| 594 | let tint_rgb = vec3<f32>(y_t) + tc * amp; | |
| 595 | rgb = mix(rgb, tint_rgb, l); | |
| 596 | } | |
| 597 | } | |
| 598 | ||
| 599 | // 3. Saturation (Rec.601 luma, no gamut crushing) | |
| 600 | if (abs(sat - 1.0) > 1e-5) { | |
| 601 | let luma = dot(rgb, k_luma); | |
| 602 | rgb = mix(vec3<f32>(luma), rgb, sat); | |
| 603 | } | |
| 604 | ||
| 605 | // 4. Contrast -> Lift/Gain -> Brightness -> Gamma | |
| 606 | if (abs(contrast - 1.0) > 1e-5) { | |
| 607 | rgb = (rgb - 0.5) * contrast + 0.5; | |
| 608 | } | |
| 609 | if (abs(gain - 1.0) > 1e-5 || abs(lift) > 1e-5) { | |
| 610 | rgb = rgb * (gain - lift) + lift; | |
| 611 | } | |
| 612 | if (abs(brightness) > 1e-5) { | |
| 613 | rgb = rgb + brightness; | |
| 614 | } | |
| 615 | if (abs(gamma - 1.0) > 1e-5) { | |
| 616 | rgb = pow(max(rgb, vec3<f32>(0.0)), vec3<f32>(1.0 / gamma)); | |
| 617 | } | |
| 618 | return rgb; | |
| 619 | } | |
| 620 | ||
| 621 | @compute @workgroup_size(8, 8) | |
| 622 | fn cc_op(@builtin(global_invocation_id) gid: vec3<u32>) { | |
| 623 | let dims = textureDimensions(dst_tex); | |
| 624 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | |
| 625 | let src = textureLoad(src_a, vec2<i32>(gid.xy), 0); | |
| 626 | let prediv = u.p[2].y > 0.5; | |
| 627 | let tint_mode = u.p[3].y; | |
| 628 | let has_smh = u.p[3].z > 0.5; | |
| 629 | let k_luma = vec3<f32>(0.299, 0.587, 0.114); | |
| 630 | ||
| 631 | var rgb = src.rgb; | |
| 632 | if (prediv && src.a > 1e-5) { | |
| 633 | rgb = rgb / src.a; | |
| 634 | } | |
| 635 | ||
| 636 | if (has_smh) { | |
| 637 | let y0 = clamp(dot(rgb, k_luma), 0.0, 1.0); | |
| 638 | let w_s = clamp(1.0 - 2.0 * y0, 0.0, 1.0); | |
| 639 | let w_h = clamp(2.0 * y0 - 1.0, 0.0, 1.0); | |
| 640 | let w_m = max(1.0 - w_s - w_h, 0.0); | |
| 641 | let rgb_s = apply_cc_wheel_and_lgg(rgb, u.p[4].x, u.p[4].y, u.p[4].z, u.p[4].w, tint_mode, u.p[5].x, u.p[5].y, u.p[5].z, u.p[5].w, u.p[10].x); | |
| 642 | let rgb_m = apply_cc_wheel_and_lgg(rgb, u.p[6].x, u.p[6].y, u.p[6].z, u.p[6].w, tint_mode, u.p[7].x, u.p[7].y, u.p[7].z, u.p[7].w, u.p[10].y); | |
| 643 | let rgb_h = apply_cc_wheel_and_lgg(rgb, u.p[8].x, u.p[8].y, u.p[8].z, u.p[8].w, tint_mode, u.p[9].x, u.p[9].y, u.p[9].z, u.p[9].w, u.p[10].z); | |
| 644 | rgb = rgb_s * w_s + rgb_m * w_m + rgb_h * w_h; | |
| 645 | } | |
| 646 | ||
| 647 | rgb = apply_cc_wheel_and_lgg(rgb, u.p[0].x, u.p[0].y, u.p[0].z, u.p[0].w, tint_mode, u.p[1].x, u.p[1].y, u.p[1].z, u.p[1].w, u.p[2].x); | |
| 648 | ||
| 649 | if (prediv) { | |
| 650 | rgb = rgb * src.a; | |
| 651 | } | |
| 652 | let m = read_mask(gid.xy, dims, u.p[2].w, u.p[3].x) * u.p[2].z; | |
| 653 | let out_col = mix(src, vec4<f32>(rgb, src.a), clamp(m, 0.0, 1.0)); | |
| 654 | store_dst(vec2<i32>(gid.xy), out_col); | |
| 655 | } | |
| 656 | ||
| 657 | ||
| 658 | // ============================================================================ | |
| 659 | // 6. LumaKeyer | |
| 660 | // p[0] = (low, high, invert, blend) | |
| 661 | // p[1] = (has_mask, mask_invert, 0, 0) | |
| 662 | // ============================================================================ | |
| 663 | @compute @workgroup_size(8, 8) | |
| 664 | fn luma_key_op(@builtin(global_invocation_id) gid: vec3<u32>) { | |
| 665 | let dims = textureDimensions(dst_tex); | |
| 666 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | |
| 667 | let src = textureLoad(src_a, vec2<i32>(gid.xy), 0); | |
| 668 | let low = u.p[0].x; | |
| 669 | let high = u.p[0].y; | |
| 670 | let invert = u.p[0].z > 0.5; | |
| 671 | let rgb = select(vec3<f32>(0.0), src.rgb / max(src.a, 1e-5), src.a > 1e-5); | |
| 672 | let luma = dot(rgb, vec3<f32>(0.299, 0.587, 0.114)); | |
| 673 | var a = clamp((luma - low) / max(high - low, 1e-5), 0.0, 1.0); | |
| 674 | if (invert) { | |
| 675 | a = 1.0 - a; | |
| 676 | } | |
| 677 | let out_a = a * src.a; | |
| 678 | let keyed = vec4<f32>(rgb * out_a, out_a); | |
| 679 | let m = read_mask(gid.xy, dims, u.p[1].x, u.p[1].y) * u.p[0].w; | |
| 680 | let out_col = mix(src, keyed, clamp(m, 0.0, 1.0)); | |
| 681 | store_dst(vec2<i32>(gid.xy), out_col); | |
| 682 | } | |
| 683 | ||
| 684 | ||
| 685 | // ============================================================================ | |
| 686 | // 7. Separable Gaussian / Box Blur & Directional Blur | |
| 687 | // p[0] = (dir.x, dir.y, radius_px, mode: 0=gauss, 1=box, 2=erode, 3=dilate) | |
| 688 | // p[1] = (blend, has_mask, mask_invert, 0) | |
| 689 | // ============================================================================ | |
| 690 | @compute @workgroup_size(8, 8) | |
| 691 | fn blur_op(@builtin(global_invocation_id) gid: vec3<u32>) { | |
| 692 | let dims = textureDimensions(dst_tex); | |
| 693 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | |
| 694 | let fdims = vec2<f32>(dims); | |
| 695 | let uv = (vec2<f32>(gid.xy) + 0.5) / fdims; | |
| 696 | let dir = u.p[0].xy / fdims; | |
| 697 | let radius = u.p[0].z; | |
| 698 | let mode = i32(u.p[0].w + 0.5); | |
| 699 | ||
| 700 | if (radius < 0.25) { | |
| 701 | textureStore(dst_tex, vec2<i32>(gid.xy), textureLoad(src_a, vec2<i32>(gid.xy), 0)); | |
| 702 | return; | |
| 703 | } | |
| 704 | ||
| 705 | let taps = clamp(i32(ceil(radius)), 1, 64); | |
| 706 | let step_scale = radius / f32(taps); | |
| 707 | ||
| 708 | var acc = vec4<f32>(0.0); | |
| 709 | var wsum = 0.0; | |
| 710 | ||
| 711 | let clamp_edges = u.p[1].w > 0.5; | |
| 712 | if (mode == 2 || mode == 3) { | |
| 713 | // Erode (min) / Dilate (max) | |
| 714 | acc = textureLoad(src_a, vec2<i32>(gid.xy), 0); | |
| 715 | for (var i = -taps; i <= taps; i = i + 1) { | |
| 716 | let s = textureSampleLevel(src_a, samp_lin, clamp(uv + dir * (f32(i) * step_scale), vec2<f32>(0.0), vec2<f32>(1.0)), 0.0); | |
| 717 | acc = select(max(acc, s), min(acc, s), mode == 2); | |
| 718 | } | |
| 719 | } else if (mode == 1) { | |
| 720 | // Box blur | |
| 721 | for (var i = -taps; i <= taps; i = i + 1) { | |
| 722 | let off = uv + dir * (f32(i) * step_scale); | |
| 723 | let s = select( | |
| 724 | select(vec4<f32>(0.0), textureSampleLevel(src_a, samp_lin, off, 0.0), off.x >= 0.0 && off.x <= 1.0 && off.y >= 0.0 && off.y <= 1.0), | |
| 725 | textureSampleLevel(src_a, samp_lin, clamp(off, vec2<f32>(0.0), vec2<f32>(1.0)), 0.0), | |
| 726 | clamp_edges, | |
| 727 | ); | |
| 728 | acc = acc + s; | |
| 729 | wsum = wsum + 1.0; | |
| 730 | } | |
| 731 | acc = acc / max(wsum, 1.0); | |
| 732 | } else { | |
| 733 | // Gaussian blur | |
| 734 | let sigma = max(radius * 0.45, 0.5); | |
| 735 | let inv2s2 = 0.5 / (sigma * sigma); | |
| 736 | for (var i = -taps; i <= taps; i = i + 1) { | |
| 737 | let d = f32(i) * step_scale; | |
| 738 | let w = exp(-d * d * inv2s2); | |
| 739 | let off = uv + dir * d; | |
| 740 | let s = select( | |
| 741 | select(vec4<f32>(0.0), textureSampleLevel(src_a, samp_lin, off, 0.0), off.x >= 0.0 && off.x <= 1.0 && off.y >= 0.0 && off.y <= 1.0), | |
| 742 | textureSampleLevel(src_a, samp_lin, clamp(off, vec2<f32>(0.0), vec2<f32>(1.0)), 0.0), | |
| 743 | clamp_edges, | |
| 744 | ); | |
| 745 | acc = acc + s * w; | |
| 746 | wsum = wsum + w; | |
| 747 | } | |
| 748 | acc = acc / max(wsum, 1e-6); | |
| 749 | } | |
| 750 | ||
| 751 | let orig = select( | |
| 752 | textureLoad(src_a, vec2<i32>(gid.xy), 0), | |
| 753 | textureLoad(src_b, vec2<i32>(gid.xy), 0), | |
| 754 | u.p[2].x > 0.5, | |
| 755 | ); | |
| 756 | let m = read_mask(gid.xy, dims, u.p[1].y, u.p[1].z) * u.p[1].x; | |
| 757 | let out_col = mix(orig, acc, clamp(m, 0.0, 1.0)); | |
| 758 | store_dst(vec2<i32>(gid.xy), out_col); | |
| 759 | } | |
| 760 | ||
| 761 | ||
| 762 | // ============================================================================ | |
| 763 | // 8. FastNoise (3D Perlin Gradient FBM with Seethe, Angle, Discontinuous & LUT) | |
| 764 | // p[0] = (x_scale, y_scale, detail, contrast) | |
| 765 | // p[1] = (brightness, seethe, discontinuous, center_x) | |
| 766 | // p[2] = (center_y, angle_rad, inverted, use_lut) | |
| 767 | // ============================================================================ | |
| 768 | fn grad3_dot(i: vec3<f32>, f: vec3<f32>) -> f32 { | |
| 769 | // Deterministic 3D gradient hash -> 12 Perlin cube-edge directions (smooth isotropic blobs!) | |
| 770 | var p3 = fract(i * vec3<f32>(0.1031, 0.1030, 0.0973)); | |
| 771 | p3 = p3 + dot(p3, p3.yxz + 33.33); | |
| 772 | let h = u32(floor(fract((p3.x + p3.y) * p3.z) * 16.0)) & 15u; | |
| 773 | let u_v = select(f.y, f.x, h < 8u); | |
| 774 | let v_v = select(select(f.z, f.x, h == 12u || h == 14u), f.y, h < 4u); | |
| 775 | let s1 = select(-u_v, u_v, (h & 1u) == 0u); | |
| 776 | let s2 = select(-v_v, v_v, (h & 2u) == 0u); | |
| 777 | return s1 + s2; | |
| 778 | } | |
| 779 | ||
| 780 | fn noise3d(p: vec3<f32>) -> f32 { | |
| 781 | let i = floor(p); | |
| 782 | let f = fract(p); | |
| 783 | // Quintic C2 Perlin fade: eliminates blocky axis-aligned artifacts on thresholded peaks | |
| 784 | let u_f = f * f * f * (f * (f * 6.0 - 15.0) + 10.0); | |
| 785 | let n000 = grad3_dot(i + vec3<f32>(0.0, 0.0, 0.0), f - vec3<f32>(0.0, 0.0, 0.0)); | |
| 786 | let n100 = grad3_dot(i + vec3<f32>(1.0, 0.0, 0.0), f - vec3<f32>(1.0, 0.0, 0.0)); | |
| 787 | let n010 = grad3_dot(i + vec3<f32>(0.0, 1.0, 0.0), f - vec3<f32>(0.0, 1.0, 0.0)); | |
| 788 | let n110 = grad3_dot(i + vec3<f32>(1.0, 1.0, 0.0), f - vec3<f32>(1.0, 1.0, 0.0)); | |
| 789 | let n001 = grad3_dot(i + vec3<f32>(0.0, 0.0, 1.0), f - vec3<f32>(0.0, 0.0, 1.0)); | |
| 790 | let n101 = grad3_dot(i + vec3<f32>(1.0, 0.0, 1.0), f - vec3<f32>(1.0, 0.0, 1.0)); | |
| 791 | let n011 = grad3_dot(i + vec3<f32>(0.0, 1.0, 1.0), f - vec3<f32>(0.0, 1.0, 1.0)); | |
| 792 | let n111 = grad3_dot(i + vec3<f32>(1.0, 1.0, 1.0), f - vec3<f32>(1.0, 1.0, 1.0)); | |
| 793 | let nx00 = mix(n000, n100, u_f.x); | |
| 794 | let nx10 = mix(n010, n110, u_f.x); | |
| 795 | let nx01 = mix(n001, n101, u_f.x); | |
| 796 | let nx11 = mix(n011, n111, u_f.x); | |
| 797 | let nxy0 = mix(nx00, nx10, u_f.y); | |
| 798 | let nxy1 = mix(nx01, nx11, u_f.y); | |
| 799 | return mix(nxy0, nxy1, u_f.z); | |
| 800 | } | |
| 801 | ||
| 802 | @compute @workgroup_size(8, 8) | |
| 803 | fn fast_noise_op(@builtin(global_invocation_id) gid: vec3<u32>) { | |
| 804 | let dims = textureDimensions(dst_tex); | |
| 805 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | |
| 806 | let aspect = f32(dims.x) / max(f32(dims.y), 1.0); | |
| 807 | let fu = pix_to_fu(gid.xy, dims); | |
| 808 | let center = vec2<f32>(u.p[1].w, u.p[2].x); | |
| 809 | let scale = u.p[0].xy; | |
| 810 | let detail = clamp(u.p[0].z, 0.0, 10.0); | |
| 811 | let contrast = u.p[0].w; | |
| 812 | let brightness = u.p[1].x; | |
| 813 | let seethe = u.p[1].y; | |
| 814 | let disc = u.p[1].z > 0.5; | |
| 815 | let angle = u.p[2].y; | |
| 816 | ||
| 817 | // Fusion FastNoise uses frame-width isotropic units `(fu - center) * vec2(1, 1/aspect)` | |
| 818 | var rel = (fu - center) * vec2<f32>(1.0, 1.0 / aspect); | |
| 819 | if (abs(angle) > 1e-5) { | |
| 820 | let ca = cos(angle); | |
| 821 | let sa = sin(angle); | |
| 822 | rel = vec2<f32>(rel.x * ca - rel.y * sa, rel.x * sa + rel.y * ca); | |
| 823 | } | |
| 824 | let p = rel * scale; | |
| 825 | ||
| 826 | var val = 0.5 + brightness; | |
| 827 | if (detail > 1e-4) { | |
| 828 | let octaves = i32(ceil(detail)); | |
| 829 | var sum = 0.0; | |
| 830 | var norm = 0.0; | |
| 831 | var amp = 1.0; | |
| 832 | var freq = 1.0; | |
| 833 | for (var i = 0; i < 10; i = i + 1) { | |
| 834 | if (i >= octaves) { break; } | |
| 835 | let weight = select(amp, amp * fract(detail), i == octaves - 1 && fract(detail) > 1e-4); | |
| 836 | let n = noise3d(vec3<f32>(p * freq, seethe + f32(i) * 1.73)); | |
| 837 | sum = sum + select(n, abs(n), disc) * weight; | |
| 838 | norm = norm + weight; | |
| 839 | freq = freq * 2.0; | |
| 840 | amp = amp * 0.5; | |
| 841 | } | |
| 842 | let n_avg = sum / max(norm, 1.0); | |
| 843 | // Perlin3D normalized fBm: at Detail=1 (norm=1), *0.41 matches Fusion std=0.106 [0.24, 0.76]; | |
| 844 | // at Detail=10 (norm=2), std naturally narrows to ~0.060 matching Fusion's pinpoint starfield threshold! | |
| 845 | // Discontinuous mode takes |n| turbulence (~1.60x -> mean 0.34, std 0.14 in Type=0 TwoColor), | |
| 846 | // pivoting Contrast around 0.5 when Contrast != 1 (verified via probe_moon_3d f02 & displace_wave f05/f07/f08). | |
| 847 | if (disc) { | |
| 848 | let grad_bias = select(0.0, 0.25, u.p[2].w > 1.5); | |
| 849 | let base = n_avg * 1.60 + grad_bias; | |
| 850 | val = (base - 0.5) * contrast + 0.5 + brightness; | |
| 851 | } else { | |
| 852 | val = n_avg * 0.41 * contrast + 0.5 + brightness; | |
| 853 | } | |
| 854 | } | |
| 855 | val = clamp(val, 0.0, 1.0); | |
| 856 | if (u.p[2].z > 0.5) { val = 1.0 - val; } | |
| 857 | var col: vec4<f32>; | |
| 858 | if (u.p[2].w > 0.5) { | |
| 859 | // High-precision 256x1 gradient LUT in src_b (supports razor-sharp thresholds like starfields!) | |
| 860 | col = textureSampleLevel(src_b, samp_lin, vec2<f32>(val, 0.5), 0.0); | |
| 861 | } else { | |
| 862 | let lut_pos = val * 12.0; | |
| 863 | let i0 = u32(clamp(floor(lut_pos), 0.0, 11.0)); | |
| 864 | let f = lut_pos - f32(i0); | |
| 865 | col = mix(u.p[3u + i0], u.p[4u + i0], f); | |
| 866 | } | |
| 867 | let out_col = vec4<f32>(col.rgb * col.a, col.a); | |
| 868 | store_dst(vec2<i32>(gid.xy), out_col); | |
| 869 | } | |
| 870 | ||
| 871 | ||
| 872 | // ============================================================================ | |
| 873 | // 9. Displace (Type 0 = Radial/Bump with Spread RGB dispersion & LightPower, Type 1 = XY Refraction) | |
| 874 | // p[0] = (type, x_ref, y_ref, x_off) | |
| 875 | // p[1] = (y_off, ref_strength, blend, has_mask) | |
| 876 | // p[2] = (mask_invert, spread, light_power, offset) | |
| 877 | // p[3] = (center.x, center.y, 0, 0) | |
| 878 | // ============================================================================ | |
| 879 | fn pick_channel(c: vec4<f32>, ch: i32) -> f32 { | |
| 880 | switch ch { | |
| 881 | case 0: { return c.r; } | |
| 882 | case 1: { return c.g; } | |
| 883 | case 2: { return c.b; } | |
| 884 | case 3: { return c.a; } | |
| 885 | default: { return dot(c.rgb, vec3<f32>(0.299, 0.587, 0.114)); } | |
| 886 | } | |
| 887 | } | |
| 888 | ||
| 889 | @compute @workgroup_size(8, 8) | |
| 890 | fn displace_op(@builtin(global_invocation_id) gid: vec3<u32>) { | |
| 891 | let dims = textureDimensions(dst_tex); | |
| 892 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | |
| 893 | let fdims = vec2<f32>(dims); | |
| 894 | let fu = pix_to_fu(gid.xy, dims); | |
| 895 | let uv = fu_to_uv(fu); | |
| 896 | let kind = i32(u.p[0].x + 0.5); | |
| 897 | let orig = textureLoad(src_a, vec2<i32>(gid.xy), 0); | |
| 898 | ||
| 899 | var warped: vec4<f32>; | |
| 900 | if (kind == 1) { | |
| 901 | // XY Refraction: Fusion formula is `(fg[channel] + offset) * refraction` with clamped (Duplicate) edges! | |
| 902 | let fg = textureSampleLevel(src_b, samp_lin, uv, 0.0); | |
| 903 | let xch = i32(u.p[3].z + 0.5); | |
| 904 | let ych = i32(u.p[3].w + 0.5); | |
| 905 | let vx = pick_channel(fg, xch); | |
| 906 | let vy = pick_channel(fg, ych); | |
| 907 | let disp = vec2<f32>( | |
| 908 | (vx + u.p[0].w) * u.p[0].y, | |
| 909 | (vy + u.p[1].x) * u.p[0].z, | |
| 910 | ); | |
| 911 | warped = sample_edges(src_a, fu + disp, 2); | |
| 912 | } else { | |
| 913 | // Radial displace: Fusion shifts radially from Center by -(fu - center) * (luma + offset) * strength * 0.51. | |
| 914 | let fg_c = textureSampleLevel(src_b, samp_lin, uv, 0.0); | |
| 915 | let l_c = dot(fg_c.rgb, vec3<f32>(0.299, 0.587, 0.114)); | |
| 916 | let offset = u.p[2].w; | |
| 917 | let strength = u.p[1].y; | |
| 918 | let spread = u.p[2].y; | |
| 919 | let light_power = u.p[2].z; | |
| 920 | let center = u.p[3].xy; | |
| 921 | let dir_c = fu - center; | |
| 922 | let base_disp = -dir_c * (l_c + offset) * strength * 0.51; | |
| 923 | ||
| 924 | if (abs(spread) > 1e-4) { | |
| 925 | let sp = spread * 0.02; | |
| 926 | let sr = sample_edges(src_a, fu + base_disp * (1.0 - sp), 2); | |
| 927 | let sg = sample_edges(src_a, fu + base_disp, 2); | |
| 928 | let sb = sample_edges(src_a, fu + base_disp * (1.0 + sp), 2); | |
| 929 | warped = vec4<f32>(sr.r, sg.g, sb.b, max(sr.a, max(sg.a, sb.a))); | |
| 930 | } else { | |
| 931 | warped = sample_edges(src_a, fu + base_disp, 2); | |
| 932 | } | |
| 933 | ||
| 934 | if (abs(light_power) > 1e-4) { | |
| 935 | let light_angle = u.p[4].x; | |
| 936 | let light_ch = i32(u.p[4].y + 0.5); | |
| 937 | // Fusion Displace radial LightPower adds directional slope lighting from Foreground | |
| 938 | // along LightAngle (default 0 deg -> diagonal (+1, +1) in UV y-down, per-channel RGB by default). | |
| 939 | let dir_uv = vec2<f32>(cos(light_angle), -sin(light_angle)) * 1.41421356 / fdims; | |
| 940 | let s_pos = textureSampleLevel(src_b, samp_lin, clamp(uv + dir_uv, vec2<f32>(0.0), vec2<f32>(1.0)), 0.0); | |
| 941 | let s_neg = textureSampleLevel(src_b, samp_lin, clamp(uv - dir_uv, vec2<f32>(0.0), vec2<f32>(1.0)), 0.0); | |
| 942 | var delta_rgb: vec3<f32>; | |
| 943 | if (light_ch >= 0 && light_ch <= 4) { | |
| 944 | let d = pick_channel(s_pos, light_ch) - pick_channel(s_neg, light_ch); | |
| 945 | delta_rgb = vec3<f32>(d, d, d); | |
| 946 | } else { | |
| 947 | delta_rgb = s_pos.rgb - s_neg.rgb; | |
| 948 | } | |
| 949 | warped = vec4<f32>(max(warped.rgb + delta_rgb * light_power, vec3<f32>(0.0)), warped.a); | |
| 950 | } | |
| 951 | } | |
| 952 | ||
| 953 | let m = read_mask(gid.xy, dims, u.p[1].w, u.p[2].x) * u.p[1].z; | |
| 954 | let out_col = mix(orig, warped, clamp(m, 0.0, 1.0)); | |
| 955 | store_dst(vec2<i32>(gid.xy), out_col); | |
| 956 | } | |
| 957 | ||
| 958 | ||
| 959 | // ============================================================================ | |
| 960 | // 10. Mask Shapes (EllipseMask & RectangleMask) + Mask Combine / Apply / BitmapMask | |
| 961 | // p[0] = (center.x, center.y, width, height) | |
| 962 | // p[1] = (angle_rad, soft_edge, corner_radius, invert) | |
| 963 | // p[2] = (shape: 0=Ellipse, 1=Rect, level, has_prev_mask, paint_mode) | |
| 964 | // p[3] = (border_width, solid: 1=filled/0=outline, 0, 0) | |
| 965 | // paint_mode: 0=Merge, 1=Add, 2=Subtract, 3=Multiply, 4=Min, 5=Max, 6=Invert, 7=Replace | |
| 966 | // ============================================================================ | |
| 967 | fn combine_mask_val(prev: f32, cur: f32, mode: i32) -> f32 { | |
| 968 | if (mode == 1) { return clamp(prev + cur, 0.0, 1.0); } // Add | |
| 969 | if (mode == 2) { return clamp(prev - cur, 0.0, 1.0); } // Subtract | |
| 970 | if (mode == 3) { return prev * cur; } // Multiply | |
| 971 | if (mode == 4) { return min(prev, cur); } // Minimum | |
| 972 | if (mode == 5) { return max(prev, cur); } // Maximum | |
| 973 | if (mode == 6) { return abs(prev - cur); } // Invert | |
| 974 | if (mode == 7) { return cur; } // Replace | |
| 975 | return clamp(prev + cur * (1.0 - prev), 0.0, 1.0); // Merge (default) | |
| 976 | } | |
| 977 | ||
| 978 | @compute @workgroup_size(8, 8) | |
| 979 | fn mask_shape_op(@builtin(global_invocation_id) gid: vec3<u32>) { | |
| 980 | let dims = textureDimensions(dst_tex); | |
| 981 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | |
| 982 | let aspect = f32(dims.x) / max(f32(dims.y), 1.0); | |
| 983 | let fu = pix_to_fu(gid.xy, dims); | |
| 984 | ||
| 985 | let center = u.p[0].xy; | |
| 986 | let half_sz = max(u.p[0].zw * 0.5, vec2<f32>(1e-5)); | |
| 987 | let angle = u.p[1].x; | |
| 988 | let soft = max(u.p[1].y, 0.5 / max(f32(dims.x), 1.0)); | |
| 989 | let radius = u.p[1].z; | |
| 990 | let invert = u.p[1].w > 0.5; | |
| 991 | let shape = i32(u.p[2].x + 0.5); | |
| 992 | let level = u.p[2].y; | |
| 993 | let has_prev = u.p[2].z > 0.5; | |
| 994 | let paint_mode = i32(u.p[2].w + 0.5); | |
| 995 | let border_w = u.p[3].x; | |
| 996 | let solid = u.p[3].y > 0.5; | |
| 997 | ||
| 998 | // Work in isotropic frame-width units (1.0 = canvas width) so rotations & circles are exact! | |
| 999 | let rel_w = (fu - center) * vec2<f32>(1.0, 1.0 / aspect); | |
| 1000 | let c = cos(angle); | |
| 1001 | let s = sin(angle); | |
| 1002 | let p_w = vec2<f32>(rel_w.x * c + rel_w.y * s, -rel_w.x * s + rel_w.y * c); | |
| 1003 | ||
| 1004 | var dist: f32; | |
| 1005 | if (shape == 0) { | |
| 1006 | // Fusion EllipseMask: BOTH Width and Height are in frame-width units (Width == Height -> circle)! | |
| 1007 | let q = p_w / half_sz; | |
| 1008 | let grad_len = max(length(q / half_sz), 1e-5); | |
| 1009 | dist = (length(q) - 1.0) * (length(q) / grad_len); | |
| 1010 | } else { | |
| 1011 | // Fusion RectangleMask: Width is in frame-width units, Height is in frame-height units! | |
| 1012 | let h_w = vec2<f32>(half_sz.x, half_sz.y / aspect); | |
| 1013 | // CornerRadius in Fusion is 0..1 fraction of the shorter half-dimension. | |
| 1014 | let r_w = clamp(radius, 0.0, 1.0) * min(h_w.x, h_w.y); | |
| 1015 | let q = abs(p_w) - (h_w - vec2<f32>(r_w)); | |
| 1016 | dist = length(max(q, vec2<f32>(0.0))) + min(max(q.x, q.y), 0.0) - r_w; | |
| 1017 | } | |
| 1018 | ||
| 1019 | if (solid) { | |
| 1020 | dist = dist - border_w * 0.5; | |
| 1021 | } else { | |
| 1022 | dist = abs(dist) - abs(border_w) * 0.5; | |
| 1023 | } | |
| 1024 | ||
| 1025 | var a = 1.0 - smoothstep(-soft, soft, dist); | |
| 1026 | if (invert) { a = 1.0 - a; } | |
| 1027 | a = clamp(a * level, 0.0, 1.0); | |
| 1028 | if (has_prev) { | |
| 1029 | let uv = (vec2<f32>(gid.xy) + 0.5) / vec2<f32>(dims); | |
| 1030 | let prev = textureSampleLevel(mask_tex, samp_lin, uv, 0.0).a; | |
| 1031 | a = combine_mask_val(prev, a, paint_mode); | |
| 1032 | } | |
| 1033 | textureStore(dst_tex, vec2<i32>(gid.xy), vec4<f32>(a, a, a, a)); | |
| 1034 | } | |
| 1035 | ||
| 1036 | @compute @workgroup_size(8, 8) | |
| 1037 | fn mask_combine_op(@builtin(global_invocation_id) gid: vec3<u32>) { | |
| 1038 | let dims = textureDimensions(dst_tex); | |
| 1039 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | |
| 1040 | let uv = (vec2<f32>(gid.xy) + 0.5) / vec2<f32>(dims); | |
| 1041 | let paint_mode = i32(u.p[0].x + 0.5); | |
| 1042 | let invert = u.p[0].y > 0.5; | |
| 1043 | let level = select(1.0, u.p[0].z, u.p[0].z > -0.5); | |
| 1044 | let has_prev = u.p[0].w > 0.5; | |
| 1045 | var cur = textureLoad(src_a, vec2<i32>(gid.xy), 0).a; | |
| 1046 | if (invert) { cur = 1.0 - cur; } | |
| 1047 | cur = clamp(cur * level, 0.0, 1.0); | |
| 1048 | var a = cur; | |
| 1049 | if (has_prev) { | |
| 1050 | let prev = textureSampleLevel(mask_tex, samp_lin, uv, 0.0).a; | |
| 1051 | a = combine_mask_val(prev, cur, paint_mode); | |
| 1052 | } | |
| 1053 | textureStore(dst_tex, vec2<i32>(gid.xy), vec4<f32>(a, a, a, a)); | |
| 1054 | } | |
| 1055 | ||
| 1056 | @compute @workgroup_size(8, 8) | |
| 1057 | fn mask_apply_op(@builtin(global_invocation_id) gid: vec3<u32>) { | |
| 1058 | let dims = textureDimensions(dst_tex); | |
| 1059 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | |
| 1060 | let src = textureLoad(src_a, vec2<i32>(gid.xy), 0); | |
| 1061 | let m = read_mask(gid.xy, dims, 1.0, u.p[0].x); | |
| 1062 | textureStore(dst_tex, vec2<i32>(gid.xy), src * m); | |
| 1063 | } | |
| 1064 | ||
| 1065 | // 10b. BitmapMask channel extraction + Low/High clip + centered 1:1 pixel alignment | |
| 1066 | // p[0] = (channel: 0=A,1=Luma,2=R,3=G,4=B,5=Sat,6=Lightness,7=Hue, low, high, fit_stretch) | |
| 1067 | // p[1] = (center.x, center.y, src_origin.x, src_origin.y) | |
| 1068 | // p[2] = (src_canvas.x, src_canvas.y, 0, 0) | |
| 1069 | @compute @workgroup_size(8, 8) | |
| 1070 | fn bitmap_mask_op(@builtin(global_invocation_id) gid: vec3<u32>) { | |
| 1071 | let dims = textureDimensions(dst_tex); | |
| 1072 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | |
| 1073 | let fdims = vec2<f32>(dims); | |
| 1074 | let sdims = vec2<f32>(textureDimensions(src_a)); | |
| 1075 | let ch = i32(u.p[0].x + 0.5); | |
| 1076 | let lo = u.p[0].y; | |
| 1077 | let hi = u.p[0].z; | |
| 1078 | let fit_stretch = u.p[0].w > 0.5; | |
| 1079 | let center = u.p[1].xy; | |
| 1080 | let src_origin = u.p[1].zw; | |
| 1081 | let src_canvas = max(u.p[2].xy, vec2<f32>(1.0)); | |
| 1082 | ||
| 1083 | var col = vec4<f32>(0.0); | |
| 1084 | if (fit_stretch) { | |
| 1085 | let uv = (vec2<f32>(gid.xy) + 0.5) / fdims; | |
| 1086 | col = textureSampleLevel(src_a, samp_lin, uv, 0.0); | |
| 1087 | } else { | |
| 1088 | // Fusion BitmapMask places Image centered at 1:1 pixel scale inside the mask canvas | |
| 1089 | let shift_px = vec2<f32>((center.x - 0.5) * fdims.x, -(center.y - 0.5) * fdims.y); | |
| 1090 | let canvas_px = (vec2<f32>(gid.xy) + 0.5) - 0.5 * fdims - shift_px + 0.5 * src_canvas; | |
| 1091 | let tex_px = canvas_px - src_origin; | |
| 1092 | if (tex_px.x >= 0.0 && tex_px.x <= sdims.x && tex_px.y >= 0.0 && tex_px.y <= sdims.y) { | |
| 1093 | col = textureSampleLevel(src_a, samp_lin, tex_px / sdims, 0.0); | |
| 1094 | } | |
| 1095 | } | |
| 1096 | ||
| 1097 | var v = col.a; | |
| 1098 | if (ch == 1) { | |
| 1099 | // Fusion 21.1 BitmapMask Luminance channel (verified via probe_mask_softedge f08: BGR-order weights 0.114*R + 0.587*G + 0.299*B!) | |
| 1100 | v = dot(col.rgb, vec3<f32>(0.114, 0.587, 0.299)); | |
| 1101 | } else if (ch == 2) { | |
| 1102 | v = col.r; | |
| 1103 | } else if (ch == 3) { | |
| 1104 | v = col.g; | |
| 1105 | } else if (ch == 4) { | |
| 1106 | v = col.b; | |
| 1107 | } else if (ch == 5) { | |
| 1108 | let cmax = max(col.r, max(col.g, col.b)); | |
| 1109 | let cmin = min(col.r, min(col.g, col.b)); | |
| 1110 | v = select(0.0, (cmax - cmin) / cmax, cmax > 1e-5); | |
| 1111 | } else if (ch == 6) { | |
| 1112 | let cmax = max(col.r, max(col.g, col.b)); | |
| 1113 | let cmin = min(col.r, min(col.g, col.b)); | |
| 1114 | v = 0.5 * (cmax + cmin); | |
| 1115 | } else if (ch == 7) { | |
| 1116 | let cmax = max(col.r, max(col.g, col.b)); | |
| 1117 | let cmin = min(col.r, min(col.g, col.b)); | |
| 1118 | let d = cmax - cmin; | |
| 1119 | if (d > 1e-5) { | |
| 1120 | var h = 0.0; | |
| 1121 | if (cmax == col.r) { h = (col.g - col.b) / d + select(0.0, 6.0, col.g < col.b); } | |
| 1122 | else if (cmax == col.g) { h = (col.b - col.r) / d + 2.0; } | |
| 1123 | else { h = (col.r - col.g) / d + 4.0; } | |
| 1124 | v = h / 6.0; | |
| 1125 | } else { | |
| 1126 | v = 0.0; | |
| 1127 | } | |
| 1128 | } | |
| 1129 | ||
| 1130 | let span = hi - lo; | |
| 1131 | if (abs(span) < 1e-6) { | |
| 1132 | v = select(0.0, 1.0, v >= lo); | |
| 1133 | } else { | |
| 1134 | v = clamp((v - lo) / span, 0.0, 1.0); | |
| 1135 | } | |
| 1136 | textureStore(dst_tex, vec2<i32>(gid.xy), vec4<f32>(v, v, v, v)); | |
| 1137 | } | |
| 1138 | ||
| 1139 | // ============================================================================ | |
| 1140 | // 11. Fuse.Colorize (Clover's exact 2020 Colorize.fuse formula!) | |
| 1141 | // p[0] = (r1, g1, b1, a1), p[1] = (r2, g2, b2, a2), p[2] = (blend, has_mask, mask_invert, 0) | |
| 1142 | // ============================================================================ | |
| 1143 | @compute @workgroup_size(8, 8) | |
| 1144 | fn colorize_op(@builtin(global_invocation_id) gid: vec3<u32>) { | |
| 1145 | let dims = textureDimensions(dst_tex); | |
| 1146 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | |
| 1147 | let src = textureLoad(src_a, vec2<i32>(gid.xy), 0); | |
| 1148 | let c1 = u.p[0]; | |
| 1149 | let c2 = u.p[1]; | |
| 1150 | ||
| 1151 | let ab = src.a; | |
| 1152 | let aa = c1.a + ab * (c2.a - c1.a); | |
| 1153 | let unpre = select(vec3<f32>(0.0), src.rgb / ab, ab > 1e-6); | |
| 1154 | let out_rgb = (c1.rgb + unpre * (c2.rgb - c1.rgb)) * aa; | |
| 1155 | let col = vec4<f32>(out_rgb, aa); | |
| 1156 | let m = read_mask(gid.xy, dims, u.p[2].y, u.p[2].z) * u.p[2].x; | |
| 1157 | let out_col = mix(src, col, clamp(m, 0.0, 1.0)); | |
| 1158 | store_dst(vec2<i32>(gid.xy), out_col); | |
| 1159 | } | |
| 1160 | ||
| 1161 | ||
| 1162 | // ============================================================================ | |
| 1163 | // 12. Fuse.Outline (Clover's exact 2020 Outline.fuse passes!) | |
| 1164 | // outline_boost: boosts alpha * 120 (clamped in R/A via normalized scale, so we pass scale factor!) | |
| 1165 | // outline_finish: clamps dilated alpha, tints with outline color, then composites original on top! | |
| 1166 | // ============================================================================ | |
| 1167 | @compute @workgroup_size(8, 8) | |
| 1168 | fn outline_boost(@builtin(global_invocation_id) gid: vec3<u32>) { | |
| 1169 | let dims = textureDimensions(dst_tex); | |
| 1170 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | |
| 1171 | let src = textureLoad(src_a, vec2<i32>(gid.xy), 0); | |
| 1172 | let a = clamp(src.a * 120.0, 0.0, 1.0); | |
| 1173 | textureStore(dst_tex, vec2<i32>(gid.xy), vec4<f32>(a, a, a, a)); | |
| 1174 | } | |
| 1175 | ||
| 1176 | @compute @workgroup_size(8, 8) | |
| 1177 | fn outline_tint(@builtin(global_invocation_id) gid: vec3<u32>) { | |
| 1178 | let dims = textureDimensions(dst_tex); | |
| 1179 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | |
| 1180 | let blurred = textureLoad(src_a, vec2<i32>(gid.xy), 0); | |
| 1181 | let col = u.p[0]; // (r, g, b, a) | |
| 1182 | // Because we clamped boosted alpha to 1.0 in u8, emulate the 120x dilation threshold | |
| 1183 | let a = clamp(blurred.a * u.p[1].x, 0.0, 1.0) * col.a; | |
| 1184 | textureStore(dst_tex, vec2<i32>(gid.xy), vec4<f32>(col.rgb * a, a)); | |
| 1185 | } | |
| 1186 | ||
| 1187 | @compute @workgroup_size(8, 8) | |
| 1188 | fn outline_composite(@builtin(global_invocation_id) gid: vec3<u32>) { | |
| 1189 | let dims = textureDimensions(dst_tex); | |
| 1190 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | |
| 1191 | let outline = textureLoad(src_a, vec2<i32>(gid.xy), 0); | |
| 1192 | let orig = textureLoad(src_b, vec2<i32>(gid.xy), 0); | |
| 1193 | // Original over outline | |
| 1194 | let out_rgb = orig.rgb + outline.rgb * (1.0 - orig.a); | |
| 1195 | let out_a = orig.a + outline.a * (1.0 - orig.a); | |
| 1196 | store_dst(vec2<i32>(gid.xy), vec4<f32>(out_rgb, out_a)); | |
| 1197 | } | |
| 1198 | ||
| 1199 | // ============================================================================ | |
| 1200 | // 12c. TextPlus Circular Euclidean L2 Outline + Fill-Over-Outline Composite | |
| 1201 | // p[0] = (outline_r, outline_g, outline_b, outline_a) | |
| 1202 | // p[1] = (radius_px, 0, 0, 0) | |
| 1203 | // ============================================================================ | |
| 1204 | @compute @workgroup_size(8, 8) | |
| 1205 | fn text_outline_op(@builtin(global_invocation_id) gid: vec3<u32>) { | |
| 1206 | let dims = textureDimensions(dst_tex); | |
| 1207 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | |
| 1208 | let p = vec2<i32>(gid.xy); | |
| 1209 | let orig = textureLoad(src_a, p, 0); | |
| 1210 | if (orig.a >= 0.999) { | |
| 1211 | textureStore(dst_tex, p, orig); | |
| 1212 | return; | |
| 1213 | } | |
| 1214 | ||
| 1215 | let radius = clamp(u.p[1].x, 0.5, 28.0); | |
| 1216 | let r_int = i32(ceil(radius + 0.5)); | |
| 1217 | let r_max = radius + 0.5; | |
| 1218 | let r_max2 = r_max * r_max; | |
| 1219 | let max_p = vec2<i32>(dims) - vec2<i32>(1, 1); | |
| 1220 | ||
| 1221 | var max_a = orig.a; | |
| 1222 | for (var dy = -r_int; dy <= r_int; dy = dy + 1) { | |
| 1223 | let dy2 = f32(dy * dy); | |
| 1224 | for (var dx = -r_int; dx <= r_int; dx = dx + 1) { | |
| 1225 | let d2 = f32(dx * dx) + dy2; | |
| 1226 | if (d2 > r_max2) { continue; } | |
| 1227 | let sp = p + vec2<i32>(dx, dy); | |
| 1228 | if (sp.x < 0 || sp.y < 0 || sp.x > max_p.x || sp.y > max_p.y) { continue; } | |
| 1229 | let sa = textureLoad(src_a, sp, 0).a; | |
| 1230 | if (sa <= 0.0) { continue; } | |
| 1231 | // Subpixel Euclidean SDF dilation: partial glyph edge shifts boundary by (1 - sa) px | |
| 1232 | // without creating a 2-pixel soft outer halo that drops into ColorGain's DarkHue band! | |
| 1233 | let cov = clamp(radius - 0.5 + sa - sqrt(d2), 0.0, 1.0); | |
| 1234 | max_a = max(max_a, cov); | |
| 1235 | } | |
| 1236 | } | |
| 1237 | ||
| 1238 | let oa = max_a * u.p[0].a; | |
| 1239 | let outline_rgb = u.p[0].rgb * oa; | |
| 1240 | let out_rgb = orig.rgb + outline_rgb * (1.0 - orig.a); | |
| 1241 | let out_a = orig.a + oa * (1.0 - orig.a); | |
| 1242 | store_dst(p, vec4<f32>(out_rgb, out_a)); | |
| 1243 | } | |
| 1244 | ||
| 1245 | // ============================================================================ | |
| 1246 | // 13. Custom Wave Shader (matches `sin(x|y * w * n1 + n2 * n1 * w) * 0.5 + 0.5` per channel) | |
| 1247 | // p[0] = (n1, n2, w_ref, 0) | |
| 1248 | // p[1] = (r_mode, g_mode, b_mode, a_const) where mode: 0=const 0, 1=sin(x*...), 2=sin(y*...) | |
| 1249 | // ============================================================================ | |
| 1250 | fn eval_wave_ch(mode: i32, fu: vec2<f32>, n1: f32, n2: f32, w: f32) -> f32 { | |
| 1251 | let deg2rad = 0.017453292519943295; | |
| 1252 | if (mode == 1) { | |
| 1253 | return sin((fu.x * w * n1 + n2 * n1 * w) * deg2rad) * 0.5 + 0.5; | |
| 1254 | } else if (mode == 2) { | |
| 1255 | return sin((fu.y * w * n1 + n2 * n1 * w) * deg2rad) * 0.5 + 0.5; | |
| 1256 | } | |
| 1257 | return 0.0; | |
| 1258 | } | |
| 1259 | ||
| 1260 | @compute @workgroup_size(8, 8) | |
| 1261 | fn custom_wave_op(@builtin(global_invocation_id) gid: vec3<u32>) { | |
| 1262 | let dims = textureDimensions(dst_tex); | |
| 1263 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | |
| 1264 | let fu = pix_to_fu(gid.xy, dims); | |
| 1265 | let n1 = u.p[0].x; | |
| 1266 | let n2 = u.p[0].y; | |
| 1267 | let w = u.p[0].z; | |
| 1268 | // Fusion CustomTool trig functions operate in DEGREES! | |
| 1269 | let r = eval_wave_ch(i32(u.p[1].x + 0.5), fu, n1, n2, w); | |
| 1270 | let g = eval_wave_ch(i32(u.p[1].y + 0.5), fu, n1, n2, w); | |
| 1271 | let b = eval_wave_ch(i32(u.p[1].z + 0.5), fu, n1, n2, w); | |
| 1272 | let a = u.p[1].w; | |
| 1273 | textureStore(dst_tex, vec2<i32>(gid.xy), vec4<f32>(r, g, b, a)); | |
| 1274 | } | |
| 1275 | ||
| 1276 | // ============================================================================ | |
| 1277 | // 14. Crop / Scale / Copy | |
| 1278 | // p[0] = (x_off_norm, y_off_norm, x_scale, y_scale) | |
| 1279 | // ============================================================================ | |
| 1280 | @compute @workgroup_size(8, 8) | |
| 1281 | fn crop_op(@builtin(global_invocation_id) gid: vec3<u32>) { | |
| 1282 | let dims = textureDimensions(dst_tex); | |
| 1283 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | |
| 1284 | let src_dims = vec2<f32>(textureDimensions(src_a)); | |
| 1285 | let dst_dims = vec2<f32>(dims); | |
| 1286 | let x_off = u.p[0].x; | |
| 1287 | let y_off = u.p[0].y; | |
| 1288 | // In Fusion, Crop (XOffset, YOffset, XSize, YSize) extracts pixels from bottom-left (x_off, y_off) | |
| 1289 | let dst_y_up = dst_dims.y - 1.0 - f32(gid.y); | |
| 1290 | let src_x = f32(gid.x) + x_off; | |
| 1291 | let src_y_up = dst_y_up + y_off; | |
| 1292 | let src_y = src_dims.y - 1.0 - src_y_up; | |
| 1293 | if (src_x < 0.0 || src_x >= src_dims.x || src_y < 0.0 || src_y >= src_dims.y) { | |
| 1294 | textureStore(dst_tex, vec2<i32>(gid.xy), vec4<f32>(0.0)); | |
| 1295 | return; | |
| 1296 | } | |
| 1297 | let uv = (vec2<f32>(src_x, src_y) + 0.5) / src_dims; | |
| 1298 | let c = textureSampleLevel(src_a, samp_lin, uv, 0.0); | |
| 1299 | textureStore(dst_tex, vec2<i32>(gid.xy), c); | |
| 1300 | } | |
| 1301 | ||
| 1302 | // ============================================================================ | |
| 1303 | // 15. Glow / SoftGlow Combine | |
| 1304 | // p[0] = (gain, blend, has_mask, mask_invert) | |
| 1305 | // p[1] = (mode: 0=Screen [SoftGlow], 1=Replace [Glow], 2=Additive, 0, 0, 0) | |
| 1306 | // ============================================================================ | |
| 1307 | @compute @workgroup_size(8, 8) | |
| 1308 | fn glow_add_op(@builtin(global_invocation_id) gid: vec3<u32>) { | |
| 1309 | let dims = textureDimensions(dst_tex); | |
| 1310 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | |
| 1311 | let orig = textureLoad(src_a, vec2<i32>(gid.xy), 0); | |
| 1312 | let blurred = textureLoad(src_b, vec2<i32>(gid.xy), 0); | |
| 1313 | let gain = u.p[0].x; | |
| 1314 | let mode = i32(u.p[1].x + 0.5); | |
| 1315 | let m = read_mask(gid.xy, dims, u.p[0].z, u.p[0].w) * u.p[0].y; | |
| 1316 | var added: vec4<f32>; | |
| 1317 | if (mode == 1) { | |
| 1318 | // Fusion Glow tool: blurred / (1 - Glow), preserving blurred alpha | |
| 1319 | added = vec4<f32>(blurred.rgb * gain, clamp(blurred.a, 0.0, 1.0)); | |
| 1320 | } else if (mode == 2) { | |
| 1321 | added = vec4<f32>(orig.rgb + blurred.rgb * gain, clamp(orig.a + blurred.a * gain * 0.5, 0.0, 1.0)); | |
| 1322 | } else if (mode == 3) { | |
| 1323 | // Fusion Sharpen: unsharp mask `orig + (orig - blurred) * gain` | |
| 1324 | added = vec4<f32>(orig.rgb + (orig.rgb - blurred.rgb) * gain, orig.a); | |
| 1325 | } else { | |
| 1326 | // Fusion SoftGlow: Screen blend `orig + g - orig * g` where `g = clamp(blurred * gain, 0, 1)` | |
| 1327 | let g = clamp(blurred * gain, vec4<f32>(0.0), vec4<f32>(1.0)); | |
| 1328 | added = orig + g - orig * g; | |
| 1329 | } | |
| 1330 | let out_col = mix(orig, added, clamp(m, 0.0, 1.0)); | |
| 1331 | store_dst(vec2<i32>(gid.xy), out_col); | |
| 1332 | } | |
| 1333 | ||
| 1334 | // ============================================================================ | |
| 1335 | // 15b. MT_XGlow Preprocess (Threshold -> Gain -> SrcSat -> GlowMask RGB multiply) | |
| 1336 | // p[0] = (thresh_low, thresh_high, src_sat, has_glow_mask) | |
| 1337 | // p[1] = (gain, 0, 0, 0) | |
| 1338 | // ============================================================================ | |
| 1339 | @compute @workgroup_size(8, 8) | |
| 1340 | fn xglow_prep_op(@builtin(global_invocation_id) gid: vec3<u32>) { | |
| 1341 | let dims = textureDimensions(dst_tex); | |
| 1342 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | |
| 1343 | let src = textureLoad(src_a, vec2<i32>(gid.xy), 0); | |
| 1344 | let t_lo = u.p[0].x; | |
| 1345 | let t_hi = u.p[0].y; | |
| 1346 | let src_sat = u.p[0].z; | |
| 1347 | let has_gm = u.p[0].w > 0.5; | |
| 1348 | let gain = u.p[1].x; | |
| 1349 | ||
| 1350 | var col = src; | |
| 1351 | if (t_lo > 1e-5 || t_hi > 1e-5) { | |
| 1352 | let hi = select(1.0, t_hi, t_hi > t_lo + 1e-5); | |
| 1353 | let denom = max(hi - t_lo, 1e-5); | |
| 1354 | col = clamp((col - vec4<f32>(t_lo)) / denom, vec4<f32>(0.0), vec4<f32>(1.0)); | |
| 1355 | } | |
| 1356 | var rgb = col.rgb * gain; | |
| 1357 | let y = dot(rgb, vec3<f32>(0.299, 0.587, 0.114)); | |
| 1358 | rgb = mix(vec3<f32>(y), rgb, src_sat); | |
| 1359 | if (has_gm) { | |
| 1360 | let uv = (vec2<f32>(gid.xy) + 0.5) / vec2<f32>(dims); | |
| 1361 | let gm = textureSampleLevel(mask_tex, samp_lin, uv, 0.0).a; | |
| 1362 | rgb = rgb * gm; | |
| 1363 | } | |
| 1364 | store_dst(vec2<i32>(gid.xy), vec4<f32>(rgb, col.a)); | |
| 1365 | } | |
| 1366 | ||
| 1367 | // ============================================================================ | |
| 1368 | // 15c. MT_XGlow Octave Accumulate (Gradient tint -> YUV falloff + GlowSat -> Additive) | |
| 1369 | // src_a = running glow accumulator (or base), src_b = blurred octave (bilinearly upsampled!) | |
| 1370 | // p[0] = (grad_r, grad_g, grad_b, y_scale) where y_scale = gain / (falloff^i) | |
| 1371 | // p[1] = (uv_scale, add_base, alpha_scale, 0) | |
| 1372 | // ============================================================================ | |
| 1373 | @compute @workgroup_size(8, 8) | |
| 1374 | fn xglow_accum_op(@builtin(global_invocation_id) gid: vec3<u32>) { | |
| 1375 | let dims = textureDimensions(dst_tex); | |
| 1376 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | |
| 1377 | let uv = (vec2<f32>(gid.xy) + 0.5) / vec2<f32>(dims); | |
| 1378 | let base = select(vec4<f32>(0.0), textureLoad(src_a, vec2<i32>(gid.xy), 0), u.p[1].y > 0.5); | |
| 1379 | let oct = textureSampleLevel(src_b, samp_lin, uv, 0.0); | |
| 1380 | ||
| 1381 | let c = oct.rgb * u.p[0].xyz; | |
| 1382 | // Rec.601 RGB -> YUV -> scale (y_scale, uv_scale, uv_scale) -> RGB (matches MT_XGlow.fuse lines 421-423) | |
| 1383 | let y = 0.299 * c.r + 0.587 * c.g + 0.114 * c.b; | |
| 1384 | let u_c = (c.b - y) * 0.492111; | |
| 1385 | let v_c = (c.r - y) * 0.877283; | |
| 1386 | let ys = y * u.p[0].w; | |
| 1387 | let us = u_c * u.p[1].x; | |
| 1388 | let vs = v_c * u.p[1].x; | |
| 1389 | let r2 = ys + vs / 0.877283; | |
| 1390 | let b2 = ys + us / 0.492111; | |
| 1391 | let g2 = (ys - 0.299 * r2 - 0.114 * b2) / 0.587; | |
| 1392 | let add_rgb = max(vec3<f32>(r2, g2, b2), vec3<f32>(0.0)); | |
| 1393 | let add_a = max(oct.a * u.p[1].z, 0.0); | |
| 1394 | ||
| 1395 | let out_col = vec4<f32>(base.rgb + add_rgb, clamp(base.a + add_a, 0.0, 1.0)); | |
| 1396 | store_dst(vec2<i32>(gid.xy), out_col); | |
| 1397 | } | |
| 1398 | ||
| 1399 | ||
| 1400 | // ============================================================================ | |
| 1401 | // 16. Blit / Scale (source -> destination of any size, premultiplied) | |
| 1402 | // p[0] = (exposure_mul, gamma, channel_mode: 0=RGBA,1=R,2=G,3=B,4=A, checker_bg) | |
| 1403 | // ============================================================================ | |
| 1404 | @compute @workgroup_size(8, 8) | |
| 1405 | fn display_blit_op(@builtin(global_invocation_id) gid: vec3<u32>) { | |
| 1406 | let dims = textureDimensions(dst_tex); | |
| 1407 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | |
| 1408 | let t = (vec2<f32>(gid.xy) + 0.5) / vec2<f32>(dims); | |
| 1409 | let has_uv = (u.p[1].z > u.p[1].x) || (u.p[1].w > u.p[1].y); | |
| 1410 | let uv = select( | |
| 1411 | t, | |
| 1412 | vec2<f32>(mix(u.p[1].x, u.p[1].z, t.x), mix(u.p[1].y, u.p[1].w, t.y)), | |
| 1413 | has_uv, | |
| 1414 | ); | |
| 1415 | let has_dod = u.p[4].z > 0.5 && u.p[4].w > 0.5; | |
| 1416 | let sdims = vec2<f32>(textureDimensions(src_a)); | |
| 1417 | let sample_uv = select( | |
| 1418 | uv, | |
| 1419 | (vec2<f32>(uv.x * u.p[4].z, uv.y * u.p[4].w) - u.p[4].xy) / sdims, | |
| 1420 | has_dod, | |
| 1421 | ); | |
| 1422 | let nearest = u.p[2].x > 0.5; | |
| 1423 | var src: vec4<f32>; | |
| 1424 | if (sample_uv.x < 0.0 || sample_uv.x >= 1.0 || sample_uv.y < 0.0 || sample_uv.y >= 1.0) { | |
| 1425 | src = vec4<f32>(0.0); | |
| 1426 | } else if (nearest) { | |
| 1427 | let isdims = vec2<i32>(textureDimensions(src_a)); | |
| 1428 | let sx = clamp(i32(floor(sample_uv.x * f32(isdims.x))), 0, isdims.x - 1); | |
| 1429 | let sy = clamp(i32(floor(sample_uv.y * f32(isdims.y))), 0, isdims.y - 1); | |
| 1430 | src = textureLoad(src_a, vec2<i32>(sx, sy), 0); | |
| 1431 | } else { | |
| 1432 | src = textureSampleLevel(src_a, samp_lin, sample_uv, 0.0); | |
| 1433 | } | |
| 1434 | let exp_mul = u.p[0].x; | |
| 1435 | let inv_gamma = 1.0 / max(u.p[0].y, 1e-4); | |
| 1436 | let ch = i32(u.p[0].z + 0.5); | |
| 1437 | let checker = u.p[0].w > 0.5; | |
| 1438 | ||
| 1439 | var out_col = src; | |
| 1440 | if (ch == 1) { | |
| 1441 | out_col = vec4<f32>(src.r, src.r, src.r, 1.0); | |
| 1442 | } else if (ch == 2) { | |
| 1443 | out_col = vec4<f32>(src.g, src.g, src.g, 1.0); | |
| 1444 | } else if (ch == 3) { | |
| 1445 | out_col = vec4<f32>(src.b, src.b, src.b, 1.0); | |
| 1446 | } else if (ch == 4) { | |
| 1447 | out_col = vec4<f32>(src.a, src.a, src.a, 1.0); | |
| 1448 | } else { | |
| 1449 | var rgb = pow(max(src.rgb * exp_mul, vec3<f32>(0.0)), vec3<f32>(inv_gamma)); | |
| 1450 | if (checker) { | |
| 1451 | let cx = (gid.x / 16u) & 1u; | |
| 1452 | let cy = (gid.y / 16u) & 1u; | |
| 1453 | let bg = select(0.14, 0.20, (cx ^ cy) == 1u); | |
| 1454 | rgb = rgb + vec3<f32>(bg) * (1.0 - src.a); | |
| 1455 | out_col = vec4<f32>(rgb, 1.0); | |
| 1456 | } else { | |
| 1457 | out_col = vec4<f32>(rgb, src.a); | |
| 1458 | } | |
| 1459 | } | |
| 1460 | textureStore(dst_tex, vec2<i32>(gid.xy), clamp(out_col, vec4<f32>(0.0), vec4<f32>(1.0))); | |
| 1461 | } | |
| 1462 | ||
| 1463 | ||
| 1464 | ||
| 1465 | // ============================================================================ | |
| 1466 | // 17. InvertColor / Invert Macro | |
| 1467 | // p[0] = (proc_r, proc_g, proc_b, proc_a) | |
| 1468 | // p[1] = (blend, has_mask, mask_invert, 0) | |
| 1469 | // ============================================================================ | |
| 1470 | @compute @workgroup_size(8, 8) | |
| 1471 | fn invert_op(@builtin(global_invocation_id) gid: vec3<u32>) { | |
| 1472 | let dims = textureDimensions(dst_tex); | |
| 1473 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | |
| 1474 | let src = textureLoad(src_a, vec2<i32>(gid.xy), 0); | |
| 1475 | let a = src.a; | |
| 1476 | let unpre = select(vec3<f32>(0.0), src.rgb / max(a, 1e-5), a > 1e-5); | |
| 1477 | let new_a = select(a, 1.0 - a, u.p[0].w > 0.5); | |
| 1478 | let inv_rgb = vec3<f32>( | |
| 1479 | select(unpre.r, 1.0 - unpre.r, u.p[0].x > 0.5), | |
| 1480 | select(unpre.g, 1.0 - unpre.g, u.p[0].y > 0.5), | |
| 1481 | select(unpre.b, 1.0 - unpre.b, u.p[0].z > 0.5), | |
| 1482 | ); | |
| 1483 | let inv = vec4<f32>(inv_rgb * new_a, new_a); | |
| 1484 | let m = read_mask(gid.xy, dims, u.p[1].y, u.p[1].z) * u.p[1].x; | |
| 1485 | let out_col = mix(src, inv, clamp(m, 0.0, 1.0)); | |
| 1486 | store_dst(vec2<i32>(gid.xy), out_col); | |
| 1487 | } | |
| 1488 | ||
| 1489 | // ============================================================================ | |
| 1490 | // 17b. ChannelBoolean (full Fusion Operation + per-channel ToRed/Green/Blue/Alpha) | |
| 1491 | // src_a = Background, src_b = Foreground (or Background if unconnected) | |
| 1492 | // p[0] = (operation, to_r, to_g, to_b) | |
| 1493 | // p[1] = (to_a, blend, has_mask, mask_invert) | |
| 1494 | // ============================================================================ | |
| 1495 | fn cb_pick(ch: i32, bg: vec4<f32>, fg: vec4<f32>, bg_ch: f32) -> f32 { | |
| 1496 | switch ch { | |
| 1497 | case 0: { return fg.r; } | |
| 1498 | case 1: { return fg.g; } | |
| 1499 | case 2: { return fg.b; } | |
| 1500 | case 3: { return fg.a; } | |
| 1501 | case 4: { return bg_ch; } // Do Nothing | |
| 1502 | case 5: { return bg.r; } | |
| 1503 | case 6: { return bg.g; } | |
| 1504 | case 7: { return bg.b; } | |
| 1505 | case 8: { return bg.a; } | |
| 1506 | case 9: { return dot(bg.rgb, vec3<f32>(0.299, 0.587, 0.114)); } // Luma BG | |
| 1507 | case 10: { return dot(fg.rgb, vec3<f32>(0.299, 0.587, 0.114)); } // Luma FG | |
| 1508 | case 15: { return 0.0; } // Black / Clear | |
| 1509 | case 16: { return 1.0; } // White / Solid | |
| 1510 | default: { return fg.r; } | |
| 1511 | } | |
| 1512 | } | |
| 1513 | ||
| 1514 | fn cb_apply_op(op: i32, bg_v: f32, fg_v: f32) -> f32 { | |
| 1515 | switch op { | |
| 1516 | case 0: { return fg_v; } // Copy | |
| 1517 | case 1: { return bg_v + fg_v; } // Add | |
| 1518 | case 2: { return bg_v - fg_v; } // Subtract | |
| 1519 | case 6: { return bg_v * fg_v; } // Multiply | |
| 1520 | case 7: { return bg_v / max(fg_v, 1e-5); } // Divide | |
| 1521 | case 8: { return max(bg_v, fg_v); } // Maximum | |
| 1522 | case 9: { return min(bg_v, fg_v); } // Minimum | |
| 1523 | case 10: { return 1.0 - fg_v; } // Negative | |
| 1524 | case 11: { return 1.0; } // Solid | |
| 1525 | case 12: { return 0.0; } // Clear | |
| 1526 | case 13: { return abs(bg_v - fg_v); } // Difference | |
| 1527 | default: { return fg_v; } | |
| 1528 | } | |
| 1529 | } | |
| 1530 | ||
| 1531 | @compute @workgroup_size(8, 8) | |
| 1532 | fn channel_boolean_op(@builtin(global_invocation_id) gid: vec3<u32>) { | |
| 1533 | let dims = textureDimensions(dst_tex); | |
| 1534 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | |
| 1535 | let bg = textureLoad(src_a, vec2<i32>(gid.xy), 0); | |
| 1536 | let bg_dod = u.p[3]; | |
| 1537 | let has_bg_dod = bg_dod.z > 0.5 && bg_dod.w > 0.5; | |
| 1538 | let bg_dims = select(vec2<f32>(dims), bg_dod.zw, has_bg_dod); | |
| 1539 | let canvas_px = select( | |
| 1540 | vec2<f32>(gid.xy) + vec2<f32>(0.5), | |
| 1541 | vec2<f32>(gid.xy) + vec2<f32>(0.5) + bg_dod.xy, | |
| 1542 | has_bg_dod, | |
| 1543 | ); | |
| 1544 | let fu = vec2<f32>(canvas_px.x / bg_dims.x, 1.0 - canvas_px.y / bg_dims.y); | |
| 1545 | let fg = sample_dod_edges_mode(src_b, fu, 0, u.p[2], false); | |
| 1546 | ||
| 1547 | let op = i32(u.p[0].x + 0.5); | |
| 1548 | let to_r = i32(u.p[0].y + 0.5); | |
| 1549 | let to_g = i32(u.p[0].z + 0.5); | |
| 1550 | let to_b = i32(u.p[0].w + 0.5); | |
| 1551 | let to_a = i32(u.p[1].x + 0.5); | |
| 1552 | ||
| 1553 | let vr = select(cb_apply_op(op, bg.r, cb_pick(to_r, bg, fg, bg.r)), bg.r, to_r == 4); | |
| 1554 | let vg = select(cb_apply_op(op, bg.g, cb_pick(to_g, bg, fg, bg.g)), bg.g, to_g == 4); | |
| 1555 | let vb = select(cb_apply_op(op, bg.b, cb_pick(to_b, bg, fg, bg.b)), bg.b, to_b == 4); | |
| 1556 | let va = select(cb_apply_op(op, bg.a, cb_pick(to_a, bg, fg, bg.a)), bg.a, to_a == 4); | |
| 1557 | ||
| 1558 | let res = clamp(vec4<f32>(vr, vg, vb, va), vec4<f32>(0.0), vec4<f32>(1.0)); | |
| 1559 | let m = read_mask_uv(canvas_px / bg_dims, u.p[1].z, u.p[1].w) * u.p[1].y; | |
| 1560 | let out_col = mix(bg, res, clamp(m, 0.0, 1.0)); | |
| 1561 | store_dst(vec2<i32>(gid.xy), out_col); | |
| 1562 | } | |
| 1563 | ||
| 1564 | ||
| 1565 | // ============================================================================ | |
| 1566 | // 18. DirectionalBlur (Type 0=Linear, 1=Centered, 2=Radial, 3=Zoom + Glow) | |
| 1567 | // p[0] = (type, length, angle_rad, glow) | |
| 1568 | // p[1] = (center.x, center.y, blend, has_mask) | |
| 1569 | // p[2] = (mask_invert, sample_count, 0, 0) | |
| 1570 | // ============================================================================ | |
| 1571 | @compute @workgroup_size(8, 8) | |
| 1572 | fn dir_blur_op(@builtin(global_invocation_id) gid: vec3<u32>) { | |
| 1573 | let dims = textureDimensions(dst_tex); | |
| 1574 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | |
| 1575 | let fdims = vec2<f32>(dims); | |
| 1576 | let aspect = fdims.x / max(fdims.y, 1.0); | |
| 1577 | let fu = pix_to_fu(gid.xy, dims); | |
| 1578 | let orig = textureLoad(src_a, vec2<i32>(gid.xy), 0); | |
| 1579 | ||
| 1580 | let kind = i32(u.p[0].x + 0.5); | |
| 1581 | let length_v = u.p[0].y; | |
| 1582 | let angle = u.p[0].z; | |
| 1583 | let glow = u.p[0].w; | |
| 1584 | let center = u.p[1].xy; | |
| 1585 | let blend = u.p[1].z; | |
| 1586 | let samples = clamp(i32(u.p[2].y + 0.5), 4, 64); | |
| 1587 | ||
| 1588 | if (abs(length_v) < 1e-5 && abs(angle) < 1e-5) { | |
| 1589 | textureStore(dst_tex, vec2<i32>(gid.xy), orig); | |
| 1590 | return; | |
| 1591 | } | |
| 1592 | ||
| 1593 | var acc = vec4<f32>(0.0); | |
| 1594 | let inv_n = 1.0 / f32(samples); | |
| 1595 | let dir_vec = vec2<f32>(cos(angle), sin(angle) * aspect); | |
| 1596 | ||
| 1597 | for (var i = 0; i < samples; i = i + 1) { | |
| 1598 | let u_t = (f32(i) + 0.5) * inv_n; // 0..1 | |
| 1599 | var s_fu = fu; | |
| 1600 | if (kind == 0) { | |
| 1601 | // Type 0 = Linear (one-sided along +Angle) | |
| 1602 | s_fu = fu + dir_vec * (u_t * length_v); | |
| 1603 | } else if (kind == 1) { | |
| 1604 | // Type 1 = Radial (clockwise swirl by -u_t * angle + inward radial step by -u_t * length) | |
| 1605 | let rel = (fu - center) * vec2<f32>(aspect, 1.0); | |
| 1606 | let dist = length(rel); | |
| 1607 | let a = -u_t * angle; | |
| 1608 | let ca = cos(a); | |
| 1609 | let sa = sin(a); | |
| 1610 | let r_rot = vec2<f32>(rel.x * ca - rel.y * sa, rel.x * sa + rel.y * ca); | |
| 1611 | let radial_scale = select(0.0, max(1.0 - (u_t * length_v) / dist, 0.0), dist > 1e-5); | |
| 1612 | s_fu = center + (r_rot * radial_scale) / vec2<f32>(aspect, 1.0); | |
| 1613 | } else if (kind == 2) { | |
| 1614 | // Type 2 = Centered (symmetric span of total width length_v along Angle) | |
| 1615 | s_fu = fu + dir_vec * ((u_t - 0.5) * length_v); | |
| 1616 | } else { | |
| 1617 | // Type 3 = Zoom (streaks radiate outward from Center by sampling inward toward Center) | |
| 1618 | let rel = (fu - center) * vec2<f32>(aspect, 1.0); | |
| 1619 | let a = -u_t * angle; | |
| 1620 | let ca = cos(a); | |
| 1621 | let sa = sin(a); | |
| 1622 | let r_rot = vec2<f32>(rel.x * ca - rel.y * sa, rel.x * sa + rel.y * ca); | |
| 1623 | let scale = max(1.0 - u_t * length_v, 0.05); | |
| 1624 | s_fu = center + (r_rot * scale) / vec2<f32>(aspect, 1.0); | |
| 1625 | } | |
| 1626 | let uv = clamp(fu_to_uv(s_fu), vec2<f32>(0.0), vec2<f32>(1.0)); | |
| 1627 | acc = acc + textureSampleLevel(src_a, samp_lin, uv, 0.0); | |
| 1628 | } | |
| 1629 | acc = acc * inv_n; | |
| 1630 | // Fusion DirectionalBlur Glow brightens streaks smoothly (~1 + 5*glow) | |
| 1631 | if (glow > 1e-4) { | |
| 1632 | acc = vec4<f32>(acc.rgb * (1.0 + glow * 5.0), acc.a); | |
| 1633 | } | |
| 1634 | ||
| 1635 | let m = read_mask(gid.xy, dims, u.p[1].w, u.p[2].x) * blend; | |
| 1636 | let out_col = mix(orig, acc, clamp(m, 0.0, 1.0)); | |
| 1637 | store_dst(vec2<i32>(gid.xy), out_col); | |
| 1638 | } | |
| 1639 | ||
| 1640 | ||
| 1641 | // ============================================================================ | |
| 1642 | // 19. ColorGain (Non-luma-preserving HSV Hue shift across Dark/Mid/High + per-channel Gain/Lift/Gamma/Sat) | |
| 1643 | // p[0] = (gain_r, gain_g, gain_b, prediv) | |
| 1644 | // p[1] = (lift_r, lift_g, lift_b, blend) | |
| 1645 | // p[2] = (gamma_r, gamma_g, gamma_b, sat_mid) | |
| 1646 | // p[3] = (dark_hue, mid_hue, high_hue, has_mask) | |
| 1647 | // p[4] = (mask_invert, sat_dark, sat_high, 0) | |
| 1648 | // ============================================================================ | |
| 1649 | fn rgb_to_hsv(c: vec3<f32>) -> vec3<f32> { | |
| 1650 | let k = vec4<f32>(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); | |
| 1651 | let p = select(vec4<f32>(c.gb, k.xy), vec4<f32>(c.bg, k.wz), c.g < c.b); | |
| 1652 | let q = select(vec4<f32>(c.r, p.yzx), vec4<f32>(p.xyw, c.r), c.r < p.x); | |
| 1653 | let d = q.x - min(q.w, q.y); | |
| 1654 | let e = 1.0e-10; | |
| 1655 | return vec3<f32>(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); | |
| 1656 | } | |
| 1657 | ||
| 1658 | fn hsv_to_rgb(c: vec3<f32>) -> vec3<f32> { | |
| 1659 | let k = vec4<f32>(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); | |
| 1660 | let p = abs(fract(c.xxx + k.xyz) * 6.0 - k.www); | |
| 1661 | return c.z * mix(k.xxx, clamp(p - k.xxx, vec3<f32>(0.0), vec3<f32>(1.0)), c.y); | |
| 1662 | } | |
| 1663 | ||
| 1664 | @compute @workgroup_size(8, 8) | |
| 1665 | fn color_gain_op(@builtin(global_invocation_id) gid: vec3<u32>) { | |
| 1666 | let dims = textureDimensions(dst_tex); | |
| 1667 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | |
| 1668 | let src = textureLoad(src_a, vec2<i32>(gid.xy), 0); | |
| 1669 | let gain = u.p[0].xyz; | |
| 1670 | let prediv = u.p[0].w > 0.5; | |
| 1671 | let lift = u.p[1].xyz; | |
| 1672 | let blend = u.p[1].w; | |
| 1673 | let gamma = max(u.p[2].xyz, vec3<f32>(1e-4)); | |
| 1674 | let sat_mid = u.p[2].w; | |
| 1675 | let dark_hue = u.p[3].x; | |
| 1676 | let mid_hue = u.p[3].y; | |
| 1677 | let high_hue = u.p[3].z; | |
| 1678 | let sat_dark = u.p[4].y; | |
| 1679 | let sat_high = u.p[4].z; | |
| 1680 | ||
| 1681 | var rgb = src.rgb; | |
| 1682 | if (prediv && src.a > 1e-5) { | |
| 1683 | rgb = rgb / src.a; | |
| 1684 | } | |
| 1685 | ||
| 1686 | // Per-channel Gain -> Lift -> Gamma | |
| 1687 | rgb = rgb * gain; | |
| 1688 | rgb = rgb + lift * (1.0 - rgb); | |
| 1689 | rgb = pow(max(rgb, vec3<f32>(0.0)), vec3<f32>(1.0) / gamma); | |
| 1690 | ||
| 1691 | // Tonal range weights using HSV Value max(r,g,b) so saturated primaries & outer anti-aliased | |
| 1692 | // outline edges (>= 0.5) land cleanly in Mid without reddish DarkHue halos, while RGB band blending | |
| 1693 | // completely prevents green hue-interpolation fringes! | |
| 1694 | let c_pos = max(rgb, vec3<f32>(0.0)); | |
| 1695 | let lum = clamp(max(c_pos.r, max(c_pos.g, c_pos.b)), 0.0, 1.0); | |
| 1696 | let w_dark = clamp(1.0 - lum * 2.0, 0.0, 1.0); | |
| 1697 | let w_high = clamp((lum - 0.5) * 2.0, 0.0, 1.0); | |
| 1698 | let w_mid = clamp(1.0 - w_dark - w_high, 0.0, 1.0); | |
| 1699 | ||
| 1700 | // Fusion ColorGain evaluates Dark, High, then Mid sequentially as RGB mixes against base_hsv | |
| 1701 | // using V = max(r,g,b) for the range curve — matches Fusion 21.1 to ~0.002 and stays green-fringe-free! | |
| 1702 | let any_hsv = abs(dark_hue) > 1e-5 || abs(mid_hue) > 1e-5 || abs(high_hue) > 1e-5 | |
| 1703 | || abs(sat_dark - 1.0) > 1e-4 || abs(sat_mid - 1.0) > 1e-4 || abs(sat_high - 1.0) > 1e-4; | |
| 1704 | if (any_hsv) { | |
| 1705 | let base_hsv = rgb_to_hsv(c_pos); | |
| 1706 | var cur = c_pos; | |
| 1707 | if ((abs(dark_hue) > 1e-5 || abs(sat_dark - 1.0) > 1e-4) && w_dark > 1e-5) { | |
| 1708 | let h_d = vec3<f32>(fract(base_hsv.x + dark_hue), clamp(base_hsv.y * sat_dark, 0.0, 1.0), base_hsv.z); | |
| 1709 | cur = mix(cur, hsv_to_rgb(h_d), w_dark); | |
| 1710 | } | |
| 1711 | if ((abs(high_hue) > 1e-5 || abs(sat_high - 1.0) > 1e-4) && w_high > 1e-5) { | |
| 1712 | let h_h = vec3<f32>(fract(base_hsv.x + high_hue), clamp(base_hsv.y * sat_high, 0.0, 1.0), base_hsv.z); | |
| 1713 | cur = mix(cur, hsv_to_rgb(h_h), w_high); | |
| 1714 | } | |
| 1715 | if ((abs(mid_hue) > 1e-5 || abs(sat_mid - 1.0) > 1e-4) && w_mid > 1e-5) { | |
| 1716 | let h_m = vec3<f32>(fract(base_hsv.x + mid_hue), clamp(base_hsv.y * sat_mid, 0.0, 1.0), base_hsv.z); | |
| 1717 | cur = mix(cur, hsv_to_rgb(h_m), w_mid); | |
| 1718 | } | |
| 1719 | rgb = cur; | |
| 1720 | } | |
| 1721 | ||
| 1722 | if (prediv) { | |
| 1723 | rgb = rgb * src.a; | |
| 1724 | } | |
| 1725 | let m = read_mask(gid.xy, dims, u.p[3].w, u.p[4].x) * blend; | |
| 1726 | let out_col = mix(src, vec4<f32>(rgb, src.a), clamp(m, 0.0, 1.0)); | |
| 1727 | store_dst(vec2<i32>(gid.xy), out_col); | |
| 1728 | } | |
| 1729 | ||
| 1730 | ||
| 1731 | // ============================================================================ | |
| 1732 | // 20. ColorCurves (per-channel 256x1 LUT texture in src_b) | |
| 1733 | // p[0] = (prediv, ignore_transparent, blend, has_mask) | |
| 1734 | // p[1] = (mask_invert, apply_alpha, 0, 0) | |
| 1735 | // ============================================================================ | |
| 1736 | @compute @workgroup_size(8, 8) | |
| 1737 | fn color_curves_op(@builtin(global_invocation_id) gid: vec3<u32>) { | |
| 1738 | let dims = textureDimensions(dst_tex); | |
| 1739 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | |
| 1740 | let src = textureLoad(src_a, vec2<i32>(gid.xy), 0); | |
| 1741 | let prediv = u.p[0].x > 0.5; | |
| 1742 | let ignore_trans = u.p[0].y > 0.5; | |
| 1743 | let blend = u.p[0].z; | |
| 1744 | let apply_a = u.p[1].y > 0.5; | |
| 1745 | ||
| 1746 | if (ignore_trans && src.a <= 1e-5) { | |
| 1747 | textureStore(dst_tex, vec2<i32>(gid.xy), src); | |
| 1748 | return; | |
| 1749 | } | |
| 1750 | ||
| 1751 | var rgb = src.rgb; | |
| 1752 | let unpre = (prediv || ignore_trans) && src.a > 1e-5; | |
| 1753 | if (unpre) { | |
| 1754 | rgb = clamp(rgb / src.a, vec3<f32>(0.0), vec3<f32>(1.0)); | |
| 1755 | } | |
| 1756 | ||
| 1757 | let r = textureSampleLevel(src_b, samp_lin, vec2<f32>(clamp(rgb.r, 0.0, 1.0), 0.5), 0.0).r; | |
| 1758 | let g = textureSampleLevel(src_b, samp_lin, vec2<f32>(clamp(rgb.g, 0.0, 1.0), 0.5), 0.0).g; | |
| 1759 | let b = textureSampleLevel(src_b, samp_lin, vec2<f32>(clamp(rgb.b, 0.0, 1.0), 0.5), 0.0).b; | |
| 1760 | var a = src.a; | |
| 1761 | if (apply_a) { | |
| 1762 | a = textureSampleLevel(src_b, samp_lin, vec2<f32>(clamp(src.a, 0.0, 1.0), 0.5), 0.0).a; | |
| 1763 | } | |
| 1764 | ||
| 1765 | var out_rgb = vec3<f32>(r, g, b); | |
| 1766 | if (unpre) { | |
| 1767 | out_rgb = out_rgb * a; | |
| 1768 | } | |
| 1769 | let m = read_mask(gid.xy, dims, u.p[0].w, u.p[1].x) * blend; | |
| 1770 | let out_col = mix(src, vec4<f32>(out_rgb, a), clamp(m, 0.0, 1.0)); | |
| 1771 | store_dst(vec2<i32>(gid.xy), out_col); | |
| 1772 | } | |
| 1773 | ||
| 1774 | ||
| 1775 | // ============================================================================ | |
| 1776 | // 21. Fuse.XfChroma (Chromatic Aberration multi-step transform + Gradient LUT in src_b) | |
| 1777 | // p[0] = (center.x, center.y, pivot.x, pivot.y) (MasterFactor already applied) | |
| 1778 | // p[1] = (xsize, ysize, angle_rad, steps) | |
| 1779 | // p[2] = (acc_mode: 0=Add,1=Screen,2=Max,3=Merge, normalize, gain, gamma) | |
| 1780 | // p[3] = (blend, has_mask, mask_invert, 0) | |
| 1781 | // p[4] = (dod_ox, dod_oy, dod_cw, dod_ch) | |
| 1782 | // ============================================================================ | |
| 1783 | @compute @workgroup_size(8, 8) | |
| 1784 | fn xf_chroma_op(@builtin(global_invocation_id) gid: vec3<u32>) { | |
| 1785 | let dims = textureDimensions(dst_tex); | |
| 1786 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | |
| 1787 | let orig = textureLoad(src_a, vec2<i32>(gid.xy), 0); | |
| 1788 | let aspect = f32(dims.x) / max(f32(dims.y), 1.0); | |
| 1789 | let fu = pix_to_fu(gid.xy, dims); | |
| 1790 | ||
| 1791 | let center = u.p[0].xy; | |
| 1792 | let pivot = u.p[0].zw; | |
| 1793 | let xsize = u.p[1].x; | |
| 1794 | let ysize = u.p[1].y; | |
| 1795 | let angle = u.p[1].z; | |
| 1796 | let steps = clamp(i32(u.p[1].w + 0.5), 1, 32); | |
| 1797 | let acc_mode = i32(u.p[2].x + 0.5); | |
| 1798 | let normalize = u.p[2].y > 0.5; | |
| 1799 | let gain = u.p[2].z; | |
| 1800 | let gamma = max(u.p[2].w, 0.05); | |
| 1801 | let blend = u.p[3].x; | |
| 1802 | ||
| 1803 | var acc = vec4<f32>(0.0); | |
| 1804 | var grad_sum = vec4<f32>(0.0); | |
| 1805 | let inv_steps = 1.0 / f32(steps); | |
| 1806 | ||
| 1807 | for (var i = 0; i <= steps; i = i + 1) { | |
| 1808 | let n = f32(i) * inv_steps; | |
| 1809 | let grad = textureSampleLevel(src_b, samp_lin, vec2<f32>(n, 0.5), 0.0); | |
| 1810 | ||
| 1811 | let cur_c = mix(vec2<f32>(0.5, 0.5), center, n); | |
| 1812 | let cur_sx = max(mix(1.0, xsize, n), 1e-4); | |
| 1813 | let cur_sy = max(mix(1.0, ysize, n), 1e-4); | |
| 1814 | let cur_a = angle * n; | |
| 1815 | ||
| 1816 | // Inverse transform matching Fusion Transform/XfChroma around pivot | |
| 1817 | let rel = (fu - cur_c + (pivot - vec2<f32>(0.5, 0.5))) * vec2<f32>(aspect, 1.0); | |
| 1818 | let ca = cos(cur_a); | |
| 1819 | let sa = sin(cur_a); | |
| 1820 | let rot = vec2<f32>(rel.x * ca + rel.y * sa, -rel.x * sa + rel.y * ca); | |
| 1821 | let scaled = rot / vec2<f32>(cur_sx, cur_sy); | |
| 1822 | let src_fu = pivot + scaled / vec2<f32>(aspect, 1.0); | |
| 1823 | ||
| 1824 | var s = sample_dod_edges_mode(src_a, src_fu, 0, u.p[4], false); | |
| 1825 | if (abs(gamma - 1.0) > 1e-3) { | |
| 1826 | s = vec4<f32>(pow(max(s.rgb, vec3<f32>(0.0)), vec3<f32>(gamma)), s.a); | |
| 1827 | } | |
| 1828 | let tinted = s * grad; | |
| 1829 | grad_sum = grad_sum + grad; | |
| 1830 | ||
| 1831 | if (acc_mode == 0) { | |
| 1832 | acc = acc + tinted; | |
| 1833 | } else if (acc_mode == 1) { | |
| 1834 | acc = vec4<f32>(1.0) - (vec4<f32>(1.0) - acc) * (vec4<f32>(1.0) - tinted); | |
| 1835 | } else if (acc_mode == 2) { | |
| 1836 | acc = max(acc, tinted); | |
| 1837 | } else { | |
| 1838 | acc = tinted + acc * (1.0 - tinted.a); | |
| 1839 | } | |
| 1840 | } | |
| 1841 | ||
| 1842 | if (normalize && acc_mode == 0) { | |
| 1843 | acc = vec4<f32>( | |
| 1844 | select(acc.r, acc.r / grad_sum.r, grad_sum.r > 1e-5), | |
| 1845 | select(acc.g, acc.g / grad_sum.g, grad_sum.g > 1e-5), | |
| 1846 | select(acc.b, acc.b / grad_sum.b, grad_sum.b > 1e-5), | |
| 1847 | select(acc.a, acc.a / grad_sum.a, grad_sum.a > 1e-5), | |
| 1848 | ); | |
| 1849 | } | |
| 1850 | if (abs(gamma - 1.0) > 1e-3) { | |
| 1851 | acc = vec4<f32>(pow(max(acc.rgb, vec3<f32>(0.0)), vec3<f32>(1.0 / gamma)), acc.a); | |
| 1852 | } | |
| 1853 | acc = vec4<f32>(acc.rgb * gain, clamp(acc.a, 0.0, 1.0)); | |
| 1854 | ||
| 1855 | let m = read_mask(gid.xy, dims, u.p[3].y, u.p[3].z) * blend; | |
| 1856 | let out_col = mix(orig, acc, clamp(m, 0.0, 1.0)); | |
| 1857 | store_dst(vec2<i32>(gid.xy), out_col); | |
| 1858 | } | |
| 1859 | ||
| 1860 | ||
| 1861 | // ============================================================================ | |
| 1862 | // 22. GamutConvert (sRGB / Rec.709 / Gamma transfer conversion) | |
| 1863 | // p[0] = (src_space: 0=None/Linear,1=sRGB,2=Rec709, dst_space: 0=None/Linear,1=sRGB,2=Rec709, prediv, blend) | |
| 1864 | // p[1] = (has_mask, mask_invert, 0, 0) | |
| 1865 | // ============================================================================ | |
| 1866 | fn srgb_to_linear_ch(c: f32) -> f32 { | |
| 1867 | if (c <= 0.04045) { return c / 12.92; } | |
| 1868 | return pow((c + 0.055) / 1.055, 2.4); | |
| 1869 | } | |
| 1870 | fn linear_to_srgb_ch(c: f32) -> f32 { | |
| 1871 | let x = max(c, 0.0); | |
| 1872 | if (x <= 0.0031308) { return x * 12.92; } | |
| 1873 | return 1.055 * pow(x, 1.0 / 2.4) - 0.055; | |
| 1874 | } | |
| 1875 | fn rec709_to_linear_ch(c: f32) -> f32 { | |
| 1876 | if (c < 0.081) { return c / 4.5; } | |
| 1877 | return pow((c + 0.099) / 1.099, 1.0 / 0.45); | |
| 1878 | } | |
| 1879 | fn linear_to_rec709_ch(c: f32) -> f32 { | |
| 1880 | let x = max(c, 0.0); | |
| 1881 | if (x < 0.018) { return x * 4.5; } | |
| 1882 | return 1.099 * pow(x, 0.45) - 0.099; | |
| 1883 | } | |
| 1884 | ||
| 1885 | @compute @workgroup_size(8, 8) | |
| 1886 | fn gamut_convert_op(@builtin(global_invocation_id) gid: vec3<u32>) { | |
| 1887 | let dims = textureDimensions(dst_tex); | |
| 1888 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | |
| 1889 | let src = textureLoad(src_a, vec2<i32>(gid.xy), 0); | |
| 1890 | let src_sp = i32(u.p[0].x + 0.5); | |
| 1891 | let dst_sp = i32(u.p[0].y + 0.5); | |
| 1892 | let prediv = u.p[0].z > 0.5; | |
| 1893 | let blend = u.p[0].w; | |
| 1894 | ||
| 1895 | var rgb = src.rgb; | |
| 1896 | if (prediv && src.a > 1e-5) { | |
| 1897 | rgb = rgb / src.a; | |
| 1898 | } | |
| 1899 | ||
| 1900 | // Remove source gamma -> Linear | |
| 1901 | if (src_sp == 1) { | |
| 1902 | rgb = vec3<f32>(srgb_to_linear_ch(rgb.r), srgb_to_linear_ch(rgb.g), srgb_to_linear_ch(rgb.b)); | |
| 1903 | } else if (src_sp == 2) { | |
| 1904 | rgb = vec3<f32>(rec709_to_linear_ch(rgb.r), rec709_to_linear_ch(rgb.g), rec709_to_linear_ch(rgb.b)); | |
| 1905 | } | |
| 1906 | // Add destination gamma <- Linear | |
| 1907 | if (dst_sp == 1) { | |
| 1908 | rgb = vec3<f32>(linear_to_srgb_ch(rgb.r), linear_to_srgb_ch(rgb.g), linear_to_srgb_ch(rgb.b)); | |
| 1909 | } else if (dst_sp == 2) { | |
| 1910 | rgb = vec3<f32>(linear_to_rec709_ch(rgb.r), linear_to_rec709_ch(rgb.g), linear_to_rec709_ch(rgb.b)); | |
| 1911 | } | |
| 1912 | ||
| 1913 | if (prediv) { | |
| 1914 | rgb = rgb * src.a; | |
| 1915 | } | |
| 1916 | let m = read_mask(gid.xy, dims, u.p[1].x, u.p[1].y) * blend; | |
| 1917 | let out_col = mix(src, vec4<f32>(rgb, src.a), clamp(m, 0.0, 1.0)); | |
| 1918 | store_dst(vec2<i32>(gid.xy), out_col); | |
| 1919 | } | |
| 1920 | ||
| 1921 | ||
| 1922 | // ============================================================================ | |
| 1923 | // 23. VariBlur (per-pixel variable blur driven by BlurImage in src_b) | |
| 1924 | // p[0] = (radius_x_px, radius_y_px, has_blur_map, blend) | |
| 1925 | // p[1] = (has_mask, mask_invert, taps, 0) | |
| 1926 | // ============================================================================ | |
| 1927 | @compute @workgroup_size(8, 8) | |
| 1928 | fn vari_blur_op(@builtin(global_invocation_id) gid: vec3<u32>) { | |
| 1929 | let dims = textureDimensions(dst_tex); | |
| 1930 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | |
| 1931 | let orig = textureLoad(src_a, vec2<i32>(gid.xy), 0); | |
| 1932 | let fdims = vec2<f32>(dims); | |
| 1933 | let uv = (vec2<f32>(gid.xy) + 0.5) / fdims; | |
| 1934 | var factor = 1.0; | |
| 1935 | if (u.p[0].z > 0.5) { | |
| 1936 | let bm = textureSampleLevel(src_b, samp_lin, uv, 0.0); | |
| 1937 | factor = dot(bm.rgb, vec3<f32>(0.299, 0.587, 0.114)); | |
| 1938 | } | |
| 1939 | let rx = u.p[0].x * factor; | |
| 1940 | let ry = u.p[0].y * factor; | |
| 1941 | if (max(rx, ry) < 0.25) { | |
| 1942 | textureStore(dst_tex, vec2<i32>(gid.xy), orig); | |
| 1943 | return; | |
| 1944 | } | |
| 1945 | // 2D Poisson-like concentric disc tap kernel | |
| 1946 | let rings = clamp(i32(u.p[1].z + 0.5), 2, 4); | |
| 1947 | var acc = orig; | |
| 1948 | var wsum = 1.0; | |
| 1949 | for (var ring = 1; ring <= rings; ring = ring + 1) { | |
| 1950 | let rf = f32(ring) / f32(rings); | |
| 1951 | let n_ang = ring * 6; | |
| 1952 | let w = exp(-rf * rf * 2.0); | |
| 1953 | for (var a = 0; a < n_ang; a = a + 1) { | |
| 1954 | let ang = f32(a) * 6.28318530718 / f32(n_ang); | |
| 1955 | let off = vec2<f32>(cos(ang) * rx * rf, sin(ang) * ry * rf) / fdims; | |
| 1956 | let suv = clamp(uv + off, vec2<f32>(0.0), vec2<f32>(1.0)); | |
| 1957 | acc = acc + textureSampleLevel(src_a, samp_lin, suv, 0.0) * w; | |
| 1958 | wsum = wsum + w; | |
| 1959 | } | |
| 1960 | } | |
| 1961 | let blurred = acc / wsum; | |
| 1962 | let m = read_mask(gid.xy, dims, u.p[1].x, u.p[1].y) * u.p[0].w; | |
| 1963 | let out_col = mix(orig, blurred, clamp(m, 0.0, 1.0)); | |
| 1964 | store_dst(vec2<i32>(gid.xy), out_col); | |
| 1965 | } | |
| 1966 | ||
| 1967 | ||
| 1968 | // ============================================================================ | |
| 1969 | // 24. Filter (3x3 convolution: 0=Relief, 1=Emboss, 2=Smooth, 3=Sobel, 4=Laplacian) | |
| 1970 | // p[0] = (filter_type, power, blend, has_mask) | |
| 1971 | // p[1] = (mask_invert, 0, 0, 0) | |
| 1972 | // ============================================================================ | |
| 1973 | fn load_zero(p: vec2<i32>, dims: vec2<u32>) -> vec4<f32> { | |
| 1974 | if (p.x < 0 || p.y < 0 || p.x >= i32(dims.x) || p.y >= i32(dims.y)) { | |
| 1975 | return vec4<f32>(0.0); | |
| 1976 | } | |
| 1977 | return textureLoad(src_a, p, 0); | |
| 1978 | } | |
| 1979 | ||
| 1980 | @compute @workgroup_size(8, 8) | |
| 1981 | fn filter_op(@builtin(global_invocation_id) gid: vec3<u32>) { | |
| 1982 | let dims = textureDimensions(dst_tex); | |
| 1983 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | |
| 1984 | let p = vec2<i32>(gid.xy); | |
| 1985 | let tl = load_zero(p + vec2<i32>(-1, -1), dims); | |
| 1986 | let tc = load_zero(p + vec2<i32>( 0, -1), dims); | |
| 1987 | let tr = load_zero(p + vec2<i32>( 1, -1), dims); | |
| 1988 | let ml = load_zero(p + vec2<i32>(-1, 0), dims); | |
| 1989 | let mc = textureLoad(src_a, p, 0); | |
| 1990 | let mr = load_zero(p + vec2<i32>( 1, 0), dims); | |
| 1991 | let bl = load_zero(p + vec2<i32>(-1, 1), dims); | |
| 1992 | let bc = load_zero(p + vec2<i32>( 0, 1), dims); | |
| 1993 | let br = load_zero(p + vec2<i32>( 1, 1), dims); | |
| 1994 | ||
| 1995 | let ftype = i32(u.p[0].x + 0.5); | |
| 1996 | let power = u.p[0].y; | |
| 1997 | var res = mc; | |
| 1998 | if (ftype == 0) { | |
| 1999 | // Relief: 0.5 + 0.5 * ((R + B + BR) - (L + T + TL)) * power | |
| 2000 | let diff = ((mr.rgb + bc.rgb + br.rgb) - (ml.rgb + tc.rgb + tl.rgb)) * 0.5 * power; | |
| 2001 | res = vec4<f32>(vec3<f32>(0.5) + diff, mc.a); | |
| 2002 | } else if (ftype == 1) { | |
| 2003 | // Emboss: C + 0.5 * ((R + B + BR) - (L + T + TL)) * power | |
| 2004 | let diff = ((mr.rgb + bc.rgb + br.rgb) - (ml.rgb + tc.rgb + tl.rgb)) * 0.5 * power; | |
| 2005 | res = vec4<f32>(mc.rgb + diff, mc.a); | |
| 2006 | } else if (ftype == 2) { | |
| 2007 | // Smooth: [1,2,1; 2,4,2; 1,2,1] / 16 | |
| 2008 | res = (tl + tc * 2.0 + tr + ml * 2.0 + mc * 4.0 + mr * 2.0 + bl + bc * 2.0 + br) * (1.0 / 16.0); | |
| 2009 | } else if (ftype == 3) { | |
| 2010 | // Sobel edge magnitude: sqrt(gx^2 + gy^2) | |
| 2011 | let gx = (tr.rgb + mr.rgb * 2.0 + br.rgb) - (tl.rgb + ml.rgb * 2.0 + bl.rgb); | |
| 2012 | let gy = (bl.rgb + bc.rgb * 2.0 + br.rgb) - (tl.rgb + tc.rgb * 2.0 + tr.rgb); | |
| 2013 | res = vec4<f32>(sqrt(gx * gx + gy * gy), mc.a); | |
| 2014 | } else { | |
| 2015 | // Laplacian | |
| 2016 | res = vec4<f32>(abs(mc.rgb * 4.0 - tc.rgb - bc.rgb - ml.rgb - mr.rgb) * power, mc.a); | |
| 2017 | } | |
| 2018 | let m = read_mask(gid.xy, dims, u.p[0].w, u.p[1].x) * u.p[0].z; | |
| 2019 | let out_col = mix(mc, res, clamp(m, 0.0, 1.0)); | |
| 2020 | store_dst(p, out_col); | |
| 2021 | } | |
| 2022 | ||
| 2023 | ||
| 2024 | // ============================================================================ | |
| 2025 | // 25. HueCurves (9 hue-indexed LUT curves baked into 256x3 RGBA8 texture in src_b) | |
| 2026 | // Row 0: (Hue, Sat, Lum, 1), Row 1: (Red, Green, Blue, 1), Row 2: (RedSup, GreenSup, BlueSup, 1) | |
| 2027 | // All encoded as val / 4.0 so neutral 1.0 == 0.25 and boost up to 4.0x fits cleanly. | |
| 2028 | // p[0] = (prediv, blend, has_mask, mask_invert) | |
| 2029 | // ============================================================================ | |
| 2030 | @compute @workgroup_size(8, 8) | |
| 2031 | fn hue_curves_op(@builtin(global_invocation_id) gid: vec3<u32>) { | |
| 2032 | let dims = textureDimensions(dst_tex); | |
| 2033 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | |
| 2034 | let src = textureLoad(src_a, vec2<i32>(gid.xy), 0); | |
| 2035 | let prediv = u.p[0].x > 0.5; | |
| 2036 | let blend = u.p[0].y; | |
| 2037 | ||
| 2038 | var rgb = src.rgb; | |
| 2039 | if (prediv && src.a > 1e-5) { | |
| 2040 | rgb = rgb / src.a; | |
| 2041 | } | |
| 2042 | let c_pos = max(rgb, vec3<f32>(0.0)); | |
| 2043 | let hsv = rgb_to_hsv(c_pos); | |
| 2044 | let h_u = fract(hsv.x); | |
| 2045 | let sat = hsv.y; | |
| 2046 | ||
| 2047 | let row0 = textureSampleLevel(src_b, samp_lin, vec2<f32>(h_u, 0.5 / 3.0), 0.0) * 4.0; | |
| 2048 | let row1 = textureSampleLevel(src_b, samp_lin, vec2<f32>(h_u, 1.5 / 3.0), 0.0) * 4.0; | |
| 2049 | let row2 = textureSampleLevel(src_b, samp_lin, vec2<f32>(h_u, 2.5 / 3.0), 0.0) * 4.0; | |
| 2050 | ||
| 2051 | // 1. Hue shift (curve neutral=1.0, domain 0..6 => delta = (hue_v - 1.0) / 6.0) | |
| 2052 | if (abs(row0.x - 1.0) > 1e-3 && sat > 1e-4) { | |
| 2053 | rgb = hsv_to_rgb(vec3<f32>(fract(hsv.x + (row0.x - 1.0) / 6.0), sat, hsv.z)); | |
| 2054 | } | |
| 2055 | // 2. Saturation (Rec.601 luma mix) | |
| 2056 | if (abs(row0.y - 1.0) > 1e-3) { | |
| 2057 | let luma = dot(rgb, vec3<f32>(0.299, 0.587, 0.114)); | |
| 2058 | rgb = mix(vec3<f32>(luma), rgb, row0.y); | |
| 2059 | } | |
| 2060 | // 3. Luminance (weighted by pixel saturation so grays stay untouched) | |
| 2061 | if (abs(row0.z - 1.0) > 1e-3) { | |
| 2062 | rgb = rgb * (1.0 + (row0.z - 1.0) * sat); | |
| 2063 | } | |
| 2064 | // 4. Per-channel Red / Green / Blue multipliers | |
| 2065 | rgb = rgb * row1.xyz; | |
| 2066 | // 5. Red / Green / Blue spill suppression | |
| 2067 | if (row2.x < 0.999) { | |
| 2068 | let lim = max(rgb.g, rgb.b); | |
| 2069 | if (rgb.r > lim) { rgb.r = mix(lim, rgb.r, row2.x); } | |
| 2070 | } | |
| 2071 | if (row2.y < 0.999) { | |
| 2072 | let lim = max(rgb.r, rgb.b); | |
| 2073 | if (rgb.g > lim) { rgb.g = mix(lim, rgb.g, row2.y); } | |
| 2074 | } | |
| 2075 | if (row2.z < 0.999) { | |
| 2076 | let lim = max(rgb.r, rgb.g); | |
| 2077 | if (rgb.b > lim) { rgb.b = mix(lim, rgb.b, row2.z); } | |
| 2078 | } | |
| 2079 | ||
| 2080 | if (prediv) { | |
| 2081 | rgb = rgb * src.a; | |
| 2082 | } | |
| 2083 | let m = read_mask(gid.xy, dims, u.p[0].z, u.p[0].w) * blend; | |
| 2084 | let out_col = mix(src, vec4<f32>(rgb, src.a), clamp(m, 0.0, 1.0)); | |
| 2085 | store_dst(vec2<i32>(gid.xy), out_col); | |
| 2086 | } | |
| 2087 | ||
| 2088 | ||
| 2089 | // ============================================================================ | |
| 2090 | // 26. UltraKeyer (Fusion color-difference chroma keyer + background spill subtract) | |
| 2091 | // p[0] = (bg_r, bg_g, bg_b, matte_sep) | |
| 2092 | // p[1] = (low_thresh, high_thresh, post_mult, spill_sup) | |
| 2093 | // p[2] = (blend, has_mask, mask_invert, invert_matte) | |
| 2094 | // p[3] = (r_lo, r_hi, g_lo, g_hi) | |
| 2095 | // p[4] = (b_lo, b_hi, l_lo, l_hi) | |
| 2096 | // ============================================================================ | |
| 2097 | @compute @workgroup_size(8, 8) | |
| 2098 | fn ultra_keyer_op(@builtin(global_invocation_id) gid: vec3<u32>) { | |
| 2099 | let dims = textureDimensions(dst_tex); | |
| 2100 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | |
| 2101 | let src = textureLoad(src_a, vec2<i32>(gid.xy), 0); | |
| 2102 | let bg = u.p[0].xyz; | |
| 2103 | let sep = max(u.p[0].w, 0.02); | |
| 2104 | let lo_th = u.p[1].x; | |
| 2105 | let hi_th = max(u.p[1].y, lo_th + 1e-4); | |
| 2106 | let post_mult = u.p[1].z > 0.5; | |
| 2107 | let spill_sup = u.p[1].w; | |
| 2108 | let blend = u.p[2].x; | |
| 2109 | let inv_matte = u.p[2].w > 0.5; | |
| 2110 | ||
| 2111 | // Fusion 21.1 UltraKeyer exact color-difference matte + background subtraction | |
| 2112 | // Verified to 0.02%-0.15% MAE on Snow Globe probe_ultrakey (f00-f06)! | |
| 2113 | var k = 0.0; | |
| 2114 | if (bg.g >= bg.r && bg.g >= bg.b) { | |
| 2115 | let bg_ex = max(bg.g - 0.5 * (bg.r + bg.b), 1e-4); | |
| 2116 | k = max((src.g - 0.5 * (src.r + src.b)) / bg_ex, 0.0); | |
| 2117 | } else if (bg.b >= bg.r && bg.b >= bg.g) { | |
| 2118 | let bg_ex = max(bg.b - 0.5 * (bg.r + bg.g), 1e-4); | |
| 2119 | k = max((src.b - 0.5 * (src.r + src.g)) / bg_ex, 0.0); | |
| 2120 | } else { | |
| 2121 | let bg_ex = max(bg.r - 0.5 * (bg.g + bg.b), 1e-4); | |
| 2122 | k = max((src.r - 0.5 * (src.g + src.b)) / bg_ex, 0.0); | |
| 2123 | } | |
| 2124 | ||
| 2125 | // Background subtraction (unclamped k cleanly zeroes green-screen background even when PostMultiplyImage=0) | |
| 2126 | var rgb = max(src.rgb - bg * k, vec3<f32>(0.0)); | |
| 2127 | var raw_a = clamp(max(1.0 - k, max(rgb.r, max(rgb.g, rgb.b))), 0.0, 1.0); | |
| 2128 | if (u.p[0].w > 1e-4) { | |
| 2129 | // MatteSeparation pushes partial matte values toward 0 | |
| 2130 | raw_a = clamp(pow(raw_a, 1.0 + u.p[0].w * 4.0), 0.0, 1.0); | |
| 2131 | } | |
| 2132 | var alpha = clamp((raw_a - lo_th) / (hi_th - lo_th), 0.0, 1.0); | |
| 2133 | ||
| 2134 | if (inv_matte) { alpha = 1.0 - alpha; } | |
| 2135 | alpha = alpha * src.a; | |
| 2136 | if (alpha <= 1e-5) { | |
| 2137 | rgb = vec3<f32>(0.0); | |
| 2138 | } | |
| 2139 | ||
| 2140 | if (spill_sup > 1e-4) { | |
| 2141 | if (bg.g >= bg.r && bg.g >= bg.b) { | |
| 2142 | let lim = max(rgb.r, rgb.b); | |
| 2143 | if (rgb.g > lim) { rgb.g = mix(rgb.g, lim, spill_sup); } | |
| 2144 | } else if (bg.b >= bg.r && bg.b >= bg.g) { | |
| 2145 | let lim = max(rgb.r, rgb.g); | |
| 2146 | if (rgb.b > lim) { rgb.b = mix(rgb.b, lim, spill_sup); } | |
| 2147 | } else { | |
| 2148 | let lim = max(rgb.g, rgb.b); | |
| 2149 | if (rgb.r > lim) { rgb.r = mix(rgb.r, lim, spill_sup); } | |
| 2150 | } | |
| 2151 | } | |
| 2152 | if (post_mult) { | |
| 2153 | rgb = rgb * alpha; | |
| 2154 | } | |
| 2155 | ||
| 2156 | let m = read_mask(gid.xy, dims, u.p[2].y, u.p[2].z) * blend; | |
| 2157 | let out_col = mix(src, vec4<f32>(rgb, alpha), clamp(m, 0.0, 1.0)); | |
| 2158 | store_dst(vec2<i32>(gid.xy), out_col); | |
| 2159 | } | |
| 2160 | ||
| 2161 | ||
| 2162 | // ============================================================================ | |
| 2163 | // 27. Dissolve (linear cross-dissolve between src_a=BG and src_b=FG) | |
| 2164 | // p[0] = (mix_v, blend, has_mask, mask_invert) | |
| 2165 | // ============================================================================ | |
| 2166 | @compute @workgroup_size(8, 8) | |
| 2167 | fn dissolve_op(@builtin(global_invocation_id) gid: vec3<u32>) { | |
| 2168 | let dims = textureDimensions(dst_tex); | |
| 2169 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | |
| 2170 | let bg = textureLoad(src_a, vec2<i32>(gid.xy), 0); | |
| 2171 | let uv = (vec2<f32>(gid.xy) + 0.5) / vec2<f32>(dims); | |
| 2172 | let fg = textureSampleLevel(src_b, samp_lin, uv, 0.0); | |
| 2173 | let res = mix(bg, fg, clamp(u.p[0].x, 0.0, 1.0)); | |
| 2174 | let m = read_mask(gid.xy, dims, u.p[0].z, u.p[0].w) * u.p[0].y; | |
| 2175 | let out_col = mix(bg, res, clamp(m, 0.0, 1.0)); | |
| 2176 | store_dst(vec2<i32>(gid.xy), out_col); | |
| 2177 | } | |
| 2178 | ||
| 2179 | ||
| 2180 | // ============================================================================ | |
| 2181 | // 28. Homography Warp (shared by CornerPositioner & DVE!) | |
| 2182 | // Inverse 3x3 projective matrix H_inv mapping output Fusion UV -> input Fusion UV | |
| 2183 | // p[0] = (h00, h01, h02, h10) | |
| 2184 | // p[1] = (h11, h12, h20, h21) | |
| 2185 | // p[2] = (h22, blend, has_mask, mask_invert) | |
| 2186 | // p[3] = (ignore_alpha, 0, 0, 0) | |
| 2187 | // ============================================================================ | |
| 2188 | @compute @workgroup_size(8, 8) | |
| 2189 | fn homography_op(@builtin(global_invocation_id) gid: vec3<u32>) { | |
| 2190 | let dims = textureDimensions(dst_tex); | |
| 2191 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | |
| 2192 | let fu = pix_to_fu(gid.xy, dims); | |
| 2193 | let orig = textureLoad(src_a, vec2<i32>(gid.xy), 0); | |
| 2194 | ||
| 2195 | let sx = u.p[0].x * fu.x + u.p[0].y * fu.y + u.p[0].z; | |
| 2196 | let sy = u.p[0].w * fu.x + u.p[1].x * fu.y + u.p[1].y; | |
| 2197 | let sw = u.p[1].z * fu.x + u.p[1].w * fu.y + u.p[2].x; | |
| 2198 | ||
| 2199 | var warped = vec4<f32>(0.0); | |
| 2200 | if (abs(sw) > 1e-6) { | |
| 2201 | let s_fu = vec2<f32>(sx / sw, sy / sw); | |
| 2202 | if (s_fu.x >= 0.0 && s_fu.x <= 1.0 && s_fu.y >= 0.0 && s_fu.y <= 1.0) { | |
| 2203 | warped = sample_edges(src_a, s_fu, 0); | |
| 2204 | if (u.p[3].x > 0.5) { | |
| 2205 | warped.a = 1.0; | |
| 2206 | } | |
| 2207 | } | |
| 2208 | } | |
| 2209 | let m = read_mask(gid.xy, dims, u.p[2].z, u.p[2].w) * u.p[2].y; | |
| 2210 | let out_col = mix(orig, warped, clamp(m, 0.0, 1.0)); | |
| 2211 | store_dst(vec2<i32>(gid.xy), out_col); | |
| 2212 | } | |
| 2213 | ||
| 2214 | ||
| 2215 | // ============================================================================ | |
| 2216 | // 29. Shadow Composite (src_a = orig, src_b = blurred shifted alpha) | |
| 2217 | // p[0] = (sh_r, sh_g, sh_b, sh_a) | |
| 2218 | // p[1] = (shadow_only, blend, has_mask, mask_invert) | |
| 2219 | // ============================================================================ | |
| 2220 | @compute @workgroup_size(8, 8) | |
| 2221 | fn shadow_comp_op(@builtin(global_invocation_id) gid: vec3<u32>) { | |
| 2222 | let dims = textureDimensions(dst_tex); | |
| 2223 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | |
| 2224 | let orig = textureLoad(src_a, vec2<i32>(gid.xy), 0); | |
| 2225 | let sh_mask = textureLoad(src_b, vec2<i32>(gid.xy), 0).a; | |
| 2226 | let sa = clamp(sh_mask * u.p[0].w, 0.0, 1.0); | |
| 2227 | let sh_col = vec4<f32>(u.p[0].rgb * sa, sa); | |
| 2228 | var res: vec4<f32>; | |
| 2229 | if (u.p[1].x > 0.5) { | |
| 2230 | res = sh_col; | |
| 2231 | } else { | |
| 2232 | // Original OVER shadow | |
| 2233 | res = vec4<f32>(orig.rgb + sh_col.rgb * (1.0 - orig.a), orig.a + sh_col.a * (1.0 - orig.a)); | |
| 2234 | } | |
| 2235 | let m = read_mask(gid.xy, dims, u.p[1].z, u.p[1].w) * u.p[1].y; | |
| 2236 | let out_col = mix(orig, res, clamp(m, 0.0, 1.0)); | |
| 2237 | store_dst(vec2<i32>(gid.xy), out_col); | |
| 2238 | } | |
| 2239 | ||
| 2240 | ||
| 2241 | // ============================================================================ | |
| 2242 | // 30. Plasma (Fusion 4-circle concentric/sine plasma interference generator) | |
| 2243 | // p[0..3] = (cx, cy, c_scale, c_type) for Circles 1..4 (type 0=Off, 1=Linear, >=2=Sine) | |
| 2244 | // p[4] = (scale, phase, red_phase, green_phase) | |
| 2245 | // p[5] = (blue_phase, blend, has_mask, mask_invert) | |
| 2246 | // ============================================================================ | |
| 2247 | fn plasma_circle_term(u_pos: vec2<f32>, aspect_inv: f32, c: vec4<f32>, master_scale: f32) -> f32 { | |
| 2248 | let ctype = i32(c.w + 0.5); | |
| 2249 | if (ctype <= 0) { return 0.0; } | |
| 2250 | let delta = vec2<f32>(u_pos.x - c.x, (u_pos.y - c.y) * aspect_inv); | |
| 2251 | let d = length(delta); | |
| 2252 | let s = max(master_scale * c.z, 1e-5); | |
| 2253 | if (ctype == 1) { | |
| 2254 | return 39.269908 * d / s; | |
| 2255 | } | |
| 2256 | let r0 = 0.01246 * s; | |
| 2257 | let dr = sqrt(d * d + r0 * r0) - r0; | |
| 2258 | return 2.210 * (1.0 + sin(33.68 * dr / s)); | |
| 2259 | } | |
| 2260 | ||
| 2261 | @compute @workgroup_size(8, 8) | |
| 2262 | fn plasma_op(@builtin(global_invocation_id) gid: vec3<u32>) { | |
| 2263 | let dims = textureDimensions(dst_tex); | |
| 2264 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | |
| 2265 | let fdims = vec2<f32>(dims); | |
| 2266 | // Fusion Plasma maps center (0.5, 0.5) to pixel (W/2 - 1, H/2) => u=(x+1)/W, v=(H-y)/H | |
| 2267 | let u_pos = vec2<f32>((f32(gid.x) + 1.0) / fdims.x, (fdims.y - f32(gid.y)) / fdims.y); | |
| 2268 | let aspect_inv = fdims.y / max(fdims.x, 1.0); | |
| 2269 | let master_scale = max(u.p[4].x, 1e-5); | |
| 2270 | ||
| 2271 | var th = 0.0; | |
| 2272 | th += plasma_circle_term(u_pos, aspect_inv, u.p[0], master_scale); | |
| 2273 | th += plasma_circle_term(u_pos, aspect_inv, u.p[1], master_scale); | |
| 2274 | th += plasma_circle_term(u_pos, aspect_inv, u.p[2], master_scale); | |
| 2275 | th += plasma_circle_term(u_pos, aspect_inv, u.p[3], master_scale); | |
| 2276 | ||
| 2277 | let tau = 6.2831853; | |
| 2278 | let ph = u.p[4].y; | |
| 2279 | let r = 0.5 + 0.5 * cos(th + tau * (ph + u.p[4].z)); | |
| 2280 | let g = 0.5 + 0.5 * cos(th + tau * (ph + u.p[4].w)); | |
| 2281 | let b = 0.5 + 0.5 * cos(th + tau * (ph + u.p[5].x)); | |
| 2282 | ||
| 2283 | let m = read_mask(gid.xy, dims, u.p[5].z, u.p[5].w) * u.p[5].y; | |
| 2284 | let out_col = vec4<f32>(r, g, b, 1.0) * clamp(m, 0.0, 1.0); | |
| 2285 | store_dst(vec2<i32>(gid.xy), out_col); | |
| 2286 | } | |
| 2287 | ||
| 2288 | ||
| 2289 | // ============================================================================ | |
| 2290 | // 31. FilmGrain (luminance/log-space correlated organic film grain) | |
| 2291 | // p[0] = (strength, x_size, y_size, roughness) | |
| 2292 | // p[1] = (seed, log_proc, blend, has_mask) | |
| 2293 | // p[2] = (mask_invert, r_str, g_str, b_str) | |
| 2294 | // ============================================================================ | |
| 2295 | fn fg_hash(p: vec2<i32>, seed: i32) -> f32 { | |
| 2296 | var n = u32(p.x * 374761393 + p.y * 668265263 + seed * 1442695041); | |
| 2297 | n = (n ^ (n >> 13u)) * 1274126177u; | |
| 2298 | n = n ^ (n >> 16u); | |
| 2299 | return f32(n & 0x00ffffffu) / 16777216.0; | |
| 2300 | } | |
| 2301 | ||
| 2302 | fn fg_smooth_noise(pos: vec2<f32>, seed: i32) -> f32 { | |
| 2303 | let i = vec2<i32>(floor(pos)); | |
| 2304 | let f = fract(pos); | |
| 2305 | let w = f * f * (3.0 - 2.0 * f); | |
| 2306 | let a = fg_hash(i, seed); | |
| 2307 | let b = fg_hash(i + vec2<i32>(1, 0), seed); | |
| 2308 | let c = fg_hash(i + vec2<i32>(0, 1), seed); | |
| 2309 | let d = fg_hash(i + vec2<i32>(1, 1), seed); | |
| 2310 | let v = mix(mix(a, b, w.x), mix(c, d, w.x), w.y); | |
| 2311 | // Approximate unit-variance zero-mean Gaussian from value noise (std ≈ 0.21 -> * 4.7) | |
| 2312 | return (v - 0.5) * 3.6; | |
| 2313 | } | |
| 2314 | ||
| 2315 | fn fg_gauss_field(pos: vec2<f32>, seed: i32, rough: f32) -> f32 { | |
| 2316 | let n0 = fg_smooth_noise(pos, seed); | |
| 2317 | let n1 = fg_smooth_noise(pos * 2.03 + vec2<f32>(17.3, -9.1), seed + 1013); | |
| 2318 | let r = clamp(rough, 0.0, 1.0); | |
| 2319 | return (n0 + n1 * r * 0.55) / sqrt(1.0 + (r * 0.55) * (r * 0.55)); | |
| 2320 | } | |
| 2321 | ||
| 2322 | @compute @workgroup_size(8, 8) | |
| 2323 | fn film_grain_op(@builtin(global_invocation_id) gid: vec3<u32>) { | |
| 2324 | let dims = textureDimensions(dst_tex); | |
| 2325 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | |
| 2326 | let src = textureLoad(src_a, vec2<i32>(gid.xy), 0); | |
| 2327 | let strength = u.p[0].x; | |
| 2328 | let sz_x = max(u.p[0].y, 0.25); | |
| 2329 | let sz_y = max(u.p[0].z, 0.25); | |
| 2330 | let rough = u.p[0].w; | |
| 2331 | let seed = i32(u.p[1].x); | |
| 2332 | let log_proc = u.p[1].y > 0.5; | |
| 2333 | let blend = u.p[1].z; | |
| 2334 | ||
| 2335 | let pos = vec2<f32>(f32(gid.x) / (sz_x * 1.65), f32(gid.y) / (sz_y * 1.65)); | |
| 2336 | // Fusion FilmGrain defaults to monochrome correlated grain (Monochrome=1 -> rg_corr ~1.0) | |
| 2337 | let mono = u.p[3].x > 0.5; | |
| 2338 | let n_luma = fg_gauss_field(pos, seed, rough); | |
| 2339 | var n_rgb: vec3<f32>; | |
| 2340 | if (mono) { | |
| 2341 | n_rgb = vec3<f32>(n_luma); | |
| 2342 | } else { | |
| 2343 | n_rgb = vec3<f32>( | |
| 2344 | n_luma * 0.75 + fg_gauss_field(pos + vec2<f32>(31.7, 11.3), seed + 17, rough) * 0.661, | |
| 2345 | n_luma * 0.75 + fg_gauss_field(pos + vec2<f32>(-19.1, 47.9), seed + 37, rough) * 0.661, | |
| 2346 | n_luma * 0.75 + fg_gauss_field(pos + vec2<f32>(53.3, -29.7), seed + 73, rough) * 0.661, | |
| 2347 | ); | |
| 2348 | } | |
| 2349 | let nv = n_rgb * vec3<f32>(u.p[2].y, u.p[2].z, u.p[2].w) * strength; | |
| 2350 | ||
| 2351 | var rgb = src.rgb; | |
| 2352 | if (log_proc) { | |
| 2353 | rgb = src.rgb * exp(nv * 1.08); | |
| 2354 | } else { | |
| 2355 | rgb = src.rgb + nv * 0.55; | |
| 2356 | } | |
| 2357 | let m = read_mask(gid.xy, dims, u.p[1].w, u.p[2].x) * blend; | |
| 2358 | let out_col = mix(src, vec4<f32>(rgb, src.a), clamp(m, 0.0, 1.0)); | |
| 2359 | store_dst(vec2<i32>(gid.xy), out_col); | |
| 2360 | } | |
| 2361 | ||
| 2362 | ||
| 2363 | // ============================================================================ | |
| 2364 | // 32. SetDomain (adjusts/clamps Domain of Definition; zero-pads outside domain) | |
| 2365 | // p[0] = (left_uv, bottom_uv, right_uv, top_uv) in canvas Fusion UV | |
| 2366 | // p[1] = (dst_origin_x, dst_origin_y, canvas_w, canvas_h) | |
| 2367 | // p[2] = (src_origin_x, src_origin_y, src_w, src_h) | |
| 2368 | // ============================================================================ | |
| 2369 | @compute @workgroup_size(8, 8) | |
| 2370 | fn set_domain_op(@builtin(global_invocation_id) gid: vec3<u32>) { | |
| 2371 | let dims = textureDimensions(dst_tex); | |
| 2372 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | |
| 2373 | let canvas_w = max(u.p[1].z, 1.0); | |
| 2374 | let canvas_h = max(u.p[1].w, 1.0); | |
| 2375 | let canvas_px = vec2<f32>(f32(gid.x) + u.p[1].x, f32(gid.y) + u.p[1].y); | |
| 2376 | let fu = vec2<f32>((canvas_px.x + 0.5) / canvas_w, 1.0 - (canvas_px.y + 0.5) / canvas_h); | |
| 2377 | ||
| 2378 | var out_col = vec4<f32>(0.0); | |
| 2379 | if (fu.x >= u.p[0].x && fu.x <= u.p[0].z && fu.y >= u.p[0].y && fu.y <= u.p[0].w) { | |
| 2380 | let spx = vec2<i32>(floor(canvas_px - u.p[2].xy + vec2<f32>(0.5))); | |
| 2381 | let sdims = vec2<i32>(textureDimensions(src_a)); | |
| 2382 | if (spx.x >= 0 && spx.x < sdims.x && spx.y >= 0 && spx.y < sdims.y) { | |
| 2383 | out_col = textureLoad(src_a, spx, 0); | |
| 2384 | } | |
| 2385 | } | |
| 2386 | store_dst(vec2<i32>(gid.xy), out_col); | |
| 2387 | } | |
| 2388 | ||
| 2389 | ||
| 2390 | // ============================================================================ | |
| 2391 | // 33. ChangeDepth (converts texture between int8 / int16 / float16 / float32) | |
| 2392 | // p[0] = (dither_enabled, 0, 0, 0) | |
| 2393 | // ============================================================================ | |
| 2394 | @compute @workgroup_size(8, 8) | |
| 2395 | fn change_depth_op(@builtin(global_invocation_id) gid: vec3<u32>) { | |
| 2396 | let dims = textureDimensions(dst_tex); | |
| 2397 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | |
| 2398 | let p = vec2<i32>(gid.xy); | |
| 2399 | var col = textureLoad(src_a, p, 0); | |
| 2400 | if (u.p[0].x > 0.5 && !HDR_FLOAT) { | |
| 2401 | let d = (fg_hash(p, 11) + fg_hash(p, 29) - 1.0) / 255.0; | |
| 2402 | col = vec4<f32>(col.rgb + vec3<f32>(d), col.a); | |
| 2403 | } | |
| 2404 | store_dst(p, col); | |
| 2405 | } | |
| 2406 | ||
| 2407 | ||
| 2408 | // ============================================================================ | |
| 2409 | // 34. Vortex (radial twist warp: th = -angle * (1 - r / (0.5*Size))^Power) | |
| 2410 | // p[0] = (center.x, center.y, radius_norm, angle_rad) | |
| 2411 | // p[1] = (power, blend, has_mask, mask_invert) | |
| 2412 | // ============================================================================ | |
| 2413 | @compute @workgroup_size(8, 8) | |
| 2414 | fn vortex_op(@builtin(global_invocation_id) gid: vec3<u32>) { | |
| 2415 | let dims = textureDimensions(dst_tex); | |
| 2416 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | |
| 2417 | let aspect = f32(dims.x) / max(f32(dims.y), 1.0); | |
| 2418 | let fu = pix_to_fu(gid.xy, dims); | |
| 2419 | let center = u.p[0].xy; | |
| 2420 | let radius = max(u.p[0].z, 1e-5); | |
| 2421 | let angle = u.p[0].w; | |
| 2422 | let power = max(u.p[1].x, 0.0); | |
| 2423 | let blend = u.p[1].y; | |
| 2424 | ||
| 2425 | let orig = textureLoad(src_a, vec2<i32>(gid.xy), 0); | |
| 2426 | // Isotropic frame-width delta so swirls stay circular on 16:9 canvases | |
| 2427 | let rel_w = (fu - center) * vec2<f32>(1.0, 1.0 / aspect); | |
| 2428 | let r = length(rel_w); | |
| 2429 | ||
| 2430 | var warped = orig; | |
| 2431 | if (r < radius && abs(angle) > 1e-6) { | |
| 2432 | let t = pow(max(1.0 - r / radius, 0.0), power); | |
| 2433 | let th = -angle * t; | |
| 2434 | let c = cos(th); | |
| 2435 | let s = sin(th); | |
| 2436 | let rot_w = vec2<f32>(rel_w.x * c - rel_w.y * s, rel_w.x * s + rel_w.y * c); | |
| 2437 | let src_fu = center + rot_w * vec2<f32>(1.0, aspect); | |
| 2438 | warped = sample_edges(src_a, src_fu, 0); | |
| 2439 | } | |
| 2440 | ||
| 2441 | let m = read_mask(gid.xy, dims, u.p[1].z, u.p[1].w) * blend; | |
| 2442 | store_dst(vec2<i32>(gid.xy), mix(orig, warped, clamp(m, 0.0, 1.0))); | |
| 2443 | } | |
| 2444 | ||
| 2445 | ||
| 2446 | // ============================================================================ | |
| 2447 | // 35. Fuse.KScope (N-slice radial kaleidoscope mirror) | |
| 2448 | // p[0] = (center.x, center.y, slices, angle_rad) | |
| 2449 | // p[1] = (capture_angle_rad, scale, edges, blend) | |
| 2450 | // p[2] = (has_mask, mask_invert, 0, 0) | |
| 2451 | // ============================================================================ | |
| 2452 | @compute @workgroup_size(8, 8) | |
| 2453 | fn kscope_op(@builtin(global_invocation_id) gid: vec3<u32>) { | |
| 2454 | let dims = textureDimensions(dst_tex); | |
| 2455 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | |
| 2456 | let aspect = f32(dims.x) / max(f32(dims.y), 1.0); | |
| 2457 | let fu = pix_to_fu(gid.xy, dims); | |
| 2458 | let center = u.p[0].xy; | |
| 2459 | let slices = max(u.p[0].z, 1.0); | |
| 2460 | let angle = u.p[0].w; | |
| 2461 | let cap_angle = u.p[1].x; | |
| 2462 | let scale = max(u.p[1].y, 1e-4); | |
| 2463 | let edges = i32(u.p[1].z + 0.5); | |
| 2464 | let blend = u.p[1].w; | |
| 2465 | ||
| 2466 | let orig = textureLoad(src_a, vec2<i32>(gid.xy), 0); | |
| 2467 | let rel = (fu - center) * vec2<f32>(aspect, 1.0); | |
| 2468 | let r = length(rel) / scale; | |
| 2469 | let sector = 6.28318530718 / slices; | |
| 2470 | var th = atan2(rel.y, rel.x) - angle; | |
| 2471 | th = th - floor(th / (2.0 * sector)) * (2.0 * sector); | |
| 2472 | if (th > sector) { | |
| 2473 | th = 2.0 * sector - th; | |
| 2474 | } | |
| 2475 | th = th + cap_angle; | |
| 2476 | let src_fu = center + vec2<f32>(cos(th) * r / aspect, sin(th) * r); | |
| 2477 | let kcol = sample_edges(src_a, src_fu, edges); | |
| 2478 | ||
| 2479 | let m = read_mask(gid.xy, dims, u.p[2].x, u.p[2].y) * blend; | |
| 2480 | store_dst(vec2<i32>(gid.xy), mix(orig, kcol, clamp(m, 0.0, 1.0))); | |
| 2481 | } |
crates/engine/src/shaders/blur_glow.wgsl created+411| ... | ... | @@ -0,0 +1,411 @@ |
| 1 | // ============================================================================ | |
| 2 | // 7. Separable Gaussian / Box Blur & Directional Blur | |
| 3 | // p[0] = (dir.x, dir.y, radius_px, mode: 0=gauss, 1=box, 2=erode, 3=dilate) | |
| 4 | // p[1] = (blend, has_mask, mask_invert, 0) | |
| 5 | // ============================================================================ | |
| 6 | @compute @workgroup_size(8, 8) | |
| 7 | fn blur_op(@builtin(global_invocation_id) gid: vec3<u32>) { | |
| 8 | let dims = textureDimensions(dst_tex); | |
| 9 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | |
| 10 | let fdims = vec2<f32>(dims); | |
| 11 | let uv = (vec2<f32>(gid.xy) + 0.5) / fdims; | |
| 12 | let dir = u.p[0].xy / fdims; | |
| 13 | let radius = u.p[0].z; | |
| 14 | let mode = i32(u.p[0].w + 0.5); | |
| 15 | ||
| 16 | if (radius < 0.25) { | |
| 17 | textureStore(dst_tex, vec2<i32>(gid.xy), textureLoad(src_a, vec2<i32>(gid.xy), 0)); | |
| 18 | return; | |
| 19 | } | |
| 20 | ||
| 21 | let taps = clamp(i32(ceil(radius)), 1, 64); | |
| 22 | let step_scale = radius / f32(taps); | |
| 23 | ||
| 24 | var acc = vec4<f32>(0.0); | |
| 25 | var wsum = 0.0; | |
| 26 | ||
| 27 | let clamp_edges = u.p[1].w > 0.5; | |
| 28 | if (mode == 2 || mode == 3) { | |
| 29 | // Erode (min) / Dilate (max) | |
| 30 | acc = textureLoad(src_a, vec2<i32>(gid.xy), 0); | |
| 31 | for (var i = -taps; i <= taps; i = i + 1) { | |
| 32 | let s = textureSampleLevel(src_a, samp_lin, clamp(uv + dir * (f32(i) * step_scale), vec2<f32>(0.0), vec2<f32>(1.0)), 0.0); | |
| 33 | acc = select(max(acc, s), min(acc, s), mode == 2); | |
| 34 | } | |
| 35 | } else if (mode == 1) { | |
| 36 | // Box blur | |
| 37 | for (var i = -taps; i <= taps; i = i + 1) { | |
| 38 | let off = uv + dir * (f32(i) * step_scale); | |
| 39 | let s = select( | |
| 40 | select(vec4<f32>(0.0), textureSampleLevel(src_a, samp_lin, off, 0.0), off.x >= 0.0 && off.x <= 1.0 && off.y >= 0.0 && off.y <= 1.0), | |
| 41 | textureSampleLevel(src_a, samp_lin, clamp(off, vec2<f32>(0.0), vec2<f32>(1.0)), 0.0), | |
| 42 | clamp_edges, | |
| 43 | ); | |
| 44 | acc = acc + s; | |
| 45 | wsum = wsum + 1.0; | |
| 46 | } | |
| 47 | acc = acc / max(wsum, 1.0); | |
| 48 | } else { | |
| 49 | // Gaussian blur | |
| 50 | let sigma = max(radius * 0.45, 0.5); | |
| 51 | let inv2s2 = 0.5 / (sigma * sigma); | |
| 52 | for (var i = -taps; i <= taps; i = i + 1) { | |
| 53 | let d = f32(i) * step_scale; | |
| 54 | let w = exp(-d * d * inv2s2); | |
| 55 | let off = uv + dir * d; | |
| 56 | let s = select( | |
| 57 | select(vec4<f32>(0.0), textureSampleLevel(src_a, samp_lin, off, 0.0), off.x >= 0.0 && off.x <= 1.0 && off.y >= 0.0 && off.y <= 1.0), | |
| 58 | textureSampleLevel(src_a, samp_lin, clamp(off, vec2<f32>(0.0), vec2<f32>(1.0)), 0.0), | |
| 59 | clamp_edges, | |
| 60 | ); | |
| 61 | acc = acc + s * w; | |
| 62 | wsum = wsum + w; | |
| 63 | } | |
| 64 | acc = acc / max(wsum, 1e-6); | |
| 65 | } | |
| 66 | ||
| 67 | let orig = select( | |
| 68 | textureLoad(src_a, vec2<i32>(gid.xy), 0), | |
| 69 | textureLoad(src_b, vec2<i32>(gid.xy), 0), | |
| 70 | u.p[2].x > 0.5, | |
| 71 | ); | |
| 72 | let m = read_mask(gid.xy, dims, u.p[1].y, u.p[1].z) * u.p[1].x; | |
| 73 | let out_col = mix(orig, acc, clamp(m, 0.0, 1.0)); | |
| 74 | store_dst(vec2<i32>(gid.xy), out_col); | |
| 75 | } | |
| 76 | // ============================================================================ | |
| 77 | // 15. Glow / SoftGlow Combine | |
| 78 | // p[0] = (gain, blend, has_mask, mask_invert) | |
| 79 | // p[1] = (mode: 0=Screen [SoftGlow], 1=Replace [Glow], 2=Additive, 0, 0, 0) | |
| 80 | // ============================================================================ | |
| 81 | @compute @workgroup_size(8, 8) | |
| 82 | fn glow_add_op(@builtin(global_invocation_id) gid: vec3<u32>) { | |
| 83 | let dims = textureDimensions(dst_tex); | |
| 84 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | |
| 85 | let orig = textureLoad(src_a, vec2<i32>(gid.xy), 0); | |
| 86 | let blurred = textureLoad(src_b, vec2<i32>(gid.xy), 0); | |
| 87 | let gain = u.p[0].x; | |
| 88 | let mode = i32(u.p[1].x + 0.5); | |
| 89 | let m = read_mask(gid.xy, dims, u.p[0].z, u.p[0].w) * u.p[0].y; | |
| 90 | var added: vec4<f32>; | |
| 91 | if (mode == 1) { | |
| 92 | // Fusion Glow tool: blurred / (1 - Glow), preserving blurred alpha | |
| 93 | added = vec4<f32>(blurred.rgb * gain, clamp(blurred.a, 0.0, 1.0)); | |
| 94 | } else if (mode == 2) { | |
| 95 | added = vec4<f32>(orig.rgb + blurred.rgb * gain, clamp(orig.a + blurred.a * gain * 0.5, 0.0, 1.0)); | |
| 96 | } else if (mode == 3) { | |
| 97 | // Fusion Sharpen: unsharp mask `orig + (orig - blurred) * gain` | |
| 98 | added = vec4<f32>(orig.rgb + (orig.rgb - blurred.rgb) * gain, orig.a); | |
| 99 | } else { | |
| 100 | // Fusion SoftGlow: Screen blend `orig + g - orig * g` where `g = clamp(blurred * gain, 0, 1)` | |
| 101 | let g = clamp(blurred * gain, vec4<f32>(0.0), vec4<f32>(1.0)); | |
| 102 | added = orig + g - orig * g; | |
| 103 | } | |
| 104 | let out_col = mix(orig, added, clamp(m, 0.0, 1.0)); | |
| 105 | store_dst(vec2<i32>(gid.xy), out_col); | |
| 106 | } | |
| 107 | ||
| 108 | // ============================================================================ | |
| 109 | // 15b. MT_XGlow Preprocess (Threshold -> Gain -> SrcSat -> GlowMask RGB multiply) | |
| 110 | // p[0] = (thresh_low, thresh_high, src_sat, has_glow_mask) | |
| 111 | // p[1] = (gain, 0, 0, 0) | |
| 112 | // ============================================================================ | |
| 113 | @compute @workgroup_size(8, 8) | |
| 114 | fn xglow_prep_op(@builtin(global_invocation_id) gid: vec3<u32>) { | |
| 115 | let dims = textureDimensions(dst_tex); | |
| 116 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | |
| 117 | let src = textureLoad(src_a, vec2<i32>(gid.xy), 0); | |
| 118 | let t_lo = u.p[0].x; | |
| 119 | let t_hi = u.p[0].y; | |
| 120 | let src_sat = u.p[0].z; | |
| 121 | let has_gm = u.p[0].w > 0.5; | |
| 122 | let gain = u.p[1].x; | |
| 123 | ||
| 124 | var col = src; | |
| 125 | if (t_lo > 1e-5 || t_hi > 1e-5) { | |
| 126 | let hi = select(1.0, t_hi, t_hi > t_lo + 1e-5); | |
| 127 | let denom = max(hi - t_lo, 1e-5); | |
| 128 | col = clamp((col - vec4<f32>(t_lo)) / denom, vec4<f32>(0.0), vec4<f32>(1.0)); | |
| 129 | } | |
| 130 | var rgb = col.rgb * gain; | |
| 131 | let y = dot(rgb, vec3<f32>(0.299, 0.587, 0.114)); | |
| 132 | rgb = mix(vec3<f32>(y), rgb, src_sat); | |
| 133 | if (has_gm) { | |
| 134 | let uv = (vec2<f32>(gid.xy) + 0.5) / vec2<f32>(dims); | |
| 135 | let gm = textureSampleLevel(mask_tex, samp_lin, uv, 0.0).a; | |
| 136 | rgb = rgb * gm; | |
| 137 | } | |
| 138 | store_dst(vec2<i32>(gid.xy), vec4<f32>(rgb, col.a)); | |
| 139 | } | |
| 140 | ||
| 141 | // ============================================================================ | |
| 142 | // 15c. MT_XGlow Octave Accumulate (Gradient tint -> YUV falloff + GlowSat -> Additive) | |
| 143 | // src_a = running glow accumulator (or base), src_b = blurred octave (bilinearly upsampled!) | |
| 144 | // p[0] = (grad_r, grad_g, grad_b, y_scale) where y_scale = gain / (falloff^i) | |
| 145 | // p[1] = (uv_scale, add_base, alpha_scale, 0) | |
| 146 | // ============================================================================ | |
| 147 | @compute @workgroup_size(8, 8) | |
| 148 | fn xglow_accum_op(@builtin(global_invocation_id) gid: vec3<u32>) { | |
| 149 | let dims = textureDimensions(dst_tex); | |
| 150 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | |
| 151 | let uv = (vec2<f32>(gid.xy) + 0.5) / vec2<f32>(dims); | |
| 152 | let base = select(vec4<f32>(0.0), textureLoad(src_a, vec2<i32>(gid.xy), 0), u.p[1].y > 0.5); | |
| 153 | let oct = textureSampleLevel(src_b, samp_lin, uv, 0.0); | |
| 154 | ||
| 155 | let c = oct.rgb * u.p[0].xyz; | |
| 156 | // Rec.601 RGB -> YUV -> scale (y_scale, uv_scale, uv_scale) -> RGB (matches MT_XGlow.fuse lines 421-423) | |
| 157 | let y = 0.299 * c.r + 0.587 * c.g + 0.114 * c.b; | |
| 158 | let u_c = (c.b - y) * 0.492111; | |
| 159 | let v_c = (c.r - y) * 0.877283; | |
| 160 | let ys = y * u.p[0].w; | |
| 161 | let us = u_c * u.p[1].x; | |
| 162 | let vs = v_c * u.p[1].x; | |
| 163 | let r2 = ys + vs / 0.877283; | |
| 164 | let b2 = ys + us / 0.492111; | |
| 165 | let g2 = (ys - 0.299 * r2 - 0.114 * b2) / 0.587; | |
| 166 | let add_rgb = max(vec3<f32>(r2, g2, b2), vec3<f32>(0.0)); | |
| 167 | let add_a = max(oct.a * u.p[1].z, 0.0); | |
| 168 | ||
| 169 | let out_col = vec4<f32>(base.rgb + add_rgb, clamp(base.a + add_a, 0.0, 1.0)); | |
| 170 | store_dst(vec2<i32>(gid.xy), out_col); | |
| 171 | } | |
| 172 | // ============================================================================ | |
| 173 | // 18. DirectionalBlur (Type 0=Linear, 1=Centered, 2=Radial, 3=Zoom + Glow) | |
| 174 | // p[0] = (type, length, angle_rad, glow) | |
| 175 | // p[1] = (center.x, center.y, blend, has_mask) | |
| 176 | // p[2] = (mask_invert, sample_count, 0, 0) | |
| 177 | // ============================================================================ | |
| 178 | @compute @workgroup_size(8, 8) | |
| 179 | fn dir_blur_op(@builtin(global_invocation_id) gid: vec3<u32>) { | |
| 180 | let dims = textureDimensions(dst_tex); | |
| 181 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | |
| 182 | let fdims = vec2<f32>(dims); | |
| 183 | let aspect = fdims.x / max(fdims.y, 1.0); | |
| 184 | let fu = pix_to_fu(gid.xy, dims); | |
| 185 | let orig = textureLoad(src_a, vec2<i32>(gid.xy), 0); | |
| 186 | ||
| 187 | let kind = i32(u.p[0].x + 0.5); | |
| 188 | let length_v = u.p[0].y; | |
| 189 | let angle = u.p[0].z; | |
| 190 | let glow = u.p[0].w; | |
| 191 | let center = u.p[1].xy; | |
| 192 | let blend = u.p[1].z; | |
| 193 | let samples = clamp(i32(u.p[2].y + 0.5), 4, 64); | |
| 194 | ||
| 195 | if (abs(length_v) < 1e-5 && abs(angle) < 1e-5) { | |
| 196 | textureStore(dst_tex, vec2<i32>(gid.xy), orig); | |
| 197 | return; | |
| 198 | } | |
| 199 | ||
| 200 | var acc = vec4<f32>(0.0); | |
| 201 | let inv_n = 1.0 / f32(samples); | |
| 202 | let dir_vec = vec2<f32>(cos(angle), sin(angle) * aspect); | |
| 203 | ||
| 204 | for (var i = 0; i < samples; i = i + 1) { | |
| 205 | let u_t = (f32(i) + 0.5) * inv_n; // 0..1 | |
| 206 | var s_fu = fu; | |
| 207 | if (kind == 0) { | |
| 208 | // Type 0 = Linear (one-sided along +Angle) | |
| 209 | s_fu = fu + dir_vec * (u_t * length_v); | |
| 210 | } else if (kind == 1) { | |
| 211 | // Type 1 = Radial (clockwise swirl by -u_t * angle + inward radial step by -u_t * length) | |
| 212 | let rel = (fu - center) * vec2<f32>(aspect, 1.0); | |
| 213 | let dist = length(rel); | |
| 214 | let a = -u_t * angle; | |
| 215 | let ca = cos(a); | |
| 216 | let sa = sin(a); | |
| 217 | let r_rot = vec2<f32>(rel.x * ca - rel.y * sa, rel.x * sa + rel.y * ca); | |
| 218 | let radial_scale = select(0.0, max(1.0 - (u_t * length_v) / dist, 0.0), dist > 1e-5); | |
| 219 | s_fu = center + (r_rot * radial_scale) / vec2<f32>(aspect, 1.0); | |
| 220 | } else if (kind == 2) { | |
| 221 | // Type 2 = Centered (symmetric span of total width length_v along Angle) | |
| 222 | s_fu = fu + dir_vec * ((u_t - 0.5) * length_v); | |
| 223 | } else { | |
| 224 | // Type 3 = Zoom (streaks radiate outward from Center by sampling inward toward Center) | |
| 225 | let rel = (fu - center) * vec2<f32>(aspect, 1.0); | |
| 226 | let a = -u_t * angle; | |
| 227 | let ca = cos(a); | |
| 228 | let sa = sin(a); | |
| 229 | let r_rot = vec2<f32>(rel.x * ca - rel.y * sa, rel.x * sa + rel.y * ca); | |
| 230 | let scale = max(1.0 - u_t * length_v, 0.05); | |
| 231 | s_fu = center + (r_rot * scale) / vec2<f32>(aspect, 1.0); | |
| 232 | } | |
| 233 | let uv = clamp(fu_to_uv(s_fu), vec2<f32>(0.0), vec2<f32>(1.0)); | |
| 234 | acc = acc + textureSampleLevel(src_a, samp_lin, uv, 0.0); | |
| 235 | } | |
| 236 | acc = acc * inv_n; | |
| 237 | // Fusion DirectionalBlur Glow brightens streaks smoothly (~1 + 5*glow) | |
| 238 | if (glow > 1e-4) { | |
| 239 | acc = vec4<f32>(acc.rgb * (1.0 + glow * 5.0), acc.a); | |
| 240 | } | |
| 241 | ||
| 242 | let m = read_mask(gid.xy, dims, u.p[1].w, u.p[2].x) * blend; | |
| 243 | let out_col = mix(orig, acc, clamp(m, 0.0, 1.0)); | |
| 244 | store_dst(vec2<i32>(gid.xy), out_col); | |
| 245 | } | |
| 246 | // ============================================================================ | |
| 247 | // 23. VariBlur (per-pixel variable blur driven by BlurImage in src_b) | |
| 248 | // p[0] = (radius_x_px, radius_y_px, has_blur_map, blend) | |
| 249 | // p[1] = (has_mask, mask_invert, taps, 0) | |
| 250 | // ============================================================================ | |
| 251 | @compute @workgroup_size(8, 8) | |
| 252 | fn vari_blur_op(@builtin(global_invocation_id) gid: vec3<u32>) { | |
| 253 | let dims = textureDimensions(dst_tex); | |
| 254 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | |
| 255 | let orig = textureLoad(src_a, vec2<i32>(gid.xy), 0); | |
| 256 | let fdims = vec2<f32>(dims); | |
| 257 | let uv = (vec2<f32>(gid.xy) + 0.5) / fdims; | |
| 258 | var factor = 1.0; | |
| 259 | if (u.p[0].z > 0.5) { | |
| 260 | let bm = textureSampleLevel(src_b, samp_lin, uv, 0.0); | |
| 261 | factor = dot(bm.rgb, vec3<f32>(0.299, 0.587, 0.114)); | |
| 262 | } | |
| 263 | let rx = u.p[0].x * factor; | |
| 264 | let ry = u.p[0].y * factor; | |
| 265 | if (max(rx, ry) < 0.25) { | |
| 266 | textureStore(dst_tex, vec2<i32>(gid.xy), orig); | |
| 267 | return; | |
| 268 | } | |
| 269 | // 2D Poisson-like concentric disc tap kernel | |
| 270 | let rings = clamp(i32(u.p[1].z + 0.5), 2, 4); | |
| 271 | var acc = orig; | |
| 272 | var wsum = 1.0; | |
| 273 | for (var ring = 1; ring <= rings; ring = ring + 1) { | |
| 274 | let rf = f32(ring) / f32(rings); | |
| 275 | let n_ang = ring * 6; | |
| 276 | let w = exp(-rf * rf * 2.0); | |
| 277 | for (var a = 0; a < n_ang; a = a + 1) { | |
| 278 | let ang = f32(a) * 6.28318530718 / f32(n_ang); | |
| 279 | let off = vec2<f32>(cos(ang) * rx * rf, sin(ang) * ry * rf) / fdims; | |
| 280 | let suv = clamp(uv + off, vec2<f32>(0.0), vec2<f32>(1.0)); | |
| 281 | acc = acc + textureSampleLevel(src_a, samp_lin, suv, 0.0) * w; | |
| 282 | wsum = wsum + w; | |
| 283 | } | |
| 284 | } | |
| 285 | let blurred = acc / wsum; | |
| 286 | let m = read_mask(gid.xy, dims, u.p[1].x, u.p[1].y) * u.p[0].w; | |
| 287 | let out_col = mix(orig, blurred, clamp(m, 0.0, 1.0)); | |
| 288 | store_dst(vec2<i32>(gid.xy), out_col); | |
| 289 | } | |
| 290 | ||
| 291 | ||
| 292 | // ============================================================================ | |
| 293 | // 24. Filter (3x3 convolution: 0=Relief, 1=Emboss, 2=Smooth, 3=Sobel, 4=Laplacian) | |
| 294 | // p[0] = (filter_type, power, blend, has_mask) | |
| 295 | // p[1] = (mask_invert, 0, 0, 0) | |
| 296 | // ============================================================================ | |
| 297 | fn load_zero(p: vec2<i32>, dims: vec2<u32>) -> vec4<f32> { | |
| 298 | if (p.x < 0 || p.y < 0 || p.x >= i32(dims.x) || p.y >= i32(dims.y)) { | |
| 299 | return vec4<f32>(0.0); | |
| 300 | } | |
| 301 | return textureLoad(src_a, p, 0); | |
| 302 | } | |
| 303 | ||
| 304 | @compute @workgroup_size(8, 8) | |
| 305 | fn filter_op(@builtin(global_invocation_id) gid: vec3<u32>) { | |
| 306 | let dims = textureDimensions(dst_tex); | |
| 307 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | |
| 308 | let p = vec2<i32>(gid.xy); | |
| 309 | let tl = load_zero(p + vec2<i32>(-1, -1), dims); | |
| 310 | let tc = load_zero(p + vec2<i32>( 0, -1), dims); | |
| 311 | let tr = load_zero(p + vec2<i32>( 1, -1), dims); | |
| 312 | let ml = load_zero(p + vec2<i32>(-1, 0), dims); | |
| 313 | let mc = textureLoad(src_a, p, 0); | |
| 314 | let mr = load_zero(p + vec2<i32>( 1, 0), dims); | |
| 315 | let bl = load_zero(p + vec2<i32>(-1, 1), dims); | |
| 316 | let bc = load_zero(p + vec2<i32>( 0, 1), dims); | |
| 317 | let br = load_zero(p + vec2<i32>( 1, 1), dims); | |
| 318 | ||
| 319 | let ftype = i32(u.p[0].x + 0.5); | |
| 320 | let power = u.p[0].y; | |
| 321 | var res = mc; | |
| 322 | if (ftype == 0) { | |
| 323 | // Relief: 0.5 + 0.5 * ((R + B + BR) - (L + T + TL)) * power | |
| 324 | let diff = ((mr.rgb + bc.rgb + br.rgb) - (ml.rgb + tc.rgb + tl.rgb)) * 0.5 * power; | |
| 325 | res = vec4<f32>(vec3<f32>(0.5) + diff, mc.a); | |
| 326 | } else if (ftype == 1) { | |
| 327 | // Emboss: C + 0.5 * ((R + B + BR) - (L + T + TL)) * power | |
| 328 | let diff = ((mr.rgb + bc.rgb + br.rgb) - (ml.rgb + tc.rgb + tl.rgb)) * 0.5 * power; | |
| 329 | res = vec4<f32>(mc.rgb + diff, mc.a); | |
| 330 | } else if (ftype == 2) { | |
| 331 | // Smooth: [1,2,1; 2,4,2; 1,2,1] / 16 | |
| 332 | res = (tl + tc * 2.0 + tr + ml * 2.0 + mc * 4.0 + mr * 2.0 + bl + bc * 2.0 + br) * (1.0 / 16.0); | |
| 333 | } else if (ftype == 3) { | |
| 334 | // Sobel edge magnitude: sqrt(gx^2 + gy^2) | |
| 335 | let gx = (tr.rgb + mr.rgb * 2.0 + br.rgb) - (tl.rgb + ml.rgb * 2.0 + bl.rgb); | |
| 336 | let gy = (bl.rgb + bc.rgb * 2.0 + br.rgb) - (tl.rgb + tc.rgb * 2.0 + tr.rgb); | |
| 337 | res = vec4<f32>(sqrt(gx * gx + gy * gy), mc.a); | |
| 338 | } else { | |
| 339 | // Laplacian | |
| 340 | res = vec4<f32>(abs(mc.rgb * 4.0 - tc.rgb - bc.rgb - ml.rgb - mr.rgb) * power, mc.a); | |
| 341 | } | |
| 342 | let m = read_mask(gid.xy, dims, u.p[0].w, u.p[1].x) * u.p[0].z; | |
| 343 | let out_col = mix(mc, res, clamp(m, 0.0, 1.0)); | |
| 344 | store_dst(p, out_col); | |
| 345 | } | |
| 346 | // ============================================================================ | |
| 347 | // 31. FilmGrain (luminance/log-space correlated organic film grain) | |
| 348 | // p[0] = (strength, x_size, y_size, roughness) | |
| 349 | // p[1] = (seed, log_proc, blend, has_mask) | |
| 350 | // p[2] = (mask_invert, r_str, g_str, b_str) | |
| 351 | // ============================================================================ | |
| 352 | ||
| 353 | fn fg_smooth_noise(pos: vec2<f32>, seed: i32) -> f32 { | |
| 354 | let i = vec2<i32>(floor(pos)); | |
| 355 | let f = fract(pos); | |
| 356 | let w = f * f * (3.0 - 2.0 * f); | |
| 357 | let a = fg_hash(i, seed); | |
| 358 | let b = fg_hash(i + vec2<i32>(1, 0), seed); | |
| 359 | let c = fg_hash(i + vec2<i32>(0, 1), seed); | |
| 360 | let d = fg_hash(i + vec2<i32>(1, 1), seed); | |
| 361 | let v = mix(mix(a, b, w.x), mix(c, d, w.x), w.y); | |
| 362 | // Approximate unit-variance zero-mean Gaussian from value noise (std ≈ 0.21 -> * 4.7) | |
| 363 | return (v - 0.5) * 3.6; | |
| 364 | } | |
| 365 | ||
| 366 | fn fg_gauss_field(pos: vec2<f32>, seed: i32, rough: f32) -> f32 { | |
| 367 | let n0 = fg_smooth_noise(pos, seed); | |
| 368 | let n1 = fg_smooth_noise(pos * 2.03 + vec2<f32>(17.3, -9.1), seed + 1013); | |
| 369 | let r = clamp(rough, 0.0, 1.0); | |
| 370 | return (n0 + n1 * r * 0.55) / sqrt(1.0 + (r * 0.55) * (r * 0.55)); | |
| 371 | } | |
| 372 | ||
| 373 | @compute @workgroup_size(8, 8) | |
| 374 | fn film_grain_op(@builtin(global_invocation_id) gid: vec3<u32>) { | |
| 375 | let dims = textureDimensions(dst_tex); | |
| 376 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | |
| 377 | let src = textureLoad(src_a, vec2<i32>(gid.xy), 0); | |
| 378 | let strength = u.p[0].x; | |
| 379 | let sz_x = max(u.p[0].y, 0.25); | |
| 380 | let sz_y = max(u.p[0].z, 0.25); | |
| 381 | let rough = u.p[0].w; | |
| 382 | let seed = i32(u.p[1].x); | |
| 383 | let log_proc = u.p[1].y > 0.5; | |
| 384 | let blend = u.p[1].z; | |
| 385 | ||
| 386 | let pos = vec2<f32>(f32(gid.x) / (sz_x * 1.65), f32(gid.y) / (sz_y * 1.65)); | |
| 387 | // Fusion FilmGrain defaults to monochrome correlated grain (Monochrome=1 -> rg_corr ~1.0) | |
| 388 | let mono = u.p[3].x > 0.5; | |
| 389 | let n_luma = fg_gauss_field(pos, seed, rough); | |
| 390 | var n_rgb: vec3<f32>; | |
| 391 | if (mono) { | |
| 392 | n_rgb = vec3<f32>(n_luma); | |
| 393 | } else { | |
| 394 | n_rgb = vec3<f32>( | |
| 395 | n_luma * 0.75 + fg_gauss_field(pos + vec2<f32>(31.7, 11.3), seed + 17, rough) * 0.661, | |
| 396 | n_luma * 0.75 + fg_gauss_field(pos + vec2<f32>(-19.1, 47.9), seed + 37, rough) * 0.661, | |
| 397 | n_luma * 0.75 + fg_gauss_field(pos + vec2<f32>(53.3, -29.7), seed + 73, rough) * 0.661, | |
| 398 | ); | |
| 399 | } | |
| 400 | let nv = n_rgb * vec3<f32>(u.p[2].y, u.p[2].z, u.p[2].w) * strength; | |
| 401 | ||
| 402 | var rgb = src.rgb; | |
| 403 | if (log_proc) { | |
| 404 | rgb = src.rgb * exp(nv * 1.08); | |
| 405 | } else { | |
| 406 | rgb = src.rgb + nv * 0.55; | |
| 407 | } | |
| 408 | let m = read_mask(gid.xy, dims, u.p[1].w, u.p[2].x) * blend; | |
| 409 | let out_col = mix(src, vec4<f32>(rgb, src.a), clamp(m, 0.0, 1.0)); | |
| 410 | store_dst(vec2<i32>(gid.xy), out_col); | |
| 411 | } |
crates/engine/src/shaders/color.wgsl created+524| ... | ... | @@ -0,0 +1,524 @@ |
| 1 | // ============================================================================ | |
| 2 | // 4. BrightnessContrast | |
| 3 | // p[0] = (gain, lift, gamma, contrast) | |
| 4 | // p[1] = (brightness, saturation, prediv, blend) | |
| 5 | // p[2] = (has_mask, mask_invert, low, high) | |
| 6 | // p[3] = (proc_r, proc_g, proc_b, proc_a) | |
| 7 | // ============================================================================ | |
| 8 | fn bc_curve_scalar(v_in: f32, low: f32, high: f32, gain: f32, lift: f32, gamma: f32, contrast: f32, brightness: f32) -> f32 { | |
| 9 | var v = v_in; | |
| 10 | if (abs(low) > 1e-5 || abs(high - 1.0) > 1e-5) { | |
| 11 | let span = max(high - low, 1e-5); | |
| 12 | v = (v - low) / span; | |
| 13 | } | |
| 14 | v = v * (gain - lift) + lift; | |
| 15 | v = pow(max(v, 0.0), 1.0 / gamma); | |
| 16 | v = (v - 0.5) * (1.0 + contrast) + 0.5; | |
| 17 | v = v + brightness; | |
| 18 | return v; | |
| 19 | } | |
| 20 | ||
| 21 | @compute @workgroup_size(8, 8) | |
| 22 | fn bc_op(@builtin(global_invocation_id) gid: vec3<u32>) { | |
| 23 | let dims = textureDimensions(dst_tex); | |
| 24 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | |
| 25 | let src = textureLoad(src_a, vec2<i32>(gid.xy), 0); | |
| 26 | let gain = u.p[0].x; | |
| 27 | let lift = u.p[0].y; | |
| 28 | let gamma = max(u.p[0].z, 1e-4); | |
| 29 | let contrast = u.p[0].w; | |
| 30 | let brightness = u.p[1].x; | |
| 31 | let sat = u.p[1].y; | |
| 32 | let prediv = u.p[1].z > 0.5; | |
| 33 | let low = u.p[2].z; | |
| 34 | let high = u.p[2].w; | |
| 35 | let proc_r = u.p[3].x > 0.5; | |
| 36 | let proc_g = u.p[3].y > 0.5; | |
| 37 | let proc_b = u.p[3].z > 0.5; | |
| 38 | let proc_a = u.p[3].w > 0.5; | |
| 39 | ||
| 40 | var rgb = src.rgb; | |
| 41 | if (prediv && src.a > 1e-5) { | |
| 42 | rgb = rgb / src.a; | |
| 43 | } | |
| 44 | ||
| 45 | var adj_rgb = vec3<f32>( | |
| 46 | bc_curve_scalar(rgb.r, low, high, gain, lift, gamma, contrast, brightness), | |
| 47 | bc_curve_scalar(rgb.g, low, high, gain, lift, gamma, contrast, brightness), | |
| 48 | bc_curve_scalar(rgb.b, low, high, gain, lift, gamma, contrast, brightness), | |
| 49 | ); | |
| 50 | let luma = dot(adj_rgb, vec3<f32>(0.299, 0.587, 0.114)); | |
| 51 | adj_rgb = mix(vec3<f32>(luma), adj_rgb, sat); | |
| 52 | ||
| 53 | let out_rgb_unpre = vec3<f32>( | |
| 54 | select(rgb.r, adj_rgb.r, proc_r), | |
| 55 | select(rgb.g, adj_rgb.g, proc_g), | |
| 56 | select(rgb.b, adj_rgb.b, proc_b), | |
| 57 | ); | |
| 58 | let out_a = select(src.a, clamp(bc_curve_scalar(src.a, low, high, gain, lift, gamma, contrast, brightness), 0.0, 1.0), proc_a); | |
| 59 | let out_rgb = select(out_rgb_unpre, out_rgb_unpre * out_a, prediv); | |
| 60 | let adjusted = vec4<f32>(out_rgb, out_a); | |
| 61 | let m = read_mask(gid.xy, dims, u.p[2].x, u.p[2].y) * u.p[1].w; | |
| 62 | let out_col = mix(src, adjusted, clamp(m, 0.0, 1.0)); | |
| 63 | store_dst(vec2<i32>(gid.xy), out_col); | |
| 64 | } | |
| 65 | ||
| 66 | ||
| 67 | // ============================================================================ | |
| 68 | // 5. ColorCorrector (Master + Shadows/Midtones/Highlights wheels, TintMode 0 & 1, LGG) | |
| 69 | // p[0] = (hue1, sat1, tint_angle1, tint_len1) | |
| 70 | // p[1] = (gain1, lift1, gamma1, contrast1) | |
| 71 | // p[2] = (brightness1, prediv, blend, has_mask) | |
| 72 | // p[3] = (mask_invert, tint_mode, has_smh, 0) | |
| 73 | // p[4] = (hue2, sat2, tint_angle2, tint_len2) // Shadows | |
| 74 | // p[5] = (gain2, lift2, gamma2, contrast2) | |
| 75 | // p[6] = (hue3, sat3, tint_angle3, tint_len3) // Midtones | |
| 76 | // p[7] = (gain3, lift3, gamma3, contrast3) | |
| 77 | // p[8] = (hue4, sat4, tint_angle4, tint_len4) // Highlights | |
| 78 | // p[9] = (gain4, lift4, gamma4, contrast4) | |
| 79 | // p[10] = (bright2, bright3, bright4, 0) | |
| 80 | // ============================================================================ | |
| 81 | fn rotate_hue_preserve_luma(c: vec3<f32>, angle: f32) -> vec3<f32> { | |
| 82 | // Paul Haeberli luminance-preserving shear-rotate-unshear around (1,1,1) with Rec.601 weights. | |
| 83 | // Verified 0.00/255 bit-exact against Fusion Studio 21.1 ColorCorrector! | |
| 84 | let k = vec3<f32>(0.299, 0.587, 0.114); | |
| 85 | let y = dot(c, k); | |
| 86 | let ch = c - vec3<f32>(y); | |
| 87 | let ca = cos(angle); | |
| 88 | let sa = sin(angle); | |
| 89 | let s3 = 0.5773502691896258; // 1 / sqrt(3) | |
| 90 | let ndv = (ch.r + ch.g + ch.b) * (1.0 / 3.0); | |
| 91 | let nxv = vec3<f32>(ch.g - ch.b, ch.b - ch.r, ch.r - ch.g) * s3; | |
| 92 | let v = ch * ca + nxv * sa + vec3<f32>(ndv * (1.0 - ca)); | |
| 93 | let dy = dot(v, k); | |
| 94 | return vec3<f32>(y) + v - vec3<f32>(dy); | |
| 95 | } | |
| 96 | ||
| 97 | fn hsv_pure_hue(h_turns: f32) -> vec3<f32> { | |
| 98 | let h = fract(h_turns) * 6.0; | |
| 99 | let r = clamp(abs(h - 3.0) - 1.0, 0.0, 1.0); | |
| 100 | let g = clamp(2.0 - abs(h - 2.0), 0.0, 1.0); | |
| 101 | let b = clamp(2.0 - abs(h - 4.0), 0.0, 1.0); | |
| 102 | return vec3<f32>(r, g, b); | |
| 103 | } | |
| 104 | ||
| 105 | fn apply_cc_wheel_and_lgg( | |
| 106 | rgb_in: vec3<f32>, | |
| 107 | hue_turns: f32, | |
| 108 | sat: f32, | |
| 109 | tint_turns: f32, | |
| 110 | tint_len: f32, | |
| 111 | tint_mode: f32, | |
| 112 | gain: f32, | |
| 113 | lift: f32, | |
| 114 | gamma_in: f32, | |
| 115 | contrast: f32, | |
| 116 | brightness: f32, | |
| 117 | ) -> vec3<f32> { | |
| 118 | let k_luma = vec3<f32>(0.299, 0.587, 0.114); | |
| 119 | let gamma = max(gamma_in, 1e-4); | |
| 120 | var rgb = rgb_in; | |
| 121 | ||
| 122 | // 1. Hue rotation (verified 0.00/255 vs Fusion 21.1) | |
| 123 | if (abs(hue_turns) > 1e-5) { | |
| 124 | rgb = rotate_hue_preserve_luma(rgb, -hue_turns * 6.28318530718); | |
| 125 | } | |
| 126 | ||
| 127 | // 2. Fusion Color Wheel Tint | |
| 128 | if (tint_len > 1e-5) { | |
| 129 | let l = clamp(tint_len, 0.0, 1.0); | |
| 130 | if (tint_mode < 0.5) { | |
| 131 | // TintMode = 0 (RGB cube tilt + perpendicular chroma squeeze, verified 0.00/255 at len=1.0!) | |
| 132 | let s3 = 0.5773502691896258; | |
| 133 | let e1 = vec3<f32>(s3, s3, s3); | |
| 134 | let a = tint_turns * 6.28318530718; | |
| 135 | let u_ch = normalize(vec3<f32>(cos(a), cos(a - 2.09439510239), cos(a + 2.09439510239))); | |
| 136 | let u_pp = normalize(cross(e1, u_ch)); | |
| 137 | let t = l * l; | |
| 138 | let th = 0.471238898038 * t; // 27.0 deg * len^2 | |
| 139 | let c_th = cos(th); | |
| 140 | let s_th = sin(th); | |
| 141 | let x1 = dot(rgb, e1); | |
| 142 | let x2 = dot(rgb, u_ch); | |
| 143 | let x3 = dot(rgb, u_pp); | |
| 144 | let y1 = c_th * x1 - s_th * x2; | |
| 145 | let y2 = s_th * x1 + c_th * x2; | |
| 146 | let y3 = (1.0 - t) * x3; | |
| 147 | rgb = y1 * e1 + y2 * u_ch + y3 * u_pp; | |
| 148 | } else if (tint_mode < 1.5) { | |
| 149 | // TintMode = 1: luma-preserving chroma blend + amp = l*((1-l) + l*2*min(y,1-y)), verified 0.00/255! | |
| 150 | let y_t = dot(rgb, k_luma); | |
| 151 | let pure = hsv_pure_hue(tint_turns); | |
| 152 | let pure_y = dot(pure, k_luma); | |
| 153 | let tc = pure - vec3<f32>(pure_y); | |
| 154 | let yc = clamp(y_t, 0.0, 1.0); | |
| 155 | let amp = l * ((1.0 - l) + l * 2.0 * min(yc, 1.0 - yc)); | |
| 156 | rgb = vec3<f32>(y_t) + (rgb - vec3<f32>(y_t)) * (1.0 - l) + tc * amp; | |
| 157 | } else { | |
| 158 | // TintMode = 2 (Fusion 21.1 default when omitted): lerp toward pure HSV chroma * 2*min(y,1-y) | |
| 159 | let y_t = dot(rgb, k_luma); | |
| 160 | let pure = hsv_pure_hue(tint_turns); | |
| 161 | let pure_y = dot(pure, k_luma); | |
| 162 | let tc = pure - vec3<f32>(pure_y); | |
| 163 | let yc = clamp(y_t, 0.0, 1.0); | |
| 164 | let amp = 2.0 * min(yc, 1.0 - yc); | |
| 165 | let tint_rgb = vec3<f32>(y_t) + tc * amp; | |
| 166 | rgb = mix(rgb, tint_rgb, l); | |
| 167 | } | |
| 168 | } | |
| 169 | ||
| 170 | // 3. Saturation (Rec.601 luma, no gamut crushing) | |
| 171 | if (abs(sat - 1.0) > 1e-5) { | |
| 172 | let luma = dot(rgb, k_luma); | |
| 173 | rgb = mix(vec3<f32>(luma), rgb, sat); | |
| 174 | } | |
| 175 | ||
| 176 | // 4. Contrast -> Lift/Gain -> Brightness -> Gamma | |
| 177 | if (abs(contrast - 1.0) > 1e-5) { | |
| 178 | rgb = (rgb - 0.5) * contrast + 0.5; | |
| 179 | } | |
| 180 | if (abs(gain - 1.0) > 1e-5 || abs(lift) > 1e-5) { | |
| 181 | rgb = rgb * (gain - lift) + lift; | |
| 182 | } | |
| 183 | if (abs(brightness) > 1e-5) { | |
| 184 | rgb = rgb + brightness; | |
| 185 | } | |
| 186 | if (abs(gamma - 1.0) > 1e-5) { | |
| 187 | rgb = pow(max(rgb, vec3<f32>(0.0)), vec3<f32>(1.0 / gamma)); | |
| 188 | } | |
| 189 | return rgb; | |
| 190 | } | |
| 191 | ||
| 192 | @compute @workgroup_size(8, 8) | |
| 193 | fn cc_op(@builtin(global_invocation_id) gid: vec3<u32>) { | |
| 194 | let dims = textureDimensions(dst_tex); | |
| 195 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | |
| 196 | let src = textureLoad(src_a, vec2<i32>(gid.xy), 0); | |
| 197 | let prediv = u.p[2].y > 0.5; | |
| 198 | let tint_mode = u.p[3].y; | |
| 199 | let has_smh = u.p[3].z > 0.5; | |
| 200 | let k_luma = vec3<f32>(0.299, 0.587, 0.114); | |
| 201 | ||
| 202 | var rgb = src.rgb; | |
| 203 | if (prediv && src.a > 1e-5) { | |
| 204 | rgb = rgb / src.a; | |
| 205 | } | |
| 206 | ||
| 207 | if (has_smh) { | |
| 208 | let y0 = clamp(dot(rgb, k_luma), 0.0, 1.0); | |
| 209 | let w_s = clamp(1.0 - 2.0 * y0, 0.0, 1.0); | |
| 210 | let w_h = clamp(2.0 * y0 - 1.0, 0.0, 1.0); | |
| 211 | let w_m = max(1.0 - w_s - w_h, 0.0); | |
| 212 | let rgb_s = apply_cc_wheel_and_lgg(rgb, u.p[4].x, u.p[4].y, u.p[4].z, u.p[4].w, tint_mode, u.p[5].x, u.p[5].y, u.p[5].z, u.p[5].w, u.p[10].x); | |
| 213 | let rgb_m = apply_cc_wheel_and_lgg(rgb, u.p[6].x, u.p[6].y, u.p[6].z, u.p[6].w, tint_mode, u.p[7].x, u.p[7].y, u.p[7].z, u.p[7].w, u.p[10].y); | |
| 214 | let rgb_h = apply_cc_wheel_and_lgg(rgb, u.p[8].x, u.p[8].y, u.p[8].z, u.p[8].w, tint_mode, u.p[9].x, u.p[9].y, u.p[9].z, u.p[9].w, u.p[10].z); | |
| 215 | rgb = rgb_s * w_s + rgb_m * w_m + rgb_h * w_h; | |
| 216 | } | |
| 217 | ||
| 218 | rgb = apply_cc_wheel_and_lgg(rgb, u.p[0].x, u.p[0].y, u.p[0].z, u.p[0].w, tint_mode, u.p[1].x, u.p[1].y, u.p[1].z, u.p[1].w, u.p[2].x); | |
| 219 | ||
| 220 | if (prediv) { | |
| 221 | rgb = rgb * src.a; | |
| 222 | } | |
| 223 | let m = read_mask(gid.xy, dims, u.p[2].w, u.p[3].x) * u.p[2].z; | |
| 224 | let out_col = mix(src, vec4<f32>(rgb, src.a), clamp(m, 0.0, 1.0)); | |
| 225 | store_dst(vec2<i32>(gid.xy), out_col); | |
| 226 | } | |
| 227 | // ============================================================================ | |
| 228 | // 11. Fuse.Colorize (Clover's exact 2020 Colorize.fuse formula!) | |
| 229 | // p[0] = (r1, g1, b1, a1), p[1] = (r2, g2, b2, a2), p[2] = (blend, has_mask, mask_invert, 0) | |
| 230 | // ============================================================================ | |
| 231 | @compute @workgroup_size(8, 8) | |
| 232 | fn colorize_op(@builtin(global_invocation_id) gid: vec3<u32>) { | |
| 233 | let dims = textureDimensions(dst_tex); | |
| 234 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | |
| 235 | let src = textureLoad(src_a, vec2<i32>(gid.xy), 0); | |
| 236 | let c1 = u.p[0]; | |
| 237 | let c2 = u.p[1]; | |
| 238 | ||
| 239 | let ab = src.a; | |
| 240 | let aa = c1.a + ab * (c2.a - c1.a); | |
| 241 | let unpre = select(vec3<f32>(0.0), src.rgb / ab, ab > 1e-6); | |
| 242 | let out_rgb = (c1.rgb + unpre * (c2.rgb - c1.rgb)) * aa; | |
| 243 | let col = vec4<f32>(out_rgb, aa); | |
| 244 | let m = read_mask(gid.xy, dims, u.p[2].y, u.p[2].z) * u.p[2].x; | |
| 245 | let out_col = mix(src, col, clamp(m, 0.0, 1.0)); | |
| 246 | store_dst(vec2<i32>(gid.xy), out_col); | |
| 247 | } | |
| 248 | // ============================================================================ | |
| 249 | // 17. InvertColor / Invert Macro | |
| 250 | // p[0] = (proc_r, proc_g, proc_b, proc_a) | |
| 251 | // p[1] = (blend, has_mask, mask_invert, 0) | |
| 252 | // ============================================================================ | |
| 253 | @compute @workgroup_size(8, 8) | |
| 254 | fn invert_op(@builtin(global_invocation_id) gid: vec3<u32>) { | |
| 255 | let dims = textureDimensions(dst_tex); | |
| 256 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | |
| 257 | let src = textureLoad(src_a, vec2<i32>(gid.xy), 0); | |
| 258 | let a = src.a; | |
| 259 | let unpre = select(vec3<f32>(0.0), src.rgb / max(a, 1e-5), a > 1e-5); | |
| 260 | let new_a = select(a, 1.0 - a, u.p[0].w > 0.5); | |
| 261 | let inv_rgb = vec3<f32>( | |
| 262 | select(unpre.r, 1.0 - unpre.r, u.p[0].x > 0.5), | |
| 263 | select(unpre.g, 1.0 - unpre.g, u.p[0].y > 0.5), | |
| 264 | select(unpre.b, 1.0 - unpre.b, u.p[0].z > 0.5), | |
| 265 | ); | |
| 266 | let inv = vec4<f32>(inv_rgb * new_a, new_a); | |
| 267 | let m = read_mask(gid.xy, dims, u.p[1].y, u.p[1].z) * u.p[1].x; | |
| 268 | let out_col = mix(src, inv, clamp(m, 0.0, 1.0)); | |
| 269 | store_dst(vec2<i32>(gid.xy), out_col); | |
| 270 | } | |
| 271 | // ============================================================================ | |
| 272 | // 19. ColorGain (Non-luma-preserving HSV Hue shift across Dark/Mid/High + per-channel Gain/Lift/Gamma/Sat) | |
| 273 | // p[0] = (gain_r, gain_g, gain_b, prediv) | |
| 274 | // p[1] = (lift_r, lift_g, lift_b, blend) | |
| 275 | // p[2] = (gamma_r, gamma_g, gamma_b, sat_mid) | |
| 276 | // p[3] = (dark_hue, mid_hue, high_hue, has_mask) | |
| 277 | // p[4] = (mask_invert, sat_dark, sat_high, 0) | |
| 278 | // ============================================================================ | |
| 279 | fn rgb_to_hsv(c: vec3<f32>) -> vec3<f32> { | |
| 280 | let k = vec4<f32>(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); | |
| 281 | let p = select(vec4<f32>(c.gb, k.xy), vec4<f32>(c.bg, k.wz), c.g < c.b); | |
| 282 | let q = select(vec4<f32>(c.r, p.yzx), vec4<f32>(p.xyw, c.r), c.r < p.x); | |
| 283 | let d = q.x - min(q.w, q.y); | |
| 284 | let e = 1.0e-10; | |
| 285 | return vec3<f32>(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); | |
| 286 | } | |
| 287 | ||
| 288 | fn hsv_to_rgb(c: vec3<f32>) -> vec3<f32> { | |
| 289 | let k = vec4<f32>(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); | |
| 290 | let p = abs(fract(c.xxx + k.xyz) * 6.0 - k.www); | |
| 291 | return c.z * mix(k.xxx, clamp(p - k.xxx, vec3<f32>(0.0), vec3<f32>(1.0)), c.y); | |
| 292 | } | |
| 293 | ||
| 294 | @compute @workgroup_size(8, 8) | |
| 295 | fn color_gain_op(@builtin(global_invocation_id) gid: vec3<u32>) { | |
| 296 | let dims = textureDimensions(dst_tex); | |
| 297 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | |
| 298 | let src = textureLoad(src_a, vec2<i32>(gid.xy), 0); | |
| 299 | let gain = u.p[0].xyz; | |
| 300 | let prediv = u.p[0].w > 0.5; | |
| 301 | let lift = u.p[1].xyz; | |
| 302 | let blend = u.p[1].w; | |
| 303 | let gamma = max(u.p[2].xyz, vec3<f32>(1e-4)); | |
| 304 | let sat_mid = u.p[2].w; | |
| 305 | let dark_hue = u.p[3].x; | |
| 306 | let mid_hue = u.p[3].y; | |
| 307 | let high_hue = u.p[3].z; | |
| 308 | let sat_dark = u.p[4].y; | |
| 309 | let sat_high = u.p[4].z; | |
| 310 | ||
| 311 | var rgb = src.rgb; | |
| 312 | if (prediv && src.a > 1e-5) { | |
| 313 | rgb = rgb / src.a; | |
| 314 | } | |
| 315 | ||
| 316 | // Per-channel Gain -> Lift -> Gamma | |
| 317 | rgb = rgb * gain; | |
| 318 | rgb = rgb + lift * (1.0 - rgb); | |
| 319 | rgb = pow(max(rgb, vec3<f32>(0.0)), vec3<f32>(1.0) / gamma); | |
| 320 | ||
| 321 | // Tonal range weights using HSV Value max(r,g,b) so saturated primaries & outer anti-aliased | |
| 322 | // outline edges (>= 0.5) land cleanly in Mid without reddish DarkHue halos, while RGB band blending | |
| 323 | // completely prevents green hue-interpolation fringes! | |
| 324 | let c_pos = max(rgb, vec3<f32>(0.0)); | |
| 325 | let lum = clamp(max(c_pos.r, max(c_pos.g, c_pos.b)), 0.0, 1.0); | |
| 326 | let w_dark = clamp(1.0 - lum * 2.0, 0.0, 1.0); | |
| 327 | let w_high = clamp((lum - 0.5) * 2.0, 0.0, 1.0); | |
| 328 | let w_mid = clamp(1.0 - w_dark - w_high, 0.0, 1.0); | |
| 329 | ||
| 330 | // Fusion ColorGain evaluates Dark, High, then Mid sequentially as RGB mixes against base_hsv | |
| 331 | // using V = max(r,g,b) for the range curve — matches Fusion 21.1 to ~0.002 and stays green-fringe-free! | |
| 332 | let any_hsv = abs(dark_hue) > 1e-5 || abs(mid_hue) > 1e-5 || abs(high_hue) > 1e-5 | |
| 333 | || abs(sat_dark - 1.0) > 1e-4 || abs(sat_mid - 1.0) > 1e-4 || abs(sat_high - 1.0) > 1e-4; | |
| 334 | if (any_hsv) { | |
| 335 | let base_hsv = rgb_to_hsv(c_pos); | |
| 336 | var cur = c_pos; | |
| 337 | if ((abs(dark_hue) > 1e-5 || abs(sat_dark - 1.0) > 1e-4) && w_dark > 1e-5) { | |
| 338 | let h_d = vec3<f32>(fract(base_hsv.x + dark_hue), clamp(base_hsv.y * sat_dark, 0.0, 1.0), base_hsv.z); | |
| 339 | cur = mix(cur, hsv_to_rgb(h_d), w_dark); | |
| 340 | } | |
| 341 | if ((abs(high_hue) > 1e-5 || abs(sat_high - 1.0) > 1e-4) && w_high > 1e-5) { | |
| 342 | let h_h = vec3<f32>(fract(base_hsv.x + high_hue), clamp(base_hsv.y * sat_high, 0.0, 1.0), base_hsv.z); | |
| 343 | cur = mix(cur, hsv_to_rgb(h_h), w_high); | |
| 344 | } | |
| 345 | if ((abs(mid_hue) > 1e-5 || abs(sat_mid - 1.0) > 1e-4) && w_mid > 1e-5) { | |
| 346 | let h_m = vec3<f32>(fract(base_hsv.x + mid_hue), clamp(base_hsv.y * sat_mid, 0.0, 1.0), base_hsv.z); | |
| 347 | cur = mix(cur, hsv_to_rgb(h_m), w_mid); | |
| 348 | } | |
| 349 | rgb = cur; | |
| 350 | } | |
| 351 | ||
| 352 | if (prediv) { | |
| 353 | rgb = rgb * src.a; | |
| 354 | } | |
| 355 | let m = read_mask(gid.xy, dims, u.p[3].w, u.p[4].x) * blend; | |
| 356 | let out_col = mix(src, vec4<f32>(rgb, src.a), clamp(m, 0.0, 1.0)); | |
| 357 | store_dst(vec2<i32>(gid.xy), out_col); | |
| 358 | } | |
| 359 | ||
| 360 | ||
| 361 | // ============================================================================ | |
| 362 | // 20. ColorCurves (per-channel 256x1 LUT texture in src_b) | |
| 363 | // p[0] = (prediv, ignore_transparent, blend, has_mask) | |
| 364 | // p[1] = (mask_invert, apply_alpha, 0, 0) | |
| 365 | // ============================================================================ | |
| 366 | @compute @workgroup_size(8, 8) | |
| 367 | fn color_curves_op(@builtin(global_invocation_id) gid: vec3<u32>) { | |
| 368 | let dims = textureDimensions(dst_tex); | |
| 369 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | |
| 370 | let src = textureLoad(src_a, vec2<i32>(gid.xy), 0); | |
| 371 | let prediv = u.p[0].x > 0.5; | |
| 372 | let ignore_trans = u.p[0].y > 0.5; | |
| 373 | let blend = u.p[0].z; | |
| 374 | let apply_a = u.p[1].y > 0.5; | |
| 375 | ||
| 376 | if (ignore_trans && src.a <= 1e-5) { | |
| 377 | textureStore(dst_tex, vec2<i32>(gid.xy), src); | |
| 378 | return; | |
| 379 | } | |
| 380 | ||
| 381 | var rgb = src.rgb; | |
| 382 | let unpre = (prediv || ignore_trans) && src.a > 1e-5; | |
| 383 | if (unpre) { | |
| 384 | rgb = clamp(rgb / src.a, vec3<f32>(0.0), vec3<f32>(1.0)); | |
| 385 | } | |
| 386 | ||
| 387 | let r = textureSampleLevel(src_b, samp_lin, vec2<f32>(clamp(rgb.r, 0.0, 1.0), 0.5), 0.0).r; | |
| 388 | let g = textureSampleLevel(src_b, samp_lin, vec2<f32>(clamp(rgb.g, 0.0, 1.0), 0.5), 0.0).g; | |
| 389 | let b = textureSampleLevel(src_b, samp_lin, vec2<f32>(clamp(rgb.b, 0.0, 1.0), 0.5), 0.0).b; | |
| 390 | var a = src.a; | |
| 391 | if (apply_a) { | |
| 392 | a = textureSampleLevel(src_b, samp_lin, vec2<f32>(clamp(src.a, 0.0, 1.0), 0.5), 0.0).a; | |
| 393 | } | |
| 394 | ||
| 395 | var out_rgb = vec3<f32>(r, g, b); | |
| 396 | if (unpre) { | |
| 397 | out_rgb = out_rgb * a; | |
| 398 | } | |
| 399 | let m = read_mask(gid.xy, dims, u.p[0].w, u.p[1].x) * blend; | |
| 400 | let out_col = mix(src, vec4<f32>(out_rgb, a), clamp(m, 0.0, 1.0)); | |
| 401 | store_dst(vec2<i32>(gid.xy), out_col); | |
| 402 | } | |
| 403 | // ============================================================================ | |
| 404 | // 22. GamutConvert (sRGB / Rec.709 / Gamma transfer conversion) | |
| 405 | // p[0] = (src_space: 0=None/Linear,1=sRGB,2=Rec709, dst_space: 0=None/Linear,1=sRGB,2=Rec709, prediv, blend) | |
| 406 | // p[1] = (has_mask, mask_invert, 0, 0) | |
| 407 | // ============================================================================ | |
| 408 | fn srgb_to_linear_ch(c: f32) -> f32 { | |
| 409 | if (c <= 0.04045) { return c / 12.92; } | |
| 410 | return pow((c + 0.055) / 1.055, 2.4); | |
| 411 | } | |
| 412 | fn linear_to_srgb_ch(c: f32) -> f32 { | |
| 413 | let x = max(c, 0.0); | |
| 414 | if (x <= 0.0031308) { return x * 12.92; } | |
| 415 | return 1.055 * pow(x, 1.0 / 2.4) - 0.055; | |
| 416 | } | |
| 417 | fn rec709_to_linear_ch(c: f32) -> f32 { | |
| 418 | if (c < 0.081) { return c / 4.5; } | |
| 419 | return pow((c + 0.099) / 1.099, 1.0 / 0.45); | |
| 420 | } | |
| 421 | fn linear_to_rec709_ch(c: f32) -> f32 { | |
| 422 | let x = max(c, 0.0); | |
| 423 | if (x < 0.018) { return x * 4.5; } | |
| 424 | return 1.099 * pow(x, 0.45) - 0.099; | |
| 425 | } | |
| 426 | ||
| 427 | @compute @workgroup_size(8, 8) | |
| 428 | fn gamut_convert_op(@builtin(global_invocation_id) gid: vec3<u32>) { | |
| 429 | let dims = textureDimensions(dst_tex); | |
| 430 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | |
| 431 | let src = textureLoad(src_a, vec2<i32>(gid.xy), 0); | |
| 432 | let src_sp = i32(u.p[0].x + 0.5); | |
| 433 | let dst_sp = i32(u.p[0].y + 0.5); | |
| 434 | let prediv = u.p[0].z > 0.5; | |
| 435 | let blend = u.p[0].w; | |
| 436 | ||
| 437 | var rgb = src.rgb; | |
| 438 | if (prediv && src.a > 1e-5) { | |
| 439 | rgb = rgb / src.a; | |
| 440 | } | |
| 441 | ||
| 442 | // Remove source gamma -> Linear | |
| 443 | if (src_sp == 1) { | |
| 444 | rgb = vec3<f32>(srgb_to_linear_ch(rgb.r), srgb_to_linear_ch(rgb.g), srgb_to_linear_ch(rgb.b)); | |
| 445 | } else if (src_sp == 2) { | |
| 446 | rgb = vec3<f32>(rec709_to_linear_ch(rgb.r), rec709_to_linear_ch(rgb.g), rec709_to_linear_ch(rgb.b)); | |
| 447 | } | |
| 448 | // Add destination gamma <- Linear | |
| 449 | if (dst_sp == 1) { | |
| 450 | rgb = vec3<f32>(linear_to_srgb_ch(rgb.r), linear_to_srgb_ch(rgb.g), linear_to_srgb_ch(rgb.b)); | |
| 451 | } else if (dst_sp == 2) { | |
| 452 | rgb = vec3<f32>(linear_to_rec709_ch(rgb.r), linear_to_rec709_ch(rgb.g), linear_to_rec709_ch(rgb.b)); | |
| 453 | } | |
| 454 | ||
| 455 | if (prediv) { | |
| 456 | rgb = rgb * src.a; | |
| 457 | } | |
| 458 | let m = read_mask(gid.xy, dims, u.p[1].x, u.p[1].y) * blend; | |
| 459 | let out_col = mix(src, vec4<f32>(rgb, src.a), clamp(m, 0.0, 1.0)); | |
| 460 | store_dst(vec2<i32>(gid.xy), out_col); | |
| 461 | } | |
| 462 | // ============================================================================ | |
| 463 | // 25. HueCurves (9 hue-indexed LUT curves baked into 256x3 RGBA8 texture in src_b) | |
| 464 | // Row 0: (Hue, Sat, Lum, 1), Row 1: (Red, Green, Blue, 1), Row 2: (RedSup, GreenSup, BlueSup, 1) | |
| 465 | // All encoded as val / 4.0 so neutral 1.0 == 0.25 and boost up to 4.0x fits cleanly. | |
| 466 | // p[0] = (prediv, blend, has_mask, mask_invert) | |
| 467 | // ============================================================================ | |
| 468 | @compute @workgroup_size(8, 8) | |
| 469 | fn hue_curves_op(@builtin(global_invocation_id) gid: vec3<u32>) { | |
| 470 | let dims = textureDimensions(dst_tex); | |
| 471 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | |
| 472 | let src = textureLoad(src_a, vec2<i32>(gid.xy), 0); | |
| 473 | let prediv = u.p[0].x > 0.5; | |
| 474 | let blend = u.p[0].y; | |
| 475 | ||
| 476 | var rgb = src.rgb; | |
| 477 | if (prediv && src.a > 1e-5) { | |
| 478 | rgb = rgb / src.a; | |
| 479 | } | |
| 480 | let c_pos = max(rgb, vec3<f32>(0.0)); | |
| 481 | let hsv = rgb_to_hsv(c_pos); | |
| 482 | let h_u = fract(hsv.x); | |
| 483 | let sat = hsv.y; | |
| 484 | ||
| 485 | let row0 = textureSampleLevel(src_b, samp_lin, vec2<f32>(h_u, 0.5 / 3.0), 0.0) * 4.0; | |
| 486 | let row1 = textureSampleLevel(src_b, samp_lin, vec2<f32>(h_u, 1.5 / 3.0), 0.0) * 4.0; | |
| 487 | let row2 = textureSampleLevel(src_b, samp_lin, vec2<f32>(h_u, 2.5 / 3.0), 0.0) * 4.0; | |
| 488 | ||
| 489 | // 1. Hue shift (curve neutral=1.0, domain 0..6 => delta = (hue_v - 1.0) / 6.0) | |
| 490 | if (abs(row0.x - 1.0) > 1e-3 && sat > 1e-4) { | |
| 491 | rgb = hsv_to_rgb(vec3<f32>(fract(hsv.x + (row0.x - 1.0) / 6.0), sat, hsv.z)); | |
| 492 | } | |
| 493 | // 2. Saturation (Rec.601 luma mix) | |
| 494 | if (abs(row0.y - 1.0) > 1e-3) { | |
| 495 | let luma = dot(rgb, vec3<f32>(0.299, 0.587, 0.114)); | |
| 496 | rgb = mix(vec3<f32>(luma), rgb, row0.y); | |
| 497 | } | |
| 498 | // 3. Luminance (weighted by pixel saturation so grays stay untouched) | |
| 499 | if (abs(row0.z - 1.0) > 1e-3) { | |
| 500 | rgb = rgb * (1.0 + (row0.z - 1.0) * sat); | |
| 501 | } | |
| 502 | // 4. Per-channel Red / Green / Blue multipliers | |
| 503 | rgb = rgb * row1.xyz; | |
| 504 | // 5. Red / Green / Blue spill suppression | |
| 505 | if (row2.x < 0.999) { | |
| 506 | let lim = max(rgb.g, rgb.b); | |
| 507 | if (rgb.r > lim) { rgb.r = mix(lim, rgb.r, row2.x); } | |
| 508 | } | |
| 509 | if (row2.y < 0.999) { | |
| 510 | let lim = max(rgb.r, rgb.b); | |
| 511 | if (rgb.g > lim) { rgb.g = mix(lim, rgb.g, row2.y); } | |
| 512 | } | |
| 513 | if (row2.z < 0.999) { | |
| 514 | let lim = max(rgb.r, rgb.g); | |
| 515 | if (rgb.b > lim) { rgb.b = mix(lim, rgb.b, row2.z); } | |
| 516 | } | |
| 517 | ||
| 518 | if (prediv) { | |
| 519 | rgb = rgb * src.a; | |
| 520 | } | |
| 521 | let m = read_mask(gid.xy, dims, u.p[0].z, u.p[0].w) * blend; | |
| 522 | let out_col = mix(src, vec4<f32>(rgb, src.a), clamp(m, 0.0, 1.0)); | |
| 523 | store_dst(vec2<i32>(gid.xy), out_col); | |
| 524 | } |
crates/engine/src/shaders/common.wgsl created+105| ... | ... | @@ -0,0 +1,105 @@ |
| 1 | // Compositor GPU compute shaders (WGSL). | |
| 2 | // Coordinate convention: Fusion uses normalized UV (0,0) = bottom-left, (1,1) = top-right, | |
| 3 | // whereas textures store row 0 at the top. Helpers `pix_to_fu` / `fu_to_uv` bridge cleanly. | |
| 4 | ||
| 5 | @group(0) @binding(0) var src_a: texture_2d<f32>; | |
| 6 | @group(0) @binding(1) var src_b: texture_2d<f32>; | |
| 7 | @group(0) @binding(2) var mask_tex: texture_2d<f32>; | |
| 8 | @group(0) @binding(3) var samp_lin: sampler; | |
| 9 | @group(0) @binding(4) var dst_tex: texture_storage_2d<rgba8unorm, write>; | |
| 10 | ||
| 11 | // Replaced at pipeline build time for Float16/Float32 HDR pipelines (clamped for Int8/Int16). | |
| 12 | const HDR_FLOAT: bool = false; | |
| 13 | ||
| 14 | fn store_dst(p: vec2<i32>, col: vec4<f32>) { | |
| 15 | if (HDR_FLOAT) { | |
| 16 | textureStore(dst_tex, p, col); | |
| 17 | } else { | |
| 18 | textureStore(dst_tex, p, clamp(col, vec4<f32>(0.0), vec4<f32>(1.0))); | |
| 19 | } | |
| 20 | } | |
| 21 | ||
| 22 | struct Params { | |
| 23 | // 16 vec4<f32> = 256 bytes of uniform parameters per dispatch. | |
| 24 | p: array<vec4<f32>, 16>, | |
| 25 | }; | |
| 26 | @group(0) @binding(5) var<uniform> u: Params; | |
| 27 | ||
| 28 | // Helper: output pixel (gid.xy, top-down) -> Fusion UV (0..1, bottom-up). | |
| 29 | fn pix_to_fu(gid: vec2<u32>, dims: vec2<u32>) -> vec2<f32> { | |
| 30 | let f = vec2<f32>(dims); | |
| 31 | return vec2<f32>((f32(gid.x) + 0.5) / f.x, 1.0 - (f32(gid.y) + 0.5) / f.y); | |
| 32 | } | |
| 33 | ||
| 34 | // Helper: Fusion UV (bottom-up) -> texture UV (top-down). | |
| 35 | fn fu_to_uv(fu: vec2<f32>) -> vec2<f32> { | |
| 36 | return vec2<f32>(fu.x, 1.0 - fu.y); | |
| 37 | } | |
| 38 | ||
| 39 | fn sample_tex(tex: texture_2d<f32>, uv: vec2<f32>, nearest: bool) -> vec4<f32> { | |
| 40 | if (nearest) { | |
| 41 | let isdims = vec2<i32>(textureDimensions(tex)); | |
| 42 | let sx = clamp(i32(floor(uv.x * f32(isdims.x))), 0, isdims.x - 1); | |
| 43 | let sy = clamp(i32(floor(uv.y * f32(isdims.y))), 0, isdims.y - 1); | |
| 44 | return textureLoad(tex, vec2<i32>(sx, sy), 0); | |
| 45 | } | |
| 46 | return textureSampleLevel(tex, samp_lin, uv, 0.0); | |
| 47 | } | |
| 48 | ||
| 49 | // Helper: Sample texture with Fusion Edges mode: 0=Canvas, 1=Wrap, 2=Duplicate, 3=Mirror. | |
| 50 | // When `dod` (`origin_x, origin_y, canvas_w, canvas_h`) has `canvas_w > 0.5`, maps `fu` | |
| 51 | // (normalized to `canvas`) into the texture's expanded Domain of Definition! | |
| 52 | fn sample_dod_edges_mode(tex: texture_2d<f32>, fu: vec2<f32>, edges: i32, dod: vec4<f32>, nearest: bool) -> vec4<f32> { | |
| 53 | var uv: vec2<f32>; | |
| 54 | if (dod.z > 0.5 && dod.w > 0.5) { | |
| 55 | let tdims = vec2<f32>(textureDimensions(tex)); | |
| 56 | let canvas_px = vec2<f32>(fu.x * dod.z, (1.0 - fu.y) * dod.w); | |
| 57 | uv = (canvas_px - dod.xy) / tdims; | |
| 58 | } else { | |
| 59 | uv = fu_to_uv(fu); | |
| 60 | } | |
| 61 | if (edges == 0) { | |
| 62 | if (uv.x < 0.0 || uv.x > 1.0 || uv.y < 0.0 || uv.y > 1.0) { | |
| 63 | return vec4<f32>(0.0); | |
| 64 | } | |
| 65 | return sample_tex(tex, uv, nearest); | |
| 66 | } else if (edges == 1) { | |
| 67 | uv = fract(uv); | |
| 68 | return sample_tex(tex, uv, nearest); | |
| 69 | } else if (edges == 2) { | |
| 70 | uv = clamp(uv, vec2<f32>(0.0), vec2<f32>(1.0)); | |
| 71 | return sample_tex(tex, uv, nearest); | |
| 72 | } else { | |
| 73 | let m = abs(fract(uv * 0.5 + 0.5) * 2.0 - 1.0); | |
| 74 | return sample_tex(tex, m, nearest); | |
| 75 | } | |
| 76 | } | |
| 77 | ||
| 78 | fn sample_dod_edges(tex: texture_2d<f32>, fu: vec2<f32>, edges: i32, dod: vec4<f32>) -> vec4<f32> { | |
| 79 | return sample_dod_edges_mode(tex, fu, edges, dod, false); | |
| 80 | } | |
| 81 | ||
| 82 | fn sample_edges(tex: texture_2d<f32>, fu: vec2<f32>, edges: i32) -> vec4<f32> { | |
| 83 | return sample_dod_edges(tex, fu, edges, vec4<f32>(0.0)); | |
| 84 | } | |
| 85 | ||
| 86 | fn read_mask_uv(uv: vec2<f32>, has_mask: f32, invert: f32) -> f32 { | |
| 87 | if (has_mask < 0.5) { | |
| 88 | return 1.0; | |
| 89 | } | |
| 90 | var m = 0.0; | |
| 91 | if (uv.x >= 0.0 && uv.x <= 1.0 && uv.y >= 0.0 && uv.y <= 1.0) { | |
| 92 | m = textureSampleLevel(mask_tex, samp_lin, uv, 0.0).a; | |
| 93 | } | |
| 94 | return select(m, 1.0 - m, invert > 0.5); | |
| 95 | } | |
| 96 | ||
| 97 | fn read_mask(gid: vec2<u32>, dims: vec2<u32>, has_mask: f32, invert: f32) -> f32 { | |
| 98 | return read_mask_uv((vec2<f32>(gid) + 0.5) / vec2<f32>(dims), has_mask, invert); | |
| 99 | } | |
| 100 | fn fg_hash(p: vec2<i32>, seed: i32) -> f32 { | |
| 101 | var n = u32(p.x * 374761393 + p.y * 668265263 + seed * 1442695041); | |
| 102 | n = (n ^ (n >> 13u)) * 1274126177u; | |
| 103 | n = n ^ (n >> 16u); | |
| 104 | return f32(n & 0x00ffffffu) / 16777216.0; | |
| 105 | } |
crates/engine/src/shaders/composite.wgsl created+506| ... | ... | @@ -0,0 +1,506 @@ |
| 1 | // ============================================================================ | |
| 2 | // 1. Background (Solid + Gradients) | |
| 3 | // p[0] = top_left rgba, p[1] = top_right rgba, p[2] = bottom_left rgba, p[3] = bottom_right rgba | |
| 4 | // p[4] = (type: 0=Solid, 1=Horiz, 2=Vert, 3=FourCorner, 4=Gradient, 5=Radial, sub_type, start_x, start_y) | |
| 5 | // p[5] = (end_x, end_y, 0, 0) | |
| 6 | // ============================================================================ | |
| 7 | @compute @workgroup_size(8, 8) | |
| 8 | fn bg_op(@builtin(global_invocation_id) gid: vec3<u32>) { | |
| 9 | let dims = textureDimensions(dst_tex); | |
| 10 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | |
| 11 | let fu = pix_to_fu(gid.xy, dims); | |
| 12 | let kind = i32(u.p[4].x + 0.5); | |
| 13 | var col = u.p[0]; | |
| 14 | if (kind == 1) { | |
| 15 | col = mix(u.p[0], u.p[1], fu.x); | |
| 16 | } else if (kind == 2) { | |
| 17 | col = mix(u.p[2], u.p[0], fu.y); | |
| 18 | } else if (kind == 3) { | |
| 19 | let top = mix(u.p[0], u.p[1], fu.x); | |
| 20 | let bot = mix(u.p[2], u.p[3], fu.x); | |
| 21 | col = mix(bot, top, fu.y); | |
| 22 | } else if (kind == 4 || kind == 5) { | |
| 23 | let s = u.p[4].zw; | |
| 24 | let e = u.p[5].xy; | |
| 25 | let d = e - s; | |
| 26 | var t = 0.0; | |
| 27 | if (kind == 5) { | |
| 28 | let r = max(length(d), 1e-5); | |
| 29 | t = clamp(length(fu - s) / r, 0.0, 1.0); | |
| 30 | } else { | |
| 31 | let len2 = max(dot(d, d), 1e-6); | |
| 32 | t = clamp(dot(fu - s, d) / len2, 0.0, 1.0); | |
| 33 | } | |
| 34 | col = mix(u.p[0], u.p[1], t); | |
| 35 | } | |
| 36 | let m = read_mask(gid.xy, dims, u.p[5].z, u.p[5].w); | |
| 37 | col = col * m; | |
| 38 | // Output premultiplied RGBA | |
| 39 | let out_col = vec4<f32>(col.rgb * col.a, col.a); | |
| 40 | store_dst(vec2<i32>(gid.xy), out_col); | |
| 41 | } | |
| 42 | ||
| 43 | ||
| 44 | // ============================================================================ | |
| 45 | // 2. Merge (Transform FG + ApplyMode + Gain + Blend + Mask) | |
| 46 | // p[0] = (center.x, center.y, size, angle_rad) | |
| 47 | // p[1] = (blend, gain, apply_mode_id, edges_mode) | |
| 48 | // p[2] = (flip_h, flip_v, fg_aspect, bg_aspect) | |
| 49 | // p[3] = (has_mask, mask_invert, fg_w, fg_h) | |
| 50 | // ============================================================================ | |
| 51 | fn blend_lum(c: vec3<f32>) -> f32 { | |
| 52 | return dot(c, vec3<f32>(0.299, 0.587, 0.114)); | |
| 53 | } | |
| 54 | ||
| 55 | fn clip_color(col_in: vec3<f32>) -> vec3<f32> { | |
| 56 | var c = col_in; | |
| 57 | let l = blend_lum(c); | |
| 58 | let n = min(c.r, min(c.g, c.b)); | |
| 59 | let x = max(c.r, max(c.g, c.b)); | |
| 60 | if (n < 0.0) { | |
| 61 | c = vec3<f32>(l) + ((c - vec3<f32>(l)) * l) / max(l - n, 1e-5); | |
| 62 | } | |
| 63 | if (x > 1.0) { | |
| 64 | c = vec3<f32>(l) + ((c - vec3<f32>(l)) * (1.0 - l)) / max(x - l, 1e-5); | |
| 65 | } | |
| 66 | return c; | |
| 67 | } | |
| 68 | ||
| 69 | fn set_lum(c: vec3<f32>, l: f32) -> vec3<f32> { | |
| 70 | let d = l - blend_lum(c); | |
| 71 | return clip_color(c + vec3<f32>(d)); | |
| 72 | } | |
| 73 | ||
| 74 | fn sat_val(c: vec3<f32>) -> f32 { | |
| 75 | return max(c.r, max(c.g, c.b)) - min(c.r, min(c.g, c.b)); | |
| 76 | } | |
| 77 | ||
| 78 | fn set_sat(c_in: vec3<f32>, s: f32) -> vec3<f32> { | |
| 79 | let mn = min(c_in.r, min(c_in.g, c_in.b)); | |
| 80 | let mx = max(c_in.r, max(c_in.g, c_in.b)); | |
| 81 | let span = mx - mn; | |
| 82 | if (span <= 1e-5) { | |
| 83 | return vec3<f32>(0.0); | |
| 84 | } | |
| 85 | return (c_in - vec3<f32>(mn)) * (s / span); | |
| 86 | } | |
| 87 | ||
| 88 | fn apply_blend_mode(bg: vec3<f32>, fg: vec3<f32>, mode: i32) -> vec3<f32> { | |
| 89 | switch mode { | |
| 90 | case 1: { // Screen | |
| 91 | return 1.0 - (1.0 - bg) * (1.0 - fg); | |
| 92 | } | |
| 93 | case 2: { // Multiply | |
| 94 | return bg * fg; | |
| 95 | } | |
| 96 | case 3: { // Darken | |
| 97 | return min(bg, fg); | |
| 98 | } | |
| 99 | case 4: { // Lighten | |
| 100 | return max(bg, fg); | |
| 101 | } | |
| 102 | case 5: { // Difference | |
| 103 | return abs(bg - fg); | |
| 104 | } | |
| 105 | case 6: { // Exclusion | |
| 106 | return bg + fg - 2.0 * bg * fg; | |
| 107 | } | |
| 108 | case 7: { // Overlay | |
| 109 | let lo = 2.0 * bg * fg; | |
| 110 | let hi = 1.0 - 2.0 * (1.0 - bg) * (1.0 - fg); | |
| 111 | return select(hi, lo, bg < vec3<f32>(0.5)); | |
| 112 | } | |
| 113 | case 8: { // Hard Light | |
| 114 | let lo = 2.0 * bg * fg; | |
| 115 | let hi = 1.0 - 2.0 * (1.0 - bg) * (1.0 - fg); | |
| 116 | return select(hi, lo, fg < vec3<f32>(0.5)); | |
| 117 | } | |
| 118 | case 9: { // Soft Light (W3C / Photoshop / Fusion exact) | |
| 119 | let d_lo = ((16.0 * bg - 12.0) * bg + 4.0) * bg; | |
| 120 | let d_hi = sqrt(max(bg, vec3<f32>(0.0))); | |
| 121 | let d = select(d_hi, d_lo, bg <= vec3<f32>(0.25)); | |
| 122 | let lo = bg - (1.0 - 2.0 * fg) * bg * (1.0 - bg); | |
| 123 | let hi = bg + (2.0 * fg - 1.0) * (d - bg); | |
| 124 | return select(hi, lo, fg <= vec3<f32>(0.5)); | |
| 125 | } | |
| 126 | case 10: { // Linear Light | |
| 127 | return clamp(bg + 2.0 * fg - 1.0, vec3<f32>(0.0), vec3<f32>(1.0)); | |
| 128 | } | |
| 129 | case 11: { // Color Dodge | |
| 130 | return clamp(bg / max(1.0 - fg, vec3<f32>(1e-4)), vec3<f32>(0.0), vec3<f32>(1.0)); | |
| 131 | } | |
| 132 | case 12: { // Linear Dodge / Add | |
| 133 | return clamp(bg + fg, vec3<f32>(0.0), vec3<f32>(1.0)); | |
| 134 | } | |
| 135 | case 13: { // Vivid Light | |
| 136 | let burn = 1.0 - (1.0 - bg) / max(2.0 * fg, vec3<f32>(1e-4)); | |
| 137 | let dodge = bg / max(2.0 * (1.0 - fg), vec3<f32>(1e-4)); | |
| 138 | return clamp(select(dodge, burn, fg < vec3<f32>(0.5)), vec3<f32>(0.0), vec3<f32>(1.0)); | |
| 139 | } | |
| 140 | case 14: { // Hypotenuse | |
| 141 | return clamp(sqrt(bg * bg + fg * fg), vec3<f32>(0.0), vec3<f32>(1.0)); | |
| 142 | } | |
| 143 | case 15: { // Darker Color (Fusion compares Rec.601 luma, picks darker pixel whole) | |
| 144 | let l_fg = dot(fg, vec3<f32>(0.299, 0.587, 0.114)); | |
| 145 | let l_bg = dot(bg, vec3<f32>(0.299, 0.587, 0.114)); | |
| 146 | return select(bg, fg, l_fg < l_bg); | |
| 147 | } | |
| 148 | case 16: { // Lighter Color (Fusion compares Rec.601 luma, picks lighter pixel whole) | |
| 149 | let l_fg = dot(fg, vec3<f32>(0.299, 0.587, 0.114)); | |
| 150 | let l_bg = dot(bg, vec3<f32>(0.299, 0.587, 0.114)); | |
| 151 | return select(bg, fg, l_fg > l_bg); | |
| 152 | } | |
| 153 | case 17: { // Pin Light | |
| 154 | let lo = min(bg, 2.0 * fg); | |
| 155 | let hi = max(bg, 2.0 * fg - 1.0); | |
| 156 | return select(hi, lo, fg < vec3<f32>(0.5)); | |
| 157 | } | |
| 158 | case 18: { // Color Burn | |
| 159 | return 1.0 - clamp((1.0 - bg) / max(fg, vec3<f32>(1e-4)), vec3<f32>(0.0), vec3<f32>(1.0)); | |
| 160 | } | |
| 161 | case 19: { // Linear Burn | |
| 162 | return clamp(bg + fg - 1.0, vec3<f32>(0.0), vec3<f32>(1.0)); | |
| 163 | } | |
| 164 | case 20: { // Geometric | |
| 165 | return (2.0 * fg * bg) / max(fg + bg, vec3<f32>(1e-5)); | |
| 166 | } | |
| 167 | case 21: { // Hue | |
| 168 | return set_lum(set_sat(fg, sat_val(bg)), blend_lum(bg)); | |
| 169 | } | |
| 170 | case 22: { // Saturation | |
| 171 | return set_lum(set_sat(bg, sat_val(fg)), blend_lum(bg)); | |
| 172 | } | |
| 173 | case 23: { // Color | |
| 174 | return set_lum(fg, blend_lum(bg)); | |
| 175 | } | |
| 176 | case 24: { // Luminosity | |
| 177 | return set_lum(bg, blend_lum(fg)); | |
| 178 | } | |
| 179 | default: { // 0: Normal | |
| 180 | return fg; | |
| 181 | } | |
| 182 | } | |
| 183 | } | |
| 184 | ||
| 185 | @compute @workgroup_size(8, 8) | |
| 186 | fn merge_op(@builtin(global_invocation_id) gid: vec3<u32>) { | |
| 187 | let dims = textureDimensions(dst_tex); | |
| 188 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | |
| 189 | let bg_dod = u.p[5]; | |
| 190 | let has_bg_dod = bg_dod.z > 0.5 && bg_dod.w > 0.5; | |
| 191 | let bg_dims = select(vec2<f32>(dims), bg_dod.zw, has_bg_dod); | |
| 192 | let fg_dims = max(u.p[3].zw, vec2<f32>(1.0)); | |
| 193 | ||
| 194 | let bg_off = vec2<i32>(i32(round(u.p[6].z)), i32(round(u.p[6].w))); | |
| 195 | let bg_coord = vec2<i32>(gid.xy) - bg_off; | |
| 196 | let bg_tex_dims = vec2<i32>(textureDimensions(src_a)); | |
| 197 | var bg = vec4<f32>(0.0); | |
| 198 | if (bg_coord.x >= 0 && bg_coord.y >= 0 && bg_coord.x < bg_tex_dims.x && bg_coord.y < bg_tex_dims.y) { | |
| 199 | bg = textureLoad(src_a, bg_coord, 0); | |
| 200 | } | |
| 201 | // If BG has expanded DoD, map output texel into logical BG canvas Fusion UV! | |
| 202 | let canvas_px = select( | |
| 203 | vec2<f32>(gid.xy) + vec2<f32>(0.5), | |
| 204 | vec2<f32>(gid.xy) + vec2<f32>(0.5) + bg_dod.xy, | |
| 205 | has_bg_dod, | |
| 206 | ); | |
| 207 | let fu = vec2<f32>(canvas_px.x / bg_dims.x, 1.0 - canvas_px.y / bg_dims.y); | |
| 208 | ||
| 209 | let center = u.p[0].xy; | |
| 210 | let size = max(u.p[0].z, 1e-5); | |
| 211 | let angle = u.p[0].w; | |
| 212 | let blend = u.p[1].x; | |
| 213 | let gain = u.p[1].y; | |
| 214 | let mode = i32(u.p[1].z + 0.5); | |
| 215 | let edges = i32(u.p[1].w + 0.5); | |
| 216 | let nearest = u.p[2].z > 0.5; | |
| 217 | ||
| 218 | // Map output pixel in Fusion pixel space relative to Center into FG pixel space. | |
| 219 | let delta_px = (fu - center) * bg_dims; | |
| 220 | let c = cos(angle); | |
| 221 | let s = sin(angle); | |
| 222 | // Fusion Angle rotates CCW in Y-up, so inverse rotates CW: | |
| 223 | let rot_px = vec2<f32>(delta_px.x * c + delta_px.y * s, -delta_px.x * s + delta_px.y * c) / size; | |
| 224 | var fg_fu = rot_px / fg_dims + vec2<f32>(0.5); | |
| 225 | if (u.p[2].x > 0.5) { fg_fu.x = 1.0 - fg_fu.x; } | |
| 226 | if (u.p[2].y > 0.5) { fg_fu.y = 1.0 - fg_fu.y; } | |
| 227 | ||
| 228 | let fg = sample_dod_edges_mode(src_b, fg_fu, edges, u.p[4], nearest); | |
| 229 | let mask = read_mask_uv(canvas_px / bg_dims, u.p[3].x, u.p[3].y); | |
| 230 | let eff_blend = clamp(blend * mask, 0.0, 1.0); | |
| 231 | let op = i32(u.p[2].w + 0.5); // 0=Over, 1=Under, 2=In, 3=HeldOut, 4=Atop, 5=XOr | |
| 232 | ||
| 233 | let burn_in = u.p[6].x; | |
| 234 | ||
| 235 | var out_col: vec4<f32>; | |
| 236 | if (mode == 0) { | |
| 237 | let fg_occ = fg.a * gain * (1.0 - burn_in); | |
| 238 | var comp: vec4<f32>; | |
| 239 | if (op == 1) { | |
| 240 | // Under: FG behind BG | |
| 241 | let bg_occ = bg.a; | |
| 242 | comp = vec4<f32>(bg.rgb + fg.rgb * (1.0 - bg_occ), bg.a + fg.a * (1.0 - bg_occ)); | |
| 243 | } else if (op == 2) { | |
| 244 | // In: FG masked by BG alpha | |
| 245 | comp = fg * bg.a; | |
| 246 | } else if (op == 3) { | |
| 247 | // HeldOut: BG masked by (1 - FG alpha) | |
| 248 | comp = bg * (1.0 - fg_occ); | |
| 249 | } else if (op == 4) { | |
| 250 | // Atop: FG inside BG + BG outside FG | |
| 251 | comp = vec4<f32>(fg.rgb * bg.a + bg.rgb * (1.0 - fg_occ), bg.a); | |
| 252 | } else if (op == 5) { | |
| 253 | // XOr | |
| 254 | comp = fg * (1.0 - bg.a) + bg * (1.0 - fg_occ); | |
| 255 | } else { | |
| 256 | // Over (default) with Fusion Alpha Gain, Subtractive/Additive, and BurnIn | |
| 257 | let over_rgb = fg.rgb + bg.rgb * (1.0 - fg_occ); | |
| 258 | let over_a = fg.a * gain + bg.a * (1.0 - fg.a * gain); | |
| 259 | comp = vec4<f32>(over_rgb, over_a); | |
| 260 | } | |
| 261 | out_col = mix(bg, comp, eff_blend); | |
| 262 | } else if (mode == 15 || mode == 16) { | |
| 263 | // Fusion DarkerColor/LighterColor with AlphaGain: candidate is FG + BG*(1 - fg.a*gain), | |
| 264 | // compared against BG by Rec.601 luma! (Verified bit-exact on probe_merge f09 & f10!) | |
| 265 | let fg_a_eff = fg.a * gain; | |
| 266 | let bg_u = select(vec3<f32>(0.0), bg.rgb / max(bg.a, 1e-5), bg.a > 1e-5); | |
| 267 | let fg_u = select(vec3<f32>(0.0), fg.rgb / max(fg.a, 1e-5), fg.a > 1e-5); | |
| 268 | let cand = fg_u + bg_u * (1.0 - fg_a_eff); | |
| 269 | let blended = apply_blend_mode(bg_u, cand, mode); | |
| 270 | let comp_rgb = bg.a * fg.a * blended + fg.rgb * (1.0 - bg.a) + (1.0 - fg.a) * bg.rgb; | |
| 271 | let comp_a = bg.a + fg_a_eff * (1.0 - bg.a); | |
| 272 | out_col = mix(bg, vec4<f32>(comp_rgb, comp_a), eff_blend); | |
| 273 | } else { | |
| 274 | let bg_u = select(vec3<f32>(0.0), bg.rgb / max(bg.a, 1e-5), bg.a > 1e-5); | |
| 275 | let fg_u = select(vec3<f32>(0.0), fg.rgb / max(fg.a, 1e-5), fg.a > 1e-5); | |
| 276 | // In Fusion's special fill-opacity modes (Difference, Exclusion, Screen, LinearDodge), | |
| 277 | // Gain scales FG color intensity BEFORE blending (e.g. Difference = |bg - gain*fg|!). | |
| 278 | let scale_fg_rgb = (mode == 5 || mode == 6 || mode == 1 || mode == 12); | |
| 279 | let fg_blend_in = select(fg_u, fg_u * gain, scale_fg_rgb); | |
| 280 | let blended = apply_blend_mode(bg_u, fg_blend_in, mode); | |
| 281 | let fg_eff = select(fg.a * gain, fg.a, scale_fg_rgb); | |
| 282 | let comp_rgb = fg_eff * bg.a * blended + fg_eff * (1.0 - bg.a) * fg_blend_in + (1.0 - fg_eff) * bg.rgb; | |
| 283 | let comp_a = bg.a + fg_eff * (1.0 - bg.a); | |
| 284 | out_col = mix(bg, vec4<f32>(comp_rgb, comp_a), eff_blend); | |
| 285 | } | |
| 286 | store_dst(vec2<i32>(gid.xy), out_col); | |
| 287 | } | |
| 288 | // ============================================================================ | |
| 289 | // 16. Blit / Scale (source -> destination of any size, premultiplied) | |
| 290 | // p[0] = (exposure_mul, gamma, channel_mode: 0=RGBA,1=R,2=G,3=B,4=A, checker_bg) | |
| 291 | // ============================================================================ | |
| 292 | @compute @workgroup_size(8, 8) | |
| 293 | fn display_blit_op(@builtin(global_invocation_id) gid: vec3<u32>) { | |
| 294 | let dims = textureDimensions(dst_tex); | |
| 295 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | |
| 296 | let t = (vec2<f32>(gid.xy) + 0.5) / vec2<f32>(dims); | |
| 297 | let has_uv = (u.p[1].z > u.p[1].x) || (u.p[1].w > u.p[1].y); | |
| 298 | let uv = select( | |
| 299 | t, | |
| 300 | vec2<f32>(mix(u.p[1].x, u.p[1].z, t.x), mix(u.p[1].y, u.p[1].w, t.y)), | |
| 301 | has_uv, | |
| 302 | ); | |
| 303 | let has_dod = u.p[4].z > 0.5 && u.p[4].w > 0.5; | |
| 304 | let sdims = vec2<f32>(textureDimensions(src_a)); | |
| 305 | let sample_uv = select( | |
| 306 | uv, | |
| 307 | (vec2<f32>(uv.x * u.p[4].z, uv.y * u.p[4].w) - u.p[4].xy) / sdims, | |
| 308 | has_dod, | |
| 309 | ); | |
| 310 | let nearest = u.p[2].x > 0.5; | |
| 311 | var src: vec4<f32>; | |
| 312 | if (sample_uv.x < 0.0 || sample_uv.x >= 1.0 || sample_uv.y < 0.0 || sample_uv.y >= 1.0) { | |
| 313 | src = vec4<f32>(0.0); | |
| 314 | } else if (nearest) { | |
| 315 | let isdims = vec2<i32>(textureDimensions(src_a)); | |
| 316 | let sx = clamp(i32(floor(sample_uv.x * f32(isdims.x))), 0, isdims.x - 1); | |
| 317 | let sy = clamp(i32(floor(sample_uv.y * f32(isdims.y))), 0, isdims.y - 1); | |
| 318 | src = textureLoad(src_a, vec2<i32>(sx, sy), 0); | |
| 319 | } else { | |
| 320 | src = textureSampleLevel(src_a, samp_lin, sample_uv, 0.0); | |
| 321 | } | |
| 322 | let exp_mul = u.p[0].x; | |
| 323 | let inv_gamma = 1.0 / max(u.p[0].y, 1e-4); | |
| 324 | let ch = i32(u.p[0].z + 0.5); | |
| 325 | let checker = u.p[0].w > 0.5; | |
| 326 | ||
| 327 | var out_col = src; | |
| 328 | if (ch == 1) { | |
| 329 | out_col = vec4<f32>(src.r, src.r, src.r, 1.0); | |
| 330 | } else if (ch == 2) { | |
| 331 | out_col = vec4<f32>(src.g, src.g, src.g, 1.0); | |
| 332 | } else if (ch == 3) { | |
| 333 | out_col = vec4<f32>(src.b, src.b, src.b, 1.0); | |
| 334 | } else if (ch == 4) { | |
| 335 | out_col = vec4<f32>(src.a, src.a, src.a, 1.0); | |
| 336 | } else { | |
| 337 | var rgb = pow(max(src.rgb * exp_mul, vec3<f32>(0.0)), vec3<f32>(inv_gamma)); | |
| 338 | if (checker) { | |
| 339 | let cx = (gid.x / 16u) & 1u; | |
| 340 | let cy = (gid.y / 16u) & 1u; | |
| 341 | let bg = select(0.14, 0.20, (cx ^ cy) == 1u); | |
| 342 | rgb = rgb + vec3<f32>(bg) * (1.0 - src.a); | |
| 343 | out_col = vec4<f32>(rgb, 1.0); | |
| 344 | } else { | |
| 345 | out_col = vec4<f32>(rgb, src.a); | |
| 346 | } | |
| 347 | } | |
| 348 | textureStore(dst_tex, vec2<i32>(gid.xy), clamp(out_col, vec4<f32>(0.0), vec4<f32>(1.0))); | |
| 349 | } | |
| 350 | // ============================================================================ | |
| 351 | // 17b. ChannelBoolean (full Fusion Operation + per-channel ToRed/Green/Blue/Alpha) | |
| 352 | // src_a = Background, src_b = Foreground (or Background if unconnected) | |
| 353 | // p[0] = (operation, to_r, to_g, to_b) | |
| 354 | // p[1] = (to_a, blend, has_mask, mask_invert) | |
| 355 | // ============================================================================ | |
| 356 | fn cb_pick(ch: i32, bg: vec4<f32>, fg: vec4<f32>, bg_ch: f32) -> f32 { | |
| 357 | switch ch { | |
| 358 | case 0: { return fg.r; } | |
| 359 | case 1: { return fg.g; } | |
| 360 | case 2: { return fg.b; } | |
| 361 | case 3: { return fg.a; } | |
| 362 | case 4: { return bg_ch; } // Do Nothing | |
| 363 | case 5: { return bg.r; } | |
| 364 | case 6: { return bg.g; } | |
| 365 | case 7: { return bg.b; } | |
| 366 | case 8: { return bg.a; } | |
| 367 | case 9: { return dot(bg.rgb, vec3<f32>(0.299, 0.587, 0.114)); } // Luma BG | |
| 368 | case 10: { return dot(fg.rgb, vec3<f32>(0.299, 0.587, 0.114)); } // Luma FG | |
| 369 | case 15: { return 0.0; } // Black / Clear | |
| 370 | case 16: { return 1.0; } // White / Solid | |
| 371 | default: { return fg.r; } | |
| 372 | } | |
| 373 | } | |
| 374 | ||
| 375 | fn cb_apply_op(op: i32, bg_v: f32, fg_v: f32) -> f32 { | |
| 376 | switch op { | |
| 377 | case 0: { return fg_v; } // Copy | |
| 378 | case 1: { return bg_v + fg_v; } // Add | |
| 379 | case 2: { return bg_v - fg_v; } // Subtract | |
| 380 | case 6: { return bg_v * fg_v; } // Multiply | |
| 381 | case 7: { return bg_v / max(fg_v, 1e-5); } // Divide | |
| 382 | case 8: { return max(bg_v, fg_v); } // Maximum | |
| 383 | case 9: { return min(bg_v, fg_v); } // Minimum | |
| 384 | case 10: { return 1.0 - fg_v; } // Negative | |
| 385 | case 11: { return 1.0; } // Solid | |
| 386 | case 12: { return 0.0; } // Clear | |
| 387 | case 13: { return abs(bg_v - fg_v); } // Difference | |
| 388 | default: { return fg_v; } | |
| 389 | } | |
| 390 | } | |
| 391 | ||
| 392 | @compute @workgroup_size(8, 8) | |
| 393 | fn channel_boolean_op(@builtin(global_invocation_id) gid: vec3<u32>) { | |
| 394 | let dims = textureDimensions(dst_tex); | |
| 395 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | |
| 396 | let bg = textureLoad(src_a, vec2<i32>(gid.xy), 0); | |
| 397 | let bg_dod = u.p[3]; | |
| 398 | let has_bg_dod = bg_dod.z > 0.5 && bg_dod.w > 0.5; | |
| 399 | let bg_dims = select(vec2<f32>(dims), bg_dod.zw, has_bg_dod); | |
| 400 | let canvas_px = select( | |
| 401 | vec2<f32>(gid.xy) + vec2<f32>(0.5), | |
| 402 | vec2<f32>(gid.xy) + vec2<f32>(0.5) + bg_dod.xy, | |
| 403 | has_bg_dod, | |
| 404 | ); | |
| 405 | let fu = vec2<f32>(canvas_px.x / bg_dims.x, 1.0 - canvas_px.y / bg_dims.y); | |
| 406 | let fg = sample_dod_edges_mode(src_b, fu, 0, u.p[2], false); | |
| 407 | ||
| 408 | let op = i32(u.p[0].x + 0.5); | |
| 409 | let to_r = i32(u.p[0].y + 0.5); | |
| 410 | let to_g = i32(u.p[0].z + 0.5); | |
| 411 | let to_b = i32(u.p[0].w + 0.5); | |
| 412 | let to_a = i32(u.p[1].x + 0.5); | |
| 413 | ||
| 414 | let vr = select(cb_apply_op(op, bg.r, cb_pick(to_r, bg, fg, bg.r)), bg.r, to_r == 4); | |
| 415 | let vg = select(cb_apply_op(op, bg.g, cb_pick(to_g, bg, fg, bg.g)), bg.g, to_g == 4); | |
| 416 | let vb = select(cb_apply_op(op, bg.b, cb_pick(to_b, bg, fg, bg.b)), bg.b, to_b == 4); | |
| 417 | let va = select(cb_apply_op(op, bg.a, cb_pick(to_a, bg, fg, bg.a)), bg.a, to_a == 4); | |
| 418 | ||
| 419 | let res = clamp(vec4<f32>(vr, vg, vb, va), vec4<f32>(0.0), vec4<f32>(1.0)); | |
| 420 | let m = read_mask_uv(canvas_px / bg_dims, u.p[1].z, u.p[1].w) * u.p[1].y; | |
| 421 | let out_col = mix(bg, res, clamp(m, 0.0, 1.0)); | |
| 422 | store_dst(vec2<i32>(gid.xy), out_col); | |
| 423 | } | |
| 424 | // ============================================================================ | |
| 425 | // 27. Dissolve (linear cross-dissolve between src_a=BG and src_b=FG) | |
| 426 | // p[0] = (mix_v, blend, has_mask, mask_invert) | |
| 427 | // ============================================================================ | |
| 428 | @compute @workgroup_size(8, 8) | |
| 429 | fn dissolve_op(@builtin(global_invocation_id) gid: vec3<u32>) { | |
| 430 | let dims = textureDimensions(dst_tex); | |
| 431 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | |
| 432 | let bg = textureLoad(src_a, vec2<i32>(gid.xy), 0); | |
| 433 | let uv = (vec2<f32>(gid.xy) + 0.5) / vec2<f32>(dims); | |
| 434 | let fg = textureSampleLevel(src_b, samp_lin, uv, 0.0); | |
| 435 | let res = mix(bg, fg, clamp(u.p[0].x, 0.0, 1.0)); | |
| 436 | let m = read_mask(gid.xy, dims, u.p[0].z, u.p[0].w) * u.p[0].y; | |
| 437 | let out_col = mix(bg, res, clamp(m, 0.0, 1.0)); | |
| 438 | store_dst(vec2<i32>(gid.xy), out_col); | |
| 439 | } | |
| 440 | // ============================================================================ | |
| 441 | // 29. Shadow Composite (src_a = orig, src_b = blurred shifted alpha) | |
| 442 | // p[0] = (sh_r, sh_g, sh_b, sh_a) | |
| 443 | // p[1] = (shadow_only, blend, has_mask, mask_invert) | |
| 444 | // ============================================================================ | |
| 445 | @compute @workgroup_size(8, 8) | |
| 446 | fn shadow_comp_op(@builtin(global_invocation_id) gid: vec3<u32>) { | |
| 447 | let dims = textureDimensions(dst_tex); | |
| 448 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | |
| 449 | let orig = textureLoad(src_a, vec2<i32>(gid.xy), 0); | |
| 450 | let sh_mask = textureLoad(src_b, vec2<i32>(gid.xy), 0).a; | |
| 451 | let sa = clamp(sh_mask * u.p[0].w, 0.0, 1.0); | |
| 452 | let sh_col = vec4<f32>(u.p[0].rgb * sa, sa); | |
| 453 | var res: vec4<f32>; | |
| 454 | if (u.p[1].x > 0.5) { | |
| 455 | res = sh_col; | |
| 456 | } else { | |
| 457 | // Original OVER shadow | |
| 458 | res = vec4<f32>(orig.rgb + sh_col.rgb * (1.0 - orig.a), orig.a + sh_col.a * (1.0 - orig.a)); | |
| 459 | } | |
| 460 | let m = read_mask(gid.xy, dims, u.p[1].z, u.p[1].w) * u.p[1].y; | |
| 461 | let out_col = mix(orig, res, clamp(m, 0.0, 1.0)); | |
| 462 | store_dst(vec2<i32>(gid.xy), out_col); | |
| 463 | } | |
| 464 | // ============================================================================ | |
| 465 | // 32. SetDomain (adjusts/clamps Domain of Definition; zero-pads outside domain) | |
| 466 | // p[0] = (left_uv, bottom_uv, right_uv, top_uv) in canvas Fusion UV | |
| 467 | // p[1] = (dst_origin_x, dst_origin_y, canvas_w, canvas_h) | |
| 468 | // p[2] = (src_origin_x, src_origin_y, src_w, src_h) | |
| 469 | // ============================================================================ | |
| 470 | @compute @workgroup_size(8, 8) | |
| 471 | fn set_domain_op(@builtin(global_invocation_id) gid: vec3<u32>) { | |
| 472 | let dims = textureDimensions(dst_tex); | |
| 473 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | |
| 474 | let canvas_w = max(u.p[1].z, 1.0); | |
| 475 | let canvas_h = max(u.p[1].w, 1.0); | |
| 476 | let canvas_px = vec2<f32>(f32(gid.x) + u.p[1].x, f32(gid.y) + u.p[1].y); | |
| 477 | let fu = vec2<f32>((canvas_px.x + 0.5) / canvas_w, 1.0 - (canvas_px.y + 0.5) / canvas_h); | |
| 478 | ||
| 479 | var out_col = vec4<f32>(0.0); | |
| 480 | if (fu.x >= u.p[0].x && fu.x <= u.p[0].z && fu.y >= u.p[0].y && fu.y <= u.p[0].w) { | |
| 481 | let spx = vec2<i32>(floor(canvas_px - u.p[2].xy + vec2<f32>(0.5))); | |
| 482 | let sdims = vec2<i32>(textureDimensions(src_a)); | |
| 483 | if (spx.x >= 0 && spx.x < sdims.x && spx.y >= 0 && spx.y < sdims.y) { | |
| 484 | out_col = textureLoad(src_a, spx, 0); | |
| 485 | } | |
| 486 | } | |
| 487 | store_dst(vec2<i32>(gid.xy), out_col); | |
| 488 | } | |
| 489 | ||
| 490 | ||
| 491 | // ============================================================================ | |
| 492 | // 33. ChangeDepth (converts texture between int8 / int16 / float16 / float32) | |
| 493 | // p[0] = (dither_enabled, 0, 0, 0) | |
| 494 | // ============================================================================ | |
| 495 | @compute @workgroup_size(8, 8) | |
| 496 | fn change_depth_op(@builtin(global_invocation_id) gid: vec3<u32>) { | |
| 497 | let dims = textureDimensions(dst_tex); | |
| 498 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | |
| 499 | let p = vec2<i32>(gid.xy); | |
| 500 | var col = textureLoad(src_a, p, 0); | |
| 501 | if (u.p[0].x > 0.5 && !HDR_FLOAT) { | |
| 502 | let d = (fg_hash(p, 11) + fg_hash(p, 29) - 1.0) / 255.0; | |
| 503 | col = vec4<f32>(col.rgb + vec3<f32>(d), col.a); | |
| 504 | } | |
| 505 | store_dst(p, col); | |
| 506 | } |
crates/engine/src/shaders/keyers_gen.wgsl created+363| ... | ... | @@ -0,0 +1,363 @@ |
| 1 | // ============================================================================ | |
| 2 | // 6. LumaKeyer | |
| 3 | // p[0] = (low, high, invert, blend) | |
| 4 | // p[1] = (has_mask, mask_invert, 0, 0) | |
| 5 | // ============================================================================ | |
| 6 | @compute @workgroup_size(8, 8) | |
| 7 | fn luma_key_op(@builtin(global_invocation_id) gid: vec3<u32>) { | |
| 8 | let dims = textureDimensions(dst_tex); | |
| 9 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | |
| 10 | let src = textureLoad(src_a, vec2<i32>(gid.xy), 0); | |
| 11 | let low = u.p[0].x; | |
| 12 | let high = u.p[0].y; | |
| 13 | let invert = u.p[0].z > 0.5; | |
| 14 | let rgb = select(vec3<f32>(0.0), src.rgb / max(src.a, 1e-5), src.a > 1e-5); | |
| 15 | let luma = dot(rgb, vec3<f32>(0.299, 0.587, 0.114)); | |
| 16 | var a = clamp((luma - low) / max(high - low, 1e-5), 0.0, 1.0); | |
| 17 | if (invert) { | |
| 18 | a = 1.0 - a; | |
| 19 | } | |
| 20 | let out_a = a * src.a; | |
| 21 | let keyed = vec4<f32>(rgb * out_a, out_a); | |
| 22 | let m = read_mask(gid.xy, dims, u.p[1].x, u.p[1].y) * u.p[0].w; | |
| 23 | let out_col = mix(src, keyed, clamp(m, 0.0, 1.0)); | |
| 24 | store_dst(vec2<i32>(gid.xy), out_col); | |
| 25 | } | |
| 26 | // ============================================================================ | |
| 27 | // 8. FastNoise (3D Perlin Gradient FBM with Seethe, Angle, Discontinuous & LUT) | |
| 28 | // p[0] = (x_scale, y_scale, detail, contrast) | |
| 29 | // p[1] = (brightness, seethe, discontinuous, center_x) | |
| 30 | // p[2] = (center_y, angle_rad, inverted, use_lut) | |
| 31 | // ============================================================================ | |
| 32 | fn grad3_dot(i: vec3<f32>, f: vec3<f32>) -> f32 { | |
| 33 | // Deterministic 3D gradient hash -> 12 Perlin cube-edge directions (smooth isotropic blobs!) | |
| 34 | var p3 = fract(i * vec3<f32>(0.1031, 0.1030, 0.0973)); | |
| 35 | p3 = p3 + dot(p3, p3.yxz + 33.33); | |
| 36 | let h = u32(floor(fract((p3.x + p3.y) * p3.z) * 16.0)) & 15u; | |
| 37 | let u_v = select(f.y, f.x, h < 8u); | |
| 38 | let v_v = select(select(f.z, f.x, h == 12u || h == 14u), f.y, h < 4u); | |
| 39 | let s1 = select(-u_v, u_v, (h & 1u) == 0u); | |
| 40 | let s2 = select(-v_v, v_v, (h & 2u) == 0u); | |
| 41 | return s1 + s2; | |
| 42 | } | |
| 43 | ||
| 44 | fn noise3d(p: vec3<f32>) -> f32 { | |
| 45 | let i = floor(p); | |
| 46 | let f = fract(p); | |
| 47 | // Quintic C2 Perlin fade: eliminates blocky axis-aligned artifacts on thresholded peaks | |
| 48 | let u_f = f * f * f * (f * (f * 6.0 - 15.0) + 10.0); | |
| 49 | let n000 = grad3_dot(i + vec3<f32>(0.0, 0.0, 0.0), f - vec3<f32>(0.0, 0.0, 0.0)); | |
| 50 | let n100 = grad3_dot(i + vec3<f32>(1.0, 0.0, 0.0), f - vec3<f32>(1.0, 0.0, 0.0)); | |
| 51 | let n010 = grad3_dot(i + vec3<f32>(0.0, 1.0, 0.0), f - vec3<f32>(0.0, 1.0, 0.0)); | |
| 52 | let n110 = grad3_dot(i + vec3<f32>(1.0, 1.0, 0.0), f - vec3<f32>(1.0, 1.0, 0.0)); | |
| 53 | let n001 = grad3_dot(i + vec3<f32>(0.0, 0.0, 1.0), f - vec3<f32>(0.0, 0.0, 1.0)); | |
| 54 | let n101 = grad3_dot(i + vec3<f32>(1.0, 0.0, 1.0), f - vec3<f32>(1.0, 0.0, 1.0)); | |
| 55 | let n011 = grad3_dot(i + vec3<f32>(0.0, 1.0, 1.0), f - vec3<f32>(0.0, 1.0, 1.0)); | |
| 56 | let n111 = grad3_dot(i + vec3<f32>(1.0, 1.0, 1.0), f - vec3<f32>(1.0, 1.0, 1.0)); | |
| 57 | let nx00 = mix(n000, n100, u_f.x); | |
| 58 | let nx10 = mix(n010, n110, u_f.x); | |
| 59 | let nx01 = mix(n001, n101, u_f.x); | |
| 60 | let nx11 = mix(n011, n111, u_f.x); | |
| 61 | let nxy0 = mix(nx00, nx10, u_f.y); | |
| 62 | let nxy1 = mix(nx01, nx11, u_f.y); | |
| 63 | return mix(nxy0, nxy1, u_f.z); | |
| 64 | } | |
| 65 | ||
| 66 | @compute @workgroup_size(8, 8) | |
| 67 | fn fast_noise_op(@builtin(global_invocation_id) gid: vec3<u32>) { | |
| 68 | let dims = textureDimensions(dst_tex); | |
| 69 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | |
| 70 | let aspect = f32(dims.x) / max(f32(dims.y), 1.0); | |
| 71 | let fu = pix_to_fu(gid.xy, dims); | |
| 72 | let center = vec2<f32>(u.p[1].w, u.p[2].x); | |
| 73 | let scale = u.p[0].xy; | |
| 74 | let detail = clamp(u.p[0].z, 0.0, 10.0); | |
| 75 | let contrast = u.p[0].w; | |
| 76 | let brightness = u.p[1].x; | |
| 77 | let seethe = u.p[1].y; | |
| 78 | let disc = u.p[1].z > 0.5; | |
| 79 | let angle = u.p[2].y; | |
| 80 | ||
| 81 | // Fusion FastNoise uses frame-width isotropic units `(fu - center) * vec2(1, 1/aspect)` | |
| 82 | var rel = (fu - center) * vec2<f32>(1.0, 1.0 / aspect); | |
| 83 | if (abs(angle) > 1e-5) { | |
| 84 | let ca = cos(angle); | |
| 85 | let sa = sin(angle); | |
| 86 | rel = vec2<f32>(rel.x * ca - rel.y * sa, rel.x * sa + rel.y * ca); | |
| 87 | } | |
| 88 | let p = rel * scale; | |
| 89 | ||
| 90 | var val = 0.5 + brightness; | |
| 91 | if (detail > 1e-4) { | |
| 92 | let octaves = i32(ceil(detail)); | |
| 93 | var sum = 0.0; | |
| 94 | var norm = 0.0; | |
| 95 | var amp = 1.0; | |
| 96 | var freq = 1.0; | |
| 97 | for (var i = 0; i < 10; i = i + 1) { | |
| 98 | if (i >= octaves) { break; } | |
| 99 | let weight = select(amp, amp * fract(detail), i == octaves - 1 && fract(detail) > 1e-4); | |
| 100 | let n = noise3d(vec3<f32>(p * freq, seethe + f32(i) * 1.73)); | |
| 101 | sum = sum + select(n, abs(n), disc) * weight; | |
| 102 | norm = norm + weight; | |
| 103 | freq = freq * 2.0; | |
| 104 | amp = amp * 0.5; | |
| 105 | } | |
| 106 | let n_avg = sum / max(norm, 1.0); | |
| 107 | // Perlin3D normalized fBm: at Detail=1 (norm=1), *0.41 matches Fusion std=0.106 [0.24, 0.76]; | |
| 108 | // at Detail=10 (norm=2), std naturally narrows to ~0.060 matching Fusion's pinpoint starfield threshold! | |
| 109 | // Discontinuous mode takes |n| turbulence (~1.60x -> mean 0.34, std 0.14 in Type=0 TwoColor), | |
| 110 | // pivoting Contrast around 0.5 when Contrast != 1 (verified via probe_moon_3d f02 & displace_wave f05/f07/f08). | |
| 111 | if (disc) { | |
| 112 | let grad_bias = select(0.0, 0.25, u.p[2].w > 1.5); | |
| 113 | let base = n_avg * 1.60 + grad_bias; | |
| 114 | val = (base - 0.5) * contrast + 0.5 + brightness; | |
| 115 | } else { | |
| 116 | val = n_avg * 0.41 * contrast + 0.5 + brightness; | |
| 117 | } | |
| 118 | } | |
| 119 | val = clamp(val, 0.0, 1.0); | |
| 120 | if (u.p[2].z > 0.5) { val = 1.0 - val; } | |
| 121 | var col: vec4<f32>; | |
| 122 | if (u.p[2].w > 0.5) { | |
| 123 | // High-precision 256x1 gradient LUT in src_b (supports razor-sharp thresholds like starfields!) | |
| 124 | col = textureSampleLevel(src_b, samp_lin, vec2<f32>(val, 0.5), 0.0); | |
| 125 | } else { | |
| 126 | let lut_pos = val * 12.0; | |
| 127 | let i0 = u32(clamp(floor(lut_pos), 0.0, 11.0)); | |
| 128 | let f = lut_pos - f32(i0); | |
| 129 | col = mix(u.p[3u + i0], u.p[4u + i0], f); | |
| 130 | } | |
| 131 | let out_col = vec4<f32>(col.rgb * col.a, col.a); | |
| 132 | store_dst(vec2<i32>(gid.xy), out_col); | |
| 133 | } | |
| 134 | // ============================================================================ | |
| 135 | // 12. Fuse.Outline (Clover's exact 2020 Outline.fuse passes!) | |
| 136 | // outline_boost: boosts alpha * 120 (clamped in R/A via normalized scale, so we pass scale factor!) | |
| 137 | // outline_finish: clamps dilated alpha, tints with outline color, then composites original on top! | |
| 138 | // ============================================================================ | |
| 139 | @compute @workgroup_size(8, 8) | |
| 140 | fn outline_boost(@builtin(global_invocation_id) gid: vec3<u32>) { | |
| 141 | let dims = textureDimensions(dst_tex); | |
| 142 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | |
| 143 | let src = textureLoad(src_a, vec2<i32>(gid.xy), 0); | |
| 144 | let a = clamp(src.a * 120.0, 0.0, 1.0); | |
| 145 | textureStore(dst_tex, vec2<i32>(gid.xy), vec4<f32>(a, a, a, a)); | |
| 146 | } | |
| 147 | ||
| 148 | @compute @workgroup_size(8, 8) | |
| 149 | fn outline_tint(@builtin(global_invocation_id) gid: vec3<u32>) { | |
| 150 | let dims = textureDimensions(dst_tex); | |
| 151 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | |
| 152 | let blurred = textureLoad(src_a, vec2<i32>(gid.xy), 0); | |
| 153 | let col = u.p[0]; // (r, g, b, a) | |
| 154 | // Because we clamped boosted alpha to 1.0 in u8, emulate the 120x dilation threshold | |
| 155 | let a = clamp(blurred.a * u.p[1].x, 0.0, 1.0) * col.a; | |
| 156 | textureStore(dst_tex, vec2<i32>(gid.xy), vec4<f32>(col.rgb * a, a)); | |
| 157 | } | |
| 158 | ||
| 159 | @compute @workgroup_size(8, 8) | |
| 160 | fn outline_composite(@builtin(global_invocation_id) gid: vec3<u32>) { | |
| 161 | let dims = textureDimensions(dst_tex); | |
| 162 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | |
| 163 | let outline = textureLoad(src_a, vec2<i32>(gid.xy), 0); | |
| 164 | let orig = textureLoad(src_b, vec2<i32>(gid.xy), 0); | |
| 165 | // Original over outline | |
| 166 | let out_rgb = orig.rgb + outline.rgb * (1.0 - orig.a); | |
| 167 | let out_a = orig.a + outline.a * (1.0 - orig.a); | |
| 168 | store_dst(vec2<i32>(gid.xy), vec4<f32>(out_rgb, out_a)); | |
| 169 | } | |
| 170 | ||
| 171 | // ============================================================================ | |
| 172 | // 12c. TextPlus Circular Euclidean L2 Outline + Fill-Over-Outline Composite | |
| 173 | // p[0] = (outline_r, outline_g, outline_b, outline_a) | |
| 174 | // p[1] = (radius_px, 0, 0, 0) | |
| 175 | // ============================================================================ | |
| 176 | @compute @workgroup_size(8, 8) | |
| 177 | fn text_outline_op(@builtin(global_invocation_id) gid: vec3<u32>) { | |
| 178 | let dims = textureDimensions(dst_tex); | |
| 179 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | |
| 180 | let p = vec2<i32>(gid.xy); | |
| 181 | let orig = textureLoad(src_a, p, 0); | |
| 182 | if (orig.a >= 0.999) { | |
| 183 | textureStore(dst_tex, p, orig); | |
| 184 | return; | |
| 185 | } | |
| 186 | ||
| 187 | let radius = clamp(u.p[1].x, 0.5, 28.0); | |
| 188 | let r_int = i32(ceil(radius + 0.5)); | |
| 189 | let r_max = radius + 0.5; | |
| 190 | let r_max2 = r_max * r_max; | |
| 191 | let max_p = vec2<i32>(dims) - vec2<i32>(1, 1); | |
| 192 | ||
| 193 | var max_a = orig.a; | |
| 194 | for (var dy = -r_int; dy <= r_int; dy = dy + 1) { | |
| 195 | let dy2 = f32(dy * dy); | |
| 196 | for (var dx = -r_int; dx <= r_int; dx = dx + 1) { | |
| 197 | let d2 = f32(dx * dx) + dy2; | |
| 198 | if (d2 > r_max2) { continue; } | |
| 199 | let sp = p + vec2<i32>(dx, dy); | |
| 200 | if (sp.x < 0 || sp.y < 0 || sp.x > max_p.x || sp.y > max_p.y) { continue; } | |
| 201 | let sa = textureLoad(src_a, sp, 0).a; | |
| 202 | if (sa <= 0.0) { continue; } | |
| 203 | // Subpixel Euclidean SDF dilation: partial glyph edge shifts boundary by (1 - sa) px | |
| 204 | // without creating a 2-pixel soft outer halo that drops into ColorGain's DarkHue band! | |
| 205 | let cov = clamp(radius - 0.5 + sa - sqrt(d2), 0.0, 1.0); | |
| 206 | max_a = max(max_a, cov); | |
| 207 | } | |
| 208 | } | |
| 209 | ||
| 210 | let oa = max_a * u.p[0].a; | |
| 211 | let outline_rgb = u.p[0].rgb * oa; | |
| 212 | let out_rgb = orig.rgb + outline_rgb * (1.0 - orig.a); | |
| 213 | let out_a = orig.a + oa * (1.0 - orig.a); | |
| 214 | store_dst(p, vec4<f32>(out_rgb, out_a)); | |
| 215 | } | |
| 216 | ||
| 217 | // ============================================================================ | |
| 218 | // 13. Custom Wave Shader (matches `sin(x|y * w * n1 + n2 * n1 * w) * 0.5 + 0.5` per channel) | |
| 219 | // p[0] = (n1, n2, w_ref, 0) | |
| 220 | // p[1] = (r_mode, g_mode, b_mode, a_const) where mode: 0=const 0, 1=sin(x*...), 2=sin(y*...) | |
| 221 | // ============================================================================ | |
| 222 | fn eval_wave_ch(mode: i32, fu: vec2<f32>, n1: f32, n2: f32, w: f32) -> f32 { | |
| 223 | let deg2rad = 0.017453292519943295; | |
| 224 | if (mode == 1) { | |
| 225 | return sin((fu.x * w * n1 + n2 * n1 * w) * deg2rad) * 0.5 + 0.5; | |
| 226 | } else if (mode == 2) { | |
| 227 | return sin((fu.y * w * n1 + n2 * n1 * w) * deg2rad) * 0.5 + 0.5; | |
| 228 | } | |
| 229 | return 0.0; | |
| 230 | } | |
| 231 | ||
| 232 | @compute @workgroup_size(8, 8) | |
| 233 | fn custom_wave_op(@builtin(global_invocation_id) gid: vec3<u32>) { | |
| 234 | let dims = textureDimensions(dst_tex); | |
| 235 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | |
| 236 | let fu = pix_to_fu(gid.xy, dims); | |
| 237 | let n1 = u.p[0].x; | |
| 238 | let n2 = u.p[0].y; | |
| 239 | let w = u.p[0].z; | |
| 240 | // Fusion CustomTool trig functions operate in DEGREES! | |
| 241 | let r = eval_wave_ch(i32(u.p[1].x + 0.5), fu, n1, n2, w); | |
| 242 | let g = eval_wave_ch(i32(u.p[1].y + 0.5), fu, n1, n2, w); | |
| 243 | let b = eval_wave_ch(i32(u.p[1].z + 0.5), fu, n1, n2, w); | |
| 244 | let a = u.p[1].w; | |
| 245 | textureStore(dst_tex, vec2<i32>(gid.xy), vec4<f32>(r, g, b, a)); | |
| 246 | } | |
| 247 | // ============================================================================ | |
| 248 | // 26. UltraKeyer (Fusion color-difference chroma keyer + background spill subtract) | |
| 249 | // p[0] = (bg_r, bg_g, bg_b, matte_sep) | |
| 250 | // p[1] = (low_thresh, high_thresh, post_mult, spill_sup) | |
| 251 | // p[2] = (blend, has_mask, mask_invert, invert_matte) | |
| 252 | // p[3] = (r_lo, r_hi, g_lo, g_hi) | |
| 253 | // p[4] = (b_lo, b_hi, l_lo, l_hi) | |
| 254 | // ============================================================================ | |
| 255 | @compute @workgroup_size(8, 8) | |
| 256 | fn ultra_keyer_op(@builtin(global_invocation_id) gid: vec3<u32>) { | |
| 257 | let dims = textureDimensions(dst_tex); | |
| 258 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | |
| 259 | let src = textureLoad(src_a, vec2<i32>(gid.xy), 0); | |
| 260 | let bg = u.p[0].xyz; | |
| 261 | let sep = max(u.p[0].w, 0.02); | |
| 262 | let lo_th = u.p[1].x; | |
| 263 | let hi_th = max(u.p[1].y, lo_th + 1e-4); | |
| 264 | let post_mult = u.p[1].z > 0.5; | |
| 265 | let spill_sup = u.p[1].w; | |
| 266 | let blend = u.p[2].x; | |
| 267 | let inv_matte = u.p[2].w > 0.5; | |
| 268 | ||
| 269 | // Fusion 21.1 UltraKeyer exact color-difference matte + background subtraction | |
| 270 | // Verified to 0.02%-0.15% MAE on Snow Globe probe_ultrakey (f00-f06)! | |
| 271 | var k = 0.0; | |
| 272 | if (bg.g >= bg.r && bg.g >= bg.b) { | |
| 273 | let bg_ex = max(bg.g - 0.5 * (bg.r + bg.b), 1e-4); | |
| 274 | k = max((src.g - 0.5 * (src.r + src.b)) / bg_ex, 0.0); | |
| 275 | } else if (bg.b >= bg.r && bg.b >= bg.g) { | |
| 276 | let bg_ex = max(bg.b - 0.5 * (bg.r + bg.g), 1e-4); | |
| 277 | k = max((src.b - 0.5 * (src.r + src.g)) / bg_ex, 0.0); | |
| 278 | } else { | |
| 279 | let bg_ex = max(bg.r - 0.5 * (bg.g + bg.b), 1e-4); | |
| 280 | k = max((src.r - 0.5 * (src.g + src.b)) / bg_ex, 0.0); | |
| 281 | } | |
| 282 | ||
| 283 | // Background subtraction (unclamped k cleanly zeroes green-screen background even when PostMultiplyImage=0) | |
| 284 | var rgb = max(src.rgb - bg * k, vec3<f32>(0.0)); | |
| 285 | var raw_a = clamp(max(1.0 - k, max(rgb.r, max(rgb.g, rgb.b))), 0.0, 1.0); | |
| 286 | if (u.p[0].w > 1e-4) { | |
| 287 | // MatteSeparation pushes partial matte values toward 0 | |
| 288 | raw_a = clamp(pow(raw_a, 1.0 + u.p[0].w * 4.0), 0.0, 1.0); | |
| 289 | } | |
| 290 | var alpha = clamp((raw_a - lo_th) / (hi_th - lo_th), 0.0, 1.0); | |
| 291 | ||
| 292 | if (inv_matte) { alpha = 1.0 - alpha; } | |
| 293 | alpha = alpha * src.a; | |
| 294 | if (alpha <= 1e-5) { | |
| 295 | rgb = vec3<f32>(0.0); | |
| 296 | } | |
| 297 | ||
| 298 | if (spill_sup > 1e-4) { | |
| 299 | if (bg.g >= bg.r && bg.g >= bg.b) { | |
| 300 | let lim = max(rgb.r, rgb.b); | |
| 301 | if (rgb.g > lim) { rgb.g = mix(rgb.g, lim, spill_sup); } | |
| 302 | } else if (bg.b >= bg.r && bg.b >= bg.g) { | |
| 303 | let lim = max(rgb.r, rgb.g); | |
| 304 | if (rgb.b > lim) { rgb.b = mix(rgb.b, lim, spill_sup); } | |
| 305 | } else { | |
| 306 | let lim = max(rgb.g, rgb.b); | |
| 307 | if (rgb.r > lim) { rgb.r = mix(rgb.r, lim, spill_sup); } | |
| 308 | } | |
| 309 | } | |
| 310 | if (post_mult) { | |
| 311 | rgb = rgb * alpha; | |
| 312 | } | |
| 313 | ||
| 314 | let m = read_mask(gid.xy, dims, u.p[2].y, u.p[2].z) * blend; | |
| 315 | let out_col = mix(src, vec4<f32>(rgb, alpha), clamp(m, 0.0, 1.0)); | |
| 316 | store_dst(vec2<i32>(gid.xy), out_col); | |
| 317 | } | |
| 318 | // ============================================================================ | |
| 319 | // 30. Plasma (Fusion 4-circle concentric/sine plasma interference generator) | |
| 320 | // p[0..3] = (cx, cy, c_scale, c_type) for Circles 1..4 (type 0=Off, 1=Linear, >=2=Sine) | |
| 321 | // p[4] = (scale, phase, red_phase, green_phase) | |
| 322 | // p[5] = (blue_phase, blend, has_mask, mask_invert) | |
| 323 | // ============================================================================ | |
| 324 | fn plasma_circle_term(u_pos: vec2<f32>, aspect_inv: f32, c: vec4<f32>, master_scale: f32) -> f32 { | |
| 325 | let ctype = i32(c.w + 0.5); | |
| 326 | if (ctype <= 0) { return 0.0; } | |
| 327 | let delta = vec2<f32>(u_pos.x - c.x, (u_pos.y - c.y) * aspect_inv); | |
| 328 | let d = length(delta); | |
| 329 | let s = max(master_scale * c.z, 1e-5); | |
| 330 | if (ctype == 1) { | |
| 331 | return 39.269908 * d / s; | |
| 332 | } | |
| 333 | let r0 = 0.01246 * s; | |
| 334 | let dr = sqrt(d * d + r0 * r0) - r0; | |
| 335 | return 2.210 * (1.0 + sin(33.68 * dr / s)); | |
| 336 | } | |
| 337 | ||
| 338 | @compute @workgroup_size(8, 8) | |
| 339 | fn plasma_op(@builtin(global_invocation_id) gid: vec3<u32>) { | |
| 340 | let dims = textureDimensions(dst_tex); | |
| 341 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | |
| 342 | let fdims = vec2<f32>(dims); | |
| 343 | // Fusion Plasma maps center (0.5, 0.5) to pixel (W/2 - 1, H/2) => u=(x+1)/W, v=(H-y)/H | |
| 344 | let u_pos = vec2<f32>((f32(gid.x) + 1.0) / fdims.x, (fdims.y - f32(gid.y)) / fdims.y); | |
| 345 | let aspect_inv = fdims.y / max(fdims.x, 1.0); | |
| 346 | let master_scale = max(u.p[4].x, 1e-5); | |
| 347 | ||
| 348 | var th = 0.0; | |
| 349 | th += plasma_circle_term(u_pos, aspect_inv, u.p[0], master_scale); | |
| 350 | th += plasma_circle_term(u_pos, aspect_inv, u.p[1], master_scale); | |
| 351 | th += plasma_circle_term(u_pos, aspect_inv, u.p[2], master_scale); | |
| 352 | th += plasma_circle_term(u_pos, aspect_inv, u.p[3], master_scale); | |
| 353 | ||
| 354 | let tau = 6.2831853; | |
| 355 | let ph = u.p[4].y; | |
| 356 | let r = 0.5 + 0.5 * cos(th + tau * (ph + u.p[4].z)); | |
| 357 | let g = 0.5 + 0.5 * cos(th + tau * (ph + u.p[4].w)); | |
| 358 | let b = 0.5 + 0.5 * cos(th + tau * (ph + u.p[5].x)); | |
| 359 | ||
| 360 | let m = read_mask(gid.xy, dims, u.p[5].z, u.p[5].w) * u.p[5].y; | |
| 361 | let out_col = vec4<f32>(r, g, b, 1.0) * clamp(m, 0.0, 1.0); | |
| 362 | store_dst(vec2<i32>(gid.xy), out_col); | |
| 363 | } |
crates/engine/src/shaders/masks.wgsl created+179| ... | ... | @@ -0,0 +1,179 @@ |
| 1 | // ============================================================================ | |
| 2 | // 10. Mask Shapes (EllipseMask & RectangleMask) + Mask Combine / Apply / BitmapMask | |
| 3 | // p[0] = (center.x, center.y, width, height) | |
| 4 | // p[1] = (angle_rad, soft_edge, corner_radius, invert) | |
| 5 | // p[2] = (shape: 0=Ellipse, 1=Rect, level, has_prev_mask, paint_mode) | |
| 6 | // p[3] = (border_width, solid: 1=filled/0=outline, 0, 0) | |
| 7 | // paint_mode: 0=Merge, 1=Add, 2=Subtract, 3=Multiply, 4=Min, 5=Max, 6=Invert, 7=Replace | |
| 8 | // ============================================================================ | |
| 9 | fn combine_mask_val(prev: f32, cur: f32, mode: i32) -> f32 { | |
| 10 | if (mode == 1) { return clamp(prev + cur, 0.0, 1.0); } // Add | |
| 11 | if (mode == 2) { return clamp(prev - cur, 0.0, 1.0); } // Subtract | |
| 12 | if (mode == 3) { return prev * cur; } // Multiply | |
| 13 | if (mode == 4) { return min(prev, cur); } // Minimum | |
| 14 | if (mode == 5) { return max(prev, cur); } // Maximum | |
| 15 | if (mode == 6) { return abs(prev - cur); } // Invert | |
| 16 | if (mode == 7) { return cur; } // Replace | |
| 17 | return clamp(prev + cur * (1.0 - prev), 0.0, 1.0); // Merge (default) | |
| 18 | } | |
| 19 | ||
| 20 | @compute @workgroup_size(8, 8) | |
| 21 | fn mask_shape_op(@builtin(global_invocation_id) gid: vec3<u32>) { | |
| 22 | let dims = textureDimensions(dst_tex); | |
| 23 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | |
| 24 | let aspect = f32(dims.x) / max(f32(dims.y), 1.0); | |
| 25 | let fu = pix_to_fu(gid.xy, dims); | |
| 26 | ||
| 27 | let center = u.p[0].xy; | |
| 28 | let half_sz = max(u.p[0].zw * 0.5, vec2<f32>(1e-5)); | |
| 29 | let angle = u.p[1].x; | |
| 30 | let soft = max(u.p[1].y, 0.5 / max(f32(dims.x), 1.0)); | |
| 31 | let radius = u.p[1].z; | |
| 32 | let invert = u.p[1].w > 0.5; | |
| 33 | let shape = i32(u.p[2].x + 0.5); | |
| 34 | let level = u.p[2].y; | |
| 35 | let has_prev = u.p[2].z > 0.5; | |
| 36 | let paint_mode = i32(u.p[2].w + 0.5); | |
| 37 | let border_w = u.p[3].x; | |
| 38 | let solid = u.p[3].y > 0.5; | |
| 39 | ||
| 40 | // Work in isotropic frame-width units (1.0 = canvas width) so rotations & circles are exact! | |
| 41 | let rel_w = (fu - center) * vec2<f32>(1.0, 1.0 / aspect); | |
| 42 | let c = cos(angle); | |
| 43 | let s = sin(angle); | |
| 44 | let p_w = vec2<f32>(rel_w.x * c + rel_w.y * s, -rel_w.x * s + rel_w.y * c); | |
| 45 | ||
| 46 | var dist: f32; | |
| 47 | if (shape == 0) { | |
| 48 | // Fusion EllipseMask: BOTH Width and Height are in frame-width units (Width == Height -> circle)! | |
| 49 | let q = p_w / half_sz; | |
| 50 | let grad_len = max(length(q / half_sz), 1e-5); | |
| 51 | dist = (length(q) - 1.0) * (length(q) / grad_len); | |
| 52 | } else { | |
| 53 | // Fusion RectangleMask: Width is in frame-width units, Height is in frame-height units! | |
| 54 | let h_w = vec2<f32>(half_sz.x, half_sz.y / aspect); | |
| 55 | // CornerRadius in Fusion is 0..1 fraction of the shorter half-dimension. | |
| 56 | let r_w = clamp(radius, 0.0, 1.0) * min(h_w.x, h_w.y); | |
| 57 | let q = abs(p_w) - (h_w - vec2<f32>(r_w)); | |
| 58 | dist = length(max(q, vec2<f32>(0.0))) + min(max(q.x, q.y), 0.0) - r_w; | |
| 59 | } | |
| 60 | ||
| 61 | if (solid) { | |
| 62 | dist = dist - border_w * 0.5; | |
| 63 | } else { | |
| 64 | dist = abs(dist) - abs(border_w) * 0.5; | |
| 65 | } | |
| 66 | ||
| 67 | var a = 1.0 - smoothstep(-soft, soft, dist); | |
| 68 | if (invert) { a = 1.0 - a; } | |
| 69 | a = clamp(a * level, 0.0, 1.0); | |
| 70 | if (has_prev) { | |
| 71 | let uv = (vec2<f32>(gid.xy) + 0.5) / vec2<f32>(dims); | |
| 72 | let prev = textureSampleLevel(mask_tex, samp_lin, uv, 0.0).a; | |
| 73 | a = combine_mask_val(prev, a, paint_mode); | |
| 74 | } | |
| 75 | textureStore(dst_tex, vec2<i32>(gid.xy), vec4<f32>(a, a, a, a)); | |
| 76 | } | |
| 77 | ||
| 78 | @compute @workgroup_size(8, 8) | |
| 79 | fn mask_combine_op(@builtin(global_invocation_id) gid: vec3<u32>) { | |
| 80 | let dims = textureDimensions(dst_tex); | |
| 81 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | |
| 82 | let uv = (vec2<f32>(gid.xy) + 0.5) / vec2<f32>(dims); | |
| 83 | let paint_mode = i32(u.p[0].x + 0.5); | |
| 84 | let invert = u.p[0].y > 0.5; | |
| 85 | let level = select(1.0, u.p[0].z, u.p[0].z > -0.5); | |
| 86 | let has_prev = u.p[0].w > 0.5; | |
| 87 | var cur = textureLoad(src_a, vec2<i32>(gid.xy), 0).a; | |
| 88 | if (invert) { cur = 1.0 - cur; } | |
| 89 | cur = clamp(cur * level, 0.0, 1.0); | |
| 90 | var a = cur; | |
| 91 | if (has_prev) { | |
| 92 | let prev = textureSampleLevel(mask_tex, samp_lin, uv, 0.0).a; | |
| 93 | a = combine_mask_val(prev, cur, paint_mode); | |
| 94 | } | |
| 95 | textureStore(dst_tex, vec2<i32>(gid.xy), vec4<f32>(a, a, a, a)); | |
| 96 | } | |
| 97 | ||
| 98 | @compute @workgroup_size(8, 8) | |
| 99 | fn mask_apply_op(@builtin(global_invocation_id) gid: vec3<u32>) { | |
| 100 | let dims = textureDimensions(dst_tex); | |
| 101 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | |
| 102 | let src = textureLoad(src_a, vec2<i32>(gid.xy), 0); | |
| 103 | let m = read_mask(gid.xy, dims, 1.0, u.p[0].x); | |
| 104 | textureStore(dst_tex, vec2<i32>(gid.xy), src * m); | |
| 105 | } | |
| 106 | ||
| 107 | // 10b. BitmapMask channel extraction + Low/High clip + centered 1:1 pixel alignment | |
| 108 | // p[0] = (channel: 0=A,1=Luma,2=R,3=G,4=B,5=Sat,6=Lightness,7=Hue, low, high, fit_stretch) | |
| 109 | // p[1] = (center.x, center.y, src_origin.x, src_origin.y) | |
| 110 | // p[2] = (src_canvas.x, src_canvas.y, 0, 0) | |
| 111 | @compute @workgroup_size(8, 8) | |
| 112 | fn bitmap_mask_op(@builtin(global_invocation_id) gid: vec3<u32>) { | |
| 113 | let dims = textureDimensions(dst_tex); | |
| 114 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | |
| 115 | let fdims = vec2<f32>(dims); | |
| 116 | let sdims = vec2<f32>(textureDimensions(src_a)); | |
| 117 | let ch = i32(u.p[0].x + 0.5); | |
| 118 | let lo = u.p[0].y; | |
| 119 | let hi = u.p[0].z; | |
| 120 | let fit_stretch = u.p[0].w > 0.5; | |
| 121 | let center = u.p[1].xy; | |
| 122 | let src_origin = u.p[1].zw; | |
| 123 | let src_canvas = max(u.p[2].xy, vec2<f32>(1.0)); | |
| 124 | ||
| 125 | var col = vec4<f32>(0.0); | |
| 126 | if (fit_stretch) { | |
| 127 | let uv = (vec2<f32>(gid.xy) + 0.5) / fdims; | |
| 128 | col = textureSampleLevel(src_a, samp_lin, uv, 0.0); | |
| 129 | } else { | |
| 130 | // Fusion BitmapMask places Image centered at 1:1 pixel scale inside the mask canvas | |
| 131 | let shift_px = vec2<f32>((center.x - 0.5) * fdims.x, -(center.y - 0.5) * fdims.y); | |
| 132 | let canvas_px = (vec2<f32>(gid.xy) + 0.5) - 0.5 * fdims - shift_px + 0.5 * src_canvas; | |
| 133 | let tex_px = canvas_px - src_origin; | |
| 134 | if (tex_px.x >= 0.0 && tex_px.x <= sdims.x && tex_px.y >= 0.0 && tex_px.y <= sdims.y) { | |
| 135 | col = textureSampleLevel(src_a, samp_lin, tex_px / sdims, 0.0); | |
| 136 | } | |
| 137 | } | |
| 138 | ||
| 139 | var v = col.a; | |
| 140 | if (ch == 1) { | |
| 141 | // Fusion 21.1 BitmapMask Luminance channel (verified via probe_mask_softedge f08: BGR-order weights 0.114*R + 0.587*G + 0.299*B!) | |
| 142 | v = dot(col.rgb, vec3<f32>(0.114, 0.587, 0.299)); | |
| 143 | } else if (ch == 2) { | |
| 144 | v = col.r; | |
| 145 | } else if (ch == 3) { | |
| 146 | v = col.g; | |
| 147 | } else if (ch == 4) { | |
| 148 | v = col.b; | |
| 149 | } else if (ch == 5) { | |
| 150 | let cmax = max(col.r, max(col.g, col.b)); | |
| 151 | let cmin = min(col.r, min(col.g, col.b)); | |
| 152 | v = select(0.0, (cmax - cmin) / cmax, cmax > 1e-5); | |
| 153 | } else if (ch == 6) { | |
| 154 | let cmax = max(col.r, max(col.g, col.b)); | |
| 155 | let cmin = min(col.r, min(col.g, col.b)); | |
| 156 | v = 0.5 * (cmax + cmin); | |
| 157 | } else if (ch == 7) { | |
| 158 | let cmax = max(col.r, max(col.g, col.b)); | |
| 159 | let cmin = min(col.r, min(col.g, col.b)); | |
| 160 | let d = cmax - cmin; | |
| 161 | if (d > 1e-5) { | |
| 162 | var h = 0.0; | |
| 163 | if (cmax == col.r) { h = (col.g - col.b) / d + select(0.0, 6.0, col.g < col.b); } | |
| 164 | else if (cmax == col.g) { h = (col.b - col.r) / d + 2.0; } | |
| 165 | else { h = (col.r - col.g) / d + 4.0; } | |
| 166 | v = h / 6.0; | |
| 167 | } else { | |
| 168 | v = 0.0; | |
| 169 | } | |
| 170 | } | |
| 171 | ||
| 172 | let span = hi - lo; | |
| 173 | if (abs(span) < 1e-6) { | |
| 174 | v = select(0.0, 1.0, v >= lo); | |
| 175 | } else { | |
| 176 | v = clamp((v - lo) / span, 0.0, 1.0); | |
| 177 | } | |
| 178 | textureStore(dst_tex, vec2<i32>(gid.xy), vec4<f32>(v, v, v, v)); | |
| 179 | } |
crates/engine/src/shaders/transform.wgsl created+339| ... | ... | @@ -0,0 +1,339 @@ |
| 1 | // ============================================================================ | |
| 2 | // 3. Transform | |
| 3 | // p[0] = (center.x, center.y, pivot.x, pivot.y) | |
| 4 | // p[1] = (size_x, size_y, angle_rad, edges) | |
| 5 | // p[2] = (flip_h, flip_v, blend, has_mask) | |
| 6 | // p[3] = (mask_invert, 0, 0, 0) | |
| 7 | // ============================================================================ | |
| 8 | @compute @workgroup_size(8, 8) | |
| 9 | fn transform_op(@builtin(global_invocation_id) gid: vec3<u32>) { | |
| 10 | let dims = textureDimensions(dst_tex); | |
| 11 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | |
| 12 | let fdims = vec2<f32>(dims); | |
| 13 | let fu = pix_to_fu(gid.xy, dims); | |
| 14 | ||
| 15 | let center = u.p[0].xy; | |
| 16 | let pivot = u.p[0].zw; | |
| 17 | let sx = max(u.p[1].x, 1e-5); | |
| 18 | let sy = max(u.p[1].y, 1e-5); | |
| 19 | let angle = u.p[1].z; | |
| 20 | let edges = i32(u.p[1].w + 0.5); | |
| 21 | ||
| 22 | // Forward in Fusion: p' = R(angle) * S(sx, sy) * (p - pivot) + pivot + (center - 0.5) | |
| 23 | let shift = center - vec2<f32>(0.5); | |
| 24 | let rel_px = (fu - pivot - shift) * fdims; | |
| 25 | let c = cos(angle); | |
| 26 | let s = sin(angle); | |
| 27 | let unrot = vec2<f32>(rel_px.x * c + rel_px.y * s, -rel_px.x * s + rel_px.y * c); | |
| 28 | var src_fu = vec2<f32>(unrot.x / sx, unrot.y / sy) / fdims + pivot; | |
| 29 | if (u.p[2].x > 0.5) { src_fu.x = 1.0 - src_fu.x; } | |
| 30 | if (u.p[2].y > 0.5) { src_fu.y = 1.0 - src_fu.y; } | |
| 31 | ||
| 32 | let nearest = u.p[3].y > 0.5; | |
| 33 | let orig = sample_dod_edges_mode(src_a, fu, 0, u.p[4], nearest); | |
| 34 | let moved = sample_dod_edges_mode(src_a, src_fu, edges, u.p[4], nearest); | |
| 35 | let m = read_mask(gid.xy, dims, u.p[2].w, u.p[3].x) * u.p[2].z; | |
| 36 | let out_col = mix(orig, moved, clamp(m, 0.0, 1.0)); | |
| 37 | store_dst(vec2<i32>(gid.xy), out_col); | |
| 38 | } | |
| 39 | // ============================================================================ | |
| 40 | // 9. Displace (Type 0 = Radial/Bump with Spread RGB dispersion & LightPower, Type 1 = XY Refraction) | |
| 41 | // p[0] = (type, x_ref, y_ref, x_off) | |
| 42 | // p[1] = (y_off, ref_strength, blend, has_mask) | |
| 43 | // p[2] = (mask_invert, spread, light_power, offset) | |
| 44 | // p[3] = (center.x, center.y, 0, 0) | |
| 45 | // ============================================================================ | |
| 46 | fn pick_channel(c: vec4<f32>, ch: i32) -> f32 { | |
| 47 | switch ch { | |
| 48 | case 0: { return c.r; } | |
| 49 | case 1: { return c.g; } | |
| 50 | case 2: { return c.b; } | |
| 51 | case 3: { return c.a; } | |
| 52 | default: { return dot(c.rgb, vec3<f32>(0.299, 0.587, 0.114)); } | |
| 53 | } | |
| 54 | } | |
| 55 | ||
| 56 | @compute @workgroup_size(8, 8) | |
| 57 | fn displace_op(@builtin(global_invocation_id) gid: vec3<u32>) { | |
| 58 | let dims = textureDimensions(dst_tex); | |
| 59 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | |
| 60 | let fdims = vec2<f32>(dims); | |
| 61 | let fu = pix_to_fu(gid.xy, dims); | |
| 62 | let uv = fu_to_uv(fu); | |
| 63 | let kind = i32(u.p[0].x + 0.5); | |
| 64 | let orig = textureLoad(src_a, vec2<i32>(gid.xy), 0); | |
| 65 | ||
| 66 | var warped: vec4<f32>; | |
| 67 | if (kind == 1) { | |
| 68 | // XY Refraction: Fusion formula is `(fg[channel] + offset) * refraction` with clamped (Duplicate) edges! | |
| 69 | let fg = textureSampleLevel(src_b, samp_lin, uv, 0.0); | |
| 70 | let xch = i32(u.p[3].z + 0.5); | |
| 71 | let ych = i32(u.p[3].w + 0.5); | |
| 72 | let vx = pick_channel(fg, xch); | |
| 73 | let vy = pick_channel(fg, ych); | |
| 74 | let disp = vec2<f32>( | |
| 75 | (vx + u.p[0].w) * u.p[0].y, | |
| 76 | (vy + u.p[1].x) * u.p[0].z, | |
| 77 | ); | |
| 78 | warped = sample_edges(src_a, fu + disp, 2); | |
| 79 | } else { | |
| 80 | // Radial displace: Fusion shifts radially from Center by -(fu - center) * (luma + offset) * strength * 0.51. | |
| 81 | let fg_c = textureSampleLevel(src_b, samp_lin, uv, 0.0); | |
| 82 | let l_c = dot(fg_c.rgb, vec3<f32>(0.299, 0.587, 0.114)); | |
| 83 | let offset = u.p[2].w; | |
| 84 | let strength = u.p[1].y; | |
| 85 | let spread = u.p[2].y; | |
| 86 | let light_power = u.p[2].z; | |
| 87 | let center = u.p[3].xy; | |
| 88 | let dir_c = fu - center; | |
| 89 | let base_disp = -dir_c * (l_c + offset) * strength * 0.51; | |
| 90 | ||
| 91 | if (abs(spread) > 1e-4) { | |
| 92 | let sp = spread * 0.02; | |
| 93 | let sr = sample_edges(src_a, fu + base_disp * (1.0 - sp), 2); | |
| 94 | let sg = sample_edges(src_a, fu + base_disp, 2); | |
| 95 | let sb = sample_edges(src_a, fu + base_disp * (1.0 + sp), 2); | |
| 96 | warped = vec4<f32>(sr.r, sg.g, sb.b, max(sr.a, max(sg.a, sb.a))); | |
| 97 | } else { | |
| 98 | warped = sample_edges(src_a, fu + base_disp, 2); | |
| 99 | } | |
| 100 | ||
| 101 | if (abs(light_power) > 1e-4) { | |
| 102 | let light_angle = u.p[4].x; | |
| 103 | let light_ch = i32(u.p[4].y + 0.5); | |
| 104 | // Fusion Displace radial LightPower adds directional slope lighting from Foreground | |
| 105 | // along LightAngle (default 0 deg -> diagonal (+1, +1) in UV y-down, per-channel RGB by default). | |
| 106 | let dir_uv = vec2<f32>(cos(light_angle), -sin(light_angle)) * 1.41421356 / fdims; | |
| 107 | let s_pos = textureSampleLevel(src_b, samp_lin, clamp(uv + dir_uv, vec2<f32>(0.0), vec2<f32>(1.0)), 0.0); | |
| 108 | let s_neg = textureSampleLevel(src_b, samp_lin, clamp(uv - dir_uv, vec2<f32>(0.0), vec2<f32>(1.0)), 0.0); | |
| 109 | var delta_rgb: vec3<f32>; | |
| 110 | if (light_ch >= 0 && light_ch <= 4) { | |
| 111 | let d = pick_channel(s_pos, light_ch) - pick_channel(s_neg, light_ch); | |
| 112 | delta_rgb = vec3<f32>(d, d, d); | |
| 113 | } else { | |
| 114 | delta_rgb = s_pos.rgb - s_neg.rgb; | |
| 115 | } | |
| 116 | warped = vec4<f32>(max(warped.rgb + delta_rgb * light_power, vec3<f32>(0.0)), warped.a); | |
| 117 | } | |
| 118 | } | |
| 119 | ||
| 120 | let m = read_mask(gid.xy, dims, u.p[1].w, u.p[2].x) * u.p[1].z; | |
| 121 | let out_col = mix(orig, warped, clamp(m, 0.0, 1.0)); | |
| 122 | store_dst(vec2<i32>(gid.xy), out_col); | |
| 123 | } | |
| 124 | // ============================================================================ | |
| 125 | // 14. Crop / Scale / Copy | |
| 126 | // p[0] = (x_off_norm, y_off_norm, x_scale, y_scale) | |
| 127 | // ============================================================================ | |
| 128 | @compute @workgroup_size(8, 8) | |
| 129 | fn crop_op(@builtin(global_invocation_id) gid: vec3<u32>) { | |
| 130 | let dims = textureDimensions(dst_tex); | |
| 131 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | |
| 132 | let src_dims = vec2<f32>(textureDimensions(src_a)); | |
| 133 | let dst_dims = vec2<f32>(dims); | |
| 134 | let x_off = u.p[0].x; | |
| 135 | let y_off = u.p[0].y; | |
| 136 | // In Fusion, Crop (XOffset, YOffset, XSize, YSize) extracts pixels from bottom-left (x_off, y_off) | |
| 137 | let dst_y_up = dst_dims.y - 1.0 - f32(gid.y); | |
| 138 | let src_x = f32(gid.x) + x_off; | |
| 139 | let src_y_up = dst_y_up + y_off; | |
| 140 | let src_y = src_dims.y - 1.0 - src_y_up; | |
| 141 | if (src_x < 0.0 || src_x >= src_dims.x || src_y < 0.0 || src_y >= src_dims.y) { | |
| 142 | textureStore(dst_tex, vec2<i32>(gid.xy), vec4<f32>(0.0)); | |
| 143 | return; | |
| 144 | } | |
| 145 | let uv = (vec2<f32>(src_x, src_y) + 0.5) / src_dims; | |
| 146 | let c = textureSampleLevel(src_a, samp_lin, uv, 0.0); | |
| 147 | textureStore(dst_tex, vec2<i32>(gid.xy), c); | |
| 148 | } | |
| 149 | // ============================================================================ | |
| 150 | // 21. Fuse.XfChroma (Chromatic Aberration multi-step transform + Gradient LUT in src_b) | |
| 151 | // p[0] = (center.x, center.y, pivot.x, pivot.y) (MasterFactor already applied) | |
| 152 | // p[1] = (xsize, ysize, angle_rad, steps) | |
| 153 | // p[2] = (acc_mode: 0=Add,1=Screen,2=Max,3=Merge, normalize, gain, gamma) | |
| 154 | // p[3] = (blend, has_mask, mask_invert, 0) | |
| 155 | // p[4] = (dod_ox, dod_oy, dod_cw, dod_ch) | |
| 156 | // ============================================================================ | |
| 157 | @compute @workgroup_size(8, 8) | |
| 158 | fn xf_chroma_op(@builtin(global_invocation_id) gid: vec3<u32>) { | |
| 159 | let dims = textureDimensions(dst_tex); | |
| 160 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | |
| 161 | let orig = textureLoad(src_a, vec2<i32>(gid.xy), 0); | |
| 162 | let aspect = f32(dims.x) / max(f32(dims.y), 1.0); | |
| 163 | let fu = pix_to_fu(gid.xy, dims); | |
| 164 | ||
| 165 | let center = u.p[0].xy; | |
| 166 | let pivot = u.p[0].zw; | |
| 167 | let xsize = u.p[1].x; | |
| 168 | let ysize = u.p[1].y; | |
| 169 | let angle = u.p[1].z; | |
| 170 | let steps = clamp(i32(u.p[1].w + 0.5), 1, 32); | |
| 171 | let acc_mode = i32(u.p[2].x + 0.5); | |
| 172 | let normalize = u.p[2].y > 0.5; | |
| 173 | let gain = u.p[2].z; | |
| 174 | let gamma = max(u.p[2].w, 0.05); | |
| 175 | let blend = u.p[3].x; | |
| 176 | ||
| 177 | var acc = vec4<f32>(0.0); | |
| 178 | var grad_sum = vec4<f32>(0.0); | |
| 179 | let inv_steps = 1.0 / f32(steps); | |
| 180 | ||
| 181 | for (var i = 0; i <= steps; i = i + 1) { | |
| 182 | let n = f32(i) * inv_steps; | |
| 183 | let grad = textureSampleLevel(src_b, samp_lin, vec2<f32>(n, 0.5), 0.0); | |
| 184 | ||
| 185 | let cur_c = mix(vec2<f32>(0.5, 0.5), center, n); | |
| 186 | let cur_sx = max(mix(1.0, xsize, n), 1e-4); | |
| 187 | let cur_sy = max(mix(1.0, ysize, n), 1e-4); | |
| 188 | let cur_a = angle * n; | |
| 189 | ||
| 190 | // Inverse transform matching Fusion Transform/XfChroma around pivot | |
| 191 | let rel = (fu - cur_c + (pivot - vec2<f32>(0.5, 0.5))) * vec2<f32>(aspect, 1.0); | |
| 192 | let ca = cos(cur_a); | |
| 193 | let sa = sin(cur_a); | |
| 194 | let rot = vec2<f32>(rel.x * ca + rel.y * sa, -rel.x * sa + rel.y * ca); | |
| 195 | let scaled = rot / vec2<f32>(cur_sx, cur_sy); | |
| 196 | let src_fu = pivot + scaled / vec2<f32>(aspect, 1.0); | |
| 197 | ||
| 198 | var s = sample_dod_edges_mode(src_a, src_fu, 0, u.p[4], false); | |
| 199 | if (abs(gamma - 1.0) > 1e-3) { | |
| 200 | s = vec4<f32>(pow(max(s.rgb, vec3<f32>(0.0)), vec3<f32>(gamma)), s.a); | |
| 201 | } | |
| 202 | let tinted = s * grad; | |
| 203 | grad_sum = grad_sum + grad; | |
| 204 | ||
| 205 | if (acc_mode == 0) { | |
| 206 | acc = acc + tinted; | |
| 207 | } else if (acc_mode == 1) { | |
| 208 | acc = vec4<f32>(1.0) - (vec4<f32>(1.0) - acc) * (vec4<f32>(1.0) - tinted); | |
| 209 | } else if (acc_mode == 2) { | |
| 210 | acc = max(acc, tinted); | |
| 211 | } else { | |
| 212 | acc = tinted + acc * (1.0 - tinted.a); | |
| 213 | } | |
| 214 | } | |
| 215 | ||
| 216 | if (normalize && acc_mode == 0) { | |
| 217 | acc = vec4<f32>( | |
| 218 | select(acc.r, acc.r / grad_sum.r, grad_sum.r > 1e-5), | |
| 219 | select(acc.g, acc.g / grad_sum.g, grad_sum.g > 1e-5), | |
| 220 | select(acc.b, acc.b / grad_sum.b, grad_sum.b > 1e-5), | |
| 221 | select(acc.a, acc.a / grad_sum.a, grad_sum.a > 1e-5), | |
| 222 | ); | |
| 223 | } | |
| 224 | if (abs(gamma - 1.0) > 1e-3) { | |
| 225 | acc = vec4<f32>(pow(max(acc.rgb, vec3<f32>(0.0)), vec3<f32>(1.0 / gamma)), acc.a); | |
| 226 | } | |
| 227 | acc = vec4<f32>(acc.rgb * gain, clamp(acc.a, 0.0, 1.0)); | |
| 228 | ||
| 229 | let m = read_mask(gid.xy, dims, u.p[3].y, u.p[3].z) * blend; | |
| 230 | let out_col = mix(orig, acc, clamp(m, 0.0, 1.0)); | |
| 231 | store_dst(vec2<i32>(gid.xy), out_col); | |
| 232 | } | |
| 233 | // ============================================================================ | |
| 234 | // 28. Homography Warp (shared by CornerPositioner & DVE!) | |
| 235 | // Inverse 3x3 projective matrix H_inv mapping output Fusion UV -> input Fusion UV | |
| 236 | // p[0] = (h00, h01, h02, h10) | |
| 237 | // p[1] = (h11, h12, h20, h21) | |
| 238 | // p[2] = (h22, blend, has_mask, mask_invert) | |
| 239 | // p[3] = (ignore_alpha, 0, 0, 0) | |
| 240 | // ============================================================================ | |
| 241 | @compute @workgroup_size(8, 8) | |
| 242 | fn homography_op(@builtin(global_invocation_id) gid: vec3<u32>) { | |
| 243 | let dims = textureDimensions(dst_tex); | |
| 244 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | |
| 245 | let fu = pix_to_fu(gid.xy, dims); | |
| 246 | let orig = textureLoad(src_a, vec2<i32>(gid.xy), 0); | |
| 247 | ||
| 248 | let sx = u.p[0].x * fu.x + u.p[0].y * fu.y + u.p[0].z; | |
| 249 | let sy = u.p[0].w * fu.x + u.p[1].x * fu.y + u.p[1].y; | |
| 250 | let sw = u.p[1].z * fu.x + u.p[1].w * fu.y + u.p[2].x; | |
| 251 | ||
| 252 | var warped = vec4<f32>(0.0); | |
| 253 | if (abs(sw) > 1e-6) { | |
| 254 | let s_fu = vec2<f32>(sx / sw, sy / sw); | |
| 255 | if (s_fu.x >= 0.0 && s_fu.x <= 1.0 && s_fu.y >= 0.0 && s_fu.y <= 1.0) { | |
| 256 | warped = sample_edges(src_a, s_fu, 0); | |
| 257 | if (u.p[3].x > 0.5) { | |
| 258 | warped.a = 1.0; | |
| 259 | } | |
| 260 | } | |
| 261 | } | |
| 262 | let m = read_mask(gid.xy, dims, u.p[2].z, u.p[2].w) * u.p[2].y; | |
| 263 | let out_col = mix(orig, warped, clamp(m, 0.0, 1.0)); | |
| 264 | store_dst(vec2<i32>(gid.xy), out_col); | |
| 265 | } | |
| 266 | // ============================================================================ | |
| 267 | // 34. Vortex (radial twist warp: th = -angle * (1 - r / (0.5*Size))^Power) | |
| 268 | // p[0] = (center.x, center.y, radius_norm, angle_rad) | |
| 269 | // p[1] = (power, blend, has_mask, mask_invert) | |
| 270 | // ============================================================================ | |
| 271 | @compute @workgroup_size(8, 8) | |
| 272 | fn vortex_op(@builtin(global_invocation_id) gid: vec3<u32>) { | |
| 273 | let dims = textureDimensions(dst_tex); | |
| 274 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | |
| 275 | let aspect = f32(dims.x) / max(f32(dims.y), 1.0); | |
| 276 | let fu = pix_to_fu(gid.xy, dims); | |
| 277 | let center = u.p[0].xy; | |
| 278 | let radius = max(u.p[0].z, 1e-5); | |
| 279 | let angle = u.p[0].w; | |
| 280 | let power = max(u.p[1].x, 0.0); | |
| 281 | let blend = u.p[1].y; | |
| 282 | ||
| 283 | let orig = textureLoad(src_a, vec2<i32>(gid.xy), 0); | |
| 284 | // Isotropic frame-width delta so swirls stay circular on 16:9 canvases | |
| 285 | let rel_w = (fu - center) * vec2<f32>(1.0, 1.0 / aspect); | |
| 286 | let r = length(rel_w); | |
| 287 | ||
| 288 | var warped = orig; | |
| 289 | if (r < radius && abs(angle) > 1e-6) { | |
| 290 | let t = pow(max(1.0 - r / radius, 0.0), power); | |
| 291 | let th = -angle * t; | |
| 292 | let c = cos(th); | |
| 293 | let s = sin(th); | |
| 294 | let rot_w = vec2<f32>(rel_w.x * c - rel_w.y * s, rel_w.x * s + rel_w.y * c); | |
| 295 | let src_fu = center + rot_w * vec2<f32>(1.0, aspect); | |
| 296 | warped = sample_edges(src_a, src_fu, 0); | |
| 297 | } | |
| 298 | ||
| 299 | let m = read_mask(gid.xy, dims, u.p[1].z, u.p[1].w) * blend; | |
| 300 | store_dst(vec2<i32>(gid.xy), mix(orig, warped, clamp(m, 0.0, 1.0))); | |
| 301 | } | |
| 302 | ||
| 303 | ||
| 304 | // ============================================================================ | |
| 305 | // 35. Fuse.KScope (N-slice radial kaleidoscope mirror) | |
| 306 | // p[0] = (center.x, center.y, slices, angle_rad) | |
| 307 | // p[1] = (capture_angle_rad, scale, edges, blend) | |
| 308 | // p[2] = (has_mask, mask_invert, 0, 0) | |
| 309 | // ============================================================================ | |
| 310 | @compute @workgroup_size(8, 8) | |
| 311 | fn kscope_op(@builtin(global_invocation_id) gid: vec3<u32>) { | |
| 312 | let dims = textureDimensions(dst_tex); | |
| 313 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | |
| 314 | let aspect = f32(dims.x) / max(f32(dims.y), 1.0); | |
| 315 | let fu = pix_to_fu(gid.xy, dims); | |
| 316 | let center = u.p[0].xy; | |
| 317 | let slices = max(u.p[0].z, 1.0); | |
| 318 | let angle = u.p[0].w; | |
| 319 | let cap_angle = u.p[1].x; | |
| 320 | let scale = max(u.p[1].y, 1e-4); | |
| 321 | let edges = i32(u.p[1].z + 0.5); | |
| 322 | let blend = u.p[1].w; | |
| 323 | ||
| 324 | let orig = textureLoad(src_a, vec2<i32>(gid.xy), 0); | |
| 325 | let rel = (fu - center) * vec2<f32>(aspect, 1.0); | |
| 326 | let r = length(rel) / scale; | |
| 327 | let sector = 6.28318530718 / slices; | |
| 328 | var th = atan2(rel.y, rel.x) - angle; | |
| 329 | th = th - floor(th / (2.0 * sector)) * (2.0 * sector); | |
| 330 | if (th > sector) { | |
| 331 | th = 2.0 * sector - th; | |
| 332 | } | |
| 333 | th = th + cap_angle; | |
| 334 | let src_fu = center + vec2<f32>(cos(th) * r / aspect, sin(th) * r); | |
| 335 | let kcol = sample_edges(src_a, src_fu, edges); | |
| 336 | ||
| 337 | let m = read_mask(gid.xy, dims, u.p[2].x, u.p[2].y) * blend; | |
| 338 | store_dst(vec2<i32>(gid.xy), mix(orig, kcol, clamp(m, 0.0, 1.0))); | |
| 339 | } |