Blender V4.3
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
8
9/* NOTE: svm_ramp.h, svm_ramp_util.h and node_ramp_util.h must stay consistent */
10
12{
13 uint4 node = kernel_data_fetch(svm_nodes, offset);
14 return __uint_as_float(node.x);
15}
16
18 KernelGlobals kg, int offset, float f, bool interpolate, bool extrapolate, int table_size)
19{
20 if ((f < 0.0f || f > 1.0f) && extrapolate) {
21 float t0, dy;
22 if (f < 0.0f) {
23 t0 = fetch_float(kg, offset);
24 dy = t0 - fetch_float(kg, offset + 1);
25 f = -f;
26 }
27 else {
28 t0 = fetch_float(kg, offset + table_size - 1);
29 dy = t0 - fetch_float(kg, offset + table_size - 2);
30 f = f - 1.0f;
31 }
32 return t0 + dy * f * (table_size - 1);
33 }
34
35 f = saturatef(f) * (table_size - 1);
36
37 /* clamp int as well in case of NaN */
38 int i = clamp(float_to_int(f), 0, table_size - 1);
39 float t = f - (float)i;
40
41 float a = fetch_float(kg, offset + i);
42
43 if (interpolate && t > 0.0f)
44 a = (1.0f - t) * a + t * fetch_float(kg, offset + i + 1);
45
46 return a;
47}
48
50 KernelGlobals kg, int offset, float f, bool interpolate, bool extrapolate, int table_size)
51{
52 if ((f < 0.0f || f > 1.0f) && extrapolate) {
53 float4 t0, dy;
54 if (f < 0.0f) {
55 t0 = fetch_node_float(kg, offset);
56 dy = t0 - fetch_node_float(kg, offset + 1);
57 f = -f;
58 }
59 else {
60 t0 = fetch_node_float(kg, offset + table_size - 1);
61 dy = t0 - fetch_node_float(kg, offset + table_size - 2);
62 f = f - 1.0f;
63 }
64 return t0 + dy * f * (table_size - 1);
65 }
66
67 f = saturatef(f) * (table_size - 1);
68
69 /* clamp int as well in case of NaN */
70 int i = clamp(float_to_int(f), 0, table_size - 1);
71 float t = f - (float)i;
72
73 float4 a = fetch_node_float(kg, offset + i);
74
75 if (interpolate && t > 0.0f)
76 a = (1.0f - t) * a + t * fetch_node_float(kg, offset + i + 1);
77
78 return a;
79}
80
82 KernelGlobals kg, ccl_private ShaderData *sd, ccl_private float *stack, uint4 node, int offset)
83{
84 uint fac_offset, color_offset, alpha_offset;
85 uint interpolate = node.z;
86
87 svm_unpack_node_uchar3(node.y, &fac_offset, &color_offset, &alpha_offset);
88
89 uint table_size = read_node(kg, &offset).x;
90
91 float fac = stack_load_float(stack, fac_offset);
92 float4 color = rgb_ramp_lookup(kg, offset, fac, interpolate, false, table_size);
93
94 if (stack_valid(color_offset))
95 stack_store_float3(stack, color_offset, float4_to_float3(color));
96 if (stack_valid(alpha_offset))
97 stack_store_float(stack, alpha_offset, color.w);
98
99 offset += table_size;
100 return offset;
101}
102
104 KernelGlobals kg, ccl_private ShaderData *sd, ccl_private float *stack, uint4 node, int offset)
105{
106 uint fac_offset, color_offset, out_offset, extrapolate;
107 svm_unpack_node_uchar4(node.y, &fac_offset, &color_offset, &out_offset, &extrapolate);
108
109 uint table_size = read_node(kg, &offset).x;
110
111 float fac = stack_load_float(stack, fac_offset);
112 float3 color = stack_load_float3(stack, color_offset);
113
114 const float min_x = __int_as_float(node.z), max_x = __int_as_float(node.w);
115 const float range_x = max_x - min_x;
116 const float3 relpos = (color - make_float3(min_x, min_x, min_x)) / range_x;
117
118 float r = rgb_ramp_lookup(kg, offset, relpos.x, true, extrapolate, table_size).x;
119 float g = rgb_ramp_lookup(kg, offset, relpos.y, true, extrapolate, table_size).y;
120 float b = rgb_ramp_lookup(kg, offset, relpos.z, true, extrapolate, table_size).z;
121
122 color = (1.0f - fac) * color + fac * make_float3(r, g, b);
123 stack_store_float3(stack, out_offset, color);
124
125 offset += table_size;
126 return offset;
127}
128
130 KernelGlobals kg, ccl_private ShaderData *sd, ccl_private float *stack, uint4 node, int offset)
131{
132 uint fac_offset, value_in_offset, out_offset, extrapolate;
133 svm_unpack_node_uchar4(node.y, &fac_offset, &value_in_offset, &out_offset, &extrapolate);
134
135 uint table_size = read_node(kg, &offset).x;
136
137 float fac = stack_load_float(stack, fac_offset);
138 float in = stack_load_float(stack, value_in_offset);
139
140 const float min = __int_as_float(node.z), max = __int_as_float(node.w);
141 const float range = max - min;
142 const float relpos = (in - min) / range;
143
144 float v = float_ramp_lookup(kg, offset, relpos, true, extrapolate, table_size);
145
146 in = (1.0f - fac) * in + fac * v;
147 stack_store_float(stack, out_offset, in);
148
149 offset += table_size;
150 return offset;
151}
152
unsigned int uint
Group Output data from inside of a node group A color picker Mix two input colors RGB to Convert a color s luminance to a grayscale value Generate a normal vector and a dot product Brightness Control the brightness and contrast of the input color Vector Map input vector components with curves Camera Retrieve information about the camera and how it relates to the current shading point s position Clamp a value between a minimum and a maximum Vector Perform vector math operation Invert Invert a color
ATTR_WARN_UNUSED_RESULT const BMVert * v
local_group_size(16, 16) .push_constant(Type b
const KernelGlobalsCPU *ccl_restrict KernelGlobals
#define kernel_data_fetch(name, index)
#define ccl_private
#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)
draw_view in_light_buf[] float
ccl_device_inline void stack_store_float3(ccl_private float *stack, uint a, float3 f)
ccl_device_inline float4 fetch_node_float(KernelGlobals kg, int offset)
CCL_NAMESPACE_BEGIN ccl_device_inline float3 stack_load_float3(ccl_private float *stack, uint a)
ccl_device_inline uint4 read_node(KernelGlobals kg, ccl_private int *offset)
ccl_device_forceinline void svm_unpack_node_uchar3(uint i, ccl_private uint *x, ccl_private uint *y, ccl_private uint *z)
ccl_device_inline void stack_store_float(ccl_private float *stack, uint a, float f)
ccl_device_inline float stack_load_float(ccl_private float *stack, uint a)
ccl_device_forceinline void svm_unpack_node_uchar4(uint i, ccl_private uint *x, ccl_private uint *y, ccl_private uint *z, ccl_private uint *w)
ccl_device_inline bool stack_valid(uint a)
ShaderData
ccl_device_inline float float_ramp_lookup(KernelGlobals kg, int offset, float f, bool interpolate, bool extrapolate, int table_size)
Definition ramp.h:17
ccl_device_inline float4 rgb_ramp_lookup(KernelGlobals kg, int offset, float f, bool interpolate, bool extrapolate, int table_size)
Definition ramp.h:49
CCL_NAMESPACE_BEGIN ccl_device_inline float fetch_float(KernelGlobals kg, int offset)
Definition ramp.h:11
ccl_device_noinline int svm_node_rgb_ramp(KernelGlobals kg, ccl_private ShaderData *sd, ccl_private float *stack, uint4 node, int offset)
Definition ramp.h:81
ccl_device_noinline int svm_node_curves(KernelGlobals kg, ccl_private ShaderData *sd, ccl_private float *stack, uint4 node, int offset)
Definition ramp.h:103
ccl_device_noinline int svm_node_curve(KernelGlobals kg, ccl_private ShaderData *sd, ccl_private float *stack, uint4 node, int offset)
Definition ramp.h:129
#define min(a, b)
Definition sort.c:32
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:15
uint y
Definition types_uint4.h:15
uint z
Definition types_uint4.h:15
uint w
Definition types_uint4.h:15
VecBase< float, 4 > float4
float max
ccl_device_inline int float_to_int(float f)
Definition util/math.h:424
ccl_device_inline float3 float4_to_float3(const float4 a)
Definition util/math.h:535
ccl_device_inline int clamp(int a, int mn, int mx)
Definition util/math.h:379