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.

467 lines
14 KiB

  1. //----------------------------------------------------------------------------
  2. //
  3. // d3dif.cpp
  4. //
  5. // shared interface functions
  6. //
  7. // Copyright (C) Microsoft Corporation, 1997.
  8. //
  9. //----------------------------------------------------------------------------
  10. #include "pch.cpp"
  11. #pragma hdrstop
  12. //----------------------------------------------------------------------------
  13. //
  14. // FindOutSurfFormat
  15. //
  16. // Converts a DDPIXELFORMAT to D3DI_SPANTEX_FORMAT.
  17. //
  18. //----------------------------------------------------------------------------
  19. HRESULT FASTCALL
  20. FindOutSurfFormat(LPDDPIXELFORMAT pDdPixFmt,
  21. D3DI_SPANTEX_FORMAT *pFmt)
  22. {
  23. if (pDdPixFmt->dwFlags & DDPF_ZBUFFER)
  24. {
  25. switch(pDdPixFmt->dwZBitMask)
  26. {
  27. default:
  28. case 0x0000FFFF: *pFmt = D3DI_SPTFMT_Z16S0; break;
  29. case 0xFFFFFF00: *pFmt = D3DI_SPTFMT_Z24S8; break;
  30. case 0x0000FFFE: *pFmt = D3DI_SPTFMT_Z15S1; break;
  31. case 0xFFFFFFFF: *pFmt = D3DI_SPTFMT_Z32S0; break;
  32. }
  33. }
  34. else if (pDdPixFmt->dwFlags & DDPF_BUMPDUDV)
  35. {
  36. UINT uFmt = pDdPixFmt->dwBumpDvBitMask;
  37. switch (uFmt)
  38. {
  39. case 0x0000ff00:
  40. switch (pDdPixFmt->dwRGBBitCount)
  41. {
  42. case 24:
  43. *pFmt = D3DI_SPTFMT_U8V8L8;
  44. break;
  45. case 16:
  46. *pFmt = D3DI_SPTFMT_U8V8;
  47. break;
  48. }
  49. break;
  50. case 0x000003e0:
  51. *pFmt = D3DI_SPTFMT_U5V5L6;
  52. break;
  53. }
  54. }
  55. else if (pDdPixFmt->dwFlags & DDPF_PALETTEINDEXED8)
  56. {
  57. *pFmt = D3DI_SPTFMT_PALETTE8;
  58. }
  59. else if (pDdPixFmt->dwFlags & DDPF_PALETTEINDEXED4)
  60. {
  61. *pFmt = D3DI_SPTFMT_PALETTE4;
  62. }
  63. else if (pDdPixFmt->dwFourCC == MAKEFOURCC('U', 'Y', 'V', 'Y'))
  64. {
  65. *pFmt = D3DI_SPTFMT_UYVY;
  66. }
  67. else if (pDdPixFmt->dwFourCC == MAKEFOURCC('Y', 'U', 'Y', '2'))
  68. {
  69. *pFmt = D3DI_SPTFMT_YUY2;
  70. }
  71. else if (pDdPixFmt->dwFourCC == MAKEFOURCC('D', 'X', 'T', '1'))
  72. {
  73. *pFmt = D3DI_SPTFMT_DXT1;
  74. }
  75. else if (pDdPixFmt->dwFourCC == MAKEFOURCC('D', 'X', 'T', '2'))
  76. {
  77. *pFmt = D3DI_SPTFMT_DXT2;
  78. }
  79. else if (pDdPixFmt->dwFourCC == MAKEFOURCC('D', 'X', 'T', '3'))
  80. {
  81. *pFmt = D3DI_SPTFMT_DXT3;
  82. }
  83. else if (pDdPixFmt->dwFourCC == MAKEFOURCC('D', 'X', 'T', '4'))
  84. {
  85. *pFmt = D3DI_SPTFMT_DXT4;
  86. }
  87. else if (pDdPixFmt->dwFourCC == MAKEFOURCC('D', 'X', 'T', '5'))
  88. {
  89. *pFmt = D3DI_SPTFMT_DXT5;
  90. }
  91. else
  92. {
  93. UINT uFmt = pDdPixFmt->dwGBitMask | pDdPixFmt->dwRBitMask;
  94. if (pDdPixFmt->dwFlags & DDPF_ALPHAPIXELS)
  95. {
  96. uFmt |= pDdPixFmt->dwRGBAlphaBitMask;
  97. }
  98. switch (uFmt)
  99. {
  100. case 0x00ffff00:
  101. switch (pDdPixFmt->dwRGBBitCount)
  102. {
  103. case 32:
  104. *pFmt = D3DI_SPTFMT_B8G8R8X8;
  105. break;
  106. case 24:
  107. *pFmt = D3DI_SPTFMT_B8G8R8;
  108. break;
  109. }
  110. break;
  111. case 0xffffff00:
  112. *pFmt = D3DI_SPTFMT_B8G8R8A8;
  113. break;
  114. case 0xffe0:
  115. if (pDdPixFmt->dwFlags & DDPF_ALPHAPIXELS)
  116. {
  117. *pFmt = D3DI_SPTFMT_B5G5R5A1;
  118. }
  119. else
  120. {
  121. *pFmt = D3DI_SPTFMT_B5G6R5;
  122. }
  123. break;
  124. case 0x07fe0:
  125. *pFmt = D3DI_SPTFMT_B5G5R5;
  126. break;
  127. case 0xff0:
  128. *pFmt = D3DI_SPTFMT_B4G4R4;
  129. break;
  130. case 0xfff0:
  131. *pFmt = D3DI_SPTFMT_B4G4R4A4;
  132. break;
  133. case 0xff:
  134. *pFmt = D3DI_SPTFMT_L8;
  135. break;
  136. case 0xffff:
  137. *pFmt = D3DI_SPTFMT_L8A8;
  138. break;
  139. case 0xfc:
  140. *pFmt = D3DI_SPTFMT_B2G3R3;
  141. break;
  142. default:
  143. *pFmt = D3DI_SPTFMT_NULL;
  144. break;
  145. }
  146. }
  147. return D3D_OK;
  148. }
  149. //----------------------------------------------------------------------------
  150. //
  151. // ValidTextureSize
  152. //
  153. // checks for power of two texture size
  154. //
  155. //----------------------------------------------------------------------------
  156. BOOL FASTCALL
  157. ValidTextureSize(INT16 iuSize, INT16 iuShift,
  158. INT16 ivSize, INT16 ivShift)
  159. {
  160. if (iuSize == 1)
  161. {
  162. if (ivSize == 1)
  163. {
  164. return TRUE;
  165. }
  166. else
  167. {
  168. return !(ivSize & (~(1 << ivShift)));
  169. }
  170. }
  171. else
  172. {
  173. if (ivSize == 1)
  174. {
  175. return !(iuSize & (~(1 << iuShift)));
  176. }
  177. else
  178. {
  179. return (!(iuSize & (~(1 << iuShift)))
  180. && !(iuSize & (~(1 << iuShift))));
  181. }
  182. }
  183. }
  184. //----------------------------------------------------------------------------
  185. //
  186. // ValidMipmapSize
  187. //
  188. // Computes size of next smallest mipmap level, clamping at 1
  189. //
  190. //----------------------------------------------------------------------------
  191. BOOL FASTCALL
  192. ValidMipmapSize(INT16 iPreSize, INT16 iSize)
  193. {
  194. if (iPreSize == 1)
  195. {
  196. if (iSize == 1)
  197. {
  198. return TRUE;
  199. }
  200. else
  201. {
  202. return FALSE;
  203. }
  204. }
  205. else
  206. {
  207. return ((iPreSize >> 1) == iSize);
  208. }
  209. }
  210. //----------------------------------------------------------------------------
  211. //
  212. // TextureFormats
  213. //
  214. // Returns all the texture formats supported by our rasterizer.
  215. // Right now, it's called at device creation time to fill the driver gloabl
  216. // data.
  217. //
  218. //----------------------------------------------------------------------------
  219. #define NUM_SUPPORTED_TEXTURE_FORMATS 22
  220. int
  221. TextureFormats(LPDDSURFACEDESC* lplpddsd, DWORD dwVersion, SW_RAST_TYPE RastType)
  222. {
  223. int i = 0;
  224. if (RastType == SW_RAST_MMX && dwVersion < 3)
  225. {
  226. static DDSURFACEDESC mmx_ddsd[1];
  227. /* pal8 */
  228. mmx_ddsd[i].dwSize = sizeof(mmx_ddsd[0]);
  229. mmx_ddsd[i].dwFlags = DDSD_PIXELFORMAT | DDSD_CAPS;
  230. mmx_ddsd[i].ddsCaps.dwCaps = DDSCAPS_TEXTURE;
  231. mmx_ddsd[i].ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
  232. mmx_ddsd[i].ddpfPixelFormat.dwFlags = DDPF_PALETTEINDEXED8 | DDPF_RGB;
  233. mmx_ddsd[i].ddpfPixelFormat.dwRGBBitCount = 8;
  234. i++;
  235. *lplpddsd = mmx_ddsd;
  236. return i;
  237. }
  238. static DDSURFACEDESC ddsd_RGBMMX_Dev3[NUM_SUPPORTED_TEXTURE_FORMATS];
  239. static DDSURFACEDESC ddsd_RGBMMX_Dev2[NUM_SUPPORTED_TEXTURE_FORMATS];
  240. DDSURFACEDESC *ddsd;
  241. if (dwVersion >= 3)
  242. {
  243. ddsd = ddsd_RGBMMX_Dev3;
  244. }
  245. else
  246. {
  247. ddsd = ddsd_RGBMMX_Dev2;
  248. }
  249. /* 888 */
  250. ddsd[i].dwSize = sizeof(ddsd[0]);
  251. ddsd[i].dwFlags = DDSD_PIXELFORMAT | DDSD_CAPS;
  252. ddsd[i].ddsCaps.dwCaps = DDSCAPS_TEXTURE;
  253. ddsd[i].ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
  254. ddsd[i].ddpfPixelFormat.dwFlags = DDPF_RGB;
  255. ddsd[i].ddpfPixelFormat.dwRGBBitCount = 32;
  256. ddsd[i].ddpfPixelFormat.dwRBitMask = 0xff0000;
  257. ddsd[i].ddpfPixelFormat.dwGBitMask = 0x00ff00;
  258. ddsd[i].ddpfPixelFormat.dwBBitMask = 0x0000ff;
  259. i++;
  260. /* 8888 */
  261. ddsd[i].dwSize = sizeof(ddsd[0]);
  262. ddsd[i].dwFlags = DDSD_PIXELFORMAT | DDSD_CAPS;
  263. ddsd[i].ddsCaps.dwCaps = DDSCAPS_TEXTURE;
  264. ddsd[i].ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
  265. ddsd[i].ddpfPixelFormat.dwFlags = DDPF_RGB | DDPF_ALPHAPIXELS;
  266. ddsd[i].ddpfPixelFormat.dwRGBBitCount = 32;
  267. ddsd[i].ddpfPixelFormat.dwRGBAlphaBitMask = 0xff000000;
  268. ddsd[i].ddpfPixelFormat.dwRBitMask = 0xff0000;
  269. ddsd[i].ddpfPixelFormat.dwGBitMask = 0x00ff00;
  270. ddsd[i].ddpfPixelFormat.dwBBitMask = 0x0000ff;
  271. i++;
  272. /* 565 */
  273. ddsd[i].dwSize = sizeof(ddsd[0]);
  274. ddsd[i].dwFlags = DDSD_PIXELFORMAT | DDSD_CAPS;
  275. ddsd[i].ddsCaps.dwCaps = DDSCAPS_TEXTURE;
  276. ddsd[i].ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
  277. ddsd[i].ddpfPixelFormat.dwFlags = DDPF_RGB;
  278. ddsd[i].ddpfPixelFormat.dwRGBBitCount = 16;
  279. ddsd[i].ddpfPixelFormat.dwRBitMask = 0xf800;
  280. ddsd[i].ddpfPixelFormat.dwGBitMask = 0x07e0;
  281. ddsd[i].ddpfPixelFormat.dwBBitMask = 0x001f;
  282. i++;
  283. /* 555 */
  284. ddsd[i].dwSize = sizeof(ddsd[0]);
  285. ddsd[i].dwFlags = DDSD_PIXELFORMAT | DDSD_CAPS;
  286. ddsd[i].ddsCaps.dwCaps = DDSCAPS_TEXTURE;
  287. ddsd[i].ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
  288. ddsd[i].ddpfPixelFormat.dwFlags = DDPF_RGB;
  289. ddsd[i].ddpfPixelFormat.dwRGBBitCount = 16;
  290. ddsd[i].ddpfPixelFormat.dwRBitMask = 0x7c00;
  291. ddsd[i].ddpfPixelFormat.dwGBitMask = 0x03e0;
  292. ddsd[i].ddpfPixelFormat.dwBBitMask = 0x001f;
  293. i++;
  294. /* pal4 */
  295. ddsd[i].dwSize = sizeof(ddsd[0]);
  296. ddsd[i].dwFlags = DDSD_PIXELFORMAT | DDSD_CAPS;
  297. ddsd[i].ddsCaps.dwCaps = DDSCAPS_TEXTURE;
  298. ddsd[i].ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
  299. ddsd[i].ddpfPixelFormat.dwFlags = DDPF_PALETTEINDEXED4 | DDPF_RGB;
  300. ddsd[i].ddpfPixelFormat.dwRGBBitCount = 4;
  301. i++;
  302. /* pal8 */
  303. ddsd[i].dwSize = sizeof(ddsd[0]);
  304. ddsd[i].dwFlags = DDSD_PIXELFORMAT | DDSD_CAPS;
  305. ddsd[i].ddsCaps.dwCaps = DDSCAPS_TEXTURE;
  306. ddsd[i].ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
  307. ddsd[i].ddpfPixelFormat.dwFlags = DDPF_PALETTEINDEXED8 | DDPF_RGB;
  308. ddsd[i].ddpfPixelFormat.dwRGBBitCount = 8;
  309. i++;
  310. if (dwVersion >= 3)
  311. {
  312. /* 1555 */
  313. ddsd[i].dwSize = sizeof(ddsd[0]);
  314. ddsd[i].dwFlags = DDSD_PIXELFORMAT | DDSD_CAPS;
  315. ddsd[i].ddsCaps.dwCaps = DDSCAPS_TEXTURE;
  316. ddsd[i].ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
  317. ddsd[i].ddpfPixelFormat.dwFlags = DDPF_RGB | DDPF_ALPHAPIXELS;
  318. ddsd[i].ddpfPixelFormat.dwRGBBitCount = 16;
  319. ddsd[i].ddpfPixelFormat.dwRGBAlphaBitMask = 0x8000;
  320. ddsd[i].ddpfPixelFormat.dwRBitMask = 0x7c00;
  321. ddsd[i].ddpfPixelFormat.dwGBitMask = 0x03e0;
  322. ddsd[i].ddpfPixelFormat.dwBBitMask = 0x001f;
  323. i++;
  324. // A formats for PC98 consistency
  325. // 4444 ARGB (it is already supported by S3 Virge)
  326. ddsd[i].dwSize = sizeof(ddsd[0]);
  327. ddsd[i].dwFlags = DDSD_PIXELFORMAT | DDSD_CAPS;
  328. ddsd[i].ddsCaps.dwCaps = DDSCAPS_TEXTURE;
  329. ddsd[i].ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
  330. ddsd[i].ddpfPixelFormat.dwFlags = DDPF_RGB | DDPF_ALPHAPIXELS;
  331. ddsd[i].ddpfPixelFormat.dwRGBBitCount = 16;
  332. ddsd[i].ddpfPixelFormat.dwRGBAlphaBitMask = 0xf000;
  333. ddsd[i].ddpfPixelFormat.dwRBitMask = 0x0f00;
  334. ddsd[i].ddpfPixelFormat.dwGBitMask = 0x00f0;
  335. ddsd[i].ddpfPixelFormat.dwBBitMask = 0x000f;
  336. i++;
  337. }
  338. if ((dwVersion >= 2) && (RastType == SW_RAST_REFNULL))
  339. {
  340. // 332 8-bit RGB
  341. ddsd[i].dwSize = sizeof(ddsd[0]);
  342. ddsd[i].dwFlags = DDSD_PIXELFORMAT | DDSD_CAPS;
  343. ddsd[i].ddsCaps.dwCaps = DDSCAPS_TEXTURE;
  344. ddsd[i].ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
  345. ddsd[i].ddpfPixelFormat.dwFlags = DDPF_RGB;
  346. ddsd[i].ddpfPixelFormat.dwRGBBitCount = 8;
  347. ddsd[i].ddpfPixelFormat.dwRBitMask = 0xe0;
  348. ddsd[i].ddpfPixelFormat.dwGBitMask = 0x1c;
  349. ddsd[i].ddpfPixelFormat.dwBBitMask = 0x03;
  350. i++;
  351. }
  352. if (dwVersion >= 3)
  353. {
  354. /* 8 bit luminance-only */
  355. ddsd[i].dwSize = sizeof(ddsd[0]);
  356. ddsd[i].dwFlags = DDSD_PIXELFORMAT | DDSD_CAPS;
  357. ddsd[i].ddsCaps.dwCaps = DDSCAPS_TEXTURE;
  358. ddsd[i].ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
  359. ddsd[i].ddpfPixelFormat.dwFlags = DDPF_LUMINANCE;
  360. ddsd[i].ddpfPixelFormat.dwLuminanceBitCount = 8;
  361. ddsd[i].ddpfPixelFormat.dwLuminanceBitMask = 0xff;
  362. i++;
  363. /* 16 bit alpha-luminance */
  364. ddsd[i].dwSize = sizeof(ddsd[0]);
  365. ddsd[i].dwFlags = DDSD_PIXELFORMAT | DDSD_CAPS;
  366. ddsd[i].ddsCaps.dwCaps = DDSCAPS_TEXTURE;
  367. ddsd[i].ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
  368. ddsd[i].ddpfPixelFormat.dwFlags = DDPF_LUMINANCE | DDPF_ALPHAPIXELS;
  369. ddsd[i].ddpfPixelFormat.dwLuminanceBitCount = 16;
  370. ddsd[i].ddpfPixelFormat.dwRGBAlphaBitMask = 0xff00;
  371. ddsd[i].ddpfPixelFormat.dwLuminanceBitMask = 0xff;
  372. i++;
  373. }
  374. *lplpddsd = ddsd;
  375. return i;
  376. }
  377. //----------------------------------------------------------------------------
  378. //
  379. // ZBufferFormats
  380. //
  381. // Must return union of all the Z buffer formats supported by all rasterizers.
  382. // CreateDevice will screen out device-specific ones (i.e. ones ramp doesnt handle) later.
  383. // Called at device creation time and by DDHEL to validate software ZBuffer
  384. // creation.
  385. //
  386. //----------------------------------------------------------------------------
  387. #define NUM_SUPPORTED_ZBUFFER_FORMATS 4
  388. int
  389. ZBufferFormats(DDPIXELFORMAT** ppDDPF)
  390. {
  391. static DDPIXELFORMAT DDPF[NUM_SUPPORTED_ZBUFFER_FORMATS];
  392. int i = 0;
  393. memset(&DDPF[0],0,sizeof(DDPF));
  394. /* 16 bit Z; no stencil */
  395. DDPF[i].dwSize = sizeof(DDPIXELFORMAT);
  396. DDPF[i].dwFlags = DDPF_ZBUFFER;
  397. DDPF[i].dwZBufferBitDepth = 16;
  398. DDPF[i].dwStencilBitDepth = 0;
  399. DDPF[i].dwZBitMask = 0xffff;
  400. DDPF[i].dwStencilBitMask = 0x0000;
  401. i++;
  402. /* 24 bit Z; 8 bit stencil */
  403. DDPF[i].dwSize = sizeof(DDPIXELFORMAT);
  404. DDPF[i].dwFlags = DDPF_ZBUFFER | DDPF_STENCILBUFFER;
  405. DDPF[i].dwZBufferBitDepth = 32; // ZBufferBitDepth represents the total bits. Z Bits are ZBBitDepth-StencilBitDepth
  406. DDPF[i].dwStencilBitDepth = 8;
  407. DDPF[i].dwZBitMask = 0xffffff00;
  408. DDPF[i].dwStencilBitMask = 0x000000ff;
  409. i++;
  410. *ppDDPF = DDPF;
  411. return i;
  412. }