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.

354 lines
11 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Copyright (C) Microsoft Corporation, 1998.
  3. //
  4. // refrasti.cpp
  5. //
  6. // Direct3D Reference Rasterizer - Main Internal Object Methods
  7. //
  8. ///////////////////////////////////////////////////////////////////////////////
  9. #include "pch.cpp"
  10. #pragma hdrstop
  11. //////////////////////////////////////////////////////////////////////////////////
  12. // //
  13. // global controls //
  14. // //
  15. //////////////////////////////////////////////////////////////////////////////////
  16. // alpha needs to be less than this for a pixel to be considered non-opaque
  17. UINT8 g_uTransparencyAlphaThreshold = 0xff;
  18. //////////////////////////////////////////////////////////////////////////////////
  19. // //
  20. // ReferenceRasterizer Methods //
  21. // //
  22. //////////////////////////////////////////////////////////////////////////////////
  23. //-----------------------------------------------------------------------------
  24. //
  25. // Overload new & delete for core object so that it can be allocated from
  26. // caller-controlled pool
  27. //
  28. //-----------------------------------------------------------------------------
  29. void*
  30. ReferenceRasterizer::operator new(size_t)
  31. {
  32. void* pMem = (void*)MEMALLOC( sizeof(ReferenceRasterizer) );
  33. _ASSERTa( NULL != pMem, "malloc failure on RR object", return NULL; );
  34. return pMem;
  35. }
  36. //-----------------------------------------------------------------------------
  37. void
  38. ReferenceRasterizer::operator delete(void* pv,size_t)
  39. {
  40. MEMFREE( pv );
  41. }
  42. //-----------------------------------------------------------------------------
  43. //
  44. // Constructor/Destructor for renderer core object.
  45. //
  46. //-----------------------------------------------------------------------------
  47. ReferenceRasterizer::ReferenceRasterizer( LPDDRAWI_DIRECTDRAW_LCL pDDLcl,
  48. DWORD dwInterfaceType,
  49. RRDEVICETYPE dwDriverType )
  50. {
  51. memset( this, 0, sizeof(*this) );
  52. // allocate scan converter state and statistics
  53. m_pSCS = (RRSCANCNVSTATE*)MEMALLOC( sizeof( *m_pSCS ) );
  54. m_pStt = (RRSTATS*)MEMALLOC( sizeof( *m_pStt ) );
  55. _ASSERTa( ( NULL != m_pSCS ) && ( NULL != m_pStt),
  56. "malloc failure on ReferenceRasterizer object", return; );
  57. // associate the (single) static attribute data structure with each attribute
  58. // function instance
  59. int i, j;
  60. for ( i = 0; i < RR_N_ATTRIBS; i++ )
  61. {
  62. m_pSCS->AttribFuncs[i].SetStaticDataPointer( &(m_pSCS->AttribFuncStatic) );
  63. }
  64. for ( i = 0; i < D3DHAL_TSS_MAXSTAGES; i++ )
  65. {
  66. for( j = 0; j < RR_N_TEX_ATTRIBS; j++)
  67. {
  68. m_pSCS->TextureFuncs[i][j].SetStaticDataPointer( &(m_pSCS->AttribFuncStatic) );
  69. }
  70. }
  71. // this is zero'ed above, so just set the 1.0 elements
  72. // of the identity matrices
  73. //
  74. // 0 1 2 3
  75. // 4 5 6 7
  76. // 8 9 10 11
  77. // 12 13 14 15
  78. //
  79. for ( i = 0; i < D3DHAL_TSS_MAXSTAGES; i++ )
  80. {
  81. m_TextureStageState[i].m_fVal[D3DTSSI_MATRIX+0] = 1.0f;
  82. m_TextureStageState[i].m_fVal[D3DTSSI_MATRIX+5] = 1.0f;
  83. m_TextureStageState[i].m_fVal[D3DTSSI_MATRIX+10] = 1.0f;
  84. m_TextureStageState[i].m_fVal[D3DTSSI_MATRIX+15] = 1.0f;
  85. }
  86. // All render and texture stage state is initialized by
  87. // DIRECT3DDEVICEI::stateInitialize
  88. m_dwInterfaceType = dwInterfaceType;
  89. m_dwDriverType = dwDriverType;
  90. m_pDDLcl = pDDLcl;
  91. // defer allocating and clearing of fragment pointer buffer until fragments
  92. // are actually generated
  93. m_ppFragBuf = NULL;
  94. // Texture handles
  95. m_ppTextureArray = NULL;
  96. m_dwTexArrayLength = 0;
  97. // StateOverride initialize
  98. STATESET_INIT( m_renderstate_override );
  99. // Initialize TL state and Data
  100. InitTLData();
  101. SetSetStateFunctions();
  102. ClearTexturesLocked();
  103. }
  104. //-----------------------------------------------------------------------------
  105. ReferenceRasterizer::~ReferenceRasterizer( void )
  106. {
  107. MEMFREE( m_ppFragBuf );
  108. MEMFREE( m_pSCS);
  109. MEMFREE( m_pStt);
  110. // Clean up statesets
  111. for (DWORD i = 0; i < m_pStateSets.ArraySize(); i++)
  112. {
  113. if (m_pStateSets[i] != NULL)
  114. delete m_pStateSets[i];
  115. }
  116. // Free the Light Array
  117. if (m_pLightArray) delete m_pLightArray;
  118. // Free the Texture Array
  119. for (i = 0; i<m_dwTexArrayLength; i++)
  120. {
  121. RRTexture* pTex = m_ppTextureArray[i];
  122. if (pTex) delete pTex;
  123. }
  124. if (m_ppTextureArray) delete m_ppTextureArray;
  125. }
  126. ///////////////////////////////////////////////////////////////////////////////
  127. // //
  128. // State Management Utilities //
  129. // //
  130. ///////////////////////////////////////////////////////////////////////////////
  131. //-----------------------------------------------------------------------------
  132. //
  133. // MapTextureHandleToDevice - This is called when texture handles change or
  134. // when leaving legacy texture mode. This maps the texture handle embedded
  135. // in the per-stage state to texture object pointers.
  136. //
  137. //-----------------------------------------------------------------------------
  138. void
  139. ReferenceRasterizer::MapTextureHandleToDevice( int iStage )
  140. {
  141. // map one
  142. m_pTexture[iStage] =
  143. MapHandleToTexture( m_TextureStageState[iStage].m_dwVal[D3DTSS_TEXTUREMAP] );
  144. // initialize m_pStageState pointer in texture
  145. if (m_pTexture[iStage])
  146. {
  147. m_pTexture[iStage]->m_pStageState = &m_TextureStageState[0];
  148. }
  149. // update num active stages
  150. UpdateActiveTexStageCount();
  151. }
  152. //-----------------------------------------------------------------------------
  153. //
  154. // GrowTexArray - On DX7.
  155. //
  156. //-----------------------------------------------------------------------------
  157. HRESULT
  158. ReferenceRasterizer::GrowTexArray( DWORD dwTexHandle )
  159. {
  160. DWORD dwNewArraySize = dwTexHandle+16;
  161. RRTexture **ppTmpTexArray =
  162. (RRTexture **)MEMALLOC( dwNewArraySize*sizeof(RRTexture*) );
  163. if (ppTmpTexArray == NULL)
  164. {
  165. return DDERR_OUTOFMEMORY;
  166. }
  167. memset( ppTmpTexArray, 0, dwNewArraySize*sizeof(RRTexture*) );
  168. // Save all the textures
  169. for (DWORD i=0; i<m_dwTexArrayLength; i++)
  170. {
  171. ppTmpTexArray[i] = m_ppTextureArray[i];
  172. }
  173. if (m_ppTextureArray)
  174. {
  175. delete m_ppTextureArray;
  176. }
  177. m_ppTextureArray = ppTmpTexArray;
  178. m_dwTexArrayLength = dwNewArraySize;
  179. return D3D_OK;
  180. }
  181. //-----------------------------------------------------------------------------
  182. //
  183. // SetTextureHandle - On DX7, this is called when a texture handle is set.
  184. // This maps the texture handle embedded in the per-stage state to texture
  185. // object pointers.
  186. //
  187. //-----------------------------------------------------------------------------
  188. HRESULT
  189. ReferenceRasterizer::SetTextureHandle( int iStage, DWORD dwTexHandle )
  190. {
  191. HRESULT hr = D3D_OK;
  192. // Special case, if texture handle == 0, then unmap the texture from the TSS
  193. if (dwTexHandle == 0)
  194. {
  195. m_pTexture[iStage] = NULL;
  196. // update num active stages
  197. UpdateActiveTexStageCount();
  198. return D3D_OK;
  199. }
  200. //
  201. // If the texture handle is greater than the length of the array,
  202. // the array needs to be grown.
  203. //
  204. if (dwTexHandle >= m_dwTexArrayLength)
  205. {
  206. HR_RET(GrowTexArray( dwTexHandle ));
  207. }
  208. // Ask DDraw to decipher what this particular handle meant wrt. to the
  209. // the DDraw_Local associated with this instance of the Refrast
  210. LPDDRAWI_DDRAWSURFACE_LCL pDDSLcl = NULL;
  211. BOOL bIsNew = FALSE;
  212. pDDSLcl = GetDDSurfaceLocal(m_pDDLcl, dwTexHandle, &bIsNew);
  213. //
  214. // If the particular array element is NULL it means that the
  215. // texture has not yet been created.
  216. //
  217. if (m_ppTextureArray[dwTexHandle] == NULL)
  218. {
  219. if (TextureCreate(dwTexHandle, &m_ppTextureArray[dwTexHandle])
  220. == FALSE)
  221. {
  222. return DDERR_OUTOFMEMORY;
  223. }
  224. HR_RET(m_ppTextureArray[dwTexHandle]->Initialize( pDDSLcl ));
  225. }
  226. // This means that the texture bound to the dwHandle is not the
  227. // same as what Refrast thinks it is, hence revalidate everything
  228. else if (bIsNew)
  229. {
  230. HR_RET(m_ppTextureArray[dwTexHandle]->Initialize( pDDSLcl ));
  231. }
  232. // map one
  233. m_pTexture[iStage] = m_ppTextureArray[dwTexHandle];
  234. // initialize m_pStageState pointer in texture
  235. if (m_pTexture[iStage])
  236. {
  237. #if DBG
  238. int iTexCount = 0;
  239. for (int i = 0; i < D3DHAL_TSS_MAXSTAGES; i++)
  240. {
  241. if (m_pTexture[iStage] == m_pTexture[i])
  242. {
  243. iTexCount ++;
  244. }
  245. }
  246. if (iTexCount > 1)
  247. {
  248. DPFM(0,RAST,("Same texture handle was used more than once.\n"))
  249. }
  250. #endif
  251. m_pTexture[iStage]->m_pStageState = &m_TextureStageState[0];
  252. }
  253. // update num active stages
  254. UpdateActiveTexStageCount();
  255. return D3D_OK;
  256. }
  257. //-----------------------------------------------------------------------------
  258. //
  259. // UpdateActiveTexStageCount - Steps through per-stage renderstate and computes
  260. // a count of currently active texture stages. For legacy texture, the count
  261. // is always one.
  262. //
  263. //-----------------------------------------------------------------------------
  264. void
  265. ReferenceRasterizer::UpdateActiveTexStageCount( void )
  266. {
  267. // always one active texture stage for legacy texture mode
  268. if ( NULL != m_dwRenderState[D3DRENDERSTATE_TEXTUREHANDLE] )
  269. {
  270. m_cActiveTextureStages = 1; return;
  271. }
  272. // count number of contiguous-from-zero active texture blend stages
  273. m_cActiveTextureStages = 0;
  274. for ( int iStage=0; iStage<D3DHAL_TSS_MAXSTAGES; iStage++ )
  275. {
  276. // check fir disabled stage (subsequent are thus inactive)
  277. if ( m_TextureStageState[iStage].m_dwVal[D3DTSS_COLOROP] == D3DTOP_DISABLE )
  278. {
  279. break;
  280. }
  281. // check for incorrectly enabled stage (may be legacy)
  282. if ( ( m_pTexture[iStage] == NULL ) &&
  283. ( m_TextureStageState[iStage].m_dwVal[D3DTSS_COLORARG1] == D3DTA_TEXTURE ) )
  284. {
  285. break;
  286. }
  287. // stage is active
  288. m_cActiveTextureStages++;
  289. }
  290. }
  291. //-----------------------------------------------------------------------------
  292. //
  293. // MapHandleToTexture - Map handle to RRTexture pointer. Handle is a ppTex,
  294. // so test it and reference it.
  295. //
  296. //-----------------------------------------------------------------------------
  297. RRTexture*
  298. ReferenceRasterizer::MapHandleToTexture( D3DTEXTUREHANDLE hTex )
  299. {
  300. if ( 0x0 == hTex ) { return NULL; }
  301. return ( *(RRTexture**)ULongToPtr(hTex) );
  302. }
  303. ///////////////////////////////////////////////////////////////////////////////
  304. // end