Debian Codegraph @

src/base_rendering/castleglimages_load_3d.inc

raw
1 {%MainUnit castleglimages.pas}
2 {
3 Copyright 2001-2023 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: 3D textures sizing and loading. }
18
19 {$ifdef read_interface}
20
21 {$ifdef read_interface_func}
22 { Comfortably load a 3D texture.
23 Think about this as doing glTexImage3D(...) for you.
24 It also sets texture minification, magnification filters and creates
25 mipmaps if necessary.
26
27 It checks OpenGL 3D texture size requirements, and throws exceptions
28 if not satisfied.
29
30 It takes care about OpenGL unpack parameters. Just don't worry about it.
31
32 If Filter uses mipmaps, then all mipmap levels will be loaded.
33
34 @orderedList(
35 @item(
36 As a first try, if CompositeForMipmaps is non-nil
37 and has mipmaps (CompositeForMipmaps.Mipmaps), we will load these mipmaps.
38 CompositeForMipmaps must be a 3D texture (CompositeType = ctVolume).)
39
40 @item(Otherwise, we'll generate mipmaps.
41
42 GenerateMipmap functionality will be required for this.
43 When it is not available on this OpenGL implementation,
44 we will change minification filter to simple linear and make WritelnWarning.
45 So usually you just don't have to worry about this.)
46 )
47
48 Pass TextureIdForProfiler only for profiling purposes (for TextureMemoryProfiler).
49 This procedure assumes that the texture is already bound.
50
51 @raises(ETextureLoadError If texture cannot be loaded for whatever reason,
52 for example it's size is not correct for OpenGL 3D texture (we cannot
53 automatically resize 3D textures, at least for now).
54 Or it's compressed (although we support here TEncodedImage,
55 OpenGL doesn't have any 3D texture compression available.))
56
57 @raises(EImageClassNotSupportedForOpenGL When Image class is not supported
58 by OpenGL.)
59 }
60 procedure glTextureImage3D(const TextureIdForProfiler: TGLTextureId;
61 const Image: TEncodedImage;
62 Filter: TTextureFilter; CompositeForMipmaps: TCompositeImage);
63 {$endif read_interface_func}
64
65 {$endif read_interface}
66
67 {$ifdef read_implementation}
68
69 function IsTexture3DSized(const Size: Cardinal): boolean; overload;
70 begin
71 Result :=
72 (not GLFeatures.Texture3D) or
73 (
74 IsPowerOf2(Size) and
75 (Size > 0) and
76 (Size <= GLFeatures.MaxTexture3DSize)
77 );
78 end;
79
80 function IsTexture3DSized(const R: TCastleImage): boolean; overload;
81 begin
82 if GLFeatures.Texture3D then
83 begin
84 Result :=
85 IsPowerOf2(R.Width ) and (R.Width > 0) and (R.Width <= GLFeatures.MaxTexture3DSize) and
86 IsPowerOf2(R.Height) and (R.Height > 0) and (R.Height <= GLFeatures.MaxTexture3DSize) and
87 IsPowerOf2(R.Depth ) and (R.Depth > 0) and (R.Depth <= GLFeatures.MaxTexture3DSize);
88 end else
89 Result := true;
90 end;
91
92 procedure glTextureImage3D(const TextureIdForProfiler: TGLTextureId;
93 const Image: TEncodedImage;
94 Filter: TTextureFilter; CompositeForMipmaps: TCompositeImage);
95 var
96 ImageInternalFormat: TGLuint;
97 ImageFormat: TGLuint;
98 LoadedSize: Int64;
99
100 { Calls glTexImage3D for given image.
101 Takes care of OpenGL unpacking (alignment etc.).
102 Takes care of Image size --- makes sure that image has the right size
103 (power of 2, within OpenGL required sizes). }
104 procedure glTexImage3DImage(Image: TCastleImage; Level: TGLuint);
105
106 { This is like glTexImage3DImage, but it doesn't take care
107 of Image size. }
108 procedure Core(Image: TCastleImage);
109 begin
110 BeforeUnpackImage(Image);
111 try
112 if GLFeatures.Texture3D then
113 glTexImage3D(GL_TEXTURE_3D, Level, ImageInternalFormat,
114 Image.Width, Image.Height, Image.Depth, 0, ImageFormat, ImageGLType(Image),
115 Image.RawPixels);
116 LoadedSize := LoadedSize + Image.Size;
117 finally AfterUnpackImage(Image) end;
118 end;
119
120 begin
121 if not IsTexture3DSized(Image) then
122 raise ETextureLoadError.CreateFmt('Image is not properly sized for a 3D texture, sizes must be a power-of-two and <= GL_MAX_3D_TEXTURE_SIZE (%d). Sizes are: %d x %d x %d',
123 [ GLFeatures.MaxTexture3DSize, Image.Width, Image.Height, Image.Depth ]);
124
125 Core(Image);
126 end;
127
128 { Check should we load mipmaps from Composite. Load them, if yes. }
129 function LoadMipmapsFromComposite(Composite: TCompositeImage): boolean;
130 var
131 I: Integer;
132 begin
133 Result := (Composite <> nil) and Composite.Mipmaps;
134 if Result and (Composite.CompositeType <> ctVolume) then
135 begin
136 WritelnWarning('Texture', 'Composite image contains mipmaps, but not for 3D (volume) texture');
137 Result := false;
138 end;
139
140 if Result and (not GLFeatures.TextureMaxLevel) then
141 begin
142 WritelnWarning('Texture', 'Cannot load Composite image containing mipmaps, because OpenGL(ES) does not support GL_TEXTURE_MAX_LEVEL');
143 Result := false;
144 end;
145
146 if Result then
147 begin
148 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAX_LEVEL, Composite.MipmapsCount - 1);
149 for I := 1 to Composite.MipmapsCount - 1 do
150 if Composite.Images[I] is TCastleImage then
151 glTexImage3DImage(TCastleImage(Composite.Images[I]), I) else
152 raise ETextureLoadError.CreateFmt('Image class %s cannot be loaded to OpenGL 3D texture. OpenGL doesn''t allow any 3D texture compression formats',
153 [Image.ClassName]);
154 end;
155 end;
156
157 var
158 ImageSwizzle: TVector4Integer;
159 begin
160 if not (Image is TCastleImage) then
161 raise ETextureLoadError.CreateFmt('Image class %s cannot be loaded to OpenGL 3D texture. OpenGL doesn''t allow any 3D texture compression formats',
162 [Image.ClassName]);
163
164 UncompressedGLFormatAndInternalFormat(TCastleImage(Image), ImageFormat, ImageInternalFormat, ImageSwizzle);
165 LoadedSize := 0;
166
167 glTexImage3DImage(TCastleImage(Image), 0);
168
169 if not GLFeatures.Texture3D then
170 raise Exception.Create('3D textures not supported by OpenGL(ES)');
171
172 if GLFeatures.TextureModernFormatsAndSwizzle then
173 glTexParameteriv(GL_TEXTURE_3D, GL_TEXTURE_SWIZZLE_RGBA, @ImageSwizzle);
174
175 if Filter.NeedsMipmaps then
176 begin
177 if not LoadMipmapsFromComposite(CompositeForMipmaps) then
178 try
179 GenerateMipmap(GL_TEXTURE_3D);
180 LoadedSize := LoadedSize + TTextureMemoryProfiler.MipmapsSize(Image);
181 except
182 on E: EGenerateMipmapNotAvailable do
183 begin
184 Filter.Minification := minLinear;
185 WritelnWarning('Texture', 'Creating mipmaps for 3D textures requires GenerateMipmap functionality, will fallback to GL_LINEAR minification: ' + E.Message);
186 end;
187 end;
188 end;
189
190 TextureMemoryProfiler.Allocate(TextureIdForProfiler, Image.URL, Image.ClassName,
191 LoadedSize, Filter.NeedsMipmaps, Image.Width, Image.Height, Image.Depth);
192
193 SetTextureFilter(GL_TEXTURE_3D, Filter);
194 end;
195
196 {$endif read_implementation}