ilut_directx.c
Upload User: wmy0603
Upload Date: 2022-05-02
Package Size: 1808k
Code Size: 14k
Development Platform:

Visual C++

  1. //-----------------------------------------------------------------------------
  2. //
  3. // ImageLib Utility Toolkit Sources
  4. // Copyright (C) 2000-2008 by Denton Woods
  5. // Last modified: 12/25/2001
  6. //
  7. // Filename: src-ILUT/src/ilut_directx.c
  8. //
  9. // Description: DirectX 8 functions for textures
  10. //
  11. //-----------------------------------------------------------------------------
  12. #include "ilut_internal.h"
  13. #ifdef ILUT_USE_DIRECTX8
  14. #include <d3d8.h>
  15. //#include <d3dx8tex.h>
  16. //pragma comment(lib, "d3d8.lib")
  17. //pragma comment(lib, "d3dx8.lib")
  18. ILimage* MakeD3D8Compliant(IDirect3DDevice8 *Device, D3DFORMAT *DestFormat);
  19. ILenum GetD3D8Compat(ILenum Format);
  20. D3DFORMAT GetD3DFormat(ILenum Format);
  21. ILboolean iD3D8CreateMipmaps(IDirect3DTexture8 *Texture, ILimage *Image);
  22. ILboolean FormatsDX8Checked = IL_FALSE;
  23. ILboolean FormatsDX8supported[6] =
  24. { IL_FALSE, IL_FALSE, IL_FALSE, IL_FALSE, IL_FALSE, IL_FALSE };
  25. D3DFORMAT FormatsDX8[6] =
  26. { D3DFMT_R8G8B8, D3DFMT_A8R8G8B8, D3DFMT_L8, D3DFMT_DXT1, D3DFMT_DXT3, D3DFMT_DXT5 };
  27. ILboolean ilutD3D8Init()
  28. {
  29. return IL_TRUE;
  30. }
  31. ILvoid CheckFormatsDX8(IDirect3DDevice8 *Device)
  32. {
  33. D3DDISPLAYMODE DispMode;
  34. HRESULT hr;
  35. IDirect3D8 *TestD3D8;
  36. ILuint i;
  37. IDirect3DDevice8_GetDirect3D(Device, (IDirect3D8**)&TestD3D8);
  38. IDirect3DDevice8_GetDisplayMode(Device, &DispMode);
  39. for (i = 0; i < 6; i++) {
  40. hr = IDirect3D8_CheckDeviceFormat(TestD3D8, D3DADAPTER_DEFAULT,
  41. D3DDEVTYPE_HAL, DispMode.Format, 0, D3DRTYPE_TEXTURE, FormatsDX8[i]);
  42. FormatsDX8supported[i] = SUCCEEDED(hr);
  43. }
  44. IDirect3D8_Release(TestD3D8);
  45. FormatsDX8Checked = IL_TRUE;
  46. return;
  47. }
  48. #ifndef _WIN32_WCE
  49. ILboolean ILAPIENTRY ilutD3D8TexFromFile(IDirect3DDevice8 *Device, char *FileName, IDirect3DTexture8 **Texture)
  50. {
  51. iBindImageTemp();
  52. if (!ilLoadImage(FileName))
  53. return IL_FALSE;
  54. *Texture = ilutD3D8Texture(Device);
  55. return IL_TRUE;
  56. }
  57. #endif//_WIN32_WCE
  58. #ifndef _WIN32_WCE
  59. ILboolean ILAPIENTRY ilutD3D8VolTexFromFile(IDirect3DDevice8 *Device, char *FileName, IDirect3DVolumeTexture8 **Texture)
  60. {
  61. iBindImageTemp();
  62. if (!ilLoadImage(FileName))
  63. return IL_FALSE;
  64. *Texture = ilutD3D8VolumeTexture(Device);
  65. return IL_TRUE;
  66. }
  67. #endif//_WIN32_WCE
  68. ILboolean ILAPIENTRY ilutD3D8TexFromFileInMemory(IDirect3DDevice8 *Device, ILvoid *Lump, ILuint Size, IDirect3DTexture8 **Texture)
  69. {
  70. iBindImageTemp();
  71. if (!ilLoadIL_TYPE_UNKNOWN_L(, Lump, Size))
  72. return IL_FALSE;
  73. *Texture = ilutD3D8Texture(Device);
  74. return IL_TRUE;
  75. }
  76. ILboolean ILAPIENTRY ilutD3D8VolTexFromFileInMemory(IDirect3DDevice8 *Device, ILvoid *Lump, ILuint Size, IDirect3DVolumeTexture8 **Texture)
  77. {
  78. iBindImageTemp();
  79. if (!ilLoadIL_TYPE_UNKNOWN_L(, Lump, Size))
  80. return IL_FALSE;
  81. *Texture = ilutD3D8VolumeTexture(Device);
  82. return IL_TRUE;
  83. }
  84. ILboolean ILAPIENTRY ilutD3D8TexFromResource(IDirect3DDevice8 *Device, HMODULE SrcModule, char *SrcResource, IDirect3DTexture8 **Texture)
  85. {
  86. HRSRC Resource;
  87. ILubyte *Data;
  88. iBindImageTemp();
  89. Resource = (HRSRC)LoadResource(SrcModule, FindResource(SrcModule, SrcResource, RT_BITMAP));
  90. Data = (ILubyte*)LockResource(Resource);
  91. if (!ilLoadIL_TYPE_UNKNOWN_L(, Data, SizeofResource(SrcModule, FindResource(SrcModule, SrcResource, RT_BITMAP))))
  92. return IL_FALSE;
  93. *Texture = ilutD3D8Texture(Device);
  94. return IL_TRUE;
  95. }
  96. ILboolean ILAPIENTRY ilutD3D8VolTexFromResource(IDirect3DDevice8 *Device, HMODULE SrcModule, char *SrcResource, IDirect3DVolumeTexture8 **Texture)
  97. {
  98. HRSRC Resource;
  99. ILubyte *Data;
  100. iBindImageTemp();
  101. Resource = (HRSRC)LoadResource(SrcModule, FindResource(SrcModule, SrcResource, RT_BITMAP));
  102. Data = (ILubyte*)LockResource(Resource);
  103. if (!ilLoadIL_TYPE_UNKNOWN_L(, Data, SizeofResource(SrcModule, FindResource(SrcModule, SrcResource, RT_BITMAP))))
  104. return IL_FALSE;
  105. *Texture = ilutD3D8VolumeTexture(Device);
  106. return IL_TRUE;
  107. }
  108. ILboolean ILAPIENTRY ilutD3D8TexFromFileHandle(IDirect3DDevice8 *Device, ILHANDLE File, IDirect3DTexture8 **Texture)
  109. {
  110. iBindImageTemp();
  111. if (!ilLoadIL_TYPE_UNKNOWN_F(, File))
  112. return IL_FALSE;
  113. *Texture = ilutD3D8Texture(Device);
  114. return IL_TRUE;
  115. }
  116. ILboolean ILAPIENTRY ilutD3D8VolTexFromFileHandle(IDirect3DDevice8 *Device, ILHANDLE File, IDirect3DVolumeTexture8 **Texture)
  117. {
  118. iBindImageTemp();
  119. if (!ilLoadIL_TYPE_UNKNOWN_F(, File))
  120. return IL_FALSE;
  121. *Texture = ilutD3D8VolumeTexture(Device);
  122. return IL_TRUE;
  123. }
  124. /*
  125. D3DFORMAT D3DGetDXTCNumDX8(ILenum DXTCFormat)
  126. {
  127. switch (DXTCFormat)
  128. {
  129. case IL_DXT1:
  130. return D3DFMT_DXT1;
  131. case IL_DXT3:
  132. return D3DFMT_DXT3;
  133. case IL_DXT5:
  134. return D3DFMT_DXT5;
  135. }
  136. return D3DFMT_UNKNOWN;
  137. }
  138. */
  139. IDirect3DTexture8* ILAPIENTRY ilutD3D8Texture(IDirect3DDevice8 *Device)
  140. {
  141. IDirect3DTexture8 *Texture;
  142. D3DLOCKED_RECT Rect;
  143. D3DFORMAT Format;
  144. ILimage *Image;
  145. ILenum DXTCFormat;
  146. ILuint Size;
  147. ILubyte *Buffer;
  148. Image = ilutCurImage = ilGetCurImage();
  149. if (ilutCurImage == NULL) {
  150. ilSetError(ILUT_ILLEGAL_OPERATION);
  151. return NULL;
  152. }
  153. if (!FormatsDX8Checked)
  154. CheckFormatsDX8(Device);
  155. /*
  156. if (ilutGetBoolean(ILUT_D3D_USE_DXTC) && FormatsDX8supported[3] && FormatsDX8supported[4] && FormatsDX8supported[5]) {
  157. if (ilutCurImage->DxtcData != NULL && ilutCurImage->DxtcSize != 0) {
  158. Format = D3DGetDXTCNumDX8(ilutCurImage->DxtcFormat);
  159. if (FAILED(IDirect3DDevice8_CreateTexture(Device, ilutCurImage->Width,
  160. ilutCurImage->Height, ilutGetInteger(ILUT_D3D_MIPLEVELS), 0, Format,
  161. (D3DPOOL)ilutGetInteger(ILUT_D3D_POOL), &Texture)))
  162. return NULL;
  163. if (FAILED(IDirect3DTexture8_LockRect(Texture, 0, &Rect, NULL, 0)))
  164. return NULL;
  165. memcpy(Rect.pBits, ilutCurImage->DxtcData, ilutCurImage->DxtcSize);
  166. goto success;
  167. }
  168. if (ilutGetBoolean(ILUT_D3D_GEN_DXTC)) {
  169. DXTCFormat = ilutGetInteger(ILUT_DXTC_FORMAT);
  170. Size = ilGetDXTCData(NULL, 0, DXTCFormat);
  171. if (Size != 0) {
  172. Buffer = (ILubyte*)ialloc(Size);
  173. if (Buffer == NULL)
  174. return NULL;
  175. Size = ilGetDXTCData(Buffer, Size, DXTCFormat);
  176. if (Size == 0) {
  177. ifree(Buffer);
  178. return NULL;
  179. }
  180. Format = D3DGetDXTCNumDX8(DXTCFormat);
  181. if (FAILED(IDirect3DDevice8_CreateTexture(Device, ilutCurImage->Width,
  182. ilutCurImage->Height, ilutGetInteger(ILUT_D3D_MIPLEVELS), 0, Format,
  183. (D3DPOOL)ilutGetInteger(ILUT_D3D_POOL), &Texture))) {
  184. ifree(Buffer);
  185. return NULL;
  186. }
  187. if (FAILED(IDirect3DTexture8_LockRect(Texture, 0, &Rect, NULL, 0))) {
  188. ifree(Buffer);
  189. return NULL;
  190. }
  191. memcpy(Rect.pBits, Buffer, Size);
  192. ifree(Buffer);
  193. goto success;
  194. }
  195. }
  196. }
  197. */
  198. Image = MakeD3D8Compliant(Device, &Format);
  199. if (Image == NULL) {
  200. if (Image != ilutCurImage)
  201. ilCloseImage(Image);
  202. return NULL;
  203. }
  204. if (FAILED(IDirect3DDevice8_CreateTexture(Device, Image->Width, Image->Height,
  205. ilutGetInteger(ILUT_D3D_MIPLEVELS), 0, Format, (D3DPOOL)ilutGetInteger(ILUT_D3D_POOL), &Texture))) {
  206. if (Image != ilutCurImage)
  207. ilCloseImage(Image);
  208. return NULL;
  209. }
  210. if (FAILED(IDirect3DTexture8_LockRect(Texture, 0, &Rect, NULL, 0)))
  211. return NULL;
  212. memcpy(Rect.pBits, Image->Data, Image->SizeOfPlane);
  213. success:
  214. IDirect3DTexture8_UnlockRect(Texture, 0);
  215. // Just let D3DX filter for us.
  216. //D3DXFilterTexture(Texture, NULL, D3DX_DEFAULT, D3DX_FILTER_BOX);
  217. iD3D8CreateMipmaps(Texture, Image);
  218. if (Image != ilutCurImage)
  219. ilCloseImage(Image);
  220. return Texture;
  221. }
  222. IDirect3DVolumeTexture8* ILAPIENTRY ilutD3D8VolumeTexture(IDirect3DDevice8 *Device)
  223. {
  224. IDirect3DVolumeTexture8 *Texture;
  225. D3DLOCKED_BOX Box;
  226. D3DFORMAT Format;
  227. ILimage *Image;
  228. ilutCurImage = ilGetCurImage();
  229. if (ilutCurImage == NULL) {
  230. ilSetError(ILUT_ILLEGAL_OPERATION);
  231. return NULL;
  232. }
  233. if (!FormatsDX8Checked)
  234. CheckFormatsDX8(Device);
  235. Image = MakeD3D8Compliant(Device, &Format);
  236. if (Image == NULL)
  237. return NULL;
  238. if (FAILED(IDirect3DDevice8_CreateVolumeTexture(Device, Image->Width, Image->Height,
  239. Image->Depth, 1, 0, Format, (D3DPOOL)ilutGetInteger(ILUT_D3D_POOL), &Texture)))
  240. return NULL;
  241. if (FAILED(IDirect3DVolumeTexture8_LockBox(Texture, 0, &Box, NULL, 0)))
  242. return NULL;
  243. memcpy(Box.pBits, Image->Data, Image->SizeOfData);
  244. if (!IDirect3DVolumeTexture8_UnlockBox(Texture, 0))
  245. return IL_FALSE;
  246. // We don't want to have mipmaps for such a large image.
  247. if (Image != ilutCurImage)
  248. ilCloseImage(Image);
  249. return Texture;
  250. }
  251. ILimage *MakeD3D8Compliant(IDirect3DDevice8 *Device, D3DFORMAT *DestFormat)
  252. {
  253. ILimage *Converted, *Scaled, *CurImage;
  254. *DestFormat = D3DFMT_A8R8G8B8;
  255. // Images must be in BGRA format.
  256. if (ilutCurImage->Format != IL_BGRA) {
  257. Converted = iConvertImage(ilutCurImage, IL_BGRA, IL_UNSIGNED_BYTE);
  258. if (Converted == NULL)
  259. return NULL;
  260. }
  261. else {
  262. Converted = ilutCurImage;
  263. }
  264. // Images must have their origin in the upper left.
  265. if (Converted->Origin != IL_ORIGIN_UPPER_LEFT) {
  266. CurImage = ilutCurImage;
  267. ilSetCurImage(Converted);
  268. iluFlipImage();
  269. ilSetCurImage(CurImage);
  270. }
  271. // Images must have powers-of-2 dimensions.
  272. if (ilNextPower2(ilutCurImage->Width) != ilutCurImage->Width ||
  273. ilNextPower2(ilutCurImage->Height) != ilutCurImage->Height ||
  274. ilNextPower2(ilutCurImage->Depth) != ilutCurImage->Depth) {
  275. Scaled = iluScale_(Converted, ilNextPower2(ilutCurImage->Width),
  276. ilNextPower2(ilutCurImage->Height), ilNextPower2(ilutCurImage->Depth));
  277. if (Converted != ilutCurImage) {
  278. ilCloseImage(Converted);
  279. }
  280. if (Scaled == NULL) {
  281. return NULL;
  282. }
  283. Converted = Scaled;
  284. }
  285. return Converted;
  286. }
  287. ILboolean iD3D8CreateMipmaps(IDirect3DTexture8 *Texture, ILimage *Image)
  288. {
  289. D3DLOCKED_RECT Rect;
  290. D3DSURFACE_DESC Desc;
  291. ILuint NumMips, Width, Height, i;
  292. ILimage *CurImage, *MipImage, *Temp;
  293. NumMips = IDirect3DTexture8_GetLevelCount(Texture);
  294. Width = Image->Width;
  295. Height = Image->Height;
  296. CurImage = ilGetCurImage();
  297. MipImage = ilCopyImage_(CurImage);
  298. ilSetCurImage(MipImage);
  299. if (!iluBuildMipmaps()) {
  300. ilCloseImage(MipImage);
  301. ilSetCurImage(CurImage);
  302. return IL_FALSE;
  303. }
  304. ilSetCurImage(CurImage);
  305. Temp = MipImage->Mipmaps;
  306. // Counts the base texture as 1.
  307. for (i = 1; i < NumMips && Temp != NULL; i++) {
  308. if (FAILED(IDirect3DTexture8_LockRect(Texture, i, &Rect, NULL, 0)))
  309. return IL_FALSE;
  310. Width = IL_MAX(1, Width / 2);
  311. Height = IL_MAX(1, Height / 2);
  312. IDirect3DTexture8_GetLevelDesc(Texture, i, &Desc);
  313. if (Desc.Width != Width || Desc.Height != Height) {
  314. IDirect3DTexture8_UnlockRect(Texture, i);
  315. return IL_FALSE;
  316. }
  317. memcpy(Rect.pBits, Temp->Data, Temp->SizeOfData);
  318. IDirect3DTexture8_UnlockRect(Texture, i);
  319. Temp = Temp->Next;
  320. }
  321. ilCloseImage(MipImage);
  322. return IL_TRUE;
  323. }
  324. //
  325. // SaveSurfaceToFile.cpp
  326. //
  327. // Copyright (c) 2001 David Galeano
  328. //
  329. // Permission to use, copy, modify and distribute this software
  330. // is hereby granted, provided that both the copyright notice and 
  331. // this permission notice appear in all copies of the software, 
  332. // derivative works or modified versions.
  333. //
  334. // THE AUTHOR ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
  335. // CONDITION AND DISCLAIMS ANY LIABILITY OF ANY KIND FOR ANY DAMAGES 
  336. // WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
  337. //
  338. ILAPI ILboolean ILAPIENTRY ilutD3D8LoadSurface(IDirect3DDevice8 *Device, IDirect3DSurface8 *Surface)
  339. {
  340. HRESULT hr;
  341. D3DSURFACE_DESC d3dsd;
  342. LPDIRECT3DSURFACE8 SurfaceCopy;
  343. D3DLOCKED_RECT d3dLR;
  344. ILboolean bHasAlpha;
  345. ILubyte *Image, *ImageAux, *Data;
  346. ILuint y, x;
  347. ILushort dwColor;
  348. IDirect3DSurface8_GetDesc(Surface, &d3dsd);
  349. bHasAlpha = (d3dsd.Format == D3DFMT_A8R8G8B8 || d3dsd.Format == D3DFMT_A1R5G5B5);
  350. if (bHasAlpha) {
  351. if (!ilTexImage(d3dsd.Width, d3dsd.Height, 1, 4, IL_BGRA, IL_UNSIGNED_BYTE, NULL)) {
  352. return IL_FALSE;
  353. }
  354. }
  355. else {
  356. if (!ilTexImage(d3dsd.Width, d3dsd.Height, 1, 3, IL_BGR, IL_UNSIGNED_BYTE, NULL)) {
  357. return IL_FALSE;
  358. }
  359. }
  360. hr = IDirect3DDevice8_CreateImageSurface(Device, d3dsd.Width, d3dsd.Height, d3dsd.Format, &SurfaceCopy);
  361. if (FAILED(hr)) {
  362. ilSetError(ILUT_ILLEGAL_OPERATION);
  363. return IL_FALSE;
  364. }
  365. hr = IDirect3DDevice8_CopyRects(Device, Surface, NULL, 0, SurfaceCopy, NULL);
  366. if (FAILED(hr)) {
  367. ilSetError(ILUT_ILLEGAL_OPERATION);
  368. return IL_FALSE;
  369. }
  370. hr = IDirect3DSurface8_LockRect(SurfaceCopy, &d3dLR, NULL, D3DLOCK_NO_DIRTY_UPDATE | D3DLOCK_NOSYSLOCK | D3DLOCK_READONLY);
  371. if (FAILED(hr)) {
  372. IDirect3DSurface8_Release(SurfaceCopy);
  373. ilSetError(ILUT_ILLEGAL_OPERATION);
  374. return IL_FALSE;
  375. }
  376. Image = (ILubyte*)d3dLR.pBits;
  377. Data = ilutCurImage->Data;
  378. for (y = 0; y < d3dsd.Height; y++) {
  379. if (d3dsd.Format == D3DFMT_X8R8G8B8) {
  380. ImageAux = Image;
  381. for (x = 0; x < d3dsd.Width; x++) {
  382. Data[0] = ImageAux[0];
  383. Data[1] = ImageAux[1];
  384. Data[2] = ImageAux[2];
  385. Data += 3;
  386. ImageAux += 4;
  387. }
  388. }
  389. else if (d3dsd.Format == D3DFMT_A8R8G8B8) {
  390. memcpy(Data, Image, d3dsd.Width * 4);
  391. }
  392. else if (d3dsd.Format == D3DFMT_R5G6B5) {
  393. ImageAux = Image;
  394. for (x = 0; x < d3dsd.Width; x++) {
  395. dwColor = *((ILushort*)ImageAux);
  396. Data[0] = (ILubyte)((dwColor&0x001f)<<3);
  397. Data[1] = (ILubyte)(((dwColor&0x7e0)>>5)<<2);
  398. Data[2] = (ILubyte)(((dwColor&0xf800)>>11)<<3);
  399. Data += 3;
  400. ImageAux += 2;
  401. }
  402. }
  403. else if (d3dsd.Format == D3DFMT_X1R5G5B5) {
  404. ImageAux = Image;
  405. for (x = 0; x < d3dsd.Width; x++) {
  406. dwColor = *((ILushort*)ImageAux);
  407. Data[0] = (ILubyte)((dwColor&0x001f)<<3);
  408. Data[1] = (ILubyte)(((dwColor&0x3e0)>>5)<<3);
  409. Data[2] = (ILubyte)(((dwColor&0x7c00)>>10)<<3);
  410. Data += 3;
  411. ImageAux += 2;
  412. }
  413. }
  414. else if (d3dsd.Format == D3DFMT_A1R5G5B5) {
  415. ImageAux = Image;
  416. for (x = 0; x < d3dsd.Width; x++) {
  417. dwColor = *((ILushort*)ImageAux);
  418. Data[0] = (ILubyte)((dwColor&0x001f)<<3);
  419. Data[1] = (ILubyte)(((dwColor&0x3e0)>>5)<<3);
  420. Data[2] = (ILubyte)(((dwColor&0x7c00)>>10)<<3);
  421. Data[3] = (ILubyte)(((dwColor&0x8000)>>15)*255);
  422. Data += 4;
  423. ImageAux += 2;
  424. }
  425. }
  426. Image += d3dLR.Pitch;
  427. }
  428. IDirect3DSurface8_UnlockRect(SurfaceCopy);
  429. IDirect3DSurface8_Release(SurfaceCopy);
  430. return IL_TRUE;
  431. }
  432. #endif//ILUT_USE_DIRECTX8