Blender V5.3
gpu_shader_common_curves.bsl.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2022 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
8
9float4 white_balance(float4 color, float4 black_level, float4 white_level)
10{
11 float4 range = max(white_level - black_level, float4(1e-5f));
12 return (color - black_level) / range;
13}
14
15float extrapolate_if_needed(float parameter, float value, float start_slope, float end_slope)
16{
17 if (parameter < 0.0f) {
18 return value + parameter * start_slope;
19 }
20
21 if (parameter > 1.0f) {
22 return value + (parameter - 1.0f) * end_slope;
23 }
24
25 return value;
26}
27
28/* Same as extrapolate_if_needed but vectorized. */
30 float3 values,
31 float3 start_slopes,
32 float3 end_slopes)
33{
34 float3 end_or_zero_slopes = mix(float3(0.0f), end_slopes, greaterThan(parameters, float3(1.0f)));
35 float3 slopes = mix(end_or_zero_slopes, start_slopes, lessThan(parameters, float3(0.0f)));
36 parameters = parameters - mix(float3(0.0f), float3(1.0f), greaterThan(parameters, float3(1.0f)));
37 return values + parameters * slopes;
38}
39
40/* Curve maps are stored in texture samplers, so ensure that the parameters evaluate the sampler at
41 * the center of the pixels, because samplers are evaluated using linear interpolation. Given the
42 * parameter in the [0, 1] range. */
43float compute_curve_map_coordinates(float parameter)
44{
45 /* Curve maps have a fixed width of 257. We offset by the equivalent of half a pixel and scale
46 * down such that the normalized parameter 1.0 corresponds to the center of the last pixel. */
47 float sampler_offset = 0.5f / 257.0f;
48 float sampler_scale = 1.0f - (1.0f / 257.0f);
49 return parameter * sampler_scale + sampler_offset;
50}
51
52/* Same as compute_curve_map_coordinates but vectorized. */
54{
55 float sampler_offset = 0.5f / 257.0f;
56 float sampler_scale = 1.0f - (1.0f / 257.0f);
57 return parameters * sampler_scale + sampler_offset;
58}
59
60[[node]]
61void curves_combined_rgb(float factor,
62 float4 color,
63 float4 black_level,
64 float4 white_level,
65 sampler1DArray curve_map,
66 const float layer,
67 float4 range_minimums,
68 float4 range_dividers,
69 float4 start_slopes,
70 float4 end_slopes,
71 float4 &result)
72{
73 float4 balanced = white_balance(color, black_level, white_level);
74
75 /* First, evaluate alpha curve map at all channels. The alpha curve is the Combined curve in the
76 * UI. The channels are first normalized into the [0, 1] range. */
77 float3 parameters = (balanced.rgb - range_minimums.aaa) * range_dividers.aaa;
78 float3 coordinates = compute_curve_map_coordinates(parameters);
79 result.r = texture(curve_map, float2(coordinates.x, layer)).a;
80 result.g = texture(curve_map, float2(coordinates.y, layer)).a;
81 result.b = texture(curve_map, float2(coordinates.z, layer)).a;
82
83 /* Then, extrapolate if needed. */
84 result.rgb = extrapolate_if_needed(parameters, result.rgb, start_slopes.aaa, end_slopes.aaa);
85
86 /* Then, evaluate each channel on its curve map. The channels are first normalized into the
87 * [0, 1] range. */
88 parameters = (result.rgb - range_minimums.rgb) * range_dividers.rgb;
89 coordinates = compute_curve_map_coordinates(parameters);
90 result.r = texture(curve_map, float2(coordinates.r, layer)).r;
91 result.g = texture(curve_map, float2(coordinates.g, layer)).g;
92 result.b = texture(curve_map, float2(coordinates.b, layer)).b;
93
94 /* Then, extrapolate again if needed. */
95 result.rgb = extrapolate_if_needed(parameters, result.rgb, start_slopes.rgb, end_slopes.rgb);
96
97 result.a = color.a;
98
99 result = mix(color, result, factor);
100}
101
102[[node]]
104 float factor,
105 float4 black_level,
106 float4 white_level,
107 sampler1DArray curve_map,
108 const float layer,
109 float4 range_minimums,
110 float4 range_dividers,
111 float4 start_slopes,
112 float4 end_slopes,
113 float4 &result)
114{
115 curves_combined_rgb(factor,
116 color,
117 black_level,
118 white_level,
119 curve_map,
120 layer,
121 range_minimums,
122 range_dividers,
123 start_slopes,
124 end_slopes,
125 result);
126}
127
128[[node]]
129void curves_combined_only(float factor,
130 float4 color,
131 float4 black_level,
132 float4 white_level,
133 sampler1DArray curve_map,
134 const float layer,
135 float range_minimum,
136 float range_divider,
137 float start_slope,
138 float end_slope,
139 float4 &result)
140{
141 float4 balanced = white_balance(color, black_level, white_level);
142
143 /* Evaluate alpha curve map at all channels. The alpha curve is the Combined curve in the
144 * UI. The channels are first normalized into the [0, 1] range. */
145 float3 parameters = (balanced.rgb - float3(range_minimum)) * float3(range_divider);
146 float3 coordinates = compute_curve_map_coordinates(parameters);
147 result.r = texture(curve_map, float2(coordinates.x, layer)).a;
148 result.g = texture(curve_map, float2(coordinates.y, layer)).a;
149 result.b = texture(curve_map, float2(coordinates.z, layer)).a;
150
151 /* Then, extrapolate if needed. */
152 result.rgb = extrapolate_if_needed(
153 parameters, result.rgb, float3(start_slope), float3(end_slope));
154
155 result.a = color.a;
156
157 result = mix(color, result, factor);
158}
159
160[[node]]
162 float factor,
163 float4 black_level,
164 float4 white_level,
165 sampler1DArray curve_map,
166 const float layer,
167 float range_minimum,
168 float range_divider,
169 float start_slope,
170 float end_slope,
171 float4 &result)
172{
174 color,
175 black_level,
176 white_level,
177 curve_map,
178 layer,
179 range_minimum,
180 range_divider,
181 start_slope,
182 end_slope,
183 result);
184}
185
186/* Contrary to standard tone curve implementations, the film-like implementation tries to preserve
187 * the hue of the colors as much as possible. To understand why this might be a problem, consider
188 * the violet color (0.5, 0.0, 1.0). If this color was to be evaluated at a power curve x^4, the
189 * color will be blue (0.0625, 0.0, 1.0). So the color changes and not just its luminosity,
190 * which is what film-like tone curves tries to avoid.
191 *
192 * First, the channels with the lowest and highest values are identified and evaluated at the
193 * curve. Then, the third channel---the median---is computed while maintaining the original hue of
194 * the color. To do that, we look at the equation for deriving the hue from RGB values. Assuming
195 * the maximum, minimum, and median channels are known, and ignoring the 1/3 period offset of the
196 * hue, the equation is:
197 *
198 * hue = (median - min) / (max - min) [1]
199 *
200 * Since we have the new values for the minimum and maximum after evaluating at the curve, we also
201 * have:
202 *
203 * hue = (new_median - new_min) / (new_max - new_min) [2]
204 *
205 * Since we want the hue to be equivalent, by equating [1] and [2] and rearranging:
206 *
207 * (new_median - new_min) / (new_max - new_min) = (median - min) / (max - min)
208 * new_median - new_min = (new_max - new_min) * (median - min) / (max - min)
209 * new_median = new_min + (new_max - new_min) * (median - min) / (max - min)
210 * new_median = new_min + (median - min) * ((new_max - new_min) / (max - min)) [QED]
211 *
212 * Which gives us the median color that preserves the hue. More intuitively, the median is computed
213 * such that the change in the distance from the median to the minimum is proportional to the
214 * change in the distance from the minimum to the maximum. Finally, each of the new minimum,
215 * maximum, and median values are written to the color channel that they were originally extracted
216 * from. */
217[[node]]
218void curves_film_like(float factor,
219 float4 color,
220 float4 black_level,
221 float4 white_level,
222 sampler1DArray curve_map,
223 const float layer,
224 float range_minimum,
225 float range_divider,
226 float start_slope,
227 float end_slope,
228 float4 &result)
229{
230 float4 balanced = white_balance(color, black_level, white_level);
231
232 /* Find the maximum, minimum, and median of the color channels. */
233 float minimum = min(balanced.r, min(balanced.g, balanced.b));
234 float maximum = max(balanced.r, max(balanced.g, balanced.b));
235 float median = max(min(balanced.r, balanced.g), min(balanced.b, max(balanced.r, balanced.g)));
236
237 /* Evaluate alpha curve map at the maximum and minimum channels. The alpha curve is the Combined
238 * curve in the UI. The channels are first normalized into the [0, 1] range. */
239 float min_parameter = (minimum - range_minimum) * range_divider;
240 float max_parameter = (maximum - range_minimum) * range_divider;
241 float min_coordinates = compute_curve_map_coordinates(min_parameter);
242 float max_coordinates = compute_curve_map_coordinates(max_parameter);
243 float new_min = texture(curve_map, float2(min_coordinates, layer)).a;
244 float new_max = texture(curve_map, float2(max_coordinates, layer)).a;
245
246 /* Then, extrapolate if needed. */
247 new_min = extrapolate_if_needed(min_parameter, new_min, start_slope, end_slope);
248 new_max = extrapolate_if_needed(max_parameter, new_max, start_slope, end_slope);
249
250 /* Compute the new median using the ratio between the new and the original range. */
251 float scaling_ratio = (new_max - new_min) / (maximum - minimum);
252 float new_median = new_min + (median - minimum) * scaling_ratio;
253
254 /* Write each value to its original channel. */
255 bool3 channel_is_min = equal(balanced.rgb, float3(minimum));
256 float3 median_or_min = mix(float3(new_median), float3(new_min), channel_is_min);
257 bool3 channel_is_max = equal(balanced.rgb, float3(maximum));
258 result.rgb = mix(median_or_min, float3(new_max), channel_is_max);
259
260 result.a = color.a;
261
262 result = mix(color, result, clamp(factor, 0.0f, 1.0f));
263}
264
265[[node]]
267 float factor,
268 float4 black_level,
269 float4 white_level,
270 sampler1DArray curve_map,
271 const float layer,
272 float range_minimum,
273 float range_divider,
274 float start_slope,
275 float end_slope,
276 float4 &result)
277{
278 curves_film_like(factor,
279 color,
280 black_level,
281 white_level,
282 curve_map,
283 layer,
284 range_minimum,
285 range_divider,
286 start_slope,
287 end_slope,
288 result);
289}
290
291[[node]]
293 sampler1DArray curve_map,
294 const float layer,
295 float3 range_minimums,
296 float3 range_dividers,
297 float3 start_slopes,
298 float3 end_slopes,
299 float3 &result)
300{
301 /* Evaluate each component on its curve map.
302 * The components are first normalized into the [0, 1] range. */
303 float3 parameters = (vector - range_minimums) * range_dividers;
304 float3 coordinates = compute_curve_map_coordinates(parameters);
305 result.x = texture(curve_map, float2(coordinates.x, layer)).x;
306 result.y = texture(curve_map, float2(coordinates.y, layer)).y;
307 result.z = texture(curve_map, float2(coordinates.z, layer)).z;
308
309 /* Then, extrapolate if needed. */
310 result = extrapolate_if_needed(parameters, result, start_slopes, end_slopes);
311}
312
313[[node]]
314void curves_vector_mixed(float factor,
316 sampler1DArray curve_map,
317 const float layer,
318 float3 range_minimums,
319 float3 range_dividers,
320 float3 start_slopes,
321 float3 end_slopes,
322 float3 &result)
323{
325 vector, curve_map, layer, range_minimums, range_dividers, start_slopes, end_slopes, result);
326 result = mix(vector, result, factor);
327}
328
329[[node]]
330void curves_float(float value,
331 sampler1DArray curve_map,
332 const float layer,
333 float range_minimum,
334 float range_divider,
335 float start_slope,
336 float end_slope,
337 float &result)
338{
339 /* Evaluate the normalized value on the first curve map. */
340 float parameter = (value - range_minimum) * range_divider;
341 float coordinates = compute_curve_map_coordinates(parameter);
342 result = texture(curve_map, float2(coordinates, layer)).x;
343
344 /* Then, extrapolate if needed. */
345 result = extrapolate_if_needed(parameter, result, start_slope, end_slope);
346}
347
348[[node]]
349void curves_float_mixed(float factor,
350 float value,
351 sampler1DArray curve_map,
352 const float layer,
353 float range_minimum,
354 float range_divider,
355 float start_slope,
356 float end_slope,
357 float &result)
358{
360 value, curve_map, layer, range_minimum, range_divider, start_slope, end_slope, result);
361 result = mix(value, result, factor);
362}
btScalar max
float compute_curve_map_coordinates(float parameter)
void curves_film_like(float factor, float4 color, float4 black_level, float4 white_level, sampler1DArray curve_map, const float layer, float range_minimum, float range_divider, float start_slope, float end_slope, float4 &result)
void curves_film_like_compositor(float4 color, float factor, float4 black_level, float4 white_level, sampler1DArray curve_map, const float layer, float range_minimum, float range_divider, float start_slope, float end_slope, float4 &result)
float4 white_balance(float4 color, float4 black_level, float4 white_level)
void curves_vector_mixed(float factor, float3 vector, sampler1DArray curve_map, const float layer, float3 range_minimums, float3 range_dividers, float3 start_slopes, float3 end_slopes, float3 &result)
void curves_combined_only_compositor(float4 color, float factor, float4 black_level, float4 white_level, sampler1DArray curve_map, const float layer, float range_minimum, float range_divider, float start_slope, float end_slope, float4 &result)
float extrapolate_if_needed(float parameter, float value, float start_slope, float end_slope)
void curves_float(float value, sampler1DArray curve_map, const float layer, float range_minimum, float range_divider, float start_slope, float end_slope, float &result)
void curves_vector(float3 vector, sampler1DArray curve_map, const float layer, float3 range_minimums, float3 range_dividers, float3 start_slopes, float3 end_slopes, float3 &result)
void curves_combined_only(float factor, float4 color, float4 black_level, float4 white_level, sampler1DArray curve_map, const float layer, float range_minimum, float range_divider, float start_slope, float end_slope, float4 &result)
void curves_combined_rgb_compositor(float4 color, float factor, float4 black_level, float4 white_level, sampler1DArray curve_map, const float layer, float4 range_minimums, float4 range_dividers, float4 start_slopes, float4 end_slopes, float4 &result)
void curves_combined_rgb(float factor, float4 color, float4 black_level, float4 white_level, sampler1DArray curve_map, const float layer, float4 range_minimums, float4 range_dividers, float4 start_slopes, float4 end_slopes, float4 &result)
void curves_float_mixed(float factor, float value, sampler1DArray curve_map, const float layer, float range_minimum, float range_divider, float start_slope, float end_slope, float &result)
#define layer
VecBase< bool, D > greaterThan(VecOp< T, D >, VecOp< T, D >) RET
VecBase< bool, D > lessThan(VecOp< T, D >, VecOp< T, D >) RET
VecBase< bool, D > equal(VecOp< T, D >, VecOp< T, D >) RET
constexpr T clamp(T, U, U) RET
SamplerBase< float, 1, false, true > sampler1DArray
TEX_TEMPLATE DataVec texture(T, FltCoord, float=0.0f) RET
VecBase< bool, 3 > bool3
#define mix
#define min(a, b)
Definition sort.cc:38
float z
Definition sky_math.h:136
float y
Definition sky_math.h:136
float x
Definition sky_math.h:136