Blender V5.0
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#pragma once
6
7#include "util/math.h"
8#include "util/types.h"
9
11
12/* NOTE: svm_ramp.h, svm_ramp_util.h and node_ramp_util.h must stay consistent */
13
15 const float3 *ramp, float f, bool interpolate, bool extrapolate, const int table_size)
16{
17 if ((f < 0.0f || f > 1.0f) && extrapolate) {
18 float3 t0;
19 float3 dy;
20 if (f < 0.0f) {
21 t0 = ramp[0];
22 dy = t0 - ramp[1], f = -f;
23 }
24 else {
25 t0 = ramp[table_size - 1];
26 dy = t0 - ramp[table_size - 2];
27 f = f - 1.0f;
28 }
29 return t0 + dy * f * (table_size - 1);
30 }
31
32 f = clamp(f, 0.0f, 1.0f) * (table_size - 1);
33
34 /* clamp int as well in case of NaN */
35 const int i = clamp(float_to_int(f), 0, table_size - 1);
36 const float t = f - (float)i;
37
38 float3 result = ramp[i];
39
40 if (interpolate && t > 0.0f) {
41 result = (1.0f - t) * result + t * ramp[i + 1];
42 }
43
44 return result;
45}
46
48 const float *ramp, float f, bool interpolate, bool extrapolate, const int table_size)
49{
50 if ((f < 0.0f || f > 1.0f) && extrapolate) {
51 float t0;
52 float dy;
53 if (f < 0.0f) {
54 t0 = ramp[0];
55 dy = t0 - ramp[1], f = -f;
56 }
57 else {
58 t0 = ramp[table_size - 1];
59 dy = t0 - ramp[table_size - 2];
60 f = f - 1.0f;
61 }
62 return t0 + dy * f * (table_size - 1);
63 }
64
65 f = clamp(f, 0.0f, 1.0f) * (table_size - 1);
66
67 /* clamp int as well in case of NaN */
68 const int i = clamp(float_to_int(f), 0, table_size - 1);
69 const float t = f - (float)i;
70
71 float result = ramp[i];
72
73 if (interpolate && t > 0.0f) {
74 result = (1.0f - t) * result + t * ramp[i + 1];
75 }
76
77 return result;
78}
79
nullptr float
#define ccl_device_inline
#define CCL_NAMESPACE_END
constexpr T clamp(T, U, U) RET
ccl_device_inline int float_to_int(const float f)
Definition math_base.h:407
#define ccl_device
CCL_NAMESPACE_BEGIN ccl_device_inline float3 rgb_ramp_lookup(const float3 *ramp, float f, bool interpolate, bool extrapolate, const int table_size)
Definition ramp_util.h:14
ccl_device float float_ramp_lookup(const float *ramp, float f, bool interpolate, bool extrapolate, const int table_size)
Definition ramp_util.h:47
i
Definition text_draw.cc:230