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.

358 lines
11 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_pDbgMon = NULL;
  35. m_pDDLcl = NULL;
  36. m_wSaveFP = 0;
  37. m_bInBegin = FALSE;
  38. m_bPointSprite = 0;
  39. m_pRenderTarget = NULL;
  40. memset( m_fWBufferNorm, 0, sizeof( float)*2 );
  41. memset( m_dwRenderState, 0, sizeof( DWORD ) * D3DHAL_MAX_RSTATES );
  42. memset( &m_renderstate_override, 0, sizeof(m_renderstate_override) );
  43. m_cActiveTextureStages = 0;
  44. m_ReferencedTexCoordMask = 0;
  45. memset( m_pTexture, 0, sizeof(RDSurface2D*)*D3DHAL_TSS_MAXSTAGES );
  46. memset( m_dwTextureStageState, 0, sizeof(m_dwTextureStageState) );
  47. for (int i=0; i<D3DHAL_TSS_MAXSTAGES; i++) m_pTextureStageState[i] = m_dwTextureStageState[i];
  48. m_dwTexArrayLength = 0;
  49. m_LastState = 0;
  50. m_primType = (D3DPRIMITIVETYPE)0;
  51. m_dwNumVertices = 0;
  52. m_dwStartVertex = 0;
  53. m_dwNumIndices = 0;
  54. m_dwStartIndex = 0;
  55. m_RefVP.m_pDev = m_RefVM.m_pDev = m_Clipper.m_pDev = this;
  56. m_CurrentVShaderHandle = 0;
  57. m_pCurrentVShader = NULL;
  58. m_qwFVFOut = 0;
  59. m_CurrentPShaderHandle = 0x0;
  60. m_Rast.Init( this );
  61. // All render and texture stage state is initialized by
  62. // DIRECT3DDEVICEI::stateInitialize
  63. m_dwInterfaceType = dwInterfaceType;
  64. m_dwDDIType = dwDDIType;
  65. m_pDDLcl = pDDLcl;
  66. // StateOverride initialize
  67. STATESET_INIT( m_renderstate_override );
  68. m_bOverrideTCI = FALSE;
  69. SetSetStateFunctions();
  70. // Set this renderstate so that the pre-DX8 emulations continue to work
  71. GetRS()[D3DRS_COLORWRITEENABLE] = 0xf;
  72. // set 'changed' flags
  73. m_dwRastFlags =
  74. RDRF_MULTISAMPLE_CHANGED|
  75. RDRF_PIXELSHADER_CHANGED|
  76. RDRF_LEGACYPIXELSHADER_CHANGED|
  77. RDRF_TEXTURESTAGESTATE_CHANGED;
  78. // make the gamma table
  79. {
  80. FLOAT fGamma = (float)(log10(0.5f)/log10((float)g_iGamma/255));
  81. FLOAT fOOGamma = 1/fGamma;
  82. FLOAT fA = 0.018f;
  83. FLOAT fS = (float)(((1-fOOGamma)*pow(fA,fOOGamma))/(1-(1-fOOGamma)*pow(fA,fOOGamma)));
  84. FLOAT fGain = (float)((fOOGamma*pow(fA,(fOOGamma-1)))/(1-(1-fOOGamma)*pow(fA,fOOGamma)));
  85. FLOAT fX;
  86. int i;
  87. for (i = 0; i < 4; i++)
  88. g_GammaTable[i] = (float)(fGain*(((float)i)/255));
  89. for (i = 4; i < 256; i++)
  90. g_GammaTable[i] = (float)((1+fS)*pow((((float)i)/255),fOOGamma)-fS);
  91. }
  92. }
  93. //-----------------------------------------------------------------------------
  94. RefDev::~RefDev( void )
  95. {
  96. UINT i;
  97. // Clean up statesets
  98. for ( i = 0; i < m_pStateSets.ArraySize(); i++ )
  99. {
  100. if (m_pStateSets[i] != NULL)
  101. delete m_pStateSets[i];
  102. }
  103. // Clean up vertex shaders
  104. for( i=0; i<m_VShaderHandleArray.GetSize(); i++ )
  105. {
  106. delete m_VShaderHandleArray[i].m_pShader;
  107. }
  108. // Clean up pixel shaders
  109. for( i=0; i<m_PShaderHandleArray.GetSize(); i++ )
  110. {
  111. delete m_PShaderHandleArray[i].m_pShader;
  112. }
  113. // Clean up palette handles
  114. for( i=0; i<m_PaletteHandleArray.GetSize(); i++ )
  115. {
  116. delete m_PaletteHandleArray[i].m_pPal;
  117. }
  118. if (m_pDbgMon) delete m_pDbgMon;
  119. }
  120. ///////////////////////////////////////////////////////////////////////////////
  121. // //
  122. // State Management Utilities //
  123. // //
  124. ///////////////////////////////////////////////////////////////////////////////
  125. //-----------------------------------------------------------------------------
  126. //
  127. // MapTextureHandleToDevice - This is called when texture handles change or
  128. // when leaving legacy texture mode. This maps the texture handle embedded
  129. // in the per-stage state to texture object pointers.
  130. //
  131. //-----------------------------------------------------------------------------
  132. void
  133. RefDev::MapTextureHandleToDevice( int iStage )
  134. {
  135. // map one
  136. m_pTexture[iStage] =
  137. MapHandleToTexture( m_TextureStageState[iStage].m_dwVal[D3DTSS_TEXTUREMAP] );
  138. m_pTexture[iStage]->SetRefDev(this);
  139. // update num active stages
  140. UpdateActiveTexStageCount();
  141. }
  142. //-----------------------------------------------------------------------------
  143. //
  144. // SetTextureHandle - On DX7, this is called when a texture handle is set.
  145. // This maps the texture handle embedded in the per-stage state to texture
  146. // object pointers.
  147. //
  148. //-----------------------------------------------------------------------------
  149. HRESULT
  150. RefDev::SetTextureHandle( int iStage, DWORD dwTexHandle )
  151. {
  152. HRESULT hr = D3D_OK;
  153. // Special case, if texture handle == 0, then unmap the texture from the TSS
  154. if (dwTexHandle == 0)
  155. {
  156. m_pTexture[iStage] = NULL;
  157. // update num active stages
  158. UpdateActiveTexStageCount();
  159. return D3D_OK;
  160. }
  161. // Ask DDraw to decipher what this particular handle meant wrt. to the
  162. // the DDraw_Local associated with this instance of the Refrast
  163. RDSurface2D* pTex = (RDSurface2D *)g_SurfMgr.GetSurfFromList(m_pDDLcl,
  164. dwTexHandle);
  165. if( pTex == NULL )
  166. {
  167. DPFERR( "Unable to obtain Texture from the list" );
  168. return DDERR_INVALIDOBJECT;
  169. }
  170. // map one
  171. m_pTexture[iStage] = pTex;
  172. m_pTexture[iStage]->SetRefDev(this);
  173. // update num active stages
  174. UpdateActiveTexStageCount();
  175. return D3D_OK;
  176. }
  177. //-----------------------------------------------------------------------------
  178. //
  179. // UpdateActiveTexStageCount - Updates number of texture coordinate/lookup
  180. // stages that are active.
  181. //
  182. //-----------------------------------------------------------------------------
  183. void
  184. RefDev::UpdateActiveTexStageCount( void )
  185. {
  186. m_dwRastFlags |= RDRF_TEXTURESTAGESTATE_CHANGED;
  187. // DX3/5 - always one active texture stage
  188. if ( NULL != m_dwRenderState[D3DRENDERSTATE_TEXTUREHANDLE] )
  189. {
  190. m_cActiveTextureStages = 1;
  191. m_ReferencedTexCoordMask = 0x1;
  192. return;
  193. }
  194. // DX8+ pixel shading model - count derived from shader code
  195. if (m_CurrentPShaderHandle)
  196. {
  197. RDPShader* pShader = GetPShader(m_CurrentPShaderHandle);
  198. if( pShader )
  199. {
  200. m_cActiveTextureStages = pShader->m_cActiveTextureStages;
  201. m_ReferencedTexCoordMask = pShader->m_ReferencedTexCoordMask;
  202. }
  203. else
  204. {
  205. m_cActiveTextureStages = 0;
  206. m_ReferencedTexCoordMask = 0;
  207. }
  208. return;
  209. }
  210. // DX6/7 pixel shading model
  211. m_cActiveTextureStages = 0;
  212. for ( int iStage=0; iStage<D3DHAL_TSS_MAXSTAGES; iStage++ )
  213. {
  214. // check for disabled stage (subsequent are thus inactive)
  215. if ( m_TextureStageState[iStage].m_dwVal[D3DTSS_COLOROP] == D3DTOP_DISABLE )
  216. {
  217. break;
  218. }
  219. // check for incorrectly enabled stage (may be legacy)
  220. if ( ( m_pTexture[iStage] == NULL ) &&
  221. ( m_TextureStageState[iStage].m_dwVal[D3DTSS_COLORARG1] == D3DTA_TEXTURE ) )
  222. {
  223. break;
  224. }
  225. // stage is active
  226. m_cActiveTextureStages++;
  227. }
  228. m_ReferencedTexCoordMask = (1<<m_cActiveTextureStages)-1;
  229. }
  230. //-----------------------------------------------------------------------------
  231. //
  232. // MapHandleToTexture - Map handle to RDSurface2D pointer. Handle is a ppTex,
  233. // so test it and reference it.
  234. //
  235. //-----------------------------------------------------------------------------
  236. RDSurface2D*
  237. RefDev::MapHandleToTexture( D3DTEXTUREHANDLE hTex )
  238. {
  239. if ( 0x0 == hTex ) { return NULL; }
  240. #ifdef _IA64_
  241. _ASSERTa(FALSE, "This will not work on IA64", return NULL;);
  242. #endif
  243. return ( *(RDSurface2D**)ULongToPtr(hTex) );
  244. }
  245. #ifndef __D3D_NULL_REF
  246. //-----------------------------------------------------------------------------
  247. //
  248. // Generic shared memory object, implemented using Win32 file mapping.
  249. // _snprintf interface for name.
  250. //
  251. //-----------------------------------------------------------------------------
  252. D3DSharedMem::D3DSharedMem( INT_PTR cbSize, const char* pszFormat, ... )
  253. {
  254. m_pMem = NULL;
  255. char pszName[1024] = "\0";
  256. va_list marker;
  257. va_start(marker, pszFormat);
  258. _vsnprintf(pszName+lstrlen(pszName), 1024-lstrlen(pszName), pszFormat, marker);
  259. m_hFileMap = CreateFileMapping(INVALID_HANDLE_VALUE,
  260. NULL, PAGE_READWRITE, 0, (DWORD)cbSize, pszName);
  261. // check if it existed already
  262. m_bAlreadyExisted = (m_hFileMap != NULL) && (GetLastError() == ERROR_ALREADY_EXISTS);
  263. if (NULL == m_hFileMap)
  264. {
  265. _ASSERT(0,"D3DSharedMem: file mapping failed")
  266. }
  267. else
  268. {
  269. // Map a view of the file into the address space.
  270. m_pMem = (void *)MapViewOfFile(m_hFileMap, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, 0);
  271. if (NULL == m_pMem)
  272. {
  273. _ASSERT(0,"D3DSharedMem: view map failed")
  274. if (NULL != m_hFileMap) CloseHandle(m_hFileMap);
  275. }
  276. }
  277. }
  278. D3DSharedMem::~D3DSharedMem(void)
  279. {
  280. if (NULL != m_pMem) UnmapViewOfFile((LPVOID) m_pMem);
  281. if (NULL != m_hFileMap) CloseHandle(m_hFileMap);
  282. }
  283. //------------------------------------------------------------------------
  284. //
  285. // Called upon loading/unloading DLL
  286. //
  287. //------------------------------------------------------------------------
  288. BOOL WINAPI
  289. DllMain(HINSTANCE hmod, DWORD dwReason, LPVOID lpvReserved)
  290. {
  291. switch( dwReason )
  292. {
  293. case DLL_PROCESS_ATTACH:
  294. break;
  295. case DLL_PROCESS_DETACH:
  296. break;
  297. default:
  298. break;
  299. }
  300. return TRUE;
  301. }
  302. #endif //__D3D_NULL_REF
  303. ///////////////////////////////////////////////////////////////////////////////
  304. // end