Direct3D9 moving window to another monitor

Problems building or running the engine, queries about how to use features etc.
rpgplayerrobin
Orc Shaman
Posts: 792
Joined: Wed Mar 18, 2009 3:03 am
x 452

Direct3D9 moving window to another monitor

Post by rpgplayerrobin »

Ogre Version: 14.1.2
Operating System: Windows 10-11
Render System: Direct3D9

Hello!

I am having an issue when moving my application window from one monitor to another one.
It completely freezes for a couple of seconds and then it seems to lose some resources and some objects become black.

I have tried this on 3 different computers and the same thing happens with multiple monitors.
I can resize the window without anything strange happening, so it has nothing to do with that.

Also, there is no issue for Direct3D11, only for Direct3D9.
The same bug also happens in the SampleBrowser for Ogre 14.1.2.

To reproduce it for the SampleBrowser, first delete the cache in C:\Users\USERNAME\Documents\OGRE Sample Browser, then start the application, choose Direct3D9 and just start with the default variables. Then go into the Cube Mapping sample, alt-tab (so you can use your mouse) and drag the window to another monitor. As soon as you let go on the other monitor, it lags for a couple of seconds and then the screen in the window goes mostly black.

The Ogre log after I drag the window into the other monitor shows a lot of textures being loaded, then followed by a lot of these lines for many textures (probably as many as were previously loaded):

Code: Select all

23:45:40: D3D9 device: 0x[000000000AD37200] destroy. Releasing D3D9 texture: leaf.png
23:45:40: Released D3D9 texture: leaf.png

I have also tried to use this, but it does not fix anything:

Code: Select all

m_RenderSystem->setConfigOption("Auto hardware buffer management", "Yes");
m_RenderSystem->setConfigOption("Use Multihead", "Auto");
m_RenderSystem->setConfigOption("Resource Creation Policy", "Create on all devices");

Is this a known bug for Direct3D9?

paroj
OGRE Team Member
OGRE Team Member
Posts: 2251
Joined: Sun Mar 30, 2014 2:51 pm
x 1227

Re: Direct3D9 moving window to another monitor

Post by paroj »

on D3D9 each monitor has its own device and all assets must be re-created or migrated on moving the window. The rendersystem options that you have set should mitigate any issues by duplicating everything. However it seems that something is going wrong. It used to work at some point. If you can point me to a commit that broke it, I can take a look.

rpgplayerrobin
Orc Shaman
Posts: 792
Joined: Wed Mar 18, 2009 3:03 am
x 452

Re: Direct3D9 moving window to another monitor

Post by rpgplayerrobin »

I have now downloaded different versions of Ogre and tested their sample browser with this issue (in the "Cube Mapping" sample), from here: https://cloudsmith.io/~ogrecave/repos/ogre/packages/

For my test, I deleted the "Ogre"/"OGRE Sample Browser" directories in the documents folder before each start, and I made sure I used these settings each start:
Rendering Subsystem: Direct3D9 Rendering System
Multi device memory hint: Auto hardware buffers management / Auto hardware buffer management: Yes
Resource Creation Policy: Create on all devices
Use Multihead: Auto
Full Screen: No (of course, otherwise it is impossible to drag the window)

Here are the results:
ogre-sdk-1.10.12-vc15-x64: Crashes
ogre-sdk-1.11.0-vc15-x64: Crashes
ogre-sdk-1.11.5-vc15-x64: Works!
ogre-sdk-1.12.0-vc15-x64: Works!
ogre-sdk-1.12.5-vc15-x64: Works!
ogre-sdk-v1.12.6-vc15-x64: Works!
ogre-sdk-v1.12.7-vc15-x64: Works!
ogre-sdk-v1.12.8-vc15-x64: Works!
ogre-sdk-v1.12.9-vc15-x64: Black screen
ogre-sdk-v1.12.10-vc15-x64: Black screen
ogre-sdk-v1.12.13-vc15-x64: Black screen
ogre-sdk-v13.0.0-msvc141-x64: Black screen
ogre-sdk-v13.4.0-msvc141-x64: Black screen
ogre-sdk-v14.0.0-msvc141-x64: Black screen
ogre-sdk-v14.2.4-msvc142-x64: Black screen

As you can see, something happened between 1.12.8 and 1.12.9.
One thing I noticed there is that their dialogue on startup changed its appearance, so maybe it has something to do with that? Not sure.

From the notes, it says "drop D3DPOOL_MANAGED for buffers & discard HBU_DETAIL_DISCARDABLE", which may have something to do with this issue? (https://github.com/OGRECave/ogre/releases/tag/v1.12.9)

I used the compare feature on github to the 1.12.8 version (https://github.com/OGRECave/ogre/compar ... ...v1.12.9) and then clicked on "Files changed".
The biggest thing I can see is that "#define OGRE_D3D_MANAGE_BUFFERS 1" was removed in "RenderSystems/Direct3D9/include/OgreD3D9Prerequisites.h", which makes a big difference later in "RenderSystems/Direct3D9/src/OgreD3D9HardwareBufferManager.cpp".
There are also other bigger changes in that link for:
RenderSystems/Direct3D9/src/OgreD3D9HardwareIndexBuffer.cpp (D3DPOOL_MANAGED is no longer used, which may be an issue?)
RenderSystems/Direct3D9/src/OgreD3D9HardwareVertexBuffer.cpp (D3DPOOL_MANAGED is no longer used, which may be an issue?)
RenderSystems/Direct3D9/src/OgreD3D9Mappings.cpp
RenderSystems/Direct3D9/src/OgreD3D9RenderSystem.cpp

Also, this commit in that period is interesting, since it mentions a multi-monitor rework (but I don't know what commit the multi-monitor rework was in):
https://github.com/OGRECave/ogre/commit ... 5a976995a3

I would guess those file changes is the reason it no longer works, but I have no idea how to actually fix this.

paroj
OGRE Team Member
OGRE Team Member
Posts: 2251
Joined: Sun Mar 30, 2014 2:51 pm
x 1227

Re: Direct3D9 moving window to another monitor

Post by paroj »

I tried to reproduce your findings and noticed that when I start the SampleBrowser without window grab to ease switching monitors, the issue goes away:

Code: Select all

SampleBrowser.exe nograb

can you confirm?

rpgplayerrobin
Orc Shaman
Posts: 792
Joined: Wed Mar 18, 2009 3:03 am
x 452

Re: Direct3D9 moving window to another monitor

Post by rpgplayerrobin »

It does not seem to work for me.

I tried it for these:
ogre-sdk-v1.12.9-vc15-x64
ogre-sdk-v14.2.4-msvc142-x64
ogre-sdk-master-msvc142-x64 (Which was added today to the download list)

I also tried it on my local specific source code version by forcing nograb to be true in the code, but it did not have any impact.

Of course I also removed the Ogre files from documents before each try.

paroj
OGRE Team Member
OGRE Team Member
Posts: 2251
Joined: Sun Mar 30, 2014 2:51 pm
x 1227

Re: Direct3D9 moving window to another monitor

Post by paroj »

rpgplayerrobin wrote: Sat May 04, 2024 10:51 pm

Of course I also removed the Ogre files from documents before each try.

I did some more testing. The issue seems only to appear when the config dialog is shown i.e. on the first run. On subsequent runs everything works.

rpgplayerrobin
Orc Shaman
Posts: 792
Joined: Wed Mar 18, 2009 3:03 am
x 452

Re: Direct3D9 moving window to another monitor

Post by rpgplayerrobin »

You are right, even without nograb it works on the second try (but not on the first try), I tried it for these:
ogre-sdk-v1.12.9-vc15-x64
ogre-sdk-v14.2.4-msvc142-x64
ogre-sdk-master-msvc142-x64

I had the source for "ogre-14.1.2" so I also attempted it there, and I got the same result.

Debugging that version shows that the difference is in Root::restoreConfig, the second try it goes into rs->setConfigOption for all options.
On the first try, mAutoHardwareBufferManagement is false, but on the second try it is true.
If I alter "ConfigDialog::PrivateData::DlgProc" to set the config option the first try (before .setRenderSystem), it works:
dlg->mImpl->mSelectedRenderSystem->setConfigOption("Auto hardware buffer management", "Yes");

However, that for some reason does not work for all Ogre versions, since my own source version that I use for my game does not work with that fix in the Sample Browser. There it does not even work on the second try...

rpgplayerrobin
Orc Shaman
Posts: 792
Joined: Wed Mar 18, 2009 3:03 am
x 452

Re: Direct3D9 moving window to another monitor

Post by rpgplayerrobin »

For my own built source code I found the issue as well, it has to do with D3D9HardwareBufferManager::createVertexBuffer and D3D9HardwareBufferManager::createIndexBuffer, since my source still uses OGRE_D3D_MANAGE_BUFFERS because of performance issues displayed here:
viewtopic.php?p=552885#p552885
viewtopic.php?p=552890#p552890

I downloaded the source code for 1.12.8 and checked how that version (when OGRE_D3D_MANAGE_BUFFERS was still a thing) handled monitor switching, and it seemed I missed one thing.

I fixed it in my local source code version by changing a couple of lines in D3D9HardwareBufferManager::createVertexBuffer and D3D9HardwareBufferManager::createIndexBuffer:
From this:

Code: Select all

auto impl = new D3D9HardwareBuffer(D3DFMT_VERTEXDATA, vertexSize * numVerts, usage, tmpUseShadowBuffer);

To this:

Code: Select all

bool tmpUseShadowBuffer = useShadowBuffer ||
	// Allocate the system memory buffer for restoring after device lost.
	(((usage & HardwareBuffer::HBU_WRITE_ONLY) != 0) && mAutoHardwareBufferManagement);

auto impl = new D3D9HardwareBuffer(D3DFMT_VERTEXDATA, vertexSize * numVerts, usage, /*useShadowBuffer*/ tmpUseShadowBuffer);

Of course, this has nothing to do with the latest version of Ogre, but it seems to have solved the issue for my own version.

You should probably fix it in the SampleBrowser by forcing the variables (like "Auto hardware buffer management") to also be used when using the dialog by using setConfigOption, and not just when loading the values from the cfg file, otherwise mAutoHardwareBufferManagement will stay as false on the first try.

paroj
OGRE Team Member
OGRE Team Member
Posts: 2251
Joined: Sun Mar 30, 2014 2:51 pm
x 1227

Re: Direct3D9 moving window to another monitor

Post by paroj »

rpgplayerrobin wrote: Sun May 05, 2024 6:34 pm

If I alter "ConfigDialog::PrivateData::DlgProc" to set the config option the first try (before .setRenderSystem), it works:
dlg->mImpl->mSelectedRenderSystem->setConfigOption("Auto hardware buffer management", "Yes");

the correct fix is actually much simpler:
https://github.com/OGRECave/ogre/pull/3097

paroj
OGRE Team Member
OGRE Team Member
Posts: 2251
Joined: Sun Mar 30, 2014 2:51 pm
x 1227

Re: Direct3D9 moving window to another monitor

Post by paroj »

rpgplayerrobin wrote: Fri May 03, 2024 3:22 pm

Also, this commit in that period is interesting, since it mentions a multi-monitor rework (but I don't know what commit the multi-monitor rework was in):
https://github.com/OGRECave/ogre/commit ... 5a976995a3

for reference, this commit was referring to this: https://github.com/OGRECave/ogre/commit ... 93cad3b196