-
Notifications
You must be signed in to change notification settings - Fork 796
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Backport][2022.3][SRPF] Use FNV-1a 32 for caching and resource hashc…
…ode in Render Graph The main issue goes to the hash method generation that was used for identifying Render Graph resources. We used the most performant way but it was too many collisions. After a bit of research the decision is to use FNV-1a implementation as it's the same performance but much more reliable. There's still a possibility for collision but a probability should be much smaller.
- Loading branch information
1 parent
00a903f
commit 1f0f19d
Showing
5 changed files
with
170 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
Packages/com.unity.render-pipelines.core/Runtime/Utilities/HashFNV1A32.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
using System; | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace UnityEngine.Rendering | ||
{ | ||
internal ref struct HashFNV1A32 | ||
{ | ||
/// <summary> | ||
/// FNV prime. | ||
/// </summary> | ||
const uint k_Prime = 16777619; | ||
|
||
/// <summary> | ||
/// FNV offset basis. | ||
/// </summary> | ||
const uint k_OffsetBasis = 2166136261; | ||
|
||
uint m_Hash; | ||
|
||
public static HashFNV1A32 Create() | ||
{ | ||
return new HashFNV1A32 { m_Hash = k_OffsetBasis }; | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public void Append(in int input) | ||
{ | ||
unchecked | ||
{ | ||
m_Hash = (m_Hash ^ (uint)input) * k_Prime; | ||
} | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public void Append(in uint input) | ||
{ | ||
unchecked | ||
{ | ||
m_Hash = (m_Hash ^ input) * k_Prime; | ||
} | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public void Append(in bool input) | ||
{ | ||
m_Hash = (m_Hash ^ (input ? 1u : 0u)) * k_Prime; | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public void Append(in float input) | ||
{ | ||
unchecked | ||
{ | ||
m_Hash = (m_Hash ^ (uint)input.GetHashCode()) * k_Prime; | ||
} | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public void Append(in double input) | ||
{ | ||
unchecked | ||
{ | ||
m_Hash = (m_Hash ^ (uint)input.GetHashCode()) * k_Prime; | ||
} | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public void Append(in Vector2 input) | ||
{ | ||
unchecked | ||
{ | ||
m_Hash = (m_Hash ^ (uint)input.GetHashCode()) * k_Prime; | ||
} | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public void Append(in Vector3 input) | ||
{ | ||
unchecked | ||
{ | ||
m_Hash = (m_Hash ^ (uint)input.GetHashCode()) * k_Prime; | ||
} | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public void Append(in Vector4 input) | ||
{ | ||
unchecked | ||
{ | ||
m_Hash = (m_Hash ^ (uint)input.GetHashCode()) * k_Prime; | ||
} | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public void Append<T>(T input) where T : struct | ||
{ | ||
unchecked | ||
{ | ||
m_Hash = (m_Hash ^ (uint)input.GetHashCode()) * k_Prime; | ||
} | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public void Append(Delegate del) | ||
{ | ||
unchecked | ||
{ | ||
m_Hash = (m_Hash ^ (uint)GetFuncHashCode(del)) * k_Prime; | ||
} | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
internal static int GetFuncHashCode(Delegate del) | ||
{ | ||
return del.Method.GetHashCode() ^ RuntimeHelpers.GetHashCode(del.Target); | ||
} | ||
|
||
public int value => (int)m_Hash; | ||
|
||
public override int GetHashCode() | ||
{ | ||
return value; | ||
} | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
Packages/com.unity.render-pipelines.core/Runtime/Utilities/HashFNV1A32.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.