diff --git a/com.unity.render-pipelines.high-definition/Runtime/Lighting/Light/HDGpuLightsBuilder.Jobs.cs b/com.unity.render-pipelines.high-definition/Runtime/Lighting/Light/HDGpuLightsBuilder.Jobs.cs index 6161bf7f537..2f24a43267e 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Lighting/Light/HDGpuLightsBuilder.Jobs.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/Lighting/Light/HDGpuLightsBuilder.Jobs.cs @@ -683,6 +683,12 @@ private void ConvertDirectionalLightToGPUFormat( lightData.flareSize = Mathf.Max(lightRenderData.flareSize * Mathf.Deg2Rad, 5.960464478e-8f); lightData.flareFalloff = lightRenderData.flareFalloff; + + // On some vendors trigonometry has very bad precision, so we precompute what we can on CPU to avoid precision issues (case 1369376). + float radInner = 0.5f * lightData.angularDiameter; + lightData.flareCosInner = Mathf.Cos(radInner); + lightData.flareCosOuter = Mathf.Cos(radInner + lightData.flareSize); + lightData.flareTint = (Vector3)(Vector4)lightRenderData.flareTint; lightData.surfaceTint = (Vector3)(Vector4)lightRenderData.surfaceTint; diff --git a/com.unity.render-pipelines.high-definition/Runtime/Lighting/LightDefinition.cs b/com.unity.render-pipelines.high-definition/Runtime/Lighting/LightDefinition.cs index 60ac297011c..03d4c59d05a 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Lighting/LightDefinition.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/Lighting/LightDefinition.cs @@ -99,12 +99,16 @@ struct DirectionalLightData public float diffuseDimmer; public float specularDimmer; + public float penumbraTint; public float isRayTracedContactShadow; public float distanceFromCamera; // -1 -> no sky interaction public float angularDiameter; // Units: radians + public float flareFalloff; + public float flareCosInner; + public float flareCosOuter; public float __unused__; public Vector3 flareTint; diff --git a/com.unity.render-pipelines.high-definition/Runtime/Lighting/LightDefinition.cs.hlsl b/com.unity.render-pipelines.high-definition/Runtime/Lighting/LightDefinition.cs.hlsl index bbf1e994bc2..f6a26c97c57 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Lighting/LightDefinition.cs.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/Lighting/LightDefinition.cs.hlsl @@ -80,6 +80,8 @@ struct DirectionalLightData float distanceFromCamera; float angularDiameter; float flareFalloff; + float flareCosInner; + float flareCosOuter; float __unused__; float3 flareTint; float flareSize; diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDStringConstants.cs b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDStringConstants.cs index 5cde0415cc3..aa8f90bb31d 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDStringConstants.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDStringConstants.cs @@ -581,7 +581,6 @@ static class HDShaderIDs public static readonly int _SpaceEmissionMultiplier = Shader.PropertyToID("_SpaceEmissionMultiplier"); public static readonly int _RenderSunDisk = Shader.PropertyToID("_RenderSunDisk"); - public static readonly int _SunDiskCosines = Shader.PropertyToID("_SunDiskCosines"); public static readonly int _ColorSaturation = Shader.PropertyToID("_ColorSaturation"); public static readonly int _AlphaSaturation = Shader.PropertyToID("_AlphaSaturation"); diff --git a/com.unity.render-pipelines.high-definition/Runtime/Sky/PhysicallyBasedSky/PhysicallyBasedSky.shader b/com.unity.render-pipelines.high-definition/Runtime/Sky/PhysicallyBasedSky/PhysicallyBasedSky.shader index 4dfea227c0e..98e859f9ff6 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Sky/PhysicallyBasedSky/PhysicallyBasedSky.shader +++ b/com.unity.render-pipelines.high-definition/Runtime/Sky/PhysicallyBasedSky/PhysicallyBasedSky.shader @@ -4,6 +4,7 @@ Shader "Hidden/HDRP/Sky/PbrSky" #pragma vertex Vert + // #pragma enable_d3d11_debug_symbols #pragma editor_sync_compilation #pragma target 4.5 #pragma only_renderers d3d11 playstation xboxone xboxseries vulkan metal switch @@ -25,13 +26,6 @@ Shader "Hidden/HDRP/Sky/PbrSky" float _GroundEmissionMultiplier; float _SpaceEmissionMultiplier; - // Inner and outer cosine computed as: - // float radInner = 0.5 * light.angularDiameter - // float cosInner = cos(radInner); // (In _SunDiskCosines.x) - // float cosOuter = cos(radInner + light.flareSize); // (In _SunDiskCosines.y) - // We need to pass it over instead of computing it here because on some vendors trigonometry has very bad precision, so we precompute what we can on CPU to have better precision. - float4 _SunDiskCosines; - // Sky framework does not set up global shader variables (even per-view ones), // so they can contain garbage. It's very difficult to not include them, however, // since the sky framework includes them internally in many header files. @@ -118,19 +112,16 @@ Shader "Hidden/HDRP/Sky/PbrSky" float rad = acos(LdotV); float radInner = 0.5 * light.angularDiameter; - float cosInner = _SunDiskCosines.x; - float cosOuter = _SunDiskCosines.y; - - float solidAngle = TWO_PI * (1 - cosInner); + float solidAngle = TWO_PI * (1 - light.flareCosInner); - if (LdotV >= cosOuter) + if (LdotV >= light.flareCosOuter) { // Sun flare is visible. Sun disk may or may not be visible. // Assume uniform emission. float3 color = light.color.rgb; float scale = rcp(solidAngle); - if (LdotV >= cosInner) // Sun disk. + if (LdotV >= light.flareCosInner) // Sun disk. { tFrag = lightDist; diff --git a/com.unity.render-pipelines.high-definition/Runtime/Sky/PhysicallyBasedSky/PhysicallyBasedSkyRenderer.cs b/com.unity.render-pipelines.high-definition/Runtime/Sky/PhysicallyBasedSky/PhysicallyBasedSkyRenderer.cs index 54898c0321b..19ebc2d518d 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Sky/PhysicallyBasedSky/PhysicallyBasedSkyRenderer.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/Sky/PhysicallyBasedSky/PhysicallyBasedSkyRenderer.cs @@ -500,14 +500,6 @@ public override void RenderSky(BuiltinSkyParameters builtinParams, bool renderFo } s_PbrSkyMaterialProperties.SetInt(HDShaderIDs._HasSpaceEmissionTexture, hasSpaceEmissionTexture); - // We need to pass it over instead of computing it here because on some vendors trigonometry has very bad precision, so we precompute what we can on CPU to have better precision. - // We can safely retrieve HDAdditionalLightData as for PBR sky the sunlight is always going to be an HDRP light. - var lightData = builtinParams.sunLight.gameObject.GetComponent(); - float radInner = 0.5f * lightData.angularDiameter * Mathf.Deg2Rad; - float cosInner = Mathf.Cos(radInner); - float cosOuter = Mathf.Cos(radInner + lightData.flareSize); - s_PbrSkyMaterialProperties.SetVector(HDShaderIDs._SunDiskCosines, new Vector4(cosInner, cosOuter, 0, 0)); - s_PbrSkyMaterialProperties.SetInt(HDShaderIDs._RenderSunDisk, renderSunDisk ? 1 : 0); int pass = (renderForCubemap ? 0 : 2); diff --git a/com.unity.template-hd/Assets/Scenes/SampleScene.unity b/com.unity.template-hd/Assets/Scenes/SampleScene.unity index e73ae786441..fe71edce88c 100644 --- a/com.unity.template-hd/Assets/Scenes/SampleScene.unity +++ b/com.unity.template-hd/Assets/Scenes/SampleScene.unity @@ -28899,9 +28899,9 @@ MonoBehaviour: m_UseScreenSpaceShadows: 0 m_InteractsWithSky: 1 m_AngularDiameter: 0.53 - m_FlareSize: 0 - m_FlareTint: {r: 0, g: 0, b: 0, a: 1} - m_FlareFalloff: 0 + m_FlareSize: 2 + m_FlareTint: {r: 1, g: 1, b: 1, a: 1} + m_FlareFalloff: 4 m_SurfaceTexture: {fileID: 0} m_SurfaceTint: {r: 1, g: 1, b: 1, a: 1} m_Distance: 150000000