Skip to content

Commit

Permalink
Palette performance fix Aircoookie#3978 backport to 0.14
Browse files Browse the repository at this point in the history
  • Loading branch information
jw2013 committed May 15, 2024
1 parent ecc9443 commit f2a25e8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
5 changes: 4 additions & 1 deletion wled00/FX.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
//#define SEGLEN strip._segments[strip.getCurrSegmentId()].virtualLength()
#define SEGCOLOR(x) strip.segColor(x) /* saves us a few kbytes of code */
#define SEGPALETTE strip._currentPalette
#define SEGPALETTEIDX strip._currentPaletteIndex
#define SEGLEN strip._virtualSegmentLength /* saves us a few kbytes of code */
#define SPEED_FORMULA_L (5U + (50U*(255U - SEGMENT.speed))/SEGLEN)

Expand Down Expand Up @@ -567,7 +568,7 @@ typedef struct Segment {
uint8_t currentMode(void);
uint32_t currentColor(uint8_t slot);
CRGBPalette16 &loadPalette(CRGBPalette16 &tgt, uint8_t pal);
CRGBPalette16 &currentPalette(CRGBPalette16 &tgt, uint8_t paletteID);
inline CRGBPalette16 &currentPalette(uint8_t paletteID);

// 1D strip
uint16_t virtualLength(void) const;
Expand Down Expand Up @@ -694,6 +695,7 @@ class WS2812FX { // 96 bytes
#endif
// semi-private (just obscured) used in effect functions through macros
_currentPalette(CRGBPalette16(CRGB::Black)),
_currentPaletteIndex(255),
_colors_t{0,0,0},
_virtualSegmentLength(0),
// true private variables
Expand Down Expand Up @@ -889,6 +891,7 @@ class WS2812FX { // 96 bytes

void loadCustomPalettes(void); // loads custom palettes from JSON
CRGBPalette16 _currentPalette; // palette used for current effect (includes transition)
uint8_t _currentPaletteIndex;
std::vector<CRGBPalette16> customPalettes; // TODO: move custom palettes out of WS2812FX class

// using public variables to reduce code size increase due to inline function getSegment() (with bounds checking)
Expand Down
21 changes: 10 additions & 11 deletions wled00/FX_fcn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -448,18 +448,21 @@ uint32_t Segment::currentColor(uint8_t slot) {
#endif
}

CRGBPalette16 &Segment::currentPalette(CRGBPalette16 &targetPalette, uint8_t pal) {
loadPalette(targetPalette, pal);
inline CRGBPalette16 &Segment::currentPalette(uint8_t pal) {
if ( SEGPALETTEIDX != pal ) {
loadPalette(SEGPALETTE, pal);
SEGPALETTEIDX = pal;
}
uint16_t prog = progress();
if (strip.paletteFade && prog < 0xFFFFU) {
// blend palettes
// there are about 255 blend passes of 48 "blends" to completely blend two palettes (in _dur time)
// minimum blend time is 100ms maximum is 65535ms
uint16_t noOfBlends = ((255U * prog) / 0xFFFFU) - _t->_prevPaletteBlends;
for (int i=0; i<noOfBlends; i++, _t->_prevPaletteBlends++) nblendPaletteTowardPalette(_t->_palT, targetPalette, 48);
targetPalette = _t->_palT; // copy transitioning/temporary palette
for (int i=0; i<noOfBlends; i++, _t->_prevPaletteBlends++) nblendPaletteTowardPalette(_t->_palT, SEGPALETTE, 48);
return _t->_palT;
}
return targetPalette;
return SEGPALETTE;
}

// relies on WS2812FX::service() to call it max every 8ms or more (MIN_SHOW_DELAY)
Expand Down Expand Up @@ -1078,11 +1081,7 @@ uint32_t Segment::color_from_palette(uint16_t i, bool mapping, bool wrap, uint8_
uint8_t paletteIndex = i;
if (mapping && virtualLength() > 1) paletteIndex = (i*255)/(virtualLength() -1);
if (!wrap && strip.paletteBlend != 3) paletteIndex = scale8(paletteIndex, 240); //cut off blend at palette "end"
CRGBPalette16 curPal;
curPal = currentPalette(curPal, palette);
//if (isInTransition()) curPal = _t->_palT;
//else loadPalette(curPal, palette);
CRGB fastled_col = ColorFromPalette(curPal, paletteIndex, pbri, (strip.paletteBlend == 3)? NOBLEND:LINEARBLEND); // NOTE: paletteBlend should be global
CRGB fastled_col = ColorFromPalette(currentPalette(palette), paletteIndex, pbri, (strip.paletteBlend == 3)? NOBLEND:LINEARBLEND); // NOTE: paletteBlend should be global

return RGBW32(fastled_col.r, fastled_col.g, fastled_col.b, 0);
}
Expand Down Expand Up @@ -1187,7 +1186,7 @@ void WS2812FX::service() {
_colors_t[0] = seg.currentColor(0);
_colors_t[1] = seg.currentColor(1);
_colors_t[2] = seg.currentColor(2);
seg.currentPalette(_currentPalette, seg.palette); // we need to pass reference
seg.currentPalette(seg.palette); // we need to pass reference

if (!cctFromRgb || correctWB) busses.setSegmentCCT(seg.currentBri(true), correctWB);
for (int c = 0; c < NUM_COLORS; c++) _colors_t[c] = gamma32(_colors_t[c]);
Expand Down

0 comments on commit f2a25e8

Please sign in to comment.