Source code of Windows XP (NT5)
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.

461 lines
15 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Copyright (C) Microsoft Corporation, 2000.
  3. //
  4. // refrast.cpp
  5. //
  6. // Direct3D Reference Device - rasterizer miscellaneous
  7. //
  8. ///////////////////////////////////////////////////////////////////////////////
  9. #include "pch.cpp"
  10. #pragma hdrstop
  11. ///////////////////////////////////////////////////////////////////////////////
  12. // //
  13. // RDColor //
  14. // //
  15. ///////////////////////////////////////////////////////////////////////////////
  16. void
  17. RDColor::ConvertFrom( RDSurfaceFormat Type, const char* pSurfaceBits )
  18. {
  19. UINT16 u16BITS;
  20. UINT8 u8BITS;
  21. switch (Type)
  22. {
  23. default:
  24. case RD_SF_NULL: return;
  25. case RD_SF_B8G8R8A8: *this = *((UINT32*)pSurfaceBits); break;
  26. case RD_SF_B8G8R8X8: *this = *((UINT32*)pSurfaceBits); A = 1.F; break;
  27. case RD_SF_B5G6R5:
  28. u16BITS = *((UINT16*)pSurfaceBits);
  29. R = ((u16BITS>>(6+5)) & 0x001F)/31.f;
  30. G = ((u16BITS>> 5) & 0x003F)/63.f;
  31. B = ((u16BITS ) & 0x001F)/31.f;
  32. A = 1.F;
  33. break;
  34. case RD_SF_B5G5R5X1:
  35. u16BITS = *((UINT16*)pSurfaceBits);
  36. R = ((u16BITS>>(5+5)) & 0x001F)/31.f;
  37. G = ((u16BITS>> 5) & 0x001F)/31.f;
  38. B = ((u16BITS ) & 0x001F)/31.f;
  39. A = 1.F;
  40. break;
  41. case RD_SF_B5G5R5A1:
  42. u16BITS = *((UINT16*)pSurfaceBits);
  43. R = ((u16BITS>>(5+5)) & 0x001F)/31.f;
  44. G = ((u16BITS>> 5) & 0x001F)/31.f;
  45. B = ((u16BITS ) & 0x001F)/31.f;
  46. A = ( u16BITS & 0x8000 ) ? 1.f : 0.f;
  47. break;
  48. case RD_SF_B4G4R4A4:
  49. u16BITS = *((UINT16*)pSurfaceBits);
  50. R = ((u16BITS>> (4+4)) & 0x000F)/15.f;
  51. G = ((u16BITS>> (4)) & 0x000F)/15.f;
  52. B = ((u16BITS ) & 0x000F)/15.f;
  53. A = ((u16BITS>>(4+4+4)) & 0x000F)/15.f;
  54. break;
  55. case RD_SF_B4G4R4X4:
  56. u16BITS = *((UINT16*)pSurfaceBits);
  57. R = ((u16BITS>>(4+4)) & 0x000F)/15.f;
  58. G = ((u16BITS>> (4)) & 0x000F)/15.f;
  59. B = ((u16BITS ) & 0x000F)/15.f;
  60. A = 1.f;
  61. break;
  62. case RD_SF_B8G8R8:
  63. R = *((UINT8*)pSurfaceBits+2)/255.f;
  64. G = *((UINT8*)pSurfaceBits+1)/255.f;
  65. B = *((UINT8*)pSurfaceBits+0)/255.f;
  66. A = 1.F;
  67. break;
  68. case RD_SF_L8:
  69. R = G = B = *((UINT8*)pSurfaceBits)/255.f;
  70. A = 1.F;
  71. break;
  72. case RD_SF_L8A8:
  73. u16BITS = *((UINT16*)pSurfaceBits);
  74. R = G = B = (UINT8)(0xff & u16BITS)/255.f;
  75. A = (UINT8)(0xff & (u16BITS >> 8))/255.f;
  76. break;
  77. case RD_SF_A8:
  78. R = G = B = 0.f;
  79. A = *((UINT8*)pSurfaceBits)/255.f;
  80. break;
  81. case RD_SF_B2G3R3:
  82. u8BITS = *((UINT8*)pSurfaceBits);
  83. R = ((u8BITS>>(3+2)) & 0x07)/7.f;
  84. G = ((u8BITS>> 2) & 0x07)/7.f;
  85. B = ((u8BITS ) & 0x03)/3.f;
  86. A = 1.F;
  87. break;
  88. case RD_SF_L4A4:
  89. u16BITS = *((UINT8*)pSurfaceBits);
  90. R = G = B = (u16BITS & 0x0f)/15.f;
  91. A = ((u16BITS>>4) & 0x0f)/15.f;
  92. break;
  93. case RD_SF_B2G3R3A8:
  94. u16BITS = *((UINT16*)pSurfaceBits);
  95. R = ((u16BITS>>(3+2)) & 0x07)/7.f;
  96. G = ((u16BITS>> 2) & 0x07)/7.f;
  97. B = ((u16BITS ) & 0x03)/3.f;
  98. A = (UINT8)(0xff & (u16BITS >> 8))/255.f;
  99. break;
  100. case RD_SF_U8V8:
  101. {
  102. INT8 iDU = *(( INT8*)pSurfaceBits+0);
  103. INT8 iDV = *(( INT8*)pSurfaceBits+1);
  104. R = CLAMP_SIGNED8(iDU); // fDU
  105. G = CLAMP_SIGNED8(iDV); // fDV
  106. B = 1.0F; // fL
  107. A = 1.F;
  108. }
  109. break;
  110. case RD_SF_U16V16:
  111. {
  112. INT16 iDU = *(( INT16*)pSurfaceBits+0);
  113. INT16 iDV = *(( INT16*)pSurfaceBits+1);
  114. R = CLAMP_SIGNED16(iDU); // fDU
  115. G = CLAMP_SIGNED16(iDV); // fDV
  116. B = 1.0f; // 1.0 here is intentional
  117. A = 1.0f;
  118. }
  119. break;
  120. case RD_SF_U5V5L6:
  121. {
  122. UINT16 u16BITS = *((UINT16*)pSurfaceBits);
  123. INT8 iDU = (INT8)(u16BITS & 0x1f);
  124. INT8 iDV = (INT8)((u16BITS>>5) & 0x1f);
  125. UINT8 uL = (UINT8)(u16BITS >> 10);
  126. R = CLAMP_SIGNED5(iDU); // fDU
  127. G = CLAMP_SIGNED5(iDV); // fDV
  128. // the unsigned uL is normalized with 2^N - 1, since this is the
  129. // largest representable value
  130. B = (FLOAT)uL * (1.0F/63.0F); // fL
  131. A = 1.0f;
  132. }
  133. break;
  134. case RD_SF_U8V8L8X8:
  135. {
  136. INT8 iDU = *(( INT8*)pSurfaceBits+0);
  137. INT8 iDV = *(( INT8*)pSurfaceBits+1);
  138. UINT8 uL = *((UINT8*)pSurfaceBits+2);
  139. R = CLAMP_SIGNED8(iDU); // fDU
  140. G = CLAMP_SIGNED8(iDV); // fDV
  141. // the unsigned uL is normalized with 2^N - 1, since this is the
  142. // largest representable value
  143. B = (FLOAT)uL * (1.0F/255.0F); // fL
  144. A = 1.0f;
  145. }
  146. break;
  147. case RD_SF_U8V8W8Q8:
  148. {
  149. INT8 iDU = *(( INT8*)pSurfaceBits+0);
  150. INT8 iDV = *(( INT8*)pSurfaceBits+1);
  151. INT8 iDW = *(( INT8*)pSurfaceBits+2);
  152. INT8 iDQ = *(( INT8*)pSurfaceBits+3);
  153. // signed values are normalized with 2^(N-1), since -2^(N-1) can
  154. // be exactly expressed in N bits
  155. R = CLAMP_SIGNED8(iDU); // fDU
  156. G = CLAMP_SIGNED8(iDV); // fDV
  157. B = CLAMP_SIGNED8(iDW); // fDW
  158. A = CLAMP_SIGNED8(iDQ); // fDQ
  159. }
  160. break;
  161. case RD_SF_U10V11W11:
  162. {
  163. UINT32 u32BITS = *((UINT32*)pSurfaceBits);
  164. INT16 iDU = (INT16)((u32BITS>>(0 )) & 0x3FF);
  165. INT16 iDV = (INT16)((u32BITS>>(10 )) & 0x7FF);
  166. INT16 iDW = (INT16)((u32BITS>>(10+11)) & 0x7FF);
  167. // signed values are normalized with 2^(N-1), since -2^(N-1) can
  168. // be exactly expressed in N bits
  169. R = CLAMP_SIGNED10(iDU); // fDU
  170. G = CLAMP_SIGNED11(iDV); // fDV
  171. B = CLAMP_SIGNED11(iDW); // fDW
  172. A = 1.0f;
  173. }
  174. break;
  175. case RD_SF_R10G10B10A2:
  176. {
  177. UINT32 u32BITS = *((UINT32*)pSurfaceBits);
  178. R = ((u32BITS>>(0 )) & 0x3FF)/1023.f;
  179. G = ((u32BITS>>(10 )) & 0x3FF)/1023.f;
  180. B = ((u32BITS>>(10+10)) & 0x3FF)/1023.f;
  181. A = ((u32BITS>>(10+10+10)) & 0x3)/3.f;
  182. }
  183. break;
  184. case RD_SF_R8G8B8A8:
  185. {
  186. R = *(( UINT8*)pSurfaceBits+0)/255.f;
  187. G = *(( UINT8*)pSurfaceBits+1)/255.f;
  188. B = *(( UINT8*)pSurfaceBits+2)/255.f;
  189. A = *(( UINT8*)pSurfaceBits+3)/255.f;
  190. }
  191. break;
  192. case RD_SF_R8G8B8X8:
  193. {
  194. R = *(( UINT8*)pSurfaceBits+0)/255.f;
  195. G = *(( UINT8*)pSurfaceBits+1)/255.f;
  196. B = *(( UINT8*)pSurfaceBits+2)/255.f;
  197. A = 1.f;
  198. }
  199. break;
  200. case RD_SF_R16G16:
  201. {
  202. R = *(( UINT16*)pSurfaceBits+0)/65535.f;
  203. G = *(( UINT16*)pSurfaceBits+1)/65535.f;
  204. B = 1.0f; // 1.0 here is intentional
  205. A = 1.0f;
  206. }
  207. break;
  208. case RD_SF_U11V11W10:
  209. {
  210. UINT32 u32BITS = *((UINT32*)pSurfaceBits);
  211. INT16 iDU = (INT16)((u32BITS>>(0 )) & 0x7FF);
  212. INT16 iDV = (INT16)((u32BITS>>(11 )) & 0x7FF);
  213. INT16 iDW = (INT16)((u32BITS>>(11+11)) & 0x3FF);
  214. // signed values are normalized with 2^(N-1), since -2^(N-1) can
  215. // be exactly expressed in N bits
  216. R = CLAMP_SIGNED11(iDU); // fDU
  217. G = CLAMP_SIGNED11(iDV); // fDV
  218. B = CLAMP_SIGNED10(iDW); // fDW
  219. A = 1.0f;
  220. }
  221. break;
  222. case RD_SF_U10V10W10A2:
  223. {
  224. UINT32 u32BITS = *((UINT32*)pSurfaceBits);
  225. INT16 iDU = (INT16)((u32BITS>>(0 )) & 0x3FF);
  226. INT16 iDV = (INT16)((u32BITS>>(10 )) & 0x3FF);
  227. INT16 iDW = (INT16)((u32BITS>>(10+10)) & 0x3FF);
  228. // signed values are normalized with 2^(N-1), since -2^(N-1) can
  229. // be exactly expressed in N bits
  230. R = CLAMP_SIGNED10(iDU); // fDU
  231. G = CLAMP_SIGNED10(iDV); // fDV
  232. B = CLAMP_SIGNED10(iDW); // fDW
  233. // Note: The A component is treated as an unsigned component
  234. A = ((u32BITS>>(10+10+10)) & 0x3)/3.f;
  235. }
  236. break;
  237. case RD_SF_U8V8X8A8:
  238. {
  239. INT8 iU = *(( INT8*)pSurfaceBits+0);
  240. INT8 iV = *(( INT8*)pSurfaceBits+1);
  241. // signed values are normalized with 2^(N-1), since -2^(N-1) can
  242. // be exactly expressed in N bits
  243. R = CLAMP_SIGNED8(iU);
  244. G = CLAMP_SIGNED8(iV);
  245. B = 1.0f;
  246. // Note: The A component is treated as unsigned
  247. A = *(( INT8*)pSurfaceBits+3)/255.f;
  248. }
  249. break;
  250. case RD_SF_U8V8X8L8:
  251. {
  252. INT8 iU = *(( INT8*)pSurfaceBits+0);
  253. INT8 iV = *(( INT8*)pSurfaceBits+1);
  254. INT8 iL = *(( INT8*)pSurfaceBits+3);
  255. // signed values are normalized with 2^(N-1), since -2^(N-1) can
  256. // be exactly expressed in N bits
  257. R = CLAMP_SIGNED8(iU);
  258. G = CLAMP_SIGNED8(iV);
  259. B = CLAMP_SIGNED8(iL);
  260. A = 1.0f;
  261. }
  262. break;
  263. // shadow map texture formats (read only, not needed for ConvertTo)
  264. case RD_SF_Z16S0:
  265. {
  266. UINT16 u16BITS = *((UINT16*)pSurfaceBits);
  267. R = 0.0F;
  268. G = (FLOAT)u16BITS * (1.0F/(FLOAT)0xffff);
  269. B = 0.0F;
  270. A = 1.0f;
  271. }
  272. break;
  273. case RD_SF_Z24S8:
  274. case RD_SF_Z24X8:
  275. case RD_SF_Z24X4S4:
  276. {
  277. UINT32 u32BITS = *((UINT32*)pSurfaceBits);
  278. R = 0.0F;
  279. G = (FLOAT)(u32BITS>>8) * (1.0F/(FLOAT)0xffffff);
  280. B = 0.0F;
  281. A = 1.0f;
  282. }
  283. break;
  284. case RD_SF_S8Z24:
  285. case RD_SF_X8Z24:
  286. case RD_SF_X4S4Z24:
  287. {
  288. UINT32 u32BITS = *((UINT32*)pSurfaceBits);
  289. R = 0.0F;
  290. G = (FLOAT)(u32BITS&0x00ffffff) * (1.0F/(FLOAT)0xffffff);
  291. B = 0.0F;
  292. A = 1.0f;
  293. }
  294. break;
  295. case RD_SF_Z15S1:
  296. {
  297. UINT16 u16BITS = *((UINT16*)pSurfaceBits);
  298. R = 0.0F;
  299. G = (FLOAT)(u16BITS>>1) * (1.0F/(FLOAT)0x7fff);
  300. B = 0.0F;
  301. A = 1.0f;
  302. }
  303. break;
  304. case RD_SF_S1Z15:
  305. {
  306. UINT16 u16BITS = *((UINT16*)pSurfaceBits);
  307. R = 0.0F;
  308. G = (FLOAT)(u16BITS&0x7fff) * (1.0F/(FLOAT)0x7fff);
  309. B = 0.0F;
  310. A = 1.0f;
  311. }
  312. break;
  313. case RD_SF_Z32S0:
  314. {
  315. UINT32 u32BITS = *((UINT32*)pSurfaceBits);
  316. R = 0.0F;
  317. G = (FLOAT)u32BITS * (1.0F/(FLOAT)0xffffffff);
  318. B = 0.0F;
  319. A = 1.0f;
  320. }
  321. break;
  322. }
  323. }
  324. // Convert surface type format to RDColor
  325. void
  326. RDColor::ConvertTo( RDSurfaceFormat Type, float fRoundOffset, char* pSurfaceBits ) const
  327. {
  328. int iR, iG, iB, iA;
  329. switch (Type)
  330. {
  331. case RD_SF_B8G8R8A8:
  332. *((UINT8*)pSurfaceBits+0) = (UINT8)((FLOAT)B * 255. + fRoundOffset);
  333. *((UINT8*)pSurfaceBits+1) = (UINT8)((FLOAT)G * 255. + fRoundOffset);
  334. *((UINT8*)pSurfaceBits+2) = (UINT8)((FLOAT)R * 255. + fRoundOffset);
  335. *((UINT8*)pSurfaceBits+3) = (UINT8)((FLOAT)A * 255. + fRoundOffset);
  336. break;
  337. case RD_SF_B8G8R8X8:
  338. *((UINT8*)pSurfaceBits+0) = (UINT8)((FLOAT)B * 255. + fRoundOffset);
  339. *((UINT8*)pSurfaceBits+1) = (UINT8)((FLOAT)G * 255. + fRoundOffset);
  340. *((UINT8*)pSurfaceBits+2) = (UINT8)((FLOAT)R * 255. + fRoundOffset);
  341. *((UINT8*)pSurfaceBits+3) = 0x00;
  342. break;
  343. case RD_SF_B8G8R8:
  344. *((UINT8*)pSurfaceBits+0) = (UINT8)((FLOAT)B * 255. + fRoundOffset);
  345. *((UINT8*)pSurfaceBits+1) = (UINT8)((FLOAT)G * 255. + fRoundOffset);
  346. *((UINT8*)pSurfaceBits+2) = (UINT8)((FLOAT)R * 255. + fRoundOffset);
  347. break;
  348. case RD_SF_B4G4R4A4:
  349. iA = (FLOAT)A * 15. + fRoundOffset;
  350. iR = (FLOAT)R * 15. + fRoundOffset;
  351. iG = (FLOAT)G * 15. + fRoundOffset;
  352. iB = (FLOAT)B * 15. + fRoundOffset;
  353. *((UINT16*)pSurfaceBits) = (iA<<12) | (iR<<8) | (iG<<4) | iB;
  354. break;
  355. case RD_SF_B4G4R4X4:
  356. iR = (FLOAT)R * 15. + fRoundOffset;
  357. iG = (FLOAT)G * 15. + fRoundOffset;
  358. iB = (FLOAT)B * 15. + fRoundOffset;
  359. *((UINT16*)pSurfaceBits) = (0x00<<12) | (iR<<8) | (iG<<4) | iB;
  360. break;
  361. case RD_SF_B5G6R5:
  362. iR = (FLOAT)R * 31. + fRoundOffset; // apply rounding bias then truncate
  363. iG = (FLOAT)G * 63. + fRoundOffset;
  364. iB = (FLOAT)B * 31. + fRoundOffset;
  365. *((UINT16*)pSurfaceBits) = (iR<<11) | (iG<<5) | iB;
  366. break;
  367. case RD_SF_B5G5R5A1:
  368. iA = (FLOAT)A * 1. + fRoundOffset;
  369. iR = (FLOAT)R * 31. + fRoundOffset;
  370. iG = (FLOAT)G * 31. + fRoundOffset;
  371. iB = (FLOAT)B * 31. + fRoundOffset;
  372. *((UINT16*)pSurfaceBits) = (iA<<15) | (iR<<10) | (iG<<5) | iB;
  373. break;
  374. case RD_SF_B5G5R5X1:
  375. iR = (FLOAT)R * 31. + fRoundOffset;
  376. iG = (FLOAT)G * 31. + fRoundOffset;
  377. iB = (FLOAT)B * 31. + fRoundOffset;
  378. *((UINT16*)pSurfaceBits) = (iR<<10) | (iG<<5) | iB;
  379. break;
  380. case RD_SF_B2G3R3:
  381. iR = (FLOAT)R * 7. + fRoundOffset;
  382. iG = (FLOAT)G * 7. + fRoundOffset;
  383. iB = (FLOAT)B * 3. + fRoundOffset;
  384. *((UINT8*)pSurfaceBits) = (iR<<5) | (iG<<2) | iB;
  385. break;
  386. case RD_SF_B2G3R3A8:
  387. iA = (FLOAT)A * 255. + fRoundOffset;
  388. iR = (FLOAT)R * 7. + fRoundOffset;
  389. iG = (FLOAT)G * 7. + fRoundOffset;
  390. iB = (FLOAT)B * 3. + fRoundOffset;
  391. *((UINT16*)pSurfaceBits) = (iA<<8) | (iR<<5) | (iG<<2) | iB;
  392. break;
  393. case RD_SF_R10G10B10A2:
  394. iR = (FLOAT)R * 1023.f + fRoundOffset;
  395. iG = (FLOAT)G * 1023.f + fRoundOffset;
  396. iB = (FLOAT)B * 1023.f + fRoundOffset;
  397. iA = (FLOAT)A * 3.f + fRoundOffset;
  398. *((UINT32*)pSurfaceBits) = (iA<<(10+10+10)) | (iB<<(10+10)) | (iG<<10) | iR;
  399. break;
  400. case RD_SF_R8G8B8A8:
  401. *((UINT8*)pSurfaceBits+0) = (UINT8)((FLOAT)R * 255. + fRoundOffset);
  402. *((UINT8*)pSurfaceBits+1) = (UINT8)((FLOAT)G * 255. + fRoundOffset);
  403. *((UINT8*)pSurfaceBits+2) = (UINT8)((FLOAT)B * 255. + fRoundOffset);
  404. *((UINT8*)pSurfaceBits+3) = (UINT8)((FLOAT)A * 255. + fRoundOffset);
  405. break;
  406. case RD_SF_R8G8B8X8:
  407. *((UINT8*)pSurfaceBits+0) = (UINT8)((FLOAT)R * 255. + fRoundOffset);
  408. *((UINT8*)pSurfaceBits+1) = (UINT8)((FLOAT)G * 255. + fRoundOffset);
  409. *((UINT8*)pSurfaceBits+2) = (UINT8)((FLOAT)B * 255. + fRoundOffset);
  410. *((UINT8*)pSurfaceBits+3) = 0x00;
  411. break;
  412. }
  413. }
  414. ///////////////////////////////////////////////////////////////////////////////
  415. // end