From 8b61a46ec09da938af925bcc66519043ccef4773 Mon Sep 17 00:00:00 2001 From: Kleber Garcia Date: Tue, 14 Dec 2021 12:39:35 -0500 Subject: [PATCH 1/2] ** Adding override of dots instance ID for dots instancing ** * Correcting issues * Undoing unused API * Adding redundant conditional --- .../ShaderLibrary/UnityDOTSInstancing.hlsl | 19 +++++++++++++------ .../ShaderLibrary/UnityInstancing.hlsl | 5 +++++ 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/com.unity.render-pipelines.core/ShaderLibrary/UnityDOTSInstancing.hlsl b/com.unity.render-pipelines.core/ShaderLibrary/UnityDOTSInstancing.hlsl index 9dfccc03776..aa4d60bc717 100644 --- a/com.unity.render-pipelines.core/ShaderLibrary/UnityDOTSInstancing.hlsl +++ b/com.unity.render-pipelines.core/ShaderLibrary/UnityDOTSInstancing.hlsl @@ -150,14 +150,21 @@ static const uint kDOTSInstancingFlagCameraMotion = (1 << 2); // Object uses static const uint kDOTSInstancingFlagHasPrevPosition = (1 << 3); // Object has a separate previous frame position vertex streams (e.g. for deformed objects) static const uint kDOTSInstancingFlagMainLightEnabled = (1 << 4); // Object should receive direct lighting from the main light (e.g. light not baked into lightmap) +static DOTSVisibleData unity_SampledDOTSVisibleData; + +void SetupDOTSVisibleInstancingData() +{ + unity_SampledDOTSVisibleData = unity_DOTSVisibleInstances[unity_InstanceID]; +} + uint GetDOTSInstanceIndex() { - return unity_DOTSVisibleInstances[unity_InstanceID].VisibleData.x; + return unity_SampledDOTSVisibleData.VisibleData.x; } int GetDOTSInstanceCrossfadeSnorm8() { - return unity_DOTSVisibleInstances[unity_InstanceID].VisibleData.y; + return unity_SampledDOTSVisibleData.VisibleData.y; } bool IsDOTSInstancedProperty(uint metadata) @@ -286,24 +293,24 @@ float2x4 LoadDOTSInstancedData_float2x4(float2x4 default_value, uint metadata) float4 LoadDOTSInstancedData_RenderingLayer() { - return float4(asfloat(unity_DOTSVisibleInstances[0].VisibleData.z), 0,0,0); + return float4(asfloat(unity_SampledDOTSVisibleData.VisibleData.z), 0,0,0); } float4 LoadDOTSInstancedData_MotionVectorsParams() { - uint flags = unity_DOTSVisibleInstances[0].VisibleData.w; + uint flags = unity_SampledDOTSVisibleData.VisibleData.w; return float4(0, flags & kDOTSInstancingFlagForceZeroMotion ? 0.0f : 1.0f, -0.001f, flags & kDOTSInstancingFlagCameraMotion ? 0.0f : 1.0f); } float4 LoadDOTSInstancedData_WorldTransformParams() { - uint flags = unity_DOTSVisibleInstances[0].VisibleData.w; + uint flags = unity_SampledDOTSVisibleData.VisibleData.w; return float4(0, 0, 0, flags & kDOTSInstancingFlagFlipWinding ? -1.0f : 1.0f); } float4 LoadDOTSInstancedData_LightData() { - uint flags = unity_DOTSVisibleInstances[0].VisibleData.w; + uint flags = unity_SampledDOTSVisibleData.VisibleData.w; // X channel = light start index (not supported in DOTS instancing) // Y channel = light count (not supported in DOTS instancing) // Z channel = main light strength diff --git a/com.unity.render-pipelines.core/ShaderLibrary/UnityInstancing.hlsl b/com.unity.render-pipelines.core/ShaderLibrary/UnityInstancing.hlsl index 139bc7e6297..2570bb1a676 100644 --- a/com.unity.render-pipelines.core/ShaderLibrary/UnityInstancing.hlsl +++ b/com.unity.render-pipelines.core/ShaderLibrary/UnityInstancing.hlsl @@ -239,6 +239,11 @@ #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/UnityDOTSInstancing.hlsl" + #if defined(UNITY_SETUP_INSTANCE_ID) && defined(DOTS_INSTANCING_ON) + #undef UNITY_SETUP_INSTANCE_ID + #define UNITY_SETUP_INSTANCE_ID(input) { DEFAULT_UNITY_SETUP_INSTANCE_ID(input); SetupDOTSVisibleInstancingData(); } + #endif + #else #define UNITY_INSTANCING_BUFFER_START(buf) UNITY_INSTANCING_CBUFFER_SCOPE_BEGIN(UnityInstancing_##buf) struct { #define UNITY_INSTANCING_BUFFER_END(arr) } arr##Array[UNITY_INSTANCED_ARRAY_SIZE]; UNITY_INSTANCING_CBUFFER_SCOPE_END From 535355952dd9954fa67968a00ab4ce142171fe12 Mon Sep 17 00:00:00 2001 From: Kleber Garcia Date: Wed, 15 Dec 2021 15:51:32 -0500 Subject: [PATCH 2/2] Preserving sampling of index 0 of dots instancing cbuffer: this data must remain in index 0 of the batch --- .../ShaderLibrary/UnityDOTSInstancing.hlsl | 8 ++++---- .../ShaderLibrary/UnityInstancing.hlsl | 7 ++++--- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/com.unity.render-pipelines.core/ShaderLibrary/UnityDOTSInstancing.hlsl b/com.unity.render-pipelines.core/ShaderLibrary/UnityDOTSInstancing.hlsl index aa4d60bc717..e9b2d49e805 100644 --- a/com.unity.render-pipelines.core/ShaderLibrary/UnityDOTSInstancing.hlsl +++ b/com.unity.render-pipelines.core/ShaderLibrary/UnityDOTSInstancing.hlsl @@ -293,24 +293,24 @@ float2x4 LoadDOTSInstancedData_float2x4(float2x4 default_value, uint metadata) float4 LoadDOTSInstancedData_RenderingLayer() { - return float4(asfloat(unity_SampledDOTSVisibleData.VisibleData.z), 0,0,0); + return float4(asfloat(unity_DOTSVisibleInstances[0].VisibleData.z), 0,0,0); } float4 LoadDOTSInstancedData_MotionVectorsParams() { - uint flags = unity_SampledDOTSVisibleData.VisibleData.w; + uint flags = unity_DOTSVisibleInstances[0].VisibleData.w; return float4(0, flags & kDOTSInstancingFlagForceZeroMotion ? 0.0f : 1.0f, -0.001f, flags & kDOTSInstancingFlagCameraMotion ? 0.0f : 1.0f); } float4 LoadDOTSInstancedData_WorldTransformParams() { - uint flags = unity_SampledDOTSVisibleData.VisibleData.w; + uint flags = unity_DOTSVisibleInstances[0].VisibleData.w; return float4(0, 0, 0, flags & kDOTSInstancingFlagFlipWinding ? -1.0f : 1.0f); } float4 LoadDOTSInstancedData_LightData() { - uint flags = unity_SampledDOTSVisibleData.VisibleData.w; + uint flags = unity_DOTSVisibleInstances[0].VisibleData.w; // X channel = light start index (not supported in DOTS instancing) // Y channel = light count (not supported in DOTS instancing) // Z channel = main light strength diff --git a/com.unity.render-pipelines.core/ShaderLibrary/UnityInstancing.hlsl b/com.unity.render-pipelines.core/ShaderLibrary/UnityInstancing.hlsl index 2570bb1a676..555df8d8eb9 100644 --- a/com.unity.render-pipelines.core/ShaderLibrary/UnityInstancing.hlsl +++ b/com.unity.render-pipelines.core/ShaderLibrary/UnityInstancing.hlsl @@ -238,10 +238,11 @@ #define UNITY_ACCESS_INSTANCED_PROP(arr, var) var #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/UnityDOTSInstancing.hlsl" - - #if defined(UNITY_SETUP_INSTANCE_ID) && defined(DOTS_INSTANCING_ON) + #if defined(UNITY_SETUP_INSTANCE_ID) #undef UNITY_SETUP_INSTANCE_ID - #define UNITY_SETUP_INSTANCE_ID(input) { DEFAULT_UNITY_SETUP_INSTANCE_ID(input); SetupDOTSVisibleInstancingData(); } + #define UNITY_SETUP_INSTANCE_ID(input) {\ + DEFAULT_UNITY_SETUP_INSTANCE_ID(input);\ + SetupDOTSVisibleInstancingData();} #endif #else