045041b062ee7d3055813b7e105fe7ebb7bcb712
[mupen64plus-pandora.git] / source / rice_gles / src / RenderBase.cpp
1 /*
2 Copyright (C) 2003 Rice1964
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17
18 */
19
20 #include <cmath>
21 #include <vector>
22
23 #include "osal_preproc.h"
24 #include "float.h"
25 #include "DeviceBuilder.h"
26 #include "VertexShaderConstantDef.h"
27 #include "Render.h"
28 #include "Timing.h"
29
30 extern FiddledVtx * g_pVtxBase;
31
32 #define ENABLE_CLIP_TRI
33 #define X_CLIP_MAX  0x1
34 #define X_CLIP_MIN  0x2
35 #define Y_CLIP_MAX  0x4
36 #define Y_CLIP_MIN  0x8
37 #define Z_CLIP_MAX  0x10
38 #define Z_CLIP_MIN  0x20
39
40 #ifdef ENABLE_CLIP_TRI
41
42 inline void RSP_Vtx_Clipping(int i)
43 {
44     g_clipFlag[i] = 0;
45     g_clipFlag2[i] = 0;
46     if( g_vecProjected[i].w > 0 )
47     {
48         /*
49         if( gRSP.bRejectVtx )
50         {
51             if( g_vecProjected[i].x > 1 )   
52             {
53                 g_clipFlag2[i] |= X_CLIP_MAX;
54                 if( g_vecProjected[i].x > gRSP.real_clip_ratio_posx )   
55                     g_clipFlag[i] |= X_CLIP_MAX;
56             }
57
58             if( g_vecProjected[i].x < -1 )  
59             {
60                 g_clipFlag2[i] |= X_CLIP_MIN;
61                 if( g_vecProjected[i].x < gRSP.real_clip_ratio_negx )   
62                     g_clipFlag[i] |= X_CLIP_MIN;
63             }
64
65             if( g_vecProjected[i].y > 1 )   
66             {
67                 g_clipFlag2[i] |= Y_CLIP_MAX;
68                 if( g_vecProjected[i].y > gRSP.real_clip_ratio_posy )   
69                     g_clipFlag[i] |= Y_CLIP_MAX;
70             }
71
72             if( g_vecProjected[i].y < -1 )  
73             {
74                 g_clipFlag2[i] |= Y_CLIP_MIN;
75                 if( g_vecProjected[i].y < gRSP.real_clip_ratio_negy )   
76                     g_clipFlag[i] |= Y_CLIP_MIN;
77             }
78
79             //if( g_vecProjected[i].z > 1.0f )  
80             //{
81             //  g_clipFlag2[i] |= Z_CLIP_MAX;
82             //  g_clipFlag[i] |= Z_CLIP_MAX;
83             //}
84
85             //if( gRSP.bNearClip && g_vecProjected[i].z < -1.0f )   
86             //{
87             //  g_clipFlag2[i] |= Z_CLIP_MIN;
88             //  g_clipFlag[i] |= Z_CLIP_MIN;
89             //}
90         }
91         else
92         */
93         {
94             if( g_vecProjected[i].x > 1 )   g_clipFlag2[i] |= X_CLIP_MAX;
95             if( g_vecProjected[i].x < -1 )  g_clipFlag2[i] |= X_CLIP_MIN;
96             if( g_vecProjected[i].y > 1 )   g_clipFlag2[i] |= Y_CLIP_MAX;
97             if( g_vecProjected[i].y < -1 )  g_clipFlag2[i] |= Y_CLIP_MIN;
98             //if( g_vecProjected[i].z > 1.0f )  g_clipFlag2[i] |= Z_CLIP_MAX;
99             //if( gRSP.bNearClip && g_vecProjected[i].z < -1.0f )   g_clipFlag2[i] |= Z_CLIP_MIN;
100         }
101
102     }
103 }
104
105 #else
106 inline void RSP_Vtx_Clipping(int i) {}
107 #endif
108
109 /*
110  *  Global variables
111  */
112 ALIGN(16,RSP_Options gRSP)
113 ALIGN(16,RDP_Options gRDP)
114
115 static ALIGN(16,XVECTOR4 g_normal)
116 //static int norms[3];
117
118 ALIGN(16,XVECTOR4 g_vtxNonTransformed[MAX_VERTS])
119 ALIGN(16,XVECTOR4 g_vecProjected[MAX_VERTS])
120 ALIGN(16,XVECTOR4 g_vtxTransformed[MAX_VERTS])
121
122 float       g_vtxProjected5[1000][5];
123 float       g_vtxProjected5Clipped[2000][5];
124
125 //uint32        g_dwVtxFlags[MAX_VERTS];            // Z_POS Z_NEG etc
126 VECTOR2     g_fVtxTxtCoords[MAX_VERTS];
127 uint32      g_dwVtxDifColor[MAX_VERTS];
128 uint32      g_clipFlag[MAX_VERTS];
129 uint32      g_clipFlag2[MAX_VERTS];
130 RenderTexture g_textures[MAX_TEXTURES];
131 float       g_fFogCoord[MAX_VERTS];
132
133 EXTERNAL_VERTEX g_vtxForExternal[MAX_VERTS];
134
135 TLITVERTEX          g_vtxBuffer[1000];
136 TLITVERTEX          g_clippedVtxBuffer[2000];
137 uint8               g_oglVtxColors[1000][4];
138 int                 g_clippedVtxCount=0;
139 TLITVERTEX          g_texRectTVtx[4];
140 unsigned short      g_vtxIndex[1000];
141 unsigned int        g_minIndex, g_maxIndex;
142
143 float               gRSPfFogMin;
144 float               gRSPfFogMax;
145 float               gRSPfFogDivider;
146
147 uint32          gRSPnumLights;
148 Light   gRSPlights[16];
149
150 ALIGN(16,Matrix  gRSPworldProjectTransported)
151 ALIGN(16,Matrix  gRSPworldProject)
152 ALIGN(16,Matrix  gRSPmodelViewTop)
153 ALIGN(16,Matrix  gRSPmodelViewTopTranspose)
154 ALIGN(16,Matrix  dkrMatrixTransposed)
155
156 N64Light        gRSPn64lights[16];
157
158
159 void (*ProcessVertexData)(uint32 dwAddr, uint32 dwV0, uint32 dwNum)=NULL;
160
161 /*
162  *  
163  */
164
165
166 /*n.x = (g_normal.x * matWorld.m00) + (g_normal.y * matWorld.m10) + (g_normal.z * matWorld.m20);
167 n.y = (g_normal.x * matWorld.m01) + (g_normal.y * matWorld.m11) + (g_normal.z * matWorld.m21);
168 n.z = (g_normal.x * matWorld.m02) + (g_normal.y * matWorld.m12) + (g_normal.z * matWorld.m22);*/
169
170 // Multiply (x,y,z,0) by matrix m, then normalize
171 #if defined(__INTEL_COMPILER) && !defined(NO_ASM)
172 #define Vec3TransformNormal(vec, m) __asm                   \
173 {                                       \
174     __asm fld   dword ptr [vec + 0]     \
175     __asm fmul  dword ptr [m + 0]       \ /* x m00*/
176     __asm fld   dword ptr [vec + 0]     \
177     __asm fmul  dword ptr [m + 4]       \ /* x m01  x m00*/
178     __asm fld   dword ptr [vec + 0]     \
179     __asm fmul  dword ptr [m + 8]       \ /* x m02  x m01  x m00*/
180                                         \
181     __asm fld   dword ptr [vec + 4]     \
182     __asm fmul  dword ptr [m + 16]      \ /* y m10  x m02  x m01  x m00*/
183     __asm fld   dword ptr [vec + 4]     \
184     __asm fmul  dword ptr [m + 20]      \ /* y m11  y m10  x m02  x m01  x m00*/
185     __asm fld   dword ptr [vec + 4]     \
186     __asm fmul  dword ptr [m + 24]      \ /* y m12  y m11  y m10  x m02  x m01  x m00*/
187                                         \
188     __asm fxch  st(2)                   \ /* y m10  y m11  y m12  x m02  x m01  x m00*/
189     __asm faddp st(5), st(0)            \ /* y m11  y m12  x m02  x m01  (x m00 + y m10)*/
190     __asm faddp st(3), st(0)            \ /* y m12  x m02  (x m01 + ym11)  (x m00 + y m10)*/
191     __asm faddp st(1), st(0)            \ /* (x m02 + y m12) (x m01 + ym11)  (x m00 + y m10)*/
192                                         \
193     __asm fld   dword ptr [vec + 8]     \
194     __asm fmul  dword ptr [m + 32]      \ /* z m20  (x m02 + y m12) (x m01 + ym11)  (x m00 + y m10)*/
195     __asm fld   dword ptr [vec + 8]     \
196     __asm fmul  dword ptr [m + 36]      \ /* z m21  z m20  (x m02 + y m12) (x m01 + ym11)  (x m00 + y m10)*/
197     __asm fld   dword ptr [vec + 8]     \
198     __asm fmul  dword ptr [m + 40]      \ /* z m22  z m21  z m20  (x m02 + y m12) (x m01 + ym11)  (x m00 + y m10)*/
199                                         \
200     __asm fxch  st(2)                   \ /* z m20  z m21  z m22  (x m02 + y m12) (x m01 + ym11)  (x m00 + y m10)*/
201     __asm faddp st(5), st(0)            \ /* z m21  z m22  (x m02 + y m12) (x m01 + ym11)  (x m00 + y m10 + z m20)*/ 
202     __asm faddp st(3), st(0)            \ /* z m22  (x m02 + y m12) (x m01 + ym11 + z m21)  (x m00 + y m10 + z m20)*/
203     __asm faddp st(1), st(0)            \ /* (x m02 + y m12 + z m 22) (x m01 + ym11 + z m21)  (x m00 + y m10 + z m20)*/
204                                         \
205     __asm fxch  st(2)                   \ /* (x m00 + y m10 + z m20) (x m01 + ym11 + z m21) (x m02 + y m12 + z m 22) */
206                                         \
207     __asm fld1                          \ /* 1 x y z */
208     __asm fld   st(1)                   \ /* x 1 x y z */
209     __asm fmul  st(0),st(0)             \ /* xx 1 x y z */
210     __asm fld   st(3)                   \ /* y xx 1 x y z */
211     __asm fmul  st(0),st(0)             \ /* yy xx 1 x y z */
212     __asm fld   st(5)                   \ /* z yy xx 1 x y z */
213     __asm fmul  st(0),st(0)             \ /* zz yy xx 1 x y z */
214                                         \
215     __asm fxch  st(2)                   \ /* xx yy zz 1 x y z */
216                                         \
217     __asm faddp st(1),st(0)             \ /* (xx+yy) zz 1 x y z */
218     __asm faddp st(1),st(0)             \ /* (xx+yy+zz) 1 x y z */
219                                         \
220     __asm ftst                          \ /* Compare ST to 0  */
221     __asm fstsw ax                      \ /* Store FPU status word in a   */
222     __asm sahf                          \ /* Transfer ax to flags register */
223     __asm jz        l2                  \ /* Skip if length is zero   */
224                                         \
225     __asm fsqrt                         \ /* l 1 x y z */
226                                         \
227     __asm fdivp st(1),st(0)             \ /* (1/l) x y z */
228                                         \
229     __asm fmul  st(3),st(0)             \ /* f x y fz */
230     __asm fmul  st(2),st(0)             \ /* f x fy fz */
231     __asm fmulp st(1),st(0)             \ /* fx fy fz */
232                                         \
233     __asm fstp  dword ptr [vec + 0]     \ /* fy fz*/
234     __asm fstp  dword ptr [vec + 4]     \ /* fz   */
235     __asm fstp  dword ptr [vec + 8]     \ /* done */
236     __asm jmp   l3                      \
237 __asm l2:                               \
238     __asm mov dword ptr [vec + 0], 0    \
239     __asm mov dword ptr [vec + 4], 0    \
240     __asm mov dword ptr [vec + 8], 0    \
241 __asm l3:                               \
242 }                                       \
243
244 //#else  // use C code in other cases, this is probably faster anyway
245 #elif defined(__ARM_NEON__0)
246 #define Vec3TransformNormal(vec, mtx) \
247 { \
248         asm volatile ( \
249         "vld1.32                {d0}, [%0]                      \n\t" \
250         "flds                   s2, [%0, #8]                    \n\t" \
251         "vld1.32                {d18, d19}, [%1]                \n\t" \
252         "vld1.32                {d20, d21}, [%1, #16]       \n\t" \
253         "vld1.32                {d22, d23}, [%1, #32]       \n\t" \
254  \
255         "vmul.f32               q2, q9, d0[0]                   \n\t" \
256         "vmla.f32               q2, q10, d0[1]                  \n\t" \
257         "vmla.f32               q2, q11, d1[0]                  \n\t" \
258  \
259     "vmul.f32           d0, d4, d4                              \n\t" \
260         "vpadd.f32              d0, d0, d0                              \n\t" \
261     "vmla.f32           d0, d5, d5                              \n\t" \
262  \
263         "vmov.f32               d1, d0                                  \n\t" \
264         "vrsqrte.f32    d0, d0                                  \n\t" \
265         "vmul.f32               d2, d0, d1                              \n\t" \
266         "vrsqrts.f32    d3, d2, d0                              \n\t" \
267         "vmul.f32               d0, d0, d3                              \n\t" \
268         "vmul.f32               d2, d0, d1                              \n\t" \
269         "vrsqrts.f32    d3, d2, d0                              \n\t" \
270         "vmul.f32               d0, d0, d3                              \n\t" \
271  \
272         "vmul.f32               q2, q2, d0[0]                   \n\t" \
273  \
274         "vst1.32                {d4}, [%0]                  \n\t" \
275         "fsts                   s10, [%0, #8]           \n\t" \
276         : :"r"(vec), "r"(&mtx._11) \
277     : "d0","d1","d2","d3","d18","d19","d20","d21","d22", "d23", "memory" \
278         ); \
279 }
280 #else
281 #define Vec3TransformNormal(vec, m) \
282    VECTOR3 temp; \
283    temp.x = (vec.x * m._11) + (vec.y * m._21) + (vec.z * m._31); \
284    temp.y = (vec.x * m._12) + (vec.y * m._22) + (vec.z * m._32); \
285    temp.z = (vec.x * m._13) + (vec.y * m._23) + (vec.z * m._33); \
286    float norm = sqrtf(temp.x*temp.x+temp.y*temp.y+temp.z*temp.z); \
287    if (norm == 0.0) { vec.x = 0.0; vec.y = 0.0; vec.z = 0.0;} else \
288    { float rep=1/norm;vec.x = temp.x*rep; vec.y = temp.y*rep; vec.z = temp.z*rep; }
289 #endif
290
291
292 #if !defined(__GNUC__) && !defined(NO_ASM)
293 __declspec( naked ) void  __fastcall SSEVec3Transform(int i)
294 {
295     __asm
296     {
297         shl     ecx,4;      // ecx = i
298
299         movaps  xmm1,   DWORD PTR g_vtxNonTransformed [ecx];        // xmm1 as original vector
300
301         movaps  xmm4,   DWORD PTR gRSPworldProjectTransported;          // row1
302         movaps  xmm5,   DWORD PTR gRSPworldProjectTransported[0x10];    // row2
303         movaps  xmm6,   DWORD PTR gRSPworldProjectTransported[0x20];    // row3
304         movaps  xmm7,   DWORD PTR gRSPworldProjectTransported[0x30];    // row4
305
306         mulps   xmm4, xmm1;     // row 1
307         mulps   xmm5, xmm1;     // row 2
308         mulps   xmm6, xmm1;     // row 3
309         mulps   xmm7, xmm1;     // row 4
310
311         movhlps xmm0, xmm4;     // xmm4 high to xmm0 low
312         movlhps xmm0, xmm5;     // xmm5 low to xmm0 high
313
314         addps   xmm4, xmm0;     // result of add are in xmm4 low
315         addps   xmm5, xmm0;     // result of add are in xmm5 high
316
317         shufps  xmm0, xmm4, 0x44;   // move xmm4 low DWORDs to xmm0 high
318         shufps  xmm4, xmm5, 0xe4;   // move xmm5 high DWORS to xmm4
319         movhlps xmm5, xmm0;         // xmm4, xmm5 are mirrored
320
321         shufps  xmm4, xmm4, 0x08;   // move xmm4's 3rd uint32 to its 2nd uint32
322         shufps  xmm5, xmm5, 0x0d;   // move xmm5's 4th uint32 to its 2nd uint32, 
323                                     // and move its 2nd uint32 to its 1st uint32
324         
325         addps   xmm4, xmm5;     // results are in 1st and 2nd uint32
326
327
328         movhlps xmm0, xmm6;     // xmm6 high to xmm0 low
329         movlhps xmm0, xmm7;     // xmm7 low to xmm0 high
330
331         addps   xmm6, xmm0;     // result of add are in xmm6 low
332         addps   xmm7, xmm0;     // result of add are in xmm7 high
333
334         shufps  xmm0, xmm6, 0x44;   // move xmm6 low DWORDs to xmm0 high
335         shufps  xmm6, xmm7, 0xe4;   // move xmm7 high DWORS to xmm6
336         movhlps xmm7, xmm0;         // xmm6, xmm7 are mirrored
337
338         shufps  xmm6, xmm6, 0x08;   // move xmm6's 3rd uint32 to its 2nd uint32
339         shufps  xmm7, xmm7, 0x0d;   // move xmm7's 4th uint32 to its 2nd uint32, 
340                                     // and move its 2nd uint32 to its 1st uint32
341         
342         addps   xmm6, xmm7;     // results are in 1st and 2nd uint32
343         
344         movlhps xmm4, xmm6;     // final result is in xmm4
345         movaps  DWORD PTR g_vtxTransformed [ecx], xmm4;
346
347         movaps  xmm0,xmm4;
348         shufps  xmm0,xmm0,0xff;
349         divps   xmm4,xmm0;
350         rcpps   xmm0,xmm0;
351         movhlps xmm0,xmm4;
352         shufps  xmm0,xmm0,0xe8;
353         movlhps xmm4,xmm0;
354
355         movaps  DWORD PTR g_vecProjected [ecx], xmm4;
356
357         emms;
358         ret;
359     }
360 }
361
362 // Only used by DKR
363 __declspec( naked ) void  __fastcall SSEVec3TransformDKR(XVECTOR4 &pOut, const XVECTOR4 &pV)
364 {
365     __asm
366     {
367         movaps  xmm1,   DWORD PTR [edx];        // xmm1 as original vector
368
369         movaps  xmm4,   DWORD PTR dkrMatrixTransposed;  // row1
370         movaps  xmm5,   DWORD PTR dkrMatrixTransposed[0x10];    // row2
371         movaps  xmm6,   DWORD PTR dkrMatrixTransposed[0x20];    // row3
372         movaps  xmm7,   DWORD PTR dkrMatrixTransposed[0x30];    // row4
373
374         mulps   xmm4, xmm1;     // row 1
375         mulps   xmm5, xmm1;     // row 2
376         mulps   xmm6, xmm1;     // row 3
377         mulps   xmm7, xmm1;     // row 4
378
379         movhlps xmm0, xmm4;     // xmm4 high to xmm0 low
380         movlhps xmm0, xmm5;     // xmm5 low to xmm0 high
381
382         addps   xmm4, xmm0;     // result of add are in xmm4 low
383         addps   xmm5, xmm0;     // result of add are in xmm5 high
384
385         shufps  xmm0, xmm4, 0x44;   // move xmm4 low DWORDs to xmm0 high
386         shufps  xmm4, xmm5, 0xe4;   // move xmm5 high DWORS to xmm4
387         movhlps xmm5, xmm0;         // xmm4, xmm5 are mirrored
388
389         shufps  xmm4, xmm4, 0x08;   // move xmm4's 3rd uint32 to its 2nd uint32
390         shufps  xmm5, xmm5, 0x0d;   // move xmm5's 4th uint32 to its 2nd uint32, 
391         // and move its 2nd uint32 to its 1st uint32
392
393         addps   xmm4, xmm5;     // results are in 1st and 2nd uint32
394
395
396         movhlps xmm0, xmm6;     // xmm6 high to xmm0 low
397         movlhps xmm0, xmm7;     // xmm7 low to xmm0 high
398
399         addps   xmm6, xmm0;     // result of add are in xmm6 low
400         addps   xmm7, xmm0;     // result of add are in xmm7 high
401
402         shufps  xmm0, xmm6, 0x44;   // move xmm6 low DWORDs to xmm0 high
403         shufps  xmm6, xmm7, 0xe4;   // move xmm7 high DWORS to xmm6
404         movhlps xmm7, xmm0;         // xmm6, xmm7 are mirrored
405
406         shufps  xmm6, xmm6, 0x08;   // move xmm6's 3rd uint32 to its 2nd uint32
407         shufps  xmm7, xmm7, 0x0d;   // move xmm7's 4th uint32 to its 2nd uint32, 
408         // and move its 2nd uint32 to its 1st uint32
409
410         addps   xmm6, xmm7;     // results are in 1st and 2nd uint32
411
412         movlhps xmm4, xmm6;     // final result is in xmm4
413         movaps  DWORD PTR [ecx], xmm4;
414
415         emms;
416         ret;
417     }
418 }
419 #elif defined(__GNUC__) && defined(__x86_64__) && !defined(NO_ASM)
420 void SSEVec3Transform(int i)
421 {
422   asm volatile(" shl               $4,      %0   \n"
423                " movslq           %k0,     %q0   \n"
424                " movaps      (%1,%q0),  %%xmm1   \n"
425                " movaps         0(%2),  %%xmm4   \n"
426                " movaps        16(%2),  %%xmm5   \n"
427                " movaps        32(%2),  %%xmm6   \n"
428                " movaps        48(%2),  %%xmm7   \n"
429                " mulps         %%xmm1,  %%xmm4   \n"
430                " mulps         %%xmm1,  %%xmm5   \n"
431                " mulps         %%xmm1,  %%xmm6   \n"
432                " mulps         %%xmm1,  %%xmm7   \n"
433                " movhlps       %%xmm4,  %%xmm0   \n"
434                " movlhps       %%xmm5,  %%xmm0   \n"
435                " addps         %%xmm0,  %%xmm4   \n"
436                " addps         %%xmm0,  %%xmm5   \n"
437                " shufps $0x44, %%xmm4,  %%xmm0   \n"
438                " shufps $0xe4, %%xmm5,  %%xmm4   \n"
439                " movhlps       %%xmm0,  %%xmm5   \n"
440                " shufps $0x08, %%xmm4,  %%xmm4   \n"
441                " shufps $0x0d, %%xmm5,  %%xmm5   \n"
442                " addps         %%xmm5,  %%xmm4   \n"
443                " movhlps       %%xmm6,  %%xmm0   \n"
444                " movlhps       %%xmm7,  %%xmm0   \n"
445                " addps         %%xmm0,  %%xmm6   \n"
446                " addps         %%xmm0,  %%xmm7   \n"
447                " shufps $0x44, %%xmm6,  %%xmm0   \n"
448                " shufps $0xe4, %%xmm7,  %%xmm6   \n"
449                " movhlps       %%xmm0,  %%xmm7   \n"
450                " shufps $0x08, %%xmm6,  %%xmm6   \n"
451                " shufps $0x0d, %%xmm7,  %%xmm7   \n"
452                " addps         %%xmm7,  %%xmm6   \n"
453                " movlhps       %%xmm6,  %%xmm4   \n"
454                " movaps        %%xmm4, (%3,%q0)  \n"
455                " movaps        %%xmm4,  %%xmm0   \n"
456                " shufps $0xff, %%xmm0,  %%xmm0   \n"
457                " divps         %%xmm0,  %%xmm4   \n"
458                " rcpps         %%xmm0,  %%xmm0   \n"
459                " movhlps       %%xmm4,  %%xmm0   \n"
460                " shufps $0xe8, %%xmm0,  %%xmm0   \n"
461                " movlhps       %%xmm0,  %%xmm4   \n"
462                " movaps        %%xmm4, (%4,%q0)  \n"
463                : "+r"(i)
464                : "r"(g_vtxNonTransformed), "r"(&gRSPworldProjectTransported.m[0][0]), "r"(g_vtxTransformed), "r"(g_vecProjected)
465                : "memory", "%xmm0", "%xmm1", "%xmm4", "%xmm5", "%xmm6", "%xmm7"
466                );
467 }
468 #elif !defined(NO_ASM) // 32-bit GCC assumed
469 void SSEVec3Transform(int i)
470 {
471   asm volatile(" shl               $4,      %0   \n"
472                " movaps       (%1,%0),  %%xmm1   \n"
473                " movaps         0(%2),  %%xmm4   \n"
474                " movaps        16(%2),  %%xmm5   \n"
475                " movaps        32(%2),  %%xmm6   \n"
476                " movaps        48(%2),  %%xmm7   \n"
477                " mulps         %%xmm1,  %%xmm4   \n"
478                " mulps         %%xmm1,  %%xmm5   \n"
479                " mulps         %%xmm1,  %%xmm6   \n"
480                " mulps         %%xmm1,  %%xmm7   \n"
481                " movhlps       %%xmm4,  %%xmm0   \n"
482                " movlhps       %%xmm5,  %%xmm0   \n"
483                " addps         %%xmm0,  %%xmm4   \n"
484                " addps         %%xmm0,  %%xmm5   \n"
485                " shufps $0x44, %%xmm4,  %%xmm0   \n"
486                " shufps $0xe4, %%xmm5,  %%xmm4   \n"
487                " movhlps       %%xmm0,  %%xmm5   \n"
488                " shufps $0x08, %%xmm4,  %%xmm4   \n"
489                " shufps $0x0d, %%xmm5,  %%xmm5   \n"
490                " addps         %%xmm5,  %%xmm4   \n"
491                " movhlps       %%xmm6,  %%xmm0   \n"
492                " movlhps       %%xmm7,  %%xmm0   \n"
493                " addps         %%xmm0,  %%xmm6   \n"
494                " addps         %%xmm0,  %%xmm7   \n"
495                " shufps $0x44, %%xmm6,  %%xmm0   \n"
496                " shufps $0xe4, %%xmm7,  %%xmm6   \n"
497                " movhlps       %%xmm0,  %%xmm7   \n"
498                " shufps $0x08, %%xmm6,  %%xmm6   \n"
499                " shufps $0x0d, %%xmm7,  %%xmm7   \n"
500                " addps         %%xmm7,  %%xmm6   \n"
501                " movlhps       %%xmm6,  %%xmm4   \n"
502                " movaps        %%xmm4,  (%3,%0)  \n"
503                " movaps        %%xmm4,  %%xmm0   \n"
504                " shufps $0xff, %%xmm0,  %%xmm0   \n"
505                " divps         %%xmm0,  %%xmm4   \n"
506                " rcpps         %%xmm0,  %%xmm0   \n"
507                " movhlps       %%xmm4,  %%xmm0   \n"
508                " shufps $0xe8, %%xmm0,  %%xmm0   \n"
509                " movlhps       %%xmm0,  %%xmm4   \n"
510                " movaps        %%xmm4,  (%4,%0)  \n"
511                : "+r"(i)
512                : "r"(g_vtxNonTransformed), "r"(&gRSPworldProjectTransported.m[0][0]), "r"(g_vtxTransformed), "r"(g_vecProjected)
513                : "memory", "%xmm0", "%xmm1", "%xmm4", "%xmm5", "%xmm6", "%xmm7"
514                );
515 }
516 #endif
517 float real255 = 255.0f;
518 float real128 = 128.0f;
519
520 #if !defined(__GNUC__) && !defined(NO_ASM)
521 __declspec( naked ) void  __fastcall SSEVec3TransformNormal()
522 {
523     __asm
524     {
525         mov     DWORD PTR [g_normal][12], 0;
526
527         movaps  xmm4,   DWORD PTR gRSPmodelViewTopTranspose;    // row1
528         movaps  xmm5,   DWORD PTR gRSPmodelViewTopTranspose[0x10];  // row2
529         movaps  xmm1,   DWORD PTR [g_normal];       // xmm1 as the normal vector
530         movaps  xmm6,   DWORD PTR gRSPmodelViewTopTranspose[0x20];  // row3
531
532         mulps   xmm4, xmm1;     // row 1
533         mulps   xmm5, xmm1;     // row 2
534         mulps   xmm6, xmm1;     // row 3
535
536         movhlps xmm0, xmm4;     // xmm4 high to xmm0 low
537         movlhps xmm0, xmm5;     // xmm5 low to xmm0 high
538
539         addps   xmm4, xmm0;     // result of add are in xmm4 low
540         addps   xmm5, xmm0;     // result of add are in xmm5 high
541
542         shufps  xmm0, xmm4, 0x44;   // move xmm4 low DWORDs to xmm0 high
543         shufps  xmm4, xmm5, 0xe4;   // move xmm5 high DWORS to xmm4
544         movhlps xmm5, xmm0;         // xmm4, xmm5 are mirrored
545
546         shufps  xmm4, xmm4, 0x08;   // move xmm4's 3rd uint32 to its 2nd uint32
547         shufps  xmm5, xmm5, 0x0d;   // move xmm5's 4th uint32 to its 2nd uint32, 
548
549         addps   xmm4, xmm5;     // results are in 1st and 2nd uint32
550
551         movaps  xmm1,xmm4;
552         mulps   xmm1,xmm1;  //square
553         movlhps xmm7, xmm1;
554         shufps  xmm7, xmm7,0x03;
555         addss   xmm7, xmm1;
556
557         movhlps xmm0, xmm6;     // xmm6 high to xmm0 low
558         addps   xmm6, xmm0;     // result of add are in xmm6 low
559
560         movlhps xmm0, xmm6;
561         shufps  xmm0, xmm0, 0x03;
562         addss   xmm0, xmm6;     // result of add is at xmm0's 1st uint32
563
564         movlhps xmm4, xmm0;
565
566         mulss   xmm0,xmm0;
567         addss   xmm7,xmm0;      // xmm7 1st uint32 is the sum of squares
568
569 #ifdef DEBUGGER
570         movaps  DWORD PTR [g_normal], xmm4;
571         movss  DWORD PTR [g_normal][12], xmm7;
572 #endif
573         xorps   xmm0,xmm0;
574         ucomiss xmm0,xmm7;
575         jz      l2
576
577         rsqrtss xmm7,xmm7;
578         shufps  xmm7,xmm7,0;
579 #ifdef DEBUGGER
580         movss  DWORD PTR [g_normal][12], xmm7;
581 #endif
582         mulps   xmm4,xmm7;
583
584         movaps  DWORD PTR [g_normal], xmm4;     // Normalized
585         mov     DWORD PTR [g_normal][12], 0;
586
587         emms;
588         ret;
589 l2:
590         movss   DWORD PTR [g_normal], xmm0;
591         movss   DWORD PTR [g_normal][12], xmm0;
592         emms;
593         ret;
594     }
595 }
596 #elif defined(__GNUC__) && !defined(NO_ASM)  // this code should compile for both 64-bit and 32-bit architectures
597 void SSEVec3TransformNormal(void)
598 {
599   asm volatile(" movl              $0,  12(%0)    \n"
600            " movaps          (%1),  %%xmm4    \n"
601            " movaps        16(%1),  %%xmm5    \n"
602            " movaps          (%0),  %%xmm1    \n"
603            " movaps        32(%1),  %%xmm6    \n"
604            " mulps         %%xmm1,  %%xmm4    \n"
605            " mulps         %%xmm1,  %%xmm5    \n"
606            " mulps         %%xmm1,  %%xmm6    \n"
607            " movhlps       %%xmm4,  %%xmm0    \n"
608            " movlhps       %%xmm5,  %%xmm0    \n"
609            " addps         %%xmm0,  %%xmm4    \n"
610            " addps         %%xmm0,  %%xmm5    \n"
611            " shufps $0x44, %%xmm4,  %%xmm0    \n"
612            " shufps $0xe4, %%xmm5,  %%xmm4    \n"
613            " movhlps       %%xmm0,  %%xmm5    \n"
614            " shufps $0x08, %%xmm4,  %%xmm4    \n"
615            " shufps $0x0d, %%xmm5,  %%xmm5    \n"
616            " addps         %%xmm5,  %%xmm4    \n"
617            " movaps        %%xmm4,  %%xmm1    \n"
618            " mulps         %%xmm1,  %%xmm1    \n"
619            " movlhps       %%xmm1,  %%xmm7    \n"
620            " shufps $0x03, %%xmm7,  %%xmm7    \n"
621            " addss         %%xmm1,  %%xmm7    \n"
622            " movhlps       %%xmm6,  %%xmm0    \n"
623            " addps         %%xmm0,  %%xmm6    \n"
624            " movlhps       %%xmm6,  %%xmm0    \n"
625            " shufps $0x03, %%xmm0,  %%xmm0    \n"
626            " addss         %%xmm6,  %%xmm0    \n"
627            " movlhps       %%xmm0,  %%xmm4    \n"
628            " mulss         %%xmm0,  %%xmm0    \n"
629            " addss         %%xmm0,  %%xmm7    \n"
630 #ifdef DEBUGGER
631            " movaps        %%xmm4,    (%0)    \n"
632            " movss         %%xmm7,  12(%0)    \n"
633 #endif
634            " xorps         %%xmm0,  %%xmm0    \n"
635            " ucomiss       %%xmm7,  %%xmm0    \n"
636            " jz                0f             \n"
637            " rsqrtss       %%xmm7,  %%xmm7    \n"
638            " shufps $0x00, %%xmm7,  %%xmm7    \n"
639 #ifdef DEBUGGER
640            " movss         %%xmm7,  12(%0)    \n"
641 #endif
642                " mulps         %%xmm7,  %%xmm4    \n"
643                " movaps        %%xmm4,    (%0)    \n"
644                " movl              $0,  12(%0)    \n"
645                " jmp               1f             \n"
646                "0:                                \n"
647                " movss         %%xmm0,    (%0)    \n"
648                " movss         %%xmm0,  12(%0)    \n"
649                "1:                                \n"
650                :
651                : "r"(&g_normal.x), "r"(&gRSPmodelViewTopTranspose.m[0][0])
652                : "memory", "cc", "%xmm0", "%xmm1", "%xmm4", "%xmm5", "%xmm6", "%xmm7"
653                );
654 }
655 #endif
656
657 void NormalizeNormalVec()
658 #ifdef __ARM_NEON__0
659 {
660         asm volatile (
661         "vld1.32                {d4}, [%0]                      \n\t"   //d4={x,y}
662         "flds                   s10, [%0, #8]                   \n\t"   //d5[0] = z
663 //      "sub                    %0, %0, #8              \n\t"   //d5[0] = z
664         "vmul.f32               d0, d4, d4                              \n\t"   //d0= d4*d4
665         "vpadd.f32              d0, d0, d0                              \n\t"   //d0 = d[0] + d[1]
666     "vmla.f32           d0, d5, d5                              \n\t"   //d0 = d0 + d5*d5
667
668         "vmov.f32               d1, d0                                  \n\t"   //d1 = d0
669         "vrsqrte.f32    d0, d0                                  \n\t"   //d0 = ~ 1.0 / sqrt(d0)
670         "vmul.f32               d2, d0, d1                              \n\t"   //d2 = d0 * d1
671         "vrsqrts.f32    d3, d2, d0                              \n\t"   //d3 = (3 - d0 * d2) / 2
672         "vmul.f32               d0, d0, d3                              \n\t"   //d0 = d0 * d3
673         "vmul.f32               d2, d0, d1                              \n\t"   //d2 = d0 * d1
674         "vrsqrts.f32    d3, d2, d0                              \n\t"   //d3 = (3 - d0 * d3) / 2
675         "vmul.f32               d0, d0, d3                              \n\t"   //d0 = d0 * d4
676
677         "vmul.f32               q2, q2, d0[0]                   \n\t"   //d0= d2*d4
678         "vst1.32                {d4}, [%0]                      \n\t"   //d2={x0,y0}, d3={z0, w0}
679         "fsts                   s10, [%0, #8]                           \n\t"   //d2={x0,y0}, d3={z0, w0}
680
681         ::"r"(g_normal)
682     : "d0", "d1", "d2", "d3", "d4", "d5", "memory"
683         );
684 }
685 #else
686 {
687     float w = 1/sqrtf(g_normal.x*g_normal.x + g_normal.y*g_normal.y + g_normal.z*g_normal.z);
688     g_normal.x *= w;
689     g_normal.y *= w;
690     g_normal.z *= w;
691 }
692 #endif
693
694 void InitRenderBase()
695 {
696 #if !defined(NO_ASM)
697     if( status.isSSEEnabled && !g_curRomInfo.bPrimaryDepthHack && options.enableHackForGames != HACK_FOR_NASCAR)
698     {
699         ProcessVertexData = ProcessVertexDataSSE;
700     }
701     else
702 #elif defined(__ARM_NEON__)
703     if( !g_curRomInfo.bPrimaryDepthHack && options.enableHackForGames != HACK_FOR_NASCAR && options.enableHackForGames != HACK_FOR_ZELDA_MM && !options.bWinFrameMode)
704     {
705         ProcessVertexData = ProcessVertexDataNEON;
706     }
707     else
708 #endif
709     {
710         ProcessVertexData = ProcessVertexDataNoSSE;
711     }
712
713     gRSPfFogMin = gRSPfFogMax = 0.0f;
714     windowSetting.fMultX = windowSetting.fMultY = 2.0f;
715     windowSetting.vpLeftW = windowSetting.vpTopW = 0;
716     windowSetting.vpRightW = windowSetting.vpWidthW = 640;
717     windowSetting.vpBottomW = windowSetting.vpHeightW = 480;
718     gRSP.maxZ = 0;
719     gRSP.nVPLeftN = gRSP.nVPTopN = 0;
720     gRSP.nVPRightN = 640;
721     gRSP.nVPBottomN = 640;
722     gRSP.nVPWidthN = 640;
723     gRSP.nVPHeightN = 640;
724     gRDP.scissor.left=gRDP.scissor.top=0;
725     gRDP.scissor.right=gRDP.scissor.bottom=640;
726     
727     gRSP.bLightingEnable = gRSP.bTextureGen = false;
728     gRSP.curTile=gRSPnumLights=gRSP.ambientLightColor=gRSP.ambientLightIndex= 0;
729     gRSP.fAmbientLightR=gRSP.fAmbientLightG=gRSP.fAmbientLightB=0;
730     gRSP.projectionMtxTop = gRSP.modelViewMtxTop = 0;
731     gRDP.fogColor = gRDP.primitiveColor = gRDP.envColor = gRDP.primitiveDepth = gRDP.primLODMin = gRDP.primLODFrac = gRDP.LODFrac = 0;
732     gRDP.fPrimitiveDepth = 0;
733     gRSP.numVertices = 0;
734     gRSP.maxVertexID = 0;
735     gRSP.bCullFront=false;
736     gRSP.bCullBack=true;
737     gRSP.bFogEnabled=gRDP.bFogEnableInBlender=false;
738     gRSP.bZBufferEnabled=true;
739     gRSP.shadeMode=SHADE_SMOOTH;
740     gRDP.keyR=gRDP.keyG=gRDP.keyB=gRDP.keyA=gRDP.keyRGB=gRDP.keyRGBA = 0;
741     gRDP.fKeyA = 0;
742     gRSP.DKRCMatrixIndex = gRSP.dwDKRVtxAddr = gRSP.dwDKRMatrixAddr = 0;
743     gRSP.DKRBillBoard = false;
744
745     gRSP.fTexScaleX = 1/32.0f;
746     gRSP.fTexScaleY = 1/32.0f;
747     gRSP.bTextureEnabled = FALSE;
748
749     gRSP.clip_ratio_left = 0;
750     gRSP.clip_ratio_top = 0;
751     gRSP.clip_ratio_right = 640;
752     gRSP.clip_ratio_bottom = 480;
753     gRSP.clip_ratio_negx = 1;
754     gRSP.clip_ratio_negy = 1;
755     gRSP.clip_ratio_posx = 1;
756     gRSP.clip_ratio_posy = 1;
757     gRSP.real_clip_scissor_left = 0;
758     gRSP.real_clip_scissor_top = 0;
759     gRSP.real_clip_scissor_right = 640;
760     gRSP.real_clip_scissor_bottom = 480;
761     windowSetting.clipping.left = 0;
762     windowSetting.clipping.top = 0;
763     windowSetting.clipping.right = 640;
764     windowSetting.clipping.bottom = 480;
765     windowSetting.clipping.width = 640;
766     windowSetting.clipping.height = 480;
767     windowSetting.clipping.needToClip = false;
768     gRSP.real_clip_ratio_negx = 1;
769     gRSP.real_clip_ratio_negy = 1;
770     gRSP.real_clip_ratio_posx = 1;
771     gRSP.real_clip_ratio_posy = 1;
772
773     gRSP.DKRCMatrixIndex=0;
774     gRSP.DKRVtxCount=0;
775     gRSP.DKRBillBoard = false;
776     gRSP.dwDKRVtxAddr=0;
777     gRSP.dwDKRMatrixAddr=0;
778
779
780     gRDP.geometryMode   = 0;
781     gRDP.otherModeL     = 0;
782     gRDP.otherModeH     = 0;
783     gRDP.fillColor      = 0xFFFFFFFF;
784     gRDP.originalFillColor  =0;
785
786     gRSP.ucode      = 1;
787     gRSP.vertexMult = 10;
788     gRSP.bNearClip  = false;
789     gRSP.bRejectVtx = false;
790
791     gRDP.texturesAreReloaded = false;
792     gRDP.textureIsChanged = false;
793     gRDP.colorsAreReloaded = false;
794
795     memset(&gRDP.otherMode,0,sizeof(RDP_OtherMode));
796     memset(&gRDP.tiles,0,sizeof(Tile)*8);
797
798     for( int i=0; i<MAX_VERTS; i++ )
799     {
800         g_clipFlag[i] = 0;
801         g_vtxNonTransformed[i].w = 1;
802     }
803
804     memset(gRSPn64lights, 0, sizeof(N64Light)*16);
805 }
806
807 void SetFogMinMax(float fMin, float fMax, float fMul, float fOffset)
808 {
809     if( fMin > fMax )
810     {
811         float temp = fMin;
812         fMin = fMax;
813         fMax = temp;
814     }
815
816     {
817         gRSPfFogMin = max(0,fMin/500-1);
818         gRSPfFogMax = fMax/500-1;
819     }
820
821     gRSPfFogDivider = 255/(gRSPfFogMax-gRSPfFogMin);
822     CRender::g_pRender->SetFogMinMax(fMin, fMax);
823 }
824
825 void InitVertexColors()
826 {
827 }
828
829 void InitVertexTextureConstants()
830 {
831     float scaleX;
832     float scaleY;
833
834     RenderTexture &tex0 = g_textures[gRSP.curTile];
835     //CTexture *surf = tex0.m_pCTexture;
836     Tile &tile0 = gRDP.tiles[gRSP.curTile];
837
838     scaleX = gRSP.fTexScaleX;
839     scaleY = gRSP.fTexScaleY;
840
841     gRSP.tex0scaleX = scaleX * tile0.fShiftScaleS/tex0.m_fTexWidth;
842     gRSP.tex0scaleY = scaleY * tile0.fShiftScaleT/tex0.m_fTexHeight;
843
844     gRSP.tex0OffsetX = tile0.fhilite_sl/tex0.m_fTexWidth;
845     gRSP.tex0OffsetY = tile0.fhilite_tl/tex0.m_fTexHeight;
846
847     if( CRender::g_pRender->IsTexel1Enable() )
848     {
849         RenderTexture &tex1 = g_textures[(gRSP.curTile+1)&7];
850         //CTexture *surf = tex1.m_pCTexture;
851         Tile &tile1 = gRDP.tiles[(gRSP.curTile+1)&7];
852
853         gRSP.tex1scaleX = scaleX * tile1.fShiftScaleS/tex1.m_fTexWidth;
854         gRSP.tex1scaleY = scaleY * tile1.fShiftScaleT/tex1.m_fTexHeight;
855
856         gRSP.tex1OffsetX = tile1.fhilite_sl/tex1.m_fTexWidth;
857         gRSP.tex1OffsetY = tile1.fhilite_tl/tex1.m_fTexHeight;
858     }
859
860     gRSP.texGenXRatio = tile0.fShiftScaleS;
861     gRSP.texGenYRatio = gRSP.fTexScaleX/gRSP.fTexScaleY*tex0.m_fTexWidth/tex0.m_fTexHeight*tile0.fShiftScaleT;
862 }
863
864 void TexGen(float &s, float &t)
865 {
866     if (gRDP.geometryMode & G_TEXTURE_GEN_LINEAR)
867     {   
868         s = acosf(g_normal.x) / 3.14159f;
869         t = acosf(g_normal.y) / 3.14159f;
870     }
871     else
872     {
873         s = 0.5f * ( 1.0f + g_normal.x);
874         t = 0.5f * ( 1.0f - g_normal.y);
875     }
876 }
877
878 void ComputeLOD(bool openGL)
879 {
880     TLITVERTEX &v0 = g_vtxBuffer[0];
881     TLITVERTEX &v1 = g_vtxBuffer[1];
882     RenderTexture &tex0 = g_textures[gRSP.curTile];
883
884     float d,dt;
885     if( openGL )
886     {
887         float x = g_vtxProjected5[0][0] / g_vtxProjected5[0][4] - g_vtxProjected5[1][0] / g_vtxProjected5[1][4];
888         float y = g_vtxProjected5[0][1] / g_vtxProjected5[0][4] - g_vtxProjected5[1][1] / g_vtxProjected5[1][4];
889
890         x = windowSetting.vpWidthW*x/windowSetting.fMultX/2;
891         y = windowSetting.vpHeightW*y/windowSetting.fMultY/2;
892         d = sqrtf(x*x+y*y);
893     }
894     else
895     {
896         float x = (v0.x - v1.x)/ windowSetting.fMultX;
897         float y = (v0.y - v1.y)/ windowSetting.fMultY;
898         d = sqrtf(x*x+y*y);
899     }
900
901     float s0 = v0.tcord[0].u * tex0.m_fTexWidth;
902     float t0 = v0.tcord[0].v * tex0.m_fTexHeight;
903     float s1 = v1.tcord[0].u * tex0.m_fTexWidth;
904     float t1 = v1.tcord[0].v * tex0.m_fTexHeight;
905
906     dt = sqrtf((s0-s1)*(s0-s1)+(t0-t1)*(t0-t1));
907
908     float lod = dt/d;
909     float frac = log10f(lod)/log10f(2.0f);
910     //DEBUGGER_IF_DUMP(pauseAtNext,{DebuggerAppendMsg("LOD frac = %f", frac);});
911     frac = (lod / powf(2.0f,floorf(frac)));
912     frac = frac - floorf(frac);
913     //DEBUGGER_IF_DUMP(pauseAtNext,{DebuggerAppendMsg("LOD = %f, frac = %f", lod, frac);});
914     gRDP.LODFrac = (uint32)(frac*255);
915     CRender::g_pRender->SetCombinerAndBlender();
916 }
917
918 bool bHalfTxtScale=false;
919 extern uint32 lastSetTile;
920
921 void InitVertex(uint32 dwV, uint32 vtxIndex, bool bTexture, bool openGL)
922 {
923     VTX_DUMP(TRACE2("Init vertex (%d) to vtx buf[%d]:", dwV, vtxIndex));
924
925     TLITVERTEX &v = g_vtxBuffer[vtxIndex];
926     VTX_DUMP(TRACE4("  Trans: x=%f, y=%f, z=%f, w=%f",  g_vtxTransformed[dwV].x,g_vtxTransformed[dwV].y,g_vtxTransformed[dwV].z,g_vtxTransformed[dwV].w));
927     if( openGL )
928     {
929         g_vtxProjected5[vtxIndex][0] = g_vtxTransformed[dwV].x;
930         g_vtxProjected5[vtxIndex][1] = g_vtxTransformed[dwV].y;
931         g_vtxProjected5[vtxIndex][2] = g_vtxTransformed[dwV].z;
932         g_vtxProjected5[vtxIndex][3] = g_vtxTransformed[dwV].w;
933         g_vtxProjected5[vtxIndex][4] = g_vecProjected[dwV].z;
934
935         if( g_vtxTransformed[dwV].w < 0 )
936             g_vtxProjected5[vtxIndex][4] = 0;
937
938         g_vtxIndex[vtxIndex] = vtxIndex;
939     }
940
941     if( !openGL || options.bOGLVertexClipper == TRUE )
942     {
943         v.x = g_vecProjected[dwV].x*gRSP.vtxXMul+gRSP.vtxXAdd;
944         v.y = g_vecProjected[dwV].y*gRSP.vtxYMul+gRSP.vtxYAdd;
945         v.z = (g_vecProjected[dwV].z + 1.0f) * 0.5f;    // DirectX minZ=0, maxZ=1
946         //v.z = g_vecProjected[dwV].z;  // DirectX minZ=0, maxZ=1
947         v.rhw = g_vecProjected[dwV].w;
948         VTX_DUMP(TRACE4("  Proj : x=%f, y=%f, z=%f, rhw=%f",  v.x,v.y,v.z,v.rhw));
949
950         if( gRSP.bProcessSpecularColor )
951         {
952             v.dcSpecular = CRender::g_pRender->PostProcessSpecularColor();
953             if( gRSP.bFogEnabled )
954             {
955                 v.dcSpecular &= 0x00FFFFFF;
956                 uint32  fogFct = 0xFF-(uint8)((g_fFogCoord[dwV]-gRSPfFogMin)*gRSPfFogDivider);
957                 v.dcSpecular |= (fogFct<<24);
958             }
959         }
960         else if( gRSP.bFogEnabled )
961         {
962             uint32  fogFct = 0xFF-(uint8)((g_fFogCoord[dwV]-gRSPfFogMin)*gRSPfFogDivider);
963             v.dcSpecular = (fogFct<<24);
964         }
965     }
966     VTX_DUMP(TRACE2("  (U,V): %f, %f",  g_fVtxTxtCoords[dwV].x,g_fVtxTxtCoords[dwV].y));
967
968     v.dcDiffuse = g_dwVtxDifColor[dwV];
969     if( gRDP.otherMode.key_en )
970     {
971         v.dcDiffuse &= 0x00FFFFFF;
972         v.dcDiffuse |= (gRDP.keyA<<24);
973     }
974     else if( gRDP.otherMode.aa_en && gRDP.otherMode.clr_on_cvg==0 )
975     {
976         v.dcDiffuse |= 0xFF000000;
977     }
978
979     if( gRSP.bProcessDiffuseColor )
980     {
981         v.dcDiffuse = CRender::g_pRender->PostProcessDiffuseColor(v.dcDiffuse);
982     }
983     if( options.bWinFrameMode )
984     {
985         v.dcDiffuse = g_dwVtxDifColor[dwV];
986     }
987
988     if( openGL )
989     {
990         g_oglVtxColors[vtxIndex][0] = v.r;
991         g_oglVtxColors[vtxIndex][1] = v.g;
992         g_oglVtxColors[vtxIndex][2] = v.b;
993         g_oglVtxColors[vtxIndex][3] = v.a;
994     }
995
996     if( bTexture )
997     {
998         // If the vert is already lit, then there is no normal (and hence we can't generate tex coord)
999         // Only scale if not generated automatically
1000         if (gRSP.bTextureGen && gRSP.bLightingEnable)
1001         {
1002             // Correction for texGen result
1003             float u0,u1,v0,v1;
1004             RenderTexture &tex0 = g_textures[gRSP.curTile];
1005             u0 = g_fVtxTxtCoords[dwV].x * 32 * 1024 * gRSP.fTexScaleX / tex0.m_fTexWidth;
1006             v0 = g_fVtxTxtCoords[dwV].y * 32 * 1024 * gRSP.fTexScaleY / tex0.m_fTexHeight;
1007             u0 *= (gRDP.tiles[gRSP.curTile].fShiftScaleS);
1008             v0 *= (gRDP.tiles[gRSP.curTile].fShiftScaleT);
1009
1010             if( CRender::g_pRender->IsTexel1Enable() )
1011             {
1012                 RenderTexture &tex1 = g_textures[(gRSP.curTile+1)&7];
1013                 u1 = g_fVtxTxtCoords[dwV].x * 32 * 1024 * gRSP.fTexScaleX / tex1.m_fTexWidth;
1014                 v1 = g_fVtxTxtCoords[dwV].y * 32 * 1024 * gRSP.fTexScaleY / tex1.m_fTexHeight;
1015                 u1 *= gRDP.tiles[(gRSP.curTile+1)&7].fShiftScaleS;
1016                 v1 *= gRDP.tiles[(gRSP.curTile+1)&7].fShiftScaleT;
1017                 CRender::g_pRender->SetVertexTextureUVCoord(v, u0, v0, u1, v1);
1018             }
1019             else
1020             {
1021                 CRender::g_pRender->SetVertexTextureUVCoord(v, u0, v0);
1022             }
1023         }
1024         else
1025         {
1026             float tex0u = g_fVtxTxtCoords[dwV].x *gRSP.tex0scaleX - gRSP.tex0OffsetX ;
1027             float tex0v = g_fVtxTxtCoords[dwV].y *gRSP.tex0scaleY - gRSP.tex0OffsetY ;
1028
1029             if( CRender::g_pRender->IsTexel1Enable() )
1030             {
1031                 float tex1u = g_fVtxTxtCoords[dwV].x *gRSP.tex1scaleX - gRSP.tex1OffsetX ;
1032                 float tex1v = g_fVtxTxtCoords[dwV].y *gRSP.tex1scaleY - gRSP.tex1OffsetY ;
1033
1034                 CRender::g_pRender->SetVertexTextureUVCoord(v, tex0u, tex0v, tex1u, tex1v);
1035                 VTX_DUMP(TRACE2("  (tex0): %f, %f",  tex0u,tex0v));
1036                 VTX_DUMP(TRACE2("  (tex1): %f, %f",  tex1u,tex1v));
1037             }
1038             else
1039             {
1040                 CRender::g_pRender->SetVertexTextureUVCoord(v, tex0u, tex0v);
1041                 VTX_DUMP(TRACE2("  (tex0): %f, %f",  tex0u,tex0v));
1042             }
1043         }
1044
1045         // Check for txt scale hack
1046         if( !bHalfTxtScale && g_curRomInfo.bTextureScaleHack &&
1047             (gRDP.tiles[lastSetTile].dwSize == TXT_SIZE_32b || gRDP.tiles[lastSetTile].dwSize == TXT_SIZE_4b ) )
1048         {
1049             int width = ((gRDP.tiles[lastSetTile].sh-gRDP.tiles[lastSetTile].sl+1)<<1);
1050             int height = ((gRDP.tiles[lastSetTile].th-gRDP.tiles[lastSetTile].tl+1)<<1);
1051             if( g_fVtxTxtCoords[dwV].x*gRSP.fTexScaleX == width || g_fVtxTxtCoords[dwV].y*gRSP.fTexScaleY == height )
1052             {
1053                 bHalfTxtScale=true;
1054             }
1055         }
1056     }
1057
1058     if( g_curRomInfo.bEnableTxtLOD && vtxIndex == 1 && gRDP.otherMode.text_lod )
1059     {
1060         if( CRender::g_pRender->IsTexel1Enable() && CRender::g_pRender->m_pColorCombiner->m_pDecodedMux->isUsed(MUX_LODFRAC) )
1061         {
1062             ComputeLOD(openGL);
1063         }
1064         else
1065         {
1066             gRDP.LODFrac = 0;
1067         }
1068     }
1069
1070     VTX_DUMP(TRACE2("  DIF(%08X), SPE(%08X)",   v.dcDiffuse, v.dcSpecular));
1071     VTX_DUMP(TRACE0(""));
1072 }
1073
1074 uint32 LightVert(XVECTOR4 & norm, int vidx)
1075 {
1076     float fCosT;
1077
1078     // Do ambient
1079     register float r = gRSP.fAmbientLightR;
1080     register float g = gRSP.fAmbientLightG;
1081     register float b = gRSP.fAmbientLightB;
1082
1083     if( options.enableHackForGames != HACK_FOR_ZELDA_MM )
1084     {
1085         for (register unsigned int l=0; l < gRSPnumLights; l++)
1086         {
1087             fCosT = norm.x*gRSPlights[l].x + norm.y*gRSPlights[l].y + norm.z*gRSPlights[l].z; 
1088
1089             if (fCosT > 0 )
1090             {
1091                 r += gRSPlights[l].fr * fCosT;
1092                 g += gRSPlights[l].fg * fCosT;
1093                 b += gRSPlights[l].fb * fCosT;
1094             }
1095         }
1096     }
1097     else
1098     {
1099         XVECTOR4 v;
1100         bool transformed = false;
1101
1102         for (register unsigned int l=0; l < gRSPnumLights; l++)
1103         {
1104             if( gRSPlights[l].range == 0 )
1105             {
1106                 // Regular directional light
1107                 fCosT = norm.x*gRSPlights[l].x + norm.y*gRSPlights[l].y + norm.z*gRSPlights[l].z; 
1108
1109                 if (fCosT > 0 )
1110                 {
1111                     r += gRSPlights[l].fr * fCosT;
1112                     g += gRSPlights[l].fg * fCosT;
1113                     b += gRSPlights[l].fb * fCosT;
1114                 }
1115             }
1116             else //if( (gRSPlights[l].col&0x00FFFFFF) != 0x00FFFFFF )
1117             {
1118                 // Point light
1119                 if( !transformed )
1120                 {
1121                     Vec3Transform(&v, (XVECTOR3*)&g_vtxNonTransformed[vidx], &gRSPmodelViewTop);    // Convert to w=1
1122                     transformed = true;
1123                 }
1124
1125                 XVECTOR3 dir(gRSPlights[l].x - v.x, gRSPlights[l].y - v.y, gRSPlights[l].z - v.z);
1126                 //XVECTOR3 dir(v.x-gRSPlights[l].x, v.y-gRSPlights[l].y, v.z-gRSPlights[l].z);
1127                 float d2 = sqrtf(dir.x*dir.x+dir.y*dir.y+dir.z*dir.z);
1128                 dir.x /= d2;
1129                 dir.y /= d2;
1130                 dir.z /= d2;
1131
1132                 fCosT = norm.x*dir.x + norm.y*dir.y + norm.z*dir.z; 
1133
1134                 if (fCosT > 0 )
1135                 {
1136                     //float f = d2/gRSPlights[l].range*50;
1137                     float f = d2/15000*50;
1138                     f = 1 - min(f,1);
1139                     fCosT *= f*f;
1140
1141                     r += gRSPlights[l].fr * fCosT;
1142                     g += gRSPlights[l].fg * fCosT;
1143                     b += gRSPlights[l].fb * fCosT;
1144                 }
1145             }
1146         }
1147     }
1148
1149     if (r > 255) r = 255;
1150     if (g > 255) g = 255;
1151     if (b > 255) b = 255;
1152     return ((0xff000000)|(((uint32)r)<<16)|(((uint32)g)<<8)|((uint32)b));
1153 }
1154
1155 uint32 LightVertNew(XVECTOR4 & norm)
1156 {
1157     float fCosT;
1158
1159     // Do ambient
1160     register float r = gRSP.fAmbientLightR;
1161     register float g = gRSP.fAmbientLightG;
1162     register float b = gRSP.fAmbientLightB;
1163
1164
1165     for (register unsigned int l=0; l < gRSPnumLights; l++)
1166     {
1167         fCosT = norm.x*gRSPlights[l].tx + norm.y*gRSPlights[l].ty + norm.z*gRSPlights[l].tz; 
1168
1169         if (fCosT > 0 )
1170         {
1171             r += gRSPlights[l].fr * fCosT;
1172             g += gRSPlights[l].fg * fCosT;
1173             b += gRSPlights[l].fb * fCosT;
1174         }
1175     }
1176
1177     if (r > 255) r = 255;
1178     if (g > 255) g = 255;
1179     if (b > 255) b = 255;
1180     return ((0xff000000)|(((uint32)r)<<16)|(((uint32)g)<<8)|((uint32)b));
1181 }
1182
1183
1184 float zero = 0.0f;
1185 float onef = 1.0f;
1186 float fcosT;
1187
1188 #if !defined(__GNUC__) && !defined(NO_ASM)
1189 __declspec( naked ) uint32  __fastcall SSELightVert()
1190 {
1191     __asm
1192     {
1193         movaps      xmm3, DWORD PTR gRSP;   // loading Ambient colors, xmm3 is the result color
1194         movaps      xmm4, DWORD PTR [g_normal]; // xmm4 is the normal
1195
1196         mov         ecx, 0;
1197 loopback:
1198         cmp         ecx, DWORD PTR gRSPnumLights;
1199         jae         breakout;
1200         mov         eax,ecx;
1201         imul        eax,0x44;
1202         movups      xmm5, DWORD PTR gRSPlights[eax];        // Light Dir
1203         movups      xmm1, DWORD PTR gRSPlights[0x14][eax];  // Light color
1204         mulps       xmm5, xmm4;                 // Lightdir * normals
1205
1206         movhlps     xmm0,xmm5;
1207         addps       xmm0,xmm5;
1208         shufps      xmm5,xmm0,0x01;
1209         addps       xmm0,xmm5;
1210
1211         comiss      xmm0,zero;
1212         jc          endloop
1213
1214         shufps      xmm0,xmm0,0;                    // fcosT
1215         mulps       xmm1,xmm0; 
1216         addps       xmm3,xmm1; 
1217 endloop:
1218         inc         ecx;
1219         jmp         loopback;
1220 breakout:
1221
1222         movss       xmm0,DWORD PTR real255;
1223         shufps      xmm0,xmm0,0;
1224         minps       xmm0,xmm3;
1225
1226         // Without using a memory
1227         cvtss2si    eax,xmm0;       // move the 1st uint32 to eax
1228         shl         eax,10h;
1229         or          eax,0FF000000h;
1230         shufps      xmm0,xmm0,0E5h; // move the 2nd uint32 to the 1st uint32
1231         cvtss2si    ecx,xmm0;       // move the 1st uint32 to ecx
1232         shl         ecx,8;
1233         or          eax,ecx;
1234         shufps      xmm0,xmm0,0E6h; // Move the 3rd uint32 to the 1st uint32
1235         cvtss2si    ecx,xmm0;
1236         or          eax,ecx;
1237
1238         ret;
1239     }
1240 }
1241 #elif defined(__GNUC__) && defined(__x86_64__) && !defined(NO_ASM)
1242 uint32 SSELightVert(void)
1243 {
1244   uint32 rval;
1245   float f255 = 255.0, fZero = 0.0;
1246   
1247   asm volatile(" movaps        %1,  %%xmm3    \n" // xmm3 == gRSP.fAmbientLight{RGBA}
1248            " movaps            %2,  %%xmm4    \n" // xmm4 == g_normal.{xyz}
1249            " xor            %%rcx,   %%rcx    \n"
1250            "0:                                \n"
1251            " cmpl              %3,   %%ecx    \n"
1252            " jae               2f             \n"
1253            " mov            %%rcx,   %%rax    \n"
1254            " imul    $0x44, %%rax,   %%rax    \n"
1255            " movups   (%4,%%rax,),  %%xmm5    \n"  // xmm5 == gRSPlights[l].{xyzr}
1256            " movups 20(%4,%%rax,),  %%xmm1    \n"  // xmm1 == gRSPlights[l].{frfgfbfa}
1257            " mulps         %%xmm4,  %%xmm5    \n"
1258            " movhlps       %%xmm5,  %%xmm0    \n"
1259            " addps         %%xmm5,  %%xmm0    \n"
1260            " shufps $0x01, %%xmm0,  %%xmm5    \n"
1261            " addps         %%xmm5,  %%xmm0    \n"
1262            " comiss            %6,  %%xmm0    \n"
1263            " jc                1f             \n"
1264            " shufps $0x00, %%xmm0,  %%xmm0    \n"
1265            " mulps         %%xmm0,  %%xmm1    \n"
1266            " addps         %%xmm1,  %%xmm3    \n"
1267            "1:                                \n"
1268            " inc            %%rcx             \n"
1269            " jmp               0b             \n"
1270            "2:                                \n"
1271            " movss             %5,  %%xmm0    \n"
1272            " shufps $0x00, %%xmm0,  %%xmm0    \n"
1273            " minps         %%xmm3,  %%xmm0    \n"
1274            " cvtss2si      %%xmm0,   %%eax    \n"
1275            " shll           $0x10,   %%eax    \n"
1276            " orl      $0xff000000,   %%eax    \n"
1277            " shufps $0xe5, %%xmm0,  %%xmm0    \n"
1278            " cvtss2si      %%xmm0,   %%ecx    \n"
1279            " shll              $8,   %%ecx    \n"
1280            " orl            %%ecx,   %%eax    \n"
1281            " shufps $0xe6, %%xmm0,  %%xmm0    \n"
1282            " cvtss2si      %%xmm0,   %%ecx    \n"
1283            " orl            %%ecx,   %%eax    \n"
1284            : "=&a"(rval)
1285            : "m"(gRSP), "m"(g_normal), "m"(gRSPnumLights), "r"(gRSPlights), "m"(f255), "m"(fZero)
1286            : "%rcx", "memory", "cc", "%xmm0", "%xmm1", "%xmm3", "%xmm4", "%xmm5"
1287            );
1288   return rval;
1289 }
1290 #elif !defined(NO_ASM) // 32-bit GCC assumed
1291 uint32 SSELightVert(void)
1292 {
1293   uint32 rval;
1294   float f255 = 255.0, fZero = 0.0;
1295
1296   asm volatile(" movaps            %1,  %%xmm3    \n"
1297                " movaps            %2,  %%xmm4    \n"
1298                " xor            %%ecx,   %%ecx    \n"
1299                "0:                                \n"
1300                " cmpl              %3,   %%ecx    \n"
1301                " jae               2f             \n"
1302                " mov            %%ecx,   %%eax    \n"
1303                " imul    $0x44, %%eax,   %%eax    \n"
1304                " movups   (%4,%%eax,),  %%xmm5    \n"
1305                " movups 20(%4,%%eax,),  %%xmm1    \n"
1306                " mulps         %%xmm4,  %%xmm5    \n"
1307                " movhlps       %%xmm5,  %%xmm0    \n"
1308                " addps         %%xmm5,  %%xmm0    \n"
1309                " shufps $0x01, %%xmm0,  %%xmm5    \n"
1310                " addps         %%xmm5,  %%xmm0    \n"
1311                " comiss            %6,  %%xmm0    \n"
1312                " jc                1f             \n"
1313                " shufps $0x00, %%xmm0,  %%xmm0    \n"
1314                " mulps         %%xmm0,  %%xmm1    \n"
1315                " addps         %%xmm1,  %%xmm3    \n"
1316                "1:                                \n"
1317                " inc            %%ecx             \n"
1318                " jmp               0b             \n"
1319                "2:                                \n"
1320                " movss             %5,  %%xmm0    \n"
1321                " shufps $0x00, %%xmm0,  %%xmm0    \n"
1322                " minps         %%xmm3,  %%xmm0    \n"
1323                " cvtss2si      %%xmm0,   %%eax    \n"
1324                " shll           $0x10,   %%eax    \n"
1325                " orl      $0xff000000,   %%eax    \n"
1326                " shufps $0xe5, %%xmm0,  %%xmm0    \n"
1327                " cvtss2si      %%xmm0,   %%ecx    \n"
1328                " shll              $8,   %%ecx    \n"
1329                " orl            %%ecx,   %%eax    \n"
1330                " shufps $0xe6, %%xmm0,  %%xmm0    \n"
1331                " cvtss2si      %%xmm0,   %%ecx    \n"
1332                " orl            %%ecx,   %%eax    \n"
1333                : "=&a"(rval)
1334                : "m"(gRSP), "m"(g_normal), "m"(gRSPnumLights), "r"(gRSPlights), "m"(f255), "m"(fZero)
1335                : "%rcx", "memory", "cc", "%xmm0", "%xmm1", "%xmm3", "%xmm4", "%xmm5"
1336                );
1337   return rval;
1338 }
1339 #endif
1340
1341 inline void ReplaceAlphaWithFogFactor(int i)
1342 {
1343     if( gRDP.geometryMode & G_FOG )
1344     {
1345         // Use fog factor to replace vertex alpha
1346         if( g_vecProjected[i].z > 1 )
1347             *(((uint8*)&(g_dwVtxDifColor[i]))+3) = 0xFF;
1348         if( g_vecProjected[i].z < 0 )
1349             *(((uint8*)&(g_dwVtxDifColor[i]))+3) = 0;
1350         else
1351             *(((uint8*)&(g_dwVtxDifColor[i]))+3) = (uint8)(g_vecProjected[i].z*255);    
1352     }
1353 }
1354
1355
1356 // Bits
1357 // +-+-+-
1358 // xxyyzz
1359 #define Z_NEG  0x01
1360 #define Z_POS  0x02
1361 #define Y_NEG  0x04
1362 #define Y_POS  0x08
1363 #define X_NEG  0x10
1364 #define X_POS  0x20
1365
1366 // Assumes dwAddr has already been checked! 
1367 // Don't inline - it's too big with the transform macros
1368
1369 #if !defined(NO_ASM)
1370 void ProcessVertexDataSSE(uint32 dwAddr, uint32 dwV0, uint32 dwNum)
1371 {
1372     UpdateCombinedMatrix();
1373
1374     // This function is called upon SPvertex
1375     // - do vertex matrix transform
1376     // - do vertex lighting
1377     // - do texture cooridinate transform if needed
1378     // - calculate normal vector
1379
1380     // Output:  - g_vecProjected[i]             -> transformed vertex x,y,z
1381     //          - g_vecProjected[i].w                       -> saved vertex 1/w
1382     //          - g_dwVtxFlags[i]               -> flags
1383     //          - g_dwVtxDifColor[i]            -> vertex color
1384     //          - g_fVtxTxtCoords[i]                -> vertex texture cooridinates
1385
1386     FiddledVtx * pVtxBase = (FiddledVtx*)(g_pRDRAMu8 + dwAddr);
1387     g_pVtxBase = pVtxBase;
1388
1389     for (uint32 i = dwV0; i < dwV0 + dwNum; i++)
1390     {
1391         SP_Timing(RSP_GBI0_Vtx);
1392
1393         FiddledVtx & vert = pVtxBase[i - dwV0];
1394
1395         g_vtxNonTransformed[i].x = (float)vert.x;
1396         g_vtxNonTransformed[i].y = (float)vert.y;
1397         g_vtxNonTransformed[i].z = (float)vert.z;
1398
1399         SSEVec3Transform(i);
1400
1401         if( gRSP.bFogEnabled )
1402         {
1403             g_fFogCoord[i] = g_vecProjected[i].z;
1404             if( g_vecProjected[i].w < 0 || g_vecProjected[i].z < 0 || g_fFogCoord[i] < gRSPfFogMin )
1405                 g_fFogCoord[i] = gRSPfFogMin;
1406         }
1407
1408         ReplaceAlphaWithFogFactor(i);
1409
1410
1411         VTX_DUMP( 
1412         {
1413             uint32 *dat = (uint32*)(&vert);
1414             DebuggerAppendMsg("vtx %d: %08X %08X %08X %08X", i, dat[0],dat[1],dat[2],dat[3]); 
1415             DebuggerAppendMsg("      : %f, %f, %f, %f", 
1416                 g_vtxTransformed[i].x,g_vtxTransformed[i].y,g_vtxTransformed[i].z,g_vtxTransformed[i].w);
1417             DebuggerAppendMsg("      : %f, %f, %f, %f", 
1418                 g_vecProjected[i].x,g_vecProjected[i].y,g_vecProjected[i].z,g_vecProjected[i].w);
1419         });
1420
1421         RSP_Vtx_Clipping(i);
1422
1423         if( gRSP.bLightingEnable )
1424         {
1425             g_normal.x = (float)vert.norma.nx;
1426             g_normal.y = (float)vert.norma.ny;
1427             g_normal.z = (float)vert.norma.nz;
1428
1429             SSEVec3TransformNormal();
1430             if( options.enableHackForGames != HACK_FOR_ZELDA_MM )
1431                 g_dwVtxDifColor[i] = SSELightVert();
1432             else
1433                 g_dwVtxDifColor[i] = LightVert(g_normal, i);
1434             *(((uint8*)&(g_dwVtxDifColor[i]))+3) = vert.rgba.a; // still use alpha from the vertex
1435         }
1436         else
1437         {
1438             if( (gRDP.geometryMode & G_SHADE) == 0 && gRSP.ucode < 5 )  //Shade is disabled
1439             {
1440                 //FLAT shade
1441                 g_dwVtxDifColor[i] = gRDP.primitiveColor;
1442             }
1443             else
1444             {
1445                 register IColor &color = *(IColor*)&g_dwVtxDifColor[i];
1446                 color.b = vert.rgba.r;
1447                 color.g = vert.rgba.g;
1448                 color.r = vert.rgba.b;
1449                 color.a = vert.rgba.a;
1450             }
1451         }
1452
1453         if( options.bWinFrameMode )
1454         {
1455             g_dwVtxDifColor[i] = COLOR_RGBA(vert.rgba.r, vert.rgba.g, vert.rgba.b, vert.rgba.a);
1456         }
1457
1458         // Update texture coords n.b. need to divide tu/tv by bogus scale on addition to buffer
1459
1460         // If the vert is already lit, then there is no normal (and hence we
1461         // can't generate tex coord)
1462         if (gRSP.bTextureGen && gRSP.bLightingEnable )
1463         {
1464             TexGen(g_fVtxTxtCoords[i].x, g_fVtxTxtCoords[i].y);
1465         }
1466         else
1467         {
1468             g_fVtxTxtCoords[i].x = (float)vert.tu;
1469             g_fVtxTxtCoords[i].y = (float)vert.tv; 
1470         }
1471     }
1472
1473     VTX_DUMP(TRACE2("Setting Vertexes: %d - %d\n", dwV0, dwV0+dwNum-1));
1474     DEBUGGER_PAUSE_AND_DUMP(NEXT_VERTEX_CMD,{TRACE0("Paused at Vertex Cmd");});
1475 }
1476 #endif
1477
1478 void ProcessVertexDataNoSSE(uint32 dwAddr, uint32 dwV0, uint32 dwNum)
1479 {
1480
1481     UpdateCombinedMatrix();
1482
1483     // This function is called upon SPvertex
1484     // - do vertex matrix transform
1485     // - do vertex lighting
1486     // - do texture cooridinate transform if needed
1487     // - calculate normal vector
1488
1489     // Output:  - g_vecProjected[i]             -> transformed vertex x,y,z
1490     //          - g_vecProjected[i].w                       -> saved vertex 1/w
1491     //          - g_dwVtxFlags[i]               -> flags
1492     //          - g_dwVtxDifColor[i]            -> vertex color
1493     //          - g_fVtxTxtCoords[i]                -> vertex texture cooridinates
1494
1495     FiddledVtx * pVtxBase = (FiddledVtx*)(g_pRDRAMu8 + dwAddr);
1496     g_pVtxBase = pVtxBase;
1497
1498     for (uint32 i = dwV0; i < dwV0 + dwNum; i++)
1499     {
1500         SP_Timing(RSP_GBI0_Vtx);
1501
1502         FiddledVtx & vert = pVtxBase[i - dwV0];
1503
1504         g_vtxNonTransformed[i].x = (float)vert.x;
1505         g_vtxNonTransformed[i].y = (float)vert.y;
1506         g_vtxNonTransformed[i].z = (float)vert.z;
1507
1508         Vec3Transform(&g_vtxTransformed[i], (XVECTOR3*)&g_vtxNonTransformed[i], &gRSPworldProject); // Convert to w=1
1509
1510         g_vecProjected[i].w = 1.0f / g_vtxTransformed[i].w;
1511         g_vecProjected[i].x = g_vtxTransformed[i].x * g_vecProjected[i].w;
1512         g_vecProjected[i].y = g_vtxTransformed[i].y * g_vecProjected[i].w;
1513         if ((g_curRomInfo.bPrimaryDepthHack || options.enableHackForGames == HACK_FOR_NASCAR ) && gRDP.otherMode.depth_source )
1514         {
1515             g_vecProjected[i].z = gRDP.fPrimitiveDepth;
1516             g_vtxTransformed[i].z = gRDP.fPrimitiveDepth*g_vtxTransformed[i].w;
1517         }
1518         else
1519         {
1520             g_vecProjected[i].z = g_vtxTransformed[i].z * g_vecProjected[i].w;
1521         }
1522
1523         if( gRSP.bFogEnabled )
1524         {
1525             g_fFogCoord[i] = g_vecProjected[i].z;
1526             if( g_vecProjected[i].w < 0 || g_vecProjected[i].z < 0 || g_fFogCoord[i] < gRSPfFogMin )
1527                 g_fFogCoord[i] = gRSPfFogMin;
1528         }
1529
1530         VTX_DUMP( 
1531         {
1532             uint32 *dat = (uint32*)(&vert);
1533             DebuggerAppendMsg("vtx %d: %08X %08X %08X %08X", i, dat[0],dat[1],dat[2],dat[3]); 
1534             DebuggerAppendMsg("      : %f, %f, %f, %f", 
1535                 g_vtxTransformed[i].x,g_vtxTransformed[i].y,g_vtxTransformed[i].z,g_vtxTransformed[i].w);
1536             DebuggerAppendMsg("      : %f, %f, %f, %f", 
1537                 g_vecProjected[i].x,g_vecProjected[i].y,g_vecProjected[i].z,g_vecProjected[i].w);
1538         });
1539
1540         RSP_Vtx_Clipping(i);
1541
1542         if( gRSP.bLightingEnable )
1543         {
1544             g_normal.x = (float)vert.norma.nx;
1545             g_normal.y = (float)vert.norma.ny;
1546             g_normal.z = (float)vert.norma.nz;
1547
1548             Vec3TransformNormal(g_normal, gRSPmodelViewTop);
1549             g_dwVtxDifColor[i] = LightVert(g_normal, i);
1550             *(((uint8*)&(g_dwVtxDifColor[i]))+3) = vert.rgba.a; // still use alpha from the vertex
1551         }
1552         else
1553         {
1554             if( (gRDP.geometryMode & G_SHADE) == 0 && gRSP.ucode < 5 )  //Shade is disabled
1555             {
1556                 //FLAT shade
1557                 g_dwVtxDifColor[i] = gRDP.primitiveColor;
1558             }
1559             else
1560             {
1561                 register IColor &color = *(IColor*)&g_dwVtxDifColor[i];
1562                 color.b = vert.rgba.r;
1563                 color.g = vert.rgba.g;
1564                 color.r = vert.rgba.b;
1565                 color.a = vert.rgba.a;
1566             }
1567         }
1568
1569         if( options.bWinFrameMode )
1570         {
1571             g_dwVtxDifColor[i] = COLOR_RGBA(vert.rgba.r, vert.rgba.g, vert.rgba.b, vert.rgba.a);
1572         }
1573
1574         ReplaceAlphaWithFogFactor(i);
1575
1576         // Update texture coords n.b. need to divide tu/tv by bogus scale on addition to buffer
1577
1578         // If the vert is already lit, then there is no normal (and hence we
1579         // can't generate tex coord)
1580         if (gRSP.bTextureGen && gRSP.bLightingEnable )
1581         {
1582             TexGen(g_fVtxTxtCoords[i].x, g_fVtxTxtCoords[i].y);
1583         }
1584         else
1585         {
1586             g_fVtxTxtCoords[i].x = (float)vert.tu;
1587             g_fVtxTxtCoords[i].y = (float)vert.tv; 
1588         }
1589     }
1590
1591     VTX_DUMP(TRACE2("Setting Vertexes: %d - %d\n", dwV0, dwV0+dwNum-1));
1592     DEBUGGER_PAUSE_AND_DUMP(NEXT_VERTEX_CMD,{TRACE0("Paused at Vertex Cmd");});
1593 }
1594
1595 extern "C" void pv_neon(XVECTOR4 *g_vtxTransformed, XVECTOR4 *g_vecProjected,
1596     uint32 *g_dwVtxDifColor, VECTOR2 *g_fVtxTxtCoords,
1597     float *g_fFogCoord, uint32 *g_clipFlag2,
1598     uint32 dwNum, const FiddledVtx *vtx,
1599     const Light *gRSPlights, const float *fRSPAmbientLightRGBA,
1600     const XMATRIX *gRSPworldProject, const XMATRIX *gRSPmodelViewTop,
1601     uint32 gRSPnumLights, float gRSPfFogMin);
1602
1603 void ProcessVertexDataNEON(uint32 dwAddr, uint32 dwV0, uint32 dwNum)
1604 {
1605     if (gRSP.bTextureGen && gRSP.bLightingEnable) {
1606         ProcessVertexDataNoSSE(dwAddr, dwV0,dwNum);
1607         return;
1608     }
1609
1610     // assumtions:
1611     // - g_clipFlag is not used at all
1612     // - g_vtxNonTransformed is not used after ProcessVertexData*() returns
1613     // - g_normal - same
1614
1615 #define PV_NEON_ENABLE_LIGHT    (1 << 0)
1616 #define PV_NEON_ENABLE_SHADE    (1 << 1)
1617 #define PV_NEON_ENABLE_FOG      (1 << 2)
1618 #define PV_NEON_FOG_ALPHA       (1 << 3)
1619
1620     int neon_state = 0;
1621     if ( gRSP.bLightingEnable )
1622         neon_state |= PV_NEON_ENABLE_LIGHT;
1623     if ( (gRDP.geometryMode & G_SHADE) || gRSP.ucode >= 5 )
1624         neon_state |= PV_NEON_ENABLE_SHADE;
1625     if ( gRSP.bFogEnabled )
1626         neon_state |= PV_NEON_ENABLE_FOG;
1627     if ( gRDP.geometryMode & G_FOG )
1628         neon_state |= PV_NEON_FOG_ALPHA;
1629
1630     uint32 i;
1631
1632     UpdateCombinedMatrix();
1633
1634     // This function is called upon SPvertex
1635     // - do vertex matrix transform
1636     // - do vertex lighting
1637     // - do texture cooridinate transform if needed
1638     // - calculate normal vector
1639
1640     // Output:  - g_vecProjected[i]             -> transformed vertex x,y,z
1641     //          - g_vecProjected[i].w           -> saved vertex 1/w
1642     //          - g_vtxTransformed[i]
1643     //          - g_dwVtxDifColor[i]            -> vertex color
1644     //          - g_fVtxTxtCoords[i]            -> vertex texture cooridinates
1645     //          - g_fFogCoord[i]
1646     //          - g_clipFlag2[i]
1647
1648     const FiddledVtx * pVtxBase = (const FiddledVtx*)(g_pRDRAMu8 + dwAddr);
1649     g_pVtxBase = (FiddledVtx *)pVtxBase;
1650
1651     // SP_Timing(RSP_GBI0_Vtx);
1652     status.SPCycleCount += Timing_RSP_GBI0_Vtx * dwNum;
1653
1654     if (!(neon_state & (PV_NEON_ENABLE_LIGHT | PV_NEON_ENABLE_SHADE))) {
1655         for (i = dwV0; i < dwV0 + dwNum; i++)
1656             g_dwVtxDifColor[i] = gRDP.primitiveColor; // FLAT shade
1657     }
1658
1659     for (i = dwV0; i < dwV0 + dwNum; i++)
1660     {
1661         const FiddledVtx & vert = pVtxBase[i - dwV0];
1662         XVECTOR3 vtx_raw; // was g_vtxNonTransformed
1663
1664         vtx_raw.x = (float)vert.x;
1665         vtx_raw.y = (float)vert.y;
1666         vtx_raw.z = (float)vert.z;
1667
1668         Vec3Transform(&g_vtxTransformed[i], &vtx_raw, &gRSPworldProject); // Convert to w=1
1669
1670         g_vecProjected[i].w = 1.0f / g_vtxTransformed[i].w;
1671         g_vecProjected[i].x = g_vtxTransformed[i].x * g_vecProjected[i].w;
1672         g_vecProjected[i].y = g_vtxTransformed[i].y * g_vecProjected[i].w;
1673         g_vecProjected[i].z = g_vtxTransformed[i].z * g_vecProjected[i].w;
1674
1675         if( neon_state & PV_NEON_ENABLE_FOG )
1676         {
1677             g_fFogCoord[i] = g_vecProjected[i].z;
1678             if( g_vecProjected[i].w < 0 || g_vecProjected[i].z < 0 || g_fFogCoord[i] < gRSPfFogMin )
1679                 g_fFogCoord[i] = gRSPfFogMin;
1680         }
1681
1682         // RSP_Vtx_Clipping(i);
1683         g_clipFlag2[i] = 0;
1684         if( g_vecProjected[i].w > 0 )
1685         {
1686             if( g_vecProjected[i].x > 1 )   g_clipFlag2[i] |= X_CLIP_MAX;
1687             if( g_vecProjected[i].x < -1 )  g_clipFlag2[i] |= X_CLIP_MIN;
1688             if( g_vecProjected[i].y > 1 )   g_clipFlag2[i] |= Y_CLIP_MAX;
1689             if( g_vecProjected[i].y < -1 )  g_clipFlag2[i] |= Y_CLIP_MIN;
1690         }
1691
1692         if( neon_state & PV_NEON_ENABLE_LIGHT )
1693         {
1694             XVECTOR3 normal; // was g_normal
1695             float r, g, b;
1696
1697             normal.x = (float)vert.norma.nx;
1698             normal.y = (float)vert.norma.ny;
1699             normal.z = (float)vert.norma.nz;
1700
1701             Vec3TransformNormal(normal, gRSPmodelViewTop);
1702
1703             r = gRSP.fAmbientLightR;
1704             g = gRSP.fAmbientLightG;
1705             b = gRSP.fAmbientLightB;
1706
1707             for (unsigned int l=0; l < gRSPnumLights; l++)
1708             {
1709                 float fCosT = normal.x * gRSPlights[l].x + normal.y * gRSPlights[l].y + normal.z * gRSPlights[l].z; 
1710
1711                 if (fCosT > 0 )
1712                 {
1713                     r += gRSPlights[l].fr * fCosT;
1714                     g += gRSPlights[l].fg * fCosT;
1715                     b += gRSPlights[l].fb * fCosT;
1716                 }
1717             }
1718             if (r > 255) r = 255;
1719             if (g > 255) g = 255;
1720             if (b > 255) b = 255;
1721             g_dwVtxDifColor[i] = ((vert.rgba.a<<24)|(((uint32)r)<<16)|(((uint32)g)<<8)|((uint32)b));
1722         }
1723         else if( neon_state & PV_NEON_ENABLE_SHADE )
1724         {
1725             IColor &color = *(IColor*)&g_dwVtxDifColor[i];
1726             color.b = vert.rgba.r;
1727             color.g = vert.rgba.g;
1728             color.r = vert.rgba.b;
1729             color.a = vert.rgba.a;
1730         }
1731
1732         // ReplaceAlphaWithFogFactor(i);
1733         if( neon_state & PV_NEON_FOG_ALPHA )
1734         {
1735             // Use fog factor to replace vertex alpha
1736             if( g_vecProjected[i].z > 1 )
1737                 *(((uint8*)&(g_dwVtxDifColor[i]))+3) = 0xFF;
1738             if( g_vecProjected[i].z < 0 )
1739                 *(((uint8*)&(g_dwVtxDifColor[i]))+3) = 0;
1740             else
1741                 *(((uint8*)&(g_dwVtxDifColor[i]))+3) = (uint8)(g_vecProjected[i].z*255);    
1742         }
1743
1744         g_fVtxTxtCoords[i].x = (float)vert.tu;
1745         g_fVtxTxtCoords[i].y = (float)vert.tv; 
1746     }
1747 }
1748
1749  bool PrepareTriangle(uint32 dwV0, uint32 dwV1, uint32 dwV2)
1750 {
1751     if( status.isVertexShaderEnabled || status.bUseHW_T_L )
1752     {
1753         g_vtxIndex[gRSP.numVertices++] = dwV0;
1754         g_vtxIndex[gRSP.numVertices++] = dwV1;
1755         g_vtxIndex[gRSP.numVertices++] = dwV2;
1756         status.dwNumTrisRendered++;
1757         gRSP.maxVertexID = max(gRSP.maxVertexID,max(dwV0,max(dwV1,dwV2)));
1758     }
1759     else
1760     {
1761         SP_Timing(SP_Each_Triangle);
1762
1763         bool textureFlag = (CRender::g_pRender->IsTextureEnabled() || gRSP.ucode == 6 );
1764         bool openGL = CDeviceBuilder::m_deviceGeneralType == OGL_DEVICE;
1765
1766         InitVertex(dwV0, gRSP.numVertices, textureFlag, openGL);
1767         InitVertex(dwV1, gRSP.numVertices+1, textureFlag, openGL);
1768         InitVertex(dwV2, gRSP.numVertices+2, textureFlag, openGL);
1769
1770         gRSP.numVertices += 3;
1771         status.dwNumTrisRendered++;
1772     }
1773
1774     return true;
1775 }
1776
1777
1778
1779 // Returns TRUE if it thinks the triangle is visible
1780 // Returns FALSE if it is clipped
1781 bool IsTriangleVisible(uint32 dwV0, uint32 dwV1, uint32 dwV2)
1782 {
1783     //return true;  //fix me
1784
1785     if( status.isVertexShaderEnabled || status.bUseHW_T_L ) return true;    // We won't have access to transformed vertex data
1786
1787     DEBUGGER_ONLY_IF( (!debuggerEnableTestTris || !debuggerEnableCullFace), {return TRUE;});
1788     
1789 #ifdef DEBUGGER
1790     // Check vertices are valid!
1791     if (dwV0 >= MAX_VERTS || dwV1 >= MAX_VERTS || dwV2 >= MAX_VERTS)
1792         return false;
1793 #endif
1794
1795     // Here we AND all the flags. If any of the bits is set for all
1796     // 3 vertices, it means that all three x, y or z lie outside of
1797     // the current viewing volume.
1798     // Currently disabled - still seems a bit dodgy
1799     if ((gRSP.bCullFront || gRSP.bCullBack) && gRDP.otherMode.zmode != 3)
1800     {
1801         XVECTOR4 & v0 = g_vecProjected[dwV0];
1802         XVECTOR4 & v1 = g_vecProjected[dwV1];
1803         XVECTOR4 & v2 = g_vecProjected[dwV2];
1804
1805         // Only try to clip if the tri is onscreen. For some reason, this
1806         // method doesnt' work well when the z value is outside of screenspace
1807         //if (v0.z < 1 && v1.z < 1 && v2.z < 1)
1808         {
1809             float V1 = v2.x - v0.x;
1810             float V2 = v2.y - v0.y;
1811             
1812             float W1 = v2.x - v1.x;
1813             float W2 = v2.y - v1.y;
1814
1815             float fDirection = (V1 * W2) - (V2 * W1);
1816             fDirection = fDirection * v1.w * v2.w * v0.w;
1817             //float fDirection = v0.x*v1.y-v1.x*v0.y+v1.x*v2.y-v2.x*v1.y+v2.x*v0.y-v0.x*v2.y;
1818
1819             if (fDirection < 0 && gRSP.bCullBack)
1820             {
1821                 status.dwNumTrisClipped++;
1822                 return false;
1823             }
1824             else if (fDirection > 0 && gRSP.bCullFront)
1825             {
1826                 status.dwNumTrisClipped++;
1827                 return false;
1828             }
1829         }
1830     }
1831     
1832 #ifdef ENABLE_CLIP_TRI
1833     //if( gRSP.bRejectVtx && (g_clipFlag[dwV0]|g_clipFlag[dwV1]|g_clipFlag[dwV2]) ) 
1834     //  return;
1835     if( g_clipFlag2[dwV0]&g_clipFlag2[dwV1]&g_clipFlag2[dwV2] )
1836     {
1837         //DebuggerAppendMsg("Clipped");
1838         return false;
1839     }
1840 #endif
1841
1842     return true;
1843 }
1844
1845
1846 void SetPrimitiveColor(uint32 dwCol, uint32 LODMin, uint32 LODFrac)
1847 {
1848     gRDP.colorsAreReloaded = true;
1849     gRDP.primitiveColor = dwCol;
1850     gRDP.primLODMin = LODMin;
1851     gRDP.primLODFrac = LODFrac;
1852     if( gRDP.primLODFrac < gRDP.primLODMin )
1853     {
1854         gRDP.primLODFrac = gRDP.primLODMin;
1855     }
1856
1857     gRDP.fvPrimitiveColor[0] = ((dwCol>>16)&0xFF)/255.0f;  //r
1858     gRDP.fvPrimitiveColor[1] = ((dwCol>>8)&0xFF)/255.0f;   //g
1859     gRDP.fvPrimitiveColor[2] = ((dwCol)&0xFF)/255.0f;      //b
1860     gRDP.fvPrimitiveColor[3] = ((dwCol>>24)&0xFF)/255.0f;  //a
1861 }
1862
1863 void SetPrimitiveDepth(uint32 z, uint32 dwDZ)
1864 {
1865     gRDP.primitiveDepth = z & 0x7FFF;
1866     gRDP.fPrimitiveDepth = (float)(gRDP.primitiveDepth)/(float)0x8000;
1867
1868     //gRDP.fPrimitiveDepth = gRDP.fPrimitiveDepth*2-1;  
1869     /*
1870     z=0xFFFF    ->  1   the farest
1871     z=0         ->  -1  the nearest
1872     */
1873
1874     //how to use dwDZ?
1875
1876 #ifdef DEBUGGER
1877     if( (pauseAtNext && (eventToPause == NEXT_VERTEX_CMD || eventToPause == NEXT_FLUSH_TRI )) )//&& logTriangles ) 
1878     {
1879         DebuggerAppendMsg("Set prim Depth: %f, (%08X, %08X)", gRDP.fPrimitiveDepth, z, dwDZ); 
1880     }
1881 #endif
1882 }
1883
1884 void SetVertexXYZ(uint32 vertex, float x, float y, float z)
1885 {
1886     g_vecProjected[vertex].x = x;
1887     g_vecProjected[vertex].y = y;
1888     g_vecProjected[vertex].z = z;
1889
1890     g_vtxTransformed[vertex].x = x*g_vtxTransformed[vertex].w;
1891     g_vtxTransformed[vertex].y = y*g_vtxTransformed[vertex].w;
1892     g_vtxTransformed[vertex].z = z*g_vtxTransformed[vertex].w;
1893 }
1894
1895 void ModifyVertexInfo(uint32 where, uint32 vertex, uint32 val)
1896 {
1897     switch (where)
1898     {
1899     case RSP_MV_WORD_OFFSET_POINT_RGBA:     // Modify RGBA
1900         {
1901             uint32 r = (val>>24)&0xFF;
1902             uint32 g = (val>>16)&0xFF;
1903             uint32 b = (val>>8)&0xFF;
1904             uint32 a = val&0xFF;
1905             g_dwVtxDifColor[vertex] = COLOR_RGBA(r, g, b, a);
1906             LOG_UCODE("Modify vert %d color, 0x%08x", vertex, g_dwVtxDifColor[vertex]);
1907         }
1908         break;
1909     case RSP_MV_WORD_OFFSET_POINT_XYSCREEN:     // Modify X,Y
1910         {
1911             uint16 nX = (uint16)(val>>16);
1912             short x = *((short*)&nX);
1913             x /= 4;
1914
1915             uint16 nY = (uint16)(val&0xFFFF);
1916             short y = *((short*)&nY);
1917             y /= 4;
1918
1919             // Should do viewport transform.
1920
1921
1922             x -= windowSetting.uViWidth/2;
1923             y = windowSetting.uViHeight/2-y;
1924
1925             if( options.bEnableHacks && ((*g_GraphicsInfo.VI_X_SCALE_REG)&0xF) != 0 )
1926             {
1927                 // Tarzan
1928                 // I don't know why Tarzan is different
1929                 SetVertexXYZ(vertex, x/windowSetting.fViWidth, y/windowSetting.fViHeight, g_vecProjected[vertex].z);
1930             }
1931             else
1932             {
1933                 // Toy Story 2 and other games
1934                 SetVertexXYZ(vertex, x*2/windowSetting.fViWidth, y*2/windowSetting.fViHeight, g_vecProjected[vertex].z);
1935             }
1936
1937             LOG_UCODE("Modify vert %d: x=%d, y=%d", vertex, x, y);
1938             VTX_DUMP(TRACE3("Modify vert %d: (%d,%d)", vertex, x, y));
1939         }
1940         break;
1941     case RSP_MV_WORD_OFFSET_POINT_ZSCREEN:      // Modify C
1942         {
1943             int z = val>>16;
1944
1945             SetVertexXYZ(vertex, g_vecProjected[vertex].x, g_vecProjected[vertex].y, (((float)z/0x03FF)+0.5f)/2.0f );
1946             LOG_UCODE("Modify vert %d: z=%d", vertex, z);
1947             VTX_DUMP(TRACE2("Modify vert %d: z=%d", vertex, z));
1948         }
1949         break;
1950     case RSP_MV_WORD_OFFSET_POINT_ST:       // Texture
1951         {
1952             short tu = short(val>>16);
1953             short tv = short(val & 0xFFFF);
1954             float ftu = tu / 32.0f;
1955             float ftv = tv / 32.0f;
1956             LOG_UCODE("      Setting vertex %d tu/tv to %f, %f", vertex, (float)tu, (float)tv);
1957             CRender::g_pRender->SetVtxTextureCoord(vertex, ftu/gRSP.fTexScaleX, ftv/gRSP.fTexScaleY);
1958         }
1959         break;
1960     }
1961     DEBUGGER_PAUSE_AND_DUMP(NEXT_VERTEX_CMD,{TRACE0("Paused at ModVertex Cmd");});
1962 }
1963
1964 void ProcessVertexDataDKR(uint32 dwAddr, uint32 dwV0, uint32 dwNum)
1965 {
1966     UpdateCombinedMatrix();
1967
1968     long long pVtxBase = (long long) (g_pRDRAMu8 + dwAddr);
1969     g_pVtxBase = (FiddledVtx*)pVtxBase;
1970
1971     Matrix &matWorldProject = gRSP.DKRMatrixes[gRSP.DKRCMatrixIndex];
1972
1973     int nOff;
1974
1975     bool addbase=false;
1976     if ((!gRSP.DKRBillBoard) || (gRSP.DKRCMatrixIndex != 2) )
1977         addbase = false;
1978     else
1979         addbase = true;
1980
1981     if( addbase && gRSP.DKRVtxCount == 0 && dwNum > 1 )
1982     {
1983         gRSP.DKRVtxCount++;
1984     }
1985
1986     LOG_UCODE("    ProcessVertexDataDKR, CMatrix = %d, Add base=%s", gRSP.DKRCMatrixIndex, gRSP.DKRBillBoard?"true":"false");
1987     VTX_DUMP(TRACE2("DKR Setting Vertexes\nCMatrix = %d, Add base=%s", gRSP.DKRCMatrixIndex, gRSP.DKRBillBoard?"true":"false"));
1988
1989     nOff = 0;
1990     uint32 end = dwV0 + dwNum;
1991     for (uint32 i = dwV0; i < end; i++)
1992     {
1993         XVECTOR3 w;
1994
1995         g_vtxNonTransformed[i].x = (float)*(short*)((pVtxBase+nOff + 0) ^ 2);
1996         g_vtxNonTransformed[i].y = (float)*(short*)((pVtxBase+nOff + 2) ^ 2);
1997         g_vtxNonTransformed[i].z = (float)*(short*)((pVtxBase+nOff + 4) ^ 2);
1998
1999         //if( status.isSSEEnabled )
2000         //  SSEVec3TransformDKR(g_vtxTransformed[i], g_vtxNonTransformed[i]);
2001         //else
2002             Vec3Transform(&g_vtxTransformed[i], (XVECTOR3*)&g_vtxNonTransformed[i], &matWorldProject);  // Convert to w=1
2003
2004         if( gRSP.DKRVtxCount == 0 && dwNum==1 )
2005         {
2006             gRSP.DKRBaseVec.x = g_vtxTransformed[i].x;
2007             gRSP.DKRBaseVec.y = g_vtxTransformed[i].y;
2008             gRSP.DKRBaseVec.z = g_vtxTransformed[i].z;
2009             gRSP.DKRBaseVec.w = g_vtxTransformed[i].w;
2010         }
2011         else if( addbase )
2012         {
2013             g_vtxTransformed[i].x += gRSP.DKRBaseVec.x;
2014             g_vtxTransformed[i].y += gRSP.DKRBaseVec.y;
2015             g_vtxTransformed[i].z += gRSP.DKRBaseVec.z;
2016             g_vtxTransformed[i].w  = gRSP.DKRBaseVec.w;
2017         }
2018
2019         g_vecProjected[i].w = 1.0f / g_vtxTransformed[i].w;
2020         g_vecProjected[i].x = g_vtxTransformed[i].x * g_vecProjected[i].w;
2021         g_vecProjected[i].y = g_vtxTransformed[i].y * g_vecProjected[i].w;
2022         g_vecProjected[i].z = g_vtxTransformed[i].z * g_vecProjected[i].w;
2023
2024         gRSP.DKRVtxCount++;
2025
2026         VTX_DUMP(TRACE5("vtx %d: %f, %f, %f, %f", i, 
2027             g_vtxTransformed[i].x,g_vtxTransformed[i].y,g_vtxTransformed[i].z,g_vtxTransformed[i].w));
2028
2029         if( gRSP.bFogEnabled )
2030         {
2031             g_fFogCoord[i] = g_vecProjected[i].z;
2032             if( g_vecProjected[i].w < 0 || g_vecProjected[i].z < 0 || g_fFogCoord[i] < gRSPfFogMin )
2033                 g_fFogCoord[i] = gRSPfFogMin;
2034         }
2035
2036         RSP_Vtx_Clipping(i);
2037
2038         short wA = *(short*)((pVtxBase+nOff + 6) ^ 2);
2039         short wB = *(short*)((pVtxBase+nOff + 8) ^ 2);
2040
2041         s8 r = (s8)(wA >> 8);
2042         s8 g = (s8)(wA);
2043         s8 b = (s8)(wB >> 8);
2044         s8 a = (s8)(wB);
2045
2046         if (gRSP.bLightingEnable)
2047         {
2048             g_normal.x = (char)r; //norma.nx;
2049             g_normal.y = (char)g; //norma.ny;
2050             g_normal.z = (char)b; //norma.nz;
2051
2052             Vec3TransformNormal(g_normal, matWorldProject)
2053 #if !defined(NO_ASM)
2054             if( status.isSSEEnabled )
2055                 g_dwVtxDifColor[i] = SSELightVert();
2056             else
2057 #endif
2058                 g_dwVtxDifColor[i] = LightVert(g_normal, i);
2059         }
2060         else
2061         {
2062             int nR, nG, nB, nA;
2063
2064             nR = r;
2065             nG = g;
2066             nB = b;
2067             nA = a;
2068             // Assign true vert colour after lighting/fogging
2069             g_dwVtxDifColor[i] = COLOR_RGBA(nR, nG, nB, nA);
2070         }
2071
2072         ReplaceAlphaWithFogFactor(i);
2073
2074         g_fVtxTxtCoords[i].x = g_fVtxTxtCoords[i].y = 1;
2075
2076         nOff += 10;
2077     }
2078
2079
2080     DEBUGGER_PAUSE_AND_DUMP(NEXT_VERTEX_CMD,{DebuggerAppendMsg("Paused at DKR Vertex Cmd, v0=%d, vn=%d, addr=%08X", dwV0, dwNum, dwAddr);});
2081 }
2082
2083
2084 extern uint32 dwPDCIAddr;
2085 void ProcessVertexDataPD(uint32 dwAddr, uint32 dwV0, uint32 dwNum)
2086 {
2087     UpdateCombinedMatrix();
2088
2089     N64VtxPD * pVtxBase = (N64VtxPD*)(g_pRDRAMu8 + dwAddr);
2090     g_pVtxBase = (FiddledVtx*)pVtxBase; // Fix me
2091
2092     for (uint32 i = dwV0; i < dwV0 + dwNum; i++)
2093     {
2094         N64VtxPD &vert = pVtxBase[i - dwV0];
2095
2096         g_vtxNonTransformed[i].x = (float)vert.x;
2097         g_vtxNonTransformed[i].y = (float)vert.y;
2098         g_vtxNonTransformed[i].z = (float)vert.z;
2099
2100 #if !defined(NO_ASM)
2101         if( status.isSSEEnabled )
2102             SSEVec3Transform(i);
2103         else
2104 #endif
2105         {
2106             Vec3Transform(&g_vtxTransformed[i], (XVECTOR3*)&g_vtxNonTransformed[i], &gRSPworldProject); // Convert to w=1
2107             g_vecProjected[i].w = 1.0f / g_vtxTransformed[i].w;
2108             g_vecProjected[i].x = g_vtxTransformed[i].x * g_vecProjected[i].w;
2109             g_vecProjected[i].y = g_vtxTransformed[i].y * g_vecProjected[i].w;
2110             g_vecProjected[i].z = g_vtxTransformed[i].z * g_vecProjected[i].w;
2111         }
2112
2113         g_fFogCoord[i] = g_vecProjected[i].z;
2114         if( g_vecProjected[i].w < 0 || g_vecProjected[i].z < 0 || g_fFogCoord[i] < gRSPfFogMin )
2115             g_fFogCoord[i] = gRSPfFogMin;
2116
2117         RSP_Vtx_Clipping(i);
2118
2119         uint8 *addr = g_pRDRAMu8+dwPDCIAddr+ (vert.cidx&0xFF);
2120         uint32 a = addr[0];
2121         uint32 r = addr[3];
2122         uint32 g = addr[2];
2123         uint32 b = addr[1];
2124
2125         if( gRSP.bLightingEnable )
2126         {
2127             g_normal.x = (char)r;
2128             g_normal.y = (char)g;
2129             g_normal.z = (char)b;
2130 #if !defined(NO_ASM)
2131             if( status.isSSEEnabled )
2132             {
2133                 SSEVec3TransformNormal();
2134                 g_dwVtxDifColor[i] = SSELightVert();
2135             }
2136             else
2137 #endif
2138             {
2139                 Vec3TransformNormal(g_normal, gRSPmodelViewTop);
2140                 g_dwVtxDifColor[i] = LightVert(g_normal, i);
2141             }
2142             *(((uint8*)&(g_dwVtxDifColor[i]))+3) = (uint8)a;    // still use alpha from the vertex
2143         }
2144         else
2145         {
2146             if( (gRDP.geometryMode & G_SHADE) == 0 && gRSP.ucode < 5 )  //Shade is disabled
2147             {
2148                 g_dwVtxDifColor[i] = gRDP.primitiveColor;
2149             }
2150             else    //FLAT shade
2151             {
2152                 g_dwVtxDifColor[i] = COLOR_RGBA(r, g, b, a);
2153             }
2154         }
2155
2156         if( options.bWinFrameMode )
2157         {
2158             g_dwVtxDifColor[i] = COLOR_RGBA(r, g, b, a);
2159         }
2160
2161         ReplaceAlphaWithFogFactor(i);
2162
2163         VECTOR2 & t = g_fVtxTxtCoords[i];
2164         if (gRSP.bTextureGen && gRSP.bLightingEnable )
2165         {
2166             // Not sure if we should transform the normal here
2167             //Matrix & matWV = gRSP.projectionMtxs[gRSP.projectionMtxTop];
2168             //Vec3TransformNormal(g_normal, matWV);
2169
2170             TexGen(g_fVtxTxtCoords[i].x, g_fVtxTxtCoords[i].y);
2171         }
2172         else
2173         {
2174             t.x = vert.s;
2175             t.y = vert.t; 
2176         }
2177
2178
2179         VTX_DUMP( 
2180         {
2181             DebuggerAppendMsg("vtx %d: %d %d %d", i, vert.x,vert.y,vert.z); 
2182             DebuggerAppendMsg("      : %f, %f, %f, %f", 
2183                 g_vtxTransformed[i].x,g_vtxTransformed[i].y,g_vtxTransformed[i].z,g_vtxTransformed[i].w);
2184             DebuggerAppendMsg("      : %X, %X, %X, %X", r,g,b,a);
2185             DebuggerAppendMsg("      : u=%f, v=%f", t.x, t.y);
2186         });
2187     }
2188
2189     VTX_DUMP(TRACE2("Setting Vertexes: %d - %d\n", dwV0, dwV0+dwNum-1));
2190     DEBUGGER_PAUSE_AND_DUMP(NEXT_VERTEX_CMD,{TRACE0("Paused at Vertex Cmd");});
2191 }
2192
2193 extern uint32 dwConkerVtxZAddr;
2194 void ProcessVertexDataConker(uint32 dwAddr, uint32 dwV0, uint32 dwNum)
2195 {
2196     UpdateCombinedMatrix();
2197
2198     FiddledVtx * pVtxBase = (FiddledVtx*)(g_pRDRAMu8 + dwAddr);
2199     g_pVtxBase = pVtxBase;
2200     //short *vertexColoraddr = (short*)(g_pRDRAMu8+dwConkerVtxZAddr);
2201
2202     for (uint32 i = dwV0; i < dwV0 + dwNum; i++)
2203     {
2204         SP_Timing(RSP_GBI0_Vtx);
2205
2206         FiddledVtx & vert = pVtxBase[i - dwV0];
2207
2208         g_vtxNonTransformed[i].x = (float)vert.x;
2209         g_vtxNonTransformed[i].y = (float)vert.y;
2210         g_vtxNonTransformed[i].z = (float)vert.z;
2211
2212 #if !defined(NO_ASM)
2213         if( status.isSSEEnabled )
2214             SSEVec3Transform(i);
2215         else
2216 #endif
2217         {
2218             Vec3Transform(&g_vtxTransformed[i], (XVECTOR3*)&g_vtxNonTransformed[i], &gRSPworldProject); // Convert to w=1
2219             g_vecProjected[i].w = 1.0f / g_vtxTransformed[i].w;
2220             g_vecProjected[i].x = g_vtxTransformed[i].x * g_vecProjected[i].w;
2221             g_vecProjected[i].y = g_vtxTransformed[i].y * g_vecProjected[i].w;
2222             g_vecProjected[i].z = g_vtxTransformed[i].z * g_vecProjected[i].w;
2223         }
2224
2225         g_fFogCoord[i] = g_vecProjected[i].z;
2226         if( g_vecProjected[i].w < 0 || g_vecProjected[i].z < 0 || g_fFogCoord[i] < gRSPfFogMin )
2227             g_fFogCoord[i] = gRSPfFogMin;
2228
2229         VTX_DUMP( 
2230         {
2231             uint32 *dat = (uint32*)(&vert);
2232             DebuggerAppendMsg("vtx %d: %08X %08X %08X %08X", i, dat[0],dat[1],dat[2],dat[3]); 
2233             DebuggerAppendMsg("      : %f, %f, %f, %f", 
2234                 g_vtxTransformed[i].x,g_vtxTransformed[i].y,g_vtxTransformed[i].z,g_vtxTransformed[i].w);
2235             DebuggerAppendMsg("      : %f, %f, %f, %f", 
2236                 g_vecProjected[i].x,g_vecProjected[i].y,g_vecProjected[i].z,g_vecProjected[i].w);
2237         });
2238
2239         RSP_Vtx_Clipping(i);
2240
2241         if( gRSP.bLightingEnable )
2242         {
2243             {
2244                 uint32 r= ((gRSP.ambientLightColor>>16)&0xFF);
2245                 uint32 g= ((gRSP.ambientLightColor>> 8)&0xFF);
2246                 uint32 b= ((gRSP.ambientLightColor    )&0xFF);
2247                 for( uint32 k=1; k<=gRSPnumLights; k++)
2248                 {
2249                     r += gRSPlights[k].r;
2250                     g += gRSPlights[k].g;
2251                     b += gRSPlights[k].b;
2252                 }
2253                 if( r>255 ) r=255;
2254                 if( g>255 ) g=255;
2255                 if( b>255 ) b=255;
2256                 r *= vert.rgba.r ;
2257                 g *= vert.rgba.g ;
2258                 b *= vert.rgba.b ;
2259                 r >>= 8;
2260                 g >>= 8;
2261                 b >>= 8;
2262                 g_dwVtxDifColor[i] = 0xFF000000;
2263                 g_dwVtxDifColor[i] |= (r<<16);
2264                 g_dwVtxDifColor[i] |= (g<< 8);
2265                 g_dwVtxDifColor[i] |= (b    );          
2266             }
2267
2268             *(((uint8*)&(g_dwVtxDifColor[i]))+3) = vert.rgba.a; // still use alpha from the vertex
2269         }
2270         else
2271         {
2272             if( (gRDP.geometryMode & G_SHADE) == 0 && gRSP.ucode < 5 )  //Shade is disabled
2273             {
2274                 g_dwVtxDifColor[i] = gRDP.primitiveColor;
2275             }
2276             else    //FLAT shade
2277             {
2278                 g_dwVtxDifColor[i] = COLOR_RGBA(vert.rgba.r, vert.rgba.g, vert.rgba.b, vert.rgba.a);
2279             }
2280         }
2281
2282         if( options.bWinFrameMode )
2283         {
2284             //g_vecProjected[i].z = 0;
2285             g_dwVtxDifColor[i] = COLOR_RGBA(vert.rgba.r, vert.rgba.g, vert.rgba.b, vert.rgba.a);
2286         }
2287
2288         ReplaceAlphaWithFogFactor(i);
2289
2290         // Update texture coords n.b. need to divide tu/tv by bogus scale on addition to buffer
2291         //VECTOR2 & t = g_fVtxTxtCoords[i];
2292
2293         // If the vert is already lit, then there is no normal (and hence we
2294         // can't generate tex coord)
2295         if (gRSP.bTextureGen && gRSP.bLightingEnable )
2296         {
2297                 g_normal.x = (float)*(char*)(g_pRDRAMu8+ (((i<<1)+0)^3)+dwConkerVtxZAddr);
2298                 g_normal.y = (float)*(char*)(g_pRDRAMu8+ (((i<<1)+1)^3)+dwConkerVtxZAddr);
2299                 g_normal.z = (float)*(char*)(g_pRDRAMu8+ (((i<<1)+2)^3)+dwConkerVtxZAddr);
2300                 Vec3TransformNormal(g_normal, gRSPmodelViewTop);
2301                 TexGen(g_fVtxTxtCoords[i].x, g_fVtxTxtCoords[i].y);
2302         }
2303         else
2304         {
2305             g_fVtxTxtCoords[i].x = (float)vert.tu;
2306             g_fVtxTxtCoords[i].y = (float)vert.tv; 
2307         }
2308     }
2309
2310     VTX_DUMP(TRACE2("Setting Vertexes: %d - %d\n", dwV0, dwV0+dwNum-1));
2311     DEBUGGER_PAUSE_AND_DUMP(NEXT_VERTEX_CMD,{DebuggerAppendMsg("Paused at Vertex Cmd");});
2312 }
2313
2314
2315 typedef struct{
2316     short y;
2317     short x;
2318     short flag;
2319     short z;
2320 } RS_Vtx_XYZ;
2321
2322 typedef union {
2323     struct {
2324         uint8 a;
2325         uint8 b;
2326         uint8 g;
2327         uint8 r;
2328     };
2329     struct {
2330         char na;    //a
2331         char nz;    //b
2332         char ny;    //g
2333         char nx;    //r
2334     };
2335 } RS_Vtx_Color;
2336
2337
2338 void ProcessVertexData_Rogue_Squadron(uint32 dwXYZAddr, uint32 dwColorAddr, uint32 dwXYZCmd, uint32 dwColorCmd)
2339 {
2340     UpdateCombinedMatrix();
2341
2342     uint32 dwV0 = 0;
2343     uint32 dwNum = (dwXYZCmd&0xFF00)>>10;
2344
2345     RS_Vtx_XYZ * pVtxXYZBase = (RS_Vtx_XYZ*)(g_pRDRAMu8 + dwXYZAddr);
2346     RS_Vtx_Color * pVtxColorBase = (RS_Vtx_Color*)(g_pRDRAMu8 + dwColorAddr);
2347
2348     uint32 i;
2349     for (i = dwV0; i < dwV0 + dwNum; i++)
2350     {
2351         RS_Vtx_XYZ & vertxyz = pVtxXYZBase[i - dwV0];
2352         RS_Vtx_Color & vertcolors = pVtxColorBase[i - dwV0];
2353
2354         g_vtxNonTransformed[i].x = (float)vertxyz.x;
2355         g_vtxNonTransformed[i].y = (float)vertxyz.y;
2356         g_vtxNonTransformed[i].z = (float)vertxyz.z;
2357
2358 #if !defined(NO_ASM)
2359         if( status.isSSEEnabled )
2360             SSEVec3Transform(i);
2361         else
2362 #endif
2363         {
2364             Vec3Transform(&g_vtxTransformed[i], (XVECTOR3*)&g_vtxNonTransformed[i], &gRSPworldProject); // Convert to w=1
2365             g_vecProjected[i].w = 1.0f / g_vtxTransformed[i].w;
2366             g_vecProjected[i].x = g_vtxTransformed[i].x * g_vecProjected[i].w;
2367             g_vecProjected[i].y = g_vtxTransformed[i].y * g_vecProjected[i].w;
2368             g_vecProjected[i].z = g_vtxTransformed[i].z * g_vecProjected[i].w;
2369         }
2370
2371         VTX_DUMP( 
2372         {
2373             DebuggerAppendMsg("      : %f, %f, %f, %f", 
2374                 g_vtxTransformed[i].x,g_vtxTransformed[i].y,g_vtxTransformed[i].z,g_vtxTransformed[i].w);
2375             DebuggerAppendMsg("      : %f, %f, %f, %f", 
2376                 g_vecProjected[i].x,g_vecProjected[i].y,g_vecProjected[i].z,g_vecProjected[i].w);
2377         });
2378
2379         g_fFogCoord[i] = g_vecProjected[i].z;
2380         if( g_vecProjected[i].w < 0 || g_vecProjected[i].z < 0 || g_fFogCoord[i] < gRSPfFogMin )
2381             g_fFogCoord[i] = gRSPfFogMin;
2382
2383         RSP_Vtx_Clipping(i);
2384
2385         if( gRSP.bLightingEnable )
2386         {
2387             g_normal.x = (float)vertcolors.nx;
2388             g_normal.y = (float)vertcolors.ny;
2389             g_normal.z = (float)vertcolors.nz;
2390
2391 #if !defined(NO_ASM)
2392             if( status.isSSEEnabled )
2393             {
2394                 SSEVec3TransformNormal();
2395                 g_dwVtxDifColor[i] = SSELightVert();
2396             }
2397             else
2398 #endif
2399             {
2400                 Vec3TransformNormal(g_normal, gRSPmodelViewTop);
2401                 g_dwVtxDifColor[i] = LightVert(g_normal, i);
2402             }
2403             *(((uint8*)&(g_dwVtxDifColor[i]))+3) = vertcolors.a;    // still use alpha from the vertex
2404         }
2405         else
2406         {
2407             if( (gRDP.geometryMode & G_SHADE) == 0 && gRSP.ucode < 5 )  //Shade is disabled
2408             {
2409                 g_dwVtxDifColor[i] = gRDP.primitiveColor;
2410             }
2411             else    //FLAT shade
2412             {
2413                 g_dwVtxDifColor[i] = COLOR_RGBA(vertcolors.r, vertcolors.g, vertcolors.b, vertcolors.a);
2414             }
2415         }
2416
2417         if( options.bWinFrameMode )
2418         {
2419             g_dwVtxDifColor[i] = COLOR_RGBA(vertcolors.r, vertcolors.g, vertcolors.b, vertcolors.a);
2420         }
2421
2422         ReplaceAlphaWithFogFactor(i);
2423
2424         /*
2425         // Update texture coords n.b. need to divide tu/tv by bogus scale on addition to buffer
2426         VECTOR2 & t = g_fVtxTxtCoords[i];
2427
2428         // If the vert is already lit, then there is no normal (and hence we
2429         // can't generate tex coord)
2430         if (gRSP.bTextureGen && gRSP.bLightingEnable && g_textures[gRSP.curTile].m_bTextureEnable )
2431         {
2432             TexGen(g_fVtxTxtCoords[i].x, g_fVtxTxtCoords[i].y);
2433         }
2434         else
2435         {
2436             t.x = (float)vert.tu;
2437             t.y = (float)vert.tv; 
2438         }
2439         */
2440     }
2441
2442     VTX_DUMP(TRACE2("Setting Vertexes: %d - %d\n", dwV0, dwV0+dwNum-1));
2443     DEBUGGER_PAUSE_AND_DUMP(NEXT_VERTEX_CMD,{TRACE0("Paused at Vertex Cmd");});
2444 }
2445
2446 void SetLightCol(uint32 dwLight, uint32 dwCol)
2447 {
2448     gRSPlights[dwLight].r = (uint8)((dwCol >> 24)&0xFF);
2449     gRSPlights[dwLight].g = (uint8)((dwCol >> 16)&0xFF);
2450     gRSPlights[dwLight].b = (uint8)((dwCol >>  8)&0xFF);
2451     gRSPlights[dwLight].a = 255;    // Ignore light alpha
2452     gRSPlights[dwLight].fr = (float)gRSPlights[dwLight].r;
2453     gRSPlights[dwLight].fg = (float)gRSPlights[dwLight].g;
2454     gRSPlights[dwLight].fb = (float)gRSPlights[dwLight].b;
2455     gRSPlights[dwLight].fa = 255;   // Ignore light alpha
2456
2457     //TRACE1("Set light %d color", dwLight);
2458     LIGHT_DUMP(TRACE2("Set Light %d color: %08X", dwLight, dwCol));
2459 }
2460
2461 void SetLightDirection(uint32 dwLight, float x, float y, float z, float range)
2462 {
2463     //gRSP.bLightIsUpdated = true;
2464
2465     //gRSPlights[dwLight].ox = x;
2466     //gRSPlights[dwLight].oy = y;
2467     //gRSPlights[dwLight].oz = z;
2468
2469     register float w = range == 0 ? (float)sqrt(x*x+y*y+z*z) : 1;
2470
2471     gRSPlights[dwLight].x = x/w;
2472     gRSPlights[dwLight].y = y/w;
2473     gRSPlights[dwLight].z = z/w;
2474     gRSPlights[dwLight].range = range;
2475     DEBUGGER_PAUSE_AND_DUMP(NEXT_SET_LIGHT,TRACE5("Set Light %d dir: %.4f, %.4f, %.4f, %.4f", dwLight, x, y, z, range));
2476 }
2477
2478 static float maxS0, maxT0;
2479 static float maxS1, maxT1;
2480 static bool validS0, validT0;
2481 static bool validS1, validT1;
2482
2483 void LogTextureCoords(float fTex0S, float fTex0T, float fTex1S, float fTex1T)
2484 {
2485     if( validS0 )
2486     {
2487         if( fTex0S<0 || fTex0S>maxS0 )  validS0 = false;
2488     }
2489     if( validT0 )
2490     {
2491         if( fTex0T<0 || fTex0T>maxT0 )  validT0 = false;
2492     }
2493     if( validS1 )
2494     {
2495         if( fTex1S<0 || fTex1S>maxS1 )  validS1 = false;
2496     }
2497     if( validT1 )
2498     {
2499         if( fTex1T<0 || fTex1T>maxT1 )  validT1 = false;
2500     }
2501 }
2502
2503 bool CheckTextureCoords(int tex)
2504 {
2505     if( tex==0 )
2506     {
2507         return validS0&&validT0;
2508     }
2509     else
2510     {
2511         return validS1&&validT1;
2512     }
2513 }
2514
2515 void ResetTextureCoordsLog(float maxs0, float maxt0, float maxs1, float maxt1)
2516 {
2517     maxS0 = maxs0;
2518     maxT0 = maxt0;
2519     maxS1 = maxs1;
2520     maxT1 = maxt1;
2521     validS0 = validT0 = true;
2522     validS1 = validT1 = true;
2523 }
2524
2525 void ForceMainTextureIndex(int dwTile) 
2526 {
2527     if( dwTile == 1 && !(CRender::g_pRender->IsTexel0Enable()) && CRender::g_pRender->IsTexel1Enable() )
2528     {
2529         // Hack
2530         gRSP.curTile = 0;
2531     }
2532     else
2533     {
2534         gRSP.curTile = dwTile;
2535     }
2536 }
2537
2538 float HackZ2(float z)
2539 {
2540     z = (z+9)/10;
2541     return z;
2542 }
2543
2544 float HackZ(float z)
2545 {
2546     return HackZ2(z);
2547
2548     if( z < 0.1 && z >= 0 )
2549         z = (.1f+z)/2;
2550     else if( z < 0 )
2551         //return (10+z)/100;
2552         z = (expf(z)/20);
2553     return z;
2554 }
2555
2556 void HackZ(std::vector<XVECTOR3>& points)
2557 {
2558     int size = points.size();
2559     for( int i=0; i<size; i++)
2560     {
2561         XVECTOR3 &v = points[i];
2562         v.z = (float)HackZ(v.z);
2563     }
2564 }
2565
2566 void HackZAll()
2567 {
2568     if( CDeviceBuilder::m_deviceGeneralType == DIRECTX_DEVICE )
2569     {
2570         for( uint32 i=0; i<gRSP.numVertices; i++)
2571         {
2572             g_vtxBuffer[i].z = HackZ(g_vtxBuffer[i].z);
2573         }
2574     }
2575     else
2576     {
2577         for( uint32 i=0; i<gRSP.numVertices; i++)
2578         {
2579             float w = g_vtxProjected5[i][3];
2580             g_vtxProjected5[i][2] = HackZ(g_vtxProjected5[i][2]/w)*w;
2581         }
2582     }
2583 }
2584
2585
2586 extern XMATRIX reverseXY;
2587 extern XMATRIX reverseY;
2588
2589 void UpdateCombinedMatrix()
2590 {
2591     if( gRSP.bMatrixIsUpdated )
2592     {
2593         gRSPworldProject = gRSP.modelviewMtxs[gRSP.modelViewMtxTop] * gRSP.projectionMtxs[gRSP.projectionMtxTop];
2594         gRSP.bMatrixIsUpdated = false;
2595         gRSP.bCombinedMatrixIsUpdated = true;
2596     }
2597
2598     if( gRSP.bCombinedMatrixIsUpdated )
2599     {
2600         if( options.enableHackForGames == HACK_REVERSE_XY_COOR )
2601         {
2602             gRSPworldProject = gRSPworldProject * reverseXY;
2603         }
2604         if( options.enableHackForGames == HACK_REVERSE_Y_COOR )
2605         {
2606             gRSPworldProject = gRSPworldProject * reverseY;
2607         }
2608 #if !defined(NO_ASM)
2609         if( status.isSSEEnabled )
2610         {
2611             MatrixTranspose(&gRSPworldProjectTransported, &gRSPworldProject);
2612         }
2613 #endif
2614         gRSP.bCombinedMatrixIsUpdated = false;
2615     }
2616
2617     //if( gRSP.bWorldMatrixIsUpdated || gRSP.bLightIsUpdated )
2618     //{
2619     //  // Update lights with transported world matrix
2620     //  for( unsigned int l=0; l<gRSPnumLights; l++)
2621     //  {
2622     //      Vec3TransformCoord(&gRSPlights[l].td, &gRSPlights[l].od, &gRSPmodelViewTopTranspose);
2623     //      Vec3Normalize(&gRSPlights[l].td,&gRSPlights[l].td);
2624     //  }
2625
2626     //  gRSP.bWorldMatrixIsUpdated = false;
2627     //  gRSP.bLightIsUpdated = false;
2628     //}
2629 }
2630