Files
UnrealEngineUWP/Engine/Source/Developer/HTML5/HTML5TargetPlatform/Private/HTML5TargetPlatform.cpp
Josh Adams cf651e0c63 Change 2898947 on 2016/03/08 by Mark.Satterthwaite
Move the Apple Driver Monitor stats into their own stat groups, DriverMonitor has the common stats, DriverMonitorAMD/Intel/Nvidia have the vendor/GPU specific stats. The Metal and OpenGL RHIs update the driver monitor stats group for the current GPU at the appropriate time.

Change 2898950 on 2016/03/08 by Mark.Satterthwaite

	More shader cache code documentation.

Change 2898952 on 2016/03/08 by Michael.Trepka

	Check GPU driver version and warn of bad drivers only on Windows

Change 2898964 on 2016/03/08 by Mark.Satterthwaite

	Only verify the vertex attribute layout for Metal in debug builds or when using development with the debug layer turned on. It reduces performance significantly and isn't all that helpful unless you are attempting to debug a mismatch.

Change 2898973 on 2016/03/08 by Mark.Satterthwaite

	Switch uniform buffers to managed memory on Mac as this is more appropriate for AMD & Nvidia GPUs.

Change 2898988 on 2016/03/08 by Mark.Satterthwaite

	Simplify MetalContext by having only one SubmitCommandsHint implementation.

Change 2899011 on 2016/03/08 by Mark.Satterthwaite

	Duplicate 4.11 CL #2898988:

	Proper fix for UE-25804 - we have to manually expand PF_G8 + SRGB to RGBA8_sRGB - this then fixes UE-27483.
	#jira UE-25804
	#jira UE-27483

Change 2899024 on 2016/03/08 by Mark.Satterthwaite

	Duplicate 4.11 CL #2887365 & CL #2887583:

	Allow InfiltratorDemoEditor under Metal to issue a query buffer reset without crashing - the function that switches to the new query buffer needs to reapply some of the draw-state so that future commands don't dereference nil.
	#jira UE-27513

	My earlier fix for UE-27513 overlooked various internal details that meant it wouldn't restore state correctly, would fail validation and could crash in a new place. This version will ensure that cached state is only reset when it is appropriate to do so and will restore it correct when doing a query buffer reset.
	#jira UE-27513

Change 2899418 on 2016/03/08 by Daniel.Lamb

	Added support for textboxes in the editor to convert uasset filenames into long package names. As this is more useful to the cooker and more portable for projects.
	#codereview Matt.Kuhlenschmidt
	#jira UE-27785

Change 2899419 on 2016/03/08 by Daniel.Lamb

	Added support for passing -opengl command through to launch on if the editor is started with it.
	#codereview Michael.Trepka

Change 2900846 on 2016/03/09 by Mark.Satterthwaite

	Reimplement Metal object lifetime tracking as stats in the stat-group, though the old system is maintained as a debug-only tool that could (and probably should) be extended to track over/under-release bugs. Currently the texture count will be distorted by texture SRVs so will need improvement but other stats should be reliable. In order to properly report the number of buffers the TResourcePool policy class must now define a FreeResource function, so I've added them to the appropriate places too.

Change 2900853 on 2016/03/09 by Mark.Satterthwaite

	Optimise away empty encoders that don't perform a clear operation on AMD & Intel, but not Nvidia or non-Mac Metal devices. This should slightly improve performance.

Change 2900927 on 2016/03/09 by Mark.Satterthwaite

	Implemented operation threshold submission of Metal command buffers to keep the GPU busier and not just idle waiting for the CPU. Whenever rhi.Metal.CommandBufferCommitThreshold is set to a value >0 and the current command buffer has >= draw/dispatch operations outstanding then the command-buffer will be committed at the next encoder boundary. The default value is 100 operations which is currently arbitrary and the feature can be disabled by setting the value to <= 0 in which case only explicit submissions will occur as previously.

Change 2901310 on 2016/03/09 by Mark.Satterthwaite

	Change OneColor clear shader setup so that it works with parallel encoding in Metal.

Change 2903002 on 2016/03/10 by Mark.Satterthwaite

	Instantiate the OneColor shaders once in Metal.

Change 2903274 on 2016/03/10 by Mark.Satterthwaite

	Remove more unnecessary parallel execution stalls from MetalRHI.

Change 2903402 on 2016/03/10 by Mark.Satterthwaite

	Implement Metal support for index buffer SRVs.

Change 2903419 on 2016/03/10 by Mark.Satterthwaite

	Always use Managed memory on Mac Metal for buffers.

Change 2905206 on 2016/03/11 by Mark.Satterthwaite

	Worked around UE-27818 "ElementalDemo Causes Invalid Rendering on AMD GPUs" - recent changes to allow mesh particles to write to velocity leave a texture-buffer unbound & then use a uniform value & an if-branch to guard against access but AMD's Mac GL driver notices that the buffer is referenced in the shader but not bound & promptly tries to fallback to Apple's S/W renderer regardless of what the uniform value is. That's legal behaviour for an OpenGL implementation so the C++ code has been changed to allocate and write the current transforms into the buffer for OpenGL when they wouldn't otherwise be provided. This is sufficient to avoid the problem without affecting any other API.

Change 2906217 on 2016/03/11 by Nick.Shin

	re-enabled http network file server
	it was disabled in CL: #2790193

	#jira UE-22166 HTML5 Cook on the fly will launch and then close browser

Change 2908203 on 2016/03/14 by Michael.Trepka

	Merging //UE4/Dev-Main to Dev-Platform (//UE4/Dev-Platform). Everything but SSF lib.

Change 2908553 on 2016/03/14 by Mark.Satterthwaite

	Force a submit & wait in Metal when contexts are being destroyed to prevent kernel panics in drivers which continue to process the now abandoned command-queue and encounter invalid resources (because we destroy them on shutdown).

Change 2908595 on 2016/03/14 by Michael.Trepka

	Fixed iOS compile error in MetalUniformBuffer.cpp

	#codereview Mark.Satterthwaite

Change 2910106 on 2016/03/15 by Mark.Satterthwaite

	Use a dispatch_semaphore not an FEvent for Metal free-list synchronisation as the dispatch_worker threads can't be properly setup for FStats and this causes problems.

Change 2910107 on 2016/03/15 by Mark.Satterthwaite

	Fix Metal reporting of GPU memory through the RHI as it is in bytes, not MB.

Change 2910138 on 2016/03/15 by Mark.Satterthwaite

	Properly retain/release dispatch_semaphore for Metal command buffer completion block & allow uniform buffer creation on parallel encoding thread.

Change 2911735 on 2016/03/16 by Nick.Shin

	housekeeping

	removing extra and inconsistant whitespace as well as making tabs & spaces consistant

[CL 2936662 by Josh Adams in Main branch]
2016-04-07 12:59:50 -04:00

350 lines
9.4 KiB
C++

// Copyright 1998-2016 Epic Games, Inc. All Rights Reserved.
/*=============================================================================
HTML5TargetPlatform.cpp: Implements the FHTML5TargetPlatform class.
=============================================================================*/
#include "HTML5TargetPlatformPrivatePCH.h"
#if WITH_ENGINE
#include "DeviceProfiles/DeviceProfile.h"
#endif
DEFINE_LOG_CATEGORY_STATIC(LogHTML5TargetPlatform, Log, All);
/* Static initialization
*****************************************************************************/
FCriticalSection FHTML5TargetPlatform::DevicesCriticalSection;
/* FHTML5TargetPlatform structors
*****************************************************************************/
FHTML5TargetPlatform::FHTML5TargetPlatform( )
{
RefreshHTML5Setup();
#if WITH_ENGINE
// load up texture settings from the config file
HTML5LODSettings = nullptr;
StaticMeshLODSettings.Initialize(HTML5EngineSettings);
#endif
}
/* ITargetPlatform interface
*****************************************************************************/
void FHTML5TargetPlatform::GetAllDevices( TArray<ITargetDevicePtr>& OutDevices ) const
{
FScopeLock Lock( &DevicesCriticalSection );
OutDevices.Reset();
for( auto Iter = Devices.CreateConstIterator(); Iter; ++Iter )
{
OutDevices.Add( Iter.Value() );
}
}
ECompressionFlags FHTML5TargetPlatform::GetBaseCompressionMethod( ) const
{
return COMPRESS_ZLIB;
}
ITargetDevicePtr FHTML5TargetPlatform::GetDefaultDevice( ) const
{
FScopeLock Lock( &DevicesCriticalSection );
return Devices.FindRef( DefaultDeviceName );
}
ITargetDevicePtr FHTML5TargetPlatform::GetDevice( const FTargetDeviceId& DeviceId )
{
if( DeviceId.GetPlatformName() == this->PlatformName() )
{
FScopeLock Lock( &DevicesCriticalSection );
for( auto MapIt = Devices.CreateIterator(); MapIt; ++MapIt )
{
FHTML5TargetDevicePtr& Device = MapIt->Value;
if( Device->GetName() == DeviceId.GetDeviceName() )
{
return Device;
}
}
}
return nullptr;
}
bool FHTML5TargetPlatform::IsSdkInstalled(bool bProjectHasCode, FString& OutDocumentationPath) const
{
FString SDKPath = FPaths::EngineDir() / TEXT("Source/ThirdParty/HTML5/emsdk") /
#if PLATFORM_WINDOWS
TEXT("Win64");
#elif PLATFORM_MAC
TEXT("Mac");
#elif PLATFORM_LINUX
TEXT("Linux");
#else
TEXT("UNKNOWN_PLATFORM");
#endif
FString SDKDirectory = FPaths::ConvertRelativePathToFull(SDKPath);
if (IFileManager::Get().DirectoryExists(*SDKDirectory))
{
return true;
}
return false;
}
bool FHTML5TargetPlatform::IsRunningPlatform( ) const
{
return false; // but this will never be called because this platform doesn't run the target platform framework
}
#if WITH_ENGINE
static FName NAME_GLSL_ES2_WEBGL(TEXT("GLSL_ES2_WEBGL"));
void FHTML5TargetPlatform::GetAllPossibleShaderFormats( TArray<FName>& OutFormats ) const
{
OutFormats.AddUnique(NAME_GLSL_ES2_WEBGL);
}
void FHTML5TargetPlatform::GetAllTargetedShaderFormats( TArray<FName>& OutFormats ) const
{
GetAllPossibleShaderFormats(OutFormats);
}
const class FStaticMeshLODSettings& FHTML5TargetPlatform::GetStaticMeshLODSettings( ) const
{
return StaticMeshLODSettings;
}
void FHTML5TargetPlatform::GetTextureFormats( const UTexture* Texture, TArray<FName>& OutFormats ) const
{
FName TextureFormatName = NAME_None;
#if WITH_EDITOR
// Supported texture format names.
static FName NameDXT1(TEXT("DXT1"));
static FName NameDXT3(TEXT("DXT3"));
static FName NameDXT5(TEXT("DXT5"));
static FName NameDXT5n(TEXT("DXT5n"));
static FName NameAutoDXT(TEXT("AutoDXT"));
static FName NameBGRA8(TEXT("BGRA8"));
static FName NameG8(TEXT("G8"));
static FName NameRGBA16F(TEXT("RGBA16F"));
static FName NameRGBA8(TEXT("RGBA8"));
bool bNoCompression = Texture->CompressionNone // Code wants the texture uncompressed.
|| (HasEditorOnlyData() && Texture->DeferCompression) // The user wishes to defer compression, this is ok for the Editor only.
|| (Texture->CompressionSettings == TC_EditorIcon)
|| (Texture->LODGroup == TEXTUREGROUP_ColorLookupTable) // Textures in certain LOD groups should remain uncompressed.
|| (Texture->LODGroup == TEXTUREGROUP_Bokeh)
|| (Texture->LODGroup == TEXTUREGROUP_IESLightProfile)
|| (Texture->Source.GetSizeX() < 4) // Don't compress textures smaller than the DXT block size.
|| (Texture->Source.GetSizeY() < 4)
|| (Texture->Source.GetSizeX() % 4 != 0)
|| (Texture->Source.GetSizeY() % 4 != 0);
ETextureSourceFormat SourceFormat = Texture->Source.GetFormat();
// Determine the pixel format of the (un/)compressed texture
if (bNoCompression)
{
if (Texture->HasHDRSource())
{
TextureFormatName = NameBGRA8;
}
else if (SourceFormat == TSF_G8 || Texture->CompressionSettings == TC_Grayscale)
{
TextureFormatName = NameG8;
}
else if (Texture->LODGroup == TEXTUREGROUP_Shadowmap)
{
TextureFormatName = NameG8;
}
else
{
TextureFormatName = NameRGBA8;
}
}
else if (Texture->CompressionSettings == TC_HDR
|| Texture->CompressionSettings == TC_HDR_Compressed)
{
TextureFormatName = NameRGBA16F;
}
else if (Texture->CompressionSettings == TC_Normalmap)
{
TextureFormatName = NameDXT5;
}
else if (Texture->CompressionSettings == TC_Displacementmap)
{
TextureFormatName = NameG8;
}
else if (Texture->CompressionSettings == TC_VectorDisplacementmap)
{
TextureFormatName = NameRGBA8;
}
else if (Texture->CompressionSettings == TC_Grayscale)
{
TextureFormatName = NameG8;
}
else if( Texture->CompressionSettings == TC_Alpha)
{
TextureFormatName = NameDXT5;
}
else if (Texture->CompressionSettings == TC_DistanceFieldFont)
{
TextureFormatName = NameG8;
}
else if (Texture->CompressionNoAlpha)
{
TextureFormatName = NameDXT1;
}
else if (Texture->bDitherMipMapAlpha)
{
TextureFormatName = NameDXT5;
}
else
{
TextureFormatName = NameAutoDXT;
}
// Some PC GPUs don't support sRGB read from G8 textures (e.g. AMD DX10 cards on ShaderModel3.0)
// This solution requires 4x more memory but a lot of PC HW emulate the format anyway
if ((TextureFormatName == NameG8) && Texture->SRGB && !SupportsFeature(ETargetPlatformFeatures::GrayscaleSRGB))
{
TextureFormatName = NameBGRA8;
}
#endif
OutFormats.Add( TextureFormatName);
}
const UTextureLODSettings& FHTML5TargetPlatform::GetTextureLODSettings() const
{
return *HTML5LODSettings;
}
FName FHTML5TargetPlatform::GetWaveFormat( const USoundWave* Wave ) const
{
static FName NAME_OGG(TEXT("OGG"));
return NAME_OGG;
}
#endif // WITH_ENGINE
void FHTML5TargetPlatform::RefreshHTML5Setup()
{
FString Temp;
if (!FHTML5TargetPlatform::IsSdkInstalled(true, Temp))
{
// nothing to do.
return;
}
// update available devices
TArray<FString> DeviceMaps;
GConfig->GetArray( TEXT("/Script/HTML5PlatformEditor.HTML5SDKSettings"), TEXT("DeviceMap"), DeviceMaps, GEngineIni );
if ( ! DeviceMaps.Num() )
{
// trash can: nukes everything
// default list will be repopulated
FScopeLock Lock( &DevicesCriticalSection );
for (auto Iter = Devices.CreateIterator(); Iter; ++Iter)
{
FHTML5TargetDevicePtr Device = Iter->Value;
Iter.RemoveCurrent();
DeviceLostEvent.Broadcast(Device.ToSharedRef());
}
DefaultDeviceName.Empty();
}
else
{
// add or update
for (auto It : DeviceMaps)
{
FString DeviceName = "";
FString DevicePath = "";
if( FParse::Value( *It, TEXT( "DeviceName=" ), DeviceName ) &&
FParse::Value( *It, TEXT( "DevicePath=(FilePath=" ), DevicePath ) )
{
if (FPlatformFileManager::Get().GetPlatformFile().FileExists(*DevicePath) ||
FPlatformFileManager::Get().GetPlatformFile().DirectoryExists(*DevicePath))
{
FScopeLock Lock( &DevicesCriticalSection );
DeviceName = TEXT("user: ") + DeviceName;
FHTML5TargetDevicePtr& Device = Devices.FindOrAdd( DeviceName );
if( Device.IsValid() )
{ // remove "existing" - so can "update" it
DeviceLostEvent.Broadcast(Device.ToSharedRef());
}
Device = MakeShareable( new FHTML5TargetDevice( *this, DeviceName, DevicePath ) );
DeviceDiscoveredEvent.Broadcast( Device.ToSharedRef() );
if ( DefaultDeviceName.IsEmpty() )
{
DefaultDeviceName = DeviceName;
}
}
}
}
}
struct FBrowserLocation
{
FString Name;
FString Path;
} PossibleLocations[] =
{
#if PLATFORM_WINDOWS
{ TEXT("Nightly(64bit)"), TEXT("C:/Program Files/Nightly/firefox.exe") },
{ TEXT("Nightly"), TEXT("C:/Program Files (x86)/Nightly/firefox.exe") },
{ TEXT("Firefox(64bit)"), TEXT("C:/Program Files/Mozilla Firefox/firefox.exe") },
{ TEXT("Firefox"), TEXT("C:/Program Files (x86)/Mozilla Firefox/firefox.exe") },
{ TEXT("Chrome"), TEXT("C:/Program Files (x86)/Google/Chrome/Application/chrome.exe") },
#elif PLATFORM_MAC
{ TEXT("Safari"), TEXT("/Applications/Safari.app") },
{ TEXT("Firefox"), TEXT("/Applications/Firefox.app") },
{ TEXT("Chrome"), TEXT("/Applications/Google Chrome.app") },
#elif PLATFORM_LINUX
{ TEXT("Firefox"), TEXT("/usr/bin/firefox") },
#else
{ TEXT("Firefox"), TEXT("") },
#endif
};
// Add default devices..
for (const auto& Loc : PossibleLocations)
{
if (FPlatformFileManager::Get().GetPlatformFile().FileExists(*Loc.Path) || FPlatformFileManager::Get().GetPlatformFile().DirectoryExists(*Loc.Path))
{
FScopeLock Lock( &DevicesCriticalSection );
FHTML5TargetDevicePtr& Device = Devices.FindOrAdd( *Loc.Name );
if( !Device.IsValid() )
{
Device = MakeShareable( new FHTML5TargetDevice( *this, *Loc.Name, *Loc.Path ) );
DeviceDiscoveredEvent.Broadcast(Device.ToSharedRef());
}
}
}
}