2023-06-14 16:52:36 +10:00
|
|
|
/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
|
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: Apache-2.0 */
|
2017-04-14 14:05:23 +02:00
|
|
|
|
2024-12-26 17:53:56 +01:00
|
|
|
#pragma once
|
2017-04-14 14:05:23 +02:00
|
|
|
|
2024-12-26 17:53:56 +01:00
|
|
|
#include "util/math_base.h"
|
|
|
|
|
#include "util/types_float2.h"
|
|
|
|
|
#include "util/types_float4.h"
|
2017-04-14 14:05:23 +02:00
|
|
|
|
|
|
|
|
CCL_NAMESPACE_BEGIN
|
|
|
|
|
|
2021-02-17 01:47:18 +01:00
|
|
|
ccl_device_inline float2 zero_float2()
|
|
|
|
|
{
|
|
|
|
|
return make_float2(0.0f, 0.0f);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ccl_device_inline float2 one_float2()
|
|
|
|
|
{
|
|
|
|
|
return make_float2(1.0f, 1.0f);
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-20 19:28:45 +01:00
|
|
|
ccl_device_template_spec float2 make_zero()
|
|
|
|
|
{
|
|
|
|
|
return zero_float2();
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-18 14:25:05 +01:00
|
|
|
#if !defined(__KERNEL_METAL__)
|
2017-04-14 14:05:23 +02:00
|
|
|
ccl_device_inline float2 operator-(const float2 &a)
|
|
|
|
|
{
|
|
|
|
|
return make_float2(-a.x, -a.y);
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-01 15:16:55 +01:00
|
|
|
ccl_device_inline float2 operator*(const float2 a, const float2 b)
|
2017-04-14 14:05:23 +02:00
|
|
|
{
|
|
|
|
|
return make_float2(a.x * b.x, a.y * b.y);
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-01 18:15:54 +01:00
|
|
|
ccl_device_inline float2 operator*(const float2 a, const float f)
|
2017-04-14 14:05:23 +02:00
|
|
|
{
|
|
|
|
|
return make_float2(a.x * f, a.y * f);
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-01 15:16:55 +01:00
|
|
|
ccl_device_inline float2 operator*(float f, const float2 a)
|
2017-04-14 14:05:23 +02:00
|
|
|
{
|
|
|
|
|
return make_float2(a.x * f, a.y * f);
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-01 15:16:55 +01:00
|
|
|
ccl_device_inline float2 operator/(float f, const float2 a)
|
2017-04-14 14:05:23 +02:00
|
|
|
{
|
|
|
|
|
return make_float2(f / a.x, f / a.y);
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-01 18:15:54 +01:00
|
|
|
ccl_device_inline float2 operator/(const float2 a, const float f)
|
2017-04-14 14:05:23 +02:00
|
|
|
{
|
2024-12-29 17:32:00 +01:00
|
|
|
const float invf = 1.0f / f;
|
2017-04-14 14:05:23 +02:00
|
|
|
return make_float2(a.x * invf, a.y * invf);
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-01 15:16:55 +01:00
|
|
|
ccl_device_inline float2 operator/(const float2 a, const float2 b)
|
2017-04-14 14:05:23 +02:00
|
|
|
{
|
|
|
|
|
return make_float2(a.x / b.x, a.y / b.y);
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-01 15:16:55 +01:00
|
|
|
ccl_device_inline float2 operator+(const float2 a, const float2 b)
|
2019-09-12 13:09:31 +02:00
|
|
|
{
|
2022-11-01 15:16:55 +01:00
|
|
|
return make_float2(a.x + b.x, a.y + b.y);
|
2019-09-12 13:09:31 +02:00
|
|
|
}
|
|
|
|
|
|
2022-11-01 15:16:55 +01:00
|
|
|
ccl_device_inline float2 operator+(const float2 a, const float f)
|
2017-04-14 14:05:23 +02:00
|
|
|
{
|
2022-11-01 15:16:55 +01:00
|
|
|
return a + make_float2(f, f);
|
2017-04-14 14:05:23 +02:00
|
|
|
}
|
|
|
|
|
|
2022-11-01 15:16:55 +01:00
|
|
|
ccl_device_inline float2 operator-(const float2 a, const float2 b)
|
2019-09-12 13:09:31 +02:00
|
|
|
{
|
2022-11-01 15:16:55 +01:00
|
|
|
return make_float2(a.x - b.x, a.y - b.y);
|
2019-09-12 13:09:31 +02:00
|
|
|
}
|
|
|
|
|
|
2022-11-01 15:16:55 +01:00
|
|
|
ccl_device_inline float2 operator-(const float2 a, const float f)
|
2017-04-14 14:05:23 +02:00
|
|
|
{
|
2022-11-01 15:16:55 +01:00
|
|
|
return a - make_float2(f, f);
|
2017-04-14 14:05:23 +02:00
|
|
|
}
|
|
|
|
|
|
2022-11-01 15:16:55 +01:00
|
|
|
ccl_device_inline float2 operator+=(float2 &a, const float2 b)
|
2017-04-14 14:05:23 +02:00
|
|
|
{
|
|
|
|
|
return a = a + b;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-01 15:16:55 +01:00
|
|
|
ccl_device_inline float2 operator*=(float2 &a, const float2 b)
|
2017-04-14 14:05:23 +02:00
|
|
|
{
|
|
|
|
|
return a = a * b;
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-01 18:15:54 +01:00
|
|
|
ccl_device_inline float2 operator*=(float2 &a, const float f)
|
2017-04-14 14:05:23 +02:00
|
|
|
{
|
|
|
|
|
return a = a * f;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-01 15:16:55 +01:00
|
|
|
ccl_device_inline float2 operator/=(float2 &a, const float2 b)
|
2017-04-14 14:05:23 +02:00
|
|
|
{
|
|
|
|
|
return a = a / b;
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-01 18:15:54 +01:00
|
|
|
ccl_device_inline float2 operator/=(float2 &a, const float f)
|
2017-04-14 14:05:23 +02:00
|
|
|
{
|
2024-12-29 17:32:00 +01:00
|
|
|
const float invf = 1.0f / f;
|
2017-04-14 14:05:23 +02:00
|
|
|
return a = a * invf;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-01 15:16:55 +01:00
|
|
|
ccl_device_inline bool operator==(const float2 a, const float2 b)
|
2017-04-14 14:05:23 +02:00
|
|
|
{
|
|
|
|
|
return (a.x == b.x && a.y == b.y);
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-01 15:16:55 +01:00
|
|
|
ccl_device_inline bool operator!=(const float2 a, const float2 b)
|
2017-04-14 14:05:23 +02:00
|
|
|
{
|
|
|
|
|
return !(a == b);
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-09 13:40:10 +02:00
|
|
|
ccl_device_inline int2 operator>=(const float2 a, const float2 b)
|
|
|
|
|
{
|
|
|
|
|
return make_int2(a.x >= b.x, a.y >= b.y);
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-01 15:16:55 +01:00
|
|
|
ccl_device_inline bool is_zero(const float2 a)
|
2017-04-14 14:05:23 +02:00
|
|
|
{
|
|
|
|
|
return (a.x == 0.0f && a.y == 0.0f);
|
|
|
|
|
}
|
|
|
|
|
|
Cycles: Add thin film iridescence to Principled BSDF
This is an implementation of thin film iridescence in the Principled BSDF based on "A Practical Extension to Microfacet Theory for the Modeling of Varying Iridescence".
There are still several open topics that are left for future work:
- Currently, the thin film only affects dielectric Fresnel, not metallic. Properly specifying thin films on metals requires a proper conductive Fresnel term with complex IOR inputs, any attempt of trying to hack it into the F82 model we currently use for the Principled BSDF is fundamentally flawed. In the future, we'll add a node for proper conductive Fresnel, including thin films.
- The F0/F90 control is not very elegantly implemented right now. It fundamentally works, but enabling thin film while using a Specular Tint causes a jump in appearance since the models integrate it differently. Then again, thin film interference is a physical effect, so of course a non-physical tweak doesn't play nicely with it.
- The white point handling is currently quite crude. In short: The code computes XYZ values of the reflectance spectrum, but we'd need the XYZ values of the product of the reflectance spectrum and the neutral illuminant of the working color space. Currently, this is addressed by just dividing by the XYZ values of the illuminant, but it would be better to do a proper chromatic adaptation transform or to use the proper reference curves for the working space instead of the XYZ curves from the paper.
Pull Request: https://projects.blender.org/blender/blender/pulls/118477
2024-05-02 14:28:44 +02:00
|
|
|
ccl_device_inline float dot(const float2 a, const float2 b)
|
|
|
|
|
{
|
|
|
|
|
return a.x * b.x + a.y * b.y;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2022-11-01 15:16:55 +01:00
|
|
|
ccl_device_inline float average(const float2 a)
|
2017-04-14 14:05:23 +02:00
|
|
|
{
|
|
|
|
|
return (a.x + a.y) * (1.0f / 2.0f);
|
|
|
|
|
}
|
|
|
|
|
|
Cycles: Add thin film iridescence to Principled BSDF
This is an implementation of thin film iridescence in the Principled BSDF based on "A Practical Extension to Microfacet Theory for the Modeling of Varying Iridescence".
There are still several open topics that are left for future work:
- Currently, the thin film only affects dielectric Fresnel, not metallic. Properly specifying thin films on metals requires a proper conductive Fresnel term with complex IOR inputs, any attempt of trying to hack it into the F82 model we currently use for the Principled BSDF is fundamentally flawed. In the future, we'll add a node for proper conductive Fresnel, including thin films.
- The F0/F90 control is not very elegantly implemented right now. It fundamentally works, but enabling thin film while using a Specular Tint causes a jump in appearance since the models integrate it differently. Then again, thin film interference is a physical effect, so of course a non-physical tweak doesn't play nicely with it.
- The white point handling is currently quite crude. In short: The code computes XYZ values of the reflectance spectrum, but we'd need the XYZ values of the product of the reflectance spectrum and the neutral illuminant of the working color space. Currently, this is addressed by just dividing by the XYZ values of the illuminant, but it would be better to do a proper chromatic adaptation transform or to use the proper reference curves for the working space instead of the XYZ curves from the paper.
Pull Request: https://projects.blender.org/blender/blender/pulls/118477
2024-05-02 14:28:44 +02:00
|
|
|
ccl_device_inline bool isequal(const float2 a, const float2 b)
|
2019-09-12 13:09:31 +02:00
|
|
|
{
|
Cycles: Add thin film iridescence to Principled BSDF
This is an implementation of thin film iridescence in the Principled BSDF based on "A Practical Extension to Microfacet Theory for the Modeling of Varying Iridescence".
There are still several open topics that are left for future work:
- Currently, the thin film only affects dielectric Fresnel, not metallic. Properly specifying thin films on metals requires a proper conductive Fresnel term with complex IOR inputs, any attempt of trying to hack it into the F82 model we currently use for the Principled BSDF is fundamentally flawed. In the future, we'll add a node for proper conductive Fresnel, including thin films.
- The F0/F90 control is not very elegantly implemented right now. It fundamentally works, but enabling thin film while using a Specular Tint causes a jump in appearance since the models integrate it differently. Then again, thin film interference is a physical effect, so of course a non-physical tweak doesn't play nicely with it.
- The white point handling is currently quite crude. In short: The code computes XYZ values of the reflectance spectrum, but we'd need the XYZ values of the product of the reflectance spectrum and the neutral illuminant of the working color space. Currently, this is addressed by just dividing by the XYZ values of the illuminant, but it would be better to do a proper chromatic adaptation transform or to use the proper reference curves for the working space instead of the XYZ curves from the paper.
Pull Request: https://projects.blender.org/blender/blender/pulls/118477
2024-05-02 14:28:44 +02:00
|
|
|
#if defined(__KERNEL_METAL__)
|
|
|
|
|
return all(a == b);
|
|
|
|
|
#else
|
|
|
|
|
return a == b;
|
2022-11-01 15:16:55 +01:00
|
|
|
#endif
|
Cycles: Add thin film iridescence to Principled BSDF
This is an implementation of thin film iridescence in the Principled BSDF based on "A Practical Extension to Microfacet Theory for the Modeling of Varying Iridescence".
There are still several open topics that are left for future work:
- Currently, the thin film only affects dielectric Fresnel, not metallic. Properly specifying thin films on metals requires a proper conductive Fresnel term with complex IOR inputs, any attempt of trying to hack it into the F82 model we currently use for the Principled BSDF is fundamentally flawed. In the future, we'll add a node for proper conductive Fresnel, including thin films.
- The F0/F90 control is not very elegantly implemented right now. It fundamentally works, but enabling thin film while using a Specular Tint causes a jump in appearance since the models integrate it differently. Then again, thin film interference is a physical effect, so of course a non-physical tweak doesn't play nicely with it.
- The white point handling is currently quite crude. In short: The code computes XYZ values of the reflectance spectrum, but we'd need the XYZ values of the product of the reflectance spectrum and the neutral illuminant of the working color space. Currently, this is addressed by just dividing by the XYZ values of the illuminant, but it would be better to do a proper chromatic adaptation transform or to use the proper reference curves for the working space instead of the XYZ curves from the paper.
Pull Request: https://projects.blender.org/blender/blender/pulls/118477
2024-05-02 14:28:44 +02:00
|
|
|
}
|
2019-09-12 13:09:31 +02:00
|
|
|
|
2025-04-08 17:04:18 +02:00
|
|
|
template<class MaskType>
|
|
|
|
|
ccl_device_inline float2 select(const MaskType mask, const float2 a, const float2 b)
|
|
|
|
|
{
|
|
|
|
|
return make_float2((mask.x) ? a.x : b.x, (mask.y) ? a.y : b.y);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<class MaskType> ccl_device_inline float2 mask(const MaskType mask, const float2 a)
|
|
|
|
|
{
|
|
|
|
|
/* Replace elements of x with zero where mask isn't set. */
|
|
|
|
|
return select(mask, a, zero_float2());
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-01 15:16:55 +01:00
|
|
|
ccl_device_inline float len(const float2 a)
|
2017-04-14 14:05:23 +02:00
|
|
|
{
|
2022-11-01 15:16:55 +01:00
|
|
|
return sqrtf(dot(a, a));
|
2017-04-14 14:05:23 +02:00
|
|
|
}
|
|
|
|
|
|
2023-05-16 13:56:08 +02:00
|
|
|
ccl_device_inline float reduce_min(const float2 a)
|
|
|
|
|
{
|
|
|
|
|
return min(a.x, a.y);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ccl_device_inline float reduce_max(const float2 a)
|
|
|
|
|
{
|
|
|
|
|
return max(a.x, a.y);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ccl_device_inline float reduce_add(const float2 a)
|
|
|
|
|
{
|
|
|
|
|
return a.x + a.y;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-24 17:34:24 +01:00
|
|
|
ccl_device_inline float len_squared(const float2 a)
|
|
|
|
|
{
|
|
|
|
|
return dot(a, a);
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-29 07:56:46 +02:00
|
|
|
ccl_device_inline float2 safe_normalize(const float2 a)
|
|
|
|
|
{
|
2024-12-29 17:32:00 +01:00
|
|
|
const float t = len(a);
|
2024-06-29 07:56:46 +02:00
|
|
|
return (t != 0.0f) ? a / t : a;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-01 15:16:55 +01:00
|
|
|
#if !defined(__KERNEL_METAL__)
|
|
|
|
|
ccl_device_inline float distance(const float2 a, const float2 b)
|
|
|
|
|
{
|
|
|
|
|
return len(a - b);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ccl_device_inline float cross(const float2 a, const float2 b)
|
2017-04-14 14:05:23 +02:00
|
|
|
{
|
|
|
|
|
return (a.x * b.y - a.y * b.x);
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-01 15:16:55 +01:00
|
|
|
ccl_device_inline float2 normalize(const float2 a)
|
2017-04-14 14:05:23 +02:00
|
|
|
{
|
|
|
|
|
return a / len(a);
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-01 15:16:55 +01:00
|
|
|
ccl_device_inline float2 normalize_len(const float2 a, ccl_private float *t)
|
2017-04-14 14:05:23 +02:00
|
|
|
{
|
|
|
|
|
*t = len(a);
|
|
|
|
|
return a / (*t);
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-01 15:16:55 +01:00
|
|
|
ccl_device_inline float2 min(const float2 a, const float2 b)
|
2017-04-14 14:05:23 +02:00
|
|
|
{
|
|
|
|
|
return make_float2(min(a.x, b.x), min(a.y, b.y));
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-01 15:16:55 +01:00
|
|
|
ccl_device_inline float2 max(const float2 a, const float2 b)
|
2017-04-14 14:05:23 +02:00
|
|
|
{
|
|
|
|
|
return make_float2(max(a.x, b.x), max(a.y, b.y));
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-01 15:16:55 +01:00
|
|
|
ccl_device_inline float2 clamp(const float2 a, const float2 mn, const float2 mx)
|
2017-04-14 14:05:23 +02:00
|
|
|
{
|
|
|
|
|
return min(max(a, mn), mx);
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-29 16:12:23 +01:00
|
|
|
ccl_device_inline float2 fmod(const float2 a, const float b)
|
|
|
|
|
{
|
|
|
|
|
return make_float2(fmodf(a.x, b), fmodf(a.y, b));
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-01 15:16:55 +01:00
|
|
|
ccl_device_inline float2 fabs(const float2 a)
|
2017-04-14 14:05:23 +02:00
|
|
|
{
|
|
|
|
|
return make_float2(fabsf(a.x), fabsf(a.y));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ccl_device_inline float2 as_float2(const float4 &a)
|
|
|
|
|
{
|
|
|
|
|
return make_float2(a.x, a.y);
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-01 18:15:54 +01:00
|
|
|
ccl_device_inline float2 interp(const float2 a, const float2 b, const float t)
|
2017-04-14 14:05:23 +02:00
|
|
|
{
|
|
|
|
|
return a + t * (b - a);
|
|
|
|
|
}
|
2019-03-05 14:54:54 +01:00
|
|
|
|
2025-01-01 18:15:54 +01:00
|
|
|
ccl_device_inline float2 mix(const float2 a, const float2 b, const float t)
|
2019-03-05 14:54:54 +01:00
|
|
|
{
|
|
|
|
|
return a + t * (b - a);
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-01 15:16:55 +01:00
|
|
|
ccl_device_inline float2 floor(const float2 a)
|
2019-09-12 13:09:31 +02:00
|
|
|
{
|
|
|
|
|
return make_float2(floorf(a.x), floorf(a.y));
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-18 14:25:05 +01:00
|
|
|
#endif /* !__KERNEL_METAL__ */
|
|
|
|
|
|
2023-06-13 09:18:12 +02:00
|
|
|
/* Consistent name for this would be pow, but HIP compiler crashes in name mangling. */
|
2025-01-01 18:15:54 +01:00
|
|
|
ccl_device_inline float2 power(const float2 v, const float e)
|
2023-06-13 09:18:12 +02:00
|
|
|
{
|
|
|
|
|
return make_float2(powf(v.x, e), powf(v.y, e));
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-12 13:09:31 +02:00
|
|
|
ccl_device_inline float2 safe_divide_float2_float(const float2 a, const float b)
|
|
|
|
|
{
|
2021-02-17 01:47:18 +01:00
|
|
|
return (b != 0.0f) ? a / b : zero_float2();
|
2019-09-12 13:09:31 +02:00
|
|
|
}
|
|
|
|
|
|
2017-04-14 14:05:23 +02:00
|
|
|
CCL_NAMESPACE_END
|