Skip to content

Commit

Permalink
[Release] v. 1.1.0
Browse files Browse the repository at this point in the history
* NativeAccumulatedProduct

* NativeIndexedArray

* NativeIndexedList

* update README, CHANGELOG, and package.json
  • Loading branch information
andywiecko committed Dec 4, 2021
1 parent d1c9906 commit f965d25
Show file tree
Hide file tree
Showing 34 changed files with 1,177 additions and 2 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# Change log

## [1.1.0] - 2021-12-04

### Features

- `Id{T}` added.
- `NativeIndexedArray{Id, T}` added.
- `NativeIndexedList{Id, T}` added.
- `NativeAccumulatedProduct{T, Op}` added.
- `IAbelianOperator` and basic implementations added.

## [1.0.0] ⁠– 2021-11-28

### Features
Expand Down
79 changes: 79 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ Burst friendly (special) native collections for Unity.
- [BurstCollections](#burstcollections)
- [Getting started](#getting-started)
- [NativeStack{T}](#nativestackt)
- [NativeAccumulatedProduct{T, Op}](#nativeaccumulatedproductt-op)
- [NativeIndexedArray{Id, T}](#nativeindexedarrayid-t)
- [NativeIndexedList{Id, T}](#nativeindexedlistid-t)
- [BoundingVolumeTree{T}](#boundingvolumetreet)
- [Example usage](#example-usage)
- [Results](#results)
Expand Down Expand Up @@ -43,6 +46,82 @@ stack.Dispose();

Remarks: implementation probably will be deprecated in the future, when Unity team add stack implementation to `Unity.Collections`.

## NativeAccumulatedProduct{T, Op}

A wrapper which is especially useful for parallel calculation of the abelian operators, e.g. sum, min, max.
Generic parameter `Op` must implement the `IAbelianOperator<T>` interface

```csharp
public interface IAbelianOperator<TSelf> where TSelf : unmanaged
{
TSelf Product(TSelf a, TSelf b);
TSelf NeturalElement { get; }
}
```

Consider the following example

```csharp
var values = Enumerable.Range(0, 1024 * 1024).ToArray();
using var data = new NativeArray<int>(values, Allocator.Persistent);
using var product = new NativeAccumulatedProduct<int, IntSum>(Allocator.Persistent);

JobHandle dependencies = default;

dependencies = product.AccumulateProducts(data.AsReadOnly(), innerloopBatchCount: 64, dependencies).
dependencies = product.Combine(dependencies);

dependencies.Complete();

Debug.Log(product.Value); // Expected: 0 + 1 + 2 + ... + (1024 * 1024 - 1).
```

In the example presented above, we first, allocate the array with numbers: 0, 1, 2, 3, ..., then
we allocate buffers for accumulated products and we chose the `IntSum` operation.
Then after jobs completion, we obtain the sum of all elements, which
was done in **parallel**.

Supported operations:

- `int/int2/int3/int4`: `Int(2/3/4)Sum`, `Int(2/3/4)Min`, `Int(2/3/4)Max`,
- `float/float2/float3/float4`: `Float(2/3/4)Sum`, `Float(2/3/4)Min`, `Float(2/3/4)Max`,
- `AABB`: `AABBUnion`.

## NativeIndexedArray{Id, T}

Wrapper for `NativeArray<T>` which supports indexing via `Id<T>` instead of `int`, where `T` is a non-constraint generic parameter.
The collection is useful for enumeration of the specifically listed objects like triangles, circles, points, etc., and their properties.
Using `Id` could protect from errors related to reading from nonrelated buffer with the given type of objects.
Consider the following struct

```csharp
public readonly struct Triangle { /* ... */ }
```

Then using the `NativeIndexedArray` one can prepare the following collections
to group some triangle properties.

```csharp
using var triangles = new NativeIndexedArray<Id<Triangle>, Triangle>(128, Allocator.Persistent);
using var areas = new NativeIndexedArray<Id<Triangle>, float>(128, Allocator.Persistent);
using var neighborsCount = new NativeIndexedArray<Id<Triangle>, int>(128, Allocator.Persistent);
```

To access the elements one has to use `Id<Triangle>` instead of `int`

```csharp
var triangleId = (Id<Triangle>)42;

var triangle = triangles[triangleId];
var area = areas[triangleId];
var neighborCount = neighborsCount[triangleId];
```

## NativeIndexedList{Id, T}

Wrapper for `NativeList<T>` which supports indexing via `Id<T>` instead of `int`, where `T` is a non-constraint generic parameter.
See [NativeIndexedArray{Id, T}](#nativeindexedarrayid-t) for more details.

## BoundingVolumeTree{T}

A bounding volume tree is a data structure that is especially useful as supporting operations during collision detection or ray tracing algorithms.
Expand Down
8 changes: 8 additions & 0 deletions Runtime/NativeAccumulatedProduct.meta

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

8 changes: 8 additions & 0 deletions Runtime/NativeAccumulatedProduct/AABBAbelianOperators.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace andywiecko.BurstCollections
{
public readonly struct AABBUnion : IAbelianOperator<AABB>
{
public AABB NeutralElement => new AABB(min: float.MaxValue, max: float.MinValue);
public AABB Product(AABB a, AABB b) => a.Union(b);
}
}
11 changes: 11 additions & 0 deletions Runtime/NativeAccumulatedProduct/AABBAbelianOperators.cs.meta

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

84 changes: 84 additions & 0 deletions Runtime/NativeAccumulatedProduct/FloatAbelianOperators.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using Unity.Mathematics;

namespace andywiecko.BurstCollections
{
#region Float
public readonly struct FloatSum : IAbelianOperator<float>
{
public float NeutralElement => 0;
public float Product(float a, float b) => a + b;
}

public readonly struct FloatMin : IAbelianOperator<float>
{
public float NeutralElement => float.MaxValue;
public float Product(float a, float b) => math.min(a, b);
}

public readonly struct FloatMax : IAbelianOperator<float>
{
public float NeutralElement => float.MinValue;
public float Product(float a, float b) => math.max(a, b);
}
#endregion

#region Float2
public readonly struct Float2Sum : IAbelianOperator<float2>
{
public float2 NeutralElement => 0;
public float2 Product(float2 a, float2 b) => a + b;
}

public readonly struct Float2Min : IAbelianOperator<float2>
{
public float2 NeutralElement => float.MaxValue;
public float2 Product(float2 a, float2 b) => math.min(a, b);
}

public readonly struct Float2Max : IAbelianOperator<float2>
{
public float2 NeutralElement => float.MinValue;
public float2 Product(float2 a, float2 b) => math.max(a, b);
}
#endregion

#region Float3
public readonly struct Float3Sum : IAbelianOperator<float3>
{
public float3 NeutralElement => 0;
public float3 Product(float3 a, float3 b) => a + b;
}

public readonly struct Float3Min : IAbelianOperator<float3>
{
public float3 NeutralElement => float.MaxValue;
public float3 Product(float3 a, float3 b) => math.min(a, b);
}

public readonly struct Float3Max : IAbelianOperator<float3>
{
public float3 NeutralElement => float.MinValue;
public float3 Product(float3 a, float3 b) => math.max(a, b);
}
#endregion

#region Float4
public readonly struct Float4Sum : IAbelianOperator<float4>
{
public float4 NeutralElement => 0;
public float4 Product(float4 a, float4 b) => a + b;
}

public readonly struct Float4Min : IAbelianOperator<float4>
{
public float4 NeutralElement => float.MaxValue;
public float4 Product(float4 a, float4 b) => math.min(a, b);
}

public readonly struct Float4Max : IAbelianOperator<float4>
{
public float4 NeutralElement => float.MinValue;
public float4 Product(float4 a, float4 b) => math.max(a, b);
}
#endregion
}
11 changes: 11 additions & 0 deletions Runtime/NativeAccumulatedProduct/FloatAbelianOperators.cs.meta

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

8 changes: 8 additions & 0 deletions Runtime/NativeAccumulatedProduct/IAbelianOperator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace andywiecko.BurstCollections
{
public interface IAbelianOperator<TSelf> where TSelf : unmanaged
{
TSelf Product(TSelf a, TSelf b);
TSelf NeutralElement { get; }
}
}
11 changes: 11 additions & 0 deletions Runtime/NativeAccumulatedProduct/IAbelianOperator.cs.meta

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

84 changes: 84 additions & 0 deletions Runtime/NativeAccumulatedProduct/IntAbelianOperators.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using Unity.Mathematics;

namespace andywiecko.BurstCollections
{
#region Int
public readonly struct IntSum : IAbelianOperator<int>
{
public int NeutralElement => 0;
public int Product(int a, int b) => a + b;
}

public readonly struct IntMin : IAbelianOperator<int>
{
public int NeutralElement => int.MaxValue;
public int Product(int a, int b) => math.min(a, b);
}

public readonly struct IntMax : IAbelianOperator<int>
{
public int NeutralElement => int.MinValue;
public int Product(int a, int b) => math.max(a, b);
}
#endregion

#region Int2
public readonly struct Int2Sum : IAbelianOperator<int2>
{
public int2 NeutralElement => 0;
public int2 Product(int2 a, int2 b) => a + b;
}

public readonly struct Int2Min : IAbelianOperator<int2>
{
public int2 NeutralElement => int.MaxValue;
public int2 Product(int2 a, int2 b) => math.min(a, b);
}

public readonly struct Int2Max : IAbelianOperator<int2>
{
public int2 NeutralElement => int.MinValue;
public int2 Product(int2 a, int2 b) => math.max(a, b);
}
#endregion

#region Int3
public readonly struct Int3Sum : IAbelianOperator<int3>
{
public int3 NeutralElement => 0;
public int3 Product(int3 a, int3 b) => a + b;
}

public readonly struct Int3Min : IAbelianOperator<int3>
{
public int3 NeutralElement => int.MaxValue;
public int3 Product(int3 a, int3 b) => math.min(a, b);
}

public readonly struct Int3Max : IAbelianOperator<int3>
{
public int3 NeutralElement => int.MinValue;
public int3 Product(int3 a, int3 b) => math.max(a, b);
}
#endregion

#region Int4
public readonly struct Int4Sum : IAbelianOperator<int4>
{
public int4 NeutralElement => 0;
public int4 Product(int4 a, int4 b) => a + b;
}

public readonly struct Int4Min : IAbelianOperator<int4>
{
public int4 NeutralElement => int.MaxValue;
public int4 Product(int4 a, int4 b) => math.min(a, b);
}

public readonly struct Int4Max : IAbelianOperator<int4>
{
public int4 NeutralElement => int.MinValue;
public int4 Product(int4 a, int4 b) => math.max(a, b);
}
#endregion
}
11 changes: 11 additions & 0 deletions Runtime/NativeAccumulatedProduct/IntAbelianOperators.cs.meta

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

Loading

0 comments on commit f965d25

Please sign in to comment.