GXS.FileDDS.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. //
  2. // The graphics engine GLXEngine. The unit of GXScene for Delphi
  3. //
  4. unit GXS.FileDDS;
  5. (* DDS File support *)
  6. interface
  7. {$I Stage.Defines.inc}
  8. uses
  9. Winapi.OpenGL,
  10. Winapi.OpenGLext,
  11. System.Classes,
  12. System.SysUtils,
  13. System.Math,
  14. Stage.VectorGeometry,
  15. Stage.RGBE,
  16. Stage.Strings,
  17. Stage.TextureFormat,
  18. GXS.ApplicationFileIO,
  19. GXS.Context,
  20. GXS.Graphics,
  21. Formatx.DXTC;
  22. type
  23. TgxDDSDetailLevels = (ddsHighDet, ddsMediumDet, ddsLowDet);
  24. TgxDDSImage = class(TgxBaseImage)
  25. private
  26. procedure flipSurface(chgData: PGLubyte; w, h, d: integer);
  27. public
  28. class function Capabilities: TDataFileCapabilities; override;
  29. procedure LoadFromFile(const filename: string); override;
  30. procedure SaveToFile(const filename: string); override;
  31. procedure LoadFromStream(stream: TStream); override;
  32. procedure SaveToStream(stream: TStream); override;
  33. // Assigns from any Texture.
  34. procedure AssignFromTexture(textureContext: TgxContext; const textureHandle: GLuint;
  35. textureTarget: TglTextureTarget; const CurrentFormat: Boolean; const intFormat: TglInternalFormat); reintroduce;
  36. end;
  37. var
  38. (* Variable determines which resolution to use textures,
  39. high - it loads all levels,
  40. midle - skipped the first level,
  41. low - skipped the first two levels. *)
  42. vDDSDetailLevel: TgxDDSDetailLevels = ddsHighDet;
  43. // ======================================================
  44. implementation
  45. // ======================================================
  46. // ------------------
  47. // ------------------ TgxDDSImage ------------------
  48. // ------------------
  49. procedure TgxDDSImage.LoadFromFile(const filename: string);
  50. var
  51. fs: TStream;
  52. begin
  53. if FileStreamExists(filename) then
  54. begin
  55. fs := TFileStream.Create(filename, fmOpenRead);
  56. try
  57. LoadFromStream(fs);
  58. finally
  59. fs.Free;
  60. ResourceName := filename;
  61. end;
  62. end
  63. else
  64. raise EInvalidRasterFile.CreateFmt(strFileNotFound, [filename]);
  65. end;
  66. procedure TgxDDSImage.SaveToFile(const filename: string);
  67. var
  68. fs: TStream;
  69. begin
  70. fs := TFileStream.Create(filename, fmOpenWrite or fmCreate);
  71. try
  72. SaveToStream(fs);
  73. finally
  74. fs.Free;
  75. end;
  76. ResourceName := filename;
  77. end;
  78. procedure TgxDDSImage.LoadFromStream(stream: TStream);
  79. var
  80. header: TDDSHeader;
  81. DX10header: TDDS_HEADER_DXT10;
  82. btcCompressed: Boolean;
  83. face, faceCount, level: integer;
  84. w, h, d, bw, bh, size, offset: integer;
  85. bDXT10Header: Boolean;
  86. procedure CalcSize;
  87. begin
  88. if btcCompressed then
  89. begin
  90. bw := (w + 3) div 4;
  91. bh := (h + 3) div 4;
  92. end
  93. else
  94. begin
  95. bw := w;
  96. bh := h;
  97. end;
  98. if d = 0 then
  99. d := 1;
  100. size := bw * bh * d * fElementSize;
  101. end;
  102. begin
  103. stream.Read(header, Sizeof(TDDSHeader));
  104. // DDS files always start with the same magic number ("DDS ")
  105. if TFOURCC(header.Magic) <> 'DDS ' then
  106. raise EInvalidRasterFile.Create('Invalid DDS file');
  107. // Verify header to validate DDS file
  108. if (header.SurfaceFormat.dwSize <> Sizeof(TDDSURFACEDESC2)) or (header.SurfaceFormat.ddpf.dwSize <> Sizeof(TDDPIXELFORMAT))
  109. then
  110. raise EInvalidRasterFile.Create('Invalid DDS file');
  111. // Check for DX10 extension
  112. bDXT10Header := (header.SurfaceFormat.ddpf.dwFlags and DDPF_FOURCC <> 0) and
  113. (header.SurfaceFormat.ddpf.dwFourCC = FOURCC_DX10);
  114. if bDXT10Header then
  115. stream.Read(DX10header, Sizeof(TDDS_HEADER_DXT10));
  116. with header.SurfaceFormat do
  117. begin
  118. { There are flags that are supposed to mark these fields as valid,
  119. but some dds files don't set them properly }
  120. UnMipmap;
  121. FLOD[0].Width := dwWidth;
  122. FLOD[0].Height := dwHeight;
  123. // check if image is a volume texture
  124. if ((dwCaps2 and DDSCAPS2_VOLUME) <> 0) and (dwDepth > 0) then
  125. FLOD[0].Depth := dwDepth
  126. else
  127. FLOD[0].Depth := 0;
  128. if (dwFlags and DDSD_MIPMAPCOUNT) <> 0 then
  129. fLevelCount := MaxInteger(dwMipMapCount, 1)
  130. else
  131. fLevelCount := 1;
  132. // check cube-map faces
  133. fCubeMap := false;
  134. faceCount := 0;
  135. if (dwCaps2 and DDSCAPS2_CUBEMAP) <> 0 then
  136. begin
  137. // this is a cubemap, count the faces
  138. if (dwCaps2 and DDSCAPS2_CUBEMAP_POSITIVEX) <> 0 then
  139. Inc(faceCount);
  140. if (dwCaps2 and DDSCAPS2_CUBEMAP_NEGATIVEX) <> 0 then
  141. Inc(faceCount);
  142. if (dwCaps2 and DDSCAPS2_CUBEMAP_POSITIVEY) <> 0 then
  143. Inc(faceCount);
  144. if (dwCaps2 and DDSCAPS2_CUBEMAP_NEGATIVEY) <> 0 then
  145. Inc(faceCount);
  146. if (dwCaps2 and DDSCAPS2_CUBEMAP_POSITIVEZ) <> 0 then
  147. Inc(faceCount);
  148. if (dwCaps2 and DDSCAPS2_CUBEMAP_NEGATIVEZ) <> 0 then
  149. Inc(faceCount);
  150. // check for a complete cubemap
  151. if (faceCount <> 6) or (GetWidth <> GetHeight) then
  152. raise EInvalidRasterFile.Create('Invalid cubemap');
  153. fCubeMap := true;
  154. end;
  155. fTextureArray := false;
  156. if not DDSHeaderToGLEnum(header, DX10header, bDXT10Header,
  157. fInternalFormat, fColorFormat, fDataType, fElementSize) then
  158. raise EInvalidRasterFile.Create('DDS errorneus format');
  159. btcCompressed := IsCompressedFormat(fInternalFormat);
  160. end; // of with
  161. offset := 0;
  162. case vDDSDetailLevel of
  163. ddsHighDet:
  164. ; // Do nothing..
  165. ddsMediumDet:
  166. if fLevelCount > 1 then
  167. begin
  168. w := FLOD[0].Width;
  169. h := FLOD[0].Height;
  170. d := FLOD[0].Depth;
  171. CalcSize;
  172. offset := size;
  173. FLOD[0].Width := FLOD[0].Width div 2;
  174. FLOD[0].Height := FLOD[0].Height div 2;
  175. FLOD[0].Depth := FLOD[0].Depth div 2;
  176. Dec(fLevelCount);
  177. end;
  178. ddsLowDet:
  179. if fLevelCount > 2 then
  180. begin
  181. w := FLOD[0].Width;
  182. h := FLOD[0].Height;
  183. d := FLOD[0].Depth;
  184. CalcSize;
  185. offset := size;
  186. Div2(w);
  187. Div2(h);
  188. Div2(d);
  189. CalcSize;
  190. offset := offset + size;
  191. FLOD[0].Width := FLOD[0].Width div 4;
  192. FLOD[0].Height := FLOD[0].Height div 4;
  193. FLOD[0].Depth := FLOD[0].Depth div 4;
  194. Dec(fLevelCount, 2);
  195. end;
  196. else
  197. Assert(false, strErrorEx + strUnknownType);
  198. end;
  199. ReallocMem(fData, DataSize);
  200. if not fCubeMap then
  201. faceCount := 1;
  202. for face := 0 to faceCount - 1 do
  203. begin
  204. if offset > 0 then
  205. stream.Seek(offset, soCurrent);
  206. for level := 0 to fLevelCount - 1 do
  207. begin
  208. stream.Read(GetLevelAddress(level, face)^, GetLevelSizeInByte(level) div faceCount);
  209. if not fCubeMap and vVerticalFlipDDS then
  210. flipSurface(GetLevelAddress(level, face), FLOD[level].Width, FLOD[level].Height, FLOD[level].Depth);
  211. end;
  212. end; // for level
  213. end;
  214. procedure TgxDDSImage.SaveToStream(stream: TStream);
  215. const
  216. Magic: array [0 .. 3] of AnsiChar = 'DDS ';
  217. var
  218. header: TDDSHeader;
  219. DX10header: TDDS_HEADER_DXT10;
  220. buffer: PGLubyte;
  221. level, size: integer;
  222. begin
  223. FillChar(header, Sizeof(TDDSHeader), 0);
  224. header.Magic := Cardinal(Magic);
  225. header.SurfaceFormat.dwSize := Sizeof(TDDSURFACEDESC2);
  226. header.SurfaceFormat.ddpf.dwSize := Sizeof(TDDPIXELFORMAT);
  227. header.SurfaceFormat.dwWidth := GetWidth;
  228. header.SurfaceFormat.dwHeight := GetHeight;
  229. header.SurfaceFormat.dwDepth := GetDepth;
  230. header.SurfaceFormat.dwPitchOrLinearSize := fElementSize * GetWidth;
  231. header.SurfaceFormat.dwFlags := DDSD_CAPS or DDSD_HEIGHT or DDSD_WIDTH or DDSD_PIXELFORMAT;
  232. if IsCompressed then
  233. begin
  234. header.SurfaceFormat.dwPitchOrLinearSize := header.SurfaceFormat.dwPitchOrLinearSize * Cardinal(GetHeight) *
  235. Cardinal(GetDepth);
  236. header.SurfaceFormat.dwFlags := header.SurfaceFormat.dwFlags or DDSD_PITCH;
  237. end
  238. else
  239. header.SurfaceFormat.dwFlags := header.SurfaceFormat.dwFlags or DDSD_LINEARSIZE;
  240. header.SurfaceFormat.dwCaps := DDSCAPS_TEXTURE;
  241. header.SurfaceFormat.dwCaps2 := 0;
  242. if IsVolume then
  243. begin
  244. header.SurfaceFormat.dwFlags := header.SurfaceFormat.dwFlags or DDSD_DEPTH;
  245. header.SurfaceFormat.dwCaps := header.SurfaceFormat.dwCaps or DDSCAPS_COMPLEX;
  246. header.SurfaceFormat.dwCaps2 := header.SurfaceFormat.dwCaps2 or DDSCAPS2_VOLUME;
  247. end;
  248. if fLevelCount > 1 then
  249. begin
  250. header.SurfaceFormat.dwCaps := header.SurfaceFormat.dwCaps or DDSCAPS_COMPLEX or DDSCAPS_MIPMAP;
  251. header.SurfaceFormat.dwFlags := header.SurfaceFormat.dwFlags or DDSD_MIPMAPCOUNT;
  252. header.SurfaceFormat.dwMipMapCount := fLevelCount;
  253. end
  254. else
  255. header.SurfaceFormat.dwMipMapCount := 0;
  256. if fCubeMap then
  257. begin
  258. header.SurfaceFormat.dwCaps := header.SurfaceFormat.dwCaps or DDSCAPS_COMPLEX;
  259. header.SurfaceFormat.dwCaps2 := header.SurfaceFormat.dwCaps2 or DDSCAPS2_CUBEMAP or DDSCAPS2_CUBEMAP_POSITIVEX or
  260. DDSCAPS2_CUBEMAP_NEGATIVEX or DDSCAPS2_CUBEMAP_POSITIVEY or DDSCAPS2_CUBEMAP_NEGATIVEY or DDSCAPS2_CUBEMAP_POSITIVEZ or
  261. DDSCAPS2_CUBEMAP_NEGATIVEZ;
  262. end;
  263. if not GLEnumToDDSHeader(header, DX10header, false, fInternalFormat, fColorFormat, fDataType, fElementSize) then
  264. raise EInvalidRasterFile.Create('These image format do not match the DDS format specification.');
  265. stream.Write(header, Sizeof(TDDSHeader));
  266. // stream.Write(DX10header, Sizeof(TDDS_HEADER_DXT10));
  267. if fCubeMap or not vVerticalFlipDDS then
  268. begin
  269. stream.Write(fData[0], DataSize);
  270. Exit;
  271. end
  272. else
  273. begin
  274. GetMem(buffer, GetLevelSizeInByte(0));
  275. try
  276. for level := 0 to fLevelCount - 1 do
  277. begin
  278. size := GetLevelSizeInByte(level);
  279. Move(GetLevelAddress(level)^, buffer^, size);
  280. flipSurface(buffer, LevelWidth[level], LevelHeight[level], LevelDepth[level]);
  281. stream.Write(buffer^, size);
  282. end;
  283. finally
  284. FreeMem(buffer);
  285. end;
  286. end;
  287. end;
  288. procedure TgxDDSImage.AssignFromTexture(textureContext: TgxContext; const textureHandle: GLuint;
  289. textureTarget: TglTextureTarget; const CurrentFormat: Boolean; const intFormat: TglInternalFormat);
  290. var
  291. oldContext: TgxContext;
  292. contextActivate: Boolean;
  293. texFormat, texLod, optLod: Cardinal;
  294. level, faceCount, face: integer;
  295. residentFormat: TglInternalFormat;
  296. bCompressed: Boolean;
  297. vtcBuffer, top, bottom: PGLubyte;
  298. i, j, k: integer;
  299. cw, ch: integer;
  300. glTarget: GLEnum;
  301. function blockOffset(x, y, z: integer): integer;
  302. begin
  303. if z >= (FLOD[level].Depth and -4) then
  304. Result := fElementSize * (cw * ch * (FLOD[level].Depth and -4) + x + cw * (y + ch * (z - 4 * ch)))
  305. else
  306. Result := fElementSize * (4 * (x + cw * (y + ch * Floor(z / 4))) + (z and 3));
  307. if Result < 0 then
  308. Result := 0;
  309. end;
  310. begin
  311. oldContext := CurrentContext;
  312. contextActivate := (oldContext <> textureContext);
  313. if contextActivate then
  314. begin
  315. if Assigned(oldContext) then
  316. oldContext.Deactivate;
  317. textureContext.Activate;
  318. end;
  319. glTarget := DecodeTextureTarget(textureTarget);
  320. try
  321. textureContext.gxStates.TextureBinding[0, textureTarget] := textureHandle;
  322. fLevelCount := 0;
  323. glGetTexParameteriv(glTarget, GL_TEXTURE_MAX_LEVEL, @texLod);
  324. if glTarget = GL_TEXTURE_CUBE_MAP then
  325. begin
  326. fCubeMap := true;
  327. faceCount := 6;
  328. glTarget := GL_TEXTURE_CUBE_MAP_POSITIVE_X;
  329. end
  330. else
  331. begin
  332. fCubeMap := false;
  333. faceCount := 1;
  334. end;
  335. fTextureArray := (glTarget = GL_TEXTURE_1D_ARRAY) or (glTarget = GL_TEXTURE_2D_ARRAY) or
  336. (glTarget = GL_TEXTURE_CUBE_MAP_ARRAY);
  337. repeat
  338. // Check level existence
  339. glGetTexLevelParameteriv(glTarget, fLevelCount, GL_TEXTURE_INTERNAL_FORMAT, @texFormat);
  340. if texFormat = 1 then
  341. Break;
  342. Inc(fLevelCount);
  343. if fLevelCount = 1 then
  344. begin
  345. glGetTexLevelParameteriv(glTarget, 0, GL_TEXTURE_WIDTH, @FLOD[0].Width);
  346. glGetTexLevelParameteriv(glTarget, 0, GL_TEXTURE_HEIGHT, @FLOD[0].Height);
  347. FLOD[0].Depth := 0;
  348. if (glTarget = GL_TEXTURE_3D) or (glTarget = GL_TEXTURE_2D_ARRAY) or (glTarget = GL_TEXTURE_CUBE_MAP_ARRAY) then
  349. glGetTexLevelParameteriv(glTarget, 0, GL_TEXTURE_DEPTH, @FLOD[0].Depth);
  350. residentFormat := OpenGLFormatToInternalFormat(texFormat);
  351. if CurrentFormat then
  352. fInternalFormat := residentFormat
  353. else
  354. fInternalFormat := intFormat;
  355. if not FindDDSCompatibleDataFormat(fInternalFormat, fColorFormat, fDataType) then
  356. FindCompatibleDataFormat(fInternalFormat, fColorFormat, fDataType);
  357. // Get optimal number or MipMap levels
  358. optLod := GetImageLodNumber(FLOD[0].Width, FLOD[0].Height, FLOD[0].Depth, glTarget = GL_TEXTURE_3D);
  359. if texLod > optLod then
  360. texLod := optLod;
  361. // Check for MipMap posibility
  362. if ((fInternalFormat >= tfFLOAT_R16) and (fInternalFormat <= tfFLOAT_RGBA32)) then
  363. texLod := 1;
  364. end;
  365. until fLevelCount = integer(texLod);
  366. if fLevelCount > 0 then
  367. begin
  368. fElementSize := GetTextureElementSize(fColorFormat, fDataType);
  369. ReallocMem(fData, DataSize);
  370. bCompressed := IsCompressed;
  371. vtcBuffer := nil;
  372. for face := 0 to faceCount - 1 do
  373. begin
  374. if fCubeMap then
  375. glTarget := face + GL_TEXTURE_CUBE_MAP_POSITIVE_X;
  376. for level := 0 to fLevelCount - 1 do
  377. begin
  378. if bCompressed then
  379. begin
  380. if (FLOD[level].Depth > 0) and not fTextureArray then
  381. /// and GL_NV_texture_compression_vtc
  382. begin
  383. if level = 0 then
  384. GetMem(vtcBuffer, GetLevelSizeInByte(0));
  385. glGetCompressedTexImage(glTarget, level, vtcBuffer);
  386. // Shufle blocks from VTC to S3TC
  387. cw := (FLOD[level].Width + 3) div 4;
  388. ch := (FLOD[level].Height + 3) div 4;
  389. top := GetLevelAddress(level);
  390. for k := 0 to FLOD[level].Depth - 1 do
  391. for i := 0 to ch - 1 do
  392. for j := 0 to cw - 1 do
  393. begin
  394. bottom := vtcBuffer;
  395. Inc(bottom, blockOffset(j, i, k));
  396. Move(bottom^, top^, fElementSize);
  397. Inc(top, fElementSize);
  398. end;
  399. end
  400. else
  401. glGetCompressedTexImage(glTarget, level, GetLevelAddress(level));
  402. end
  403. else
  404. glGetTexImage(glTarget, level, fColorFormat, fDataType, GetLevelAddress(level));
  405. end; // for level
  406. end; // for face
  407. if Assigned(vtcBuffer) then
  408. FreeMem(vtcBuffer);
  409. // Check memory corruption
  410. ReallocMem(fData, DataSize);
  411. end;
  412. if fLevelCount < 1 then
  413. fLevelCount := 1;
  414. finally
  415. if contextActivate then
  416. begin
  417. textureContext.Deactivate;
  418. if Assigned(oldContext) then
  419. oldContext.Activate;
  420. end;
  421. end;
  422. end;
  423. procedure TgxDDSImage.flipSurface(chgData: PGLubyte; w, h, d: integer);
  424. var
  425. lineSize: integer;
  426. sliceSize: integer;
  427. tempBuf: PGLubyte;
  428. i, j: integer;
  429. top, bottom: PGLubyte;
  430. flipblocks: procedure(data: PGLubyte; size: integer);
  431. begin
  432. if d = 0 then
  433. d := 1;
  434. if not IsCompressed then
  435. begin
  436. lineSize := fElementSize * w;
  437. sliceSize := lineSize * h;
  438. GetMem(tempBuf, lineSize);
  439. for i := 0 to d - 1 do
  440. begin
  441. top := chgData;
  442. Inc(top, i * sliceSize);
  443. bottom := top;
  444. Inc(bottom, sliceSize - lineSize);
  445. for j := 0 to (h div 2) - 1 do
  446. begin
  447. Move(top^, tempBuf^, lineSize);
  448. Move(bottom^, top^, lineSize);
  449. Move(tempBuf^, bottom^, lineSize);
  450. Inc(top, lineSize);
  451. Dec(bottom, lineSize);
  452. end;
  453. end;
  454. FreeMem(tempBuf);
  455. end
  456. else
  457. begin
  458. w := (w + 3) div 4;
  459. h := (h + 3) div 4;
  460. case fColorFormat of
  461. GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
  462. flipblocks := flip_blocks_dxtc1;
  463. GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
  464. flipblocks := flip_blocks_dxtc3;
  465. GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
  466. flipblocks := flip_blocks_dxtc5;
  467. else
  468. Exit;
  469. end;
  470. lineSize := fElementSize * w;
  471. sliceSize := lineSize * h;
  472. GetMem(tempBuf, lineSize);
  473. for i := 0 to d - 1 do
  474. begin
  475. top := chgData;
  476. Inc(top, i * sliceSize);
  477. bottom := top;
  478. Inc(bottom, sliceSize - lineSize);
  479. for j := 0 to (h div 2) - 1 do
  480. begin
  481. if top = bottom then
  482. begin
  483. flipblocks(top, w);
  484. Break;
  485. end;
  486. flipblocks(top, w);
  487. flipblocks(bottom, w);
  488. Move(top^, tempBuf^, lineSize);
  489. Move(bottom^, top^, lineSize);
  490. Move(tempBuf^, bottom^, lineSize);
  491. Inc(top, lineSize);
  492. Dec(bottom, lineSize);
  493. end;
  494. end;
  495. FreeMem(tempBuf);
  496. end;
  497. end;
  498. class function TgxDDSImage.Capabilities: TDataFileCapabilities;
  499. begin
  500. Result := [dfcRead, dfcWrite];
  501. end;
  502. //---------------------------------------
  503. initialization
  504. //---------------------------------------
  505. RegisterRasterFormat('dds', 'Direct Draw Surface', TgxDDSImage);
  506. end.