Skip to content

Commit

Permalink
Improve BlendAttachmentDescription
Browse files Browse the repository at this point in the history
  • Loading branch information
xoofx committed Jun 28, 2024
1 parent fb41c81 commit 79dd2b2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 43 deletions.
58 changes: 16 additions & 42 deletions src/XenoAtom.Graphics/BlendAttachmentDescription.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
using System;
using System;

namespace XenoAtom.Graphics
{
/// <summary>
/// A <see cref="Pipeline"/> component describing the blend behavior for an individual color attachment.
/// </summary>
public struct BlendAttachmentDescription : IEquatable<BlendAttachmentDescription>
public record struct BlendAttachmentDescription
{
/// <summary>
/// Controls whether blending is enabled for the color attachment.
/// </summary>
public bool BlendEnabled;
/// <summary>
/// Controls which components of the color will be written to the framebuffer.
/// If <c>null</c>, the mask will be set to <see cref="XenoAtom.Graphics.ColorWriteMask.All"/>.
/// Controls which components of the color will be written to the framebuffer. The default is <see cref="ColorWriteMask.All"/>.
/// </summary>
public ColorWriteMask? ColorWriteMask;
public ColorWriteMask ColorWriteMask;
/// <summary>
/// Controls the source color's influence on the blend result.
/// </summary>
Expand All @@ -41,6 +40,13 @@ public struct BlendAttachmentDescription : IEquatable<BlendAttachmentDescription
/// </summary>
public BlendFunction AlphaFunction;


public BlendAttachmentDescription()
{
ColorWriteMask = ColorWriteMask.All;
}


/// <summary>
/// Constructs a new <see cref="BlendAttachmentDescription"/>.
/// </summary>
Expand All @@ -67,7 +73,7 @@ public BlendAttachmentDescription(
SourceAlphaFactor = sourceAlphaFactor;
DestinationAlphaFactor = destinationAlphaFactor;
AlphaFunction = alphaFunction;
ColorWriteMask = null;
ColorWriteMask = ColorWriteMask.All;
}

/// <summary>
Expand Down Expand Up @@ -113,7 +119,7 @@ public BlendAttachmentDescription(
/// DestinationAlphaFactor = BlendFactor.Zero
/// AlphaFunction = BlendFunction.Add
/// </summary>
public static readonly BlendAttachmentDescription OverrideBlend = new BlendAttachmentDescription
public static readonly BlendAttachmentDescription OverrideBlend = new()
{
BlendEnabled = true,
SourceColorFactor = BlendFactor.One,
Expand All @@ -136,7 +142,7 @@ public BlendAttachmentDescription(
/// DestinationAlphaFactor = BlendFactor.InverseSourceAlpha
/// AlphaFunction = BlendFunction.Add
/// </summary>
public static readonly BlendAttachmentDescription AlphaBlend = new BlendAttachmentDescription
public static readonly BlendAttachmentDescription AlphaBlend = new()
{
BlendEnabled = true,
SourceColorFactor = BlendFactor.SourceAlpha,
Expand All @@ -159,7 +165,7 @@ public BlendAttachmentDescription(
/// DestinationAlphaFactor = BlendFactor.One
/// AlphaFunction = BlendFunction.Add
/// </summary>
public static readonly BlendAttachmentDescription AdditiveBlend = new BlendAttachmentDescription
public static readonly BlendAttachmentDescription AdditiveBlend = new()
{
BlendEnabled = true,
SourceColorFactor = BlendFactor.SourceAlpha,
Expand All @@ -182,7 +188,7 @@ public BlendAttachmentDescription(
/// DestinationAlphaFactor = BlendFactor.Zero
/// AlphaFunction = BlendFunction.Add
/// </summary>
public static readonly BlendAttachmentDescription Disabled = new BlendAttachmentDescription
public static readonly BlendAttachmentDescription Disabled = new()
{
BlendEnabled = false,
SourceColorFactor = BlendFactor.One,
Expand All @@ -192,37 +198,5 @@ public BlendAttachmentDescription(
DestinationAlphaFactor = BlendFactor.Zero,
AlphaFunction = BlendFunction.Add,
};

/// <summary>
/// Element-wise equality.
/// </summary>
/// <param name="other">The instance to compare to.</param>
/// <returns>True if all elements and all array elements are equal; false otherswise.</returns>
public bool Equals(BlendAttachmentDescription other)
{
return BlendEnabled.Equals(other.BlendEnabled)
&& ColorWriteMask.Equals(other.ColorWriteMask)
&& SourceColorFactor == other.SourceColorFactor
&& DestinationColorFactor == other.DestinationColorFactor && ColorFunction == other.ColorFunction
&& SourceAlphaFactor == other.SourceAlphaFactor && DestinationAlphaFactor == other.DestinationAlphaFactor
&& AlphaFunction == other.AlphaFunction;
}

/// <summary>
/// Returns the hash code for this instance.
/// </summary>
/// <returns>A 32-bit signed integer that is the hash code for this instance.</returns>
public override int GetHashCode()
{
return HashHelper.Combine(
BlendEnabled.GetHashCode(),
ColorWriteMask.GetHashCode(),
(int)SourceColorFactor,
(int)DestinationColorFactor,
(int)ColorFunction,
(int)SourceAlphaFactor,
(int)DestinationAlphaFactor,
(int)AlphaFunction);
}
}
}
2 changes: 1 addition & 1 deletion src/XenoAtom.Graphics/Vk/VkPipeline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public VkPipeline(VkGraphicsDevice gd, ref GraphicsPipelineDescription descripti
attachmentState.srcAlphaBlendFactor = VkFormats.VdToVkBlendFactor(vdDesc.SourceAlphaFactor);
attachmentState.dstAlphaBlendFactor = VkFormats.VdToVkBlendFactor(vdDesc.DestinationAlphaFactor);
attachmentState.alphaBlendOp = VkFormats.VdToVkBlendOp(vdDesc.AlphaFunction);
attachmentState.colorWriteMask = VkFormats.VdToVkColorWriteMask(vdDesc.ColorWriteMask.GetOrDefault());
attachmentState.colorWriteMask = VkFormats.VdToVkColorWriteMask(vdDesc.ColorWriteMask);
attachmentState.blendEnable = vdDesc.BlendEnabled;
attachmentsPtr[i] = attachmentState;
}
Expand Down

0 comments on commit 79dd2b2

Please sign in to comment.