From 863658cac661087a31c9fb908573fcf657fbfd9d Mon Sep 17 00:00:00 2001 From: John Parsaie Date: Mon, 15 Mar 2021 16:19:08 -0400 Subject: [PATCH 1/5] Apply the nan guard fix --- .../Runtime/Material/Decal/Decal.hlsl | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/com.unity.render-pipelines.high-definition/Runtime/Material/Decal/Decal.hlsl b/com.unity.render-pipelines.high-definition/Runtime/Material/Decal/Decal.hlsl index 6d5bc6229f4..2ce3dc71d66 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Material/Decal/Decal.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/Material/Decal/Decal.hlsl @@ -82,6 +82,11 @@ void DecodeFromDBuffer( // Range goes from -0.99607 to 1.0039 surfaceData.normalWS.xyz = inDBuffer1.xyz * 2.0 - (254.0 / 255.0); surfaceData.normalWS.w = inDBuffer1.w; + + // Case 1317162: Apply a NaN guard via the normal blend mask. + // An epsilon will prevent a situation where two opposing normals (surface & decal) produce a 0 normal from blending. + surfaceData.normalWS.w += 1e-5; + surfaceData.mask = inDBuffer2; #ifdef DECALS_4RT surfaceData.MAOSBlend = inDBuffer3; From d13c4f8657ab580f1ebf2cee41921264ddff75a6 Mon Sep 17 00:00:00 2001 From: John Parsaie Date: Mon, 15 Mar 2021 16:20:23 -0400 Subject: [PATCH 2/5] Changelog --- com.unity.render-pipelines.high-definition/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/com.unity.render-pipelines.high-definition/CHANGELOG.md b/com.unity.render-pipelines.high-definition/CHANGELOG.md index 3b163913f4c..b6937e61acf 100644 --- a/com.unity.render-pipelines.high-definition/CHANGELOG.md +++ b/com.unity.render-pipelines.high-definition/CHANGELOG.md @@ -94,6 +94,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - Fixed gizmo rendering when wireframe mode is selected. - Fixed issue in path tracing, where objects would cast shadows even if not present in the path traced layers (case 1318857). - Fixed SRP batcher not compatible with Decal (case 1311586) +- Fixed an issue with Decal normal blending producing NaNs. ### Changed - Changed Window/Render Pipeline/HD Render Pipeline Wizard to Window/Rendering/HDRP Wizard From 7d0c23da0f6019dc93048ba4a89b20972292862e Mon Sep 17 00:00:00 2001 From: John Parsaie Date: Mon, 15 Mar 2021 16:49:41 -0400 Subject: [PATCH 3/5] Move the epsilon onto the same line --- .../Runtime/Material/Decal/Decal.hlsl | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/com.unity.render-pipelines.high-definition/Runtime/Material/Decal/Decal.hlsl b/com.unity.render-pipelines.high-definition/Runtime/Material/Decal/Decal.hlsl index 2ce3dc71d66..7a7111c7c00 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Material/Decal/Decal.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/Material/Decal/Decal.hlsl @@ -81,11 +81,10 @@ void DecodeFromDBuffer( // Use (254.0 / 255.0) instead of 0.5 to allow to encode 0 perfectly (encode as 127) // Range goes from -0.99607 to 1.0039 surfaceData.normalWS.xyz = inDBuffer1.xyz * 2.0 - (254.0 / 255.0); - surfaceData.normalWS.w = inDBuffer1.w; // Case 1317162: Apply a NaN guard via the normal blend mask. // An epsilon will prevent a situation where two opposing normals (surface & decal) produce a 0 normal from blending. - surfaceData.normalWS.w += 1e-5; + surfaceData.normalWS.w = inDBuffer1.w + 1e-5; surfaceData.mask = inDBuffer2; #ifdef DECALS_4RT From c239490c5c34678786217b6ca455f8f0713839d8 Mon Sep 17 00:00:00 2001 From: John Parsaie Date: Wed, 17 Mar 2021 10:43:24 -0400 Subject: [PATCH 4/5] Remove the nan fix via normal blend weight epsilon --- .../Runtime/Material/Decal/Decal.hlsl | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/com.unity.render-pipelines.high-definition/Runtime/Material/Decal/Decal.hlsl b/com.unity.render-pipelines.high-definition/Runtime/Material/Decal/Decal.hlsl index 7a7111c7c00..619fa2d5132 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Material/Decal/Decal.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/Material/Decal/Decal.hlsl @@ -81,10 +81,7 @@ void DecodeFromDBuffer( // Use (254.0 / 255.0) instead of 0.5 to allow to encode 0 perfectly (encode as 127) // Range goes from -0.99607 to 1.0039 surfaceData.normalWS.xyz = inDBuffer1.xyz * 2.0 - (254.0 / 255.0); - - // Case 1317162: Apply a NaN guard via the normal blend mask. - // An epsilon will prevent a situation where two opposing normals (surface & decal) produce a 0 normal from blending. - surfaceData.normalWS.w = inDBuffer1.w + 1e-5; + surfaceData.normalWS.w = inDBuffer1.w; surfaceData.mask = inDBuffer2; #ifdef DECALS_4RT From 746460ec4faea9c8c7912aae7be9a1cab2dcfb28 Mon Sep 17 00:00:00 2001 From: John Parsaie Date: Wed, 17 Mar 2021 10:46:27 -0400 Subject: [PATCH 5/5] Suppress the nan via safe normalize for the zero length normal --- .../Editor/Material/Eye/ShaderGraph/ShaderPass.template.hlsl | 2 +- .../Material/Fabric/ShaderGraph/ShaderPass.template.hlsl | 2 +- .../Editor/Material/Hair/ShaderGraph/ShaderPass.template.hlsl | 2 +- .../Material/StackLit/ShaderGraph/ShaderPass.template.hlsl | 2 +- .../Runtime/Material/AxF/AxFData.hlsl | 4 ++-- .../Runtime/Material/Lit/LitDecalData.hlsl | 2 +- 6 files changed, 7 insertions(+), 7 deletions(-) diff --git a/com.unity.render-pipelines.high-definition/Editor/Material/Eye/ShaderGraph/ShaderPass.template.hlsl b/com.unity.render-pipelines.high-definition/Editor/Material/Eye/ShaderGraph/ShaderPass.template.hlsl index 292399ba400..71a718bec55 100644 --- a/com.unity.render-pipelines.high-definition/Editor/Material/Eye/ShaderGraph/ShaderPass.template.hlsl +++ b/com.unity.render-pipelines.high-definition/Editor/Material/Eye/ShaderGraph/ShaderPass.template.hlsl @@ -6,7 +6,7 @@ void ApplyDecalToSurfaceData(DecalSurfaceData decalSurfaceData, float3 vtxNormal // Always test the normal as we can have decompression artifact if (decalSurfaceData.normalWS.w < 1.0) { - surfaceData.normalWS.xyz = normalize(surfaceData.normalWS.xyz * decalSurfaceData.normalWS.w + decalSurfaceData.normalWS.xyz); + surfaceData.normalWS.xyz = SafeNormalize(surfaceData.normalWS.xyz * decalSurfaceData.normalWS.w + decalSurfaceData.normalWS.xyz); } #ifdef DECALS_4RT // only smoothness in 3RT mode diff --git a/com.unity.render-pipelines.high-definition/Editor/Material/Fabric/ShaderGraph/ShaderPass.template.hlsl b/com.unity.render-pipelines.high-definition/Editor/Material/Fabric/ShaderGraph/ShaderPass.template.hlsl index 2d6ca2f1276..42941cc4ef0 100644 --- a/com.unity.render-pipelines.high-definition/Editor/Material/Fabric/ShaderGraph/ShaderPass.template.hlsl +++ b/com.unity.render-pipelines.high-definition/Editor/Material/Fabric/ShaderGraph/ShaderPass.template.hlsl @@ -6,7 +6,7 @@ void ApplyDecalToSurfaceData(DecalSurfaceData decalSurfaceData, float3 vtxNormal // Always test the normal as we can have decompression artifact if (decalSurfaceData.normalWS.w < 1.0) { - surfaceData.normalWS.xyz = normalize(surfaceData.normalWS.xyz * decalSurfaceData.normalWS.w + decalSurfaceData.normalWS.xyz); + surfaceData.normalWS.xyz = SafeNormalize(surfaceData.normalWS.xyz * decalSurfaceData.normalWS.w + decalSurfaceData.normalWS.xyz); } #ifdef DECALS_4RT // only smoothness in 3RT mode diff --git a/com.unity.render-pipelines.high-definition/Editor/Material/Hair/ShaderGraph/ShaderPass.template.hlsl b/com.unity.render-pipelines.high-definition/Editor/Material/Hair/ShaderGraph/ShaderPass.template.hlsl index 69cdc4c59a6..c3888a372d1 100644 --- a/com.unity.render-pipelines.high-definition/Editor/Material/Hair/ShaderGraph/ShaderPass.template.hlsl +++ b/com.unity.render-pipelines.high-definition/Editor/Material/Hair/ShaderGraph/ShaderPass.template.hlsl @@ -6,7 +6,7 @@ void ApplyDecalToSurfaceData(DecalSurfaceData decalSurfaceData, float3 vtxNormal // Always test the normal as we can have decompression artifact if (decalSurfaceData.normalWS.w < 1.0) { - surfaceData.normalWS.xyz = normalize(surfaceData.normalWS.xyz * decalSurfaceData.normalWS.w + decalSurfaceData.normalWS.xyz); + surfaceData.normalWS.xyz = SafeNormalize(surfaceData.normalWS.xyz * decalSurfaceData.normalWS.w + decalSurfaceData.normalWS.xyz); } #ifdef DECALS_4RT // only smoothness in 3RT mode diff --git a/com.unity.render-pipelines.high-definition/Editor/Material/StackLit/ShaderGraph/ShaderPass.template.hlsl b/com.unity.render-pipelines.high-definition/Editor/Material/StackLit/ShaderGraph/ShaderPass.template.hlsl index 6f70d6ec3e6..60a14ce2a3a 100644 --- a/com.unity.render-pipelines.high-definition/Editor/Material/StackLit/ShaderGraph/ShaderPass.template.hlsl +++ b/com.unity.render-pipelines.high-definition/Editor/Material/StackLit/ShaderGraph/ShaderPass.template.hlsl @@ -6,7 +6,7 @@ void ApplyDecalToSurfaceData(DecalSurfaceData decalSurfaceData, float3 vtxNormal // Always test the normal as we can have decompression artifact if (decalSurfaceData.normalWS.w < 1.0) { - surfaceData.normalWS.xyz = normalize(surfaceData.normalWS.xyz * decalSurfaceData.normalWS.w + decalSurfaceData.normalWS.xyz); + surfaceData.normalWS.xyz = SafeNormalize(surfaceData.normalWS.xyz * decalSurfaceData.normalWS.w + decalSurfaceData.normalWS.xyz); } // TODOTODO: _MATERIAL_FEATURE_SPECULAR_COLOR and _MATERIAL_FEATURE_HAZY_GLOSS diff --git a/com.unity.render-pipelines.high-definition/Runtime/Material/AxF/AxFData.hlsl b/com.unity.render-pipelines.high-definition/Runtime/Material/AxF/AxFData.hlsl index 108f7d376c8..d785529bf9d 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Material/AxF/AxFData.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/Material/AxF/AxFData.hlsl @@ -537,8 +537,8 @@ void ApplyDecalToSurfaceData(DecalSurfaceData decalSurfaceData, float3 vtxNormal if (decalSurfaceData.normalWS.w < 1.0) { // Affect both normal and clearcoat normal - surfaceData.normalWS.xyz = normalize(surfaceData.normalWS.xyz * decalSurfaceData.normalWS.w + decalSurfaceData.normalWS.xyz); - surfaceData.clearcoatNormalWS = normalize(surfaceData.clearcoatNormalWS.xyz * decalSurfaceData.normalWS.w + decalSurfaceData.normalWS.xyz); + surfaceData.normalWS.xyz = SafeNormalize(surfaceData.normalWS.xyz * decalSurfaceData.normalWS.w + decalSurfaceData.normalWS.xyz); + surfaceData.clearcoatNormalWS = SafeNormalize(surfaceData.clearcoatNormalWS.xyz * decalSurfaceData.normalWS.w + decalSurfaceData.normalWS.xyz); } #ifdef DECALS_4RT // only smoothness in 3RT mode diff --git a/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/LitDecalData.hlsl b/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/LitDecalData.hlsl index 2ec0165034d..af6e087cf2a 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/LitDecalData.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/LitDecalData.hlsl @@ -8,7 +8,7 @@ void ApplyDecalToSurfaceData(DecalSurfaceData decalSurfaceData, float3 vtxNormal // Always test the normal as we can have decompression artifact if (decalSurfaceData.normalWS.w < 1.0) { - surfaceData.normalWS.xyz = normalize(surfaceData.normalWS.xyz * decalSurfaceData.normalWS.w + decalSurfaceData.normalWS.xyz); + surfaceData.normalWS.xyz = SafeNormalize(surfaceData.normalWS.xyz * decalSurfaceData.normalWS.w + decalSurfaceData.normalWS.xyz); } #ifdef DECALS_4RT // only smoothness in 3RT mode