-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyPaintKernels.cl
More file actions
255 lines (202 loc) · 7.88 KB
/
MyPaintKernels.cl
File metadata and controls
255 lines (202 loc) · 7.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Copyright 2008-2011 Martin Renold <martinxyz@gmx.ch>
* 2014 Daniel Sabo
*/
float calculate_alpha (float rr, float hardness, float slope1, float slope2);
float color_query_weight (float xx, float yy, float radius);
float alpha_from_dab(float x, float y, float4 matrix, float hardness, float slope1, float slope2);
float alpha_from_subpixel_dab(float x, float y, float4 matrix, float hardness, float slope1, float slope2);
float alpha_from_mask(float x, float y, float4 matrix, image2d_t stamp);
inline float4 apply_normal_mode (float4 pixel, float4 color, float alpha, float color_alpha);
inline float4 apply_locked_normal_mode (float4 pixel, float4 color, float alpha);
inline float4 apply_isolate_mode (float4 pixel, float4 color, float alpha);
float apply_texture(float alpha, float x, float y, image2d_t texture, float tex_strength);
float calculate_alpha (float rr, float hardness, float slope1, float slope2)
{
float segment1_offset = 1.0f;
float segment1_slope = slope1;
float segment2_offset = -slope2;
float segment2_slope = slope2;
if (rr <= 1.0f)
{
if (rr <= hardness)
return segment1_offset + rr * segment1_slope;
else
return segment2_offset + rr * segment2_slope;
}
return 0.0f;
}
float alpha_from_dab(float x, float y, float4 matrix, float hardness, float slope1, float slope2)
{
float xr = x * matrix.s0 + y * matrix.s1;
float yr = x * matrix.s2 + y * matrix.s3;
float rr = (yr * yr + xr * xr);
return calculate_alpha(rr, hardness, slope1, slope2);
}
float alpha_from_subpixel_dab(float x, float y, float4 matrix, float hardness, float slope1, float slope2)
{
// The corners of the pixel, in the dab's coordiate space
float xr0 = (x - 0.5f) * matrix.s0 + (y - 0.5f) * matrix.s1;
float yr0 = (x - 0.5f) * matrix.s2 + (y - 0.5f) * matrix.s3;
float xr1 = (x + 0.5f) * matrix.s0 + (y + 0.5f) * matrix.s1;
float yr1 = (x + 0.5f) * matrix.s2 + (y + 0.5f) * matrix.s3;
float x_near, y_near;
// If the signs differ this pixel contains the center of the dab
if (signbit(xr0) != signbit(xr1))
x_near = 0;
else
x_near = min(fabs(xr0), fabs(xr1));
if (signbit(yr0) != signbit(yr1))
y_near = 0;
else
y_near = min(fabs(yr0), fabs(yr1));
// distance^2 for the point closes to the dab's center
float rr_near = x_near * x_near + y_near * y_near;
if (rr_near > 1.0f)
{
// This pixel is entirely outside the dab
return 0.0f;
}
else
{
// distance^2 for a distant point
// This point is somewhat arbitrary, it just needs to be reasonably far away from the near point.
float x_far = max(fabs(xr0), fabs(xr1));
float y_far = max(fabs(yr0), fabs(yr1));
float rr_far = x_far * x_far + y_far * y_far;
// MyPaint's AA blend
float visibilityNear = 1.0f - rr_near;
float delta = rr_far - rr_near;
float delta2 = 1.0f + delta;
visibilityNear /= delta2;
float rr = 1.0f - visibilityNear;
return calculate_alpha(rr, hardness, slope1, slope2);
}
}
float alpha_from_mask(float x, float y, float4 matrix, image2d_t stamp)
{
const sampler_t sampler = CLK_NORMALIZED_COORDS_TRUE | CLK_ADDRESS_CLAMP | CLK_FILTER_LINEAR;
// (x, y) is centered on the range (-radius, radius)
float2 coord = (float2)(x, y);
// Apply rotation and shift to (0.0, 1.0)
coord = (float2)(dot(coord, matrix.s01) + 0.5f,
dot(coord, matrix.s23) + 0.5f);
return read_imagef(stamp, sampler, coord).s0;
}
float apply_texture(float alpha, float x, float y, image2d_t texture, float tex_strength)
{
const sampler_t sampler = CLK_NORMALIZED_COORDS_TRUE | CLK_ADDRESS_REPEAT | CLK_FILTER_NEAREST;
float2 coord = (float2)(x, y) / (float2)(get_image_width(texture), get_image_height(texture));
float texture_value = read_imagef(texture, sampler, coord).s0 * tex_strength;
return clamp(alpha - texture_value, 0.0f, 1.0f);
}
float color_query_weight (float xx, float yy, float radius)
{
/* The real mypaint function renders a dab with these constants
const float hardness = 0.5f;
const float aspect_ratio = 1.0f;
const float angle = 0.0f;
Therefor:
float slope1 = -(1.0f / 0.5f - 1.0f);
float slope2 = -(0.5f / (1.0f - 0.5f));
float slope1 = -1.0f;
float slope2 = -1.0f);
*/
float rr = (yy * yy + xx * xx) / (radius * radius);
if (rr <= 1.0f)
return 1.0f - rr;
return 0.0f;
}
__kernel void mypaint_color_query_part1(__global float4 *buf,
float x,
float y,
int offset,
int width,
int accum_offset,
__global float4 *accum,
float radius)
{
int gidy = get_global_id(0);
float4 total_accum = (float4)(0.0f, 0.0f, 0.0f, 0.0f);
float total_weight = 0.0f;
for (int ix = 0; ix < width; ++ix)
{
float xx = (ix - x);
float yy = (gidy - y);
float pixel_weight = color_query_weight (xx, yy, radius);
float4 pixel = buf[ix + gidy * TILE_PIXEL_WIDTH + offset];
total_accum += pixel * (float4)(pixel.s333, 1.0f) * pixel_weight;
total_weight += pixel_weight;
}
accum[(gidy + accum_offset) * 2] = total_accum;
accum[(gidy + accum_offset) * 2 + 1] = (float4)(total_weight);
}
__kernel void mypaint_color_query_empty_part1( float x,
float y,
int width,
int accum_offset,
__global float4 *accum,
float radius)
{
int gidy = get_global_id(0);
float4 total_accum = (float4)(0.0f, 0.0f, 0.0f, 0.0f);
float total_weight = 0.0f;
for (int ix = 0; ix < width; ++ix)
{
float xx = (ix - x);
float yy = (gidy - y);
total_weight += color_query_weight(xx, yy, radius);
}
accum[(gidy + accum_offset) * 2] = total_accum;
accum[(gidy + accum_offset) * 2 + 1] = (float4)(total_weight);
}
__kernel void mypaint_color_query_part2(__global float4 *accum,
int count)
{
float4 total_accum = (float4)(0.0f, 0.0f, 0.0f, 0.0f);
float total_weight = 0.0f;
size_t end_i = count * 2;
size_t i = 0;
while (i < end_i)
{
total_accum += accum[i++];
total_weight += accum[i++].s0;
}
accum[0] = total_accum;
accum[1] = (float4)(total_weight);
}
inline float4 apply_normal_mode(float4 pixel, float4 color, float alpha, float color_alpha)
{
alpha = alpha * color.s3;
float dst_alpha = pixel.s3;
float a = alpha * color_alpha + dst_alpha * (1.0f - alpha);
float a_term = dst_alpha * (1.0f - alpha);
if (a > 0.0f) /* Needed because color_alpha can make a = zero */
pixel.s012 = (color.s012 * alpha * color_alpha + pixel.s012 * a_term) / a;
pixel.s3 = a;
return pixel;
}
inline float4 apply_locked_normal_mode(float4 pixel, float4 color, float alpha)
{
if (pixel.s3)
{
alpha = alpha * color.s3;
float a_term = 1.0f - alpha;
pixel.s012 = color.s012 * alpha + pixel.s012 * a_term;
}
return pixel;
}
inline float4 apply_isolate_mode(float4 pixel, float4 color, float alpha)
{
alpha = alpha * color.s3;
float dst_alpha = pixel.s3;
float a_term = dst_alpha - dst_alpha * alpha;
float a = alpha + a_term;
pixel.s012 = (color.s012 * alpha + pixel.s012 * a_term) / a;
pixel.s3 = max(min(a, color.s3), dst_alpha);
return pixel;
}