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;
640 ProcessVertexData = ProcessVertexDataNoSSE;
643 gRSPfFogMin = gRSPfFogMax = 0.0f;
644 windowSetting.fMultX = windowSetting.fMultY = 2.0f;
645 windowSetting.vpLeftW = windowSetting.vpTopW = 0;
646 windowSetting.vpRightW = windowSetting.vpWidthW = 640;
647 windowSetting.vpBottomW = windowSetting.vpHeightW = 480;
649 gRSP.nVPLeftN = gRSP.nVPTopN = 0;
650 gRSP.nVPRightN = 640;
651 gRSP.nVPBottomN = 640;
652 gRSP.nVPWidthN = 640;
653 gRSP.nVPHeightN = 640;
654 gRDP.scissor.left=gRDP.scissor.top=0;
655 gRDP.scissor.right=gRDP.scissor.bottom=640;
657 gRSP.bLightingEnable = gRSP.bTextureGen = false;
658 gRSP.curTile=gRSPnumLights=gRSP.ambientLightColor=gRSP.ambientLightIndex= 0;
659 gRSP.fAmbientLightR=gRSP.fAmbientLightG=gRSP.fAmbientLightB=0;
660 gRSP.projectionMtxTop = gRSP.modelViewMtxTop = 0;
661 gRDP.fogColor = gRDP.primitiveColor = gRDP.envColor = gRDP.primitiveDepth = gRDP.primLODMin = gRDP.primLODFrac = gRDP.LODFrac = 0;
662 gRDP.fPrimitiveDepth = 0;
663 gRSP.numVertices = 0;
664 gRSP.maxVertexID = 0;
665 gRSP.bCullFront=false;
667 gRSP.bFogEnabled=gRDP.bFogEnableInBlender=false;
668 gRSP.bZBufferEnabled=true;
669 gRSP.shadeMode=SHADE_SMOOTH;
670 gRDP.keyR=gRDP.keyG=gRDP.keyB=gRDP.keyA=gRDP.keyRGB=gRDP.keyRGBA = 0;
672 gRSP.DKRCMatrixIndex = gRSP.dwDKRVtxAddr = gRSP.dwDKRMatrixAddr = 0;
673 gRSP.DKRBillBoard = false;
675 gRSP.fTexScaleX = 1/32.0f;
676 gRSP.fTexScaleY = 1/32.0f;
677 gRSP.bTextureEnabled = FALSE;
679 gRSP.clip_ratio_left = 0;
680 gRSP.clip_ratio_top = 0;
681 gRSP.clip_ratio_right = 640;
682 gRSP.clip_ratio_bottom = 480;
683 gRSP.clip_ratio_negx = 1;
684 gRSP.clip_ratio_negy = 1;
685 gRSP.clip_ratio_posx = 1;
686 gRSP.clip_ratio_posy = 1;
687 gRSP.real_clip_scissor_left = 0;
688 gRSP.real_clip_scissor_top = 0;
689 gRSP.real_clip_scissor_right = 640;
690 gRSP.real_clip_scissor_bottom = 480;
691 windowSetting.clipping.left = 0;
692 windowSetting.clipping.top = 0;
693 windowSetting.clipping.right = 640;
694 windowSetting.clipping.bottom = 480;
695 windowSetting.clipping.width = 640;
696 windowSetting.clipping.height = 480;
697 windowSetting.clipping.needToClip = false;
698 gRSP.real_clip_ratio_negx = 1;
699 gRSP.real_clip_ratio_negy = 1;
700 gRSP.real_clip_ratio_posx = 1;
701 gRSP.real_clip_ratio_posy = 1;
703 gRSP.DKRCMatrixIndex=0;
705 gRSP.DKRBillBoard = false;
707 gRSP.dwDKRMatrixAddr=0;
710 gRDP.geometryMode = 0;
713 gRDP.fillColor = 0xFFFFFFFF;
714 gRDP.originalFillColor =0;
717 gRSP.vertexMult = 10;
718 gRSP.bNearClip = false;
719 gRSP.bRejectVtx = false;
721 gRDP.texturesAreReloaded = false;
722 gRDP.textureIsChanged = false;
723 gRDP.colorsAreReloaded = false;
725 memset(&gRDP.otherMode,0,sizeof(RDP_OtherMode));
726 memset(&gRDP.tiles,0,sizeof(Tile)*8);
728 for( int i=0; i<MAX_VERTS; i++ )
731 g_vtxNonTransformed[i].w = 1;
734 memset(gRSPn64lights, 0, sizeof(N64Light)*16);
737 void SetFogMinMax(float fMin, float fMax, float fMul, float fOffset)
747 gRSPfFogMin = max(0,fMin/500-1);
748 gRSPfFogMax = fMax/500-1;
751 gRSPfFogDivider = 255/(gRSPfFogMax-gRSPfFogMin);
752 CRender::g_pRender->SetFogMinMax(fMin, fMax);
755 void InitVertexColors()
759 void InitVertexTextureConstants()
764 RenderTexture &tex0 = g_textures[gRSP.curTile];
765 //CTexture *surf = tex0.m_pCTexture;
766 Tile &tile0 = gRDP.tiles[gRSP.curTile];
768 scaleX = gRSP.fTexScaleX;
769 scaleY = gRSP.fTexScaleY;
771 gRSP.tex0scaleX = scaleX * tile0.fShiftScaleS/tex0.m_fTexWidth;
772 gRSP.tex0scaleY = scaleY * tile0.fShiftScaleT/tex0.m_fTexHeight;
774 gRSP.tex0OffsetX = tile0.fhilite_sl/tex0.m_fTexWidth;
775 gRSP.tex0OffsetY = tile0.fhilite_tl/tex0.m_fTexHeight;
777 if( CRender::g_pRender->IsTexel1Enable() )
779 RenderTexture &tex1 = g_textures[(gRSP.curTile+1)&7];
780 //CTexture *surf = tex1.m_pCTexture;
781 Tile &tile1 = gRDP.tiles[(gRSP.curTile+1)&7];
783 gRSP.tex1scaleX = scaleX * tile1.fShiftScaleS/tex1.m_fTexWidth;
784 gRSP.tex1scaleY = scaleY * tile1.fShiftScaleT/tex1.m_fTexHeight;
786 gRSP.tex1OffsetX = tile1.fhilite_sl/tex1.m_fTexWidth;
787 gRSP.tex1OffsetY = tile1.fhilite_tl/tex1.m_fTexHeight;
790 gRSP.texGenXRatio = tile0.fShiftScaleS;
791 gRSP.texGenYRatio = gRSP.fTexScaleX/gRSP.fTexScaleY*tex0.m_fTexWidth/tex0.m_fTexHeight*tile0.fShiftScaleT;
794 void TexGen(float &s, float &t)
796 if (gRDP.geometryMode & G_TEXTURE_GEN_LINEAR)
798 s = acosf(g_normal.x) / 3.14159f;
799 t = acosf(g_normal.y) / 3.14159f;
803 s = 0.5f * ( 1.0f + g_normal.x);
804 t = 0.5f * ( 1.0f - g_normal.y);
808 void ComputeLOD(bool openGL)
810 TLITVERTEX &v0 = g_vtxBuffer[0];
811 TLITVERTEX &v1 = g_vtxBuffer[1];
812 RenderTexture &tex0 = g_textures[gRSP.curTile];
817 float x = g_vtxProjected5[0][0] / g_vtxProjected5[0][4] - g_vtxProjected5[1][0] / g_vtxProjected5[1][4];
818 float y = g_vtxProjected5[0][1] / g_vtxProjected5[0][4] - g_vtxProjected5[1][1] / g_vtxProjected5[1][4];
820 x = windowSetting.vpWidthW*x/windowSetting.fMultX/2;
821 y = windowSetting.vpHeightW*y/windowSetting.fMultY/2;
826 float x = (v0.x - v1.x)/ windowSetting.fMultX;
827 float y = (v0.y - v1.y)/ windowSetting.fMultY;
831 float s0 = v0.tcord[0].u * tex0.m_fTexWidth;
832 float t0 = v0.tcord[0].v * tex0.m_fTexHeight;
833 float s1 = v1.tcord[0].u * tex0.m_fTexWidth;
834 float t1 = v1.tcord[0].v * tex0.m_fTexHeight;
836 dt = sqrtf((s0-s1)*(s0-s1)+(t0-t1)*(t0-t1));
839 float frac = log10f(lod)/log10f(2.0f);
840 //DEBUGGER_IF_DUMP(pauseAtNext,{DebuggerAppendMsg("LOD frac = %f", frac);});
841 frac = (lod / powf(2.0f,floorf(frac)));
842 frac = frac - floorf(frac);
843 //DEBUGGER_IF_DUMP(pauseAtNext,{DebuggerAppendMsg("LOD = %f, frac = %f", lod, frac);});
844 gRDP.LODFrac = (uint32)(frac*255);
845 CRender::g_pRender->SetCombinerAndBlender();
848 bool bHalfTxtScale=false;
849 extern uint32 lastSetTile;
851 void InitVertex(uint32 dwV, uint32 vtxIndex, bool bTexture, bool openGL)
853 VTX_DUMP(TRACE2("Init vertex (%d) to vtx buf[%d]:", dwV, vtxIndex));
855 TLITVERTEX &v = g_vtxBuffer[vtxIndex];
856 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));
859 g_vtxProjected5[vtxIndex][0] = g_vtxTransformed[dwV].x;
860 g_vtxProjected5[vtxIndex][1] = g_vtxTransformed[dwV].y;
861 g_vtxProjected5[vtxIndex][2] = g_vtxTransformed[dwV].z;
862 g_vtxProjected5[vtxIndex][3] = g_vtxTransformed[dwV].w;
863 g_vtxProjected5[vtxIndex][4] = g_vecProjected[dwV].z;
865 if( g_vtxTransformed[dwV].w < 0 )
866 g_vtxProjected5[vtxIndex][4] = 0;
868 g_vtxIndex[vtxIndex] = vtxIndex;
871 if( !openGL || options.bOGLVertexClipper == TRUE )
873 v.x = g_vecProjected[dwV].x*gRSP.vtxXMul+gRSP.vtxXAdd;
874 v.y = g_vecProjected[dwV].y*gRSP.vtxYMul+gRSP.vtxYAdd;
875 v.z = (g_vecProjected[dwV].z + 1.0f) * 0.5f; // DirectX minZ=0, maxZ=1
876 //v.z = g_vecProjected[dwV].z; // DirectX minZ=0, maxZ=1
877 v.rhw = g_vecProjected[dwV].w;
878 VTX_DUMP(TRACE4(" Proj : x=%f, y=%f, z=%f, rhw=%f", v.x,v.y,v.z,v.rhw));
880 if( gRSP.bProcessSpecularColor )
882 v.dcSpecular = CRender::g_pRender->PostProcessSpecularColor();
883 if( gRSP.bFogEnabled )
885 v.dcSpecular &= 0x00FFFFFF;
886 uint32 fogFct = 0xFF-(uint8)((g_fFogCoord[dwV]-gRSPfFogMin)*gRSPfFogDivider);
887 v.dcSpecular |= (fogFct<<24);
890 else if( gRSP.bFogEnabled )
892 uint32 fogFct = 0xFF-(uint8)((g_fFogCoord[dwV]-gRSPfFogMin)*gRSPfFogDivider);
893 v.dcSpecular = (fogFct<<24);
896 VTX_DUMP(TRACE2(" (U,V): %f, %f", g_fVtxTxtCoords[dwV].x,g_fVtxTxtCoords[dwV].y));
898 v.dcDiffuse = g_dwVtxDifColor[dwV];
899 if( gRDP.otherMode.key_en )
901 v.dcDiffuse &= 0x00FFFFFF;
902 v.dcDiffuse |= (gRDP.keyA<<24);
904 else if( gRDP.otherMode.aa_en && gRDP.otherMode.clr_on_cvg==0 )
906 v.dcDiffuse |= 0xFF000000;
909 if( gRSP.bProcessDiffuseColor )
911 v.dcDiffuse = CRender::g_pRender->PostProcessDiffuseColor(v.dcDiffuse);
913 if( options.bWinFrameMode )
915 v.dcDiffuse = g_dwVtxDifColor[dwV];
920 g_oglVtxColors[vtxIndex][0] = v.r;
921 g_oglVtxColors[vtxIndex][1] = v.g;
922 g_oglVtxColors[vtxIndex][2] = v.b;
923 g_oglVtxColors[vtxIndex][3] = v.a;
928 // If the vert is already lit, then there is no normal (and hence we can't generate tex coord)
929 // Only scale if not generated automatically
930 if (gRSP.bTextureGen && gRSP.bLightingEnable)
932 // Correction for texGen result
934 RenderTexture &tex0 = g_textures[gRSP.curTile];
935 u0 = g_fVtxTxtCoords[dwV].x * 32 * 1024 * gRSP.fTexScaleX / tex0.m_fTexWidth;
936 v0 = g_fVtxTxtCoords[dwV].y * 32 * 1024 * gRSP.fTexScaleY / tex0.m_fTexHeight;
937 u0 *= (gRDP.tiles[gRSP.curTile].fShiftScaleS);
938 v0 *= (gRDP.tiles[gRSP.curTile].fShiftScaleT);
940 if( CRender::g_pRender->IsTexel1Enable() )
942 RenderTexture &tex1 = g_textures[(gRSP.curTile+1)&7];
943 u1 = g_fVtxTxtCoords[dwV].x * 32 * 1024 * gRSP.fTexScaleX / tex1.m_fTexWidth;
944 v1 = g_fVtxTxtCoords[dwV].y * 32 * 1024 * gRSP.fTexScaleY / tex1.m_fTexHeight;
945 u1 *= gRDP.tiles[(gRSP.curTile+1)&7].fShiftScaleS;
946 v1 *= gRDP.tiles[(gRSP.curTile+1)&7].fShiftScaleT;
947 CRender::g_pRender->SetVertexTextureUVCoord(v, u0, v0, u1, v1);
951 CRender::g_pRender->SetVertexTextureUVCoord(v, u0, v0);
956 float tex0u = g_fVtxTxtCoords[dwV].x *gRSP.tex0scaleX - gRSP.tex0OffsetX ;
957 float tex0v = g_fVtxTxtCoords[dwV].y *gRSP.tex0scaleY - gRSP.tex0OffsetY ;
959 if( CRender::g_pRender->IsTexel1Enable() )
961 float tex1u = g_fVtxTxtCoords[dwV].x *gRSP.tex1scaleX - gRSP.tex1OffsetX ;
962 float tex1v = g_fVtxTxtCoords[dwV].y *gRSP.tex1scaleY - gRSP.tex1OffsetY ;
964 CRender::g_pRender->SetVertexTextureUVCoord(v, tex0u, tex0v, tex1u, tex1v);
965 VTX_DUMP(TRACE2(" (tex0): %f, %f", tex0u,tex0v));
966 VTX_DUMP(TRACE2(" (tex1): %f, %f", tex1u,tex1v));
970 CRender::g_pRender->SetVertexTextureUVCoord(v, tex0u, tex0v);
971 VTX_DUMP(TRACE2(" (tex0): %f, %f", tex0u,tex0v));
975 // Check for txt scale hack
976 if( !bHalfTxtScale && g_curRomInfo.bTextureScaleHack &&
977 (gRDP.tiles[lastSetTile].dwSize == TXT_SIZE_32b || gRDP.tiles[lastSetTile].dwSize == TXT_SIZE_4b ) )
979 int width = ((gRDP.tiles[lastSetTile].sh-gRDP.tiles[lastSetTile].sl+1)<<1);
980 int height = ((gRDP.tiles[lastSetTile].th-gRDP.tiles[lastSetTile].tl+1)<<1);
981 if( g_fVtxTxtCoords[dwV].x*gRSP.fTexScaleX == width || g_fVtxTxtCoords[dwV].y*gRSP.fTexScaleY == height )
988 if( g_curRomInfo.bEnableTxtLOD && vtxIndex == 1 && gRDP.otherMode.text_lod )
990 if( CRender::g_pRender->IsTexel1Enable() && CRender::g_pRender->m_pColorCombiner->m_pDecodedMux->isUsed(MUX_LODFRAC) )
1000 VTX_DUMP(TRACE2(" DIF(%08X), SPE(%08X)", v.dcDiffuse, v.dcSpecular));
1001 VTX_DUMP(TRACE0(""));
1004 uint32 LightVert(XVECTOR4 & norm, int vidx)
1009 register float r = gRSP.fAmbientLightR;
1010 register float g = gRSP.fAmbientLightG;
1011 register float b = gRSP.fAmbientLightB;
1013 if( options.enableHackForGames != HACK_FOR_ZELDA_MM )
1015 for (register unsigned int l=0; l < gRSPnumLights; l++)
1017 fCosT = norm.x*gRSPlights[l].x + norm.y*gRSPlights[l].y + norm.z*gRSPlights[l].z;
1021 r += gRSPlights[l].fr * fCosT;
1022 g += gRSPlights[l].fg * fCosT;
1023 b += gRSPlights[l].fb * fCosT;
1030 bool transformed = false;
1032 for (register unsigned int l=0; l < gRSPnumLights; l++)
1034 if( gRSPlights[l].range == 0 )
1036 // Regular directional light
1037 fCosT = norm.x*gRSPlights[l].x + norm.y*gRSPlights[l].y + norm.z*gRSPlights[l].z;
1041 r += gRSPlights[l].fr * fCosT;
1042 g += gRSPlights[l].fg * fCosT;
1043 b += gRSPlights[l].fb * fCosT;
1046 else //if( (gRSPlights[l].col&0x00FFFFFF) != 0x00FFFFFF )
1051 Vec3Transform(&v, (XVECTOR3*)&g_vtxNonTransformed[vidx], &gRSPmodelViewTop); // Convert to w=1
1055 XVECTOR3 dir(gRSPlights[l].x - v.x, gRSPlights[l].y - v.y, gRSPlights[l].z - v.z);
1056 //XVECTOR3 dir(v.x-gRSPlights[l].x, v.y-gRSPlights[l].y, v.z-gRSPlights[l].z);
1057 float d2 = sqrtf(dir.x*dir.x+dir.y*dir.y+dir.z*dir.z);
1062 fCosT = norm.x*dir.x + norm.y*dir.y + norm.z*dir.z;
1066 //float f = d2/gRSPlights[l].range*50;
1067 float f = d2/15000*50;
1071 r += gRSPlights[l].fr * fCosT;
1072 g += gRSPlights[l].fg * fCosT;
1073 b += gRSPlights[l].fb * fCosT;
1079 if (r > 255) r = 255;
1080 if (g > 255) g = 255;
1081 if (b > 255) b = 255;
1082 return ((0xff000000)|(((uint32)r)<<16)|(((uint32)g)<<8)|((uint32)b));
1085 uint32 LightVertNew(XVECTOR4 & norm)
1090 register float r = gRSP.fAmbientLightR;
1091 register float g = gRSP.fAmbientLightG;
1092 register float b = gRSP.fAmbientLightB;
1095 for (register unsigned int l=0; l < gRSPnumLights; l++)
1097 fCosT = norm.x*gRSPlights[l].tx + norm.y*gRSPlights[l].ty + norm.z*gRSPlights[l].tz;
1101 r += gRSPlights[l].fr * fCosT;
1102 g += gRSPlights[l].fg * fCosT;
1103 b += gRSPlights[l].fb * fCosT;
1107 if (r > 255) r = 255;
1108 if (g > 255) g = 255;
1109 if (b > 255) b = 255;
1110 return ((0xff000000)|(((uint32)r)<<16)|(((uint32)g)<<8)|((uint32)b));
1118 #if !defined(__GNUC__) && !defined(NO_ASM)
1119 __declspec( naked ) uint32 __fastcall SSELightVert()
1123 movaps xmm3, DWORD PTR gRSP; // loading Ambient colors, xmm3 is the result color
1124 movaps xmm4, DWORD PTR [g_normal]; // xmm4 is the normal
1128 cmp ecx, DWORD PTR gRSPnumLights;
1132 movups xmm5, DWORD PTR gRSPlights[eax]; // Light Dir
1133 movups xmm1, DWORD PTR gRSPlights[0x14][eax]; // Light color
1134 mulps xmm5, xmm4; // Lightdir * normals
1138 shufps xmm5,xmm0,0x01;
1144 shufps xmm0,xmm0,0; // fcosT
1152 movss xmm0,DWORD PTR real255;
1156 // Without using a memory
1157 cvtss2si eax,xmm0; // move the 1st uint32 to eax
1160 shufps xmm0,xmm0,0E5h; // move the 2nd uint32 to the 1st uint32
1161 cvtss2si ecx,xmm0; // move the 1st uint32 to ecx
1164 shufps xmm0,xmm0,0E6h; // Move the 3rd uint32 to the 1st uint32
1171 #elif defined(__GNUC__) && defined(__x86_64__) && !defined(NO_ASM)
1172 uint32 SSELightVert(void)
1175 float f255 = 255.0, fZero = 0.0;
1177 asm volatile(" movaps %1, %%xmm3 \n" // xmm3 == gRSP.fAmbientLight{RGBA}
1178 " movaps %2, %%xmm4 \n" // xmm4 == g_normal.{xyz}
1179 " xor %%rcx, %%rcx \n"
1181 " cmpl %3, %%ecx \n"
1183 " mov %%rcx, %%rax \n"
1184 " imul $0x44, %%rax, %%rax \n"
1185 " movups (%4,%%rax,), %%xmm5 \n" // xmm5 == gRSPlights[l].{xyzr}
1186 " movups 20(%4,%%rax,), %%xmm1 \n" // xmm1 == gRSPlights[l].{frfgfbfa}
1187 " mulps %%xmm4, %%xmm5 \n"
1188 " movhlps %%xmm5, %%xmm0 \n"
1189 " addps %%xmm5, %%xmm0 \n"
1190 " shufps $0x01, %%xmm0, %%xmm5 \n"
1191 " addps %%xmm5, %%xmm0 \n"
1192 " comiss %6, %%xmm0 \n"
1194 " shufps $0x00, %%xmm0, %%xmm0 \n"
1195 " mulps %%xmm0, %%xmm1 \n"
1196 " addps %%xmm1, %%xmm3 \n"
1201 " movss %5, %%xmm0 \n"
1202 " shufps $0x00, %%xmm0, %%xmm0 \n"
1203 " minps %%xmm3, %%xmm0 \n"
1204 " cvtss2si %%xmm0, %%eax \n"
1205 " shll $0x10, %%eax \n"
1206 " orl $0xff000000, %%eax \n"
1207 " shufps $0xe5, %%xmm0, %%xmm0 \n"
1208 " cvtss2si %%xmm0, %%ecx \n"
1209 " shll $8, %%ecx \n"
1210 " orl %%ecx, %%eax \n"
1211 " shufps $0xe6, %%xmm0, %%xmm0 \n"
1212 " cvtss2si %%xmm0, %%ecx \n"
1213 " orl %%ecx, %%eax \n"
1215 : "m"(gRSP), "m"(g_normal), "m"(gRSPnumLights), "r"(gRSPlights), "m"(f255), "m"(fZero)
1216 : "%rcx", "memory", "cc", "%xmm0", "%xmm1", "%xmm3", "%xmm4", "%xmm5"
1220 #elif !defined(NO_ASM) // 32-bit GCC assumed
1221 uint32 SSELightVert(void)
1224 float f255 = 255.0, fZero = 0.0;
1226 asm volatile(" movaps %1, %%xmm3 \n"
1227 " movaps %2, %%xmm4 \n"
1228 " xor %%ecx, %%ecx \n"
1230 " cmpl %3, %%ecx \n"
1232 " mov %%ecx, %%eax \n"
1233 " imul $0x44, %%eax, %%eax \n"
1234 " movups (%4,%%eax,), %%xmm5 \n"
1235 " movups 20(%4,%%eax,), %%xmm1 \n"
1236 " mulps %%xmm4, %%xmm5 \n"
1237 " movhlps %%xmm5, %%xmm0 \n"
1238 " addps %%xmm5, %%xmm0 \n"
1239 " shufps $0x01, %%xmm0, %%xmm5 \n"
1240 " addps %%xmm5, %%xmm0 \n"
1241 " comiss %6, %%xmm0 \n"
1243 " shufps $0x00, %%xmm0, %%xmm0 \n"
1244 " mulps %%xmm0, %%xmm1 \n"
1245 " addps %%xmm1, %%xmm3 \n"
1250 " movss %5, %%xmm0 \n"
1251 " shufps $0x00, %%xmm0, %%xmm0 \n"
1252 " minps %%xmm3, %%xmm0 \n"
1253 " cvtss2si %%xmm0, %%eax \n"
1254 " shll $0x10, %%eax \n"
1255 " orl $0xff000000, %%eax \n"
1256 " shufps $0xe5, %%xmm0, %%xmm0 \n"
1257 " cvtss2si %%xmm0, %%ecx \n"
1258 " shll $8, %%ecx \n"
1259 " orl %%ecx, %%eax \n"
1260 " shufps $0xe6, %%xmm0, %%xmm0 \n"
1261 " cvtss2si %%xmm0, %%ecx \n"
1262 " orl %%ecx, %%eax \n"
1264 : "m"(gRSP), "m"(g_normal), "m"(gRSPnumLights), "r"(gRSPlights), "m"(f255), "m"(fZero)
1265 : "%rcx", "memory", "cc", "%xmm0", "%xmm1", "%xmm3", "%xmm4", "%xmm5"
1271 inline void ReplaceAlphaWithFogFactor(int i)
1273 if( gRDP.geometryMode & G_FOG )
1275 // Use fog factor to replace vertex alpha
1276 if( g_vecProjected[i].z > 1 )
1277 *(((uint8*)&(g_dwVtxDifColor[i]))+3) = 0xFF;
1278 if( g_vecProjected[i].z < 0 )
1279 *(((uint8*)&(g_dwVtxDifColor[i]))+3) = 0;
1281 *(((uint8*)&(g_dwVtxDifColor[i]))+3) = (uint8)(g_vecProjected[i].z*255);
1296 // Assumes dwAddr has already been checked!
1297 // Don't inline - it's too big with the transform macros
1299 #if !defined(NO_ASM)
1300 void ProcessVertexDataSSE(uint32 dwAddr, uint32 dwV0, uint32 dwNum)
1302 UpdateCombinedMatrix();
1304 // This function is called upon SPvertex
1305 // - do vertex matrix transform
1306 // - do vertex lighting
1307 // - do texture cooridinate transform if needed
1308 // - calculate normal vector
1310 // Output: - g_vecProjected[i] -> transformed vertex x,y,z
1311 // - g_vecProjected[i].w -> saved vertex 1/w
1312 // - g_dwVtxFlags[i] -> flags
1313 // - g_dwVtxDifColor[i] -> vertex color
1314 // - g_fVtxTxtCoords[i] -> vertex texture cooridinates
1316 FiddledVtx * pVtxBase = (FiddledVtx*)(g_pRDRAMu8 + dwAddr);
1317 g_pVtxBase = pVtxBase;
1319 for (uint32 i = dwV0; i < dwV0 + dwNum; i++)
1321 SP_Timing(RSP_GBI0_Vtx);
1323 FiddledVtx & vert = pVtxBase[i - dwV0];
1325 g_vtxNonTransformed[i].x = (float)vert.x;
1326 g_vtxNonTransformed[i].y = (float)vert.y;
1327 g_vtxNonTransformed[i].z = (float)vert.z;
1329 SSEVec3Transform(i);
1331 if( gRSP.bFogEnabled )
1333 g_fFogCoord[i] = g_vecProjected[i].z;
1334 if( g_vecProjected[i].w < 0 || g_vecProjected[i].z < 0 || g_fFogCoord[i] < gRSPfFogMin )
1335 g_fFogCoord[i] = gRSPfFogMin;
1338 ReplaceAlphaWithFogFactor(i);
1343 uint32 *dat = (uint32*)(&vert);
1344 DebuggerAppendMsg("vtx %d: %08X %08X %08X %08X", i, dat[0],dat[1],dat[2],dat[3]);
1345 DebuggerAppendMsg(" : %f, %f, %f, %f",
1346 g_vtxTransformed[i].x,g_vtxTransformed[i].y,g_vtxTransformed[i].z,g_vtxTransformed[i].w);
1347 DebuggerAppendMsg(" : %f, %f, %f, %f",
1348 g_vecProjected[i].x,g_vecProjected[i].y,g_vecProjected[i].z,g_vecProjected[i].w);
1351 RSP_Vtx_Clipping(i);
1353 if( gRSP.bLightingEnable )
1355 g_normal.x = (float)vert.norma.nx;
1356 g_normal.y = (float)vert.norma.ny;
1357 g_normal.z = (float)vert.norma.nz;
1359 SSEVec3TransformNormal();
1360 if( options.enableHackForGames != HACK_FOR_ZELDA_MM )
1361 g_dwVtxDifColor[i] = SSELightVert();
1363 g_dwVtxDifColor[i] = LightVert(g_normal, i);
1364 *(((uint8*)&(g_dwVtxDifColor[i]))+3) = vert.rgba.a; // still use alpha from the vertex
1368 if( (gRDP.geometryMode & G_SHADE) == 0 && gRSP.ucode < 5 ) //Shade is disabled
1371 g_dwVtxDifColor[i] = gRDP.primitiveColor;
1375 register IColor &color = *(IColor*)&g_dwVtxDifColor[i];
1376 color.b = vert.rgba.r;
1377 color.g = vert.rgba.g;
1378 color.r = vert.rgba.b;
1379 color.a = vert.rgba.a;
1383 if( options.bWinFrameMode )
1385 g_dwVtxDifColor[i] = COLOR_RGBA(vert.rgba.r, vert.rgba.g, vert.rgba.b, vert.rgba.a);
1388 // Update texture coords n.b. need to divide tu/tv by bogus scale on addition to buffer
1390 // If the vert is already lit, then there is no normal (and hence we
1391 // can't generate tex coord)
1392 if (gRSP.bTextureGen && gRSP.bLightingEnable )
1394 TexGen(g_fVtxTxtCoords[i].x, g_fVtxTxtCoords[i].y);
1398 g_fVtxTxtCoords[i].x = (float)vert.tu;
1399 g_fVtxTxtCoords[i].y = (float)vert.tv;
1403 VTX_DUMP(TRACE2("Setting Vertexes: %d - %d\n", dwV0, dwV0+dwNum-1));
1404 DEBUGGER_PAUSE_AND_DUMP(NEXT_VERTEX_CMD,{TRACE0("Paused at Vertex Cmd");});
1408 void ProcessVertexDataNoSSE(uint32 dwAddr, uint32 dwV0, uint32 dwNum)
1411 UpdateCombinedMatrix();
1413 // This function is called upon SPvertex
1414 // - do vertex matrix transform
1415 // - do vertex lighting
1416 // - do texture cooridinate transform if needed
1417 // - calculate normal vector
1419 // Output: - g_vecProjected[i] -> transformed vertex x,y,z
1420 // - g_vecProjected[i].w -> saved vertex 1/w
1421 // - g_dwVtxFlags[i] -> flags
1422 // - g_dwVtxDifColor[i] -> vertex color
1423 // - g_fVtxTxtCoords[i] -> vertex texture cooridinates
1425 FiddledVtx * pVtxBase = (FiddledVtx*)(g_pRDRAMu8 + dwAddr);
1426 g_pVtxBase = pVtxBase;
1428 for (uint32 i = dwV0; i < dwV0 + dwNum; i++)
1430 SP_Timing(RSP_GBI0_Vtx);
1432 FiddledVtx & vert = pVtxBase[i - dwV0];
1434 g_vtxNonTransformed[i].x = (float)vert.x;
1435 g_vtxNonTransformed[i].y = (float)vert.y;
1436 g_vtxNonTransformed[i].z = (float)vert.z;
1438 Vec3Transform(&g_vtxTransformed[i], (XVECTOR3*)&g_vtxNonTransformed[i], &gRSPworldProject); // Convert to w=1
1440 g_vecProjected[i].w = 1.0f / g_vtxTransformed[i].w;
1441 g_vecProjected[i].x = g_vtxTransformed[i].x * g_vecProjected[i].w;
1442 g_vecProjected[i].y = g_vtxTransformed[i].y * g_vecProjected[i].w;
1443 if ((g_curRomInfo.bPrimaryDepthHack || options.enableHackForGames == HACK_FOR_NASCAR ) && gRDP.otherMode.depth_source )
1445 g_vecProjected[i].z = gRDP.fPrimitiveDepth;
1446 g_vtxTransformed[i].z = gRDP.fPrimitiveDepth*g_vtxTransformed[i].w;
1450 g_vecProjected[i].z = g_vtxTransformed[i].z * g_vecProjected[i].w;
1453 if( gRSP.bFogEnabled )
1455 g_fFogCoord[i] = g_vecProjected[i].z;
1456 if( g_vecProjected[i].w < 0 || g_vecProjected[i].z < 0 || g_fFogCoord[i] < gRSPfFogMin )
1457 g_fFogCoord[i] = gRSPfFogMin;
1462 uint32 *dat = (uint32*)(&vert);
1463 DebuggerAppendMsg("vtx %d: %08X %08X %08X %08X", i, dat[0],dat[1],dat[2],dat[3]);
1464 DebuggerAppendMsg(" : %f, %f, %f, %f",
1465 g_vtxTransformed[i].x,g_vtxTransformed[i].y,g_vtxTransformed[i].z,g_vtxTransformed[i].w);
1466 DebuggerAppendMsg(" : %f, %f, %f, %f",
1467 g_vecProjected[i].x,g_vecProjected[i].y,g_vecProjected[i].z,g_vecProjected[i].w);
1470 RSP_Vtx_Clipping(i);
1472 if( gRSP.bLightingEnable )
1474 g_normal.x = (float)vert.norma.nx;
1475 g_normal.y = (float)vert.norma.ny;
1476 g_normal.z = (float)vert.norma.nz;
1478 Vec3TransformNormal(g_normal, gRSPmodelViewTop);
1479 g_dwVtxDifColor[i] = LightVert(g_normal, i);
1480 *(((uint8*)&(g_dwVtxDifColor[i]))+3) = vert.rgba.a; // still use alpha from the vertex
1484 if( (gRDP.geometryMode & G_SHADE) == 0 && gRSP.ucode < 5 ) //Shade is disabled
1487 g_dwVtxDifColor[i] = gRDP.primitiveColor;
1491 register IColor &color = *(IColor*)&g_dwVtxDifColor[i];
1492 color.b = vert.rgba.r;
1493 color.g = vert.rgba.g;
1494 color.r = vert.rgba.b;
1495 color.a = vert.rgba.a;
1499 if( options.bWinFrameMode )
1501 g_dwVtxDifColor[i] = COLOR_RGBA(vert.rgba.r, vert.rgba.g, vert.rgba.b, vert.rgba.a);
1504 ReplaceAlphaWithFogFactor(i);
1506 // Update texture coords n.b. need to divide tu/tv by bogus scale on addition to buffer
1508 // If the vert is already lit, then there is no normal (and hence we
1509 // can't generate tex coord)
1510 if (gRSP.bTextureGen && gRSP.bLightingEnable )
1512 TexGen(g_fVtxTxtCoords[i].x, g_fVtxTxtCoords[i].y);
1516 g_fVtxTxtCoords[i].x = (float)vert.tu;
1517 g_fVtxTxtCoords[i].y = (float)vert.tv;
1521 VTX_DUMP(TRACE2("Setting Vertexes: %d - %d\n", dwV0, dwV0+dwNum-1));
1522 DEBUGGER_PAUSE_AND_DUMP(NEXT_VERTEX_CMD,{TRACE0("Paused at Vertex Cmd");});
1525 bool PrepareTriangle(uint32 dwV0, uint32 dwV1, uint32 dwV2)
1527 if( status.isVertexShaderEnabled || status.bUseHW_T_L )
1529 g_vtxIndex[gRSP.numVertices++] = dwV0;
1530 g_vtxIndex[gRSP.numVertices++] = dwV1;
1531 g_vtxIndex[gRSP.numVertices++] = dwV2;
1532 status.dwNumTrisRendered++;
1533 gRSP.maxVertexID = max(gRSP.maxVertexID,max(dwV0,max(dwV1,dwV2)));
1537 SP_Timing(SP_Each_Triangle);
1539 bool textureFlag = (CRender::g_pRender->IsTextureEnabled() || gRSP.ucode == 6 );
1540 bool openGL = CDeviceBuilder::m_deviceGeneralType == OGL_DEVICE;
1542 InitVertex(dwV0, gRSP.numVertices, textureFlag, openGL);
1543 InitVertex(dwV1, gRSP.numVertices+1, textureFlag, openGL);
1544 InitVertex(dwV2, gRSP.numVertices+2, textureFlag, openGL);
1546 gRSP.numVertices += 3;
1547 status.dwNumTrisRendered++;
1555 // Returns TRUE if it thinks the triangle is visible
1556 // Returns FALSE if it is clipped
1557 bool IsTriangleVisible(uint32 dwV0, uint32 dwV1, uint32 dwV2)
1559 //return true; //fix me
1561 if( status.isVertexShaderEnabled || status.bUseHW_T_L ) return true; // We won't have access to transformed vertex data
1563 DEBUGGER_ONLY_IF( (!debuggerEnableTestTris || !debuggerEnableCullFace), {return TRUE;});
1566 // Check vertices are valid!
1567 if (dwV0 >= MAX_VERTS || dwV1 >= MAX_VERTS || dwV2 >= MAX_VERTS)
1571 // Here we AND all the flags. If any of the bits is set for all
1572 // 3 vertices, it means that all three x, y or z lie outside of
1573 // the current viewing volume.
1574 // Currently disabled - still seems a bit dodgy
1575 if ((gRSP.bCullFront || gRSP.bCullBack) && gRDP.otherMode.zmode != 3)
1577 XVECTOR4 & v0 = g_vecProjected[dwV0];
1578 XVECTOR4 & v1 = g_vecProjected[dwV1];
1579 XVECTOR4 & v2 = g_vecProjected[dwV2];
1581 // Only try to clip if the tri is onscreen. For some reason, this
1582 // method doesnt' work well when the z value is outside of screenspace
1583 //if (v0.z < 1 && v1.z < 1 && v2.z < 1)
1585 float V1 = v2.x - v0.x;
1586 float V2 = v2.y - v0.y;
1588 float W1 = v2.x - v1.x;
1589 float W2 = v2.y - v1.y;
1591 float fDirection = (V1 * W2) - (V2 * W1);
1592 fDirection = fDirection * v1.w * v2.w * v0.w;
1593 //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;
1595 if (fDirection < 0 && gRSP.bCullBack)
1597 status.dwNumTrisClipped++;
1600 else if (fDirection > 0 && gRSP.bCullFront)
1602 status.dwNumTrisClipped++;
1608 #ifdef ENABLE_CLIP_TRI
1609 //if( gRSP.bRejectVtx && (g_clipFlag[dwV0]|g_clipFlag[dwV1]|g_clipFlag[dwV2]) )
1611 if( g_clipFlag2[dwV0]&g_clipFlag2[dwV1]&g_clipFlag2[dwV2] )
1613 //DebuggerAppendMsg("Clipped");
1622 void SetPrimitiveColor(uint32 dwCol, uint32 LODMin, uint32 LODFrac)
1624 gRDP.colorsAreReloaded = true;
1625 gRDP.primitiveColor = dwCol;
1626 gRDP.primLODMin = LODMin;
1627 gRDP.primLODFrac = LODFrac;
1628 if( gRDP.primLODFrac < gRDP.primLODMin )
1630 gRDP.primLODFrac = gRDP.primLODMin;
1633 gRDP.fvPrimitiveColor[0] = ((dwCol>>16)&0xFF)/255.0f; //r
1634 gRDP.fvPrimitiveColor[1] = ((dwCol>>8)&0xFF)/255.0f; //g
1635 gRDP.fvPrimitiveColor[2] = ((dwCol)&0xFF)/255.0f; //b
1636 gRDP.fvPrimitiveColor[3] = ((dwCol>>24)&0xFF)/255.0f; //a
1639 void SetPrimitiveDepth(uint32 z, uint32 dwDZ)
1641 gRDP.primitiveDepth = z & 0x7FFF;
1642 gRDP.fPrimitiveDepth = (float)(gRDP.primitiveDepth)/(float)0x8000;
1644 //gRDP.fPrimitiveDepth = gRDP.fPrimitiveDepth*2-1;
1646 z=0xFFFF -> 1 the farest
1647 z=0 -> -1 the nearest
1653 if( (pauseAtNext && (eventToPause == NEXT_VERTEX_CMD || eventToPause == NEXT_FLUSH_TRI )) )//&& logTriangles )
1655 DebuggerAppendMsg("Set prim Depth: %f, (%08X, %08X)", gRDP.fPrimitiveDepth, z, dwDZ);
1660 void SetVertexXYZ(uint32 vertex, float x, float y, float z)
1662 g_vecProjected[vertex].x = x;
1663 g_vecProjected[vertex].y = y;
1664 g_vecProjected[vertex].z = z;
1666 g_vtxTransformed[vertex].x = x*g_vtxTransformed[vertex].w;
1667 g_vtxTransformed[vertex].y = y*g_vtxTransformed[vertex].w;
1668 g_vtxTransformed[vertex].z = z*g_vtxTransformed[vertex].w;
1671 void ModifyVertexInfo(uint32 where, uint32 vertex, uint32 val)
1675 case RSP_MV_WORD_OFFSET_POINT_RGBA: // Modify RGBA
1677 uint32 r = (val>>24)&0xFF;
1678 uint32 g = (val>>16)&0xFF;
1679 uint32 b = (val>>8)&0xFF;
1680 uint32 a = val&0xFF;
1681 g_dwVtxDifColor[vertex] = COLOR_RGBA(r, g, b, a);
1682 LOG_UCODE("Modify vert %d color, 0x%08x", vertex, g_dwVtxDifColor[vertex]);
1685 case RSP_MV_WORD_OFFSET_POINT_XYSCREEN: // Modify X,Y
1687 uint16 nX = (uint16)(val>>16);
1688 short x = *((short*)&nX);
1691 uint16 nY = (uint16)(val&0xFFFF);
1692 short y = *((short*)&nY);
1695 // Should do viewport transform.
1698 x -= windowSetting.uViWidth/2;
1699 y = windowSetting.uViHeight/2-y;
1701 if( options.bEnableHacks && ((*g_GraphicsInfo.VI_X_SCALE_REG)&0xF) != 0 )
1704 // I don't know why Tarzan is different
1705 SetVertexXYZ(vertex, x/windowSetting.fViWidth, y/windowSetting.fViHeight, g_vecProjected[vertex].z);
1709 // Toy Story 2 and other games
1710 SetVertexXYZ(vertex, x*2/windowSetting.fViWidth, y*2/windowSetting.fViHeight, g_vecProjected[vertex].z);
1713 LOG_UCODE("Modify vert %d: x=%d, y=%d", vertex, x, y);
1714 VTX_DUMP(TRACE3("Modify vert %d: (%d,%d)", vertex, x, y));
1717 case RSP_MV_WORD_OFFSET_POINT_ZSCREEN: // Modify C
1721 SetVertexXYZ(vertex, g_vecProjected[vertex].x, g_vecProjected[vertex].y, (((float)z/0x03FF)+0.5f)/2.0f );
1722 LOG_UCODE("Modify vert %d: z=%d", vertex, z);
1723 VTX_DUMP(TRACE2("Modify vert %d: z=%d", vertex, z));
1726 case RSP_MV_WORD_OFFSET_POINT_ST: // Texture
1728 short tu = short(val>>16);
1729 short tv = short(val & 0xFFFF);
1730 float ftu = tu / 32.0f;
1731 float ftv = tv / 32.0f;
1732 LOG_UCODE(" Setting vertex %d tu/tv to %f, %f", vertex, (float)tu, (float)tv);
1733 CRender::g_pRender->SetVtxTextureCoord(vertex, ftu/gRSP.fTexScaleX, ftv/gRSP.fTexScaleY);
1737 DEBUGGER_PAUSE_AND_DUMP(NEXT_VERTEX_CMD,{TRACE0("Paused at ModVertex Cmd");});
1740 void ProcessVertexDataDKR(uint32 dwAddr, uint32 dwV0, uint32 dwNum)
1742 UpdateCombinedMatrix();
1744 long long pVtxBase = (long long) (g_pRDRAMu8 + dwAddr);
1745 g_pVtxBase = (FiddledVtx*)pVtxBase;
1747 Matrix &matWorldProject = gRSP.DKRMatrixes[gRSP.DKRCMatrixIndex];
1752 if ((!gRSP.DKRBillBoard) || (gRSP.DKRCMatrixIndex != 2) )
1757 if( addbase && gRSP.DKRVtxCount == 0 && dwNum > 1 )
1762 LOG_UCODE(" ProcessVertexDataDKR, CMatrix = %d, Add base=%s", gRSP.DKRCMatrixIndex, gRSP.DKRBillBoard?"true":"false");
1763 VTX_DUMP(TRACE2("DKR Setting Vertexes\nCMatrix = %d, Add base=%s", gRSP.DKRCMatrixIndex, gRSP.DKRBillBoard?"true":"false"));
1766 uint32 end = dwV0 + dwNum;
1767 for (uint32 i = dwV0; i < end; i++)
1771 g_vtxNonTransformed[i].x = (float)*(short*)((pVtxBase+nOff + 0) ^ 2);
1772 g_vtxNonTransformed[i].y = (float)*(short*)((pVtxBase+nOff + 2) ^ 2);
1773 g_vtxNonTransformed[i].z = (float)*(short*)((pVtxBase+nOff + 4) ^ 2);
1775 //if( status.isSSEEnabled )
1776 // SSEVec3TransformDKR(g_vtxTransformed[i], g_vtxNonTransformed[i]);
1778 Vec3Transform(&g_vtxTransformed[i], (XVECTOR3*)&g_vtxNonTransformed[i], &matWorldProject); // Convert to w=1
1780 if( gRSP.DKRVtxCount == 0 && dwNum==1 )
1782 gRSP.DKRBaseVec.x = g_vtxTransformed[i].x;
1783 gRSP.DKRBaseVec.y = g_vtxTransformed[i].y;
1784 gRSP.DKRBaseVec.z = g_vtxTransformed[i].z;
1785 gRSP.DKRBaseVec.w = g_vtxTransformed[i].w;
1789 g_vtxTransformed[i].x += gRSP.DKRBaseVec.x;
1790 g_vtxTransformed[i].y += gRSP.DKRBaseVec.y;
1791 g_vtxTransformed[i].z += gRSP.DKRBaseVec.z;
1792 g_vtxTransformed[i].w = gRSP.DKRBaseVec.w;
1795 g_vecProjected[i].w = 1.0f / g_vtxTransformed[i].w;
1796 g_vecProjected[i].x = g_vtxTransformed[i].x * g_vecProjected[i].w;
1797 g_vecProjected[i].y = g_vtxTransformed[i].y * g_vecProjected[i].w;
1798 g_vecProjected[i].z = g_vtxTransformed[i].z * g_vecProjected[i].w;
1802 VTX_DUMP(TRACE5("vtx %d: %f, %f, %f, %f", i,
1803 g_vtxTransformed[i].x,g_vtxTransformed[i].y,g_vtxTransformed[i].z,g_vtxTransformed[i].w));
1805 if( gRSP.bFogEnabled )
1807 g_fFogCoord[i] = g_vecProjected[i].z;
1808 if( g_vecProjected[i].w < 0 || g_vecProjected[i].z < 0 || g_fFogCoord[i] < gRSPfFogMin )
1809 g_fFogCoord[i] = gRSPfFogMin;
1812 RSP_Vtx_Clipping(i);
1814 short wA = *(short*)((pVtxBase+nOff + 6) ^ 2);
1815 short wB = *(short*)((pVtxBase+nOff + 8) ^ 2);
1817 s8 r = (s8)(wA >> 8);
1819 s8 b = (s8)(wB >> 8);
1822 if (gRSP.bLightingEnable)
1824 g_normal.x = (char)r; //norma.nx;
1825 g_normal.y = (char)g; //norma.ny;
1826 g_normal.z = (char)b; //norma.nz;
1828 Vec3TransformNormal(g_normal, matWorldProject)
1829 #if !defined(NO_ASM)
1830 if( status.isSSEEnabled )
1831 g_dwVtxDifColor[i] = SSELightVert();
1834 g_dwVtxDifColor[i] = LightVert(g_normal, i);
1844 // Assign true vert colour after lighting/fogging
1845 g_dwVtxDifColor[i] = COLOR_RGBA(nR, nG, nB, nA);
1848 ReplaceAlphaWithFogFactor(i);
1850 g_fVtxTxtCoords[i].x = g_fVtxTxtCoords[i].y = 1;
1856 DEBUGGER_PAUSE_AND_DUMP(NEXT_VERTEX_CMD,{DebuggerAppendMsg("Paused at DKR Vertex Cmd, v0=%d, vn=%d, addr=%08X", dwV0, dwNum, dwAddr);});
1860 extern uint32 dwPDCIAddr;
1861 void ProcessVertexDataPD(uint32 dwAddr, uint32 dwV0, uint32 dwNum)
1863 UpdateCombinedMatrix();
1865 N64VtxPD * pVtxBase = (N64VtxPD*)(g_pRDRAMu8 + dwAddr);
1866 g_pVtxBase = (FiddledVtx*)pVtxBase; // Fix me
1868 for (uint32 i = dwV0; i < dwV0 + dwNum; i++)
1870 N64VtxPD &vert = pVtxBase[i - dwV0];
1872 g_vtxNonTransformed[i].x = (float)vert.x;
1873 g_vtxNonTransformed[i].y = (float)vert.y;
1874 g_vtxNonTransformed[i].z = (float)vert.z;
1876 #if !defined(NO_ASM)
1877 if( status.isSSEEnabled )
1878 SSEVec3Transform(i);
1882 Vec3Transform(&g_vtxTransformed[i], (XVECTOR3*)&g_vtxNonTransformed[i], &gRSPworldProject); // Convert to w=1
1883 g_vecProjected[i].w = 1.0f / g_vtxTransformed[i].w;
1884 g_vecProjected[i].x = g_vtxTransformed[i].x * g_vecProjected[i].w;
1885 g_vecProjected[i].y = g_vtxTransformed[i].y * g_vecProjected[i].w;
1886 g_vecProjected[i].z = g_vtxTransformed[i].z * g_vecProjected[i].w;
1889 g_fFogCoord[i] = g_vecProjected[i].z;
1890 if( g_vecProjected[i].w < 0 || g_vecProjected[i].z < 0 || g_fFogCoord[i] < gRSPfFogMin )
1891 g_fFogCoord[i] = gRSPfFogMin;
1893 RSP_Vtx_Clipping(i);
1895 uint8 *addr = g_pRDRAMu8+dwPDCIAddr+ (vert.cidx&0xFF);
1901 if( gRSP.bLightingEnable )
1903 g_normal.x = (char)r;
1904 g_normal.y = (char)g;
1905 g_normal.z = (char)b;
1906 #if !defined(NO_ASM)
1907 if( status.isSSEEnabled )
1909 SSEVec3TransformNormal();
1910 g_dwVtxDifColor[i] = SSELightVert();
1915 Vec3TransformNormal(g_normal, gRSPmodelViewTop);
1916 g_dwVtxDifColor[i] = LightVert(g_normal, i);
1918 *(((uint8*)&(g_dwVtxDifColor[i]))+3) = (uint8)a; // still use alpha from the vertex
1922 if( (gRDP.geometryMode & G_SHADE) == 0 && gRSP.ucode < 5 ) //Shade is disabled
1924 g_dwVtxDifColor[i] = gRDP.primitiveColor;
1928 g_dwVtxDifColor[i] = COLOR_RGBA(r, g, b, a);
1932 if( options.bWinFrameMode )
1934 g_dwVtxDifColor[i] = COLOR_RGBA(r, g, b, a);
1937 ReplaceAlphaWithFogFactor(i);
1939 VECTOR2 & t = g_fVtxTxtCoords[i];
1940 if (gRSP.bTextureGen && gRSP.bLightingEnable )
1942 // Not sure if we should transform the normal here
1943 //Matrix & matWV = gRSP.projectionMtxs[gRSP.projectionMtxTop];
1944 //Vec3TransformNormal(g_normal, matWV);
1946 TexGen(g_fVtxTxtCoords[i].x, g_fVtxTxtCoords[i].y);
1957 DebuggerAppendMsg("vtx %d: %d %d %d", i, vert.x,vert.y,vert.z);
1958 DebuggerAppendMsg(" : %f, %f, %f, %f",
1959 g_vtxTransformed[i].x,g_vtxTransformed[i].y,g_vtxTransformed[i].z,g_vtxTransformed[i].w);
1960 DebuggerAppendMsg(" : %X, %X, %X, %X", r,g,b,a);
1961 DebuggerAppendMsg(" : u=%f, v=%f", t.x, t.y);
1965 VTX_DUMP(TRACE2("Setting Vertexes: %d - %d\n", dwV0, dwV0+dwNum-1));
1966 DEBUGGER_PAUSE_AND_DUMP(NEXT_VERTEX_CMD,{TRACE0("Paused at Vertex Cmd");});
1969 extern uint32 dwConkerVtxZAddr;
1970 void ProcessVertexDataConker(uint32 dwAddr, uint32 dwV0, uint32 dwNum)
1972 UpdateCombinedMatrix();
1974 FiddledVtx * pVtxBase = (FiddledVtx*)(g_pRDRAMu8 + dwAddr);
1975 g_pVtxBase = pVtxBase;
1976 //short *vertexColoraddr = (short*)(g_pRDRAMu8+dwConkerVtxZAddr);
1978 for (uint32 i = dwV0; i < dwV0 + dwNum; i++)
1980 SP_Timing(RSP_GBI0_Vtx);
1982 FiddledVtx & vert = pVtxBase[i - dwV0];
1984 g_vtxNonTransformed[i].x = (float)vert.x;
1985 g_vtxNonTransformed[i].y = (float)vert.y;
1986 g_vtxNonTransformed[i].z = (float)vert.z;
1988 #if !defined(NO_ASM)
1989 if( status.isSSEEnabled )
1990 SSEVec3Transform(i);
1994 Vec3Transform(&g_vtxTransformed[i], (XVECTOR3*)&g_vtxNonTransformed[i], &gRSPworldProject); // Convert to w=1
1995 g_vecProjected[i].w = 1.0f / g_vtxTransformed[i].w;
1996 g_vecProjected[i].x = g_vtxTransformed[i].x * g_vecProjected[i].w;
1997 g_vecProjected[i].y = g_vtxTransformed[i].y * g_vecProjected[i].w;
1998 g_vecProjected[i].z = g_vtxTransformed[i].z * g_vecProjected[i].w;
2001 g_fFogCoord[i] = g_vecProjected[i].z;
2002 if( g_vecProjected[i].w < 0 || g_vecProjected[i].z < 0 || g_fFogCoord[i] < gRSPfFogMin )
2003 g_fFogCoord[i] = gRSPfFogMin;
2007 uint32 *dat = (uint32*)(&vert);
2008 DebuggerAppendMsg("vtx %d: %08X %08X %08X %08X", i, dat[0],dat[1],dat[2],dat[3]);
2009 DebuggerAppendMsg(" : %f, %f, %f, %f",
2010 g_vtxTransformed[i].x,g_vtxTransformed[i].y,g_vtxTransformed[i].z,g_vtxTransformed[i].w);
2011 DebuggerAppendMsg(" : %f, %f, %f, %f",
2012 g_vecProjected[i].x,g_vecProjected[i].y,g_vecProjected[i].z,g_vecProjected[i].w);
2015 RSP_Vtx_Clipping(i);
2017 if( gRSP.bLightingEnable )
2020 uint32 r= ((gRSP.ambientLightColor>>16)&0xFF);
2021 uint32 g= ((gRSP.ambientLightColor>> 8)&0xFF);
2022 uint32 b= ((gRSP.ambientLightColor )&0xFF);
2023 for( uint32 k=1; k<=gRSPnumLights; k++)
2025 r += gRSPlights[k].r;
2026 g += gRSPlights[k].g;
2027 b += gRSPlights[k].b;
2038 g_dwVtxDifColor[i] = 0xFF000000;
2039 g_dwVtxDifColor[i] |= (r<<16);
2040 g_dwVtxDifColor[i] |= (g<< 8);
2041 g_dwVtxDifColor[i] |= (b );
2044 *(((uint8*)&(g_dwVtxDifColor[i]))+3) = vert.rgba.a; // still use alpha from the vertex
2048 if( (gRDP.geometryMode & G_SHADE) == 0 && gRSP.ucode < 5 ) //Shade is disabled
2050 g_dwVtxDifColor[i] = gRDP.primitiveColor;
2054 g_dwVtxDifColor[i] = COLOR_RGBA(vert.rgba.r, vert.rgba.g, vert.rgba.b, vert.rgba.a);
2058 if( options.bWinFrameMode )
2060 //g_vecProjected[i].z = 0;
2061 g_dwVtxDifColor[i] = COLOR_RGBA(vert.rgba.r, vert.rgba.g, vert.rgba.b, vert.rgba.a);
2064 ReplaceAlphaWithFogFactor(i);
2066 // Update texture coords n.b. need to divide tu/tv by bogus scale on addition to buffer
2067 //VECTOR2 & t = g_fVtxTxtCoords[i];
2069 // If the vert is already lit, then there is no normal (and hence we
2070 // can't generate tex coord)
2071 if (gRSP.bTextureGen && gRSP.bLightingEnable )
2073 g_normal.x = (float)*(char*)(g_pRDRAMu8+ (((i<<1)+0)^3)+dwConkerVtxZAddr);
2074 g_normal.y = (float)*(char*)(g_pRDRAMu8+ (((i<<1)+1)^3)+dwConkerVtxZAddr);
2075 g_normal.z = (float)*(char*)(g_pRDRAMu8+ (((i<<1)+2)^3)+dwConkerVtxZAddr);
2076 Vec3TransformNormal(g_normal, gRSPmodelViewTop);
2077 TexGen(g_fVtxTxtCoords[i].x, g_fVtxTxtCoords[i].y);
2081 g_fVtxTxtCoords[i].x = (float)vert.tu;
2082 g_fVtxTxtCoords[i].y = (float)vert.tv;
2086 VTX_DUMP(TRACE2("Setting Vertexes: %d - %d\n", dwV0, dwV0+dwNum-1));
2087 DEBUGGER_PAUSE_AND_DUMP(NEXT_VERTEX_CMD,{DebuggerAppendMsg("Paused at Vertex Cmd");});
2114 void ProcessVertexData_Rogue_Squadron(uint32 dwXYZAddr, uint32 dwColorAddr, uint32 dwXYZCmd, uint32 dwColorCmd)
2116 UpdateCombinedMatrix();
2119 uint32 dwNum = (dwXYZCmd&0xFF00)>>10;
2121 RS_Vtx_XYZ * pVtxXYZBase = (RS_Vtx_XYZ*)(g_pRDRAMu8 + dwXYZAddr);
2122 RS_Vtx_Color * pVtxColorBase = (RS_Vtx_Color*)(g_pRDRAMu8 + dwColorAddr);
2125 for (i = dwV0; i < dwV0 + dwNum; i++)
2127 RS_Vtx_XYZ & vertxyz = pVtxXYZBase[i - dwV0];
2128 RS_Vtx_Color & vertcolors = pVtxColorBase[i - dwV0];
2130 g_vtxNonTransformed[i].x = (float)vertxyz.x;
2131 g_vtxNonTransformed[i].y = (float)vertxyz.y;
2132 g_vtxNonTransformed[i].z = (float)vertxyz.z;
2134 #if !defined(NO_ASM)
2135 if( status.isSSEEnabled )
2136 SSEVec3Transform(i);
2140 Vec3Transform(&g_vtxTransformed[i], (XVECTOR3*)&g_vtxNonTransformed[i], &gRSPworldProject); // Convert to w=1
2141 g_vecProjected[i].w = 1.0f / g_vtxTransformed[i].w;
2142 g_vecProjected[i].x = g_vtxTransformed[i].x * g_vecProjected[i].w;
2143 g_vecProjected[i].y = g_vtxTransformed[i].y * g_vecProjected[i].w;
2144 g_vecProjected[i].z = g_vtxTransformed[i].z * g_vecProjected[i].w;
2149 DebuggerAppendMsg(" : %f, %f, %f, %f",
2150 g_vtxTransformed[i].x,g_vtxTransformed[i].y,g_vtxTransformed[i].z,g_vtxTransformed[i].w);
2151 DebuggerAppendMsg(" : %f, %f, %f, %f",
2152 g_vecProjected[i].x,g_vecProjected[i].y,g_vecProjected[i].z,g_vecProjected[i].w);
2155 g_fFogCoord[i] = g_vecProjected[i].z;
2156 if( g_vecProjected[i].w < 0 || g_vecProjected[i].z < 0 || g_fFogCoord[i] < gRSPfFogMin )
2157 g_fFogCoord[i] = gRSPfFogMin;
2159 RSP_Vtx_Clipping(i);
2161 if( gRSP.bLightingEnable )
2163 g_normal.x = (float)vertcolors.nx;
2164 g_normal.y = (float)vertcolors.ny;
2165 g_normal.z = (float)vertcolors.nz;
2167 #if !defined(NO_ASM)
2168 if( status.isSSEEnabled )
2170 SSEVec3TransformNormal();
2171 g_dwVtxDifColor[i] = SSELightVert();
2176 Vec3TransformNormal(g_normal, gRSPmodelViewTop);
2177 g_dwVtxDifColor[i] = LightVert(g_normal, i);
2179 *(((uint8*)&(g_dwVtxDifColor[i]))+3) = vertcolors.a; // still use alpha from the vertex
2183 if( (gRDP.geometryMode & G_SHADE) == 0 && gRSP.ucode < 5 ) //Shade is disabled
2185 g_dwVtxDifColor[i] = gRDP.primitiveColor;
2189 g_dwVtxDifColor[i] = COLOR_RGBA(vertcolors.r, vertcolors.g, vertcolors.b, vertcolors.a);
2193 if( options.bWinFrameMode )
2195 g_dwVtxDifColor[i] = COLOR_RGBA(vertcolors.r, vertcolors.g, vertcolors.b, vertcolors.a);
2198 ReplaceAlphaWithFogFactor(i);
2201 // Update texture coords n.b. need to divide tu/tv by bogus scale on addition to buffer
2202 VECTOR2 & t = g_fVtxTxtCoords[i];
2204 // If the vert is already lit, then there is no normal (and hence we
2205 // can't generate tex coord)
2206 if (gRSP.bTextureGen && gRSP.bLightingEnable && g_textures[gRSP.curTile].m_bTextureEnable )
2208 TexGen(g_fVtxTxtCoords[i].x, g_fVtxTxtCoords[i].y);
2212 t.x = (float)vert.tu;
2213 t.y = (float)vert.tv;
2218 VTX_DUMP(TRACE2("Setting Vertexes: %d - %d\n", dwV0, dwV0+dwNum-1));
2219 DEBUGGER_PAUSE_AND_DUMP(NEXT_VERTEX_CMD,{TRACE0("Paused at Vertex Cmd");});
2222 void SetLightCol(uint32 dwLight, uint32 dwCol)
2224 gRSPlights[dwLight].r = (uint8)((dwCol >> 24)&0xFF);
2225 gRSPlights[dwLight].g = (uint8)((dwCol >> 16)&0xFF);
2226 gRSPlights[dwLight].b = (uint8)((dwCol >> 8)&0xFF);
2227 gRSPlights[dwLight].a = 255; // Ignore light alpha
2228 gRSPlights[dwLight].fr = (float)gRSPlights[dwLight].r;
2229 gRSPlights[dwLight].fg = (float)gRSPlights[dwLight].g;
2230 gRSPlights[dwLight].fb = (float)gRSPlights[dwLight].b;
2231 gRSPlights[dwLight].fa = 255; // Ignore light alpha
2233 //TRACE1("Set light %d color", dwLight);
2234 LIGHT_DUMP(TRACE2("Set Light %d color: %08X", dwLight, dwCol));
2237 void SetLightDirection(uint32 dwLight, float x, float y, float z, float range)
2239 //gRSP.bLightIsUpdated = true;
2241 //gRSPlights[dwLight].ox = x;
2242 //gRSPlights[dwLight].oy = y;
2243 //gRSPlights[dwLight].oz = z;
2245 register float w = range == 0 ? (float)sqrt(x*x+y*y+z*z) : 1;
2247 gRSPlights[dwLight].x = x/w;
2248 gRSPlights[dwLight].y = y/w;
2249 gRSPlights[dwLight].z = z/w;
2250 gRSPlights[dwLight].range = range;
2251 DEBUGGER_PAUSE_AND_DUMP(NEXT_SET_LIGHT,TRACE5("Set Light %d dir: %.4f, %.4f, %.4f, %.4f", dwLight, x, y, z, range));
2254 static float maxS0, maxT0;
2255 static float maxS1, maxT1;
2256 static bool validS0, validT0;
2257 static bool validS1, validT1;
2259 void LogTextureCoords(float fTex0S, float fTex0T, float fTex1S, float fTex1T)
2263 if( fTex0S<0 || fTex0S>maxS0 ) validS0 = false;
2267 if( fTex0T<0 || fTex0T>maxT0 ) validT0 = false;
2271 if( fTex1S<0 || fTex1S>maxS1 ) validS1 = false;
2275 if( fTex1T<0 || fTex1T>maxT1 ) validT1 = false;
2279 bool CheckTextureCoords(int tex)
2283 return validS0&&validT0;
2287 return validS1&&validT1;
2291 void ResetTextureCoordsLog(float maxs0, float maxt0, float maxs1, float maxt1)
2297 validS0 = validT0 = true;
2298 validS1 = validT1 = true;
2301 void ForceMainTextureIndex(int dwTile)
2303 if( dwTile == 1 && !(CRender::g_pRender->IsTexel0Enable()) && CRender::g_pRender->IsTexel1Enable() )
2310 gRSP.curTile = dwTile;
2314 float HackZ2(float z)
2320 float HackZ(float z)
2324 if( z < 0.1 && z >= 0 )
2327 //return (10+z)/100;
2332 void HackZ(std::vector<XVECTOR3>& points)
2334 int size = points.size();
2335 for( int i=0; i<size; i++)
2337 XVECTOR3 &v = points[i];
2338 v.z = (float)HackZ(v.z);
2344 if( CDeviceBuilder::m_deviceGeneralType == DIRECTX_DEVICE )
2346 for( uint32 i=0; i<gRSP.numVertices; i++)
2348 g_vtxBuffer[i].z = HackZ(g_vtxBuffer[i].z);
2353 for( uint32 i=0; i<gRSP.numVertices; i++)
2355 float w = g_vtxProjected5[i][3];
2356 g_vtxProjected5[i][2] = HackZ(g_vtxProjected5[i][2]/w)*w;
2362 extern XMATRIX reverseXY;
2363 extern XMATRIX reverseY;
2365 void UpdateCombinedMatrix()
2367 if( gRSP.bMatrixIsUpdated )
2369 gRSPworldProject = gRSP.modelviewMtxs[gRSP.modelViewMtxTop] * gRSP.projectionMtxs[gRSP.projectionMtxTop];
2370 gRSP.bMatrixIsUpdated = false;
2371 gRSP.bCombinedMatrixIsUpdated = true;
2374 if( gRSP.bCombinedMatrixIsUpdated )
2376 if( options.enableHackForGames == HACK_REVERSE_XY_COOR )
2378 gRSPworldProject = gRSPworldProject * reverseXY;
2380 if( options.enableHackForGames == HACK_REVERSE_Y_COOR )
2382 gRSPworldProject = gRSPworldProject * reverseY;
2384 #if !defined(NO_ASM)
2385 if( status.isSSEEnabled )
2387 MatrixTranspose(&gRSPworldProjectTransported, &gRSPworldProject);
2390 gRSP.bCombinedMatrixIsUpdated = false;
2393 //if( gRSP.bWorldMatrixIsUpdated || gRSP.bLightIsUpdated )
2395 // // Update lights with transported world matrix
2396 // for( unsigned int l=0; l<gRSPnumLights; l++)
2398 // Vec3TransformCoord(&gRSPlights[l].td, &gRSPlights[l].od, &gRSPmodelViewTopTranspose);
2399 // Vec3Normalize(&gRSPlights[l].td,&gRSPlights[l].td);
2402 // gRSP.bWorldMatrixIsUpdated = false;
2403 // gRSP.bLightIsUpdated = false;