From 4e648af1958849d9b65bdaa98fb978b253ef43fe Mon Sep 17 00:00:00 2001 From: FrancescoC-Unity Date: Tue, 28 Apr 2020 18:28:40 +0200 Subject: [PATCH 1/4] Add resolve if needed of transparent movec --- .../Runtime/Material/SharedRTManager.cs | 17 +++ .../Runtime/RenderPipeline/HDProfileId.cs | 1 + .../RenderPipeline/HDRenderPipeline.cs | 5 + .../MSAA/ResolveTransparentMotionVec.shader | 143 ++++++++++++++++++ .../ResolveTransparentMotionVec.shader.meta | 10 ++ .../RenderPipeline/RenderPipelineResources.cs | 2 + 6 files changed, 178 insertions(+) create mode 100644 com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/RenderPass/MSAA/ResolveTransparentMotionVec.shader create mode 100644 com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/RenderPass/MSAA/ResolveTransparentMotionVec.shader.meta diff --git a/com.unity.render-pipelines.high-definition/Runtime/Material/SharedRTManager.cs b/com.unity.render-pipelines.high-definition/Runtime/Material/SharedRTManager.cs index 42b8fd65502..6da33cef502 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Material/SharedRTManager.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/Material/SharedRTManager.cs @@ -29,6 +29,7 @@ class SharedRTManager // MSAA resolve materials Material m_DepthResolveMaterial = null; Material m_ColorResolveMaterial = null; + Material m_MotionVectorResolve = null; // Flags that defines if we are using a local texture or external bool m_ReuseGBufferMemory = false; @@ -94,6 +95,7 @@ public void InitSharedBuffers(GBufferManager gbufferManager, RenderPipelineSetti // Create the required resolve materials m_DepthResolveMaterial = CoreUtils.CreateEngineMaterial(resources.shaders.depthValuesPS); m_ColorResolveMaterial = CoreUtils.CreateEngineMaterial(resources.shaders.colorResolvePS); + m_MotionVectorResolve = CoreUtils.CreateEngineMaterial(resources.shaders.resolveMotionVecPS); CoreUtils.SetKeyword(m_DepthResolveMaterial, "_HAS_MOTION_VECTORS", m_MotionVectorsSupport); } @@ -310,6 +312,7 @@ public void Cleanup() // Do not forget to release the materials CoreUtils.Destroy(m_DepthResolveMaterial); CoreUtils.Destroy(m_ColorResolveMaterial); + CoreUtils.Destroy(m_MotionVectorResolve); } } @@ -372,6 +375,20 @@ public void ResolveSharedRT(CommandBuffer cmd, HDCamera hdCamera) } } } + + public void ResolveMotionVectorTexture(CommandBuffer cmd, HDCamera hdCamera) + { + if (hdCamera.frameSettings.IsEnabled(FrameSettingsField.MSAA) && m_MotionVectorsSupport) + { + using (new ProfilingScope(cmd, ProfilingSampler.Get(HDProfileId.ResolveMSAAMotionVector))) + { + CoreUtils.SetRenderTarget(cmd, m_MotionVectorsRT, m_CameraDepthStencilBuffer); + Shader.SetGlobalTexture(HDShaderIDs._MotionVectorTextureMS, m_MotionVectorsMSAART); + cmd.DrawProcedural(Matrix4x4.identity, m_MotionVectorResolve, SampleCountToPassIndex(m_MSAASamples), MeshTopology.Triangles, 3, 1); + } + } + } + public void ResolveMSAAColor(CommandBuffer cmd, HDCamera hdCamera, RTHandle msaaTarget, RTHandle simpleTarget) { if (hdCamera.frameSettings.IsEnabled(FrameSettingsField.MSAA)) diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDProfileId.cs b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDProfileId.cs index 8d369531b2a..3538dfff77d 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDProfileId.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDProfileId.cs @@ -76,6 +76,7 @@ internal enum HDProfileId RenderWireFrame, PushToColorPicker, ResolveMSAAColor, + ResolveMSAAMotionVector, ResolveMSAADepth, ConvolveReflectionProbe, ConvolvePlanarReflectionProbe, diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.cs b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.cs index ba05ca5c744..a91cafb9291 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.cs @@ -2453,6 +2453,11 @@ void Callback(CommandBuffer c, HDCamera cam) // Render all type of transparent forward (unlit, lit, complex (hair...)) to keep the sorting between transparent objects. RenderForwardTransparent(cullingResults, hdCamera, false, renderContext, cmd); + if (hdCamera.frameSettings.IsEnabled(FrameSettingsField.TransparentsWriteMotionVector)) + { + m_SharedRTManager.ResolveMotionVectorTexture(cmd, hdCamera); + } + // We push the motion vector debug texture here as transparent object can overwrite the motion vector texture content. if(m_Asset.currentPlatformRenderPipelineSettings.supportMotionVectors) PushFullScreenDebugTexture(hdCamera, cmd, m_SharedRTManager.GetMotionVectorsBuffer(), FullScreenDebugMode.MotionVectors); diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/RenderPass/MSAA/ResolveTransparentMotionVec.shader b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/RenderPass/MSAA/ResolveTransparentMotionVec.shader new file mode 100644 index 00000000000..7bc7c253adb --- /dev/null +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/RenderPass/MSAA/ResolveTransparentMotionVec.shader @@ -0,0 +1,143 @@ +Shader "Hidden/HDRP/ResolveTransparentMotionVec" +{ + HLSLINCLUDE + #pragma target 4.5 + #pragma only_renderers d3d11 playstation xboxone vulkan metal switch + + #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl" + #include "Packages/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariables.hlsl" + // #pragma enable_d3d11_debug_symbols + + // Target multisampling textures + TEXTURE2D_X_MSAA(float2, _MotionVectorTextureMS); + + struct Attributes + { + uint vertexID : SV_VertexID; + UNITY_VERTEX_INPUT_INSTANCE_ID + }; + + struct Varyings + { + float4 positionCS : SV_POSITION; + float2 texcoord : TEXCOORD0; + UNITY_VERTEX_OUTPUT_STEREO + }; + + struct FragOut + { + float2 motionVectors : SV_Target0; + }; + + Varyings Vert(Attributes input) + { + Varyings output; + UNITY_SETUP_INSTANCE_ID(input); + UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output); + output.positionCS = GetFullScreenTriangleVertexPosition(input.vertexID); + output.texcoord = GetFullScreenTriangleTexCoord(input.vertexID) * _ScreenSize.xy; + return output; + } + + FragOut Frag1X(Varyings input) + { + UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input); + FragOut fragO; + int2 pixelCoords = int2(input.texcoord); + fragO.motionVectors = LOAD_TEXTURE2D_X_MSAA(_MotionVectorTextureMS, pixelCoords, 0); + return fragO; + } + + FragOut Frag2X(Varyings input) + { + UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input); + FragOut fragO; + int2 pixelCoords = int2(input.texcoord); + float2 outMotionVec = 0; + for(int sampleIdx = 0; sampleIdx < 2; ++sampleIdx) + { + outMotionVec += LOAD_TEXTURE2D_X_MSAA(_MotionVectorTextureMS, pixelCoords, sampleIdx); + } + fragO.motionVectors = outMotionVec * 0.5f; + return fragO; + } + + FragOut Frag4X(Varyings input) + { + UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input); + FragOut fragO; + int2 pixelCoords = int2(input.texcoord); + float2 outMotionVec = 0; + for(int sampleIdx = 0; sampleIdx < 4; ++sampleIdx) + { + outMotionVec += LOAD_TEXTURE2D_X_MSAA(_MotionVectorTextureMS, pixelCoords, sampleIdx); + } + fragO.motionVectors = outMotionVec * 0.25f; + + return fragO; + } + + FragOut Frag8X(Varyings input) + { + UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input); + FragOut fragO; + int2 pixelCoords = int2(input.texcoord); + float2 outMotionVec = 0; + for(int sampleIdx = 0; sampleIdx < 8; ++sampleIdx) + { + outMotionVec += LOAD_TEXTURE2D_X_MSAA(_MotionVectorTextureMS, pixelCoords, sampleIdx); + } + fragO.motionVectors = outMotionVec * 0.125f; + return fragO; + } + ENDHLSL + SubShader + { + Tags{ "RenderPipeline" = "HDRenderPipeline" } + + // 0: MSAA 1x + Pass + { + ZWrite On ZTest Always Blend Off Cull Off + + HLSLPROGRAM + #pragma vertex Vert + #pragma fragment Frag1X + ENDHLSL + } + + // 1: MSAA 2x + Pass + { + ZWrite On ZTest Always Blend Off Cull Off + + HLSLPROGRAM + #pragma vertex Vert + #pragma fragment Frag2X + ENDHLSL + } + + // 2: MSAA 4X + Pass + { + ZWrite On ZTest Always Blend Off Cull Off + + HLSLPROGRAM + #pragma vertex Vert + #pragma fragment Frag4X + ENDHLSL + } + + // 3: MSAA 8X + Pass + { + ZWrite On ZTest Always Blend Off Cull Off + + HLSLPROGRAM + #pragma vertex Vert + #pragma fragment Frag8X + ENDHLSL + } + } + Fallback Off +} diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/RenderPass/MSAA/ResolveTransparentMotionVec.shader.meta b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/RenderPass/MSAA/ResolveTransparentMotionVec.shader.meta new file mode 100644 index 00000000000..67281e0e43e --- /dev/null +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/RenderPass/MSAA/ResolveTransparentMotionVec.shader.meta @@ -0,0 +1,10 @@ +fileFormatVersion: 2 +guid: 7742d8d6ed31f6f4d9f2cba5c4435e94 +ShaderImporter: + externalObjects: {} + defaultTextures: [] + nonModifiableTextures: [] + preprocessorOverride: 0 + userData: + assetBundleName: + assetBundleVariant: diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/RenderPipelineResources.cs b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/RenderPipelineResources.cs index 1adb07ae3df..f09ab7373f4 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/RenderPipelineResources.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/RenderPipelineResources.cs @@ -205,6 +205,8 @@ public sealed class ShaderResources public Shader depthValuesPS; [Reload("Runtime/RenderPipeline/RenderPass/MSAA/ColorResolve.shader")] public Shader colorResolvePS; + [Reload("Runtime/RenderPipeline/RenderPass/MSAA/ResolveTransparentMotionVec.shader")] + public Shader resolveMotionVecPS; // Post-processing [Reload("Runtime/PostProcessing/Shaders/AlphaCopy.compute")] From 5fae5dc991e1c4be35bb2965d143449e3128dde4 Mon Sep 17 00:00:00 2001 From: FrancescoC-Unity Date: Tue, 28 Apr 2020 18:30:41 +0200 Subject: [PATCH 2/4] rename --- ...solveTransparentMotionVec.shader => MotionVecResolve.shader} | 2 +- ...parentMotionVec.shader.meta => MotionVecResolve.shader.meta} | 2 +- .../Runtime/RenderPipeline/RenderPipelineResources.cs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) rename com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/RenderPass/MSAA/{ResolveTransparentMotionVec.shader => MotionVecResolve.shader} (98%) rename com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/RenderPass/MSAA/{ResolveTransparentMotionVec.shader.meta => MotionVecResolve.shader.meta} (83%) diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/RenderPass/MSAA/ResolveTransparentMotionVec.shader b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/RenderPass/MSAA/MotionVecResolve.shader similarity index 98% rename from com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/RenderPass/MSAA/ResolveTransparentMotionVec.shader rename to com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/RenderPass/MSAA/MotionVecResolve.shader index 7bc7c253adb..09246ce23c3 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/RenderPass/MSAA/ResolveTransparentMotionVec.shader +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/RenderPass/MSAA/MotionVecResolve.shader @@ -1,4 +1,4 @@ -Shader "Hidden/HDRP/ResolveTransparentMotionVec" +Shader "Hidden/HDRP/MotionVecResolve" { HLSLINCLUDE #pragma target 4.5 diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/RenderPass/MSAA/ResolveTransparentMotionVec.shader.meta b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/RenderPass/MSAA/MotionVecResolve.shader.meta similarity index 83% rename from com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/RenderPass/MSAA/ResolveTransparentMotionVec.shader.meta rename to com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/RenderPass/MSAA/MotionVecResolve.shader.meta index 67281e0e43e..b65eb06c832 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/RenderPass/MSAA/ResolveTransparentMotionVec.shader.meta +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/RenderPass/MSAA/MotionVecResolve.shader.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: 7742d8d6ed31f6f4d9f2cba5c4435e94 +guid: ea18ca9826385e943979c46cf98968cc ShaderImporter: externalObjects: {} defaultTextures: [] diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/RenderPipelineResources.cs b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/RenderPipelineResources.cs index f09ab7373f4..72f1a623d29 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/RenderPipelineResources.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/RenderPipelineResources.cs @@ -205,7 +205,7 @@ public sealed class ShaderResources public Shader depthValuesPS; [Reload("Runtime/RenderPipeline/RenderPass/MSAA/ColorResolve.shader")] public Shader colorResolvePS; - [Reload("Runtime/RenderPipeline/RenderPass/MSAA/ResolveTransparentMotionVec.shader")] + [Reload("Runtime/RenderPipeline/RenderPass/MSAA/MotionVecResolve.shader")] public Shader resolveMotionVecPS; // Post-processing From 5164b6c06a7a0ca2a49e41e02960da13c18d29cf Mon Sep 17 00:00:00 2001 From: FrancescoC-Unity Date: Tue, 28 Apr 2020 18:41:22 +0200 Subject: [PATCH 3/4] rendergraph --- .../HDRenderPipeline.RenderGraph.cs | 47 +++++++++++++++++++ .../RenderPipeline/HDRenderPipeline.cs | 3 ++ 2 files changed, 50 insertions(+) diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.RenderGraph.cs b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.RenderGraph.cs index 497c5984b3a..00657624e74 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.RenderGraph.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.RenderGraph.cs @@ -147,6 +147,11 @@ void ExecuteWithRenderGraph( RenderRequest renderRequest, colorBuffer = RenderTransparency(m_RenderGraph, hdCamera, colorBuffer, prepassOutput.depthBuffer, prepassOutput.motionVectorsBuffer, currentColorPyramid, prepassOutput.depthPyramidTexture, shadowResult, cullingResults); + if (hdCamera.frameSettings.IsEnabled(FrameSettingsField.TransparentsWriteMotionVector)) + { + prepassOutput.motionVectorsBuffer = ResolveMotionVector(m_RenderGraph, hdCamera, prepassOutput.motionVectorsBuffer); + } + // TODO RENDERGRAPH : Move this to the end after we do move semantic and graph pruning to avoid doing the rest of the frame for nothing // Transparent objects may write to the depth and motion vectors buffers. aovRequest.PushCameraTexture(m_RenderGraph, AOVBuffers.DepthStencil, hdCamera, prepassOutput.resolvedDepthBuffer, aovBuffers); @@ -1015,6 +1020,48 @@ TextureHandle ResolveMSAAColor(RenderGraph renderGraph, HDCamera hdCamera, Textu } } + class ResolveMotionVectorData + { + public TextureHandle input; + public TextureHandle output; + public Material resolveMaterial; + public int passIndex; + } + + TextureHandle ResolveMotionVector(RenderGraph renderGraph, HDCamera hdCamera, TextureHandle input) + { + if (hdCamera.frameSettings.IsEnabled(FrameSettingsField.MSAA)) + { + using (var builder = renderGraph.AddRenderPass("ResolveMotionVector", out var passData)) + { + var outputDesc = renderGraph.GetTextureDesc(input); + outputDesc.enableMSAA = false; + outputDesc.enableRandomWrite = true; + outputDesc.bindTextureMS = false; + outputDesc.name = string.Format("{0}Resolved", outputDesc.name); + + passData.input = builder.ReadTexture(input); + passData.output = builder.UseColorBuffer(renderGraph.CreateTexture(outputDesc), 0); + passData.resolveMaterial = m_MotionVectorResolve; + passData.passIndex = SampleCountToPassIndex(m_MSAASamples); + + builder.SetRenderFunc( + (ResolveColorData data, RenderGraphContext context) => + { + var res = context.resources; + var mpb = context.renderGraphPool.GetTempMaterialPropertyBlock(); + mpb.SetTexture(HDShaderIDs._MotionVectorTextureMS, res.GetTexture(data.input)); + context.cmd.DrawProcedural(Matrix4x4.identity, data.resolveMaterial, data.passIndex, MeshTopology.Triangles, 3, 1, mpb); + }); + + return passData.output; + } + } + else + { + return input; + } + } #if UNITY_EDITOR class RenderGizmosPassData { diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.cs b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.cs index a91cafb9291..16970f25719 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.cs @@ -323,6 +323,7 @@ internal bool showCascade // MSAA resolve materials Material m_ColorResolveMaterial = null; + Material m_MotionVectorResolve = null; // Flag that defines if ray tracing is supported by the current asset and platform bool m_RayTracingSupported = false; @@ -512,6 +513,7 @@ public HDRenderPipeline(HDRenderPipelineAsset asset, HDRenderPipelineAsset defau InitializePrepass(m_Asset); m_ColorResolveMaterial = CoreUtils.CreateEngineMaterial(asset.renderPipelineResources.shaders.colorResolvePS); + m_MotionVectorResolve = CoreUtils.CreateEngineMaterial(asset.renderPipelineResources.shaders.resolveMotionVecPS); InitializeProbeVolumes(); } @@ -960,6 +962,7 @@ protected override void Dispose(bool disposing) m_RenderGraph.UnRegisterDebug(); CleanupPrepass(); CoreUtils.Destroy(m_ColorResolveMaterial); + CoreUtils.Destroy(m_MotionVectorResolve); #if UNITY_EDITOR SceneViewDrawMode.ResetDrawMode(); From dbb94fc43d26e4a8332caaf4b8ca28dfd6cacb8a Mon Sep 17 00:00:00 2001 From: FrancescoC-Unity Date: Tue, 28 Apr 2020 18:44:16 +0200 Subject: [PATCH 4/4] changelog --- com.unity.render-pipelines.high-definition/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/com.unity.render-pipelines.high-definition/CHANGELOG.md b/com.unity.render-pipelines.high-definition/CHANGELOG.md index d2ab7d1072e..97d8ac1f7da 100644 --- a/com.unity.render-pipelines.high-definition/CHANGELOG.md +++ b/com.unity.render-pipelines.high-definition/CHANGELOG.md @@ -568,6 +568,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - Fixed shadowmask UI now correctly showing shadowmask disable - Made more explicit the warning about raytracing and asynchronous compute. Also fixed the condition in which it appears. - Fixed a null ref exception in static sky when the default volume profile is invalid. +- Fixed transparent motion vectors not working when in MSAA. ### Changed - Improve MIP selection for decals on Transparents