2023-06-14 16:52:36 +10:00
|
|
|
/* SPDX-FileCopyrightText: 2011-2013 Intel Corporation
|
|
|
|
|
* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
|
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: Apache-2.0 */
|
2017-04-14 14:05:23 +02:00
|
|
|
|
|
|
|
|
#ifndef __UTIL_MATH_FLOAT3_H__
|
|
|
|
|
#define __UTIL_MATH_FLOAT3_H__
|
|
|
|
|
|
|
|
|
|
#ifndef __UTIL_MATH_H__
|
2021-10-24 14:19:19 +02:00
|
|
|
# error "Do not include this file directly, include util/types.h instead."
|
2017-04-14 14:05:23 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
CCL_NAMESPACE_BEGIN
|
|
|
|
|
|
2021-02-17 01:47:18 +01:00
|
|
|
ccl_device_inline float3 zero_float3()
|
|
|
|
|
{
|
|
|
|
|
#ifdef __KERNEL_SSE__
|
|
|
|
|
return float3(_mm_setzero_ps());
|
|
|
|
|
#else
|
|
|
|
|
return make_float3(0.0f, 0.0f, 0.0f);
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ccl_device_inline float3 one_float3()
|
|
|
|
|
{
|
|
|
|
|
return make_float3(1.0f, 1.0f, 1.0f);
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-18 14:25:05 +01:00
|
|
|
#if defined(__KERNEL_METAL__)
|
|
|
|
|
|
|
|
|
|
ccl_device_inline float3 rcp(float3 a)
|
|
|
|
|
{
|
|
|
|
|
return make_float3(1.0f / a.x, 1.0f / a.y, 1.0f / a.z);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
2017-04-14 14:05:23 +02:00
|
|
|
ccl_device_inline float3 operator-(const float3 &a)
|
|
|
|
|
{
|
2021-11-18 14:25:05 +01:00
|
|
|
# ifdef __KERNEL_SSE__
|
2017-04-14 14:05:23 +02:00
|
|
|
return float3(_mm_xor_ps(a.m128, _mm_castsi128_ps(_mm_set1_epi32(0x80000000))));
|
2021-11-18 14:25:05 +01:00
|
|
|
# else
|
2017-04-14 14:05:23 +02:00
|
|
|
return make_float3(-a.x, -a.y, -a.z);
|
2021-11-18 14:25:05 +01:00
|
|
|
# endif
|
2017-04-14 14:05:23 +02:00
|
|
|
}
|
|
|
|
|
|
2022-11-01 15:16:55 +01:00
|
|
|
ccl_device_inline float3 operator*(const float3 a, const float3 b)
|
2017-04-14 14:05:23 +02:00
|
|
|
{
|
2021-11-18 14:25:05 +01:00
|
|
|
# ifdef __KERNEL_SSE__
|
2017-04-14 14:05:23 +02:00
|
|
|
return float3(_mm_mul_ps(a.m128, b.m128));
|
2021-11-18 14:25:05 +01:00
|
|
|
# else
|
2017-04-14 14:05:23 +02:00
|
|
|
return make_float3(a.x * b.x, a.y * b.y, a.z * b.z);
|
2021-11-18 14:25:05 +01:00
|
|
|
# endif
|
2017-04-14 14:05:23 +02:00
|
|
|
}
|
|
|
|
|
|
2022-11-01 15:16:55 +01:00
|
|
|
ccl_device_inline float3 operator*(const float3 a, const float f)
|
2017-04-14 14:05:23 +02:00
|
|
|
{
|
2021-11-18 14:25:05 +01:00
|
|
|
# ifdef __KERNEL_SSE__
|
2017-04-14 14:05:23 +02:00
|
|
|
return float3(_mm_mul_ps(a.m128, _mm_set1_ps(f)));
|
2021-11-18 14:25:05 +01:00
|
|
|
# else
|
2017-04-14 14:05:23 +02:00
|
|
|
return make_float3(a.x * f, a.y * f, a.z * f);
|
2021-11-18 14:25:05 +01:00
|
|
|
# endif
|
2017-04-14 14:05:23 +02:00
|
|
|
}
|
|
|
|
|
|
2022-11-01 15:16:55 +01:00
|
|
|
ccl_device_inline float3 operator*(const float f, const float3 a)
|
2017-04-14 14:05:23 +02:00
|
|
|
{
|
2021-11-18 14:25:05 +01:00
|
|
|
# if defined(__KERNEL_SSE__)
|
2017-04-14 14:05:23 +02:00
|
|
|
return float3(_mm_mul_ps(_mm_set1_ps(f), a.m128));
|
2021-11-18 14:25:05 +01:00
|
|
|
# else
|
2017-04-14 14:05:23 +02:00
|
|
|
return make_float3(a.x * f, a.y * f, a.z * f);
|
2021-11-18 14:25:05 +01:00
|
|
|
# endif
|
2017-04-14 14:05:23 +02:00
|
|
|
}
|
|
|
|
|
|
2022-11-01 15:16:55 +01:00
|
|
|
ccl_device_inline float3 operator/(const float f, const float3 a)
|
2017-04-14 14:05:23 +02:00
|
|
|
{
|
2021-11-18 14:25:05 +01:00
|
|
|
# if defined(__KERNEL_SSE__)
|
2017-08-02 02:23:03 +02:00
|
|
|
return float3(_mm_div_ps(_mm_set1_ps(f), a.m128));
|
2021-11-18 14:25:05 +01:00
|
|
|
# else
|
2017-04-14 14:05:23 +02:00
|
|
|
return make_float3(f / a.x, f / a.y, f / a.z);
|
2021-11-18 14:25:05 +01:00
|
|
|
# endif
|
2017-04-14 14:05:23 +02:00
|
|
|
}
|
|
|
|
|
|
2022-11-01 15:16:55 +01:00
|
|
|
ccl_device_inline float3 operator/(const float3 a, const float f)
|
2017-04-14 14:05:23 +02:00
|
|
|
{
|
2022-07-21 15:49:00 +02:00
|
|
|
# if defined(__KERNEL_SSE__)
|
|
|
|
|
return float3(_mm_div_ps(a.m128, _mm_set1_ps(f)));
|
|
|
|
|
# else
|
2023-02-27 10:39:19 +01:00
|
|
|
float invf = 1.0f / f;
|
|
|
|
|
return make_float3(a.x * invf, a.y * invf, a.z * invf);
|
2022-07-21 15:49:00 +02:00
|
|
|
# endif
|
2017-04-14 14:05:23 +02:00
|
|
|
}
|
|
|
|
|
|
2022-11-01 15:16:55 +01:00
|
|
|
ccl_device_inline float3 operator/(const float3 a, const float3 b)
|
2017-04-14 14:05:23 +02:00
|
|
|
{
|
2021-11-18 14:25:05 +01:00
|
|
|
# if defined(__KERNEL_SSE__)
|
2017-08-02 02:23:03 +02:00
|
|
|
return float3(_mm_div_ps(a.m128, b.m128));
|
2021-11-18 14:25:05 +01:00
|
|
|
# else
|
2017-04-14 14:05:23 +02:00
|
|
|
return make_float3(a.x / b.x, a.y / b.y, a.z / b.z);
|
2021-11-18 14:25:05 +01:00
|
|
|
# endif
|
2017-04-14 14:05:23 +02:00
|
|
|
}
|
|
|
|
|
|
2022-11-01 15:16:55 +01:00
|
|
|
ccl_device_inline float3 operator+(const float3 a, const float3 b)
|
2017-04-14 14:05:23 +02:00
|
|
|
{
|
2021-11-18 14:25:05 +01:00
|
|
|
# ifdef __KERNEL_SSE__
|
2017-04-14 14:05:23 +02:00
|
|
|
return float3(_mm_add_ps(a.m128, b.m128));
|
2021-11-18 14:25:05 +01:00
|
|
|
# else
|
2017-04-14 14:05:23 +02:00
|
|
|
return make_float3(a.x + b.x, a.y + b.y, a.z + b.z);
|
2021-11-18 14:25:05 +01:00
|
|
|
# endif
|
2017-04-14 14:05:23 +02:00
|
|
|
}
|
|
|
|
|
|
2022-11-01 15:16:55 +01:00
|
|
|
ccl_device_inline float3 operator+(const float3 a, const float f)
|
2019-09-12 13:09:31 +02:00
|
|
|
{
|
2022-11-01 15:16:55 +01:00
|
|
|
return a + make_float3(f, f, f);
|
2019-09-12 13:09:31 +02:00
|
|
|
}
|
|
|
|
|
|
2022-11-01 15:16:55 +01:00
|
|
|
ccl_device_inline float3 operator-(const float3 a, const float3 b)
|
2017-04-14 14:05:23 +02:00
|
|
|
{
|
2021-11-18 14:25:05 +01:00
|
|
|
# ifdef __KERNEL_SSE__
|
2017-04-14 14:05:23 +02:00
|
|
|
return float3(_mm_sub_ps(a.m128, b.m128));
|
2021-11-18 14:25:05 +01:00
|
|
|
# else
|
2017-04-14 14:05:23 +02:00
|
|
|
return make_float3(a.x - b.x, a.y - b.y, a.z - b.z);
|
2021-11-18 14:25:05 +01:00
|
|
|
# endif
|
2017-04-14 14:05:23 +02:00
|
|
|
}
|
|
|
|
|
|
2022-11-01 15:16:55 +01:00
|
|
|
ccl_device_inline float3 operator-(const float3 a, const float f)
|
|
|
|
|
{
|
|
|
|
|
return a - make_float3(f, f, f);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ccl_device_inline float3 operator+=(float3 &a, const float3 b)
|
2017-04-14 14:05:23 +02:00
|
|
|
{
|
|
|
|
|
return a = a + b;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-01 15:16:55 +01:00
|
|
|
ccl_device_inline float3 operator-=(float3 &a, const float3 b)
|
2017-05-07 14:40:58 +02:00
|
|
|
{
|
|
|
|
|
return a = a - b;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-01 15:16:55 +01:00
|
|
|
ccl_device_inline float3 operator*=(float3 &a, const float3 b)
|
2017-04-14 14:05:23 +02:00
|
|
|
{
|
|
|
|
|
return a = a * b;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ccl_device_inline float3 operator*=(float3 &a, float f)
|
|
|
|
|
{
|
|
|
|
|
return a = a * f;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-01 15:16:55 +01:00
|
|
|
ccl_device_inline float3 operator/=(float3 &a, const float3 b)
|
2017-04-14 14:05:23 +02:00
|
|
|
{
|
|
|
|
|
return a = a / b;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ccl_device_inline float3 operator/=(float3 &a, float f)
|
|
|
|
|
{
|
|
|
|
|
float invf = 1.0f / f;
|
|
|
|
|
return a = a * invf;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-15 15:40:29 +02:00
|
|
|
# if !(defined(__KERNEL_METAL__) || defined(__KERNEL_CUDA__) || defined(__KERNEL_HIP__))
|
2022-11-01 15:16:55 +01:00
|
|
|
ccl_device_inline packed_float3 operator*=(packed_float3 &a, const float3 b)
|
2021-11-16 14:03:59 +01:00
|
|
|
{
|
|
|
|
|
a = float3(a) * b;
|
|
|
|
|
return a;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ccl_device_inline packed_float3 operator*=(packed_float3 &a, float f)
|
|
|
|
|
{
|
|
|
|
|
a = float3(a) * f;
|
|
|
|
|
return a;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-01 15:16:55 +01:00
|
|
|
ccl_device_inline packed_float3 operator/=(packed_float3 &a, const float3 b)
|
2021-11-16 14:03:59 +01:00
|
|
|
{
|
|
|
|
|
a = float3(a) / b;
|
|
|
|
|
return a;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ccl_device_inline packed_float3 operator/=(packed_float3 &a, float f)
|
|
|
|
|
{
|
|
|
|
|
a = float3(a) / f;
|
|
|
|
|
return a;
|
|
|
|
|
}
|
2021-11-29 15:06:22 +00:00
|
|
|
# endif
|
2021-11-16 14:03:59 +01:00
|
|
|
|
2022-11-01 15:16:55 +01:00
|
|
|
ccl_device_inline bool operator==(const float3 a, const float3 b)
|
2017-04-14 14:05:23 +02:00
|
|
|
{
|
2021-11-18 14:25:05 +01:00
|
|
|
# ifdef __KERNEL_SSE__
|
2017-04-14 14:05:23 +02:00
|
|
|
return (_mm_movemask_ps(_mm_cmpeq_ps(a.m128, b.m128)) & 7) == 7;
|
2021-11-18 14:25:05 +01:00
|
|
|
# else
|
2017-04-14 14:05:23 +02:00
|
|
|
return (a.x == b.x && a.y == b.y && a.z == b.z);
|
2021-11-18 14:25:05 +01:00
|
|
|
# endif
|
2017-04-14 14:05:23 +02:00
|
|
|
}
|
|
|
|
|
|
2022-11-01 15:16:55 +01:00
|
|
|
ccl_device_inline bool operator!=(const float3 a, const float3 b)
|
2017-04-14 14:05:23 +02:00
|
|
|
{
|
|
|
|
|
return !(a == b);
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-01 15:16:55 +01:00
|
|
|
ccl_device_inline float dot(const float3 a, const float3 b)
|
2017-04-14 14:05:23 +02:00
|
|
|
{
|
2021-11-18 14:25:05 +01:00
|
|
|
# if defined(__KERNEL_SSE41__) && defined(__KERNEL_SSE__)
|
2017-04-14 14:05:23 +02:00
|
|
|
return _mm_cvtss_f32(_mm_dp_ps(a, b, 0x7F));
|
2021-11-18 14:25:05 +01:00
|
|
|
# else
|
2017-04-14 14:05:23 +02:00
|
|
|
return a.x * b.x + a.y * b.y + a.z * b.z;
|
2021-11-18 14:25:05 +01:00
|
|
|
# endif
|
2017-04-14 14:05:23 +02:00
|
|
|
}
|
|
|
|
|
|
2022-11-01 15:16:55 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
ccl_device_inline float dot_xy(const float3 a, const float3 b)
|
2017-04-14 14:05:23 +02:00
|
|
|
{
|
2022-11-01 15:16:55 +01:00
|
|
|
#if defined(__KERNEL_SSE41__) && defined(__KERNEL_SSE__)
|
2017-04-14 14:05:23 +02:00
|
|
|
return _mm_cvtss_f32(_mm_hadd_ps(_mm_mul_ps(a, b), b));
|
2022-11-01 15:16:55 +01:00
|
|
|
#else
|
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
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ccl_device_inline float len(const float3 a)
|
|
|
|
|
{
|
|
|
|
|
#if defined(__KERNEL_SSE41__) && defined(__KERNEL_SSE__)
|
|
|
|
|
return _mm_cvtss_f32(_mm_sqrt_ss(_mm_dp_ps(a.m128, a.m128, 0x7F)));
|
|
|
|
|
#else
|
|
|
|
|
return sqrtf(dot(a, a));
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ccl_device_inline float reduce_min(float3 a)
|
|
|
|
|
{
|
|
|
|
|
return min(min(a.x, a.y), a.z);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ccl_device_inline float reduce_max(float3 a)
|
|
|
|
|
{
|
|
|
|
|
return max(max(a.x, a.y), a.z);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ccl_device_inline float len_squared(const float3 a)
|
|
|
|
|
{
|
|
|
|
|
return dot(a, a);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#ifndef __KERNEL_METAL__
|
|
|
|
|
|
|
|
|
|
ccl_device_inline float distance(const float3 a, const float3 b)
|
|
|
|
|
{
|
|
|
|
|
return len(a - b);
|
2017-04-14 14:05:23 +02:00
|
|
|
}
|
|
|
|
|
|
2022-11-01 15:16:55 +01:00
|
|
|
ccl_device_inline float3 cross(const float3 a, const float3 b)
|
2017-04-14 14:05:23 +02:00
|
|
|
{
|
2022-07-21 15:49:00 +02:00
|
|
|
# ifdef __KERNEL_SSE__
|
2022-11-01 15:16:55 +01:00
|
|
|
const float4 x = float4(a.m128);
|
|
|
|
|
const float4 y = shuffle<1, 2, 0, 3>(float4(b.m128));
|
|
|
|
|
const float4 z = float4(_mm_mul_ps(shuffle<1, 2, 0, 3>(float4(a.m128)), float4(b.m128)));
|
|
|
|
|
|
|
|
|
|
return float3(shuffle<1, 2, 0, 3>(msub(x, y, z)).m128);
|
2022-07-21 15:49:00 +02:00
|
|
|
# else
|
|
|
|
|
return make_float3(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x);
|
|
|
|
|
# endif
|
2017-04-14 14:05:23 +02:00
|
|
|
}
|
|
|
|
|
|
2022-11-01 15:16:55 +01:00
|
|
|
ccl_device_inline float3 normalize(const float3 a)
|
2017-04-14 14:05:23 +02:00
|
|
|
{
|
2021-11-18 14:25:05 +01:00
|
|
|
# if defined(__KERNEL_SSE41__) && defined(__KERNEL_SSE__)
|
2017-04-14 14:05:23 +02:00
|
|
|
__m128 norm = _mm_sqrt_ps(_mm_dp_ps(a.m128, a.m128, 0x7F));
|
|
|
|
|
return float3(_mm_div_ps(a.m128, norm));
|
2021-11-18 14:25:05 +01:00
|
|
|
# else
|
2017-04-14 14:05:23 +02:00
|
|
|
return a / len(a);
|
2021-11-18 14:25:05 +01:00
|
|
|
# endif
|
2017-04-14 14:05:23 +02:00
|
|
|
}
|
|
|
|
|
|
2022-11-01 15:16:55 +01:00
|
|
|
ccl_device_inline float3 min(const float3 a, const float3 b)
|
2017-04-14 14:05:23 +02:00
|
|
|
{
|
2021-11-18 14:25:05 +01:00
|
|
|
# ifdef __KERNEL_SSE__
|
2017-04-14 14:05:23 +02:00
|
|
|
return float3(_mm_min_ps(a.m128, b.m128));
|
2021-11-18 14:25:05 +01:00
|
|
|
# else
|
2017-04-14 14:05:23 +02:00
|
|
|
return make_float3(min(a.x, b.x), min(a.y, b.y), min(a.z, b.z));
|
2021-11-18 14:25:05 +01:00
|
|
|
# endif
|
2017-04-14 14:05:23 +02:00
|
|
|
}
|
|
|
|
|
|
2022-11-01 15:16:55 +01:00
|
|
|
ccl_device_inline float3 max(const float3 a, const float3 b)
|
2017-04-14 14:05:23 +02:00
|
|
|
{
|
2021-11-18 14:25:05 +01:00
|
|
|
# ifdef __KERNEL_SSE__
|
2017-04-14 14:05:23 +02:00
|
|
|
return float3(_mm_max_ps(a.m128, b.m128));
|
2021-11-18 14:25:05 +01:00
|
|
|
# else
|
2017-04-14 14:05:23 +02:00
|
|
|
return make_float3(max(a.x, b.x), max(a.y, b.y), max(a.z, b.z));
|
2021-11-18 14:25:05 +01:00
|
|
|
# endif
|
2017-04-14 14:05:23 +02:00
|
|
|
}
|
|
|
|
|
|
2022-11-01 15:16:55 +01:00
|
|
|
ccl_device_inline float3 clamp(const float3 a, const float3 mn, const float3 mx)
|
2017-04-14 14:05:23 +02:00
|
|
|
{
|
|
|
|
|
return min(max(a, mn), mx);
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-01 15:16:55 +01:00
|
|
|
ccl_device_inline float3 fabs(const float3 a)
|
2017-04-14 14:05:23 +02:00
|
|
|
{
|
2021-11-18 14:25:05 +01:00
|
|
|
# ifdef __KERNEL_SSE__
|
|
|
|
|
# ifdef __KERNEL_NEON__
|
2021-02-14 15:01:26 +01:00
|
|
|
return float3(vabsq_f32(a.m128));
|
2021-11-18 14:25:05 +01:00
|
|
|
# else
|
2017-04-14 14:05:23 +02:00
|
|
|
__m128 mask = _mm_castsi128_ps(_mm_set1_epi32(0x7fffffff));
|
|
|
|
|
return float3(_mm_and_ps(a.m128, mask));
|
2021-11-18 14:25:05 +01:00
|
|
|
# endif
|
|
|
|
|
# else
|
Cycles: merge of cycles-x branch, a major update to the renderer
This includes much improved GPU rendering performance, viewport interactivity,
new shadow catcher, revamped sampling settings, subsurface scattering anisotropy,
new GPU volume sampling, improved PMJ sampling pattern, and more.
Some features have also been removed or changed, breaking backwards compatibility.
Including the removal of the OpenCL backend, for which alternatives are under
development.
Release notes and code docs:
https://wiki.blender.org/wiki/Reference/Release_Notes/3.0/Cycles
https://wiki.blender.org/wiki/Source/Render/Cycles
Credits:
* Sergey Sharybin
* Brecht Van Lommel
* Patrick Mours (OptiX backend)
* Christophe Hery (subsurface scattering anisotropy)
* William Leeson (PMJ sampling pattern)
* Alaska (various fixes and tweaks)
* Thomas Dinges (various fixes)
For the full commit history, see the cycles-x branch. This squashes together
all the changes since intermediate changes would often fail building or tests.
Ref T87839, T87837, T87836
Fixes T90734, T89353, T80267, T80267, T77185, T69800
2021-09-20 17:59:20 +02:00
|
|
|
return make_float3(fabsf(a.x), fabsf(a.y), fabsf(a.z));
|
2021-11-18 14:25:05 +01:00
|
|
|
# endif
|
2017-04-14 14:05:23 +02:00
|
|
|
}
|
|
|
|
|
|
2022-11-01 15:16:55 +01:00
|
|
|
ccl_device_inline float3 sqrt(const float3 a)
|
2018-02-28 04:36:55 +01:00
|
|
|
{
|
2021-11-18 14:25:05 +01:00
|
|
|
# ifdef __KERNEL_SSE__
|
2018-02-28 04:36:55 +01:00
|
|
|
return float3(_mm_sqrt_ps(a));
|
2021-11-18 14:25:05 +01:00
|
|
|
# else
|
2018-02-28 04:36:55 +01:00
|
|
|
return make_float3(sqrtf(a.x), sqrtf(a.y), sqrtf(a.z));
|
2021-11-18 14:25:05 +01:00
|
|
|
# endif
|
2018-02-28 04:36:55 +01:00
|
|
|
}
|
|
|
|
|
|
2022-11-01 15:16:55 +01:00
|
|
|
ccl_device_inline float3 floor(const float3 a)
|
Shading: Add more operators to Vector Math node.
Add Multiply, Divide, Project, Reflect, Distance, Length, Scale, Snap,
Floor, Ceil, Modulo, Fraction, Absolute, Minimum, and Maximum operators
to the Vector Math node. The Value output has been removed from operators
whose output is a vector, and the other way around. All of those removals
has been handled properly in versioning code.
The patch doesn't include tests for the new operators. Tests will be added
in a later patch.
Reviewers: brecht, JacquesLucke
Differential Revision: https://developer.blender.org/D5523
2019-08-21 19:36:33 +02:00
|
|
|
{
|
2021-11-18 14:25:05 +01:00
|
|
|
# ifdef __KERNEL_SSE__
|
Shading: Add more operators to Vector Math node.
Add Multiply, Divide, Project, Reflect, Distance, Length, Scale, Snap,
Floor, Ceil, Modulo, Fraction, Absolute, Minimum, and Maximum operators
to the Vector Math node. The Value output has been removed from operators
whose output is a vector, and the other way around. All of those removals
has been handled properly in versioning code.
The patch doesn't include tests for the new operators. Tests will be added
in a later patch.
Reviewers: brecht, JacquesLucke
Differential Revision: https://developer.blender.org/D5523
2019-08-21 19:36:33 +02:00
|
|
|
return float3(_mm_floor_ps(a));
|
2021-11-18 14:25:05 +01:00
|
|
|
# else
|
Shading: Add more operators to Vector Math node.
Add Multiply, Divide, Project, Reflect, Distance, Length, Scale, Snap,
Floor, Ceil, Modulo, Fraction, Absolute, Minimum, and Maximum operators
to the Vector Math node. The Value output has been removed from operators
whose output is a vector, and the other way around. All of those removals
has been handled properly in versioning code.
The patch doesn't include tests for the new operators. Tests will be added
in a later patch.
Reviewers: brecht, JacquesLucke
Differential Revision: https://developer.blender.org/D5523
2019-08-21 19:36:33 +02:00
|
|
|
return make_float3(floorf(a.x), floorf(a.y), floorf(a.z));
|
2021-11-18 14:25:05 +01:00
|
|
|
# endif
|
Shading: Add more operators to Vector Math node.
Add Multiply, Divide, Project, Reflect, Distance, Length, Scale, Snap,
Floor, Ceil, Modulo, Fraction, Absolute, Minimum, and Maximum operators
to the Vector Math node. The Value output has been removed from operators
whose output is a vector, and the other way around. All of those removals
has been handled properly in versioning code.
The patch doesn't include tests for the new operators. Tests will be added
in a later patch.
Reviewers: brecht, JacquesLucke
Differential Revision: https://developer.blender.org/D5523
2019-08-21 19:36:33 +02:00
|
|
|
}
|
|
|
|
|
|
2022-11-01 15:16:55 +01:00
|
|
|
ccl_device_inline float3 ceil(const float3 a)
|
Shading: Add more operators to Vector Math node.
Add Multiply, Divide, Project, Reflect, Distance, Length, Scale, Snap,
Floor, Ceil, Modulo, Fraction, Absolute, Minimum, and Maximum operators
to the Vector Math node. The Value output has been removed from operators
whose output is a vector, and the other way around. All of those removals
has been handled properly in versioning code.
The patch doesn't include tests for the new operators. Tests will be added
in a later patch.
Reviewers: brecht, JacquesLucke
Differential Revision: https://developer.blender.org/D5523
2019-08-21 19:36:33 +02:00
|
|
|
{
|
2021-11-18 14:25:05 +01:00
|
|
|
# ifdef __KERNEL_SSE__
|
Shading: Add more operators to Vector Math node.
Add Multiply, Divide, Project, Reflect, Distance, Length, Scale, Snap,
Floor, Ceil, Modulo, Fraction, Absolute, Minimum, and Maximum operators
to the Vector Math node. The Value output has been removed from operators
whose output is a vector, and the other way around. All of those removals
has been handled properly in versioning code.
The patch doesn't include tests for the new operators. Tests will be added
in a later patch.
Reviewers: brecht, JacquesLucke
Differential Revision: https://developer.blender.org/D5523
2019-08-21 19:36:33 +02:00
|
|
|
return float3(_mm_ceil_ps(a));
|
2021-11-18 14:25:05 +01:00
|
|
|
# else
|
Shading: Add more operators to Vector Math node.
Add Multiply, Divide, Project, Reflect, Distance, Length, Scale, Snap,
Floor, Ceil, Modulo, Fraction, Absolute, Minimum, and Maximum operators
to the Vector Math node. The Value output has been removed from operators
whose output is a vector, and the other way around. All of those removals
has been handled properly in versioning code.
The patch doesn't include tests for the new operators. Tests will be added
in a later patch.
Reviewers: brecht, JacquesLucke
Differential Revision: https://developer.blender.org/D5523
2019-08-21 19:36:33 +02:00
|
|
|
return make_float3(ceilf(a.x), ceilf(a.y), ceilf(a.z));
|
2021-11-18 14:25:05 +01:00
|
|
|
# endif
|
Shading: Add more operators to Vector Math node.
Add Multiply, Divide, Project, Reflect, Distance, Length, Scale, Snap,
Floor, Ceil, Modulo, Fraction, Absolute, Minimum, and Maximum operators
to the Vector Math node. The Value output has been removed from operators
whose output is a vector, and the other way around. All of those removals
has been handled properly in versioning code.
The patch doesn't include tests for the new operators. Tests will be added
in a later patch.
Reviewers: brecht, JacquesLucke
Differential Revision: https://developer.blender.org/D5523
2019-08-21 19:36:33 +02:00
|
|
|
}
|
|
|
|
|
|
2022-11-01 15:16:55 +01:00
|
|
|
ccl_device_inline float3 mix(const float3 a, const float3 b, float t)
|
2017-04-14 14:05:23 +02:00
|
|
|
{
|
|
|
|
|
return a + t * (b - a);
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-01 15:16:55 +01:00
|
|
|
ccl_device_inline float3 rcp(const float3 a)
|
2017-04-14 14:05:23 +02:00
|
|
|
{
|
2021-11-18 14:25:05 +01:00
|
|
|
# ifdef __KERNEL_SSE__
|
2017-08-02 02:23:03 +02:00
|
|
|
/* Don't use _mm_rcp_ps due to poor precision. */
|
|
|
|
|
return float3(_mm_div_ps(_mm_set_ps1(1.0f), a.m128));
|
2021-11-18 14:25:05 +01:00
|
|
|
# else
|
2017-04-14 14:05:23 +02:00
|
|
|
return make_float3(1.0f / a.x, 1.0f / a.y, 1.0f / a.z);
|
2021-11-18 14:25:05 +01:00
|
|
|
# endif
|
2017-04-14 14:05:23 +02:00
|
|
|
}
|
2022-06-23 14:29:17 +02:00
|
|
|
|
|
|
|
|
ccl_device_inline float3 saturate(float3 a)
|
|
|
|
|
{
|
|
|
|
|
return make_float3(saturatef(a.x), saturatef(a.y), saturatef(a.z));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ccl_device_inline float3 exp(float3 v)
|
|
|
|
|
{
|
|
|
|
|
return make_float3(expf(v.x), expf(v.y), expf(v.z));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ccl_device_inline float3 log(float3 v)
|
|
|
|
|
{
|
|
|
|
|
return make_float3(logf(v.x), logf(v.y), logf(v.z));
|
|
|
|
|
}
|
|
|
|
|
|
Shading: Add more operators to Vector Math node.
Add Multiply, Divide, Project, Reflect, Distance, Length, Scale, Snap,
Floor, Ceil, Modulo, Fraction, Absolute, Minimum, and Maximum operators
to the Vector Math node. The Value output has been removed from operators
whose output is a vector, and the other way around. All of those removals
has been handled properly in versioning code.
The patch doesn't include tests for the new operators. Tests will be added
in a later patch.
Reviewers: brecht, JacquesLucke
Differential Revision: https://developer.blender.org/D5523
2019-08-21 19:36:33 +02:00
|
|
|
ccl_device_inline float3 reflect(const float3 incident, const float3 normal)
|
|
|
|
|
{
|
|
|
|
|
float3 unit_normal = normalize(normal);
|
|
|
|
|
return incident - 2.0f * unit_normal * dot(incident, unit_normal);
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-23 09:21:56 +00:00
|
|
|
ccl_device_inline float3 refract(const float3 incident, const float3 normal, const float eta)
|
|
|
|
|
{
|
|
|
|
|
float k = 1.0f - eta * eta * (1.0f - dot(normal, incident) * dot(normal, incident));
|
|
|
|
|
if (k < 0.0f)
|
|
|
|
|
return zero_float3();
|
|
|
|
|
else
|
|
|
|
|
return eta * incident - (eta * dot(normal, incident) + sqrt(k)) * normal;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ccl_device_inline float3 faceforward(const float3 vector,
|
|
|
|
|
const float3 incident,
|
|
|
|
|
const float3 reference)
|
|
|
|
|
{
|
|
|
|
|
return (dot(reference, incident) < 0.0f) ? vector : -vector;
|
|
|
|
|
}
|
2021-11-18 14:25:05 +01:00
|
|
|
#endif
|
2021-03-23 09:21:56 +00:00
|
|
|
|
Shading: Add more operators to Vector Math node.
Add Multiply, Divide, Project, Reflect, Distance, Length, Scale, Snap,
Floor, Ceil, Modulo, Fraction, Absolute, Minimum, and Maximum operators
to the Vector Math node. The Value output has been removed from operators
whose output is a vector, and the other way around. All of those removals
has been handled properly in versioning code.
The patch doesn't include tests for the new operators. Tests will be added
in a later patch.
Reviewers: brecht, JacquesLucke
Differential Revision: https://developer.blender.org/D5523
2019-08-21 19:36:33 +02:00
|
|
|
ccl_device_inline float3 project(const float3 v, const float3 v_proj)
|
|
|
|
|
{
|
|
|
|
|
float len_squared = dot(v_proj, v_proj);
|
2021-02-17 01:47:18 +01:00
|
|
|
return (len_squared != 0.0f) ? (dot(v, v_proj) / len_squared) * v_proj : zero_float3();
|
Shading: Add more operators to Vector Math node.
Add Multiply, Divide, Project, Reflect, Distance, Length, Scale, Snap,
Floor, Ceil, Modulo, Fraction, Absolute, Minimum, and Maximum operators
to the Vector Math node. The Value output has been removed from operators
whose output is a vector, and the other way around. All of those removals
has been handled properly in versioning code.
The patch doesn't include tests for the new operators. Tests will be added
in a later patch.
Reviewers: brecht, JacquesLucke
Differential Revision: https://developer.blender.org/D5523
2019-08-21 19:36:33 +02:00
|
|
|
}
|
|
|
|
|
|
Cycles: Kernel address space changes for MSL
This is the first of a sequence of changes to support compiling Cycles kernels as MSL (Metal Shading Language) in preparation for a Metal GPU device implementation.
MSL requires that all pointer types be declared with explicit address space attributes (device, thread, etc...). There is already precedent for this with Cycles' address space macros (ccl_global, ccl_private, etc...), therefore the first step of MSL-enablement is to apply these consistently. Line-for-line this represents the largest change required to enable MSL. Applying this change first will simplify future patches as well as offering the emergent benefit of enhanced descriptiveness.
The vast majority of deltas in this patch fall into one of two cases:
- Ensuring ccl_private is specified for thread-local pointer types
- Ensuring ccl_global is specified for device-wide pointer types
Additionally, the ccl_addr_space qualifier can be removed. Prior to Cycles X, ccl_addr_space was used as a context-dependent address space qualifier, but now it is either redundant (e.g. in struct typedefs), or can be replaced by ccl_global in the case of pointer types. Associated function variants (e.g. lcg_step_float_addrspace) are also redundant.
In cases where address space qualifiers are chained with "const", this patch places the address space qualifier first. The rationale for this is that the choice of address space is likely to have the greater impact on runtime performance and overall architecture.
The final part of this patch is the addition of a metal/compat.h header. This is partially complete and will be extended in future patches, paving the way for the full Metal implementation.
Ref T92212
Reviewed By: brecht
Maniphest Tasks: T92212
Differential Revision: https://developer.blender.org/D12864
2021-10-14 13:53:40 +01:00
|
|
|
ccl_device_inline float3 normalize_len(const float3 a, ccl_private float *t)
|
2017-04-14 14:05:23 +02:00
|
|
|
{
|
|
|
|
|
*t = len(a);
|
|
|
|
|
float x = 1.0f / *t;
|
|
|
|
|
return a * x;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ccl_device_inline float3 safe_normalize(const float3 a)
|
|
|
|
|
{
|
|
|
|
|
float t = len(a);
|
|
|
|
|
return (t != 0.0f) ? a * (1.0f / t) : a;
|
|
|
|
|
}
|
|
|
|
|
|
Cycles: Kernel address space changes for MSL
This is the first of a sequence of changes to support compiling Cycles kernels as MSL (Metal Shading Language) in preparation for a Metal GPU device implementation.
MSL requires that all pointer types be declared with explicit address space attributes (device, thread, etc...). There is already precedent for this with Cycles' address space macros (ccl_global, ccl_private, etc...), therefore the first step of MSL-enablement is to apply these consistently. Line-for-line this represents the largest change required to enable MSL. Applying this change first will simplify future patches as well as offering the emergent benefit of enhanced descriptiveness.
The vast majority of deltas in this patch fall into one of two cases:
- Ensuring ccl_private is specified for thread-local pointer types
- Ensuring ccl_global is specified for device-wide pointer types
Additionally, the ccl_addr_space qualifier can be removed. Prior to Cycles X, ccl_addr_space was used as a context-dependent address space qualifier, but now it is either redundant (e.g. in struct typedefs), or can be replaced by ccl_global in the case of pointer types. Associated function variants (e.g. lcg_step_float_addrspace) are also redundant.
In cases where address space qualifiers are chained with "const", this patch places the address space qualifier first. The rationale for this is that the choice of address space is likely to have the greater impact on runtime performance and overall architecture.
The final part of this patch is the addition of a metal/compat.h header. This is partially complete and will be extended in future patches, paving the way for the full Metal implementation.
Ref T92212
Reviewed By: brecht
Maniphest Tasks: T92212
Differential Revision: https://developer.blender.org/D12864
2021-10-14 13:53:40 +01:00
|
|
|
ccl_device_inline float3 safe_normalize_len(const float3 a, ccl_private float *t)
|
2017-04-14 14:05:23 +02:00
|
|
|
{
|
|
|
|
|
*t = len(a);
|
|
|
|
|
return (*t != 0.0f) ? a / (*t) : a;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-23 14:29:17 +02:00
|
|
|
ccl_device_inline float3 safe_divide(const float3 a, const float3 b)
|
Shading: Add more operators to Vector Math node.
Add Multiply, Divide, Project, Reflect, Distance, Length, Scale, Snap,
Floor, Ceil, Modulo, Fraction, Absolute, Minimum, and Maximum operators
to the Vector Math node. The Value output has been removed from operators
whose output is a vector, and the other way around. All of those removals
has been handled properly in versioning code.
The patch doesn't include tests for the new operators. Tests will be added
in a later patch.
Reviewers: brecht, JacquesLucke
Differential Revision: https://developer.blender.org/D5523
2019-08-21 19:36:33 +02:00
|
|
|
{
|
|
|
|
|
return make_float3((b.x != 0.0f) ? a.x / b.x : 0.0f,
|
|
|
|
|
(b.y != 0.0f) ? a.y / b.y : 0.0f,
|
|
|
|
|
(b.z != 0.0f) ? a.z / b.z : 0.0f);
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-23 14:29:17 +02:00
|
|
|
ccl_device_inline float3 safe_divide(const float3 a, const float b)
|
Shading: Add more operators to Vector Math node.
Add Multiply, Divide, Project, Reflect, Distance, Length, Scale, Snap,
Floor, Ceil, Modulo, Fraction, Absolute, Minimum, and Maximum operators
to the Vector Math node. The Value output has been removed from operators
whose output is a vector, and the other way around. All of those removals
has been handled properly in versioning code.
The patch doesn't include tests for the new operators. Tests will be added
in a later patch.
Reviewers: brecht, JacquesLucke
Differential Revision: https://developer.blender.org/D5523
2019-08-21 19:36:33 +02:00
|
|
|
{
|
2021-02-17 01:47:18 +01:00
|
|
|
return (b != 0.0f) ? a / b : zero_float3();
|
Shading: Add more operators to Vector Math node.
Add Multiply, Divide, Project, Reflect, Distance, Length, Scale, Snap,
Floor, Ceil, Modulo, Fraction, Absolute, Minimum, and Maximum operators
to the Vector Math node. The Value output has been removed from operators
whose output is a vector, and the other way around. All of those removals
has been handled properly in versioning code.
The patch doesn't include tests for the new operators. Tests will be added
in a later patch.
Reviewers: brecht, JacquesLucke
Differential Revision: https://developer.blender.org/D5523
2019-08-21 19:36:33 +02:00
|
|
|
}
|
|
|
|
|
|
2017-04-14 14:05:23 +02:00
|
|
|
ccl_device_inline float3 interp(float3 a, float3 b, float t)
|
|
|
|
|
{
|
|
|
|
|
return a + t * (b - a);
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-23 14:29:17 +02:00
|
|
|
ccl_device_inline float3 sqr(float3 a)
|
2019-05-31 22:38:50 +02:00
|
|
|
{
|
|
|
|
|
return a * a;
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-14 14:05:23 +02:00
|
|
|
ccl_device_inline bool is_zero(const float3 a)
|
|
|
|
|
{
|
|
|
|
|
#ifdef __KERNEL_SSE__
|
|
|
|
|
return a == make_float3(0.0f);
|
|
|
|
|
#else
|
|
|
|
|
return (a.x == 0.0f && a.y == 0.0f && a.z == 0.0f);
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ccl_device_inline float reduce_add(const float3 a)
|
|
|
|
|
{
|
2021-02-14 15:01:26 +01:00
|
|
|
#if defined(__KERNEL_SSE__) && defined(__KERNEL_NEON__)
|
|
|
|
|
__m128 t = a.m128;
|
|
|
|
|
t[3] = 0.0f;
|
|
|
|
|
return vaddvq_f32(t);
|
|
|
|
|
#else
|
2017-04-14 14:05:23 +02:00
|
|
|
return (a.x + a.y + a.z);
|
2021-02-14 15:01:26 +01:00
|
|
|
#endif
|
2017-04-14 14:05:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ccl_device_inline float average(const float3 a)
|
|
|
|
|
{
|
|
|
|
|
return reduce_add(a) * (1.0f / 3.0f);
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-23 14:29:17 +02:00
|
|
|
ccl_device_inline bool isequal(const float3 a, const float3 b)
|
2017-04-14 14:05:23 +02:00
|
|
|
{
|
2021-11-18 14:25:05 +01:00
|
|
|
#if defined(__KERNEL_METAL__)
|
|
|
|
|
return all(a == b);
|
|
|
|
|
#else
|
2017-04-14 14:05:23 +02:00
|
|
|
return a == b;
|
2021-11-18 14:25:05 +01:00
|
|
|
#endif
|
2017-04-14 14:05:23 +02:00
|
|
|
}
|
2017-05-07 14:40:58 +02:00
|
|
|
|
2023-06-13 09:18:12 +02:00
|
|
|
/* Consistent name for this would be pow, but HIP compiler crashes in name mangling. */
|
|
|
|
|
ccl_device_inline float3 power(float3 v, float e)
|
2018-07-20 19:07:35 +02:00
|
|
|
{
|
|
|
|
|
return make_float3(powf(v.x, e), powf(v.y, e), powf(v.z, e));
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-23 14:29:17 +02:00
|
|
|
ccl_device_inline bool isfinite_safe(float3 v)
|
2017-05-19 23:04:13 +02:00
|
|
|
{
|
|
|
|
|
return isfinite_safe(v.x) && isfinite_safe(v.y) && isfinite_safe(v.z);
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-23 14:29:17 +02:00
|
|
|
ccl_device_inline float3 ensure_finite(float3 v)
|
2017-05-07 14:40:58 +02:00
|
|
|
{
|
2017-06-29 22:52:31 -04:00
|
|
|
if (!isfinite_safe(v.x))
|
|
|
|
|
v.x = 0.0f;
|
|
|
|
|
if (!isfinite_safe(v.y))
|
|
|
|
|
v.y = 0.0f;
|
|
|
|
|
if (!isfinite_safe(v.z))
|
|
|
|
|
v.z = 0.0f;
|
2017-05-07 14:40:58 +02:00
|
|
|
return v;
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-14 14:05:23 +02:00
|
|
|
CCL_NAMESPACE_END
|
|
|
|
|
|
|
|
|
|
#endif /* __UTIL_MATH_FLOAT3_H__ */
|