summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorskal <pascal.massimino@gmail.com>2026-03-10 20:42:32 +0100
committerskal <pascal.massimino@gmail.com>2026-03-10 20:42:32 +0100
commit3547424b8c5f3884f84d16fb3f08b47965d62428 (patch)
tree18cfcc23fad133a9cf2e84ba4fb77b1d5826ff8a /src
parent7ca40818e3d0401f97efae6fe876038df4f5bd00 (diff)
ntsc: factor common code into snippet; add RGB and YIQ input variants
- Extract shared NTSC logic into render/ntsc_common.wgsl snippet - sample_ntsc_signal() hook decouples input format from processing - ntsc_rgb.wgsl: RGB input (converts via rgba_to_luma_chroma_phase) - ntsc_yiq.wgsl: YIQ passthrough for RotatingCube output - Add NtscYiq WgslEffect thin wrapper; register both in tests handoff(Claude): NTSC refactor complete; NtscYiq ready for timeline use with RotatingCube. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Diffstat (limited to 'src')
-rw-r--r--src/effects/ntsc_effect.h14
-rw-r--r--src/effects/ntsc_rgb.wgsl20
-rw-r--r--src/effects/ntsc_yiq.wgsl19
-rw-r--r--src/effects/shaders.cc5
-rw-r--r--src/effects/shaders.h3
-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.cc3
7 files changed, 71 insertions, 32 deletions
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;