Files
UnrealEngineUWP/Engine/Source/Developer/MaterialBaking/Private/MaterialBakingModule.h

91 lines
3.8 KiB
C
Raw Normal View History

// Copyright Epic Games, Inc. All Rights Reserved.
#pragma once
#include "CoreMinimal.h"
#include "Modules/ModuleInterface.h"
#include "PixelFormat.h"
#include "SceneTypes.h"
#include "IMaterialBakingModule.h"
#include "MaterialPropertyEx.h"
class UTextureRenderTarget2D;
class UMaterialOptions;
class UMaterialInterface;
class FExportMaterialProxy;
class FTextureRenderTargetResource;
struct FMaterialData;
struct FMeshData;
struct FBakeOutput;
class MATERIALBAKING_API FMaterialBakingModule : public IMaterialBakingModule
{
public:
/** IModuleInterface overrides begin */
virtual void StartupModule();
virtual void ShutdownModule();
/** IModuleInterface overrides end */
/** Bakes out material properties according to MaterialSettings using MeshSettings and stores the output in Output */
virtual void BakeMaterials(const TArray<FMaterialData*>& MaterialSettings, const TArray<FMeshData*>& MeshSettings, TArray<FBakeOutput>& Output) override;
/** Bakes out material properties according to extended MaterialSettings using MeshSettings and stores the output in Output */
virtual void BakeMaterials(const TArray<FMaterialDataEx*>& MaterialSettings, const TArray<FMeshData*>& MeshSettings, TArray<FBakeOutputEx>& Output) override;
/** Promps a slate window to allow the user to populate specific material baking settings used while baking out materials */
virtual bool SetupMaterialBakeSettings(TArray<TWeakObjectPtr<UObject>>& OptionObjects, int32 NumLODs) override;
/** Outputs true HDR version of emissive color */
virtual void SetEmissiveHDR(bool bHDR) override;
/** Bakes all material properties to linear textures, except for colors */
virtual void SetLinearBake(bool bCorrectLinear) override;
protected:
/* Creates and adds or reuses a RenderTarget from the pool */
UTextureRenderTarget2D* CreateRenderTarget(bool bInForceLinearGamma, EPixelFormat InPixelFormat, const FIntPoint& InTargetSize);
/* Creates and adds (or reuses a ExportMaterialProxy from the pool if MaterialBaking.UseMaterialProxyCaching is set to 1) */
FExportMaterialProxy* CreateMaterialProxy(const FMaterialDataEx* MaterialSettings, const FMaterialPropertyEx& Property);
Edigrating 3 CLs to improve HLOD generation time (faster material baking & mesh merging) CL 10373564 by danny.couture Optimize Material Baking (Phase 1) - Introduce a mecanism to override the vertex/index buffer allocator used for dynamic meshes - Avoid GDynamicMesh non-ticked pools build-up by using our own vertex/index buffer pool during baking - Reduce reallocation and incurred soft page faults by reusing a single set of vertex/index buffers big enough for the biggest mesh - Preemptively detect if smearing would result in monochrome texture to avoid useless work - Shrink smeared monochrome textures during the baking process for huge memory savings - Move UV smearing in worker threads to avoid blocking the game thread - Required shaders are now built asynchronously - Add progress bar for material baking - 28m23 [at] 150 GB RAM -> 2m14s [at] 45 GB RAM for 6 channels [at] 512x512 when baking materials on ProxyLOD for DATASET-0008a with DDC empty #rb Jurre.deBaare, Sebastien.Lussier CL 10516258 by danny.couture Optimize Material Baking (Phase 2) - Implement pipelining with staging buffers to avoid GPU stalls when reading from render targets - Reuse the same prepared FMeshBatch instead of rebuilding it for each draw pass - Prepare the RenderItem in advance on other threads to reduce work on the game thread - Move the staging surface copy out of the render thread - Small vertex and index buffers are not reused to avoid dependency locks when mapping them - Fix bug in Canvas Flush_RenderThread found while running HLOD rebuild commandlet on Fortnite - Delete old and unused MaterialBakingModule.h from public files - 4m44s -> 59s for baking 6 channel [at] 1024x1024 when baking materials on ProxyLOD for DATASET-0008a with shaders already compiled - Time spent in Material Baking when rebuilding all HLOD on Apollo_POI_Large_HLOD (Phase 1 + 2 combined) - 10m18s -> 2m36s for a first rebuild all in editor with no shaders in DDC (cold) - 1m23s -> 20s for a second rebuild all in editor (warm) #rb Jeremy.Moore, Sebastien.Lussier CL 11135986 by sebastien.lussier Optimized mesh merging * Added DeletePolygons() & DeleteTriangles methods to FMeshDescription which rely on TSets<> instead of performing costly TArray::AddUnique() calls() * Parallelized UV generation and avoided duplicate processing of the same mesh+lod pairs * Optimized FMeshDescriptionOperations::GenerateUniqueUVsForStaticMesh() * Goes from 100s to 10s in my test case #rb danny.couture, jeanfrancois.dube, richard.talbotwatkin #ROBOMERGE-OWNER: sebastien.lussier #ROBOMERGE-AUTHOR: sebastien.lussier #ROBOMERGE-SOURCE: CL 11206337 via CL 11206341 via CL 11206346 #ROBOMERGE-BOT: (v643-11205221) [CL 11206493 by sebastien lussier in Main branch]
2020-02-03 11:08:35 -05:00
/** Helper for emissive color conversion to Output */
static void ProcessEmissiveOutput(const FFloat16Color* Color16, int32 Color16Pitch, const FIntPoint& OutputSize, TArray<FColor>& Output, float& EmissiveScale);
Edigrating 3 CLs to improve HLOD generation time (faster material baking & mesh merging) CL 10373564 by danny.couture Optimize Material Baking (Phase 1) - Introduce a mecanism to override the vertex/index buffer allocator used for dynamic meshes - Avoid GDynamicMesh non-ticked pools build-up by using our own vertex/index buffer pool during baking - Reduce reallocation and incurred soft page faults by reusing a single set of vertex/index buffers big enough for the biggest mesh - Preemptively detect if smearing would result in monochrome texture to avoid useless work - Shrink smeared monochrome textures during the baking process for huge memory savings - Move UV smearing in worker threads to avoid blocking the game thread - Required shaders are now built asynchronously - Add progress bar for material baking - 28m23 [at] 150 GB RAM -> 2m14s [at] 45 GB RAM for 6 channels [at] 512x512 when baking materials on ProxyLOD for DATASET-0008a with DDC empty #rb Jurre.deBaare, Sebastien.Lussier CL 10516258 by danny.couture Optimize Material Baking (Phase 2) - Implement pipelining with staging buffers to avoid GPU stalls when reading from render targets - Reuse the same prepared FMeshBatch instead of rebuilding it for each draw pass - Prepare the RenderItem in advance on other threads to reduce work on the game thread - Move the staging surface copy out of the render thread - Small vertex and index buffers are not reused to avoid dependency locks when mapping them - Fix bug in Canvas Flush_RenderThread found while running HLOD rebuild commandlet on Fortnite - Delete old and unused MaterialBakingModule.h from public files - 4m44s -> 59s for baking 6 channel [at] 1024x1024 when baking materials on ProxyLOD for DATASET-0008a with shaders already compiled - Time spent in Material Baking when rebuilding all HLOD on Apollo_POI_Large_HLOD (Phase 1 + 2 combined) - 10m18s -> 2m36s for a first rebuild all in editor with no shaders in DDC (cold) - 1m23s -> 20s for a second rebuild all in editor (warm) #rb Jeremy.Moore, Sebastien.Lussier CL 11135986 by sebastien.lussier Optimized mesh merging * Added DeletePolygons() & DeleteTriangles methods to FMeshDescription which rely on TSets<> instead of performing costly TArray::AddUnique() calls() * Parallelized UV generation and avoided duplicate processing of the same mesh+lod pairs * Optimized FMeshDescriptionOperations::GenerateUniqueUVsForStaticMesh() * Goes from 100s to 10s in my test case #rb danny.couture, jeanfrancois.dube, richard.talbotwatkin #ROBOMERGE-OWNER: sebastien.lussier #ROBOMERGE-AUTHOR: sebastien.lussier #ROBOMERGE-SOURCE: CL 11206337 via CL 11206341 via CL 11206346 #ROBOMERGE-BOT: (v643-11205221) [CL 11206493 by sebastien lussier in Main branch]
2020-02-03 11:08:35 -05:00
/** Cleans up all cached material proxies in MaterialProxyPool */
void CleanupMaterialProxies();
/** Callback for modified objects which should be removed from MaterialProxyPool in that case */
void OnObjectModified(UObject* Object);
/** Callback used to clear material proxy cache on garbage collection */
void OnPreGarbageCollect();
private:
enum EPropertyColorSpace
{
Linear,
sRGB,
};
/** Pool of available render targets, cached for re-using on consecutive property rendering */
TArray<UTextureRenderTarget2D*> RenderTargetPool;
/** Pool of cached material proxies to optimize material baking workflow, stays resident when MaterialBaking.UseMaterialProxyCaching is set to 1 */
typedef TWeakObjectPtr<UMaterialInterface> FMaterialPoolKey;
typedef TPair<FMaterialPropertyEx, FExportMaterialProxy*> FMaterialPoolValue;
typedef TMultiMap<FMaterialPoolKey, FMaterialPoolValue, FDefaultSetAllocator, TWeakObjectPtrMapKeyFuncs<FMaterialPoolKey, FMaterialPoolValue, true /*bInAllowDuplicateKeys*/>> FMaterialPoolMap;
FMaterialPoolMap MaterialProxyPool;
/** Pixel formats to use for baking out specific material properties */
TMap<FMaterialPropertyEx, EPixelFormat> PerPropertyFormat;
/** Whether or not to enforce gamma correction while baking out specific material properties */
TMap<FMaterialPropertyEx, EPropertyColorSpace> PerPropertyColorSpace;
EPropertyColorSpace DefaultColorSpace;
bool bEmissiveHDR;
};