It seems like burst function pointers work fine when using boolean parameters as part of the method signature (MyMethod(bool, int) for example), but according to the documentation https://docs.unity3d.com/Packages/com.unity.burst@1.8/manual/csharp-burst-intrinsics-dllimport.html bool is not part of the built-in supported types. Is this supported?
#Are bool parameters suported in burst function pointers?
1 messages · Page 1 of 1 (latest)
The bool type is really not supported, because it is not a blittable type in .NET (https://learn.microsoft.com/en-us/dotnet/framework/interop/blittable-and-non-blittable-types). Burst only support blittable types for this case.
.NET treats a managed bool type as 4 bytes when it maps across a p/invoke boundary (this is to match the way the Win32 C API treats bool types). But bool in C++ is only 1 byte. So something bool types seem to work, but code code in that case maybe reading four bytes, and instead of one byte, so those extra three bytes maight happen to be a value that makes sense (maybe zero, for example). But in other cases maybe those 3 bytes are used for something else, and now your code is reading bad data.
I see, currently compiling signatures with booleans doesn't log any warnings/errors so it might be a good idea to add some. If the function pointer were only to be used by native code and not by C# would it be safe to have booleans as part of the signature?
That might be OK, but I would play it safe and use byte instead.
I don't expect we will add errors or warnings, as .NET similarly does not warn or error for this.
Any time we get into p/invoke territory there are plenty of things to watch out for. 🙂
If I were to use byte how would I check for true/false? Just check if it's diferrent from 0?
Yes