Example #1
0
    private void DoExample()
    {
        NativeArray <float> resultArray = new NativeArray <float>(1, Allocator.TempJob);

        // 1. Instantiate Job
        SimpleJob myJob = new SimpleJob
        {
            // 2. Initialize Job
            a      = 5.0f,
            result = resultArray
        };
        AnotherJob secondJob = new AnotherJob
        {
            result = resultArray
        };

        // 3. Shedule Job
        JobHandle handle       = myJob.Schedule();
        JobHandle secondHandle = secondJob.Schedule(handle); // !! передаем дескриптор во вторую работу

        // 4. Complete Job
        handle.Complete();  // работу следует завершить прежде, чем пользоваться результатами
        secondHandle.Complete();

        float resultingValue = resultArray[0];

        Debug.Log("result = " + resultingValue);

        resultArray.Dispose();
    }
Example #2
0
    private void DoExample()
    {
        NativeArray <float> resultArray = new NativeArray <float>(1, Allocator.TempJob);
        SampleJob           job1        = new SampleJob()
        {
            a      = 100,
            result = resultArray,
        };

        AnotherJob job2 = new AnotherJob()
        {
            result = resultArray,
        };


        var handle = job1.Schedule();

        handle.Complete();
        job2.Schedule().Complete();
        var value = job1.result[0];

        Debug.Log($"result : {value}");
        Debug.Log($"job.a : {job1.a}");
        resultArray.Dispose();
    }
Example #3
0
    private void DoExample()
    {
        // create & set aside data as early as possible:
        NativeArray <float> resultArray = new NativeArray <float>(1, Allocator.TempJob);

        // instantiate the job
        //SimpleJob myJob = new SimpleJob();
        // initialize the job instance with data
        //myJob.a = 5;
        //myJob.result = new NativeArray<float>(1, Allocator.TempJob);

        // Do the above but using object initializer syntax:
        SimpleJob myJob = new SimpleJob
        {
            a      = 5f,
            result = resultArray
        };
        AnotherJob secondJob = new AnotherJob
        {
            result = resultArray
        };
        // Allocator controls how long the job's data lasts. Most common options are:
        // Temp = 1 frame
        // TempJob = lifetime of Job or 4 frames, whichever comes first
        // Persistent = as long as you need

        // schedule the job instance into the CPU
        JobHandle handle       = myJob.Schedule();
        JobHandle secondHandle = secondJob.Schedule(handle); // requires handle to complete before running!

        // ...
        // other tasks to run in Main Thread in parallel with other jobs:
        // ...

        //handle.Complete(); // dependency of handles implies the first one already completes
        secondHandle.Complete();

        // job must complete before accessing job data, especially native container data!
        float resultValue = resultArray[0];

        Debug.Log($"Result was {resultValue}");         // gets value from native container, so we can access it
        Debug.Log($"myJob.a has a value of {myJob.a}"); // gets value from copy of job data, does not
        resultArray.Dispose();
    }
Example #4
0
    private void DoExample()
    {
        NativeArray <float> resultArray = new NativeArray <float>(1, Allocator.TempJob);

        //Instantiate a job
        SimpleJob myJob = new SimpleJob
        {
            //Initialize data to work with
            a      = 5f,
            result = resultArray
        };

        AnotherJob secondJob = new AnotherJob();

        secondJob.result = resultArray;



        //Initialize data to work with
        //myJob.a = 5f;
        //myJob.result = new NativeArray<float>(1, Allocator.TempJob);



        //Schedule the job
        JobHandle handle       = myJob.Schedule();
        JobHandle secondHandle = secondJob.Schedule(handle);



        //Other tasks within the main thread
        //handle.Complete();
        secondHandle.Complete();

        float resultingValue = resultArray[0];

        Debug.Log("Result: " + resultingValue);

        resultArray.Dispose();
    }
    private void DoExample()
    {
        // Allocate Memorys
        NativeArray <float> resultArray = new NativeArray <float>(1, Allocator.TempJob);

        // 1. instantiate
        SimpleJob firstJob = new SimpleJob
        {
            // 2. initialize
            a      = 5f,
            arrays = resultArray
        };

        AnotherJob secondJob = new AnotherJob
        {
            arrays = resultArray
        };

        // 3. schedule. Best Utilize, Schedule Early
        JobHandle fhandle = firstJob.Schedule();
        JobHandle shandle = secondJob.Schedule(fhandle);

        // other tasks to run in Main Thread in parallel. Best Utilize, Complete late
        // Complete 전, Native Variable에 접근할 경우 Error 발생

        // fhandle.Complete(); <- shandle이 대신함
        shandle.Complete();

        float resultingValue = resultArray[0];

        Debug.Log("Result = " + resultingValue);
        Debug.Log("job A = " + firstJob.a);

        // reAllocate Memorys
        resultArray.Dispose();
    }
Example #6
0
    private void DoExample()
    {
        // Initialize variables
        //
        NativeArray <float> resultArray = new NativeArray <float>(1, Allocator.TempJob);

        // Instantiate Job
        Simplejob simplejob = new Simplejob()
        {
            variable    = 5f,
            resultArray = resultArray
        };

        AnotherJob secondJob = new AnotherJob();

        secondJob.resultArray = resultArray;

        // Schedule Job
        JobHandle handle = simplejob.Schedule <Simplejob>();

        //!! When you pass the handle, you make a dependency of this job with the first one
        //!! The first job automatically completes, so you don't have to do it
        JobHandle secondHandle = secondJob.Schedule <AnotherJob>(handle);

        // Wait for it to complete.
        //!! Normally you access or complete the job from other threads
        //!! Cannot access native array data without completing the job
        //handle.Complete();
        secondHandle.Complete();

        // Get result
        Debug.Log("Result of the example is: " + resultArray[0]);

        // Disposing of the array
        resultArray.Dispose();
    }