Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fixing DearIMGUI NewFrame()/EndFrame() cycle. #2834

Merged
merged 2 commits into from
Feb 21, 2022

Conversation

ohlidalp
Copy link
Member

@ohlidalp ohlidalp commented Dec 15, 2021

I'm getting debug asserts from DearIMGUI that NewFrame()/EndFrame() functions are not being called in sync. It only happens at certain stage of loading certain terrains. Things to know before moving further:

Original concern

Update: root cause found

IM_ASSERT((g.FrameCount == 0 || g.FrameCountEnded == g.FrameCount) && "Forgot to call Render() or EndFrame() at the end of the previous frame?"); in NewFrameSanityChecks(). This happens during loading Aspen Grove terrain. When continued with debugger, the game runs fine. See logs:
RoR-reverted-debuggerAbort.log
RoR-reverted-debuggerContinueGameRuns.log
Note that Train Valley loads without problem.

The reverted commit 627d85b

it fixed the Aspen Grove case, but introduced another in Train Valley terrain: IM_ASSERT(g.FrameScopeActive); // Forgot to call ImGui::NewFrame() in Begin(). Continuing with debugger triggers the FrameCount assert, then FrameScope again, then FrameCount again and then it hangs in Collisions::hashfunc(). See logs.
RoR-upstream-debugerAbort.log
RoR-upstream-debuggerContinue3x.log

Test PC spec:

  • OS Windows 10, Version 10.0.19042 Build 19042
  • Processor AMD Ryzen 5 5600X 6-Core Processor, 3701 Mhz, 6 Core(s), 12 Logical Processor(s)
  • Graphics card nVidia GeForce GTX 660, driver 466.27
  • Microsoft Visual Studio Community 2019 Version 16.11.3
  • using Debug x64, RenderSystem_GL

Tomorrow I'll add debug checks and re-run it to see when it breaks.

@ohlidalp
Copy link
Member Author

ohlidalp commented Dec 16, 2021

I just tried on my work laptop:

  • Windows10 Enterprise, Version 10.0.19042 Build 19042
  • Processor Intel(R) Core(TM) i7-7820HQ CPU @ 2.90GHz, 2901 Mhz, 4 Core(s), 8 Logical Processor(s)
  • Intel(R) HD Graphics 630
  • Microsoft Visual Studio Professional 2019 Version 16.11.2
  • running Debug, x64, OpenGL Rendering Subsystem

Same result with Aspen Grove (without 627d85b):
RoR-laptop-AspenGrove-debuggerContinue.log
RoR-laptop-AspenGrove-debuggerAbort.log

@ohlidalp
Copy link
Member Author

I added debug macros and tried again (on work laptop):
RoR-laptop-NFEV-cmdlineAspen-debuggerIgnore.log
RoR-laptop-NFEF-cmdlineAspen-debuggerAbort.log
This is what I get from the logs:

11:24:11:  - NFEF - calling ImGui::NewFrame()
11:24:11:  - NFEF - calling ImGui::Render()
11:24:11: [RoR|PleaseWaitUI] <44%> Loading terrain pages ...
11:24:11: Terrain created; size=1025 minBatch=33 maxBatch=65 treeDepth=5 lodLevels=6 leafLods=2
11:24:16: Texture 'a89a5UID-aspen.dds': Loading 1 faces(PF_DXT5,1024x1024x1) with 10 custom mipmaps from Image. Internal format is PF_DXT5,1024x1024x1.
11:24:16: Texture 'traildirt.dds': Loading 1 faces(PF_DXT5,512x512x1) with 9 custom mipmaps from Image. Internal format is PF_DXT5,512x512x1.
11:24:16: done loading page: loaded 2 layers
11:24:17:  - NFEF - calling ImGui::NewFrame()
11:24:17:  - NFEF - running OgreImGui::renderQueueStarted(): overlays disabled!
11:24:17: [RoR|PleaseWaitUI] <60%> Initializing Collision Subsystem
11:24:17:  - NFEF - calling ImGui::NewFrame()

But when I set a data breakpoint to Viewport::mShowOverlays, I get no hit - the value seems to be always true. I verified the bool value is 1 byte:

+		&vp->mShowOverlays	0x000001867ba527f9 {true}	bool *
		(int)((uint8_t*)&vp->mShowOverlays)[0]	1	int
+		&vp->mShowSkies	0x000001867ba527fa {true}	bool *

@ohlidalp
Copy link
Member Author

ohlidalp commented Dec 16, 2021

Mystery explained - for some reason, OgreRoot::GetRenderSystem()->_getViewport() gives me a different viewport object that single time. The original one still has overlays on, that's why data breakpoint didn't hit.

RoR-NFEFr2-debuggerAbort.log

@paroj Any idea why

Ogre::Viewport* vp = Ogre::Root::getSingletonPtr()->getRenderSystem()->_getViewport();
would suddenly return a different viewport?

@paroj
Copy link
Contributor

paroj commented Dec 16, 2021

_getViewport() gives you the currently active viewport. Depending on when you ask, this might be the shadowmap.

This is how I attached ImGui code to one specific Viewport in Ogre:
OGRECave/ogre@6ea11c9#diff-ba044a06f13f5520d6a7f71483b63942b77482b9f1c948fac74fc8159eec544a

@ohlidalp ohlidalp force-pushed the imgui-NewFrameEndFrame branch from 404a390 to e28216b Compare February 21, 2022 03:57
@ohlidalp ohlidalp marked this pull request as ready for review February 21, 2022 03:58
@ohlidalp
Copy link
Member Author

@paroj problem found: Checking sceneMgr->_getCurrentRenderStage() == Ogre::SceneManager::IRS_RENDER_TO_TEXTURE)
doesn't do the trick if the RTT is updated by calling Ogre::RenderTarget::update() by hand,
which we do frequently.

I fixed it by adding check against the game's viewport pointer. Your solution OGRECave/ogre@6ea11c9#diff-ba044a06f13f5520d6a7f71483b63942b77482b9f1c948fac74fc8159eec544a is'n applicable here because it requires all GUI to be drawn in RenderTargetListener::preViewportUpdate() callback, and we need it available anytime in the main loop (debug views, scripting, but also convenience).

I moved the branch with debug logging to https://github.com/only-a-ptr/rigs-of-rods/commits/archive/imgui-NewFrameEndFrame-debug and only applied the fix here.

Copy link
Collaborator

@tritonas00 tritonas00 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

6a0120a85dcdae970b0128776ff992970c-pi

@ohlidalp ohlidalp merged commit ad9fb06 into RigsOfRods:master Feb 21, 2022
@ohlidalp ohlidalp deleted the imgui-NewFrameEndFrame branch February 21, 2022 09:55
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants