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 #elif defined(__ARM_NEON__0)
246 #define Vec3TransformNormal(vec, mtx) \
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" \
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" \
259 "vmul.f32 d0, d4, d4 \n\t" \
260 "vpadd.f32 d0, d0, d0 \n\t" \
261 "vmla.f32 d0, d5, d5 \n\t" \
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" \
272 "vmul.f32 q2, q2, d0[0] \n\t" \
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" \
281 #define Vec3TransformNormal(vec, m) \
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; }
292 #if !defined(__GNUC__) && !defined(NO_ASM)
293 __declspec( naked ) void __fastcall SSEVec3Transform(int i)
297 shl ecx,4; // ecx = i
299 movaps xmm1, DWORD PTR g_vtxNonTransformed [ecx]; // xmm1 as original vector
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
306 mulps xmm4, xmm1; // row 1
307 mulps xmm5, xmm1; // row 2
308 mulps xmm6, xmm1; // row 3
309 mulps xmm7, xmm1; // row 4
311 movhlps xmm0, xmm4; // xmm4 high to xmm0 low
312 movlhps xmm0, xmm5; // xmm5 low to xmm0 high
314 addps xmm4, xmm0; // result of add are in xmm4 low
315 addps xmm5, xmm0; // result of add are in xmm5 high
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
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
325 addps xmm4, xmm5; // results are in 1st and 2nd uint32
328 movhlps xmm0, xmm6; // xmm6 high to xmm0 low
329 movlhps xmm0, xmm7; // xmm7 low to xmm0 high
331 addps xmm6, xmm0; // result of add are in xmm6 low
332 addps xmm7, xmm0; // result of add are in xmm7 high
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
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
342 addps xmm6, xmm7; // results are in 1st and 2nd uint32
344 movlhps xmm4, xmm6; // final result is in xmm4
345 movaps DWORD PTR g_vtxTransformed [ecx], xmm4;
348 shufps xmm0,xmm0,0xff;
352 shufps xmm0,xmm0,0xe8;
355 movaps DWORD PTR g_vecProjected [ecx], xmm4;
363 __declspec( naked ) void __fastcall SSEVec3TransformDKR(XVECTOR4 &pOut, const XVECTOR4 &pV)
367 movaps xmm1, DWORD PTR [edx]; // xmm1 as original vector
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
374 mulps xmm4, xmm1; // row 1
375 mulps xmm5, xmm1; // row 2
376 mulps xmm6, xmm1; // row 3
377 mulps xmm7, xmm1; // row 4
379 movhlps xmm0, xmm4; // xmm4 high to xmm0 low
380 movlhps xmm0, xmm5; // xmm5 low to xmm0 high
382 addps xmm4, xmm0; // result of add are in xmm4 low
383 addps xmm5, xmm0; // result of add are in xmm5 high
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
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
393 addps xmm4, xmm5; // results are in 1st and 2nd uint32
396 movhlps xmm0, xmm6; // xmm6 high to xmm0 low
397 movlhps xmm0, xmm7; // xmm7 low to xmm0 high
399 addps xmm6, xmm0; // result of add are in xmm6 low
400 addps xmm7, xmm0; // result of add are in xmm7 high
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
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
410 addps xmm6, xmm7; // results are in 1st and 2nd uint32
412 movlhps xmm4, xmm6; // final result is in xmm4
413 movaps DWORD PTR [ecx], xmm4;
419 #elif defined(__GNUC__) && defined(__x86_64__) && !defined(NO_ASM)
420 void SSEVec3Transform(int i)
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"
464 : "r"(g_vtxNonTransformed), "r"(&gRSPworldProjectTransported.m[0][0]), "r"(g_vtxTransformed), "r"(g_vecProjected)
465 : "memory", "%xmm0", "%xmm1", "%xmm4", "%xmm5", "%xmm6", "%xmm7"
468 #elif !defined(NO_ASM) // 32-bit GCC assumed
469 void SSEVec3Transform(int i)
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"
512 : "r"(g_vtxNonTransformed), "r"(&gRSPworldProjectTransported.m[0][0]), "r"(g_vtxTransformed), "r"(g_vecProjected)
513 : "memory", "%xmm0", "%xmm1", "%xmm4", "%xmm5", "%xmm6", "%xmm7"
517 float real255 = 255.0f;
518 float real128 = 128.0f;
520 #if !defined(__GNUC__) && !defined(NO_ASM)
521 __declspec( naked ) void __fastcall SSEVec3TransformNormal()
525 mov DWORD PTR [g_normal][12], 0;
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
532 mulps xmm4, xmm1; // row 1
533 mulps xmm5, xmm1; // row 2
534 mulps xmm6, xmm1; // row 3
536 movhlps xmm0, xmm4; // xmm4 high to xmm0 low
537 movlhps xmm0, xmm5; // xmm5 low to xmm0 high
539 addps xmm4, xmm0; // result of add are in xmm4 low
540 addps xmm5, xmm0; // result of add are in xmm5 high
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
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,
549 addps xmm4, xmm5; // results are in 1st and 2nd uint32
552 mulps xmm1,xmm1; //square
554 shufps xmm7, xmm7,0x03;
557 movhlps xmm0, xmm6; // xmm6 high to xmm0 low
558 addps xmm6, xmm0; // result of add are in xmm6 low
561 shufps xmm0, xmm0, 0x03;
562 addss xmm0, xmm6; // result of add is at xmm0's 1st uint32
567 addss xmm7,xmm0; // xmm7 1st uint32 is the sum of squares
570 movaps DWORD PTR [g_normal], xmm4;
571 movss DWORD PTR [g_normal][12], xmm7;
580 movss DWORD PTR [g_normal][12], xmm7;
584 movaps DWORD PTR [g_normal], xmm4; // Normalized
585 mov DWORD PTR [g_normal][12], 0;
590 movss DWORD PTR [g_normal], xmm0;
591 movss DWORD PTR [g_normal][12], xmm0;
596 #elif defined(__GNUC__) && !defined(NO_ASM) // this code should compile for both 64-bit and 32-bit architectures
597 void SSEVec3TransformNormal(void)
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"
631 " movaps %%xmm4, (%0) \n"
632 " movss %%xmm7, 12(%0) \n"
634 " xorps %%xmm0, %%xmm0 \n"
635 " ucomiss %%xmm7, %%xmm0 \n"
637 " rsqrtss %%xmm7, %%xmm7 \n"
638 " shufps $0x00, %%xmm7, %%xmm7 \n"
640 " movss %%xmm7, 12(%0) \n"
642 " mulps %%xmm7, %%xmm4 \n"
643 " movaps %%xmm4, (%0) \n"
644 " movl $0, 12(%0) \n"
647 " movss %%xmm0, (%0) \n"
648 " movss %%xmm0, 12(%0) \n"
651 : "r"(&g_normal.x), "r"(&gRSPmodelViewTopTranspose.m[0][0])
652 : "memory", "cc", "%xmm0", "%xmm1", "%xmm4", "%xmm5", "%xmm6", "%xmm7"
657 void NormalizeNormalVec()
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
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
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}
682 : "d0", "d1", "d2", "d3", "d4", "d5", "memory"
687 float w = 1/sqrtf(g_normal.x*g_normal.x + g_normal.y*g_normal.y + g_normal.z*g_normal.z);
694 void InitRenderBase()
697 if( status.isSSEEnabled && !g_curRomInfo.bPrimaryDepthHack && options.enableHackForGames != HACK_FOR_NASCAR)
699 ProcessVertexData = ProcessVertexDataSSE;
702 #elif defined(__ARM_NEON__)
703 if( !g_curRomInfo.bPrimaryDepthHack && options.enableHackForGames != HACK_FOR_NASCAR && options.enableHackForGames != HACK_FOR_ZELDA_MM && !options.bWinFrameMode)
705 ProcessVertexData = ProcessVertexDataNEON;
710 ProcessVertexData = ProcessVertexDataNoSSE;
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;
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;
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;
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;
742 gRSP.DKRCMatrixIndex = gRSP.dwDKRVtxAddr = gRSP.dwDKRMatrixAddr = 0;
743 gRSP.DKRBillBoard = false;
745 gRSP.fTexScaleX = 1/32.0f;
746 gRSP.fTexScaleY = 1/32.0f;
747 gRSP.bTextureEnabled = FALSE;
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;
773 gRSP.DKRCMatrixIndex=0;
775 gRSP.DKRBillBoard = false;
777 gRSP.dwDKRMatrixAddr=0;
780 gRDP.geometryMode = 0;
783 gRDP.fillColor = 0xFFFFFFFF;
784 gRDP.originalFillColor =0;
787 gRSP.vertexMult = 10;
788 gRSP.bNearClip = false;
789 gRSP.bRejectVtx = false;
791 gRDP.texturesAreReloaded = false;
792 gRDP.textureIsChanged = false;
793 gRDP.colorsAreReloaded = false;
795 memset(&gRDP.otherMode,0,sizeof(RDP_OtherMode));
796 memset(&gRDP.tiles,0,sizeof(Tile)*8);
798 for( int i=0; i<MAX_VERTS; i++ )
801 g_vtxNonTransformed[i].w = 1;
804 memset(gRSPn64lights, 0, sizeof(N64Light)*16);
807 void SetFogMinMax(float fMin, float fMax, float fMul, float fOffset)
817 gRSPfFogMin = max(0,fMin/500-1);
818 gRSPfFogMax = fMax/500-1;
821 gRSPfFogDivider = 255/(gRSPfFogMax-gRSPfFogMin);
822 CRender::g_pRender->SetFogMinMax(fMin, fMax);
825 void InitVertexColors()
829 void InitVertexTextureConstants()
834 RenderTexture &tex0 = g_textures[gRSP.curTile];
835 //CTexture *surf = tex0.m_pCTexture;
836 Tile &tile0 = gRDP.tiles[gRSP.curTile];
838 scaleX = gRSP.fTexScaleX;
839 scaleY = gRSP.fTexScaleY;
841 gRSP.tex0scaleX = scaleX * tile0.fShiftScaleS/tex0.m_fTexWidth;
842 gRSP.tex0scaleY = scaleY * tile0.fShiftScaleT/tex0.m_fTexHeight;
844 gRSP.tex0OffsetX = tile0.fhilite_sl/tex0.m_fTexWidth;
845 gRSP.tex0OffsetY = tile0.fhilite_tl/tex0.m_fTexHeight;
847 if( CRender::g_pRender->IsTexel1Enable() )
849 RenderTexture &tex1 = g_textures[(gRSP.curTile+1)&7];
850 //CTexture *surf = tex1.m_pCTexture;
851 Tile &tile1 = gRDP.tiles[(gRSP.curTile+1)&7];
853 gRSP.tex1scaleX = scaleX * tile1.fShiftScaleS/tex1.m_fTexWidth;
854 gRSP.tex1scaleY = scaleY * tile1.fShiftScaleT/tex1.m_fTexHeight;
856 gRSP.tex1OffsetX = tile1.fhilite_sl/tex1.m_fTexWidth;
857 gRSP.tex1OffsetY = tile1.fhilite_tl/tex1.m_fTexHeight;
860 gRSP.texGenXRatio = tile0.fShiftScaleS;
861 gRSP.texGenYRatio = gRSP.fTexScaleX/gRSP.fTexScaleY*tex0.m_fTexWidth/tex0.m_fTexHeight*tile0.fShiftScaleT;
864 void TexGen(float &s, float &t)
866 if (gRDP.geometryMode & G_TEXTURE_GEN_LINEAR)
868 s = acosf(g_normal.x) / 3.14159f;
869 t = acosf(g_normal.y) / 3.14159f;
873 s = 0.5f * ( 1.0f + g_normal.x);
874 t = 0.5f * ( 1.0f - g_normal.y);
878 void ComputeLOD(bool openGL)
880 TLITVERTEX &v0 = g_vtxBuffer[0];
881 TLITVERTEX &v1 = g_vtxBuffer[1];
882 RenderTexture &tex0 = g_textures[gRSP.curTile];
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];
890 x = windowSetting.vpWidthW*x/windowSetting.fMultX/2;
891 y = windowSetting.vpHeightW*y/windowSetting.fMultY/2;
896 float x = (v0.x - v1.x)/ windowSetting.fMultX;
897 float y = (v0.y - v1.y)/ windowSetting.fMultY;
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;
906 dt = sqrtf((s0-s1)*(s0-s1)+(t0-t1)*(t0-t1));
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();
918 bool bHalfTxtScale=false;
919 extern uint32 lastSetTile;
920 #define noinline __attribute__((noinline))
922 static noinline void InitVertex_scale_hack_check(uint32 dwV)
924 // Check for txt scale hack
925 if( gRDP.tiles[lastSetTile].dwSize == TXT_SIZE_32b || gRDP.tiles[lastSetTile].dwSize == TXT_SIZE_4b )
927 int width = ((gRDP.tiles[lastSetTile].sh-gRDP.tiles[lastSetTile].sl+1)<<1);
928 int height = ((gRDP.tiles[lastSetTile].th-gRDP.tiles[lastSetTile].tl+1)<<1);
929 if( g_fVtxTxtCoords[dwV].x*gRSP.fTexScaleX == width || g_fVtxTxtCoords[dwV].y*gRSP.fTexScaleY == height )
936 static noinline void InitVertex_notopengl_or_clipper_adjust(TLITVERTEX &v, uint32 dwV)
938 v.x = g_vecProjected[dwV].x*gRSP.vtxXMul+gRSP.vtxXAdd;
939 v.y = g_vecProjected[dwV].y*gRSP.vtxYMul+gRSP.vtxYAdd;
940 v.z = (g_vecProjected[dwV].z + 1.0f) * 0.5f; // DirectX minZ=0, maxZ=1
941 //v.z = g_vecProjected[dwV].z; // DirectX minZ=0, maxZ=1
942 v.rhw = g_vecProjected[dwV].w;
943 VTX_DUMP(TRACE4(" Proj : x=%f, y=%f, z=%f, rhw=%f", v.x,v.y,v.z,v.rhw));
945 if( gRSP.bProcessSpecularColor )
947 v.dcSpecular = CRender::g_pRender->PostProcessSpecularColor();
948 if( gRSP.bFogEnabled )
950 v.dcSpecular &= 0x00FFFFFF;
951 uint32 fogFct = 0xFF-(uint8)((g_fFogCoord[dwV]-gRSPfFogMin)*gRSPfFogDivider);
952 v.dcSpecular |= (fogFct<<24);
955 else if( gRSP.bFogEnabled )
957 uint32 fogFct = 0xFF-(uint8)((g_fFogCoord[dwV]-gRSPfFogMin)*gRSPfFogDivider);
958 v.dcSpecular = (fogFct<<24);
962 static noinline void InitVertex_texgen_correct(TLITVERTEX &v, uint32 dwV)
964 // Correction for texGen result
966 RenderTexture &tex0 = g_textures[gRSP.curTile];
967 u0 = g_fVtxTxtCoords[dwV].x * 32 * 1024 * gRSP.fTexScaleX / tex0.m_fTexWidth;
968 v0 = g_fVtxTxtCoords[dwV].y * 32 * 1024 * gRSP.fTexScaleY / tex0.m_fTexHeight;
969 u0 *= (gRDP.tiles[gRSP.curTile].fShiftScaleS);
970 v0 *= (gRDP.tiles[gRSP.curTile].fShiftScaleT);
972 if( CRender::g_pRender->IsTexel1Enable() )
974 RenderTexture &tex1 = g_textures[(gRSP.curTile+1)&7];
975 u1 = g_fVtxTxtCoords[dwV].x * 32 * 1024 * gRSP.fTexScaleX / tex1.m_fTexWidth;
976 v1 = g_fVtxTxtCoords[dwV].y * 32 * 1024 * gRSP.fTexScaleY / tex1.m_fTexHeight;
977 u1 *= gRDP.tiles[(gRSP.curTile+1)&7].fShiftScaleS;
978 v1 *= gRDP.tiles[(gRSP.curTile+1)&7].fShiftScaleT;
979 CRender::g_pRender->SetVertexTextureUVCoord(v, u0, v0, u1, v1);
983 CRender::g_pRender->SetVertexTextureUVCoord(v, u0, v0);
988 static void multiply_subtract2(float *d, const float *m1, const float *m2, const float *s)
991 for (i = 0; i < 2; i++)
992 d[i] = m1[i] * m2[i] - s[i];
995 extern "C" void multiply_subtract2(float *d, const float *m1, const float *m2, const float *s);
998 void InitVertex(uint32 dwV, uint32 vtxIndex, bool bTexture, bool openGL)
1000 VTX_DUMP(TRACE2("Init vertex (%d) to vtx buf[%d]:", dwV, vtxIndex));
1003 openGL = 1; // what else there is?
1005 TLITVERTEX &v = g_vtxBuffer[vtxIndex];
1006 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));
1009 g_vtxProjected5[vtxIndex][0] = g_vtxTransformed[dwV].x;
1010 g_vtxProjected5[vtxIndex][1] = g_vtxTransformed[dwV].y;
1011 g_vtxProjected5[vtxIndex][2] = g_vtxTransformed[dwV].z;
1012 g_vtxProjected5[vtxIndex][3] = g_vtxTransformed[dwV].w;
1013 g_vtxProjected5[vtxIndex][4] = g_vecProjected[dwV].z;
1015 if( *(int *)&g_vtxTransformed[dwV].w < 0 )
1016 g_vtxProjected5[vtxIndex][4] = 0;
1018 g_vtxIndex[vtxIndex] = vtxIndex;
1021 if( __builtin_expect(!openGL || options.bOGLVertexClipper == TRUE, 0) )
1023 InitVertex_notopengl_or_clipper_adjust(v, dwV);
1025 VTX_DUMP(TRACE2(" (U,V): %f, %f", g_fVtxTxtCoords[dwV].x,g_fVtxTxtCoords[dwV].y));
1027 v.dcDiffuse = g_dwVtxDifColor[dwV];
1028 if( gRDP.otherMode.key_en )
1030 v.dcDiffuse &= 0x00FFFFFF;
1031 v.dcDiffuse |= (gRDP.keyA<<24);
1033 else if( gRDP.otherMode.aa_en && gRDP.otherMode.clr_on_cvg==0 )
1035 v.dcDiffuse |= 0xFF000000;
1038 if( gRSP.bProcessDiffuseColor )
1040 v.dcDiffuse = CRender::g_pRender->PostProcessDiffuseColor(v.dcDiffuse);
1042 if( options.bWinFrameMode )
1044 v.dcDiffuse = g_dwVtxDifColor[dwV];
1049 g_oglVtxColors[vtxIndex][0] = v.r;
1050 g_oglVtxColors[vtxIndex][1] = v.g;
1051 g_oglVtxColors[vtxIndex][2] = v.b;
1052 g_oglVtxColors[vtxIndex][3] = v.a;
1057 // If the vert is already lit, then there is no normal (and hence we can't generate tex coord)
1058 // Only scale if not generated automatically
1059 if ( __builtin_expect(gRSP.bTextureGen && gRSP.bLightingEnable, 0) )
1061 InitVertex_texgen_correct(v, dwV);
1066 multiply_subtract2(&tex0.u, &g_fVtxTxtCoords[dwV].x, &gRSP.tex0scaleX, &gRSP.tex0OffsetX);
1068 if( CRender::g_pRender->IsTexel1Enable() )
1071 multiply_subtract2(&tex1.u, &g_fVtxTxtCoords[dwV].x, &gRSP.tex1scaleX, &gRSP.tex1OffsetX);
1073 CRender::g_pRender->SetVertexTextureUVCoord(v, tex0, tex1);
1074 VTX_DUMP(TRACE2(" (tex0): %f, %f", tex0.u,tex0.v));
1075 VTX_DUMP(TRACE2(" (tex1): %f, %f", tex1.u,tex1.v));
1079 CRender::g_pRender->SetVertexTextureUVCoord(v, tex0);
1080 VTX_DUMP(TRACE2(" (tex0): %f, %f", tex0.u,tex0.v));
1084 if( __builtin_expect(g_curRomInfo.bTextureScaleHack && !bHalfTxtScale, 0) )
1085 InitVertex_scale_hack_check(dwV);
1088 VTX_DUMP(TRACE2(" DIF(%08X), SPE(%08X)", v.dcDiffuse, v.dcSpecular));
1089 VTX_DUMP(TRACE0(""));
1092 uint32 LightVert(XVECTOR4 & norm, int vidx)
1097 register float r = gRSP.fAmbientLightR;
1098 register float g = gRSP.fAmbientLightG;
1099 register float b = gRSP.fAmbientLightB;
1101 if( options.enableHackForGames != HACK_FOR_ZELDA_MM )
1103 for (register unsigned int l=0; l < gRSPnumLights; l++)
1105 fCosT = norm.x*gRSPlights[l].x + norm.y*gRSPlights[l].y + norm.z*gRSPlights[l].z;
1109 r += gRSPlights[l].fr * fCosT;
1110 g += gRSPlights[l].fg * fCosT;
1111 b += gRSPlights[l].fb * fCosT;
1118 bool transformed = false;
1120 for (register unsigned int l=0; l < gRSPnumLights; l++)
1122 if( gRSPlights[l].range == 0 )
1124 // Regular directional light
1125 fCosT = norm.x*gRSPlights[l].x + norm.y*gRSPlights[l].y + norm.z*gRSPlights[l].z;
1129 r += gRSPlights[l].fr * fCosT;
1130 g += gRSPlights[l].fg * fCosT;
1131 b += gRSPlights[l].fb * fCosT;
1134 else //if( (gRSPlights[l].col&0x00FFFFFF) != 0x00FFFFFF )
1139 Vec3Transform(&v, (XVECTOR3*)&g_vtxNonTransformed[vidx], &gRSPmodelViewTop); // Convert to w=1
1143 XVECTOR3 dir(gRSPlights[l].x - v.x, gRSPlights[l].y - v.y, gRSPlights[l].z - v.z);
1144 //XVECTOR3 dir(v.x-gRSPlights[l].x, v.y-gRSPlights[l].y, v.z-gRSPlights[l].z);
1145 float d2 = sqrtf(dir.x*dir.x+dir.y*dir.y+dir.z*dir.z);
1150 fCosT = norm.x*dir.x + norm.y*dir.y + norm.z*dir.z;
1154 //float f = d2/gRSPlights[l].range*50;
1155 float f = d2/15000*50;
1159 r += gRSPlights[l].fr * fCosT;
1160 g += gRSPlights[l].fg * fCosT;
1161 b += gRSPlights[l].fb * fCosT;
1167 if (r > 255) r = 255;
1168 if (g > 255) g = 255;
1169 if (b > 255) b = 255;
1170 return ((0xff000000)|(((uint32)r)<<16)|(((uint32)g)<<8)|((uint32)b));
1173 uint32 LightVertNew(XVECTOR4 & norm)
1178 register float r = gRSP.fAmbientLightR;
1179 register float g = gRSP.fAmbientLightG;
1180 register float b = gRSP.fAmbientLightB;
1183 for (register unsigned int l=0; l < gRSPnumLights; l++)
1185 fCosT = norm.x*gRSPlights[l].tx + norm.y*gRSPlights[l].ty + norm.z*gRSPlights[l].tz;
1189 r += gRSPlights[l].fr * fCosT;
1190 g += gRSPlights[l].fg * fCosT;
1191 b += gRSPlights[l].fb * fCosT;
1195 if (r > 255) r = 255;
1196 if (g > 255) g = 255;
1197 if (b > 255) b = 255;
1198 return ((0xff000000)|(((uint32)r)<<16)|(((uint32)g)<<8)|((uint32)b));
1206 #if !defined(__GNUC__) && !defined(NO_ASM)
1207 __declspec( naked ) uint32 __fastcall SSELightVert()
1211 movaps xmm3, DWORD PTR gRSP; // loading Ambient colors, xmm3 is the result color
1212 movaps xmm4, DWORD PTR [g_normal]; // xmm4 is the normal
1216 cmp ecx, DWORD PTR gRSPnumLights;
1220 movups xmm5, DWORD PTR gRSPlights[eax]; // Light Dir
1221 movups xmm1, DWORD PTR gRSPlights[0x14][eax]; // Light color
1222 mulps xmm5, xmm4; // Lightdir * normals
1226 shufps xmm5,xmm0,0x01;
1232 shufps xmm0,xmm0,0; // fcosT
1240 movss xmm0,DWORD PTR real255;
1244 // Without using a memory
1245 cvtss2si eax,xmm0; // move the 1st uint32 to eax
1248 shufps xmm0,xmm0,0E5h; // move the 2nd uint32 to the 1st uint32
1249 cvtss2si ecx,xmm0; // move the 1st uint32 to ecx
1252 shufps xmm0,xmm0,0E6h; // Move the 3rd uint32 to the 1st uint32
1259 #elif defined(__GNUC__) && defined(__x86_64__) && !defined(NO_ASM)
1260 uint32 SSELightVert(void)
1263 float f255 = 255.0, fZero = 0.0;
1265 asm volatile(" movaps %1, %%xmm3 \n" // xmm3 == gRSP.fAmbientLight{RGBA}
1266 " movaps %2, %%xmm4 \n" // xmm4 == g_normal.{xyz}
1267 " xor %%rcx, %%rcx \n"
1269 " cmpl %3, %%ecx \n"
1271 " mov %%rcx, %%rax \n"
1272 " imul $0x44, %%rax, %%rax \n"
1273 " movups (%4,%%rax,), %%xmm5 \n" // xmm5 == gRSPlights[l].{xyzr}
1274 " movups 20(%4,%%rax,), %%xmm1 \n" // xmm1 == gRSPlights[l].{frfgfbfa}
1275 " mulps %%xmm4, %%xmm5 \n"
1276 " movhlps %%xmm5, %%xmm0 \n"
1277 " addps %%xmm5, %%xmm0 \n"
1278 " shufps $0x01, %%xmm0, %%xmm5 \n"
1279 " addps %%xmm5, %%xmm0 \n"
1280 " comiss %6, %%xmm0 \n"
1282 " shufps $0x00, %%xmm0, %%xmm0 \n"
1283 " mulps %%xmm0, %%xmm1 \n"
1284 " addps %%xmm1, %%xmm3 \n"
1289 " movss %5, %%xmm0 \n"
1290 " shufps $0x00, %%xmm0, %%xmm0 \n"
1291 " minps %%xmm3, %%xmm0 \n"
1292 " cvtss2si %%xmm0, %%eax \n"
1293 " shll $0x10, %%eax \n"
1294 " orl $0xff000000, %%eax \n"
1295 " shufps $0xe5, %%xmm0, %%xmm0 \n"
1296 " cvtss2si %%xmm0, %%ecx \n"
1297 " shll $8, %%ecx \n"
1298 " orl %%ecx, %%eax \n"
1299 " shufps $0xe6, %%xmm0, %%xmm0 \n"
1300 " cvtss2si %%xmm0, %%ecx \n"
1301 " orl %%ecx, %%eax \n"
1303 : "m"(gRSP), "m"(g_normal), "m"(gRSPnumLights), "r"(gRSPlights), "m"(f255), "m"(fZero)
1304 : "%rcx", "memory", "cc", "%xmm0", "%xmm1", "%xmm3", "%xmm4", "%xmm5"
1308 #elif !defined(NO_ASM) // 32-bit GCC assumed
1309 uint32 SSELightVert(void)
1312 float f255 = 255.0, fZero = 0.0;
1314 asm volatile(" movaps %1, %%xmm3 \n"
1315 " movaps %2, %%xmm4 \n"
1316 " xor %%ecx, %%ecx \n"
1318 " cmpl %3, %%ecx \n"
1320 " mov %%ecx, %%eax \n"
1321 " imul $0x44, %%eax, %%eax \n"
1322 " movups (%4,%%eax,), %%xmm5 \n"
1323 " movups 20(%4,%%eax,), %%xmm1 \n"
1324 " mulps %%xmm4, %%xmm5 \n"
1325 " movhlps %%xmm5, %%xmm0 \n"
1326 " addps %%xmm5, %%xmm0 \n"
1327 " shufps $0x01, %%xmm0, %%xmm5 \n"
1328 " addps %%xmm5, %%xmm0 \n"
1329 " comiss %6, %%xmm0 \n"
1331 " shufps $0x00, %%xmm0, %%xmm0 \n"
1332 " mulps %%xmm0, %%xmm1 \n"
1333 " addps %%xmm1, %%xmm3 \n"
1338 " movss %5, %%xmm0 \n"
1339 " shufps $0x00, %%xmm0, %%xmm0 \n"
1340 " minps %%xmm3, %%xmm0 \n"
1341 " cvtss2si %%xmm0, %%eax \n"
1342 " shll $0x10, %%eax \n"
1343 " orl $0xff000000, %%eax \n"
1344 " shufps $0xe5, %%xmm0, %%xmm0 \n"
1345 " cvtss2si %%xmm0, %%ecx \n"
1346 " shll $8, %%ecx \n"
1347 " orl %%ecx, %%eax \n"
1348 " shufps $0xe6, %%xmm0, %%xmm0 \n"
1349 " cvtss2si %%xmm0, %%ecx \n"
1350 " orl %%ecx, %%eax \n"
1352 : "m"(gRSP), "m"(g_normal), "m"(gRSPnumLights), "r"(gRSPlights), "m"(f255), "m"(fZero)
1353 : "%rcx", "memory", "cc", "%xmm0", "%xmm1", "%xmm3", "%xmm4", "%xmm5"
1359 inline void ReplaceAlphaWithFogFactor(int i)
1361 if( gRDP.geometryMode & G_FOG )
1363 // Use fog factor to replace vertex alpha
1364 if( g_vecProjected[i].z > 1 )
1365 *(((uint8*)&(g_dwVtxDifColor[i]))+3) = 0xFF;
1366 if( g_vecProjected[i].z < 0 )
1367 *(((uint8*)&(g_dwVtxDifColor[i]))+3) = 0;
1369 *(((uint8*)&(g_dwVtxDifColor[i]))+3) = (uint8)(g_vecProjected[i].z*255);
1384 // Assumes dwAddr has already been checked!
1385 // Don't inline - it's too big with the transform macros
1387 #if !defined(NO_ASM)
1388 void ProcessVertexDataSSE(uint32 dwAddr, uint32 dwV0, uint32 dwNum)
1390 UpdateCombinedMatrix();
1392 // This function is called upon SPvertex
1393 // - do vertex matrix transform
1394 // - do vertex lighting
1395 // - do texture cooridinate transform if needed
1396 // - calculate normal vector
1398 // Output: - g_vecProjected[i] -> transformed vertex x,y,z
1399 // - g_vecProjected[i].w -> saved vertex 1/w
1400 // - g_dwVtxFlags[i] -> flags
1401 // - g_dwVtxDifColor[i] -> vertex color
1402 // - g_fVtxTxtCoords[i] -> vertex texture cooridinates
1404 FiddledVtx * pVtxBase = (FiddledVtx*)(g_pRDRAMu8 + dwAddr);
1405 g_pVtxBase = pVtxBase;
1407 for (uint32 i = dwV0; i < dwV0 + dwNum; i++)
1409 SP_Timing(RSP_GBI0_Vtx);
1411 FiddledVtx & vert = pVtxBase[i - dwV0];
1413 g_vtxNonTransformed[i].x = (float)vert.x;
1414 g_vtxNonTransformed[i].y = (float)vert.y;
1415 g_vtxNonTransformed[i].z = (float)vert.z;
1417 SSEVec3Transform(i);
1419 if( gRSP.bFogEnabled )
1421 g_fFogCoord[i] = g_vecProjected[i].z;
1422 if( g_vecProjected[i].w < 0 || g_vecProjected[i].z < 0 || g_fFogCoord[i] < gRSPfFogMin )
1423 g_fFogCoord[i] = gRSPfFogMin;
1426 ReplaceAlphaWithFogFactor(i);
1431 uint32 *dat = (uint32*)(&vert);
1432 DebuggerAppendMsg("vtx %d: %08X %08X %08X %08X", i, dat[0],dat[1],dat[2],dat[3]);
1433 DebuggerAppendMsg(" : %f, %f, %f, %f",
1434 g_vtxTransformed[i].x,g_vtxTransformed[i].y,g_vtxTransformed[i].z,g_vtxTransformed[i].w);
1435 DebuggerAppendMsg(" : %f, %f, %f, %f",
1436 g_vecProjected[i].x,g_vecProjected[i].y,g_vecProjected[i].z,g_vecProjected[i].w);
1439 RSP_Vtx_Clipping(i);
1441 if( gRSP.bLightingEnable )
1443 g_normal.x = (float)vert.norma.nx;
1444 g_normal.y = (float)vert.norma.ny;
1445 g_normal.z = (float)vert.norma.nz;
1447 SSEVec3TransformNormal();
1448 if( options.enableHackForGames != HACK_FOR_ZELDA_MM )
1449 g_dwVtxDifColor[i] = SSELightVert();
1451 g_dwVtxDifColor[i] = LightVert(g_normal, i);
1452 *(((uint8*)&(g_dwVtxDifColor[i]))+3) = vert.rgba.a; // still use alpha from the vertex
1456 if( (gRDP.geometryMode & G_SHADE) == 0 && gRSP.ucode < 5 ) //Shade is disabled
1459 g_dwVtxDifColor[i] = gRDP.primitiveColor;
1463 register IColor &color = *(IColor*)&g_dwVtxDifColor[i];
1464 color.b = vert.rgba.r;
1465 color.g = vert.rgba.g;
1466 color.r = vert.rgba.b;
1467 color.a = vert.rgba.a;
1471 if( options.bWinFrameMode )
1473 g_dwVtxDifColor[i] = COLOR_RGBA(vert.rgba.r, vert.rgba.g, vert.rgba.b, vert.rgba.a);
1476 // Update texture coords n.b. need to divide tu/tv by bogus scale on addition to buffer
1478 // If the vert is already lit, then there is no normal (and hence we
1479 // can't generate tex coord)
1480 if (gRSP.bTextureGen && gRSP.bLightingEnable )
1482 TexGen(g_fVtxTxtCoords[i].x, g_fVtxTxtCoords[i].y);
1486 g_fVtxTxtCoords[i].x = (float)vert.tu;
1487 g_fVtxTxtCoords[i].y = (float)vert.tv;
1491 VTX_DUMP(TRACE2("Setting Vertexes: %d - %d\n", dwV0, dwV0+dwNum-1));
1492 DEBUGGER_PAUSE_AND_DUMP(NEXT_VERTEX_CMD,{TRACE0("Paused at Vertex Cmd");});
1496 void ProcessVertexDataNoSSE(uint32 dwAddr, uint32 dwV0, uint32 dwNum)
1499 UpdateCombinedMatrix();
1501 // This function is called upon SPvertex
1502 // - do vertex matrix transform
1503 // - do vertex lighting
1504 // - do texture cooridinate transform if needed
1505 // - calculate normal vector
1507 // Output: - g_vecProjected[i] -> transformed vertex x,y,z
1508 // - g_vecProjected[i].w -> saved vertex 1/w
1509 // - g_dwVtxFlags[i] -> flags
1510 // - g_dwVtxDifColor[i] -> vertex color
1511 // - g_fVtxTxtCoords[i] -> vertex texture cooridinates
1513 FiddledVtx * pVtxBase = (FiddledVtx*)(g_pRDRAMu8 + dwAddr);
1514 g_pVtxBase = pVtxBase;
1516 for (uint32 i = dwV0; i < dwV0 + dwNum; i++)
1518 SP_Timing(RSP_GBI0_Vtx);
1520 FiddledVtx & vert = pVtxBase[i - dwV0];
1522 g_vtxNonTransformed[i].x = (float)vert.x;
1523 g_vtxNonTransformed[i].y = (float)vert.y;
1524 g_vtxNonTransformed[i].z = (float)vert.z;
1526 Vec3Transform(&g_vtxTransformed[i], (XVECTOR3*)&g_vtxNonTransformed[i], &gRSPworldProject); // Convert to w=1
1528 g_vecProjected[i].w = 1.0f / g_vtxTransformed[i].w;
1529 g_vecProjected[i].x = g_vtxTransformed[i].x * g_vecProjected[i].w;
1530 g_vecProjected[i].y = g_vtxTransformed[i].y * g_vecProjected[i].w;
1531 if ((g_curRomInfo.bPrimaryDepthHack || options.enableHackForGames == HACK_FOR_NASCAR ) && gRDP.otherMode.depth_source )
1533 g_vecProjected[i].z = gRDP.fPrimitiveDepth;
1534 g_vtxTransformed[i].z = gRDP.fPrimitiveDepth*g_vtxTransformed[i].w;
1538 g_vecProjected[i].z = g_vtxTransformed[i].z * g_vecProjected[i].w;
1541 if( gRSP.bFogEnabled )
1543 g_fFogCoord[i] = g_vecProjected[i].z;
1544 if( g_vecProjected[i].w < 0 || g_vecProjected[i].z < 0 || g_fFogCoord[i] < gRSPfFogMin )
1545 g_fFogCoord[i] = gRSPfFogMin;
1550 uint32 *dat = (uint32*)(&vert);
1551 DebuggerAppendMsg("vtx %d: %08X %08X %08X %08X", i, dat[0],dat[1],dat[2],dat[3]);
1552 DebuggerAppendMsg(" : %f, %f, %f, %f",
1553 g_vtxTransformed[i].x,g_vtxTransformed[i].y,g_vtxTransformed[i].z,g_vtxTransformed[i].w);
1554 DebuggerAppendMsg(" : %f, %f, %f, %f",
1555 g_vecProjected[i].x,g_vecProjected[i].y,g_vecProjected[i].z,g_vecProjected[i].w);
1558 RSP_Vtx_Clipping(i);
1560 if( gRSP.bLightingEnable )
1562 g_normal.x = (float)vert.norma.nx;
1563 g_normal.y = (float)vert.norma.ny;
1564 g_normal.z = (float)vert.norma.nz;
1566 Vec3TransformNormal(g_normal, gRSPmodelViewTop);
1567 g_dwVtxDifColor[i] = LightVert(g_normal, i);
1568 *(((uint8*)&(g_dwVtxDifColor[i]))+3) = vert.rgba.a; // still use alpha from the vertex
1572 if( (gRDP.geometryMode & G_SHADE) == 0 && gRSP.ucode < 5 ) //Shade is disabled
1575 g_dwVtxDifColor[i] = gRDP.primitiveColor;
1579 register IColor &color = *(IColor*)&g_dwVtxDifColor[i];
1580 color.b = vert.rgba.r;
1581 color.g = vert.rgba.g;
1582 color.r = vert.rgba.b;
1583 color.a = vert.rgba.a;
1587 if( options.bWinFrameMode )
1589 g_dwVtxDifColor[i] = COLOR_RGBA(vert.rgba.r, vert.rgba.g, vert.rgba.b, vert.rgba.a);
1592 ReplaceAlphaWithFogFactor(i);
1594 // Update texture coords n.b. need to divide tu/tv by bogus scale on addition to buffer
1596 // If the vert is already lit, then there is no normal (and hence we
1597 // can't generate tex coord)
1598 if (gRSP.bTextureGen && gRSP.bLightingEnable )
1600 TexGen(g_fVtxTxtCoords[i].x, g_fVtxTxtCoords[i].y);
1604 g_fVtxTxtCoords[i].x = (float)vert.tu;
1605 g_fVtxTxtCoords[i].y = (float)vert.tv;
1609 VTX_DUMP(TRACE2("Setting Vertexes: %d - %d\n", dwV0, dwV0+dwNum-1));
1610 DEBUGGER_PAUSE_AND_DUMP(NEXT_VERTEX_CMD,{TRACE0("Paused at Vertex Cmd");});
1615 #include "RenderBase_neon.h"
1617 extern "C" void pv_neon(XVECTOR4 *g_vtxTransformed, XVECTOR4 *g_vecProjected,
1618 uint32 *g_dwVtxDifColor, VECTOR2 *g_fVtxTxtCoords,
1619 float *g_fFogCoord, uint32 *g_clipFlag2,
1620 uint32 dwNum, int neon_state,
1621 const FiddledVtx *vtx,
1622 const Light *gRSPlights, const float *fRSPAmbientLightRGBA,
1623 const XMATRIX *gRSPworldProject, const XMATRIX *gRSPmodelViewTop,
1624 uint32 gRSPnumLights, float gRSPfFogMin,
1625 uint32 primitiveColor, uint32 primitiveColor_);
1627 extern "C" int tv_direction(const XVECTOR4 *v0, const XVECTOR4 *v1, const XVECTOR4 *v2);
1629 void ProcessVertexDataNEON(uint32 dwAddr, uint32 dwV0, uint32 dwNum)
1631 if (gRSP.bTextureGen && gRSP.bLightingEnable) {
1632 ProcessVertexDataNoSSE(dwAddr, dwV0,dwNum);
1637 // - g_clipFlag is not used at all
1638 // - g_fFogCoord is not used at all
1639 // - g_vtxNonTransformed is not used after ProcessVertexData*() returns
1640 // - g_normal - same
1643 if ( gRSP.bLightingEnable )
1644 neon_state |= PV_NEON_ENABLE_LIGHT;
1645 if ( (gRDP.geometryMode & G_SHADE) || gRSP.ucode >= 5 )
1646 neon_state |= PV_NEON_ENABLE_SHADE;
1647 if ( gRSP.bFogEnabled )
1648 neon_state |= PV_NEON_ENABLE_FOG;
1649 if ( gRDP.geometryMode & G_FOG )
1650 neon_state |= PV_NEON_FOG_ALPHA;
1654 UpdateCombinedMatrix();
1656 // This function is called upon SPvertex
1657 // - do vertex matrix transform
1658 // - do vertex lighting
1659 // - do texture cooridinate transform if needed
1660 // - calculate normal vector
1662 // Output: - g_vecProjected[i] -> transformed vertex x,y,z
1663 // - g_vecProjected[i].w -> saved vertex 1/w
1664 // - g_vtxTransformed[i]
1665 // - g_dwVtxDifColor[i] -> vertex color
1666 // - g_fVtxTxtCoords[i] -> vertex texture cooridinates
1667 // - g_fFogCoord[i] -> unused
1670 const FiddledVtx * pVtxBase = (const FiddledVtx*)(g_pRDRAMu8 + dwAddr);
1671 g_pVtxBase = (FiddledVtx *)pVtxBase;
1673 gRSPmodelViewTop._14 = gRSPmodelViewTop._24 =
1674 gRSPmodelViewTop._34 = 0;
1676 // SP_Timing(RSP_GBI0_Vtx);
1677 status.SPCycleCount += Timing_RSP_GBI0_Vtx * dwNum;
1682 pv_neon(&g_vtxTransformed[i], &g_vecProjected[i],
1683 &g_dwVtxDifColor[i], &g_fVtxTxtCoords[i],
1684 &g_fFogCoord[i], &g_clipFlag2[i],
1685 dwNum, neon_state, &pVtxBase[i - dwV0],
1686 gRSPlights, gRSP.fAmbientColors,
1687 &gRSPworldProject, &gRSPmodelViewTop,
1688 gRSPnumLights, gRSPfFogMin,
1689 gRDP.primitiveColor, gRDP.primitiveColor);
1692 for (i = dwV0; i < dwV0 + dwNum; i++)
1694 const FiddledVtx & vert = pVtxBase[i - dwV0];
1695 XVECTOR3 vtx_raw; // was g_vtxNonTransformed
1697 vtx_raw.x = (float)vert.x;
1698 vtx_raw.y = (float)vert.y;
1699 vtx_raw.z = (float)vert.z;
1701 Vec3Transform(&g_vtxTransformed[i], &vtx_raw, &gRSPworldProject); // Convert to w=1
1703 g_vecProjected[i].w = 1.0f / g_vtxTransformed[i].w;
1704 g_vecProjected[i].x = g_vtxTransformed[i].x * g_vecProjected[i].w;
1705 g_vecProjected[i].y = g_vtxTransformed[i].y * g_vecProjected[i].w;
1706 g_vecProjected[i].z = g_vtxTransformed[i].z * g_vecProjected[i].w;
1708 // RSP_Vtx_Clipping(i);
1710 if( g_vecProjected[i].w > 0 )
1712 if( g_vecProjected[i].x > 1 ) g_clipFlag2[i] |= X_CLIP_MAX;
1713 if( g_vecProjected[i].x < -1 ) g_clipFlag2[i] |= X_CLIP_MIN;
1714 if( g_vecProjected[i].y > 1 ) g_clipFlag2[i] |= Y_CLIP_MAX;
1715 if( g_vecProjected[i].y < -1 ) g_clipFlag2[i] |= Y_CLIP_MIN;
1718 if( neon_state & PV_NEON_ENABLE_LIGHT )
1720 XVECTOR3 normal; // was g_normal
1723 normal.x = (float)vert.norma.nx;
1724 normal.y = (float)vert.norma.ny;
1725 normal.z = (float)vert.norma.nz;
1727 Vec3TransformNormal(normal, gRSPmodelViewTop);
1729 r = gRSP.fAmbientLightR;
1730 g = gRSP.fAmbientLightG;
1731 b = gRSP.fAmbientLightB;
1733 for (unsigned int l=0; l < gRSPnumLights; l++)
1735 float fCosT = normal.x * gRSPlights[l].x + normal.y * gRSPlights[l].y + normal.z * gRSPlights[l].z;
1739 r += gRSPlights[l].fr * fCosT;
1740 g += gRSPlights[l].fg * fCosT;
1741 b += gRSPlights[l].fb * fCosT;
1744 if (r > 255) r = 255;
1745 if (g > 255) g = 255;
1746 if (b > 255) b = 255;
1747 g_dwVtxDifColor[i] = ((vert.rgba.a<<24)|(((uint32)r)<<16)|(((uint32)g)<<8)|((uint32)b));
1749 else if( neon_state & PV_NEON_ENABLE_SHADE )
1751 IColor &color = *(IColor*)&g_dwVtxDifColor[i];
1752 color.b = vert.rgba.r;
1753 color.g = vert.rgba.g;
1754 color.r = vert.rgba.b;
1755 color.a = vert.rgba.a;
1758 g_dwVtxDifColor[i] = gRDP.primitiveColor; // FLAT shade
1760 // ReplaceAlphaWithFogFactor(i);
1761 if( neon_state & PV_NEON_FOG_ALPHA )
1763 // Use fog factor to replace vertex alpha
1764 if( g_vecProjected[i].z > 1 )
1765 *(((uint8*)&(g_dwVtxDifColor[i]))+3) = 0xFF;
1766 // missing 'else' in original code??
1767 else if( g_vecProjected[i].z < 0 )
1768 *(((uint8*)&(g_dwVtxDifColor[i]))+3) = 0;
1770 *(((uint8*)&(g_dwVtxDifColor[i]))+3) = (uint8)(g_vecProjected[i].z*255);
1773 g_fVtxTxtCoords[i].x = (float)vert.tu;
1774 g_fVtxTxtCoords[i].y = (float)vert.tv;
1779 bool PrepareTriangle(uint32 dwV0, uint32 dwV1, uint32 dwV2)
1781 if( status.isVertexShaderEnabled || status.bUseHW_T_L )
1783 g_vtxIndex[gRSP.numVertices++] = dwV0;
1784 g_vtxIndex[gRSP.numVertices++] = dwV1;
1785 g_vtxIndex[gRSP.numVertices++] = dwV2;
1786 status.dwNumTrisRendered++;
1787 gRSP.maxVertexID = max(gRSP.maxVertexID,max(dwV0,max(dwV1,dwV2)));
1791 SP_Timing(SP_Each_Triangle);
1793 bool textureFlag = (CRender::g_pRender->IsTextureEnabled() || gRSP.ucode == 6 );
1794 bool openGL = CDeviceBuilder::m_deviceGeneralType == OGL_DEVICE;
1796 InitVertex(dwV0, gRSP.numVertices, textureFlag, openGL);
1797 InitVertex(dwV1, gRSP.numVertices+1, textureFlag, openGL);
1798 InitVertex(dwV2, gRSP.numVertices+2, textureFlag, openGL);
1800 if( __builtin_expect(gRSP.numVertices == 0 && g_curRomInfo.bEnableTxtLOD && gRDP.otherMode.text_lod, 0) )
1802 if( CRender::g_pRender->IsTexel1Enable() && CRender::g_pRender->m_pColorCombiner->m_pDecodedMux->isUsed(MUX_LODFRAC) )
1812 gRSP.numVertices += 3;
1813 status.dwNumTrisRendered++;
1821 // Returns TRUE if it thinks the triangle is visible
1822 // Returns FALSE if it is clipped
1823 bool IsTriangleVisible(uint32 dwV0, uint32 dwV1, uint32 dwV2)
1825 //return true; //fix me
1827 if( status.isVertexShaderEnabled || status.bUseHW_T_L ) return true; // We won't have access to transformed vertex data
1829 DEBUGGER_ONLY_IF( (!debuggerEnableTestTris || !debuggerEnableCullFace), {return TRUE;});
1832 // Check vertices are valid!
1833 if (dwV0 >= MAX_VERTS || dwV1 >= MAX_VERTS || dwV2 >= MAX_VERTS)
1837 // Here we AND all the flags. If any of the bits is set for all
1838 // 3 vertices, it means that all three x, y or z lie outside of
1839 // the current viewing volume.
1840 // Currently disabled - still seems a bit dodgy
1841 if ((gRSP.bCullFront || gRSP.bCullBack) && gRDP.otherMode.zmode != 3)
1843 XVECTOR4 & v0 = g_vecProjected[dwV0];
1844 XVECTOR4 & v1 = g_vecProjected[dwV1];
1845 XVECTOR4 & v2 = g_vecProjected[dwV2];
1847 // Only try to clip if the tri is onscreen. For some reason, this
1848 // method doesnt' work well when the z value is outside of screenspace
1849 //if (v0.z < 1 && v1.z < 1 && v2.z < 1)
1851 #ifndef __ARM_NEON__
1852 float V1 = v2.x - v0.x;
1853 float V2 = v2.y - v0.y;
1855 float W1 = v2.x - v1.x;
1856 float W2 = v2.y - v1.y;
1858 float fDirection = (V1 * W2) - (V2 * W1);
1859 fDirection = fDirection * v1.w * v2.w * v0.w;
1860 //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;
1862 // really returns float, but we only need sign
1863 int fDirection = tv_direction(&v0, &v1, &v2);
1866 if (fDirection < 0 && gRSP.bCullBack)
1868 status.dwNumTrisClipped++;
1871 else if (fDirection > 0 && gRSP.bCullFront)
1873 status.dwNumTrisClipped++;
1879 #ifdef ENABLE_CLIP_TRI
1880 //if( gRSP.bRejectVtx && (g_clipFlag[dwV0]|g_clipFlag[dwV1]|g_clipFlag[dwV2]) )
1882 if( g_clipFlag2[dwV0]&g_clipFlag2[dwV1]&g_clipFlag2[dwV2] )
1884 //DebuggerAppendMsg("Clipped");
1893 void SetPrimitiveColor(uint32 dwCol, uint32 LODMin, uint32 LODFrac)
1895 gRDP.colorsAreReloaded = true;
1896 gRDP.primitiveColor = dwCol;
1897 gRDP.primLODMin = LODMin;
1898 gRDP.primLODFrac = LODFrac;
1899 if( gRDP.primLODFrac < gRDP.primLODMin )
1901 gRDP.primLODFrac = gRDP.primLODMin;
1904 gRDP.fvPrimitiveColor[0] = ((dwCol>>16)&0xFF)/255.0f; //r
1905 gRDP.fvPrimitiveColor[1] = ((dwCol>>8)&0xFF)/255.0f; //g
1906 gRDP.fvPrimitiveColor[2] = ((dwCol)&0xFF)/255.0f; //b
1907 gRDP.fvPrimitiveColor[3] = ((dwCol>>24)&0xFF)/255.0f; //a
1910 void SetPrimitiveDepth(uint32 z, uint32 dwDZ)
1912 gRDP.primitiveDepth = z & 0x7FFF;
1913 gRDP.fPrimitiveDepth = (float)(gRDP.primitiveDepth)/(float)0x8000;
1915 //gRDP.fPrimitiveDepth = gRDP.fPrimitiveDepth*2-1;
1917 z=0xFFFF -> 1 the farest
1918 z=0 -> -1 the nearest
1924 if( (pauseAtNext && (eventToPause == NEXT_VERTEX_CMD || eventToPause == NEXT_FLUSH_TRI )) )//&& logTriangles )
1926 DebuggerAppendMsg("Set prim Depth: %f, (%08X, %08X)", gRDP.fPrimitiveDepth, z, dwDZ);
1931 void SetVertexXYZ(uint32 vertex, float x, float y, float z)
1933 g_vecProjected[vertex].x = x;
1934 g_vecProjected[vertex].y = y;
1935 g_vecProjected[vertex].z = z;
1937 g_vtxTransformed[vertex].x = x*g_vtxTransformed[vertex].w;
1938 g_vtxTransformed[vertex].y = y*g_vtxTransformed[vertex].w;
1939 g_vtxTransformed[vertex].z = z*g_vtxTransformed[vertex].w;
1942 void ModifyVertexInfo(uint32 where, uint32 vertex, uint32 val)
1946 case RSP_MV_WORD_OFFSET_POINT_RGBA: // Modify RGBA
1948 uint32 r = (val>>24)&0xFF;
1949 uint32 g = (val>>16)&0xFF;
1950 uint32 b = (val>>8)&0xFF;
1951 uint32 a = val&0xFF;
1952 g_dwVtxDifColor[vertex] = COLOR_RGBA(r, g, b, a);
1953 LOG_UCODE("Modify vert %d color, 0x%08x", vertex, g_dwVtxDifColor[vertex]);
1956 case RSP_MV_WORD_OFFSET_POINT_XYSCREEN: // Modify X,Y
1958 uint16 nX = (uint16)(val>>16);
1959 short x = *((short*)&nX);
1962 uint16 nY = (uint16)(val&0xFFFF);
1963 short y = *((short*)&nY);
1966 // Should do viewport transform.
1969 x -= windowSetting.uViWidth/2;
1970 y = windowSetting.uViHeight/2-y;
1972 if( options.bEnableHacks && ((*g_GraphicsInfo.VI_X_SCALE_REG)&0xF) != 0 )
1975 // I don't know why Tarzan is different
1976 SetVertexXYZ(vertex, x/windowSetting.fViWidth, y/windowSetting.fViHeight, g_vecProjected[vertex].z);
1980 // Toy Story 2 and other games
1981 SetVertexXYZ(vertex, x*2/windowSetting.fViWidth, y*2/windowSetting.fViHeight, g_vecProjected[vertex].z);
1984 LOG_UCODE("Modify vert %d: x=%d, y=%d", vertex, x, y);
1985 VTX_DUMP(TRACE3("Modify vert %d: (%d,%d)", vertex, x, y));
1988 case RSP_MV_WORD_OFFSET_POINT_ZSCREEN: // Modify C
1992 SetVertexXYZ(vertex, g_vecProjected[vertex].x, g_vecProjected[vertex].y, (((float)z/0x03FF)+0.5f)/2.0f );
1993 LOG_UCODE("Modify vert %d: z=%d", vertex, z);
1994 VTX_DUMP(TRACE2("Modify vert %d: z=%d", vertex, z));
1997 case RSP_MV_WORD_OFFSET_POINT_ST: // Texture
1999 short tu = short(val>>16);
2000 short tv = short(val & 0xFFFF);
2001 float ftu = tu / 32.0f;
2002 float ftv = tv / 32.0f;
2003 LOG_UCODE(" Setting vertex %d tu/tv to %f, %f", vertex, (float)tu, (float)tv);
2004 CRender::g_pRender->SetVtxTextureCoord(vertex, ftu/gRSP.fTexScaleX, ftv/gRSP.fTexScaleY);
2008 DEBUGGER_PAUSE_AND_DUMP(NEXT_VERTEX_CMD,{TRACE0("Paused at ModVertex Cmd");});
2011 void ProcessVertexDataDKR(uint32 dwAddr, uint32 dwV0, uint32 dwNum)
2013 UpdateCombinedMatrix();
2015 long long pVtxBase = (long long) (g_pRDRAMu8 + dwAddr);
2016 g_pVtxBase = (FiddledVtx*)pVtxBase;
2018 Matrix &matWorldProject = gRSP.DKRMatrixes[gRSP.DKRCMatrixIndex];
2023 if ((!gRSP.DKRBillBoard) || (gRSP.DKRCMatrixIndex != 2) )
2028 if( addbase && gRSP.DKRVtxCount == 0 && dwNum > 1 )
2033 LOG_UCODE(" ProcessVertexDataDKR, CMatrix = %d, Add base=%s", gRSP.DKRCMatrixIndex, gRSP.DKRBillBoard?"true":"false");
2034 VTX_DUMP(TRACE2("DKR Setting Vertexes\nCMatrix = %d, Add base=%s", gRSP.DKRCMatrixIndex, gRSP.DKRBillBoard?"true":"false"));
2037 uint32 end = dwV0 + dwNum;
2038 for (uint32 i = dwV0; i < end; i++)
2042 g_vtxNonTransformed[i].x = (float)*(short*)((pVtxBase+nOff + 0) ^ 2);
2043 g_vtxNonTransformed[i].y = (float)*(short*)((pVtxBase+nOff + 2) ^ 2);
2044 g_vtxNonTransformed[i].z = (float)*(short*)((pVtxBase+nOff + 4) ^ 2);
2046 //if( status.isSSEEnabled )
2047 // SSEVec3TransformDKR(g_vtxTransformed[i], g_vtxNonTransformed[i]);
2049 Vec3Transform(&g_vtxTransformed[i], (XVECTOR3*)&g_vtxNonTransformed[i], &matWorldProject); // Convert to w=1
2051 if( gRSP.DKRVtxCount == 0 && dwNum==1 )
2053 gRSP.DKRBaseVec.x = g_vtxTransformed[i].x;
2054 gRSP.DKRBaseVec.y = g_vtxTransformed[i].y;
2055 gRSP.DKRBaseVec.z = g_vtxTransformed[i].z;
2056 gRSP.DKRBaseVec.w = g_vtxTransformed[i].w;
2060 g_vtxTransformed[i].x += gRSP.DKRBaseVec.x;
2061 g_vtxTransformed[i].y += gRSP.DKRBaseVec.y;
2062 g_vtxTransformed[i].z += gRSP.DKRBaseVec.z;
2063 g_vtxTransformed[i].w = gRSP.DKRBaseVec.w;
2066 g_vecProjected[i].w = 1.0f / g_vtxTransformed[i].w;
2067 g_vecProjected[i].x = g_vtxTransformed[i].x * g_vecProjected[i].w;
2068 g_vecProjected[i].y = g_vtxTransformed[i].y * g_vecProjected[i].w;
2069 g_vecProjected[i].z = g_vtxTransformed[i].z * g_vecProjected[i].w;
2073 VTX_DUMP(TRACE5("vtx %d: %f, %f, %f, %f", i,
2074 g_vtxTransformed[i].x,g_vtxTransformed[i].y,g_vtxTransformed[i].z,g_vtxTransformed[i].w));
2076 if( gRSP.bFogEnabled )
2078 g_fFogCoord[i] = g_vecProjected[i].z;
2079 if( g_vecProjected[i].w < 0 || g_vecProjected[i].z < 0 || g_fFogCoord[i] < gRSPfFogMin )
2080 g_fFogCoord[i] = gRSPfFogMin;
2083 RSP_Vtx_Clipping(i);
2085 short wA = *(short*)((pVtxBase+nOff + 6) ^ 2);
2086 short wB = *(short*)((pVtxBase+nOff + 8) ^ 2);
2088 s8 r = (s8)(wA >> 8);
2090 s8 b = (s8)(wB >> 8);
2093 if (gRSP.bLightingEnable)
2095 g_normal.x = (char)r; //norma.nx;
2096 g_normal.y = (char)g; //norma.ny;
2097 g_normal.z = (char)b; //norma.nz;
2099 Vec3TransformNormal(g_normal, matWorldProject)
2100 #if !defined(NO_ASM)
2101 if( status.isSSEEnabled )
2102 g_dwVtxDifColor[i] = SSELightVert();
2105 g_dwVtxDifColor[i] = LightVert(g_normal, i);
2115 // Assign true vert colour after lighting/fogging
2116 g_dwVtxDifColor[i] = COLOR_RGBA(nR, nG, nB, nA);
2119 ReplaceAlphaWithFogFactor(i);
2121 g_fVtxTxtCoords[i].x = g_fVtxTxtCoords[i].y = 1;
2127 DEBUGGER_PAUSE_AND_DUMP(NEXT_VERTEX_CMD,{DebuggerAppendMsg("Paused at DKR Vertex Cmd, v0=%d, vn=%d, addr=%08X", dwV0, dwNum, dwAddr);});
2131 extern uint32 dwPDCIAddr;
2132 void ProcessVertexDataPD(uint32 dwAddr, uint32 dwV0, uint32 dwNum)
2134 UpdateCombinedMatrix();
2136 N64VtxPD * pVtxBase = (N64VtxPD*)(g_pRDRAMu8 + dwAddr);
2137 g_pVtxBase = (FiddledVtx*)pVtxBase; // Fix me
2139 for (uint32 i = dwV0; i < dwV0 + dwNum; i++)
2141 N64VtxPD &vert = pVtxBase[i - dwV0];
2143 g_vtxNonTransformed[i].x = (float)vert.x;
2144 g_vtxNonTransformed[i].y = (float)vert.y;
2145 g_vtxNonTransformed[i].z = (float)vert.z;
2147 #if !defined(NO_ASM)
2148 if( status.isSSEEnabled )
2149 SSEVec3Transform(i);
2153 Vec3Transform(&g_vtxTransformed[i], (XVECTOR3*)&g_vtxNonTransformed[i], &gRSPworldProject); // Convert to w=1
2154 g_vecProjected[i].w = 1.0f / g_vtxTransformed[i].w;
2155 g_vecProjected[i].x = g_vtxTransformed[i].x * g_vecProjected[i].w;
2156 g_vecProjected[i].y = g_vtxTransformed[i].y * g_vecProjected[i].w;
2157 g_vecProjected[i].z = g_vtxTransformed[i].z * g_vecProjected[i].w;
2160 g_fFogCoord[i] = g_vecProjected[i].z;
2161 if( g_vecProjected[i].w < 0 || g_vecProjected[i].z < 0 || g_fFogCoord[i] < gRSPfFogMin )
2162 g_fFogCoord[i] = gRSPfFogMin;
2164 RSP_Vtx_Clipping(i);
2166 uint8 *addr = g_pRDRAMu8+dwPDCIAddr+ (vert.cidx&0xFF);
2172 if( gRSP.bLightingEnable )
2174 g_normal.x = (char)r;
2175 g_normal.y = (char)g;
2176 g_normal.z = (char)b;
2177 #if !defined(NO_ASM)
2178 if( status.isSSEEnabled )
2180 SSEVec3TransformNormal();
2181 g_dwVtxDifColor[i] = SSELightVert();
2186 Vec3TransformNormal(g_normal, gRSPmodelViewTop);
2187 g_dwVtxDifColor[i] = LightVert(g_normal, i);
2189 *(((uint8*)&(g_dwVtxDifColor[i]))+3) = (uint8)a; // still use alpha from the vertex
2193 if( (gRDP.geometryMode & G_SHADE) == 0 && gRSP.ucode < 5 ) //Shade is disabled
2195 g_dwVtxDifColor[i] = gRDP.primitiveColor;
2199 g_dwVtxDifColor[i] = COLOR_RGBA(r, g, b, a);
2203 if( options.bWinFrameMode )
2205 g_dwVtxDifColor[i] = COLOR_RGBA(r, g, b, a);
2208 ReplaceAlphaWithFogFactor(i);
2210 VECTOR2 & t = g_fVtxTxtCoords[i];
2211 if (gRSP.bTextureGen && gRSP.bLightingEnable )
2213 // Not sure if we should transform the normal here
2214 //Matrix & matWV = gRSP.projectionMtxs[gRSP.projectionMtxTop];
2215 //Vec3TransformNormal(g_normal, matWV);
2217 TexGen(g_fVtxTxtCoords[i].x, g_fVtxTxtCoords[i].y);
2228 DebuggerAppendMsg("vtx %d: %d %d %d", i, vert.x,vert.y,vert.z);
2229 DebuggerAppendMsg(" : %f, %f, %f, %f",
2230 g_vtxTransformed[i].x,g_vtxTransformed[i].y,g_vtxTransformed[i].z,g_vtxTransformed[i].w);
2231 DebuggerAppendMsg(" : %X, %X, %X, %X", r,g,b,a);
2232 DebuggerAppendMsg(" : u=%f, v=%f", t.x, t.y);
2236 VTX_DUMP(TRACE2("Setting Vertexes: %d - %d\n", dwV0, dwV0+dwNum-1));
2237 DEBUGGER_PAUSE_AND_DUMP(NEXT_VERTEX_CMD,{TRACE0("Paused at Vertex Cmd");});
2240 extern uint32 dwConkerVtxZAddr;
2241 void ProcessVertexDataConker(uint32 dwAddr, uint32 dwV0, uint32 dwNum)
2243 UpdateCombinedMatrix();
2245 FiddledVtx * pVtxBase = (FiddledVtx*)(g_pRDRAMu8 + dwAddr);
2246 g_pVtxBase = pVtxBase;
2247 //short *vertexColoraddr = (short*)(g_pRDRAMu8+dwConkerVtxZAddr);
2249 for (uint32 i = dwV0; i < dwV0 + dwNum; i++)
2251 SP_Timing(RSP_GBI0_Vtx);
2253 FiddledVtx & vert = pVtxBase[i - dwV0];
2255 g_vtxNonTransformed[i].x = (float)vert.x;
2256 g_vtxNonTransformed[i].y = (float)vert.y;
2257 g_vtxNonTransformed[i].z = (float)vert.z;
2259 #if !defined(NO_ASM)
2260 if( status.isSSEEnabled )
2261 SSEVec3Transform(i);
2265 Vec3Transform(&g_vtxTransformed[i], (XVECTOR3*)&g_vtxNonTransformed[i], &gRSPworldProject); // Convert to w=1
2266 g_vecProjected[i].w = 1.0f / g_vtxTransformed[i].w;
2267 g_vecProjected[i].x = g_vtxTransformed[i].x * g_vecProjected[i].w;
2268 g_vecProjected[i].y = g_vtxTransformed[i].y * g_vecProjected[i].w;
2269 g_vecProjected[i].z = g_vtxTransformed[i].z * g_vecProjected[i].w;
2272 g_fFogCoord[i] = g_vecProjected[i].z;
2273 if( g_vecProjected[i].w < 0 || g_vecProjected[i].z < 0 || g_fFogCoord[i] < gRSPfFogMin )
2274 g_fFogCoord[i] = gRSPfFogMin;
2278 uint32 *dat = (uint32*)(&vert);
2279 DebuggerAppendMsg("vtx %d: %08X %08X %08X %08X", i, dat[0],dat[1],dat[2],dat[3]);
2280 DebuggerAppendMsg(" : %f, %f, %f, %f",
2281 g_vtxTransformed[i].x,g_vtxTransformed[i].y,g_vtxTransformed[i].z,g_vtxTransformed[i].w);
2282 DebuggerAppendMsg(" : %f, %f, %f, %f",
2283 g_vecProjected[i].x,g_vecProjected[i].y,g_vecProjected[i].z,g_vecProjected[i].w);
2286 RSP_Vtx_Clipping(i);
2288 if( gRSP.bLightingEnable )
2291 uint32 r= ((gRSP.ambientLightColor>>16)&0xFF);
2292 uint32 g= ((gRSP.ambientLightColor>> 8)&0xFF);
2293 uint32 b= ((gRSP.ambientLightColor )&0xFF);
2294 for( uint32 k=1; k<=gRSPnumLights; k++)
2296 r += gRSPlights[k].r;
2297 g += gRSPlights[k].g;
2298 b += gRSPlights[k].b;
2309 g_dwVtxDifColor[i] = 0xFF000000;
2310 g_dwVtxDifColor[i] |= (r<<16);
2311 g_dwVtxDifColor[i] |= (g<< 8);
2312 g_dwVtxDifColor[i] |= (b );
2315 *(((uint8*)&(g_dwVtxDifColor[i]))+3) = vert.rgba.a; // still use alpha from the vertex
2319 if( (gRDP.geometryMode & G_SHADE) == 0 && gRSP.ucode < 5 ) //Shade is disabled
2321 g_dwVtxDifColor[i] = gRDP.primitiveColor;
2325 g_dwVtxDifColor[i] = COLOR_RGBA(vert.rgba.r, vert.rgba.g, vert.rgba.b, vert.rgba.a);
2329 if( options.bWinFrameMode )
2331 //g_vecProjected[i].z = 0;
2332 g_dwVtxDifColor[i] = COLOR_RGBA(vert.rgba.r, vert.rgba.g, vert.rgba.b, vert.rgba.a);
2335 ReplaceAlphaWithFogFactor(i);
2337 // Update texture coords n.b. need to divide tu/tv by bogus scale on addition to buffer
2338 //VECTOR2 & t = g_fVtxTxtCoords[i];
2340 // If the vert is already lit, then there is no normal (and hence we
2341 // can't generate tex coord)
2342 if (gRSP.bTextureGen && gRSP.bLightingEnable )
2344 g_normal.x = (float)*(char*)(g_pRDRAMu8+ (((i<<1)+0)^3)+dwConkerVtxZAddr);
2345 g_normal.y = (float)*(char*)(g_pRDRAMu8+ (((i<<1)+1)^3)+dwConkerVtxZAddr);
2346 g_normal.z = (float)*(char*)(g_pRDRAMu8+ (((i<<1)+2)^3)+dwConkerVtxZAddr);
2347 Vec3TransformNormal(g_normal, gRSPmodelViewTop);
2348 TexGen(g_fVtxTxtCoords[i].x, g_fVtxTxtCoords[i].y);
2352 g_fVtxTxtCoords[i].x = (float)vert.tu;
2353 g_fVtxTxtCoords[i].y = (float)vert.tv;
2357 VTX_DUMP(TRACE2("Setting Vertexes: %d - %d\n", dwV0, dwV0+dwNum-1));
2358 DEBUGGER_PAUSE_AND_DUMP(NEXT_VERTEX_CMD,{DebuggerAppendMsg("Paused at Vertex Cmd");});
2385 void ProcessVertexData_Rogue_Squadron(uint32 dwXYZAddr, uint32 dwColorAddr, uint32 dwXYZCmd, uint32 dwColorCmd)
2387 UpdateCombinedMatrix();
2390 uint32 dwNum = (dwXYZCmd&0xFF00)>>10;
2392 RS_Vtx_XYZ * pVtxXYZBase = (RS_Vtx_XYZ*)(g_pRDRAMu8 + dwXYZAddr);
2393 RS_Vtx_Color * pVtxColorBase = (RS_Vtx_Color*)(g_pRDRAMu8 + dwColorAddr);
2396 for (i = dwV0; i < dwV0 + dwNum; i++)
2398 RS_Vtx_XYZ & vertxyz = pVtxXYZBase[i - dwV0];
2399 RS_Vtx_Color & vertcolors = pVtxColorBase[i - dwV0];
2401 g_vtxNonTransformed[i].x = (float)vertxyz.x;
2402 g_vtxNonTransformed[i].y = (float)vertxyz.y;
2403 g_vtxNonTransformed[i].z = (float)vertxyz.z;
2405 #if !defined(NO_ASM)
2406 if( status.isSSEEnabled )
2407 SSEVec3Transform(i);
2411 Vec3Transform(&g_vtxTransformed[i], (XVECTOR3*)&g_vtxNonTransformed[i], &gRSPworldProject); // Convert to w=1
2412 g_vecProjected[i].w = 1.0f / g_vtxTransformed[i].w;
2413 g_vecProjected[i].x = g_vtxTransformed[i].x * g_vecProjected[i].w;
2414 g_vecProjected[i].y = g_vtxTransformed[i].y * g_vecProjected[i].w;
2415 g_vecProjected[i].z = g_vtxTransformed[i].z * g_vecProjected[i].w;
2420 DebuggerAppendMsg(" : %f, %f, %f, %f",
2421 g_vtxTransformed[i].x,g_vtxTransformed[i].y,g_vtxTransformed[i].z,g_vtxTransformed[i].w);
2422 DebuggerAppendMsg(" : %f, %f, %f, %f",
2423 g_vecProjected[i].x,g_vecProjected[i].y,g_vecProjected[i].z,g_vecProjected[i].w);
2426 g_fFogCoord[i] = g_vecProjected[i].z;
2427 if( g_vecProjected[i].w < 0 || g_vecProjected[i].z < 0 || g_fFogCoord[i] < gRSPfFogMin )
2428 g_fFogCoord[i] = gRSPfFogMin;
2430 RSP_Vtx_Clipping(i);
2432 if( gRSP.bLightingEnable )
2434 g_normal.x = (float)vertcolors.nx;
2435 g_normal.y = (float)vertcolors.ny;
2436 g_normal.z = (float)vertcolors.nz;
2438 #if !defined(NO_ASM)
2439 if( status.isSSEEnabled )
2441 SSEVec3TransformNormal();
2442 g_dwVtxDifColor[i] = SSELightVert();
2447 Vec3TransformNormal(g_normal, gRSPmodelViewTop);
2448 g_dwVtxDifColor[i] = LightVert(g_normal, i);
2450 *(((uint8*)&(g_dwVtxDifColor[i]))+3) = vertcolors.a; // still use alpha from the vertex
2454 if( (gRDP.geometryMode & G_SHADE) == 0 && gRSP.ucode < 5 ) //Shade is disabled
2456 g_dwVtxDifColor[i] = gRDP.primitiveColor;
2460 g_dwVtxDifColor[i] = COLOR_RGBA(vertcolors.r, vertcolors.g, vertcolors.b, vertcolors.a);
2464 if( options.bWinFrameMode )
2466 g_dwVtxDifColor[i] = COLOR_RGBA(vertcolors.r, vertcolors.g, vertcolors.b, vertcolors.a);
2469 ReplaceAlphaWithFogFactor(i);
2472 // Update texture coords n.b. need to divide tu/tv by bogus scale on addition to buffer
2473 VECTOR2 & t = g_fVtxTxtCoords[i];
2475 // If the vert is already lit, then there is no normal (and hence we
2476 // can't generate tex coord)
2477 if (gRSP.bTextureGen && gRSP.bLightingEnable && g_textures[gRSP.curTile].m_bTextureEnable )
2479 TexGen(g_fVtxTxtCoords[i].x, g_fVtxTxtCoords[i].y);
2483 t.x = (float)vert.tu;
2484 t.y = (float)vert.tv;
2489 VTX_DUMP(TRACE2("Setting Vertexes: %d - %d\n", dwV0, dwV0+dwNum-1));
2490 DEBUGGER_PAUSE_AND_DUMP(NEXT_VERTEX_CMD,{TRACE0("Paused at Vertex Cmd");});
2493 void SetLightCol(uint32 dwLight, uint32 dwCol)
2495 gRSPlights[dwLight].r = (uint8)((dwCol >> 24)&0xFF);
2496 gRSPlights[dwLight].g = (uint8)((dwCol >> 16)&0xFF);
2497 gRSPlights[dwLight].b = (uint8)((dwCol >> 8)&0xFF);
2498 gRSPlights[dwLight].a = 255; // Ignore light alpha
2499 gRSPlights[dwLight].fr = (float)gRSPlights[dwLight].r;
2500 gRSPlights[dwLight].fg = (float)gRSPlights[dwLight].g;
2501 gRSPlights[dwLight].fb = (float)gRSPlights[dwLight].b;
2502 gRSPlights[dwLight].fa = 255; // Ignore light alpha
2504 //TRACE1("Set light %d color", dwLight);
2505 LIGHT_DUMP(TRACE2("Set Light %d color: %08X", dwLight, dwCol));
2508 void SetLightDirection(uint32 dwLight, float x, float y, float z, float range)
2510 //gRSP.bLightIsUpdated = true;
2512 //gRSPlights[dwLight].ox = x;
2513 //gRSPlights[dwLight].oy = y;
2514 //gRSPlights[dwLight].oz = z;
2516 register float w = range == 0 ? (float)sqrt(x*x+y*y+z*z) : 1;
2518 gRSPlights[dwLight].x = x/w;
2519 gRSPlights[dwLight].y = y/w;
2520 gRSPlights[dwLight].z = z/w;
2521 gRSPlights[dwLight].range = range;
2522 DEBUGGER_PAUSE_AND_DUMP(NEXT_SET_LIGHT,TRACE5("Set Light %d dir: %.4f, %.4f, %.4f, %.4f", dwLight, x, y, z, range));
2525 static float maxS0, maxT0;
2526 static float maxS1, maxT1;
2527 static bool validS0, validT0;
2528 static bool validS1, validT1;
2530 void LogTextureCoords(float fTex0S, float fTex0T, float fTex1S, float fTex1T)
2534 if( fTex0S<0 || fTex0S>maxS0 ) validS0 = false;
2538 if( fTex0T<0 || fTex0T>maxT0 ) validT0 = false;
2542 if( fTex1S<0 || fTex1S>maxS1 ) validS1 = false;
2546 if( fTex1T<0 || fTex1T>maxT1 ) validT1 = false;
2550 bool CheckTextureCoords(int tex)
2554 return validS0&&validT0;
2558 return validS1&&validT1;
2562 void ResetTextureCoordsLog(float maxs0, float maxt0, float maxs1, float maxt1)
2568 validS0 = validT0 = true;
2569 validS1 = validT1 = true;
2572 void ForceMainTextureIndex(int dwTile)
2574 if( dwTile == 1 && !(CRender::g_pRender->IsTexel0Enable()) && CRender::g_pRender->IsTexel1Enable() )
2581 gRSP.curTile = dwTile;
2585 float HackZ2(float z)
2591 float HackZ(float z)
2595 if( z < 0.1 && z >= 0 )
2598 //return (10+z)/100;
2603 void HackZ(std::vector<XVECTOR3>& points)
2605 int size = points.size();
2606 for( int i=0; i<size; i++)
2608 XVECTOR3 &v = points[i];
2609 v.z = (float)HackZ(v.z);
2615 if( CDeviceBuilder::m_deviceGeneralType == DIRECTX_DEVICE )
2617 for( uint32 i=0; i<gRSP.numVertices; i++)
2619 g_vtxBuffer[i].z = HackZ(g_vtxBuffer[i].z);
2624 for( uint32 i=0; i<gRSP.numVertices; i++)
2626 float w = g_vtxProjected5[i][3];
2627 g_vtxProjected5[i][2] = HackZ(g_vtxProjected5[i][2]/w)*w;
2633 extern XMATRIX reverseXY;
2634 extern XMATRIX reverseY;
2636 void UpdateCombinedMatrix()
2638 if( gRSP.bMatrixIsUpdated )
2640 gRSPworldProject = gRSP.modelviewMtxs[gRSP.modelViewMtxTop] * gRSP.projectionMtxs[gRSP.projectionMtxTop];
2641 gRSP.bMatrixIsUpdated = false;
2642 gRSP.bCombinedMatrixIsUpdated = true;
2645 if( gRSP.bCombinedMatrixIsUpdated )
2647 if( options.enableHackForGames == HACK_REVERSE_XY_COOR )
2649 gRSPworldProject = gRSPworldProject * reverseXY;
2651 if( options.enableHackForGames == HACK_REVERSE_Y_COOR )
2653 gRSPworldProject = gRSPworldProject * reverseY;
2655 #if !defined(NO_ASM)
2656 if( status.isSSEEnabled )
2658 MatrixTranspose(&gRSPworldProjectTransported, &gRSPworldProject);
2661 gRSP.bCombinedMatrixIsUpdated = false;
2664 //if( gRSP.bWorldMatrixIsUpdated || gRSP.bLightIsUpdated )
2666 // // Update lights with transported world matrix
2667 // for( unsigned int l=0; l<gRSPnumLights; l++)
2669 // Vec3TransformCoord(&gRSPlights[l].td, &gRSPlights[l].od, &gRSPmodelViewTopTranspose);
2670 // Vec3Normalize(&gRSPlights[l].td,&gRSPlights[l].td);
2673 // gRSP.bWorldMatrixIsUpdated = false;
2674 // gRSP.bLightIsUpdated = false;