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.

414 lines
13 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Copyright (C) Microsoft Corporation, 2000.
  3. //
  4. // setup.cpp
  5. //
  6. // Direct3D Reference Device - Primitive Setup
  7. //
  8. ///////////////////////////////////////////////////////////////////////////////
  9. #include "pch.cpp"
  10. #pragma hdrstop
  11. //-----------------------------------------------------------------------------
  12. //
  13. //-----------------------------------------------------------------------------
  14. // texture coordinates for use in point sprite mode
  15. static const
  16. FLOAT g_PSTexCoord[3][4] =
  17. {
  18. { 0., 0., 0., 1. },
  19. { 1., 0., 0., 1. },
  20. { 0., 1., 0., 1. },
  21. };
  22. void
  23. RefRast::SetAttributeFunctions(
  24. const RDVertex& Vtx0,
  25. const RDVertex& Vtx1,
  26. const RDVertex& Vtx2 )
  27. {
  28. _ASSERT( Vtx0.m_qwFVF, "0x0 FVF code in setup" );
  29. // compute depth function
  30. if ( D3DZB_USEW == m_pRD->GetRS()[D3DRS_ZENABLE] )
  31. {
  32. FLOAT fW0 = 1./Vtx0.GetRHW();
  33. FLOAT fW1 = 1./Vtx1.GetRHW();
  34. FLOAT fW2 = 1./Vtx2.GetRHW();
  35. m_Attr[RDATTR_DEPTH].Setup( &fW0, &fW1, &fW2 );
  36. }
  37. else
  38. {
  39. FLOAT z0 = Vtx0.GetZ();
  40. FLOAT z1 = Vtx1.GetZ();
  41. FLOAT z2 = Vtx2.GetZ();
  42. m_Attr[RDATTR_DEPTH].Setup( &z0, &z1, &z2 );
  43. }
  44. if( m_pRD->GetRS()[D3DRS_SHADEMODE] == D3DSHADE_FLAT )
  45. {
  46. m_Attr[RDATTR_COLOR].m_bFlatShade = TRUE;
  47. m_Attr[RDATTR_SPECULAR].m_bFlatShade = TRUE;
  48. }
  49. else
  50. {
  51. m_Attr[RDATTR_COLOR].m_bFlatShade = FALSE;
  52. m_Attr[RDATTR_SPECULAR].m_bFlatShade = FALSE;
  53. }
  54. // compute color functions
  55. m_Attr[RDATTR_COLOR].Setup(
  56. Vtx0.GetDiffuse(), Vtx1.GetDiffuse(), Vtx2.GetDiffuse() );
  57. m_Attr[RDATTR_SPECULAR].Setup(
  58. Vtx0.GetSpecular(), Vtx1.GetSpecular(), Vtx2.GetSpecular() );
  59. // compute vertex fog function
  60. if ( m_pRD->GetRS()[D3DRS_FOGENABLE] &&
  61. ( m_pRD->GetRS()[D3DRS_FOGTABLEMODE] == D3DFOG_NONE ) )
  62. {
  63. FLOAT fF0 = Vtx0.GetFog();
  64. FLOAT fF1 = Vtx1.GetFog();
  65. FLOAT fF2 = Vtx2.GetFog();
  66. m_Attr[RDATTR_FOG].Setup( &fF0, &fF1, &fF2 );
  67. }
  68. for (INT32 iStage = 0; iStage < D3DHAL_TSS_MAXSTAGES; iStage++)
  69. {
  70. FLOAT TexCrd0[4];
  71. FLOAT TexCrd1[4];
  72. FLOAT TexCrd2[4];
  73. if( !(m_pRD->m_ReferencedTexCoordMask & (1<<iStage) ) )
  74. continue;
  75. if (m_pRD->m_bPointSprite)
  76. {
  77. // set texture coords to map full range
  78. m_Attr[RDATTR_TEXTURE0+iStage].Setup( &g_PSTexCoord[0][0],
  79. &g_PSTexCoord[1][0],
  80. &g_PSTexCoord[2][0]);
  81. }
  82. else
  83. {
  84. // TCI pass through
  85. UINT CoordSet;
  86. if( m_pRD->m_bOverrideTCI )
  87. {
  88. CoordSet = iStage;
  89. }
  90. else
  91. {
  92. CoordSet = (UINT)m_pRD->GetTSS(iStage)[D3DTSS_TEXCOORDINDEX] & 0x0000FFFF;
  93. }
  94. for( UINT i=0; i<4; i++)
  95. {
  96. TexCrd0[i] = Vtx0.GetTexCrd( i, CoordSet );
  97. TexCrd1[i] = Vtx1.GetTexCrd( i, CoordSet );
  98. TexCrd2[i] = Vtx2.GetTexCrd( i, CoordSet );
  99. }
  100. if( m_pRD->GetTSS(iStage)[D3DTSS_TEXTURETRANSFORMFLAGS] &
  101. D3DTTFF_PROJECTED )
  102. {
  103. // Always divide by the 4th coordinate while projecting.
  104. m_Attr[RDATTR_TEXTURE0+iStage].SetProjection(3);
  105. // For the projection, fix up the 4th coordinate
  106. // for the fixed function vertex-shaders.
  107. if( m_pRD->m_pCurrentVShader->IsFixedFunction() )
  108. {
  109. TexCrd0[3] = Vtx0.GetLastTexCrd( CoordSet );
  110. TexCrd1[3] = Vtx1.GetLastTexCrd( CoordSet );
  111. TexCrd2[3] = Vtx2.GetLastTexCrd( CoordSet );
  112. }
  113. }
  114. else
  115. {
  116. m_Attr[RDATTR_TEXTURE0+iStage].SetProjection(0);
  117. }
  118. m_Attr[RDATTR_TEXTURE0+iStage].SetWrapFlags(
  119. m_pRD->GetRS()[D3DRS_WRAP0+CoordSet] );
  120. m_Attr[RDATTR_TEXTURE0+iStage].Setup( TexCrd0, TexCrd1, TexCrd2 );
  121. }
  122. }
  123. }
  124. ///////////////////////////////////////////////////////////////////////////////
  125. //
  126. // Triangle Drawing
  127. //
  128. ///////////////////////////////////////////////////////////////////////////////
  129. //-----------------------------------------------------------------------------
  130. //
  131. // DrawTriangle - Takes three vertices and does triangle setup, setting the
  132. // primitive, attribute, and edge structures which are input to the triangle
  133. // scanner, then invokes the scan conversion.
  134. //
  135. // wFlags - Edge (and other) flags.
  136. //
  137. //-----------------------------------------------------------------------------
  138. void
  139. RefDev::DrawTriangle(
  140. RDVertex* pV0, RDVertex* pV1, RDVertex* pV2, WORD wFlags )
  141. {
  142. UpdateRastState();
  143. // sort to ensure consistent attribute evaluation
  144. // for identical vertices passed in different order
  145. RDVertex* pV[3];
  146. {
  147. FLOAT fD0 = *(pV0->GetPtrXYZ()+0) + *(pV0->GetPtrXYZ()+1);
  148. FLOAT fD1 = *(pV1->GetPtrXYZ()+0) + *(pV1->GetPtrXYZ()+1);
  149. FLOAT fD2 = *(pV2->GetPtrXYZ()+0) + *(pV2->GetPtrXYZ()+1);
  150. UINT MinVtx = 0;
  151. if ( (fD1 < fD0) ) MinVtx = 1;
  152. if ( (fD2 < fD0) && (fD2 < fD1) ) MinVtx = 2;
  153. switch (MinVtx)
  154. {
  155. case 0: pV[0] = pV0; pV[1] = pV1; pV[2] = pV2; m_Rast.m_iFlatVtx = 0; break;
  156. case 1: pV[0] = pV1; pV[1] = pV2; pV[2] = pV0; m_Rast.m_iFlatVtx = 2; break;
  157. case 2: pV[0] = pV2; pV[1] = pV0; pV[2] = pV1; m_Rast.m_iFlatVtx = 1; break;
  158. }
  159. }
  160. if ( m_Rast.PerTriangleSetup(
  161. pV[0]->GetPtrXYZ(), pV[1]->GetPtrXYZ(), pV[2]->GetPtrXYZ(),
  162. m_dwRenderState[D3DRS_CULLMODE],
  163. &(m_pRenderTarget->m_Clip)) )
  164. {
  165. return; // discarded due to degenerate, cull, or no viewport overlap
  166. }
  167. // process point and wireframe fill mode
  168. if ( m_dwRenderState[D3DRS_FILLMODE] == D3DFILL_POINT )
  169. {
  170. if( m_dwRenderState[D3DRS_SHADEMODE] == D3DSHADE_FLAT )
  171. {
  172. // Colors from the first vertex are used for all vertices in flat
  173. // shading mode
  174. RDCOLOR4 diffuse1 = pV1->m_diffuse;
  175. RDCOLOR4 diffuse2 = pV2->m_diffuse;
  176. RDCOLOR4 specular1 = pV1->m_specular;
  177. RDCOLOR4 specular2 = pV2->m_specular;
  178. // Colors are modified in place
  179. pV1->m_diffuse = pV0->m_diffuse;
  180. pV1->m_specular = pV0->m_specular;
  181. pV2->m_diffuse = pV0->m_diffuse;
  182. pV2->m_specular = pV0->m_specular;
  183. DrawPoint( pV0 );
  184. DrawPoint( pV1 );
  185. DrawPoint( pV2 );
  186. // Restore old color values
  187. pV1->m_diffuse = diffuse1;
  188. pV2->m_diffuse = diffuse2;
  189. pV1->m_specular = specular1;
  190. pV2->m_specular = specular2;
  191. }
  192. else
  193. {
  194. DrawPoint( pV0 );
  195. DrawPoint( pV1 );
  196. DrawPoint( pV2 );
  197. }
  198. return;
  199. }
  200. else if ( m_dwRenderState[D3DRS_FILLMODE] == D3DFILL_WIREFRAME )
  201. {
  202. // use original vertex pointers for lines so edge enables line up
  203. if ( wFlags & D3DTRIFLAG_EDGEENABLE1 ) { DrawLine( pV0, pV1, pV0 ); }
  204. if ( wFlags & D3DTRIFLAG_EDGEENABLE2 ) { DrawLine( pV1, pV2, pV0 ); }
  205. if ( wFlags & D3DTRIFLAG_EDGEENABLE3 ) { DrawLine( pV2, pV0, pV0 ); }
  206. return;
  207. }
  208. // compute edge functions
  209. m_Rast.m_Edge[0].Set( m_Rast.m_iDet > 0,
  210. m_Rast.m_iX0, m_Rast.m_iY0, m_Rast.m_iX1, m_Rast.m_iY1 );
  211. m_Rast.m_Edge[1].Set( m_Rast.m_iDet > 0,
  212. m_Rast.m_iX1, m_Rast.m_iY1, m_Rast.m_iX2, m_Rast.m_iY2 );
  213. m_Rast.m_Edge[2].Set( m_Rast.m_iDet > 0,
  214. m_Rast.m_iX2, m_Rast.m_iY2, m_Rast.m_iX0, m_Rast.m_iY0 );
  215. // compute attribute functions
  216. m_Rast.SetAttributeFunctions( *pV[0], *pV[1], *pV[2] );
  217. // not culled, so rasterize it
  218. m_Rast.DoScanCnvTri(3);
  219. }
  220. ///////////////////////////////////////////////////////////////////////////////
  221. //
  222. // Point Drawing
  223. //
  224. ///////////////////////////////////////////////////////////////////////////////
  225. void
  226. RefDev::DrawPoint(
  227. RDVertex* pvV0Public )
  228. {
  229. UpdateRastState();
  230. // copy pointsprite enable for duration of point rendering (only)
  231. m_bPointSprite = m_dwRenderState[D3DRS_POINTSPRITEENABLE];
  232. RDVertex V0, V1, V2;
  233. V0 = V1 = V2 = *pvV0Public;
  234. FLOAT fS = 1.0f;
  235. if (pvV0Public->m_qwFVF & D3DFVF_PSIZE)
  236. {
  237. fS = V0.GetPointSize();
  238. }
  239. else
  240. {
  241. fS = m_fRenderState[D3DRS_POINTSIZE];
  242. }
  243. fS = MAX( m_fRenderState[D3DRS_POINTSIZE_MIN], fS );
  244. fS = MIN( MIN(m_fRenderState[D3DRS_POINTSIZE_MAX], RD_MAX_POINT_SIZE), fS );
  245. // divide point size by 2 to get delta
  246. fS *= .5f;
  247. // Move points based on point size
  248. FLOAT *pXY = V0.GetPtrXYZ();
  249. FLOAT fX3 = pXY[0] + fS;
  250. FLOAT fY3 = pXY[1] + fS;
  251. pXY[0] += -fS;
  252. pXY[1] += -fS;
  253. pXY = V1.GetPtrXYZ();
  254. pXY[0] += fS;
  255. pXY[1] += -fS;
  256. pXY = V2.GetPtrXYZ();
  257. pXY[0] += -fS;
  258. pXY[1] += fS;
  259. if ( m_Rast.PerTriangleSetup(
  260. V0.GetPtrXYZ(), V1.GetPtrXYZ(), V2.GetPtrXYZ(),
  261. D3DCULL_NONE, // never cull points
  262. &(m_pRenderTarget->m_Clip)) )
  263. {
  264. m_bPointSprite = FALSE;
  265. return;
  266. }
  267. // compute edge functions
  268. INT32 iX3 = FloatToNdot4( fX3 );
  269. INT32 iY3 = FloatToNdot4( fY3 );
  270. #define DP_POINT_UL m_Rast.m_iX0, m_Rast.m_iY0 // upper left
  271. #define DP_POINT_UR m_Rast.m_iX1, m_Rast.m_iY1 // upper right
  272. #define DP_POINT_LL m_Rast.m_iX2, m_Rast.m_iY2 // lower left
  273. #define DP_POINT_LR iX3, iY3 // lower right
  274. m_Rast.m_Edge[0].Set( m_Rast.m_iDet > 0, DP_POINT_UL, DP_POINT_UR );
  275. m_Rast.m_Edge[1].Set( m_Rast.m_iDet > 0, DP_POINT_UR, DP_POINT_LR );
  276. m_Rast.m_Edge[2].Set( m_Rast.m_iDet > 0, DP_POINT_LR, DP_POINT_LL );
  277. m_Rast.m_Edge[3].Set( m_Rast.m_iDet > 0, DP_POINT_LL, DP_POINT_UL );
  278. // compute attribute functions
  279. m_Rast.SetAttributeFunctions( V0, V1, V2 );
  280. // not culled, so rasterize it
  281. m_Rast.DoScanCnvTri(4);
  282. m_bPointSprite = FALSE;
  283. return;
  284. }
  285. ///////////////////////////////////////////////////////////////////////////////
  286. //
  287. // Line Drawing
  288. //
  289. ///////////////////////////////////////////////////////////////////////////////
  290. //-----------------------------------------------------------------------------
  291. //
  292. // DrawLine - Takes two vertices and draws a line.
  293. //
  294. //-----------------------------------------------------------------------------
  295. void
  296. RefDev::DrawLine(
  297. RDVertex* pV0, RDVertex* pV1, RDVertex* pVFlat )
  298. {
  299. UpdateRastState();
  300. if( m_Rast.m_SampleCount > 1 )
  301. {
  302. // if multisampling draw quad
  303. D3DVALUE Perp[2];
  304. RDVertex Quad[4];
  305. Perp[0] = pV1->m_pos.y - pV0->m_pos.y;
  306. Perp[1] = -(pV1->m_pos.x - pV0->m_pos.x);
  307. float Length = SQRTF(Perp[0]*Perp[0] + Perp[1]*Perp[1]);
  308. if( 0 == Length )
  309. return;
  310. float Scale = 0.7f / Length; // 0.7 makes the line width 1.4.
  311. // (arbitrary "nice looking" choice)
  312. // Dividing by Length to normalize perp. vector.
  313. Perp[0] *= Scale;
  314. Perp[1] *= Scale;
  315. Quad[0] = *pV0;
  316. Quad[0].m_pos.x -= Perp[0];
  317. Quad[0].m_pos.y -= Perp[1];
  318. Quad[1] = *pV0;
  319. Quad[1].m_pos.x += Perp[0];
  320. Quad[1].m_pos.y += Perp[1];
  321. Quad[2] = *pV1;
  322. Quad[2].m_pos.x += Perp[0];
  323. Quad[2].m_pos.y += Perp[1];
  324. Quad[3] = *pV1;
  325. Quad[3].m_pos.x -= Perp[0];
  326. Quad[3].m_pos.y -= Perp[1];
  327. DWORD dwCull = m_dwRenderState[D3DRS_CULLMODE];
  328. DWORD dwFill = m_dwRenderState[D3DRS_FILLMODE];
  329. m_dwRenderState[D3DRS_CULLMODE] = D3DCULL_NONE;
  330. m_dwRenderState[D3DRS_FILLMODE] = D3DFILL_SOLID;
  331. DrawTriangle(&Quad[0],&Quad[1],&Quad[2],0 );
  332. DrawTriangle(&Quad[0],&Quad[2],&Quad[3],0 );
  333. m_dwRenderState[D3DRS_CULLMODE] = dwCull;
  334. m_dwRenderState[D3DRS_FILLMODE] = dwFill;
  335. return;
  336. }
  337. if ( m_Rast.PerLineSetup(
  338. pV0->GetPtrXYZ(), pV1->GetPtrXYZ(),
  339. m_dwRenderState[D3DRS_LASTPIXEL],
  340. &(m_pRenderTarget->m_Clip)) )
  341. {
  342. return; // discarded due to degenerate or no viewport overlap
  343. }
  344. // compute attribute functions
  345. m_Rast.SetAttributeFunctions( *pV0, *pV1, pVFlat ? (*pVFlat) : (*pV0) );
  346. // rasterize it
  347. m_Rast.DoScanCnvLine();
  348. }
  349. ///////////////////////////////////////////////////////////////////////////////
  350. // end