关于GL_SRC_ALPHA缩放因子及kA计算的技术疑问
Great questions about the nitty-gritty of OpenGL fixed-function blending—let's break each part down clearly:
1. What are bitplanes?
Bitplanes refer to the individual binary bits that make up the storage for a color (R/G/B) or alpha channel. Back when OpenGL relied heavily on fixed-function hardware, these channels were stored as integers with a fixed number of bits of precision.
For example:
- An 8-bit alpha channel has 8 bitplanes (one for each bit in the 0-255 integer range).
- Each bitplane represents a single binary digit that contributes to the overall value of the channel. So if an alpha channel has
m_cbitplanes, that means it's stored withm_cbits of precision, allowing integer values from 0 to2^m_c - 1.
2. Why is kA calculated using the bitplane count?
In fixed-function OpenGL, hardware stored color/alpha values as integers (not floating-point) for performance reasons. The kA factor is defined as kA = 1/(2^m_c - 1), where m_c is the number of bitplanes for the alpha channel.
Here's why this formula matters:
- It converts the integer alpha value
A_s0(which ranges from 0 to2^m_c -1) into a normalized floating-point value between 0.0 and 1.0. - Blending calculations depend on proportional weights (how much source color mixes with target color) that need to be in the [0,1] range to work correctly. Without this normalization, an 8-bit alpha value of 255 would act as a weight of 255 instead of 1.0, which would completely blow out the final color.
3. What's kA's role when using GL_SRC_ALPHA + GL_ONE_MINUS_SRC_ALPHA?
When you use this blending pair, the core equation is:
final_color = (source_color * (A_s0 * kA)) + (destination_color * (1 - A_s0 * kA))
This is linear interpolation between source and destination colors, weighted by the source alpha.
- When kA=1: This means the source alpha
A_s0is already in the [0,1] floating-point range (common with modern floating-point framebuffers or textures). The weight becomes justA_s0, so the interpolation is direct—e.g., an alpha of 0.5 gives a perfect 50/50 mix of source and destination. - When kA < 1: This is the classic integer channel scenario (like 8-bit alpha). kA normalizes the integer
A_s0to the [0,1] range, ensuring the interpolation behaves as intended. For example, an 8-bit alpha value of 128 becomes128 * (1/255) ≈ 0.5, resulting in that same 50/50 mix.
In short, kA acts as a bridge between how alpha values are stored (integer vs. floating-point) and how blending needs to use them—ensuring the weight is always in the correct range for smooth, predictable linear interpolation.
内容的提问来源于stack exchange,提问作者haykoandri

