Leaked source code of windows server 2003
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.

773 lines
27 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Copyright (C) Microsoft Corporation, 1998.
  3. //
  4. // refrasti.hpp
  5. //
  6. // Direct3D Reference Rasterizer - Main Internal Header File
  7. //
  8. ///////////////////////////////////////////////////////////////////////////////
  9. #ifndef _REFRASTI_HPP
  10. #define _REFRASTI_HPP
  11. ///////////////////////////////////////////////////////////////////////////////
  12. // //
  13. // Pixel Component Classes //
  14. // //
  15. ///////////////////////////////////////////////////////////////////////////////
  16. //-----------------------------------------------------------------------------
  17. //
  18. // Color Component Class - Class used for color channel (alpha & rgb)
  19. // processing.
  20. //
  21. // Internal format is single precision floating point. The 1.0 value maps
  22. // to 0xff for 8 bit color.
  23. //
  24. //-----------------------------------------------------------------------------
  25. class RRColorComp
  26. {
  27. FLOAT m_fVal;
  28. public:
  29. // default, UINT8, & FLOAT assignment constructor
  30. RRColorComp(void) : m_fVal(0.F) {;}
  31. RRColorComp(UINT8 uVal) : m_fVal((FLOAT)uVal/255.F) {;}
  32. RRColorComp(FLOAT fVal) : m_fVal(fVal) {;}
  33. // copy and assignment operators
  34. RRColorComp& operator=(const RRColorComp& A) { m_fVal = A.m_fVal; return *this; }
  35. RRColorComp& operator=(UINT8 uVal) { m_fVal = (1.f/255.f)*(FLOAT)uVal; return *this; }
  36. RRColorComp& operator=(FLOAT fVal) { m_fVal = fVal; return *this; }
  37. // round for integer get operations
  38. operator UINT8() const { return (UINT8)( ( (255.f)*m_fVal) + .5f); }
  39. operator unsigned() const { return (unsigned)( ( (255.f)*m_fVal ) + .5f); }
  40. operator FLOAT() const { return m_fVal; }
  41. // fixed point get function - specify number of integral and fractional bits
  42. INT32 GetFixed( int iIntBits, int iFracBits ) const
  43. {
  44. // float value is in 0. to 1. range, so scale up by the total number of
  45. // bits (the '-1' does the mapping such that (2**n)-1 is the max representable
  46. // value, for example 0xff is max for 8 integral bits (not 0x100))
  47. return (INT32)( ( m_fVal * (FLOAT)((1<<(iIntBits+iFracBits))-1) ) + .5f);
  48. }
  49. //
  50. // overloaded arithmetic operators - not much going on here for floating
  51. // point (would be much more interesting if internal representation was
  52. // fixed point)
  53. //
  54. // use compliment operator for component inverse (1. - value)
  55. friend RRColorComp operator~(const RRColorComp& A)
  56. {
  57. return RRColorComp( 1.F - A.m_fVal );
  58. }
  59. friend RRColorComp operator+(const RRColorComp& A, const RRColorComp& B)
  60. {
  61. return RRColorComp( A.m_fVal + B.m_fVal );
  62. }
  63. RRColorComp& operator+=(const RRColorComp& A)
  64. {
  65. m_fVal += A.m_fVal; return *this;
  66. }
  67. friend RRColorComp operator-(const RRColorComp& A, const RRColorComp& B)
  68. {
  69. return RRColorComp( A.m_fVal - B.m_fVal );
  70. }
  71. friend RRColorComp operator-(const RRColorComp& A, FLOAT fB)
  72. {
  73. return RRColorComp( A.m_fVal - fB );
  74. }
  75. friend RRColorComp operator*(const RRColorComp& A, const RRColorComp& B)
  76. {
  77. return RRColorComp( A.m_fVal * B.m_fVal );
  78. }
  79. friend RRColorComp operator*(const RRColorComp& A, FLOAT fB)
  80. {
  81. return RRColorComp( A.m_fVal * fB );
  82. }
  83. RRColorComp& operator*=(const RRColorComp& A)
  84. {
  85. m_fVal *= A.m_fVal; return *this;
  86. }
  87. RRColorComp& operator*=(const UINT8 uA)
  88. {
  89. m_fVal *= ((1./255.)*(FLOAT)uA); return *this;
  90. }
  91. friend RRColorComp minimum(const RRColorComp& A, const RRColorComp& B)
  92. {
  93. return RRColorComp( min( A.m_fVal, B.m_fVal ) );
  94. }
  95. friend RRColorComp maximum(const RRColorComp& A, const RRColorComp& B)
  96. {
  97. return RRColorComp( max( A.m_fVal, B.m_fVal ) );
  98. }
  99. };
  100. //-----------------------------------------------------------------------------
  101. //
  102. // Color Value Class - Holds an Alpha,Red,Green,Blue color value consisting of
  103. // four RRColorComp objects.
  104. //
  105. //-----------------------------------------------------------------------------
  106. class RRColor
  107. {
  108. public:
  109. RRColorComp A;
  110. RRColorComp R;
  111. RRColorComp G;
  112. RRColorComp B;
  113. // default and UINT32 assignment constructor
  114. RRColor( void ) : A(0.F), R(0.F), G(0.F), B(0.F) {;}
  115. RRColor( UINT32 uVal ) :
  116. A (UINT8( RGBA_GETALPHA( uVal ) )),
  117. R (UINT8( RGBA_GETRED( uVal ) )),
  118. G (UINT8( RGBA_GETGREEN( uVal ) )),
  119. B (UINT8( RGBA_GETBLUE( uVal ) )) {;}
  120. RRColor( FLOAT fR, FLOAT fG, FLOAT fB, FLOAT fA ) :
  121. R(fR), G(fG), B(fB), A(fA) {;}
  122. // UINT32 copy operator
  123. void operator=(const UINT32 uVal) // TODO: implement proper assignment operator?
  124. {
  125. A = UINT8( RGBA_GETALPHA( uVal ) );
  126. R = UINT8( RGBA_GETRED( uVal ) );
  127. G = UINT8( RGBA_GETGREEN( uVal ) );
  128. B = UINT8( RGBA_GETBLUE( uVal ) );
  129. }
  130. // casting operator
  131. operator UINT32() const
  132. {
  133. return D3DRGBA( FLOAT(R), FLOAT(G), FLOAT(B), FLOAT(A) );
  134. }
  135. // methods to set all channels
  136. void SetAllChannels( const RRColorComp& Val )
  137. {
  138. A = Val; R = Val; G = Val; B = Val;
  139. }
  140. void SetAllChannels( FLOAT fVal )
  141. {
  142. A = fVal; R = fVal; G = fVal; B = fVal;
  143. }
  144. //
  145. // conversions between surface format and RRColor - these define the
  146. // correct way to map between resolutions
  147. //
  148. // convert from surface type format to RRColor
  149. void ConvertFrom( RRSurfaceType Type, const char* pSurfaceBits )
  150. {
  151. UINT16 u16BITS;
  152. switch (Type)
  153. {
  154. default:
  155. case RR_STYPE_NULL: return;
  156. case RR_STYPE_B8G8R8A8: *this = *((UINT32*)pSurfaceBits); break;
  157. case RR_STYPE_B8G8R8X8: *this = *((UINT32*)pSurfaceBits); A = 1.F; break;
  158. case RR_STYPE_B5G6R5:
  159. u16BITS = *((UINT16*)pSurfaceBits);
  160. R = ((u16BITS>>(6+5)) & 0x001F)/31.f;
  161. G = ((u16BITS>> 5) & 0x003F)/63.f;
  162. B = ((u16BITS ) & 0x001F)/31.f;
  163. A = 1.F;
  164. break;
  165. case RR_STYPE_B5G5R5:
  166. u16BITS = *((UINT16*)pSurfaceBits);
  167. R = ((u16BITS>>(5+5)) & 0x001F)/31.f;
  168. G = ((u16BITS>> 5) & 0x001F)/31.f;
  169. B = ((u16BITS ) & 0x001F)/31.f;
  170. A = 1.F;
  171. break;
  172. case RR_STYPE_B5G5R5A1:
  173. u16BITS = *((UINT16*)pSurfaceBits);
  174. R = ((u16BITS>>(5+5)) & 0x001F)/31.f;
  175. G = ((u16BITS>> 5) & 0x001F)/31.f;
  176. B = ((u16BITS ) & 0x001F)/31.f;
  177. A = ( u16BITS & 0x8000 ) ? 1.f : 0.f;
  178. break;
  179. case RR_STYPE_B4G4R4A4:
  180. u16BITS = *((UINT16*)pSurfaceBits);
  181. R = ((u16BITS>>(4+4)) & 0x000F)/15.f;
  182. G = ((u16BITS>> 4) & 0x000F)/15.f;
  183. B = ((u16BITS ) & 0x000F)/15.f;
  184. A = ((u16BITS>>(4+4+4)) & 0x000F)/15.f;
  185. break;
  186. case RR_STYPE_B8G8R8:
  187. R = *((UINT8*)pSurfaceBits+2);
  188. G = *((UINT8*)pSurfaceBits+1);
  189. B = *((UINT8*)pSurfaceBits+0);
  190. A = 1.F;
  191. break;
  192. case RR_STYPE_L8:
  193. R = G = B = *((UINT8*)pSurfaceBits);
  194. A = 1.F;
  195. break;
  196. case RR_STYPE_L8A8:
  197. u16BITS = *((UINT16*)pSurfaceBits);
  198. R = G = B = (UINT8)(0xff & u16BITS);
  199. A = (UINT8)(0xff & (u16BITS >> 8));
  200. break;
  201. case RR_STYPE_B2G3R3:
  202. u16BITS = *((UINT8*)pSurfaceBits);
  203. R = ((u16BITS>>(3+2)) & 0x07)/7.f;
  204. G = ((u16BITS>> 2) & 0x07)/7.f;
  205. B = ((u16BITS ) & 0x03)/3.f;
  206. A = 1.F;
  207. break;
  208. case RR_STYPE_L4A4:
  209. u16BITS = *((UINT8*)pSurfaceBits);
  210. R = G = B = (u16BITS & 0x0f)/15.f;
  211. A = ((u16BITS>>4) & 0x0f)/15.f;
  212. break;
  213. case RR_STYPE_B2G3R3A8:
  214. u16BITS = *((UINT16*)pSurfaceBits);
  215. R = ((u16BITS>>(3+2)) & 0x07)/7.f;
  216. G = ((u16BITS>> 2) & 0x07)/7.f;
  217. B = ((u16BITS ) & 0x03)/3.f;
  218. A = (UINT8)(0xff & (u16BITS >> 8));
  219. break;
  220. case RR_STYPE_U8V8:
  221. {
  222. INT8 iDU = *(( INT8*)pSurfaceBits+0);
  223. INT8 iDV = *(( INT8*)pSurfaceBits+1);
  224. // signed values are normalized with 2^(N-1), since -2^(N-1) can
  225. // be exactly expressed in N bits
  226. R = (FLOAT)iDU * (1.0F/128.0F); // fDU
  227. G = (FLOAT)iDV * (1.0F/128.0F); // fDV
  228. B = 1.0F; // fL
  229. }
  230. break;
  231. case RR_STYPE_U5V5L6:
  232. {
  233. UINT16 u16BITS = *((UINT16*)pSurfaceBits);
  234. INT8 iDU = (INT8)(u16BITS & 0x1f);
  235. INT8 iDV = (INT8)((u16BITS>>5) & 0x1f);
  236. UINT8 uL = (UINT8)(u16BITS >> 10);
  237. iDU <<= 3; iDU >>= 3; // sign extension
  238. iDV <<= 3; iDV >>= 3;
  239. // signed values are normalized with 2^(N-1), since -2^(N-1) can
  240. // be exactly expressed in N bits
  241. R = (FLOAT)iDU * (1.0F/16.0F); // fDU
  242. G = (FLOAT)iDV * (1.0F/16.0F); // fDV
  243. // the unsigned uL is normalized with 2^N - 1, since this is the
  244. // largest representable value
  245. B = (FLOAT)uL * (1.0F/63.0F); // fL
  246. }
  247. break;
  248. case RR_STYPE_U8V8L8:
  249. {
  250. INT8 iDU = *(( INT8*)pSurfaceBits+0);
  251. INT8 iDV = *(( INT8*)pSurfaceBits+1);
  252. UINT8 uL = *((UINT8*)pSurfaceBits+2);
  253. // signed values are normalized with 2^(N-1), since -2^(N-1) can
  254. // be exactly expressed in N bits
  255. R = (FLOAT)iDU * (1.0F/128.0F); // fDU
  256. G = (FLOAT)iDV * (1.0F/128.0F); // fDV
  257. // the unsigned uL is normalized with 2^N - 1, since this is the
  258. // largest representable value
  259. B = (FLOAT)uL * (1.0F/255.0F); // fL
  260. }
  261. break;
  262. // shadow map texture formats (read only, not needed for ConvertTo)
  263. case RR_STYPE_Z16S0:
  264. {
  265. UINT16 u16BITS = *((UINT16*)pSurfaceBits);
  266. R = 0.0F;
  267. G = (FLOAT)u16BITS * (1.0F/(FLOAT)0xffff);
  268. B = 0.0F;
  269. }
  270. break;
  271. case RR_STYPE_Z24S8:
  272. case RR_STYPE_Z24S4:
  273. {
  274. UINT32 u32BITS = *((UINT32*)pSurfaceBits);
  275. R = 0.0F;
  276. G = (FLOAT)(u32BITS>>8) * (1.0F/(FLOAT)0xffffff);
  277. B = 0.0F;
  278. }
  279. break;
  280. case RR_STYPE_S8Z24:
  281. case RR_STYPE_S4Z24:
  282. {
  283. UINT32 u32BITS = *((UINT32*)pSurfaceBits);
  284. R = 0.0F;
  285. G = (FLOAT)(u32BITS&0x00ffffff) * (1.0F/(FLOAT)0xffffff);
  286. B = 0.0F;
  287. }
  288. break;
  289. case RR_STYPE_Z15S1:
  290. {
  291. UINT16 u16BITS = *((UINT16*)pSurfaceBits);
  292. R = 0.0F;
  293. G = (FLOAT)(u16BITS>>1) * (1.0F/(FLOAT)0x7fff);
  294. B = 0.0F;
  295. }
  296. break;
  297. case RR_STYPE_S1Z15:
  298. {
  299. UINT16 u16BITS = *((UINT16*)pSurfaceBits);
  300. R = 0.0F;
  301. G = (FLOAT)(u16BITS&0x7fff) * (1.0F/(FLOAT)0x7fff);
  302. B = 0.0F;
  303. }
  304. break;
  305. case RR_STYPE_Z32S0:
  306. {
  307. UINT32 u32BITS = *((UINT32*)pSurfaceBits);
  308. R = 0.0F;
  309. G = (FLOAT)u32BITS * (1.0F/(FLOAT)0xffffffff);
  310. B = 0.0F;
  311. }
  312. break;
  313. }
  314. }
  315. // Convert surface type format to RRColor
  316. void ConvertTo( RRSurfaceType Type, float fRoundOffset, char* pSurfaceBits ) const
  317. {
  318. int iR, iG, iB, iA;
  319. switch (Type)
  320. {
  321. case RR_STYPE_B8G8R8A8:
  322. *((UINT8*)pSurfaceBits+0) = (UINT8)((FLOAT)B * 255. + fRoundOffset);
  323. *((UINT8*)pSurfaceBits+1) = (UINT8)((FLOAT)G * 255. + fRoundOffset);
  324. *((UINT8*)pSurfaceBits+2) = (UINT8)((FLOAT)R * 255. + fRoundOffset);
  325. *((UINT8*)pSurfaceBits+3) = (UINT8)((FLOAT)A * 255. + fRoundOffset);
  326. break;
  327. case RR_STYPE_B8G8R8X8:
  328. *((UINT8*)pSurfaceBits+0) = (UINT8)((FLOAT)B * 255. + fRoundOffset);
  329. *((UINT8*)pSurfaceBits+1) = (UINT8)((FLOAT)G * 255. + fRoundOffset);
  330. *((UINT8*)pSurfaceBits+2) = (UINT8)((FLOAT)R * 255. + fRoundOffset);
  331. *((UINT8*)pSurfaceBits+3) = 0x00;
  332. break;
  333. case RR_STYPE_B8G8R8:
  334. *((UINT8*)pSurfaceBits+0) = (UINT8)((FLOAT)B * 255. + fRoundOffset);
  335. *((UINT8*)pSurfaceBits+1) = (UINT8)((FLOAT)G * 255. + fRoundOffset);
  336. *((UINT8*)pSurfaceBits+2) = (UINT8)((FLOAT)R * 255. + fRoundOffset);
  337. break;
  338. case RR_STYPE_B4G4R4A4:
  339. iA = (FLOAT)A * 15. + fRoundOffset;
  340. iR = (FLOAT)R * 15. + fRoundOffset;
  341. iG = (FLOAT)G * 15. + fRoundOffset;
  342. iB = (FLOAT)B * 15. + fRoundOffset;
  343. *((UINT16*)pSurfaceBits) = (iA<<12) | (iR<<8) | (iG<<4) | iB;
  344. break;
  345. case RR_STYPE_B5G6R5:
  346. iR = (FLOAT)R * 31. + fRoundOffset; // apply rounding bias then truncate
  347. iG = (FLOAT)G * 63. + fRoundOffset;
  348. iB = (FLOAT)B * 31. + fRoundOffset;
  349. *((UINT16*)pSurfaceBits) = (iR<<11) | (iG<<5) | iB;
  350. break;
  351. case RR_STYPE_B5G5R5A1:
  352. iA = (FLOAT)A * 1. + fRoundOffset;
  353. iR = (FLOAT)R * 31. + fRoundOffset;
  354. iG = (FLOAT)G * 31. + fRoundOffset;
  355. iB = (FLOAT)B * 31. + fRoundOffset;
  356. *((UINT16*)pSurfaceBits) = (iA<<15) | (iR<<10) | (iG<<5) | iB;
  357. break;
  358. case RR_STYPE_B5G5R5:
  359. iR = (FLOAT)R * 31. + fRoundOffset;
  360. iG = (FLOAT)G * 31. + fRoundOffset;
  361. iB = (FLOAT)B * 31. + fRoundOffset;
  362. *((UINT16*)pSurfaceBits) = (iR<<10) | (iG<<5) | iB;
  363. break;
  364. case RR_STYPE_B2G3R3:
  365. iR = (FLOAT)R * 7. + fRoundOffset;
  366. iG = (FLOAT)G * 7. + fRoundOffset;
  367. iB = (FLOAT)B * 3. + fRoundOffset;
  368. *((UINT8*)pSurfaceBits) = (iR<<5) | (iG<<2) | iB;
  369. break;
  370. }
  371. }
  372. };
  373. //-----------------------------------------------------------------------------
  374. //
  375. // RRDepth - Class for storing and manipulating pixel depth values. Underlying
  376. // storage is a double precision floating point, which has sufficient precision
  377. // and range to support 16 and 32 bit fixed point and 32 bit floating point.
  378. //
  379. // The UINT32 methods receive a 24 or 32 bit value, and the UINT16
  380. // methods receive a 15 or 16 bit value.
  381. //
  382. //-----------------------------------------------------------------------------
  383. class RRDepth
  384. {
  385. DOUBLE m_dVal;
  386. RRSurfaceType m_DepthSType;
  387. DOUBLE dGetValClamped(void) const { return min(1.,max(0.,m_dVal)); }
  388. DOUBLE dGetCnvScale(void) const
  389. {
  390. switch(m_DepthSType)
  391. {
  392. case RR_STYPE_Z16S0:
  393. return DOUBLE((1<<16)-1);
  394. case RR_STYPE_Z24S8:
  395. case RR_STYPE_S8Z24:
  396. case RR_STYPE_Z24S4:
  397. case RR_STYPE_S4Z24:
  398. return DOUBLE((1<<24)-1);
  399. case RR_STYPE_Z15S1:
  400. case RR_STYPE_S1Z15:
  401. return DOUBLE((1<<15)-1);
  402. case RR_STYPE_Z32S0:
  403. return DOUBLE(0xffffffff); // too big to be generated as above without INT64's
  404. default:
  405. DPFRR(0, "RRDepth not initialized correctly");
  406. return DOUBLE(0.0);
  407. }
  408. }
  409. public:
  410. // default and UINT16/32 assignment constructor
  411. // default only for Pixel class, and requires that SetSType be called later
  412. RRDepth() : m_dVal(0.F), m_DepthSType(RR_STYPE_NULL) {;}
  413. RRDepth(RRSurfaceType SType) : m_dVal(0.F), m_DepthSType(SType) {;}
  414. RRDepth(UINT16 uVal, RRSurfaceType SType): m_DepthSType(SType), m_dVal((DOUBLE)uVal/dGetCnvScale()) {;}
  415. RRDepth(UINT32 uVal, RRSurfaceType SType): m_DepthSType(SType), m_dVal((DOUBLE)uVal/dGetCnvScale()) {;}
  416. // copy and assignment operators
  417. RRDepth& operator=(const RRDepth& A) { m_dVal = A.m_dVal; m_DepthSType = A.m_DepthSType; return *this; }
  418. RRDepth& operator=(UINT16 uVal) { m_dVal = (DOUBLE)uVal/dGetCnvScale(); return *this; }
  419. RRDepth& operator=(UINT32 uVal) { m_dVal = (DOUBLE)uVal/dGetCnvScale(); return *this; }
  420. RRDepth& operator=(FLOAT fVal) { m_dVal = (DOUBLE)fVal; return *this; }
  421. // round for integer get operations
  422. operator UINT16() const { return (UINT16)( (dGetValClamped()*dGetCnvScale()) + .5); }
  423. operator UINT32() const { return (UINT32)( (dGetValClamped()*dGetCnvScale()) + .5); }
  424. operator DOUBLE() const { return dGetValClamped(); }
  425. operator FLOAT() const { return (FLOAT)dGetValClamped(); }
  426. void SetSType(RRSurfaceType SType) { m_DepthSType = SType; }
  427. RRSurfaceType GetSType(void) const { return m_DepthSType; }
  428. };
  429. //-----------------------------------------------------------------------------
  430. //
  431. // RRPixel - Class for encapsulation of all pixel information passed from
  432. // scan conversion to pixel and fragment processing.
  433. //
  434. //-----------------------------------------------------------------------------
  435. class RRPixel
  436. {
  437. public:
  438. INT16 iX; // pixel location
  439. INT16 iY; //
  440. RRColor Color; // pixel diffuse color
  441. RRColor Specular; // pixel specular color (rgb only - alpha unused)
  442. RRDepth Depth; // pixel depth
  443. FLOAT fW; // pixel W value (unnormalized)
  444. RRColorComp FogIntensity; // fog intensity (scalar)
  445. RRCvgMask CvgMask; // coverage mask
  446. };
  447. ///////////////////////////////////////////////////////////////////////////////
  448. // //
  449. // Setup & Scan Convert //
  450. // //
  451. ///////////////////////////////////////////////////////////////////////////////
  452. //-----------------------------------------------------------------------------
  453. //
  454. // Scan Converter State - Holds input and current state of scan converter.
  455. // Filled in by setup.
  456. //
  457. //-----------------------------------------------------------------------------
  458. struct _RRSCANCNVSTATE
  459. {
  460. // primitive vertex data
  461. FLOAT fX0, fY0, fRHW0;
  462. FLOAT fX1, fY1, fRHW1;
  463. FLOAT fX2, fY2, fRHW2;
  464. // primitive transformed texture coord data
  465. FLOAT fTexCoord[D3DHAL_TSS_MAXSTAGES][3][4];
  466. FLOAT fRHQW[D3DHAL_TSS_MAXSTAGES][3];
  467. BOOL bWrap[D3DHAL_TSS_MAXSTAGES][4];
  468. // x,y deltas
  469. FLOAT fDelX10, fDelX02, fDelX21;
  470. FLOAT fDelY01, fDelY20, fDelY12;
  471. // triangle edge functions and gradient data
  472. RREdgeFunc EdgeFuncs[4]; // A,B,C values and A,B sign bits for 4 edges
  473. // the fourth edge is only used to scan convert points
  474. // triangle bounding box
  475. INT16 iXMin, iXMax;
  476. INT16 iYMin, iYMax;
  477. // line drawing data
  478. INT64 iLineEdgeFunc[3]; // line function: Pminor = ([0]*Pmajor + [1])/[2]
  479. BOOL bXMajor; // TRUE if X major; else Y major
  480. INT16 iLineMin, iLineMax; // min and max pixel extent in major direction
  481. INT16 iLineStep; // +1 or -1 depending on line major direction
  482. // depth range for primitive (for clamp when sampling outside primitive area)
  483. // may be Z or W
  484. FLOAT fDepthMin, fDepthMax;
  485. //
  486. // attribute functions - static (per-primitive) data, non-texture,
  487. // and texture functions
  488. //
  489. RRAttribFuncStatic AttribFuncStatic;
  490. #define ATTRFUNC_R 0
  491. #define ATTRFUNC_G 1
  492. #define ATTRFUNC_B 2
  493. #define ATTRFUNC_A 3
  494. #define ATTRFUNC_SR 4
  495. #define ATTRFUNC_SG 5
  496. #define ATTRFUNC_SB 6
  497. #define ATTRFUNC_SA 7
  498. #define ATTRFUNC_F 8
  499. #define ATTRFUNC_Z 9
  500. #define RR_N_ATTRIBS 10
  501. RRAttribFunc AttribFuncs[RR_N_ATTRIBS];
  502. #define TEXFUNC_0 0
  503. #define TEXFUNC_1 1
  504. #define TEXFUNC_2 2
  505. #define TEXFUNC_3 3
  506. #define RR_N_TEX_ATTRIBS 4
  507. RRAttribFunc TextureFuncs[D3DHAL_TSS_MAXSTAGES][RR_N_TEX_ATTRIBS];
  508. //
  509. // per-pixel data
  510. //
  511. // current position
  512. INT16 iX,iY;
  513. };
  514. //-----------------------------------------------------------------------------
  515. //
  516. // Texture
  517. //
  518. //-----------------------------------------------------------------------------
  519. //
  520. // structure containing texture coordinate and gradient information
  521. // for lookup and filtering
  522. //
  523. class RRTextureCoord
  524. {
  525. public:
  526. FLOAT fU; // texture coordinate
  527. FLOAT fV;
  528. FLOAT fDUDX; // texture gradient dU/dX
  529. FLOAT fDUDY; // texture gradient dU/dY
  530. FLOAT fDVDX; // texture gradient dV/dX
  531. FLOAT fDVDY; // texture gradient dV/dY
  532. };
  533. //
  534. // structure containing normal and gradient information
  535. // for environment map lookup and filtering
  536. //
  537. class RREnvTextureCoord
  538. {
  539. public:
  540. FLOAT fNX; // normal or reflection normal
  541. FLOAT fNY;
  542. FLOAT fNZ;
  543. // FLOAT fENX; // eye normal
  544. // FLOAT fENY;
  545. // FLOAT fENZ;
  546. FLOAT fDNXDX; // normal gradient dNX/dX
  547. FLOAT fDNXDY; // normal gradient dNX/dY
  548. FLOAT fDNYDX; // normal gradient dNY/dX
  549. FLOAT fDNYDY; // normal gradient dNY/dY
  550. FLOAT fDNZDX; // normal gradient dNZ/dX
  551. FLOAT fDNZDY; // normal gradient dNZ/dY
  552. };
  553. //
  554. // routines to compute level of detail (texel->pixel coverage)
  555. //
  556. // (texfilt.cpp)
  557. void
  558. ComputeSimpleLevelOfDetail(
  559. const RRTextureCoord& TCoord, // inputs
  560. FLOAT& fLOD ); // outputs
  561. void
  562. ComputeEnvMapLevelOfDetail(
  563. const RRTextureCoord& TCoord, // inputs
  564. FLOAT& fLOD ); // outputs
  565. void
  566. ComputeAnisotropicLevelOfDetail(
  567. const RRTextureCoord& TCoord, FLOAT fMaxAniso, // inputs
  568. FLOAT& fLOD, FLOAT& fRatio, FLOAT fDelta[] ); // outputs
  569. ///////////////////////////////////////////////////////////////////////////////
  570. // //
  571. // Pixel Engine //
  572. // //
  573. ///////////////////////////////////////////////////////////////////////////////
  574. // coverage mask values
  575. #define TL_CVGFULL 0xffff
  576. #define TL_CVGZERO 0x0000
  577. #define TL_CVGBITS 16
  578. #define TL_CVGBITSm 4
  579. typedef UINT16 CVGMASK;
  580. // fragment - fragmented pixels have linked lists of these
  581. struct _RRFRAGMENT
  582. {
  583. RRColor Color;
  584. RRDepth Depth;
  585. CVGMASK CvgMask;
  586. void* pNext;
  587. };
  588. int CountFrags(RRFRAGMENT* pFrag);
  589. void DPFFrags(RRFRAGMENT* pFrag);
  590. //-----------------------------------------------------------------------------
  591. //
  592. // Fragment Resolution Accumulator - accumulates fragments presented in
  593. // front-to-back order; does fully correct transparency computations for
  594. // non-opaque fragments
  595. //
  596. //-----------------------------------------------------------------------------
  597. class FragResolveAccum
  598. {
  599. public:
  600. // coverage accumulation array - holds up to CVGBITS different
  601. // alpha values and associated coverage masks; UsageMask indicates
  602. // which entries are currently in use - each set bit in Usage mask
  603. // indicates that the array entry corresponding to the index of the
  604. // that bit holds a valid mask and alpha;
  605. //
  606. // the general idea here is that, in cases in which there are few
  607. // fragments, numerous sample locations (within the pixel) will have
  608. // the same alpha value, and thus can be grouped for the accumulation
  609. struct {
  610. CVGMASK Mask;
  611. FLOAT fAlpha;
  612. } m_CvgArray[TL_CVGBITS];
  613. CVGMASK m_ArrayUsageMask;
  614. // accumulated color and alpha value
  615. FLOAT m_fA, m_fR, m_fG, m_fB;
  616. // mask where set bit indicates a subpixel with opaque alpha
  617. CVGMASK m_CvgOpaqueMask;
  618. // reset before each use...
  619. void Reset( void );
  620. // accumulate new fragment (front to back) - returns TRUE if
  621. // full coverage achieved, FALSE otherwise
  622. BOOL Accum( const CVGMASK CvgMask, const RRColor& Color );
  623. // get RRColor from accumulator
  624. void GetColor( RRColor& Color );
  625. };
  626. //-----------------------------------------------------------------------------
  627. //
  628. // statistics
  629. //
  630. //-----------------------------------------------------------------------------
  631. struct _RRSTATS
  632. {
  633. INT32 cFragsAllocd;
  634. INT32 cMaxFragsAllocd;
  635. INT32 cFragsMerged;
  636. INT32 cFragsMergedToFull;
  637. };
  638. //-----------------------------------------------------------------------------
  639. //
  640. // utilities
  641. //
  642. //-----------------------------------------------------------------------------
  643. // compute pixel address from base, pitch, and surface type
  644. char*
  645. PixelAddress( int iX, int iY, char* pBits, int iYPitch, RRSurfaceType SType );
  646. //-----------------------------------------------------------------------------
  647. //
  648. // color interpolation utilities
  649. //
  650. //-----------------------------------------------------------------------------
  651. void LerpColor(RRColor& Color,
  652. const RRColor& Color0, const RRColor& Color1, UINT8 uT);
  653. void BiLerpColor( RRColor& OutColor,
  654. const RRColor& Color00, const RRColor& Color01,
  655. const RRColor& Color10, const RRColor& Color11,
  656. UINT8 uA, UINT8 uB);
  657. //-----------------------------------------------------------------------------
  658. //
  659. // Globals
  660. //
  661. //-----------------------------------------------------------------------------
  662. // something to experiment with sometime - this sets the threshold at which
  663. // pixel samples are considered opaque (and thus don't generate fragment
  664. // buffer entries when doing D3DRENDERSTATE_TRANSLUCENTSORTINDEPENDENT);
  665. // no need to have all those 0xfe's generate fragments...
  666. extern UINT8 g_uTransparencyAlphaThreshold;
  667. //-----------------------------------------------------------------------------
  668. //
  669. // One special legacy texture op we can't easily map into the new texture
  670. // ops.
  671. //
  672. //-----------------------------------------------------------------------------
  673. #define D3DTOP_LEGACY_ALPHAOVR (0x7fffffff)
  674. ///////////////////////////////////////////////////////////////////////////////
  675. #endif // _REFRASTI_HPP