From bdc0c52336174f24315abbfc3cf6f795ce7eb204 Mon Sep 17 00:00:00 2001 From: Kleber Garcia Date: Tue, 2 Feb 2021 13:32:19 -0500 Subject: [PATCH 1/7] SCENEPICKING pass was utilizing a camera that had relative rendering on, however world positions in SCENEPICKING are absolute. This causes inconsystencies in tesselation wireframe view. This fix corrects the offset of worldspace on tesselation for SCENEPICKING pass. --- .../ShaderLibrary/Tessellation.hlsl | 17 +++++++++++------ .../Material/Lit/LitDataMeshModification.hlsl | 10 +++++++++- .../Runtime/ShaderLibrary/ShaderVariables.hlsl | 15 +++++++++++++++ 3 files changed, 35 insertions(+), 7 deletions(-) diff --git a/com.unity.render-pipelines.core/ShaderLibrary/Tessellation.hlsl b/com.unity.render-pipelines.core/ShaderLibrary/Tessellation.hlsl index 6174898fee7..dd126a25958 100644 --- a/com.unity.render-pipelines.core/ShaderLibrary/Tessellation.hlsl +++ b/com.unity.render-pipelines.core/ShaderLibrary/Tessellation.hlsl @@ -16,13 +16,8 @@ real3 PhongTessellation(real3 positionWS, real3 p0, real3 p1, real3 p2, real3 n0 // Reference: http://twvideo01.ubm-us.net/o1/vault/gdc10/slides/Bilodeau_Bill_Direct3D11TutorialTessellation.pdf // Compute both screen and distance based adaptation - return factor between 0 and 1 -real3 GetScreenSpaceTessFactor(real3 p0, real3 p1, real3 p2, real4x4 viewProjectionMatrix, real4 screenSize, real triangleSize) +real3 GetScreenSpaceTessFactor(real2 edgeScreenPosition0, real2 edgeScreenPosition1, real2 edgeScreenPosition2, real triangleSize) { - // Get screen space adaptive scale factor - real2 edgeScreenPosition0 = ComputeNormalizedDeviceCoordinates(p0, viewProjectionMatrix) * screenSize.xy; - real2 edgeScreenPosition1 = ComputeNormalizedDeviceCoordinates(p1, viewProjectionMatrix) * screenSize.xy; - real2 edgeScreenPosition2 = ComputeNormalizedDeviceCoordinates(p2, viewProjectionMatrix) * screenSize.xy; - real EdgeScale = 1.0 / triangleSize; // Edge size in reality, but name is simpler real3 tessFactor; tessFactor.x = saturate(distance(edgeScreenPosition1, edgeScreenPosition2) * EdgeScale); @@ -32,6 +27,16 @@ real3 GetScreenSpaceTessFactor(real3 p0, real3 p1, real3 p2, real4x4 viewProject return tessFactor; } +real3 GetScreenSpaceTessFactor(real3 p0, real3 p1, real3 p2, real4x4 viewProjectionMatrix, real4 screenSize, real triangleSize) +{ + // Get screen space adaptive scale factor + real2 edgeScreenPosition0 = ComputeNormalizedDeviceCoordinates(p0, viewProjectionMatrix) * screenSize.xy; + real2 edgeScreenPosition1 = ComputeNormalizedDeviceCoordinates(p1, viewProjectionMatrix) * screenSize.xy; + real2 edgeScreenPosition2 = ComputeNormalizedDeviceCoordinates(p2, viewProjectionMatrix) * screenSize.xy; + + return GetScreenSpaceTessFactor(edgeScreenPosition0, edgeScreenPosition1, edgeScreenPosition2, triangleSize); +} + real3 GetDistanceBasedTessFactor(real3 p0, real3 p1, real3 p2, real3 cameraPosWS, real tessMinDist, real tessMaxDist) { real3 edgePosition0 = 0.5 * (p1 + p2); diff --git a/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/LitDataMeshModification.hlsl b/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/LitDataMeshModification.hlsl index 2151286da7a..2dcb33f0e97 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/LitDataMeshModification.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/LitDataMeshModification.hlsl @@ -108,7 +108,15 @@ float4 GetTessellationFactors(float3 p0, float3 p1, float3 p2, float3 n0, float3 { // return a value between 0 and 1 // Warning: '_ViewProjMatrix' can be the viewproj matrix of the light when we render shadows, that's why we use _CameraViewProjMatrix instead - edgeTessFactors *= GetScreenSpaceTessFactor( p0, p1, p2, _CameraViewProjMatrix, _ScreenSize, _TessellationFactorTriangleSize); // Use primary camera view + + bool isPositionCameraRelative = true; + #if defined(SCENEPICKINGPASS) + isPositionCameraRelative = false; + #endif + float2 sp0 = GetMainCameraPixelCoordFromWorldPosition(p0, isPositionCameraRelative); + float2 sp1 = GetMainCameraPixelCoordFromWorldPosition(p1, isPositionCameraRelative); + float2 sp2 = GetMainCameraPixelCoordFromWorldPosition(p2, isPositionCameraRelative); + edgeTessFactors *= GetScreenSpaceTessFactor(sp0, sp1, sp2, _TessellationFactorTriangleSize); // Use primary camera view } // Distance based tessellation diff --git a/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariables.hlsl b/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariables.hlsl index 6f35c90e717..420aed8055d 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariables.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariables.hlsl @@ -332,6 +332,21 @@ uint Get1DAddressFromPixelCoord(uint2 pixCoord, uint2 screenSize) return Get1DAddressFromPixelCoord(pixCoord, screenSize, 0); } +float2 GetMainCameraPixelCoordFromWorldPosition(float3 positionWS, bool isPositionCameraRelative) +{ + //The _CameraViewProjMatrix obeys the camera relative rendering options. + //Some passes in unity that require main camera position (such as scenepick selection pass) + //Use the standard unity matrices, which might output the world position as absolute. + #if SHADEROPTIONS_CAMERA_RELATIVE_RENDERING != 0 + if (!isPositionCameraRelative) + positionWS -= _WorldSpaceCameraPos.xyz; + #endif + + float4 hp = mul(_CameraViewProjMatrix, float4(positionWS, 1)); + hp *= rcp(hp.w); + return hp.xy * _ScreenSize; +} + // Define Model Matrix Macro // Note: In order to be able to define our macro to forbid usage of unity_ObjectToWorld/unity_WorldToObject // We need to declare inline function. Using uniform directly mean they are expand with the macro From 9ac27a402288f0626795e6e7588d486aeabfdfcd Mon Sep 17 00:00:00 2001 From: Kleber Garcia Date: Tue, 2 Feb 2021 13:45:43 -0500 Subject: [PATCH 2/7] Correct range (0 to 1) in order to get pixel accurage projection value. --- .../Runtime/ShaderLibrary/ShaderVariables.hlsl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariables.hlsl b/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariables.hlsl index 420aed8055d..49fdc146943 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariables.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariables.hlsl @@ -344,7 +344,7 @@ float2 GetMainCameraPixelCoordFromWorldPosition(float3 positionWS, bool isPositi float4 hp = mul(_CameraViewProjMatrix, float4(positionWS, 1)); hp *= rcp(hp.w); - return hp.xy * _ScreenSize; + return (hp.xy * 0.5 + 0.5) * _ScreenSize; } // Define Model Matrix Macro From f26c5f6224e4897e249c7b448e5c4076c5b5e09e Mon Sep 17 00:00:00 2001 From: Kleber Garcia Date: Tue, 2 Feb 2021 15:32:34 -0500 Subject: [PATCH 3/7] Moving function to LitDataMeshModification.hlsl for clarity. --- .../Material/Lit/LitDataMeshModification.hlsl | 27 ++++++++++++------- .../ShaderLibrary/ShaderVariables.hlsl | 15 ----------- 2 files changed, 18 insertions(+), 24 deletions(-) diff --git a/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/LitDataMeshModification.hlsl b/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/LitDataMeshModification.hlsl index 2dcb33f0e97..99e5878f03b 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/LitDataMeshModification.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/LitDataMeshModification.hlsl @@ -49,6 +49,21 @@ void ApplyVertexModification(AttributesMesh input, float3 normalWS, inout float3 #ifdef TESSELLATION_ON +float2 GetMainCameraPixelCoordFromWorldPosition(float3 positionWS) +{ + //The _CameraViewProjMatrix obeys the camera relative rendering options. + //Some passes in unity that require main camera position (such as scenepick selection pass) + //Use the standard unity matrices, which might output the world position as absolute. + #if defined(SCENEPICKINGPASS) && SHADEROPTIONS_CAMERA_RELATIVE_RENDERING != 0 + positionWS -= _WorldSpaceCameraPos.xyz; + #endif + + // Warning: '_ViewProjMatrix' can be the viewproj matrix of the light when we render shadows, that's why we use _CameraViewProjMatrix instead + float2 hp = ComputeNormalizedDeviceCoordinates(positionWS, _CameraViewProjMatrix); + return hp * _ScreenSize; +} + + float4 GetTessellationFactors(float3 p0, float3 p1, float3 p2, float3 n0, float3 n1, float3 n2) { float maxDisplacement = GetMaxDisplacement(); @@ -107,15 +122,9 @@ float4 GetTessellationFactors(float3 p0, float3 p1, float3 p2, float3 n0, float3 if (_TessellationFactorTriangleSize > 0.0) { // return a value between 0 and 1 - // Warning: '_ViewProjMatrix' can be the viewproj matrix of the light when we render shadows, that's why we use _CameraViewProjMatrix instead - - bool isPositionCameraRelative = true; - #if defined(SCENEPICKINGPASS) - isPositionCameraRelative = false; - #endif - float2 sp0 = GetMainCameraPixelCoordFromWorldPosition(p0, isPositionCameraRelative); - float2 sp1 = GetMainCameraPixelCoordFromWorldPosition(p1, isPositionCameraRelative); - float2 sp2 = GetMainCameraPixelCoordFromWorldPosition(p2, isPositionCameraRelative); + float2 sp0 = GetMainCameraPixelCoordFromWorldPosition(p0); + float2 sp1 = GetMainCameraPixelCoordFromWorldPosition(p1); + float2 sp2 = GetMainCameraPixelCoordFromWorldPosition(p2); edgeTessFactors *= GetScreenSpaceTessFactor(sp0, sp1, sp2, _TessellationFactorTriangleSize); // Use primary camera view } diff --git a/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariables.hlsl b/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariables.hlsl index 49fdc146943..6f35c90e717 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariables.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariables.hlsl @@ -332,21 +332,6 @@ uint Get1DAddressFromPixelCoord(uint2 pixCoord, uint2 screenSize) return Get1DAddressFromPixelCoord(pixCoord, screenSize, 0); } -float2 GetMainCameraPixelCoordFromWorldPosition(float3 positionWS, bool isPositionCameraRelative) -{ - //The _CameraViewProjMatrix obeys the camera relative rendering options. - //Some passes in unity that require main camera position (such as scenepick selection pass) - //Use the standard unity matrices, which might output the world position as absolute. - #if SHADEROPTIONS_CAMERA_RELATIVE_RENDERING != 0 - if (!isPositionCameraRelative) - positionWS -= _WorldSpaceCameraPos.xyz; - #endif - - float4 hp = mul(_CameraViewProjMatrix, float4(positionWS, 1)); - hp *= rcp(hp.w); - return (hp.xy * 0.5 + 0.5) * _ScreenSize; -} - // Define Model Matrix Macro // Note: In order to be able to define our macro to forbid usage of unity_ObjectToWorld/unity_WorldToObject // We need to declare inline function. Using uniform directly mean they are expand with the macro From d3aa17783159503b154505bec3f16d3b1fbad1d6 Mon Sep 17 00:00:00 2001 From: Kleber Garcia Date: Wed, 3 Feb 2021 14:29:08 -0500 Subject: [PATCH 4/7] Changing CameraView matrix instead of offsetting the position using macro forwarding. --- .../ShaderLibrary/Tessellation.hlsl | 17 ++++++--------- .../Material/Lit/LitDataMeshModification.hlsl | 21 ++----------------- .../Material/Lit/LitTessellation.shader | 2 +- .../ShaderLibrary/PickingSpaceTransforms.hlsl | 5 +++++ .../ShaderVariablesFunctions.hlsl | 18 ++++++++++++++++ 5 files changed, 32 insertions(+), 31 deletions(-) diff --git a/com.unity.render-pipelines.core/ShaderLibrary/Tessellation.hlsl b/com.unity.render-pipelines.core/ShaderLibrary/Tessellation.hlsl index dd126a25958..6174898fee7 100644 --- a/com.unity.render-pipelines.core/ShaderLibrary/Tessellation.hlsl +++ b/com.unity.render-pipelines.core/ShaderLibrary/Tessellation.hlsl @@ -16,8 +16,13 @@ real3 PhongTessellation(real3 positionWS, real3 p0, real3 p1, real3 p2, real3 n0 // Reference: http://twvideo01.ubm-us.net/o1/vault/gdc10/slides/Bilodeau_Bill_Direct3D11TutorialTessellation.pdf // Compute both screen and distance based adaptation - return factor between 0 and 1 -real3 GetScreenSpaceTessFactor(real2 edgeScreenPosition0, real2 edgeScreenPosition1, real2 edgeScreenPosition2, real triangleSize) +real3 GetScreenSpaceTessFactor(real3 p0, real3 p1, real3 p2, real4x4 viewProjectionMatrix, real4 screenSize, real triangleSize) { + // Get screen space adaptive scale factor + real2 edgeScreenPosition0 = ComputeNormalizedDeviceCoordinates(p0, viewProjectionMatrix) * screenSize.xy; + real2 edgeScreenPosition1 = ComputeNormalizedDeviceCoordinates(p1, viewProjectionMatrix) * screenSize.xy; + real2 edgeScreenPosition2 = ComputeNormalizedDeviceCoordinates(p2, viewProjectionMatrix) * screenSize.xy; + real EdgeScale = 1.0 / triangleSize; // Edge size in reality, but name is simpler real3 tessFactor; tessFactor.x = saturate(distance(edgeScreenPosition1, edgeScreenPosition2) * EdgeScale); @@ -27,16 +32,6 @@ real3 GetScreenSpaceTessFactor(real2 edgeScreenPosition0, real2 edgeScreenPositi return tessFactor; } -real3 GetScreenSpaceTessFactor(real3 p0, real3 p1, real3 p2, real4x4 viewProjectionMatrix, real4 screenSize, real triangleSize) -{ - // Get screen space adaptive scale factor - real2 edgeScreenPosition0 = ComputeNormalizedDeviceCoordinates(p0, viewProjectionMatrix) * screenSize.xy; - real2 edgeScreenPosition1 = ComputeNormalizedDeviceCoordinates(p1, viewProjectionMatrix) * screenSize.xy; - real2 edgeScreenPosition2 = ComputeNormalizedDeviceCoordinates(p2, viewProjectionMatrix) * screenSize.xy; - - return GetScreenSpaceTessFactor(edgeScreenPosition0, edgeScreenPosition1, edgeScreenPosition2, triangleSize); -} - real3 GetDistanceBasedTessFactor(real3 p0, real3 p1, real3 p2, real3 cameraPosWS, real tessMinDist, real tessMaxDist) { real3 edgePosition0 = 0.5 * (p1 + p2); diff --git a/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/LitDataMeshModification.hlsl b/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/LitDataMeshModification.hlsl index 99e5878f03b..141bd4fdf73 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/LitDataMeshModification.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/LitDataMeshModification.hlsl @@ -49,21 +49,6 @@ void ApplyVertexModification(AttributesMesh input, float3 normalWS, inout float3 #ifdef TESSELLATION_ON -float2 GetMainCameraPixelCoordFromWorldPosition(float3 positionWS) -{ - //The _CameraViewProjMatrix obeys the camera relative rendering options. - //Some passes in unity that require main camera position (such as scenepick selection pass) - //Use the standard unity matrices, which might output the world position as absolute. - #if defined(SCENEPICKINGPASS) && SHADEROPTIONS_CAMERA_RELATIVE_RENDERING != 0 - positionWS -= _WorldSpaceCameraPos.xyz; - #endif - - // Warning: '_ViewProjMatrix' can be the viewproj matrix of the light when we render shadows, that's why we use _CameraViewProjMatrix instead - float2 hp = ComputeNormalizedDeviceCoordinates(positionWS, _CameraViewProjMatrix); - return hp * _ScreenSize; -} - - float4 GetTessellationFactors(float3 p0, float3 p1, float3 p2, float3 n0, float3 n1, float3 n2) { float maxDisplacement = GetMaxDisplacement(); @@ -122,10 +107,8 @@ float4 GetTessellationFactors(float3 p0, float3 p1, float3 p2, float3 n0, float3 if (_TessellationFactorTriangleSize > 0.0) { // return a value between 0 and 1 - float2 sp0 = GetMainCameraPixelCoordFromWorldPosition(p0); - float2 sp1 = GetMainCameraPixelCoordFromWorldPosition(p1); - float2 sp2 = GetMainCameraPixelCoordFromWorldPosition(p2); - edgeTessFactors *= GetScreenSpaceTessFactor(sp0, sp1, sp2, _TessellationFactorTriangleSize); // Use primary camera view + // Warning: '_ViewProjMatrix' can be the viewproj matrix of the light when we render shadows, that's why we use GetCameraViewProjMatrix() instead + edgeTessFactors *= GetScreenSpaceTessFactor( p0, p1, p2, GetCameraViewProjMatrix(), _ScreenSize, _TessellationFactorTriangleSize); // Use primary camera view } // Distance based tessellation diff --git a/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/LitTessellation.shader b/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/LitTessellation.shader index 5a829b1cfec..006e368d3dd 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/LitTessellation.shader +++ b/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/LitTessellation.shader @@ -410,9 +410,9 @@ Shader "HDRP/LitTessellation" #define SCENEPICKINGPASS #include "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/Material.hlsl" #include "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/Lit.hlsl" + #include "Packages/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/PickingSpaceTransforms.hlsl" #include "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/ShaderPass/LitDepthPass.hlsl" #include "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/LitData.hlsl" - #include "Packages/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/PickingSpaceTransforms.hlsl" #include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassDepthOnly.hlsl" #pragma vertex Vert diff --git a/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/PickingSpaceTransforms.hlsl b/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/PickingSpaceTransforms.hlsl index 0b3be72d754..9a8648062cb 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/PickingSpaceTransforms.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/PickingSpaceTransforms.hlsl @@ -58,6 +58,11 @@ float4x4 glstate_matrix_projection; #define TransformTangentToObject TransformTangentToObject_Picking #define TransformObjectToTangent TransformObjectToTangent_Picking +#ifdef GetCameraViewProjMatrix +#undef GetCameraViewProjMatrix +#endif + +#define GetCameraViewProjMatrix _GetCameraViewProjMatrixForAbsolute // Redefine the functions using the new macros #undef UNITY_SPACE_TRANSFORMS_INCLUDED diff --git a/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariablesFunctions.hlsl b/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariablesFunctions.hlsl index 3de4892d739..be677c63ea9 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariablesFunctions.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariablesFunctions.hlsl @@ -233,4 +233,22 @@ uint2 ComputeFadeMaskSeed(float3 V, uint2 positionSS) return fadeMaskSeed; } +float4x4 _GetCameraViewProjMatrixRaw() +{ + return _CameraViewProjMatrix; +} + +float4x4 _GetCameraViewProjMatrixForAbsolute() +{ + float4x4 translationMatrix = { + { 1.0 ,0.0 , 0.0, -_WorldSpaceCameraPos.x }, + { 0.0 ,1.0 , 0.0, -_WorldSpaceCameraPos.y }, + { 0.0 ,0.0 , 1.0, -_WorldSpaceCameraPos.z }, + { 0.0 ,0.0 , 0.0, 1.0} }; + + return mul(_GetCameraViewProjMatrixRaw(), translationMatrix); +} + +#define GetCameraViewProjMatrix _GetCameraViewProjMatrixRaw + #endif // UNITY_SHADER_VARIABLES_FUNCTIONS_INCLUDED From 5c7c6ff70bc9dc9e4cc390603fa486d5e03cd112 Mon Sep 17 00:00:00 2001 From: Kleber Garcia Date: Wed, 3 Feb 2021 16:29:01 -0500 Subject: [PATCH 5/7] Redirecting camera matrix more cleanly. --- .../Material/Lit/LitDataMeshModification.hlsl | 4 ++-- .../ShaderLibrary/PickingSpaceTransforms.hlsl | 16 ++++++++++++---- .../ShaderVariablesFunctions.hlsl | 18 ------------------ 3 files changed, 14 insertions(+), 24 deletions(-) diff --git a/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/LitDataMeshModification.hlsl b/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/LitDataMeshModification.hlsl index 141bd4fdf73..2151286da7a 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/LitDataMeshModification.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/LitDataMeshModification.hlsl @@ -107,8 +107,8 @@ float4 GetTessellationFactors(float3 p0, float3 p1, float3 p2, float3 n0, float3 if (_TessellationFactorTriangleSize > 0.0) { // return a value between 0 and 1 - // Warning: '_ViewProjMatrix' can be the viewproj matrix of the light when we render shadows, that's why we use GetCameraViewProjMatrix() instead - edgeTessFactors *= GetScreenSpaceTessFactor( p0, p1, p2, GetCameraViewProjMatrix(), _ScreenSize, _TessellationFactorTriangleSize); // Use primary camera view + // Warning: '_ViewProjMatrix' can be the viewproj matrix of the light when we render shadows, that's why we use _CameraViewProjMatrix instead + edgeTessFactors *= GetScreenSpaceTessFactor( p0, p1, p2, _CameraViewProjMatrix, _ScreenSize, _TessellationFactorTriangleSize); // Use primary camera view } // Distance based tessellation diff --git a/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/PickingSpaceTransforms.hlsl b/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/PickingSpaceTransforms.hlsl index 9a8648062cb..d6d014f6c8e 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/PickingSpaceTransforms.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/PickingSpaceTransforms.hlsl @@ -58,11 +58,19 @@ float4x4 glstate_matrix_projection; #define TransformTangentToObject TransformTangentToObject_Picking #define TransformObjectToTangent TransformObjectToTangent_Picking -#ifdef GetCameraViewProjMatrix -#undef GetCameraViewProjMatrix -#endif +float4x4 ScenePickingGetCameraViewProjMatrix() +{ + float4x4 translationMatrix = { + { 1.0 ,0.0 , 0.0, -_WorldSpaceCameraPos.x }, + { 0.0 ,1.0 , 0.0, -_WorldSpaceCameraPos.y }, + { 0.0 ,0.0 , 1.0, -_WorldSpaceCameraPos.z }, + { 0.0 ,0.0 , 0.0, 1.0} }; + + return mul(_CameraViewProjMatrix, translationMatrix); +} + +#define _CameraViewProjMatrix ScenePickingGetCameraViewProjMatrix() -#define GetCameraViewProjMatrix _GetCameraViewProjMatrixForAbsolute // Redefine the functions using the new macros #undef UNITY_SPACE_TRANSFORMS_INCLUDED diff --git a/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariablesFunctions.hlsl b/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariablesFunctions.hlsl index be677c63ea9..3de4892d739 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariablesFunctions.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariablesFunctions.hlsl @@ -233,22 +233,4 @@ uint2 ComputeFadeMaskSeed(float3 V, uint2 positionSS) return fadeMaskSeed; } -float4x4 _GetCameraViewProjMatrixRaw() -{ - return _CameraViewProjMatrix; -} - -float4x4 _GetCameraViewProjMatrixForAbsolute() -{ - float4x4 translationMatrix = { - { 1.0 ,0.0 , 0.0, -_WorldSpaceCameraPos.x }, - { 0.0 ,1.0 , 0.0, -_WorldSpaceCameraPos.y }, - { 0.0 ,0.0 , 1.0, -_WorldSpaceCameraPos.z }, - { 0.0 ,0.0 , 0.0, 1.0} }; - - return mul(_GetCameraViewProjMatrixRaw(), translationMatrix); -} - -#define GetCameraViewProjMatrix _GetCameraViewProjMatrixRaw - #endif // UNITY_SHADER_VARIABLES_FUNCTIONS_INCLUDED From 72efb4e7e5ac32ce6623211eefb8240cec9799e0 Mon Sep 17 00:00:00 2001 From: Kleber Garcia Date: Wed, 3 Feb 2021 17:17:49 -0500 Subject: [PATCH 6/7] Reordering picking transforms include for proper macro order. --- .../Editor/Material/Decal/ShaderGraph/DecalSubTarget.cs | 2 +- .../Editor/Material/ShaderGraph/HDShaderPasses.cs | 2 +- .../Runtime/Material/AxF/AxF.shader | 2 +- .../Runtime/Material/Decal/Decal.shader | 2 +- .../Runtime/Material/LayeredLit/LayeredLit.shader | 2 +- .../Runtime/Material/LayeredLit/LayeredLitTessellation.shader | 2 +- .../Runtime/Material/Lit/Lit.shader | 2 +- .../Runtime/Material/Lit/LitTessellation.shader | 2 +- 8 files changed, 8 insertions(+), 8 deletions(-) diff --git a/com.unity.render-pipelines.high-definition/Editor/Material/Decal/ShaderGraph/DecalSubTarget.cs b/com.unity.render-pipelines.high-definition/Editor/Material/Decal/ShaderGraph/DecalSubTarget.cs index d2b15c235fb..dadf13148ee 100644 --- a/com.unity.render-pipelines.high-definition/Editor/Material/Decal/ShaderGraph/DecalSubTarget.cs +++ b/com.unity.render-pipelines.high-definition/Editor/Material/Decal/ShaderGraph/DecalSubTarget.cs @@ -587,8 +587,8 @@ static class DecalIncludes { kColor, IncludeLocation.Pregraph }, { kFunctions, IncludeLocation.Pregraph }, { CoreIncludes.MinimalCorePregraph }, - { kDecal, IncludeLocation.Pregraph }, { CoreIncludes.kPickingSpaceTransforms, IncludeLocation.Pregraph }, + { kDecal, IncludeLocation.Pregraph }, { kPassDecal, IncludeLocation.Postgraph }, }; } diff --git a/com.unity.render-pipelines.high-definition/Editor/Material/ShaderGraph/HDShaderPasses.cs b/com.unity.render-pipelines.high-definition/Editor/Material/ShaderGraph/HDShaderPasses.cs index 8d4bfbf6cda..fd644416c85 100644 --- a/com.unity.render-pipelines.high-definition/Editor/Material/ShaderGraph/HDShaderPasses.cs +++ b/com.unity.render-pipelines.high-definition/Editor/Material/ShaderGraph/HDShaderPasses.cs @@ -96,11 +96,11 @@ IncludeCollection GenerateIncludes() { var includes = new IncludeCollection(); + includes.Add(CoreIncludes.kPickingSpaceTransforms, IncludeLocation.Pregraph); includes.Add(CoreIncludes.CorePregraph); includes.Add(CoreIncludes.kPassPlaceholder, IncludeLocation.Pregraph); includes.Add(CoreIncludes.CoreUtility); includes.Add(CoreIncludes.kShaderGraphFunctions, IncludeLocation.Pregraph); - includes.Add(CoreIncludes.kPickingSpaceTransforms, IncludeLocation.Pregraph); includes.Add(CoreIncludes.kPassDepthOnly, IncludeLocation.Postgraph); return includes; diff --git a/com.unity.render-pipelines.high-definition/Runtime/Material/AxF/AxF.shader b/com.unity.render-pipelines.high-definition/Runtime/Material/AxF/AxF.shader index 1722da83356..23e30cb2c6b 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Material/AxF/AxF.shader +++ b/com.unity.render-pipelines.high-definition/Runtime/Material/AxF/AxF.shader @@ -248,11 +248,11 @@ Shader "HDRP/AxF" // We reuse depth prepass for the scene selection, allow to handle alpha correctly as well as tessellation and vertex animation #define SHADERPASS SHADERPASS_DEPTH_ONLY #define SCENEPICKINGPASS + #include "Packages/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/PickingSpaceTransforms.hlsl" #include "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/Material.hlsl" #include "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/AxF/AxF.hlsl" #include "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/AxF/ShaderPass/AxFDepthPass.hlsl" #include "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/AxF/AxFData.hlsl" - #include "Packages/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/PickingSpaceTransforms.hlsl" #include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassDepthOnly.hlsl" #pragma vertex Vert diff --git a/com.unity.render-pipelines.high-definition/Runtime/Material/Decal/Decal.shader b/com.unity.render-pipelines.high-definition/Runtime/Material/Decal/Decal.shader index fb49f74d51a..ddb2e50f0a6 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Material/Decal/Decal.shader +++ b/com.unity.render-pipelines.high-definition/Runtime/Material/Decal/Decal.shader @@ -284,10 +284,10 @@ Shader "HDRP/Decal" #define SHADERPASS SHADERPASS_DEPTH_ONLY #define SCENEPICKINGPASS #include "Packages/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariables.hlsl" + #include "Packages/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/PickingSpaceTransforms.hlsl" #include "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/Decal/DecalProperties.hlsl" #include "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/Decal/Decal.hlsl" #include "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/Decal/ShaderPass/DecalSharePass.hlsl" - #include "Packages/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/PickingSpaceTransforms.hlsl" #include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassDecal.hlsl" #pragma editor_sync_compilation diff --git a/com.unity.render-pipelines.high-definition/Runtime/Material/LayeredLit/LayeredLit.shader b/com.unity.render-pipelines.high-definition/Runtime/Material/LayeredLit/LayeredLit.shader index 9fd0e624ef8..781f3703d3a 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Material/LayeredLit/LayeredLit.shader +++ b/com.unity.render-pipelines.high-definition/Runtime/Material/LayeredLit/LayeredLit.shader @@ -589,11 +589,11 @@ Shader "HDRP/LayeredLit" // We reuse depth prepass for the scene selection, allow to handle alpha correctly as well as tessellation and vertex animation #define SHADERPASS SHADERPASS_DEPTH_ONLY #define SCENEPICKINGPASS + #include "Packages/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/PickingSpaceTransforms.hlsl" #include "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/Material.hlsl" #include "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/Lit.hlsl" #include "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/ShaderPass/LitDepthPass.hlsl" #include "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/LayeredLit/LayeredLitData.hlsl" - #include "Packages/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/PickingSpaceTransforms.hlsl" #include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassDepthOnly.hlsl" #pragma vertex Vert diff --git a/com.unity.render-pipelines.high-definition/Runtime/Material/LayeredLit/LayeredLitTessellation.shader b/com.unity.render-pipelines.high-definition/Runtime/Material/LayeredLit/LayeredLitTessellation.shader index dfc9bd27c4d..19a42fe18aa 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Material/LayeredLit/LayeredLitTessellation.shader +++ b/com.unity.render-pipelines.high-definition/Runtime/Material/LayeredLit/LayeredLitTessellation.shader @@ -566,11 +566,11 @@ Shader "HDRP/LayeredLitTessellation" // We reuse depth prepass for the scene selection, allow to handle alpha correctly as well as tessellation and vertex animation #define SHADERPASS SHADERPASS_DEPTH_ONLY #define SCENEPICKINGPASS + #include "Packages/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/PickingSpaceTransforms.hlsl" #include "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/Material.hlsl" #include "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/Lit.hlsl" #include "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/ShaderPass/LitDepthPass.hlsl" #include "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/LayeredLit/LayeredLitData.hlsl" - #include "Packages/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/PickingSpaceTransforms.hlsl" #include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassDepthOnly.hlsl" #pragma vertex Vert diff --git a/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/Lit.shader b/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/Lit.shader index 8c4583eaa40..de83182d5ff 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/Lit.shader +++ b/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/Lit.shader @@ -406,11 +406,11 @@ Shader "HDRP/Lit" // We reuse depth prepass for the scene selection, allow to handle alpha correctly as well as tessellation and vertex animation #define SHADERPASS SHADERPASS_DEPTH_ONLY #define SCENEPICKINGPASS + #include "Packages/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/PickingSpaceTransforms.hlsl" #include "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/Material.hlsl" #include "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/Lit.hlsl" #include "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/ShaderPass/LitDepthPass.hlsl" #include "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/LitData.hlsl" - #include "Packages/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/PickingSpaceTransforms.hlsl" #include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassDepthOnly.hlsl" #pragma vertex Vert diff --git a/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/LitTessellation.shader b/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/LitTessellation.shader index 006e368d3dd..2ba0e8c4a2b 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/LitTessellation.shader +++ b/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/LitTessellation.shader @@ -408,9 +408,9 @@ Shader "HDRP/LitTessellation" // We reuse depth prepass for the scene selection, allow to handle alpha correctly as well as tessellation and vertex animation #define SHADERPASS SHADERPASS_DEPTH_ONLY #define SCENEPICKINGPASS + #include "Packages/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/PickingSpaceTransforms.hlsl" #include "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/Material.hlsl" #include "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/Lit.hlsl" - #include "Packages/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/PickingSpaceTransforms.hlsl" #include "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/ShaderPass/LitDepthPass.hlsl" #include "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/LitData.hlsl" #include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassDepthOnly.hlsl" From 93a65c5e2b3a778ea59bf53ff4db1732494a9728 Mon Sep 17 00:00:00 2001 From: Kleber Garcia Date: Wed, 3 Feb 2021 21:22:41 -0500 Subject: [PATCH 7/7] Changelog description --- 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 993cd504eb3..9a39b8924d1 100644 --- a/com.unity.render-pipelines.high-definition/CHANGELOG.md +++ b/com.unity.render-pipelines.high-definition/CHANGELOG.md @@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - White flashes on camera cuts on volumetric fog. - Fixed light layer issue when performing editing on multiple lights. - Fixed an issue where selection in a debug panel would reset when cycling through enum items. +- Fixed incorrect debug wireframe overlay on tessellated geometry (using littessellation), caused by the picking pass using an incorrect camera matrix. ### Changed - Removed the material pass probe volumes evaluation mode.