diff options
| -rw-r--r-- | PROJECT_CONTEXT.md | 2 | ||||
| -rw-r--r-- | doc/COMPLETED.md | 2 | ||||
| -rw-r--r-- | src/effects/ntsc_effect.h | 14 | ||||
| -rw-r--r-- | src/effects/ntsc_rgb.wgsl | 20 | ||||
| -rw-r--r-- | src/effects/ntsc_yiq.wgsl | 19 | ||||
| -rw-r--r-- | src/effects/shaders.cc | 5 | ||||
| -rw-r--r-- | src/effects/shaders.h | 3 | ||||
| -rw-r--r-- | src/shaders/render/ntsc_common.wgsl (renamed from src/effects/ntsc.wgsl) | 39 | ||||
| -rw-r--r-- | src/tests/gpu/test_demo_effects.cc | 3 | ||||
| -rw-r--r-- | workspaces/main/assets.txt | 4 |
10 files changed, 76 insertions, 35 deletions
diff --git a/PROJECT_CONTEXT.md b/PROJECT_CONTEXT.md index 93e67ca..6b4468f 100644 --- a/PROJECT_CONTEXT.md +++ b/PROJECT_CONTEXT.md @@ -34,7 +34,7 @@ - **Timing System:** **Beat-based timelines** for musical synchronization. Sequences defined in beats, converted to seconds at runtime. Effects receive both physical time (constant) and beat time (musical). Variable tempo affects audio only. See `doc/BEAT_TIMING.md`. - **Workspace system:** Multi-workspace support. Easy switching with `-DDEMO_WORKSPACE=<name>`. Organized structure: `music/`, `weights/`, `obj/`, `shaders/`. Shared common shaders in `src/shaders/`. See `doc/WORKSPACE_SYSTEM.md`. - **Audio:** Sample-accurate sync. Zero heap allocations per frame. Variable tempo. OLA-IDCT synthesis (v2 .spec): Hann analysis window, rectangular synthesis, 50% overlap, click-free. V1 (raw DCT-512) preserved for generated notes. .spec files regenerated as v2. -- **Shaders:** Parameterized effects (UniformHelper, .seq syntax). Beat-synchronized animation support (`beat_time`, `beat_phase`). Modular WGSL composition with ShaderComposer. 27 shared common shaders (math, render, compute). Reusable snippets: `render/scratch_lines`, `math/color` (YIQ/NTSC), `math/color_c64` (C64 palette, Bayer dither, border animation). +- **Shaders:** Parameterized effects (UniformHelper, .seq syntax). Beat-synchronized animation support (`beat_time`, `beat_phase`). Modular WGSL composition with ShaderComposer. 27 shared common shaders (math, render, compute). Reusable snippets: `render/scratch_lines`, `render/ntsc_common` (NTSC signal processing, RGB and YIQ input variants via `sample_ntsc_signal` hook), `math/color` (YIQ/NTSC), `math/color_c64` (C64 palette, Bayer dither, border animation). - **3D:** Hybrid SDF/rasterization with BVH. Binary scene loader. Blender pipeline. - **Effects:** CNN post-processing: CNNEffect (v1) and CNNv2Effect operational. CNN v2: sigmoid activation, storage buffer weights (~3.2 KB), 7D static features, dynamic layers. Training stable, convergence validated. - **Tools:** CNN test tool operational. Texture readback utility functional. Timeline editor (web-based, beat-aligned, audio playback). diff --git a/doc/COMPLETED.md b/doc/COMPLETED.md index 2e229cb..9299a93 100644 --- a/doc/COMPLETED.md +++ b/doc/COMPLETED.md @@ -31,7 +31,7 @@ Completed task archive. See `doc/archive/` for detailed historical documents. ## March 2026 -- [x] **NTSC post-process effect** — Fisheye distortion + NTSC scan-line simulation as `WgslEffect` thin wrapper. Files: `src/effects/ntsc.wgsl`, `ntsc_effect.h`. Tests: 35/35. +- [x] **NTSC post-process effect** — Fisheye distortion + NTSC scan-line simulation as `WgslEffect` thin wrappers. Common logic in `render/ntsc_common` snippet (`sample_ntsc_signal` hook). Two variants: `ntsc_rgb.wgsl` (RGB→YIQ internally, `Ntsc`) and `ntsc_yiq.wgsl` (YIQ passthrough, `NtscYiq`, for RotatingCube output). Files: `src/effects/ntsc_rgb.wgsl`, `ntsc_yiq.wgsl`, `src/shaders/render/ntsc_common.wgsl`, `ntsc_effect.h`. Tests: 36/36. - [x] **Scratch post-process effect** — Film-scratch overlay effect. Scratch logic extracted into reusable snippet `src/shaders/render/scratch_lines.wgsl` (`#include "render/scratch_lines"`). Applied to first two timeline sequences. Tests: 35/35. diff --git a/src/effects/ntsc_effect.h b/src/effects/ntsc_effect.h index d8624ea..4737291 100644 --- a/src/effects/ntsc_effect.h +++ b/src/effects/ntsc_effect.h @@ -1,12 +1,22 @@ -// NTSC post-process effect with fisheye distortion +// NTSC post-process effects: RGB and YIQ input variants. #pragma once #include "effects/shaders.h" #include "gpu/wgsl_effect.h" +// RGB input: converts RGB → YIQ internally (standard post-process use). struct Ntsc : public WgslEffect { Ntsc(const GpuContext& ctx, const std::vector<std::string>& inputs, const std::vector<std::string>& outputs, float start_time, float end_time) : WgslEffect(ctx, inputs, outputs, start_time, end_time, - ntsc_shader_wgsl) {} + ntsc_rgb_shader_wgsl) {} +}; + +// YIQ input: input texture already stores luma/chroma/phase (e.g. RotatingCube output). +struct NtscYiq : public WgslEffect { + NtscYiq(const GpuContext& ctx, const std::vector<std::string>& inputs, + const std::vector<std::string>& outputs, float start_time, + float end_time) + : WgslEffect(ctx, inputs, outputs, start_time, end_time, + ntsc_yiq_shader_wgsl) {} }; diff --git a/src/effects/ntsc_rgb.wgsl b/src/effects/ntsc_rgb.wgsl new file mode 100644 index 0000000..09adbf1 --- /dev/null +++ b/src/effects/ntsc_rgb.wgsl @@ -0,0 +1,20 @@ +// NTSC post-process effect (RGB input): fisheye distortion, scanlines, color bleeding. +// Input texture is in RGB color space; converted to YIQ for NTSC processing. +#include "sequence_uniforms" +#include "render/fullscreen_uv_vs" +#include "math/noise" +#include "math/color" +#include "math/color_c64" +#include "debug/debug_print" + +@group(0) @binding(0) var input_sampler: sampler; +@group(0) @binding(1) var input_texture: texture_2d<f32>; +@group(0) @binding(2) var<uniform> uniforms: UniformsSequenceParams; + +// RGB input: sample texture and convert to luma/chroma/phase +fn sample_ntsc_signal(uv: vec2f) -> vec4f { + let rgba = textureSample(input_texture, input_sampler, uv); + return rgba_to_luma_chroma_phase(rgba, uv.y, YSIZE); +} + +#include "render/ntsc_common" diff --git a/src/effects/ntsc_yiq.wgsl b/src/effects/ntsc_yiq.wgsl new file mode 100644 index 0000000..8ab36d3 --- /dev/null +++ b/src/effects/ntsc_yiq.wgsl @@ -0,0 +1,19 @@ +// NTSC post-process effect (YIQ input): fisheye distortion, scanlines, color bleeding. +// Input texture already stores luma/chroma/phase (e.g. from RotatingCube output). +#include "sequence_uniforms" +#include "render/fullscreen_uv_vs" +#include "math/noise" +#include "math/color" +#include "math/color_c64" +#include "debug/debug_print" + +@group(0) @binding(0) var input_sampler: sampler; +@group(0) @binding(1) var input_texture: texture_2d<f32>; +@group(0) @binding(2) var<uniform> uniforms: UniformsSequenceParams; + +// YIQ input: texture already stores luma/chroma/phase — pass through directly +fn sample_ntsc_signal(uv: vec2f) -> vec4f { + return textureSample(input_texture, input_sampler, uv); +} + +#include "render/ntsc_common" diff --git a/src/effects/shaders.cc b/src/effects/shaders.cc index 44dfa0b..8a81bb0 100644 --- a/src/effects/shaders.cc +++ b/src/effects/shaders.cc @@ -55,6 +55,8 @@ void InitShaderComposer() { AssetId::ASSET_SHADER_DEBUG_DEBUG_PRINT); register_if_exists("render/scratch_lines", AssetId::ASSET_SHADER_RENDER_SCRATCH_LINES); + register_if_exists("render/ntsc_common", + AssetId::ASSET_SHADER_RENDER_NTSC_COMMON); register_if_exists("render/fullscreen_vs", AssetId::ASSET_SHADER_RENDER_FULLSCREEN_VS); register_if_exists("render/fullscreen_uv_vs", @@ -103,7 +105,8 @@ const char* flash_shader_wgsl = SafeGetAsset(AssetId::ASSET_SHADER_FLASH); const char* scene1_shader_wgsl = SafeGetAsset(AssetId::ASSET_SHADER_SCENE1); const char* scene2_shader_wgsl = SafeGetAsset(AssetId::ASSET_SHADER_SCENE2); const char* scratch_shader_wgsl = SafeGetAsset(AssetId::ASSET_SHADER_SCRATCH); -const char* ntsc_shader_wgsl = SafeGetAsset(AssetId::ASSET_SHADER_NTSC); +const char* ntsc_rgb_shader_wgsl = SafeGetAsset(AssetId::ASSET_SHADER_NTSC_RGB); +const char* ntsc_yiq_shader_wgsl = SafeGetAsset(AssetId::ASSET_SHADER_NTSC_YIQ); // Compute shaders const char* gen_noise_compute_wgsl = diff --git a/src/effects/shaders.h b/src/effects/shaders.h index 0686b7e..527a8a3 100644 --- a/src/effects/shaders.h +++ b/src/effects/shaders.h @@ -17,7 +17,8 @@ extern const char* flash_shader_wgsl; extern const char* scene1_shader_wgsl; extern const char* scene2_shader_wgsl; extern const char* scratch_shader_wgsl; -extern const char* ntsc_shader_wgsl; +extern const char* ntsc_rgb_shader_wgsl; +extern const char* ntsc_yiq_shader_wgsl; // Compute shaders extern const char* gen_noise_compute_wgsl; diff --git a/src/effects/ntsc.wgsl b/src/shaders/render/ntsc_common.wgsl index 3ee02bc..61ea254 100644 --- a/src/effects/ntsc.wgsl +++ b/src/shaders/render/ntsc_common.wgsl @@ -1,11 +1,5 @@ -// NTSC post-process effect: fisheye distortion, scanlines, and color bleeding. -// Produces a retro CRT/NTSC look using YIQ color space and C64-style dithering. -#include "sequence_uniforms" -#include "render/fullscreen_uv_vs" -#include "math/noise" -#include "math/color" -#include "math/color_c64" -#include "debug/debug_print" +// Shared NTSC post-process constants and functions. +// Requires sample_ntsc_signal(uv: vec2f) -> vec4f to be defined by the includer. const PI = 3.14159265; const TAU = 6.28318530718; @@ -26,11 +20,6 @@ const CROSS_TALK = 0.1; const CROSS_Y_INTERFERENCE = 30.; const CHROMA_MOD_FREQ = (0.4 * PI); - -@group(0) @binding(0) var input_sampler: sampler; -@group(0) @binding(1) var input_texture: texture_2d<f32>; -@group(0) @binding(2) var<uniform> uniforms: UniformsSequenceParams; - // Barrel (fisheye) distortion: strength > 0 = barrel, < 0 = pincushion fn fisheye(uv: vec2f, scale: f32) -> vec2f { const strength = vec2f(0.1, 0.24); @@ -45,20 +34,6 @@ fn vignette(uv: vec2f) -> f32 { return smoothstep(0., vignetteSmoothness, amount); } -// returns Luma, chroma subcarrier level, phase, transparency -fn get_luma_chroma_phase_a(uv: vec2f) -> vec4f { - let rgba = textureSample(input_texture, input_sampler, uv); - return rgba_to_luma_chroma_phase(rgba, uv.y, YSIZE); -} - -fn get_value(uv: vec2f, off: f32, yscale: f32) -> vec4f { - return get_luma_chroma_phase_a(uv + off * vec2f(1., yscale)); -} - -fn peak(y: f32, ypos: f32, scale: f32) -> f32 { - return clamp((y - 1.) * scale * log(abs(y - ypos)), 0.0, 1.0); -} - // 6-taps Luma horizontal filtering // fs = 3.84 MHz (Nyquist 1.92 MHz) // Passband: 0–2.8 MHz @@ -80,6 +55,14 @@ const chroma_filter = array<f32, 2 * 6 + 1>( 0.0000, 0.3456, 0.2984, 0.1678, 0.0234, -0.0892 ); +fn get_value(uv: vec2f, off: f32, yscale: f32) -> vec4f { + return sample_ntsc_signal(uv + off * vec2f(1., yscale)); +} + +fn peak(y: f32, ypos: f32, scale: f32) -> f32 { + return clamp((y - 1.) * scale * log(abs(y - ypos)), 0.0, 1.0); +} + fn get_signal(uv: vec2f, d: f32) -> vec4f { var signal = vec4f(0.0); for (var i = 0; i <= 12; i += 1) { @@ -88,7 +71,7 @@ fn get_signal(uv: vec2f, d: f32) -> vec4f { let sumc = chroma_filter[i] * get_value(uv, offset * d * CHROMA_SIZE, 0.67); signal += vec4f(suml.x, sumc.y, sumc.z, suml.a); } - let base = get_luma_chroma_phase_a(uv); + let base = sample_ntsc_signal(uv); return mix(signal, base, vec4f(LUMA_BLUR, CHROMA_BLUR, CHROMA_BLUR, 1.)); } diff --git a/src/tests/gpu/test_demo_effects.cc b/src/tests/gpu/test_demo_effects.cc index b9d1463..9d8cb7d 100644 --- a/src/tests/gpu/test_demo_effects.cc +++ b/src/tests/gpu/test_demo_effects.cc @@ -76,6 +76,9 @@ static void test_effects() { {"Ntsc", std::make_shared<Ntsc>( fixture.ctx(), std::vector<std::string>{"source"}, std::vector<std::string>{"sink"}, 0.0f, 1000.0f)}, + {"NtscYiq", std::make_shared<NtscYiq>( + fixture.ctx(), std::vector<std::string>{"source"}, + std::vector<std::string>{"sink"}, 0.0f, 1000.0f)}, }; int passed = 0; diff --git a/workspaces/main/assets.txt b/workspaces/main/assets.txt index a3b1165..b50f2fb 100644 --- a/workspaces/main/assets.txt +++ b/workspaces/main/assets.txt @@ -94,7 +94,9 @@ SHADER_SCENE1, WGSL, ../../src/effects/scene1.wgsl, "Scene1 effect shader" SHADER_SCENE2, WGSL, ../../src/effects/scene2.wgsl, "Scene2 effect shader" SHADER_RENDER_SCRATCH_LINES, WGSL, ../../src/shaders/render/scratch_lines.wgsl, "Film scratch lines snippet" SHADER_SCRATCH, WGSL, ../../src/effects/scratch.wgsl, "Scratch effect shader" -SHADER_NTSC, WGSL, ../../src/effects/ntsc.wgsl, "NTSC effect shader" +SHADER_NTSC_RGB, WGSL, ../../src/effects/ntsc_rgb.wgsl, "NTSC effect shader (RGB input)" +SHADER_NTSC_YIQ, WGSL, ../../src/effects/ntsc_yiq.wgsl, "NTSC effect shader (YIQ input)" +SHADER_RENDER_NTSC_COMMON, WGSL, ../../src/shaders/render/ntsc_common.wgsl, "NTSC shared constants and functions snippet" SHADER_DEBUG_DEBUG_PRINT, WGSL, ../../src/shaders/debug/debug_print.wgsl, "Debug print snippet" # --- Sequence Shaders --- |
