Skip to content

Color pipeline P8: chipset-aware quantization, dithering, 5-bit semantics #4042

Closed

Description

@zackees

Phase P8 of the color pipeline implementation (see parent issue). Independently mergeable.

Scope

The chipset boundary: final quantization, dithering, and per-chip drive semantics.

  • Encoders consume wide pipeline output directly — no RGB8 correction round-trip. Chipset encoders own only genuinely chipset-specific quantization/current encoding (§6); no color science duplicated in drivers.
  • Per-chip 5-bit semantics (B1): APA102 = secondary slow-PWM stage — joint code/field solve carries an explicit flicker constraint (configurable minimum 5-bit floor, conservative default); SK9822 = current gain — field held fixed below the documented threshold unless the profile carries current-vs-chromaticity data; unknown chips get the SK9822-conservative treatment. No second correction/gamma stage after the device solve; UCS7604's built-in expansion applies only in its 16-bit modes (audit smaller-note) and the encoder table records which modes embed one.
  • WS2812-class 8-bit study (§5): compare independent RGB gamma, value-only shaping, HUE16/HSV strategies, colorBoost(), linear-light temporal dithering — measured on dark-level hue and neutral-axis error, not smoothness alone.
  • Pipeline dithering (C5): mutually exclusive per channel with legacy BINARY_DITHER; TINY tier limited to stateless/temporal forms; error diffusion tier-gated to the bounded line buffer reserved in P6; flicker constraints documented.
  • HD/16-bit encoders (APA102HD/SK9822HD, HD108, UCS7604-16): monotonic output, improved low-light resolution, verified against the P5 harness.

Decisions implemented

B1, C5, §5, §6.

Acceptance criteria owned

  • WS2812-class gamma/value/HUE16 + temporal dithering tests with effective-step/hue/neutral/flicker metrics.
  • APA102/SK9822 HD + native 16-bit encoders consume wide output; monotonic; improved low-light resolution.

Dependencies

P6 (pipeline core), P7 (solved drive values to encode).

Deliverables

Encoder integrations replacing P6's pass-through stub, dithering implementation, WS2812-class strategy report, per-chip semantics table wired from profile data.

Activity

  1. zackees commented on Sep 7, 2026

    @zackees
    MemberAuthor

    P6's wiring (#4200) quantizes the pipeline's drives to 8 bits and hands them to the encoder — the "pass-through final encoder stub" this phase is meant to replace. Before starting the §5 strategy comparison I measured how much that stub actually costs, so the study has a baseline to beat rather than an assumption.

    The dark-level cost is large and worth the study

    A saturated target swept from black to full, quantized to 8 bits per channel in linear light, scored against the unquantized ideal:

    level 8-bit codes hue error ΔE2000
    0.002 (1, 0, 0) 21.9° 7.31
    0.005 (1, 0, 0) 22.2° 5.45
    0.01 (3, 1, 0) 43.9° 14.54
    0.02 (5, 2, 1) 15.7° 5.25
    0.05 (13, 4, 2) 8.5° 3.61
    0.1 (26, 9, 3) 1.2° 0.59
    1.0 (255, 89, 31) 0.4° 0.27

    So the effect §5 is about is real: 44° of hue error at 1% drive, against A1's whole-pipeline budget of 0.5 ΔE2000. Above about 10% it is already negligible, which usefully bounds where any strategy has to earn its keep — the interesting range is roughly the bottom 5%.

    The non-monotonicity between 0.005 and 0.01 is the quantization lattice, not noise: at (1,0,0) two channels have already collapsed to zero, so the hue is pinned to the red primary and cannot drift further.

    A note on the neutral-axis metric, because I got it wrong first

    My first attempt measured "does a grey ramp stay grey" by feeding equal drives and taking Lab chroma against D65. That produces nonsense — 226 units of chroma at full drive — and the reason is worth writing down: on the corpus's rgb device the emitters are normalized to unit luminance each, so equal drives are neither D65 nor unit luminance. The number measured my model, not the quantizer.

    The neutral-axis criterion has to be evaluated on a neutral target through the pipeline, not on equal drives. That is not a detail — it is the difference between measuring the strategy and measuring the device normalization.

    An open question about the study's shape

    P7's algorithm selection worked as a Python study because every candidate was a few lines of arithmetic. §5's candidates are not: colorBoost(), the HUE16/HSV path and the gamma LUTs are real implementations in src/fl/gfx/, and re-modelling them in Python risks scoring a paraphrase rather than the code that ships.

    So this one probably wants to run in C++ against the real functions, with the scoring done on the resulting light. That is a different shape from the P7 report and worth settling before the harness is written, rather than discovering halfway through that the Python model and colorBoost() disagree.

  2. zackees commented on Sep 10, 2026

    @zackees
    MemberAuthor

    The harness question is settled, and two candidates are out (#4251)

    Picking up my own open question from above — whether §5 should run in Python or C++ — the answer is C++, for the reason I flagged: the candidates are real implementations and a Python model would score a paraphrase. So the paths are the shipping functions called directly, and the scoring is the shipping colorimetry (rgb_source_to_XYZ + xyzToOklabQ16) rather than a second copy.

    That let the first, unambiguous half of §5 be answered without settling the whole strategy-space question: what does each existing path cost on its own? A path that moves hue or tints neutrals by itself cannot be part of the answer, whatever it does for smoothness.

    path worst hue drift chroma ratio collapses to black
    identity (control) 0.000° 1.000x 0 / 360
    HSV16 round trip 0.649° 0.973x .. 1.003x 0 / 360
    colorBoost(EASE_IN_QUAD, EASE_NONE) 142.969° 0.170x .. 6.479x 0 / 360
    colorBoost(EASE_NONE, EASE_IN_QUAD) 143.798° 0.288x .. 2.123x 254 / 360

    colorBoost is out, and the reason is checkable by hand

    (1, 3, 1)  ->  (0, 3, 0)
    

    A dim, lightly saturated colour becomes pure green. Easing saturation upward at low codes has nothing to work with, and rounding takes both minor channels to zero — 143° of hue. Not near-axis ill-conditioning: the sample stays visibly chromatic on both sides, and all 360 survive the chroma screen.

    The range where §5 needs a strategy is exactly the range where colorBoost destroys the colour. It stays a fine appearance control — which is what it was written to be — but it is not a neutral 8-bit re-encoding, and the study now says so in a form a later comparison cannot overlook.

    The luminance-easing form drives 254 of 360 samples to black. It is a brightness curve and belongs with brightness.

    HSV16 survives at under 0.65° hue and ±3% chroma, so the HUE16 path can carry a strategy without contributing error of its own.

    The neutral-axis target

    My note above said the criterion has to be evaluated on a neutral target, not on equal drives. Doing that:

    0.0605 OKLab chroma at 2% luminance, from rounding three drives to 8 bits. Neither existing path improves it; colorBoost makes it worse (0.0676). That is the number any dithering strategy has to beat.

    Two harness bugs worth recording

    Both were found by measuring, and either one silently invalidates the result:

    1. A fully saturated sweep makes colorBoost's saturation easing a no-op — no headroom to boost — and it then scores as perfectly colour-preserving for the one reason that says nothing about it. The first run reported chroma gain of exactly 1.000x.
    2. I repeated my own equal-drives mistake in the first neutral metric. The test now pins the correct form: substituting equal drives for the solved D65 target fails it.

    What is left for §5

    Two of five candidates gone. The remaining comparison is independent RGB gamma vs value-only shaping over HSV16 vs linear-light temporal dithering, against 0.0605 as the target.

    Temporal dithering is stateful and wants the frame cadence P6 wires up, so it stays behind #4200. The gamma LUT paths are strategies rather than fixed paths, and scoring them still needs the strategy-space definition the §5 comparison proper has to settle — that part of my open question is not closed here.

  3. zackees commented on Sep 10, 2026

    @zackees
    MemberAuthor

    The black floor for §5, measured rather than assumed (#4269)

    Section 5 is comparing shaping strategies against the 0.0605 neutral-chroma target, with temporal dithering parked behind #4200. One input that comparison will need is now measured: where the bottom of the range actually is, from #4156 R8.

    PixelController::dither() is b ? qadd8(b, d) : 0, so a source code of zero is excluded from dithering by construction. Nothing below one source code is reachable at any brightness, refresh rate, or cycle length. The black floor is the quantization of the source, not something a longer dither cycle reaches under.

    That matters for §5 in two ways:

    1. Any strategy that relies on temporal averaging to reach below one 8-bit code needs a different mechanism from BINARY_DITHER, which recovers precision lost in the brightness multiply and is gated on a nonzero source. A pipeline dither operating on 16-bit values before quantization is a different animal, and the comparison should not assume the shipped one generalizes.
    2. The bottom two or three codes carry a systematic upward bias. Over a full cycle, source code 1 at premixed 255 emits 11 against an exact 8 (+37.5%) and code 2 emits 19 against 16 (+18.8%); by code 64 it is under 2%. Any §5 measurement taken at very low codes is measuring that bias as well as the strategy.

    Both are pinned in tests/pixel_controller.cpp as sums over a whole eight-frame cycle, which makes them phase-independent, and written up in docs/color-ws2812-shaping-paths.md.

  4. zackees commented on Sep 10, 2026

    @zackees
    MemberAuthor

    Section 5's static half is answered (#4277)

    The blocker was the strategy-space definition:

    The gamma LUT paths are not scored here either — they are shaping strategies rather than fixed paths, and scoring them requires the strategy-space definition that the section-5 comparison proper has to settle.

    #4265 removed the need for one. It measured what any static strategy can reach, and a single frame cannot beat the best code that frame can emit. So both candidates are scored at their best rather than through a particular curve, and the numbers bound the family instead of one member.

    Independent RGB gamma is out

    Worse than plain rounding at 40 of 40 targets, by at least 2.3× even where rounding does worst:

    luminance rounding gamma 2.2
    2% 0.0605 0.1417
    8% 0.0247 0.1734
    14% 0.0108 0.1930

    Same reason colorBoost went in #4251: a WS2812 drives its diodes with linear PWM, so nothing downstream decodes the curve — the emitted light is the code, not the drive the code came from. An appearance control, not a re-encoding.

    Value-only shaping over HSV16 survives, and takes about a third

    Never worse than rounding (0 of 40 regress), because hue and saturation come from the rounded code and only the value moves. Every 16-bit value is searched, so this bounds the whole V-only family.

    at 2% luminance chroma
    per-channel rounding 0.0605
    best value-only shaping 0.0530
    static ceiling (#4265) 0.0382

    0.0075 of an available 0.0223 — about a third of the room a single frame has.

    It lands at or below the distance-optimal code's chroma at 36 of 40 targets; the four exceptions are the distance-versus-chroma disagreement #4265 recorded, not a failure here.

    What is left

    Linear-light temporal dithering is the only candidate that could close the other two thirds, and it is stateful and waits on the frame cadence P6 wires up (#4200). Worth pairing with the black-floor result above: a pipeline dither operating on 16-bit values before quantization is a different mechanism from BINARY_DITHER, which is gated on a nonzero source and recovers precision lost in the brightness multiply.

    So section 5's remaining work is one candidate, and it is behind P6 rather than behind a definition.

  5. zackees commented on Sep 10, 2026

    @zackees
    MemberAuthor

    Section 5 is fully scored — the last candidate did not need the cadence (#4280)

    Following #4277, which left linear-light temporal dithering as the remaining candidate "behind P6". That framing was mine and it was half wrong: the cadence is needed to ship it, not to score it. An N-frame cycle's time-averaged light is arithmetic, and #4265's static ceiling is the same construction with N = 1.

    A cycle that spends k of its N frames one code higher averages to a drive of base + k/N, per channel. Averaging is done in linear XYZ, not OKLab — light averages, perceptual coordinates do not.

    Worst case over the same 40 targets

    worst chroma
    per-channel rounding 0.0605
    static ceiling 0.0466
    temporal, 2 frames 0.0304
    temporal, 4 frames 0.0109
    temporal, 8 frames 0.0086

    At the 2% target — rounding 0.0605, best single frame 0.0382 — eight frames reach 0.0016. It does not close the last third; it closes almost all of it. And eight frames is the cycle BINARY_DITHER already runs, so the cadence question is not new.

    Section 5, complete

    candidate verdict
    colorBoost saturation easing out (#4251)
    colorBoost luminance easing out (#4251)
    independent RGB gamma out — worse at 40/40 targets (#4277)
    value-only shaping over HSV16 survives, takes about a third (#4277)
    linear-light temporal dithering worth the state it costs (#4280)

    What this does not settle

    Three limits are in the doc rather than implied. It is a ceiling, not an algorithm's score. It says nothing about flicker — a cycle that reaches the floor by toggling the bottom codes is exactly the trade #4156 R8 asks to have declared, and that is now the open question rather than "which strategy". And it assumes a pipeline dither that can choose sub-code average drives, which the black-floor result above establishes is a different mechanism from BINARY_DITHER.

    So what is left for section 5 is building it, and the flicker budget that decides how long the cycle may be.

  6. zackees commented on Sep 10, 2026

    @zackees
    MemberAuthor

    The monotonicity half of P8's second criterion is now measured (#4289)

    Section 5 is fully scored above, and what is left there is building it plus the flicker budget. Meanwhile P8's other owned criterion had a half nobody had checked:

    APA102/SK9822 HD + native 16-bit encoders consume wide output; monotonic; improved low-light resolution.

    Monotonicity was covered for the gamma LUTs, the transfer functions and OKLab — but not for the encoders, which is where it is least obvious. The five-bit path is a joint solve of an 8-bit code against a shared 5-bit current field, and that shape is the classic source of reversal: a one-step rise in the input moves the solver to a different field and lands lower than it started.

    It does not reverse. Zero reversals across neutral ramps at six brightnesses, a channel rising under a pinned maximum, rising global brightness, and a non-uniform colors_scale. Measured in emitted light (code * field) rather than in the code, since an APA102-class part drives PWM duty times current — checking the code alone would miss the failure being looked for. The solve is genuinely exercised: 31 of 32 fields appear across one neutral ramp, and a vacuity case pins that.

    HD108 is not the same shape, and that changes what the criterion means for it

    hd108BrightnessHeader discards its brightness argument and pins all three gains to 31, with brightness applied to the 16-bit values before encoding. The only caller passes 255 and pre-scales through makeScaledPixelRangeRGB, so the behaviour is right — but there is no field for a rising input to move, and HD108's monotonicity reduces to its 16-bit code's.

    Worth recording because it bears on the phase's wording: "native 16-bit encoders consume wide output" reads as though HD108 has a joint code/field solve to do. It does not today — it takes maximum gain unconditionally. If P8 intends a joint solve there (trading gain against code for low-light resolution, as the APA102 arm does), that is unbuilt rather than built-and-verified.

    I found this the hard way: an earlier draft read a gain nibble as though HD108 had a per-input field, it is constant, and the sweep was trivially monotonic and passing until the vacuity guard said so. The guard is kept with that story in its comment.

    Also noted, not fixed

    encodeHD108's global_brightness is accepted, documented at the public entry as "Global 8-bit brightness (0-255)", and never used — only the helper in encoder_utils.h says it is ignored. Not a live bug, since the sole caller passes 255 and pre-scales. A caller using the template directly with another value would get no dimming.

  7. zackees commented on Sep 10, 2026

    @zackees
    MemberAuthor

    The cadence number is now a named constant, and the case that prices it actually reads it (#4294)

    Following the cadence section in docs/color-ws2812-shaping-paths.md, which ends with:

    Worth having before any pipeline dither is built, because that one modulates harder.

    Before building anything, I went back to check that the existing record would hold. It would not.

    The case in tests/pixel_controller.cpp says it "fails if either number moves so that whoever moves it writes down why". It did not fail. show() and showColor() each carried a bare if (mNFPS < 100), and the case carried its own const int kDitherEnableFps = 100 because there was no constant to read. Changing the threshold moved the behaviour and failed nothing — the same defect #4279 fixed elsewhere.

    FL_DITHER_ENABLE_MIN_REFRESH_HZ now exists once, both call sites use it, and the case reads it. Setting it to 200 fails three assertions including the 12.5 Hz cadence pin; before, that mutation passed.

    No behaviour change — the value is still 100, and the 4x gap against MIN_ACCEPTABLE_DITHER_RATE_HZ is still there. Raising it to MAX_LIKELY_UPDATE_RATE_HZ, which is the refresh that would actually reach the floor, disables dithering for most sketches, so it remains this phase's decision rather than a cleanup I should make.

    What changes is that the arithmetic is at the definition with the consequence attached, instead of a literal in two places that nothing was watching. When the flicker budget is decided, the number it moves is now a single named thing with a case behind it.

  8. 16 remaining items

  9. zackees commented on Sep 19, 2026

    @zackees
    MemberAuthor

    P8 closed: both acceptance criteria met, every scope item has a merged artifact

    Acceptance criteria

    criterion evidence
    WS2812-class gamma/value/HUE16 + temporal-dithering tests with effective-step / hue / neutral / flicker metrics §5 study: #4251, #4265, #4277, #4280, docs/color-ws2812-shaping-paths.md. Temporal dither: #4451, with tests for effective step (≤ 1/16 code vs rounding's 0.5), one-code amplitude, whole-strip flicker (constant total light per frame) and shared phase, so equal drives stay equal. Each is mutation-checked.
    APA102/SK9822 HD + native 16-bit encoders consume wide output; monotonic; improved low-light resolution UCS7604-16 #4368 and HD108 #4387 consume the drive. APA102-class HD joint code/field solve on the 16-bit drive #4453: monotonic over all 65,536 drive values, and floor 1 resolves more than 8× the low-light levels of a fixed field. Legacy HD monotonicity is pinned by #4289.

    Scope and decisions

    Re-assigned by follow-up issues (per P10's rule)

  10. moved this from Triage to Done in FastLED Trackeron Sep 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions