| 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: 2D textures sizing and loading. |
| 18 |
Also decompressing GPU-compressed 2D textures, since it's technically related. } |
| 19 |
|
| 20 |
{$ifdef read_interface} |
| 21 |
|
| 22 |
{ ---------------------------------------------------------------------- |
| 23 |
Adjusting image sizes to load them as textures. |
| 24 |
Usually you don't need these functions, LoadGLTexture* and TDrawableImage |
| 25 |
and such call it automatically when needed. } |
| 26 |
{$ifdef read_interface_type} |
| 27 |
// type (continue type section from previous include file) |
| 28 |
{ Constraints on texture size, used by @link(ResizeToTextureSize) and @link(IsTextureSized). } |
| 29 |
TTextureSizing = ( |
| 30 |
{ Texture size does not have to be a power of two |
| 31 |
(unless GLFeatures.TextureNonPowerOfTwo = @false, in which case all textures |
| 32 |
must have power-of-two, and then tsAny may be scaled to satisfy it |
| 33 |
(but it still will not be scaled for GLTextureScale)). |
| 34 |
It is not affected by GLTextureScale. } |
| 35 |
tsAny, |
| 36 |
{ Texture size must be a power of two. |
| 37 |
It is not affected by GLTextureScale, because we cannot scale it. } |
| 38 |
tsRequiredPowerOf2, |
| 39 |
{ Texture size must be a power of two. |
| 40 |
It is affected by GLTextureScale, we can scale it. } |
| 41 |
tsScalablePowerOf2 |
| 42 |
); |
| 43 |
{$endif read_interface_type} |
| 44 |
|
| 45 |
{$ifdef read_interface_func} |
| 46 |
{ Resize the image to a size accepted as GL_TEXTURE_2D texture size |
| 47 |
for OpenGL. It tries to resize to a larger size, not smaller, |
| 48 |
to avoid losing image information. |
| 49 |
|
| 50 |
It also makes texture have power-of-two size, if Sizing <> tsAny |
| 51 |
(or if GLFeatures.TextureNonPowerOfTwo = @false). |
| 52 |
This is a must for normal textures, used for 3D rendering |
| 53 |
(with mipmapping and such). |
| 54 |
Using OpenGL non-power-of-2 textures is not good for such usage case, |
| 55 |
some OpenGLs crash (ATI), |
| 56 |
some are ultra slow (NVidia), some cause artifacts (Mesa). |
| 57 |
OpenGL ES explicitly limits what you can do with non-power-of-2. |
| 58 |
Sample model using non-power-of-2 is in inlined_textures.wrl. |
| 59 |
|
| 60 |
Use Sizing = tsAny only for textures that you plan to use |
| 61 |
for drawing GUI images by TDrawableImage. |
| 62 |
|
| 63 |
@groupBegin } |
| 64 |
procedure ResizeForTextureSize(var r: TCastleImage; const Sizing: TTextureSizing); |
| 65 |
function ResizeToTextureSize(const r: TCastleImage; const Sizing: TTextureSizing): TCastleImage; overload; |
| 66 |
function ResizeToTextureSize(const Size: Cardinal; const Sizing: TTextureSizing): Cardinal; overload; |
| 67 |
{ @groupEnd } |
| 68 |
|
| 69 |
{ Does image have proper size for 2D OpenGL texture. |
| 70 |
See ResizeForTextureSize. Note that this checks glGet(GL_MAX_TEXTURE_SIZE), |
| 71 |
so requires initialized OpenGL context. } |
| 72 |
function IsTextureSized(const r: TEncodedImage; const Sizing: TTextureSizing): boolean; overload; |
| 73 |
|
| 74 |
function IsTextureSized(const Width, Height: Cardinal; const Sizing: TTextureSizing): boolean; overload; |
| 75 |
|
| 76 |
var |
| 77 |
{ Scaling for all textures loaded to OpenGL. |
| 78 |
This allows you to conserve GPU memory. |
| 79 |
Each size (width, height, and (for 3D textures) depth) is scaled |
| 80 |
by 1 / 2^GLTextureScale. |
| 81 |
So value = 1 means no scaling, value = 2 means that each size is 1/2 |
| 82 |
(texture area is 1/4), value = 3 means that each size is 1/4 and so on. |
| 83 |
|
| 84 |
Note that textures used for GUI, by TDrawableImage |
| 85 |
(more precisely: all non-power-of-2 textures) avoid this scaling entirely. |
| 86 |
|
| 87 |
This allows to scale the textures at runtime. |
| 88 |
For an independent mechanism that allows to @bold(load) already |
| 89 |
downscaled textures (and scale them as a preprocessing step when |
| 90 |
packaging your game), see @link(TextureLoadingScale). |
| 91 |
The documentation of @link(TextureLoadingScale) lists all the differences |
| 92 |
between these two approaches to scaling. } |
| 93 |
GLTextureScale: Cardinal = 1; |
| 94 |
|
| 95 |
{ Constraints the scaling done by GLTextureScale. |
| 96 |
Scaling caused by GLTextureScale cannot scale texture to something |
| 97 |
less than GLTextureMinSize. If texture size was already < GLTextureMinSize, |
| 98 |
it is not scaled at all by GLTextureScale. |
| 99 |
This must be a power of two. } |
| 100 |
GLTextureMinSize: Cardinal = 16; |
| 101 |
|
| 102 |
{ Load new texture to OpenGL. Generates new texture number by glGenTextures, |
| 103 |
then binds this texture, and loads it's data. |
| 104 |
|
| 105 |
Takes care of UNPACK_ALIGNMENT inside (if needed, we'll change it and |
| 106 |
later revert back, so that the texture is correctly loaded). |
| 107 |
|
| 108 |
Sets texture minification, magnification filters and wrap parameters. |
| 109 |
|
| 110 |
Changes currently bound texture to this one (returned). |
| 111 |
|
| 112 |
If mipmaps will be needed (this is decided looking at Filter.Minification) |
| 113 |
we will load them too. |
| 114 |
|
| 115 |
@orderedList( |
| 116 |
@item( |
| 117 |
As a first try, if CompositeForMipmaps is non-nil |
| 118 |
and has mipmaps (CompositeForMipmaps.Mipmaps), we will load these mipmaps. |
| 119 |
CompositeForMipmaps must be a normal 2D texture (CompositeType = ctTexture). |
| 120 |
|
| 121 |
Otherwise, we'll try to generate mipmaps, using various OpenGL mechanisms.) |
| 122 |
|
| 123 |
@item( |
| 124 |
We will try using GenerateMipmap functionality to generate mipmaps on GPU. |
| 125 |
If not available, for uncompressed textures, we will generate mipmaps on CPU. |
| 126 |
For compressed textures, we will change minification filter to simple |
| 127 |
minLinear and make WritelnWarning.) |
| 128 |
) |
| 129 |
|
| 130 |
@param(GUITexture If @true, the texture is not forced to have sizes |
| 131 |
as power-of-two. For TTextureSizing, it is like tsAny instead |
| 132 |
of tsScalablePowerOf2. |
| 133 |
Also, mipmaps are always disabled, regardless of requested |
| 134 |
Filter value, since mipmaps cannot work without power-of-two.) |
| 135 |
|
| 136 |
@raises(ETextureLoadError If texture cannot be loaded for whatever reason. |
| 137 |
This includes ECannotLoadCompressedTexture if the compressed texture cannot be |
| 138 |
loaded for whatever reason. |
| 139 |
This includes EInvalidImageForOpenGLTexture if Image class is invalid |
| 140 |
for an OpenGL texture.) |
| 141 |
|
| 142 |
@raises(EImageClassNotSupportedForOpenGL When Image class is not supported |
| 143 |
by OpenGL.) |
| 144 |
|
| 145 |
@groupBegin } |
| 146 |
function LoadGLTexture(const image: TEncodedImage; |
| 147 |
const Filter: TTextureFilter; |
| 148 |
const Wrap: TTextureWrap2D; |
| 149 |
const CompositeForMipmaps: TCompositeImage = nil; |
| 150 |
const GUITexture: boolean = false): TGLTextureId; overload; |
| 151 |
|
| 152 |
function LoadGLTexture(const Url: String; |
| 153 |
const Filter: TTextureFilter; |
| 154 |
const Wrap: TTextureWrap2D; |
| 155 |
const CompositeForMipmaps: TCompositeImage = nil; |
| 156 |
const GUITexture: boolean = false): TGLTextureId; overload; |
| 157 |
{ @groupEnd } |
| 158 |
|
| 159 |
{ Load OpenGL texture into already reserved texture number. |
| 160 |
It uses existing OpenGL texture number (TextureId). Everything else |
| 161 |
works exactly the same as LoadGLTexture. |
| 162 |
|
| 163 |
You can also use this to set "default unnamed OpenGL texture" parameters |
| 164 |
by passing TextureId = 0. |
| 165 |
|
| 166 |
@raises(ETextureLoadError Raised in the same situations as LoadGLTexture.) |
| 167 |
|
| 168 |
@raises(EImageClassNotSupportedForOpenGL When Image class is not supported |
| 169 |
by OpenGL.) |
| 170 |
|
| 171 |
@groupBegin } |
| 172 |
procedure LoadGLGeneratedTexture(const TextureId: TGLTextureId; const image: TEncodedImage; |
| 173 |
Filter: TTextureFilter; |
| 174 |
const Wrap: TTextureWrap2D; |
| 175 |
const CompositeForMipmaps: TCompositeImage = nil; |
| 176 |
const GUITexture: boolean = false); overload; |
| 177 |
{ @groupEnd } |
| 178 |
|
| 179 |
{ Decompress texture image by loading it to a temporary OpenGL(ES) texture and |
| 180 |
reading back. IOW, this does decompression by using current OpenGL(ES) context. |
| 181 |
|
| 182 |
TODO: Note that the current implementation will always raise ECannotLoadCompressedTexture |
| 183 |
on OpenGLES, e.g. on mobile. Same as SaveTextureContents. |
| 184 |
This is fixable (we need to use FBO instead of glGetTexImage on OpenGLES), |
| 185 |
please submit a bugreport if you need it. |
| 186 |
|
| 187 |
@raises(ECannotLoadCompressedTexture If we cannot decompress, for example |
| 188 |
because we cannot load to OpenGL this compressed texture (for example |
| 189 |
because appropriate OpenGL extensions are not available, or such).) } |
| 190 |
function GLDecompressTexture(const Image: TGPUCompressedImage): TCastleImage; |
| 191 |
|
| 192 |
{$endif read_interface_func} |
| 193 |
|
| 194 |
{$ifdef read_interface_type} |
| 195 |
// type (continue type section from previous include file) |
| 196 |
{ Raised by SaveTextureContents when we cannot get texture contents. } |
| 197 |
ECannotSaveTextureContents = class(Exception); |
| 198 |
{$endif read_interface_type} |
| 199 |
|
| 200 |
{$ifdef read_interface_func} |
| 201 |
{ Save OpenGL texture contents to TCastleImage. |
| 202 |
@bold(Size of the texture must match the Image size, |
| 203 |
otherwise terrible things (writing over unallocated memory) will happen). |
| 204 |
|
| 205 |
@raises(ECannotSaveTextureContents If we cannot get texture contents |
| 206 |
(not supported on OpenGLES).) } |
| 207 |
procedure SaveTextureContents(const Image: TCastleImage; const Texture: TGLTextureId); |
| 208 |
{$endif read_interface_func} |
| 209 |
|
| 210 |
{$endif read_interface} |
| 211 |
|
| 212 |
{$ifdef read_implementation} |
| 213 |
|
| 214 |
{ ---------------------------------------------------------------------- |
| 215 |
Adjusting image size for 2D texture. } |
| 216 |
|
| 217 |
function ResizeToTextureSize(const Size: Cardinal; const Sizing: TTextureSizing): Cardinal; |
| 218 |
begin |
| 219 |
{ make sure it's power of 2, if necessary } |
| 220 |
if ((Sizing = tsAny) and GLFeatures.TextureNonPowerOfTwo) or |
| 221 |
IsPowerOf2(Size) then |
| 222 |
Result := Size else |
| 223 |
Result := 1 shl (Biggest2Exponent(Size) + 1); |
| 224 |
|
| 225 |
{ apply GLTextureScale } |
| 226 |
if (Sizing = tsScalablePowerOf2) and |
| 227 |
(GLTextureScale > 1) and |
| 228 |
(Result > GLTextureMinSize) then |
| 229 |
Result := Max(Result shr (GLTextureScale - 1), GLTextureMinSize); |
| 230 |
|
| 231 |
{ honour GLFeatures.MaxTextureSize } |
| 232 |
if Result > GLFeatures.MaxTextureSize then |
| 233 |
Result := GLFeatures.MaxTextureSize; |
| 234 |
end; |
| 235 |
|
| 236 |
function ResizeToTextureSize(const r: TCastleImage; const Sizing: TTextureSizing): TCastleImage; |
| 237 |
var |
| 238 |
NewWidth, NewHeight: Cardinal; |
| 239 |
begin |
| 240 |
NewWidth := ResizeToTextureSize(R.Width , Sizing); |
| 241 |
NewHeight := ResizeToTextureSize(R.Height, Sizing); |
| 242 |
|
| 243 |
WritelnLog('Textures', Format('Resizing 2D texture "%s" from %dx%d to %dx%d', |
| 244 |
[UriDisplay(R.Url), R.Width, R.Height, NewWidth, NewHeight])); |
| 245 |
|
| 246 |
result := r.MakeResized(NewWidth, NewHeight, riBilinear); |
| 247 |
end; |
| 248 |
|
| 249 |
function IsTextureSized(const Width, Height: Cardinal; |
| 250 |
const Sizing: TTextureSizing): boolean; |
| 251 |
begin |
| 252 |
Result := |
| 253 |
(Width = ResizeToTextureSize(Width , Sizing)) and |
| 254 |
(Height = ResizeToTextureSize(Height, Sizing)); |
| 255 |
end; |
| 256 |
|
| 257 |
function IsTextureSized(const r: TEncodedImage; const Sizing: TTextureSizing): boolean; |
| 258 |
begin |
| 259 |
Result := IsTextureSized(r.Width, r.Height, Sizing); |
| 260 |
end; |
| 261 |
|
| 262 |
procedure ResizeForTextureSize(var r: TCastleImage; const Sizing: TTextureSizing); |
| 263 |
var |
| 264 |
newR: TCastleImage; |
| 265 |
begin |
| 266 |
if not IsTextureSized(r, Sizing) then |
| 267 |
begin |
| 268 |
newR := ResizeToTextureSize(r, Sizing); |
| 269 |
FreeAndNil(r); |
| 270 |
r := newR; |
| 271 |
end; |
| 272 |
end; |
| 273 |
|
| 274 |
function TargetIsCubeMapSide(const Target: TGLenum): Boolean; |
| 275 |
begin |
| 276 |
{ We rely on the fact that 6 relevant constants are consecutive. } |
| 277 |
Result := Between(Target, GL_TEXTURE_CUBE_MAP_POSITIVE_X, GL_TEXTURE_CUBE_MAP_NEGATIVE_Z); |
| 278 |
end; |
| 279 |
|
| 280 |
{ Call glTexImage2D to load given image. |
| 281 |
Takes care of OpenGL(ES) unpacking (alignment etc.). |
| 282 |
Assumes that image already has the right size |
| 283 |
(power of 2 and within OpenGL(ES) required sizes). |
| 284 |
|
| 285 |
Level = 0 for base (not a mipmap sublevel) image. |
| 286 |
|
| 287 |
Target has the same meaning as for glTexImage2D. |
| 288 |
|
| 289 |
LoadedSize will be increased. } |
| 290 |
procedure CastleTexImage2D(const Target: TGLenum; const Level: TGLint; const Image: TCastleImage; |
| 291 |
var LoadedSize: Int64); |
| 292 |
var |
| 293 |
ImageFormat, ImageInternalFormat, SwizzleTarget: TGLenum; |
| 294 |
ImageSwizzle: TVector4Integer; |
| 295 |
begin |
| 296 |
BeforeUnpackImage(Image); |
| 297 |
try |
| 298 |
{$ifndef OpenGLES} |
| 299 |
{ Workaround Mesa 7.9-devel bug (at least with Intel DRI, |
| 300 |
on Ubuntu 10.10, observed on domek): glTexImage2D accidentally |
| 301 |
enables GL_TEXTURE_2D. } |
| 302 |
if GLFeatures.EnableFixedFunction and GLVersion.Mesa then |
| 303 |
glPushAttrib(GL_ENABLE_BIT); |
| 304 |
{$endif} |
| 305 |
|
| 306 |
UncompressedGLFormatAndInternalFormat(Image, ImageFormat, ImageInternalFormat, ImageSwizzle); |
| 307 |
glTexImage2D(Target, Level, ImageInternalFormat, |
| 308 |
Image.Width, Image.Height, 0, ImageFormat, ImageGLType(Image), |
| 309 |
Image.RawPixels); |
| 310 |
LoadedSize := LoadedSize + Image.Size; |
| 311 |
|
| 312 |
if GLFeatures.TextureModernFormatsAndSwizzle then |
| 313 |
begin |
| 314 |
{ We cannot use cubemap side as swizzle target, |
| 315 |
we need to use GL_TEXTURE_CUBE_MAP, |
| 316 |
following https://registry.khronos.org/OpenGL-Refpages/gl4/html/glTexParameter.xhtml . } |
| 317 |
SwizzleTarget := Target; |
| 318 |
if TargetIsCubeMapSide(SwizzleTarget) then |
| 319 |
SwizzleTarget := GL_TEXTURE_CUBE_MAP; |
| 320 |
glTexParameteriv(SwizzleTarget, GL_TEXTURE_SWIZZLE_RGBA, @ImageSwizzle); |
| 321 |
end; |
| 322 |
|
| 323 |
{$ifndef OpenGLES} |
| 324 |
if GLFeatures.EnableFixedFunction and GLVersion.Mesa then |
| 325 |
glPopAttrib; |
| 326 |
{$endif} |
| 327 |
finally AfterUnpackImage(Image) end; |
| 328 |
end; |
| 329 |
|
| 330 |
{ Load the image, loading also mipmaps, generating mipmaps for all the levels. |
| 331 |
As mipmaps only make sense for power-of-2 images, |
| 332 |
this also resizes original image to be power-of-2, if necessary. |
| 333 |
|
| 334 |
This is similar to libGLU gluBuild2DMipmaps. |
| 335 |
|
| 336 |
Note that mipmaps are done by our code, on CPU. |
| 337 |
You should prefer using OpenGL(ES) GenerateMipmap feature, |
| 338 |
available on modern OpenGL and on all OpenGLES >= 2, |
| 339 |
than this routine. |
| 340 |
|
| 341 |
Target has the same meaning as for glTexImage2D. |
| 342 |
|
| 343 |
LoadedSize will be increased. } |
| 344 |
procedure CastleBuild2DMipmaps(const Target: TGLenum; const Image: TCastleImage; var LoadedSize: Int64); |
| 345 |
const |
| 346 |
Interpolation = riBilinear; |
| 347 |
var |
| 348 |
W, H, Level: Cardinal; |
| 349 |
NewImage: TCastleImage; |
| 350 |
begin |
| 351 |
(* For history: implementation using libGLU: |
| 352 |
|
| 353 |
BeforeUnpackImage(Image); |
| 354 |
try |
| 355 |
gluBuild2DMipmaps(Target, ImageInternalFormat, |
| 356 |
Image.Width, Image.Height, ImageFormat, ImageGLType(Image), |
| 357 |
Image.RawPixels); |
| 358 |
LoadedSize := LoadedSize + TTextureMemoryProfiler.MipmapsSize(Image); |
| 359 |
finally AfterUnpackImage(Image) end; |
| 360 |
*) |
| 361 |
|
| 362 |
if Image.Depth <> 1 then |
| 363 |
raise ETextureLoadError.Create('Cannot generate mipmaps on CPU for 3D texture (depth <> 1)'); |
| 364 |
|
| 365 |
WritelnWarning('CastleBuild2DMipmaps', 'Generating mipmaps on CPU for texture "%s". This indicates very old GPU (not capable of glGenerateMipmap), and will be slow.', [ |
| 366 |
UriDisplay(Image.Url) |
| 367 |
]); |
| 368 |
|
| 369 |
Level := 0; |
| 370 |
W := ResizeToTextureSize(Image.Width , tsScalablePowerOf2); |
| 371 |
H := ResizeToTextureSize(Image.Height, tsScalablePowerOf2); |
| 372 |
|
| 373 |
// WritelnLog('CastleBuild2DMipmaps', Format('Resizing 2D texture "%s" from %dx%d to %dx%d', [ |
| 374 |
// UriDisplay(Image.Url), |
| 375 |
// Image.Width, |
| 376 |
// Image.Height, |
| 377 |
// W, |
| 378 |
// H |
| 379 |
// ])); |
| 380 |
|
| 381 |
NewImage := Image.MakeResized(W, H, riBilinear); |
| 382 |
try |
| 383 |
repeat |
| 384 |
CastleTexImage2D(Target, Level, NewImage, LoadedSize); |
| 385 |
|
| 386 |
if not ((W > 1) or (H > 1)) then Break; // no size > 1 ? then finish |
| 387 |
if W > 1 then W := W div 2; |
| 388 |
if H > 1 then H := H div 2; |
| 389 |
Inc(Level); |
| 390 |
|
| 391 |
// WritelnLog('CastleBuild2DMipmaps', Format('Resizing 2D texture "%s" from %dx%d to %dx%d', [ |
| 392 |
// UriDisplay(NewImage.Url), |
| 393 |
// NewImage.Width, |
| 394 |
// NewImage.Height, |
| 395 |
// W, |
| 396 |
// H |
| 397 |
// ])); |
| 398 |
NewImage.Resize(W, H, riBilinear); |
| 399 |
until false; |
| 400 |
finally FreeAndNil(NewImage) end; |
| 401 |
end; |
| 402 |
|
| 403 |
{ 2D texture loading --------------------------------------------------------- } |
| 404 |
|
| 405 |
function LoadGLTexture(const image: TEncodedImage; |
| 406 |
const Filter: TTextureFilter; const Wrap: TTextureWrap2D; |
| 407 |
const CompositeForMipmaps: TCompositeImage; const GUITexture: boolean): TGLTextureId; |
| 408 |
begin |
| 409 |
glGenTextures(1, @result); |
| 410 |
LoadGLGeneratedTexture(result, image, Filter, Wrap, CompositeForMipmaps, GUITexture); |
| 411 |
end; |
| 412 |
|
| 413 |
function LoadGLTexture(const Url: String; |
| 414 |
const Filter: TTextureFilter; const Wrap: TTextureWrap2D; |
| 415 |
const CompositeForMipmaps: TCompositeImage; const GUITexture: boolean): TGLTextureId; |
| 416 |
var |
| 417 |
Image: TEncodedImage; |
| 418 |
begin |
| 419 |
Image := LoadTextureImage(Url); |
| 420 |
try |
| 421 |
Result := LoadGLTexture(Image, Filter, Wrap, CompositeForMipmaps, GUITexture); |
| 422 |
finally Image.Free end; |
| 423 |
end; |
| 424 |
|
| 425 |
{ Load Image through glCompressedTexImage2D[ARB]. |
| 426 |
This checks existence of OpenGL extensions for Image compression format, |
| 427 |
and checks Image sizes. |
| 428 |
It also takes care of pixel packing, although actually nothing needs |
| 429 |
be done about it when using compressed textures. |
| 430 |
|
| 431 |
Level = 0 for base (not a mipmap sublevel) image. |
| 432 |
|
| 433 |
@raises(ECannotLoadCompressedTexture If texture size is bad or necessary |
| 434 |
OpenGL extensions (for the image compression type) are missing.) } |
| 435 |
procedure glCompressedTextureImage2D(Image: TGPUCompressedImage; Level: TGLint; |
| 436 |
var LoadedSize: Int64); |
| 437 |
begin |
| 438 |
if not (Image.Compression in GLFeatures.TextureCompression) then |
| 439 |
raise ECannotLoadCompressedTexture.CreateFmt('Cannot load textures compressed using %s, your OpenGL/OpenGLES does not support the necessary extensions', |
| 440 |
[TextureCompressionInfo[Image.Compression].Name]); |
| 441 |
|
| 442 |
if TextureCompressionInfo[Image.Compression].RequiresPowerOf2 and |
| 443 |
not IsTextureSized(Image, tsRequiredPowerOf2) then |
| 444 |
raise ECannotLoadCompressedTexture.CreateFmt('Cannot load compressed textures: texture size is %d x %d, it''s not a power-of-2. Textures compressed with %s must be provided with power-of-2 sizes.', |
| 445 |
[Image.Width, Image.Height, TextureCompressionInfo[Image.Compression].Name]); |
| 446 |
|
| 447 |
{ Pixel packing parameters (stuff changed by Before/AfterUnpackImage) |
| 448 |
doesn't affect loading compressed textures, as far as I understand. |
| 449 |
So no need to call it. } |
| 450 |
glCompressedTexImage2D(GL_TEXTURE_2D, Level, ImageGLInternalFormat(Image), |
| 451 |
Image.Width, Image.Height, 0, Image.Size, Image.RawPixels); |
| 452 |
LoadedSize := LoadedSize + Image.Size; |
| 453 |
end; |
| 454 |
|
| 455 |
procedure CalculateImageRightSize( |
| 456 |
const Image: TCastleImage; const GUITexture: boolean; |
| 457 |
out ImageRightSize: TCastleImage; out ImageRightSizeFree: boolean); |
| 458 |
var |
| 459 |
Sizing: TTextureSizing; |
| 460 |
begin |
| 461 |
if GUITexture then |
| 462 |
Sizing := tsAny else |
| 463 |
Sizing := tsScalablePowerOf2; |
| 464 |
if IsTextureSized(Image, Sizing) then |
| 465 |
begin |
| 466 |
ImageRightSize := Image; |
| 467 |
ImageRightSizeFree := false; |
| 468 |
end else |
| 469 |
begin |
| 470 |
ImageRightSize := ResizeToTextureSize(Image, Sizing); |
| 471 |
ImageRightSizeFree := true; |
| 472 |
end; |
| 473 |
end; |
| 474 |
|
| 475 |
procedure LoadGLGeneratedTexture(const TextureId: TGLTextureId; |
| 476 |
const Image: TEncodedImage; Filter: TTextureFilter; const Wrap: TTextureWrap2D; |
| 477 |
const CompositeForMipmaps: TCompositeImage; const GUITexture: boolean); |
| 478 |
var |
| 479 |
LoadedSize: Int64; |
| 480 |
|
| 481 |
procedure LoadNormal(const Image: TCastleImage); |
| 482 |
begin |
| 483 |
CastleTexImage2D(GL_TEXTURE_2D, 0, Image, LoadedSize); |
| 484 |
end; |
| 485 |
|
| 486 |
{ Check should we load mipmaps from Composite image. Load them, if yes. |
| 487 |
|
| 488 |
If LoadBase this also loads the base image (mipmap level 0). |
| 489 |
|
| 490 |
Note that I observed a bug on NVidia GeForce FX 5200, with various driver |
| 491 |
versions on both Linux 32 bit, 64 bit, and Windows 32 bit: |
| 492 |
you cannot load the base texture level (0) *after* loading the mipmaps. |
| 493 |
Doing so results in mipmaps being ignored, and seemingly GL_NEAREST |
| 494 |
minification filtering used (ignoring our set Filter.Minification). |
| 495 |
This could be easily observed with |
| 496 |
demo_models/x3d/tex_visualize_mipmaps.x3dv, |
| 497 |
switching to viewpoint like "Mipmaps from DDS" or "Colored mipmaps from DDS" |
| 498 |
--- you could clearly see that mipmaps are ignored and ugly nearest filtering |
| 499 |
gets used. |
| 500 |
Using LoadBase automatically workarounds this. } |
| 501 |
function LoadMipmapsFromComposite(Composite: TCompositeImage; LoadBase: boolean): boolean; |
| 502 |
var |
| 503 |
I, FromLevel: Integer; |
| 504 |
ImageRightSize: TCastleImage; |
| 505 |
ImageRightSizeFree: boolean; |
| 506 |
begin |
| 507 |
Result := (Composite <> nil) and Composite.Mipmaps; |
| 508 |
if not Result then Exit; |
| 509 |
|
| 510 |
if Composite.CompositeType <> ctTexture then |
| 511 |
begin |
| 512 |
WritelnWarning('Texture', 'Composite (DDS, KTX...) image contains mipmaps, but not for 2D texture'); |
| 513 |
Exit(false); |
| 514 |
end; |
| 515 |
|
| 516 |
if not GLFeatures.TextureMaxLevel then |
| 517 |
begin |
| 518 |
WritelnWarning('Texture', 'Cannot load composite (DDS, KTX...) image containing mipmaps, because OpenGL(ES) does not support GL_TEXTURE_MAX_LEVEL'); |
| 519 |
Exit(false); |
| 520 |
end; |
| 521 |
|
| 522 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, Composite.MipmapsCount - 1); |
| 523 |
if LoadBase then |
| 524 |
FromLevel := 0 |
| 525 |
else |
| 526 |
FromLevel := 1; |
| 527 |
for I := FromLevel to Composite.MipmapsCount - 1 do |
| 528 |
if Composite.Images[I] is TCastleImage then |
| 529 |
begin |
| 530 |
CalculateImageRightSize(TCastleImage(Composite.Images[I]), GUITexture, |
| 531 |
ImageRightSize, ImageRightSizeFree); |
| 532 |
try |
| 533 |
CastleTexImage2D(GL_TEXTURE_2D, I, ImageRightSize, LoadedSize); |
| 534 |
finally |
| 535 |
if ImageRightSizeFree then |
| 536 |
FreeAndNil(ImageRightSize) |
| 537 |
else |
| 538 |
ImageRightSize := nil; |
| 539 |
end; |
| 540 |
end else |
| 541 |
if Composite.Images[I] is TGPUCompressedImage then |
| 542 |
glCompressedTextureImage2D(TGPUCompressedImage(Composite.Images[I]), I, LoadedSize) |
| 543 |
else |
| 544 |
raise EInvalidImageForOpenGLTexture.CreateFmt('Cannot load to OpenGL texture image class %s', [Image.ClassName]); |
| 545 |
end; |
| 546 |
|
| 547 |
procedure LoadMipmapped(const Image: TCastleImage); |
| 548 |
begin |
| 549 |
if not LoadMipmapsFromComposite(CompositeForMipmaps, true) then |
| 550 |
begin |
| 551 |
if HasGenerateMipmap then |
| 552 |
begin |
| 553 |
CastleTexImage2D(GL_TEXTURE_2D, 0, Image, LoadedSize); |
| 554 |
{ hardware-accelerated mipmap generation } |
| 555 |
GenerateMipmap(GL_TEXTURE_2D); |
| 556 |
LoadedSize := LoadedSize + TTextureMemoryProfiler.MipmapsSize(Image); |
| 557 |
end else |
| 558 |
begin |
| 559 |
CastleBuild2DMipmaps(GL_TEXTURE_2D, Image, LoadedSize); |
| 560 |
end; |
| 561 |
end; |
| 562 |
end; |
| 563 |
|
| 564 |
var |
| 565 |
ImageRightSize: TCastleImage; |
| 566 |
ImageRightSizeFree: boolean; |
| 567 |
begin |
| 568 |
if GUITexture then |
| 569 |
Filter.DisableMipmaps; |
| 570 |
|
| 571 |
LoadedSize := 0; |
| 572 |
|
| 573 |
{ bind the texture, set min, mag filters and wrap parameters } |
| 574 |
glBindTexture(GL_TEXTURE_2D, TextureId); |
| 575 |
SetTextureFilter(GL_TEXTURE_2D, Filter); |
| 576 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, Wrap.Data[0]); |
| 577 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, Wrap.Data[1]); |
| 578 |
|
| 579 |
{ give the texture data } |
| 580 |
if Image is TCastleImage then |
| 581 |
begin |
| 582 |
CalculateImageRightSize(TCastleImage(Image), GUITexture, |
| 583 |
ImageRightSize, ImageRightSizeFree); |
| 584 |
try |
| 585 |
{ Load uncompressed } |
| 586 |
if Filter.NeedsMipmaps then |
| 587 |
LoadMipmapped(ImageRightSize) |
| 588 |
else |
| 589 |
LoadNormal(ImageRightSize); |
| 590 |
|
| 591 |
TextureMemoryProfiler.Allocate(TextureId, Image.Url, Image.ClassName, LoadedSize, |
| 592 |
Filter.NeedsMipmaps, ImageRightSize.Width, ImageRightSize.Height, 1); |
| 593 |
finally |
| 594 |
if ImageRightSizeFree then |
| 595 |
FreeAndNil(ImageRightSize) |
| 596 |
else |
| 597 |
ImageRightSize := nil; |
| 598 |
end; |
| 599 |
end else |
| 600 |
if Image is TGPUCompressedImage then |
| 601 |
begin |
| 602 |
{ Load compressed } |
| 603 |
glCompressedTextureImage2D(TGPUCompressedImage(Image), 0, LoadedSize); |
| 604 |
|
| 605 |
if Filter.NeedsMipmaps then |
| 606 |
begin |
| 607 |
if not LoadMipmapsFromComposite(CompositeForMipmaps, false) then |
| 608 |
begin |
| 609 |
// GenerateMipmap(GL_TEXTURE_2D); |
| 610 |
// LoadedSize := LoadedSize + TTextureMemoryProfiler.MipmapsSize(Image); |
| 611 |
|
| 612 |
{ On Android doing GenerateMipmap on compressed images |
| 613 |
definitely fails. Tested on: |
| 614 |
- Qualcomm/Adreno (Nexus 5), OpenGL ES 3.0 [email protected] AU@ |
| 615 |
- PowerVR (Pentagram Tab 7.6) |
| 616 |
- Qualcomm/Adreno (older phone from PW) |
| 617 |
and GenerateMipmap there definitely doesn't work. |
| 618 |
|
| 619 |
On Nexus 5 it even makes GL_INVALID_OPERATION. |
| 620 |
(On PowerVR my testcase was doing GL_OUT_OF_MEMORY, so it posibly |
| 621 |
masked earlier GL_INVALID_OPERATION. And on "older phone from PW" |
| 622 |
I didn't run -dDEBUG version. IOW, it is possible that *everywhere* |
| 623 |
we had GL_INVALID_OPERATION error.) |
| 624 |
|
| 625 |
OTOH, on desktop OpenGL, with DXT5 textures, |
| 626 |
4.4.0 NVIDIA 340.96 on Linux, GenerateMipmap on DXT5 indeed works. |
| 627 |
No GL error (and glGetError is checked), |
| 628 |
and the mipmaps are really generated (commenting out makes them white). |
| 629 |
|
| 630 |
Docs (for OpenGL, GLES 2.0, GLES 3.2 |
| 631 |
https://www.khronos.org/opengles/sdk/docs/man/xhtml/glGenerateMipmap.xml |
| 632 |
https://www.khronos.org/opengles/sdk/docs/man/ |
| 633 |
https://www.khronos.org/opengles/sdk/docs/man32/ |
| 634 |
) all say: |
| 635 |
GL_INVALID_OPERATION is generated if the zero level array is stored in a compressed internal format. |
| 636 |
|
| 637 |
This suggests that it's safest to assume that GenerateMipmap DOES NOT |
| 638 |
work on compressed textures, and (if mipmaps were not provided explicitly) |
| 639 |
we cannot use them. } |
| 640 |
|
| 641 |
{ Update GL_TEXTURE_MIN_FILTER, since we already initialized it earlier. } |
| 642 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 643 |
{ Warn, so that developer is aware of it. |
| 644 |
Better solution may be to |
| 645 |
- compress textures along with mipmaps. |
| 646 |
- or not request mipmaps (filtering = linear from start). |
| 647 |
Without doing anything, using compressed textures will be inconsistent |
| 648 |
with uncompressed, since the analogous code will succeed in creating |
| 649 |
mipmaps from uncompressed texture data. } |
| 650 |
WritelnWarning('Texture', 'Generating mipmaps for compressed textures not possible. Turning off mipmaps for ' + Image.Url); |
| 651 |
end; |
| 652 |
end; |
| 653 |
|
| 654 |
TextureMemoryProfiler.Allocate(TextureId, Image.Url, Image.ClassName, LoadedSize, |
| 655 |
Filter.NeedsMipmaps, Image.Width, Image.Height, 1); |
| 656 |
end else |
| 657 |
raise EInvalidImageForOpenGLTexture.CreateFmt('Cannot load to OpenGL texture image class %s', [Image.ClassName]); |
| 658 |
end; |
| 659 |
|
| 660 |
{ GLDecompressTexture ------------------------------------------------------------- } |
| 661 |
|
| 662 |
function GLDecompressTexture(const Image: TGPUCompressedImage): TCastleImage; |
| 663 |
|
| 664 |
{$ifndef OpenGLES} |
| 665 |
var |
| 666 |
Tex: TGLTextureId; |
| 667 |
LoadedSize: Int64; |
| 668 |
begin |
| 669 |
glGenTextures(1, @Tex); |
| 670 |
glBindTexture(GL_TEXTURE_2D, Tex); |
| 671 |
|
| 672 |
{ Testcase that fails on Radeon chantal (ATI Radeon X1600) Linux: |
| 673 |
demo_models/textures/marble_with_mipmaps_s3tc.dds |
| 674 |
|
| 675 |
No problem on NVidia (fpc 2.2.2 kocury/linux/32, fpc 2.2.4 kocury/linux/32), |
| 676 |
and no problem on macOS with the same GPU (also chantal, 32bit, fpc 2.2.4). |
| 677 |
So I'm assuming it's fglrx-specific bug. } |
| 678 |
if GLVersion.Fglrx and ( (Image.Width < 4) or (Image.Height < 4) ) then |
| 679 |
raise ECannotDecompressTexture.CreateFmt('Cannot decompress texture: fglrx (proprietary Radeon drivers on Linux) may awfully crash when one of texture sizes is smaller than 4, and your texture size is %d x %d', |
| 680 |
[Image.Width, Image.Height]); |
| 681 |
|
| 682 |
try |
| 683 |
LoadedSize := 0; |
| 684 |
glCompressedTextureImage2D(Image, 0, LoadedSize); |
| 685 |
TextureMemoryProfiler.Allocate(Tex, Image.Url, Image.ClassName, |
| 686 |
LoadedSize, false, Image.Width, Image.Height, 1); |
| 687 |
except |
| 688 |
{ catch ECannotLoadCompressedTexture and change it to ECannotDecompressTexture } |
| 689 |
on E: ECannotLoadCompressedTexture do |
| 690 |
raise ECannotDecompressTexture.Create('Cannot decompress texture: ' + E.Message); |
| 691 |
end; |
| 692 |
|
| 693 |
if Image.HasAlpha then |
| 694 |
Result := TRGBAlphaImage.Create(Image.Width, Image.Height, Image.Depth) else |
| 695 |
Result := TRGBImage.Create(Image.Width, Image.Height, Image.Depth); |
| 696 |
|
| 697 |
SaveTextureContents(Result, Tex); |
| 698 |
glFreeTexture(Tex); |
| 699 |
end; |
| 700 |
|
| 701 |
{$else} |
| 702 |
begin |
| 703 |
{ glGetTexImage is not available on OpenGLES, so the above trick |
| 704 |
will not work on OpenGLES. TODO: We need more elaborate way to do this |
| 705 |
(namely, we need to simply draw the texture to an uncompressed texture, |
| 706 |
e.g. using TGLRenderToTexture. } |
| 707 |
raise ECannotDecompressTexture.Create('Cannot decompress texture on OpenGL ES'); |
| 708 |
Result := nil; // get rid of warning |
| 709 |
end; |
| 710 |
{$endif} |
| 711 |
|
| 712 |
procedure SaveTextureContents(const Image: TCastleImage; const Texture: TGLTextureId); |
| 713 |
{$ifndef OpenGLES} |
| 714 |
begin |
| 715 |
BeforePackImage(Image); |
| 716 |
try |
| 717 |
glBindTexture(GL_TEXTURE_2D, Texture); |
| 718 |
glGetTexImage(GL_TEXTURE_2D, 0, |
| 719 |
ImageGLFormat(Image), ImageGLType(Image), Image.RawPixels); |
| 720 |
finally AfterPackImage(Image) end; |
| 721 |
end; |
| 722 |
{$else} |
| 723 |
begin |
| 724 |
{ glGetTexImage is not available on OpenGLES } |
| 725 |
raise ECannotSaveTextureContents.Create('Cannot SaveTextureContents on OpenGL ES'); |
| 726 |
end; |
| 727 |
{$endif} |
| 728 |
|
| 729 |
{$endif read_implementation} |