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

Commit d803468

Browse files
committed
GC fix in 2018.1 (and potentially previous versions)
`SystemInfo.SupportsRenderTextureFormat()` generates garbage......
1 parent ba043d8 commit d803468

File tree

7 files changed

+41
-21
lines changed

7 files changed

+41
-21
lines changed

PostProcessing/Runtime/Effects/AmbientOcclusion.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,9 @@ public override bool IsEnabledAndSupported(PostProcessRenderContext context)
7777
{
7878
#if UNITY_2017_1_OR_NEWER
7979
state &= SystemInfo.supportsComputeShaders
80-
&& SystemInfo.SupportsRenderTextureFormat(RenderTextureFormat.RFloat)
81-
&& SystemInfo.SupportsRenderTextureFormat(RenderTextureFormat.RHalf)
82-
&& SystemInfo.SupportsRenderTextureFormat(RenderTextureFormat.R8);
80+
&& RenderTextureFormat.RFloat.IsSupported()
81+
&& RenderTextureFormat.RHalf.IsSupported()
82+
&& RenderTextureFormat.R8.IsSupported();
8383
#else
8484
state = false;
8585
#endif

PostProcessing/Runtime/Effects/AutoExposure.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public override bool IsEnabledAndSupported(PostProcessRenderContext context)
4040
{
4141
return enabled.value
4242
&& SystemInfo.supportsComputeShaders
43-
&& SystemInfo.SupportsRenderTextureFormat(RenderTextureFormat.RFloat);
43+
&& RenderTextureFormat.RFloat.IsSupported();
4444
}
4545
}
4646

@@ -124,7 +124,7 @@ public override void Render(PostProcessRenderContext context)
124124
m_AutoExposurePingPong[context.xrActiveEye] = ++pp % 2;
125125
m_CurrentAutoExposure = dst;
126126
}
127-
127+
128128
cmd.EndSample("AutoExposureLookup");
129129

130130
context.autoExposureTexture = m_CurrentAutoExposure;

PostProcessing/Runtime/Effects/ColorGrading.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -544,14 +544,14 @@ static RenderTextureFormat GetLutFormat()
544544
// Use ARGBHalf if possible, fallback on ARGB2101010 and ARGB32 otherwise
545545
var format = RenderTextureFormat.ARGBHalf;
546546

547-
if (!SystemInfo.SupportsRenderTextureFormat(format))
547+
if (!format.IsSupported())
548548
{
549549
format = RenderTextureFormat.ARGB2101010;
550550

551551
// Note that using a log lut in ARGB32 is a *very* bad idea but we need it for
552552
// compatibility reasons (else if a platform doesn't support one of the previous
553553
// format it'll output a black screen, or worse will segfault on the user).
554-
if (!SystemInfo.SupportsRenderTextureFormat(format))
554+
if (!format.IsSupported())
555555
format = RenderTextureFormat.ARGB32;
556556
}
557557

PostProcessing/Runtime/Effects/DepthOfField.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public override bool IsEnabledAndSupported(PostProcessRenderContext context)
3535
&& SystemInfo.graphicsShaderLevel >= 35;
3636
}
3737
}
38-
38+
3939
// TODO: Look into minimum blur amount in the distance, right now it's lerped until a point
4040
// TODO: Doesn't play nice with alpha propagation, see if it can be fixed without killing performances
4141
public sealed class DepthOfFieldRenderer : PostProcessEffectRenderer<DepthOfField>
@@ -81,10 +81,10 @@ public override DepthTextureMode GetCameraFlags()
8181

8282
RenderTextureFormat SelectFormat(RenderTextureFormat primary, RenderTextureFormat secondary)
8383
{
84-
if (SystemInfo.SupportsRenderTextureFormat(primary))
84+
if (primary.IsSupported())
8585
return primary;
8686

87-
if (SystemInfo.SupportsRenderTextureFormat(secondary))
87+
if (secondary.IsSupported())
8888
return secondary;
8989

9090
return RenderTextureFormat.Default;
@@ -159,7 +159,7 @@ public override void Render(PostProcessRenderContext context)
159159
float motionBlending = context.temporalAntialiasing.motionBlending;
160160
float blend = m_ResetHistory ? 0f : motionBlending; // Handles first frame blending
161161
var jitter = context.temporalAntialiasing.jitter;
162-
162+
163163
sheet.properties.SetVector(ShaderIDs.TaaParams, new Vector3(jitter.x, jitter.y, blend));
164164

165165
int pp = m_HistoryPingPong[context.xrActiveEye];

PostProcessing/Runtime/Effects/Grain.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ public override bool IsEnabledAndSupported(PostProcessRenderContext context)
2424
&& intensity.value > 0f;
2525
}
2626
}
27-
27+
2828
public sealed class GrainRenderer : PostProcessEffectRenderer<Grain>
2929
{
3030
RenderTexture m_GrainLookupRT;
31-
31+
3232
const int k_SampleCount = 1024;
3333
int m_SampleIndex;
3434

@@ -63,7 +63,7 @@ public override void Render(PostProcessRenderContext context)
6363

6464
m_GrainLookupRT.Create();
6565
}
66-
66+
6767
var sheet = context.propertySheets.Get(context.resources.shaders.grainBaker);
6868
sheet.properties.Clear();
6969
sheet.properties.SetFloat(ShaderIDs.Phase, time % 10f);
@@ -82,7 +82,7 @@ public override void Render(PostProcessRenderContext context)
8282

8383
RenderTextureFormat GetLookupFormat()
8484
{
85-
if (SystemInfo.SupportsRenderTextureFormat(RenderTextureFormat.ARGBHalf))
85+
if (RenderTextureFormat.ARGBHalf.IsSupported())
8686
return RenderTextureFormat.ARGBHalf;
8787

8888
return RenderTextureFormat.ARGB32;

PostProcessing/Runtime/Effects/MotionBlur.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ public override bool IsEnabledAndSupported(PostProcessRenderContext context)
2222
&& Application.isPlaying
2323
#endif
2424
&& SystemInfo.supportsMotionVectors
25-
&& SystemInfo.SupportsRenderTextureFormat(RenderTextureFormat.RGHalf)
25+
&& RenderTextureFormat.RGHalf.IsSupported()
2626
&& !RuntimeUtilities.isVREnabled;
2727
}
2828
}
29-
29+
3030
public sealed class MotionBlurRenderer : PostProcessEffectRenderer<MotionBlur>
3131
{
3232
enum Pass
@@ -57,7 +57,7 @@ public override void Render(PostProcessRenderContext context)
5757

5858
const float kMaxBlurRadius = 5f;
5959
var vectorRTFormat = RenderTextureFormat.RGHalf;
60-
var packedRTFormat = SystemInfo.SupportsRenderTextureFormat(RenderTextureFormat.ARGB2101010)
60+
var packedRTFormat = RenderTextureFormat.ARGB2101010.IsSupported()
6161
? RenderTextureFormat.ARGB2101010
6262
: RenderTextureFormat.ARGB32;
6363

PostProcessing/Runtime/Utils/TextureFormatUtilities.cs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Linq;
34
using UnityEngine.Assertions;
45

56
namespace UnityEngine.Rendering.PostProcessing
67
{
78
// Temporary code dump until the texture format refactor goes into trunk...
89
public static class TextureFormatUtilities
910
{
10-
static Dictionary<TextureFormat, RenderTextureFormat> s_FormatMap;
11+
static Dictionary<TextureFormat, RenderTextureFormat> s_FormatAliasMap;
12+
static Dictionary<int, bool> s_SupportedRenderTextureFormats;
1113

1214
static TextureFormatUtilities()
1315
{
14-
s_FormatMap = new Dictionary<TextureFormat, RenderTextureFormat>
16+
s_FormatAliasMap = new Dictionary<TextureFormat, RenderTextureFormat>
1517
{
1618
{ TextureFormat.Alpha8, RenderTextureFormat.ARGB32 },
1719
{ TextureFormat.ARGB4444, RenderTextureFormat.ARGB4444 },
@@ -66,6 +68,17 @@ static TextureFormatUtilities()
6668
{ TextureFormat.ETC_RGB4_3DS, RenderTextureFormat.ARGB32 },
6769
{ TextureFormat.ETC_RGBA8_3DS, RenderTextureFormat.ARGB32 }
6870
};
71+
72+
// In 2018.1 SystemInfo.SupportsRenderTextureFormat() generates garbage so we need to
73+
// cache its calls to avoid that...
74+
s_SupportedRenderTextureFormats = new Dictionary<int, bool>();
75+
var values = Enum.GetValues(typeof(RenderTextureFormat)).Cast<int>();
76+
77+
foreach (var format in values)
78+
{
79+
bool supported = SystemInfo.SupportsRenderTextureFormat((RenderTextureFormat)format);
80+
s_SupportedRenderTextureFormats.Add(format, supported);
81+
}
6982
}
7083

7184
public static RenderTextureFormat GetUncompressedRenderTextureFormat(Texture texture)
@@ -80,13 +93,20 @@ public static RenderTextureFormat GetUncompressedRenderTextureFormat(Texture tex
8093
var inFormat = ((Texture2D)texture).format;
8194
RenderTextureFormat outFormat;
8295

83-
if (!s_FormatMap.TryGetValue(inFormat, out outFormat))
96+
if (!s_FormatAliasMap.TryGetValue(inFormat, out outFormat))
8497
throw new NotSupportedException("Texture format not supported");
8598

8699
return outFormat;
87100
}
88101

89102
return RenderTextureFormat.Default;
90103
}
104+
105+
internal static bool IsSupported(this RenderTextureFormat format)
106+
{
107+
bool supported;
108+
s_SupportedRenderTextureFormats.TryGetValue((int)format, out supported);
109+
return supported;
110+
}
91111
}
92112
}

0 commit comments

Comments
 (0)