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

[BUG] SKSL doesn't recognize 'smoothstep', or 'fwidth' (and other OpenGL operators) #2649

Closed
1 task done
najak3d opened this issue Oct 16, 2023 · 11 comments
Closed
1 task done

Comments

@najak3d
Copy link

najak3d commented Oct 16, 2023

Description

We are needing SkiaSharp's SKSL to provide better OpenGL shader support - namely 'smoothstep' and 'fwidth'.

Both of these operators (built-in methods) result in this shader compiler error:

"error: 48: unknown identifier 'smoothstep'"

And the shader won't compile.

NOTE: We're using latest Skia 2.88.6 (not .3 as shown below, as it didn't give me the .6 option)

NOTE#2: If we can't get Skia to do these operations, our fallback is to do something more complex where we do it using OpenTK/OpenGL - with a RenderTarget that hopefully we can share back with SKIA, where we can Blit our OpenTK/GL result onto an SKGLSurface in some efficient manner.

Code

Our actual shader is far more involved, but the following very simple shader code triggers the exact same error:

float4 main(float2 fragCoord) {
	float red = smoothstep(0.3, 0.7, fragCoord.x);   // <== FAILS 'smoothstep'
	float blue = fwidth(fragCoord.x);                      // <== ALSO FAILS 'fwidth'
	float4 color = float4(red, 0.0, blue, 1.0);
	return color;
}

Then in C#, our code looks like this: (where 'source' string is equal to the SKSL code above)

	_shaderEffect = SKRuntimeEffect.Create(source, out string error);

Error returns as "error: 48: unknown identifier 'smoothstep'"

Expected Behavior

I was sure hoping that SKSL would support these very useful and commonly used operators - smoothstep and fwidth, and that we could see the same effects here as we were producing using OpenGL, in a 3D view context.

Actual Behavior

Shader won't compile, and therefore does not run at all.

Version of SkiaSharp

2.88.3 (Current)

Last Known Good Version of SkiaSharp

Other (Please indicate in the description)

IDE / Editor

Visual Studio (Windows)

Platform / Operating System

Windows

Platform / Operating System Version

Window 11. Latest Home Edition

Devices

Dell XPS PC

Relevant Screenshots

No response

Relevant Log Output

No response

Code of Conduct

  • I agree to follow this project's Code of Conduct
@najak3d
Copy link
Author

najak3d commented Oct 16, 2023

Looking more, it looks like you may have fixed this in #2547.
#2547

But I don't know how to access this fix. Is there a way I can get a pre-release of 3.0. It's not showing up as an option under Nuget "pre-releases", so I'm unable to validate this fix, assuming it's included in 3.0 already.

@angelofb
Copy link

add this line to your nuget.config and enable prerelease

<add key="skiasharp-eap" value="https://aka.ms/skiasharp-eap/index.json" />

@najak3d
Copy link
Author

najak3d commented Oct 19, 2023

For now, we are omitting the feature that requires 'fwidth' (i.e. elevation contour lines on the map), and have simulated, well-enough, the 'smoothstep' with our own method:

float smoothstep(float min, float max, float val) {
return (val <= min) ? 0.0 : (val >= max) ? 1.0 : ((val - min) / (max - min));
}

So we're less desperate for this currently. I think we may wait for the first official "Pre-release" of 3.0 to test out the 'fwidth'.

Since the 3.0 Shader support API has changed, it wouldn't be a trivial "trial" for us -- we'd actually have to change code to test it, but since we have to release our next release to production in 2 months, we can't use 3.0 for that.

We have 3-4 releases per year, and are tentatively planning to incorporate SkiaSharp 3.0 with 'fwidth' (contour lines) and the actual 'smoothstep' by our July 2024 release next year.

@taublast
Copy link
Contributor

taublast commented Oct 21, 2023

maybe


 float smoothstep(float a, float b, float x) {
        float t = clamp((x - a) / (b - a), 0.0, 1.0);
        return t * t * (3.0 - 2.0 * t);
    }

@najak3d
Copy link
Author

najak3d commented Oct 21, 2023

maybe


 float smoothstep(float a, float b, float x) {
        float t = clamp((x - a) / (b - a), 0.0, 1.0);
        return t * t * (3.0 - 2.0 * t);
    }

Thank you for this!

@najak3d
Copy link
Author

najak3d commented Nov 18, 2023

add this line to your nuget.config and enable prerelease

<add key="skiasharp-eap" value="https://aka.ms/skiasharp-eap/index.json" />

@angelofb :
I tried for about an hour to get this working... but no dice. (I went to every possible location for the Nuget.config.)

In Visual Studio 2022, Nuget still won't show me the Alpha release option for SkiaSharp. I have "pre-releases" checkbox checked in VStudio 2022, and can see the "pre-releases" fine, but 2.88 is still the latest it shows in Visual Studio 2022.

Is there some other way to get the 3.0 alpha? Or got any tips on how to make Visual Studio show it?

@taublast
Copy link
Contributor

Have you checked the "Include prerelease" option? Also make sure package source is not selected to show "nuget.org" only.

@najak3d
Copy link
Author

najak3d commented Nov 19, 2023

Have you checked the "Include prerelease" option? Also make sure package source is not selected to show "nuget.org" only.

THANKS!! That worked -- I was set to "nuget.org" -- I've never noticed that setting before. I changed it to "skiasharp-eap" and now I can see the alpha releases. I'll be trying those out very soon.

image

@mattleibow
Copy link
Contributor

Not sure if this issue is now resolved in v3?

@ghost
Copy link

ghost commented Jan 15, 2024

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment. If it is closed, feel free to comment when you are able to provide the additional information and we will re-investigate.

@najak3d
Copy link
Author

najak3d commented Jan 15, 2024

Not sure if this issue is now resolved in v3?

I'm not sure yet either. Is it supposedly resolved in v3? I've been hoping to push off validation of this until v3 reaches Beta, rather than while it's still in Alpha.

@ghost ghost closed this as completed Jan 18, 2024
@ghost ghost locked as resolved and limited conversation to collaborators Feb 17, 2024
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants