Skip to content

Commit 81a1ed4

Browse files
eh-unityEvergreen
authored andcommitted
Graphics/urp/alpha output
Post-processing alpha handling and output when the render target has an alpha channel and post-process alpha processing is enabled from the asset. The feature is opt-in. URP alpha output semantics are similar to HDRP. Enabled by toggling post-process alpha-processing from URPAsset->Post-Processing->Alpha Processing. ![image](https://media.github.cds.internal.unity3d.com/user/2125/files/243baf93-35d6-4d34-959a-c9af140cd841) Alpha output is disabled by default to avoid regressions in existing projects. If disabled, the alpha output shaders are stripped. Alpha output warns if enabled, but the back-buffer format does not support alpha. ![image](https://media.github.cds.internal.unity3d.com/user/2125/files/ff56b253-13d6-433a-ad6f-b9c6f061f737) Alpha output requires selecting a back-buffer format with alpha from URPAsset->Quality->HDR/HDR Precision. ![image](https://media.github.cds.internal.unity3d.com/user/2125/files/1bb561fb-1947-4a10-9725-d2dbb77c7da0) | Setting | Format. |Alpha Output| |------------|-----------|-------------| | SDR 32-bit | RGBA8 | Yes | | HDR 32-bit | RGB11f | No | | HDR 64-bit | RGBA16f  | Yes | [**URP-1586**](https://jira.unity3d.com/browse/URP-1586) [**POI-1037**](https://jira.unity3d.com/browse/POI-1037)
1 parent 1fe7aea commit 81a1ed4

File tree

71 files changed

+8683
-322
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+8683
-322
lines changed

Packages/com.unity.render-pipelines.universal/Editor/ShaderBuildPreprocessor.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ enum ShaderFeatures : long
6969
SoftShadowsLow = (1L << 46),
7070
SoftShadowsMedium = (1L << 47),
7171
SoftShadowsHigh = (1L << 48),
72+
AlphaOutput = (1L << 49),
7273

7374
}
7475

@@ -453,6 +454,16 @@ internal static ShaderFeatures GetSupportedShaderFeaturesFromAsset(
453454
if (urpAsset.gpuResidentDrawerMode != GPUResidentDrawerMode.Disabled)
454455
urpAssetShaderFeatures |= ShaderFeatures.UseLegacyLightmaps;
455456

457+
// URP post-processing and alpha output follows the back-buffer color format requested in the asset.
458+
// Back-buffer alpha format is required. Or a render texture with alpha formats.
459+
// Without any external option we would need to keep all shaders and assume potential alpha output for all projects.
460+
// Therefore we strip the shader based on the asset enabling the alpha output for post-processing.
461+
// Alpha backbuffer is supported for:
462+
// SDR 32-bit, RGBA8, (!urpAsset.supportsHDR)
463+
// HDR 64-bit, RGBA16Float, (urpAsset.supportsHDR && urpAsset.hdrColorBufferPrecision == HDRColorBufferPrecision._64Bits)
464+
if(urpAsset.allowPostProcessAlphaOutput)
465+
urpAssetShaderFeatures |= ShaderFeatures.AlphaOutput;
466+
456467
// Check each renderer & renderer feature
457468
urpAssetShaderFeatures = GetSupportedShaderFeaturesFromRenderers(
458469
ref urpAsset,
@@ -856,6 +867,7 @@ ref List<ScreenSpaceAmbientOcclusionSettings> ssaoRendererFeatures
856867
spd.stripSoftShadowsQualityMedium = !IsFeatureEnabled(shaderFeatures, ShaderFeatures.SoftShadowsMedium);
857868
spd.stripSoftShadowsQualityHigh = !IsFeatureEnabled(shaderFeatures, ShaderFeatures.SoftShadowsHigh);
858869
spd.stripHDRKeywords = stripHDR;
870+
spd.stripAlphaOutputKeywords = !IsFeatureEnabled(shaderFeatures, ShaderFeatures.AlphaOutput);
859871
spd.stripDebugDisplay = stripDebug;
860872
spd.stripScreenCoordOverride = stripScreenCoord;
861873

Packages/com.unity.render-pipelines.universal/Editor/UniversalRenderPipelineAsset/SerializedUniversalRenderPipelineAsset.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ internal class SerializedUniversalRenderPipelineAsset
7878

7979
public SerializedProperty colorGradingMode { get; }
8080
public SerializedProperty colorGradingLutSize { get; }
81+
public SerializedProperty allowPostProcessAlphaOutput { get; }
8182
public SerializedProperty useFastSRGBLinearConversion { get; }
8283
public SerializedProperty supportDataDrivenLensFlare { get; }
8384
public SerializedProperty supportScreenSpaceLensFlare { get; }
@@ -172,6 +173,7 @@ public SerializedUniversalRenderPipelineAsset(SerializedObject serializedObject)
172173
colorGradingMode = serializedObject.FindProperty("m_ColorGradingMode");
173174
colorGradingLutSize = serializedObject.FindProperty("m_ColorGradingLutSize");
174175

176+
allowPostProcessAlphaOutput = serializedObject.FindProperty("m_AllowPostProcessAlphaOutput");
175177
useFastSRGBLinearConversion = serializedObject.FindProperty("m_UseFastSRGBLinearConversion");
176178
supportDataDrivenLensFlare = serializedObject.FindProperty("m_SupportDataDrivenLensFlare");
177179
supportScreenSpaceLensFlare = serializedObject.FindProperty("m_SupportScreenSpaceLensFlare");

Packages/com.unity.render-pipelines.universal/Editor/UniversalRenderPipelineAsset/UniversalRenderPipelineAssetUI.Drawers.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,12 @@ static void DrawPostProcessing(SerializedUniversalRenderPipelineAsset serialized
617617
if (isHdrOn && serialized.colorGradingMode.intValue == (int)ColorGradingMode.HighDynamicRange && serialized.colorGradingLutSize.intValue < 32)
618618
EditorGUILayout.HelpBox(Styles.colorGradingLutSizeWarning, MessageType.Warning);
619619

620+
HDRColorBufferPrecision hdrPrecision = (HDRColorBufferPrecision)serialized.hdrColorBufferPrecisionProp.intValue;
621+
bool alphaEnabled = !isHdrOn /*RGBA8*/ || (isHdrOn && hdrPrecision == HDRColorBufferPrecision._64Bits); /*RGBA16Float*/
622+
EditorGUILayout.PropertyField(serialized.allowPostProcessAlphaOutput, Styles.allowPostProcessAlphaOutput);
623+
if(!alphaEnabled && serialized.allowPostProcessAlphaOutput.boolValue)
624+
EditorGUILayout.HelpBox(Styles.alphaOutputWarning, MessageType.Warning);
625+
620626
EditorGUILayout.PropertyField(serialized.useFastSRGBLinearConversion, Styles.useFastSRGBLinearConversion);
621627
EditorGUILayout.PropertyField(serialized.supportDataDrivenLensFlare, Styles.supportDataDrivenLensFlare);
622628
EditorGUILayout.PropertyField(serialized.supportScreenSpaceLensFlare, Styles.supportScreenSpaceLensFlare);

Packages/com.unity.render-pipelines.universal/Editor/UniversalRenderPipelineAsset/UniversalRenderPipelineAssetUI.Skin.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,11 @@ internal static class Styles
108108
// Post-processing
109109
public static GUIContent colorGradingMode = EditorGUIUtility.TrTextContent("Grading Mode", "Defines how color grading will be applied. Operators will react differently depending on the mode.");
110110
public static GUIContent colorGradingLutSize = EditorGUIUtility.TrTextContent("LUT size", "Sets the size of the internal and external color grading lookup textures (LUTs).");
111+
public static GUIContent allowPostProcessAlphaOutput = EditorGUIUtility.TrTextContent("Alpha Processing", "When enabled, post-processing outputs alpha channel if available. Alpha 0 preserves the original color. Otherwise post-processing applies to the alpha channel as well. Results may vary depending on the effect.");
111112
public static GUIContent useFastSRGBLinearConversion = EditorGUIUtility.TrTextContent("Fast sRGB/Linear conversions", "Use faster, but less accurate approximation functions when converting between the sRGB and Linear color spaces.");
112113
public static GUIContent supportDataDrivenLensFlare = EditorGUIUtility.TrTextContent("Data Driven Lens Flare", "When enabled, URP allocates shader variants and memory for Data Driven Lens Flare effect.");
113114
public static GUIContent supportScreenSpaceLensFlare = EditorGUIUtility.TrTextContent("Screen Space Lens Flare", "When enabled, URP allocates shader variants and memory for Screen Space Lens Flare effect.");
115+
public static string alphaOutputWarning = "Camera back-buffer format does not support alpha channel. Final output will be opaque.";
114116
public static string colorGradingModeWarning = "HDR rendering is required to use the high dynamic range color grading mode. The low dynamic range will be used instead.";
115117
public static string colorGradingModeWithHDROutput = "With the current configuration, Unity uses the HDR color grading mode when outputting to an HDR display.";
116118
public static string colorGradingModeSpecInfo = "The high dynamic range color grading mode works best on platforms that support floating point textures.";

Packages/com.unity.render-pipelines.universal/Runtime/2D/Renderer2D.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public Renderer2D(Renderer2DData data) : base(data)
9494

9595
var ppParams = PostProcessParams.Create();
9696
ppParams.blitMaterial = m_BlitMaterial;
97-
ppParams.requestHDRFormat = GraphicsFormat.B10G11R11_UFloatPack32;
97+
ppParams.requestColorFormat = GraphicsFormat.B10G11R11_UFloatPack32;
9898
m_PostProcessPasses = new PostProcessPasses(data.postProcessData, ref ppParams);
9999

100100
m_UseDepthStencilBuffer = data.useDepthStencilBuffer;

Packages/com.unity.render-pipelines.universal/Runtime/Data/UniversalRenderPipelineAsset.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,10 @@ public partial class UniversalRenderPipelineAsset : RenderPipelineAsset<Universa
598598
// Post-processing settings
599599
[SerializeField] ColorGradingMode m_ColorGradingMode = ColorGradingMode.LowDynamicRange;
600600
[SerializeField] int m_ColorGradingLutSize = 32;
601+
#if UNITY_EDITOR // multi_compile_fragment _ _ENABLE_ALPHA_OUTPUT
602+
[ShaderKeywordFilter.SelectOrRemove(true, keywordNames: ShaderKeywordStrings._ENABLE_ALPHA_OUTPUT)]
603+
#endif
604+
[SerializeField] bool m_AllowPostProcessAlphaOutput = false;
601605
#if UNITY_EDITOR // multi_compile_local_fragment _ _USE_FAST_SRGB_LINEAR_CONVERSION
602606
[ShaderKeywordFilter.SelectOrRemove(true, keywordNames: ShaderKeywordStrings.UseFastSRGBLinearConversion)]
603607
#endif
@@ -1584,6 +1588,11 @@ public int colorGradingLutSize
15841588
set => m_ColorGradingLutSize = Mathf.Clamp(value, k_MinLutSize, k_MaxLutSize);
15851589
}
15861590

1591+
/// <summary>
1592+
/// Returns true if post-processing should process and output alpha. Requires the color target to have an alpha channel.
1593+
/// </summary>
1594+
public bool allowPostProcessAlphaOutput => m_AllowPostProcessAlphaOutput;
1595+
15871596
/// <summary>
15881597
/// Returns true if fast approximation functions are used when converting between the sRGB and Linear color spaces, false otherwise.
15891598
/// </summary>

Packages/com.unity.render-pipelines.universal/Runtime/Data/UniversalRenderPipelineAssetPrefiltering.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,10 @@ internal enum PrefilteringModeAdditionalLights
118118
})]
119119
[SerializeField] private bool m_PrefilterHDROutput = false;
120120

121+
// Alpha Output
122+
[ShaderKeywordFilter.RemoveIf(true, keywordNames: ShaderKeywordStrings._ENABLE_ALPHA_OUTPUT)]
123+
[SerializeField] private bool m_PrefilterAlphaOutput = false;
124+
121125
// Screen Space Ambient Occlusion (SSAO) specific keywords
122126
[ShaderKeywordFilter.RemoveIf(true, keywordNames: ScreenSpaceAmbientOcclusion.k_SourceDepthNormalsKeyword)]
123127
[SerializeField] private bool m_PrefilterSSAODepthNormals = false;
@@ -193,6 +197,7 @@ internal struct ShaderPrefilteringData
193197

194198
public bool stripXRKeywords;
195199
public bool stripHDRKeywords;
200+
public bool stripAlphaOutputKeywords;
196201
public bool stripDebugDisplay;
197202
public bool stripScreenCoordOverride;
198203
public bool stripWriteRenderingLayers;
@@ -231,6 +236,7 @@ internal void UpdateShaderKeywordPrefiltering(ref ShaderPrefilteringData prefilt
231236

232237
m_PrefilterXRKeywords = prefilteringData.stripXRKeywords;
233238
m_PrefilterHDROutput = prefilteringData.stripHDRKeywords;
239+
m_PrefilterAlphaOutput = prefilteringData.stripAlphaOutputKeywords;
234240
m_PrefilterDebugKeywords = prefilteringData.stripDebugDisplay;
235241
m_PrefilterWriteRenderingLayers = prefilteringData.stripWriteRenderingLayers;
236242
m_PrefilterScreenCoord = prefilteringData.stripScreenCoordOverride;

Packages/com.unity.render-pipelines.universal/Runtime/FrameData/UniversalCameraData.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,11 @@ internal Matrix4x4 GetGPUProjectionMatrix(bool renderIntoTexture, int viewIndex
220220
/// </summary>
221221
public bool allowHDROutput;
222222

223+
/// <summary>
224+
/// True if this camera can write the alpha channel. Post-processing uses this. Requires the color target to have an alpha channel.
225+
/// </summary>
226+
public bool isAlphaOutputEnabled;
227+
223228
/// <summary>
224229
/// True if this camera requires to write _CameraDepthTexture.
225230
/// </summary>
@@ -604,6 +609,7 @@ public override void Reset()
604609
isDefaultViewport = false;
605610
isHdrEnabled = false;
606611
allowHDROutput = false;
612+
isAlphaOutputEnabled = false;
607613
requiresDepthTexture = false;
608614
requiresOpaqueTexture = false;
609615
postProcessingRequiresDepthTexture = false;

Packages/com.unity.render-pipelines.universal/Runtime/Passes/FinalBlitPass.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,8 @@ public override void Execute(ScriptableRenderContext context, ref RenderingData
127127
UniversalCameraData cameraData = frameData.Get<UniversalCameraData>();
128128

129129
bool outputsToHDR = renderingData.cameraData.isHDROutputActive;
130-
InitPassData(cameraData, ref m_PassData, outputsToHDR ? BlitType.HDR : BlitType.Core);
130+
bool outputsAlpha = false;
131+
InitPassData(cameraData, ref m_PassData, outputsToHDR ? BlitType.HDR : BlitType.Core, outputsAlpha);
131132

132133
if (m_PassData.blitMaterialData.material == null)
133134
{
@@ -232,6 +233,8 @@ private static void ExecutePass(RasterCommandBuffer cmd, PassData data, RTHandle
232233
// we never want them to show up as wireframe
233234
cmd.SetWireframe(false);
234235

236+
CoreUtils.SetKeyword(data.blitMaterialData.material, ShaderKeywordStrings._ENABLE_ALPHA_OUTPUT, data.enableAlphaOutput);
237+
235238
int shaderPassIndex = source.rt?.filterMode == FilterMode.Bilinear ? data.blitMaterialData.bilinearSamplerPass : data.blitMaterialData.nearestSamplerPass;
236239
Blitter.BlitTexture(cmd, source, scaleBias, data.blitMaterialData.material, shaderPassIndex);
237240
}
@@ -243,6 +246,7 @@ private class PassData
243246
internal int sourceID;
244247
internal Vector4 hdrOutputLuminanceParams;
245248
internal bool requireSrgbConversion;
249+
internal bool enableAlphaOutput;
246250
internal BlitMaterialData blitMaterialData;
247251
internal UniversalCameraData cameraData;
248252
}
@@ -251,10 +255,11 @@ private class PassData
251255
/// Initialize the shared pass data.
252256
/// </summary>
253257
/// <param name="passData"></param>
254-
private void InitPassData(UniversalCameraData cameraData, ref PassData passData, BlitType blitType)
258+
private void InitPassData(UniversalCameraData cameraData, ref PassData passData, BlitType blitType, bool enableAlphaOutput)
255259
{
256260
passData.cameraData = cameraData;
257261
passData.requireSrgbConversion = cameraData.requireSrgbConversion;
262+
passData.enableAlphaOutput = enableAlphaOutput;
258263

259264
passData.blitMaterialData = m_BlitMaterialData[(int)blitType];
260265
}
@@ -273,7 +278,8 @@ internal void Render(RenderGraph renderGraph, UniversalCameraData cameraData, in
273278
}
274279

275280
bool outputsToHDR = cameraData.isHDROutputActive;
276-
InitPassData(cameraData, ref passData, outputsToHDR ? BlitType.HDR : BlitType.Core);
281+
bool outputsAlpha = cameraData.isAlphaOutputEnabled;
282+
InitPassData(cameraData, ref passData, outputsToHDR ? BlitType.HDR : BlitType.Core, outputsAlpha);
277283

278284
passData.sourceID = ShaderPropertyId.sourceTex;
279285
passData.source = src;

0 commit comments

Comments
 (0)