Mostly correct blending for 2D renderers

This commit is contained in:
CasualPokePlayer 2024-05-25 12:01:52 -07:00
parent 9bc3e182f9
commit 75064675f1
9 changed files with 54 additions and 20 deletions

View File

@ -18,7 +18,8 @@ namespace BizHawk.Bizware.Graphics
public ID3D11DeviceContext Context;
public IDXGIFactory1 Factory1;
public IDXGIFactory2 Factory2;
public ID3D11BlendState BlendEnableState;
public ID3D11BlendState BlendNormalState;
public ID3D11BlendState BlendAlphaState;
public ID3D11BlendState BlendDisableState;
public ID3D11SamplerState PointSamplerState;
public ID3D11SamplerState LinearSamplerState;
@ -84,11 +85,13 @@ namespace BizHawk.Bizware.Graphics
bd.RenderTarget[0].DestinationBlendAlpha = Blend.InverseSourceAlpha;
bd.RenderTarget[0].BlendOperationAlpha = BlendOperation.Add;
bd.RenderTarget[0].RenderTargetWriteMask = ColorWriteEnable.All;
BlendEnableState = Device.CreateBlendState(bd);
BlendNormalState = Device.CreateBlendState(bd);
bd.RenderTarget[0].BlendEnable = false;
bd.RenderTarget[0].SourceBlend = Blend.One;
bd.RenderTarget[0].DestinationBlend = Blend.Zero;
BlendAlphaState = Device.CreateBlendState(bd);
bd.RenderTarget[0].BlendEnable = false;
BlendDisableState = Device.CreateBlendState(bd);
PointSamplerState = Device.CreateSamplerState(SamplerDescription.PointClamp);
@ -141,8 +144,10 @@ namespace BizHawk.Bizware.Graphics
RasterizerState?.Dispose();
RasterizerState = null;
BlendEnableState?.Dispose();
BlendEnableState = null;
BlendNormalState?.Dispose();
BlendNormalState = null;
BlendAlphaState?.Dispose();
BlendAlphaState = null;
BlendDisableState?.Dispose();
BlendDisableState = null;

View File

@ -25,7 +25,8 @@ namespace BizHawk.Bizware.Graphics
private ID3D11DeviceContext Context => _resources.Context;
private IDXGIFactory1 Factory1 => _resources.Factory1;
private IDXGIFactory2 Factory2 => _resources.Factory2;
private ID3D11BlendState BlendEnableState => _resources.BlendEnableState;
private ID3D11BlendState BlendNormalState => _resources.BlendNormalState;
private ID3D11BlendState BlendAlphaState => _resources.BlendAlphaState;
private ID3D11BlendState BlendDisableState => _resources.BlendDisableState;
private ID3D11RasterizerState RasterizerState => _resources.RasterizerState;
@ -184,8 +185,11 @@ namespace BizHawk.Bizware.Graphics
public void ClearColor(Color color)
=> Context.ClearRenderTargetView(CurRenderTarget?.RTV ?? _controlSwapChain.RTV, new(color.R, color.B, color.G, color.A));
public void EnableBlending()
=> Context.OMSetBlendState(BlendEnableState);
public void EnableBlendNormal()
=> Context.OMSetBlendState(BlendNormalState);
public void EnableBlendAlpha()
=> Context.OMSetBlendState(BlendAlphaState);
public void DisableBlending()
=> Context.OMSetBlendState(BlendDisableState);

View File

@ -25,7 +25,14 @@ namespace BizHawk.Bizware.Graphics
public void ClearColor(Color color)
=> GetCurrentGraphics().Clear(color);
public void EnableBlending()
public void EnableBlendNormal()
{
var g = GetCurrentGraphics();
g.CompositingMode = CompositingMode.SourceOver;
g.CompositingQuality = CompositingQuality.Default;
}
public void EnableBlendAlpha()
{
var g = GetCurrentGraphics();
g.CompositingMode = CompositingMode.SourceOver;

View File

@ -57,7 +57,13 @@ namespace BizHawk.Bizware.Graphics
/// <summary>
/// Enables normal (non-premultiplied) alpha blending.
/// </summary>
void EnableBlending();
void EnableBlendNormal();
/// <summary>
/// Enables alpha blending (non-alpha values are copied from the source fragment).
/// This mimics GDI+ blending
/// </summary>
void EnableBlendAlpha();
/// <summary>
/// Disables blending (alpha values are copied from the source fragment)

View File

@ -41,13 +41,20 @@ namespace BizHawk.Bizware.Graphics
GL.Clear(ClearBufferMask.ColorBufferBit);
}
public void EnableBlending()
public void EnableBlendNormal()
{
GL.Enable(EnableCap.Blend);
GL.BlendEquation(GLEnum.FuncAdd);
GL.BlendFuncSeparate(BlendingFactor.SrcAlpha, BlendingFactor.OneMinusSrcAlpha, BlendingFactor.One, BlendingFactor.OneMinusSrcAlpha);
}
public void EnableBlendAlpha()
{
GL.Enable(EnableCap.Blend);
GL.BlendEquation(GLEnum.FuncAdd);
GL.BlendFuncSeparate(BlendingFactor.One, BlendingFactor.Zero, BlendingFactor.One, BlendingFactor.OneMinusSrcAlpha);
}
public void DisableBlending()
=> GL.Disable(EnableCap.Blend);

View File

@ -88,7 +88,7 @@ namespace BizHawk.Bizware.Graphics
}
public void EnableBlending()
=> Owner.EnableBlending();
=> Owner.EnableBlendNormal();
public void DisableBlending()
=> Owner.DisableBlending();

View File

@ -120,7 +120,7 @@ namespace BizHawk.Bizware.Graphics
public void EnableBlending()
{
Flush();
Owner.EnableBlending();
Owner.EnableBlendNormal();
}
public void DisableBlending()

View File

@ -153,7 +153,8 @@ namespace BizHawk.Bizware.Graphics
{
None,
DisableBlending,
EnableBlending,
EnableBlendAlpha,
EnableBlendNormal,
DrawString,
}
@ -161,7 +162,7 @@ namespace BizHawk.Bizware.Graphics
{
if (EnableBlending)
{
_igl.EnableBlending();
_igl.EnableBlendAlpha();
}
else
{
@ -223,8 +224,11 @@ namespace BizHawk.Bizware.Graphics
case DrawCallbackId.DisableBlending:
_igl.DisableBlending();
break;
case DrawCallbackId.EnableBlending:
_igl.EnableBlending();
case DrawCallbackId.EnableBlendAlpha:
_igl.EnableBlendAlpha();
break;
case DrawCallbackId.EnableBlendNormal:
_igl.EnableBlendNormal();
break;
case DrawCallbackId.DrawString:
{
@ -277,7 +281,7 @@ namespace BizHawk.Bizware.Graphics
{
ClearStringOutput();
// synthesize an add image command for our string bitmap
_imGuiDrawList.AddCallback((IntPtr)DrawCallbackId.EnableBlending, IntPtr.Zero);
_imGuiDrawList.AddCallback((IntPtr)DrawCallbackId.EnableBlendNormal, IntPtr.Zero);
DrawImage(_stringOutput, 0, 0);
}
@ -311,7 +315,7 @@ namespace BizHawk.Bizware.Graphics
break;
// CompositingMode.SourceOver means enable blending
case false when value == CompositingMode.SourceOver:
_imGuiDrawList.AddCallback((IntPtr)DrawCallbackId.EnableBlending, IntPtr.Zero);
_imGuiDrawList.AddCallback((IntPtr)DrawCallbackId.EnableBlendAlpha, IntPtr.Zero);
_pendingBlendEnable = true;
break;
}

View File

@ -165,7 +165,8 @@ namespace BizHawk.Bizware.Graphics
case DrawCallbackId.DisableBlending:
_ = SDL_SetRenderDrawBlendMode(sdlRenderer, SDL_BlendMode.SDL_BLENDMODE_NONE);
break;
case DrawCallbackId.EnableBlending:
case DrawCallbackId.EnableBlendAlpha:
case DrawCallbackId.EnableBlendNormal:
_ = SDL_SetRenderDrawBlendMode(sdlRenderer, SDL_BlendMode.SDL_BLENDMODE_BLEND);
break;
case DrawCallbackId.DrawString: