Repository navigation
fix #1658: increase fixed-point maths accuracy in gradient_fill - #1659
sutaburosu wants to merge 1 commit into
Conversation
|
So this code looks good, though I haven't gone through with a mathematical proof. For this code to go in, you'd need to write a small test program and prove that the color values for at least a small group of pixels (~32) produces the same output using the new fill technique vs the old. Is it too much to ask that you compare both functions over a full rainbow rotation? You could use this online CPP compiler: https://www.online-cpp.com/ If it looks good then I'll approve a CL, but with the following changes:
|
|
Okay, I created an isolated test. At led count = 16, it's exactly the same, at led count = 32 there is one mismatch. At higher amounts there are more mismatches. It's off by one so I think this algorithm is working as intended: |
I couldn't figure out a way to quickly get enough of FastLED into online-cpp.com to test this effectively. Instead I relied on good old Wokwi. Here is the test sketch which prints differences in the first 32 LEDs. It also shows the RGB value that the last pixel should have reached if any differences are detected. You can pause execution at any time to study the results. There are often differences in a few of the first 32 LEDs. There are sometimes differences in all the LEDs. In both cases the original code never reached the desired CHSV value at the last pixel, whereas my fixed version did. There are often cases where the original code fills the whole buffer with a single RGB value because of the limited resolution of the deltas.
I would argue that this is unnecessary for two reasons:
edited to add: thanks for taking the time to create a test case yourself. |
|
Further to my last comment, I have seen cases where my version also fails to get to the desired colour when the range of the gradients of hues/saturations/values is small. It always does better than the original code though, so there's that. I doubt it's worth going to 64-bit 8.56 fixed-point to correct for this, as it only affects sketches with thousands of LEDs which is not a very common use-case for FastLED. |
|
Thanks for the tests. I've gone ahead and merged this cl: |
|
Thanks for your interest in improving FastLED. I very much appreciate it, and I know many silent users are also benefitting from you spending time with this project. Seeing as you are actually active in committing PRs, I would love it if you could take a look at #202. It's very stale now and will need some work to get it into a state where it could be merged. I am willing to do that work. This is my test case from years ago. I'm sure you'll quickly see which half of the matrix is running the original code vs the proposed code. |
I rescind this; I've just noticed that I don't even get to benefit from this bug-fix on AVR after your changes, so this is yet another change that I have to maintain in my own (private) fork of FastLED to get performant and correct code. You could have pushed back against my arguments, but instead you silently took the credit for fixing it. It's good for raising your velocity though. Enjoy. The comment you added is wrong. It uses s8.7 and u8.8 maths. That's 16-bits, not 8. Apologies for not catching the compiler warning. I have just set up a new environment and it seems I neglected to turn on warnings. Ignoring the warnings is poor form. The correct way to fix this would be to cast to the larger type before shifting: uint32_t hue824 = (uint32_t) startcolor.hue << 24;
uint32_t sat824 = (uint32_t) startcolor.sat << 24;
uint32_t val824 = (uint32_t) startcolor.val << 24;A benchmark on AVR averaging the duration of 100 calls to fill_gradient() filling 2,400 LEDs showed a performance penalty of 6-9%. |
|
I'm sorry you didn't get credit. It was an oversight on my part. Going forward I'm using AI generated release notes that will fix this. The only reason I re-did the PR is Will you accept my apology? |
|
Also github is atrocious for giving me the right notifications so also apologies for not seeing this until this morning. |
@zackees Accepted. I'm pleased that you are driving FastLED forwards after years of stagnation. I still feel that the bug should be fixed on AVR too, but hey it's just a |
|
@zackees oh, I forgot to mention that ignoring the compiler warnings gives wildly incorrect behaviour on AVR (and presumably other platforms with registers less than 32-bits wide). All generated colours are black because the HSV values are always 0. The fix I suggested previously gives correct behaviour on all platforms.
uint32_t hue824 = (uint32_t) startcolor.hue << 24;
uint32_t sat824 = (uint32_t) startcolor.sat << 24;
uint32_t val824 = (uint32_t) startcolor.val << 24; |
|
Yes I went ahead and applied the fix with a static cast before the bit shift. It should already be committed. I'm not sure if you looked or not. |
|
Ah, yes. So you did. I was only looking at the changes in your first pull request. Sorry for the noise. |
Should fix #1658.