Welcome to mirror list, hosted at ThFree Co, Russian Federation.

svm_mix.h « svm « kernel « cycles « intern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c9e6cdf43b954c3693d3b883cabdbc408bf0e80a (plain)
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/*
 * Copyright 2011, Blender Foundation.
 *
 * 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.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

CCL_NAMESPACE_BEGIN

__device float3 rgb_to_hsv(float3 rgb)
{
	float cmax, cmin, h, s, v, cdelta;
	float3 c;

	cmax = fmaxf(rgb.x, fmaxf(rgb.y, rgb.z));
	cmin = min(rgb.x, min(rgb.y, rgb.z));
	cdelta = cmax - cmin;

	v = cmax;

	if(cmax != 0.0f) {
		s = cdelta/cmax;
	}
	else {
		s = 0.0f;
		h = 0.0f;
	}

	if(s == 0.0f) {
		h = 0.0f;
	}
	else {
		float3 cmax3 = make_float3(cmax, cmax, cmax);
		c = (cmax3 - rgb)/cdelta;

		if(rgb.x == cmax) h = c.z - c.y;
		else if(rgb.y == cmax) h = 2.0f + c.x -  c.z;
		else h = 4.0f + c.y - c.x;

		h /= 6.0f;

		if(h < 0.0f)
			h += 1.0f;
	}

	return make_float3(h, s, v);
}

__device float3 hsv_to_rgb(float3 hsv)
{
	float i, f, p, q, t, h, s, v;
	float3 rgb;

	h = hsv.x;
	s = hsv.y;
	v = hsv.z;

	if(s==0.0f) {
		rgb = make_float3(v, v, v);
	}
	else {
		if(h==1.0f)
			h = 0.0f;
		
		h *= 6.0f;
		i = floor(h);
		f = h - i;
		rgb = make_float3(f, f, f);
		p = v*(1.0f-s);
		q = v*(1.0f-(s*f));
		t = v*(1.0f-(s*(1.0f-f)));
		
		if(i == 0.0f) rgb = make_float3(v, t, p);
		else if(i == 1.0f) rgb = make_float3(q, v, p);
		else if(i == 2.0f) rgb = make_float3(p, v, t);
		else if(i == 3.0f) rgb = make_float3(p, q, v);
		else if(i == 4.0f) rgb = make_float3(t, p, v);
		else rgb = make_float3(v, p, q);
	}

	return rgb;
}

__device float3 svm_lerp(const float3 a, const float3 b, float t)
{
	return (a * (1.0f - t) + b * t);
}

__device float3 svm_mix_blend(float t, float3 col1, float3 col2)
{
	return svm_lerp(col1, col2, t);
}

__device float3 svm_mix_add(float t, float3 col1, float3 col2)
{
	return svm_lerp(col1, col1 + col2, t);
}

__device float3 svm_mix_mul(float t, float3 col1, float3 col2)
{
	return svm_lerp(col1, col1 * col2, t);
}

__device float3 svm_mix_screen(float t, float3 col1, float3 col2)
{
	float tm = 1.0f - t;
	float3 one = make_float3(1.0f, 1.0f, 1.0f);
	float3 tm3 = make_float3(tm, tm, tm);

	return one - (tm3 + t*(one - col2))*(one - col1);
}

__device float3 svm_mix_overlay(float t, float3 col1, float3 col2)
{
	float tm = 1.0f - t;

	float3 outcol = col1;

	if(outcol.x < 0.5f)
		outcol.x *= tm + 2.0f*t*col2.x;
	else
		outcol.x = 1.0f - (tm + 2.0f*t*(1.0f - col2.x))*(1.0f - outcol.x);

	if(outcol.y < 0.5f)
		outcol.y *= tm + 2.0f*t*col2.y;
	else
		outcol.y = 1.0f - (tm + 2.0f*t*(1.0f - col2.y))*(1.0f - outcol.y);

	if(outcol.z < 0.5f)
		outcol.z *= tm + 2.0f*t*col2.z;
	else
		outcol.z = 1.0f - (tm + 2.0f*t*(1.0f - col2.z))*(1.0f - outcol.z);
	
	return outcol;
}

__device float3 svm_mix_sub(float t, float3 col1, float3 col2)
{
	return svm_lerp(col1, col1 - col2, t);
}

__device float3 svm_mix_div(float t, float3 col1, float3 col2)
{
	float tm = 1.0f - t;

	float3 outcol = col1;

	if(col2.x != 0.0f) outcol.x = tm*outcol.x + t*outcol.x/col2.x;
	if(col2.y != 0.0f) outcol.y = tm*outcol.y + t*outcol.y/col2.y;
	if(col2.z != 0.0f) outcol.z = tm*outcol.z + t*outcol.z/col2.z;

	return outcol;
}

__device float3 svm_mix_diff(float t, float3 col1, float3 col2)
{
	return svm_lerp(col1, fabs(col1 - col2), t);
}

__device float3 svm_mix_dark(float t, float3 col1, float3 col2)
{
	return min(col1, col2*t);
}

__device float3 svm_mix_light(float t, float3 col1, float3 col2)
{
	return max(col1, col2*t);
}

__device float3 svm_mix_dodge(float t, float3 col1, float3 col2)
{
	float3 outcol = col1;

	if(outcol.x != 0.0f) {
		float tmp = 1.0f - t*col2.x;
		if(tmp <= 0.0f)
			outcol.x = 1.0f;
		else if((tmp = outcol.x/tmp) > 1.0f)
			outcol.x = 1.0f;
		else
			outcol.x = tmp;
	}
	if(outcol.y != 0.0f) {
		float tmp = 1.0f - t*col2.y;
		if(tmp <= 0.0f)
			outcol.y = 1.0f;
		else if((tmp = outcol.y/tmp) > 1.0f)
			outcol.y = 1.0f;
		else
			outcol.y = tmp;
	}
	if(outcol.z != 0.0f) {
		float tmp = 1.0f - t*col2.z;
		if(tmp <= 0.0f)
			outcol.z = 1.0f;
		else if((tmp = outcol.z/tmp) > 1.0f)
			outcol.z = 1.0f;
		else
			outcol.z = tmp;
	}

	return outcol;
}

__device float3 svm_mix_burn(float t, float3 col1, float3 col2)
{
	float tmp, tm = 1.0f - t;

	float3 outcol = col1;

	tmp = tm + t*col2.x;
	if(tmp <= 0.0f)
		outcol.x = 0.0f;
	else if((tmp = (1.0f - (1.0f - outcol.x)/tmp)) < 0.0f)
		outcol.x = 0.0f;
	else if(tmp > 1.0f)
		outcol.x = 1.0f;
	else
		outcol.x = tmp;

	tmp = tm + t*col2.y;
	if(tmp <= 0.0f)
		outcol.y = 0.0f;
	else if((tmp = (1.0f - (1.0f - outcol.y)/tmp)) < 0.0f)
		outcol.y = 0.0f;
	else if(tmp > 1.0f)
		outcol.y = 1.0f;
	else
		outcol.y = tmp;

	tmp = tm + t*col2.z;
	if(tmp <= 0.0f)
		outcol.z = 0.0f;
	else if((tmp = (1.0f - (1.0f - outcol.z)/tmp)) < 0.0f)
		outcol.z = 0.0f;
	else if(tmp > 1.0f)
		outcol.z = 1.0f;
	else
		outcol.z = tmp;
	
	return outcol;
}

__device float3 svm_mix_hue(float t, float3 col1, float3 col2)
{
	float3 outcol = col1;

	float3 hsv2 = rgb_to_hsv(col2);

	if(hsv2.y != 0.0f) {
		float3 hsv = rgb_to_hsv(outcol);
		hsv.x = hsv2.x;
		float3 tmp = hsv_to_rgb(hsv); 

		outcol = svm_lerp(outcol, tmp, t);
	}

	return outcol;
}

__device float3 svm_mix_sat(float t, float3 col1, float3 col2)
{
	float tm = 1.0f - t;

	float3 outcol = col1;

	float3 hsv = rgb_to_hsv(outcol);

	if(hsv.y != 0.0f) {
		float3 hsv2 = rgb_to_hsv(col2);

		hsv.y = tm*hsv.y + t*hsv2.y;
		outcol = hsv_to_rgb(hsv);
	}

	return outcol;
}

__device float3 svm_mix_val(float t, float3 col1, float3 col2)
{
	float tm = 1.0f - t;

	float3 hsv = rgb_to_hsv(col1);
	float3 hsv2 = rgb_to_hsv(col2);

	hsv.z = tm*hsv.z + t*hsv2.z;

	return hsv_to_rgb(hsv);
}

__device float3 svm_mix_color(float t, float3 col1, float3 col2)
{
	float3 outcol = col1;
	float3 hsv2 = rgb_to_hsv(col2);

	if(hsv2.y != 0.0f) {
		float3 hsv = rgb_to_hsv(outcol);
		hsv.x = hsv2.x;
		hsv.y = hsv2.y;
		float3 tmp = hsv_to_rgb(hsv); 

		outcol = svm_lerp(outcol, tmp, t);
	}

	return outcol;
}

__device float3 svm_mix_soft(float t, float3 col1, float3 col2)
{
	float tm = 1.0f - t;

	float3 one= make_float3(1.0f, 1.0f, 1.0f);
	float3 scr= one - (one - col2)*(one - col1);

	return tm*col1 + t*((one - col1)*col2*col1 + col1*scr);
}

__device float3 svm_mix_linear(float t, float3 col1, float3 col2)
{
	float3 outcol = col1;

	if(col2.x > 0.5f)
		outcol.x= col1.x + t*(2.0f*(col2.x - 0.5f));
	else
		outcol.x= col1.x + t*(2.0f*(col2.x) - 1.0f);

	if(col2.y > 0.5f)
		outcol.y= col1.y + t*(2.0f*(col2.y - 0.5f));
	else
		outcol.y= col1.y + t*(2.0f*(col2.y) - 1.0f);

	if(col2.z > 0.5f)
		outcol.z= col1.z + t*(2.0f*(col2.z - 0.5f));
	else
		outcol.z= col1.z + t*(2.0f*(col2.z) - 1.0f);
	
	return outcol;
}

__device float3 svm_mix(NodeMix type, float fac, float3 c1, float3 c2)
{
	float t = clamp(fac, 0.0f, 1.0f);

	switch(type) {
		case NODE_MIX_BLEND: return svm_mix_blend(t, c1, c2);
		case NODE_MIX_ADD: return svm_mix_add(t, c1, c2);
		case NODE_MIX_MUL: return svm_mix_mul(t, c1, c2);
		case NODE_MIX_SCREEN: return svm_mix_screen(t, c1, c2);
		case NODE_MIX_OVERLAY: return svm_mix_overlay(t, c1, c2);
		case NODE_MIX_SUB: return svm_mix_sub(t, c1, c2);
		case NODE_MIX_DIV: return svm_mix_div(t, c1, c2);
		case NODE_MIX_DIFF: return svm_mix_diff(t, c1, c2);
		case NODE_MIX_DARK: return svm_mix_dark(t, c1, c2);
		case NODE_MIX_LIGHT: return svm_mix_light(t, c1, c2);
		case NODE_MIX_DODGE: return svm_mix_dodge(t, c1, c2);
		case NODE_MIX_BURN: return svm_mix_burn(t, c1, c2);
		case NODE_MIX_HUE: return svm_mix_hue(t, c1, c2);
		case NODE_MIX_SAT: return svm_mix_sat(t, c1, c2);
		case NODE_MIX_VAL: return svm_mix_val (t, c1, c2);
		case NODE_MIX_COLOR: return svm_mix_color(t, c1, c2);
		case NODE_MIX_SOFT: return svm_mix_soft(t, c1, c2);
		case NODE_MIX_LINEAR: return svm_mix_linear(t, c1, c2);
	}

	return make_float3(0.0f, 0.0f, 0.0f);
}

/* Node */

__device void svm_node_mix(KernelGlobals *kg, ShaderData *sd, float *stack, uint fac_offset, uint c1_offset, uint c2_offset, int *offset)
{
	/* read extra data */
	uint4 node1 = read_node(kg, offset);

	float fac = stack_load_float(stack, fac_offset);
	float3 c1 = stack_load_float3(stack, c1_offset);
	float3 c2 = stack_load_float3(stack, c2_offset);
	float3 result = svm_mix((NodeMix)node1.y, fac, c1, c2);

	stack_store_float3(stack, node1.z, result);
}

CCL_NAMESPACE_END