How to resize DirectX window efficiently?

那年仲夏 提交于 2019-12-05 17:09:33

This is a consequence of how d3d9 was designed. Resizing a d3d9 window requires a call to IDirect3DDevice9::Reset, and from the docs:

Calling IDirect3DDevice9::Reset causes all texture memory surfaces to be lost, managed textures to be flushed from video memory, and all state information to be lost.

So it is what it is. You don't actually customarily change the resolution of the display, at all, ever, from game start to game end. Usually you set the resolution on game start, allow resetting in some special settings screen.

Now I should add, if you are using D3D9, there is a special resource allocation flag you can use (D3DPOOL_MANAGED) that will actually take care of re-loading resources for you. So a local (system memory) copy of all textures, vertex buffers, etc is automatically maintained in system memory, and when the GPU is reset, those textures etc are automatically copied back down to the GPU.

Keep in mind D3DPOOL_MANAGED isn't allowed if you're using the newer IDirect3DDevice9Ex.

In a non-game business application where frequent resizing windows was an important part of the UI, I worked around this issue by creating the D3D device as a memory device at the detected full-screen resolution, then used StretchBlt in the CWnd::OnDraw(...) to resize the memory bitmap as it was being written from the backbuffer to the screen. This isn't a good solution for high frame rate animation, but in a relatively static view situation where the user's actions control the object/view motion it works better than the tear-down and recreate approach.

Create the back buffer to be the monitor's max resolution, and use SWAPEFFECT_COPY.

When drawing, have your code draw to the current window size.

When calling Present, provide the window size for the source and destination rectangles.

Then you have a resizable window that doesn't need to recreate the D3D device when the window size changes.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!