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
16 changes: 16 additions & 0 deletions Core/2.Processing/Cache/ChiselLookup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ public class Data
// Each brush as the exact CSG sees it (ExactBrush: exact planes, corners, bounds), rebuilt when the brush
// changes (ExactInputJob).
public NativeList<BlobAssetReference<ExactBrush>> exactBrushCache;
// the last ExactBrush.generation given out in this tree (ExactBrushes.SetGeneration)
public NativeReference<long> exactBrushGenerations;
// what ExactCSGJob built of each brush's faces, and from what, by node order (ExactFaceCache)
public NativeList<ExactFaceCache> exactFaceCache;
// what the last plane merge found (ExactPlaneMerge.MergeAgain)
public ExactPlaneMergeMemory planeMergeMemory;
// what the cleanup's last plan found for each brush (OutputMeshCleanup.PrepareFromMemory)
Expand Down Expand Up @@ -100,6 +104,8 @@ internal void Initialize()
brushesTouchedByBrushCache = new NativeList<BlobAssetReference<BrushesTouchedByBrush>>(1000, Allocator.Persistent); // Confirmed to get disposed
brushRenderBufferCache = new NativeList<BlobAssetReference<ChiselBrushRenderBuffer>>(1000, Allocator.Persistent); // Confirmed to get disposed
exactBrushCache = new NativeList<BlobAssetReference<ExactBrush>>(1000, Allocator.Persistent);
exactBrushGenerations = new NativeReference<long>(Allocator.Persistent);
exactFaceCache = new NativeList<ExactFaceCache>(1000, Allocator.Persistent);
planeMergeMemory = ExactPlaneMergeMemory.Create(Allocator.Persistent);
meshCleanupPlanMemory = MeshCleanupPlanMemory.Create(Allocator.Persistent);
meshSectionMemory = MeshSectionMemory.Create(Allocator.Persistent);
Expand Down Expand Up @@ -262,6 +268,16 @@ internal void Dispose()
exactBrushCache.Dispose();
}
exactBrushCache = default;
if (exactBrushGenerations.IsCreated)
exactBrushGenerations.Dispose();
exactBrushGenerations = default;
if (exactFaceCache.IsCreated)
{
for (int i = 0; i < exactFaceCache.Length; i++)
exactFaceCache[i].Dispose();
exactFaceCache.Dispose();
}
exactFaceCache = default;
planeMergeMemory.Dispose();
planeMergeMemory = default;
meshCleanupPlanMemory.Dispose();
Expand Down
320 changes: 273 additions & 47 deletions Core/2.Processing/Exact/ExactCSGJob.cs

Large diffs are not rendered by default.

124 changes: 124 additions & 0 deletions Core/2.Processing/Exact/ExactFaceCache.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
using System;
using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;
using Unity.Mathematics;

namespace Chisel.Core
{
// What a face was built from, of one brush that can reach it (ExactFaceCache.Reaches), in the order the brushes touch the
// face's brush: a face built from the same brushes, seen the same way, is built the same. The face builder reads of the
// brush only its planes (by its generation), what it gives each part of the face and its routing lookup: which brush
// it is is not part of it.
struct ExactFaceKey : IEquatable<ExactFaceKey>
{
public ulong generation; // ExactBrush.generation: its planes
public byte insideCategory; // the categories it gives the face's parts (ExactTouchingBrush)
public byte alignedCategory;
public byte reverseAlignedCategory;
public byte routes;
public int lookup; // its lookup in the face's brush's routing table, -1 for none

public bool Equals(ExactFaceKey other)
{
return generation == other.generation &&
insideCategory == other.insideCategory && alignedCategory == other.alignedCategory &&
reverseAlignedCategory == other.reverseAlignedCategory && routes == other.routes && lookup == other.lookup;
}
}

// One face of the brush as its last build wrote it
struct ExactFaceRecord
{
public int keyStart, keyCount; // into ExactFaceCache.keys
public int dataStart, dataCount; // into ExactFaceCache.data: what it wrote to the output (ExactCSGJob.EmitFace)
public int routingErrors; // ExactCSGStat.RoutingOutOfRange
public byte replayable; // 0 for a face not kept: one routed through the tables of brushes it gives way to
public byte faceFailure; // ExactFaceFailure
public byte triangulationFailure0; // ExactTriangulationFailure of its first two parts
public byte triangulationFailure1;
}

// What ExactCSGJob built of one brush's faces at the last update that built the brush, and what each face was built
// from. A face whose key is the same is written again as it was, not built: its own brush, its routing table and the
// brushes that can reach it are all it is built from (ExactFaceBuilder.BuildFace).
struct ExactFaceCache : IDisposable
{
public CompactNodeID nodeID; // which brush: the cache is by node order, which another brush can take
public ulong generation; // the brush's ExactBrush.generation
public byte collectDiscarded;
public UnsafeList<ushort> routingRows; // its routing table as level 0 of ExactRouting has it
public UnsafeList<int2> routingLookups; // each lookup's rows, start and end
public UnsafeList<ExactFaceRecord> faces; // by surface
public UnsafeList<ExactFaceKey> keys;
public UnsafeList<int> data;

public bool IsCreated => faces.IsCreated;

public static ExactFaceCache Create(Allocator allocator)
{
return new ExactFaceCache
{
routingRows = new UnsafeList<ushort>(64, allocator),
routingLookups = new UnsafeList<int2>(16, allocator),
faces = new UnsafeList<ExactFaceRecord>(8, allocator),
keys = new UnsafeList<ExactFaceKey>(32, allocator),
data = new UnsafeList<int>(256, allocator),
};
}

// A copy with every list as long as it needs to be, no longer: what is kept between updates
public ExactFaceCache Copy(Allocator allocator)
{
var copy = new ExactFaceCache
{
nodeID = nodeID,
generation = generation,
collectDiscarded = collectDiscarded,
routingRows = new UnsafeList<ushort>(math.max(1, routingRows.Length), allocator),
routingLookups = new UnsafeList<int2>(math.max(1, routingLookups.Length), allocator),
faces = new UnsafeList<ExactFaceRecord>(math.max(1, faces.Length), allocator),
keys = new UnsafeList<ExactFaceKey>(math.max(1, keys.Length), allocator),
data = new UnsafeList<int>(math.max(1, data.Length), allocator),
};
copy.routingRows.AddRange(routingRows);
copy.routingLookups.AddRange(routingLookups);
copy.faces.AddRange(faces);
copy.keys.AddRange(keys);
copy.data.AddRange(data);
return copy;
}

public void Dispose()
{
if (routingRows.IsCreated) routingRows.Dispose();
if (routingLookups.IsCreated) routingLookups.Dispose();
if (faces.IsCreated) faces.Dispose();
if (keys.IsCreated) keys.Dispose();
if (data.IsCreated) data.Dispose();
this = default;
}

// Whether brush `touching` can change face `face` of `own`: its bounds meet the face's, and no plane of it has every
// corner of the face strictly outside. One that can't makes no region of the face (ExactFaceBuilder.Coverage misses
// it) and has no interval on any line through it (the line's segment lies in the face, so Separated): it adds
// nothing to the face, and a lookup to it routes as Outside, as one to no brush does.
public static bool Reaches(ref ExactBrush own, int face, in ExactTouchingBrush touching, ref ExactList<ExactPlane> planes)
{
var faceBounds = own.faceBounds[face];
var bounds = touching.bounds;
if (!(math.all(bounds.Min <= faceBounds.Max) && math.all(bounds.Max >= faceBounds.Min)))
return false;
var range = own.faces[face];
for (int p = 0; p < touching.planeCount; p++)
{
var plane = planes[touching.planeOffset + p];
bool allOutside = true;
for (int e = 0; e < range.y && allOutside; e++)
allOutside = ExactPredicates.Side(plane, own.faceEdges[range.x + e].start) > 0;
if (allOutside && range.y > 0)
return false;
}
return true;
}
}
}
2 changes: 2 additions & 0 deletions Core/2.Processing/Exact/ExactFaceCache.cs.meta

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

44 changes: 40 additions & 4 deletions Core/2.Processing/Exact/ExactInputJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,39 @@ struct ExactBrush
public BlobArray<int2> faces;
public BlobArray<MinMaxAABB> faceBounds;
public BlobArray<ExactPolygonEdge> faceEdges;
// the same for every brush the CSG sees the same (SetGeneration): what the faces replayed from an earlier update
// were built with is known by it (ExactFaceCache)
public ulong generation;
}

static class ExactBrushes
{
// A brush built again keeps the generation of the one it replaces when the CSG sees it the same: the same planes
// and corner count, as all else it reads of the brush (its bounds, faces and their polygons) is made from them.
// Otherwise it takes a new one from the tree's counter.
internal static unsafe void SetGeneration(BlobAssetReference<ExactBrush> brush, BlobAssetReference<ExactBrush> previous, long* counter)
{
ref var value = ref brush.Value;
if (previous.IsCreated && SeenTheSame(ref previous.Value, ref value))
value.generation = previous.Value.generation;
else
value.generation = (ulong)Interlocked.Increment(ref *counter);
}

static bool SeenTheSame(ref ExactBrush a, ref ExactBrush b)
{
if (a.cornerCount != b.cornerCount || a.planes.Length != b.planes.Length)
return false;
for (int p = 0; p < a.planes.Length; p++)
{
var pa = a.planes[p];
var pb = b.planes[p];
if (pa.a != pb.a || pa.b != pb.b || pa.c != pb.c || !Int128.Equals(pa.w, pb.w))
return false;
}
return true;
}

public static BlobAssetReference<ExactBrush> Build(BlobAssetReference<BrushMeshBlob> meshRef, in float4x4 nodeToTree,
ref ExactFaceBuilder builder, ref ExactList<ExactVertex> corners,
out ExactCSGStat failure)
Expand Down Expand Up @@ -331,7 +360,7 @@ public static void Include(ref MinMaxAABB box, in ExactVertex corner)
}

[BurstCompile(CompileSynchronously = true)]
struct ExactInputJob : IJobParallelForDefer
unsafe struct ExactInputJob : IJobParallelForDefer
{
// Read
[NoAlias, ReadOnly] public NativeList<IndexOrder> rebuildTreeBrushIndexOrders;
Expand All @@ -343,6 +372,8 @@ struct ExactInputJob : IJobParallelForDefer
[NoAlias] public NativeArray<int> stats;
[NativeDisableParallelForRestriction]
[NoAlias] public NativeList<BlobAssetReference<ExactBrush>> exactBrushCache; // by node order
// the tree's brush generations (ExactBrushes.SetGeneration)
[NativeDisableUnsafePtrRestriction] public long* generationCounter;

// Write
[NoAlias, WriteOnly] public NativeList<BlobAssetReference<ExactBrush>>.ParallelWriter disposeList;
Expand All @@ -361,6 +392,7 @@ public unsafe void Execute(int index)
if (failure != ExactCSGStat.Count)
Interlocked.Increment(ref counters[(int)failure]);
var previous = exactBrushCache[nodeOrder];
ExactBrushes.SetGeneration(brush, previous, generationCounter);
if (previous.IsCreated)
disposeList.AddNoResize(previous);
exactBrushCache[nodeOrder] = brush;
Expand All @@ -374,7 +406,7 @@ public unsafe void Execute(int index)
}

[BurstCompile(CompileSynchronously = true)]
struct ExactBoundsJob : IJob
unsafe struct ExactBoundsJob : IJob
{
// Read
[NoAlias, ReadOnly] public NativeList<IndexOrder> allTreeBrushIndexOrders;
Expand All @@ -384,6 +416,8 @@ struct ExactBoundsJob : IJob
// Read / Write
[NoAlias] public NativeArray<int> stats;
[NoAlias] public NativeList<BlobAssetReference<ExactBrush>> exactBrushCache; // by node order
// the tree's brush generations (ExactBrushes.SetGeneration)
[NativeDisableUnsafePtrRestriction] public long* generationCounter;

// Write
[NoAlias] public NativeList<MinMaxAABB> bounds; // by node order
Expand All @@ -402,8 +436,10 @@ public void Execute()
int nodeOrder = allTreeBrushIndexOrders[b].nodeOrder;
if (!exactBrushCache[nodeOrder].IsCreated)
{
exactBrushCache[nodeOrder] = ExactBrushes.Build(brushMeshLookup[nodeOrder], transformationCache[nodeOrder].nodeToTree,
ref builder, ref corners, out var failure);
var brush = ExactBrushes.Build(brushMeshLookup[nodeOrder], transformationCache[nodeOrder].nodeToTree,
ref builder, ref corners, out var failure);
ExactBrushes.SetGeneration(brush, default, generationCounter);
exactBrushCache[nodeOrder] = brush;
stats[(int)ExactCSGStat.InputBrushes]++;
if (failure != ExactCSGStat.Count)
stats[(int)failure]++;
Expand Down
5 changes: 4 additions & 1 deletion Core/2.Processing/Exact/ExactPlaneMerge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,7 @@ public void Execute()

// The brushes the merge changed, built again with their new planes
[BurstCompile(CompileSynchronously = true)]
struct ExactMergedRebuildJob : IJobParallelForDefer
unsafe struct ExactMergedRebuildJob : IJobParallelForDefer
{
// Read
[NoAlias, ReadOnly] public NativeList<IndexOrder> mergedBrushIndexOrders;
Expand All @@ -807,6 +807,8 @@ struct ExactMergedRebuildJob : IJobParallelForDefer
[NoAlias] public NativeArray<int> stats;
[NativeDisableParallelForRestriction]
[NoAlias] public NativeList<BlobAssetReference<ExactBrush>> exactBrushCache; // by node order
// the tree's brush generations (ExactBrushes.SetGeneration)
[NativeDisableUnsafePtrRestriction] public long* generationCounter;

// Write
[NoAlias, WriteOnly] public NativeList<BlobAssetReference<ExactBrush>>.ParallelWriter disposeList;
Expand All @@ -825,6 +827,7 @@ public unsafe void Execute(int index)
builder.planes.Add(mergedPlanes[offset + p]);
var previous = exactBrushCache[nodeOrder];
var brush = ExactBrushes.Rebuild(ref previous.Value, ref builder, ref corners, out var failure);
ExactBrushes.SetGeneration(brush, previous, generationCounter);
if (failure != ExactCSGStat.Count)
{
var counters = (int*)stats.GetUnsafePtr();
Expand Down
Loading
Loading