• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Norman Barrows

DX9 Lost Device Example w/code and explanation

1 post in this topic

DX9 Lost Device Example
 
I decided to implement Alt-tab in my project, and went looking for info on handling
lost devices. The DirectX docs were their usual disappointment. Eventually a serach
of Gamedev and the Web found all the bits an pieces needed, but not in one place.
So I decided to post what I'd found for use by others.
 
First, you'll want to make sure you load all non-dynamic resources into managed memory pool,
not default. This essentially keep a backup copy of resources in regular ram that D3D can use 
to automtically restore vidram as needed. 
 
 
 
Here's the basic algo:
 
result=Present();
if (result == D3DERR_DEVICELOST) do_lost_device();
 
 
 
do_lost_device:
while (    TestCooperativeLevel()   !=    D3DERR_DEVICENOTRESET    )   
    {
    do_win_messages();
    Sleep(10);
    }
release_stuff();
d3d_device_ptr->Reset(&params);
while   (   TestCooperativeLevel()   !=    D3D_OK   )      
    {
    do_win_messages();
    Sleep(10);
    }
reload_stuff();
reset_stuff();
 
 
release_stuff:
call OnLostDevice for all your interfaces (fonts, sprite object, etc). You don't
have to release them. Release all default memory pool resources: dynamic textures,
dynamic bitmaps, index and vertex buffers you allocate, etc. Note that 
LoadMeshFromX with no flags defaults to D3DPOOL_DEFAULT, and Mesh.GetVertexBuffer
returns a copy of the mesh's vertex buffer object, not its address. Both of these
bit me when trying to find all the places I created something in D3DPOOL_DEFAULT. 
 
 
reload_stuff:
Call OnResetDevice for your interfaces (fonts, sprite object, etc).
Reload / recreate your D3DPOOL_DEFAULT resources: dynamic textures, dynamic
bitmaps, index and vertex buffers you allocate, etc. This is basically a copy 
and paste of the code where you do this the first time.
 
 
 
 
reset_stuff:
Pretty much all of D3D's internal states get wiped on lost device, so you have to
set everything back to the way you want it again. This will vary by title. 
I reset everyting that I set between the time I create the D3D device and the 
time I start the game itself:
set default material
set flexible vertex format
set projection matrix
set default texture stage states
set gourard shading
turn on mipmaps
set min mag filetrs to anisotropic
set ambient light level
turn on normalizing normals (I do scaling and lighting)
turn on specular
turn on alpha blending
 
 
 
 
I saw one posting that tested co-op level before calling present, but that would
add an unnecessary check when things are running correctly, i would think.
 
The basic idea is you wait for "device not reset". You have to process windows 
messages while you do so. It has to do with D3D knowing when you get focus back 
apparently. The call to Sleep() releases clock cycles to the OS while you wait.
When you get "Device not reset", you release stuff from vidram. Then you call
Reset(). 
 
IF YOU STILL HAVE STUFF IN VIDRAM, Reset() WILL FAIL WITH "Invalid Call"!
 
If Reset() succeeds, you then have to wait for DirectX to come back alive. So you
test cooperative level until you get "D3D_OK". During this time, apparenly windows
will switch the resolution to match your desired resolution, thus the need to wait
a moment. Again, windows messages must be processed while you wait, and you want 
to Sleep() to give clock cycles back to the OS. Once you get "D3D_OK", you own 
the video card again, and you're pretty much back where you were when you first
created your D3D device, except your managed stuff is still loaded. Call OnReset()
for your interfaces (font, sprite, etc), reload or re-create your non-managed
resources, reset D3D back to the way you want it, and your done!  :)
 
Ok, so you do all this, and it doesn't work, or it works sometimes, but not at
other times. Then what? Enter the dreaded "Purple Screen"! purplescreen(); is 
a simple tesing routine that you can insert into your code to determine where 
you are creating things in vidram that you're not releasing. In semi-pseudocode:
 
void purplescreen()
{
int a;
a=0;
do
    {
    beginscene
    clear        // clear screen to purple   100,0,50
    endscene
    result=present
    if result == D3DERR_DEVICELOST do_lost_device()
    do_win_messages();
    if (keypressed(VK_ESCAPE)) a=1;
    Sleep(10);
    }
    while (a != 1);
}
 
 
While the purple screen is displayed, you can alt-tab to and from your game, then
hit Esc to continue. Use it to "bracket" where you're using vidram you need to
relase. If you can alt-tab from the purple screen and back, your ok. If you get 
"invalid call" when you Reset(), you've loaded something you haven't released.
So you put the purple screen at program start, right after you load everything.
well of course it works there, you used that code to decide what to release!
So add another purple screen later on, like right after you draw your scene and
present it. OK, doesn't work there. Now you know D3D is allocating vidram
somewhere between those two points. Move around your calls to purplescreen
until you find where. Once you find the resource, add it to release_stuff()
and reload_stuff(). Test it again. Eventually you'll find them all, and all of
a sudden it will work!
 
The info in this post came from about 4 different sources and took me about two 
days to find and put together into working code. It took me another two days 
to find, release, and reload/recreate everything in my project. In my case it was
finding index and vertex buffers used to draw procedurally height mapped ground
quads.
 
Hopefully this post will save someone some time in the future.
1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0