Files
UnrealEngineUWP/Engine/Shaders/TranslucentLightingShaders.usf

189 lines
7.6 KiB
Plaintext
Raw Normal View History

// Copyright 1998-2017 Epic Games, Inc. All Rights Reserved.
/**
* TranslucentLightingShaders.usf: Shaders for calculating lighting in a volume to use on translucency
*/
#include "Common.usf"
#include "SHCommon.usf"
#if INJECTION_PIXEL_SHADER
#include "ShadowProjectionCommon.usf"
#endif
struct FWriteToSliceVertexOutput
{
FScreenVertexOutput Vertex;
#if USING_VERTEX_SHADER_LAYER
uint LayerIndex : SV_RenderTargetArrayIndex;
#else
uint LayerIndex : TEXCOORD1;
#endif
};
/** Z index of the minimum slice in the range. */
int MinZ;
float4 UVScaleBias;
uint VolumeCascadeIndex;
/** Vertex shader that writes to a range of slices of a volume texture. */
void WriteToSliceMainVS(
float2 InPosition : ATTRIBUTE0,
float2 InUV : ATTRIBUTE1,
uint LayerIndex : SV_InstanceID,
out FWriteToSliceVertexOutput Output
)
{
Output.Vertex.Position = float4( InPosition, 0, 1 );
// Remap UVs based on the subregion of the volume texture being rendered to
Output.Vertex.UV = InUV * UVScaleBias.xy + UVScaleBias.zw;
Output.LayerIndex = LayerIndex + MinZ;
}
void CopySceneAlphaMain(
FScreenVertexOutput Input,
out float OutAlpha : SV_Target0
)
{
OutAlpha = Texture2DSample(SceneColorTexture, SceneColorTextureSampler, Input.UV).a;
}
void CopySceneColorMain(
FScreenVertexOutput Input,
out float4 OutColor : SV_Target0
)
{
float4 LinearColor = Texture2DSample(SceneColorTexture, SceneColorTextureSampler, Input.UV);
OutColor = float4(EncodeSceneColorForMaterialNode(LinearColor.rgb), 0);
}
#if FEATURE_LEVEL >= FEATURE_LEVEL_SM4
/** Geometry shader that writes to a range of slices of a volume texture. */
[maxvertexcount(3)]
void WriteToSliceMainGS(triangle FWriteToSliceVertexOutput Input[3], inout TriangleStream<FWriteToSliceGeometryOutput> OutStream)
{
FWriteToSliceGeometryOutput Vertex0;
Vertex0.Vertex = Input[0].Vertex;
Vertex0.LayerIndex = Input[0].LayerIndex;
FWriteToSliceGeometryOutput Vertex1;
Vertex1.Vertex = Input[1].Vertex;
Vertex1.LayerIndex = Input[1].LayerIndex;
FWriteToSliceGeometryOutput Vertex2;
Vertex2.Vertex = Input[2].Vertex;
Vertex2.LayerIndex = Input[2].LayerIndex;
OutStream.Append(Vertex0);
OutStream.Append(Vertex1);
OutStream.Append(Vertex2);
}
/** Filter pass inputs. */
Texture3D TranslucencyLightingVolumeAmbient;
SamplerState TranslucencyLightingVolumeAmbientSampler;
Texture3D TranslucencyLightingVolumeDirectional;
SamplerState TranslucencyLightingVolumeDirectionalSampler;
float TexelSize;
/** Filters the volume lighting to reduce aliasing. */
void FilterMainPS(
FWriteToSliceGeometryOutput Input,
out float4 OutColor0 : SV_Target0,
out float4 OutColor1 : SV_Target1
)
{
float4 TextureValue0 = 0;
float4 TextureValue1 = 0;
float3 VolumeUV = float3(Input.Vertex.UV, (Input.LayerIndex + .5f) * TexelSize);
#define USE_FILTER 1
#if USE_FILTER
// Use trilinear filtering to filter neighbors to the current voxel with minimal texture fetches
TextureValue0 += Texture3DSample(TranslucencyLightingVolumeAmbient, TranslucencyLightingVolumeAmbientSampler, VolumeUV + .5f * TexelSize * float3(1, 1, 1));
TextureValue0 += Texture3DSample(TranslucencyLightingVolumeAmbient, TranslucencyLightingVolumeAmbientSampler, VolumeUV + .5f * TexelSize * float3(-1, 1, 1));
TextureValue0 += Texture3DSample(TranslucencyLightingVolumeAmbient, TranslucencyLightingVolumeAmbientSampler, VolumeUV + .5f * TexelSize * float3(1, -1, 1));
TextureValue0 += Texture3DSample(TranslucencyLightingVolumeAmbient, TranslucencyLightingVolumeAmbientSampler, VolumeUV + .5f * TexelSize * float3(-1, -1, 1));
TextureValue0 += Texture3DSample(TranslucencyLightingVolumeAmbient, TranslucencyLightingVolumeAmbientSampler, VolumeUV + .5f * TexelSize * float3(1, 1, -1));
TextureValue0 += Texture3DSample(TranslucencyLightingVolumeAmbient, TranslucencyLightingVolumeAmbientSampler, VolumeUV + .5f * TexelSize * float3(-1, 1, -1));
TextureValue0 += Texture3DSample(TranslucencyLightingVolumeAmbient, TranslucencyLightingVolumeAmbientSampler, VolumeUV + .5f * TexelSize * float3(1, -1, -1));
TextureValue0 += Texture3DSample(TranslucencyLightingVolumeAmbient, TranslucencyLightingVolumeAmbientSampler, VolumeUV + .5f * TexelSize * float3(-1, -1, -1));
TextureValue1 += Texture3DSample(TranslucencyLightingVolumeDirectional, TranslucencyLightingVolumeDirectionalSampler, VolumeUV + .5f * TexelSize * float3(1, 1, 1));
TextureValue1 += Texture3DSample(TranslucencyLightingVolumeDirectional, TranslucencyLightingVolumeDirectionalSampler, VolumeUV + .5f * TexelSize * float3(-1, 1, 1));
TextureValue1 += Texture3DSample(TranslucencyLightingVolumeDirectional, TranslucencyLightingVolumeDirectionalSampler, VolumeUV + .5f * TexelSize * float3(1, -1, 1));
TextureValue1 += Texture3DSample(TranslucencyLightingVolumeDirectional, TranslucencyLightingVolumeDirectionalSampler, VolumeUV + .5f * TexelSize * float3(-1, -1, 1));
TextureValue1 += Texture3DSample(TranslucencyLightingVolumeDirectional, TranslucencyLightingVolumeDirectionalSampler, VolumeUV + .5f * TexelSize * float3(1, 1, -1));
TextureValue1 += Texture3DSample(TranslucencyLightingVolumeDirectional, TranslucencyLightingVolumeDirectionalSampler, VolumeUV + .5f * TexelSize * float3(-1, 1, -1));
TextureValue1 += Texture3DSample(TranslucencyLightingVolumeDirectional, TranslucencyLightingVolumeDirectionalSampler, VolumeUV + .5f * TexelSize * float3(1, -1, -1));
TextureValue1 += Texture3DSample(TranslucencyLightingVolumeDirectional, TranslucencyLightingVolumeDirectionalSampler, VolumeUV + .5f * TexelSize * float3(-1, -1, -1));
float InvWeight = 1.0f / 8;
#else
TextureValue0 = Texture3DSample(TranslucencyLightingVolumeAmbient, TranslucencyLightingVolumeAmbientSampler, VolumeUV);
TextureValue1 = Texture3DSample(TranslucencyLightingVolumeDirectional, TranslucencyLightingVolumeDirectionalSampler, VolumeUV);
float InvWeight = 1;
#endif
OutColor0 = TextureValue0 * InvWeight;
OutColor1 = TextureValue1 * InvWeight;
}
#if INJECTION_PIXEL_SHADER
/** Parameters needed to access the shadow map of the light. */
float4x4 WorldToShadowMatrix;
float4 ShadowmapMinMax;
/** Pixel shader that calculates per object shadowing from translucency for a volume texture and outputs the shadow factor. */
void PerObjectShadowingMainPS(
FWriteToSliceGeometryOutput Input,
out float4 OutColor : SV_Target0
)
{
Copying //UE4/Dev-VR to //UE4/Dev-Main (Source: //UE4/Dev-VR @ 3016398) #lockdown nick.penwarden #rb nobody ========================== MAJOR FEATURES + CHANGES ========================== Change 2945508 on 2016/04/15 by Nick.Whiting Integrating fix for module loading for SteamVR, prevents crashing in race conditions Change 2950385 on 2016/04/20 by Ryan.Vance We need to test if the hmd is enabled if it exists. Otherwise, this will return true even if we aren't rendering in stereo if there's an hmd plugin loaded. Change 2955406 on 2016/04/25 by Chad.Taylor Factor scale into the motion controller component late update Change 2956275 on 2016/04/26 by Nick.Whiting Initial integration of OSVR plugin support #pr 2097 Change 2964412 on 2016/05/03 by Chad.Taylor PSVR's GetControllerOrientationAndPosition now returns false if status is NOT_STARTED or CALIBRATING Change 2964612 on 2016/05/03 by Ryan.Vance Copying //UE4/Dev-VR-InstancedStereo to Dev-VR-Minimal (//UE4/Dev-VR-Minimal) Change 2985528 on 2016/05/20 by Ryan.Vance #jira UE-30715 Keep from spamming the output log for every single shader compile when instanced stereo is enabled for a shader platform that doesn't support it. Change 2986246 on 2016/05/22 by Chad.Taylor HMD late-update thread safety Change 2998629 on 2016/06/02 by Ryan.Vance Post 4.12 Oculus plugin integration Change 3000057 on 2016/06/03 by Ryan.Vance Updating serialize function for custom material nodes. The instanced stereo refactor moved Frame uniforms *back* to View (post 4.11). This should update only objects that went through the prior transformation from View *to* Frame. The serialize function was also being used to update Parameters.WorldPosition to Parameters.AbsoluteWorldPosition. This should still run as expected. Change 3002187 on 2016/06/06 by Ryan.Vance Switching from ._m syntax to array syntax. The cross compiler chokes on the former. Change 3004153 on 2016/06/07 by Chad.Taylor Enable PSVR on VS2015 #jira UE-31202 Change 3009958 on 2016/06/10 by Ryan.Vance #jira UE-31922 Velocity and depth pre-pass for dynamic instanced meshes with isr was only rendering in the left eye. Need to loop over the draw call the same way we do for the base pass. Change 3011054 on 2016/06/13 by Chad.Taylor Merging "SteamVR Positional Late-Update" back into Dev-VR Change 3013361 on 2016/06/14 by Ryan.Vance #jira UE-32022 Fixing dbuffer decal integration errors. [CL 3018810 by Ryan Vance in Main branch]
2016-06-17 20:25:37 -04:00
float ZPosition = View.TranslucencyLightingVolumeMin[VolumeCascadeIndex].z + (Input.LayerIndex + .5f) * View.TranslucencyLightingVolumeInvSize[VolumeCascadeIndex].w;
float3 WorldPosition = float3(View.TranslucencyLightingVolumeMin[VolumeCascadeIndex].xy + Input.Vertex.UV / View.TranslucencyLightingVolumeInvSize[VolumeCascadeIndex].xy - .5f * View.TranslucencyLightingVolumeInvSize[VolumeCascadeIndex].w, ZPosition);
float VoxelSize = View.TranslucencyLightingVolumeInvSize[VolumeCascadeIndex].w;
float ShadowFactor = 1;
float3 WorldPositionForShadowing = WorldPosition;
// Transform the world position into shadowmap space
float4 HomogeneousShadowPosition = mul(float4(WorldPositionForShadowing, 1), WorldToShadowMatrix);
float2 ShadowUVs = HomogeneousShadowPosition.xy / HomogeneousShadowPosition.w;
// Treat as unshadowed if the voxel is outside of the shadow map
if (all(ShadowUVs >= ShadowmapMinMax.xy && ShadowUVs <= ShadowmapMinMax.zw))
{
ShadowFactor = CalculateTranslucencyShadowing(ShadowUVs, HomogeneousShadowPosition.z);
}
OutColor = float4(ShadowFactor.xxx, 0);
}
#endif // #if INJECTION_PIXEL_SHADER
#include "CubemapCommon.usf"
/** Add AmbientCubemap color to the volume. */
void InjectAmbientCubemapMainPS(
FWriteToSliceGeometryOutput Input,
out float4 OutColor : SV_Target0
)
{
// can be optimized by moving it into the vertex/geometry shader
OutColor = float4(ComputeAmbientCubemapAvgColor(), 0);
}
#endif // FEATURE_LEVEL