DirectX : 3d Hardware Reset Failure

Posted By: jumpman

DirectX : 3d Hardware Reset Failure - 11/27/20 02:22

Hello friends

When I run my game, and a screensaver takes over, there is a chance my game will crash with a "3d Hardware Reset Failure" error when I wake up the computer. Not every single time, but eventually it will crash with this error, whether I wake up the computer from a screen saver, or lock/unlock the computer. If I run the game fullscreen, the screensaver will not run, however if you lock your computer and let the screensaver take over, log back in, the game will be frozen.

I think it has to do with a render chain, or any views that have a stage. When I turn off view stages, the crash doesnt happen. However, I lose all my fancy effects/shadows frown

Does anyone have any experience with a render chain and this error?
Posted By: Dooley

Re: DirectX : 3d Hardware Reset Failure - 11/27/20 03:44

I've seen it happen with my games too. Not sure about how to avoid that. I'm wondering if you could switch off view stages if the game window loses focus, or perhaps there is a windows.h command that would let you prevent a screensaver from running.
Posted By: jumpman

Re: DirectX : 3d Hardware Reset Failure - 11/27/20 05:23

Hmm thats an interesting idea, I can look inside the windows.H to see if I can find anything, thank you Dooley!

Currently "on_d3d_lost " and "on_d3d_reset" dont fire off before the "3d hardware reset failure" error, so I cant find a way to intercept that error by switching the stages off before the wakeup during a screensaver. I was testing some other people's simple level files with a simple post processing chain, and the error happened even there when waking up from a screensaver.

Acknex/DirectX is having trouble recreating textures linked in a stage, whether its a TargetMap or an explicitly created bmap attached to a view frown

Please let me know if anyone has a workaround for this, outside of not using a render chain altogether.
Posted By: jumpman

Re: DirectX : 3d Hardware Reset Failure - 11/27/20 07:29

I think I found something inside windows.H Dooley! Will test and report my findings asap!
Posted By: Emre

Re: DirectX : 3d Hardware Reset Failure - 11/27/20 11:06

Hello jumpman, there is a workaround for this problem: https://opserver.de/ubb7/ubbthreads.php?ubb=showflat&Number=478106#Post478106
Posted By: jumpman

Re: DirectX : 3d Hardware Reset Failure - 11/28/20 01:58

Hi Emre, thanks for linking your solution. Here is what I've done so far, with advice and code snippets from both of you.

It currently isnt working 100%, but here is the major parts of the code that I believe help a lot, I think it completely got rid of the 3d Hardware Reset failure crash when waking up from a screensaver:

Code
#include <windows.h>  //----------include windows.h

...

function main()
{

var render_chain_destroy=0;
var render_chain_state=0;

while(1) ///--------------------put this in your main loop
{

BOOL bSaver;  //--------------------------------creates a BOOL flag that will be changed by the windows.h function SystemParametersInfo
SystemParametersInfo(114,0,&bSaver,0);  //---------114 is a specific parameter that tells gamestudio whether or not a screensaver is running

...


        //---------If bSaver is 1/true then it means the windows screensaver is running:

	if((bSaver)||(window_focus==0))  //-----------bSaver is a flag set by SystemParametersInfo above. I put window_focus in here as well 
	{

   render_chain_destroy=1;  //-------get ready to destroy the render chain

        }

       else
      {

      render_chain_destroy=0;  //-------if the screen saver isnt running, dont destroy the render chain

      }



if(render_chain_destroy==1)  //-----we are ready to destroy the render chain because the screensaver is on
{

if(render_chain_state==0)
{

destroy_render_chain();  //-------put your render chain destruction here (remove stages, remove bmaps, purge bmaps
render_chain_state=1;  //-----destroy the chain once
}


}


if(render_chain_destroy==0)  //-------if the system doesnt have a screensaver running
{

if(render_chain_state==1) //------and we recently destroyed the render chain because we destroyed it when a screensaver came on before
{
recreate_render_chain(); //--------recreate your render chain here
wait(-2); //------------I waited 2 seconds here, not sure if this helps or not
render_chain_state=0;  //-------we reset this var to 0, which is only set to 1 when a screensaver is on.

}


}


There some caveats to the above screensaver code. It only consistently works if gamestudio is running in a window. Obviously, if gamestudio is full screen, the screensaver will never trigger.

But lets work on the next hurdle. Locking your computer.

...
if (DeskHwnd != 0)
{
DesktopResult = SwitchDesktop(DeskHwnd);
}


if((DesktopResult==0)||(key_o))
{
...

With Emre's code, I can get gamestudio to recognize when the computer is locked! I destroy the render chain and remove the stages the moment gamestudio detects a lock.

However, when you log back in to a running windowed gamestudio exe, the game gave me a "cant create texture unnamed", but it didnt crash the game. When you select OK, the game seems to run ok in windowed mode after.

If you run the game in fullscreen, then lock, destroy the render chain in gamestudio, then log back in, the game looks like its frozen, however if you press alt+enter, the game seems to unfreeze and start working again.

Emre, have you tried to see if your game works when you run fullscreen, then lock, then log back into the desktop to see what a running gamestudio does?

Thank you two for your help so far.
Posted By: Emre

Re: DirectX : 3d Hardware Reset Failure - 11/28/20 07:20

Thank you for sharing code.

Originally Posted by jumpman

Emre, have you tried to see if your game works when you run fullscreen, then lock, then log back into the desktop to see what a running gamestudio does?


I've never tried fullscreen test before. You're right, it freezes when in fullscreen indeed.

Originally Posted by jumpman

However, when you log back in to a running windowed gamestudio exe, the game gave me a "cant create texture unnamed", but it didnt crash the game. When you select OK, the game seems to run ok in windowed mode after.

I surprised by this. Although i run four pp effects at the same time, i don't get any error. I don't know what caused this problem.

EDIT: Btw i found another solution and this is effective in any case in window mode. (Screensaver, UAC, Lock secreen, etc...)

Code

#include <acknex.h>
#include <windows.h>
#include <default.c>

#ifndef material_and_view
	#define material_and_view
	MATERIAL* test_mat1 = 
	{
		effect="Texture TargetMap;
		sampler2D g_samSrcColor = sampler_state { texture = <TargetMap>; MipFilter = Linear;	};
		float4 postprocessing_negative( float2 Tex : TEXCOORD0 ) : COLOR0 
		{
			float3 Color = 1. - tex2D( g_samSrcColor, Tex.xy).xyz;
			return float4(Color,1.);
		}
		technique PostProcess 
		{
			pass p1 
			{
				AlphaBlendEnable = false;
				VertexShader = null;
				PixelShader = compile ps_2_0 postprocessing_negative();
			}
		}";
	}

	VIEW* post_view1 ={layer = 0; flags = PROCESS_TARGET; material=test_mat1;}
#endif



action testet()
{
	while(1)
	{
		my.pan+=5*time_step;
		wait(1);
	}
}
function main()
{
	video_mode=10;
	video_screen=2;

	wait(3);
	fps_max=500;
	
	level_load("");
	camera.stage = post_view1;  
	ent_create(CUBE_MDL,vector(150,0,0),testet);
	HWND	foreground = GetForegroundWindow();
	while(1)
	{
		foreground = GetForegroundWindow();

		if(foreground!=hWnd)
		{
			beep();
			camera.stage=NULL;
			while(1)
			{
				foreground = GetForegroundWindow();
				if(foreground==hWnd)
				{
					break;
				}
				wait(1);
			}
			camera.stage=post_view1;
			beep();
		}
		
		wait(1);
	}
}



EDIT 2: For fullscreen, you can use video_set after the background, instead of alt + enter. i don't know how but it restore something.

Code

foreground = GetForegroundWindow();

		if(foreground!=hWnd)
		{
			beep();
			camera.stage=NULL;
			while(1)
			{
				foreground = GetForegroundWindow();
				if(foreground==hWnd)
				{
					break;
				}
				wait(1);
			}
			camera.stage=post_view1;
			video_set(0,0,0,2);
			beep();
		}
		


Posted By: jumpman

Re: DirectX : 3d Hardware Reset Failure - 11/28/20 18:28

I noticed the same thing too with video_set! Emre, to me this is very elegant and simpler code, thank you.

It currently works without hitch/error on my desktop, so I'm going to be testing it on my laptop and post the results here today.
Posted By: Emre

Re: DirectX : 3d Hardware Reset Failure - 12/01/20 12:10

I'm happy to help. Hope it works without any error.
Posted By: jumpman

Re: DirectX : 3d Hardware Reset Failure - 12/01/20 17:32

Hey Emre, so far its been working really well on multiple desktops and laptops! I always have the feeling its not 100% perfect, but I think this is it!

I hope everyone can see Emre's solution above, because its been working for me!

Thank you Emre and Dooley!!!
Posted By: 3run

Re: DirectX : 3d Hardware Reset Failure - 12/01/20 18:23

Thank you for your time and help guys!
Posted By: Dooley

Re: DirectX : 3d Hardware Reset Failure - 12/31/21 21:24

I don't know if this will help anyone else, but it seems to be the best option for my game.

Code
foreground = GetForegroundWindow();

		if(foreground!=hWnd)
		{
			beep();
			proc_mode = PROC_NOFREEZE;
			freeze_mode = 2;
			while(1)
			{
				foreground = GetForegroundWindow();
				if(foreground==hWnd)
				{
					freeze_mode = 0;
					proc_mode = PROC_GLOBAL;
					break;
				}
				wait(1);
			}
			
			wait(1);
			beep();
		}


Since my game does not need to run in the background, I just froze it whenever it was not the foreground window. It's based on Emri's code, but adjusted slightly. This is all in another while loop that I use for other things, like calculating fps and some other general purpose scripts.
© 2024 lite-C Forums