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] Shadders with SkiaSharp.Views.WPF Dont Work #2536

Closed
Kuumba123 opened this issue Jul 26, 2023 · 3 comments
Closed

[BUG] Shadders with SkiaSharp.Views.WPF Dont Work #2536

Kuumba123 opened this issue Jul 26, 2023 · 3 comments

Comments

@Kuumba123
Copy link

Kuumba123 commented Jul 26, 2023

Description

Calling a Drawing function while using an SKPAINT brings up an System.Runtime.InteropServices.SEHException and of course ends the program. My code is based off an example I found someone in this repro (Im not super used to graphics API but I really need shadders for my next project and WPF's shadders are really hard to work when it comes to my low level projects) . Also I am using SkiaSharp.Views.WPF so that I can easlily use SkiSharp with WPF . That might be related to the problem but there wasn't much xaml code to begin with

Code

        private void AUView_PaintSurface(object sender, SkiaSharp.Views.Desktop.SKPaintSurfaceEventArgs e)
        {
            //SkiaSharp.SKRuntimeEffect
            // Your SkiaSharp drawing code here
            using (SKCanvas canvas = e.Surface.Canvas)
            {

                // input values
                float threshold = 1.05f;
                float exponent = 1.5f;

                // image source
                 var blueShirt = SKImage.FromEncodedData(@"D:\Images\PogYouMan\PogYouMan.png");
                 var textureShader = blueShirt.ToShader();

                // shader
                var src = @"
uniform shader 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;
}";
                var effect = SKRuntimeEffect.Create(src, out var errorText);

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

                // shader values
                var children = new SKRuntimeEffectChildren(effect)
                {
                    ["color_map"] = textureShader
                };

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

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

XAML

    <Window x:Class="SkiSharpTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:skia="clr-namespace:SkiaSharp.Views.WPF;assembly=SkiaSharp.Views.WPF"
        xmlns:local="clr-namespace:SkiSharpTest"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <skia:SKElement  Width="100" Height="100" Name="AUView" PaintSurface="AUView_PaintSurface"/>
    </Grid>
</Window>

(I get the exception on the canvas.DrawRect line)
Expected Behavior

Actual Behavior

?
Basic Information

  • Version with issue:
  • Last known good version:
  • IDE:
  • Platform Target Frameworks:
    • Android:
    • iOS:
    • Linux:
    • macOS:
    • Tizen:
    • tvOS:
    • UWP:
    • watchOS:
    • Windows Classic:
  • Target Devices:
    Windows 10
Detailed IDE/OS information (click to expand)

PASTE ANY DETAILED VERSION INFO HERE

Screenshots

Reproduction Link

@mattleibow
Copy link
Contributor

As of today, SKRuntimeEffect is not supported on a raster/bitmap canvas and only on the GPU.

@mattleibow
Copy link
Contributor

Duplicate of #1822

@mattleibow mattleibow marked this as a duplicate of #1822 Jul 31, 2023
@Kuumba123
Copy link
Author

thanks for letting me know ! Does the custom shadders work with WinForm hosted inside WPF ?

@ghost ghost locked as resolved and limited conversation to collaborators Aug 31, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants