FastLED 3.9.15
Loading...
Searching...
No Matches
perlin_s8x8.cpp.hpp
Go to the documentation of this file.
1#pragma once
2// allow-include-after-namespace
3
4// 2D Perlin noise s8x8 implementation
5// Implementation file - included from perlin_s8x8.h
6
9
11
12namespace fl {
13
14void perlin_s8x8::init_fade_lut(fl::i32 *table) {
15 for (int i = 0; i <= 256; i++) {
16 fl::i16 t = static_cast<fl::i16>((i * HP_ONE) / 256);
17 fl::i16 t2 = static_cast<fl::i16>((static_cast<fl::i32>(t) * t) >> HP_BITS);
18 fl::i16 t3 = static_cast<fl::i16>((static_cast<fl::i32>(t2) * t) >> HP_BITS);
19 fl::i16 inner = static_cast<fl::i16>((static_cast<fl::i32>(t) * (6 * HP_ONE)) >> HP_BITS);
20 inner -= 15 * HP_ONE;
21 inner = static_cast<fl::i16>((static_cast<fl::i32>(t) * inner) >> HP_BITS);
22 inner += 10 * HP_ONE;
23 table[i] = static_cast<fl::i32>((static_cast<fl::i32>(t3) * inner) >> HP_BITS);
24 }
25}
26
28 const fl::i32 *fade_lut,
29 const fl::u8 *perm) {
31 pnoise2d_raw(fx.raw(), fy.raw(), fade_lut, perm));
32}
33
34fl::i32 perlin_s8x8::pnoise2d_raw(fl::i32 fx_raw, fl::i32 fy_raw,
35 const fl::i32 *fade_lut,
36 const fl::u8 *perm) {
37 int X, Y;
38 fl::i16 x, y;
39 floor_frac(fx_raw, X, x);
40 floor_frac(fy_raw, Y, y);
41 X &= 255;
42 Y &= 255;
43
44 fl::i16 u = fade(x, fade_lut);
45 fl::i16 v = fade(y, fade_lut);
46
47 int A = perm[X & 255] + Y;
48 int AA = perm[A & 255];
49 int AB = perm[(A + 1) & 255];
50 int B = perm[(X + 1) & 255] + Y;
51 int BA = perm[B & 255];
52 int BB = perm[(B + 1) & 255];
53
54 fl::i16 result = lerp(v,
55 lerp(u, grad(perm[AA & 255], x, y),
56 grad(perm[BA & 255], x - HP_ONE, y)),
57 lerp(u, grad(perm[AB & 255], x, y - HP_ONE),
58 grad(perm[BB & 255], x - HP_ONE, y - HP_ONE)));
59
60 // Shift from Q8 to s16x16's Q16 format
61 return static_cast<fl::i32>(static_cast<fl::u32>(static_cast<fl::i32>(result)) << (fl::s16x16::FRAC_BITS - HP_BITS));
62}
63
64FASTLED_FORCE_INLINE void perlin_s8x8::floor_frac(fl::i32 fp16, int &ifloor,
65 fl::i16 &frac8) {
66 ifloor = fp16 >> FP_BITS;
67 fl::i32 frac16 = fp16 & (FP_ONE - 1);
68 // Shift from 16 frac bits to 8 frac bits
69 frac8 = static_cast<fl::i16>(frac16 >> (FP_BITS - HP_BITS));
70}
71
72FASTLED_FORCE_INLINE fl::i16 perlin_s8x8::fade(fl::i16 t, const fl::i32 *table) {
73 // t is Q8 (0-255 range), use directly as index
74 fl::u8 idx = static_cast<fl::u8>(t);
75 // Return LUT value, convert from i32 to i16 (values fit in Q8 range)
76 return static_cast<fl::i16>(table[idx]);
77}
78
79FASTLED_FORCE_INLINE fl::i16 perlin_s8x8::lerp(fl::i16 t, fl::i16 a, fl::i16 b) {
80 // All values in Q8, result stays Q8
81 return static_cast<fl::i16>(
82 a + (((static_cast<fl::i32>(t) * (b - a)) >> HP_BITS)));
83}
84
85FASTLED_FORCE_INLINE fl::i16 perlin_s8x8::grad(int hash, fl::i16 x, fl::i16 y) {
86 struct GradCoeff { fl::i8 cx; fl::i8 cy; };
87 constexpr GradCoeff lut[16] = {
88 { 1, 1}, {-1, 1}, { 1, -1}, {-1, -1},
89 { 1, 0}, {-1, 0}, { 1, 0}, {-1, 0},
90 { 0, 1}, { 0, -1}, { 0, 1}, { 0, -1},
91 { 1, 1}, { 0, -1}, {-1, 1}, { 0, -1},
92 };
93 const GradCoeff &g = lut[hash & 15];
94 return static_cast<fl::i16>(g.cx * x + g.cy * y);
95}
96
97} // namespace fl
98
static constexpr int FRAC_BITS
Definition s16x16.h:22
static constexpr FASTLED_FORCE_INLINE s16x16 from_raw(i32 raw) FL_NOEXCEPT
Definition s16x16.h:54
constexpr i32 raw() const FL_NOEXCEPT
Definition s16x16.h:60
unsigned char u8
Definition s16x16x4.h:132
signed char i8
Definition s16x16x4.h:131
FL_DISABLE_WARNING_PUSH unsigned char * B
expected< T, E > result
Alias for expected (Rust-style naming)
Definition result.h:31
Base definition for an LED controller.
Definition crgb.hpp:179
#define FL_OPTIMIZATION_LEVEL_O3_BEGIN
#define FASTLED_FORCE_INLINE
#define FL_OPTIMIZATION_LEVEL_O3_END
static constexpr fl::i32 HP_ONE
Definition perlin_s8x8.h:16
static FASTLED_FORCE_INLINE fl::i16 grad(int hash, fl::i16 x, fl::i16 y)
static constexpr fl::i32 FP_ONE
Definition perlin_s8x8.h:34
static FASTLED_FORCE_INLINE void floor_frac(fl::i32 fp16, int &ifloor, fl::i16 &frac8)
static fl::i32 pnoise2d_raw(fl::i32 fx_raw, fl::i32 fy_raw, const fl::i32 *fade_lut, const fl::u8 *perm)
static FASTLED_FORCE_INLINE fl::i16 fade(fl::i16 t, const fl::i32 *table)
static constexpr int HP_BITS
Definition perlin_s8x8.h:15
static void init_fade_lut(fl::i32 *table)
static fl::s16x16 pnoise2d(fl::s16x16 fx, fl::s16x16 fy, const fl::i32 *fade_lut, const fl::u8 *perm)
static FASTLED_FORCE_INLINE fl::i16 lerp(fl::i16 t, fl::i16 a, fl::i16 b)
static constexpr int FP_BITS
Definition perlin_s8x8.h:33