Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resize DirectX window efficiently?

I looked at this site example: http://www.codesampler.com/dx9src/dx9src_6.htm

But the code is rather weird: it frees every texture, vertex buffer, everything! and then builds them up again: loads the model from disk ... i dont think that is very efficient! OpenGL can do window resizing on fly with no such limitations. How can i do the same as OpenGL does: efficient window resize with no need to free every resource in my program?

I tried the code in the above link example, without those invalidateDeviceObjects/restoreDeviceObjects function calls, but it didnt work.. so i guess it must be done? I hope not, or window resizing would be a pain on larger apps where you have hundreds of megabytes of data allocated in textures, models etc.

like image 621
Rookie Avatar asked Jan 06 '12 20:01

Rookie


2 Answers

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.

like image 183
bobobobo Avatar answered Oct 02 '22 13:10

bobobobo


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.

like image 45
Mark Taylor Avatar answered Oct 02 '22 13:10

Mark Taylor