# D3D Hook rendering question

## Recommended Posts

Pickl3d    166
Hi guys, thanks for the wealth of information here on DirectX, fantastic! I have a question about the DirectX hooking sample found here, on some games when I draw an overlayed coloured triangle, it works perfectly (via EndScene wrapper interface hook). However on other games the scene is all messed up (either completely black or extremely pixelated). So my question is, what should I ensure when drawing over the top of a game so as not to upset the rendering? I tried GetRenderState and restoring any "Original" states back to what they were after I have drawn my primatives (aswell as the FVF state)...but that still gave me the problems above. Ultimately i'd like to make my own little generic menu system and "mini game" inside that process. Thanks for any pointers on how to safely draw on another process and what I might be missing.

##### Share on other sites
Evil Steve    2017
What exactly are you doing to draw the triangle? Can we see the code?

Basically, you need to restore everything to how it was before you were there; but GetRenderState() may not work - if the device was created as a pure device then most Get*() calls will fail.

##### Share on other sites
Pickl3d    166
Ah interesting, didn't know about the pure device thing,I will go research that, I never bothered to check the result of the GetRender state so step through the code later.

I'll post up my code as soon as I get home :)

Thanks Steve

##### Share on other sites
Pickl3d    166
Yeh still struggling, this works ok with a good load of different games I have:

STDMETHOD(EndScene)(THIS){	if(InitializedPrivateData==0)	{//... create vertex buffer..	}              //BACKUP - Is this right?        IDirect3DStateBlock9* pStateBlock = NULL;        CreateStateBlock( D3DSBT_ALL, &pStateBlock );		/* draw 1 colored triangle*/	SetRenderState(D3DRS_CULLMODE,D3DCULL_NONE);	SetRenderState(D3DRS_ZENABLE,D3DZB_FALSE);	SetRenderState(D3DRS_LIGHTING, 0);	SetFVF(CUSTOMFVF);	SetStreamSource(0, t_buffer, 0, sizeof(CUSTOMVERTEX));	DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);	/* draw things */        //RESTORE - Is this right?	pStateBlock->Apply();	return m_device->EndScene();}

I modified it so games can't use passive mode by unsetting the bit on createdevice.

Any of this look suss?

I guess I need to understand some elements that different games use so I can simply draw right on the top and see the result not a "black" triangle :/

[Edited by - Pickl3d on December 17, 2008 6:13:14 PM]

##### Share on other sites
Evil Steve    2017
You need to call IDirect3DStateBlock9::Capture before changing any render states, so the state block contains all of the state information at that time.

If you're using state blocks, you can still use a pure device so there's no need to unset that flag.

Calling CreateStateBlock() creates a new state block, and you never Release() it, so you have a memory leak. Make sure you call pStateBlock->Release(); after pStateBlock->Apply();.
Having said that, creating objects at runtime like that isn't a good idea, since it's slow and can lead to fragmenting video memory, particularly on bad drivers. It'd be preferable to call CreateStateBlock() just after the device is created, and make sure you Release() it before any Reset() call, re-create it after, and Release() the state block when the device is Release()d.

##### Share on other sites
Pickl3d    166
Good info Steve thanks, I see what you are saying about creation times and leaks heh, ouch :)

CreateStateBlock apparently initializes the State block data since we asked for D3DSBT_ALL, so I believe there is no requirement to Capture in this case, from what I gather Capture is a requirement to change the existing state acquiring from D3DSBT_ALL.

I figured out why it wasn't working properly, seems I should of been setting some shader settings for certain some games. Learning all the time.

Time to buy a decent book on DirectX now.

Have a nice Christmas Steve.