Skip to content

[HDRP][Compositor] Fix issues with bloom, alpha and HDR layers in the compositor #2083

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Oct 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,353 changes: 1,152 additions & 201 deletions TestProjects/HDRP_Tests/Assets/GraphicTests/Scenes/9x_Other/9800_Compositor.unity

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_ObjectHideFlags: 8
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
Expand All @@ -18,18 +18,34 @@ MonoBehaviour:
value: {x: 0, y: 0, z: 0, w: 0}
rangeLimits: {x: 0, y: 0}
flags: 4
canBeUsedAsRT: 0
- propertyName: Layer1
propertyType: 4
value: {x: 0, y: 0, z: 0, w: 0}
rangeLimits: {x: 0, y: 0}
flags: 4
canBeUsedAsRT: 0
- propertyName: Logo
propertyType: 4
value: {x: 0, y: 0, z: 0, w: 0}
rangeLimits: {x: 0, y: 0}
flags: 4
canBeUsedAsRT: 0
- propertyName: GreenScreen
propertyType: 4
value: {x: 0, y: 0, z: 0, w: 0}
rangeLimits: {x: 0, y: 0}
flags: 4
canBeUsedAsRT: 0
- propertyName: AOVStack
propertyType: 4
value: {x: 0, y: 0, z: 0, w: 0}
rangeLimits: {x: 0, y: 0}
flags: 4
canBeUsedAsRT: 1
- propertyName: UILayer
propertyType: 4
value: {x: 0, y: 0, z: 0, w: 0}
rangeLimits: {x: 0, y: 0}
flags: 4
canBeUsedAsRT: 1

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions com.unity.render-pipelines.high-definition/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Added support for camera relative ray tracing (and keeping non-camera relative ray tracing working)
- Added a rough refraction option on planar reflections.
- Added scalability settings for the planar reflection resolution.
- Added tests for AOV stacking and UI rendering in the graphics compositor.

### Fixed
- Fixed several issues with physically-based DoF (TAA ghosting of the CoC buffer, smooth layer transitions, etc)
Expand Down Expand Up @@ -139,6 +140,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Fixed backface selection on some shader not ignore correctly.
- Disable quad overdraw on ps4.
- Fixed error when resizing the graphics compositor's output and when re-adding a compositor in the scene
- Fixed issues with bloom, alpha and HDR layers in the compositor (case 1272621).
- Fixed alpha not having TAA applied to it.
- Fix issue with alpha output in forward.

### Changed
- Preparation pass for RTSSShadows to be supported by render graph.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public enum LayerType
public enum UIColorBufferFormat
{
R11G11B10 = GraphicsFormat.B10G11R11_UFloatPack32,
R16G16B16A16 = GraphicsFormat.R16G16B16A16_UNorm,
R16G16B16A16 = GraphicsFormat.R16G16B16A16_SFloat,
R32G32B32A32 = GraphicsFormat.R32G32B32A32_SFloat
};

Expand Down Expand Up @@ -264,6 +264,14 @@ public void Init(string layerID = "")
m_ClearsBackGround = false;
m_LayerPositionInStack = 0; // will be set in SetupLayerCamera

// Migrate any formats that we don't support anymore (like R16G16B16A16_UNORM)
if (m_ColorBufferFormat != UIColorBufferFormat.R11G11B10 &&
m_ColorBufferFormat != UIColorBufferFormat.R16G16B16A16 &&
m_ColorBufferFormat != UIColorBufferFormat.R32G32B32A32)
{
m_ColorBufferFormat = UIColorBufferFormat.R16G16B16A16;
}

if (m_OutputTarget != OutputTarget.CameraStack && m_RenderTarget == null)
{
// If we don't have a valid camera (zero width or height) avoid creating the RT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ float4 ApplyBlendMode(float3 diffuseLighting, float3 specularLighting, float opa
// When doing off screen rendering (low-res transparency or after post process pass) and rendering opaque objects, opacity needs to be set to zero for proper compositing.
if (_OffScreenRendering != 0)
opacity = 0;

#ifndef _ALPHATEST_ON
else
// We hardcode opacity to 1 to avoid issues in forward when alpha might be coming from the texture source, but we don't want to keep it in case alpha is preserved.
opacity = 1;
#endif
return float4(diffuseLighting + specularLighting, opacity);
#else

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3046,6 +3046,8 @@ BloomParameters PrepareBloomParameters(HDCamera camera)
parameters.bloomPrefilterCS.EnableKeyword("HIGH_QUALITY");
else
parameters.bloomPrefilterCS.EnableKeyword("LOW_QUALITY");
if (m_EnableAlpha)
parameters.bloomPrefilterCS.EnableKeyword("ENABLE_ALPHA");

parameters.bloomBlurCS = m_Resources.shaders.bloomBlurCS;
parameters.bloomBlurKernel = parameters.bloomBlurCS.FindKernel("KMain");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl"
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariables.hlsl"
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/Shaders/BloomCommon.hlsl"
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/Shaders/PostProcessDefines.hlsl"

#pragma only_renderers d3d11 playstation xboxone vulkan metal switch

#pragma kernel KMain

#pragma multi_compile LOW_QUALITY HIGH_QUALITY
#pragma multi_compile _ ENABLE_ALPHA

TEXTURE2D_X(_InputTexture);

RW_TEXTURE2D_X(float3, _OutputTexture);
RW_TEXTURE2D_X(CTYPE, _OutputTexture);

CBUFFER_START(cb0)
float4 _TexelSize;
Expand All @@ -20,11 +22,16 @@ CBUFFER_END

float3 BilinearSample(float2 uv, float2 offset, out float weight)
{
float3 c = SAMPLE_TEXTURE2D_X_LOD(_InputTexture, s_linear_clamp_sampler, ClampAndScaleUVForBilinear(uv + offset * _ScreenSize.zw), 0.0).xyz;
c = QuadraticThreshold(c, _BloomThreshold.x, _BloomThreshold.yzw);
CTYPE c = SAMPLE_TEXTURE2D_X_LOD(_InputTexture, s_linear_clamp_sampler, ClampAndScaleUVForBilinear(uv + offset * _ScreenSize.zw), 0.0).CTYPE_SWIZZLE;
#ifdef ENABLE_ALPHA
// When alpha is enabled, regions with zero alpha should not generate any bloom / glow. Therefore we pre-multipy the color with the alpha channel here and the rest
// of the computations remain float3. Still, when bloom is applied to the final image, bloom will still be spread on regions with zero alpha (see UberPost.compute)
c.xyz *= c.w;
#endif
c.xyz = QuadraticThreshold(c.xyz, _BloomThreshold.x, _BloomThreshold.yzw);

weight = 1.0 / (Luminance(c) + 1.0);
return c;
weight = 1.0 / (Luminance(c.xyz) + 1.0);
return c.xyz;
}

float3 PartialAverage(float3 c0, float3 c1, float3 c2, float3 c3, float w0, float w1, float w2, float w3)
Expand Down Expand Up @@ -98,5 +105,9 @@ void KMain(uint3 dispatchThreadId : SV_DispatchThreadID)
// Guard bands
output *= all(dispatchThreadId.xy <= uint2(_TexelSize.xy));

#ifdef ENABLE_ALPHA
_OutputTexture[COORD_TEXTURE2D_X(posInputs.positionSS)] = float4(output,1);
#else
_OutputTexture[COORD_TEXTURE2D_X(posInputs.positionSS)] = output;
#endif
}
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,10 @@ Shader "Hidden/HDRP/TemporalAA"

color.xyz = ConvertToOutputSpace(finalColor.xyz);
color.xyz = clamp(color.xyz, 0, CLAMP_MAX);
#if defined(ENABLE_ALPHA)
// Set output alpha to the antialiased alpha.
color.w = filteredColor.w;
#endif

_OutputHistoryTexture[COORD_TEXTURE2D_X(input.positionCS.xy)] = color.CTYPE_SWIZZLE;
outColor = color.CTYPE_SWIZZLE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,11 @@ void Uber(uint3 dispatchThreadId : SV_DispatchThreadID)
float3 dirt = SAMPLE_TEXTURE2D_LOD(_BloomDirtTexture, sampler_LinearClamp, uvDistorted * DirtScale + DirtOffset, 0.0).xyz;
color.xyz += bloom * dirt * DirtIntensity;
}

#ifdef ENABLE_ALPHA
// Bloom should also spread in areas with zero alpha, so we save the image with bloom here to do the mixing at the end of the shader
inputColor.xyz = color.xyz;
#endif
}

// Vignette
Expand Down