Debian Codegraph @

src/base_rendering/castleglimages_miscellaneous.inc

raw
1 {%MainUnit castleglimages.pas}
2 {
3 Copyright 2001-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 { Part of CastleGLImages unit: miscellaneous stuff. }
18 {$ifdef read_interface}
19
20 {$ifdef read_interface_const}
21 const
22 PixelsImageClasses: array [0..8] of TEncodedImageClass = (
23 // 8-bit
24 TRGBImage,
25 TRGBAlphaImage,
26 TGrayscaleImage,
27 TGrayscaleAlphaImage,
28
29 // compressed
30 TGPUCompressedImage,
31
32 // floating-point
33 TGrayscaleFloatImage,
34 TGrayscaleAlphaFloatImage,
35 TRGBFloatImage,
36 TRGBAlphaFloatImage
37 );
38 {$endif read_interface_const}
39
40 {$ifdef read_interface_func}
41 { Return appropriate OpenGL format and type constants
42 for given TCastleImage descendant. If you will pass here Img
43 that is not a descendant of one of TextureImageClassesAll
44 or PixelsImageClasses, they will raise EImageClassNotSupportedForOpenGL.
45
46 ImageGLInternalFormat works with TGPUCompressedImage classes also, returning
47 appropriate enum, suitable for glCompressedTexImage2D.
48
49 @raises(EImageClassNotSupportedForOpenGL When Img class is not supported
50 by OpenGL.)
51
52 @groupBegin }
53 function ImageGLFormat(const Img: TCastleImage): TGLenum;
54 function ImageGLInternalFormat(const Img: TEncodedImage): TGLenum;
55 function ImageGLType(const Img: TCastleImage): TGLenum;
56 { @groupEnd }
57 {$endif read_interface_func}
58
59 {$ifdef read_interface_type}
60 //type - don't add type Delphi can't parse that correctly
61 TGLTextureId = TGLuint;
62
63 ETextureLoadError = class(Exception);
64 EImageClassNotSupportedForOpenGL = class(ETextureLoadError);
65 ECannotLoadCompressedTexture = class(ETextureLoadError);
66 EInvalidImageForOpenGLTexture = class(ETextureLoadError);
67 {$endif read_interface_type}
68
69 {$ifdef read_interface_func}
70 { If Tex <> 0 then it does glDeleteTextures on Tex and sets Tex to 0.
71 In other words, this is a simple wrapper over glDeleteTextures that
72 @orderedList(
73 @item checks if Tex really should be deleted
74 @item sets Tex to 0 to not free it once again
75 ) }
76 procedure glFreeTexture(var Tex: TGLTextureId);
77
78 { Call glTexParameterf to set GL_TEXTURE_MAX_ANISOTROPY_EXT on given texture
79 target.
80
81 Takes care to check for appropriate OpenGL extension (if not present,
82 does nothing), and to query OpenGL limit for Anisotropy (eventually
83 clamping provided Anisotropy down). }
84 procedure TexParameterMaxAnisotropy(const target: TGLenum; const Anisotropy: TGLfloat);
85 {$endif read_interface_func}
86
87 {$endif read_interface}
88
89 {$ifdef read_implementation}
90
91 { Missing from FPC OpenGL units. }
92 const
93 GL_TEXTURE_SWIZZLE_RGBA = $8E46;
94
95 { Get OpenGL enums for "format" and "internalFormat" for OpenGL routines like
96 https://registry.khronos.org/OpenGL-Refpages/gl4/html/glTexImage2D.xhtml
97 that load uncompressed images. }
98 procedure UncompressedGLFormatAndInternalFormat(const Img: TCastleImage;
99 out ImageFormat: TGLenum;
100 out ImageInternalFormat: TGLenum;
101 out ImageSwizzle: TVector4Integer);
102
103 { Missing from FPC OpenGL units. }
104 const
105 GL_R8 = $8229;
106 GL_RG = $8227;
107 GL_RG8 = $822B;
108
109 { For all image types, we have
110
111 - Modern enum, required by OpenGL "core" profile,
112 see https://www.khronos.org/opengl/wiki/Image_Format .
113 We can use it if GLFeatures.TextureModernFormatsAndSwizzle .
114
115 - Compatibility enum: name accepted by OpenGL ES 2 (
116 https://registry.khronos.org/OpenGL-Refpages/es2.0/xhtml/glTexImage2D.xml )
117 and latest OpenGL in compatibility mode.
118
119 - Moreover, when GLFeatures.EnableFixedFunction, we adjust to OpenGL 1.x where
120 the "internal format" for glTexImage2d was just a number,
121 like 2,3,4.
122
123 In OpenGLES >= 2 and in OpenGL >= 3.2 "core" profile this is not allowed
124 anymore.
125 }
126
127 begin
128 { ImageSwizzle passing depends on it. }
129 Assert(SizeOf(Integer) = SizeOf(TGLint));
130
131 if Img is TRGBImage then
132 begin
133 ImageSwizzle := Vector4Integer(GL_RED, GL_GREEN, GL_BLUE, GL_ONE);
134 if GLFeatures.TextureModernFormatsAndSwizzle then
135 begin
136 ImageFormat := GL_RGB;
137 ImageInternalFormat := GL_RGB8;
138 end else
139 begin
140 ImageFormat := GL_RGB;
141 if GLFeatures.EnableFixedFunction then
142 ImageInternalFormat := Img.ColorComponentsCount
143 else
144 ImageInternalFormat := ImageFormat;
145 end;
146 end else
147 if Img is TRGBAlphaImage then
148 begin
149 ImageSwizzle := Vector4Integer(GL_RED, GL_GREEN, GL_BLUE, GL_ALPHA);
150 if GLFeatures.TextureModernFormatsAndSwizzle then
151 begin
152 ImageFormat := GL_RGBA;
153 ImageInternalFormat := GL_RGBA8;
154 end else
155 begin
156 ImageFormat := GL_RGBA;
157 if GLFeatures.EnableFixedFunction then
158 ImageInternalFormat := Img.ColorComponentsCount
159 else
160 ImageInternalFormat := ImageFormat;
161 end;
162 end else
163 if Img is TGrayscaleImage then
164 begin
165 { Use texture swizzle to load texture 1st channel (red) -> as luminance (fill RGB) or alpha. }
166 if TGrayscaleImage(Img).TreatAsAlpha then
167 ImageSwizzle := Vector4Integer(GL_ONE, GL_ONE, GL_ONE, GL_RED)
168 else
169 ImageSwizzle := Vector4Integer(GL_RED, GL_RED, GL_RED, GL_ONE);
170 if GLFeatures.TextureModernFormatsAndSwizzle then
171 begin
172 ImageFormat := GL_RED;
173 ImageInternalFormat := GL_R8;
174 end else
175 begin
176 ImageFormat := Iff(TGrayscaleImage(Img).TreatAsAlpha, GL_ALPHA, GL_LUMINANCE);
177 if GLFeatures.EnableFixedFunction and (not TGrayscaleImage(Img).TreatAsAlpha) then
178 ImageInternalFormat := Img.ColorComponentsCount
179 else
180 ImageInternalFormat := ImageFormat;
181 end;
182 end else
183 if Img is TGrayscaleAlphaImage then
184 begin
185 { Use texture swizzle to load texture
186 - 1st channel (red) -> RBG
187 - 2nd channel (green) -> alpha }
188 ImageSwizzle := Vector4Integer(GL_RED, GL_RED, GL_RED, GL_GREEN);
189 if GLFeatures.TextureModernFormatsAndSwizzle then
190 begin
191 ImageFormat := GL_RG;
192 ImageInternalFormat := GL_RG8;
193 end else
194 begin
195 ImageFormat := GL_LUMINANCE_ALPHA;
196 if GLFeatures.EnableFixedFunction then
197 ImageInternalFormat := Img.ColorComponentsCount
198 else
199 ImageInternalFormat := ImageFormat;
200 end;
201 end else
202 if Img is TGrayscaleFloatImage then
203 begin
204 ImageSwizzle := Vector4Integer(GL_RED, GL_RED, GL_RED, GL_ONE);
205 if GLFeatures.TextureModernFormatsAndSwizzle then
206 begin
207 ImageFormat := GL_RED;
208 ImageInternalFormat := GL_R32F;
209 end else
210 begin
211 ImageFormat := GL_LUMINANCE;
212 if GLFeatures.EnableFixedFunction then
213 ImageInternalFormat := Img.ColorComponentsCount
214 else
215 ImageInternalFormat := ImageFormat;
216 end;
217 end else
218 if Img is TGrayscaleAlphaFloatImage then
219 begin
220 { Use texture swizzle to load texture
221 - 1st channel (red) -> RBG
222 - 2nd channel (green) -> alpha }
223 ImageSwizzle := Vector4Integer(GL_RED, GL_RED, GL_RED, GL_GREEN);
224 if GLFeatures.TextureModernFormatsAndSwizzle then
225 begin
226 ImageFormat := GL_RG;
227 ImageInternalFormat := GL_RG32F;
228 end else
229 begin
230 ImageFormat := GL_LUMINANCE_ALPHA;
231 if GLFeatures.EnableFixedFunction then
232 ImageInternalFormat := Img.ColorComponentsCount
233 else
234 ImageInternalFormat := ImageFormat;
235 end;
236 end else
237 if Img is TRGBFloatImage then
238 begin
239 ImageSwizzle := Vector4Integer(GL_RED, GL_GREEN, GL_BLUE, GL_ONE);
240 if GLFeatures.TextureModernFormatsAndSwizzle then
241 begin
242 ImageFormat := GL_RGB;
243 ImageInternalFormat := GL_RGB32F;
244 end else
245 begin
246 ImageFormat := GL_RGB;
247 if GLFeatures.EnableFixedFunction then
248 ImageInternalFormat := Img.ColorComponentsCount
249 else
250 ImageInternalFormat := ImageFormat;
251 end;
252 end else
253 if Img is TRGBAlphaFloatImage then
254 begin
255 ImageSwizzle := Vector4Integer(GL_RED, GL_GREEN, GL_BLUE, GL_ALPHA);
256 if GLFeatures.TextureModernFormatsAndSwizzle then
257 begin
258 ImageFormat := GL_RGBA;
259 ImageInternalFormat := GL_RGBA32F;
260 end else
261 begin
262 ImageFormat := GL_RGBA;
263 if GLFeatures.EnableFixedFunction then
264 ImageInternalFormat := Img.ColorComponentsCount
265 else
266 ImageInternalFormat := ImageFormat;
267 end;
268 end else
269 raise EImageClassNotSupportedForOpenGL.CreateFmt('Image class %s cannot be loaded to OpenGL', [Img.ClassName]);
270 end;
271
272 function ImageGLFormat(const Img: TCastleImage): TGLenum;
273 var
274 IgnoredImageInternalFormat: TGLenum;
275 IgnoredImageSwizzle: TVector4Integer;
276 begin
277 UncompressedGLFormatAndInternalFormat(Img, Result, IgnoredImageInternalFormat, IgnoredImageSwizzle);
278 end;
279
280 function ImageGLInternalFormat(const Img: TEncodedImage): TGLenum;
281 {$I gl_texture_compression_constants.inc}
282 var
283 IgnoredImageFormat: TGLenum;
284 IgnoredImageSwizzle: TVector4Integer;
285 begin
286 if Img is TCastleImage then
287 begin
288 UncompressedGLFormatAndInternalFormat(TCastleImage(Img), IgnoredImageFormat, Result, IgnoredImageSwizzle);
289 end else
290 if Img is TGPUCompressedImage then
291 begin
292 case TGPUCompressedImage(Img).Compression of
293 tcDxt1_RGB : Result := GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
294 tcDxt1_RGBA : Result := GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
295 tcDxt3 : Result := GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
296 tcDxt5 : Result := GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
297
298 tcPvrtc1_4bpp_RGB : Result := GL_COMPRESSED_RGB_PVRTC_4BPPV1_IMG;
299 tcPvrtc1_2bpp_RGB : Result := GL_COMPRESSED_RGB_PVRTC_2BPPV1_IMG;
300 tcPvrtc1_4bpp_RGBA: Result := GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG;
301 tcPvrtc1_2bpp_RGBA: Result := GL_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG;
302 tcPvrtc2_4bpp : Result := GL_COMPRESSED_RGBA_PVRTC_4BPPV2_IMG;
303 tcPvrtc2_2bpp : Result := GL_COMPRESSED_RGBA_PVRTC_2BPPV2_IMG;
304
305 tcATITC_RGB : Result := GL_ATC_RGB_AMD;
306 tcATITC_RGBA_InterpolatedAlpha: Result := GL_ATC_RGBA_INTERPOLATED_ALPHA_AMD;
307 tcATITC_RGBA_ExplicitAlpha : Result := GL_ATC_RGBA_EXPLICIT_ALPHA_AMD;
308
309 tcETC1 : Result := GL_ETC1_RGB8_OES;
310
311 tcASTC_4x4_RGBA : Result := GL_COMPRESSED_RGBA_ASTC_4x4_KHR;
312 tcASTC_5x4_RGBA : Result := GL_COMPRESSED_RGBA_ASTC_5x4_KHR;
313 tcASTC_5x5_RGBA : Result := GL_COMPRESSED_RGBA_ASTC_5x5_KHR;
314 tcASTC_6x5_RGBA : Result := GL_COMPRESSED_RGBA_ASTC_6x5_KHR;
315 tcASTC_6x6_RGBA : Result := GL_COMPRESSED_RGBA_ASTC_6x6_KHR;
316 tcASTC_8x5_RGBA : Result := GL_COMPRESSED_RGBA_ASTC_8x5_KHR;
317 tcASTC_8x6_RGBA : Result := GL_COMPRESSED_RGBA_ASTC_8x6_KHR;
318 tcASTC_8x8_RGBA : Result := GL_COMPRESSED_RGBA_ASTC_8x8_KHR;
319 tcASTC_10x5_RGBA : Result := GL_COMPRESSED_RGBA_ASTC_10x5_KHR;
320 tcASTC_10x6_RGBA : Result := GL_COMPRESSED_RGBA_ASTC_10x6_KHR;
321 tcASTC_10x8_RGBA : Result := GL_COMPRESSED_RGBA_ASTC_10x8_KHR;
322 tcASTC_10x10_RGBA : Result := GL_COMPRESSED_RGBA_ASTC_10x10_KHR;
323 tcASTC_12x10_RGBA : Result := GL_COMPRESSED_RGBA_ASTC_12x10_KHR;
324 tcASTC_12x12_RGBA : Result := GL_COMPRESSED_RGBA_ASTC_12x12_KHR;
325 tcASTC_4x4_SRGB8_ALPHA8 : Result := GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR;
326 tcASTC_5x4_SRGB8_ALPHA8 : Result := GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR;
327 tcASTC_5x5_SRGB8_ALPHA8 : Result := GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR;
328 tcASTC_6x5_SRGB8_ALPHA8 : Result := GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR;
329 tcASTC_6x6_SRGB8_ALPHA8 : Result := GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR;
330 tcASTC_8x5_SRGB8_ALPHA8 : Result := GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR;
331 tcASTC_8x6_SRGB8_ALPHA8 : Result := GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR;
332 tcASTC_8x8_SRGB8_ALPHA8 : Result := GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR;
333 tcASTC_10x5_SRGB8_ALPHA8 : Result := GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR;
334 tcASTC_10x6_SRGB8_ALPHA8 : Result := GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR;
335 tcASTC_10x8_SRGB8_ALPHA8 : Result := GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR;
336 tcASTC_10x10_SRGB8_ALPHA8 : Result := GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR;
337 tcASTC_12x10_SRGB8_ALPHA8 : Result := GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR;
338 tcASTC_12x12_SRGB8_ALPHA8 : Result := GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR;
339
340 {$ifndef COMPILER_CASE_ANALYSIS}
341 else raise EImageClassNotSupportedForOpenGL.Create('TGPUCompressedImage.Compression not supported by OpenGL');
342 {$endif}
343 end;
344 end else
345 raise EImageClassNotSupportedForOpenGL.CreateFmt('Image class %s cannot be loaded to OpenGL',
346 [Img.ClassName]);
347 end;
348
349 function ImageGLType(const Img: TCastleImage): TGLenum;
350 begin
351 if (Img is TRGBImage) or
352 (Img is TRGBAlphaImage) or
353 (Img is TGrayscaleImage) or
354 (Img is TGrayscaleAlphaImage) then
355 Result := GL_UNSIGNED_BYTE
356 else
357 if (Img is TGrayscaleFloatImage) or
358 (Img is TGrayscaleAlphaFloatImage) or
359 (Img is TRGBFloatImage) or
360 (Img is TRGBAlphaFloatImage) then
361 Result := GL_FLOAT
362 else
363 raise EImageClassNotSupportedForOpenGL.CreateFmt('Image class %s cannot be loaded to OpenGL',
364 [Img.ClassName]);
365 end;
366
367 procedure glFreeTexture(var Tex: TGLTextureId);
368 begin
369 if Tex <> 0 then
370 begin
371 TextureMemoryProfiler.Deallocate(Tex);
372 glDeleteTextures(1, @Tex);
373 Tex := 0;
374 end;
375 end;
376
377 { Internal utility in this unit, sets read buffer on desktop OpenGL. }
378 procedure SetReadBuffer(const Buffer: TGLEnum);
379 begin
380 {$ifndef OpenGLES}
381 glReadBuffer(Buffer);
382 {$endif}
383 end;
384
385 { Internal utility in this unit, sets draw buffer on desktop OpenGL. }
386 procedure SetDrawBuffer(const Buffer: TGLEnum);
387 begin
388 {$ifndef OpenGLES}
389 glDrawBuffer(Buffer);
390 {$endif}
391 end;
392
393 { Anisotropy ----------------------------------------------------------------- }
394
395 procedure TexParameterMaxAnisotropy(const target: TGLenum; const Anisotropy: TGLfloat);
396 begin
397 if GLFeatures.TextureFilterAnisotropic then
398 glTexParameterf(Target, GL_TEXTURE_MAX_ANISOTROPY_EXT,
399 Min(GLFeatures.MaxTextureMaxAnisotropy, Anisotropy));
400 end;
401
402 {$endif read_implementation}