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

View File

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

View File

@ -25,7 +25,14 @@ namespace BizHawk.Bizware.Graphics
public void ClearColor(Color color) public void ClearColor(Color color)
=> GetCurrentGraphics().Clear(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(); var g = GetCurrentGraphics();
g.CompositingMode = CompositingMode.SourceOver; g.CompositingMode = CompositingMode.SourceOver;

View File

@ -57,7 +57,13 @@ namespace BizHawk.Bizware.Graphics
/// <summary> /// <summary>
/// Enables normal (non-premultiplied) alpha blending. /// Enables normal (non-premultiplied) alpha blending.
/// </summary> /// </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> /// <summary>
/// Disables blending (alpha values are copied from the source fragment) /// Disables blending (alpha values are copied from the source fragment)

View File

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

View File

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

View File

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

View File

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

View File

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