From e491ec1f0b56ec9cbcfcc8ecc92b6c2bc61932b4 Mon Sep 17 00:00:00 2001 From: "Paul C. Scharf" Date: Mon, 22 Jul 2024 20:46:08 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=9B=A0=EF=B8=8F=20Allow=20custom=20blend?= =?UTF-8?q?=20modes=20in=20pipeline?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Pipelines/Context/BlendMode.cs | 29 +++++++++++-------- Bearded.Graphics/Pipelines/Context/GLState.cs | 18 ++---------- 2 files changed, 19 insertions(+), 28 deletions(-) diff --git a/Bearded.Graphics/Pipelines/Context/BlendMode.cs b/Bearded.Graphics/Pipelines/Context/BlendMode.cs index 2650f1a..e189684 100644 --- a/Bearded.Graphics/Pipelines/Context/BlendMode.cs +++ b/Bearded.Graphics/Pipelines/Context/BlendMode.cs @@ -1,14 +1,19 @@ -namespace Bearded.Graphics.Pipelines.Context +using OpenTK.Graphics.OpenGL; +using static OpenTK.Graphics.OpenGL.BlendingFactor; +using static OpenTK.Graphics.OpenGL.BlendEquationMode; + +namespace Bearded.Graphics.Pipelines.Context; + +public readonly record struct BlendMode( + BlendingFactor Source, BlendingFactor Destination, BlendEquationMode Function) { - public enum BlendMode - { - None = 0, - Alpha, - Add, - Subtract, - Multiply, - Premultiplied, - Min, - Max, - } + public static BlendMode Disable => default; + + public static BlendMode Alpha => new(SrcAlpha, OneMinusSrcAlpha, FuncAdd); + public static BlendMode Add => new(SrcAlpha, One, FuncAdd); + public static BlendMode Subtract => new(SrcAlpha, One, FuncReverseSubtract); + public static BlendMode Multiply => new(Zero, SrcColor, FuncAdd); + public static BlendMode Premultiplied => new(One, OneMinusSrcAlpha, FuncAdd); + public static BlendMode Min => new(One, One, BlendEquationMode.Min); + public static BlendMode Max => new(One, One, BlendEquationMode.Max); } diff --git a/Bearded.Graphics/Pipelines/Context/GLState.cs b/Bearded.Graphics/Pipelines/Context/GLState.cs index 6ff72c5..4f03abe 100644 --- a/Bearded.Graphics/Pipelines/Context/GLState.cs +++ b/Bearded.Graphics/Pipelines/Context/GLState.cs @@ -2,8 +2,6 @@ using System.Drawing; using OpenTK.Graphics.OpenGL; using static Bearded.Graphics.Pipelines.Context.CullMode; -using static OpenTK.Graphics.OpenGL.BlendEquationMode; -using static OpenTK.Graphics.OpenGL.BlendingFactor; namespace Bearded.Graphics.Pipelines.Context { @@ -53,7 +51,7 @@ public static void SetBlendMode(BlendMode mode) { BlendMode = mode; - if (mode == BlendMode.None) + if (mode == BlendMode.Disable) { GL.Disable(EnableCap.Blend); return; @@ -61,19 +59,7 @@ public static void SetBlendMode(BlendMode mode) GL.Enable(EnableCap.Blend); - // TODO: refactor to work similarly to depth mode to make more flexible - var (src, dst, equation) = mode switch - { - BlendMode.Alpha => (SrcAlpha, OneMinusSrcAlpha, FuncAdd), - BlendMode.Add => (SrcAlpha, One, FuncAdd), - BlendMode.Subtract => (SrcAlpha, One, FuncReverseSubtract), - BlendMode.Multiply => (Zero, SrcColor, FuncAdd), - BlendMode.Premultiplied => (One, OneMinusSrcAlpha, FuncAdd), - BlendMode.Min => (One, One, Min), - BlendMode.Max => (One, One, Max), - BlendMode.None => throw new InvalidOperationException(), - _ => throw new ArgumentOutOfRangeException(nameof(mode), mode, null) - }; + var (src, dst, equation) = mode; GL.BlendFunc(src, dst); GL.BlendEquation(equation);