You've already forked UnrealEngineUWP
mirror of
https://github.com/izzy2lost/UnrealEngineUWP.git
synced 2026-03-26 18:15:20 -07:00
#lockdown Nick.Penwarden ============================ MAJOR FEATURES & CHANGES ============================ Change 3503468 by Marcus.Wassmer Fix merge conflicts Change 3537059 by Ben.Marsh Fixing case of iOS directories, pt1 Change 3537060 by Ben.Marsh Fixing case of iOS directories, pt2 Change 3608300 by Chris.Bunner Added post process material to preview compile cache set to allow post process volume preview scene improvements. Change 3608302 by Chris.Bunner Fixed decal lifetime fading. #jira UE-48400 Change 3608303 by Chris.Bunner Updated default WritesAllPixels input to ignore dithering (as intended, was disabled due to isues at the time). Fixed material instances returning their local data when not overridden. #jira UE-48254 Change 3608455 by Mark.Satterthwaite Enabling WorldPositionOffset requires disabling fast-math on Metal because the manually specified FMA's are not respected if one or more arguments is a literal which then leads to very different compiler optimisation between the depth-only shader and the base-pass shader. This change will only affect the way Metal compiles shaders. #jira UE-47372 Change 3608462 by Rolando.Caloca DR - Cloth vertex buffers no longer generate dummy vertices Copy from 3608349 and 3608407 Change 3608491 by Rolando.Caloca DR - hlsl - Fix crash when type was not found Change 3608513 by Rolando.Caloca DR - Default to real uniform buffers for Vulkan SM4 & SM5 Change 3608794 by Mark.Satterthwaite Implement SV_DepthLessEqual (maybe right?) for Metal - seems to work in the ParallaxOcclusionMapping test map. #jira UE-47614 Change 3608929 by Mark.Satterthwaite Fix ambiguous expression compile error. Change 3608991 by Mark.Satterthwaite Fix a dumb bug when parsing the Metal compiler version that breaks Metal shader PCH generation on HFS+ volumes. Change 3609090 by Uriel.Doyon StaticMeshComponent and LandscapeComponent now register AO material mask and sky occlusion texture in the texture streamer. Changing the current lighting scenario now triggers an update of the texture streamer, and a refresh of lighting data for instanced static meshes. Added an option to the "liststreamingtextures" named UNKOWNREF allowing to inspect texture without references in the texture streamer. BUILDMATERIALTEXTURESTREAMINGDATA now rebuild every shader in memory and mark for save those with different data. MipBias now behaves the same way in shipping than in other builds. Fixed texture resolution logic for editor tooltips and in game stats. Change 3609659 by Richard.Wallis Remove Eye Adaption Pixel Shader Workaround for macOS 10.11 (El Cap) Nividia. #jira UE-48642 Change 3610552 by Mark.Satterthwaite Optimise the constant-propagation pass in hlslcc by using a hash-table to reduce the cost of looking up the existing constant assignment instructions. The get_assignment_entry drops from 25% of runtime to 2.2% of runtime on a 4.2Ghz Quad i7 2017 iMac. Change 3610662 by Rolando.Caloca DR - hlsl - Fix for rwstructured buffer Fix for floats printed as ints Change 3610830 by Michael.Lentine ByteAddressBuffer does not have a subtype. Change 3610869 by Rolando.Caloca DR - hlsl - Fix disambiguation between 1.r and 1.0.r Change 3610982 by Mark.Satterthwaite Use the correct code to dump Metal shader text for debugging at runtime. Change 3610996 by Rolando.Caloca DR - hlsl - Actual fix for 0.r Change 3611312 by Rolando.Caloca DR - Integrate: Improve performance of bokeh depth of field. * Fewer instances with more work (higher quad count) per instance. * Improves performance on RX 480 in the Infiltrator demo by 0.37 ms at 1080p and 0.50 ms at 1440p (average frame time over the beginning of the demo, including the hallway confrontation between the guard and the infiltrator, where heavy DOF is used). * Similar optimizations may be possible for other systems that perform similar "instanced draws of quads" (e.g. virtual texture page table updates, lens blur, and velocity scatter). Change 3611345 by Mark.Satterthwaite Missed the hash-table destructor in previous change. Change 3611372 by Rolando.Caloca DR - vk - New barrier/layout api Change 3611445 by Mark.Satterthwaite Fix stupid bugs in MetalBackend's LoadRWBuffer helper function where the wrong type was being used - won't fix problems in the LinearTexture case though. Change 3611686 by Mark.Satterthwaite Remove the sampler from the Metal Linear Texture SRV path as for reasons so far unknown it doesn?╟╓t work with the light grid culling. #jira UE-48881 Change 3611743 by Mark.Satterthwaite Implement early depth test for Metal - it is implemented such that manual specification of the SV_Depth* outputs will elide the early_fragment_test qualifier as Metal does not permit both at present. Change 3611746 by Mark.Satterthwaite Use early fragment tests implicitly unless we perform a direct resource write or use discard - explicit depth writes always disable early fragment tests as Metal doesn?╟╓t allow both. This should better match D3D driver behaviour. Change 3611756 by Mark.Satterthwaite Missed a header file in last commit. Change 3611836 by Mark.Satterthwaite Fixed the use of Metal?╟╓s capture manager so that it doesn?╟╓t capture more frames than intended. Change 3611843 by Mark.Satterthwaite Tidy up the handling of when to increment the frame count for the Metal capture manager. Change 3612279 by Michael.Lentine Move FP16 Math to Public so that it can be included as part of platform which is where the other float/half defines happen. Change 3612595 by Rolando.Caloca DR - hlslcc - Rebuilt with CL 3611345 Change 3612665 by Rolando.Caloca DR - Make cubemap mip barrier consistent with HZB mip barriers Change 3612758 by Daniel.Wright FColor usage comment Change 3612980 by Rolando.Caloca DR - hlsl - Do not overflow ints Change 3613068 by Rolando.Caloca DR - vk - Initial fix for transition validation warnings Change 3613115 by Daniel.Wright Volumetric lightmap voxels are now always cubes Bricks outside of any Lightmass Importance Volume will never be refined Change 3613124 by zachary.wilson Enabling Eye-Adaptation in TM-ShaderModels. Change 3613205 by Mark.Satterthwaite Fully disable linear textures in Metal - they simply aren't performant. Instead we'll have to use helper functions to dynamically type-cast appropriately within the shader. This is currently only configured for a handful of UAV types and will need to be extended. Change 3613208 by Mark.Satterthwaite Add code to MetalBackend to promote half types to float for math operations to avoid compiler errors. Change 3613354 by zachary.wilson Fixing up the Bloom_FFT map. Renaming to fit qa conventions, updating content and improving workflow. Change 3613409 by Rolando.Caloca DR - vk - Layout as part of descriptor writes Some access flag warning fixes Change 3613518 by Daniel.Wright Added 'Render Unbuilt Preview Shadows in game' rendering project setting and r.Shadow.UnbuiltPreviewInGame cvar Change 3613610 by Daniel.Wright Volumetric lightmap visualization sphere size is now a fraction of the corresponding brick world size Change 3613651 by Daniel.Wright [Copy] Fixed landscape in the Global Distance field on PS4. Multiple updates to a vertex buffer using BUF_Dynamic cause a race condition on PS4 with no assert. Also added shrinking for GDistanceFieldUploadData which saved 15Mb. Change 3613696 by Mark.Satterthwaite Add the Metal SRV format for Index buffers so that they can be properly type-cast inside the shader. Fixes recompute tangents with latest changes. Change 3613697 by Rolando.Caloca DR - vk - Fix missing layout Change 3613922 by Rolando.Caloca DR - vk - Some fixes for layout/transitions - Disable GSupportsDepthFetchDuringDepthTest on desktop as the deferred renderer is not copying the aux depth in the right spot and will be removed Change 3614009 by Mark.Satterthwaite TPS Approved: Integrating the MIT-licensed mtlpp C++ Metal wrapper from Nikolay Aleksiev which will slowly replace previous Metal API wrappers in MetalRHI. Change 3614015 by Mark.Satterthwaite Initial extensions to mtlpp: - Fixed over retention of alloc-init'd objects. - Added 10_13 & 11_0 availablity macros. - Started, but have not yet finished adding new Metal API function wrappers. Change 3614909 by Rolando.Caloca DR - Fix static analysis Change 3614916 by Michael.Lentine Add function to convert FP32 to FP16 Change 3614957 by Mark.Satterthwaite mtlpp declarations for macOS 10.13 & iOS 11 Metal features - no matching definitions yet. Change 3614995 by Mark.Satterthwaite Revert all changes to project config's from Rhino that should not have come back to Dev-Rendering, keeping only the solitary change to Metal shader standard necessary for ShowdownDemo. Change 3615035 by Rolando.Caloca DR - Generate mips using shader for HZB Change 3615561 by Rolando.Caloca DR - Fix deprecation warning Change 3615787 by Mark.Satterthwaite Only emit min. OS version specification into the Metal shader bytecode for macOS as we share shaders between iOS & tvOS and this option inhibts that. #jira UE-48919 Change 3616317 by Mark.Satterthwaite Make TonemapperConfBitmaskPC the proper size so we dn't attempt to access uninitialized memory. Change 3616357 by Mark.Satterthwaite And fix some compile errors... Change 3616473 by Rolando.Caloca DR - Render pass api minor changes Change 3616518 by Mark.Satterthwaite Fix a merge snafu where dead code was retained where it shouldn't be. #jira UE-48472 Change 3616706 by Rolando.Caloca DR - Vulkan fixes (integration from Vulkan working branch) - Fix for editor outline - Fix for profilegpu Change 3616770 by Rolando.Caloca DR - vk - Mark GIsGPUCrashed on device lost Change3616993by Daniel.Wright IndirectLightingCacheQuality respects VolumetricLightingMethod Change 3616996 by Daniel.Wright Volumetric Lightmap show flag is respected by Volumetric Fog Change 3616999 by Daniel.Wright Fixed ObjectRadius in Volume domain materials Change 3617777 by Rolando.Caloca DR - Fix static analysis warning Change 3617863 by Guillaume.Abadie PR #3875: Removed Duplicated "RHI" Module Dependency (Contributed by DavidNSilva) #jira UE-48159 Change 3618133 by Rolando.Caloca DR - vk - Set general layout for compute shader resources - Assume transitions to writable imply end render pass Change 3618292 by Michael.Lentine Add support for Expressions, Jump Statments, and Structs. Change 3618326 by Rolando.Caloca DR - vk - Fix transition flags Change 3618408 by Daniel.Wright Lightmass skylight solver improvements * Lightmass uses a filtered cubemap to represent the skylight instead of a 3rd order Spherical Harmonic. Directionality in shadowed areas is improved. Mip level is chosen based on the ray differential for anti-aliasing. * Multiple skylight and emissive bounces are now supported with a radiosity solver, controlled by NumSkyLightingBounces in Lightmass WorldSettings. More bounces results in longer build times, and the radiosity time is not distributable. * The mapping surface cache is now rasterized with supersampling, reduces incorrect darkness in corners * Combined direct lighting, photon irradiance, skylight radiosity and diffuse in the mapping surface cache so final gather rays only have to do one memory fetch, speeds up lighting builds by 7%. * Added support for Embree packet tracing although no solver algorithms use it yet Change 3618413 by Daniel.Wright Swarm hands out the most expensive tasks in roughly a round robin ordering among distribution agents. Lightmass processing of a single task is multithreaded, so ideally the most expensive tasks are evenly distributed among active agents. This has the biggest impact in small scenes with 10's of high resolution lightmaps, and with a distribution farm. Build time in one scene went from to 113s -> 47s. Change 3618439 by Mark.Satterthwaite Fix the assert in hlslcc when we have saturate(int) and the shader language spec. supports a native saturate intrinsic. Change 3618468 by Rolando.Caloca DR - vk - Fix copy to non render target surface Change 3618696 by Daniel.Wright Worked around Lightmass crash callstacks not getting reported back to the editor Change 3618779 by Mark.Satterthwaite mtlpp definitions for a few of the new calls & fixing the max. number of samplers it assumes. Change 3618789 by Daniel.Wright Added missing file Change3618816by Daniel.Wright Another missing file Change 3618855 by Rolando.Caloca DR - vk - Show user debug markers when using dump layers - Remove old defines Change 3618887 by Rolando.Caloca DR - Fix for missing transition to readable for blur widget. Was causing corruption on Vulkan. Change 3618999 by Mark.Satterthwaite Definitions for Metal's new CaptureManager & CaptureScope classes. Change 3619790 by Jian.Ru Add some debug info #jira UE-48710 Change 3619834 by Rolando.Caloca DR - vk - static analysis fix Change 3619952 by Rolando.Caloca DR - vk - Static analysis not smart enough... Change 3620191 by Jian.Ru Revert 3584245 to prevent focus stealing #jira UE-49044 Change 3620402 by Mark.Satterthwaite Remaining Metal definitions for mtlpp. Change 3620803 by Brian.Karis Removed faceting bug I introduced to Dither Opacity Mask. Removes the attempt to make opacity stack properly. Change 3620904 by Michael.Lentine Change the order of static and const Change 3620975 by Rolando.Caloca DR - Updated Vulkan headers to SDK 1.0.57.0 Change 3621026 by Rolando.Caloca DR - Remove unused type - Force recompile with new Vulkan headers Change 3621070 by Rolando.Caloca DR - glslang - Fix pdb option Change 3621157 by Arciel.Rekman Added files to cross-build glslang on Windows. (Edigrating //UE4/Main/...@3621127 to //UE4/Dev-Rendering/...) Change 3621194 by Rolando.Caloca DR - glslang - Update to 1.0.57.0 - Fix some tab/whitespace mismatch Change 3621225 by Rolando.Caloca DR - Revert glslang (Back out changelist 3621194) Change 3621254 by Mark.Satterthwaite Duplicate 3610656 and revert the incorrect merge from the Rhino task stream. Fixes EyeAdaptation on all clang platforms properly thanks to RCL. Change 3621261 by Mark.Satterthwaite Trivial FMetalStateCache optimisations - won't help much but equally they shouldn't hurt. Change 3621262 by Mark.Satterthwaite Correct the handling of MSAA target in Desktop Forward for iOS - now the problem is that iOS always creates an internal resolve target so which texture to bind depends on the shader parameter type. Not sure (yet) how best to solve that. Change 3621263 by Mark.Satterthwaite Don't mandate Mobile Metal for projects that have Metal MRT enabled. Change 3621301 by Rolando.Caloca DR - Unity build fix Change 3621349 by Mark.Satterthwaite Fix a bug in MetalBackend that was omitting the depth-output variable from the hlslcc signature if the semantic was SV_DepthLessEqual rather than SV_Depth. Change 3621546 by Uriel.Doyon Refactor of the texture 2D mip update logic to offload more work on the async thread. #jira UE-45332 #jira UE-45789 Change 3622210 by Rolando.Caloca DR - Do not store DDC data if static mesh failed to build #jira UE-48358 Change 3622349 by Arciel.Rekman Better build script for Linux glslang and a bugfix. (Edigrating CL 3622235 from //UE4/Main/... to //UE4/Dev-Rendering/...) Change 3622401 by Rolando.Caloca DR - vk - Integration - Support for r.Vulkan.ProfileCmdBuffers Change 3622506 by Rolando.Caloca DR - vk - Back out changelist 3622401 Change 3622521 by Mark.Satterthwaite Support disabling V-Sync in MetalRHI on macOS 10.13+. Change 3622910 by Rolando.Caloca DR - static analysis fix Change 3622964 by Mark.Satterthwaite Fix generation of .metallib on local Macs and exclude .metallib files from the pak - they must always be loaded from disk. #jira UE-48193 Change 3622986 by Mark.Satterthwaite A couple more trivial optimisations to MetalRHI for iOS: - Metal page size is 4k but only buffers under 512 bytes should go through set*Bytes on iOS to balance CPU cost. - On iOS the minimum buffer size should therefore be 1k and on Mac 4k as nothing else makes much sense. - No need to rebind uniform buffers if to the same slot - it just wastes cycles. Change 3623266 by Rolando.Caloca DR - Fix GL4 rendering #jira UE-49187 Change 3623377 by Daniel.Wright Volume materials applied to static meshes operate on the object's bounding sphere Change 3623427 by Mark.Satterthwaite Fix MetalViewport compile errors on Xode 8.3. #jira UE-49231 Change 3623443 by Daniel.Wright Fixed out of bounds crash in lightmass Change 3623751 by Daniel.Wright Volume materials on static meshes now voxelize the mesh's Object space bounding box Change 3625142 by Guillaume.Abadie PR #2992: Fixing aspect ratio issue of SceneCapture2D rendering in "Ortho" camera mode (Contributed by monsieurgustav) Change 3625983 by Jian.Ru Fix a LPV race condtion due to parallel RSM draw-call submission #jira UE-48247 Change 3626015 by Jian.Ru Small fix to 3625983 Change 3626294 by Michael.Trepka Copy of CL 3535792 and 3576637 Added support for changing monitor's display mode on Mac in fullscreen mode. This greatly improves performance on Retina screens when playing in resolutions lower than native. Fixed a problem with incorrect viewport size being set in windowed fullscreen in some cases. Also, slightly improved screen fades for fullscreen mode transitions on Mac. #jira UE-48018 Change 3626532 by Marcus.Wassmer Fix divide by 0 crash when GPU timing frequency not available for whatever reason. Change 3626548 by Ryan.Brucks KismetRenderingLibrary: Added EditorOnly function for creating static textures from Render Targets. Has options for Mip and Compression Settings Change 3626874 by Mark.Satterthwaite Fix Metal 2.0 compilation. Change 3626997 by Rolando.Caloca DR - vk - cis fix - Initial RGBA16 readback Change 3627016 by Mark.Satterthwaite Workaround more of Metal's unfortunate tendency to re-associate float mul/add/sub operations - this time from Metal's own standard-library. Change 3627040 by Brian.Karis Removed old rasterized deferred reflection env path. Removed reflection compute shader. Replaced with PS. Small perf gain. Change 3627055 by Mark.Satterthwaite No MSAA support on Intel Metal or iOS Desktop Forward for the moment as neitehr work and I don't want to have lots of crashes out in the wild until we have a solution. Change 3627057 by Mark.Satterthwaite Make SCW's directcompile not fall over with Metal when there are compilation errors. Change 3627083 by Mark.Satterthwaite Invalidate Metal shaders so QA testing picks up the most recent changes. Change 3627788 by Chris.Bunner [Duplicate, CL 3627751] - VisibleExpressions static switch value evaluation needs to handle reroute nodes rather than only verify first expression. Change 3627834 by Rolando.Caloca DR - cis fix Change 3627847 by Rolando.Caloca DR - 4th try to fix static analysis Change 3627877 by Guillaume.Abadie Works arround a HLSLCC bug in a SimpleComposure project's material where x != x does not work for an unknown reason yet. #jira UE-48063 Change 3628035 by Marcus.Wassmer Duplicate 3620990 Smarter scenecapture allocation behavior. Change 3628204 by Daniel.Wright Fixed denormalization scale on one of the 2nd SH band of volumetric lightmaps Change 3628217 by Mark.Satterthwaite Fix InfiltratorForward project defaults so that iOS will package. Change 3628515 by Arne.Schober DR - [UE-49213] - Fix case where HZB was not generated for SSR and SSAO when Occlusion culling was disabled. #RB Marcus.Wassmer Change 3628550 by Chris.Bunner Merge fixes. Change 3628597 by Chris.Bunner Merge fixes. Change 3628656 by Michael.Trepka One more workaround for a bug in StandardPlatformString.cpp. It doesn't handle %lf format correctly, parsing it as long double instead of ignoring the 'l' format sub-specifier. Change 3628685 by Daniel.Wright CPU interpolation of Volumetric Lightmaps for the mobile renderer. They use a scene cache based on interpolation position, since the precomputed lighting buffer for movable objects is recreated every frame. Change 3629094 by Ryan.Brucks Fixes to RenderTargetCreateStaticTexture2DEditorOnly with additional error checks #RB none Change 3629223 by Rolando.Caloca DR - Rollback //UE4/Dev-Rendering/Engine/Source/Runtime/VulkanRHI to changelist 3627847 Change 3629491 by Rolando.Caloca DR - Revert back to emulated uniform buffers on SM4/SM5 Change 3629663 by Daniel.Wright Fixed NaN when capsule shadow direction is derived from volumetric lightmap with completely black lighting Change 3629664 by Daniel.Wright Don't render dynamic indirect occlusion from mesh distance fields when operating on a movable skylight, since DFAO fills that role Change 3629708 by Rolando.Caloca DR - vk - Redo some changes from DevMobile 3601439 3604186 3606672 3617383 3617474 3617483 Change 3629770 by Mark.Satterthwaite Fix a mobile Metal shader compilation error when using the FMA workaround for "cross" which should only be applied if the min. Metal version is 1.2 (as FMA is not known to work prior to this). Change 3629793 by Daniel.Wright Fixed VolumetricLightmapDetailCellSize not being respected in small levels, causing too much volumetric lightmap density and memory Change 3629859 by Mark.Satterthwaite macOS 10.12 also had problems with MSAA in forward rendering - so only permit it to work on macOS 10.13 and above. Change 3630790 by Mark.Satterthwaite Move RHISupportsMSAA so that the Metal related complications for when it is viable can be hidden within. Change 3630990 by Rolando.Caloca DR - vk - Redid CL 3617437 (optimize number of Buffer Views, eg 165 to 58) Change 3631071 by Mark.Satterthwaite Fix a small gotcha in a change from Dev-Mobile: for MetalRHI we need to explicitly configure the ShaderCacheContext for the immediate/device context after initialising the shader-cache. #jira UE-49431 Change 3631076 by Rolando.Caloca DR - vk - Redo 3617574, reduce number of render pass objects created Change 3631250 by Mark.Satterthwaite Make another Metal warning a Verbose log instead as it isn't interesting unless you are me. Change 3631911 by Chris.Bunner Back out changelist 3628035. #jira UE-49364, UE-49365 Change 3632041 by Mark.Satterthwaite Fix cloth rendering on Metal - some of the data in FClothVertex is uint but we load it from a float buffer. This could be due to a bug in Metal's as_type<uint4>() or it could be that Xcode 9's compiler is now finally enforcing Metal's official flush-to-zero-on-load semantics for denorms - it isn't immediately obvious. #jira UE-49439 Change 3632261 by Brian.Karis SM4 fallback for reflection captures. Change 3632281 by Mark.Satterthwaite Fix an intermittent assert on startup when the AVFoundation movie player gets the QAGame TM-ShaderModels video ready to play prior to the rendering thread being back online when resizing the window. This is done by deferring the processing of AVFoundation events to the game-thread where it won't cause a threading violation. Change 3632382 by Rolando.Caloca DR - vk - Fix clang warning Change 3633338 by Chris.Bunner Static analysis/Linux compile fix. #jira UE-49502 Change 3633616 by Jian.Ru Force alpha to 0xff for functional UI screenshot tests #jira UE-48266 Change 3633818 by Daniel.Wright Better indirection texture clamping and asserts Change 3634319 by Mark.Satterthwaite Stop FVolumetricLightmapDataLayer ::Discard which is invoked by the Editor RHI during texture-upload from chucking the backing data when in the Editor - because if we do that then cooking will serialise an empty array. This was only apparent on Mac because Metal always invokes Discard on BulkDataInterfaces and D3D11 never does. #jira UE-49381 Change 3634613 by Rolando.Caloca DR - Call discard on bulk data for textures #jira UE-49533 Change 3634654 by Mark.Satterthwaite Fixes for broken iOS builds: - Fix RHIGetShaderLanguageVersion returning the wrong version for iOS Metal if the Mac version had already been queried - this has been wrong for a long while. - Remove the precise:: qualifier for Metal's fma intrinsic - it isn't necessary and breaks on older OSes. #jira UE-49381 Change 3634820 by Mark.Satterthwaite Change the hash-function for the preprocessed HLSL source in FMetalShaderOutputCooker to reduce risk of hash-collisions. Fixes one cause of UE-49381 and reveals an underlying driver bug on iOS 9 with runtime-compiled text shaders *only*. #jira UE-49381 Change 3634821 by Mark.Satterthwaite Force Metal shaders only to recompile by incrementing the format version. [CL 3635058 by Chris Bunner in Main branch]
1637 lines
64 KiB
C++
1637 lines
64 KiB
C++
// Copyright 1998-2017 Epic Games, Inc. All Rights Reserved.
|
|
|
|
/*=============================================================================
|
|
Functionality for capturing the scene into reflection capture cubemaps, and prefiltering
|
|
=============================================================================*/
|
|
|
|
#include "ReflectionEnvironmentCapture.h"
|
|
#include "Misc/FeedbackContext.h"
|
|
#include "RenderingThread.h"
|
|
#include "RenderResource.h"
|
|
#include "ShowFlags.h"
|
|
#include "UnrealClient.h"
|
|
#include "ShaderParameters.h"
|
|
#include "RendererInterface.h"
|
|
#include "RHIStaticStates.h"
|
|
#include "SceneView.h"
|
|
#include "Shader.h"
|
|
#include "TextureResource.h"
|
|
#include "StaticBoundShaderState.h"
|
|
#include "SceneUtils.h"
|
|
#include "SceneManagement.h"
|
|
#include "Components/SkyLightComponent.h"
|
|
#include "Components/ReflectionCaptureComponent.h"
|
|
#include "Engine/TextureCube.h"
|
|
#include "PostProcess/SceneRenderTargets.h"
|
|
#include "GlobalShader.h"
|
|
#include "SceneRenderTargetParameters.h"
|
|
#include "SceneRendering.h"
|
|
#include "ScenePrivate.h"
|
|
#include "PostProcess/SceneFilterRendering.h"
|
|
#include "PostProcess/PostProcessing.h"
|
|
#include "ScreenRendering.h"
|
|
#include "ReflectionEnvironment.h"
|
|
#include "OneColorShader.h"
|
|
#include "PipelineStateCache.h"
|
|
#include "MobileReflectionEnvironmentCapture.h"
|
|
|
|
/** Near plane to use when capturing the scene. */
|
|
float GReflectionCaptureNearPlane = 5;
|
|
|
|
int32 GSupersampleCaptureFactor = 1;
|
|
|
|
/**
|
|
* Mip map used by a Roughness of 0, counting down from the lowest resolution mip (MipCount - 1).
|
|
* This has been tweaked along with ReflectionCaptureRoughnessMipScale to make good use of the resolution in each mip, especially the highest resolution mips.
|
|
* This value is duplicated in ReflectionEnvironmentShared.usf!
|
|
*/
|
|
float ReflectionCaptureRoughestMip = 1;
|
|
|
|
/**
|
|
* Scales the log2 of Roughness when computing which mip to use for a given roughness.
|
|
* Larger values make the higher resolution mips sharper.
|
|
* This has been tweaked along with ReflectionCaptureRoughnessMipScale to make good use of the resolution in each mip, especially the highest resolution mips.
|
|
* This value is duplicated in ReflectionEnvironmentShared.usf!
|
|
*/
|
|
float ReflectionCaptureRoughnessMipScale = 1.2f;
|
|
|
|
int32 GDiffuseIrradianceCubemapSize = 32;
|
|
|
|
void OnUpdateReflectionCaptures( UWorld* InWorld )
|
|
{
|
|
InWorld->UpdateAllReflectionCaptures();
|
|
}
|
|
|
|
FAutoConsoleCommandWithWorld CaptureConsoleCommand(
|
|
TEXT("r.ReflectionCapture"),
|
|
TEXT("Updates all reflection captures"),
|
|
FConsoleCommandWithWorldDelegate::CreateStatic(OnUpdateReflectionCaptures)
|
|
);
|
|
|
|
static TAutoConsoleVariable<int32> CVarReflectionCaptureGPUArrayCopy(
|
|
TEXT("r.ReflectionCaptureGPUArrayCopy"),
|
|
1,
|
|
TEXT("Do a fast copy of the reflection capture array when resizing if possible. This avoids hitches on the rendering thread when the cubemap array needs to grow.\n")
|
|
TEXT(" 0 is off, 1 is on (default)"),
|
|
ECVF_ReadOnly);
|
|
|
|
|
|
bool DoGPUArrayCopy()
|
|
{
|
|
return GRHISupportsResolveCubemapFaces && CVarReflectionCaptureGPUArrayCopy.GetValueOnAnyThread();
|
|
}
|
|
|
|
void FullyResolveReflectionScratchCubes(FRHICommandListImmediate& RHICmdList)
|
|
{
|
|
SCOPED_DRAW_EVENT(RHICmdList, FullyResolveReflectionScratchCubes);
|
|
FSceneRenderTargets& SceneContext = FSceneRenderTargets::Get(RHICmdList);
|
|
FTextureRHIRef& Scratch0 = SceneContext.ReflectionColorScratchCubemap[0]->GetRenderTargetItem().TargetableTexture;
|
|
FTextureRHIRef& Scratch1 = SceneContext.ReflectionColorScratchCubemap[1]->GetRenderTargetItem().TargetableTexture;
|
|
FResolveParams ResolveParams(FResolveRect(), CubeFace_PosX, -1, -1, -1);
|
|
RHICmdList.CopyToResolveTarget(Scratch0, Scratch0, true, ResolveParams);
|
|
RHICmdList.CopyToResolveTarget(Scratch1, Scratch1, true, ResolveParams);
|
|
}
|
|
|
|
IMPLEMENT_SHADER_TYPE(,FCubeFilterPS,TEXT("/Engine/Private/ReflectionEnvironmentShaders.usf"),TEXT("DownsamplePS"),SF_Pixel);
|
|
|
|
IMPLEMENT_SHADER_TYPE(template<>,TCubeFilterPS<0>,TEXT("/Engine/Private/ReflectionEnvironmentShaders.usf"),TEXT("FilterPS"),SF_Pixel);
|
|
IMPLEMENT_SHADER_TYPE(template<>,TCubeFilterPS<1>,TEXT("/Engine/Private/ReflectionEnvironmentShaders.usf"),TEXT("FilterPS"),SF_Pixel);
|
|
|
|
/** Computes the average brightness of a 1x1 mip of a cubemap. */
|
|
class FComputeBrightnessPS : public FGlobalShader
|
|
{
|
|
DECLARE_SHADER_TYPE(FComputeBrightnessPS,Global)
|
|
public:
|
|
|
|
static bool ShouldCache(EShaderPlatform Platform)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
static void ModifyCompilationEnvironment(EShaderPlatform Platform, FShaderCompilerEnvironment& OutEnvironment)
|
|
{
|
|
FGlobalShader::ModifyCompilationEnvironment(Platform, OutEnvironment);
|
|
OutEnvironment.SetDefine(TEXT("COMPUTEBRIGHTNESS_PIXELSHADER"), 1);
|
|
}
|
|
|
|
FComputeBrightnessPS(const ShaderMetaType::CompiledShaderInitializerType& Initializer)
|
|
: FGlobalShader(Initializer)
|
|
{
|
|
ReflectionEnvironmentColorTexture.Bind(Initializer.ParameterMap,TEXT("ReflectionEnvironmentColorTexture"));
|
|
ReflectionEnvironmentColorSampler.Bind(Initializer.ParameterMap,TEXT("ReflectionEnvironmentColorSampler"));
|
|
NumCaptureArrayMips.Bind(Initializer.ParameterMap, TEXT("NumCaptureArrayMips"));
|
|
}
|
|
|
|
FComputeBrightnessPS()
|
|
{
|
|
}
|
|
|
|
void SetParameters(FRHICommandList& RHICmdList, int32 TargetSize, FSceneRenderTargetItem& Cubemap)
|
|
{
|
|
const int32 EffectiveTopMipSize = TargetSize;
|
|
const int32 NumMips = FMath::CeilLogTwo(EffectiveTopMipSize) + 1;
|
|
// Read from the smallest mip that was downsampled to
|
|
|
|
if (Cubemap.IsValid())
|
|
{
|
|
SetTextureParameter(
|
|
RHICmdList,
|
|
GetPixelShader(),
|
|
ReflectionEnvironmentColorTexture,
|
|
ReflectionEnvironmentColorSampler,
|
|
TStaticSamplerState<SF_Trilinear, AM_Clamp, AM_Clamp, AM_Clamp>::GetRHI(),
|
|
Cubemap.ShaderResourceTexture);
|
|
}
|
|
|
|
SetShaderValue(RHICmdList, GetPixelShader(), NumCaptureArrayMips, FMath::CeilLogTwo(TargetSize) + 1);
|
|
}
|
|
|
|
virtual bool Serialize(FArchive& Ar) override
|
|
{
|
|
bool bShaderHasOutdatedParameters = FGlobalShader::Serialize(Ar);
|
|
Ar << ReflectionEnvironmentColorTexture;
|
|
Ar << ReflectionEnvironmentColorSampler;
|
|
Ar << NumCaptureArrayMips;
|
|
return bShaderHasOutdatedParameters;
|
|
}
|
|
|
|
private:
|
|
|
|
FShaderResourceParameter ReflectionEnvironmentColorTexture;
|
|
FShaderResourceParameter ReflectionEnvironmentColorSampler;
|
|
FShaderParameter NumCaptureArrayMips;
|
|
};
|
|
|
|
IMPLEMENT_SHADER_TYPE(,FComputeBrightnessPS,TEXT("/Engine/Private/ReflectionEnvironmentShaders.usf"),TEXT("ComputeBrightnessMain"),SF_Pixel);
|
|
|
|
void CreateCubeMips( FRHICommandListImmediate& RHICmdList, ERHIFeatureLevel::Type FeatureLevel, int32 NumMips, FSceneRenderTargetItem& Cubemap )
|
|
{
|
|
SCOPED_DRAW_EVENT(RHICmdList, CreateCubeMips);
|
|
|
|
FTextureRHIParamRef CubeRef = Cubemap.TargetableTexture.GetReference();
|
|
|
|
if (GSupportsGenerateMips)
|
|
{
|
|
RHICmdList.GenerateMips(CubeRef/*, NumMips*/);
|
|
}
|
|
else
|
|
{
|
|
auto ShaderMap = GetGlobalShaderMap(FeatureLevel);
|
|
|
|
FGraphicsPipelineStateInitializer GraphicsPSOInit;
|
|
GraphicsPSOInit.RasterizerState = TStaticRasterizerState<FM_Solid, CM_None>::GetRHI();
|
|
GraphicsPSOInit.DepthStencilState = TStaticDepthStencilState<false, CF_Always>::GetRHI();
|
|
GraphicsPSOInit.BlendState = TStaticBlendState<>::GetRHI();
|
|
|
|
//Use RWBarrier since we don't transition individual subresources. Basically treat the whole texture as R/W as we walk down the mip chain.
|
|
RHICmdList.TransitionResources(EResourceTransitionAccess::ERWSubResBarrier, &CubeRef, 1);
|
|
|
|
// Downsample all the mips, each one reads from the mip above it
|
|
for (int32 MipIndex = 1; MipIndex < NumMips; MipIndex++)
|
|
{
|
|
const int32 MipSize = 1 << (NumMips - MipIndex - 1);
|
|
|
|
for (int32 CubeFace = 0; CubeFace < CubeFace_MAX; CubeFace++)
|
|
{
|
|
SetRenderTarget(RHICmdList, Cubemap.TargetableTexture, MipIndex, CubeFace, NULL, false);
|
|
RHICmdList.ApplyCachedRenderTargets(GraphicsPSOInit);
|
|
|
|
const FIntRect ViewRect(0, 0, MipSize, MipSize);
|
|
RHICmdList.SetViewport(0, 0, 0.0f, MipSize, MipSize, 1.0f);
|
|
|
|
|
|
TShaderMapRef<FScreenVS> VertexShader(ShaderMap);
|
|
TShaderMapRef<FCubeFilterPS> PixelShader(ShaderMap);
|
|
|
|
GraphicsPSOInit.BoundShaderState.VertexDeclarationRHI = GFilterVertexDeclaration.VertexDeclarationRHI;
|
|
GraphicsPSOInit.BoundShaderState.VertexShaderRHI = GETSAFERHISHADER_VERTEX(*VertexShader);
|
|
GraphicsPSOInit.BoundShaderState.PixelShaderRHI = GETSAFERHISHADER_PIXEL(*PixelShader);
|
|
GraphicsPSOInit.PrimitiveType = PT_TriangleList;
|
|
|
|
SetGraphicsPipelineState(RHICmdList, GraphicsPSOInit);
|
|
|
|
{
|
|
const FPixelShaderRHIParamRef ShaderRHI = PixelShader->GetPixelShader();
|
|
|
|
SetShaderValue(RHICmdList, ShaderRHI, PixelShader->CubeFace, CubeFace);
|
|
SetShaderValue(RHICmdList, ShaderRHI, PixelShader->MipIndex, MipIndex);
|
|
|
|
SetShaderValue(RHICmdList, ShaderRHI, PixelShader->NumMips, NumMips);
|
|
|
|
SetSRVParameter(RHICmdList, ShaderRHI, PixelShader->SourceTexture, Cubemap.MipSRVs[MipIndex - 1]);
|
|
SetSamplerParameter(RHICmdList, ShaderRHI, PixelShader->SourceTextureSampler, TStaticSamplerState<SF_Bilinear, AM_Clamp, AM_Clamp, AM_Clamp>::GetRHI());
|
|
}
|
|
|
|
DrawRectangle(
|
|
RHICmdList,
|
|
ViewRect.Min.X, ViewRect.Min.Y,
|
|
ViewRect.Width(), ViewRect.Height(),
|
|
ViewRect.Min.X, ViewRect.Min.Y,
|
|
ViewRect.Width(), ViewRect.Height(),
|
|
FIntPoint(ViewRect.Width(), ViewRect.Height()),
|
|
FIntPoint(MipSize, MipSize),
|
|
*VertexShader);
|
|
|
|
//Use ERWSubResBarrier since we don't transition individual subresources. Basically treat the whole texture as R/W as we walk down the mip chain.
|
|
RHICmdList.TransitionResources(EResourceTransitionAccess::ERWSubResBarrier, &CubeRef, 1);
|
|
}
|
|
}
|
|
|
|
RHICmdList.TransitionResources(EResourceTransitionAccess::EReadable, &CubeRef, 1);
|
|
}
|
|
}
|
|
|
|
/** Computes the average brightness of the given reflection capture and stores it in the scene. */
|
|
float ComputeSingleAverageBrightnessFromCubemap(FRHICommandListImmediate& RHICmdList, ERHIFeatureLevel::Type FeatureLevel, int32 TargetSize, FSceneRenderTargetItem& Cubemap)
|
|
{
|
|
SCOPED_DRAW_EVENT(RHICmdList, ComputeSingleAverageBrightnessFromCubemap);
|
|
|
|
TRefCountPtr<IPooledRenderTarget> ReflectionBrightnessTarget;
|
|
FPooledRenderTargetDesc Desc(FPooledRenderTargetDesc::Create2DDesc(FIntPoint(1, 1), PF_FloatRGBA, FClearValueBinding::None, TexCreate_None, TexCreate_RenderTargetable, false));
|
|
GRenderTargetPool.FindFreeElement(RHICmdList, Desc, ReflectionBrightnessTarget, TEXT("ReflectionBrightness"));
|
|
|
|
FTextureRHIRef& BrightnessTarget = ReflectionBrightnessTarget->GetRenderTargetItem().TargetableTexture;
|
|
SetRenderTarget(RHICmdList, BrightnessTarget, NULL, true);
|
|
|
|
FGraphicsPipelineStateInitializer GraphicsPSOInit;
|
|
RHICmdList.ApplyCachedRenderTargets(GraphicsPSOInit);
|
|
GraphicsPSOInit.RasterizerState = TStaticRasterizerState<FM_Solid, CM_None>::GetRHI();
|
|
GraphicsPSOInit.DepthStencilState = TStaticDepthStencilState<false, CF_Always>::GetRHI();
|
|
GraphicsPSOInit.BlendState = TStaticBlendState<>::GetRHI();
|
|
|
|
auto ShaderMap = GetGlobalShaderMap(FeatureLevel);
|
|
TShaderMapRef<FPostProcessVS> VertexShader(ShaderMap);
|
|
TShaderMapRef<FComputeBrightnessPS> PixelShader(ShaderMap);
|
|
|
|
GraphicsPSOInit.BoundShaderState.VertexDeclarationRHI = GFilterVertexDeclaration.VertexDeclarationRHI;
|
|
GraphicsPSOInit.BoundShaderState.VertexShaderRHI = GETSAFERHISHADER_VERTEX(*VertexShader);
|
|
GraphicsPSOInit.BoundShaderState.PixelShaderRHI = GETSAFERHISHADER_PIXEL(*PixelShader);
|
|
GraphicsPSOInit.PrimitiveType = PT_TriangleList;
|
|
|
|
SetGraphicsPipelineState(RHICmdList, GraphicsPSOInit);
|
|
|
|
PixelShader->SetParameters(RHICmdList, TargetSize, Cubemap);
|
|
|
|
DrawRectangle(
|
|
RHICmdList,
|
|
0, 0,
|
|
1, 1,
|
|
0, 0,
|
|
1, 1,
|
|
FIntPoint(1, 1),
|
|
FIntPoint(1, 1),
|
|
*VertexShader);
|
|
|
|
RHICmdList.CopyToResolveTarget(BrightnessTarget, BrightnessTarget, true, FResolveParams());
|
|
|
|
FSceneRenderTargetItem& EffectiveRT = ReflectionBrightnessTarget->GetRenderTargetItem();
|
|
check(EffectiveRT.ShaderResourceTexture->GetFormat() == PF_FloatRGBA);
|
|
|
|
TArray<FFloat16Color> SurfaceData;
|
|
RHICmdList.ReadSurfaceFloatData(EffectiveRT.ShaderResourceTexture, FIntRect(0, 0, 1, 1), SurfaceData, CubeFace_PosX, 0, 0);
|
|
|
|
float AverageBrightness = SurfaceData[0].R.GetFloat();
|
|
return AverageBrightness;
|
|
}
|
|
|
|
void ComputeAverageBrightness(FRHICommandListImmediate& RHICmdList, ERHIFeatureLevel::Type FeatureLevel, int32 CubmapSize, float& OutAverageBrightness)
|
|
{
|
|
SCOPED_DRAW_EVENT(RHICmdList, ComputeAverageBrightness);
|
|
|
|
const int32 EffectiveTopMipSize = CubmapSize;
|
|
const int32 NumMips = FMath::CeilLogTwo(EffectiveTopMipSize) + 1;
|
|
|
|
// necessary to resolve the clears which touched all the mips. scene rendering only resolves mip 0.
|
|
FullyResolveReflectionScratchCubes(RHICmdList);
|
|
|
|
FSceneRenderTargetItem& DownSampledCube = FSceneRenderTargets::Get(RHICmdList).ReflectionColorScratchCubemap[0]->GetRenderTargetItem();
|
|
CreateCubeMips( RHICmdList, FeatureLevel, NumMips, DownSampledCube );
|
|
|
|
OutAverageBrightness = ComputeSingleAverageBrightnessFromCubemap(RHICmdList, FeatureLevel, CubmapSize, DownSampledCube);
|
|
}
|
|
|
|
/** Generates mips for glossiness and filters the cubemap for a given reflection. */
|
|
void FilterReflectionEnvironment(FRHICommandListImmediate& RHICmdList, ERHIFeatureLevel::Type FeatureLevel, int32 CubmapSize, FSHVectorRGB3* OutIrradianceEnvironmentMap)
|
|
{
|
|
SCOPED_DRAW_EVENT(RHICmdList, FilterReflectionEnvironment);
|
|
|
|
const int32 EffectiveTopMipSize = CubmapSize;
|
|
const int32 NumMips = FMath::CeilLogTwo(EffectiveTopMipSize) + 1;
|
|
|
|
FSceneRenderTargetItem& EffectiveColorRT = FSceneRenderTargets::Get(RHICmdList).ReflectionColorScratchCubemap[0]->GetRenderTargetItem();
|
|
|
|
FGraphicsPipelineStateInitializer GraphicsPSOInit;
|
|
GraphicsPSOInit.RasterizerState = TStaticRasterizerState<FM_Solid, CM_None>::GetRHI();
|
|
GraphicsPSOInit.DepthStencilState = TStaticDepthStencilState<false, CF_Always>::GetRHI();
|
|
GraphicsPSOInit.BlendState = TStaticBlendState<CW_RGBA, BO_Add, BF_Zero, BF_DestAlpha, BO_Add, BF_Zero, BF_One>::GetRHI();
|
|
|
|
// Premultiply alpha in-place using alpha blending
|
|
for (uint32 CubeFace = 0; CubeFace < CubeFace_MAX; CubeFace++)
|
|
{
|
|
SetRenderTarget(RHICmdList, EffectiveColorRT.TargetableTexture, 0, CubeFace, NULL, true);
|
|
RHICmdList.ApplyCachedRenderTargets(GraphicsPSOInit);
|
|
|
|
const FIntPoint SourceDimensions(CubmapSize, CubmapSize);
|
|
const FIntRect ViewRect(0, 0, EffectiveTopMipSize, EffectiveTopMipSize);
|
|
RHICmdList.SetViewport(0, 0, 0.0f, EffectiveTopMipSize, EffectiveTopMipSize, 1.0f);
|
|
|
|
TShaderMapRef<FScreenVS> VertexShader(GetGlobalShaderMap(FeatureLevel));
|
|
TShaderMapRef<FOneColorPS> PixelShader(GetGlobalShaderMap(FeatureLevel));
|
|
|
|
GraphicsPSOInit.BoundShaderState.VertexDeclarationRHI = GFilterVertexDeclaration.VertexDeclarationRHI;
|
|
GraphicsPSOInit.BoundShaderState.VertexShaderRHI = GETSAFERHISHADER_VERTEX(*VertexShader);
|
|
GraphicsPSOInit.BoundShaderState.PixelShaderRHI = GETSAFERHISHADER_PIXEL(*PixelShader);
|
|
GraphicsPSOInit.PrimitiveType = PT_TriangleList;
|
|
|
|
SetGraphicsPipelineState(RHICmdList, GraphicsPSOInit);
|
|
|
|
FLinearColor UnusedColors[1] = { FLinearColor::Black };
|
|
PixelShader->SetColors(RHICmdList, UnusedColors, ARRAY_COUNT(UnusedColors));
|
|
|
|
DrawRectangle(
|
|
RHICmdList,
|
|
ViewRect.Min.X, ViewRect.Min.Y,
|
|
ViewRect.Width(), ViewRect.Height(),
|
|
0, 0,
|
|
SourceDimensions.X, SourceDimensions.Y,
|
|
FIntPoint(ViewRect.Width(), ViewRect.Height()),
|
|
SourceDimensions,
|
|
*VertexShader);
|
|
|
|
RHICmdList.CopyToResolveTarget(EffectiveColorRT.TargetableTexture, EffectiveColorRT.ShaderResourceTexture, true, FResolveParams(FResolveRect(), (ECubeFace)CubeFace));
|
|
}
|
|
|
|
auto ShaderMap = GetGlobalShaderMap(FeatureLevel);
|
|
FSceneRenderTargets& SceneContext = FSceneRenderTargets::Get(RHICmdList);
|
|
|
|
FSceneRenderTargetItem& DownSampledCube = FSceneRenderTargets::Get(RHICmdList).ReflectionColorScratchCubemap[0]->GetRenderTargetItem();
|
|
FSceneRenderTargetItem& FilteredCube = FSceneRenderTargets::Get(RHICmdList).ReflectionColorScratchCubemap[1]->GetRenderTargetItem();
|
|
|
|
CreateCubeMips( RHICmdList, FeatureLevel, NumMips, DownSampledCube );
|
|
|
|
if (OutIrradianceEnvironmentMap)
|
|
{
|
|
SCOPED_DRAW_EVENT(RHICmdList, ComputeDiffuseIrradiance);
|
|
|
|
const int32 NumDiffuseMips = FMath::CeilLogTwo( GDiffuseIrradianceCubemapSize ) + 1;
|
|
const int32 DiffuseConvolutionSourceMip = NumMips - NumDiffuseMips;
|
|
|
|
ComputeDiffuseIrradiance(RHICmdList, FeatureLevel, DownSampledCube.ShaderResourceTexture, DiffuseConvolutionSourceMip, OutIrradianceEnvironmentMap);
|
|
}
|
|
|
|
{
|
|
SCOPED_DRAW_EVENT(RHICmdList, FilterCubeMap);
|
|
|
|
GraphicsPSOInit.RasterizerState = TStaticRasterizerState<FM_Solid, CM_None>::GetRHI();
|
|
GraphicsPSOInit.DepthStencilState = TStaticDepthStencilState<false, CF_Always>::GetRHI();
|
|
GraphicsPSOInit.BlendState = TStaticBlendState<>::GetRHI();
|
|
|
|
// Filter all the mips
|
|
for (int32 MipIndex = 0; MipIndex < NumMips; MipIndex++)
|
|
{
|
|
const int32 MipSize = 1 << (NumMips - MipIndex - 1);
|
|
|
|
for (int32 CubeFace = 0; CubeFace < CubeFace_MAX; CubeFace++)
|
|
{
|
|
SetRenderTarget(RHICmdList, FilteredCube.TargetableTexture, MipIndex, CubeFace, NULL, true);
|
|
RHICmdList.ApplyCachedRenderTargets(GraphicsPSOInit);
|
|
|
|
const FIntRect ViewRect(0, 0, MipSize, MipSize);
|
|
RHICmdList.SetViewport(0, 0, 0.0f, MipSize, MipSize, 1.0f);
|
|
|
|
|
|
TShaderMapRef<FScreenVS> VertexShader(GetGlobalShaderMap(FeatureLevel));
|
|
TShaderMapRef< TCubeFilterPS<1> > CaptureCubemapArrayPixelShader(GetGlobalShaderMap(FeatureLevel));
|
|
|
|
FCubeFilterPS* PixelShader;
|
|
|
|
PixelShader = *TShaderMapRef< TCubeFilterPS<0> >(ShaderMap);
|
|
check(PixelShader);
|
|
|
|
GraphicsPSOInit.BoundShaderState.VertexDeclarationRHI = GFilterVertexDeclaration.VertexDeclarationRHI;
|
|
GraphicsPSOInit.BoundShaderState.VertexShaderRHI = GETSAFERHISHADER_VERTEX(*VertexShader);
|
|
GraphicsPSOInit.BoundShaderState.PixelShaderRHI = GETSAFERHISHADER_PIXEL(PixelShader);
|
|
GraphicsPSOInit.PrimitiveType = PT_TriangleList;
|
|
|
|
SetGraphicsPipelineState(RHICmdList, GraphicsPSOInit);
|
|
|
|
{
|
|
const FPixelShaderRHIParamRef ShaderRHI = PixelShader->GetPixelShader();
|
|
|
|
SetShaderValue( RHICmdList, ShaderRHI, PixelShader->CubeFace, CubeFace );
|
|
SetShaderValue( RHICmdList, ShaderRHI, PixelShader->MipIndex, MipIndex );
|
|
|
|
SetShaderValue( RHICmdList, ShaderRHI, PixelShader->NumMips, NumMips );
|
|
|
|
SetTextureParameter(
|
|
RHICmdList,
|
|
ShaderRHI,
|
|
PixelShader->SourceTexture,
|
|
PixelShader->SourceTextureSampler,
|
|
TStaticSamplerState<SF_Trilinear, AM_Clamp, AM_Clamp, AM_Clamp>::GetRHI(),
|
|
DownSampledCube.ShaderResourceTexture);
|
|
}
|
|
|
|
DrawRectangle(
|
|
RHICmdList,
|
|
ViewRect.Min.X, ViewRect.Min.Y,
|
|
ViewRect.Width(), ViewRect.Height(),
|
|
ViewRect.Min.X, ViewRect.Min.Y,
|
|
ViewRect.Width(), ViewRect.Height(),
|
|
FIntPoint(ViewRect.Width(), ViewRect.Height()),
|
|
FIntPoint(MipSize, MipSize),
|
|
*VertexShader);
|
|
|
|
RHICmdList.CopyToResolveTarget(FilteredCube.TargetableTexture, FilteredCube.ShaderResourceTexture, true, FResolveParams(FResolveRect(), (ECubeFace)CubeFace, MipIndex));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/** Vertex shader used when writing to a cubemap. */
|
|
class FCopyToCubeFaceVS : public FGlobalShader
|
|
{
|
|
DECLARE_SHADER_TYPE(FCopyToCubeFaceVS,Global);
|
|
public:
|
|
|
|
static bool ShouldCache(EShaderPlatform Platform)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
FCopyToCubeFaceVS() {}
|
|
FCopyToCubeFaceVS(const ShaderMetaType::CompiledShaderInitializerType& Initializer):
|
|
FGlobalShader(Initializer)
|
|
{
|
|
}
|
|
|
|
void SetParameters(FRHICommandList& RHICmdList, const FViewInfo& View)
|
|
{
|
|
FGlobalShader::SetParameters<FViewUniformShaderParameters>(RHICmdList, GetVertexShader(),View.ViewUniformBuffer);
|
|
}
|
|
|
|
virtual bool Serialize(FArchive& Ar) override
|
|
{
|
|
bool bShaderHasOutdatedParameters = FGlobalShader::Serialize(Ar);
|
|
return bShaderHasOutdatedParameters;
|
|
}
|
|
};
|
|
|
|
IMPLEMENT_SHADER_TYPE(,FCopyToCubeFaceVS,TEXT("/Engine/Private/ReflectionEnvironmentShaders.usf"),TEXT("CopyToCubeFaceVS"),SF_Vertex);
|
|
|
|
/** Pixel shader used when copying scene color from a scene render into a face of a reflection capture cubemap. */
|
|
class FCopySceneColorToCubeFacePS : public FGlobalShader
|
|
{
|
|
DECLARE_SHADER_TYPE(FCopySceneColorToCubeFacePS,Global);
|
|
public:
|
|
|
|
static bool ShouldCache(EShaderPlatform Platform)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
static void ModifyCompilationEnvironment(EShaderPlatform Platform, FShaderCompilerEnvironment& OutEnvironment)
|
|
{
|
|
FGlobalShader::ModifyCompilationEnvironment(Platform,OutEnvironment);
|
|
}
|
|
|
|
FCopySceneColorToCubeFacePS(const ShaderMetaType::CompiledShaderInitializerType& Initializer):
|
|
FGlobalShader(Initializer)
|
|
{
|
|
DeferredParameters.Bind(Initializer.ParameterMap);
|
|
InTexture.Bind(Initializer.ParameterMap,TEXT("InTexture"));
|
|
InTextureSampler.Bind(Initializer.ParameterMap,TEXT("InTextureSampler"));
|
|
SkyLightCaptureParameters.Bind(Initializer.ParameterMap,TEXT("SkyLightCaptureParameters"));
|
|
LowerHemisphereColor.Bind(Initializer.ParameterMap,TEXT("LowerHemisphereColor"));
|
|
}
|
|
FCopySceneColorToCubeFacePS() {}
|
|
|
|
void SetParameters(FRHICommandList& RHICmdList, const FViewInfo& View, bool bCapturingForSkyLight, bool bLowerHemisphereIsBlack, const FLinearColor& LowerHemisphereColorValue)
|
|
{
|
|
const FPixelShaderRHIParamRef ShaderRHI = GetPixelShader();
|
|
|
|
FGlobalShader::SetParameters<FViewUniformShaderParameters>(RHICmdList, ShaderRHI, View.ViewUniformBuffer);
|
|
DeferredParameters.Set(RHICmdList, ShaderRHI, View, MD_PostProcess);
|
|
|
|
SetTextureParameter(
|
|
RHICmdList,
|
|
ShaderRHI,
|
|
InTexture,
|
|
InTextureSampler,
|
|
TStaticSamplerState<SF_Bilinear, AM_Clamp, AM_Clamp, AM_Clamp>::GetRHI(),
|
|
FSceneRenderTargets::Get(RHICmdList).GetSceneColor()->GetRenderTargetItem().ShaderResourceTexture);
|
|
|
|
FVector SkyLightParametersValue = FVector::ZeroVector;
|
|
FScene* Scene = (FScene*)View.Family->Scene;
|
|
|
|
if (bCapturingForSkyLight)
|
|
{
|
|
// When capturing reflection captures, support forcing all low hemisphere lighting to be black
|
|
SkyLightParametersValue = FVector(0, 0, bLowerHemisphereIsBlack ? 1.0f : 0.0f);
|
|
}
|
|
else if (Scene->SkyLight && !Scene->SkyLight->bHasStaticLighting)
|
|
{
|
|
// When capturing reflection captures and there's a stationary sky light, mask out any pixels whose depth classify it as part of the sky
|
|
// This will allow changing the stationary sky light at runtime
|
|
SkyLightParametersValue = FVector(1, Scene->SkyLight->SkyDistanceThreshold, 0);
|
|
}
|
|
else
|
|
{
|
|
// When capturing reflection captures and there's no sky light, or only a static sky light, capture all depth ranges
|
|
SkyLightParametersValue = FVector(2, 0, 0);
|
|
}
|
|
|
|
SetShaderValue(RHICmdList, ShaderRHI, SkyLightCaptureParameters, SkyLightParametersValue);
|
|
SetShaderValue(RHICmdList, ShaderRHI, LowerHemisphereColor, LowerHemisphereColorValue);
|
|
}
|
|
|
|
virtual bool Serialize(FArchive& Ar) override
|
|
{
|
|
bool bShaderHasOutdatedParameters = FGlobalShader::Serialize(Ar);
|
|
Ar << DeferredParameters;
|
|
Ar << InTexture;
|
|
Ar << InTextureSampler;
|
|
Ar << SkyLightCaptureParameters;
|
|
Ar << LowerHemisphereColor;
|
|
return bShaderHasOutdatedParameters;
|
|
}
|
|
|
|
private:
|
|
FDeferredPixelShaderParameters DeferredParameters;
|
|
FShaderResourceParameter InTexture;
|
|
FShaderResourceParameter InTextureSampler;
|
|
FShaderParameter SkyLightCaptureParameters;
|
|
FShaderParameter LowerHemisphereColor;
|
|
};
|
|
|
|
IMPLEMENT_SHADER_TYPE(,FCopySceneColorToCubeFacePS,TEXT("/Engine/Private/ReflectionEnvironmentShaders.usf"),TEXT("CopySceneColorToCubeFaceColorPS"),SF_Pixel);
|
|
|
|
/** Pixel shader used when copying a cubemap into a face of a reflection capture cubemap. */
|
|
class FCopyCubemapToCubeFacePS : public FGlobalShader
|
|
{
|
|
DECLARE_SHADER_TYPE(FCopyCubemapToCubeFacePS,Global);
|
|
public:
|
|
|
|
static bool ShouldCache(EShaderPlatform Platform)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
FCopyCubemapToCubeFacePS(const ShaderMetaType::CompiledShaderInitializerType& Initializer):
|
|
FGlobalShader(Initializer)
|
|
{
|
|
CubeFace.Bind(Initializer.ParameterMap,TEXT("CubeFace"));
|
|
SourceTexture.Bind(Initializer.ParameterMap,TEXT("SourceTexture"));
|
|
SourceTextureSampler.Bind(Initializer.ParameterMap,TEXT("SourceTextureSampler"));
|
|
SkyLightCaptureParameters.Bind(Initializer.ParameterMap,TEXT("SkyLightCaptureParameters"));
|
|
LowerHemisphereColor.Bind(Initializer.ParameterMap,TEXT("LowerHemisphereColor"));
|
|
SinCosSourceCubemapRotation.Bind(Initializer.ParameterMap,TEXT("SinCosSourceCubemapRotation"));
|
|
}
|
|
FCopyCubemapToCubeFacePS() {}
|
|
|
|
void SetParameters(FRHICommandList& RHICmdList, const FTexture* SourceCubemap, uint32 CubeFaceValue, bool bIsSkyLight, bool bLowerHemisphereIsBlack, float SourceCubemapRotation, const FLinearColor& LowerHemisphereColorValue)
|
|
{
|
|
const FPixelShaderRHIParamRef ShaderRHI = GetPixelShader();
|
|
|
|
SetShaderValue(RHICmdList, ShaderRHI, CubeFace, CubeFaceValue);
|
|
|
|
SetTextureParameter(
|
|
RHICmdList,
|
|
ShaderRHI,
|
|
SourceTexture,
|
|
SourceTextureSampler,
|
|
SourceCubemap);
|
|
|
|
SetShaderValue(RHICmdList, ShaderRHI, SkyLightCaptureParameters, FVector(bIsSkyLight ? 1.0f : 0.0f, 0.0f, bLowerHemisphereIsBlack ? 1.0f : 0.0f));
|
|
SetShaderValue(RHICmdList, ShaderRHI, LowerHemisphereColor, LowerHemisphereColorValue);
|
|
|
|
SetShaderValue(RHICmdList, ShaderRHI, SinCosSourceCubemapRotation, FVector2D(FMath::Sin(SourceCubemapRotation), FMath::Cos(SourceCubemapRotation)));
|
|
}
|
|
|
|
virtual bool Serialize(FArchive& Ar) override
|
|
{
|
|
bool bShaderHasOutdatedParameters = FGlobalShader::Serialize(Ar);
|
|
Ar << CubeFace;
|
|
Ar << SourceTexture;
|
|
Ar << SourceTextureSampler;
|
|
Ar << SkyLightCaptureParameters;
|
|
Ar << LowerHemisphereColor;
|
|
Ar << SinCosSourceCubemapRotation;
|
|
return bShaderHasOutdatedParameters;
|
|
}
|
|
|
|
private:
|
|
FShaderParameter CubeFace;
|
|
FShaderResourceParameter SourceTexture;
|
|
FShaderResourceParameter SourceTextureSampler;
|
|
FShaderParameter SkyLightCaptureParameters;
|
|
FShaderParameter LowerHemisphereColor;
|
|
FShaderParameter SinCosSourceCubemapRotation;
|
|
};
|
|
|
|
IMPLEMENT_SHADER_TYPE(,FCopyCubemapToCubeFacePS,TEXT("/Engine/Private/ReflectionEnvironmentShaders.usf"),TEXT("CopyCubemapToCubeFaceColorPS"),SF_Pixel);
|
|
|
|
int32 FindOrAllocateCubemapIndex(FScene* Scene, const UReflectionCaptureComponent* Component)
|
|
{
|
|
int32 CaptureIndex = -1;
|
|
|
|
// Try to find an existing capture index for this component
|
|
const FCaptureComponentSceneState* CaptureSceneStatePtr = Scene->ReflectionSceneData.AllocatedReflectionCaptureState.Find(Component);
|
|
|
|
if (CaptureSceneStatePtr)
|
|
{
|
|
CaptureIndex = CaptureSceneStatePtr->CaptureIndex;
|
|
}
|
|
else
|
|
{
|
|
// Reuse a freed index if possible
|
|
CaptureIndex = Scene->ReflectionSceneData.CubemapArraySlotsUsed.FindAndSetFirstZeroBit();
|
|
if (CaptureIndex == INDEX_NONE)
|
|
{
|
|
// If we didn't find a free index, allocate a new one from the CubemapArraySlotsUsed bitfield
|
|
CaptureIndex = Scene->ReflectionSceneData.CubemapArraySlotsUsed.Num();
|
|
Scene->ReflectionSceneData.CubemapArraySlotsUsed.Add(true);
|
|
}
|
|
|
|
Scene->ReflectionSceneData.AllocatedReflectionCaptureState.Add(Component, FCaptureComponentSceneState(CaptureIndex));
|
|
|
|
check(CaptureIndex < GMaxNumReflectionCaptures);
|
|
}
|
|
|
|
check(CaptureIndex >= 0);
|
|
return CaptureIndex;
|
|
}
|
|
|
|
void ClearScratchCubemaps(FRHICommandList& RHICmdList, int32 TargetSize)
|
|
{
|
|
SCOPED_DRAW_EVENT(RHICmdList, ClearScratchCubemaps);
|
|
|
|
FSceneRenderTargets& SceneContext = FSceneRenderTargets::Get(RHICmdList);
|
|
SceneContext.AllocateReflectionTargets(RHICmdList, TargetSize);
|
|
// Clear scratch render targets to a consistent but noticeable value
|
|
// This makes debugging capture issues much easier, otherwise the random contents from previous captures is shown
|
|
|
|
FSceneRenderTargetItem& RT0 = SceneContext.ReflectionColorScratchCubemap[0]->GetRenderTargetItem();
|
|
int32 NumMips = (int32)RT0.TargetableTexture->GetNumMips();
|
|
|
|
{
|
|
SCOPED_DRAW_EVENT(RHICmdList, ClearScratchCubemapsRT0);
|
|
|
|
for (int32 MipIndex = 0; MipIndex < NumMips; MipIndex++)
|
|
{
|
|
for (int32 CubeFace = 0; CubeFace < CubeFace_MAX; CubeFace++)
|
|
{
|
|
TransitionSetRenderTargetsHelper(RHICmdList, RT0.TargetableTexture, FTextureRHIParamRef(), FExclusiveDepthStencil::DepthWrite_StencilWrite);
|
|
|
|
FRHIRenderTargetView RtView = FRHIRenderTargetView(RT0.TargetableTexture, ERenderTargetLoadAction::EClear, MipIndex, CubeFace);
|
|
FRHISetRenderTargetsInfo Info(1, &RtView, FRHIDepthRenderTargetView());
|
|
RHICmdList.SetRenderTargetsAndClear(Info);
|
|
}
|
|
}
|
|
}
|
|
|
|
{
|
|
SCOPED_DRAW_EVENT(RHICmdList, ClearScratchCubemapsRT1);
|
|
|
|
FSceneRenderTargetItem& RT1 = SceneContext.ReflectionColorScratchCubemap[1]->GetRenderTargetItem();
|
|
NumMips = (int32)RT1.TargetableTexture->GetNumMips();
|
|
|
|
for (int32 MipIndex = 0; MipIndex < NumMips; MipIndex++)
|
|
{
|
|
for (int32 CubeFace = 0; CubeFace < CubeFace_MAX; CubeFace++)
|
|
{
|
|
TransitionSetRenderTargetsHelper(RHICmdList, RT1.TargetableTexture, FTextureRHIParamRef(), FExclusiveDepthStencil::DepthWrite_StencilWrite);
|
|
|
|
FRHIRenderTargetView RtView = FRHIRenderTargetView(RT1.TargetableTexture, ERenderTargetLoadAction::EClear, MipIndex, CubeFace);
|
|
FRHISetRenderTargetsInfo Info(1, &RtView, FRHIDepthRenderTargetView());
|
|
RHICmdList.SetRenderTargetsAndClear(Info);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/** Captures the scene for a reflection capture by rendering the scene multiple times and copying into a cubemap texture. */
|
|
void CaptureSceneToScratchCubemap(FRHICommandListImmediate& RHICmdList, FSceneRenderer* SceneRenderer, ECubeFace CubeFace, int32 CubemapSize, bool bCapturingForSkyLight, bool bLowerHemisphereIsBlack, const FLinearColor& LowerHemisphereColor)
|
|
{
|
|
FMemMark MemStackMark(FMemStack::Get());
|
|
|
|
// update any resources that needed a deferred update
|
|
FDeferredUpdateResource::UpdateResources(RHICmdList);
|
|
|
|
const auto FeatureLevel = SceneRenderer->FeatureLevel;
|
|
|
|
{
|
|
SCOPED_DRAW_EVENT(RHICmdList, CubeMapCapture);
|
|
|
|
// Render the scene normally for one face of the cubemap
|
|
SceneRenderer->Render(RHICmdList);
|
|
check(&RHICmdList == &FRHICommandListExecutor::GetImmediateCommandList());
|
|
check(IsInRenderingThread());
|
|
{
|
|
QUICK_SCOPE_CYCLE_COUNTER(STAT_CaptureSceneToScratchCubemap_Flush);
|
|
FRHICommandListExecutor::GetImmediateCommandList().ImmediateFlush(EImmediateFlushType::FlushRHIThread);
|
|
}
|
|
|
|
// some platforms may not be able to keep enqueueing commands like crazy, this will
|
|
// allow them to restart their command buffers
|
|
RHICmdList.SubmitCommandsAndFlushGPU();
|
|
|
|
FSceneRenderTargets& SceneContext = FSceneRenderTargets::Get(RHICmdList);
|
|
SceneContext.AllocateReflectionTargets(RHICmdList, CubemapSize);
|
|
|
|
auto ShaderMap = GetGlobalShaderMap(FeatureLevel);
|
|
|
|
const int32 EffectiveSize = CubemapSize;
|
|
FSceneRenderTargetItem& EffectiveColorRT = SceneContext.ReflectionColorScratchCubemap[0]->GetRenderTargetItem();
|
|
|
|
{
|
|
SCOPED_DRAW_EVENT(RHICmdList, CubeMapCopyScene);
|
|
|
|
// Copy the captured scene into the cubemap face
|
|
SetRenderTarget(RHICmdList, EffectiveColorRT.TargetableTexture, 0, CubeFace, NULL);
|
|
|
|
const FIntRect ViewRect(0, 0, EffectiveSize, EffectiveSize);
|
|
RHICmdList.SetViewport(0, 0, 0.0f, EffectiveSize, EffectiveSize, 1.0f);
|
|
|
|
FGraphicsPipelineStateInitializer GraphicsPSOInit;
|
|
RHICmdList.ApplyCachedRenderTargets(GraphicsPSOInit);
|
|
GraphicsPSOInit.RasterizerState = TStaticRasterizerState<FM_Solid, CM_None>::GetRHI();
|
|
GraphicsPSOInit.DepthStencilState = TStaticDepthStencilState<false, CF_Always>::GetRHI();
|
|
GraphicsPSOInit.BlendState = TStaticBlendState<>::GetRHI();
|
|
|
|
TShaderMapRef<FCopyToCubeFaceVS> VertexShader(GetGlobalShaderMap(FeatureLevel));
|
|
TShaderMapRef<FCopySceneColorToCubeFacePS> PixelShader(GetGlobalShaderMap(FeatureLevel));
|
|
|
|
GraphicsPSOInit.BoundShaderState.VertexDeclarationRHI = GFilterVertexDeclaration.VertexDeclarationRHI;
|
|
GraphicsPSOInit.BoundShaderState.VertexShaderRHI = GETSAFERHISHADER_VERTEX(*VertexShader);
|
|
GraphicsPSOInit.BoundShaderState.PixelShaderRHI = GETSAFERHISHADER_PIXEL(*PixelShader);
|
|
GraphicsPSOInit.PrimitiveType = PT_TriangleList;
|
|
|
|
SetGraphicsPipelineState(RHICmdList, GraphicsPSOInit);
|
|
|
|
PixelShader->SetParameters(RHICmdList, SceneRenderer->Views[0], bCapturingForSkyLight, bLowerHemisphereIsBlack, LowerHemisphereColor);
|
|
VertexShader->SetParameters(RHICmdList, SceneRenderer->Views[0]);
|
|
|
|
DrawRectangle(
|
|
RHICmdList,
|
|
ViewRect.Min.X, ViewRect.Min.Y,
|
|
ViewRect.Width(), ViewRect.Height(),
|
|
ViewRect.Min.X, ViewRect.Min.Y,
|
|
ViewRect.Width() * GSupersampleCaptureFactor, ViewRect.Height() * GSupersampleCaptureFactor,
|
|
FIntPoint(ViewRect.Width(), ViewRect.Height()),
|
|
SceneContext.GetBufferSizeXY(),
|
|
*VertexShader);
|
|
|
|
RHICmdList.CopyToResolveTarget(EffectiveColorRT.TargetableTexture, EffectiveColorRT.ShaderResourceTexture, true, FResolveParams(FResolveRect(), CubeFace));
|
|
}
|
|
}
|
|
|
|
FSceneRenderer::WaitForTasksClearSnapshotsAndDeleteSceneRenderer(RHICmdList, SceneRenderer);
|
|
}
|
|
|
|
void CopyCubemapToScratchCubemap(FRHICommandList& RHICmdList, ERHIFeatureLevel::Type FeatureLevel, UTextureCube* SourceCubemap, int32 CubemapSize, bool bIsSkyLight, bool bLowerHemisphereIsBlack, float SourceCubemapRotation, const FLinearColor& LowerHemisphereColorValue)
|
|
{
|
|
SCOPED_DRAW_EVENT(RHICmdList, CopyCubemapToScratchCubemap);
|
|
check(SourceCubemap);
|
|
|
|
const int32 EffectiveSize = CubemapSize;
|
|
FSceneRenderTargetItem& EffectiveColorRT = FSceneRenderTargets::Get(RHICmdList).ReflectionColorScratchCubemap[0]->GetRenderTargetItem();
|
|
|
|
for (uint32 CubeFace = 0; CubeFace < CubeFace_MAX; CubeFace++)
|
|
{
|
|
// Copy the captured scene into the cubemap face
|
|
SetRenderTarget(RHICmdList, EffectiveColorRT.TargetableTexture, 0, CubeFace, NULL, true);
|
|
|
|
const FTexture* SourceCubemapResource = SourceCubemap->Resource;
|
|
const FIntPoint SourceDimensions(SourceCubemapResource->GetSizeX(), SourceCubemapResource->GetSizeY());
|
|
const FIntRect ViewRect(0, 0, EffectiveSize, EffectiveSize);
|
|
RHICmdList.SetViewport(0, 0, 0.0f, EffectiveSize, EffectiveSize, 1.0f);
|
|
|
|
FGraphicsPipelineStateInitializer GraphicsPSOInit;
|
|
RHICmdList.ApplyCachedRenderTargets(GraphicsPSOInit);
|
|
GraphicsPSOInit.RasterizerState = TStaticRasterizerState<FM_Solid, CM_None>::GetRHI();
|
|
GraphicsPSOInit.DepthStencilState = TStaticDepthStencilState<false, CF_Always>::GetRHI();
|
|
GraphicsPSOInit.BlendState = TStaticBlendState<>::GetRHI();
|
|
|
|
TShaderMapRef<FScreenVS> VertexShader(GetGlobalShaderMap(FeatureLevel));
|
|
TShaderMapRef<FCopyCubemapToCubeFacePS> PixelShader(GetGlobalShaderMap(FeatureLevel));
|
|
|
|
GraphicsPSOInit.BoundShaderState.VertexDeclarationRHI = GFilterVertexDeclaration.VertexDeclarationRHI;
|
|
GraphicsPSOInit.BoundShaderState.VertexShaderRHI = GETSAFERHISHADER_VERTEX(*VertexShader);
|
|
GraphicsPSOInit.BoundShaderState.PixelShaderRHI = GETSAFERHISHADER_PIXEL(*PixelShader);
|
|
GraphicsPSOInit.PrimitiveType = PT_TriangleList;
|
|
|
|
SetGraphicsPipelineState(RHICmdList, GraphicsPSOInit);
|
|
PixelShader->SetParameters(RHICmdList, SourceCubemapResource, CubeFace, bIsSkyLight, bLowerHemisphereIsBlack, SourceCubemapRotation, LowerHemisphereColorValue);
|
|
|
|
DrawRectangle(
|
|
RHICmdList,
|
|
ViewRect.Min.X, ViewRect.Min.Y,
|
|
ViewRect.Width(), ViewRect.Height(),
|
|
0, 0,
|
|
SourceDimensions.X, SourceDimensions.Y,
|
|
FIntPoint(ViewRect.Width(), ViewRect.Height()),
|
|
SourceDimensions,
|
|
*VertexShader);
|
|
|
|
RHICmdList.CopyToResolveTarget(EffectiveColorRT.TargetableTexture, EffectiveColorRT.ShaderResourceTexture, true, FResolveParams(FResolveRect(), (ECubeFace)CubeFace));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Allocates reflection captures in the scene's reflection cubemap array and updates them by recapturing the scene.
|
|
* Existing captures will only be updated. Must be called from the game thread.
|
|
*/
|
|
void FScene::AllocateReflectionCaptures(const TArray<UReflectionCaptureComponent*>& NewCaptures)
|
|
{
|
|
if (NewCaptures.Num() > 0)
|
|
{
|
|
if (GetFeatureLevel() >= ERHIFeatureLevel::SM5)
|
|
{
|
|
for (int32 CaptureIndex = 0; CaptureIndex < NewCaptures.Num(); CaptureIndex++)
|
|
{
|
|
bool bAlreadyExists = false;
|
|
|
|
// Try to find an existing allocation
|
|
for (TSparseArray<UReflectionCaptureComponent*>::TIterator It(ReflectionSceneData.AllocatedReflectionCapturesGameThread); It; ++It)
|
|
{
|
|
UReflectionCaptureComponent* OtherComponent = *It;
|
|
|
|
if (OtherComponent == NewCaptures[CaptureIndex])
|
|
{
|
|
bAlreadyExists = true;
|
|
}
|
|
}
|
|
|
|
// Add the capture to the allocated list
|
|
if (!bAlreadyExists && ReflectionSceneData.AllocatedReflectionCapturesGameThread.Num() < GMaxNumReflectionCaptures)
|
|
{
|
|
ReflectionSceneData.AllocatedReflectionCapturesGameThread.Add(NewCaptures[CaptureIndex]);
|
|
}
|
|
}
|
|
|
|
// Request the exact amount needed by default
|
|
int32 DesiredMaxCubemaps = ReflectionSceneData.AllocatedReflectionCapturesGameThread.Num();
|
|
const float MaxCubemapsRoundUpBase = 1.5f;
|
|
|
|
// If this is not the first time the scene has allocated the cubemap array, include slack to reduce reallocations
|
|
if (ReflectionSceneData.MaxAllocatedReflectionCubemapsGameThread > 0)
|
|
{
|
|
float Exponent = FMath::LogX(MaxCubemapsRoundUpBase, ReflectionSceneData.AllocatedReflectionCapturesGameThread.Num());
|
|
|
|
// Round up to the next integer exponent to provide stability and reduce reallocations
|
|
DesiredMaxCubemaps = FMath::Pow(MaxCubemapsRoundUpBase, FMath::TruncToInt(Exponent) + 1);
|
|
}
|
|
|
|
DesiredMaxCubemaps = FMath::Min(DesiredMaxCubemaps, GMaxNumReflectionCaptures);
|
|
|
|
const int32 ReflectionCaptureSize = UReflectionCaptureComponent::GetReflectionCaptureSize_GameThread();
|
|
bool bNeedsUpdateAllCaptures = DesiredMaxCubemaps != ReflectionSceneData.MaxAllocatedReflectionCubemapsGameThread || ReflectionCaptureSize != ReflectionSceneData.CubemapArray.GetCubemapSize();
|
|
|
|
if (DoGPUArrayCopy() && bNeedsUpdateAllCaptures)
|
|
{
|
|
// If we're not in the editor, we discard the CPU-side reflection capture data after loading to save memory, so we can't resize if the resolution changes. If this happens, we assert
|
|
check(GIsEditor || ReflectionCaptureSize == ReflectionSceneData.CubemapArray.GetCubemapSize() || ReflectionSceneData.CubemapArray.GetCubemapSize() == 0);
|
|
|
|
if (ReflectionCaptureSize == ReflectionSceneData.CubemapArray.GetCubemapSize())
|
|
{
|
|
// We can do a fast GPU copy to realloc the array, so we don't need to update all captures
|
|
ReflectionSceneData.MaxAllocatedReflectionCubemapsGameThread = DesiredMaxCubemaps;
|
|
ENQUEUE_UNIQUE_RENDER_COMMAND_THREEPARAMETER(
|
|
GPUResizeArrayCommand,
|
|
FScene*, Scene, this,
|
|
uint32, MaxSize, ReflectionSceneData.MaxAllocatedReflectionCubemapsGameThread,
|
|
int32, ReflectionCaptureSize, ReflectionCaptureSize,
|
|
{
|
|
// Update the scene's cubemap array, preserving the original contents with a GPU-GPU copy
|
|
Scene->ReflectionSceneData.ResizeCubemapArrayGPU(MaxSize, ReflectionCaptureSize);
|
|
});
|
|
|
|
bNeedsUpdateAllCaptures = false;
|
|
}
|
|
}
|
|
|
|
if (bNeedsUpdateAllCaptures)
|
|
{
|
|
ReflectionSceneData.MaxAllocatedReflectionCubemapsGameThread = DesiredMaxCubemaps;
|
|
|
|
ENQUEUE_UNIQUE_RENDER_COMMAND_THREEPARAMETER(
|
|
ResizeArrayCommand,
|
|
FScene*, Scene, this,
|
|
uint32, MaxSize, ReflectionSceneData.MaxAllocatedReflectionCubemapsGameThread,
|
|
int32, ReflectionCaptureSize, ReflectionCaptureSize,
|
|
{
|
|
// Update the scene's cubemap array, which will reallocate it, so we no longer have the contents of existing entries
|
|
Scene->ReflectionSceneData.CubemapArray.UpdateMaxCubemaps(MaxSize, ReflectionCaptureSize);
|
|
});
|
|
|
|
// Recapture all reflection captures now that we have reallocated the cubemap array
|
|
UpdateAllReflectionCaptures();
|
|
}
|
|
else
|
|
{
|
|
// No teardown of the cubemap array was needed, just update the captures that were requested
|
|
for (TSparseArray<UReflectionCaptureComponent*>::TIterator It(ReflectionSceneData.AllocatedReflectionCapturesGameThread); It; ++It)
|
|
{
|
|
UReflectionCaptureComponent* CurrentComponent = *It;
|
|
|
|
if (NewCaptures.Contains(CurrentComponent))
|
|
{
|
|
UpdateReflectionCaptureContents(CurrentComponent);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
else if (GetFeatureLevel() == ERHIFeatureLevel::SM4)
|
|
{
|
|
for (int32 ComponentIndex = 0; ComponentIndex < NewCaptures.Num(); ComponentIndex++)
|
|
{
|
|
UReflectionCaptureComponent* CurrentComponent = NewCaptures[ComponentIndex];
|
|
UpdateReflectionCaptureContents(CurrentComponent);
|
|
}
|
|
}
|
|
|
|
for (int32 CaptureIndex = 0; CaptureIndex < NewCaptures.Num(); CaptureIndex++)
|
|
{
|
|
UReflectionCaptureComponent* Component = NewCaptures[CaptureIndex];
|
|
|
|
Component->SetCaptureCompleted();
|
|
|
|
if (Component->SceneProxy)
|
|
{
|
|
// Update the transform of the reflection capture
|
|
// This is not done earlier by the reflection capture when it detects that it is dirty,
|
|
// To ensure that the RT sees both the new transform and the new contents on the same frame.
|
|
Component->SendRenderTransform_Concurrent();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/** Updates the contents of all reflection captures in the scene. Must be called from the game thread. */
|
|
void FScene::UpdateAllReflectionCaptures()
|
|
{
|
|
if (IsReflectionEnvironmentAvailable(GetFeatureLevel()))
|
|
{
|
|
ENQUEUE_UNIQUE_RENDER_COMMAND_ONEPARAMETER(
|
|
CaptureCommand,
|
|
FScene*, Scene, this,
|
|
{
|
|
Scene->ReflectionSceneData.AllocatedReflectionCaptureState.Empty();
|
|
Scene->ReflectionSceneData.CubemapArraySlotsUsed.Reset();
|
|
});
|
|
|
|
const int32 UpdateDivisor = FMath::Max(ReflectionSceneData.AllocatedReflectionCapturesGameThread.Num() / 20, 1);
|
|
const bool bDisplayStatus = ReflectionSceneData.AllocatedReflectionCapturesGameThread.Num() > 50;
|
|
|
|
if (bDisplayStatus)
|
|
{
|
|
const FText Status = NSLOCTEXT("Engine", "BeginReflectionCapturesTask", "Updating Reflection Captures...");
|
|
GWarn->BeginSlowTask( Status, true );
|
|
GWarn->StatusUpdate(0, ReflectionSceneData.AllocatedReflectionCapturesGameThread.Num(), Status);
|
|
}
|
|
|
|
int32 CaptureIndex = 0;
|
|
|
|
for (TSparseArray<UReflectionCaptureComponent*>::TIterator It(ReflectionSceneData.AllocatedReflectionCapturesGameThread); It; ++It)
|
|
{
|
|
// Update progress occasionally
|
|
if (bDisplayStatus && CaptureIndex % UpdateDivisor == 0)
|
|
{
|
|
GWarn->UpdateProgress(CaptureIndex, ReflectionSceneData.AllocatedReflectionCapturesGameThread.Num());
|
|
}
|
|
|
|
CaptureIndex++;
|
|
UReflectionCaptureComponent* CurrentComponent = *It;
|
|
UpdateReflectionCaptureContents(CurrentComponent);
|
|
}
|
|
|
|
if (bDisplayStatus)
|
|
{
|
|
GWarn->EndSlowTask();
|
|
}
|
|
}
|
|
}
|
|
|
|
void GetReflectionCaptureData_RenderingThread(FRHICommandListImmediate& RHICmdList, FScene* Scene, const UReflectionCaptureComponent* Component, FReflectionCaptureFullHDR* OutDerivedData)
|
|
{
|
|
const FCaptureComponentSceneState* ComponentStatePtr = Scene->ReflectionSceneData.AllocatedReflectionCaptureState.Find(Component);
|
|
|
|
if (ComponentStatePtr)
|
|
{
|
|
FSceneRenderTargetItem& EffectiveDest = Scene->ReflectionSceneData.CubemapArray.GetRenderTarget();
|
|
|
|
const int32 CaptureIndex = ComponentStatePtr->CaptureIndex;
|
|
const int32 NumMips = EffectiveDest.ShaderResourceTexture->GetNumMips();
|
|
const int32 EffectiveTopMipSize = FMath::Pow(2, NumMips - 1);
|
|
|
|
TArray<uint8> CaptureData;
|
|
int32 CaptureDataSize = 0;
|
|
|
|
for (int32 MipIndex = 0; MipIndex < NumMips; MipIndex++)
|
|
{
|
|
const int32 MipSize = 1 << (NumMips - MipIndex - 1);
|
|
|
|
for (int32 CubeFace = 0; CubeFace < CubeFace_MAX; CubeFace++)
|
|
{
|
|
CaptureDataSize += MipSize * MipSize * sizeof(FFloat16Color);
|
|
}
|
|
}
|
|
|
|
CaptureData.Empty(CaptureDataSize);
|
|
CaptureData.AddZeroed(CaptureDataSize);
|
|
int32 MipBaseIndex = 0;
|
|
|
|
for (int32 MipIndex = 0; MipIndex < NumMips; MipIndex++)
|
|
{
|
|
check(EffectiveDest.ShaderResourceTexture->GetFormat() == PF_FloatRGBA);
|
|
const int32 MipSize = 1 << (NumMips - MipIndex - 1);
|
|
const int32 CubeFaceBytes = MipSize * MipSize * sizeof(FFloat16Color);
|
|
|
|
for (int32 CubeFace = 0; CubeFace < CubeFace_MAX; CubeFace++)
|
|
{
|
|
TArray<FFloat16Color> SurfaceData;
|
|
// Read each mip face
|
|
//@todo - do this without blocking the GPU so many times
|
|
//@todo - pool the temporary textures in RHIReadSurfaceFloatData instead of always creating new ones
|
|
RHICmdList.ReadSurfaceFloatData(EffectiveDest.ShaderResourceTexture, FIntRect(0, 0, MipSize, MipSize), SurfaceData, (ECubeFace)CubeFace, CaptureIndex, MipIndex);
|
|
const int32 DestIndex = MipBaseIndex + CubeFace * CubeFaceBytes;
|
|
uint8* FaceData = &CaptureData[DestIndex];
|
|
check(SurfaceData.Num() * SurfaceData.GetTypeSize() == CubeFaceBytes);
|
|
FMemory::Memcpy(FaceData, SurfaceData.GetData(), CubeFaceBytes);
|
|
}
|
|
|
|
MipBaseIndex += CubeFaceBytes * CubeFace_MAX;
|
|
}
|
|
|
|
OutDerivedData->InitializeFromUncompressedData(CaptureData, EffectiveTopMipSize);
|
|
}
|
|
}
|
|
|
|
void FScene::GetReflectionCaptureData(UReflectionCaptureComponent* Component, FReflectionCaptureFullHDR& OutDerivedData)
|
|
{
|
|
check(GetFeatureLevel() >= ERHIFeatureLevel::SM5);
|
|
|
|
ENQUEUE_UNIQUE_RENDER_COMMAND_THREEPARAMETER(
|
|
GetReflectionDataCommand,
|
|
FScene*,Scene,this,
|
|
const UReflectionCaptureComponent*,Component,Component,
|
|
FReflectionCaptureFullHDR*,OutDerivedData,&OutDerivedData,
|
|
{
|
|
GetReflectionCaptureData_RenderingThread(RHICmdList, Scene, Component, OutDerivedData);
|
|
});
|
|
|
|
// Necessary since the RT is writing to OutDerivedData directly
|
|
FlushRenderingCommands();
|
|
}
|
|
|
|
void UploadReflectionCapture_RenderingThread(FScene* Scene, const FReflectionCaptureFullHDR* FullHDRData, const UReflectionCaptureComponent* CaptureComponent)
|
|
{
|
|
const int32 EffectiveTopMipSize = FullHDRData->CubemapSize;
|
|
const int32 NumMips = FMath::CeilLogTwo(EffectiveTopMipSize) + 1;
|
|
|
|
const int32 CaptureIndex = FindOrAllocateCubemapIndex(Scene, CaptureComponent);
|
|
FTextureCubeRHIRef& CubeMapArray = (FTextureCubeRHIRef&)Scene->ReflectionSceneData.CubemapArray.GetRenderTarget().ShaderResourceTexture;
|
|
check(CubeMapArray->GetFormat() == PF_FloatRGBA);
|
|
|
|
TRefCountPtr<FReflectionCaptureUncompressedData> SourceCubemapData = FullHDRData->GetUncompressedData();
|
|
int32 MipBaseIndex = 0;
|
|
|
|
for (int32 MipIndex = 0; MipIndex < NumMips; MipIndex++)
|
|
{
|
|
const int32 MipSize = 1 << (NumMips - MipIndex - 1);
|
|
const int32 CubeFaceBytes = MipSize * MipSize * sizeof(FFloat16Color);
|
|
|
|
for (int32 CubeFace = 0; CubeFace < CubeFace_MAX; CubeFace++)
|
|
{
|
|
uint32 DestStride = 0;
|
|
uint8* DestBuffer = (uint8*)RHILockTextureCubeFace(CubeMapArray, CubeFace, CaptureIndex, MipIndex, RLM_WriteOnly, DestStride, false);
|
|
|
|
// Handle DestStride by copying each row
|
|
for (int32 Y = 0; Y < MipSize; Y++)
|
|
{
|
|
FFloat16Color* DestPtr = (FFloat16Color*)((uint8*)DestBuffer + Y * DestStride);
|
|
const int32 SourceIndex = MipBaseIndex + CubeFace * CubeFaceBytes + Y * MipSize * sizeof(FFloat16Color);
|
|
const uint8* SourcePtr = SourceCubemapData->GetData(SourceIndex);
|
|
FMemory::Memcpy(DestPtr, SourcePtr, MipSize * sizeof(FFloat16Color));
|
|
}
|
|
|
|
RHIUnlockTextureCubeFace(CubeMapArray, CubeFace, CaptureIndex, MipIndex, false);
|
|
}
|
|
|
|
MipBaseIndex += CubeFaceBytes * CubeFace_MAX;
|
|
}
|
|
}
|
|
|
|
/** Creates a transformation for a cubemap face, following the D3D cubemap layout. */
|
|
FMatrix CalcCubeFaceViewRotationMatrix(ECubeFace Face)
|
|
{
|
|
FMatrix Result(FMatrix::Identity);
|
|
|
|
static const FVector XAxis(1.f,0.f,0.f);
|
|
static const FVector YAxis(0.f,1.f,0.f);
|
|
static const FVector ZAxis(0.f,0.f,1.f);
|
|
|
|
// vectors we will need for our basis
|
|
FVector vUp(YAxis);
|
|
FVector vDir;
|
|
|
|
switch( Face )
|
|
{
|
|
case CubeFace_PosX:
|
|
vDir = XAxis;
|
|
break;
|
|
case CubeFace_NegX:
|
|
vDir = -XAxis;
|
|
break;
|
|
case CubeFace_PosY:
|
|
vUp = -ZAxis;
|
|
vDir = YAxis;
|
|
break;
|
|
case CubeFace_NegY:
|
|
vUp = ZAxis;
|
|
vDir = -YAxis;
|
|
break;
|
|
case CubeFace_PosZ:
|
|
vDir = ZAxis;
|
|
break;
|
|
case CubeFace_NegZ:
|
|
vDir = -ZAxis;
|
|
break;
|
|
}
|
|
|
|
// derive right vector
|
|
FVector vRight( vUp ^ vDir );
|
|
// create matrix from the 3 axes
|
|
Result = FBasisVectorMatrix( vRight, vUp, vDir, FVector::ZeroVector );
|
|
|
|
return Result;
|
|
}
|
|
|
|
/**
|
|
* Render target class required for rendering the scene.
|
|
* This doesn't actually allocate a render target as we read from scene color to get HDR results directly.
|
|
*/
|
|
class FCaptureRenderTarget : public FRenderResource, public FRenderTarget
|
|
{
|
|
public:
|
|
|
|
FCaptureRenderTarget() :
|
|
Size(0)
|
|
{}
|
|
|
|
virtual const FTexture2DRHIRef& GetRenderTargetTexture() const
|
|
{
|
|
static FTexture2DRHIRef DummyTexture;
|
|
return DummyTexture;
|
|
}
|
|
|
|
void SetSize(int32 TargetSize) { Size = TargetSize; }
|
|
virtual FIntPoint GetSizeXY() const { return FIntPoint(Size, Size); }
|
|
virtual float GetDisplayGamma() const { return 1.0f; }
|
|
|
|
private:
|
|
|
|
int32 Size;
|
|
};
|
|
|
|
TGlobalResource<FCaptureRenderTarget> GReflectionCaptureRenderTarget;
|
|
|
|
void CaptureSceneIntoScratchCubemap(
|
|
FScene* Scene,
|
|
FVector CapturePosition,
|
|
int32 CubemapSize,
|
|
bool bCapturingForSkyLight,
|
|
bool bStaticSceneOnly,
|
|
float SkyLightNearPlane,
|
|
bool bLowerHemisphereIsBlack,
|
|
bool bCaptureEmissiveOnly,
|
|
const FLinearColor& LowerHemisphereColor
|
|
)
|
|
{
|
|
for (int32 CubeFace = 0; CubeFace < CubeFace_MAX; CubeFace++)
|
|
{
|
|
if( !bCapturingForSkyLight )
|
|
{
|
|
// Alert the RHI that we're rendering a new frame
|
|
// Not really a new frame, but it will allow pooling mechanisms to update, like the uniform buffer pool
|
|
ENQUEUE_UNIQUE_RENDER_COMMAND(
|
|
BeginFrame,
|
|
{
|
|
GFrameNumberRenderThread++;
|
|
RHICmdList.BeginFrame();
|
|
})
|
|
}
|
|
|
|
GReflectionCaptureRenderTarget.SetSize(CubemapSize);
|
|
|
|
auto ViewFamilyInit = FSceneViewFamily::ConstructionValues(
|
|
&GReflectionCaptureRenderTarget,
|
|
Scene,
|
|
FEngineShowFlags(ESFIM_Game)
|
|
)
|
|
.SetResolveScene(false);
|
|
|
|
if( bStaticSceneOnly )
|
|
{
|
|
ViewFamilyInit.SetWorldTimes( 0.0f, 0.0f, 0.0f );
|
|
}
|
|
|
|
FSceneViewFamilyContext ViewFamily( ViewFamilyInit );
|
|
|
|
// Disable features that are not desired when capturing the scene
|
|
ViewFamily.EngineShowFlags.PostProcessing = 0;
|
|
ViewFamily.EngineShowFlags.MotionBlur = 0;
|
|
ViewFamily.EngineShowFlags.SetOnScreenDebug(false);
|
|
ViewFamily.EngineShowFlags.HMDDistortion = 0;
|
|
// Exclude particles and light functions as they are usually dynamic, and can't be captured well
|
|
ViewFamily.EngineShowFlags.Particles = 0;
|
|
ViewFamily.EngineShowFlags.LightFunctions = 0;
|
|
ViewFamily.EngineShowFlags.SetCompositeEditorPrimitives(false);
|
|
// These are highly dynamic and can't be captured effectively
|
|
ViewFamily.EngineShowFlags.LightShafts = 0;
|
|
// Don't apply sky lighting diffuse when capturing the sky light source, or we would have feedback
|
|
ViewFamily.EngineShowFlags.SkyLighting = !bCapturingForSkyLight;
|
|
// Skip lighting for emissive only
|
|
ViewFamily.EngineShowFlags.Lighting = !bCaptureEmissiveOnly;
|
|
|
|
FSceneViewInitOptions ViewInitOptions;
|
|
ViewInitOptions.ViewFamily = &ViewFamily;
|
|
ViewInitOptions.BackgroundColor = FLinearColor::Black;
|
|
ViewInitOptions.OverlayColor = FLinearColor::Black;
|
|
ViewInitOptions.SetViewRectangle(FIntRect(0, 0, CubemapSize * GSupersampleCaptureFactor, CubemapSize * GSupersampleCaptureFactor));
|
|
|
|
const float NearPlane = bCapturingForSkyLight ? SkyLightNearPlane : GReflectionCaptureNearPlane;
|
|
|
|
// Projection matrix based on the fov, near / far clip settings
|
|
// Each face always uses a 90 degree field of view
|
|
if ((bool)ERHIZBuffer::IsInverted)
|
|
{
|
|
ViewInitOptions.ProjectionMatrix = FReversedZPerspectiveMatrix(
|
|
90.0f * (float)PI / 360.0f,
|
|
(float)CubemapSize * GSupersampleCaptureFactor,
|
|
(float)CubemapSize * GSupersampleCaptureFactor,
|
|
NearPlane
|
|
);
|
|
}
|
|
else
|
|
{
|
|
ViewInitOptions.ProjectionMatrix = FPerspectiveMatrix(
|
|
90.0f * (float)PI / 360.0f,
|
|
(float)CubemapSize * GSupersampleCaptureFactor,
|
|
(float)CubemapSize * GSupersampleCaptureFactor,
|
|
NearPlane
|
|
);
|
|
}
|
|
|
|
ViewInitOptions.ViewOrigin = CapturePosition;
|
|
ViewInitOptions.ViewRotationMatrix = CalcCubeFaceViewRotationMatrix((ECubeFace)CubeFace);
|
|
|
|
FSceneView* View = new FSceneView(ViewInitOptions);
|
|
|
|
// Force all surfaces diffuse
|
|
View->RoughnessOverrideParameter = FVector2D( 1.0f, 0.0f );
|
|
|
|
if (bCaptureEmissiveOnly)
|
|
{
|
|
View->DiffuseOverrideParameter = FVector4(0, 0, 0, 0);
|
|
View->SpecularOverrideParameter = FVector4(0, 0, 0, 0);
|
|
}
|
|
|
|
View->bIsReflectionCapture = true;
|
|
View->bStaticSceneOnly = bStaticSceneOnly;
|
|
View->StartFinalPostprocessSettings(CapturePosition);
|
|
View->EndFinalPostprocessSettings(ViewInitOptions);
|
|
|
|
ViewFamily.Views.Add(View);
|
|
|
|
FSceneRenderer* SceneRenderer = FSceneRenderer::CreateSceneRenderer(&ViewFamily, NULL);
|
|
|
|
ENQUEUE_UNIQUE_RENDER_COMMAND_SIXPARAMETER(
|
|
CaptureCommand,
|
|
FSceneRenderer*, SceneRenderer, SceneRenderer,
|
|
ECubeFace, CubeFace, (ECubeFace)CubeFace,
|
|
int32, CubemapSize, CubemapSize,
|
|
bool, bCapturingForSkyLight, bCapturingForSkyLight,
|
|
bool, bLowerHemisphereIsBlack, bLowerHemisphereIsBlack,
|
|
FLinearColor, LowerHemisphereColor, LowerHemisphereColor,
|
|
{
|
|
CaptureSceneToScratchCubemap(RHICmdList, SceneRenderer, CubeFace, CubemapSize, bCapturingForSkyLight, bLowerHemisphereIsBlack, LowerHemisphereColor);
|
|
|
|
if( !bCapturingForSkyLight )
|
|
{
|
|
RHICmdList.EndFrame();
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
void CopyToSceneArray(FRHICommandListImmediate& RHICmdList, FScene* Scene, FReflectionCaptureProxy* ReflectionProxy)
|
|
{
|
|
SCOPED_DRAW_EVENT(RHICmdList, CopyToSceneArray);
|
|
const int32 EffectiveTopMipSize = UReflectionCaptureComponent::GetReflectionCaptureSize_RenderThread();
|
|
const int32 NumMips = FMath::CeilLogTwo(EffectiveTopMipSize) + 1;
|
|
|
|
const int32 CaptureIndex = FindOrAllocateCubemapIndex(Scene, ReflectionProxy->Component);
|
|
FSceneRenderTargets& SceneContext = FSceneRenderTargets::Get(RHICmdList);
|
|
|
|
FSceneRenderTargetItem& FilteredCube = FSceneRenderTargets::Get(RHICmdList).ReflectionColorScratchCubemap[1]->GetRenderTargetItem();
|
|
FSceneRenderTargetItem& DestCube = Scene->ReflectionSceneData.CubemapArray.GetRenderTarget();
|
|
|
|
// GPU copy back to the scene's texture array, which is not a render target
|
|
for (int32 MipIndex = 0; MipIndex < NumMips; MipIndex++)
|
|
{
|
|
for (int32 CubeFace = 0; CubeFace < CubeFace_MAX; CubeFace++)
|
|
{
|
|
RHICmdList.CopyToResolveTarget(FilteredCube.ShaderResourceTexture, DestCube.ShaderResourceTexture, true, FResolveParams(FResolveRect(), (ECubeFace)CubeFace, MipIndex, 0, CaptureIndex));
|
|
}
|
|
}
|
|
}
|
|
|
|
void CopyToComponentTexture(FRHICommandList& RHICmdList, FScene* Scene, FReflectionCaptureProxy* ReflectionProxy)
|
|
{
|
|
SCOPED_DRAW_EVENT(RHICmdList, CopyToComponentTexture);
|
|
check(ReflectionProxy->SM4FullHDRCubemap);
|
|
|
|
const int32 EffectiveTopMipSize = UReflectionCaptureComponent::GetReflectionCaptureSize_RenderThread();
|
|
const int32 NumMips = FMath::CeilLogTwo(EffectiveTopMipSize) + 1;
|
|
FSceneRenderTargets& SceneContext = FSceneRenderTargets::Get(RHICmdList);
|
|
|
|
FSceneRenderTargetItem& FilteredCube = FSceneRenderTargets::Get(RHICmdList).ReflectionColorScratchCubemap[1]->GetRenderTargetItem();
|
|
|
|
// GPU copy back to the component's cubemap texture, which is not a render target
|
|
for (int32 MipIndex = 0; MipIndex < NumMips; MipIndex++)
|
|
{
|
|
for (int32 CubeFace = 0; CubeFace < CubeFace_MAX; CubeFace++)
|
|
{
|
|
RHICmdList.CopyToResolveTarget(FilteredCube.ShaderResourceTexture, ReflectionProxy->SM4FullHDRCubemap->TextureRHI, true, FResolveParams(FResolveRect(), (ECubeFace)CubeFace, MipIndex, 0, 0));
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Updates the contents of the given reflection capture by rendering the scene.
|
|
* This must be called on the game thread.
|
|
*/
|
|
void FScene::UpdateReflectionCaptureContents(UReflectionCaptureComponent* CaptureComponent)
|
|
{
|
|
const bool bCubemapSpecified = CaptureComponent->ReflectionSourceType == EReflectionSourceType::SpecifiedCubemap && CaptureComponent->Cubemap;
|
|
const int32 ReflectionCaptureSize = UReflectionCaptureComponent::GetReflectionCaptureSize_GameThread();
|
|
|
|
if (IsReflectionEnvironmentAvailable(GetFeatureLevel()) || bCubemapSpecified)
|
|
{
|
|
const FReflectionCaptureFullHDR* DerivedData = CaptureComponent->GetFullHDRData();
|
|
|
|
// Upload existing derived data if it exists, instead of capturing
|
|
if (DerivedData && DerivedData->HasValidData() )
|
|
{
|
|
// For other feature levels the reflection textures are stored on the component instead of in a scene-wide texture array
|
|
if (GetFeatureLevel() >= ERHIFeatureLevel::SM5)
|
|
{
|
|
ENQUEUE_UNIQUE_RENDER_COMMAND_THREEPARAMETER(
|
|
UploadCaptureCommand,
|
|
FScene*, Scene, this,
|
|
const FReflectionCaptureFullHDR*, DerivedData, DerivedData,
|
|
const UReflectionCaptureComponent*, CaptureComponent, CaptureComponent,
|
|
{
|
|
UploadReflectionCapture_RenderingThread(Scene, DerivedData, CaptureComponent);
|
|
});
|
|
|
|
if ( DoGPUArrayCopy() && !GIsEditor )
|
|
{
|
|
// We no longer need the HDR data, since we have a copy on the GPU
|
|
// In the editor we need this data for serialization, however
|
|
DerivedData = nullptr;
|
|
CaptureComponent->ReleaseHDRData();
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (CaptureComponent->ReflectionSourceType == EReflectionSourceType::SpecifiedCubemap && !CaptureComponent->Cubemap)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (FPlatformProperties::RequiresCookedData())
|
|
{
|
|
UE_LOG(LogEngine, Warning, TEXT("No built data for %s, skipping generation in cooked build."), *CaptureComponent->GetPathName() )
|
|
return;
|
|
}
|
|
|
|
ENQUEUE_UNIQUE_RENDER_COMMAND_ONEPARAMETER(
|
|
ClearCommand,
|
|
int32, ReflectionCaptureSize, ReflectionCaptureSize,
|
|
{
|
|
ClearScratchCubemaps(RHICmdList, ReflectionCaptureSize);
|
|
});
|
|
|
|
if (CaptureComponent->ReflectionSourceType == EReflectionSourceType::CapturedScene)
|
|
{
|
|
CaptureSceneIntoScratchCubemap(this, CaptureComponent->GetComponentLocation() + CaptureComponent->CaptureOffset, ReflectionCaptureSize, false, true, 0, false, false, FLinearColor());
|
|
}
|
|
else if (CaptureComponent->ReflectionSourceType == EReflectionSourceType::SpecifiedCubemap)
|
|
{
|
|
ENQUEUE_UNIQUE_RENDER_COMMAND_FOURPARAMETER(
|
|
CopyCubemapCommand,
|
|
UTextureCube*, SourceTexture, CaptureComponent->Cubemap,
|
|
int32, ReflectionCaptureSize, ReflectionCaptureSize,
|
|
float, SourceCubemapRotation, CaptureComponent->SourceCubemapAngle * (PI / 180.f),
|
|
ERHIFeatureLevel::Type, FeatureLevel, GetFeatureLevel(),
|
|
{
|
|
CopyCubemapToScratchCubemap(RHICmdList, FeatureLevel, SourceTexture, ReflectionCaptureSize, false, false, SourceCubemapRotation, FLinearColor());
|
|
});
|
|
}
|
|
else
|
|
{
|
|
check(!TEXT("Unknown reflection source type"));
|
|
}
|
|
|
|
ENQUEUE_UNIQUE_RENDER_COMMAND_THREEPARAMETER(
|
|
FilterCommand,
|
|
ERHIFeatureLevel::Type, FeatureLevel, GetFeatureLevel(),
|
|
int32, ReflectionCaptureSize, ReflectionCaptureSize,
|
|
float&, AverageBrightness, *CaptureComponent->GetAverageBrightnessPtr(),
|
|
{
|
|
ComputeAverageBrightness(RHICmdList, FeatureLevel, ReflectionCaptureSize, AverageBrightness);
|
|
FilterReflectionEnvironment(RHICmdList, FeatureLevel, ReflectionCaptureSize, NULL);
|
|
}
|
|
);
|
|
|
|
// Create a proxy to represent the reflection capture to the rendering thread
|
|
// The rendering thread will be responsible for deleting this when done with the filtering operation
|
|
// We can't use the component's SceneProxy here because the component may not be registered with the scene
|
|
FReflectionCaptureProxy* ReflectionProxy = new FReflectionCaptureProxy(CaptureComponent);
|
|
|
|
ENQUEUE_UNIQUE_RENDER_COMMAND_THREEPARAMETER(
|
|
CopyCommand,
|
|
FScene*, Scene, this,
|
|
FReflectionCaptureProxy*, ReflectionProxy, ReflectionProxy,
|
|
ERHIFeatureLevel::Type, FeatureLevel, GetFeatureLevel(),
|
|
{
|
|
if (FeatureLevel == ERHIFeatureLevel::SM5)
|
|
{
|
|
CopyToSceneArray(RHICmdList, Scene, ReflectionProxy);
|
|
}
|
|
else if (FeatureLevel == ERHIFeatureLevel::SM4)
|
|
{
|
|
CopyToComponentTexture(RHICmdList, Scene, ReflectionProxy);
|
|
}
|
|
|
|
// Clean up the proxy now that the rendering thread is done with it
|
|
delete ReflectionProxy;
|
|
});
|
|
} //-V773
|
|
}
|
|
}
|
|
|
|
void ReadbackRadianceMap(FRHICommandListImmediate& RHICmdList, int32 CubmapSize, TArray<FFloat16Color>& OutRadianceMap)
|
|
{
|
|
OutRadianceMap.Empty(CubmapSize * CubmapSize * 6);
|
|
OutRadianceMap.AddZeroed(CubmapSize * CubmapSize * 6);
|
|
|
|
const int32 MipIndex = 0;
|
|
|
|
FSceneRenderTargetItem& SourceCube = FSceneRenderTargets::Get(RHICmdList).ReflectionColorScratchCubemap[0]->GetRenderTargetItem();
|
|
check(SourceCube.ShaderResourceTexture->GetFormat() == PF_FloatRGBA);
|
|
const int32 CubeFaceBytes = CubmapSize * CubmapSize * OutRadianceMap.GetTypeSize();
|
|
|
|
for (int32 CubeFace = 0; CubeFace < CubeFace_MAX; CubeFace++)
|
|
{
|
|
TArray<FFloat16Color> SurfaceData;
|
|
|
|
// Read each mip face
|
|
RHICmdList.ReadSurfaceFloatData(SourceCube.ShaderResourceTexture, FIntRect(0, 0, CubmapSize, CubmapSize), SurfaceData, (ECubeFace)CubeFace, 0, MipIndex);
|
|
const int32 DestIndex = CubeFace * CubmapSize * CubmapSize;
|
|
FFloat16Color* FaceData = &OutRadianceMap[DestIndex];
|
|
check(SurfaceData.Num() * SurfaceData.GetTypeSize() == CubeFaceBytes);
|
|
FMemory::Memcpy(FaceData, SurfaceData.GetData(), CubeFaceBytes);
|
|
}
|
|
}
|
|
|
|
void CopyToSkyTexture(FRHICommandList& RHICmdList, FScene* Scene, FTexture* ProcessedTexture)
|
|
{
|
|
SCOPED_DRAW_EVENT(RHICmdList, CopyToSkyTexture);
|
|
if (ProcessedTexture->TextureRHI)
|
|
{
|
|
const int32 EffectiveTopMipSize = ProcessedTexture->GetSizeX();
|
|
const int32 NumMips = FMath::CeilLogTwo(EffectiveTopMipSize) + 1;
|
|
FSceneRenderTargets& SceneContext = FSceneRenderTargets::Get(RHICmdList);
|
|
|
|
FSceneRenderTargetItem& FilteredCube = FSceneRenderTargets::Get(RHICmdList).ReflectionColorScratchCubemap[1]->GetRenderTargetItem();
|
|
|
|
// GPU copy back to the skylight's texture, which is not a render target
|
|
for (int32 MipIndex = 0; MipIndex < NumMips; MipIndex++)
|
|
{
|
|
for (int32 CubeFace = 0; CubeFace < CubeFace_MAX; CubeFace++)
|
|
{
|
|
RHICmdList.CopyToResolveTarget(FilteredCube.ShaderResourceTexture, ProcessedTexture->TextureRHI, true, FResolveParams(FResolveRect(), (ECubeFace)CubeFace, MipIndex, 0, 0));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Warning: returns before writes to OutIrradianceEnvironmentMap have completed, as they are queued on the rendering thread
|
|
void FScene::UpdateSkyCaptureContents(
|
|
const USkyLightComponent* CaptureComponent,
|
|
bool bCaptureEmissiveOnly,
|
|
UTextureCube* SourceCubemap,
|
|
FTexture* OutProcessedTexture,
|
|
float& OutAverageBrightness,
|
|
FSHVectorRGB3& OutIrradianceEnvironmentMap,
|
|
TArray<FFloat16Color>* OutRadianceMap)
|
|
{
|
|
if (GSupportsRenderTargetFormat_PF_FloatRGBA || GetFeatureLevel() >= ERHIFeatureLevel::SM4)
|
|
{
|
|
QUICK_SCOPE_CYCLE_COUNTER(STAT_UpdateSkyCaptureContents);
|
|
{
|
|
World = GetWorld();
|
|
if (World)
|
|
{
|
|
//guarantee that all render proxies are up to date before kicking off this render
|
|
World->SendAllEndOfFrameUpdates();
|
|
}
|
|
}
|
|
ENQUEUE_UNIQUE_RENDER_COMMAND_ONEPARAMETER(
|
|
ClearCommand,
|
|
int32, CubemapSize, CaptureComponent->CubemapResolution,
|
|
{
|
|
ClearScratchCubemaps(RHICmdList, CubemapSize);
|
|
});
|
|
|
|
if (CaptureComponent->SourceType == SLS_CapturedScene)
|
|
{
|
|
bool bStaticSceneOnly = CaptureComponent->Mobility == EComponentMobility::Static;
|
|
CaptureSceneIntoScratchCubemap(this, CaptureComponent->GetComponentLocation(), CaptureComponent->CubemapResolution, true, bStaticSceneOnly, CaptureComponent->SkyDistanceThreshold, CaptureComponent->bLowerHemisphereIsBlack, bCaptureEmissiveOnly, CaptureComponent->LowerHemisphereColor);
|
|
}
|
|
else if (CaptureComponent->SourceType == SLS_SpecifiedCubemap)
|
|
{
|
|
ENQUEUE_UNIQUE_RENDER_COMMAND_SIXPARAMETER(
|
|
CopyCubemapCommand,
|
|
UTextureCube*, SourceTexture, SourceCubemap,
|
|
int32, CubemapSize, CaptureComponent->CubemapResolution,
|
|
bool, bLowerHemisphereIsBlack, CaptureComponent->bLowerHemisphereIsBlack,
|
|
float, SourceCubemapRotation, CaptureComponent->SourceCubemapAngle * (PI / 180.f),
|
|
ERHIFeatureLevel::Type, FeatureLevel, GetFeatureLevel(),
|
|
FLinearColor, LowerHemisphereColor, CaptureComponent->LowerHemisphereColor,
|
|
{
|
|
CopyCubemapToScratchCubemap(RHICmdList, FeatureLevel, SourceTexture, CubemapSize, true, bLowerHemisphereIsBlack, SourceCubemapRotation, LowerHemisphereColor);
|
|
});
|
|
}
|
|
else
|
|
{
|
|
check(0);
|
|
}
|
|
|
|
if (OutRadianceMap)
|
|
{
|
|
ENQUEUE_UNIQUE_RENDER_COMMAND_TWOPARAMETER(
|
|
ReadbackCommand,
|
|
int32, CubemapSize, CaptureComponent->CubemapResolution,
|
|
TArray<FFloat16Color>*, RadianceMap, OutRadianceMap,
|
|
{
|
|
ReadbackRadianceMap(RHICmdList, CubemapSize, *RadianceMap);
|
|
});
|
|
}
|
|
|
|
ENQUEUE_UNIQUE_RENDER_COMMAND_FOURPARAMETER(
|
|
FilterCommand,
|
|
int32, CubemapSize, CaptureComponent->CubemapResolution,
|
|
float&, AverageBrightness, OutAverageBrightness,
|
|
FSHVectorRGB3*, IrradianceEnvironmentMap, &OutIrradianceEnvironmentMap,
|
|
ERHIFeatureLevel::Type, FeatureLevel, GetFeatureLevel(),
|
|
{
|
|
if (FeatureLevel <= ERHIFeatureLevel::ES3_1)
|
|
{
|
|
MobileReflectionEnvironmentCapture::ComputeAverageBrightness(RHICmdList, FeatureLevel, CubemapSize, AverageBrightness);
|
|
MobileReflectionEnvironmentCapture::FilterReflectionEnvironment(RHICmdList, FeatureLevel, CubemapSize, IrradianceEnvironmentMap);
|
|
}
|
|
else
|
|
{
|
|
ComputeAverageBrightness(RHICmdList, FeatureLevel, CubemapSize, AverageBrightness);
|
|
FilterReflectionEnvironment(RHICmdList, FeatureLevel, CubemapSize, IrradianceEnvironmentMap);
|
|
}
|
|
});
|
|
|
|
// Optionally copy the filtered mip chain to the output texture
|
|
if (OutProcessedTexture)
|
|
{
|
|
ENQUEUE_UNIQUE_RENDER_COMMAND_THREEPARAMETER(
|
|
CopyCommand,
|
|
FScene*, Scene, this,
|
|
FTexture*, ProcessedTexture, OutProcessedTexture,
|
|
ERHIFeatureLevel::Type, FeatureLevel, GetFeatureLevel(),
|
|
{
|
|
if (FeatureLevel <= ERHIFeatureLevel::ES3_1)
|
|
{
|
|
MobileReflectionEnvironmentCapture::CopyToSkyTexture(RHICmdList, Scene, ProcessedTexture);
|
|
}
|
|
else
|
|
{
|
|
CopyToSkyTexture(RHICmdList, Scene, ProcessedTexture);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
}
|