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

Commit 0b312e0

Browse files
authored
Merge pull request #834 from JoJo2nd/v2-dyn-res
Added support for dynamic resolution
2 parents caa8ceb + 5b946b2 commit 0b312e0

File tree

6 files changed

+102
-37
lines changed

6 files changed

+102
-37
lines changed

PostProcessing/Editor/PostProcessLayerEditor.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ sealed class PostProcessLayerEditor : BaseEditor<PostProcessLayer>
3838

3939
Dictionary<PostProcessEvent, ReorderableList> m_CustomLists;
4040

41+
#if UNITY_2017_3_OR_NEWER
42+
Camera m_TargetCameraComponent;
43+
#endif
44+
4145
static GUIContent[] s_AntialiasingMethodNames =
4246
{
4347
new GUIContent("No Anti-aliasing"),
@@ -75,6 +79,10 @@ void OnEnable()
7579

7680
m_ShowToolkit = serializedObject.FindProperty("m_ShowToolkit");
7781
m_ShowCustomSorter = serializedObject.FindProperty("m_ShowCustomSorter");
82+
83+
#if UNITY_2017_3_OR_NEWER
84+
m_TargetCameraComponent = m_Target.GetComponent<Camera>();
85+
#endif
7886
}
7987

8088
void OnDisable()
@@ -162,6 +170,10 @@ void DoAntialiasing()
162170
if (RuntimeUtilities.isSinglePassStereoSelected)
163171
EditorGUILayout.HelpBox("TAA requires Unity 2017.3+ for Single-pass stereo rendering support.", MessageType.Warning);
164172
#endif
173+
#if UNITY_2017_3_OR_NEWER
174+
if (m_TargetCameraComponent != null && m_TargetCameraComponent.allowDynamicResolution)
175+
EditorGUILayout.HelpBox("TAA is not supported with Dynamic Resolution.", MessageType.Warning);
176+
#endif
165177

166178
EditorGUILayout.PropertyField(m_TaaJitterSpread);
167179
EditorGUILayout.PropertyField(m_TaaStationaryBlending);

PostProcessing/Runtime/Effects/MotionBlur.cs

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,25 @@ public override DepthTextureMode GetCameraFlags()
5858
return DepthTextureMode.Depth | DepthTextureMode.MotionVectors;
5959
}
6060

61+
private void CreateTemporaryRT(PostProcessRenderContext context, int nameID, int width, int height, RenderTextureFormat RTFormat)
62+
{
63+
var cmd = context.command;
64+
#if UNITY_2017_2_OR_NEWER
65+
var rtDesc = context.GetDescriptor(0, RTFormat, RenderTextureReadWrite.Linear);
66+
rtDesc.width = width;
67+
rtDesc.height = height;
68+
#if UNITY_2019_1_OR_NEWER
69+
cmd.GetTemporaryRT(nameID, rtDesc, FilterMode.Point);
70+
#elif UNITY_2017_3_OR_NEWER
71+
cmd.GetTemporaryRT(nameID, rtDesc.width, rtDesc.height, rtDesc.depthBufferBits, FilterMode.Point, rtDesc.colorFormat, RenderTextureReadWrite.Linear, rtDesc.msaaSamples, rtDesc.enableRandomWrite, rtDesc.memoryless, context.camera.allowDynamicResolution);
72+
#else
73+
cmd.GetTemporaryRT(nameID, rtDesc.width, rtDesc.height, rtDesc.depthBufferBits, FilterMode.Point, rtDesc.colorFormat, RenderTextureReadWrite.Linear, rtDesc.msaaSamples, rtDesc.enableRandomWrite, rtDesc.memoryless);
74+
#endif
75+
#else
76+
cmd.GetTemporaryRT(nameID, width, height, 0, FilterMode.Point, RTFormat, RenderTextureReadWrite.Linear);
77+
#endif
78+
}
79+
6180
public override void Render(PostProcessRenderContext context)
6281
{
6382
var cmd = context.command;
@@ -92,27 +111,23 @@ public override void Render(PostProcessRenderContext context)
92111
sheet.properties.SetFloat(ShaderIDs.RcpMaxBlurRadius, 1f / maxBlurPixels);
93112

94113
int vbuffer = ShaderIDs.VelocityTex;
95-
cmd.GetTemporaryRT(vbuffer, context.width, context.height, 0, FilterMode.Point,
96-
packedRTFormat, RenderTextureReadWrite.Linear);
114+
CreateTemporaryRT(context, vbuffer, context.width, context.height, packedRTFormat);
97115
cmd.BlitFullscreenTriangle(BuiltinRenderTextureType.None, vbuffer, sheet, (int)Pass.VelocitySetup);
98116

99117
// Pass 2 - First TileMax filter (1/2 downsize)
100118
int tile2 = ShaderIDs.Tile2RT;
101-
cmd.GetTemporaryRT(tile2, context.width / 2, context.height / 2, 0, FilterMode.Point,
102-
vectorRTFormat, RenderTextureReadWrite.Linear);
119+
CreateTemporaryRT(context, tile2, context.width / 2, context.height / 2, vectorRTFormat);
103120
cmd.BlitFullscreenTriangle(vbuffer, tile2, sheet, (int)Pass.TileMax1);
104121

105122
// Pass 3 - Second TileMax filter (1/2 downsize)
106123
int tile4 = ShaderIDs.Tile4RT;
107-
cmd.GetTemporaryRT(tile4, context.width / 4, context.height / 4, 0, FilterMode.Point,
108-
vectorRTFormat, RenderTextureReadWrite.Linear);
124+
CreateTemporaryRT(context, tile4, context.width / 4, context.height / 4, vectorRTFormat);
109125
cmd.BlitFullscreenTriangle(tile2, tile4, sheet, (int)Pass.TileMax2);
110126
cmd.ReleaseTemporaryRT(tile2);
111127

112128
// Pass 4 - Third TileMax filter (1/2 downsize)
113129
int tile8 = ShaderIDs.Tile8RT;
114-
cmd.GetTemporaryRT(tile8, context.width / 8, context.height / 8, 0, FilterMode.Point,
115-
vectorRTFormat, RenderTextureReadWrite.Linear);
130+
CreateTemporaryRT(context, tile8, context.width / 8, context.height / 8, vectorRTFormat);
116131
cmd.BlitFullscreenTriangle(tile4, tile8, sheet, (int)Pass.TileMax2);
117132
cmd.ReleaseTemporaryRT(tile4);
118133

@@ -122,17 +137,13 @@ public override void Render(PostProcessRenderContext context)
122137
sheet.properties.SetFloat(ShaderIDs.TileMaxLoop, (int)(tileSize / 8f));
123138

124139
int tile = ShaderIDs.TileVRT;
125-
cmd.GetTemporaryRT(tile, context.width / tileSize, context.height / tileSize, 0,
126-
FilterMode.Point, vectorRTFormat, RenderTextureReadWrite.Linear);
140+
CreateTemporaryRT(context, tile, context.width / tileSize, context.height / tileSize, vectorRTFormat);
127141
cmd.BlitFullscreenTriangle(tile8, tile, sheet, (int)Pass.TileMaxV);
128142
cmd.ReleaseTemporaryRT(tile8);
129143

130144
// Pass 6 - NeighborMax filter
131145
int neighborMax = ShaderIDs.NeighborMaxTex;
132-
int neighborMaxWidth = context.width / tileSize;
133-
int neighborMaxHeight = context.height / tileSize;
134-
cmd.GetTemporaryRT(neighborMax, neighborMaxWidth, neighborMaxHeight, 0,
135-
FilterMode.Point, vectorRTFormat, RenderTextureReadWrite.Linear);
146+
CreateTemporaryRT(context, neighborMax, context.width / tileSize, context.height / tileSize, vectorRTFormat);
136147
cmd.BlitFullscreenTriangle(tile, neighborMax, sheet, (int)Pass.NeighborMax);
137148
cmd.ReleaseTemporaryRT(tile);
138149

PostProcessing/Runtime/Effects/MultiScaleVO.cs

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,9 @@ enum Pass
4040
readonly float[] m_InvThicknessTable = new float[12];
4141
readonly float[] m_SampleWeightTable = new float[12];
4242

43-
readonly int[] m_Widths = new int[7];
44-
readonly int[] m_Heights = new int[7];
43+
// Scaled dimensions used with dynamic resolution
44+
readonly int[] m_ScaledWidths = new int[7];
45+
readonly int[] m_ScaledHeights = new int[7];
4546

4647
AmbientOcclusion m_Settings;
4748
PropertySheet m_PropertySheet;
@@ -79,8 +80,8 @@ void Alloc(CommandBuffer cmd, int id, MipLevel size, RenderTextureFormat format,
7980
int sizeId = (int)size;
8081
cmd.GetTemporaryRT(id, new RenderTextureDescriptor
8182
{
82-
width = m_Widths[sizeId],
83-
height = m_Heights[sizeId],
83+
width = m_ScaledWidths[sizeId],
84+
height = m_ScaledHeights[sizeId],
8485
colorFormat = format,
8586
depthBufferBits = 0,
8687
volumeDepth = 1,
@@ -97,8 +98,8 @@ void AllocArray(CommandBuffer cmd, int id, MipLevel size, RenderTextureFormat fo
9798
int sizeId = (int)size;
9899
cmd.GetTemporaryRT(id, new RenderTextureDescriptor
99100
{
100-
width = m_Widths[sizeId],
101-
height = m_Heights[sizeId],
101+
width = m_ScaledWidths[sizeId],
102+
height = m_ScaledHeights[sizeId],
102103
colorFormat = format,
103104
depthBufferBits = 0,
104105
volumeDepth = 16,
@@ -135,26 +136,31 @@ float CalculateTanHalfFovHeight(Camera camera)
135136

136137
Vector2 GetSize(MipLevel mip)
137138
{
138-
return new Vector2(m_Widths[(int)mip], m_Heights[(int)mip]);
139+
return new Vector2(m_ScaledWidths[(int)mip], m_ScaledHeights[(int)mip]);
139140
}
140141

141142
Vector3 GetSizeArray(MipLevel mip)
142143
{
143-
return new Vector3(m_Widths[(int)mip], m_Heights[(int)mip], 16);
144+
return new Vector3(m_ScaledWidths[(int)mip], m_ScaledHeights[(int)mip], 16);
144145
}
145146

146147
public void GenerateAOMap(CommandBuffer cmd, Camera camera, RenderTargetIdentifier destination, RenderTargetIdentifier? depthMap, bool invert, bool isMSAA)
147148
{
148149
// Base size
149-
m_Widths[0] = camera.pixelWidth * (RuntimeUtilities.isSinglePassStereoEnabled ? 2 : 1);
150-
m_Heights[0] = camera.pixelHeight;
151-
150+
#if UNITY_2017_3_OR_NEWER
151+
m_ScaledWidths[0] = camera.scaledPixelWidth * (RuntimeUtilities.isSinglePassStereoEnabled ? 2 : 1);
152+
m_ScaledHeights[0] = camera.scaledPixelHeight;
153+
#else
154+
m_ScaledWidths[0] = camera.pixelWidth * (RuntimeUtilities.isSinglePassStereoEnabled ? 2 : 1);
155+
m_ScaledHeights[0] = camera.pixelHeight;
156+
#endif
157+
152158
// L1 -> L6 sizes
153159
for (int i = 1; i < 7; i++)
154160
{
155161
int div = 1 << i;
156-
m_Widths[i] = (m_Widths[0] + (div - 1)) / div;
157-
m_Heights[i] = (m_Heights[0] + (div - 1)) / div;
162+
m_ScaledWidths[i] = (m_ScaledWidths[0] + (div - 1)) / div;
163+
m_ScaledHeights[i] = (m_ScaledHeights[0] + (div - 1)) / div;
158164
}
159165

160166
// Allocate temporary textures
@@ -266,7 +272,7 @@ void PushDownsampleCommands(CommandBuffer cmd, Camera camera, RenderTargetIdenti
266272
cmd.SetComputeVectorParam(cs, "ZBufferParams", CalculateZBufferParams(camera));
267273
cmd.SetComputeTextureParam(cs, kernel, "Depth", depthMapId);
268274

269-
cmd.DispatchCompute(cs, kernel, m_Widths[(int)MipLevel.L4], m_Heights[(int)MipLevel.L4], 1);
275+
cmd.DispatchCompute(cs, kernel, m_ScaledWidths[(int)MipLevel.L4], m_ScaledHeights[(int)MipLevel.L4], 1);
270276

271277
if (needDepthMapRelease)
272278
Release(cmd, ShaderIDs.DepthCopy);
@@ -281,7 +287,7 @@ void PushDownsampleCommands(CommandBuffer cmd, Camera camera, RenderTargetIdenti
281287
cmd.SetComputeTextureParam(cs, kernel, "DS8xAtlas", ShaderIDs.TiledDepth3);
282288
cmd.SetComputeTextureParam(cs, kernel, "DS16xAtlas", ShaderIDs.TiledDepth4);
283289

284-
cmd.DispatchCompute(cs, kernel, m_Widths[(int)MipLevel.L6], m_Heights[(int)MipLevel.L6], 1);
290+
cmd.DispatchCompute(cs, kernel, m_ScaledWidths[(int)MipLevel.L6], m_ScaledHeights[(int)MipLevel.L6], 1);
285291
}
286292

287293
void PushRenderCommands(CommandBuffer cmd, int source, int destination, Vector3 sourceSize, float tanHalfFovH, bool isMSAA)
@@ -451,15 +457,22 @@ void PreparePropertySheet(PostProcessRenderContext context)
451457

452458
void CheckAOTexture(PostProcessRenderContext context)
453459
{
454-
if (m_AmbientOnlyAO == null || !m_AmbientOnlyAO.IsCreated() || m_AmbientOnlyAO.width != context.width || m_AmbientOnlyAO.height != context.height)
460+
bool AOUpdateNeeded = m_AmbientOnlyAO == null || !m_AmbientOnlyAO.IsCreated() || m_AmbientOnlyAO.width != context.width || m_AmbientOnlyAO.height != context.height;
461+
#if UNITY_2017_3_OR_NEWER
462+
AOUpdateNeeded = AOUpdateNeeded || m_AmbientOnlyAO.useDynamicScale != context.camera.allowDynamicResolution;
463+
#endif
464+
if (AOUpdateNeeded)
455465
{
456466
RuntimeUtilities.Destroy(m_AmbientOnlyAO);
457467

458468
m_AmbientOnlyAO = new RenderTexture(context.width, context.height, 0, RenderTextureFormat.R8, RenderTextureReadWrite.Linear)
459469
{
460470
hideFlags = HideFlags.DontSave,
461471
filterMode = FilterMode.Point,
462-
enableRandomWrite = true
472+
enableRandomWrite = true,
473+
#if UNITY_2017_3_OR_NEWER
474+
useDynamicScale = context.camera.allowDynamicResolution
475+
#endif
463476
};
464477
m_AmbientOnlyAO.Create();
465478
}

PostProcessing/Runtime/Effects/SubpixelMorphologicalAntialiasing.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,13 @@ internal void Render(PostProcessRenderContext context)
6363
var cmd = context.command;
6464
cmd.BeginSample("SubpixelMorphologicalAntialiasing");
6565

66-
cmd.GetTemporaryRT(ShaderIDs.SMAA_Flip, context.width, context.height, 0, FilterMode.Bilinear, context.sourceFormat, RenderTextureReadWrite.Linear);
67-
cmd.GetTemporaryRT(ShaderIDs.SMAA_Flop, context.width, context.height, 0, FilterMode.Bilinear, context.sourceFormat, RenderTextureReadWrite.Linear);
66+
#if UNITY_2017_3_OR_NEWER
67+
cmd.GetTemporaryRT(ShaderIDs.SMAA_Flip, context.width, context.height, 0, FilterMode.Bilinear, context.sourceFormat, RenderTextureReadWrite.Linear, 1, false, RenderTextureMemoryless.None, context.camera.allowDynamicResolution);
68+
cmd.GetTemporaryRT(ShaderIDs.SMAA_Flop, context.width, context.height, 0, FilterMode.Bilinear, context.sourceFormat, RenderTextureReadWrite.Linear, 1, false, RenderTextureMemoryless.None, context.camera.allowDynamicResolution);
69+
#else
70+
cmd.GetTemporaryRT(ShaderIDs.SMAA_Flip, context.width, context.height, 0, FilterMode.Bilinear, context.sourceFormat, RenderTextureReadWrite.Linear, 1, false);
71+
cmd.GetTemporaryRT(ShaderIDs.SMAA_Flop, context.width, context.height, 0, FilterMode.Bilinear, context.sourceFormat, RenderTextureReadWrite.Linear, 1, false);
72+
#endif
6873

6974
cmd.BlitFullscreenTriangle(context.source, ShaderIDs.SMAA_Flip, sheet, (int)Pass.EdgeDetection + (int)quality, true);
7075
cmd.BlitFullscreenTriangle(ShaderIDs.SMAA_Flip, ShaderIDs.SMAA_Flop, sheet, (int)Pass.BlendWeights + (int)quality);

PostProcessing/Runtime/PostProcessLayer.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,14 +237,20 @@ void InitLegacy()
237237
m_CurrentContext = new PostProcessRenderContext();
238238
}
239239

240+
#if UNITY_2019_1_OR_NEWER
241+
bool DynamicResolutionAllowsFinalBlitToCameraTarget()
242+
{
243+
return (!m_Camera.allowDynamicResolution || (ScalableBufferManager.heightScaleFactor == 1.0 && ScalableBufferManager.widthScaleFactor == 1.0));
244+
}
245+
#endif
240246

241247
#if UNITY_2019_1_OR_NEWER
242248
// We always use a CommandBuffer to blit to the final render target
243249
// OnRenderImage is used only to avoid the automatic blit from the RenderTexture of Camera.forceIntoRenderTexture to the actual target
244250
[ImageEffectUsesCommandBuffer]
245251
void OnRenderImage(RenderTexture src, RenderTexture dst)
246252
{
247-
if (finalBlitToCameraTarget)
253+
if (finalBlitToCameraTarget && DynamicResolutionAllowsFinalBlitToCameraTarget())
248254
RenderTexture.active = dst; // silence warning
249255
else
250256
Graphics.Blit(src, dst);
@@ -607,7 +613,7 @@ void BuildCommandBuffers()
607613
context.destination = cameraTarget;
608614

609615
#if UNITY_2019_1_OR_NEWER
610-
if (finalBlitToCameraTarget && !RuntimeUtilities.scriptableRenderPipelineActive)
616+
if (finalBlitToCameraTarget && !RuntimeUtilities.scriptableRenderPipelineActive && DynamicResolutionAllowsFinalBlitToCameraTarget())
611617
{
612618
if (m_Camera.targetTexture)
613619
{

PostProcessing/Runtime/PostProcessRenderContext.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,14 @@ public void PushDebugOverlay(CommandBuffer cmd, RenderTargetIdentifier source, P
322322
// size usages explicit
323323
#if UNITY_2017_2_OR_NEWER
324324
RenderTextureDescriptor m_sourceDescriptor;
325-
RenderTextureDescriptor GetDescriptor(int depthBufferBits = 0, RenderTextureFormat colorFormat = RenderTextureFormat.Default, RenderTextureReadWrite readWrite = RenderTextureReadWrite.Default)
325+
/// <summary>
326+
/// Returns a modified copy the RenderTextureDescriptor used by the context object.
327+
/// </summary>
328+
/// <param name="depthBufferBits">The number of bits to use for the depth buffer</param>
329+
/// <param name="colorFormat">The render texture format</param>
330+
/// <param name="readWrite">The color space conversion mode</param>
331+
/// <returns>A RenderTextureDescriptor object</returns>
332+
public RenderTextureDescriptor GetDescriptor(int depthBufferBits = 0, RenderTextureFormat colorFormat = RenderTextureFormat.Default, RenderTextureReadWrite readWrite = RenderTextureReadWrite.Default)
326333
{
327334
var modifiedDesc = new RenderTextureDescriptor(m_sourceDescriptor.width, m_sourceDescriptor.height,
328335
m_sourceDescriptor.colorFormat, depthBufferBits);
@@ -337,6 +344,11 @@ RenderTextureDescriptor GetDescriptor(int depthBufferBits = 0, RenderTextureForm
337344
modifiedDesc.enableRandomWrite = m_sourceDescriptor.enableRandomWrite;
338345
modifiedDesc.shadowSamplingMode = m_sourceDescriptor.shadowSamplingMode;
339346

347+
#if UNITY_2019_1_OR_NEWER
348+
if (m_Camera.allowDynamicResolution)
349+
modifiedDesc.useDynamicScale = true;
350+
#endif
351+
340352
if (colorFormat != RenderTextureFormat.Default)
341353
modifiedDesc.colorFormat = colorFormat;
342354

@@ -380,8 +392,14 @@ public void GetScreenSpaceTemporaryRT(CommandBuffer cmd, int nameID,
380392
//intermediates in VR are unchanged
381393
if (stereoActive && desc.dimension == Rendering.TextureDimension.Tex2DArray)
382394
desc.dimension = Rendering.TextureDimension.Tex2D;
383-
395+
396+
#if UNITY_2019_1_OR_NEWER
384397
cmd.GetTemporaryRT(nameID, desc, filter);
398+
#elif UNITY_2017_3_OR_NEWER
399+
cmd.GetTemporaryRT(nameID, desc.width, desc.height, desc.depthBufferBits, filter, desc.colorFormat, readWrite, desc.msaaSamples, desc.enableRandomWrite, desc.memoryless, m_Camera.allowDynamicResolution);
400+
#else
401+
cmd.GetTemporaryRT(nameID, desc.width, desc.height, desc.depthBufferBits, filter, desc.colorFormat, readWrite, desc.msaaSamples, desc.enableRandomWrite, desc.memoryless);
402+
#endif
385403
#else
386404
int actualWidth = width;
387405
int actualHeight = height;

0 commit comments

Comments
 (0)