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

Add the SKRuntimeEffect API #1604

Merged
merged 5 commits into from
Feb 5, 2021
Merged

Add the SKRuntimeEffect API #1604

merged 5 commits into from
Feb 5, 2021

Conversation

mattleibow
Copy link
Contributor

@mattleibow mattleibow commented Feb 3, 2021

Description of Change

This was originally merged in a branch that was going to be m84, but the API was going to change so I delayed the release.

Bugs Fixed

API Changes

SKCanvas canvas = ...; // GPU canvas

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

// image source
using var blueShirt = SKImage.FromEncodedData(Path.Combine(PathToImages, "blue-shirt.jpg"));
using 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;
}";
using 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
using var shader = effect.ToShader(true, uniforms, children);

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

Behavioral Changes

PR Checklist

  • Has tests (if omitted, state reason in description)
  • Rebased on top of master at time of PR
  • Changes adhere to coding standard
  • Updated documentation

@mattleibow mattleibow added this to In progress in v2.88.x via automation Feb 3, 2021
@mattleibow mattleibow merged commit 148e455 into develop Feb 5, 2021
v2.88.x automation moved this from In progress to Done Feb 5, 2021
@mattleibow mattleibow deleted the dev/runtime-effects branch February 5, 2021 08:43
@mattleibow mattleibow added this to Done in v2.88.0 Jul 12, 2021
@mattleibow mattleibow added this to the v2.88.0 milestone May 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

None yet

1 participant