optimization: eliminate projection division from column loop
[gbajam22] / src / voxscape.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <stdint.h>
5 #include <math.h>
6 #include <assert.h>
7 #include "voxscape.h"
8 #include "debug.h"
9
10 /* hardcoded dimensions for the GBA */
11 #define FBWIDTH         240
12 #define FBHEIGHT        160
13 #define FBPITCH         240
14 /* map size */
15 #define XSZ                     512
16 #define YSZ                     512
17 #define XSHIFT          9
18 #define XMASK           0x1ff
19 #define YMASK           0x1ff
20 #define HSCALE          40
21
22
23 #define NO_LERP
24
25 #define XLERP(a, b, t, fp) \
26         ((((a) << (fp)) + ((b) - (a)) * (t)) >> fp)
27
28 enum {
29         SLICELEN        = 1
30 };
31
32 struct voxscape {
33         int xsz, ysz;
34         unsigned char *height;
35         unsigned char *color;
36         int xshift, xmask, ymask;
37
38         int hfilt, cfilt;
39
40         /* framebuffer */
41         uint16_t *fb;
42         int fbwidth, fbheight;
43         int *coltop;
44         int horizon;
45
46         /* view */
47         int32_t x, y, angle;
48         int vheight;
49
50         /* projection */
51         int fov, znear, zfar;
52         int nslices;
53         int32_t *slicelen;
54         int proj_dist;
55
56         int zfog;       /* fog start Z (0: no fog) */
57         uint8_t fogcolor;
58
59         unsigned int valid;
60 };
61
62 static int *projtab;
63
64 int vox_quality = 1;
65
66 struct voxscape *vox_create(int xsz, int ysz, uint8_t *himg, uint8_t *cimg)
67 {
68         struct voxscape *vox;
69
70         assert(xsz == XSZ && ysz == YSZ);
71
72         if(!(vox = calloc(1, sizeof *vox))) {
73                 return 0;
74         }
75         vox->height = himg;
76         vox->color = cimg;
77         vox->xsz = xsz;
78         vox->ysz = ysz;
79
80         vox->xmask = vox->xsz - 1;
81         vox->ymask = vox->ysz - 1;
82
83         vox->xshift = -1;
84         while(xsz) {
85                 xsz >>= 1;
86                 vox->xshift++;
87         }
88
89         vox->vheight = 80;
90         vox->proj_dist = 4;     /* TODO */
91
92         return vox;
93 }
94
95 void vox_free(struct voxscape *vox)
96 {
97         if(!vox) return;
98
99         free(vox->color);
100         free(vox->height);
101         free(vox->coltop);
102         free(vox->slicelen);
103         free(vox);
104 }
105
106 uint8_t *vox_texture(struct voxscape *vox, uint8_t *data)
107 {
108         if(data) {
109                 memcpy(vox->color, data, vox->xsz * vox->ysz);
110         }
111         return vox->color;
112 }
113
114 uint8_t *vox_heightmap(struct voxscape *vox, uint8_t *data)
115 {
116         if(data) {
117                 memcpy(vox->height, data, vox->xsz * vox->ysz);
118         }
119         return vox->height;
120 }
121
122 void vox_fog(struct voxscape *vox, int zstart, uint8_t color)
123 {
124         vox->zfog = zstart;
125         vox->fogcolor = color;
126 }
127
128 #define H(x, y) \
129         vox->height[((((y) >> 16) & YMASK) << XSHIFT) + (((x) >> 16) & XMASK)]
130 #define C(x, y) \
131         vox->color[((((y) >> 16) & YMASK) << XSHIFT) + (((x) >> 16) & XMASK)]
132
133 #ifdef NO_LERP
134 #define vox_height(vox, x, y)   H(x, y)
135 #define vox_color(vox, x, y)    C(x, y)
136
137 #else
138
139 int vox_height(struct voxscape *vox, int32_t x, int32_t y)
140 {
141         int32_t u, v;
142         int h00, h01, h10, h11, h0, h1;
143
144         if(!vox->hfilt) {
145                 return H(x, y);
146         }
147
148         h00 = H(x, y);
149         h01 = H(x, y + 0x10000);
150         h10 = H(x + 0x10000, y);
151         h11 = H(x + 0x10000, y + 0x10000);
152
153         u = x & 0xffff;
154         v = y & 0xffff;
155
156         h0 = XLERP(h00, h01, v, 16);
157         h1 = XLERP(h10, h11, v, 16);
158         return XLERP(h0, h1, u, 16);
159 }
160
161 int vox_color(struct voxscape *vox, int32_t x, int32_t y)
162 {
163         int32_t u, v;
164         int c00, c01, c10, c11, c0, c1;
165
166         if(!vox->cfilt) {
167                 return C(x, y);
168         }
169
170         c00 = C(x, y);
171         c01 = C(x, y + 0x10000);
172         c10 = C(x + 0x10000, y);
173         c11 = C(x + 0x10000, y + 0x10000);
174
175         u = x & 0xffff;
176         v = y & 0xffff;
177
178         c0 = XLERP(c00, c01, v, 16);
179         c1 = XLERP(c10, c11, v, 16);
180         return XLERP(c0, c1, u, 16);
181 }
182 #endif  /* !NO_LERP */
183
184
185 void vox_filter(struct voxscape *vox, int hfilt, int cfilt)
186 {
187         vox->hfilt = hfilt;
188         vox->cfilt = cfilt;
189 }
190
191 void vox_framebuf(struct voxscape *vox, int xres, int yres, void *fb, int horizon)
192 {
193         if(xres != vox->fbwidth) {
194                 if(!(vox->coltop = iwram_sbrk(xres * sizeof *vox->coltop))) {
195                         panic(get_pc(), "vox_framebuf: failed to allocate column table (%d)\n", xres);
196                         return;
197                 }
198         }
199         vox->fb = fb;
200         vox->fbwidth = xres;
201         vox->fbheight = yres;
202         vox->horizon = horizon >= 0 ? horizon : (vox->fbheight >> 1);
203 }
204
205 void vox_view(struct voxscape *vox, int32_t x, int32_t y, int h, int32_t angle)
206 {
207         if(h < 0) {
208                 h = vox_height(vox, x, y) - h;
209         }
210
211         vox->x = x;
212         vox->y = y;
213         vox->vheight = h;
214         vox->angle = angle;
215
216         vox->valid &= ~SLICELEN;
217 }
218
219 void vox_proj(struct voxscape *vox, int fov, int znear, int zfar)
220 {
221         vox->fov = fov;
222         vox->znear = znear;
223         vox->zfar = zfar;
224
225         vox->nslices = vox->zfar - vox->znear;
226         if(!vox->slicelen) {
227                 if(!(vox->slicelen = iwram_sbrk(vox->nslices * sizeof *vox->slicelen))) {
228                         panic(get_pc(), "vox_proj: failed to allocate slice length table (%d)\n", vox->nslices);
229                 }
230                 if(!(projtab = iwram_sbrk(vox->nslices * sizeof *projtab))) {
231                         panic(get_pc(), "vox_proj: failed to allocate projection table (%d)\n", vox->nslices);
232                 }
233         }
234
235         vox->valid &= ~SLICELEN;
236 }
237
238 /* algorithm:
239  * calculate extents of horizontal equidistant line from the viewer based on fov
240  * for each column step along this line and compute height for each pixel
241  * fill the visible (top) part of each column
242  */
243 ARM_IWRAM
244 void vox_render(struct voxscape *vox)
245 {
246         int i;
247
248         vox_begin(vox);
249
250         if(vox_quality) {
251                 for(i=0; i<vox->nslices; i++) {
252                         vox_render_slice(vox, i);
253                 }
254         } else {
255                 for(i=0; i<vox->nslices; i++) {
256                         if(i >= 10 && (i & 1) == 0) {
257                                 continue;
258                         }
259                         vox_render_slice(vox, i);
260                 }
261         }
262 }
263
264 ARM_IWRAM
265 void vox_begin(struct voxscape *vox)
266 {
267         int i;
268
269         memset(vox->coltop, 0, FBWIDTH * sizeof *vox->coltop);
270
271         if(!(vox->valid & SLICELEN)) {
272                 float theta = (float)vox->fov * M_PI / 360.0f;  /* half angle */
273                 for(i=0; i<vox->nslices; i++) {
274                         vox->slicelen[i] = (int32_t)((vox->znear + i) * tan(theta) * 4.0f * 65536.0f);
275                         projtab[i] = (HSCALE << 8) / (vox->znear + i);
276                 }
277                 vox->valid |= SLICELEN;
278         }
279 }
280
281 ARM_IWRAM
282 void vox_render_slice(struct voxscape *vox, int n)
283 {
284         int i, j, hval, last_hval, colstart, colheight, col, z, offs, last_offs = -1;
285         int32_t x, y, len, xstep, ystep;
286         uint8_t color, last_col;
287         uint16_t *fbptr;
288
289         z = vox->znear + n;
290
291         len = vox->slicelen[n] >> 8;
292         xstep = (((COS(vox->angle) >> 4) * len) >> 4) / (FBWIDTH / 2);
293         ystep = (((SIN(vox->angle) >> 4) * len) >> 4) / (FBWIDTH / 2);
294
295         x = vox->x - SIN(vox->angle) * z - xstep * (FBWIDTH / 4);
296         y = vox->y + COS(vox->angle) * z - ystep * (FBWIDTH / 4);
297
298         for(i=0; i<FBWIDTH/2; i++) {
299                 col = i << 1;
300                 offs = (((y >> 16) & YMASK) << XSHIFT) + ((x >> 16) & XMASK);
301                 if(offs == last_offs) {
302                         hval = last_hval;
303                         color = last_col;
304                 } else {
305                         hval = vox->height[offs] - vox->vheight;
306                         /*hval = hval * HSCALE / (vox->znear + n) + vox->horizon;*/
307                         hval = ((hval * projtab[n]) >> 8) + vox->horizon;
308                         if(hval > FBHEIGHT) hval = FBHEIGHT;
309                         color = vox->color[offs];
310                         last_offs = offs;
311                         last_hval = hval;
312                         last_col = color;
313                 }
314                 if(hval > vox->coltop[col]) {
315                         colstart = FBHEIGHT - hval;
316                         colheight = hval - vox->coltop[col];
317                         fbptr = vox->fb + colstart * (FBPITCH / 2) + i;
318
319                         for(j=0; j<colheight; j++) {
320                                 *fbptr = color | ((uint16_t)color << 8);
321                                 fbptr += FBPITCH / 2;
322                         }
323                         vox->coltop[col] = hval;
324                 }
325                 x += xstep;
326                 y += ystep;
327         }
328 }
329
330 ARM_IWRAM
331 void vox_sky_solid(struct voxscape *vox, uint8_t color)
332 {
333         int i, j, colheight;
334         uint16_t *fbptr;
335
336         for(i=0; i<FBWIDTH / 2; i++) {
337                 fbptr = vox->fb + i;
338                 colheight = FBHEIGHT - vox->coltop[i << 1];
339
340                 for(j=0; j<colheight; j++) {
341                         *fbptr = color | ((uint16_t)color << 8);
342                         fbptr += FBPITCH / 2;
343                 }
344         }
345 }
346
347 ARM_IWRAM
348 void vox_sky_grad(struct voxscape *vox, uint8_t chor, uint8_t ctop)
349 {
350         int i, j, colheight, t;
351         int d = FBHEIGHT - vox->horizon;
352         uint8_t grad[FBHEIGHT];
353         uint16_t *fbptr;
354
355         for(i=0; i<d; i++) {
356                 t = (i << 16) / d;
357                 grad[i] = XLERP(ctop, chor, t, 16);
358         }
359         for(i=d; i<FBHEIGHT; i++) {
360                 grad[i] = chor;
361         }
362
363         for(i=0; i<FBWIDTH / 2; i++) {
364                 fbptr = vox->fb + i;
365                 colheight = FBHEIGHT - vox->coltop[i << 1];
366
367                 for(j=0; j<colheight; j++) {
368                         *fbptr = grad[j] | ((uint16_t)grad[j] << 8);
369                         fbptr += FBPITCH / 2;
370                 }
371         }
372 }