Blender V4.3
node_ramp_util.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5/* NOTE: svm_ramp.h, svm_ramp_util.h and node_ramp_util.h must stay consistent */
6
7color rgb_ramp_lookup(color ramp[], float at, int interpolate, int extrapolate)
8{
9 float f = at;
10 int table_size = arraylength(ramp);
11
12 if ((f < 0.0 || f > 1.0) && extrapolate) {
13 color t0, dy;
14 if (f < 0.0) {
15 t0 = ramp[0];
16 dy = t0 - ramp[1];
17 f = -f;
18 }
19 else {
20 t0 = ramp[table_size - 1];
21 dy = t0 - ramp[table_size - 2];
22 f = f - 1.0;
23 }
24 return t0 + dy * f * (table_size - 1);
25 }
26
27 f = clamp(at, 0.0, 1.0) * (table_size - 1);
28
29 /* clamp int as well in case of NaN */
30 int i = (int)f;
31 if (i < 0) {
32 i = 0;
33 }
34 if (i >= table_size) {
35 i = table_size - 1;
36 }
37 float t = f - (float)i;
38
39 color result = ramp[i];
40
41 if (interpolate && t > 0.0) {
42 result = (1.0 - t) * result + t * ramp[i + 1];
43 }
44
45 return result;
46}
47
48float rgb_ramp_lookup(float ramp[], float at, int interpolate, int extrapolate)
49{
50 float f = at;
51 int table_size = arraylength(ramp);
52
53 if ((f < 0.0 || f > 1.0) && extrapolate) {
54 float t0, dy;
55 if (f < 0.0) {
56 t0 = ramp[0];
57 dy = t0 - ramp[1];
58 f = -f;
59 }
60 else {
61 t0 = ramp[table_size - 1];
62 dy = t0 - ramp[table_size - 2];
63 f = f - 1.0;
64 }
65 return t0 + dy * f * (table_size - 1);
66 }
67
68 f = clamp(at, 0.0, 1.0) * (table_size - 1);
69
70 /* clamp int as well in case of NaN */
71 int i = (int)f;
72 if (i < 0) {
73 i = 0;
74 }
75 if (i >= table_size) {
76 i = table_size - 1;
77 }
78 float t = f - (float)i;
79
80 float result = ramp[i];
81
82 if (interpolate && t > 0.0) {
83 result = (1.0 - t) * result + t * ramp[i + 1];
84 }
85
86 return result;
87}
draw_view in_light_buf[] float
draw_view push_constant(Type::INT, "radiance_src") .push_constant(Type capture_info_buf storage_buf(1, Qualifier::READ, "ObjectBounds", "bounds_buf[]") .push_constant(Type draw_view int
color rgb_ramp_lookup(color ramp[], float at, int interpolate, int extrapolate)
ccl_device_inline int clamp(int a, int mn, int mx)
Definition util/math.h:379