1 /// tmap_scanline_per - Pentium-Pro-optimized assembly version
2 /// written by Brian Raiter, Mar 1998.
3 /// lighting roundoff error fixed by Matt Mueller, July 1999
5 /// The gist of the algorithm is as follows (note that this is
6 /// pseudocode, not actual C):
12 /// int x, ubyz, vbyz;
13 /// byte texmap[64][64] = pixptr;
14 /// byte framebuffer[][bytes_per_row] = write_buffer;
15 /// byte lightingtable[][256] = gr_fade_table;
18 /// for (x = fx_xleft ; x <= fx_xright ; ++x) {
19 /// ubyz = (u / z) & 63;
20 /// vbyz = (v / z) & 63;
21 /// c = texmap[ubyz][vbyz];
22 /// if (c != TRANSPARENT_COLOR)
23 /// framebuffer[fx_y][x] = lightingtable[l / 65536][c];
30 /// The global variable Transparency_on is zero when it is known that
31 /// there are no transparencies involved, so in that case we use a
32 /// different loop that skips the transparency test.
34 /// The actual algorithm used here only does the division calculations
35 /// every fourth pixel, and linearly interpolates the other three.
36 /// Something along the lines of:
38 /// /* Initial values as before */
39 /// int ubyz0, ubyz0, ubyz4, ubyz4, du1, dv1, i;
43 /// for (x = fx_xleft ; x <= fx_xright - 3 ; x += 4) {
44 /// u += fx_du_dx * 4;
45 /// v += fx_dv_dx * 4;
46 /// z += fx_dz_dx * 4;
49 /// du1 = (ubyz4 - ubyz0) / 4;
50 /// dv1 = (vbyz4 - vbyz0) / 4;
53 /// for (i = 0 ; i < 4 ; ++i) {
54 /// c = texmap[ubyz & 63][vbyz & 63];
55 /// if (c != TRANSPARENT_COLOR)
56 /// framebuffer[fx_y][x + i] = lightingtable[l / 65536][c];
64 /// for ( ; x <= fx_xright ; ++x) {
65 /// /* Finish off remaining 0-3 pixels */
68 /// So much for the basic overview.
70 /// In this version, the PPro's floating-point unit is pressed into
71 /// service to do the actual divisions, so that 1/z can be calculated
72 /// first, and the resulting reciprocal multiplied with u and v. These
73 /// two products are then stored back out as integers. This keeps us
74 /// down to doing only one division every four pixels, during which
75 /// other integer instructions can be overlapped.
77 /// The algorithm actually divides 64 by z, so that the rounded-off
78 /// products will effectively be stored with six fraction bits. This
79 /// allows the algorithm to correct for minor floating-point roundoff
80 /// errors. Two fraction bits are kept during the interpolation of the
81 /// three middle pixels, which hopefully increases the accuracy of the
84 /// We only need the lowest six (integral) bits of u/z and v/z for
85 /// each pixptr offset, so we only need eight bits of each fourth
86 /// pair of values to figure the interpolation. Add with the two
87 /// fractional bits we keep for extra precision flavor, this makes ten
88 /// bits for each value, or twenty to store the full pair. To simplify
89 /// the interpolation, the pair is packed into a single 32-bit
94 /// vvVVVVVVvv____________uuUUUUUUuu
97 /// The unused bits between the u and v values permit the packed
98 /// values to be added/subtracted without the u values spilling over
99 /// into the v values. Then, the instructions "bswap %eax ; roll $6,
100 /// %eax ; andl $0x0FFF, %eax" will right-justify the desired values
101 /// into a pixptr offset.
103 /// The FP stack is loaded up with the values of u, v, and z,
104 /// converted to floats. %ebp is used to hold the value of l, %esi is
105 /// is set to pixptr, and %edi points to our current position in
110 // This is used to abbreviate an annoying external variable name.
112 .equ fadetbl, _gr_fade_table
115 // The following macro encapsulates the floating-point instructions
116 // that put the results of a prior division to use and prepare for the
117 // next division. At the beginning of the macro, the FP stack contains
118 // (from top to bottom): 64/z, z, u, v. The macro computes (64*u)/z,
119 // which is stored in ubyz4, and (64*v)/z, which is stored in vybz4.
120 // Simultaneous with this, the macro adds dudx to u, dvdx to v, and
121 // dzdx to z, and finally puts 64 back onto the stack. At the end of
122 // the macro, the stack contains: 64, z, u, v.
124 .macro DoFPCalcs 0 // The FP stack after each instruction:
126 fst %st(4) // 64/z z u v 64/z
127 fxch %st(2) // u z 64/z v 64/z
128 fmul %st, %st(4) // (64 * u) / z u z 64/z v u/z
129 fadds (dudx) // u += dudx u' z 64/z v u/z
130 fxch %st(3) // v z 64/z u' u/z
131 fmul %st, %st(2) // (64 * v) / z v z v/z u' u/z
132 fadds (dvdx) // v += dvdx v' z v/z u' u/z
133 fxch %st(1) // z v' v/z u' u/z
134 fadds (dzdx) // z += dzdx z' v' v/z u' u/z
135 fxch %st(2) // v/z v' z' u' u/z
136 flds (flt64) // 64 v/z v' z' u' u/z
137 fxch %st(5) // u/z v/z v' z' u' 64
138 fistpl (ubyz4) // v/z v' z' u' 64
139 fistpl (vbyz4) // v' z' u' 64
140 fxch %st(3) // 64 z' u' v'
141 // (ready to start the next division)
147 .equ _gr_fade_table, gr_fade_table
148 .equ _write_buffer, write_buffer
149 .equ _bytes_per_row, bytes_per_row
150 .equ _fx_xleft, fx_xleft
151 .equ _fx_xright, fx_xright
157 .equ _fx_du_dx, fx_du_dx
158 .equ _fx_dv_dx, fx_dv_dx
159 .equ _fx_dz_dx, fx_dz_dx
160 .equ _fx_dl_dx, fx_dl_dx
161 .equ _Transparency_on, Transparency_on
163 .globl asm_ppro_tmap_scanline_per
165 .globl _asm_ppro_tmap_scanline_per
168 .extern _pixptr, _gr_fade_table, _write_buffer
169 .extern _bytes_per_row, _fx_xleft, _fx_xright, _fx_y
170 .extern _fx_u, _fx_v, _fx_z, _fx_l
171 .extern _fx_du_dx, _fx_dv_dx, _fx_dz_dx, _fx_dl_dx
172 .extern _Transparency_on
174 //.local dudx, dvdx, dzdx, dldx, l
175 //.local ubyz, vbyz, uvzero
176 //.local lastquartet, lastpixel, ctwl
183 dudx: .long 0 // u's rate of change as a float
184 dvdx: .long 0 // v's rate of change as a float
185 dzdx: .long 0 // z's rate of change as a float
186 dldx: .long 0 // l's rate of change as an integer
187 l: .long 0 // the current l value
188 ubyz4: .long 0 // u/z for the next iteration
189 vbyz4: .long 0 // v/z for the next iteration
190 uvzero: .long 0 // packed u/z and v/z values
191 lastquartet: .long 0 // where to stop the 4-pixels loop
192 lastpixel: .long 0 // where to stop drawing entirely
193 flt64: .long 0x42800000 // 64.0 (what we divide z into)
194 ctlwd: .long 0 // the pre-tweaked FPU control word
202 // void c_tmap_scanline_per(void)
206 asm_ppro_tmap_scanline_per:
208 _asm_ppro_tmap_scanline_per:
211 // Save registers the compiler might be using.
217 // Kick the FPU into the lowest precision (still enough for our needs)
218 // so as to speed up fdiv.
228 // Multiply dudx, dvdx, and dzdx by four, and store locally, converted
229 // into floating point.
231 movl (_fx_du_dx), %eax
234 movl (_fx_dv_dx), %eax
237 movl (_fx_dz_dx), %eax
248 // bytes_per_row * fx_y is the offset for the current scanline. (We do
249 // this now before we start the first FP division.)
251 movl (_bytes_per_row), %eax
255 // Push v, u, z, and 64.0 onto the FPU stack, and then start
256 // calculating the first 64 / z.
264 // Meanwhile, get l and dldx (again, the latter multiplied by four).
265 // l will be stored in %ebp for the duration. The original values are
266 // divided by 256 so that the byte needed for the fade table offset
269 //Dividing by 256 is bad.. rounding errors and crap. We'll now do that
270 //right before we need to access the table instead. -MM
275 movl (_fx_dl_dx), %edx
280 // Store pixptr, the pointer to our 64x64 texture map, in %esi. Store
281 // write_buffer, the pointer to our frame buffer, in %edi. Then offset
282 // %edi so that it points to pixel [fx_y][fx_xleft]. Calculate a
283 // pointer to [fx_y][fx_xright + 1] so we know when to stop drawing.
284 // Also calculate a pointer to [fx_y][(fx_xright + 1) & ~3] so we know
285 // when to stop drawing four pixels at a time.
288 movl (_write_buffer), %edi
289 movl (_fx_xright), %ecx
293 movl %ecx, (lastpixel)
294 addl (_fx_xleft), %edi
300 movl %ecx, (lastquartet)
302 // Calculate round(64 * u / z) and round(64 * v / z), store, and
303 // increment u, v, and z. Then start calculating the second 64 / z.
308 // Get our u/z and v/z values, lop off the bits we don't care
309 // about, pack, and store in uvzero.
322 // Are there at least four pixels to draw? If not, skip to the epilog
328 // Do we need to test for transparencies?
330 testl $(~0), (_Transparency_on)
333 // If not, then use the simpler loop here.
339 // While the FPU is busy dividing, the latest u/z and v/z values are
340 // retrieved, packed, and stored in uvzero (to be used again in the
341 // next iteration). The old uvzero value, which contains the uv values
342 // for pixel 0, gets subtracted from the new uvzero value to
343 // determined the total change in u/z and v/z across the four pixels,
344 // and this is divided by 4 to get the average. This average is then
345 // used to estimate the values for pixels 1, $2, and 3. The old uvzero
346 // value is used immediately to calculate pixel 0, while %eax, %ebx, and
347 // %ecx are entrusted with the uv values for pixels 1, $2, and 3
348 // respectively, while %edx is our "cleansed" register for using byte
349 // values as memory pointer offsets. %ebp is loaded with the high byte
350 // of l, forming half of the offset for the fade table lookup. (The
351 // pixel from the texture-map bitmap supplies the other half.) Each
352 // value is used to set its pixel as follows (assuming %eax holds our
355 // a: bswapl %eax / move u and v to the
356 // b: roll $6, %eax / far right
357 // c: andl $0x0FFF, %eax / mask off extra bits
358 // d: movb (%esi,%eax), %dl / get texture-map pixel
359 // e: movb fadetbl(%edx,%ebp), %dl / correct for lighting
360 // f: movb %dl, (%edi) / write to frame buffer
362 // The above is done four times, once for each pixel. Some of the
363 // calculations may appear to be interleaved haphazardly, but the PPro
364 // seems to like it this way.
370 movl (uvzero), %eax // %eax = uv for pixel 0
373 andl $0x0FFF, %eax // 0 c
374 movb (%esi,%eax), %dl // 0 d
381 movb fadetbl(%edx,%ebp), %dl // 0 e
398 movb %dl, (%edi) // 0 f
399 lea (%eax,%ecx,2), %ebx // %ebx = uv for pixel 2
400 addl %ecx, %eax // %eax = uv for pixel 1
403 addl %ebx, %ecx // %ecx = uv for pixel 3
407 andl $0x0FFF, %eax // 1 c
408 andl $0x0FFF, %ebx // 2 c
411 movb (%esi,%eax), %dl // 1 d
412 movb fadetbl(%edx,%ebp), %al // 1 e
413 movb (%esi,%ebx), %dl // 2 d
414 movb fadetbl(%edx,%ebp), %bl // 2 e
415 movb %al, 1(%edi) // 1 f
416 andl $0x0FFF, %ecx // 3 c
417 movb %bl, 2(%edi) // 2 f
418 movb (%esi,%ecx), %dl // 3 d
419 movb fadetbl(%edx,%ebp), %cl // 3 e
420 movb %cl, 3(%edi) // 3 f
423 cmpl (lastquartet), %edi
426 // Are there any pixels left at all?
428 cmpl (lastpixel), %edi
437 // This is similar to the LoopTransOff loop, the big change being that
438 // each value retrieved from the texture map is tested against 255,
439 // the transparent "color". A value of 255 in the texture map means to
440 // let the existing value for that pixel in write_buffer go by
441 // unchanged. Thus the code for each pixel looks something like this
444 // a: bswapl %eax / move u and v to the
445 // b: roll $6, %eax / far right
446 // c: andl $0x0FFF, %eax / mask off extra bits
447 // d: movb (%esi,%eax), %dl / get texture-map pixel
448 // e: cmpb $255, %dl / is pixel transparent?
449 // f: sbbb %ah, %ah / yes:%ah=00, no:%ah=FF
450 // g: movb fadetbl(%edx,%ebp), %dl / correct for lighting
451 // h: movb (%edi), %al / get current pixel
452 // i: xorb %al, %dl / combine the two
453 // j: andb %dl, %ah / use %ah as a mask to
454 // k: xorb %ah, %al / select which pixel
455 // l: movb %al, (%edi) / write to frame buffer
457 // When the texture-map value is 255, the code simply writes the
458 // original frame-buffer value back out again; otherwise the new pixel
459 // is written instead. The ands and xors used to accomplish this bulk
460 // up the code, but on the whole it is better than having four
461 // unpredictable jumps in the loop.
466 movl (uvzero), %eax // %eax = uv for pixel 0
472 andl $0x0FFF, %eax // 0 c
474 movb (%esi,%eax), %dl // 0 d
475 cmpb $255, %dl // 0 e
481 movb fadetbl(%edx,%ebp), %dl // 0 g
482 movb (%edi), %al // 0 h
486 movb %al, (%edi) // 0 l
503 lea (%eax,%ecx,2), %ebx // %ebx = uv for pixel 2
504 addl %ecx, %eax // %eax = uv for pixel 1
507 addl %ebx, %ecx // %ecx = uv for pixel 3
510 andl $0x0FFF, %eax // 1 c
511 movb (%esi,%eax), %dl // 1 d
512 cmpb $255, %dl // 1 e
515 movb 1(%edi), %al // 1 h
516 movb fadetbl(%edx,%ebp), %dl // 1 g
519 andl $0x0FFF, %ebx // 2 c
522 movb (%esi,%ebx), %dl // 2 d
523 cmpb $255, %dl // 2 e
525 movb fadetbl(%edx,%ebp), %dl // 2 g
526 andl $0x0FFF, %ecx // 3 c
527 movb 2(%edi), %bl // 2 h
531 movb (%esi,%ecx), %dl // 3 d
532 cmpb $255, %dl // 3 e
534 movb 3(%edi), %cl // 3 h
535 movb fadetbl(%edx,%ebp), %dl // 3 g
540 movb %al, 1(%edi) // 1 l
542 movb %bl, 2(%edi) // 2 l
544 movb %cl, 3(%edi) // 3 l
547 cmpl (lastquartet), %edi
550 // Quit if there are none at all left.
552 cmpl (lastpixel), %edi
558 // Here we finish off the last one-to-three pixels assigned to us.
559 // Rather than calculating values for all four pixels, we just divide
560 // the difference by four and keep adding this average into the value
561 // as needed. (This code is not particularly optimized, by the way,
562 // since it represents such a miniscule amount of the running time.)
583 LoopLastBits: movl %ebx, %eax
587 movb (%esi,%eax), %dl
590 movb fadetbl(%edx,%ebp), %dl
592 LetPixelBy: incl %edi
594 cmpl (lastpixel), %edi
600 // We're done! Clear the stacks, reset the FPU control word, and we
601 // are so out of here.