Debian Codegraph @

src/images/castleimages_astc.inc

raw
1 {%MainUnit castleimages.pas}
2 {
3 Copyright 2021-2021 Yevhen Loza, 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 type
18 { ASTC compressed image header
19 adapted from https://arm-software.github.io/vulkan-sdk/_a_s_t_c.html }
20 {TAstcHeader = packed record
21 Magic: array[1..4] of Byte;
22 BlockdimX: Byte;
23 BlockdimY: Byte;
24 BlockdimZ: Byte;
25 XSize: array[1..3] of Byte;
26 YSize: array[1..3] of Byte;
27 ZSize: array[1..3] of Byte;
28 end;}
29 TAstcInfo = packed record
30 BlockdimX: Byte;
31 BlockdimY: Byte;
32 BlockdimZ: Byte;
33 XSize: UInt32;
34 YSize: UInt32;
35 ZSize: UInt32;
36 end;
37
38 function LoadASTC(const Stream: TStream;
39 const AllowedImageClasses: array of TEncodedImageClass): TEncodedImage;
40 const
41 MagicAstc : UInt32 = $5CA1AB13;
42 var
43 Magic: UInt32;
44 Word24bit: array[0..2] of Byte;
45 Info: TAstcInfo;
46 Res: TGPUCompressedImage absolute Result;
47
48 function BlockSizeToTextureCompression: TTextureCompression;
49 var
50 BlockSizeString: String;
51 begin
52 BlockSizeString := Info.BlockdimX.ToString + 'x' + Info.BlockdimY.ToString + 'x' + Info.BlockdimZ.ToString;
53
54 if BlockSizeString = '4x4x1' then
55 Result := tcASTC_4x4_RGBA
56 else
57 if BlockSizeString = '5x4x1'then
58 Result := tcASTC_5x4_RGBA
59 else
60 if BlockSizeString = '5x5x1'then
61 Result := tcASTC_5x5_RGBA
62 else
63 if BlockSizeString = '6x5x1'then
64 Result := tcASTC_6x5_RGBA
65 else
66 if BlockSizeString = '6x6x1'then
67 Result := tcASTC_6x6_RGBA
68 else
69 if BlockSizeString = '8x5x1'then
70 Result := tcASTC_8x5_RGBA
71 else
72 if BlockSizeString = '8x6x1'then
73 Result := tcASTC_8x6_RGBA
74 else
75 if BlockSizeString = '8x8x1'then
76 Result := tcASTC_8x8_RGBA
77 else
78 if BlockSizeString = '10x5x1'then
79 Result := tcASTC_10x5_RGBA
80 else
81 if BlockSizeString = '10x6x1'then
82 Result := tcASTC_10x6_RGBA
83 else
84 if BlockSizeString = '10x8x1'then
85 Result := tcASTC_10x8_RGBA
86 else
87 if BlockSizeString = '10x10x1'then
88 Result := tcASTC_10x10_RGBA
89 else
90 if BlockSizeString = '12x10x1'then
91 Result := tcASTC_12x10_RGBA
92 else
93 if BlockSizeString = '12x12x1'then
94 Result := tcASTC_12x12_RGBA
95 else
96 raise EInvalidASTC.Create('Unexpected block size in ASTC: ' + BlockSizeString);
97
98 { TODO: There doesn't seem to be any way to tell which color space
99 was encoded inside the ASTC file.
100 So we never set Result to
101
102 tcASTC_4x4_SRGB8_ALPHA8,
103 tcASTC_5x4_SRGB8_ALPHA8,
104 tcASTC_5x5_SRGB8_ALPHA8,
105 tcASTC_6x5_SRGB8_ALPHA8,
106 tcASTC_6x6_SRGB8_ALPHA8,
107 tcASTC_8x5_SRGB8_ALPHA8,
108 tcASTC_8x6_SRGB8_ALPHA8,
109 tcASTC_8x8_SRGB8_ALPHA8,
110 tcASTC_10x5_SRGB8_ALPHA8,
111 tcASTC_10x6_SRGB8_ALPHA8,
112 tcASTC_10x8_SRGB8_ALPHA8,
113 tcASTC_10x10_SRGB8_ALPHA8,
114 tcASTC_12x10_SRGB8_ALPHA8,
115 tcASTC_12x12_SRGB8_ALPHA8
116
117 But for now, we also don't use SRGB texture GPU types in CGE,
118 we do gamma correction to pow in GLSL instead, for max portability. }
119 end;
120
121 begin
122 try
123 Stream.ReadLE(Magic);
124 if Magic <> MagicAstc then
125 raise Exception.Create('Invalid ASTC header: $' + IntToHex(Magic, 8) + ' expected: $' + IntToHex(MagicAstc, 8));
126
127 Stream.ReadBuffer(Info.BlockdimX, 1);
128 Stream.ReadBuffer(Info.BlockdimY, 1);
129 Stream.ReadBuffer(Info.BlockdimZ, 1);
130
131 Stream.ReadBuffer(Word24bit, 3);
132 Info.XSize := Word24Bit[0] + Word24Bit[1] shl 8 + Word24Bit[2] shl 16;
133 Stream.ReadBuffer(Word24bit, 3);
134 Info.YSize := Word24Bit[0] + Word24Bit[1] shl 8 + Word24Bit[2] shl 16;
135 Stream.ReadBuffer(Word24bit, 3);
136 Info.ZSize := Word24Bit[0] + Word24Bit[1] shl 8 + Word24Bit[2] shl 16;
137
138 Result := TGPUCompressedImage.Create(Info.XSize, Info.YSize, Info.ZSize,
139 BlockSizeToTextureCompression);
140 try
141 Stream.ReadBuffer(Res.RawPixels^, Res.Size);
142 //Res.FlipVertical; Not implemented
143 { on unhandled error, make sure to free result }
144 except
145 FreeAndNil(Result);
146 raise;
147 end;
148 except
149 { EReadError is thrown by Stream.ReadBuffer when it can't read
150 specified number of bytes }
151 on E: EReadError do raise EInvalidASTC.Create('Read error: ' + E.Message);
152 on E: ECheckFailed do raise EInvalidASTC.Create('Wrong ASTC: ' + E.Message);
153 end;
154 end;