Blame c++/contourgl/cl/contour.cl

d989ab
/*
d989ab
    ......... 2015 Ivan Mahonin
d989ab
d989ab
    This program is free software: you can redistribute it and/or modify
d989ab
    it under the terms of the GNU General Public License as published by
d989ab
    the Free Software Foundation, either version 3 of the License, or
d989ab
    (at your option) any later version.
d989ab
d989ab
    This program is distributed in the hope that it will be useful,
d989ab
    but WITHOUT ANY WARRANTY; without even the implied warranty of
d989ab
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
d989ab
    GNU General Public License for more details.
d989ab
d989ab
    You should have received a copy of the GNU General Public License
d989ab
    along with this program.  If not, see <http: licenses="" www.gnu.org="">.</http:>
d989ab
*/
d989ab
c7fa36
kernel void clear(
0b3288
	global int4 *mark_buffer,
0b3288
	int width )
67876c
{
0b3288
	size_t id = get_global_id(0);
0b3288
	int c = id % width;
0b3288
	int4 v = { 0, 0, c | (c + 1), 0 };
0b3288
	mark_buffer[id] = v;
67876c
}
67876c
c7fa36
kernel void path(
c7fa36
	int width,
c7fa36
	int height,
c7fa36
	global int *mark_buffer,
c7fa36
	global float2 *path )
d989ab
{
5890eb
	const float e = 1e-6f;
c7fa36
	size_t id = get_global_id(0);
c7fa36
	
c7fa36
	float2 s = { (float)width, (float)height }; 
c7fa36
	int w1 = width - 1;
c7fa36
	int h1 = height - 1;
d989ab
c7fa36
	float2 p0 = path[id];
c7fa36
	float2 p1 = path[id + 1];
c7fa36
	bool flipx = p1.x < p0.x;
c7fa36
	bool flipy = p1.y < p0.y;
c7fa36
	if (flipx) { p0.x = s.x - p0.x; p1.x = s.x - p1.x; }
c7fa36
	if (flipy) { p0.y = s.y - p0.y; p1.y = s.y - p1.y; }
c7fa36
	float2 d = p1 - p0;
c7fa36
	float kx = fabs(d.y) < e ? 1e10 : d.x/d.y;
c7fa36
	float ky = fabs(d.x) < e ? 1e10 : d.y/d.x;
c7fa36
	
c7fa36
	while(p0.x != p1.x || p0.y != p1.y) {
c7fa36
		int ix = (int)floor(p0.x + e);
c7fa36
		int iy = (int)floor(p0.y + e);
c7fa36
		if (iy > h1) break;
d989ab
c7fa36
		float2 px, py;
c7fa36
		px.x = (float)(ix + 1);
c7fa36
		px.y = p0.y + ky*(px.x - p0.x);
c7fa36
		py.y = max((float)(iy + 1), 0.f);
c7fa36
		py.x = p0.x + kx*(py.y - p0.y);
c7fa36
		float2 pp1 = p1;
c7fa36
		if (pp1.x > px.x) pp1 = px;
c7fa36
		if (pp1.y > py.y) pp1 = py;
c7fa36
		
c7fa36
		if (iy >= 0) {
c7fa36
			float cover = pp1.y - p0.y;
c7fa36
			float area = px.x - 0.5f*(p0.x + pp1.x);
c7fa36
			if (flipx) { ix = w1 - ix; area = 1.f - area; }
c7fa36
			if (flipy) { iy = h1 - iy; cover = -cover; }
0b3288
			ix = clamp(ix, 0, w1);
0b3288
			global int *row = mark_buffer + 4*iy*width;
0b3288
			global int *mark = row + 4*ix;
0b3288
			atomic_add(mark, (int)round(area*cover*65536.f));
0b3288
			atomic_add(mark + 1, (int)round(cover*65536.f));
0b3288
			int iix = (ix & (ix + 1)) - 1;
0b3288
			while(iix > 0) {
0b3288
				atomic_min(row + 4*iix + 2, ix);
0b3288
				iix = (iix & (iix + 1)) - 1;
0b3288
			}
d989ab
		}
c7fa36
		
c7fa36
		p0 = pp1;
d989ab
	}
d989ab
}
d989ab
c7fa36
kernel void fill(
5890eb
	int width,
0b3288
	global int4 *mark_buffer,
c7fa36
	read_only image2d_t surface_read_image,
c7fa36
	write_only image2d_t surface_write_image,
a04770
	float4 color,
5890eb
	int invert,
5890eb
	int evenodd )
d989ab
{
0b3288
	const int scale = 65536;
0b3288
	const int scale2 = 2*scale;
0b3288
	const int scale05 = scale/2;
0b3288
5890eb
	size_t id = get_global_id(0);
0b3288
	int w1 = width - 1;
0b3288
	global int4 *row = mark_buffer + id*width;
0b3288
0b3288
	int cover = 0;
0b3288
	int ialpha;
0b3288
	int2 c0 = { 0, id };
0b3288
	int2 c1 = c0;
0b3288
	int4 empty_mark = { 0, 0, 0, 0 };
0b3288
	while(c0.x < w1) {
0b3288
		int4 mark;
0b3288
		while(c1.x < width) {
0b3288
			mark = row[c1.x];
0b3288
			empty_mark.z = c1.x | (c1.x + 1);
0b3288
			row[c1.x] = empty_mark; 
0b3288
			if (mark.x || mark.y) break;
0b3288
			c1.x = min(mark.z, width);
0b3288
		}
0b3288
		
0b3288
		ialpha = abs(cover);
0b3288
		ialpha = evenodd ? scale - abs((ialpha % scale2) - scale)
0b3288
						 : min(ialpha, scale);
0b3288
		//if (invert) ialpha = scale - ialpha;  
0b3288
		if (ialpha > scale05) {
0b3288
			while(c0.x < c1.x) {
0b3288
				write_imagef(surface_write_image, c0, color);
0b3288
				++c0.x;
0b3288
			}
0b3288
		}
0b3288
	
0b3288
		if (c1.x < width) {
0b3288
			ialpha = abs(mark.x + cover);
0b3288
			ialpha = evenodd ? scale - abs((ialpha % scale2) - scale)
0b3288
			                 : min(ialpha, scale);
0b3288
			//if (invert) ialpha = scale - ialpha;  
0b3288
			if (ialpha > 4) {
0b3288
				float alpha = (float)ialpha/(float)scale;
0b3288
				float alpha_inv = 1.f - alpha;
0b3288
				float4 cl = read_imagef(surface_read_image, c1);
0b3288
				cl.x = cl.x*alpha_inv + color.x*alpha;
0b3288
				cl.y = cl.y*alpha_inv + color.y*alpha;
0b3288
				cl.z = cl.z*alpha_inv + color.z*alpha;
0b3288
				cl.w = min(cl.w + alpha, 1.f);
0b3288
				write_imagef(surface_write_image, c1, cl);
0b3288
			}
0b3288
		}
a04770
		
0b3288
		c0.x = c1.x + 1;
0b3288
		c1.x = min(mark.z, width);
0b3288
		cover += mark.y;
d989ab
	}
d989ab
}