Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

376 lines
12 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. // $Id$
  3. #include "raytrace.h"
  4. #include <mathlib/halton.h>
  5. static uint32 MapDistanceToPixel(float t)
  6. {
  7. if (t<0) return 0xffff0000;
  8. if (t>100) return 0xff000000;
  9. int a=t*1000; a&=0xff;
  10. int b=t*10; b &=0xff;
  11. int c=t*.01; c &=0xff;
  12. return 0xff000000+(a<<16)+(b<<8)+c;
  13. }
  14. #define IGAMMA (1.0/2.2)
  15. #define MAGIC_NUMBER (1<<23)
  16. static fltx4 Four_MagicNumbers={ MAGIC_NUMBER, MAGIC_NUMBER, MAGIC_NUMBER, MAGIC_NUMBER };
  17. static ALIGN16 int32 Four_255s[4]= {0xff,0xff,0xff,0xff};
  18. #define PIXMASK ( * ( reinterpret_cast< fltx4 *>( &Four_255s ) ) )
  19. void MapLinearIntensities(FourVectors const &intens,uint32 *p1, uint32 *p2, uint32 *p3, uint32 *p4)
  20. {
  21. // convert four pixels worth of sse-style rgb into argb lwords
  22. // NOTE the _mm_empty macro is voodoo. do not mess with this routine casually - simply throwing
  23. // anything that ends up generating a fpu stack references in here would be bad news.
  24. static fltx4 pixscale={255.0,255.0,255.0,255.0};
  25. fltx4 r,g,b;
  26. r=MinSIMD(pixscale,MulSIMD(pixscale,PowSIMD(intens.x,IGAMMA)));
  27. g=MinSIMD(pixscale,MulSIMD(pixscale,PowSIMD(intens.y,IGAMMA)));
  28. b=MinSIMD(pixscale,MulSIMD(pixscale,PowSIMD(intens.z,IGAMMA)));
  29. // now, convert to integer
  30. r=AndSIMD( AddSIMD( r, Four_MagicNumbers ), PIXMASK );
  31. g=AndSIMD( AddSIMD( g, Four_MagicNumbers ), PIXMASK );
  32. b=AndSIMD( AddSIMD( b, Four_MagicNumbers ), PIXMASK );
  33. *(p1)=(SubInt(r, 0))|(SubInt(g, 0)<<8)|(SubInt(b, 0)<<16);
  34. *(p2)=(SubInt(r, 1))|(SubInt(g, 1)<<8)|(SubInt(b, 1)<<16);
  35. *(p3)=(SubInt(r, 2))|(SubInt(g, 2)<<8)|(SubInt(b, 2)<<16);
  36. *(p4)=(SubInt(r, 3))|(SubInt(g, 3)<<8)|(SubInt(b, 3)<<16);
  37. }
  38. static ALIGN16 uint32 signmask[4]={0x80000000,0x80000000,0x80000000,0x80000000};
  39. static ALIGN16 int32 all_ones[4]={-1,-1,-1,-1};
  40. static fltx4 all_zeros={0,0,0,0};
  41. static fltx4 TraceLimit={1.0e20,1.0e20,1.0e20,1.0e20};
  42. void RayTracingEnvironment::RenderScene(
  43. int width, int height, // width and height of desired rendering
  44. int stride, // actual width in pixels of target buffer
  45. uint32 *output_buffer, // pointer to destination
  46. Vector CameraOrigin, // eye position
  47. Vector ULCorner, // word space coordinates of upper left
  48. // monitor corner
  49. Vector URCorner, // top right corner
  50. Vector LLCorner, // lower left
  51. Vector LRCorner, // lower right
  52. RayTraceLightingMode_t lmode)
  53. {
  54. // first, compute deltas
  55. Vector dxvector=URCorner;
  56. dxvector-=ULCorner;
  57. dxvector*=(1.0/width);
  58. Vector dxvectortimes2=dxvector;
  59. dxvectortimes2+=dxvector;
  60. Vector dyvector=LLCorner;
  61. dyvector-=ULCorner;
  62. dyvector*=(1.0/height);
  63. // block_offsets-relative offsets for eahc of the 4 pixels in the block, in sse format
  64. FourVectors block_offsets;
  65. block_offsets.LoadAndSwizzle(Vector(0,0,0),dxvector,dyvector,dxvector+dyvector);
  66. FourRays myrays;
  67. myrays.origin.DuplicateVector(CameraOrigin);
  68. // tmprays is used fo rthe case when we cannot trace 4 rays at once.
  69. FourRays tmprays;
  70. tmprays.origin.DuplicateVector(CameraOrigin);
  71. // now, we will ray trace pixels. we will do the rays in a 2x2 pattern
  72. for(int y=0;y<height;y+=2)
  73. {
  74. Vector SLoc=dyvector;
  75. SLoc*=((float) y);
  76. SLoc+=ULCorner;
  77. uint32 *dest=output_buffer+y*stride;
  78. for(int x=0;x<width;x+=2)
  79. {
  80. myrays.direction.DuplicateVector(SLoc);
  81. myrays.direction+=block_offsets;
  82. myrays.direction.VectorNormalize();
  83. RayTracingResult rslt;
  84. Trace4Rays(myrays,all_zeros,TraceLimit, &rslt);
  85. if ((rslt.HitIds[0]==-1) && (rslt.HitIds[1]==-1) &&
  86. (rslt.HitIds[2]==-1) && (rslt.HitIds[3]==-1))
  87. MapLinearIntensities(BackgroundColor,dest,dest+1,dest+stride,dest+stride+1);
  88. else
  89. {
  90. // make sure normal points back towards ray origin
  91. fltx4 ndoti=rslt.surface_normal*myrays.direction;
  92. fltx4 bad_dirs=AndSIMD(CmpGtSIMD(ndoti,Four_Zeros),
  93. LoadAlignedSIMD((float *) signmask));
  94. // flip signs of all "wrong" normals
  95. rslt.surface_normal.x=XorSIMD(bad_dirs,rslt.surface_normal.x);
  96. rslt.surface_normal.y=XorSIMD(bad_dirs,rslt.surface_normal.y);
  97. rslt.surface_normal.z=XorSIMD(bad_dirs,rslt.surface_normal.z);
  98. FourVectors intens;
  99. intens.DuplicateVector(Vector(0,0,0));
  100. // set up colors
  101. FourVectors surf_colors;
  102. surf_colors.DuplicateVector(Vector(0,0,0));
  103. for(int i=0;i<4;i++)
  104. {
  105. if (rslt.HitIds[i]>=0)
  106. {
  107. surf_colors.X(i)=TriangleColors[rslt.HitIds[i]].x;
  108. surf_colors.Y(i)=TriangleColors[rslt.HitIds[i]].y;
  109. surf_colors.Z(i)=TriangleColors[rslt.HitIds[i]].z;
  110. }
  111. }
  112. FourVectors surface_pos=myrays.direction;
  113. surface_pos*=rslt.HitDistance;
  114. surface_pos+=myrays.origin;
  115. switch(lmode)
  116. {
  117. case DIRECT_LIGHTING:
  118. {
  119. // light all points
  120. for(int l=0;l<LightList.Count();l++)
  121. {
  122. LightList[l].ComputeLightAtPoints(surface_pos,rslt.surface_normal,
  123. intens);
  124. }
  125. }
  126. break;
  127. case DIRECT_LIGHTING_WITH_SHADOWS:
  128. {
  129. // light all points
  130. for(int l=0;l<LightList.Count();l++)
  131. {
  132. FourVectors ldir;
  133. ldir.DuplicateVector(LightList[l].m_Position);
  134. ldir-=surface_pos;
  135. fltx4 MaxT=ldir.length();
  136. ldir.VectorNormalizeFast();
  137. // now, compute shadow flag
  138. //FourRays myrays;
  139. myrays.origin=surface_pos;
  140. FourVectors epsilon=ldir;
  141. epsilon*=0.01;
  142. myrays.origin+=epsilon;
  143. myrays.direction=ldir;
  144. RayTracingResult shadowtest;
  145. Trace4Rays(myrays,Four_Zeros,MaxT, &shadowtest);
  146. fltx4 unshadowed=CmpGtSIMD(shadowtest.HitDistance,MaxT);
  147. if (! (IsAllZeros(unshadowed)))
  148. {
  149. FourVectors tmp;
  150. tmp.DuplicateVector(Vector(0,0,0));
  151. LightList[l].ComputeLightAtPoints(surface_pos,rslt.surface_normal,
  152. tmp);
  153. intens.x=AddSIMD(intens.x,AndSIMD(tmp.x,unshadowed));
  154. intens.y=AddSIMD(intens.y,AndSIMD(tmp.y,unshadowed));
  155. intens.z=AddSIMD(intens.z,AndSIMD(tmp.z,unshadowed));
  156. }
  157. }
  158. }
  159. break;
  160. }
  161. // now, mask off non-hitting pixels
  162. intens.VProduct(surf_colors);
  163. fltx4 no_hit_mask=CmpGtSIMD(rslt.HitDistance,TraceLimit);
  164. intens.x=OrSIMD(AndSIMD(BackgroundColor.x,no_hit_mask),
  165. AndNotSIMD(no_hit_mask,intens.x));
  166. intens.y=OrSIMD(AndSIMD(BackgroundColor.y,no_hit_mask),
  167. AndNotSIMD(no_hit_mask,intens.y));
  168. intens.z=OrSIMD(AndSIMD(BackgroundColor.z,no_hit_mask),
  169. AndNotSIMD(no_hit_mask,intens.z));
  170. MapLinearIntensities(intens,dest,dest+1,dest+stride,dest+stride+1);
  171. }
  172. dest+=2;
  173. SLoc+=dxvectortimes2;
  174. }
  175. }
  176. }
  177. #define SQ(x) ((x)*(x))
  178. void RayTracingEnvironment::ComputeVirtualLightSources(void)
  179. {
  180. int start_pos=0;
  181. for(int b=0;b<3;b++)
  182. {
  183. int nl=LightList.Count();
  184. int where_to_start=start_pos;
  185. start_pos=nl;
  186. for(int l=where_to_start;l<nl;l++)
  187. {
  188. DirectionalSampler_t sample_generator;
  189. int n_desired=1*LightList[l].m_Color.Length();
  190. if (LightList[l].m_Type==MATERIAL_LIGHT_SPOT)
  191. n_desired*=LightList[l].m_Phi/2;
  192. for(int try1=0;try1<n_desired;try1++)
  193. {
  194. LightDesc_t const &li=LightList[l];
  195. FourRays myrays;
  196. myrays.origin.DuplicateVector(li.m_Position);
  197. RayTracingResult rslt;
  198. Vector trial_dir=sample_generator.NextValue();
  199. if (li.IsDirectionWithinLightCone(trial_dir))
  200. {
  201. myrays.direction.DuplicateVector(trial_dir);
  202. Trace4Rays(myrays,all_zeros,ReplicateX4(1000.0), &rslt);
  203. if ((rslt.HitIds[0]!=-1))
  204. {
  205. // make sure normal points back towards ray origin
  206. fltx4 ndoti=rslt.surface_normal*myrays.direction;
  207. fltx4 bad_dirs=AndSIMD(CmpGtSIMD(ndoti,Four_Zeros),
  208. LoadAlignedSIMD((float *) signmask));
  209. // flip signs of all "wrong" normals
  210. rslt.surface_normal.x=XorSIMD(bad_dirs,rslt.surface_normal.x);
  211. rslt.surface_normal.y=XorSIMD(bad_dirs,rslt.surface_normal.y);
  212. rslt.surface_normal.z=XorSIMD(bad_dirs,rslt.surface_normal.z);
  213. // a hit! let's make a virtual light source
  214. // treat the virtual light as a disk with its center at the hit position
  215. // and its radius scaled by the amount of the solid angle this probe
  216. // represents.
  217. float area_of_virtual_light=
  218. 4.0*M_PI*SQ( SubFloat( rslt.HitDistance, 0 ) )*(1.0/n_desired);
  219. FourVectors intens;
  220. intens.DuplicateVector(Vector(0,0,0));
  221. FourVectors surface_pos=myrays.direction;
  222. surface_pos*=rslt.HitDistance;
  223. surface_pos+=myrays.origin;
  224. FourVectors delta=rslt.surface_normal;
  225. delta*=0.1;
  226. surface_pos+=delta;
  227. LightList[l].ComputeLightAtPoints(surface_pos,rslt.surface_normal,
  228. intens);
  229. FourVectors surf_colors;
  230. surf_colors.DuplicateVector(TriangleColors[rslt.HitIds[0]]);
  231. intens*=surf_colors;
  232. // see if significant
  233. LightDesc_t l1;
  234. l1.m_Type=MATERIAL_LIGHT_SPOT;
  235. l1.m_Position=Vector(surface_pos.X(0),surface_pos.Y(0),surface_pos.Z(0));
  236. l1.m_Direction=Vector(rslt.surface_normal.X(0),rslt.surface_normal.Y(0),
  237. rslt.surface_normal.Z(0));
  238. l1.m_Color=Vector(intens.X(0),intens.Y(0),intens.Z(0));
  239. if (l1.m_Color.Length()>0)
  240. {
  241. l1.m_Color*=area_of_virtual_light/M_PI;
  242. l1.m_Range=0.0;
  243. l1.m_Falloff=1.0;
  244. l1.m_Attenuation0=1.0;
  245. l1.m_Attenuation1=0.0;
  246. l1.m_Attenuation2=1.0; // intens falls off as 1/r^2
  247. l1.m_Theta=0;
  248. l1.m_Phi=M_PI;
  249. l1.RecalculateDerivedValues();
  250. LightList.AddToTail(l1);
  251. }
  252. }
  253. }
  254. }
  255. }
  256. }
  257. }
  258. static unsigned int GetSignMask(Vector const &v)
  259. {
  260. unsigned int ret=0;
  261. if (v.x<0.0)
  262. ret++;
  263. if (v.y<0)
  264. ret+=2;
  265. if (v.z<0)
  266. ret+=4;
  267. return ret;
  268. }
  269. inline void RayTracingEnvironment::FlushStreamEntry(RayStream &s,int msk)
  270. {
  271. assert(msk>=0);
  272. assert(msk<8);
  273. fltx4 tmax=s.PendingRays[msk].direction.length();
  274. fltx4 scl=ReciprocalSaturateSIMD(tmax);
  275. s.PendingRays[msk].direction*=scl; // normalize
  276. RayTracingResult tmpresult;
  277. Trace4Rays(s.PendingRays[msk],Four_Zeros,tmax,msk,&tmpresult);
  278. // now, write out results
  279. for(int r=0;r<4;r++)
  280. {
  281. RayTracingSingleResult *out=s.PendingStreamOutputs[msk][r];
  282. out->ray_length=SubFloat( tmax, r );
  283. out->surface_normal.x=tmpresult.surface_normal.X(r);
  284. out->surface_normal.y=tmpresult.surface_normal.Y(r);
  285. out->surface_normal.z=tmpresult.surface_normal.Z(r);
  286. out->HitID=tmpresult.HitIds[r];
  287. out->HitDistance=SubFloat( tmpresult.HitDistance, r );
  288. }
  289. s.n_in_stream[msk]=0;
  290. }
  291. void RayTracingEnvironment::AddToRayStream(RayStream &s,
  292. Vector const &start,Vector const &end,
  293. RayTracingSingleResult *rslt_out)
  294. {
  295. Vector delta=end;
  296. delta-=start;
  297. int msk=GetSignMask(delta);
  298. assert(msk>=0);
  299. assert(msk<8);
  300. int pos=s.n_in_stream[msk];
  301. assert(pos<4);
  302. s.PendingRays[msk].origin.X(pos)=start.x;
  303. s.PendingRays[msk].origin.Y(pos)=start.y;
  304. s.PendingRays[msk].origin.Z(pos)=start.z;
  305. s.PendingRays[msk].direction.X(pos)=delta.x;
  306. s.PendingRays[msk].direction.Y(pos)=delta.y;
  307. s.PendingRays[msk].direction.Z(pos)=delta.z;
  308. s.PendingStreamOutputs[msk][pos]=rslt_out;
  309. if (pos==3)
  310. {
  311. FlushStreamEntry(s,msk);
  312. }
  313. else
  314. s.n_in_stream[msk]++;
  315. }
  316. void RayTracingEnvironment::FinishRayStream(RayStream &s)
  317. {
  318. for(int msk=0;msk<8;msk++)
  319. {
  320. int cnt=s.n_in_stream[msk];
  321. if (cnt)
  322. {
  323. // fill in unfilled entries with dups of first
  324. for(int c=cnt;c<4;c++)
  325. {
  326. s.PendingRays[msk].origin.X(c) = s.PendingRays[msk].origin.X(0);
  327. s.PendingRays[msk].origin.Y(c) = s.PendingRays[msk].origin.Y(0);
  328. s.PendingRays[msk].origin.Z(c) = s.PendingRays[msk].origin.Z(0);
  329. s.PendingRays[msk].direction.X(c) = s.PendingRays[msk].direction.X(0);
  330. s.PendingRays[msk].direction.Y(c) = s.PendingRays[msk].direction.Y(0);
  331. s.PendingRays[msk].direction.Z(c) = s.PendingRays[msk].direction.Z(0);
  332. s.PendingStreamOutputs[msk][c]=s.PendingStreamOutputs[msk][0];
  333. }
  334. FlushStreamEntry(s,msk);
  335. }
  336. }
  337. }