Files
UnrealEngineUWP/Engine/Source/Developer/TextureFormatPVR/Private/TextureFormatPVR.cpp
Chris Babcock dc647b9547 Copying //UE4/Dev-Mobile to //UE4/Main (Source: //UE4/Dev-Mobile @ 3600060)
#rb none
#lockdown nick.penwarden

==========================
MAJOR FEATURES + CHANGES
==========================

Change 3292215 on 2017/02/08 by Nick.Shin

	HTML5 emscripten: wasm and wbegl2 support

	- emscripten toolchain

	#jira UEPLAT-1437  Switch [to] web assembly
	#rb none

Change 3293994 on 2017/02/09 by Nick.Shin

	HTML5 emscripten: wasm and webgl2 support

	- OSX toolchain

	#jira UEPLAT-1437  Switch [to] web assembly
	#rb none

Change 3317951 on 2017/02/22 by Nick.Shin

	HTML5 emscripten: wasm & webgl2 support - RC1

	- emscripten toolchain

	WARNING: emscripten/incoming/source/include/libc/bit (the file) might need to be deleted first

	#jira UEMOB-263  Switch [to] web assembly
	#jira UEMOB-201  Support ES3 / WebGL2 in HTML5
	#rb none

Change 3318669 on 2017/02/23 by Nick.Shin

	HTML5 emscripten: wasm & webgl2 support - RC1

	- OSX toolchain

	#jira UEMOB-263  Switch [to] web assembly
	#jira UEMOB-201  Support ES3 / WebGL2 in HTML5
	#rb none

Change 3462146 on 2017/05/26 by Nick.Shin

	HTML5 - merge from Release-4.16 to Dev-Mobile

	#jira none
	#rb none
	#rnx

Change 3504996 on 2017/06/22 by Cosmin.Sulea

	UEMOB-362 - Add per-texture and per-format compression quality override settings
	#rb Dmitriy.Dyomin
	#jira UEMOB-362
	#codereview Dmitriy.Dyomin
	#codereview Jack.Porter

Change 3505056 on 2017/06/22 by Cosmin.Sulea

	Back out changelist 3504996 - due to errors generated in xboxOne, PS4 and Switch versions
	#rb none

Change 3508049 on 2017/06/23 by Nick.Shin

	HTML5 toolchain notes corrections

	#jira none
	#rb none
	#rnx

Change 3508663 on 2017/06/24 by Nick.Shin

	HTML5LaunchHelper.exe on linux - redo

	- it seems that i need to also check-in the exe and pdb file instead of having CIS make and checking-in them itself...
	- modified c# program to output a version number to help track which version of HTML5LaunchHelper is running...

	#jira UE-45302  HTML5LaunchHelper.exe hosts the files in the current working directory on Linux
	#rnx
	#rb none

Change 3509210 on 2017/06/26 by Dmitriy.Dyomin

	 ExposureScale will be applied during tonemap pass when MobileHDR is on
	#rb jack.porter
	#codereview Allan.Bentham

Change 3511058 on 2017/06/27 by Cosmin.Sulea

	UEMOB-362 - Add per-texture and per-format compression quality override settings - resubmitted
	#rb Dmitriy.Dyomin
	#jira UEMOB-362
	#codereview Dmitriy.Dyomin

Change 3511069 on 2017/06/27 by Jack.Porter

	PS4, XboxOne and Switch fixes for changes to ITextureFormat interface
	#rb Dmitriy.Dyomin
	#jira UEMOB-362

Change 3513028 on 2017/06/28 by Jack.Porter

	Merging //UE4/Dev-Main to Dev-Mobile (//UE4/Dev-Mobile)
	#rb None

Change 3517409 on 2017/06/30 by Jack.Porter

	Merging //UE4/Dev-Main to Dev-Mobile (//UE4/Dev-Mobile)
	#rb None

Change 3517730 on 2017/06/30 by Cosmin.Sulea

	UEMOB-328 - Improve handling of iOS signing key on remote Mac system keychain when using remote toolchain
	#rb Jack.Porter
	#jira UEMOB-328
	#codereview: peter.sauerbrei

Change 3517757 on 2017/06/30 by Cosmin.Sulea

	UE-46245 - Building with remote toolchain does not use Project Setting for iOS signing identity which can cause signing errors
	#rb Jack.Porter
	#jira UE-46245
	#codereview: peter.sauerbrei

Change 3518149 on 2017/06/30 by Adrian.Chelu

	UE-43035 Tilt axis for X and Z are not consistent between Android and iOS devices
	#rb Jack.Porter
	#jira UE-46245
	#codereview: Chris Babcock <chris.babcock@epicgames.com>

Change 3524242 on 2017/07/06 by Nick.Shin

	HTML5 - refraction shader

	note: this CL also contains fixes to webgl2 [float4 vs half2] and a [% vs Mod()] material custom function changes to some TM-ShaderModels shaders
	specifically: fixes to and similar with: DitherTemporalAA

	#jria UE-46434  No Refraction in QA Game TM-Shadermodels HTML5
	#rb none
	#rn
	#codereview jack.porter dmitriy.dyomin

Change 3535295 on 2017/07/13 by Allan.Bentham

	#jira UEMOB-390
	Add Android cpu stats.
	add 'stat AndroidCPU' to android's console spinner UI.
	increase GetCPUState's core count support to 16.
	#jira UE-45888
	Use cvar value to limit android cpu stat update rate.
	#rb none

Change 3535306 on 2017/07/13 by Allan.Bentham

	Add missing pragma once
	#rb none

Change 3537047 on 2017/07/13 by Ben.Marsh

	Fixing case of iOS directories, pt1

	#rb none

Change 3537051 on 2017/07/13 by Ben.Marsh

	Fixing case of iOS directories, pt2

	#rb none

Change 3537373 on 2017/07/14 by Allan.Bentham

	Add scope level android egl error verification.
	work around minor issue with invalid egl config property.
	#rb chris.babcock

Change 3541735 on 2017/07/18 by Allan.Bentham

	Add 'sustained performance mode' support for API 24+ devices.
	#jira UEMOB-386
	#rb chris.babcock

Change 3543001 on 2017/07/18 by Sorin.Gradinaru

	#jira UE-45766 Improved Virtual Keyboard cannot receive non-English characters.

	- for Android, add an native EditBox above the virtual keyboard to receive the text and pass it to the object from the slate

	#rb Chris.Babcock

Change 3554399 on 2017/07/25 by Nick.Shin

	STATS disabled for non multi-threaded platforms

	#jira UE-47485  ( Pri:1 - 4.18 )  Crash running Stat Command test in TM-Core on Firefox
	#rnx
	#rb none

Change 3554402 on 2017/07/25 by Nick.Shin

	STATS TaskGraph disabled for non multi-threaded platforms

	#jira UE-47486  ( Pri:1 - 4.18 )  QAGame hard locks on Firefox when triggering Task Graph Benchmark test
	#rb none
	#rnx

Change 3556957 on 2017/07/26 by Nick.Shin

	HTML5 - WASM enabled by default - part 1 -- commenting out asmjs stuff

	begin sunsetting ASM.JS

	note to self: CL#3462146 "backout" asmjs

	#jira UEMOB-416  WASM enabled by default
	#rnx
	#rb none

Change 3557654 on 2017/07/26 by Nick.Shin

	HTML5 - WASM enabled by default - part 2 -- remove asmjs code

	sunsetting ASM.JS

	note to self: CL#3462146 "backout" asmjs

	#jira UEMOB-416  WASM enabled by default
	#rn
	#rb none

Change 3557910 on 2017/07/27 by Jack.Porter

	Support Client configuration when packaging in the editor
	#jira UE-39973
	#rb Dmitriy.Dyomin

Change 3557917 on 2017/07/27 by Jack.Porter

	Missing file from CL 3557910
	#rb trivial

Change 3559642 on 2017/07/27 by Nick.Shin

	STATS TaskGraph disabled for non multi-threaded platforms

	- both "LockFree stress test" and "task graph benchmark" are disabled - no multi-threading for WASM exist yet (note: ASM.JS has been sunsetted)

	- stat command crash "fixed" - but, font size are totally broken - i can look at this (much) later...

	- new bug: physx will crash on "gc and level load stress test" -- please bug this as a new jira

	#jira UE-47486  ( Pri:1 - 4.18 )  QAGame hard locks on Firefox when triggering Task Graph Benchmark test
	#rb none
	#rnx

Change 3565656 on 2017/07/31 by Dmitriy.Dyomin

	Added a way to lock level position in Word Composition
	#jira UE-47713
	#rb none

Change 3565757 on 2017/08/01 by Dmitriy.Dyomin

	compile fix
	#rb none

Change 3567446 on 2017/08/01 by Chris.Babcock

	Allow addElement and addElements to only insert once with once="true" attribute in UPL
	#jira UE-47951
	#ue4
	#android
	#rb Peter.Sauerbrei

Change 3567592 on 2017/08/01 by Chris.Babcock

	Use absolute path for repositories for Gradle
	#jira UE-47952
	#ue4
	#android
	#rb Tim.Lincoln

Change 3568690 on 2017/08/02 by Chris.Babcock

	Removed warnings for once attribute in UPL
	#ue4
	#android
	#rb none

Change 3569975 on 2017/08/02 by Chris.Babcock

	Add <baseBuildGradleAdditions> to UPL to allow additions to the root-level build.gradle
	#jira UE-47995
	#ue4
	#android
	#rb Tim.Lincoln

Change 3570117 on 2017/08/02 by Chris.Babcock

	Add <setBoolFromPropertyContains> to UPL
	- sets bool to true if string list in ini matches contains attribute
	#jira UE-47996
	#ue4
	#android
	#rb Jack.Porter

Change 3571552 on 2017/08/03 by Chris.Babcock

	Removed unneeded settings.gradle file (generated)
	#jira UE-48041
	#ue4
	#android
	#rb none

Change 3572224 on 2017/08/04 by Dmitriy.Dyomin

	Better selection tracking in world composition
	#rb none

Change 3573662 on 2017/08/04 by Nick.Shin

	HTML5 remove PreLoadMap "feature" (was only available/used with HTML5)

	- asyncronous loads are not allowed during UEngine::LoadMap()
	- the files/code will be repurposed for pakfile CHUNK support

	#jira UEMOB-425  HTML5 streaming content investigation (part 1 of 2)
	#rn
	#rb none

Change 3574471 on 2017/08/07 by Dmitriy.Dyomin

	Export ULevelStreamingKismet::LoadLevelInstance function
	#rb none

Change 3576262 on 2017/08/08 by Dmitriy.Dyomin

	Fixed: widget clipping issues in world composition
	#rb none

Change 3576845 on 2017/08/08 by Nick.Shin

	set HTML5LaunchHelper application's icon to UE4.ico

	#jira UE-19225 HTML5LaunchHelper application does not have an unreal icon
	#rb none
	#rnx

Change 3578313 on 2017/08/09 by Dmitriy.Dyomin

	Added: an RHI call to invalidate/clear cached state, RHIInvalidateCachedState
	#jira UEMOB-435
	#rb jack.porter

Change 3578364 on 2017/08/09 by Dmitriy.Dyomin

	Vertex Fog is disabled on mobile by default. If scene uses vertex fog - Mobile preview and device will show on screen message: PROJECT HAS VERTEX FOG ON MOBILE DISABLED
	This saves about 90 instructions in VS and a few in PS
	#jira UEMOB-166
	#rb jack.porter

Change 3578703 on 2017/08/09 by Nick.Shin

	set HTML5LaunchHelper application's icon to UE4.ico

	forgot to check in exe and pdb file

	#jira UE-19225 HTML5LaunchHelper application does not have an unreal icon
	#rb none
	#rnx

Change 3578961 on 2017/08/09 by Peter.Sauerbrei

	deprecate IOS 8 as the minimum OS supported.
	#jira UEMOB-429
	#rb chris.babcock

Change 3579319 on 2017/08/09 by Peter.Sauerbrei

	fixes for compile errors with Xcode 9 beta 4
	#rb none

Change 3579356 on 2017/08/09 by Peter.Sauerbrei

	modified minimum IOS to build with
	#rb chris.babcock

Change 3579687 on 2017/08/09 by Chris.Babcock

	Fix GoogleVR Gradle packaging
	#jira UE-48239
	#ue4
	#android
	#rb none

Change 3579921 on 2017/08/10 by Dmitriy.Dyomin

	GitHub 3670 : More zoom levels for World Composition (300)
	#contributedby: user37337
	#jira UE-45977
	#3670
	#rb none

Change 3580576 on 2017/08/10 by Peter.Sauerbrei

	detection of iPad Pro 10.5 and IPad Pro 12.9 (2nd Gen)
	#rb chris.babcock

Change 3580611 on 2017/08/10 by Chris.Babcock

	Set online provider back to GooglePlay and remove forcing IAP permission (contributed by umerov1999)
	#jira UE-48185
	#PR #3876
	#ue4
	#android
	#rb Peter.Sauerbrei

Change 3582166 on 2017/08/11 by Nick.Shin

	nuke PLATFORM_HTML5_WIN32


	PLATFORM_HTML5_WIN32 code removal tested successfully with (force rebuild and repackaging):

	* Win64 server (WindowsServer)
	* Win64 client (WindowsNoEditor)
	* HTML5 client

	all playing together via websocket net driver (i've attached a screen shot of this in jira)


	code changes touches: physics, audio and main build files


	#jira UEMOB-433  Remove Win32 SDL "HTML5 Simulator" code
	#rb ben.marsh
	#rnx
	#codereview josh.adams
	#fyi ori.cohen, aaron.mclera

Change 3582474 on 2017/08/11 by Chris.Babcock

	Don't use V2 signing for Gear VR APKs
	#jira UE-48354
	#ue4
	#android
	#rb Peter.Sauerbrei

Change 3582614 on 2017/08/11 by Chris.Babcock

	Filter out unneeded architectures from APK for Gradle builds
	#jira UE-48355
	#ue4
	#android
	#rb Peter.Sauerbrei

Change 3582923 on 2017/08/11 by Nick.Shin

	backport release 4.17 to dev-mobile

	#jira none
	#rb none
	#rnx

Change 3582924 on 2017/08/11 by Nick.Shin

	FNetworkFileServerHttp - error gracefully when port is already in use

	#jira UE-46409  [CrashReport] Assertion on Mac: Could not create a libwebsocket - FNetworkFileServerHttp::Init()
	#rnx
	#rb none

Change 3582925 on 2017/08/11 by Nick.Shin

	HTML5 - turn off pak file compression in favor of gzip packages

	#jira UE-46729  HTML5 - on shipping builds - turn off pak file compression in favor of gzip packages
	#rn
	#rb none

Change 3583943 on 2017/08/14 by Cosmin.Sulea

	UEMOB-363 - second iteration - Project wide texture quality control by texture group
	#rb Dmitriy Dyomin
	#jira UEMOB-363

Change 3583967 on 2017/08/14 by Cosmin.Sulea

	Back out changelist 3583943
	#rb none

Change 3584121 on 2017/08/14 by Peter.Sauerbrei

	fix for mac compile failure
	#rb none

Change 3587877 on 2017/08/15 by Peter.Sauerbrei

	josh's suggested fix is not working for Xcode 8.3, so brute forcing for now
	#rb none

Change 3588612 on 2017/08/15 by Peter.Sauerbrei

	Xcode 9 project compatbility updates
	#rb chris.babcock
	#codereview michael.trepka

Change 3589223 on 2017/08/15 by Dmitriy.Dyomin

	Fixed: bNavigationAutoUpdateEnabled was not always working when reopeinig the map
	Fixed: Navigation Build was not clearing some mesh tiles when bNavigationAutoUpdateEnabled is enabled
	Fixed: Streaming out a level in editor was not always updating NavMesh debug draw
	#rb lukasz.furman

Change 3589900 on 2017/08/16 by Dmitriy.Dyomin

	Support vulkan validation layers on Android, only in Debug and Development configuration (requires r.Vulkan.EnableValidation=1)
	#codereview chris.babcock, rolando.caloca
	#rb none

Change 3590592 on 2017/08/16 by Nick.Shin

	HTML5 emscripten 1.37.19 OSX

	#jira UE-47813
	#rb none
	#rn HTML5 emscripten 1.37.19 OSX

Change 3590597 on 2017/08/16 by Nick.Shin

	HTML5 emscripten 1.37.19 Linux

	#jira UE-47813
	#rb none
	#rn HTML5 emscripten 1.37.19 Linux

Change 3590624 on 2017/08/16 by Nick.Shin

	HTML5 emscripten 1.37.19 toolchain

	#jira UE-47813
	#rb none
	#rn HTML5 emscripten 1.37.19 toolchain

Change 3591720 on 2017/08/16 by Chris.Babcock

	Enable Gradle by default and add button to accept Android SDK license to project settings
	#jira UE-48519
	#ue4
	#android
	#rb Tim.Lincoln
	#fyi Peter.Sauerbrei

Change 3591998 on 2017/08/16 by Chris.Babcock

	Fix nonunity build
	#ue4
	#android
	#rb none

Change 3592407 on 2017/08/17 by Nick.Shin

	HTML5 emscripten 1.37.19 Win64

	#jira UE-47813
	#rb none
	#rn HTML5 emscripten 1.37.19 Win64

Change 3592479 on 2017/08/17 by Nick.Shin

	HTML5 3rd Party Libs - compiled with emscripten 1.37.19

	#jira UE-47813
	#rb none
	#rn HTML5 3rd Party Libs - compiled with emscripten 1.37.19 toolchain

Change 3592480 on 2017/08/17 by Nick.Shin

	HTML5 emscripten 1.37.19 toolchain Epic edits

	as well as setting UE4 HTML c# scripts to use new toolchain

	#jira UE-47813
	#rb none
	#rn HTML5 emscripten 1.37.19 toolchain Epic edits

Change 3592481 on 2017/08/17 by Nick.Shin

	HTML5 remove old emscripten toolchain

	#jira UE-47813
	#rb none
	#rn HTML5 remove old emscripten toolchain

Change 3592485 on 2017/08/17 by Nick.Shin

	HTML5 undo CanUseXGE - this might be breaking CIS for HTML5 builds...

	#jira UE-47813
	#rb none
	#rnx

Change 3592549 on 2017/08/17 by Dmitriy.Dyomin

	Added GetDiskTotalAndFreeSpace for IOS and Android
	#jira UE-46479
	#codereview chris.babcock, peter.sauerbrei
	#rb none

Change 3594045 on 2017/08/17 by Peter.Sauerbrei

	comment about potential failure case in the remote tool chain
	#rb none

Change 3594342 on 2017/08/17 by Peter.Sauerbrei

	Merging

	//UE4/Main/...

	to //UE4/Dev-Mobile/...

	#rb none

Change 3594920 on 2017/08/17 by Peter.Sauerbrei

	fix for non-unity builds (accidentally merged something incorrectly)
	#rb none

Change 3595347 on 2017/08/17 by Chris.Babcock

	merge fixes for Android
	#ue4
	#android
	#rb Peter.Sauerbrei
	#lockdown Peter.Sauerbrei

Change 3595752 on 2017/08/17 by Chris.Babcock

	Update Facebook plugin to support Gradle
	#jira UE-48569
	#ue4
	#android
	#fyi Josh.Markiewicz
	#rb none
	#lockdown Peter.Sauerbrei

Change 3595849 on 2017/08/17 by Chris.Babcock

	Fix issue with libovrplatformloader.so for non armv7 targets
	#jira UE-48533
	#ue4
	#android
	#rb none
	#lockdown Peter.Sauerbrei

Change 3596419 on 2017/08/18 by Peter.Sauerbrei

	fix for Mac Editor build failure
	#rb none

Change 3597023 on 2017/08/18 by Peter.Sauerbrei

	fix for game editor build failure
	#rb none

Change 3597032 on 2017/08/18 by Peter.Sauerbrei

	fix for app bundle id in Info-Editor.plist
	#rb none

Change 3597034 on 2017/08/18 by Peter.Sauerbrei

	put back the info.plist, found the real problem
	#rb none

Change 3597197 on 2017/08/18 by Peter.Sauerbrei

	pull Info.plist from the build products
	#rb none

[CL 3600450 by Chris Babcock in Main branch]
2017-08-21 15:05:19 -04:00

574 lines
18 KiB
C++

// Copyright 1998-2017 Epic Games, Inc. All Rights Reserved.
#include "CoreMinimal.h"
#include "HAL/PlatformProcess.h"
#include "HAL/FileManager.h"
#include "Misc/CommandLine.h"
#include "Misc/FileHelper.h"
#include "Misc/Paths.h"
#include "Misc/Guid.h"
#include "Misc/ConfigCacheIni.h"
#include "ImageCore.h"
#include "Modules/ModuleManager.h"
#include "Interfaces/ITextureFormat.h"
#include "Interfaces/ITextureFormatModule.h"
#include "TextureCompressorModule.h"
#include "PixelFormat.h"
#define MAX_QUALITY 4
DEFINE_LOG_CATEGORY_STATIC(LogTextureFormatPVR, Log, All);
/**
* Macro trickery for supported format names.
*/
#define ENUM_SUPPORTED_FORMATS(op) \
op(PVRTC2) \
op(PVRTC4) \
op(PVRTCN) \
op(AutoPVRTC)
#define DECL_FORMAT_NAME(FormatName) static FName GTextureFormatName##FormatName = FName(TEXT(#FormatName));
ENUM_SUPPORTED_FORMATS(DECL_FORMAT_NAME);
#undef DECL_FORMAT_NAME
#define DECL_FORMAT_NAME_ENTRY(FormatName) GTextureFormatName##FormatName ,
static FName GSupportedTextureFormatNames[] =
{
ENUM_SUPPORTED_FORMATS(DECL_FORMAT_NAME_ENTRY)
};
#undef DECL_FORMAT_NAME_ENTRY
#undef ENUM_SUPPORTED_FORMATS
// PVR file header format
#if PLATFORM_SUPPORTS_PRAGMA_PACK
#pragma pack(push, 4)
#endif
struct FPVRHeader
{
uint32 Version;
uint32 Flags;
uint64 PixelFormat ;
uint32 ColorSpace;
uint32 ChannelType;
uint32 Height;
uint32 Width;
uint32 Depth;
uint32 NumSurfaces;
uint32 NumFaces;
uint32 NumMipmaps;
uint32 MetaDataSize;
};
#if PLATFORM_SUPPORTS_PRAGMA_PACK
#pragma pack(pop)
#endif
/**
* Converts a power-of-two image to a square format (ex: 256x512 -> 512x512). Be wary of memory waste when too many texture are not square.
*
* @param Image The image to be converted to a square
* @return true if the image was converted successfully, else false
*/
static bool SquarifyImage(FImage& Image, uint32 MinSquareSize)
{
// Early out
if (Image.SizeX == Image.SizeY && Image.SizeX >= int32(MinSquareSize))
{
return true;
}
// Figure out the squarified size
uint32 SquareSize = FMath::Max(Image.SizeX, Image.SizeY);
if(SquareSize < MinSquareSize)
{
SquareSize = MinSquareSize;
}
// Calculate how many times to duplicate each row of column
uint32 MultX = SquareSize / Image.SizeX;
uint32 MultY = SquareSize / Image.SizeY;
// Only give memory overhead warning if we're actually going to use a larger image
// Small mips that have to be upscaled for compression only save the smaller mip for use
if(MultX == 1 || MultY == 1)
{
float FOverhead = float(FMath::Min(Image.SizeX, Image.SizeY)) / float(SquareSize);
int32 POverhead = FMath::RoundToInt(100.0f - (FOverhead * 100.0f));
UE_LOG(LogTextureFormatPVR, Warning, TEXT("Expanding mip (%d,%d) to (%d, %d). Memory overhead: ~%d%%"),
Image.SizeX, Image.SizeY, SquareSize, SquareSize, POverhead);
}
else if (MultX != MultY)
{
float FOverhead = float(FMath::Min(Image.SizeX, Image.SizeY)) / float(FMath::Max(Image.SizeX, Image.SizeY));
int32 POverhead = FMath::RoundToInt(100.0f - (FOverhead * 100.0f));
UE_LOG(LogTextureFormatPVR, Warning, TEXT("Expanding mip (%d,%d) to (%d, %d). Memory overhead: ~%d%%"),
Image.SizeX, Image.SizeY, FMath::Max(Image.SizeX, Image.SizeY), FMath::Max(Image.SizeX, Image.SizeY), POverhead);
}
// Allocate room to fill out into
TArray<uint32> SquareRawData;
SquareRawData.SetNumUninitialized(SquareSize * SquareSize * Image.NumSlices);
int32 SourceSliceSize = Image.SizeX * Image.SizeY;
int32 DestSliceSize = SquareSize * SquareSize;
for ( int32 SliceIndex=0; SliceIndex < Image.NumSlices; ++SliceIndex )
{
uint32* RectData = ((uint32*)Image.RawData.GetData()) + SliceIndex * SourceSliceSize;
uint32* SquareData = ((uint32*)SquareRawData.GetData()) + SliceIndex * DestSliceSize;
for ( int32 Y = 0; Y < Image.SizeY; ++Y )
{
for ( int32 X = 0; X < Image.SizeX; ++X )
{
uint32 SourceColor = *(RectData + Y * Image.SizeX + X);
for ( uint32 YDup = 0; YDup < MultY; ++YDup )
{
for ( uint32 XDup = 0; XDup < MultX; ++XDup )
{
uint32* DestColor = SquareData + ((Y * MultY + YDup) * SquareSize + (X * MultX + XDup));
*DestColor = SourceColor;
}
}
}
}
}
// Put the new image data into the existing Image (copying from uint32 array to uint8 array)
Image.RawData.Empty(SquareSize * SquareSize * Image.NumSlices * sizeof(uint32));
Image.RawData.SetNumUninitialized(SquareSize * SquareSize * Image.NumSlices * sizeof(uint32));
uint32* FinalData = (uint32*)Image.RawData.GetData();
FMemory::Memcpy(Image.RawData.GetData(), SquareRawData.GetData(), SquareSize * SquareSize * Image.NumSlices * sizeof(uint32));
Image.SizeX = SquareSize;
Image.SizeY = SquareSize;
return true;
}
static void DeriveNormalZ(FImage& Image)
{
int32 SliceSize = Image.SizeX * Image.SizeY;
for ( int32 SliceIndex=0; SliceIndex < Image.NumSlices; ++SliceIndex )
{
FColor* RectData = (FColor*)Image.RawData.GetData() + SliceIndex * SliceSize;
for(int32 Y = 0; Y < Image.SizeY; ++Y)
{
for(int32 X = 0; X < Image.SizeX; ++X)
{
FColor& SourceColor = *(RectData + Y * Image.SizeX + X);
const float NormalX = SourceColor.R / 255.0f * 2 - 1;
const float NormalY = SourceColor.G / 255.0f * 2 - 1;
const float NormalZ = FMath::Sqrt(FMath::Clamp<float>(1 - (NormalX * NormalX + NormalY * NormalY), 0, 1));
SourceColor.B = FMath::TruncToInt((NormalZ + 1) / 2.0f * 255.0f);
}
}
}
}
/**
* Checks if the passed image is a proper power-of-2 image
*
* @param Image The Image to evaluate
* @return true if the image is a power of 2, else false
*/
static bool ValidateImagePower(const FImage& Image)
{
// Image must already have power of 2 dimensions
bool bDimensionsValid = true;
int DimX = Image.SizeX;
int DimY = Image.SizeY;
while(DimX >= 2)
{
if(DimX % 2 == 1)
{
bDimensionsValid = false;
break;
}
DimX /= 2;
}
while(DimY >= 2 && bDimensionsValid)
{
if(DimY % 2 == 1)
{
bDimensionsValid = false;
break;
}
DimY /= 2;
}
if(!bDimensionsValid)
{
return false;
}
return true;
}
/**
* Fills the output structure with the original uncompressed mip information
*
* @param InImage The mip to compress
* @param OutCompressImage The output image (uncompressed in this case)
*/
static void UseOriginal(const FImage& InImage, FCompressedImage2D& OutCompressedImage, EPixelFormat CompressedPixelFormat, EGammaSpace GammaSpace)
{
// Get Raw Data
FImage Image;
InImage.CopyTo(Image, ERawImageFormat::BGRA8, GammaSpace);
// Fill out the output information
OutCompressedImage.SizeX = Image.SizeX;
OutCompressedImage.SizeY = Image.SizeY;
OutCompressedImage.PixelFormat = CompressedPixelFormat;
// Output Data
OutCompressedImage.RawData.SetNumUninitialized(Image.SizeX * Image.SizeY * 4);
void* MipData = (void*)Image.RawData.GetData();
FMemory::Memcpy(MipData, Image.RawData.GetData(), Image.SizeX * Image.SizeY * 4);
}
static int32 GetDefaultCompressionValue()
{
// start at default quality, then lookup in .ini file
int32 CompressionModeValue = 0;
GConfig->GetInt(TEXT("/Script/UnrealEd.CookerSettings"), TEXT("DefaultPVRTCQuality"), CompressionModeValue, GEngineIni);
FParse::Value(FCommandLine::Get(), TEXT("-pvrtcquality="), CompressionModeValue);
CompressionModeValue = FMath::Min<uint32>(CompressionModeValue, MAX_QUALITY);
return CompressionModeValue;
}
static FString GetPVRTCQualityString(int32 OverrideSizeValue = -1)
{
// convert to a string
FString CompressionMode;
switch (OverrideSizeValue >= 0 ? OverrideSizeValue : GetDefaultCompressionValue())
{
case 0: CompressionMode = TEXT("fastest"); break;
case 1: CompressionMode = TEXT("fast"); break;
case 2: CompressionMode = TEXT("normal"); break;
case 3: CompressionMode = TEXT("high"); break;
case 4: CompressionMode = TEXT("best"); break;
default: UE_LOG(LogTemp, Fatal, TEXT("Max quality higher than expected"));
}
return CompressionMode;
}
static uint16 GetPVRTCQualityForVersion(int32 OverrideSizeValue = -1)
{
// top 3 bits for compression value
return (OverrideSizeValue >= 0 ? OverrideSizeValue : GetDefaultCompressionValue()) << 13;
}
/**
* PVR texture format handler.
*/
class FTextureFormatPVR : public ITextureFormat
{
virtual bool AllowParallelBuild() const override
{
return true;
}
virtual uint16 GetVersion(
FName Format,
const struct FTextureBuildSettings* BuildSettings = nullptr
) const override
{
return 7 + GetPVRTCQualityForVersion(BuildSettings ? BuildSettings->CompressionQuality : -1);
}
virtual void GetSupportedFormats(TArray<FName>& OutFormats) const override
{
for (int32 i = 0; i < ARRAY_COUNT(GSupportedTextureFormatNames); ++i)
{
OutFormats.Add(GSupportedTextureFormatNames[i]);
}
}
virtual FTextureFormatCompressorCaps GetFormatCapabilities() const override
{
FTextureFormatCompressorCaps RetCaps;
// PVR compressor is limited to <=4096 in any direction.
RetCaps.MaxTextureDimension = 4096;
return RetCaps;
}
virtual bool CompressImage(
const FImage& InImage,
const struct FTextureBuildSettings& BuildSettings,
bool bImageHasAlphaChannel,
FCompressedImage2D& OutCompressedImage
) const override
{
// Get Raw Image Data from passed in FImage
FImage Image;
InImage.CopyTo(Image, ERawImageFormat::BGRA8, BuildSettings.GetGammaSpace());
// Get the compressed format
EPixelFormat CompressedPixelFormat = PF_Unknown;
if (BuildSettings.TextureFormatName == GTextureFormatNamePVRTC2)
{
CompressedPixelFormat = PF_PVRTC2;
}
else if (BuildSettings.TextureFormatName == GTextureFormatNamePVRTC4 || BuildSettings.TextureFormatName == GTextureFormatNamePVRTCN)
{
CompressedPixelFormat = PF_PVRTC4;
}
else if (BuildSettings.TextureFormatName == GTextureFormatNameAutoPVRTC)
{
CompressedPixelFormat = bImageHasAlphaChannel ? PF_PVRTC4 : PF_PVRTC2;
}
// Verify Power of 2
if ( !ValidateImagePower(Image) )
{
UE_LOG(LogTextureFormatPVR, Warning, TEXT("Mip size (%d,%d) does not have power-of-two dimensions and cannot be compressed to PVRTC%d"),
Image.SizeX, Image.SizeY, CompressedPixelFormat == PF_PVRTC2 ? 2 : 4);
return false;
}
// Squarify image
int32 FinalSquareSize = FGenericPlatformMath::Max(Image.SizeX, Image.SizeY);
SquarifyImage(Image, (CompressedPixelFormat == PF_PVRTC2) ? 16 : 8);
check(Image.SizeX == Image.SizeY);
if ( BuildSettings.TextureFormatName == GTextureFormatNamePVRTCN )
{
// Derive Z from X and Y to be consistent with BC5 normal maps used on PC (toss the texture's actual Z)
DeriveNormalZ(Image);
}
bool bCompressionSucceeded = true;
int32 SliceSize = Image.SizeX * Image.SizeY;
for (int32 SliceIndex = 0; SliceIndex < Image.NumSlices && bCompressionSucceeded; ++SliceIndex)
{
TArray<uint8> CompressedSliceData;
bCompressionSucceeded = CompressImageUsingPVRTexTool(
Image.AsBGRA8() + SliceIndex * SliceSize,
CompressedPixelFormat,
Image.SizeX,
Image.SizeY,
Image.IsGammaCorrected(),
FinalSquareSize,
CompressedSliceData,
BuildSettings
);
OutCompressedImage.RawData.Append(CompressedSliceData);
}
if ( bCompressionSucceeded )
{
OutCompressedImage.SizeX = FinalSquareSize;
OutCompressedImage.SizeY = FinalSquareSize;
OutCompressedImage.PixelFormat = CompressedPixelFormat;
}
// Return success status
return bCompressionSucceeded;
}
static bool CompressImageUsingPVRTexTool( void* SourceData, EPixelFormat PixelFormat, int32 SizeX, int32 SizeY, bool bSRGB, int32 FinalSquareSize, TArray<uint8>& OutCompressedData, const struct FTextureBuildSettings& BuildSettings )
{
// Figure out whether to use 2 bits or 4 bits per pixel (PVRTC2/PVRTC4)
bool bIsPVRTC2 = (PixelFormat == PF_PVRTC2);
const int32 BlockSizeX = bIsPVRTC2 ? 8 : 4; // PVRTC2 uses 8x4 blocks, PVRTC4 uses 4x4 blocks
const int32 BlockSizeY = 4;
const int32 BlockBytes = 8; // Both PVRTC2 and PVRTC4 are 8 bytes per block
const uint32 DestSizeX = FinalSquareSize;
const uint32 DestSizeY = FinalSquareSize;
// min 2x2 blocks per mip
const uint32 DestBlocksX = FGenericPlatformMath::Max<uint32>(DestSizeX / BlockSizeX, 2);
const uint32 DestBlocksY = FGenericPlatformMath::Max<uint32>(DestSizeY / BlockSizeY, 2);
const uint32 DestNumBytes = DestBlocksX * DestBlocksY * BlockBytes;
// If using an image that's too small, compressor needs to generate mips for us with an upscaled image
check(SizeX == SizeY);
const int32 SourceSquareSize = SizeX;
bool bGenerateMips = (FinalSquareSize < SourceSquareSize) ? true : false;
// Allocate space to store compressed data.
OutCompressedData.Empty(DestNumBytes);
OutCompressedData.AddUninitialized(DestNumBytes);
void* MipData = OutCompressedData.GetData();
// Write SourceData into PVR file on disk
// Init Header
FPVRHeader PVRHeader;
PVRHeader.Version = 0x03525650; // endianess does not match
PVRHeader.Flags = 0;
PVRHeader.PixelFormat = 0x0808080861726762; // Format of the UTexture 8bpp and BGRA ordered
PVRHeader.ColorSpace = 0; // Setting to 1 indicates SRGB, but causes PVRTexTool to unpack to linear. We want the image to remain in sRGB space as we do the conversion in the shader.
PVRHeader.ChannelType = 0;
PVRHeader.Height = SizeY;
PVRHeader.Width = SizeX;
PVRHeader.Depth = 1;
PVRHeader.NumSurfaces = 1;
PVRHeader.NumFaces = 1;
PVRHeader.NumMipmaps = 1;
PVRHeader.MetaDataSize = 0;
// Get file paths for intermediates. Unique path to avoid filename collision
FGuid Guid;
FPlatformMisc::CreateGuid(Guid);
FString InputFilePath = FString::Printf(TEXT("Cache/%x%x%x%xRGBToPVRIn.pvr"), Guid.A, Guid.B, Guid.C, Guid.D);
InputFilePath = FPaths::ProjectIntermediateDir() + InputFilePath;
FString OutputFilePath = FString::Printf(TEXT("Cache/%x%x%x%xRGBToPVROut.pvr"), Guid.A, Guid.B, Guid.C, Guid.D);
OutputFilePath = FPaths::ProjectIntermediateDir() + OutputFilePath;
FArchive* PVRFile = NULL;
while(!PVRFile)
{
PVRFile = IFileManager::Get().CreateFileWriter(*InputFilePath); // Occasionally returns NULL due to error code ERROR_SHARING_VIOLATION
FPlatformProcess::Sleep(0.01f); // ... no choice but to wait for the file to become free to access
}
// Write out header
uint32 HeaderSize = sizeof( PVRHeader );
check(HeaderSize==52);
PVRFile->Serialize(&PVRHeader, HeaderSize);
// Write out uncompressed data
PVRFile->Serialize(SourceData, SizeX * SizeY * sizeof(uint32));
// Finished writing file
PVRFile->Close();
delete PVRFile;
// Compress PVR file to PVRTC
FString CompressionMode = GetPVRTCQualityString(BuildSettings.CompressionQuality);
// Use PowerVR's new CLI tool commandline
FString Params = FString::Printf(TEXT("-i \"%s\" -o \"%s\" %s -legacypvr -q pvrtc%s -f PVRTC1_%d"),
*InputFilePath, *OutputFilePath,
bGenerateMips ? TEXT("-m") : TEXT(""),
*CompressionMode,
bIsPVRTC2 ? 2 : 4);
#if PLATFORM_MAC
FString CompressorPath(FPaths::EngineDir() + TEXT("Binaries/ThirdParty/ImgTec/PVRTexToolCLI"));
#elif PLATFORM_LINUX
FString CompressorPath(FPaths::EngineDir() + TEXT("Binaries/ThirdParty/ImgTec/PVRTexToolCLI.lnx"));
#elif PLATFORM_WINDOWS
FString CompressorPath(FPaths::EngineDir() + TEXT("Binaries/ThirdParty/ImgTec/PVRTexToolCLI.exe"));
#else
#error Unsupported platform
#endif
UE_LOG(LogTemp, Log, TEXT("Running texturetool with '%s'"), *Params);
// Give a debug message about the process
if (IsRunningCommandlet())
{
//UE_LOG(LogTextureFormatPVR, Display, TEXT("Compressing mip (%dx%d) to PVRTC%d for mobile devices..."), ImageSizeX, ImageSizeY, bIsPVRTC2 ? 2 : 4);
}
// Start Compressor
FProcHandle Proc = FPlatformProcess::CreateProc(*CompressorPath, *Params, true, false, false, NULL, -1, NULL, NULL);
bool bConversionWasSuccessful = true;
// Failed to start the compressor process?
if (!Proc.IsValid())
{
UE_LOG(LogTextureFormatPVR, Error, TEXT("Failed to start PVR compressor tool. (Path:%s)"), *CompressorPath);
bConversionWasSuccessful = false;
}
if ( bConversionWasSuccessful )
{
// Wait for the process to complete
int ReturnCode;
while (!FPlatformProcess::GetProcReturnCode(Proc, &ReturnCode))
{
FPlatformProcess::Sleep(0.01f);
}
FPlatformProcess::CloseProc(Proc);
// Did it fail?
if ( ReturnCode != 0 )
{
UE_LOG(LogTextureFormatPVR, Error, TEXT("PVR tool Failed with Return Code %d Mip Size (%d,%d)"), ReturnCode, SizeX, SizeY);
bConversionWasSuccessful = false;
}
}
// Open compressed file and put the data in OutCompressedImage
if ( bConversionWasSuccessful )
{
// Calculate which mip to pull from compressed image
int32 MipLevel = 0;
{
int32 i = SizeX;
while ( i > FinalSquareSize )
{
i /= 2;
++MipLevel;
}
}
// Get Raw File Data
TArray<uint8> PVRData;
FFileHelper::LoadFileToArray(PVRData, *OutputFilePath);
// Process It
FPVRHeader* Header = (FPVRHeader*)PVRData.GetData();
// Calculate the offset to get to the mip data
int FileOffset = HeaderSize;
for(int32 i = 0; i < MipLevel; ++i)
{
// Get the mip size for each image before the mip we want
uint32 LocalMipSizeX = FGenericPlatformMath::Max<uint32>(SizeX >> i, 1);
uint32 LocalMipSizeY = LocalMipSizeX;
uint32 LocalBlocksX = FGenericPlatformMath::Max<uint32>(LocalMipSizeX / BlockSizeX, 2);
uint32 LocalBlocksY = FGenericPlatformMath::Max<uint32>(LocalMipSizeY / BlockSizeY, 2);
uint32 LocalMipSize = LocalBlocksX * LocalBlocksY * BlockBytes;
// Add that mip's size to the offset
FileOffset += LocalMipSize;
}
// Copy compressed data
FMemory::Memcpy(MipData, PVRData.GetData() + FileOffset, DestNumBytes);
}
// Delete intermediate files
IFileManager::Get().Delete(*InputFilePath);
IFileManager::Get().Delete(*OutputFilePath);
return bConversionWasSuccessful;
}
};
/**
* Module for PVR texture compression.
*/
static ITextureFormat* Singleton = NULL;
class FTextureFormatPVRModule : public ITextureFormatModule
{
public:
virtual ~FTextureFormatPVRModule()
{
delete Singleton;
Singleton = NULL;
}
virtual ITextureFormat* GetTextureFormat()
{
if (!Singleton)
{
Singleton = new FTextureFormatPVR();
}
return Singleton;
}
};
IMPLEMENT_MODULE(FTextureFormatPVRModule, TextureFormatPVR);