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.

313 lines
10 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Copyright (C) Microsoft Corporation, 2000.
  3. //
  4. // refdevi.cpp
  5. //
  6. // Direct3D Reference Device - Main Internal Object Methods
  7. //
  8. ///////////////////////////////////////////////////////////////////////////////
  9. #include "pch.cpp"
  10. #pragma hdrstop
  11. //////////////////////////////////////////////////////////////////////////////////
  12. // //
  13. // global controls //
  14. // //
  15. //////////////////////////////////////////////////////////////////////////////////
  16. float g_GammaTable[256];
  17. UINT g_iGamma = 150;
  18. //////////////////////////////////////////////////////////////////////////////////
  19. // //
  20. // RefDev Methods //
  21. // //
  22. //////////////////////////////////////////////////////////////////////////////////
  23. //-----------------------------------------------------------------------------
  24. //
  25. // Constructor/Destructor for renderer core object.
  26. //
  27. //-----------------------------------------------------------------------------
  28. RefDev::RefDev( LPDDRAWI_DIRECTDRAW_LCL pDDLcl, DWORD dwInterfaceType,
  29. RDDDITYPE dwDDIType, D3DCAPS8* pCaps8 )
  30. : m_RefVP(), m_RefVM(), m_Clipper(),
  31. m_FVFShader()
  32. {
  33. m_Caps8 = *pCaps8;
  34. m_pDDLcl = NULL;
  35. m_wSaveFP = 0;
  36. m_bInBegin = FALSE;
  37. m_bPointSprite = 0;
  38. m_pRenderTarget = NULL;
  39. memset( m_fWBufferNorm, 0, sizeof( float)*2 );
  40. memset( m_dwRenderState, 0, sizeof( DWORD ) * D3DHAL_MAX_RSTATES );
  41. memset( &m_renderstate_override, 0, sizeof(m_renderstate_override) );
  42. m_cActiveTextureStages = 0;
  43. m_ReferencedTexCoordMask = 0;
  44. memset( m_pTexture, 0, sizeof(RDSurface2D*)*D3DHAL_TSS_MAXSTAGES );
  45. memset( m_dwTextureStageState, 0, sizeof(m_dwTextureStageState) );
  46. for (int i=0; i<D3DHAL_TSS_MAXSTAGES; i++) m_pTextureStageState[i] = m_dwTextureStageState[i];
  47. m_dwTexArrayLength = 0;
  48. m_LastState = 0;
  49. m_primType = (D3DPRIMITIVETYPE)0;
  50. m_dwNumVertices = 0;
  51. m_dwStartVertex = 0;
  52. m_dwNumIndices = 0;
  53. m_dwStartIndex = 0;
  54. m_RefVP.m_pDev = m_RefVM.m_pDev = m_Clipper.m_pDev = this;
  55. m_CurrentVShaderHandle = 0;
  56. m_pCurrentVShader = NULL;
  57. m_qwFVFOut = 0;
  58. m_CurrentPShaderHandle = 0x0;
  59. m_Rast.Init( this );
  60. // All render and texture stage state is initialized by
  61. // DIRECT3DDEVICEI::stateInitialize
  62. m_dwInterfaceType = dwInterfaceType;
  63. m_dwDDIType = dwDDIType;
  64. m_pDDLcl = pDDLcl;
  65. // StateOverride initialize
  66. STATESET_INIT( m_renderstate_override );
  67. m_bOverrideTCI = FALSE;
  68. SetSetStateFunctions();
  69. // Set this renderstate so that the pre-DX8 emulations continue to work
  70. GetRS()[D3DRS_COLORWRITEENABLE] = 0xf;
  71. // set 'changed' flags
  72. m_dwRastFlags =
  73. RDRF_MULTISAMPLE_CHANGED|
  74. RDRF_PIXELSHADER_CHANGED|
  75. RDRF_LEGACYPIXELSHADER_CHANGED|
  76. RDRF_TEXTURESTAGESTATE_CHANGED;
  77. // make the gamma table
  78. {
  79. FLOAT fGamma = (float)(log10(0.5f)/log10((float)g_iGamma/255));
  80. FLOAT fOOGamma = 1/fGamma;
  81. FLOAT fA = 0.018f;
  82. FLOAT fS = (float)(((1-fOOGamma)*pow(fA,fOOGamma))/(1-(1-fOOGamma)*pow(fA,fOOGamma)));
  83. FLOAT fGain = (float)((fOOGamma*pow(fA,(fOOGamma-1)))/(1-(1-fOOGamma)*pow(fA,fOOGamma)));
  84. FLOAT fX;
  85. int i;
  86. for (i = 0; i < 4; i++)
  87. g_GammaTable[i] = (float)(fGain*(((float)i)/255));
  88. for (i = 4; i < 256; i++)
  89. g_GammaTable[i] = (float)((1+fS)*pow((((float)i)/255),fOOGamma)-fS);
  90. }
  91. }
  92. //-----------------------------------------------------------------------------
  93. RefDev::~RefDev( void )
  94. {
  95. UINT i;
  96. // Clean up statesets
  97. for ( i = 0; i < m_pStateSets.ArraySize(); i++ )
  98. {
  99. if (m_pStateSets[i] != NULL)
  100. delete m_pStateSets[i];
  101. }
  102. // Clean up vertex shaders
  103. for( i=0; i<m_VShaderHandleArray.GetSize(); i++ )
  104. {
  105. delete m_VShaderHandleArray[i].m_pShader;
  106. }
  107. // Clean up pixel shaders
  108. for( i=0; i<m_PShaderHandleArray.GetSize(); i++ )
  109. {
  110. delete m_PShaderHandleArray[i].m_pShader;
  111. }
  112. // Clean up palette handles
  113. for( i=0; i<m_PaletteHandleArray.GetSize(); i++ )
  114. {
  115. delete m_PaletteHandleArray[i].m_pPal;
  116. }
  117. }
  118. ///////////////////////////////////////////////////////////////////////////////
  119. // //
  120. // State Management Utilities //
  121. // //
  122. ///////////////////////////////////////////////////////////////////////////////
  123. //-----------------------------------------------------------------------------
  124. //
  125. // MapTextureHandleToDevice - This is called when texture handles change or
  126. // when leaving legacy texture mode. This maps the texture handle embedded
  127. // in the per-stage state to texture object pointers.
  128. //
  129. //-----------------------------------------------------------------------------
  130. void
  131. RefDev::MapTextureHandleToDevice( int iStage )
  132. {
  133. // map one
  134. m_pTexture[iStage] =
  135. MapHandleToTexture( m_TextureStageState[iStage].m_dwVal[D3DTSS_TEXTUREMAP] );
  136. m_pTexture[iStage]->SetRefDev(this);
  137. // update num active stages
  138. UpdateActiveTexStageCount();
  139. }
  140. //-----------------------------------------------------------------------------
  141. //
  142. // SetTextureHandle - On DX7, this is called when a texture handle is set.
  143. // This maps the texture handle embedded in the per-stage state to texture
  144. // object pointers.
  145. //
  146. //-----------------------------------------------------------------------------
  147. HRESULT
  148. RefDev::SetTextureHandle( int iStage, DWORD dwTexHandle )
  149. {
  150. HRESULT hr = D3D_OK;
  151. // Special case, if texture handle == 0, then unmap the texture from the TSS
  152. if (dwTexHandle == 0)
  153. {
  154. m_pTexture[iStage] = NULL;
  155. // update num active stages
  156. UpdateActiveTexStageCount();
  157. return D3D_OK;
  158. }
  159. // Ask DDraw to decipher what this particular handle meant wrt. to the
  160. // the DDraw_Local associated with this instance of the Refrast
  161. RDSurface2D* pTex = (RDSurface2D *)g_SurfMgr.GetSurfFromList(m_pDDLcl,
  162. dwTexHandle);
  163. if( pTex == NULL )
  164. {
  165. DPFERR( "Unable to obtain Texture from the list" );
  166. return DDERR_INVALIDOBJECT;
  167. }
  168. // map one
  169. m_pTexture[iStage] = pTex;
  170. m_pTexture[iStage]->SetRefDev(this);
  171. // update num active stages
  172. UpdateActiveTexStageCount();
  173. return D3D_OK;
  174. }
  175. //-----------------------------------------------------------------------------
  176. //
  177. // UpdateActiveTexStageCount - Updates number of texture coordinate/lookup
  178. // stages that are active.
  179. //
  180. //-----------------------------------------------------------------------------
  181. void
  182. RefDev::UpdateActiveTexStageCount( void )
  183. {
  184. m_dwRastFlags |= RDRF_TEXTURESTAGESTATE_CHANGED;
  185. // DX3/5 - always one active texture stage
  186. if ( NULL != m_dwRenderState[D3DRENDERSTATE_TEXTUREHANDLE] )
  187. {
  188. m_cActiveTextureStages = 1;
  189. m_ReferencedTexCoordMask = 0x1;
  190. return;
  191. }
  192. // DX8+ pixel shading model - count derived from shader code
  193. if (m_CurrentPShaderHandle)
  194. {
  195. RDPShader* pShader = GetPShader(m_CurrentPShaderHandle);
  196. if( pShader )
  197. {
  198. m_cActiveTextureStages = pShader->m_cActiveTextureStages;
  199. m_ReferencedTexCoordMask = pShader->m_ReferencedTexCoordMask;
  200. }
  201. else
  202. {
  203. m_cActiveTextureStages = 0;
  204. m_ReferencedTexCoordMask = 0;
  205. }
  206. return;
  207. }
  208. // DX6/7 pixel shading model
  209. m_cActiveTextureStages = 0;
  210. for ( int iStage=0; iStage<D3DHAL_TSS_MAXSTAGES; iStage++ )
  211. {
  212. // check for disabled stage (subsequent are thus inactive)
  213. if ( m_TextureStageState[iStage].m_dwVal[D3DTSS_COLOROP] == D3DTOP_DISABLE )
  214. {
  215. break;
  216. }
  217. // check for incorrectly enabled stage (may be legacy)
  218. if ( ( m_pTexture[iStage] == NULL ) &&
  219. ( m_TextureStageState[iStage].m_dwVal[D3DTSS_COLORARG1] == D3DTA_TEXTURE ) )
  220. {
  221. break;
  222. }
  223. // stage is active
  224. m_cActiveTextureStages++;
  225. }
  226. m_ReferencedTexCoordMask = (1<<m_cActiveTextureStages)-1;
  227. }
  228. //-----------------------------------------------------------------------------
  229. //
  230. // MapHandleToTexture - Map handle to RDSurface2D pointer. Handle is a ppTex,
  231. // so test it and reference it.
  232. //
  233. //-----------------------------------------------------------------------------
  234. RDSurface2D*
  235. RefDev::MapHandleToTexture( D3DTEXTUREHANDLE hTex )
  236. {
  237. if ( 0x0 == hTex ) { return NULL; }
  238. #ifdef _IA64_
  239. _ASSERTa(FALSE, "This will not work on IA64", return NULL;);
  240. #endif
  241. return ( *(RDSurface2D**)ULongToPtr(hTex) );
  242. }
  243. #ifndef __D3D_NULL_REF
  244. //------------------------------------------------------------------------
  245. //
  246. // Called upon loading/unloading DLL
  247. //
  248. //------------------------------------------------------------------------
  249. BOOL WINAPI
  250. DllMain(HINSTANCE hmod, DWORD dwReason, LPVOID lpvReserved)
  251. {
  252. switch( dwReason )
  253. {
  254. case DLL_PROCESS_ATTACH:
  255. break;
  256. case DLL_PROCESS_DETACH:
  257. break;
  258. default:
  259. break;
  260. }
  261. return TRUE;
  262. }
  263. #endif //__D3D_NULL_REF
  264. ///////////////////////////////////////////////////////////////////////////////
  265. // end