Skip to content

Instantly share code, notes, and snippets.

@forsythetony
Last active January 24, 2020 04:32
Show Gist options
  • Save forsythetony/ff62824861896cd9e60e1f258c34a37d to your computer and use it in GitHub Desktop.
Save forsythetony/ff62824861896cd9e60e1f258c34a37d to your computer and use it in GitHub Desktop.
//
// MetalHelpers.metal
// LargeFormatSimulator
//
// Created by Anthony Forsythe on 1/22/20.
// Copyright © 2020 Anthony Forsythe. All rights reserved.
//
#include <metal_stdlib>
using namespace metal;
#include "MetalHelpers.h"
constant half3 kRec709Luma = half3(0.2126, 0.7152, 0.0722);
half invert(half c) {
return 1.0 - c;
}
half4 convertToGreyscale(half4 inputColor) {
// `inputColor.rgb` will give you some value like [ .5, .5, .5 ]
// and the `dot` function will give you the dot matrix multiplication
// product of its inputs
// In this case that would be [0.2126, 0.7152, 0.0722] * [ .5, .5, .5 ]
// Or when decomposed...
// result = 0.2126 * .5 + 0.7152 * .5 + 0.0722 * .5
//
// You can test it here -> https://bit.ly/2RonWPm
//
// To see where the luma values came from read this -> https://en.wikipedia.org/wiki/Grayscale
half gray = dot(inputColor.rgb, kRec709Luma);
return half4(grey, grey, grey, 1.0);
}
half4 invertColor(half4 inputColor) {
return half4(invert(inputColor.r), invert(inputColor.g), invert(inputColor.b), 1.0);
}
#include <metal_stdlib>
using namespace metal;
#include "MetalHelpers.h"
// Compute kernel
kernel void negativeEffect(texture2d<half, access::read> inputTexture [[ texture(0) ]],
texture2d<half, access::write> outputTexture [[ texture(1) ]],
uint2 gid [[thread_position_in_grid]])
{
// Don't read or write outside of the texture.
if ((gid.x >= inputTexture.get_width()) || (gid.y >= inputTexture.get_height())) {
return;
}
half4 inputColor = inputTexture.read(gid);
half4 negativeColor = invertColor(inputColor);
outputTexture.write(negativeColor, gid);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment