• Stars
    star
    152
  • Rank 243,434 (Top 5 %)
  • Language
    C#
  • License
    MIT License
  • Created over 4 years ago
  • Updated about 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Zero-copy sharing between managed and native arrays in Unity

SharedArray

A SharedArray is a segment of memory that is represented both as a normal C# array T[], and a Unity NativeArray<T>.

It's designed to reduce the overhead of communicating between C# job data in NativeArray and APIs that use a normal array of structs, such as Graphics.DrawMeshInstanced(), by eliminating the need to copy data.

Installation

Minimum Unity version is 2018.4.

To install, grab the latest .unitypackage from the Releases Tab and import it to your project.

Usage

// SharedArray implicitly converts to both managed and native array
SharedArray<Vector4> shared = new SharedArray<Vector4>(8);
NativeArray<Vector4> asNative = shared;
Vector4[] asManaged = shared;

Please see the demo project for a more detailed usage example.

Safety System

Unity's job system has a safety system for reading & writing data (in the Editor only). This catches cases where a data race would occur and warns you about it.

SharedArray works with this safety system, so when you access the data on the main thread, the system knows whether it is safe to read or write, just like using a NativeArray allocated the normal way.

Here's all of the operations that include a check of the safety system.

SharedArray<T> sharedArray;            // created elsewhere 

// These 4 operations will check that no jobs are using the data, in any way
T[] asNormalArray = sharedArray; 
sharedArray.Clear();
sharedArray.Resize(32);
sharedArray.Dispose();

// Enumerating in either of these ways will check if any jobs are writing to the data, but allow other readers
foreach(var element in sharedArray) { }

var enumerator = sharedArray.GetEnumerator();

The safest way to use SharedArray is:

  1. Manipulate data in a C# job, using the NativeArray<T> representation
  2. Convert to a managed array T[] only right before you use it on the main thread.

This is important if you want the safety system to work - if you pass around a reference to the managed representation, you won't get the safety system checks.

You can see this pattern demonstrated in the demo project.

Aliasing

It's possible to have the NativeArray representation of the data be of a different type than the source managed array.

To do so, create the SharedArray with 2 types instead of 1 :

Vector4[] source = new Vector4[64];
SharedArray<Vector4, float4> shared = new SharedArray<Vector4, float4>(source);
NativeArray<float4> native = shared;
Vector4[] asManaged = shared;

The only safety check that aliasing makes is that the types are both unmanaged and the same size.

Why Alias Types ?

Aliasing was made to eliminate the overhead of converting between analogous types in Unity.Mathematics and UnityEngine (such as float4 <-> Vector4 or float4x4 <-> Matrix4x4).

These Unity.Mathematics types have optimizations specific to the Burst compiler, and replace the existing Unity math structs and methods. We want to get the compiler-specific performance advantage of using those new types, without the overhead of converting back from Unity.Mathematics types.

For types that are laid out the same in memory, we can just treat one like the other. Since we do this for the whole array, there is never any conversion between types happening, and thus no overhead - it's just a different "view" on the same memory.

More Repositories

1

job-system-cookbook

Unity Technologies management has fucked everything up. this is a guide to the job system circa 2019
C#
1,384
star
2

OscCore

A performance-oriented OSC library for Unity
C#
142
star
3

Resolink

Unity package to make integrating with Resolume (a VJing software) easier
C#
64
star
4

weekend-tracer

An implementation of 'Ray Tracing in One Weekend', using C# and Unity's Burst compiler
C#
45
star
5

BurstImageProcessing

fast parallel image processing on the CPU, using Unity C# jobs and Burst compilation
C#
21
star
6

SharedArray-Demo

Demo Unity project for SharedArray library
C#
19
star
7

vision-union

simple CV pipeline prototype using Unity C# jobs
C#
18
star
8

SchlickCurve

Unity implementation of a generalization of Schlick’s bias & gain functions
C#
10
star
9

ByteStrings

Minimal implementation of storing strings in native memory
C#
8
star
10

BlobHandles

A way to use chunks of bytes as hash keys, for Unity
C#
6
star
11

NtpTimestamp

A representation of 64-bit NTP timestamps for use in other projects
C#
3
star
12

DotGraph

Basic graph theory constructs implemented via the Unity C# Job System
C#
3
star
13

pubbable

DAO-ish thing for cocktail bars
TypeScript
3
star
14

star

toy 3d engine
C++
2
star
15

micro-benchmarks

C# micro benchmarks for Unity
C#
2
star
16

solana-data

toy CLI tool for accessing & analyzing Solana blockchain data
Rust
1
star
17

ucharts-prototype

C#
1
star
18

readback-loop-demo

C#
1
star
19

Serial-Object-Stream

do you need to serialize a fuckton of some object to disk in Unity, but you can't stop the app to do so? probably not, but that's what this does
C#
1
star
20

AR-Clips

C#
1
star
21

rave-strategies

a port of https://github.com/datacorruption/Rave-Strategies to the browser
JavaScript
1
star
22

ExternJobs-Project

Auto-generate wrappers to integrate native code with Unity's C# job system
C#
1
star