SkiaSharp icon indicating copy to clipboard operation
SkiaSharp copied to clipboard

[BUG] SKRuntimeEffect doesn't work on Bitmap Canvas

Open substanc3-dev opened this issue 3 years ago • 4 comments

Description

When attempting to use SKSL effects on bitmap canvases, there is a native exception raised.

Code

var bitmap = new SKBitmap(100, 100, false);
using var canvas = new SKCanvas(bitmap);

var src = @"
half4 main(float2 fragCoord) {
  return half4(0.5, 0.5, 0.5, 1);
}";

using var effect = SKRuntimeEffect.Create(src, out var error);

using (SKPaint bgPaint = new SKPaint { IsAntialias = true, Shader = effect.ToShader(false) })
    canvas.DrawRoundRect(0, 0, 100, 100, 15, 15, bgPaint); // This raises an exception

Expected Behavior

SKSL runtime effect is successfully used on drawing operation.

Actual Behavior

An exception occurs on attempting a draw operation: An unhandled exception of type 'System.Runtime.InteropServices.SEHException' occurred in SkiaSharp.dll External component has thrown an exception.

Basic Information

  • Version with issue: 2.88.0-preview.145
  • Last known good version: None that I'm aware of
  • IDE: Visual Studio
  • Platform Target Frameworks:
    • Windows 10 (21H1 19043.1237) running .NET 6.0.0-rc.1.21451.13

substanc3-dev avatar Oct 04 '21 11:10 substanc3-dev

SkVM is not integrated (for official build) into Skia 88th milestone, support starts at 90th milestone.

AnarchyMob avatar Oct 07 '21 21:10 AnarchyMob

specifically the issue is the SK_ENABLE_SKSL_INTERPRETER is undefined when skia is being compiled

std::unique_ptr<ByteCode> Compiler::toByteCode(Program& program) {
#if defined(SK_ENABLE_SKSL_INTERPRETER)
    AutoSource as(this, program.fSource.get());
    std::unique_ptr<ByteCode> result(new ByteCode());
    ByteCodeGenerator cg(fContext.get(), &program, this, result.get());
    bool success = cg.generateCode();
    if (success) {
        return result;
    }
#else
    ABORT("ByteCode interpreter not enabled");
#endif
    return nullptr;
}

mgood7123 avatar Sep 03 '22 01:09 mgood7123

enabling this in the CPP file directly enables SKSL to work on CPU

mgood7123 avatar Sep 03 '22 01:09 mgood7123

specifically

 // required to enable CPU support for SKSL, this enables SKSL to work in a CPU canvas
#define SK_ENABLE_SKSL_INTERPRETER

in SkiaSharp\externals\skia\src\sksl\SkSLByteCode.cpp and SkiaSharp\externals\skia\src\sksl\SkSLCompiler.cpp

also

// enable JIT if possible
#define SKVM_JIT_WHEN_POSSIBLE

in SkiaSharp\externals\skia\src\core\SkVM.h (doesnt hurt to JIT :) )

mgood7123 avatar Sep 03 '22 01:09 mgood7123

@mgood7123 did those changes work for you? I am still seeing the exception after applying those changes. Could you please help? Here are my changes.

image

xtu avatar Feb 22 '23 10:02 xtu

it should work, did you try recompiling from scratch?

if not then you will need to try to debug the exception

mgood7123 avatar Feb 22 '23 10:02 mgood7123

I used the same azure-pipelines.yml from this repo to create an Azure pipeline, so I think it is recompiling from scratch.

I cannot debug yet. It would take some time to setup the build environment.

Do you have a copy of the libSkiaSharp.dll that have these changes for me to try?

xtu avatar Feb 22 '23 13:02 xtu

Just been testing this in the new Skia update and it is looking so good. This shaders thing looks like a game changer. GPU and CPU surfaces are all fast!

mattleibow avatar Aug 05 '23 06:08 mattleibow

@mattleibow Is there any documentation and/or samples about this update that brings custom shaders support? I've been trying a piece of simplest code with SkiaSharp 2.88.5, which is the latest one available, and it still crashes the same way.

I've tried both bitmap canvas and rendering onscreen through SKCanvasView, both crash.

Gobra avatar Aug 29 '23 12:08 Gobra

Have you tried the v3 alphas? The v2 is still using the same version of skia that does not yet support this.

mattleibow avatar Aug 29 '23 14:08 mattleibow

Have you tried the v3 alphas? The v2 is still using the same version of skia that does not yet support this.

I'm a bit lost here, which v3 alphas are you referring to? NuGet offset not pre-release versions and 2.88.5 is the freshest one, I've checked tags and branches within this project to no result and I've googled around for both SkiaSharp and more general MAUI alpha builds to no avail again.

What am I missing?

Gobra avatar Aug 29 '23 16:08 Gobra

They are on the preview feed: https://aka.ms/skiasharp-eap/index.json

However, I am hoping to release a preview of v3 to nuget soon.

mattleibow avatar Jan 10 '24 20:01 mattleibow