Skip to content
This repository was archived by the owner on Nov 30, 2020. It is now read-only.

Commit 1bf4f14

Browse files
committed
New PostProcessRenderContext.GetScreenSpaceTemporaryRT
We need XRSettings.eyeTextureDesc to create proper single-pass temporary RTs. But they aren't available until 2017.2. So if there is the need to create a temporary screen space RT, there is a new function that handles the allocation. It either uses the old explicit-argument-driven GetTemporaryRT for pre-2017.2 (monoscopic + multi-pass) or RenderTextureDescriptor 2017.2+. There is also a 'scaler' that is applied to the texture dimensions (if needed). I might make this more explicit in the future with explicit arguments changing the size.
1 parent 035ba06 commit 1bf4f14

3 files changed

Lines changed: 34 additions & 8 deletions

File tree

PostProcessing/Runtime/Effects/Bloom.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public override void Render(PostProcessRenderContext context)
100100

101101
// Do bloom on a half-res buffer, full-res doesn't bring much and kills performances on
102102
// fillrate limited platforms
103-
RenderTextureDescriptor pyramidDesc = context.GetDescriptor(0, context.sourceFormat);
103+
var pyramidDesc = context.GetDescriptor(0, context.sourceFormat);
104104
pyramidDesc.width = Mathf.FloorToInt(pyramidDesc.width / (2f - rw));
105105
pyramidDesc.height = Mathf.FloorToInt(pyramidDesc.height / (2f - rh));
106106

PostProcessing/Runtime/Effects/DepthOfField.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,10 @@ public override void Render(PostProcessRenderContext context)
124124
cocFormat = SelectFormat(RenderTextureFormat.RHalf, RenderTextureFormat.Default);
125125
#endif
126126

127-
RenderTextureDescriptor colorDesc = context.GetDescriptor(0, colorFormat);
127+
var colorDesc = context.GetDescriptor(0, colorFormat);
128128
colorDesc.width /= 2;
129129
colorDesc.height /= 2;
130-
RenderTextureDescriptor cocDesc = context.GetDescriptor(0, cocFormat, RenderTextureReadWrite.Linear);
130+
var cocDesc = context.GetDescriptor(0, cocFormat, RenderTextureReadWrite.Linear);
131131

132132
// Material setup
133133
var f = settings.focalLength.value / 1000f;

PostProcessing/Runtime/PostProcessRenderContext.cs

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,18 @@ public Camera camera
2626
if (XRSettings.isDeviceActive)
2727
{
2828
#if UNITY_2017_2_OR_NEWER
29-
RenderTextureDescriptor xrDesc = XRSettings.eyeTextureDesc;
29+
var xrDesc = XRSettings.eyeTextureDesc;
3030
width = xrDesc.width;
3131
height = xrDesc.height;
3232
m_sourceDescriptor = xrDesc;
3333
#else
34-
width = XRSettings.eyeTextureWidth; // double this for single-pass when we can't query eyeTextureDesc
34+
// Single-pass is only supported with 2017.2+ because
35+
// that is when XRSettings.eyeTextureDesc is available.
36+
// Without it, we don't have a robust method of determining
37+
// if we are in single-pass. Users can just double the width
38+
// here if they KNOW they are using single-pass.
39+
width = XRSettings.eyeTextureWidth;
3540
height = XRSettings.eyeTextureHeight;
36-
// TODO: fill in m_sourceDescriptor
3741
#endif
3842

3943
if (camera.stereoActiveEye == Camera.MonoOrStereoscopicEye.Right)
@@ -47,9 +51,10 @@ public Camera camera
4751
width = m_Camera.pixelWidth;
4852
height = m_Camera.pixelHeight;
4953

54+
#if UNITY_2017_2_OR_NEWER
5055
m_sourceDescriptor.width = width;
5156
m_sourceDescriptor.height = height;
52-
57+
#endif
5358
xrSingleEyeWidth = width;
5459
xrSingleEyeHeight = height;
5560
}
@@ -97,10 +102,11 @@ public Camera camera
97102
// Current camera height in pixels
98103
public int height { get; private set; }
99104

105+
#if UNITY_2017_2_OR_NEWER
100106
private RenderTextureDescriptor m_sourceDescriptor;
101107
public RenderTextureDescriptor GetDescriptor(int depthBufferBits = 0, RenderTextureFormat colorFormat = RenderTextureFormat.Default, RenderTextureReadWrite readWrite = RenderTextureReadWrite.Default)
102108
{
103-
RenderTextureDescriptor modifiedDesc = new RenderTextureDescriptor(m_sourceDescriptor.width, m_sourceDescriptor.height,
109+
var modifiedDesc = new RenderTextureDescriptor(m_sourceDescriptor.width, m_sourceDescriptor.height,
104110
m_sourceDescriptor.colorFormat, depthBufferBits);
105111
modifiedDesc.dimension = m_sourceDescriptor.dimension;
106112
modifiedDesc.volumeDepth = m_sourceDescriptor.volumeDepth;
@@ -120,6 +126,24 @@ public RenderTextureDescriptor GetDescriptor(int depthBufferBits = 0, RenderText
120126

121127
return modifiedDesc;
122128
}
129+
#endif
130+
131+
public void GetScreenSpaceTemporaryRT(CommandBuffer cmd, int nameID,
132+
int depthBufferBits = 0, RenderTextureFormat colorFormat = RenderTextureFormat.Default, RenderTextureReadWrite readWrite = RenderTextureReadWrite.Default,
133+
FilterMode filter = FilterMode.Bilinear, int dimScaler = 1)
134+
{
135+
#if UNITY_2017_2_OR_NEWER
136+
var desc = GetDescriptor(depthBufferBits, colorFormat, readWrite);
137+
desc.width /= dimScaler;
138+
desc.height /= dimScaler;
139+
cmd.GetTemporaryRT(nameID, desc, filter);
140+
#else
141+
int actualWidth = width / dimScaler;
142+
int actualHeight = height / dimScaler;
143+
cmd.GetTemporaryRT(nameID, width, height, depthBufferBits, filter, colorFormat, readWrite);
144+
// TODO: How to handle MSAA for XR in older versions? Query cam?
145+
#endif
146+
}
123147

124148
// Current active rendering eye (for XR)
125149
public int xrActiveEye { get; private set; }
@@ -146,7 +170,9 @@ public void Reset()
146170
width = 0;
147171
height = 0;
148172

173+
#if UNITY_2017_2_OR_NEWER
149174
m_sourceDescriptor = new RenderTextureDescriptor(0, 0);
175+
#endif
150176

151177
xrActiveEye = (int)Camera.StereoscopicEye.Left;
152178
xrSingleEyeWidth = 0;

0 commit comments

Comments
 (0)