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

105dfe
/*
20cefb
    ......... 2015-2018 Ivan Mahonin
105dfe
105dfe
    This program is free software: you can redistribute it and/or modify
105dfe
    it under the terms of the GNU General Public License as published by
105dfe
    the Free Software Foundation, either version 3 of the License, or
105dfe
    (at your option) any later version.
105dfe
105dfe
    This program is distributed in the hope that it will be useful,
105dfe
    but WITHOUT ANY WARRANTY; without even the implied warranty of
105dfe
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
105dfe
    GNU General Public License for more details.
105dfe
105dfe
    You should have received a copy of the GNU General Public License
105dfe
    along with this program.  If not, see <http: licenses="" www.gnu.org="">.</http:>
105dfe
*/
105dfe
feaa05
#pragma OPENCL EXTENSION cl_khr_int64_base_atomics: enable
feaa05
5a7afd
#define ONE       65536
5a7afd
#define TWO      131072               // (ONE)*2
5a7afd
#define HALF      32768               // (ONE)/2
5a7afd
#define ONE_F     65536.f             // (float)(ONE)
5a7afd
#define DIV_ONE_F 0.0000152587890625f // 1.f/(ONE_F)
5a7afd
5a7afd
105dfe
kernel void path(
105dfe
	int width,
105dfe
	int height,
8cf99a
	global long *marks,
105dfe
	global float2 *points,
77d271
	int end,
77d271
	int minx )
105dfe
{
105dfe
	int id = get_global_id(0);
77d271
	if (id >= end) return;
105dfe
	float2 p0 = points[id];
105dfe
	float2 p1 = points[id + 1];
20cefb
	
105dfe
	bool flipx = p1.x < p0.x;
105dfe
	bool flipy = p1.y < p0.y;
8cf99a
	if (flipx) { p0.x = (float)width  - p0.x; p1.x = (float)width  - p1.x; }
20cefb
	if (flipy) { p0.y = (float)height - p0.y; p1.y = (float)height - p1.y; }
105dfe
	float2 d = p1 - p0;
20cefb
	int w1 = width - 1;
20cefb
	int h1 = height - 1;
8cf99a
	float kx = d.x/d.y;
8cf99a
	float ky = d.y/d.x;
105dfe
	
105dfe
	while(p0.x != p1.x || p0.y != p1.y) {
a7622f
		int ix = max((int)p0.x, 0);
a7622f
		int iy = (int)p0.y;
a7622f
		if (ix > w1) return;
105dfe
8cf99a
		float2 px, py;
105dfe
		px.x = (float)(ix + 1);
f14ea7
		py.y = (float)(iy + 1);
a7622f
		iy = clamp(iy, 0, h1);
8cf99a
		
8cf99a
		px.y = p0.y + ky*(px.x - p0.x);
105dfe
		py.x = p0.x + kx*(py.y - p0.y);
8cf99a
		
8cf99a
		float2 pp1 = p1;
105dfe
		if (pp1.x > px.x) pp1 = px;
105dfe
		if (pp1.y > py.y) pp1 = py;
105dfe
		
a7622f
		float cover = (pp1.x - p0.x)*ONE_F;
a7622f
		float area = py.y - 0.5f*(p0.y + pp1.y);
a7622f
		if (flipx) { ix = w1 - ix; cover = -cover; }
a7622f
		if (flipy) { iy = h1 - iy; area = 1.f - area; }
105dfe
		p0 = pp1;
8cf99a
		
a7622f
		atomic_add(marks + iy*width + ix, upsample((int)cover, (int)(area*cover)));
105dfe
	}
105dfe
}
105dfe
82f284
// TODO:
82f284
// different implementations for:
82f284
//   antialiased, transparent, inverted, evenodd contours and combinations (total 16 implementations)
105dfe
kernel void fill(
a7622f
	int width,
8cf99a
	global int2 *marks,
105dfe
	global float4 *image,
105dfe
	float4 color,
77d271
	int4 bounds )
105dfe
{
a7622f
	if (get_global_id(0) >= bounds.s2) return;
a7622f
	int id = (int)get_global_id(0) + bounds.s1*width;
20cefb
	marks += id;
20cefb
	image += id;
20cefb
8cf99a
	int icover = 0;
8cf99a
	while(true) {
8cf99a
		int2 m = *marks;
8cf99a
		*marks = (int2)(0, 0);
8cf99a
		float alpha = (float)abs(m.x + icover)*color.w*DIV_ONE_F;
a7622f
		marks += width;
82f284
82f284
		icover += m.y;
8cf99a
		*image = *image*(1.f - alpha) + color*alpha;
8cf99a
		
a7622f
		if (++bounds.s1 >= bounds.s3) return;
a7622f
		image += width;
105dfe
	}
105dfe
}