Skip to content

Commit cb7c1df

Browse files
svc-reach-platform-supportEvergreen
authored andcommitted
[Port] [6000.4] RenderGraph - Add new operator == for the TextureHandle class
1 parent e40a293 commit cb7c1df

3 files changed

Lines changed: 189 additions & 9 deletions

File tree

Packages/com.unity.render-pipelines.core/Runtime/RenderGraph/RenderGraphResourceTexture.cs

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ internal enum TextureUVOriginSelection
9797
/// </summary>
9898
[DebuggerDisplay("Texture ({handle.index})")]
9999
[MovedFrom(true, "UnityEngine.Experimental.Rendering.RenderGraphModule", "UnityEngine.Rendering.RenderGraphModule")]
100-
public readonly struct TextureHandle
100+
public readonly struct TextureHandle : IEquatable<TextureHandle>
101101
{
102102
private static TextureHandle s_NullHandle = new TextureHandle();
103103

@@ -151,6 +151,52 @@ internal TextureHandle(int handle, bool shared = false, bool builtin = false)
151151
/// <returns>Resource as a RTHandle.</returns>
152152
public static implicit operator RTHandle(TextureHandle texture) => texture.IsValid() ? RenderGraphResourceRegistry.current.GetTexture(texture) : null;
153153

154+
/// <summary>
155+
/// Determines whether this instance and another specified <see cref="TextureHandle"/> object have the same underlying resource handle.
156+
/// </summary>
157+
/// <param name="other">The texture handle to compare with the current instance.</param>
158+
/// <returns>
159+
/// True if both texture handles reference the same underlying resource; otherwise, false.
160+
/// </returns>
161+
public bool Equals(TextureHandle other) => handle.Equals(other.handle);
162+
163+
/// <summary>
164+
/// Determines whether the specified object is equal to the current <see cref="TextureHandle"/>.
165+
/// </summary>
166+
/// <param name="obj">The object to compare with the current instance.</param>
167+
/// <returns>
168+
/// True if the specified object is a <see cref="TextureHandle"/> and references the same underlying resource; otherwise, false.
169+
/// </returns>
170+
public override bool Equals(object obj) => obj is TextureHandle other && Equals(other);
171+
172+
/// <summary>
173+
/// Returns the hash code for this <see cref="TextureHandle"/>.
174+
/// </summary>
175+
/// <returns>
176+
/// The hash code of the current <see cref="TextureHandle"/>.
177+
/// </returns>
178+
public override int GetHashCode() => handle.GetHashCode();
179+
180+
/// <summary>
181+
/// Determines whether two <see cref="TextureHandle"/> instances reference the same underlying resource.
182+
/// </summary>
183+
/// <param name="lhs">The first texture handle to compare.</param>
184+
/// <param name="rhs">The second texture handle to compare.</param>
185+
/// <returns>
186+
/// True if both handles reference the same underlying resource; otherwise, false.
187+
/// </returns>
188+
public static bool operator ==(TextureHandle lhs, TextureHandle rhs) => lhs.handle.Equals(rhs.handle);
189+
190+
/// <summary>
191+
/// Determines whether two <see cref="TextureHandle"/> instances reference different underlying resources.
192+
/// </summary>
193+
/// <param name="lhs">The first texture handle to compare.</param>
194+
/// <param name="rhs">The second texture handle to compare.</param>
195+
/// <returns>
196+
/// True if the handles reference different underlying resources; otherwise, false.
197+
/// </returns>
198+
public static bool operator !=(TextureHandle lhs, TextureHandle rhs) => !lhs.handle.Equals(rhs.handle);
199+
154200
/// <summary>
155201
/// Return true if the handle is valid.
156202
/// </summary>

Packages/com.unity.render-pipelines.core/Runtime/RenderGraph/RenderGraphResources.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,21 @@ public bool Equals(ResourceHandle hdl)
123123
{
124124
return hdl.m_Value == this.m_Value && hdl.m_Version == this.m_Version && hdl.type == this.type;
125125
}
126+
127+
public static bool operator ==(ResourceHandle lhs, ResourceHandle rhs) => lhs.Equals(rhs);
128+
129+
public static bool operator !=(ResourceHandle lhs, ResourceHandle rhs) => !lhs.Equals(rhs);
130+
131+
public override bool Equals(object obj) => obj is ResourceHandle other && Equals(other);
132+
133+
public override int GetHashCode()
134+
{
135+
var hashCode = HashFNV1A32.Create();
136+
hashCode.Append(m_Value);
137+
hashCode.Append(m_Version);
138+
hashCode.Append(m_Type);
139+
return hashCode.value;
140+
}
126141
}
127142

128143
class IRenderGraphResource

Packages/com.unity.render-pipelines.core/Tests/Editor/RenderGraphTests.cs

Lines changed: 127 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
using UnityEngine.TestTools;
77
using Unity.Collections;
88
using UnityEngine.Rendering.RendererUtils;
9-
using System.Text.RegularExpressions;
9+
using UnityEngine.Rendering.RenderGraphModule.NativeRenderPassCompiler;
1010

1111
#if UNITY_EDITOR
1212
using UnityEditor;
@@ -1489,7 +1489,7 @@ public void ImportedTexturesAreClearedOnFirstUse()
14891489

14901490
m_RenderGraphTestPipeline.recordRenderGraphBody = (context, camera, cmd) =>
14911491
{
1492-
var redTexture = CreateRedTexture(kWidth, kHeight);
1492+
var redTexture = CreateColorTexture(kWidth, kHeight, Color.red);
14931493
ImportResourceParams importParams = new ImportResourceParams()
14941494
{
14951495
clearColor = Color.blue, clearOnFirstUse = true
@@ -1533,6 +1533,128 @@ public void ImportedTexturesAreClearedOnFirstUse()
15331533
pixels.Dispose();
15341534
}
15351535

1536+
[Test]
1537+
public void ImportedTexturesOperatorEqualAndNotEqual()
1538+
{
1539+
const int kWidth = 4;
1540+
const int kHeight = 4;
1541+
1542+
m_RenderGraphTestPipeline.recordRenderGraphBody = (context, camera, cmd) =>
1543+
{
1544+
var importParams = new ImportResourceParams()
1545+
{
1546+
clearColor = Color.black,
1547+
clearOnFirstUse = true
1548+
};
1549+
1550+
var redTexture = CreateColorTexture(kWidth, kHeight, Color.red);
1551+
var red2Texture = CreateColorTexture(kWidth, kHeight, Color.red);
1552+
var blackTexture = CreateColorTexture(kWidth, kHeight, Color.black);
1553+
1554+
var importedTextureRed = m_RenderGraph.ImportTexture(redTexture, importParams);
1555+
var importedTextureRed2 = m_RenderGraph.ImportTexture(red2Texture, importParams);
1556+
var importedTextureRedSame = m_RenderGraph.ImportTexture(redTexture, importParams);
1557+
var importedTextureBlack = m_RenderGraph.ImportTexture(blackTexture, importParams);
1558+
1559+
// Different textures, different handles.
1560+
Assert.True(importedTextureRed != importedTextureBlack);
1561+
1562+
// Different textures, different handles.
1563+
Assert.True(importedTextureRed != importedTextureRed2);
1564+
1565+
// Same texture, different handles.
1566+
Assert.False(importedTextureRed == importedTextureRedSame);
1567+
1568+
importedTextureRedSame = importedTextureRed;
1569+
1570+
// Same texture, same handle.
1571+
Assert.True(importedTextureRed == importedTextureRedSame);
1572+
};
1573+
1574+
m_Camera.Render();
1575+
}
1576+
1577+
[Test]
1578+
public void CreatedTexturesOperatorEqualAndNotEqual()
1579+
{
1580+
m_RenderGraphTestPipeline.recordRenderGraphBody = (context, camera, cmd) =>
1581+
{
1582+
const int kWidth = 4;
1583+
const int kHeight = 4;
1584+
1585+
var texture0 = m_RenderGraph.CreateTexture(new TextureDesc(kWidth, kHeight) { colorFormat = GraphicsFormat.R8G8B8A8_UNorm, name = "Texture0" });
1586+
var texture1 = m_RenderGraph.CreateTexture(new TextureDesc(kWidth, kHeight) { colorFormat = GraphicsFormat.R8G8B8A8_UNorm, name = "Texture1" });
1587+
1588+
// Different textures, different handles.
1589+
Assert.True(texture0 != texture1);
1590+
1591+
// Different textures, different handles.
1592+
Assert.False(texture0 == texture1);
1593+
1594+
texture1 = texture0;
1595+
1596+
// Same texture, same handles.
1597+
Assert.True(texture0 == texture1);
1598+
1599+
// Same texture, same handles.
1600+
Assert.False(texture0 != texture1);
1601+
};
1602+
1603+
m_Camera.Render();
1604+
}
1605+
1606+
[Test]
1607+
public void TexturesOperatorWorksInList()
1608+
{
1609+
m_RenderGraphTestPipeline.recordRenderGraphBody = (context, camera, cmd) =>
1610+
{
1611+
const int kWidth = 4;
1612+
const int kHeight = 4;
1613+
1614+
var texture0 = m_RenderGraph.CreateTexture(new TextureDesc(kWidth, kHeight) { colorFormat = GraphicsFormat.R8G8B8A8_UNorm, name = "Texture0" });
1615+
var texture1 = m_RenderGraph.CreateTexture(new TextureDesc(kWidth, kHeight) { colorFormat = GraphicsFormat.R8G8B8A8_UNorm, name = "Texture1" });
1616+
1617+
var listHandles = new List<TextureHandle>();
1618+
listHandles.Add(texture0);
1619+
1620+
Assert.True(listHandles.Contains(texture0));
1621+
Assert.True(!listHandles.Contains(texture1));
1622+
1623+
listHandles.Add(texture1);
1624+
1625+
Assert.True(listHandles.Contains(texture1));
1626+
};
1627+
1628+
m_Camera.Render();
1629+
}
1630+
1631+
[Test]
1632+
public void TexturesOperatorWorksInDictionary()
1633+
{
1634+
m_RenderGraphTestPipeline.recordRenderGraphBody = (context, camera, cmd) =>
1635+
{
1636+
const int kWidth = 4;
1637+
const int kHeight = 4;
1638+
1639+
var texture0 = m_RenderGraph.CreateTexture(new TextureDesc(kWidth, kHeight) { colorFormat = GraphicsFormat.R8G8B8A8_UNorm, name = "Texture0" });
1640+
var texture1 = m_RenderGraph.CreateTexture(new TextureDesc(kWidth, kHeight) { colorFormat = GraphicsFormat.R8G8B8A8_UNorm, name = "Texture1" });
1641+
1642+
var dictionaryHandles = new Dictionary<TextureHandle, int>();
1643+
dictionaryHandles.Add(texture0, 0);
1644+
1645+
Assert.True(dictionaryHandles.ContainsKey(texture0));
1646+
Assert.True(dictionaryHandles.TryGetValue(texture0, out var value0) && value0 == 0);
1647+
Assert.True(!dictionaryHandles.ContainsKey(texture1));
1648+
1649+
dictionaryHandles.Add(texture1, 1);
1650+
1651+
Assert.True(dictionaryHandles.ContainsKey(texture1));
1652+
Assert.True(dictionaryHandles.TryGetValue(texture1, out var value1) && value1 == 1);
1653+
};
1654+
1655+
m_Camera.Render();
1656+
}
1657+
15361658
[Test]
15371659
public void RequestAsyncReadbackIntoNativeArrayWorks()
15381660
{
@@ -1552,7 +1674,7 @@ public void RequestAsyncReadbackIntoNativeArrayWorks()
15521674

15531675
passExecuted = true;
15541676

1555-
var redTexture = CreateRedTexture(kWidth, kHeight);
1677+
var redTexture = CreateColorTexture(kWidth, kHeight, Color.red);
15561678
var texture0 = m_RenderGraph.ImportTexture(redTexture);
15571679

15581680
pixels = new NativeArray<byte>(kWidth * kHeight * 4, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
@@ -1603,11 +1725,8 @@ void RenderGraphTest_AsyncReadbackCallback(AsyncGPUReadbackRequest request, ref
16031725
}
16041726
}
16051727

1606-
RTHandle CreateRedTexture(int width, int height)
1728+
RTHandle CreateColorTexture(int width, int height, Color color)
16071729
{
1608-
// Create a red color
1609-
Color redColor = Color.red;
1610-
16111730
// Initialize the RTHandle system if necessary
16121731
RTHandles.Initialize(width, height);
16131732

@@ -1625,7 +1744,7 @@ RTHandle CreateRedTexture(int width, int height)
16251744
{
16261745
for (int x = 0; x < tempTexture.width; x++)
16271746
{
1628-
tempTexture.SetPixel(x, y, redColor);
1747+
tempTexture.SetPixel(x, y, color);
16291748
}
16301749
}
16311750
tempTexture.Apply();

0 commit comments

Comments
 (0)