Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix incorrect D3D9 blending #4268

Closed
wants to merge 1 commit into from

Conversation

NZJenkins
Copy link
Contributor

@NZJenkins NZJenkins commented Jun 25, 2021

imgui only uses texture stage state for stage 0
But any state set in stages >= 1 will affect rendering
Set stage 1 COLOROP to DISABLED, to deactivate stages >= 1

Our rendering was using fixed function and texture stage state - our states in stage 1, 2, and 3 would interfere with imgui rendering, since it intended to use stage 0 only.

https://docs.microsoft.com/en-us/windows/win32/direct3d9/texture-blending-operations-and-arguments

You can disable a texture stage - and any subsequent texture blending stages in the cascade - by setting the color operation for that stage to D3DTOP_DISABLE. Disabling the color operation effectively disables the alpha operation as well. Alpha operations cannot be disabled when color operations are enabled. Setting the alpha operation to D3DTOP_DISABLE when color blending is enabled causes undefined behavior.

imgui only uses texture stage state for stage 0
But any state set in stages >= 1 will affect rendering
Set stage 1 COLOROP to DISABLED, to deactivate stages >= 1
@ocornut
Copy link
Owner

ocornut commented Jun 25, 2021

Hello,
I need to confirm exactly what we are fixing and if other state such as ALPHAOP for stage 1 needs explicit setting.

Could you clarify how to "break" rendering without this patch?

If before the call to ImGui_ImplDX9_RenderDrawData() I add, e.g.

g_pd3dDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_MODULATE);
g_pd3dDevice->SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_TEXTURE);
g_pd3dDevice->SetTextureStageState(1, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
g_pd3dDevice->SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
g_pd3dDevice->SetTextureStageState(1, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
g_pd3dDevice->SetTextureStageState(1, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE);
g_pd3dDevice->SetTextureStageState(2, D3DTSS_COLOROP, D3DTOP_MODULATE);
g_pd3dDevice->SetTextureStageState(2, D3DTSS_COLORARG1, D3DTA_TEXTURE);
g_pd3dDevice->SetTextureStageState(2, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
g_pd3dDevice->SetTextureStageState(2, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
g_pd3dDevice->SetTextureStageState(2, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
g_pd3dDevice->SetTextureStageState(2, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE);

Without your patch everything renders correctly.

ocornut pushed a commit that referenced this pull request Jun 25, 2021
…4268)

imgui only uses texture stage state for stage 0
But any state set in stages >= 1 will affect rendering
Set stage 1 COLOROP to DISABLED, to deactivate stages >= 1
@ocornut
Copy link
Owner

ocornut commented Jun 25, 2021

OK i realized that my example was essentially setting other stage with same settings.
I merged as 8ced415 also added the explicit call for g_pd3dDevice->SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_DISABLE);
Thank you!

@ocornut ocornut closed this Jun 25, 2021
@NZJenkins NZJenkins deleted the d3d9-state-fix branch June 25, 2021 11:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants