Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dual Source Blending #1804

Closed
wez opened this issue Aug 14, 2021 · 4 comments · Fixed by #4022
Closed

Dual Source Blending #1804

wez opened this issue Aug 14, 2021 · 4 comments · Fixed by #4022
Labels
area: api Issues related to API surface type: enhancement New feature or request

Comments

@wez
Copy link
Contributor

wez commented Aug 14, 2021

I'd like to be able to use "Dual Source Blending" with wgpu, so that I can perform sub-pixel-AA with alpha blending.

I can see some mention of whether the Metal backend supports this in the code, but I don't see anything more than this so far.
I think that what I'd like to see for this is for BlendFactor to have enum variants representing the second source, and a way to reference that in the shader program.

Is this a lot of work to plumb in and get implemented?

For my purposes, I'm most interested in using wgpu as a way to run against Metal and DX backends as I already have an OpenGL implementation of my renderer.

@cwfitzgerald cwfitzgerald added area: api Issues related to API surface type: enhancement New feature or request labels Aug 15, 2021
@kvark
Copy link
Member

kvark commented Aug 16, 2021

This is desired as a feature. See also - gpuweb/gpuweb#391 upstream.

@freqmod
Copy link
Contributor

freqmod commented Aug 3, 2023

Hi, i did an attempt at this over at https://github.com/freqmod/wgpu/tree/alt_blend / https://github.com/freqmod/naga/tree/alt_blend . Currently it is working for metal and vulkan.
I guess the API / shader language could be discussed, but at least it is a starting point.

I can submit a PR and help getting this merged, if there are interest to accept the current API or work to make a suitable one.

For vulkan the feature must be enabled for the functionality to work without warnings:

            features: wgpu::Features::default().union(wgpu::Features::BLEND_FUNC_EXTENDED),

Attached is a simple attempt at a fragment shader for rgb subpixel font rendering. It is still a bit wrong, but it shows how to write to the secondary output for blending (inspired by description in https://github.com/servo/webrender/blob/master/webrender/doc/text-rendering.md ),

struct Output {
    @location(0) color: vec4<f32>,
};

@group(0)@binding(1)
var atlas_sampler: sampler;

@group(0)@binding(2)
var atlas_texture: texture_2d<f32>;

@group(0)@binding(3)
var palette_sampler: sampler;

@group(0)@binding(4)
var palette_texture: texture_2d<f32>;

struct VertexOutput {
    @location(0) t_position: vec2<f32>,
    @location(1) c_position: vec2<f32>,
    @builtin(position) position: vec4<f32>,
};

struct FragmentOutput{
    @location(0) color: vec4<f32>,
    @location(0,1) mask: vec4<f32>,
}
@fragment
fn main(vo: VertexOutput) -> FragmentOutput {
    var font_col = textureSample(atlas_texture, atlas_sampler, vo.t_position);
    var palette_col = textureSample(palette_texture, palette_sampler, vec2<f32>(vo.c_position.x, 1.0));
    var a = font_col.x + font_col.y + font_col.z;
    var color = vec4(
        font_col.b * palette_col.b,
        font_col.g * palette_col.g,
        font_col.r * palette_col.r,
        a * palette_col.a);
    var mask = vec4(1.0,1.0,1.0,1.0);
    return FragmentOutput(color, mask);
}

and the target descriptor:

        fragment: Some(wgpu::FragmentState {
            module: text_fs_module,
            entry_point: "main",
            targets: &[Some(wgpu::ColorTargetState {
                format: wgpu::TextureFormat::Bgra8UnormSrgb,
                blend: Some(wgpu::BlendState {
                    color: wgpu::BlendComponent {
                        src_factor: wgpu::BlendFactor::Src,
                        dst_factor: wgpu::BlendFactor::OneMinusSrc1,
                        operation: wgpu::BlendOperation::Add,
                    },
                    alpha: wgpu::BlendComponent {
                        src_factor: wgpu::BlendFactor::Src,
                        dst_factor: wgpu::BlendFactor::OneMinusSrc1Alpha,
                        operation: wgpu::BlendOperation::Add,
                    },
                }),
                write_mask: wgpu::ColorWrites::ALL,
            })],
        }),

@teoxoy
Copy link
Member

teoxoy commented Aug 8, 2023

PRs as a next step would be great! I have a couple of comments but it would be easier to leave them on a PR.

@freqmod
Copy link
Contributor

freqmod commented Aug 9, 2023

PRs as a next step would be great! I have a couple of comments but it would be easier to leave them on a PR.

Great. See
gfx-rs/naga#2427
and
#4022

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: api Issues related to API surface type: enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants