FileDXTC.pas 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339
  1. //
  2. // This unit is part of the GLScene Engine, http://glscene.org
  3. //
  4. (*
  5. DXTC (also S3TC) decoding.
  6. Adapted from DevIL image library (http://openil.sourceforge.net)
  7. *)
  8. unit FileDXTC;
  9. interface
  10. {$I GLScene.inc}
  11. {$Z4} // Minimum enum size = dword
  12. uses
  13. System.SysUtils,
  14. OpenGLTokens,
  15. GLTextureFormat;
  16. const
  17. DDSD_CAPS = $00000001;
  18. DDSD_HEIGHT = $00000002;
  19. DDSD_WIDTH = $00000004;
  20. DDSD_PITCH = $00000008;
  21. DDSD_PIXELFORMAT = $00001000;
  22. DDSD_MIPMAPCOUNT = $00020000;
  23. DDSD_LINEARSIZE = $00080000;
  24. DDSD_DEPTH = $00800000;
  25. DDPF_ALPHAPIXELS = $00000001;
  26. DDPF_A = $00000002;
  27. DDPF_FOURCC = $00000004;
  28. DDPF_RGB = $00000040;
  29. DDPF_RGBA = $00000041;
  30. DDPF_L = $00020000;
  31. DDPF_LA = $00020001;
  32. DDSCAPS_COMPLEX = $00000008;
  33. DDSCAPS_TEXTURE = $00001000;
  34. DDSCAPS_MIPMAP = $00400000;
  35. DDSCAPS2_CUBEMAP = $00000200;
  36. DDSCAPS2_CUBEMAP_POSITIVEX = $00000400;
  37. DDSCAPS2_CUBEMAP_NEGATIVEX = $00000800;
  38. DDSCAPS2_CUBEMAP_POSITIVEY = $00001000;
  39. DDSCAPS2_CUBEMAP_NEGATIVEY = $00002000;
  40. DDSCAPS2_CUBEMAP_POSITIVEZ = $00004000;
  41. DDSCAPS2_CUBEMAP_NEGATIVEZ = $00008000;
  42. DDSCAPS2_VOLUME = $00200000;
  43. type
  44. TDDPIXELFORMAT = record
  45. dwSize,
  46. dwFlags,
  47. dwFourCC,
  48. dwRGBBitCount,
  49. dwRBitMask,
  50. dwGBitMask,
  51. dwBBitMask,
  52. dwRGBAlphaBitMask : Cardinal;
  53. end;
  54. TDDSURFACEDESC2 = record
  55. dwSize,
  56. dwFlags,
  57. dwHeight,
  58. dwWidth,
  59. dwPitchOrLinearSize, {The number of bytes per scan line in an
  60. uncompressed texture; the total number of bytes
  61. in the top level texture for a compressed texture.}
  62. dwDepth,
  63. dwMipMapCount : Cardinal;
  64. dwReserved1 : array[0..10] of Cardinal;
  65. ddpf : TDDPIXELFORMAT;
  66. dwCaps,
  67. dwCaps2,
  68. dwCaps3,
  69. dwCaps4 : Cardinal;
  70. dwReserved2 : Cardinal;
  71. end;
  72. TDDSHeader = record
  73. Magic : Cardinal;
  74. SurfaceFormat : TDDSURFACEDESC2;
  75. end;
  76. DXTColBlock = record
  77. col0: Word;
  78. col1: Word;
  79. row: array[0..3] of Byte;
  80. end;
  81. PDXTColBlock = ^DXTColBlock;
  82. DXT3AlphaBlock = record
  83. row: array[0..3] of Word;
  84. end;
  85. PDXT3AlphaBlock = ^DXT3AlphaBlock;
  86. DXT5AlphaBlock = record
  87. alpha0 : Byte;
  88. alpha1 : Byte;
  89. row : array[0..5] of Byte;
  90. end;
  91. PDXT5AlphaBlock = ^DXT5AlphaBlock;
  92. const
  93. // TDXGI_FORMAT =
  94. // (
  95. DXGI_FORMAT_FORCE_UINT = -1;
  96. DXGI_FORMAT_UNKNOWN = 0;
  97. DXGI_FORMAT_R32G32B32A32_TYPELESS = 1;
  98. DXGI_FORMAT_R32G32B32A32_FLOAT = 2;
  99. DXGI_FORMAT_R32G32B32A32_UINT = 3;
  100. DXGI_FORMAT_R32G32B32A32_SINT = 4;
  101. DXGI_FORMAT_R32G32B32_TYPELESS = 5;
  102. DXGI_FORMAT_R32G32B32_FLOAT = 6;
  103. DXGI_FORMAT_R32G32B32_UINT = 7;
  104. DXGI_FORMAT_R32G32B32_SINT = 8;
  105. DXGI_FORMAT_R16G16B16A16_TYPELESS = 9;
  106. DXGI_FORMAT_R16G16B16A16_FLOAT = 10;
  107. DXGI_FORMAT_R16G16B16A16_UNORM = 11;
  108. DXGI_FORMAT_R16G16B16A16_UINT = 12;
  109. DXGI_FORMAT_R16G16B16A16_SNORM = 13;
  110. DXGI_FORMAT_R16G16B16A16_SINT = 14;
  111. DXGI_FORMAT_R32G32_TYPELESS = 15;
  112. DXGI_FORMAT_R32G32_FLOAT = 16;
  113. DXGI_FORMAT_R32G32_UINT = 17;
  114. DXGI_FORMAT_R32G32_SINT = 18;
  115. DXGI_FORMAT_R32G8X24_TYPELESS = 19;
  116. DXGI_FORMAT_D32_FLOAT_S8X24_UINT = 20;
  117. DXGI_FORMAT_R32_FLOAT_X8X24_TYPELESS = 21;
  118. DXGI_FORMAT_X32_TYPELESS_G8X24_UINT = 22;
  119. DXGI_FORMAT_R10G10B10A2_TYPELESS = 23;
  120. DXGI_FORMAT_R10G10B10A2_UNORM = 24;
  121. DXGI_FORMAT_R10G10B10A2_UINT = 25;
  122. DXGI_FORMAT_R11G11B10_FLOAT = 26;
  123. DXGI_FORMAT_R8G8B8A8_TYPELESS = 27;
  124. DXGI_FORMAT_R8G8B8A8_UNORM = 28;
  125. DXGI_FORMAT_R8G8B8A8_UNORM_SRGB = 29;
  126. DXGI_FORMAT_R8G8B8A8_UINT = 30;
  127. DXGI_FORMAT_R8G8B8A8_SNORM = 31;
  128. DXGI_FORMAT_R8G8B8A8_SINT = 32;
  129. DXGI_FORMAT_R16G16_TYPELESS = 33;
  130. DXGI_FORMAT_R16G16_FLOAT = 34;
  131. DXGI_FORMAT_R16G16_UNORM = 35;
  132. DXGI_FORMAT_R16G16_UINT = 36;
  133. DXGI_FORMAT_R16G16_SNORM = 37;
  134. DXGI_FORMAT_R16G16_SINT = 38;
  135. DXGI_FORMAT_R32_TYPELESS = 39;
  136. DXGI_FORMAT_D32_FLOAT = 40;
  137. DXGI_FORMAT_R32_FLOAT = 41;
  138. DXGI_FORMAT_R32_UINT = 42;
  139. DXGI_FORMAT_R32_SINT = 43;
  140. DXGI_FORMAT_R24G8_TYPELESS = 44;
  141. DXGI_FORMAT_D24_UNORM_S8_UINT = 45;
  142. DXGI_FORMAT_R24_UNORM_X8_TYPELESS = 46;
  143. DXGI_FORMAT_X24_TYPELESS_G8_UINT = 47;
  144. DXGI_FORMAT_R8G8_TYPELESS = 48;
  145. DXGI_FORMAT_R8G8_UNORM = 49;
  146. DXGI_FORMAT_R8G8_UINT = 50;
  147. DXGI_FORMAT_R8G8_SNORM = 51;
  148. DXGI_FORMAT_R8G8_SINT = 52;
  149. DXGI_FORMAT_R16_TYPELESS = 53;
  150. DXGI_FORMAT_R16_FLOAT = 54;
  151. DXGI_FORMAT_D16_UNORM = 55;
  152. DXGI_FORMAT_R16_UNORM = 56;
  153. DXGI_FORMAT_R16_UINT = 57;
  154. DXGI_FORMAT_R16_SNORM = 58;
  155. DXGI_FORMAT_R16_SINT = 59;
  156. DXGI_FORMAT_R8_TYPELESS = 60;
  157. DXGI_FORMAT_R8_UNORM = 61;
  158. DXGI_FORMAT_R8_UINT = 62;
  159. DXGI_FORMAT_R8_SNORM = 63;
  160. DXGI_FORMAT_R8_SINT = 64;
  161. DXGI_FORMAT_A8_UNORM = 65;
  162. DXGI_FORMAT_R1_UNORM = 66;
  163. DXGI_FORMAT_R9G9B9E5_SHAREDEXP = 67;
  164. DXGI_FORMAT_R8G8_B8G8_UNORM = 68;
  165. DXGI_FORMAT_G8R8_G8B8_UNORM = 69;
  166. DXGI_FORMAT_BC1_TYPELESS = 70;
  167. DXGI_FORMAT_BC1_UNORM = 71;
  168. DXGI_FORMAT_BC1_UNORM_SRGB = 72;
  169. DXGI_FORMAT_BC2_TYPELESS = 73;
  170. DXGI_FORMAT_BC2_UNORM = 74;
  171. DXGI_FORMAT_BC2_UNORM_SRGB = 75;
  172. DXGI_FORMAT_BC3_TYPELESS = 76;
  173. DXGI_FORMAT_BC3_UNORM = 77;
  174. DXGI_FORMAT_BC3_UNORM_SRGB = 78;
  175. DXGI_FORMAT_BC4_TYPELESS = 79;
  176. DXGI_FORMAT_BC4_UNORM = 80;
  177. DXGI_FORMAT_BC4_SNORM = 81;
  178. DXGI_FORMAT_BC5_TYPELESS = 82;
  179. DXGI_FORMAT_BC5_UNORM = 83;
  180. DXGI_FORMAT_BC5_SNORM = 84;
  181. DXGI_FORMAT_B5G6R5_UNORM = 85;
  182. DXGI_FORMAT_B5G5R5A1_UNORM = 86;
  183. DXGI_FORMAT_B8G8R8A8_UNORM = 87;
  184. DXGI_FORMAT_B8G8R8X8_UNORM = 88;
  185. DXGI_FORMAT_R10G10B10_XR_BIAS_A2_UNORM = 89;
  186. DXGI_FORMAT_B8G8R8A8_TYPELESS = 90;
  187. DXGI_FORMAT_B8G8R8A8_UNORM_SRGB = 91;
  188. DXGI_FORMAT_B8G8R8X8_TYPELESS = 92;
  189. DXGI_FORMAT_B8G8R8X8_UNORM_SRGB = 93;
  190. DXGI_FORMAT_BC6H_TYPELESS = 94;
  191. DXGI_FORMAT_BC6H_UF16 = 95;
  192. DXGI_FORMAT_BC6H_SF16 = 96;
  193. DXGI_FORMAT_BC7_TYPELESS = 97;
  194. DXGI_FORMAT_BC7_UNORM = 98;
  195. DXGI_FORMAT_BC7_UNORM_SRGB = 99;
  196. // );
  197. // TD3D11_RESOURCE_DIMENSION =
  198. // (
  199. D3D11_RESOURCE_DIMENSION_UNKNOWN = 0;
  200. D3D11_RESOURCE_DIMENSION_BUFFER = 1;
  201. D3D11_RESOURCE_DIMENSION_TEXTURE1D = 2;
  202. D3D11_RESOURCE_DIMENSION_TEXTURE2D = 3;
  203. D3D11_RESOURCE_DIMENSION_TEXTURE3D = 4;
  204. // );
  205. type
  206. TDDS_HEADER_DXT10 = record
  207. dxgiFormat : Integer; //TDXGI_FORMAT;
  208. resourceDimension : Integer; //TD3D11_RESOURCE_DIMENSION;
  209. miscFlag : Cardinal;
  210. arraySize : Cardinal;
  211. reserved : Cardinal;
  212. end;
  213. TFOURCC = array[0..3] of AnsiChar;
  214. const
  215. FOURCC_UNKNOWN = 0;
  216. FOURCC_R8G8B8 = 20;
  217. FOURCC_A8R8G8B8 = 21;
  218. FOURCC_X8R8G8B8 = 22;
  219. FOURCC_R5G6B5 = 23;
  220. FOURCC_X1R5G5B5 = 24;
  221. FOURCC_A1R5G5B5 = 25;
  222. FOURCC_A4R4G4B4 = 26;
  223. FOURCC_R3G3B2 = 27;
  224. FOURCC_A8 = 28;
  225. FOURCC_A8R3G3B2 = 29;
  226. FOURCC_X4R4G4B4 = 30;
  227. FOURCC_A2B10G10R10 = 31;
  228. FOURCC_A8B8G8R8 = 32;
  229. FOURCC_X8B8G8R8 = 33;
  230. FOURCC_G16R16 = 34;
  231. FOURCC_A2R10G10B10 = 35;
  232. FOURCC_A16B16G16R16 = 36;
  233. FOURCC_L8 = 50;
  234. FOURCC_A8L8 = 51;
  235. FOURCC_A4L4 = 52;
  236. FOURCC_DXT1 = $31545844;
  237. FOURCC_DXT2 = $32545844;
  238. FOURCC_DXT3 = $33545844;
  239. FOURCC_DXT4 = $34545844;
  240. FOURCC_DXT5 = $35545844;
  241. FOURCC_ATI1 = $31495441;
  242. FOURCC_ATI2 = $32495441;
  243. FOURCC_D16_LOCKABLE = 70;
  244. FOURCC_D32 = 71;
  245. FOURCC_D24X8 = 77;
  246. FOURCC_D16 = 80;
  247. FOURCC_D32F_LOCKABLE = 82;
  248. FOURCC_L16 = 81;
  249. // Floating point surface formats
  250. // s10e5 formats (16-bits per channel)
  251. FOURCC_R16F = 111;
  252. FOURCC_G16R16F = 112;
  253. FOURCC_A16B16G16R16F = 113;
  254. // IEEE s23e8 formats (32-bits per channel)
  255. FOURCC_R32F = 114;
  256. FOURCC_G32R32F = 115;
  257. FOURCC_A32B32G32R32F = 116;
  258. // DX10 header indicator
  259. FOURCC_DX10 = $47495844;
  260. type
  261. TGLImageDataFormat = record
  262. ColorFlag: Cardinal;
  263. RBits, GBits, BBits, ABits: Cardinal;
  264. colorFormat: Cardinal;
  265. TexFormat: TGLInternalFormat;
  266. dType: Cardinal;
  267. end;
  268. const
  269. cImageDataFormat8bits: array[0..3] of TGLImageDataFormat = (
  270. (ColorFlag: DDPF_RGB;
  271. RBits: $E0;
  272. GBits: $1C;
  273. BBits: $03;
  274. ABits: $00;
  275. colorFormat: GL_RGB;
  276. TexFormat: tfR3_G3_B2;
  277. dType: GL_UNSIGNED_BYTE_3_3_2),
  278. (ColorFlag: DDPF_LA;
  279. RBits: $0F;
  280. GBits: $00;
  281. BBits: $00;
  282. ABits: $F0;
  283. colorFormat: GL_LUMINANCE_ALPHA;
  284. TexFormat: tfLUMINANCE4_ALPHA4;
  285. dType: GL_UNSIGNED_BYTE),
  286. (ColorFlag: DDPF_A;
  287. RBits: $00;
  288. GBits: $00;
  289. BBits: $00;
  290. ABits: $FF;
  291. colorFormat: GL_ALPHA;
  292. TexFormat: tfALPHA8;
  293. dType: GL_UNSIGNED_BYTE),
  294. (ColorFlag: DDPF_L;
  295. RBits: $FF;
  296. GBits: $00;
  297. BBits: $00;
  298. ABits: $00;
  299. colorFormat: GL_LUMINANCE;
  300. TexFormat: tfLUMINANCE8;
  301. dType: GL_UNSIGNED_BYTE)
  302. );
  303. cImageDataFormat16bits: array[0..4] of TGLImageDataFormat = (
  304. (ColorFlag: DDPF_RGBA;
  305. RBits: $0F00;
  306. GBits: $F0;
  307. BBits: $0F;
  308. ABits: $F000;
  309. colorFormat: GL_BGRA;
  310. TexFormat: tfRGBA4;
  311. dType: GL_UNSIGNED_SHORT_4_4_4_4_REV),
  312. (ColorFlag: DDPF_RGB;
  313. RBits: $F800;
  314. GBits: $07E0;
  315. BBits: $1F;
  316. ABits: $00;
  317. colorFormat: GL_RGB;
  318. TexFormat: tfRGB5;
  319. dType: GL_UNSIGNED_SHORT_5_6_5),
  320. (ColorFlag: DDPF_L;
  321. RBits: $FFFF;
  322. GBits: $00;
  323. BBits: $00;
  324. ABits: $00;
  325. colorFormat: GL_LUMINANCE;
  326. TexFormat: tfLUMINANCE16;
  327. dType: GL_UNSIGNED_SHORT),
  328. (ColorFlag: DDPF_LA;
  329. RBits: $FF;
  330. GBits: $00;
  331. BBits: $00;
  332. ABits: $FF00;
  333. colorFormat: GL_LUMINANCE_ALPHA;
  334. TexFormat: tfLUMINANCE8_ALPHA8;
  335. dType: GL_UNSIGNED_BYTE),
  336. (ColorFlag: DDPF_RGBA;
  337. RBits: $7C00;
  338. GBits: $03E0;
  339. BBits: $1F;
  340. ABits: $8000;
  341. colorFormat: GL_BGRA;
  342. TexFormat: tfRGB5_A1;
  343. dType: GL_UNSIGNED_SHORT_1_5_5_5_REV)
  344. );
  345. cImageDataFormat24bits: array[0..0] of TGLImageDataFormat = (
  346. (ColorFlag: DDPF_RGB;
  347. RBits: $FF0000;
  348. GBits: $FF00;
  349. BBits: $FF;
  350. ABits: $00;
  351. colorFormat: GL_BGR;
  352. TexFormat: tfRGB8;
  353. dType: GL_UNSIGNED_BYTE)
  354. );
  355. cImageDataFormat32bits: array[0..6] of TGLImageDataFormat = (
  356. (ColorFlag: DDPF_RGBA;
  357. RBits: $FF;
  358. GBits: $FF00;
  359. BBits: $FF0000;
  360. ABits: $FF000000;
  361. colorFormat: GL_RGBA;
  362. TexFormat: tfRGBA8;
  363. dType: GL_UNSIGNED_BYTE),
  364. (ColorFlag: DDPF_RGBA;
  365. RBits: $FF0000;
  366. GBits: $FF00;
  367. BBits: $FF;
  368. ABits: $FF000000;
  369. colorFormat: GL_BGRA;
  370. TexFormat: tfRGBA8;
  371. dType: GL_UNSIGNED_BYTE),
  372. (ColorFlag: DDPF_RGBA;
  373. RBits: $3FF00000;
  374. GBits: $0FFC00;
  375. BBits: $03FF;
  376. ABits: $0C0000000;
  377. colorFormat: GL_RGBA;
  378. TexFormat: tfRGB10_A2;
  379. dType: GL_UNSIGNED_INT_2_10_10_10_REV),
  380. (ColorFlag: DDPF_RGBA;
  381. RBits: $03FF;
  382. GBits: $FFC00;
  383. BBits: $3FF00000;
  384. ABits: $C0000000;
  385. colorFormat: GL_BGRA;
  386. TexFormat: tfRGB10_A2;
  387. dType: GL_UNSIGNED_INT_2_10_10_10_REV),
  388. (ColorFlag: DDPF_RGBA;
  389. RBits: $FF0000;
  390. GBits: $FF00;
  391. BBits: $FF;
  392. ABits: $FF000000;
  393. colorFormat: GL_BGRA;
  394. TexFormat: tfRGB8;
  395. dType: GL_UNSIGNED_INT_8_8_8_8),
  396. (ColorFlag: DDPF_RGBA;
  397. RBits: $FF;
  398. GBits: $FF00;
  399. BBits: $FF0000;
  400. ABits: $FF000000;
  401. colorFormat: GL_RGBA;
  402. TexFormat: tfRGB8;
  403. dType: GL_UNSIGNED_INT_8_8_8_8),
  404. (ColorFlag: DDPF_RGB;
  405. RBits: $FFFF;
  406. GBits: $FFFF0000;
  407. BBits: $00;
  408. ABits: $00;
  409. colorFormat: GL_RG;
  410. TexFormat: tfRG16;
  411. dType: GL_UNSIGNED_SHORT)
  412. );
  413. procedure DecodeDXT1toBitmap32(
  414. encData, decData : PByteArray;
  415. w,h : Integer; var trans : Boolean);
  416. procedure DecodeDXT3toBitmap32(encData, decData : PByteArray; w,h : Integer);
  417. procedure DecodeDXT5toBitmap32(encData, decData : PByteArray; w,h : Integer);
  418. procedure flip_blocks_dxtc1( data : PGLubyte; numBlocks: integer);
  419. procedure flip_blocks_dxtc3( data : PGLubyte; numBlocks: integer);
  420. procedure flip_blocks_dxtc5( data : PGLubyte; numBlocks: integer);
  421. procedure flip_dxt5_alpha ( block : PDXT5AlphaBlock);
  422. function DDSHeaderToGLEnum(const DX9header: TDDSHeader;
  423. const DX11header: TDDS_HEADER_DXT10;
  424. const useDX11: Boolean;
  425. out iFormat: TGLInternalFormat;
  426. out colorFormat: Cardinal;
  427. out dataType: Cardinal;
  428. out bpe: Integer): Boolean;
  429. function GLEnumToDDSHeader(var DX9header: TDDSHeader;
  430. var DX11header: TDDS_HEADER_DXT10;
  431. const useDX11: Boolean;
  432. const iFormat: TGLInternalFormat;
  433. const colorFormat: Cardinal;
  434. const dataType: Cardinal;
  435. const bpe: Integer): Boolean;
  436. function FindDDSCompatibleDataFormat(const iFormat: TGLInternalFormat;
  437. out colorFormat: Cardinal;
  438. out dataType: Cardinal): Boolean;
  439. implementation
  440. procedure DecodeColor565(col : Word; out r,g,b : Byte);
  441. begin
  442. r:=col and $1F;
  443. g:=(col shr 5) and $3F;
  444. b:=(col shr 11) and $1F;
  445. end;
  446. procedure DecodeDXT1toBitmap32(
  447. encData, decData : PByteArray;
  448. w,h : Integer; var trans : Boolean);
  449. var
  450. x,y,i,j,k,select : Integer;
  451. col0, col1 : Word;
  452. colors : array[0..3] of array[0..3] of Byte;
  453. bitmask : Cardinal;
  454. temp : PGLubyte;
  455. r0,g0,b0,r1,g1,b1 : Byte;
  456. begin
  457. trans:=False;
  458. if not (Assigned(encData) and Assigned(decData)) then exit;
  459. temp:=PGLubyte(encData);
  460. for y:=0 to (h div 4)-1 do begin
  461. for x:=0 to (w div 4)-1 do begin
  462. col0:=PWord(temp)^; Inc(temp, 2);
  463. col1:=PWord(temp)^; Inc(temp, 2);
  464. bitmask:=PCardinal(temp)^; Inc(temp, 4);
  465. DecodeColor565(col0,r0,g0,b0);
  466. DecodeColor565(col1,r1,g1,b1);
  467. colors[0][0]:=r0 shl 3;
  468. colors[0][1]:=g0 shl 2;
  469. colors[0][2]:=b0 shl 3;
  470. colors[0][3]:=$FF;
  471. colors[1][0]:=r1 shl 3;
  472. colors[1][1]:=g1 shl 2;
  473. colors[1][2]:=b1 shl 3;
  474. colors[1][3]:=$FF;
  475. if col0>col1 then begin
  476. colors[2][0]:=(2*colors[0][0]+colors[1][0]+1) div 3;
  477. colors[2][1]:=(2*colors[0][1]+colors[1][1]+1) div 3;
  478. colors[2][2]:=(2*colors[0][2]+colors[1][2]+1) div 3;
  479. colors[2][3]:=$FF;
  480. colors[3][0]:=(colors[0][0]+2*colors[1][0]+1) div 3;
  481. colors[3][1]:=(colors[0][1]+2*colors[1][1]+1) div 3;
  482. colors[3][2]:=(colors[0][2]+2*colors[1][2]+1) div 3;
  483. colors[3][3]:=$FF;
  484. end else begin
  485. trans:=True;
  486. colors[2][0]:=(colors[0][0]+colors[1][0]) div 2;
  487. colors[2][1]:=(colors[0][1]+colors[1][1]) div 2;
  488. colors[2][2]:=(colors[0][2]+colors[1][2]) div 2;
  489. colors[2][3]:=$FF;
  490. colors[3][0]:=(colors[0][0]+2*colors[1][0]+1) div 3;
  491. colors[3][1]:=(colors[0][1]+2*colors[1][1]+1) div 3;
  492. colors[3][2]:=(colors[0][2]+2*colors[1][2]+1) div 3;
  493. colors[3][3]:=0;
  494. end;
  495. k:=0;
  496. for j:=0 to 3 do begin
  497. for i:=0 to 3 do begin
  498. select:=(bitmask and (3 shl (k*2))) shr (k*2);
  499. if ((4*x+i)<w) and ((4*y+j)<h) then
  500. PCardinal(@decData[((4*y+j)*w+(4*x+i))*4])^:=Cardinal(colors[select]);
  501. Inc(k);
  502. end;
  503. end;
  504. end;
  505. end;
  506. end;
  507. procedure DecodeDXT3toBitmap32(encData, decData : PByteArray; w,h : Integer);
  508. var
  509. x,y,i,j,k,select : Integer;
  510. col0, col1, wrd : Word;
  511. colors : array[0..3] of array[0..3] of Byte;
  512. bitmask, offset : Cardinal;
  513. temp : PGLubyte;
  514. r0,g0,b0,r1,g1,b1 : Byte;
  515. alpha : array[0..3] of Word;
  516. begin
  517. if not (Assigned(encData) and Assigned(decData)) then exit;
  518. temp:=PGLubyte(encData);
  519. for y:=0 to (h div 4)-1 do begin
  520. for x:=0 to (w div 4)-1 do begin
  521. alpha[0]:=PWord(temp)^; Inc(temp, 2);
  522. alpha[1]:=PWord(temp)^; Inc(temp, 2);
  523. alpha[2]:=PWord(temp)^; Inc(temp, 2);
  524. alpha[3]:=PWord(temp)^; Inc(temp, 2);
  525. col0:=PWord(temp)^; Inc(temp, 2);
  526. col1:=PWord(temp)^; Inc(temp, 2);
  527. bitmask:=PCardinal(temp)^; Inc(temp, 4);
  528. DecodeColor565(col0,r0,g0,b0);
  529. DecodeColor565(col1,r1,g1,b1);
  530. colors[0][0]:=r0 shl 3;
  531. colors[0][1]:=g0 shl 2;
  532. colors[0][2]:=b0 shl 3;
  533. colors[0][3]:=$FF;
  534. colors[1][0]:=r1 shl 3;
  535. colors[1][1]:=g1 shl 2;
  536. colors[1][2]:=b1 shl 3;
  537. colors[1][3]:=$FF;
  538. colors[2][0]:=(2*colors[0][0]+colors[1][0]+1) div 3;
  539. colors[2][1]:=(2*colors[0][1]+colors[1][1]+1) div 3;
  540. colors[2][2]:=(2*colors[0][2]+colors[1][2]+1) div 3;
  541. colors[2][3]:=$FF;
  542. colors[3][0]:=(colors[0][0]+2*colors[1][0]+1) div 3;
  543. colors[3][1]:=(colors[0][1]+2*colors[1][1]+1) div 3;
  544. colors[3][2]:=(colors[0][2]+2*colors[1][2]+1) div 3;
  545. colors[3][3]:=$FF;
  546. k:=0;
  547. for j:=0 to 3 do begin
  548. for i:=0 to 3 do begin
  549. select:=(bitmask and (3 shl (k*2))) shr (k*2);
  550. if ((4*x+i)<w) and ((4*y+j)<h) then
  551. PCardinal(@decData[((4*y+j)*w+(4*x+i))*4])^:=Cardinal(colors[select]);
  552. Inc(k);
  553. end;
  554. end;
  555. for j:=0 to 3 do begin
  556. wrd:=alpha[j];
  557. for i:=0 to 3 do begin
  558. if (((4*x+i)<w) and ((4*y+j)<h)) then begin
  559. offset:=((4*y+j)*w+(4*x+i))*4+3;
  560. decData[offset]:=wrd and $0F;
  561. decData[offset]:=decData[offset] or (decData[offset] shl 4);
  562. end;
  563. wrd:=wrd shr 4;
  564. end;
  565. end;
  566. end;
  567. end;
  568. end;
  569. procedure DecodeDXT5toBitmap32(encData, decData : PByteArray; w,h : Integer);
  570. var
  571. x,y,i,j,k,select : Integer;
  572. col0, col1 : Word;
  573. colors : array[0..3] of array[0..3] of Byte;
  574. bits, bitmask, offset : Cardinal;
  575. temp, alphamask : PGLubyte;
  576. r0,g0,b0,r1,g1,b1 : Byte;
  577. alphas : array[0..7] of Byte;
  578. begin
  579. if not (Assigned(encData) and Assigned(decData)) then exit;
  580. temp:=PGLubyte(encData);
  581. for y:=0 to (h div 4)-1 do begin
  582. for x:=0 to (w div 4)-1 do begin
  583. alphas[0]:=temp^; Inc(temp);
  584. alphas[1]:=temp^; Inc(temp);
  585. alphamask:=temp; Inc(temp, 6);
  586. col0:=PWord(temp)^; Inc(temp, 2);
  587. col1:=PWord(temp)^; Inc(temp, 2);
  588. bitmask:=PCardinal(temp)^; Inc(temp, 4);
  589. DecodeColor565(col0,r0,g0,b0);
  590. DecodeColor565(col1,r1,g1,b1);
  591. colors[0][0]:=r0 shl 3;
  592. colors[0][1]:=g0 shl 2;
  593. colors[0][2]:=b0 shl 3;
  594. colors[0][3]:=$FF;
  595. colors[1][0]:=r1 shl 3;
  596. colors[1][1]:=g1 shl 2;
  597. colors[1][2]:=b1 shl 3;
  598. colors[1][3]:=$FF;
  599. colors[2][0]:=(2*colors[0][0]+colors[1][0]+1) div 3;
  600. colors[2][1]:=(2*colors[0][1]+colors[1][1]+1) div 3;
  601. colors[2][2]:=(2*colors[0][2]+colors[1][2]+1) div 3;
  602. colors[2][3]:=$FF;
  603. colors[3][0]:=(colors[0][0]+2*colors[1][0]+1) div 3;
  604. colors[3][1]:=(colors[0][1]+2*colors[1][1]+1) div 3;
  605. colors[3][2]:=(colors[0][2]+2*colors[1][2]+1) div 3;
  606. colors[3][3]:=$FF;
  607. k:=0;
  608. for j:=0 to 3 do begin
  609. for i:=0 to 3 do begin
  610. select:=(bitmask and (3 shl (k*2))) shr (k*2);
  611. if ((4*x+i)<w) and ((4*y+j)<h) then
  612. PCardinal(@decData[((4*y+j)*w+(4*x+i))*4])^:=Cardinal(colors[select]);
  613. Inc(k);
  614. end;
  615. end;
  616. if (alphas[0] > alphas[1]) then begin
  617. alphas[2]:=(6*alphas[0]+1*alphas[1]+3) div 7;
  618. alphas[3]:=(5*alphas[0]+2*alphas[1]+3) div 7;
  619. alphas[4]:=(4*alphas[0]+3*alphas[1]+3) div 7;
  620. alphas[5]:=(3*alphas[0]+4*alphas[1]+3) div 7;
  621. alphas[6]:=(2*alphas[0]+5*alphas[1]+3) div 7;
  622. alphas[7]:=(1*alphas[0]+6*alphas[1]+3) div 7;
  623. end else begin
  624. alphas[2]:=(4*alphas[0]+1*alphas[1]+2) div 5;
  625. alphas[3]:=(3*alphas[0]+2*alphas[1]+2) div 5;
  626. alphas[4]:=(2*alphas[0]+3*alphas[1]+2) div 5;
  627. alphas[5]:=(1*alphas[0]+4*alphas[1]+2) div 5;
  628. alphas[6]:=0;
  629. alphas[7]:=$FF;
  630. end;
  631. bits:=PCardinal(alphamask)^;
  632. for j:=0 to 1 do begin
  633. for i:=0 to 3 do begin
  634. if (((4*x+i)<w) and ((4*y+j)<h)) then begin
  635. offset:=((4*y+j)*w+(4*x+i))*4+3;
  636. decData[Offset]:=alphas[bits and 7];
  637. end;
  638. bits:=bits shr 3;
  639. end;
  640. end;
  641. Inc(alphamask, 3);
  642. bits:=PCardinal(alphamask)^;
  643. for j:=2 to 3 do begin
  644. for i:=0 to 3 do begin
  645. if (((4*x+i)<w) and ((4*y+j)<h)) then begin
  646. offset:=((4*y+j)*w+(4*x+i))*4+3;
  647. decData[offset]:=alphas[bits and 7];
  648. end;
  649. bits:=bits shr 3;
  650. end;
  651. end;
  652. end;
  653. end;
  654. end;
  655. ////////////////////////////////////////////////////////////
  656. procedure flip_blocks_dxtc1( data : PGLubyte; numBlocks: integer);
  657. var
  658. curblock : PDXTColBlock;
  659. temp : Byte;
  660. i : integer;
  661. begin
  662. curblock := PDXTColBlock( data );
  663. for i := 0 to numBlocks-1 do begin
  664. temp := curblock.row[0];
  665. curblock.row[0] := curblock.row[3];
  666. curblock.row[3] := temp;
  667. temp := curblock.row[1];
  668. curblock.row[1] := curblock.row[2];
  669. curblock.row[2] := temp;
  670. Inc( curblock );
  671. end;
  672. end;
  673. // flip a DXT3 color block
  674. ////////////////////////////////////////////////////////////
  675. procedure flip_blocks_dxtc3( data: PGLubyte; numBlocks: integer );
  676. var
  677. curblock : PDXTColBlock;
  678. alphablock : PDXT3AlphaBlock;
  679. tempS : Word;
  680. tempB : Byte;
  681. i : integer;
  682. begin
  683. curblock := PDXTColBlock( data );
  684. for i := 0 to numBlocks-1 do
  685. begin
  686. alphablock := PDXT3AlphaBlock( curblock );
  687. tempS := alphablock.row[0];
  688. alphablock.row[0] := alphablock.row[3];
  689. alphablock.row[3] := tempS;
  690. tempS := alphablock.row[1];
  691. alphablock.row[1] := alphablock.row[2];
  692. alphablock.row[2] := tempS;
  693. Inc( curblock );
  694. tempB := curblock.row[0];
  695. curblock.row[0] := curblock.row[3];
  696. curblock.row[3] := tempB;
  697. tempB := curblock.row[1];
  698. curblock.row[1] := curblock.row[2];
  699. curblock.row[2] := tempB;
  700. Inc( curblock );
  701. end;
  702. end;
  703. ////////////////////////////////////////////////////////////
  704. procedure flip_dxt5_alpha( block : PDXT5AlphaBlock);
  705. const
  706. mask = $00000007; // bits = 00 00 01 11
  707. var
  708. gBits : array[0..3, 0..3] of Byte;
  709. bits : Integer;
  710. begin
  711. bits := 0;
  712. Move(block.row[0], bits, sizeof(Byte) * 3);
  713. gBits[0][0] := Byte(bits and mask);
  714. bits := bits shr 3;
  715. gBits[0][1] := Byte(bits and mask);
  716. bits := bits shr 3;
  717. gBits[0][2] := Byte(bits and mask);
  718. bits := bits shr 3;
  719. gBits[0][3] := Byte(bits and mask);
  720. bits := bits shr 3;
  721. gBits[1][0] := Byte(bits and mask);
  722. bits := bits shr 3;
  723. gBits[1][1] := Byte(bits and mask);
  724. bits := bits shr 3;
  725. gBits[1][2] := Byte(bits and mask);
  726. bits := bits shr 3;
  727. gBits[1][3] := Byte(bits and mask);
  728. bits := 0;
  729. Move(block.row[3], bits, sizeof(Byte) * 3);
  730. gBits[2][0] := Byte(bits and mask);
  731. bits := bits shr 3;
  732. gBits[2][1] := Byte(bits and mask);
  733. bits := bits shr 3;
  734. gBits[2][2] := Byte(bits and mask);
  735. bits := bits shr 3;
  736. gBits[2][3] := Byte(bits and mask);
  737. bits := bits shr 3;
  738. gBits[3][0] := Byte(bits and mask);
  739. bits := bits shr 3;
  740. gBits[3][1] := Byte(bits and mask);
  741. bits := bits shr 3;
  742. gBits[3][2] := Byte(bits and mask);
  743. bits := bits shr 3;
  744. gBits[3][3] := Byte(bits and mask);
  745. // clear existing alpha bits
  746. FillChar( block.row, sizeof(Byte) * 6, 0);
  747. bits := block.row[0]+block.row[1]*$100+block.row[2]*$10000;
  748. bits := bits or (gBits[3][0] shl 0);
  749. bits := bits or (gBits[3][1] shl 3);
  750. bits := bits or (gBits[3][2] shl 6);
  751. bits := bits or (gBits[3][3] shl 9);
  752. bits := bits or (gBits[2][0] shl 12);
  753. bits := bits or (gBits[2][1] shl 15);
  754. bits := bits or (gBits[2][2] shl 18);
  755. bits := bits or (gBits[2][3] shl 21);
  756. block.row[0] := bits and $FF;
  757. block.row[1] := (bits shr 8) and $FF;
  758. block.row[2] := (bits shr 16) and $FF;
  759. bits := block.row[3]+block.row[4]*$100+block.row[5]*$10000;
  760. bits := bits or (gBits[1][0] shl 0);
  761. bits := bits or (gBits[1][1] shl 3);
  762. bits := bits or (gBits[1][2] shl 6);
  763. bits := bits or (gBits[1][3] shl 9);
  764. bits := bits or (gBits[0][0] shl 12);
  765. bits := bits or (gBits[0][1] shl 15);
  766. bits := bits or (gBits[0][2] shl 18);
  767. bits := bits or (gBits[0][3] shl 21);
  768. block.row[3] := bits and $FF;
  769. block.row[4] := (bits shr 8) and $FF;
  770. block.row[5] := (bits shr 16) and $FF;
  771. end;
  772. ////////////////////////////////////////////////////////////
  773. procedure flip_blocks_dxtc5( data: PGLubyte; numBlocks: integer );
  774. var
  775. curblock : PDXTColBlock;
  776. temp : Byte;
  777. i : integer;
  778. begin
  779. curblock := PDXTColBlock( data );
  780. for i := 0 to numBlocks-1 do
  781. begin
  782. flip_dxt5_alpha( PDXT5AlphaBlock( curblock ) );
  783. Inc( curblock );
  784. temp := curblock.row[0];
  785. curblock.row[0] := curblock.row[3];
  786. curblock.row[3] := temp;
  787. temp := curblock.row[1];
  788. curblock.row[1] := curblock.row[2];
  789. curblock.row[2] := temp;
  790. Inc( curblock );
  791. end;
  792. end;
  793. function DDSHeaderToGLEnum(const DX9header: TDDSHeader;
  794. const DX11header: TDDS_HEADER_DXT10;
  795. const useDX11: Boolean;
  796. out iFormat: TGLInternalFormat;
  797. out colorFormat: Cardinal;
  798. out dataType: Cardinal;
  799. out bpe: Integer): Boolean;
  800. var
  801. i: Integer;
  802. begin
  803. Result := true;
  804. if useDX11 then
  805. begin
  806. Assert(false, 'DXGI images not supported.');
  807. end
  808. // Use DX9 formats
  809. else begin
  810. // figure out what the image format is
  811. if (DX9header.SurfaceFormat.ddpf.dwFlags and DDPF_FOURCC)<>0 then
  812. begin
  813. case DX9header.SurfaceFormat.ddpf.dwFourCC of
  814. FOURCC_DXT1:
  815. begin
  816. colorFormat := GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
  817. iFormat := tfCOMPRESSED_RGBA_S3TC_DXT1;
  818. dataType := GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
  819. bpe := 8;
  820. end;
  821. FOURCC_DXT2, FOURCC_DXT3:
  822. begin
  823. colorFormat := GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
  824. iFormat := tfCOMPRESSED_RGBA_S3TC_DXT3;
  825. dataType := GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
  826. bpe := 16;
  827. end;
  828. FOURCC_DXT4, FOURCC_DXT5:
  829. begin
  830. colorFormat := GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
  831. iFormat := tfCOMPRESSED_RGBA_S3TC_DXT5;
  832. dataType := GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
  833. bpe := 16;
  834. end;
  835. FOURCC_ATI1:
  836. begin
  837. colorFormat := GL_COMPRESSED_RED_RGTC1;
  838. iFormat := tfCOMPRESSED_RED_RGTC1;
  839. dataType := GL_COMPRESSED_RED_RGTC1;
  840. bpe := 8;
  841. end;
  842. FOURCC_ATI2:
  843. begin
  844. colorFormat := GL_COMPRESSED_RG_RGTC2;
  845. iFormat := tfCOMPRESSED_RG_RGTC2;
  846. dataType := GL_COMPRESSED_RG_RGTC2;
  847. bpe := 16;
  848. end;
  849. FOURCC_R8G8B8:
  850. begin
  851. colorFormat := GL_BGR;
  852. iFormat := tfRGB8;
  853. dataType := GL_UNSIGNED_BYTE;
  854. bpe := 3;
  855. end;
  856. FOURCC_A8R8G8B8:
  857. begin
  858. colorFormat := GL_BGRA;
  859. iFormat := tfRGBA8;
  860. dataType := GL_UNSIGNED_BYTE;
  861. bpe := 4;
  862. end;
  863. FOURCC_X8R8G8B8:
  864. begin
  865. colorFormat := GL_BGRA;
  866. iFormat := tfRGB8;
  867. dataType := GL_UNSIGNED_INT_8_8_8_8;
  868. bpe := 4;
  869. end;
  870. FOURCC_R5G6B5:
  871. begin
  872. colorFormat := GL_RGB;
  873. iFormat := tfRGB5;
  874. dataType := GL_UNSIGNED_SHORT_5_6_5;
  875. bpe := 2;
  876. end;
  877. FOURCC_A8:
  878. begin
  879. colorFormat := GL_ALPHA;
  880. iFormat := tfALPHA8;
  881. dataType := GL_UNSIGNED_BYTE;
  882. bpe := 1;
  883. end;
  884. FOURCC_A2B10G10R10:
  885. begin
  886. colorFormat := GL_RGBA;
  887. iFormat := tfRGB10_A2;
  888. dataType := GL_UNSIGNED_INT_10_10_10_2;
  889. bpe := 4;
  890. end;
  891. FOURCC_A8B8G8R8:
  892. begin
  893. colorFormat := GL_RGBA;
  894. iFormat := tfRGBA8;
  895. dataType := GL_UNSIGNED_BYTE;
  896. bpe := 4;
  897. end;
  898. FOURCC_X8B8G8R8:
  899. begin
  900. colorFormat := GL_RGBA;
  901. iFormat := tfRGB8;
  902. dataType := GL_UNSIGNED_INT_8_8_8_8;
  903. bpe := 4;
  904. end;
  905. FOURCC_A2R10G10B10:
  906. begin
  907. colorFormat := GL_BGRA;
  908. iFormat := tfRGB10_A2;
  909. dataType := GL_UNSIGNED_INT_10_10_10_2;
  910. bpe := 4;
  911. end;
  912. FOURCC_A16B16G16R16:
  913. begin
  914. colorFormat := GL_RGBA;
  915. iFormat := tfR16G16B16A16;
  916. dataType := GL_UNSIGNED_SHORT;
  917. bpe := 8;
  918. end;
  919. FOURCC_L8:
  920. begin
  921. colorFormat := GL_LUMINANCE;
  922. iFormat := tfLUMINANCE8;
  923. dataType := GL_UNSIGNED_BYTE;
  924. bpe := 1;
  925. end;
  926. FOURCC_A8L8:
  927. begin
  928. colorFormat := GL_LUMINANCE_ALPHA;
  929. iFormat := tfLUMINANCE8_ALPHA8;
  930. dataType := GL_UNSIGNED_BYTE;
  931. bpe := 2;
  932. end;
  933. FOURCC_L16:
  934. begin
  935. colorFormat := GL_LUMINANCE;
  936. iFormat := tfLUMINANCE16;
  937. dataType := GL_UNSIGNED_SHORT;
  938. bpe := 2;
  939. end;
  940. FOURCC_R16F:
  941. begin
  942. colorFormat := GL_RED;
  943. iFormat := tfLUMINANCE_FLOAT16;
  944. dataType := GL_HALF_FLOAT_ARB;
  945. bpe := 2;
  946. end;
  947. FOURCC_A16B16G16R16F:
  948. begin
  949. colorFormat := GL_RGBA;
  950. iFormat := tfRGBA_FLOAT16;
  951. dataType := GL_HALF_FLOAT_ARB;
  952. bpe := 8;
  953. end;
  954. FOURCC_R32F:
  955. begin
  956. colorFormat := GL_RED;
  957. iFormat := tfLUMINANCE_FLOAT32;
  958. dataType := GL_FLOAT;
  959. bpe := 4;
  960. end;
  961. FOURCC_G16R16:
  962. begin
  963. colorFormat := GL_RG;
  964. iFormat := tfRG16;
  965. dataType := GL_UNSIGNED_SHORT;
  966. bpe := 4;
  967. end;
  968. FOURCC_G16R16F:
  969. begin
  970. colorFormat := GL_RG;
  971. iFormat := tfRG16F;
  972. dataType := GL_HALF_FLOAT;
  973. bpe := 4;
  974. end;
  975. FOURCC_G32R32F:
  976. begin
  977. colorFormat := GL_RG;
  978. iFormat := tfRG32F;
  979. dataType := GL_FLOAT;
  980. bpe := 8;
  981. end;
  982. FOURCC_UNKNOWN,
  983. FOURCC_X1R5G5B5,
  984. FOURCC_A1R5G5B5,
  985. FOURCC_A4R4G4B4,
  986. FOURCC_R3G3B2,
  987. FOURCC_A8R3G3B2,
  988. FOURCC_X4R4G4B4,
  989. FOURCC_A4L4,
  990. FOURCC_D16_LOCKABLE,
  991. FOURCC_D32,
  992. FOURCC_D24X8,
  993. FOURCC_D16,
  994. FOURCC_D32F_LOCKABLE: Result := false; //these are unsupported for now
  995. end; // of case
  996. end // not FOURCC
  997. else
  998. with DX9header.SurfaceFormat.ddpf do
  999. case dwRGBBitCount of
  1000. 8: begin
  1001. for i := 0 to High(cImageDataFormat8bits) do
  1002. if (cImageDataFormat8bits[i].ColorFlag = dwFlags)
  1003. and (cImageDataFormat8bits[i].RBits = dwRBitMask)
  1004. and (cImageDataFormat8bits[i].GBits = dwGBitMask)
  1005. and (cImageDataFormat8bits[i].BBits = dwBBitMask)
  1006. and (cImageDataFormat8bits[i].ABits = dwRGBAlphaBitMask) then
  1007. begin
  1008. colorFormat := cImageDataFormat8bits[i].colorFormat;
  1009. iFormat := cImageDataFormat8bits[i].TexFormat;
  1010. dataType := cImageDataFormat8bits[i].dType;
  1011. Result := true;
  1012. Break;
  1013. end;
  1014. bpe := 1;
  1015. end;
  1016. 16: begin
  1017. for i := 0 to High(cImageDataFormat16bits) do
  1018. if (cImageDataFormat16bits[i].ColorFlag = dwFlags)
  1019. and (cImageDataFormat16bits[i].RBits = dwRBitMask)
  1020. and (cImageDataFormat16bits[i].GBits = dwGBitMask)
  1021. and (cImageDataFormat16bits[i].BBits = dwBBitMask)
  1022. and (cImageDataFormat16bits[i].ABits = dwRGBAlphaBitMask) then
  1023. begin
  1024. colorFormat := cImageDataFormat16bits[i].colorFormat;
  1025. iFormat := cImageDataFormat16bits[i].TexFormat;
  1026. dataType := cImageDataFormat16bits[i].dType;
  1027. Result := true;
  1028. Break;
  1029. end;
  1030. bpe := 2;
  1031. end;
  1032. 24: begin
  1033. for i := 0 to High(cImageDataFormat24bits) do
  1034. if (cImageDataFormat24bits[i].ColorFlag = dwFlags)
  1035. and (cImageDataFormat24bits[i].RBits = dwRBitMask)
  1036. and (cImageDataFormat24bits[i].GBits = dwGBitMask)
  1037. and (cImageDataFormat24bits[i].BBits = dwBBitMask)
  1038. and (cImageDataFormat24bits[i].ABits = dwRGBAlphaBitMask) then
  1039. begin
  1040. colorFormat := cImageDataFormat24bits[i].colorFormat;
  1041. iFormat := cImageDataFormat24bits[i].TexFormat;
  1042. dataType := cImageDataFormat24bits[i].dType;
  1043. Result := true;
  1044. Break;
  1045. end;
  1046. bpe := 3;
  1047. end;
  1048. 32: begin
  1049. for i := 0 to High(cImageDataFormat32bits) do
  1050. if (cImageDataFormat32bits[i].ColorFlag = dwFlags)
  1051. and (cImageDataFormat32bits[i].RBits = dwRBitMask)
  1052. and (cImageDataFormat32bits[i].GBits = dwGBitMask)
  1053. and (cImageDataFormat32bits[i].BBits = dwBBitMask)
  1054. and (cImageDataFormat32bits[i].ABits = dwRGBAlphaBitMask) then
  1055. begin
  1056. colorFormat := cImageDataFormat32bits[i].colorFormat;
  1057. iFormat := cImageDataFormat32bits[i].TexFormat;
  1058. dataType := cImageDataFormat32bits[i].dType;
  1059. Result := true;
  1060. Break;
  1061. end;
  1062. bpe := 4;
  1063. end;
  1064. else Result := false;
  1065. end; // of case
  1066. end;
  1067. end;
  1068. function GLEnumToDDSHeader(var DX9header: TDDSHeader;
  1069. var DX11header: TDDS_HEADER_DXT10;
  1070. const useDX11: Boolean;
  1071. const iFormat: TGLInternalFormat;
  1072. const colorFormat: Cardinal;
  1073. const dataType: Cardinal;
  1074. const bpe: Integer): Boolean;
  1075. var
  1076. i: Integer;
  1077. begin
  1078. Result := true;
  1079. if useDX11 then
  1080. begin
  1081. Assert(false, 'DXGI images not supported.');
  1082. end;
  1083. if IsCompressedFormat(iFormat) then
  1084. begin
  1085. with DX9header.SurfaceFormat.ddpf do
  1086. begin
  1087. dwFlags := DDPF_FOURCC;
  1088. case iFormat of
  1089. tfCOMPRESSED_RGB_S3TC_DXT1: dwFourCC := FOURCC_DXT1;
  1090. tfCOMPRESSED_RGBA_S3TC_DXT1: dwFourCC := FOURCC_DXT1;
  1091. tfCOMPRESSED_RGBA_S3TC_DXT3: dwFourCC := FOURCC_DXT3;
  1092. tfCOMPRESSED_RGBA_S3TC_DXT5: dwFourCC := FOURCC_DXT5;
  1093. tfCOMPRESSED_LUMINANCE_LATC1: dwFourCC := FOURCC_ATI1;
  1094. tfCOMPRESSED_LUMINANCE_ALPHA_LATC2: dwFourCC := FOURCC_ATI2;
  1095. else Result := false;
  1096. end;
  1097. end;
  1098. end
  1099. else if IsFloatFormat(iFormat) then
  1100. begin
  1101. with DX9header.SurfaceFormat.ddpf do
  1102. begin
  1103. dwFlags := DDPF_FOURCC;
  1104. case iFormat of
  1105. tfINTENSITY_FLOAT16,
  1106. tfLUMINANCE_FLOAT16,
  1107. tfR16F: dwFourCC := FOURCC_R16F;
  1108. tfRGBA_FLOAT16: dwFourCC := FOURCC_A16B16G16R16F;
  1109. tfINTENSITY_FLOAT32,
  1110. tfLUMINANCE_FLOAT32,
  1111. tfR32F: dwFourCC := FOURCC_R32F;
  1112. tfLUMINANCE_ALPHA_FLOAT16,
  1113. tfRG16F: dwFourCC := FOURCC_G16R16F;
  1114. tfLUMINANCE_ALPHA_FLOAT32,
  1115. tfRG32F: dwFourCC := FOURCC_G32R32F;
  1116. tfRGBA_FLOAT32: dwFourCC := FOURCC_A32B32G32R32F
  1117. else Result := false;
  1118. end;
  1119. end;
  1120. end
  1121. else with DX9header.SurfaceFormat.ddpf do
  1122. begin
  1123. dwFourCC := 0;
  1124. dwRGBBitCount := bpe * 8;
  1125. case bpe of
  1126. 1: begin
  1127. for i := 0 to High(cImageDataFormat8bits) do
  1128. if (cImageDataFormat8bits[i].colorFormat = colorFormat)
  1129. and (cImageDataFormat8bits[i].TexFormat = iFormat)
  1130. and (cImageDataFormat8bits[i].dType = dataType) then
  1131. begin
  1132. dwFlags := cImageDataFormat8bits[i].ColorFlag;
  1133. dwRBitMask := cImageDataFormat8bits[i].RBits;
  1134. dwGBitMask := cImageDataFormat8bits[i].GBits;
  1135. dwBBitMask := cImageDataFormat8bits[i].BBits;
  1136. dwRGBAlphaBitMask := cImageDataFormat8bits[i].ABits;
  1137. Break;
  1138. end;
  1139. end;
  1140. 2: begin
  1141. for i := 0 to High(cImageDataFormat16bits) do
  1142. if (cImageDataFormat16bits[i].colorFormat = colorFormat)
  1143. and (cImageDataFormat16bits[i].TexFormat = iFormat)
  1144. and (cImageDataFormat16bits[i].dType = dataType) then
  1145. begin
  1146. dwFlags := cImageDataFormat16bits[i].ColorFlag;
  1147. dwRBitMask := cImageDataFormat16bits[i].RBits;
  1148. dwGBitMask := cImageDataFormat16bits[i].GBits;
  1149. dwBBitMask := cImageDataFormat16bits[i].BBits;
  1150. dwRGBAlphaBitMask := cImageDataFormat16bits[i].ABits;
  1151. Break;
  1152. end;
  1153. end;
  1154. 3: begin
  1155. for i := 0 to High(cImageDataFormat24bits) do
  1156. if (cImageDataFormat24bits[i].colorFormat = colorFormat)
  1157. and (cImageDataFormat24bits[i].TexFormat = iFormat)
  1158. and (cImageDataFormat24bits[i].dType = dataType) then
  1159. begin
  1160. dwFlags := cImageDataFormat24bits[i].ColorFlag;
  1161. dwRBitMask := cImageDataFormat24bits[i].RBits;
  1162. dwGBitMask := cImageDataFormat24bits[i].GBits;
  1163. dwBBitMask := cImageDataFormat24bits[i].BBits;
  1164. dwRGBAlphaBitMask := cImageDataFormat24bits[i].ABits;
  1165. Break;
  1166. end;
  1167. end;
  1168. 4: begin
  1169. for i := 0 to High(cImageDataFormat32bits) do
  1170. if (cImageDataFormat32bits[i].colorFormat = colorFormat)
  1171. and (cImageDataFormat32bits[i].TexFormat = iFormat)
  1172. and (cImageDataFormat32bits[i].dType = dataType) then
  1173. begin
  1174. dwFlags := cImageDataFormat32bits[i].ColorFlag;
  1175. dwRBitMask := cImageDataFormat32bits[i].RBits;
  1176. dwGBitMask := cImageDataFormat32bits[i].GBits;
  1177. dwBBitMask := cImageDataFormat32bits[i].BBits;
  1178. dwRGBAlphaBitMask := cImageDataFormat32bits[i].ABits;
  1179. Break;
  1180. end;
  1181. end;
  1182. else Result := false;
  1183. end; // of case
  1184. end;
  1185. end;
  1186. function FindDDSCompatibleDataFormat(const iFormat: TGLInternalFormat;
  1187. out colorFormat: Cardinal;
  1188. out dataType: Cardinal): Boolean;
  1189. var
  1190. i: Integer;
  1191. begin
  1192. Result := false;
  1193. // 32 bits data format
  1194. for i := 0 to High(cImageDataFormat32bits) do
  1195. if cImageDataFormat32bits[i].TexFormat = iFormat then
  1196. begin
  1197. colorFormat := cImageDataFormat32bits[i].colorFormat;
  1198. dataType := cImageDataFormat32bits[i].dType;
  1199. Result := true;
  1200. Exit;
  1201. end;
  1202. // 24 bits data format
  1203. for i := 0 to High(cImageDataFormat24bits) do
  1204. if cImageDataFormat24bits[i].TexFormat = iFormat then
  1205. begin
  1206. colorFormat := cImageDataFormat24bits[i].colorFormat;
  1207. dataType := cImageDataFormat24bits[i].dType;
  1208. Result := true;
  1209. Exit;
  1210. end;
  1211. // 16 bits data format
  1212. for i := 0 to High(cImageDataFormat16bits) do
  1213. if cImageDataFormat16bits[i].TexFormat = iFormat then
  1214. begin
  1215. colorFormat := cImageDataFormat16bits[i].colorFormat;
  1216. dataType := cImageDataFormat16bits[i].dType;
  1217. Result := true;
  1218. Exit;
  1219. end;
  1220. // 8 bits data format
  1221. for i := 0 to High(cImageDataFormat8bits) do
  1222. if cImageDataFormat8bits[i].TexFormat = iFormat then
  1223. begin
  1224. colorFormat := cImageDataFormat8bits[i].colorFormat;
  1225. dataType := cImageDataFormat8bits[i].dType;
  1226. Result := true;
  1227. Exit;
  1228. end;
  1229. end;
  1230. end.