Skip to content

Commit

Permalink
Add support for Dual Source Blending
Browse files Browse the repository at this point in the history
I'm no expert, but this seems to allow using dual source blending ala:

```
let alpha_blending = glium::DrawParameters {
    blend: glium::Blend {
        color: BlendingFunction::Addition {
            source: LinearBlendingFactor::SourceOneColor,
            destination: LinearBlendingFactor::OneMinusSourceOneColor,
        },
        alpha: BlendingFunction::Addition {
            source: LinearBlendingFactor::SourceOneColor,
            destination: LinearBlendingFactor::OneMinusSourceOneColor,
        },
        constant_value: (0.0, 0.0, 0.0, 0.0),
    },

    ..Default::default()
};
```

and this in the shader:

```
// The color + alpha
layout(location=0, index=0) out vec4 color;
// Individual alpha channels for RGBA in color, used for subpixel
// antialiasing blending
layout(location=0, index=1) out vec4 colorMask;
```

based on my reading of:
https://stackoverflow.com/questions/48491340/use-rgb-texture-as-alpha-values-subpixel-font-rendering-in-opengl
https://www.khronos.org/registry/OpenGL/extensions/ARB/ARB_blend_func_extended.txt

refs: wez/wezterm#932
  • Loading branch information
wez committed Aug 10, 2021
1 parent 2c6caf9 commit aed9527
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions build/main.rs
Expand Up @@ -30,6 +30,7 @@ where
"GL_AMD_depth_clamp_separate",
"GL_APPLE_vertex_array_object",
"GL_ARB_bindless_texture",
"GL_ARB_blend_func_extended",
"GL_ARB_buffer_storage",
"GL_ARB_compute_shader",
"GL_ARB_copy_buffer",
Expand Down
21 changes: 21 additions & 0 deletions src/draw_parameters/blend.rs
Expand Up @@ -178,6 +178,22 @@ pub enum LinearBlendingFactor {
/// Multiply the source or destination component by `1.0` minus the alpha value of
/// `Blend::const_value`.
OneMinusConstantAlpha,

/// Multiply the source or destination component by its corresponding value
/// in source index one (you need to explicitly specify `layout(location=0, index=1)`
/// to bind it in your shader.
/// This is useful in Dual Source Blending
/// <https://www.khronos.org/registry/OpenGL/extensions/ARB/ARB_blend_func_extended.txt>
SourceOneColor,

/// Equivalent to `1 - SourceOneColor`.
OneMinusSourceOneColor,

/// Multiply the source or destination component by the alpha value of source index 1.
SourceOneAlpha,

/// Multiply the source or destination component by `1.0` minus the alpha value of source index 1.
OneMinusSourceOneAlpha,
}

impl LinearBlendingFactor {
Expand All @@ -198,6 +214,11 @@ impl LinearBlendingFactor {
LinearBlendingFactor::OneMinusConstantColor => gl::ONE_MINUS_CONSTANT_COLOR,
LinearBlendingFactor::ConstantAlpha => gl::CONSTANT_ALPHA,
LinearBlendingFactor::OneMinusConstantAlpha => gl::ONE_MINUS_CONSTANT_ALPHA,
LinearBlendingFactor::SourceOneColor => gl::SRC1_COLOR,
LinearBlendingFactor::OneMinusSourceOneColor => gl::ONE_MINUS_SRC1_COLOR,
LinearBlendingFactor::SourceOneAlpha => gl::SRC1_ALPHA,
LinearBlendingFactor::OneMinusSourceOneAlpha => gl::ONE_MINUS_SRC1_ALPHA,

}
}
}
Expand Down

0 comments on commit aed9527

Please sign in to comment.