Files
UnrealEngineUWP/Engine/Source/Developer/MaterialBaking/Private/MaterialBakingModule.cpp
Chris Bunner 337291cb1d Copying //UE4/Dev-Rendering to //UE4/Dev-Main (Source: //UE4/Dev-Rendering @ 3760894)
#rb Rendering
#lockdown Nick.Penwarden

============================
  MAJOR FEATURES & CHANGES
============================

Change 3658809 by Chris.Bunner

	Changing default HDR display gamut to P3 as in practice that's more common than Rec2020, this should be a user-facing option where possible though as we can't automatically retrieve that data.

Change 3658842 by Chris.Bunner

	Backing out previous HDR default gamut change as it conflicts with mandatory platform defaults.

Change 3695269 by Arne.Schober

	DR - Make clang happy wreorder

Change 3695418 by Guillaume.Abadie

	Fixes compilation failure in FoliageType_InstancedStaticMesh.cpp.

Change 3695430 by Guillaume.Abadie

	Fixes missing BeginFrame dynamic resolution event in EngineTest.

Change 3695469 by Guillaume.Abadie

	Fixes crash when passing down an invalid parameter on the sample material expression's DDX, DDY parameters.

Change 3696091 by Guillaume.Abadie

	Fixes Linux compilation failure in DynamicResolution.cpp

Change 3696593 by Chris.Bunner

	Fixed typo in vetex factory enum.

Change 3696596 by Chris.Bunner

	Added material attributes type checking to If material expression.
	Updated If material expression to validate compilation of inputs.

Change 3696597 by Chris.Bunner

	Allow visible parameter retrieval to correctly traverse through internally called functions. Previous check was intended to prevent function previews from leaving their graph through unhooked inputs, but unintentionally blocked all function inputs.

Change 3696599 by Chris.Bunner

	Fixed material instance parameter visiblity when using nested static switches across functions.

	#jira UE-50878

Change 3696734 by Chris.Bunner

	Return type fix.

Change 3697123 by Guillaume.Abadie

	Fixes compilation failure in PostProcessWeightedSampleSum.cpp on Windows 32bits.

Change 3697125 by Guillaume.Abadie

	Fixes compilation failure in MaterialExpressionIf.h

Change 3697127 by Guillaume.Abadie

	Fixes compilation failure in DynamicResolution.cpp on shipping build.

Change 3697135 by Guillaume.Abadie

	Fixes crash in dynamic resolution event frontend when resizing game play viewport in EngineTest.

Change 3697199 by Guillaume.Abadie

	Fixes TAA upsample's shader compilation failure on Mac.

Change 3697220 by Guillaume.Abadie

	Makes static analysis happy again.

Change 3697280 by Chris.Bunner

	Fixing up invalid casts in material layers validation.

Change 3697366 by Rolando.Caloca

	DR - hlslcc - Fix warning

	#jira UE-43988

Change 3697451 by Rolando.Caloca

	DR - vk - Per pipeline descriptor pools
	Descriptor pool are now allocated per PSO instead of globally to reduce peak mem consumption and fragmentation
	Enabled on Windows only via VULKAN_USE_PER_PIPELINE_DESCRIPTOR_POOLS

Change 3697477 by Rolando.Caloca

	DR - vk - Custom memory allocator
	Remove old/unused stats

Change 3697486 by Rolando.Caloca

	DR - vk - Fix validation issue

Change 3697488 by Richard.Wallis

	Fix for Mac editor session no longer accurately tracking Mouse location after moving between Desktops in Mission Control on 10.12.6.  Problem stems from the extra call to update the slate cached window position in mouse move while dragging, which is itself is a hack but apparently this is needed as we don't get window position updates on Mac while dragging (although I couldn't see any -ve side effects without it) then the OS (in 10.12.6) doesn't always push out a final window did move notificaiton when changing desktops which leaves the window according to slate incorrectly positoned to it's frame.

	Solution is to either remove the mouse-move-while-drag window position hack or add a final window position update to the mouse-up event while dragging - this change is the latter.

	#jira UE-37553

Change 3697501 by Richard.Wallis

	Move audio processing over to audio bus tap.  Currently on Mac Media audio playback uses OS media player mixer rather than Engine as existing implemtation, using AVAssetReader, now suffers from poor performance with new Media Framework.  This audio tap version replaces that asset reader implementation but also suffers from bad quality audio hence is still disabled at the top of AvfMediaTracks.cpp.

	Original Code Review Description:

	Convert Mac to Play audio through the engine MediaFramework API rather than using AVMediaPlayer.  This is the Mac implementation only - this should work ok on iOS but unable to test due to missing audio type implementation (throws error "Init Buffer on unsupported sound type name = Synth type = 5"), as such is only enabled for Mac.  There maybe some extra tweaks required for iOS on app backgrounding etc if this feature is enabled.

	- Stuttering Audio Performace issue investiagation:  Re-Tested this implementation against [now fixed] current Mac implementation which was working fine last year and that implementation now has the same audio output quality (performance) issues as this one.  Basic investigation seems to point to somewhere in the engine audio handing.  When poor audio is heard the FMediaAudioResampler::Generate() function is dequeing an IMediaAudioSample sample buffer and the audio sample queue usually has 50-100 of these IMediaAudioSample buffers waiting in the queue.  I think the AvfMedia playback system is providing the sample buffers in good time but they are not getting consumed "fast" enough.  This under consuming also occurs if I force the Core Audio - Audio Unit mixer to use 48000 samples/sec.

	#jira UEPLAT-1677

Change 3697517 by Richard.Wallis

	XCode 9.0 extra nullability specifiers required.

Change 3697537 by Richard.Wallis

	Back out revision 23 from //UE4/Dev-Rendering/Engine/Plugins/Media/AvfMedia/Source/AvfMedia/Private/Player/AvfMediaTracks.cpp

Change 3697670 by Rolando.Caloca

	DR - vk - Fix mapstaging surface

Change 3697846 by Uriel.Doyon

	Allow denormalized values when converting float32 to float16.

Change 3697892 by Uriel.Doyon

	Fix for unaligned structure elements

Change 3699335 by Richard.Wallis

	Mac compile fix - turns out I did need these nullability specifiers here.

Change 3699663 by Guillaume.Abadie

	Fixes time unit conversions from microseconds to milliseconds error in dynamic resolution heuristic when using GPU busy time queries.

Change 3699959 by Rolando.Caloca

	DR - Fix barrier in the middle of render pass

Change 3699969 by Rolando.Caloca

	DR - vk - Change dump layer location so it prints out validation ids

Change 3700356 by Guillaume.Abadie

	Implements secondary screen percentage to be able to do TAA upsample followed spatial upscale so that the editor viewport still have same TAA upsample screen percentage range to test the content with no matter monitor's DPI.

Change 3701105 by Guillaume.Abadie

	Ignore per view automatic mip bias on texture type other than 2d textures.

	#jira UE-51396

Change 3702297 by Richard.Wallis

	Mac compile fix for nullable specifier.  Looks like Obj class using the C++ class also needs this otherwise it throws.  Seems to be some kind of xcode/compiler caching bug with this stuff as it'll report the error once then on subsequent compiles say everything is ok.

	#jira UE-51386

Change 3702357 by Richard.Wallis

	Mac nullability compile fix - again.  Looks like I fell foul of that xcode compile caching!
	#jira UE-51386

Change 3702424 by Guillaume.Abadie

	Fixes planar reflection from drowing themselves in their own FSceneRenderer in forward shading.

	#jira UE-51395

Change 3702464 by Guillaume.Abadie

	Fixes wrong viewport to buffer conversion of the distortion.

	#jira UE-51406

Change 3702819 by Guillaume.Abadie

	Fixes planar reflections with secondary screen percentage for HighDPI editor viewports.

Change 3703732 by Guillaume.Abadie

	Removes unecessary check(); when there is more than 2 players with planar reflections.

	#jira UE-51436

Change 3704302 by Guillaume.Abadie

	Removes unecessary Interface suffix on new dynamic resolution related interfaces

Change 3704390 by Chris.Bunner

	Fixed a coincidentally correct define.

Change 3704730 by Rolando.Caloca

	DR - vk - Fix map for depth surfaces

Change 3704739 by Rolando.Caloca

	DR - Debug label on D3D11 UAVs
	- Validate when running -d3debug

Change 3705000 by Chris.Bunner

	Skip compiling opacity and opacity mask inputs on opaque surface materials. Previously the code was always added to the shader, sometimes we force opaque materials down a masked path which then calls the dormant code unintentionally. A safer fix for UE-48254.
	Partially reverted previous fix in CL 3608303 which removed a material instance optimization caching the overridden base properties.

Change 3706065 by Guillaume.Abadie

	Does some renaming for primary screen percentage, and move the primary screen percentage method selection from dynamic resolution driver to FSceneView.

Change 3706464 by Chris.Bunner

	Fixed material property translate overrides that were generating code in the wrong entry.
	Fixed conditions  in If material expression GetInputType and IsMA check.

	#jira UE-51368

Change 3706641 by Chris.Bunner

	Missing "break" in switch statement (which unfortunately needs another bump to resolve).

Change 3706642 by Guillaume.Abadie

	Fixes assertion failure when r.TemporalAA.EnableUpscale = 1

Change 3706650 by Gil.Gribb

	UE4 - UE4 - Changes from intel. Increase number of worker threads on Windows to if hyperthreads (hyperthreads √ 2) else cores √ 1 up to a max of 22 workers. Increase MAX_THREADS multiplier per bank from 22 to 26. Intel VTune ITT event annotations. Wrapped in same function as your existing CPU events and enabled with √vtune. Optimize NV cloth by consuming FVector instead of FVector4 out of the solver. Vertex buffers were using FVector all along. ~15% improvement. Optimize cloth copy to vertex buffer by adding prefetch (similar to how bones are already done). Move local to world cloth transform from CPU to GPU. When simulating lots of vertices game thread was becoming bottleneck doing matrix multiply. Add your TaskGraph task switch latency test code.

Change 3706733 by Daniel.Wright

	Print Embree Build time

Change 3706841 by Daniel.Wright

	EmbreeFilterFunc4 now handles masked out intersections properly

Change 3707437 by Rolando.Caloca

	DR - vk - Android compile fix

	#jira UE-51474

Change 3707785 by Guillaume.Abadie

	Fixes viewport issue in bloom setup pass with TAA upsample.

Change 3709623 by Rolando.Caloca

	DR - vk - Missing barrier for reading into cpu

Change 3709633 by Rolando.Caloca

	DR - vk - Compile fix

Change 3710454 by Mark.Satterthwaite

	Refactor the way we compile Buffer<> & RWBuffer<> types for Metal so that we can support the type-conversion semantics of HLSL/D3D.
	- Buffer<> types are converted to Linear Textures unless the internal type is 3-compnent or the STRONG_TYPE macro is added as a type-qualifier. Linear Textures require an MTLTexture "view" object be created around the MTLBuffer which is the backing-store and it is typically best if that buffer is marked as Private (GPU-only) memory, reading from this in the shader then uses the texture-fetch hardware to perform the format conversion on load.
	- RWBuffer<> & 3-compnent Buffer<> types are converted to use template functions to load/store - the implementation of which will read the format from the BufferSizes meta-table and determine which type-conversion to apply. Function-constants are used to specialise the shader where feasible to reduce branch costs (function-constants are a Metal feature that allow efficient runtime recompilation of bytecode shaders).
	- Buffer<> & RWBuffer<> types where the STRONG_TYPE macro is added as a type-qualifier (only does something on Metal, everywhere else it is #define'd away) are compiled as "raw" Metal buffers of the inner-type (e.g. float4 for Buffer<float4>) and the MetalRHI runtime will enforce that only SRVs/UAVs of the proper format are bound to it. This is necessary in a couple of cases (BoneMatrices, NumCulledLightsGrid, CulledLightDataGrid & ForwardLocalLightBuffer buffers) which are used in a larger number of shaders as Linear Textures have poorer performance than Buffer<>/RWBuffer<>.
	- Most of the complications to generating subtly different Metal code for different OS/device combinations have been factored out into ue4_stdlib.metal which acts as an extension to the Metal shader standard-library and helps simplify the MetalBackend code - particularly helpful for Buffer<>/RWBuffer<> but also texturecube_array and the SM6 wave-related intrinsics.
	- Reverted some of the awkward Metal-specific changes Richard.Wallis & Arne had to make to the high-level shaders as they aren't necessary anymore.
	- Made the existing Metal-specific changes to use uint32 for all light-grid injection buffers apply to all Metal platforms again (I had hoped that it would not be necessary anymore, but it is much faster this way).
	- STRONG_TYPE is actually hlslcc's "invariant" keyword applied as a type-qualifier to a Buffer<>/RWBuffer<> type - only valid when using Metal which exports this through ILanguageSpec and #define'd out for everyone else.
	- Old versions of iOS (anything earlier than iOS 10.3) won't be able to use this new code, so every buffer will be treated as "raw" and the MetalRHI will now properly report when something goes awry rather than it leading to mysterious rendering errors and crashes.

Change 3710456 by Mark.Satterthwaite

	Fix the Eddie workset project generator so that Enterprise projects don't get mixed in with regular projects at the top-level because of the way Eddie combines workset groups.

Change 3710457 by Mark.Satterthwaite

	DX11 texture formats for Mac Metal please!

Change 3710480 by Mark.Satterthwaite

	Permit RHI thread and parallel execution in Mac -game mode again.

Change 3710522 by Mark.Satterthwaite

	MSVC type-mismatch error fixes.

Change 3710580 by Mark.Satterthwaite

	Alright then - if I can't use the C++11 extended string semantics I'll have to use "xxd -i" to generate a hex-dump include header from ue4_stdlib.metal instead. This can only be updated from a machine with access to the POSIX xxd command (Mac & Linux, possibly the new Linux sub-system for Win10).

Change 3710616 by Mark.Satterthwaite

	Missing file.

Change 3712972 by Guillaume.Abadie

	Fixes Circle DOF's negative alpha channel getting clamped to 0 in TAA pass.

Change 3712979 by Guillaume.Abadie

	Fixes wrong RT reallocation when doing TAA upsample in editor viewports with secondary upscale.

Change 3713406 by Mark.Satterthwaite

	Use GPU morph targets on Mac - the necessary buffer conversions will always be available there. For iOS it can only be supported if iOS 10 is the minimum OS & Metal standard so leave that on the CPU path for now.

Change 3713494 by Richard.Wallis

	Fix for hitch when PIE unloading sublevel.  PerformReachabilityAnalysisOnObjects is spawing multiple threads in Editor builds as there is an extra code path that results in Critical Section locking within a singleton type static object - this is a bottle neck for multiple threads.  However they all just need to read the data not change it.  Replaced FScopeLock with a Read/Write version allowing these threads to all take a read lock at the same time to reduce contention.

	Changed the FUObjectAnnotationDense implementation only - left the sparse implementation alone as its not currently affecting this - although we could proactivly change that too.

	Also tested again repro in linked bug UE-24711.

	#jira UE-40533

Change 3713612 by Mark.Satterthwaite

	Integrate LPV_STORE_INDEX_IN_HEAD_BUFFER related changes from //depot/Partners/Microsoft/UE4-MS/Engine-Fable @ 2954744

	This should make Light Propagation Volumes potentially viable on non-Microsoft platforms.

Change 3713623 by Mark.Satterthwaite

	Implement ByteAddressBuffer/RWByteAddressBuffer in hlslcc in a similar manner to StructuredBuffer/RWStructuredBuffer so that the backends don't need too much modification. Implement the necessary changes into MetalBackend to make this work for Metal.

	Load/Store{+2,3,4} & Atomics are supported. Counter operations are not supported and aren't likely to be.

Change 3713636 by Mark.Satterthwaite

	Enable LPVs for Mac Metal.
	- Rework some multi-dimensional arrays & array-index dependent HLSL code that hlslcc simply can't cope with, the mesa-glsl compiler core is only capable of dealing with 1 dimensional arrays and array-indexing can't itself be directly dependent on the result of an array-index operation.
	- MetalRHI needs to ignore any SetRenderTargets call that binds nothing at all as you must bind at least one target (UAV, RT, Depth/Stencil) for it to be able to do anything sensible.
	- Turn on LPVs for Metal as it works now.

Change 3714049 by Guillaume.Abadie

	Do not set screen percentage method to TAA upsample when anti aliasing method is not TAA even if there is automatic fallback in the renderer.

Change 3714306 by Guillaume.Abadie

	Fixes assertion failure in dynamic resolution state proxy with GPU busy time queries.

Change 3714714 by Mark.Satterthwaite

	Tweak Metal GPU identification so that it works with eGPU boxes and protoype hardware - these changes only apply to macOS 10.13 so the system as a whole remains.

Change 3716104 by Mark.Satterthwaite

	Fix 10.12/Xcode 8 compile errors from the build-farm which is still split until Fortnite can update.

Change 3716120 by Mark.Satterthwaite

	Silence static-analysis.

Change 3716158 by Guillaume.Abadie

	Rewrites editor primitive compositing to support TAA upsample.

	This takes the oportunity to remove the manual depth testing in base pass pixel shader of editor primitives.

Change 3716271 by Daniel.Wright

	Lightmass correctness fixes
	* After these changes, point, spot, directional and sky lights closely match reference renderer Mitsuba after light unit conversions
	* Photon density trimming intended for direct photons was affecting indirect photons as well.  This caused high noise for point / spot lights with a large attenuation radius.  Indirect photon density even for small lights is 5x with this change, which improves 2nd bounce quality.
	* Removed legacy fudge factor on point / spot light photon energy
	* Spotlights no longer emit based on indirect photon paths.  Fixes excessive photon energy from spot lights as they were emitting outside of the cone.
	* Fixed photons computing one more bounce than requested.
	* Added an option to use the Radiosity solver for all multibounce, replacing photons.  Useful as a reference but generally too much noise indoors.
	* Fixed visualization of photons without final gather

Change 3716434 by Mark.Satterthwaite

	Backout the remaining change from 3632041 that is no longer necessary - this was the last of the 4.18 Metal workarounds.

Change 3716491 by Chris.Bunner

	Fixing up an edge-case on a recent optimization.

Change 3716611 by Guillaume.Abadie

	Allows secondary screen percentage >= 100%.

Change 3716977 by Guillaume.Abadie

	Back out changelist 3716158 to unblock QA pass.

	#jira UE-51580

Change 3717111 by Arne.Schober

	Fixing nomalization of Morph Tangents https://udn.unrealengine.com/questions/392462/
	Also implemanted batching of the dispatches which should help worst case perfomance where dispatches become too small.
	CalculateInverseAccumulatedWeights is not cheap and proably should be moved onto a task thread that runs as soon as the input weights are ready.

Change 3717127 by Mark.Satterthwaite

	Fix a mismerge from the reversion of 3632041 - part of the modified code had been moved into another file and I didn't initially notice.

Change 3717178 by Mark.Satterthwaite

	Remove useless copy-pasted expressions from glsl_type::GetByteAddressBufferInstance & force MetalBackend to relink. Apparently the previous Mac libs were mysteriously broken.

	#jira UE-51583

Change 3717476 by Marcus.Wassmer

	Fix PS4 compile.  funciton local statics not allowed on PSSL
	Also enabled the new atomics method for LPVs for all platforms

Change 3717502 by Arne.Schober

	DR - Compiletime option for compressed ruleset (0.02ms perf gain on PS4 and disabled by default as it limits array size to 2million entries)

Change 3717601 by Arne.Schober

	DR - Move cycle counter into more meaningfull locations.

Change 3718054 by Guillaume.Abadie

	Removes unecessary check() failure on secondary upscale that fires when testing raw output screen percentage method.

Change 3718066 by Guillaume.Abadie

	Reland: Rewrites editor primitive compositing to support TAA upsample.

	This takes the oportunity to remove the manual depth testing in base pass pixel shader of editor primitives.

Change 3718589 by Mark.Satterthwaite

	Console-variable to enable and disable Manual-Vertex-Fetch for Metal and fix the internal code to handle the subtle changes in behaviour for vertex-declarations so we don't explode under the Metal validation layer. MVF works on macOS, though testing did expose an error with Tessellation on Nvidia (true for MVF enabled & disabled).

Change 3718633 by Guillaume.Abadie

	Fixes temporal instability issue of TAA upsample with secondary screen percentage.

Change 3718658 by Arne.Schober

	DR - 25% MorphTarget Speed increase because there was a bit of cache thrashing between the waves going on.

Change 3718818 by Mark.Satterthwaite

	Fix compilation on hlslcc - integral values are not automatically converted into comparisons with zero.

Change 3719004 by Guillaume.Abadie

	Lets the game viewport client automatically set raw output screen percentage method when doing dynamic resolution with stereo rendering but without TAA upsample.

Change 3719375 by Mark.Satterthwaite

	Extend mtlpp compiler testing app to support Metal tessellation compute shaders so we can send Nvidia a much simpler reproduction of their regression.

Change 3720099 by Mark.Satterthwaite

	Make the left-hand arguments work in airdiff.

Change 3720413 by Mark.Satterthwaite

	Support standalone compute shaders in the mtlpp compiler test app.

Change 3721232 by Mark.Satterthwaite

	No more Metal Shader Model 4 - instead we have to have a Metal Shader Model 5 w/o Tessellation as Nvidia's shader compiler is broken on all tessellation shaders in 10.13.0 and above. There is no guarantee that they will fix this prior to 10.14 and I can't afford to disable tessellation entirely as if I do that then the AMD & Intel compilers will also regress. As there is no Shader Model 4 platform on Mac anymore I've amended the LevelEditorActions to disable the preview modes when no appropriate shader platform is available.

Change 3721244 by Mark.Satterthwaite

	Fix incorrect enum handling for Metal features due to overflow.

	#jira UE-51643

Change 3721338 by Mark.Satterthwaite

	MIssing file from 3721232

Change 3721818 by Mark.Satterthwaite

	Fix the Intel vector-array-dereference workaround so that it doesn't cause the AMD compiler to explode instead.

Change 3722139 by Arne.Schober

	DR - [UE-51602] -Fixed Typo that accidently bound the LightingInstancebuffer to the Transform one

	#jira UE-51602

Change 3722165 by Rolando.Caloca

	DR - Default -opengl to GL4

Change 3722682 by Guillaume.Abadie

	Fixes wrong clear color in SSR important for VR that has a HMD mesh.

Change 3722766 by Rolando.Caloca

	DR - Fix static analysis

Change 3722943 by Mark.Satterthwaite

	Disable the METAL_SM5_NOTESS shader platform again - I can workaround the Nvidia pipeline state compiler crash by changing the buffer address space from "constant" to "device" as we're managing to confuse the poor thing. This won't materially affect AMD or Intel as they don't care much about this, but to limit performance issues on Nvidia we only need to do this for Tessellation Compute shaders.

Change 3723100 by Mark.Satterthwaite

	Apparently users like enabling Metal shader standards that won't work on their current OS, so don't display those that aren't going to work & display an error message before quitting rather than crashing when trying to load a project that tries to use an incompatible shader version.

Change 3723121 by Mark.Satterthwaite

	Fix build error.

Change 3723245 by Daniel.Wright

	Ensure for when a reflection capture upload fails due to incorrect lighting scenario level handling
	Reflection captures with no data use an array index of 0, instead of -1.  Might avoid reading uninitialized memory on PS4.

Change 3723387 by Arne.Schober

	DR - Metal already applies the instance and vertexoffset in the shader

Change 3723393 by Mark.Satterthwaite

	More fixes to the mtlpp compiler test application.

Change 3725258 by Guillaume.Abadie

	Improves fast TAA upsample shader permutation by 15% on console.

Change 3725555 by Chris.Bunner

	[Dupliate] CL 3725548 - Fixed invalid screenpercentage value in VehicleGame sample (was setting -1 but should default to 100). This has always been broken but was recently exposed by CL 3686200.

Change 3726845 by Guillaume.Abadie

	Exposes SvPosition to material through screen position material expression, so that material no longer have SvPosition * InvViewSize * ViewSize precision loss.

	#jira UE-51428

Change 3728014 by Guillaume.Abadie

	Uses ScreenPosition material expression's PixelPosition pin in existing engine functions to improve precision.

	#jira UE-51428

Change 3728053 by Richard.Wallis

	Duplicate CL 3727958: Crash fix when using shared material libraries.  Initial shader code library offset is not zero'd so all entry offsets were garbage.

Change 3728339 by Guillaume.Abadie

	Adds project setting for TAA upample, and officialises TAA upsampling CVar.

Change 3728549 by Guillaume.Abadie

	CsvProfiler is pretty cool, but even better with console autocompletion for lazy developers.

Change 3728752 by nick.bullard

	Built and re-saved QA-MeshPaint
	#jira UE-50978

Change 3728775 by Guillaume.Abadie

	Implements r.DynamicRes.ChangePercentageThreshold to stabilize primary screen percentage.

Change 3729224 by Uriel.Doyon

	Hidden levels now keep their last build data when using lighting scenarios.
	Hidden levels don't affect the scene anymore volumetric lighting when not using lighting scenarios.

	#jira UE-40454
	#jira UE-38131

Change 3729243 by Marcus.Wassmer

	Update Ansel to 1.4
	#github 4159
	#jira UE-51545

Change 3729325 by zachary.wilson

	Adding indirect lighting to TM-LightingChannels
	#jira UE-47069

Change 3729485 by zachary.wilson

	Fixing ambient occlusion bias on QA-LightsStationary. Removed global PPV with bad settings, also fixed the shadow on the roof.
	#jira UE-50972

Change 3729629 by Uriel.Doyon

	Fixed crash when using debug view modes.
	Fixed d3ddebug error when clearing quad overdraw buffer.

	#jira UE-51836

Change 3730053 by Guillaume.Abadie

	Allows edititing of AScreenshotFunctionalTestBase::ScreenshotCamera.

Change 3730308 by Guillaume.Abadie

	Disables TAA upsample on buffer visualization, and disallow screen percentage preview in editor viewport with any buffer visualization.

Change 3730355 by Guillaume.Abadie

	Sacrifices consistency for good cvar name for TAA upsample.

Change 3731403 by Daniel.Wright

	Reduced slider for ContactShadowLength to .1, algorithm produces poor results with larger values.

Change 3731404 by Daniel.Wright

	Checkpoint for ScreenShadowMaskTexture, allowing 'vis ScreenShadowMaskTexture'

Change 3731407 by Daniel.Wright

	Must opt-in for FDistanceFieldSceneData::VerifyIntegrity

Change 3731517 by Guillaume.Abadie

	Freezes dynamic resolution heuristic when doing pause.

Change 3732168 by Guillaume.Abadie

	Renames TAA upsampling cvar.

Change 3732295 by Guillaume.Abadie

	Lets the scene texture's size and texel size return the correct sizes after TAA upsample.

Change 3732313 by Guillaume.Abadie

	Implements SceneTexture material expressions' automated tests.

Change 3734928 by Guillaume.Abadie

	Adds panic mode when the last N frames are over budget to the dynamic resolution heuristic.

Change 3735966 by Ryan.Vance

	Fixing mac steamvr compile issue. Missed a few lines in the refactor because mac.

Change 3736104 by Guillaume.Abadie

	Removes FSceneViewInitOptions::bDisableGameScreenPercentage brought by 4.18, that new screen percentage API do in a better way.

Change 3736346 by Daniel.Wright

	Volumetric fog is always interpolated in the pixel shader, since per-vertex interpolation gives consistently poor results.  Fixes Volumetric Fog on opaque in Forward, and on transparent in Deferred.
	Forward shading: per-pixel height fog is always done in the base pass, to work with MSAA correctly

Change 3736348 by Daniel.Wright

	Forward shadowing of directional light for translucency
	 * Static shadowing and CSM supported with minimal filtering (1 PCF)
	 * Deferred renderer: affects translucency using 'Surface ForwardShading' lighting mode.  Forward renderer: affects all translucency.

Change 3736650 by Rolando.Caloca

	DR - vk - # of desc pools

Change 3737985 by Guillaume.Abadie

	Fixes pixel inspector with primary and secondary screen percentage.

Change 3738638 by Michael.Lentine

	Compile fix due to unclear operator precendence.

Change 3739417 by Daniel.Wright

	Fixed a few issues with irradiance cache visualization

Change 3739447 by Daniel.Wright

	Skip forward static shadowing in projects with static lighting disabled

Change 3739595 by Daniel.Wright

	ConditionalPostLoad DistanceFieldReplacementMesh.  Should fix a crash on load when static mesh derived data is being rebuilt, and the DistanceFieldReplacementMesh is in use.

Change 3739598 by Daniel.Wright

	Disable capsules shadows on lowest shadow quality

Change 3739611 by Daniel.Wright

	Added r.CapsuleDirectShadows and r.CapsuleIndirectShadows for more specific scalability control over capsule shadow features
	New Lighting Feature show flags for RTDF shadows and Capsule Shadows

Change 3740516 by Guillaume.Abadie

	Fixes VR editor rendering only on eye with TAA upsample.

	#jira UE-52016

Change 3740580 by Guillaume.Abadie

	Fixes chromatic aberration with TAA upsample and multiple view rendering.

	#jira UE-51993

Change 3740588 by Guillaume.Abadie

	Gives to FXAA a more explicit draw event name for easier UDN support.

Change 3740845 by Michael.Lentine

	Fix shipping build.

Change 3740903 by Guillaume.Abadie

	Disables dynamic resolution threading outliers detection by default and includes editor UI GPU cost within dynamic resolution's begin/end frame events for better reliability of timestamp query based dynamic res in editor.

Change 3741355 by Daniel.Wright

	Normalize planar reflection plane - fixes crash when scaling a BP with a planar reflection component

Change 3741357 by Daniel.Wright

	More info on volumetric lightmap import failure

Change 3742535 by Ryan.Vance

	Fix for view rect changes.

Change 3743282 by Guillaume.Abadie

	Fixes a bug in dynamic resolution heuristic's outlier detection that was preventing the over budget panic to react.

Change 3743559 by Michael.Lentine

	Port Siren changes for recompute tangents. This adds recompute tangents for cloth as well as the ability for recompute tangents to work across seams where vertices are duplicated.

Change 3743679 by Guillaume.Abadie

	Cherry-pick 3743621: Fixes subsurface profile fallback to lit shading model when Opacity == 0, introduced by 3447144.

	#jira UE-51569

Change 3743906 by Ryan.Brucks

	BlueprintMaterialAndTextureNodes Plugin:  Fix for clamping sampled HDR render target values by setting  ERangeCompressionMode in the FReadSurfaceDataFlags to RCM_MinMax

Change 3744096 by Ryan.Brucks

	BlueprintMaterialAndTextureNodes Plugin:  removed Mip option from Texture2D_SampleUV_EditorOnly for now since reads from source data cannot access mips and it can be misleading.

Change 3744253 by Guillaume.Abadie

	Fixes merge collisions of debug canvas rendering with High DPI, fixes stat unit on high DPI monitors, and fixes secondary screen percentages.

Change 3744953 by Chris.Bunner

	Crash workaround.

Change 3745628 by Marcus.Wassmer

	Temporarily disable recalctangent normal-smoothing

	#jira UE-52166

Change 3745942 by Guillaume.Abadie

	Fixes a todo in FCommonViewportClient

Change 3746005 by Guillaume.Abadie

	Fixes stat UnitGraph on high DPI monitor.

Change 3746029 by Guillaume.Abadie

	Oups.... Fix compilation. :D

Change 3748322 by Guillaume.Abadie

	Shows dynamic resolution's primary screen percentage on stat unit/unitgraph console commands.

Change 3748346 by Chris.Bunner

	Potential static analysis fix.

Change 3748349 by Chris.Bunner

	Mac feature support flag fix on versions < 10.30.

Change 3749336 by Guillaume.Abadie

	Fixes some spelling mistakes in dynamic resolution cvars. Thanks Daniel!

Change 3749374 by Guillaume.Abadie

	Adds a black background on the stat unitgraph so timing curves can be seen no matter the content.

Change 3749437 by Guillaume.Abadie

	Final UI polish up for `stat unitgraph`

Change 3749719 by Guillaume.Abadie

	Fixes a crash when changing r.DynamicRes.MaxScreenPercentage below current screen percentage.

Change 3750243 by Chris.Bunner

	Increasing controller's automated test timeout to allow for slower machines to complete the longest tests.

	#jira UE-48494, UE-51907

Change 3750728 by Guillaume.Abadie

	Fixes merge collision in chromatic aberration.

	#jira UE-52282

Change 3750791 by Guillaume.Abadie

	Fixes chromatic baerration R and G channel swap.

Change 3751246 by Guillaume.Abadie

	Bypasses screen percentage apply with mobile LDR rendering.

	#jira UE-52089

Change 3752624 by Guillaume.Abadie

	Simplies dyn res state's event interface to a single virtual method.

Change 3753766 by Chris.Bunner

	Rebuilt volumetric baked lighting test map and updated screenshots.

	#jira UE-52322

Change 3755108 by Guillaume.Abadie

	Fixes a bug where default dynamic resolution state was created at startup of server build.

	#jira UE-52345

Change 3755267 by Mark.Satterthwaite

	Fix condition controlling which features are enabled when iOS >= 10.3 - it wasn't working for iOS 11+ which was causing all kinds of problems.

	#jira UE-52301

Change 3755811 by Chris.Bunner

	Disable some new logging that was causing a stack overflow during EnginePreInit.

	#jira UE-52345

Change 3756983 by Mark.Satterthwaite

	Prevent different versions of metal_stdlib/ue4_stdlib from causing shader compilation failures due to a time-stamp mismatch between the local file & the PCH. This can happen when working with Xcode Beta releases that change the modification date, but not the content or compiler version, amongst other possibilities.

	#jira UE-52073

Change 3757156 by Guillaume.Abadie

	Fixes editor compositing with wireframe rendering.

	#jira UE-52017

Change 3757435 by Mark.Satterthwaite

	Workaround a bug in the MobileSceneCaptureRendering where it was copying the ViewInfo's ViewRect prior to it being configured by the mobile renderer.
	#jira UE-52327

Change 3757523 by Uriel.Doyon

	Fixed d3ddebug warning with unused inputs

Change 3758318 by Guillaume.Abadie

	Cleaner fix for mobile scene captures.

	#jira UE-52327

Change 3759541 by Mark.Satterthwaite

	Don't enable Manual Vertex Fetch on iOS Metal for the moment as it isn't well tested there and will probably need further changes.

Change 3695086 by Guillaume.Abadie

	Render thread dynamic resolution & TAA upsample.

	Merging //Tasks/UE4/Dev-DynamicRes/...@3694528 to //UE4/Dev-Rendering/...

	New features breakdown:
	- TAA upsample compute shader that accepts screen percentage from 50% to 200%, with a faster shader permutation for consoles;
	- Material no longer have to deal with BufferUV, and post process material after TAA upsample can sample any scene buffer seamlessly;
	- Material texture per view mip bias to produce sharper images with TAA upsample;
	- Render thread dynamic resolution heuristic is fully plugable by game code (for VR plugin specific heuristics);
	- Dynamic resolution in PIE and game builds;
	- Busy time queries in the RHI to be implemented on the different platforms so that the dynamic resolution heuristic can  exactly associate GPU frame times with screen percentages in its history;
	- Game user settings to enable/disable dynamic resolution;
	- In editor viewport screen percentage config to previsualise and test content at different screen percentage.

	Fixes:
	- Various fixes for algorithms producing different outputs at different screen percentage.
	- Various fixes for algorithms sampling outside view rects.

	Refactors:
	- TAA shader
	- Moved some screen percentage specific members from FSceneView to FViewInfo for thread race bullet proofing.

	Aknowledgements:
	- VR plugins are broken
	- DFAO still have some artifacts

	Premiliminary review: Marcus.Wassmer
	Review for TAA refactor and TAA upsample shader: Brian.Karis
	Review for dynamic resolution: Brian.Karis

[CL 3761165 by Chris Bunner in Main branch]
2017-11-16 11:36:35 -05:00

420 lines
16 KiB
C++

// Copyright 1998-2017 Epic Games, Inc. All Rights Reserved.
#include "MaterialBakingModule.h"
#include "MaterialRenderItem.h"
#include "Engine/TextureRenderTarget2D.h"
#include "ExportMaterialProxy.h"
#include "Interfaces/IMainFrameModule.h"
#include "MaterialOptionsWindow.h"
#include "MaterialOptions.h"
#include "PropertyEditorModule.h"
#include "MaterialOptionsCustomization.h"
#include "UObject/UObjectGlobals.h"
#include "MaterialBakingStructures.h"
#include "Framework/Application/SlateApplication.h"
#include "MaterialBakingHelpers.h"
#include "Async/ParallelFor.h"
#if WITH_EDITOR
#include "FileHelper.h"
#endif
IMPLEMENT_MODULE(FMaterialBakingModule, MaterialBaking);
#define LOCTEXT_NAMESPACE "MaterialBakingModule"
/** Cvars for advanced features */
static TAutoConsoleVariable<int32> CVarUseMaterialProxyCaching(
TEXT("MaterialBaking.UseMaterialProxyCaching"),
1,
TEXT("Determines whether or not Material Proxies should be cached to speed up material baking.\n")
TEXT("0: Turned Off\n")
TEXT("1: Turned On"),
ECVF_Default);
static TAutoConsoleVariable<int32> CVarSaveIntermediateTextures(
TEXT("MaterialBaking.SaveIntermediateTextures"),
0,
TEXT("Determines whether or not to save out intermediate BMP images for each flattened material property.\n")
TEXT("0: Turned Off\n")
TEXT("1: Turned On"),
ECVF_Default);
void FMaterialBakingModule::StartupModule()
{
// Set which properties should enforce gamme correction
FMemory::Memset(PerPropertyGamma, (uint8)false);
PerPropertyGamma[MP_Normal] = true;
PerPropertyGamma[MP_Opacity] = true;
PerPropertyGamma[MP_OpacityMask] = true;
// Set which pixel format should be used for the possible baked out material properties
FMemory::Memset(PerPropertyFormat, (uint8)PF_Unknown);
PerPropertyFormat[MP_EmissiveColor] = PF_FloatRGBA;
PerPropertyFormat[MP_Opacity] = PF_B8G8R8A8;
PerPropertyFormat[MP_OpacityMask] = PF_B8G8R8A8;
PerPropertyFormat[MP_BaseColor] = PF_B8G8R8A8;
PerPropertyFormat[MP_Metallic] = PF_B8G8R8A8;
PerPropertyFormat[MP_Specular] = PF_B8G8R8A8;
PerPropertyFormat[MP_Roughness] = PF_B8G8R8A8;
PerPropertyFormat[MP_Normal] = PF_B8G8R8A8;
PerPropertyFormat[MP_AmbientOcclusion] = PF_B8G8R8A8;
PerPropertyFormat[MP_SubsurfaceColor] = PF_B8G8R8A8;
// Register property customization
FPropertyEditorModule& Module = FModuleManager::Get().LoadModuleChecked<FPropertyEditorModule>("PropertyEditor");
Module.RegisterCustomPropertyTypeLayout(TEXT("PropertyEntry"), FOnGetPropertyTypeCustomizationInstance::CreateStatic(&FPropertyEntryCustomization::MakeInstance));
// Register callback for modified objects
FCoreUObjectDelegates::OnObjectModified.AddRaw(this, &FMaterialBakingModule::OnObjectModified);
}
void FMaterialBakingModule::ShutdownModule()
{
// Urnegister customization and callback
FPropertyEditorModule& Module = FModuleManager::Get().LoadModuleChecked<FPropertyEditorModule>("PropertyEditor");
Module.UnregisterCustomPropertyTypeLayout(TEXT("PropertyEntry"));
FCoreUObjectDelegates::OnObjectModified.RemoveAll(this);
}
void FMaterialBakingModule::BakeMaterials(const TArray<FMaterialData*>& MaterialSettings, const TArray<FMeshData*>& MeshSettings, TArray<FBakeOutput>& Output)
{
checkf(MaterialSettings.Num() == MeshSettings.Num(), TEXT("Number of material settings does not match that of MeshSettings"));
const int32 NumMaterials = MaterialSettings.Num();
const bool bSaveIntermediateTextures = CVarSaveIntermediateTextures.GetValueOnAnyThread() == 1;
for (int32 MaterialIndex = 0; MaterialIndex < NumMaterials; ++MaterialIndex)
{
const FMaterialData* CurrentMaterialSettings = MaterialSettings[MaterialIndex];
const FMeshData* CurrentMeshSettings = MeshSettings[MaterialIndex];
FBakeOutput& CurrentOutput = [&Output]() -> FBakeOutput&
{
Output.AddDefaulted(1);
return Output.Last();
}();
// Canvas per size / special property?
TArray<TPair<UTextureRenderTarget2D*, FSceneViewFamily>> TargetsViewFamilyPairs;
TArray<FExportMaterialProxy*> MaterialRenderProxies;
TArray<EMaterialProperty> MaterialPropertiesToBakeOut;
TArray<UTexture*> MaterialTextures;
CurrentMaterialSettings->Material->GetUsedTextures(MaterialTextures, EMaterialQualityLevel::Num, true, GMaxRHIFeatureLevel, true);
// Force load materials used by the current material
for (UTexture* Texture : MaterialTextures)
{
if (Texture != NULL)
{
UTexture2D* Texture2D = Cast<UTexture2D>(Texture);
if (Texture2D)
{
Texture2D->SetForceMipLevelsToBeResident(30.0f);
Texture2D->WaitForStreaming();
}
}
}
// Generate view family, material proxy and render targets for each material property being baked out
for (TMap<EMaterialProperty, FIntPoint>::TConstIterator PropertySizeIterator = CurrentMaterialSettings->PropertySizes.CreateConstIterator(); PropertySizeIterator; ++PropertySizeIterator)
{
const EMaterialProperty& Property = PropertySizeIterator.Key();
UTextureRenderTarget2D* RenderTarget = CreateRenderTarget(PerPropertyGamma[Property], PerPropertyFormat[Property], PropertySizeIterator.Value());
FExportMaterialProxy* Proxy = CreateMaterialProxy(CurrentMaterialSettings->Material, Property);
FSceneViewFamily ViewFamily(FSceneViewFamily::ConstructionValues(RenderTarget->GameThread_GetRenderTargetResource(), nullptr,
FEngineShowFlags(ESFIM_Game))
.SetWorldTimes(0.0f, 0.0f, 0.0f)
.SetGammaCorrection(RenderTarget->GameThread_GetRenderTargetResource()->GetDisplayGamma()));
TargetsViewFamilyPairs.Add(TPair<UTextureRenderTarget2D*, FSceneViewFamily>(RenderTarget, Forward<FSceneViewFamily>(ViewFamily)));
MaterialRenderProxies.Add(Proxy);
MaterialPropertiesToBakeOut.Add(Property);
}
const int32 NumPropertiesToRender = CurrentMaterialSettings->PropertySizes.Num();
if (NumPropertiesToRender > 0)
{
FCanvas Canvas(TargetsViewFamilyPairs[0].Key->GameThread_GetRenderTargetResource(), nullptr, FApp::GetCurrentTime() - GStartTime, FApp::GetDeltaTime(), FApp::GetCurrentTime() - GStartTime, GMaxRHIFeatureLevel);
Canvas.SetAllowedModes(FCanvas::Allow_Flush);
UTextureRenderTarget2D* PreviousRenderTarget = nullptr;
FTextureRenderTargetResource* RenderTargetResource = nullptr;
FMeshMaterialRenderItem RenderItem(CurrentMaterialSettings, CurrentMeshSettings, MaterialPropertiesToBakeOut[0]);
FCanvas::FCanvasSortElement& SortElement = Canvas.GetSortElement(Canvas.TopDepthSortKey());
for (int32 PropertyIndex = 0; PropertyIndex < NumPropertiesToRender; ++PropertyIndex)
{
const EMaterialProperty Property = MaterialPropertiesToBakeOut[PropertyIndex];
// Update render item
RenderItem.MaterialProperty = Property;
RenderItem.MaterialRenderProxy = MaterialRenderProxies[PropertyIndex];
UTextureRenderTarget2D* RenderTarget = TargetsViewFamilyPairs[PropertyIndex].Key;
if (RenderTarget != nullptr)
{
// Check if the render target changed, in which case we will need to reinit the resource, view family and possibly the mesh data
if (RenderTarget != PreviousRenderTarget)
{
// Setup render target and view family
RenderTargetResource = RenderTarget->GameThread_GetRenderTargetResource();
Canvas.SetRenderTarget_GameThread(RenderTargetResource);
const FRenderTarget* CanvasRenderTarget = Canvas.GetRenderTarget();
RenderItem.ViewFamily = &TargetsViewFamilyPairs[PropertyIndex].Value;
if (PreviousRenderTarget != nullptr && (PreviousRenderTarget->GetSurfaceHeight() != RenderTarget->GetSurfaceHeight() || PreviousRenderTarget->GetSurfaceWidth() != RenderTarget->GetSurfaceWidth()))
{
RenderItem.GenerateRenderData();
}
Canvas.SetRenderTargetRect(FIntRect(0, 0, RenderTarget->GetSurfaceWidth(), RenderTarget->GetSurfaceHeight()));
Canvas.SetBaseTransform(Canvas.CalcBaseTransform2D(RenderTarget->GetSurfaceWidth(), RenderTarget->GetSurfaceHeight()));
PreviousRenderTarget = RenderTarget;
}
// Clear canvas before rendering
Canvas.Clear(RenderTarget->ClearColor);
SortElement.RenderBatchArray.Add(&RenderItem);
// Do rendering
Canvas.Flush_GameThread();
FlushRenderingCommands();
SortElement.RenderBatchArray.Empty();
ReadTextureOutput(RenderTargetResource, Property, CurrentOutput);
FMaterialBakingHelpers::PerformUVBorderSmear(CurrentOutput.PropertyData[Property], RenderTarget->GetSurfaceWidth(), RenderTarget->GetSurfaceHeight(), Property == MP_Normal);
#if WITH_EDITOR
// If saving intermediates is turned on
if (bSaveIntermediateTextures)
{
const UEnum* PropertyEnum = FindObject<UEnum>(ANY_PACKAGE, TEXT("EMaterialProperty"));
FName PropertyName = PropertyEnum->GetNameByValue(Property);
FString TrimmedPropertyName = PropertyName.ToString();
TrimmedPropertyName.RemoveFromStart(TEXT("MP_"));
const FString DirectoryPath = FPaths::ConvertRelativePathToFull(FPaths::ProjectIntermediateDir() + TEXT("MaterialBaking/"));
FString FilenameString = FString::Printf(*(DirectoryPath + TEXT("%s-%d-%s.bmp")),
*CurrentMaterialSettings->Material->GetName(), MaterialIndex, *TrimmedPropertyName);
FFileHelper::CreateBitmap(*FilenameString, CurrentOutput.PropertySizes[Property].X, CurrentOutput.PropertySizes[Property].Y, CurrentOutput.PropertyData[Property].GetData());
}
}
#endif // WITH_EDITOR
}
}
}
if (!CVarUseMaterialProxyCaching.GetValueOnAnyThread())
{
CleanupMaterialProxies();
}
}
bool FMaterialBakingModule::SetupMaterialBakeSettings(TArray<TWeakObjectPtr<UObject>>& OptionObjects, int32 NumLODs)
{
TSharedRef<SWindow> Window = SNew(SWindow)
.Title(LOCTEXT("WindowTitle", "Material Baking Options"))
.SizingRule(ESizingRule::Autosized);
TSharedPtr<SMaterialOptions> Options;
Window->SetContent
(
SAssignNew(Options, SMaterialOptions)
.WidgetWindow(Window)
.NumLODs(NumLODs)
.SettingsObjects(OptionObjects)
);
TSharedPtr<SWindow> ParentWindow;
if (FModuleManager::Get().IsModuleLoaded("MainFrame"))
{
IMainFrameModule& MainFrame = FModuleManager::LoadModuleChecked<IMainFrameModule>("MainFrame");
ParentWindow = MainFrame.GetParentWindow();
FSlateApplication::Get().AddModalWindow(Window, ParentWindow, false);
return !Options->WasUserCancelled();
}
return false;
}
void FMaterialBakingModule::CleanupMaterialProxies()
{
for (auto Iterator : MaterialProxyPool)
{
delete Iterator.Value;
}
MaterialProxyPool.Reset();
}
UTextureRenderTarget2D* FMaterialBakingModule::CreateRenderTarget(bool bInForceLinearGamma, EPixelFormat InPixelFormat, const FIntPoint& InTargetSize)
{
UTextureRenderTarget2D* RenderTarget = nullptr;
const FIntPoint ClampedTargetSize(FMath::Clamp(InTargetSize.X, 1, (int32)GetMax2DTextureDimension()), FMath::Clamp(InTargetSize.Y, 1, (int32)GetMax2DTextureDimension()));
auto RenderTargetComparison = [bInForceLinearGamma, InPixelFormat, ClampedTargetSize](const UTextureRenderTarget2D* CompareRenderTarget) -> bool
{
return (CompareRenderTarget->SizeX == ClampedTargetSize.X && CompareRenderTarget->SizeY == ClampedTargetSize.Y && CompareRenderTarget->OverrideFormat == InPixelFormat && CompareRenderTarget->bForceLinearGamma == bInForceLinearGamma);
};
// Find any pooled render target with suitable properties.
UTextureRenderTarget2D** FindResult = RenderTargetPool.FindByPredicate(RenderTargetComparison);
if (FindResult)
{
RenderTarget = *FindResult;
}
else
{
// Not found - create a new one.
RenderTarget = NewObject<UTextureRenderTarget2D>();
check(RenderTarget);
RenderTarget->AddToRoot();
RenderTarget->ClearColor = FLinearColor(1.0f, 0.0f, 1.0f);
RenderTarget->ClearColor.A = 1.0f;
RenderTarget->TargetGamma = 0.0f;
RenderTarget->InitCustomFormat(ClampedTargetSize.X, ClampedTargetSize.Y, InPixelFormat, bInForceLinearGamma);
RenderTargetPool.Add(RenderTarget);
}
checkf(RenderTarget != nullptr, TEXT("Unable to create or find valid render target"));
return RenderTarget;
}
FExportMaterialProxy* FMaterialBakingModule::CreateMaterialProxy(UMaterialInterface* Material, const EMaterialProperty Property)
{
FExportMaterialProxy* Proxy = nullptr;
// Find any pooled material proxy matching this material and property
FExportMaterialProxy** FindResult = MaterialProxyPool.Find(TPair<UMaterialInterface*, EMaterialProperty>(Material, Property));
if (FindResult)
{
Proxy = *FindResult;
}
else
{
Proxy = new FExportMaterialProxy(Material, Property);
MaterialProxyPool.Add(TPair<UMaterialInterface*, EMaterialProperty>(Material, Property), Proxy);
}
return Proxy;
}
void FMaterialBakingModule::ReadTextureOutput(FTextureRenderTargetResource* RenderTargetResource, EMaterialProperty Property, FBakeOutput& Output)
{
checkf(!Output.PropertyData.Contains(Property) && !Output.PropertySizes.Contains(Property), TEXT("Should be reading same property data twice"));
TArray<FColor>& OutputColor = Output.PropertyData.Add(Property);
FIntPoint& OutputSize = Output.PropertySizes.Add(Property);
// Retrieve rendered size
OutputSize = RenderTargetResource->GetSizeXY();
const bool bNormalmap = (Property== MP_Normal);
FReadSurfaceDataFlags ReadPixelFlags(bNormalmap ? RCM_SNorm : RCM_UNorm);
ReadPixelFlags.SetLinearToGamma(false);
if (Property != MP_EmissiveColor)
{
// Read out pixel data
RenderTargetResource->ReadPixels(OutputColor, ReadPixelFlags);
}
else
{
// Emissive is a special case
TArray<FFloat16Color> Color16;
RenderTargetResource->ReadFloat16Pixels(Color16);
const int32 NumThreads = [&]()
{
return FPlatformProcess::SupportsMultithreading() ? FPlatformMisc::NumberOfCores() : 1;
}();
float* MaxValue = new float[NumThreads];
const int32 LinesPerThread = FMath::CeilToInt((float)OutputSize.Y / (float)NumThreads);
// Find maximum float value across texture
ParallelFor(NumThreads, [&Color16, LinesPerThread, MaxValue, OutputSize](int32 Index)
{
const int32 EndY = FMath::Min((Index + 1) * LinesPerThread, OutputSize.Y);
float& CurrentMaxValue = MaxValue[Index];
const FFloat16Color MagentaFloat16 = FFloat16Color(FLinearColor(1.0f, 0.0f, 1.0f));
for (int32 PixelY = Index * LinesPerThread; PixelY < EndY; ++PixelY)
{
const int32 YOffset = PixelY * OutputSize.X;
for (int32 PixelX = 0; PixelX < OutputSize.X; PixelX++)
{
const FFloat16Color& Pixel16 = Color16[PixelX + YOffset];
// Find maximum channel value across texture
if (!(Pixel16 == MagentaFloat16))
{
CurrentMaxValue = FMath::Max(CurrentMaxValue, FMath::Max3(Pixel16.R.GetFloat(), Pixel16.G.GetFloat(), Pixel16.B.GetFloat()));
}
}
}
});
const float GlobalMaxValue = [&MaxValue, NumThreads]
{
float TempValue = 0.0f;
for (int32 ThreadIndex = 0; ThreadIndex < NumThreads; ++ThreadIndex)
{
TempValue = FMath::Max(TempValue, MaxValue[ThreadIndex]);
}
return TempValue;
}();
if (GlobalMaxValue <= 0.01f)
{
// Black emissive, drop it
}
// Now convert Float16 to Color using the scale
OutputColor.SetNumUninitialized(Color16.Num());
const float Scale = 255.0f / GlobalMaxValue;
ParallelFor(NumThreads, [&Color16, LinesPerThread, &OutputColor, OutputSize, Scale](int32 Index)
{
const int32 EndY = FMath::Min((Index + 1) * LinesPerThread, OutputSize.Y);
for (int32 PixelY = Index * LinesPerThread; PixelY < EndY; ++PixelY)
{
const int32 YOffset = PixelY * OutputSize.X;
for (int32 PixelX = 0; PixelX < OutputSize.X; PixelX++)
{
const FFloat16Color& Pixel16 = Color16[PixelX + YOffset];
FColor& Pixel8 = OutputColor[PixelX + YOffset];
Pixel8.R = (uint8)FMath::RoundToInt(Pixel16.R.GetFloat() * Scale);
Pixel8.G = (uint8)FMath::RoundToInt(Pixel16.G.GetFloat() * Scale);
Pixel8.B = (uint8)FMath::RoundToInt(Pixel16.B.GetFloat() * Scale);
Pixel8.A = 255;
}
}
});
// This scale will be used in the proxy material to get the original range of emissive values outside of 0-1
Output.EmissiveScale = GlobalMaxValue;
}
}
void FMaterialBakingModule::OnObjectModified(UObject* Object)
{
if (CVarUseMaterialProxyCaching.GetValueOnAnyThread())
{
if (Object && Object->IsA<UMaterialInterface>())
{
UMaterialInterface* Material = Cast<UMaterialInterface>(Object);
if (MaterialProxyPool.Contains(TPair<UMaterialInterface*, EMaterialProperty>(Material, MP_BaseColor)))
{
for (int32 PropertyIndex = 0; PropertyIndex < MP_MAX; ++PropertyIndex)
{
MaterialProxyPool.Remove(TPair<UMaterialInterface*, EMaterialProperty>(Material, (EMaterialProperty)PropertyIndex));
}
}
}
}
}
#undef LOCTEXT_NAMESPACE //"MaterialBakingModule"