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