Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.96 KB | None | 0 0
  1. static void ProjectDlightTexture_altivec( void ) {
  2.     int     i, l;
  3.     vec_t   origin0, origin1, origin2;
  4.     float   texCoords0, texCoords1;
  5.     vector float floatColorVec0, floatColorVec1;
  6.     vector float modulateVec, colorVec, zero;
  7.     vector short colorShort;
  8.     vector signed int colorInt;
  9.     vector unsigned char floatColorVecPerm, modulatePerm, colorChar;
  10.     vector unsigned char vSel = VECCONST_UINT8(0x00, 0x00, 0x00, 0xff,
  11.                                                0x00, 0x00, 0x00, 0xff,
  12.                                                0x00, 0x00, 0x00, 0xff,
  13.                                                0x00, 0x00, 0x00, 0xff);
  14.     float   *texCoords;
  15.     byte    *colors;
  16.     byte    clipBits[SHADER_MAX_VERTEXES];
  17.     float   texCoordsArray[SHADER_MAX_VERTEXES][2];
  18.     byte    colorArray[SHADER_MAX_VERTEXES][4];
  19.     unsigned    hitIndexes[SHADER_MAX_INDEXES];
  20.     int     numIndexes;
  21.     float   scale;
  22.     float   radius;
  23.     vec3_t  floatColor;
  24.     float   modulate = 0.0f;
  25.  
  26.     if ( !backEnd.refdef.num_dlights ) {
  27.         return;
  28.     }
  29.  
  30.     // There has to be a better way to do this so that floatColor
  31.     // and/or modulate are already 16-byte aligned.
  32.     floatColorVecPerm = vec_lvsl(0,(float *)floatColor);
  33.     modulatePerm = vec_lvsl(0,(float *)&modulate);
  34.     modulatePerm = (vector unsigned char)vec_splat((vector unsigned int)modulatePerm,0);
  35.     zero = (vector float)vec_splat_s8(0);
  36.  
  37.     for ( l = 0 ; l < backEnd.refdef.num_dlights ; l++ ) {
  38.         dlight_t    *dl;
  39.  
  40.         if ( !( tess.dlightBits & ( 1 << l ) ) ) {
  41.             continue;   // this surface definately doesn't have any of this light
  42.         }
  43.         texCoords = texCoordsArray[0];
  44.         colors = colorArray[0];
  45.  
  46.         dl = &backEnd.refdef.dlights[l];
  47.         origin0 = dl->transformed[0];
  48.         origin1 = dl->transformed[1];
  49.         origin2 = dl->transformed[2];
  50.         radius = dl->radius;
  51.         scale = 1.0f / radius;
  52.  
  53.         floatColor[0] = dl->color[0] * 255.0f;
  54.         floatColor[1] = dl->color[1] * 255.0f;
  55.         floatColor[2] = dl->color[2] * 255.0f;
  56.         floatColorVec0 = vec_ld(0, floatColor);
  57.         floatColorVec1 = vec_ld(11, floatColor);
  58.         floatColorVec0 = vec_perm(floatColorVec0,floatColorVec0,floatColorVecPerm);
  59.         for ( i = 0 ; i < tess.numVertexes ; i++, texCoords += 2, colors += 4 ) {
  60.             int     clip = 0;
  61.             vec_t dist0, dist1, dist2;
  62.            
  63.             dist0 = origin0 - tess.xyz[i][0];
  64.             dist1 = origin1 - tess.xyz[i][1];
  65.             dist2 = origin2 - tess.xyz[i][2];
  66.  
  67.             backEnd.pc.c_dlightVertexes++;
  68.  
  69.             texCoords0 = 0.5f + dist0 * scale;
  70.             texCoords1 = 0.5f + dist1 * scale;
  71.  
  72.             if( !r_dlightBacks->integer &&
  73.                     // dist . tess.normal[i]
  74.                     ( dist0 * tess.normal[i][0] +
  75.                     dist1 * tess.normal[i][1] +
  76.                     dist2 * tess.normal[i][2] ) < 0.0f ) {
  77.                 clip = 63;
  78.             } else {
  79.                 if ( texCoords0 < 0.0f ) {
  80.                     clip |= 1;
  81.                 } else if ( texCoords0 > 1.0f ) {
  82.                     clip |= 2;
  83.                 }
  84.                 if ( texCoords1 < 0.0f ) {
  85.                     clip |= 4;
  86.                 } else if ( texCoords1 > 1.0f ) {
  87.                     clip |= 8;
  88.                 }
  89.                 texCoords[0] = texCoords0;
  90.                 texCoords[1] = texCoords1;
  91.  
  92.                 // modulate the strength based on the height and color
  93.                 if ( dist2 > radius ) {
  94.                     clip |= 16;
  95.                     modulate = 0.0f;
  96.                 } else if ( dist2 < -radius ) {
  97.                     clip |= 32;
  98.                     modulate = 0.0f;
  99.                 } else {
  100.                     dist2 = Q_fabs(dist2);
  101.                     if ( dist2 < radius * 0.5f ) {
  102.                         modulate = 1.0f;
  103.                     } else {
  104.                         modulate = 2.0f * (radius - dist2) * scale;
  105.                     }
  106.                 }
  107.             }
  108.             clipBits[i] = clip;
  109.  
  110.             modulateVec = vec_ld(0,(float *)&modulate);
  111.             modulateVec = vec_perm(modulateVec,modulateVec,modulatePerm);
  112.             colorVec = vec_madd(floatColorVec0,modulateVec,zero);
  113.             colorInt = vec_cts(colorVec,0); // RGBx
  114.             colorShort = vec_pack(colorInt,colorInt);       // RGBxRGBx
  115.             colorChar = vec_packsu(colorShort,colorShort);  // RGBxRGBxRGBxRGBx
  116.             colorChar = vec_sel(colorChar,vSel,vSel);       // RGBARGBARGBARGBA replace alpha with 255
  117.             vec_ste((vector unsigned int)colorChar,0,(unsigned int *)colors);   // store color
  118.         }
  119.  
  120.         // build a list of triangles that need light
  121.         numIndexes = 0;
  122.         for ( i = 0 ; i < tess.numIndexes ; i += 3 ) {
  123.             int     a, b, c;
  124.  
  125.             a = tess.indexes[i];
  126.             b = tess.indexes[i+1];
  127.             c = tess.indexes[i+2];
  128.             if ( clipBits[a] & clipBits[b] & clipBits[c] ) {
  129.                 continue;   // not lighted
  130.             }
  131.             hitIndexes[numIndexes] = a;
  132.             hitIndexes[numIndexes+1] = b;
  133.             hitIndexes[numIndexes+2] = c;
  134.             numIndexes += 3;
  135.         }
  136.  
  137.         if ( !numIndexes ) {
  138.             continue;
  139.         }
  140.  
  141.         qglEnableClientState( GL_TEXTURE_COORD_ARRAY );
  142.         qglTexCoordPointer( 2, GL_FLOAT, 0, texCoordsArray[0] );
  143.  
  144.         qglEnableClientState( GL_COLOR_ARRAY );
  145.         qglColorPointer( 4, GL_UNSIGNED_BYTE, 0, colorArray );
  146.  
  147.         GL_Bind( tr.dlightImage );
  148.         // include GLS_DEPTHFUNC_EQUAL so alpha tested surfaces don't add light
  149.         // where they aren't rendered
  150.         if ( dl->additive ) {
  151.             GL_State( GLS_SRCBLEND_ONE | GLS_DSTBLEND_ONE | GLS_DEPTHFUNC_EQUAL );
  152.         }
  153.         else {
  154.             GL_State( GLS_SRCBLEND_DST_COLOR | GLS_DSTBLEND_ONE | GLS_DEPTHFUNC_EQUAL );
  155.         }
  156.         R_DrawElements( numIndexes, hitIndexes );
  157.         backEnd.pc.c_totalIndexes += numIndexes;
  158.         backEnd.pc.c_dlightIndexes += numIndexes;
  159.     }
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement