[Burst] Access Burst function IntPtr

I’m currently creating a system that needs to call one of 100+ functions on some data. At the moment, everything is set up in an IJobChunk, I put all the relevant data from Components into NativeArrays, and passing said NativeArrays by ref, invoke a FunctionPointer that I set on creation of the Job struct using Marshal.GetFunctionPointerForDelegate. However, it seems like the resulting IntPtr points to the non-Burst version of the relevant function, as in addition to a huge (20x) performance hit, I’m able to inspect it with Deep Profiling, whereas when I directly call the same function instead of using the FunctionPointer, the Job struct’s only child in the Profiler’s hierarchy is JobAlloc.Grow.

So my question is, how do I get a FunctionPointer to point to a Burst-compiled function?

Alternatively if that’s not possible, would there be a way of invoking one of 100 or so Burst-compiled functions without creating a monolithic if-statement binary-tree?

I think you are searching for BurstCompiler.CompileFunctionPointer(T delegateMethod), it returns a custom FunctionPointer struct.

2 Likes

You da MVP, seems exactly what I was after.
That should really be in the docs page for FunctionPointer. In fact it looks like it was going to be, but it just cuts off at “It needs to be compiled through” and just ends right there, without saying what, haha.
At the moment I’m getting a generic error “Burst failed to compile the given delegate”, but I’ll probably be able to figure out what’s wrong. Will edit with update.

Edit: Turns out even if the method is within a [BurstCompile]d struct, it needs to be explicitly tagged with [BurstCompile]. Thanks again, kind stranger!

2 Likes

Hello,
thanks for highlighting the issue in the docs. We’ll fix it as soon as possible.
And yes, the method needs the explicit attribute in this case, glad you found out :slight_smile:

Thanks

1 Like