Skip to content

refactor(dither): extract binary dithering into a tested, zero-cost, disableable module (and fix uninitialised d/e under NO_DITHERING) #4672

Closed

Description

@zackees

Summary

Temporal binary dithering lives inline in PixelController (src/pixel_controller.h) as two raw arrays (d[3], e[3]) plus ~6 member functions, with the arithmetic copied into several places. Pull the algorithm out into one small, header-only, unit-tested module that compiles to the same code when enabled and to nothing when disabled. AVR flash must not grow.

Where the algorithm lives today

Piece Location
Per-frame setup (bit-reverse the phase, centre it, scale by 256/premixed) PixelController::init_binary_dithering()
The same setup again with a loop instead of an unrolled chain PixelController::reseed_binary_dithering() (#4347)
Per-pixel toggle d = e - d stepDithering(), preStepFirstByteDithering()
Apply b ? qadd8(b, d) : 0 dither<SLOT>() (two overloads), loadAndScaleChannel()
"Is dithering on?" (e[0] | e[1] | e[2]) != 0 fl/channels/channel.cpp.hpp, fl/channels/slim_spi_bridge_controller.h, fl/channels/color_managed_source.cpp.hpp
Disable enable_dithering(DISABLE_DITHER) zeroes the arrays; NO_DITHERING compiles out only the setup body

Hand-written assembly and per-platform structs read pixels.d[]/pixels.e[] directly: platforms/avr/clockless_avr.h, platforms/avr/attiny/clockless_blocking*.h, platforms/arm/{lpc,kl26,nrf51,teensy/teensy_lc}, platforms/arm/teensy/teensy4_common/block_clockless_arm_mxrt1062.h. So the fields must keep their names and stay public on PixelController.

Bug found while auditing

NO_DITHERING is forced on for every FL_IS_AVR_LIMITED chip (platforms/avr/led_sysdefs_avr.h: all ATtiny parts, ATmega32U2/16U2/8U2, AT90USB162). Under it, init_binary_dithering() has an empty body. The default dither mode is still BINARY_DITHER (fl/channels/options.h, CLEDController), so the constructor calls enable_dithering(BINARY_DITHER) → empty init → d[] and e[] are never written. The AVR clockless asm then adds those uninitialised stack bytes to every byte it sends (PRESCALEA2(d0) / MOV_ADDDE04), and stepDithering() toggles them. The result is undefined behaviour: random brightness offsets on ATtiny.

It is narrower than "every frame" because CFastLED::show() calls setDither(0) while mNFPS < FL_DITHER_ENABLE_MIN_REFRESH_HZ, and mNFPS starts at 0, so the first FastLED.show() turns dither off. It is still reachable in two ways:

  • the sketch calls FastLED.setDither(BINARY_DITHER) (documented API) after that first frame;
  • the sketch drives a controller directly (controller->showLeds() / CLEDController::show()).

Plan

  1. New module src/fl/gfx/binary_dither.h. Header-only, everything FASTLED_FORCE_INLINE, operating on caller-owned u8 d[3]/u8 e[3]:

    • phase(frame): bit-reverse to VIRTUAL_BITS and centre. One implementation, shared by the setup and the reseed.
    • init(d, e, scale, frame), reseed(d, e, frame), clear(d, e), active(e).
    • step(d, e), stepChannel(d, e, c), apply(b, d).

    Two policies share this interface: fl::BinaryDither (the algorithm) and fl::NoDither (init → clear, step no-op, apply(b, d) → b). fl::Dither selects between them from NO_DITHERING. Disabling it is one switch, and once disabled the work compiles away rather than running against zeros.

  2. PixelController keeps d[3]/e[3] (layout and names unchanged) and its existing method names. Each becomes a one-line forward to fl::Dither. No platform driver changes.

  3. Replace the three open-coded (e[0]|e[1]|e[2]) checks with PixelController::ditherActive().

  4. Fix the NO_DITHERING bug. With NoDither, enable_dithering() always clears d/e. This should cost nothing: the clear branch already exists for DISABLE_DITHER, and the mode compare goes away.

Acceptance

  • fl::BinaryDither unit tests (tests/fl/gfx/binary_dither.cpp):
    • phase() matches the documented table 16, 144, 80, 208, 48, 176, 112, 240 and wraps every 8 frames.
    • init gives e = 256/s and d ∈ [0, e]. A zero scale gives d = e = 0.
    • reseed(f) matches init(f) for every frame and scale.
    • step toggles d and e - d, and stepping twice is the identity.
    • apply never lifts 0 and saturates at 255.
    • Over one 8-frame cycle the mean of scale8(apply(v, d), s) is closer to v·s/255 than the undithered value, for a sweep of v/s.
    • A golden test that the module reproduces the pre-refactor PixelController output byte-for-byte.
    • NoDither clears state, never changes a byte, and active() is false.
  • Existing tests/pixel_controller.cpp dither tests pass unchanged.
  • bash bloat <board> --build for AVR (uno, leonardo, nano_every, attiny85) × (Blink, Apa102, ColorPalette): image_flash before vs after posted on the PR, with no increase.
  • bash test --cpp and bash lint are green.

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