Skip to content

Commit

Permalink
Custom Interpolator Feature (#3281)
Browse files Browse the repository at this point in the history
* Initial merge for custom interpolator feature for Shader Graph.
* Changes made to HDRP and URP shaders, targets, and templates to support injecting additional interpolators.
* Rudimentary warning system for potential overuse.
SG:
* Generator modified to inject custom interpolators.
* Custom Interpolator Node added with searcher support.
* Packing modified to avoid unexpected conflicts and to support packing float1.
* Various other ShaderGraph systems modified to support the feature.
  • Loading branch information
esmelusina authored Feb 23, 2021
1 parent a12035d commit 09dda8d
Show file tree
Hide file tree
Showing 47 changed files with 1,223 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ public static class DecalPasses
pragmas = DecalPragmas.Instanced,
defines = CoreDefines.ScenePicking,
includes = DecalIncludes.ScenePicking,
customInterpolators = CoreCustomInterpolators.Common,
};

public static PassDescriptor DBufferProjector = new PassDescriptor()
Expand All @@ -274,6 +275,7 @@ public static class DecalPasses
pragmas = DecalPragmas.Instanced,
keywords = DecalDefines.Decals,
includes = DecalIncludes.Default,
customInterpolators = CoreCustomInterpolators.Common,
};

public static PassDescriptor DecalProjectorForwardEmissive = new PassDescriptor()
Expand All @@ -296,6 +298,7 @@ public static class DecalPasses
pragmas = DecalPragmas.Instanced,
defines = DecalDefines.Emission,
includes = DecalIncludes.Default,
customInterpolators = CoreCustomInterpolators.Common,
};

public static PassDescriptor DBufferMesh = new PassDescriptor()
Expand All @@ -319,6 +322,7 @@ public static class DecalPasses
pragmas = DecalPragmas.Instanced,
keywords = DecalDefines.Decals,
includes = DecalIncludes.Default,
customInterpolators = CoreCustomInterpolators.Common,
};

public static PassDescriptor DecalMeshForwardEmissive = new PassDescriptor()
Expand All @@ -342,6 +346,7 @@ public static class DecalPasses
pragmas = DecalPragmas.Instanced,
defines = DecalDefines.Emission,
includes = DecalIncludes.Default,
customInterpolators = CoreCustomInterpolators.Common,
};

public static PassDescriptor Preview = new PassDescriptor()
Expand All @@ -364,6 +369,7 @@ public static class DecalPasses
renderStates = DecalRenderStates.Preview,
pragmas = DecalPragmas.Instanced,
includes = DecalIncludes.Default,
customInterpolators = CoreCustomInterpolators.Common,
};
}
#endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public static PassDescriptor GenerateDistortionPass(bool supportLighting)
pragmas = CorePragmas.DotsInstancedInV1AndV2,
defines = CoreDefines.ShaderGraphRaytracingDefault,
includes = GenerateIncludes(),
customInterpolators = CoreCustomInterpolators.Common,
};

RenderStateCollection GenerateRenderState()
Expand Down Expand Up @@ -90,6 +91,7 @@ public static PassDescriptor GenerateScenePicking()
pragmas = CorePragmas.DotsInstancedInV1AndV2EditorSync,
defines = CoreDefines.ScenePicking,
includes = GenerateIncludes(),
customInterpolators = CoreCustomInterpolators.Common,
};

IncludeCollection GenerateIncludes()
Expand Down Expand Up @@ -126,6 +128,7 @@ public static PassDescriptor GenerateSceneSelection(bool supportLighting)
pragmas = CorePragmas.DotsInstancedInV1AndV2EditorSync,
defines = CoreDefines.SceneSelection,
includes = GenerateIncludes(),
customInterpolators = CoreCustomInterpolators.Common,
};

IncludeCollection GenerateIncludes()
Expand Down Expand Up @@ -176,6 +179,7 @@ static public PassDescriptor GenerateShadowCaster(bool supportLighting)
renderStates = CoreRenderStates.ShadowCaster,
pragmas = CorePragmas.DotsInstancedInV2Only,
includes = GenerateIncludes(),
customInterpolators = CoreCustomInterpolators.Common,
};

IncludeCollection GenerateIncludes()
Expand Down Expand Up @@ -265,6 +269,7 @@ public static PassDescriptor GenerateDepthForwardOnlyPass(bool supportLighting)
pragmas = CorePragmas.DotsInstancedInV2Only,
defines = supportLighting ? CoreDefines.DepthForwardOnly : CoreDefines.DepthForwardOnlyUnlit,
includes = GenerateIncludes(),
customInterpolators = CoreCustomInterpolators.Common,
};

RenderStateCollection GenerateRenderState()
Expand Down Expand Up @@ -331,6 +336,7 @@ public static PassDescriptor GenerateMotionVectors(bool supportLighting, bool su
defines = GenerateDefines(),
pragmas = CorePragmas.DotsInstancedInV2Only,
includes = GenerateIncludes(),
customInterpolators = CoreCustomInterpolators.Common,
};

DefineCollection GenerateDefines()
Expand Down Expand Up @@ -402,6 +408,7 @@ public static PassDescriptor GenerateForwardOnlyPass(bool supportLighting)
includes = GenerateIncludes(),

virtualTextureFeedback = true,
customInterpolators = CoreCustomInterpolators.Common
};

FieldCollection GenerateRequiredFields()
Expand Down Expand Up @@ -509,6 +516,7 @@ public static PassDescriptor GenerateBackThenFront(bool supportLighting)
pragmas = CorePragmas.DotsInstancedInV1AndV2,
defines = CoreDefines.BackThenFront,
includes = GenerateIncludes(),
customInterpolators = CoreCustomInterpolators.Common
};

IncludeCollection GenerateIncludes()
Expand Down Expand Up @@ -583,6 +591,7 @@ public static PassDescriptor GenerateTransparentDepthPrepass(bool supportLightin
pragmas = CorePragmas.DotsInstancedInV1AndV2,
defines = CoreDefines.TransparentDepthPrepass,
includes = GenerateIncludes(),
customInterpolators = CoreCustomInterpolators.Common,
};

RenderStateCollection GenerateRenderState()
Expand Down Expand Up @@ -669,6 +678,7 @@ public static PassDescriptor GenerateTransparentDepthPostpass(bool supportLighti
pragmas = CorePragmas.DotsInstancedInV1AndV2,
defines = CoreDefines.TransparentDepthPostpass,
includes = GenerateIncludes(),
customInterpolators = CoreCustomInterpolators.Common,
};

IncludeCollection GenerateIncludes()
Expand Down Expand Up @@ -725,6 +735,7 @@ public static PassDescriptor GenerateLitDepthOnly()
defines = CoreDefines.ShaderGraphRaytracingDefault,
keywords = LitDepthOnlyKeywords,
includes = DepthOnlyIncludes,
customInterpolators = CoreCustomInterpolators.Common,
};
}

Expand Down Expand Up @@ -766,7 +777,7 @@ public static PassDescriptor GenerateGBuffer()
defines = CoreDefines.ShaderGraphRaytracingDefault,
keywords = GBufferKeywords,
includes = GBufferIncludes,

customInterpolators = CoreCustomInterpolators.Common,
virtualTextureFeedback = true,
};
}
Expand Down Expand Up @@ -821,7 +832,7 @@ public static PassDescriptor GenerateLitForward()
pragmas = CorePragmas.DotsInstancedInV1AndV2,
defines = CoreDefines.Forward,
includes = ForwardIncludes,

customInterpolators = CoreCustomInterpolators.Common,
virtualTextureFeedback = true,
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ static class HDStructs
{
name = "VaryingsMeshToPS",
packFields = true,
populateWithCustomInterpolators = true,
fields = new FieldDescriptor[]
{
HDStructFields.VaryingsMeshToPS.positionCS,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ sealed class HDTarget : Target, IHasMetadata, ILegacyTarget
[SerializeField]
string m_CustomEditorGUI;

internal override bool ignoreCustomInterpolators => false;
internal override int padCustomInterpolatorLimit => 8;

public override bool IsNodeAllowedByTarget(Type nodeType)
{
SRPFilterAttribute srpFilter = NodeClassCache.GetAttributeOnNodeType<SRPFilterAttribute>(nodeType);
Expand Down Expand Up @@ -1404,4 +1407,19 @@ static class CoreKeywordDescriptors
};
}
#endregion

#region CustomInterpolators
static class CoreCustomInterpolators
{
public static readonly CustomInterpSubGen.Collection Common = new CustomInterpSubGen.Collection
{
CustomInterpSubGen.Descriptor.MakeDefine(CustomInterpSubGen.Splice.k_splicePreVertex, "USE_CUSTOMINTERP_APPLYMESHMOD"),
CustomInterpSubGen.Descriptor.MakeStruct(CustomInterpSubGen.Splice.k_splicePreInclude, "CustomInterpolators", "USE_CUSTOMINTERP_SUBSTRUCT"),
CustomInterpSubGen.Descriptor.MakeBlock("CustomInterpolatorVertexDefinitionToVaryings", "varyings", "vertexDescription"),
CustomInterpSubGen.Descriptor.MakeBlock("CustomInterpolatorVaryingsToFragInputs", "output.customInterpolators", "input"),
CustomInterpSubGen.Descriptor.MakeBlock(CustomInterpSubGen.Splice.k_spliceCopyToSDI, "output", "input.customInterpolators")
};
}

#endregion
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ Pass
$splice(PassKeywords)
$splice(GraphKeywords)

// For custom interpolators to inject a substruct definition before FragInputs definition,
// allowing for FragInputs to capture CI's intended for ShaderGraph's SDI.
$splice(CustomInterpolatorPreInclude)

#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl"
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Texture.hlsl"
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariables.hlsl"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ FragInputs BuildFragInputs(VaryingsMeshToPS input)
$FragInputs.texCoord3: output.texCoord3 = input.texCoord3;
$FragInputs.color: output.color = input.color;

// splice point to copy custom interpolator fields from varyings to frag inputs
$splice(CustomInterpolatorVaryingsToFragInputs)

return output;
}

Expand Down Expand Up @@ -60,6 +63,9 @@ SurfaceDescriptionInputs FragInputsToSurfaceDescriptionInputs(FragInputs input,
$SurfaceDescriptionInputs.FaceSign: output.FaceSign = input.isFrontFace;
$SurfaceDescriptionInputs.TimeParameters: output.TimeParameters = _TimeParameters.xyz; // This is mainly for LW as HD overwrite this value

// splice point to copy frag inputs custom interpolator pack into the SDI
$splice(CustomInterpolatorCopyToSDI)

return output;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,17 @@ VertexDescriptionInputs AttributesMeshToVertexDescriptionInputs(AttributesMesh i
return output;
}

AttributesMesh ApplyMeshModification(AttributesMesh input, float3 timeParameters)
// This is used for injecting the define below.
$splice(CustomInterpolatorPreVertex)



AttributesMesh ApplyMeshModification(AttributesMesh input, float3 timeParameters
#if defined(USE_CUSTOMINTERP_APPLYMESHMOD) // mirrored in VertMesh.hlsl and MotionVectorVertexShaderCommon.hlsl
// use ifdef via TESSELLATION_ON to use VaryingsMeshToDS (Domain varyings instead of pixel varyings) whenever SG is modified to support Tess.
, inout VaryingsMeshToPS varyings
#endif
)
{
// build graph inputs
VertexDescriptionInputs vertexDescriptionInputs = AttributesMeshToVertexDescriptionInputs(input);
Expand All @@ -54,5 +64,8 @@ AttributesMesh ApplyMeshModification(AttributesMesh input, float3 timeParameters
$VertexDescription.Normal: input.normalOS = vertexDescription.Normal;
$VertexDescription.Tangent: input.tangentOS.xyz = vertexDescription.Tangent;

// The purpose of the above ifdef, this allows shader graph custom interpolators to write directly to the varyings structs.
$splice(CustomInterpolatorVertexDefinitionToVaryings)

return input;
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ struct FragInputs

// For two sided lighting
bool isFrontFace;

// append a substruct for custom interpolators to be copied correctly into SDI from Varyings.
#if defined(USE_CUSTOMINTERP_SUBSTRUCT)
CustomInterpolators customInterpolators;
#endif
};

void GetVaryingsDataDebug(uint paramId, FragInputs input, inout float3 result, inout bool needLinearToSRGB)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,14 @@ PackedVaryingsType MotionVectorVS(inout VaryingsType varyingsType, AttributesMes
// Need to apply any vertex animation to the previous worldspace position, if we want it to show up in the motion vector buffer
#if defined(HAVE_MESH_MODIFICATION)
AttributesMesh previousMesh = inputMesh;
previousMesh.positionOS = effectivePositionOS ;
previousMesh.positionOS = effectivePositionOS;

previousMesh = ApplyMeshModification(previousMesh, _LastTimeParameters.xyz
#if defined(USE_CUSTOMINTERP_APPLYMESHMOD)
, varyingsType.vmesh
#endif
);

previousMesh = ApplyMeshModification(previousMesh, _LastTimeParameters.xyz);
float3 previousPositionRWS = TransformPreviousObjectToWorld(previousMesh.positionOS);
#else
float3 previousPositionRWS = TransformPreviousObjectToWorld(effectivePositionOS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,19 @@ VaryingsToDS InterpolateWithBaryCoordsToDS(VaryingsToDS input0, VaryingsToDS inp
VaryingsMeshType VertMesh(AttributesMesh input, float3 worldSpaceOffset)
{
VaryingsMeshType output;
ZERO_INITIALIZE(VaryingsMeshType, output);

UNITY_SETUP_INSTANCE_ID(input);
UNITY_TRANSFER_INSTANCE_ID(input, output);


#if defined(HAVE_MESH_MODIFICATION)
input = ApplyMeshModification(input, _TimeParameters.xyz);
input = ApplyMeshModification(input, _TimeParameters.xyz
// If custom interpolators are in use, we need to write them to the shader graph generated VaryingsMesh
#if defined(USE_CUSTOMINTERP_APPLYMESHMOD)
, output
#endif
);
#endif

// This return the camera relative position (if enable)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ Varyings BuildVaryings(Attributes input)
VertexDescriptionInputs vertexDescriptionInputs = BuildVertexDescriptionInputs(input);
VertexDescription vertexDescription = VertexDescriptionFunction(vertexDescriptionInputs);

#if defined(CUSTOMINTERPOLATOR_VARYPASSTHROUGH_FUNC)
CustomInterpolatorPassThroughFunc(output, vertexDescription);
#endif

// Assign modified vertex attributes
input.positionOS = vertexDescription.Position;
#if defined(VARYINGS_NEED_NORMAL_WS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,9 @@ static class LitPasses
pragmas = CorePragmas.Forward, // NOTE: SM 2.0 only GL
keywords = LitKeywords.Forward,
includes = LitIncludes.Forward,

// Custom Interpolator Support
customInterpolators = CoreCustomInterpDescriptors.Common
};

public static PassDescriptor ForwardOnly = new PassDescriptor
Expand Down Expand Up @@ -414,6 +417,9 @@ static class LitPasses
pragmas = CorePragmas.Forward, // NOTE: SM 2.0 only GL
keywords = LitKeywords.Forward,
includes = LitIncludes.Forward,

// Custom Interpolator Support
customInterpolators = CoreCustomInterpDescriptors.Common
};

// Deferred only in SM4.5, MRT not supported in GLES2
Expand Down Expand Up @@ -442,6 +448,9 @@ static class LitPasses
pragmas = CorePragmas.DOTSGBuffer,
keywords = LitKeywords.GBuffer,
includes = LitIncludes.GBuffer,

// Custom Interpolator Support
customInterpolators = CoreCustomInterpDescriptors.Common
};

public static PassDescriptor Meta = new PassDescriptor()
Expand Down Expand Up @@ -469,6 +478,9 @@ static class LitPasses
pragmas = CorePragmas.Default,
keywords = LitKeywords.Meta,
includes = LitIncludes.Meta,

// Custom Interpolator Support
customInterpolators = CoreCustomInterpDescriptors.Common
};

public static readonly PassDescriptor _2D = new PassDescriptor()
Expand All @@ -493,6 +505,9 @@ static class LitPasses
renderStates = CoreRenderStates.Default,
pragmas = CorePragmas.Instanced,
includes = LitIncludes._2D,

// Custom Interpolator Support
customInterpolators = CoreCustomInterpDescriptors.Common
};

public static readonly PassDescriptor DepthNormalOnly = new PassDescriptor()
Expand Down Expand Up @@ -520,6 +535,9 @@ static class LitPasses
renderStates = CoreRenderStates.DepthNormalsOnly,
pragmas = CorePragmas.Instanced,
includes = CoreIncludes.DepthNormalsOnly,

// Custom Interpolator Support
customInterpolators = CoreCustomInterpDescriptors.Common
};
}
#endregion
Expand Down
Loading

0 comments on commit 09dda8d

Please sign in to comment.