Calling Draw.WireBox() recursively causes a crash

  • ALINE version: 1.7.4
  • Unity version: 6000.0.18f1
  • Render pipeline: HDRP

Using this piece of code called from a DrawGizmos() result in the editor crashing

public void DrawNode()
        {
            Draw.WireBox(bounds,Color.green);

            foreach (OctreeObject obj in objects)
            {
                if (obj.Intersects(bounds))
                { 
                    Draw.WireBox(bounds, Color.red);
                }
            }

            if (children != null)
            {
                foreach (OctreeNode octreeNode in children)
                {
                    if (octreeNode != null) octreeNode.DrawNode();
                }
            }
        }

Hi

Do you have any more information about the error message when it crashes?
Are you sure you are not accidentally recursing indefinitely?

I’m sure I’m not recusing indefinitely since the tree is being drawn with regular Gizmos.Draw calls but that is very slow.
This is the stacktrace I am getting :

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

0x00007FFC896EFC8C (Unity) block_locate_free
0x00007FFC896F0432 (Unity) tlsf_memalign
0x00007FFC872279AB (Unity) DynamicHeapAllocator::Allocate
0x00007FFC872281DD (Unity) MemoryManager::Allocate
0x00007FFC8722867B (Unity) PerThreadPageAllocatorArena::AllocatePage
0x00007FFC8722C004 (Unity) PerThreadPageAllocator::Initialize
0x00007FFC8732E606 (Unity) BeginRenderQueueExtraction
0x00007FFC8757D566 (Unity) PrepareDrawShadowsCommandStep1
0x00007FFC87579633 (Unity) ScriptableRenderContext::ExecuteScriptableRenderLoop
0x00007FFC86B4D8C4 (Unity) ScriptableRenderContext_CUSTOM_Submit_Internal
0x00000133228299E4 (Mono JIT Code) (wrapper managed-to-native) UnityEngine.Rendering.ScriptableRenderContext:Submit_Internal (UnityEngine.Rendering.ScriptableRenderContext&)
0x0000013322829823 (Mono JIT Code) UnityEngine.Rendering.ScriptableRenderContext:Submit ()
0x000001331E9C53CB (Mono JIT Code) [.\Library\PackageCache\com.unity.render-pipelines.high-definition\Runtime\RenderPipeline\HDRenderPipeline.cs:2402] UnityEngine.Rendering.HighDefinition.HDRenderPipeline:Render (UnityEngine.Rendering.ScriptableRenderContext,System.Collections.Generic.List`1<UnityEngine.Camera>) 
0x000001331E9BE1E9 (Mono JIT Code) UnityEngine.Rendering.RenderPipeline:InternalRender (UnityEngine.Rendering.ScriptableRenderContext,System.Collections.Generic.List`1<UnityEngine.Camera>)
0x0000013311D4F13B (Mono JIT Code) UnityEngine.Rendering.RenderPipelineManager:DoRenderLoop_Internal (UnityEngine.Rendering.RenderPipelineAsset,intptr,UnityEngine.Object,Unity.Collections.LowLevel.Unsafe.AtomicSafetyHandle)
0x0000013311D4F645 (Mono JIT Code) (wrapper runtime-invoke) <Module>:runtime_invoke_void_object_intptr_object_AtomicSafetyHandle (object,intptr,intptr,intptr)
0x00007FFCA8CD697E (mono-2.0-bdwgc) [C:\build\output\Unity-Technologies\mono\mono\mini\mini-runtime.c:3445] mono_jit_runtime_invoke 
0x00007FFCA8C18444 (mono-2.0-bdwgc) [C:\build\output\Unity-Technologies\mono\mono\metadata\object.c:3068] do_runtime_invoke 
0x00007FFCA8C18530 (mono-2.0-bdwgc) [C:\build\output\Unity-Technologies\mono\mono\metadata\object.c:3115] mono_runtime_invoke 
0x00007FFC879C29B4 (Unity) scripting_method_invoke
0x00007FFC8799A983 (Unity) ScriptingInvocation::Invoke
0x00007FFC87995C45 (Unity) ScriptingInvocation::Invoke<void>
0x00007FFC87B20B0F (Unity) Scripting::UnityEngine::Rendering::RenderPipelineManagerProxy::DoRenderLoop_Internal
0x00007FFC8757BA7C (Unity) ScriptableRenderContext::ExtractAndExecuteRenderPipeline
0x00007FFC88B91A0B (Unity) Camera::ExecuteCustomRenderPipeline
0x00007FFC88B958CC (Unity) Camera::RenderEditorCamera
0x00007FFC88B975BE (Unity) Camera::RenderEditorCamera
0x00007FFC86FAEED0 (Unity) Handles_CUSTOM_Internal_DrawCameraWithGrid
0x0000013311D5F93E (Mono JIT Code) (wrapper managed-to-native) UnityEditor.Handles:Internal_DrawCameraWithGrid_Injected (intptr,UnityEditor.DrawCameraMode,UnityEditor.DrawGridParameters&,bool,bool)
0x0000013311D5F7C3 (Mono JIT Code) UnityEditor.Handles:Internal_DrawCameraWithGrid (UnityEngine.Camera,UnityEditor.DrawCameraMode,UnityEditor.DrawGridParameters&,bool,bool)
0x0000013311CED90B (Mono JIT Code) UnityEditor.Handles:DrawCameraImpl (UnityEngine.Rect,UnityEngine.Camera,UnityEditor.DrawCameraMode,bool,UnityEditor.DrawGridParameters,bool,bool,bool,UnityEngine.GameObject[])
0x0000013311CECD3B (Mono JIT Code) UnityEditor.Handles:DrawCameraStep1 (UnityEngine.Rect,UnityEngine.Camera,UnityEditor.DrawCameraMode,UnityEditor.DrawGridParameters,bool,bool)
0x0000013311CE7F33 (Mono JIT Code) UnityEditor.SceneView:DoDrawCamera (UnityEngine.Rect,UnityEngine.Rect,bool&)
0x0000013311CC8AC3 (Mono JIT Code) UnityEditor.SceneView:DoOnGUI ()
0x0000013311CC6A73 (Mono JIT Code) UnityEditor.SceneView:OnSceneGUI ()
0x0000013311DC66B9 (Mono JIT Code) UnityEngine.UIElements.IMGUIContainer:DoOnGUI (UnityEngine.Event,UnityEngine.Matrix4x4,UnityEngine.Rect,bool,UnityEngine.Rect,System.Action,bool)
0x0000013310AF2DE3 (Mono JIT Code) UnityEngine.UIElements.IMGUIContainer:HandleIMGUIEvent (UnityEngine.Event,UnityEngine.Matrix4x4,UnityEngine.Rect,System.Action,bool)
0x000001330FA6F133 (Mono JIT Code) UnityEngine.UIElements.IMGUIContainer:DoIMGUIRepaint ()
0x000001330FA6B9B2 (Mono JIT Code) UnityEngine.UIElements.UIR.RenderChainCommand:ExecuteNonDrawMesh (UnityEngine.UIElements.UIR.DrawParams,single,System.Exception&)
0x000001330FA689D3 (Mono JIT Code) UnityEngine.UIElements.UIR.UIRenderDevice:EvaluateChain (UnityEngine.UIElements.UIR.RenderChainCommand,UnityEngine.Material,UnityEngine.Material,UnityEngine.Texture,UnityEngine.Texture,single,System.Exception&)
0x000001330FA63023 (Mono JIT Code) UnityEngine.UIElements.UIR.RenderChain:Render ()
0x000001330FA61F7B (Mono JIT Code) UnityEngine.UIElements.UIRRepaintUpdater:Render ()
0x000001330FA61D88 (Mono JIT Code) UnityEngine.UIElements.BaseVisualElementPanel:Render ()
0x000001330FA61A9B (Mono JIT Code) UnityEngine.UIElements.Panel:Render ()
0x000001330FA61903 (Mono JIT Code) UnityEditor.UIElements.EditorPanel:Render ()
0x00000132E75A0E68 (Mono JIT Code) UnityEngine.UIElements.UIElementsUtility:DoDispatch (UnityEngine.UIElements.BaseVisualElementPanel)
0x00000132E75A0813 (Mono JIT Code) UnityEngine.UIElements.UIElementsUtility:UnityEngine.UIElements.IUIElementsUtility.ProcessEvent (int,intptr,bool&)
0x00000132E75A0234 (Mono JIT Code) UnityEngine.UIElements.UIEventRegistration:ProcessEvent (int,intptr)
0x00000132E759FC4B (Mono JIT Code) UnityEngine.UIElements.UIEventRegistration/<>c:<.cctor>b__1_2 (int,intptr)
0x00000132E759D5DC (Mono JIT Code) UnityEngine.GUIUtility:ProcessEvent (int,intptr,bool&)
0x00000132E759D986 (Mono JIT Code) (wrapper runtime-invoke) <Module>:runtime_invoke_void_int_intptr_intptr& (object,intptr,intptr,intptr)
0x00007FFCA8CD697E (mono-2.0-bdwgc) [C:\build\output\Unity-Technologies\mono\mono\mini\mini-runtime.c:3445] mono_jit_runtime_invoke 
0x00007FFCA8C18444 (mono-2.0-bdwgc) [C:\build\output\Unity-Technologies\mono\mono\metadata\object.c:3068] do_runtime_invoke 
0x00007FFCA8C18530 (mono-2.0-bdwgc) [C:\build\output\Unity-Technologies\mono\mono\metadata\object.c:3115] mono_runtime_invoke 
0x00007FFC879C29B4 (Unity) scripting_method_invoke
0x00007FFC8799A983 (Unity) ScriptingInvocation::Invoke
0x00007FFC87995C45 (Unity) ScriptingInvocation::Invoke<void>
0x00007FFC87AD9F6D (Unity) Scripting::UnityEngine::GUIUtilityProxy::ProcessEvent
0x00007FFC88CC6214 (Unity) GUIView::ProcessRetainedMode
0x00007FFC88D5172A (Unity) GUIView::OnInputEvent
0x00007FFC88CC6143 (Unity) GUIView::ProcessInputEventInternal
0x00007FFC88CC5FD1 (Unity) GUIView::ProcessInputEvent
0x00007FFC88D4D1FB (Unity) GUIView::DoPaint
0x00007FFC88D529BC (Unity) GUIViewPlatform::RepaintViews
0x00007FFC88724CE7 (Unity) PlayerLoopController::InternalUpdateScene
0x00007FFC887238B3 (Unity) PlayerLoopController::EnterPlayMode
0x00007FFC8872552E (Unity) PlayerLoopController::SetIsPlaying
0x00007FFC887208ED (Unity) Application::TickTimer
0x00007FFC88D154EA (Unity) MainMessageLoop
0x00007FFC88D1B1C2 (Unity) UnityMain
0x00007FF73FB42FBA (Unity) __scrt_common_main_seh
0x00007FFD21177374 (KERNEL32) BaseThreadInitThunk
0x00007FFD21DBCC91 (ntdll) RtlUserThreadStart

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

I tried to draw a simple octree where it hold less objects and that works fine, however the frame rate is still bad.

How many nodes are we talking about here? From the stack trace, it looks like you are running out of memory.

The node count when it crashes is 839481

That’s a lot of nodes.
Going by your code, you’d render about 10 million lines. That’s never going to be fast. Even just rendering 20 million triangles (2 triangles per line) is on the high end for most GPUs.

ALINE was not designed for quite this many lines, I’m afraid.
I’m not quite sure why it crashes, but some memory running out seems likely. Just the internal command buffer would probably be a couple of hundred megabytes (just two positions for each line).

I suppose I have not considered the amount of nodes. This octree is used in a rather dense scene to do 3D navigation. Do you have any suggestions on optimizing the debug drawing ? I do not need to view the whole tree at once. Maybe just close to the camera or close to the agents using it.

Sure. You can cut off branches that are far away from agents and/or the camera. And you probably want to have some hard cap on the number of nodes that you render.