Debian Codegraph @

code/renderergl1/tr_altivec.c

raw symbols SCIP
1 /*
2 ===========================================================================
3 Copyright (C) 1999-2005 Id Software, Inc.
4
5 This file is part of Quake III Arena source code.
6
7 Quake III Arena source code is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
11
12 Quake III Arena source code is distributed in the hope that it will be
13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Quake III Arena source code; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 ===========================================================================
21 */
22
23 /* This file is only compiled for PowerPC builds with Altivec support.
24 Altivec intrinsics need to be in a separate file, so GCC's -maltivec
25 command line can enable them, but give us the option to _not_ use that
26 on other files, where the compiler might then generate Altivec
27 instructions for normal floating point, crashing on G3 (etc) processors. */
28
29 #include "tr_local.h"
30
31 #if idppc_altivec
32
33 #if !defined(__APPLE__)
34 #include <altivec.h>
35 #endif
36
37 void ProjectDlightTexture_altivec( void ) {
38 int i, l;
39 vec_t origin0, origin1, origin2;
40 float texCoords0, texCoords1;
41 vector float floatColorVec0, floatColorVec1;
42 vector float modulateVec, colorVec, zero;
43 vector short colorShort;
44 vector signed int colorInt;
45 vector unsigned char floatColorVecPerm, modulatePerm, colorChar;
46 vector unsigned char vSel = VECCONST_UINT8(0x00, 0x00, 0x00, 0xff,
47 0x00, 0x00, 0x00, 0xff,
48 0x00, 0x00, 0x00, 0xff,
49 0x00, 0x00, 0x00, 0xff);
50 float *texCoords;
51 byte *colors;
52 byte clipBits[SHADER_MAX_VERTEXES];
53 float texCoordsArray[SHADER_MAX_VERTEXES][2];
54 byte colorArray[SHADER_MAX_VERTEXES][4];
55 glIndex_t hitIndexes[SHADER_MAX_INDEXES];
56 int numIndexes;
57 float scale;
58 float radius;
59 vec3_t floatColor;
60 float modulate = 0.0f;
61
62 if ( !backEnd.refdef.num_dlights ) {
63 return;
64 }
65
66 // There has to be a better way to do this so that floatColor
67 // and/or modulate are already 16-byte aligned.
68 floatColorVecPerm = vec_lvsl(0,(float *)floatColor);
69 modulatePerm = vec_lvsl(0,(float *)&modulate);
70 modulatePerm = (vector unsigned char)vec_splat((vector unsigned int)modulatePerm,0);
71 zero = (vector float)vec_splat_s8(0);
72
73 for ( l = 0 ; l < backEnd.refdef.num_dlights ; l++ ) {
74 dlight_t *dl;
75
76 if ( !( tess.dlightBits & ( 1 << l ) ) ) {
77 continue; // this surface definitely doesn't have any of this light
78 }
79 texCoords = texCoordsArray[0];
80 colors = colorArray[0];
81
82 dl = &backEnd.refdef.dlights[l];
83 origin0 = dl->transformed[0];
84 origin1 = dl->transformed[1];
85 origin2 = dl->transformed[2];
86 radius = dl->radius;
87 scale = 1.0f / radius;
88
89 if(r_greyscale->integer)
90 {
91 float luminance;
92
93 luminance = LUMA(dl->color[0], dl->color[1], dl->color[2]) * 255.0f;
94 floatColor[0] = floatColor[1] = floatColor[2] = luminance;
95 }
96 else if(r_greyscale->value)
97 {
98 float luminance;
99
100 luminance = LUMA(dl->color[0], dl->color[1], dl->color[2]) * 255.0f;
101 floatColor[0] = LERP(dl->color[0] * 255.0f, luminance, r_greyscale->value);
102 floatColor[1] = LERP(dl->color[1] * 255.0f, luminance, r_greyscale->value);
103 floatColor[2] = LERP(dl->color[2] * 255.0f, luminance, r_greyscale->value);
104 }
105 else
106 {
107 floatColor[0] = dl->color[0] * 255.0f;
108 floatColor[1] = dl->color[1] * 255.0f;
109 floatColor[2] = dl->color[2] * 255.0f;
110 }
111 floatColorVec0 = vec_ld(0, floatColor);
112 floatColorVec1 = vec_ld(11, floatColor);
113 floatColorVec0 = vec_perm(floatColorVec0,floatColorVec0,floatColorVecPerm);
114 for ( i = 0 ; i < tess.numVertexes ; i++, texCoords += 2, colors += 4 ) {
115 int clip = 0;
116 vec_t dist0, dist1, dist2;
117
118 dist0 = origin0 - tess.xyz[i][0];
119 dist1 = origin1 - tess.xyz[i][1];
120 dist2 = origin2 - tess.xyz[i][2];
121
122 backEnd.pc.c_dlightVertexes++;
123
124 texCoords0 = 0.5f + dist0 * scale;
125 texCoords1 = 0.5f + dist1 * scale;
126
127 if( !r_dlightBacks->integer &&
128 // dist . tess.normal[i]
129 ( dist0 * tess.normal[i][0] +
130 dist1 * tess.normal[i][1] +
131 dist2 * tess.normal[i][2] ) < 0.0f ) {
132 clip = 63;
133 } else {
134 if ( texCoords0 < 0.0f ) {
135 clip |= 1;
136 } else if ( texCoords0 > 1.0f ) {
137 clip |= 2;
138 }
139 if ( texCoords1 < 0.0f ) {
140 clip |= 4;
141 } else if ( texCoords1 > 1.0f ) {
142 clip |= 8;
143 }
144 texCoords[0] = texCoords0;
145 texCoords[1] = texCoords1;
146
147 // modulate the strength based on the height and color
148 if ( dist2 > radius ) {
149 clip |= 16;
150 modulate = 0.0f;
151 } else if ( dist2 < -radius ) {
152 clip |= 32;
153 modulate = 0.0f;
154 } else {
155 dist2 = Q_fabs(dist2);
156 if ( dist2 < radius * 0.5f ) {
157 modulate = 1.0f;
158 } else {
159 modulate = 2.0f * (radius - dist2) * scale;
160 }
161 }
162 }
163 clipBits[i] = clip;
164
165 modulateVec = vec_ld(0,(float *)&modulate);
166 modulateVec = vec_perm(modulateVec,modulateVec,modulatePerm);
167 colorVec = vec_madd(floatColorVec0,modulateVec,zero);
168 colorInt = vec_cts(colorVec,0); // RGBx
169 colorShort = vec_pack(colorInt,colorInt); // RGBxRGBx
170 colorChar = vec_packsu(colorShort,colorShort); // RGBxRGBxRGBxRGBx
171 colorChar = vec_sel(colorChar,vSel,vSel); // RGBARGBARGBARGBA replace alpha with 255
172 vec_ste((vector unsigned int)colorChar,0,(unsigned int *)colors); // store color
173 }
174
175 // build a list of triangles that need light
176 numIndexes = 0;
177 for ( i = 0 ; i < tess.numIndexes ; i += 3 ) {
178 int a, b, c;
179
180 a = tess.indexes[i];
181 b = tess.indexes[i+1];
182 c = tess.indexes[i+2];
183 if ( clipBits[a] & clipBits[b] & clipBits[c] ) {
184 continue; // not lighted
185 }
186 hitIndexes[numIndexes] = a;
187 hitIndexes[numIndexes+1] = b;
188 hitIndexes[numIndexes+2] = c;
189 numIndexes += 3;
190 }
191
192 if ( !numIndexes ) {
193 continue;
194 }
195
196 qglEnableClientState( GL_TEXTURE_COORD_ARRAY );
197 qglTexCoordPointer( 2, GL_FLOAT, 0, texCoordsArray[0] );
198
199 qglEnableClientState( GL_COLOR_ARRAY );
200 qglColorPointer( 4, GL_UNSIGNED_BYTE, 0, colorArray );
201
202 GL_Bind( tr.dlightImage );
203 // include GLS_DEPTHFUNC_EQUAL so alpha tested surfaces don't add light
204 // where they aren't rendered
205 if ( dl->additive ) {
206 GL_State( GLS_SRCBLEND_ONE | GLS_DSTBLEND_ONE | GLS_DEPTHFUNC_EQUAL );
207 }
208 else {
209 GL_State( GLS_SRCBLEND_DST_COLOR | GLS_DSTBLEND_ONE | GLS_DEPTHFUNC_EQUAL );
210 }
211 R_DrawElements( numIndexes, hitIndexes );
212 backEnd.pc.c_totalIndexes += numIndexes;
213 backEnd.pc.c_dlightIndexes += numIndexes;
214 }
215 }
216
217 void RB_CalcDiffuseColor_altivec( unsigned char *colors )
218 {
219 int i;
220 float *v, *normal;
221 trRefEntity_t *ent;
222 int ambientLightInt;
223 vec3_t lightDir;
224 int numVertexes;
225 vector unsigned char vSel = VECCONST_UINT8(0x00, 0x00, 0x00, 0xff,
226 0x00, 0x00, 0x00, 0xff,
227 0x00, 0x00, 0x00, 0xff,
228 0x00, 0x00, 0x00, 0xff);
229 vector float ambientLightVec;
230 vector float directedLightVec;
231 vector float lightDirVec;
232 vector float normalVec0, normalVec1;
233 vector float incomingVec0, incomingVec1, incomingVec2;
234 vector float zero, jVec;
235 vector signed int jVecInt;
236 vector signed short jVecShort;
237 vector unsigned char jVecChar, normalPerm;
238 ent = backEnd.currentEntity;
239 ambientLightInt = ent->ambientLightInt;
240 // A lot of this could be simplified if we made sure
241 // entities light info was 16-byte aligned.
242 jVecChar = vec_lvsl(0, ent->ambientLight);
243 ambientLightVec = vec_ld(0, (vector float *)ent->ambientLight);
244 jVec = vec_ld(11, (vector float *)ent->ambientLight);
245 ambientLightVec = vec_perm(ambientLightVec,jVec,jVecChar);
246
247 jVecChar = vec_lvsl(0, ent->directedLight);
248 directedLightVec = vec_ld(0,(vector float *)ent->directedLight);
249 jVec = vec_ld(11,(vector float *)ent->directedLight);
250 directedLightVec = vec_perm(directedLightVec,jVec,jVecChar);
251
252 jVecChar = vec_lvsl(0, ent->lightDir);
253 lightDirVec = vec_ld(0,(vector float *)ent->lightDir);
254 jVec = vec_ld(11,(vector float *)ent->lightDir);
255 lightDirVec = vec_perm(lightDirVec,jVec,jVecChar);
256
257 zero = (vector float)vec_splat_s8(0);
258 VectorCopy( ent->lightDir, lightDir );
259
260 v = tess.xyz[0];
261 normal = tess.normal[0];
262
263 normalPerm = vec_lvsl(0,normal);
264 numVertexes = tess.numVertexes;
265 for (i = 0 ; i < numVertexes ; i++, v += 4, normal += 4) {
266 normalVec0 = vec_ld(0,(vector float *)normal);
267 normalVec1 = vec_ld(11,(vector float *)normal);
268 normalVec0 = vec_perm(normalVec0,normalVec1,normalPerm);
269 incomingVec0 = vec_madd(normalVec0, lightDirVec, zero);
270 incomingVec1 = vec_sld(incomingVec0,incomingVec0,4);
271 incomingVec2 = vec_add(incomingVec0,incomingVec1);
272 incomingVec1 = vec_sld(incomingVec1,incomingVec1,4);
273 incomingVec2 = vec_add(incomingVec2,incomingVec1);
274 incomingVec0 = vec_splat(incomingVec2,0);
275 incomingVec0 = vec_max(incomingVec0,zero);
276 normalPerm = vec_lvsl(12,normal);
277 jVec = vec_madd(incomingVec0, directedLightVec, ambientLightVec);
278 jVecInt = vec_cts(jVec,0); // RGBx
279 jVecShort = vec_pack(jVecInt,jVecInt); // RGBxRGBx
280 jVecChar = vec_packsu(jVecShort,jVecShort); // RGBxRGBxRGBxRGBx
281 jVecChar = vec_sel(jVecChar,vSel,vSel); // RGBARGBARGBARGBA replace alpha with 255
282 vec_ste((vector unsigned int)jVecChar,0,(unsigned int *)&colors[i*4]); // store color
283 }
284 }
285
286 void LerpMeshVertexes_altivec(md3Surface_t *surf, float backlerp)
287 {
288 short *oldXyz, *newXyz, *oldNormals, *newNormals;
289 float *outXyz, *outNormal;
290 float oldXyzScale Q_ALIGN(16);
291 float newXyzScale Q_ALIGN(16);
292 float oldNormalScale Q_ALIGN(16);
293 float newNormalScale Q_ALIGN(16);
294 int vertNum;
295 unsigned lat, lng;
296 int numVerts;
297
298 outXyz = tess.xyz[tess.numVertexes];
299 outNormal = tess.normal[tess.numVertexes];
300
301 newXyz = (short *)((byte *)surf + surf->ofsXyzNormals)
302 + (backEnd.currentEntity->e.frame * surf->numVerts * 4);
303 newNormals = newXyz + 3;
304
305 newXyzScale = MD3_XYZ_SCALE * (1.0 - backlerp);
306 newNormalScale = 1.0 - backlerp;
307
308 numVerts = surf->numVerts;
309
310 if ( backlerp == 0 ) {
311 vector signed short newNormalsVec0;
312 vector signed short newNormalsVec1;
313 vector signed int newNormalsIntVec;
314 vector float newNormalsFloatVec;
315 vector float newXyzScaleVec;
316 vector unsigned char newNormalsLoadPermute;
317 vector unsigned char newNormalsStorePermute;
318 vector float zero;
319
320 newNormalsStorePermute = vec_lvsl(0,(float *)&newXyzScaleVec);
321 newXyzScaleVec = *(vector float *)&newXyzScale;
322 newXyzScaleVec = vec_perm(newXyzScaleVec,newXyzScaleVec,newNormalsStorePermute);
323 newXyzScaleVec = vec_splat(newXyzScaleVec,0);
324 newNormalsLoadPermute = vec_lvsl(0,newXyz);
325 newNormalsStorePermute = vec_lvsr(0,outXyz);
326 zero = (vector float)vec_splat_s8(0);
327 //
328 // just copy the vertexes
329 //
330 for (vertNum=0 ; vertNum < numVerts ; vertNum++,
331 newXyz += 4, newNormals += 4,
332 outXyz += 4, outNormal += 4)
333 {
334 newNormalsLoadPermute = vec_lvsl(0,newXyz);
335 newNormalsStorePermute = vec_lvsr(0,outXyz);
336 newNormalsVec0 = vec_ld(0,newXyz);
337 newNormalsVec1 = vec_ld(16,newXyz);
338 newNormalsVec0 = vec_perm(newNormalsVec0,newNormalsVec1,newNormalsLoadPermute);
339 newNormalsIntVec = vec_unpackh(newNormalsVec0);
340 newNormalsFloatVec = vec_ctf(newNormalsIntVec,0);
341 newNormalsFloatVec = vec_madd(newNormalsFloatVec,newXyzScaleVec,zero);
342 newNormalsFloatVec = vec_perm(newNormalsFloatVec,newNormalsFloatVec,newNormalsStorePermute);
343 //outXyz[0] = newXyz[0] * newXyzScale;
344 //outXyz[1] = newXyz[1] * newXyzScale;
345 //outXyz[2] = newXyz[2] * newXyzScale;
346
347 lat = ( newNormals[0] >> 8 ) & 0xff;
348 lng = ( newNormals[0] & 0xff );
349 lat *= (FUNCTABLE_SIZE/256);
350 lng *= (FUNCTABLE_SIZE/256);
351
352 // decode X as cos( lat ) * sin( long )
353 // decode Y as sin( lat ) * sin( long )
354 // decode Z as cos( long )
355
356 outNormal[0] = tr.sinTable[(lat+(FUNCTABLE_SIZE/4))&FUNCTABLE_MASK] * tr.sinTable[lng];
357 outNormal[1] = tr.sinTable[lat] * tr.sinTable[lng];
358 outNormal[2] = tr.sinTable[(lng+(FUNCTABLE_SIZE/4))&FUNCTABLE_MASK];
359
360 vec_ste(newNormalsFloatVec,0,outXyz);
361 vec_ste(newNormalsFloatVec,4,outXyz);
362 vec_ste(newNormalsFloatVec,8,outXyz);
363 }
364 } else {
365 //
366 // interpolate and copy the vertex and normal
367 //
368 oldXyz = (short *)((byte *)surf + surf->ofsXyzNormals)
369 + (backEnd.currentEntity->e.oldframe * surf->numVerts * 4);
370 oldNormals = oldXyz + 3;
371
372 oldXyzScale = MD3_XYZ_SCALE * backlerp;
373 oldNormalScale = backlerp;
374
375 for (vertNum=0 ; vertNum < numVerts ; vertNum++,
376 oldXyz += 4, newXyz += 4, oldNormals += 4, newNormals += 4,
377 outXyz += 4, outNormal += 4)
378 {
379 vec3_t uncompressedOldNormal, uncompressedNewNormal;
380
381 // interpolate the xyz
382 outXyz[0] = oldXyz[0] * oldXyzScale + newXyz[0] * newXyzScale;
383 outXyz[1] = oldXyz[1] * oldXyzScale + newXyz[1] * newXyzScale;
384 outXyz[2] = oldXyz[2] * oldXyzScale + newXyz[2] * newXyzScale;
385
386 // FIXME: interpolate lat/long instead?
387 lat = ( newNormals[0] >> 8 ) & 0xff;
388 lng = ( newNormals[0] & 0xff );
389 lat *= 4;
390 lng *= 4;
391 uncompressedNewNormal[0] = tr.sinTable[(lat+(FUNCTABLE_SIZE/4))&FUNCTABLE_MASK] * tr.sinTable[lng];
392 uncompressedNewNormal[1] = tr.sinTable[lat] * tr.sinTable[lng];
393 uncompressedNewNormal[2] = tr.sinTable[(lng+(FUNCTABLE_SIZE/4))&FUNCTABLE_MASK];
394
395 lat = ( oldNormals[0] >> 8 ) & 0xff;
396 lng = ( oldNormals[0] & 0xff );
397 lat *= 4;
398 lng *= 4;
399
400 uncompressedOldNormal[0] = tr.sinTable[(lat+(FUNCTABLE_SIZE/4))&FUNCTABLE_MASK] * tr.sinTable[lng];
401 uncompressedOldNormal[1] = tr.sinTable[lat] * tr.sinTable[lng];
402 uncompressedOldNormal[2] = tr.sinTable[(lng+(FUNCTABLE_SIZE/4))&FUNCTABLE_MASK];
403
404 outNormal[0] = uncompressedOldNormal[0] * oldNormalScale + uncompressedNewNormal[0] * newNormalScale;
405 outNormal[1] = uncompressedOldNormal[1] * oldNormalScale + uncompressedNewNormal[1] * newNormalScale;
406 outNormal[2] = uncompressedOldNormal[2] * oldNormalScale + uncompressedNewNormal[2] * newNormalScale;
407
408 // VectorNormalize (outNormal);
409 }
410 VectorArrayNormalize((vec4_t *)tess.normal[tess.numVertexes], numVerts);
411 }
412 }
413
414 #endif