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