Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

_pixelbuf: Increase performance of brightness-scaling #4258

Merged
merged 2 commits into from
Feb 25, 2021

Conversation

jepler
Copy link
Member

@jepler jepler commented Feb 24, 2021

On the Pico, this increases the "fill rate" of

    pixels[:] = newvalues

considerably. On a strip of 240 RGB LEDs, auto_write=False, the timingsare:

Brightness Before After Improvement
1.0 117 kpix/s 307 kpix/s 2.62x
0.07 117 kpix/s 273 kpix/s 2.33x

It's worth noting that even the "before" rate is fast compared to the time to transmit a single neopixel, but any time we can gain back in the whole pipeline will let marginal animations work a little better. To set all the pixels in this way and then show() gives a pleasant bump to the framerate, from about 108Hz to 124Hz (1.15x)

The main source of speed-up is using integer math instead of floating point math for the calculation of the post-scaled pixel values. A slight secondary gain is achieved by avoiding the scaling altogether when the scale factor is 1.0.

Because the math is not exactly the same, some scaled pixel values may change by +- 1 RGBW "step". In practice, this is unlikely to matter.

The gains are bigger on the Pico and other M0 microcontrollers than M4 microcontrollers with floating point math in the hardware.

Happily, flash size is also improved a bit on the Pico build I did, going from

542552 bytes used, 506024 bytes free in flash firmware space out of 1048576 bytes (1024.0kB).

to

542376 bytes used, 506200 bytes free in flash firmware space out of 1048576 bytes (1024.0kB).

On the Pico, this increases the "fill rate" of
    pixels[:] = newvalues
considerably.  On a strip of 240 RGB LEDs, auto_write=False, the timings
are:

|| Brightness || Before || After || Improvement ||
|| 1.0        || 117 kpix/s || 307 kpix/s || 2.62x ||
|| 0.07       || 117 kpix/s || 273 kpix/s || 2.33x ||

It's worth noting that even the "before" rate is fast compared to the
time to transmit a single neopixel, but any time we can gain back
in the whole pipeline will let marginal animations work a little better.
To set all the pixels in this way and then show() gives a pleasant bump
to the framerate, from about 108Hz to 124Hz (1.15x)

The main source of speed-up is using integer math instead of floating
point math for the calculation of the post-scaled pixel values.  A slight
secondary gain is achieved by avoiding the scaling altogether when
the scale factor is 1.0.

Because the math is not exactly the same, some scaled pixel values may
change by +- 1 RGBW "step".  In practice, this is unlikely to matter.

The gains are bigger on the Pico and other M0 microcontrollers than M4
microcontrollers with floating point math in the hardware.

Happily, flash size is also improved a bit on the Pico build I did,
going from
> 542552 bytes used, 506024 bytes free in flash firmware space out of 1048576 bytes (1024.0kB).

to
> 542376 bytes used, 506200 bytes free in flash firmware space out of 1048576 bytes (1024.0kB).
@tannewt tannewt self-requested a review February 24, 2021 17:59
Copy link
Member

@tannewt tannewt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the speed up! Just a couple small things.

shared-module/_pixelbuf/PixelBuf.c Outdated Show resolved Hide resolved
shared-module/_pixelbuf/PixelBuf.c Outdated Show resolved Hide resolved
shared-module/_pixelbuf/PixelBuf.c Outdated Show resolved Hide resolved
shared-module/_pixelbuf/PixelBuf.c Outdated Show resolved Hide resolved
 * Comment on the reason for scaling by 256
 * Divide by 256 instead of shifting
 * fix a cast; eliminate an unneeded roundf() to get a few bytes code back
@jepler jepler requested a review from tannewt February 24, 2021 22:29
Copy link
Member

@tannewt tannewt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! Thank you!

@tannewt tannewt merged commit d4bf0d5 into adafruit:main Feb 25, 2021
@jepler jepler deleted the pixelbuf-brightness-performance branch November 3, 2021 21:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants