shaders in m85

15 views
Skip to first unread message

Shawn Riordan

unread,
Sep 20, 2022, 12:35:54 PM (4 days ago) Sep 20
to skia-discuss
Before SkRuntimeEffect and SkRuntimeShaderBuilder was it possible to write your own fragment shader?  This is back in milestone 85.

Brian Osman

unread,
Sep 20, 2022, 12:56:14 PM (4 days ago) Sep 20
to skia-d...@googlegroups.com
Those APIs were the first time that we exposed custom fragment shading, but they were available in m85: https://skia.googlesource.com/skia/+/refs/heads/chrome/m85/include/effects/SkRuntimeEffect.h

On Tue, Sep 20, 2022 at 12:35 PM 'Shawn Riordan' via skia-discuss <skia-d...@googlegroups.com> wrote:
Before SkRuntimeEffect and SkRuntimeShaderBuilder was it possible to write your own fragment shader?  This is back in milestone 85.

--
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 on the web visit https://groups.google.com/d/msgid/skia-discuss/36495c18-107e-400e-807f-a89131a97164n%40googlegroups.com.

craste...@gmail.com

unread,
Sep 20, 2022, 1:12:47 PM (4 days ago) Sep 20
to skia-discuss
Thanks, I will give it a try

Shawn Riordan

unread,
Sep 21, 2022, 11:38:39 AM (3 days ago) Sep 21
to skia-discuss
When I compile this:

const char* sksl =
            "layout(color) uniform vec4 color;"
            "uniform shader image;"

            "half4 main(float2 coord)"
            "{"
               "float inv_alpha = 1 - image.eval(coord).x;" // using 'x' since this is a single channel image
               "float alpha = color.a * (1 - (inv_alpha * inv_alpha));"
               "return half4(color.r * alpha,"
                            "color.g * alpha,"
                            "color.b * alpha,"
                            "alpha);"
            "}";

I get the errors:
error: 1: 'color' is not a valid layout qualifier
error: 1: no type named 'vec4'
2 errors

So, vec4 didn't exist in 85?
What were they called?

Brian Osman

unread,
Sep 21, 2022, 11:46:46 AM (3 days ago) Sep 21
to skia-d...@googlegroups.com
Right. m85 pre-dates many of the changes we made to syntax, in anticipation of launching as AGSL. (That included things like GLSL `vec` type aliases, a better convention for main signatures, better color management, etc.). Regardless, you can find examples in the m85 branch: https://skia.googlesource.com/skia/+/refs/heads/chrome/m85/gm/runtimeshader.cpp#177

In particular, vec4 is basically float4 (SkSL internally always uses either floatX or halfX to denote vectors ... the vecX types are just aliased to the floatX ones to make it easier when coming from GLSL). Also note that we still used an inout color parameter (rather than returning the color). The layout(color) qualifier is a newer name/feature, as well - you should be able to use layout(srgb_unpremul) to get the same basic feature back then.

I will say that things were definitely rougher if you're this far back. We've fixed quite a few bugs in both the SkSL compiler, and in how runtime effects interoperate with the rest of the SkPaint since then.

Shawn Riordan

unread,
Sep 21, 2022, 12:31:16 PM (3 days ago) Sep 21
to skia-discuss
const char* sksl =
            "uniform float4 in_color;"
            "in shader input;"

            "void main(float2 xy, inout half4 color)"
            "{"
               "float inv_alpha = 1 - sample(input, xy);"
               "float alpha = in_color.a * (1 - (inv_alpha * inv_alpha));"
               "color.r = in_color.r * alpha;"
               "color.g = in_color.g * alpha;"
               "color.b = in_color.b * alpha;"
               "color.a = alpha;"
            "}";

error: 1: expected 'float', but found 'half4'
1 error

So, half4 only appears in the main function parameter list. why was it expecting float?

Brian Osman

unread,
Sep 21, 2022, 12:38:39 PM (3 days ago) Sep 21
to skia-d...@googlegroups.com
sample() returns a half4 ... you're probably just missing a `.a` swizzle on the results of the sample call.

Brian Salomon

unread,
Sep 21, 2022, 1:20:31 PM (3 days ago) Sep 21
to skia-d...@googlegroups.com
Maybe the confusion is around line number 1 in the error message? I think that's because that is really a single line string in C++. You need \n at the end of the lines or have to use R"()" style strings to get useful line numbers.



--

Brian Salomon | Office Hours: go/bsalomon-office | bsal...@google.com

craste...@gmail.com

unread,
Sep 21, 2022, 1:31:08 PM (3 days ago) Sep 21
to skia-discuss
Thank you kind sir.  That worked
Reply all
Reply to author
Forward
0 new messages