Skip to content

[Draft] Fix upgrade of Unlit and PBR cross pipeline SG to HD SG [Skip CI] #685

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jun 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
using System.Linq;
using System.Reflection;
using UnityEngine;
using UnityEditor.ShaderGraph;
using UnityEngine.Rendering.HighDefinition;
using UnityEditor.Rendering.HighDefinition.ShaderGraph;

// Material property names
using static UnityEngine.Rendering.HighDefinition.HDMaterialProperties;
Expand Down Expand Up @@ -129,7 +131,7 @@ static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAsse
AssetVersion assetVersion = null;
foreach (var subAsset in assetVersions)
{
if (subAsset.GetType() == typeof(AssetVersion))
if (subAsset != null && subAsset.GetType() == typeof(AssetVersion))
{
assetVersion = subAsset as AssetVersion;
break;
Expand Down Expand Up @@ -338,8 +340,58 @@ static void RenderQueueUpgrade(Material material, HDShaderUtils.ShaderID id)
HDShaderUtils.ResetMaterialKeywords(material);
}

// properties in this tab should be properties from Unlit or PBR cross pipeline shader
// that are suppose to be synchronize with the Material during upgrade
readonly static string[] s_ShadergraphStackFloatPropertiesToSynchronize = {
"_SurfaceType",
"_BlendMode",
"_DstBlend",
"_SrcBlend",
"_AlphaDstBlend",
"_AlphaSrcBlend",
"_AlphaCutoff",
"_AlphaCutoffEnable",
"_DoubleSidedEnable",
"_DoubleSidedNormalMode",
"_ZWrite", // Needed to fix older bug
"_RenderQueueType" // Needed as seems to not reset correctly
};

static void ShaderGraphStack(Material material, HDShaderUtils.ShaderID id)
=> HDShaderUtils.ResetMaterialKeywords(material);
{
Shader shader = material.shader;

if (shader.IsShaderGraph())
{
if (shader.TryGetMetadataOfType<HDMetadata>(out var obj))
{
// Material coming from old cross pipeline shader (Unlit and PBR) are not synchronize correctly with their
// shader graph. This code below ensure it is
if (obj.migrateFromOldCrossPipelineSG) // come from PBR or Unlit cross pipeline SG?
{
var defaultProperties = new Material(material.shader);

foreach (var floatToSync in s_ShadergraphStackFloatPropertiesToSynchronize)
if (material.HasProperty(floatToSync))
material.SetFloat(floatToSync, defaultProperties.GetFloat(floatToSync));

defaultProperties = null;

// Postprocess now that material is correctly sync
bool isTransparent = material.HasProperty("_SurfaceType") && material.GetFloat("_SurfaceType") > 0.0f;
bool alphaTest = material.HasProperty("_AlphaCutoffEnable") && material.GetFloat("_AlphaCutoffEnable") > 0.0f;

material.renderQueue = isTransparent ? (int)HDRenderQueue.Priority.Transparent :
alphaTest ? (int)HDRenderQueue.Priority.OpaqueAlphaTest : (int)HDRenderQueue.Priority.Opaque;

material.SetFloat("_RenderQueueType", isTransparent ? (float)HDRenderQueue.RenderQueueType.Transparent : (float)HDRenderQueue.RenderQueueType.Opaque);
}

}
}

HDShaderUtils.ResetMaterialKeywords(material);
}

#region Serialization_API
//Methods in this region interact on the serialized material
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,20 @@ public bool TryUpgradeFromMasterNode(IMasterNode1 masterNode, out Dictionary<Blo

void UpgradePBRMasterNode(PBRMasterNode1 pbrMasterNode, out Dictionary<BlockFieldDescriptor, int> blockMap)
{
m_MigrateFromOldCrossPipelineSG = true;

// Set data
systemData.surfaceType = (SurfaceType)pbrMasterNode.m_SurfaceType;
systemData.blendMode = HDSubShaderUtilities.UpgradeLegacyAlphaModeToBlendMode((int)pbrMasterNode.m_AlphaMode);
systemData.doubleSidedMode = pbrMasterNode.m_TwoSided ? DoubleSidedMode.Enabled : DoubleSidedMode.Disabled;
systemData.alphaTest = HDSubShaderUtilities.UpgradeLegacyAlphaClip(pbrMasterNode);
systemData.dotsInstancing = pbrMasterNode.m_DOTSInstancing;
systemData.dotsInstancing = false;
builtinData.addPrecomputedVelocity = false;
lightingData.blendPreserveSpecular = false;
lightingData.normalDropOffSpace = pbrMasterNode.m_NormalDropOffSpace;
lightingData.receiveDecals = false;
lightingData.receiveSSR = true;
lightingData.receiveSSRTransparent = false;
litData.materialType = pbrMasterNode.m_Model == PBRMasterNode1.Model.Specular ? HDLitData.MaterialType.SpecularColor : HDLitData.MaterialType.Standard;
litData.energyConservingSpecular = false;
target.customEditorGUI = pbrMasterNode.m_OverrideEnabled ? pbrMasterNode.m_ShaderGUIOverride : "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ abstract class HDSubTarget : SubTarget<HDTarget>, IHasMetadata,
IRequiresData<SystemData>
{
SystemData m_SystemData;
protected bool m_MigrateFromOldCrossPipelineSG; // Use only for the migration to shader stack architecture

// Interface Properties
SystemData IRequiresData<SystemData>.data
Expand Down Expand Up @@ -53,6 +54,7 @@ public virtual ScriptableObject GetMetadataObject()
{
var hdMetadata = ScriptableObject.CreateInstance<HDMetadata>();
hdMetadata.shaderID = shaderID;
hdMetadata.migrateFromOldCrossPipelineSG = m_MigrateFromOldCrossPipelineSG;
return hdMetadata;
}

Expand Down Expand Up @@ -107,4 +109,4 @@ public override object saveContext
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,16 @@ public bool TryUpgradeFromMasterNode(IMasterNode1 masterNode, out Dictionary<Blo

void UpgradeUnlitMasterNode(UnlitMasterNode1 unlitMasterNode, out Dictionary<BlockFieldDescriptor, int> blockMap)
{
m_MigrateFromOldCrossPipelineSG = true;

// Set data
systemData.surfaceType = (SurfaceType)unlitMasterNode.m_SurfaceType;
systemData.blendMode = HDSubShaderUtilities.UpgradeLegacyAlphaModeToBlendMode((int)unlitMasterNode.m_AlphaMode);
systemData.doubleSidedMode = unlitMasterNode.m_TwoSided ? DoubleSidedMode.Enabled : DoubleSidedMode.Disabled;
systemData.alphaTest = HDSubShaderUtilities.UpgradeLegacyAlphaClip(unlitMasterNode);
systemData.dotsInstancing = unlitMasterNode.m_DOTSInstancing;
systemData.dotsInstancing = false;
systemData.zWrite = false;
builtinData.addPrecomputedVelocity = unlitMasterNode.m_AddPrecomputedVelocity;
builtinData.addPrecomputedVelocity = false;
target.customEditorGUI = unlitMasterNode.m_OverrideEnabled ? unlitMasterNode.m_ShaderGUIOverride : "";

// Set blockmap
Expand Down Expand Up @@ -105,4 +107,4 @@ void UpgradeHDUnlitMasterNode(HDUnlitMasterNode1 hdUnlitMasterNode, out Dictiona
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using UnityEngine;
using UnityEditor.Rendering.HighDefinition;

Expand All @@ -10,10 +10,19 @@ sealed class HDMetadata : ScriptableObject
[SerializeField]
HDShaderUtils.ShaderID m_ShaderID;

[SerializeField]
bool m_MigrateFromOldCrossPipelineSG; // Keep track from which old SG master node we come from

public HDShaderUtils.ShaderID shaderID
{
get => m_ShaderID;
set => m_ShaderID = value;
}

public bool migrateFromOldCrossPipelineSG
{
get => m_MigrateFromOldCrossPipelineSG;
set => m_MigrateFromOldCrossPipelineSG = value;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ void UpgradeAlphaClip()
m_AlphaMode = (AlphaMode)unlitMasterNode.m_AlphaMode;
m_TwoSided = unlitMasterNode.m_TwoSided;
UpgradeAlphaClip();
m_AddPrecomputedVelocity = unlitMasterNode.m_AddPrecomputedVelocity;
m_AddPrecomputedVelocity = false;
m_CustomEditorGUI = unlitMasterNode.m_OverrideEnabled ? unlitMasterNode.m_ShaderGUIOverride : "";
break;
case SpriteLitMasterNode1 spriteLitMasterNode:
Expand Down
3 changes: 1 addition & 2 deletions com.unity.shadergraph/Editor/Data/Legacy/PBRMasterNode1.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using UnityEditor.Graphing;

Expand Down Expand Up @@ -32,7 +32,6 @@ public enum Model
public AlphaMode m_AlphaMode;
public bool m_TwoSided;
public NormalDropOffSpace m_NormalDropOffSpace;
public bool m_DOTSInstancing;
public string m_ShaderGUIOverride;
public bool m_OverrideEnabled;
}
Expand Down
4 changes: 1 addition & 3 deletions com.unity.shadergraph/Editor/Data/Legacy/UnlitMasterNode1.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using UnityEditor.Graphing;

Expand All @@ -24,8 +24,6 @@ public enum AlphaMode
public SurfaceType m_SurfaceType;
public AlphaMode m_AlphaMode;
public bool m_TwoSided;
public bool m_AddPrecomputedVelocity;
public bool m_DOTSInstancing;
public string m_ShaderGUIOverride;
public bool m_OverrideEnabled;
}
Expand Down