| #include <stddef.h> |
| #include <stdio.h> |
| #include <stdlib.h> |
| #include <stdint.h> |
| |
| /* Set to 1 to enable debug log traces. */ |
| #define DEBUG 0 |
| |
| /* Set to 1 to optimize memory stores when generating plasma. */ |
| #define OPTIMIZE_WRITES 1 |
| |
| /* We're going to perform computations for every pixel of the target |
| * bitmap. floating-point operations are very slow on ARMv5, and not |
| * too bad on ARMv7 with the exception of trigonometric functions. |
| * |
| * For better performance on all platforms, we're going to use fixed-point |
| * arithmetic and all kinds of tricks |
| */ |
| |
| typedef int32_t Fixed; |
| |
| #define FIXED_BITS 16 |
| #define FIXED_ONE (1 << FIXED_BITS) |
| #define FIXED_AVERAGE(x,y) (((x) + (y)) >> 1) |
| |
| #define FIXED_FROM_INT(x) ((x) << FIXED_BITS) |
| #define FIXED_TO_INT(x) ((x) >> FIXED_BITS) |
| |
| #define FIXED_FROM_FLOAT(x) ((Fixed)((x)*FIXED_ONE)) |
| #define FIXED_TO_FLOAT(x) ((x)/(1.*FIXED_ONE)) |
| |
| #define FIXED_MUL(x,y) (((int64_t)(x) * (y)) >> FIXED_BITS) |
| #define FIXED_DIV(x,y) (((int64_t)(x) * FIXED_ONE) / (y)) |
| |
| #define FIXED_DIV2(x) ((x) >> 1) |
| #define FIXED_AVERAGE(x,y) (((x) + (y)) >> 1) |
| |
| #define FIXED_FRAC(x) ((x) & ((1 << FIXED_BITS)-1)) |
| #define FIXED_TRUNC(x) ((x) & ~((1 << FIXED_BITS)-1)) |
| |
| #define FIXED_FROM_INT_FLOAT(x,f) (Fixed)((x)*(FIXED_ONE*(f))) |
| |
| typedef int32_t Angle; |
| |
| #define ANGLE_BITS 9 |
| |
| #if ANGLE_BITS < 8 |
| # error ANGLE_BITS must be at least 8 |
| #endif |
| |
| #define ANGLE_2PI (1 << ANGLE_BITS) |
| #define ANGLE_PI (1 << (ANGLE_BITS-1)) |
| #define ANGLE_PI2 (1 << (ANGLE_BITS-2)) |
| #define ANGLE_PI4 (1 << (ANGLE_BITS-3)) |
| |
| #define ANGLE_FROM_FLOAT(x) (Angle)((x)*ANGLE_PI/M_PI) |
| #define ANGLE_TO_FLOAT(x) ((x)*M_PI/ANGLE_PI) |
| |
| #if ANGLE_BITS <= FIXED_BITS |
| # define ANGLE_FROM_FIXED(x) (Angle)((x) >> (FIXED_BITS - ANGLE_BITS)) |
| # define ANGLE_TO_FIXED(x) (Fixed)((x) << (FIXED_BITS - ANGLE_BITS)) |
| #else |
| # define ANGLE_FROM_FIXED(x) (Angle)((x) << (ANGLE_BITS - FIXED_BITS)) |
| # define ANGLE_TO_FIXED(x) (Fixed)((x) >> (ANGLE_BITS - FIXED_BITS)) |
| #endif |
| |
| static Fixed *angle_sin_tab; |
| //static Fixed angle_sin_tab[ANGLE_2PI+1]; |
| |
| static __inline__ Fixed angle_sin( Angle a ) |
| { |
| return angle_sin_tab[(uint32_t)a & (ANGLE_2PI-1)]; |
| } |
| |
| static __inline__ Fixed angle_cos( Angle a ) |
| { |
| return angle_sin(a + ANGLE_PI2); |
| } |
| |
| static __inline__ Fixed fixed_sin( Fixed f ) |
| { |
| return angle_sin(ANGLE_FROM_FIXED(f)); |
| } |
| |
| static __inline__ Fixed fixed_cos( Fixed f ) |
| { |
| return angle_cos(ANGLE_FROM_FIXED(f)); |
| } |
| |
| /* Color palette used for rendering the plasma */ |
| #define PALETTE_BITS 8 |
| #define PALETTE_SIZE (1 << PALETTE_BITS) |
| |
| #if PALETTE_BITS > FIXED_BITS |
| # error PALETTE_BITS must be smaller than FIXED_BITS |
| #endif |
| |
| #if 0 |
| static uint16_t make565(int red, int green, int blue) |
| { |
| return (uint16_t)( ((red << 8) & 0xf800) | |
| ((green << 2) & 0x03e0) | |
| ((blue >> 3) & 0x001f) ); |
| } |
| |
| static void init_palette(uint16_t *palette) |
| { |
| int nn, mm = 0; |
| /* fun with colors */ |
| for (nn = 0; nn < PALETTE_SIZE/4; nn++) { |
| int jj = (nn-mm)*4*255/PALETTE_SIZE; |
| palette[nn] = make565(255, jj, 255-jj); |
| } |
| |
| for ( mm = nn; nn < PALETTE_SIZE/2; nn++ ) { |
| int jj = (nn-mm)*4*255/PALETTE_SIZE; |
| palette[nn] = make565(255-jj, 255, jj); |
| } |
| |
| for ( mm = nn; nn < PALETTE_SIZE*3/4; nn++ ) { |
| int jj = (nn-mm)*4*255/PALETTE_SIZE; |
| palette[nn] = make565(0, 255-jj, 255); |
| } |
| |
| for ( mm = nn; nn < PALETTE_SIZE; nn++ ) { |
| int jj = (nn-mm)*4*255/PALETTE_SIZE; |
| palette[nn] = make565(jj, 0, 255); |
| } |
| } |
| #endif |
| static __inline__ uint16_t palette_from_fixed(uint16_t* palette, Fixed x ) |
| { |
| if (x < 0) x = -x; |
| if (x >= FIXED_ONE) x = FIXED_ONE-1; |
| int idx = FIXED_FRAC(x) >> (FIXED_BITS - PALETTE_BITS); |
| return palette[idx & (PALETTE_SIZE-1)]; |
| } |
| |
| |
| extern void root(uint32_t width, uint32_t height, uint32_t stride, double t, uint16_t* palette, void* pixels, void *_angle_sin_tab) |
| { |
| angle_sin_tab = _angle_sin_tab; |
| Fixed ft = FIXED_FROM_FLOAT(t/1000.); |
| Fixed yt1 = FIXED_FROM_FLOAT(t/1230.); |
| Fixed yt2 = yt1; |
| Fixed xt10 = FIXED_FROM_FLOAT(t/3000.); |
| Fixed xt20 = xt10; |
| |
| #define YT1_INCR FIXED_FROM_FLOAT(1/100.) |
| #define YT2_INCR FIXED_FROM_FLOAT(1/163.) |
| |
| int yy; |
| for (yy = 0; yy < height; yy++) { |
| uint16_t* line = (uint16_t*)pixels; |
| Fixed base = fixed_sin(yt1) + fixed_sin(yt2); |
| Fixed xt1 = xt10; |
| Fixed xt2 = xt20; |
| |
| yt1 += YT1_INCR; |
| yt2 += YT2_INCR; |
| |
| #define XT1_INCR FIXED_FROM_FLOAT(1/173.) |
| #define XT2_INCR FIXED_FROM_FLOAT(1/242.) |
| |
| uint16_t* line_end = line + width; |
| |
| if (line < line_end) { |
| if (((uint32_t)line & 3) != 0) { |
| Fixed ii = base + fixed_sin(xt1) + fixed_sin(xt2); |
| |
| xt1 += XT1_INCR; |
| xt2 += XT2_INCR; |
| |
| line[0] = palette_from_fixed(palette, ii >> 2); |
| line++; |
| } |
| |
| while (line + 2 <= line_end) { |
| Fixed i1 = base + fixed_sin(xt1) + fixed_sin(xt2); |
| xt1 += XT1_INCR; |
| xt2 += XT2_INCR; |
| |
| Fixed i2 = base + fixed_sin(xt1) + fixed_sin(xt2); |
| xt1 += XT1_INCR; |
| xt2 += XT2_INCR; |
| |
| uint32_t pixel = ((uint32_t)palette_from_fixed(palette, i1 >> 2) << 16) | |
| (uint32_t)palette_from_fixed(palette, i2 >> 2); |
| |
| ((uint32_t*)line)[0] = pixel; |
| line += 2; |
| } |
| |
| if (line < line_end) { |
| Fixed ii = base + fixed_sin(xt1) + fixed_sin(xt2); |
| line[0] = palette_from_fixed(palette, ii >> 2); |
| line++; |
| } |
| } |
| |
| // go to next line |
| pixels = (char*)pixels + stride; |
| } |
| } |
| |
| /* simple stats management */ |
| typedef struct { |
| double renderTime; |
| double frameTime; |
| } FrameStats; |
| |
| #define MAX_FRAME_STATS 200 |
| #define MAX_PERIOD_MS 1500 |
| |
| typedef struct { |
| double firstTime; |
| double lastTime; |
| double frameTime; |
| |
| int firstFrame; |
| int numFrames; |
| FrameStats frames[ MAX_FRAME_STATS ]; |
| } Stats; |
| |