gba-x3dtest
view src/x3d.c @ 19:62390f9cc93e
texture cache optimization failed
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Mon, 30 Jun 2014 09:07:41 +0300 |
parents | 0a7f402892b3 |
children | 2e903e27e35a |
line source
1 #include "config.h"
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <math.h>
6 #include "x3d.h"
7 #include "fixed.h"
8 #include "sincos.h"
9 #include "logger.h"
10 #include "polyfill.h"
11 #include "gbasys.h"
12 #include "x3d_impl.h"
14 int dbg_fill_dump;
16 #define MAT_STACK_SIZE 4
18 struct matrix {
19 int32_t m[12];
20 };
22 static void proc_vertex(const int32_t *vin, const int32_t *cin, const int32_t *tin,
23 pvec3 *vout, pvec3 *cout, pvec2 *tout);
24 static int dump_frame(struct pixel_buffer *frame);
27 static int32_t proj_fov = M_PI_X16;
28 static int32_t proj_aspect = 65536;
29 static int32_t inv_proj_aspect = 65536;
30 static int32_t proj_near = ftox16(0.5);
31 static int32_t proj_far = 500 << 16;
32 static int32_t inv_tan_half_xfov, inv_tan_half_yfov;
34 #define ID_INIT {65536, 0, 0, 0, 0, 65536, 0, 0, 0, 0, 65536, 0}
36 static struct matrix identity = { ID_INIT };
38 static short mtop;
39 static struct matrix mstack[MAT_STACK_SIZE] = { {ID_INIT}, {ID_INIT} };
41 static const int32_t *vertex_array;
42 static unsigned short vertex_count;
43 static const int32_t *color_array;
44 static unsigned short color_count;
45 static const int32_t *texcoord_array;
46 static unsigned short texcoord_count;
48 static int32_t im_color[3];
49 static int32_t im_texcoord[2];
50 static uint8_t im_color_index;
52 #define MAX_TEXTURES 64
53 static struct texture textures[MAX_TEXTURES];
54 static short cur_tex = -1;
57 void x3d_projection(int fov, int32_t aspect, int32_t nearz, int32_t farz)
58 {
59 proj_fov = (M_PI_X16 * fov) / 180;
60 proj_aspect = aspect;
61 inv_proj_aspect = x16div(65536, proj_aspect);
62 proj_near = nearz;
63 proj_far = farz;
65 inv_tan_half_yfov = (int32_t)(65536.0 / tan(0.5 * proj_fov / 65536.0));
66 inv_tan_half_xfov = x16mul(inv_tan_half_yfov, aspect);
67 }
69 int x3d_push_matrix(void)
70 {
71 short newtop = mtop + 1;
72 if(newtop >= MAT_STACK_SIZE) {
73 return -1;
74 }
75 memcpy(mstack + newtop, mstack + mtop, sizeof *mstack);
76 mtop = newtop;
77 return 0;
78 }
80 int x3d_pop_matrix(void)
81 {
82 if(mtop <= 0) {
83 return -1;
84 }
85 --mtop;
86 return 0;
87 }
89 void x3d_load_matrix(int32_t *m)
90 {
91 memcpy(mstack[mtop].m, m, sizeof *mstack);
92 }
95 #define M(i,j) (((i) << 2) + (j))
96 void x3d_mult_matrix(int32_t *m)
97 {
98 int i, j;
99 struct matrix tmp;
101 memcpy(tmp.m, mstack[mtop].m, sizeof tmp);
103 for(i=0; i<3; i++) {
104 for(j=0; j<4; j++) {
105 mstack[mtop].m[M(i, j)] =
106 x16mul(m[M(0, j)], tmp.m[M(i, 0)]) +
107 x16mul(m[M(1, j)], tmp.m[M(i, 1)]) +
108 x16mul(m[M(2, j)], tmp.m[M(i, 2)]);
109 }
110 mstack[mtop].m[M(i, 3)] += tmp.m[M(i, 3)];
111 }
112 }
114 void x3d_load_identity(void)
115 {
116 memcpy(mstack[mtop].m, identity.m, sizeof identity);
117 }
119 void x3d_translate(int32_t x, int32_t y, int32_t z)
120 {
121 int32_t m[] = ID_INIT;
122 m[3] = x;
123 m[7] = y;
124 m[11] = z;
126 x3d_mult_matrix(m);
127 }
129 void x3d_rotate(int32_t deg, int32_t x, int32_t y, int32_t z)
130 {
131 int32_t xform[] = ID_INIT;
133 int32_t angle = x16mul(M_PI_X16, deg) / 180;
134 int32_t sina = sin_x16(angle);
135 int32_t cosa = cos_x16(angle);
136 int32_t one_minus_cosa = 65536 - cosa;
137 int32_t nxsq = x16sq(x);
138 int32_t nysq = x16sq(y);
139 int32_t nzsq = x16sq(z);
141 xform[0] = nxsq + x16mul(65536 - nxsq, cosa);
142 xform[4] = x16mul(x16mul(x, y), one_minus_cosa) - x16mul(z, sina);
143 xform[8] = x16mul(x16mul(x, z), one_minus_cosa) + x16mul(y, sina);
144 xform[1] = x16mul(x16mul(x, y), one_minus_cosa) + x16mul(z, sina);
145 xform[5] = nysq + x16mul(65536 - nysq, cosa);
146 xform[9] = x16mul(x16mul(y, z), one_minus_cosa) - x16mul(x, sina);
147 xform[2] = x16mul(x16mul(x, z), one_minus_cosa) - x16mul(y, sina);
148 xform[6] = x16mul(x16mul(y, z), one_minus_cosa) + x16mul(x, sina);
149 xform[10] = nzsq + x16mul(65536 - nzsq, cosa);
151 x3d_mult_matrix(xform);
152 }
154 void x3d_scale(int32_t x, int32_t y, int32_t z)
155 {
156 int32_t m[] = ID_INIT;
158 m[0] = x;
159 m[5] = y;
160 m[10] = z;
162 x3d_mult_matrix(m);
163 }
165 void x3d_vertex_array(int count, const int32_t *ptr)
166 {
167 vertex_array = ptr;
168 vertex_count = count;
169 }
171 void x3d_color_array(int count, const int32_t *ptr)
172 {
173 color_array = ptr;
174 color_count = count;
175 }
177 void x3d_texcoord_array(int count, const int32_t *ptr)
178 {
179 texcoord_array = ptr;
180 texcoord_count = count;
181 }
183 int x3d_draw(int prim, int vnum)
184 {
185 int i, j, pverts = prim;
186 const int32_t *vptr = vertex_array;
187 const int32_t *cptr = color_array;
188 const int32_t *tptr = texcoord_array;
189 #ifndef PALMODE
190 short cr, cg, cb;
191 #endif
192 uint16_t color;
194 if(!vertex_array) return -1;
196 if(vnum > vertex_count) {
197 logmsg(LOG_DBG, "%s called with vnum=%d, but current vertex array has %d vertices\n",
198 __FUNCTION__, vnum, vertex_count);
199 vnum = vertex_count;
200 }
201 if(color_array && vnum > color_count) {
202 logmsg(LOG_DBG, "%s called with vnum=%d, but current color array has %d elements\n",
203 __FUNCTION__, vnum, color_count);
204 vnum = color_count;
205 }
206 if(texcoord_array && vnum > texcoord_count) {
207 logmsg(LOG_DBG, "%s called with vnum=%d, but current texcoord array has %d elements\n",
208 __FUNCTION__, vnum, texcoord_count);
209 vnum = texcoord_count;
210 }
212 for(i=0; i<vnum; i+=pverts) {
213 /* process vertices */
214 pvec3 vpos[4];
215 pvec3 col[4];
216 pvec2 tex[4];
218 for(j=0; j<pverts; j++) {
219 proc_vertex(vptr, cptr, tptr, vpos + j, col + j, tex + j);
221 if(vpos[j].z <= proj_near) {
222 goto skip_prim;
223 }
225 vptr += 3;
226 if(cptr) cptr += 3;
227 if(tptr) tptr += 2;
228 }
230 #ifdef PALMODE
231 color = im_color_index;
232 #else
233 cr = col[0].x >> 8;
234 cg = col[0].y >> 8;
235 cb = col[0].z >> 8;
237 if(cr > 255) cr = 255;
238 if(cg > 255) cg = 255;
239 if(cb > 255) cb = 255;
241 color = RGB(cr, cg, cb);
242 #endif
244 /* project & viewport */
245 for(j=0; j<pverts; j++) {
246 int32_t x, y;
248 x = x16mul(vpos[j].x, inv_tan_half_xfov);
249 x = x16div(x, vpos[j].z);
250 vpos[j].x = (x16mul(x, inv_proj_aspect) + 65536) * (WIDTH / 2);
252 y = x16mul(vpos[j].y, inv_tan_half_yfov);
253 y = x16div(y, vpos[j].z);
254 vpos[j].y = (65536 - y) * (HEIGHT / 2);
255 }
257 switch(pverts) {
258 case X3D_POINTS:
259 draw_point(vpos, color);
260 break;
262 case X3D_LINES:
263 break;
265 case X3D_TRIANGLES:
266 case X3D_QUADS:
267 draw_poly(pverts, vpos, tex, color, cur_tex >= 0 ? textures + cur_tex : 0);
268 if(dbg_fill_dump) {
269 dump_frame(back_buffer);
270 }
271 break;
272 }
273 skip_prim: ;
274 }
276 dbg_fill_dump = 0;
277 return 0;
278 }
280 static void proc_vertex(const int32_t *vin, const int32_t *cin, const int32_t *tin,
281 pvec3 *vout, pvec3 *cout, pvec2 *tout)
282 {
283 int i;
284 int32_t tvert[3];
285 int32_t *mvmat = mstack[mtop].m;
287 /* transform vertex with current matrix */
288 for(i=0; i<3; i++) {
289 tvert[i] = x16mul(mvmat[0], vin[0]) +
290 x16mul(mvmat[1], vin[1]) +
291 x16mul(mvmat[2], vin[2]) +
292 mvmat[3];
293 mvmat += 4;
294 }
296 vout->x = tvert[0];
297 vout->y = tvert[1];
298 vout->z = tvert[2];
299 /*logmsg(LOG_DBG, "%s: (%g %g %g) -> (%g %g %g)\n", __FUNCTION__,
300 x16tof(vin[0]), x16tof(vin[1]), x16tof(vin[2]),
301 x16tof(vout->x), x16tof(vout->y), x16tof(vout->z));*/
303 if(color_array) {
304 cout->x = cin[0];
305 cout->y = cin[1];
306 cout->z = cin[2];
307 } else {
308 cout->x = im_color[0];
309 cout->y = im_color[1];
310 cout->z = im_color[2];
311 }
313 if(texcoord_array) {
314 tout->x = tin[0];
315 tout->y = tin[1];
316 } else {
317 tout->x = im_texcoord[0];
318 tout->y = im_texcoord[1];
319 }
320 }
322 void x3d_color_index(int cidx)
323 {
324 im_color_index = cidx;
325 }
327 void x3d_color(int32_t r, int32_t g, int32_t b)
328 {
329 im_color[0] = r;
330 im_color[1] = g;
331 im_color[2] = b;
332 }
334 static int count_bits(int x)
335 {
336 int i, count = 0;
337 for(i=0; i<32; i++) {
338 if(x & 1) count++;
339 x >>= 1;
340 }
341 return count;
342 }
344 int x3d_create_texture_rgb(int xsz, int ysz, const uint16_t *pixels)
345 {
346 int i, j;
348 if(xsz == 0 || count_bits(xsz) > 1 || ysz == 0 || count_bits(ysz) > 1) {
349 logmsg(LOG_DBG, "%s: texture size (%dx%d) not power of two!\n", __func__, xsz, ysz);
350 return -1;
351 }
353 for(i=0; i<MAX_TEXTURES; i++) {
354 if(!textures[i].pixels) {
355 /*textures[i].pixels = pixels;*/
356 textures[i].pixels = malloc(xsz * ysz * 2);
357 memcpy(textures[i].pixels, pixels, xsz * ysz * 2);
358 textures[i].xsz = xsz;
359 textures[i].ysz = ysz;
360 textures[i].umask = xsz - 1;
361 textures[i].vmask = ysz - 1;
363 for(j=0; j<32; j++) {
364 if((1 << j) == xsz) {
365 textures[i].ushift = j;
366 }
367 if((1 << j) == ysz) {
368 textures[i].vshift = j;
369 }
370 }
372 logmsg(LOG_DBG, "create texture %dx%d: %p\n", xsz, ysz, pixels);
374 return i;
375 }
376 }
377 return -1;
378 }
380 void x3d_enable_texture(int texid)
381 {
382 cur_tex = texid;
383 }
385 void x3d_disable_texture(void)
386 {
387 cur_tex = 0;
388 }
390 int x3d_get_active_texture(void)
391 {
392 return cur_tex;
393 }
395 static int dump_frame(struct pixel_buffer *frame)
396 {
397 static int frameno;
398 char buf[128];
399 FILE *fp;
400 int i, npix;
401 uint16_t *ptr = frame->pixels;
403 sprintf(buf, "dump%03d.ppm", ++frameno);
405 if(!(fp = fopen(buf, "wb"))) {
406 fprintf(stderr, "failed to dump file: %s\n", buf);
407 return -1;
408 }
410 fprintf(fp, "P6\n%d %d\n255\n", frame->x, frame->y);
412 npix = frame->x * frame->y;
413 for(i=0; i<npix; i++) {
414 uint16_t pixel = *ptr++;
415 fputc(GET_R(pixel), fp);
416 fputc(GET_G(pixel), fp);
417 fputc(GET_B(pixel), fp);
418 }
419 fclose(fp);
420 return 0;
421 }