将片段着色器应用于图像

发布于 2025-01-15 11:19:07 字数 2379 浏览 3 评论 0原文

我的目标是将用 SKSL 编写的任何着色器应用于图像。我知道此功能在 Skia 中可用,但在 SkiaSharp 中不可用,但我发现最近有人在移植该功能方面做出了一些努力。我一直在尝试复制其中一位贡献者在这里制作的预发布版本之一的解决方案:https://github.com/mono/SkiaSharp/issues/1319#issuecomment-640529824 但我不得不对我的代码和着色器进行一些更改,因为它们似乎与最新的 pre 不兼容-发布版本(2.88.0-preview.232)。这是我的供参考:

            // input values
            SKBitmap bmp = new SKBitmap(1000, 1000);
            SKCanvas canvas = new SKCanvas(bmp);
            float threshold = 1.05f;
            float exponent = 1.5f;

            // shader
            var src = @"
                in fragmentProcessor color_map;

                uniform float scale;
                uniform half exp;
                uniform float3 in_colors0;

                half4 main(float2 p) {
                    half4 texColor = sample(color_map, p);
                    if (length(abs(in_colors0 - pow(texColor.rgb, half3(exp)))) < scale)
                        discard;
                    return texColor;
                }";
            using var effect = SKRuntimeEffect.Create(src, out var errorText);

            // input values
            var inputs = new SKRuntimeEffectUniforms(effect);
            inputs["scale"] = threshold;
            inputs["exp"] = exponent;
            inputs["in_colors0"] = new[] {1f, 1f, 1f};

            // shader values
            using var blueShirt = SKImage.FromEncodedData(Path.Combine(PathToImages, "blue-shirt.jpg"));
            using var textureShader = blueShirt.ToShader();
            var children = new SKRuntimeEffectChildren(effect);
            children["color_map"] = textureShader;

            // create actual shader
            using var shader = effect.ToShader(true, inputs, children);

            // draw as normal
            canvas.Clear(SKColors.Black);
            using var paint = new SKPaint { Shader = shader };
            canvas.DrawRect(SKRect.Create(400, 400), paint);

但是,我在 canvas.DrawRect() 上收到一个 System.Runtime.InteropServices.SEHException: 'External component has throw an exception.'相信要么是我的着色器有问题,要么这个功能尚未完全支持。除了使用 C# 手动修改单个像素(这应该比着色器操作慢很多)之外,后者在 SkiaSharp 的当前领域内不可能吗?如果没有,我想我需要将图像加载到一些具有 OpenGL 绑定的库(如 OpenTK)中,应用着色器,截取画布的屏幕截图,然后将该图像加载到 SkiaSharp 中,我认为这不是一个很好的方法解决方案。我希望看到甚至在 Skia 之外的可能解决方案。

My goal is to apply any shader written in SKSL to an image. I am aware that this feature is available in Skia but not in SkiaSharp, but I see that there has been some effort recently to port the functionality. I've been trying to replicate a solution for one of the pre-release versions that one of the contributors made here: https://github.com/mono/SkiaSharp/issues/1319#issuecomment-640529824 but I had to make a few changes to my code and shader as they weren't seemingly compatible with the latest pre-release version (2.88.0-preview.232). Here is mine for reference:

            // input values
            SKBitmap bmp = new SKBitmap(1000, 1000);
            SKCanvas canvas = new SKCanvas(bmp);
            float threshold = 1.05f;
            float exponent = 1.5f;

            // shader
            var src = @"
                in fragmentProcessor color_map;

                uniform float scale;
                uniform half exp;
                uniform float3 in_colors0;

                half4 main(float2 p) {
                    half4 texColor = sample(color_map, p);
                    if (length(abs(in_colors0 - pow(texColor.rgb, half3(exp)))) < scale)
                        discard;
                    return texColor;
                }";
            using var effect = SKRuntimeEffect.Create(src, out var errorText);

            // input values
            var inputs = new SKRuntimeEffectUniforms(effect);
            inputs["scale"] = threshold;
            inputs["exp"] = exponent;
            inputs["in_colors0"] = new[] {1f, 1f, 1f};

            // shader values
            using var blueShirt = SKImage.FromEncodedData(Path.Combine(PathToImages, "blue-shirt.jpg"));
            using var textureShader = blueShirt.ToShader();
            var children = new SKRuntimeEffectChildren(effect);
            children["color_map"] = textureShader;

            // create actual shader
            using var shader = effect.ToShader(true, inputs, children);

            // draw as normal
            canvas.Clear(SKColors.Black);
            using var paint = new SKPaint { Shader = shader };
            canvas.DrawRect(SKRect.Create(400, 400), paint);

However, I get a System.Runtime.InteropServices.SEHException: 'External component has thrown an exception.' on canvas.DrawRect() which I believe is either a problem with my shader or that this feature is not yet fully supported. Besides manually modifying individual pixels with C# which should be a lot slower than a shader operation, is the latter not possible within the current realm of SkiaSharp? If not, I think I'll be needing to load an image into some library with OpenGL bindings (like OpenTK), apply the shader, take a screenshot of the canvas, then load that image into SkiaSharp, which I imagine is not a great solution. I'd like to see possible solutions even outside of Skia.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文