-
Notifications
You must be signed in to change notification settings - Fork 855
[ShaderGraph] [bugfix 1352662] [2022.1] Use VPOS for fragment stage screenspace calculations #5428
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
Conversation
…ations. This might be incorrect though for anyone who wants to read zw...
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. HDRP URP Shader Graph VFX 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. |
# Conflicts: # com.unity.shadergraph/CHANGELOG.md
{ | ||
[Serializable] | ||
class VertexColorMaterialSlot : Vector4MaterialSlot, IMayRequireScreenPosition | ||
class VertexColorMaterialSlot : Vector4MaterialSlot, IMayRequireVertexColor |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this class seems to have mistakenly implemented IMayRequireScreenPosition instead of IMayRequireVertexColor, which would make more sense...
Hi, is this PR targetting 21.2 ? in this case it will need a backport |
com.unity.shadergraph/Editor/Generation/TargetResources/FieldDependencies.cs
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking at the description and what has been tested, it seems ok to me.
Thanks for the detailed explanations, screenshots and the list of tests.
# Conflicts: # com.unity.shadergraph/CHANGELOG.md
# Conflicts: # com.unity.shadergraph/CHANGELOG.md # com.unity.shadergraph/Editor/Data/Util/ScreenSpaceType.cs
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Approving without testing. Developer did a great job by testing these changes. New tests added. Good and clear description. Good job!
This changes leads to a new compilation failure in VFX_URP test: |
Use common function to factorize the requirement test from SG Regression introduced by #5428
* Fix inconsistent NeedsPositionWorldInterpolator Use common function to factorize the requirement test from SG Regression introduced by #5428 * Better fix & Better performance Isolate screen computation from the world dependancy See #5784 (comment) /!\ Remove the VFXTransformPositionWorldToClip evaluated in fragment /!\ GraphicTest are 🟢 locally * Fix incorrect computation of INSG.ScreenPosition If we want to be consistent with SG implementation, we have to reproduced this code: https://github.com/Unity-Technologies/Graphics/blob/75f4b421fa774d43000d703f2bbf05d7a1ca6606/com.unity.render-pipelines.universal/Editor/ShaderGraph/Templates/SharedCode.template.hlsl#L56 ``` $SurfaceDescriptionInputs.ScreenPosition: output.ScreenPosition = ComputeScreenPos(TransformWorldToHClip(input.positionWS), _ProjectionParams.x); ``` Checked manually the values were consistent with VFX
…creenspace calculations (#5428) * Using SV_Position to calculate screenspace, instead of projection equations. This might be incorrect though for anyone who wants to read zw... * Working URP ScreenPositions * Adding changelog * Adding "ScreenPosition_Accuracy" to the input nodes tests * Fixes for HDRP target * Updating reference images * Fixing vertex color slot -- was using wrong interface * Fix for HDRP * Fix VFX ScreenPos usage * Convert VFX to use VPOS in fragment shader # Conflicts: # com.unity.shadergraph/CHANGELOG.md # com.unity.shadergraph/Editor/Data/Util/ScreenSpaceType.cs
* Fix inconsistent NeedsPositionWorldInterpolator Use common function to factorize the requirement test from SG Regression introduced by #5428 * Better fix & Better performance Isolate screen computation from the world dependancy See #5784 (comment) /!\ Remove the VFXTransformPositionWorldToClip evaluated in fragment /!\ GraphicTest are 🟢 locally * Fix incorrect computation of INSG.ScreenPosition If we want to be consistent with SG implementation, we have to reproduced this code: https://github.com/Unity-Technologies/Graphics/blob/75f4b421fa774d43000d703f2bbf05d7a1ca6606/com.unity.render-pipelines.universal/Editor/ShaderGraph/Templates/SharedCode.template.hlsl#L56 ``` $SurfaceDescriptionInputs.ScreenPosition: output.ScreenPosition = ComputeScreenPos(TransformWorldToHClip(input.positionWS), _ProjectionParams.x); ``` Checked manually the values were consistent with VFX
Purpose of this PR
Fix for https://fogbugz.unity3d.com/f/cases/1352662/
2022.1:
2021.2 backport:
The issue is that the calculation for screenspace position is calculated from world space position via applying the matrix transforms. However, the calculation is prone to floating point error, that is exacerbated greatly when the camera is far from the origin and the pipeline is not using camera-relative "world" space.
You can see in the examples below that the error can easily grow to be larger than a pixel.
This PR fixes this by changing the calculation in the fragment shader, to be based on the value returned by the VPOS fragment input. This value is not based on the projection matrices, independent of camera position, and much more accurate overall.
Docs ticket: https://jira.unity3d.com/browse/GSG-593
Console Test PR: https://github.cds.internal.unity3d.com/unity/ScriptableRenderPipelinePrivate/pull/239
Test example:
Left sphere visualizes frac(screenspace pixel coordinate) using a gradient (red, black, green) where 0.5 is black.
Right sphere shows a screenspace checkerboard alphatest pattern
Before: (at 20000 distance from origin) -- quite a bit of inaccuracy

After: (at 20000 distance from origin) -- perfect 0.5 centered pixel coordinates everywhere!

Implementation Details:
We introduce two new global values that ShaderGraph can produce, and nodes can request, in addition to ScreenPosition.
The values are defined as:
PixelPosition 2D pixel coordinate of the current fragment or vertex. Pixels are centered at 0.5.
NDCPosition 2D coordinate (0,1) across the viewport; equal to PixelPosition / ScreenResolution.
ScreenPosition (raw) 4D Clip Space homogenous coordinates of the current fragment or vertex.
Both PixelPosition and NDCPosition are calculated from VPOS in the fragment stage, and are calculated from ScreenPosition in other stages.
Testing status
Tested in both HDRP and URP:
Added a Yamato test to validate screen position accuracy in fragment shader. Previously you would have a good amount of error in the screen position from the floating point roundoff on the matrix transform calculations. Now it gives exact pixel coordinate values out to at least 3 fractional digits.
Updated reference images. Note that the new accuracy actually changes the test results for the dither node, because the position is slightly different (now more accurate), and the specific test we use is extremely position sensitive.
Master base branch: 35e38b1
Yamato:
ShaderGraph: 🟢
https://yamato.cds.internal.unity3d.com/jobs/902-Graphics/tree/sg%252Ffix%252F1352662/.yamato%252Fall-shadergraph.yml%2523ShaderGraph_2021.2/8408772/job/pipeline
master:
https://yamato.cds.internal.unity3d.com/jobs/902-Graphics/tree/master/.yamato%252Fall-shadergraph.yml%2523ShaderGraph_2021.2/8364150/job/pipeline
HDRP: 🟢
https://yamato.cds.internal.unity3d.com/jobs/902-Graphics/tree/sg%252Ffix%252F1352662/.yamato%252Fall-hdrp.yml%2523PR_HDRP_trunk/8681927/job/pipeline
URP: 🟡 -- failures match master
https://yamato.cds.internal.unity3d.com/jobs/902-Graphics/tree/sg%252Ffix%252F1352662/.yamato%252Fall-urp.yml%2523PR_URP_trunk/8702585/job/pipeline
master:
https://yamato.cds.internal.unity3d.com/jobs/902-Graphics/tree/master/.yamato%252Fall-urp.yml%2523PR_URP_trunk/8678639/job/pipeline
Failing jobs:
Build URP_Terrain on iPhone_Metal_il2cpp_Linear_Standalone_build_Player on version trunk -- both failed non-test-related 🟡
Build URP_Terrain on Android_Vulkan_il2cpp_Linear_Standalone_build_Player on version trunk -- both failed non-test-related 🟡
Build URP_Terrain on Android_OpenGLES3_il2cpp_Linear_Standalone_build_Player on version trunk -- both failed non-test-related 🟡
Build URP_Terrain on Linux_Vulkan_mono_Linear_Standalone_build_Player on version trunk -- both failed non-test-related 🟡
Build URP_Terrain on Win_Vulkan_mono_Linear_Standalone_build_Player on version trunk -- both failed non-test-related 🟡
Build URP_Terrain on Win_DX12_mono_Linear_Standalone_XR_build_Player on version trunk -- both failed non-test-related 🟡
Build URP_Terrain on Win_DX12_mono_Linear_Standalone_build_Player on version trunk -- both failed non-test-related 🟡
Build URP_Terrain on Win_DX11_mono_Linear_Standalone_XR_build_Player on version trunk -- both failed non-test-related 🟡
Build URP_Terrain on Win_DX11_mono_Linear_Standalone_build_Player on version trunk -- both failed non-test-related 🟡
URP_Foundation on iPhone_Metal_Standalone_il2cpp_Linear on version trunk -- both failed same 4 tests 🟡
URP_Lighting on iPhone_Metal_Standalone_il2cpp_Linear on version trunk -- both failed same 1 test 🟡
URP_Lighting on Android_Vulkan_Standalone_il2cpp_Linear on version trunk -- both failed on re-run (nondeterministic) 🟡
URP_Terrain on OSX_Metal_playmode_mono_Linear on version trunk -- both failed same 19 tests 🟡
URP_Terrain on Win_DX11_playmode_XR_mono_Linear on version trunk -- both failed same 13 tests 🟡
URP_PostPro on Win_DX11_playmode_XR_mono_Linear on version trunk -- both failed on re-run (nondeterministic) 🟡
ShaderGraph on OSX_Metal_playmode_mono_Linear on version trunk -- success on re-run 🟢
Console Tests: 🟡 -- failures match master
Console Test PR: https://github.cds.internal.unity3d.com/unity/ScriptableRenderPipelinePrivate/pull/239
Comments to reviewers
Notes for the reviewers you have assigned.