Skip to content

Commit 3806572

Browse files
robinb-u3dChmansebastienlagarde
authored
Fix for dynamic resolution and MSVO causing a higher peak VRAM usage and possible fragmentation issues when the dynamic scale is changed regularly. Brought across from https://github.com/Unity-Technologies/PostProcessing/compare/v2-msvo-dynamic-res-fix (#3442)
Co-authored-by: Thomas <[email protected]> Co-authored-by: sebastienlagarde <[email protected]>
1 parent c27d45b commit 3806572

File tree

3 files changed

+73
-51
lines changed

3 files changed

+73
-51
lines changed

com.unity.postprocessing/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1212

1313
### Fixed
1414
- Fix for issue thrown upon unloading a scene from an AssetBundle (case 1262826)
15+
- Fix for MSVO when used with dynamic resolution reallocating temporary render targets whenever the dynamic resolution scale was changed which could cause a higher peak VRAM usage and fragmentation (Case 1285605). Temporary targets will now use dynamic scaling as well to solve this. Please note there is a bug in Unity that breaks this fix (case 1285577) To make use of dynamic resolution and MSVO please use Unity 2019.4.19f1, 2020.2.2f1, 2021.1.0a9 or later.
1516

1617
## [3.0.3] - 2021-02-19
1718

com.unity.postprocessing/PostProcessing/Runtime/Effects/MultiScaleVO.cs

+71-51
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ enum Pass
3939
readonly float[] m_InvThicknessTable = new float[12];
4040
readonly float[] m_SampleWeightTable = new float[12];
4141

42+
readonly int[] m_Widths = new int[7];
43+
readonly int[] m_Heights = new int[7];
4244
// Scaled dimensions used with dynamic resolution
4345
readonly int[] m_ScaledWidths = new int[7];
4446
readonly int[] m_ScaledHeights = new int[7];
@@ -74,41 +76,57 @@ public void SetResources(PostProcessResources resources)
7476
m_Resources = resources;
7577
}
7678

77-
void Alloc(CommandBuffer cmd, int id, MipLevel size, RenderTextureFormat format, bool uav)
79+
void Alloc(CommandBuffer cmd, int id, MipLevel size, RenderTextureFormat format, bool uav, bool dynamicScale)
7880
{
7981
int sizeId = (int)size;
8082
cmd.GetTemporaryRT(id, new RenderTextureDescriptor
8183
{
84+
#if UNITY_2019_4_OR_NEWER
85+
width = m_Widths[sizeId],
86+
height = m_Heights[sizeId],
87+
#else
8288
width = m_ScaledWidths[sizeId],
8389
height = m_ScaledHeights[sizeId],
90+
#endif
8491
colorFormat = format,
8592
depthBufferBits = 0,
8693
volumeDepth = 1,
8794
autoGenerateMips = false,
8895
msaaSamples = 1,
8996
#if UNITY_2019_2_OR_NEWER
9097
mipCount = 1,
98+
#endif
99+
#if UNITY_2019_4_OR_NEWER
100+
useDynamicScale = dynamicScale,
91101
#endif
92102
enableRandomWrite = uav,
93103
dimension = TextureDimension.Tex2D,
94104
sRGB = false
95105
}, FilterMode.Point);
96106
}
97107

98-
void AllocArray(CommandBuffer cmd, int id, MipLevel size, RenderTextureFormat format, bool uav)
108+
void AllocArray(CommandBuffer cmd, int id, MipLevel size, RenderTextureFormat format, bool uav, bool dynamicScale)
99109
{
100110
int sizeId = (int)size;
101111
cmd.GetTemporaryRT(id, new RenderTextureDescriptor
102112
{
113+
#if UNITY_2019_4_OR_NEWER
114+
width = m_Widths[sizeId],
115+
height = m_Heights[sizeId],
116+
#else
103117
width = m_ScaledWidths[sizeId],
104118
height = m_ScaledHeights[sizeId],
119+
#endif
105120
colorFormat = format,
106121
depthBufferBits = 0,
107122
volumeDepth = 16,
108123
autoGenerateMips = false,
109124
msaaSamples = 1,
110125
#if UNITY_2019_2_OR_NEWER
111126
mipCount = 1,
127+
#endif
128+
#if UNITY_2019_4_OR_NEWER
129+
useDynamicScale = dynamicScale,
112130
#endif
113131
enableRandomWrite = uav,
114132
dimension = TextureDimension.Tex2DArray,
@@ -152,24 +170,26 @@ Vector3 GetSizeArray(MipLevel mip)
152170
public void GenerateAOMap(CommandBuffer cmd, Camera camera, RenderTargetIdentifier destination, RenderTargetIdentifier? depthMap, bool invert, bool isMSAA)
153171
{
154172
// Base size
173+
m_Widths[0] = m_ScaledWidths[0] = camera.pixelWidth * (RuntimeUtilities.isSinglePassStereoEnabled ? 2 : 1);
174+
m_Heights[0] = m_ScaledHeights[0] = camera.pixelHeight;
155175
#if UNITY_2017_3_OR_NEWER
156176
m_ScaledWidths[0] = camera.scaledPixelWidth * (RuntimeUtilities.isSinglePassStereoEnabled ? 2 : 1);
157177
m_ScaledHeights[0] = camera.scaledPixelHeight;
158-
#else
159-
m_ScaledWidths[0] = camera.pixelWidth * (RuntimeUtilities.isSinglePassStereoEnabled ? 2 : 1);
160-
m_ScaledHeights[0] = camera.pixelHeight;
161178
#endif
162-
179+
float widthScalingFactor = ScalableBufferManager.widthScaleFactor;
180+
float heightScalingFactor = ScalableBufferManager.heightScaleFactor;
163181
// L1 -> L6 sizes
164182
for (int i = 1; i < 7; i++)
165183
{
166184
int div = 1 << i;
167-
m_ScaledWidths[i] = (m_ScaledWidths[0] + (div - 1)) / div;
168-
m_ScaledHeights[i] = (m_ScaledHeights[0] + (div - 1)) / div;
185+
m_Widths[i] = (m_Widths[0] + (div - 1)) / div;
186+
m_Heights[i] = (m_Heights[0] + (div - 1)) / div;
187+
m_ScaledWidths[i] = Mathf.CeilToInt(m_Widths[i] * widthScalingFactor);
188+
m_ScaledHeights[i] = Mathf.CeilToInt(m_Heights[i] * heightScalingFactor);
169189
}
170190

171191
// Allocate temporary textures
172-
PushAllocCommands(cmd, isMSAA);
192+
PushAllocCommands(cmd, isMSAA, camera);
173193

174194
// Render logic
175195
PushDownsampleCommands(cmd, camera, depthMap, isMSAA);
@@ -189,53 +209,53 @@ public void GenerateAOMap(CommandBuffer cmd, Camera camera, RenderTargetIdentifi
189209
PushReleaseCommands(cmd);
190210
}
191211

192-
void PushAllocCommands(CommandBuffer cmd, bool isMSAA)
212+
void PushAllocCommands(CommandBuffer cmd, bool isMSAA, Camera camera)
193213
{
194214
if (isMSAA)
195215
{
196-
Alloc(cmd, ShaderIDs.LinearDepth, MipLevel.Original, RenderTextureFormat.RGHalf, true);
197-
198-
Alloc(cmd, ShaderIDs.LowDepth1, MipLevel.L1, RenderTextureFormat.RGFloat, true);
199-
Alloc(cmd, ShaderIDs.LowDepth2, MipLevel.L2, RenderTextureFormat.RGFloat, true);
200-
Alloc(cmd, ShaderIDs.LowDepth3, MipLevel.L3, RenderTextureFormat.RGFloat, true);
201-
Alloc(cmd, ShaderIDs.LowDepth4, MipLevel.L4, RenderTextureFormat.RGFloat, true);
202-
203-
AllocArray(cmd, ShaderIDs.TiledDepth1, MipLevel.L3, RenderTextureFormat.RGHalf, true);
204-
AllocArray(cmd, ShaderIDs.TiledDepth2, MipLevel.L4, RenderTextureFormat.RGHalf, true);
205-
AllocArray(cmd, ShaderIDs.TiledDepth3, MipLevel.L5, RenderTextureFormat.RGHalf, true);
206-
AllocArray(cmd, ShaderIDs.TiledDepth4, MipLevel.L6, RenderTextureFormat.RGHalf, true);
207-
208-
Alloc(cmd, ShaderIDs.Occlusion1, MipLevel.L1, RenderTextureFormat.RG16, true);
209-
Alloc(cmd, ShaderIDs.Occlusion2, MipLevel.L2, RenderTextureFormat.RG16, true);
210-
Alloc(cmd, ShaderIDs.Occlusion3, MipLevel.L3, RenderTextureFormat.RG16, true);
211-
Alloc(cmd, ShaderIDs.Occlusion4, MipLevel.L4, RenderTextureFormat.RG16, true);
212-
213-
Alloc(cmd, ShaderIDs.Combined1, MipLevel.L1, RenderTextureFormat.RG16, true);
214-
Alloc(cmd, ShaderIDs.Combined2, MipLevel.L2, RenderTextureFormat.RG16, true);
215-
Alloc(cmd, ShaderIDs.Combined3, MipLevel.L3, RenderTextureFormat.RG16, true);
216+
Alloc(cmd, ShaderIDs.LinearDepth, MipLevel.Original, RenderTextureFormat.RGHalf, true, camera.allowDynamicResolution);
217+
218+
Alloc(cmd, ShaderIDs.LowDepth1, MipLevel.L1, RenderTextureFormat.RGFloat, true, camera.allowDynamicResolution);
219+
Alloc(cmd, ShaderIDs.LowDepth2, MipLevel.L2, RenderTextureFormat.RGFloat, true, camera.allowDynamicResolution);
220+
Alloc(cmd, ShaderIDs.LowDepth3, MipLevel.L3, RenderTextureFormat.RGFloat, true, camera.allowDynamicResolution);
221+
Alloc(cmd, ShaderIDs.LowDepth4, MipLevel.L4, RenderTextureFormat.RGFloat, true, camera.allowDynamicResolution);
222+
223+
AllocArray(cmd, ShaderIDs.TiledDepth1, MipLevel.L3, RenderTextureFormat.RGHalf, true, camera.allowDynamicResolution);
224+
AllocArray(cmd, ShaderIDs.TiledDepth2, MipLevel.L4, RenderTextureFormat.RGHalf, true, camera.allowDynamicResolution);
225+
AllocArray(cmd, ShaderIDs.TiledDepth3, MipLevel.L5, RenderTextureFormat.RGHalf, true, camera.allowDynamicResolution);
226+
AllocArray(cmd, ShaderIDs.TiledDepth4, MipLevel.L6, RenderTextureFormat.RGHalf, true, camera.allowDynamicResolution);
227+
228+
Alloc(cmd, ShaderIDs.Occlusion1, MipLevel.L1, RenderTextureFormat.RG16, true, camera.allowDynamicResolution);
229+
Alloc(cmd, ShaderIDs.Occlusion2, MipLevel.L2, RenderTextureFormat.RG16, true, camera.allowDynamicResolution);
230+
Alloc(cmd, ShaderIDs.Occlusion3, MipLevel.L3, RenderTextureFormat.RG16, true, camera.allowDynamicResolution);
231+
Alloc(cmd, ShaderIDs.Occlusion4, MipLevel.L4, RenderTextureFormat.RG16, true, camera.allowDynamicResolution);
232+
233+
Alloc(cmd, ShaderIDs.Combined1, MipLevel.L1, RenderTextureFormat.RG16, true, camera.allowDynamicResolution);
234+
Alloc(cmd, ShaderIDs.Combined2, MipLevel.L2, RenderTextureFormat.RG16, true, camera.allowDynamicResolution);
235+
Alloc(cmd, ShaderIDs.Combined3, MipLevel.L3, RenderTextureFormat.RG16, true, camera.allowDynamicResolution);
216236
}
217237
else
218238
{
219-
Alloc(cmd, ShaderIDs.LinearDepth, MipLevel.Original, RenderTextureFormat.RHalf, true);
220-
221-
Alloc(cmd, ShaderIDs.LowDepth1, MipLevel.L1, RenderTextureFormat.RFloat, true);
222-
Alloc(cmd, ShaderIDs.LowDepth2, MipLevel.L2, RenderTextureFormat.RFloat, true);
223-
Alloc(cmd, ShaderIDs.LowDepth3, MipLevel.L3, RenderTextureFormat.RFloat, true);
224-
Alloc(cmd, ShaderIDs.LowDepth4, MipLevel.L4, RenderTextureFormat.RFloat, true);
225-
226-
AllocArray(cmd, ShaderIDs.TiledDepth1, MipLevel.L3, RenderTextureFormat.RHalf, true);
227-
AllocArray(cmd, ShaderIDs.TiledDepth2, MipLevel.L4, RenderTextureFormat.RHalf, true);
228-
AllocArray(cmd, ShaderIDs.TiledDepth3, MipLevel.L5, RenderTextureFormat.RHalf, true);
229-
AllocArray(cmd, ShaderIDs.TiledDepth4, MipLevel.L6, RenderTextureFormat.RHalf, true);
230-
231-
Alloc(cmd, ShaderIDs.Occlusion1, MipLevel.L1, RenderTextureFormat.R8, true);
232-
Alloc(cmd, ShaderIDs.Occlusion2, MipLevel.L2, RenderTextureFormat.R8, true);
233-
Alloc(cmd, ShaderIDs.Occlusion3, MipLevel.L3, RenderTextureFormat.R8, true);
234-
Alloc(cmd, ShaderIDs.Occlusion4, MipLevel.L4, RenderTextureFormat.R8, true);
235-
236-
Alloc(cmd, ShaderIDs.Combined1, MipLevel.L1, RenderTextureFormat.R8, true);
237-
Alloc(cmd, ShaderIDs.Combined2, MipLevel.L2, RenderTextureFormat.R8, true);
238-
Alloc(cmd, ShaderIDs.Combined3, MipLevel.L3, RenderTextureFormat.R8, true);
239+
Alloc(cmd, ShaderIDs.LinearDepth, MipLevel.Original, RenderTextureFormat.RHalf, true, camera.allowDynamicResolution);
240+
241+
Alloc(cmd, ShaderIDs.LowDepth1, MipLevel.L1, RenderTextureFormat.RFloat, true, camera.allowDynamicResolution);
242+
Alloc(cmd, ShaderIDs.LowDepth2, MipLevel.L2, RenderTextureFormat.RFloat, true, camera.allowDynamicResolution);
243+
Alloc(cmd, ShaderIDs.LowDepth3, MipLevel.L3, RenderTextureFormat.RFloat, true, camera.allowDynamicResolution);
244+
Alloc(cmd, ShaderIDs.LowDepth4, MipLevel.L4, RenderTextureFormat.RFloat, true, camera.allowDynamicResolution);
245+
246+
AllocArray(cmd, ShaderIDs.TiledDepth1, MipLevel.L3, RenderTextureFormat.RHalf, true, camera.allowDynamicResolution);
247+
AllocArray(cmd, ShaderIDs.TiledDepth2, MipLevel.L4, RenderTextureFormat.RHalf, true, camera.allowDynamicResolution);
248+
AllocArray(cmd, ShaderIDs.TiledDepth3, MipLevel.L5, RenderTextureFormat.RHalf, true, camera.allowDynamicResolution);
249+
AllocArray(cmd, ShaderIDs.TiledDepth4, MipLevel.L6, RenderTextureFormat.RHalf, true, camera.allowDynamicResolution);
250+
251+
Alloc(cmd, ShaderIDs.Occlusion1, MipLevel.L1, RenderTextureFormat.R8, true, camera.allowDynamicResolution);
252+
Alloc(cmd, ShaderIDs.Occlusion2, MipLevel.L2, RenderTextureFormat.R8, true, camera.allowDynamicResolution);
253+
Alloc(cmd, ShaderIDs.Occlusion3, MipLevel.L3, RenderTextureFormat.R8, true, camera.allowDynamicResolution);
254+
Alloc(cmd, ShaderIDs.Occlusion4, MipLevel.L4, RenderTextureFormat.R8, true, camera.allowDynamicResolution);
255+
256+
Alloc(cmd, ShaderIDs.Combined1, MipLevel.L1, RenderTextureFormat.R8, true, camera.allowDynamicResolution);
257+
Alloc(cmd, ShaderIDs.Combined2, MipLevel.L2, RenderTextureFormat.R8, true, camera.allowDynamicResolution);
258+
Alloc(cmd, ShaderIDs.Combined3, MipLevel.L3, RenderTextureFormat.R8, true, camera.allowDynamicResolution);
239259
}
240260
}
241261

@@ -254,7 +274,7 @@ void PushDownsampleCommands(CommandBuffer cmd, Camera camera, RenderTargetIdenti
254274
// buffer (it's only available in some specific situations).
255275
if (!RuntimeUtilities.IsResolvedDepthAvailable(camera))
256276
{
257-
Alloc(cmd, ShaderIDs.DepthCopy, MipLevel.Original, RenderTextureFormat.RFloat, false);
277+
Alloc(cmd, ShaderIDs.DepthCopy, MipLevel.Original, RenderTextureFormat.RFloat, false, camera.allowDynamicResolution);
258278
depthMapId = new RenderTargetIdentifier(ShaderIDs.DepthCopy);
259279
cmd.BlitFullscreenTriangle(BuiltinRenderTextureType.None, depthMapId, m_PropertySheet, (int)Pass.DepthCopy);
260280
needDepthMapRelease = true;

com.unity.postprocessing/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"version": "3.1.0",
44
"displayName": "Post Processing",
55
"unity": "2019.4",
6+
"unityRelease": "19f1",
67
"description": "The post-processing stack (v2) comes with a collection of effects and image filters you can apply to your cameras to improve the visuals of your games.",
78
"dependencies": {
89
"com.unity.modules.physics": "1.0.0"

0 commit comments

Comments
 (0)