Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Calling native code from Burst optimized jobs

Discussion in 'Data Oriented Technology Stack' started by korzen303, Apr 14, 2018.

  1. korzen303

    korzen303

    Joined:
    Oct 2, 2012
    Posts:
    172
    I need to call a native code function in a Burst optimized job

    Code (CSharp):
    1.        
    2. [ComputeJobOptimization]
    3. public struct TreeRefitJob : IJob
    4. {
    5.     [NativeDisableUnsafePtrRestriction]
    6.     public void* treePtr;
    7.  
    8.     [NativeDisableUnsafePtrRestriction]
    9.     public void* positionsPtr;
    10.  
    11.     [NativeDisableUnsafePtrRestriction]
    12.     public void* trianglesPtr;
    13.  
    14.     public int trianglesCount;
    15.  
    16.     public void Execute()
    17.     {
    18.         DbvtWrapper.DbvtRefitTriangles(treePtr, positionsPtr, trianglesPtr, trianglesCount);
    19.  
    20.         //DO SOME HEAVY MATH HERE
    21.     }
    22.  
    23. }
    24.  
    but I am getting the following error:

    Code (CSharp):
    1. Unexpected exception Burst.Compiler.IL.CompilerException: Missing external function `name: DefKit.DbvtWrapper::DbvtRefitTriangles` => `method: System.Void DefKit.DbvtWrapper::DbvtRefitTriangles(System.Void*,System.Void*,System.Void*,System.Int32). Reason: Function pointer cannot be IntPtr.Zero`
    2.  
    3.   at Burst.Compiler.IL.Jit.JitCompiler.CompileMethod (Mono.Cecil.MethodReference methodReference, Burst.Compiler.IL.Jit.JitOptions jitOptions) [0x003e8] in <ff35cf56a4e14266be5fb4c5a298e1c8>:0
    4.   at Burst.Compiler.IL.Jit.JitCompilerService.Compile (Burst.Compiler.IL.Jit.JitCompilerService+CompileJob job) [0x002a6] in <ff35cf56a4e14266be5fb4c5a298e1c8>:0
    5.  
    6. While compiling job: System.Void Unity.Jobs.IJobExtensions/JobStruct`1<DefKit.DbvtTree/TreeRefitJob>::Execute(T&,System.IntPtr,System.IntPtr,Unity.Jobs.LowLevel.Unsafe.JobRanges&,System.Int32)
     
  2. recursive

    recursive

    Joined:
    Jul 12, 2012
    Posts:
    595
    I don't think Burst supports calls into native yet (among a lot of other features it should support eventually). You may want to split the calls into a 2 job chain:
    * A non-Burst job that calls your native code.
    * A burst-enabled job that has your HEAVY MATH, dependent on the first job.

    Hopefully that works.
     
  3. korzen303

    korzen303

    Joined:
    Oct 2, 2012
    Posts:
    172
    Thanks @recursive, I know that I can split the job in two but then I am getting a performance hit from transferring the temporary data between jobs. Maybe in the above example it is not that visible but for sure that might be a case.
     
  4. superpig

    superpig

    Quis aedificabit ipsos aedificatores? Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,194
    Why? Aren't both your jobs just going to be pointing at the same buffers?
     
  5. korzen303

    korzen303

    Joined:
    Oct 2, 2012
    Posts:
    172
    yes, but you need to store and re-read data from job to job, which requires slow memory accesses.
    On the GPUs this is might be a performance bottleneck.
     
  6. GabrieleUnity

    GabrieleUnity

    Unity Technologies

    Joined:
    Sep 4, 2012
    Posts:
    116
    @korzen303 This is not supported ATM, but it will likely be in the official release.