Sign in to follow this  

[DX9] About lost devices...

This topic is 3574 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I've added some code to catch when the device is lost. An it is working. The problem is that I'm loosing my texture( I only have one in the scene, drawn to the screen using a sprite ). Now, as I understand it this is normal when using D3DPOOL_DEFAULT. However, I am using D3DPOOL_MANAGED. I'm loading the texture using the following line:
if(SUCCEEDED(Err = D3DXCreateTextureFromFile(D3DDev, "..\\media\\non\\back.dds", &BackTex)))

The DirectX documentation states that D3DXCreateTextureFromFile equates to:
Quote:
The function is equivalent to D3DXCreateTextureFromFileEx(pDevice, pSrcFile, D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, ppTexture).
Which is using the managed pool. I have tested the reset by dropping out of fullscreen, I even have it dumping the error messages to the console so I can see what it is doing. The device then gets successfully reset but the texture no longer gets rendered - unless I recreate it. I'm sure I'm being retarded somewhere, but what is it that i'm doing wrong? Or is this normal behaviour and I've misunderstood D3DPOOL_MANAGED? Thanks.

Share this post


Link to post
Share on other sites
D3DPOOL_MANAGED resources aren't lost when the device is lost and reset. However, the device state is - so any textures that were set via SetTexture() will need reset. Similarly for everything else like transforms, render states, stream sources, index buffers, shaders, etc.

Are you using the Debug runtimes? Do they report anything out of the ordinary?

Share this post


Link to post
Share on other sites
Thanks for the quick reply.

I'm not using SetTexture anywhere. I added the render state's to the reset function but the same issue occurs.

I forgot to add the debug output in my original post, my apologies. But here it is.

If I run the application and then quit it terms cleanly and I loose no memory. I do get a couple warnings about redundant SetRenderStates - but I can google those later. However, if I force a device reset, recreate the texture, I loose 960 bytes of memory. But, if I force a device reset and do not recreate the texture I get a lot of:

Quote:

First-chance exception at 0x76cdb09e in blank.exe: Microsoft C++ exception: long at memory location 0x001af990..
Direct3D9: (ERROR) :State block release failed.

Direct3D9: (ERROR) :State block handle is greater than available number of blocks


I don't know if this information is important but here is the loading, loop and reset( It's a little ugly I'm afraid, I'm a Delphi programmer natively ). You'll notice I'm also using a font. The documentation doesn't state which pool this uses, if any?. For the purposes of this I'm just focusing on the texture. Once that is working I can delve in to the font if it doesn't use managed.


void CreateDebugDisplay()
{
std::cout << " " << std::endl;
std::cout << "FPS Display Enabled." << std::endl;
HRESULT Err;

if(SUCCEEDED(Err = D3DXCreateFont(D3DDev, 18, 0, FW_BOLD, 1, false, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_SWISS, "Consolas", &Font)))
{

if(SUCCEEDED(Err = D3DXCreateSprite(D3DDev, &BackSprite)))
{
if(SUCCEEDED(Err = D3DXCreateTextureFromFile(D3DDev, "..\\media\\non\\back.dds", &BackTex)))
{
std::cout << " " << std::endl;
std::cout << "FPS Display Loaded." << std::endl;
}
else
{
std::cout << " " << std::endl;
std::cout << "FPS Texture Failure." << std::endl;
std::cout << DXGetErrorString9(Err) << std::endl;
std::cout << "Error ( " << DXGetErrorDescription9(Err) << " )" << std::endl;
std::cout << "This error is non-terminal. FPS Display Disabled." << std::endl;
std::cout << " " << std::endl;
DoDisplay = false;
}
}
else
{
std::cout << " " << std::endl;
std::cout << "FPS Display Sprite Failure." << std::endl;
std::cout << DXGetErrorString9(Err) << std::endl;
std::cout << "Error ( " << DXGetErrorDescription9(Err) << " )" << std::endl;
std::cout << "This error is non-terminal. FPS Display Disabled." << std::endl;
std::cout << " " << std::endl;
DoDisplay = false;
}
}
else
{
std::cout << " " << std::endl;
std::cout << "FPS Display Font Failure." << std::endl;
std::cout << DXGetErrorString9(Err) << std::endl;
std::cout << "Error ( " << DXGetErrorDescription9(Err) << " )" << std::endl;
std::cout << "This error is non-terminal. FPS Display Disabled." << std::endl;
std::cout << " " << std::endl;
DoDisplay = false;
}

return;
}




void Render()
{
HRESULT Err;
Err = pixie->GetDevice()->TestCooperativeLevel();

if(Err == D3D_OK)
{
pixie->Open();
//Do map here.
pixie->Close();
Update();
}
else
{
std::cout << "Device Lost. Waiting for regain." << std::endl;
if(Err == D3DERR_DEVICENOTRESET) pixie->ResetDevice(true);
}
}




void ResetDevice(bool ForExclusive)
{
ZeroMemory(&D3DPp, sizeof(D3DPp));
D3DPp.Windowed = !ForExclusive;
D3DPp.SwapEffect = D3DSWAPEFFECT_DISCARD;
D3DPp.BackBufferFormat = Format;
D3DPp.hDeviceWindow = Handle;
D3DPp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;

if(!ForExclusive)
{
SetWindowPos(Handle, 0, (sWidth / 2) - (wWidth / 2), (sHeight / 2) - (wHeight / 2), wWidth, wHeight, NULL);

D3DPp.BackBufferWidth = wWidth;
D3DPp.BackBufferHeight = wHeight;
sWidth = wWidth;
sHeight = wHeight;
}
else
{
D3DPp.BackBufferWidth = fWidth;
D3DPp.BackBufferHeight = fHeight;
sWidth = fWidth;
sHeight = fHeight;
}

D3DPp.EnableAutoDepthStencil = TRUE;
D3DPp.AutoDepthStencilFormat = D3DFMT_D16;

HRESULT Err;

if(SUCCEEDED(Err = D3DDev->Reset(&D3DPp)))
{
D3DDev->SetRenderState(D3DRS_LIGHTING, true);
D3DDev->SetRenderState(D3DRS_ZENABLE, true);
D3DDev->SetRenderState(D3DRS_AMBIENT, D3DCOLOR_XRGB(50, 50, 50));
std::cout << " " << std::endl;
std::cout << "Device Regained." << std::endl;
std::cout << " " << std::endl;
//CreateDebugDisplay();
}
else
{
std::cout << " " << std::endl;
std::cout << "Attempted Device Regain Failed." << std::endl;
std::cout << " " << std::endl;
std::cout << DXGetErrorString9(Err) << std::endl;
std::cout << "Error ( " << DXGetErrorDescription9(Err) << " )" << std::endl;
std::cout << " " << std::endl;
}

return;
}



The render code is just:


void RenderDebugDisplay()
{
static RECT box;

if(!CapFPS)
{
std::ostringstream FpsBuffer;
FpsBuffer << (1 / FrameDelta);
Fps = FpsBuffer.str()+"fps";
}

D3DXVECTOR3 Centre(0.0f, 0.0f, 0.0f);
D3DXVECTOR3 Pos(50.0f, 50.0f, 0.0f);
D3DXVECTOR3 PosB(0.0f, 0.0f, 0.0f);
SetRect(&box, 0, sHeight - 20, sWidth, sHeight);

BackSprite->Begin(NULL);
BackSprite->Draw(BackTex, &box, &Centre, &PosB, D3DCOLOR_ARGB(255, 255, 83, 0));
BackSprite->End();

SetRect(&box, 10, 2, sWidth, sHeight);
Font->DrawTextA(NULL, Fps.c_str(), strlen(Fps.c_str()), &box, DT_LEFT | DT_TOP, D3DCOLOR_ARGB(255, 225, 225, 225));

SetRect(&box, 0, 0, sWidth, 20);
Font->DrawTextA(NULL, "Test Project", strlen("Test Project"), &box, DT_VCENTER | DT_CENTER, D3DCOLOR_ARGB(255, 225, 225, 225));

if(!CapFPS)
{
std::ostringstream TBuffer;
TBuffer << PT->GetDeltaMillis();
Tick = TBuffer.str()+"tick";
}

SetRect(&box, 0, 0, sWidth - 10, 20);
Font->DrawTextA(NULL, Tick.c_str(), strlen(Tick.c_str()), &box, DT_RIGHT | DT_TOP, D3DCOLOR_ARGB(255, 225, 225, 225));

return;
}



Sorry for all the source, but at the moment I'm not 100% sure what is relevant to the question and what isn't.

Thanks.

Share this post


Link to post
Share on other sites
Ok, I was being special. I had a complete mental block to the Sprite object. If I recreate that then it works.

Thanks again for the advice EvilSteve, I appreciate it. :)

Share this post


Link to post
Share on other sites

This topic is 3574 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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