Skip to content

Commit

Permalink
Update compress code for UE4.18, issue: microsoft#433
Browse files Browse the repository at this point in the history
  • Loading branch information
sytelus committed Apr 28, 2018
1 parent 53ad594 commit aaa7d80
Show file tree
Hide file tree
Showing 11 changed files with 139 additions and 40 deletions.
2 changes: 1 addition & 1 deletion Unreal/Environments/Blocks/Blocks.uproject
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"FileVersion": 3,
"EngineAssociation": "4.16",
"EngineAssociation": "4.18",
"Category": "",
"Description": "",
"Modules": [
Expand Down
2 changes: 2 additions & 0 deletions Unreal/Environments/Blocks/Config/DefaultEngine.ini
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ bSuppressFaceRemapTable=False
bSupportUVFromHitResults=False
bDisableActiveActors=False
bDisableCCD=False
bEnableEnhancedDeterminism=False
MaxPhysicsDeltaTime=0.033333
bSubstepping=False
bSubsteppingAsync=False
Expand All @@ -65,5 +66,6 @@ MaxSubsteps=6
SyncSceneSmoothingFactor=0.000000
AsyncSceneSmoothingFactor=0.990000
InitialAverageFrameRate=0.016667
PhysXTreeRebuildRate=10


36 changes: 35 additions & 1 deletion Unreal/Environments/Blocks/Config/DefaultGame.ini
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,39 @@ ProjectDisplayedTitle=NSLOCTEXT("[/Script/EngineSettings]", "8F8B6B2A472F9FDFB69
ProjectDebugTitleInfo=NSLOCTEXT("[/Script/EngineSettings]", "F31D7C524A9E9BC66DD2AA922D309408", "Blocks Environment for AirSim")

[/Script/UnrealEd.ProjectPackagingSettings]
IncludeDebugFiles=True
Build=IfProjectHasCode
BuildConfiguration=PPBC_Development
StagingDirectory=(Path="C:/temp")
FullRebuild=True
ForDistribution=False
IncludeDebugFiles=False
BlueprintNativizationMethod=Disabled
bIncludeNativizedAssetsInProjectGeneration=False
UsePakFile=True
bGenerateChunks=False
bGenerateNoChunks=False
bChunkHardReferencesOnly=False
bBuildHttpChunkInstallData=False
HttpChunkInstallDataDirectory=(Path="")
HttpChunkInstallDataVersion=
IncludePrerequisites=True
IncludeAppLocalPrerequisites=False
bShareMaterialShaderCode=True
bSharedMaterialNativeLibraries=True
ApplocalPrerequisitesDirectory=(Path="")
IncludeCrashReporter=False
InternationalizationPreset=English
-CulturesToStage=en
+CulturesToStage=en
bCookAll=False
bCookMapsOnly=False
bCompressed=True
bEncryptIniFiles=False
bEncryptPakIndex=False
bSkipEditorContent=False
+MapsToCook=(FilePath="/Game/FlyingCPP/Maps/FlyingExampleMap")
+MapsToCook=(FilePath="/Game/AirSimAssets")
bNativizeBlueprintAssets=False
bNativizeOnlySelectedBlueprints=False


Binary file not shown.
74 changes: 64 additions & 10 deletions Unreal/Plugins/AirSim/Source/AirBlueprintLib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
#include "Engine/LocalPlayer.h"
#include "Engine/SkeletalMesh.h"
#include "Slate/SceneViewport.h"
#include "IImageWrapper.h"
#include "ObjectThumbnail.h"
#include "Engine/Engine.h"

/*
Expand All @@ -29,10 +31,11 @@ Methods -> CamelCase
parameters -> camel_case
*/

bool UAirBlueprintLib::log_messages_hidden = false;
uint32_t UAirBlueprintLib::FlushOnDrawCount = 0;
msr::airlib::AirSimSettings::SegmentationSettings::MeshNamingMethodType UAirBlueprintLib::mesh_naming_method =
bool UAirBlueprintLib::log_messages_hidden_ = false;
uint32_t UAirBlueprintLib::flush_on_draw_count_ = 0;
msr::airlib::AirSimSettings::SegmentationSettings::MeshNamingMethodType UAirBlueprintLib::mesh_naming_method_ =
msr::airlib::AirSimSettings::SegmentationSettings::MeshNamingMethodType::OwnerName;
IImageWrapperModule* UAirBlueprintLib::image_wrapper_module_ = nullptr;

void UAirBlueprintLib::LogMessageString(const std::string &prefix, const std::string &suffix, LogDebugLevel level, float persist_sec)
{
Expand Down Expand Up @@ -122,31 +125,38 @@ void UAirBlueprintLib::enableViewportRendering(AActor* context, bool enable)
// Do this only if the main viewport is not being rendered anyway in case there are
// any adverse performance effects during main rendering.
//HACK: FViewPort doesn't expose this field so we are doing dirty work around by maintaining count by ourselves
if (FlushOnDrawCount == 0)
if (flush_on_draw_count_ == 0)
viewport->GetGameViewport()->IncrementFlushOnDraw();
}
else {
viewport->EngineShowFlags.SetRendering(true);

//HACK: FViewPort doesn't expose this field so we are doing dirty work around by maintaining count by ourselves
if (FlushOnDrawCount > 0)
if (flush_on_draw_count_ > 0)
viewport->GetGameViewport()->DecrementFlushOnDraw();
}
}

void UAirBlueprintLib::OnBeginPlay()
{
FlushOnDrawCount = 0;
flush_on_draw_count_ = 0;
image_wrapper_module_ = & FModuleManager::LoadModuleChecked<IImageWrapperModule>(FName("ImageWrapper"));
}

void UAirBlueprintLib::OnEndPlay()
{
//nothing to do for now
image_wrapper_module_ = nullptr;
}

IImageWrapperModule* UAirBlueprintLib::getImageWrapperModule()
{
return image_wrapper_module_;
}

void UAirBlueprintLib::LogMessage(const FString &prefix, const FString &suffix, LogDebugLevel level, float persist_sec)
{
if (log_messages_hidden)
if (log_messages_hidden_)
return;


Expand Down Expand Up @@ -334,7 +344,7 @@ void UAirBlueprintLib::SetObjectStencilID(ALandscapeProxy* mesh, int object_id)
template<class T>
std::string UAirBlueprintLib::GetMeshName(T* mesh)
{
switch(mesh_naming_method)
switch(mesh_naming_method_)
{
case msr::airlib::AirSimSettings::SegmentationSettings::MeshNamingMethodType::OwnerName:
if (mesh->GetOwner())
Expand All @@ -354,7 +364,7 @@ std::string UAirBlueprintLib::GetMeshName(T* mesh)
template<>
std::string UAirBlueprintLib::GetMeshName<USkinnedMeshComponent>(USkinnedMeshComponent* mesh)
{
switch(mesh_naming_method)
switch(mesh_naming_method_)
{
case msr::airlib::AirSimSettings::SegmentationSettings::MeshNamingMethodType::OwnerName:
if (mesh->GetOwner())
Expand Down Expand Up @@ -630,4 +640,48 @@ UClass* UAirBlueprintLib::LoadClass(const std::string& name)
throw std::invalid_argument(msg);
}
return cls;
}
}

void UAirBlueprintLib::CompressImageArray(int32 width, int32 height, const TArray<FColor> &src, TArray<uint8> &dest)
{
TArray<FColor> MutableSrcData = src;

// PNGs are saved as RGBA but FColors are stored as BGRA. An option to swap the order upon compression may be added at
// some point. At the moment, manually swapping Red and Blue
for (int32 Index = 0; Index < width*height; Index++)
{
uint8 TempRed = MutableSrcData[Index].R;
MutableSrcData[Index].R = MutableSrcData[Index].B;
MutableSrcData[Index].B = TempRed;
}

FObjectThumbnail TempThumbnail;
TempThumbnail.SetImageSize(width, height);
TArray<uint8>& ThumbnailByteArray = TempThumbnail.AccessImageData();

// Copy scaled image into destination thumb
int32 MemorySize = width*height * sizeof(FColor);
ThumbnailByteArray.AddUninitialized(MemorySize);
FMemory::Memcpy(ThumbnailByteArray.GetData(), MutableSrcData.GetData(), MemorySize);

// Compress data - convert into a .png
CompressUsingImageWrapper(ThumbnailByteArray, width, height, dest);;
}

bool UAirBlueprintLib::CompressUsingImageWrapper(const TArray<uint8>& uncompressed, const int32 width, const int32 height, TArray<uint8>& compressed)
{
bool bSucceeded = false;
compressed.Reset();
if (uncompressed.Num() > 0)
{
IImageWrapperModule* ImageWrapperModule = UAirBlueprintLib::getImageWrapperModule();
TSharedPtr<IImageWrapper> ImageWrapper = ImageWrapperModule->CreateImageWrapper(EImageFormat::PNG);
if (ImageWrapper.IsValid() && ImageWrapper->SetRaw(&uncompressed[0], uncompressed.Num(), width, height, ERGBFormat::RGBA, 8))
{
compressed = ImageWrapper->GetCompressed();
bSucceeded = true;
}
}

return bSucceeded;
}
18 changes: 12 additions & 6 deletions Unreal/Plugins/AirSim/Source/AirBlueprintLib.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "Components/MeshComponent.h"
#include "LandscapeProxy.h"
#include "common/AirSimSettings.hpp"
#include "IImageWrapperModule.h"
#include "AirBlueprintLib.generated.h"


Expand Down Expand Up @@ -91,15 +92,15 @@ class UAirBlueprintLib : public UBlueprintFunctionLibrary

static bool getLogMessagesHidden()
{
return log_messages_hidden;
return log_messages_hidden_;
}
static void setLogMessagesHidden(bool is_hidden)
{
log_messages_hidden = is_hidden;
log_messages_hidden_ = is_hidden;
}
static void SetMeshNamingMethod(msr::airlib::AirSimSettings::SegmentationSettings::MeshNamingMethodType method)
{
mesh_naming_method = method;
mesh_naming_method_ = method;
}

static void enableWorldRendering(AActor* context, bool enable);
Expand All @@ -112,6 +113,8 @@ class UAirBlueprintLib : public UBlueprintFunctionLibrary
static UClass* LoadClass(const std::string& name);

static void setUnrealClockSpeed(const AActor* context, float clock_speed);
static IImageWrapperModule* getImageWrapperModule();
static void CompressImageArray(int32 width, int32 height, const TArray<FColor> &src, TArray<uint8> &dest);

private:
template<typename T>
Expand All @@ -126,11 +129,14 @@ class UAirBlueprintLib : public UBlueprintFunctionLibrary
static void SetObjectStencilID(T* mesh, int object_id);
static void SetObjectStencilID(ALandscapeProxy* mesh, int object_id);

static bool CompressUsingImageWrapper(const TArray<uint8>& uncompressed, const int32 width, const int32 height, TArray<uint8>& compressed);

private:
static bool log_messages_hidden;
static bool log_messages_hidden_;
//FViewPort doesn't expose this field so we are doing dirty work around by maintaining count by ourselves
static uint32_t FlushOnDrawCount;
static msr::airlib::AirSimSettings::SegmentationSettings::MeshNamingMethodType mesh_naming_method;
static uint32_t flush_on_draw_count_;
static msr::airlib::AirSimSettings::SegmentationSettings::MeshNamingMethodType mesh_naming_method_;

static IImageWrapperModule* image_wrapper_module_;
};

2 changes: 2 additions & 0 deletions Unreal/Plugins/AirSim/Source/Car/SimModeCar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ void ASimModeCar::continueForTime(double seconds)

void ASimModeCar::setupClockSpeed()
{
ASimModeBase::setupClockSpeed(); //do the default setup, //TODO: may be we want explicit scalable clock?

current_clockspeed_ = getSettings().clock_speed;

//setup clock in PhysX
Expand Down
3 changes: 2 additions & 1 deletion Unreal/Plugins/AirSim/Source/RenderRequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "Engine/TextureRenderTarget2D.h"
#include "TaskGraphInterfaces.h"
#include "ImageUtils.h"
#include "AirBlueprintLib.h"

RenderRequest::RenderRequest(bool use_safe_method)
: use_safe_method_(use_safe_method), params_(nullptr), results_(nullptr), req_size_(0),
Expand Down Expand Up @@ -71,7 +72,7 @@ void RenderRequest::getScreenshot(std::shared_ptr<RenderParams> params[], std::v
if (results[i]->width != 0 && results[i]->height != 0) {
results[i]->image_data_uint8.SetNumUninitialized(results[i]->width * results[i]->height * 4, false);
if (params[i]->compress)
FImageUtils::CompressImageArray(results[i]->width, results[i]->height, results[i]->bmp, results[i]->image_data_uint8);
UAirBlueprintLib::CompressImageArray(results[i]->width, results[i]->height, results[i]->bmp, results[i]->image_data_uint8);
else {
uint8* ptr = results[i]->image_data_uint8.GetData();
for (const auto& item : results[i]->bmp) {
Expand Down
4 changes: 2 additions & 2 deletions build.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ REM //---------- Build rpclib ------------
ECHO Starting cmake to build rpclib...
IF NOT EXIST external\rpclib\rpclib-2.2.1\build mkdir external\rpclib\rpclib-2.2.1\build
cd external\rpclib\rpclib-2.2.1\build
REM cmake -G"Visual Studio 15 2017 Win64" ..
cmake -G"Visual Studio 14 2015 Win64" ..
REM cmake -G"Visual Studio 14 2015 Win64" ..
cmake -G"Visual Studio 15 2017 Win64" ..
cmake --build .
cmake --build . --config Release
if ERRORLEVEL 1 goto :buildfailed
Expand Down
8 changes: 4 additions & 4 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ else
if [ "$(uname)" == "Darwin" ]; then
CMAKE="$(greadlink -f cmake_build/bin/cmake)"

export CC=/usr/local/opt/llvm\@3.9/bin/clang
export CXX=/usr/local/opt/llvm\@3.9/bin/clang++
export CC=/usr/local/opt/llvm\@5.0/bin/clang
export CXX=/usr/local/opt/llvm\@5.0/bin/clang++
else
CMAKE="$(readlink -f cmake_build/bin/cmake)"

export CC="clang-3.9"
export CXX="clang++-3.9"
export CC="clang-5.0"
export CXX="clang++-5.0"
fi
fi

Expand Down
30 changes: 15 additions & 15 deletions setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,21 @@ fi

#give user perms to access USB port - this is not needed if not using PX4 HIL
#TODO: figure out how to do below in travis
if [ "$(uname)" == "Darwin" ]; then
if [ "$(uname)" == "Darwin" ]; then # osx
if [[ ! -z "${whoami}" ]]; then #this happens when running in travis
sudo dseditgroup -o edit -a `whoami` -t user dialout
fi

#below takes way too long
# brew install [email protected]
brew install --force-bottle llvm@3.9
brew install --force-bottle llvm@5.0

brew install wget
brew install coreutils

export C_COMPILER=/usr/local/opt/llvm\@3.9/bin/clang
export COMPILER=/usr/local/opt/llvm\@3.9/bin/clang++
else
export C_COMPILER=/usr/local/opt/llvm\@5.09/bin/clang
export COMPILER=/usr/local/opt/llvm\@5.0/bin/clang++
else #linux
if [[ ! -z "${whoami}" ]]; then #this happens when running in travis
sudo /usr/sbin/useradd -G dialout $USER
sudo usermod -a -G dialout $USER
Expand All @@ -38,14 +38,14 @@ else
sudo apt-get install -y build-essential
wget -O - http://apt.llvm.org/llvm-snapshot.gpg.key|sudo apt-key add -
sudo apt-get update
sudo apt-get install -y clang-3.9 clang++-3.9
sudo apt-get install -y clang-5.0 clang++-5.0
sudo apt-get install -y unzip

export C_COMPILER=clang-3.9
export COMPILER=clang++-3.9
export C_COMPILER=clang-5.0
export COMPILER=clang++-5.0
fi

#download cmake - we need v3.9+ which is not available in Ubuntu 16.04
#download cmake - we need v3.9+ which is not out of box in Ubuntu 16.04
if [[ ! -d "cmake_build/bin" ]]; then
echo "Downloading cmake..."
wget https://cmake.org/files/v3.10/cmake-3.10.2.tar.gz \
Expand Down Expand Up @@ -121,12 +121,12 @@ fi
# #sudo apt-get install -y clang-3.9-doc libclang-common-3.9-dev libclang-3.9-dev libclang1-3.9 libclang1-3.9-dbg libllvm-3.9-ocaml-dev libllvm3.9 libllvm3.9-dbg lldb-3.9 llvm-3.9 llvm-3.9-dev llvm-3.9-doc llvm-3.9-examples llvm-3.9-runtime clang-format-3.9 python-clang-3.9 libfuzzer-3.9-dev

#get libc++ source
if [[ ! -d "llvm-source-39" ]]; then
git clone --depth=1 -b release_39 https://github.com/llvm-mirror/llvm.git llvm-source-39
git clone --depth=1 -b release_39 https://github.com/llvm-mirror/libcxx.git llvm-source-39/projects/libcxx
git clone --depth=1 -b release_39 https://github.com/llvm-mirror/libcxxabi.git llvm-source-39/projects/libcxxabi
if [[ ! -d "llvm-source-50" ]]; then
git clone --depth=1 -b release_50 https://github.com/llvm-mirror/llvm.git llvm-source-50
git clone --depth=1 -b release_50 https://github.com/llvm-mirror/libcxx.git llvm-source-50/projects/libcxx
git clone --depth=1 -b release_50 https://github.com/llvm-mirror/libcxxabi.git llvm-source-50/projects/libcxxabi
else
echo "folder llvm-source already exists, skipping git clone..."
echo "folder llvm-source-50 already exists, skipping git clone..."
fi

#build libc++
Expand All @@ -142,7 +142,7 @@ pushd llvm-build >/dev/null
"$CMAKE" -DCMAKE_C_COMPILER=${C_COMPILER} -DCMAKE_CXX_COMPILER=${COMPILER} \
-LIBCXX_ENABLE_EXPERIMENTAL_LIBRARY=OFF -DLIBCXX_INSTALL_EXPERIMENTAL_LIBRARY=OFF \
-DCMAKE_BUILD_TYPE=RelWithDebInfo -DCMAKE_INSTALL_PREFIX=./output \
../llvm-source-39
../llvm-source-50

make cxx

Expand Down

0 comments on commit aaa7d80

Please sign in to comment.