Skip to content

Commit 0827329

Browse files
Fix NaN on Intel GPU when using PBR Sky and rendering sun disk (#6145)
* Enable debug symbols * tentative * test using cos from cpu * Finalize fix * Missing file * Remove debug symbols.
1 parent 5c727d4 commit 0827329

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDStringConstants.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,7 @@ static class HDShaderIDs
574574
public static readonly int _SpaceEmissionMultiplier = Shader.PropertyToID("_SpaceEmissionMultiplier");
575575

576576
public static readonly int _RenderSunDisk = Shader.PropertyToID("_RenderSunDisk");
577+
public static readonly int _SunDiskCosines = Shader.PropertyToID("_SunDiskCosines");
577578

578579
public static readonly int _ColorSaturation = Shader.PropertyToID("_ColorSaturation");
579580
public static readonly int _AlphaSaturation = Shader.PropertyToID("_AlphaSaturation");

com.unity.render-pipelines.high-definition/Runtime/Sky/PhysicallyBasedSky/PhysicallyBasedSky.shader

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ Shader "Hidden/HDRP/Sky/PbrSky"
44

55
#pragma vertex Vert
66

7-
// #pragma enable_d3d11_debug_symbols
87
#pragma editor_sync_compilation
98
#pragma target 4.5
109
#pragma only_renderers d3d11 playstation xboxone xboxseries vulkan metal switch
@@ -26,6 +25,13 @@ Shader "Hidden/HDRP/Sky/PbrSky"
2625
float _GroundEmissionMultiplier;
2726
float _SpaceEmissionMultiplier;
2827

28+
// Inner and outer cosine computed as:
29+
// float radInner = 0.5 * light.angularDiameter
30+
// float cosInner = cos(radInner); // (In _SunDiskCosines.x)
31+
// float cosOuter = cos(radInner + light.flareSize); // (In _SunDiskCosines.y)
32+
// 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.
33+
float4 _SunDiskCosines;
34+
2935
// Sky framework does not set up global shader variables (even per-view ones),
3036
// so they can contain garbage. It's very difficult to not include them, however,
3137
// since the sky framework includes them internally in many header files.
@@ -111,8 +117,9 @@ Shader "Hidden/HDRP/Sky/PbrSky"
111117
float LdotV = -dot(L, V);
112118
float rad = acos(LdotV);
113119
float radInner = 0.5 * light.angularDiameter;
114-
float cosInner = cos(radInner);
115-
float cosOuter = cos(radInner + light.flareSize);
120+
121+
float cosInner = _SunDiskCosines.x;
122+
float cosOuter = _SunDiskCosines.y;
116123

117124
float solidAngle = TWO_PI * (1 - cosInner);
118125

com.unity.render-pipelines.high-definition/Runtime/Sky/PhysicallyBasedSky/PhysicallyBasedSkyRenderer.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,14 @@ public override void RenderSky(BuiltinSkyParameters builtinParams, bool renderFo
500500
}
501501
s_PbrSkyMaterialProperties.SetInt(HDShaderIDs._HasSpaceEmissionTexture, hasSpaceEmissionTexture);
502502

503+
// 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.
504+
// We can safely retrieve HDAdditionalLightData as for PBR sky the sunlight is always going to be an HDRP light.
505+
var lightData = builtinParams.sunLight.gameObject.GetComponent<HDAdditionalLightData>();
506+
float radInner = 0.5f * lightData.angularDiameter * Mathf.Deg2Rad;
507+
float cosInner = Mathf.Cos(radInner);
508+
float cosOuter = Mathf.Cos(radInner + lightData.flareSize);
509+
s_PbrSkyMaterialProperties.SetVector(HDShaderIDs._SunDiskCosines, new Vector4(cosInner, cosOuter, 0, 0));
510+
503511
s_PbrSkyMaterialProperties.SetInt(HDShaderIDs._RenderSunDisk, renderSunDisk ? 1 : 0);
504512

505513
int pass = (renderForCubemap ? 0 : 2);

0 commit comments

Comments
 (0)