| 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: cubemaps sizing and loading. } |
| 18 |
|
| 19 |
{$ifdef read_interface} |
| 20 |
|
| 21 |
{$ifdef read_interface_func} |
| 22 |
function IsCubeMapTextureSized(const Size: Cardinal): boolean; overload; |
| 23 |
function ResizeToCubeMapTextureSize(const Size: Cardinal): Cardinal; overload; |
| 24 |
|
| 25 |
{ Comfortably load all six cube map texture images. |
| 26 |
Think about this as doing glTexImage2D(Side, ...) for each cube side. |
| 27 |
It takes care of (almost?) everything you need to prepare OpenGL cube map |
| 28 |
texture. |
| 29 |
|
| 30 |
It automatically takes care to adjust the texture size to |
| 31 |
appropriate size, honoring the "power of two" requirement and |
| 32 |
the GL_MAX_CUBE_MAP_TEXTURE_SIZE_ARB limit of OpenGL. So texture image |
| 33 |
may be resized (preferably up) internally before loading. |
| 34 |
Although, if texture is compressed, we cannot resize it |
| 35 |
--- so @link(ECannotLoadCompressedTexture) will be raised if texture is not appropriate |
| 36 |
size. |
| 37 |
|
| 38 |
It takes care about OpenGL unpack parameters. Just don't worry about it. |
| 39 |
|
| 40 |
Pass TextureIdForProfiler only for profiling purposes (for TextureMemoryProfiler). |
| 41 |
This procedure assumes that the texture is already bound. |
| 42 |
|
| 43 |
If mipmaps are requested: |
| 44 |
|
| 45 |
@orderedList( |
| 46 |
@item(First of all, if CompositeForMipmaps is non-nil and has mipmaps defined, |
| 47 |
we will load them from this CompositeForMipmaps image. |
| 48 |
CompositeForMipmaps must have CompositeType = ctCubeMap.) |
| 49 |
|
| 50 |
@item(Otherwise, we'll try to generate images using OpenGL GenerateMipmap.) |
| 51 |
|
| 52 |
@item(As a last resort, if GenerateMipmap is not available, |
| 53 |
we will fallback to generating mipmaps on CPU.) |
| 54 |
) |
| 55 |
|
| 56 |
@raises(ETextureLoadError If texture cannot be loaded for whatever reason. |
| 57 |
This includes @link(ECannotLoadCompressedTexture) if the compressed |
| 58 |
texture cannot be |
| 59 |
loaded for whatever reason (not availble appropriate extension, |
| 60 |
not correct texture size, mipmaps requested and |
| 61 |
CompositeForMipmaps/glGenerateMipmap not available). |
| 62 |
This includes EInvalidImageForOpenGLTexture if Image class is invalid |
| 63 |
for an OpenGL texture.) |
| 64 |
|
| 65 |
@raises(EImageClassNotSupportedForOpenGL When Image class is not supported |
| 66 |
by OpenGL.) |
| 67 |
} |
| 68 |
procedure glTextureCubeMap(const TextureIdForProfiler: TGLTextureId; |
| 69 |
const PositiveX, NegativeX, |
| 70 |
PositiveY, NegativeY, |
| 71 |
PositiveZ, NegativeZ: TEncodedImage; |
| 72 |
const CompositeForMipmaps: TCompositeImage; |
| 73 |
const Mipmaps: boolean; |
| 74 |
out DisableMipmaps: Boolean); |
| 75 |
{$endif read_interface_func} |
| 76 |
|
| 77 |
{$endif read_interface} |
| 78 |
|
| 79 |
{$ifdef read_implementation} |
| 80 |
|
| 81 |
{ ---------------------------------------------------------------------------- |
| 82 |
Adjusting image size for cube map texture. } |
| 83 |
|
| 84 |
function IsCubeMapTextureSized(const Size: Cardinal): boolean; overload; |
| 85 |
begin |
| 86 |
Result := |
| 87 |
(not GLFeatures.TextureCubeMap) or |
| 88 |
( |
| 89 |
IsPowerOf2(Size) and |
| 90 |
(Size > 0) and |
| 91 |
(Size <= GLFeatures.MaxCubeMapTextureSize) |
| 92 |
); |
| 93 |
end; |
| 94 |
|
| 95 |
function IsCubeMapTextureSized(const R: TEncodedImage): boolean; overload; |
| 96 |
begin |
| 97 |
Result := |
| 98 |
(not GLFeatures.TextureCubeMap) or |
| 99 |
( |
| 100 |
(r.Width = r.Height) { must be square } and |
| 101 |
IsPowerOf2(r.Width) and |
| 102 |
(r.Width > 0) and |
| 103 |
(r.Width <= GLFeatures.MaxCubeMapTextureSize) |
| 104 |
); |
| 105 |
end; |
| 106 |
|
| 107 |
function ResizeToCubeMapTextureSize(const r: TCastleImage): TCastleImage; overload; forward; |
| 108 |
|
| 109 |
procedure ResizeForCubeMapTextureSize(var r: TCastleImage); |
| 110 |
var |
| 111 |
newR: TCastleImage; |
| 112 |
begin |
| 113 |
if not IsCubeMapTextureSized(r) then |
| 114 |
begin |
| 115 |
newR := ResizeToCubeMapTextureSize(r); |
| 116 |
FreeAndNil(r); |
| 117 |
r := newR; |
| 118 |
end; |
| 119 |
end; |
| 120 |
|
| 121 |
function ResizeToCubeMapTextureSize(const Size: Cardinal): Cardinal; overload; |
| 122 |
begin |
| 123 |
Result := Size; |
| 124 |
if GLFeatures.TextureCubeMap then |
| 125 |
begin |
| 126 |
if Size <= 0 then |
| 127 |
Result := 1 |
| 128 |
else |
| 129 |
if Size > GLFeatures.MaxCubeMapTextureSize then |
| 130 |
Result := GLFeatures.MaxCubeMapTextureSize |
| 131 |
else |
| 132 |
if IsPowerOf2(Size) then |
| 133 |
Result := Size |
| 134 |
else |
| 135 |
{ Result jakie otrzymamy below jest na pewno < MaxTexSize bo |
| 136 |
skoro Size <= MaxTexSize i not IsPowerOf2(Size) to Size < MaxTexSize |
| 137 |
a MaxTexSize samo jest potega dwojki. } |
| 138 |
Result := 1 shl (Biggest2Exponent(Size) + 1); |
| 139 |
end; |
| 140 |
end; |
| 141 |
|
| 142 |
function ResizeToCubeMapTextureSize(const r: TCastleImage): TCastleImage; overload; |
| 143 |
var |
| 144 |
Size: Cardinal; |
| 145 |
begin |
| 146 |
if GLFeatures.TextureCubeMap then |
| 147 |
begin |
| 148 |
Size := Max(r.Width, r.Height); |
| 149 |
Size := ResizeToCubeMapTextureSize(Size); |
| 150 |
|
| 151 |
WritelnLog('Texture loading', Format('Resizing image for cube map texture from (%d, %d) to (%d, %d)', |
| 152 |
[R.Width, R.Height, Size, Size])); |
| 153 |
|
| 154 |
result := r.MakeResized(Size, Size, riBilinear); |
| 155 |
end else |
| 156 |
result := r.MakeCopy; |
| 157 |
end; |
| 158 |
|
| 159 |
{ Cube map texture loading --------------------------------------------------- } |
| 160 |
|
| 161 |
{ Comfortably load a single image for one cube map texture side. |
| 162 |
|
| 163 |
This is pretty much like glTexImages2DForCubeMap, |
| 164 |
except it operates only on one side of the cube. |
| 165 |
Target should be one of the six cube map texture targets: |
| 166 |
GL_TEXTURE_CUBE_MAP_POSITIVE/NEGATIVE_X/Y/Z_ARB. |
| 167 |
|
| 168 |
Also, this cannot load mipmaps from Composite or use GenerateMipmap |
| 169 |
(GenerateMipmap call must be done for whole cube map texture target). |
| 170 |
So this can create mipmaps only by CastleBuild2DMipmaps. It will also |
| 171 |
fail with @link(ECannotLoadCompressedTexture) if mipmaps will be requested --- |
| 172 |
we cannot generate mipmaps for compressed textures here. If you want to use |
| 173 |
more modern GenerateMipmap, you should use higher-level |
| 174 |
glTexImages2DForCubeMap (takes all six images), or pass Mipmaps = @false |
| 175 |
and do it yourself. |
| 176 |
|
| 177 |
Level must be 0, unless you provide mipmaps. |
| 178 |
|
| 179 |
WasCompressed will be set to @true if this is compressed |
| 180 |
(otherwise we leave it unchanged). |
| 181 |
|
| 182 |
@raises(ETextureLoadError If texture cannot be loaded for whatever reason. |
| 183 |
This includes @link(ECannotLoadCompressedTexture) if the compressed texture cannot be |
| 184 |
loaded for whatever reason (not availble appropriate extension, |
| 185 |
not correct texture size, mipmaps requested). |
| 186 |
This includes EInvalidImageForOpenGLTexture if Image class is invalid |
| 187 |
for an OpenGL texture.) } |
| 188 |
procedure glTextureCubeMapSide( |
| 189 |
const Target: TGLenum; const Image: TEncodedImage; const Level: TGLuint; const Mipmaps: boolean; |
| 190 |
var LoadedSize: Int64; var WasCompressed: Boolean); |
| 191 |
|
| 192 |
{ Load Image through glCompressedTexImage2D[ARB]. |
| 193 |
This checks existence of OpenGL extensions for compressed texture, |
| 194 |
and checks Image sizes. |
| 195 |
It also takes care of pixel packing, although actually nothing needs |
| 196 |
be done about it when using compressed textures. |
| 197 |
|
| 198 |
@raises(ECannotLoadCompressedTexture If texture size is bad or OpenGL |
| 199 |
extensions are missing or mipmaps were required.) } |
| 200 |
procedure LoadCompressed(const Image: TGPUCompressedImage); |
| 201 |
begin |
| 202 |
if not (Image.Compression in GLFeatures.TextureCompression) then |
| 203 |
raise ECannotLoadCompressedTexture.CreateFmt('Cannot load textures compressed using %s, your OpenGL/OpenGLES does not support the necessary extensions', |
| 204 |
[TextureCompressionInfo[Image.Compression].Name]); |
| 205 |
|
| 206 |
if not IsCubeMapTextureSized(Image) then |
| 207 |
raise ECannotLoadCompressedTexture.CreateFmt('Cannot load compressed textures: texture size is %d x %d, it''s not correct for a cube map, and we cannot resize compressed textures', |
| 208 |
[Image.Width, Image.Height]); |
| 209 |
|
| 210 |
if Mipmaps then |
| 211 |
raise ECannotLoadCompressedTexture.Create('Cannot create mipmaps on CPU for compressed images'); |
| 212 |
|
| 213 |
{ Pixel packing parameters (stuff changed by Before/AfterUnpackImage) |
| 214 |
doesn't affect loading compressed textures, as far as I understand. |
| 215 |
So no need to call it. } |
| 216 |
glCompressedTexImage2D(Target, Level, ImageGLInternalFormat(Image), |
| 217 |
Image.Width, Image.Height, 0, Image.Size, Image.RawPixels); |
| 218 |
LoadedSize := LoadedSize + Image.Size; |
| 219 |
WasCompressed := true; |
| 220 |
end; |
| 221 |
|
| 222 |
procedure LoadMipmapped(const Image: TCastleImage); |
| 223 |
begin |
| 224 |
{ This should only be called by glTextureCubeMap when mipmaps are needed |
| 225 |
but GenerateMipmap was not available. |
| 226 |
We have no choice but to use old CastleBuild2DMipmaps. |
| 227 |
|
| 228 |
Note: SGIS_generate_mipmap doesn't work on cube map texture, |
| 229 |
testing on ATI Mobility Radeon X1600 (fglrx, Linux, on Mac Book Pro). } |
| 230 |
|
| 231 |
CastleBuild2DMipmaps(Target, Image, LoadedSize); |
| 232 |
end; |
| 233 |
|
| 234 |
{ Calls glTexImage2D for given image. |
| 235 |
Takes care of OpenGL unpacking (alignment etc.). |
| 236 |
Takes care of Image size --- makes sure that image has the right size |
| 237 |
(power of 2, within OpenGL required sizes). } |
| 238 |
procedure LoadNormal(const Image: TCastleImage); |
| 239 |
var |
| 240 |
ImgGood: TCastleImage; |
| 241 |
begin |
| 242 |
if IsCubeMapTextureSized(Image) then |
| 243 |
CastleTexImage2D(Target, Level, Image, LoadedSize) |
| 244 |
else |
| 245 |
begin |
| 246 |
ImgGood := ResizeToCubeMapTextureSize(Image); |
| 247 |
try |
| 248 |
CastleTexImage2D(Target, Level, ImgGood, LoadedSize); |
| 249 |
finally ImgGood.Free end; |
| 250 |
end; |
| 251 |
end; |
| 252 |
|
| 253 |
begin |
| 254 |
if Image is TGPUCompressedImage then |
| 255 |
LoadCompressed(TGPUCompressedImage(Image)) |
| 256 |
else |
| 257 |
if Image Is TCastleImage then |
| 258 |
begin |
| 259 |
if Mipmaps then |
| 260 |
LoadMipmapped(TCastleImage(Image)) |
| 261 |
else |
| 262 |
LoadNormal(TCastleImage(Image)); |
| 263 |
end else |
| 264 |
raise EInvalidImageForOpenGLTexture.CreateFmt('Cannot load to OpenGL texture image class %s', [Image.ClassName]); |
| 265 |
end; |
| 266 |
|
| 267 |
procedure glTextureCubeMap(const TextureIdForProfiler: TGLTextureId; |
| 268 |
const PositiveX, NegativeX, |
| 269 |
PositiveY, NegativeY, |
| 270 |
PositiveZ, NegativeZ: TEncodedImage; |
| 271 |
const CompositeForMipmaps: TCompositeImage; |
| 272 |
const Mipmaps: boolean; |
| 273 |
out DisableMipmaps: Boolean); |
| 274 |
var |
| 275 |
LoadedSize: Int64; |
| 276 |
|
| 277 |
{ Check should we load mipmaps from Composite. } |
| 278 |
function HasMipmapsFromComposite(Composite: TCompositeImage): boolean; |
| 279 |
begin |
| 280 |
Result := (Composite <> nil) and Composite.Mipmaps; |
| 281 |
if not Result then Exit; |
| 282 |
|
| 283 |
if Composite.CompositeType <> ctCubeMap then |
| 284 |
begin |
| 285 |
WritelnWarning('Texture', 'Composite (DDS, KTX...) image contains mipmaps, but not for CubeMap texture'); |
| 286 |
Exit(false); |
| 287 |
end; |
| 288 |
|
| 289 |
if not GLFeatures.TextureMaxLevel then |
| 290 |
begin |
| 291 |
WritelnWarning('Texture', 'Cannot load composite (DDS, KTX...) image containing mipmaps, because OpenGL(ES) does not support GL_TEXTURE_MAX_LEVEL'); |
| 292 |
Exit(false); |
| 293 |
end; |
| 294 |
end; |
| 295 |
|
| 296 |
{ Load mipmaps from Composite. Assume HasMipmapsFromComposite was true. } |
| 297 |
procedure LoadMipmapsFromComposite(const Composite: TCompositeImage); |
| 298 |
|
| 299 |
procedure LoadMipmapsFromCompositeSide(const GLSide: TGLenum; |
| 300 |
const CompositeSide: TCubeMapSide); |
| 301 |
var |
| 302 |
I: Integer; |
| 303 |
IgnoredWasCompressed: Boolean; |
| 304 |
begin |
| 305 |
IgnoredWasCompressed := false; |
| 306 |
for I := 1 to Composite.MipmapsCount - 1 do |
| 307 |
glTextureCubeMapSide(GLSide, |
| 308 |
Composite.CubeMapImage(CompositeSide, I), I, false, LoadedSize, IgnoredWasCompressed); |
| 309 |
end; |
| 310 |
|
| 311 |
begin |
| 312 |
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAX_LEVEL, Composite.MipmapsCount - 1); |
| 313 |
LoadMipmapsFromCompositeSide(GL_TEXTURE_CUBE_MAP_POSITIVE_X, csPositiveX); |
| 314 |
LoadMipmapsFromCompositeSide(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, csNegativeX); |
| 315 |
LoadMipmapsFromCompositeSide(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, csPositiveY); |
| 316 |
LoadMipmapsFromCompositeSide(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, csNegativeY); |
| 317 |
LoadMipmapsFromCompositeSide(GL_TEXTURE_CUBE_MAP_POSITIVE_Z, csPositiveZ); |
| 318 |
LoadMipmapsFromCompositeSide(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, csNegativeZ); |
| 319 |
end; |
| 320 |
|
| 321 |
var |
| 322 |
WasCompressed: Boolean; |
| 323 |
begin |
| 324 |
LoadedSize := 0; |
| 325 |
WasCompressed := false; |
| 326 |
DisableMipmaps := false; |
| 327 |
|
| 328 |
if Mipmaps and (HasMipmapsFromComposite(CompositeForMipmaps) or HasGenerateMipmap) then |
| 329 |
begin |
| 330 |
{ Load six cube faces without mipmaps, then generate them all |
| 331 |
in one go with GenerateMipmap. } |
| 332 |
glTextureCubeMapSide(GL_TEXTURE_CUBE_MAP_POSITIVE_X, PositiveX, 0, false, LoadedSize, WasCompressed); |
| 333 |
glTextureCubeMapSide(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, NegativeX, 0, false, LoadedSize, WasCompressed); |
| 334 |
glTextureCubeMapSide(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, PositiveY, 0, false, LoadedSize, WasCompressed); |
| 335 |
glTextureCubeMapSide(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, NegativeY, 0, false, LoadedSize, WasCompressed); |
| 336 |
glTextureCubeMapSide(GL_TEXTURE_CUBE_MAP_POSITIVE_Z, PositiveZ, 0, false, LoadedSize, WasCompressed); |
| 337 |
glTextureCubeMapSide(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, NegativeZ, 0, false, LoadedSize, WasCompressed); |
| 338 |
if HasMipmapsFromComposite(CompositeForMipmaps) then |
| 339 |
LoadMipmapsFromComposite(CompositeForMipmaps) else |
| 340 |
begin |
| 341 |
if WasCompressed then |
| 342 |
begin |
| 343 |
{ We cannot generate mipmaps when sides used GPU compression. |
| 344 |
See https://docs.gl/es2/glGenerateMipmap , |
| 345 |
"GL_INVALID_OPERATION is generated if the zero level array is stored in a compressed internal format." |
| 346 |
} |
| 347 |
WritelnWarning('Cubemap is loaded from a GPU-compressed texture, without mipmaps. Disabling mipmaps usage, as we cannot use GenerateMipmap on GPU-compressed cube map.'); |
| 348 |
DisableMipmaps := true; |
| 349 |
end else |
| 350 |
begin |
| 351 |
GenerateMipmap(GL_TEXTURE_CUBE_MAP); |
| 352 |
{ TODO: this is cubemap mipmaps size assuming that provided |
| 353 |
cubemap images were already power of 2, and within good dimensions. } |
| 354 |
LoadedSize := LoadedSize + TTextureMemoryProfiler.MipmapsSize(PositiveX); |
| 355 |
LoadedSize := LoadedSize + TTextureMemoryProfiler.MipmapsSize(NegativeX); |
| 356 |
LoadedSize := LoadedSize + TTextureMemoryProfiler.MipmapsSize(PositiveY); |
| 357 |
LoadedSize := LoadedSize + TTextureMemoryProfiler.MipmapsSize(NegativeY); |
| 358 |
LoadedSize := LoadedSize + TTextureMemoryProfiler.MipmapsSize(PositiveZ); |
| 359 |
LoadedSize := LoadedSize + TTextureMemoryProfiler.MipmapsSize(NegativeZ); |
| 360 |
end; |
| 361 |
end; |
| 362 |
end else |
| 363 |
begin |
| 364 |
glTextureCubeMapSide(GL_TEXTURE_CUBE_MAP_POSITIVE_X, PositiveX, 0, Mipmaps, LoadedSize, WasCompressed); |
| 365 |
glTextureCubeMapSide(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, NegativeX, 0, Mipmaps, LoadedSize, WasCompressed); |
| 366 |
glTextureCubeMapSide(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, PositiveY, 0, Mipmaps, LoadedSize, WasCompressed); |
| 367 |
glTextureCubeMapSide(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, NegativeY, 0, Mipmaps, LoadedSize, WasCompressed); |
| 368 |
glTextureCubeMapSide(GL_TEXTURE_CUBE_MAP_POSITIVE_Z, PositiveZ, 0, Mipmaps, LoadedSize, WasCompressed); |
| 369 |
glTextureCubeMapSide(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, NegativeZ, 0, Mipmaps, LoadedSize, WasCompressed); |
| 370 |
end; |
| 371 |
|
| 372 |
TextureMemoryProfiler.Allocate(TextureIdForProfiler, PositiveX.URL, |
| 373 |
'cubemap-' + PositiveX.ClassName, LoadedSize, Mipmaps, |
| 374 |
PositiveX.Width, PositiveX.Height, 1); |
| 375 |
end; |
| 376 |
|
| 377 |
{$endif read_implementation} |