2 Copyright (C) 2003 Rice1964
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.
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.
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.
23 #include "osal_preproc.h"
25 #include "DeviceBuilder.h"
26 #include "VertexShaderConstantDef.h"
30 extern FiddledVtx * g_pVtxBase;
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
40 #ifdef ENABLE_CLIP_TRI
42 inline void RSP_Vtx_Clipping(int i)
46 if( g_vecProjected[i].w > 0 )
51 if( g_vecProjected[i].x > 1 )
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;
58 if( g_vecProjected[i].x < -1 )
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;
65 if( g_vecProjected[i].y > 1 )
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;
72 if( g_vecProjected[i].y < -1 )
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;
79 //if( g_vecProjected[i].z > 1.0f )
81 // g_clipFlag2[i] |= Z_CLIP_MAX;
82 // g_clipFlag[i] |= Z_CLIP_MAX;
85 //if( gRSP.bNearClip && g_vecProjected[i].z < -1.0f )
87 // g_clipFlag2[i] |= Z_CLIP_MIN;
88 // g_clipFlag[i] |= Z_CLIP_MIN;
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;
106 inline void RSP_Vtx_Clipping(int i) {}
112 ALIGN(16,RSP_Options gRSP)
113 ALIGN(16,RDP_Options gRDP)
115 static ALIGN(16,XVECTOR4 g_normal)
116 //static int norms[3];
118 ALIGN(16,XVECTOR4 g_vtxNonTransformed[MAX_VERTS])
119 ALIGN(16,XVECTOR4 g_vecProjected[MAX_VERTS])
120 ALIGN(16,XVECTOR4 g_vtxTransformed[MAX_VERTS])
122 float g_vtxProjected5[1000][5];
123 float g_vtxProjected5Clipped[2000][5];
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];
133 EXTERNAL_VERTEX g_vtxForExternal[MAX_VERTS];
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;
145 float gRSPfFogDivider;
147 uint32 gRSPnumLights;
148 Light gRSPlights[16];
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)
156 N64Light gRSPn64lights[16];
159 void (*ProcessVertexData)(uint32 dwAddr, uint32 dwV0, uint32 dwNum)=NULL;
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);*/
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 \
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*/
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*/
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)*/
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)*/
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)*/
205 __asm fxch st(2) \ /* (x m00 + y m10 + z m20) (x m01 + ym11 + z m21) (x m02 + y m12 + z m 22) */
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 */
215 __asm fxch st(2) \ /* xx yy zz 1 x y z */
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 */
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 */
225 __asm fsqrt \ /* l 1 x y z */
227 __asm fdivp st(1),st(0) \ /* (1/l) x y z */
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 */
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 */
238 __asm mov dword ptr [vec + 0], 0 \
239 __asm mov dword ptr [vec + 4], 0 \
240 __asm mov dword ptr [vec + 8], 0 \
244 #else // use C code in other cases, this is probably faster anyway
245 #define Vec3TransformNormal(vec, m) \
247 temp.x = (vec.x * m._11) + (vec.y * m._21) + (vec.z * m._31); \
248 temp.y = (vec.x * m._12) + (vec.y * m._22) + (vec.z * m._32); \
249 temp.z = (vec.x * m._13) + (vec.y * m._23) + (vec.z * m._33); \
250 float norm = sqrt(temp.x*temp.x+temp.y*temp.y+temp.z*temp.z); \
251 if (norm == 0.0) { vec.x = 0.0; vec.y = 0.0; vec.z = 0.0;} else \
252 { vec.x = temp.x/norm; vec.y = temp.y/norm; vec.z = temp.z/norm; }
256 #if !defined(__GNUC__) && !defined(NO_ASM)
257 __declspec( naked ) void __fastcall SSEVec3Transform(int i)
261 shl ecx,4; // ecx = i
263 movaps xmm1, DWORD PTR g_vtxNonTransformed [ecx]; // xmm1 as original vector
265 movaps xmm4, DWORD PTR gRSPworldProjectTransported; // row1
266 movaps xmm5, DWORD PTR gRSPworldProjectTransported[0x10]; // row2
267 movaps xmm6, DWORD PTR gRSPworldProjectTransported[0x20]; // row3
268 movaps xmm7, DWORD PTR gRSPworldProjectTransported[0x30]; // row4
270 mulps xmm4, xmm1; // row 1
271 mulps xmm5, xmm1; // row 2
272 mulps xmm6, xmm1; // row 3
273 mulps xmm7, xmm1; // row 4
275 movhlps xmm0, xmm4; // xmm4 high to xmm0 low
276 movlhps xmm0, xmm5; // xmm5 low to xmm0 high
278 addps xmm4, xmm0; // result of add are in xmm4 low
279 addps xmm5, xmm0; // result of add are in xmm5 high
281 shufps xmm0, xmm4, 0x44; // move xmm4 low DWORDs to xmm0 high
282 shufps xmm4, xmm5, 0xe4; // move xmm5 high DWORS to xmm4
283 movhlps xmm5, xmm0; // xmm4, xmm5 are mirrored
285 shufps xmm4, xmm4, 0x08; // move xmm4's 3rd uint32 to its 2nd uint32
286 shufps xmm5, xmm5, 0x0d; // move xmm5's 4th uint32 to its 2nd uint32,
287 // and move its 2nd uint32 to its 1st uint32
289 addps xmm4, xmm5; // results are in 1st and 2nd uint32
292 movhlps xmm0, xmm6; // xmm6 high to xmm0 low
293 movlhps xmm0, xmm7; // xmm7 low to xmm0 high
295 addps xmm6, xmm0; // result of add are in xmm6 low
296 addps xmm7, xmm0; // result of add are in xmm7 high
298 shufps xmm0, xmm6, 0x44; // move xmm6 low DWORDs to xmm0 high
299 shufps xmm6, xmm7, 0xe4; // move xmm7 high DWORS to xmm6
300 movhlps xmm7, xmm0; // xmm6, xmm7 are mirrored
302 shufps xmm6, xmm6, 0x08; // move xmm6's 3rd uint32 to its 2nd uint32
303 shufps xmm7, xmm7, 0x0d; // move xmm7's 4th uint32 to its 2nd uint32,
304 // and move its 2nd uint32 to its 1st uint32
306 addps xmm6, xmm7; // results are in 1st and 2nd uint32
308 movlhps xmm4, xmm6; // final result is in xmm4
309 movaps DWORD PTR g_vtxTransformed [ecx], xmm4;
312 shufps xmm0,xmm0,0xff;
316 shufps xmm0,xmm0,0xe8;
319 movaps DWORD PTR g_vecProjected [ecx], xmm4;
327 __declspec( naked ) void __fastcall SSEVec3TransformDKR(XVECTOR4 &pOut, const XVECTOR4 &pV)
331 movaps xmm1, DWORD PTR [edx]; // xmm1 as original vector
333 movaps xmm4, DWORD PTR dkrMatrixTransposed; // row1
334 movaps xmm5, DWORD PTR dkrMatrixTransposed[0x10]; // row2
335 movaps xmm6, DWORD PTR dkrMatrixTransposed[0x20]; // row3
336 movaps xmm7, DWORD PTR dkrMatrixTransposed[0x30]; // row4
338 mulps xmm4, xmm1; // row 1
339 mulps xmm5, xmm1; // row 2
340 mulps xmm6, xmm1; // row 3
341 mulps xmm7, xmm1; // row 4
343 movhlps xmm0, xmm4; // xmm4 high to xmm0 low
344 movlhps xmm0, xmm5; // xmm5 low to xmm0 high
346 addps xmm4, xmm0; // result of add are in xmm4 low
347 addps xmm5, xmm0; // result of add are in xmm5 high
349 shufps xmm0, xmm4, 0x44; // move xmm4 low DWORDs to xmm0 high
350 shufps xmm4, xmm5, 0xe4; // move xmm5 high DWORS to xmm4
351 movhlps xmm5, xmm0; // xmm4, xmm5 are mirrored
353 shufps xmm4, xmm4, 0x08; // move xmm4's 3rd uint32 to its 2nd uint32
354 shufps xmm5, xmm5, 0x0d; // move xmm5's 4th uint32 to its 2nd uint32,
355 // and move its 2nd uint32 to its 1st uint32
357 addps xmm4, xmm5; // results are in 1st and 2nd uint32
360 movhlps xmm0, xmm6; // xmm6 high to xmm0 low
361 movlhps xmm0, xmm7; // xmm7 low to xmm0 high
363 addps xmm6, xmm0; // result of add are in xmm6 low
364 addps xmm7, xmm0; // result of add are in xmm7 high
366 shufps xmm0, xmm6, 0x44; // move xmm6 low DWORDs to xmm0 high
367 shufps xmm6, xmm7, 0xe4; // move xmm7 high DWORS to xmm6
368 movhlps xmm7, xmm0; // xmm6, xmm7 are mirrored
370 shufps xmm6, xmm6, 0x08; // move xmm6's 3rd uint32 to its 2nd uint32
371 shufps xmm7, xmm7, 0x0d; // move xmm7's 4th uint32 to its 2nd uint32,
372 // and move its 2nd uint32 to its 1st uint32
374 addps xmm6, xmm7; // results are in 1st and 2nd uint32
376 movlhps xmm4, xmm6; // final result is in xmm4
377 movaps DWORD PTR [ecx], xmm4;
383 #elif defined(__GNUC__) && defined(__x86_64__) && !defined(NO_ASM)
384 void SSEVec3Transform(int i)
386 asm volatile(" shl $4, %0 \n"
387 " movslq %k0, %q0 \n"
388 " movaps (%1,%q0), %%xmm1 \n"
389 " movaps 0(%2), %%xmm4 \n"
390 " movaps 16(%2), %%xmm5 \n"
391 " movaps 32(%2), %%xmm6 \n"
392 " movaps 48(%2), %%xmm7 \n"
393 " mulps %%xmm1, %%xmm4 \n"
394 " mulps %%xmm1, %%xmm5 \n"
395 " mulps %%xmm1, %%xmm6 \n"
396 " mulps %%xmm1, %%xmm7 \n"
397 " movhlps %%xmm4, %%xmm0 \n"
398 " movlhps %%xmm5, %%xmm0 \n"
399 " addps %%xmm0, %%xmm4 \n"
400 " addps %%xmm0, %%xmm5 \n"
401 " shufps $0x44, %%xmm4, %%xmm0 \n"
402 " shufps $0xe4, %%xmm5, %%xmm4 \n"
403 " movhlps %%xmm0, %%xmm5 \n"
404 " shufps $0x08, %%xmm4, %%xmm4 \n"
405 " shufps $0x0d, %%xmm5, %%xmm5 \n"
406 " addps %%xmm5, %%xmm4 \n"
407 " movhlps %%xmm6, %%xmm0 \n"
408 " movlhps %%xmm7, %%xmm0 \n"
409 " addps %%xmm0, %%xmm6 \n"
410 " addps %%xmm0, %%xmm7 \n"
411 " shufps $0x44, %%xmm6, %%xmm0 \n"
412 " shufps $0xe4, %%xmm7, %%xmm6 \n"
413 " movhlps %%xmm0, %%xmm7 \n"
414 " shufps $0x08, %%xmm6, %%xmm6 \n"
415 " shufps $0x0d, %%xmm7, %%xmm7 \n"
416 " addps %%xmm7, %%xmm6 \n"
417 " movlhps %%xmm6, %%xmm4 \n"
418 " movaps %%xmm4, (%3,%q0) \n"
419 " movaps %%xmm4, %%xmm0 \n"
420 " shufps $0xff, %%xmm0, %%xmm0 \n"
421 " divps %%xmm0, %%xmm4 \n"
422 " rcpps %%xmm0, %%xmm0 \n"
423 " movhlps %%xmm4, %%xmm0 \n"
424 " shufps $0xe8, %%xmm0, %%xmm0 \n"
425 " movlhps %%xmm0, %%xmm4 \n"
426 " movaps %%xmm4, (%4,%q0) \n"
428 : "r"(g_vtxNonTransformed), "r"(&gRSPworldProjectTransported.m[0][0]), "r"(g_vtxTransformed), "r"(g_vecProjected)
429 : "memory", "%xmm0", "%xmm1", "%xmm4", "%xmm5", "%xmm6", "%xmm7"
432 #elif !defined(NO_ASM) // 32-bit GCC assumed
433 void SSEVec3Transform(int i)
435 asm volatile(" shl $4, %0 \n"
436 " movaps (%1,%0), %%xmm1 \n"
437 " movaps 0(%2), %%xmm4 \n"
438 " movaps 16(%2), %%xmm5 \n"
439 " movaps 32(%2), %%xmm6 \n"
440 " movaps 48(%2), %%xmm7 \n"
441 " mulps %%xmm1, %%xmm4 \n"
442 " mulps %%xmm1, %%xmm5 \n"
443 " mulps %%xmm1, %%xmm6 \n"
444 " mulps %%xmm1, %%xmm7 \n"
445 " movhlps %%xmm4, %%xmm0 \n"
446 " movlhps %%xmm5, %%xmm0 \n"
447 " addps %%xmm0, %%xmm4 \n"
448 " addps %%xmm0, %%xmm5 \n"
449 " shufps $0x44, %%xmm4, %%xmm0 \n"
450 " shufps $0xe4, %%xmm5, %%xmm4 \n"
451 " movhlps %%xmm0, %%xmm5 \n"
452 " shufps $0x08, %%xmm4, %%xmm4 \n"
453 " shufps $0x0d, %%xmm5, %%xmm5 \n"
454 " addps %%xmm5, %%xmm4 \n"
455 " movhlps %%xmm6, %%xmm0 \n"
456 " movlhps %%xmm7, %%xmm0 \n"
457 " addps %%xmm0, %%xmm6 \n"
458 " addps %%xmm0, %%xmm7 \n"
459 " shufps $0x44, %%xmm6, %%xmm0 \n"
460 " shufps $0xe4, %%xmm7, %%xmm6 \n"
461 " movhlps %%xmm0, %%xmm7 \n"
462 " shufps $0x08, %%xmm6, %%xmm6 \n"
463 " shufps $0x0d, %%xmm7, %%xmm7 \n"
464 " addps %%xmm7, %%xmm6 \n"
465 " movlhps %%xmm6, %%xmm4 \n"
466 " movaps %%xmm4, (%3,%0) \n"
467 " movaps %%xmm4, %%xmm0 \n"
468 " shufps $0xff, %%xmm0, %%xmm0 \n"
469 " divps %%xmm0, %%xmm4 \n"
470 " rcpps %%xmm0, %%xmm0 \n"
471 " movhlps %%xmm4, %%xmm0 \n"
472 " shufps $0xe8, %%xmm0, %%xmm0 \n"
473 " movlhps %%xmm0, %%xmm4 \n"
474 " movaps %%xmm4, (%4,%0) \n"
476 : "r"(g_vtxNonTransformed), "r"(&gRSPworldProjectTransported.m[0][0]), "r"(g_vtxTransformed), "r"(g_vecProjected)
477 : "memory", "%xmm0", "%xmm1", "%xmm4", "%xmm5", "%xmm6", "%xmm7"
481 float real255 = 255.0f;
482 float real128 = 128.0f;
484 #if !defined(__GNUC__) && !defined(NO_ASM)
485 __declspec( naked ) void __fastcall SSEVec3TransformNormal()
489 mov DWORD PTR [g_normal][12], 0;
491 movaps xmm4, DWORD PTR gRSPmodelViewTopTranspose; // row1
492 movaps xmm5, DWORD PTR gRSPmodelViewTopTranspose[0x10]; // row2
493 movaps xmm1, DWORD PTR [g_normal]; // xmm1 as the normal vector
494 movaps xmm6, DWORD PTR gRSPmodelViewTopTranspose[0x20]; // row3
496 mulps xmm4, xmm1; // row 1
497 mulps xmm5, xmm1; // row 2
498 mulps xmm6, xmm1; // row 3
500 movhlps xmm0, xmm4; // xmm4 high to xmm0 low
501 movlhps xmm0, xmm5; // xmm5 low to xmm0 high
503 addps xmm4, xmm0; // result of add are in xmm4 low
504 addps xmm5, xmm0; // result of add are in xmm5 high
506 shufps xmm0, xmm4, 0x44; // move xmm4 low DWORDs to xmm0 high
507 shufps xmm4, xmm5, 0xe4; // move xmm5 high DWORS to xmm4
508 movhlps xmm5, xmm0; // xmm4, xmm5 are mirrored
510 shufps xmm4, xmm4, 0x08; // move xmm4's 3rd uint32 to its 2nd uint32
511 shufps xmm5, xmm5, 0x0d; // move xmm5's 4th uint32 to its 2nd uint32,
513 addps xmm4, xmm5; // results are in 1st and 2nd uint32
516 mulps xmm1,xmm1; //square
518 shufps xmm7, xmm7,0x03;
521 movhlps xmm0, xmm6; // xmm6 high to xmm0 low
522 addps xmm6, xmm0; // result of add are in xmm6 low
525 shufps xmm0, xmm0, 0x03;
526 addss xmm0, xmm6; // result of add is at xmm0's 1st uint32
531 addss xmm7,xmm0; // xmm7 1st uint32 is the sum of squares
534 movaps DWORD PTR [g_normal], xmm4;
535 movss DWORD PTR [g_normal][12], xmm7;
544 movss DWORD PTR [g_normal][12], xmm7;
548 movaps DWORD PTR [g_normal], xmm4; // Normalized
549 mov DWORD PTR [g_normal][12], 0;
554 movss DWORD PTR [g_normal], xmm0;
555 movss DWORD PTR [g_normal][12], xmm0;
560 #elif defined(__GNUC__) && !defined(NO_ASM) // this code should compile for both 64-bit and 32-bit architectures
561 void SSEVec3TransformNormal(void)
563 asm volatile(" movl $0, 12(%0) \n"
564 " movaps (%1), %%xmm4 \n"
565 " movaps 16(%1), %%xmm5 \n"
566 " movaps (%0), %%xmm1 \n"
567 " movaps 32(%1), %%xmm6 \n"
568 " mulps %%xmm1, %%xmm4 \n"
569 " mulps %%xmm1, %%xmm5 \n"
570 " mulps %%xmm1, %%xmm6 \n"
571 " movhlps %%xmm4, %%xmm0 \n"
572 " movlhps %%xmm5, %%xmm0 \n"
573 " addps %%xmm0, %%xmm4 \n"
574 " addps %%xmm0, %%xmm5 \n"
575 " shufps $0x44, %%xmm4, %%xmm0 \n"
576 " shufps $0xe4, %%xmm5, %%xmm4 \n"
577 " movhlps %%xmm0, %%xmm5 \n"
578 " shufps $0x08, %%xmm4, %%xmm4 \n"
579 " shufps $0x0d, %%xmm5, %%xmm5 \n"
580 " addps %%xmm5, %%xmm4 \n"
581 " movaps %%xmm4, %%xmm1 \n"
582 " mulps %%xmm1, %%xmm1 \n"
583 " movlhps %%xmm1, %%xmm7 \n"
584 " shufps $0x03, %%xmm7, %%xmm7 \n"
585 " addss %%xmm1, %%xmm7 \n"
586 " movhlps %%xmm6, %%xmm0 \n"
587 " addps %%xmm0, %%xmm6 \n"
588 " movlhps %%xmm6, %%xmm0 \n"
589 " shufps $0x03, %%xmm0, %%xmm0 \n"
590 " addss %%xmm6, %%xmm0 \n"
591 " movlhps %%xmm0, %%xmm4 \n"
592 " mulss %%xmm0, %%xmm0 \n"
593 " addss %%xmm0, %%xmm7 \n"
595 " movaps %%xmm4, (%0) \n"
596 " movss %%xmm7, 12(%0) \n"
598 " xorps %%xmm0, %%xmm0 \n"
599 " ucomiss %%xmm7, %%xmm0 \n"
601 " rsqrtss %%xmm7, %%xmm7 \n"
602 " shufps $0x00, %%xmm7, %%xmm7 \n"
604 " movss %%xmm7, 12(%0) \n"
606 " mulps %%xmm7, %%xmm4 \n"
607 " movaps %%xmm4, (%0) \n"
608 " movl $0, 12(%0) \n"
611 " movss %%xmm0, (%0) \n"
612 " movss %%xmm0, 12(%0) \n"
615 : "r"(&g_normal.x), "r"(&gRSPmodelViewTopTranspose.m[0][0])
616 : "memory", "cc", "%xmm0", "%xmm1", "%xmm4", "%xmm5", "%xmm6", "%xmm7"
621 void NormalizeNormalVec()
623 float w = 1/sqrtf(g_normal.x*g_normal.x + g_normal.y*g_normal.y + g_normal.z*g_normal.z);
630 void InitRenderBase()
633 if( status.isSSEEnabled && !g_curRomInfo.bPrimaryDepthHack && options.enableHackForGames != HACK_FOR_NASCAR)
635 ProcessVertexData = ProcessVertexDataSSE;
638 #elif defined(__ARM_NEON__)
639 if( !g_curRomInfo.bPrimaryDepthHack && options.enableHackForGames != HACK_FOR_NASCAR && options.enableHackForGames != HACK_FOR_ZELDA_MM && !options.bWinFrameMode)
641 ProcessVertexData = ProcessVertexDataNEON;
646 ProcessVertexData = ProcessVertexDataNoSSE;
649 gRSPfFogMin = gRSPfFogMax = 0.0f;
650 windowSetting.fMultX = windowSetting.fMultY = 2.0f;
651 windowSetting.vpLeftW = windowSetting.vpTopW = 0;
652 windowSetting.vpRightW = windowSetting.vpWidthW = 640;
653 windowSetting.vpBottomW = windowSetting.vpHeightW = 480;
655 gRSP.nVPLeftN = gRSP.nVPTopN = 0;
656 gRSP.nVPRightN = 640;
657 gRSP.nVPBottomN = 640;
658 gRSP.nVPWidthN = 640;
659 gRSP.nVPHeightN = 640;
660 gRDP.scissor.left=gRDP.scissor.top=0;
661 gRDP.scissor.right=gRDP.scissor.bottom=640;
663 gRSP.bLightingEnable = gRSP.bTextureGen = false;
664 gRSP.curTile=gRSPnumLights=gRSP.ambientLightColor=gRSP.ambientLightIndex= 0;
665 gRSP.fAmbientLightR=gRSP.fAmbientLightG=gRSP.fAmbientLightB=0;
666 gRSP.projectionMtxTop = gRSP.modelViewMtxTop = 0;
667 gRDP.fogColor = gRDP.primitiveColor = gRDP.envColor = gRDP.primitiveDepth = gRDP.primLODMin = gRDP.primLODFrac = gRDP.LODFrac = 0;
668 gRDP.fPrimitiveDepth = 0;
669 gRSP.numVertices = 0;
670 gRSP.maxVertexID = 0;
671 gRSP.bCullFront=false;
673 gRSP.bFogEnabled=gRDP.bFogEnableInBlender=false;
674 gRSP.bZBufferEnabled=true;
675 gRSP.shadeMode=SHADE_SMOOTH;
676 gRDP.keyR=gRDP.keyG=gRDP.keyB=gRDP.keyA=gRDP.keyRGB=gRDP.keyRGBA = 0;
678 gRSP.DKRCMatrixIndex = gRSP.dwDKRVtxAddr = gRSP.dwDKRMatrixAddr = 0;
679 gRSP.DKRBillBoard = false;
681 gRSP.fTexScaleX = 1/32.0f;
682 gRSP.fTexScaleY = 1/32.0f;
683 gRSP.bTextureEnabled = FALSE;
685 gRSP.clip_ratio_left = 0;
686 gRSP.clip_ratio_top = 0;
687 gRSP.clip_ratio_right = 640;
688 gRSP.clip_ratio_bottom = 480;
689 gRSP.clip_ratio_negx = 1;
690 gRSP.clip_ratio_negy = 1;
691 gRSP.clip_ratio_posx = 1;
692 gRSP.clip_ratio_posy = 1;
693 gRSP.real_clip_scissor_left = 0;
694 gRSP.real_clip_scissor_top = 0;
695 gRSP.real_clip_scissor_right = 640;
696 gRSP.real_clip_scissor_bottom = 480;
697 windowSetting.clipping.left = 0;
698 windowSetting.clipping.top = 0;
699 windowSetting.clipping.right = 640;
700 windowSetting.clipping.bottom = 480;
701 windowSetting.clipping.width = 640;
702 windowSetting.clipping.height = 480;
703 windowSetting.clipping.needToClip = false;
704 gRSP.real_clip_ratio_negx = 1;
705 gRSP.real_clip_ratio_negy = 1;
706 gRSP.real_clip_ratio_posx = 1;
707 gRSP.real_clip_ratio_posy = 1;
709 gRSP.DKRCMatrixIndex=0;
711 gRSP.DKRBillBoard = false;
713 gRSP.dwDKRMatrixAddr=0;
716 gRDP.geometryMode = 0;
719 gRDP.fillColor = 0xFFFFFFFF;
720 gRDP.originalFillColor =0;
723 gRSP.vertexMult = 10;
724 gRSP.bNearClip = false;
725 gRSP.bRejectVtx = false;
727 gRDP.texturesAreReloaded = false;
728 gRDP.textureIsChanged = false;
729 gRDP.colorsAreReloaded = false;
731 memset(&gRDP.otherMode,0,sizeof(RDP_OtherMode));
732 memset(&gRDP.tiles,0,sizeof(Tile)*8);
734 for( int i=0; i<MAX_VERTS; i++ )
737 g_vtxNonTransformed[i].w = 1;
740 memset(gRSPn64lights, 0, sizeof(N64Light)*16);
743 void SetFogMinMax(float fMin, float fMax, float fMul, float fOffset)
753 gRSPfFogMin = max(0,fMin/500-1);
754 gRSPfFogMax = fMax/500-1;
757 gRSPfFogDivider = 255/(gRSPfFogMax-gRSPfFogMin);
758 CRender::g_pRender->SetFogMinMax(fMin, fMax);
761 void InitVertexColors()
765 void InitVertexTextureConstants()
770 RenderTexture &tex0 = g_textures[gRSP.curTile];
771 //CTexture *surf = tex0.m_pCTexture;
772 Tile &tile0 = gRDP.tiles[gRSP.curTile];
774 scaleX = gRSP.fTexScaleX;
775 scaleY = gRSP.fTexScaleY;
777 gRSP.tex0scaleX = scaleX * tile0.fShiftScaleS/tex0.m_fTexWidth;
778 gRSP.tex0scaleY = scaleY * tile0.fShiftScaleT/tex0.m_fTexHeight;
780 gRSP.tex0OffsetX = tile0.fhilite_sl/tex0.m_fTexWidth;
781 gRSP.tex0OffsetY = tile0.fhilite_tl/tex0.m_fTexHeight;
783 if( CRender::g_pRender->IsTexel1Enable() )
785 RenderTexture &tex1 = g_textures[(gRSP.curTile+1)&7];
786 //CTexture *surf = tex1.m_pCTexture;
787 Tile &tile1 = gRDP.tiles[(gRSP.curTile+1)&7];
789 gRSP.tex1scaleX = scaleX * tile1.fShiftScaleS/tex1.m_fTexWidth;
790 gRSP.tex1scaleY = scaleY * tile1.fShiftScaleT/tex1.m_fTexHeight;
792 gRSP.tex1OffsetX = tile1.fhilite_sl/tex1.m_fTexWidth;
793 gRSP.tex1OffsetY = tile1.fhilite_tl/tex1.m_fTexHeight;
796 gRSP.texGenXRatio = tile0.fShiftScaleS;
797 gRSP.texGenYRatio = gRSP.fTexScaleX/gRSP.fTexScaleY*tex0.m_fTexWidth/tex0.m_fTexHeight*tile0.fShiftScaleT;
800 void TexGen(float &s, float &t)
802 if (gRDP.geometryMode & G_TEXTURE_GEN_LINEAR)
804 s = acosf(g_normal.x) / 3.14159f;
805 t = acosf(g_normal.y) / 3.14159f;
809 s = 0.5f * ( 1.0f + g_normal.x);
810 t = 0.5f * ( 1.0f - g_normal.y);
814 void ComputeLOD(bool openGL)
816 TLITVERTEX &v0 = g_vtxBuffer[0];
817 TLITVERTEX &v1 = g_vtxBuffer[1];
818 RenderTexture &tex0 = g_textures[gRSP.curTile];
823 float x = g_vtxProjected5[0][0] / g_vtxProjected5[0][4] - g_vtxProjected5[1][0] / g_vtxProjected5[1][4];
824 float y = g_vtxProjected5[0][1] / g_vtxProjected5[0][4] - g_vtxProjected5[1][1] / g_vtxProjected5[1][4];
826 x = windowSetting.vpWidthW*x/windowSetting.fMultX/2;
827 y = windowSetting.vpHeightW*y/windowSetting.fMultY/2;
832 float x = (v0.x - v1.x)/ windowSetting.fMultX;
833 float y = (v0.y - v1.y)/ windowSetting.fMultY;
837 float s0 = v0.tcord[0].u * tex0.m_fTexWidth;
838 float t0 = v0.tcord[0].v * tex0.m_fTexHeight;
839 float s1 = v1.tcord[0].u * tex0.m_fTexWidth;
840 float t1 = v1.tcord[0].v * tex0.m_fTexHeight;
842 dt = sqrtf((s0-s1)*(s0-s1)+(t0-t1)*(t0-t1));
845 float frac = log10f(lod)/log10f(2.0f);
846 //DEBUGGER_IF_DUMP(pauseAtNext,{DebuggerAppendMsg("LOD frac = %f", frac);});
847 frac = (lod / powf(2.0f,floorf(frac)));
848 frac = frac - floorf(frac);
849 //DEBUGGER_IF_DUMP(pauseAtNext,{DebuggerAppendMsg("LOD = %f, frac = %f", lod, frac);});
850 gRDP.LODFrac = (uint32)(frac*255);
851 CRender::g_pRender->SetCombinerAndBlender();
854 bool bHalfTxtScale=false;
855 extern uint32 lastSetTile;
857 #define noinline __attribute__((noinline))
859 static noinline void InitVertex_scale_hack_check(uint32 dwV)
861 // Check for txt scale hack
862 if( gRDP.tiles[lastSetTile].dwSize == TXT_SIZE_32b || gRDP.tiles[lastSetTile].dwSize == TXT_SIZE_4b )
864 int width = ((gRDP.tiles[lastSetTile].sh-gRDP.tiles[lastSetTile].sl+1)<<1);
865 int height = ((gRDP.tiles[lastSetTile].th-gRDP.tiles[lastSetTile].tl+1)<<1);
866 if( g_fVtxTxtCoords[dwV].x*gRSP.fTexScaleX == width || g_fVtxTxtCoords[dwV].y*gRSP.fTexScaleY == height )
873 static noinline void InitVertex_notopengl_or_clipper_adjust(TLITVERTEX &v, uint32 dwV)
875 v.x = g_vecProjected[dwV].x*gRSP.vtxXMul+gRSP.vtxXAdd;
876 v.y = g_vecProjected[dwV].y*gRSP.vtxYMul+gRSP.vtxYAdd;
877 v.z = (g_vecProjected[dwV].z + 1.0f) * 0.5f; // DirectX minZ=0, maxZ=1
878 //v.z = g_vecProjected[dwV].z; // DirectX minZ=0, maxZ=1
879 v.rhw = g_vecProjected[dwV].w;
880 VTX_DUMP(TRACE4(" Proj : x=%f, y=%f, z=%f, rhw=%f", v.x,v.y,v.z,v.rhw));
882 if( gRSP.bProcessSpecularColor )
884 v.dcSpecular = CRender::g_pRender->PostProcessSpecularColor();
885 if( gRSP.bFogEnabled )
887 v.dcSpecular &= 0x00FFFFFF;
888 uint32 fogFct = 0xFF-(uint8)((g_fFogCoord[dwV]-gRSPfFogMin)*gRSPfFogDivider);
889 v.dcSpecular |= (fogFct<<24);
892 else if( gRSP.bFogEnabled )
894 uint32 fogFct = 0xFF-(uint8)((g_fFogCoord[dwV]-gRSPfFogMin)*gRSPfFogDivider);
895 v.dcSpecular = (fogFct<<24);
899 static noinline void InitVertex_texgen_correct(TLITVERTEX &v, uint32 dwV)
901 // Correction for texGen result
903 RenderTexture &tex0 = g_textures[gRSP.curTile];
904 u0 = g_fVtxTxtCoords[dwV].x * 32 * 1024 * gRSP.fTexScaleX / tex0.m_fTexWidth;
905 v0 = g_fVtxTxtCoords[dwV].y * 32 * 1024 * gRSP.fTexScaleY / tex0.m_fTexHeight;
906 u0 *= (gRDP.tiles[gRSP.curTile].fShiftScaleS);
907 v0 *= (gRDP.tiles[gRSP.curTile].fShiftScaleT);
909 if( CRender::g_pRender->IsTexel1Enable() )
911 RenderTexture &tex1 = g_textures[(gRSP.curTile+1)&7];
912 u1 = g_fVtxTxtCoords[dwV].x * 32 * 1024 * gRSP.fTexScaleX / tex1.m_fTexWidth;
913 v1 = g_fVtxTxtCoords[dwV].y * 32 * 1024 * gRSP.fTexScaleY / tex1.m_fTexHeight;
914 u1 *= gRDP.tiles[(gRSP.curTile+1)&7].fShiftScaleS;
915 v1 *= gRDP.tiles[(gRSP.curTile+1)&7].fShiftScaleT;
916 CRender::g_pRender->SetVertexTextureUVCoord(v, u0, v0, u1, v1);
920 CRender::g_pRender->SetVertexTextureUVCoord(v, u0, v0);
925 static void multiply_subtract2(float *d, const float *m1, const float *m2, const float *s)
928 for (i = 0; i < 2; i++)
929 d[i] = m1[i] * m2[i] - s[i];
932 extern "C" void multiply_subtract2(float *d, const float *m1, const float *m2, const float *s);
935 void InitVertex(uint32 dwV, uint32 vtxIndex, bool bTexture, bool openGL)
937 VTX_DUMP(TRACE2("Init vertex (%d) to vtx buf[%d]:", dwV, vtxIndex));
939 openGL = 1; // what else there is?
941 TLITVERTEX &v = g_vtxBuffer[vtxIndex];
942 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));
945 g_vtxProjected5[vtxIndex][0] = g_vtxTransformed[dwV].x;
946 g_vtxProjected5[vtxIndex][1] = g_vtxTransformed[dwV].y;
947 g_vtxProjected5[vtxIndex][2] = g_vtxTransformed[dwV].z;
948 g_vtxProjected5[vtxIndex][3] = g_vtxTransformed[dwV].w;
949 g_vtxProjected5[vtxIndex][4] = g_vecProjected[dwV].z;
951 if( *(int *)&g_vtxTransformed[dwV].w < 0 )
952 g_vtxProjected5[vtxIndex][4] = 0;
954 g_vtxIndex[vtxIndex] = vtxIndex;
957 if( __builtin_expect(!openGL || options.bOGLVertexClipper == TRUE, 0) )
959 InitVertex_notopengl_or_clipper_adjust(v, dwV);
961 VTX_DUMP(TRACE2(" (U,V): %f, %f", g_fVtxTxtCoords[dwV].x,g_fVtxTxtCoords[dwV].y));
963 v.dcDiffuse = g_dwVtxDifColor[dwV];
964 if( gRDP.otherMode.key_en )
966 v.dcDiffuse &= 0x00FFFFFF;
967 v.dcDiffuse |= (gRDP.keyA<<24);
969 else if( gRDP.otherMode.aa_en && gRDP.otherMode.clr_on_cvg==0 )
971 v.dcDiffuse |= 0xFF000000;
974 if( gRSP.bProcessDiffuseColor )
976 v.dcDiffuse = CRender::g_pRender->PostProcessDiffuseColor(v.dcDiffuse);
978 if( options.bWinFrameMode )
980 v.dcDiffuse = g_dwVtxDifColor[dwV];
985 g_oglVtxColors[vtxIndex][0] = v.r;
986 g_oglVtxColors[vtxIndex][1] = v.g;
987 g_oglVtxColors[vtxIndex][2] = v.b;
988 g_oglVtxColors[vtxIndex][3] = v.a;
993 // If the vert is already lit, then there is no normal (and hence we can't generate tex coord)
994 // Only scale if not generated automatically
995 if ( __builtin_expect(gRSP.bTextureGen && gRSP.bLightingEnable, 0) )
997 InitVertex_texgen_correct(v, dwV);
1002 multiply_subtract2(&tex0.u, &g_fVtxTxtCoords[dwV].x, &gRSP.tex0scaleX, &gRSP.tex0OffsetX);
1004 if( CRender::g_pRender->IsTexel1Enable() )
1007 multiply_subtract2(&tex1.u, &g_fVtxTxtCoords[dwV].x, &gRSP.tex1scaleX, &gRSP.tex1OffsetX);
1009 CRender::g_pRender->SetVertexTextureUVCoord(v, tex0, tex1);
1010 VTX_DUMP(TRACE2(" (tex0): %f, %f", tex0.u,tex0.v));
1011 VTX_DUMP(TRACE2(" (tex1): %f, %f", tex1.u,tex1.v));
1015 CRender::g_pRender->SetVertexTextureUVCoord(v, tex0);
1016 VTX_DUMP(TRACE2(" (tex0): %f, %f", tex0.u,tex0.v));
1020 if( __builtin_expect(g_curRomInfo.bTextureScaleHack && !bHalfTxtScale, 0) )
1021 InitVertex_scale_hack_check(dwV);
1024 VTX_DUMP(TRACE2(" DIF(%08X), SPE(%08X)", v.dcDiffuse, v.dcSpecular));
1025 VTX_DUMP(TRACE0(""));
1028 uint32 LightVert(XVECTOR4 & norm, int vidx)
1033 register float r = gRSP.fAmbientLightR;
1034 register float g = gRSP.fAmbientLightG;
1035 register float b = gRSP.fAmbientLightB;
1037 if( options.enableHackForGames != HACK_FOR_ZELDA_MM )
1039 for (register unsigned int l=0; l < gRSPnumLights; l++)
1041 fCosT = norm.x*gRSPlights[l].x + norm.y*gRSPlights[l].y + norm.z*gRSPlights[l].z;
1045 r += gRSPlights[l].fr * fCosT;
1046 g += gRSPlights[l].fg * fCosT;
1047 b += gRSPlights[l].fb * fCosT;
1054 bool transformed = false;
1056 for (register unsigned int l=0; l < gRSPnumLights; l++)
1058 if( gRSPlights[l].range == 0 )
1060 // Regular directional light
1061 fCosT = norm.x*gRSPlights[l].x + norm.y*gRSPlights[l].y + norm.z*gRSPlights[l].z;
1065 r += gRSPlights[l].fr * fCosT;
1066 g += gRSPlights[l].fg * fCosT;
1067 b += gRSPlights[l].fb * fCosT;
1070 else //if( (gRSPlights[l].col&0x00FFFFFF) != 0x00FFFFFF )
1075 Vec3Transform(&v, (XVECTOR3*)&g_vtxNonTransformed[vidx], &gRSPmodelViewTop); // Convert to w=1
1079 XVECTOR3 dir(gRSPlights[l].x - v.x, gRSPlights[l].y - v.y, gRSPlights[l].z - v.z);
1080 //XVECTOR3 dir(v.x-gRSPlights[l].x, v.y-gRSPlights[l].y, v.z-gRSPlights[l].z);
1081 float d2 = sqrtf(dir.x*dir.x+dir.y*dir.y+dir.z*dir.z);
1086 fCosT = norm.x*dir.x + norm.y*dir.y + norm.z*dir.z;
1090 //float f = d2/gRSPlights[l].range*50;
1091 float f = d2/15000*50;
1095 r += gRSPlights[l].fr * fCosT;
1096 g += gRSPlights[l].fg * fCosT;
1097 b += gRSPlights[l].fb * fCosT;
1103 if (r > 255) r = 255;
1104 if (g > 255) g = 255;
1105 if (b > 255) b = 255;
1106 return ((0xff000000)|(((uint32)r)<<16)|(((uint32)g)<<8)|((uint32)b));
1109 uint32 LightVertNew(XVECTOR4 & norm)
1114 register float r = gRSP.fAmbientLightR;
1115 register float g = gRSP.fAmbientLightG;
1116 register float b = gRSP.fAmbientLightB;
1119 for (register unsigned int l=0; l < gRSPnumLights; l++)
1121 fCosT = norm.x*gRSPlights[l].tx + norm.y*gRSPlights[l].ty + norm.z*gRSPlights[l].tz;
1125 r += gRSPlights[l].fr * fCosT;
1126 g += gRSPlights[l].fg * fCosT;
1127 b += gRSPlights[l].fb * fCosT;
1131 if (r > 255) r = 255;
1132 if (g > 255) g = 255;
1133 if (b > 255) b = 255;
1134 return ((0xff000000)|(((uint32)r)<<16)|(((uint32)g)<<8)|((uint32)b));
1142 #if !defined(__GNUC__) && !defined(NO_ASM)
1143 __declspec( naked ) uint32 __fastcall SSELightVert()
1147 movaps xmm3, DWORD PTR gRSP; // loading Ambient colors, xmm3 is the result color
1148 movaps xmm4, DWORD PTR [g_normal]; // xmm4 is the normal
1152 cmp ecx, DWORD PTR gRSPnumLights;
1156 movups xmm5, DWORD PTR gRSPlights[eax]; // Light Dir
1157 movups xmm1, DWORD PTR gRSPlights[0x14][eax]; // Light color
1158 mulps xmm5, xmm4; // Lightdir * normals
1162 shufps xmm5,xmm0,0x01;
1168 shufps xmm0,xmm0,0; // fcosT
1176 movss xmm0,DWORD PTR real255;
1180 // Without using a memory
1181 cvtss2si eax,xmm0; // move the 1st uint32 to eax
1184 shufps xmm0,xmm0,0E5h; // move the 2nd uint32 to the 1st uint32
1185 cvtss2si ecx,xmm0; // move the 1st uint32 to ecx
1188 shufps xmm0,xmm0,0E6h; // Move the 3rd uint32 to the 1st uint32
1195 #elif defined(__GNUC__) && defined(__x86_64__) && !defined(NO_ASM)
1196 uint32 SSELightVert(void)
1199 float f255 = 255.0, fZero = 0.0;
1201 asm volatile(" movaps %1, %%xmm3 \n" // xmm3 == gRSP.fAmbientLight{RGBA}
1202 " movaps %2, %%xmm4 \n" // xmm4 == g_normal.{xyz}
1203 " xor %%rcx, %%rcx \n"
1205 " cmpl %3, %%ecx \n"
1207 " mov %%rcx, %%rax \n"
1208 " imul $0x44, %%rax, %%rax \n"
1209 " movups (%4,%%rax,), %%xmm5 \n" // xmm5 == gRSPlights[l].{xyzr}
1210 " movups 20(%4,%%rax,), %%xmm1 \n" // xmm1 == gRSPlights[l].{frfgfbfa}
1211 " mulps %%xmm4, %%xmm5 \n"
1212 " movhlps %%xmm5, %%xmm0 \n"
1213 " addps %%xmm5, %%xmm0 \n"
1214 " shufps $0x01, %%xmm0, %%xmm5 \n"
1215 " addps %%xmm5, %%xmm0 \n"
1216 " comiss %6, %%xmm0 \n"
1218 " shufps $0x00, %%xmm0, %%xmm0 \n"
1219 " mulps %%xmm0, %%xmm1 \n"
1220 " addps %%xmm1, %%xmm3 \n"
1225 " movss %5, %%xmm0 \n"
1226 " shufps $0x00, %%xmm0, %%xmm0 \n"
1227 " minps %%xmm3, %%xmm0 \n"
1228 " cvtss2si %%xmm0, %%eax \n"
1229 " shll $0x10, %%eax \n"
1230 " orl $0xff000000, %%eax \n"
1231 " shufps $0xe5, %%xmm0, %%xmm0 \n"
1232 " cvtss2si %%xmm0, %%ecx \n"
1233 " shll $8, %%ecx \n"
1234 " orl %%ecx, %%eax \n"
1235 " shufps $0xe6, %%xmm0, %%xmm0 \n"
1236 " cvtss2si %%xmm0, %%ecx \n"
1237 " orl %%ecx, %%eax \n"
1239 : "m"(gRSP), "m"(g_normal), "m"(gRSPnumLights), "r"(gRSPlights), "m"(f255), "m"(fZero)
1240 : "%rcx", "memory", "cc", "%xmm0", "%xmm1", "%xmm3", "%xmm4", "%xmm5"
1244 #elif !defined(NO_ASM) // 32-bit GCC assumed
1245 uint32 SSELightVert(void)
1248 float f255 = 255.0, fZero = 0.0;
1250 asm volatile(" movaps %1, %%xmm3 \n"
1251 " movaps %2, %%xmm4 \n"
1252 " xor %%ecx, %%ecx \n"
1254 " cmpl %3, %%ecx \n"
1256 " mov %%ecx, %%eax \n"
1257 " imul $0x44, %%eax, %%eax \n"
1258 " movups (%4,%%eax,), %%xmm5 \n"
1259 " movups 20(%4,%%eax,), %%xmm1 \n"
1260 " mulps %%xmm4, %%xmm5 \n"
1261 " movhlps %%xmm5, %%xmm0 \n"
1262 " addps %%xmm5, %%xmm0 \n"
1263 " shufps $0x01, %%xmm0, %%xmm5 \n"
1264 " addps %%xmm5, %%xmm0 \n"
1265 " comiss %6, %%xmm0 \n"
1267 " shufps $0x00, %%xmm0, %%xmm0 \n"
1268 " mulps %%xmm0, %%xmm1 \n"
1269 " addps %%xmm1, %%xmm3 \n"
1274 " movss %5, %%xmm0 \n"
1275 " shufps $0x00, %%xmm0, %%xmm0 \n"
1276 " minps %%xmm3, %%xmm0 \n"
1277 " cvtss2si %%xmm0, %%eax \n"
1278 " shll $0x10, %%eax \n"
1279 " orl $0xff000000, %%eax \n"
1280 " shufps $0xe5, %%xmm0, %%xmm0 \n"
1281 " cvtss2si %%xmm0, %%ecx \n"
1282 " shll $8, %%ecx \n"
1283 " orl %%ecx, %%eax \n"
1284 " shufps $0xe6, %%xmm0, %%xmm0 \n"
1285 " cvtss2si %%xmm0, %%ecx \n"
1286 " orl %%ecx, %%eax \n"
1288 : "m"(gRSP), "m"(g_normal), "m"(gRSPnumLights), "r"(gRSPlights), "m"(f255), "m"(fZero)
1289 : "%rcx", "memory", "cc", "%xmm0", "%xmm1", "%xmm3", "%xmm4", "%xmm5"
1295 inline void ReplaceAlphaWithFogFactor(int i)
1297 if( gRDP.geometryMode & G_FOG )
1299 // Use fog factor to replace vertex alpha
1300 if( g_vecProjected[i].z > 1 )
1301 *(((uint8*)&(g_dwVtxDifColor[i]))+3) = 0xFF;
1302 if( g_vecProjected[i].z < 0 )
1303 *(((uint8*)&(g_dwVtxDifColor[i]))+3) = 0;
1305 *(((uint8*)&(g_dwVtxDifColor[i]))+3) = (uint8)(g_vecProjected[i].z*255);
1320 // Assumes dwAddr has already been checked!
1321 // Don't inline - it's too big with the transform macros
1323 #if !defined(NO_ASM)
1324 void ProcessVertexDataSSE(uint32 dwAddr, uint32 dwV0, uint32 dwNum)
1326 UpdateCombinedMatrix();
1328 // This function is called upon SPvertex
1329 // - do vertex matrix transform
1330 // - do vertex lighting
1331 // - do texture cooridinate transform if needed
1332 // - calculate normal vector
1334 // Output: - g_vecProjected[i] -> transformed vertex x,y,z
1335 // - g_vecProjected[i].w -> saved vertex 1/w
1336 // - g_dwVtxFlags[i] -> flags
1337 // - g_dwVtxDifColor[i] -> vertex color
1338 // - g_fVtxTxtCoords[i] -> vertex texture cooridinates
1340 FiddledVtx * pVtxBase = (FiddledVtx*)(g_pRDRAMu8 + dwAddr);
1341 g_pVtxBase = pVtxBase;
1343 for (uint32 i = dwV0; i < dwV0 + dwNum; i++)
1345 SP_Timing(RSP_GBI0_Vtx);
1347 FiddledVtx & vert = pVtxBase[i - dwV0];
1349 g_vtxNonTransformed[i].x = (float)vert.x;
1350 g_vtxNonTransformed[i].y = (float)vert.y;
1351 g_vtxNonTransformed[i].z = (float)vert.z;
1353 SSEVec3Transform(i);
1355 if( gRSP.bFogEnabled )
1357 g_fFogCoord[i] = g_vecProjected[i].z;
1358 if( g_vecProjected[i].w < 0 || g_vecProjected[i].z < 0 || g_fFogCoord[i] < gRSPfFogMin )
1359 g_fFogCoord[i] = gRSPfFogMin;
1362 ReplaceAlphaWithFogFactor(i);
1367 uint32 *dat = (uint32*)(&vert);
1368 DebuggerAppendMsg("vtx %d: %08X %08X %08X %08X", i, dat[0],dat[1],dat[2],dat[3]);
1369 DebuggerAppendMsg(" : %f, %f, %f, %f",
1370 g_vtxTransformed[i].x,g_vtxTransformed[i].y,g_vtxTransformed[i].z,g_vtxTransformed[i].w);
1371 DebuggerAppendMsg(" : %f, %f, %f, %f",
1372 g_vecProjected[i].x,g_vecProjected[i].y,g_vecProjected[i].z,g_vecProjected[i].w);
1375 RSP_Vtx_Clipping(i);
1377 if( gRSP.bLightingEnable )
1379 g_normal.x = (float)vert.norma.nx;
1380 g_normal.y = (float)vert.norma.ny;
1381 g_normal.z = (float)vert.norma.nz;
1383 SSEVec3TransformNormal();
1384 if( options.enableHackForGames != HACK_FOR_ZELDA_MM )
1385 g_dwVtxDifColor[i] = SSELightVert();
1387 g_dwVtxDifColor[i] = LightVert(g_normal, i);
1388 *(((uint8*)&(g_dwVtxDifColor[i]))+3) = vert.rgba.a; // still use alpha from the vertex
1392 if( (gRDP.geometryMode & G_SHADE) == 0 && gRSP.ucode < 5 ) //Shade is disabled
1395 g_dwVtxDifColor[i] = gRDP.primitiveColor;
1399 register IColor &color = *(IColor*)&g_dwVtxDifColor[i];
1400 color.b = vert.rgba.r;
1401 color.g = vert.rgba.g;
1402 color.r = vert.rgba.b;
1403 color.a = vert.rgba.a;
1407 if( options.bWinFrameMode )
1409 g_dwVtxDifColor[i] = COLOR_RGBA(vert.rgba.r, vert.rgba.g, vert.rgba.b, vert.rgba.a);
1412 // Update texture coords n.b. need to divide tu/tv by bogus scale on addition to buffer
1414 // If the vert is already lit, then there is no normal (and hence we
1415 // can't generate tex coord)
1416 if (gRSP.bTextureGen && gRSP.bLightingEnable )
1418 TexGen(g_fVtxTxtCoords[i].x, g_fVtxTxtCoords[i].y);
1422 g_fVtxTxtCoords[i].x = (float)vert.tu;
1423 g_fVtxTxtCoords[i].y = (float)vert.tv;
1427 VTX_DUMP(TRACE2("Setting Vertexes: %d - %d\n", dwV0, dwV0+dwNum-1));
1428 DEBUGGER_PAUSE_AND_DUMP(NEXT_VERTEX_CMD,{TRACE0("Paused at Vertex Cmd");});
1432 void ProcessVertexDataNoSSE(uint32 dwAddr, uint32 dwV0, uint32 dwNum)
1435 UpdateCombinedMatrix();
1437 // This function is called upon SPvertex
1438 // - do vertex matrix transform
1439 // - do vertex lighting
1440 // - do texture cooridinate transform if needed
1441 // - calculate normal vector
1443 // Output: - g_vecProjected[i] -> transformed vertex x,y,z
1444 // - g_vecProjected[i].w -> saved vertex 1/w
1445 // - g_dwVtxFlags[i] -> flags
1446 // - g_dwVtxDifColor[i] -> vertex color
1447 // - g_fVtxTxtCoords[i] -> vertex texture cooridinates
1449 FiddledVtx * pVtxBase = (FiddledVtx*)(g_pRDRAMu8 + dwAddr);
1450 g_pVtxBase = pVtxBase;
1452 for (uint32 i = dwV0; i < dwV0 + dwNum; i++)
1454 SP_Timing(RSP_GBI0_Vtx);
1456 FiddledVtx & vert = pVtxBase[i - dwV0];
1458 g_vtxNonTransformed[i].x = (float)vert.x;
1459 g_vtxNonTransformed[i].y = (float)vert.y;
1460 g_vtxNonTransformed[i].z = (float)vert.z;
1462 Vec3Transform(&g_vtxTransformed[i], (XVECTOR3*)&g_vtxNonTransformed[i], &gRSPworldProject); // Convert to w=1
1464 g_vecProjected[i].w = 1.0f / g_vtxTransformed[i].w;
1465 g_vecProjected[i].x = g_vtxTransformed[i].x * g_vecProjected[i].w;
1466 g_vecProjected[i].y = g_vtxTransformed[i].y * g_vecProjected[i].w;
1467 if ((g_curRomInfo.bPrimaryDepthHack || options.enableHackForGames == HACK_FOR_NASCAR ) && gRDP.otherMode.depth_source )
1469 g_vecProjected[i].z = gRDP.fPrimitiveDepth;
1470 g_vtxTransformed[i].z = gRDP.fPrimitiveDepth*g_vtxTransformed[i].w;
1474 g_vecProjected[i].z = g_vtxTransformed[i].z * g_vecProjected[i].w;
1477 if( gRSP.bFogEnabled )
1479 g_fFogCoord[i] = g_vecProjected[i].z;
1480 if( g_vecProjected[i].w < 0 || g_vecProjected[i].z < 0 || g_fFogCoord[i] < gRSPfFogMin )
1481 g_fFogCoord[i] = gRSPfFogMin;
1486 uint32 *dat = (uint32*)(&vert);
1487 DebuggerAppendMsg("vtx %d: %08X %08X %08X %08X", i, dat[0],dat[1],dat[2],dat[3]);
1488 DebuggerAppendMsg(" : %f, %f, %f, %f",
1489 g_vtxTransformed[i].x,g_vtxTransformed[i].y,g_vtxTransformed[i].z,g_vtxTransformed[i].w);
1490 DebuggerAppendMsg(" : %f, %f, %f, %f",
1491 g_vecProjected[i].x,g_vecProjected[i].y,g_vecProjected[i].z,g_vecProjected[i].w);
1494 RSP_Vtx_Clipping(i);
1496 if( gRSP.bLightingEnable )
1498 g_normal.x = (float)vert.norma.nx;
1499 g_normal.y = (float)vert.norma.ny;
1500 g_normal.z = (float)vert.norma.nz;
1502 Vec3TransformNormal(g_normal, gRSPmodelViewTop);
1503 g_dwVtxDifColor[i] = LightVert(g_normal, i);
1504 *(((uint8*)&(g_dwVtxDifColor[i]))+3) = vert.rgba.a; // still use alpha from the vertex
1508 if( (gRDP.geometryMode & G_SHADE) == 0 && gRSP.ucode < 5 ) //Shade is disabled
1511 g_dwVtxDifColor[i] = gRDP.primitiveColor;
1515 register IColor &color = *(IColor*)&g_dwVtxDifColor[i];
1516 color.b = vert.rgba.r;
1517 color.g = vert.rgba.g;
1518 color.r = vert.rgba.b;
1519 color.a = vert.rgba.a;
1523 if( options.bWinFrameMode )
1525 g_dwVtxDifColor[i] = COLOR_RGBA(vert.rgba.r, vert.rgba.g, vert.rgba.b, vert.rgba.a);
1528 ReplaceAlphaWithFogFactor(i);
1530 // Update texture coords n.b. need to divide tu/tv by bogus scale on addition to buffer
1532 // If the vert is already lit, then there is no normal (and hence we
1533 // can't generate tex coord)
1534 if (gRSP.bTextureGen && gRSP.bLightingEnable )
1536 TexGen(g_fVtxTxtCoords[i].x, g_fVtxTxtCoords[i].y);
1540 g_fVtxTxtCoords[i].x = (float)vert.tu;
1541 g_fVtxTxtCoords[i].y = (float)vert.tv;
1545 VTX_DUMP(TRACE2("Setting Vertexes: %d - %d\n", dwV0, dwV0+dwNum-1));
1546 DEBUGGER_PAUSE_AND_DUMP(NEXT_VERTEX_CMD,{TRACE0("Paused at Vertex Cmd");});
1551 #include "RenderBase_neon.h"
1553 extern "C" void pv_neon(XVECTOR4 *g_vtxTransformed, XVECTOR4 *g_vecProjected,
1554 uint32 *g_dwVtxDifColor, VECTOR2 *g_fVtxTxtCoords,
1555 float *g_fFogCoord, uint32 *g_clipFlag2,
1556 uint32 dwNum, int neon_state,
1557 const FiddledVtx *vtx,
1558 const Light *gRSPlights, const float *fRSPAmbientLightRGBA,
1559 const XMATRIX *gRSPworldProject, const XMATRIX *gRSPmodelViewTop,
1560 uint32 gRSPnumLights, float gRSPfFogMin,
1561 uint32 primitiveColor, uint32 primitiveColor_);
1563 extern "C" int tv_direction(const XVECTOR4 *v0, const XVECTOR4 *v1, const XVECTOR4 *v2);
1565 void ProcessVertexDataNEON(uint32 dwAddr, uint32 dwV0, uint32 dwNum)
1567 if (gRSP.bTextureGen && gRSP.bLightingEnable) {
1568 ProcessVertexDataNoSSE(dwAddr, dwV0,dwNum);
1573 // - g_clipFlag is not used at all
1574 // - g_fFogCoord is not used at all
1575 // - g_vtxNonTransformed is not used after ProcessVertexData*() returns
1576 // - g_normal - same
1579 if ( gRSP.bLightingEnable )
1580 neon_state |= PV_NEON_ENABLE_LIGHT;
1581 if ( (gRDP.geometryMode & G_SHADE) || gRSP.ucode >= 5 )
1582 neon_state |= PV_NEON_ENABLE_SHADE;
1583 if ( gRSP.bFogEnabled )
1584 neon_state |= PV_NEON_ENABLE_FOG;
1585 if ( gRDP.geometryMode & G_FOG )
1586 neon_state |= PV_NEON_FOG_ALPHA;
1590 UpdateCombinedMatrix();
1592 // This function is called upon SPvertex
1593 // - do vertex matrix transform
1594 // - do vertex lighting
1595 // - do texture cooridinate transform if needed
1596 // - calculate normal vector
1598 // Output: - g_vecProjected[i] -> transformed vertex x,y,z
1599 // - g_vecProjected[i].w -> saved vertex 1/w
1600 // - g_vtxTransformed[i]
1601 // - g_dwVtxDifColor[i] -> vertex color
1602 // - g_fVtxTxtCoords[i] -> vertex texture cooridinates
1603 // - g_fFogCoord[i] -> unused
1606 const FiddledVtx * pVtxBase = (const FiddledVtx*)(g_pRDRAMu8 + dwAddr);
1607 g_pVtxBase = (FiddledVtx *)pVtxBase;
1609 gRSPmodelViewTop._14 = gRSPmodelViewTop._24 =
1610 gRSPmodelViewTop._34 = 0;
1612 // SP_Timing(RSP_GBI0_Vtx);
1613 status.SPCycleCount += Timing_RSP_GBI0_Vtx * dwNum;
1617 pv_neon(&g_vtxTransformed[i], &g_vecProjected[i],
1618 &g_dwVtxDifColor[i], &g_fVtxTxtCoords[i],
1619 &g_fFogCoord[i], &g_clipFlag2[i],
1620 dwNum, neon_state, &pVtxBase[i - dwV0],
1621 gRSPlights, gRSP.fAmbientColors,
1622 &gRSPworldProject, &gRSPmodelViewTop,
1623 gRSPnumLights, gRSPfFogMin,
1624 gRDP.primitiveColor, gRDP.primitiveColor);
1626 for (i = dwV0; i < dwV0 + dwNum; i++)
1628 const FiddledVtx & vert = pVtxBase[i - dwV0];
1629 XVECTOR3 vtx_raw; // was g_vtxNonTransformed
1631 vtx_raw.x = (float)vert.x;
1632 vtx_raw.y = (float)vert.y;
1633 vtx_raw.z = (float)vert.z;
1635 Vec3Transform(&g_vtxTransformed[i], &vtx_raw, &gRSPworldProject); // Convert to w=1
1637 g_vecProjected[i].w = 1.0f / g_vtxTransformed[i].w;
1638 g_vecProjected[i].x = g_vtxTransformed[i].x * g_vecProjected[i].w;
1639 g_vecProjected[i].y = g_vtxTransformed[i].y * g_vecProjected[i].w;
1640 g_vecProjected[i].z = g_vtxTransformed[i].z * g_vecProjected[i].w;
1642 // RSP_Vtx_Clipping(i);
1644 if( g_vecProjected[i].w > 0 )
1646 if( g_vecProjected[i].x > 1 ) g_clipFlag2[i] |= X_CLIP_MAX;
1647 if( g_vecProjected[i].x < -1 ) g_clipFlag2[i] |= X_CLIP_MIN;
1648 if( g_vecProjected[i].y > 1 ) g_clipFlag2[i] |= Y_CLIP_MAX;
1649 if( g_vecProjected[i].y < -1 ) g_clipFlag2[i] |= Y_CLIP_MIN;
1652 if( neon_state & PV_NEON_ENABLE_LIGHT )
1654 XVECTOR3 normal; // was g_normal
1657 normal.x = (float)vert.norma.nx;
1658 normal.y = (float)vert.norma.ny;
1659 normal.z = (float)vert.norma.nz;
1661 Vec3TransformNormal(normal, gRSPmodelViewTop);
1663 r = gRSP.fAmbientLightR;
1664 g = gRSP.fAmbientLightG;
1665 b = gRSP.fAmbientLightB;
1667 for (unsigned int l=0; l < gRSPnumLights; l++)
1669 float fCosT = normal.x * gRSPlights[l].x + normal.y * gRSPlights[l].y + normal.z * gRSPlights[l].z;
1673 r += gRSPlights[l].fr * fCosT;
1674 g += gRSPlights[l].fg * fCosT;
1675 b += gRSPlights[l].fb * fCosT;
1678 if (r > 255) r = 255;
1679 if (g > 255) g = 255;
1680 if (b > 255) b = 255;
1681 g_dwVtxDifColor[i] = ((vert.rgba.a<<24)|(((uint32)r)<<16)|(((uint32)g)<<8)|((uint32)b));
1683 else if( neon_state & PV_NEON_ENABLE_SHADE )
1685 IColor &color = *(IColor*)&g_dwVtxDifColor[i];
1686 color.b = vert.rgba.r;
1687 color.g = vert.rgba.g;
1688 color.r = vert.rgba.b;
1689 color.a = vert.rgba.a;
1692 g_dwVtxDifColor[i] = gRDP.primitiveColor; // FLAT shade
1694 // ReplaceAlphaWithFogFactor(i);
1695 if( neon_state & PV_NEON_FOG_ALPHA )
1697 // Use fog factor to replace vertex alpha
1698 if( g_vecProjected[i].z > 1 )
1699 *(((uint8*)&(g_dwVtxDifColor[i]))+3) = 0xFF;
1700 // missing 'else' in original code??
1701 else if( g_vecProjected[i].z < 0 )
1702 *(((uint8*)&(g_dwVtxDifColor[i]))+3) = 0;
1704 *(((uint8*)&(g_dwVtxDifColor[i]))+3) = (uint8)(g_vecProjected[i].z*255);
1707 g_fVtxTxtCoords[i].x = (float)vert.tu;
1708 g_fVtxTxtCoords[i].y = (float)vert.tv;
1713 bool PrepareTriangle(uint32 dwV0, uint32 dwV1, uint32 dwV2)
1715 if( status.isVertexShaderEnabled || status.bUseHW_T_L )
1717 g_vtxIndex[gRSP.numVertices++] = dwV0;
1718 g_vtxIndex[gRSP.numVertices++] = dwV1;
1719 g_vtxIndex[gRSP.numVertices++] = dwV2;
1720 status.dwNumTrisRendered++;
1721 gRSP.maxVertexID = max(gRSP.maxVertexID,max(dwV0,max(dwV1,dwV2)));
1725 SP_Timing(SP_Each_Triangle);
1727 bool textureFlag = (CRender::g_pRender->IsTextureEnabled() || gRSP.ucode == 6 );
1728 bool openGL = CDeviceBuilder::m_deviceGeneralType == OGL_DEVICE;
1730 InitVertex(dwV0, gRSP.numVertices, textureFlag, openGL);
1731 InitVertex(dwV1, gRSP.numVertices+1, textureFlag, openGL);
1732 InitVertex(dwV2, gRSP.numVertices+2, textureFlag, openGL);
1734 if( __builtin_expect(gRSP.numVertices == 0 && g_curRomInfo.bEnableTxtLOD && gRDP.otherMode.text_lod, 0) )
1736 if( CRender::g_pRender->IsTexel1Enable() && CRender::g_pRender->m_pColorCombiner->m_pDecodedMux->isUsed(MUX_LODFRAC) )
1746 gRSP.numVertices += 3;
1747 status.dwNumTrisRendered++;
1755 // Returns TRUE if it thinks the triangle is visible
1756 // Returns FALSE if it is clipped
1757 bool IsTriangleVisible(uint32 dwV0, uint32 dwV1, uint32 dwV2)
1759 //return true; //fix me
1761 if( status.isVertexShaderEnabled || status.bUseHW_T_L ) return true; // We won't have access to transformed vertex data
1763 DEBUGGER_ONLY_IF( (!debuggerEnableTestTris || !debuggerEnableCullFace), {return TRUE;});
1766 // Check vertices are valid!
1767 if (dwV0 >= MAX_VERTS || dwV1 >= MAX_VERTS || dwV2 >= MAX_VERTS)
1771 // Here we AND all the flags. If any of the bits is set for all
1772 // 3 vertices, it means that all three x, y or z lie outside of
1773 // the current viewing volume.
1774 // Currently disabled - still seems a bit dodgy
1775 if ((gRSP.bCullFront || gRSP.bCullBack) && gRDP.otherMode.zmode != 3)
1777 XVECTOR4 & v0 = g_vecProjected[dwV0];
1778 XVECTOR4 & v1 = g_vecProjected[dwV1];
1779 XVECTOR4 & v2 = g_vecProjected[dwV2];
1781 // Only try to clip if the tri is onscreen. For some reason, this
1782 // method doesnt' work well when the z value is outside of screenspace
1783 //if (v0.z < 1 && v1.z < 1 && v2.z < 1)
1785 #ifndef __ARM_NEON__
1786 float V1 = v2.x - v0.x;
1787 float V2 = v2.y - v0.y;
1789 float W1 = v2.x - v1.x;
1790 float W2 = v2.y - v1.y;
1792 float fDirection = (V1 * W2) - (V2 * W1);
1793 fDirection = fDirection * v1.w * v2.w * v0.w;
1794 //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;
1796 // really returns float, but we only need sign
1797 int fDirection = tv_direction(&v0, &v1, &v2);
1800 if (fDirection < 0 && gRSP.bCullBack)
1802 status.dwNumTrisClipped++;
1805 else if (fDirection > 0 && gRSP.bCullFront)
1807 status.dwNumTrisClipped++;
1813 #ifdef ENABLE_CLIP_TRI
1814 //if( gRSP.bRejectVtx && (g_clipFlag[dwV0]|g_clipFlag[dwV1]|g_clipFlag[dwV2]) )
1816 if( g_clipFlag2[dwV0]&g_clipFlag2[dwV1]&g_clipFlag2[dwV2] )
1818 //DebuggerAppendMsg("Clipped");
1827 void SetPrimitiveColor(uint32 dwCol, uint32 LODMin, uint32 LODFrac)
1829 gRDP.colorsAreReloaded = true;
1830 gRDP.primitiveColor = dwCol;
1831 gRDP.primLODMin = LODMin;
1832 gRDP.primLODFrac = LODFrac;
1833 if( gRDP.primLODFrac < gRDP.primLODMin )
1835 gRDP.primLODFrac = gRDP.primLODMin;
1838 gRDP.fvPrimitiveColor[0] = ((dwCol>>16)&0xFF)/255.0f; //r
1839 gRDP.fvPrimitiveColor[1] = ((dwCol>>8)&0xFF)/255.0f; //g
1840 gRDP.fvPrimitiveColor[2] = ((dwCol)&0xFF)/255.0f; //b
1841 gRDP.fvPrimitiveColor[3] = ((dwCol>>24)&0xFF)/255.0f; //a
1844 void SetPrimitiveDepth(uint32 z, uint32 dwDZ)
1846 gRDP.primitiveDepth = z & 0x7FFF;
1847 gRDP.fPrimitiveDepth = (float)(gRDP.primitiveDepth)/(float)0x8000;
1849 //gRDP.fPrimitiveDepth = gRDP.fPrimitiveDepth*2-1;
1851 z=0xFFFF -> 1 the farest
1852 z=0 -> -1 the nearest
1858 if( (pauseAtNext && (eventToPause == NEXT_VERTEX_CMD || eventToPause == NEXT_FLUSH_TRI )) )//&& logTriangles )
1860 DebuggerAppendMsg("Set prim Depth: %f, (%08X, %08X)", gRDP.fPrimitiveDepth, z, dwDZ);
1865 void SetVertexXYZ(uint32 vertex, float x, float y, float z)
1867 g_vecProjected[vertex].x = x;
1868 g_vecProjected[vertex].y = y;
1869 g_vecProjected[vertex].z = z;
1871 g_vtxTransformed[vertex].x = x*g_vtxTransformed[vertex].w;
1872 g_vtxTransformed[vertex].y = y*g_vtxTransformed[vertex].w;
1873 g_vtxTransformed[vertex].z = z*g_vtxTransformed[vertex].w;
1876 void ModifyVertexInfo(uint32 where, uint32 vertex, uint32 val)
1880 case RSP_MV_WORD_OFFSET_POINT_RGBA: // Modify RGBA
1882 uint32 r = (val>>24)&0xFF;
1883 uint32 g = (val>>16)&0xFF;
1884 uint32 b = (val>>8)&0xFF;
1885 uint32 a = val&0xFF;
1886 g_dwVtxDifColor[vertex] = COLOR_RGBA(r, g, b, a);
1887 LOG_UCODE("Modify vert %d color, 0x%08x", vertex, g_dwVtxDifColor[vertex]);
1890 case RSP_MV_WORD_OFFSET_POINT_XYSCREEN: // Modify X,Y
1892 uint16 nX = (uint16)(val>>16);
1893 short x = *((short*)&nX);
1896 uint16 nY = (uint16)(val&0xFFFF);
1897 short y = *((short*)&nY);
1900 // Should do viewport transform.
1903 x -= windowSetting.uViWidth/2;
1904 y = windowSetting.uViHeight/2-y;
1906 if( options.bEnableHacks && ((*g_GraphicsInfo.VI_X_SCALE_REG)&0xF) != 0 )
1909 // I don't know why Tarzan is different
1910 SetVertexXYZ(vertex, x/windowSetting.fViWidth, y/windowSetting.fViHeight, g_vecProjected[vertex].z);
1914 // Toy Story 2 and other games
1915 SetVertexXYZ(vertex, x*2/windowSetting.fViWidth, y*2/windowSetting.fViHeight, g_vecProjected[vertex].z);
1918 LOG_UCODE("Modify vert %d: x=%d, y=%d", vertex, x, y);
1919 VTX_DUMP(TRACE3("Modify vert %d: (%d,%d)", vertex, x, y));
1922 case RSP_MV_WORD_OFFSET_POINT_ZSCREEN: // Modify C
1926 SetVertexXYZ(vertex, g_vecProjected[vertex].x, g_vecProjected[vertex].y, (((float)z/0x03FF)+0.5f)/2.0f );
1927 LOG_UCODE("Modify vert %d: z=%d", vertex, z);
1928 VTX_DUMP(TRACE2("Modify vert %d: z=%d", vertex, z));
1931 case RSP_MV_WORD_OFFSET_POINT_ST: // Texture
1933 short tu = short(val>>16);
1934 short tv = short(val & 0xFFFF);
1935 float ftu = tu / 32.0f;
1936 float ftv = tv / 32.0f;
1937 LOG_UCODE(" Setting vertex %d tu/tv to %f, %f", vertex, (float)tu, (float)tv);
1938 CRender::g_pRender->SetVtxTextureCoord(vertex, ftu/gRSP.fTexScaleX, ftv/gRSP.fTexScaleY);
1942 DEBUGGER_PAUSE_AND_DUMP(NEXT_VERTEX_CMD,{TRACE0("Paused at ModVertex Cmd");});
1945 void ProcessVertexDataDKR(uint32 dwAddr, uint32 dwV0, uint32 dwNum)
1947 UpdateCombinedMatrix();
1949 long long pVtxBase = (long long) (g_pRDRAMu8 + dwAddr);
1950 g_pVtxBase = (FiddledVtx*)pVtxBase;
1952 Matrix &matWorldProject = gRSP.DKRMatrixes[gRSP.DKRCMatrixIndex];
1957 if ((!gRSP.DKRBillBoard) || (gRSP.DKRCMatrixIndex != 2) )
1962 if( addbase && gRSP.DKRVtxCount == 0 && dwNum > 1 )
1967 LOG_UCODE(" ProcessVertexDataDKR, CMatrix = %d, Add base=%s", gRSP.DKRCMatrixIndex, gRSP.DKRBillBoard?"true":"false");
1968 VTX_DUMP(TRACE2("DKR Setting Vertexes\nCMatrix = %d, Add base=%s", gRSP.DKRCMatrixIndex, gRSP.DKRBillBoard?"true":"false"));
1971 uint32 end = dwV0 + dwNum;
1972 for (uint32 i = dwV0; i < end; i++)
1976 g_vtxNonTransformed[i].x = (float)*(short*)((pVtxBase+nOff + 0) ^ 2);
1977 g_vtxNonTransformed[i].y = (float)*(short*)((pVtxBase+nOff + 2) ^ 2);
1978 g_vtxNonTransformed[i].z = (float)*(short*)((pVtxBase+nOff + 4) ^ 2);
1980 //if( status.isSSEEnabled )
1981 // SSEVec3TransformDKR(g_vtxTransformed[i], g_vtxNonTransformed[i]);
1983 Vec3Transform(&g_vtxTransformed[i], (XVECTOR3*)&g_vtxNonTransformed[i], &matWorldProject); // Convert to w=1
1985 if( gRSP.DKRVtxCount == 0 && dwNum==1 )
1987 gRSP.DKRBaseVec.x = g_vtxTransformed[i].x;
1988 gRSP.DKRBaseVec.y = g_vtxTransformed[i].y;
1989 gRSP.DKRBaseVec.z = g_vtxTransformed[i].z;
1990 gRSP.DKRBaseVec.w = g_vtxTransformed[i].w;
1994 g_vtxTransformed[i].x += gRSP.DKRBaseVec.x;
1995 g_vtxTransformed[i].y += gRSP.DKRBaseVec.y;
1996 g_vtxTransformed[i].z += gRSP.DKRBaseVec.z;
1997 g_vtxTransformed[i].w = gRSP.DKRBaseVec.w;
2000 g_vecProjected[i].w = 1.0f / g_vtxTransformed[i].w;
2001 g_vecProjected[i].x = g_vtxTransformed[i].x * g_vecProjected[i].w;
2002 g_vecProjected[i].y = g_vtxTransformed[i].y * g_vecProjected[i].w;
2003 g_vecProjected[i].z = g_vtxTransformed[i].z * g_vecProjected[i].w;
2007 VTX_DUMP(TRACE5("vtx %d: %f, %f, %f, %f", i,
2008 g_vtxTransformed[i].x,g_vtxTransformed[i].y,g_vtxTransformed[i].z,g_vtxTransformed[i].w));
2010 if( gRSP.bFogEnabled )
2012 g_fFogCoord[i] = g_vecProjected[i].z;
2013 if( g_vecProjected[i].w < 0 || g_vecProjected[i].z < 0 || g_fFogCoord[i] < gRSPfFogMin )
2014 g_fFogCoord[i] = gRSPfFogMin;
2017 RSP_Vtx_Clipping(i);
2019 short wA = *(short*)((pVtxBase+nOff + 6) ^ 2);
2020 short wB = *(short*)((pVtxBase+nOff + 8) ^ 2);
2022 s8 r = (s8)(wA >> 8);
2024 s8 b = (s8)(wB >> 8);
2027 if (gRSP.bLightingEnable)
2029 g_normal.x = (char)r; //norma.nx;
2030 g_normal.y = (char)g; //norma.ny;
2031 g_normal.z = (char)b; //norma.nz;
2033 Vec3TransformNormal(g_normal, matWorldProject)
2034 #if !defined(NO_ASM)
2035 if( status.isSSEEnabled )
2036 g_dwVtxDifColor[i] = SSELightVert();
2039 g_dwVtxDifColor[i] = LightVert(g_normal, i);
2049 // Assign true vert colour after lighting/fogging
2050 g_dwVtxDifColor[i] = COLOR_RGBA(nR, nG, nB, nA);
2053 ReplaceAlphaWithFogFactor(i);
2055 g_fVtxTxtCoords[i].x = g_fVtxTxtCoords[i].y = 1;
2061 DEBUGGER_PAUSE_AND_DUMP(NEXT_VERTEX_CMD,{DebuggerAppendMsg("Paused at DKR Vertex Cmd, v0=%d, vn=%d, addr=%08X", dwV0, dwNum, dwAddr);});
2065 extern uint32 dwPDCIAddr;
2066 void ProcessVertexDataPD(uint32 dwAddr, uint32 dwV0, uint32 dwNum)
2068 UpdateCombinedMatrix();
2070 N64VtxPD * pVtxBase = (N64VtxPD*)(g_pRDRAMu8 + dwAddr);
2071 g_pVtxBase = (FiddledVtx*)pVtxBase; // Fix me
2073 for (uint32 i = dwV0; i < dwV0 + dwNum; i++)
2075 N64VtxPD &vert = pVtxBase[i - dwV0];
2077 g_vtxNonTransformed[i].x = (float)vert.x;
2078 g_vtxNonTransformed[i].y = (float)vert.y;
2079 g_vtxNonTransformed[i].z = (float)vert.z;
2081 #if !defined(NO_ASM)
2082 if( status.isSSEEnabled )
2083 SSEVec3Transform(i);
2087 Vec3Transform(&g_vtxTransformed[i], (XVECTOR3*)&g_vtxNonTransformed[i], &gRSPworldProject); // Convert to w=1
2088 g_vecProjected[i].w = 1.0f / g_vtxTransformed[i].w;
2089 g_vecProjected[i].x = g_vtxTransformed[i].x * g_vecProjected[i].w;
2090 g_vecProjected[i].y = g_vtxTransformed[i].y * g_vecProjected[i].w;
2091 g_vecProjected[i].z = g_vtxTransformed[i].z * g_vecProjected[i].w;
2094 g_fFogCoord[i] = g_vecProjected[i].z;
2095 if( g_vecProjected[i].w < 0 || g_vecProjected[i].z < 0 || g_fFogCoord[i] < gRSPfFogMin )
2096 g_fFogCoord[i] = gRSPfFogMin;
2098 RSP_Vtx_Clipping(i);
2100 uint8 *addr = g_pRDRAMu8+dwPDCIAddr+ (vert.cidx&0xFF);
2106 if( gRSP.bLightingEnable )
2108 g_normal.x = (char)r;
2109 g_normal.y = (char)g;
2110 g_normal.z = (char)b;
2111 #if !defined(NO_ASM)
2112 if( status.isSSEEnabled )
2114 SSEVec3TransformNormal();
2115 g_dwVtxDifColor[i] = SSELightVert();
2120 Vec3TransformNormal(g_normal, gRSPmodelViewTop);
2121 g_dwVtxDifColor[i] = LightVert(g_normal, i);
2123 *(((uint8*)&(g_dwVtxDifColor[i]))+3) = (uint8)a; // still use alpha from the vertex
2127 if( (gRDP.geometryMode & G_SHADE) == 0 && gRSP.ucode < 5 ) //Shade is disabled
2129 g_dwVtxDifColor[i] = gRDP.primitiveColor;
2133 g_dwVtxDifColor[i] = COLOR_RGBA(r, g, b, a);
2137 if( options.bWinFrameMode )
2139 g_dwVtxDifColor[i] = COLOR_RGBA(r, g, b, a);
2142 ReplaceAlphaWithFogFactor(i);
2144 VECTOR2 & t = g_fVtxTxtCoords[i];
2145 if (gRSP.bTextureGen && gRSP.bLightingEnable )
2147 // Not sure if we should transform the normal here
2148 //Matrix & matWV = gRSP.projectionMtxs[gRSP.projectionMtxTop];
2149 //Vec3TransformNormal(g_normal, matWV);
2151 TexGen(g_fVtxTxtCoords[i].x, g_fVtxTxtCoords[i].y);
2162 DebuggerAppendMsg("vtx %d: %d %d %d", i, vert.x,vert.y,vert.z);
2163 DebuggerAppendMsg(" : %f, %f, %f, %f",
2164 g_vtxTransformed[i].x,g_vtxTransformed[i].y,g_vtxTransformed[i].z,g_vtxTransformed[i].w);
2165 DebuggerAppendMsg(" : %X, %X, %X, %X", r,g,b,a);
2166 DebuggerAppendMsg(" : u=%f, v=%f", t.x, t.y);
2170 VTX_DUMP(TRACE2("Setting Vertexes: %d - %d\n", dwV0, dwV0+dwNum-1));
2171 DEBUGGER_PAUSE_AND_DUMP(NEXT_VERTEX_CMD,{TRACE0("Paused at Vertex Cmd");});
2174 extern uint32 dwConkerVtxZAddr;
2175 void ProcessVertexDataConker(uint32 dwAddr, uint32 dwV0, uint32 dwNum)
2177 UpdateCombinedMatrix();
2179 FiddledVtx * pVtxBase = (FiddledVtx*)(g_pRDRAMu8 + dwAddr);
2180 g_pVtxBase = pVtxBase;
2181 //short *vertexColoraddr = (short*)(g_pRDRAMu8+dwConkerVtxZAddr);
2183 for (uint32 i = dwV0; i < dwV0 + dwNum; i++)
2185 SP_Timing(RSP_GBI0_Vtx);
2187 FiddledVtx & vert = pVtxBase[i - dwV0];
2189 g_vtxNonTransformed[i].x = (float)vert.x;
2190 g_vtxNonTransformed[i].y = (float)vert.y;
2191 g_vtxNonTransformed[i].z = (float)vert.z;
2193 #if !defined(NO_ASM)
2194 if( status.isSSEEnabled )
2195 SSEVec3Transform(i);
2199 Vec3Transform(&g_vtxTransformed[i], (XVECTOR3*)&g_vtxNonTransformed[i], &gRSPworldProject); // Convert to w=1
2200 g_vecProjected[i].w = 1.0f / g_vtxTransformed[i].w;
2201 g_vecProjected[i].x = g_vtxTransformed[i].x * g_vecProjected[i].w;
2202 g_vecProjected[i].y = g_vtxTransformed[i].y * g_vecProjected[i].w;
2203 g_vecProjected[i].z = g_vtxTransformed[i].z * g_vecProjected[i].w;
2206 g_fFogCoord[i] = g_vecProjected[i].z;
2207 if( g_vecProjected[i].w < 0 || g_vecProjected[i].z < 0 || g_fFogCoord[i] < gRSPfFogMin )
2208 g_fFogCoord[i] = gRSPfFogMin;
2212 uint32 *dat = (uint32*)(&vert);
2213 DebuggerAppendMsg("vtx %d: %08X %08X %08X %08X", i, dat[0],dat[1],dat[2],dat[3]);
2214 DebuggerAppendMsg(" : %f, %f, %f, %f",
2215 g_vtxTransformed[i].x,g_vtxTransformed[i].y,g_vtxTransformed[i].z,g_vtxTransformed[i].w);
2216 DebuggerAppendMsg(" : %f, %f, %f, %f",
2217 g_vecProjected[i].x,g_vecProjected[i].y,g_vecProjected[i].z,g_vecProjected[i].w);
2220 RSP_Vtx_Clipping(i);
2222 if( gRSP.bLightingEnable )
2225 uint32 r= ((gRSP.ambientLightColor>>16)&0xFF);
2226 uint32 g= ((gRSP.ambientLightColor>> 8)&0xFF);
2227 uint32 b= ((gRSP.ambientLightColor )&0xFF);
2228 for( uint32 k=1; k<=gRSPnumLights; k++)
2230 r += gRSPlights[k].r;
2231 g += gRSPlights[k].g;
2232 b += gRSPlights[k].b;
2243 g_dwVtxDifColor[i] = 0xFF000000;
2244 g_dwVtxDifColor[i] |= (r<<16);
2245 g_dwVtxDifColor[i] |= (g<< 8);
2246 g_dwVtxDifColor[i] |= (b );
2249 *(((uint8*)&(g_dwVtxDifColor[i]))+3) = vert.rgba.a; // still use alpha from the vertex
2253 if( (gRDP.geometryMode & G_SHADE) == 0 && gRSP.ucode < 5 ) //Shade is disabled
2255 g_dwVtxDifColor[i] = gRDP.primitiveColor;
2259 g_dwVtxDifColor[i] = COLOR_RGBA(vert.rgba.r, vert.rgba.g, vert.rgba.b, vert.rgba.a);
2263 if( options.bWinFrameMode )
2265 //g_vecProjected[i].z = 0;
2266 g_dwVtxDifColor[i] = COLOR_RGBA(vert.rgba.r, vert.rgba.g, vert.rgba.b, vert.rgba.a);
2269 ReplaceAlphaWithFogFactor(i);
2271 // Update texture coords n.b. need to divide tu/tv by bogus scale on addition to buffer
2272 //VECTOR2 & t = g_fVtxTxtCoords[i];
2274 // If the vert is already lit, then there is no normal (and hence we
2275 // can't generate tex coord)
2276 if (gRSP.bTextureGen && gRSP.bLightingEnable )
2278 g_normal.x = (float)*(char*)(g_pRDRAMu8+ (((i<<1)+0)^3)+dwConkerVtxZAddr);
2279 g_normal.y = (float)*(char*)(g_pRDRAMu8+ (((i<<1)+1)^3)+dwConkerVtxZAddr);
2280 g_normal.z = (float)*(char*)(g_pRDRAMu8+ (((i<<1)+2)^3)+dwConkerVtxZAddr);
2281 Vec3TransformNormal(g_normal, gRSPmodelViewTop);
2282 TexGen(g_fVtxTxtCoords[i].x, g_fVtxTxtCoords[i].y);
2286 g_fVtxTxtCoords[i].x = (float)vert.tu;
2287 g_fVtxTxtCoords[i].y = (float)vert.tv;
2291 VTX_DUMP(TRACE2("Setting Vertexes: %d - %d\n", dwV0, dwV0+dwNum-1));
2292 DEBUGGER_PAUSE_AND_DUMP(NEXT_VERTEX_CMD,{DebuggerAppendMsg("Paused at Vertex Cmd");});
2319 void ProcessVertexData_Rogue_Squadron(uint32 dwXYZAddr, uint32 dwColorAddr, uint32 dwXYZCmd, uint32 dwColorCmd)
2321 UpdateCombinedMatrix();
2324 uint32 dwNum = (dwXYZCmd&0xFF00)>>10;
2326 RS_Vtx_XYZ * pVtxXYZBase = (RS_Vtx_XYZ*)(g_pRDRAMu8 + dwXYZAddr);
2327 RS_Vtx_Color * pVtxColorBase = (RS_Vtx_Color*)(g_pRDRAMu8 + dwColorAddr);
2330 for (i = dwV0; i < dwV0 + dwNum; i++)
2332 RS_Vtx_XYZ & vertxyz = pVtxXYZBase[i - dwV0];
2333 RS_Vtx_Color & vertcolors = pVtxColorBase[i - dwV0];
2335 g_vtxNonTransformed[i].x = (float)vertxyz.x;
2336 g_vtxNonTransformed[i].y = (float)vertxyz.y;
2337 g_vtxNonTransformed[i].z = (float)vertxyz.z;
2339 #if !defined(NO_ASM)
2340 if( status.isSSEEnabled )
2341 SSEVec3Transform(i);
2345 Vec3Transform(&g_vtxTransformed[i], (XVECTOR3*)&g_vtxNonTransformed[i], &gRSPworldProject); // Convert to w=1
2346 g_vecProjected[i].w = 1.0f / g_vtxTransformed[i].w;
2347 g_vecProjected[i].x = g_vtxTransformed[i].x * g_vecProjected[i].w;
2348 g_vecProjected[i].y = g_vtxTransformed[i].y * g_vecProjected[i].w;
2349 g_vecProjected[i].z = g_vtxTransformed[i].z * g_vecProjected[i].w;
2354 DebuggerAppendMsg(" : %f, %f, %f, %f",
2355 g_vtxTransformed[i].x,g_vtxTransformed[i].y,g_vtxTransformed[i].z,g_vtxTransformed[i].w);
2356 DebuggerAppendMsg(" : %f, %f, %f, %f",
2357 g_vecProjected[i].x,g_vecProjected[i].y,g_vecProjected[i].z,g_vecProjected[i].w);
2360 g_fFogCoord[i] = g_vecProjected[i].z;
2361 if( g_vecProjected[i].w < 0 || g_vecProjected[i].z < 0 || g_fFogCoord[i] < gRSPfFogMin )
2362 g_fFogCoord[i] = gRSPfFogMin;
2364 RSP_Vtx_Clipping(i);
2366 if( gRSP.bLightingEnable )
2368 g_normal.x = (float)vertcolors.nx;
2369 g_normal.y = (float)vertcolors.ny;
2370 g_normal.z = (float)vertcolors.nz;
2372 #if !defined(NO_ASM)
2373 if( status.isSSEEnabled )
2375 SSEVec3TransformNormal();
2376 g_dwVtxDifColor[i] = SSELightVert();
2381 Vec3TransformNormal(g_normal, gRSPmodelViewTop);
2382 g_dwVtxDifColor[i] = LightVert(g_normal, i);
2384 *(((uint8*)&(g_dwVtxDifColor[i]))+3) = vertcolors.a; // still use alpha from the vertex
2388 if( (gRDP.geometryMode & G_SHADE) == 0 && gRSP.ucode < 5 ) //Shade is disabled
2390 g_dwVtxDifColor[i] = gRDP.primitiveColor;
2394 g_dwVtxDifColor[i] = COLOR_RGBA(vertcolors.r, vertcolors.g, vertcolors.b, vertcolors.a);
2398 if( options.bWinFrameMode )
2400 g_dwVtxDifColor[i] = COLOR_RGBA(vertcolors.r, vertcolors.g, vertcolors.b, vertcolors.a);
2403 ReplaceAlphaWithFogFactor(i);
2406 // Update texture coords n.b. need to divide tu/tv by bogus scale on addition to buffer
2407 VECTOR2 & t = g_fVtxTxtCoords[i];
2409 // If the vert is already lit, then there is no normal (and hence we
2410 // can't generate tex coord)
2411 if (gRSP.bTextureGen && gRSP.bLightingEnable && g_textures[gRSP.curTile].m_bTextureEnable )
2413 TexGen(g_fVtxTxtCoords[i].x, g_fVtxTxtCoords[i].y);
2417 t.x = (float)vert.tu;
2418 t.y = (float)vert.tv;
2423 VTX_DUMP(TRACE2("Setting Vertexes: %d - %d\n", dwV0, dwV0+dwNum-1));
2424 DEBUGGER_PAUSE_AND_DUMP(NEXT_VERTEX_CMD,{TRACE0("Paused at Vertex Cmd");});
2427 void SetLightCol(uint32 dwLight, uint32 dwCol)
2429 gRSPlights[dwLight].r = (uint8)((dwCol >> 24)&0xFF);
2430 gRSPlights[dwLight].g = (uint8)((dwCol >> 16)&0xFF);
2431 gRSPlights[dwLight].b = (uint8)((dwCol >> 8)&0xFF);
2432 gRSPlights[dwLight].a = 255; // Ignore light alpha
2433 gRSPlights[dwLight].fr = (float)gRSPlights[dwLight].r;
2434 gRSPlights[dwLight].fg = (float)gRSPlights[dwLight].g;
2435 gRSPlights[dwLight].fb = (float)gRSPlights[dwLight].b;
2436 gRSPlights[dwLight].fa = 255; // Ignore light alpha
2438 //TRACE1("Set light %d color", dwLight);
2439 LIGHT_DUMP(TRACE2("Set Light %d color: %08X", dwLight, dwCol));
2442 void SetLightDirection(uint32 dwLight, float x, float y, float z, float range)
2444 //gRSP.bLightIsUpdated = true;
2446 //gRSPlights[dwLight].ox = x;
2447 //gRSPlights[dwLight].oy = y;
2448 //gRSPlights[dwLight].oz = z;
2450 register float w = range == 0 ? (float)sqrt(x*x+y*y+z*z) : 1;
2452 gRSPlights[dwLight].x = x/w;
2453 gRSPlights[dwLight].y = y/w;
2454 gRSPlights[dwLight].z = z/w;
2455 gRSPlights[dwLight].range = range;
2456 DEBUGGER_PAUSE_AND_DUMP(NEXT_SET_LIGHT,TRACE5("Set Light %d dir: %.4f, %.4f, %.4f, %.4f", dwLight, x, y, z, range));
2459 static float maxS0, maxT0;
2460 static float maxS1, maxT1;
2461 static bool validS0, validT0;
2462 static bool validS1, validT1;
2464 void LogTextureCoords(float fTex0S, float fTex0T, float fTex1S, float fTex1T)
2468 if( fTex0S<0 || fTex0S>maxS0 ) validS0 = false;
2472 if( fTex0T<0 || fTex0T>maxT0 ) validT0 = false;
2476 if( fTex1S<0 || fTex1S>maxS1 ) validS1 = false;
2480 if( fTex1T<0 || fTex1T>maxT1 ) validT1 = false;
2484 bool CheckTextureCoords(int tex)
2488 return validS0&&validT0;
2492 return validS1&&validT1;
2496 void ResetTextureCoordsLog(float maxs0, float maxt0, float maxs1, float maxt1)
2502 validS0 = validT0 = true;
2503 validS1 = validT1 = true;
2506 void ForceMainTextureIndex(int dwTile)
2508 if( dwTile == 1 && !(CRender::g_pRender->IsTexel0Enable()) && CRender::g_pRender->IsTexel1Enable() )
2515 gRSP.curTile = dwTile;
2519 float HackZ2(float z)
2525 float HackZ(float z)
2529 if( z < 0.1 && z >= 0 )
2532 //return (10+z)/100;
2537 void HackZ(std::vector<XVECTOR3>& points)
2539 int size = points.size();
2540 for( int i=0; i<size; i++)
2542 XVECTOR3 &v = points[i];
2543 v.z = (float)HackZ(v.z);
2549 if( CDeviceBuilder::m_deviceGeneralType == DIRECTX_DEVICE )
2551 for( uint32 i=0; i<gRSP.numVertices; i++)
2553 g_vtxBuffer[i].z = HackZ(g_vtxBuffer[i].z);
2558 for( uint32 i=0; i<gRSP.numVertices; i++)
2560 float w = g_vtxProjected5[i][3];
2561 g_vtxProjected5[i][2] = HackZ(g_vtxProjected5[i][2]/w)*w;
2567 extern XMATRIX reverseXY;
2568 extern XMATRIX reverseY;
2570 void UpdateCombinedMatrix()
2572 if( gRSP.bMatrixIsUpdated )
2574 gRSPworldProject = gRSP.modelviewMtxs[gRSP.modelViewMtxTop] * gRSP.projectionMtxs[gRSP.projectionMtxTop];
2575 gRSP.bMatrixIsUpdated = false;
2576 gRSP.bCombinedMatrixIsUpdated = true;
2579 if( gRSP.bCombinedMatrixIsUpdated )
2581 if( options.enableHackForGames == HACK_REVERSE_XY_COOR )
2583 gRSPworldProject = gRSPworldProject * reverseXY;
2585 if( options.enableHackForGames == HACK_REVERSE_Y_COOR )
2587 gRSPworldProject = gRSPworldProject * reverseY;
2589 #if !defined(NO_ASM)
2590 if( status.isSSEEnabled )
2592 MatrixTranspose(&gRSPworldProjectTransported, &gRSPworldProject);
2595 gRSP.bCombinedMatrixIsUpdated = false;
2598 //if( gRSP.bWorldMatrixIsUpdated || gRSP.bLightIsUpdated )
2600 // // Update lights with transported world matrix
2601 // for( unsigned int l=0; l<gRSPnumLights; l++)
2603 // Vec3TransformCoord(&gRSPlights[l].td, &gRSPlights[l].od, &gRSPmodelViewTopTranspose);
2604 // Vec3Normalize(&gRSPlights[l].td,&gRSPlights[l].td);
2607 // gRSP.bWorldMatrixIsUpdated = false;
2608 // gRSP.bLightIsUpdated = false;