Files
UnrealEngineUWP/Engine/Shaders/Private/VisualizeVolumetricLightmap.usf
Ben Ingram 14ea9b00e5 Merging Dev-LWCRendering into Main, this includes initial work to support rendering with LWC-scale position
Basic approach is to add HLSL types FLWCScalar, FLWCMatrix, FLWCVector, etc.  Inside shaders, absolute world space position values should be represented as FLWCVector3.  Matrices that transform *into* absolute world space become FLWCMatrix.  Matrices that transform *from* world space become FLWCInverseMatrix.  Generally LWC values work by extending the regular 'float' value with an additional tile coordinate.  Final tile size will be a trade-off between scale/accuracy; I'm using 256k for now, but may need to be adjusted.  Value represented by a FLWCVector thus becomes V.Tile * TileSize + V.Offset.  Most operations can be performed directly on LWC values.  There are HLSL functions like LWCAdd, LWCSub, LWCMultiply, LWCDivide (operator overloading would be really nice here).  The goal is to stay with LWC values for as long as needed, then convert to regular float values when possible.  One thing that comes up a lot is working in translated (rather than absolute) world space.  WorldSpace + View.PrevPreViewTranslation = TranslatedWorldspace.  Except 'View.PrevPreViewTranslation' is now a FLWCVector3, and WorldSpace quantities should be as well.  So that becomes LWCAdd(WorldSpace, View.PrevPreViewTranslation) = TranslatedWorldspace.  Assuming that we're talking about a position that's "reasonably close" to the camera, it should be safe to convert the translated WS value to float.  The 'tile' coordinate of the 2 LWC values should cancel out when added together in this case.  I've done some work throughout the shader code to do this.  Materials are fully supporting LWC-values as well.  Projective texturing and vertex animation materials that I've tested work correctly even when positioned "far away" from the origin.

Lots of work remains to fully convert all of our shader code.  There's a function LWCHackToFloat(), which is a simple wrapper for LWCToFloat().  The idea of HackToFloat is to mark places that need further attention, where I'm simply converting absolute WS positions to float, to get shaders to compile.  Shaders converted in this way should continue to work for all existing content (without LWC-scale values), but they will break if positions get too large.

General overview of changed files:
LargeWorldCoordinates.ush - This defines the FLWC types and operations
GPUScene.cpp, SceneData.ush - Primitives add an extra 'float3' tile coordinate.  Instance data is unchanged, so instances need to stay within single-precision range of the primitive origin.  Could potentially split instances behind the scenes (I think) if we don't want this limitation
HLSLMaterialDerivativeAutogen.cpp, HLSLMaterialTranslator.cpp, Preshader.cpp - Translated materials to use LWC values
SceneView.cpp, SceneRelativeViewMatrices.cpp, ShaderCompiler.cpp, InstancedStereo.ush - View uniform buffer includes LWC values where appropriate
#jira UE-117101
#rb arne.schober, Michael.Galetzka

[CL 17787435 by Ben Ingram in ue5-main branch]
2021-10-12 13:29:45 -04:00

119 lines
4.9 KiB
Plaintext

// Copyright Epic Games, Inc. All Rights Reserved.
#include "Common.ush"
#include "SHCommon.ush"
#include "VolumetricLightmapShared.ush"
#include "DeferredShadingCommon.ush"
#ifndef QUADS_PER_INSTANCE
#define QUADS_PER_INSTANCE 0
#endif
struct FVisualizeVolumetricLightmapVSToPS
{
nointerpolation float3 BrickUVs : TEXCOORD0;
nointerpolation float FadeToAmbient : TEXCOORD1;
nointerpolation float4 CellSphere : TEXCOORD2;
float3 VertexPosition : TEXCOORD3;
};
float VisualizationRadiusScale;
float VisualizationMinScreenFraction;
void VisualizeVolumetricLightmapVS(
uint VertexId : SV_VertexID,
uint InstanceId : SV_InstanceID,
out FVisualizeVolumetricLightmapVSToPS Output,
out float4 OutPosition : SV_POSITION
)
{
uint EffectiveInstanceId = InstanceId * QUADS_PER_INSTANCE + VertexId / 4;
uint3 NumHighestDensityCells = View.VolumetricLightmapIndirectionTextureSize * View.VolumetricLightmapBrickSize;
uint3 CellVector = uint3(
EffectiveInstanceId % NumHighestDensityCells.x,
(EffectiveInstanceId / NumHighestDensityCells.x) % NumHighestDensityCells.y,
EffectiveInstanceId / (NumHighestDensityCells.x * NumHighestDensityCells.y));
float3 IndirectionTextureTexelCoordinate = CellVector / View.VolumetricLightmapBrickSize;
float4 BrickOffsetAndSize = View.VolumetricLightmapIndirectionTexture.Load(int4(IndirectionTextureTexelCoordinate, 0));
float PaddedBrickSize = View.VolumetricLightmapBrickSize + 1;
float3 LocalCellPosition = frac(IndirectionTextureTexelCoordinate / BrickOffsetAndSize.w) * View.VolumetricLightmapBrickSize;
Output.BrickUVs = (BrickOffsetAndSize.xyz * PaddedBrickSize + LocalCellPosition + .5f) * View.VolumetricLightmapBrickTexelSize;
float3 GridLocalCellPosition = round(LocalCellPosition);
bool bValidCell = all(abs(LocalCellPosition - GridLocalCellPosition) < .01f);
Output.CellSphere.xyz = (CellVector / (float3)NumHighestDensityCells - View.VolumetricLightmapWorldToUVAdd) / View.VolumetricLightmapWorldToUVScale;
float DetailVoxelWorldSize = 1.0f / ((float)View.VolumetricLightmapIndirectionTextureSize.x * View.VolumetricLightmapWorldToUVScale.x);
// Set visualization sphere radius as a fraction of the corresponding brick's world space size
Output.CellSphere.w = BrickOffsetAndSize.w * DetailVoxelWorldSize * VisualizationRadiusScale;
{
float DistanceToCamera = length(Output.CellSphere.xyz - LWCHackToFloat(PrimaryView.WorldViewOrigin));
float SizeOnScreen = Output.CellSphere.w * View.ViewToClip[0][0] / DistanceToCamera;
Output.FadeToAmbient = saturate(SizeOnScreen * 100 - .01f);
// Maintain a minimum size on screen to reduce aliasing
SizeOnScreen = max(SizeOnScreen, VisualizationMinScreenFraction * sqrt(BrickOffsetAndSize.w));
Output.CellSphere.w = SizeOnScreen * DistanceToCamera / View.ViewToClip[0][0];
}
float2 TexCoord = float2(
((VertexId >> 0) & 1) ? 1.0f : -1.0f,
((VertexId >> 1) & 1) ? 1.0f : -1.0f);
float2 RescaledTexCoord = TexCoord * Output.CellSphere.w * sqrt(2.0);
Output.VertexPosition = Output.CellSphere.xyz + RescaledTexCoord.x * View.ViewRight + RescaledTexCoord.y * View.ViewUp;
OutPosition = mul(float4(Output.VertexPosition, 1), LWCHackToFloat(PrimaryView.WorldToClip));
if (!bValidCell)
{
// This cell is not resident, emit a degenerate triangle
OutPosition = 0;
}
}
#define WRITE_TO_SHADING_MODEL (FEATURE_LEVEL >= FEATURE_LEVEL_SM4 && !FORWARD_SHADING)
float3 DiffuseColor;
void VisualizeVolumetricLightmapPS(
FVisualizeVolumetricLightmapVSToPS Input,
out float4 OutColor : SV_Target0
#if WRITE_TO_SHADING_MODEL
,out float4 OutGBufferB : SV_Target1
#endif
)
{
float3 RayDirection = Input.VertexPosition - LWCHackToFloat(PrimaryView.WorldViewOrigin);
float2 SphereIntersections = RayIntersectSphere(LWCHackToFloat(PrimaryView.WorldViewOrigin), RayDirection, Input.CellSphere);
float3 IntersectionPosition = LWCHackToFloat(PrimaryView.WorldViewOrigin) + SphereIntersections.x * RayDirection;
clip(SphereIntersections.x);
FThreeBandSHVectorRGB IrradianceSH = GetVolumetricLightmapSH3(Input.BrickUVs);
float3 WorldNormal = normalize(IntersectionPosition - Input.CellSphere.xyz);
FThreeBandSHVector DiffuseTransferSH = CalcDiffuseTransferSH3(WorldNormal, 1);
float3 SphereLighting = max(float3(0,0,0), DotSH3(IrradianceSH, DiffuseTransferSH)) / PI;
float3 AmbientLighting = float3(IrradianceSH.R.V0.x, IrradianceSH.G.V0.x, IrradianceSH.B.V0.x) / PI;
// Lerp to ambient lighting in the distance to reduce aliasing
float3 FinalLighting = lerp(AmbientLighting, SphereLighting, Input.FadeToAmbient);
FinalLighting *= View.PreExposure;
// Use a diffuse color that matches the rest of the scene for LightingOnly and DetailLighting view modes
float3 EffectiveDiffuseColor = DiffuseColor * View.DiffuseOverrideParameter.w + View.DiffuseOverrideParameter.xyz;
OutColor = float4(EffectiveDiffuseColor * FinalLighting, 0);
//OutColor = float4(WorldNormal, 0);
#if WRITE_TO_SHADING_MODEL
// Shading model unlit
SetGBufferForUnlit(OutGBufferB);
#endif
}