Skip to content

Commit 883bd1c

Browse files
authored
Fix code where FormatUsage enums are used as flags (#4972)
Co-authored-by: Florian Penzkofer <florian@unity3d.com>
1 parent 8598e7c commit 883bd1c

File tree

10 files changed

+32
-19
lines changed

10 files changed

+32
-19
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,9 @@ public override bool IsEnabledAndSupported(PostProcessRenderContext context)
190190

191191
state &= SystemInfo.supportsComputeShaders
192192
&& !RuntimeUtilities.isAndroidOpenGL
193-
&& SystemInfo.IsFormatSupported(GraphicsFormat.R32_SFloat, FormatUsage.Render | FormatUsage.Sparse)
194-
&& SystemInfo.IsFormatSupported(GraphicsFormat.R16_SFloat, FormatUsage.Render | FormatUsage.Sparse)
195-
&& SystemInfo.IsFormatSupported(GraphicsFormat.R8_UNorm, FormatUsage.Render | FormatUsage.Sparse);
193+
&& SystemInfo.IsFormatSupported(GraphicsFormat.R32_SFloat, FormatUsage.Sparse) // Sparse implies Render
194+
&& SystemInfo.IsFormatSupported(GraphicsFormat.R16_SFloat, FormatUsage.Sparse)
195+
&& SystemInfo.IsFormatSupported(GraphicsFormat.R8_UNorm, FormatUsage.Sparse);
196196
}
197197

198198
return state;

com.unity.render-pipelines.core/Runtime/Utilities/TextureCurve.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,9 @@ public void SetDirty()
111111

112112
static GraphicsFormat GetTextureFormat()
113113
{
114-
if (SystemInfo.IsFormatSupported(GraphicsFormat.R16_SFloat, FormatUsage.Sample | FormatUsage.SetPixels))
114+
if (SystemInfo.IsFormatSupported(GraphicsFormat.R16_SFloat, FormatUsage.Sample) && SystemInfo.IsFormatSupported(GraphicsFormat.R16_SFloat, FormatUsage.SetPixels))
115115
return GraphicsFormat.R16_SFloat;
116-
if (SystemInfo.IsFormatSupported(GraphicsFormat.R8_UNorm, FormatUsage.Sample | FormatUsage.SetPixels))
116+
if (SystemInfo.IsFormatSupported(GraphicsFormat.R8_UNorm, FormatUsage.Sample) && SystemInfo.IsFormatSupported(GraphicsFormat.R8_UNorm, FormatUsage.SetPixels))
117117
return GraphicsFormat.R8_UNorm;
118118

119119
return GraphicsFormat.R8G8B8A8_UNorm;

com.unity.render-pipelines.universal/Runtime/2D/Passes/Utility/RendererLighting.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ private static GraphicsFormat GetRenderTextureFormat()
6868
{
6969
if (!s_HasSetupRenderTextureFormatToUse)
7070
{
71-
if (SystemInfo.IsFormatSupported(GraphicsFormat.B10G11R11_UFloatPack32, FormatUsage.Linear | FormatUsage.Render))
71+
if (SystemInfo.IsFormatSupported(GraphicsFormat.B10G11R11_UFloatPack32, FormatUsage.Linear) && SystemInfo.IsFormatSupported(GraphicsFormat.B10G11R11_UFloatPack32, FormatUsage.Render))
7272
s_RenderTextureFormatToUse = GraphicsFormat.B10G11R11_UFloatPack32;
73-
else if (SystemInfo.IsFormatSupported(GraphicsFormat.R16G16B16A16_SFloat, FormatUsage.Linear | FormatUsage.Render))
73+
else if (SystemInfo.IsFormatSupported(GraphicsFormat.R16G16B16A16_SFloat, FormatUsage.Linear) && SystemInfo.IsFormatSupported(GraphicsFormat.R16G16B16A16_SFloat, FormatUsage.Render))
7474
s_RenderTextureFormatToUse = GraphicsFormat.R16G16B16A16_SFloat;
7575

7676
s_HasSetupRenderTextureFormatToUse = true;

com.unity.render-pipelines.universal/Runtime/NativeRenderPass.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -670,10 +670,10 @@ private static GraphicsFormat GetDefaultGraphicsFormat(CameraData cameraData)
670670

671671
if (!Graphics.preserveFramebufferAlpha &&
672672
RenderingUtils.SupportsGraphicsFormat(GraphicsFormat.B10G11R11_UFloatPack32,
673-
FormatUsage.Linear | FormatUsage.Render))
673+
FormatUsage.Linear, FormatUsage.Render))
674674
hdrFormat = GraphicsFormat.B10G11R11_UFloatPack32;
675675
else if (RenderingUtils.SupportsGraphicsFormat(GraphicsFormat.R16G16B16A16_SFloat,
676-
FormatUsage.Linear | FormatUsage.Render))
676+
FormatUsage.Linear, FormatUsage.Render))
677677
hdrFormat = GraphicsFormat.R16G16B16A16_SFloat;
678678
else
679679
hdrFormat = SystemInfo.GetGraphicsFormat(DefaultFormat.HDR);

com.unity.render-pipelines.universal/Runtime/Passes/ColorGradingLutPass.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,9 @@ Material Load(Shader shader)
3939
m_LutBuilderHdr = Load(data.shaders.lutBuilderHdrPS);
4040

4141
// Warm up lut format as IsFormatSupported adds GC pressure...
42-
const FormatUsage kFlags = FormatUsage.Linear | FormatUsage.Render;
43-
if (SystemInfo.IsFormatSupported(GraphicsFormat.R16G16B16A16_SFloat, kFlags))
42+
if (SystemInfo.IsFormatSupported(GraphicsFormat.R16G16B16A16_SFloat, FormatUsage.Linear) && SystemInfo.IsFormatSupported(GraphicsFormat.R16G16B16A16_SFloat, FormatUsage.Render))
4443
m_HdrLutFormat = GraphicsFormat.R16G16B16A16_SFloat;
45-
else if (SystemInfo.IsFormatSupported(GraphicsFormat.B10G11R11_UFloatPack32, kFlags))
44+
else if (SystemInfo.IsFormatSupported(GraphicsFormat.B10G11R11_UFloatPack32, FormatUsage.Linear) && SystemInfo.IsFormatSupported(GraphicsFormat.B10G11R11_UFloatPack32, FormatUsage.Render))
4645
m_HdrLutFormat = GraphicsFormat.B10G11R11_UFloatPack32;
4746
else
4847
// Obviously using this for log lut encoding is a very bad idea for precision but we

com.unity.render-pipelines.universal/Runtime/Passes/PostProcessPass.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ public PostProcessPass(RenderPassEvent evt, PostProcessData data, Material blitM
9797
m_BlitMaterial = blitMaterial;
9898

9999
// Texture format pre-lookup
100-
if (SystemInfo.IsFormatSupported(GraphicsFormat.B10G11R11_UFloatPack32, FormatUsage.Linear | FormatUsage.Render))
100+
if (SystemInfo.IsFormatSupported(GraphicsFormat.B10G11R11_UFloatPack32, FormatUsage.Linear) &&
101+
SystemInfo.IsFormatSupported(GraphicsFormat.B10G11R11_UFloatPack32, FormatUsage.Render))
101102
{
102103
m_DefaultHDRFormat = GraphicsFormat.B10G11R11_UFloatPack32;
103104
m_UseRGBM = false;
@@ -116,9 +117,9 @@ public PostProcessPass(RenderPassEvent evt, PostProcessData data, Material blitM
116117
else
117118
m_SMAAEdgeFormat = GraphicsFormat.R8G8B8A8_UNorm;
118119

119-
if (SystemInfo.IsFormatSupported(GraphicsFormat.R16_UNorm, FormatUsage.Linear | FormatUsage.Render))
120+
if (SystemInfo.IsFormatSupported(GraphicsFormat.R16_UNorm, FormatUsage.Linear) && SystemInfo.IsFormatSupported(GraphicsFormat.R16_UNorm, FormatUsage.Render))
120121
m_GaussianCoCFormat = GraphicsFormat.R16_UNorm;
121-
else if (SystemInfo.IsFormatSupported(GraphicsFormat.R16_SFloat, FormatUsage.Linear | FormatUsage.Render))
122+
else if (SystemInfo.IsFormatSupported(GraphicsFormat.R16_SFloat, FormatUsage.Linear) && SystemInfo.IsFormatSupported(GraphicsFormat.R16_SFloat, FormatUsage.Render))
122123
m_GaussianCoCFormat = GraphicsFormat.R16_SFloat;
123124
else // Expect CoC banding
124125
m_GaussianCoCFormat = GraphicsFormat.R8_UNorm;

com.unity.render-pipelines.universal/Runtime/Passes/ScreenSpaceShadowResolvePass.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public void Setup(RenderTextureDescriptor baseDescriptor)
2626
m_RenderTextureDescriptor = baseDescriptor;
2727
m_RenderTextureDescriptor.depthBufferBits = 0;
2828
m_RenderTextureDescriptor.msaaSamples = 1;
29-
m_RenderTextureDescriptor.graphicsFormat = RenderingUtils.SupportsGraphicsFormat(GraphicsFormat.R8_UNorm, FormatUsage.Linear | FormatUsage.Render)
29+
m_RenderTextureDescriptor.graphicsFormat = RenderingUtils.SupportsGraphicsFormat(GraphicsFormat.R8_UNorm, FormatUsage.Linear, FormatUsage.Render)
3030
? GraphicsFormat.R8_UNorm
3131
: GraphicsFormat.B8G8R8A8_UNorm;
3232
}

com.unity.render-pipelines.universal/Runtime/RendererFeatures/ScreenSpaceShadows.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public override void OnCameraSetup(CommandBuffer cmd, ref RenderingData renderin
128128
m_RenderTextureDescriptor = renderingData.cameraData.cameraTargetDescriptor;
129129
m_RenderTextureDescriptor.depthBufferBits = 0;
130130
m_RenderTextureDescriptor.msaaSamples = 1;
131-
m_RenderTextureDescriptor.graphicsFormat = RenderingUtils.SupportsGraphicsFormat(GraphicsFormat.R8_UNorm, FormatUsage.Linear | FormatUsage.Render)
131+
m_RenderTextureDescriptor.graphicsFormat = RenderingUtils.SupportsGraphicsFormat(GraphicsFormat.R8_UNorm, FormatUsage.Linear, FormatUsage.Render)
132132
? GraphicsFormat.R8_UNorm
133133
: GraphicsFormat.B8G8R8A8_UNorm;
134134

com.unity.render-pipelines.universal/Runtime/RenderingUtils.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,19 @@ public static bool SupportsGraphicsFormat(GraphicsFormat format, FormatUsage usa
314314
return support;
315315
}
316316

317+
/// <summary>
318+
/// Checks if a texture format is supported by the run-time system.
319+
/// Similar to <see cref="SystemInfo.IsFormatSupported"/>, but doesn't allocate memory.
320+
/// </summary>
321+
/// <param name="format">The format to look up.</param>
322+
/// <param name="usage1">The first format usage to look up.</param>
323+
/// <param name="usage2">The second format usage to look up.</param>
324+
/// <returns>Returns true if the graphics card supports the given <c>GraphicsFormat</c> and both usage1 and usage 2.</returns>
325+
public static bool SupportsGraphicsFormat(GraphicsFormat format, FormatUsage usage1, FormatUsage usage2)
326+
{
327+
return SupportsGraphicsFormat(format, usage1) && SupportsGraphicsFormat(format, usage2);
328+
}
329+
317330
/// <summary>
318331
/// Return the last colorBuffer index actually referring to an existing RenderTarget
319332
/// </summary>

com.unity.render-pipelines.universal/Runtime/UniversalRenderPipelineCore.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -513,9 +513,9 @@ static GraphicsFormat MakeRenderTextureGraphicsFormat(bool isHdrEnabled, bool ne
513513
{
514514
if (isHdrEnabled)
515515
{
516-
if (!needsAlpha && RenderingUtils.SupportsGraphicsFormat(GraphicsFormat.B10G11R11_UFloatPack32, FormatUsage.Linear | FormatUsage.Render))
516+
if (!needsAlpha && RenderingUtils.SupportsGraphicsFormat(GraphicsFormat.B10G11R11_UFloatPack32, FormatUsage.Linear, FormatUsage.Render))
517517
return GraphicsFormat.B10G11R11_UFloatPack32;
518-
if (RenderingUtils.SupportsGraphicsFormat(GraphicsFormat.R16G16B16A16_SFloat, FormatUsage.Linear | FormatUsage.Render))
518+
if (RenderingUtils.SupportsGraphicsFormat(GraphicsFormat.R16G16B16A16_SFloat, FormatUsage.Linear, FormatUsage.Render))
519519
return GraphicsFormat.R16G16B16A16_SFloat;
520520
return SystemInfo.GetGraphicsFormat(DefaultFormat.HDR); // This might actually be a LDR format on old devices.
521521
}

0 commit comments

Comments
 (0)