From 723ecfae64407a0828372b2ae3b043bd7457d2ad Mon Sep 17 00:00:00 2001 From: Sebastien Lagarde Date: Tue, 12 May 2020 18:30:33 +0200 Subject: [PATCH 1/5] Move code to there right location --- .../ShaderLibrary/Common.hlsl | 31 ------------------- .../ShaderLibrary/Packing.hlsl | 31 +++++++++++++++++++ 2 files changed, 31 insertions(+), 31 deletions(-) diff --git a/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl b/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl index 46fd2557bdf..80861fe440f 100644 --- a/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl +++ b/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl @@ -1111,37 +1111,6 @@ void ApplyDepthOffsetPositionInput(float3 V, float depthOffsetVS, float3 viewFor posInput.linearDepth += depthOffsetVS * abs(dot(V, viewForwardDir)); } -// ---------------------------------------------------------------------------- -// Terrain/Brush heightmap encoding/decoding -// ---------------------------------------------------------------------------- - -#if defined(SHADER_API_VULKAN) || defined(SHADER_API_GLES) || defined(SHADER_API_GLES3) - -real4 PackHeightmap(real height) -{ - uint a = (uint)(65535.0 * height); - return real4((a >> 0) & 0xFF, (a >> 8) & 0xFF, 0, 0) / 255.0; -} - -real UnpackHeightmap(real4 height) -{ - return (height.r + height.g * 256.0) / 257.0; // (255.0 * height.r + 255.0 * 256.0 * height.g) / 65535.0 -} - -#else - -real4 PackHeightmap(real height) -{ - return real4(height, 0, 0, 0); -} - -real UnpackHeightmap(real4 height) -{ - return height.r; -} - -#endif - // ---------------------------------------------------------------------------- // Misc utilities // ---------------------------------------------------------------------------- diff --git a/com.unity.render-pipelines.core/ShaderLibrary/Packing.hlsl b/com.unity.render-pipelines.core/ShaderLibrary/Packing.hlsl index 36c2c9a923c..283ee7765f2 100644 --- a/com.unity.render-pipelines.core/ShaderLibrary/Packing.hlsl +++ b/com.unity.render-pipelines.core/ShaderLibrary/Packing.hlsl @@ -217,6 +217,37 @@ real3 UnpackNormalScale(real4 packedNormal, real bumpScale) #endif } +// ---------------------------------------------------------------------------- +// Terrain/Brush heightmap encoding/decoding +// ---------------------------------------------------------------------------- + +#if defined(SHADER_API_VULKAN) || defined(SHADER_API_GLES) || defined(SHADER_API_GLES3) + +real4 PackHeightmap(real height) +{ + uint a = (uint)(65535.0 * height); + return real4((a >> 0) & 0xFF, (a >> 8) & 0xFF, 0, 0) / 255.0; +} + +real UnpackHeightmap(real4 height) +{ + return (height.r + height.g * 256.0) / 257.0; // (255.0 * height.r + 255.0 * 256.0 * height.g) / 65535.0 +} + +#else + +real4 PackHeightmap(real height) +{ + return real4(height, 0, 0, 0); +} + +real UnpackHeightmap(real4 height) +{ + return height.r; +} + +#endif + //----------------------------------------------------------------------------- // HDR packing //----------------------------------------------------------------------------- From a955130a9b0f4ee10a8872625b566cb0734a5f56 Mon Sep 17 00:00:00 2001 From: Sebastien Lagarde Date: Tue, 12 May 2020 19:00:56 +0200 Subject: [PATCH 2/5] Move dots intancing code to the right location --- .../ShaderLibrary/UnityInstancing.hlsl | 39 ++++++++++++++ .../ShaderLibrary/ShaderVariables.hlsl | 51 ++----------------- 2 files changed, 44 insertions(+), 46 deletions(-) diff --git a/com.unity.render-pipelines.core/ShaderLibrary/UnityInstancing.hlsl b/com.unity.render-pipelines.core/ShaderLibrary/UnityInstancing.hlsl index c1937f5358b..101045ee5f7 100644 --- a/com.unity.render-pipelines.core/ShaderLibrary/UnityInstancing.hlsl +++ b/com.unity.render-pipelines.core/ShaderLibrary/UnityInstancing.hlsl @@ -364,6 +364,45 @@ #define UNITY_MATRIX_M UNITY_ACCESS_DOTS_INSTANCED_PROP(float4x4, unity_ObjectToWorld) #define UNITY_MATRIX_I_M UNITY_ACCESS_DOTS_INSTANCED_PROP(float4x4, unity_WorldToObject) #endif + + UNITY_DOTS_INSTANCING_START(BuiltinPropertyMetadata) + UNITY_DOTS_INSTANCED_PROP(float4x4, unity_ObjectToWorld) + UNITY_DOTS_INSTANCED_PROP(float4x4, unity_WorldToObject) + UNITY_DOTS_INSTANCED_PROP(float4, unity_LODFade) + UNITY_DOTS_INSTANCED_PROP(float4, unity_WorldTransformParams) + UNITY_DOTS_INSTANCED_PROP(float4, unity_RenderingLayer) + UNITY_DOTS_INSTANCED_PROP(float4, unity_LightmapST) + UNITY_DOTS_INSTANCED_PROP(float4, unity_DynamicLightmapST) + UNITY_DOTS_INSTANCED_PROP(float4, unity_SHAr) + UNITY_DOTS_INSTANCED_PROP(float4, unity_SHAg) + UNITY_DOTS_INSTANCED_PROP(float4, unity_SHAb) + UNITY_DOTS_INSTANCED_PROP(float4, unity_SHBr) + UNITY_DOTS_INSTANCED_PROP(float4, unity_SHBg) + 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(float4x4, unity_MatrixPreviousM) + UNITY_DOTS_INSTANCED_PROP(float4x4, unity_MatrixPreviousMI) + UNITY_DOTS_INSTANCING_END(BuiltinPropertyMetadata) + + #define unity_LODFade UNITY_ACCESS_DOTS_INSTANCED_PROP_FROM_MACRO(float4, Metadata_unity_LODFade) + #define unity_WorldTransformParams UNITY_ACCESS_DOTS_INSTANCED_PROP_FROM_MACRO(float4, Metadata_unity_WorldTransformParams) + #define unity_RenderingLayer UNITY_ACCESS_DOTS_INSTANCED_PROP_FROM_MACRO(float4, Metadata_unity_RenderingLayer) + #define unity_LightmapST UNITY_ACCESS_DOTS_INSTANCED_PROP_FROM_MACRO(float4, Metadata_unity_LightmapST) + #define unity_DynamicLightmapST UNITY_ACCESS_DOTS_INSTANCED_PROP_FROM_MACRO(float4, Metadata_unity_DynamicLightmapST) + #define unity_SHAr UNITY_ACCESS_DOTS_INSTANCED_PROP_FROM_MACRO(float4, Metadata_unity_SHAr) + #define unity_SHAg UNITY_ACCESS_DOTS_INSTANCED_PROP_FROM_MACRO(float4, Metadata_unity_SHAg) + #define unity_SHAb UNITY_ACCESS_DOTS_INSTANCED_PROP_FROM_MACRO(float4, Metadata_unity_SHAb) + #define unity_SHBr UNITY_ACCESS_DOTS_INSTANCED_PROP_FROM_MACRO(float4, Metadata_unity_SHBr) + #define unity_SHBg UNITY_ACCESS_DOTS_INSTANCED_PROP_FROM_MACRO(float4, Metadata_unity_SHBg) + #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 UNITY_ACCESS_DOTS_INSTANCED_PROP_FROM_MACRO(float4x4, Metadata_unity_MatrixPreviousM) + #define unity_MatrixPreviousMI UNITY_ACCESS_DOTS_INSTANCED_PROP_FROM_MACRO(float4x4, Metadata_unity_MatrixPreviousMI) + + #endif + #else #ifndef UNITY_DONT_INSTANCE_OBJECT_MATRICES 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 8add9af27a0..b0fc38ed89f 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariables.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariables.hlsl @@ -7,7 +7,7 @@ #include "Packages/com.unity.render-pipelines.high-definition-config/Runtime/ShaderConfig.cs.hlsl" #include "Packages/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/TextureXR.hlsl" -// This must be included first before we declare any global constant buffer and will onyl affect ray tracing shaders +// This must be included first before we declare any global constant buffer and will only affect ray tracing shaders #include "Packages/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariablesGlobal.hlsl" // CAUTION: @@ -324,55 +324,14 @@ float4x4 GetRawUnityWorldToObject() { return unity_WorldToObject; } #define UNITY_MATRIX_M ApplyCameraTranslationToMatrix(GetRawUnityObjectToWorld()) #define UNITY_MATRIX_I_M ApplyCameraTranslationToInverseMatrix(GetRawUnityWorldToObject()) -// 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 - // 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 #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/UnityInstancing.hlsl" -#ifdef UNITY_DOTS_INSTANCING_ENABLED -// Undef the matrix error macros so that the DOTS instancing macro works -#undef unity_ObjectToWorld -#undef unity_WorldToObject -UNITY_DOTS_INSTANCING_START(BuiltinPropertyMetadata) - UNITY_DOTS_INSTANCED_PROP(float4x4, unity_ObjectToWorld) - UNITY_DOTS_INSTANCED_PROP(float4x4, unity_WorldToObject) - UNITY_DOTS_INSTANCED_PROP(float4, unity_LODFade) - UNITY_DOTS_INSTANCED_PROP(float4, unity_WorldTransformParams) - UNITY_DOTS_INSTANCED_PROP(float4, unity_RenderingLayer) - UNITY_DOTS_INSTANCED_PROP(float4, unity_LightmapST) - UNITY_DOTS_INSTANCED_PROP(float4, unity_DynamicLightmapST) - UNITY_DOTS_INSTANCED_PROP(float4, unity_SHAr) - UNITY_DOTS_INSTANCED_PROP(float4, unity_SHAg) - UNITY_DOTS_INSTANCED_PROP(float4, unity_SHAb) - UNITY_DOTS_INSTANCED_PROP(float4, unity_SHBr) - UNITY_DOTS_INSTANCED_PROP(float4, unity_SHBg) - 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(float4x4, unity_MatrixPreviousM) - UNITY_DOTS_INSTANCED_PROP(float4x4, unity_MatrixPreviousMI) -UNITY_DOTS_INSTANCING_END(BuiltinPropertyMetadata) - -// Note: Macros for unity_ObjectToWorld and unity_WorldToObject are declared elsewhere -#define unity_LODFade UNITY_ACCESS_DOTS_INSTANCED_PROP_FROM_MACRO(float4, Metadata_unity_LODFade) -#define unity_WorldTransformParams UNITY_ACCESS_DOTS_INSTANCED_PROP_FROM_MACRO(float4, Metadata_unity_WorldTransformParams) -#define unity_RenderingLayer UNITY_ACCESS_DOTS_INSTANCED_PROP_FROM_MACRO(float4, Metadata_unity_RenderingLayer) -#define unity_LightmapST UNITY_ACCESS_DOTS_INSTANCED_PROP_FROM_MACRO(float4, Metadata_unity_LightmapST) -#define unity_DynamicLightmapST UNITY_ACCESS_DOTS_INSTANCED_PROP_FROM_MACRO(float4, Metadata_unity_DynamicLightmapST) -#define unity_SHAr UNITY_ACCESS_DOTS_INSTANCED_PROP_FROM_MACRO(float4, Metadata_unity_SHAr) -#define unity_SHAg UNITY_ACCESS_DOTS_INSTANCED_PROP_FROM_MACRO(float4, Metadata_unity_SHAg) -#define unity_SHAb UNITY_ACCESS_DOTS_INSTANCED_PROP_FROM_MACRO(float4, Metadata_unity_SHAb) -#define unity_SHBr UNITY_ACCESS_DOTS_INSTANCED_PROP_FROM_MACRO(float4, Metadata_unity_SHBr) -#define unity_SHBg UNITY_ACCESS_DOTS_INSTANCED_PROP_FROM_MACRO(float4, Metadata_unity_SHBg) -#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 UNITY_ACCESS_DOTS_INSTANCED_PROP_FROM_MACRO(float4x4, Metadata_unity_MatrixPreviousM) -#define unity_MatrixPreviousMI UNITY_ACCESS_DOTS_INSTANCED_PROP_FROM_MACRO(float4x4, Metadata_unity_MatrixPreviousMI) -#endif +// To get instancing working, we must use UNITY_MATRIX_M / UNITY_MATRIX_I_M as UnityInstancing.hlsl redefine them +// So throw an error if there is an attempt to do direct access +#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 View/Projection matrix macro #include "Packages/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariablesMatrixDefsHDCamera.hlsl" From 1aaca160b3c2af0a5d797662853a404558a40f3c Mon Sep 17 00:00:00 2001 From: Sebastien Lagarde Date: Tue, 12 May 2020 19:03:53 +0200 Subject: [PATCH 3/5] Update UnityInstancing.hlsl --- .../ShaderLibrary/UnityInstancing.hlsl | 2 -- 1 file changed, 2 deletions(-) diff --git a/com.unity.render-pipelines.core/ShaderLibrary/UnityInstancing.hlsl b/com.unity.render-pipelines.core/ShaderLibrary/UnityInstancing.hlsl index 101045ee5f7..095e1ceb01e 100644 --- a/com.unity.render-pipelines.core/ShaderLibrary/UnityInstancing.hlsl +++ b/com.unity.render-pipelines.core/ShaderLibrary/UnityInstancing.hlsl @@ -401,8 +401,6 @@ #define unity_MatrixPreviousM UNITY_ACCESS_DOTS_INSTANCED_PROP_FROM_MACRO(float4x4, Metadata_unity_MatrixPreviousM) #define unity_MatrixPreviousMI UNITY_ACCESS_DOTS_INSTANCED_PROP_FROM_MACRO(float4x4, Metadata_unity_MatrixPreviousMI) - #endif - #else #ifndef UNITY_DONT_INSTANCE_OBJECT_MATRICES From ccb4045156bef840a7020026070b37f49a0be551 Mon Sep 17 00:00:00 2001 From: Jussi Knuuttila Date: Wed, 13 May 2020 14:10:33 +0300 Subject: [PATCH 4/5] Change the Hybrid V2 versions of UNITY_MATRIX_ macros to use the FROM_MACRO versions to avoid problems with other macros. --- .../ShaderLibrary/UnityInstancing.hlsl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/com.unity.render-pipelines.core/ShaderLibrary/UnityInstancing.hlsl b/com.unity.render-pipelines.core/ShaderLibrary/UnityInstancing.hlsl index 095e1ceb01e..8df5497bec4 100644 --- a/com.unity.render-pipelines.core/ShaderLibrary/UnityInstancing.hlsl +++ b/com.unity.render-pipelines.core/ShaderLibrary/UnityInstancing.hlsl @@ -358,11 +358,11 @@ #undef UNITY_MATRIX_M #undef UNITY_MATRIX_I_M #ifdef MODIFY_MATRIX_FOR_CAMERA_RELATIVE_RENDERING - #define UNITY_MATRIX_M ApplyCameraTranslationToMatrix(UNITY_ACCESS_DOTS_INSTANCED_PROP(float4x4, unity_ObjectToWorld)) - #define UNITY_MATRIX_I_M ApplyCameraTranslationToInverseMatrix(UNITY_ACCESS_DOTS_INSTANCED_PROP(float4x4, unity_WorldToObject)) + #define UNITY_MATRIX_M ApplyCameraTranslationToMatrix(UNITY_ACCESS_DOTS_INSTANCED_PROP_FROM_MACRO(float4x4, Metadata_unity_ObjectToWorld)) + #define UNITY_MATRIX_I_M ApplyCameraTranslationToInverseMatrix(UNITY_ACCESS_DOTS_INSTANCED_PROP_FROM_MACRO(float4x4, Metadata_unity_WorldToObject)) #else - #define UNITY_MATRIX_M UNITY_ACCESS_DOTS_INSTANCED_PROP(float4x4, unity_ObjectToWorld) - #define UNITY_MATRIX_I_M UNITY_ACCESS_DOTS_INSTANCED_PROP(float4x4, unity_WorldToObject) + #define UNITY_MATRIX_M UNITY_ACCESS_DOTS_INSTANCED_PROP_FROM_MACRO(float4x4, Metadata_unity_ObjectToWorld) + #define UNITY_MATRIX_I_M UNITY_ACCESS_DOTS_INSTANCED_PROP_FROM_MACRO(float4x4, Metadata_unity_WorldToObject) #endif UNITY_DOTS_INSTANCING_START(BuiltinPropertyMetadata) From 2f2cdbd12349d4a33e75fbcfdf83d17752489532 Mon Sep 17 00:00:00 2001 From: Jussi Knuuttila Date: Wed, 13 May 2020 15:40:28 +0300 Subject: [PATCH 5/5] Replace ifdef'd UNITY_MATRIX_M with redefined GetRawUnityObjectToWorld for Hybrid V2 to clean up code and reduce corner cases. --- .../ShaderLibrary/UnityInstancing.hlsl | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/com.unity.render-pipelines.core/ShaderLibrary/UnityInstancing.hlsl b/com.unity.render-pipelines.core/ShaderLibrary/UnityInstancing.hlsl index 8df5497bec4..236a5aff150 100644 --- a/com.unity.render-pipelines.core/ShaderLibrary/UnityInstancing.hlsl +++ b/com.unity.render-pipelines.core/ShaderLibrary/UnityInstancing.hlsl @@ -353,18 +353,7 @@ UNITY_INSTANCING_BUFFER_END(unity_Builtins2) #endif - // TODO: What about UNITY_DONT_INSTANCE_OBJECT_MATRICES for DOTS? #if defined(UNITY_DOTS_INSTANCING_ENABLED) - #undef UNITY_MATRIX_M - #undef UNITY_MATRIX_I_M - #ifdef MODIFY_MATRIX_FOR_CAMERA_RELATIVE_RENDERING - #define UNITY_MATRIX_M ApplyCameraTranslationToMatrix(UNITY_ACCESS_DOTS_INSTANCED_PROP_FROM_MACRO(float4x4, Metadata_unity_ObjectToWorld)) - #define UNITY_MATRIX_I_M ApplyCameraTranslationToInverseMatrix(UNITY_ACCESS_DOTS_INSTANCED_PROP_FROM_MACRO(float4x4, Metadata_unity_WorldToObject)) - #else - #define UNITY_MATRIX_M UNITY_ACCESS_DOTS_INSTANCED_PROP_FROM_MACRO(float4x4, Metadata_unity_ObjectToWorld) - #define UNITY_MATRIX_I_M UNITY_ACCESS_DOTS_INSTANCED_PROP_FROM_MACRO(float4x4, Metadata_unity_WorldToObject) - #endif - UNITY_DOTS_INSTANCING_START(BuiltinPropertyMetadata) UNITY_DOTS_INSTANCED_PROP(float4x4, unity_ObjectToWorld) UNITY_DOTS_INSTANCED_PROP(float4x4, unity_WorldToObject) @@ -401,6 +390,13 @@ #define unity_MatrixPreviousM UNITY_ACCESS_DOTS_INSTANCED_PROP_FROM_MACRO(float4x4, Metadata_unity_MatrixPreviousM) #define unity_MatrixPreviousMI UNITY_ACCESS_DOTS_INSTANCED_PROP_FROM_MACRO(float4x4, Metadata_unity_MatrixPreviousMI) + // Redirect GetRawUnityObjectToWorld etc to load from DOTS instancing buffers, which should also make UNITY_MATRIX_M work correctly + float4x4 GetRawUnityObjectToWorldDotsInstanced() { return UNITY_ACCESS_DOTS_INSTANCED_PROP_FROM_MACRO(float4x4, Metadata_unity_ObjectToWorld); } + float4x4 GetRawUnityWorldToObjectDotsInstanced() { return UNITY_ACCESS_DOTS_INSTANCED_PROP_FROM_MACRO(float4x4, Metadata_unity_WorldToObject); } + + #define GetRawUnityObjectToWorld GetRawUnityObjectToWorldDotsInstanced + #define GetRawUnityWorldToObject GetRawUnityWorldToObjectDotsInstanced + #else #ifndef UNITY_DONT_INSTANCE_OBJECT_MATRICES