GLES2RICE: Fix for good this time the shader not compiling
[mupen64plus-pandora.git] / source / gles2rice / src / RenderBase.cpp
CommitLineData
292f9317 1/*
2Copyright (C) 2003 Rice1964
3
4This program is free software; you can redistribute it and/or
5modify it under the terms of the GNU General Public License
6as published by the Free Software Foundation; either version 2
7of the License, or (at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program; if not, write to the Free Software
16Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
18*/
19
20#include <cmath>
21#include <vector>
22
23#include "osal_preproc.h"
24#include "float.h"
25#include "DeviceBuilder.h"
26#include "VertexShaderConstantDef.h"
27#include "Render.h"
28#include "Timing.h"
29
30extern FiddledVtx * g_pVtxBase;
31
32#define ENABLE_CLIP_TRI
33#define X_CLIP_MAX 0x1
34#define X_CLIP_MIN 0x2
35#define Y_CLIP_MAX 0x4
36#define Y_CLIP_MIN 0x8
37#define Z_CLIP_MAX 0x10
38#define Z_CLIP_MIN 0x20
39
40#ifdef ENABLE_CLIP_TRI
41
42inline void RSP_Vtx_Clipping(int i)
43{
44 g_clipFlag[i] = 0;
45 g_clipFlag2[i] = 0;
46 if( g_vecProjected[i].w > 0 )
47 {
48 /*
49 if( gRSP.bRejectVtx )
50 {
51 if( g_vecProjected[i].x > 1 )
52 {
53 g_clipFlag2[i] |= X_CLIP_MAX;
54 if( g_vecProjected[i].x > gRSP.real_clip_ratio_posx )
55 g_clipFlag[i] |= X_CLIP_MAX;
56 }
57
58 if( g_vecProjected[i].x < -1 )
59 {
60 g_clipFlag2[i] |= X_CLIP_MIN;
61 if( g_vecProjected[i].x < gRSP.real_clip_ratio_negx )
62 g_clipFlag[i] |= X_CLIP_MIN;
63 }
64
65 if( g_vecProjected[i].y > 1 )
66 {
67 g_clipFlag2[i] |= Y_CLIP_MAX;
68 if( g_vecProjected[i].y > gRSP.real_clip_ratio_posy )
69 g_clipFlag[i] |= Y_CLIP_MAX;
70 }
71
72 if( g_vecProjected[i].y < -1 )
73 {
74 g_clipFlag2[i] |= Y_CLIP_MIN;
75 if( g_vecProjected[i].y < gRSP.real_clip_ratio_negy )
76 g_clipFlag[i] |= Y_CLIP_MIN;
77 }
78
79 //if( g_vecProjected[i].z > 1.0f )
80 //{
81 // g_clipFlag2[i] |= Z_CLIP_MAX;
82 // g_clipFlag[i] |= Z_CLIP_MAX;
83 //}
84
85 //if( gRSP.bNearClip && g_vecProjected[i].z < -1.0f )
86 //{
87 // g_clipFlag2[i] |= Z_CLIP_MIN;
88 // g_clipFlag[i] |= Z_CLIP_MIN;
89 //}
90 }
91 else
92 */
93 {
94 if( g_vecProjected[i].x > 1 ) g_clipFlag2[i] |= X_CLIP_MAX;
95 if( g_vecProjected[i].x < -1 ) g_clipFlag2[i] |= X_CLIP_MIN;
96 if( g_vecProjected[i].y > 1 ) g_clipFlag2[i] |= Y_CLIP_MAX;
97 if( g_vecProjected[i].y < -1 ) g_clipFlag2[i] |= Y_CLIP_MIN;
98 //if( g_vecProjected[i].z > 1.0f ) g_clipFlag2[i] |= Z_CLIP_MAX;
99 //if( gRSP.bNearClip && g_vecProjected[i].z < -1.0f ) g_clipFlag2[i] |= Z_CLIP_MIN;
100 }
101
102 }
103}
104
105#else
106inline void RSP_Vtx_Clipping(int i) {}
107#endif
108
109/*
110 * Global variables
111 */
112ALIGN(16,RSP_Options gRSP)
113ALIGN(16,RDP_Options gRDP)
114
115static ALIGN(16,XVECTOR4 g_normal)
116//static int norms[3];
117
118ALIGN(16,XVECTOR4 g_vtxNonTransformed[MAX_VERTS])
119ALIGN(16,XVECTOR4 g_vecProjected[MAX_VERTS])
120ALIGN(16,XVECTOR4 g_vtxTransformed[MAX_VERTS])
121
122float g_vtxProjected5[1000][5];
123float g_vtxProjected5Clipped[2000][5];
124
125//uint32 g_dwVtxFlags[MAX_VERTS]; // Z_POS Z_NEG etc
126VECTOR2 g_fVtxTxtCoords[MAX_VERTS];
127uint32 g_dwVtxDifColor[MAX_VERTS];
128uint32 g_clipFlag[MAX_VERTS];
129uint32 g_clipFlag2[MAX_VERTS];
130RenderTexture g_textures[MAX_TEXTURES];
131float g_fFogCoord[MAX_VERTS];
132
133EXTERNAL_VERTEX g_vtxForExternal[MAX_VERTS];
134
135TLITVERTEX g_vtxBuffer[1000];
136TLITVERTEX g_clippedVtxBuffer[2000];
137uint8 g_oglVtxColors[1000][4];
138int g_clippedVtxCount=0;
139TLITVERTEX g_texRectTVtx[4];
140unsigned short g_vtxIndex[1000];
141unsigned int g_minIndex, g_maxIndex;
142
143float gRSPfFogMin;
144float gRSPfFogMax;
145float gRSPfFogDivider;
146
147uint32 gRSPnumLights;
148Light gRSPlights[16];
149
150ALIGN(16,Matrix gRSPworldProjectTransported)
151ALIGN(16,Matrix gRSPworldProject)
152ALIGN(16,Matrix gRSPmodelViewTop)
153ALIGN(16,Matrix gRSPmodelViewTopTranspose)
154ALIGN(16,Matrix dkrMatrixTransposed)
155
156N64Light gRSPn64lights[16];
157
158
159void (*ProcessVertexData)(uint32 dwAddr, uint32 dwV0, uint32 dwNum)=NULL;
160
161/*
162 *
163 */
164
165
166/*n.x = (g_normal.x * matWorld.m00) + (g_normal.y * matWorld.m10) + (g_normal.z * matWorld.m20);
167n.y = (g_normal.x * matWorld.m01) + (g_normal.y * matWorld.m11) + (g_normal.z * matWorld.m21);
168n.z = (g_normal.x * matWorld.m02) + (g_normal.y * matWorld.m12) + (g_normal.z * matWorld.m22);*/
169
170// Multiply (x,y,z,0) by matrix m, then normalize
171#if defined(__INTEL_COMPILER) && !defined(NO_ASM)
172#define Vec3TransformNormal(vec, m) __asm \
173{ \
174 __asm fld dword ptr [vec + 0] \
175 __asm fmul dword ptr [m + 0] \ /* x m00*/
176 __asm fld dword ptr [vec + 0] \
177 __asm fmul dword ptr [m + 4] \ /* x m01 x m00*/
178 __asm fld dword ptr [vec + 0] \
179 __asm fmul dword ptr [m + 8] \ /* x m02 x m01 x m00*/
180 \
181 __asm fld dword ptr [vec + 4] \
182 __asm fmul dword ptr [m + 16] \ /* y m10 x m02 x m01 x m00*/
183 __asm fld dword ptr [vec + 4] \
184 __asm fmul dword ptr [m + 20] \ /* y m11 y m10 x m02 x m01 x m00*/
185 __asm fld dword ptr [vec + 4] \
186 __asm fmul dword ptr [m + 24] \ /* y m12 y m11 y m10 x m02 x m01 x m00*/
187 \
188 __asm fxch st(2) \ /* y m10 y m11 y m12 x m02 x m01 x m00*/
189 __asm faddp st(5), st(0) \ /* y m11 y m12 x m02 x m01 (x m00 + y m10)*/
190 __asm faddp st(3), st(0) \ /* y m12 x m02 (x m01 + ym11) (x m00 + y m10)*/
191 __asm faddp st(1), st(0) \ /* (x m02 + y m12) (x m01 + ym11) (x m00 + y m10)*/
192 \
193 __asm fld dword ptr [vec + 8] \
194 __asm fmul dword ptr [m + 32] \ /* z m20 (x m02 + y m12) (x m01 + ym11) (x m00 + y m10)*/
195 __asm fld dword ptr [vec + 8] \
196 __asm fmul dword ptr [m + 36] \ /* z m21 z m20 (x m02 + y m12) (x m01 + ym11) (x m00 + y m10)*/
197 __asm fld dword ptr [vec + 8] \
198 __asm fmul dword ptr [m + 40] \ /* z m22 z m21 z m20 (x m02 + y m12) (x m01 + ym11) (x m00 + y m10)*/
199 \
200 __asm fxch st(2) \ /* z m20 z m21 z m22 (x m02 + y m12) (x m01 + ym11) (x m00 + y m10)*/
201 __asm faddp st(5), st(0) \ /* z m21 z m22 (x m02 + y m12) (x m01 + ym11) (x m00 + y m10 + z m20)*/
202 __asm faddp st(3), st(0) \ /* z m22 (x m02 + y m12) (x m01 + ym11 + z m21) (x m00 + y m10 + z m20)*/
203 __asm faddp st(1), st(0) \ /* (x m02 + y m12 + z m 22) (x m01 + ym11 + z m21) (x m00 + y m10 + z m20)*/
204 \
205 __asm fxch st(2) \ /* (x m00 + y m10 + z m20) (x m01 + ym11 + z m21) (x m02 + y m12 + z m 22) */
206 \
207 __asm fld1 \ /* 1 x y z */
208 __asm fld st(1) \ /* x 1 x y z */
209 __asm fmul st(0),st(0) \ /* xx 1 x y z */
210 __asm fld st(3) \ /* y xx 1 x y z */
211 __asm fmul st(0),st(0) \ /* yy xx 1 x y z */
212 __asm fld st(5) \ /* z yy xx 1 x y z */
213 __asm fmul st(0),st(0) \ /* zz yy xx 1 x y z */
214 \
215 __asm fxch st(2) \ /* xx yy zz 1 x y z */
216 \
217 __asm faddp st(1),st(0) \ /* (xx+yy) zz 1 x y z */
218 __asm faddp st(1),st(0) \ /* (xx+yy+zz) 1 x y z */
219 \
220 __asm ftst \ /* Compare ST to 0 */
221 __asm fstsw ax \ /* Store FPU status word in a */
222 __asm sahf \ /* Transfer ax to flags register */
223 __asm jz l2 \ /* Skip if length is zero */
224 \
225 __asm fsqrt \ /* l 1 x y z */
226 \
227 __asm fdivp st(1),st(0) \ /* (1/l) x y z */
228 \
229 __asm fmul st(3),st(0) \ /* f x y fz */
230 __asm fmul st(2),st(0) \ /* f x fy fz */
231 __asm fmulp st(1),st(0) \ /* fx fy fz */
232 \
233 __asm fstp dword ptr [vec + 0] \ /* fy fz*/
234 __asm fstp dword ptr [vec + 4] \ /* fz */
235 __asm fstp dword ptr [vec + 8] \ /* done */
236 __asm jmp l3 \
237__asm l2: \
238 __asm mov dword ptr [vec + 0], 0 \
239 __asm mov dword ptr [vec + 4], 0 \
240 __asm mov dword ptr [vec + 8], 0 \
241__asm l3: \
242} \
243
244#else // use C code in other cases, this is probably faster anyway
245#define Vec3TransformNormal(vec, m) \
246 VECTOR3 temp; \
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; }
253#endif
254
255
256#if !defined(__GNUC__) && !defined(NO_ASM)
257__declspec( naked ) void __fastcall SSEVec3Transform(int i)
258{
259 __asm
260 {
261 shl ecx,4; // ecx = i
262
263 movaps xmm1, DWORD PTR g_vtxNonTransformed [ecx]; // xmm1 as original vector
264
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
269
270 mulps xmm4, xmm1; // row 1
271 mulps xmm5, xmm1; // row 2
272 mulps xmm6, xmm1; // row 3
273 mulps xmm7, xmm1; // row 4
274
275 movhlps xmm0, xmm4; // xmm4 high to xmm0 low
276 movlhps xmm0, xmm5; // xmm5 low to xmm0 high
277
278 addps xmm4, xmm0; // result of add are in xmm4 low
279 addps xmm5, xmm0; // result of add are in xmm5 high
280
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
284
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
288
289 addps xmm4, xmm5; // results are in 1st and 2nd uint32
290
291
292 movhlps xmm0, xmm6; // xmm6 high to xmm0 low
293 movlhps xmm0, xmm7; // xmm7 low to xmm0 high
294
295 addps xmm6, xmm0; // result of add are in xmm6 low
296 addps xmm7, xmm0; // result of add are in xmm7 high
297
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
301
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
305
306 addps xmm6, xmm7; // results are in 1st and 2nd uint32
307
308 movlhps xmm4, xmm6; // final result is in xmm4
309 movaps DWORD PTR g_vtxTransformed [ecx], xmm4;
310
311 movaps xmm0,xmm4;
312 shufps xmm0,xmm0,0xff;
313 divps xmm4,xmm0;
314 rcpps xmm0,xmm0;
315 movhlps xmm0,xmm4;
316 shufps xmm0,xmm0,0xe8;
317 movlhps xmm4,xmm0;
318
319 movaps DWORD PTR g_vecProjected [ecx], xmm4;
320
321 emms;
322 ret;
323 }
324}
325
326// Only used by DKR
327__declspec( naked ) void __fastcall SSEVec3TransformDKR(XVECTOR4 &pOut, const XVECTOR4 &pV)
328{
329 __asm
330 {
331 movaps xmm1, DWORD PTR [edx]; // xmm1 as original vector
332
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
337
338 mulps xmm4, xmm1; // row 1
339 mulps xmm5, xmm1; // row 2
340 mulps xmm6, xmm1; // row 3
341 mulps xmm7, xmm1; // row 4
342
343 movhlps xmm0, xmm4; // xmm4 high to xmm0 low
344 movlhps xmm0, xmm5; // xmm5 low to xmm0 high
345
346 addps xmm4, xmm0; // result of add are in xmm4 low
347 addps xmm5, xmm0; // result of add are in xmm5 high
348
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
352
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
356
357 addps xmm4, xmm5; // results are in 1st and 2nd uint32
358
359
360 movhlps xmm0, xmm6; // xmm6 high to xmm0 low
361 movlhps xmm0, xmm7; // xmm7 low to xmm0 high
362
363 addps xmm6, xmm0; // result of add are in xmm6 low
364 addps xmm7, xmm0; // result of add are in xmm7 high
365
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
369
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
373
374 addps xmm6, xmm7; // results are in 1st and 2nd uint32
375
376 movlhps xmm4, xmm6; // final result is in xmm4
377 movaps DWORD PTR [ecx], xmm4;
378
379 emms;
380 ret;
381 }
382}
383#elif defined(__GNUC__) && defined(__x86_64__) && !defined(NO_ASM)
384void SSEVec3Transform(int i)
385{
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"
427 : "+r"(i)
428 : "r"(g_vtxNonTransformed), "r"(&gRSPworldProjectTransported.m[0][0]), "r"(g_vtxTransformed), "r"(g_vecProjected)
429 : "memory", "%xmm0", "%xmm1", "%xmm4", "%xmm5", "%xmm6", "%xmm7"
430 );
431}
432#elif !defined(NO_ASM) // 32-bit GCC assumed
433void SSEVec3Transform(int i)
434{
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"
475 : "+r"(i)
476 : "r"(g_vtxNonTransformed), "r"(&gRSPworldProjectTransported.m[0][0]), "r"(g_vtxTransformed), "r"(g_vecProjected)
477 : "memory", "%xmm0", "%xmm1", "%xmm4", "%xmm5", "%xmm6", "%xmm7"
478 );
479}
480#endif
481float real255 = 255.0f;
482float real128 = 128.0f;
483
484#if !defined(__GNUC__) && !defined(NO_ASM)
485__declspec( naked ) void __fastcall SSEVec3TransformNormal()
486{
487 __asm
488 {
489 mov DWORD PTR [g_normal][12], 0;
490
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
495
496 mulps xmm4, xmm1; // row 1
497 mulps xmm5, xmm1; // row 2
498 mulps xmm6, xmm1; // row 3
499
500 movhlps xmm0, xmm4; // xmm4 high to xmm0 low
501 movlhps xmm0, xmm5; // xmm5 low to xmm0 high
502
503 addps xmm4, xmm0; // result of add are in xmm4 low
504 addps xmm5, xmm0; // result of add are in xmm5 high
505
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
509
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,
512
513 addps xmm4, xmm5; // results are in 1st and 2nd uint32
514
515 movaps xmm1,xmm4;
516 mulps xmm1,xmm1; //square
517 movlhps xmm7, xmm1;
518 shufps xmm7, xmm7,0x03;
519 addss xmm7, xmm1;
520
521 movhlps xmm0, xmm6; // xmm6 high to xmm0 low
522 addps xmm6, xmm0; // result of add are in xmm6 low
523
524 movlhps xmm0, xmm6;
525 shufps xmm0, xmm0, 0x03;
526 addss xmm0, xmm6; // result of add is at xmm0's 1st uint32
527
528 movlhps xmm4, xmm0;
529
530 mulss xmm0,xmm0;
531 addss xmm7,xmm0; // xmm7 1st uint32 is the sum of squares
532
533#ifdef DEBUGGER
534 movaps DWORD PTR [g_normal], xmm4;
535 movss DWORD PTR [g_normal][12], xmm7;
536#endif
537 xorps xmm0,xmm0;
538 ucomiss xmm0,xmm7;
539 jz l2
540
541 rsqrtss xmm7,xmm7;
542 shufps xmm7,xmm7,0;
543#ifdef DEBUGGER
544 movss DWORD PTR [g_normal][12], xmm7;
545#endif
546 mulps xmm4,xmm7;
547
548 movaps DWORD PTR [g_normal], xmm4; // Normalized
549 mov DWORD PTR [g_normal][12], 0;
550
551 emms;
552 ret;
553l2:
554 movss DWORD PTR [g_normal], xmm0;
555 movss DWORD PTR [g_normal][12], xmm0;
556 emms;
557 ret;
558 }
559}
560#elif defined(__GNUC__) && !defined(NO_ASM) // this code should compile for both 64-bit and 32-bit architectures
561void SSEVec3TransformNormal(void)
562{
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"
594#ifdef DEBUGGER
595 " movaps %%xmm4, (%0) \n"
596 " movss %%xmm7, 12(%0) \n"
597#endif
598 " xorps %%xmm0, %%xmm0 \n"
599 " ucomiss %%xmm7, %%xmm0 \n"
600 " jz 0f \n"
601 " rsqrtss %%xmm7, %%xmm7 \n"
602 " shufps $0x00, %%xmm7, %%xmm7 \n"
603#ifdef DEBUGGER
604 " movss %%xmm7, 12(%0) \n"
605#endif
606 " mulps %%xmm7, %%xmm4 \n"
607 " movaps %%xmm4, (%0) \n"
608 " movl $0, 12(%0) \n"
609 " jmp 1f \n"
610 "0: \n"
611 " movss %%xmm0, (%0) \n"
612 " movss %%xmm0, 12(%0) \n"
613 "1: \n"
614 :
615 : "r"(&g_normal.x), "r"(&gRSPmodelViewTopTranspose.m[0][0])
616 : "memory", "cc", "%xmm0", "%xmm1", "%xmm4", "%xmm5", "%xmm6", "%xmm7"
617 );
618}
619#endif
620
621void NormalizeNormalVec()
622{
623 float w = 1/sqrtf(g_normal.x*g_normal.x + g_normal.y*g_normal.y + g_normal.z*g_normal.z);
624 g_normal.x *= w;
625 g_normal.y *= w;
626 g_normal.z *= w;
627}
628
629
630void InitRenderBase()
631{
632#if !defined(NO_ASM)
633 if( status.isSSEEnabled && !g_curRomInfo.bPrimaryDepthHack && options.enableHackForGames != HACK_FOR_NASCAR)
634 {
635 ProcessVertexData = ProcessVertexDataSSE;
636 }
637 else
638#endif
639 {
640 ProcessVertexData = ProcessVertexDataNoSSE;
641 }
642
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;
648 gRSP.maxZ = 0;
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;
656
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;
666 gRSP.bCullBack=true;
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;
671 gRDP.fKeyA = 0;
672 gRSP.DKRCMatrixIndex = gRSP.dwDKRVtxAddr = gRSP.dwDKRMatrixAddr = 0;
673 gRSP.DKRBillBoard = false;
674
675 gRSP.fTexScaleX = 1/32.0f;
676 gRSP.fTexScaleY = 1/32.0f;
677 gRSP.bTextureEnabled = FALSE;
678
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;
702
703 gRSP.DKRCMatrixIndex=0;
704 gRSP.DKRVtxCount=0;
705 gRSP.DKRBillBoard = false;
706 gRSP.dwDKRVtxAddr=0;
707 gRSP.dwDKRMatrixAddr=0;
708
709
710 gRDP.geometryMode = 0;
711 gRDP.otherModeL = 0;
712 gRDP.otherModeH = 0;
713 gRDP.fillColor = 0xFFFFFFFF;
714 gRDP.originalFillColor =0;
715
716 gRSP.ucode = 1;
717 gRSP.vertexMult = 10;
718 gRSP.bNearClip = false;
719 gRSP.bRejectVtx = false;
720
721 gRDP.texturesAreReloaded = false;
722 gRDP.textureIsChanged = false;
723 gRDP.colorsAreReloaded = false;
724
725 memset(&gRDP.otherMode,0,sizeof(RDP_OtherMode));
726 memset(&gRDP.tiles,0,sizeof(Tile)*8);
727
728 for( int i=0; i<MAX_VERTS; i++ )
729 {
730 g_clipFlag[i] = 0;
731 g_vtxNonTransformed[i].w = 1;
732 }
733
734 memset(gRSPn64lights, 0, sizeof(N64Light)*16);
735}
736
737void SetFogMinMax(float fMin, float fMax, float fMul, float fOffset)
738{
739 if( fMin > fMax )
740 {
741 float temp = fMin;
742 fMin = fMax;
743 fMax = temp;
744 }
745
746 {
747 gRSPfFogMin = max(0,fMin/500-1);
748 gRSPfFogMax = fMax/500-1;
749 }
750
751 gRSPfFogDivider = 255/(gRSPfFogMax-gRSPfFogMin);
752 CRender::g_pRender->SetFogMinMax(fMin, fMax);
753}
754
755void InitVertexColors()
756{
757}
758
759void InitVertexTextureConstants()
760{
761 float scaleX;
762 float scaleY;
763
764 RenderTexture &tex0 = g_textures[gRSP.curTile];
765 //CTexture *surf = tex0.m_pCTexture;
766 Tile &tile0 = gRDP.tiles[gRSP.curTile];
767
768 scaleX = gRSP.fTexScaleX;
769 scaleY = gRSP.fTexScaleY;
770
771 gRSP.tex0scaleX = scaleX * tile0.fShiftScaleS/tex0.m_fTexWidth;
772 gRSP.tex0scaleY = scaleY * tile0.fShiftScaleT/tex0.m_fTexHeight;
773
774 gRSP.tex0OffsetX = tile0.fhilite_sl/tex0.m_fTexWidth;
775 gRSP.tex0OffsetY = tile0.fhilite_tl/tex0.m_fTexHeight;
776
777 if( CRender::g_pRender->IsTexel1Enable() )
778 {
779 RenderTexture &tex1 = g_textures[(gRSP.curTile+1)&7];
780 //CTexture *surf = tex1.m_pCTexture;
781 Tile &tile1 = gRDP.tiles[(gRSP.curTile+1)&7];
782
783 gRSP.tex1scaleX = scaleX * tile1.fShiftScaleS/tex1.m_fTexWidth;
784 gRSP.tex1scaleY = scaleY * tile1.fShiftScaleT/tex1.m_fTexHeight;
785
786 gRSP.tex1OffsetX = tile1.fhilite_sl/tex1.m_fTexWidth;
787 gRSP.tex1OffsetY = tile1.fhilite_tl/tex1.m_fTexHeight;
788 }
789
790 gRSP.texGenXRatio = tile0.fShiftScaleS;
791 gRSP.texGenYRatio = gRSP.fTexScaleX/gRSP.fTexScaleY*tex0.m_fTexWidth/tex0.m_fTexHeight*tile0.fShiftScaleT;
792}
793
794void TexGen(float &s, float &t)
795{
796 if (gRDP.geometryMode & G_TEXTURE_GEN_LINEAR)
797 {
798 s = acosf(g_normal.x) / 3.14159f;
799 t = acosf(g_normal.y) / 3.14159f;
800 }
801 else
802 {
803 s = 0.5f * ( 1.0f + g_normal.x);
804 t = 0.5f * ( 1.0f - g_normal.y);
805 }
806}
807
808void ComputeLOD(bool openGL)
809{
810 TLITVERTEX &v0 = g_vtxBuffer[0];
811 TLITVERTEX &v1 = g_vtxBuffer[1];
812 RenderTexture &tex0 = g_textures[gRSP.curTile];
813
814 float d,dt;
815 if( openGL )
816 {
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];
819
820 x = windowSetting.vpWidthW*x/windowSetting.fMultX/2;
821 y = windowSetting.vpHeightW*y/windowSetting.fMultY/2;
822 d = sqrtf(x*x+y*y);
823 }
824 else
825 {
826 float x = (v0.x - v1.x)/ windowSetting.fMultX;
827 float y = (v0.y - v1.y)/ windowSetting.fMultY;
828 d = sqrtf(x*x+y*y);
829 }
830
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;
835
836 dt = sqrtf((s0-s1)*(s0-s1)+(t0-t1)*(t0-t1));
837
838 float lod = dt/d;
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();
846}
847
848bool bHalfTxtScale=false;
849extern uint32 lastSetTile;
850
851void InitVertex(uint32 dwV, uint32 vtxIndex, bool bTexture, bool openGL)
852{
853 VTX_DUMP(TRACE2("Init vertex (%d) to vtx buf[%d]:", dwV, vtxIndex));
854
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));
857 if( openGL )
858 {
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;
864
865 if( g_vtxTransformed[dwV].w < 0 )
866 g_vtxProjected5[vtxIndex][4] = 0;
867
868 g_vtxIndex[vtxIndex] = vtxIndex;
869 }
870
871 if( !openGL || options.bOGLVertexClipper == TRUE )
872 {
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));
879
880 if( gRSP.bProcessSpecularColor )
881 {
882 v.dcSpecular = CRender::g_pRender->PostProcessSpecularColor();
883 if( gRSP.bFogEnabled )
884 {
885 v.dcSpecular &= 0x00FFFFFF;
886 uint32 fogFct = 0xFF-(uint8)((g_fFogCoord[dwV]-gRSPfFogMin)*gRSPfFogDivider);
887 v.dcSpecular |= (fogFct<<24);
888 }
889 }
890 else if( gRSP.bFogEnabled )
891 {
892 uint32 fogFct = 0xFF-(uint8)((g_fFogCoord[dwV]-gRSPfFogMin)*gRSPfFogDivider);
893 v.dcSpecular = (fogFct<<24);
894 }
895 }
896 VTX_DUMP(TRACE2(" (U,V): %f, %f", g_fVtxTxtCoords[dwV].x,g_fVtxTxtCoords[dwV].y));
897
898 v.dcDiffuse = g_dwVtxDifColor[dwV];
899 if( gRDP.otherMode.key_en )
900 {
901 v.dcDiffuse &= 0x00FFFFFF;
902 v.dcDiffuse |= (gRDP.keyA<<24);
903 }
904 else if( gRDP.otherMode.aa_en && gRDP.otherMode.clr_on_cvg==0 )
905 {
906 v.dcDiffuse |= 0xFF000000;
907 }
908
909 if( gRSP.bProcessDiffuseColor )
910 {
911 v.dcDiffuse = CRender::g_pRender->PostProcessDiffuseColor(v.dcDiffuse);
912 }
913 if( options.bWinFrameMode )
914 {
915 v.dcDiffuse = g_dwVtxDifColor[dwV];
916 }
917
918 if( openGL )
919 {
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;
924 }
925
926 if( bTexture )
927 {
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)
931 {
932 // Correction for texGen result
933 float u0,u1,v0,v1;
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);
939
940 if( CRender::g_pRender->IsTexel1Enable() )
941 {
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);
948 }
949 else
950 {
951 CRender::g_pRender->SetVertexTextureUVCoord(v, u0, v0);
952 }
953 }
954 else
955 {
956 float tex0u = g_fVtxTxtCoords[dwV].x *gRSP.tex0scaleX - gRSP.tex0OffsetX ;
957 float tex0v = g_fVtxTxtCoords[dwV].y *gRSP.tex0scaleY - gRSP.tex0OffsetY ;
958
959 if( CRender::g_pRender->IsTexel1Enable() )
960 {
961 float tex1u = g_fVtxTxtCoords[dwV].x *gRSP.tex1scaleX - gRSP.tex1OffsetX ;
962 float tex1v = g_fVtxTxtCoords[dwV].y *gRSP.tex1scaleY - gRSP.tex1OffsetY ;
963
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));
967 }
968 else
969 {
970 CRender::g_pRender->SetVertexTextureUVCoord(v, tex0u, tex0v);
971 VTX_DUMP(TRACE2(" (tex0): %f, %f", tex0u,tex0v));
972 }
973 }
974
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 ) )
978 {
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 )
982 {
983 bHalfTxtScale=true;
984 }
985 }
986 }
987
988 if( g_curRomInfo.bEnableTxtLOD && vtxIndex == 1 && gRDP.otherMode.text_lod )
989 {
990 if( CRender::g_pRender->IsTexel1Enable() && CRender::g_pRender->m_pColorCombiner->m_pDecodedMux->isUsed(MUX_LODFRAC) )
991 {
992 ComputeLOD(openGL);
993 }
994 else
995 {
996 gRDP.LODFrac = 0;
997 }
998 }
999
1000 VTX_DUMP(TRACE2(" DIF(%08X), SPE(%08X)", v.dcDiffuse, v.dcSpecular));
1001 VTX_DUMP(TRACE0(""));
1002}
1003
1004uint32 LightVert(XVECTOR4 & norm, int vidx)
1005{
1006 float fCosT;
1007
1008 // Do ambient
1009 register float r = gRSP.fAmbientLightR;
1010 register float g = gRSP.fAmbientLightG;
1011 register float b = gRSP.fAmbientLightB;
1012
1013 if( options.enableHackForGames != HACK_FOR_ZELDA_MM )
1014 {
1015 for (register unsigned int l=0; l < gRSPnumLights; l++)
1016 {
1017 fCosT = norm.x*gRSPlights[l].x + norm.y*gRSPlights[l].y + norm.z*gRSPlights[l].z;
1018
1019 if (fCosT > 0 )
1020 {
1021 r += gRSPlights[l].fr * fCosT;
1022 g += gRSPlights[l].fg * fCosT;
1023 b += gRSPlights[l].fb * fCosT;
1024 }
1025 }
1026 }
1027 else
1028 {
1029 XVECTOR4 v;
1030 bool transformed = false;
1031
1032 for (register unsigned int l=0; l < gRSPnumLights; l++)
1033 {
1034 if( gRSPlights[l].range == 0 )
1035 {
1036 // Regular directional light
1037 fCosT = norm.x*gRSPlights[l].x + norm.y*gRSPlights[l].y + norm.z*gRSPlights[l].z;
1038
1039 if (fCosT > 0 )
1040 {
1041 r += gRSPlights[l].fr * fCosT;
1042 g += gRSPlights[l].fg * fCosT;
1043 b += gRSPlights[l].fb * fCosT;
1044 }
1045 }
1046 else //if( (gRSPlights[l].col&0x00FFFFFF) != 0x00FFFFFF )
1047 {
1048 // Point light
1049 if( !transformed )
1050 {
1051 Vec3Transform(&v, (XVECTOR3*)&g_vtxNonTransformed[vidx], &gRSPmodelViewTop); // Convert to w=1
1052 transformed = true;
1053 }
1054
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);
1058 dir.x /= d2;
1059 dir.y /= d2;
1060 dir.z /= d2;
1061
1062 fCosT = norm.x*dir.x + norm.y*dir.y + norm.z*dir.z;
1063
1064 if (fCosT > 0 )
1065 {
1066 //float f = d2/gRSPlights[l].range*50;
1067 float f = d2/15000*50;
1068 f = 1 - min(f,1);
1069 fCosT *= f*f;
1070
1071 r += gRSPlights[l].fr * fCosT;
1072 g += gRSPlights[l].fg * fCosT;
1073 b += gRSPlights[l].fb * fCosT;
1074 }
1075 }
1076 }
1077 }
1078
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));
1083}
1084
1085uint32 LightVertNew(XVECTOR4 & norm)
1086{
1087 float fCosT;
1088
1089 // Do ambient
1090 register float r = gRSP.fAmbientLightR;
1091 register float g = gRSP.fAmbientLightG;
1092 register float b = gRSP.fAmbientLightB;
1093
1094
1095 for (register unsigned int l=0; l < gRSPnumLights; l++)
1096 {
1097 fCosT = norm.x*gRSPlights[l].tx + norm.y*gRSPlights[l].ty + norm.z*gRSPlights[l].tz;
1098
1099 if (fCosT > 0 )
1100 {
1101 r += gRSPlights[l].fr * fCosT;
1102 g += gRSPlights[l].fg * fCosT;
1103 b += gRSPlights[l].fb * fCosT;
1104 }
1105 }
1106
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));
1111}
1112
1113
1114float zero = 0.0f;
1115float onef = 1.0f;
1116float fcosT;
1117
1118#if !defined(__GNUC__) && !defined(NO_ASM)
1119__declspec( naked ) uint32 __fastcall SSELightVert()
1120{
1121 __asm
1122 {
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
1125
1126 mov ecx, 0;
1127loopback:
1128 cmp ecx, DWORD PTR gRSPnumLights;
1129 jae breakout;
1130 mov eax,ecx;
1131 imul eax,0x44;
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
1135
1136 movhlps xmm0,xmm5;
1137 addps xmm0,xmm5;
1138 shufps xmm5,xmm0,0x01;
1139 addps xmm0,xmm5;
1140
1141 comiss xmm0,zero;
1142 jc endloop
1143
1144 shufps xmm0,xmm0,0; // fcosT
1145 mulps xmm1,xmm0;
1146 addps xmm3,xmm1;
1147endloop:
1148 inc ecx;
1149 jmp loopback;
1150breakout:
1151
1152 movss xmm0,DWORD PTR real255;
1153 shufps xmm0,xmm0,0;
1154 minps xmm0,xmm3;
1155
1156 // Without using a memory
1157 cvtss2si eax,xmm0; // move the 1st uint32 to eax
1158 shl eax,10h;
1159 or eax,0FF000000h;
1160 shufps xmm0,xmm0,0E5h; // move the 2nd uint32 to the 1st uint32
1161 cvtss2si ecx,xmm0; // move the 1st uint32 to ecx
1162 shl ecx,8;
1163 or eax,ecx;
1164 shufps xmm0,xmm0,0E6h; // Move the 3rd uint32 to the 1st uint32
1165 cvtss2si ecx,xmm0;
1166 or eax,ecx;
1167
1168 ret;
1169 }
1170}
1171#elif defined(__GNUC__) && defined(__x86_64__) && !defined(NO_ASM)
1172uint32 SSELightVert(void)
1173{
1174 uint32 rval;
1175 float f255 = 255.0, fZero = 0.0;
1176
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"
1180 "0: \n"
1181 " cmpl %3, %%ecx \n"
1182 " jae 2f \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"
1193 " jc 1f \n"
1194 " shufps $0x00, %%xmm0, %%xmm0 \n"
1195 " mulps %%xmm0, %%xmm1 \n"
1196 " addps %%xmm1, %%xmm3 \n"
1197 "1: \n"
1198 " inc %%rcx \n"
1199 " jmp 0b \n"
1200 "2: \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"
1214 : "=&a"(rval)
1215 : "m"(gRSP), "m"(g_normal), "m"(gRSPnumLights), "r"(gRSPlights), "m"(f255), "m"(fZero)
1216 : "%rcx", "memory", "cc", "%xmm0", "%xmm1", "%xmm3", "%xmm4", "%xmm5"
1217 );
1218 return rval;
1219}
1220#elif !defined(NO_ASM) // 32-bit GCC assumed
1221uint32 SSELightVert(void)
1222{
1223 uint32 rval;
1224 float f255 = 255.0, fZero = 0.0;
1225
1226 asm volatile(" movaps %1, %%xmm3 \n"
1227 " movaps %2, %%xmm4 \n"
1228 " xor %%ecx, %%ecx \n"
1229 "0: \n"
1230 " cmpl %3, %%ecx \n"
1231 " jae 2f \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"
1242 " jc 1f \n"
1243 " shufps $0x00, %%xmm0, %%xmm0 \n"
1244 " mulps %%xmm0, %%xmm1 \n"
1245 " addps %%xmm1, %%xmm3 \n"
1246 "1: \n"
1247 " inc %%ecx \n"
1248 " jmp 0b \n"
1249 "2: \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"
1263 : "=&a"(rval)
1264 : "m"(gRSP), "m"(g_normal), "m"(gRSPnumLights), "r"(gRSPlights), "m"(f255), "m"(fZero)
1265 : "%rcx", "memory", "cc", "%xmm0", "%xmm1", "%xmm3", "%xmm4", "%xmm5"
1266 );
1267 return rval;
1268}
1269#endif
1270
1271inline void ReplaceAlphaWithFogFactor(int i)
1272{
1273 if( gRDP.geometryMode & G_FOG )
1274 {
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;
1280 else
1281 *(((uint8*)&(g_dwVtxDifColor[i]))+3) = (uint8)(g_vecProjected[i].z*255);
1282 }
1283}
1284
1285
1286// Bits
1287// +-+-+-
1288// xxyyzz
1289#define Z_NEG 0x01
1290#define Z_POS 0x02
1291#define Y_NEG 0x04
1292#define Y_POS 0x08
1293#define X_NEG 0x10
1294#define X_POS 0x20
1295
1296// Assumes dwAddr has already been checked!
1297// Don't inline - it's too big with the transform macros
1298
1299#if !defined(NO_ASM)
1300void ProcessVertexDataSSE(uint32 dwAddr, uint32 dwV0, uint32 dwNum)
1301{
1302 UpdateCombinedMatrix();
1303
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
1309
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
1315
1316 FiddledVtx * pVtxBase = (FiddledVtx*)(g_pRDRAMu8 + dwAddr);
1317 g_pVtxBase = pVtxBase;
1318
1319 for (uint32 i = dwV0; i < dwV0 + dwNum; i++)
1320 {
1321 SP_Timing(RSP_GBI0_Vtx);
1322
1323 FiddledVtx & vert = pVtxBase[i - dwV0];
1324
1325 g_vtxNonTransformed[i].x = (float)vert.x;
1326 g_vtxNonTransformed[i].y = (float)vert.y;
1327 g_vtxNonTransformed[i].z = (float)vert.z;
1328
1329 SSEVec3Transform(i);
1330
1331 if( gRSP.bFogEnabled )
1332 {
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;
1336 }
1337
1338 ReplaceAlphaWithFogFactor(i);
1339
1340
1341 VTX_DUMP(
1342 {
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);
1349 });
1350
1351 RSP_Vtx_Clipping(i);
1352
1353 if( gRSP.bLightingEnable )
1354 {
1355 g_normal.x = (float)vert.norma.nx;
1356 g_normal.y = (float)vert.norma.ny;
1357 g_normal.z = (float)vert.norma.nz;
1358
1359 SSEVec3TransformNormal();
1360 if( options.enableHackForGames != HACK_FOR_ZELDA_MM )
1361 g_dwVtxDifColor[i] = SSELightVert();
1362 else
1363 g_dwVtxDifColor[i] = LightVert(g_normal, i);
1364 *(((uint8*)&(g_dwVtxDifColor[i]))+3) = vert.rgba.a; // still use alpha from the vertex
1365 }
1366 else
1367 {
1368 if( (gRDP.geometryMode & G_SHADE) == 0 && gRSP.ucode < 5 ) //Shade is disabled
1369 {
1370 //FLAT shade
1371 g_dwVtxDifColor[i] = gRDP.primitiveColor;
1372 }
1373 else
1374 {
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;
1380 }
1381 }
1382
1383 if( options.bWinFrameMode )
1384 {
1385 g_dwVtxDifColor[i] = COLOR_RGBA(vert.rgba.r, vert.rgba.g, vert.rgba.b, vert.rgba.a);
1386 }
1387
1388 // Update texture coords n.b. need to divide tu/tv by bogus scale on addition to buffer
1389
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 )
1393 {
1394 TexGen(g_fVtxTxtCoords[i].x, g_fVtxTxtCoords[i].y);
1395 }
1396 else
1397 {
1398 g_fVtxTxtCoords[i].x = (float)vert.tu;
1399 g_fVtxTxtCoords[i].y = (float)vert.tv;
1400 }
1401 }
1402
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");});
1405}
1406#endif
1407
1408void ProcessVertexDataNoSSE(uint32 dwAddr, uint32 dwV0, uint32 dwNum)
1409{
1410
1411 UpdateCombinedMatrix();
1412
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
1418
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
1424
1425 FiddledVtx * pVtxBase = (FiddledVtx*)(g_pRDRAMu8 + dwAddr);
1426 g_pVtxBase = pVtxBase;
1427
1428 for (uint32 i = dwV0; i < dwV0 + dwNum; i++)
1429 {
1430 SP_Timing(RSP_GBI0_Vtx);
1431
1432 FiddledVtx & vert = pVtxBase[i - dwV0];
1433
1434 g_vtxNonTransformed[i].x = (float)vert.x;
1435 g_vtxNonTransformed[i].y = (float)vert.y;
1436 g_vtxNonTransformed[i].z = (float)vert.z;
1437
1438 Vec3Transform(&g_vtxTransformed[i], (XVECTOR3*)&g_vtxNonTransformed[i], &gRSPworldProject); // Convert to w=1
1439
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 )
1444 {
1445 g_vecProjected[i].z = gRDP.fPrimitiveDepth;
1446 g_vtxTransformed[i].z = gRDP.fPrimitiveDepth*g_vtxTransformed[i].w;
1447 }
1448 else
1449 {
1450 g_vecProjected[i].z = g_vtxTransformed[i].z * g_vecProjected[i].w;
1451 }
1452
1453 if( gRSP.bFogEnabled )
1454 {
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;
1458 }
1459
1460 VTX_DUMP(
1461 {
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);
1468 });
1469
1470 RSP_Vtx_Clipping(i);
1471
1472 if( gRSP.bLightingEnable )
1473 {
1474 g_normal.x = (float)vert.norma.nx;
1475 g_normal.y = (float)vert.norma.ny;
1476 g_normal.z = (float)vert.norma.nz;
1477
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
1481 }
1482 else
1483 {
1484 if( (gRDP.geometryMode & G_SHADE) == 0 && gRSP.ucode < 5 ) //Shade is disabled
1485 {
1486 //FLAT shade
1487 g_dwVtxDifColor[i] = gRDP.primitiveColor;
1488 }
1489 else
1490 {
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;
1496 }
1497 }
1498
1499 if( options.bWinFrameMode )
1500 {
1501 g_dwVtxDifColor[i] = COLOR_RGBA(vert.rgba.r, vert.rgba.g, vert.rgba.b, vert.rgba.a);
1502 }
1503
1504 ReplaceAlphaWithFogFactor(i);
1505
1506 // Update texture coords n.b. need to divide tu/tv by bogus scale on addition to buffer
1507
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 )
1511 {
1512 TexGen(g_fVtxTxtCoords[i].x, g_fVtxTxtCoords[i].y);
1513 }
1514 else
1515 {
1516 g_fVtxTxtCoords[i].x = (float)vert.tu;
1517 g_fVtxTxtCoords[i].y = (float)vert.tv;
1518 }
1519 }
1520
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");});
1523}
1524
1525bool PrepareTriangle(uint32 dwV0, uint32 dwV1, uint32 dwV2)
1526{
1527 if( status.isVertexShaderEnabled || status.bUseHW_T_L )
1528 {
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)));
1534 }
1535 else
1536 {
1537 SP_Timing(SP_Each_Triangle);
1538
1539 bool textureFlag = (CRender::g_pRender->IsTextureEnabled() || gRSP.ucode == 6 );
1540 bool openGL = CDeviceBuilder::m_deviceGeneralType == OGL_DEVICE;
1541
1542 InitVertex(dwV0, gRSP.numVertices, textureFlag, openGL);
1543 InitVertex(dwV1, gRSP.numVertices+1, textureFlag, openGL);
1544 InitVertex(dwV2, gRSP.numVertices+2, textureFlag, openGL);
1545
1546 gRSP.numVertices += 3;
1547 status.dwNumTrisRendered++;
1548 }
1549
1550 return true;
1551}
1552
1553
1554
1555// Returns TRUE if it thinks the triangle is visible
1556// Returns FALSE if it is clipped
1557bool IsTriangleVisible(uint32 dwV0, uint32 dwV1, uint32 dwV2)
1558{
1559 //return true; //fix me
1560
1561 if( status.isVertexShaderEnabled || status.bUseHW_T_L ) return true; // We won't have access to transformed vertex data
1562
1563 DEBUGGER_ONLY_IF( (!debuggerEnableTestTris || !debuggerEnableCullFace), {return TRUE;});
1564
1565#ifdef DEBUGGER
1566 // Check vertices are valid!
1567 if (dwV0 >= MAX_VERTS || dwV1 >= MAX_VERTS || dwV2 >= MAX_VERTS)
1568 return false;
1569#endif
1570
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)
1576 {
1577 XVECTOR4 & v0 = g_vecProjected[dwV0];
1578 XVECTOR4 & v1 = g_vecProjected[dwV1];
1579 XVECTOR4 & v2 = g_vecProjected[dwV2];
1580
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)
1584 {
1585 float V1 = v2.x - v0.x;
1586 float V2 = v2.y - v0.y;
1587
1588 float W1 = v2.x - v1.x;
1589 float W2 = v2.y - v1.y;
1590
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;
1594
1595 if (fDirection < 0 && gRSP.bCullBack)
1596 {
1597 status.dwNumTrisClipped++;
1598 return false;
1599 }
1600 else if (fDirection > 0 && gRSP.bCullFront)
1601 {
1602 status.dwNumTrisClipped++;
1603 return false;
1604 }
1605 }
1606 }
1607
1608#ifdef ENABLE_CLIP_TRI
1609 //if( gRSP.bRejectVtx && (g_clipFlag[dwV0]|g_clipFlag[dwV1]|g_clipFlag[dwV2]) )
1610 // return;
1611 if( g_clipFlag2[dwV0]&g_clipFlag2[dwV1]&g_clipFlag2[dwV2] )
1612 {
1613 //DebuggerAppendMsg("Clipped");
1614 return false;
1615 }
1616#endif
1617
1618 return true;
1619}
1620
1621
1622void SetPrimitiveColor(uint32 dwCol, uint32 LODMin, uint32 LODFrac)
1623{
1624 gRDP.colorsAreReloaded = true;
1625 gRDP.primitiveColor = dwCol;
1626 gRDP.primLODMin = LODMin;
1627 gRDP.primLODFrac = LODFrac;
1628 if( gRDP.primLODFrac < gRDP.primLODMin )
1629 {
1630 gRDP.primLODFrac = gRDP.primLODMin;
1631 }
1632
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
1637}
1638
1639void SetPrimitiveDepth(uint32 z, uint32 dwDZ)
1640{
1641 gRDP.primitiveDepth = z & 0x7FFF;
1642 gRDP.fPrimitiveDepth = (float)(gRDP.primitiveDepth)/(float)0x8000;
1643
1644 //gRDP.fPrimitiveDepth = gRDP.fPrimitiveDepth*2-1;
1645 /*
1646 z=0xFFFF -> 1 the farest
1647 z=0 -> -1 the nearest
1648 */
1649
1650 //how to use dwDZ?
1651
1652#ifdef DEBUGGER
1653 if( (pauseAtNext && (eventToPause == NEXT_VERTEX_CMD || eventToPause == NEXT_FLUSH_TRI )) )//&& logTriangles )
1654 {
1655 DebuggerAppendMsg("Set prim Depth: %f, (%08X, %08X)", gRDP.fPrimitiveDepth, z, dwDZ);
1656 }
1657#endif
1658}
1659
1660void SetVertexXYZ(uint32 vertex, float x, float y, float z)
1661{
1662 g_vecProjected[vertex].x = x;
1663 g_vecProjected[vertex].y = y;
1664 g_vecProjected[vertex].z = z;
1665
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;
1669}
1670
1671void ModifyVertexInfo(uint32 where, uint32 vertex, uint32 val)
1672{
1673 switch (where)
1674 {
1675 case RSP_MV_WORD_OFFSET_POINT_RGBA: // Modify RGBA
1676 {
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]);
1683 }
1684 break;
1685 case RSP_MV_WORD_OFFSET_POINT_XYSCREEN: // Modify X,Y
1686 {
1687 uint16 nX = (uint16)(val>>16);
1688 short x = *((short*)&nX);
1689 x /= 4;
1690
1691 uint16 nY = (uint16)(val&0xFFFF);
1692 short y = *((short*)&nY);
1693 y /= 4;
1694
1695 // Should do viewport transform.
1696
1697
1698 x -= windowSetting.uViWidth/2;
1699 y = windowSetting.uViHeight/2-y;
1700
1701 if( options.bEnableHacks && ((*g_GraphicsInfo.VI_X_SCALE_REG)&0xF) != 0 )
1702 {
1703 // Tarzan
1704 // I don't know why Tarzan is different
1705 SetVertexXYZ(vertex, x/windowSetting.fViWidth, y/windowSetting.fViHeight, g_vecProjected[vertex].z);
1706 }
1707 else
1708 {
1709 // Toy Story 2 and other games
1710 SetVertexXYZ(vertex, x*2/windowSetting.fViWidth, y*2/windowSetting.fViHeight, g_vecProjected[vertex].z);
1711 }
1712
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));
1715 }
1716 break;
1717 case RSP_MV_WORD_OFFSET_POINT_ZSCREEN: // Modify C
1718 {
1719 int z = val>>16;
1720
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));
1724 }
1725 break;
1726 case RSP_MV_WORD_OFFSET_POINT_ST: // Texture
1727 {
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);
1734 }
1735 break;
1736 }
1737 DEBUGGER_PAUSE_AND_DUMP(NEXT_VERTEX_CMD,{TRACE0("Paused at ModVertex Cmd");});
1738}
1739
1740void ProcessVertexDataDKR(uint32 dwAddr, uint32 dwV0, uint32 dwNum)
1741{
1742 UpdateCombinedMatrix();
1743
1744 long long pVtxBase = (long long) (g_pRDRAMu8 + dwAddr);
1745 g_pVtxBase = (FiddledVtx*)pVtxBase;
1746
1747 Matrix &matWorldProject = gRSP.DKRMatrixes[gRSP.DKRCMatrixIndex];
1748
1749 int nOff;
1750
1751 bool addbase=false;
1752 if ((!gRSP.DKRBillBoard) || (gRSP.DKRCMatrixIndex != 2) )
1753 addbase = false;
1754 else
1755 addbase = true;
1756
1757 if( addbase && gRSP.DKRVtxCount == 0 && dwNum > 1 )
1758 {
1759 gRSP.DKRVtxCount++;
1760 }
1761
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"));
1764
1765 nOff = 0;
1766 uint32 end = dwV0 + dwNum;
1767 for (uint32 i = dwV0; i < end; i++)
1768 {
1769 XVECTOR3 w;
1770
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);
1774
1775 //if( status.isSSEEnabled )
1776 // SSEVec3TransformDKR(g_vtxTransformed[i], g_vtxNonTransformed[i]);
1777 //else
1778 Vec3Transform(&g_vtxTransformed[i], (XVECTOR3*)&g_vtxNonTransformed[i], &matWorldProject); // Convert to w=1
1779
1780 if( gRSP.DKRVtxCount == 0 && dwNum==1 )
1781 {
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;
1786 }
1787 else if( addbase )
1788 {
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;
1793 }
1794
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;
1799
1800 gRSP.DKRVtxCount++;
1801
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));
1804
1805 if( gRSP.bFogEnabled )
1806 {
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;
1810 }
1811
1812 RSP_Vtx_Clipping(i);
1813
1814 short wA = *(short*)((pVtxBase+nOff + 6) ^ 2);
1815 short wB = *(short*)((pVtxBase+nOff + 8) ^ 2);
1816
1817 s8 r = (s8)(wA >> 8);
1818 s8 g = (s8)(wA);
1819 s8 b = (s8)(wB >> 8);
1820 s8 a = (s8)(wB);
1821
1822 if (gRSP.bLightingEnable)
1823 {
1824 g_normal.x = (char)r; //norma.nx;
1825 g_normal.y = (char)g; //norma.ny;
1826 g_normal.z = (char)b; //norma.nz;
1827
1828 Vec3TransformNormal(g_normal, matWorldProject)
1829#if !defined(NO_ASM)
1830 if( status.isSSEEnabled )
1831 g_dwVtxDifColor[i] = SSELightVert();
1832 else
1833#endif
1834 g_dwVtxDifColor[i] = LightVert(g_normal, i);
1835 }
1836 else
1837 {
1838 int nR, nG, nB, nA;
1839
1840 nR = r;
1841 nG = g;
1842 nB = b;
1843 nA = a;
1844 // Assign true vert colour after lighting/fogging
1845 g_dwVtxDifColor[i] = COLOR_RGBA(nR, nG, nB, nA);
1846 }
1847
1848 ReplaceAlphaWithFogFactor(i);
1849
1850 g_fVtxTxtCoords[i].x = g_fVtxTxtCoords[i].y = 1;
1851
1852 nOff += 10;
1853 }
1854
1855
1856 DEBUGGER_PAUSE_AND_DUMP(NEXT_VERTEX_CMD,{DebuggerAppendMsg("Paused at DKR Vertex Cmd, v0=%d, vn=%d, addr=%08X", dwV0, dwNum, dwAddr);});
1857}
1858
1859
1860extern uint32 dwPDCIAddr;
1861void ProcessVertexDataPD(uint32 dwAddr, uint32 dwV0, uint32 dwNum)
1862{
1863 UpdateCombinedMatrix();
1864
1865 N64VtxPD * pVtxBase = (N64VtxPD*)(g_pRDRAMu8 + dwAddr);
1866 g_pVtxBase = (FiddledVtx*)pVtxBase; // Fix me
1867
1868 for (uint32 i = dwV0; i < dwV0 + dwNum; i++)
1869 {
1870 N64VtxPD &vert = pVtxBase[i - dwV0];
1871
1872 g_vtxNonTransformed[i].x = (float)vert.x;
1873 g_vtxNonTransformed[i].y = (float)vert.y;
1874 g_vtxNonTransformed[i].z = (float)vert.z;
1875
1876#if !defined(NO_ASM)
1877 if( status.isSSEEnabled )
1878 SSEVec3Transform(i);
1879 else
1880#endif
1881 {
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;
1887 }
1888
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;
1892
1893 RSP_Vtx_Clipping(i);
1894
1895 uint8 *addr = g_pRDRAMu8+dwPDCIAddr+ (vert.cidx&0xFF);
1896 uint32 a = addr[0];
1897 uint32 r = addr[3];
1898 uint32 g = addr[2];
1899 uint32 b = addr[1];
1900
1901 if( gRSP.bLightingEnable )
1902 {
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 )
1908 {
1909 SSEVec3TransformNormal();
1910 g_dwVtxDifColor[i] = SSELightVert();
1911 }
1912 else
1913#endif
1914 {
1915 Vec3TransformNormal(g_normal, gRSPmodelViewTop);
1916 g_dwVtxDifColor[i] = LightVert(g_normal, i);
1917 }
1918 *(((uint8*)&(g_dwVtxDifColor[i]))+3) = (uint8)a; // still use alpha from the vertex
1919 }
1920 else
1921 {
1922 if( (gRDP.geometryMode & G_SHADE) == 0 && gRSP.ucode < 5 ) //Shade is disabled
1923 {
1924 g_dwVtxDifColor[i] = gRDP.primitiveColor;
1925 }
1926 else //FLAT shade
1927 {
1928 g_dwVtxDifColor[i] = COLOR_RGBA(r, g, b, a);
1929 }
1930 }
1931
1932 if( options.bWinFrameMode )
1933 {
1934 g_dwVtxDifColor[i] = COLOR_RGBA(r, g, b, a);
1935 }
1936
1937 ReplaceAlphaWithFogFactor(i);
1938
1939 VECTOR2 & t = g_fVtxTxtCoords[i];
1940 if (gRSP.bTextureGen && gRSP.bLightingEnable )
1941 {
1942 // Not sure if we should transform the normal here
1943 //Matrix & matWV = gRSP.projectionMtxs[gRSP.projectionMtxTop];
1944 //Vec3TransformNormal(g_normal, matWV);
1945
1946 TexGen(g_fVtxTxtCoords[i].x, g_fVtxTxtCoords[i].y);
1947 }
1948 else
1949 {
1950 t.x = vert.s;
1951 t.y = vert.t;
1952 }
1953
1954
1955 VTX_DUMP(
1956 {
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);
1962 });
1963 }
1964
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");});
1967}
1968
1969extern uint32 dwConkerVtxZAddr;
1970void ProcessVertexDataConker(uint32 dwAddr, uint32 dwV0, uint32 dwNum)
1971{
1972 UpdateCombinedMatrix();
1973
1974 FiddledVtx * pVtxBase = (FiddledVtx*)(g_pRDRAMu8 + dwAddr);
1975 g_pVtxBase = pVtxBase;
1976 //short *vertexColoraddr = (short*)(g_pRDRAMu8+dwConkerVtxZAddr);
1977
1978 for (uint32 i = dwV0; i < dwV0 + dwNum; i++)
1979 {
1980 SP_Timing(RSP_GBI0_Vtx);
1981
1982 FiddledVtx & vert = pVtxBase[i - dwV0];
1983
1984 g_vtxNonTransformed[i].x = (float)vert.x;
1985 g_vtxNonTransformed[i].y = (float)vert.y;
1986 g_vtxNonTransformed[i].z = (float)vert.z;
1987
1988#if !defined(NO_ASM)
1989 if( status.isSSEEnabled )
1990 SSEVec3Transform(i);
1991 else
1992#endif
1993 {
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;
1999 }
2000
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;
2004
2005 VTX_DUMP(
2006 {
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);
2013 });
2014
2015 RSP_Vtx_Clipping(i);
2016
2017 if( gRSP.bLightingEnable )
2018 {
2019 {
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++)
2024 {
2025 r += gRSPlights[k].r;
2026 g += gRSPlights[k].g;
2027 b += gRSPlights[k].b;
2028 }
2029 if( r>255 ) r=255;
2030 if( g>255 ) g=255;
2031 if( b>255 ) b=255;
2032 r *= vert.rgba.r ;
2033 g *= vert.rgba.g ;
2034 b *= vert.rgba.b ;
2035 r >>= 8;
2036 g >>= 8;
2037 b >>= 8;
2038 g_dwVtxDifColor[i] = 0xFF000000;
2039 g_dwVtxDifColor[i] |= (r<<16);
2040 g_dwVtxDifColor[i] |= (g<< 8);
2041 g_dwVtxDifColor[i] |= (b );
2042 }
2043
2044 *(((uint8*)&(g_dwVtxDifColor[i]))+3) = vert.rgba.a; // still use alpha from the vertex
2045 }
2046 else
2047 {
2048 if( (gRDP.geometryMode & G_SHADE) == 0 && gRSP.ucode < 5 ) //Shade is disabled
2049 {
2050 g_dwVtxDifColor[i] = gRDP.primitiveColor;
2051 }
2052 else //FLAT shade
2053 {
2054 g_dwVtxDifColor[i] = COLOR_RGBA(vert.rgba.r, vert.rgba.g, vert.rgba.b, vert.rgba.a);
2055 }
2056 }
2057
2058 if( options.bWinFrameMode )
2059 {
2060 //g_vecProjected[i].z = 0;
2061 g_dwVtxDifColor[i] = COLOR_RGBA(vert.rgba.r, vert.rgba.g, vert.rgba.b, vert.rgba.a);
2062 }
2063
2064 ReplaceAlphaWithFogFactor(i);
2065
2066 // Update texture coords n.b. need to divide tu/tv by bogus scale on addition to buffer
2067 //VECTOR2 & t = g_fVtxTxtCoords[i];
2068
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 )
2072 {
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);
2078 }
2079 else
2080 {
2081 g_fVtxTxtCoords[i].x = (float)vert.tu;
2082 g_fVtxTxtCoords[i].y = (float)vert.tv;
2083 }
2084 }
2085
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");});
2088}
2089
2090
2091typedef struct{
2092 short y;
2093 short x;
2094 short flag;
2095 short z;
2096} RS_Vtx_XYZ;
2097
2098typedef union {
2099 struct {
2100 uint8 a;
2101 uint8 b;
2102 uint8 g;
2103 uint8 r;
2104 };
2105 struct {
2106 char na; //a
2107 char nz; //b
2108 char ny; //g
2109 char nx; //r
2110 };
2111} RS_Vtx_Color;
2112
2113
2114void ProcessVertexData_Rogue_Squadron(uint32 dwXYZAddr, uint32 dwColorAddr, uint32 dwXYZCmd, uint32 dwColorCmd)
2115{
2116 UpdateCombinedMatrix();
2117
2118 uint32 dwV0 = 0;
2119 uint32 dwNum = (dwXYZCmd&0xFF00)>>10;
2120
2121 RS_Vtx_XYZ * pVtxXYZBase = (RS_Vtx_XYZ*)(g_pRDRAMu8 + dwXYZAddr);
2122 RS_Vtx_Color * pVtxColorBase = (RS_Vtx_Color*)(g_pRDRAMu8 + dwColorAddr);
2123
2124 uint32 i;
2125 for (i = dwV0; i < dwV0 + dwNum; i++)
2126 {
2127 RS_Vtx_XYZ & vertxyz = pVtxXYZBase[i - dwV0];
2128 RS_Vtx_Color & vertcolors = pVtxColorBase[i - dwV0];
2129
2130 g_vtxNonTransformed[i].x = (float)vertxyz.x;
2131 g_vtxNonTransformed[i].y = (float)vertxyz.y;
2132 g_vtxNonTransformed[i].z = (float)vertxyz.z;
2133
2134#if !defined(NO_ASM)
2135 if( status.isSSEEnabled )
2136 SSEVec3Transform(i);
2137 else
2138#endif
2139 {
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;
2145 }
2146
2147 VTX_DUMP(
2148 {
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);
2153 });
2154
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;
2158
2159 RSP_Vtx_Clipping(i);
2160
2161 if( gRSP.bLightingEnable )
2162 {
2163 g_normal.x = (float)vertcolors.nx;
2164 g_normal.y = (float)vertcolors.ny;
2165 g_normal.z = (float)vertcolors.nz;
2166
2167#if !defined(NO_ASM)
2168 if( status.isSSEEnabled )
2169 {
2170 SSEVec3TransformNormal();
2171 g_dwVtxDifColor[i] = SSELightVert();
2172 }
2173 else
2174#endif
2175 {
2176 Vec3TransformNormal(g_normal, gRSPmodelViewTop);
2177 g_dwVtxDifColor[i] = LightVert(g_normal, i);
2178 }
2179 *(((uint8*)&(g_dwVtxDifColor[i]))+3) = vertcolors.a; // still use alpha from the vertex
2180 }
2181 else
2182 {
2183 if( (gRDP.geometryMode & G_SHADE) == 0 && gRSP.ucode < 5 ) //Shade is disabled
2184 {
2185 g_dwVtxDifColor[i] = gRDP.primitiveColor;
2186 }
2187 else //FLAT shade
2188 {
2189 g_dwVtxDifColor[i] = COLOR_RGBA(vertcolors.r, vertcolors.g, vertcolors.b, vertcolors.a);
2190 }
2191 }
2192
2193 if( options.bWinFrameMode )
2194 {
2195 g_dwVtxDifColor[i] = COLOR_RGBA(vertcolors.r, vertcolors.g, vertcolors.b, vertcolors.a);
2196 }
2197
2198 ReplaceAlphaWithFogFactor(i);
2199
2200 /*
2201 // Update texture coords n.b. need to divide tu/tv by bogus scale on addition to buffer
2202 VECTOR2 & t = g_fVtxTxtCoords[i];
2203
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 )
2207 {
2208 TexGen(g_fVtxTxtCoords[i].x, g_fVtxTxtCoords[i].y);
2209 }
2210 else
2211 {
2212 t.x = (float)vert.tu;
2213 t.y = (float)vert.tv;
2214 }
2215 */
2216 }
2217
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");});
2220}
2221
2222void SetLightCol(uint32 dwLight, uint32 dwCol)
2223{
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
2232
2233 //TRACE1("Set light %d color", dwLight);
2234 LIGHT_DUMP(TRACE2("Set Light %d color: %08X", dwLight, dwCol));
2235}
2236
2237void SetLightDirection(uint32 dwLight, float x, float y, float z, float range)
2238{
2239 //gRSP.bLightIsUpdated = true;
2240
2241 //gRSPlights[dwLight].ox = x;
2242 //gRSPlights[dwLight].oy = y;
2243 //gRSPlights[dwLight].oz = z;
2244
2245 register float w = range == 0 ? (float)sqrt(x*x+y*y+z*z) : 1;
2246
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));
2252}
2253
2254static float maxS0, maxT0;
2255static float maxS1, maxT1;
2256static bool validS0, validT0;
2257static bool validS1, validT1;
2258
2259void LogTextureCoords(float fTex0S, float fTex0T, float fTex1S, float fTex1T)
2260{
2261 if( validS0 )
2262 {
2263 if( fTex0S<0 || fTex0S>maxS0 ) validS0 = false;
2264 }
2265 if( validT0 )
2266 {
2267 if( fTex0T<0 || fTex0T>maxT0 ) validT0 = false;
2268 }
2269 if( validS1 )
2270 {
2271 if( fTex1S<0 || fTex1S>maxS1 ) validS1 = false;
2272 }
2273 if( validT1 )
2274 {
2275 if( fTex1T<0 || fTex1T>maxT1 ) validT1 = false;
2276 }
2277}
2278
2279bool CheckTextureCoords(int tex)
2280{
2281 if( tex==0 )
2282 {
2283 return validS0&&validT0;
2284 }
2285 else
2286 {
2287 return validS1&&validT1;
2288 }
2289}
2290
2291void ResetTextureCoordsLog(float maxs0, float maxt0, float maxs1, float maxt1)
2292{
2293 maxS0 = maxs0;
2294 maxT0 = maxt0;
2295 maxS1 = maxs1;
2296 maxT1 = maxt1;
2297 validS0 = validT0 = true;
2298 validS1 = validT1 = true;
2299}
2300
2301void ForceMainTextureIndex(int dwTile)
2302{
2303 if( dwTile == 1 && !(CRender::g_pRender->IsTexel0Enable()) && CRender::g_pRender->IsTexel1Enable() )
2304 {
2305 // Hack
2306 gRSP.curTile = 0;
2307 }
2308 else
2309 {
2310 gRSP.curTile = dwTile;
2311 }
2312}
2313
2314float HackZ2(float z)
2315{
2316 z = (z+9)/10;
2317 return z;
2318}
2319
2320float HackZ(float z)
2321{
2322 return HackZ2(z);
2323
2324 if( z < 0.1 && z >= 0 )
2325 z = (.1f+z)/2;
2326 else if( z < 0 )
2327 //return (10+z)/100;
2328 z = (expf(z)/20);
2329 return z;
2330}
2331
2332void HackZ(std::vector<XVECTOR3>& points)
2333{
2334 int size = points.size();
2335 for( int i=0; i<size; i++)
2336 {
2337 XVECTOR3 &v = points[i];
2338 v.z = (float)HackZ(v.z);
2339 }
2340}
2341
2342void HackZAll()
2343{
2344 if( CDeviceBuilder::m_deviceGeneralType == DIRECTX_DEVICE )
2345 {
2346 for( uint32 i=0; i<gRSP.numVertices; i++)
2347 {
2348 g_vtxBuffer[i].z = HackZ(g_vtxBuffer[i].z);
2349 }
2350 }
2351 else
2352 {
2353 for( uint32 i=0; i<gRSP.numVertices; i++)
2354 {
2355 float w = g_vtxProjected5[i][3];
2356 g_vtxProjected5[i][2] = HackZ(g_vtxProjected5[i][2]/w)*w;
2357 }
2358 }
2359}
2360
2361
2362extern XMATRIX reverseXY;
2363extern XMATRIX reverseY;
2364
2365void UpdateCombinedMatrix()
2366{
2367 if( gRSP.bMatrixIsUpdated )
2368 {
2369 gRSPworldProject = gRSP.modelviewMtxs[gRSP.modelViewMtxTop] * gRSP.projectionMtxs[gRSP.projectionMtxTop];
2370 gRSP.bMatrixIsUpdated = false;
2371 gRSP.bCombinedMatrixIsUpdated = true;
2372 }
2373
2374 if( gRSP.bCombinedMatrixIsUpdated )
2375 {
2376 if( options.enableHackForGames == HACK_REVERSE_XY_COOR )
2377 {
2378 gRSPworldProject = gRSPworldProject * reverseXY;
2379 }
2380 if( options.enableHackForGames == HACK_REVERSE_Y_COOR )
2381 {
2382 gRSPworldProject = gRSPworldProject * reverseY;
2383 }
2384#if !defined(NO_ASM)
2385 if( status.isSSEEnabled )
2386 {
2387 MatrixTranspose(&gRSPworldProjectTransported, &gRSPworldProject);
2388 }
2389#endif
2390 gRSP.bCombinedMatrixIsUpdated = false;
2391 }
2392
2393 //if( gRSP.bWorldMatrixIsUpdated || gRSP.bLightIsUpdated )
2394 //{
2395 // // Update lights with transported world matrix
2396 // for( unsigned int l=0; l<gRSPnumLights; l++)
2397 // {
2398 // Vec3TransformCoord(&gRSPlights[l].td, &gRSPlights[l].od, &gRSPmodelViewTopTranspose);
2399 // Vec3Normalize(&gRSPlights[l].td,&gRSPlights[l].td);
2400 // }
2401
2402 // gRSP.bWorldMatrixIsUpdated = false;
2403 // gRSP.bLightIsUpdated = false;
2404 //}
2405}
2406