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.

293 lines
9.6 KiB

  1. #ifndef __CLIPPER_HPP_
  2. #define __CLIPPER_HPP_
  3. //----------------------------------------------------------------------------
  4. // Clipping macros
  5. //----------------------------------------------------------------------------
  6. // Interpolation flags
  7. #define RRCLIP_INTERPOLATE_COLOR (1<< 0)
  8. #define RRCLIP_INTERPOLATE_SPECULAR (1<< 1)
  9. #define RRCLIP_INTERPOLATE_TEXTURE (1<< 2)
  10. #define RRCLIP_INTERPOLATE_S (1<< 3)
  11. #define RRCLIP_INTERPOLATE_EYENORMAL (1<< 4)
  12. #define RRCLIP_INTERPOLATE_EYEXYZ (1<< 5)
  13. // Non guardband clipping bits
  14. #define RRCLIP_LEFT D3DCLIP_LEFT
  15. #define RRCLIP_RIGHT D3DCLIP_RIGHT
  16. #define RRCLIP_TOP D3DCLIP_TOP
  17. #define RRCLIP_BOTTOM D3DCLIP_BOTTOM
  18. #define RRCLIP_FRONT D3DCLIP_FRONT
  19. #define RRCLIP_BACK D3DCLIP_BACK
  20. //----------------------------------------------------------------------------
  21. // User define clip plane bits.
  22. // Each of these bits is set if the vertex is clipped by the associated
  23. // clip plane.
  24. //----------------------------------------------------------------------------
  25. #define RRCLIP_USERCLIPPLANE0 D3DCLIP_GEN0
  26. #define RRCLIP_USERCLIPPLANE1 D3DCLIP_GEN1
  27. #define RRCLIP_USERCLIPPLANE2 D3DCLIP_GEN2
  28. #define RRCLIP_USERCLIPPLANE3 D3DCLIP_GEN3
  29. #define RRCLIP_USERCLIPPLANE4 D3DCLIP_GEN4
  30. #define RRCLIP_USERCLIPPLANE5 D3DCLIP_GEN5
  31. const DWORD RRCLIP_USERPLANES_ALL = (RRCLIP_USERCLIPPLANE0 |
  32. RRCLIP_USERCLIPPLANE1 |
  33. RRCLIP_USERCLIPPLANE2 |
  34. RRCLIP_USERCLIPPLANE3 |
  35. RRCLIP_USERCLIPPLANE4 |
  36. RRCLIP_USERCLIPPLANE5 );
  37. //---------------------------------------------------------------------
  38. // Guard band clipping bits
  39. //
  40. // A guard bit is set when a point is out of guard band
  41. // Guard bits should be cleared before a call to clip a triangle, because
  42. // they are the same as CLIPPED_... bits
  43. //
  44. // Example of clipping bits setting for X coordinate:
  45. //
  46. // if -w < x < w no clipping bit is set
  47. // if -w*ax1 < x <= -w RRCLIP_LEFT bit is set
  48. // if x < -w*ax1 RRCLIPGB_LEFT bit is set
  49. //---------------------------------------------------------------------
  50. #define RRCLIPGB_LEFT (RRCLIP_USERCLIPPLANE5 << 1)
  51. #define RRCLIPGB_RIGHT (RRCLIP_USERCLIPPLANE5 << 2)
  52. #define RRCLIPGB_TOP (RRCLIP_USERCLIPPLANE5 << 3)
  53. #define RRCLIPGB_BOTTOM (RRCLIP_USERCLIPPLANE5 << 4)
  54. #define RRCLIP_ALL (RRCLIP_LEFT | RRCLIP_RIGHT | \
  55. RRCLIP_TOP | RRCLIP_BOTTOM | \
  56. RRCLIP_FRONT | RRCLIP_BACK | \
  57. RRCLIP_USERPLANES_ALL)
  58. #define RRCLIPGB_ALL (RRCLIPGB_LEFT | RRCLIPGB_RIGHT | \
  59. RRCLIPGB_TOP | RRCLIPGB_BOTTOM | \
  60. RRCLIP_FRONT | RRCLIP_BACK | \
  61. RRCLIP_USERPLANES_ALL)
  62. // If only these bits are set, then this point is inside the guard band
  63. #define RRCLIP_INGUARDBAND (RRCLIP_LEFT | RRCLIP_RIGHT | \
  64. RRCLIP_TOP | RRCLIP_BOTTOM)
  65. //---------------------------------------------------------------------
  66. // Bit numbers for each clip flag
  67. //
  68. #define RRCLIP_LEFTBIT 1
  69. #define RRCLIP_RIGHTBIT 2
  70. #define RRCLIP_TOPBIT 3
  71. #define RRCLIP_BOTTOMBIT 4
  72. #define RRCLIP_FRONTBIT 5
  73. #define RRCLIP_BACKBIT 6
  74. #define RRCLIP_USERCLIPLANE0BIT 7
  75. #define RRCLIP_USERCLIPLANE1BIT 8
  76. #define RRCLIP_USERCLIPLANE2BIT 9
  77. #define RRCLIP_USERCLIPLANE3BIT 10
  78. #define RRCLIP_USERCLIPLANE4BIT 11
  79. #define RRCLIP_USERCLIPLANE5BIT 12
  80. #define RRCLIPGB_LEFTBIT 13
  81. #define RRCLIPGB_RIGHTBIT 14
  82. #define RRCLIPGB_TOPBIT 15
  83. #define RRCLIPGB_BOTTOMBIT 16
  84. #define CLIPPED_LEFT (RRCLIP_USERCLIPPLANE5 << 1)
  85. #define CLIPPED_RIGHT (RRCLIP_USERCLIPPLANE5 << 2)
  86. #define CLIPPED_TOP (RRCLIP_USERCLIPPLANE5 << 3)
  87. #define CLIPPED_BOTTOM (RRCLIP_USERCLIPPLANE5 << 4)
  88. #define CLIPPED_FRONT (RRCLIP_USERCLIPPLANE5 << 5)
  89. #define CLIPPED_BACK (RRCLIP_USERCLIPPLANE5 << 6)
  90. #define CLIPPED_ENABLE (RRCLIP_USERCLIPPLANE5 << 7) // wireframe enable flag
  91. #define CLIPPED_ALL (CLIPPED_LEFT|CLIPPED_RIGHT \
  92. |CLIPPED_TOP|CLIPPED_BOTTOM \
  93. |CLIPPED_FRONT|CLIPPED_BACK)
  94. const DWORD CLIPPED_USERCLIPPLANE0 = RRCLIP_USERCLIPPLANE5 << 8;
  95. const DWORD CLIPPED_USERCLIPPLANE1 = RRCLIP_USERCLIPPLANE5 << 9;
  96. const DWORD CLIPPED_USERCLIPPLANE2 = RRCLIP_USERCLIPPLANE5 << 10;
  97. const DWORD CLIPPED_USERCLIPPLANE3 = RRCLIP_USERCLIPPLANE5 << 11;
  98. const DWORD CLIPPED_USERCLIPPLANE4 = RRCLIP_USERCLIPPLANE5 << 12;
  99. const DWORD CLIPPED_USERCLIPPLANE5 = RRCLIP_USERCLIPPLANE5 << 13;
  100. //---------------------------------------------------------------------
  101. // Make clip vertex from D3D vertex
  102. //
  103. // device - DIRECT3DDEVICEI *
  104. // cn - clipVertex
  105. // pVtx - a TL vertex
  106. // qwFVF - FVF of the input TL vertex
  107. //---------------------------------------------------------------------
  108. inline void
  109. MakeClipVertexFromFVF( RRCLIPVTX& cv, LPVOID pVtx,
  110. const RRVIEWPORTDATA& VData,
  111. DWORD dwTexCoordSize,
  112. UINT64 qwFVF, DWORD dwClipFlag,
  113. DWORD dwClipMask)
  114. {
  115. LPBYTE pv = (LPBYTE)pVtx;
  116. // If the clip flag for this vertex is set, that means that the
  117. // transformation loop has not computed the screen coordinates for
  118. // this vertex, it has simply stored the clip coordinates for this
  119. // vertex
  120. #if 0
  121. float x = *(D3DVALUE *)&((DWORD *)pv)[0]
  122. float y = *(D3DVALUE *)&((DWORD *)pv)[1]
  123. float z = *(D3DVALUE *)&((DWORD *)pv)[2]
  124. float w = *(D3DVALUE *)&((DWORD *)pv)[3]
  125. #endif
  126. if (dwClipFlag & dwClipMask)
  127. {
  128. // This is a clipped vertex, simply no screen coordinates
  129. cv.sx = D3DVALUE(0);
  130. cv.sy = D3DVALUE(0);
  131. cv.sz = D3DVALUE(0);
  132. cv.rhw = D3DVALUE(0);
  133. // Since this vertex has been clipped, the transformation loop
  134. // has put in the clip coordinates instead
  135. cv.hx = ((D3DVALUE*)pv)[0];
  136. cv.hy = ((D3DVALUE*)pv)[1];
  137. cv.hz = ((D3DVALUE*)pv)[2];
  138. cv.hw = ((D3DVALUE*)pv)[3];
  139. }
  140. else
  141. {
  142. // This vertex is not clipped, so its screen coordinates have been
  143. // computed
  144. cv.sx = ((D3DVALUE*)pv)[0];
  145. cv.sy = ((D3DVALUE*)pv)[1];
  146. cv.sz = ((D3DVALUE*)pv)[2];
  147. cv.rhw = ((D3DVALUE*)pv)[3];
  148. // Transform the screen coordinate back to the clipping space
  149. cv.hw = 1.0f / cv.rhw;
  150. cv.hx = (cv.sx - VData.offsetX) * cv.hw * VData.scaleXi;
  151. cv.hy = (cv.sy - VData.offsetY) * cv.hw * VData.scaleYi;
  152. cv.hz = (cv.sz - VData.offsetZ) * cv.hw * VData.scaleZi;
  153. }
  154. pv += sizeof(D3DVALUE) * 4;
  155. if (qwFVF & D3DFVF_DIFFUSE)
  156. {
  157. cv.color = *(DWORD*)pv;
  158. pv += sizeof(D3DVALUE);
  159. }
  160. if (qwFVF & D3DFVF_SPECULAR)
  161. {
  162. cv.specular= *(DWORD*)pv;
  163. pv += sizeof(DWORD);
  164. }
  165. memcpy(cv.tex, pv, dwTexCoordSize);
  166. pv += dwTexCoordSize;
  167. if (qwFVF & D3DFVF_S)
  168. {
  169. cv.s= *(FLOAT*)pv;
  170. pv += sizeof(FLOAT);
  171. }
  172. if (qwFVF & D3DFVFP_EYENORMAL)
  173. {
  174. cv.eyenx= *(FLOAT*)pv;
  175. pv += sizeof(FLOAT);
  176. cv.eyeny= *(FLOAT*)pv;
  177. pv += sizeof(FLOAT);
  178. cv.eyenz= *(FLOAT*)pv;
  179. pv += sizeof(FLOAT);
  180. }
  181. if (qwFVF & D3DFVFP_EYEXYZ)
  182. {
  183. cv.eyex= *(FLOAT*)pv;
  184. pv += sizeof(FLOAT);
  185. cv.eyey= *(FLOAT*)pv;
  186. pv += sizeof(FLOAT);
  187. cv.eyez= *(FLOAT*)pv;
  188. pv += sizeof(FLOAT);
  189. }
  190. cv.clip = dwClipFlag;
  191. }
  192. //---------------------------------------------------------------------
  193. // Make TL vertex from clip vertex
  194. //
  195. // device - DIRECT3DDEVICEI *
  196. // in - clipVertex
  197. // out - TL vertex
  198. //---------------------------------------------------------------------
  199. inline void
  200. MakeFVFVertexFromClip(LPVOID out, RRCLIPVTX *cv,
  201. UINT64 qwFVF, DWORD dwTexCoordSize)
  202. {
  203. LPBYTE pv = (LPBYTE)out;
  204. ((D3DVALUE*)pv)[0] = cv->sx;
  205. ((D3DVALUE*)pv)[1] = cv->sy;
  206. ((D3DVALUE*)pv)[2] = cv->sz;
  207. ((D3DVALUE*)pv)[3] = D3DVAL(1)/cv->hw;
  208. pv += sizeof(D3DVALUE)*4;
  209. if (qwFVF & D3DFVF_DIFFUSE)
  210. {
  211. *(DWORD*)pv = cv->color;
  212. pv += sizeof(DWORD);
  213. }
  214. if (qwFVF & D3DFVF_SPECULAR)
  215. {
  216. *(DWORD*)pv = cv->specular;
  217. pv += sizeof(DWORD);
  218. }
  219. memcpy(pv, &cv->tex, dwTexCoordSize);
  220. pv += dwTexCoordSize;
  221. if (qwFVF & D3DFVF_S)
  222. {
  223. *(FLOAT*)pv = cv->s;
  224. pv += sizeof(FLOAT);
  225. }
  226. if (qwFVF & D3DFVFP_EYENORMAL)
  227. {
  228. *(FLOAT*)pv = cv->eyenx;
  229. pv += sizeof(FLOAT);
  230. *(FLOAT*)pv = cv->eyeny;
  231. pv += sizeof(FLOAT);
  232. *(FLOAT*)pv = cv->eyenz;
  233. pv += sizeof(FLOAT);
  234. }
  235. if (qwFVF & D3DFVFP_EYEXYZ)
  236. {
  237. *(FLOAT*)pv = cv->eyex;
  238. pv += sizeof(FLOAT);
  239. *(FLOAT*)pv = cv->eyey;
  240. pv += sizeof(FLOAT);
  241. *(FLOAT*)pv = cv->eyez;
  242. pv += sizeof(FLOAT);
  243. }
  244. }
  245. //---------------------------------------------------------------------
  246. // Returns TRUE if clipping is needed
  247. //---------------------------------------------------------------------
  248. inline BOOL
  249. NeedClipping(BOOL bUseGB, RRCLIPCODE clipUnion)
  250. {
  251. if( bUseGB && (clipUnion & ~RRCLIP_INGUARDBAND) )
  252. {
  253. return TRUE;
  254. }
  255. else if( clipUnion )
  256. {
  257. return TRUE;
  258. }
  259. return FALSE;
  260. }
  261. #endif //__CLIPPER_HPP_