Skip to content

[2021.2] [ShaderGraph] URP Uber Shader #3705

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 74 commits into from
Apr 29, 2021
Merged

[2021.2] [ShaderGraph] URP Uber Shader #3705

merged 74 commits into from
Apr 29, 2021

Conversation

cdxntchou
Copy link

@cdxntchou cdxntchou commented Mar 1, 2021

Purpose of this PR

This PR adds an "Allow Material Override" checkbox to Lit and Unlit subtargets, as well as controls for Cast and Receive Shadows, Depth Write and Depth Test.
imageimage

By default, "Allow Material Override" will not be checked, both to preserve the behavior of existing shadergraphs, and to simplify the initial user experience. When checked, it allows the Material to control the following surface options (that were previously only controlled from the ShaderGraph):

For Lit subtargets:

  • Workflow Mode
  • Surface Type
  • Render Face
  • Depth Write
  • Depth Test
  • Alpha Clipping
  • Cast Shadows
  • Receive Shadows

For Unlit subtargets:

  • Surface Type
  • Render Face
  • Depth Write
  • Depth Test
  • Alpha Clipping
  • Cast Shadows

Example Unlit Material, when"Allow Material Override" is checked on the ShaderGraph:
image

It achieves this by modifying the subtargets to generate properties and keywords to control the surface options, in combination with a setup function that applies state updates to the Materials to control these properties and keywords: ShaderUtils.UpdateMaterial(material). To be able to run the correct update on each material, we created a ShaderID enum to identify the various Universal shader types, extending the existing ShaderPathID to include shadergraph generated shaders. We also attach metadata to Universal ShaderGraph assets, so we can tag them with their ShaderID. The helper function ShaderUtils.GetShaderID() is provided to get the ShaderID for any shader, either hand-written or shadergraph generated.

This PR also creates two new Material Inspectors: ShaderGraphLitGUI (for Lit subtarget), and ShaderGraphUnlitGUI (For Unlit subtarget) to handle displaying the surface controls appropriately, and calling the update function when needed.

These inspectors are based off of the BaseShaderGUI base class, so some changes are made to BaseShaderGUI so that it works with ShaderGraph shaders in addition to the existing hand-written shaders.

This PR also makes a few associated changes to ShaderGraph:

  1. Adds "ShaderGraphShader"="True" tag to all ShaderGraph-built subshaders, allowing us to easily tell when a Material is a ShaderGraph (even if the ShaderGraph is not asset based). Added "Material.IsShaderGraph" utility function, and changed the old "Shader.IsShaderGraph" function to "Shader.IsShaderGraphAsset" to make it clear it only works on Assets.

  2. Adds "Stages" dropdown to Keywords, allowing user to set up "Fragment" or "Vertex" only keywords to reduce compiled variants.
    image

  3. Adds linearGrey and red options as default texture mode, and changed the "bump" option to be called "Normal Map"
    image

  4. Texture2D properties using Normal Map (old "bump") default will now be marked with [NormalMap] tag, so that users are reminded to assign a normal map usage texture to that property.

  5. Adds CastsShadows option (for Materials that use Lit/Unlit SubTargets)

  6. Adds Depth Test control (for Materials that use Lit/Unlit SubTargets)

  7. Made the SubTarget and Material controls look identical (same naming, same options available)
    image
    image


Testing status

Describe what manual/automated tests were performed for this PR

Tested:

  • Tested that assigning a new Shader to a Material will reset keywords properly
  • Tested that newly created Materials have their keywords set up properly even if the inspector is not visible or run.
  • Test bump maps / normal maps work with the new subtargets
  • Tested combinations of different Target settings, to see that they modify the default properties in the generated shader, and will populate newly created Materials with those defaults.
  • Tested all cull mode settings (front/back/both) on Material
  • Tested setting Opaque/Transparent, and all blend modes on Material
  • Tested enabling/disabling alpha clipping on Material
  • Tested enabling/disabling receive shadows on Material
  • Tested enabling/disabling cast shadows on Material
  • Tested Depth Write and Depth Test settings on Material
  • Verified that keyword options all generate the correct #pragma in HLSL
  • Tested that exposed keyword options are controllable in the new Material Inspector
  • Tested that keyword stages option UI works with undo/redo, and is serialized/deserialized properly
  • Verified that existing KeywordDescriptors from Targets are generated in code correctly
  • Tested pre-existing URP graphs upgrade to an equivalent behavior with this PR -- tested alpha clipping, workflow mode, surface type, render face
  • Validate that old (pre-stack) graphs upgrade two sided setting correctly from PBRMaster and UnlitMaster
  • Verified that shadergraph master previews show up correctly with URP Lit/Unlit subtarget, and selecting various combinations of default surface settings on them
  • Test we didn't break URP sprite subtargets
  • Test that material inspectors for old Lit.shader, Unlit.shader, and any other variants still work properly.
  • Tested that pre-existing Universal Lit ShaderGraph Materials that are using Baked GI continue to work after upgrading to this PR. The GI flag is preserved and the baked results are identical (both with GI disabled and GI enabled):
    image
  • Tested adding keywords, modifying them from the Material, check they are preserved
  • Tested use of Clear Coat -- upgrades correctly, Target only control, renders properly
  • Previews work in both Material Override and non override mode, identical results
  • Tested that modifying ShaderGraph updates the keywords on the Materials using that ShaderGraph
  • Tested that Material versioning can process ShaderGraph Materials now
  • Tested that modifying Materials via inspector will update keywords
  • Tested that Target and Material Inspector surface controls look and behave consistently
  • Tested that ShaderGraph blocks (Alpha, Alpha Threshold, Specular, Metallic) are added or removed correctly based on the active options in the Target.
  • Tested that shadow culling matches the setting of alpha clip, render face settings.
  • Tested with both URP and HDRP Targets active

Yamato:

ShaderGraph PR Job: 🟢
https://yamato.cds.internal.unity3d.com/jobs/902-Graphics/tree/sg%252Furp-uber/.yamato%252Fall-shadergraph.yml%2523PR_ShaderGraph_trunk/6559408/job/pipeline

Universal PR Job: matches equivalent master results 🟢
https://yamato.cds.internal.unity3d.com/jobs/902-Graphics/tree/sg%252Furp-uber/.yamato%252Fall-universal_split.yml%2523PR_Universal_Split_trunk/6560960/job/pipeline

failing jobs:
URP_PostPro on Win_DX11_Standalone_XR_cache_mono_Linear on version trunk -- same 24/55 tests failed.
URP_PostPro on Win_DX11_playmode_XR_cache_mono_Linear on version trunk -- same 24/55 tests failed.
URP_PostPro on Win_DX12_Standalone_XR_cache_mono_Linear on version trunk -- same 24/55 tests failed.
URP_PostPro on OSX_Metal_playmode_cache_mono_Linear on version trunk -- success on re-run
URP_Lighting on Win_DX11_Standalone_XR_cache_mono_Linear on version trunk -- same 28/75 tests failed.
URP_Lighting on Win_DX11_playmode_XR_cache_mono_Linear on version trunk -- same 28/94 tests failed.
URP_Lighting on Win_DX12_Standalone_XR_cache_mono_Linear on version trunk -- same 28/75 tests failed.
URP_Foundation on Win_DX11_Standalone_cache_mono_Linear on version trunk -- success on re-run
URP_Foundation on Win_DX11_Standalone_XR_cache_mono_Linear on version trunk -- same 60/94 tests failed.
URP_Foundation on Win_DX11_playmode_XR_cache_mono_Linear on version trunk -- same 60/113 tests failed.
URP_Foundation on Win_DX12_Standalone_XR_cache_mono_Linear on version trunk -- same 60/94 tests failed.
URP_Foundation on OSX_Metal_playmode_cache_mono_Linear on version trunk -- success on re-run
URP_Terrain on Win_DX11_playmode_XR_cache_mono_Linear on version trunk -- same 8/68 tests failed.
URP_2D on Win_DX11_playmode_XR_cache_mono_Linear on version trunk -- same 18/74 tests failed.
URP_2D on OSX_OpenGLCore_editmode_mono_Linear on version trunk -- same 1/63 tests failed.
URP_2D on Win_DX11_editmode_mono_Linear on version trunk -- same 1/63 tests failed.

equivalent master version: ed96290

master failing job;
https://yamato.cds.internal.unity3d.com/jobs/902-Graphics/tree/master/.yamato%252Fall-universal_split.yml%2523Nightly_Universal_Split_trunk/6518789/job/pipeline

HDRP PR Job: 🟢
https://yamato.cds.internal.unity3d.com/jobs/902-Graphics/tree/sg%252Furp-uber/.yamato%252Fall-hdrp.yml%2523PR_HDRP_trunk/6433493/job/pipeline

latest after merge: lots of failures, but matches trunk 🟢
https://yamato.cds.internal.unity3d.com/jobs/902-Graphics/tree/sg%252Furp-uber/.yamato%252Fall-hdrp.yml%2523PR_HDRP_trunk/6567033/job/pipeline

rerunning:
Build HDRP on Win_DX11_mono_Linear_Standalone_cache_build_Player on version trunk
HDRP on Win_DX11_editmode_mono_Linear on version trunk


Comments to reviewers

Notes for the reviewers you have assigned.

Discovered Issues for emulating Lit.Shader / Unlit.Shader,
These won't necessarily be fixed in this PR, I'm just noting the issue for future work on the Lit.shadergraph:

  • no linearGrey default color option (this PR)
  • no MainTexture / MainColor tags (separate PR)
  • no ToggleOff tag
  • no Normal tag (this PR)
  • no HiddenInInspector tag exposed on blackboard properties (only internal in code) -- can hack serialized file
  • no Texture Scale and Offset support (separate PR)
  • keyword "stage" not exposed on blackboard keywords -- added drop down (this PR)
  • ShaderGraph Unlit subtarget casts shadows by default, Unlit.Shader does NOT, will need to make sure to flag Unlit.ShaderGraph as non-shadowcasting by default to maintain behavior.
  • ShaderGraph generated meta passes (for Global Illumination) do not do the same diffuse/specular color blend that Lit.shader does

Issues Found, that are Not Fixed / by design for now:

  • Transparent Multiply mode ignores alpha (pre-existing issue -- may be intended?)
  • Transparent Premultiply mode -- specular ignores alpha (pre-existing issue -- this is intended)
  • culling option currently also controls culling in shadow passes. May want independent shadow pass culling control like HDRP?
  • DepthOnly needs to be stripped when ZWrite is force disabled
  • should we strip DepthNormalOnly passes when ZWrite is force disabled?
  • Baked GI for alpha-clipped shadergraphs is not correct -- will ignore the alpha clipping. Seems to be a limitation of the meta-pass approach to gathering GI values?
  • meta pass GI also doesn't seem to take into account differing values for the front and back sides of the object -- even when double-sided GI is enabled on the Material.
  • Switching URP ==> HDRP does not currently update material keywords (until you open the material inspector)
  • Swtiching HDRP ==> URP does not currently update material keywords (until you open the material inspector)

Chris Tchou added 24 commits March 1, 2021 12:19
…rGUI material inspector, cleaning up keyword code generation
# Conflicts:
#	com.unity.render-pipelines.universal/Editor/ShaderGUI/BaseShaderGUI.cs
#	com.unity.render-pipelines.universal/Editor/ShaderGUI/ShadingModels/LitGUI.cs
#	com.unity.render-pipelines.universal/Editor/ShaderGraph/Targets/UniversalLitSubTarget.cs
#	com.unity.shadergraph/Editor/Generation/Processors/Generator.cs
…lling PreviewMaterial setup on URP Lit shadergraphs (fixup code not designed to work on shadergraphs)
@cdxntchou cdxntchou requested a review from phi-lira April 22, 2021 16:50
@cdxntchou
Copy link
Author

@phi-lira @eh-unity @Verasl @LandonTownsendUnity This PR is ready to go from my standpoint, all yamato tests passing, all of the feedback addressed: the default behavior is now identical to the old behavior (Target control), and the property and keyword names match Lit.shader / Unlit.shader. Please check if there is anything else that should be changed!

I've got tests being added in a separate branch: sg/urp-uber-tests (all green so far there as well), but would like to get this large change in to unblock dependent work and avoid continuous master merge fixups and re-running yamato tests..

@cinight cinight self-requested a review April 26, 2021 12:08
Copy link
Contributor

@phi-lira phi-lira left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Minor comments, missing public API docs.
Also, PBRMasterGUI.cs file was deleted but I didn't see any .meta file being deleted for that?

Comment on lines +423 to +433
if (isShaderGraph)
{
// Lit.shadergraph or Unlit.shadergraph, but no material control defined
// enable the pass in the material, so shader can decide...
castShadows = true;
}
else
{
// Lit.shader or Unlit.shader -- set based on transparency
castShadows = Rendering.Universal.ShaderGUI.LitGUI.IsOpaque(material);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for handling this. I'll have a discussion with @Verasl I think URP team should align with ShaderGraph (we can do in follow up PR)

Copy link
Contributor

@eh-unity eh-unity left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks pretty good to me.


// when the surface options are material controlled, we must show all of these blocks
// when target controlled, we can cull the unnecessary blocks
context.AddBlock(BlockFields.SurfaceDescription.Specular, (workflowMode == WorkflowMode.Specular) || target.allowMaterialOverride);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since there are these clear sections for different "kinds" of blocks, perhaps we should separate them into internal helper functions?

if (target.mayWriteDepth)
result.passes.Add(CorePasses.DepthOnly(target));

result.passes.Add(LitPasses.DepthNormalOnly(target));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, I added method chaining/fluent interface support to the shadergraph collections a while back. You could write these as result.passes.Add().Add().Add()...
I think this is fine as is not necessary to change.

Copy link
Contributor

@erikabar erikabar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cinight is on this PR and will test it, I am approving without testing.

Chris Tchou added 5 commits April 27, 2021 11:00
# Conflicts:
#	com.unity.render-pipelines.universal/CHANGELOG.md
#	com.unity.render-pipelines.universal/Editor/ShaderGraph/Targets/UniversalTarget.cs
#	com.unity.render-pipelines.universal/Editor/ShaderGraph/Targets/UniversalUnlitSubTarget.cs
#	com.unity.shadergraph/CHANGELOG.md
Copy link

@ghost ghost left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Developer manual testing is sufficient

Copy link

@ghost ghost left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That render queue override we saw during the pair testing doesn't exist on master - it looks like this PR caused that.
image

@ghost ghost self-requested a review April 28, 2021 17:28
Copy link

@ghost ghost left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving now that the advanced controls are fixed.

@cdxntchou cdxntchou requested a review from marctem April 29, 2021 05:24
Copy link
Contributor

@cinight cinight left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@marctem marctem merged commit 973e3ef into master Apr 29, 2021
@marctem marctem deleted the sg/urp-uber branch April 29, 2021 18:34
@jessebarker jessebarker mentioned this pull request May 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants