Lol, I had my browser open for 4 hours now and forgot to press the submit button...
But even if this one is solved, other people may run into similar problems, so there's the all-in-one solution mentioned by Kulik:
Code: Select all
void my_render_function()
{
IDirect3DStateBlock9* old_state = 0;
m_pDevice->CreateStateBlock(D3DSBT_ALL, &old_state);
old_state->Capture();
CEGUI::System::getSingleton().renderGUI();
old_state->Apply();
}
Additionally, the CEGUI itself also requires some D3D9 states to be set correctly, so this may not be enough in some cases.
For my overlay, I needed to create a default state before rendering the GUI:
Code: Select all
void D3D9Injector::set_default_state()
{
if (default_state_ && default_state_->Apply() != S_OK)
default_state_ = 0;
if (default_state_)
return;
device_->CreateStateBlock(D3DSBT_ALL, default_state_.ref());
if (!default_state_)
return;
CEGUI::Logger::getSingleton().logEvent("d3d9: Storing default rendering state");
device_->SetVertexShader(NULL);
device_->SetPixelShader(NULL);
//device_->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
//device_->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
device_->SetRenderState(D3DRS_SHADEMODE, D3DSHADE_GOURAUD);
//device_->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
//device_->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
//device_->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ONE);
//device_->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
//device_->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
device_->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
device_->SetRenderState(D3DRS_ALPHAREF, 0);
device_->SetRenderState(D3DRS_ALPHAFUNC, D3DCMP_ALWAYS);
//device_->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
//device_->SetRenderState(D3DRS_FOGENABLE, FALSE);
device_->SetRenderState(D3DRS_TEXTUREFACTOR, 0xFFFFFFFF);
device_->SetRenderState(D3DRS_WRAP0, FALSE); // 0x80
//device_->SetRenderState(D3DRS_LIGHTING, FALSE);
device_->SetRenderState(D3DRS_COLORVERTEX, FALSE);
device_->SetRenderState(D3DRS_DIFFUSEMATERIALSOURCE, D3DMCS_COLOR1);
device_->SetRenderState(D3DRS_SPECULARMATERIALSOURCE, D3DMCS_COLOR2);
device_->SetRenderState(D3DRS_AMBIENTMATERIALSOURCE, D3DMCS_MATERIAL);
device_->SetRenderState(D3DRS_EMISSIVEMATERIALSOURCE, D3DMCS_MATERIAL);
device_->SetRenderState(D3DRS_COLORWRITEENABLE, 15);
//device_->SetRenderState(D3DRS_SCISSORTESTENABLE, TRUE);
device_->SetRenderState(D3DRS_BLENDFACTOR, 0);
device_->SetRenderState(D3DRS_DEPTHBIAS, 0);
//device_->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUEFALSE);
//device_->SetRenderState(D3DRS_SRCBLENDALPHA, D3DBLEND_INVDESTALPHA);
//device_->SetRenderState(D3DRS_DESTBLENDALPHA, D3DBLEND_ONE);
device_->SetRenderState(D3DRS_BLENDOPALPHA, D3DBLENDOP_ADD);
device_->SetRenderState(D3DRS_SRGBWRITEENABLE, FALSE);
device_->SetSamplerState(0, D3DSAMP_ADDRESSU, 1);
device_->SetSamplerState(0, D3DSAMP_ADDRESSV, 1);
device_->SetSamplerState(0, D3DSAMP_ADDRESSW, 3);
device_->SetSamplerState(0, D3DSAMP_BORDERCOLOR, 0);
device_->SetSamplerState(0, D3DSAMP_MAGFILTER, 1);
device_->SetSamplerState(0, D3DSAMP_MINFILTER, 1);
device_->SetSamplerState(0, D3DSAMP_MIPFILTER, 0);
device_->SetSamplerState(0, D3DSAMP_MIPMAPLODBIAS, 0);
device_->SetSamplerState(0, D3DSAMP_MAXMIPLEVEL, 0);
device_->SetSamplerState(0, D3DSAMP_MAXANISOTROPY, 1);
device_->SetSamplerState(0, D3DSAMP_SRGBTEXTURE, 0);
device_->SetSamplerState(0, D3DSAMP_ELEMENTINDEX, 0);
device_->SetSamplerState(0, D3DSAMP_DMAPOFFSET, 0);
device_->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
device_->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
device_->SetTextureStageState(0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE);
device_->LightEnable(0, FALSE);
for (size_t i = 1; i < 8; ++i)
device_->SetTextureStageState(i, D3DTSS_COLOROP, D3DTOP_DISABLE);
default_state_->Capture();
}
The commented lines are the states set by the CEGUI itself, so they're just there for completeness' sake.
Something is still missing (my overlay still has strange effects in some games), but it should be sufficient for most cases.
If anyone finds something I may have missed, I'd be glad to hear from you