Created
February 18, 2015 21:14
-
-
Save victusfate/bdbed266a73a19f81a94 to your computer and use it in GitHub Desktop.
simple transform and blend with opencl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// affine | |
// http://en.wikipedia.org/wiki/Transformation_matrix#Affine_transformations | |
// http://cairographics.org/manual/cairo-cairo-matrix-t.html#cairo-matrix-t | |
// 3x3 affine matrix | |
// xx xy x0 | |
// yx yy y0 | |
// 0 0 1 | |
// translate, T | |
// 0 0 x0 | |
// 0 0 y0 | |
// 0 0 1 | |
// rotate, counter clockwise, R | |
// cos(a) -sin(a) 0 | |
// sin(a) cos(a) 0 | |
// 0 0 1 | |
// scale, S | |
// sx 0 0 | |
// 0 sy 0 | |
// 0 0 1 | |
// shear/skew | |
// 1 kx 0 | |
// ky 1 0 | |
// 0 0 1 | |
// note for all drawing we accept an inverse affine and for | |
// each destination pixel and grab a linear interpolated source value | |
float2 getSourceCoords(int2 dstCoords, | |
float xx, | |
float yx, | |
float xy, | |
float yy, | |
float x0, | |
float y0); | |
float2 getSourceCoords(int2 dstCoords, | |
float xx, | |
float yx, | |
float xy, | |
float yy, | |
float x0, | |
float y0) | |
{ | |
// forward affine | |
// x_new = x * xx + y * xy + x0; | |
// y_new = x * yx + y * yy + y0; | |
float2 dstCoordsFloat = convert_float2( dstCoords ); | |
float2 srcCoords = { | |
mad(dstCoordsFloat.x , xx, mad(dstCoordsFloat.y , xy , x0 ) ), | |
mad(dstCoordsFloat.x , yx, mad(dstCoordsFloat.y , yy , y0 ) ) | |
// (float)dstCoords.x * xx + (float)dstCoords.y * xy + x0, | |
// (float)dstCoords.x * yx + (float)dstCoords.y * yy + y0 | |
}; | |
return srcCoords; | |
} | |
__kernel void affineDestinationToSource(__read_only image2d_t src, | |
__read_only image2d_t dstBuff, // to normalize args with blend modes | |
__write_only image2d_t dst, | |
float xx, | |
float yx, | |
float xy, | |
float yy, | |
float x0, | |
float y0, | |
float opacity, | |
int setOpacity) | |
{ | |
// globals go over destination width/height | |
int x = get_global_id(0); | |
int y = get_global_id(1); | |
int2 dstCoords = (int2)(x,y); | |
int2 outDim = get_image_dim(dst); | |
if (outsideImage(dstCoords, outDim)) return; | |
float2 srcCoords = getSourceCoords(dstCoords,xx,yx,xy,yy,x0,y0); | |
uint4 srcPixel = read_imageui(src, lsamp, srcCoords); | |
if (setOpacity) srcPixel.w = clamp(opacity * 255.0f,0.0f,255.0f); | |
write_imageui( dst, dstCoords, convert_uint4_sat( srcPixel ) ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment