texture.c
9.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#include "graphic.h"
int lod_tab[MM_MAX_LEVEL+2] =
{ 0, 51, 102, 153, 204, 256 };
/*------------------------------*/
set_texture_image( im)
struct Image *im;
{
struct Image *ti = &tex_image[0];
*ti = *im;
}
/*------------------------------*/
do_tc_step()
{
struct TC_step *ts;
struct Tex_coord *tc;
ts = &tc_step[0];
tc = &tex_coord[0];
/* perspective on first coord of span */
if (ts->first) {
ts->first = FALSE;
tc_div_count = 0; /* force last interval coord */
do_tc_persp( ts->pnt.s, ts->pnt.t, ts->pnt.w, &tc_cur.s, &tc_cur.t);
}
/* perspective on coord of span at TC_DIV_STEP interval */
if (--tc_div_count < 0 ) {
tc_pre = tc_cur; /* copy persp coord */
tc_div_count = TC_DIV_STEP-1; /* enable interpolation */
/* step to span coordinate at plus TC_DIV_STEP */
ts->pnt.s += ts->del.s << TC_DIV_SHFT;
ts->pnt.t += ts->del.t << TC_DIV_SHFT;
ts->pnt.w += ts->del.w << TC_DIV_SHFT;
do_tc_persp( ts->pnt.s, ts->pnt.t, ts->pnt.w, &tc_cur.s, &tc_cur.t);
/* compute delta for linear interpolation in TC_DIV_STEP interval */
tc_del.s = (tc_cur.s - tc_pre.s) >> TC_DIV_SHFT;
tc_del.t = (tc_cur.t - tc_pre.t) >> TC_DIV_SHFT;
}
/* write s, t, l to texture filter */
tc->s = int_ewa( tc_pre.s);
tc->t = int_ewa( tc_pre.t);
tc->l = int_ewa( ts->pnt.l);
/* interpolate to next coord */
tc_pre.s += tc_del.s;
tc_pre.t += tc_del.t;
ts->pnt.l += ts->del.l;
}
/*------------------------------*/
do_tc_persp( s, t, w, sp, tp)
int s, t, w, *sp, *tp;
{
/* extract integer portion */
s = int_ewa( s);
t = int_ewa( t);
w = int_ewa( w);
/* perspective correction s/w, t/w */
if (en_tc_persp) {
/* clamp w before divide so no overflow */
clamp( 0, w, (TC_PERSP_ONE-1));
s = (int)((float)TC_PERSP_ONE*(float)s/(float)w);
t = (int)((float)TC_PERSP_ONE*(float)t/(float)w);
}
/* convert to fixed point for interpolation */
*sp = fix_ewa( s);
*tp = fix_ewa( t);
}
/*------------------------------*/
do_tex_filter()
{
struct Image *im;
struct Tex_coord *tc;
struct Tex_color *tp;
struct Tex_color level_color, *tq = &level_color; /* lod color */
int level_type, level_frac, level; /* mipmap */
int l, f; /* lod temps */
/* assign pointers */
im = &tex_image[0];
tc = &tex_coord[0];
tp = &tex_color[0];
/* default to mipmap level 0 */
level = 0;
/* if not tram tile, use tile to hold image info */
if (!en_tramtile) {
mipmap[level].w = im->xsize;
mipmap[level].s = im->xsize;
mipmap[level].t = im->ysize;
mipmap[level].addr = 0;
}
/* extract level of detail */
if (en_tc_lod) {
int i;
if (!en_mipmap) max_mipmap = MM_MAX_LEVEL;
level_type = MM_LEVEL_LO;
level_frac = 0;
l = tc->l >> TI_FRAC_SHFT;
if (l & (-1 << (max_mipmap+1))) level_type = MM_LEVEL_HI;
else {
for (i=max_mipmap; i>=0; i--) {
if (l & (1 << i)) break;
}
if (i >= 0) {
level = i;
level_frac = (tc->l >> level) & TI_FRAC_MASK;
level_type = MM_LEVEL_IN;
}
}
}
/* sample texture */
if (en_tc_lod) {
unpack_colora( im->avg_color, tq->r, tq->g, tq->b, tq->a);
if (level_type == MM_LEVEL_HI) *tp = *tq;
else if (level_type == MM_LEVEL_IN) {
if (en_mipmap) {
do_tile_sample( level, tc->s, tc->t, tp);
if (level < max_mipmap)
do_tile_sample( level+1, tc->s, tc->t, tq);
f = (int)((float)level_frac * (256.0/TI_FRAC_ONE));
} else { /* no mipmap */
do_tile_sample( 0, tc->s, tc->t, tp);
f = lod_tab[level];
f += (int)((float)(lod_tab[level+1] - f)
* level_frac * (1.0/TI_FRAC_ONE));
}
tp->r = blend_color( tp->r, tq->r, f);
tp->g = blend_color( tp->g, tq->g, f);
tp->b = blend_color( tp->b, tq->b, f);
tp->a = blend_color( tp->a, tq->a, f);
}
else if (level_type == MM_LEVEL_LO)
do_tile_sample( 0, tc->s, tc->t, tp);
} /* end en_tc_lod */
else do_tile_sample( 0, tc->s, tc->t, tp);
}
/*------------------------------*/
do_tile_sample( level, s, t, tp)
int level, s, t;
struct Tex_color *tp;
{
struct Image *im = &tex_image[0];
struct Tile *tile;
int sf, tf;
int si, ti;
int s4[4], t4[4];
int tex4[4];
int r0, g0, b0, a0;
int r1, g1, b1, a1;
int r2, g2, b2, a2;
int r3, g3, b3, a3;
int c0, c1;
int sin = s, tin = t;
/* XXX if tile, subtract first s, t of tile to normalize for now */
if (en_tramtile) {
s -= im->s << TI_FRAC_SHFT;
t -= im->t << TI_FRAC_SHFT;
}
/* get pointer to tile */
tile = &mipmap[level];
/* shift coordinate by level */
s = s >> level;
t = t >> level;
/* extract fractional coordinates */
sf = s & TI_FRAC_MASK;
tf = t & TI_FRAC_MASK;
/* extract integer coordinates */
s = s >> TI_FRAC_SHFT;
t = t >> TI_FRAC_SHFT;
/* increment for tile coords and clamp */
si = s + 1;
if (s < 0) {
/*
printf("s underflow tc->s %d im->s %d level %d s %d t->s %d.\n",
sin, im->s, level, s, tile->s);
*/
s = 0;
si = s;
sf = 0;
} else if (s >= tile->s-1) {
/*
printf("s overflow tc->s %d im->s %d level %d s %d t->s %d.\n",
sin, im->s, level, s, tile->s);
*/
s = tile->s-1;
si = s;
sf = 0;
}
ti = t + 1;
if (t < 0) {
t = 0;
ti = t;
tf = 0;
} else if (t >= tile->t-1) {
t = tile->t-1;
ti = t;
tf = 0;
}
/* construct array of texel coordinates of tile */
s4[0] = s; t4[0] = t;
s4[1] = si; t4[1] = t;
s4[2] = s; t4[2] = ti;
s4[3] = si; t4[3] = ti;
/* get tile of texels */
if (en_tramtile) tram_tile( tile, s4, t4, tex4);
/* XXX 4/4/4/4 textures for now */
/* XXXX bypass tram and directly read image data for debugging */
else {
struct Image *im = &tex_image[0];
char *tex;
int i;
for (i=0; i<4; i++) {
tex = im->base + (t4[i] * im->lsize) + (s4[i] << 1);
tex4[i] = *(unsigned short *)tex;
}
} /* end read image direct */
/* bilinear filter tile */
if (en_tf_bilinear) {
unpack_colora4( tex4[0], r0, g0, b0, a0);
unpack_colora4( tex4[1], r1, g1, b1, a1);
unpack_colora4( tex4[2], r2, g2, b2, a2);
unpack_colora4( tex4[3], r3, g3, b3, a3);
/* linearly interpolate each channel */
c0 = filter_color( r0, r1, sf);
c1 = filter_color( r2, r3, sf);
tp->r = filter_color( c0, c1, tf);
c0 = filter_color( g0, g1, sf);
c1 = filter_color( g2, g3, sf);
tp->g = filter_color( c0, c1, tf);
c0 = filter_color( b0, b1, sf);
c1 = filter_color( b2, b3, sf);
tp->b = filter_color( c0, c1, tf);
c0 = filter_color( a0, a1, sf);
c1 = filter_color( a2, a3, sf);
tp->a = filter_color( c0, c1, tf);
} else { /* point sample */
unpack_colora4( tex4[0], tp->r, tp->g, tp->b, tp->a);
}
}
/*------------------------------*/
/*
* Texture Memory Simulator
*/
tram_load(struct Image *im)
{
char *iaddr, *taddr;
int b, h;
int flip;
int im_bytes, tr_bytes;
txlsize = IM_BYTE(im->type);
/* XXX do precision mask later */
iaddr = (im->t * im->lsize) + (im->s * txlsize) + im->base;
im_bytes = im->w * txlsize;
tr_bytes = im_bytes / TRAM_LSIZE;
tr_bytes = tr_bytes * TRAM_LSIZE;
if (im_bytes > tr_bytes) tr_bytes += TRAM_LSIZE;
taddr = &(tram[im->addr]);
for (h=0; h<im->h; h++) {
flip = (h & 1) << 2;
for (b=0; b<im_bytes; b++)
*(taddr+(b^flip)) = *(iaddr+b);
/* pick up trailing bytes */
for (b=im_bytes; b<tr_bytes; b++)
*(taddr+(b^flip)) = 0;
iaddr += im->lsize;
taddr += tr_bytes;
}
/* save tile attributes in top mipmap */
mipmap[0].s = im->w;
mipmap[0].t = im->h;
mipmap[0].w = tr_bytes / txlsize;
mipmap[0].addr = im->addr;
max_mipmap = MM_MAX_LEVEL;
if (en_tc_lod && en_mipmap) {/* generate mip map for this tile */
int level;
ubyte *taddr, *saddr;
int s, t, si, ti;
int s4[4];
int t4[4];
int tex4[4];
int r0, g0, b0, a0, r1, g1, b1, a1;
int texel;
int i;
for (level=1; level<=MM_MAX_LEVEL; level++) {
mipmap[level].addr = mipmap[level-1].addr +
mipmap[level-1].w * txlsize * mipmap[level-1].t;
taddr = &(tram[ mipmap[level].addr ]);
/* downfilter by 2X, bump odd size */
mipmap[level].s = (mipmap[0].s + (1<<(level-1))) >> level;
mipmap[level].t = (mipmap[0].t + (1<<(level-1))) >> level;
/* width must be a multiple of 4 texels */
mipmap[level].w = ((mipmap[level].s + 3) >> 2) << 2;
for (t=0; t<mipmap[level-1].t; t+=2) {
flip = (t & 2) << 1; /* invert bit 4 on odd line */
ti = t + 1;
if (ti >= mipmap[level-1].t) ti = t;
saddr = taddr;
taddr += mipmap[level].w * txlsize;
for (s=0; s<mipmap[level-1].s; s+=2) {
si = s + 1;
if (si >= mipmap[level-1].s) si = s;
s4[0] = s; t4[0] = t;
s4[1] = si; t4[1] = t;
s4[2] = s; t4[2] = ti;
s4[3] = si; t4[3] = ti;
tram_tile( &mipmap[level-1], s4, t4, tex4);
r1 = g1 = b1 = a1 = 0;
for (i=0; i<4; i++) {
unpack_colora4( tex4[i], r0, g0, b0, a0);
r1 += r0; g1 += g0; b1 += b0; a1 += a0;
}
r1 = r1 >> 2; g1 = g1 >> 2; b1 = b1 >> 2; a1 = a1 >> 2;
texel = pack_colora4( r1, g1, b1, a1);
*(short *)((int)saddr^flip) = texel;
saddr += txlsize;
} /* end s */
} /* end t */
if (mipmap[level].s <= MM_MIN_SIZE &&
mipmap[level].t <= MM_MIN_SIZE) {
max_mipmap = level;
break;
}
} /* end level */
}/* end generate mipmap */
}
/*----------------------------------------------------*/
tram_tile(struct Tile *tile, int *s, int *t, unsigned long *texel)
{
int i;
int bank, row;
unsigned int addr;
int overlap;
unsigned char r, g, b, a;
unsigned long tex;
struct Image *im;
/* XXX 4 16 bit 4/4/4/4 samples for now */
for (i=0; i<4; i++) {
/* bank and row indexing */
bank = (s[i] & 0x3 ^ ((t[i] & 0x1) << 1)) << 1;
row = ((t[i] * tile->w + s[i]) * txlsize) / TRAM_LSIZE;
addr = tile->addr + row * TRAM_WSIZE + bank;
overlap = (i == 0) ? bank : overlap ^ bank;
/* XXX sort for output texture quad 0:0,0 1:1,0 2:1,0 */
/* for now, out sort in the same order as in sort */
if ((addr < 0) || (addr > TRAM_SIZE-1))
printf("tram addr error.\n");
else
texel[i] = (tram[addr] << 8) | tram[addr+1];
#ifdef 0
printf("%1d: s:%3d t:%3d bank:%3d row:%3d addr:%3d tex:%4x\n",
i, s[i], t[i], bank, row, addr, tex);
#endif
}
if (overlap)
printf("TRAM bank overlap\n");
}
tram_span()
{
}