no-thread option, coalesced tunnel lut, more accurate fps counter
[fbgfx] / src / tunnel.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <math.h>
4 #include <assert.h>
5 #include <imago2.h>
6 #ifndef NO_THREADS
7 #include "tpool.h"
8 #endif
9 #include "demo.h"
10 #include "screen.h"
11
12 #define VSCALE  1.5
13
14 #define TEX_FNAME       "data/grid.png"
15 #define TEX_USCALE      4
16 #define TEX_VSCALE      2
17
18 #define NUM_WORK_ITEMS  8
19
20 static struct work {
21         void *pixels;
22         int starty, num_lines;
23         long tm;
24         int xoffs, yoffs;
25 } work[NUM_WORK_ITEMS];
26
27 static int init(void);
28 static void destroy(void);
29 static void start(long trans_time);
30 static void stop(long trans_time);
31 static void draw(void);
32
33 static void (*draw_tunnel_range)(void*, int, int, int, int, long);
34
35 static void draw_tunnel_range16(void *pixels, int xoffs, int yoffs, int starty, int num_lines, long tm);
36 static void draw_tunnel_range32(void *pixels, int xoffs, int yoffs, int starty, int num_lines, long tm);
37 static int count_bits(unsigned int x);
38 static int count_zeros(unsigned int x);
39
40 static struct screen scr = {
41         "tunnel",
42         init,
43         destroy,
44         start,
45         stop,
46         draw
47 };
48
49 struct tunmap {
50         int u, v;
51         unsigned char fog;
52 };
53
54 static int xsz, ysz, vxsz, vysz;
55 static int pan_width, pan_height;
56 static struct tunmap *tunnel_map;
57
58 static int tex_xsz, tex_ysz;
59 static unsigned int *tex_pixels;
60 static int tex_xshift, tex_yshift;
61 static unsigned int tex_xmask, tex_ymask;
62
63 #ifndef NO_THREADS
64 static struct thread_pool *tpool;
65 #endif
66
67 static long trans_start, trans_dur;
68 static int trans_dir;
69
70
71 struct screen *tunnel_screen(void)
72 {
73         return &scr;
74 }
75
76
77 static int init(void)
78 {
79         int i, j, n;
80         struct tunmap *tmap;
81         float aspect = (float)fb_width / (float)fb_height;
82
83         switch(fb_depth) {
84         case 16:
85                 draw_tunnel_range = draw_tunnel_range16;
86                 break;
87         case 32:
88                 draw_tunnel_range = draw_tunnel_range32;
89                 break;
90         default:
91                 fprintf(stderr, "unsupported color depth: %d\n", fb_depth);
92                 return -1;
93         }
94
95         xsz = fb_width / 2;
96         ysz = fb_height;
97         vxsz = xsz * VSCALE;
98         vysz = ysz * VSCALE;
99
100         pan_width = vxsz - xsz;
101         pan_height = vysz - ysz;
102
103         if(!(tunnel_map = malloc(vxsz * vysz * sizeof *tunnel_map))) {
104                 fprintf(stderr, "failed to allocate tunnel map\n");
105                 return -1;
106         }
107
108         tmap = tunnel_map;
109
110         for(i=0; i<vysz; i++) {
111                 float y = 2.0 * (float)i / (float)vysz - 1.0;
112                 for(j=0; j<vxsz; j++) {
113                         float x = aspect * (2.0 * (float)j / (float)vxsz - 1.0);
114                         float tu = atan2(y, x) / M_PI * 0.5 + 0.5;
115                         float d = sqrt(x * x + y * y);
116                         float tv = d == 0.0 ? 0.0 : 1.0 / d;
117
118                         int fog = (int)(d * 128.0f);
119
120                         tmap->u = (int)(tu * 65535.0 * TEX_USCALE) & 0xffff;
121                         tmap->v = (int)(tv * 65535.0 * TEX_VSCALE) & 0xffff;
122                         tmap->fog = fog > 255 ? 255 : fog;
123                         tmap++;
124                 }
125         }
126
127         if(!(tex_pixels = img_load_pixels(TEX_FNAME, &tex_xsz, &tex_ysz, IMG_FMT_RGBA32))) {
128                 fprintf(stderr, "failed to load image " TEX_FNAME "\n");
129                 return -1;
130         }
131         if((count_bits(tex_xsz) | count_bits(tex_ysz)) != 1) {
132                 fprintf(stderr, "non-pow2 image (%dx%d)\n", tex_xsz, tex_ysz);
133                 return -1;
134         }
135
136         n = count_zeros(tex_xsz);
137         for(i=0; i<n; i++) {
138                 tex_xmask |= 1 << i;
139         }
140         tex_xshift = n;
141
142         n = count_zeros(tex_ysz);
143         for(i=0; i<n; i++) {
144                 tex_ymask |= 1 << i;
145         }
146         tex_yshift = n;
147
148 #ifndef NO_THREADS
149         if(!(tpool = tpool_create(0))) {
150                 fprintf(stderr, "failed to create thread pool\n");
151                 return -1;
152         }
153 #endif
154
155         /* initialize the constant part of all work items */
156         for(i=0; i<NUM_WORK_ITEMS; i++) {
157                 int num_lines = ysz / NUM_WORK_ITEMS;
158                 work[i].pixels = fb_pixels;
159                 work[i].starty = i * num_lines;
160         }
161
162         return 0;
163 }
164
165 static void destroy(void)
166 {
167 #ifndef NO_THREADS
168         tpool_destroy(tpool);
169 #endif
170         free(tunnel_map);
171 }
172
173 static void start(long trans_time)
174 {
175         if(trans_time) {
176                 trans_start = time_msec;
177                 trans_dur = trans_time;
178                 trans_dir = 1;
179         }
180 }
181
182 static void stop(long trans_time)
183 {
184         if(trans_time) {
185                 trans_start = time_msec;
186                 trans_dur = trans_time;
187                 trans_dir = -1;
188         }
189 }
190
191
192 static void work_func(void *cls)
193 {
194         struct work *w = (struct work*)cls;
195         draw_tunnel_range(w->pixels, w->xoffs, w->yoffs, w->starty, w->num_lines, w->tm);
196 }
197
198 static void draw(void)
199 {
200         int i, num_lines = ysz / NUM_WORK_ITEMS;
201         int draw_lines = num_lines;
202         float t;
203         int xoffs, yoffs;
204
205         if(trans_dir) {
206                 long interval = time_msec - trans_start;
207                 int progr = num_lines * interval / trans_dur;
208                 if(trans_dir < 0) {
209                         draw_lines = num_lines - progr - 1;
210                 } else {
211                         draw_lines = progr;
212                 }
213                 if(progr >= num_lines) {
214                         trans_dir = 0;
215                 }
216         }
217
218         t = time_msec / 10000.0;
219         xoffs = (int)(cos(t * 3.0) * pan_width / 2) + pan_width / 2;
220         yoffs = (int)(sin(t * 4.0) * pan_height / 2) + pan_height / 2;
221
222         for(i=0; i<NUM_WORK_ITEMS; i++) {
223                 work[i].num_lines = draw_lines;
224                 work[i].tm = time_msec;
225                 work[i].xoffs = xoffs;
226                 work[i].yoffs = yoffs;
227
228 #ifdef NO_THREADS
229                 work_func(work + i);
230         }
231 #else
232                 tpool_enqueue(tpool, work + i, work_func, 0);
233         }
234         tpool_wait(tpool);
235 #endif
236 }
237
238 static void tunnel_color(int *rp, int *gp, int *bp, long toffs, struct tunmap *tmap)
239 {
240         int r, g, b;
241         unsigned int col;
242         unsigned int tx = (tmap->u << tex_xshift) >> 16;
243         unsigned int ty = (tmap->v << tex_yshift) >> 16;
244         tx += toffs;
245         ty += toffs << 1;
246
247         tx &= tex_xmask;
248         ty &= tex_ymask;
249
250         col = tex_pixels[(ty << tex_xshift) + tx];
251         r = col & 0xff;
252         g = (col >> 8) & 0xff;
253         b = (col >> 16) & 0xff;
254
255         *rp = (r * tmap->fog) >> 8;
256         *gp = (g * tmap->fog) >> 8;
257         *bp = (b * tmap->fog) >> 8;
258 }
259
260 #define PACK_RGB16(r, g, b) \
261         (((((r) >> 3) & 0x1f) << 11) | ((((g) >> 2) & 0x3f) << 5) | (((b) >> 3) & 0x1f))
262 #define PACK_RGB32(r, g, b) \
263         ((((r) & 0xff) << 16) | (((g) & 0xff) << 8) | ((b) & 0xff))
264
265 static void draw_tunnel_range16(void *pix, int xoffs, int yoffs, int starty, int num_lines, long tm)
266 {
267         int i, j;
268         struct tunmap *tmap = tunnel_map + (starty + yoffs) * vxsz + xoffs;
269
270         long toffs = tm / 4;
271         unsigned int *pixels = (unsigned int*)pix + starty * (fb_width >> 1);
272
273         for(i=0; i<num_lines; i++) {
274                 for(j=0; j<xsz; j++) {
275                         unsigned int col;
276                         int r, g, b;
277
278                         tunnel_color(&r, &g, &b, toffs, tmap + j);
279                         col = PACK_RGB16(r, g, b);
280                         *pixels++ = (col << 16) | col;
281                 }
282                 tmap += vxsz;
283         }
284 }
285
286 static void draw_tunnel_range32(void *pix, int xoffs, int yoffs, int starty, int num_lines, long tm)
287 {
288         int i, j;
289         struct tunmap *tmap = tunnel_map + (starty + yoffs) * vxsz + xoffs;
290
291         long toffs = tm / 4;
292         unsigned int *pixels = (unsigned int*)pix + starty * fb_width;
293
294         for(i=0; i<num_lines; i++) {
295                 for(j=0; j<xsz; j++) {
296                         unsigned int col;
297                         int r, g, b;
298
299                         tunnel_color(&r, &g, &b, toffs, tmap + j);
300                         col = PACK_RGB32(r, g, b);
301
302                         *pixels++ = col;
303                         *pixels++ = col;
304                 }
305                 tmap += vxsz;
306         }
307 }
308
309 static int count_bits(unsigned int x)
310 {
311         int i, nbits = 0;
312         for(i=0; i<32; i++) {
313                 if(x & 1) ++nbits;
314                 x >>= 1;
315         }
316         return nbits;
317 }
318
319 static int count_zeros(unsigned int x)
320 {
321         int i, num = 0;
322         for(i=0; i<32; i++) {
323                 if(x & 1) break;
324                 ++num;
325                 x >>= 1;
326         }
327         return num;
328 }