Debian Codegraph @

src/images/castleinternalcompositeimage_ktx.inc

raw
1 {%MainUnit castleinternalcompositeimage.pas}
2 {
3 Copyright 2009-2024 Michalis Kamburelis.
4
5 This file is part of "Castle Game Engine".
6
7 "Castle Game Engine" is free software; see the file COPYING.txt,
8 included in this distribution, for details about the copyright.
9
10 "Castle Game Engine" is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
14 ----------------------------------------------------------------------------
15 }
16
17 { KTX loading. }
18
19 { TKTXHandler ----------------------------------------------------------------- }
20
21 type
22 TKTXHandler = class(TCompositeFormatHandler)
23 strict private
24 const
25 // See https://www.khronos.org/opengles/sdk/tools/KTX/file_format_spec/
26 CorrectFileId: array [0..11] of Byte =
27 ($AB, $4B, $54, $58, $20, $31, $31, $BB, $0D, $0A, $1A, $0A);
28
29 type
30 TKTXHeader = record
31 FileId: array [0..High(CorrectFileId)] of Byte;
32 Endianness,
33 GlType,
34 GlTypeSize,
35 GlFormat,
36 GlInternalFormat,
37 GlBaseInternalFormat,
38 Width,
39 Height,
40 Depth,
41 NumberOfArrayElements,
42 NumberOfFaces,
43 NumberOfMipmapLevels,
44 BytesOfKeyValueData: UInt32;
45 end;
46 const
47 // See /usr/include/GL/gl.h , /usr/include/GL/glext.h
48 //GL_BYTE = $1400;
49 GL_UNSIGNED_BYTE = $1401;
50 //GL_SHORT = $1402;
51 GL_UNSIGNED_SHORT = $1403;
52 //GL_INT = $1404;
53 //GL_UNSIGNED_INT = $1405;
54 GL_FLOAT = $1406;
55 //GL_DOUBLE = $140A;
56 GL_HALF_FLOAT = $140B;
57
58 GL_RED = $1903;
59 //GL_GREEN = $1904;
60 //GL_BLUE = $1905;
61 //GL_ALPHA = $1906;
62 GL_RGB = $1907;
63 GL_RGBA = $1908;
64 GL_LUMINANCE = $1909;
65 //GL_LUMINANCE_ALPHA = $190A;
66
67 GL_LUMINANCE8_EXT = $8040;
68 GL_BGR = $80E0;
69 GL_BGRA = $80E1;
70
71 GL_RGB8 = $8051;
72 GL_SRGB8 = $8C41;
73 GL_R16 = $822A;
74 GL_RG8 = $822B;
75 GL_RG = $8227;
76 //GL_R16F = $822D;
77 GL_R32F = $822E;
78 GL_RG32F = $8230;
79 GL_RGB32F = $8815;
80 GL_RGBA32F = $8814;
81 GL_R16UI = $8234;
82 GL_SRGB8_ALPHA8 = $8C43;
83
84 GL_RED_INTEGER = $8D94;
85
86 var
87 Header: TKTXHeader;
88 SwapEndianness: boolean;
89 RowsTopToBottom, SlicesFrontToBack: boolean;
90
91 procedure ReadKeyValues(const Stream: TStream);
92 procedure ReadOrientation(const Value: String);
93
94 { The KTX specification says
95 "Uncompressed texture data matches a GL_UNPACK_ALIGNMENT of 4." }
96 function RowBytePadding(const Image: TCastleImage): Integer;
97
98 procedure ReadOneImage(const Stream: TStream; const W, H, D: Cardinal);
99
100 { Optionally invert Y, depending on RowsTopToBottom.
101 H should be height of the current image. }
102 function TargetY(const Y: Integer; const H: Cardinal): Integer;
103
104 { Optionally invert Z, depending on SlicesFrontToBack.
105 D should be depth of the current image. }
106 function TargetZ(const Z: Integer; const D: Cardinal): Integer;
107
108 procedure ReadG(const Stream: TStream; const W, H, D: Cardinal);
109 procedure ReadG16(const Stream: TStream; const W, H, D: Cardinal);
110 procedure ReadGA(const Stream: TStream; const W, H, D: Cardinal);
111 procedure ReadRGB(const Stream: TStream; const W, H, D: Cardinal);
112 procedure ReadBGR(const Stream: TStream; const W, H, D: Cardinal);
113 procedure ReadRGBA(const Stream: TStream; const W, H, D: Cardinal);
114 procedure ReadBGRA(const Stream: TStream; const W, H, D: Cardinal);
115 procedure ReadFloatImage(const ImageClass: TCastleImageClass;
116 const Stream: TStream; const W, H, D: Cardinal);
117 procedure ReadCompressed(const Stream: TStream; const Compression: TTextureCompression; const W, H, D: Cardinal);
118 public
119 procedure LoadFromStream(const Stream: TStream; const Url: String); override;
120 end;
121
122 function TKTXHandler.TargetY(const Y: Integer; const H: Cardinal): Integer;
123 begin
124 if RowsTopToBottom then
125 Result := H - Y - 1
126 else
127 Result := Y;
128 end;
129
130 function TKTXHandler.TargetZ(const Z: Integer; const D: Cardinal): Integer;
131 begin
132 if SlicesFrontToBack then
133 Result := D - Z - 1
134 else
135 Result := Z;
136 end;
137
138 procedure TKTXHandler.ReadG(const Stream: TStream; const W, H, D: Cardinal);
139 var
140 Y, Z: Integer;
141 Row: PByteArray;
142 Image: TGrayscaleImage;
143 Padding: Integer;
144 begin
145 Image := TGrayscaleImage.Create(W, H, D);
146 Images.Add(Image);
147
148 Padding := RowBytePadding(Image);
149
150 for Z := 0 to D - 1 do
151 for Y := 0 to H - 1 do
152 begin
153 Row := Image.RowPtr(TargetY(Y, H), TargetZ(Z, D));
154 Stream.ReadBuffer(Row^, Image.PixelSize * W);
155 if Padding <> 0 then
156 Stream.Seek(Padding, soFromCurrent);
157 end;
158 end;
159
160 procedure TKTXHandler.ReadG16(const Stream: TStream; const W, H, D: Cardinal);
161 var
162 RowData: TWordList;
163 TargetRow: PByteArray;
164 X, Y, Z: Integer;
165 Image: TGrayscaleImage;
166 Padding: Integer;
167 begin
168 Image := TGrayscaleImage.Create(W, H, D);
169 Images.Add(Image);
170
171 Padding := RowBytePadding(Image);
172
173 RowData := TWordList.Create;
174 try
175 RowData.Count := W;
176
177 for Z := 0 to D - 1 do
178 for Y := 0 to H - 1 do
179 begin
180 Stream.ReadBuffer(RowData.L^, W * SizeOf(Word));
181 { convert row of Words to Bytes }
182 TargetRow := Image.RowPtr(TargetY(Y, H), TargetZ(Z, D));
183 for X := 0 to W - 1 do
184 TargetRow^[X] := RowData.L[X] shr 8;
185 if Padding <> 0 then
186 Stream.Seek(Padding, soFromCurrent);
187 end;
188 finally FreeAndNil(RowData) end;
189 end;
190
191 procedure TKTXHandler.ReadGA(const Stream: TStream; const W, H, D: Cardinal);
192 var
193 Y, Z: Integer;
194 Row: PVector2ByteArray;
195 Image: TGrayscaleAlphaImage;
196 Padding: Integer;
197 begin
198 Image := TGrayscaleAlphaImage.Create(W, H, D);
199 Images.Add(Image);
200
201 Padding := RowBytePadding(Image);
202
203 for Z := 0 to D - 1 do
204 for Y := 0 to H - 1 do
205 begin
206 Row := Image.RowPtr(TargetY(Y, H), TargetZ(Z, D));
207 Stream.ReadBuffer(Row^, Image.PixelSize * W);
208 if Padding <> 0 then
209 Stream.Seek(Padding, soFromCurrent);
210 end;
211 end;
212
213 procedure TKTXHandler.ReadRGB(const Stream: TStream; const W, H, D: Cardinal);
214 var
215 Y, Z: Integer;
216 Row: PVector3ByteArray;
217 Image: TRGBImage;
218 Padding: Integer;
219 begin
220 Image := TRGBImage.Create(W, H, D);
221 Images.Add(Image);
222
223 Padding := RowBytePadding(Image);
224
225 for Z := 0 to D - 1 do
226 for Y := 0 to H - 1 do
227 begin
228 Row := Image.RowPtr(TargetY(Y, H), TargetZ(Z, D));
229 Stream.ReadBuffer(Row^, Image.PixelSize * W);
230 if Padding <> 0 then
231 Stream.Seek(Padding, soFromCurrent);
232 end;
233 end;
234
235 procedure TKTXHandler.ReadBGR(const Stream: TStream; const W, H, D: Cardinal);
236 var
237 X, Y, Z: Integer;
238 Row: PVector3Byte;
239 Image: TRGBImage;
240 Padding: Integer;
241 begin
242 Image := TRGBImage.Create(W, H, D);
243 Images.Add(Image);
244
245 Padding := RowBytePadding(Image);
246
247 for Z := 0 to D - 1 do
248 for Y := 0 to H - 1 do
249 begin
250 Row := PVector3Byte(Image.RowPtr(TargetY(Y, H), TargetZ(Z, D)));
251 Stream.ReadBuffer(Row^, Image.PixelSize * W);
252 { Now invert red and blue. }
253 for X := 0 to W - 1 do
254 begin
255 SwapValues(Row^.Z, Row^.X);
256 Inc(Row);
257 end;
258 if Padding <> 0 then
259 Stream.Seek(Padding, soFromCurrent);
260 end;
261 end;
262
263 procedure TKTXHandler.ReadRGBA(const Stream: TStream; const W, H, D: Cardinal);
264 var
265 Y, Z: Integer;
266 Row: PVector4ByteArray;
267 Image: TRGBAlphaImage;
268 begin
269 Image := TRGBAlphaImage.Create(W, H, D);
270 // when Image.PixelSize is 4, there is never any padding
271 Assert(RowBytePadding(Image) = 0);
272 Images.Add(Image);
273
274 for Z := 0 to D - 1 do
275 for Y := 0 to H - 1 do
276 begin
277 Row := Image.RowPtr(TargetY(Y, H), TargetZ(Z, D));
278 Stream.ReadBuffer(Row^, Image.PixelSize * W);
279 end;
280 end;
281
282 procedure TKTXHandler.ReadBGRA(const Stream: TStream; const W, H, D: Cardinal);
283 var
284 X, Y, Z: Integer;
285 Row: PVector4Byte;
286 Image: TRGBAlphaImage;
287 begin
288 Image := TRGBAlphaImage.Create(W, H, D);
289 // when Image.PixelSize is 4, there is never any padding
290 Assert(RowBytePadding(Image) = 0);
291 Images.Add(Image);
292
293 for Z := 0 to D - 1 do
294 for Y := 0 to H - 1 do
295 begin
296 Row := PVector4Byte(Image.RowPtr(TargetY(Y, H), TargetZ(Z, D)));
297 Stream.ReadBuffer(Row^, Image.PixelSize * W);
298 { Now invert red and blue. }
299 for X := 0 to W - 1 do
300 begin
301 SwapValues(Row^.Z, Row^.X);
302 Inc(Row);
303 end;
304 end;
305 end;
306
307 procedure TKTXHandler.ReadFloatImage(const ImageClass: TCastleImageClass;
308 const Stream: TStream; const W, H, D: Cardinal);
309 var
310 Y, Z: Integer;
311 Image: TCastleImage;
312 ImageRow: Pointer;
313 begin
314 Image := ImageClass.Create(W, H, D);
315 // Image.PixelSize is always multiple of SizeOf(Single)
316 Assert(Image.PixelSize mod 4 = 0);
317 // when Image.PixelSize is divisible by 4, there is never any padding
318 Assert(RowBytePadding(Image) = 0);
319 Images.Add(Image);
320
321 for Z := 0 to D - 1 do
322 for Y := 0 to H - 1 do
323 begin
324 ImageRow := Image.RowPtr(TargetY(Y, H), TargetZ(Z, D));
325 Stream.ReadBuffer(ImageRow^, Image.PixelSize * W);
326 end;
327 end;
328
329 procedure TKTXHandler.ReadOrientation(const Value: String);
330 begin
331 if (Value = 'S=r,T=d') or
332 (Value = 'S=r,T=d,R=o') then
333 begin
334 RowsTopToBottom := true;
335 SlicesFrontToBack := false;
336 end else
337 if (Value = 'S=r,T=u') or
338 (Value = 'S=r,T=u,R=o') then
339 begin
340 RowsTopToBottom := false;
341 SlicesFrontToBack := false;
342 end else
343 { This orientation is not adviced by KTX specification,
344 but is used by PowerVR texture tools if you do "vertical flip" at encoding. }
345 if Value = 'S=r,T=u,R=i' then
346 begin
347 RowsTopToBottom := false;
348 SlicesFrontToBack := true;
349 end else
350 if Value = 'S=r,T=d,R=i' then
351 begin
352 RowsTopToBottom := true;
353 SlicesFrontToBack := true;
354 end else
355 WritelnWarning('KTX', 'Unrecognized KTX orientation "%s"', [Value]);
356
357 WritelnLog('KTX', 'Got KTX orientation "%s". RowsTopToBottom: %s, SlicesFrontToBack: %s.',
358 [Value,
359 BoolToStr(RowsTopToBottom, true),
360 BoolToStr(SlicesFrontToBack, true)
361 ]);
362 end;
363
364 procedure TKTXHandler.ReadKeyValues(const Stream: TStream);
365 var
366 RemainingToRead, KeyAndValueByteSize, ValuePadding: UInt32;
367 KeyAndValue: array of AnsiChar;
368 Key, Value: AnsiString;
369 begin
370 RemainingToRead := Header.BytesOfKeyValueData;
371 while RemainingToRead > 0 do
372 begin
373 Stream.ReadBuffer(KeyAndValueByteSize, SizeOf(KeyAndValueByteSize));
374 if SwapEndianness then
375 KeyAndValueByteSize := Swap32(KeyAndValueByteSize);
376 if KeyAndValueByteSize > 0 then
377 begin
378 SetLength(KeyAndValue, KeyAndValueByteSize);
379 Stream.ReadBuffer(KeyAndValue[0], KeyAndValueByteSize);
380 ValuePadding := 3 - ((KeyAndValueByteSize + 3) mod 4);
381 if ValuePadding <> 0 then
382 Stream.Seek(ValuePadding, soFromCurrent);
383
384 Key := PAnsiChar(@(KeyAndValue[0]));
385 SetLength(Value, KeyAndValueByteSize - Length(Key) - 1);
386 if Length(Value) > 0 then
387 begin
388 // check is Value NULL-terminated
389 if KeyAndValue[KeyAndValueByteSize - 1] = #0 then
390 SetLength(Value, Length(Value) - 1);
391 Move(KeyAndValue[Length(Key) + 1], Value[1], Length(Value));
392 end;
393
394 { Compare ignoring case, as specification recommends using
395 'KTXorientation' (as samples in https://github.com/KhronosGroup/KTX
396 follow it), but PowerVR texture tools write 'KTXOrientation' }
397 if SameText(Key, 'KTXorientation') then
398 ReadOrientation(Value);
399 end else
400 ValuePadding := 0;
401
402 RemainingToRead := RemainingToRead -
403 (SizeOf(KeyAndValueByteSize) + KeyAndValueByteSize + ValuePadding);
404 end;
405 end;
406
407 procedure TKTXHandler.ReadCompressed(const Stream: TStream; const Compression: TTextureCompression; const W, H, D: Cardinal);
408 var
409 Image: TGPUCompressedImage;
410 begin
411 Image := TGPUCompressedImage.Create(W, H, D, Compression);
412 Images.Add(Image);
413
414 Stream.ReadBuffer(Image.RawPixels^, Image.Size);
415
416 if RowsTopToBottom then
417 Image.FlipVertical;
418 end;
419
420 function TKTXHandler.RowBytePadding(const Image: TCastleImage): Integer;
421 begin
422 Result := Image.PixelSize * Image.Width mod 4;
423 if Result <> 0 then
424 Result := 4 - Result;
425 end;
426
427 procedure TKTXHandler.ReadOneImage(const Stream: TStream; const W, H, D: Cardinal);
428 {$I gl_texture_compression_constants.inc}
429 var
430 Compression: TTextureCompression;
431 begin
432 if (Header.GlType = GL_UNSIGNED_BYTE) and
433 (Header.GlTypeSize = 1) and
434 (Header.GlFormat = GL_LUMINANCE) and
435 (Header.GlInternalFormat = GL_LUMINANCE8_EXT) and
436 (Header.GlBaseInternalFormat = GL_LUMINANCE) then
437 begin
438 ReadG(Stream, W, H, D);
439 end else
440 if (
441 { You can generate this with
442 - PowerVR Texture Tool GUI with "Encode" to R16
443 - PowerVR Texture Tool CLI with "-f r16,US" encoding
444 }
445 (Header.GlType = GL_UNSIGNED_SHORT) and
446 (Header.GlTypeSize = 2) and
447 (Header.GlFormat = GL_RED_INTEGER) and
448 (Header.GlInternalFormat = GL_R16UI) and
449 (Header.GlBaseInternalFormat = GL_RED_INTEGER)
450 ) or (
451 { You can generate this with
452 - PowerVR Texture Tool CLI with "-f r16,USN" encoding
453 }
454 (Header.GlType = GL_UNSIGNED_SHORT) and
455 (Header.GlTypeSize = 2) and
456 (Header.GlFormat = GL_RED) and
457 (Header.GlInternalFormat = GL_R16) and
458 (Header.GlBaseInternalFormat = GL_RED)
459 ) then
460 begin
461 ReadG16(Stream, W, H, D);
462 end else
463 if (Header.GlType = GL_UNSIGNED_BYTE) and
464 (Header.GlTypeSize = 1) and
465 (Header.GlFormat = GL_RG) and
466 (Header.GlInternalFormat = GL_RG8) and
467 (Header.GlBaseInternalFormat = GL_RG) then
468 begin
469 ReadGA(Stream, W, H, D);
470 end else
471 if (Header.GlType = GL_UNSIGNED_BYTE) and
472 (Header.GlTypeSize = 1) and
473 (Header.GlFormat = GL_BGRA) and
474 (Header.GlInternalFormat = GL_BGRA) and
475 (Header.GlBaseInternalFormat = GL_BGRA) then
476 begin
477 ReadBGRA(Stream, W, H, D);
478 end else
479 if (Header.GlType = GL_UNSIGNED_BYTE) and
480 (Header.GlTypeSize = 1) and
481 (Header.GlFormat = GL_RGBA) and
482 (
483 (Header.GlInternalFormat = GL_RGBA) or
484 { Testcase: rgba-reference.ktx from https://github.com/KhronosGroup/KTX-Software/tree/master/tests/testimages
485 Note that we ignore sRGB here for now, we read it just like regular image.
486 In CGE we don't have a control per-image for sRGB, yet. }
487 (Header.GlInternalFormat = GL_SRGB8_ALPHA8)
488 ) and
489 (Header.GlBaseInternalFormat = GL_RGBA) then
490 begin
491 ReadRGBA(Stream, W, H, D);
492 end else
493 if (Header.GlType = GL_UNSIGNED_BYTE) and
494 (Header.GlTypeSize = 1) and
495 (Header.GlFormat = GL_BGR) and
496 (Header.GlInternalFormat = GL_BGR) and
497 (Header.GlBaseInternalFormat = GL_BGR) then
498 begin
499 ReadBGR(Stream, W, H, D);
500 end else
501 if (Header.GlType = GL_UNSIGNED_BYTE) and
502 (Header.GlTypeSize = 1) and
503 (Header.GlFormat = GL_RGB) and
504 ( (Header.GlInternalFormat = GL_RGB) or
505 (Header.GlInternalFormat = GL_RGB8) or
506 { TODO: for now SRGB8 is read the same way,
507 we ignore information that it is in sRGB color-space.
508 Whether the image is assumed to be in sRGB or linear color-space
509 depends in CGE on the image function (whether it is placed in
510 e.g. TPhysicalMaterial.BaseTexture or NormalTexture)
511 and global @link(ColorSpace) variable. }
512 (Header.GlInternalFormat = GL_SRGB8) ) and
513 (Header.GlBaseInternalFormat = GL_RGB) then
514 begin
515 ReadRGB(Stream, W, H, D);
516 end else
517 if (Header.GlType = GL_FLOAT) and
518 (Header.GlTypeSize = SizeOf(Single)) and
519 (Header.GlFormat = GL_RED) and
520 (Header.GlInternalFormat = GL_R32F) and
521 (Header.GlBaseInternalFormat = GL_RED) then
522 begin
523 ReadFloatImage(TGrayscaleFloatImage, Stream, W, H, D);
524 end else
525 if (Header.GlType = GL_FLOAT) and
526 (Header.GlTypeSize = SizeOf(Single)) and
527 (Header.GlFormat = GL_RG) and
528 (Header.GlInternalFormat = GL_RG32F) and
529 (Header.GlBaseInternalFormat = GL_RG) then
530 begin
531 ReadFloatImage(TGrayscaleAlphaFloatImage, Stream, W, H, D);
532 end else
533 if (Header.GlType = GL_FLOAT) and
534 (Header.GlTypeSize = SizeOf(Single)) and
535 (Header.GlFormat = GL_RGB) and
536 (Header.GlInternalFormat = GL_RGB32F) and
537 (Header.GlBaseInternalFormat = GL_RGB) then
538 begin
539 ReadFloatImage(TRGBFloatImage, Stream, W, H, D);
540 end else
541 if (Header.GlType = GL_FLOAT) and
542 (Header.GlTypeSize = SizeOf(Single)) and
543 (Header.GlFormat = GL_RGBA) and
544 (Header.GlInternalFormat = GL_RGBA32F) and
545 (Header.GlBaseInternalFormat = GL_RGBA) then
546 begin
547 ReadFloatImage(TRGBAlphaFloatImage, Stream, W, H, D);
548 end else
549 if Header.GlType = GL_HALF_FLOAT then
550 begin
551 raise EInvalidKTX.Create('Cannot handle KTX with half-float (16-bit floats) data');
552 end else
553 if (Header.GlType = 0) and
554 { Header.GlTypeSize is commonly 1,
555 but Header.GlTypeSize = 0 is used by Apple, see https://github.com/castle-engine/castle-image-viewer/issues/1 .
556 Spec doesn't clarify it for compressed textures ( http://github.khronos.org/KTX-Specification/ )
557 so just ignore checking GlTypeSize below. }
558 // (Header.GlTypeSize = 1) and
559 (Header.GlFormat = 0) and
560 ((Header.GlBaseInternalFormat = GL_RGBA) or (Header.GlBaseInternalFormat = GL_RGB)) then // GL_RGB is needed by ETC1
561 begin
562 case Header.GlInternalFormat of
563 GL_COMPRESSED_RGB_S3TC_DXT1_EXT : Compression := tcDxt1_RGB;
564 GL_COMPRESSED_RGBA_S3TC_DXT1_EXT : Compression := tcDxt1_RGBA;
565 GL_COMPRESSED_RGBA_S3TC_DXT3_EXT : Compression := tcDxt3;
566 GL_COMPRESSED_RGBA_S3TC_DXT5_EXT : Compression := tcDxt5;
567
568 GL_COMPRESSED_RGB_PVRTC_4BPPV1_IMG : Compression := tcPvrtc1_4bpp_RGB;
569 GL_COMPRESSED_RGB_PVRTC_2BPPV1_IMG : Compression := tcPvrtc1_2bpp_RGB;
570 GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG : Compression := tcPvrtc1_4bpp_RGBA;
571 GL_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG : Compression := tcPvrtc1_2bpp_RGBA;
572 GL_COMPRESSED_RGBA_PVRTC_4BPPV2_IMG : Compression := tcPvrtc2_4bpp;
573 GL_COMPRESSED_RGBA_PVRTC_2BPPV2_IMG : Compression := tcPvrtc2_2bpp;
574
575 GL_ATC_RGB_AMD : Compression := tcATITC_RGB;
576 GL_ATC_RGBA_INTERPOLATED_ALPHA_AMD : Compression := tcATITC_RGBA_InterpolatedAlpha;
577 GL_ATC_RGBA_EXPLICIT_ALPHA_AMD : Compression := tcATITC_RGBA_ExplicitAlpha;
578
579 GL_ETC1_RGB8_OES : Compression := tcETC1;
580
581 GL_COMPRESSED_RGBA_ASTC_4x4_KHR : Compression := tcASTC_4x4_RGBA;
582 GL_COMPRESSED_RGBA_ASTC_5x4_KHR : Compression := tcASTC_5x4_RGBA;
583 GL_COMPRESSED_RGBA_ASTC_5x5_KHR : Compression := tcASTC_5x5_RGBA;
584 GL_COMPRESSED_RGBA_ASTC_6x5_KHR : Compression := tcASTC_6x5_RGBA;
585 GL_COMPRESSED_RGBA_ASTC_6x6_KHR : Compression := tcASTC_6x6_RGBA;
586 GL_COMPRESSED_RGBA_ASTC_8x5_KHR : Compression := tcASTC_8x5_RGBA;
587 GL_COMPRESSED_RGBA_ASTC_8x6_KHR : Compression := tcASTC_8x6_RGBA;
588 GL_COMPRESSED_RGBA_ASTC_8x8_KHR : Compression := tcASTC_8x8_RGBA;
589 GL_COMPRESSED_RGBA_ASTC_10x5_KHR : Compression := tcASTC_10x5_RGBA;
590 GL_COMPRESSED_RGBA_ASTC_10x6_KHR : Compression := tcASTC_10x6_RGBA;
591 GL_COMPRESSED_RGBA_ASTC_10x8_KHR : Compression := tcASTC_10x8_RGBA;
592 GL_COMPRESSED_RGBA_ASTC_10x10_KHR : Compression := tcASTC_10x10_RGBA;
593 GL_COMPRESSED_RGBA_ASTC_12x10_KHR : Compression := tcASTC_12x10_RGBA;
594 GL_COMPRESSED_RGBA_ASTC_12x12_KHR : Compression := tcASTC_12x12_RGBA;
595
596 GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR : Compression := tcASTC_4x4_SRGB8_ALPHA8;
597 GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR : Compression := tcASTC_5x4_SRGB8_ALPHA8;
598 GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR : Compression := tcASTC_5x5_SRGB8_ALPHA8;
599 GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR : Compression := tcASTC_6x5_SRGB8_ALPHA8;
600 GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR : Compression := tcASTC_6x6_SRGB8_ALPHA8;
601 GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR : Compression := tcASTC_8x5_SRGB8_ALPHA8;
602 GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR : Compression := tcASTC_8x6_SRGB8_ALPHA8;
603 GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR : Compression := tcASTC_8x8_SRGB8_ALPHA8;
604 GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR : Compression := tcASTC_10x5_SRGB8_ALPHA8;
605 GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR : Compression := tcASTC_10x6_SRGB8_ALPHA8;
606 GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR : Compression := tcASTC_10x8_SRGB8_ALPHA8;
607 GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR: Compression := tcASTC_10x10_SRGB8_ALPHA8;
608 GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR: Compression := tcASTC_12x10_SRGB8_ALPHA8;
609 GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR: Compression := tcASTC_12x12_SRGB8_ALPHA8;
610
611 else raise EInvalidKTX.CreateFmt('GPU compression 0x%.4x not supported',
612 [Header.GlInternalFormat]);
613 end;
614
615 ReadCompressed(Stream, Compression, W, H, D);
616 end else
617 begin
618 if (Header.GlType = 0) and (Header.GlFormat = 0) then
619 begin
620 raise EInvalidKTX.CreateFmt('Cannot read data from this KTX (compressed) format.' + NL +
621 'Details:' + NL +
622 ' glTypeSize %d' + NL +
623 ' glInternalFormat 0x%.4x' + NL +
624 ' glBaseInternalFormat 0x%.4x', [
625 Header.GlTypeSize,
626 Header.GlInternalFormat,
627 Header.GlBaseInternalFormat
628 ]);
629 end else
630 begin
631 raise EInvalidKTX.CreateFmt('Cannot read data from this KTX (uncompressed) format.' + NL +
632 'Details:' + NL +
633 ' glType 0x%.4x' + NL +
634 ' glTypeSize %d' + NL +
635 ' glFormat 0x%.4x' + NL +
636 ' glInternalFormat 0x%.4x' + NL +
637 ' glBaseInternalFormat 0x%.4x', [
638 Header.GlType,
639 Header.GlTypeSize,
640 Header.GlFormat,
641 Header.GlInternalFormat,
642 Header.GlBaseInternalFormat
643 ]);
644 end;
645 end;
646 end;
647
648 procedure TKTXHandler.LoadFromStream(const Stream: TStream; const Url: String);
649
650 procedure ReadMipmapLevel(const W, H, D: Cardinal);
651 var
652 I: Integer;
653 ImageSize: UInt32;
654 begin
655 Stream.ReadBuffer(ImageSize, SizeOf(ImageSize));
656 if SwapEndianness then
657 ImageSize := Swap32(ImageSize);
658
659 ReadOneImage(Stream, W, H, D);
660
661 { TODO: For now, we don't support cubemaps.
662 But we can read multiple images in case of NumberOfArrayElements > 1. }
663 if (Header.NumberOfFaces <= 1) and
664 (Header.NumberOfArrayElements > 1) then
665 begin
666 for I := 1 to Header.NumberOfArrayElements - 1 do
667 ReadOneImage(Stream, W, H, D);
668 end;
669
670 { Note: Ignoring mipPadding from KTX spec
671 https://www.khronos.org/registry/KTX/specs/1.0/ktxspec_v1.html
672 as it should be 0 in practice. }
673 end;
674
675 var
676 I: Integer;
677 W, H, D: Cardinal;
678 begin
679 try
680 Stream.ReadBuffer(Header, SizeOf(Header));
681
682 for I := 0 to High(CorrectFileId) do
683 if Header.FileId[I] <> CorrectFileId[I] then
684 raise EInvalidKTX.Create('Invalid KTX file header');
685
686 SwapEndianness := Header.Endianness = $01020304;
687 if SwapEndianness then
688 begin
689 Header.Endianness := Swap32(Header.Endianness );
690 Header.GlType := Swap32(Header.GlType );
691 Header.GlTypeSize := Swap32(Header.GlTypeSize );
692 Header.GlFormat := Swap32(Header.GlFormat );
693 Header.GlInternalFormat := Swap32(Header.GlInternalFormat );
694 Header.GlBaseInternalFormat := Swap32(Header.GlBaseInternalFormat );
695 Header.Width := Swap32(Header.Width );
696 Header.Height := Swap32(Header.Height );
697 Header.Depth := Swap32(Header.Depth );
698 Header.NumberOfArrayElements := Swap32(Header.NumberOfArrayElements);
699 Header.NumberOfFaces := Swap32(Header.NumberOfFaces );
700 Header.NumberOfMipmapLevels := Swap32(Header.NumberOfMipmapLevels );
701 Header.BytesOfKeyValueData := Swap32(Header.BytesOfKeyValueData );
702 end;
703
704 if Header.Endianness <> $04030201 then
705 raise EInvalidKTX.Create('Invalid KTX endianess');
706
707 Width := Header.Width;
708 // by using Max(1, ...) for Height we should support 1D textures too
709 Height := Max(1, Header.Height);
710 Depth := Max(1, Header.Depth);
711
712 RowsTopToBottom := false;
713 SlicesFrontToBack := false;
714
715 // Skip BytesOfKeyValueData
716 //Stream.Seek(Header.BytesOfKeyValueData, soFromCurrent);
717
718 ReadKeyValues(Stream);
719
720 { interpret mipmap information }
721 if Header.NumberOfMipmapLevels = 0 then
722 begin
723 WritelnWarning('TODO: Not supported KTX numberOfMipmapLevels = 0 (indicates we should generate mipmaps at KTX loading)');
724 Header.NumberOfMipmapLevels := 1;
725 end;
726 Composite.FMipmaps := Header.NumberOfMipmapLevels > 1;
727 Composite.FMipmapsCount := Header.NumberOfMipmapLevels;
728 { Check that Width/Height are power of two, this is needed to make
729 sure images reading code is sensible (otherwise, there's no way
730 to tell what are the sizes of following images). }
731 if Composite.FMipmaps then
732 begin
733 if (not IsPowerOf2(Width)) or
734 (not IsPowerOf2(Height)) then
735 begin
736 WritelnWarning('KTX image has mipmaps, but width or height are not a power of 2: %d x %d. We will ignore these mipmaps, since we cannot determine their size', [Width, Height]);
737 Composite.FMipmaps := false;
738 end;
739 end;
740 //WritelnLog('KTX', 'Mipmap levels: %d', [Composite.FMipmapsCount]);
741
742 W := Width;
743 H := Height;
744 D := Depth;
745 for I := 0 to Composite.FMipmapsCount - 1 do
746 begin
747 ReadMipmapLevel(W, H, D);
748 W := Max(1, W div 2);
749 H := Max(1, H div 2);
750 D := Max(1, D div 2);
751 end;
752 except
753 { EReadError is raised by Stream.ReadBuffer when it can't read
754 specified number of bytes }
755 on E: EReadError do raise EInvalidKTX.Create('Read error: ' + E.Message);
756 on E: ECheckFailed do raise EInvalidKTX.Create('Invalid KTX file: ' + E.Message);
757 end;
758 end;