Direct3d device lost)

Source: Internet
Author: User

1. When will the device be lost?


A direct3d device has two statuses: operation or loss.

Operation Status: Indicates the normal status of the device. The device runs as expected and can present all rendering effects.

Loss status: All rendering operations quietly fail. idirect3ddevice9: Present returns the error code d3derr_devicelost.


Check and help lost devices (direct3d 9) For more details. Let's briefly explain in the most migrant languages: When will the device be lost?

(1) When the current direct3d window loses focus in full screen mode

(2) There are multiple direct3d windows. If one of them enters the full screen mode, other devices will be lost. (when multiple video cards are used, the situation will be different. Each video card has only one device capable of full screen)

System sleep or locked (WIN + l)

(3) idirect3ddevice9: When the Reset Call fails, the device is also set to lost.

(4) When you reset the direct3d device parameter



2. How to Detect Device loss



Idirect3ddevice9: Present returns the error code d3derr_devicelost or d3derr_driverinternalerror (in this case, exit is required)

Idirect3ddevice9: testcooperativelevel returned error code d3derr_devicelost or d3derr_driverinternalerror (dx10 integrated into the present)


No matter the device is lost in any way, almost all operations will fail, and only release () can be used-in fact, d3d will ensure that some operations can be successful,

Yes, it is only "acceptable" to succeed, not "certain" to succeed. Therefore, it is better for you to think that all failures will fail when the loss is lost -- and

Idirect3ddevice9: testcooperativelevel. Therefore, after the device is lost, you should stop the entire game loop and call

Idirect3ddevice9: testcooperativelevel determines whether the device is available.

"Idirect3ddevice9: testcooperativelevel 』

This method detects the current device status. There are four types of return values: d3d_ OK: everything is normal, d3derr_devicelost is lost, d3derr_devicenotreset is

Reset. In addition, there is also the d3derr_driverinternalerror added by d3d9. When this happens, you will be finished, and it is basically impossible to recover. Stop the program.

In order, if the game is running properly, d3d_ OK will return; if a device is lost and cannot be recovered at this time, such as in full screen mode

After switching to the Windows desktop, the user will return d3derr_devicelost. If the user switches back to the game, the device can be restored (not recovered yet! Only "Yes

Returns d3derr_devicenotreset.

In addition, idirect3ddevice9: Present will return a similar value, but you 'd better not count on this, honestly using testcooperativelevel. Because

The present still returns d3derr_devicelost when the device can be restored (the testcooperativelevel will be fully integrated into the present when the device is d3d10 ).

In it, congratulations, but congratulations)


3. After the device is lost


1. Check the device status to see if it can be restored to the operation status. If not, wait until the device is restored.


2. Release all resources allocated in d3dpool_default, including using idirect3ddevice9: createrendertarget and

Idirect3ddevice9: createdepthstencilsurface method to create resources. (If you do not perform this step, the next reset operation will fail)


3. Call idirect3ddevice: reset to restore the device. The reset method is the only valid method when the device is lost, and the application can use it to remove the device from the lost status.

The only way to restore to the operation status.


4. Rebuild the resources released in step 2, and reset the state.


4. manage resources


1. resource management is a process of raising resources from the system memory to the memory accessible to devices and discarding the memory accessible to devices.


2. d3dpool_managed indicates a resource managed by the system. Resources managed by the system exist continuously in the switching between the lost and operational statuses of devices. Call

The idirect3ddevice9: reset device can be reset, and such resources can continue to work normally without reloading images. However, if the device must be destroyed and

Then, all resources created with d3dpool_managed must be rebuilt.


3. The d3dpool_default flag specifies that resources are placed in the default pool. Resources in the default pool do not exist continuously during the switch from the lost status to the operation status,

These resources must be released before calling reset and then rebuilt


4. Not all types and uses support resource management. For example, the object created with the d3dusage_rendertarget flag does not support resource management. In addition, we do not recommend that you

Objects that frequently change their content use resource management. For example, automatic management of a vertex cache that needs to be changed per frame on some hardware will seriously reduce performance. However,

This is not a problem for texture Resources


5. Handle device loss


The following pseudo code:

Switch (idirect3ddevice9: testcooperativelevel ()){
Case d3d_ OK:
Gameloop ();
Case d3derr_devicelost:
Case d3derr_devicenotreset
Onlostdevice ();
Idirect3ddevice9: reset ();
Onresetdevice ();
Quitgame ();

Gameloop () is the process of running your game. Write the switch in the gamemain () part of our game framework. The specific location can be seen

Source code.

It seems that I have never talked about the idirect3ddevice9: reset parameter? Because there is only one parameter, It is the pointer to d3dpresent_params. Take your

The d3dpresent_params structure used to create a device is saved for reset.

Onlostdevice () is to release all d3dpool_default resources, and onresetdevice () is to create * () to restore! You may notice that

Id3dxfont and id3dxsprite all have methods of the same name, which are called at this time. If you do not do this, that is, keep any

For d3dpool_default resources, idirect3ddevice9: reset will certainly fail.

In addition, in onresetdevice, you need to re-set setrenderstate, setsamplerstate, and so on. After reset, these items are also lost. Actually

Reset is similar to re-creating a device. If you re-create a device, you need to release the resources of d3dpool_managed. This topic is not


It can be seen from the code that the program did not do anything when d3derr_devicelost was just dumb. I think this is a good habit, because it cannot be guaranteed

When d3derr_devicelost is used, in addition to release, it is better to wait for the device to use it.

If you are too lazy to manage resources, all d3dpool_managed is better. As for rendering objects? Find a solution.


6. Manual Manufacturing "device loss


"Why do we need to make devices lost ?" If you want to change the game resolution, color depth, and switch the full screen and window status, reset the operation.

Before reset, all d3dpool_default resources must be released. (In fact, more resources must be released, but such resources will not be created in 2D.

And call methods such as id3dxsprite: onlostdevice. This is the "device loss" of manual manufacturing. In this process, the device

There is no real loss, but it will be unavailable for a period of time. At this time, the reset has not been returned, and the entire d3d device is like dead. For example, you can switch the table

Surface resolution, there will be a period of time on the display nothing, and then soon it will be normal. This is the same reason. After successful reset, remember to restore the resource.

You may notice that the reset here is not the same as the reset above. This is indeed the case. This is to reset the status rather than recover the device. Therefore, change the resolution and color.

A deep reset needs to be written outside the switch, that is, do not confuse it with the meaning-_-BB. And you only need onlostdevice-> Reset-> onresetdevice. Note

Hold: The correct call to reset will not cause device loss. Do not confuse this concept.



(Switch) direct3d device loss (device lost)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.