Blender V4.5
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 ShaderData *sd,
97 ccl_private float *stack,
98 const uint4 node,
99 int offset)
100{
101 uint fac_offset;
102 uint color_offset;
103 uint alpha_offset;
104 const uint interpolate = node.z;
105
106 svm_unpack_node_uchar3(node.y, &fac_offset, &color_offset, &alpha_offset);
107
108 const uint table_size = read_node(kg, &offset).x;
109
110 const float fac = stack_load_float(stack, fac_offset);
111 const float4 color = rgb_ramp_lookup(kg, offset, fac, interpolate, false, table_size);
112
113 if (stack_valid(color_offset)) {
114 stack_store_float3(stack, color_offset, make_float3(color));
115 }
116 if (stack_valid(alpha_offset)) {
117 stack_store_float(stack, alpha_offset, color.w);
118 }
119
120 offset += table_size;
121 return offset;
122}
123
125 ccl_private ShaderData *sd,
126 ccl_private float *stack,
127 const uint4 node,
128 int offset)
129{
130 uint fac_offset;
131 uint color_offset;
132 uint out_offset;
133 uint extrapolate;
134 svm_unpack_node_uchar4(node.y, &fac_offset, &color_offset, &out_offset, &extrapolate);
135
136 const uint table_size = read_node(kg, &offset).x;
137
138 const float fac = stack_load_float(stack, fac_offset);
139 float3 color = stack_load_float3(stack, color_offset);
140
141 const float min_x = __int_as_float(node.z);
142 const float max_x = __int_as_float(node.w);
143 const float range_x = max_x - min_x;
144 const float3 relpos = (color - make_float3(min_x, min_x, min_x)) / range_x;
145
146 const float r = rgb_ramp_lookup(kg, offset, relpos.x, true, extrapolate, table_size).x;
147 const float g = rgb_ramp_lookup(kg, offset, relpos.y, true, extrapolate, table_size).y;
148 const float b = rgb_ramp_lookup(kg, offset, relpos.z, true, extrapolate, table_size).z;
149
150 color = (1.0f - fac) * color + fac * make_float3(r, g, b);
151 stack_store_float3(stack, out_offset, color);
152
153 offset += table_size;
154 return offset;
155}
156
158 ccl_private ShaderData *sd,
159 ccl_private float *stack,
160 const uint4 node,
161 int offset)
162{
163 uint fac_offset;
164 uint value_in_offset;
165 uint out_offset;
166 uint extrapolate;
167 svm_unpack_node_uchar4(node.y, &fac_offset, &value_in_offset, &out_offset, &extrapolate);
168
169 const uint table_size = read_node(kg, &offset).x;
170
171 const float fac = stack_load_float(stack, fac_offset);
172 float in = stack_load_float(stack, value_in_offset);
173
174 const float min = __int_as_float(node.z);
175 const float max = __int_as_float(node.w);
176 const float range = max - min;
177 const float relpos = (in - min) / range;
178
179 const float v = float_ramp_lookup(kg, offset, relpos, true, extrapolate, table_size);
180
181 in = (1.0f - fac) * in + fac * v;
182 stack_store_float(stack, out_offset, in);
183
184 offset += table_size;
185 return offset;
186}
187
unsigned int uint
ATTR_WARN_UNUSED_RESULT const BMVert * v
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)
VecBase< float, 4 > float4
#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 ShaderData *sd, ccl_private float *stack, const uint4 node, int offset)
Definition ramp.h:124
CCL_NAMESPACE_BEGIN ccl_device_inline float fetch_float(KernelGlobals kg, const int offset)
Definition ramp.h:13
ccl_device_noinline int svm_node_curve(KernelGlobals kg, ccl_private ShaderData *sd, ccl_private float *stack, const uint4 node, int offset)
Definition ramp.h:157
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 ShaderData *sd, 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_float3.h:27
float y
Definition sky_float3.h:27
float x
Definition sky_float3.h:27
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