Skip to content

Fix URP blending modes and separate preserve specular lighting feature. #5762

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

Conversation

eh-unity
Copy link
Contributor

@eh-unity eh-unity commented Sep 22, 2021

Checklist for PR maker

  • Have you added a backport label (if needed)? For example, the need-backport-* label. After you backport the PR, the label changes to backported-*.
  • Have you updated the changelog? Each package has a CHANGELOG.md file.
  • Have you updated or added the documentation for your PR? When you add a new feature, change a property name, or change the behavior of a feature, it's best practice to include related documentation changes in the same PR. If you do add documentation, make sure to add the relevant Graphics Docs team member as a reviewer of the PR. If you are not sure which person to add, see the Docs team contacts sheet.
  • Have you added a graphic test for your PR (if needed)? When you add a new feature, or discover a bug that tests don't cover, please add a graphic test.

Purpose of this PR

There is a Preserve Specular Lighting feature which applies the alpha differently for diffuse and specular parts of shading. The intent is glass rendering.
Specular reflection should not be transparent while the diffuse part should be transparent (transmittance).
It is implemented by lifting the Src Alpha multiplication from fixed HW to the shader, so that we can selectively vary the alpha.

Previously URP implemented/mixed that with premultiplied mode, probably because that mode does use premultiplied blend mode in its implementation, even if the logical blend is Alpha blending.

This PR separates the Premultiplied alpha blend mode and Preserve Spec mode. It aims to parity with HDRP.
Preserve Spec mode is supported for Alpha and Additive blending.

This PR also adds "alpha emulation" for multiply blend mode.
It lerps the source texture towards white based on alpha value. Emulating the manual process of lessening color multiply effect by authoring the texture pixels towards white (value of 1). Effectively a sort of automatic "premultiplied" case for multiply.

The preserve spec is not supported for premultiply or multiply modes.

  • Premultiplied mode should have all the alpha already multiplied in the content.
  • Multiply doesn't really have a concept of alpha.

Previous "Premultiplied" mode can be achieved with Alpha Blend + Preserve Spec mode.

This PR handles the written shader and shadergraph cases.

Fixes at least bugs: 1260085, 1347301 and 1357703.


Testing status

Ran URP test locally.

Added a new test for basic blend modes:
066_Blending

UI Changes, preserve spec toggle:
Material:
image
Shadergraph target:
image

Comments to reviewers

Does not implement fog on transparent or off-screen accumulation features for HDRP parity.

Those should be a separate PR(s).

@github-actions
Copy link

Hi! This comment will help you figure out which jobs to run before merging your PR. The suggestions are dynamic based on what files you have changed.
Link to Yamato: https://yamato.cds.internal.unity3d.com/jobs/902-Graphics
Search for your PR branch using the sidebar on the left, then add the following segment(s) to the end of the URL (you may need multiple tabs depending on how many packages you change)

URP
/.yamato%252Fall-urp.yml%2523PR_URP_trunk
With changes to URP packages, you should also run
/.yamato%252Fall-lightmapper.yml%2523PR_LightMapper_trunk

Shader Graph
/.yamato%252Fall-shadergraph.yml%2523PR_ShaderGraph_trunk
Depending on your PR, you may also want
/.yamato%252Fall-shadergraph_builtin_foundation.yml%2523PR_ShaderGraph_BuiltIn_Foundation_trunk
/.yamato%252Fall-shadergraph_builtin_lighting.yml%2523PR_ShaderGraph_BuiltIn_Lighting_trunk

Depending on the scope of your PR, you may need to run more jobs than what has been suggested. Please speak to your lead or a Graphics SDET (#devs-graphics-automation) if you are unsure.

@eh-unity eh-unity changed the title Fix URP blending modes. Fix URP blending modes and separate preserve specular lighting feature. Sep 22, 2021
@eh-unity eh-unity marked this pull request as ready for review September 22, 2021 14:56
@eh-unity eh-unity requested review from a team as code owners September 22, 2021 14:56
@ellioman ellioman requested a review from Verasl September 24, 2021 08:19
@eh-unity eh-unity requested a review from a team as a code owner October 11, 2021 13:18
// ref: http://advances.realtimerendering.com/other/2016/naughty_dog/NaughtyDog_TechArt_Final.pdf
bool preserveSpecular = (material.HasProperty(Property.BlendModePreserveSpecular) &&
material.GetFloat(Property.BlendModePreserveSpecular) > 0) &&
blendMode != BlendMode.Multiply && blendMode != BlendMode.Premultiply;
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it by design additive mode supports premultiply?

@@ -249,7 +251,7 @@ Shader "Universal Render Pipeline/Baked Lit"
#pragma shader_feature_local _NORMALMAP
#pragma shader_feature_local_fragment _SURFACE_TYPE_TRANSPARENT
#pragma shader_feature_local_fragment _ALPHATEST_ON
#pragma shader_feature_local_fragment _ALPHAPREMULTIPLY_ON
#pragma shader_feature_local_fragment _ _ALPHAMODULATE_ON
Copy link
Contributor

Choose a reason for hiding this comment

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

Not an issue, but the _ is not necessary for shader feature when there are only two keywords. Just mentioning because it's inconsistent with the other pass in same shader (line 54)

Copy link

@aleksandrasdzikia aleksandrasdzikia left a comment

Choose a reason for hiding this comment

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

LGTM. Ran some automated tests with Universal Rendering Examples, tested different settings. Good to see new test added for blend modes. However, I would like to see more complex tests added in the future.

@phi-lira phi-lira changed the base branch from master to universal/staging October 26, 2021 14:54
@phi-lira
Copy link
Contributor

PR had green ABV before merging staging. Talked to @Verasl this one is good to go.

@phi-lira phi-lira merged commit 0218f28 into universal/staging Oct 26, 2021
@phi-lira phi-lira deleted the universal/fix-premultiplied-alpha-resurrect branch October 26, 2021 15:01
phi-lira added a commit that referenced this pull request Nov 9, 2021
…e. (#5762)

* Blend Test, Unlit, Simple Lit blending.

* Move alpha modulate to match "premultiplied" manual authoring.

* Reduce keywords.

* Lit blending fixes + clean up.

* Shadergraph blending & preserve spec fixes.

* Update test scene.

* Graph allow override test and fix.

* Fix tests, update change log.

* Add blend test platform ref images.

* Change multiply blend mode alpha to keep dst instead of multiply.

* Change the Alpha blend property from ...BlendA to ...BlendAlpha to be more clear.
* Update ref images.

* Update change log.

* Update Images.

* Fix Lit graph Preserve Spec GUI.

* Update d3d ref images.

* Hide "Preserve Spec" button when not available.

* Shadergraph: Make sure alpha keywords are only set on transparent surfaces.

* Use HDR for test output image.

* Reset ref images to match expectations for new test result settings.

* Move AlphaModulate into it's own function.

* Update ref pics.

* Refactor AlphaModulate for PBRForwardPass.

* Add material upgrader for blend modes.

* Allow material upgrades for (overridable) shadergraph materials.

* Fix blend specular graph UI.

* Add upgrade path for UniversalLit graph target settings.

* Add updated materials.

* Fix Preserve Spec for ParticleLit.

* Fix double multiplying of alpha for Particle shaders.
* Separate AlphaModulate and AlphaPremultiply in Particle Lit code.
* Move separated AlphaModulate and AlphaPremultiply into URP core.
* Remove ApplyAlphaModulate as it's now not needed.

* Fix more manually updated materials.

* Review improvements.

* Improve upgrader comment.

* Switch the blending test to use the backbuffer.

* Update ref images to match backbuffer rendering.

* Disable XR compatibility due to minor test artifact causing either XR or Non-XR to always fail.

* ref pic.

* Bump wait frames so that indirect light gets baked on all platforms.

* Revert "Disable XR compatibility due to minor test artifact causing either XR or Non-XR to always fail."

This reverts commit ea05278.

* Clamp source textures, solid color background.

* Unlit and BakedLit does not have specular. Remove _ALPHAPREMULTIPLY.

* Add unlit shader path for material postprocessor.

* Upgrade Unlit shadergraph blendmode.

* Upgrade only active subtarget to avoid inactive subtarget override.

* Update Unlit premultiplied .shadergraph.

* Adjust blending test threshold and images.

* Disable XR, adjust ref image to 1920x1080.

* Update 050_Shader_Graphs lighting settings and materials.

* Update 050_Shader_Graphs images.

* Remove ref images from wrong folder.

* Update 066_Blending ref images.

* Update linux vulkan.

* Add changelog for material upgrade.

* Disable 050_ShaderGraphs for Android and iOS.

Co-authored-by: Felipe Lira <felipedrl@gmail.com>
@phi-lira phi-lira mentioned this pull request Nov 9, 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.

5 participants