Skip to content

Commit

Permalink
feat: UniversalRP Scene View (#22)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
Co-authored-by: Till Davin <>
  • Loading branch information
happy-turtle and Till Davin authored Dec 30, 2023
1 parent 0d4459c commit 6fb79a9
Show file tree
Hide file tree
Showing 16 changed files with 186 additions and 118 deletions.
7 changes: 4 additions & 3 deletions HDRP/Runtime/OitRenderPass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
5 changes: 3 additions & 2 deletions PostProcessingStackV2/Runtime/OitPostProcess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ internal class OitPostProcessRenderer : PostProcessEffectRenderer<OitPostProcess
public override void Init()
{
base.Init();
orderIndependentTransparency ??= new OitLinkedList();
orderIndependentTransparency ??= new OitLinkedList("OitRenderPPv2");
Camera.onPreRender += PreRender;
}

Expand All @@ -33,7 +33,8 @@ private void PreRender(Camera cam)

public override void Render(PostProcessRenderContext context)
{
orderIndependentTransparency?.Render(context.command, context.source, context.destination);
var mat = orderIndependentTransparency?.Render(context.command, context.source, context.destination);
context.command.Blit(context.source, context.destination, mat);
}

public override void Release()
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ If in doubt try to import the sample you want to use and start from there.

1. Setup the rendering implementation for your chosen pipeline:
- **High-Definition Render Pipeline:** Create a [Custom Pass volume](https://docs.unity3d.com/Packages/[email protected]/manual/Custom-Pass-Creating.html) and add `OitRenderPass` to it.
- **Universal Render Pipeline:** Add the renderer feature `Order Independent Transparency Renderer` to your Universal Renderer Asset. _Note: URP documentation only describes a [fullscreen Blit](https://docs.unity3d.com/Packages/[email protected]/manual/renderer-features/how-to-fullscreen-blit.html) which only works in Game view. If you know whether a fullscreen Blit in URP in Scene view as well as in Game view is possible, help would be very much appreciated._
- **Universal Render Pipeline:** Add the renderer feature `Order Independent Transparency Renderer` to your Universal Renderer Asset.
- **Post-Processing Stack v2:** Add the post-processing override `Order Independent Transparency` to a post-processing volume in your scene.

2. Change the material of every object that shall be rendered with order-independent transparency. They have to be rendered with a shader writing to the buffer used by the order-independent transparency implementation. Two sample shaders that you can use are included in this project: `OrderIndependentTransparency/Unlit` for all pipelines and additionally `OrderIndependentTransparency/Standard` for the built-in pipeline.
Expand Down
1 change: 0 additions & 1 deletion Shaders/LinkedListRendering.hlsl
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#ifndef OIT_LINKED_LIST_INCLUDED
#define OIT_LINKED_LIST_INCLUDED

#include "UnityShaderVariables.cginc"
#include "OitUtils.hlsl"

struct FragmentAndLinkBuffer_STRUCT
Expand Down
95 changes: 0 additions & 95 deletions Shaders/Resources/OitFullscreenRender.shader

This file was deleted.

46 changes: 46 additions & 0 deletions Shaders/Resources/OitRenderHDRP.shader
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
Shader "Hidden/OitRenderHDRP"
{
Properties{
_MainTex("Main Texture", 2DArray) = "white" {}
}
SubShader
{
PackageRequirements {
"com.unity.render-pipelines.high-definition"
}
Tags { "RenderPipeline" = "HDRenderPipeline" }
Pass {
Name "HDRP Order-Independent Transparency Pass"
ZTest Always
ZWrite Off
Blend Off
Cull Off

HLSLPROGRAM
#pragma fragment frag
#pragma vertex Vert
#pragma target 5.0
#pragma only_renderers d3d11 playstation xboxone xboxseries vulkan metal switch
#pragma require randomwrite
// #pragma enable_d3d11_debug_symbols

#include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/RenderPass/CustomPass/CustomPassCommon.hlsl"
#include "../LinkedListRendering.hlsl"

float4 frag(Varyings input, uint uSampleIndex : SV_SampleIndex) : SV_Target
{
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);

float4 col = float4 (0, 1, 0, 1);
float depth = LoadCameraDepth(input.positionCS.xy);
PositionInputs posInput = GetPositionInput(input.positionCS.xy, _ScreenSize.zw, depth, UNITY_MATRIX_I_VP, UNITY_MATRIX_V);
// Load the camera color buffer at the mip 0 if we're not at the before rendering injection point
if (_CustomPassInjectionPoint != CUSTOMPASSINJECTIONPOINT_BEFORE_RENDERING)
col = float4(CustomPassSampleCameraColor(posInput.positionNDC.xy, 0), 1);

return renderLinkedList(col, input.positionCS.xy, uSampleIndex);
}
ENDHLSL
}
}
}
9 changes: 9 additions & 0 deletions Shaders/Resources/OitRenderHDRP.shader.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 55 additions & 0 deletions Shaders/Resources/OitRenderPPv2.shader
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
Shader "Hidden/OitRenderPPv2"
{
Properties{
_MainTex("Main Texture", 2DArray) = "white" {}
}
SubShader
{
Pass {
ZTest Always
ZWrite Off
Cull Off
Blend Off

HLSLPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma target 5.0
#pragma require randomwrite
// #pragma enable_d3d11_debug_symbols

#include "UnityCG.cginc"
#include "../LinkedListRendering.hlsl"

struct appdata {
float4 vertex : POSITION;
float2 texcoord : TEXCOORD0;
};
struct v2f {
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};

sampler2D _MainTex;
float4 _MainTex_ST;

v2f vert(appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
return o;
}

//Pixel function returns a solid color for each point.
fixed4 frag(v2f i, uint uSampleIndex : SV_SampleIndex) : SV_Target
{
// Retrieve current color from background texture
float4 col = tex2D(_MainTex, i.uv);

return renderLinkedList(col, i.vertex.xy, uSampleIndex);
}
ENDHLSL
}
}
}
File renamed without changes.
42 changes: 42 additions & 0 deletions Shaders/Resources/OitRenderURP.shader
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
Shader "Hidden/OitRenderURP"
{
SubShader
{
PackageRequirements {
"com.unity.render-pipelines.universal"
}
Tags { "RenderPipeline" = "UniversalRenderPipeline" }
Pass {
Name "URP Order-Independent Transparency Pass"
ZTest Always
ZWrite Off
Cull Off
Blend Off

HLSLPROGRAM
#pragma vertex Vert
#pragma fragment frag
#pragma target 5.0
#pragma require randomwrite
// #pragma enable_d3d11_debug_symbols

#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#include "Packages/com.unity.render-pipelines.core/Runtime/Utilities/Blit.hlsl"
#include "../LinkedListRendering.hlsl"

TEXTURE2D_X(_CameraOpaqueTexture);
SAMPLER(sampler_CameraOpaqueTexture);

//Pixel function returns a solid color for each point.
half4 frag(Varyings input, uint uSampleIndex: SV_SampleIndex) : SV_Target
{
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
// Retrieve current color from background texture
float4 col = SAMPLE_TEXTURE2D_X(_CameraOpaqueTexture, sampler_CameraOpaqueTexture, input.texcoord);

return renderLinkedList(col, input.positionCS.xy, uSampleIndex);
}
ENDHLSL
}
}
}
9 changes: 9 additions & 0 deletions Shaders/Resources/OitRenderURP.shader.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Shared/OitInterface.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using UnityEngine;
using UnityEngine.Rendering;

namespace OrderIndependentTransparency
{
public interface IOrderIndependentTransparency
{
void PreRender(CommandBuffer command);
void Render(CommandBuffer command, RenderTargetIdentifier src, RenderTargetIdentifier dest);
Material Render(CommandBuffer command, RenderTargetIdentifier src, RenderTargetIdentifier dest);
void Release();
}
}
8 changes: 4 additions & 4 deletions Shared/OitLinkedList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ public class OitLinkedList : IOrderIndependentTransparency
private readonly int clearStartOffsetBufferKernel;
private int dispatchGroupSizeX, dispatchGroupSizeY;

public OitLinkedList()
public OitLinkedList(string shaderName)
{
linkedListMaterial = new Material(Resources.Load<Shader>("OitFullscreenRender"));
linkedListMaterial = new Material(Resources.Load<Shader>(shaderName));
fragmentLinkBufferId = Shader.PropertyToID("FLBuffer");
startOffsetBufferId = Shader.PropertyToID("StartOffsetBuffer");

Expand All @@ -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()
Expand Down
Loading

0 comments on commit 6fb79a9

Please sign in to comment.