Debian Codegraph @

src/images/castleimages_class_gpu_compressed.inc

raw
1 {
2 Copyright 2001-2024 Michalis Kamburelis.
3
4 This file is part of "Castle Game Engine".
5
6 "Castle Game Engine" is free software; see the file COPYING.txt,
7 included in this distribution, for details about the copyright.
8
9 "Castle Game Engine" is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 ----------------------------------------------------------------------------
14 }
15
16 {$ifdef read_interface}
17
18 { Image compressed using one of the GPU texture compression algorithms. }
19 TGPUCompressedImage = class(TEncodedImage)
20 private
21 FCompression: TTextureCompression;
22 FSize: Cardinal;
23 public
24 constructor Create(const AWidth, AHeight, ADepth: Cardinal;
25 const ACompression: TTextureCompression);
26
27 property Compression: TTextureCompression read FCompression;
28
29 { Size of the whole image data inside RawPixels, in bytes. }
30 function Size: Cardinal; override;
31
32 function HasAlpha: boolean; override;
33 function AlphaChannel(
34 const AlphaTolerance: Byte): TAlphaChannel; override;
35
36 { Flip compressed image vertically, losslessly.
37
38 This works only for S3TC images, and only when their height is 1, 2, 3
39 or a multiple of 4. Note that this is always satisfied if image height
40 is a power of two (as common for textures).
41 It uses the knowledge of how S3TC compression works
42 to losslessly flip the image, without re-compressing it.
43 The idea is described here
44 [http://users.telenet.be/tfautre/softdev/ddsload/explanation.htm]. }
45 procedure FlipVertical; override;
46
47 { Decompress the image.
48
49 This uses DecompressTexture routine.
50 By default, it is assigned only when OpenGL(ES) context is available
51 and can decompress textures with the help of OpenGL(ES).
52
53 @raises(ECannotDecompressTexture If we cannot decompress the texture,
54 because decompressor is not set or there was some other error
55 within decompressor.) }
56 function Decompress: TCastleImage;
57
58 { Create a new image object that has exactly the same class
59 and the same data (size, pixels) as this image. }
60 function MakeCopy: TGPUCompressedImage;
61
62 { Create a new image object that has exactly the same class
63 and the same data (size, pixels) as this image.
64
65 Equivalent to MakeCopy, but virtual and declared as returning TEncodedImage class. }
66 function CreateCopy: TEncodedImage; override;
67 end;
68
69 {$endif read_interface}
70
71 {$ifdef read_implementation}
72
73 { TGPUCompressedImage ----------------------------------------------------------------- }
74
75 constructor TGPUCompressedImage.Create(
76 const AWidth, AHeight, ADepth: Cardinal;
77 const ACompression: TTextureCompression);
78 begin
79 inherited Create;
80 FWidth := AWidth;
81 FHeight := AHeight;
82 FDepth := ADepth;
83 FCompression := ACompression;
84
85 case Compression of
86 { Size formula for S3TC textures:
87 All DXT* compression methods compress 4x4 pixels into some constant size.
88 When Width / Height is not divisible by 4, we have to round up.
89
90 This matches what MSDN docs say about DDS with mipmaps:
91 http://msdn.microsoft.com/en-us/library/bb205578(VS.85).aspx
92 When mipmaps are used, DDS Width/Height must be power-of-two,
93 so the base level is usually divisible by 4. But on the following mipmap
94 levels the size decreases, eventually to 1x1, so this still matters.
95 And MSDN says then explicitly that with DXT1, you have always
96 minimum 8 bytes, and with DXT2-5 minimum 16 bytes.
97
98 This also means that we cannot simply calculate size of mipmap in a DDS
99 by looking at size of base image, and dividing by 2/4/8 as mipmap size
100 decreases. We have to calculate size always rounding up to 4x4 block of pixels.
101 }
102 tcDxt1_RGB, tcDxt1_RGBA:
103 FSize := FDepth * DivRoundUp(FWidth, 4) * DivRoundUp(FHeight, 4) * 8 { 8 bytes for each 16 pixels };
104 tcDxt3, tcDxt5:
105 FSize := FDepth * DivRoundUp(FWidth, 4) * DivRoundUp(FHeight, 4) * 16 { 16 bytes for each 16 pixels };
106
107 { see https://www.khronos.org/registry/gles/extensions/IMG/IMG_texture_compression_pvrtc2.txt
108 for size formula size.
109 Note that minimum size is 32 bytes,
110 see https://developer.apple.com/library/ios/qa/qa1611/_index.html }
111 tcPvrtc1_2bpp_RGB, tcPvrtc1_2bpp_RGBA:
112 FSize := Max(32, (FDepth * Max(FWidth, 16) * Max(FHeight, 8) * 2 + 7) div 8);
113 tcPvrtc1_4bpp_RGB, tcPvrtc1_4bpp_RGBA:
114 FSize := Max(32, (FDepth * Max(FWidth, 8) * Max(FHeight, 8) * 4 + 7) div 8);
115
116 { see https://www.khronos.org/registry/gles/extensions/IMG/IMG_texture_compression_pvrtc2.txt
117 for size formula source }
118 tcPvrtc2_2bpp:
119 FSize := FDepth * DivRoundUp(FWidth, 8) * DivRoundUp(FHeight, 4) * 8;
120 tcPvrtc2_4bpp:
121 FSize := FDepth * DivRoundUp(FWidth, 4) * DivRoundUp(FHeight, 4) * 8;
122
123 { see https://www.khronos.org/registry/gles/extensions/AMD/AMD_compressed_ATC_texture.txt
124 for size formula source }
125 tcATITC_RGB:
126 FSize := FDepth * DivRoundUp(FWidth, 4) * DivRoundUp(FHeight, 4) * 8;
127 tcATITC_RGBA_ExplicitAlpha,
128 tcATITC_RGBA_InterpolatedAlpha:
129 FSize := FDepth * DivRoundUp(FWidth, 4) * DivRoundUp(FHeight, 4) * 16;
130
131 { size formula from
132 http://en.wikipedia.org/wiki/Ericsson_Texture_Compression
133 "ETC1 takes 4x4 groups of pixel data and compresses each into a single 64-bit word" }
134 tcETC1:
135 FSize := FDepth * DivRoundUp(FWidth, 4) * DivRoundUp(FHeight, 4) * 8;
136
137 { size formula from
138 https://www.khronos.org/registry/OpenGL/extensions/KHR/KHR_texture_compression_astc_hdr.txt }
139 tcASTC_4x4_RGBA, tcASTC_4x4_SRGB8_ALPHA8:
140 FSize := FDepth * DivRoundUp(FWidth, 4) * DivRoundUp(FHeight, 4) * 16;
141 tcASTC_5x4_RGBA, tcASTC_5x4_SRGB8_ALPHA8:
142 FSize := FDepth * DivRoundUp(FWidth, 5) * DivRoundUp(FHeight, 4) * 16;
143 tcASTC_5x5_RGBA, tcASTC_5x5_SRGB8_ALPHA8:
144 FSize := FDepth * DivRoundUp(FWidth, 5) * DivRoundUp(FHeight, 5) * 16;
145 tcASTC_6x5_RGBA, tcASTC_6x5_SRGB8_ALPHA8:
146 FSize := FDepth * DivRoundUp(FWidth, 6) * DivRoundUp(FHeight, 5) * 16;
147 tcASTC_6x6_RGBA, tcASTC_6x6_SRGB8_ALPHA8:
148 FSize := FDepth * DivRoundUp(FWidth, 6) * DivRoundUp(FHeight, 6) * 16;
149 tcASTC_8x5_RGBA, tcASTC_8x5_SRGB8_ALPHA8:
150 FSize := FDepth * DivRoundUp(FWidth, 8) * DivRoundUp(FHeight, 5) * 16;
151 tcASTC_8x6_RGBA, tcASTC_8x6_SRGB8_ALPHA8:
152 FSize := FDepth * DivRoundUp(FWidth, 8) * DivRoundUp(FHeight, 6) * 16;
153 tcASTC_8x8_RGBA, tcASTC_8x8_SRGB8_ALPHA8:
154 FSize := FDepth * DivRoundUp(FWidth, 8) * DivRoundUp(FHeight, 8) * 16;
155 tcASTC_10x5_RGBA, tcASTC_10x5_SRGB8_ALPHA8:
156 FSize := FDepth * DivRoundUp(FWidth, 10) * DivRoundUp(FHeight, 5) * 16;
157 tcASTC_10x6_RGBA, tcASTC_10x6_SRGB8_ALPHA8:
158 FSize := FDepth * DivRoundUp(FWidth, 10) * DivRoundUp(FHeight, 6) * 16;
159 tcASTC_10x8_RGBA, tcASTC_10x8_SRGB8_ALPHA8:
160 FSize := FDepth * DivRoundUp(FWidth, 10) * DivRoundUp(FHeight, 8) * 16;
161 tcASTC_10x10_RGBA, tcASTC_10x10_SRGB8_ALPHA8:
162 FSize := FDepth * DivRoundUp(FWidth, 10) * DivRoundUp(FHeight, 10) * 16;
163 tcASTC_12x10_RGBA, tcASTC_12x10_SRGB8_ALPHA8:
164 FSize := FDepth * DivRoundUp(FWidth, 12) * DivRoundUp(FHeight, 10) * 16;
165 tcASTC_12x12_RGBA, tcASTC_12x12_SRGB8_ALPHA8:
166 FSize := FDepth * DivRoundUp(FWidth, 12) * DivRoundUp(FHeight, 12) * 16;
167
168 {$ifndef COMPILER_CASE_ANALYSIS}
169 else raise EInvalidDDS.CreateFmt('Cannot calculate size for texture compressed with %s',
170 [TextureCompressionInfo[Compression].Name]);
171 {$endif}
172 end;
173
174 FRawPixels := GetMem(FSize);
175 end;
176
177 function TGPUCompressedImage.Size: Cardinal;
178 begin
179 Result := FSize;
180 end;
181
182 function TGPUCompressedImage.HasAlpha: boolean;
183 begin
184 Result := TextureCompressionInfo[Compression].AlphaChannel <> acNone;
185 end;
186
187 function TGPUCompressedImage.AlphaChannel(
188 const AlphaTolerance: Byte): TAlphaChannel;
189 begin
190 { Compressed data doesn't analyze alpha channel, instead
191 we determine alpha channel from the compression type. }
192 Result := TextureCompressionInfo[Compression].AlphaChannel;
193 end;
194
195 {$I castleimages_s3tc_flip_vertical.inc}
196
197 function TGPUCompressedImage.Decompress: TCastleImage;
198 begin
199 WritelnLog('Decompressing GPU-compressed "%s", this is usually a waste of time for normal games that should load textures in format (compressed or not) suitable for current GPU', [
200 Url
201 ]);
202 if Assigned(DecompressTexture) then
203 Result := DecompressTexture(Self)
204 else
205 raise ECannotDecompressTexture.Create('Cannot decompress GPU-compressed texture: no decompressor initialized');
206 end;
207
208 function TGPUCompressedImage.MakeCopy: TGPUCompressedImage;
209 begin
210 Result := CreateCopy as TGPUCompressedImage;
211 end;
212
213 function TGPUCompressedImage.CreateCopy: TEncodedImage;
214 begin
215 Result := TGPUCompressedImage.Create(Width, Height, Depth, Compression);
216 Assert(Result.Size = Size);
217 Move(RawPixels^, Result.RawPixels^, Size);
218 Result.Url := Url;
219 end;
220
221 {$endif read_implementation}