From 7c6178418a203fce98353468df677b897e09a2e3 Mon Sep 17 00:00:00 2001 From: Frank <91616163+softhack007@users.noreply.github.com> Date: Sat, 19 Oct 2024 23:29:53 +0200 Subject: [PATCH] cache virtualLength() and some cleanup speedup for 1D->2D mapping --- wled00/FX.h | 10 +++++++++- wled00/FX_2Dfcn.cpp | 3 ++- wled00/FX_fcn.cpp | 24 ++++++++---------------- 3 files changed, 19 insertions(+), 18 deletions(-) diff --git a/wled00/FX.h b/wled00/FX.h index 997e30c1..77b8b7bc 100644 --- a/wled00/FX.h +++ b/wled00/FX.h @@ -428,6 +428,7 @@ typedef struct Segment { bool _firstFill = true; // dirty HACK support uint16_t _2dWidth = 0; // virtualWidth uint16_t _2dHeight = 0; // virtualHeight + uint16_t _virtuallength = 0; // virtualLength void setPixelColorXY_slow(int x, int y, uint32_t c); // set relative pixel within segment with color - full slow version #else @@ -619,7 +620,12 @@ typedef struct Segment { void setCurrentPalette(void); // 1D strip - uint16_t virtualLength(void) const; + uint16_t calc_virtualLength(void) const; +#ifndef WLEDMM_FASTPATH + inline uint16_t virtualLength(void) const {return calc_virtualLength();} +#else + inline uint16_t virtualLength(void) const {return _virtuallength;} +#endif void setPixelColor(int n, uint32_t c); // set relative pixel within segment with color inline void setPixelColor(int n, byte r, byte g, byte b, byte w = 0) { setPixelColor(n, RGBW32(r,g,b,w)); } // automatically inline inline void setPixelColor(int n, CRGB c) { setPixelColor(n, RGBW32(c.r,c.g,c.b,0)); } // automatically inline @@ -660,12 +666,14 @@ typedef struct Segment { if (mirror) vWidth = (vWidth + 1) /2; // divide by 2 if mirror, leave at least a single LED return vWidth; } + inline uint16_t calc_virtualWidth() const { return virtualWidth();} inline uint16_t virtualHeight() const { // WLEDMM use fast types, and make function inline uint_fast16_t groupLen = groupLength(); uint_fast16_t vHeight = ((transpose ? width() : height()) + groupLen - 1) / groupLen; if (mirror_y) vHeight = (vHeight + 1) /2; // divide by 2 if mirror, leave at least a single LED return vHeight; } + inline uint16_t calc_virtualHeight() const { return virtualHeight();} #else inline uint16_t virtualWidth() const { return(_2dWidth);} // WLEDMM get pre-calculated virtualWidth inline uint16_t virtualHeight() const { return(_2dHeight);} // WLEDMM get pre-calculated virtualHeight diff --git a/wled00/FX_2Dfcn.cpp b/wled00/FX_2Dfcn.cpp index 2259c549..850444b8 100644 --- a/wled00/FX_2Dfcn.cpp +++ b/wled00/FX_2Dfcn.cpp @@ -255,8 +255,9 @@ void Segment::startFrame(void) { _isValid2D = isActive() && is2D(); _brightness = currentBri(on ? opacity : 0); // if (reverse_y) _isSimpleSegment = false; // for A/B testing - _2dWidth = is2D() ? calc_virtualWidth() : virtualLength(); + _2dWidth = is2D() ? calc_virtualWidth() : calc_virtualLength(); _2dHeight = calc_virtualHeight(); + _virtuallength = calc_virtualLength(); #if 0 && defined(WLED_ENABLE_HUB75MATRIX) _firstFill = true; // dirty HACK #endif diff --git a/wled00/FX_fcn.cpp b/wled00/FX_fcn.cpp index bd979e62..c7ba1470 100644 --- a/wled00/FX_fcn.cpp +++ b/wled00/FX_fcn.cpp @@ -637,13 +637,13 @@ uint16_t Segment::nrOfVStrips() const { if (is2D()) { switch (map1D2D) { case M12_pBar: - vLen = virtualWidth(); + vLen = calc_virtualWidth(); break; case M12_sCircle: //WLEDMM - vLen = (virtualWidth() + virtualHeight()) / 6; // take third of the average width + vLen = (calc_virtualWidth() + calc_virtualHeight()) / 6; // take third of the average width break; case M12_sBlock: //WLEDMM - vLen = (virtualWidth() + virtualHeight()) / 8; // take half of the average width + vLen = (calc_virtualWidth() + calc_virtualHeight()) / 8; // take half of the average width break; } } @@ -682,11 +682,7 @@ class JMapC { if (size > 0) return size; else -#ifndef WLEDMM_FASTPATH - return SEGMENT.virtualWidth() * SEGMENT.virtualHeight(); //pixels -#else return SEGMENT.calc_virtualWidth() * SEGMENT.calc_virtualHeight(); // calc pixel sizes -#endif } void setPixelColor(uint16_t i, uint32_t col) { updatejMapDoc(); @@ -778,11 +774,7 @@ class JMapC { jMapFile.close(); maxWidth++; maxHeight++; -#ifndef WLEDMM_FASTPATH - scale = min(SEGMENT.virtualWidth() / maxWidth, SEGMENT.virtualHeight() / maxHeight); // WLEDMM use native min/max -#else scale = min(SEGMENT.calc_virtualWidth() / maxWidth, SEGMENT.calc_virtualHeight() / maxHeight); // WLEDMM re-calc width/heiht from active settings -#endif dataSize += sizeof(jVectorMap); USER_PRINT("dataSize "); USER_PRINT(dataSize); @@ -858,11 +850,11 @@ static int getPinwheelLength(int vW, int vH) { #endif // 1D strip -uint16_t Segment::virtualLength() const { +uint16_t Segment::calc_virtualLength() const { #ifndef WLED_DISABLE_2D if (is2D()) { - uint16_t vW = virtualWidth(); - uint16_t vH = virtualHeight(); + uint16_t vW = calc_virtualWidth(); + uint16_t vH = calc_virtualHeight(); uint16_t vLen = vW * vH; // use all pixels from segment switch (map1D2D) { case M12_pBar: @@ -1905,7 +1897,7 @@ void WS2812FX::service() { uint16_t frameDelay = FRAMETIME; // WLEDMM avoid name clash with "delay" function if (!seg.freeze) { //only run effect function if not frozen - _virtualSegmentLength = seg.virtualLength(); + _virtualSegmentLength = seg.calc_virtualLength(); _colors_t[0] = seg.currentColor(0, seg.colors[0]); _colors_t[1] = seg.currentColor(1, seg.colors[1]); _colors_t[2] = seg.currentColor(2, seg.colors[2]); @@ -2441,7 +2433,7 @@ uint8_t WS2812FX::setPixelSegment(uint8_t n) { uint8_t prevSegId = _segment_index; if (n < _segments.size()) { _segment_index = n; - _virtualSegmentLength = _segments[_segment_index].virtualLength(); + _virtualSegmentLength = _segments[_segment_index].calc_virtualLength(); } return prevSegId; }