Re: Understanding coordinates in SKSL

199 views
Skip to first unread message

Brian Salomon

unread,
Oct 10, 2022, 12:31:09 PM10/10/22
to skia-d...@googlegroups.com
The coordinates that get passed to the main() function of a SkShader snapped from SkRuntimeEffect are in the same space as the geometry you're drawing. So if you drew a surface filling rect you should get:

(0.5, 0.5), (1.5, 0.5), (2.5, 0.5), ...
(0.5, 1.5), (1.5, 1.5), (2.5, 1.5), ...
(0.5, 2.5), (1.5, 2.5), (2.5, 2.5), ...

You might have better luck figuring out what's going on by entering your shader into https://shaders.skia.org/ and hitting "Debug" instead of using readbacks. You can inspect variable values during shader execution.

We don't offer the equivalent of fragment coordinates like gl_FragCoord for two reasons:

1) Skia can redirect your draw calls to offscreen surfaces for many different reasons, image filtering, saveLayer being two notable cases. Given the rest of our API it would only make sense if the coordinates seen by the SkShader reflected where the output will land in the final SkSurface. Otherwise the inserting  something like saveLayer()/restore() into a drawing sequence could change how things get shaded, which would produce surprising results. Getting all this plumbing correct is a lot of effort and there hasn't been an overwhelming demand for it.

2) Many of the important embedders of Skia (who effectively "pay" for Skia's development) assume that a drawing sequence's result is independent of where on the screen it falls and so they do things like cache rasterizations and then translate the result during a scroll. If what gets drawn is dependent on where on the screen it falls, that would imply a lot of invalidation/redraw logic adjustments in the calling code to Skia.

Brian

On Mon, Oct 10, 2022 at 8:19 AM Alexander Vestin <al...@clayeditor.com> wrote:
Hello, 

I've been trying to a RuntimeEffect with SKSL to produce an index of what pixel,
something similar to 
`int index = gl_FragCoord.x + gl_FragCoord.y * u_size.x`

the issue I'm getting is that the coordinates passed into the main function isn't (or doesn't seem to be) scaled to [0, size]

```
float4 main(float2 c) {
return float4(c.x / 1280.0);
}
```
when read back prints as

``` 

0: 0 1 1 1 

4: 2 2 2 3 

...

```

Where I'd expect it to increment 0, 1, 2, 3 and so on. There isn't any transforms applied to the canvas (also added a resetMatrix before the draw to make sure). It seems the width is scaled by 256, so maybe there's some default values being applied, or am I missing setting the size somewhere? I'm using an 1280x720 8bit surface (with kR8_unorm_SkColorType), if that makes any difference

Any ideas on what's going awry?

the code used: https://pastebin.com/vHG1jUtY

--
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/803f3f04-970a-4598-8cef-b10b63f7924cn%40googlegroups.com.


--

Brian Salomon | Office Hours: go/bsalomon-office | bsal...@google.com
Reply all
Reply to author
Forward
0 new messages