From 2858ef31f0b5d231f33e4fe8bf6a176f4f15b64b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antoine=20Leli=C3=A8vre?= Date: Wed, 8 Jul 2020 17:03:38 +0200 Subject: [PATCH 1/3] Migration code for depth pre/post pass and lod crossfade in shadergraph --- .../Eye/ShaderGraph/EyeSubTarget.Migration.cs | 6 ++-- .../ShaderGraph/FabricSubTarget.Migration.cs | 2 +- .../ShaderGraph/HairSubTarget.Migration.cs | 10 +++--- .../ShaderGraph/HDLitSubTarget.Migration.cs | 12 +++---- .../AdvancedOptionsPropertyBlock.cs | 2 +- .../Material/ShaderGraph/HDSubTarget.cs | 8 ++--- .../ShaderGraph/ShaderGraphVersion.cs | 1 + .../ShaderGraph/SurfaceOptionPropertyBlock.cs | 4 +-- .../Material/ShaderGraph/SurfaceSubTarget.cs | 35 +++++++++++++++---- .../ShaderGraph/TargetData/BuiltinData.cs | 24 +++++++++++++ .../ShaderGraph/TargetData/SystemData.cs | 29 ++++----------- .../StackLitSubTarget.Migration.cs | 2 +- 12 files changed, 81 insertions(+), 54 deletions(-) diff --git a/com.unity.render-pipelines.high-definition/Editor/Material/Eye/ShaderGraph/EyeSubTarget.Migration.cs b/com.unity.render-pipelines.high-definition/Editor/Material/Eye/ShaderGraph/EyeSubTarget.Migration.cs index 581652235c7..6d4ded6341a 100644 --- a/com.unity.render-pipelines.high-definition/Editor/Material/Eye/ShaderGraph/EyeSubTarget.Migration.cs +++ b/com.unity.render-pipelines.high-definition/Editor/Material/Eye/ShaderGraph/EyeSubTarget.Migration.cs @@ -27,17 +27,17 @@ public bool TryUpgradeFromMasterNode(IMasterNode1 masterNode, out Dictionary lightingData.specularOcclusionMode, (newValue) => lightingData.specularOcclusionMode = newValue); AddProperty(Styles.overrideBakedGI, () => lightingData.overrideBakedGI, (newValue) => lightingData.overrideBakedGI = newValue); } - AddProperty(Styles.supportLodCrossFade, () => systemData.supportLodCrossFade, (newValue) => systemData.supportLodCrossFade = newValue); + AddProperty(Styles.supportLodCrossFade, () => builtinData.supportLodCrossFade, (newValue) => builtinData.supportLodCrossFade = newValue); AddProperty(addPrecomputedVelocityText, () => builtinData.addPrecomputedVelocity, (newValue) => builtinData.addPrecomputedVelocity = newValue); } } 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 b16eea5f1ca..a2c53f27e9b 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 @@ -33,12 +33,7 @@ public SystemData systemData set => m_SystemData = value; } - protected virtual int ComputeMaterialNeedsUpdateHash() - { - // Alpha test is currently the only property in system data to trigger the material upgrade script. - int hash = systemData.alphaTest.GetHashCode(); - return hash; - } + protected virtual int ComputeMaterialNeedsUpdateHash() => 0; public override bool IsActive() => true; @@ -94,6 +89,7 @@ public override void Setup(ref TargetSetupContext context) context.AddAssetDependencyPath(AssetDatabase.GUIDToAssetPath(subTargetAssetGuid)); context.SetDefaultShaderGUI(customInspector); + Debug.Log("Migration step: " + systemData.version); if (migrationSteps.Migrate(this)) OnBeforeSerialize(); diff --git a/com.unity.render-pipelines.high-definition/Editor/Material/ShaderGraph/ShaderGraphVersion.cs b/com.unity.render-pipelines.high-definition/Editor/Material/ShaderGraph/ShaderGraphVersion.cs index 1864ea0f241..27c489b382f 100644 --- a/com.unity.render-pipelines.high-definition/Editor/Material/ShaderGraph/ShaderGraphVersion.cs +++ b/com.unity.render-pipelines.high-definition/Editor/Material/ShaderGraph/ShaderGraphVersion.cs @@ -9,5 +9,6 @@ namespace UnityEditor.Rendering.HighDefinition.ShaderGraph public enum ShaderGraphVersion { Initial = 0, + MoveCrossFadePrePostPassAndAlphaClipToBuiltinData = 1, } } diff --git a/com.unity.render-pipelines.high-definition/Editor/Material/ShaderGraph/SurfaceOptionPropertyBlock.cs b/com.unity.render-pipelines.high-definition/Editor/Material/ShaderGraph/SurfaceOptionPropertyBlock.cs index bde3e02903f..2819b8a2b79 100644 --- a/com.unity.render-pipelines.high-definition/Editor/Material/ShaderGraph/SurfaceOptionPropertyBlock.cs +++ b/com.unity.render-pipelines.high-definition/Editor/Material/ShaderGraph/SurfaceOptionPropertyBlock.cs @@ -65,8 +65,8 @@ protected override void CreatePropertyGUI() AddProperty(transparentCullModeText, () => systemData.transparentCullMode, (newValue) => systemData.transparentCullMode = newValue); AddProperty(transparentSortPriorityText, () => systemData.sortPriority, (newValue) => systemData.sortPriority = HDRenderQueue.ClampsTransparentRangePriority(newValue)); AddProperty(transparentBackfaceEnableText, () => builtinData.backThenFrontRendering, (newValue) => builtinData.backThenFrontRendering = newValue); - AddProperty(transparentDepthPrepassEnableText, () => systemData.transparentDepthPrepass, (newValue) => systemData.transparentDepthPrepass = newValue); - AddProperty(transparentDepthPostpassEnableText, () => systemData.transparentDepthPostpass, (newValue) => systemData.transparentDepthPostpass = newValue); + AddProperty(transparentDepthPrepassEnableText, () => builtinData.transparentDepthPrepass, (newValue) => builtinData.transparentDepthPrepass = newValue); + AddProperty(transparentDepthPostpassEnableText, () => builtinData.transparentDepthPostpass, (newValue) => builtinData.transparentDepthPostpass = newValue); AddProperty(transparentWritingMotionVecText, () => builtinData.transparentWritesMotionVec, (newValue) => builtinData.transparentWritesMotionVec = newValue); if (lightingData != null) 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 d20a3fd2290..1ba6eeb3fee 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 @@ -42,6 +42,13 @@ protected override string renderQueue protected virtual bool supportDistortion => false; protected override bool supportRaytracing => true; + protected override int ComputeMaterialNeedsUpdateHash() + { + // Alpha test is currently the only property in buitin data to trigger the material upgrade script. + int hash = systemData.alphaTest.GetHashCode(); + return hash; + } + public override void Setup(ref TargetSetupContext context) { context.AddAssetDependencyPath(AssetDatabase.GUIDToAssetPath("f4df7e8f9b8c23648ae50cbca0221e47")); // SurfaceSubTarget.cs @@ -194,15 +201,15 @@ public override void GetFields(ref TargetFieldContext context) context.AddField(HDFields.DoAlphaTestShadow, systemData.alphaTest && builtinData.alphaTestShadow && isShadowPass && context.pass.validPixelBlocks.Contains(HDBlockFields.SurfaceDescription.AlphaClipThresholdShadow)); // Pre/post pass always use the specific alpha test provided for those pass - context.AddField(HDFields.DoAlphaTestPrepass, systemData.alphaTest && systemData.transparentDepthPrepass && isTransparentDepthPrepass && + context.AddField(HDFields.DoAlphaTestPrepass, systemData.alphaTest && builtinData.transparentDepthPrepass && isTransparentDepthPrepass && context.pass.validPixelBlocks.Contains(HDBlockFields.SurfaceDescription.AlphaClipThresholdDepthPrepass)); // Features & Misc - context.AddField(Fields.LodCrossFade, systemData.supportLodCrossFade); + context.AddField(Fields.LodCrossFade, builtinData.supportLodCrossFade); context.AddField(Fields.AlphaToMask, systemData.alphaTest); context.AddField(HDFields.TransparentBackFace, builtinData.backThenFrontRendering); - context.AddField(HDFields.TransparentDepthPrePass, systemData.transparentDepthPrepass); - context.AddField(HDFields.TransparentDepthPostPass, systemData.transparentDepthPostpass); + context.AddField(HDFields.TransparentDepthPrePass, builtinData.transparentDepthPrepass); + context.AddField(HDFields.TransparentDepthPostPass, builtinData.transparentDepthPostpass); context.AddField(HDFields.DepthOffset, builtinData.depthOffset && context.pass.validPixelBlocks.Contains(HDBlockFields.SurfaceDescription.DepthOffset)); @@ -239,8 +246,8 @@ public override void GetActiveBlocks(ref TargetActiveBlockContext context) context.AddBlock(BlockFields.SurfaceDescription.AlphaClipThreshold, systemData.alphaTest); // Alpha Test - context.AddBlock(HDBlockFields.SurfaceDescription.AlphaClipThresholdDepthPrepass, systemData.alphaTest && systemData.transparentDepthPrepass); - context.AddBlock(HDBlockFields.SurfaceDescription.AlphaClipThresholdDepthPostpass, systemData.alphaTest && systemData.transparentDepthPostpass); + context.AddBlock(HDBlockFields.SurfaceDescription.AlphaClipThresholdDepthPrepass, systemData.alphaTest && builtinData.transparentDepthPrepass); + context.AddBlock(HDBlockFields.SurfaceDescription.AlphaClipThresholdDepthPostpass, systemData.alphaTest && builtinData.transparentDepthPostpass); context.AddBlock(HDBlockFields.SurfaceDescription.AlphaClipThresholdShadow, systemData.alphaTest && builtinData.alphaTestShadow); // Misc @@ -306,7 +313,7 @@ public override void CollectShaderProperties(PropertyCollector collector, Genera // Common properties for all "surface" master nodes HDSubShaderUtilities.AddAlphaCutoffShaderProperties(collector, systemData.alphaTest, builtinData.alphaTestShadow); HDSubShaderUtilities.AddDoubleSidedProperty(collector, systemData.doubleSidedMode); - HDSubShaderUtilities.AddPrePostPassProperties(collector, systemData.transparentDepthPrepass, systemData.transparentDepthPostpass); + HDSubShaderUtilities.AddPrePostPassProperties(collector, builtinData.transparentDepthPrepass, builtinData.transparentDepthPostpass); // Add all shader properties required by the inspector HDSubShaderUtilities.AddBlendingStatesShaderProperties( @@ -343,5 +350,19 @@ public override void ProcessPreviewMaterial(Material material) LightingShaderGraphGUI.SetupMaterialKeywordsAndPass(material); } + + internal override void MigrateTo(ShaderGraphVersion version) + { + base.MigrateTo(version); + + if (version == ShaderGraphVersion.MoveCrossFadePrePostPassAndAlphaClipToBuiltinData) + { +#pragma warning disable 618 + builtinData.transparentDepthPrepass = systemData.m_TransparentDepthPrepass; + builtinData.transparentDepthPostpass = systemData.m_TransparentDepthPostpass; + builtinData.supportLodCrossFade = systemData.m_SupportLodCrossFade; +#pragma warning restore 618 + } + } } } diff --git a/com.unity.render-pipelines.high-definition/Editor/Material/ShaderGraph/TargetData/BuiltinData.cs b/com.unity.render-pipelines.high-definition/Editor/Material/ShaderGraph/TargetData/BuiltinData.cs index 9631c205651..d6e530607bd 100644 --- a/com.unity.render-pipelines.high-definition/Editor/Material/ShaderGraph/TargetData/BuiltinData.cs +++ b/com.unity.render-pipelines.high-definition/Editor/Material/ShaderGraph/TargetData/BuiltinData.cs @@ -85,5 +85,29 @@ public bool backThenFrontRendering get => m_BackThenFrontRendering; set => m_BackThenFrontRendering = value; } + + [SerializeField] + bool m_TransparentDepthPrepass; + public bool transparentDepthPrepass + { + get => m_TransparentDepthPrepass; + set => m_TransparentDepthPrepass = value; + } + + [SerializeField] + bool m_TransparentDepthPostpass; + public bool transparentDepthPostpass + { + get => m_TransparentDepthPostpass; + set => m_TransparentDepthPostpass = value; + } + + [SerializeField] + bool m_SupportLodCrossFade; + public bool supportLodCrossFade + { + get => m_SupportLodCrossFade; + set => m_SupportLodCrossFade = value; + } } } diff --git a/com.unity.render-pipelines.high-definition/Editor/Material/ShaderGraph/TargetData/SystemData.cs b/com.unity.render-pipelines.high-definition/Editor/Material/ShaderGraph/TargetData/SystemData.cs index 54d45df996c..304dfa3eeb9 100644 --- a/com.unity.render-pipelines.high-definition/Editor/Material/ShaderGraph/TargetData/SystemData.cs +++ b/com.unity.render-pipelines.high-definition/Editor/Material/ShaderGraph/TargetData/SystemData.cs @@ -90,21 +90,14 @@ public bool alphaTest set => m_AlphaTest = value; } - [SerializeField] - bool m_TransparentDepthPrepass; - public bool transparentDepthPrepass - { - get => m_TransparentDepthPrepass; - set => m_TransparentDepthPrepass = value; - } + [SerializeField, Obsolete("Keep for migration")] + internal bool m_TransparentDepthPrepass; - [SerializeField] - bool m_TransparentDepthPostpass; - public bool transparentDepthPostpass - { - get => m_TransparentDepthPostpass; - set => m_TransparentDepthPostpass = value; - } + [SerializeField, Obsolete("Keep for migration")] + internal bool m_TransparentDepthPostpass; + + [SerializeField, Obsolete("Keep for migration")] + internal bool m_SupportLodCrossFade; [SerializeField] DoubleSidedMode m_DoubleSidedMode; @@ -114,14 +107,6 @@ public DoubleSidedMode doubleSidedMode set => m_DoubleSidedMode = value; } - [SerializeField] - bool m_SupportLodCrossFade; - public bool supportLodCrossFade - { - get => m_SupportLodCrossFade; - set => m_SupportLodCrossFade = value; - } - // TODO: This was on HDUnlitMaster but not used anywhere // TODO: On HDLit it adds the field `HDFields.DotsInstancing` // TODO: Should this be added properly to HDUnlit? diff --git a/com.unity.render-pipelines.high-definition/Editor/Material/StackLit/ShaderGraph/StackLitSubTarget.Migration.cs b/com.unity.render-pipelines.high-definition/Editor/Material/StackLit/ShaderGraph/StackLitSubTarget.Migration.cs index e3beab194e2..cc644d73c9f 100644 --- a/com.unity.render-pipelines.high-definition/Editor/Material/StackLit/ShaderGraph/StackLitSubTarget.Migration.cs +++ b/com.unity.render-pipelines.high-definition/Editor/Material/StackLit/ShaderGraph/StackLitSubTarget.Migration.cs @@ -32,10 +32,10 @@ public bool TryUpgradeFromMasterNode(IMasterNode1 masterNode, out Dictionary Date: Wed, 8 Jul 2020 17:23:11 +0200 Subject: [PATCH 2/3] Removed log --- .../Editor/Material/ShaderGraph/HDSubTarget.cs | 1 - 1 file changed, 1 deletion(-) 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 a2c53f27e9b..c6cb9657b05 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 @@ -89,7 +89,6 @@ public override void Setup(ref TargetSetupContext context) context.AddAssetDependencyPath(AssetDatabase.GUIDToAssetPath(subTargetAssetGuid)); context.SetDefaultShaderGUI(customInspector); - Debug.Log("Migration step: " + systemData.version); if (migrationSteps.Migrate(this)) OnBeforeSerialize(); From 1b8ffd9206d61af949a25dfda5a7df1913ba5997 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antoine=20Leli=C3=A8vre?= Date: Thu, 9 Jul 2020 11:35:47 +0200 Subject: [PATCH 3/3] Added "first time" migration system --- .../Editor/Material/ShaderGraph/HDSubTarget.cs | 9 +++++++++ .../Editor/Material/ShaderGraph/ShaderGraphVersion.cs | 2 +- .../Editor/Material/ShaderGraph/SurfaceSubTarget.cs | 2 +- .../Editor/Material/ShaderGraph/TargetData/SystemData.cs | 8 ++++++++ 4 files changed, 19 insertions(+), 2 deletions(-) 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 c6cb9657b05..3664233f5f1 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 @@ -92,6 +92,15 @@ public override void Setup(ref TargetSetupContext context) if (migrationSteps.Migrate(this)) OnBeforeSerialize(); + // Migration hack to have the case where SG doesn't have version yet but is already upgraded to the stack system + if (!systemData.firstTimeMigrationExecuted) + { + // Force the initial migration step + MigrateTo(ShaderGraphVersion.FirstTimeMigration); + systemData.firstTimeMigrationExecuted = true; + OnBeforeSerialize(); + } + foreach (var subShader in EnumerateSubShaders()) { // patch render type and render queue from pass declaration: diff --git a/com.unity.render-pipelines.high-definition/Editor/Material/ShaderGraph/ShaderGraphVersion.cs b/com.unity.render-pipelines.high-definition/Editor/Material/ShaderGraph/ShaderGraphVersion.cs index 27c489b382f..35e55f3d8d6 100644 --- a/com.unity.render-pipelines.high-definition/Editor/Material/ShaderGraph/ShaderGraphVersion.cs +++ b/com.unity.render-pipelines.high-definition/Editor/Material/ShaderGraph/ShaderGraphVersion.cs @@ -9,6 +9,6 @@ namespace UnityEditor.Rendering.HighDefinition.ShaderGraph public enum ShaderGraphVersion { Initial = 0, - MoveCrossFadePrePostPassAndAlphaClipToBuiltinData = 1, + FirstTimeMigration = Initial, } } 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 1ba6eeb3fee..5aa154d37d2 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 @@ -355,7 +355,7 @@ internal override void MigrateTo(ShaderGraphVersion version) { base.MigrateTo(version); - if (version == ShaderGraphVersion.MoveCrossFadePrePostPassAndAlphaClipToBuiltinData) + if (version == ShaderGraphVersion.FirstTimeMigration) { #pragma warning disable 618 builtinData.transparentDepthPrepass = systemData.m_TransparentDepthPrepass; diff --git a/com.unity.render-pipelines.high-definition/Editor/Material/ShaderGraph/TargetData/SystemData.cs b/com.unity.render-pipelines.high-definition/Editor/Material/ShaderGraph/TargetData/SystemData.cs index 304dfa3eeb9..2e1a29fa305 100644 --- a/com.unity.render-pipelines.high-definition/Editor/Material/ShaderGraph/TargetData/SystemData.cs +++ b/com.unity.render-pipelines.high-definition/Editor/Material/ShaderGraph/TargetData/SystemData.cs @@ -126,6 +126,14 @@ public ShaderGraphVersion version set => m_Version = value; } + [SerializeField] + bool m_FirstTimeMigrationExecuted = false; + public bool firstTimeMigrationExecuted + { + get => m_FirstTimeMigrationExecuted; + set => m_FirstTimeMigrationExecuted = value; + } + internal int inspectorFoldoutMask; }