Created
December 4, 2019 08:17
-
-
Save tsubaki/bc97e27677a2f2d172b54009776ef32b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using Unity.Jobs.LowLevel; | |
using Unity.Jobs; | |
using Unity.Jobs.LowLevel.Unsafe; | |
using Unity.Collections; | |
using Unity.Collections.LowLevel; | |
using Unity.Collections.LowLevel.Unsafe; | |
public class PositionUpdateSystem : MonoBehaviour | |
{ | |
JobHandle handle; | |
void Awake() | |
{ | |
JobsUtility.JobWorkerCount = 2; | |
Debug.Log(JobsUtility.JobWorkerMaximumCount - 1); | |
} | |
void OnDestroy() | |
{ | |
handle.Complete(); | |
} | |
void Update() | |
{ | |
handle.Complete(); | |
var array = new NativeArray<int>(JobsUtility.MaxJobThreadCount, Allocator.TempJob); | |
var counter = new NativeArray<int>(1, Allocator.TempJob); | |
handle = new CountJob { array = array }.Schedule( 24576, 8 ); | |
handle = new GatherJob { array = array, counter = counter }.Schedule(handle); | |
handle = new LogJob { counter = counter }.Schedule(handle); | |
array.Dispose(handle); | |
counter.Dispose(handle); | |
JobHandle.ScheduleBatchedJobs(); | |
} | |
//[Unity.Burst.BurstCompile] | |
struct CountJob : IJobParallelFor | |
{ | |
[NativeSetThreadIndex] | |
int threadIndex; | |
[NativeDisableContainerSafetyRestriction] | |
public NativeArray<int> array; | |
public void Execute(int index) | |
{ | |
var count = array[threadIndex]; | |
count= count + 1; | |
array[threadIndex] = count; | |
} | |
} | |
//[Unity.Burst.BurstCompile] | |
struct GatherJob : IJob | |
{ | |
[ReadOnly] | |
public NativeArray<int> array; | |
[WriteOnly] | |
public NativeArray<int> counter; | |
public void Execute() | |
{ | |
var count = 0; | |
for (int index = 0; index < array.Length; index++) | |
{ | |
count = count + array[index]; | |
} | |
counter[0] = count; | |
} | |
} | |
struct LogJob : IJob | |
{ | |
[ReadOnly] | |
public NativeArray<int> counter; | |
public void Execute() | |
{ | |
Debug.Log(counter[0]); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment