Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Packages/NativeTrees/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
Unreleased (after v0.1.10)
- Fixed octree and quadtree subdivision from managed threads that are not Unity job workers. Copy and nearest-query helpers retain their existing allocator thread requirements.

v0.1.2
- Fixed bug for InsertPoint methods

v0.1.1
- Removed redundant call to Clear in constructors

v0.1
- Initial release
- Initial release
19 changes: 12 additions & 7 deletions Packages/NativeTrees/Runtime/Octree/NativeOctree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ public partial struct NativeOctree<T> : INativeDisposable
/// </summary>
private NativeParallelHashMap<uint, int> nodes;
private NativeParallelMultiHashMap<uint, ObjWrapper> objects;

/// <summary>
/// Reused while redistributing a full node. Insertion is single-threaded, and redistribution completes before recursively subdividing children.
/// </summary>
private NativeArray<ObjWrapper> subdivisionBuffer;

/// <summary>
/// Constructs an octree with a max depth of 8
Expand All @@ -84,6 +89,7 @@ public NativeOctree(AABB bounds, int objectsPerNode, int maxDepth, Allocator all

objects = new NativeParallelMultiHashMap<uint, ObjWrapper>(initialCapacity, allocator);
nodes = new NativeParallelHashMap<uint, int>(initialCapacity / objectsPerNode, allocator);
subdivisionBuffer = new NativeArray<ObjWrapper>(objectsPerNode + 1, allocator, NativeArrayOptions.UninitializedMemory);

this.objectsPerNode = objectsPerNode;
this.maxDepth = maxDepth;
Expand Down Expand Up @@ -195,17 +201,16 @@ bool TryInsert(uint nodeId, in QuarterSizeBounds extents, in ObjWrapper objWrapp
void Subdivide(uint nodeId, in QuarterSizeBounds quarterSizeBounds, int depth)
{
int objectCount = 0;
NativeArray<ObjWrapper> tempObjects = new NativeArray<ObjWrapper>(objectsPerNode + 1, Allocator.Temp, NativeArrayOptions.UninitializedMemory);
foreach (var tempObj in objects.GetValuesForKey(nodeId))
tempObjects[objectCount++] = tempObj;
subdivisionBuffer[objectCount++] = tempObj;

FixedList64Bytes<int> countPerOctant = new FixedList64Bytes<int>();
countPerOctant.Length = 8;

objects.Remove(nodeId); // remove all occurances of objects in our original
for (int i = 0; i < objectCount; i++)
{
var moveObject = tempObjects[i];
var moveObject = subdivisionBuffer[i];
int aabbMask = GetBoundsMask(quarterSizeBounds.nodeCenter, moveObject.bounds);

// Can't make the point optimization here because we can't be certain the node only contained points
Expand All @@ -222,8 +227,6 @@ void Subdivide(uint nodeId, in QuarterSizeBounds quarterSizeBounds, int depth)
}
}

tempObjects.Dispose();

// Update counts, create nodes when neccessary
depth++;
for (int i = 0; i < 8; i++)
Expand Down Expand Up @@ -437,14 +440,16 @@ public void Dispose()
{
nodes.Dispose();
objects.Dispose();
subdivisionBuffer.Dispose();
}

/// <summary>
/// Dispose the NativeOctree
/// </summary>
public JobHandle Dispose(JobHandle inputDeps)
{
return JobHandle.CombineDependencies(nodes.Dispose(inputDeps), objects.Dispose(inputDeps));
var containersHandle = JobHandle.CombineDependencies(nodes.Dispose(inputDeps), objects.Dispose(inputDeps));
return JobHandle.CombineDependencies(containersHandle, subdivisionBuffer.Dispose(inputDeps));
}

/// <summary>
Expand Down Expand Up @@ -483,4 +488,4 @@ void Gizmos(uint nodeId, in ExtentsBounds quarterSizeBounds, int objectCount, in
parentDepth: depth);
}
}
}
}
19 changes: 12 additions & 7 deletions Packages/NativeTrees/Runtime/Quadtree/NativeQuadtree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ public partial struct NativeQuadtree<T> : INativeDisposable where T : unmanaged
/// </summary>
private NativeParallelHashMap<uint, int> nodes;
private NativeParallelMultiHashMap<uint, ObjWrapper> objects;

/// <summary>
/// Reused while redistributing a full node. Insertion is single-threaded, and redistribution completes before recursively subdividing children.
/// </summary>
private NativeArray<ObjWrapper> subdivisionBuffer;

/// <summary>
/// Constructs an quadtree with a max depth of 8
Expand All @@ -84,6 +89,7 @@ public NativeQuadtree(AABB2D bounds, int objectsPerNode, int maxDepth, Allocator

objects = new NativeParallelMultiHashMap<uint, ObjWrapper>(initialCapacity, allocator);
nodes = new NativeParallelHashMap<uint, int>(initialCapacity / objectsPerNode, allocator);
subdivisionBuffer = new NativeArray<ObjWrapper>(objectsPerNode + 1, allocator, NativeArrayOptions.UninitializedMemory);

this.objectsPerNode = objectsPerNode;
this.maxDepth = maxDepth;
Expand Down Expand Up @@ -195,17 +201,16 @@ bool TryInsert(uint nodeId, in QuarterSizeBounds extents, in ObjWrapper objWrapp
void Subdivide(uint nodeId, in QuarterSizeBounds quarterSizeBounds, int depth)
{
int objectCount = 0;
NativeArray<ObjWrapper> tempObjects = new NativeArray<ObjWrapper>(objectsPerNode + 1, Allocator.Temp, NativeArrayOptions.UninitializedMemory);
foreach (var tempObj in objects.GetValuesForKey(nodeId))
tempObjects[objectCount++] = tempObj;
subdivisionBuffer[objectCount++] = tempObj;

FixedList32Bytes<int> countPerQuad = new FixedList32Bytes<int>();
countPerQuad.Length = 4;

objects.Remove(nodeId); // remove all occurances of objects in our original
for (int i = 0; i < objectCount; i++)
{
var moveObject = tempObjects[i];
var moveObject = subdivisionBuffer[i];
int aabbMask = GetBoundsMask(quarterSizeBounds.nodeCenter, moveObject.bounds);

// Can't make the point optimization here because we can't be certain the node only contained points
Expand All @@ -222,8 +227,6 @@ void Subdivide(uint nodeId, in QuarterSizeBounds quarterSizeBounds, int depth)
}
}

tempObjects.Dispose();

// Update counts, create nodes when neccessary
depth++;
for (int i = 0; i < 4; i++)
Expand Down Expand Up @@ -412,14 +415,16 @@ public void Dispose()
{
nodes.Dispose();
objects.Dispose();
subdivisionBuffer.Dispose();
}

/// <summary>
/// Dispose the NativeOctree
/// </summary>
public JobHandle Dispose(JobHandle inputDeps)
{
return JobHandle.CombineDependencies(nodes.Dispose(inputDeps), objects.Dispose(inputDeps));
var containersHandle = JobHandle.CombineDependencies(nodes.Dispose(inputDeps), objects.Dispose(inputDeps));
return JobHandle.CombineDependencies(containersHandle, subdivisionBuffer.Dispose(inputDeps));
}

/// <summary>
Expand Down Expand Up @@ -458,4 +463,4 @@ void Gizmos(uint nodeId, in ExtentsBounds quarterSizeBounds, int objectCount, in
parentDepth: depth);
}
}
}
}
8 changes: 8 additions & 0 deletions Packages/NativeTrees/Tests.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Packages/NativeTrees/Tests/Editor.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

119 changes: 119 additions & 0 deletions Packages/NativeTrees/Tests/Editor/ThreadedSubdivisionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
using System;
using System.Threading;
using NUnit.Framework;
using Unity.Collections;
using Unity.Mathematics;

namespace NativeTrees.Tests
{
public class ThreadedSubdivisionTests
{
[Test]
public void OctreeCanSubdivideOnManagedThread()
{
var treeBounds = new AABB(new float3(-10f), new float3(10f));
var tree = new NativeOctree<int>(
treeBounds,
1,
8,
Allocator.Persistent);

var exception = InsertOnManagedThread(
() =>
{
var point = new float3(1.2345f);
var bounds = new AABB(point, point);
tree.Insert(1, bounds);
tree.Insert(2, bounds);

var secondPoint = new float3(-4.5f, 3.25f, -2.75f);
tree.Insert(3, new AABB(secondPoint, secondPoint));

var thirdPoint = new float3(4.25f, -3.5f, 2.5f);
tree.Insert(4, new AABB(thirdPoint, thirdPoint));
});

try
{
Assert.That(exception, Is.Null);

using var results = new NativeParallelHashSet<int>(4, Allocator.Temp);
tree.RangeAABBUnique(treeBounds, results);
Assert.That(results.Count(), Is.EqualTo(4));
Assert.That(results.Contains(1), Is.True);
Assert.That(results.Contains(2), Is.True);
Assert.That(results.Contains(3), Is.True);
Assert.That(results.Contains(4), Is.True);
}
finally
{
tree.Dispose();
}
}

[Test]
public void QuadtreeCanSubdivideOnManagedThread()
{
var treeBounds = new AABB2D(new float2(-10f), new float2(10f));
var tree = new NativeQuadtree<int>(
treeBounds,
1,
8,
Allocator.Persistent);

var exception = InsertOnManagedThread(
() =>
{
var point = new float2(1.2345f);
var bounds = new AABB2D(point, point);
tree.Insert(1, bounds);
tree.Insert(2, bounds);

var secondPoint = new float2(-4.5f, 3.25f);
tree.Insert(3, new AABB2D(secondPoint, secondPoint));

var thirdPoint = new float2(4.25f, -3.5f);
tree.Insert(4, new AABB2D(thirdPoint, thirdPoint));
});

try
{
Assert.That(exception, Is.Null);

using var results = new NativeParallelHashSet<int>(4, Allocator.Temp);
tree.RangeAABBUnique(treeBounds, results);
Assert.That(results.Count(), Is.EqualTo(4));
Assert.That(results.Contains(1), Is.True);
Assert.That(results.Contains(2), Is.True);
Assert.That(results.Contains(3), Is.True);
Assert.That(results.Contains(4), Is.True);
}
finally
{
tree.Dispose();
}
}

private static Exception InsertOnManagedThread(ThreadStart insert)
{
Exception exception = null;
var thread = new Thread(
() =>
{
try
{
insert();
}
catch (Exception ex)
{
exception = ex;
}
});

thread.IsBackground = true;
thread.Start();
Assert.That(thread.Join(TimeSpan.FromSeconds(10)), Is.True, "Managed insertion thread did not finish.");
return exception;
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "com.bartofzo.nativetrees.tests",
"rootNamespace": "NativeTrees.Tests",
"references": [
"com.bartofzo.nativetrees",
"Unity.Collections",
"Unity.Mathematics",
"UnityEngine.TestRunner",
"UnityEditor.TestRunner"
],
"includePlatforms": [
"Editor"
],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": true,
"precompiledReferences": [
"nunit.framework.dll"
],
"autoReferenced": false,
"defineConstraints": [
"UNITY_INCLUDE_TESTS"
],
"versionDefines": [],
"noEngineReferences": false
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion Packages/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@
"com.unity.burst": "1.6.6",
"com.unity.collections": "1.4.0",
"com.unity.ide.rider": "3.0.16"
}
},
"testables": [
"com.bartofzo.nativetrees"
]
}