c++windowsvisual-c++directx

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.


Solution

  • 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.