FileDXTC.pas 43 KB

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