28. The Framebuffer
28.1. Blending
Blending combines the incoming source fragment’s R, G, B, and A values with the destination R, G, B, and A values of each sample stored in the framebuffer at the fragment’s (xf,yf) location. Blending is performed for each color sample covered by the fragment, rather than just once for each fragment.
Source and destination values are combined according to the blend operation, quadruplets of source and destination weighting factors determined by the blend factors, and a blend constant, to obtain a new set of R, G, B, and A values, as described below.
Blending is computed and applied separately to each color attachment used by the subpass, with separate controls for each attachment.
Prior to performing the blend operation, signed and unsigned normalized fixed-point color components undergo an implied conversion to floating-point as specified by Conversion from Normalized Fixed-Point to Floating-Point. Blending computations are treated as if carried out in floating-point, and basic blend operations are performed with a precision and dynamic range no lower than that used to represent destination components. Advanced blending operations are performed with a precision and dynamic range no lower than the smaller of that used to represent destination components or that used to represent 16-bit floating-point values.
Blending applies only to fixed-point and floating-point color attachments. If the color attachment has an integer format, blending is not applied.
The pipeline blend state is included in the
VkPipelineColorBlendStateCreateInfo structure during graphics pipeline
creation:
The VkPipelineColorBlendStateCreateInfo structure is defined as:
typedef struct VkPipelineColorBlendStateCreateInfo {
VkStructureType sType;
const void* pNext;
VkPipelineColorBlendStateCreateFlags flags;
VkBool32 logicOpEnable;
VkLogicOp logicOp;
uint32_t attachmentCount;
const VkPipelineColorBlendAttachmentState* pAttachments;
float blendConstants[4];
} VkPipelineColorBlendStateCreateInfo;
-
sTypeis the type of this structure. -
pNextisNULLor a pointer to an extension-specific structure. -
flagsis reserved for future use. -
logicOpEnablecontrols whether to apply Logical Operations. -
logicOpselects which logical operation to apply. -
attachmentCountis the number ofVkPipelineColorBlendAttachmentStateelements inpAttachments. This value must equal thecolorAttachmentCountfor the subpass in which this pipeline is used. -
pAttachments: is a pointer to array of per target attachment states. -
blendConstantsis an array of four values used as the R, G, B, and A components of the blend constant that are used in blending, depending on the blend factor.
Each element of the pAttachments array is a
VkPipelineColorBlendAttachmentState structure specifying per-target
blending state for each individual color attachment.
If the independent blending feature is not
enabled on the device, all VkPipelineColorBlendAttachmentState
elements in the pAttachments array must be identical.
typedef VkFlags VkPipelineColorBlendStateCreateFlags;
VkPipelineColorBlendStateCreateFlags is a bitmask type for setting a
mask, but is currently reserved for future use.
The VkPipelineColorBlendAttachmentState structure is defined as:
typedef struct VkPipelineColorBlendAttachmentState {
VkBool32 blendEnable;
VkBlendFactor srcColorBlendFactor;
VkBlendFactor dstColorBlendFactor;
VkBlendOp colorBlendOp;
VkBlendFactor srcAlphaBlendFactor;
VkBlendFactor dstAlphaBlendFactor;
VkBlendOp alphaBlendOp;
VkColorComponentFlags colorWriteMask;
} VkPipelineColorBlendAttachmentState;
-
blendEnablecontrols whether blending is enabled for the corresponding color attachment. If blending is not enabled, the source fragment’s color for that attachment is passed through unmodified. -
srcColorBlendFactorselects which blend factor is used to determine the source factors (Sr,Sg,Sb). -
dstColorBlendFactorselects which blend factor is used to determine the destination factors (Dr,Dg,Db). -
colorBlendOpselects which blend operation is used to calculate the RGB values to write to the color attachment. -
srcAlphaBlendFactorselects which blend factor is used to determine the source factor Sa. -
dstAlphaBlendFactorselects which blend factor is used to determine the destination factor Da. -
alphaBlendOpselects which blend operation is use to calculate the alpha values to write to the color attachment. -
colorWriteMaskis a bitmask of VkColorComponentFlagBits specifying which of the R, G, B, and/or A components are enabled for writing, as described for the Color Write Mask.
28.1.1. Blend Factors
The source and destination color and alpha blending factors are selected from the enum:
typedef enum VkBlendFactor {
VK_BLEND_FACTOR_ZERO = 0,
VK_BLEND_FACTOR_ONE = 1,
VK_BLEND_FACTOR_SRC_COLOR = 2,
VK_BLEND_FACTOR_ONE_MINUS_SRC_COLOR = 3,
VK_BLEND_FACTOR_DST_COLOR = 4,
VK_BLEND_FACTOR_ONE_MINUS_DST_COLOR = 5,
VK_BLEND_FACTOR_SRC_ALPHA = 6,
VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA = 7,
VK_BLEND_FACTOR_DST_ALPHA = 8,
VK_BLEND_FACTOR_ONE_MINUS_DST_ALPHA = 9,
VK_BLEND_FACTOR_CONSTANT_COLOR = 10,
VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_COLOR = 11,
VK_BLEND_FACTOR_CONSTANT_ALPHA = 12,
VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_ALPHA = 13,
VK_BLEND_FACTOR_SRC_ALPHA_SATURATE = 14,
VK_BLEND_FACTOR_SRC1_COLOR = 15,
VK_BLEND_FACTOR_ONE_MINUS_SRC1_COLOR = 16,
VK_BLEND_FACTOR_SRC1_ALPHA = 17,
VK_BLEND_FACTOR_ONE_MINUS_SRC1_ALPHA = 18,
VK_BLEND_FACTOR_MAX_ENUM = 0x7FFFFFFF
} VkBlendFactor;
The semantics of each enum value is described in the table below:
| VkBlendFactor | RGB Blend Factors (Sr,Sg,Sb) or (Dr,Dg,Db) | Alpha Blend Factor (Sa or Da) |
|---|---|---|
|
(0,0,0) |
0 |
|
(1,1,1) |
1 |
|
(Rs0,Gs0,Bs0) |
As0 |
|
(1-Rs0,1-Gs0,1-Bs0) |
1-As0 |
|
(Rd,Gd,Bd) |
Ad |
|
(1-Rd,1-Gd,1-Bd) |
1-Ad |
|
(As0,As0,As0) |
As0 |
|
(1-As0,1-As0,1-As0) |
1-As0 |
|
(Ad,Ad,Ad) |
Ad |
|
(1-Ad,1-Ad,1-Ad) |
1-Ad |
|
(Rc,Gc,Bc) |
Ac |
|
(1-Rc,1-Gc,1-Bc) |
1-Ac |
|
(Ac,Ac,Ac) |
Ac |
|
(1-Ac,1-Ac,1-Ac) |
1-Ac |
|
(f,f,f); f = min(As0,1-Ad) |
1 |
|
(Rs1,Gs1,Bs1) |
As1 |
|
(1-Rs1,1-Gs1,1-Bs1) |
1-As1 |
|
(As1,As1,As1) |
As1 |
|
(1-As1,1-As1,1-As1) |
1-As1 |
In this table, the following conventions are used:
-
Rs0,Gs0,Bs0 and As0 represent the first source color R, G, B, and A components, respectively, for the fragment output location corresponding to the color attachment being blended.
-
Rs1,Gs1,Bs1 and As1 represent the second source color R, G, B, and A components, respectively, used in dual source blending modes, for the fragment output location corresponding to the color attachment being blended.
-
Rd,Gd,Bd and Ad represent the R, G, B, and A components of the destination color. That is, the color currently in the corresponding color attachment for this fragment/sample.
-
Rc,Gc,Bc and Ac represent the blend constant R, G, B, and A components, respectively.
If the pipeline state object is created without the
VK_DYNAMIC_STATE_BLEND_CONSTANTS dynamic state enabled then the blend
constant (Rc,Gc,Bc,Ac) is specified via the
blendConstants member of VkPipelineColorBlendStateCreateInfo.
Otherwise, to dynamically set and change the blend constant, call:
void vkCmdSetBlendConstants(
VkCommandBuffer commandBuffer,
const float blendConstants[4]);
-
commandBufferis the command buffer into which the command will be recorded. -
blendConstantsis an array of four values specifying the R, G, B, and A components of the blend constant color used in blending, depending on the blend factor.
28.1.2. Dual-Source Blending
Blend factors that use the secondary color input
(Rs1,Gs1,Bs1,As1) (VK_BLEND_FACTOR_SRC1_COLOR,
VK_BLEND_FACTOR_ONE_MINUS_SRC1_COLOR,
VK_BLEND_FACTOR_SRC1_ALPHA, and
VK_BLEND_FACTOR_ONE_MINUS_SRC1_ALPHA) may consume implementation
resources that could otherwise be used for rendering to multiple color
attachments.
Therefore, the number of color attachments that can be used in a
framebuffer may be lower when using dual-source blending.
Dual-source blending is only supported if the
dualSrcBlend feature is enabled.
The maximum number of color attachments that can be used in a subpass when
using dual-source blending functions is implementation-dependent and is
reported as the maxFragmentDualSrcAttachments member of
VkPhysicalDeviceLimits.
When using a fragment shader with dual-source blending functions, the color
outputs are bound to the first and second inputs of the blender using the
Index decoration, as described in Fragment
Output Interface.
If the second color input to the blender is not written in the shader, or if
no output is bound to the second input of a blender, the result of the
blending operation is not defined.
28.1.3. Blend Operations
Once the source and destination blend factors have been selected, they along with the source and destination components are passed to the blending operations. RGB and alpha components can use different operations. Possible values of VkBlendOp, specifying the operations, are:
typedef enum VkBlendOp {
VK_BLEND_OP_ADD = 0,
VK_BLEND_OP_SUBTRACT = 1,
VK_BLEND_OP_REVERSE_SUBTRACT = 2,
VK_BLEND_OP_MIN = 3,
VK_BLEND_OP_MAX = 4,
VK_BLEND_OP_ZERO_EXT = 1000148000,
VK_BLEND_OP_SRC_EXT = 1000148001,
VK_BLEND_OP_DST_EXT = 1000148002,
VK_BLEND_OP_SRC_OVER_EXT = 1000148003,
VK_BLEND_OP_DST_OVER_EXT = 1000148004,
VK_BLEND_OP_SRC_IN_EXT = 1000148005,
VK_BLEND_OP_DST_IN_EXT = 1000148006,
VK_BLEND_OP_SRC_OUT_EXT = 1000148007,
VK_BLEND_OP_DST_OUT_EXT = 1000148008,
VK_BLEND_OP_SRC_ATOP_EXT = 1000148009,
VK_BLEND_OP_DST_ATOP_EXT = 1000148010,
VK_BLEND_OP_XOR_EXT = 1000148011,
VK_BLEND_OP_MULTIPLY_EXT = 1000148012,
VK_BLEND_OP_SCREEN_EXT = 1000148013,
VK_BLEND_OP_OVERLAY_EXT = 1000148014,
VK_BLEND_OP_DARKEN_EXT = 1000148015,
VK_BLEND_OP_LIGHTEN_EXT = 1000148016,
VK_BLEND_OP_COLORDODGE_EXT = 1000148017,
VK_BLEND_OP_COLORBURN_EXT = 1000148018,
VK_BLEND_OP_HARDLIGHT_EXT = 1000148019,
VK_BLEND_OP_SOFTLIGHT_EXT = 1000148020,
VK_BLEND_OP_DIFFERENCE_EXT = 1000148021,
VK_BLEND_OP_EXCLUSION_EXT = 1000148022,
VK_BLEND_OP_INVERT_EXT = 1000148023,
VK_BLEND_OP_INVERT_RGB_EXT = 1000148024,
VK_BLEND_OP_LINEARDODGE_EXT = 1000148025,
VK_BLEND_OP_LINEARBURN_EXT = 1000148026,
VK_BLEND_OP_VIVIDLIGHT_EXT = 1000148027,
VK_BLEND_OP_LINEARLIGHT_EXT = 1000148028,
VK_BLEND_OP_PINLIGHT_EXT = 1000148029,
VK_BLEND_OP_HARDMIX_EXT = 1000148030,
VK_BLEND_OP_HSL_HUE_EXT = 1000148031,
VK_BLEND_OP_HSL_SATURATION_EXT = 1000148032,
VK_BLEND_OP_HSL_COLOR_EXT = 1000148033,
VK_BLEND_OP_HSL_LUMINOSITY_EXT = 1000148034,
VK_BLEND_OP_PLUS_EXT = 1000148035,
VK_BLEND_OP_PLUS_CLAMPED_EXT = 1000148036,
VK_BLEND_OP_PLUS_CLAMPED_ALPHA_EXT = 1000148037,
VK_BLEND_OP_PLUS_DARKER_EXT = 1000148038,
VK_BLEND_OP_MINUS_EXT = 1000148039,
VK_BLEND_OP_MINUS_CLAMPED_EXT = 1000148040,
VK_BLEND_OP_CONTRAST_EXT = 1000148041,
VK_BLEND_OP_INVERT_OVG_EXT = 1000148042,
VK_BLEND_OP_RED_EXT = 1000148043,
VK_BLEND_OP_GREEN_EXT = 1000148044,
VK_BLEND_OP_BLUE_EXT = 1000148045,
VK_BLEND_OP_MAX_ENUM = 0x7FFFFFFF
} VkBlendOp;
The semantics of each basic blend operations is described in the table below:
| VkBlendOp | RGB Components | Alpha Component |
|---|---|---|
|
R = Rs0 × Sr + Rd × Dr |
A = As0 × Sa + Ad × Da |
|
R = Rs0 × Sr - Rd × Dr |
A = As0 × Sa - Ad × Da |
|
R = Rd × Dr - Rs0 × Sr |
A = Ad × Da - As0 × Sa |
|
R = min(Rs0,Rd) |
A = min(As0,Ad) |
|
R = max(Rs0,Rd) |
A = max(As0,Ad) |
In this table, the following conventions are used:
-
Rs0, Gs0, Bs0 and As0 represent the first source color R, G, B, and A components, respectively.
-
Rd, Gd, Bd and Ad represent the R, G, B, and A components of the destination color. That is, the color currently in the corresponding color attachment for this fragment/sample.
-
Sr, Sg, Sb and Sa represent the source blend factor R, G, B, and A components, respectively.
-
Dr, Dg, Db and Da represent the destination blend factor R, G, B, and A components, respectively.
The blending operation produces a new set of values R, G, B and A, which are written to the framebuffer attachment. If blending is not enabled for this attachment, then R, G, B and A are assigned Rs0, Gs0, Bs0 and As0, respectively.
If the color attachment is fixed-point, the components of the source and destination values and blend factors are each clamped to [0,1] or [-1,1] respectively for an unsigned normalized or signed normalized color attachment prior to evaluating the blend operations. If the color attachment is floating-point, no clamping occurs.
If the numeric format of a framebuffer attachment uses sRGB encoding, the R, G, and B destination color values (after conversion from fixed-point to floating-point) are considered to be encoded for the sRGB color space and hence are linearized prior to their use in blending. Each R, G, and B component is converted from nonlinear to linear as described in the “sRGB EOTF” section of the Khronos Data Format Specification. If the format is not sRGB, no linearization is performed.
If the numeric format of a framebuffer attachment uses sRGB encoding, then the final R, G and B values are converted into the nonlinear sRGB representation before being written to the framebuffer attachment as described in the “sRGB EOTF -1” section of the Khronos Data Format Specification.
If the framebuffer color attachment numeric format is not sRGB encoded then the resulting cs values for R, G and B are unmodified. The value of A is never sRGB encoded. That is, the alpha component is always stored in memory as linear.
If the framebuffer color attachment is VK_ATTACHMENT_UNUSED, no writes
are performed through that attachment.
Framebuffer color attachments greater than or equal to
VkSubpassDescription::colorAttachmentCount perform no writes.
28.1.4. Advanced Blend Operations
The advanced blend operations are those listed in tables f/X/Y/Z Advanced Blend Operations, Hue-Saturation-Luminosity Advanced Blend Operations, and Additional RGB Blend Operations.
If the pNext chain of VkPipelineColorBlendStateCreateInfo
includes a VkPipelineColorBlendAdvancedStateCreateInfoEXT structure,
then that structure includes parameters that affect advanced blend
operations.
The VkPipelineColorBlendAdvancedStateCreateInfoEXT structure is
defined as:
typedef struct VkPipelineColorBlendAdvancedStateCreateInfoEXT {
VkStructureType sType;
const void* pNext;
VkBool32 srcPremultiplied;
VkBool32 dstPremultiplied;
VkBlendOverlapEXT blendOverlap;
} VkPipelineColorBlendAdvancedStateCreateInfoEXT;
-
sTypeis the type of this structure. -
pNextisNULLor a pointer to an extension-specific structure. -
srcPremultipliedspecifies whether the source color of the blend operation is treated as premultiplied. -
dstPremultipliedspecifies whether the destination color of the blend operation is treated as premultiplied. -
blendOverlapis a VkBlendOverlapEXT value specifying how the source and destination sample’s coverage is correlated.
If this structure is not present, srcPremultiplied and
dstPremultiplied are both considered to be VK_TRUE, and
blendOverlap is considered to be
VK_BLEND_OVERLAP_UNCORRELATED_EXT.
When using one of the operations in table f/X/Y/Z Advanced Blend Operations or Hue-Saturation-Luminosity Advanced Blend Operations, blending is performed according to the following equations:
where the function f and terms X, Y, and Z are specified in the table.
The R, G, and B components of the source color used for blending are derived
according to srcPremultiplied.
If srcPremultiplied is set to VK_TRUE, the fragment color
components are considered to have been premultiplied by the A component
prior to blending.
The base source color (Rs',Gs',Bs') is obtained by dividing
through by the A component:
If srcPremultiplied is VK_FALSE, the fragment color components
are used as the base color:
The R, G, and B components of the destination color used for blending are
derived according to dstPremultiplied.
If dstPremultiplied is set to VK_TRUE, the destination
components are considered to have been premultiplied by the A component
prior to blending.
The base destination color (Rd',Gd',Bd') is obtained by dividing
through by the A component:
If dstPremultiplied is VK_FALSE, the destination color
components are used as the base color:
When blending using advanced blend operations, we expect that the R, G, and B components of premultiplied source and destination color inputs be stored as the product of non-premultiplied R, G, and B component values and the A component of the color. If any R, G, or B component of a premultiplied input color is non-zero and the A component is zero, the color is considered ill-formed, and the corresponding component of the blend result is undefined.
The weighting functions p0, p1, and p2 are defined in table Advanced Blend Overlap Modes. In these functions, the A components of the source and destination colors are taken to indicate the portion of the pixel covered by the fragment (source) and the fragments previously accumulated in the pixel (destination). The functions p0, p1, and p2 approximate the relative portion of the pixel covered by the intersection of the source and destination, covered only by the source, and covered only by the destination, respectively.
Possible values of
VkPipelineColorBlendAdvancedStateCreateInfoEXT::blendOverlap,
specifying the blend overlap functions, are:
typedef enum VkBlendOverlapEXT {
VK_BLEND_OVERLAP_UNCORRELATED_EXT = 0,
VK_BLEND_OVERLAP_DISJOINT_EXT = 1,
VK_BLEND_OVERLAP_CONJOINT_EXT = 2,
VK_BLEND_OVERLAP_MAX_ENUM_EXT = 0x7FFFFFFF
} VkBlendOverlapEXT;
-
VK_BLEND_OVERLAP_UNCORRELATED_EXTspecifies that there is no correlation between the source and destination coverage. -
VK_BLEND_OVERLAP_CONJOINT_EXTspecifies that the source and destination coverage are considered to have maximal overlap. -
VK_BLEND_OVERLAP_DISJOINT_EXTspecifies that the source and destination coverage are considered to have minimal overlap.
| Overlap Mode | Weighting Equations |
|---|---|
|
\[ \begin{aligned}
p_0(A_s,A_d) & = A_sA_d \\
p_1(A_s,A_d) & = A_s(1-A_d) \\
p_2(A_s,A_d) & = A_d(1-A_s) \\
\end{aligned}\]
|
|
\[ \begin{aligned}
p_0(A_s,A_d) & = min(A_s,A_d) \\
p_1(A_s,A_d) & = max(A_s-A_d,0) \\
p_2(A_s,A_d) & = max(A_d-A_s,0) \\
\end{aligned}\]
|
|
\[ \begin{aligned}
p_0(A_s,A_d) & = max(A_s+A_d-1,0) \\
p_1(A_s,A_d) & = min(A_s,1-A_d) \\
p_2(A_s,A_d) & = min(A_d,1-A_s) \\
\end{aligned}\]
|
| Mode | Blend Coefficients |
|---|---|
|
\[ \begin{aligned}
(X,Y,Z) & = (0,0,0) \\
f(C_s,C_d) & = 0
\end{aligned}\]
|
|
\[ \begin{aligned}
(X,Y,Z) & = (1,1,0) \\
f(C_s,C_d) & = C_s
\end{aligned}\]
|
|
\[ \begin{aligned}
(X,Y,Z) & = (1,0,1) \\
f(C_s,C_d) & = C_d
\end{aligned}\]
|
|
\[ \begin{aligned}
(X,Y,Z) & = (1,1,1) \\
f(C_s,C_d) & = C_s
\end{aligned}\]
|
|
\[ \begin{aligned}
(X,Y,Z) & = (1,1,1) \\
f(C_s,C_d) & = C_d
\end{aligned}\]
|
|
\[ \begin{aligned}
(X,Y,Z) & = (1,0,0) \\
f(C_s,C_d) & = C_s
\end{aligned}\]
|
|
\[ \begin{aligned}
(X,Y,Z) & = (1,0,0) \\
f(C_s,C_d) & = C_d
\end{aligned}\]
|
|
\[ \begin{aligned}
(X,Y,Z) & = (0,1,0) \\
f(C_s,C_d) & = 0
\end{aligned}\]
|
|
\[ \begin{aligned}
(X,Y,Z) & = (0,0,1) \\
f(C_s,C_d) & = 0
\end{aligned}\]
|
|
\[ \begin{aligned}
(X,Y,Z) & = (1,0,1) \\
f(C_s,C_d) & = C_s
\end{aligned}\]
|
|
\[ \begin{aligned}
(X,Y,Z) & = (1,1,0) \\
f(C_s,C_d) & = C_d
\end{aligned}\]
|
|
\[ \begin{aligned}
(X,Y,Z) & = (0,1,1) \\
f(C_s,C_d) & = 0
\end{aligned}\]
|
|
\[ \begin{aligned}
(X,Y,Z) & = (1,1,1) \\
f(C_s,C_d) & = C_sC_d
\end{aligned}\]
|
|
\[ \begin{aligned}
(X,Y,Z) & = (1,1,1) \\
f(C_s,C_d) & = C_s+C_d-C_sC_d
\end{aligned}\]
|
|
\[ \begin{aligned}
(X,Y,Z) & = (1,1,1) \\
f(C_s,C_d) & =
\begin{cases}
2 C_sC_d & C_d \leq 0.5 \\
1-2 (1-C_s)(1-C_d) & \text{otherwise}
\end{cases}
\end{aligned}\]
|
|
\[ \begin{aligned}
(X,Y,Z) & = (1,1,1) \\
f(C_s,C_d) & = min(C_s,C_d)
\end{aligned}\]
|
|
\[ \begin{aligned}
(X,Y,Z) & = (1,1,1) \\
f(C_s,C_d) & = max(C_s,C_d)
\end{aligned}\]
|
|
\[ \begin{aligned}
(X,Y,Z) & = (1,1,1) \\
f(C_s,C_d) & =
\begin{cases}
0 & C_d \leq 0 \\
min(1,\frac{C_d}{1-C_s}) & C_d \gt 0 \text{ and } C_s \lt 1 \\
1 & C_d \gt 0 \text{ and } C_s \geq 1
\end{cases}
\end{aligned}\]
|
|
\[ \begin{aligned}
(X,Y,Z) & = (1,1,1) \\
f(C_s,C_d) & =
\begin{cases}
1 & C_d \geq 1 \\
1 - min(1,\frac{1-C_d}{C_s}) & C_d \lt 1 \text{ and } C_s \gt 0 \\
0 & C_d \lt 1 \text{ and } C_s \leq 0
\end{cases}
\end{aligned}\]
|
|
\[ \begin{aligned}
(X,Y,Z) & = (1,1,1) \\
f(C_s,C_d) & =
\begin{cases}
2 C_sC_d & C_s \leq 0.5 \\
1-2 (1-C_s)(1-C_d) & \text{otherwise}
\end{cases}
\end{aligned}\]
|
|
\[ \begin{aligned}
(X,Y,Z) & = (1,1,1) \\
f(C_s,C_d) & =
\begin{cases}
C_d-(1-2 C_s)C_d(1-C_d) & C_s \leq 0.5 \\
C_d+(2 C_s-1)C_d((16 C_d-12)C_d+3) & C_s \gt 0.5 \text{ and } C_d \leq 0.25 \\
C_d+(2 C_s-1)(\sqrt{C_d}-C_d) & C_s \gt 0.5 \text{ and } C_d \gt 0.25
\end{cases}
\end{aligned}\]
|
|
\[ \begin{aligned}
(X,Y,Z) & = (1,1,1) \\
f(C_s,C_d) & = \lvert C_d-C_s \rvert
\end{aligned}\]
|
|
\[ \begin{aligned}
(X,Y,Z) & = (1,1,1) \\
f(C_s,C_d) & = C_s+C_d-2C_sC_d
\end{aligned}\]
|
|
\[ \begin{aligned}
(X,Y,Z) & = (1,0,1) \\
f(C_s,C_d) & = 1-C_d
\end{aligned}\]
|
|
\[ \begin{aligned}
(X,Y,Z) & = (1,0,1) \\
f(C_s,C_d) & = C_s(1-C_d)
\end{aligned}\]
|
|
\[ \begin{aligned}
(X,Y,Z) & = (1,1,1) \\
f(C_s,C_d) & =
\begin{cases}
C_s+C_d & C_s+C_d \leq 1 \\
1 & \text{otherwise}
\end{cases}
\end{aligned}\]
|
|
\[ \begin{aligned}
(X,Y,Z) & = (1,1,1) \\
f(C_s,C_d) & =
\begin{cases}
C_s+C_d-1 & C_s+C_d \gt 1 \\
0 & \text{otherwise}
\end{cases}
\end{aligned}\]
|
|
\[ \begin{aligned}
(X,Y,Z) & = (1,1,1) \\
f(C_s,C_d) & =
\begin{cases}
1-min(1,\frac{1-C_d}{2C_s}) & 0 \lt C_s \lt 0.5 \\
0 & C_s \leq 0 \\
min(1,\frac{C_d}{2(1-C_s)}) & 0.5 \leq C_s \lt 1 \\
1 & C_s \geq 1
\end{cases}
\end{aligned}\]
|
|
\[ \begin{aligned}
(X,Y,Z) & = (1,1,1) \\
f(C_s,C_d) & =
\begin{cases}
1 & 2C_s+C_d \gt 2 \\
2C_s+C_d-1 & 1 \lt 2C_s+C_d \leq 2 \\
0 & 2C_s+C_d \leq 1
\end{cases}
\end{aligned}\]
|
|
\[ \begin{aligned}
(X,Y,Z) & = (1,1,1) \\
f(C_s,C_d) & =
\begin{cases}
0 & 2C_s-1 \gt C_d \text{ and } C_s \lt 0.5 \\
2C_s-1 & 2C_s-1 \gt C_d \text{ and } C_s \geq 0.5 \\
2C_s & 2C_s-1 \leq C_d \text{ and } C_s \lt 0.5C_d \\
C_d & 2C_s-1 \leq C_d \text{ and } C_s \geq 0.5C_d
\end{cases}
\end{aligned}\]
|
|
\[ \begin{aligned}
(X,Y,Z) & = (1,1,1) \\
f(C_s,C_d) & =
\begin{cases}
0 & C_s+C_d \lt 1 \\
1 & \text{otherwise}
\end{cases}
\end{aligned}\]
|
When using one of the HSL blend operations in table Hue-Saturation-Luminosity Advanced Blend Operations as the blend operation, the RGB color components produced by the function f are effectively obtained by converting both the non-premultiplied source and destination colors to the HSL (hue, saturation, luminosity) color space, generating a new HSL color by selecting H, S, and L components from the source or destination according to the blend operation, and then converting the result back to RGB. In the equations below, a blended RGB color is produced according to the following pseudocode:
float minv3(vec3 c) {
return min(min(c.r, c.g), c.b);
}
float maxv3(vec3 c) {
return max(max(c.r, c.g), c.b);
}
float lumv3(vec3 c) {
return dot(c, vec3(0.30, 0.59, 0.11));
}
float satv3(vec3 c) {
return maxv3(c) - minv3(c);
}
// If any color components are outside [0,1], adjust the color to
// get the components in range.
vec3 ClipColor(vec3 color) {
float lum = lumv3(color);
float mincol = minv3(color);
float maxcol = maxv3(color);
if (mincol < 0.0) {
color = lum + ((color-lum)*lum) / (lum-mincol);
}
if (maxcol > 1.0) {
color = lum + ((color-lum)*lum) / (maxcol-lum);
}
return color;
}
// Take the base RGB color <cbase> and override its luminosity
// with that of the RGB color <clum>.
vec3 SetLum(vec3 cbase, vec3 clum) {
float lbase = lumv3(cbase);
float llum = lumv3(clum);
float ldiff = llum - lbase;
vec3 color = cbase + vec3(ldiff);
return ClipColor(color);
}
// Take the base RGB color <cbase> and override its saturation with
// that of the RGB color <csat>. The override the luminosity of the
// result with that of the RGB color <clum>.
vec3 SetLumSat(vec3 cbase, vec3 csat, vec3 clum)
{
float minbase = minv3(cbase);
float sbase = satv3(cbase);
float ssat = satv3(csat);
vec3 color;
if (sbase > 0) {
// Equivalent (modulo rounding errors) to setting the
// smallest (R,G,B) component to 0, the largest to <ssat>,
// and interpolating the "middle" component based on its
// original value relative to the smallest/largest.
color = (cbase - minbase) * ssat / sbase;
} else {
color = vec3(0.0);
}
return SetLum(color, clum);
}
| Mode | Result |
|---|---|
|
\[ \begin{aligned}
(X,Y,Z) & = (1,1,1) \\
f(C_s,C_d) & = SetLumSat(C_s,C_d,C_d)
\end{aligned}\]
|
|
\[ \begin{aligned}
(X,Y,Z) & = (1,1,1) \\
f(C_s,C_d) & = SetLumSat(C_d,C_s,C_d)
\end{aligned}\]
|
|
\[ \begin{aligned}
(X,Y,Z) & = (1,1,1) \\
f(C_s,C_d) & = SetLum(C_s,C_d)
\end{aligned}\]
|
|
\[ \begin{aligned}
(X,Y,Z) & = (1,1,1) \\
f(C_s,C_d) & = SetLum(C_d,C_s)
\end{aligned}\]
|
When using one of the operations in table
Additional RGB Blend
Operations as the blend operation, the source and destination colors used
by these blending operations are interpreted according to
srcPremultiplied and dstPremultiplied.
The blending operations below are evaluated where the RGB source and
destination color components are both considered to have been premultiplied
by the corresponding A component.
| Mode | Result |
|---|---|
|
\[ \begin{aligned}
(R,G,B,A) = ( & R_s'+R_d', \\
& G_s'+G_d', \\
& B_s'+B_d', \\
& A_s+A_d)
\end{aligned}\]
|
|
\[ \begin{aligned}
(R,G,B,A) = ( & min(1,R_s'+R_d'), \\
& min(1,G_s'+G_d'), \\
& min(1,B_s'+B_d'), \\
& min(1,A_s+A_d))
\end{aligned}\]
|
|
\[ \begin{aligned}
(R,G,B,A) = ( & min(min(1,A_s+A_d),R_s'+R_d'), \\
& min(min(1,A_s+A_d),G_s'+G_d'), \\
& min(min(1,A_s+A_d),B_s'+B_d'), \\
& min(1,A_s+A_d))
\end{aligned}\]
|
|
\[ \begin{aligned}
(R,G,B,A) = ( & max(0,min(1,A_s+A_d)-((A_s-R_s')+(A_d-R_d'))), \\
& max(0,min(1,A_s+A_d)-((A_s-G_s')+(A_d-G_d'))), \\
& max(0,min(1,A_s+A_d)-((A_s-B_s')+(A_d-B_d'))), \\
& min(1,A_s+A_d))
\end{aligned}\]
|
|
\[ \begin{aligned}
(R,G,B,A) = ( & R_d'-R_s', \\
& G_d'-G_s', \\
& B_d'-B_s', \\
& A_d-A_s)
\end{aligned}\]
|
|
\[ \begin{aligned}
(R,G,B,A) = ( & max(0,R_d'-R_s'), \\
& max(0,G_d'-G_s'), \\
& max(0,B_d'-B_s'), \\
& max(0,A_d-A_s))
\end{aligned}\]
|
|
\[ \begin{aligned}
(R,G,B,A) = ( & \frac{A_d}{2} + 2(R_d'-\frac{A_d}{2})(R_s'-\frac{A_s}{2}), \\
& \frac{A_d}{2} + 2(G_d'-\frac{A_d}{2})(G_s'-\frac{A_s}{2}), \\
& \frac{A_d}{2} + 2(B_d'-\frac{A_d}{2})(B_s'-\frac{A_s}{2}), \\
& A_d)
\end{aligned}\]
|
|
\[ \begin{aligned}
(R,G,B,A) = ( & A_s(1-R_d') + (1-A_s)R_d', \\
& A_s(1-G_d') + (1-A_s)G_d', \\
& A_s(1-B_d') + (1-A_s)B_d', \\
& A_s+A_d-A_sA_d)
\end{aligned}\]
|
|
\[ \begin{aligned}
(R,G,B,A) & = (R_s', G_d', B_d', A_d)
\end{aligned}\]
|
|
\[ \begin{aligned}
(R,G,B,A) & = (R_d', G_s', B_d', A_d)
\end{aligned}\]
|
|
\[ \begin{aligned}
(R,G,B,A) & = (R_d', G_d', B_s', A_d)
\end{aligned}\]
|
28.2. Logical Operations
The application can enable a logical operation between the fragment’s color values and the existing value in the framebuffer attachment. This logical operation is applied prior to updating the framebuffer attachment. Logical operations are applied only for signed and unsigned integer and normalized integer framebuffers. Logical operations are not applied to floating-point or sRGB format color attachments.
Logical operations are controlled by the logicOpEnable and
logicOp members of VkPipelineColorBlendStateCreateInfo.
If logicOpEnable is VK_TRUE, then a logical operation selected
by logicOp is applied between each color attachment and the fragment’s
corresponding output value, and blending of all attachments is treated as if
it were disabled.
Any attachments using color formats for which logical operations are not
supported simply pass through the color values unmodified.
The logical operation is applied independently for each of the red, green,
blue, and alpha components.
The logicOp is selected from the following operations:
typedef enum VkLogicOp {
VK_LOGIC_OP_CLEAR = 0,
VK_LOGIC_OP_AND = 1,
VK_LOGIC_OP_AND_REVERSE = 2,
VK_LOGIC_OP_COPY = 3,
VK_LOGIC_OP_AND_INVERTED = 4,
VK_LOGIC_OP_NO_OP = 5,
VK_LOGIC_OP_XOR = 6,
VK_LOGIC_OP_OR = 7,
VK_LOGIC_OP_NOR = 8,
VK_LOGIC_OP_EQUIVALENT = 9,
VK_LOGIC_OP_INVERT = 10,
VK_LOGIC_OP_OR_REVERSE = 11,
VK_LOGIC_OP_COPY_INVERTED = 12,
VK_LOGIC_OP_OR_INVERTED = 13,
VK_LOGIC_OP_NAND = 14,
VK_LOGIC_OP_SET = 15,
VK_LOGIC_OP_MAX_ENUM = 0x7FFFFFFF
} VkLogicOp;
The logical operations supported by Vulkan are summarized in the following table in which
-
¬ is bitwise invert,
-
∧ is bitwise and,
-
∨ is bitwise or,
-
⊕ is bitwise exclusive or,
-
s is the fragment’s Rs0, Gs0, Bs0 or As0 component value for the fragment output corresponding to the color attachment being updated, and
-
d is the color attachment’s R, G, B or A component value:
| Mode | Operation |
|---|---|
|
0 |
|
s ∧ d |
|
s ∧ ¬ d |
|
s |
|
¬ s ∧ d |
|
d |
|
s ⊕ d |
|
s ∨ d |
|
¬ (s ∨ d) |
|
¬ (s ⊕ d) |
|
¬ d |
|
s ∨ ¬ d |
|
¬ s |
|
¬ s ∨ d |
|
¬ (s ∧ d) |
|
all 1s |
The result of the logical operation is then written to the color attachment as controlled by the component write mask, described in Blend Operations.
28.3. Color Write Mask
Bits which can be set in
VkPipelineColorBlendAttachmentState::colorWriteMask to determine
whether the final color values R, G, B and A are written to the
framebuffer attachment are:
typedef enum VkColorComponentFlagBits {
VK_COLOR_COMPONENT_R_BIT = 0x00000001,
VK_COLOR_COMPONENT_G_BIT = 0x00000002,
VK_COLOR_COMPONENT_B_BIT = 0x00000004,
VK_COLOR_COMPONENT_A_BIT = 0x00000008,
VK_COLOR_COMPONENT_FLAG_BITS_MAX_ENUM = 0x7FFFFFFF
} VkColorComponentFlagBits;
-
VK_COLOR_COMPONENT_R_BITspecifies that the R value is written to the color attachment for the appropriate sample. Otherwise, the value in memory is unmodified. -
VK_COLOR_COMPONENT_G_BITspecifies that the G value is written to the color attachment for the appropriate sample. Otherwise, the value in memory is unmodified. -
VK_COLOR_COMPONENT_B_BITspecifies that the B value is written to the color attachment for the appropriate sample. Otherwise, the value in memory is unmodified. -
VK_COLOR_COMPONENT_A_BITspecifies that the A value is written to the color attachment for the appropriate sample. Otherwise, the value in memory is unmodified.
The color write mask operation is applied regardless of whether blending is enabled.
typedef VkFlags VkColorComponentFlags;
VkColorComponentFlags is a bitmask type for setting a mask of zero or
more VkColorComponentFlagBits.