Skip to content

Commit feb8f7a

Browse files
lpledouxUnityEvergreen
authored andcommitted
Spacewarp support for transparent and UI objects
1 parent b48f52d commit feb8f7a

File tree

14 files changed

+58
-11
lines changed

14 files changed

+58
-11
lines changed

Packages/com.unity.render-pipelines.universal/Editor/ShaderGraph/Includes/MotionVectorPass.hlsl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,9 @@ float4 frag(
184184

185185

186186
#if defined(APPLICATION_SPACE_WARP_MOTION)
187+
#if APPLICATION_SPACE_WARP_MOTION_TRANSPARENT && defined(_SURFACE_TYPE_TRANSPARENT)
188+
clip(surfaceDescription.Alpha - 0.001);
189+
#endif
187190
return float4(CalcAswNdcMotionVectorFromCsPositions(mvInput.positionCSNoJitter, mvInput.previousPositionCSNoJitter), 1);
188191
#else
189192
return float4(CalcNdcMotionVectorFromCsPositions(mvInput.positionCSNoJitter, mvInput.previousPositionCSNoJitter), 0, 0);

Packages/com.unity.render-pipelines.universal/Editor/ShaderGraph/Targets/UniversalLitSubTarget.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,9 @@ public override void ProcessPreviewMaterial(Material material)
119119
material.SetFloat(Property.QueueControl, (float)BaseShaderGUI.QueueControl.Auto);
120120

121121
if (IsSpacewarpSupported())
122+
{
122123
material.SetFloat(Property.XrMotionVectorsPass, 1.0f);
124+
}
123125

124126
// call the full unlit material setup function
125127
ShaderGraphLitGUI.UpdateMaterial(material, MaterialUpdateType.CreatedNewMaterial);
@@ -201,7 +203,9 @@ public override void CollectShaderProperties(PropertyCollector collector, Genera
201203
collector.AddFloatProperty(Property.QueueControl, -1.0f);
202204

203205
if (IsSpacewarpSupported())
206+
{
204207
collector.AddFloatProperty(Property.XrMotionVectorsPass, 1.0f);
208+
}
205209
}
206210

207211
public override void GetPropertiesGUI(ref TargetPropertyGUIContext context, Action onChange, Action<String> registerUndo)

Packages/com.unity.render-pipelines.universal/Editor/ShaderGraph/Targets/UniversalTarget.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1685,6 +1685,8 @@ public static RenderStateCollection ScenePicking(UniversalTarget target)
16851685
#region Pragmas
16861686
static class CorePragmas
16871687
{
1688+
public static PragmaDescriptor MultiCompileAppSpacewarpTransparent => new PragmaDescriptor { value = "multi_compile _ APPLICATION_SPACE_WARP_MOTION_TRANSPARENT" };
1689+
16881690
public static readonly PragmaCollection Default = new PragmaCollection
16891691
{
16901692
{ Pragma.Target(ShaderModel.Target20) },
@@ -1712,6 +1714,7 @@ static class CorePragmas
17121714
{
17131715
{ Pragma.Target(ShaderModel.Target35) },
17141716
{ Pragma.MultiCompileInstancing },
1717+
{ MultiCompileAppSpacewarpTransparent },
17151718
{ Pragma.Vertex("vert") },
17161719
{ Pragma.Fragment("frag") },
17171720
};

Packages/com.unity.render-pipelines.universal/Editor/ShaderGraph/Targets/UniversalUnlitSubTarget.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,9 @@ public override void ProcessPreviewMaterial(Material material)
9595
material.SetFloat(Property.QueueControl, (float)BaseShaderGUI.QueueControl.Auto);
9696

9797
if (IsSpacewarpSupported())
98+
{
9899
material.SetFloat(Property.XrMotionVectorsPass, 1.0f);
100+
}
99101

100102
// call the full unlit material setup function
101103
ShaderGraphUnlitGUI.UpdateMaterial(material, MaterialUpdateType.CreatedNewMaterial);
@@ -142,7 +144,9 @@ public override void CollectShaderProperties(PropertyCollector collector, Genera
142144
collector.AddFloatProperty(Property.QueueControl, -1.0f);
143145

144146
if (IsSpacewarpSupported())
147+
{
145148
collector.AddFloatProperty(Property.XrMotionVectorsPass, 1.0f);
149+
}
146150
}
147151

148152
public override void GetPropertiesGUI(ref TargetPropertyGUIContext context, Action onChange, Action<String> registerUndo)

Packages/com.unity.render-pipelines.universal/Runtime/Passes/XRDepthMotionPass.cs

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,15 @@ public class XRDepthMotionPass : ScriptableRenderPass
1818
private RTHandle m_XRMotionVectorDepth;
1919
private TextureHandle xrMotionVectorDepth;
2020
private bool m_XRSpaceWarpRightHandedNDC;
21+
private LayerMask m_transparentlayerMask;
2122

2223
/// <summary>
2324
/// Creates a new <c>XRDepthMotionPass</c> instance.
2425
/// </summary>
2526
/// <param name="evt">The <c>RenderPassEvent</c> to use.</param>
2627
/// <param name="xrMotionVector">The Shader used for rendering XR camera motion vector.</param>
2728
/// <seealso cref="RenderPassEvent"/>
28-
public XRDepthMotionPass(RenderPassEvent evt, Shader xrMotionVector)
29+
public XRDepthMotionPass(RenderPassEvent evt, Shader xrMotionVector, LayerMask transparentLayerMask)
2930
{
3031
base.profilingSampler = new ProfilingSampler(nameof(XRDepthMotionPass));
3132
renderPassEvent = evt;
@@ -35,12 +36,14 @@ public XRDepthMotionPass(RenderPassEvent evt, Shader xrMotionVector)
3536
m_XRMotionVectorColor = null;
3637
xrMotionVectorDepth = TextureHandle.nullHandle;
3738
m_XRMotionVectorDepth = null;
39+
m_transparentlayerMask = transparentLayerMask;
3840
}
3941

4042
private const int k_XRViewCountPerPass = 2;
4143
private class PassData
4244
{
4345
internal RendererListHandle objMotionRendererList;
46+
internal RendererListHandle objTransparentMotionRendererList;
4447
internal Matrix4x4[] previousViewProjectionStereo = new Matrix4x4[k_XRViewCountPerPass];
4548
internal Matrix4x4[] viewProjectionStereo = new Matrix4x4[k_XRViewCountPerPass];
4649
internal Material xrMotionVector;
@@ -57,9 +60,9 @@ private class PassData
5760
// Motion Vector
5861
private Material m_XRMotionVectorMaterial;
5962

60-
private static DrawingSettings GetObjectMotionDrawingSettings(Camera camera)
63+
private static DrawingSettings GetObjectMotionDrawingSettings(Camera camera, bool isTransparent = false)
6164
{
62-
var sortingSettings = new SortingSettings(camera) { criteria = SortingCriteria.CommonOpaque };
65+
var sortingSettings = new SortingSettings(camera) { criteria = isTransparent ? SortingCriteria.CommonTransparent : SortingCriteria.CommonOpaque };
6366
// Notes: Usually, PerObjectData.MotionVectors will filter the renderer nodes to only draw moving objects.
6467
// In our case, we use forceAllMotionVectorObjects in the filteringSettings to draw idle objects as well to populate depth.
6568
var drawingSettings = new DrawingSettings(k_MotionOnlyShaderTagId, sortingSettings)
@@ -77,15 +80,24 @@ private void InitObjectMotionRendererLists(ref PassData passData, ref CullingRes
7780
{
7881
var objectMotionDrawingSettings = GetObjectMotionDrawingSettings(camera);
7982

80-
// XRTODO: Extend RenderQueueRange.all to support transparent objects?
81-
// URP current' doesn't support this, missing motion override for transparent materials.
8283
var filteringSettings = new FilteringSettings(RenderQueueRange.opaque, camera.cullingMask);
8384
// Also render game objects that are not moved since last frame to save depth prepass requirement for camera motion.
8485
filteringSettings.forceAllMotionVectorObjects = true;
8586
var renderStateBlock = new RenderStateBlock(RenderStateMask.Nothing);
8687

8788
RenderingUtils.CreateRendererListWithRenderStateBlock(renderGraph, ref cullResults, objectMotionDrawingSettings, filteringSettings, renderStateBlock, ref passData.objMotionRendererList);
8889
}
90+
private void InitTransparentObjectMotionRendererLists(ref PassData passData, ref CullingResults cullResults, RenderGraph renderGraph, Camera camera)
91+
{
92+
var objectMotionDrawingSettings = GetObjectMotionDrawingSettings(camera, true);
93+
94+
var filteringSettings = new FilteringSettings(RenderQueueRange.transparent, m_transparentlayerMask);
95+
// Also render game objects that are not moved since last frame to save depth prepass requirement for camera motion.
96+
filteringSettings.forceAllMotionVectorObjects = true;
97+
var renderStateBlock = new RenderStateBlock(RenderStateMask.Nothing);
98+
99+
RenderingUtils.CreateRendererListWithRenderStateBlock(renderGraph, ref cullResults, objectMotionDrawingSettings, filteringSettings, renderStateBlock, ref passData.objTransparentMotionRendererList);
100+
}
89101

90102
/// <summary>
91103
/// Initialize the RenderGraph pass data.
@@ -214,6 +226,8 @@ internal void Render(RenderGraph renderGraph, ContextContainer frameData)
214226
// Setup RendererList
215227
InitObjectMotionRendererLists(ref passData, ref renderingData.cullResults, renderGraph, cameraData.camera);
216228
builder.UseRendererList(passData.objMotionRendererList);
229+
InitTransparentObjectMotionRendererLists(ref passData, ref renderingData.cullResults, renderGraph, cameraData.camera);
230+
builder.UseRendererList(passData.objTransparentMotionRendererList);
217231

218232
// Allow setting up global matrix array
219233
builder.AllowGlobalStateModification(true);
@@ -235,6 +249,11 @@ internal void Render(RenderGraph renderGraph, ContextContainer frameData)
235249

236250
// Fill mv texturew with camera motion for pixels that don't have mv stencil bit.
237251
context.cmd.DrawProcedural(Matrix4x4.identity, data.xrMotionVector, 0, MeshTopology.Triangles, 3, 1);
252+
253+
// Transparent Object Motion for both static and dynamic objects, fill stencil for mv filled pixels.
254+
context.cmd.SetKeyword(ShaderGlobalKeywords.APPLICATION_SPACE_WARP_MOTION_TRANSPARENT, true);
255+
context.cmd.DrawRendererList(passData.objTransparentMotionRendererList);
256+
context.cmd.SetKeyword(ShaderGlobalKeywords.APPLICATION_SPACE_WARP_MOTION_TRANSPARENT, false);
238257
});
239258
}
240259
}

Packages/com.unity.render-pipelines.universal/Runtime/UniversalRenderPipelineCore.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -991,7 +991,7 @@ internal static class ShaderGlobalKeywords
991991
public static GlobalKeyword META_QUEST_LIGHTUNROLL;
992992
public static GlobalKeyword META_QUEST_NO_SPOTLIGHTS_LIGHT_LOOP;
993993
#endif
994-
994+
public static GlobalKeyword APPLICATION_SPACE_WARP_MOTION_TRANSPARENT;
995995
// TODO: Move following keywords to Local keywords?
996996
// https://docs.unity3d.com/ScriptReference/Rendering.LocalKeyword.html
997997
//public static GlobalKeyword TonemapACES;
@@ -1112,7 +1112,7 @@ public static void InitializeShaderGlobalKeywords()
11121112
ShaderGlobalKeywords.META_QUEST_LIGHTUNROLL = GlobalKeyword.Create(ShaderKeywordStrings.META_QUEST_LIGHTUNROLL);
11131113
ShaderGlobalKeywords.META_QUEST_NO_SPOTLIGHTS_LIGHT_LOOP = GlobalKeyword.Create(ShaderKeywordStrings.META_QUEST_NO_SPOTLIGHTS_LIGHT_LOOP);
11141114
#endif
1115-
1115+
ShaderGlobalKeywords.APPLICATION_SPACE_WARP_MOTION_TRANSPARENT = GlobalKeyword.Create(ShaderKeywordStrings.APPLICATION_SPACE_WARP_MOTION_TRANSPARENT);
11161116
}
11171117
}
11181118

@@ -1462,6 +1462,9 @@ public static class ShaderKeywordStrings
14621462
internal const string META_QUEST_NO_SPOTLIGHTS_LIGHT_LOOP = "META_QUEST_NO_SPOTLIGHTS_LIGHT_LOOP";
14631463
#endif
14641464

1465+
/// <summary> Keyword used for transparency spacewarp-related code </summary>
1466+
internal const string APPLICATION_SPACE_WARP_MOTION_TRANSPARENT = "APPLICATION_SPACE_WARP_MOTION_TRANSPARENT";
1467+
14651468
/// <summary> Keyword used for Multi Sampling Anti-Aliasing (MSAA) with 2 per pixel sample count. </summary>
14661469
public const string Msaa2 = "_MSAA_2";
14671470

Packages/com.unity.render-pipelines.universal/Runtime/UniversalRenderer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ public UniversalRenderer(UniversalRendererData data) : base(data)
225225
if (GraphicsSettings.TryGetRenderPipelineSettings<UniversalRenderPipelineRuntimeXRResources>(out var xrResources))
226226
{
227227
Experimental.Rendering.XRSystem.Initialize(XRPassUniversal.Create, xrResources.xrOcclusionMeshPS, xrResources.xrMirrorViewPS);
228-
m_XRDepthMotionPass = new XRDepthMotionPass(RenderPassEvent.BeforeRenderingPrePasses, xrResources.xrMotionVector);
228+
m_XRDepthMotionPass = new XRDepthMotionPass(RenderPassEvent.BeforeRenderingPrePasses, xrResources.xrMotionVector, data.transparentLayerMask);
229229
}
230230
#endif
231231
if (GraphicsSettings.TryGetRenderPipelineSettings<UniversalRenderPipelineRuntimeShaders>(

Packages/com.unity.render-pipelines.universal/ShaderLibrary/ObjectMotionVectors.hlsl

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
struct Attributes
3232
{
3333
float4 position : POSITION;
34-
#if _ALPHATEST_ON
34+
#if _ALPHATEST_ON || APPLICATION_SPACE_WARP_MOTION_TRANSPARENT
3535
float2 uv : TEXCOORD0;
3636
#endif
3737
float3 positionOld : TEXCOORD4;
@@ -46,7 +46,7 @@ struct Varyings
4646
float4 positionCS : SV_POSITION;
4747
float4 positionCSNoJitter : POSITION_CS_NO_JITTER;
4848
float4 previousPositionCSNoJitter : PREV_POSITION_CS_NO_JITTER;
49-
#if _ALPHATEST_ON
49+
#if _ALPHATEST_ON || APPLICATION_SPACE_WARP_MOTION_TRANSPARENT
5050
float2 uv : TEXCOORD0;
5151
#endif
5252
UNITY_VERTEX_INPUT_INSTANCE_ID
@@ -65,7 +65,7 @@ Varyings vert(Attributes input)
6565

6666
const VertexPositionInputs vertexInput = GetVertexPositionInputs(input.position.xyz);
6767

68-
#if defined(_ALPHATEST_ON)
68+
#if defined(_ALPHATEST_ON) || APPLICATION_SPACE_WARP_MOTION_TRANSPARENT
6969
output.uv = TRANSFORM_TEX(input.uv, _BaseMap);
7070
#endif
7171

@@ -106,6 +106,9 @@ float4 frag(Varyings input) : SV_Target
106106
#endif
107107

108108
#if defined(APPLICATION_SPACE_WARP_MOTION)
109+
#if APPLICATION_SPACE_WARP_MOTION_TRANSPARENT
110+
clip((SampleAlbedoAlpha(input.uv, TEXTURE2D_ARGS(_BaseMap, sampler_BaseMap)).a * _BaseColor.a) - 0.001);
111+
#endif
109112
return float4(CalcAswNdcMotionVectorFromCsPositions(input.positionCSNoJitter, input.previousPositionCSNoJitter), 1);
110113
#else
111114
return float4(CalcNdcMotionVectorFromCsPositions(input.positionCSNoJitter, input.previousPositionCSNoJitter), 0, 0);

Packages/com.unity.render-pipelines.universal/Shaders/BakedLit.shader

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,7 @@ Shader "Universal Render Pipeline/Baked Lit"
395395
HLSLPROGRAM
396396
#pragma shader_feature_local _ALPHATEST_ON
397397
#pragma multi_compile _ LOD_FADE_CROSSFADE
398+
#pragma multi_compile _ APPLICATION_SPACE_WARP_MOTION_TRANSPARENT
398399
#pragma shader_feature_local_vertex _ADD_PRECOMPUTED_VELOCITY
399400
#define APPLICATION_SPACE_WARP_MOTION 1
400401

Packages/com.unity.render-pipelines.universal/Shaders/ComplexLit.shader

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,7 @@ Shader "Universal Render Pipeline/Complex Lit"
536536
HLSLPROGRAM
537537
#pragma shader_feature_local _ALPHATEST_ON
538538
#pragma multi_compile _ LOD_FADE_CROSSFADE
539+
#pragma multi_compile _ APPLICATION_SPACE_WARP_MOTION_TRANSPARENT
539540
#pragma shader_feature_local_vertex _ADD_PRECOMPUTED_VELOCITY
540541
#define APPLICATION_SPACE_WARP_MOTION 1
541542

0 commit comments

Comments
 (0)