Skip to content

Instantly share code, notes, and snippets.

View Sergio0694's full-sized avatar
🚀
Coding all day

Sergio Pedri Sergio0694

🚀
Coding all day
View GitHub Profile
@Sergio0694
Sergio0694 / GetAttachedSemiAcrylicEffectAsync.cs
Last active April 22, 2022 17:04
A method that combines Composition APIs and Win2D to replicate the Acrylic brush on Windows 10 Creator's Update
/// <summary>
/// Gets a shared semaphore to avoid loading multiple Win2D resources at the same time
/// </summary>
private static readonly SemaphoreSlim Win2DSemaphore = new SemaphoreSlim(1);
/// <summary>
/// Creates an effect brush that's similar to the official Acrylic brush in the Fall Creator's Update.
/// The pipeline uses the following effects: HostBackdropBrush > <see cref="LuminanceToAlphaEffect"/> >
/// <see cref="OpacityEffect"/> > <see cref="BlendEffect"/> > <see cref="ArithmeticCompositeEffect"/> >
/// <see cref="ColorSourceEffect"/> > <see cref="BorderEffect"/> with customizable blend factors for each couple of layers
@Sergio0694
Sergio0694 / tf_keras_error_repro.py
Created July 11, 2018 13:29
Reproduces an error when loading multiple shared instances of a Keras model in the same scope
import tensorflow as tf
'''
This snippet reproduces an error when trying to load multiple shared instances
of a Keras model in TensorFlow, when accessing the same scope multiple times.
Creating multiple instances repeatedly in a single step works.
'''
def load_vgg19_multiple(tensors):
'''Creates n shared instances of VGG19, in a single run.'''
@Sergio0694
Sergio0694 / GenericOperatorPattern.md
Last active March 16, 2019 17:20
A pattern to work around the lack of static, virtual generic interface methods in C#

The Generic Operator pattern

The Generic Operator pattern (GO pattern, for short) works around two limitations in C#:

  • The lack of static interface methods
  • The lack of virtual static methods (eg. static methods that can be inherited and overridden)

The first one is pretty easy to demonstrate:

public interface IDoThings
@Sergio0694
Sergio0694 / ComputeSharpBokehTest.cs
Created August 28, 2019 15:19
A benchmark between ImageSharp and ComputeSharp with a bokeh effect
using System;
using System.IO;
using System.Numerics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using ComputeSharp;
using SixLabors.ImageSharp;
public static void Main()
{
int[] array = new int[100];
int value = 1;
Action<int> action = i => array[i] = value;
}
[CompilerGenerated]
private sealed class <>c__DisplayClass0_0
{
public int[] array;
public int value;
internal void <Main>b__0(int i)
{
array[i] = value;
public static void Main()
{
int[] array = new int[100];
{
int value = 1;
Action<int> action = i => array[i] = value;
}
}
[CompilerGenerated]
private sealed class <>c__DisplayClass0_0
{
public int[] array;
}
[CompilerGenerated]
private sealed class <>c__DisplayClass0_1
{
public int value;
public static void Main()
{
object instance = "Hello world";
Type type = instance.GetType(); // System.String
PropertyInfo property = type.GetProperty("Length");
object value = property.GetValue(instance);
Console.WriteLine(value); // Prints 11
public static void Main()
{
object instance = new Vector3(1, 2, 3);
foreach (FieldInfo property in instance.GetType().GetFields())
{
Console.WriteLine($"{property.Name}: {property.GetValue(instance)}");
// X: 1
// Y: 2
// Z: 3