Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

Inquiry About Using GL_EXT_shader_framebuffer_fetch in Skia

63 views
Skip to first unread message

程洋

unread,
Feb 7, 2025, 10:02:12 PMFeb 7
to skia-discuss

I am currently working with Skia and need to implement a blending shader that modifies the color at a given (x, y) coordinate based on the original color at that location. Ideally, I would like to use GL_EXT_shader_framebuffer_fetch to access the existing framebuffer color directly within the fragment shader.


I am aware that an alternative approach would be to use surface->makeImageSnapshot() to create a sampler, but this method is significantly slow and introduces high overhead. Therefore, I am looking for a more efficient way to achieve this within Skia.


Does Skia support GL_EXT_shader_framebuffer_fetch, or is there an alternative method that can efficiently provide access to the previous framebuffer color?


This is my code below:
```
    float rectPos[2] = {float(rect.fLeft), float(rect.fTop)};

    // 创建 shader,并应用在 canvas 上
    SkSamplingOptions linear(SkFilterMode::kLinear, SkMipmapMode::kNone);

    SkRuntimeShaderBuilder TmuaBuild(mTmuaEffect);
    TmuaBuild.child("blurBackground") = downSampleBlurImage->makeShader(SkTileMode::kClamp, SkTileMode::kClamp, linear, nullptr);
    TmuaBuild.child("number") = number->makeShader(SkTileMode::kClamp, SkTileMode::kClamp, linear, nullptr);
    TmuaBuild.uniform("rectPos").set(rectPos, 2);
    // TmuaBuild.uniform("scaleArg") = SkV4{1.0f / scale, 1.0f, 1.0f, 0.0f};

// 绘制 shader 到 canvas
    paint.setShader(TmuaBuild.makeShader());

    canvas->drawRect(rect, paint);


   sk_sp<SkImage> background = surface->makeImageSnapshot();

   SkRuntimeShaderBuilder TvcmEffectBuild(mTvcmEffect);
   TvcmEffectBuild.child("background") = background->makeShader(SkTileMode::kClamp, SkTileMode::kClamp, linear, nullptr);
   TvcmEffectBuild.child("number") = number->makeShader(SkTileMode::kClamp, SkTileMode::kClamp, linear, nullptr);
   TvcmEffectBuild.uniform("rectPos").set(rectPos, 2);

// 绘制 shader 到 canvas
   paint.setShader(TvcmEffectBuild.makeShader());

   canvas->drawRect(rect, paint);

```

br...@pentrek.com

unread,
Feb 8, 2025, 6:54:05 AMFeb 8
to skia-d...@googlegroups.com, skia-discuss
If you make a SkBlender instead of SkShader (using SkSL) and the GPU supports framebuffer fetch then the generated code that calls your function will use the extension to provide the dst argument.

On Feb 7, 2025, at 10:02 PM, 程洋 <d171...@gmail.com> wrote:


--
You received this message because you are subscribed to the Google Groups "skia-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to skia-discuss...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/skia-discuss/bbad895b-c5c9-42c4-baa1-6ed76fed1b3en%40googlegroups.com.

程洋

unread,
Feb 9, 2025, 10:36:09 PMFeb 9
to skia-discuss
That works! Thank you very much.
BTW is it possible for skia custom blender to blend multiple texture? It seems that only 1 `dst` argument is passed into sksl shader

Brian Salomon

unread,
Feb 10, 2025, 10:27:47 AMFeb 10
to skia-d...@googlegroups.com
The dst color always represents the thing you are drawing on top of and there is only ever one (Skia doesn’t have an equivalent to multiple-rendertarget). If you want to access additional textures probably the best way is to also supply a custom (SkSL) SkShader that reads the textures. The output of the SkShader will become the src input to the SkBlender (there is also SkColorFilter in between, but typically that is null/passthrough). If you need the value from the texture to really be part of the blend calculation in a way that can't be factored into just affecting the src input, then I don’t think there is a way do that. SkBlenders don’t get coordinates (by design) and so you wouldn’t be able to access different samples from the texture for different runs of the fragment shader.

Brian 

程洋

unread,
Feb 10, 2025, 8:50:10 PMFeb 10
to skia-discuss
Thank you very much for your help
Reply all
Reply to author
Forward
0 new messages