FileDXTC.pas 43 KB

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