How to enforce Skia to use float texture rather than half?

48 views
Skip to first unread message

程洋

unread,
May 31, 2024, 11:10:16 AMMay 31
to skia-discuss
When I attempted to port a Metal shader from iOS, I tried rewriting the shader in SKSL and running it on iOS. The program compiles and runs, but there are some precision issues. After debugging with the Metal Debugger, I discovered that the problem arises because SKSL uses half for textures and samplers by default. Is there a way to make SKSL use float for textures and samplers when it ultimately compiles to iOS's MSL?




---------------------------------------------------------------------------------------------------------------------------------------------
My SKSL looks like:
```

            uniform shader child;

            uniform float scale;

            uniform float imageWH[2];


            float4 srgbToLinear(float4 coord) {

                       ......

                       float4 color = child.eval(coord)

```
And the result in debugger:
```
kernel float4

srgbToLinear(texture2d<half, access::sample>  inTexture xxxx

        .......

        float4 p0 = inTexture.sample(smp, coord)


```

John Stiles

unread,
May 31, 2024, 11:16:33 AMMay 31
to skia-discuss
Unfortunately, SkSL doesn't directly support high precision texture2D sampling.
However, we do offer a lot of built-in SkSL color management features, including sRGB<->linear conversion, which are very accurate.
Please take a look at the docs, e.g. https://skia.org/docs/user/sksl/#raw-image-shaders  

I suspect your issue might be more subtle than just "the precision isn't good enough" because a pixel's color components are generally only 8 or 10 bits, and a half is perfectly capable of handling this with extra headroom to spare. It's more likely that something else more subtle is happening to cause a problem.

程洋

unread,
May 31, 2024, 1:37:22 PMMay 31
to skia-discuss

In fact, I am trying to rewrite the Lanczos scaling algorithm from iOS in SKIA. However, after running it, we found that most pixel RGB values have a difference of 1-2. It's not particularly critical, but we still want to see if there is a way to fix this issue.

John Stiles

unread,
May 31, 2024, 1:45:03 PMMay 31
to skia-discuss
No, Skia doesn't support float sampling.
Unless your input textures are 32-bit float per channel, I highly doubt this is the root cause of your difference. If you have a standard 8-bits per channel RGBA input image, then using half to sample the texture is definitely as precise as you need. You can just immediately cast the value to float if you want to work in float.
If you are trying to get one GPU to perfectly, identically pixel-match values from another GPU, we have found that this is extraordinarily difficult and borders on impossible. In fact, some GPUs are not even internally self-consistent, and will draw the same image with slight differences from run to run of our test suite. A difference of 1-2 in a handful of pixels is accepted as "visually identical" for Skia's purposes. 

程洋

unread,
May 31, 2024, 2:21:17 PMMay 31
to skia-discuss
Sound reasonable. Thank you very much
Reply all
Reply to author
Forward
0 new messages