Blender V5.3
gpu_shader_common_mix_rgb.bsl.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2019-2022 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
8
10
11[[node]]
12void mix_blend(float fac, float4 col1, float4 col2, float4 &outcol)
13{
14 outcol = mix(col1, col2, fac);
15 outcol.a = col1.a;
16}
17
18[[node]]
19void mix_add(float fac, float4 col1, float4 col2, float4 &outcol)
20{
21 outcol = mix(col1, col1 + col2, fac);
22 outcol.a = col1.a;
23}
24
25[[node]]
26void mix_mult(float fac, float4 col1, float4 col2, float4 &outcol)
27{
28 outcol = mix(col1, col1 * col2, fac);
29 outcol.a = col1.a;
30}
31
32[[node]]
33void mix_screen(float fac, float4 col1, float4 col2, float4 &outcol)
34{
35 float facm = 1.0f - fac;
36
37 outcol = float4(1.0f) - (float4(facm) + fac * (float4(1.0f) - col2)) * (float4(1.0f) - col1);
38 outcol.a = col1.a;
39}
40
41[[node]]
42void mix_overlay(float fac, float4 col1, float4 col2, float4 &outcol)
43{
44 float facm = 1.0f - fac;
45
46 outcol = col1;
47
48 if (outcol.r < 0.5f) {
49 outcol.r *= facm + 2.0f * fac * col2.r;
50 }
51 else {
52 outcol.r = 1.0f - (facm + 2.0f * fac * (1.0f - col2.r)) * (1.0f - outcol.r);
53 }
54
55 if (outcol.g < 0.5f) {
56 outcol.g *= facm + 2.0f * fac * col2.g;
57 }
58 else {
59 outcol.g = 1.0f - (facm + 2.0f * fac * (1.0f - col2.g)) * (1.0f - outcol.g);
60 }
61
62 if (outcol.b < 0.5f) {
63 outcol.b *= facm + 2.0f * fac * col2.b;
64 }
65 else {
66 outcol.b = 1.0f - (facm + 2.0f * fac * (1.0f - col2.b)) * (1.0f - outcol.b);
67 }
68}
69
70[[node]]
71void mix_sub(float fac, float4 col1, float4 col2, float4 &outcol)
72{
73 outcol = mix(col1, col1 - col2, fac);
74 outcol.a = col1.a;
75}
76
77[[node]]
78void mix_div(float fac, float4 col1, float4 col2, float4 &outcol)
79{
80 float facm = 1.0f - fac;
81
82 outcol = float4(float3(0.0f), col1.a);
83
84 if (col2.r != 0.0f) {
85 outcol.r = facm * col1.r + fac * col1.r / col2.r;
86 }
87 if (col2.g != 0.0f) {
88 outcol.g = facm * col1.g + fac * col1.g / col2.g;
89 }
90 if (col2.b != 0.0f) {
91 outcol.b = facm * col1.b + fac * col1.b / col2.b;
92 }
93}
94
95/* A variant of mix_div that fallback to the first color upon zero division. */
96[[node]]
97void mix_div_fallback(float fac, float4 col1, float4 col2, float4 &outcol)
98{
99 float facm = 1.0f - fac;
100
101 outcol = col1;
102
103 if (col2.r != 0.0f) {
104 outcol.r = facm * outcol.r + fac * outcol.r / col2.r;
105 }
106 if (col2.g != 0.0f) {
107 outcol.g = facm * outcol.g + fac * outcol.g / col2.g;
108 }
109 if (col2.b != 0.0f) {
110 outcol.b = facm * outcol.b + fac * outcol.b / col2.b;
111 }
112}
113
114[[node]]
115void mix_diff(float fac, float4 col1, float4 col2, float4 &outcol)
116{
117 outcol = mix(col1, abs(col1 - col2), fac);
118 outcol.a = col1.a;
119}
120
121[[node]]
122void mix_exclusion(float fac, float4 col1, float4 col2, float4 &outcol)
123{
124 outcol = max(mix(col1, col1 + col2 - 2.0f * col1 * col2, fac), 0.0f);
125 outcol.a = col1.a;
126}
127
128[[node]]
129void mix_dark(float fac, float4 col1, float4 col2, float4 &outcol)
130{
131 outcol.rgb = mix(col1.rgb, min(col1.rgb, col2.rgb), fac);
132 outcol.a = col1.a;
133}
134
135[[node]]
136void mix_light(float fac, float4 col1, float4 col2, float4 &outcol)
137{
138 outcol.rgb = mix(col1.rgb, max(col1.rgb, col2.rgb), fac);
139 outcol.a = col1.a;
140}
141
142[[node]]
143void mix_dodge(float fac, float4 col1, float4 col2, float4 &outcol)
144{
145 outcol = col1;
146
147 if (outcol.r != 0.0f) {
148 float tmp = 1.0f - fac * col2.r;
149 if (tmp <= 0.0f) {
150 outcol.r = 1.0f;
151 }
152 else if ((tmp = outcol.r / tmp) > 1.0f) {
153 outcol.r = 1.0f;
154 }
155 else {
156 outcol.r = tmp;
157 }
158 }
159 if (outcol.g != 0.0f) {
160 float tmp = 1.0f - fac * col2.g;
161 if (tmp <= 0.0f) {
162 outcol.g = 1.0f;
163 }
164 else if ((tmp = outcol.g / tmp) > 1.0f) {
165 outcol.g = 1.0f;
166 }
167 else {
168 outcol.g = tmp;
169 }
170 }
171 if (outcol.b != 0.0f) {
172 float tmp = 1.0f - fac * col2.b;
173 if (tmp <= 0.0f) {
174 outcol.b = 1.0f;
175 }
176 else if ((tmp = outcol.b / tmp) > 1.0f) {
177 outcol.b = 1.0f;
178 }
179 else {
180 outcol.b = tmp;
181 }
182 }
183}
184
185[[node]]
186void mix_burn(float fac, float4 col1, float4 col2, float4 &outcol)
187{
188 float tmp, facm = 1.0f - fac;
189
190 outcol = col1;
191
192 tmp = facm + fac * col2.r;
193 if (tmp <= 0.0f) {
194 outcol.r = 0.0f;
195 }
196 else if ((tmp = (1.0f - (1.0f - outcol.r) / tmp)) < 0.0f) {
197 outcol.r = 0.0f;
198 }
199 else if (tmp > 1.0f) {
200 outcol.r = 1.0f;
201 }
202 else {
203 outcol.r = tmp;
204 }
205
206 tmp = facm + fac * col2.g;
207 if (tmp <= 0.0f) {
208 outcol.g = 0.0f;
209 }
210 else if ((tmp = (1.0f - (1.0f - outcol.g) / tmp)) < 0.0f) {
211 outcol.g = 0.0f;
212 }
213 else if (tmp > 1.0f) {
214 outcol.g = 1.0f;
215 }
216 else {
217 outcol.g = tmp;
218 }
219
220 tmp = facm + fac * col2.b;
221 if (tmp <= 0.0f) {
222 outcol.b = 0.0f;
223 }
224 else if ((tmp = (1.0f - (1.0f - outcol.b) / tmp)) < 0.0f) {
225 outcol.b = 0.0f;
226 }
227 else if (tmp > 1.0f) {
228 outcol.b = 1.0f;
229 }
230 else {
231 outcol.b = tmp;
232 }
233}
234
235[[node]]
236void mix_hue(float fac, float4 col1, float4 col2, float4 &outcol)
237{
238 outcol = col1;
239
240 float4 hsv, hsv2, tmp;
241 rgb_to_hsv(col2, hsv2);
242
243 if (hsv2.y != 0.0f) {
244 rgb_to_hsv(outcol, hsv);
245 hsv.x = hsv2.x;
246 hsv_to_rgb(hsv, tmp);
247
248 outcol = mix(outcol, tmp, fac);
249 outcol.a = col1.a;
250 }
251}
252
253[[node]]
254void mix_sat(float fac, float4 col1, float4 col2, float4 &outcol)
255{
256 float facm = 1.0f - fac;
257
258 outcol = col1;
259
260 float4 hsv, hsv2;
261 rgb_to_hsv(outcol, hsv);
262
263 if (hsv.y != 0.0f) {
264 rgb_to_hsv(col2, hsv2);
265
266 hsv.y = facm * hsv.y + fac * hsv2.y;
267 hsv_to_rgb(hsv, outcol);
268 }
269}
270
271[[node]]
272void mix_val(float fac, float4 col1, float4 col2, float4 &outcol)
273{
274 float facm = 1.0f - fac;
275
276 float4 hsv, hsv2;
277 rgb_to_hsv(col1, hsv);
278 rgb_to_hsv(col2, hsv2);
279
280 hsv.z = facm * hsv.z + fac * hsv2.z;
281 hsv_to_rgb(hsv, outcol);
282}
283
284[[node]]
285void mix_color(float fac, float4 col1, float4 col2, float4 &outcol)
286{
287 outcol = col1;
288
289 float4 hsv, hsv2, tmp;
290 rgb_to_hsv(col2, hsv2);
291
292 if (hsv2.y != 0.0f) {
293 rgb_to_hsv(outcol, hsv);
294 hsv.x = hsv2.x;
295 hsv.y = hsv2.y;
296 hsv_to_rgb(hsv, tmp);
297
298 outcol = mix(outcol, tmp, fac);
299 outcol.a = col1.a;
300 }
301}
302
303[[node]]
304void mix_soft(float fac, float4 col1, float4 col2, float4 &outcol)
305{
306 float facm = 1.0f - fac;
307
308 float4 one = float4(1.0f);
309 float4 scr = one - (one - col2) * (one - col1);
310 outcol = facm * col1 + fac * ((one - col1) * col2 * col1 + col1 * scr);
311 outcol.a = col1.a;
312}
313
314[[node]]
315void mix_linear(float fac, float4 col1, float4 col2, float4 &outcol)
316{
317 outcol = col1 + fac * (2.0f * (col2 - float4(0.5f)));
318 outcol.a = col1.a;
319}
320
321[[node]]
322void clamp_color(float4 vec, const float4 min, const float4 max, float4 &out_vec)
323{
324 out_vec = clamp(vec, min, max);
325}
326
327[[node]]
328void multiply_by_alpha(float factor, float4 color, float &result)
329{
330 result = factor * color.a;
331}
btScalar max
void rgb_to_hsv(float4 rgb, float4 &outcol)
void hsv_to_rgb(float4 hsv, float4 &outcol)
void mix_dodge(float fac, float4 col1, float4 col2, float4 &outcol)
void mix_mult(float fac, float4 col1, float4 col2, float4 &outcol)
void mix_sat(float fac, float4 col1, float4 col2, float4 &outcol)
void mix_color(float fac, float4 col1, float4 col2, float4 &outcol)
void mix_div(float fac, float4 col1, float4 col2, float4 &outcol)
void clamp_color(float4 vec, const float4 min, const float4 max, float4 &out_vec)
void multiply_by_alpha(float factor, float4 color, float &result)
void mix_hue(float fac, float4 col1, float4 col2, float4 &outcol)
void mix_light(float fac, float4 col1, float4 col2, float4 &outcol)
void mix_overlay(float fac, float4 col1, float4 col2, float4 &outcol)
void mix_burn(float fac, float4 col1, float4 col2, float4 &outcol)
void mix_val(float fac, float4 col1, float4 col2, float4 &outcol)
void mix_soft(float fac, float4 col1, float4 col2, float4 &outcol)
void mix_linear(float fac, float4 col1, float4 col2, float4 &outcol)
void mix_exclusion(float fac, float4 col1, float4 col2, float4 &outcol)
void mix_add(float fac, float4 col1, float4 col2, float4 &outcol)
void mix_diff(float fac, float4 col1, float4 col2, float4 &outcol)
void mix_div_fallback(float fac, float4 col1, float4 col2, float4 &outcol)
void mix_dark(float fac, float4 col1, float4 col2, float4 &outcol)
void mix_screen(float fac, float4 col1, float4 col2, float4 &outcol)
void mix_blend(float fac, float4 col1, float4 col2, float4 &outcol)
void mix_sub(float fac, float4 col1, float4 col2, float4 &outcol)
#define abs
constexpr T clamp(T, U, U) RET
#define mix
#define min(a, b)
Definition sort.cc:38
float y
Definition sky_math.h:225
float z
Definition sky_math.h:225
float x
Definition sky_math.h:225