Files
UnrealEngineUWP/Engine/Source/Editor/MaterialEditor/Public/MaterialStatsCommon.h
Jason Nadro b914ef3c2f Only compile the shaders needed to gather shader platform stats in the Material Editor.
# Results
Modfiying WorldGridMaterial with two platforms open in platform stats.
- We compile a fixed number of shaders (less than 10 per platform) now instead of the 1078 shaders (per platform) w/ WorldGridMaterial.
- On average ProcessCompiledShaderMaps is 40x faster. (1727ms vs. 43ms)
- In the worst cast ProcessCompiledShaderMaps is 10.9x faster. (4865ms vs 445ms)
- The material editor in this scenario goes from unusable to useable.

# Changes
- Add `FMaterial::CacheGivenTypes` to compile just the shader types given to it.
- Call `GetRepresentativeShaderTypesAndDescriptions` to gather the shader types we care about and only submit jobs to compile those shaders when generating platform stats.
- Since we are no longer compile a complete shader map the ShaderCount is incorrect.  Now we call `GetShaderTypes` of the `FMaterialResource` to gather the number of shaders in the material.  This function doesn't trigger shader compilation which is important.
- Adding const to the following functions:
    - FMaterial::GetShaderTypesForLayout
    - FMaterial::GetShaderTypes
- FMaterial::GetShaderTypes is now ENGINE_API so we can call it from the Material Editor.
- Add `TBasePassPSFCachedVolumeIndirectLightingPolicy` shader type to FMaterialStatsUtils::GetRepresentativeShaderTypesAndDescriptions.  This shader has the worst case sampler count.  This ensures we compile this shader and are able to query the worst case sampler count for the platform stats.  This is very ad-hoc, and could be improved in the future.

#rb Ben.Ingram
#jira UE-138623
#preflight 623b4e4cc3399da9533282cc

[CL 19481265 by Jason Nadro in ue5-main branch]
2022-03-23 13:03:01 -04:00

118 lines
4.2 KiB
C++

// Copyright Epic Games, Inc. All Rights Reserved.
#pragma once
#include "SceneTypes.h"
#include "RHIDefinitions.h"
#include "MaterialShared.h"
#include "Styling/SlateColor.h"
/** custom resource material class used to mark the resource as used for shader stats extraction */
class MATERIALEDITOR_API FMaterialResourceStats : public FMaterialResource
{
public:
FMaterialResourceStats() {}
virtual ~FMaterialResourceStats() {}
FORCEINLINE UMaterial* GetMaterial()
{
return Material;
}
/**
* Should shaders compiled for this material be saved to disk?
*/
virtual bool IsPersistent() const override { return false; }
/** Material resource stats never requires a synchronous compilation, otherwise opening up empty (newly created) material instance will block compiling default mat's shaders. */
virtual bool RequiresSynchronousCompilation() const override { return false; }
/** this will enable shader source extraction and pass paths to (eventual) offline shader compilers */
virtual void SetupExtaCompilationSettings(const EShaderPlatform Platform, FExtraShaderCompilerSettings& Settings) const override;
};
/** enumeration used to group shader platforms */
enum class EPlatformCategoryType : int32
{
Desktop,
Android,
IOS,
Console,
Num
};
/** enumeration containing the "types" of used shaders to display statistics */
enum class ERepresentativeShader
{
FirstFragmentShader,
StationarySurface = FirstFragmentShader,
StationarySurfaceCSM,
StationarySurfaceNPointLights,
DynamicallyLitObject,
UIDefaultFragmentShader,
LastFragmentShader = UIDefaultFragmentShader,
FirstVertexShader,
StaticMesh = FirstVertexShader,
SkeletalMesh,
UIDefaultVertexShader,
UIInstancedVertexShader,
LastVertexShader = UIInstancedVertexShader,
Num
};
/** class used for various stats utilities */
class FMaterialStatsUtils
{
public:
struct MATERIALEDITOR_API FShaderInstructionsInfo
{
ERepresentativeShader ShaderType;
FString ShaderDescription;
int32 InstructionCount;
};
struct MATERIALEDITOR_API FRepresentativeShaderInfo
{
ERepresentativeShader ShaderType;
FName ShaderName;
FString ShaderDescription;
FRepresentativeShaderInfo(const ERepresentativeShader _ShaderType, const FName _ShaderName, const FString& _StrDescription) :
ShaderType(_ShaderType), ShaderName(_ShaderName), ShaderDescription(_StrDescription)
{}
};
public:
/** call this to create an instance to FMaterialStats */
static TSharedPtr<class FMaterialStats> CreateMaterialStats(class IMaterialEditor* MaterialEditor);
/** utility functions that translate various enum values to strings */
static FString MaterialQualityToString(const EMaterialQualityLevel::Type Quality);
static FString MaterialQualityToShortString(const EMaterialQualityLevel::Type Quality);
static EMaterialQualityLevel::Type StringToMaterialQuality(const FString& StrQuality);
static FString GetPlatformTypeName(const EPlatformCategoryType InEnumValue);
static FString ShaderPlatformTypeName(const EShaderPlatform PlatformID);
/**
* Gets instruction counts that best represent the likely usage of this material based on shading model and other factors.
* @param Results - an array of descriptions to be populated
*/
static void GetRepresentativeInstructionCounts(TArray<FShaderInstructionsInfo>& Results, const class FMaterialResource* Target);
MATERIALEDITOR_API static void GetRepresentativeShaderTypesAndDescriptions(TMap<FName, TArray<FRepresentativeShaderInfo>>& OutShaderTypeNameAndDescriptions, const class FMaterial* TargetMaterial);
MATERIALEDITOR_API static void ExtractMatertialStatsInfo(EShaderPlatform ShaderPlatform, struct FShaderStatsInfo& OutInfo, const FMaterialResource* Target);
static FString RepresentativeShaderTypeToString(const ERepresentativeShader ShaderType);
static FSlateColor QualitySettingColor(const EMaterialQualityLevel::Type QualityType);
static FSlateColor PlatformTypeColor(EPlatformCategoryType PlatformType);
MATERIALEDITOR_API static bool IsPlatformOfflineCompilerAvailable(const EShaderPlatform ShaderPlatform);
MATERIALEDITOR_API static FString GetPlatformOfflineCompilerPath(const EShaderPlatform ShaderPlatform);
MATERIALEDITOR_API static bool PlatformNeedsOfflineCompiler(const EShaderPlatform ShaderPlatform);
};