Skip to content

Commit

Permalink
wu_pixel small optimization
Browse files Browse the repository at this point in the history
5% faster
  • Loading branch information
softhack007 committed Sep 29, 2024
1 parent 10d8cfd commit d3c401e
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions wled00/FX_2Dfcn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -704,11 +704,14 @@ void Segment::wu_pixel(uint32_t x, uint32_t y, CRGB c) { //awesome wu_pixel
WU_WEIGHT(ix, yy), WU_WEIGHT(xx, yy)};
// multiply the intensities by the colour, and saturating-add them to the pixels
for (int i = 0; i < 4; i++) {
CRGB led = getPixelColorXY((x >> 8) + (i & 1), (y >> 8) + ((i >> 1) & 1));
int wu_x = (x >> 8) + (i & 1); // precalculate x
int wu_y = (y >> 8) + ((i >> 1) & 1); // precalculate y
CRGB led = getPixelColorXY(wu_x, wu_y);
CRGB oldLed = led;
led.r = qadd8(led.r, c.r * wu[i] >> 8);
led.g = qadd8(led.g, c.g * wu[i] >> 8);
led.b = qadd8(led.b, c.b * wu[i] >> 8);
setPixelColorXY(int((x >> 8) + (i & 1)), int((y >> 8) + ((i >> 1) & 1)), led);
if (led != oldLed) setPixelColorXY(wu_x, wu_y, led); // don't repaint if same color

This comment has been minimized.

Copy link
@DedeHai

DedeHai Sep 30, 2024

Collaborator

precalculation is done even if this condition is false. What is the point of the precalculation? the compiler will probably drop those.
edit:
disregard that, I just now realised the value is used twice...

}
}
#undef WU_WEIGHT
Expand Down

5 comments on commit d3c401e

@softhack007
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@blazoncek, in principle, wu_pixel could be modified to use uint32_t color instead of CRGB. Basically we would simply replicate line 713 for the W channel. 🤔 But not sure if that's worth it ... what do you think?

@softhack007
Copy link
Collaborator Author

@softhack007 softhack007 commented on d3c401e Sep 29, 2024

Choose a reason for hiding this comment

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

@DedeHai I've just "rediscovered" this old function, which performs setPixelColor with 256 subpixels very accurately and fast.
I was wondering if that's useful for your particle effect PR #3823 ?

@blazoncek
Copy link
Collaborator

Choose a reason for hiding this comment

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

wu_pixel could be modified to use uint32_t color instead of CRGB

Of course. I just ported it from FastLED examples with as little effort I managed.

@DedeHai
Copy link
Collaborator

@DedeHai DedeHai commented on d3c401e Sep 30, 2024

Choose a reason for hiding this comment

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

I did look at the 'wu algorithm' a while back, need to check my notes for details but IIRC I found it to be ill-suited for the particle system (but it does almost the same thing). With knowledge gained from all the optimization work I did recently I may revisit the topic though.
Regarding white channel: my take on this is still that 2D setups very rarely (at least currently) use RGBW pixels, palettes do not use any white channel either. I still do not fully understand, where the white channel is even used in rendering (before white channel calculation, which is done at bus level if I am not mistaken).
I would be in favour a solution that drops the white channel completely and use an alpha channel instead.
EDIT:
if you want to add white channel support: this would be a good opportunity to use the CRGBW struct I added in the speed improvements PR.

@blazoncek
Copy link
Collaborator

Choose a reason for hiding this comment

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

White channel is only used in one effect if memory serves me well. Not sure in which.
I am in favour of ditching W or replace it with alpha.

If anyone wants to reduce power usage by utilising W channel he/she can do with auto-white calculation.

Please sign in to comment.