顶点着色器:
void Main(
FVertexFactoryInput Input,
out FBasePassVSToPS Output
)
{
const uint EyeIndex = 0;
ResolvedView = (ResolveView()) ;
FVertexFactoryIntermediates VFIntermediates = GetVertexFactoryIntermediates(Input);
float4 WorldPositionExcludingWPO = VertexFactoryGetWorldPosition(Input, VFIntermediates);
float4 WorldPosition = WorldPositionExcludingWPO;
float4 ClipSpacePosition;
float3x3 TangentToLocal = VertexFactoryGetTangentToLocal(Input, VFIntermediates);
FMaterialVertexParameters VertexParameters = GetMaterialVertexParameters(Input, VFIntermediates, WorldPosition.xyz, TangentToLocal);
{
WorldPosition.xyz += GetMaterialWorldPositionOffset(VertexParameters);
}
{
float4 RasterizedWorldPosition = VertexFactoryGetRasterizedWorldPosition(Input, VFIntermediates, WorldPosition);
ClipSpacePosition = mul(RasterizedWorldPosition, ResolvedView.TranslatedWorldToClip);
Output.Position = MakePrecise(ClipSpacePosition) ;
}
Output.FactoryInterpolants = VertexFactoryGetInterpolantsVSToPS (Input, VFIntermediates, VertexParameters);
{
float4 PrevTranslatedWorldPosition = float4(0, 0, 0, 1);
[flatten]
if ((GetPrimitiveData(VertexParameters).Flags & 0x20 ) != 0)
{
PrevTranslatedWorldPosition = VertexFactoryGetPreviousWorldPosition( Input, VFIntermediates );
VertexParameters = GetMaterialVertexParameters(Input, VFIntermediates, PrevTranslatedWorldPosition.xyz, TangentToLocal);
PrevTranslatedWorldPosition.xyz += GetMaterialPreviousWorldPositionOffset(VertexParameters);
PrevTranslatedWorldPosition = mul(float4(PrevTranslatedWorldPosition.xyz, 1), ResolvedView.PrevTranslatedWorldToClip);
}
Output.BasePassInterpolants.VelocityPrevScreenPosition = MakePrecise(PrevTranslatedWorldPosition) ;
}
}
片元着色器:
PiexelShaderOutputCommon.ush
void MainPS
(
FVertexFactoryInterpolantsVSToPS Interpolants,
FSharedBasePassInterpolants BasePassInterpolants,
in float4 SvPosition : SV_Position
, in bool bIsFrontFace : SV_IsFrontFace
, out float4 OutTarget0 : SV_Target0
, out float4 OutTarget1 : SV_Target1
, out float4 OutTarget2 : SV_Target2
, out float4 OutTarget3 : SV_Target3
, out float4 OutTarget4 : SV_Target4
, out float4 OutTarget6 : SV_Target6
)
{
FPixelShaderIn PixelShaderIn = (FPixelShaderIn)0;
FPixelShaderOut PixelShaderOut = (FPixelShaderOut)0;
PixelShaderIn.SvPosition = SvPosition;
PixelShaderIn.bIsFrontFace = bIsFrontFace;
FPixelShaderInOut_MainPS(Interpolants, BasePassInterpolants, PixelShaderIn, PixelShaderOut);
OutTarget0 = PixelShaderOut.MRT[0];
OutTarget1 = PixelShaderOut.MRT[1];
OutTarget2 = PixelShaderOut.MRT[2];
OutTarget3 = PixelShaderOut.MRT[3];
OutTarget4 = PixelShaderOut.MRT[4];
OutTarget6 = PixelShaderOut.MRT[6];
}
BasePassPixelShader.usf
void FPixelShaderInOut_MainPS(
FVertexFactoryInterpolantsVSToPS Interpolants,
FSharedBasePassInterpolants BasePassInterpolants,
in FPixelShaderIn In,
inout FPixelShaderOut Out)
{
const uint EyeIndex = 0;
ResolvedView = ResolveView();
float4 OutVelocity = 0;
float4 OutGBufferD = 0;
float4 OutGBufferE = 0;
FMaterialPixelParameters MaterialParameters = GetMaterialPixelParameters(Interpolants, In.SvPosition);
FPixelMaterialInputs PixelMaterialInputs;
float LightmapVTPageTableResult = ( float )0.0f;
{
CalcMaterialParameters(MaterialParameters, PixelMaterialInputs, In.SvPosition, In.bIsFrontFace);
}
const bool bEditorWeightedZBuffering = false;
if (!bEditorWeightedZBuffering)
{
GetMaterialCoverageAndClipping(MaterialParameters, PixelMaterialInputs);
}
const float Dither = InterleavedGradientNoise(MaterialParameters.SvPosition.xy, View_StateFrameIndexMod8);
float3 BaseColor = GetMaterialBaseColor(PixelMaterialInputs);
float Metallic = GetMaterialMetallic(PixelMaterialInputs);
float Specular = GetMaterialSpecular(PixelMaterialInputs);
float Roughness = GetMaterialRoughness(PixelMaterialInputs);
float Anisotropy = GetMaterialAnisotropy(PixelMaterialInputs);
uint ShadingModel = GetMaterialShadingModel(PixelMaterialInputs);
float Opacity = GetMaterialOpacity(PixelMaterialInputs);
float MaterialAO = GetMaterialAmbientOcclusion(PixelMaterialInputs);
float3 SubsurfaceColor = 0;
float SubsurfaceProfile = 0;
float DBufferOpacity = 1.0f;
[flatten]
if ((GetPrimitiveData(MaterialParameters).Flags & 0x8 ) != 0 && View_ShowDecalsMask > 0)
{
uint ValidDBufferTargetMask = GetDBufferTargetMask(uint2(In.SvPosition.xy)) & 7 ;
[branch]
if (ValidDBufferTargetMask)
{
float2 BufferUV = SvPositionToBufferUV(In.SvPosition);
FDBufferData DBufferData = GetDBufferData(BufferUV, ValidDBufferTargetMask);
ApplyDBufferData(DBufferData, MaterialParameters.WorldNormal, SubsurfaceColor, Roughness, BaseColor, Metallic, Specular);
DBufferOpacity = (DBufferData.ColorOpacity + DBufferData.NormalOpacity + DBufferData.RoughnessOpacity) * (1.0f / 3.0f);
}
}
const float BaseMaterialCoverageOverWater = Opacity;
const float WaterVisibility = 1.0 - BaseMaterialCoverageOverWater;
float3 VolumetricLightmapBrickTextureUVs;
FGBufferData GBuffer = (FGBufferData)0;
GBuffer.GBufferAO = MaterialAO;
GBuffer.PerObjectGBufferData = GetPrimitive_PerObjectGBufferData(MaterialParameters.PrimitiveId);
GBuffer.Depth = MaterialParameters.ScreenPosition.w;
GBuffer.PrecomputedShadowFactors = GetPrecomputedShadowMasks(LightmapVTPageTableResult, Interpolants, MaterialParameters, VolumetricLightmapBrickTextureUVs);
SetGBufferForShadingModel(
GBuffer,
MaterialParameters,
Opacity,
BaseColor,
Metallic,
Specular,
Roughness,
Anisotropy,
SubsurfaceColor,
SubsurfaceProfile,
Dither,
ShadingModel
);
GBuffer.SelectiveOutputMask = GetSelectiveOutputMask() >> 4;
GBuffer.Velocity = 0;
[branch]
if ((GetPrimitiveData(MaterialParameters).Flags & 0x20 ) != 0)
{
float3 Velocity = Calculate3DVelocity(MaterialParameters.ScreenPosition, BasePassInterpolants.VelocityPrevScreenPosition);
float4 EncodedVelocity = EncodeVelocityToTexture(Velocity);
GBuffer.Velocity = EncodedVelocity;
}
const bool bChecker = CheckerFromPixelPos(MaterialParameters.SvPosition.xy);
GBuffer.SpecularColor = ComputeF0(Specular, BaseColor, Metallic);
GBuffer.DiffuseColor = BaseColor - BaseColor * Metallic;
{
GBuffer.DiffuseColor = GBuffer.DiffuseColor * View_DiffuseOverrideParameter.w + View_DiffuseOverrideParameter.xyz;
GBuffer.SpecularColor = GBuffer.SpecularColor * View_SpecularOverrideParameter.w + View_SpecularOverrideParameter.xyz;
}
if (View_RenderingReflectionCaptureMask)
{
EnvBRDFApproxFullyRough(GBuffer.DiffuseColor, GBuffer.SpecularColor);
}
float3 InputBentNormal = MaterialParameters.WorldNormal;
[branch] if( GBuffer.ShadingModelID == 4 && 0 )
{
const float2 oct1 = ((float2(GBuffer.CustomData.a, GBuffer.CustomData.z) * 4) - (512.0/255.0)) + UnitVectorToOctahedron(GBuffer.WorldNormal);
InputBentNormal = OctahedronToUnitVector(oct1);
}
const FShadingOcclusion ShadingOcclusion = ApplyBentNormal(MaterialParameters.CameraVector, InputBentNormal, GetWorldBentNormalZero(MaterialParameters), GBuffer.Roughness, MaterialAO);
GBuffer.GBufferAO = AOMultiBounce( Luminance( GBuffer.SpecularColor ), ShadingOcclusion.SpecOcclusion ).g;
GBuffer.DiffuseIndirectSampleOcclusion = GetDiffuseIndirectSampleOcclusion(GBuffer, MaterialParameters.CameraVector, MaterialParameters.WorldNormal, GetWorldBentNormalZero(MaterialParameters), In.SvPosition.xy, MaterialAO);
float3 DiffuseColor = 0;
float3 Color = 0;
float IndirectIrradiance = 0;
float3 ColorSeparateSpecular = 0;
float3 ColorSeparateEmissive = 0;
float3 DiffuseIndirectLighting = 0;
float3 SubsurfaceIndirectLighting = 0;
bool bSeparateWaterMainDirLightLuminance = ( 0 ) > 0 && View_SeparateWaterMainDirLightLuminance > 0.0f;
float3 SeparatedWaterMainDirLightLuminance = float3(0, 0, 0);
float3 DiffuseDir = ShadingOcclusion.BentNormal;
float3 DiffuseColorForIndirect = GBuffer.DiffuseColor;
const bool bEvaluateBackface = GetShadingModelRequiresBackfaceLighting(GBuffer.ShadingModelID);
GetPrecomputedIndirectLightingAndSkyLight(MaterialParameters, Interpolants, BasePassInterpolants, LightmapVTPageTableResult, bEvaluateBackface, DiffuseDir, VolumetricLightmapBrickTextureUVs, DiffuseIndirectLighting, SubsurfaceIndirectLighting, IndirectIrradiance);
float IndirectOcclusion = 1.0f;
float2 NearestResolvedDepthScreenUV = 0;
float DirectionalLightShadow = 1.0f;
float DirectionalLightCloudShadow = 1.0f;
DiffuseColor += (DiffuseIndirectLighting * DiffuseColorForIndirect + SubsurfaceIndirectLighting * SubsurfaceColor) * AOMultiBounce( GBuffer.BaseColor, ShadingOcclusion.DiffOcclusion );
float4 HeightFogging = float4(0,0,0,1);
float4 Fogging = HeightFogging;
float3 Emissive = 0;
float3 GBufferDiffuseColor = GBuffer.DiffuseColor;
float3 GBufferSpecularColor = GBuffer.SpecularColor;
EnvBRDFApproxFullyRough(GBufferDiffuseColor, GBufferSpecularColor);
Color = lerp(Color, GBufferDiffuseColor, View_UnlitViewmodeMask);
Emissive = GetMaterialEmissive(PixelMaterialInputs);
float3 OutOfBoundsMaskLuminance = 0;
[branch]
if (View_OutOfBoundsMask > 0)
{
FPrimitiveSceneData PrimitiveData = GetPrimitiveData(MaterialParameters);
float3 ObjectBounds =
float3(
PrimitiveData.ObjectBoundsX,
PrimitiveData.ObjectBoundsY,
PrimitiveData.ObjectBoundsZ
);
if (any(abs(LWCToFloat(LWCSubtract(MaterialParameters.AbsoluteWorldPosition, PrimitiveData.ObjectWorldPosition))) > ObjectBounds + 1))
{
float Gradient = LWCFrac(LWCDivide(LWCDot(MaterialParameters.AbsoluteWorldPosition, float3(.577f, .577f, .577f)), 500.0f));
OutOfBoundsMaskLuminance = lerp(float3(1,1,0), float3(0,1,1), Gradient.xxx > .5f);
Emissive = OutOfBoundsMaskLuminance;
Opacity = 1;
}
}
Color += DiffuseColor;
Color += Emissive;
bool bStrataSubsurfaceEnable = false;
{
FLightAccumulator LightAccumulator = (FLightAccumulator)0;
Color = Color * Fogging.a + Fogging.rgb;
LightAccumulator_Add(LightAccumulator, Color, 0, 1.0f, false);
Out.MRT[0] = ( LightAccumulator_GetResult(LightAccumulator) ) ;
}
float QuantizationBias = PseudoRandom( MaterialParameters.SvPosition.xy ) - 0.5f;
GBuffer.IndirectIrradiance = IndirectIrradiance;
{
GBuffer.GenericAO = EncodeIndirectIrradiance(GBuffer.IndirectIrradiance * GBuffer.GBufferAO) + QuantizationBias * (1.0 / 255.0);
EncodeGBufferToMRT(Out, GBuffer, QuantizationBias);
if (GBuffer.ShadingModelID == 0 && ! 0 )
{
Out.MRT[1] = 0;
SetGBufferForUnlit(Out.MRT[2]);
Out.MRT[3] = 0;
Out.MRT[ 1 ? 5 : 4] = 0;
Out.MRT[ 1 ? 6 : 5] = 0;
}
if(bEditorWeightedZBuffering)
{
Out.MRT[0].a = 1;
clip(Out.MRT[0].a - GetMaterialOpacityMaskClipValue());
}
const float ViewPreExposure = View_PreExposure;
Out.MRT[0].rgba *= ViewPreExposure;
}