Esempio n. 1
0
        public void InvalidSourceFailsCreation(string src, string expected)
        {
            using var effect = SKRuntimeEffect.Create(src, out var errorText);

            Assert.Null(effect);
            Assert.Contains(expected, errorText);
        }
Esempio n. 2
0
        public void ColorFiltersRunOnGpu()
        {
            using var ctx = CreateGlContext();
            ctx.MakeCurrent();

            using var grContext = GRContext.CreateGl();

            var info = new SKImageInfo(512, 256, SKColorType.Rgba8888);

            using var surface = SKSurface.Create(grContext, false, info);
            using var canvas  = surface.Canvas;

            var src = @"
uniform shader input;
half4 main() {
    return sample(input);
}";

            using var img = SKImage.FromEncodedData(Path.Combine(PathToImages, "baboon.jpg"));
            canvas.DrawImage(img, 0, 0);

            using var effect = SKRuntimeEffect.Create(src, out var errorText);
            Assert.Null(errorText);
            Assert.NotNull(effect);

            var cf1 = effect.ToColorFilter();

            using var p = new SKPaint
                  {
                      ColorFilter = cf1,
                  };
            canvas.DrawImage(img, 256, 0, p);
        }
Esempio n. 3
0
        public void RuntimeEffectUniformsThrowsCorrectly(string name, object value)
        {
            var src = @"
uniform float uniform_float;
uniform float uniform_float_array[2];
half4 main() { return half4(0); }";

            using var effect = SKRuntimeEffect.Create(src, out _);
            var uniforms = new SKRuntimeEffectUniforms(effect);

            if (value is int intValue)
            {
                Assert.Throws <ArgumentException>(() => uniforms.Add(name, intValue));
            }
            else if (value is float floatValue)
            {
                Assert.Throws <ArgumentException>(() => uniforms.Add(name, floatValue));
            }
            else if (value is float[] floatArray)
            {
                Assert.Throws <ArgumentException>(() => uniforms.Add(name, floatArray));
            }
            else
            {
                throw new ArgumentException($"Invalid test data type {value}");
            }
        }
Esempio n. 4
0
        public void RuntimeEffectChildrenWorksCorrectly()
        {
            using var blueShirt     = SKImage.FromEncodedData(Path.Combine(PathToImages, "blue-shirt.jpg"));
            using var textureShader = blueShirt.ToShader();

            var src = @"
uniform shader color_map;
uniform shader normal_map;
half4 main() { return half4(0); }";

            using var effect = SKRuntimeEffect.Create(src, out _);
            var children = new SKRuntimeEffectChildren(effect);

            children.Add("color_map", textureShader);
            Assert.Equal(new SKShader[] { textureShader, null }, children.ToArray());

            children.Add("normal_map", textureShader);
            Assert.Equal(new SKShader[] { textureShader, textureShader }, children.ToArray());

            children.Add("color_map", null);
            Assert.Equal(new SKShader[] { null, textureShader }, children.ToArray());

            children.Reset();
            Assert.Equal(new SKShader[] { null, null }, children.ToArray());
        }
Esempio n. 5
0
        public void CorrectUniformsAreListed(string header, string[] uniforms)
        {
            var src = $"{header} half4 main() {{ return half4(0); }}";

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

            Assert.Equal(uniforms, effect.Uniforms);
        }
Esempio n. 6
0
        public void CorrectChildrenAreListed(string header, string[] children)
        {
            var src = $"{header} half4 main() {{ return half4(0); }}";

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

            Assert.Equal(children, effect.Children);
        }
Esempio n. 7
0
        public void InvalidColorFilters(string src)
        {
            using var effect = SKRuntimeEffect.Create(src, out var errorText);

            var uniforms = new SKRuntimeEffectUniforms(effect);

            Assert.NotNull(effect.ToShader(false, uniforms));
            Assert.Null(effect.ToColorFilter(uniforms));
        }
Esempio n. 8
0
 protected override void OnLoad()
 {
     base.OnLoad();
     _runtimeEffect = SKRuntimeEffect.Create(_shaderText, out var errors);
     if (_runtimeEffect == null)
     {
         Console.Error.WriteLine(errors);
         Environment.Exit(1);
     }
 }
Esempio n. 9
0
            public TestEffect(string src)
            {
                effect = SKRuntimeEffect.Create(src, out var errorText);

                Assert.Null(errorText);
                Assert.NotNull(effect);

                uniforms = new SKRuntimeEffectUniforms(effect);
                children = new SKRuntimeEffectChildren(effect);
            }
Esempio n. 10
0
        public void RuntimeEffectUniformsThrowsWhithInvalidName()
        {
            var src = @"
uniform float uniform_float;
half4 main() { return half4(0); }";

            using var effect = SKRuntimeEffect.Create(src, out _);
            var uniforms = new SKRuntimeEffectUniforms(effect);

            Assert.Throws <ArgumentOutOfRangeException>(() => uniforms.Add("invalid", 1f));
        }
Esempio n. 11
0
        public void RuntimeEffectChildrenMatchesEffect(string header, string[] children)
        {
            var src = $"{header} half4 main() {{ return half4(0); }}";

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

            var effectChildren = new SKRuntimeEffectChildren(effect);

            Assert.Equal(children, effectChildren.Names);
            Assert.Equal(new SKShader[children.Length], effectChildren.ToArray());
        }
Esempio n. 12
0
        public void RuntimeEffectUniformsMatchesEffect(string header, string[] uniforms)
        {
            var src = $"{header} half4 main() {{ return half4(0); }}";

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

            var effectUniforms = new SKRuntimeEffectUniforms(effect);

            Assert.Equal(uniforms, effectUniforms.Names);
            Assert.Equal(effect.UniformSize, effectUniforms.ToData().Size);
            Assert.Equal(uniforms, effectUniforms);
        }
Esempio n. 13
0
        public void RuntimeEffectInitializesCorrectChildren()
        {
            var src = @"
uniform shader color_map;
uniform shader normal_map;
half4 main() { return half4(0); }";

            using var effect = SKRuntimeEffect.Create(src, out _);
            var effectChildren = new SKRuntimeEffectChildren(effect);

            Assert.Equal(2, effectChildren.Count);
            Assert.Equal(2, effectChildren.Names.Count);
            Assert.Equal(new[] { "color_map", "normal_map" }, effectChildren.Names);
            Assert.Equal(new[] { "color_map", "normal_map" }, effectChildren);
        }
Esempio n. 14
0
        public void RuntimeEffectUniformsWorksCorrectly()
        {
            var src = @"
uniform float uniform_float;
uniform float uniform_float_array[2];
uniform float2 uniform_float2;
half4 main() { return half4(0); }";

            using var effect = SKRuntimeEffect.Create(src, out _);
            var uniforms = new SKRuntimeEffectUniforms(effect);

            uniforms.Add("uniform_float", 1f);
            uniforms.Add("uniform_float_array", new[] { 1f, 2f });
            uniforms.Add("uniform_float2", new[] { 1f, 2f });
            uniforms.Reset();
        }
Esempio n. 15
0
        public void RuntimeEffectUniformsWorksCorrectlyWithCollectionIndexer()
        {
            var src = @"
uniform float uniform_float;
uniform float uniform_float_array[2];
uniform float2 uniform_float2;
half4 main() { return half4(0); }";

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

            var uniforms = new SKRuntimeEffectUniforms(effect)
            {
                ["uniform_float"]       = 1f,
                ["uniform_float_array"] = new[] { 1f, 2f },
                ["uniform_float2"]      = new[] { 1f, 2f }
            };
        }
Esempio n. 16
0
        public void ImageCanClearBackground(float threshold, float exponent, uint backgroundColor, uint shirtColor)
        {
            using var ctx = CreateGlContext();
            ctx.MakeCurrent();
            using var grContext = GRContext.CreateGl();

            var info = new SKImageInfo(500, 500, SKColorType.Rgba8888);

            using var surface = SKSurface.Create(grContext, false, info);
            var canvas = surface.Canvas;

            canvas.Clear(SKColors.Black);

            using var blueShirt     = SKImage.FromEncodedData(Path.Combine(PathToImages, "blue-shirt.jpg"));
            using var textureShader = blueShirt.ToShader();

            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);
            Assert.Null(errorText);
            Assert.NotNull(effect);

            var uniformSize = effect.UniformSize;

            Assert.Equal(5 * sizeof(float), uniformSize);

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

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

            using var shader = effect.ToShader(true, uniforms, children);

            using var paint = new SKPaint { Shader = shader };
            canvas.DrawRect(SKRect.Create(400, 400), paint);

            var actual = new SKColor[500 * 500];

            fixed(void *a = actual)
            Assert.True(surface.ReadPixels(info, (IntPtr)a, info.RowBytes, 0, 0));

            Assert.Equal(backgroundColor, actual[100 * info.Width + 100]);
            Assert.Equal(shirtColor, actual[230 * info.Width + 300]);
        }