diff --git a/com.unity.render-pipelines.high-definition/Editor/Material/Decal/ShaderGraph/DecalSubTarget.cs b/com.unity.render-pipelines.high-definition/Editor/Material/Decal/ShaderGraph/DecalSubTarget.cs index 67e0d60c1e5..0a70a92da46 100644 --- a/com.unity.render-pipelines.high-definition/Editor/Material/Decal/ShaderGraph/DecalSubTarget.cs +++ b/com.unity.render-pipelines.high-definition/Editor/Material/Decal/ShaderGraph/DecalSubTarget.cs @@ -18,21 +18,19 @@ sealed partial class DecalSubTarget : HDSubTarget, ILegacyTarget, IRequiresData< { public DecalSubTarget() => displayName = "Decal"; - public static string passTemplatePath => $"{HDUtils.GetHDRenderPipelinePath()}Editor/Material/Decal/ShaderGraph/DecalPass.template"; - - static string[] passTemplateMaterialDirectories = new string[] + protected override string templatePath => $"{HDUtils.GetHDRenderPipelinePath()}Editor/Material/Decal/ShaderGraph/DecalPass.template"; + protected override string[] templateMaterialDirectories => new string[] { $"{HDUtils.GetHDRenderPipelinePath()}Editor/Material/ShaderGraph/Templates", $"{HDUtils.GetHDRenderPipelinePath()}Editor/Material/ShaderGraph/Templates/" }; - - protected override string templatePath => passTemplatePath; - protected override string[] templateMaterialDirectories => passTemplateMaterialDirectories; protected override string subTargetAssetGuid => "3ec927dfcb5d60e4883b2c224857b6c2"; protected override string customInspector => "Rendering.HighDefinition.DecalGUI"; protected override string renderType => HDRenderTypeTags.Opaque.ToString(); protected override string renderQueue => HDRenderQueue.GetShaderTagValue(HDRenderQueue.ChangeType(HDRenderQueue.RenderQueueType.Opaque, decalData.drawOrder, false, false)); protected override ShaderID shaderID => HDShaderUtils.ShaderID.SG_Lit; + protected override FieldDescriptor subShaderField => new FieldDescriptor(kSubShader, "Decal Subshader", ""); + protected override string subShaderInclude => "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/Decal/Decal.hlsl"; // Material Data DecalData m_DecalData; @@ -53,7 +51,12 @@ public DecalData decalData protected override IEnumerable EnumerateSubShaders() { - yield return SubShaders.Decal; + yield return PostProcessSubShader(SubShaders.Decal); + } + + protected override void CollectPassKeywords(ref PassDescriptor pass) + { + pass.keywords.Add(CoreKeywordDescriptors.AlphaTest, new FieldCondition(Fields.AlphaTest, true)); } public static FieldDescriptor AffectsAlbedo = new FieldDescriptor(kMaterial, "AffectsAlbedo", "_MATERIAL_AFFECTS_ALBEDO 1"); @@ -127,7 +130,6 @@ static class SubShaders { public static SubShaderDescriptor Decal = new SubShaderDescriptor() { - pipelineTag = HDRenderPipeline.k_ShaderTagName, generatesPreview = true, passes = new PassCollection { @@ -156,10 +158,6 @@ public static class DecalPasses lightMode = DecalSystem.s_MaterialSGDecalPassNames[(int)DecalSystem.MaterialSGDecalPass.ShaderGraph_DBufferProjector3RT], useInPreview = false, - // Template - passTemplatePath = passTemplatePath, - sharedTemplateDirectories = passTemplateMaterialDirectories, - // Port mask validPixelBlocks = DecalBlockMasks.FragmentDefault, @@ -180,10 +178,6 @@ public static class DecalPasses lightMode = DecalSystem.s_MaterialSGDecalPassNames[(int)DecalSystem.MaterialSGDecalPass.ShaderGraph_DBufferProjector4RT], useInPreview = false, - // Template - passTemplatePath = passTemplatePath, - sharedTemplateDirectories = passTemplateMaterialDirectories, - // Port mask validPixelBlocks = DecalBlockMasks.FragmentDefault, @@ -206,10 +200,6 @@ public static class DecalPasses lightMode = DecalSystem.s_MaterialSGDecalPassNames[(int)DecalSystem.MaterialSGDecalPass.ShaderGraph_ProjectorEmissive], useInPreview = false, - // Template - passTemplatePath = passTemplatePath, - sharedTemplateDirectories = passTemplateMaterialDirectories, - // Port mask validPixelBlocks = DecalBlockMasks.FragmentEmissive, @@ -231,10 +221,6 @@ public static class DecalPasses lightMode = DecalSystem.s_MaterialSGDecalPassNames[(int)DecalSystem.MaterialSGDecalPass.ShaderGraph_DBufferMesh3RT], useInPreview = false, - // Template - passTemplatePath = passTemplatePath, - sharedTemplateDirectories = passTemplateMaterialDirectories, - // Port mask validPixelBlocks = DecalBlockMasks.FragmentDefault, @@ -258,10 +244,6 @@ public static class DecalPasses lightMode = DecalSystem.s_MaterialSGDecalPassNames[(int)DecalSystem.MaterialSGDecalPass.ShaderGraph_DBufferMesh4RT], useInPreview = false, - // Template - passTemplatePath = passTemplatePath, - sharedTemplateDirectories = passTemplateMaterialDirectories, - // Port mask validPixelBlocks = DecalBlockMasks.FragmentDefault, @@ -285,10 +267,6 @@ public static class DecalPasses lightMode = DecalSystem.s_MaterialSGDecalPassNames[(int)DecalSystem.MaterialSGDecalPass.ShaderGraph_MeshEmissive], useInPreview = false, - // Template - passTemplatePath = passTemplatePath, - sharedTemplateDirectories = passTemplateMaterialDirectories, - // Port mask validPixelBlocks = DecalBlockMasks.FragmentMeshEmissive, @@ -311,10 +289,6 @@ public static class DecalPasses lightMode = "ForwardOnly", useInPreview = true, - // Template - passTemplatePath = passTemplatePath, - sharedTemplateDirectories = passTemplateMaterialDirectories, - // Port mask validPixelBlocks = DecalBlockMasks.FragmentMeshEmissive, @@ -592,7 +566,6 @@ static class Descriptors }; } #endregion - // protected override IncludeCollection subShaderIncludes => "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/Decal/Decal.hlsl"; #region Includes static class DecalIncludes diff --git a/com.unity.render-pipelines.high-definition/Editor/Material/Eye/ShaderGraph/EyeSubTarget.cs b/com.unity.render-pipelines.high-definition/Editor/Material/Eye/ShaderGraph/EyeSubTarget.cs index 74fb4ad68af..fc5a9b74225 100644 --- a/com.unity.render-pipelines.high-definition/Editor/Material/Eye/ShaderGraph/EyeSubTarget.cs +++ b/com.unity.render-pipelines.high-definition/Editor/Material/Eye/ShaderGraph/EyeSubTarget.cs @@ -59,6 +59,10 @@ public override void GetFields(ref TargetFieldContext context) context.AddField(Eye, eyeData.materialType == EyeData.MaterialType.Eye); context.AddField(EyeCinematic, eyeData.materialType == EyeData.MaterialType.EyeCinematic); context.AddField(SubsurfaceScattering, eyeData.subsurfaceScattering && systemData.surfaceType != SurfaceType.Transparent); + + context.AddField(SpecularAA, lightingData.specularAA && + context.pass.validPixelBlocks.Contains(HDBlockFields.SurfaceDescription.SpecularAAThreshold) && + context.pass.validPixelBlocks.Contains(HDBlockFields.SurfaceDescription.SpecularAAScreenSpaceVariance)); } public override void GetActiveBlocks(ref TargetActiveBlockContext context) diff --git a/com.unity.render-pipelines.high-definition/Editor/Material/Fabric/ShaderGraph/FabricSubTarget.cs b/com.unity.render-pipelines.high-definition/Editor/Material/Fabric/ShaderGraph/FabricSubTarget.cs index c477cc86ef2..73b7c60e95a 100644 --- a/com.unity.render-pipelines.high-definition/Editor/Material/Fabric/ShaderGraph/FabricSubTarget.cs +++ b/com.unity.render-pipelines.high-definition/Editor/Material/Fabric/ShaderGraph/FabricSubTarget.cs @@ -70,6 +70,10 @@ public override void GetFields(ref TargetFieldContext context) context.AddField(SubsurfaceScattering, fabricData.subsurfaceScattering && systemData.surfaceType != SurfaceType.Transparent); context.AddField(Transmission, fabricData.transmission); context.AddField(EnergyConservingSpecular, fabricData.energyConservingSpecular); + + context.AddField(SpecularAA, lightingData.specularAA && + context.pass.validPixelBlocks.Contains(HDBlockFields.SurfaceDescription.SpecularAAThreshold) && + context.pass.validPixelBlocks.Contains(HDBlockFields.SurfaceDescription.SpecularAAScreenSpaceVariance)); } public override void GetActiveBlocks(ref TargetActiveBlockContext context) diff --git a/com.unity.render-pipelines.high-definition/Editor/Material/Lit/ShaderGraph/HDLitSubTarget.cs b/com.unity.render-pipelines.high-definition/Editor/Material/Lit/ShaderGraph/HDLitSubTarget.cs index 2f17dd7c09f..9d5ac222318 100644 --- a/com.unity.render-pipelines.high-definition/Editor/Material/Lit/ShaderGraph/HDLitSubTarget.cs +++ b/com.unity.render-pipelines.high-definition/Editor/Material/Lit/ShaderGraph/HDLitSubTarget.cs @@ -119,6 +119,10 @@ public override void GetFields(ref TargetFieldContext context) context.AddField(ClearCoat, litData.clearCoat); // Enable clear coat material feature context.AddField(Tangent, descs.Contains(HDBlockFields.SurfaceDescription.Tangent) && context.pass.validPixelBlocks.Contains(HDBlockFields.SurfaceDescription.Tangent)); context.AddField(RayTracing, litData.rayTracing); + + context.AddField(SpecularAA, lightingData.specularAA && + context.pass.validPixelBlocks.Contains(HDBlockFields.SurfaceDescription.SpecularAAThreshold) && + context.pass.validPixelBlocks.Contains(HDBlockFields.SurfaceDescription.SpecularAAScreenSpaceVariance)); } public override void GetActiveBlocks(ref TargetActiveBlockContext context) diff --git a/com.unity.render-pipelines.high-definition/Editor/Material/ShaderGraph/HDShaderPasses.cs b/com.unity.render-pipelines.high-definition/Editor/Material/ShaderGraph/HDShaderPasses.cs index c5b07266101..c5605bfb61e 100644 --- a/com.unity.render-pipelines.high-definition/Editor/Material/ShaderGraph/HDShaderPasses.cs +++ b/com.unity.render-pipelines.high-definition/Editor/Material/ShaderGraph/HDShaderPasses.cs @@ -26,7 +26,6 @@ public static PassDescriptor GenerateDistortionPass(bool supportLighting) renderStates = GenerateRenderState(), pragmas = CorePragmas.DotsInstancedInV1AndV2, defines = CoreDefines.ShaderGraphRaytracingHigh, - keywords = CoreKeywords.HDBase, includes = GenerateIncludes(), }; @@ -91,7 +90,6 @@ public static PassDescriptor GenerateSceneSelection(bool supportLighting) renderStates = CoreRenderStates.SceneSelection, pragmas = CorePragmas.DotsInstancedInV1AndV2EditorSync, defines = CoreDefines.SceneSelection, - keywords = CoreKeywords.HDBase, includes = GenerateIncludes(), }; @@ -141,7 +139,6 @@ static public PassDescriptor GenerateShadowCaster(bool supportLighting) // Collections renderStates = CoreRenderStates.ShadowCaster, pragmas = CorePragmas.DotsInstancedInV2Only, - keywords = CoreKeywords.HDBase, includes = GenerateIncludes(), }; @@ -188,7 +185,6 @@ public static PassDescriptor GenerateMETA(bool supportLighting) renderStates = CoreRenderStates.Meta, pragmas = CorePragmas.DotsInstancedInV1AndV2, defines = CoreDefines.ShaderGraphRaytracingHigh, - keywords = CoreKeywords.HDBase, includes = GenerateIncludes(), }; @@ -232,28 +228,9 @@ public static PassDescriptor GenerateDepthForwardOnlyPass(bool supportLighting) renderStates = GenerateRenderState(), pragmas = CorePragmas.DotsInstancedInV2Only, defines = supportLighting ? CoreDefines.DepthForwardOnly : null, - keywords = GenerateKeywords(), includes = GenerateIncludes(), }; - KeywordCollection GenerateKeywords() - { - var keywords = new KeywordCollection - { - { CoreKeywords.HDBase }, - { CoreKeywordDescriptors.WriteMsaaDepth }, - // Note: normal buffer write is a define for forward only - { CoreKeywordDescriptors.AlphaToMask, new FieldCondition(Fields.AlphaToMask, true) }, - }; - - if (supportLighting) - { - keywords.Add(CoreKeywordDescriptors.WriteDecalBuffer); - } - - return keywords; - } - RenderStateCollection GenerateRenderState() { var renderState = new RenderStateCollection{ CoreRenderStates.DepthOnly }; @@ -329,7 +306,6 @@ public static PassDescriptor GenerateMotionVectors(bool supportLighting, bool su renderStates = GenerateRenderState(), defines = GenerateDefines(), pragmas = CorePragmas.DotsInstancedInV2Only, - keywords = GenerateKeywords(), includes = GenerateIncludes(), }; @@ -338,16 +314,13 @@ DefineCollection GenerateDefines() if (!supportLighting) return null; - var defines = new DefineCollection - { - { RayTracingNode.GetRayTracingKeyword(), 0 }, - }; + var defines = new DefineCollection { Defines.raytracingHigh }; // #define WRITE_NORMAL_BUFFER for motion vector in forward case - if (supportForward) - { - defines.Add(CoreKeywordDescriptors.WriteNormalBuffer, 1); - } + // if (supportForward) + // { + // defines.Add(CoreKeywordDescriptors.WriteNormalBuffer, 1); + // } return defines; } @@ -371,27 +344,6 @@ RenderStateCollection GenerateRenderState() return renderState; } - KeywordCollection GenerateKeywords() - { - var keywords = new KeywordCollection - { - { CoreKeywords.HDBase }, - { CoreKeywordDescriptors.WriteMsaaDepth }, - { CoreKeywordDescriptors.AlphaToMask, new FieldCondition(Fields.AlphaToMask, true) }, - }; - - // #pragma multi_compile _ WRITE_DECAL_BUFFER for both deferred and forward - // #pragma multi_compile _ WRITE_NORMAL_BUFFER for deferred (forward is a define) - if (supportLighting) - { - keywords.Add(CoreKeywordDescriptors.WriteDecalBuffer); - if (!supportForward) - keywords.Add(CoreKeywordDescriptors.WriteNormalBuffer); - } - - return keywords; - } - IncludeCollection GenerateIncludes() { var includes = new IncludeCollection(); @@ -433,7 +385,6 @@ public static PassDescriptor GenerateForwardOnlyPass(bool supportLighting) renderStates = CoreRenderStates.Forward, pragmas = CorePragmas.DotsInstancedInV2Only, defines = supportLighting ? CoreDefines.Forward : null, - keywords = supportLighting ? CoreKeywords.Forward : UnlitForwardKeywords, includes = GenerateIncludes(), virtualTextureFeedback = true, @@ -483,13 +434,6 @@ IncludeCollection GenerateIncludes() } } - public static KeywordCollection UnlitForwardKeywords = new KeywordCollection - { - { CoreKeywords.HDBase }, - { CoreKeywordDescriptors.DebugDisplay }, - { CoreKeywordDescriptors.Shadow, new FieldCondition(HDUnlitSubTarget.EnableShadowMatte, true) }, - }; - #endregion #region Back then front pass @@ -509,7 +453,6 @@ public static PassDescriptor GenerateBackThenFront(bool supportLighting) renderStates = CoreRenderStates.TransparentBackface, pragmas = CorePragmas.DotsInstancedInV1AndV2, defines = CoreDefines.BackThenFront, - keywords = supportLighting ? CoreKeywords.BackThenFrontTransparent : UnlitForwardKeywords, includes = GenerateIncludes(), }; @@ -582,23 +525,9 @@ public static PassDescriptor GenerateTransparentDepthPrepass(bool supportLightin renderStates = GenerateRenderState(), pragmas = CorePragmas.DotsInstancedInV1AndV2, defines = CoreDefines.TransparentDepthPrepass, - keywords = GenerateKeywords(), includes = GenerateIncludes(), }; - KeywordCollection GenerateKeywords() - { - KeywordCollection keywords = new KeywordCollection { CoreKeywords.HDBase }; - - // For now we can generate WriteNormalBuffer the way we want with SG generator - // as it require a conditional define. So instead it is hard coded in - // ShaderPass.template. Looks for SHADERPASS_TRANSPARENT_DEPTH_PREPASS in the file - //if (supportLighting) - // keywords.Add(CoreKeywordDescriptors.WriteNormalBuffer); - - return keywords; - } - RenderStateCollection GenerateRenderState() { var renderState = new RenderStateCollection @@ -693,7 +622,6 @@ public static PassDescriptor GenerateTransparentDepthPostpass(bool supportLighti renderStates = GenerateRenderState(), pragmas = CorePragmas.DotsInstancedInV1AndV2, defines = CoreDefines.TransparentDepthPostpass, - keywords = CoreKeywords.HDBase, includes = GenerateIncludes(), }; @@ -768,11 +696,7 @@ public static PassDescriptor GenerateLitDepthOnly() public static KeywordCollection LitDepthOnlyKeywords = new KeywordCollection { - { CoreKeywords.HDBase }, - { CoreKeywordDescriptors.WriteMsaaDepth }, { CoreKeywordDescriptors.WriteNormalBuffer }, - { CoreKeywordDescriptors.WriteDecalBuffer }, - { CoreKeywordDescriptors.AlphaToMask, new FieldCondition(Fields.AlphaToMask, true) }, }; #endregion @@ -803,12 +727,7 @@ public static PassDescriptor GenerateGBuffer() public static KeywordCollection GBufferKeywords = new KeywordCollection { - { CoreKeywords.HDBase }, - { CoreKeywordDescriptors.DebugDisplay }, - { CoreKeywords.Lightmaps }, - { CoreKeywordDescriptors.ShadowsShadowmask }, { CoreKeywordDescriptors.LightLayers }, - { CoreKeywordDescriptors.Decals }, }; public static IncludeCollection GBufferIncludes = new IncludeCollection @@ -855,7 +774,6 @@ public static PassDescriptor GenerateLitForward() renderStates = CoreRenderStates.Forward, pragmas = CorePragmas.DotsInstancedInV1AndV2, defines = CoreDefines.Forward, - keywords = CoreKeywords.Forward, includes = ForwardIncludes, virtualTextureFeedback = true, @@ -895,7 +813,6 @@ public static PassDescriptor GenerateLitRaytracingPrepass() renderStates = RayTracingPrepassRenderState, pragmas = LitRaytracingPrepassPragmas, defines = CoreDefines.ShaderGraphRaytracingHigh, - keywords = CoreKeywords.HDBase, includes = RayTracingPrepassIncludes, }; } @@ -945,7 +862,6 @@ public static PassDescriptor GenerateRaytracingIndirect(bool supportLighting) // Collections pragmas = CorePragmas.RaytracingBasic, defines = supportLighting ? RaytracingIndirectDefines : null, - keywords = supportLighting ? CoreKeywords.RaytracingIndirect : CoreKeywords.RaytracingIndirectUnlit, includes = GenerateIncludes(), }; @@ -1061,7 +977,6 @@ public static PassDescriptor GenerateRaytracingForward(bool supportLighting) // Collections pragmas = CorePragmas.RaytracingBasic, defines = supportLighting ? RaytracingForwardDefines : null, - keywords = supportLighting ? CoreKeywords.RaytracingForward : CoreKeywords.RaytracingForwardUnlit, includes = GenerateIncludes(), }; @@ -1128,7 +1043,7 @@ public static PassDescriptor GenerateRaytracingGBuffer(bool supportLighting) // Collections pragmas = CorePragmas.RaytracingBasic, defines = supportLighting ? RaytracingGBufferDefines : null, - keywords = supportLighting ? CoreKeywords.RaytracingGBuffer : CoreKeywords.RaytracingGBufferUnlit, + keywords = supportLighting ? CoreKeywords.RaytracingGBuffer : null, includes = GenerateIncludes(), }; @@ -1189,7 +1104,6 @@ public static PassDescriptor GeneratePathTracing(bool supportLighting) //Collections pragmas = CorePragmas.RaytracingBasic, defines = supportLighting ? RaytracingPathTracingDefines : null, - keywords = CoreKeywords.HDBaseNoCrossFade, includes = GenerateIncludes(), }; @@ -1255,7 +1169,6 @@ public static PassDescriptor GenerateRaytracingSubsurface() //Collections pragmas = CorePragmas.RaytracingBasic, defines = RaytracingSubsurfaceDefines, - keywords = CoreKeywords.RaytracingForward, includes = GenerateIncludes(), }; diff --git a/com.unity.render-pipelines.high-definition/Editor/Material/ShaderGraph/HDSubTarget.cs b/com.unity.render-pipelines.high-definition/Editor/Material/ShaderGraph/HDSubTarget.cs index 44e609a1d88..b16eea5f1ca 100644 --- a/com.unity.render-pipelines.high-definition/Editor/Material/ShaderGraph/HDSubTarget.cs +++ b/com.unity.render-pipelines.high-definition/Editor/Material/ShaderGraph/HDSubTarget.cs @@ -49,6 +49,13 @@ protected virtual int ComputeMaterialNeedsUpdateHash() protected abstract string renderQueue { get; } protected abstract string templatePath { get; } protected abstract string[] templateMaterialDirectories { get; } + protected abstract FieldDescriptor subShaderField { get; } + protected abstract string subShaderInclude { get; } + + protected virtual string postDecalsInclude => null; + protected virtual string raytracingInclude => null; + protected virtual bool supportPathtracing => false; + protected virtual bool supportRaytracing => false; public virtual string identifier => GetType().Name; @@ -100,6 +107,78 @@ public override void Setup(ref TargetSetupContext context) } } + protected SubShaderDescriptor PostProcessSubShader(SubShaderDescriptor subShaderDescriptor) + { + if (String.IsNullOrEmpty(subShaderDescriptor.pipelineTag)) + subShaderDescriptor.pipelineTag = HDRenderPipeline.k_ShaderTagName; + + var passes = subShaderDescriptor.passes.ToArray(); + PassCollection finalPasses = new PassCollection(); + for (int i = 0; i < passes.Length; i++) + { + var passDescriptor = passes[i].descriptor; + passDescriptor.passTemplatePath = templatePath; + passDescriptor.sharedTemplateDirectories = templateMaterialDirectories; + + // Add the subShader to enable fields that depends on it + var originalRequireFields = passDescriptor.requiredFields; + // Duplicate require fields to avoid unwanted shared list modification + passDescriptor.requiredFields = new FieldCollection(); + if (originalRequireFields != null) + foreach (var field in originalRequireFields) + passDescriptor.requiredFields.Add(field.field); + passDescriptor.requiredFields.Add(subShaderField); + + IncludeCollection finalIncludes = new IncludeCollection(); + var includeList = passDescriptor.includes.Select(include => include.descriptor).ToList(); + + // Replace include placeholders if necessary: + foreach (var include in passDescriptor.includes) + { + if (include.descriptor.value == CoreIncludes.kPassPlaceholder) + include.descriptor.value = subShaderInclude; + if (include.descriptor.value == CoreIncludes.kPostDecalsPlaceholder) + include.descriptor.value = postDecalsInclude; + if (include.descriptor.value == CoreIncludes.kRaytracingPlaceholder) + include.descriptor.value = raytracingInclude; + + if (!String.IsNullOrEmpty(include.descriptor.value)) + finalIncludes.Add(include.descriptor.value, include.descriptor.location, include.fieldConditions); + } + passDescriptor.includes = finalIncludes; + + // Add keywords from subshaders: + passDescriptor.keywords = passDescriptor.keywords == null ? new KeywordCollection() : new KeywordCollection{ passDescriptor.keywords }; // Duplicate keywords to avoid side effects (static list modification) + passDescriptor.defines = passDescriptor.defines == null ? new DefineCollection() : new DefineCollection{ passDescriptor.defines }; // Duplicate defines to avoid side effects (static list modification) + // foreach (var l in passDescriptor.keywords) + // if (l.descriptor.referenceName.Contains("DEBUG_DISPLAY")) + // Debug.Log(passDescriptor.lightMode); + CollectPassKeywords(ref passDescriptor); + + // Replace valid pixel blocks by automatic thing so we don't have to write them + var tmpCtx = new TargetActiveBlockContext(new List(), passDescriptor); + GetActiveBlocks(ref tmpCtx); + if (passDescriptor.validPixelBlocks == null) + passDescriptor.validPixelBlocks = tmpCtx.activeBlocks.Where(b => b.shaderStage == ShaderStage.Fragment).ToArray(); + if (passDescriptor.validVertexBlocks == null) + passDescriptor.validVertexBlocks = tmpCtx.activeBlocks.Where(b => b.shaderStage == ShaderStage.Vertex).ToArray(); + + // Set default values for HDRP "surface" passes: + if (passDescriptor.structs == null) + passDescriptor.structs = CoreStructCollections.Default; + if (passDescriptor.fieldDependencies == null) + passDescriptor.fieldDependencies = CoreFieldDependencies.Default; + + finalPasses.Add(passDescriptor, passes[i].fieldConditions); + } + + subShaderDescriptor.passes = finalPasses; + + return subShaderDescriptor; + } + + protected virtual void CollectPassKeywords(ref PassDescriptor pass) {} + public override void GetFields(ref TargetFieldContext context) { // Common properties between all HD master nodes diff --git a/com.unity.render-pipelines.high-definition/Editor/Material/ShaderGraph/HDTarget.cs b/com.unity.render-pipelines.high-definition/Editor/Material/ShaderGraph/HDTarget.cs index fc5746fa51c..901e617f35b 100644 --- a/com.unity.render-pipelines.high-definition/Editor/Material/ShaderGraph/HDTarget.cs +++ b/com.unity.render-pipelines.high-definition/Editor/Material/ShaderGraph/HDTarget.cs @@ -793,117 +793,13 @@ static class CorePragmas #region Keywords static class CoreKeywords { - public static KeywordCollection HDBaseNoCrossFade = new KeywordCollection - { - { CoreKeywordDescriptors.SurfaceTypeTransparent }, - { CoreKeywordDescriptors.BlendMode }, - { CoreKeywordDescriptors.DoubleSided, new FieldCondition(HDFields.Unlit, false) }, - { CoreKeywordDescriptors.DisableDecals, new FieldCondition(HDFields.Unlit, false) }, - { CoreKeywordDescriptors.DisableSSR, new FieldCondition(HDFields.Unlit, false) }, - { CoreKeywordDescriptors.DisableSSRTransparent, new FieldCondition(HDFields.Unlit, false) }, - // { CoreKeywordDescriptors.EnableGeometricSpecularAA, new FieldCondition(HDFields.Unlit, false) }, - { CoreKeywordDescriptors.BlendModePreserveSpecularLighting, new FieldCondition(HDFields.Unlit, false) }, - { CoreKeywordDescriptors.AddPrecomputedVelocity }, - { CoreKeywordDescriptors.TransparentWritesMotionVector }, - { CoreKeywordDescriptors.DepthOffset, new FieldCondition(HDFields.DepthOffset, true) }, - { CoreKeywordDescriptors.FogOnTransparent }, - { CoreKeywordDescriptors.AlphaTest, new FieldCondition(Fields.AlphaTest, true) }, - }; - - public static KeywordCollection HDBase = new KeywordCollection - { - { CoreKeywordDescriptors.LodFadeCrossfade, new FieldCondition(Fields.LodCrossFade, true) }, - { HDBaseNoCrossFade } - }; - - public static KeywordCollection Lightmaps = new KeywordCollection - { - { CoreKeywordDescriptors.Lightmap }, - { CoreKeywordDescriptors.DirectionalLightmapCombined }, - { CoreKeywordDescriptors.DynamicLightmap }, - }; - - public static KeywordCollection LightmapsRaytracing = new KeywordCollection - { - { CoreKeywordDescriptors.Lightmap }, - { CoreKeywordDescriptors.DirectionalLightmapCombined }, - }; - - public static KeywordCollection WriteMsaaDepth = new KeywordCollection - { - { CoreKeywordDescriptors.WriteMsaaDepth }, - }; - - public static KeywordCollection DebugDisplay = new KeywordCollection - { - { CoreKeywordDescriptors.DebugDisplay }, - }; - - public static KeywordCollection ForwardBase = new KeywordCollection - { - { HDBase }, - { CoreKeywordDescriptors.DebugDisplay }, - { Lightmaps }, - { CoreKeywordDescriptors.ShadowsShadowmask }, - { CoreKeywordDescriptors.Shadow }, - { CoreKeywordDescriptors.ScreenSpaceShadow }, - { CoreKeywordDescriptors.Decals }, - }; - - public static KeywordCollection Forward = new KeywordCollection - { - { ForwardBase }, - { CoreKeywordDescriptors.LightList }, - }; - - public static KeywordCollection BackThenFrontTransparent = new KeywordCollection - { - { ForwardBase }, - }; - - public static KeywordCollection RaytracingIndirect = new KeywordCollection - { - { HDBaseNoCrossFade }, - { CoreKeywordDescriptors.DebugDisplay }, - { LightmapsRaytracing }, - }; - - public static KeywordCollection RaytracingIndirectUnlit = new KeywordCollection - { - { HDBaseNoCrossFade }, - { CoreKeywordDescriptors.DebugDisplay }, - }; - - public static KeywordCollection RaytracingForward = new KeywordCollection - { - { HDBaseNoCrossFade }, - { CoreKeywordDescriptors.DebugDisplay }, - { LightmapsRaytracing }, - }; - - public static KeywordCollection RaytracingForwardUnlit = new KeywordCollection - { - { HDBaseNoCrossFade }, - { CoreKeywordDescriptors.DebugDisplay }, - }; - public static KeywordCollection RaytracingGBuffer = new KeywordCollection { - { HDBaseNoCrossFade }, - { CoreKeywordDescriptors.DebugDisplay }, - { LightmapsRaytracing }, { CoreKeywordDescriptors.RaytraceMinimalGBuffer }, }; - public static KeywordCollection RaytracingGBufferUnlit = new KeywordCollection - { - { HDBaseNoCrossFade }, - { CoreKeywordDescriptors.DebugDisplay }, - }; - public static KeywordCollection RaytracingVisiblity = new KeywordCollection { - { HDBaseNoCrossFade }, { CoreKeywordDescriptors.TransparentColorShadow }, }; @@ -949,7 +845,7 @@ static class CoreDefines { { CoreKeywordDescriptors.HasLightloop, 1 }, { RayTracingNode.GetRayTracingKeyword(), 0 }, - { CoreKeywordDescriptors.LightList, 1 }, // BackThenFront Transparent use #define USE_CLUSTERED_LIGHTLIST + // { CoreKeywordDescriptors.LightList, 1 }, // BackThenFront Transparent use #define USE_CLUSTERED_LIGHTLIST }; } #endregion @@ -1185,7 +1081,7 @@ static class CoreKeywordDescriptors displayName = "Has Lightloop", referenceName = "HAS_LIGHTLOOP", type = KeywordType.Boolean, - definition = KeywordDefinition.MultiCompile, + definition = KeywordDefinition.Predefined, scope = KeywordScope.Global, }; diff --git a/com.unity.render-pipelines.high-definition/Editor/Material/ShaderGraph/LightingSubTarget.cs b/com.unity.render-pipelines.high-definition/Editor/Material/ShaderGraph/LightingSubTarget.cs index 786a1545129..d14169bcde9 100644 --- a/com.unity.render-pipelines.high-definition/Editor/Material/ShaderGraph/LightingSubTarget.cs +++ b/com.unity.render-pipelines.high-definition/Editor/Material/ShaderGraph/LightingSubTarget.cs @@ -78,9 +78,6 @@ public override void GetFields(ref TargetFieldContext context) var descs = context.blocks.Select(x => x.descriptor); // Misc - context.AddField(SpecularAA, lightingData.specularAA && - context.pass.validPixelBlocks.Contains(HDBlockFields.SurfaceDescription.SpecularAAThreshold) && - context.pass.validPixelBlocks.Contains(HDBlockFields.SurfaceDescription.SpecularAAScreenSpaceVariance)); context.AddField(LightingGI, descs.Contains(HDBlockFields.SurfaceDescription.BakedGI) && context.pass.validPixelBlocks.Contains(HDBlockFields.SurfaceDescription.BakedGI)); context.AddField(BackLightingGI, descs.Contains(HDBlockFields.SurfaceDescription.BakedBackGI) && context.pass.validPixelBlocks.Contains(HDBlockFields.SurfaceDescription.BakedBackGI)); context.AddField(BentNormal, descs.Contains(HDBlockFields.SurfaceDescription.BentNormal) && context.connectedBlocks.Contains(HDBlockFields.SurfaceDescription.BentNormal) && context.pass.validPixelBlocks.Contains(HDBlockFields.SurfaceDescription.BentNormal)); @@ -96,6 +93,46 @@ public override void GetFields(ref TargetFieldContext context) context.AddField(DoubleSidedMirror, systemData.doubleSidedMode == DoubleSidedMode.MirroredNormals && context.pass.referenceName != "SHADERPASS_MOTION_VECTORS"); } + protected override void CollectPassKeywords(ref PassDescriptor pass) + { + base.CollectPassKeywords(ref pass); + + pass.keywords.Add(CoreKeywordDescriptors.DisableDecals); + pass.keywords.Add(CoreKeywordDescriptors.DisableSSR); + pass.keywords.Add(CoreKeywordDescriptors.DisableSSRTransparent); + pass.keywords.Add(CoreKeywordDescriptors.BlendModePreserveSpecularLighting); + // pass.keywords.Add(CoreKeywordDescriptors.EnableGeometricSpecularAA); + + if (pass.IsDepthOrMV()) + { + pass.keywords.Add(CoreKeywordDescriptors.WriteDecalBuffer); + } + + if (pass.IsLightingOrMaterial()) + { + pass.keywords.Add(CoreKeywordDescriptors.Lightmap); + pass.keywords.Add(CoreKeywordDescriptors.DirectionalLightmapCombined); + + if (!pass.IsDXR()) + { + pass.keywords.Add(CoreKeywordDescriptors.DynamicLightmap); + pass.keywords.Add(CoreKeywordDescriptors.ShadowsShadowmask); + pass.keywords.Add(CoreKeywordDescriptors.Decals); + } + } + + if (pass.IsForward()) + { + pass.keywords.Add(CoreKeywordDescriptors.Shadow); + pass.keywords.Add(CoreKeywordDescriptors.ScreenSpaceShadow); + + if (pass.lightMode == HDShaderPassNames.s_TransparentBackfaceStr) + pass.defines.Add(CoreKeywordDescriptors.LightList, 1); + else + pass.keywords.Add(CoreKeywordDescriptors.LightList); + } + } + public override void GetActiveBlocks(ref TargetActiveBlockContext context) { base.GetActiveBlocks(ref context); diff --git a/com.unity.render-pipelines.high-definition/Editor/Material/ShaderGraph/PassDescriptorExtension.cs b/com.unity.render-pipelines.high-definition/Editor/Material/ShaderGraph/PassDescriptorExtension.cs new file mode 100644 index 00000000000..490ef9a8f7d --- /dev/null +++ b/com.unity.render-pipelines.high-definition/Editor/Material/ShaderGraph/PassDescriptorExtension.cs @@ -0,0 +1,41 @@ +using System; +using UnityEngine; +using UnityEditor.ShaderGraph; +using UnityEditor.ShaderGraph.Internal; +using UnityEditor.Graphing; +using UnityEngine.Rendering.HighDefinition; + +namespace UnityEditor.Rendering.HighDefinition.ShaderGraph +{ + static class PassDescriptorExtension + { + public static bool IsDepthOrMV(this PassDescriptor pass) + { + return pass.lightMode == HDShaderPassNames.s_DepthForwardOnlyStr + || pass.lightMode == HDShaderPassNames.s_DepthOnlyStr + || pass.lightMode == HDShaderPassNames.s_MotionVectorsStr; + } + + public static bool IsLightingOrMaterial(this PassDescriptor pass) + { + return pass.IsForward() + || pass.lightMode == HDShaderPassNames.s_GBufferStr + // DXR passes without visibility, prepass or path tracing + || (pass.lightMode.Contains("DXR") && pass.lightMode != HDShaderPassNames.s_RayTracingVisibilityStr && pass.lightMode != HDShaderPassNames.s_PathTracingDXRStr); + } + + public static bool IsDXR(this PassDescriptor pass) + { + return pass.lightMode.Contains("DXR") + || pass.lightMode == HDShaderPassNames.s_RayTracingVisibilityStr + || pass.lightMode == HDShaderPassNames.s_RayTracingPrepassStr; + } + + public static bool IsForward(this PassDescriptor pass) + { + return pass.lightMode == HDShaderPassNames.s_ForwardOnlyStr + || pass.lightMode == HDShaderPassNames.s_ForwardStr + || pass.lightMode == HDShaderPassNames.s_TransparentBackfaceStr; + } + } +} diff --git a/com.unity.render-pipelines.high-definition/Editor/Material/ShaderGraph/PassDescriptorExtension.cs.meta b/com.unity.render-pipelines.high-definition/Editor/Material/ShaderGraph/PassDescriptorExtension.cs.meta new file mode 100644 index 00000000000..77aab03997d --- /dev/null +++ b/com.unity.render-pipelines.high-definition/Editor/Material/ShaderGraph/PassDescriptorExtension.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 55e83618b2c3f5a44a0c0f18727b2786 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/com.unity.render-pipelines.high-definition/Editor/Material/ShaderGraph/SurfaceSubTarget.cs b/com.unity.render-pipelines.high-definition/Editor/Material/ShaderGraph/SurfaceSubTarget.cs index 569f8e1acb2..d20a3fd2290 100644 --- a/com.unity.render-pipelines.high-definition/Editor/Material/ShaderGraph/SurfaceSubTarget.cs +++ b/com.unity.render-pipelines.high-definition/Editor/Material/ShaderGraph/SurfaceSubTarget.cs @@ -40,13 +40,7 @@ protected override string renderQueue protected virtual bool supportForward => false; protected virtual bool supportLighting => false; protected virtual bool supportDistortion => false; - protected virtual bool supportPathtracing => false; - protected virtual bool supportRaytracing => true; - - protected abstract string subShaderInclude { get; } - protected virtual string postDecalsInclude => null; - protected virtual string raytracingInclude => null; - protected abstract FieldDescriptor subShaderField { get; } + protected override bool supportRaytracing => true; public override void Setup(ref TargetSetupContext context) { @@ -134,66 +128,37 @@ PassCollection GetPasses() } } - SubShaderDescriptor PostProcessSubShader(SubShaderDescriptor subShaderDescriptor) + protected override void CollectPassKeywords(ref PassDescriptor pass) { - if (String.IsNullOrEmpty(subShaderDescriptor.pipelineTag)) - subShaderDescriptor.pipelineTag = HDRenderPipeline.k_ShaderTagName; - - var passes = subShaderDescriptor.passes.ToArray(); - PassCollection finalPasses = new PassCollection(); - for (int i = 0; i < passes.Length; i++) + pass.keywords.Add(CoreKeywordDescriptors.AlphaTest, new FieldCondition(Fields.AlphaTest, true)); + + if (pass.IsDepthOrMV()) { - var passDescriptor = passes[i].descriptor; - passDescriptor.passTemplatePath = templatePath; - passDescriptor.sharedTemplateDirectories = templateMaterialDirectories; - - // Add the subShader to enable fields that depends on it - var originalRequireFields = passDescriptor.requiredFields; - // Duplicate require fields to avoid unwanted shared list modification - passDescriptor.requiredFields = new FieldCollection(); - if (originalRequireFields != null) - foreach (var field in originalRequireFields) - passDescriptor.requiredFields.Add(field.field); - passDescriptor.requiredFields.Add(subShaderField); - - IncludeCollection finalIncludes = new IncludeCollection(); - var includeList = passDescriptor.includes.Select(include => include.descriptor).ToList(); - - // Replace include placeholders if necessary: - foreach (var include in passDescriptor.includes) - { - if (include.descriptor.value == CoreIncludes.kPassPlaceholder) - include.descriptor.value = subShaderInclude; - if (include.descriptor.value == CoreIncludes.kPostDecalsPlaceholder) - include.descriptor.value = postDecalsInclude; - if (include.descriptor.value == CoreIncludes.kRaytracingPlaceholder) - include.descriptor.value = raytracingInclude; - - if (!String.IsNullOrEmpty(include.descriptor.value)) - finalIncludes.Add(include.descriptor.value, include.descriptor.location, include.fieldConditions); - } - passDescriptor.includes = finalIncludes; - - // Replace valid pixel blocks by automatic thing so we don't have to write them - var tmpCtx = new TargetActiveBlockContext(new List(), passDescriptor); - GetActiveBlocks(ref tmpCtx); - if (passDescriptor.validPixelBlocks == null) - passDescriptor.validPixelBlocks = tmpCtx.activeBlocks.Where(b => b.shaderStage == ShaderStage.Fragment).ToArray(); - if (passDescriptor.validVertexBlocks == null) - passDescriptor.validVertexBlocks = CoreBlockMasks.Vertex; - - // Set default values for HDRP "surface" passes: - if (passDescriptor.structs == null) - passDescriptor.structs = CoreStructCollections.Default; - if (passDescriptor.fieldDependencies == null) - passDescriptor.fieldDependencies = CoreFieldDependencies.Default; - - finalPasses.Add(passDescriptor, passes[i].fieldConditions); + pass.keywords.Add(CoreKeywordDescriptors.AlphaToMask, new FieldCondition(Fields.AlphaToMask, true)); + pass.keywords.Add(CoreKeywordDescriptors.WriteMsaaDepth); } - subShaderDescriptor.passes = finalPasses; + pass.keywords.Add(CoreKeywordDescriptors.SurfaceTypeTransparent); + pass.keywords.Add(CoreKeywordDescriptors.BlendMode); + pass.keywords.Add(CoreKeywordDescriptors.DoubleSided, new FieldCondition(HDFields.Unlit, false)); + pass.keywords.Add(CoreKeywordDescriptors.DepthOffset, new FieldCondition(HDFields.DepthOffset, true)); + pass.keywords.Add(CoreKeywordDescriptors.AddPrecomputedVelocity); + pass.keywords.Add(CoreKeywordDescriptors.TransparentWritesMotionVector); + pass.keywords.Add(CoreKeywordDescriptors.FogOnTransparent); - return subShaderDescriptor; + if (pass.IsLightingOrMaterial()) + pass.keywords.Add(CoreKeywordDescriptors.DebugDisplay); + + if (!pass.IsDXR()) + pass.keywords.Add(CoreKeywordDescriptors.LodFadeCrossfade, new FieldCondition(Fields.LodCrossFade, true)); + + if (pass.lightMode == HDShaderPassNames.s_MotionVectorsStr) + { + if (supportForward) + pass.defines.Add(CoreKeywordDescriptors.WriteNormalBuffer, 1, new FieldCondition(HDFields.Unlit, false)); + else + pass.keywords.Add(CoreKeywordDescriptors.WriteNormalBuffer, new FieldCondition(HDFields.Unlit, false)); + } } public override void GetFields(ref TargetFieldContext context) diff --git a/com.unity.render-pipelines.high-definition/Editor/Material/Unlit/ShaderGraph/HDUnlitSubTarget.cs b/com.unity.render-pipelines.high-definition/Editor/Material/Unlit/ShaderGraph/HDUnlitSubTarget.cs index 219c575ab62..a97e74834f2 100644 --- a/com.unity.render-pipelines.high-definition/Editor/Material/Unlit/ShaderGraph/HDUnlitSubTarget.cs +++ b/com.unity.render-pipelines.high-definition/Editor/Material/Unlit/ShaderGraph/HDUnlitSubTarget.cs @@ -79,6 +79,14 @@ protected override SubShaderDescriptor GetSubShaderDescriptor() } } + protected override void CollectPassKeywords(ref PassDescriptor pass) + { + base.CollectPassKeywords(ref pass); + + if (pass.IsForward()) + pass.keywords.Add(CoreKeywordDescriptors.Shadow, new FieldCondition(HDUnlitSubTarget.EnableShadowMatte, true)); + } + public override void GetFields(ref TargetFieldContext context) { base.GetFields(ref context); diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDStringConstants.cs b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDStringConstants.cs index 5b067baaad6..c43119a1969 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDStringConstants.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDStringConstants.cs @@ -17,6 +17,8 @@ static class HDShaderPassNames public static readonly string s_TransparentBackfaceStr = "TransparentBackface"; public static readonly string s_TransparentDepthPostpassStr = "TransparentDepthPostpass"; public static readonly string s_RayTracingPrepassStr = "RayTracingPrepass"; + public static readonly string s_RayTracingVisibilityStr = "VisibilityDXR"; + public static readonly string s_PathTracingDXRStr = "PathTracingDXR"; public static readonly string s_MetaStr = "META"; public static readonly string s_ShadowCasterStr = "ShadowCaster"; public static readonly string s_MeshDecalsMStr = DecalSystem.s_MaterialDecalPassNames[(int)DecalSystem.MaterialDecalPass.DBufferMesh_M];