From 514c24496322b83501d74163a6d9d01ff12c1551 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jarkko=20Lempi=C3=A4inen?= Date: Thu, 8 Apr 2021 11:38:22 +0300 Subject: [PATCH 1/5] - initial commit --- .../ShaderLibrary/SpaceTransforms.hlsl | 10 ++++++ .../ShaderLibrary/UnityInstancing.hlsl | 31 ++++++++++++++----- .../ShaderLibrary/PickingSpaceTransforms.hlsl | 9 ++++++ .../ShaderLibrary/ShaderVariables.hlsl | 28 +++++++++++------ .../ShaderVariablesFunctions.hlsl | 7 ++--- .../ShaderGraphLibrary/ShaderVariables.hlsl | 4 +-- 6 files changed, 65 insertions(+), 24 deletions(-) diff --git a/com.unity.render-pipelines.core/ShaderLibrary/SpaceTransforms.hlsl b/com.unity.render-pipelines.core/ShaderLibrary/SpaceTransforms.hlsl index 52903b13e55..101ab023c97 100644 --- a/com.unity.render-pipelines.core/ShaderLibrary/SpaceTransforms.hlsl +++ b/com.unity.render-pipelines.core/ShaderLibrary/SpaceTransforms.hlsl @@ -18,6 +18,16 @@ float4x4 GetWorldToObjectMatrix() return UNITY_MATRIX_I_M; } +float4x4 GetPrevObjectToWorldMatrix() +{ + return UNITY_PREV_MATRIX_M; +} + +float4x4 GetPrevWorldToObjectMatrix() +{ + return UNITY_PREV_MATRIX_I_M; +} + float4x4 GetWorldToViewMatrix() { return UNITY_MATRIX_V; diff --git a/com.unity.render-pipelines.core/ShaderLibrary/UnityInstancing.hlsl b/com.unity.render-pipelines.core/ShaderLibrary/UnityInstancing.hlsl index 513094e6c89..2ea45ae1f09 100644 --- a/com.unity.render-pipelines.core/ShaderLibrary/UnityInstancing.hlsl +++ b/com.unity.render-pipelines.core/ShaderLibrary/UnityInstancing.hlsl @@ -294,6 +294,10 @@ #if UNITY_WORLDTOOBJECTARRAY_CB == 0 UNITY_DEFINE_INSTANCED_PROP(float4x4, unity_WorldToObjectArray) #endif + UNITY_DEFINE_INSTANCED_PROP(float4x4, unity_PrevObjectToWorldArray) + #if UNITY_WORLDTOOBJECTARRAY_CB == 0 + UNITY_DEFINE_INSTANCED_PROP(float4x4, unity_PrevWorldToObjectArray) + #endif #endif #if defined(UNITY_USE_LODFADE_ARRAY) && defined(UNITY_INSTANCING_SUPPORT_FLEXIBLE_ARRAY_SIZE) UNITY_DEFINE_INSTANCED_PROP(float2, unity_LODFadeArray) @@ -313,6 +317,7 @@ UNITY_INSTANCING_BUFFER_START(PerDraw1) #if !defined(UNITY_DONT_INSTANCE_OBJECT_MATRICES) && UNITY_WORLDTOOBJECTARRAY_CB == 1 UNITY_DEFINE_INSTANCED_PROP(float4x4, unity_WorldToObjectArray) + UNITY_DEFINE_INSTANCED_PROP(float4x4, unity_PrevWorldToObjectArray) #endif #if defined(UNITY_USE_LODFADE_ARRAY) && !defined(UNITY_INSTANCING_SUPPORT_FLEXIBLE_ARRAY_SIZE) UNITY_DEFINE_INSTANCED_PROP(float2, unity_LODFadeArray) @@ -361,12 +366,18 @@ #if defined(UNITY_DOTS_INSTANCING_ENABLED) #undef UNITY_MATRIX_M #undef UNITY_MATRIX_I_M + #undef UNITY_PREV_MATRIX_M + #undef UNITY_PREV_MATRIX_I_M #ifdef MODIFY_MATRIX_FOR_CAMERA_RELATIVE_RENDERING - #define UNITY_MATRIX_M ApplyCameraTranslationToMatrix(LoadDOTSInstancedData_float4x4_from_float3x4(UNITY_DOTS_INSTANCED_METADATA_NAME_FROM_MACRO(float3x4, Metadata_unity_ObjectToWorld))) - #define UNITY_MATRIX_I_M ApplyCameraTranslationToInverseMatrix(LoadDOTSInstancedData_float4x4_from_float3x4(UNITY_DOTS_INSTANCED_METADATA_NAME_FROM_MACRO(float3x4, Metadata_unity_WorldToObject))) + #define UNITY_MATRIX_M ApplyCameraTranslationToMatrix(LoadDOTSInstancedData_float4x4_from_float3x4(UNITY_DOTS_INSTANCED_METADATA_NAME_FROM_MACRO(float3x4, Metadata_unity_ObjectToWorld))) + #define UNITY_MATRIX_I_M ApplyCameraTranslationToInverseMatrix(LoadDOTSInstancedData_float4x4_from_float3x4(UNITY_DOTS_INSTANCED_METADATA_NAME_FROM_MACRO(float3x4, Metadata_unity_WorldToObject))) + #define UNITY_PREV_MATRIX_M ApplyCameraTranslationToMatrix(LoadDOTSInstancedData_float4x4_from_float3x4(UNITY_DOTS_INSTANCED_METADATA_NAME_FROM_MACRO(float3x4, Metadata_unity_PrevObjectToWorld))) + #define UNITY_PREV_MATRIX_I_M ApplyCameraTranslationToInverseMatrix(LoadDOTSInstancedData_float4x4_from_float3x4(UNITY_DOTS_INSTANCED_METADATA_NAME_FROM_MACRO(float3x4, Metadata_unity_PrevWorldToObject))) #else - #define UNITY_MATRIX_M LoadDOTSInstancedData_float4x4_from_float3x4(UNITY_DOTS_INSTANCED_METADATA_NAME_FROM_MACRO(float3x4, Metadata_unity_ObjectToWorld)) - #define UNITY_MATRIX_I_M LoadDOTSInstancedData_float4x4_from_float3x4(UNITY_DOTS_INSTANCED_METADATA_NAME_FROM_MACRO(float3x4, Metadata_unity_WorldToObject)) + #define UNITY_MATRIX_M LoadDOTSInstancedData_float4x4_from_float3x4(UNITY_DOTS_INSTANCED_METADATA_NAME_FROM_MACRO(float3x4, Metadata_unity_ObjectToWorld)) + #define UNITY_MATRIX_I_M LoadDOTSInstancedData_float4x4_from_float3x4(UNITY_DOTS_INSTANCED_METADATA_NAME_FROM_MACRO(float3x4, Metadata_unity_WorldToObject)) + #define UNITY_PREV_MATRIX_M LoadDOTSInstancedData_float4x4_from_float3x4(UNITY_DOTS_INSTANCED_METADATA_NAME_FROM_MACRO(float3x4, Metadata_unity_PrevObjectToWorld)) + #define UNITY_PREV_MATRIX_I_M LoadDOTSInstancedData_float4x4_from_float3x4(UNITY_DOTS_INSTANCED_METADATA_NAME_FROM_MACRO(float3x4, Metadata_unity_PrevWorldToObject)) #endif #else @@ -383,11 +394,15 @@ #endif #ifdef MODIFY_MATRIX_FOR_CAMERA_RELATIVE_RENDERING - #define UNITY_MATRIX_M ApplyCameraTranslationToMatrix(UNITY_ACCESS_INSTANCED_PROP(unity_Builtins0, unity_ObjectToWorldArray)) - #define UNITY_MATRIX_I_M ApplyCameraTranslationToInverseMatrix(UNITY_ACCESS_INSTANCED_PROP(UNITY_BUILTINS_WITH_WORLDTOOBJECTARRAY, unity_WorldToObjectArray)) + #define UNITY_MATRIX_M ApplyCameraTranslationToMatrix(UNITY_ACCESS_INSTANCED_PROP(unity_Builtins0, unity_ObjectToWorldArray)) + #define UNITY_MATRIX_I_M ApplyCameraTranslationToInverseMatrix(UNITY_ACCESS_INSTANCED_PROP(UNITY_BUILTINS_WITH_WORLDTOOBJECTARRAY, unity_WorldToObjectArray)) + #define UNITY_PREV_MATRIX_M ApplyCameraTranslationToMatrix(UNITY_ACCESS_INSTANCED_PROP(unity_Builtins0, unity_PrevObjectToWorldArray)) + #define UNITY_PREV_MATRIX_I_M ApplyCameraTranslationToInverseMatrix(UNITY_ACCESS_INSTANCED_PROP(UNITY_BUILTINS_WITH_WORLDTOOBJECTARRAY, unity_PrevWorldToObjectArray)) #else - #define UNITY_MATRIX_M UNITY_ACCESS_INSTANCED_PROP(unity_Builtins0, unity_ObjectToWorldArray) - #define UNITY_MATRIX_I_M UNITY_ACCESS_INSTANCED_PROP(UNITY_BUILTINS_WITH_WORLDTOOBJECTARRAY, unity_WorldToObjectArray) + #define UNITY_MATRIX_M UNITY_ACCESS_INSTANCED_PROP(unity_Builtins0, unity_ObjectToWorldArray) + #define UNITY_MATRIX_I_M UNITY_ACCESS_INSTANCED_PROP(UNITY_BUILTINS_WITH_WORLDTOOBJECTARRAY, unity_WorldToObjectArray) + #define UNITY_PREV_MATRIX_M UNITY_ACCESS_INSTANCED_PROP(unity_Builtins0, unity_PrevObjectToWorldArray) + #define UNITY_PREV_MATRIX_I_M UNITY_ACCESS_INSTANCED_PROP(UNITY_BUILTINS_WITH_WORLDTOOBJECTARRAY, unity_PrevWorldToObjectArray) #endif #endif 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 d6d014f6c8e..94114a8c790 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/PickingSpaceTransforms.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/PickingSpaceTransforms.hlsl @@ -10,6 +10,7 @@ // Define the correct matrices #undef unity_ObjectToWorld +#undef unity_PrevObjectToWorld #undef unity_MatrixVP float4x4 unity_MatrixV; float4x4 unity_MatrixVP; @@ -21,6 +22,12 @@ float4x4 glstate_matrix_projection; #undef UNITY_MATRIX_I_M #define UNITY_MATRIX_I_M Inverse(unity_ObjectToWorld) +#undef UNITY_PREV_MATRIX_M +#define UNITY_PREV_MATRIX_M unity_PrevObjectToWorld + +#undef UNITY_PREV_MATRIX_I_M +#define UNITY_PREV_MATRIX_I_M Inverse(unity_PrevObjectToWorld) + #undef UNITY_MATRIX_V #define UNITY_MATRIX_V unity_MatrixV @@ -34,6 +41,8 @@ float4x4 glstate_matrix_projection; // Overwrite the SpaceTransforms functions #define GetObjectToWorldMatrix GetObjectToWorldMatrix_Picking #define GetWorldToObjectMatrix GetWorldToObjectMatrix_Picking +#define GetPrevObjectToWorldMatrix GetPrevObjectToWorldMatrix_Picking +#define GetPrevWorldToObjectMatrix GetPrevWorldToObjectMatrix_Picking #define GetWorldToViewMatrix GetWorldToViewMatrix_Picking #define GetWorldToHClipMatrix GetWorldToHClipMatrix_Picking #define GetViewToHClipMatrix GetViewToHClipMatrix_Picking 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 86ee7a77d06..ed1c469bcb9 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariables.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariables.hlsl @@ -88,8 +88,8 @@ CBUFFER_START(UnityPerDraw) float4 unity_ProbesOcclusion; // Velocity - float4x4 unity_MatrixPreviousM; - float4x4 unity_MatrixPreviousMI; + float4x4 unity_PrevObjectToWorld; + float4x4 unity_PrevWorldToObject; //X : Use last frame positions (right now skinned meshes are the only objects that use this //Y : Force No Motion //Z : Z bias value @@ -334,15 +334,23 @@ uint Get1DAddressFromPixelCoord(uint2 pixCoord, uint2 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 -float4x4 GetRawUnityObjectToWorld() { return unity_ObjectToWorld; } -float4x4 GetRawUnityWorldToObject() { return unity_WorldToObject; } +float4x4 GetRawUnityObjectToWorld() { return unity_ObjectToWorld; } +float4x4 GetRawUnityWorldToObject() { return unity_WorldToObject; } +float4x4 GetRawUnityPrevObjectToWorld() { return unity_PrevObjectToWorld; } +float4x4 GetRawUnityPrevWorldToObject() { return unity_PrevWorldToObject; } -#define UNITY_MATRIX_M ApplyCameraTranslationToMatrix(GetRawUnityObjectToWorld()) -#define UNITY_MATRIX_I_M ApplyCameraTranslationToInverseMatrix(GetRawUnityWorldToObject()) +#define UNITY_MATRIX_M ApplyCameraTranslationToMatrix(GetRawUnityObjectToWorld()) +#define UNITY_MATRIX_I_M ApplyCameraTranslationToInverseMatrix(GetRawUnityWorldToObject()) +#define UNITY_PREV_MATRIX_M ApplyCameraTranslationToMatrix(GetRawUnityPrevObjectToWorld()) +#define UNITY_PREV_MATRIX_I_M ApplyCameraTranslationToInverseMatrix(GetRawUnityPrevWorldToObject()) // To get instancing working, we must use UNITY_MATRIX_M / UNITY_MATRIX_I_M as UnityInstancing.hlsl redefine them #define unity_ObjectToWorld Use_Macro_UNITY_MATRIX_M_instead_of_unity_ObjectToWorld #define unity_WorldToObject Use_Macro_UNITY_MATRIX_I_M_instead_of_unity_WorldToObject +#define unity_PrevObjectToWorld Use_Macro_UNITY_PREV_MATRIX_M_instead_of_unity_PrevObjectToWorld +#define unity_PrevWorldToObject Use_Macro_UNITY_PREV_MATRIX_I_M_instead_of_unity_PrevWorldToObject +#define unity_MatrixPreviousM unity_PrevObjectToWorld // to support code using deprecated unity_MatrixPreviousM, please change code to use UNITY_PREV_MATRIX_M instead +#define unity_MatrixPreviousMI unity_PrevWorldToObject // to support code using deprecated unity_MatrixPreviousMI, please change code to use UNITY_PREV_MATRIX_I_M instead // This define allow to tell to unity instancing that we will use our camera relative functions (ApplyCameraTranslationToMatrix and ApplyCameraTranslationToInverseMatrix) for the model view matrix #define MODIFY_MATRIX_FOR_CAMERA_RELATIVE_RENDERING @@ -369,8 +377,8 @@ UNITY_DOTS_INSTANCING_START(BuiltinPropertyMetadata) UNITY_DOTS_INSTANCED_PROP(float4, unity_SHBb) UNITY_DOTS_INSTANCED_PROP(float4, unity_SHC) UNITY_DOTS_INSTANCED_PROP(float4, unity_ProbesOcclusion) - UNITY_DOTS_INSTANCED_PROP(float3x4, unity_MatrixPreviousM) - UNITY_DOTS_INSTANCED_PROP(float3x4, unity_MatrixPreviousMI) + UNITY_DOTS_INSTANCED_PROP(float3x4, unity_PrevObjectToWorld) + UNITY_DOTS_INSTANCED_PROP(float3x4, unity_PrevWorldToObject) UNITY_DOTS_INSTANCING_END(BuiltinPropertyMetadata) // Note: Macros for unity_ObjectToWorld and unity_WorldToObject are declared elsewhere @@ -388,8 +396,8 @@ UNITY_DOTS_INSTANCING_END(BuiltinPropertyMetadata) #define unity_SHBb UNITY_ACCESS_DOTS_INSTANCED_PROP_FROM_MACRO(float4, Metadata_unity_SHBb) #define unity_SHC UNITY_ACCESS_DOTS_INSTANCED_PROP_FROM_MACRO(float4, Metadata_unity_SHC) #define unity_ProbesOcclusion UNITY_ACCESS_DOTS_INSTANCED_PROP_FROM_MACRO(float4, Metadata_unity_ProbesOcclusion) -#define unity_MatrixPreviousM LoadDOTSInstancedData_float4x4_from_float3x4(UNITY_DOTS_INSTANCED_METADATA_NAME_FROM_MACRO(float3x4, Metadata_unity_MatrixPreviousM)) -#define unity_MatrixPreviousMI LoadDOTSInstancedData_float4x4_from_float3x4(UNITY_DOTS_INSTANCED_METADATA_NAME_FROM_MACRO(float3x4, Metadata_unity_MatrixPreviousMI)) +#define unity_PrevObjectToWorld LoadDOTSInstancedData_float4x4_from_float3x4(UNITY_DOTS_INSTANCED_METADATA_NAME_FROM_MACRO(float3x4, Metadata_unity_PrevObjectToWorld)) +#define unity_PrevWorldToObject LoadDOTSInstancedData_float4x4_from_float3x4(UNITY_DOTS_INSTANCED_METADATA_NAME_FROM_MACRO(float3x4, Metadata_unity_PrevWorldToObject)) #endif 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..916e01ee6eb 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariablesFunctions.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariablesFunctions.hlsl @@ -145,18 +145,17 @@ float3x3 BuildTangentToWorld(float4 tangentWS, float3 normalWS) float3 TransformPreviousObjectToWorldNormal(float3 normalOS) { #ifdef UNITY_ASSUME_UNIFORM_SCALING - return normalize(mul((float3x3)unity_MatrixPreviousM, normalOS)); + return normalize(mul((float3x3)UNITY_PREV_MATRIX_M, normalOS)); #else // Normal need to be multiply by inverse transpose - return normalize(mul(normalOS, (float3x3)unity_MatrixPreviousMI)); + return normalize(mul(normalOS, (float3x3)UNITY_PREV_MATRIX_I_M)); #endif } // Transforms local position to camera relative world space float3 TransformPreviousObjectToWorld(float3 positionOS) { - float4x4 previousModelMatrix = ApplyCameraTranslationToMatrix(unity_MatrixPreviousM); - return mul(previousModelMatrix, float4(positionOS, 1.0)).xyz; + return mul(UNITY_PREV_MATRIX_M, float4(positionOS, 1.0)).xyz; } diff --git a/com.unity.shadergraph/ShaderGraphLibrary/ShaderVariables.hlsl b/com.unity.shadergraph/ShaderGraphLibrary/ShaderVariables.hlsl index 19c9b5cca65..66f06b729a0 100644 --- a/com.unity.shadergraph/ShaderGraphLibrary/ShaderVariables.hlsl +++ b/com.unity.shadergraph/ShaderGraphLibrary/ShaderVariables.hlsl @@ -234,8 +234,8 @@ SAMPLER(samplerunity_ProbeVolumeSH); CBUFFER_START(UnityVelocityPass) float4x4 unity_MatrixNonJitteredVP; float4x4 unity_MatrixPreviousVP; - float4x4 unity_MatrixPreviousM; - float4x4 unity_MatrixPreviousMI; + float4x4 unity_PrevObjectToWorld; + float4x4 unity_PrevWorldToObject; //X : Use last frame positions (right now skinned meshes are the only objects that use this //Y : Force No Motion //Z : Z bias value From 0b0c66272b7bd4e2a368bbd8589fc02ecbf0bf0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jarkko=20Lempi=C3=A4inen?= Date: Fri, 9 Apr 2021 14:26:16 +0300 Subject: [PATCH 2/5] - separate prev object<->world matrices to a separate buffer --- .../ShaderLibrary/UnityInstancing.hlsl | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/com.unity.render-pipelines.core/ShaderLibrary/UnityInstancing.hlsl b/com.unity.render-pipelines.core/ShaderLibrary/UnityInstancing.hlsl index 2ea45ae1f09..2a60366281f 100644 --- a/com.unity.render-pipelines.core/ShaderLibrary/UnityInstancing.hlsl +++ b/com.unity.render-pipelines.core/ShaderLibrary/UnityInstancing.hlsl @@ -294,10 +294,6 @@ #if UNITY_WORLDTOOBJECTARRAY_CB == 0 UNITY_DEFINE_INSTANCED_PROP(float4x4, unity_WorldToObjectArray) #endif - UNITY_DEFINE_INSTANCED_PROP(float4x4, unity_PrevObjectToWorldArray) - #if UNITY_WORLDTOOBJECTARRAY_CB == 0 - UNITY_DEFINE_INSTANCED_PROP(float4x4, unity_PrevWorldToObjectArray) - #endif #endif #if defined(UNITY_USE_LODFADE_ARRAY) && defined(UNITY_INSTANCING_SUPPORT_FLEXIBLE_ARRAY_SIZE) UNITY_DEFINE_INSTANCED_PROP(float2, unity_LODFadeArray) @@ -360,6 +356,11 @@ #define unity_ProbesOcclusion UNITY_ACCESS_INSTANCED_PROP(unity_Builtins2, unity_ProbesOcclusionArray) #endif UNITY_INSTANCING_BUFFER_END(unity_Builtins2) + + UNITY_INSTANCING_BUFFER_START(PerDraw3) + UNITY_DEFINE_INSTANCED_PROP(float4x4, unity_PrevObjectToWorldArray) + UNITY_DEFINE_INSTANCED_PROP(float4x4, unity_PrevWorldToObjectArray) + UNITY_INSTANCING_BUFFER_END(unity_Builtins3) #endif // TODO: What about UNITY_DONT_INSTANCE_OBJECT_MATRICES for DOTS? @@ -396,13 +397,13 @@ #ifdef MODIFY_MATRIX_FOR_CAMERA_RELATIVE_RENDERING #define UNITY_MATRIX_M ApplyCameraTranslationToMatrix(UNITY_ACCESS_INSTANCED_PROP(unity_Builtins0, unity_ObjectToWorldArray)) #define UNITY_MATRIX_I_M ApplyCameraTranslationToInverseMatrix(UNITY_ACCESS_INSTANCED_PROP(UNITY_BUILTINS_WITH_WORLDTOOBJECTARRAY, unity_WorldToObjectArray)) - #define UNITY_PREV_MATRIX_M ApplyCameraTranslationToMatrix(UNITY_ACCESS_INSTANCED_PROP(unity_Builtins0, unity_PrevObjectToWorldArray)) - #define UNITY_PREV_MATRIX_I_M ApplyCameraTranslationToInverseMatrix(UNITY_ACCESS_INSTANCED_PROP(UNITY_BUILTINS_WITH_WORLDTOOBJECTARRAY, unity_PrevWorldToObjectArray)) + #define UNITY_PREV_MATRIX_M ApplyCameraTranslationToMatrix(UNITY_ACCESS_INSTANCED_PROP(unity_Builtins3, unity_PrevObjectToWorldArray)) + #define UNITY_PREV_MATRIX_I_M ApplyCameraTranslationToInverseMatrix(UNITY_ACCESS_INSTANCED_PROP(unity_Builtins3, unity_PrevWorldToObjectArray)) #else #define UNITY_MATRIX_M UNITY_ACCESS_INSTANCED_PROP(unity_Builtins0, unity_ObjectToWorldArray) #define UNITY_MATRIX_I_M UNITY_ACCESS_INSTANCED_PROP(UNITY_BUILTINS_WITH_WORLDTOOBJECTARRAY, unity_WorldToObjectArray) - #define UNITY_PREV_MATRIX_M UNITY_ACCESS_INSTANCED_PROP(unity_Builtins0, unity_PrevObjectToWorldArray) - #define UNITY_PREV_MATRIX_I_M UNITY_ACCESS_INSTANCED_PROP(UNITY_BUILTINS_WITH_WORLDTOOBJECTARRAY, unity_PrevWorldToObjectArray) + #define UNITY_PREV_MATRIX_M UNITY_ACCESS_INSTANCED_PROP(unity_Builtins3, unity_PrevObjectToWorldArray) + #define UNITY_PREV_MATRIX_I_M UNITY_ACCESS_INSTANCED_PROP(unity_Builtins3, unity_PrevWorldToObjectArray) #endif #endif From bae6a45132d96736b07f3435d7541fb3c0212772 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jarkko=20Lempi=C3=A4inen?= Date: Tue, 1 Jun 2021 15:05:14 +0300 Subject: [PATCH 3/5] - reverted unity_MatrixPreviousM and unity_MatrixPreviousMI constant rename --- .../ShaderLibrary/UnityInstancing.hlsl | 8 ++++---- .../ShaderLibrary/PickingSpaceTransforms.hlsl | 6 +++--- .../ShaderLibrary/ShaderVariables.hlsl | 20 ++++++++----------- .../ShaderGraphLibrary/ShaderVariables.hlsl | 4 ++-- 4 files changed, 17 insertions(+), 21 deletions(-) diff --git a/com.unity.render-pipelines.core/ShaderLibrary/UnityInstancing.hlsl b/com.unity.render-pipelines.core/ShaderLibrary/UnityInstancing.hlsl index 2a60366281f..95ca8fe2ac9 100644 --- a/com.unity.render-pipelines.core/ShaderLibrary/UnityInstancing.hlsl +++ b/com.unity.render-pipelines.core/ShaderLibrary/UnityInstancing.hlsl @@ -372,13 +372,13 @@ #ifdef MODIFY_MATRIX_FOR_CAMERA_RELATIVE_RENDERING #define UNITY_MATRIX_M ApplyCameraTranslationToMatrix(LoadDOTSInstancedData_float4x4_from_float3x4(UNITY_DOTS_INSTANCED_METADATA_NAME_FROM_MACRO(float3x4, Metadata_unity_ObjectToWorld))) #define UNITY_MATRIX_I_M ApplyCameraTranslationToInverseMatrix(LoadDOTSInstancedData_float4x4_from_float3x4(UNITY_DOTS_INSTANCED_METADATA_NAME_FROM_MACRO(float3x4, Metadata_unity_WorldToObject))) - #define UNITY_PREV_MATRIX_M ApplyCameraTranslationToMatrix(LoadDOTSInstancedData_float4x4_from_float3x4(UNITY_DOTS_INSTANCED_METADATA_NAME_FROM_MACRO(float3x4, Metadata_unity_PrevObjectToWorld))) - #define UNITY_PREV_MATRIX_I_M ApplyCameraTranslationToInverseMatrix(LoadDOTSInstancedData_float4x4_from_float3x4(UNITY_DOTS_INSTANCED_METADATA_NAME_FROM_MACRO(float3x4, Metadata_unity_PrevWorldToObject))) + #define UNITY_PREV_MATRIX_M ApplyCameraTranslationToMatrix(LoadDOTSInstancedData_float4x4_from_float3x4(UNITY_DOTS_INSTANCED_METADATA_NAME_FROM_MACRO(float3x4, Metadata_unity_MatrixPreviousM))) + #define UNITY_PREV_MATRIX_I_M ApplyCameraTranslationToInverseMatrix(LoadDOTSInstancedData_float4x4_from_float3x4(UNITY_DOTS_INSTANCED_METADATA_NAME_FROM_MACRO(float3x4, Metadata_unity_MatrixPreviousMI))) #else #define UNITY_MATRIX_M LoadDOTSInstancedData_float4x4_from_float3x4(UNITY_DOTS_INSTANCED_METADATA_NAME_FROM_MACRO(float3x4, Metadata_unity_ObjectToWorld)) #define UNITY_MATRIX_I_M LoadDOTSInstancedData_float4x4_from_float3x4(UNITY_DOTS_INSTANCED_METADATA_NAME_FROM_MACRO(float3x4, Metadata_unity_WorldToObject)) - #define UNITY_PREV_MATRIX_M LoadDOTSInstancedData_float4x4_from_float3x4(UNITY_DOTS_INSTANCED_METADATA_NAME_FROM_MACRO(float3x4, Metadata_unity_PrevObjectToWorld)) - #define UNITY_PREV_MATRIX_I_M LoadDOTSInstancedData_float4x4_from_float3x4(UNITY_DOTS_INSTANCED_METADATA_NAME_FROM_MACRO(float3x4, Metadata_unity_PrevWorldToObject)) + #define UNITY_PREV_MATRIX_M LoadDOTSInstancedData_float4x4_from_float3x4(UNITY_DOTS_INSTANCED_METADATA_NAME_FROM_MACRO(float3x4, Metadata_unity_MatrixPreviousM)) + #define UNITY_PREV_MATRIX_I_M LoadDOTSInstancedData_float4x4_from_float3x4(UNITY_DOTS_INSTANCED_METADATA_NAME_FROM_MACRO(float3x4, Metadata_unity_MatrixPreviousMI)) #endif #else 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 94114a8c790..5bc363a55e8 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/PickingSpaceTransforms.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/PickingSpaceTransforms.hlsl @@ -10,7 +10,7 @@ // Define the correct matrices #undef unity_ObjectToWorld -#undef unity_PrevObjectToWorld +#undef unity_MatrixPreviousM #undef unity_MatrixVP float4x4 unity_MatrixV; float4x4 unity_MatrixVP; @@ -23,10 +23,10 @@ float4x4 glstate_matrix_projection; #define UNITY_MATRIX_I_M Inverse(unity_ObjectToWorld) #undef UNITY_PREV_MATRIX_M -#define UNITY_PREV_MATRIX_M unity_PrevObjectToWorld +#define UNITY_PREV_MATRIX_M unity_MatrixPreviousM #undef UNITY_PREV_MATRIX_I_M -#define UNITY_PREV_MATRIX_I_M Inverse(unity_PrevObjectToWorld) +#define UNITY_PREV_MATRIX_I_M Inverse(unity_MatrixPreviousM) #undef UNITY_MATRIX_V #define UNITY_MATRIX_V unity_MatrixV 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 1dfa3c04086..1712e924891 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariables.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariables.hlsl @@ -88,8 +88,8 @@ CBUFFER_START(UnityPerDraw) float4 unity_ProbesOcclusion; // Velocity - float4x4 unity_PrevObjectToWorld; - float4x4 unity_PrevWorldToObject; + float4x4 unity_MatrixPreviousM; + float4x4 unity_MatrixPreviousMI; //X : Use last frame positions (right now skinned meshes are the only objects that use this //Y : Force No Motion //Z : Z bias value @@ -431,8 +431,8 @@ uint Get1DAddressFromPixelCoord(uint2 pixCoord, uint2 screenSize) // We need to declare inline function. Using uniform directly mean they are expand with the macro float4x4 GetRawUnityObjectToWorld() { return unity_ObjectToWorld; } float4x4 GetRawUnityWorldToObject() { return unity_WorldToObject; } -float4x4 GetRawUnityPrevObjectToWorld() { return unity_PrevObjectToWorld; } -float4x4 GetRawUnityPrevWorldToObject() { return unity_PrevWorldToObject; } +float4x4 GetRawUnityPrevObjectToWorld() { return unity_MatrixPreviousM; } +float4x4 GetRawUnityPrevWorldToObject() { return unity_MatrixPreviousMI; } #define UNITY_MATRIX_M ApplyCameraTranslationToMatrix(GetRawUnityObjectToWorld()) #define UNITY_MATRIX_I_M ApplyCameraTranslationToInverseMatrix(GetRawUnityWorldToObject()) @@ -442,10 +442,6 @@ float4x4 GetRawUnityPrevWorldToObject() { return unity_PrevWorldToObject; } // To get instancing working, we must use UNITY_MATRIX_M / UNITY_MATRIX_I_M as UnityInstancing.hlsl redefine them #define unity_ObjectToWorld Use_Macro_UNITY_MATRIX_M_instead_of_unity_ObjectToWorld #define unity_WorldToObject Use_Macro_UNITY_MATRIX_I_M_instead_of_unity_WorldToObject -#define unity_PrevObjectToWorld Use_Macro_UNITY_PREV_MATRIX_M_instead_of_unity_PrevObjectToWorld -#define unity_PrevWorldToObject Use_Macro_UNITY_PREV_MATRIX_I_M_instead_of_unity_PrevWorldToObject -#define unity_MatrixPreviousM unity_PrevObjectToWorld // to support code using deprecated unity_MatrixPreviousM, please change code to use UNITY_PREV_MATRIX_M instead -#define unity_MatrixPreviousMI unity_PrevWorldToObject // to support code using deprecated unity_MatrixPreviousMI, please change code to use UNITY_PREV_MATRIX_I_M instead // This define allow to tell to unity instancing that we will use our camera relative functions (ApplyCameraTranslationToMatrix and ApplyCameraTranslationToInverseMatrix) for the model view matrix #define MODIFY_MATRIX_FOR_CAMERA_RELATIVE_RENDERING @@ -475,8 +471,8 @@ UNITY_DOTS_INSTANCING_START(BuiltinPropertyMetadata) UNITY_DOTS_INSTANCED_PROP(float4, unity_SHBb) UNITY_DOTS_INSTANCED_PROP(float4, unity_SHC) UNITY_DOTS_INSTANCED_PROP(float4, unity_ProbesOcclusion) - UNITY_DOTS_INSTANCED_PROP(float3x4, unity_PrevObjectToWorld) - UNITY_DOTS_INSTANCED_PROP(float3x4, unity_PrevWorldToObject) + UNITY_DOTS_INSTANCED_PROP(float3x4, unity_MatrixPreviousM) + UNITY_DOTS_INSTANCED_PROP(float3x4, unity_MatrixPreviousMI) UNITY_DOTS_INSTANCING_END(BuiltinPropertyMetadata) // Note: Macros for unity_ObjectToWorld and unity_WorldToObject are declared elsewhere @@ -494,8 +490,8 @@ UNITY_DOTS_INSTANCING_END(BuiltinPropertyMetadata) #define unity_SHBb UNITY_ACCESS_DOTS_INSTANCED_PROP_FROM_MACRO(float4, Metadata_unity_SHBb) #define unity_SHC UNITY_ACCESS_DOTS_INSTANCED_PROP_FROM_MACRO(float4, Metadata_unity_SHC) #define unity_ProbesOcclusion UNITY_ACCESS_DOTS_INSTANCED_PROP_FROM_MACRO(float4, Metadata_unity_ProbesOcclusion) -#define unity_PrevObjectToWorld LoadDOTSInstancedData_float4x4_from_float3x4(UNITY_DOTS_INSTANCED_METADATA_NAME_FROM_MACRO(float3x4, Metadata_unity_PrevObjectToWorld)) -#define unity_PrevWorldToObject LoadDOTSInstancedData_float4x4_from_float3x4(UNITY_DOTS_INSTANCED_METADATA_NAME_FROM_MACRO(float3x4, Metadata_unity_PrevWorldToObject)) +#define unity_MatrixPreviousM LoadDOTSInstancedData_float4x4_from_float3x4(UNITY_DOTS_INSTANCED_METADATA_NAME_FROM_MACRO(float3x4, Metadata_unity_MatrixPreviousM)) +#define unity_MatrixPreviousMI LoadDOTSInstancedData_float4x4_from_float3x4(UNITY_DOTS_INSTANCED_METADATA_NAME_FROM_MACRO(float3x4, Metadata_unity_MatrixPreviousMI)) #endif diff --git a/com.unity.shadergraph/ShaderGraphLibrary/ShaderVariables.hlsl b/com.unity.shadergraph/ShaderGraphLibrary/ShaderVariables.hlsl index 66f06b729a0..19c9b5cca65 100644 --- a/com.unity.shadergraph/ShaderGraphLibrary/ShaderVariables.hlsl +++ b/com.unity.shadergraph/ShaderGraphLibrary/ShaderVariables.hlsl @@ -234,8 +234,8 @@ SAMPLER(samplerunity_ProbeVolumeSH); CBUFFER_START(UnityVelocityPass) float4x4 unity_MatrixNonJitteredVP; float4x4 unity_MatrixPreviousVP; - float4x4 unity_PrevObjectToWorld; - float4x4 unity_PrevWorldToObject; + float4x4 unity_MatrixPreviousM; + float4x4 unity_MatrixPreviousMI; //X : Use last frame positions (right now skinned meshes are the only objects that use this //Y : Force No Motion //Z : Z bias value From e38040e408ae20dc4dd4ad530d8fd87d8104667b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jarkko=20Lempi=C3=A4inen?= Date: Thu, 10 Jun 2021 14:53:15 +0300 Subject: [PATCH 4/5] Updated changelogs & removed leftover unity_PrevWorldToObjectArray from PerDraw1 --- com.unity.render-pipelines.core/CHANGELOG.md | 1 + .../ShaderLibrary/UnityInstancing.hlsl | 1 - com.unity.render-pipelines.high-definition/CHANGELOG.md | 1 + 3 files changed, 2 insertions(+), 1 deletion(-) diff --git a/com.unity.render-pipelines.core/CHANGELOG.md b/com.unity.render-pipelines.core/CHANGELOG.md index 05c3a95a341..cbd2f0450f7 100644 --- a/com.unity.render-pipelines.core/CHANGELOG.md +++ b/com.unity.render-pipelines.core/CHANGELOG.md @@ -64,6 +64,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - Adding new API functions inside DynamicResolutionHandler to get mip bias. This allows dynamic resolution scaling applying a bias on the frame to improve on texture sampling detail. - Added a reminder if the data of probe volume might be obsolete. - Added new API function inside DynamicResolutionHandler and new settings in GlobalDynamicResolutionSettings to control low res transparency thresholds. This should help visuals when the screen percentage is too low. +- Added UNITY_PREV_MATRIX_M and UNITY_PREV_MATRIX_I_M shader macros to support instanced motion vector rendering ### Changed - Changed Window/Render Pipeline/Render Pipeline Debug to Window/Analysis/Rendering Debugger diff --git a/com.unity.render-pipelines.core/ShaderLibrary/UnityInstancing.hlsl b/com.unity.render-pipelines.core/ShaderLibrary/UnityInstancing.hlsl index 95ca8fe2ac9..d276986bf85 100644 --- a/com.unity.render-pipelines.core/ShaderLibrary/UnityInstancing.hlsl +++ b/com.unity.render-pipelines.core/ShaderLibrary/UnityInstancing.hlsl @@ -313,7 +313,6 @@ UNITY_INSTANCING_BUFFER_START(PerDraw1) #if !defined(UNITY_DONT_INSTANCE_OBJECT_MATRICES) && UNITY_WORLDTOOBJECTARRAY_CB == 1 UNITY_DEFINE_INSTANCED_PROP(float4x4, unity_WorldToObjectArray) - UNITY_DEFINE_INSTANCED_PROP(float4x4, unity_PrevWorldToObjectArray) #endif #if defined(UNITY_USE_LODFADE_ARRAY) && !defined(UNITY_INSTANCING_SUPPORT_FLEXIBLE_ARRAY_SIZE) UNITY_DEFINE_INSTANCED_PROP(float2, unity_LODFadeArray) diff --git a/com.unity.render-pipelines.high-definition/CHANGELOG.md b/com.unity.render-pipelines.high-definition/CHANGELOG.md index d598208065a..5e40268fbe8 100644 --- a/com.unity.render-pipelines.high-definition/CHANGELOG.md +++ b/com.unity.render-pipelines.high-definition/CHANGELOG.md @@ -70,6 +70,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - Added the receiver motion rejection toggle to RTGI (case 1330168). - Added info box when low resolution transparency is selected, but its not enabled in the HDRP settings. This will help new users find the correct knob in the HDRP Asset. - Added a dialog box when you import a Material that has a diffusion profile to add the diffusion profile to global settings. +- Added support for instanced motion vector rendering ### Fixed - Fixed Intensity Multiplier not affecting realtime global illumination. From 0e1fbfccb4464837eadb57c30698bba897ed1e01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jarkko=20Lempi=C3=A4inen?= Date: Thu, 22 Jul 2021 13:56:46 +0300 Subject: [PATCH 5/5] - added some missing UNITY_PREV_MATRIX_M & UNITY_PREV_MATRIX_I_M definitions (caused URP issues) --- com.unity.render-pipelines.universal/ShaderLibrary/Input.hlsl | 2 ++ .../Editor/Generation/Targets/BuiltIn/ShaderLibrary/Input.hlsl | 3 +++ .../ShaderVariablesMatrixDefsLegacyUnity.hlsl | 2 ++ 3 files changed, 7 insertions(+) diff --git a/com.unity.render-pipelines.universal/ShaderLibrary/Input.hlsl b/com.unity.render-pipelines.universal/ShaderLibrary/Input.hlsl index ad3dbf92268..52d801b6ef9 100644 --- a/com.unity.render-pipelines.universal/ShaderLibrary/Input.hlsl +++ b/com.unity.render-pipelines.universal/ShaderLibrary/Input.hlsl @@ -149,6 +149,8 @@ CBUFFER_END #define UNITY_MATRIX_T_MV transpose(UNITY_MATRIX_MV) #define UNITY_MATRIX_IT_MV transpose(mul(UNITY_MATRIX_I_M, UNITY_MATRIX_I_V)) #define UNITY_MATRIX_MVP mul(UNITY_MATRIX_VP, UNITY_MATRIX_M) +#define UNITY_PREV_MATRIX_M unity_MatrixPreviousM +#define UNITY_PREV_MATRIX_I_M unity_MatrixPreviousMI // Note: #include order is important here. // UnityInput.hlsl must be included before UnityInstancing.hlsl, so constant buffer diff --git a/com.unity.shadergraph/Editor/Generation/Targets/BuiltIn/ShaderLibrary/Input.hlsl b/com.unity.shadergraph/Editor/Generation/Targets/BuiltIn/ShaderLibrary/Input.hlsl index 4d489e7cc93..c8aa968544f 100644 --- a/com.unity.shadergraph/Editor/Generation/Targets/BuiltIn/ShaderLibrary/Input.hlsl +++ b/com.unity.shadergraph/Editor/Generation/Targets/BuiltIn/ShaderLibrary/Input.hlsl @@ -83,11 +83,14 @@ CBUFFER_END #define UNITY_MATRIX_T_MV transpose(UNITY_MATRIX_MV) #define UNITY_MATRIX_IT_MV transpose(mul(UNITY_MATRIX_I_M, UNITY_MATRIX_I_V)) #define UNITY_MATRIX_MVP mul(UNITY_MATRIX_VP, UNITY_MATRIX_M) +#define UNITY_PREV_MATRIX_M unity_MatrixPreviousM +#define UNITY_PREV_MATRIX_I_M unity_MatrixPreviousMI #else // Not defined already by built-in #define UNITY_MATRIX_I_M unity_WorldToObject #define UNITY_MATRIX_I_P (float4x4)0 #define UNITY_MATRIX_I_VP (float4x4)0 +#define UNITY_PREV_MATRIX_I_M unity_MatrixPreviousMI #endif diff --git a/com.unity.shadergraph/ShaderGraphLibrary/ShaderVariablesMatrixDefsLegacyUnity.hlsl b/com.unity.shadergraph/ShaderGraphLibrary/ShaderVariablesMatrixDefsLegacyUnity.hlsl index 38043f49bba..4e805eb8de2 100644 --- a/com.unity.shadergraph/ShaderGraphLibrary/ShaderVariablesMatrixDefsLegacyUnity.hlsl +++ b/com.unity.shadergraph/ShaderGraphLibrary/ShaderVariablesMatrixDefsLegacyUnity.hlsl @@ -17,5 +17,7 @@ #define UNITY_MATRIX_T_MV transpose(UNITY_MATRIX_MV) #define UNITY_MATRIX_IT_MV transpose(mul(UNITY_MATRIX_I_M, UNITY_MATRIX_I_V)) #define UNITY_MATRIX_MVP mul(UNITY_MATRIX_VP, UNITY_MATRIX_M) +#define UNITY_PREV_MATRIX_M unity_MatrixPreviousM +#define UNITY_PREV_MATRIX_I_M unity_MatrixPreviousMI #endif // UNITY_SHADER_VARIABLES_MATRIX_DEFS_LEGACY_UNITY_INCLUDED