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(
2d0519
	global int2 *mark_buffer )
67876c
{
2d0519
	const int2 v = { 0, 0 };
2d0519
	mark_buffer[ get_global_id(0) ] = 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) {
2d0519
			// calc values
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);
2d0519
			
2d0519
			// store in buffer
2d0519
			global int *mark = mark_buffer + (iy*width + ix)*2;
2d0519
			atomic_add(mark,     (int)round(area*cover*65536.f));
0b3288
			atomic_add(mark + 1, (int)round(cover*65536.f));
d989ab
		}
c7fa36
		
c7fa36
		p0 = pp1;
d989ab
	}
d989ab
}
d989ab
c7fa36
kernel void fill(
5890eb
	int width,
2d0519
	global int2 *mark_buffer,
c7fa36
	read_only image2d_t surface_read_image,
c7fa36
	write_only image2d_t surface_write_image,
2d0519
	int minx,
2d0519
	int maxx,
a04770
	float4 color,
5890eb
	int invert,
5890eb
	int evenodd )
d989ab
{
5890eb
	size_t id = get_global_id(0);
2d0519
	global int2 *row = mark_buffer + id*width;
2d0519
	const int2 empty_mark = { 0, 0 };
0b3288
2d0519
	float cover = 0.f;
2d0519
	for(int2 c = {minx, id}; c.x < maxx; ++c.x) {
2d0519
		// read mark (x: alpha, y: cover)
2d0519
		global int2 *mark = row + c.x;
2d0519
		float alpha = fabs(cover + mark->x/65536.f);
2d0519
		//if (evenodd) alpha = 1.f - fabs(fmod(alpha, 2.f) - 1.f);
2d0519
		cover += mark->y/65536.f;
2d0519
		*mark = empty_mark;
0b3288
		
2d0519
		//if (invert) alpha = 1.f - alpha;
2d0519
		alpha *= color.w;
a04770
		
2d0519
		// write color
2d0519
		float alpha_inv = 1.f - alpha;
2d0519
		float4 cl = read_imagef(surface_read_image, c);
2d0519
		cl.x = cl.x*alpha_inv + color.x*alpha;
2d0519
		cl.y = cl.y*alpha_inv + color.y*alpha;
2d0519
		cl.z = cl.z*alpha_inv + color.z*alpha;
2d0519
		cl.w = min(cl.w + alpha, 1.f);
2d0519
		write_imagef(surface_write_image, c, cl);
d989ab
	}
d989ab
}