FastLED 3.9.15
Loading...
Searching...
No Matches
fp_state.h
Go to the documentation of this file.
1#pragma once
2
3// Generic fixed-point state for Animartrix2 visualizers.
4// Holds SoA (Structure-of-Arrays) per-pixel geometry cache and Perlin fade LUT.
5// Each FP visualizer owns an FPVizState instance as a private member.
6
7#include "fl/stl/align.h"
11#include "fl/stl/stdint.h"
12#include "fl/stl/vector.h"
13
14namespace fl {
15
16// FL_ALIGNAS(16): aligns the struct to 16 bytes for potential SIMD loads.
17struct FL_ALIGNAS(16) FPVizState {
18 // SoA pixel geometry — built once when grid size changes, reused every frame.
19 // Padded to next multiple of 4 for SIMD safety.
20 fl::vector<fl::i32> polar_theta_raw; // atan2(dy, dx), raw s16x16
21 fl::vector<fl::i32> distance_raw; // hypot(dx, dy), raw s16x16
22 fl::vector<fl::i32> sqrt_distance_raw; // sqrt(distance), raw s16x16
23 fl::vector<fl::u16> pixel_idx; // pre-mapped xyMap(x,y) LED index
24 int count = 0;
25
26 // Perlin fade LUT (257 entries, Q8.24 format).
27 FL_ALIGNAS(16) fl::i32 fade_lut[257];
28 bool fade_lut_initialized = false;
29
30 FPVizState() : fade_lut{}, fade_lut_initialized(false) {}
31
32 // Rebuild per-pixel cache when grid changes.
33 // Called once per frame; rebuilds only if pixel count changed.
34 void ensureCache(Engine *e) {
35 const int num_x = e->num_x;
36 const int num_y = e->num_y;
37 const int total_pixels = num_x * num_y;
38
39 if (count != total_pixels) {
40 const int padded = (total_pixels + 3) & ~3;
41 polar_theta_raw.resize(padded, 0);
42 distance_raw.resize(padded, 0);
43 sqrt_distance_raw.resize(padded, 0);
44 pixel_idx.resize(padded, 0);
45
46 int idx = 0;
47 for (int x = 0; x < num_x; x++) {
48 for (int y = 0; y < num_y; y++) {
49 polar_theta_raw[idx] = fl::s16x16(e->polar_theta[x][y]).raw();
50 distance_raw[idx] = fl::s16x16(e->distance[x][y]).raw();
51 sqrt_distance_raw[idx] = fl::s16x16(e->distance[x][y]).sqrt().raw();
52 pixel_idx[idx] = e->mCtx->xyMapFn(x, y, e->mCtx->xyMapUserData);
53 idx++;
54 }
55 }
56 count = total_pixels;
57 }
58
59 if (!fade_lut_initialized) {
61 fade_lut_initialized = true;
62 }
63 }
64};
65
66} // namespace fl
Alignment macros and utilities for FastLED.
void resize(fl::size n) FL_NOEXCEPT
Definition vector.h:593
struct FL_ALIGNAS(4) Wave3BitExpansionLut
Lookup table for nibble-to-waveform expansion in wave3 format (32 bytes)
Definition wave3.h:31
Base definition for an LED controller.
Definition crgb.hpp:179
#define FL_ALIGNAS(N)
static void init_fade_lut(fl::i32 *table)