2 * Glide64 - Glide video plugin for Nintendo 64 emulators.
3 * Copyright (c) 2002 Dave2001
4 * Copyright (c) 2003-2009 Sergey 'Gonetz' Lipski
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 //****************************************************************
23 // Glide64 - Glide Plugin for Nintendo 64 emulators
24 // Project started on December 29th, 2001
27 // Dave2001, original author, founded the project in 2001, left it in 2002
28 // Gugaman, joined the project in 2002, left it in 2002
29 // Sergey 'Gonetz' Lipski, joined the project in 2002, main author since fall of 2002
30 // Hiroshi 'KoolSmoky' Morii, joined the project in 2007
32 //****************************************************************
35 // * Write your name and (optional)email, commented by your work, so I know who did it, and so that you can find which parts you modified when it comes time to send it to me.
36 // * Do NOT send me the whole project or file that you modified. Take out your modified code sections, and tell me where to put them. If people sent the whole thing, I would have many different versions, but no idea how to combine them all.
38 //****************************************************************
40 // Software rendering into N64 depth buffer
41 // Idea and N64 depth value format by Orkin
42 // Polygon rasterization algorithm is taken from FATMAP2 engine by Mats Byggmastar, mri@penti.sit.fi
44 // Created by Gonetz, Dec 2004
46 //****************************************************************
50 #include "DepthBufferRender.h"
58 zLUT = new wxUint16[0x40000];
59 for(int i=0; i<0x40000; i++)
61 wxUint32 exponent = 0;
62 wxUint32 testbit = 1 << 17;
63 while((i & testbit) && (exponent < 7))
66 testbit = 1 << (17 - exponent);
69 wxUint32 mantissa = (i >> (6 - (6 < exponent ? 6 : exponent))) & 0x7ff;
70 zLUT[i] = (wxUint16)(((exponent << 11) | mantissa) << 2);
80 static vertexi * max_vtx; // Max y vertex (ending vertex)
81 static vertexi * start_vtx, * end_vtx; // First and last vertex in array
82 static vertexi * right_vtx, * left_vtx; // Current right and left vertex
85 // using float should be faster than int in fact, especialy if using NEON
86 static float right_height, left_height;
87 static float right_x, right_dxdy, left_x, left_dxdy;
88 static float left_z, left_dzdy;
90 __inline float iceil(int x)
92 return cellf((float)x*(1.0f/65536.0f));
94 __inline float i2float(int x)
96 return (float)x*(1.0f/65536.0f);
98 __inline int float2i(float x)
105 static int right_height, left_height;
106 static int right_x, right_dxdy, left_x, left_dxdy;
107 static int left_z, left_dzdy;
109 __inline int imul16(int x, int y) // (x * y) >> 16
111 return (((long long)x) * ((long long)y)) >> 16;
114 __inline int imul14(int x, int y) // (x * y) >> 14
116 return (((long long)x) * ((long long)y)) >> 14;
118 __inline int idiv16(int x, int y) // (x << 16) / y
120 //x = (((long long)x) << 16) / ((long long)y);
127 #if !defined(__GNUC__) && !defined(NO_ASM)
137 #elif !defined(NO_ASM)
139 asm ("idivl %[divisor]"
140 : "=a" (x), "=d" (reminder)
141 : [divisor] "g" (y), "d" (x >> 16), "a" (x << 16));
143 x = (((long long)x) << 16) / ((long long)y);
148 __inline int iceil(int x)
155 static void RightSection(void)
157 // Walk backwards trough the vertex array
159 vertexi * v2, * v1 = right_vtx;
160 if(right_vtx > start_vtx) v2 = right_vtx-1;
161 else v2 = end_vtx; // Wrap to end of array
165 // v2 = bottom vertex
167 // Calculate number of scanlines in this section
169 right_height = iceil(v2->y) - iceil(v1->y);
170 if(right_height <= 0) return;
172 // Guard against possible div overflows
175 right_dxdy = i2float(v2->x - v1->x)/i2float(v2->y - v1->y);
177 if(right_height > 1) {
178 // OK, no worries, we have a section that is at least
179 // one pixel high. Calculate slope as usual.
181 int height = v2->y - v1->y;
182 right_dxdy = idiv16(v2->x - v1->x, height);
185 // Height is less or equal to one pixel.
186 // Calculate slope = width * 1/height
187 // using 18:14 bit precision to avoid overflows.
189 int inv_height = (0x10000 << 14) / (v2->y - v1->y);
190 right_dxdy = imul14(v2->x - v1->x, inv_height);
194 // Prestep initial values
197 float prestep = iceil(v1->y) - i2float(v1->y);
198 right_x = i2float(v1->x) + prestep*right_dxdy;
200 int prestep = (iceil(v1->y) << 16) - v1->y;
201 right_x = v1->x + imul16(prestep, right_dxdy);
205 static void LeftSection(void)
207 // Walk forward trough the vertex array
209 vertexi * v2, * v1 = left_vtx;
210 if(left_vtx < end_vtx) v2 = left_vtx+1;
211 else v2 = start_vtx; // Wrap to start of array
215 // v2 = bottom vertex
217 // Calculate number of scanlines in this section
219 left_height = iceil(v2->y) - iceil(v1->y);
220 if(left_height <= 0) return;
222 // Guard against possible div overflows
225 float invheight = 1.0f/i2float(v2->y - v1->y)
226 left_dxdy = i2float(v2->x - v1->x)*invheight;
227 left_dzdy = i2float(v2->z - v1->z)*invheight;
229 if(left_height > 1) {
230 // OK, no worries, we have a section that is at least
231 // one pixel high. Calculate slope as usual.
233 int height = v2->y - v1->y;
234 left_dxdy = idiv16(v2->x - v1->x, height);
235 left_dzdy = idiv16(v2->z - v1->z, height);
238 // Height is less or equal to one pixel.
239 // Calculate slope = width * 1/height
240 // using 18:14 bit precision to avoid overflows.
242 int inv_height = (0x10000 << 14) / (v2->y - v1->y);
243 left_dxdy = imul14(v2->x - v1->x, inv_height);
244 left_dzdy = imul14(v2->z - v1->z, inv_height);
248 // Prestep initial values
251 float prestep = iceil(v1->y) - i2float(v1->y);
252 left_x = i2float(v1->x) + prestep*left_dxdy;
253 left_z = i2float(v1->z) + prestep*left_dzdy;
255 int prestep = (iceil(v1->y) << 16) - v1->y;
256 left_x = v1->x + imul16(prestep, left_dxdy);
257 left_z = v1->z + imul16(prestep, left_dzdy);
262 void Rasterize(vertexi * vtx, int vertices, int dzdx)
264 start_vtx = vtx; // First vertex in array
266 // Search trough the vtx array to find min y, max y
267 // and the location of these structures.
269 vertexi * min_vtx = vtx;
277 for(int n=1; n<vertices; n++) {
290 // OK, now we know where in the array we should start and
291 // where to end while scanning the edges of the polygon
293 left_vtx = min_vtx; // Left side starting vertex
294 right_vtx = min_vtx; // Right side starting vertex
295 end_vtx = vtx-1; // Last vertex in array
297 // Search for the first usable right section
300 if(right_vtx == max_vtx) return;
302 } while(right_height <= 0);
304 // Search for the first usable left section
307 if(left_vtx == max_vtx) return;
309 } while(left_height <= 0);
311 wxUint16 * destptr = (wxUint16*)(gfx.RDRAM+rdp.zimg);
312 int y1 = iceil(min_y);
313 if (y1 >= (int)rdp.scissor_o.lr_y) return;
318 int x1 = iceil(left_x);
319 if (x1 < (int)rdp.scissor_o.ul_x)
320 x1 = rdp.scissor_o.ul_x;
321 int width = iceil(right_x) - x1;
322 if (x1+width >= (int)rdp.scissor_o.lr_x)
323 width = rdp.scissor_o.lr_x - x1 - 1;
325 if(width > 0 && y1 >= (int)rdp.scissor_o.ul_y) {
331 float z = left_z + (x1 - left_x)*dzdx;
333 int prestep = (x1 << 16) - left_x;
334 int z = left_z + imul16(prestep, dzdx);
337 shift = x1 + y1*rdp.zi_width;
338 //draw to depth buffer
342 for (int x = 0; x < width; x++)
345 trueZ = (int)(z*8.0f);
349 if (trueZ < 0) trueZ = 0;
350 else if (trueZ > 0x3FFFF) trueZ = 0x3FFFF;
351 encodedZ = zLUT[trueZ];
353 if(encodedZ < destptr[idx])
354 destptr[idx] = encodedZ;
359 //destptr += rdp.zi_width;
361 if (y1 >= (int)rdp.scissor_o.lr_y) return;
363 // Scan the right side
365 if(--right_height <= 0) { // End of this section?
367 if(right_vtx == max_vtx) return;
369 } while(right_height <= 0);
372 right_x += right_dxdy;
374 // Scan the left side
376 if(--left_height <= 0) { // End of this section?
378 if(left_vtx == max_vtx) return;
380 } while(left_height <= 0);