Skip to content

Native 16-bit encoders (HD108, UCS7604-16) are fed 8 bits and widened by a gamma LUT #4326

Closed

Description

@zackees

P8 (#4042) / B1 / §6 say plainly: no second correction or gamma stage after the device solve. #4156 R9 asked for ChannelOptions::mGamma to be brought into the managed-mode compatibility policy "so a legacy default cannot add unintended shaping". It has not been, and the shaping is a default rather than something a user opted into.

What is in the code

fl::Channel already excludes the legacy colour knobs when a profile binds, in three places:

if (!options.hasColorProfile()) { setCorrection(options.mCorrection); setTemperature(options.mTemperature); }

src/fl/channels/channel.cpp.hpp:307, :337, :414. showPixels states the consequence: "binding a profile sets correction and temperature to identity, so nothing else is folded into it".

mGamma has no such guard. writeUCS7604 (:476) reads it unconditionally:

float gamma = settings.mGamma.value_or(2.8f);
fl::shared_ptr<const Gamma8> gamma8 = Gamma8::getOrCreate(gamma);

and hands it to encodeUCS7604_16bit_RGB/RGBW. It receives settings — it has hasColorProfile() available and does not ask. So a UCS7604 16-bit channel with a bound profile gets gamma 2.8 applied on top of the pipeline's device drives, which is the stage the contract forbids, and 2.8 is the value_or default rather than a user's choice.

HD108 is the same shape and worse: hd108GammaCorrect is hardcoded 2.8 inside encodeHD108, and writeHD108 takes no settings at all, so the information needed to suppress it is not even in scope.

What I have not done

I have not measured this end to end. The above is read from the call chain — three guarded sites against one unguarded one — not from an encoded frame. The reason is that there is no coverage to extend: tests/fl/chipsets/ucs7604.cpp drives testUCS7604Controller, the legacy CLEDController path, so the Channels writeUCS7604 has no test at all, managed or otherwise. Building that harness is the first piece of work here, and the claim should be confirmed by it before the fix is judged.

Why the fix is not a one-line guard

The obvious parallel to setCorrection would be selecting gamma 1.0 when a profile is bound. That is not identity: Gamma8Impl computes the LUT in fixed point and is documented as landing "within ~1 LSB of true float pow()" at u16 output. Passing 1.0 would leave a near-identity shaping stage with rounding error, which is still a stage.

Doing it properly means the 16-bit encoders need a no-gamma path — a straight map8_to_16 — selected when the channel is colour-managed.

Related, same criterion, probably the same fix

P8 also owns "encoders consume wide output directly — no RGB8 correction round-trip". The native 16-bit encoders do a round trip:

encoder fed by width in
WS2816 makeScaledPixelRangeRGB16 16-bit
HD108 makeScaledPixelRangeRGB 8-bit, expanded by gamma 2.8
UCS7604 16-bit makeScaledPixelRangeRGB 8-bit, expanded by a Gamma8 LUT

So both 16-bit chipsets reach at most 256 distinct levels per channel out of 65536, and the gamma LUT is what performs the widening — which is why removing the gamma and consuming wide output are one job rather than two.

Not urgent for an unbound channel, where the 8-bit-plus-gamma path is the legacy behaviour and correct. It bites exactly when a profile is bound, which is the case the pipeline exists to serve.

🤖 Generated with Claude Code

https://claude.ai/code/session_01MLhWkMfzLjrnLTDMBE6Fj9

Activity

  1. zackees commented on Sep 10, 2026

    @zackees
    MemberAuthor

    Correction: the exclusion is already there, and I reported this from the wrong layer

    I built the harness this issue asks for as its first work item, and it refuted the issue.

    mGamma is on the profile-binding exclusion list. ChannelOptions::setColorProfile() (src/fl/channels/options.h) ends with:

    mCorrection = UncorrectedColor;
    mTemperature = UncorrectedTemperature;
    mGamma.reset();
    mDitherMode = DISABLE_DITHER;

    I looked for the guard at the three if (!options.hasColorProfile()) sites in channel.cpp.hpp and, not finding mGamma there, concluded it had none. It is cleared one layer up, at the options object, together with exactly the knobs I said it was missing from. The issue title and its central claim are wrong.

    Measured rather than read this time: two channels differing only in requested gamma emit byte-identical frames once a profile is bound, while the same pair with no profile bound differ. Both are now cases in tests/fl/channels/channel.cpp.

    I should have measured before filing. The whole reason I flagged it as read-not-measured was that this path had no coverage — and the right response to that was to write the coverage first, which is what turned it around.

    What survives, narrowed

    writeUCS7604 reads settings.mGamma.value_or(2.8f). Clearing the caller's gamma leaves the fallback 2.8 shaping stage in place. That is not yet a contract breach, because isColorManaged() is false on a profile-bound channel today — the profile binds and is owned, but P6 rendering is not active, so there is no device solve for a gamma to come after. Pinned as a case so the day it flips to true is visible.

    So the open question is smaller and later than I wrote it: when P6 rendering activates on this path, does value_or(2.8f) need to become identity? That is a real question for #4042 and not a defect today.

    Unexplained, and stated as an observation only

    A profile-bound channel and an unbound one produce very different bytes on this path: red 127 encodes to 0x245B (9307, exactly gamma_2_8(127)) unbound, and to 0x007A (122) bound — about 76x darker. Both are stable and reproduce with the driver registry cleared between runs, so it is not dispatch noise. setColorProfile also sets correction and temperature to identity, which should make the bound case brighter, not darker. I do not have an explanation, and I am not going to guess at one in an issue after guessing wrong once already. Worth its own look.

    What the wide-output half leaves standing

    Unaffected by any of the above, and still true from the call chain: writeHD108 and UCS7604's 16-bit modes are both fed makeScaledPixelRangeRGB (8-bit) and widened by a gamma LUT, while WS2816 alone gets makeScaledPixelRangeRGB16. That part of the report stands.

    Retitling would be appropriate; I have left the title alone rather than quietly rewriting the record.

  2. changed the title [-]Managed mode still applies encoder gamma: mGamma is not on the profile-binding exclusion list[/-] [+]Native 16-bit encoders (HD108, UCS7604-16) are fed 8 bits and widened by a gamma LUT[/+] on Sep 10, 2026
  3. zackees commented on Sep 10, 2026

    @zackees
    MemberAuthor

    Retitled. The original title — that managed mode still applies encoder gamma because mGamma is not on the profile-binding exclusion list — is false, as the comment above records: ChannelOptions::setColorProfile() clears it. The title now names the half that survived measurement. The original wording stays in the history and in that comment rather than being erased.

  4. zackees commented on Sep 11, 2026

    @zackees
    MemberAuthor

    Measured end to end, which this issue noted it had not been: "I have not measured this end to end ... the claim should be confirmed by it before the fix is judged."

    The harness now exists — tests/fl/channels/channel.cpp drives fl::Channel's own writeUCS7604 through a capturing driver — and both halves of the issue turn out to be one number.

    The managed drive is 8-bit quantized, then gamma-encoded

    Fixture: kProfile gives all three emitters luminance 1.0 at sRGB chromaticities; source is linear sRGB; dither off.

    Full red is a target of Y = 0.2126 (sRGB red's luminance share) against an emitter making Y = 1.0 at full drive, so the device solve's answer is a red drive of 0.2126. That is correct, and it is where the pipeline's job ends.

    red, 16-bit
    what a wide encoder should emit for drive 0.2126 13933
    what goes on the wire 849
    unbound, same pixel 65535
    bound, black 0

    849 decodes exactly: 0.2126 × 255 = 54, then (54/255)^2.8 × 65535 = 849. So the drive is narrowed to 8 bits before the encoder sees it, and the gamma-2.8 stage that B1/§6 forbid after the device solve is applied on top of it. A factor of 16.4 too dark, from a value_or(2.8f) default rather than anything a user asked for.

    Confirmed causally, not just by the arithmetic: changing that default to 1.0f moves the value and fails the case. It leaves the unbound case untouched, because there mGamma is set explicitly and value_or never fires — which is itself the confirmation that the managed path is running on the default.

    The resolution cost, quantified

    On the unbound path, sweeping all 256 source codes:

    • 252 distinct 16-bit values of 65536 — four pairs collide in the gamma LUT;
    • endpoints reached (top = 65535), so the range is used and the loss is resolution inside it;
    • widest neighbouring step: 717 counts near full scale, so 716 of every 717 representable values are unreachable there.

    That puts a number on "at most 256 distinct levels per channel out of 65536".

    What is checked in

    Two characterization cases, written to fail when this is fixed — a wide-fed encoder would emit near 13933 — which is the point of recording the current behaviour now. The fix itself is untouched here: it still needs the no-gamma path this issue describes, and HD108 still has no settings in scope at all.

  5. moved this from Triage to Done in FastLED Trackeron Sep 11, 2026
  6. zackees commented on Sep 11, 2026

    @zackees
    MemberAuthor

    Correction to the table in this issue, measured rather than read.

    encoder fed by width in
    WS2816 makeScaledPixelRangeRGB16 16-bit

    WS2816 is fed 8 bits too. ScaledPixelIteratorRGB16::advance() (src/fl/chipsets/encoders/pixel_iterator.h:495) loads the pixel through the 8-bit primitive and widens it:

    u8 b0, b1, b2;
    mPixels->loadAndScaleRGB(&b0, &b1, &b2);
    
    const u16 r16 = fl::map8_to_16(b0);
    const u16 g16 = fl::map8_to_16(b1);
    const u16 b16 = fl::map8_to_16(b2);

    So all three 16-bit chipsets reach at most 256 distinct levels per channel, not two of them. The difference is only in what does the widening: a gamma LUT for HD108 and UCS7604-16 (which is also the forbidden second shaping stage, measured in #4365 as 849 where the drive wants 13933), and an exact map8_to_16 for WS2816. WS2816 is therefore correct-but-coarse, where the other two are coarse and wrong under a bound profile.

    The blocker is upstream of the encoders

    That file already says so, in the comment immediately above:

    Doing that properly needs a primitive that loads the pixel scaled at full brightness, which PixelIterator does not expose today.

    And the managed path narrows before it ever reaches an iterator — ColorManagedPixelSource::loadAndScaleRGB runs processPixelQ16 in s16.16 and then calls quantize() to u8 (src/fl/channels/color_managed_source.cpp.hpp:11). makeScaledPixelRangeRGB16 exists but has no callers in src/ at all.

    That reframes the fix. It is not "give HD108 and UCS7604-16 a no-gamma path" — a no-gamma path fed by loadAndScaleRGB would still be 8-bit, just linearly widened, i.e. WS2816's situation. P8's "encoders consume wide output directly" needs:

    1. a wide load primitive on PixelIterator (the piece its own comment names as missing);
    2. a u16 output path on ColorManagedPixelSource, so the s16.16 drives are quantized once to 16 bits instead of to 8;
    3. the three 16-bit encoders consuming it, which is where the gamma stage drops out for the two that have one.

    (1) and (2) are the work; (3) is the part this issue describes. HD108's missing settings argument remains a separate, smaller problem on top.

    I have not implemented any of it — flagging that the scope is upstream of where this issue locates it, since the ordering matters for whoever takes it.

  7. zackees commented on Sep 11, 2026

    @zackees
    MemberAuthor

    One more note on step (1), since it is the part that looks expensive and turns out not to be.

    PixelIterator is not a C++ vtable — it is hand-built type erasure with one function pointer per operation stored in each instance, and the header says why:

    we really care about binary size since FastLED needs to run on those tiny little microcontrollers like the Attiny85 ... an optional zero-cost abstraction which doesn't affect the binary size for platforms that don't use it

    So a wide load primitive costs a pointer per PixelIterator on every platform — which is exactly the objection that would sink it on AVR.

    Except the file already solves this. Two of its pointers are conditional:

    #if FASTLED_HD_COLOR_MIXING
    mLoadRGBScaleAndBrightness = &Vtable::loadRGBScaleAndBrightness;
    mGetHdScale = &Vtable::getHdScale;
    #endif

    A wide pointer guarded by FL_COLOR_PROFILE_RUNTIME — which is !FL_PLATFORM_HAS_TINY_MEMORY, the same condition the whole pipeline already sits behind — costs a TINY target nothing, and on every other target it costs one pointer. That is the same trade the HD pointers already make, so it needs no new argument.

    Which means the shape is:

    1. loadAndScaleRGB16 on PixelIterator, guarded like the HD pair;
    2. ColorManagedPixelSource::loadAndScaleRGB16 quantizing the s16.16 drives to u16 instead of calling quantize() to u8;
    3. ScaledPixelIteratorRGB16::advance() calling it instead of loadAndScaleRGB + map8_to_16.

    Unbound channels keep the existing widen-from-8 path, so legacy output stays byte-identical and the #4034 criterion holds. WS2816 is the natural first consumer: it already has a wide encoder and no gamma stage, so it isolates "consume wide output" from "stop applying gamma", which are otherwise entangled in HD108 and UCS7604-16.

    Still not implemented — but the footprint question, which is the one that makes this look like a decision, has a precedent in the same file.

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