Auburn / FastNoise2

Modular node graph based noise generation library using SIMD, C++17 and templates
MIT License
1.01k stars 106 forks source link

Crash when using c# bindings #130

Open Skosn opened 7 months ago

Skosn commented 7 months ago

Hi, I'm using the library with the c# bindings inside unity and get crashes from time to time. Here are the stacktraces:

========== OUTPUTTING STACK TRACE ==================

0x00007FFC6A31A269 (FastNoise) FastSIMD::ClassFactory<FastNoise::White,4> 0x00007FFC6A32C21C (FastNoise) FastSIMD::ClassFactory<FastNoise::White,4> 0x00007FFC6A2E5AAF (FastNoise) fnGenUniformGrid2D 0x000002447C334E0C (Mono JIT Code) (wrapper managed-to-native) FastNoise:fnGenUniformGrid2D (intptr,single[],int,int,int,int,single,int,single[]) 0x000002447C334643 (Mono JIT Code) [J:\Unity\Nyrn\Assets\Code\Third-Party\FastNoise2.cs:177] FastNoise:GenUniformGrid2D (single[],int,int,int,int,single,int) 0x000002447C328EAB (Mono JIT Code) [J:\Unity\Nyrn\Assets\Code\WorldGeneration\TileTerrainJob.cs:86] Nyrn.WorldGeneration.TileTerrainJob:Nyrn.WorldGeneration.IGenerationJob.Execute (object) 0x00000242D903E0AA (Mono JIT Code) System.Threading.QueueUserWorkItemCallback:WaitCallback_Context (object) 0x00000242C91CA0BE (Mono JIT Code) System.Threading.ExecutionContext:RunInternal (System.Threading.ExecutionContext,System.Threading.ContextCallback,object,bool) 0x00000242C91C969B (Mono JIT Code) System.Threading.ExecutionContext:Run (System.Threading.ExecutionContext,System.Threading.ContextCallback,object,bool) 0x00000242D903DED3 (Mono JIT Code) System.Threading.QueueUserWorkItemCallback:System.Threading.IThreadPoolWorkItem.ExecuteWorkItem () 0x00000242D9039039 (Mono JIT Code) System.Threading.ThreadPoolWorkQueue:Dispatch () 0x00000242D9037DF3 (Mono JIT Code) System.Threading._ThreadPoolWaitCallback:PerformWaitCallback () 0x00000242D9038365 (Mono JIT Code) (wrapper runtime-invoke) :runtime_invoke_bool (object,intptr,intptr,intptr) 0x00007FFC22FD4B7E (mono-2.0-bdwgc) [C:\build\output\Unity-Technologies\mono\mono\mini\mini-runtime.c:3445] mono_jit_runtime_invoke 0x00007FFC22F0D204 (mono-2.0-bdwgc) [C:\build\output\Unity-Technologies\mono\mono\metadata\object.c:3068] do_runtime_invoke 0x00007FFC22F4E9AD (mono-2.0-bdwgc) [C:\build\output\Unity-Technologies\mono\mono\metadata\threadpool.c:386] worker_callback 0x00007FFC22F51C13 (mono-2.0-bdwgc) [C:\build\output\Unity-Technologies\mono\mono\metadata\threadpool-worker-default.c:502] worker_thread 0x00007FFC22F3F98B (mono-2.0-bdwgc) [C:\build\output\Unity-Technologies\mono\mono\metadata\threads.c:1272] start_wrapper_internal 0x00007FFC22F3FB9E (mono-2.0-bdwgc) [C:\build\output\Unity-Technologies\mono\mono\metadata\threads.c:1348] start_wrapper 0x00007FFCA0D97344 (KERNEL32) BaseThreadInitThunk 0x00007FFCA13A26B1 (ntdll) RtlUserThreadStart

========== END OF STACKTRACE ===========

================================================================= Managed Stacktrace:

  at <unknown> <0xffffffff>
  at FastNoise:fnGenUniformGrid2D <0x001bb>
  at FastNoise:GenUniformGrid2D <0x00142>
  at Nyrn.WorldGeneration.TileTerrainJob:Nyrn.WorldGeneration.IGenerationJob.Execute <0x00a4a>
  at System.Threading.QueueUserWorkItemCallback:WaitCallback_Context <0x000b9>
  at System.Threading.ExecutionContext:RunInternal <0x0061d>
  at System.Threading.ExecutionContext:Run <0x0008a>
  at System.Threading.QueueUserWorkItemCallback:System.Threading.IThreadPoolWorkItem.ExecuteWorkItem <0x00112>
  at System.Threading.ThreadPoolWorkQueue:Dispatch <0x00778>
  at System.Threading._ThreadPoolWaitCallback:PerformWaitCallback <0x000c2>
  at <Module>:runtime_invoke_bool <0x00184>

=================================================================

Skosn commented 7 months ago

The bindings are setting SIMD Level 0 by default, when changing it to something like e.g. 1 << 8 (AVX2) the crash disappears. Hope that information helps.

public static FastNoise FromEncodedNodeTree(string encodedNodeTree) { IntPtr nodeHandle = fnNewFromEncodedNodeTree(encodedNodeTree);

    if (nodeHandle == IntPtr.Zero)
    {
        return null;
    }

    return new FastNoise(nodeHandle);
}
Auburn commented 7 months ago

I think this is caused by a threading race condition when detecting the SIMD level.

As a workaround you can call GetSIMDLevel() at the start of the app to init this before any threading

Skosn commented 7 months ago

That seemed to work, thanks!

Skosn commented 7 months ago

Sadly it's still crashing, just happening less :/