Blender V5.0
ramp.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 "kernel/svm/util.h"
8
10
11/* NOTE: svm_ramp.h, svm_ramp_util.h and node_ramp_util.h must stay consistent */
12
13ccl_device_inline float fetch_float(KernelGlobals kg, const int offset)
14{
15 const uint4 node = kernel_data_fetch(svm_nodes, offset);
16 return __uint_as_float(node.x);
17}
18
20 const int offset,
21 float f,
22 bool interpolate,
23 bool extrapolate,
24 const int table_size)
25{
26 if ((f < 0.0f || f > 1.0f) && extrapolate) {
27 float t0;
28 float dy;
29 if (f < 0.0f) {
30 t0 = fetch_float(kg, offset);
31 dy = t0 - fetch_float(kg, offset + 1);
32 f = -f;
33 }
34 else {
35 t0 = fetch_float(kg, offset + table_size - 1);
36 dy = t0 - fetch_float(kg, offset + table_size - 2);
37 f = f - 1.0f;
38 }
39 return t0 + dy * f * (table_size - 1);
40 }
41
42 f = saturatef(f) * (table_size - 1);
43
44 /* clamp int as well in case of NaN */
45 const int i = clamp(float_to_int(f), 0, table_size - 1);
46 const float t = f - (float)i;
47
48 float a = fetch_float(kg, offset + i);
49
50 if (interpolate && t > 0.0f) {
51 a = (1.0f - t) * a + t * fetch_float(kg, offset + i + 1);
52 }
53
54 return a;
55}
56
58 const int offset,
59 float f,
60 bool interpolate,
61 bool extrapolate,
62 const int table_size)
63{
64 if ((f < 0.0f || f > 1.0f) && extrapolate) {
65 float4 t0;
66 float4 dy;
67 if (f < 0.0f) {
68 t0 = fetch_node_float(kg, offset);
69 dy = t0 - fetch_node_float(kg, offset + 1);
70 f = -f;
71 }
72 else {
73 t0 = fetch_node_float(kg, offset + table_size - 1);
74 dy = t0 - fetch_node_float(kg, offset + table_size - 2);
75 f = f - 1.0f;
76 }
77 return t0 + dy * f * (table_size - 1);
78 }
79
80 f = saturatef(f) * (table_size - 1);
81
82 /* clamp int as well in case of NaN */
83 const int i = clamp(float_to_int(f), 0, table_size - 1);
84 const float t = f - (float)i;
85
86 float4 a = fetch_node_float(kg, offset + i);
87
88 if (interpolate && t > 0.0f) {
89 a = (1.0f - t) * a + t * fetch_node_float(kg, offset + i + 1);
90 }
91
92 return a;
93}
94
96 ccl_private float *stack,
97 const uint4 node,
98 int offset)
99{
100 uint fac_offset;
101 uint color_offset;
102 uint alpha_offset;
103 const uint interpolate = node.z;
104
105 svm_unpack_node_uchar3(node.y, &fac_offset, &color_offset, &alpha_offset);
106
107 const uint table_size = read_node(kg, &offset).x;
108
109 const float fac = stack_load_float(stack, fac_offset);
110 const float4 color = rgb_ramp_lookup(kg, offset, fac, interpolate, false, table_size);
111
112 if (stack_valid(color_offset)) {
113 stack_store_float3(stack, color_offset, make_float3(color));
114 }
115 if (stack_valid(alpha_offset)) {
116 stack_store_float(stack, alpha_offset, color.w);
117 }
118
119 offset += table_size;
120 return offset;
121}
122
124 ccl_private float *stack,
125 const uint4 node,
126 int offset)
127{
128 uint fac_offset;
129 uint color_offset;
130 uint out_offset;
131 uint extrapolate;
132 svm_unpack_node_uchar4(node.y, &fac_offset, &color_offset, &out_offset, &extrapolate);
133
134 const uint table_size = read_node(kg, &offset).x;
135
136 const float fac = stack_load_float(stack, fac_offset);
137 float3 color = stack_load_float3(stack, color_offset);
138
139 const float min_x = __int_as_float(node.z);
140 const float max_x = __int_as_float(node.w);
141 const float range_x = max_x - min_x;
142 const float3 relpos = (color - make_float3(min_x, min_x, min_x)) / range_x;
143
144 const float r = rgb_ramp_lookup(kg, offset, relpos.x, true, extrapolate, table_size).x;
145 const float g = rgb_ramp_lookup(kg, offset, relpos.y, true, extrapolate, table_size).y;
146 const float b = rgb_ramp_lookup(kg, offset, relpos.z, true, extrapolate, table_size).z;
147
148 color = (1.0f - fac) * color + fac * make_float3(r, g, b);
149 stack_store_float3(stack, out_offset, color);
150
151 offset += table_size;
152 return offset;
153}
154
156 ccl_private float *stack,
157 const uint4 node,
158 int offset)
159{
160 uint fac_offset;
161 uint value_in_offset;
162 uint out_offset;
163 uint extrapolate;
164 svm_unpack_node_uchar4(node.y, &fac_offset, &value_in_offset, &out_offset, &extrapolate);
165
166 const uint table_size = read_node(kg, &offset).x;
167
168 const float fac = stack_load_float(stack, fac_offset);
169 float in = stack_load_float(stack, value_in_offset);
170
171 const float min = __int_as_float(node.z);
172 const float max = __int_as_float(node.w);
173 const float range = max - min;
174 const float relpos = (in - min) / range;
175
176 const float v = float_ramp_lookup(kg, offset, relpos, true, extrapolate, table_size);
177
178 in = (1.0f - fac) * in + fac * v;
179 stack_store_float(stack, out_offset, in);
180
181 offset += table_size;
182 return offset;
183}
184
unsigned int uint
ATTR_WARN_UNUSED_RESULT const BMVert * v
nullptr float
ccl_device_inline float4 fetch_node_float(KernelGlobals kg, const int offset)
ccl_device_inline float stack_load_float(const ccl_private float *stack, const uint a)
ccl_device_inline void stack_store_float(ccl_private float *stack, const uint a, const float f)
ccl_device_inline uint4 read_node(KernelGlobals kg, ccl_private int *const offset)
ccl_device_inline void stack_store_float3(ccl_private float *stack, const uint a, const float3 f)
ccl_device_forceinline void svm_unpack_node_uchar3(const uint i, ccl_private uint *x, ccl_private uint *y, ccl_private uint *z)
ccl_device_forceinline void svm_unpack_node_uchar4(const uint i, ccl_private uint *x, ccl_private uint *y, ccl_private uint *z, ccl_private uint *w)
ccl_device_inline bool stack_valid(const uint a)
CCL_NAMESPACE_BEGIN ccl_device_inline float3 stack_load_float3(const ccl_private float *stack, const uint a)
#define kernel_data_fetch(name, index)
#define ccl_private
const ThreadKernelGlobalsCPU * KernelGlobals
#define ccl_device_inline
#define ccl_device_noinline
#define CCL_NAMESPACE_END
#define saturatef(x)
ccl_device_forceinline float3 make_float3(const float x, const float y, const float z)
#define __int_as_float(x)
#define __uint_as_float(x)
#define in
constexpr T clamp(T, U, U) RET
ccl_device_inline int float_to_int(const float f)
Definition math_base.h:407
ccl_device_noinline int svm_node_curves(KernelGlobals kg, ccl_private float *stack, const uint4 node, int offset)
Definition ramp.h:123
ccl_device_noinline int svm_node_curve(KernelGlobals kg, ccl_private float *stack, const uint4 node, int offset)
Definition ramp.h:155
CCL_NAMESPACE_BEGIN ccl_device_inline float fetch_float(KernelGlobals kg, const int offset)
Definition ramp.h:13
ccl_device_inline float float_ramp_lookup(KernelGlobals kg, const int offset, float f, bool interpolate, bool extrapolate, const int table_size)
Definition ramp.h:19
ccl_device_noinline int svm_node_rgb_ramp(KernelGlobals kg, ccl_private float *stack, const uint4 node, int offset)
Definition ramp.h:95
ccl_device_inline float4 rgb_ramp_lookup(KernelGlobals kg, const int offset, float f, bool interpolate, bool extrapolate, const int table_size)
Definition ramp.h:57
#define min(a, b)
Definition sort.cc:36
float z
Definition sky_math.h:136
float y
Definition sky_math.h:136
float x
Definition sky_math.h:136
float y
Definition sky_math.h:225
float z
Definition sky_math.h:225
float x
Definition sky_math.h:225
float w
Definition sky_math.h:225
uint x
Definition types_uint4.h:13
uint y
Definition types_uint4.h:13
uint z
Definition types_uint4.h:13
uint w
Definition types_uint4.h:13
i
Definition text_draw.cc:230
max
Definition text_draw.cc:251