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.

343 lines
10 KiB

  1. //----------------------------------------------------------------------------
  2. //
  3. // rampif.cpp
  4. //
  5. // Implements RampService.
  6. //
  7. // Copyright (C) Microsoft Corporation, 1997.
  8. //
  9. //----------------------------------------------------------------------------
  10. #include "pch.cpp"
  11. #pragma hdrstop
  12. #include "rampif.h"
  13. #include "rampmat.hpp"
  14. //----------------------------------------------------------------------------
  15. //
  16. // RastRampService
  17. //
  18. // Called by d3dim to update RampRast about material cchanges and so on.
  19. //
  20. //----------------------------------------------------------------------------
  21. HRESULT
  22. RastRampService(ULONG_PTR dwCtx,
  23. RastRampServiceType srvType, ULONG_PTR arg1, LPVOID arg2)
  24. {
  25. D3DContext *pD3DCtx = (D3DContext *)dwCtx;
  26. switch (srvType)
  27. {
  28. case RAMP_SERVICE_CREATEMAT :
  29. return pD3DCtx->RampCreateMaterial((D3DMATERIALHANDLE) arg1);
  30. case RAMP_SERVICE_DESTORYMAT :
  31. return pD3DCtx->RampDestroyMaterial((D3DMATERIALHANDLE) arg1);
  32. case RAMP_SERVICE_SETMATDATA :
  33. return pD3DCtx->RampMaterialChanged((D3DMATERIALHANDLE) arg1);
  34. case RAMP_SERVICE_SETLIGHTSTATE:
  35. return pD3DCtx->RampSetLightstate((UINT32)arg1, arg2);
  36. case RAMP_SERVICE_FIND_LIGHTINGRANGE:
  37. return pD3DCtx->RampFindLightingRange((RAMP_RANGE_INFO *)arg1);
  38. case RAMP_SERVICE_CLEAR:
  39. return pD3DCtx->RampClear();
  40. case RAMP_SERVICE_MATERIAL_TO_PIXEL:
  41. return pD3DCtx->RampMaterialToPixel((D3DMATERIALHANDLE) arg1, (DWORD*) arg2);
  42. case RAMP_SERVICE_SCENE_CAPTURE:
  43. return pD3DCtx->RampSceneCapture((DWORD)arg1, (LPDIRECT3DDEVICEI)arg2);
  44. case RAMP_SERVICE_PALETTE_CHANGED:
  45. return pD3DCtx->RampPaletteChanged((D3DTEXTUREHANDLE)arg1);
  46. case RAMP_SERVICE_CLEAR_TEX_RECT:
  47. return pD3DCtx->RampClearTexRect((D3DMATERIALHANDLE)arg1, (LPD3DRECT)arg2);
  48. default:
  49. D3D_ERR("(Rast) Invalid Service type passed to RastRampService");
  50. return DDERR_GENERIC;
  51. }
  52. return D3D_OK;
  53. }
  54. //----------------------------------------------------------------------------
  55. //
  56. // RastService
  57. //
  58. // Generic Rasterizer service call.
  59. //
  60. // Only use currently is to compute the RGB8 clear color, which requires
  61. // accessing an internal ramp map.
  62. //
  63. //----------------------------------------------------------------------------
  64. HRESULT
  65. RastService(ULONG_PTR dwCtx,
  66. RastServiceType srvType, DWORD arg1, LPVOID arg2)
  67. {
  68. D3DContext *pD3DCtx = (D3DContext *)dwCtx;
  69. switch (srvType)
  70. {
  71. case RAST_SERVICE_RGB8COLORTOPIXEL :
  72. return pD3DCtx->RGB8ColorToPixel((D3DCOLOR) arg1, (DWORD*)arg2);
  73. default:
  74. D3D_ERR("(Rast) Invalid Service type passed to RastService");
  75. return DDERR_GENERIC;
  76. }
  77. }
  78. inline HRESULT
  79. D3DContext::RampCreateMaterial(D3DMATERIALHANDLE hMat)
  80. {
  81. return RLDDIRampCreateMaterial(
  82. (RLDDIRampLightingDriver*)m_RastCtx.pRampDrv,
  83. hMat,
  84. &(m_RastCtx));
  85. }
  86. inline HRESULT
  87. D3DContext::RampDestroyMaterial(D3DMATERIALHANDLE hMat)
  88. {
  89. return RLDDIRampDestroyMaterial(
  90. (RLDDIRampLightingDriver*)m_RastCtx.pRampDrv,
  91. hMat);
  92. }
  93. inline HRESULT
  94. D3DContext::RampMaterialChanged(D3DMATERIALHANDLE hMat)
  95. {
  96. RLDDIRampLightingDriver *pLtDriver =
  97. (RLDDIRampLightingDriver*)m_RastCtx.pRampDrv;
  98. if (pLtDriver)
  99. {
  100. // Update the material
  101. HRESULT hr = RLDDIRampMaterialChanged(pLtDriver, hMat);
  102. RampUpdateRangeInfo();
  103. return hr;
  104. }
  105. else
  106. {
  107. return DDERR_GENERIC;
  108. }
  109. }
  110. inline HRESULT
  111. D3DContext::RampSetLightstate(UINT32 uState, LPVOID pVal)
  112. {
  113. RLDDIRampLightingDriver *pRampdrv =
  114. (RLDDIRampLightingDriver*)m_RastCtx.pRampDrv;
  115. // d3dim has alreay filtered out invlaid light states
  116. switch (uState)
  117. {
  118. case D3DLIGHTSTATE_MATERIAL:
  119. RampSetMaterial(*((LPD3DMATERIALHANDLE)pVal));
  120. break;
  121. case D3DLIGHTSTATE_AMBIENT:
  122. {
  123. D3DCOLOR Color = *((D3DCOLOR*)pVal);
  124. pRampdrv->driver.ambient = (FLOAT)(RGBA_GETRED(Color) * 0x4c + RGBA_GETGREEN(Color) * 0x96 +
  125. RGBA_GETBLUE(Color) * 0x1d) * (1.0F/(256.0F*255.0F));
  126. }
  127. break;
  128. case D3DLIGHTSTATE_FOGMODE:
  129. pRampdrv->driver.fog_mode = *((D3DFOGMODE*)pVal);
  130. break;
  131. case D3DLIGHTSTATE_FOGSTART:
  132. pRampdrv->driver.fog_start = *((FLOAT*)pVal);
  133. break;
  134. case D3DLIGHTSTATE_FOGEND:
  135. pRampdrv->driver.fog_end = *((FLOAT*)pVal);
  136. break;
  137. case D3DLIGHTSTATE_FOGDENSITY:
  138. pRampdrv->driver.fog_density = *((FLOAT*)pVal);
  139. break;
  140. case D3DLIGHTSTATE_COLORMODEL:
  141. pRampdrv->driver.color_model = *((D3DCOLORMODEL*)pVal);
  142. break;
  143. }
  144. return D3D_OK;
  145. }
  146. inline void
  147. D3DContext::BeginSceneHook(void)
  148. {
  149. RLDDIRampBeginSceneHook((RLDDIRampLightingDriver*)m_RastCtx.pRampDrv);
  150. RampUpdateRangeInfo();
  151. }
  152. inline void
  153. D3DContext::EndSceneHook(void)
  154. {
  155. RLDDIRampEndSceneHook((RLDDIRampLightingDriver*)m_RastCtx.pRampDrv);
  156. }
  157. inline HRESULT
  158. D3DContext::RampMaterialToPixel(D3DMATERIALHANDLE hMat, DWORD* pPixel)
  159. {
  160. *pPixel = RLDDIRampMaterialToPixel(
  161. (RLDDIRampLightingDriver*)m_RastCtx.pRampDrv,
  162. hMat);
  163. return D3D_OK;
  164. }
  165. //----------------------------------------------------------------------------
  166. //
  167. // RastClearRamp
  168. //
  169. // This is for RampRast only.
  170. //
  171. //----------------------------------------------------------------------------
  172. inline HRESULT
  173. D3DContext::RampClear(void)
  174. {
  175. // We only need to call BeginSceneHook here
  176. BeginSceneHook();
  177. return D3D_OK;
  178. }
  179. inline void
  180. D3DContext::RampUpdateRangeInfo(void)
  181. {
  182. RLDDIRampLightingDriver *pLtDriver =
  183. (RLDDIRampLightingDriver*)m_RastCtx.pRampDrv;
  184. if (pLtDriver && pLtDriver->current_material)
  185. {
  186. // Update the ramp info. in RastCtx
  187. pLtDriver->current_material->FindLightingRange(
  188. &(m_RastCtx.RampBase),
  189. &(m_RastCtx.RampSize),
  190. &(m_RastCtx.bRampSpecular),
  191. (unsigned long**)&(m_RastCtx.pTexRampMap));
  192. // Make sure DD Palette is updated after it gets set by FindLightingRange
  193. RLDDIRampUpdateDDPalette(&m_RastCtx);
  194. }
  195. }
  196. inline void
  197. D3DContext::RampSetMaterial(D3DMATERIALHANDLE hMat)
  198. {
  199. RLDDIRampLightingDriver *pLtDriver =
  200. (RLDDIRampLightingDriver*)m_RastCtx.pRampDrv;
  201. RLDDIRampSetMaterial(pLtDriver, hMat);
  202. // Update the ramp info. in RastCtx
  203. RampUpdateRangeInfo();
  204. }
  205. inline HRESULT
  206. D3DContext::RampFindLightingRange(RAMP_RANGE_INFO *pRampInfo)
  207. {
  208. RLDDIRampLightingDriver *pLtDriver =
  209. (RLDDIRampLightingDriver*)m_RastCtx.pRampDrv;
  210. if (pLtDriver && pLtDriver->current_material)
  211. {
  212. RampUpdateRangeInfo();
  213. pRampInfo->base = m_RastCtx.RampBase;
  214. pRampInfo->size = m_RastCtx.RampSize;
  215. pRampInfo->specular = m_RastCtx.bRampSpecular;
  216. pRampInfo->pTexRampMap = m_RastCtx.pTexRampMap;
  217. return D3D_OK;
  218. }
  219. else
  220. {
  221. return DDERR_GENERIC;
  222. }
  223. }
  224. //-----------------------------------------------------------------------------
  225. //
  226. // RampSceneCapture
  227. //
  228. // Called on either begin or end scene to perform needed deferred operations on
  229. // ramp materials and palettes.
  230. //
  231. //-----------------------------------------------------------------------------
  232. inline HRESULT
  233. D3DContext::RampSceneCapture(DWORD dwStart, LPDIRECT3DDEVICEI lpDevI)
  234. {
  235. // always do begin, so that even materials used on internal begin/end's
  236. // (like textured fills) get properly aged so they don't go away.
  237. if (dwStart)
  238. {
  239. BeginSceneHook();
  240. }
  241. else
  242. {
  243. if (!(lpDevI->dwHintFlags & D3DDEVBOOL_HINTFLAGS_INTERNAL_BEGIN_END))
  244. {
  245. EndSceneHook();
  246. }
  247. }
  248. return DD_OK;
  249. }
  250. //---------------------------------------------------------------------
  251. // Builds color index that moste closely matches the D3DCOLOR source color.
  252. // For use in 8 bit RGB mode which requires a palette.
  253. //
  254. // Returns:
  255. // color index in range 0 to 0xff
  256. //
  257. //---------------------------------------------------------------------
  258. inline HRESULT
  259. D3DContext::RGB8ColorToPixel(D3DCOLOR Color, DWORD* pdwPalIdx)
  260. {
  261. if ((m_RastCtx.pRampMap == NULL) || (pdwPalIdx == NULL))
  262. {
  263. return DDERR_GENERIC;
  264. }
  265. INT32 iMapIdx = MAKE_RGB8(RGBA_GETRED(Color), RGBA_GETGREEN(Color), RGBA_GETBLUE(Color));
  266. iMapIdx &= 0xff; // RGB8 ramp map always first and only rampmap
  267. // 8 bit result
  268. *pdwPalIdx = m_RastCtx.pRampMap[iMapIdx] & 0xff;
  269. return DD_OK;
  270. }
  271. //---------------------------------------------------------------------
  272. // Causes the texture handle passed to have its colors rebuild, based
  273. // on the new palette.
  274. //
  275. // Returns:
  276. // color index in range 0 to 0xff
  277. //
  278. //---------------------------------------------------------------------
  279. inline HRESULT
  280. D3DContext::RampPaletteChanged(D3DTEXTUREHANDLE hTex)
  281. {
  282. RLDDIRampLightingDriver *pLtDriver =
  283. (RLDDIRampLightingDriver*)m_RastCtx.pRampDrv;
  284. return RLDDIRampPaletteChanged(pLtDriver, hTex);
  285. }
  286. //---------------------------------------------------------------------
  287. // Handles legacy non-power of 2 texture fill for clear.
  288. //
  289. // Returns:
  290. // D3D_OK
  291. //
  292. //---------------------------------------------------------------------
  293. inline HRESULT D3DContext::RampClearTexRect(D3DMATERIALHANDLE hMat, LPD3DRECT pRect)
  294. {
  295. switch (m_RastCtx.iSurfaceBitCount)
  296. {
  297. case 8:
  298. Ramp_Mono_ScaleImage_8(&m_RastCtx, hMat, pRect);
  299. break;
  300. case 16:
  301. Ramp_Mono_ScaleImage_16(&m_RastCtx, hMat, pRect);
  302. break;
  303. case 24:
  304. Ramp_Mono_ScaleImage_24(&m_RastCtx, hMat, pRect);
  305. break;
  306. case 32:
  307. Ramp_Mono_ScaleImage_32(&m_RastCtx, hMat, pRect);
  308. break;
  309. }
  310. return D3D_OK;
  311. }