2022-02-11 13:53:21 +01:00
|
|
|
/* SPDX-License-Identifier: Apache-2.0
|
|
|
|
* Copyright 2011-2022 Blender Foundation */
|
2016-07-30 23:30:36 +02:00
|
|
|
|
2021-10-24 14:19:19 +02:00
|
|
|
#pragma once
|
2016-07-30 23:30:36 +02:00
|
|
|
|
|
|
|
CCL_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
/* NOTE: svm_ramp.h, svm_ramp_util.h and node_ramp_util.h must stay consistent */
|
|
|
|
|
2016-08-01 15:40:46 +02:00
|
|
|
ccl_device_inline float3
|
|
|
|
rgb_ramp_lookup(const float3 *ramp, float f, bool interpolate, bool extrapolate, int table_size)
|
2016-07-30 23:30:36 +02:00
|
|
|
{
|
2016-10-24 12:26:12 +02:00
|
|
|
if ((f < 0.0f || f > 1.0f) && extrapolate) {
|
2016-07-30 23:30:36 +02:00
|
|
|
float3 t0, dy;
|
2016-10-24 12:26:12 +02:00
|
|
|
if (f < 0.0f) {
|
2016-07-30 23:30:36 +02:00
|
|
|
t0 = ramp[0];
|
|
|
|
dy = t0 - ramp[1], f = -f;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
t0 = ramp[table_size - 1];
|
|
|
|
dy = t0 - ramp[table_size - 2];
|
|
|
|
f = f - 1.0f;
|
|
|
|
}
|
|
|
|
return t0 + dy * f * (table_size - 1);
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-07-30 23:30:36 +02:00
|
|
|
f = clamp(f, 0.0f, 1.0f) * (table_size - 1);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-07-30 23:30:36 +02:00
|
|
|
/* clamp int as well in case of NaN */
|
|
|
|
int i = clamp(float_to_int(f), 0, table_size - 1);
|
|
|
|
float t = f - (float)i;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-07-30 23:30:36 +02:00
|
|
|
float3 result = ramp[i];
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-10-24 12:26:12 +02:00
|
|
|
if (interpolate && t > 0.0f) {
|
2016-07-30 23:30:36 +02:00
|
|
|
result = (1.0f - t) * result + t * ramp[i + 1];
|
2016-10-24 12:26:12 +02:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-07-30 23:30:36 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
ccl_device float float_ramp_lookup(
|
|
|
|
const float *ramp, float f, bool interpolate, bool extrapolate, int table_size)
|
|
|
|
{
|
2016-10-24 12:26:12 +02:00
|
|
|
if ((f < 0.0f || f > 1.0f) && extrapolate) {
|
2016-07-30 23:30:36 +02:00
|
|
|
float t0, dy;
|
2016-10-24 12:26:12 +02:00
|
|
|
if (f < 0.0f) {
|
2016-07-30 23:30:36 +02:00
|
|
|
t0 = ramp[0];
|
|
|
|
dy = t0 - ramp[1], f = -f;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
t0 = ramp[table_size - 1];
|
|
|
|
dy = t0 - ramp[table_size - 2];
|
|
|
|
f = f - 1.0f;
|
|
|
|
}
|
|
|
|
return t0 + dy * f * (table_size - 1);
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-07-30 23:30:36 +02:00
|
|
|
f = clamp(f, 0.0f, 1.0f) * (table_size - 1);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-07-30 23:30:36 +02:00
|
|
|
/* clamp int as well in case of NaN */
|
|
|
|
int i = clamp(float_to_int(f), 0, table_size - 1);
|
|
|
|
float t = f - (float)i;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-07-30 23:30:36 +02:00
|
|
|
float result = ramp[i];
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-10-24 12:26:12 +02:00
|
|
|
if (interpolate && t > 0.0f) {
|
2016-07-30 23:30:36 +02:00
|
|
|
result = (1.0f - t) * result + t * ramp[i + 1];
|
2016-10-24 12:26:12 +02:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-07-30 23:30:36 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
CCL_NAMESPACE_END
|