| 1 |
{ |
| 2 |
Copyright 2016-2022 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 |
{ Compressing and downscaling textures. } |
| 17 |
unit ToolTextureGeneration; |
| 18 |
|
| 19 |
{$I castleconf.inc} |
| 20 |
|
| 21 |
interface |
| 22 |
|
| 23 |
uses CastleUtils, CastleStringUtils, |
| 24 |
ToolProject; |
| 25 |
|
| 26 |
procedure AutoGenerateTextures(const Project: TCastleProject); |
| 27 |
procedure AutoGenerateCleanAll(const Project: TCastleProject); |
| 28 |
procedure AutoGenerateCleanUnused(const Project: TCastleProject); |
| 29 |
|
| 30 |
implementation |
| 31 |
|
| 32 |
{ FPC has CPU unit only for certain architectures, see |
| 33 |
$ cd fpcsrc/rtl |
| 34 |
$ find -iname cpu.pp |
| 35 |
} |
| 36 |
{$if defined(CPUi386) or |
| 37 |
defined(CPUx86_64) or |
| 38 |
defined(CPUarm) or |
| 39 |
defined(CPUi8086)} |
| 40 |
{$define HAS_CPU_UNIT} |
| 41 |
{$endif} |
| 42 |
|
| 43 |
uses SysUtils, {$ifdef HAS_CPU_UNIT} Cpu, {$endif} |
| 44 |
CastleUriUtils, CastleInternalMaterialProperties, CastleImages, CastleFilesUtils, |
| 45 |
CastleLog, CastleFindFiles, CastleTimeUtils, |
| 46 |
CastleInternalAutoGenerated, |
| 47 |
ToolCommonUtils, ToolUtils, {SHA1,} MD5; |
| 48 |
|
| 49 |
type |
| 50 |
ECannotFindTool = class(Exception) |
| 51 |
strict private |
| 52 |
FToolName: string; |
| 53 |
public |
| 54 |
constructor Create(const AToolName: string; const C: TTextureCompression); |
| 55 |
property ToolName: string read FToolName; |
| 56 |
end; |
| 57 |
|
| 58 |
constructor ECannotFindTool.Create(const AToolName: string; |
| 59 |
const C: TTextureCompression); |
| 60 |
begin |
| 61 |
FToolName := AToolName; |
| 62 |
inherited CreateFmt('Cannot find tool "%s" necessary to make compressed texture format %s', |
| 63 |
[ToolName, TextureCompressionToString(C)]); |
| 64 |
end; |
| 65 |
|
| 66 |
type |
| 67 |
TStats = record |
| 68 |
Count: Cardinal; |
| 69 |
HashTime: TFloatTime; |
| 70 |
DimensionsCount: Cardinal; |
| 71 |
DimensionsTime: TFloatTime; |
| 72 |
CompressionCount: Cardinal; |
| 73 |
CompressionTime: TFloatTime; |
| 74 |
DownscalingCount: Cardinal; |
| 75 |
DownscalingTime: TFloatTime; |
| 76 |
DxtAutoDetectCount: Cardinal; |
| 77 |
DxtAutoDetectTime: TFloatTime; |
| 78 |
end; |
| 79 |
|
| 80 |
{ Auto-detect best DXTn compression format for this image. } |
| 81 |
function AutoDetectDxt(const ImageUrl: String; var Stats: TStats): TTextureCompression; |
| 82 |
var |
| 83 |
Image: TCastleImage; |
| 84 |
TimeStart: TProcessTimerResult; |
| 85 |
begin |
| 86 |
Inc(Stats.DxtAutoDetectCount); |
| 87 |
TimeStart := ProcessTimer; |
| 88 |
|
| 89 |
Image := LoadImage(ImageUrl); |
| 90 |
try |
| 91 |
case Image.AlphaChannel of |
| 92 |
acNone : Result:= tcDxt1_RGB; |
| 93 |
acTest : Result:= tcDxt1_RGBA; |
| 94 |
acBlending: Result:= tcDxt5; |
| 95 |
{$ifndef COMPILER_CASE_ANALYSIS} |
| 96 |
else raise EInternalError.Create('Unexpected Image.AlphaChannel in AutoDetectDxt'); |
| 97 |
{$endif} |
| 98 |
end; |
| 99 |
finally FreeAndNil(Image) end; |
| 100 |
|
| 101 |
Writeln('Autodetected DXTn type for "' + ImageUrl + '": ' + |
| 102 |
TextureCompressionToString(Result)); |
| 103 |
|
| 104 |
Stats.DxtAutoDetectTime := Stats.DxtAutoDetectTime + TimeStart.ElapsedTime; |
| 105 |
end; |
| 106 |
|
| 107 |
{ Calculate file contents hash. } |
| 108 |
function CalculateHash(const FileUrl: String; var Stats: TStats): string; |
| 109 |
var |
| 110 |
TimeStart: TProcessTimerResult; |
| 111 |
begin |
| 112 |
TimeStart := ProcessTimer; |
| 113 |
//Result := SHA1Print(SHA1File(UriToFilenameSafe(FileUrl))); |
| 114 |
{ MD5 hash is ~2 times faster in my tests, once files are in OS cache. } |
| 115 |
Result := MDPrint(MDFile(UriToFilenameSafe(FileUrl), MD_VERSION_5)); |
| 116 |
Stats.HashTime := Stats.HashTime + TimeStart.ElapsedTime; |
| 117 |
end; |
| 118 |
|
| 119 |
{ Make given URL relative to project's data (fails if not possible). } |
| 120 |
function MakeUrlRelativeToData(const Project: TCastleProject; const Url: String): String; |
| 121 |
var |
| 122 |
DataUrl: String; |
| 123 |
begin |
| 124 |
DataUrl := FilenameToUriSafe(Project.DataPath); |
| 125 |
if not IsPrefix(DataUrl, Url, false) then |
| 126 |
raise Exception.CreateFmt('File (%s) is not within data (%s)', [Url, DataUrl]); |
| 127 |
Result := PrefixRemove(DataUrl, Url, false); |
| 128 |
end; |
| 129 |
|
| 130 |
procedure AutoGenerateTextures(const Project: TCastleProject); |
| 131 |
|
| 132 |
procedure TryToolExe(var ToolExe: string; const ToolExeAbsolutePath: string); |
| 133 |
begin |
| 134 |
if (ToolExe = '') and RegularFileExists(ToolExeAbsolutePath) then |
| 135 |
ToolExe := ToolExeAbsolutePath; |
| 136 |
end; |
| 137 |
|
| 138 |
procedure TryToolExePath(var ToolExe: string; const ToolExeName: string; |
| 139 |
const C: TTextureCompression); |
| 140 |
begin |
| 141 |
if ToolExe = '' then |
| 142 |
begin |
| 143 |
ToolExe := FindExe(ToolExeName); |
| 144 |
if ToolExe = '' then |
| 145 |
raise ECannotFindTool.Create(ToolExeName, C); |
| 146 |
end; |
| 147 |
end; |
| 148 |
|
| 149 |
procedure Compressonator(const InputFile, OutputFile: string; |
| 150 |
const C: TTextureCompression; MipmapsLevel: Cardinal; |
| 151 |
const CompressionNameForTool: String; |
| 152 |
const AstcBlockRate: String = ''); |
| 153 |
var |
| 154 |
ToolExe, InputFlippedFile, OutputTempFile, TempPrefix: string; |
| 155 |
Image: TCastleImage; |
| 156 |
CommandExe: string; |
| 157 |
CommandOptions: TCastleStringList; |
| 158 |
begin |
| 159 |
ToolExe := FindExeCastleTool('CompressonatorCLI'); |
| 160 |
{ otherwise, assume it's on $PATH } |
| 161 |
if ToolExe = '' then |
| 162 |
ToolExe := FindExe('CompressonatorCLI'); |
| 163 |
if ToolExe = '' then |
| 164 |
// on Linux, new released on https://github.com/GPUOpen-Tools/Compressonator/releases have it lowercase |
| 165 |
ToolExe := FindExe('compressonatorcli'); |
| 166 |
if ToolExe = '' then |
| 167 |
raise ECannotFindTool.Create('CompressonatorCLI', C); |
| 168 |
|
| 169 |
TempPrefix := GetTempFileNamePrefix; |
| 170 |
|
| 171 |
InputFlippedFile := TempPrefix + '.png'; |
| 172 |
|
| 173 |
{ In theory, when DDSFlipped = false, we could just do |
| 174 |
CheckCopyFile(InputFile, InputFlippedFile). |
| 175 |
But then AMDCompressCLI fails to read some png files (like flying in dark_dragon). |
| 176 |
TODO: this comment is possibly no longer true as of |
| 177 |
the new (open-source and cross-platform) Compressonator. } |
| 178 |
Image := LoadImage(FilenameToUriSafe(InputFile)); |
| 179 |
try |
| 180 |
if TextureCompressionInfo[C].DDSFlipped then |
| 181 |
Image.FlipVertical; |
| 182 |
SaveImage(Image, FilenameToUriSafe(InputFlippedFile)); |
| 183 |
finally FreeAndNil(Image) end; |
| 184 |
|
| 185 |
{ this is worse, as it requires ImageMagick } |
| 186 |
// RunCommandSimple(FindExe('convert'), [InputFile, '-flip', InputFlippedFile]); |
| 187 |
|
| 188 |
OutputTempFile := TempPrefix + 'output' + ExtractFileExt(OutputFile); |
| 189 |
|
| 190 |
CommandOptions := TCastleStringList.Create; |
| 191 |
try |
| 192 |
CommandExe := ToolExe; |
| 193 |
|
| 194 |
if AstcBlockRate <> '' then |
| 195 |
begin |
| 196 |
CommandOptions.Add('-BlockRate'); |
| 197 |
CommandOptions.Add(AstcBlockRate); |
| 198 |
end; |
| 199 |
|
| 200 |
if MipmapsLevel > 1 then |
| 201 |
begin |
| 202 |
if MipmapsLevel > 20 then |
| 203 |
begin |
| 204 |
WritelnWarning('Compressonator only supports mipmap levels up to 20, capping specified %d to 20', [MipmapsLevel]); |
| 205 |
MipmapsLevel := 20; |
| 206 |
end; |
| 207 |
CommandOptions.AddRange([ |
| 208 |
'-miplevels', |
| 209 |
IntToStr(MipmapsLevel) |
| 210 |
]); |
| 211 |
end else |
| 212 |
begin |
| 213 |
CommandOptions.AddRange([ |
| 214 |
'-nomipmap' |
| 215 |
]); |
| 216 |
end; |
| 217 |
|
| 218 |
CommandOptions.AddRange([ |
| 219 |
'-fd', |
| 220 |
CompressionNameForTool, |
| 221 |
InputFlippedFile, |
| 222 |
OutputTempFile |
| 223 |
]); |
| 224 |
|
| 225 |
{$ifdef UNIX} |
| 226 |
// CompressonatorCLI is just a bash script on Unix |
| 227 |
CommandOptions.Insert(0, CommandExe); |
| 228 |
CommandExe := '/bin/bash'; |
| 229 |
{$endif} |
| 230 |
|
| 231 |
{ TODO: it doesn't seem to help, DXT1_RGBA is still without |
| 232 |
anything useful in alpha value. Seems like AMDCompressCLI bug, |
| 233 |
or I just don't know how to use the DXT1 options? |
| 234 |
TODO: this comment is possibly no longer true as of |
| 235 |
the new (open-source and cross-platform) Compressonator. } |
| 236 |
if C = tcDxt1_RGB then // special options for tcDxt1_RGB |
| 237 |
CommandOptions.AddRange( |
| 238 |
['-DXT1UseAlpha', '1', '-AlphaThreshold', '0.5']); |
| 239 |
|
| 240 |
RunCommandSimple(ExtractFilePath(TempPrefix), |
| 241 |
CommandExe, CommandOptions.ToArray); |
| 242 |
finally FreeAndNil(CommandOptions) end; |
| 243 |
|
| 244 |
CheckRenameFile(OutputTempFile, OutputFile); |
| 245 |
CheckDeleteFile(InputFlippedFile, true); |
| 246 |
end; |
| 247 |
|
| 248 |
procedure PVRTexTool(const InputFile, OutputFile: string; |
| 249 |
const C: TTextureCompression; const MipmapsLevel: Cardinal; |
| 250 |
const CompressionNameForTool: string); |
| 251 |
var |
| 252 |
ToolExe: string; |
| 253 |
begin |
| 254 |
ToolExe := FindExeCastleTool('PVRTexToolCLI'); |
| 255 |
{$ifdef UNIX} |
| 256 |
{ Try the standard installation path on Linux. |
| 257 |
On x86_64, try the 64-bit version first, otherwise fallback on 32-bit. } |
| 258 |
{$ifdef CPU64} |
| 259 |
TryToolExe(ToolExe, '/opt/Imagination/PowerVR_Graphics/PowerVR_Tools/PVRTexTool/CLI/Linux_x86_64/PVRTexToolCLI'); |
| 260 |
{$endif} |
| 261 |
TryToolExe(ToolExe, '/opt/Imagination/PowerVR_Graphics/PowerVR_Tools/PVRTexTool/CLI/Linux_x86_32/PVRTexToolCLI'); |
| 262 |
{$endif} |
| 263 |
{ otherwise, assume it's on $PATH } |
| 264 |
TryToolExePath(ToolExe, 'PVRTexToolCLI', C); |
| 265 |
|
| 266 |
RunCommandSimple(ToolExe, [ |
| 267 |
'-f', CompressionNameForTool, |
| 268 |
'-q', 'pvrtcbest', |
| 269 |
'-m', IntToStr(MipmapsLevel), |
| 270 |
{ On iOS, it seems that PVRTC textures must be square. |
| 271 |
See |
| 272 |
- https://en.wikipedia.org/wiki/PVRTC |
| 273 |
- https://developer.apple.com/library/ios/documentation/3DDrawing/Conceptual/OpenGLES_ProgrammingGuide/TextureTool/TextureTool.html |
| 274 |
But this is only an Apple implementation limitation, not a limitation |
| 275 |
of PVRTC1 compression. |
| 276 |
More info on this compression on |
| 277 |
- http://cdn.imgtec.com/sdk-documentation/PVRTC+%26+Texture+Compression.User+Guide.pdf |
| 278 |
- http://blog.imgtec.com/powervr/pvrtc2-taking-texture-compression-to-a-new-dimension |
| 279 |
|
| 280 |
In practice, forcing texture here to be square is very bad: |
| 281 |
- If a texture is addressed from the top, e.g. in Spine atlas file, |
| 282 |
then it's broken now. So using a texture atlases like 1024x512 from Spine |
| 283 |
would be broken. |
| 284 |
- ... and there's no sensible solution to the above problem. |
| 285 |
We could shift the texture, but then what if something addresses |
| 286 |
it from the bottom? |
| 287 |
- What if something (VRML/X3D or Collada texture coords) addresses |
| 288 |
texture in 0...1 range? |
| 289 |
- To fully work with it, we would have to store original texture |
| 290 |
size somewhere, and it's shift with regards to new compressed texture, |
| 291 |
and support it everywhere where we "interpret" texture coordinates |
| 292 |
(like when reading Spine atlas, or in shaders when sampling |
| 293 |
texture coordinates). Absolutely ugly. |
| 294 |
|
| 295 |
So, don't do this! Allow rectangular PVRTC textures! |
| 296 |
} |
| 297 |
// '-squarecanvas', '+' , |
| 298 |
|
| 299 |
{ TODO: use "-flip y" only when |
| 300 |
- TextureCompressionInfo[C].DDSFlipped (it is false for DXTn) |
| 301 |
- OutputFile is DDS. |
| 302 |
|
| 303 |
If OutputFile is KTX, then always use |
| 304 |
'-flip' 'y,flag' |
| 305 |
which means that file should be ordered bottom-to-top (and marked as such |
| 306 |
in the KTX header). This means it can be read in an efficient way. |
| 307 |
Using KTX requires some improvements to CastleAutoGenerated.xml |
| 308 |
first (to mark that we were able to generate KTX). |
| 309 |
} |
| 310 |
'-flip', 'y', |
| 311 |
|
| 312 |
'-i', InputFile, |
| 313 |
'-o', OutputFile |
| 314 |
]); |
| 315 |
end; |
| 316 |
|
| 317 |
procedure AstcEncTool(const InputFile, OutputFile: String; |
| 318 |
const C: TTextureCompression; const MipmapsLevel: Cardinal; |
| 319 |
const CompressionNameForTool: String; |
| 320 |
const ColorspaceOption: String); |
| 321 |
var |
| 322 |
ToolExe: String; |
| 323 |
ToolName: String; |
| 324 |
begin |
| 325 |
if MipmapsLevel > 1 then |
| 326 |
raise Exception.CreateFmt('astcenc tool doesn''t support generating mipmaps, cannot generate %s->%s with mipmaps level %d', [ |
| 327 |
InputFile, |
| 328 |
OutputFile, |
| 329 |
MipmapsLevel |
| 330 |
]); |
| 331 |
|
| 332 |
ToolName := 'astcenc'; |
| 333 |
{$ifdef CPU64} |
| 334 |
ToolName := 'astcenc-sse2'; // https://github.com/ARM-software/astc-encoder says this will always work on a 64-bit processor |
| 335 |
{$ifdef HAS_CPU_UNIT} |
| 336 |
//ToolName := 'astcenc-none'; // I have no idea what is it |
| 337 |
{$if ((FPC_VERSION = 3) and (FPC_RELEASE >= 3)) or (FPC_VERSION > 3)} |
| 338 |
if SSE41Support and POPCNTSupport then |
| 339 |
ToolName := 'astcenc-sse4.1'; |
| 340 |
if AVX2Support and SSE42Support and POPCNTSupport and F16CSupport then |
| 341 |
ToolName := 'astcenc-avx2'; |
| 342 |
{$endif} |
| 343 |
{$endif} |
| 344 |
{$endif} |
| 345 |
|
| 346 |
ToolExe := FindExeCastleTool(ToolName); |
| 347 |
TryToolExePath(ToolExe, ToolName, C); |
| 348 |
|
| 349 |
RunCommandSimple(ToolExe, |
| 350 |
[ColorspaceOption, |
| 351 |
InputFile, |
| 352 |
OutputFile, |
| 353 |
CompressionNameForTool, |
| 354 |
'-exhaustive', |
| 355 |
'-yflip']); |
| 356 |
end; |
| 357 |
|
| 358 |
procedure NVCompress(const InputFile, OutputFile: string; |
| 359 |
const C: TTextureCompression; const MipmapsLevel: Cardinal; |
| 360 |
const CompressionNameForTool: string); |
| 361 |
var |
| 362 |
ToolExe: string; |
| 363 |
CommandOptions: TCastleStringList; |
| 364 |
begin |
| 365 |
ToolExe := FindExeCastleTool('nvcompress'); |
| 366 |
|
| 367 |
{ assume it's on $PATH } |
| 368 |
TryToolExePath(ToolExe, 'nvcompress', C); |
| 369 |
|
| 370 |
CommandOptions := TCastleStringList.Create; |
| 371 |
try |
| 372 |
CommandOptions.AddRange([ |
| 373 |
'-' + CompressionNameForTool, |
| 374 |
InputFile, |
| 375 |
OutputFile |
| 376 |
]); |
| 377 |
if MipmapsLevel <= 1 then |
| 378 |
begin |
| 379 |
CommandOptions.AddRange(['-nomips']); |
| 380 |
end else |
| 381 |
begin |
| 382 |
WritelnWarning('nvcompress doesn''t allow to select mipmaps level to generate, generating %s->%s with default mipmaps level of nvcompress', [ |
| 383 |
InputFile, |
| 384 |
OutputFile |
| 385 |
]); |
| 386 |
end; |
| 387 |
RunCommandSimple(ToolExe, CommandOptions.ToArray); |
| 388 |
finally FreeAndNil(CommandOptions) end; |
| 389 |
end; |
| 390 |
|
| 391 |
procedure NVCompress_FallbackCompressonator( |
| 392 |
const InputFile, OutputFile: string; |
| 393 |
const C: TTextureCompression; const MipmapsLevel: Cardinal; |
| 394 |
const CompressionNameForNVCompress, |
| 395 |
CompressionNameForCompressonator: string); |
| 396 |
begin |
| 397 |
try |
| 398 |
NVCompress(InputFile, OutputFile, C, MipmapsLevel, CompressionNameForNVCompress); |
| 399 |
Exit; // if there was no ECannotFindTool exception, then success: exit |
| 400 |
except |
| 401 |
on E: ECannotFindTool do |
| 402 |
Writeln('Cannot find nvcompress executable. Falling back to Compressonator.'); |
| 403 |
end; |
| 404 |
|
| 405 |
Compressonator(InputFile, OutputFile, C, MipmapsLevel, CompressionNameForCompressonator); |
| 406 |
end; |
| 407 |
|
| 408 |
{ Convert both URLs to filenames and check whether output should be updated. |
| 409 |
In any case, makes appropriate message to user. |
| 410 |
If the file needs to be updated, makes sure it's output directory exists. } |
| 411 |
function CheckNeedsUpdate(const InputUrl, OutputUrl: String; out InputFile, OutputFile: string; |
| 412 |
const ContentAlreadyProcessed: boolean): boolean; |
| 413 |
begin |
| 414 |
InputFile := UriToFilenameSafe(InputUrl); |
| 415 |
OutputFile := UriToFilenameSafe(OutputUrl); |
| 416 |
|
| 417 |
{ Previously, instead of checking "not ContentAlreadyProcessed", |
| 418 |
we were checking modification times: |
| 419 |
"(FileAge(OutputFile) < FileAge(InputFile))". |
| 420 |
But this was not working perfectly -- updating files from version control |
| 421 |
makes the modification times not-100%-reliable for this. } |
| 422 |
|
| 423 |
Result := (not RegularFileExists(OutputFile)) or (not ContentAlreadyProcessed); |
| 424 |
if Result then |
| 425 |
begin |
| 426 |
Writeln(Format('Updating "%s" from input "%s"', [OutputFile, InputFile])); |
| 427 |
CheckForceDirectories(ExtractFilePath(OutputFile)); |
| 428 |
end else |
| 429 |
begin |
| 430 |
if Verbose then |
| 431 |
Writeln(Format('No need to update "%s"', [OutputFile])); |
| 432 |
end; |
| 433 |
end; |
| 434 |
|
| 435 |
procedure UpdateTextureScale(const InputUrl, OutputUrl: String; |
| 436 |
const Scale: Cardinal; var Stats: TStats; |
| 437 |
const ContentAlreadyProcessed: boolean); |
| 438 |
const |
| 439 |
// equivalent of GLTextureMinSize, but for TextureLoadingScale, not for GLTextureScale |
| 440 |
TextureMinSize = 16; |
| 441 |
var |
| 442 |
InputFile, OutputFile: string; |
| 443 |
Image: TCastleImage; |
| 444 |
NewWidth, NewHeight: Integer; |
| 445 |
NewScale: Cardinal; |
| 446 |
TimeStart: TProcessTimerResult; |
| 447 |
begin |
| 448 |
if CheckNeedsUpdate(InputUrl, OutputUrl, InputFile, OutputFile, ContentAlreadyProcessed) then |
| 449 |
begin |
| 450 |
Inc(Stats.DownscalingCount); |
| 451 |
TimeStart := ProcessTimer; |
| 452 |
|
| 453 |
Image := LoadImage(InputUrl); |
| 454 |
try |
| 455 |
NewWidth := Image.Width; |
| 456 |
NewHeight := Image.Height; |
| 457 |
NewScale := 1; |
| 458 |
while (NewWidth shr 1 >= TextureMinSize) and |
| 459 |
(NewHeight shr 1 >= TextureMinSize) and |
| 460 |
(NewScale < Scale) do |
| 461 |
begin |
| 462 |
NewWidth := NewWidth shr 1; |
| 463 |
NewHeight := NewHeight shr 1; |
| 464 |
Inc(NewScale); |
| 465 |
end; |
| 466 |
if Verbose then |
| 467 |
Writeln(Format('Resizing "%s" from %dx%d to %dx%d', |
| 468 |
[InputUrl, Image.Width, Image.Height, NewWidth, NewHeight])); |
| 469 |
Image.Resize(NewWidth, NewHeight, BestInterpolation); |
| 470 |
SaveImage(Image, OutputUrl); |
| 471 |
finally FreeAndNil(Image) end; |
| 472 |
|
| 473 |
Stats.DownscalingTime := Stats.DownscalingTime + TimeStart.ElapsedTime; |
| 474 |
end; |
| 475 |
end; |
| 476 |
|
| 477 |
{ Like UpdateTextureScale, and also record the output in AutoGeneratedTex.Generated } |
| 478 |
procedure UpdateTextureScaleWhole( |
| 479 |
const AutoGeneratedTex: TAutoGenerated.TTexture; |
| 480 |
const InputUrl, OutputUrl: String; |
| 481 |
const Scale: TScale; var Stats: TStats; |
| 482 |
const ContentAlreadyProcessed: Boolean); |
| 483 |
var |
| 484 |
Generated: TAutoGenerated.TGeneratedTexture; |
| 485 |
begin |
| 486 |
UpdateTextureScale(InputUrl, OutputUrl, Scale.Value, Stats, ContentAlreadyProcessed); |
| 487 |
|
| 488 |
{ Using Low(TTextureCompression), it should not matter when Compression = false. } |
| 489 |
Generated := AutoGeneratedTex.Generated(false, Low(TTextureCompression), Scale.Value); |
| 490 |
Generated.Url := MakeUrlRelativeToData(Project, OutputUrl); |
| 491 |
Generated.Platforms := Scale.Platforms; |
| 492 |
Generated.Used := true; |
| 493 |
end; |
| 494 |
|
| 495 |
procedure UpdateTextureCompress(const InputUrl, OutputUrl: String; |
| 496 |
const C: TTextureCompression; const MipmapsLevel: Cardinal; |
| 497 |
var Stats: TStats; const ContentAlreadyProcessed: boolean); |
| 498 |
var |
| 499 |
InputFile, OutputFile: string; |
| 500 |
TimeStart: TProcessTimerResult; |
| 501 |
begin |
| 502 |
if CheckNeedsUpdate(InputUrl, OutputUrl, InputFile, OutputFile, ContentAlreadyProcessed) then |
| 503 |
begin |
| 504 |
Inc(Stats.CompressionCount); |
| 505 |
TimeStart := ProcessTimer; |
| 506 |
|
| 507 |
case C of |
| 508 |
{ For Compressonator DXT1: |
| 509 |
We have special handling for C = tcDxt1_RGB versus tcDxt1_RGBA, |
| 510 |
they will be handled differently, even though they are both called 'DXT1'. } |
| 511 |
tcDxt1_RGB : NVCompress_FallbackCompressonator(InputFile, OutputFile, C, MipmapsLevel, 'bc1' , 'DXT1'); |
| 512 |
tcDxt1_RGBA: NVCompress_FallbackCompressonator(InputFile, OutputFile, C, MipmapsLevel, 'bc1a', 'DXT1'); |
| 513 |
tcDxt3 : NVCompress_FallbackCompressonator(InputFile, OutputFile, C, MipmapsLevel, 'bc2' , 'DXT3'); |
| 514 |
tcDxt5 : NVCompress_FallbackCompressonator(InputFile, OutputFile, C, MipmapsLevel, 'bc3' , 'DXT5'); |
| 515 |
|
| 516 |
tcATITC_RGB : Compressonator(InputFile, OutputFile, C, MipmapsLevel, 'ATC_RGB' ); |
| 517 |
tcATITC_RGBA_InterpolatedAlpha: Compressonator(InputFile, OutputFile, C, MipmapsLevel, 'ATC_RGBA_Interpolated'); |
| 518 |
tcATITC_RGBA_ExplicitAlpha : Compressonator(InputFile, OutputFile, C, MipmapsLevel, 'ATC_RGBA_Explicit' ); |
| 519 |
|
| 520 |
// format names for PVRTexTool from https://docs.imgtec.com/oxy_ex-2/UtilitiesSrc/PVRTexTool/Documentation/PVRTexTool_Manual/topics/PVRTexTool%20CLI/command_line_options.html |
| 521 |
tcPvrtc1_4bpp_RGB: PVRTexTool(InputFile, OutputFile, C, MipmapsLevel, 'PVRTCI_4BPP_RGB'); |
| 522 |
tcPvrtc1_2bpp_RGB: PVRTexTool(InputFile, OutputFile, C, MipmapsLevel, 'PVRTCI_2BPP_RGB'); |
| 523 |
tcPvrtc1_4bpp_RGBA: PVRTexTool(InputFile, OutputFile, C, MipmapsLevel, 'PVRTCI_4BPP_RGBA'); |
| 524 |
tcPvrtc1_2bpp_RGBA: PVRTexTool(InputFile, OutputFile, C, MipmapsLevel, 'PVRTCI_2BPP_RGBA'); |
| 525 |
tcPvrtc2_4bpp: PVRTexTool(InputFile, OutputFile, C, MipmapsLevel, 'PVRTCII_4BPP'); |
| 526 |
tcPvrtc2_2bpp: PVRTexTool(InputFile, OutputFile, C, MipmapsLevel, 'PVRTCII_2BPP'); |
| 527 |
|
| 528 |
tcETC1: PVRTexTool(InputFile, OutputFile, C, MipmapsLevel, 'ETC1'); |
| 529 |
// or Compressonator(InputFile, OutputFile, C, MipmapsLevel, 'ETC_RGB'); |
| 530 |
|
| 531 |
{$ifdef USE_ASTCENC} |
| 532 |
tcASTC_4x4_RGBA: AstcEncTool(InputFile, OutputFile, C, MipmapsLevel, '4x4', '-cl'); |
| 533 |
tcASTC_5x4_RGBA: AstcEncTool(InputFile, OutputFile, C, MipmapsLevel, '5x4', '-cl'); |
| 534 |
tcASTC_5x5_RGBA: AstcEncTool(InputFile, OutputFile, C, MipmapsLevel, '5x5', '-cl'); |
| 535 |
tcASTC_6x5_RGBA: AstcEncTool(InputFile, OutputFile, C, MipmapsLevel, '6x5', '-cl'); |
| 536 |
tcASTC_6x6_RGBA: AstcEncTool(InputFile, OutputFile, C, MipmapsLevel, '6x6', '-cl'); |
| 537 |
tcASTC_8x5_RGBA: AstcEncTool(InputFile, OutputFile, C, MipmapsLevel, '8x5', '-cl'); |
| 538 |
tcASTC_8x6_RGBA: AstcEncTool(InputFile, OutputFile, C, MipmapsLevel, '8x6', '-cl'); |
| 539 |
tcASTC_8x8_RGBA: AstcEncTool(InputFile, OutputFile, C, MipmapsLevel, '8x8', '-cl'); |
| 540 |
tcASTC_10x5_RGBA: AstcEncTool(InputFile, OutputFile, C, MipmapsLevel, '10x5', '-cl'); |
| 541 |
tcASTC_10x6_RGBA: AstcEncTool(InputFile, OutputFile, C, MipmapsLevel, '10x6', '-cl'); |
| 542 |
tcASTC_10x8_RGBA: AstcEncTool(InputFile, OutputFile, C, MipmapsLevel, '10x8', '-cl'); |
| 543 |
tcASTC_10x10_RGBA: AstcEncTool(InputFile, OutputFile, C, MipmapsLevel, '10x10', '-cl'); |
| 544 |
tcASTC_12x10_RGBA: AstcEncTool(InputFile, OutputFile, C, MipmapsLevel, '12x10', '-cl'); |
| 545 |
tcASTC_12x12_RGBA: AstcEncTool(InputFile, OutputFile, C, MipmapsLevel, '12x12', '-cl'); |
| 546 |
|
| 547 |
tcASTC_4x4_SRGB8_ALPHA8: AstcEncTool(InputFile, OutputFile, C, MipmapsLevel, '4x4', '-ch'); |
| 548 |
tcASTC_5x4_SRGB8_ALPHA8: AstcEncTool(InputFile, OutputFile, C, MipmapsLevel, '5x4', '-ch'); |
| 549 |
tcASTC_5x5_SRGB8_ALPHA8: AstcEncTool(InputFile, OutputFile, C, MipmapsLevel, '5x5', '-ch'); |
| 550 |
tcASTC_6x5_SRGB8_ALPHA8: AstcEncTool(InputFile, OutputFile, C, MipmapsLevel, '6x5', '-ch'); |
| 551 |
tcASTC_6x6_SRGB8_ALPHA8: AstcEncTool(InputFile, OutputFile, C, MipmapsLevel, '6x6', '-ch'); |
| 552 |
tcASTC_8x5_SRGB8_ALPHA8: AstcEncTool(InputFile, OutputFile, C, MipmapsLevel, '8x5', '-ch'); |
| 553 |
tcASTC_8x6_SRGB8_ALPHA8: AstcEncTool(InputFile, OutputFile, C, MipmapsLevel, '8x6', '-ch'); |
| 554 |
tcASTC_8x8_SRGB8_ALPHA8: AstcEncTool(InputFile, OutputFile, C, MipmapsLevel, '8x8', '-ch'); |
| 555 |
tcASTC_10x5_SRGB8_ALPHA8: AstcEncTool(InputFile, OutputFile, C, MipmapsLevel, '10x5', '-ch'); |
| 556 |
tcASTC_10x6_SRGB8_ALPHA8: AstcEncTool(InputFile, OutputFile, C, MipmapsLevel, '10x6', '-ch'); |
| 557 |
tcASTC_10x8_SRGB8_ALPHA8: AstcEncTool(InputFile, OutputFile, C, MipmapsLevel, '10x8', '-ch'); |
| 558 |
tcASTC_10x10_SRGB8_ALPHA8: AstcEncTool(InputFile, OutputFile, C, MipmapsLevel, '10x10', '-ch'); |
| 559 |
tcASTC_12x10_SRGB8_ALPHA8: AstcEncTool(InputFile, OutputFile, C, MipmapsLevel, '12x10', '-ch'); |
| 560 |
tcASTC_12x12_SRGB8_ALPHA8: AstcEncTool(InputFile, OutputFile, C, MipmapsLevel, '12x12', '-ch'); |
| 561 |
|
| 562 |
{$else} |
| 563 |
// tcASTC_4x4_RGBA: PVRTexTool(InputFile, OutputFile, C, MipmapsLevel, 'ASTC_4x4'); |
| 564 |
// tcASTC_5x4_RGBA: PVRTexTool(InputFile, OutputFile, C, MipmapsLevel, 'ASTC_5x4'); |
| 565 |
// tcASTC_5x5_RGBA: PVRTexTool(InputFile, OutputFile, C, MipmapsLevel, 'ASTC_5x5'); |
| 566 |
// tcASTC_6x5_RGBA: PVRTexTool(InputFile, OutputFile, C, MipmapsLevel, 'ASTC_6x5'); |
| 567 |
// tcASTC_6x6_RGBA: PVRTexTool(InputFile, OutputFile, C, MipmapsLevel, 'ASTC_6x6'); |
| 568 |
// tcASTC_8x5_RGBA: PVRTexTool(InputFile, OutputFile, C, MipmapsLevel, 'ASTC_8x5'); |
| 569 |
// tcASTC_8x6_RGBA: PVRTexTool(InputFile, OutputFile, C, MipmapsLevel, 'ASTC_8x6'); |
| 570 |
// tcASTC_8x8_RGBA: PVRTexTool(InputFile, OutputFile, C, MipmapsLevel, 'ASTC_8x8'); |
| 571 |
// tcASTC_10x5_RGBA: PVRTexTool(InputFile, OutputFile, C, MipmapsLevel, 'ASTC_10x5'); |
| 572 |
// tcASTC_10x6_RGBA: PVRTexTool(InputFile, OutputFile, C, MipmapsLevel, 'ASTC_10x6'); |
| 573 |
// tcASTC_10x8_RGBA: PVRTexTool(InputFile, OutputFile, C, MipmapsLevel, 'ASTC_10x8'); |
| 574 |
// tcASTC_10x10_RGBA: PVRTexTool(InputFile, OutputFile, C, MipmapsLevel, 'ASTC_10x10'); |
| 575 |
// tcASTC_12x10_RGBA: PVRTexTool(InputFile, OutputFile, C, MipmapsLevel, 'ASTC_12x10'); |
| 576 |
// tcASTC_12x12_RGBA: PVRTexTool(InputFile, OutputFile, C, MipmapsLevel, 'ASTC_12x12'); |
| 577 |
|
| 578 |
// tcASTC_4x4_SRGB8_ALPHA8: PVRTexTool(InputFile, OutputFile, C, MipmapsLevel, 'ASTC_4x4,UBN,sRGB'); |
| 579 |
// tcASTC_5x4_SRGB8_ALPHA8: PVRTexTool(InputFile, OutputFile, C, MipmapsLevel, 'ASTC_5x4,UBN,sRGB'); |
| 580 |
// tcASTC_5x5_SRGB8_ALPHA8: PVRTexTool(InputFile, OutputFile, C, MipmapsLevel, 'ASTC_5x5,UBN,sRGB'); |
| 581 |
// tcASTC_6x5_SRGB8_ALPHA8: PVRTexTool(InputFile, OutputFile, C, MipmapsLevel, 'ASTC_6x5,UBN,sRGB'); |
| 582 |
// tcASTC_6x6_SRGB8_ALPHA8: PVRTexTool(InputFile, OutputFile, C, MipmapsLevel, 'ASTC_6x6,UBN,sRGB'); |
| 583 |
// tcASTC_8x5_SRGB8_ALPHA8: PVRTexTool(InputFile, OutputFile, C, MipmapsLevel, 'ASTC_8x5,UBN,sRGB'); |
| 584 |
// tcASTC_8x6_SRGB8_ALPHA8: PVRTexTool(InputFile, OutputFile, C, MipmapsLevel, 'ASTC_8x6,UBN,sRGB'); |
| 585 |
// tcASTC_8x8_SRGB8_ALPHA8: PVRTexTool(InputFile, OutputFile, C, MipmapsLevel, 'ASTC_8x8,UBN,sRGB'); |
| 586 |
// tcASTC_10x5_SRGB8_ALPHA8: PVRTexTool(InputFile, OutputFile, C, MipmapsLevel, 'ASTC_10x5,UBN,sRGB'); |
| 587 |
// tcASTC_10x6_SRGB8_ALPHA8: PVRTexTool(InputFile, OutputFile, C, MipmapsLevel, 'ASTC_10x6,UBN,sRGB'); |
| 588 |
// tcASTC_10x8_SRGB8_ALPHA8: PVRTexTool(InputFile, OutputFile, C, MipmapsLevel, 'ASTC_10x8,UBN,sRGB'); |
| 589 |
// tcASTC_10x10_SRGB8_ALPHA8: PVRTexTool(InputFile, OutputFile, C, MipmapsLevel, 'ASTC_10x10,UBN,sRGB'); |
| 590 |
// tcASTC_12x10_SRGB8_ALPHA8: PVRTexTool(InputFile, OutputFile, C, MipmapsLevel, 'ASTC_12x10,UBN,sRGB'); |
| 591 |
// tcASTC_12x12_SRGB8_ALPHA8: PVRTexTool(InputFile, OutputFile, C, MipmapsLevel, 'ASTC_12x12,UBN,sRGB'); |
| 592 |
|
| 593 |
tcASTC_4x4_RGBA: Compressonator(InputFile, OutputFile, C, MipmapsLevel, 'ASTC', '4x4'); |
| 594 |
tcASTC_5x4_RGBA: Compressonator(InputFile, OutputFile, C, MipmapsLevel, 'ASTC', '5x4'); |
| 595 |
tcASTC_5x5_RGBA: Compressonator(InputFile, OutputFile, C, MipmapsLevel, 'ASTC', '5x5'); |
| 596 |
tcASTC_6x5_RGBA: Compressonator(InputFile, OutputFile, C, MipmapsLevel, 'ASTC', '6x5'); |
| 597 |
tcASTC_6x6_RGBA: Compressonator(InputFile, OutputFile, C, MipmapsLevel, 'ASTC', '6x6'); |
| 598 |
tcASTC_8x5_RGBA: Compressonator(InputFile, OutputFile, C, MipmapsLevel, 'ASTC', '8x5'); |
| 599 |
tcASTC_8x6_RGBA: Compressonator(InputFile, OutputFile, C, MipmapsLevel, 'ASTC', '8x6'); |
| 600 |
tcASTC_8x8_RGBA: Compressonator(InputFile, OutputFile, C, MipmapsLevel, 'ASTC', '8x8'); |
| 601 |
tcASTC_10x5_RGBA: Compressonator(InputFile, OutputFile, C, MipmapsLevel, 'ASTC', '10x5'); |
| 602 |
tcASTC_10x6_RGBA: Compressonator(InputFile, OutputFile, C, MipmapsLevel, 'ASTC', '10x6'); |
| 603 |
tcASTC_10x8_RGBA: Compressonator(InputFile, OutputFile, C, MipmapsLevel, 'ASTC', '10x8'); |
| 604 |
tcASTC_10x10_RGBA: Compressonator(InputFile, OutputFile, C, MipmapsLevel, 'ASTC', '10x10'); |
| 605 |
tcASTC_12x10_RGBA: Compressonator(InputFile, OutputFile, C, MipmapsLevel, 'ASTC', '12x10'); |
| 606 |
tcASTC_12x12_RGBA: Compressonator(InputFile, OutputFile, C, MipmapsLevel, 'ASTC', '12x12'); |
| 607 |
|
| 608 |
// TODO: pass here special option for SRGB8_ALPHA8 |
| 609 |
tcASTC_4x4_SRGB8_ALPHA8: Compressonator(InputFile, OutputFile, C, MipmapsLevel, 'ASTC', '4x4'); |
| 610 |
tcASTC_5x4_SRGB8_ALPHA8: Compressonator(InputFile, OutputFile, C, MipmapsLevel, 'ASTC', '5x4'); |
| 611 |
tcASTC_5x5_SRGB8_ALPHA8: Compressonator(InputFile, OutputFile, C, MipmapsLevel, 'ASTC', '5x5'); |
| 612 |
tcASTC_6x5_SRGB8_ALPHA8: Compressonator(InputFile, OutputFile, C, MipmapsLevel, 'ASTC', '6x5'); |
| 613 |
tcASTC_6x6_SRGB8_ALPHA8: Compressonator(InputFile, OutputFile, C, MipmapsLevel, 'ASTC', '6x6'); |
| 614 |
tcASTC_8x5_SRGB8_ALPHA8: Compressonator(InputFile, OutputFile, C, MipmapsLevel, 'ASTC', '8x5'); |
| 615 |
tcASTC_8x6_SRGB8_ALPHA8: Compressonator(InputFile, OutputFile, C, MipmapsLevel, 'ASTC', '8x6'); |
| 616 |
tcASTC_8x8_SRGB8_ALPHA8: Compressonator(InputFile, OutputFile, C, MipmapsLevel, 'ASTC', '8x8'); |
| 617 |
tcASTC_10x5_SRGB8_ALPHA8: Compressonator(InputFile, OutputFile, C, MipmapsLevel, 'ASTC', '10x5'); |
| 618 |
tcASTC_10x6_SRGB8_ALPHA8: Compressonator(InputFile, OutputFile, C, MipmapsLevel, 'ASTC', '10x6'); |
| 619 |
tcASTC_10x8_SRGB8_ALPHA8: Compressonator(InputFile, OutputFile, C, MipmapsLevel, 'ASTC', '10x8'); |
| 620 |
tcASTC_10x10_SRGB8_ALPHA8: Compressonator(InputFile, OutputFile, C, MipmapsLevel, 'ASTC', '10x10'); |
| 621 |
tcASTC_12x10_SRGB8_ALPHA8: Compressonator(InputFile, OutputFile, C, MipmapsLevel, 'ASTC', '12x10'); |
| 622 |
tcASTC_12x12_SRGB8_ALPHA8: Compressonator(InputFile, OutputFile, C, MipmapsLevel, 'ASTC', '12x12'); |
| 623 |
|
| 624 |
{$endif} |
| 625 |
|
| 626 |
{$ifndef COMPILER_CASE_ANALYSIS} |
| 627 |
else WritelnWarning('GPUCompression', Format('Compressing to GPU format %s not implemented (to update "%s")', |
| 628 |
[TextureCompressionToString(C), OutputFile])); |
| 629 |
{$endif} |
| 630 |
end; |
| 631 |
|
| 632 |
Stats.CompressionTime := Stats.CompressionTime + TimeStart.ElapsedTime; |
| 633 |
end; |
| 634 |
end; |
| 635 |
|
| 636 |
{ Read texture sizes, place them in TAutoGenerated.TTexture fields. } |
| 637 |
procedure UpdateTextureDimensions(const AutoGeneratedTex: TAutoGenerated.TTexture; |
| 638 |
const TextureUrl: String; var Stats: TStats); |
| 639 |
var |
| 640 |
Image: TCastleImage; |
| 641 |
TimeStart: TProcessTimerResult; |
| 642 |
begin |
| 643 |
Inc(Stats.DimensionsCount); |
| 644 |
TimeStart := ProcessTimer; |
| 645 |
|
| 646 |
Image := LoadImage(TextureUrl); |
| 647 |
try |
| 648 |
AutoGeneratedTex.Width := Image.Width; |
| 649 |
AutoGeneratedTex.Height := Image.Height; |
| 650 |
AutoGeneratedTex.Depth := Image.Depth; |
| 651 |
finally FreeAndNil(Image) end; |
| 652 |
|
| 653 |
Stats.DimensionsTime := Stats.DimensionsTime + TimeStart.ElapsedTime; |
| 654 |
end; |
| 655 |
|
| 656 |
{ Like UpdateTextureCompress, and also record the output in AutoGeneratedTex.Generated } |
| 657 |
procedure UpdateTextureCompressWhole(const AutoGeneratedTexInfo: TAutoGeneratedTextures; |
| 658 |
const AutoGeneratedTex: TAutoGenerated.TTexture; |
| 659 |
const C: TTextureCompression; |
| 660 |
const OriginalTextureUrl, UncompressedUrl: String; |
| 661 |
const Scale: Cardinal; |
| 662 |
var Stats: TStats; |
| 663 |
const ContentAlreadyProcessed: Boolean; |
| 664 |
const Platforms: TCastlePlatforms); |
| 665 |
var |
| 666 |
CompressedUrl: String; |
| 667 |
Generated: TAutoGenerated.TGeneratedTexture; |
| 668 |
begin |
| 669 |
if (not TextureCompressionInfo[C].DDSFlipped) and |
| 670 |
(TextureCompressionInfo[C].FileExtension = '.dds') and |
| 671 |
(AutoGeneratedTex.Height > 4) and |
| 672 |
(AutoGeneratedTex.Height mod 4 <> 0) then |
| 673 |
WritelnWarning('Generating DDS from "%s" with size %dx%d with GPU compression "%s". It will not be read correctly, as we cannot flip it back at loading (and it is expected for this compression format), because the height is not a multiple of 4 (or 1,2,3). We advise to exclude this file from compression.', [ |
| 674 |
OriginalTextureUrl, |
| 675 |
AutoGeneratedTex.Width, |
| 676 |
AutoGeneratedTex.Height, |
| 677 |
TextureCompressionInfo[C].Name |
| 678 |
]); |
| 679 |
|
| 680 |
CompressedUrl := AutoGeneratedTexInfo.GeneratedTextureUrl(OriginalTextureUrl, true, C, Scale); |
| 681 |
{ We use the UncompressedUrl that was updated previously. |
| 682 |
This way there's no need to scale the texture here. } |
| 683 |
UpdateTextureCompress(UncompressedUrl, CompressedUrl, C, |
| 684 |
AutoGeneratedTexInfo.MipmapsLevel, Stats, ContentAlreadyProcessed); |
| 685 |
|
| 686 |
Generated := AutoGeneratedTex.Generated(true, C, Scale); |
| 687 |
Generated.Url := MakeUrlRelativeToData(Project, CompressedUrl); |
| 688 |
Generated.Platforms := Platforms; |
| 689 |
Generated.Used := true; |
| 690 |
end; |
| 691 |
|
| 692 |
procedure UpdateTexture( |
| 693 |
const OriginalTextureUrl: String; |
| 694 |
{ Determines how the texture should be processed (comes from material_properties.xml) } |
| 695 |
const AutoGeneratedTexInfo: TAutoGeneratedTextures; |
| 696 |
var Stats: TStats; |
| 697 |
{ Determines how were the textures processed now (comes from CastleAutoGenerated.xml) } |
| 698 |
const AutoGenerated: TAutoGenerated); |
| 699 |
var |
| 700 |
UncompressedUrl: String; |
| 701 |
C: TTextureCompression; |
| 702 |
Scale: TScale; |
| 703 |
ToGenerate: TTextureCompressionsToGenerate; |
| 704 |
Compressions: TCompressionsMap; |
| 705 |
CompressionPair: TCompressionsMap.TDictionaryPair; |
| 706 |
RelativeOriginalTextureUrl, Hash: String; |
| 707 |
ContentAlreadyProcessed: Boolean; |
| 708 |
{ Determines how was the texture processed now (comes from CastleAutoGenerated.xml) } |
| 709 |
AutoGeneratedTex: TAutoGenerated.TTexture; |
| 710 |
MipmapsLevel: Cardinal; |
| 711 |
begin |
| 712 |
Inc(Stats.Count); |
| 713 |
|
| 714 |
Hash := CalculateHash(OriginalTextureUrl, Stats); |
| 715 |
|
| 716 |
// calculate and compare Hash with AutoGenerated |
| 717 |
RelativeOriginalTextureUrl := MakeUrlRelativeToData(Project, OriginalTextureUrl); |
| 718 |
AutoGeneratedTex := AutoGenerated.Texture(RelativeOriginalTextureUrl, |
| 719 |
AutoGeneratedTexInfo.OriginalPlatforms, true); |
| 720 |
AutoGeneratedTex.Used := true; |
| 721 |
MipmapsLevel := AutoGeneratedTexInfo.MipmapsLevel; |
| 722 |
ContentAlreadyProcessed := |
| 723 |
(AutoGeneratedTex.Hash = Hash) and |
| 724 |
(AutoGeneratedTex.MipmapsLevel = MipmapsLevel); |
| 725 |
|
| 726 |
{ We could just Exit now if ContentAlreadyProcessed. |
| 727 |
But it's safer to continue, and check do the indicated (generated) files exist. |
| 728 |
If not, we will generate them, even when hash was already OK. } |
| 729 |
|
| 730 |
if (not ContentAlreadyProcessed) or |
| 731 |
{ old filed before we added dimensions } |
| 732 |
( (AutoGeneratedTex.Width = 0) and |
| 733 |
(AutoGeneratedTex.Height = 0) and |
| 734 |
(AutoGeneratedTex.Depth = 0) ) then |
| 735 |
begin |
| 736 |
// store new Hash in AutoGenerated |
| 737 |
AutoGeneratedTex.Hash := Hash; |
| 738 |
UpdateTextureDimensions(AutoGeneratedTex, OriginalTextureUrl, Stats); |
| 739 |
end; |
| 740 |
|
| 741 |
for Scale in AutoGeneratedTexInfo.Scales do |
| 742 |
begin |
| 743 |
if (Scale.Value <> 1) or AutoGeneratedTexInfo.TrivialUncompressedConvert then |
| 744 |
begin |
| 745 |
UncompressedUrl := AutoGeneratedTexInfo.GeneratedTextureUrl(OriginalTextureUrl, false, Low(TTextureCompression), Scale.Value); |
| 746 |
UpdateTextureScaleWhole(AutoGeneratedTex, OriginalTextureUrl, UncompressedUrl, Scale, Stats, ContentAlreadyProcessed); |
| 747 |
end else |
| 748 |
UncompressedUrl := OriginalTextureUrl; |
| 749 |
|
| 750 |
ToGenerate := AutoGeneratedTexInfo.CompressedFormatsToGenerate; |
| 751 |
if ToGenerate <> nil then |
| 752 |
begin |
| 753 |
Compressions := ToGenerate.Compressions; |
| 754 |
|
| 755 |
{ TODO: we perform AutoDetectDxt call every time. |
| 756 |
Instead, information in auto_generated.xml could allow us to avoid it. } |
| 757 |
if ToGenerate.DxtAutoDetect then |
| 758 |
if ToGenerate.DxtAutoDetectPlatforms * Scale.Platforms <> [] then |
| 759 |
begin |
| 760 |
C := AutoDetectDxt(OriginalTextureUrl, Stats); |
| 761 |
UpdateTextureCompressWhole(AutoGeneratedTexInfo, AutoGeneratedTex, C, OriginalTextureUrl, UncompressedUrl, Scale.Value, Stats, ContentAlreadyProcessed, |
| 762 |
ToGenerate.DxtAutoDetectPlatforms * Scale.Platforms); |
| 763 |
end; |
| 764 |
|
| 765 |
for CompressionPair in Compressions do |
| 766 |
if CompressionPair.Value * Scale.Platforms <> [] then |
| 767 |
begin |
| 768 |
C := CompressionPair.Key; |
| 769 |
UpdateTextureCompressWhole(AutoGeneratedTexInfo, AutoGeneratedTex, C, OriginalTextureUrl, UncompressedUrl, Scale.Value, Stats, ContentAlreadyProcessed, |
| 770 |
CompressionPair.Value * Scale.Platforms); |
| 771 |
end; |
| 772 |
end; |
| 773 |
end; |
| 774 |
end; |
| 775 |
|
| 776 |
var |
| 777 |
Textures: TCastleStringList; |
| 778 |
I: Integer; |
| 779 |
AutoGeneratedUrl, MatPropsUrl: String; |
| 780 |
MatProps: TMaterialProperties; |
| 781 |
Stats: TStats; |
| 782 |
AutoGenerated: TAutoGenerated; |
| 783 |
begin |
| 784 |
if not Project.DataExists then |
| 785 |
begin |
| 786 |
WritelnVerbose('Material properties file does not exist in data (because <data exists="false"/> in CastleEngineManifest.xml), so not compressing anything.'); |
| 787 |
Exit; |
| 788 |
end; |
| 789 |
|
| 790 |
MatPropsUrl := FilenameToUriSafe(Project.DataPath + 'material_properties.xml'); |
| 791 |
if not URIFileExists(MatPropsUrl) then |
| 792 |
begin |
| 793 |
WritelnVerbose('Material properties file does not exist, so not compressing anything: ' + MatPropsUrl); |
| 794 |
Exit; |
| 795 |
end; |
| 796 |
|
| 797 |
FillChar(Stats, SizeOf(Stats), 0); |
| 798 |
|
| 799 |
AutoGeneratedUrl := FilenameToUriSafe(Project.DataPath + TAutoGenerated.FileName); |
| 800 |
AutoGenerated := TAutoGenerated.Create; |
| 801 |
try |
| 802 |
AutoGenerated.LoadFromFile(AutoGeneratedUrl); |
| 803 |
|
| 804 |
MatProps := TMaterialProperties.Create(false); |
| 805 |
try |
| 806 |
MatProps.Url := MatPropsUrl; |
| 807 |
Textures := MatProps.AutoGeneratedTextures; |
| 808 |
try |
| 809 |
for I := 0 to Textures.Count - 1 do |
| 810 |
UpdateTexture(Textures[I], Textures.Objects[I] as TAutoGeneratedTextures, |
| 811 |
Stats, AutoGenerated); |
| 812 |
finally FreeAndNil(Textures) end; |
| 813 |
finally FreeAndNil(MatProps) end; |
| 814 |
|
| 815 |
AutoGenerated.CleanUnused(true); |
| 816 |
AutoGenerated.CleanNotExistingFiles(FilenameToUriSafe(Project.DataPath), true); |
| 817 |
AutoGenerated.SaveToFile(AutoGeneratedUrl); |
| 818 |
finally FreeAndNil(AutoGenerated) end; |
| 819 |
|
| 820 |
Write(Format( |
| 821 |
'Automatic texture generation completed:' + NL + |
| 822 |
' %d textures considered to be compressed and/or downscaled.' + NL + |
| 823 |
' Hashes calculation time: %f seconds.' + NL + |
| 824 |
' Dimensions calculations: %d in %f seconds.' + NL + |
| 825 |
' Compressions done: %d in %f seconds.' + NL + |
| 826 |
' Downscaling done: %d in %f seconds.' + NL + |
| 827 |
' DXTn auto-detection done: %d in %f seconds.' + NL, [ |
| 828 |
Stats.Count, |
| 829 |
Stats.HashTime, |
| 830 |
Stats.DimensionsCount, |
| 831 |
Stats.DimensionsTime, |
| 832 |
Stats.CompressionCount, |
| 833 |
Stats.CompressionTime, |
| 834 |
Stats.DownscalingCount, |
| 835 |
Stats.DownscalingTime, |
| 836 |
Stats.DxtAutoDetectCount, |
| 837 |
Stats.DxtAutoDetectTime |
| 838 |
])); |
| 839 |
end; |
| 840 |
|
| 841 |
procedure CleanDir(const FileInfo: TFileInfo; Data: Pointer; |
| 842 |
var StopSearch: boolean); |
| 843 |
begin |
| 844 |
Writeln('Deleting ', FileInfo.AbsoluteName); |
| 845 |
RemoveNonEmptyDir(FileInfo.AbsoluteName); |
| 846 |
end; |
| 847 |
|
| 848 |
procedure AutoGenerateCleanAll(const Project: TCastleProject); |
| 849 |
|
| 850 |
procedure TryDeleteFile(const FileName: string); |
| 851 |
begin |
| 852 |
if RegularFileExists(FileName) then |
| 853 |
begin |
| 854 |
Writeln('Deleting ' + FileName); |
| 855 |
CheckDeleteFile(FileName); |
| 856 |
end; |
| 857 |
end; |
| 858 |
|
| 859 |
begin |
| 860 |
if Project.DataExists then |
| 861 |
begin |
| 862 |
FindFiles(Project.DataPath, TAutoGenerated.AutoGeneratedDirName, true, |
| 863 |
@CleanDir, nil, [ffRecursive]); |
| 864 |
|
| 865 |
TryDeleteFile(Project.DataPath + TAutoGenerated.FileName); |
| 866 |
end; |
| 867 |
end; |
| 868 |
|
| 869 |
type |
| 870 |
TAutoGeneratedCleanUnusedHandler = class |
| 871 |
Project: TCastleProject; |
| 872 |
AutoGenerated: TAutoGenerated; |
| 873 |
destructor Destroy; override; |
| 874 |
procedure FindFilesCallback(const FileInfo: TFileInfo; var StopSearch: boolean); |
| 875 |
end; |
| 876 |
|
| 877 |
procedure TAutoGeneratedCleanUnusedHandler.FindFilesCallback( |
| 878 |
const FileInfo: TFileInfo; var StopSearch: boolean); |
| 879 |
var |
| 880 |
UrlInData: String; |
| 881 |
begin |
| 882 |
UrlInData := ExtractRelativePath(Project.DataPath, FileInfo.AbsoluteName); |
| 883 |
UrlInData := StringReplace(UrlInData, '\', '/', [rfReplaceAll]); |
| 884 |
|
| 885 |
if (Pos('/' + TAutoGenerated.AutoGeneratedDirName + '/', UrlInData) <> 0) and |
| 886 |
(not AutoGenerated.Used(UrlInData)) then |
| 887 |
begin |
| 888 |
WritelnVerbose('Deleting unused ' + FileInfo.AbsoluteName); |
| 889 |
CheckDeleteFile(FileInfo.AbsoluteName, true); |
| 890 |
end; |
| 891 |
end; |
| 892 |
|
| 893 |
destructor TAutoGeneratedCleanUnusedHandler.Destroy; |
| 894 |
begin |
| 895 |
FreeAndNil(AutoGenerated); |
| 896 |
inherited; |
| 897 |
end; |
| 898 |
|
| 899 |
procedure AutoGenerateCleanUnused(const Project: TCastleProject); |
| 900 |
var |
| 901 |
AutoGeneratedUrl: String; |
| 902 |
Handler: TAutoGeneratedCleanUnusedHandler; |
| 903 |
begin |
| 904 |
if Project.DataExists then |
| 905 |
begin |
| 906 |
Handler := TAutoGeneratedCleanUnusedHandler.Create; |
| 907 |
try |
| 908 |
Handler.Project := Project; |
| 909 |
|
| 910 |
AutoGeneratedUrl := FilenameToUriSafe(Project.DataPath + TAutoGenerated.FileName); |
| 911 |
Handler.AutoGenerated := TAutoGenerated.Create; |
| 912 |
Handler.AutoGenerated.LoadFromFile(AutoGeneratedUrl); |
| 913 |
|
| 914 |
FindFiles(Project.DataPath, '*', false, @Handler.FindFilesCallback, [ffRecursive]); |
| 915 |
finally FreeAndNil(Handler) end; |
| 916 |
end; |
| 917 |
end; |
| 918 |
|
| 919 |
end. |