Slide 3
Slide 3 text
September 2015
Although that may seem like a lot of steps, many of these
objects are persistent and long lived and, therefore, only need
to be instantiated once. Take a look at some of the projects
referred to in the links at the end of this article to see examples
of MetalKit based projects.
Compute Shaders
GPU programming has typically been based on two different
types of shader or program.
A vertex shader is responsible for taking each vertex of each
object in a 3D scene and translating its position from its 3D
universe to a screen location.
The second type of shader, a "agment shader, takes the data
from the vertex shader and calculates the colour for every on-
screen pixel. It’s responsible for shading, texturing, reflections
and refractions and so on.
However, for image processing, we need to use a third type of
shader, a compute shader. Compute shaders aren’t concerned
with 3D space at all, rather they accept a dataset and, in our
case, that dataset it a two dimensional array describing an
image.
The simplest compute shader for image processing would
simply pass through the colour of each pixel from a source
image and write it to a target image and look something like
this:
Here, our kernel function, customFunction, is run once for every
pixel. It has three arguments, the first is the readable input
texture, inTexture, and the second is the writable output
Simon Gladman flexmonkey.blogspot.co.uk 3
kernel void customFunction(
texture2d inTexture [[texture(0)]],
texture2d outTexture [[texture(1)]],
uint2 gid [[thread_position_in_grid]])
{
const float4 colorAtPixel = inTexture.read(gid);
outTexture.write(colorAtPixel, gid);
}