From 6fb79a906a67ca9f5323488241df82cabc4cfdb0 Mon Sep 17 00:00:00 2001 From: Till Davin Date: Sat, 30 Dec 2023 16:17:17 +0100 Subject: [PATCH] feat: UniversalRP Scene View (#22) * docs: remove unity version from package.json * fix: update deprecated colorTarget * build: add package requirement to URP shader * return material on render and use Blitter for SRP implementations * fix: fix URP fullscreen shader and add built-in shader pass * fix(urp): move cmd clear * refactor: split render shader to multiple pipeline specific shaders * docs: update readme --------- Co-authored-by: Till Davin Co-authored-by: Till Davin <> --- HDRP/Runtime/OitRenderPass.cs | 7 +- .../Runtime/OitPostProcess.cs | 5 +- README.md | 2 +- Shaders/LinkedListRendering.hlsl | 1 - Shaders/Resources/OitFullscreenRender.shader | 95 ------------------- Shaders/Resources/OitRenderHDRP.shader | 46 +++++++++ Shaders/Resources/OitRenderHDRP.shader.meta | 9 ++ Shaders/Resources/OitRenderPPv2.shader | 55 +++++++++++ ....shader.meta => OitRenderPPv2.shader.meta} | 0 Shaders/Resources/OitRenderURP.shader | 42 ++++++++ Shaders/Resources/OitRenderURP.shader.meta | 9 ++ Shared/OitInterface.cs | 3 +- Shared/OitLinkedList.cs | 8 +- URP/Runtime/OitPass.cs | 12 ++- .../OrderIndependentTransparencyRenderer.cs | 8 +- package.json | 2 +- 16 files changed, 186 insertions(+), 118 deletions(-) delete mode 100644 Shaders/Resources/OitFullscreenRender.shader create mode 100644 Shaders/Resources/OitRenderHDRP.shader create mode 100644 Shaders/Resources/OitRenderHDRP.shader.meta create mode 100644 Shaders/Resources/OitRenderPPv2.shader rename Shaders/Resources/{OitFullscreenRender.shader.meta => OitRenderPPv2.shader.meta} (100%) create mode 100644 Shaders/Resources/OitRenderURP.shader create mode 100644 Shaders/Resources/OitRenderURP.shader.meta diff --git a/HDRP/Runtime/OitRenderPass.cs b/HDRP/Runtime/OitRenderPass.cs index 7878069..e40806c 100644 --- a/HDRP/Runtime/OitRenderPass.cs +++ b/HDRP/Runtime/OitRenderPass.cs @@ -12,21 +12,22 @@ class OitRenderPass : CustomPass protected override void Setup(ScriptableRenderContext renderContext, CommandBuffer cmd) { - orderIndependentTransparency ??= new OitLinkedList(); + orderIndependentTransparency ??= new OitLinkedList("OitRenderHDRP"); } protected override void Execute(CustomPassContext ctx) { // draw objects with UAV targets set var preRenderCmd = CommandBufferPool.Get("Order Independent Transparency Pre Render"); - preRenderCmd.Clear(); orderIndependentTransparency.PreRender(preRenderCmd); ctx.renderContext.ExecuteCommandBuffer(preRenderCmd); + preRenderCmd.Clear(); CommandBufferPool.Release(preRenderCmd); CustomPassUtils.DrawRenderers(ctx, objectLayerMask); // fullscreen blend of transparent pixel buffer - orderIndependentTransparency.Render(ctx.cmd, ctx.cameraColorBuffer, ctx.cameraColorBuffer); + var mat = orderIndependentTransparency.Render(ctx.cmd, ctx.cameraColorBuffer, ctx.cameraColorBuffer); + Blitter.BlitCameraTexture(ctx.cmd, ctx.cameraColorBuffer, ctx.cameraColorBuffer, mat, 0); } protected override void Cleanup() diff --git a/PostProcessingStackV2/Runtime/OitPostProcess.cs b/PostProcessingStackV2/Runtime/OitPostProcess.cs index a7d5f8f..c29238a 100644 --- a/PostProcessingStackV2/Runtime/OitPostProcess.cs +++ b/PostProcessingStackV2/Runtime/OitPostProcess.cs @@ -19,7 +19,7 @@ internal class OitPostProcessRenderer : PostProcessEffectRenderer("OitFullscreenRender")); + linkedListMaterial = new Material(Resources.Load(shaderName)); fragmentLinkBufferId = Shader.PropertyToID("FLBuffer"); startOffsetBufferId = Shader.PropertyToID("StartOffsetBuffer"); @@ -44,13 +44,13 @@ public void PreRender(CommandBuffer command) command.SetRandomWriteTarget(2, startOffsetBuffer); } - public void Render(CommandBuffer command, RenderTargetIdentifier src, RenderTargetIdentifier dest) + public Material Render(CommandBuffer command, RenderTargetIdentifier src, RenderTargetIdentifier dest) { command.ClearRandomWriteTargets(); // blend linked list linkedListMaterial.SetBuffer(fragmentLinkBufferId, fragmentLinkBuffer); linkedListMaterial.SetBuffer(startOffsetBufferId, startOffsetBuffer); - command.Blit(src, dest, linkedListMaterial); + return linkedListMaterial; } public void Release() diff --git a/URP/Runtime/OitPass.cs b/URP/Runtime/OitPass.cs index 03d5aad..1437d34 100644 --- a/URP/Runtime/OitPass.cs +++ b/URP/Runtime/OitPass.cs @@ -12,26 +12,28 @@ internal class OitPass : ScriptableRenderPass public OitPass() { renderPassEvent = RenderPassEvent.BeforeRenderingTransparents; - orderIndependentTransparency = new OitLinkedList(); + orderIndependentTransparency = new OitLinkedList("OitRenderURP"); RenderPipelineManager.beginContextRendering += PreRender; } private void PreRender(ScriptableRenderContext context, List cameras) { CommandBuffer cmd = CommandBufferPool.Get("Order Independent Transparency Pre Render"); - cmd.Clear(); orderIndependentTransparency.PreRender(cmd); context.ExecuteCommandBuffer(cmd); + cmd.Clear(); CommandBufferPool.Release(cmd); } public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData) { CommandBuffer cmd = CommandBufferPool.Get("Order Independent Transparency"); - cmd.Clear(); - orderIndependentTransparency.Render(cmd, renderingData.cameraData.renderer.cameraColorTarget, - renderingData.cameraData.renderer.cameraColorTarget); + var mat = orderIndependentTransparency.Render(cmd, renderingData.cameraData.renderer.cameraColorTargetHandle, + renderingData.cameraData.renderer.cameraColorTargetHandle); + Blitter.BlitCameraTexture(cmd, renderingData.cameraData.renderer.cameraColorTargetHandle, + renderingData.cameraData.renderer.cameraColorTargetHandle, mat, 0); context.ExecuteCommandBuffer(cmd); + cmd.Clear(); CommandBufferPool.Release(cmd); } diff --git a/URP/Runtime/OrderIndependentTransparencyRenderer.cs b/URP/Runtime/OrderIndependentTransparencyRenderer.cs index 38641da..f5394d1 100644 --- a/URP/Runtime/OrderIndependentTransparencyRenderer.cs +++ b/URP/Runtime/OrderIndependentTransparencyRenderer.cs @@ -1,5 +1,3 @@ -using System; -using UnityEngine; using UnityEngine.Rendering.Universal; namespace OrderIndependentTransparency.URP @@ -17,10 +15,10 @@ public override void Create() public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData) { - // Unity does not provide an example how to perform a fullscreen Blit that works in scene view - // Hence only Blit in Game view for now - if (renderingData.cameraData.cameraType != CameraType.Game) + if (renderingData.cameraData.isPreviewCamera) + { return; + } //Calling ConfigureInput with the ScriptableRenderPassInput.Color argument ensures that the opaque texture is available to the Render Pass oitPass.ConfigureInput(ScriptableRenderPassInput.Color); renderer.EnqueuePass(oitPass); diff --git a/package.json b/package.json index 4cf8e99..95e83a2 100644 --- a/package.json +++ b/package.json @@ -1,9 +1,9 @@ { "name": "org.happy-turtle.order-independent-transparency", "version": "3.0.1", + "unity": "2021.2", "displayName": "Order-independent Transparency", "description": "This is an implementation of order-independent transparency for the Built-In Pipeline. It uses Per-Pixel Linked Lists, implemented with RWStructuredBuffers. This is a feature requiring Shader Model 5.0 with ComputeBuffers, see the Unity Manual for supported platforms.", - "unity": "2020.3", "author": { "name": "Till Davin", "email": "code@tilldavin.de"