Add Chromium-only Blender WebEngine parity work

This commit is contained in:
mes123456
2026-08-12 04:47:48 -04:00
commit 9fd26010f6
18225 changed files with 11622124 additions and 0 deletions

View File

@@ -0,0 +1,171 @@
# SPDX-FileCopyrightText: 2011-2022 Blender Foundation
#
# SPDX-License-Identifier: Apache-2.0
set(INC
..
)
set(INC_SYS
)
set(SRC
aligned_malloc.cpp
colorspace.cpp
debug.cpp
guarded_allocator.cpp
ies.cpp
image_maketx.cpp
image_metadata.cpp
implicit_sharing.cpp
log.cpp
math_cdf.cpp
md5.cpp
murmurhash.cpp
nanovdb.cpp
openvdb.cpp
path.cpp
profiling.cpp
string.cpp
system.cpp
task.cpp
thread.cpp
time.cpp
transform.cpp
transform_avx2.cpp
windows.cpp
)
set(LIB
PUBLIC bf::dependencies::openimageio
PUBLIC bf::dependencies::optional::tbb
PUBLIC bf::dependencies::optional::embree
PUBLIC bf::dependencies::optional::openpgl
PUBLIC bf::dependencies::optional::openvdb
PRIVATE bf::dependencies::opencolorio
PRIVATE bf::dependencies::zstd
PRIVATE bf::dependencies::openexr
# Used by OpenImageIO headers, which fallback to a bundled version. But we
# need to use the same version as Blender to avoid symbol conflicts.
PRIVATE bf::dependencies::fmt
)
set(SRC_HEADERS
algorithm.h
aligned_malloc.h
args.h
array.h
atomic.h
boundbox.h
cache_limiter.h
color.h
colorspace.h
concurrent_set.h
concurrent_vector.h
debug.h
defines.h
deque.h
disjoint_set.h
guarded_allocator.h
guiding.h
half.h
hash.h
ies.h
image.h
image_impl.h
image_maketx.h
image_metadata.h
implicit_sharing.h
list.h
log.h
map.h
math.h
math_base.h
math_cdf.h
math_fast.h
math_intersect.h
math_float2.h
math_float3.h
math_float4.h
math_float8.h
math_int2.h
math_int3.h
math_int4.h
math_int8.h
math_dual.h
md5.h
murmurhash.h
nanovdb.h
openimagedenoise.h
openvdb.h
optimization.h
param.h
path.h
profiling.h
progress.h
projection.h
projection_inverse.h
queue.h
random_access_iterator_mixin.h
rect.h
scoped_defer.h
set.h
simd.h
semaphore.h
stack_allocator.h
static_assert.h
stats.h
string.h
system.h
task.h
tbb.h
thread.h
time.h
transform.h
types.h
types_base.h
types_float2.h
types_float3.h
types_float4.h
types_float8.h
types_image.h
types_int2.h
types_int3.h
types_int4.h
types_int8.h
types_normal.h
types_spectrum.h
types_rgbe.h
types_uchar2.h
types_uchar3.h
types_uchar4.h
types_uint2.h
types_uint3.h
types_uint4.h
types_ushort4.h
types_dual.h
unique_ptr.h
unique_ptr_vector.h
vector.h
version.h
windows.h
xml.h
)
if(CXX_HAS_AVX2)
set_source_files_properties(transform_avx2.cpp PROPERTIES COMPILE_FLAGS "${CYCLES_AVX2_FLAGS}")
endif()
include_directories(${INC})
include_directories(SYSTEM ${INC_SYS})
# This works around the issue described in #120317 and https://github.com/AcademySoftwareFoundation/openvdb/pull/1786
if(WITH_OPENVDB AND MSVC_CLANG)
set_source_files_properties(openvdb.cpp PROPERTIES COMPILE_FLAGS -fno-delayed-template-parsing)
endif()
if(WIN32 AND NOT USD_OVERRIDE_OPENCOLORIO)
add_definitions(-DOpenColorIO_SKIP_IMPORTS)
endif()
cycles_add_library(cycles_util "${LIB}" ${SRC} ${SRC_HEADERS})

View File

@@ -0,0 +1,17 @@
/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#pragma once
#include <algorithm> // IWYU pragma: export
CCL_NAMESPACE_BEGIN
using std::remove;
using std::sort;
using std::stable_sort;
using std::swap;
using std::upper_bound;
CCL_NAMESPACE_END

View File

@@ -0,0 +1,73 @@
/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#include "util/aligned_malloc.h"
#include "util/guarded_allocator.h"
#ifdef WITH_BLENDER_GUARDEDALLOC
# include "../../guardedalloc/MEM_guardedalloc.h"
#endif
#include <cassert>
/* Adopted from Libmv. */
#if !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__NetBSD__) && !defined(__OpenBSD__)
/* Needed for memalign on Linux and _aligned_alloc on Windows. */
# ifdef FREE_WINDOWS
/* Make sure _aligned_malloc is included. */
# ifdef __MSVCRT_VERSION__
# undef __MSVCRT_VERSION__
# endif
# define __MSVCRT_VERSION__ 0x0700
# endif /* FREE_WINDOWS */
# include <malloc.h>
#else
/* Apple's `malloc` is 16-byte aligned, and does not have `malloc.h`, so include
* `stdilb` instead.
*/
# include <cstdlib>
#endif
CCL_NAMESPACE_BEGIN
void *util_aligned_malloc(const size_t size, const int alignment)
{
void *mem = nullptr;
#ifdef WITH_BLENDER_GUARDEDALLOC
mem = MEM_new_uninitialized_aligned(size, alignment, "Cycles Aligned Alloc");
#elif defined(_WIN32)
mem = _aligned_malloc(size, alignment);
#elif defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
if (posix_memalign(&mem, alignment, size)) {
/* Non-zero means allocation error
* either no allocation or bad alignment value. */
mem = nullptr;
}
#else /* This is for Linux. */
mem = memalign(alignment, size);
#endif
if (mem) {
util_guarded_mem_alloc(size);
}
return mem;
}
void util_aligned_free(void *ptr, const size_t size)
{
if (ptr) {
util_guarded_mem_free(size);
}
#if defined(WITH_BLENDER_GUARDEDALLOC)
if (ptr != nullptr) {
MEM_delete_void(ptr);
}
#elif defined(_WIN32)
_aligned_free(ptr);
#else
free(ptr);
#endif
}
CCL_NAMESPACE_END

View File

@@ -0,0 +1,37 @@
/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#pragma once
#include <cstddef>
CCL_NAMESPACE_BEGIN
/* Minimum alignment needed by all CPU native data types (SSE, AVX). */
#define MIN_ALIGNMENT_CPU_DATA_TYPES 16 // NOLINT
/* NanoVDB needs at least 32 byte alignment. */
#define MIN_ALIGNMENT_DEVICE_MEMORY 32 // NOLINT
/* Allocate block of size bytes at least aligned to a given value. */
void *util_aligned_malloc(const size_t size, const int alignment);
/* Free memory allocated by util_aligned_malloc. */
void util_aligned_free(void *ptr, const size_t size);
/* Aligned new operator. */
template<typename T, typename... Args> T *util_aligned_new(Args... args)
{
void *mem = util_aligned_malloc(sizeof(T), alignof(T));
return new (mem) T(args...);
}
template<typename T> void util_aligned_delete(T *t)
{
if (t) {
t->~T();
util_aligned_free(t, sizeof(T));
}
}
CCL_NAMESPACE_END

View File

@@ -0,0 +1,16 @@
/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#pragma once
/* Argument Parsing for command line, we use the OpenImageIO
* library because it has nice functions to do this. */
#include <OpenImageIO/argparse.h>
CCL_NAMESPACE_BEGIN
using OIIO::ArgParse;
CCL_NAMESPACE_END

View File

@@ -0,0 +1,310 @@
/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#pragma once
#include <cassert>
#include <cstring>
#include "util/aligned_malloc.h"
#include "util/vector.h"
CCL_NAMESPACE_BEGIN
/* Simplified version of vector, serving multiple purposes:
* - somewhat faster in that it does not clear memory on resize/alloc,
* this was actually showing up in profiles quite significantly. it
* also does not run any constructors/destructors
* - if this is used, we are not tempted to use inefficient operations
* - aligned allocation for CPU native data types */
template<typename T, const size_t alignment = MIN_ALIGNMENT_CPU_DATA_TYPES> class array {
public:
array() : data_(nullptr), datasize_(0), capacity_(0) {}
explicit array(const size_t newsize)
{
if (newsize == 0) {
data_ = nullptr;
datasize_ = 0;
capacity_ = 0;
}
else {
data_ = mem_allocate(newsize);
datasize_ = newsize;
capacity_ = datasize_;
}
}
array(const array &from)
{
if (from.datasize_ == 0) {
data_ = nullptr;
datasize_ = 0;
capacity_ = 0;
}
else {
data_ = mem_allocate(from.datasize_);
if (from.datasize_ > 0) {
mem_copy(data_, from.data_, from.datasize_);
}
datasize_ = from.datasize_;
capacity_ = datasize_;
}
}
array(array &&from)
{
data_ = from.data_;
datasize_ = from.datasize_;
capacity_ = from.capacity_;
from.data_ = nullptr;
from.datasize_ = 0;
from.capacity_ = 0;
}
array &operator=(const array &from)
{
if (this != &from) {
resize(from.size());
if (datasize_ > 0) {
mem_copy(data_, from.data_, datasize_);
}
}
return *this;
}
array &operator=(const vector<T> &from)
{
resize(from.size());
if (from.size() > 0 && datasize_ > 0) {
mem_copy(data_, from.data(), datasize_);
}
return *this;
}
~array()
{
mem_free(data_, capacity_);
}
bool operator==(const array<T> &other) const
{
if (datasize_ != other.datasize_) {
return false;
}
if (datasize_ == 0) {
return true;
}
return memcmp(data_, other.data_, datasize_ * sizeof(T)) == 0;
}
bool operator!=(const array<T> &other) const
{
return !(*this == other);
}
void steal_data(array &from)
{
if (this != &from) {
clear();
data_ = from.data_;
datasize_ = from.datasize_;
capacity_ = from.capacity_;
from.data_ = nullptr;
from.datasize_ = 0;
from.capacity_ = 0;
}
}
void set_data(T *ptr_, size_t datasize)
{
clear();
data_ = ptr_;
datasize_ = datasize;
capacity_ = datasize;
}
T *steal_pointer()
{
T *ptr = data_;
data_ = nullptr;
clear();
return ptr;
}
T *resize(const size_t newsize)
{
if (newsize == 0) {
clear();
}
else if (newsize != datasize_) {
if (newsize > capacity_) {
T *newdata = mem_allocate(newsize);
if (newdata == nullptr) {
/* Allocation failed, likely out of memory. */
clear();
return nullptr;
}
if (data_ != nullptr) {
mem_copy(newdata, data_, ((datasize_ < newsize) ? datasize_ : newsize));
mem_free(data_, capacity_);
}
data_ = newdata;
capacity_ = newsize;
}
datasize_ = newsize;
}
return data_;
}
T *resize(const size_t newsize, const T &value)
{
size_t oldsize = size();
resize(newsize);
for (size_t i = oldsize; i < size(); i++) {
data_[i] = value;
}
return data_;
}
void clear()
{
if (data_ != nullptr) {
mem_free(data_, capacity_);
data_ = nullptr;
}
datasize_ = 0;
capacity_ = 0;
}
size_t empty() const
{
return datasize_ == 0;
}
size_t size() const
{
return datasize_;
}
T *data()
{
return data_;
}
const T *data() const
{
return data_;
}
T &operator[](size_t i) const
{
assert(i < datasize_);
return data_[i];
}
T *begin()
{
return data_;
}
const T *begin() const
{
return data_;
}
T *end()
{
return data_ + datasize_;
}
const T *end() const
{
return data_ + datasize_;
}
void reserve(const size_t newcapacity)
{
if (newcapacity > capacity_) {
T *newdata = mem_allocate(newcapacity);
if (data_ != nullptr) {
mem_copy(newdata, data_, ((datasize_ < newcapacity) ? datasize_ : newcapacity));
mem_free(data_, capacity_);
}
data_ = newdata;
capacity_ = newcapacity;
}
}
size_t capacity() const
{
return capacity_;
}
// do not use this method unless you are sure the code is not performance critical
void push_back_slow(const T &t)
{
if (capacity_ == datasize_) {
reserve(datasize_ == 0 ? 1 : (size_t)((datasize_ + 1) * 1.2));
}
data_[datasize_++] = t;
}
void push_back_reserved(const T &t)
{
assert(datasize_ < capacity_);
push_back_slow(t);
}
void append(const array<T> &from)
{
if (from.size()) {
size_t old_size = size();
resize(old_size + from.size());
mem_copy(data_ + old_size, from.data(), from.size());
}
}
protected:
T *mem_allocate(const size_t N)
{
if (N == 0) {
return nullptr;
}
T *mem = (T *)util_aligned_malloc(sizeof(T) * N, alignment);
if (mem == nullptr) {
throw std::bad_alloc();
}
return mem;
}
void mem_free(T *mem, const size_t N)
{
if (mem != nullptr) {
util_aligned_free(mem, sizeof(T) * N);
}
}
void mem_copy(T *mem_to, const T *mem_from, const size_t N)
{
memcpy((void *)mem_to, mem_from, sizeof(T) * N);
}
T *data_;
size_t datasize_;
size_t capacity_;
};
CCL_NAMESPACE_END

View File

@@ -0,0 +1,315 @@
/* SPDX-FileCopyrightText: 2014-2022 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#pragma once
#ifndef __KERNEL_GPU__
/* Using atomic ops header from Blender. */
# include "atomic_ops.h" // IWYU pragma: export
# define atomic_add_and_fetch_float(p, x) atomic_add_and_fetch_fl((p), (x))
# define atomic_compare_and_swap_float(p, old_val, new_val) \
atomic_cas_float((p), (old_val), (new_val))
# define atomic_fetch_and_inc_uint32(p) atomic_fetch_and_add_uint32((p), 1)
# define atomic_fetch_and_dec_uint32(p) atomic_fetch_and_add_uint32((p), -1)
# define CCL_LOCAL_MEM_FENCE 0
# define ccl_barrier(flags) ((void)0)
#else /* __KERNEL_GPU__ */
# ifndef __KERNEL_ONEAPI__
# define atomic_fetch_and_add_uint32_shared atomic_fetch_and_add_uint32
# endif
# if defined(__KERNEL_CUDA__) || defined(__KERNEL_HIP__)
# define atomic_add_and_fetch_float(p, x) (atomicAdd((float *)(p), (float)(x)) + (float)(x))
# define atomic_fetch_and_add_uint32(p, x) atomicAdd((unsigned int *)(p), (unsigned int)(x))
# define atomic_fetch_and_sub_uint32(p, x) atomicSub((unsigned int *)(p), (unsigned int)(x))
# define atomic_fetch_and_inc_uint32(p) atomic_fetch_and_add_uint32((p), 1)
# define atomic_fetch_and_dec_uint32(p) atomic_fetch_and_sub_uint32((p), 1)
# define atomic_fetch_and_or_uint32(p, x) atomicOr((unsigned int *)(p), (unsigned int)(x))
ccl_device_inline float atomic_compare_and_swap_float(volatile float *dest,
const float old_val,
const float new_val)
{
union {
unsigned int int_value;
float float_value;
} new_value, prev_value, result;
prev_value.float_value = old_val;
new_value.float_value = new_val;
result.int_value = atomicCAS((unsigned int *)dest, prev_value.int_value, new_value.int_value);
return result.float_value;
}
# define CCL_LOCAL_MEM_FENCE
# define ccl_barrier(flags) __syncthreads()
# endif /* __KERNEL_CUDA__ */
# ifdef __KERNEL_METAL__
// global address space versions
ccl_device_inline float atomic_add_and_fetch_float(volatile ccl_global float *_source,
const float operand)
{
# if __METAL_VERSION__ >= 300
return atomic_fetch_add_explicit(
(ccl_global atomic_float *)_source, operand, memory_order_relaxed);
# else
volatile ccl_global atomic_int *source = (ccl_global atomic_int *)_source;
union {
int int_value;
float float_value;
} new_value, prev_value;
prev_value.int_value = atomic_load_explicit(source, memory_order_relaxed);
do {
new_value.float_value = prev_value.float_value + operand;
} while (!atomic_compare_exchange_weak_explicit(source,
&prev_value.int_value,
new_value.int_value,
memory_order_relaxed,
memory_order_relaxed));
return new_value.float_value;
# endif
}
template<class T> ccl_device_inline uint32_t atomic_fetch_and_add_uint32(device T *p, const int x)
{
return atomic_fetch_add_explicit((device atomic_uint *)p, x, memory_order_relaxed);
}
template<class T> ccl_device_inline uint32_t atomic_fetch_and_sub_uint32(device T *p, const int x)
{
return atomic_fetch_sub_explicit((device atomic_uint *)p, x, memory_order_relaxed);
}
template<class T> ccl_device_inline uint32_t atomic_fetch_and_inc_uint32(device T *p)
{
return atomic_fetch_add_explicit((device atomic_uint *)p, 1, memory_order_relaxed);
}
template<class T> ccl_device_inline uint32_t atomic_fetch_and_dec_uint32(device T *p)
{
return atomic_fetch_sub_explicit((device atomic_uint *)p, 1, memory_order_relaxed);
}
template<class T> ccl_device_inline uint32_t atomic_fetch_and_or_uint32(device T *p, const int x)
{
return atomic_fetch_or_explicit((device atomic_uint *)p, x, memory_order_relaxed);
}
template<class T>
ccl_device_inline uint32_t atomic_fetch_and_add_uint32(threadgroup T *p, const int x)
{
return atomic_fetch_add_explicit((threadgroup atomic_uint *)p, x, memory_order_relaxed);
}
template<class T>
ccl_device_inline uint32_t atomic_fetch_and_sub_uint32(threadgroup T *p, const int x)
{
return atomic_fetch_sub_explicit((threadgroup atomic_uint *)p, x, memory_order_relaxed);
}
template<class T> ccl_device_inline uint32_t atomic_fetch_and_inc_uint32(threadgroup T *p)
{
return atomic_fetch_add_explicit((threadgroup atomic_uint *)p, 1, memory_order_relaxed);
}
template<class T> ccl_device_inline uint32_t atomic_fetch_and_dec_uint32(threadgroup T *p)
{
return atomic_fetch_sub_explicit((threadgroup atomic_uint *)p, 1, memory_order_relaxed);
}
template<class T>
ccl_device_inline uint32_t atomic_fetch_and_or_uint32(threadgroup T *p, const int x)
{
return atomic_fetch_or_explicit((threadgroup atomic_uint *)p, x, memory_order_relaxed);
}
ccl_device_inline float atomic_compare_and_swap_float(volatile ccl_global float *dest,
const float old_val,
const float new_val)
{
# if __METAL_VERSION__ >= 300
float prev_value = old_val;
atomic_compare_exchange_weak_explicit((ccl_global atomic_float *)dest,
&prev_value,
new_val,
memory_order_relaxed,
memory_order_relaxed);
return prev_value;
# else
int prev_value;
prev_value = __float_as_int(old_val);
atomic_compare_exchange_weak_explicit((ccl_global atomic_int *)dest,
&prev_value,
__float_as_int(new_val),
memory_order_relaxed,
memory_order_relaxed);
return __int_as_float(prev_value);
# endif
}
# define atomic_store(p, x) atomic_store_explicit(p, x, memory_order_relaxed)
# define atomic_fetch(p) atomic_load_explicit(p, memory_order_relaxed)
# define atomic_store_local(p, x) \
atomic_store_explicit((ccl_gpu_shared atomic_int *)p, x, memory_order_relaxed)
# define atomic_load_local(p) \
atomic_load_explicit((ccl_gpu_shared atomic_int *)p, memory_order_relaxed)
# define CCL_LOCAL_MEM_FENCE mem_flags::mem_threadgroup
# define ccl_barrier(flags) threadgroup_barrier(flags)
# endif /* __KERNEL_METAL__ */
# ifdef __KERNEL_ONEAPI__
ccl_device_inline float atomic_add_and_fetch_float(ccl_global float *p, const float x)
{
sycl::atomic_ref<float,
sycl::memory_order::relaxed,
sycl::memory_scope::device,
sycl::access::address_space::ext_intel_global_device_space>
atomic(*p);
return atomic.fetch_add(x);
}
ccl_device_inline float atomic_compare_and_swap_float(ccl_global float *source,
float old_val,
float new_val)
{
sycl::atomic_ref<float,
sycl::memory_order::relaxed,
sycl::memory_scope::device,
sycl::access::address_space::ext_intel_global_device_space>
atomic(*source);
atomic.compare_exchange_weak(old_val, new_val);
return old_val;
}
ccl_device_inline unsigned int atomic_fetch_and_add_uint32(ccl_global unsigned int *p,
unsigned int x)
{
sycl::atomic_ref<unsigned int,
sycl::memory_order::relaxed,
sycl::memory_scope::device,
sycl::access::address_space::ext_intel_global_device_space>
atomic(*p);
return atomic.fetch_add(x);
}
ccl_device_inline int atomic_fetch_and_add_uint32(ccl_global int *p, const int x)
{
sycl::atomic_ref<int,
sycl::memory_order::relaxed,
sycl::memory_scope::device,
sycl::access::address_space::ext_intel_global_device_space>
atomic(*p);
return atomic.fetch_add(x);
}
ccl_device_inline int atomic_fetch_and_add_uint32_shared(int *p, const int x)
{
sycl::atomic_ref<int,
sycl::memory_order::relaxed,
sycl::memory_scope::device,
sycl::access::address_space::local_space>
atomic(*p);
return atomic.fetch_add(x);
}
ccl_device_inline unsigned int atomic_fetch_and_sub_uint32(ccl_global unsigned int *p,
unsigned int x)
{
sycl::atomic_ref<unsigned int,
sycl::memory_order::relaxed,
sycl::memory_scope::device,
sycl::access::address_space::ext_intel_global_device_space>
atomic(*p);
return atomic.fetch_sub(x);
}
ccl_device_inline int atomic_fetch_and_sub_uint32(ccl_global int *p, const int x)
{
sycl::atomic_ref<int,
sycl::memory_order::relaxed,
sycl::memory_scope::device,
sycl::access::address_space::ext_intel_global_device_space>
atomic(*p);
return atomic.fetch_sub(x);
}
ccl_device_inline unsigned int atomic_fetch_and_inc_uint32(ccl_global unsigned int *p)
{
return atomic_fetch_and_add_uint32(p, 1);
}
ccl_device_inline int atomic_fetch_and_inc_uint32(ccl_global int *p)
{
return atomic_fetch_and_add_uint32(p, 1);
}
ccl_device_inline unsigned int atomic_fetch_and_dec_uint32(ccl_global unsigned int *p)
{
return atomic_fetch_and_sub_uint32(p, 1);
}
ccl_device_inline int atomic_fetch_and_dec_uint32(ccl_global int *p)
{
return atomic_fetch_and_sub_uint32(p, 1);
}
ccl_device_inline unsigned int atomic_fetch_and_or_uint32(ccl_global unsigned int *p,
unsigned int x)
{
sycl::atomic_ref<unsigned int,
sycl::memory_order::relaxed,
sycl::memory_scope::device,
sycl::access::address_space::ext_intel_global_device_space>
atomic(*p);
return atomic.fetch_or(x);
}
ccl_device_inline int atomic_fetch_and_or_uint32(ccl_global int *p, const int x)
{
sycl::atomic_ref<int,
sycl::memory_order::relaxed,
sycl::memory_scope::device,
sycl::access::address_space::ext_intel_global_device_space>
atomic(*p);
return atomic.fetch_or(x);
}
ccl_device_inline void atomic_store_local(int *p, const int x)
{
sycl::atomic_ref<int,
sycl::memory_order::relaxed,
sycl::memory_scope::device,
sycl::access::address_space::local_space>
atomic(*p);
atomic.store(x);
}
ccl_device_inline int atomic_load_local(int *p)
{
sycl::atomic_ref<int,
sycl::memory_order::relaxed,
sycl::memory_scope::device,
sycl::access::address_space::local_space>
atomic(*p);
return atomic.load();
}
# endif /* __KERNEL_ONEAPI__ */
#endif /* __KERNEL_GPU__ */

View File

@@ -0,0 +1,287 @@
/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#pragma once
#include <cfloat>
#include <cmath>
#include "util/math.h"
#include "util/transform.h"
#include "util/types.h"
CCL_NAMESPACE_BEGIN
/* 3D BoundBox */
class BoundBox {
public:
float3 min, max;
__forceinline BoundBox() = default;
__forceinline BoundBox(const float3 &pt) : min(pt), max(pt) {}
__forceinline BoundBox(const float3 &min_, const float3 &max_) : min(min_), max(max_) {}
enum empty_t { empty = 0 };
__forceinline BoundBox(empty_t /*unused*/)
: min(make_float3(FLT_MAX, FLT_MAX, FLT_MAX)), max(make_float3(-FLT_MAX, -FLT_MAX, -FLT_MAX))
{
}
__forceinline void grow(const float3 &pt)
{
/* the order of arguments to min is such that if pt is nan, it will not
* influence the resulting bounding box */
min = ccl::min(pt, min);
max = ccl::max(pt, max);
}
__forceinline void grow(const float3 &pt, const float border)
{
const float3 shift = make_float3(border, border, border);
min = ccl::min(pt - shift, min);
max = ccl::max(pt + shift, max);
}
__forceinline void grow(const BoundBox &bbox)
{
min = ccl::min(bbox.min, min);
max = ccl::max(bbox.max, max);
}
__forceinline void grow_safe(const float3 &pt)
{
/* the order of arguments to min is such that if pt is nan, it will not
* influence the resulting bounding box */
if (isfinite(pt.x) && isfinite(pt.y) && isfinite(pt.z)) {
min = ccl::min(pt, min);
max = ccl::max(pt, max);
}
}
__forceinline void grow_safe(const float3 &pt, const float border)
{
if (isfinite(pt.x) && isfinite(pt.y) && isfinite(pt.z) && isfinite(border)) {
const float3 shift = make_float3(border, border, border);
min = ccl::min(pt - shift, min);
max = ccl::max(pt + shift, max);
}
}
__forceinline void grow_safe(const BoundBox &bbox)
{
if (isfinite_safe(bbox.min)) {
min = ccl::min(bbox.min, min);
}
if (isfinite_safe(bbox.max)) {
max = ccl::max(bbox.max, max);
}
}
__forceinline void intersect(const BoundBox &bbox)
{
min = ccl::max(min, bbox.min);
max = ccl::min(max, bbox.max);
}
/* todo: avoid using this */
__forceinline float safe_area() const
{
if (!((min.x <= max.x) && (min.y <= max.y) && (min.z <= max.z))) {
return 0.0f;
}
return area();
}
__forceinline float area() const
{
return half_area() * 2.0f;
}
__forceinline float half_area() const
{
const float3 d = max - min;
return (d.x * d.z + d.y * d.z + d.x * d.y);
}
__forceinline float3 center() const
{
return 0.5f * (min + max);
}
__forceinline float3 center2() const
{
return min + max;
}
__forceinline float3 size() const
{
return max - min;
}
__forceinline bool valid() const
{
return (min.x <= max.x) && (min.y <= max.y) && (min.z <= max.z) &&
(isfinite(min.x) && isfinite(min.y) && isfinite(min.z)) &&
(isfinite(max.x) && isfinite(max.y) && isfinite(max.z));
}
BoundBox transformed(const Transform *tfm) const
{
BoundBox result = BoundBox::empty;
/* Avoid transforming empty bounding box since the code below performs per-corner transform
* which will convert non-valid bounds to valid. */
if (!valid()) {
return result;
}
for (int i = 0; i < 8; i++) {
float3 p;
p.x = (i & 1) ? min.x : max.x;
p.y = (i & 2) ? min.y : max.y;
p.z = (i & 4) ? min.z : max.z;
result.grow(transform_point(tfm, p));
}
return result;
}
__forceinline bool intersects(const BoundBox &other)
{
const float3 center_diff = center() - other.center();
const float3 total_size = (size() + other.size()) * 0.5f;
return fabsf(center_diff.x) <= total_size.x && fabsf(center_diff.y) <= total_size.y &&
fabsf(center_diff.z) <= total_size.z;
}
};
__forceinline BoundBox merge(const BoundBox &bbox, const float3 &pt)
{
return BoundBox(min(bbox.min, pt), max(bbox.max, pt));
}
__forceinline BoundBox merge(const BoundBox &a, const BoundBox &b)
{
return BoundBox(min(a.min, b.min), max(a.max, b.max));
}
__forceinline BoundBox merge(const BoundBox &a,
const BoundBox &b,
const BoundBox &c,
const BoundBox &d)
{
return merge(merge(a, b), merge(c, d));
}
__forceinline BoundBox intersect(const BoundBox &a, const BoundBox &b)
{
return BoundBox(max(a.min, b.min), min(a.max, b.max));
}
__forceinline BoundBox intersect(const BoundBox &a, const BoundBox &b, const BoundBox &c)
{
return intersect(a, intersect(b, c));
}
/* 2D BoundBox */
class BoundBox2D {
public:
float left = 0.0f;
float right = 1.0f;
float bottom = 0.0f;
float top = 1.0f;
BoundBox2D() = default;
BoundBox2D(const float2 radius)
: left(-radius.x), right(radius.x), bottom(-radius.y), top(radius.y)
{
}
bool operator==(const BoundBox2D &other) const
{
return (left == other.left && right == other.right && bottom == other.bottom &&
top == other.top);
}
float width()
{
return right - left;
}
float height()
{
return top - bottom;
}
BoundBox2D operator*(float f) const
{
BoundBox2D result;
result.left = left * f;
result.right = right * f;
result.bottom = bottom * f;
result.top = top * f;
return result;
}
BoundBox2D subset(const BoundBox2D &other) const
{
BoundBox2D subset;
subset.left = left + other.left * (right - left);
subset.right = left + other.right * (right - left);
subset.bottom = bottom + other.bottom * (top - bottom);
subset.top = bottom + other.top * (top - bottom);
return subset;
}
BoundBox2D make_relative_to(const BoundBox2D &other) const
{
BoundBox2D result;
result.left = ((left - other.left) / (other.right - other.left));
result.right = ((right - other.left) / (other.right - other.left));
result.bottom = ((bottom - other.bottom) / (other.top - other.bottom));
result.top = ((top - other.bottom) / (other.top - other.bottom));
return result;
}
BoundBox2D clamp(const float mn = 0.0f, const float mx = 1.0f)
{
BoundBox2D result;
result.left = ccl::clamp(left, mn, mx);
result.right = ccl::clamp(right, mn, mx);
result.bottom = ccl::clamp(bottom, mn, mx);
result.top = ccl::clamp(top, mn, mx);
return result;
}
BoundBox2D offset(const float2 offset) const
{
BoundBox2D result;
result.left = left + offset.x;
result.right = right + offset.x;
result.bottom = bottom + offset.y;
result.top = top + offset.y;
return result;
}
};
CCL_NAMESPACE_END

View File

@@ -0,0 +1,204 @@
/* SPDX-FileCopyrightText: 2025 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#pragma once
#include <functional>
#include <list>
#include <memory>
#include <mutex>
CCL_NAMESPACE_BEGIN
/* Cache limiter
*
* System to ensure a number of cached resources do not exceed a specified maximum number of
* items. Cached handles hold a reference to the resources, and the limiter will delete
* resources if the maximum is exceeded and there no current users of the resource. */
template<typename T> class CacheLimiter;
template<typename T> class CacheHandleGuard;
/* Cache Handle
*
* Holds a reference to cached resource, which may get deleted if the maximum number
* of items is exceeded. */
template<typename T> class CacheHandle {
public:
CacheHandleGuard<T> acquire(CacheLimiter<T> &limiter,
const std::function<std::unique_ptr<T>()> &creator)
{
{
std::scoped_lock lock(internal_mutex_);
if (!internal_) {
internal_ = std::make_shared<Internal>(limiter);
}
}
return internal_->acquire(creator);
}
private:
friend class CacheLimiter<T>;
friend class CacheHandleGuard<T>;
class Internal : public std::enable_shared_from_this<Internal> {
public:
explicit Internal(CacheLimiter<T> &limiter) : limiter_(limiter) {}
~Internal()
{
limiter_.unregister(this);
delete_resource();
}
CacheHandleGuard<T> acquire(std::function<std::unique_ptr<T>()> creator)
{
limiter_.register_and_add_guard(*this);
std::lock_guard<std::mutex> lock(resource_mutex_);
if (!resource_) {
resource_ = creator();
}
return CacheHandleGuard<T>(*this);
}
void delete_resource()
{
std::lock_guard<std::mutex> lock(resource_mutex_);
resource_.reset();
}
CacheLimiter<T> &limiter_;
std::unique_ptr<T> resource_;
std::mutex resource_mutex_;
int guard_count_ = 0;
bool in_lru_list_ = false;
typename std::list<typename CacheHandle<T>::Internal *>::iterator lru_iterator_;
};
/* Shared pointer for lifetime extension in cache eviction. */
std::mutex internal_mutex_;
std::shared_ptr<Internal> internal_;
};
/* Cache Handle Guard
*
* Ensures a cache resource is not deleted as long as it exists. This is meant to
* be used as a temporary object on the stack to keep the resource alive while using
* it. A CacheHandleGuard should not outlive the CacheHandle it was created from. */
template<typename T> class CacheHandleGuard {
public:
CacheHandleGuard() = default;
~CacheHandleGuard()
{
handle_.limiter_.remove_guard(handle_);
}
CacheHandleGuard(const CacheHandleGuard &) = delete;
CacheHandleGuard &operator=(const CacheHandleGuard &) = delete;
CacheHandleGuard(CacheHandleGuard &&other) = delete;
CacheHandleGuard &operator=(CacheHandleGuard &&other) = delete;
const std::unique_ptr<T> &get() const
{
return handle_.resource_;
}
private:
friend class CacheHandle<T>;
CacheHandleGuard(typename CacheHandle<T>::Internal &source) : handle_(source) {}
typename CacheHandle<T>::Internal &handle_;
};
/* Cache limiter
*
* Cache handles are registered with this global limiter. */
template<typename T> class CacheLimiter {
public:
explicit CacheLimiter(size_t max_items) : max_items_(max_items) {}
CacheLimiter(const CacheLimiter &) = delete;
CacheLimiter &operator=(const CacheLimiter &) = delete;
private:
friend class CacheHandle<T>;
friend class CacheHandleGuard<T>;
/* When handle is deleted. */
void unregister(typename CacheHandle<T>::Internal *handle)
{
std::lock_guard<std::mutex> lock(mutex_);
if (handle->in_lru_list_) {
lru_list_.erase(handle->lru_iterator_);
handle->in_lru_list_ = false;
}
}
/* When handle guard is acquired. */
void register_and_add_guard(typename CacheHandle<T>::Internal &handle)
{
std::unique_lock<std::mutex> lock(mutex_);
/* (Re)insert handle at the front of the LRU list. */
handle.guard_count_++;
if (handle.in_lru_list_) {
lru_list_.erase(handle.lru_iterator_);
}
lru_list_.push_front(&handle);
handle.lru_iterator_ = lru_list_.begin();
handle.in_lru_list_ = true;
/* Evict other handles if needed. */
while (lru_list_.size() > max_items_) {
typename CacheHandle<T>::Internal *handle_to_evict = nullptr;
/* From the back of the LRU list, find an item with no users. */
auto it = lru_list_.rbegin();
while (it != lru_list_.rend()) {
typename CacheHandle<T>::Internal *candidate = *it;
if (candidate->guard_count_ <= 0) {
handle_to_evict = candidate;
break;
}
++it;
}
if (!handle_to_evict) {
break;
}
lru_list_.erase(handle_to_evict->lru_iterator_);
handle_to_evict->in_lru_list_ = false;
/* Get a shared pointer to ensure this does not get deleted
* before we have unloaded the resource. */
auto handle_to_evict_ptr = handle_to_evict->shared_from_this();
lock.unlock();
/* Unload resource while unlocked, to avoid holding lock
* too long if deletion is expensive. */
handle_to_evict_ptr->delete_resource();
handle_to_evict_ptr.reset();
lock.lock();
}
}
/* When handle guard is deleted. */
void remove_guard(typename CacheHandle<T>::Internal &handle)
{
std::lock_guard<std::mutex> lock(mutex_);
handle.guard_count_--;
}
size_t max_items_;
std::mutex mutex_;
std::list<typename CacheHandle<T>::Internal *> lru_list_;
};
CCL_NAMESPACE_END

View File

@@ -0,0 +1,439 @@
/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#pragma once
#include "util/math.h"
#include "util/types.h"
CCL_NAMESPACE_BEGIN
ccl_device uchar float_to_byte(const float val)
{
return ((val <= 0.0f) ?
0 :
((val > (1.0f - 0.5f / 255.0f)) ? 255 : (uchar)((255.0f * val) + 0.5f))); // NOLINT
}
ccl_device float byte_to_float(const uchar val)
{
return val * (1.0f / 255.0f);
}
ccl_device uchar4 color_float_to_byte(const float3 c)
{
uchar r;
uchar g;
uchar b;
r = float_to_byte(c.x);
g = float_to_byte(c.y);
b = float_to_byte(c.z);
return make_uchar4(r, g, b, 0);
}
ccl_device uchar4 color_float4_to_uchar4(const float4 c)
{
uchar r;
uchar g;
uchar b;
uchar a;
r = float_to_byte(c.x);
g = float_to_byte(c.y);
b = float_to_byte(c.z);
a = float_to_byte(c.w);
return make_uchar4(r, g, b, a);
}
ccl_device_inline float3 color_byte_to_float(const uchar4 c)
{
return make_float3(c.x * (1.0f / 255.0f), c.y * (1.0f / 255.0f), c.z * (1.0f / 255.0f));
}
ccl_device_inline float4 color_uchar4_to_float4(const uchar4 c)
{
return make_float4(
c.x * (1.0f / 255.0f), c.y * (1.0f / 255.0f), c.z * (1.0f / 255.0f), c.w * (1.0f / 255.0f));
}
ccl_device float color_srgb_to_linear(const float c)
{
if (c < 0.04045f) {
return (c < 0.0f) ? 0.0f : c * (1.0f / 12.92f);
}
return powf((c + 0.055f) * (1.0f / 1.055f), 2.4f);
}
ccl_device float color_linear_to_srgb(const float c)
{
if (c < 0.0031308f) {
return (c < 0.0f) ? 0.0f : c * 12.92f;
}
return 1.055f * powf(c, 1.0f / 2.4f) - 0.055f;
}
ccl_device float3 rgb_to_hsv(const float3 rgb)
{
float cmax;
float cmin;
float h;
float s;
float v;
float cdelta;
float3 c;
cmax = fmaxf(rgb.x, fmaxf(rgb.y, rgb.z));
cmin = min(rgb.x, min(rgb.y, rgb.z));
cdelta = cmax - cmin;
v = cmax;
if (cmax != 0.0f) {
s = cdelta / cmax;
}
else {
s = 0.0f;
h = 0.0f;
}
if (s != 0.0f) {
const float3 cmax3 = make_float3(cmax, cmax, cmax);
c = (cmax3 - rgb) / cdelta;
if (rgb.x == cmax) {
h = c.z - c.y;
}
else if (rgb.y == cmax) {
h = 2.0f + c.x - c.z;
}
else {
h = 4.0f + c.y - c.x;
}
h /= 6.0f;
if (h < 0.0f) {
h += 1.0f;
}
}
else {
h = 0.0f;
}
return make_float3(h, s, v);
}
ccl_device float3 hsv_to_rgb(const float3 hsv)
{
float i;
float f;
float p;
float q;
float t;
float h;
float s;
float v;
float3 rgb;
h = hsv.x;
s = hsv.y;
v = hsv.z;
if (s != 0.0f) {
if (h == 1.0f) {
h = 0.0f;
}
h *= 6.0f;
i = floorf(h);
f = h - i;
rgb = make_float3(f, f, f);
p = v * (1.0f - s);
q = v * (1.0f - (s * f));
t = v * (1.0f - (s * (1.0f - f)));
if (i == 0.0f) {
rgb = make_float3(v, t, p);
}
else if (i == 1.0f) {
rgb = make_float3(q, v, p);
}
else if (i == 2.0f) {
rgb = make_float3(p, v, t);
}
else if (i == 3.0f) {
rgb = make_float3(p, q, v);
}
else if (i == 4.0f) {
rgb = make_float3(t, p, v);
}
else {
rgb = make_float3(v, p, q);
}
}
else {
rgb = make_float3(v, v, v);
}
return rgb;
}
ccl_device float3 rgb_to_hsl(const float3 rgb)
{
float cmax;
float cmin;
float h;
float s;
float l;
cmax = fmaxf(rgb.x, fmaxf(rgb.y, rgb.z));
cmin = min(rgb.x, min(rgb.y, rgb.z));
l = min(1.0f, (cmax + cmin) / 2.0f);
if (cmax == cmin) {
h = s = 0.0f; /* achromatic */
}
else {
const float cdelta = cmax - cmin;
s = l > 0.5f ? cdelta / (2.0f - cmax - cmin) : cdelta / (cmax + cmin);
if (cmax == rgb.x) {
h = (rgb.y - rgb.z) / cdelta + (rgb.y < rgb.z ? 6.0f : 0.0f);
}
else if (cmax == rgb.y) {
h = (rgb.z - rgb.x) / cdelta + 2.0f;
}
else {
h = (rgb.x - rgb.y) / cdelta + 4.0f;
}
}
h /= 6.0f;
return make_float3(h, s, l);
}
ccl_device float3 hsl_to_rgb(const float3 hsl)
{
float nr;
float ng;
float nb;
float chroma;
float h;
float s;
float l;
h = hsl.x;
s = hsl.y;
l = hsl.z;
nr = fabsf(h * 6.0f - 3.0f) - 1.0f;
ng = 2.0f - fabsf(h * 6.0f - 2.0f);
nb = 2.0f - fabsf(h * 6.0f - 4.0f);
nr = clamp(nr, 0.0f, 1.0f);
nb = clamp(nb, 0.0f, 1.0f);
ng = clamp(ng, 0.0f, 1.0f);
chroma = (1.0f - fabsf(2.0f * l - 1.0f)) * s;
return make_float3((nr - 0.5f) * chroma + l, (ng - 0.5f) * chroma + l, (nb - 0.5f) * chroma + l);
}
ccl_device float3 xyY_to_xyz(const float x, const float y, float Y)
{
float X;
float Z;
if (y != 0.0f) {
X = (x / y) * Y;
}
else {
X = 0.0f;
}
if (y != 0.0f && Y != 0.0f) {
Z = (1.0f - x - y) / y * Y;
}
else {
Z = 0.0f;
}
return make_float3(X, Y, Z);
}
#ifdef __KERNEL_SSE2__
/*
* Calculate initial guess for arg^exp based on float representation
* This method gives a constant bias,
* which can be easily compensated by multiplication with bias_coeff.
* Gives better results for exponents near 1 (e. g. 4/5).
* exp = exponent, encoded as uint32_t
* e2coeff = 2^(127/exponent - 127) * bias_coeff^(1/exponent), encoded as uint32_t
*/
template<unsigned exp, unsigned e2coeff> ccl_device_inline float4 fastpow_sse2(const float4 &arg)
{
float4 ret = arg * cast(make_int4(e2coeff));
ret = make_float4(cast(ret));
ret = ret * cast(make_int4(exp));
ret = cast(make_int4(ret));
return ret;
}
/* Improve x ^ 1.0f/5.0f solution with Newton-Raphson method */
ccl_device_inline float4 improve_5throot_solution_sse2(const float4 &old_result, const float4 &x)
{
const float4 approx2 = old_result * old_result;
const float4 approx4 = approx2 * approx2;
const float4 t = x / approx4;
const float4 summ = madd(make_float4(4.0f), old_result, t);
return summ * make_float4(1.0f / 5.0f);
}
/* Calculate powf(x, 2.4). Working domain: 1e-10 < x < 1e+10 */
ccl_device_inline float4 fastpow24_sse2(const float4 &arg)
{
/* `max`, `avg` and |avg| errors were calculated in GCC without FMA instructions.
* The final precision should be better than `powf` in GLIBC. */
/* Calculate x^4/5, coefficient 0.994 was constructed manually to minimize avg error */
/* 0x3F4CCCCD = 4/5 */
/* 0x4F55A7FB = 2^(127/(4/5) - 127) * 0.994^(1/(4/5)) */
float4 x = fastpow_sse2<0x3F4CCCCD, 0x4F55A7FB>(
arg); // error max = 0.17 avg = 0.0018 |avg| = 0.05
const float4 arg2 = arg * arg;
const float4 arg4 = arg2 * arg2;
/* error max = 0.018 avg = 0.0031 |avg| = 0.0031 */
x = improve_5throot_solution_sse2(x, arg4);
/* error max = 0.00021 avg = 1.6e-05 |avg| = 1.6e-05 */
x = improve_5throot_solution_sse2(x, arg4);
/* error max = 6.1e-07 avg = 5.2e-08 |avg| = 1.1e-07 */
x = improve_5throot_solution_sse2(x, arg4);
return x * (x * x);
}
ccl_device float4 color_srgb_to_linear_sse2(const float4 &c)
{
const int4 cmp = c < make_float4(0.04045f);
const float4 lt = max(c * make_float4(1.0f / 12.92f), make_float4(0.0f));
const float4 gtebase = (c + make_float4(0.055f)) * make_float4(1.0f / 1.055f); /* fma */
const float4 gte = fastpow24_sse2(gtebase);
return select(cmp, lt, gte);
}
#endif /* __KERNEL_SSE2__ */
ccl_device float3 color_srgb_to_linear_v3(const float3 c)
{
return make_float3(
color_srgb_to_linear(c.x), color_srgb_to_linear(c.y), color_srgb_to_linear(c.z));
}
ccl_device float3 color_linear_to_srgb_v3(const float3 c)
{
return make_float3(
color_linear_to_srgb(c.x), color_linear_to_srgb(c.y), color_linear_to_srgb(c.z));
}
ccl_device float4 color_linear_to_srgb_v4(const float4 c)
{
return make_float4(
color_linear_to_srgb(c.x), color_linear_to_srgb(c.y), color_linear_to_srgb(c.z), c.w);
}
ccl_device float4 color_srgb_to_linear_v4(const float4 c)
{
#ifdef __KERNEL_SSE2__
float4 r = c;
r = color_srgb_to_linear_sse2(r);
r.w = c.w;
return r;
#else
return make_float4(
color_srgb_to_linear(c.x), color_srgb_to_linear(c.y), color_srgb_to_linear(c.z), c.w);
#endif
}
ccl_device float3 color_highlight_compress(float3 color, ccl_private float3 *variance)
{
color += one_float3();
if (variance) {
*variance *= sqr(one_float3() / color);
}
return log(color);
}
ccl_device float3 color_highlight_uncompress(const float3 color)
{
return exp(color) - one_float3();
}
/* Color division */
ccl_device_inline Spectrum safe_invert_color(Spectrum a)
{
FOREACH_SPECTRUM_CHANNEL (i) {
GET_SPECTRUM_CHANNEL(a, i) = (GET_SPECTRUM_CHANNEL(a, i) != 0.0f) ?
1.0f / GET_SPECTRUM_CHANNEL(a, i) :
0.0f;
}
return a;
}
/* Returns `a/b`, and replace the channel value with `fallback` if `b == 0`. */
ccl_device_inline Spectrum safe_divide_color(Spectrum a, Spectrum b, const float fallback = 0.0f)
{
FOREACH_SPECTRUM_CHANNEL (i) {
GET_SPECTRUM_CHANNEL(a, i) = (GET_SPECTRUM_CHANNEL(b, i) != 0.0f) ?
GET_SPECTRUM_CHANNEL(a, i) / GET_SPECTRUM_CHANNEL(b, i) :
fallback;
}
return a;
}
ccl_device_inline float3 safe_divide_even_color(const float3 a, const float3 b)
{
float x;
float y;
float z;
x = (b.x != 0.0f) ? a.x / b.x : 0.0f;
y = (b.y != 0.0f) ? a.y / b.y : 0.0f;
z = (b.z != 0.0f) ? a.z / b.z : 0.0f;
/* try to get gray even if b is zero */
if (b.x == 0.0f) {
if (b.y == 0.0f) {
x = z;
y = z;
}
else if (b.z == 0.0f) {
x = y;
z = y;
}
else {
x = 0.5f * (y + z);
}
}
else if (b.y == 0.0f) {
if (b.z == 0.0f) {
y = x;
z = x;
}
else {
y = 0.5f * (x + z);
}
}
else if (b.z == 0.0f) {
z = 0.5f * (x + y);
}
return make_float3(x, y, z);
}
CCL_NAMESPACE_END

View File

@@ -0,0 +1,876 @@
/* SPDX-FileCopyrightText: 2011-2026 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#include "util/colorspace.h"
#include "util/color.h"
#include "util/image.h"
#include "util/log.h"
#include "util/map.h"
#include "util/math.h"
#include "util/string.h"
#include "util/thread.h"
#include "util/transform.h"
#include "util/vector.h"
#ifdef WITH_OCIO
# include <OpenColorIO/OpenColorIO.h>
namespace OCIO = OCIO_NAMESPACE;
#endif
CCL_NAMESPACE_BEGIN
/* Builtin colorspaces. */
ustring u_colorspace_auto;
ustring u_colorspace_data("data");
ustring u_colorspace_scene_linear("scene_linear");
ustring u_colorspace_scene_linear_srgb("scene_linear_srgb");
ustring u_colorspace_srgb("__builtin_srgb");
/* Cached data. */
static thread_mutex cache_xyz_to_scene_linear_mutex;
static string cache_xyz_to_scene_linear_hash;
#ifdef WITH_OCIO
static thread_mutex cache_processors_mutex;
static unordered_map<ustring, OCIO::ConstProcessorRcPtr> cache_processors;
static thread_mutex cache_colorspaces_mutex;
static unordered_map<ustring, ustring> cached_colorspaces;
static thread_mutex cache_scene_linear_interop_id_mutex;
static bool cache_scene_linear_interop_id_done = false;
static const char *cache_scene_linear_interop_id = "";
static const char *cache_scene_linear_srgb_interop_id = "";
static void check_invalidate_caches()
{
static thread_mutex cache_scene_linear_mutex;
static string cache_scene_linear_name;
/* Invalidate cached processors and colorspace, in case Blender changed it.
* Note this should not happen during rendering, all render should be stopped
* before it is changed. */
const thread_scoped_lock cache_scene_linear_lock(cache_scene_linear_mutex);
OCIO::ConstConfigRcPtr config = nullptr;
try {
config = OCIO::GetCurrentConfig();
}
catch (const OCIO::Exception &exception) {
LOG_ERROR << "OCIO config error: " << exception.what();
return;
}
const OCIO::ConstColorSpaceRcPtr scene_linear_colorspace = config->getColorSpace("scene_linear");
if (scene_linear_colorspace && cache_scene_linear_name != scene_linear_colorspace->getName()) {
cache_scene_linear_name = scene_linear_colorspace->getName();
{
const thread_scoped_lock cache_processors_lock(cache_processors_mutex);
cache_processors.clear();
}
{
const thread_scoped_lock cache_lock(cache_colorspaces_mutex);
cached_colorspaces.clear();
}
{
const thread_scoped_lock cache_lock(cache_scene_linear_interop_id_mutex);
cache_scene_linear_interop_id_done = false;
cache_scene_linear_interop_id = "";
cache_scene_linear_srgb_interop_id = "";
}
{
const thread_scoped_lock cache_lock(cache_xyz_to_scene_linear_mutex);
cache_xyz_to_scene_linear_hash.clear();
}
}
}
#endif
ColorSpaceProcessor *ColorSpaceManager::get_processor(ustring colorspace)
{
#ifdef WITH_OCIO
/* Only use this for OpenColorIO color spaces, not the builtin ones. */
assert(colorspace != u_colorspace_scene_linear_srgb && colorspace != u_colorspace_auto);
if (colorspace == u_colorspace_data || colorspace == u_colorspace_scene_linear) {
return nullptr;
}
OCIO::ConstConfigRcPtr config = nullptr;
try {
config = OCIO::GetCurrentConfig();
}
catch (const OCIO::Exception &exception) {
LOG_WARNING << "OCIO config error: " << exception.what();
return nullptr;
}
if (!config) {
return nullptr;
}
/* Outside the mutex lock. This will also invalidate the cache if
* scene linear colorspace changed. */
const char *scene_linear_interop_id = get_scene_linear_interop_id();
/* Cache processor until free_memory(), memory overhead is expected to be
* small and the processor is likely to be reused. */
const thread_scoped_lock cache_processors_lock(cache_processors_mutex);
if (!cache_processors.contains(colorspace)) {
try {
if (colorspace == u_colorspace_srgb) {
/* Linear Rec.709 to sRGB is handled separately in to_scene_linear, here
* we only need the matrix transform from scene_linear to Linear Rec.709. */
if (strcmp(scene_linear_interop_id, "lin_rec709_scene") == 0) {
cache_processors[colorspace] = nullptr;
}
else {
const Transform rgb_to_rec709 = transform_inverse(get_xyz_to_scene_linear_rgb()) *
get_xyz_to_rec709();
const double data[16] = {rgb_to_rec709.x.x,
rgb_to_rec709.x.y,
rgb_to_rec709.x.z,
rgb_to_rec709.x.w,
rgb_to_rec709.y.x,
rgb_to_rec709.y.y,
rgb_to_rec709.y.z,
rgb_to_rec709.y.w,
rgb_to_rec709.z.x,
rgb_to_rec709.z.y,
rgb_to_rec709.z.z,
rgb_to_rec709.z.w,
0.0f,
0.0f,
0.0f,
1.0f};
OCIO::MatrixTransformRcPtr tfm = OCIO::MatrixTransform::Create();
tfm->setMatrix(data);
cache_processors[colorspace] = config->getProcessor(tfm, OCIO::TRANSFORM_DIR_FORWARD);
}
}
else {
cache_processors[colorspace] = config->getProcessor(colorspace.c_str(), "scene_linear");
}
}
catch (const OCIO::Exception &exception) {
cache_processors[colorspace] = OCIO::ConstProcessorRcPtr();
LOG_WARNING << "Colorspace " << colorspace.c_str()
<< " can't be converted to scene_linear: " << exception.what();
}
}
const OCIO::Processor *processor = cache_processors[colorspace].get();
return (ColorSpaceProcessor *)processor;
#else
/* No OpenColorIO. */
(void)colorspace;
return nullptr;
#endif
}
bool ColorSpaceManager::colorspace_is_data(ustring colorspace)
{
if (colorspace == u_colorspace_data) {
return true;
}
if (colorspace == u_colorspace_auto || colorspace == u_colorspace_scene_linear ||
colorspace == u_colorspace_scene_linear_srgb || colorspace == u_colorspace_srgb)
{
return false;
}
#ifdef WITH_OCIO
OCIO::ConstConfigRcPtr config = nullptr;
try {
config = OCIO::GetCurrentConfig();
}
catch (const OCIO::Exception &exception) {
LOG_WARNING << "OCIO config error: " << exception.what();
return false;
}
if (!config) {
return false;
}
try {
const OCIO::ConstColorSpaceRcPtr space = config->getColorSpace(colorspace.c_str());
return space && space->isData();
}
catch (const OCIO::Exception &) {
return false;
}
#else
return false;
#endif
}
const char *ColorSpaceManager::colorspace_interop_id(ustring colorspace)
{
if (colorspace == u_colorspace_auto) {
return nullptr;
}
if (colorspace == u_colorspace_data) {
return "data";
}
if (colorspace == u_colorspace_srgb) {
return "srgb_rec709_scene";
}
if (colorspace == u_colorspace_scene_linear) {
const char *interop_id = get_scene_linear_interop_id(false);
if (strcmp(interop_id, "unknown") != 0) {
return interop_id;
}
}
else if (colorspace == u_colorspace_scene_linear_srgb) {
const char *interop_id = get_scene_linear_interop_id(true);
if (strcmp(interop_id, "unknown") != 0) {
return interop_id;
}
}
#ifdef WITH_OCIO
OCIO::ConstConfigRcPtr config = nullptr;
try {
config = OCIO::GetCurrentConfig();
if (!config) {
return nullptr;
}
}
catch (const OCIO::Exception &) {
return nullptr;
}
try {
const OCIO::ConstColorSpaceRcPtr space = config->getColorSpace(colorspace.c_str());
if (space) {
if (space->isData()) {
return "data";
}
/* From https://github.com/AcademySoftwareFoundation/ColorInterop. */
/* TODO: Use native interop ID support in OpenColorIO 2.5. */
const char *interop_ids[] = {"lin_rec709_scene",
"lin_p3d65_scene",
"lin_rec2020_scene",
"lin_adobergb_scene",
"lin_ap1_scene",
"lin_ap0_scene",
"lin_ciexyzd65_scene",
"srgb_rec709_scene",
"g22_rec709_scene",
"g18_rec709_scene",
"srgb_ap1_scene",
"g22_ap1_scene",
"srgb_p3d65_scene",
"g22_adobergb_scene"};
for (const char *interop_id : interop_ids) {
if (space->hasAlias(interop_id)) {
return interop_id;
}
}
}
}
catch (const OCIO::Exception &) {
return nullptr;
}
#endif
return nullptr;
}
ustring ColorSpaceManager::detect_known_colorspace(ustring colorspace,
const char *file_colorspace,
const char * /*file_format*/,
bool is_float)
{
#ifdef WITH_OCIO
OCIO::ConstConfigRcPtr config = nullptr;
try {
config = OCIO::GetCurrentConfig();
}
catch (const OCIO::Exception &exception) {
LOG_WARNING << "OCIO config error: " << exception.what();
}
/* Rely on OpenImageIO and OpenColorIO guessed color spaces when available. This relies on
* recent OpenImageIO versions supporting interop IDs. */
if (config && colorspace == u_colorspace_auto) {
if (file_colorspace[0] && config->getColorSpace(file_colorspace)) {
colorspace = file_colorspace;
}
else {
const char *role_colorspace = (is_float) ? config->getRoleColorSpace("default_float") :
config->getRoleColorSpace("default_byte");
role_colorspace = (role_colorspace) ? role_colorspace : config->getRoleColorSpace("default");
if (role_colorspace) {
colorspace = role_colorspace;
}
}
}
#endif
/* Fall back to simple guess if we don't have OpenColorIO. */
if (colorspace == u_colorspace_auto) {
colorspace = (is_float && !(strcmp(file_colorspace, "srgb_rec709_scene") == 0 ||
strcmp(file_colorspace, "srgb_rec709_display") == 0)) ?
u_colorspace_scene_linear :
u_colorspace_srgb;
}
/* Builtin colorspaces. */
if (colorspace == u_colorspace_srgb || colorspace == u_colorspace_scene_linear ||
colorspace == u_colorspace_scene_linear_srgb || colorspace == u_colorspace_data)
{
return colorspace;
}
if (colorspace_is_data(colorspace)) {
return u_colorspace_data;
}
/* Use OpenColorIO. */
#ifdef WITH_OCIO
check_invalidate_caches();
{
const thread_scoped_lock cache_lock(cache_colorspaces_mutex);
/* Cached lookup. */
if (cached_colorspaces.contains(colorspace)) {
return cached_colorspaces[colorspace];
}
}
/* Detect if it matches a simple builtin colorspace. */
bool is_scene_linear;
bool is_scene_linear_srgb;
is_builtin_colorspace(colorspace, is_scene_linear, is_scene_linear_srgb);
const thread_scoped_lock cache_lock(cache_colorspaces_mutex);
if (is_scene_linear) {
LOG_DEBUG << "Colorspace " << colorspace.string() << " is no-op";
cached_colorspaces[colorspace] = u_colorspace_scene_linear;
return u_colorspace_scene_linear;
}
if (is_scene_linear_srgb) {
LOG_DEBUG << "Colorspace " << colorspace.string() << " is scene linear sRGB";
cached_colorspaces[colorspace] = u_colorspace_scene_linear_srgb;
return u_colorspace_scene_linear_srgb;
}
/* Verify if we can convert from the requested color space. */
if (!get_processor(colorspace)) {
if (!config || !config->getColorSpace(colorspace.c_str())) {
LOG_WARNING << "Colorspace " << colorspace.c_str()
<< " not found, using scene linear instead";
}
else {
LOG_WARNING << "Colorspace " << colorspace.c_str()
<< " can't be converted to scene_linear, using scene linear instead";
}
cached_colorspaces[colorspace] = u_colorspace_scene_linear;
return u_colorspace_scene_linear;
}
/* Convert to/from colorspace with OpenColorIO. */
LOG_DEBUG << "Colorspace " << colorspace.string() << " handled through OpenColorIO";
cached_colorspaces[colorspace] = colorspace;
return colorspace;
#else
LOG_WARNING << "Colorspace " << colorspace.c_str()
<< " not available, built without OpenColorIO";
return u_colorspace_scene_linear;
#endif
}
void ColorSpaceManager::is_builtin_colorspace(ustring colorspace,
bool &is_scene_linear,
bool &is_scene_linear_srgb)
{
#ifdef WITH_OCIO
const OCIO::Processor *processor = (const OCIO::Processor *)get_processor(colorspace);
if (!processor) {
is_scene_linear = false;
is_scene_linear_srgb = false;
return;
}
const OCIO::ConstCPUProcessorRcPtr device_processor = processor->getDefaultCPUProcessor();
is_scene_linear = true;
is_scene_linear_srgb = true;
for (int i = 0; i < 256; i++) {
const float v = i / 255.0f;
float cR[3] = {v, 0, 0};
float cG[3] = {0, v, 0};
float cB[3] = {0, 0, v};
float cW[3] = {v, v, v};
device_processor->applyRGB(cR);
device_processor->applyRGB(cG);
device_processor->applyRGB(cB);
device_processor->applyRGB(cW);
/* Make sure that there is no channel crosstalk. */
if (fabsf(cR[1]) > 1e-5f || fabsf(cR[2]) > 1e-5f || fabsf(cG[0]) > 1e-5f ||
fabsf(cG[2]) > 1e-5f || fabsf(cB[0]) > 1e-5f || fabsf(cB[1]) > 1e-5f)
{
is_scene_linear = false;
is_scene_linear_srgb = false;
break;
}
/* Make sure that the three primaries combine linearly. */
if (!compare_floats(cR[0], cW[0], 1e-6f, 64) || !compare_floats(cG[1], cW[1], 1e-6f, 64) ||
!compare_floats(cB[2], cW[2], 1e-6f, 64))
{
is_scene_linear = false;
is_scene_linear_srgb = false;
break;
}
/* Make sure that the three channels behave identically. */
if (!compare_floats(cW[0], cW[1], 1e-6f, 64) || !compare_floats(cW[1], cW[2], 1e-6f, 64)) {
is_scene_linear = false;
is_scene_linear_srgb = false;
break;
}
const float out_v = average(make_float3(cW[0], cW[1], cW[2]));
if (!compare_floats(v, out_v, 1e-6f, 64)) {
is_scene_linear = false;
}
if (!compare_floats(color_srgb_to_linear(v), out_v, 1e-4f, 64)) {
is_scene_linear_srgb = false;
}
}
#else
(void)colorspace;
is_scene_linear = false;
is_scene_linear_srgb = false;
#endif
}
#ifdef WITH_OCIO
template<typename T> inline float4 cast_to_float4(T *data)
{
return make_float4(util_image_cast_to_float(data[0]),
util_image_cast_to_float(data[1]),
util_image_cast_to_float(data[2]),
util_image_cast_to_float(data[3]));
}
template<typename T> inline void cast_from_float4(T *data, const float4 value)
{
data[0] = util_image_cast_from_float<T>(value.x);
data[1] = util_image_cast_from_float<T>(value.y);
data[2] = util_image_cast_from_float<T>(value.z);
data[3] = util_image_cast_from_float<T>(value.w);
}
/* Slower versions for other all data types, which needs to convert to float and back. */
template<typename T, bool compress_as_srgb = false>
inline void processor_apply_pixels_rgba(const OCIO::Processor *processor,
T *pixels,
const int64_t width,
const int64_t height,
const int64_t y_stride,
const bool ignore_alpha)
{
/* TODO: implement faster version for when we know the conversion
* is a simple matrix transform between linear spaces. In that case
* un-premultiply is not needed. */
const OCIO::ConstCPUProcessorRcPtr device_processor = (processor) ?
processor->getDefaultCPUProcessor() :
nullptr;
/* Process large images in chunks to keep temporary memory requirement down. */
const int64_t chunk_rows = divide_up(std::min((int64_t)(16 * 1024 * 1024), width * height),
width);
vector<float4> float_pixels(chunk_rows * width);
for (int64_t row = 0; row < height; row += chunk_rows) {
const int64_t num_rows = std::min(chunk_rows, height - row);
for (int64_t j = 0; j < num_rows; j++) {
T *pixel = pixels + (row + j) * y_stride;
float4 *float_pixel = float_pixels.data() + j * width;
for (int64_t i = 0; i < width; i++, pixel += 4, float_pixel++) {
float4 value = cast_to_float4(pixel);
if (!ignore_alpha && !(value.w <= 0.0f || value.w == 1.0f)) {
const float inv_alpha = 1.0f / value.w;
value.x *= inv_alpha;
value.y *= inv_alpha;
value.z *= inv_alpha;
}
*float_pixel = value;
}
}
if (processor) {
const OCIO::PackedImageDesc desc((float *)float_pixels.data(), num_rows * width, 1, 4);
device_processor->apply(desc);
}
for (int64_t j = 0; j < num_rows; j++) {
T *pixel = pixels + (row + j) * y_stride;
float4 *float_pixel = float_pixels.data() + j * width;
for (int64_t i = 0; i < width; i++, pixel += 4, float_pixel++) {
float4 value = *float_pixel;
if (compress_as_srgb) {
value = color_linear_to_srgb_v4(value);
}
if (!ignore_alpha && !(value.w <= 0.0f || value.w == 1.0f)) {
value.x *= value.w;
value.y *= value.w;
value.z *= value.w;
}
cast_from_float4(pixel, value);
}
}
}
}
template<typename T, bool compress_as_srgb = false>
inline void processor_apply_pixels_grayscale(const OCIO::Processor *processor,
T *pixels,
const int64_t width,
const int64_t height,
const int64_t y_stride)
{
const OCIO::ConstCPUProcessorRcPtr device_processor = (processor) ?
processor->getDefaultCPUProcessor() :
nullptr;
/* Process large images in chunks to keep temporary memory requirement down. */
const int64_t chunk_rows = divide_up(std::min((int64_t)(16 * 1024 * 1024), width * height),
width);
vector<float> float_pixels(chunk_rows * width * 3);
for (int64_t row = 0; row < height; row += chunk_rows) {
const int64_t num_rows = std::min(chunk_rows, height - row);
/* Convert to 3 channels, since that's the minimum required by OpenColorIO. */
for (int64_t j = 0; j < num_rows; j++) {
T *pixel = pixels + (row + j) * y_stride;
float *float_pixel = float_pixels.data() + j * width * 3;
for (int64_t i = 0; i < width; i++, pixel++, float_pixel += 3) {
const float f = util_image_cast_to_float<T>(*pixel);
float_pixel[0] = f;
float_pixel[1] = f;
float_pixel[2] = f;
}
}
if (processor) {
const OCIO::PackedImageDesc desc((float *)float_pixels.data(), num_rows * width, 1, 3);
device_processor->apply(desc);
}
for (int64_t j = 0; j < num_rows; j++) {
T *pixel = pixels + (row + j) * y_stride;
float *float_pixel = float_pixels.data() + j * width * 3;
for (int64_t i = 0; i < width; i++, pixel++, float_pixel += 3) {
float f = average(make_float3(float_pixel[0], float_pixel[1], float_pixel[2]));
if (compress_as_srgb) {
f = color_linear_to_srgb(f);
}
*pixel = util_image_cast_from_float<T>(f);
}
}
}
}
#endif
template<typename T>
void ColorSpaceManager::to_scene_linear(ustring colorspace,
T *pixels,
const int64_t width,
const int64_t height,
const int64_t y_stride,
bool is_rgba,
bool compress_as_srgb,
bool ignore_alpha)
{
#ifdef WITH_OCIO
const OCIO::Processor *processor = (const OCIO::Processor *)get_processor(colorspace);
/* The processor for sRGB does not include the Linear Rec.709 to sRGB transform, that
* is handled by compress_as_srgb for better performance when scene_linear is Rec.709 */
if (colorspace == u_colorspace_srgb) {
assert(!compress_as_srgb);
compress_as_srgb = true;
}
if (is_rgba) {
if (compress_as_srgb) {
/* Compress output as sRGB. */
processor_apply_pixels_rgba<T, true>(
processor, pixels, width, height, y_stride, ignore_alpha);
}
else {
/* Write output as scene linear directly. */
processor_apply_pixels_rgba<T>(processor, pixels, width, height, y_stride, ignore_alpha);
}
}
else {
if (compress_as_srgb) {
/* Compress output as sRGB. */
processor_apply_pixels_grayscale<T, true>(processor, pixels, width, height, y_stride);
}
else {
/* Write output as scene linear directly. */
processor_apply_pixels_grayscale<T>(processor, pixels, width, height, y_stride);
}
}
#else
(void)colorspace;
(void)pixels;
(void)width;
(void)height;
(void)y_stride;
(void)is_rgba;
(void)compress_as_srgb;
(void)ignore_alpha;
#endif
}
void ColorSpaceManager::free_memory()
{
#ifdef WITH_OCIO
map_free_memory(cached_colorspaces);
map_free_memory(cache_processors);
#endif
}
void ColorSpaceManager::init_fallback_config()
{
#ifdef WITH_OCIO
OCIO::SetCurrentConfig(OCIO::Config::CreateRaw());
#endif
}
#ifdef WITH_OCIO
static bool to_scene_linear_transform(OCIO::ConstConfigRcPtr &config,
const char *colorspace,
Transform &to_scene_linear)
{
OCIO::ConstProcessorRcPtr processor;
try {
processor = config->getProcessor("scene_linear", colorspace);
}
catch (OCIO::Exception &) {
return false;
}
if (!processor) {
return false;
}
const OCIO::ConstCPUProcessorRcPtr device_processor = processor->getDefaultCPUProcessor();
if (!device_processor) {
return false;
}
to_scene_linear = transform_identity();
device_processor->applyRGB(&to_scene_linear.x.x);
device_processor->applyRGB(&to_scene_linear.y.x);
device_processor->applyRGB(&to_scene_linear.z.x);
to_scene_linear = transform_transposed_inverse(to_scene_linear);
return true;
}
#endif
Transform ColorSpaceManager::get_xyz_to_rec709()
{
/* Default to ITU-BT.709 in case no appropriate transform found.
* Note XYZ here is defined as having a D65 white point. */
return make_transform(3.2404542f,
-1.5371385f,
-0.4985314f,
0.0f,
-0.9692660f,
1.8760108f,
0.0415560f,
0.0f,
0.0556434f,
-0.2040259f,
1.0572252f,
0.0f);
}
Transform ColorSpaceManager::get_xyz_to_rec2020()
{
return make_transform(1.7166512f,
-0.3556708f,
-0.2533663f,
0.0f,
-0.6666844,
1.6164812f,
0.0157685f,
0.0f,
0.0176399f,
-0.0427706f,
0.9421031f,
0.0f);
}
Transform ColorSpaceManager::get_xyz_to_acescg()
{
return transform_inverse(make_transform(0.652238f,
0.128237f,
0.169983f,
0.0f,
0.267672f,
0.674340f,
0.057988f,
0.0f,
-0.005382f,
0.001369f,
1.093071f,
0.0f));
}
Transform ColorSpaceManager::get_xyz_to_scene_linear_rgb()
{
Transform xyz_to_rgb = get_xyz_to_rec709();
#ifdef WITH_OCIO
/* Get from OpenColorO config if it has the required roles. */
OCIO::ConstConfigRcPtr config = nullptr;
try {
config = OCIO::GetCurrentConfig();
}
catch (OCIO::Exception &exception) {
LOG_WARNING << "OCIO config error: " << exception.what();
return xyz_to_rgb;
}
if (!(config && config->hasRole("scene_linear"))) {
return xyz_to_rgb;
}
if (config->hasRole("aces_interchange")) {
/* Standard OpenColorIO role, defined as ACES AP0 (ACES2065-1). */
Transform aces_to_rgb;
if (!to_scene_linear_transform(config, "aces_interchange", aces_to_rgb)) {
return xyz_to_rgb;
}
/* This is the OpenColorIO builtin transform:
* UTILITY - ACES-AP0_to_CIE-XYZ-D65_BFD. */
const Transform ACES_AP0_to_xyz_D65 = make_transform(0.938280f,
-0.004451f,
0.016628f,
0.000000f,
0.337369f,
0.729522f,
-0.066890f,
0.000000f,
0.001174f,
-0.003711f,
1.091595f,
0.000000f);
const Transform xyz_to_aces = transform_inverse(ACES_AP0_to_xyz_D65);
xyz_to_rgb = aces_to_rgb * xyz_to_aces;
return xyz_to_rgb;
}
if (config->hasRole("XYZ")) {
/* Custom role used before the standard existed. */
if (to_scene_linear_transform(config, "XYZ", xyz_to_rgb)) {
return xyz_to_rgb;
}
}
/* No reference role found to determine XYZ. */
#endif
return xyz_to_rgb;
}
std::string ColorSpaceManager::get_xyz_to_scene_linear_rgb_string()
{
#ifdef WITH_OCIO
/* Clear the cached hash if the scene linear colorspace changed. */
check_invalidate_caches();
#endif
/* NOTE: Be careful not to change existing hashes if at all possible, as this
* will cause all texture files to be regenerated with significantly increased
* disk usage. */
const thread_scoped_lock cache_lock(cache_xyz_to_scene_linear_mutex);
if (cache_xyz_to_scene_linear_hash.empty()) {
/* TODO: Verify this results in the same hash across platforms. */
const Transform xyz_to_rgb = get_xyz_to_scene_linear_rgb();
cache_xyz_to_scene_linear_hash = string_printf(
"%.4g_%.4g_%.4g_%.4g_%.4g_%.4g_%.4g_%.4g_%.4g_%.4g_%.4g_%.4g",
xyz_to_rgb.x.x,
xyz_to_rgb.x.y,
xyz_to_rgb.x.z,
xyz_to_rgb.x.w,
xyz_to_rgb.y.x,
xyz_to_rgb.y.y,
xyz_to_rgb.y.z,
xyz_to_rgb.y.w,
xyz_to_rgb.z.x,
xyz_to_rgb.z.y,
xyz_to_rgb.z.z,
xyz_to_rgb.z.w);
}
return cache_xyz_to_scene_linear_hash;
}
const char *ColorSpaceManager::get_scene_linear_interop_id(const bool srgb_encoded)
{
#ifdef WITH_OCIO
check_invalidate_caches();
const thread_scoped_lock cache_lock(cache_scene_linear_interop_id_mutex);
if (!cache_scene_linear_interop_id_done) {
const Transform xyz_to_rgb = get_xyz_to_scene_linear_rgb();
if (transform_equal_threshold(xyz_to_rgb, get_xyz_to_rec709(), 0.0001f)) {
cache_scene_linear_interop_id = "lin_rec709_scene";
cache_scene_linear_srgb_interop_id = "srgb_rec709_scene";
}
else if (transform_equal_threshold(xyz_to_rgb, get_xyz_to_rec2020(), 0.0001f)) {
cache_scene_linear_interop_id = "lin_rec2020_scene";
cache_scene_linear_srgb_interop_id = "srgb_rec2020_scene";
}
else if (transform_equal_threshold(xyz_to_rgb, get_xyz_to_acescg(), 0.0001f)) {
cache_scene_linear_interop_id = "lin_ap1_scene";
cache_scene_linear_srgb_interop_id = "srgb_ap1_scene";
}
else {
cache_scene_linear_interop_id = "unknown";
cache_scene_linear_srgb_interop_id = "unknown";
}
cache_scene_linear_interop_id_done = true;
}
return (srgb_encoded) ? cache_scene_linear_srgb_interop_id : cache_scene_linear_interop_id;
#else
(void)srgb_encoded;
return "lin_rec709_scene";
#endif
}
/* Template instantiations so we don't have to inline functions. */
template void ColorSpaceManager::to_scene_linear(
ustring, uchar *, int64_t, int64_t, int64_t, bool, bool, bool);
template void ColorSpaceManager::to_scene_linear(
ustring, ushort *, int64_t, int64_t, int64_t, bool, bool, bool);
template void ColorSpaceManager::to_scene_linear(
ustring, half *, int64_t, int64_t, int64_t, bool, bool, bool);
template void ColorSpaceManager::to_scene_linear(
ustring, float *, int64_t, int64_t, int64_t, bool, bool, bool);
CCL_NAMESPACE_END

View File

@@ -0,0 +1,79 @@
/* SPDX-FileCopyrightText: 2011-2026 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#pragma once
#include "util/param.h"
#include "util/string.h"
CCL_NAMESPACE_BEGIN
/* Automatically determine colorspace. */
extern ustring u_colorspace_auto;
/* Non-color data. */
extern ustring u_colorspace_data;
/* Scene linear colorspace used for rendering. */
extern ustring u_colorspace_scene_linear;
/* Scene linear + sRGB transfer function . */
extern ustring u_colorspace_scene_linear_srgb;
/* sRGB. */
extern ustring u_colorspace_srgb;
class ColorSpaceProcessor;
struct Transform;
class ColorSpaceManager {
public:
/* Convert used specified colorspace to a colorspace that we are able to
* convert to and from. If the colorspace is u_colorspace_auto, we auto
* detect a colospace. */
static ustring detect_known_colorspace(ustring colorspace,
const char *file_colorspace,
const char *file_format,
bool is_float);
/* Test if colorspace is for non-color data. */
static bool colorspace_is_data(ustring colorspace);
/* Return color interop forum interop ID. */
static const char *colorspace_interop_id(ustring colorspace);
/* Convert pixels in the specified colorspace to scene linear color for
* rendering. Must be a colorspace returned from detect_known_colorspace. */
template<typename T>
static void to_scene_linear(ustring colorspace,
T *pixels,
const int64_t width,
const int64_t height,
const int64_t y_stride,
bool is_rgba,
bool compress_as_srgb,
bool ignore_alpha);
/* Clear memory when the application exits. Invalidates all processors. */
static void free_memory();
/* Create a fallback color space configuration.
*
* This may be useful to allow regression test to create a configuration which is considered
* valid without knowing the actual configuration used by the final application. */
static void init_fallback_config();
/* Compute matrix to convert from XYZ to scene linear RGB, based on the config. */
static Transform get_xyz_to_scene_linear_rgb();
static Transform get_xyz_to_rec709();
static Transform get_xyz_to_rec2020();
static Transform get_xyz_to_acescg();
/* Compute unique string for texture cache hashing and metadata. */
static string get_xyz_to_scene_linear_rgb_string();
/* Determine if scene linear is a common known space. */
static const char *get_scene_linear_interop_id(const bool srgb_encoded = false);
private:
static ColorSpaceProcessor *get_processor(ustring colorspace);
static void is_builtin_colorspace(ustring colorspace,
bool &is_scene_linear,
bool &is_scene_linear_srgb);
};
CCL_NAMESPACE_END

View File

@@ -0,0 +1,19 @@
/* SPDX-FileCopyrightText: 2011-2025 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#pragma once
/* TBB includes <windows.h>, do it ourselves first so we are sure
* WIN32_LEAN_AND_MEAN and similar are defined beforehand. */
#ifdef _WIN32
# include "util/windows.h"
#endif
#include <tbb/concurrent_set.h>
CCL_NAMESPACE_BEGIN
using tbb::concurrent_set;
CCL_NAMESPACE_END

View File

@@ -0,0 +1,19 @@
/* SPDX-FileCopyrightText: 2011-2025 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#pragma once
/* TBB includes <windows.h>, do it ourselves first so we are sure
* WIN32_LEAN_AND_MEAN and similar are defined beforehand. */
#ifdef _WIN32
# include "util/windows.h"
#endif
#include <tbb/concurrent_vector.h>
CCL_NAMESPACE_BEGIN
using tbb::concurrent_vector;
CCL_NAMESPACE_END

View File

@@ -0,0 +1,123 @@
/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#include "util/debug.h"
#include <cstdlib>
#include "util/log.h"
CCL_NAMESPACE_BEGIN
DebugFlags::CPU::CPU()
{
reset();
}
void DebugFlags::CPU::reset()
{
#define STRINGIFY(x) #x
#define CHECK_CPU_FLAGS(flag, env) \
do { \
flag = (getenv(env) == nullptr); \
if (!flag) { \
LOG_INFO << "Disabling " << STRINGIFY(flag) << " instruction set."; \
} \
} while (0)
CHECK_CPU_FLAGS(avx2, "CYCLES_CPU_NO_AVX2");
#undef STRINGIFY
#undef CHECK_CPU_FLAGS
bvh_layout = BVH_LAYOUT_AUTO;
}
DebugFlags::CUDA::CUDA()
{
reset();
}
DebugFlags::HIP::HIP()
{
reset();
}
DebugFlags::Metal::Metal()
{
reset();
}
void DebugFlags::CUDA::reset()
{
if (getenv("CYCLES_CUDA_ADAPTIVE_COMPILE") != nullptr) {
adaptive_compile = true;
}
}
void DebugFlags::HIP::reset()
{
if (getenv("CYCLES_HIP_ADAPTIVE_COMPILE") != nullptr) {
adaptive_compile = true;
}
}
void DebugFlags::Metal::reset()
{
if (getenv("CYCLES_METAL_ADAPTIVE_COMPILE") != nullptr) {
adaptive_compile = true;
}
if (const char *str = getenv("CYCLES_METAL_LOCAL_ATOMIC_SORT")) {
use_local_atomic_sort = (atoi(str) != 0);
}
if (const char *str = getenv("CYCLES_METAL_NANOVDB")) {
use_nanovdb = (atoi(str) != 0);
}
if (const char *str = getenv("CYCLES_METAL_ASYNC_PSO_CREATION")) {
use_async_pso_creation = (atoi(str) != 0);
}
if (const char *str = getenv("CYCLES_METALRT_PCMI")) {
use_metalrt_pcmi = (atoi(str) != 0);
}
if (const char *str = getenv("CYCLES_METAL_RESIDENCY_SETS")) {
use_residency_sets_if_available = (atoi(str) != 0);
}
}
DebugFlags::TextureCache::TextureCache()
{
reset();
}
void DebugFlags::TextureCache::reset()
{
use_eviction = true;
preserve_unused = 0;
}
DebugFlags::OptiX::OptiX()
{
reset();
}
void DebugFlags::OptiX::reset()
{
use_debug = false;
}
void DebugFlags::reset()
{
cpu.reset();
cuda.reset();
optix.reset();
metal.reset();
texture_cache.reset();
}
CCL_NAMESPACE_END

View File

@@ -0,0 +1,169 @@
/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#pragma once
#include <cassert>
#include "bvh/params.h"
CCL_NAMESPACE_BEGIN
/* Global storage for all sort of flags used to fine-tune behavior of particular
* areas for the development purposes, without officially exposing settings to
* the interface.
*/
class DebugFlags {
public:
/* Descriptor of CPU feature-set to be used. */
struct CPU {
CPU();
/* Reset flags to their defaults. */
void reset();
/* Flags describing which instructions sets are allowed for use. */
bool avx2 = true;
bool sse42 = true;
/* Check functions to see whether instructions up to the given one
* are allowed for use.
*/
bool has_avx2()
{
return has_sse42() && avx2;
}
bool has_sse42()
{
return sse42;
}
/* Requested BVH layout.
*
* By default the fastest will be used. For debugging the BVH used by other
* CPUs and GPUs can be selected here instead.
*/
BVHLayout bvh_layout = BVH_LAYOUT_AUTO;
};
/* Descriptor of CUDA feature-set to be used. */
struct CUDA {
CUDA();
/* Reset flags to their defaults. */
void reset();
/* Whether adaptive feature based runtime compile is enabled or not.
* Requires the CUDA Toolkit and only works on Linux at the moment. */
bool adaptive_compile = false;
};
/* Descriptor of HIP feature-set to be used. */
struct HIP {
HIP();
/* Reset flags to their defaults. */
void reset();
/* Whether adaptive feature based runtime compile is enabled or not. */
bool adaptive_compile = false;
};
/* Descriptor of OptiX feature-set to be used. */
struct OptiX {
OptiX();
/* Reset flags to their defaults. */
void reset();
/* Load OptiX module with debug capabilities. Will lower logging verbosity level, enable
* validations, and lower optimization level. */
bool use_debug = false;
};
/* Descriptor of Metal feature-set to be used. */
struct Metal {
Metal();
/* Reset flags to their defaults. */
void reset();
/* Whether adaptive feature based runtime compile is enabled or not. */
bool adaptive_compile = false;
/* Whether local atomic sorting is enabled or not. */
bool use_local_atomic_sort = true;
/* Whether nanovdb is enabled or not. */
bool use_nanovdb = true;
/* Whether async PSO creation is enabled or not. */
bool use_async_pso_creation = true;
/* Whether to use per-component motion interpolation. */
bool use_metalrt_pcmi = true;
/* Whether to use residency sets. */
bool use_residency_sets_if_available = true;
};
/* Descriptor of Texture Cache feature-set to be used. */
struct TextureCache {
TextureCache();
/* Reset flags to their defaults. */
void reset();
/* Enable texture cache eviction. */
bool use_eviction = true;
/* Preserve unused image cache tile memory in megabytes. */
int preserve_unused = 0;
};
/* Get instance of debug flags registry. */
static DebugFlags &get()
{
static DebugFlags instance;
return instance;
}
/* Reset flags to their defaults. */
void reset();
/* Requested CPU flags. */
CPU cpu;
/* Requested CUDA flags. */
CUDA cuda;
/* Requested OptiX flags. */
OptiX optix;
/* Requested HIP flags. */
HIP hip;
/* Requested Metal flags. */
Metal metal;
/* Requested Texture Cache flags. */
TextureCache texture_cache;
private:
DebugFlags() = default;
public:
explicit DebugFlags(const DebugFlags & /*other*/) = delete;
void operator=(const DebugFlags & /*other*/) = delete;
};
using DebugFlagsRef = DebugFlags &;
using DebugFlagsConstRef = const DebugFlags &;
inline DebugFlags &DebugFlags()
{
return DebugFlags::get();
}
CCL_NAMESPACE_END

View File

@@ -0,0 +1,134 @@
/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
/* clang-format off */
/* #define __forceinline triggers a bug in some clang-format versions, disable
* format for entire file to keep results consistent. */
#pragma once
/* Bitness */
#if defined(__ppc64__) || defined(__PPC64__) || defined(__x86_64__) || defined(__ia64__) || \
defined(_M_X64) || defined(__aarch64__) || defined(_M_ARM64)
# define __KERNEL_64_BIT__
#endif
/* Qualifiers for kernel code shared by CPU and GPU */
#ifndef __KERNEL_GPU__
/* Leave inlining decisions to compiler for these, the inline keyword here
* is not about performance but including function definitions in headers. */
# define ccl_device static inline
# define ccl_device_extern extern "C"
# define ccl_device_noinline static inline
# define ccl_device_noinline_cpu ccl_device_noinline
/* Forced inlining. */
# if defined(_WIN32) && !defined(FREE_WINDOWS)
# define ccl_device_inline static __forceinline
# define ccl_device_forceinline static __forceinline
# define ccl_device_inline_method __forceinline
# define ccl_device_template_spec template<> __forceinline
# define ccl_align(...) __declspec(align(__VA_ARGS__))
# ifdef __KERNEL_64_BIT__
# define ccl_try_align(...) __declspec(align(__VA_ARGS__))
# else /* __KERNEL_64_BIT__ */
# undef __KERNEL_WITH_SSE_ALIGN__
/* No support for function arguments (error C2719). */
# define ccl_try_align(...)
# endif /* __KERNEL_64_BIT__ */
# define ccl_may_alias
# define ccl_always_inline __forceinline
# define ccl_never_inline __declspec(noinline)
# else /* _WIN32 && !FREE_WINDOWS */
# define ccl_device_inline static inline __attribute__((always_inline))
# define ccl_device_forceinline static inline __attribute__((always_inline))
# define ccl_device_inline_method __attribute__((always_inline))
# define ccl_device_template_spec template<> inline __attribute__((always_inline))
# define ccl_align(...) __attribute__((aligned(__VA_ARGS__)))
# ifndef FREE_WINDOWS64
# define __forceinline inline __attribute__((always_inline))
# endif
# define ccl_try_align(...) __attribute__((aligned(__VA_ARGS__)))
# define ccl_may_alias __attribute__((__may_alias__))
# define ccl_always_inline __attribute__((always_inline))
# define ccl_never_inline __attribute__((noinline))
# endif /* _WIN32 && !FREE_WINDOWS */
/* Address spaces for GPU. */
# define ccl_global
# define ccl_inline_constant inline constexpr
# define ccl_static_constexpr static constexpr
# define ccl_constant const
# define ccl_private
# define ccl_ray_data ccl_private
# define ccl_restrict __restrict
# define ccl_optional_struct_init
# define ccl_attr_maybe_unused [[maybe_unused]]
# define __KERNEL_WITH_SSE_ALIGN__
/* Use to suppress `-Wimplicit-fallthrough` (in place of `break`). */
# ifndef ATTR_FALLTHROUGH
# if defined(__GNUC__) && (__GNUC__ >= 7) /* gcc7.0+ only */
# define ATTR_FALLTHROUGH __attribute__((fallthrough))
# else
# define ATTR_FALLTHROUGH ((void)0)
# endif
# endif
#endif /* __KERNEL_GPU__ */
/* Address sanitizer suppression. */
#ifdef __KERNEL_GPU__
# define ccl_ignore_integer_overflow
#else
# if defined(__clang__)
# if __has_feature(address_sanitizer)
# define ccl_ignore_integer_overflow [[clang::no_sanitize("signed-integer-overflow")]]
# else
# define ccl_ignore_integer_overflow
# endif
# elif defined(__GNUC__) && defined(__SANITIZE_ADDRESS__)
# define ccl_ignore_integer_overflow [[gnu::no_sanitize("signed-integer-overflow")]]
# else
# define ccl_ignore_integer_overflow
# endif
#endif
/* macros */
/* hints for branch prediction, only use in code that runs a _lot_ */
#if defined(__GNUC__) && !defined(__KERNEL_GPU__)
# define LIKELY(x) __builtin_expect(!!(x), 1)
# define UNLIKELY(x) __builtin_expect(!!(x), 0)
#else
# define LIKELY(x) (x)
# define UNLIKELY(x) (x)
#endif
#ifndef __KERNEL_GPU__
# include <cassert>
# define util_assert(statement) assert(statement)
#else
# define util_assert(statement)
#endif
#if (defined __KERNEL_METAL__) && (__METAL_VERSION__ >= 320)
# define __METAL_PRINTF__
#endif
/* Metal's logging works very similar to `printf()`, except for a few differences:
* - %s is not supported,
* - double doesn't exist, so no casting to double for %f,
* - no `\n` needed at the end of the format string.
* NOTE: To see the print in the console, environment variables `MTL_LOG_LEVEL` should be set to
* `MTLLogLevelDebug`, and `MTL_LOG_TO_STDERR` should be set to `1`.
* See https://developer.apple.com/documentation/metal/logging-shader-debug-messages */
# ifdef __METAL_PRINTF__
# define printf(...) metal::os_log_default.log_debug(__VA_ARGS__)
# endif

View File

@@ -0,0 +1,13 @@
/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#pragma once
#include <deque>
CCL_NAMESPACE_BEGIN
using std::deque;
CCL_NAMESPACE_END

View File

@@ -0,0 +1,60 @@
/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#pragma once
#include "util/array.h"
#include <utility>
CCL_NAMESPACE_BEGIN
class DisjointSet {
private:
array<size_t> parents;
array<size_t> ranks;
public:
DisjointSet(const size_t size) : parents(size), ranks(size)
{
for (size_t i = 0; i < size; i++) {
parents[i] = i;
ranks[i] = 0;
}
}
size_t find(size_t x)
{
size_t root = x;
while (parents[root] != root) {
root = parents[root];
}
while (parents[x] != root) {
const size_t parent = parents[x];
parents[x] = root;
x = parent;
}
return root;
}
void join(const size_t x, const size_t y)
{
size_t x_root = find(x);
size_t y_root = find(y);
if (x_root == y_root) {
return;
}
if (ranks[x_root] < ranks[y_root]) {
std::swap(x_root, y_root);
}
parents[y_root] = x_root;
if (ranks[x_root] == ranks[y_root]) {
ranks[x_root]++;
}
}
};
CCL_NAMESPACE_END

View File

@@ -0,0 +1,36 @@
/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#include "util/guarded_allocator.h"
#include "util/stats.h"
CCL_NAMESPACE_BEGIN
static Stats global_stats(Stats::static_init);
/* Internal API. */
void util_guarded_mem_alloc(const size_t n)
{
global_stats.mem_alloc(n);
}
void util_guarded_mem_free(const size_t n)
{
global_stats.mem_free(n);
}
/* Public API. */
size_t util_guarded_get_mem_used()
{
return global_stats.mem_used;
}
size_t util_guarded_get_mem_peak()
{
return global_stats.mem_peak;
}
CCL_NAMESPACE_END

View File

@@ -0,0 +1,161 @@
/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#pragma once
#include <cstddef>
#include <cstdlib>
#include <new>
#ifdef WITH_BLENDER_GUARDEDALLOC
# include "../../guardedalloc/MEM_guardedalloc.h"
#endif
CCL_NAMESPACE_BEGIN
/* Internal use only. */
void util_guarded_mem_alloc(const size_t n);
void util_guarded_mem_free(const size_t n);
/* Guarded allocator for the use with STL. */
template<typename T> class GuardedAllocator {
public:
using size_type = size_t;
using difference_type = ptrdiff_t;
using pointer = T *;
using const_pointer = const T *;
using reference = T &;
using const_reference = const T &;
using value_type = T;
GuardedAllocator() = default;
GuardedAllocator(const GuardedAllocator & /*unused*/) = default;
T *allocate(const size_t n, const void *hint = nullptr)
{
(void)hint;
size_t size = n * sizeof(T);
util_guarded_mem_alloc(size);
if (n == 0) {
return nullptr;
}
T *mem;
#ifdef WITH_BLENDER_GUARDEDALLOC
/* C++ standard requires allocation functions to allocate memory suitably
* aligned for any standard type. This is 16 bytes for 64 bit platform as
* far as i concerned. We might over-align on 32bit here, but that should
* be all safe actually.
*/
mem = (T *)MEM_new_uninitialized_aligned(size, 16, "Cycles Alloc");
#else
mem = (T *)malloc(size);
#endif
if (mem == nullptr) {
throw std::bad_alloc();
}
return mem;
}
void deallocate(T *p, const size_t n)
{
util_guarded_mem_free(n * sizeof(T));
if (p != nullptr) {
#ifdef WITH_BLENDER_GUARDEDALLOC
MEM_delete_void(const_cast<void *>(static_cast<const void *>(p)));
#else
free(p);
#endif
}
}
T *address(T &x) const
{
return &x;
}
const T *address(const T &x) const
{
return &x;
}
GuardedAllocator<T> &operator=(const GuardedAllocator & /*unused*/) = default;
size_t max_size() const
{
return size_t(-1);
}
template<class U> struct rebind {
using other = GuardedAllocator<U>;
};
template<class U> GuardedAllocator(const GuardedAllocator<U> & /*unused*/) {}
template<class U> GuardedAllocator &operator=(const GuardedAllocator<U> & /*unused*/)
{
return *this;
}
bool operator==(const GuardedAllocator & /*other*/) const
{
return true;
}
bool operator!=(const GuardedAllocator &other) const
{
return !operator==(other);
}
#ifdef _MSC_VER
/* Welcome to the black magic here.
*
* The issue is that MSVC C++ allocates container proxy on any
* vector initialization, including static vectors which don't
* have any data yet. This leads to several issues:
*
* - Static objects initialization fiasco (global_stats from
* util_stats.h might not be initialized yet).
* - If main() function changes allocator type (for example,
* this might happen with `blender --debug-memory`) nobody
* will know how to convert already allocated memory to a new
* guarded allocator.
*
* Here we work this around by making it so container proxy does
* not use guarded allocation. A bit fragile, unfortunately.
*/
template<> struct rebind<std::_Container_proxy> {
typedef std::allocator<std::_Container_proxy> other;
};
operator std::allocator<std::_Container_proxy>() const
{
return std::allocator<std::_Container_proxy>();
}
#endif
};
/* Get memory usage and peak from the guarded STL allocator. */
size_t util_guarded_get_mem_used();
size_t util_guarded_get_mem_peak();
/* Call given function and keep track if it runs out of memory.
*
* If it does run out f memory, stop execution and set progress
* to do a global cancel.
*
* It's not fully robust, but good enough to catch obvious issues
* when running out of memory.
*/
#define MEM_GUARDED_CALL(progress, func, ...) \
do { \
try { \
(func)(__VA_ARGS__); \
} \
catch (std::bad_alloc &) { \
LOG_ERROR << "Out of memory"; \
fflush(stderr); \
(progress)->set_error("Out of memory"); \
} \
} while (false)
CCL_NAMESPACE_END

View File

@@ -0,0 +1,40 @@
/* SPDX-FileCopyrightText: 2022 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#pragma once
#if defined(WITH_PATH_GUIDING)
# include <openpgl/cpp/OpenPGL.h> // IWYU pragma: export
# include <openpgl/version.h> // IWYU pragma: export
#endif
#include "util/system.h" // IWYU pragma: keep
CCL_NAMESPACE_BEGIN
static int guiding_device_type()
{
#if defined(WITH_PATH_GUIDING)
# if defined(__ARM_NEON)
return 8;
# else
if (system_cpu_support_avx2()) {
return 8;
}
if (system_cpu_support_sse42()) {
return 4;
}
return 0;
# endif
#else
return 0;
#endif
}
static inline bool guiding_supported()
{
return guiding_device_type() != 0;
}
CCL_NAMESPACE_END

View File

@@ -0,0 +1,318 @@
/* SPDX-FileCopyrightText: 2011-2026 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#pragma once
#include "util/defines.h"
#include "util/math_base.h"
#include "util/math_float4.h"
#include "util/math_int4.h"
#include "util/types_base.h"
#include "util/types_float4.h"
#include "util/types_int4.h"
#include "util/types_uint4.h"
#if !defined(__KERNEL_GPU__) && defined(__KERNEL_SSE2__)
# include "util/optimization.h" // IWYU pragma: keep
# include "util/simd.h" // IWYU pragma: keep
#endif
CCL_NAMESPACE_BEGIN
/* Half Floats */
#if !defined(__KERNEL_GPU__)
/* GPUs have native support for this type.
* Implementing this as a class rather than a typedef so that the compiler can tell it apart from
* uint16_ts. */
class half {
public:
half() = default;
half(const uint16_t &i) : v(i) {}
operator uint16_t() const
{
return v;
}
half &operator=(const uint16_t &i)
{
v = i;
return *this;
}
private:
uint16_t v;
};
#endif
#if !defined(__KERNEL_METAL__)
struct half3 {
half x, y, z;
};
struct half4 {
half x, y, z, w;
};
#endif
#if !defined(__KERNEL_GPU__)
/* Optimized fallback implementations with fast path for normal and denormal numbers, assuming
* no Infs or NaNs. Based on public domain functions from.
*
* https://fgiesen.wordpress.com/2012/03/28/half-to-float-done-quic/
* https://gist.github.com/rygorous/2144712
* https://gist.github.com/rygorous/2156668
* https://gist.github.com/rygorous/4d9e9e88cab13c703773dc767a23575f
*/
ccl_device_inline float fallback_half_to_float(const half h)
{
const uint32_t bits = uint16_t(h);
const uint32_t s = (bits & 0x8000) << 16;
const uint32_t em = (bits & 0x7fff) << 13;
const float f = __int_as_float(em) * __int_as_float(0x77800000 /* 2^112 */);
return __int_as_float(__float_as_uint(f) | s);
}
ccl_device_inline float4 fallback_half4_to_float4(const half4 h)
{
const int4 i = make_int4(uint16_t(h.x), uint16_t(h.y), uint16_t(h.z), uint16_t(h.w));
const int4 s = (i & 0x8000) << 16;
const int4 em = (i & 0x7fff) << 13;
const float4 f = cast(em) * __int_as_float(0x77800000 /* 2^112 */);
return cast(cast(f) | s);
}
ccl_device_inline float3 fallback_half3_to_float3(const half3 h)
{
return make_float3(fallback_half4_to_float4({h.x, h.y, h.z, 0}));
}
ccl_device_inline half fallback_float_to_half(const float f)
{
const int c_f16max = (127 + 16) << 23;
const int c_infty_as_fp16 = 0x7c00;
const int c_min_normal = (127 - 14) << 23;
const int c_denorm_magic = ((127 - 15) + (23 - 10) + 1) << 23;
const int c_normal_bias = 0xfff - ((127 - 15) << 23);
const uint f_i = __float_as_uint(f);
const uint sign_i = f_i & 0x80000000u;
const int abs_i = int(f_i ^ sign_i);
uint16_t res;
if (abs_i >= c_f16max) {
/* Overflows to infinity. */
res = uint16_t(c_infty_as_fp16);
}
else if (abs_i < c_min_normal) {
/* Denormal. */
float denorm_f = __uint_as_float(uint(abs_i));
denorm_f += __int_as_float(c_denorm_magic);
const int denorm_i = int(__float_as_uint(denorm_f)) - c_denorm_magic;
res = uint16_t(denorm_i);
}
else {
/* Normal. */
const int mant_odd = int(uint(abs_i) >> 13) & 1;
res = uint16_t((abs_i + c_normal_bias + mant_odd) >> 13);
}
return half(res | uint16_t(sign_i >> 16));
}
ccl_device_inline half4 fallback_float4_to_half4(const float4 f)
{
const int4 c_f16max = make_int4((127 + 16) << 23);
const int4 c_infty_as_fp16 = make_int4(0x7c00);
const int4 c_min_normal = make_int4((127 - 14) << 23);
const int4 c_denorm_magic = make_int4(((127 - 15) + (23 - 10) + 1) << 23);
const int4 c_normal_bias = make_int4(0xfff - ((127 - 15) << 23));
const float4 abs_f = fabs(f);
const int4 abs_i = __float4_as_int4(abs_f);
const int4 b_isregular = c_f16max > abs_i;
const int4 b_isdenorm = c_min_normal > abs_i;
/* Denormal. */
const float4 denorm_f = abs_f + __int4_as_float4(c_denorm_magic);
const int4 denorm_i = __float4_as_int4(denorm_f) - c_denorm_magic;
/* Normal. */
const int4 mant_odd = (abs_i << (31 - 13)) >> 31;
const int4 normal = srl(abs_i + c_normal_bias - mant_odd, 13);
/* Combined normal and denormal. */
const int4 nonspecial = select(b_isdenorm, denorm_i, normal);
/* Combine overflow to infinity. */
const int4 combined = select(b_isregular, nonspecial, c_infty_as_fp16);
const int4 sign_i = __float4_as_int4(f ^ abs_f);
const int4 res = combined | (sign_i >> 16);
return {
half(uint16_t(res.x)), half(uint16_t(res.y)), half(uint16_t(res.z)), half(uint16_t(res.w))};
}
ccl_device_inline half3 fallback_float3_to_half3(const float3 f)
{
const half4 h = fallback_float4_to_half4(make_float4(f));
return {h.x, h.y, h.z};
}
#endif
ccl_device_inline float half_to_float(half h)
{
#if defined(__KERNEL_METAL__) || defined(__KERNEL_ONEAPI__)
return float(h);
#elif defined(__KERNEL_CUDA__) || defined(__KERNEL_HIP__)
return __half2float(h);
/* We assume half instructions are always supported when there is ARM Neon,
* which implies ARMv8.2-A+. There is no official Blender minimum, but is
* already assumed elsewhere in Blender and not that recent. */
#elif defined(__ARM_NEON) || defined(_M_ARM64)
uint16x4_t v = vdup_n_u16(uint16_t(h));
return vgetq_lane_f32(vcvt_f32_f16(vreinterpret_f16_u16(v)), 0);
#elif defined(__F16C__)
return _cvtsh_ss(uint16_t(h));
#else
/* The fallback is fast so don't bother with native instructions. */
return fallback_half_to_float(h);
#endif
}
ccl_device_inline half float_to_half(const float f)
{
#if defined(__KERNEL_METAL__) || defined(__KERNEL_ONEAPI__)
return half(f);
#elif defined(__KERNEL_CUDA__) || defined(__KERNEL_HIP__)
return __float2half(f);
#elif defined(__ARM_NEON) || defined(_M_ARM64)
return half(vget_lane_u16(vreinterpret_u16_f16(vcvt_f16_f32(vdupq_n_f32(f))), 0));
#elif defined(__F16C__)
return half((uint16_t)_cvtss_sh(f, _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC));
#else
return fallback_float_to_half(f);
#endif
}
ccl_device_inline half4 float4_to_half4(const float4 f)
{
#if defined(__KERNEL_METAL__)
return {half(f.x), half(f.y), half(f.z), half(f.w)};
#elif defined(__KERNEL_ONEAPI__)
return {half(f.x), half(f.y), half(f.z), half(f.w)};
#elif defined(__KERNEL_CUDA__) || defined(__KERNEL_HIP__)
return {__float2half(f.x), __float2half(f.y), __float2half(f.z), __float2half(f.w)};
#elif defined(__ARM_NEON) || defined(_M_ARM64)
half4 r;
vst1_u16(reinterpret_cast<uint16_t *>(&r),
vreinterpret_u16_f16(vcvt_f16_f32(float32x4_t{f.x, f.y, f.z, f.w})));
return r;
#elif defined(__KERNEL_SSE__) && defined(__F16C__)
half4 r;
const __m128i h = _mm_cvtps_ph(_mm_loadu_ps(&f.x),
_MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
_mm_storel_epi64(reinterpret_cast<__m128i *>(&r), h);
return r;
#else
return fallback_float4_to_half4(f);
#endif
}
ccl_device_inline float4 half4_to_float4(const half4 h)
{
#if defined(__KERNEL_METAL__)
return {float(h.x), float(h.y), float(h.z), float(h.w)};
#elif defined(__KERNEL_ONEAPI__)
return {float(h.x), float(h.y), float(h.z), float(h.w)};
#elif defined(__KERNEL_CUDA__) || defined(__KERNEL_HIP__)
return {__half2float(h.x), __half2float(h.y), __half2float(h.z), __half2float(h.w)};
#elif defined(__ARM_NEON) || defined(_M_ARM64)
float4 r;
vst1q_f32(&r.x,
vcvt_f32_f16(vreinterpret_f16_u16(vld1_u16(reinterpret_cast<const uint16_t *>(&h)))));
return r;
#elif defined(__KERNEL_SSE__) && defined(__F16C__)
float4 r;
_mm_storeu_ps(&r.x, _mm_cvtph_ps(_mm_loadl_epi64(reinterpret_cast<const __m128i *>(&h))));
return r;
#else
return fallback_half4_to_float4(h);
#endif
}
ccl_device_inline half3 float3_to_half3(const float3 f)
{
#if defined(__KERNEL_GPU__)
return {float_to_half(f.x), float_to_half(f.y), float_to_half(f.z)};
#elif defined(__ARM_NEON) || defined(_M_ARM64)
const uint16x4_t h = vreinterpret_u16_f16(vcvt_f16_f32(float32x4_t{f.x, f.y, f.z, 0.0f}));
return {half(vget_lane_u16(h, 0)), half(vget_lane_u16(h, 1)), half(vget_lane_u16(h, 2))};
#elif defined(__KERNEL_SSE__) && defined(__F16C__)
const __m128i h = _mm_cvtps_ph(_mm_set_ps(0.0f, f.z, f.y, f.x),
_MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
return {half(uint16_t(_mm_extract_epi16(h, 0))),
half(uint16_t(_mm_extract_epi16(h, 1))),
half(uint16_t(_mm_extract_epi16(h, 2)))};
#else
half4 h = fallback_float4_to_half4(make_float4(f));
return {h.x, h.y, h.z};
#endif
}
ccl_device_inline float3 half3_to_float3(const half3 h)
{
#if defined(__KERNEL_GPU__)
return make_float3(half_to_float(h.x), half_to_float(h.y), half_to_float(h.z));
#elif defined(__ARM_NEON) || defined(_M_ARM64)
const float32x4_t f = vcvt_f32_f16(
vreinterpret_f16_u16(uint16x4_t{uint16_t(h.x), uint16_t(h.y), uint16_t(h.z), 0}));
return make_float3(vgetq_lane_f32(f, 0), vgetq_lane_f32(f, 1), vgetq_lane_f32(f, 2));
#elif defined(__KERNEL_SSE__) && defined(__F16C__)
const __m128i v = _mm_set_epi16(0, 0, 0, 0, 0, uint16_t(h.z), uint16_t(h.y), uint16_t(h.x));
const __m128 f = _mm_cvtph_ps(v);
return make_float3(float4(f));
#else
return make_float3(fallback_half4_to_float4({h.x, h.y, h.z, 0}));
#endif
}
/* For image textures. */
ccl_device_inline half float_to_half_image(const float f)
{
return float_to_half(clamp(f, -65504.0f, 65504.0f));
}
ccl_device_inline float half_to_float_image(half h)
{
return half_to_float(h);
}
ccl_device_inline float4 half4_to_float4_image(const half4 h)
{
return half4_to_float4(h);
}
/* For render display. */
ccl_device_inline half float_to_half_display(const float f)
{
return float_to_half(clamp(f, 0.0f, 65504.0f));
}
ccl_device_inline half4 float4_to_half4_display(const float4 f)
{
return float4_to_half4(clamp(f, make_float4(0.0f), make_float4(65504.0f)));
}
#ifndef __KERNEL_GPU__
ccl_device_inline float half_is_finite(const half h)
{
const int exponent = (uint16_t(h) >> 10) & 0x001f;
return exponent < 31;
}
#endif
CCL_NAMESPACE_END

View File

@@ -0,0 +1,650 @@
/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#pragma once
#include "util/defines.h"
#include "util/math.h"
#include "util/types.h"
CCL_NAMESPACE_BEGIN
/* [0, uint_max] -> [0.0, 1.0) */
ccl_device_forceinline float uint_to_float_excl(const uint n)
{
/* NOTE: we divide by 4294967808 instead of 2^32 because the latter
* leads to a [0.0, 1.0] mapping instead of [0.0, 1.0) due to floating
* point rounding error. 4294967808 unfortunately leaves (precisely)
* one unused ULP between the max number this outputs and 1.0, but
* that's the best you can do with this construction. */
return (float)n * (1.0f / 4294967808.0f);
}
/* [0, uint_max] -> [0.0, 1.0] */
ccl_device_forceinline float uint_to_float_incl(const uint n)
{
return (float)n * (1.0f / (float)0xFFFFFFFFu);
}
/* PCG 2D, 3D and 4D hash functions,
* from "Hash Functions for GPU Rendering" JCGT 2020
* https://jcgt.org/published/0009/03/02/
*
* Slightly modified to only use signed integers,
* so that they can also be implemented in OSL.
*
* Silence UBsan warnings about signed integer overflow. */
ccl_ignore_integer_overflow ccl_device_inline int2 hash_pcg2d_i(int2 v)
{
v = v * make_int2(1664525) + make_int2(1013904223);
v.x += v.y * 1664525;
v.y += v.x * 1664525;
v = v ^ (v >> 16);
v.x += v.y * 1664525;
v.y += v.x * 1664525;
return v & make_int2(0x7FFFFFFF);
}
ccl_ignore_integer_overflow ccl_device_inline int3 hash_pcg3d_i(int3 v)
{
v = v * make_int3(1664525) + make_int3(1013904223);
v.x += v.y * v.z;
v.y += v.z * v.x;
v.z += v.x * v.y;
v = v ^ (v >> 16);
v.x += v.y * v.z;
v.y += v.z * v.x;
v.z += v.x * v.y;
return v & make_int3(0x7FFFFFFF);
}
ccl_ignore_integer_overflow ccl_device_inline int4 hash_pcg4d_i(int4 v)
{
v = v * make_int4(1664525) + make_int4(1013904223);
v.x += v.y * v.w;
v.y += v.z * v.x;
v.z += v.x * v.y;
v.w += v.y * v.z;
v = v ^ (v >> 16);
v.x += v.y * v.w;
v.y += v.z * v.x;
v.z += v.x * v.y;
v.w += v.y * v.z;
return v & make_int4(0x7FFFFFFF);
}
/* ***** Jenkins Lookup3 Hash Functions ***** */
/* Source: http://burtleburtle.net/bob/c/lookup3.c */
#define rot(x, k) (((x) << (k)) | ((x) >> (32 - (k))))
#define mix(a, b, c) \
{ \
a -= c; \
a ^= rot(c, 4); \
c += b; \
b -= a; \
b ^= rot(a, 6); \
a += c; \
c -= b; \
c ^= rot(b, 8); \
b += a; \
a -= c; \
a ^= rot(c, 16); \
c += b; \
b -= a; \
b ^= rot(a, 19); \
a += c; \
c -= b; \
c ^= rot(b, 4); \
b += a; \
} \
((void)0)
#define final(a, b, c) \
{ \
c ^= b; \
c -= rot(b, 14); \
a ^= c; \
a -= rot(c, 11); \
b ^= a; \
b -= rot(a, 25); \
c ^= b; \
c -= rot(b, 16); \
a ^= c; \
a -= rot(c, 4); \
b ^= a; \
b -= rot(a, 14); \
c ^= b; \
c -= rot(b, 24); \
} \
((void)0)
ccl_device_inline uint hash_uint(const uint kx)
{
uint a;
uint b;
uint c;
a = b = c = 0xdeadbeef + (1 << 2) + 13;
a += kx;
final(a, b, c);
return c;
}
ccl_device_inline uint hash_uint2(const uint kx, const uint ky)
{
uint a;
uint b;
uint c;
a = b = c = 0xdeadbeef + (2 << 2) + 13;
b += ky;
a += kx;
final(a, b, c);
return c;
}
ccl_device_inline uint hash_uint3(const uint kx, const uint ky, const uint kz)
{
uint a;
uint b;
uint c;
a = b = c = 0xdeadbeef + (3 << 2) + 13;
c += kz;
b += ky;
a += kx;
final(a, b, c);
return c;
}
ccl_device_inline uint hash_uint4(const uint kx, const uint ky, const uint kz, const uint kw)
{
uint a;
uint b;
uint c;
a = b = c = 0xdeadbeef + (4 << 2) + 13;
a += kx;
b += ky;
c += kz;
mix(a, b, c);
a += kw;
final(a, b, c);
return c;
}
#undef rot
#undef final
#undef mix
/* Hashing uint or uint[234] into a float in the range [0, 1]. */
ccl_device_inline float hash_uint_to_float(const uint kx)
{
return uint_to_float_incl(hash_uint(kx));
}
ccl_device_inline float hash_uint2_to_float(const uint kx, const uint ky)
{
return uint_to_float_incl(hash_uint2(kx, ky));
}
ccl_device_inline float hash_uint3_to_float(const uint kx, const uint ky, const uint kz)
{
return uint_to_float_incl(hash_uint3(kx, ky, kz));
}
ccl_device_inline float hash_uint4_to_float(const uint kx,
const uint ky,
const uint kz,
const uint kw)
{
return uint_to_float_incl(hash_uint4(kx, ky, kz, kw));
}
/* Hashing float or float[234] into a float in the range [0, 1]. */
ccl_device_inline float hash_float_to_float(const float k)
{
return hash_uint_to_float(__float_as_uint(k));
}
ccl_device_inline float hash_float2_to_float(const float2 k)
{
return hash_uint2_to_float(__float_as_uint(k.x), __float_as_uint(k.y));
}
ccl_device_inline float hash_float3_to_float(const float3 k)
{
return hash_uint3_to_float(__float_as_uint(k.x), __float_as_uint(k.y), __float_as_uint(k.z));
}
ccl_device_inline float hash_float4_to_float(const float4 k)
{
return hash_uint4_to_float(
__float_as_uint(k.x), __float_as_uint(k.y), __float_as_uint(k.z), __float_as_uint(k.w));
}
/* Hashing int[234] into float[234] of components in the range [0, 1].
* These are based on PCG 2D/3D/4D. */
ccl_device_inline float2 hash_int2_to_float2(const int2 k)
{
int2 h = hash_pcg2d_i(k);
float2 f = make_float2((float)h.x, (float)h.y);
return f * (1.0f / (float)0x7FFFFFFFu);
}
ccl_device_inline float3 hash_int3_to_float3(const int3 k)
{
int3 h = hash_pcg3d_i(k);
float3 f = make_float3((float)h.x, (float)h.y, (float)h.z);
return f * (1.0f / (float)0x7FFFFFFFu);
}
ccl_device_inline float4 hash_int4_to_float4(const int4 k)
{
int4 h = hash_pcg4d_i(k);
float4 f = make_float4(h);
return f * (1.0f / (float)0x7FFFFFFFu);
}
ccl_device_inline float3 hash_int2_to_float3(const int2 k)
{
return hash_int3_to_float3(make_int3(k.x, k.y, 0));
}
ccl_device_inline float3 hash_int4_to_float3(const int4 k)
{
return make_float3(hash_int4_to_float4(k));
}
/* Hashing int[234] / float[234] into float[234] of components in the range [0, 1].
*
* Note that while using a more modern hash (e.g. PCG) would be faster, the current
* behavior has to be kept to match what is possible in OSL (OSL lacks bit casts and unsigned
* integers). */
ccl_device_inline float2 hash_float2_to_float2(const float2 k)
{
return make_float2(hash_float2_to_float(k), hash_float3_to_float(make_float3(k.x, k.y, 1.0)));
}
ccl_device_inline float3 hash_float3_to_float3(const float3 k)
{
return make_float3(hash_float3_to_float(k),
hash_float4_to_float(make_float4(k.x, k.y, k.z, 1.0)),
hash_float4_to_float(make_float4(k.x, k.y, k.z, 2.0)));
}
ccl_device_inline float4 hash_float4_to_float4(const float4 k)
{
return make_float4(hash_float4_to_float(k),
hash_float4_to_float(make_float4(k.w, k.x, k.y, k.z)),
hash_float4_to_float(make_float4(k.z, k.w, k.x, k.y)),
hash_float4_to_float(make_float4(k.y, k.z, k.w, k.x)));
}
/* Hashing float or float[234] into float3 of components in range [0, 1]. */
ccl_device_inline float3 hash_float_to_float3(const float k)
{
return make_float3(hash_float_to_float(k),
hash_float2_to_float(make_float2(k, 1.0)),
hash_float2_to_float(make_float2(k, 2.0)));
}
ccl_device_inline float3 hash_float2_to_float3(const float2 k)
{
return make_float3(hash_float2_to_float(k),
hash_float3_to_float(make_float3(k.x, k.y, 1.0)),
hash_float3_to_float(make_float3(k.x, k.y, 2.0)));
}
ccl_device_inline float3 hash_float4_to_float3(const float4 k)
{
return make_float3(hash_float4_to_float(k),
hash_float4_to_float(make_float4(k.z, k.x, k.w, k.y)),
hash_float4_to_float(make_float4(k.w, k.z, k.y, k.x)));
}
/* Hashing float or float[234] into float2 of components in range [0, 1]. */
ccl_device_inline float2 hash_float_to_float2(const float k)
{
return make_float2(hash_float_to_float(k), hash_float2_to_float(make_float2(k, 1.0)));
}
ccl_device_inline float2 hash_float3_to_float2(const float3 k)
{
return make_float2(hash_float3_to_float(make_float3(k.x, k.y, k.z)),
hash_float3_to_float(make_float3(k.z, k.x, k.y)));
}
ccl_device_inline float2 hash_float4_to_float2(const float4 k)
{
return make_float2(hash_float4_to_float(make_float4(k.x, k.y, k.z, k.w)),
hash_float4_to_float(make_float4(k.z, k.x, k.w, k.y)));
}
/* SSE Versions Of Jenkins Lookup3 Hash Functions */
#ifdef __KERNEL_SSE__
# define rot(x, k) (((x) << (k)) | (srl(x, 32 - (k))))
# define mix(a, b, c) \
{ \
a -= c; \
a ^= rot(c, 4); \
c += b; \
b -= a; \
b ^= rot(a, 6); \
a += c; \
c -= b; \
c ^= rot(b, 8); \
b += a; \
a -= c; \
a ^= rot(c, 16); \
c += b; \
b -= a; \
b ^= rot(a, 19); \
a += c; \
c -= b; \
c ^= rot(b, 4); \
b += a; \
}
# define final(a, b, c) \
{ \
c ^= b; \
c -= rot(b, 14); \
a ^= c; \
a -= rot(c, 11); \
b ^= a; \
b -= rot(a, 25); \
c ^= b; \
c -= rot(b, 16); \
a ^= c; \
a -= rot(c, 4); \
b ^= a; \
b -= rot(a, 14); \
c ^= b; \
c -= rot(b, 24); \
}
ccl_device_inline int4 hash_int4(const int4 kx)
{
int4 a;
int4 b;
int4 c;
a = b = c = make_int4(0xdeadbeef + (1 << 2) + 13);
a += kx;
final(a, b, c);
return c;
}
ccl_device_inline int4 hash_int4_2(const int4 kx, const int4 ky)
{
int4 a;
int4 b;
int4 c;
a = b = c = make_int4(0xdeadbeef + (2 << 2) + 13);
b += ky;
a += kx;
final(a, b, c);
return c;
}
ccl_device_inline int4 hash_int4_3(const int4 kx, const int4 ky, const int4 kz)
{
int4 a;
int4 b;
int4 c;
a = b = c = make_int4(0xdeadbeef + (3 << 2) + 13);
c += kz;
b += ky;
a += kx;
final(a, b, c);
return c;
}
ccl_device_inline int4 hash_int4_4(const int4 kx, const int4 ky, const int4 kz, const int4 kw)
{
int4 a;
int4 b;
int4 c;
a = b = c = make_int4(0xdeadbeef + (4 << 2) + 13);
a += kx;
b += ky;
c += kz;
mix(a, b, c);
a += kw;
final(a, b, c);
return c;
}
# if defined(__KERNEL_AVX2__)
ccl_device_inline vint8 hash_int8(vint8 kx)
{
vint8 a, b, c;
a = b = c = make_vint8(0xdeadbeef + (1 << 2) + 13);
a += kx;
final(a, b, c);
return c;
}
ccl_device_inline vint8 hash_int8_2(vint8 kx, vint8 ky)
{
vint8 a, b, c;
a = b = c = make_vint8(0xdeadbeef + (2 << 2) + 13);
b += ky;
a += kx;
final(a, b, c);
return c;
}
ccl_device_inline vint8 hash_int8_3(vint8 kx, vint8 ky, vint8 kz)
{
vint8 a, b, c;
a = b = c = make_vint8(0xdeadbeef + (3 << 2) + 13);
c += kz;
b += ky;
a += kx;
final(a, b, c);
return c;
}
ccl_device_inline vint8 hash_int8_4(vint8 kx, vint8 ky, vint8 kz, vint8 kw)
{
vint8 a, b, c;
a = b = c = make_vint8(0xdeadbeef + (4 << 2) + 13);
a += kx;
b += ky;
c += kz;
mix(a, b, c);
a += kw;
final(a, b, c);
return c;
}
# endif
# undef rot
# undef final
# undef mix
#endif
/* ***** Hash Prospector Hash Functions *****
*
* These are based on the high-quality 32-bit hash/mixing functions from
* https://github.com/skeeto/hash-prospector
*/
ccl_device_inline uint hash_hp_uint(uint i)
{
// The actual mixing function from Hash Prospector.
i ^= i >> 16;
i *= 0x21f0aaad;
i ^= i >> 15;
i *= 0xd35a2d97;
i ^= i >> 15;
// The xor is just to make input zero not map to output zero.
// The number is randomly selected and isn't special.
return i ^ 0xe6fe3beb;
}
/* Seedable version of hash_hp_uint() above. */
ccl_device_inline uint hash_hp_seeded_uint(const uint i, uint seed)
{
// Manipulate the seed so it doesn't interact poorly with n when they
// are both e.g. incrementing. This isn't fool-proof, but is good
// enough for practical use.
seed ^= seed << 19;
return hash_hp_uint(i ^ seed);
}
/* Outputs [0.0, 1.0). */
ccl_device_inline float hash_hp_float(const uint i)
{
return uint_to_float_excl(hash_hp_uint(i));
}
/* Outputs [0.0, 1.0). */
ccl_device_inline float hash_hp_seeded_float(const uint i, const uint seed)
{
return uint_to_float_excl(hash_hp_seeded_uint(i, seed));
}
/* ***** Modified Wang Hash Functions *****
*
* These are based on a bespoke modified version of the Wang hash, and
* can serve as a faster hash when quality isn't critical.
*
* The original Wang hash is documented here:
* https://www.burtleburtle.net/bob/hash/integer.html
*/
ccl_device_inline uint hash_wang_seeded_uint(uint i, const uint seed)
{
i = (i ^ 61) ^ seed;
i += i << 3;
i ^= i >> 4;
i *= 0x27d4eb2d;
return i;
}
/* Outputs [0.0, 1.0). */
ccl_device_inline float hash_wang_seeded_float(const uint i, const uint seed)
{
return uint_to_float_excl(hash_wang_seeded_uint(i, seed));
}
/* ***** Index Shuffling Hash Function *****
*
* This function takes an index, the length of the thing the index points
* into, and returns a shuffled index. For example, if you pass indices
* 0 through 19 to this function with a length parameter of 20, it will
* return the indices in a shuffled order with no repeats. Indices
* larger than the length parameter will simply repeat the same shuffled
* pattern over and over.
*
* This is useful for iterating over an array in random shuffled order
* without having to shuffle the array itself.
*
* Passing different seeds results in different random shuffles.
*
* This function runs in average O(1) time.
*
* See https://andrew-helmer.github.io/permute/ for details on how this
* works.
*/
ccl_device_inline uint hash_shuffle_uint(uint i, const uint length, const uint seed)
{
i = i % length;
const uint mask = (1 << (32 - count_leading_zeros(length - 1))) - 1;
do {
i ^= seed;
i *= 0xe170893d;
i ^= seed >> 16;
i ^= (i & mask) >> 4;
i ^= seed >> 8;
i *= 0x0929eb3f;
i ^= seed >> 23;
i ^= (i & mask) >> 1;
i *= 1 | seed >> 27;
i *= 0x6935fa69;
i ^= (i & mask) >> 11;
i *= 0x74dcb303;
i ^= (i & mask) >> 2;
i *= 0x9e501cc3;
i ^= (i & mask) >> 2;
i *= 0xc860a3df;
i &= mask;
i ^= i >> 5;
} while (i >= length);
return i;
}
/**
* 2D hash recommended from "Hash Functions for GPU Rendering" JCGT Vol. 9, No. 3, 2020
* See https://www.shadertoy.com/view/4tXyWN and https://www.shadertoy.com/view/XlGcRh
* http://www.jcgt.org/published/0009/03/02/paper.pdf
*/
ccl_device_inline uint hash_iqnt2d(const uint x, const uint y)
{
const uint qx = 1103515245U * ((x >> 1U) ^ (y));
const uint qy = 1103515245U * ((y >> 1U) ^ (x));
const uint n = 1103515245U * ((qx) ^ (qy >> 3U));
return n;
}
/* ********** */
#ifndef __KERNEL_GPU__
static inline uint hash_string(const char *str)
{
uint i = 0;
uint c;
while ((c = *str++)) {
i = i * 37 + c;
}
return i;
}
#endif
CCL_NAMESPACE_END

View File

@@ -0,0 +1,409 @@
/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#include <algorithm>
#include "util/ies.h"
#include "util/math.h"
#include "util/string.h"
CCL_NAMESPACE_BEGIN
// NOTE: For some reason gcc-7.2 does not instantiate this version of the
// allocator here (used in IESTextParser). Works fine for gcc-6, gcc-7.3 and gcc-8.
//
// TODO(sergey): Get to the root of this issue, or confirm this is a compiler
// issue.
template class GuardedAllocator<char>;
bool IESFile::load(const string &ies)
{
clear();
if (!parse(ies) || !process()) {
clear();
return false;
}
return true;
}
void IESFile::clear()
{
intensity.clear();
v_angles.clear();
h_angles.clear();
}
int IESFile::packed_size()
{
if (!v_angles.empty() && !h_angles.empty()) {
return 2 + h_angles.size() + v_angles.size() + h_angles.size() * v_angles.size();
}
return 0;
}
void IESFile::pack(float *data)
{
if (!v_angles.empty() && !h_angles.empty()) {
*(data++) = __int_as_float(h_angles.size());
*(data++) = __int_as_float(v_angles.size());
memcpy(data, h_angles.data(), h_angles.size() * sizeof(float));
data += h_angles.size();
memcpy(data, v_angles.data(), v_angles.size() * sizeof(float));
data += v_angles.size();
for (int h = 0; h < intensity.size(); h++) {
memcpy(data, intensity[h].data(), v_angles.size() * sizeof(float));
data += v_angles.size();
}
}
}
class IESTextParser {
public:
string text;
char *data;
bool error = false;
IESTextParser(const string &str) : text(str)
{
std::replace(text.begin(), text.end(), ',', ' ');
data = strstr(text.data(), "\nTILT=");
}
bool eof()
{
return (data == nullptr) || (data[0] == '\0');
}
bool has_error()
{
return error;
}
double get_double()
{
if (eof()) {
error = true;
return 0.0;
}
char *old_data = data;
const double val = strtod(data, &data);
if (data == old_data) {
data = nullptr;
error = true;
return 0.0;
}
return val;
}
long get_long()
{
if (eof()) {
error = true;
return 0;
}
char *old_data = data;
const long val = strtol(data, &data, 10);
if (data == old_data) {
data = nullptr;
error = true;
return 0;
}
return val;
}
};
bool IESFile::parse(const string &ies)
{
if (ies.empty()) {
return false;
}
IESTextParser parser(ies);
if (parser.eof()) {
return false;
}
/* Handle the tilt data block. */
if (strncmp(parser.data, "\nTILT=INCLUDE", 13) == 0) {
parser.data += 13;
parser.get_double(); /* Lamp to Luminaire geometry */
const int num_tilt = parser.get_long(); /* Amount of tilt angles and factors */
/* Skip over angles and factors. */
for (int i = 0; i < 2 * num_tilt; i++) {
parser.get_double();
}
}
else {
/* Skip to next line. */
parser.data = strstr(parser.data + 1, "\n");
}
if (parser.eof()) {
return false;
}
parser.data++;
parser.get_long(); /* Number of lamps */
parser.get_double(); /* Lumens per lamp */
double factor = parser.get_double(); /* Candela multiplier */
const int v_angles_num = parser.get_long(); /* Number of vertical angles */
const int h_angles_num = parser.get_long(); /* Number of horizontal angles */
type = (IESType)parser.get_long(); /* Photometric type */
if (type != TYPE_A && type != TYPE_B && type != TYPE_C) {
return false;
}
parser.get_long(); /* Unit of the geometry data */
parser.get_double(); /* Width */
parser.get_double(); /* Length */
parser.get_double(); /* Height */
factor *= parser.get_double(); /* Ballast factor */
factor *= parser.get_double(); /* Ballast-Lamp Photometric factor */
parser.get_double(); /* Input Watts */
/* Intensity values in IES files are specified in candela (lumen/sr), a photometric quantity.
* Cycles expects radiometric quantities, though, which requires a conversion.
* However, the Luminous efficacy (ratio of lumens per Watt) depends on the spectral distribution
* of the light source since lumens take human perception into account.
* Since this spectral distribution is not known from the IES file, a typical one must be
* assumed. The D65 standard illuminant has a Luminous efficacy of 177.83, which is used here to
* convert to Watt/sr. A more advanced approach would be to add a Blackbody Temperature input to
* the node and numerically integrate the Luminous efficacy from the resulting spectral
* distribution. Also, the Watt/sr value must be multiplied by 4*pi to get the Watt value that
* Cycles expects for lamp strength. Therefore, the conversion here uses 4*pi/177.83 as a Candela
* to Watt factor.
*/
factor *= 0.0706650768394;
v_angles.reserve(v_angles_num);
for (int i = 0; i < v_angles_num; i++) {
v_angles.push_back((float)parser.get_double());
}
h_angles.reserve(h_angles_num);
for (int i = 0; i < h_angles_num; i++) {
h_angles.push_back((float)parser.get_double());
}
intensity.resize(h_angles_num);
for (int i = 0; i < h_angles_num; i++) {
intensity[i].reserve(v_angles_num);
for (int j = 0; j < v_angles_num; j++) {
intensity[i].push_back((float)(factor * parser.get_double()));
}
}
return !parser.has_error();
}
static bool angle_close(const float a, const float b)
{
return fabsf(a - b) < 1e-4f;
}
/* Processing functions to turn file contents into the format that Cycles expects.
* Handles type conversion (the output format is based on Type C), symmetry/mirroring,
* value shifting etc.
* Note that this code is much more forgiving than the spec. For example, in type A and B,
* the range of vertical angles officially must be either exactly 0°-90° or -90°-90°.
* However, in practice, IES files are all over the place. Therefore, the handling is as
* flexible as possible, and tries to turn any input into something useful. */
void IESFile::process_type_b()
{
/* According to the standard, Type B defines a different coordinate system where the polar axis
* is horizontal, not vertical.
* To avoid over complicating the conversion logic, we just transpose the angles and use the
* regular Type A/C coordinate system. Users can just rotate the light to get the "proper"
* orientation. */
vector<vector<float>> newintensity;
newintensity.resize(v_angles.size());
for (int i = 0; i < v_angles.size(); i++) {
newintensity[i].reserve(h_angles.size());
for (int j = 0; j < h_angles.size(); j++) {
newintensity[i].push_back(intensity[j][i]);
}
}
intensity.swap(newintensity);
h_angles.swap(v_angles);
if (angle_close(h_angles[0], 0.0f)) {
/* File angles cover 0°-90°. Mirror that to -90°-90°, and shift to 0°-180° to match Cycles. */
vector<float> new_h_angles;
vector<vector<float>> new_intensity;
const int hnum = h_angles.size();
new_h_angles.reserve(2 * hnum - 1);
new_intensity.reserve(2 * hnum - 1);
for (int i = hnum - 1; i > 0; i--) {
new_h_angles.push_back(90.0f - h_angles[i]);
new_intensity.push_back(intensity[i]);
}
for (int i = 0; i < hnum; i++) {
new_h_angles.push_back(90.0f + h_angles[i]);
new_intensity.push_back(intensity[i]);
}
h_angles.swap(new_h_angles);
intensity.swap(new_intensity);
}
else {
/* File angles cover -90°-90°. Shift to 0°-180° to match Cycles. */
for (int i = 0; i < h_angles.size(); i++) {
h_angles[i] += 90.0f;
}
}
if (angle_close(v_angles[0], 0.0f)) {
/* File angles cover 0°-90°. Mirror that to -90°-90°, and shift to 0°-180° to match Cycles. */
vector<float> new_v_angles;
const int hnum = h_angles.size();
const int vnum = v_angles.size();
new_v_angles.reserve(2 * vnum - 1);
for (int i = vnum - 1; i > 0; i--) {
new_v_angles.push_back(90.0f - v_angles[i]);
}
for (int i = 0; i < vnum; i++) {
new_v_angles.push_back(90.0f + v_angles[i]);
}
for (int i = 0; i < hnum; i++) {
vector<float> new_intensity;
new_intensity.reserve(2 * vnum - 1);
for (int j = vnum - 1; j > 0; j--) {
new_intensity.push_back(intensity[i][j]);
}
new_intensity.insert(new_intensity.end(), intensity[i].begin(), intensity[i].end());
intensity[i].swap(new_intensity);
}
v_angles.swap(new_v_angles);
}
else {
/* File angles cover -90°-90°. Shift to 0°-180° to match Cycles. */
for (int i = 0; i < v_angles.size(); i++) {
v_angles[i] += 90.0f;
}
}
}
void IESFile::process_type_a()
{
/* Convert vertical angles - just a simple offset. */
for (int i = 0; i < v_angles.size(); i++) {
v_angles[i] += 90.0f;
}
vector<float> new_h_angles;
new_h_angles.reserve(h_angles.size());
vector<vector<float>> new_intensity;
new_intensity.reserve(h_angles.size());
/* Type A goes from -90° to 90°, which is mapped to 270° to 90° respectively in Type C. */
for (int i = h_angles.size() - 1; i >= 0; i--) {
new_h_angles.push_back(180.0f - h_angles[i]);
new_intensity.push_back(intensity[i]);
}
/* If the file angles start at 0°, we need to mirror around that.
* Since the negative input range (which we generate here) maps to 180° to 270°,
* it comes after the original entries in the output. */
if (angle_close(h_angles[0], 0.0f)) {
new_h_angles.reserve(2 * h_angles.size() - 1);
new_intensity.reserve(2 * h_angles.size() - 1);
for (int i = 1; i < h_angles.size(); i++) {
new_h_angles.push_back(180.0f + h_angles[i]);
new_intensity.push_back(intensity[i]);
}
}
h_angles.swap(new_h_angles);
intensity.swap(new_intensity);
}
void IESFile::process_type_c()
{
if (angle_close(h_angles[0], 90.0f)) {
/* Some files are stored from 90° to 270°, so rotate them to the regular 0°-180° range. */
for (int i = 0; i < h_angles.size(); i++) {
h_angles[i] -= 90.0f;
}
}
if (h_angles.size() == 1) {
h_angles[0] = 0.0f;
h_angles.push_back(360.0f);
intensity.push_back(intensity[0]);
}
if (angle_close(h_angles[h_angles.size() - 1], 90.0f)) {
/* Only one quadrant is defined, so we need to mirror twice (from one to two, then to four).
* Since the two->four mirroring step might also be required if we get an input of two
* quadrants, we only do the first mirror here and later do the second mirror in either case.
*/
const int hnum = h_angles.size();
for (int i = hnum - 2; i >= 0; i--) {
h_angles.push_back(180.0f - h_angles[i]);
intensity.push_back(intensity[i]);
}
}
if (angle_close(h_angles[h_angles.size() - 1], 180.0f)) {
/* Mirror half to the full range. */
const int hnum = h_angles.size();
for (int i = hnum - 2; i >= 0; i--) {
h_angles.push_back(360.0f - h_angles[i]);
intensity.push_back(intensity[i]);
}
}
/* Some files skip the 360° entry (contrary to standard) because it's supposed to be identical to
* the 0° entry. If the file has a discernible order in its spacing, just fix this. */
if (angle_close(h_angles[0], 0.0f) && !angle_close(h_angles[h_angles.size() - 1], 360.0f)) {
const int hnum = h_angles.size();
const float last_step = h_angles[hnum - 1] - h_angles[hnum - 2];
const float first_step = h_angles[1] - h_angles[0];
const float gap_step = 360.0f - h_angles[hnum - 1];
if (angle_close(last_step, gap_step) || angle_close(first_step, gap_step)) {
h_angles.push_back(360.0f);
intensity.push_back(intensity[0]);
}
}
}
bool IESFile::process()
{
if (h_angles.empty() || v_angles.empty()) {
return false;
}
if (type == TYPE_A) {
process_type_a();
}
else if (type == TYPE_B) {
process_type_b();
}
else if (type == TYPE_C) {
process_type_c();
}
else {
return false;
}
/* Convert from deg to rad. */
for (int i = 0; i < v_angles.size(); i++) {
v_angles[i] *= M_PI_F / 180.f;
}
for (int i = 0; i < h_angles.size(); i++) {
h_angles[i] *= M_PI_F / 180.f;
}
return true;
}
IESFile::~IESFile()
{
clear();
}
CCL_NAMESPACE_END

View File

@@ -0,0 +1,43 @@
/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#pragma once
#include "util/string.h"
#include "util/vector.h"
CCL_NAMESPACE_BEGIN
class IESFile {
public:
IESFile() = default;
~IESFile();
int packed_size();
void pack(float *data);
bool load(const string &ies);
void clear();
protected:
bool parse(const string &ies);
bool process();
void process_type_a();
void process_type_b();
void process_type_c();
/* The brightness distribution is stored in spherical coordinates.
* The horizontal angles correspond to theta in the regular notation
* and always span the full range from 0° to 360°.
* The vertical angles correspond to phi and always start at 0°. */
vector<float> v_angles, h_angles;
/* The actual values are stored here, with every entry storing the values
* of one horizontal segment. */
vector<vector<float>> intensity;
/* Types of angle representation in IES files. */
enum IESType { TYPE_A = 3, TYPE_B = 2, TYPE_C = 1 } type;
};
CCL_NAMESPACE_END

View File

@@ -0,0 +1,124 @@
/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#pragma once
/* OpenImageIO is used for all image file reading and writing. */
#include <OpenImageIO/imageio.h>
#include "util/half.h"
#include "util/vector.h"
CCL_NAMESPACE_BEGIN
using OIIO::AutoStride;
using OIIO::ImageInput;
using OIIO::ImageOutput;
using OIIO::ImageSpec;
template<typename T>
void util_image_resize_pixels(const vector<T> &input_pixels,
const int64_t input_width,
const int64_t input_height,
const int64_t components,
vector<T> *output_pixels,
int64_t *output_width,
int64_t *output_height);
/* Cast input pixel from unknown storage to float. */
template<typename T> inline float util_image_cast_to_float(T value);
template<> inline float util_image_cast_to_float(const float value)
{
return value;
}
template<> inline float util_image_cast_to_float(const uchar value)
{
return (float)value / 255.0f;
}
template<> inline float util_image_cast_to_float(const uint16_t value)
{
return (float)value / 65535.0f;
}
template<> inline float util_image_cast_to_float(half value)
{
return half_to_float_image(value);
}
/* Cast float value to output pixel type. */
template<typename T> inline T util_image_cast_from_float(const float value);
template<> inline float util_image_cast_from_float(const float value)
{
return value;
}
template<> inline uchar util_image_cast_from_float(const float value)
{
if (value < 0.0f) {
return 0;
}
if (value > (1.0f - 0.5f / 255.0f)) {
return 255;
}
return (uchar)((255.0f * value) + 0.5f); // NOLINT
}
template<> inline uint16_t util_image_cast_from_float(const float value)
{
if (value < 0.0f) {
return 0;
}
if (value > (1.0f - 0.5f / 65535.0f)) {
return 65535;
}
return (uint16_t)((65535.0f * value) + 0.5f); // NOLINT
}
template<> inline half util_image_cast_from_float(const float value)
{
return float_to_half_image(value);
}
/* Multiply image pixels in native data format. */
template<typename T> inline T util_image_multiply_native(T a, T b);
template<> inline float util_image_multiply_native(const float a, const float b)
{
return a * b;
}
template<> inline uchar util_image_multiply_native(const uchar a, const uchar b)
{
return ((uint32_t)a * (uint32_t)b) / 255;
}
template<> inline uint16_t util_image_multiply_native(const uint16_t a, const uint16_t b)
{
return ((uint32_t)a * (uint32_t)b) / 65535;
}
template<> inline half util_image_multiply_native(half a, half b)
{
return float_to_half_image(half_to_float_image(a) * half_to_float_image(b));
}
/* Test if value is finite, in native data format. */
template<typename T> inline T util_image_is_finite(T a);
template<> inline float util_image_is_finite(const float a)
{
return isfinite(a);
}
template<> inline uchar util_image_is_finite(const uchar /*a*/)
{
return true;
}
template<> inline uint16_t util_image_is_finite(const uint16_t /*a*/)
{
return true;
}
template<> inline half util_image_is_finite(half a)
{
return half_is_finite(a);
}
CCL_NAMESPACE_END
#include "util/image_impl.h" // IWYU pragma: export

View File

@@ -0,0 +1,143 @@
/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#pragma once
#include <algorithm>
#include "util/image.h"
#include "util/types.h"
CCL_NAMESPACE_BEGIN
namespace {
template<typename T>
const T *util_image_read(const vector<T> &pixels,
const int64_t width,
const int64_t /*height*/,
const int64_t components,
const int64_t x,
const int64_t y)
{
const int64_t index = ((int64_t)y * width + (int64_t)x) * components;
return &pixels[index];
}
template<typename T>
void util_image_downscale_sample(const vector<T> &pixels,
const int64_t width,
const int64_t height,
const int64_t components,
const int64_t kernel_size,
const float x,
const float y,
T *result)
{
assert(components <= 4);
const int64_t ix = (int64_t)x;
const int64_t iy = (int64_t)y;
/* TODO(sergey): Support something smarter than box filer. */
float accum[4] = {0};
int64_t count = 0;
for (int64_t dy = 0; dy < kernel_size; ++dy) {
for (int64_t dx = 0; dx < kernel_size; ++dx) {
const int64_t nx = ix + dx;
const int64_t ny = iy + dy;
if (nx >= width || ny >= height) {
continue;
}
const T *pixel = util_image_read(pixels, width, height, components, nx, ny);
for (size_t k = 0; k < components; ++k) {
accum[k] += util_image_cast_to_float(pixel[k]);
}
++count;
}
}
if (count != 0) {
const float inv_count = 1.0f / (float)count;
for (int64_t k = 0; k < components; ++k) {
result[k] = util_image_cast_from_float<T>(accum[k] * inv_count);
}
}
else {
for (int64_t k = 0; k < components; ++k) {
result[k] = T(0.0f);
}
}
}
template<typename T>
void util_image_downscale_pixels(const vector<T> &input_pixels,
const int64_t input_width,
const int64_t input_height,
const int64_t components,
const float inv_scale_factor,
const int64_t output_width,
const int64_t output_height,
vector<T> *output_pixels)
{
const int64_t kernel_size = (int64_t)(inv_scale_factor + 0.5f);
for (int64_t y = 0; y < output_height; ++y) {
for (int64_t x = 0; x < output_width; ++x) {
const float input_x = (float)x * inv_scale_factor;
const float input_y = (float)y * inv_scale_factor;
const int64_t output_index = (y * output_width + x) * components;
util_image_downscale_sample(input_pixels,
input_width,
input_height,
components,
kernel_size,
input_x,
input_y,
&output_pixels->at(output_index));
}
}
}
} /* namespace */
template<typename T>
void util_image_resize_pixels(const vector<T> &input_pixels,
const int64_t input_width,
const int64_t input_height,
const int64_t components,
const float scale_factor,
vector<T> *output_pixels,
int64_t *output_width,
int64_t *output_height)
{
/* Early output for case when no scaling is applied. */
if (scale_factor == 1.0f) {
*output_width = input_width;
*output_height = input_height;
*output_pixels = input_pixels;
return;
}
/* First of all, we calculate output image dimensions.
* We clamp them to be 1 pixel at least so we do not generate degenerate
* image.
*/
*output_width = std::max((int64_t)((float)input_width * scale_factor), (int64_t)1);
*output_height = std::max((int64_t)((float)input_height * scale_factor), (int64_t)1);
/* Prepare pixel storage for the result. */
const int64_t num_output_pixels = ((*output_width) * (*output_height)) * components;
output_pixels->resize(num_output_pixels);
if (scale_factor < 1.0f) {
const float inv_scale_factor = 1.0f / scale_factor;
util_image_downscale_pixels(input_pixels,
input_width,
input_height,
components,
inv_scale_factor,
*output_width,
*output_height,
output_pixels);
}
else {
/* TODO(sergey): Needs implementation. */
}
}
CCL_NAMESPACE_END

View File

@@ -0,0 +1,908 @@
/* SPDX-FileCopyrightText: 2025 OpenImageIO project
* 2026 Blender Authors
*
* SPDX-License-Identifier: Apache-2.0
*
* This is a modified version of maketexture.cpp from OpenImageIO, to add a few
* features missing in the native implementation. */
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <memory>
#include <fmt/format.h>
#include <OpenImageIO/Imath.h>
#include <OpenImageIO/color.h>
#include <OpenImageIO/filesystem.h>
#include <OpenImageIO/filter.h>
#include <OpenImageIO/fmath.h>
#include <OpenImageIO/imagebuf.h>
#include <OpenImageIO/imagebufalgo.h>
#include <OpenImageIO/imagebufalgo_util.h>
#include <OpenImageIO/imageio.h>
#include <OpenImageIO/string_view.h>
#include <OpenImageIO/strutil.h>
#include <OpenImageIO/sysutil.h>
#include <OpenImageIO/thread.h>
#include "util/color.h"
#include "util/colorspace.h"
#include "util/image.h"
#include "util/image_maketx.h"
#include "util/image_metadata.h"
#include "util/log.h"
#include "util/md5.h"
#include "util/path.h"
#include "util/tbb.h"
#include "util/types_image.h"
CCL_NAMESPACE_BEGIN
static std::string datestring(time_t t)
{
struct tm mytm;
OIIO::Sysutil::get_local_time(&t, &mytm);
return fmt::format("{:4d}:{:02d}:{:02d} {:02d}:{:02d}:{:02d}",
mytm.tm_year + 1900,
mytm.tm_mon + 1,
mytm.tm_mday,
mytm.tm_hour,
mytm.tm_min,
mytm.tm_sec);
}
template<class SRCTYPE>
static void interppixel_NDC(const OIIO::ImageBuf &buf,
float x,
float y,
OIIO::span<float> pixel,
bool envlatlmode,
OIIO::ImageBuf::ConstIterator<SRCTYPE> &it,
OIIO::ImageBuf::WrapMode wrapmode)
{
const ImageSpec &spec(buf.spec());
int fx = spec.x;
int fy = spec.y;
int fw = spec.width;
int fh = spec.height;
x = static_cast<float>(fx) + x * static_cast<float>(fw);
y = static_cast<float>(fy) + y * static_cast<float>(fh);
int n = spec.nchannels;
float *p0 = OIIO_ALLOCA(float, 4 * n), *p1 = p0 + n, *p2 = p1 + n, *p3 = p2 + n;
x -= 0.5f;
y -= 0.5f;
int xtexel, ytexel;
float xfrac, yfrac;
xfrac = floorfrac(x, &xtexel);
yfrac = floorfrac(y, &ytexel);
/* Get the four texels */
it.rerange(xtexel, xtexel + 2, ytexel, ytexel + 2, 0, 1, wrapmode);
for (int c = 0; c < n; ++c) {
p0[c] = it[c];
}
++it;
for (int c = 0; c < n; ++c) {
p1[c] = it[c];
}
++it;
for (int c = 0; c < n; ++c) {
p2[c] = it[c];
}
++it;
for (int c = 0; c < n; ++c) {
p3[c] = it[c];
}
if (envlatlmode) {
/* For latlong environment maps, in order to conserve energy, we must weight the pixels by
* sin(t*PI) because pixels closer to the pole are actually less area on the sphere. Doing
* this wrong will tend to over-represent the high latitudes in low-res MIP levels. We fold
* the area weighting into our linear interpolation by adjusting yfrac. */
int ymin = spec.y;
int ymax = ymin + spec.height - 1;
int ynext = OIIO::clamp(ytexel + 1, ymin, ymax);
ytexel = OIIO::clamp(ytexel, ymin, ymax);
float w0 = (1.0f - yfrac) * sinf((float)M_PI * (ytexel + 0.5f) / (float)fh);
float w1 = yfrac * sinf((float)M_PI * (ynext + 0.5f) / (float)fh);
yfrac = w1 / (w0 + w1);
}
/* Bilinearly interpolate. */
OIIO::bilerp(p0, p1, p2, p3, xfrac, yfrac, n, pixel.data());
}
/* Resize src into dst, relying on the linear interpolation of
* interppixel_NDC_full or interppixel_NDC_clamped, for the pixel range. */
template<class SRCTYPE>
static bool resize_block_(OIIO::ImageBuf &dst,
const OIIO::ImageBuf &src,
OIIO::ROI roi,
bool envlatlmode)
{
int x0 = roi.xbegin, x1 = roi.xend, y0 = roi.ybegin, y1 = roi.yend;
const ImageSpec &dstspec(dst.spec());
OIIO::span<float> pel = OIIO::OIIO_ALLOCA_SPAN(float, dstspec.nchannels);
float xoffset = (float)dstspec.x;
float yoffset = (float)dstspec.y;
float xscale = 1.0f / (float)dstspec.width;
float yscale = 1.0f / (float)dstspec.height;
int nchannels = dst.nchannels();
assert(dst.spec().format == TypeFloat);
OIIO::ImageBuf::WrapMode wrapmode = OIIO::ImageBuf::WrapClamp;
OIIO::ImageBuf::ConstIterator<SRCTYPE> srcit(src);
OIIO::ImageBuf::Iterator<float> d(dst, roi);
for (int y = y0; y < y1; ++y) {
float t = (y + 0.5f) * yscale + yoffset;
for (int x = x0; x < x1; ++x, ++d) {
float s = (x + 0.5f) * xscale + xoffset;
interppixel_NDC<SRCTYPE>(src, s, t, pel, envlatlmode, srcit, wrapmode);
for (int c = 0; c < nchannels; ++c) {
d[c] = pel[c];
}
}
}
return true;
}
/* Helper function to compute the first bilerp pass into a scanline buffer. */
template<class SRCTYPE>
static void halve_scanline(const SRCTYPE *s, const int nchannels, size_t sw, float *dst)
{
for (size_t i = 0; i < sw; i += 2, s += nchannels) {
for (int j = 0; j < nchannels; ++j, ++dst, ++s) {
*dst = 0.5f * (float)(*s + *(s + nchannels));
}
}
}
/* Bilinear resize performed as a 2-pass filter.
* Optimized to assume that the images are contiguous. */
template<class SRCTYPE>
static bool resize_block_2pass(OIIO::ImageBuf &dst, const OIIO::ImageBuf &src, OIIO::ROI roi)
{
/* Two-pass filtering introduces a half-pixel shift for odd resolutions.
* Revert to correct bilerp sampling. */
if (src.spec().width % 2 || src.spec().height % 2) {
return resize_block_<SRCTYPE>(dst, src, roi, false);
}
assert(roi.ybegin + roi.height() <= dst.spec().height);
/* Allocate two scanline buffers to hold the result of the first pass. */
const int nchannels = dst.nchannels();
const size_t row_elem = roi.width() * nchannels; /* # floats in scanline */
std::unique_ptr<float[]> S0(new float[row_elem]);
std::unique_ptr<float[]> S1(new float[row_elem]);
/* We know that the buffers created for mipmapping are all contiguous,
* so we can skip the iterators for a bilerp resize entirely along with
* any NDC -> pixel math, and just directly traverse pixels. */
const SRCTYPE *s = (const SRCTYPE *)src.localpixels();
SRCTYPE *d = (SRCTYPE *)dst.localpixels();
assert(s && d); /* Assume contig bufs */
d += roi.ybegin * dst.spec().width * nchannels; /* Top of dst OIIO::ROI */
const size_t ystride = src.spec().width * nchannels; /* Scanline offset */
s += 2 * roi.ybegin * ystride; /* Top of src OIIO::ROI */
/* Run through destination rows, doing the two-pass bilerp filter. */
const size_t dw = roi.width(), dh = roi.height(); /* Loop invariants */
const size_t sw = dw * 2; /* Handle odd res */
for (size_t y = 0; y < dh; ++y) { /* For each dst OIIO::ROI row */
halve_scanline<SRCTYPE>(s, nchannels, sw, &S0[0]);
s += ystride;
halve_scanline<SRCTYPE>(s, nchannels, sw, &S1[0]);
s += ystride;
const float *s0 = &S0[0], *s1 = &S1[0];
for (size_t x = 0; x < dw; ++x) { /* For each dst OIIO::ROI col */
for (int i = 0; i < nchannels; ++i, ++s0, ++s1, ++d) {
*d = (SRCTYPE)(0.5f * (*s0 + *s1)); /* Average vertically */
}
}
}
return true;
}
template<typename SRCTYPE>
static bool resize_block(OIIO::ImageBuf &dst,
const OIIO::ImageBuf &src,
OIIO::ROI roi,
bool envlatlmode)
{
const ImageSpec &srcspec(src.spec());
const ImageSpec &dstspec(dst.spec());
assert(dstspec.nchannels == srcspec.nchannels);
assert(dst.localpixels());
bool ok;
if (src.localpixels() && /* Not a cached image */
!envlatlmode && /* not latlong wrap mode */
roi.xbegin == 0 && /* Region x at origin */
dstspec.width == roi.width() && /* Full width OIIO::ROI */
(2 * dstspec.width) == srcspec.width && /* Src is 2x resize */
dstspec.format == srcspec.format && /* Same formats */
dstspec.x == 0 && dstspec.y == 0 && /* Not a crop or overscan */
srcspec.x == 0 && srcspec.y == 0)
{
/* If all these conditions are met, we have a special case that
* can be more highly optimized. */
ok = resize_block_2pass<SRCTYPE>(dst, src, roi);
}
else {
assert(dst.spec().format == TypeFloat);
ok = resize_block_<SRCTYPE>(dst, src, roi, envlatlmode);
}
return ok;
}
static void fix_latl_edges(OIIO::ImageBuf &buf)
{
int n = buf.nchannels();
OIIO::span<float> left = OIIO::OIIO_ALLOCA_SPAN(float, n);
OIIO::span<float> right = OIIO::OIIO_ALLOCA_SPAN(float, n);
/* Make the whole first and last row be solid, since they are exactly on the pole. */
float wscale = 1.0f / (buf.spec().width);
for (int j = 0; j <= 1; ++j) {
int y = (j == 0) ? buf.ybegin() : buf.yend() - 1;
/* Use left for the sum, right for each new pixel. */
for (int c = 0; c < n; ++c) {
left[c] = 0.0f;
}
for (int x = buf.xbegin(); x < buf.xend(); ++x) {
buf.getpixel(x, y, right);
for (int c = 0; c < n; ++c) {
left[c] += right[c];
}
}
for (int c = 0; c < n; ++c) {
left[c] *= wscale;
}
for (int x = buf.xbegin(); x < buf.xend(); ++x) {
buf.setpixel(x, y, left);
}
}
/* Make the left and right match, since they are both right on the prime meridian. */
for (int y = buf.ybegin(); y < buf.yend(); ++y) {
buf.getpixel(buf.xbegin(), y, left);
buf.getpixel(buf.xend() - 1, y, right);
for (int c = 0; c < n; ++c) {
left[c] = 0.5f * left[c] + 0.5f * right[c];
}
buf.setpixel(buf.xbegin(), y, left);
buf.setpixel(buf.xend() - 1, y, left);
}
}
static bool texture_cache_file_outdated(const string &filepath, const string &tx_filepath)
{
if (!path_is_file(tx_filepath)) {
return true;
}
std::time_t in_time = OIIO::Filesystem::last_write_time(filepath);
std::time_t out_time = OIIO::Filesystem::last_write_time(tx_filepath);
if (in_time == out_time) {
LOG_INFO << "Using texture cache file: " << tx_filepath;
return false;
}
LOG_INFO << "Texture cache file is outdated: " << tx_filepath;
return true;
}
static std::string alpha_type_string(const ImageAlphaType alpha_type)
{
switch (alpha_type) {
case IMAGE_ALPHA_ASSOCIATED:
return "associated";
break;
case IMAGE_ALPHA_UNASSOCIATED:
return "unassociated";
break;
case IMAGE_ALPHA_CHANNEL_PACKED:
return "channel_packed";
break;
case IMAGE_ALPHA_IGNORE:
return "ignore";
break;
case IMAGE_ALPHA_AUTO:
return "auto";
case IMAGE_ALPHA_NUM_TYPES:
break;
}
assert(!"Unknown tx alpha type");
return "auto";
}
static std::string format_tyoe_string(const ImageFormatType format_type)
{
switch (format_type) {
case IMAGE_FORMAT_PLAIN:
return "plain";
case IMAGE_FORMAT_EQUIANGULAR:
return "equiangular";
}
assert(!"Unknown tx format type");
return "unknown";
}
static std::string unique_filename_tx(const string &filepath,
const string &texture_cache_path,
ustring colorspace,
const ImageAlphaType alpha_type,
const ImageFormatType format_type,
const bool absolute_texture_cache)
{
/* NOTE: Be careful not to change existing hashes if at all possible, as this
* will cause all texture files to be regenerated with significantly increased
* disk usage. */
MD5Hash md5;
/* Scene linear colorspace that we may be converting to. */
md5.append("xyz_to_scene_linear:" + ColorSpaceManager::get_xyz_to_scene_linear_rgb_string());
/* Colorspace. */
md5.append("colorspace:" + (ColorSpaceManager::colorspace_is_data(colorspace) ?
u_colorspace_data.string() :
colorspace.string()));
md5.append("alpha:" + alpha_type_string(alpha_type));
md5.append("format:" + format_tyoe_string(format_type));
/* For absolute texture cache path, include the full file path. This requires
* a matching directory structure though. */
/* TODO: Can we use a relative path? The problem is that we can not automatically
* distinguish between cases like /tmp/texture_cache and /projects/X/texture_cache
* where the former is not helped by a relative path, while the latter can be. */
if (absolute_texture_cache) {
md5.append("filepath:" + path_normalize(path_make_relative(filepath, texture_cache_path)));
}
const std::string version = string_printf(".%d-", TX_FILE_FORMAT_VERSION);
return path_filename(filepath) + version + md5.get_hex() + ".tx";
}
bool resolve_tx(const string &filepath,
const string &texture_cache_path,
ustring colorspace,
const ImageAlphaType alpha_type,
const ImageFormatType format_type,
string &out_filepath,
ImageMetaData &out_metadata)
{
/* Nothing to do if file doesn't even exist. */
if (!path_is_file(filepath)) {
return false;
}
const string filedir = path_dirname(filepath);
/* Check the specified directory if one is given. */
if (!texture_cache_path.empty()) {
const bool absolute_cache_path = !path_is_relative(texture_cache_path);
const string tx_filename = unique_filename_tx(
filepath, texture_cache_path, colorspace, alpha_type, format_type, absolute_cache_path);
const string tx_filepath = path_join(
absolute_cache_path ? string(texture_cache_path) : path_join(filedir, texture_cache_path),
tx_filename);
out_filepath = tx_filepath;
if (!texture_cache_file_outdated(filepath, tx_filepath)) {
if (out_metadata.oiio_load_metadata(tx_filepath) && out_metadata.is_tx_file) {
return true;
}
}
}
/* Check in the default location. */
const char *default_texture_cache_dir = "blender_tx";
if (texture_cache_path != default_texture_cache_dir) {
const string tx_filename = unique_filename_tx(
filepath, texture_cache_path, colorspace, alpha_type, format_type, false);
const string tx_default_filepath = path_join(path_join(filedir, default_texture_cache_dir),
tx_filename);
if (!texture_cache_file_outdated(filepath, tx_default_filepath)) {
if (out_metadata.oiio_load_metadata(tx_default_filepath) && out_metadata.is_tx_file) {
out_filepath = tx_default_filepath;
return true;
}
}
if (texture_cache_path.empty()) {
out_filepath = tx_default_filepath;
}
}
/* If it's already a tx, tiff or exr file, we can use it directly as well. But it's
* preferable to use a Cycles native tx file for performance. */
if (out_metadata.oiio_load_metadata(filepath) && out_metadata.is_tx_file) {
out_filepath = filepath;
return true;
}
return false;
}
static void write_stats_tx(OIIO::ImageBuf &buf, const bool use_openexr)
{
OIIO::ImageBufAlgo::PixelStats pixel_stats = OIIO::ImageBufAlgo::computePixelStats(buf);
/* Constant color optimization. Creating small image and add metadata. */
std::vector<float> constant_color(buf.nchannels());
bool is_constant_color = (pixel_stats.min == pixel_stats.max);
if (is_constant_color) {
constant_color = pixel_stats.min;
ImageSpec spec = buf.spec();
spec.width = std::min(spec.tile_width, spec.width);
spec.height = std::min(spec.tile_height, spec.height);
spec.depth = std::min(spec.tile_depth, spec.depth);
buf.reset(spec);
OIIO::ImageBufAlgo::fill(buf, constant_color);
LOG_INFO << " Constant color image detected. Creating " << spec.width << "x" << spec.height
<< " texture instead.";
}
/* TODO: Opaque detection optimization doesn't work currently, because we
* expected tile pixels to be conformed and that means 1 or 4 channels. */
const bool is_opaque = buf.spec().alpha_channel == buf.nchannels() - 1 &&
pixel_stats.min[buf.spec().alpha_channel] == 1.0f &&
pixel_stats.max[buf.spec().alpha_channel] == 1.0f;
#if 0
if (is_opaque) {
LOG_INFO << " Alpha==1 image detected. Dropping the alpha channel.";
OIIO::ImageBuf new_buf(buf.spec());
OIIO::ImageBufAlgo::channels(new_buf,
buf,
buf.nchannels() - 1,
OIIO::cspan<int>(),
OIIO::cspan<float>(),
OIIO::cspan<std::string>(),
true);
std::swap(buf, new_buf);
}
#endif
/* Monochrome detection. */
if (is_opaque && (buf.nchannels() == 3 || buf.nchannels() == 4) &&
pixel_stats.avg[0] == pixel_stats.avg[1] && pixel_stats.avg[0] == pixel_stats.avg[2] &&
OIIO::ImageBufAlgo::isMonochrome(buf))
{
LOG_INFO << " Monochrome image detected. Converting to single channel texture.";
OIIO::ImageBuf new_buf(buf.spec());
OIIO::ImageBufAlgo::channels(new_buf,
buf,
1,
OIIO::cspan<int>(),
OIIO::cspan<float>(),
OIIO::cspan<std::string>(),
true);
new_buf.specmod().default_channel_names();
std::swap(buf, new_buf);
}
/* Image description for tiff, to store arbitrary metadata because it's not natively
* supported by this file format. */
string desc;
/* Add hash attribute. */
const int sha1_blocksize = 256;
string extra_hash_data;
std::string hash_digest = OIIO::ImageBufAlgo::computePixelHashSHA1(
buf, extra_hash_data, OIIO::ROI::All(), sha1_blocksize);
if (hash_digest.length()) {
if (use_openexr) {
buf.specmod().attribute("oiio:SHA-1", hash_digest);
}
else {
if (desc.length()) {
desc += " ";
}
desc += "oiio:SHA-1=";
desc += hash_digest;
}
LOG_DEBUG << " SHA-1: " << hash_digest;
}
/* Add constant color attribute. */
if (is_constant_color) {
std::string colstr = OIIO::Strutil::join(constant_color, ",", buf.spec().nchannels);
if (use_openexr) {
buf.specmod().attribute("oiio:ConstantColor", colstr);
}
else {
desc += fmt::format("{}oiio:ConstantColor={}", desc.length() ? " " : "", colstr);
}
LOG_DEBUG << " ConstantColor: " << colstr;
}
/* Add average color attribute. */
std::string avgstr = OIIO::Strutil::join(pixel_stats.avg, ",", buf.spec().nchannels);
if (use_openexr) {
buf.specmod().attribute("oiio:AverageColor", avgstr);
}
else {
desc += fmt::format("{}oiio:AverageColor={}", desc.length() ? " " : "", avgstr);
}
LOG_DEBUG << " AverageColor: " << avgstr;
if (!use_openexr) {
buf.specmod().attribute("ImageDescription", desc);
}
}
static void clamp_half_tx(OIIO::ImageBuf &buf, const TypeDesc out_format)
{
/* Clamp to half bounds to avoid creating inf when converting from float to half on write. */
if (out_format != TypeDesc::HALF) {
return;
}
assert(buf.spec().format == TypeFloat);
const int64_t num_values = buf.spec().width * buf.spec().height * buf.spec().nchannels;
float *pixels = static_cast<float *>(buf.localpixels());
for (int64_t i = 0; i < num_values; i++) {
pixels[i] = clamp(pixels[i], -HALF_MAX, HALF_MAX);
}
}
static void convert_srgb_tx(OIIO::ImageBuf &buf, const bool from_srgb)
{
assert(buf.spec().format == TypeFloat);
assert(buf.spec().nchannels == 1 || buf.spec().nchannels == 4);
const int64_t num_pixels = buf.spec().width * buf.spec().height;
if (buf.spec().nchannels == 1) {
float *pixels = static_cast<float *>(buf.localpixels());
if (from_srgb) {
for (int64_t i = 0; i < num_pixels; i++) {
pixels[i] = color_srgb_to_linear(pixels[i]);
}
}
else {
for (int64_t i = 0; i < num_pixels; i++) {
pixels[i] = color_linear_to_srgb(pixels[i]);
}
}
}
else {
float4 *pixels = static_cast<float4 *>(buf.localpixels());
for (int64_t i = 0; i < num_pixels; i++) {
float4 value = pixels[i];
const bool has_alpha = !(value.w <= 0.0f || value.w == 1.0f);
if (has_alpha) {
const float inv_alpha = 1.0f / value.w;
value.x *= inv_alpha;
value.y *= inv_alpha;
value.z *= inv_alpha;
}
if (from_srgb) {
value = color_srgb_to_linear_v4(value);
}
else {
value = color_linear_to_srgb_v4(value);
}
if (has_alpha) {
value.x *= value.w;
value.y *= value.w;
value.z *= value.w;
}
pixels[i] = value;
}
}
}
static bool write_buf_tx(std::unique_ptr<ImageOutput> &out,
const string &out_filepath,
const TypeDesc out_format,
const ImageFormatType format_type,
const bool compress_as_srgb,
const OIIO::ImageOutput::OpenMode mode,
OIIO::ImageBuf &buf)
{
ImageSpec out_spec = buf.spec();
out_spec.set_format(out_format);
if (format_type == IMAGE_FORMAT_EQUIANGULAR) {
fix_latl_edges(buf);
}
OIIO::ImageBuf srgb_buf;
if (compress_as_srgb) {
srgb_buf.copy(buf);
convert_srgb_tx(srgb_buf, false);
}
clamp_half_tx(buf, out_format);
OIIO::ImageBuf &write_buf = (compress_as_srgb) ? srgb_buf : buf;
if (!out->open(out_filepath, out_spec, mode)) {
LOG_WARNING << "Could not open \"" << out_filepath << "\" : " << out->geterror();
return false;
}
if (!write_buf.write(out.get())) {
LOG_WARNING << "Write failed: " << write_buf.geterror();
out->close();
return false;
}
return true;
}
static bool write_mipmap_tx(std::unique_ptr<ImageOutput> &out,
const string &out_filepath,
const TypeDesc out_format,
const ImageFormatType format_type,
const bool compress_as_srgb,
OIIO::ImageBuf &large_buf)
{
const bool envlatlmode = format_type == IMAGE_FORMAT_EQUIANGULAR;
const ImageOutput::OpenMode append_mode = out->supports("mipmap") ? ImageOutput::AppendMIPLevel :
ImageOutput::AppendSubimage;
/* Write top level. */
write_buf_tx(
out, out_filepath, out_format, format_type, false, OIIO::ImageOutput::Create, large_buf);
/* When using sRGB, convert to linear before resizing, and convert to sRGB again in write_buf_tx.
* for each mip level. */
if (compress_as_srgb) {
convert_srgb_tx(large_buf, true);
}
/* Write mip levels. */
OIIO::ImageBuf small_buf;
while (large_buf.spec().width > 1 || large_buf.spec().height > 1) {
ImageSpec small_spec = large_buf.spec();
small_spec.extra_attribs.free();
/* Halve resolution and reallocate small buffer */
if (small_spec.width > 1) {
small_spec.width /= 2;
}
if (small_spec.height > 1) {
small_spec.height /= 2;
}
small_buf.reset(small_spec);
/* TODO: Support other filters than box? If we do this, be sure to include them in the
* hash like OIIO maketx does. */
/* TODO: Support highlight compensation like OIIO? */
const int rows_per_task = divide_up(16384, small_spec.width);
parallel_for(blocked_range<int64_t>(0, small_spec.height, rows_per_task),
[&](const blocked_range<int64_t> &r) {
OIIO::ROI roi(0, small_spec.width, r.begin(), r.end());
/* Always float for now. */
assert(small_buf.spec().format == TypeFloat);
resize_block<float>(small_buf, large_buf, roi, envlatlmode);
});
write_buf_tx(
out, out_filepath, out_format, format_type, compress_as_srgb, append_mode, small_buf);
std::swap(large_buf, small_buf);
}
if (!out->close()) {
LOG_WARNING << "Error writing \"" << out_filepath << "\" : " << out->geterror();
return false;
}
return true;
}
ustring make_tx_get_file_colorspace(const ImageMetaData &metadata)
{
/* Tx files are written in a handful of colorspace that can be directly loaded into
* memory without colorspace conversion. */
const bool is_data = ColorSpaceManager::colorspace_is_data(metadata.colorspace);
const bool compress_as_srgb = metadata.is_compressible_as_srgb;
return is_data ? u_colorspace_data :
compress_as_srgb ? u_colorspace_scene_linear_srgb :
u_colorspace_scene_linear;
}
static bool make_tx(const string &filepath,
const string &out_filepath,
ImageMetaData &metadata,
const ImageAlphaType alpha_type,
const ImageFormatType format_type,
const ImageSpec &in_spec)
{
const bool use_openexr = metadata.is_float();
const TypeDesc out_format = metadata.typedesc();
ImageSpec spec;
/* Dimensions. */
spec.width = metadata.width;
spec.height = metadata.height;
spec.depth = 0;
spec.nchannels = metadata.is_rgba() ? 4 : 1;
spec.set_format(out_format);
spec.tile_width = 64;
spec.tile_height = 64;
spec.tile_depth = 1;
/* Inherit extra attributes from original image. */
spec.extra_attribs = in_spec.extra_attribs;
/* Software info, version is used to detect if file format is known by old versions. */
spec.attribute("Software", string_printf("Blender maketx v%d", TX_FILE_FORMAT_VERSION));
/* Compression. */
std::string in_compression = in_spec.get_string_attribute("compression", "none");
if (in_compression == "none") {
spec.attribute("compression", "zip");
}
spec.attribute("planarconfig", "contig");
/* Always convert to scene linear or data colorspace with associated alpha. */
const ustring colorspace = make_tx_get_file_colorspace(metadata);
spec.attribute("oiio:ColorSpace", colorspace);
const char *interop_id = ColorSpaceManager::colorspace_interop_id(colorspace);
if (interop_id) {
spec.attribute("colorInteropID", interop_id);
}
else {
spec.erase_attribute("colorInteropID");
}
/* Remove other potentially interfering colorspace metadata, that is no
* longer correct after colorspace conversion. Cycles itself does not use this,
* but other software could. */
spec.erase_attribute("Exif:ColorSpace");
spec.erase_attribute("ICCProfile");
spec.erase_attribute("CICP");
spec.attribute("oiio:UnassociatedAlpha", 0);
/* Source image metadata. */
if (use_openexr) {
spec.attribute("blender:SourceFile", filepath);
spec.attribute("blender:SourceColorSpace", metadata.colorspace);
spec.attribute("blender:SourceAlpha", alpha_type_string(alpha_type));
spec.attribute("blender:XYZToSceneLinear",
ColorSpaceManager::get_xyz_to_scene_linear_rgb_string());
}
else {
/* TIFF does not have arbitrary metadata, so we put it in an existing one. */
spec.attribute("DocumentName",
string_printf("%s (colorspace: %s, xyz_to_scene_linear:%s, alpha: %s)",
filepath.c_str(),
metadata.colorspace.c_str(),
ColorSpaceManager::get_xyz_to_scene_linear_rgb_string().c_str(),
alpha_type_string(alpha_type).c_str()));
}
/* OIIO texture format metadata. */
switch (format_type) {
case IMAGE_FORMAT_PLAIN:
spec.attribute("textureformat", "Plain Texture");
spec.attribute("wrapmodes", "black,black");
break;
case IMAGE_FORMAT_EQUIANGULAR:
spec.attribute("textureformat", "LatLong Environment");
spec.attribute("wrapmodes", "periodic,clamp");
if (use_openexr) {
spec.attribute("oiio:updirection", "y");
spec.attribute("oiio:sampleborder", 1);
}
break;
}
/* OpenEXR mipmap metadata. */
if (use_openexr) {
spec.attribute("openexr:roundingmode", 0 /* ROUND_DOWN */);
spec.erase_attribute("openexr:levelmode");
}
OIIO::ImageBuf buf(spec, OIIO::InitializePixels::No);
std::time_t in_time = OIIO::Filesystem::last_write_time(filepath);
if (!metadata.oiio_load_pixels(filepath, buf.localpixels(), false)) {
LOG_WARNING << "Failed to load pixels for " << filepath;
return false;
}
metadata.conform_pixels(buf.localpixels());
/* Convert to float only after conforming, so it matches the regular image
* loading code path exactly. */
if (buf.spec().format != TypeFloat) {
spec.set_format(TypeFloat);
OIIO::ImageBuf float_buf(spec, OIIO::InitializePixels::No);
OIIO::ImageBufAlgo::copy(float_buf, buf, TypeFloat);
buf = std::move(float_buf);
}
/* Write date and time from input file. */
buf.specmod().attribute("DateTime", datestring(in_time));
/* Write pixel statistics. */
write_stats_tx(buf, use_openexr);
/* Write to temporary file, next to the final filepath for atomic rename. */
/* TODO: Can we protect against leaving files on crashes? Two possibilities:
* - Write to tmp directory, then move next to target file and rename. This reduces
* the probability but does not eliminate it.
* - Add an atexit handles that removes any temporary files we are writing. This
* does not help with a power outage or maybe some types of process killing. */
const char *tmp_extension = (use_openexr) ? ".%%%%%%%%.temp.exr" : ".%%%%%%%%.temp.tif";
string tmp_filepath = OIIO::Filesystem::unique_path(out_filepath + tmp_extension);
std::unique_ptr<ImageOutput> out = ImageOutput::create(tmp_filepath);
bool ok = write_mipmap_tx(
out, tmp_filepath, out_format, format_type, metadata.is_compressible_as_srgb, buf);
out.reset();
/* Stamp with same time as input image file to detect updates. */
if (ok) {
OIIO::Filesystem::last_write_time(tmp_filepath, in_time);
/* Atomic move in case multiple process try to do this, and to avoid writing
* incomplete files in case of failure. */
std::string rename_err;
if (!OIIO::Filesystem::rename(tmp_filepath, out_filepath, rename_err)) {
LOG_WARNING << "Could not rename file: " << rename_err;
ok = false;
}
}
path_remove(tmp_filepath);
assert(path_is_file(out_filepath));
LOG_INFO << "Wrote tx file: " << out_filepath;
return ok;
}
bool make_tx(const string &filepath,
const string &out_filepath,
ustring colorspace,
const ImageAlphaType alpha_type,
const ImageFormatType format_type)
{
LOG_INFO << "Generating tx file for: " << filepath;
if (!path_create_directories(out_filepath)) {
LOG_WARNING << "Failed to create directory for texture cache: " << path_dirname(out_filepath);
return false;
}
ImageMetaData metadata;
ImageSpec spec;
metadata.colorspace = colorspace;
if (!metadata.oiio_load_metadata(filepath, &spec)) {
LOG_WARNING << "Failed to load metadata for " << filepath;
return false;
}
metadata.finalize(alpha_type);
if (!make_tx(filepath, out_filepath, metadata, alpha_type, format_type, spec)) {
LOG_WARNING << "Failed to write tx file";
return false;
}
return true;
}
CCL_NAMESPACE_END

View File

@@ -0,0 +1,39 @@
/* SPDX-FileCopyrightText: 2025 OpenImageIO project
* 2026 Blender Authors
*
* SPDX-License-Identifier: Apache-2.0
*
* This is a modified version of maketexture.cpp from OpenImageIO, to add a few
* features missing in the native implementation. */
#pragma once
#include <cstdlib>
#include <string>
#include "util/image_metadata.h"
#include "util/param.h"
#include "util/string.h"
#include "util/types_image.h"
CCL_NAMESPACE_BEGIN
#define TX_FILE_FORMAT_VERSION 0
bool resolve_tx(const string &filepath,
const string &texture_cache_path,
ustring colorspace,
const ImageAlphaType alpha_type,
const ImageFormatType format_type,
std::string &out_filepath,
ImageMetaData &out_metadata);
bool make_tx(const string &filepath,
const string &out_filepath,
ustring colorspace,
const ImageAlphaType alpha_type,
const ImageFormatType format_type);
ustring make_tx_get_file_colorspace(const ImageMetaData &metadata);
CCL_NAMESPACE_END

View File

@@ -0,0 +1,756 @@
/* SPDX-FileCopyrightText: 2011-2026 Blender Authors
*
* SPDX-License-Identifier: Apache-2.0 */
#include <cassert>
#include <csetjmp>
#include <cstdio>
#include <OpenImageIO/filesystem.h>
#include <OpenImageIO/typedesc.h>
#include "util/color.h"
#include "util/colorspace.h"
#include "util/image.h"
#include "util/image_maketx.h"
#include "util/image_metadata.h"
#include "util/log.h"
#include "util/param.h"
#include "util/path.h"
#include "util/types_image.h"
CCL_NAMESPACE_BEGIN
ImageMetaData::ImageMetaData() = default;
bool ImageMetaData::operator==(const ImageMetaData &other) const
{
return channels == other.channels && width == other.width && height == other.height &&
use_transform_3d == other.use_transform_3d &&
(!use_transform_3d || transform_3d == other.transform_3d) && type == other.type &&
colorspace == other.colorspace &&
is_compressible_as_srgb == other.is_compressible_as_srgb;
}
bool ImageMetaData::is_float() const
{
return (type == IMAGE_DATA_TYPE_FLOAT || type == IMAGE_DATA_TYPE_FLOAT4 ||
type == IMAGE_DATA_TYPE_HALF || type == IMAGE_DATA_TYPE_HALF4);
}
bool ImageMetaData::is_half() const
{
return (type == IMAGE_DATA_TYPE_HALF || type == IMAGE_DATA_TYPE_HALF4);
}
bool ImageMetaData::is_rgba() const
{
return (type == IMAGE_DATA_TYPE_BYTE4 || type == IMAGE_DATA_TYPE_USHORT4 ||
type == IMAGE_DATA_TYPE_HALF4 || type == IMAGE_DATA_TYPE_FLOAT4);
}
TypeDesc ImageMetaData::typedesc() const
{
switch (type) {
case IMAGE_DATA_TYPE_BYTE4:
case IMAGE_DATA_TYPE_BYTE:
return OIIO::TypeUInt8;
case IMAGE_DATA_TYPE_USHORT:
case IMAGE_DATA_TYPE_USHORT4:
return OIIO::TypeUInt16;
case IMAGE_DATA_TYPE_HALF4:
case IMAGE_DATA_TYPE_HALF:
return OIIO::TypeHalf;
case IMAGE_DATA_TYPE_FLOAT4:
case IMAGE_DATA_TYPE_FLOAT:
return OIIO::TypeFloat;
case IMAGE_DATA_TYPE_NANOVDB_FLOAT:
case IMAGE_DATA_TYPE_NANOVDB_FLOAT3:
case IMAGE_DATA_TYPE_NANOVDB_FLOAT4:
case IMAGE_DATA_TYPE_NANOVDB_FPN:
case IMAGE_DATA_TYPE_NANOVDB_FP16:
case IMAGE_DATA_TYPE_NANOVDB_EMPTY:
case IMAGE_DATA_NUM_TYPES:
assert(!"Unknown data type");
break;
}
return TypeUnknown;
}
size_t ImageMetaData::pixel_memory_size() const
{
switch (type) {
case IMAGE_DATA_TYPE_BYTE:
return sizeof(uint8_t);
case IMAGE_DATA_TYPE_BYTE4:
return sizeof(uint8_t) * 4;
case IMAGE_DATA_TYPE_USHORT:
return sizeof(uint16_t);
case IMAGE_DATA_TYPE_USHORT4:
return sizeof(uint16_t) * 4;
case IMAGE_DATA_TYPE_HALF:
case IMAGE_DATA_TYPE_NANOVDB_FP16:
return sizeof(half);
case IMAGE_DATA_TYPE_HALF4:
return sizeof(half) * 4;
case IMAGE_DATA_TYPE_FLOAT:
case IMAGE_DATA_TYPE_NANOVDB_FLOAT:
return sizeof(float);
case IMAGE_DATA_TYPE_NANOVDB_FLOAT3:
return sizeof(float) * 3;
case IMAGE_DATA_TYPE_FLOAT4:
case IMAGE_DATA_TYPE_NANOVDB_FLOAT4:
return sizeof(float) * 4;
case IMAGE_DATA_TYPE_NANOVDB_FPN:
case IMAGE_DATA_TYPE_NANOVDB_EMPTY:
case IMAGE_DATA_NUM_TYPES:
break;
}
return 0;
}
size_t ImageMetaData::memory_size() const
{
/* For NanoVDB, the byte size is stored directly. */
if (is_nanovdb_type(type)) {
return nanovdb_byte_size;
}
return width * height * pixel_memory_size();
}
void ImageMetaData::finalize(const ImageAlphaType alpha_type)
{
/* Convert used specified color spaces to one we know how to handle. */
colorspace = ColorSpaceManager::detect_known_colorspace(
colorspace, colorspace_file_hint.c_str(), colorspace_file_format, is_float());
if (colorspace == u_colorspace_scene_linear || colorspace == u_colorspace_data) {
/* Nothing to do. */
}
else if (colorspace == u_colorspace_scene_linear_srgb) {
/* Keep sRGB colorspace stored as sRGB, to save memory and/or loading time
* for the common case of 8bit sRGB images like PNG. */
is_compressible_as_srgb = true;
}
else {
/* If colorspace conversion needed, use half instead of short so we can
* represent HDR values that might result from conversion. */
if (type == IMAGE_DATA_TYPE_BYTE || type == IMAGE_DATA_TYPE_USHORT) {
type = IMAGE_DATA_TYPE_HALF;
}
else if (type == IMAGE_DATA_TYPE_BYTE4 || type == IMAGE_DATA_TYPE_USHORT4) {
type = IMAGE_DATA_TYPE_HALF4;
}
}
/* For Blender tx files, match colorspace used by maketx to generate the file.
* THis is needed because TIFF files can not store arbitrary colorspace metadata. */
if (!tile_need_conform) {
colorspace = make_tx_get_file_colorspace(*this);
}
ignore_alpha = alpha_type == IMAGE_ALPHA_IGNORE;
is_channel_packed = alpha_type == IMAGE_ALPHA_CHANNEL_PACKED;
/* No alpha conversion needed for data, associated, no alpha or channel packed. */
if (ColorSpaceManager::colorspace_is_data(colorspace) || alpha_type == IMAGE_ALPHA_ASSOCIATED ||
alpha_type == IMAGE_ALPHA_IGNORE || alpha_type == IMAGE_ALPHA_CHANNEL_PACKED)
{
is_unassociated_alpha = false;
}
/* Always convert if explicitly specified as unassociated. */
else if (alpha_type == IMAGE_ALPHA_UNASSOCIATED) {
is_unassociated_alpha = true;
}
else {
/* Leave automatically detected is_unassociated_alpha unchanged. */
}
/* Convert average color to scene linear colorspace. */
if (!is_zero(average_color) && colorspace != u_colorspace_data &&
colorspace != u_colorspace_scene_linear)
{
if (colorspace == u_colorspace_scene_linear_srgb) {
average_color = color_srgb_to_linear_v4(average_color);
}
else {
ColorSpaceManager::to_scene_linear(
colorspace, &average_color.x, 1, 1, 0, true, false, ignore_alpha || is_channel_packed);
}
}
}
void ImageMetaData::make_float()
{
switch (type) {
case IMAGE_DATA_TYPE_BYTE:
case IMAGE_DATA_TYPE_USHORT:
case IMAGE_DATA_TYPE_HALF:
type = IMAGE_DATA_TYPE_FLOAT;
break;
case IMAGE_DATA_TYPE_BYTE4:
case IMAGE_DATA_TYPE_USHORT4:
case IMAGE_DATA_TYPE_HALF4:
type = IMAGE_DATA_TYPE_FLOAT4;
break;
case IMAGE_DATA_TYPE_FLOAT:
case IMAGE_DATA_TYPE_FLOAT4:
case IMAGE_DATA_TYPE_NANOVDB_FLOAT:
case IMAGE_DATA_TYPE_NANOVDB_FLOAT3:
case IMAGE_DATA_TYPE_NANOVDB_FLOAT4:
case IMAGE_DATA_TYPE_NANOVDB_FPN:
case IMAGE_DATA_TYPE_NANOVDB_FP16:
case IMAGE_DATA_TYPE_NANOVDB_EMPTY:
case IMAGE_DATA_NUM_TYPES:
break;
}
}
static bool load_metadata_color(const ImageSpec &spec, const char *name, float4 &r_color)
{
string_view metadata_color = spec.get_string_attribute(name);
if (metadata_color.size() == 0) {
return false;
}
vector<float> color;
while (metadata_color.size()) {
float val;
if (!OIIO::Strutil::parse_float(metadata_color, val)) {
break;
}
color.push_back(val);
if (!OIIO::Strutil::parse_char(metadata_color, ',')) {
break;
}
}
if (color.size() != size_t(spec.nchannels)) {
return false;
}
switch (spec.nchannels) {
case 1:
r_color = make_float4(color[0], color[0], color[0], 1.0f);
return true;
case 2:
r_color = make_float4(color[0], color[0], color[0], color[1]);
return true;
case 3:
r_color = make_float4(color[0], color[1], color[2], 1.0f);
return true;
case 4:
r_color = make_float4(color[0], color[1], color[2], color[3]);
return true;
default:
return false;
}
}
void ImageMetaData::detect_tiles(ImageInput &input,
const ImageSpec &spec,
OIIO::string_view filepath)
{
/* 1x1 file can be used as tx file regardless of how it was generated,
* could be a constant color tx or just regular file. */
is_tx_file = (width == 1 && height == 1);
if (spec.tile_width == 0) {
return;
}
const std::string software = spec.get_string_attribute("Software");
int tx_file_format_version = INT_MAX;
sscanf(software.c_str(), "Blender maketx v%d", &tx_file_format_version);
if (tx_file_format_version != INT_MAX &&
(tx_file_format_version < 0 || tx_file_format_version > TX_FILE_FORMAT_VERSION))
{
LOG_DEBUG << "Image " << OIIO::Filesystem::filename(filepath)
<< " has tiles, but file format version " << tx_file_format_version
<< " is not supported by this version of Cycles";
return;
}
if (tx_file_format_version == INT_MAX) {
LOG_DEBUG << "Image " << OIIO::Filesystem::filename(filepath)
<< " has tiles, but is missing blender:TxFileFormatVersion";
tile_need_conform = true;
}
else if (!(channels == 1 || channels == 4)) {
LOG_DEBUG << "Image " << OIIO::Filesystem::filename(filepath)
<< " has tiles, but expected 1 or 4 channels, found " << channels;
tile_need_conform = true;
}
else {
tile_need_conform = false;
}
bool has_tiles = false;
bool is_small_image = false;
if (!is_power_of_two(spec.tile_width)) {
LOG_DEBUG << "Image " << OIIO::Filesystem::filename(filepath)
<< " has tiles, but tile size not power of two (" << spec.tile_width << ")";
}
else if (spec.tile_width != spec.tile_height) {
LOG_DEBUG << "Image " << OIIO::Filesystem::filename(filepath)
<< " has tiles, but tile size is not square (" << spec.tile_width << "x"
<< spec.tile_height << ")";
}
else if (spec.tile_depth != 1) {
LOG_DEBUG << "Image " << OIIO::Filesystem::filename(filepath)
<< " has tiles, but depth is not 1";
}
else if (spec.tile_width < KERNEL_IMAGE_TEX_PADDING * 4) {
LOG_DEBUG << "Image " << OIIO::Filesystem::filename(filepath)
<< " has tiles, but tile size too small (found " << spec.tile_width << ", minimum "
<< KERNEL_IMAGE_TEX_PADDING * 4 << ")";
}
else if (width < spec.tile_width && height < spec.tile_width) {
/* We don't currently supporting using tiles for images smaller than the tile
* size, and it's also unnecessary. To enable this, we'd need to solve the
* problem where interpolation at tile pixel centers does not match full image
* sampling due to padding offset. */
LOG_DEBUG << "Image " << OIIO::Filesystem::filename(filepath)
<< " has tiles, but image resolution is smaller than tile size";
has_tiles = true;
is_small_image = true;
}
else {
tile_size = spec.tile_width;
has_tiles = true;
}
/* Check if mip levels are complete. */
has_tiles_and_mipmaps = has_tiles && tile_size;
if (has_tiles_and_mipmaps) {
for (int miplevel = 0;; miplevel++) {
if (!input.seek_subimage(0, miplevel)) {
LOG_DEBUG << "Image " << OIIO::Filesystem::filename(filepath)
<< " has tiles, but missing mip levels";
has_tiles_and_mipmaps = false;
break;
}
const int mip_width = width >> miplevel;
const int mip_height = height >> miplevel;
if (mip_width <= tile_size && mip_height <= tile_size) {
break;
}
}
input.seek_subimage(0, 0);
}
/* Tiled tx files need to either have mipmaps, or be small enough not to need mipmaps. */
if (has_tiles && (is_small_image || has_tiles_and_mipmaps)) {
is_tx_file = true;
}
}
bool ImageMetaData::oiio_load_metadata(OIIO::string_view filepath, OIIO::ImageSpec *r_spec)
{
/* Perform preliminary checks, with meaningful logging. */
if (!OIIO::Filesystem::exists(filepath)) {
LOG_WARNING << "Image file " << filepath << " does not exist.";
return false;
}
if (OIIO::Filesystem::is_directory(filepath)) {
LOG_WARNING << "Image file " << filepath << " is a directory, cannot use as image.";
return false;
}
std::unique_ptr<ImageInput> in(ImageInput::create(filepath));
if (!in) {
LOG_WARNING << "Image file " << filepath << " failed to load.";
return false;
}
ImageSpec spec;
ImageSpec config;
/* Load without automatic OIIO alpha conversion. */
config.attribute("oiio:UnassociatedAlpha", 1);
if (!in->open(filepath, spec, config)) {
LOG_WARNING << "Image file " << filepath << " failed to open.";
return false;
}
if (spec.depth > 1) {
LOG_WARNING << "Image file " << filepath << " has unsupported depth of " << spec.depth;
return false;
}
width = spec.width;
height = spec.height;
is_compressible_as_srgb = false;
/* Check the main format, and channel formats. */
size_t channel_size = spec.format.basesize();
bool is_float = false;
bool is_half = false;
if (spec.format.is_floating_point()) {
is_float = true;
}
for (size_t channel = 0; channel < spec.channelformats.size(); channel++) {
channel_size = max(channel_size, spec.channelformats[channel].basesize());
if (spec.channelformats[channel].is_floating_point()) {
is_float = true;
}
}
/* check if it's half float */
if (spec.format == TypeDesc::HALF) {
is_half = true;
}
/* set type and channels */
channels = spec.nchannels;
if (is_half) {
type = (channels > 1) ? IMAGE_DATA_TYPE_HALF4 : IMAGE_DATA_TYPE_HALF;
}
else if (is_float) {
type = (channels > 1) ? IMAGE_DATA_TYPE_FLOAT4 : IMAGE_DATA_TYPE_FLOAT;
}
else if (spec.format == TypeDesc::USHORT) {
type = (channels > 1) ? IMAGE_DATA_TYPE_USHORT4 : IMAGE_DATA_TYPE_USHORT;
}
else {
type = (channels > 1) ? IMAGE_DATA_TYPE_BYTE4 : IMAGE_DATA_TYPE_BYTE;
}
colorspace_file_format = in->format_name();
colorspace_file_hint = spec.get_string_attribute("oiio:ColorSpace");
is_unassociated_alpha = spec.get_int_attribute("oiio:UnassociatedAlpha", 0);
if (!is_unassociated_alpha && spec.alpha_channel != -1) {
/* Workaround OIIO not detecting TGA file alpha the same as Blender (since #3019).
* We want anything not marked as premultiplied alpha to get associated. */
if (strcmp(in->format_name(), "targa") == 0) {
is_unassociated_alpha = spec.get_int_attribute("targa:alpha_type", -1) != 4;
}
/* OIIO DDS reader never sets UnassociatedAlpha attribute. */
if (strcmp(in->format_name(), "dds") == 0) {
is_unassociated_alpha = true;
}
/* Workaround OIIO bug that sets oiio:UnassociatedAlpha on the last layer
* but not composite image that we read. */
if (strcmp(in->format_name(), "psd") == 0) {
is_unassociated_alpha = true;
}
}
/* Load constant or average color. */
if (load_metadata_color(spec, "oiio:ConstantColor", average_color)) {
/* Could avoid loading tiles entirely for a bit more memory saving, or even
* constant folding in the shader nodes. */
}
else {
load_metadata_color(spec, "oiio:AverageColor", average_color);
}
detect_tiles(*in, spec, filepath);
LOG_DEBUG << "Image " << OIIO::Filesystem::filename(filepath) << ", " << width << "x" << height
<< ", " << (tile_size ? "tiled" : "untiled");
if (r_spec) {
*r_spec = spec;
}
return true;
}
template<typename StorageType>
static void conform_pixels_to_metadata_type(const ImageMetaData &metadata,
StorageType *pixels,
const int64_t width,
const int64_t height,
const int64_t x_stride,
const int64_t in_y_stride,
const int64_t out_y_stride)
{
/* The kernel can handle 1 and 4 channel images. Anything that is not a single
* channel image is converted to RGBA format. */
const int channels = metadata.channels;
const bool is_rgba = metadata.is_rgba();
/* Associate alpha. */
if (channels == 4 && metadata.is_unassociated_alpha) {
for (int64_t j = 0; j < height; j++) {
StorageType *pixel = pixels + j * in_y_stride;
for (int64_t i = 0; i < width; i++, pixel += 4) {
const StorageType alpha = pixel[3];
pixel[0] = util_image_multiply_native(pixel[0], alpha);
pixel[1] = util_image_multiply_native(pixel[1], alpha);
pixel[2] = util_image_multiply_native(pixel[2], alpha);
}
}
}
if (is_rgba) {
const StorageType one = util_image_cast_from_float<StorageType>(1.0f);
if (channels == 2) {
/* Grayscale + alpha to RGBA. */
for (int64_t j = height - 1; j >= 0; j--) {
StorageType *out_pixels = pixels + j * out_y_stride;
StorageType *in_pixels = pixels + j * in_y_stride;
for (int64_t i = width - 1; i >= 0; i--) {
out_pixels[i * 4 + 3] = in_pixels[i * x_stride + 1];
out_pixels[i * 4 + 2] = in_pixels[i * x_stride + 0];
out_pixels[i * 4 + 1] = in_pixels[i * x_stride + 0];
out_pixels[i * 4 + 0] = in_pixels[i * x_stride + 0];
}
}
}
else if (channels == 3) {
/* RGB to RGBA. */
for (int64_t j = height - 1; j >= 0; j--) {
StorageType *out_pixels = pixels + j * out_y_stride;
StorageType *in_pixels = pixels + j * in_y_stride;
for (int64_t i = width - 1; i >= 0; i--) {
out_pixels[i * 4 + 3] = one;
out_pixels[i * 4 + 2] = in_pixels[i * x_stride + 2];
out_pixels[i * 4 + 1] = in_pixels[i * x_stride + 1];
out_pixels[i * 4 + 0] = in_pixels[i * x_stride + 0];
}
}
}
else if (channels == 1) {
/* Grayscale to RGBA. */
for (int64_t j = height - 1; j >= 0; j--) {
StorageType *out_pixels = pixels + j * out_y_stride;
StorageType *in_pixels = pixels + j * in_y_stride;
for (int64_t i = width - 1; i >= 0; i--) {
out_pixels[i * 4 + 3] = one;
out_pixels[i * 4 + 2] = in_pixels[i * x_stride];
out_pixels[i * 4 + 1] = in_pixels[i * x_stride];
out_pixels[i * 4 + 0] = in_pixels[i * x_stride];
}
}
}
/* Disable alpha if requested by the user. */
if (metadata.ignore_alpha) {
for (int64_t j = 0; j < height; j++) {
StorageType *out_pixels = pixels + j * out_y_stride;
for (int64_t i = 0; i < width; i++) {
out_pixels[i * 4 + 3] = one;
}
}
}
}
if (metadata.colorspace != u_colorspace_scene_linear &&
metadata.colorspace != u_colorspace_scene_linear_srgb &&
metadata.colorspace != u_colorspace_data)
{
/* Convert to scene linear. */
ColorSpaceManager::to_scene_linear(metadata.colorspace,
pixels,
width,
height,
out_y_stride,
is_rgba,
metadata.is_compressible_as_srgb,
metadata.ignore_alpha || metadata.is_channel_packed);
}
/* Make sure we don't have buggy values. */
if constexpr (std::is_same_v<float, StorageType> || std::is_same_v<half, StorageType>) {
/* For RGBA buffers we put all channels to 0 if either of them is not
* finite. This way we avoid possible artifacts caused by fully changed
* hue. */
if (is_rgba) {
for (int64_t j = 0; j < height; j++) {
StorageType *pixel = pixels + j * out_y_stride;
for (int64_t i = 0; i < width; i++, pixel += 4) {
if (!util_image_is_finite(pixel[0]) || !util_image_is_finite(pixel[1]) ||
!util_image_is_finite(pixel[2]) || !util_image_is_finite(pixel[3]))
{
pixel[0] = 0;
pixel[1] = 0;
pixel[2] = 0;
pixel[3] = 0;
}
}
}
}
else {
for (int64_t j = 0; j < height; j++) {
StorageType *pixel = pixels + j * out_y_stride;
for (int64_t i = 0; i < width; i++, pixel++) {
if (!util_image_is_finite(pixel[0])) {
pixel[0] = 0;
}
}
}
}
}
}
void ImageMetaData::conform_pixels(void *pixels,
const int64_t width,
const int64_t height,
const int64_t x_stride,
const int64_t in_y_stride,
const int64_t out_y_stride) const
{
switch (type) {
case IMAGE_DATA_TYPE_BYTE4:
case IMAGE_DATA_TYPE_BYTE:
conform_pixels_to_metadata_type<uchar>(
*this, static_cast<uchar *>(pixels), width, height, x_stride, in_y_stride, out_y_stride);
break;
case IMAGE_DATA_TYPE_USHORT:
case IMAGE_DATA_TYPE_USHORT4:
conform_pixels_to_metadata_type<uint16_t>(*this,
static_cast<uint16_t *>(pixels),
width,
height,
x_stride,
in_y_stride,
out_y_stride);
break;
case IMAGE_DATA_TYPE_HALF4:
case IMAGE_DATA_TYPE_HALF:
conform_pixels_to_metadata_type<half>(
*this, static_cast<half *>(pixels), width, height, x_stride, in_y_stride, out_y_stride);
break;
case IMAGE_DATA_TYPE_FLOAT4:
case IMAGE_DATA_TYPE_FLOAT:
conform_pixels_to_metadata_type<float>(
*this, static_cast<float *>(pixels), width, height, x_stride, in_y_stride, out_y_stride);
break;
case IMAGE_DATA_TYPE_NANOVDB_FLOAT:
case IMAGE_DATA_TYPE_NANOVDB_FLOAT3:
case IMAGE_DATA_TYPE_NANOVDB_FLOAT4:
case IMAGE_DATA_TYPE_NANOVDB_FPN:
case IMAGE_DATA_TYPE_NANOVDB_FP16:
case IMAGE_DATA_TYPE_NANOVDB_EMPTY:
case IMAGE_DATA_NUM_TYPES:
break;
}
}
void ImageMetaData::conform_pixels(void *pixels) const
{
conform_pixels(pixels, width, height, channels, width * channels, width * (is_rgba() ? 4 : 1));
}
template<TypeDesc::BASETYPE FileFormat, typename StorageType>
static bool load_pixels_oiio(const ImageMetaData &metadata,
const std::unique_ptr<ImageInput> &in,
StorageType *pixels,
const bool flip_y)
{
const int64_t width = metadata.width;
const int64_t height = metadata.height;
const int channels = metadata.channels;
const int64_t num_pixels = width * height;
int64_t read_y_stride = width * channels * sizeof(StorageType);
/* Read pixels through OpenImageIO. */
StorageType *readpixels = pixels;
vector<StorageType> tmppixels;
if (channels > 4) {
tmppixels.resize(num_pixels * channels);
readpixels = &tmppixels[0];
}
if (!in->read_image(0,
0,
0,
channels,
FileFormat,
(flip_y) ? (uchar *)readpixels + (height - 1) * read_y_stride :
(uchar *)readpixels,
AutoStride,
(flip_y) ? -read_y_stride : read_y_stride,
AutoStride))
{
return false;
}
if (channels > 4) {
for (int64_t j = 0; j < height; j++) {
const StorageType *in_pixels = tmppixels.data() + j * width * channels;
StorageType *out_pixels = pixels + j * width * 4;
for (int64_t i = 0; i < width; i++) {
out_pixels[i * 4 + 3] = in_pixels[i * channels + 3];
out_pixels[i * 4 + 2] = in_pixels[i * channels + 2];
out_pixels[i * 4 + 1] = in_pixels[i * channels + 1];
out_pixels[i * 4 + 0] = in_pixels[i * channels + 0];
}
}
tmppixels.clear();
}
return true;
}
bool ImageMetaData::oiio_load_pixels(OIIO::string_view filepath,
void *pixels,
const bool flip_y) const
{
/* load image from file through OIIO */
std::unique_ptr<ImageInput> in = ImageInput::create(filepath);
if (!in) {
return false;
}
ImageSpec spec = ImageSpec();
ImageSpec config = ImageSpec();
/* Load without automatic OIIO alpha conversion, we do it ourselves. OIIO
* will associate alpha in the 8bit buffer for PNGs, which leads to too
* much precision loss when we load it as half float to do a color-space transform. */
config.attribute("oiio:UnassociatedAlpha", 1);
if (!in->open(filepath, spec, config)) {
return false;
}
if (spec.depth > 1) {
LOG_WARNING << "Image file " << filepath << " has unsupported depth " << spec.depth;
return false;
}
switch (type) {
case IMAGE_DATA_TYPE_BYTE:
case IMAGE_DATA_TYPE_BYTE4:
return load_pixels_oiio<TypeDesc::UINT8, uchar>(*this, in, (uchar *)pixels, flip_y);
case IMAGE_DATA_TYPE_USHORT:
case IMAGE_DATA_TYPE_USHORT4:
return load_pixels_oiio<TypeDesc::USHORT, uint16_t>(*this, in, (uint16_t *)pixels, flip_y);
case IMAGE_DATA_TYPE_HALF:
case IMAGE_DATA_TYPE_HALF4:
return load_pixels_oiio<TypeDesc::HALF, half>(*this, in, (half *)pixels, flip_y);
case IMAGE_DATA_TYPE_FLOAT:
case IMAGE_DATA_TYPE_FLOAT4:
return load_pixels_oiio<TypeDesc::FLOAT, float>(*this, in, (float *)pixels, flip_y);
case IMAGE_DATA_TYPE_NANOVDB_FLOAT:
case IMAGE_DATA_TYPE_NANOVDB_FLOAT3:
case IMAGE_DATA_TYPE_NANOVDB_FLOAT4:
case IMAGE_DATA_TYPE_NANOVDB_FPN:
case IMAGE_DATA_TYPE_NANOVDB_FP16:
case IMAGE_DATA_TYPE_NANOVDB_EMPTY:
case IMAGE_DATA_NUM_TYPES:
break;
}
return false;
}
CCL_NAMESPACE_END

View File

@@ -0,0 +1,97 @@
/* SPDX-FileCopyrightText: 2011-2026 Blender Authors
*
* SPDX-License-Identifier: Apache-2.0 */
#pragma once
#include <cstdlib>
#include <OpenImageIO/imageio.h>
#include <OpenImageIO/string_view.h>
#include "util/colorspace.h"
#include "util/string.h"
#include "util/types_image.h"
CCL_NAMESPACE_BEGIN
/* Image MetaData
*
* Information about the image that is available before the image pixels are loaded.
* This can be filled by an image loaded through OpenImageIO, but is also used for
* custom image loaders that may e.g. load an image from memory or auto generate it. */
class ImageMetaData {
public:
/* Input image dimensions and format. */
int channels = 0;
int64_t width = 0, height = 0;
ImageDataType type = IMAGE_DATA_NUM_TYPES;
/* Input colorspace hints. */
string colorspace_file_hint;
const char *colorspace_file_format = "";
/* Input tile info. */
bool has_tiles_and_mipmaps = false;
uint32_t tile_size = 0;
float4 average_color = zero_float4();
bool tile_need_conform = true;
/* File is a valid tx file, but does not necessarily have mipmaps and tiles if it's
* small enough not to need them. */
bool is_tx_file = false;
/* Input NanoVDB data. */
int64_t nanovdb_byte_size = 0;
bool use_transform_3d = false;
Transform transform_3d = transform_identity();
/* Auto determined by finalize. */
ustring colorspace = u_colorspace_scene_linear;
bool is_compressible_as_srgb = false;
bool is_unassociated_alpha = false;
bool ignore_alpha = false;
bool is_channel_packed = false;
/* Constructor */
ImageMetaData();
bool operator==(const ImageMetaData &other) const;
/* Finalize image metadata, with optional user provided alpha type and colorspace. */
void finalize(const ImageAlphaType alpha_type = IMAGE_ALPHA_AUTO);
/* Image info. */
bool is_float() const;
bool is_half() const;
bool is_rgba() const;
TypeDesc typedesc() const;
/* Memory stats. */
size_t memory_size() const;
size_t pixel_memory_size() const;
/* OpenImageIO metadata and pixel loading. */
bool oiio_load_metadata(OIIO::string_view filepath, OIIO::ImageSpec *r_spec = nullptr);
bool oiio_load_pixels(OIIO::string_view filepath, void *pixels, const bool flip_y = true) const;
/* Change data type to float. */
void make_float();
/* Modify pixel data to conform to formats known to the kernel. That means 1 or 4 channels,
* a known ImageDataType, associated alpha and no NaN or infinite values. */
void conform_pixels(void *pixels) const;
void conform_pixels(void *pixels,
const int64_t width,
const int64_t height,
const int64_t x_stride,
const int64_t in_y_stride,
const int64_t out_y_stride) const;
protected:
void detect_tiles(OIIO::ImageInput &input,
const OIIO::ImageSpec &spec,
OIIO::string_view filepath);
};
CCL_NAMESPACE_END

View File

@@ -0,0 +1,18 @@
/* SPDX-FileCopyrightText: 2026 Blender Authors
*
* SPDX-License-Identifier: Apache-2.0 */
#include "util/implicit_sharing.h"
CCL_NAMESPACE_BEGIN
ImplicitSharingUserAddFn g_implicit_sharing_user_add_fn = nullptr;
ImplicitSharingUserAddFn g_implicit_sharing_user_remove_fn = nullptr;
void implicit_sharing_init(ImplicitSharingUserAddFn add_fn, ImplicitSharingUserRemoveFn remove_fn)
{
g_implicit_sharing_user_add_fn = add_fn;
g_implicit_sharing_user_remove_fn = remove_fn;
}
CCL_NAMESPACE_END

View File

@@ -0,0 +1,24 @@
/* SPDX-FileCopyrightText: 2026 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#pragma once
CCL_NAMESPACE_BEGIN
using ImplicitSharingInfo = const void *;
using ImplicitSharingUserAddFn = void (*)(ImplicitSharingInfo);
using ImplicitSharingUserRemoveFn = void (*)(ImplicitSharingInfo);
extern ImplicitSharingUserAddFn g_implicit_sharing_user_add_fn;
extern ImplicitSharingUserRemoveFn g_implicit_sharing_user_remove_fn;
/**
* Set function pointers to be called when implicit sharing (use count tracking) is enabled.
* "Sharing info" references that own data like attribute arrays are passed as void pointers to
* various Cycles API functions, and these functions are called to change the reference count.
*/
void implicit_sharing_init(ImplicitSharingUserAddFn add_fn, ImplicitSharingUserRemoveFn remove_fn);
CCL_NAMESPACE_END

View File

@@ -0,0 +1,13 @@
/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#pragma once
#include <list>
CCL_NAMESPACE_BEGIN
using std::list;
CCL_NAMESPACE_END

View File

@@ -0,0 +1,149 @@
/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#include "util/log.h"
#include "util/math.h"
#include "util/string.h"
#include "util/time.h"
#include <cstdio>
#ifdef _MSC_VER
# define snprintf _snprintf
#endif
CCL_NAMESPACE_BEGIN
LogLevel LOG_LEVEL = LOG_LEVEL_INFO_IMPORTANT;
static LogFunction LOG_FUNCTION;
static double LOG_START_TIME = time_dt();
const char *log_level_to_string(const LogLevel level)
{
switch (level) {
case LOG_LEVEL_FATAL:
case LOG_LEVEL_DFATAL:
return "FATAL";
case LOG_LEVEL_ERROR:
case LOG_LEVEL_DERROR:
return "ERROR";
case LOG_LEVEL_WARNING:
case LOG_LEVEL_DWARNING:
return "WARNING";
case LOG_LEVEL_INFO_IMPORTANT:
case LOG_LEVEL_INFO:
return "INFO";
case LOG_LEVEL_DEBUG:
return "DEBUG";
case LOG_LEVEL_TRACE:
return "TRACE";
case LOG_LEVEL_UNKNOWN:
return "UNKNOWN";
}
return "";
}
LogLevel log_string_to_level(const string &str)
{
const std::string str_lower = string_to_lower(str);
if (str_lower == "fatal") {
return LOG_LEVEL_FATAL;
}
if (str_lower == "error") {
return LOG_LEVEL_ERROR;
}
if (str_lower == "warning") {
return LOG_LEVEL_WARNING;
}
if (str_lower == "info") {
return LOG_LEVEL_INFO;
}
if (str_lower == "debug") {
return LOG_LEVEL_DEBUG;
}
if (str_lower == "trace") {
return LOG_LEVEL_TRACE;
}
return LOG_LEVEL_UNKNOWN;
}
void log_init(const LogFunction func)
{
LOG_FUNCTION = func;
LOG_START_TIME = time_dt();
}
void log_level_set(const LogLevel level)
{
LOG_LEVEL = level;
}
void log_level_set(const std::string &level)
{
const LogLevel new_level = log_string_to_level(level);
if (new_level == LOG_LEVEL_UNKNOWN) {
LOG_ERROR << "Unknown log level specified: " << level;
return;
}
LOG_LEVEL = new_level;
}
static void log_default(const LogLevel level, const std::string &time_str, const char *msg)
{
if (level >= LOG_LEVEL_INFO) {
printf("%s | %s\n", time_str.c_str(), msg);
}
else {
fflush(stdout);
fprintf(stderr, "%s | %s: %s\n", time_str.c_str(), log_level_to_string(level), msg);
}
}
void _log_message(const LogLevel level, const char *file_line, const char *func, const char *msg)
{
assert(level <= LOG_LEVEL);
if (LOG_FUNCTION) {
LOG_FUNCTION(level, file_line, func, msg);
return;
}
const std::string time_str = time_human_readable_from_seconds(time_dt() - LOG_START_TIME);
if (strchr(msg, '\n') == nullptr) {
log_default(level, time_str, msg);
return;
}
vector<string> lines;
string_split(lines, msg, "\n", false);
for (const string &line : lines) {
log_default(level, time_str, line.c_str());
}
if (level == LOG_LEVEL_FATAL || level == LOG_LEVEL_DFATAL) {
abort();
}
}
std::ostream &operator<<(std::ostream &os, const int2 &value)
{
os << "(" << value.x << ", " << value.y << ")";
return os;
}
std::ostream &operator<<(std::ostream &os, const float3 &value)
{
os << "(" << value.x << ", " << value.y << ", " << value.z << ")";
return os;
}
std::ostream &operator<<(std::ostream &os, const float4 &value)
{
os << "(" << value.x << ", " << value.y << ", " << value.z << ", " << value.w << ")";
return os;
}
CCL_NAMESPACE_END

View File

@@ -0,0 +1,169 @@
/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#pragma once
#include "util/defines.h"
#include "util/string.h"
#include "util/types.h"
#include <sstream>
CCL_NAMESPACE_BEGIN
/* Log Levels */
enum LogLevel {
LOG_LEVEL_FATAL = 0, /* Fatal error, application will abort */
LOG_LEVEL_DFATAL = 1, /* Fatal error in debug build only */
LOG_LEVEL_ERROR = 2, /* Error */
LOG_LEVEL_DERROR = 3, /* Error in debug build only */
LOG_LEVEL_WARNING = 4, /* Warning */
LOG_LEVEL_DWARNING = 5, /* Warning in debug build only */
LOG_LEVEL_INFO_IMPORTANT = 6, /* Important info that is printed by default */
LOG_LEVEL_INFO = 7, /* Info about devices, scene contents and features used. */
LOG_LEVEL_DEBUG = 8, /* Work being performed and timing/memory stats about that work. */
LOG_LEVEL_TRACE = 9, /* Verbose debug messages, to trace code being executed. */
LOG_LEVEL_UNKNOWN = -1,
};
const char *log_level_to_string(const LogLevel level);
LogLevel log_string_to_level(const string &str);
/* Log Configuration API
*
* By default will print to stdout and stderr, but a custom log function can be set
* to change this behavior. */
using LogFunction = void (*)(const LogLevel level,
const char *file_line,
const char *func,
const char *msg);
void log_init(const LogFunction func = nullptr);
void log_level_set(const LogLevel level);
void log_level_set(const string &level);
/* Internal logging API */
void _log_message(const LogLevel level, const char *file_line, const char *func, const char *msg);
class LogMessage {
public:
LogMessage(enum LogLevel level, const char *file_line, const char *func)
: level_(level), file_line_(file_line), func_(func)
{
}
~LogMessage()
{
_log_message(level_, file_line_, func_, stream_.str().c_str());
}
std::ostream &stream()
{
return stream_;
}
protected:
LogLevel level_;
const char *file_line_;
const char *func_;
std::stringstream stream_;
};
extern LogLevel LOG_LEVEL;
#define LOG_STRINGIFY_APPEND(a, b) "" a #b
#define LOG_STRINGIFY(x) LOG_STRINGIFY_APPEND("", x)
/* Macro to ensure lazy evaluation of both condition and logging text. */
#ifdef NDEBUG
# define LOG_IF(level, condition) \
if constexpr (level != LOG_LEVEL_DFATAL && level != LOG_LEVEL_DERROR && \
level != LOG_LEVEL_DWARNING) \
if (UNLIKELY(level <= LOG_LEVEL && (condition))) \
LogMessage(level, __FILE__ ":" LOG_STRINGIFY(__LINE__), __func__).stream()
#else
# define LOG_IF(level, condition) \
if (UNLIKELY(level <= LOG_LEVEL && (condition))) \
LogMessage(level, __FILE__ ":" LOG_STRINGIFY(__LINE__), __func__).stream()
#endif
/* Log a message at the desired level.
*
* Example: LOG_INFO << "This is a log message"; */
#define LOG(level) LOG_IF(level, true)
#define LOG_FATAL LOG(LOG_LEVEL_FATAL)
#define LOG_DFATAL LOG(LOG_LEVEL_DFATAL)
#define LOG_ERROR LOG(LOG_LEVEL_ERROR)
#define LOG_DERROR LOG(LOG_LEVEL_DERROR)
#define LOG_WARNING LOG(LOG_LEVEL_WARNING)
#define LOG_DWARNING LOG(LOG_LEVEL_DWARNING)
#define LOG_INFO_IMPORTANT LOG(LOG_LEVEL_INFO_IMPORTANT)
#define LOG_INFO LOG(LOG_LEVEL_INFO)
#define LOG_DEBUG LOG(LOG_LEVEL_DEBUG)
#define LOG_TRACE LOG(LOG_LEVEL_TRACE)
/* Check if logging is enabled, to avoid doing expensive work to compute
* the logging message. Note that any work to the right of LOG(level) will
* not be evaulated if logging for that level is disabled. */
#define LOG_IS_ON(level) ((level) <= LOG_LEVEL)
/* Check if expression and conditions hold true, failure will exit the program. */
#define CHECK(expression) LOG_IF(LOG_LEVEL_FATAL, !(expression))
#define CHECK_OP(op, a, b) LOG_IF(LOG_LEVEL_FATAL, !((a)op(b)))
#define CHECK_GE(a, b) CHECK_OP(>=, a, b)
#define CHECK_NE(a, b) CHECK_OP(!=, a, b)
#define CHECK_EQ(a, b) CHECK_OP(==, a, b)
#define CHECK_GT(a, b) CHECK_OP(>, a, b)
#define CHECK_LT(a, b) CHECK_OP(<, a, b)
#define CHECK_LE(a, b) CHECK_OP(<=, a, b)
/* Same checks for expressions and conditions, but only active in debug builds. */
#ifndef NDEBUG
template<typename T> T DCheckNotNull(T &&t, const char *expression)
{
if (t == nullptr) {
LOG_FATAL << "Failed " << expression << "is not null";
}
return std::forward<T>(t);
}
# define DCHECK(expression) \
LOG_IF(LOG_LEVEL_DFATAL, !(expression)) << LOG_STRINGIFY(expression) << " "
# define DCHECK_NOTNULL(expression) DCheckNotNull(expression, LOG_STRINGIFY(expression))
# define DCHECK_OP(op, a, b) \
LOG_IF(LOG_LEVEL_DFATAL, !((a)op(b))) \
<< "Failed " << LOG_STRINGIFY(a) << " (" << a << ") " << LOG_STRINGIFY(op) << " " \
<< LOG_STRINGIFY(b) << " (" << b << ") "
# define DCHECK_GE(a, b) DCHECK_OP(>=, a, b)
# define DCHECK_NE(a, b) DCHECK_OP(!=, a, b)
# define DCHECK_EQ(a, b) DCHECK_OP(==, a, b)
# define DCHECK_GT(a, b) DCHECK_OP(>, a, b)
# define DCHECK_LT(a, b) DCHECK_OP(<, a, b)
# define DCHECK_LE(a, b) DCHECK_OP(<=, a, b)
#else
# define LOG_SUPPRESS() LOG_IF(LOG_LEVEL_TRACE, false)
# define DCHECK(expression) LOG_SUPPRESS()
# define DCHECK_NOTNULL(expression) (expression)
# define DCHECK_GE(a, b) LOG_SUPPRESS()
# define DCHECK_NE(a, b) LOG_SUPPRESS()
# define DCHECK_EQ(a, b) LOG_SUPPRESS()
# define DCHECK_GT(a, b) LOG_SUPPRESS()
# define DCHECK_LT(a, b) LOG_SUPPRESS()
# define DCHECK_LE(a, b) LOG_SUPPRESS()
#endif
/* Convenient logging of common data structures. */
struct int2;
struct float3;
struct float4;
std::ostream &operator<<(std::ostream &os, const int2 &value);
std::ostream &operator<<(std::ostream &os, const float3 &value);
std::ostream &operator<<(std::ostream &os, const float4 &value);
CCL_NAMESPACE_END

View File

@@ -0,0 +1,24 @@
/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#pragma once
#include <map>
#include <unordered_map>
CCL_NAMESPACE_BEGIN
using std::map;
using std::pair;
using std::unordered_map;
using std::unordered_multimap;
template<typename T> static void map_free_memory(T &data)
{
/* Use swap() trick to actually free all internal memory. */
T empty_data;
data.swap(empty_data);
}
CCL_NAMESPACE_END

View File

@@ -0,0 +1,24 @@
/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#pragma once
#include "util/types.h" // IWYU pragma: export
#include "util/math_base.h" // IWYU pragma: export
#include "util/math_int2.h" // IWYU pragma: export
#include "util/math_int3.h" // IWYU pragma: export
#include "util/math_int4.h" // IWYU pragma: export
#include "util/math_int8.h" // IWYU pragma: export
#include "util/math_float2.h" // IWYU pragma: export
#include "util/math_float4.h" // IWYU pragma: export
#include "util/math_float8.h" // IWYU pragma: export
#include "util/math_float3.h" // IWYU pragma: export
#include "util/math_dual.h" // IWYU pragma: export
#include "util/rect.h" // IWYU pragma: export

View File

@@ -0,0 +1,964 @@
/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#pragma once
/* Math
*
* Basic math functions on scalar and vector types. This header is used by
* both the kernel code when compiled as C++, and other C++ non-kernel code. */
#include "util/defines.h"
#include "util/types_base.h"
#ifdef __HIP__
# include <hip/hip_vector_types.h>
#endif
#if !defined(__KERNEL_METAL__)
# include <cfloat> // IWYU pragma: export
# include <cmath> // IWYU pragma: export
#endif
#if !defined(__KERNEL_GPU__)
# include <bit>
#endif
CCL_NAMESPACE_BEGIN
/* Float Pi variations */
/* Division */
#ifndef M_PI_F
# define M_PI_F (3.1415926535897932f) /* `pi` */
#endif
#ifndef M_PI_2_F
# define M_PI_2_F (1.5707963267948966f) /* `pi/2` */
#endif
#ifndef M_PI_4_F
# define M_PI_4_F (0.7853981633974830f) /* `pi/4` */
#endif
#ifndef M_1_PI_F
# define M_1_PI_F (0.3183098861837067f) /* `1/pi` */
#endif
#ifndef M_2_PI_F
# define M_2_PI_F (0.6366197723675813f) /* `2/pi` */
#endif
#ifndef M_1_2PI_F
# define M_1_2PI_F (0.1591549430918953f) /* `1/(2*pi)` */
#endif
#ifndef M_1_4PI_F
# define M_1_4PI_F (0.0795774715459476f) /* `1/(4*pi)` */
#endif
#ifndef M_SQRT_PI_8_F
# define M_SQRT_PI_8_F (0.6266570686577501f) /* `sqrt(pi/8)` */
#endif
#ifndef M_LN_2PI_F
# define M_LN_2PI_F (1.8378770664093454f) /* `ln(2*pi)` */
#endif
/* Multiplication */
#ifndef M_2PI_F
# define M_2PI_F (6.2831853071795864f) /* `2*pi` */
#endif
#ifndef M_4PI_F
# define M_4PI_F (12.566370614359172f) /* `4*pi` */
#endif
#ifndef M_PI_4F
# define M_PI_4F 0.78539816339744830962f /* `pi/4` */
#endif
/* Float sqrt variations */
#ifndef M_SQRT2_F
# define M_SQRT2_F (1.4142135623730950f) /* `sqrt(2)` */
#endif
#ifndef M_CBRT2_F
# define M_CBRT2_F 1.2599210498948732f /* `cbrt(2)` */
#endif
#ifndef M_SQRT1_2F
# define M_SQRT1_2F 0.70710678118654752440f /* `sqrt(1/2)` */
#endif
#ifndef M_SQRT3_F
# define M_SQRT3_F (1.7320508075688772f) /* `sqrt(3)` */
#endif
#ifndef M_LN2_F
# define M_LN2_F (0.6931471805599453f) /* `ln(2)` */
#endif
#ifndef M_LN10_F
# define M_LN10_F (2.3025850929940457f) /* `ln(10)` */
#endif
/* Scalar */
#if !defined(__HIP__) && !defined(__KERNEL_ONEAPI__)
# ifdef _WIN32
ccl_device_inline float fmaxf(const float a, const float b)
{
return (a > b) ? a : b;
}
ccl_device_inline float fminf(const float a, const float b)
{
return (a < b) ? a : b;
}
# endif /* _WIN32 */
#endif /* __HIP__, __KERNEL_ONEAPI__ */
#if !defined(__KERNEL_GPU__) || defined(__KERNEL_ONEAPI__)
# ifndef __KERNEL_ONEAPI__
using std::isfinite;
using std::isnan;
using std::sqrt;
# else
# define isfinite(x) sycl::isfinite((x))
# define isnan(x) sycl::isnan((x))
# endif
ccl_device_inline int abs(const int x)
{
return (x > 0) ? x : -x;
}
ccl_device_inline int max(const int a, const int b)
{
return (a > b) ? a : b;
}
ccl_device_inline int min(const int a, const int b)
{
return (a < b) ? a : b;
}
ccl_device_inline uint32_t max(const uint32_t a, const uint32_t b)
{
return (a > b) ? a : b;
}
ccl_device_inline uint32_t min(const uint32_t a, const uint32_t b)
{
return (a < b) ? a : b;
}
ccl_device_inline uint64_t max(const uint64_t a, const uint64_t b)
{
return (a > b) ? a : b;
}
ccl_device_inline uint64_t min(const uint64_t a, const uint64_t b)
{
return (a < b) ? a : b;
}
/* NOTE: On 64bit Darwin the `size_t` is defined as `unsigned long int` and `uint64_t` is defined
* as `unsigned long long`. Both of the definitions are 64 bit unsigned integer, but the automatic
* substitution does not allow to automatically pick function defined for `uint64_t` as it is not
* exactly the same type definition.
* Work this around by adding a templated function enabled for `size_t` type which will be used
* when there is no explicit specialization of `min()`/`max()` above. */
template<class T>
ccl_device_inline typename std::enable_if_t<std::is_same_v<T, size_t>, T> max(T a, T b)
{
return (a > b) ? a : b;
}
template<class T>
ccl_device_inline typename std::enable_if_t<std::is_same_v<T, size_t>, T> min(T a, T b)
{
return (a < b) ? a : b;
}
ccl_device_inline float max(const float a, const float b)
{
return (a > b) ? a : b;
}
ccl_device_inline float min(const float a, const float b)
{
return (a < b) ? a : b;
}
ccl_device_inline double max(const double a, const double b)
{
return (a > b) ? a : b;
}
ccl_device_inline double min(const double a, const double b)
{
return (a < b) ? a : b;
}
/* These 2 guys are templated for usage with registers data.
*
* NOTE: Since this is CPU-only functions it is ok to use references here.
* But for other devices we'll need to be careful about this.
*/
template<typename T> ccl_device_inline T min4(const T &a, const T &b, const T &c, const T &d)
{
return min(min(a, b), min(c, d));
}
template<typename T> ccl_device_inline T max4(const T &a, const T &b, const T &c, const T &d)
{
return max(max(a, b), max(c, d));
}
#endif /* __KERNEL_GPU__ */
ccl_device_inline float min4(const float a, const float b, float c, const float d)
{
return min(min(a, b), min(c, d));
}
ccl_device_inline float max4(const float a, const float b, float c, const float d)
{
return max(max(a, b), max(c, d));
}
template<typename T> ccl_device_inline T make_zero();
ccl_device_template_spec float make_zero()
{
return 0.0f;
}
#if !defined(__KERNEL_METAL__) && !defined(__KERNEL_ONEAPI__)
/* Int/Float conversion */
ccl_device_inline int as_int(const uint i)
{
union {
uint ui;
int i;
} u;
u.ui = i;
return u.i;
}
ccl_device_inline uint as_uint(const int i)
{
union {
uint ui;
int i;
} u;
u.i = i;
return u.ui;
}
ccl_device_inline uint as_uint(const float f)
{
union {
uint i;
float f;
} u;
u.f = f;
return u.i;
}
# ifndef __HIP__
ccl_device_inline int __float_as_int(const float f)
{
union {
int i;
float f;
} u;
u.f = f;
return u.i;
}
ccl_device_inline float __int_as_float(const int i)
{
union {
int i;
float f;
} u;
u.i = i;
return u.f;
}
ccl_device_inline uint __float_as_uint(const float f)
{
union {
uint i;
float f;
} u;
u.f = f;
return u.i;
}
ccl_device_inline float __uint_as_float(const uint i)
{
union {
uint i;
float f;
} u;
u.i = i;
return u.f;
}
# endif
#endif /* !defined(__KERNEL_METAL__) */
#if defined(__KERNEL_METAL__)
ccl_device_forceinline bool isnan_safe(const float f)
{
return isnan(f);
}
ccl_device_forceinline bool isfinite_safe(const float f)
{
return isfinite(f);
}
#else
template<typename T> ccl_device_inline uint pointer_pack_to_uint_0(T *ptr)
{
return ((uint64_t)ptr) & 0xFFFFFFFF;
}
template<typename T> ccl_device_inline uint pointer_pack_to_uint_1(T *ptr)
{
return (((uint64_t)ptr) >> 32) & 0xFFFFFFFF;
}
template<typename T> ccl_device_inline T *pointer_unpack_from_uint(const uint a, const uint b)
{
return (T *)(((uint64_t)b << 32) | a);
}
ccl_device_inline uint uint16_pack_to_uint(const uint a, const uint b)
{
return (a << 16) | b;
}
ccl_device_inline uint uint16_unpack_from_uint_0(const uint i)
{
return i >> 16;
}
ccl_device_inline uint uint16_unpack_from_uint_1(const uint i)
{
return i & 0xFFFF;
}
/* Versions of functions which are safe for fast math. */
ccl_device_inline bool isnan_safe(const float f)
{
const unsigned int x = __float_as_uint(f);
return (x << 1) > 0xff000000u;
}
ccl_device_inline bool isfinite_safe(const float f)
{
/* By IEEE 754 rule, 2*Inf equals Inf */
const unsigned int x = __float_as_uint(f);
return (f == f) && (x == 0 || x == (1u << 31) || (f != 2.0f * f)) && !((x << 1) > 0xff000000u);
}
#endif
ccl_device_inline float ensure_finite(const float v)
{
return isfinite_safe(v) ? v : 0.0f;
}
#if !defined(__KERNEL_METAL__)
ccl_device_inline int clamp(const int a, const int mn, const int mx)
{
return min(max(a, mn), mx);
}
ccl_device_inline float clamp(const float a, const float mn, const float mx)
{
return min(max(a, mn), mx);
}
ccl_device_inline float mix(const float a, const float b, float t)
{
return a + t * (b - a);
}
ccl_device_inline float smoothstep(const float edge0, const float edge1, const float x)
{
float result;
if (x < edge0) {
result = 0.0f;
}
else if (x >= edge1) {
result = 1.0f;
}
else {
const float t = (x - edge0) / (edge1 - edge0);
result = (3.0f - 2.0f * t) * (t * t);
}
return result;
}
#endif /* !defined(__KERNEL_METAL__) */
#if !defined(__KERNEL_METAL__)
ccl_device_inline float saturatef(const float a)
{
# ifdef __KERNEL_OPTIX__
/* Workaround OptiX driver bug which somehow rounds constant values to
* integers when using __saturatef. This particular logic works around the
* problem, just using clamp gets optimized back to saturate. See #159954. */
if (!(a >= 0.0f)) {
return 0.0f;
}
if (!(a <= 1.0f)) {
return 1.0f;
}
return a;
# elif defined(__KERNEL_CUDA__)
return __saturatef(a);
# else
return clamp(a, 0.0f, 1.0f);
# endif
}
#endif /* __KERNEL_CUDA__ */
ccl_device_inline int float_to_int(const float f)
{
return (int)f;
}
ccl_device_inline int floor_to_int(const float f)
{
return float_to_int(floorf(f));
}
ccl_device_inline float floorfrac(const float x, ccl_private int *i)
{
const float f = floorf(x);
*i = float_to_int(f);
return x - f;
}
ccl_device_inline int ceil_to_int(const float f)
{
return float_to_int(ceilf(f));
}
ccl_device_inline float fractf(const float x)
{
return x - floorf(x);
}
/* Adapted from `godot-engine` math_funcs.h. */
ccl_device_inline float wrapf(const float value, const float max, const float min)
{
const float range = max - min;
return (range != 0.0f) ? value - (range * floorf((value - min) / range)) : min;
}
ccl_device_inline float pingpongf(const float a, const float b)
{
return (b != 0.0f) ? fabsf(fractf((a - b) / (b * 2.0f)) * b * 2.0f - b) : 0.0f;
}
ccl_device_inline float smoothminf(const float a, const float b, float k)
{
if (k != 0.0f) {
const float h = fmaxf(k - fabsf(a - b), 0.0f) / k;
return fminf(a, b) - h * h * h * k * (1.0f / 6.0f);
}
return fminf(a, b);
}
ccl_device_inline float signf(const float f)
{
return (f < 0.0f) ? -1.0f : 1.0f;
}
ccl_device_inline float nonzerof(const float f, const float eps)
{
if (fabsf(f) < eps) {
return signf(f) * eps;
}
return f;
}
/* The behavior of `atan2(0, 0)` is undefined on many platforms, to ensure consistent behavior, we
* return 0 in this case. See !126951.
* Computes the angle between the positive x axis and the vector pointing from origin to (x, y). */
ccl_device_inline float compatible_atan2(const float y, const float x)
{
return (x == 0.0f && y == 0.0f) ? 0.0f : atan2f(y, x);
}
/* `signum` function testing for zero. Matches GLSL and OSL functions. */
ccl_device_inline float compatible_signf(const float f)
{
if (f == 0.0f) {
return 0.0f;
}
return signf(f);
}
ccl_device_inline float smoothstepf(const float f)
{
if (f <= 0.0f) {
return 0.0f;
}
if (f >= 1.0f) {
return 1.0f;
}
const float ff = f * f;
return (3.0f * ff - 2.0f * ff * f);
}
ccl_device_inline int mod(const int x, const int m)
{
return (x % m + m) % m;
}
ccl_device_inline float interp(const float a, const float b, const float t)
{
return a + t * (b - a);
}
ccl_device_inline float inverse_lerp(const float a, const float b, const float x)
{
return (x - a) / (b - a);
}
/* Cubic interpolation between b and c, a and d are the previous and next point. */
ccl_device_inline float cubic_interp(const float a, const float b, float c, const float d, float x)
{
return 0.5f *
(((d + 3.0f * (b - c) - a) * x + (2.0f * a - 5.0f * b + 4.0f * c - d)) * x +
(c - a)) *
x +
b;
}
/* NaN-safe math ops */
ccl_device_inline float safe_sqrtf(const float f)
{
return sqrtf(max(f, 0.0f));
}
ccl_device_inline float inversesqrtf(const float f)
{
#if defined(__KERNEL_METAL__)
return (f > 0.0f) ? rsqrt(f) : 0.0f;
#else
return (f > 0.0f) ? 1.0f / sqrtf(f) : 0.0f;
#endif
}
ccl_device float safe_asinf(const float a)
{
return asinf(clamp(a, -1.0f, 1.0f));
}
ccl_device float safe_acosf(const float a)
{
return acosf(clamp(a, -1.0f, 1.0f));
}
ccl_device float compatible_powf(const float x, const float y)
{
if (y == 0.0f) {
/* x^0 -> 1, including 0^0. */
return 1.0f;
}
if (x == 0.0f) {
return 0.0f;
}
#ifdef __KERNEL_GPU__
/* GPU pow doesn't accept negative x, do manual checks here */
if (x < 0.0f) {
if (fmodf(-y, 2.0f) == 0.0f) {
return powf(-x, y);
}
else {
return -powf(-x, y);
}
}
#endif
return powf(x, y);
}
ccl_device float safe_powf(const float a, const float b)
{
if (UNLIKELY(a < 0.0f && b != float_to_int(b))) {
return 0.0f;
}
return compatible_powf(a, b);
}
ccl_device float safe_divide(const float a, const float b)
{
return (b != 0.0f) ? a / b : 0.0f;
}
ccl_device float safe_logf(const float a, const float b)
{
if (UNLIKELY(a <= 0.0f || b <= 0.0f)) {
return 0.0f;
}
return safe_divide(logf(a), logf(b));
}
ccl_device float safe_modulo(const float a, const float b)
{
return (b != 0.0f) ? fmodf(a, b) : 0.0f;
}
ccl_device float safe_floored_modulo(const float a, const float b)
{
return (b != 0.0f) ? a - floorf(a / b) * b : 0.0f;
}
ccl_device_inline float sqr(const float a)
{
return a * a;
}
ccl_device_inline float sin_from_cos(const float c)
{
return safe_sqrtf(1.0f - sqr(c));
}
ccl_device_inline float cos_from_sin(const float s)
{
return safe_sqrtf(1.0f - sqr(s));
}
ccl_device_inline float sin_sqr_to_one_minus_cos(const float s_sq)
{
/* Using second-order Taylor expansion at small angles for better accuracy. */
return s_sq > 0.0004f ? 1.0f - safe_sqrtf(1.0f - s_sq) : 0.5f * s_sq;
}
ccl_device_inline float one_minus_cos(const float angle)
{
/* Using second-order Taylor expansion at small angles for better accuracy. */
return angle > 0.02f ? 1.0f - cosf(angle) : 0.5f * sqr(angle);
}
/* 2^a. */
ccl_device_inline int power_of_2(const int a)
{
return 1 << a;
}
ccl_device_inline float pow20(const float a)
{
return sqr(sqr(sqr(sqr(a)) * a));
}
ccl_device_inline float pow22(const float a)
{
return sqr(a * sqr(sqr(sqr(a)) * a));
}
#ifdef __KERNEL_METAL__
ccl_device_inline float lgammaf(const float x)
{
/* Nemes, Gergő (2010), "New asymptotic expansion for the Gamma function", Archiv der Mathematik
*/
const float _1_180 = 1.0f / 180.0f;
const float log2pi = 1.83787706641f;
const float logx = log(x);
return (log2pi - logx +
x * (logx * 2.0f + log(x * sinh(1.0f / x) + (_1_180 / pow(x, 6.0f))) - 2.0f)) *
0.5f;
}
#endif
ccl_device_inline float beta(const float x, const float y)
{
return expf(lgammaf(x) + lgammaf(y) - lgammaf(x + y));
}
ccl_device_inline float xor_mask(const float x, const uint y)
{
return __uint_as_float(__float_as_uint(x) ^ y);
}
ccl_device_inline float or_mask(const float x, const uint y)
{
return __uint_as_float(__float_as_uint(x) | y);
}
ccl_device float bits_to_01(const uint bits)
{
return bits * (1.0f / (float)0xFFFFFFFF);
}
ccl_device_inline bool is_zero(const float a)
{
return a == 0.0f;
}
#if !defined(__KERNEL_GPU__)
ccl_device_inline uint popcount(const uint x)
{
return std::popcount(x);
}
ccl_device_inline uint popcount(const uint64_t x)
{
return std::popcount(x);
}
#elif defined(__KERNEL_ONEAPI__)
# define popcount(x) sycl::popcount(x)
#elif defined(__KERNEL_HIP__)
/* Use popcll to support 64-bit wave for pre-RDNA AMD GPUs */
# define popcount(x) __popcll(x)
#elif !defined(__KERNEL_METAL__)
# define popcount(x) __popc(x)
#endif
ccl_device_inline uint count_leading_zeros(const uint x)
{
#if defined(__KERNEL_CUDA__) || defined(__KERNEL_OPTIX__) || defined(__KERNEL_HIP__)
return __clz(x);
#elif defined(__KERNEL_METAL__)
return clz(x);
#elif defined(__KERNEL_ONEAPI__)
return sycl::clz(x);
#else
assert(x != 0);
# ifdef _MSC_VER
unsigned long leading_zero = 0;
_BitScanReverse(&leading_zero, x);
return (31 - leading_zero);
# else
return __builtin_clz(x);
# endif
#endif
}
ccl_device_inline uint count_trailing_zeros(const uint x)
{
#if defined(__KERNEL_CUDA__) || defined(__KERNEL_OPTIX__) || defined(__KERNEL_HIP__)
return (__ffs(x) - 1);
#elif defined(__KERNEL_METAL__)
return ctz(x);
#elif defined(__KERNEL_ONEAPI__)
return sycl::ctz(x);
#else
assert(x != 0);
# ifdef _MSC_VER
unsigned long ctz = 0;
_BitScanForward(&ctz, x);
return ctz;
# else
return __builtin_ctz(x);
# endif
#endif
}
ccl_device_inline uint find_first_set(const uint x)
{
#if defined(__KERNEL_CUDA__) || defined(__KERNEL_OPTIX__) || defined(__KERNEL_HIP__)
return __ffs(x);
#elif defined(__KERNEL_METAL__)
return (x != 0) ? ctz(x) + 1 : 0;
#else
# ifdef _MSC_VER
return (x != 0) ? (32 - count_leading_zeros(x & (~x + 1))) : 0;
# else
return __builtin_ffs(x);
# endif
#endif
}
/* Compares two floats.
* Returns true if their absolute difference is smaller than abs_diff (for numbers near zero)
* or their relative difference is less than ulp_diff ULPs.
* Based on
* https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/
*/
ccl_device_inline bool compare_floats(const float a,
const float b,
float abs_diff,
const int ulp_diff)
{
if (fabsf(a - b) < abs_diff) {
return true;
}
if ((a < 0.0f) != (b < 0.0f)) {
return false;
}
return (abs(__float_as_int(a) - __float_as_int(b)) < ulp_diff);
}
/* Return value which is greater than the given one and is a power of two. */
ccl_device_inline uint next_power_of_two(const uint x)
{
return x == 0 ? 1 : 1 << (32 - count_leading_zeros(x));
}
/* Return value which is lower than the given one and is a power of two. */
ccl_device_inline uint prev_power_of_two(const uint x)
{
return x < 2 ? x : 1 << (31 - count_leading_zeros(x - 1));
}
#ifndef __has_builtin
# define __has_builtin(v) 0
#endif
/* Reverses the bits of a 32 bit integer. */
ccl_device_inline uint32_t reverse_integer_bits(uint32_t x)
{
/* Use a native instruction if it exists. */
#if defined(__KERNEL_CUDA__)
return __brev(x);
#elif defined(__KERNEL_METAL__)
return reverse_bits(x);
#elif defined(__aarch64__) || (defined(_M_ARM64) && !defined(_MSC_VER))
/* Assume the rbit is always available on 64bit ARM architecture. */
__asm__("rbit %w0, %w1" : "=r"(x) : "r"(x));
return x;
#elif defined(__arm__) && ((__ARM_ARCH > 7) || __ARM_ARCH == 6 && __ARM_ARCH_ISA_THUMB >= 2)
/* This ARM instruction is available in ARMv6T2 and above.
* This 32-bit Thumb instruction is available in ARMv6T2 and above. */
__asm__("rbit %0, %1" : "=r"(x) : "r"(x));
return x;
#elif __has_builtin(__builtin_bitreverse32)
return __builtin_bitreverse32(x);
#else
/* Flip pairwise. */
x = ((x & 0x55555555) << 1) | ((x & 0xAAAAAAAA) >> 1);
/* Flip pairs. */
x = ((x & 0x33333333) << 2) | ((x & 0xCCCCCCCC) >> 2);
/* Flip nibbles. */
x = ((x & 0x0F0F0F0F) << 4) | ((x & 0xF0F0F0F0) >> 4);
/* Flip bytes. CPUs have an instruction for that, pretty fast one. */
# ifdef _MSC_VER
return _byteswap_ulong(x);
# elif defined(__INTEL_COMPILER)
return (uint32_t)_bswap((int)x);
# else
/* Assuming gcc or clang. */
return __builtin_bswap32(x);
# endif
#endif
}
/* Solve quadratic equation a*x^2 + b*x + c = 0, adapted from Mitsuba 3
* The solution is ordered so that x1 <= x2.
* Returns true if at least one solution is found. */
ccl_device_inline bool solve_quadratic(
const float a, const float b, const float c, ccl_private float &x1, ccl_private float &x2)
{
/* If the equation is linear, the solution is -c/b, but b has to be non-zero. */
const bool valid_linear = (a == 0.0f) && (b != 0.0f);
x1 = x2 = -c / b;
const float discriminant = sqr(b) - 4.0f * a * c;
/* Allow slightly negative discriminant in case of numerical precision issues. */
const bool valid_quadratic = (a != 0.0f) && (discriminant > -1e-5f);
if (valid_quadratic) {
/* Numerically stable version of (-b ± sqrt(discriminant)) / (2 * a), avoiding catastrophic
* cancellation when `b` is very close to `sqrt(discriminant)`, by finding the solution of
* greater magnitude which does not suffer from loss of precision, then using the identity
* x1 * x2 = c / a. */
const float temp = -0.5f * (b + copysignf(safe_sqrtf(discriminant), b));
const float r1 = temp / a;
const float r2 = c / temp;
x1 = fminf(r1, r2);
x2 = fmaxf(r1, r2);
}
return (valid_linear || valid_quadratic);
}
/* Defines a closed interval [min, max]. */
template<typename T> struct Interval {
T min;
T max;
ccl_device_inline_method bool is_empty() const
{
/* NaN-safe comparison. */
return !(min < max);
}
ccl_device_inline_method bool contains(T value) const
{
return value >= min && value <= max;
}
ccl_device_inline_method T length() const
{
return max - min;
}
};
template<typename T1, typename T2>
ccl_device_inline Interval<T1> operator/=(ccl_private Interval<T1> &interval, const T2 f)
{
interval.min /= f;
interval.max /= f;
return interval;
}
/* Computes the intersection of two intervals. */
template<typename T>
ccl_device_inline Interval<T> intervals_intersection(const ccl_private Interval<T> &first,
const ccl_private Interval<T> &second)
{
return {max(first.min, second.min), min(first.max, second.max)};
}
/* Defines the minimal and maximal values of a quantity. */
template<typename T> struct Extrema {
T min;
T max;
Extrema() = default;
ccl_device_inline_method Extrema(T value) : min(value), max(value) {}
ccl_device_inline_method Extrema(T min_, T max_) : min(min_), max(max_) {}
ccl_device_inline_method T range() const
{
return max - min;
}
};
template<typename T> ccl_device_inline Extrema<T> operator*(const Extrema<T> a, const T b)
{
return {a.min * b, a.max * b};
}
template<typename T>
ccl_device_inline Extrema<T> operator+(const ccl_private Extrema<T> &a,
const ccl_private Extrema<T> &b)
{
return {a.min + b.min, a.max + b.max};
}
template<typename T>
ccl_device_inline Extrema<T> operator+=(ccl_private Extrema<T> &a, const ccl_private Extrema<T> &b)
{
return a = a + b;
}
/* Returns the extrema of both extrema. */
template<typename T>
ccl_device_inline Extrema<T> merge(const ccl_private Extrema<T> &a,
const ccl_private Extrema<T> &b)
{
return {min(a.min, b.min), max(a.max, b.max)};
}
template<typename T>
ccl_device_inline Extrema<T> merge(const ccl_private Extrema<T> &a, const ccl_private T &v)
{
return {min(a.min, v), max(a.max, v)};
}
CCL_NAMESPACE_END

View File

@@ -0,0 +1,62 @@
/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#include "util/math_cdf.h"
#include <cassert>
#include "util/algorithm.h"
CCL_NAMESPACE_BEGIN
/* Invert pre-calculated CDF function. */
void util_cdf_invert(const int resolution,
const float from,
const float to,
const vector<float> &cdf,
const bool make_symmetric,
vector<float> &inv_cdf)
{
const int cdf_size = cdf.size();
assert(cdf[0] == 0.0f && cdf[cdf_size - 1] == 1.0f);
const float inv_resolution = 1.0f / (float)resolution;
const float range = to - from;
inv_cdf.resize(resolution);
if (make_symmetric) {
const int half_size = (resolution - 1) / 2;
for (int i = 0; i <= half_size; i++) {
const float x = i / (float)half_size;
int index = upper_bound(cdf.begin(), cdf.end(), x) - cdf.begin();
float t;
if (index < cdf_size - 1) {
t = (x - cdf[index]) / (cdf[index + 1] - cdf[index]);
}
else {
t = 0.0f;
index = cdf_size - 1;
}
const float y = ((index + t) / (resolution - 1)) * (2.0f * range);
inv_cdf[half_size + i] = 0.5f * (1.0f + y);
inv_cdf[half_size - i] = 0.5f * (1.0f - y);
}
}
else {
for (int i = 0; i < resolution; i++) {
const float x = (i + 0.5f) * inv_resolution;
int index = upper_bound(cdf.begin(), cdf.end(), x) - cdf.begin() - 1;
float t;
if (index < cdf_size - 1) {
t = (x - cdf[index]) / (cdf[index + 1] - cdf[index]);
}
else {
t = 0.0f;
index = resolution;
}
inv_cdf[i] = from + range * (index + t) * inv_resolution;
}
}
}
CCL_NAMESPACE_END

View File

@@ -0,0 +1,61 @@
/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#pragma once
#include "util/math_base.h"
#include "util/vector.h"
CCL_NAMESPACE_BEGIN
/* Evaluate CDF of a given functor with given range and resolution. */
template<typename Functor>
void util_cdf_evaluate(
const int resolution, const float from, const float to, Functor functor, vector<float> &cdf)
{
const int cdf_count = resolution + 1;
const float range = to - from;
cdf.resize(cdf_count);
cdf[0] = 0.0f;
/* Actual CDF evaluation. */
for (int i = 0; i < resolution; ++i) {
float x = from + range * (float)i / (resolution - 1);
float y = functor(x);
cdf[i + 1] = cdf[i] + fabsf(y);
}
/* Normalize the CDF. */
const float fac = (cdf[resolution] == 0.0f) ? 0.0f : 1.0f / cdf[resolution];
for (int i = 0; i <= resolution; i++) {
cdf[i] *= fac;
}
cdf[resolution] = 1.0f;
}
/* Invert pre-calculated CDF function. */
void util_cdf_invert(const int resolution,
const float from,
const float to,
const vector<float> &cdf,
const bool make_symmetric,
vector<float> &inv_cdf);
/* Evaluate inverted CDF of a given functor with given range and resolution. */
template<typename Functor>
void util_cdf_inverted(const int resolution,
const float from,
const float to,
Functor functor,
const bool make_symmetric,
vector<float> &inv_cdf)
{
vector<float> cdf;
/* There is no much smartness going around lower resolution for the CDF table,
* this just to match the old code from pixel filter so it all stays exactly
* the same and no regression tests are failed.
*/
util_cdf_evaluate(resolution - 1, from, to, functor, cdf);
util_cdf_invert(resolution, from, to, cdf, make_symmetric, inv_cdf);
}
CCL_NAMESPACE_END

View File

@@ -0,0 +1,431 @@
/* SPDX-FileCopyrightText: 2025 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#pragma once
#include "util/math_base.h"
#include "util/math_float3.h"
#include "util/types_dual.h"
#include "util/types_float3.h"
CCL_NAMESPACE_BEGIN
ccl_device_template_spec dual1 make_zero()
{
return dual1();
}
ccl_device_template_spec dual2 make_zero()
{
return dual2();
}
ccl_device_template_spec dual3 make_zero()
{
return dual3();
}
ccl_device_template_spec dual4 make_zero()
{
return dual4();
}
template<class T> ccl_device_inline bool is_zero(const ccl_private dual<T> &a)
{
return is_zero(a.val);
}
ccl_device_inline bool operator<(const ccl_private dual1 &a, const float b)
{
return a.val < b;
}
/* Multiplication of dual by scalar. */
template<class T1, class T2> ccl_device_inline dual<T1> operator*(const dual<T1> a, T2 b)
{
return {a.val * b, a.dx * b, a.dy * b};
}
/* Multiplication of scalar by dual. */
template<class T> ccl_device_inline dual<T> operator*(const T a, const ccl_private dual<T> &b)
{
return {a * b.val, a * b.dx, a * b.dy};
}
/* Multiplication of duals.
* `(uv)' = uv' + u'v`. */
template<class T1, class T2>
ccl_device_inline dual<T1> operator*(const ccl_private dual<T1> &u, const ccl_private dual<T2> &v)
{
return {u.val * v.val, u.val * v.dx + u.dx * v.val, u.val * v.dy + u.dy * v.val};
}
/* Division of dual by scalar. */
template<class T> ccl_device_inline dual<T> operator/(const dual<T> a, T b)
{
const T inv_b = 1.0f / b;
return {a.val * inv_b, a.dx * inv_b, a.dy * inv_b};
}
/* Division of dual by dual.
* `(u/v)' = (u' - v' * u/v) / v`. */
template<class T1, class T2>
ccl_device_inline dual<T1> operator/(const ccl_private dual<T1> &u, const ccl_private dual<T2> &v)
{
const T2 inv_v = 1.0f / v.val;
/* NOTE: Numerically `u/v != u*inv_v`, for compatibility we compute `u/v`. */
const T1 u_v = u.val / v.val;
return {u_v, (u.dx - u_v * v.dx) * inv_v, (u.dy - u_v * v.dy) * inv_v};
}
template<class T1, class T2>
ccl_device_inline dual<T1> operator/=(ccl_private dual<T1> &a, const ccl_private dual<T2> &b)
{
return a = a / b;
}
/* Addition of duals. */
template<class T> ccl_device_inline dual<T> operator+(const dual<T> a, const dual<T> b)
{
return {a.val + b.val, a.dx + b.dx, a.dy + b.dy};
}
/* Addition of dual and scalar. */
template<class T1, class T2> ccl_device_inline dual<T1> operator+(const dual<T1> a, T2 b)
{
return {a.val + b, a.dx, a.dy};
}
/* Addition of scalar and dual. */
template<class T1, class T2> ccl_device_inline dual<T2> operator+(const T1 a, const dual<T2> b)
{
return {a + b.val, b.dx, b.dy};
}
/* Subtraction of dual by scalar. */
template<class T1, class T2> ccl_device_inline dual<T1> operator-(const dual<T1> a, T2 b)
{
return {a.val - b, a.dx, a.dy};
}
/* Subtraction of scalar by dual. */
template<class T1, class T2> ccl_device_inline dual<T2> operator-(const T1 a, const dual<T2> b)
{
return {a - b.val, -b.dx, -b.dy};
}
/* Subtraction of duals. */
template<class T>
ccl_device_inline dual<T> operator-(const ccl_private dual<T> &a, const ccl_private dual<T> &b)
{
return {a.val - b.val, a.dx - b.dx, a.dy - b.dy};
}
/* Negation. */
template<class T> ccl_device_inline dual<T> operator-(const ccl_private dual<T> &a)
{
return {-a.val, -a.dx, -a.dy};
}
/* `dfdx = dfdu * dudx`. */
template<class T>
ccl_device_inline dual<T> chain_rule(const ccl_private dual<T> &u,
const ccl_private T &f,
const ccl_private T &dfdu)
{
return {f, dfdu * u.dx, dfdu * u.dy};
}
/* `dfdx = dfdu * dudx + dfdv * dvdx`. */
template<class T>
ccl_device_inline dual<T> chain_rule(const ccl_private dual<T> &u,
const ccl_private dual<T> &v,
const ccl_private T &f,
const ccl_private T &dfdu,
const ccl_private T &dfdv)
{
return {f, dfdu * u.dx + dfdv * v.dx, dfdu * u.dy + dfdv * v.dy};
}
template<class MaskType>
ccl_device_inline dual3 select(const MaskType mask, const dual3 a, const dual3 b)
{
#if defined(__KERNEL_METAL__)
const bool3 mask_ = bool3(mask);
return {metal::select(b.val, a.val, mask_),
metal::select(b.dx, a.dx, mask_),
metal::select(b.dy, a.dy, mask_)};
#elif defined(__KERNEL_SSE__)
# ifdef __KERNEL_SSE42__
const auto mask_ = _mm_castsi128_ps(mask.m128);
return {float3(_mm_blendv_ps(b.val.m128, a.val.m128, mask_)),
float3(_mm_blendv_ps(b.dx.m128, a.dx.m128, mask_)),
float3(_mm_blendv_ps(b.dy.m128, a.dy.m128, mask_))};
# else
const auto mask_ = _mm_castsi128_ps(mask);
return {float3(_mm_or_ps(_mm_and_ps(mask_, a.val), _mm_andnot_ps(mask_, b.val))),
float3(_mm_or_ps(_mm_and_ps(mask_, a.dx), _mm_andnot_ps(mask_, b.dx))),
float3(_mm_or_ps(_mm_and_ps(mask_, a.dy), _mm_andnot_ps(mask_, b.dy)))};
# endif
#else
return make_float3(mask.x ? a.x() : b.x(), mask.y ? a.y() : b.y(), mask.z ? a.z() : b.z());
#endif
}
/* Functions with zero derivatives. */
template<class T> ccl_device_inline dual<T> floor(const ccl_private dual<T> &a)
{
return dual<T>(floor(a.val));
}
template<class T> ccl_device_inline dual<T> ceil(const ccl_private dual<T> &a)
{
return dual<T>(ceil(a.val));
}
template<class T> ccl_device_inline dual<T> compatible_sign(const ccl_private dual<T> &u)
{
return dual<T>(compatible_sign(u.val));
}
/* f = u - round(u / v) * v, f' = u'. */
ccl_device_inline dual3 safe_fmod(const dual3 u, const dual3 v)
{
return {safe_fmod(u.val, v.val), u.dx, u.dy};
}
ccl_device_inline dual3 safe_floored_fmod(const dual3 a, const dual3 b)
{
return select(component_is_zero(b.val), make_zero<dual3>(), a - floor(a.val / b.val) * b);
}
template<class T> ccl_device_inline dual<T> safe_divide(const dual<T> f, const T g)
{
return select(component_is_zero(g), make_zero<dual<T>>(), f / g);
}
template<class T>
ccl_device_inline dual<T> safe_divide(const ccl_private dual<T> &f, const ccl_private dual<T> &g)
{
return select(component_is_zero(g.val), make_zero<dual<T>>(), f / g);
}
/* Adapted from GODOT-engine math_funcs.h. */
ccl_device_inline dual3 wrap(const dual3 value, const dual3 max, const dual3 min)
{
return safe_floored_fmod(value - min, max - min) + min;
}
ccl_device_inline dual3 min(const ccl_private dual3 &a, const ccl_private dual3 &b)
{
return select(a.val < b.val, a, b);
}
ccl_device_inline dual3 max(const ccl_private dual3 &a, const ccl_private dual3 &b)
{
return select(a.val > b.val, a, b);
}
ccl_device_inline dual1 max(const ccl_private dual1 &a, const ccl_private dual1 &b)
{
return a.val > b.val ? a : b;
}
ccl_device_inline dual3 fabs(const ccl_private dual3 &a)
{
return select(a.val > zero_float3(), a, -a);
}
template<class T> ccl_device_inline dual1 average(const dual<T> a)
{
return {average(a.val), average(a.dx), average(a.dy)};
}
template<class T> ccl_device_inline dual1 reduce_add(const dual<T> a)
{
return {reduce_add(a.val), reduce_add(a.dx), reduce_add(a.dy)};
}
/* f(u) = sqrt(u), dfdu = 1 / (2 * sqrt(u)). */
ccl_device_inline dual1 sqrt(const ccl_private dual1 &u)
{
const float f = sqrtf(u.val);
return chain_rule(u, f, 0.5f / f);
}
template<class T> ccl_device_inline dual1 len(const ccl_private dual<T> &a)
{
return sqrt(dot(a, a));
}
template<class T1, class T2> ccl_device_inline dual1 dot(const dual<T1> a, const T2 b)
{
return reduce_add(a * b);
}
template<class T> ccl_device_inline dual1 len_squared(const ccl_private dual<T> &a)
{
return dot(a, a);
}
template<class T>
ccl_device_inline dual1 distance(const ccl_private dual<T> &a, const ccl_private dual<T> &b)
{
return len(a - b);
}
ccl_device_inline dual3 cross(const ccl_private dual3 &a, const ccl_private dual3 &b)
{
return {cross(a.val, b.val),
cross(a.val, b.dx) + cross(a.dx, b.val),
cross(a.val, b.dy) + cross(a.dy, b.val)};
}
ccl_device_inline dual3 cross(const ccl_private dual3 &a, const ccl_private float3 &b)
{
return {cross(a.val, b), cross(a.dx, b), cross(a.dy, b)};
}
ccl_device_inline dual3 cross(const ccl_private float3 &a, const ccl_private dual3 &b)
{
return -cross(b, a);
}
/* f(u) = 1 / sqrt(u), dfdu = -1 / (2 * u^(3/2)). */
ccl_device_inline dual1 inversesqrt(const ccl_private dual1 &u)
{
const float f = inversesqrtf(u.val);
return chain_rule(u, f, -0.5f * safe_divide(f, u.val));
}
template<class T> ccl_device_inline dual<T> normalize(const ccl_private dual<T> &a)
{
return a * inversesqrt(len_squared(a));
}
template<class T> ccl_device_inline dual<T> safe_normalize(const ccl_private dual<T> &a)
{
const dual1 len_sq = len_squared(a);
return is_zero(len_sq) ? make_zero<dual<T>>() : a * inversesqrt(len_sq);
}
/* f(y, x) = atan2(y, x),
* dfdx = -y / (x^2 + y^2),
* dfdy = x / (x^2 + y^2) */
ccl_device_inline dual1 atan2(const ccl_private dual1 &y, const ccl_private dual1 &x)
{
const float inv_len = safe_divide(1.0f, sqr(x.val) + sqr(y.val));
const float dfdx = -y.val * inv_len;
const float dfdy = x.val * inv_len;
return chain_rule(x, y, atan2f(y.val, x.val), dfdx, dfdy);
}
/* f(u) = acos(u), dfdu = -1 / sqrt(1 - u^2). */
ccl_device_inline dual1 acos(const ccl_private dual1 &u)
{
return chain_rule(u, acosf(u.val), -inversesqrtf(1.0f - sqr(u.val)));
}
ccl_device_inline dual1 safe_acos(const ccl_private dual1 &u)
{
const float dfdu = (fabsf(u.val) >= 1.0f) ? 0.0f : -inversesqrtf(1.0f - sqr(u.val));
return chain_rule(u, safe_acosf(u.val), dfdu);
}
template<class T> ccl_device_inline dual3 reflect(const dual3 incident, const T unit_normal)
{
return incident - unit_normal * make_float3(dot(incident, unit_normal)) * 2.0f;
}
ccl_device_inline dual3 refract(const dual3 incident, const dual3 normal, const dual1 eta)
{
const dual1 NI = dot(incident, normal);
const dual1 k = 1.0f - eta * eta * (1.0f - NI * NI);
if (k.val < 0.0f) {
return dual3();
}
return incident * eta - normal * (eta * NI + sqrt(k));
}
ccl_device_inline dual3 faceforward(const dual3 vector,
const dual3 incident,
const dual3 reference)
{
return (dot(reference, incident) < 0.0f) ? vector : -vector;
}
ccl_device_inline dual3 project(const dual3 v, const dual3 v_proj)
{
const dual1 len_squared = dot(v_proj, v_proj);
return (len_squared.val != 0.0f) ? v_proj * (dot(v, v_proj) / len_squared) : dual3();
}
template<class T> ccl_device_inline dual<T> sin(const ccl_private dual<T> &x)
{
T sinx, cosx;
sincos(x.val, &sinx, &cosx);
return chain_rule(x, sinx, cosx);
}
template<class T> ccl_device_inline dual<T> cos(const ccl_private dual<T> &x)
{
T sinx, cosx;
sincos(x.val, &sinx, &cosx);
return chain_rule(x, cosx, -sinx);
}
ccl_device_inline dual3 tan(const ccl_private dual3 &x)
{
const float3 tanx = tan(x.val);
const float3 secx = safe_divide(one_float3(), cos(x.val));
return chain_rule(x, tanx, sqr(secx));
}
/* f(u, v) = u^v, dfdu = v u^(v-1), dfdv = u^v ln(u). */
template<class T>
ccl_device_inline dual<T> safe_pow(const ccl_private dual<T> &u, const ccl_private dual<T> &v)
{
/* u^(v-1). */
const T u_v_minus_1 = safe_pow(u.val, v.val - 1.0f);
/* u^v = u * u^(v-1). */
/* NOTE: numerically `u^v != u*u^(v-1)`, but the current behavior matches OSL. */
const T f = u.val * u_v_minus_1;
return chain_rule(u, v, f, v.val * u_v_minus_1, f * safe_log(u.val));
}
/* Projections. */
ccl_device_inline dual2 map_to_tube(const dual3 co)
{
dual1 u, v;
const dual1 length = len(make_float2(co));
if (length.val > 0.0f) {
u = (1.0f - (atan2(co.x(), co.y()) / M_PI_F)) * 0.5f;
v = (co.z() + 1.0f) * 0.5f;
}
else {
u = v = make_zero<dual1>();
}
return make_float2(u, v);
}
ccl_device_inline dual2 map_to_sphere(const dual3 co)
{
const dual1 l = dot(co, co);
dual1 u, v;
if (l.val > 0.0f) {
if (UNLIKELY(co.val.x == 0.0f && co.val.y == 0.0f)) {
u = make_zero<dual1>(); /* Otherwise domain error. */
}
else {
u = (0.5f - atan2(co.x(), co.y()) * M_1_2PI_F);
}
v = 1.0f - safe_acos(co.z() * inversesqrt(l)) * M_1_PI_F;
}
else {
u = v = make_zero<dual1>();
}
return make_float2(u, v);
}
CCL_NAMESPACE_END

View File

@@ -0,0 +1,650 @@
/* SPDX-FileCopyrightText: 2004 NVIDIA Corporation
* SPDX-FileCopyrightText: 2008-2014 Larry Gritz
* SPDX-FileCopyrightText: 2009-2014 Sony Pictures Imageworks Inc., et al.
*
* SPDX-License-Identifier: BSD-3-Clause
*
* Adapted from OpenImageIO
* Copyright 2008-2014 Larry Gritz and the other authors and contributors.
* All Rights Reserved.
*
* A few bits here are based upon code from NVIDIA that was also released
* under the same modified BSD license, and marked as:
* `Copyright 2004 NVIDIA Corporation. All Rights Reserved.`
*
* Some parts of this file were first open-sourced in Open Shading Language,
* then later moved here. The original copyright notice was:
* `Copyright (c) 2009-2014 Sony Pictures Imageworks Inc., et al.`
*
* Many of the math functions were copied from or inspired by other
* public domain sources or open source packages with compatible licenses.
* The individual functions give references were applicable.
*/
#pragma once
#include "util/math_base.h"
#include "util/math_float3.h"
#include "util/math_float4.h"
#include "util/math_int4.h"
#include "util/types_float3.h"
#include "util/types_float4.h"
CCL_NAMESPACE_BEGIN
ccl_device_inline float madd(const float a, const float b, const float c)
{
/* NOTE: In the future we may want to explicitly ask for a fused
* multiply-add in a specialized version for float.
*
* NOTE: GCC/ICC will turn this (for float) into a FMA unless
* explicitly asked not to, clang seems to leave the code alone.
*/
return a * b + c;
}
ccl_device_inline float4 madd4(const float4 a, const float4 b, const float4 c)
{
return a * b + c;
}
/*
* FAST & APPROXIMATE MATH
*
* The functions named "fast_*" provide a set of replacements to `libm` that
* are much faster at the expense of some accuracy and robust handling of
* extreme values. One design goal for these approximation was to avoid
* branches as much as possible and operate on single precision values only
* so that SIMD versions should be straightforward ports We also try to
* implement "safe" semantics (ie: clamp to valid range where possible)
* natively since wrapping these inline calls in another layer would be
* wasteful.
*
* Some functions are fast_safe_*, which is both a faster approximation as
* well as clamped input domain to ensure no NaN, Inf, or divide by zero.
*/
/* Round to nearest integer, returning as an int. */
ccl_device_inline int fast_rint(const float x)
{
/* used by sin/cos/tan range reduction. */
#ifdef __KERNEL_SSE42__
/* Single `roundps` instruction on SSE4.1+ for gcc/clang but not MSVC 19.35:
* float_to_int(rintf(x)); so we use the equivalent intrinsics. */
__m128 vec = _mm_set_ss(x);
vec = _mm_round_ss(vec, vec, (_MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC));
return _mm_cvtss_si32(vec);
#else
/* emulate rounding by adding/subtracting 0.5. */
return float_to_int(x + copysignf(0.5f, x));
#endif
}
ccl_device float fast_sinf(float x)
{
/* Very accurate argument reduction from SLEEF,
* starts failing around x=262000
*
* Results on: [-2pi,2pi].
*
* Examined 2173837240 values of sin: 0.00662760244 avg ULP diff, 2 max ULP,
* 1.19209e-07 max error
*/
const int q = fast_rint(x * M_1_PI_F);
const float qf = (float)q;
x = madd(qf, -0.78515625f * 4, x);
x = madd(qf, -0.00024187564849853515625f * 4, x);
x = madd(qf, -3.7747668102383613586e-08f * 4, x);
x = madd(qf, -1.2816720341285448015e-12f * 4, x);
x = M_PI_2_F - (M_PI_2_F - x); /* Crush denormals */
const float s = x * x;
if ((q & 1) != 0) {
x = -x;
}
/* This polynomial approximation has very low error on [-pi/2,+pi/2]
* 1.19209e-07 max error in total over [-2pi,+2pi]. */
float u = 2.6083159809786593541503e-06f;
u = madd(u, s, -0.0001981069071916863322258f);
u = madd(u, s, +0.00833307858556509017944336f);
u = madd(u, s, -0.166666597127914428710938f);
u = madd(s, u * x, x);
/* For large x, the argument reduction can fail and the polynomial can be
* evaluated with arguments outside the valid internal. Just clamp the bad
* values away. */
u = clamp(u, -1.0f, 1.0f);
return u;
}
ccl_device float fast_cosf(float x)
{
/* Same argument reduction as fast_sinf(). */
const int q = fast_rint(x * M_1_PI_F);
const float qf = (float)q;
x = madd(qf, -0.78515625f * 4, x);
x = madd(qf, -0.00024187564849853515625f * 4, x);
x = madd(qf, -3.7747668102383613586e-08f * 4, x);
x = madd(qf, -1.2816720341285448015e-12f * 4, x);
x = M_PI_2_F - (M_PI_2_F - x); /* Crush denormals. */
const float s = x * x;
/* Polynomial from SLEEF's sincosf, max error is
* 4.33127e-07 over [-2pi,2pi] (98% of values are "exact"). */
float u = -2.71811842367242206819355e-07f;
u = madd(u, s, +2.47990446951007470488548e-05f);
u = madd(u, s, -0.00138888787478208541870117f);
u = madd(u, s, +0.0416666641831398010253906f);
u = madd(u, s, -0.5f);
u = madd(u, s, +1.0f);
if ((q & 1) != 0) {
u = -u;
}
u = clamp(u, -1.0f, 1.0f);
return u;
}
ccl_device void fast_sincosf(float x, ccl_private float *sine, ccl_private float *cosine)
{
/* Same argument reduction as fast_sin. */
const int q = fast_rint(x * M_1_PI_F);
const float qf = (float)q;
x = madd(qf, -0.78515625f * 4, x);
x = madd(qf, -0.00024187564849853515625f * 4, x);
x = madd(qf, -3.7747668102383613586e-08f * 4, x);
x = madd(qf, -1.2816720341285448015e-12f * 4, x);
x = M_PI_2_F - (M_PI_2_F - x); // crush denormals
const float s = x * x;
/* NOTE: same exact polynomials as fast_sinf() and fast_cosf() above. */
if ((q & 1) != 0) {
x = -x;
}
float su = 2.6083159809786593541503e-06f;
su = madd(su, s, -0.0001981069071916863322258f);
su = madd(su, s, +0.00833307858556509017944336f);
su = madd(su, s, -0.166666597127914428710938f);
su = madd(s, su * x, x);
float cu = -2.71811842367242206819355e-07f;
cu = madd(cu, s, +2.47990446951007470488548e-05f);
cu = madd(cu, s, -0.00138888787478208541870117f);
cu = madd(cu, s, +0.0416666641831398010253906f);
cu = madd(cu, s, -0.5f);
cu = madd(cu, s, +1.0f);
if ((q & 1) != 0) {
cu = -cu;
}
su = clamp(su, -1.0f, 1.0f);
cu = clamp(cu, -1.0f, 1.0f);
*sine = su;
*cosine = cu;
}
/* NOTE: this approximation is only valid on [-8192.0,+8192.0], it starts
* becoming really poor outside of this range because the reciprocal amplifies
* errors.
*/
ccl_device float fast_tanf(float x)
{
/* Derived from SLEEF implementation.
*
* Note that we cannot apply the "denormal crush" trick everywhere because
* we sometimes need to take the reciprocal of the polynomial
*/
const int q = fast_rint(x * 2.0f * M_1_PI_F);
const float qf = (float)q;
x = madd(qf, -0.78515625f * 2, x);
x = madd(qf, -0.00024187564849853515625f * 2, x);
x = madd(qf, -3.7747668102383613586e-08f * 2, x);
x = madd(qf, -1.2816720341285448015e-12f * 2, x);
if ((q & 1) == 0) {
/* Crush denormals (only if we aren't inverting the result later). */
x = M_PI_4_F - (M_PI_4_F - x);
}
const float s = x * x;
float u = 0.00927245803177356719970703f;
u = madd(u, s, 0.00331984995864331722259521f);
u = madd(u, s, 0.0242998078465461730957031f);
u = madd(u, s, 0.0534495301544666290283203f);
u = madd(u, s, 0.133383005857467651367188f);
u = madd(u, s, 0.333331853151321411132812f);
u = madd(s, u * x, x);
if ((q & 1) != 0) {
u = -1.0f / u;
}
return u;
}
/* Fast, approximate sin(x*M_PI) with maximum absolute error of 0.000918954611.
*
* Adapted from http://devmaster.net/posts/9648/fast-and-accurate-sine-cosine#comment-76773
*/
ccl_device float fast_sinpif(const float x)
{
/* Fast trick to strip the integral part off, so our domain is [-1, 1]. */
const float z = x - ((x + 25165824.0f) - 25165824.0f);
const float y = z - z * fabsf(z);
const float Q = 3.10396624f;
const float P = 3.584135056f; /* P = 16-4*Q */
return y * (Q + P * fabsf(y));
/* The original article used inferior constants for Q and P and
* so had max error 1.091e-3.
*
* The optimal value for Q was determined by exhaustive search, minimizing
* the absolute numerical error relative to float(std::sin(double(phi*M_PI)))
* over the interval [0,2] (which is where most of the invocations happen).
*
* The basic idea of this approximation starts with the coarse approximation:
* sin(pi*x) ~= f(x) = 4 * (x - x * abs(x))
*
* This approximation always _over_ estimates the target. On the other hand,
* the curve:
* sin(pi*x) ~= f(x) * abs(f(x)) / 4
*
* always lies _under_ the target. Thus we can simply numerically search for
* the optimal constant to LERP these curves into a more precise
* approximation.
*
* After folding the constants together and simplifying the resulting math,
* we end up with the compact implementation above.
*
* NOTE: this function actually computes sin(x * pi) which avoids one or two
* mults in many cases and guarantees exact values at integer periods.
*/
}
/* Fast approximate cos(x*M_PI) with ~0.1% absolute error. */
ccl_device_inline float fast_cospif(const float x)
{
return fast_sinpif(x + 0.5f);
}
ccl_device float fast_acosf(const float x)
{
const float f = fabsf(x);
/* clamp and crush denormals. */
const float m = (f < 1.0f) ? 1.0f - (1.0f - f) : 1.0f;
/* Based on http://www.pouet.net/topic.php?which=9132&page=2
* 85% accurate (ULP 0)
* Examined 2130706434 values of acos:
* 15.2000597 avg ULP diff, 4492 max ULP, 4.51803e-05 max error // without "denormal crush"
* Examined 2130706434 values of acos:
* 15.2007108 avg ULP diff, 4492 max ULP, 4.51803e-05 max error // with "denormal crush"
*/
const float a = sqrtf(1.0f - m) *
(1.5707963267f + m * (-0.213300989f + m * (0.077980478f + m * -0.02164095f)));
return x < 0 ? M_PI_F - a : a;
}
ccl_device float fast_asinf(const float x)
{
/* Based on acosf approximation above.
* Max error is 4.51133e-05 (ULPS are higher because we are consistently off
* by a little amount). */
const float f = fabsf(x);
/* Clamp and crush denormals. */
const float m = (f < 1.0f) ? 1.0f - (1.0f - f) : 1.0f;
const float a = M_PI_2_F -
sqrtf(1.0f - m) * (1.5707963267f +
m * (-0.213300989f + m * (0.077980478f + m * -0.02164095f)));
return copysignf(a, x);
}
ccl_device float fast_atanf(const float x)
{
const float a = fabsf(x);
const float k = a > 1.0f ? 1 / a : a;
const float s = 1.0f - (1.0f - k); /* Crush denormals. */
const float t = s * s;
/* http://mathforum.org/library/drmath/view/62672.html
* Examined 4278190080 values of atan:
* 2.36864877 avg ULP diff, 302 max ULP, 6.55651e-06 max error // (with denormals)
* Examined 4278190080 values of atan:
* 171160502 avg ULP diff, 855638016 max ULP, 6.55651e-06 max error // (crush denormals)
*/
float r = s * madd(0.43157974f, t, 1.0f) / madd(madd(0.05831938f, t, 0.76443945f), t, 1.0f);
if (a > 1.0f) {
r = M_PI_2_F - r;
}
return copysignf(r, x);
}
ccl_device float fast_atan2f(const float y, const float x)
{
/* Based on atan approximation above.
*
* The special cases around 0 and infinity were tested explicitly.
*
* The only case not handled correctly is x=NaN,y=0 which returns 0 instead
* of nan.
*/
const float a = fabsf(x);
const float b = fabsf(y);
const float k = (b == 0) ? 0.0f : ((a == b) ? 1.0f : (b > a ? a / b : b / a));
const float s = 1.0f - (1.0f - k); /* Crush denormals */
const float t = s * s;
float r = s * madd(0.43157974f, t, 1.0f) / madd(madd(0.05831938f, t, 0.76443945f), t, 1.0f);
if (b > a) {
/* Account for arg reduction. */
r = M_PI_2_F - r;
}
/* Test sign bit of x. */
if (__float_as_uint(x) & 0x80000000u) {
r = M_PI_F - r;
}
return copysignf(r, y);
}
/* Same as precise_angle, but using fast_atan2f. Still much better that acos(dot(a, b)). */
ccl_device_inline float vector_angle(const float3 a, const float3 b)
{
return 2.0f * fast_atan2f(len(a - b), len(a + b));
}
ccl_device_inline int floor_log2f(const float x)
{
return (int)(__float_as_uint(x) >> 23) - 127;
}
/* Based on:
*
* https://github.com/LiraNuna/glsl-sse2/blob/master/source/vec4.h
*/
ccl_device float fast_log2f(float x)
{
/* NOTE: clamp to avoid special cases and make result "safe" from large
* negative values/NAN's. */
x = clamp(x, FLT_MIN, FLT_MAX);
const unsigned bits = __float_as_uint(x);
const int exponent = floor_log2f(x);
const float f = __uint_as_float((bits & 0x007FFFFF) | 0x3f800000) - 1.0f;
/* Examined 2130706432 values of log2 on [1.17549435e-38,3.40282347e+38]:
* 0.0797524457 avg ULP diff, 3713596 max ULP, 7.62939e-06 max error.
* ULP histogram:
* 0 = 97.46%
* 1 = 2.29%
* 2 = 0.11%
*/
const float f2 = f * f;
const float f4 = f2 * f2;
float hi = madd(f, -0.00931049621349f, 0.05206469089414f);
float lo = madd(f, 0.47868480909345f, -0.72116591947498f);
hi = madd(f, hi, -0.13753123777116f);
hi = madd(f, hi, 0.24187369696082f);
hi = madd(f, hi, -0.34730547155299f);
lo = madd(f, lo, 1.442689881667200f);
return ((f4 * hi) + (f * lo)) + exponent;
}
ccl_device_inline float fast_logf(const float x)
{
/* Examined 2130706432 values of logf on [1.17549435e-38,3.40282347e+38]:
* 0.313865375 avg ULP diff, 5148137 max ULP, 7.62939e-06 max error.
*/
return fast_log2f(x) * M_LN2_F;
}
ccl_device_inline float fast_log10(const float x)
{
/* Examined 2130706432 values of log10f on [1.17549435e-38,3.40282347e+38]:
* 0.631237033 avg ULP diff, 4471615 max ULP, 3.8147e-06 max error.
*/
return fast_log2f(x) * M_LN2_F / M_LN10_F;
}
ccl_device float fast_logb(float x)
{
/* Don't bother with denormals. */
x = fabsf(x);
x = clamp(x, FLT_MIN, FLT_MAX);
const unsigned bits = __float_as_uint(x);
return (float)((int)(bits >> 23) - 127);
}
ccl_device float fast_exp2f(float x)
{
/* Clamp to safe range for final addition. */
x = clamp(x, -126.0f, 126.0f);
/* Range reduction. */
const int m = (int)x;
x -= m;
x = 1.0f - (1.0f - x); /* Crush denormals (does not affect max ULPS!). */
/* 5th degree polynomial generated with sollya
* Examined 2247622658 values of exp2 on [-126,126]: 2.75764912 avg ULP diff,
* 232 max ULP.
*
* ULP histogram:
* 0 = 87.81%
* 1 = 4.18%
*/
float r = 1.33336498402e-3f;
r = madd(x, r, 9.810352697968e-3f);
r = madd(x, r, 5.551834031939e-2f);
r = madd(x, r, 0.2401793301105f);
r = madd(x, r, 0.693144857883f);
r = madd(x, r, 1.0f);
/* Multiply by 2 ^ m by adding in the exponent. */
/* NOTE: left-shift of negative number is undefined behavior. */
return __uint_as_float(__float_as_uint(r) + ((unsigned)m << 23));
}
ccl_device_inline float fast_expf(const float x)
{
/* Examined 2237485550 values of exp on [-87.3300018,87.3300018]:
* 2.6666452 avg ULP diff, 230 max ULP.
*/
return fast_exp2f(x / M_LN2_F);
}
#if !defined(__KERNEL_GPU__) && !defined(_MSC_VER)
/* MSVC seems to have a code-gen bug here in at least SSE41/AVX, see
* #78047 and #78869 for details. Just disable for now, it only makes
* a small difference in denoising performance. */
ccl_device float4 fast_exp2f4(float4 x)
{
const float4 one = make_float4(1.0f);
const float4 limit = make_float4(126.0f);
x = clamp(x, -limit, limit);
const int4 m = make_int4(x);
x = one - (one - (x - make_float4(m)));
float4 r = make_float4(1.33336498402e-3f);
r = madd4(x, r, make_float4(9.810352697968e-3f));
r = madd4(x, r, make_float4(5.551834031939e-2f));
r = madd4(x, r, make_float4(0.2401793301105f));
r = madd4(x, r, make_float4(0.693144857883f));
r = madd4(x, r, make_float4(1.0f));
return __int4_as_float4(__float4_as_int4(r) + (m << 23));
}
ccl_device_inline float4 fast_expf4(const float4 x)
{
return fast_exp2f4(x / M_LN2_F);
}
#else
ccl_device_inline float4 fast_expf4(const float4 x)
{
return make_float4(fast_expf(x.x), fast_expf(x.y), fast_expf(x.z), fast_expf(x.w));
}
#endif
ccl_device_inline float fast_exp10(const float x)
{
/* Examined 2217701018 values of exp10 on [-37.9290009,37.9290009]:
* 2.71732409 avg ULP diff, 232 max ULP.
*/
return fast_exp2f(x * M_LN10_F / M_LN2_F);
}
ccl_device_inline float fast_expm1f(float x)
{
if (fabsf(x) < 1e-5f) {
x = 1.0f - (1.0f - x); /* Crush denormals. */
return madd(0.5f, x * x, x);
}
return fast_expf(x) - 1.0f;
}
ccl_device float fast_sinhf(const float x)
{
float a = fabsf(x);
if (a > 1.0f) {
/* Examined 53389559 values of sinh on [1,87.3300018]:
* 33.6886442 avg ULP diff, 178 max ULP. */
const float e = fast_expf(a);
return copysignf(0.5f * e - 0.5f / e, x);
}
a = 1.0f - (1.0f - a); /* Crush denorms. */
const float a2 = a * a;
/* Degree 7 polynomial generated with sollya. */
/* Examined 2130706434 values of sinh on [-1,1]: 1.19209e-07 max error. */
float r = 2.03945513931e-4f;
r = madd(r, a2, 8.32990277558e-3f);
r = madd(r, a2, 0.1666673421859f);
r = madd(r * a, a2, a);
return copysignf(r, x);
}
ccl_device_inline float fast_coshf(const float x)
{
/* Examined 2237485550 values of cosh on [-87.3300018,87.3300018]:
* 1.78256726 avg ULP diff, 178 max ULP.
*/
const float e = fast_expf(fabsf(x));
return 0.5f * e + 0.5f / e;
}
ccl_device_inline float fast_tanhf(const float x)
{
/* Examined 4278190080 values of tanh on [-3.40282347e+38,3.40282347e+38]:
* 3.12924e-06 max error.
*/
/* NOTE: ULP error is high because of sub-optimal handling around the origin. */
const float e = fast_expf(2.0f * fabsf(x));
return copysignf(1.0f - 2.0f / (1.0f + e), x);
}
ccl_device float fast_safe_powf(const float x, const float y)
{
if (y == 0) {
return 1.0f; /* x^1=1 */
}
if (x == 0) {
return 0.0f; /* 0^y=0 */
}
float sign = 1.0f;
if (x < 0.0f) {
/* if x is negative, only deal with integer powers
* powf returns NaN for non-integers, we will return 0 instead.
*/
const int ybits = __float_as_int(y) & 0x7fffffff;
if (ybits >= 0x4b800000) {
// always even int, keep positive
}
else if (ybits >= 0x3f800000) {
/* Bigger than 1, check. */
const int k = (ybits >> 23) - 127; /* Get exponent. */
const int j = ybits >> (23 - k); /* Shift out possible fractional bits. */
if ((j << (23 - k)) == ybits) { /* rebuild number and check for a match. */
/* +1 for even, -1 for odd. */
sign = __int_as_float(0x3f800000 | (j << 31));
}
else {
/* Not an integer. */
return 0.0f;
}
}
else {
/* Not an integer. */
return 0.0f;
}
}
return sign * fast_exp2f(y * fast_log2f(fabsf(x)));
}
/* TODO(sergey): Check speed with our erf functions implementation from
* bsdf_microfacet.h.
*/
ccl_device_inline float fast_erff(const float x)
{
/* Examined 1082130433 values of erff on [0,4]: 1.93715e-06 max error. */
/* Abramowitz and Stegun, 7.1.28. */
const float a1 = 0.0705230784f;
const float a2 = 0.0422820123f;
const float a3 = 0.0092705272f;
const float a4 = 0.0001520143f;
const float a5 = 0.0002765672f;
const float a6 = 0.0000430638f;
const float a = fabsf(x);
if (a >= 12.3f) {
return copysignf(1.0f, x);
}
const float b = 1.0f - (1.0f - a); /* Crush denormals. */
const float r = madd(
madd(madd(madd(madd(madd(a6, b, a5), b, a4), b, a3), b, a2), b, a1), b, 1.0f);
const float s = r * r; /* ^2 */
const float t = s * s; /* ^4 */
const float u = t * t; /* ^8 */
const float v = u * u; /* ^16 */
return copysignf(1.0f - 1.0f / v, x);
}
ccl_device_inline float fast_erfcf(const float x)
{
/* Examined 2164260866 values of erfcf on [-4,4]: 1.90735e-06 max error.
*
* ULP histogram:
*
* 0 = 80.30%
*/
return 1.0f - fast_erff(x);
}
ccl_device_inline float fast_ierff(const float x)
{
/* From: Approximating the `erfinv` function by Mike Giles. */
/* To avoid trouble at the limit, clamp input to 1-epsilon. */
const float a = min(fabsf(x), 0.99999994f);
float w = -fast_logf((1.0f - a) * (1.0f + a));
float p;
if (w < 5.0f) {
w = w - 2.5f;
p = 2.81022636e-08f;
p = madd(p, w, 3.43273939e-07f);
p = madd(p, w, -3.5233877e-06f);
p = madd(p, w, -4.39150654e-06f);
p = madd(p, w, 0.00021858087f);
p = madd(p, w, -0.00125372503f);
p = madd(p, w, -0.00417768164f);
p = madd(p, w, 0.246640727f);
p = madd(p, w, 1.50140941f);
}
else {
w = sqrtf(w) - 3.0f;
p = -0.000200214257f;
p = madd(p, w, 0.000100950558f);
p = madd(p, w, 0.00134934322f);
p = madd(p, w, -0.00367342844f);
p = madd(p, w, 0.00573950773f);
p = madd(p, w, -0.0076224613f);
p = madd(p, w, 0.00943887047f);
p = madd(p, w, 1.00167406f);
p = madd(p, w, 2.83297682f);
}
return p * x;
}
/* Fast inverse cube root for positive x, with two Newton iterations to improve accuracy. */
ccl_device_inline float fast_inv_cbrtf(const float x)
{
util_assert(x >= 0.0f);
/* Constant is roughly `cbrt(2^127)`, but tweaked a bit to balance the error across the entire
* range. The exact value is not critical. */
float y = __int_as_float(0x54a24242 - __float_as_int(x) / 3);
y = (2.0f / 3) * y + 1 / (3 * y * y * x);
y = (2.0f / 3) * y + 1 / (3 * y * y * x);
return y;
}
CCL_NAMESPACE_END

View File

@@ -0,0 +1,274 @@
/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#pragma once
#include "util/math_base.h"
#include "util/types_float2.h"
#include "util/types_float4.h"
CCL_NAMESPACE_BEGIN
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);
}
ccl_device_template_spec float2 make_zero()
{
return zero_float2();
}
#if !defined(__KERNEL_METAL__)
ccl_device_inline float2 operator-(const float2 &a)
{
return make_float2(-a.x, -a.y);
}
ccl_device_inline float2 operator*(const float2 a, const float2 b)
{
return make_float2(a.x * b.x, a.y * b.y);
}
ccl_device_inline float2 operator*(const float2 a, const float f)
{
return make_float2(a.x * f, a.y * f);
}
ccl_device_inline float2 operator*(float f, const float2 a)
{
return make_float2(a.x * f, a.y * f);
}
ccl_device_inline float2 operator/(float f, const float2 a)
{
return make_float2(f / a.x, f / a.y);
}
ccl_device_inline float2 operator/(const float2 a, const float f)
{
const float invf = 1.0f / f;
return make_float2(a.x * invf, a.y * invf);
}
ccl_device_inline float2 operator/(const float2 a, const float2 b)
{
return make_float2(a.x / b.x, a.y / b.y);
}
ccl_device_inline float2 operator+(const float2 a, const float2 b)
{
return make_float2(a.x + b.x, a.y + b.y);
}
ccl_device_inline float2 operator+(const float2 a, const float f)
{
return a + make_float2(f, f);
}
ccl_device_inline float2 operator-(const float2 a, const float2 b)
{
return make_float2(a.x - b.x, a.y - b.y);
}
ccl_device_inline float2 operator-(const float2 a, const float f)
{
return a - make_float2(f, f);
}
ccl_device_inline float2 operator+=(float2 &a, const float2 b)
{
return a = a + b;
}
ccl_device_inline float2 operator*=(float2 &a, const float2 b)
{
return a = a * b;
}
ccl_device_inline float2 operator*=(float2 &a, const float f)
{
return a = a * f;
}
ccl_device_inline float2 operator/=(float2 &a, const float2 b)
{
return a = a / b;
}
ccl_device_inline float2 operator/=(float2 &a, const float f)
{
const float invf = 1.0f / f;
return a = a * invf;
}
ccl_device_inline bool operator==(const float2 a, const float2 b)
{
return (a.x == b.x && a.y == b.y);
}
ccl_device_inline bool operator!=(const float2 a, const float2 b)
{
return !(a == b);
}
ccl_device_inline int2 operator>=(const float2 a, const float2 b)
{
return make_int2(a.x >= b.x, a.y >= b.y);
}
ccl_device_inline bool is_zero(const float2 a)
{
return (a.x == 0.0f && a.y == 0.0f);
}
ccl_device_inline float dot(const float2 a, const float2 b)
{
return a.x * b.x + a.y * b.y;
}
#endif
ccl_device_inline float average(const float2 a)
{
return (a.x + a.y) * (1.0f / 2.0f);
}
ccl_device_inline bool isequal(const float2 a, const float2 b)
{
#if defined(__KERNEL_METAL__)
return all(a == b);
#else
return a == b;
#endif
}
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());
}
ccl_device_inline float len(const float2 a)
{
return sqrtf(dot(a, a));
}
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;
}
ccl_device_inline float len_squared(const float2 a)
{
return dot(a, a);
}
ccl_device_inline float2 safe_normalize(const float2 a)
{
const float t = len(a);
return (t != 0.0f) ? a / t : a;
}
#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)
{
return (a.x * b.y - a.y * b.x);
}
ccl_device_inline float2 normalize(const float2 a)
{
return a / len(a);
}
ccl_device_inline float2 normalize_len(const float2 a, ccl_private float *t)
{
*t = len(a);
return a / (*t);
}
ccl_device_inline float2 min(const float2 a, const float2 b)
{
return make_float2(min(a.x, b.x), min(a.y, b.y));
}
ccl_device_inline float2 max(const float2 a, const float2 b)
{
return make_float2(max(a.x, b.x), max(a.y, b.y));
}
ccl_device_inline float2 clamp(const float2 a, const float2 mn, const float2 mx)
{
return min(max(a, mn), mx);
}
ccl_device_inline float2 fmod(const float2 a, const float b)
{
return make_float2(fmodf(a.x, b), fmodf(a.y, b));
}
ccl_device_inline float2 fabs(const float2 a)
{
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);
}
ccl_device_inline float2 interp(const float2 a, const float2 b, const float t)
{
return a + t * (b - a);
}
ccl_device_inline float2 mix(const float2 a, const float2 b, const float t)
{
return a + t * (b - a);
}
ccl_device_inline float2 floor(const float2 a)
{
return make_float2(floorf(a.x), floorf(a.y));
}
#endif /* !__KERNEL_METAL__ */
/* Consistent name for this would be pow, but HIP compiler crashes in name mangling. */
ccl_device_inline float2 power(const float2 v, const float e)
{
return make_float2(powf(v.x, e), powf(v.y, e));
}
ccl_device_inline float2 safe_divide_float2_float(const float2 a, const float b)
{
return (b != 0.0f) ? a / b : zero_float2();
}
CCL_NAMESPACE_END

View File

@@ -0,0 +1,862 @@
/* SPDX-FileCopyrightText: 2011-2013 Intel Corporation
* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#pragma once
#include "util/math_base.h"
#include "util/math_float4.h"
#include "util/types_float3.h"
#include "util/types_float4.h"
#include "util/types_int3.h"
#include "util/types_uint3.h"
CCL_NAMESPACE_BEGIN
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);
}
ccl_device_template_spec float3 make_zero()
{
return zero_float3();
}
ccl_device_inline float3 reciprocal(const float3 a)
{
#ifdef __KERNEL_SSE__
/* Don't use _mm_rcp_ps due to poor precision. */
return float3(_mm_div_ps(_mm_set_ps1(1.0f), a.m128));
#else
return make_float3(1.0f / a.x, 1.0f / a.y, 1.0f / a.z);
#endif
}
#ifndef __KERNEL_METAL__
ccl_device_inline float3 operator-(const float3 &a)
{
# ifdef __KERNEL_SSE__
return float3(_mm_xor_ps(a.m128, _mm_castsi128_ps(_mm_set1_epi32(0x80000000))));
# else
return make_float3(-a.x, -a.y, -a.z);
# endif
}
ccl_device_inline float3 operator*(const float3 a, const float3 b)
{
# ifdef __KERNEL_SSE__
return float3(_mm_mul_ps(a.m128, b.m128));
# else
return make_float3(a.x * b.x, a.y * b.y, a.z * b.z);
# endif
}
ccl_device_inline float3 operator*(const float3 a, const float f)
{
# ifdef __KERNEL_SSE__
return float3(_mm_mul_ps(a.m128, _mm_set1_ps(f)));
# else
return make_float3(a.x * f, a.y * f, a.z * f);
# endif
}
ccl_device_inline float3 operator*(const float f, const float3 a)
{
# if defined(__KERNEL_SSE__)
return float3(_mm_mul_ps(_mm_set1_ps(f), a.m128));
# else
return make_float3(a.x * f, a.y * f, a.z * f);
# endif
}
ccl_device_inline float3 operator/(const float f, const float3 a)
{
# if defined(__KERNEL_SSE__)
return float3(_mm_div_ps(_mm_set1_ps(f), a.m128));
# else
return make_float3(f / a.x, f / a.y, f / a.z);
# endif
}
ccl_device_inline float3 operator/(const float3 a, const float f)
{
# if defined(__KERNEL_SSE__)
return float3(_mm_div_ps(a.m128, _mm_set1_ps(f)));
# else
float invf = 1.0f / f;
return make_float3(a.x * invf, a.y * invf, a.z * invf);
# endif
}
ccl_device_inline float3 operator/(const float3 a, const float3 b)
{
# if defined(__KERNEL_SSE__)
return float3(_mm_div_ps(a.m128, b.m128));
# else
return make_float3(a.x / b.x, a.y / b.y, a.z / b.z);
# endif
}
ccl_device_inline float3 operator+(const float3 a, const float3 b)
{
# ifdef __KERNEL_SSE__
return float3(_mm_add_ps(a.m128, b.m128));
# else
return make_float3(a.x + b.x, a.y + b.y, a.z + b.z);
# endif
}
ccl_device_inline float3 operator+(const float3 a, const float b)
{
return a + make_float3(b);
}
ccl_device_inline float3 operator+(const float a, const float3 b)
{
return make_float3(a) + b;
}
ccl_device_inline float3 operator-(const float3 a, const float3 b)
{
# ifdef __KERNEL_SSE__
return float3(_mm_sub_ps(a.m128, b.m128));
# else
return make_float3(a.x - b.x, a.y - b.y, a.z - b.z);
# endif
}
ccl_device_inline float3 operator-(const float3 a, const float b)
{
return a - make_float3(b);
}
ccl_device_inline float3 operator-(const float a, const float3 b)
{
return make_float3(a) - b;
}
ccl_device_inline float3 operator+=(float3 &a, const float3 b)
{
return a = a + b;
}
ccl_device_inline float3 operator-=(float3 &a, const float3 b)
{
return a = a - b;
}
ccl_device_inline float3 operator*=(float3 &a, const float3 b)
{
return a = a * b;
}
ccl_device_inline float3 operator*=(float3 &a, const float f)
{
return a = a * f;
}
ccl_device_inline float3 operator/=(float3 &a, const float3 b)
{
return a = a / b;
}
ccl_device_inline float3 operator/=(float3 &a, const float f)
{
const float invf = 1.0f / f;
return a = a * invf;
}
# if !(defined(__KERNEL_CUDA__) || defined(__KERNEL_HIP__) || defined(__KERNEL_ONEAPI__))
ccl_device_inline packed_float3 operator*=(packed_float3 &a, const float3 b)
{
a = float3(a) * b;
return a;
}
ccl_device_inline packed_float3 operator*=(packed_float3 &a, const float f)
{
a = float3(a) * f;
return a;
}
ccl_device_inline packed_float3 operator/=(packed_float3 &a, const float3 b)
{
a = float3(a) / b;
return a;
}
ccl_device_inline packed_float3 operator/=(packed_float3 &a, const float f)
{
a = float3(a) / f;
return a;
}
ccl_device_inline packed_float3 operator+=(packed_float3 &a, const float3 b)
{
a = float3(a) + b;
return a;
}
# endif
ccl_device_inline bool operator==(const float3 a, const float3 b)
{
# ifdef __KERNEL_SSE__
return (_mm_movemask_ps(_mm_cmpeq_ps(a.m128, b.m128)) & 7) == 7;
# else
return (a.x == b.x && a.y == b.y && a.z == b.z);
# endif
}
ccl_device_inline int3 operator==(const float3 a, const float b)
{
# ifdef __KERNEL_SSE__
return int3(_mm_castps_si128(_mm_cmpeq_ps(a.m128, make_float3(b).m128)));
# else
return make_int3(a.x == b, a.y == b, a.z == b);
# endif
}
ccl_device_inline bool operator!=(const float3 a, const float3 b)
{
return !(a == b);
}
ccl_device_inline int3 operator>=(const float3 a, const float3 b)
{
# ifdef __KERNEL_SSE__
return int3(_mm_castps_si128(_mm_cmpge_ps(a.m128, b.m128)));
# else
return make_int3(a.x >= b.x, a.y >= b.y, a.z >= b.z);
# endif
}
ccl_device_inline int3 operator<(const float3 a, const float3 b)
{
# ifdef __KERNEL_SSE__
return int3(_mm_castps_si128(_mm_cmplt_ps(a.m128, b.m128)));
# else
return make_int3(a.x < b.x, a.y < b.y, a.z < b.z);
# endif
}
ccl_device_inline float dot(const float3 a, const float3 b)
{
# if defined(__KERNEL_SSE42__) && defined(__KERNEL_SSE__)
return _mm_cvtss_f32(_mm_dp_ps(a, b, 0x7F));
# else
return a.x * b.x + a.y * b.y + a.z * b.z;
# endif
}
ccl_device_inline int3 operator>(const float3 a, const float3 b)
{
# ifdef __KERNEL_SSE__
return int3(_mm_castps_si128(_mm_cmpgt_ps(a.m128, b.m128)));
# else
return make_int3(a.x > b.x, a.y > b.y, a.z > b.z);
# endif
}
ccl_device_inline int3 operator>(const float3 a, const float b)
{
return a > make_float3(b);
}
#endif /* __KERNEL_METAL__ */
ccl_device_inline float dot_xy(const float3 a, const float3 b)
{
#if defined(__KERNEL_SSE42__) && defined(__KERNEL_SSE__)
return _mm_cvtss_f32(_mm_hadd_ps(_mm_mul_ps(a, b), b));
#else
return a.x * b.x + a.y * b.y;
#endif
}
ccl_device_inline float len(const float3 a)
{
#if defined(__KERNEL_SSE42__) && 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(const float3 a)
{
return min(min(a.x, a.y), a.z);
}
ccl_device_inline float reduce_max(const 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);
}
ccl_device_inline float3 cross(const float3 a, const float3 b)
{
# ifdef __KERNEL_SSE__
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);
# 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
}
ccl_device_inline float3 normalize(const float3 a)
{
# if defined(__KERNEL_SSE42__) && defined(__KERNEL_SSE__)
const __m128 norm = _mm_sqrt_ps(_mm_dp_ps(a.m128, a.m128, 0x7F));
return float3(_mm_div_ps(a.m128, norm));
# else
return a / len(a);
# endif
}
ccl_device_inline float3 min(const float3 a, const float3 b)
{
# ifdef __KERNEL_SSE__
return float3(_mm_min_ps(a.m128, b.m128));
# else
return make_float3(min(a.x, b.x), min(a.y, b.y), min(a.z, b.z));
# endif
}
ccl_device_inline float3 max(const float3 a, const float3 b)
{
# ifdef __KERNEL_SSE__
return float3(_mm_max_ps(a.m128, b.m128));
# else
return make_float3(max(a.x, b.x), max(a.y, b.y), max(a.z, b.z));
# endif
}
ccl_device_inline float3 clamp(const float3 a, const float3 mn, const float3 mx)
{
return min(max(a, mn), mx);
}
ccl_device_inline float3 fabs(const float3 a)
{
# ifdef __KERNEL_SSE__
# ifdef __KERNEL_NEON__
return float3(vabsq_f32(a.m128));
# else
__m128 mask = _mm_castsi128_ps(_mm_set1_epi32(0x7fffffff));
return float3(_mm_and_ps(a.m128, mask));
# endif
# else
return make_float3(fabsf(a.x), fabsf(a.y), fabsf(a.z));
# endif
}
/* The floating-point remainder of the division operation a / b calculated by this function is
* exactly the value a - iquot * b, where iquot is a / b with its fractional part truncated.
*
* The returned value has the same sign as a and is less than b in magnitude. */
ccl_device_inline float3 fmod(const float3 a, const float b)
{
# if defined(__KERNEL_NEON__)
/* Use native Neon instructions.
* The logic is the same as the SSE code below, but on Apple M2 Ultra this seems to be faster.
* Possibly due to some runtime checks in _mm_round_ps which do not get properly inlined. */
const float32x4_t iquot = vrndq_f32(a / b);
return float3(vsubq_f32(a, vmulq_f32(iquot, vdupq_n_f32(b))));
# elif defined(__KERNEL_SSE42__) && defined(__KERNEL_SSE__)
const __m128 iquot = _mm_round_ps(a / b, _MM_FROUND_TRUNC);
return float3(_mm_sub_ps(a, _mm_mul_ps(iquot, _mm_set1_ps(b))));
# else
return make_float3(fmodf(a.x, b), fmodf(a.y, b), fmodf(a.z, b));
# endif
}
ccl_device_inline float3 fmod(const float3 a, const float3 b)
{
# if defined(__KERNEL_NEON__)
const float32x4_t iquot = vrndq_f32(vdivq_f32(a.m128, b.m128));
return float3(vsubq_f32(a, vmulq_f32(iquot, b.m128)));
# elif defined(__KERNEL_SSE42__) && defined(__KERNEL_SSE__)
const __m128 div = _mm_div_ps(a.m128, b.m128);
const __m128 iquot = _mm_round_ps(div, _MM_FROUND_TO_ZERO | _MM_FROUND_NO_EXC);
return float3(_mm_sub_ps(a.m128, _mm_mul_ps(iquot, b.m128)));
# else
return make_float3(fmodf(a.x, b.x), fmodf(a.y, b.y), fmodf(a.z, b.z));
# endif
}
ccl_device_inline float3 sqrt(const float3 a)
{
# ifdef __KERNEL_SSE__
return float3(_mm_sqrt_ps(a));
# else
return make_float3(sqrtf(a.x), sqrtf(a.y), sqrtf(a.z));
# endif
}
ccl_device_inline float3 round(const float3 a)
{
# if defined(__KERNEL_NEON__)
return float3(vrndnq_f32(a.m128));
# elif defined(__KERNEL_SSE__)
return float3(_mm_round_ps(a.m128, _MM_FROUND_NINT));
# else
return make_float3(roundf(a.x), roundf(a.y), roundf(a.z));
# endif
}
ccl_device_inline float3 floor(const float3 a)
{
# ifdef __KERNEL_SSE__
return float3(_mm_floor_ps(a));
# else
return make_float3(floorf(a.x), floorf(a.y), floorf(a.z));
# endif
}
ccl_device_inline float3 ceil(const float3 a)
{
# ifdef __KERNEL_SSE__
return float3(_mm_ceil_ps(a));
# else
return make_float3(ceilf(a.x), ceilf(a.y), ceilf(a.z));
# endif
}
ccl_device_inline float3 mix(const float3 a, const float3 b, const float t)
{
return a + t * (b - a);
}
ccl_device_inline float3 mix(const float3 a, const float3 b, const float3 t)
{
return a + t * (b - a);
}
ccl_device_inline float3 saturate(const float3 a)
{
return make_float3(saturatef(a.x), saturatef(a.y), saturatef(a.z));
}
ccl_device_inline float3 exp(const float3 v)
{
return make_float3(expf(v.x), expf(v.y), expf(v.z));
}
ccl_device_inline float3 log(const float3 v)
{
return make_float3(logf(v.x), logf(v.y), logf(v.z));
}
ccl_device_inline float3 sin(const float3 v)
{
return make_float3(sinf(v.x), sinf(v.y), sinf(v.z));
}
ccl_device_inline float3 cos(const float3 v)
{
return make_float3(cosf(v.x), cosf(v.y), cosf(v.z));
}
ccl_device_inline float3 tan(const float3 v)
{
return make_float3(tanf(v.x), tanf(v.y), tanf(v.z));
}
ccl_device_inline float3 atan2(const float3 y, const float3 x)
{
return make_float3(atan2f(y.x, x.x), atan2f(y.y, x.y), atan2f(y.z, x.z));
}
ccl_device_inline float3 reflect(const float3 incident, const float3 unit_normal)
{
return incident - 2.0f * unit_normal * dot(incident, unit_normal);
}
ccl_device_inline float3 refract(const float3 incident, const float3 normal, const float eta)
{
const float k = 1.0f - eta * eta * (1.0f - dot(normal, incident) * dot(normal, incident));
if (k < 0.0f) {
return zero_float3();
}
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;
}
#endif
ccl_device_inline float3 safe_sqrt(const float3 a)
{
return sqrt(max(a, zero_float3()));
}
ccl_device_inline float3 project(const float3 v, const float3 v_proj)
{
const float len_squared = dot(v_proj, v_proj);
return (len_squared != 0.0f) ? (dot(v, v_proj) / len_squared) * v_proj : zero_float3();
}
ccl_device_inline float3 normalize_len(const float3 a, ccl_private float *t)
{
*t = len(a);
const float x = 1.0f / *t;
return a * x;
}
ccl_device_inline float3 safe_normalize(const float3 a)
{
const float t = len(a);
return (t != 0.0f) ? a * (1.0f / t) : a;
}
ccl_device_inline float3 safe_normalize_fallback(const float3 a, const float3 fallback)
{
const float t = len(a);
return (t != 0.0f) ? a * (1.0f / t) : fallback;
}
ccl_device_inline float3 safe_normalize_len(const float3 a, ccl_private float *t)
{
*t = len(a);
return (*t != 0.0f) ? a / (*t) : a;
}
ccl_device_inline float3 safe_divide(const float3 a, const float3 b)
{
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);
}
ccl_device_inline float3 safe_divide(const float3 a, const float b)
{
return (b != 0.0f) ? a / b : zero_float3();
}
ccl_device_inline float3 interp(const float3 a, const float3 b, const float t)
{
return a + t * (b - a);
}
ccl_device_inline float3 sqr(const float3 a)
{
return a * a;
}
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 bool any_zero(const float3 a)
{
return (a.x == 0.0f || a.y == 0.0f || a.z == 0.0f);
}
ccl_device_inline float reduce_add(const float3 a)
{
#if defined(__KERNEL_SSE__) && defined(__KERNEL_NEON__)
__m128 t = a.m128;
t = vsetq_lane_f32(0.0f, t, 3);
return vaddvq_f32(t);
#else
return (a.x + a.y + a.z);
#endif
}
ccl_device_inline float average(const float3 a)
{
return reduce_add(a) * (1.0f / 3.0f);
}
ccl_device_inline bool isequal(const float3 a, const float3 b)
{
#if defined(__KERNEL_METAL__)
return all(a == b);
#else
return a == b;
#endif
}
template<class MaskType>
ccl_device_inline float3 select(const MaskType mask, const float3 a, const float3 b)
{
#if defined(__KERNEL_METAL__)
return metal::select(b, a, bool3(mask));
#elif defined(__KERNEL_SSE__)
# ifdef __KERNEL_SSE42__
return float3(_mm_blendv_ps(b.m128, a.m128, _mm_castsi128_ps(mask.m128)));
# else
return float3(
_mm_or_ps(_mm_and_ps(_mm_castsi128_ps(mask), a), _mm_andnot_ps(_mm_castsi128_ps(mask), b)));
# endif
#else
return make_float3((mask.x) ? a.x : b.x, (mask.y) ? a.y : b.y, (mask.z) ? a.z : b.z);
#endif
}
template<class MaskType> ccl_device_inline float3 mask(const MaskType mask, const float3 a)
{
/* Replace elements of x with zero where mask isn't set. */
return select(mask, a, zero_float3());
}
/* Consistent name for this would be pow, but HIP compiler crashes in name mangling. */
ccl_device_inline float3 power(const float3 v, const float e)
{
return make_float3(powf(v.x, e), powf(v.y, e), powf(v.z, e));
}
ccl_device_inline float3 safe_pow(const float3 a, const float3 b)
{
return make_float3(safe_powf(a.x, b.x), safe_powf(a.y, b.y), safe_powf(a.z, b.z));
}
ccl_device_inline float3 safe_log(const float3 v)
{
return select(v > zero_float3(), log(v), zero_float3());
}
ccl_device_inline void sincos(const float3 x, ccl_private float3 *sine, ccl_private float3 *cosine)
{
#if defined(__KERNEL_METAL__)
*sine = sincos(x, *cosine);
#else
*sine = sin(x);
*cosine = cos(x);
#endif
}
ccl_device_inline auto component_wise_equal(const float3 a, const float3 b)
{
#if defined(__KERNEL_METAL__)
return a == b;
#elif defined __KERNEL_NEON__
return int3(vreinterpretq_m128i_u32(vceqq_f32(a.m128, b.m128)));
#elif defined(__KERNEL_SSE__)
return int3(_mm_castps_si128(_mm_cmpeq_ps(a.m128, b.m128)));
#else
return make_int3(a.x == b.x, a.y == b.y, a.z == b.z);
#endif
}
ccl_device_inline auto component_is_zero(const float3 a)
{
return component_wise_equal(a, zero_float3());
}
ccl_device_inline float3 safe_floored_fmod(const float3 a, const float3 b)
{
return select(component_is_zero(b), zero_float3(), a - floor(a / b) * b);
}
ccl_device_inline float3 wrap(const float3 value, const float3 max, const float3 min)
{
return safe_floored_fmod(value - min, max - min) + min;
}
ccl_device_inline float3 safe_fmod(const float3 a, const float3 b)
{
return select(component_is_zero(b), zero_float3(), fmod(a, b));
}
ccl_device_inline float3 compatible_sign(const float3 v)
{
return make_float3(compatible_signf(v.x), compatible_signf(v.y), compatible_signf(v.z));
}
ccl_device_inline bool isfinite_safe(const float3 v)
{
return isfinite_safe(v.x) && isfinite_safe(v.y) && isfinite_safe(v.z);
}
ccl_device_inline float3 ensure_finite(const float3 v)
{
float3 r = v;
if (!isfinite_safe(r.x)) {
r.x = 0.0f;
}
if (!isfinite_safe(r.y)) {
r.y = 0.0f;
}
if (!isfinite_safe(r.z)) {
r.z = 0.0f;
}
return r;
}
/* Triangle */
ccl_device_inline float triangle_area(const ccl_private float3 &v1,
const ccl_private float3 &v2,
const ccl_private float3 &v3)
{
return len(cross(v3 - v2, v1 - v2)) * 0.5f;
}
/* Orthonormal vectors */
ccl_device_inline void make_orthonormals(const float3 N,
ccl_private float3 *a,
ccl_private float3 *b)
{
#if 0
if (fabsf(N.y) >= 0.999f) {
*a = make_float3(1, 0, 0);
*b = make_float3(0, 0, 1);
return;
}
if (fabsf(N.z) >= 0.999f) {
*a = make_float3(1, 0, 0);
*b = make_float3(0, 1, 0);
return;
}
#endif
if (N.x != N.y || N.x != N.z) {
*a = make_float3(N.z - N.y, N.x - N.z, N.y - N.x); //(1,1,1)x N
}
else {
*a = make_float3(N.z - N.y, N.x + N.z, -N.y - N.x); //(-1,1,1)x N
}
*a = normalize(*a);
*b = cross(N, *a);
}
/* Rotation of point around axis and angle */
ccl_device_inline float3 rotate_around_axis(const float3 p, const float3 axis, const float angle)
{
const float costheta = cosf(angle);
const float sintheta = sinf(angle);
float3 r;
r.x = ((costheta + (1 - costheta) * axis.x * axis.x) * p.x) +
(((1 - costheta) * axis.x * axis.y - axis.z * sintheta) * p.y) +
(((1 - costheta) * axis.x * axis.z + axis.y * sintheta) * p.z);
r.y = (((1 - costheta) * axis.x * axis.y + axis.z * sintheta) * p.x) +
((costheta + (1 - costheta) * axis.y * axis.y) * p.y) +
(((1 - costheta) * axis.y * axis.z - axis.x * sintheta) * p.z);
r.z = (((1 - costheta) * axis.x * axis.z - axis.y * sintheta) * p.x) +
(((1 - costheta) * axis.y * axis.z + axis.x * sintheta) * p.y) +
((costheta + (1 - costheta) * axis.z * axis.z) * p.z);
return r;
}
/* Calculate the angle between the two vectors a and b.
* The usual approach `acos(dot(a, b))` has severe precision issues for small angles,
* which are avoided by this method.
* Based on "Mangled Angles" from https://people.eecs.berkeley.edu/~wkahan/Mindless.pdf
*/
ccl_device_inline float precise_angle(const float3 a, const float3 b)
{
return 2.0f * atan2f(len(a - b), len(a + b));
}
/* Tangent of the angle between vectors a and b. */
ccl_device_inline float tan_angle(const float3 a, const float3 b)
{
return len(cross(a, b)) / dot(a, b);
}
/* projections */
ccl_device_inline float2 map_to_tube(const float3 co)
{
float len;
float u;
float v;
len = sqrtf(co.x * co.x + co.y * co.y);
if (len > 0.0f) {
u = (1.0f - (atan2f(co.x / len, co.y / len) / M_PI_F)) * 0.5f;
v = (co.z + 1.0f) * 0.5f;
}
else {
u = v = 0.0f;
}
return make_float2(u, v);
}
ccl_device_inline float2 map_to_sphere(const float3 co)
{
const float l = dot(co, co);
float u;
float v;
if (l > 0.0f) {
if (UNLIKELY(co.x == 0.0f && co.y == 0.0f)) {
u = 0.0f; /* Otherwise domain error. */
}
else {
u = (0.5f - atan2f(co.x, co.y) * M_1_2PI_F);
}
v = 1.0f - safe_acosf(co.z / sqrtf(l)) * M_1_PI_F;
}
else {
u = v = 0.0f;
}
return make_float2(u, v);
}
ccl_device_inline void copy_v3_v3(ccl_private float *r, const float3 val)
{
r[0] = val.x;
r[1] = val.y;
r[2] = val.z;
}
ccl_device_inline uint3 float3_as_uint3(const float3 f)
{
#ifdef __KERNEL_METAL__
return as_type<uint3>(f);
#else
return make_uint3(__float_as_uint(f.x), __float_as_uint(f.y), __float_as_uint(f.z));
#endif
}
ccl_device_inline float3 uint3_as_float3(const uint3 f)
{
#ifdef __KERNEL_METAL__
return as_type<float3>(f);
#else
return make_float3(__uint_as_float(f.x), __uint_as_float(f.y), __uint_as_float(f.z));
#endif
}
CCL_NAMESPACE_END

View File

@@ -0,0 +1,684 @@
/* SPDX-FileCopyrightText: 2011-2013 Intel Corporation
* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#pragma once
#include "util/math_base.h"
#include "util/types_float4.h"
CCL_NAMESPACE_BEGIN
ccl_device_inline float4 zero_float4()
{
#ifdef __KERNEL_SSE__
return float4(_mm_setzero_ps());
#else
return make_float4(0.0f);
#endif
}
ccl_device_inline float4 one_float4()
{
return make_float4(1.0f);
}
ccl_device_template_spec float4 make_zero()
{
return zero_float4();
}
ccl_device_inline int4 cast(const float4 a)
{
#ifdef __KERNEL_SSE__
return int4(_mm_castps_si128(a));
#else
return make_int4(
__float_as_int(a.x), __float_as_int(a.y), __float_as_int(a.z), __float_as_int(a.w));
#endif
}
#if !defined(__KERNEL_METAL__)
ccl_device_inline float4 operator-(const float4 &a)
{
# ifdef __KERNEL_SSE__
const __m128 mask = _mm_castsi128_ps(_mm_set1_epi32(0x80000000));
return float4(_mm_xor_ps(a.m128, mask));
# else
return make_float4(-a.x, -a.y, -a.z, -a.w);
# endif
}
ccl_device_inline float4 operator*(const float4 a, const float4 b)
{
# ifdef __KERNEL_SSE__
return float4(_mm_mul_ps(a.m128, b.m128));
# else
return make_float4(a.x * b.x, a.y * b.y, a.z * b.z, a.w * b.w);
# endif
}
ccl_device_inline float4 operator*(const float4 a, const float f)
{
# if defined(__KERNEL_SSE__)
return a * make_float4(f);
# else
return make_float4(a.x * f, a.y * f, a.z * f, a.w * f);
# endif
}
ccl_device_inline float4 operator*(float f, const float4 a)
{
return a * f;
}
ccl_device_inline float4 operator/(const float f, const float4 a)
{
# ifdef __KERNEL_SSE__
return float4(_mm_div_ps(_mm_set1_ps(f), a.m128));
# else
return make_float4(f / a.x, f / a.y, f / a.z, f / a.w);
# endif
}
ccl_device_inline float4 operator/(const float4 a, const float f)
{
return a * (1.0f / f);
}
ccl_device_inline float4 operator/(const float4 a, const float4 b)
{
# ifdef __KERNEL_SSE__
return float4(_mm_div_ps(a.m128, b.m128));
# else
return make_float4(a.x / b.x, a.y / b.y, a.z / b.z, a.w / b.w);
# endif
}
ccl_device_inline float4 operator+(const float4 a, const float4 b)
{
# ifdef __KERNEL_SSE__
return float4(_mm_add_ps(a.m128, b.m128));
# else
return make_float4(a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w);
# endif
}
ccl_device_inline float4 operator+(const float4 a, const float f)
{
return a + make_float4(f);
}
ccl_device_inline float4 operator-(const float4 a, const float4 b)
{
# ifdef __KERNEL_SSE__
return float4(_mm_sub_ps(a.m128, b.m128));
# else
return make_float4(a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w);
# endif
}
ccl_device_inline float4 operator-(const float4 a, const float f)
{
return a - make_float4(f);
}
ccl_device_inline float4 operator+=(float4 &a, const float4 b)
{
return a = a + b;
}
ccl_device_inline float4 operator-=(float4 &a, const float4 b)
{
return a = a - b;
}
ccl_device_inline float4 operator*=(float4 &a, const float4 b)
{
return a = a * b;
}
ccl_device_inline float4 operator*=(float4 &a, const float f)
{
return a = a * f;
}
ccl_device_inline float4 operator/=(float4 &a, const float4 b)
{
return a = a / b;
}
ccl_device_inline float4 operator/=(float4 &a, const float f)
{
return a = a / f;
}
ccl_device_inline int4 operator<(const float4 a, const float4 b)
{
# ifdef __KERNEL_SSE__
return int4(_mm_castps_si128(_mm_cmplt_ps(a.m128, b.m128)));
# else
return make_int4(a.x < b.x, a.y < b.y, a.z < b.z, a.w < b.w);
# endif
}
ccl_device_inline int4 operator>=(const float4 a, const float4 b)
{
# ifdef __KERNEL_SSE__
return int4(_mm_castps_si128(_mm_cmpge_ps(a.m128, b.m128)));
# else
return make_int4(a.x >= b.x, a.y >= b.y, a.z >= b.z, a.w >= b.w);
# endif
}
ccl_device_inline int4 operator>(const float4 a, const float4 b)
{
# ifdef __KERNEL_SSE__
return int4(_mm_castps_si128(_mm_cmpgt_ps(a.m128, b.m128)));
# else
return make_int4(a.x > b.x, a.y > b.y, a.z > b.z, a.w > b.w);
# endif
}
ccl_device_inline int4 operator<=(const float4 a, const float4 b)
{
# ifdef __KERNEL_SSE__
return int4(_mm_castps_si128(_mm_cmple_ps(a.m128, b.m128)));
# else
return make_int4(a.x <= b.x, a.y <= b.y, a.z <= b.z, a.w <= b.w);
# endif
}
ccl_device_inline bool operator==(const float4 a, const float4 b)
{
# ifdef __KERNEL_SSE__
return (_mm_movemask_ps(_mm_cmpeq_ps(a.m128, b.m128)) & 15) == 15;
# else
return (a.x == b.x && a.y == b.y && a.z == b.z && a.w == b.w);
# endif
}
ccl_device_inline float4 operator^(const float4 a, const float4 b)
{
# ifdef __KERNEL_SSE__
return float4(_mm_xor_ps(a.m128, b.m128));
# else
return make_float4(__uint_as_float(__float_as_uint(a.x) ^ __float_as_uint(b.x)),
__uint_as_float(__float_as_uint(a.y) ^ __float_as_uint(b.y)),
__uint_as_float(__float_as_uint(a.z) ^ __float_as_uint(b.z)),
__uint_as_float(__float_as_uint(a.w) ^ __float_as_uint(b.w)));
# endif
}
ccl_device_inline float4 min(const float4 a, const float4 b)
{
# ifdef __KERNEL_SSE__
return float4(_mm_min_ps(a.m128, b.m128));
# else
return make_float4(min(a.x, b.x), min(a.y, b.y), min(a.z, b.z), min(a.w, b.w));
# endif
}
ccl_device_inline float4 max(const float4 a, const float4 b)
{
# ifdef __KERNEL_SSE__
return float4(_mm_max_ps(a.m128, b.m128));
# else
return make_float4(max(a.x, b.x), max(a.y, b.y), max(a.z, b.z), max(a.w, b.w));
# endif
}
ccl_device_inline float4 clamp(const float4 a, const float4 mn, const float4 mx)
{
return min(max(a, mn), mx);
}
#endif /* !__KERNEL_METAL__ */
ccl_device_inline float4 madd(const float4 a, const float4 b, const float4 c)
{
#ifdef __KERNEL_SSE__
# ifdef __KERNEL_NEON__
return float4(vfmaq_f32(c, a, b));
# elif defined(__KERNEL_AVX2__)
return float4(_mm_fmadd_ps(a, b, c));
# else
return a * b + c;
# endif
#else
return a * b + c;
#endif
}
ccl_device_inline float4 msub(const float4 a, const float4 b, const float4 c)
{
#ifdef __KERNEL_SSE__
# ifdef __KERNEL_NEON__
return float4(vfmaq_f32(vnegq_f32(c), a, b));
# elif defined(__KERNEL_AVX2__)
return float4(_mm_fmsub_ps(a, b, c));
# else
return a * b - c;
# endif
#else
return a * b - c;
#endif
}
#ifdef __KERNEL_SSE__
template<size_t i0, const size_t i1, const size_t i2, const size_t i3>
__forceinline float4 shuffle(const float4 a)
{
# ifdef __KERNEL_NEON__
return float4(shuffle_neon<float32x4_t, i0, i1, i2, i3>(a.m128));
# else
return float4(
_mm_castsi128_ps(_mm_shuffle_epi32(_mm_castps_si128(a), _MM_SHUFFLE(i3, i2, i1, i0))));
# endif
}
template<> __forceinline float4 shuffle<0, 1, 0, 1>(const float4 a)
{
return float4(_mm_movelh_ps(a, a));
}
template<> __forceinline float4 shuffle<2, 3, 2, 3>(const float4 a)
{
return float4(_mm_movehl_ps(a, a));
}
# ifdef __KERNEL_SSE3__
template<> __forceinline float4 shuffle<0, 0, 2, 2>(const float4 a)
{
return float4(_mm_moveldup_ps(a));
}
template<> __forceinline float4 shuffle<1, 1, 3, 3>(const float4 a)
{
return float4(_mm_movehdup_ps(a));
}
# endif /* __KERNEL_SSE3__ */
template<size_t i0, const size_t i1, const size_t i2, const size_t i3>
__forceinline float4 shuffle(const float4 a, const float4 b)
{
# ifdef __KERNEL_NEON__
return float4(shuffle_neon<float32x4_t, i0, i1, i2, i3>(a, b));
# else
return float4(_mm_shuffle_ps(a, b, _MM_SHUFFLE(i3, i2, i1, i0)));
# endif
}
template<size_t i0> __forceinline float4 shuffle(const float4 a)
{
return shuffle<i0, i0, i0, i0>(a);
}
template<size_t i0> __forceinline float4 shuffle(const float4 a, const float4 b)
{
# ifdef __KERNEL_NEON__
return float4(shuffle_neon<float32x4_t, i0, i0, i0, i0>(a, b));
# else
return float4(_mm_shuffle_ps(a, b, _MM_SHUFFLE(i0, i0, i0, i0)));
# endif
}
template<> __forceinline float4 shuffle<0, 1, 0, 1>(const float4 a, const float4 b)
{
return float4(_mm_movelh_ps(a, b));
}
template<> __forceinline float4 shuffle<2, 3, 2, 3>(const float4 a, const float4 b)
{
return float4(_mm_movehl_ps(b, a));
}
template<size_t i> __forceinline float extract(const float4 a)
{
return _mm_cvtss_f32(shuffle<i, i, i, i>(a));
}
template<> __forceinline float extract<0>(const float4 a)
{
return _mm_cvtss_f32(a);
}
#endif
ccl_device_inline float reduce_add(const float4 a)
{
#if defined(__KERNEL_SSE__)
# if defined(__KERNEL_NEON__)
return vaddvq_f32(a);
# elif defined(__KERNEL_SSE3__)
float4 h(_mm_hadd_ps(a.m128, a.m128));
return _mm_cvtss_f32(_mm_hadd_ps(h.m128, h.m128));
# else
float4 h(shuffle<1, 0, 3, 2>(a) + a);
return _mm_cvtss_f32(shuffle<2, 3, 0, 1>(h) + h);
# endif
#else
return a.x + a.y + a.z + a.w;
#endif
}
ccl_device_inline float reduce_min(const float4 a)
{
#if defined(__KERNEL_SSE__)
# if defined(__KERNEL_NEON__)
return vminvq_f32(a);
# else
float4 h = min(shuffle<1, 0, 3, 2>(a), a);
return _mm_cvtss_f32(min(shuffle<2, 3, 0, 1>(h), h));
# endif
#else
return min(min(a.x, a.y), min(a.z, a.w));
#endif
}
ccl_device_inline float reduce_max(const float4 a)
{
#if defined(__KERNEL_SSE__)
# if defined(__KERNEL_NEON__)
return vmaxvq_f32(a);
# else
float4 h = max(shuffle<1, 0, 3, 2>(a), a);
return _mm_cvtss_f32(max(shuffle<2, 3, 0, 1>(h), h));
# endif
#else
return max(max(a.x, a.y), max(a.z, a.w));
#endif
}
#if !defined(__KERNEL_METAL__)
ccl_device_inline float dot(const float4 a, const float4 b)
{
# if defined(__KERNEL_SSE42__) && defined(__KERNEL_SSE__)
# if defined(__KERNEL_NEON__)
const __m128 t = vmulq_f32(a, b);
return vaddvq_f32(t);
# else
return _mm_cvtss_f32(_mm_dp_ps(a, b, 0xFF));
# endif
# else
return (a.x * b.x + a.y * b.y) + (a.z * b.z + a.w * b.w);
# endif
}
#endif /* !defined(__KERNEL_METAL__) */
ccl_device_inline float len(const float4 a)
{
return sqrtf(dot(a, a));
}
ccl_device_inline float len_squared(const float4 a)
{
return dot(a, a);
}
ccl_device_inline float4 sqr(const float4 a)
{
return a * a;
}
#if !defined(__KERNEL_METAL__)
ccl_device_inline float distance(const float4 a, const float4 b)
{
return len(a - b);
}
ccl_device_inline float4 sqrt(const float4 a)
{
# ifdef __KERNEL_SSE__
return float4(_mm_sqrt_ps(a.m128));
# else
return make_float4(sqrtf(a.x), sqrtf(a.y), sqrtf(a.z), sqrtf(a.w));
# endif
}
ccl_device_inline float4 cross(const float4 a, const float4 b)
{
# ifdef __KERNEL_SSE__
return (shuffle<1, 2, 0, 0>(a) * shuffle<2, 0, 1, 0>(b)) -
(shuffle<2, 0, 1, 0>(a) * shuffle<1, 2, 0, 0>(b));
# else
return make_float4(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x, 0.0f);
# endif
}
ccl_device_inline bool is_zero(const float4 a)
{
# ifdef __KERNEL_SSE__
return a == zero_float4();
# else
return (a.x == 0.0f && a.y == 0.0f && a.z == 0.0f && a.w == 0.0f);
# endif
}
ccl_device_inline float average(const float4 a)
{
return reduce_add(a) * 0.25f;
}
ccl_device_inline float4 normalize(const float4 a)
{
return a / len(a);
}
ccl_device_inline float4 safe_normalize(const float4 a)
{
const float t = len(a);
return (t != 0.0f) ? a / t : a;
}
ccl_device_inline float4 fabs(const float4 a)
{
# if defined(__KERNEL_SSE__)
# if defined(__KERNEL_NEON__)
return float4(vabsq_f32(a));
# else
return float4(_mm_and_ps(a.m128, _mm_castsi128_ps(_mm_set1_epi32(0x7fffffff))));
# endif
# else
return make_float4(fabsf(a.x), fabsf(a.y), fabsf(a.z), fabsf(a.w));
# endif
}
/* The floating-point remainder of the division operation `a / b` calculated by this function is
* exactly the value `a - iquot * b`, where `iquot` is `a / b with` its fractional part truncated.
*
* The returned value has the same sign as `a` and is less than `b` in magnitude. */
ccl_device_inline float4 fmod(const float4 a, const float b)
{
# if defined(__KERNEL_NEON__)
/* Use native Neon instructions.
* The logic is the same as the SSE code below, but on Apple M2 Ultra this seems to be faster.
* Possibly due to some runtime checks in _mm_round_ps which do not get properly inlined. */
const float32x4_t iquot = vrndq_f32(a / b);
return float4(vsubq_f32(a, vmulq_f32(iquot, vdupq_n_f32(b))));
# elif defined(__KERNEL_SSE42__) && defined(__KERNEL_SSE__)
const __m128 iquot = _mm_round_ps(a / b, _MM_FROUND_TRUNC);
return float4(_mm_sub_ps(a, _mm_mul_ps(iquot, _mm_set1_ps(b))));
# else
return make_float4(fmodf(a.x, b), fmodf(a.y, b), fmodf(a.z, b), fmodf(a.w, b));
# endif
}
ccl_device_inline float4 floor(const float4 a)
{
# ifdef __KERNEL_SSE__
# if defined(__KERNEL_NEON__)
return float4(vrndmq_f32(a));
# else
return float4(_mm_floor_ps(a));
# endif
# else
return make_float4(floorf(a.x), floorf(a.y), floorf(a.z), floorf(a.w));
# endif
}
ccl_device_inline float4 floorfrac(const float4 x, ccl_private int4 *i)
{
# ifdef __KERNEL_SSE__
const float4 f = floor(x);
*i = int4(_mm_cvttps_epi32(f.m128));
return x - f;
# else
float4 r;
r.x = floorfrac(x.x, &i->x);
r.y = floorfrac(x.y, &i->y);
r.z = floorfrac(x.z, &i->z);
r.w = floorfrac(x.w, &i->w);
return r;
# endif
}
ccl_device_inline float4 mix(const float4 a, const float4 b, const float t)
{
return a + t * (b - a);
}
ccl_device_inline float4 mix(const float4 a, const float4 b, const float4 t)
{
return a + t * (b - a);
}
ccl_device_inline float4 saturate(const float4 a)
{
return make_float4(saturatef(a.x), saturatef(a.y), saturatef(a.z), saturatef(a.w));
}
ccl_device_inline float4 exp(const float4 v)
{
return make_float4(expf(v.x), expf(v.y), expf(v.z), expf(v.z));
}
ccl_device_inline float4 log(const float4 v)
{
return make_float4(logf(v.x), logf(v.y), logf(v.z), logf(v.z));
}
#endif /* !__KERNEL_METAL__ */
ccl_device_inline bool isequal(const float4 a, const float4 b)
{
#if defined(__KERNEL_METAL__)
return all(a == b);
#else
return a == b;
#endif
}
template<class MaskType>
ccl_device_inline float4 select(const MaskType mask, const float4 a, const float4 b)
{
#if defined(__KERNEL_METAL__)
return metal::select(b, a, bool4(mask));
#elif defined(__KERNEL_SSE__)
# ifdef __KERNEL_SSE42__
return float4(_mm_blendv_ps(b.m128, a.m128, _mm_castsi128_ps(mask.m128)));
# else
return float4(
_mm_or_ps(_mm_and_ps(_mm_castsi128_ps(mask), a), _mm_andnot_ps(_mm_castsi128_ps(mask), b)));
# endif
#else
return make_float4(
(mask.x) ? a.x : b.x, (mask.y) ? a.y : b.y, (mask.z) ? a.z : b.z, (mask.w) ? a.w : b.w);
#endif
}
template<class MaskType> ccl_device_inline float4 mask(const MaskType mask, const float4 a)
{
/* Replace elements of x with zero where mask isn't set. */
return select(mask, a, zero_float4());
}
#ifndef __KERNEL_GPU__
ccl_device_inline float4 load_float4(const ccl_private float *v)
{
# ifdef __KERNEL_SSE__
return float4(_mm_loadu_ps(v));
# else
return make_float4(v[0], v[1], v[2], v[3]);
# endif
}
#endif /* !__KERNEL_GPU__ */
ccl_device_inline float4 safe_divide(const float4 a, const float b)
{
return (b != 0.0f) ? a / b : zero_float4();
}
ccl_device_inline float4 safe_divide(const float4 a, const float4 b)
{
return make_float4((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,
(b.w != 0.0f) ? a.w / b.w : 0.0f);
}
ccl_device_inline bool isfinite_safe(const float4 v)
{
return isfinite_safe(v.x) && isfinite_safe(v.y) && isfinite_safe(v.z) && isfinite_safe(v.w);
}
ccl_device_inline float4 ensure_finite(const float4 v)
{
float4 r = v;
if (!isfinite_safe(r.x)) {
r.x = 0.0f;
}
if (!isfinite_safe(r.y)) {
r.y = 0.0f;
}
if (!isfinite_safe(r.z)) {
r.z = 0.0f;
}
if (!isfinite_safe(r.w)) {
r.w = 0.0f;
}
return r;
}
/* Consistent name for this would be pow, but HIP compiler crashes in name mangling. */
ccl_device_inline float4 power(const float4 v, const float e)
{
return make_float4(powf(v.x, e), powf(v.y, e), powf(v.z, e), powf(v.w, e));
}
ccl_device_inline float4 interp(float4 a, float4 b, float t)
{
return a + t * (b - a);
}
#if !defined(__KERNEL_METAL__) && !defined(__KERNEL_ONEAPI__)
/* Int/Float conversion */
ccl_device_inline int4 __float4_as_int4(const float4 f)
{
# ifdef __KERNEL_SSE__
return int4(_mm_castps_si128(f.m128));
# else
return make_int4(
__float_as_int(f.x), __float_as_int(f.y), __float_as_int(f.z), __float_as_int(f.w));
# endif
}
ccl_device_inline float4 __int4_as_float4(const int4 i)
{
# ifdef __KERNEL_SSE__
return float4(_mm_castsi128_ps(i.m128));
# else
return make_float4(
__int_as_float(i.x), __int_as_float(i.y), __int_as_float(i.z), __int_as_float(i.w));
# endif
}
#endif /* !defined(__KERNEL_METAL__) && !defined(__KERNEL_ONEAPI__) */
ccl_device_inline void copy_v4_v4(ccl_private float *r, const float4 val)
{
r[0] = val.x;
r[1] = val.y;
r[2] = val.z;
r[3] = val.w;
}
CCL_NAMESPACE_END

View File

@@ -0,0 +1,471 @@
/* SPDX-FileCopyrightText: 2011-2013 Intel Corporation
* SPDX-FileCopyrightText: 2022 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#pragma once
#include "util/math_base.h"
#include "util/types_float8.h"
#include "util/types_int8.h"
CCL_NAMESPACE_BEGIN
ccl_device_inline vfloat8 zero_vfloat8()
{
#ifdef __KERNEL_AVX__
return vfloat8(_mm256_setzero_ps());
#else
return make_vfloat8(0.0f);
#endif
}
ccl_device_inline vfloat8 one_vfloat8()
{
return make_vfloat8(1.0f);
}
ccl_device_inline vfloat8 operator+(const vfloat8 a, const vfloat8 b)
{
#ifdef __KERNEL_AVX__
return vfloat8(_mm256_add_ps(a.m256, b.m256));
#else
return make_vfloat8(
a.a + b.a, a.b + b.b, a.c + b.c, a.d + b.d, a.e + b.e, a.f + b.f, a.g + b.g, a.h + b.h);
#endif
}
ccl_device_inline vfloat8 operator+(const vfloat8 a, const float f)
{
return a + make_vfloat8(f);
}
ccl_device_inline vfloat8 operator+(const float f, const vfloat8 a)
{
return make_vfloat8(f) + a;
}
ccl_device_inline vfloat8 operator-(const vfloat8 a)
{
#ifdef __KERNEL_AVX__
__m256 mask = _mm256_castsi256_ps(_mm256_set1_epi32(0x80000000));
return vfloat8(_mm256_xor_ps(a.m256, mask));
#else
return make_vfloat8(-a.a, -a.b, -a.c, -a.d, -a.e, -a.f, -a.g, -a.h);
#endif
}
ccl_device_inline vfloat8 operator-(const vfloat8 a, const vfloat8 b)
{
#ifdef __KERNEL_AVX__
return vfloat8(_mm256_sub_ps(a.m256, b.m256));
#else
return make_vfloat8(
a.a - b.a, a.b - b.b, a.c - b.c, a.d - b.d, a.e - b.e, a.f - b.f, a.g - b.g, a.h - b.h);
#endif
}
ccl_device_inline vfloat8 operator-(const vfloat8 a, const float f)
{
return a - make_vfloat8(f);
}
ccl_device_inline vfloat8 operator-(const float f, const vfloat8 a)
{
return make_vfloat8(f) - a;
}
ccl_device_inline vfloat8 operator*(const vfloat8 a, const vfloat8 b)
{
#ifdef __KERNEL_AVX__
return vfloat8(_mm256_mul_ps(a.m256, b.m256));
#else
return make_vfloat8(
a.a * b.a, a.b * b.b, a.c * b.c, a.d * b.d, a.e * b.e, a.f * b.f, a.g * b.g, a.h * b.h);
#endif
}
ccl_device_inline vfloat8 operator*(const vfloat8 a, const float f)
{
return a * make_vfloat8(f);
}
ccl_device_inline vfloat8 operator*(const float f, const vfloat8 a)
{
return make_vfloat8(f) * a;
}
ccl_device_inline vfloat8 operator/(const vfloat8 a, const vfloat8 b)
{
#ifdef __KERNEL_AVX__
return vfloat8(_mm256_div_ps(a.m256, b.m256));
#else
return make_vfloat8(
a.a / b.a, a.b / b.b, a.c / b.c, a.d / b.d, a.e / b.e, a.f / b.f, a.g / b.g, a.h / b.h);
#endif
}
ccl_device_inline vfloat8 operator/(const vfloat8 a, const float f)
{
return a / make_vfloat8(f);
}
ccl_device_inline vfloat8 operator/(const float f, const vfloat8 a)
{
return make_vfloat8(f) / a;
}
ccl_device_inline vfloat8 operator+=(vfloat8 a, const vfloat8 b)
{
return a = a + b;
}
ccl_device_inline vfloat8 operator-=(vfloat8 a, const vfloat8 b)
{
return a = a - b;
}
ccl_device_inline vfloat8 operator*=(vfloat8 a, const vfloat8 b)
{
return a = a * b;
}
ccl_device_inline vfloat8 operator*=(vfloat8 a, const float f)
{
return a = a * f;
}
ccl_device_inline vfloat8 operator/=(vfloat8 a, const float f)
{
return a = a / f;
}
ccl_device_inline bool operator==(const vfloat8 a, const vfloat8 b)
{
#ifdef __KERNEL_AVX__
return (_mm256_movemask_ps(_mm256_castsi256_ps(
_mm256_cmpeq_epi32(_mm256_castps_si256(a.m256), _mm256_castps_si256(b.m256)))) &
0b11111111) == 0b11111111;
#else
return (a.a == b.a && a.b == b.b && a.c == b.c && a.d == b.d && a.e == b.e && a.f == b.f &&
a.g == b.g && a.h == b.h);
#endif
}
ccl_device_inline vfloat8 operator^(const vfloat8 a, const vfloat8 b)
{
#ifdef __KERNEL_AVX__
return vfloat8(_mm256_xor_ps(a.m256, b.m256));
#else
return make_vfloat8(__uint_as_float(__float_as_uint(a.a) ^ __float_as_uint(b.a)),
__uint_as_float(__float_as_uint(a.b) ^ __float_as_uint(b.b)),
__uint_as_float(__float_as_uint(a.c) ^ __float_as_uint(b.c)),
__uint_as_float(__float_as_uint(a.d) ^ __float_as_uint(b.d)),
__uint_as_float(__float_as_uint(a.e) ^ __float_as_uint(b.e)),
__uint_as_float(__float_as_uint(a.f) ^ __float_as_uint(b.f)),
__uint_as_float(__float_as_uint(a.g) ^ __float_as_uint(b.g)),
__uint_as_float(__float_as_uint(a.h) ^ __float_as_uint(b.h)));
#endif
}
ccl_device_inline vfloat8 sqrt(const vfloat8 a)
{
#ifdef __KERNEL_AVX__
return vfloat8(_mm256_sqrt_ps(a.m256));
#else
return make_vfloat8(sqrtf(a.a),
sqrtf(a.b),
sqrtf(a.c),
sqrtf(a.d),
sqrtf(a.e),
sqrtf(a.f),
sqrtf(a.g),
sqrtf(a.h));
#endif
}
ccl_device_inline vfloat8 sqr(const vfloat8 a)
{
return a * a;
}
ccl_device_inline bool is_zero(const vfloat8 a)
{
return a == make_vfloat8(0.0f);
}
ccl_device_inline float reduce_add(const vfloat8 a)
{
#ifdef __KERNEL_AVX__
vfloat8 b(_mm256_hadd_ps(a.m256, a.m256));
vfloat8 h(_mm256_hadd_ps(b.m256, b.m256));
return h[0] + h[4];
#else
return a.a + a.b + a.c + a.d + a.e + a.f + a.g + a.h;
#endif
}
ccl_device_inline float average(const vfloat8 a)
{
return reduce_add(a) / 8.0f;
}
ccl_device_inline vfloat8 min(const vfloat8 a, const vfloat8 b)
{
#ifdef __KERNEL_AVX__
return vfloat8(_mm256_min_ps(a.m256, b.m256));
#else
return make_vfloat8(min(a.a, b.a),
min(a.b, b.b),
min(a.c, b.c),
min(a.d, b.d),
min(a.e, b.e),
min(a.f, b.f),
min(a.g, b.g),
min(a.h, b.h));
#endif
}
ccl_device_inline vfloat8 max(const vfloat8 a, const vfloat8 b)
{
#ifdef __KERNEL_AVX__
return vfloat8(_mm256_max_ps(a.m256, b.m256));
#else
return make_vfloat8(max(a.a, b.a),
max(a.b, b.b),
max(a.c, b.c),
max(a.d, b.d),
max(a.e, b.e),
max(a.f, b.f),
max(a.g, b.g),
max(a.h, b.h));
#endif
}
ccl_device_inline vfloat8 clamp(const vfloat8 a, const vfloat8 mn, const vfloat8 mx)
{
return min(max(a, mn), mx);
}
ccl_device_inline vfloat8 select(const vint8 mask, const vfloat8 a, const vfloat8 b)
{
#ifdef __KERNEL_AVX__
return vfloat8(_mm256_blendv_ps(b, a, _mm256_castsi256_ps(mask)));
#else
return make_vfloat8((mask.a) ? a.a : b.a,
(mask.b) ? a.b : b.b,
(mask.c) ? a.c : b.c,
(mask.d) ? a.d : b.d,
(mask.e) ? a.e : b.e,
(mask.f) ? a.f : b.f,
(mask.g) ? a.g : b.g,
(mask.h) ? a.h : b.h);
#endif
}
ccl_device_inline vfloat8 fabs(const vfloat8 a)
{
#ifdef __KERNEL_AVX__
return vfloat8(_mm256_and_ps(a.m256, _mm256_castsi256_ps(_mm256_set1_epi32(0x7fffffff))));
#else
return make_vfloat8(fabsf(a.a),
fabsf(a.b),
fabsf(a.c),
fabsf(a.d),
fabsf(a.e),
fabsf(a.f),
fabsf(a.g),
fabsf(a.h));
#endif
}
ccl_device_inline vfloat8 mix(const vfloat8 a, const vfloat8 b, const float t)
{
return a + t * (b - a);
}
ccl_device_inline vfloat8 mix(const vfloat8 a, const vfloat8 b, vfloat8 t)
{
return a + t * (b - a);
}
ccl_device_inline vfloat8 saturate(const vfloat8 a)
{
return clamp(a, make_vfloat8(0.0f), make_vfloat8(1.0f));
}
ccl_device_inline vfloat8 exp(vfloat8 v)
{
return make_vfloat8(
expf(v.a), expf(v.b), expf(v.c), expf(v.d), expf(v.e), expf(v.f), expf(v.g), expf(v.h));
}
ccl_device_inline vfloat8 log(vfloat8 v)
{
return make_vfloat8(
logf(v.a), logf(v.b), logf(v.c), logf(v.d), logf(v.e), logf(v.f), logf(v.g), logf(v.h));
}
ccl_device_inline float dot(const vfloat8 a, const vfloat8 b)
{
#ifdef __KERNEL_AVX__
vfloat8 t(_mm256_dp_ps(a.m256, b.m256, 0xFF));
return t[0] + t[4];
#else
return (a.a * b.a) + (a.b * b.b) + (a.c * b.c) + (a.d * b.d) + (a.e * b.e) + (a.f * b.f) +
(a.g * b.g) + (a.h * b.h);
#endif
}
ccl_device_inline vfloat8 pow(vfloat8 v, const float e)
{
return make_vfloat8(powf(v.a, e),
powf(v.b, e),
powf(v.c, e),
powf(v.d, e),
powf(v.e, e),
powf(v.f, e),
powf(v.g, e),
powf(v.h, e));
}
ccl_device_inline float reduce_min(const vfloat8 a)
{
return min(min(min(a.a, a.b), min(a.c, a.d)), min(min(a.e, a.f), min(a.g, a.h)));
}
ccl_device_inline float reduce_max(const vfloat8 a)
{
return max(max(max(a.a, a.b), max(a.c, a.d)), max(max(a.e, a.f), max(a.g, a.h)));
}
ccl_device_inline bool isequal(const vfloat8 a, const vfloat8 b)
{
return a == b;
}
ccl_device_inline vfloat8 safe_divide(const vfloat8 a, const float b)
{
return (b != 0.0f) ? a / b : make_vfloat8(0.0f);
}
ccl_device_inline vfloat8 safe_divide(const vfloat8 a, const vfloat8 b)
{
return make_vfloat8((b.a != 0.0f) ? a.a / b.a : 0.0f,
(b.b != 0.0f) ? a.b / b.b : 0.0f,
(b.c != 0.0f) ? a.c / b.c : 0.0f,
(b.d != 0.0f) ? a.d / b.d : 0.0f,
(b.e != 0.0f) ? a.e / b.e : 0.0f,
(b.f != 0.0f) ? a.f / b.f : 0.0f,
(b.g != 0.0f) ? a.g / b.g : 0.0f,
(b.h != 0.0f) ? a.h / b.h : 0.0f);
}
ccl_device_inline vfloat8 ensure_finite(vfloat8 v)
{
v.a = ensure_finite(v.a);
v.b = ensure_finite(v.b);
v.c = ensure_finite(v.c);
v.d = ensure_finite(v.d);
v.e = ensure_finite(v.e);
v.f = ensure_finite(v.f);
v.g = ensure_finite(v.g);
v.h = ensure_finite(v.h);
return v;
}
ccl_device_inline bool isfinite_safe(vfloat8 v)
{
return isfinite_safe(v.a) && isfinite_safe(v.b) && isfinite_safe(v.c) && isfinite_safe(v.d) &&
isfinite_safe(v.e) && isfinite_safe(v.f) && isfinite_safe(v.g) && isfinite_safe(v.h);
}
ccl_device_inline vint8 cast(const vfloat8 a)
{
#ifdef __KERNEL_AVX__
return vint8(_mm256_castps_si256(a));
#else
return make_vint8(__float_as_int(a.a),
__float_as_int(a.b),
__float_as_int(a.c),
__float_as_int(a.d),
__float_as_int(a.e),
__float_as_int(a.f),
__float_as_int(a.g),
__float_as_int(a.h));
#endif
}
#ifdef __KERNEL_SSE__
ccl_device_forceinline float4 low(const vfloat8 a)
{
# ifdef __KERNEL_AVX__
return float4(_mm256_extractf128_ps(a.m256, 0));
# else
return make_float4(a.e, a.f, a.g, a.h);
# endif
}
ccl_device_forceinline float4 high(const vfloat8 a)
{
# ifdef __KERNEL_AVX__
return float4(_mm256_extractf128_ps(a.m256, 1));
# else
return make_float4(a.a, a.b, a.c, a.d);
# endif
}
template<int i0,
const int i1,
const int i2,
const int i3,
const int i4,
const int i5,
const int i6,
const int i7>
ccl_device_forceinline vfloat8 shuffle(const vfloat8 a)
{
# ifdef __KERNEL_AVX__
return vfloat8(_mm256_permutevar_ps(a, _mm256_set_epi32(i7, i6, i5, i4, i3, i2, i1, i0)));
# else
return make_vfloat8(a[i0], a[i1], a[i2], a[i3], a[i4 + 4], a[i5 + 4], a[i6 + 4], a[i7 + 4]);
# endif
}
template<size_t i0, const size_t i1, const size_t i2, const size_t i3>
ccl_device_forceinline vfloat8 shuffle(const vfloat8 a, const vfloat8 b)
{
# ifdef __KERNEL_AVX__
return vfloat8(_mm256_shuffle_ps(a, b, _MM_SHUFFLE(i3, i2, i1, i0)));
# else
return make_vfloat8(shuffle<i0, i1, i2, i3>(high(a), high(b)),
shuffle<i0, i1, i2, i3>(low(a), low(b)));
# endif
}
template<size_t i0, const size_t i1, const size_t i2, const size_t i3>
ccl_device_forceinline vfloat8 shuffle(const vfloat8 a)
{
return shuffle<i0, i1, i2, i3>(a, a);
}
template<size_t i0> ccl_device_forceinline vfloat8 shuffle(const vfloat8 a, const vfloat8 b)
{
return shuffle<i0, i0, i0, i0>(a, b);
}
template<size_t i0> ccl_device_forceinline vfloat8 shuffle(const vfloat8 a)
{
return shuffle<i0>(a, a);
}
template<size_t i> ccl_device_forceinline float extract(const vfloat8 a)
{
# ifdef __KERNEL_AVX__
__m256 b = shuffle<i, i, i, i>(a).m256;
return _mm256_cvtss_f32(b);
# else
return a[i];
# endif
}
#endif
CCL_NAMESPACE_END

View File

@@ -0,0 +1,59 @@
/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#pragma once
#include "util/defines.h"
#include "util/types_int2.h"
CCL_NAMESPACE_BEGIN
#if !defined(__KERNEL_METAL__)
ccl_device_inline bool operator==(const int2 a, const int2 b)
{
return (a.x == b.x && a.y == b.y);
}
ccl_device_inline int2 operator+(const int2 &a, const int2 &b)
{
return make_int2(a.x + b.x, a.y + b.y);
}
ccl_device_inline int2 operator+=(int2 &a, const int2 &b)
{
return a = a + b;
}
ccl_device_inline int2 operator-(const int2 &a, const int2 &b)
{
return make_int2(a.x - b.x, a.y - b.y);
}
ccl_device_inline int2 operator*(const int2 &a, const int2 &b)
{
return make_int2(a.x * b.x, a.y * b.y);
}
ccl_device_inline int2 operator/(const int2 &a, const int2 &b)
{
return make_int2(a.x / b.x, a.y / b.y);
}
ccl_device_inline int2 operator>>(const int2 a, const int i)
{
return make_int2(a.x >> i, a.y >> i);
}
ccl_device_inline int2 operator&(const int2 a, const int2 b)
{
return make_int2(a.x & b.x, a.y & b.y);
}
ccl_device_inline int2 operator^(const int2 a, const int2 b)
{
return make_int2(a.x ^ b.x, a.y ^ b.y);
}
#endif /* !__KERNEL_METAL__ */
CCL_NAMESPACE_END

View File

@@ -0,0 +1,117 @@
/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#pragma once
#include "util/types_int3.h"
CCL_NAMESPACE_BEGIN
#if !defined(__KERNEL_METAL__)
ccl_device_inline int3 min(const int3 a, const int3 b)
{
# if defined(__KERNEL_SSE__) && defined(__KERNEL_SSE42__)
return int3(_mm_min_epi32(a.m128, b.m128));
# else
return make_int3(min(a.x, b.x), min(a.y, b.y), min(a.z, b.z));
# endif
}
ccl_device_inline int3 max(const int3 a, const int3 b)
{
# if defined(__KERNEL_SSE__) && defined(__KERNEL_SSE42__)
return int3(_mm_max_epi32(a.m128, b.m128));
# else
return make_int3(max(a.x, b.x), max(a.y, b.y), max(a.z, b.z));
# endif
}
ccl_device_inline int3 clamp(const int3 a, const int mn, const int mx)
{
# ifdef __KERNEL_SSE__
return min(max(a, make_int3(mn)), make_int3(mx));
# else
return make_int3(clamp(a.x, mn, mx), clamp(a.y, mn, mx), clamp(a.z, mn, mx));
# endif
}
ccl_device_inline int3 clamp(const int3 a, int3 &mn, const int mx)
{
# ifdef __KERNEL_SSE__
return min(max(a, mn), make_int3(mx));
# else
return make_int3(clamp(a.x, mn.x, mx), clamp(a.y, mn.y, mx), clamp(a.z, mn.z, mx));
# endif
}
ccl_device_inline bool operator==(const int3 a, const int3 b)
{
return a.x == b.x && a.y == b.y && a.z == b.z;
}
ccl_device_inline bool operator!=(const int3 a, const int3 b)
{
return !(a == b);
}
ccl_device_inline bool operator<(const int3 a, const int3 b)
{
return a.x < b.x && a.y < b.y && a.z < b.z;
}
ccl_device_inline int3 operator+(const int3 a, const int3 b)
{
# ifdef __KERNEL_SSE__
return int3(_mm_add_epi32(a.m128, b.m128));
# else
return make_int3(a.x + b.x, a.y + b.y, a.z + b.z);
# endif
}
ccl_device_inline int3 operator-(const int3 a, const int3 b)
{
# ifdef __KERNEL_SSE__
return int3(_mm_sub_epi32(a.m128, b.m128));
# else
return make_int3(a.x - b.x, a.y - b.y, a.z - b.z);
# endif
}
ccl_device_inline int3 operator>>(const int3 a, const int i)
{
return make_int3(a.x >> i, a.y >> i, a.z >> i);
}
ccl_device_inline int3 operator*(const int3 a, const int3 b)
{
return make_int3(a.x * b.x, a.y * b.y, a.z * b.z);
}
ccl_device_inline int3 operator^(const int3 a, const int3 b)
{
return make_int3(a.x ^ b.x, a.y ^ b.y, a.z ^ b.z);
}
ccl_device_inline int3 operator&(const int3 a, const int3 b)
{
return make_int3(a.x & b.x, a.y & b.y, a.z & b.z);
}
ccl_device_inline int3 operator&(const int3 a, const int b)
{
# ifdef __KERNEL_SSE__
return int3(_mm_and_si128(a.m128, _mm_set1_epi32(b)));
# else
return make_int3(a.x & b, a.y & b, a.z & b);
# endif
}
ccl_device_inline bool all(const int3 a)
{
return a.x && a.y && a.z;
}
#endif /* !__KERNEL_METAL__ */
CCL_NAMESPACE_END

View File

@@ -0,0 +1,303 @@
/* SPDX-FileCopyrightText: 2011-2013 Intel Corporation
* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#pragma once
#include "util/types_float4.h"
#include "util/types_int4.h"
CCL_NAMESPACE_BEGIN
#if !defined(__KERNEL_METAL__)
ccl_device_inline int4 operator+(const int4 a, const int4 b)
{
# ifdef __KERNEL_SSE__
return int4(_mm_add_epi32(a.m128, b.m128));
# else
return make_int4(a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w);
# endif
}
ccl_device_inline int4 operator+=(int4 &a, const int4 b)
{
return a = a + b;
}
ccl_device_inline int4 operator-(const int4 a, const int4 b)
{
# ifdef __KERNEL_SSE__
return int4(_mm_sub_epi32(a.m128, b.m128));
# else
return make_int4(a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w);
# endif
}
ccl_device_inline int4 operator-=(int4 &a, const int4 b)
{
return a = a - b;
}
ccl_device_inline int4 operator*(const int4 a, const int4 b)
{
return make_int4(a.x * b.x, a.y * b.y, a.z * b.z, a.w * b.w);
}
ccl_device_inline int4 operator>>(const int4 a, const int i)
{
# ifdef __KERNEL_SSE__
return int4(_mm_srai_epi32(a.m128, i));
# else
return make_int4(a.x >> i, a.y >> i, a.z >> i, a.w >> i);
# endif
}
ccl_device_inline int4 operator<<(const int4 a, const int i)
{
# ifdef __KERNEL_SSE__
return int4(_mm_slli_epi32(a.m128, i));
# else
return make_int4(a.x << i, a.y << i, a.z << i, a.w << i);
# endif
}
ccl_device_inline int4 operator<(const int4 a, const int4 b)
{
# ifdef __KERNEL_SSE__
return int4(_mm_cmplt_epi32(a.m128, b.m128));
# else
return make_int4(a.x < b.x, a.y < b.y, a.z < b.z, a.w < b.w);
# endif
}
ccl_device_inline int4 operator<(const int4 a, const int b)
{
return a < make_int4(b);
}
ccl_device_inline int4 operator>(const int4 a, const int4 b)
{
return b < a;
}
ccl_device_inline int4 operator==(const int4 a, const int4 b)
{
# ifdef __KERNEL_SSE__
return int4(_mm_cmpeq_epi32(a.m128, b.m128));
# else
return make_int4(a.x == b.x, a.y == b.y, a.z == b.z, a.w == b.w);
# endif
}
ccl_device_inline int4 operator==(const int4 a, const int b)
{
return a == make_int4(b);
}
ccl_device_inline int4 operator>=(const int4 a, const int4 b)
{
# ifdef __KERNEL_SSE__
return int4(_mm_xor_si128(_mm_set1_epi32(0xffffffff), _mm_cmplt_epi32(a.m128, b.m128)));
# else
return make_int4(a.x >= b.x, a.y >= b.y, a.z >= b.z, a.w >= b.w);
# endif
}
ccl_device_inline int4 operator>=(const int4 a, const int b)
{
return a >= make_int4(b);
}
ccl_device_inline int4 operator&(const int4 a, const int4 b)
{
# ifdef __KERNEL_SSE__
return int4(_mm_and_si128(a.m128, b.m128));
# else
return make_int4(a.x & b.x, a.y & b.y, a.z & b.z, a.w & b.w);
# endif
}
ccl_device_inline int4 operator|(const int4 a, const int4 b)
{
# ifdef __KERNEL_SSE__
return int4(_mm_or_si128(a.m128, b.m128));
# else
return make_int4(a.x | b.x, a.y | b.y, a.z | b.z, a.w | b.w);
# endif
}
ccl_device_inline int4 operator^(const int4 a, const int4 b)
{
# ifdef __KERNEL_SSE__
return int4(_mm_xor_si128(a.m128, b.m128));
# else
return make_int4(a.x ^ b.x, a.y ^ b.y, a.z ^ b.z, a.w ^ b.w);
# endif
}
ccl_device_inline int4 operator&(const int32_t a, const int4 b)
{
return make_int4(a) & b;
}
ccl_device_inline int4 operator&(const int4 a, const int32_t b)
{
return a & make_int4(b);
}
ccl_device_inline int4 operator|(const int32_t a, const int4 b)
{
return make_int4(a) | b;
}
ccl_device_inline int4 operator|(const int4 a, const int32_t b)
{
return a | make_int4(b);
}
ccl_device_inline int4 operator^(const int32_t a, const int4 b)
{
return make_int4(a) ^ b;
}
ccl_device_inline int4 operator^(const int4 a, const int32_t b)
{
return a ^ make_int4(b);
}
ccl_device_inline int4 &operator&=(int4 &a, const int4 b)
{
return a = a & b;
}
ccl_device_inline int4 &operator&=(int4 &a, const int32_t b)
{
return a = a & b;
}
ccl_device_inline int4 &operator|=(int4 &a, const int4 b)
{
return a = a | b;
}
ccl_device_inline int4 &operator|=(int4 &a, const int32_t b)
{
return a = a | b;
}
ccl_device_inline int4 &operator^=(int4 &a, const int4 b)
{
return a = a ^ b;
}
ccl_device_inline int4 &operator^=(int4 &a, const int32_t b)
{
return a = a ^ b;
}
ccl_device_inline int4 &operator<<=(int4 &a, const int32_t b)
{
return a = a << b;
}
ccl_device_inline int4 &operator>>=(int4 &a, const int32_t b)
{
return a = a >> b;
}
ccl_device_inline int4 srl(const int4 a, const int i)
{
# ifdef __KERNEL_SSE__
return int4(_mm_srli_epi32(a.m128, i));
# else
return make_int4(uint32_t(a.x) >> i, uint32_t(a.y) >> i, uint32_t(a.z) >> i, uint32_t(a.w) >> i);
# endif
}
ccl_device_inline int4 min(const int4 a, const int4 b)
{
# if defined(__KERNEL_SSE__) && defined(__KERNEL_SSE42__)
return int4(_mm_min_epi32(a.m128, b.m128));
# else
return make_int4(min(a.x, b.x), min(a.y, b.y), min(a.z, b.z), min(a.w, b.w));
# endif
}
ccl_device_inline int4 max(const int4 a, const int4 b)
{
# if defined(__KERNEL_SSE__) && defined(__KERNEL_SSE42__)
return int4(_mm_max_epi32(a.m128, b.m128));
# else
return make_int4(max(a.x, b.x), max(a.y, b.y), max(a.z, b.z), max(a.w, b.w));
# endif
}
ccl_device_inline int4 clamp(const int4 a, const int4 mn, const int4 mx)
{
return min(max(a, mn), mx);
}
ccl_device_inline int4 select(const int4 mask, const int4 a, const int4 b)
{
# ifdef __KERNEL_SSE__
return int4(_mm_or_si128(_mm_and_si128(mask, a), _mm_andnot_si128(mask, b)));
# else
return make_int4(
(mask.x) ? a.x : b.x, (mask.y) ? a.y : b.y, (mask.z) ? a.z : b.z, (mask.w) ? a.w : b.w);
# endif
}
ccl_device_inline int4 load_int4(const int *v)
{
# ifdef __KERNEL_SSE__
return int4(_mm_loadu_si128((__m128i *)v));
# else
return make_int4(v[0], v[1], v[2], v[3]);
# endif
}
#endif /* __KERNEL_METAL__ */
ccl_device_inline float4 cast(const int4 a)
{
#ifdef __KERNEL_SSE__
return float4(_mm_castsi128_ps(a));
#else
return make_float4(
__int_as_float(a.x), __int_as_float(a.y), __int_as_float(a.z), __int_as_float(a.w));
#endif
}
#ifdef __KERNEL_SSE__
ccl_device_forceinline int4 andnot(const int4 a, const int4 b)
{
return int4(_mm_andnot_si128(a.m128, b.m128));
}
template<size_t i0, const size_t i1, const size_t i2, const size_t i3>
ccl_device_forceinline int4 shuffle(const int4 a)
{
# ifdef __KERNEL_NEON__
int32x4_t result = shuffle_neon<int32x4_t, i0, i1, i2, i3>(vreinterpretq_s32_m128i(a));
return int4(vreinterpretq_m128i_s32(result));
# else
return int4(_mm_shuffle_epi32(a, _MM_SHUFFLE(i3, i2, i1, i0)));
# endif
}
template<size_t i0, const size_t i1, const size_t i2, const size_t i3>
ccl_device_forceinline int4 shuffle(const int4 a, const int4 b)
{
# ifdef __KERNEL_NEON__
int32x4_t result = shuffle_neon<int32x4_t, i0, i1, i2, i3>(vreinterpretq_s32_m128i(a),
vreinterpretq_s32_m128i(b));
return int4(vreinterpretq_m128i_s32(result));
# else
return int4(_mm_castps_si128(
_mm_shuffle_ps(_mm_castsi128_ps(a), _mm_castsi128_ps(b), _MM_SHUFFLE(i3, i2, i1, i0))));
# endif
}
template<size_t i0> ccl_device_forceinline int4 shuffle(const int4 b)
{
return shuffle<i0, i0, i0, i0>(b);
}
#endif
CCL_NAMESPACE_END

View File

@@ -0,0 +1,353 @@
/* SPDX-FileCopyrightText: 2011-2013 Intel Corporation
* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#pragma once
#include "util/math_base.h"
#include "util/types_float8.h"
#include "util/types_int8.h"
CCL_NAMESPACE_BEGIN
#ifndef __KERNEL_GPU__
ccl_device_inline vint8 operator+(const vint8 a, const vint8 b)
{
# ifdef __KERNEL_AVX__
return vint8(_mm256_add_epi32(a.m256, b.m256));
# else
return make_vint8(
a.a + b.a, a.b + b.b, a.c + b.c, a.d + b.d, a.e + b.e, a.f + b.f, a.g + b.g, a.h + b.h);
# endif
}
ccl_device_inline vint8 operator+=(vint8 &a, const vint8 b)
{
return a = a + b;
}
ccl_device_inline vint8 operator-(const vint8 a, const vint8 b)
{
# ifdef __KERNEL_AVX__
return vint8(_mm256_sub_epi32(a.m256, b.m256));
# else
return make_vint8(
a.a - b.a, a.b - b.b, a.c - b.c, a.d - b.d, a.e - b.e, a.f - b.f, a.g - b.g, a.h - b.h);
# endif
}
ccl_device_inline vint8 operator-=(vint8 &a, const vint8 b)
{
return a = a - b;
}
ccl_device_inline vint8 operator>>(const vint8 a, const int i)
{
# ifdef __KERNEL_AVX__
return vint8(_mm256_srai_epi32(a.m256, i));
# else
return make_vint8(
a.a >> i, a.b >> i, a.c >> i, a.d >> i, a.e >> i, a.f >> i, a.g >> i, a.h >> i);
# endif
}
ccl_device_inline vint8 operator<<(const vint8 a, const int i)
{
# ifdef __KERNEL_AVX__
return vint8(_mm256_slli_epi32(a.m256, i));
# else
return make_vint8(
a.a << i, a.b << i, a.c << i, a.d << i, a.e << i, a.f << i, a.g << i, a.h << i);
# endif
}
ccl_device_inline vint8 operator<(const vint8 a, const vint8 b)
{
# ifdef __KERNEL_AVX__
return vint8(_mm256_cmpgt_epi32(b.m256, a.m256));
# else
return make_vint8(
a.a < b.a, a.b < b.b, a.c < b.c, a.d < b.d, a.e < b.e, a.f < b.f, a.g < b.g, a.h < b.h);
# endif
}
ccl_device_inline vint8 operator<(const vint8 a, const int b)
{
return a < make_vint8(b);
}
ccl_device_inline vint8 operator==(const vint8 a, const vint8 b)
{
# ifdef __KERNEL_AVX__
return vint8(_mm256_cmpeq_epi32(a.m256, b.m256));
# else
return make_vint8(a.a == b.a,
a.b == b.b,
a.c == b.c,
a.d == b.d,
a.e == b.e,
a.f == b.f,
a.g == b.g,
a.h == b.h);
# endif
}
ccl_device_inline vint8 operator==(const vint8 a, const int b)
{
return a == make_vint8(b);
}
ccl_device_inline vint8 operator>=(const vint8 a, const vint8 b)
{
# ifdef __KERNEL_AVX__
return vint8(
_mm256_xor_si256(_mm256_set1_epi32(0xffffffff), _mm256_cmpgt_epi32(b.m256, a.m256)));
# else
return make_vint8(a.a >= b.a,
a.b >= b.b,
a.c >= b.c,
a.d >= b.d,
a.e >= b.e,
a.f >= b.f,
a.g >= b.g,
a.h >= b.h);
# endif
}
ccl_device_inline vint8 operator>=(const vint8 a, const int b)
{
return a >= make_vint8(b);
}
ccl_device_inline vint8 operator&(const vint8 a, const vint8 b)
{
# ifdef __KERNEL_AVX__
return vint8(_mm256_and_si256(a.m256, b.m256));
# else
return make_vint8(
a.a & b.a, a.b & b.b, a.c & b.c, a.d & b.d, a.e & b.e, a.f & b.f, a.g & b.g, a.h & b.h);
# endif
}
ccl_device_inline vint8 operator|(const vint8 a, const vint8 b)
{
# ifdef __KERNEL_AVX__
return vint8(_mm256_or_si256(a.m256, b.m256));
# else
return make_vint8(
a.a | b.a, a.b | b.b, a.c | b.c, a.d | b.d, a.e | b.e, a.f | b.f, a.g | b.g, a.h | b.h);
# endif
}
ccl_device_inline vint8 operator^(const vint8 a, const vint8 b)
{
# ifdef __KERNEL_AVX__
return vint8(_mm256_xor_si256(a.m256, b.m256));
# else
return make_vint8(
a.a ^ b.a, a.b ^ b.b, a.c ^ b.c, a.d ^ b.d, a.e ^ b.e, a.f ^ b.f, a.g ^ b.g, a.h ^ b.h);
# endif
}
ccl_device_inline vint8 operator&(const int32_t a, const vint8 b)
{
return make_vint8(a) & b;
}
ccl_device_inline vint8 operator&(const vint8 a, const int32_t b)
{
return a & make_vint8(b);
}
ccl_device_inline vint8 operator|(const int32_t a, const vint8 b)
{
return make_vint8(a) | b;
}
ccl_device_inline vint8 operator|(const vint8 a, const int32_t b)
{
return a | make_vint8(b);
}
ccl_device_inline vint8 operator^(const int32_t a, const vint8 b)
{
return make_vint8(a) ^ b;
}
ccl_device_inline vint8 operator^(const vint8 a, const int32_t b)
{
return a ^ make_vint8(b);
}
ccl_device_inline vint8 &operator&=(vint8 &a, const vint8 b)
{
return a = a & b;
}
ccl_device_inline vint8 &operator&=(vint8 &a, const int32_t b)
{
return a = a & b;
}
ccl_device_inline vint8 &operator|=(vint8 &a, const vint8 b)
{
return a = a | b;
}
ccl_device_inline vint8 &operator|=(vint8 &a, const int32_t b)
{
return a = a | b;
}
ccl_device_inline vint8 &operator^=(vint8 &a, const vint8 b)
{
return a = a ^ b;
}
ccl_device_inline vint8 &operator^=(vint8 &a, const int32_t b)
{
return a = a ^ b;
}
ccl_device_inline vint8 &operator<<=(vint8 &a, const int32_t b)
{
return a = a << b;
}
ccl_device_inline vint8 &operator>>=(vint8 &a, const int32_t b)
{
return a = a >> b;
}
# ifdef __KERNEL_AVX__
const ccl_device_forceinline vint8 srl(const vint8 a, const int32_t b)
{
return vint8(_mm256_srli_epi32(a.m256, b));
}
# endif
ccl_device_inline vint8 min(vint8 a, vint8 b)
{
# if defined(__KERNEL_AVX__) && defined(__KERNEL_AVX41__)
return vint8(_mm256_min_epi32(a.m256, b.m256));
# else
return make_vint8(min(a.a, b.a),
min(a.b, b.b),
min(a.c, b.c),
min(a.d, b.d),
min(a.e, b.e),
min(a.f, b.f),
min(a.g, b.g),
min(a.h, b.h));
# endif
}
ccl_device_inline vint8 max(vint8 a, vint8 b)
{
# if defined(__KERNEL_AVX__) && defined(__KERNEL_AVX41__)
return vint8(_mm256_max_epi32(a.m256, b.m256));
# else
return make_vint8(max(a.a, b.a),
max(a.b, b.b),
max(a.c, b.c),
max(a.d, b.d),
max(a.e, b.e),
max(a.f, b.f),
max(a.g, b.g),
max(a.h, b.h));
# endif
}
ccl_device_inline vint8 clamp(const vint8 a, const vint8 mn, const vint8 mx)
{
return min(max(a, mn), mx);
}
ccl_device_inline vint8 select(const vint8 mask, const vint8 a, const vint8 b)
{
# ifdef __KERNEL_AVX__
return vint8(_mm256_castps_si256(_mm256_blendv_ps(
_mm256_castsi256_ps(b), _mm256_castsi256_ps(a), _mm256_castsi256_ps(mask))));
# else
return make_vint8((mask.a) ? a.a : b.a,
(mask.b) ? a.b : b.b,
(mask.c) ? a.c : b.c,
(mask.d) ? a.d : b.d,
(mask.e) ? a.e : b.e,
(mask.f) ? a.f : b.f,
(mask.g) ? a.g : b.g,
(mask.h) ? a.h : b.h);
# endif
}
ccl_device_inline vint8 load_vint8(const int *v)
{
# ifdef __KERNEL_AVX__
return vint8(_mm256_loadu_si256((__m256i *)v));
# else
return make_vint8(v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7]);
# endif
}
#endif /* __KERNEL_GPU__ */
ccl_device_inline vfloat8 cast(const vint8 a)
{
#ifdef __KERNEL_AVX__
return vfloat8(_mm256_castsi256_ps(a));
#else
return make_vfloat8(__int_as_float(a.a),
__int_as_float(a.b),
__int_as_float(a.c),
__int_as_float(a.d),
__int_as_float(a.e),
__int_as_float(a.f),
__int_as_float(a.g),
__int_as_float(a.h));
#endif
}
#ifdef __KERNEL_AVX__
template<size_t i> const ccl_device_forceinline vint8 shuffle(const vint8 a)
{
return vint8(
_mm256_castps_si256(_mm256_permute_ps(_mm256_castsi256_ps(a), _MM_SHUFFLE(i, i, i, i))));
}
template<size_t i0, const size_t i1> const ccl_device_forceinline vint8 shuffle(const vint8 a)
{
return vint8(_mm256_permute2f128_si256(a, a, (i1 << 4) | (i0 << 0)));
}
template<size_t i0, const size_t i1>
const ccl_device_forceinline vint8 shuffle(const vint8 a, const vint8 b)
{
return vint8(_mm256_permute2f128_si256(a, b, (i1 << 4) | (i0 << 0)));
}
template<size_t i0, const size_t i1, const size_t i2, const size_t i3>
const ccl_device_forceinline vint8 shuffle(const vint8 a)
{
return vint8(
_mm256_castps_si256(_mm256_permute_ps(_mm256_castsi256_ps(a), _MM_SHUFFLE(i3, i2, i1, i0))));
}
template<size_t i0, const size_t i1, const size_t i2, const size_t i3>
const ccl_device_forceinline vint8 shuffle(const vint8 a, const vint8 b)
{
return vint8(_mm256_castps_si256(_mm256_shuffle_ps(
_mm256_castsi256_ps(a), _mm256_castsi256_ps(b), _MM_SHUFFLE(i3, i2, i1, i0))));
}
template<> __forceinline const vint8 shuffle<0, 0, 2, 2>(const vint8 b)
{
return vint8(_mm256_castps_si256(_mm256_moveldup_ps(_mm256_castsi256_ps(b))));
}
template<> __forceinline const vint8 shuffle<1, 1, 3, 3>(const vint8 b)
{
return vint8(_mm256_castps_si256(_mm256_movehdup_ps(_mm256_castsi256_ps(b))));
}
template<> __forceinline const vint8 shuffle<0, 1, 0, 1>(const vint8 b)
{
return vint8(_mm256_castps_si256(
_mm256_castpd_ps(_mm256_movedup_pd(_mm256_castps_pd(_mm256_castsi256_ps(b))))));
}
#endif
CCL_NAMESPACE_END

View File

@@ -0,0 +1,480 @@
/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#pragma once
#include "util/math_float2.h"
#include "util/math_float3.h"
#include "util/math_float4.h"
CCL_NAMESPACE_BEGIN
/* Ray Intersection */
ccl_device bool ray_sphere_intersect(const float3 ray_P,
const float3 ray_D,
const float ray_tmin,
const float ray_tmax,
const float3 sphere_P,
const float sphere_radius,
ccl_private float3 *isect_P,
ccl_private float *isect_t)
{
const float3 d_vec = sphere_P - ray_P;
const float r_sq = sphere_radius * sphere_radius;
const float d_sq = dot(d_vec, d_vec);
const float d_cos_theta = dot(d_vec, ray_D);
if (d_sq > r_sq && d_cos_theta < 0.0f) {
/* Ray origin outside sphere and points away from sphere. */
return false;
}
const float d_sin_theta_sq = len_squared(d_vec - d_cos_theta * ray_D);
if (d_sin_theta_sq > r_sq) {
/* Closest point on ray outside sphere. */
return false;
}
/* Law of cosines. */
const float t = d_cos_theta - copysignf(sqrtf(r_sq - d_sin_theta_sq), d_sq - r_sq);
if (t > ray_tmin && t < ray_tmax) {
*isect_t = t;
*isect_P = ray_P + ray_D * t;
return true;
}
return false;
}
ccl_device bool ray_aligned_disk_intersect(const float3 ray_P,
const float3 ray_D,
const float ray_tmin,
const float ray_tmax,
const float3 disk_P,
const float disk_radius,
ccl_private float3 *isect_P,
ccl_private float *isect_t)
{
/* Aligned disk normal. */
float disk_t;
const float3 disk_N = normalize_len(ray_P - disk_P, &disk_t);
const float div = dot(ray_D, disk_N);
if (UNLIKELY(div == 0.0f)) {
return false;
}
/* Compute t to intersection point. */
const float t = -disk_t / div;
if (!(t > ray_tmin && t < ray_tmax)) {
return false;
}
/* Test if within radius. */
const float3 P = ray_P + ray_D * t;
if (len_squared(P - disk_P) > disk_radius * disk_radius) {
return false;
}
*isect_P = P;
*isect_t = t;
return true;
}
ccl_device bool ray_disk_intersect(const float3 ray_P,
const float3 ray_D,
const float ray_tmin,
const float ray_tmax,
const float3 disk_P,
const float3 disk_N,
const float disk_radius,
ccl_private float3 *isect_P,
ccl_private float *isect_t)
{
const float3 vp = ray_P - disk_P;
const float dp = dot(vp, disk_N);
const float cos_angle = dot(disk_N, -ray_D);
if (dp * cos_angle > 0.f) // front of light
{
const float t = dp / cos_angle;
if (t < 0.f) { /* Ray points away from the light. */
return false;
}
const float3 P = ray_P + t * ray_D;
const float3 T = P - disk_P;
if (dot(T, T) < sqr(disk_radius) && (t > ray_tmin && t < ray_tmax)) {
*isect_P = ray_P + t * ray_D;
*isect_t = t;
return true;
}
}
return false;
}
/* Custom rcp, cross and dot implementations that match Embree bit for bit. */
ccl_device_forceinline float ray_triangle_reciprocal(const float x)
{
#ifdef __KERNEL_NEON__
/* Move scalar to vector register and do rcp. */
__m128 a = {0};
a = vsetq_lane_f32(x, a, 0);
float32x4_t rt_rcp = vrecpeq_f32(a);
rt_rcp = vmulq_f32(vrecpsq_f32(a, rt_rcp), rt_rcp);
rt_rcp = vmulq_f32(vrecpsq_f32(a, rt_rcp), rt_rcp);
return vgetq_lane_f32(rt_rcp, 0);
#elif defined(__KERNEL_SSE__)
const __m128 a = _mm_set_ss(x);
const __m128 r = _mm_rcp_ss(a);
# ifdef __KERNEL_AVX2_
return _mm_cvtss_f32(_mm_mul_ss(r, _mm_fnmadd_ss(r, a, _mm_set_ss(2.0f))));
# else
return _mm_cvtss_f32(_mm_mul_ss(r, _mm_sub_ss(_mm_set_ss(2.0f), _mm_mul_ss(r, a))));
# endif
#else
return 1.0f / x;
#endif
}
ccl_device_inline float ray_triangle_dot(const float3 a, const float3 b)
{
#if defined(__KERNEL_SSE42__) && defined(__KERNEL_SSE__)
return madd(make_float4(a.x),
make_float4(b.x),
madd(make_float4(a.y), make_float4(b.y), make_float4(a.z) * make_float4(b.z)))[0];
#else
return a.x * b.x + a.y * b.y + a.z * b.z;
#endif
}
ccl_device_inline float3 ray_triangle_cross(const float3 a, const float3 b)
{
#if defined(__KERNEL_SSE42__) && defined(__KERNEL_SSE__)
return make_float3(
msub(make_float4(a.y), make_float4(b.z), make_float4(a.z) * make_float4(b.y))[0],
msub(make_float4(a.z), make_float4(b.x), make_float4(a.x) * make_float4(b.z))[0],
msub(make_float4(a.x), make_float4(b.y), make_float4(a.y) * make_float4(b.x))[0]);
#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
}
ccl_device_forceinline bool ray_triangle_intersect(const float3 ray_P,
const float3 ray_D,
const float ray_tmin,
const float ray_tmax,
const float3 tri_a,
const float3 tri_b,
const float3 tri_c,
ccl_private float *isect_u,
ccl_private float *isect_v,
ccl_private float *isect_t)
{
/* This implementation matches the Plücker coordinates triangle intersection
* in Embree. */
/* Calculate vertices relative to ray origin. */
const float3 v0 = tri_a - ray_P;
const float3 v1 = tri_b - ray_P;
const float3 v2 = tri_c - ray_P;
/* Calculate triangle edges. */
const float3 e0 = v2 - v0;
const float3 e1 = v0 - v1;
const float3 e2 = v1 - v2;
/* Perform edge tests. */
const float U = ray_triangle_dot(ray_triangle_cross(e0, v2 + v0), ray_D);
const float V = ray_triangle_dot(ray_triangle_cross(e1, v0 + v1), ray_D);
const float W = ray_triangle_dot(ray_triangle_cross(e2, v1 + v2), ray_D);
const float UVW = U + V + W;
const float eps = FLT_EPSILON * fabsf(UVW);
const float minUVW = min(U, min(V, W));
const float maxUVW = max(U, max(V, W));
if (!(minUVW >= -eps || maxUVW <= eps)) {
return false;
}
/* Calculate geometry normal and denominator. */
const float3 Ng1 = ray_triangle_cross(e1, e0);
const float3 Ng = Ng1 + Ng1;
const float den = dot(Ng, ray_D);
/* Avoid division by 0. */
if (UNLIKELY(den == 0.0f)) {
return false;
}
/* Perform depth test. */
const float T = dot(v0, Ng);
const float t = T / den;
if (!(t >= ray_tmin && t <= ray_tmax)) {
return false;
}
const float rcp_uvw = (fabsf(UVW) < 1e-18f) ? 0.0f : ray_triangle_reciprocal(UVW);
*isect_u = min(U * rcp_uvw, 1.0f);
*isect_v = min(V * rcp_uvw, 1.0f);
*isect_t = t;
return true;
}
ccl_device_forceinline float2 ray_triangle_uv(const float3 ray_P,
const float3 ray_D,
const float3 tri_a,
const float3 tri_b,
const float3 tri_c)
{
/* Matches ray_triangle-intersect, for an intersection known to exist. */
/* Calculate vertices relative to ray origin. */
const float3 v0 = tri_a - ray_P;
const float3 v1 = tri_b - ray_P;
const float3 v2 = tri_c - ray_P;
/* Calculate triangle edges. */
const float3 e0 = v2 - v0;
const float3 e1 = v0 - v1;
const float3 e2 = v1 - v2;
/* Compute barycentric coordinates. */
const float U = ray_triangle_dot(ray_triangle_cross(e0, v2 + v0), ray_D);
const float V = ray_triangle_dot(ray_triangle_cross(e1, v0 + v1), ray_D);
const float W = ray_triangle_dot(ray_triangle_cross(e2, v1 + v2), ray_D);
const float UVW = U + V + W;
const float rcp_uvw = (fabsf(UVW) < 1e-18f) ? 0.0f : ray_triangle_reciprocal(UVW);
return make_float2(clamp(U * rcp_uvw, 0.0f, 1.0f), clamp(V * rcp_uvw, 0.0f, 1.0f));
}
ccl_device_forceinline bool ray_triangle_intersect_self(const float3 ray_P,
const float3 ray_D,
const float3 verts[3])
{
/* Matches logic in ray_triangle_intersect, self intersection test to validate
* if a ray is going to hit self or might incorrectly hit a neighboring triangle. */
/* Calculate vertices relative to ray origin. */
const float3 v0 = verts[0] - ray_P;
const float3 v1 = verts[1] - ray_P;
const float3 v2 = verts[2] - ray_P;
/* Calculate triangle edges. */
const float3 e0 = v2 - v0;
const float3 e1 = v0 - v1;
const float3 e2 = v1 - v2;
/* Perform edge tests. */
const float U = ray_triangle_dot(ray_triangle_cross(v2 + v0, e0), ray_D);
const float V = ray_triangle_dot(ray_triangle_cross(v0 + v1, e1), ray_D);
const float W = ray_triangle_dot(ray_triangle_cross(v1 + v2, e2), ray_D);
const float eps = FLT_EPSILON * fabsf(U + V + W);
const float minUVW = min(U, min(V, W));
const float maxUVW = max(U, max(V, W));
/* Note the extended epsilon compared to ray_triangle_intersect, to account
* for intersections with neighboring triangles that have an epsilon. */
return (minUVW >= eps || maxUVW <= -eps);
}
/* Tests for an intersection between a ray and a quad defined by
* its midpoint, normal and sides.
* If ellipse is true, hits outside the ellipse that's enclosed by the
* quad are rejected.
*/
ccl_device bool ray_quad_intersect(const float3 ray_P,
const float3 ray_D,
const float ray_tmin,
const float ray_tmax,
const float3 quad_P,
const float3 inv_quad_u,
const float3 inv_quad_v,
const float3 quad_n,
ccl_private float3 *isect_P,
ccl_private float *isect_t,
ccl_private float *isect_u,
ccl_private float *isect_v,
bool ellipse)
{
/* Perform intersection test. */
const float t = -(dot(ray_P, quad_n) - dot(quad_P, quad_n)) / dot(ray_D, quad_n);
if (!(t > ray_tmin && t < ray_tmax)) {
return false;
}
const float3 hit = ray_P + t * ray_D;
const float3 inplane = hit - quad_P;
const float u = dot(inplane, inv_quad_u);
if (u < -0.5f || u > 0.5f) {
return false;
}
const float v = dot(inplane, inv_quad_v);
if (v < -0.5f || v > 0.5f) {
return false;
}
if (ellipse && (u * u + v * v > 0.25f)) {
return false;
}
/* Store the result. */
/* TODO(sergey): Check whether we can avoid some checks here. */
if (isect_P != nullptr) {
*isect_P = hit;
}
if (isect_t != nullptr) {
*isect_t = t;
}
/* NOTE: Return barycentric coordinates in the same notation as Embree and OptiX. */
if (isect_u != nullptr) {
*isect_u = v + 0.5f;
}
if (isect_v != nullptr) {
*isect_v = -u - v;
}
return true;
}
/* Find the ray segment that lies in the same side as the normal `N` of the plane.
* `P` is the vector pointing from any point on the plane to the ray origin. */
ccl_device bool ray_plane_intersect(const float3 N,
const float3 P,
const float3 ray_D,
ccl_private Interval<float> *t_range)
{
const float DN = dot(ray_D, N);
/* Distance from P to the plane. */
const float t = -dot(P, N) / DN;
/* Limit the range to the positive side. */
if (DN > 0.0f) {
t_range->min = fmaxf(t_range->min, t);
}
else {
t_range->max = fminf(t_range->max, t);
}
return !t_range->is_empty();
}
/* Find the ray segment inside an axis-aligned bounding box. */
ccl_device bool ray_aabb_intersect(const float3 bbox_min,
const float3 bbox_max,
const float3 ray_P,
const float3 ray_D,
ccl_private Interval<float> *t_range)
{
const float3 inv_ray_D = reciprocal(ray_D);
/* Absolute distances to lower and upper box coordinates; */
const float3 t_lower = (bbox_min - ray_P) * inv_ray_D;
const float3 t_upper = (bbox_max - ray_P) * inv_ray_D;
/* The four t-intervals (for x-/y-/z-slabs, and ray p(t)). */
const float4 tmins = make_float4(min(t_lower, t_upper), t_range->min);
const float4 tmaxes = make_float4(max(t_lower, t_upper), t_range->max);
/* Max of mins and min of maxes. */
const float tmin = reduce_max(tmins);
const float tmax = reduce_min(tmaxes);
*t_range = {tmin, tmax};
return !t_range->is_empty();
}
/* Find the segment of a ray defined by P + D * t that lies inside a cylinder defined by
* (x / len_u)^2 + (y / len_v)^2 = 1. */
ccl_device_inline bool ray_infinite_cylinder_intersect(const float3 P,
const float3 D,
const float len_u,
const float len_v,
ccl_private Interval<float> *t_range)
{
/* Convert to a 2D problem. */
const float2 inv_len = 1.0f / make_float2(len_u, len_v);
float2 P_proj = make_float2(P) * inv_len;
const float2 D_proj = make_float2(D) * inv_len;
/* Solve quadratic equation a*t^2 + 2b*t + c = 0. */
const float a = dot(D_proj, D_proj);
float b = dot(P_proj, D_proj);
/* Move ray origin closer to the cylinder to prevent precision issue when the ray is far away. */
const float t_mid = -b / a;
P_proj += D_proj * t_mid;
/* Recompute b from the shifted origin. */
b = dot(P_proj, D_proj);
const float c = dot(P_proj, P_proj) - 1.0f;
float tmin;
float tmax;
const bool valid = solve_quadratic(a, 2.0f * b, c, tmin, tmax);
*t_range = intervals_intersection(*t_range, {tmin + t_mid, tmax + t_mid});
return valid && !t_range->is_empty();
}
/**
* Find the ray segment inside a single-sided cone.
*
* \param axis: a unit-length direction around which the cone has a circular symmetry
* \param P: the vector pointing from the cone apex to the ray origin
* \param D: the direction of the ray, does not need to have unit-length
* \param cos_angle_sq: `sqr(cos(half_aperture_of_the_cone))`
* \param t_range: the ray segment that lies inside the cone
* \return whether the intersection exists and is in the provided range
*
* See https://www.geometrictools.com/Documentation/IntersectionLineCone.pdf for illustration
*/
ccl_device_inline bool ray_cone_intersect(const float3 axis,
const float3 P,
float3 D,
const float cos_angle_sq,
ccl_private Interval<float> *t_range)
{
if (cos_angle_sq < 1e-4f) {
/* The cone is nearly a plane. */
return ray_plane_intersect(axis, P, D, t_range);
}
const float inv_len = inversesqrtf(len_squared(D));
D *= inv_len;
const float AD = dot(axis, D);
const float AP = dot(axis, P);
const float a = sqr(AD) - cos_angle_sq;
const float b = 2.0f * (AD * AP - cos_angle_sq * dot(D, P));
const float c = sqr(AP) - cos_angle_sq * dot(P, P);
float tmin = 0.0f;
float tmax = FLT_MAX;
bool valid = solve_quadratic(a, b, c, tmin, tmax);
/* Check if the intersections are in the same hemisphere as the cone. */
const bool tmin_valid = AP + tmin * AD > 0.0f;
const bool tmax_valid = AP + tmax * AD > 0.0f;
valid &= (tmin_valid || tmax_valid);
if (!tmax_valid) {
tmax = tmin;
tmin = 0.0f;
}
else if (!tmin_valid) {
tmin = tmax;
tmax = FLT_MAX;
}
*t_range = intervals_intersection(*t_range, {tmin * inv_len, tmax * inv_len});
return valid && !t_range->is_empty();
}
CCL_NAMESPACE_END

View File

@@ -0,0 +1,393 @@
/* SPDX-FileCopyrightText: 1999, 2002 Aladdin Enterprises. All rights reserved.
*
* SPDX-License-Identifier: Zlib
*
* By `L. Peter Deutsch <ghost@aladdin.com>`. */
/* Minor modifications done to remove some code and change style. */
#include "util/md5.h"
#include "util/log.h"
#include "util/path.h"
#include <cstdio>
#include <cstring>
CCL_NAMESPACE_BEGIN
// NOLINTBEGIN
#define T_MASK ((uint32_t)~0)
#define T1 /* 0xd76aa478 */ (T_MASK ^ 0x28955b87)
#define T2 /* 0xe8c7b756 */ (T_MASK ^ 0x173848a9)
#define T3 0x242070db
#define T4 /* 0xc1bdceee */ (T_MASK ^ 0x3e423111)
#define T5 /* 0xf57c0faf */ (T_MASK ^ 0x0a83f050)
#define T6 0x4787c62a
#define T7 /* 0xa8304613 */ (T_MASK ^ 0x57cfb9ec)
#define T8 /* 0xfd469501 */ (T_MASK ^ 0x02b96afe)
#define T9 0x698098d8
#define T10 /* 0x8b44f7af */ (T_MASK ^ 0x74bb0850)
#define T11 /* 0xffff5bb1 */ (T_MASK ^ 0x0000a44e)
#define T12 /* 0x895cd7be */ (T_MASK ^ 0x76a32841)
#define T13 0x6b901122
#define T14 /* 0xfd987193 */ (T_MASK ^ 0x02678e6c)
#define T15 /* 0xa679438e */ (T_MASK ^ 0x5986bc71)
#define T16 0x49b40821
#define T17 /* 0xf61e2562 */ (T_MASK ^ 0x09e1da9d)
#define T18 /* 0xc040b340 */ (T_MASK ^ 0x3fbf4cbf)
#define T19 0x265e5a51
#define T20 /* 0xe9b6c7aa */ (T_MASK ^ 0x16493855)
#define T21 /* 0xd62f105d */ (T_MASK ^ 0x29d0efa2)
#define T22 0x02441453
#define T23 /* 0xd8a1e681 */ (T_MASK ^ 0x275e197e)
#define T24 /* 0xe7d3fbc8 */ (T_MASK ^ 0x182c0437)
#define T25 0x21e1cde6
#define T26 /* 0xc33707d6 */ (T_MASK ^ 0x3cc8f829)
#define T27 /* 0xf4d50d87 */ (T_MASK ^ 0x0b2af278)
#define T28 0x455a14ed
#define T29 /* 0xa9e3e905 */ (T_MASK ^ 0x561c16fa)
#define T30 /* 0xfcefa3f8 */ (T_MASK ^ 0x03105c07)
#define T31 0x676f02d9
#define T32 /* 0x8d2a4c8a */ (T_MASK ^ 0x72d5b375)
#define T33 /* 0xfffa3942 */ (T_MASK ^ 0x0005c6bd)
#define T34 /* 0x8771f681 */ (T_MASK ^ 0x788e097e)
#define T35 0x6d9d6122
#define T36 /* 0xfde5380c */ (T_MASK ^ 0x021ac7f3)
#define T37 /* 0xa4beea44 */ (T_MASK ^ 0x5b4115bb)
#define T38 0x4bdecfa9
#define T39 /* 0xf6bb4b60 */ (T_MASK ^ 0x0944b49f)
#define T40 /* 0xbebfbc70 */ (T_MASK ^ 0x4140438f)
#define T41 0x289b7ec6
#define T42 /* 0xeaa127fa */ (T_MASK ^ 0x155ed805)
#define T43 /* 0xd4ef3085 */ (T_MASK ^ 0x2b10cf7a)
#define T44 0x04881d05
#define T45 /* 0xd9d4d039 */ (T_MASK ^ 0x262b2fc6)
#define T46 /* 0xe6db99e5 */ (T_MASK ^ 0x1924661a)
#define T47 0x1fa27cf8
#define T48 /* 0xc4ac5665 */ (T_MASK ^ 0x3b53a99a)
#define T49 /* 0xf4292244 */ (T_MASK ^ 0x0bd6ddbb)
#define T50 0x432aff97
#define T51 /* 0xab9423a7 */ (T_MASK ^ 0x546bdc58)
#define T52 /* 0xfc93a039 */ (T_MASK ^ 0x036c5fc6)
#define T53 0x655b59c3
#define T54 /* 0x8f0ccc92 */ (T_MASK ^ 0x70f3336d)
#define T55 /* 0xffeff47d */ (T_MASK ^ 0x00100b82)
#define T56 /* 0x85845dd1 */ (T_MASK ^ 0x7a7ba22e)
#define T57 0x6fa87e4f
#define T58 /* 0xfe2ce6e0 */ (T_MASK ^ 0x01d3191f)
#define T59 /* 0xa3014314 */ (T_MASK ^ 0x5cfebceb)
#define T60 0x4e0811a1
#define T61 /* 0xf7537e82 */ (T_MASK ^ 0x08ac817d)
#define T62 /* 0xbd3af235 */ (T_MASK ^ 0x42c50dca)
#define T63 0x2ad7d2bb
#define T64 /* 0xeb86d391 */ (T_MASK ^ 0x14792c6e)
// NOLINTEND
void MD5Hash::process(const uint8_t *data /*[64]*/)
{
uint32_t a = abcd[0];
uint32_t b = abcd[1];
uint32_t c = abcd[2];
uint32_t d = abcd[3];
uint32_t t;
/* Define storage for little-endian or both types of CPUs. */
uint32_t xbuf[16];
const uint32_t *X;
{
/*
* Determine dynamically whether this is a big-endian or
* little-endian machine, since we can use a more efficient
* algorithm on the latter.
*/
static const int w = 1;
if (*((const uint8_t *)&w)) /* dynamic little-endian */ {
#if defined(__GNUC__) && defined(__clang__)
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wnull-pointer-subtraction"
#endif
/*
* On little-endian machines, we can process properly aligned
* data without copying it.
*/
if (!((data - (const uint8_t *)nullptr) & 3)) {
/* data are properly aligned */
X = (const uint32_t *)data;
}
else {
/* not aligned */
memcpy(xbuf, data, 64);
X = xbuf;
}
#if defined(__GNUC__) && defined(__clang__)
# pragma clang diagnostic pop
#endif
}
else { /* dynamic big-endian */
/*
* On big-endian machines, we must arrange the bytes in the
* right order.
*/
const uint8_t *xp = data;
int i;
X = xbuf; /* (dynamic only) */
for (i = 0; i < 16; ++i, xp += 4) {
xbuf[i] = xp[0] + (xp[1] << 8) + (xp[2] << 16) + (xp[3] << 24);
}
}
}
#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32 - (n))))
/* Round 1. */
/* Let [abcd k s i] denote the operation
* a = b + ((a + F(b,c,d) + X[k] + T[i]) <<< s). */
#define F(x, y, z) (((x) & (y)) | (~(x) & (z)))
#define SET(a, b, c, d, k, s, Ti) \
t = a + F(b, c, d) + X[k] + Ti; \
a = ROTATE_LEFT(t, s) + b
/* Do the following 16 operations. */
SET(a, b, c, d, 0, 7, T1);
SET(d, a, b, c, 1, 12, T2);
SET(c, d, a, b, 2, 17, T3);
SET(b, c, d, a, 3, 22, T4);
SET(a, b, c, d, 4, 7, T5);
SET(d, a, b, c, 5, 12, T6);
SET(c, d, a, b, 6, 17, T7);
SET(b, c, d, a, 7, 22, T8);
SET(a, b, c, d, 8, 7, T9);
SET(d, a, b, c, 9, 12, T10);
SET(c, d, a, b, 10, 17, T11);
SET(b, c, d, a, 11, 22, T12);
SET(a, b, c, d, 12, 7, T13);
SET(d, a, b, c, 13, 12, T14);
SET(c, d, a, b, 14, 17, T15);
SET(b, c, d, a, 15, 22, T16);
#undef SET
/* Round 2. */
/* Let [abcd k s i] denote the operation
* a = b + ((a + G(b,c,d) + X[k] + T[i]) <<< s). */
#define G(x, y, z) (((x) & (z)) | ((y) & ~(z)))
#define SET(a, b, c, d, k, s, Ti) \
t = a + G(b, c, d) + X[k] + Ti; \
a = ROTATE_LEFT(t, s) + b
/* Do the following 16 operations. */
SET(a, b, c, d, 1, 5, T17);
SET(d, a, b, c, 6, 9, T18);
SET(c, d, a, b, 11, 14, T19);
SET(b, c, d, a, 0, 20, T20);
SET(a, b, c, d, 5, 5, T21);
SET(d, a, b, c, 10, 9, T22);
SET(c, d, a, b, 15, 14, T23);
SET(b, c, d, a, 4, 20, T24);
SET(a, b, c, d, 9, 5, T25);
SET(d, a, b, c, 14, 9, T26);
SET(c, d, a, b, 3, 14, T27);
SET(b, c, d, a, 8, 20, T28);
SET(a, b, c, d, 13, 5, T29);
SET(d, a, b, c, 2, 9, T30);
SET(c, d, a, b, 7, 14, T31);
SET(b, c, d, a, 12, 20, T32);
#undef SET
/* Round 3. */
/* Let [abcd k s t] denote the operation
* a = b + ((a + H(b,c,d) + X[k] + T[i]) <<< s). */
#define H(x, y, z) ((x) ^ (y) ^ (z))
#define SET(a, b, c, d, k, s, Ti) \
t = a + H(b, c, d) + X[k] + Ti; \
a = ROTATE_LEFT(t, s) + b
/* Do the following 16 operations. */
SET(a, b, c, d, 5, 4, T33);
SET(d, a, b, c, 8, 11, T34);
SET(c, d, a, b, 11, 16, T35);
SET(b, c, d, a, 14, 23, T36);
SET(a, b, c, d, 1, 4, T37);
SET(d, a, b, c, 4, 11, T38);
SET(c, d, a, b, 7, 16, T39);
SET(b, c, d, a, 10, 23, T40);
SET(a, b, c, d, 13, 4, T41);
SET(d, a, b, c, 0, 11, T42);
SET(c, d, a, b, 3, 16, T43);
SET(b, c, d, a, 6, 23, T44);
SET(a, b, c, d, 9, 4, T45);
SET(d, a, b, c, 12, 11, T46);
SET(c, d, a, b, 15, 16, T47);
SET(b, c, d, a, 2, 23, T48);
#undef SET
/* Round 4. */
/* Let [abcd k s t] denote the operation
* a = b + ((a + I(b,c,d) + X[k] + T[i]) <<< s). */
#define I(x, y, z) ((y) ^ ((x) | ~(z)))
#define SET(a, b, c, d, k, s, Ti) \
t = a + I(b, c, d) + X[k] + Ti; \
a = ROTATE_LEFT(t, s) + b
/* Do the following 16 operations. */
SET(a, b, c, d, 0, 6, T49);
SET(d, a, b, c, 7, 10, T50);
SET(c, d, a, b, 14, 15, T51);
SET(b, c, d, a, 5, 21, T52);
SET(a, b, c, d, 12, 6, T53);
SET(d, a, b, c, 3, 10, T54);
SET(c, d, a, b, 10, 15, T55);
SET(b, c, d, a, 1, 21, T56);
SET(a, b, c, d, 8, 6, T57);
SET(d, a, b, c, 15, 10, T58);
SET(c, d, a, b, 6, 15, T59);
SET(b, c, d, a, 13, 21, T60);
SET(a, b, c, d, 4, 6, T61);
SET(d, a, b, c, 11, 10, T62);
SET(c, d, a, b, 2, 15, T63);
SET(b, c, d, a, 9, 21, T64);
#undef SET
/* Then perform the following additions. (That is increment each
* of the four registers by the value it had before this block
* was started.) */
abcd[0] += a;
abcd[1] += b;
abcd[2] += c;
abcd[3] += d;
}
MD5Hash::MD5Hash()
{
count[0] = count[1] = 0;
abcd[0] = 0x67452301;
abcd[1] = /*0xefcdab89*/ T_MASK ^ 0x10325476;
abcd[2] = /*0x98badcfe*/ T_MASK ^ 0x67452301;
abcd[3] = 0x10325476;
}
MD5Hash::~MD5Hash() = default;
void MD5Hash::append(const uint8_t *data, const int nbytes)
{
const uint8_t *p = data;
int left = nbytes;
const int offset = (count[0] >> 3) & 63;
const uint32_t nbits = (uint32_t)(nbytes << 3);
if (nbytes <= 0) {
return;
}
/* Update the message length. */
count[1] += nbytes >> 29;
count[0] += nbits;
if (count[0] < nbits) {
count[1]++;
}
/* Process an initial partial block. */
if (offset) {
const int copy = (offset + nbytes > 64 ? 64 - offset : nbytes);
memcpy(buf + offset, p, copy);
if (offset + copy < 64) {
return;
}
p += copy;
left -= copy;
process(buf);
}
/* Process full blocks. */
for (; left >= 64; p += 64, left -= 64) {
process(p);
}
/* Process a final partial block. */
if (left) {
memcpy(buf, p, left);
}
}
void MD5Hash::append(const string &str)
{
if (!str.empty()) {
append((const uint8_t *)str.c_str(), str.size());
}
}
bool MD5Hash::append_file(const string &filepath)
{
FILE *f = path_fopen(filepath, "rb");
if (!f) {
LOG_ERROR << "MD5: failed to open file " << filepath;
return false;
}
const size_t buffer_size = 1024;
uint8_t buffer[buffer_size];
size_t n;
do {
n = fread(buffer, 1, buffer_size, f);
append(buffer, n);
} while (n == buffer_size);
const bool success = (ferror(f) == 0);
fclose(f);
return success;
}
void MD5Hash::finish(uint8_t digest[16])
{
static const uint8_t pad[64] = {0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
uint8_t data[8];
int i;
/* Save the length before padding. */
for (i = 0; i < 8; ++i) {
data[i] = (uint8_t)(count[i >> 2] >> ((i & 3) << 3));
}
/* Pad to 56 bytes mod 64. */
append(pad, ((55 - (count[0] >> 3)) & 63) + 1);
/* Append the length. */
append(data, 8);
for (i = 0; i < 16; ++i) {
digest[i] = (uint8_t)(abcd[i >> 2] >> ((i & 3) << 3));
}
}
string MD5Hash::get_hex()
{
constexpr char kHexDigits[] = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
uint8_t digest[16];
char buf[16 * 2 + 1];
finish(digest);
for (int i = 0; i < 16; i++) {
buf[i * 2 + 0] = kHexDigits[digest[i] / 0x10];
buf[i * 2 + 1] = kHexDigits[digest[i] % 0x10];
}
buf[sizeof(buf) - 1] = '\0';
return string(buf);
}
string util_md5_string(const string &str)
{
MD5Hash md5;
md5.append((uint8_t *)str.c_str(), str.size());
return md5.get_hex();
}
CCL_NAMESPACE_END

View File

@@ -0,0 +1,40 @@
/* SPDX-FileCopyrightText: 1999, 2002 Aladdin Enterprises. All rights reserved.
*
* SPDX-License-Identifier: Zlib
*
* By `L. Peter Deutsch <ghost@aladdin.com>`. */
/* MD5
*
* Simply MD5 hash computation, used by disk cache. Adapted from external
* code, with minor code modifications done to remove some unused code and
* change code style. */
#pragma once
#include "util/string.h"
CCL_NAMESPACE_BEGIN
class MD5Hash {
public:
MD5Hash();
~MD5Hash();
void append(const uint8_t *data, const int nbytes);
void append(const string &str);
bool append_file(const string &filepath);
string get_hex();
protected:
void process(const uint8_t *data);
void finish(uint8_t digest[16]);
uint32_t count[2]; /* message length in bits, LSW first. */
uint32_t abcd[4]; /* digest buffer */
uint8_t buf[64]; /* accumulate block */
};
string util_md5_string(const string &str);
CCL_NAMESPACE_END

View File

@@ -0,0 +1,117 @@
/* SPDX-FileCopyrightText: 2018-2022 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
/* This is taken from alShaders/Cryptomatte/MurmurHash3.h:
*
* MurmurHash3 was written by Austin Appleby, and is placed in the public
* domain. The author hereby disclaims copyright to this source code.
*/
#include <cstdlib>
#include <cstring>
#include "util/math.h"
#include "util/murmurhash.h"
#if defined(_MSC_VER)
# define ROTL32(x, y) _rotl(x, y)
# define ROTL64(x, y) _rotl64(x, y)
# define BIG_CONSTANT(x) (x)
#else
ccl_device_inline uint32_t rotl32(const uint32_t x, int8_t r)
{
return (x << r) | (x >> (32 - r));
}
# define ROTL32(x, y) rotl32(x, y)
# define BIG_CONSTANT(x) (x##LLU)
#endif
CCL_NAMESPACE_BEGIN
/* Block read - if your platform needs to do endian-swapping or can only
* handle aligned reads, do the conversion here. */
ccl_device_inline uint32_t mm_hash_getblock32(const uint32_t *p, const int i)
{
return p[i];
}
/* Finalization mix - force all bits of a hash block to avalanche */
ccl_device_inline uint32_t mm_hash_fmix32(uint32_t h)
{
h ^= h >> 16;
h *= 0x85ebca6b;
h ^= h >> 13;
h *= 0xc2b2ae35;
h ^= h >> 16;
return h;
}
uint32_t util_murmur_hash3(const void *key, const int len, const uint32_t seed)
{
const uint8_t *data = (const uint8_t *)key;
const int nblocks = len / 4;
uint32_t h1 = seed;
const uint32_t c1 = 0xcc9e2d51;
const uint32_t c2 = 0x1b873593;
const uint32_t *blocks = (const uint32_t *)(data + nblocks * 4);
for (int i = -nblocks; i; i++) {
uint32_t k1 = mm_hash_getblock32(blocks, i);
k1 *= c1;
k1 = ROTL32(k1, 15);
k1 *= c2;
h1 ^= k1;
h1 = ROTL32(h1, 13);
h1 = h1 * 5 + 0xe6546b64;
}
const uint8_t *tail = (const uint8_t *)(data + nblocks * 4);
uint32_t k1 = 0;
switch (len & 3) {
case 3:
k1 ^= tail[2] << 16;
ATTR_FALLTHROUGH;
case 2:
k1 ^= tail[1] << 8;
ATTR_FALLTHROUGH;
case 1:
k1 ^= tail[0];
k1 *= c1;
k1 = ROTL32(k1, 15);
k1 *= c2;
h1 ^= k1;
ATTR_FALLTHROUGH;
default:
break;
}
h1 ^= len;
h1 = mm_hash_fmix32(h1);
return h1;
}
/* This is taken from the cryptomatte specification 1.0 */
float util_hash_to_float(const uint32_t hash)
{
const uint32_t mantissa = hash & ((1 << 23) - 1);
uint32_t exponent = (hash >> 23) & ((1 << 8) - 1);
exponent = max(exponent, (uint32_t)1);
exponent = min(exponent, (uint32_t)254);
exponent = exponent << 23;
uint32_t sign = (hash >> 31);
sign = sign << 31;
uint32_t float_bits = sign | exponent | mantissa;
float f;
memcpy(&f, &float_bits, sizeof(uint32_t));
return f;
}
CCL_NAMESPACE_END

View File

@@ -0,0 +1,14 @@
/* SPDX-FileCopyrightText: 2018-2022 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#pragma once
#include <cstdint>
CCL_NAMESPACE_BEGIN
uint32_t util_murmur_hash3(const void *key, const int len, const uint32_t seed);
float util_hash_to_float(const uint32_t hash);
CCL_NAMESPACE_END

View File

@@ -0,0 +1,311 @@
/* SPDX-FileCopyrightText: Contributors to the OpenVDB Project
* SPDX-FileCopyrightText: 2025 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#ifdef WITH_NANOVDB
# include "util/nanovdb.h"
# include "util/log.h"
# include "util/openvdb.h"
# include <openvdb/tools/Activate.h>
# include <nanovdb/util/ForEach.h>
# if NANOVDB_MAJOR_VERSION_NUMBER > 32 || \
(NANOVDB_MAJOR_VERSION_NUMBER == 32 && NANOVDB_MINOR_VERSION_NUMBER >= 7)
# include <nanovdb/tools/CreateNanoGrid.h>
# else
# include <nanovdb/util/OpenToNanoVDB.h>
# endif
CCL_NAMESPACE_BEGIN
/* Convert NanoVDB to OpenVDB mask grid.
*
* Implementation adapted from nanoToOpenVDB in nanovdb, this will create
* a MaskGrid from any type of grid using the active voxels. */
template<typename NanoBuildT> class NanoToOpenVDBMask {
using NanoNode0 = nanovdb::LeafNode<NanoBuildT, openvdb::Coord, openvdb::util::NodeMask>;
using NanoNode1 = nanovdb::InternalNode<NanoNode0>;
using NanoNode2 = nanovdb::InternalNode<NanoNode1>;
using NanoRootT = nanovdb::RootNode<NanoNode2>;
using NanoTreeT = nanovdb::Tree<NanoRootT>;
using NanoGridT = nanovdb::Grid<NanoTreeT>;
using NanoValueT = typename NanoGridT::ValueType;
using OpenBuildT = openvdb::ValueMask;
using OpenNode0 = openvdb::tree::LeafNode<OpenBuildT, NanoNode0::LOG2DIM>;
using OpenNode1 = openvdb::tree::InternalNode<OpenNode0, NanoNode1::LOG2DIM>;
using OpenNode2 = openvdb::tree::InternalNode<OpenNode1, NanoNode2::LOG2DIM>;
using OpenRootT = openvdb::tree::RootNode<OpenNode2>;
using OpenTreeT = openvdb::tree::Tree<OpenRootT>;
using OpenGridT = openvdb::Grid<OpenTreeT>;
using OpenValueT = typename OpenGridT::ValueType;
public:
NanoToOpenVDBMask() = default;
typename OpenGridT::Ptr operator()(const nanovdb::NanoGrid<NanoBuildT> &grid = 0);
private:
template<typename NanoNodeT, typename OpenNodeT>
OpenNodeT *processNode(const NanoNodeT * /*node*/);
OpenNode2 *process(const NanoNode2 *node)
{
return this->template processNode<NanoNode2, OpenNode2>(node);
}
OpenNode1 *process(const NanoNode1 *node)
{
return this->template processNode<NanoNode1, OpenNode1>(node);
}
template<typename NanoLeafT> OpenNode0 *process(const NanoLeafT *node);
};
template<typename NanoBuildT>
typename NanoToOpenVDBMask<NanoBuildT>::OpenGridT::Ptr NanoToOpenVDBMask<NanoBuildT>::operator()(
const nanovdb::NanoGrid<NanoBuildT> &grid)
{
/* Since the input nanovdb grid might use nanovdb types (Coord, Mask, Vec3)
* we cast to use openvdb types. */
const NanoGridT *srcGrid = reinterpret_cast<const NanoGridT *>(&grid);
auto dstGrid = openvdb::createGrid<OpenGridT>(OpenValueT());
/* Set transform. */
const nanovdb::Map &nanoMap = reinterpret_cast<const nanovdb::GridData *>(srcGrid)->mMap;
auto mat = openvdb::math::Mat4<double>::identity();
mat.setMat3(openvdb::math::Mat3<double>(nanoMap.mMatD));
mat = mat.transpose(); /* The 3x3 in nanovdb is transposed relative to openvdb's 3x3. */
mat.setTranslation(openvdb::math::Vec3<double>(nanoMap.mVecD));
dstGrid->setTransform(openvdb::math::Transform::createLinearTransform(mat));
/* Process root node. */
auto &root = dstGrid->tree().root();
auto *data = srcGrid->tree().root().data();
for (uint32_t i = 0; i < data->mTableSize; ++i) {
auto *tile = data->tile(i);
if (tile->isChild()) {
root.addChild(this->process(data->getChild(tile)));
}
else {
root.addTile(tile->origin(), OpenValueT(), tile->state);
}
}
return dstGrid;
}
template<typename T>
template<typename SrcNodeT, typename DstNodeT>
DstNodeT *NanoToOpenVDBMask<T>::processNode(const SrcNodeT *srcNode)
{
DstNodeT *dstNode = new DstNodeT(); /* Un-initialized for fast construction. */
dstNode->setOrigin(srcNode->origin());
const auto &childMask = srcNode->childMask();
const_cast<typename DstNodeT::NodeMaskType &>(dstNode->getValueMask()) = srcNode->valueMask();
const_cast<typename DstNodeT::NodeMaskType &>(dstNode->getChildMask()) = childMask;
auto *dstTable = const_cast<typename DstNodeT::UnionType *>(dstNode->getTable());
auto *srcData = srcNode->data();
std::vector<std::pair<uint32_t, const typename SrcNodeT::ChildNodeType *>> childNodes;
const auto childCount = childMask.countOn();
childNodes.reserve(childCount);
for (uint32_t n = 0; n < DstNodeT::NUM_VALUES; ++n) {
if (childMask.isOn(n)) {
childNodes.emplace_back(n, srcData->getChild(n));
}
}
auto kernel = [&](const auto &r) {
for (auto i = r.begin(); i != r.end(); ++i) {
auto &p = childNodes[i];
dstTable[p.first].setChild(this->process(p.second));
}
};
# if NANOVDB_MAJOR_VERSION_NUMBER > 32 || \
(NANOVDB_MAJOR_VERSION_NUMBER == 32 && NANOVDB_MINOR_VERSION_NUMBER >= 7)
nanovdb::util::forEach(0, childCount, 1, kernel);
# else
nanovdb::forEach(0, childCount, 1, kernel);
# endif
return dstNode;
}
template<typename T>
template<typename NanoLeafT>
inline typename NanoToOpenVDBMask<T>::OpenNode0 *NanoToOpenVDBMask<T>::process(
const NanoLeafT *srcNode)
{
static_assert(std::is_same_v<NanoLeafT, NanoNode0>, "NanoToOpenVDBMask wrong leaf type");
OpenNode0 *dstNode = new OpenNode0(); /* Un-initialized for fast construction. */
dstNode->setOrigin(srcNode->origin());
dstNode->setValueMask(srcNode->valueMask());
return dstNode;
}
struct NanoToOpenVDBMaskOp {
openvdb::MaskGrid::Ptr mask_grid;
template<typename NanoBuildT> bool operator()(const nanovdb::NanoGrid<NanoBuildT> &grid)
{
NanoToOpenVDBMask<NanoBuildT> tmp;
mask_grid = tmp(grid);
return true;
}
};
template<typename OpType>
bool nanovdb_grid_type_operation(const nanovdb::GridHandle<> &handle, OpType &&op)
{
const int n = 0;
if (const auto *grid = handle.template grid<float>(n)) {
return op(*grid);
}
if (const auto *grid = handle.template grid<nanovdb::Fp16>(n)) {
return op(*grid);
}
if (const auto *grid = handle.template grid<nanovdb::FpN>(n)) {
return op(*grid);
}
if (const auto *grid = handle.template grid<nanovdb::Vec3f>(n)) {
return op(*grid);
}
if (const auto *grid = handle.template grid<nanovdb::Vec4f>(n)) {
return op(*grid);
}
assert(!"Unknown NanoVDB grid type");
return true;
}
openvdb::MaskGrid::Ptr nanovdb_to_openvdb_mask(const nanovdb::GridHandle<> &handle)
{
NanoToOpenVDBMaskOp op;
nanovdb_grid_type_operation(handle, op);
return op.mask_grid;
}
/* Convert OpenVDB to NanoVDB grid. */
struct ToNanoOp {
nanovdb::GridHandle<> nanogrid;
int precision = 16;
float clipping = 0.0f;
template<typename GridType, typename FloatDataType, const int channels>
bool operator()(const typename GridType::ConstPtr &grid)
{
if constexpr (std::is_same_v<GridType, openvdb::MaskGrid>) {
return false;
}
try {
# if NANOVDB_MAJOR_VERSION_NUMBER > 32 || \
(NANOVDB_MAJOR_VERSION_NUMBER == 32 && NANOVDB_MINOR_VERSION_NUMBER >= 6)
# if NANOVDB_MAJOR_VERSION_NUMBER > 32 || \
(NANOVDB_MAJOR_VERSION_NUMBER == 32 && NANOVDB_MINOR_VERSION_NUMBER >= 7)
/* OpenVDB 12. */
using nanovdb::tools::createNanoGrid;
using nanovdb::tools::StatsMode;
# else
/* OpenVDB 11. */
using nanovdb::createNanoGrid;
using nanovdb::StatsMode;
# endif
if constexpr (std::is_same_v<GridType, openvdb::FloatGrid>) {
typename GridType::ConstPtr floatgrid = apply_clipping<GridType>(grid);
if (precision == 0) {
nanogrid = createNanoGrid<openvdb::FloatGrid, nanovdb::FpN>(*floatgrid,
StatsMode::Disable);
}
else if (precision == 16) {
nanogrid = createNanoGrid<openvdb::FloatGrid, nanovdb::Fp16>(*floatgrid,
StatsMode::Disable);
}
else {
nanogrid = createNanoGrid<openvdb::FloatGrid, float>(*floatgrid, StatsMode::Disable);
}
}
else if constexpr (std::is_same_v<GridType, openvdb::Vec3fGrid>) {
/* Enable stats for velocity grid. Weak, but there seems to be no simple iterator over all
* values in the grid? */
typename GridType::ConstPtr floatgrid = apply_clipping<GridType>(grid);
nanogrid = createNanoGrid<openvdb::Vec3fGrid, nanovdb::Vec3f>(*floatgrid,
StatsMode::MinMax);
}
else if constexpr (std::is_same_v<GridType, openvdb::Vec4fGrid>) {
typename GridType::ConstPtr floatgrid = apply_clipping<GridType>(grid);
nanogrid = createNanoGrid<openvdb::Vec4fGrid, nanovdb::Vec4f>(*floatgrid,
StatsMode::Disable);
}
# else
/* OpenVDB 10. */
if constexpr (std::is_same_v<GridType, openvdb::FloatGrid>) {
typename GridType::ConstPtr floatgrid = apply_clipping<GridType>(grid);
if (precision == 0) {
nanogrid = nanovdb::openToNanoVDB<nanovdb::HostBuffer, openvdb::FloatTree, nanovdb::FpN>(
*floatgrid);
}
else if (precision == 16) {
nanogrid =
nanovdb::openToNanoVDB<nanovdb::HostBuffer, openvdb::FloatTree, nanovdb::Fp16>(
*floatgrid);
}
else {
nanogrid = nanovdb::openToNanoVDB(*floatgrid);
}
}
else if constexpr (std::is_same_v<FloatGridType, openvdb::Vec3fGrid>) {
typename GridType::ConstPtr floatgrid = apply_clipping<GridType>(grid);
nanogrid = nanovdb::openToNanoVDB(*floatgrid);
}
else if constexpr (std::is_same_v<FloatGridType, openvdb::Vec4fGrid>) {
typename GridType::ConstPtr floatgrid = apply_clipping<GridType>(grid);
nanogrid = nanovdb::openToNanoVDB(*floatgrid);
}
# endif
}
catch (const std::exception &e) {
LOG_ERROR << "Error converting OpenVDB to NanoVDB grid: " << e.what();
}
catch (...) {
LOG_ERROR << "Error converting OpenVDB to NanoVDB grid: Unknown error";
}
return true;
}
template<typename GridT>
typename GridT::ConstPtr apply_clipping(const typename GridT::ConstPtr &grid) const
{
if (clipping == 0.0f) {
return grid;
}
/* TODO: Ideally would apply clipping during createNanoGrid, this seems slow. */
typename GridT::Ptr newgrid = grid->deepCopy();
openvdb::tools::deactivate(
*newgrid, typename GridT::ValueType(0.0f), typename GridT::ValueType(clipping));
return newgrid;
}
};
nanovdb::GridHandle<> openvdb_to_nanovdb(const openvdb::GridBase::ConstPtr &grid,
const int precision,
const float clipping)
{
ToNanoOp op;
op.precision = precision;
op.clipping = clipping;
openvdb_grid_type_operation(grid, op);
return std::move(op.nanogrid);
}
CCL_NAMESPACE_END
#endif

View File

@@ -0,0 +1,36 @@
/* SPDX-FileCopyrightText: Contributors to the OpenVDB Project
* SPDX-FileCopyrightText: 2025 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#pragma once
#ifdef WITH_NANOVDB
# include <openvdb/openvdb.h>
# define NANOVDB_USE_OPENVDB
# define NANOVDB_USE_TBB
# include <nanovdb/NanoVDB.h> // manages and streams the raw memory buffer of a NanoVDB grid.
# if NANOVDB_MAJOR_VERSION_NUMBER > 32 || \
(NANOVDB_MAJOR_VERSION_NUMBER == 32 && NANOVDB_MINOR_VERSION_NUMBER >= 7)
# include <nanovdb/GridHandle.h>
# else
# include <nanovdb/util/GridHandle.h>
# endif
CCL_NAMESPACE_BEGIN
/* Convert NanoVDB to OpenVDB mask grid that represents just the topology. */
openvdb::MaskGrid::Ptr nanovdb_to_openvdb_mask(const nanovdb::GridHandle<> &handle);
/* Convert OpenVDB to NanoVDB grid. */
nanovdb::GridHandle<> openvdb_to_nanovdb(const openvdb::GridBase::ConstPtr &grid,
const int precision,
const float clipping);
CCL_NAMESPACE_END
#endif

View File

@@ -0,0 +1,32 @@
/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#pragma once
#ifdef WITH_OPENIMAGEDENOISE
# include <OpenImageDenoise/oidn.hpp> // IWYU pragma: export
#endif
#include "util/system.h" // IWYU pragma: keep
CCL_NAMESPACE_BEGIN
static inline bool openimagedenoise_supported()
{
#ifdef WITH_OPENIMAGEDENOISE
# if defined(__APPLE__)
/* Always supported through Accelerate framework BNNS. */
return true;
# elif defined(__aarch64__) || defined(_M_ARM64)
/* OIDN 2.2 and up supports ARM64 on Windows and Linux. */
return true;
# else
return system_cpu_support_sse42();
# endif
#else
return false;
#endif
}
CCL_NAMESPACE_END

View File

@@ -0,0 +1,80 @@
/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#ifdef WITH_OPENVDB
# include "util/openvdb.h"
# include <openvdb/tools/Activate.h>
# include <openvdb/tools/Dense.h>
CCL_NAMESPACE_BEGIN
/* Convert to the few data types we native can render. */
openvdb::GridBase::ConstPtr openvdb_convert_to_known_type(const openvdb::GridBase::ConstPtr &grid)
{
if (grid->isType<openvdb::FloatGrid>()) {
return grid;
}
if (grid->isType<openvdb::Vec3fGrid>()) {
return grid;
}
if (grid->isType<openvdb::Vec4fGrid>()) {
return grid;
}
if (grid->isType<openvdb::BoolGrid>()) {
const openvdb::FloatGrid floatgrid(*openvdb::gridConstPtrCast<openvdb::BoolGrid>(grid));
return std::make_shared<openvdb::FloatGrid>(std::move(floatgrid));
}
if (grid->isType<openvdb::DoubleGrid>()) {
const openvdb::FloatGrid floatgrid(*openvdb::gridConstPtrCast<openvdb::DoubleGrid>(grid));
return std::make_shared<openvdb::FloatGrid>(std::move(floatgrid));
}
if (grid->isType<openvdb::Int32Grid>()) {
const openvdb::FloatGrid floatgrid(*openvdb::gridConstPtrCast<openvdb::Int32Grid>(grid));
return std::make_shared<openvdb::FloatGrid>(std::move(floatgrid));
}
if (grid->isType<openvdb::Int64Grid>()) {
const openvdb::FloatGrid floatgrid(*openvdb::gridConstPtrCast<openvdb::Int64Grid>(grid));
return std::make_shared<openvdb::FloatGrid>(std::move(floatgrid));
}
if (grid->isType<openvdb::Vec3IGrid>()) {
const openvdb::Vec3fGrid floatgrid(*openvdb::gridConstPtrCast<openvdb::Vec3IGrid>(grid));
return std::make_shared<openvdb::Vec3fGrid>(std::move(floatgrid));
}
if (grid->isType<openvdb::Vec3dGrid>()) {
const openvdb::Vec3fGrid floatgrid(*openvdb::gridConstPtrCast<openvdb::Vec3dGrid>(grid));
return std::make_shared<openvdb::Vec3fGrid>(std::move(floatgrid));
}
return nullptr;
}
/* Counter number of channels. */
struct NumChannelsOp {
int num_channels = 0;
template<typename GridType, typename FloatDataType, const int channels>
bool operator()(const typename GridType::ConstPtr & /*unused*/)
{
num_channels = channels;
return true;
}
};
int openvdb_num_channels(const openvdb::GridBase::ConstPtr &grid)
{
NumChannelsOp op;
if (!openvdb_grid_type_operation(grid, op)) {
return 0;
}
return op.num_channels;
}
/* Convert OpenVDB to NanoVDB. */
# ifdef WITH_NANOVDB
# endif
CCL_NAMESPACE_END
#endif

View File

@@ -0,0 +1,47 @@
/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#pragma once
#ifdef WITH_OPENVDB
# include <openvdb/openvdb.h>
namespace openvdb {
using Vec4fTree = tree::Tree4<Vec4f, 5, 4, 3>::Type;
using Vec4fGrid = Grid<Vec4fTree>;
} // namespace openvdb
CCL_NAMESPACE_BEGIN
/* Convert to the few grid types we can render natively. */
openvdb::GridBase::ConstPtr openvdb_convert_to_known_type(const openvdb::GridBase::ConstPtr &grid);
/* Apply operation to known grid types. */
template<typename OpType>
bool openvdb_grid_type_operation(const openvdb::GridBase::ConstPtr &grid, OpType &&op)
{
if (grid->isType<openvdb::FloatGrid>()) {
return op.template operator()<openvdb::FloatGrid, float, 1>(
openvdb::gridConstPtrCast<openvdb::FloatGrid>(grid));
}
if (grid->isType<openvdb::Vec3fGrid>()) {
return op.template operator()<openvdb::Vec3fGrid, openvdb::Vec3f, 3>(
openvdb::gridConstPtrCast<openvdb::Vec3fGrid>(grid));
}
if (grid->isType<openvdb::Vec4fGrid>()) {
return op.template operator()<openvdb::Vec4fGrid, openvdb::Vec4f, 4>(
openvdb::gridConstPtrCast<openvdb::Vec4fGrid>(grid));
}
assert(0);
return false;
}
/* Count number of channels in known types. */
int openvdb_num_channels(const openvdb::GridBase::ConstPtr &grid);
CCL_NAMESPACE_END
#endif

View File

@@ -0,0 +1,47 @@
/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#pragma once
#ifndef __KERNEL_GPU__
/* x86
*
* Compile a regular kernel. */
# if defined(i386) || defined(_M_IX86)
/* x86-64
*
* Compile a regular (includes SSE4.2) and AVX2 kernel. */
# elif defined(__x86_64__) || defined(_M_X64)
/* SSE4.2 is our minimum requirement for x86-64 CPUs, so auto enable */
# define __KERNEL_SSE__
# define __KERNEL_SSE2__
# define __KERNEL_SSE3__
# define __KERNEL_SSE42__
/* no SSE4.2 kernel on x86-64, part of regular kernel */
# ifdef WITH_KERNEL_AVX2
# define WITH_CYCLES_OPTIMIZED_KERNEL_AVX2
# endif
/* Arm Neon
*
* Compile a SSE4 kernel emulated with Neon. Most code is shared with
* SSE, some specializations for performance and compatibility are made
* made testing for __KERNEL_NEON__. */
# elif (defined(__ARM_NEON) || defined(_M_ARM64)) && defined(WITH_SSE2NEON)
# define __KERNEL_NEON__
# define __KERNEL_SSE__
# define __KERNEL_SSE2__
# define __KERNEL_SSE3__
# define __KERNEL_SSE42__
# endif
#endif

View File

@@ -0,0 +1,40 @@
/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#pragma once
/* Parameter value lists from OpenImageIO are used to store custom properties
* on various data, which can then later be used in shaders. */
#include <OpenImageIO/paramlist.h>
#include <OpenImageIO/typedesc.h>
#include <OpenImageIO/ustring.h>
CCL_NAMESPACE_BEGIN
using OIIO::ParamValue;
using OIIO::TypeColor;
using OIIO::TypeDesc;
using OIIO::TypeFloat;
using OIIO::TypeFloat2;
using OIIO::TypeFloat4;
using OIIO::TypeInt;
using OIIO::TypeMatrix;
using OIIO::TypeNormal;
using OIIO::TypePoint;
using OIIO::TypeString;
using OIIO::TypeUnknown;
using OIIO::TypeVector;
static constexpr TypeDesc TypeRGBA(TypeDesc::FLOAT, TypeDesc::VEC4, TypeDesc::COLOR);
static constexpr TypeDesc TypeFloatArray4(TypeDesc::FLOAT,
TypeDesc::SCALAR,
TypeDesc::NOSEMANTICS,
4);
using OIIO::ustring;
using OIIO::ustringhash;
CCL_NAMESPACE_END

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,72 @@
/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#pragma once
/* Utility functions to get paths to files distributed with the program. For
* the standalone apps, paths are relative to the executable, for dynamically
* linked libraries, the path to the library may be set with path_init, which
* then makes all paths relative to that. */
#include <cstdio>
#include "util/string.h"
#include "util/vector.h"
CCL_NAMESPACE_BEGIN
/* program paths */
void path_init(const string &path = "", const string &user_path = "");
string path_get(const string &sub = "");
string path_user_get(const string &sub = "");
string path_cache_get(const string &sub = "");
/* path string manipulation */
string path_filename(const string &path);
string path_dirname(const string &path);
string path_join(const string &dir, const string &file);
string path_escape(const string &path);
string path_normalize(const string &path);
bool path_is_relative(const string &path);
string path_make_relative(const string &path, const string &base);
/* file info */
size_t path_file_size(const string &path);
bool path_exists(const string &path);
bool path_is_directory(const string &path);
bool path_is_file(const string &path);
string path_files_md5_hash(const string &dir);
uint64_t path_modified_time(const string &path);
/* directory utility */
bool path_create_directories(const string &path);
/* file read/write utilities */
FILE *path_fopen(const string &path, const string &mode);
bool path_write_binary(const string &path, const vector<uint8_t> &binary);
bool path_write_text(const string &path, const string &text);
bool path_read_binary(const string &path, vector<uint8_t> &binary);
bool path_read_text(const string &path, string &text);
bool path_read_compressed_binary(const string &path, vector<uint8_t> &binary);
bool path_read_compressed_text(const string &path, string &text);
/* File manipulation. */
bool path_remove(const string &path);
/* source code utility */
string path_source_replace_includes(const string &source, const string &path);
/* Simple least-recently-used cache for kernels.
*
* Kernels of same type are cached in the same directory.
* Whenever a kernel is used, its last modified time is updated.
* When a new kernel is added to the cache, clear old entries of the same type (i.e. in the same
* directory). */
bool path_cache_kernel_exists_and_mark_used(const string &path);
void path_cache_kernel_mark_added_and_clear_old(const string &path,
const size_t max_old_kernel_of_same_type = 5);
CCL_NAMESPACE_END

View File

@@ -0,0 +1,182 @@
/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#include <algorithm>
#include <cassert>
#include <chrono>
#include <thread>
#include "util/profiling.h"
CCL_NAMESPACE_BEGIN
Profiler::Profiler() : do_stop_worker(true) {}
Profiler::~Profiler()
{
assert(worker == nullptr);
}
void Profiler::run()
{
uint64_t updates = 0;
auto start_time = std::chrono::system_clock::now();
while (!do_stop_worker) {
thread_scoped_lock lock(mutex);
for (ProfilingState *state : states) {
const uint32_t cur_event = state->event;
const int32_t cur_shader = state->shader;
const int32_t cur_object = state->object;
/* The state reads/writes should be atomic, but just to be sure
* check the values for validity anyway. */
if (cur_event < PROFILING_NUM_EVENTS) {
event_samples[cur_event]++;
}
if (cur_shader >= 0 && cur_shader < shader_samples.size()) {
shader_samples[cur_shader]++;
}
if (cur_object >= 0 && cur_object < object_samples.size()) {
object_samples[cur_object]++;
}
}
lock.unlock();
/* Relative waits always overshoot a bit, so just waiting 1ms every
* time would cause the sampling to drift over time.
* By keeping track of the absolute time, the wait times correct themselves -
* if one wait overshoots a lot, the next one will be shorter to compensate. */
updates++;
std::this_thread::sleep_until(start_time + updates * std::chrono::milliseconds(1));
}
}
void Profiler::reset(const int num_shaders, const int num_objects)
{
const bool running = (worker != nullptr);
if (running) {
stop();
}
/* Resize and clear the accumulation vectors. */
shader_hits.assign(num_shaders, 0);
object_hits.assign(num_objects, 0);
event_samples.assign(PROFILING_NUM_EVENTS, 0);
shader_samples.assign(num_shaders, 0);
object_samples.assign(num_objects, 0);
if (running) {
start();
}
}
void Profiler::start()
{
assert(worker == nullptr);
do_stop_worker = false;
worker = make_unique<thread>([this] { run(); });
}
void Profiler::stop()
{
if (worker != nullptr) {
do_stop_worker = true;
worker->join();
worker.reset();
}
}
void Profiler::add_state(ProfilingState *state)
{
const thread_scoped_lock lock(mutex);
/* Add the ProfilingState from the list of sampled states. */
assert(std::find(states.begin(), states.end(), state) == states.end());
states.push_back(state);
/* Resize thread-local hit counters. */
state->shader_hits.assign(shader_hits.size(), 0);
state->object_hits.assign(object_hits.size(), 0);
/* Initialize the state. */
state->event = PROFILING_UNKNOWN;
state->shader = -1;
state->object = -1;
state->active = true;
}
void Profiler::remove_state(ProfilingState *state)
{
const thread_scoped_lock lock(mutex);
/* Remove the ProfilingState from the list of sampled states. */
states.erase(std::remove(states.begin(), states.end(), state), states.end());
state->active = false;
/* Merge thread-local hit counters. */
assert(shader_hits.size() == state->shader_hits.size());
for (int i = 0; i < shader_hits.size(); i++) {
shader_hits[i] += state->shader_hits[i];
}
assert(object_hits.size() == state->object_hits.size());
for (int i = 0; i < object_hits.size(); i++) {
object_hits[i] += state->object_hits[i];
}
}
uint64_t Profiler::get_event(ProfilingEvent event)
{
assert(worker == nullptr);
if (event_samples.empty()) {
return 0;
}
assert(event_samples.size() > event);
return event_samples[event];
}
bool Profiler::get_shader(const int shader, uint64_t &samples, uint64_t &hits)
{
assert(worker == nullptr);
if (shader_samples.empty()) {
samples = 0;
hits = 0;
return false;
}
assert(shader_samples.size() > shader);
if (shader_samples[shader] == 0) {
return false;
}
samples = shader_samples[shader];
hits = shader_hits[shader];
return true;
}
bool Profiler::get_object(const int object, uint64_t &samples, uint64_t &hits)
{
assert(worker == nullptr);
if (object_samples.empty()) {
samples = 0;
hits = 0;
return false;
}
assert(object_samples.size() > object);
if (object_samples[object] == 0) {
return false;
}
samples = object_samples[object];
hits = object_hits[object];
return true;
}
bool Profiler::active() const
{
return (worker != nullptr);
}
CCL_NAMESPACE_END

View File

@@ -0,0 +1,169 @@
/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#pragma once
#include <cassert>
#include "util/thread.h"
#include "util/unique_ptr.h"
#include "util/vector.h"
CCL_NAMESPACE_BEGIN
enum ProfilingEvent : uint32_t {
PROFILING_UNKNOWN,
PROFILING_RAY_SETUP,
PROFILING_INTERSECT_CLOSEST,
PROFILING_INTERSECT_SUBSURFACE,
PROFILING_INTERSECT_SHADOW,
PROFILING_INTERSECT_VOLUME_STACK,
PROFILING_INTERSECT_DEDICATED_LIGHT,
PROFILING_SHADE_SURFACE_SETUP,
PROFILING_SHADE_SURFACE_EVAL,
PROFILING_SHADE_SURFACE_DIRECT_LIGHT,
PROFILING_SHADE_SURFACE_INDIRECT_LIGHT,
PROFILING_SHADE_SURFACE_AO,
PROFILING_SHADE_SURFACE_PASSES,
PROFILING_SHADE_DEDICATED_LIGHT,
PROFILING_SHADE_VOLUME_SETUP,
PROFILING_SHADE_VOLUME_INTEGRATE,
PROFILING_SHADE_VOLUME_DIRECT_LIGHT,
PROFILING_SHADE_VOLUME_INDIRECT_LIGHT,
PROFILING_SHADE_SHADOW_SETUP,
PROFILING_SHADE_SHADOW_SURFACE,
PROFILING_SHADE_SHADOW_VOLUME,
PROFILING_SHADE_LIGHT_SETUP,
PROFILING_SHADE_LIGHT_EVAL,
PROFILING_NUM_EVENTS,
};
/* Contains the current execution state of a worker thread.
* These values are constantly updated by the worker.
* Periodically the profiler thread will wake up, read them
* and update its internal counters based on it.
*
* Atomics aren't needed here since we're only doing direct
* writes and reads to (4-byte-aligned) uint32_t, which is
* guaranteed to be atomic on x86 since the 486.
* Memory ordering is not guaranteed but does not matter.
*
* And even on other architectures, the extremely rare corner
* case of reading an intermediate state could at worst result
* in a single incorrect sample. */
struct ProfilingState {
volatile uint32_t event = PROFILING_UNKNOWN;
volatile int32_t shader = -1;
volatile int32_t object = -1;
volatile bool active = false;
vector<uint64_t> shader_hits;
vector<uint64_t> object_hits;
};
class Profiler {
public:
Profiler();
~Profiler();
void reset(const int num_shaders, const int num_objects);
void start();
void stop();
void add_state(ProfilingState *state);
void remove_state(ProfilingState *state);
uint64_t get_event(ProfilingEvent event);
bool get_shader(const int shader, uint64_t &samples, uint64_t &hits);
bool get_object(const int object, uint64_t &samples, uint64_t &hits);
bool active() const;
protected:
void run();
/* Tracks how often the worker was in each ProfilingEvent while sampling,
* so multiplying the values by the sample frequency (currently 1ms)
* gives the approximate time spent in each state. */
vector<uint64_t> event_samples;
vector<uint64_t> shader_samples;
vector<uint64_t> object_samples;
/* Tracks the total amounts every object/shader was hit.
* Used to evaluate relative cost, written by the render thread.
* Indexed by the shader and object IDs that the kernel also uses
* to index __object_flag and __shaders. */
vector<uint64_t> shader_hits;
vector<uint64_t> object_hits;
volatile bool do_stop_worker;
unique_ptr<thread> worker;
thread_mutex mutex;
vector<ProfilingState *> states;
};
class ProfilingHelper {
public:
ProfilingHelper(ProfilingState *state, ProfilingEvent event) : state(state)
{
previous_event = state->event;
state->event = event;
}
~ProfilingHelper()
{
state->event = previous_event;
}
void set_event(ProfilingEvent event)
{
state->event = event;
}
protected:
ProfilingState *state;
uint32_t previous_event;
};
class ProfilingWithShaderHelper : public ProfilingHelper {
public:
ProfilingWithShaderHelper(ProfilingState *state, ProfilingEvent event)
: ProfilingHelper(state, event)
{
}
~ProfilingWithShaderHelper()
{
state->object = -1;
state->shader = -1;
}
void set_shader(const int object, const int shader)
{
if (state->active) {
state->shader = shader;
state->object = object;
if (shader >= 0) {
assert(shader < int(state->shader_hits.size()));
state->shader_hits[shader]++;
}
if (object >= 0) {
assert(object < int(state->object_hits.size()));
state->object_hits[object]++;
}
}
}
};
CCL_NAMESPACE_END

View File

@@ -0,0 +1,355 @@
/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#pragma once
#include <functional>
/* Progress
*
* Simple class to communicate progress status messages, timing information,
* update notifications from a job running in another thread. All methods
* except for the constructor/destructor are thread safe. */
#include "util/string.h"
#include "util/thread.h"
#include "util/time.h"
CCL_NAMESPACE_BEGIN
class Progress {
public:
Progress()
{
start_time = time_dt();
render_start_time = time_dt();
status = "Initializing";
}
Progress(Progress &progress)
{
*this = progress;
}
Progress &operator=(Progress &progress)
{
const thread_scoped_lock lock(progress.progress_mutex);
progress.get_status(status, substatus);
pixel_samples = progress.pixel_samples;
total_pixel_samples = progress.total_pixel_samples;
current_tile_sample = progress.get_current_sample();
return *this;
}
void reset()
{
pixel_samples = 0;
total_pixel_samples = 0;
current_tile_sample = 0;
rendered_tiles = 0;
denoised_tiles = 0;
start_time = time_dt();
render_start_time = time_dt();
time_limit = 0.0;
end_time = 0.0;
status = "Initializing";
substatus = "";
sync_status = "";
sync_substatus = "";
cancel = false;
cancel_message = "";
error = false;
error_message = "";
}
/* cancel */
void set_cancel(const string &cancel_message_)
{
const thread_scoped_lock lock(progress_mutex);
cancel_message = cancel_message_;
cancel = true;
}
bool get_cancel() const
{
if (!cancel && cancel_cb) {
cancel_cb();
}
return cancel;
}
string get_cancel_message() const
{
const thread_scoped_lock lock(progress_mutex);
return cancel_message;
}
void set_cancel_callback(std::function<void()> function)
{
cancel_cb = function;
}
/* error */
void set_error(const string &error_message_)
{
const thread_scoped_lock lock(progress_mutex);
error_message = error_message_;
error = true;
/* If error happens we also stop rendering. */
cancel_message = error_message_;
cancel = true;
}
bool get_error() const
{
return error;
}
string get_error_message() const
{
const thread_scoped_lock lock(progress_mutex);
return error_message;
}
/* tile and timing information */
void set_start_time()
{
const thread_scoped_lock lock(progress_mutex);
start_time = time_dt();
end_time = 0.0;
}
void set_render_start_time()
{
const thread_scoped_lock lock(progress_mutex);
render_start_time = time_dt();
}
void set_time_limit(const double time_limit_)
{
const thread_scoped_lock lock(progress_mutex);
time_limit = time_limit_;
}
void add_skip_time(const scoped_timer &start_timer, bool only_render)
{
const double skip_time = time_dt() - start_timer.get_start();
render_start_time += skip_time;
if (!only_render) {
start_time += skip_time;
}
}
void get_time(double &total_time_, double &render_time_) const
{
const thread_scoped_lock lock(progress_mutex);
const double time = (end_time > 0) ? end_time : time_dt();
total_time_ = time - start_time;
render_time_ = time - render_start_time;
}
void set_end_time()
{
end_time = time_dt();
}
void reset_sample()
{
const thread_scoped_lock lock(progress_mutex);
pixel_samples = 0;
current_tile_sample = 0;
rendered_tiles = 0;
denoised_tiles = 0;
}
void set_total_pixel_samples(const uint64_t total_pixel_samples_)
{
const thread_scoped_lock lock(progress_mutex);
total_pixel_samples = total_pixel_samples_;
}
double get_progress() const
{
const thread_scoped_lock lock(progress_mutex);
if (pixel_samples > 0) {
double progress_percent = (double)pixel_samples / (double)total_pixel_samples;
if (time_limit != 0.0) {
const double time_since_render_start = time_dt() - render_start_time;
progress_percent = fmax(progress_percent, time_since_render_start / time_limit);
}
return fmin(1.0, progress_percent);
}
return 0.0;
}
void add_samples(const uint64_t pixel_samples_, int tile_sample)
{
const thread_scoped_lock lock(progress_mutex);
pixel_samples += pixel_samples_;
current_tile_sample = tile_sample;
}
void add_samples_update(const uint64_t pixel_samples_, int tile_sample)
{
add_samples(pixel_samples_, tile_sample);
set_update();
}
void add_finished_tile(bool denoised)
{
const thread_scoped_lock lock(progress_mutex);
if (denoised) {
denoised_tiles++;
}
else {
rendered_tiles++;
}
}
int get_current_sample() const
{
const thread_scoped_lock lock(progress_mutex);
/* Note that the value here always belongs to the last tile that updated,
* so it's only useful if there is only one active tile. */
return current_tile_sample;
}
int get_rendered_tiles() const
{
const thread_scoped_lock lock(progress_mutex);
return rendered_tiles;
}
int get_denoised_tiles() const
{
const thread_scoped_lock lock(progress_mutex);
return denoised_tiles;
}
/* status messages */
void set_status(const string &status_, const string &substatus_ = "")
{
{
const thread_scoped_lock lock(progress_mutex);
status = status_;
substatus = substatus_;
}
set_update();
}
void set_substatus(const string &substatus_)
{
{
const thread_scoped_lock lock(progress_mutex);
substatus = substatus_;
}
set_update();
}
void set_sync_status(const string &status_, const string &substatus_ = "")
{
{
const thread_scoped_lock lock(progress_mutex);
sync_status = status_;
sync_substatus = substatus_;
}
set_update();
}
void set_sync_substatus(const string &substatus_)
{
{
const thread_scoped_lock lock(progress_mutex);
sync_substatus = substatus_;
}
set_update();
}
void get_status(string &status_, string &substatus_) const
{
const thread_scoped_lock lock(progress_mutex);
if (!sync_status.empty()) {
status_ = sync_status;
substatus_ = sync_substatus;
}
else {
status_ = status;
substatus_ = substatus;
}
}
/* callback */
void set_update()
{
if (update_cb) {
const thread_scoped_lock lock(update_mutex);
update_cb();
}
}
void set_update_callback(std::function<void()> function)
{
update_cb = function;
}
protected:
mutable thread_mutex progress_mutex;
mutable thread_mutex update_mutex;
std::function<void()> update_cb = nullptr;
std::function<void()> cancel_cb = nullptr;
/* pixel_samples counts how many samples have been rendered over all pixel, not just per pixel.
* This makes the progress estimate more accurate when tiles with different sizes are used.
*
* total_pixel_samples is the total amount of pixel samples that will be rendered. */
uint64_t pixel_samples = 0, total_pixel_samples = 0;
/* Stores the current sample count of the last tile that called the update function.
* It's used to display the sample count if only one tile is active. */
int current_tile_sample = 0;
/* Stores the number of tiles that's already finished.
* Used to determine whether all but the last tile are finished rendering,
* in which case the current_tile_sample is displayed. */
int rendered_tiles = 0, denoised_tiles = 0;
double start_time = 0.0, render_start_time = 0.0, time_limit = 0.0;
/* End time written when render is done, so it doesn't keep increasing on redraws. */
double end_time = 0.0;
string status;
string substatus;
string sync_status;
string sync_substatus;
volatile bool cancel = false;
string cancel_message;
volatile bool error = false;
string error_message;
};
CCL_NAMESPACE_END

View File

@@ -0,0 +1,284 @@
/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#pragma once
#include "util/transform.h"
CCL_NAMESPACE_BEGIN
ccl_device float2 direction_to_spherical(const float3 dir)
{
const float theta = safe_acosf(dir.z);
const float phi = atan2f(dir.y, dir.x);
return make_float2(theta, phi);
}
ccl_device float3 spherical_to_direction(const float theta, const float phi)
{
return make_float3(sinf(theta) * cosf(phi), sinf(theta) * sinf(phi), cosf(theta));
}
ccl_device float3 spherical_cos_to_direction(const float cos_theta, const float phi)
{
const float sin_theta = sin_from_cos(cos_theta);
return make_float3(sin_theta * cosf(phi), sin_theta * sinf(phi), cos_theta);
}
ccl_device_inline float2 polar_to_cartesian(const float r, const float phi)
{
return make_float2(r * cosf(phi), r * sinf(phi));
}
ccl_device_inline float3 disk_to_hemisphere(const float2 p)
{
return make_float3(p.x, p.y, safe_sqrtf(1.0f - len_squared(p)));
}
/* 4x4 projection matrix, perspective or orthographic. */
struct ProjectionTransform {
float4 x, y, z, w; /* rows */
#ifndef __KERNEL_GPU__
ProjectionTransform() = default;
explicit ProjectionTransform(const Transform &tfm)
: x(tfm.x), y(tfm.y), z(tfm.z), w(make_float4(0.0f, 0.0f, 0.0f, 1.0f))
{
}
#endif
};
struct PerspectiveMotionTransform {
ProjectionTransform pre;
ProjectionTransform post;
};
CCL_NAMESPACE_END
#if !defined(__KERNEL_METAL__)
# include "util/projection_inverse.h"
#endif
CCL_NAMESPACE_BEGIN
/* Functions */
ccl_device_inline float3 transform_perspective(const ccl_private ProjectionTransform *t,
const float3 a)
{
const float4 b = make_float4(a.x, a.y, a.z, 1.0f);
const float3 c = make_float3(dot(t->x, b), dot(t->y, b), dot(t->z, b));
const float w = dot(t->w, b);
return (w != 0.0f) ? c / w : zero_float3();
}
ccl_device_inline float3 transform_perspective_direction(const ccl_private ProjectionTransform *t,
const float3 a)
{
const float3 c = make_float3(a.x * t->x.x + a.y * t->x.y + a.z * t->x.z,
a.x * t->y.x + a.y * t->y.y + a.z * t->y.z,
a.x * t->z.x + a.y * t->z.y + a.z * t->z.z);
return c;
}
/* Applies transform t to point a with given derivatives `da/dx` and `da/dy`.
* Returns t(a) and sets `out_dx/dy` to the values of `dt(a)/dx` and `dt(a)/dy`, respectively. */
ccl_device_inline float3 transform_perspective_deriv(const ccl_private ProjectionTransform *t,
const float3 a,
const float3 dx,
const float3 dy,
ccl_private float3 &out_dx,
ccl_private float3 &out_dy)
{
const float4 b = make_float4(a.x, a.y, a.z, 1.0f);
const float3 c = make_float3(dot(t->x, b), dot(t->y, b), dot(t->z, b));
const float w = dot(t->w, b);
if (w != 0.0f) {
out_dx = (transform_perspective_direction(t, dx) - dot(make_float3(t->w), dx) * c) / w;
out_dy = (transform_perspective_direction(t, dy) - dot(make_float3(t->w), dy) * c) / w;
return c / w;
}
else {
out_dx = zero_float3();
out_dy = zero_float3();
return zero_float3();
}
}
ccl_device_inline ProjectionTransform make_projection(const float a,
const float b,
const float c,
const float d,
const float e,
const float f,
const float g,
const float h,
const float i,
const float j,
const float k,
const float l,
const float m,
const float n,
const float o,
const float p)
{
ProjectionTransform t;
t.x.x = a;
t.x.y = b;
t.x.z = c;
t.x.w = d;
t.y.x = e;
t.y.y = f;
t.y.z = g;
t.y.w = h;
t.z.x = i;
t.z.y = j;
t.z.z = k;
t.z.w = l;
t.w.x = m;
t.w.y = n;
t.w.z = o;
t.w.w = p;
return t;
}
ccl_device_inline ProjectionTransform projection_identity()
{
return make_projection(1.0f,
0.0f,
0.0f,
0.0f,
0.0f,
1.0f,
0.0f,
0.0f,
0.0f,
0.0f,
1.0f,
0.0f,
0.0f,
0.0f,
0.0f,
1.0f);
}
#ifndef __KERNEL_GPU__
ccl_device_inline Transform projection_to_transform(const ProjectionTransform &a)
{
Transform tfm = {a.x, a.y, a.z};
return tfm;
}
#endif
ccl_device_inline ProjectionTransform projection_transpose(const ProjectionTransform a)
{
ProjectionTransform t;
t.x.x = a.x.x;
t.x.y = a.y.x;
t.x.z = a.z.x;
t.x.w = a.w.x;
t.y.x = a.x.y;
t.y.y = a.y.y;
t.y.z = a.z.y;
t.y.w = a.w.y;
t.z.x = a.x.z;
t.z.y = a.y.z;
t.z.z = a.z.z;
t.z.w = a.w.z;
t.w.x = a.x.w;
t.w.y = a.y.w;
t.w.z = a.z.w;
t.w.w = a.w.w;
return t;
}
#if !defined(__KERNEL_METAL__)
ccl_device_inline ProjectionTransform projection_inverse(const ProjectionTransform tfm)
{
ProjectionTransform tfmR = projection_identity();
float M[4][4];
float R[4][4];
memcpy(R, (const float *)&tfmR, sizeof(R));
memcpy(M, (const float *)&tfm, sizeof(M));
if (UNLIKELY(!projection_inverse_impl(R, M))) {
return projection_identity();
}
memcpy((void *)&tfmR, R, sizeof(R));
return tfmR;
}
ccl_device_inline ProjectionTransform operator*(const ProjectionTransform a,
const ProjectionTransform b)
{
const ProjectionTransform c = projection_transpose(b);
ProjectionTransform t;
t.x = make_float4(dot(a.x, c.x), dot(a.x, c.y), dot(a.x, c.z), dot(a.x, c.w));
t.y = make_float4(dot(a.y, c.x), dot(a.y, c.y), dot(a.y, c.z), dot(a.y, c.w));
t.z = make_float4(dot(a.z, c.x), dot(a.z, c.y), dot(a.z, c.z), dot(a.z, c.w));
t.w = make_float4(dot(a.w, c.x), dot(a.w, c.y), dot(a.w, c.z), dot(a.w, c.w));
return t;
}
#endif
#ifndef __KERNEL_GPU__
ccl_device_inline ProjectionTransform operator*(const ProjectionTransform &a, const Transform &b)
{
return a * ProjectionTransform(b);
}
ccl_device_inline ProjectionTransform operator*(const Transform &a, const ProjectionTransform &b)
{
return ProjectionTransform(a) * b;
}
ccl_device_inline void print_projection(const char *label, const ProjectionTransform &t)
{
print_float4(label, t.x);
print_float4(label, t.y);
print_float4(label, t.z);
print_float4(label, t.w);
printf("\n");
}
ccl_device_inline ProjectionTransform projection_perspective(const float fov,
const float n,
float f)
{
const ProjectionTransform persp = make_projection(
1, 0, 0, 0, 0, 1, 0, 0, 0, 0, f / (f - n), -f * n / (f - n), 0, 0, 1, 0);
const float inv_angle = 1.0f / tanf(0.5f * fov);
const Transform scale = transform_scale(inv_angle, inv_angle, 1);
return scale * persp;
}
ccl_device_inline ProjectionTransform projection_orthographic(const float znear, const float zfar)
{
const Transform t = transform_scale(1.0f, 1.0f, 1.0f / (zfar - znear));
return ProjectionTransform(t);
}
#endif /* __KERNEL_GPU__ */
CCL_NAMESPACE_END

View File

@@ -0,0 +1,95 @@
/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#pragma once
#include "util/defines.h"
CCL_NAMESPACE_BEGIN
ccl_device_forceinline bool projection_inverse_impl(ccl_private float R[4][4],
ccl_private float M[4][4])
{
/* SPDX-License-Identifier: BSD-3-Clause
* Adapted from code:
* Copyright (c) 2002, Industrial Light & Magic, a division of Lucas
* Digital Ltd. LLC. All rights reserved. */
/* forward elimination */
for (int i = 0; i < 4; i++) {
int pivot = i;
float pivotsize = M[i][i];
if (pivotsize < 0) {
pivotsize = -pivotsize;
}
for (int j = i + 1; j < 4; j++) {
float tmp = M[j][i];
if (tmp < 0) {
tmp = -tmp;
}
if (tmp > pivotsize) {
pivot = j;
pivotsize = tmp;
}
}
if (UNLIKELY(pivotsize == 0.0f)) {
return false;
}
if (pivot != i) {
for (int j = 0; j < 4; j++) {
float tmp;
tmp = M[i][j];
M[i][j] = M[pivot][j];
M[pivot][j] = tmp;
tmp = R[i][j];
R[i][j] = R[pivot][j];
R[pivot][j] = tmp;
}
}
for (int j = i + 1; j < 4; j++) {
const float f = M[j][i] / M[i][i];
for (int k = 0; k < 4; k++) {
M[j][k] -= f * M[i][k];
R[j][k] -= f * R[i][k];
}
}
}
/* backward substitution */
for (int i = 3; i >= 0; --i) {
float f = M[i][i];
if (UNLIKELY(f == 0.0f)) {
return false;
}
for (int j = 0; j < 4; j++) {
M[i][j] /= f;
R[i][j] /= f;
}
for (int j = 0; j < i; j++) {
f = M[j][i];
for (int k = 0; k < 4; k++) {
M[j][k] -= f * M[i][k];
R[j][k] -= f * R[i][k];
}
}
}
return true;
}
CCL_NAMESPACE_END

View File

@@ -0,0 +1,13 @@
/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#pragma once
#include <queue>
CCL_NAMESPACE_BEGIN
using std::queue;
CCL_NAMESPACE_END

View File

@@ -0,0 +1,133 @@
/* SPDX-FileCopyrightText: 2026 Blender Authors
*
* SPDX-License-Identifier: Apache-2.0 */
#pragma once
#include <iterator>
CCL_NAMESPACE_BEGIN
/**
* Simplifies implementing a random-access-iterator.
*
* The actual iterator should derive from this class publicly. Additionally, it has to provide a
* const `iter_prop` method which returns a reference to the internal property that corresponds to
* the current position. This is typically a pointer or an index.
*
* Implementing some random-access-iterator is generally quite simple but requires a lot of
* boilerplate code because algorithms expect many operators to work on the iterator type.
* They are expected to behave similarly to pointers and thus have to implement many of the same
* operators.
*/
template<typename Derived> class random_access_iterator_mixin {
public:
using iterator_category = std::random_access_iterator_tag;
using difference_type = std::ptrdiff_t;
constexpr friend Derived &operator++(Derived &a)
{
++a.iter_prop_mutable();
return a;
}
constexpr friend Derived operator++(Derived &a, int)
{
Derived copy = a;
++a;
return copy;
}
constexpr friend Derived &operator--(Derived &a)
{
--a.iter_prop_mutable();
return a;
}
constexpr friend Derived operator--(Derived &a, int)
{
Derived copy = a;
--a;
return copy;
}
constexpr friend Derived &operator+=(Derived &a, const std::ptrdiff_t n)
{
a.iter_prop_mutable() += n;
return a;
}
constexpr friend Derived &operator-=(Derived &a, const std::ptrdiff_t n)
{
a.iter_prop_mutable() -= n;
return a;
}
constexpr friend Derived operator+(const Derived &a, const std::ptrdiff_t n)
{
Derived copy = a;
copy.iter_prop_mutable() += n;
return copy;
}
constexpr friend Derived operator-(const Derived &a, const std::ptrdiff_t n)
{
Derived copy = a;
copy.iter_prop_mutable() -= n;
return copy;
}
constexpr friend auto operator-(const Derived &a, const Derived &b)
{
return a.iter_prop() - b.iter_prop();
}
constexpr friend bool operator!=(const Derived &a, const Derived &b)
{
return a.iter_prop() != b.iter_prop();
}
constexpr friend bool operator==(const Derived &a, const Derived &b)
{
return a.iter_prop() == b.iter_prop();
}
constexpr friend bool operator<(const Derived &a, const Derived &b)
{
return a.iter_prop() < b.iter_prop();
}
constexpr friend bool operator>(const Derived &a, const Derived &b)
{
return a.iter_prop() > b.iter_prop();
}
constexpr friend bool operator<=(const Derived &a, const Derived &b)
{
return a.iter_prop() <= b.iter_prop();
}
constexpr friend bool operator>=(const Derived &a, const Derived &b)
{
return a.iter_prop() >= b.iter_prop();
}
constexpr decltype(auto) operator[](const std::ptrdiff_t i)
{
return *(*static_cast<Derived *>(this) + i);
}
constexpr decltype(auto) operator[](const std::ptrdiff_t i) const
{
return *(*static_cast<const Derived *>(this) + i);
}
auto &iter_prop_mutable()
{
const auto &const_iter_prop = static_cast<const Derived *>(this)->iter_prop();
return const_cast<std::remove_const_t<std::remove_reference_t<decltype(const_iter_prop)>> &>(
const_iter_prop);
}
};
CCL_NAMESPACE_END

View File

@@ -0,0 +1,61 @@
/* SPDX-FileCopyrightText: 2017-2022 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#pragma once
#include "util/math_base.h"
#include "util/types_int4.h"
CCL_NAMESPACE_BEGIN
/* Rectangles are represented as a int4 containing the coordinates of the lower-left and
* upper-right corners in the order (x0, y0, x1, y1). */
ccl_device_inline int4 rect_from_shape(const int x0, const int y0, const int w, int h)
{
return make_int4(x0, y0, x0 + w, y0 + h);
}
ccl_device_inline int4 rect_expand(const int4 rect, const int d)
{
return make_int4(rect.x - d, rect.y - d, rect.z + d, rect.w + d);
}
/* Returns the intersection of two rects. */
ccl_device_inline int4 rect_clip(const int4 a, const int4 b)
{
return make_int4(max(a.x, b.x), max(a.y, b.y), min(a.z, b.z), min(a.w, b.w));
}
ccl_device_inline bool rect_is_valid(const int4 rect)
{
return (rect.z > rect.x) && (rect.w > rect.y);
}
/* Returns the local row-major index of the pixel inside the rect. */
ccl_device_inline int coord_to_local_index(const int4 rect, const int x, int y)
{
const int w = rect.z - rect.x;
return (y - rect.y) * w + (x - rect.x);
}
/* Finds the coordinates of a pixel given by its row-major index in the rect,
* and returns whether the pixel is inside it. */
ccl_device_inline bool local_index_to_coord(const int4 rect,
const int idx,
ccl_private int *x,
ccl_private int *y)
{
const int w = rect.z - rect.x;
*x = (idx % w) + rect.x;
*y = (idx / w) + rect.y;
return (*y < rect.w);
}
ccl_device_inline int rect_size(const int4 rect)
{
return (rect.z - rect.x) * (rect.w - rect.y);
}
CCL_NAMESPACE_END

View File

@@ -0,0 +1,39 @@
/* SPDX-FileCopyrightText: 2026 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#pragma once
#include <utility>
CCL_NAMESPACE_BEGIN
/* Execute a callable when the scope exits. */
template<typename F> class ScopedDefer {
public:
explicit ScopedDefer(F &&func) : func_(std::forward<F>(func)) {}
~ScopedDefer()
{
func_();
}
ScopedDefer(const ScopedDefer &) = delete;
ScopedDefer &operator=(const ScopedDefer &) = delete;
private:
F func_;
};
/* Helper to deduce template argument. */
template<typename F> ScopedDefer<F> make_scoped_defer(F &&func)
{
return ScopedDefer<F>(std::forward<F>(func));
}
#define SCOPED_DEFER_NAME2(a, b) a##b
#define SCOPED_DEFER_NAME(a, b) SCOPED_DEFER_NAME2(a, b)
#define SCOPED_DEFER(code) \
auto SCOPED_DEFER_NAME(_scoped_defer_, __LINE__) = make_scoped_defer([&]() { code; })
CCL_NAMESPACE_END

View File

@@ -0,0 +1,44 @@
/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#pragma once
#include "util/thread.h"
CCL_NAMESPACE_BEGIN
/* Counting Semaphore
*
* To restrict concurrent access to a resource to a specified number
* of threads. Similar to std::counting_semaphore from C++20. */
class thread_counting_semaphore {
public:
explicit thread_counting_semaphore(const int count) : count(count) {}
thread_counting_semaphore(const thread_counting_semaphore &) = delete;
void acquire()
{
thread_scoped_lock lock(mutex);
while (count == 0) {
condition.wait(lock);
}
count--;
}
void release()
{
const thread_scoped_lock lock(mutex);
count++;
condition.notify_one();
}
protected:
thread_mutex mutex;
thread_condition_variable condition;
int count;
};
CCL_NAMESPACE_END

View File

@@ -0,0 +1,19 @@
/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#pragma once
#include <set>
#include <unordered_set>
#if defined(_MSC_VER) && (_MSC_VER >= 1900)
# include <iterator>
#endif
CCL_NAMESPACE_BEGIN
using std::set;
using std::unordered_set;
CCL_NAMESPACE_END

View File

@@ -0,0 +1,477 @@
/* SPDX-FileCopyrightText: 2011-2013 Intel Corporation
* SPDX-FileCopyrightText: 2014-2022 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#pragma once
#include <cstdint>
#include <limits>
#include "util/defines.h"
#include "util/optimization.h"
/* SSE Intrinsics includes
*
* We assume __KERNEL_SSEX__ flags to have been defined at this point.
*
* MinGW64 has conflicting declarations for these SSE headers in <windows.h>.
* Since we can't avoid including <windows.h>, better only include that */
#if defined(FREE_WINDOWS64)
# include "util/windows.h"
#elif defined(_MSC_VER) && !defined(__KERNEL_NEON__)
# include <intrin.h>
#elif (defined(__x86_64__) || defined(__i386__))
# include <x86intrin.h>
#elif defined(__KERNEL_NEON__)
# define SSE2NEON_PRECISE_MINMAX 1
# include <sse2neon.h>
#endif
/* Floating Point Control, for Embree. */
#if defined(__x86_64__) || defined(_M_X64)
# define SIMD_SET_FLUSH_TO_ZERO \
_MM_SET_FLUSH_ZERO_MODE(_MM_FLUSH_ZERO_ON); \
_MM_SET_DENORMALS_ZERO_MODE(_MM_DENORMALS_ZERO_ON);
#elif defined(__aarch64__) || defined(_M_ARM64)
/* The get/set denormals to zero was implemented in sse2neon v1.5.0.
* Keep the compatibility code until the minimum library version is increased. */
# if defined(_MM_SET_FLUSH_ZERO_MODE)
# define SIMD_SET_FLUSH_TO_ZERO \
_MM_SET_FLUSH_ZERO_MODE(_MM_FLUSH_ZERO_ON); \
_MM_SET_DENORMALS_ZERO_MODE(_MM_DENORMALS_ZERO_ON);
# elif !defined(_M_ARM64)
# define _MM_FLUSH_ZERO_ON 24
# define __get_fpcr(__fpcr) __asm__ __volatile__("mrs %0,fpcr" : "=r"(__fpcr))
# define __set_fpcr(__fpcr) __asm__ __volatile__("msr fpcr,%0" : : "ri"(__fpcr))
# define SIMD_SET_FLUSH_TO_ZERO set_fz(_MM_FLUSH_ZERO_ON);
# define SIMD_GET_FLUSH_TO_ZERO get_fz(_MM_FLUSH_ZERO_ON)
# else
# define _MM_FLUSH_ZERO_ON 24
# define __get_fpcr(__fpcr) __fpcr = _ReadStatusReg(0x5A20)
# define __set_fpcr(__fpcr) _WriteStatusReg(0x5A20, __fpcr)
# define SIMD_SET_FLUSH_TO_ZERO set_fz(_MM_FLUSH_ZERO_ON);
# define SIMD_GET_FLUSH_TO_ZERO get_fz(_MM_FLUSH_ZERO_ON)
# endif
#else
# define SIMD_SET_FLUSH_TO_ZERO
#endif
CCL_NAMESPACE_BEGIN
/* Data structures used by SSE classes. */
#ifdef __KERNEL_SSE2__
extern const __m128 _mm_lookupmask_ps[16];
static struct TrueTy {
__forceinline operator bool() const
{
return true;
}
} True ccl_attr_maybe_unused;
static struct FalseTy {
__forceinline operator bool() const
{
return false;
}
} False ccl_attr_maybe_unused;
static struct ZeroTy {
__forceinline operator float() const
{
return 0;
}
__forceinline operator int() const
{
return 0;
}
} zero ccl_attr_maybe_unused;
static struct OneTy {
__forceinline operator float() const
{
return 1;
}
__forceinline operator int() const
{
return 1;
}
} one ccl_attr_maybe_unused;
static struct NegInfTy {
__forceinline operator float() const
{
return -std::numeric_limits<float>::infinity();
}
__forceinline operator int() const
{
return std::numeric_limits<int>::min();
}
} neg_inf ccl_attr_maybe_unused;
static struct PosInfTy {
__forceinline operator float() const
{
return std::numeric_limits<float>::infinity();
}
__forceinline operator int() const
{
return std::numeric_limits<int>::max();
}
} inf ccl_attr_maybe_unused, pos_inf ccl_attr_maybe_unused;
static struct StepTy {
} step ccl_attr_maybe_unused;
#endif
#if (defined(__aarch64__) || defined(_M_ARM64)) && !defined(_MM_SET_FLUSH_ZERO_MODE)
__forceinline int set_fz(const uint32_t flag)
{
uint64_t old_fpcr;
uint64_t new_fpcr;
__get_fpcr(old_fpcr);
new_fpcr = old_fpcr | (uint64_t(1) << flag);
__set_fpcr(new_fpcr);
__get_fpcr(old_fpcr);
return old_fpcr == new_fpcr;
}
__forceinline int get_fz(const uint32_t flag)
{
uint64_t cur_fpcr;
__get_fpcr(cur_fpcr);
return (cur_fpcr & (uint64_t(1) << flag)) > 0 ? 1 : 0;
}
#endif
/* Utilities used by Neon */
#if defined(__KERNEL_NEON__)
template<class type, const int i0, const int i1, const int i2, const int i3>
type shuffle_neon(const type &a)
{
if (i0 == i1 && i0 == i2 && i0 == i3) {
return type(vdupq_laneq_s32(int32x4_t(a), i0));
}
static const uint8_t tbl[16] = {(i0 * 4) + 0,
(i0 * 4) + 1,
(i0 * 4) + 2,
(i0 * 4) + 3,
(i1 * 4) + 0,
(i1 * 4) + 1,
(i1 * 4) + 2,
(i1 * 4) + 3,
(i2 * 4) + 0,
(i2 * 4) + 1,
(i2 * 4) + 2,
(i2 * 4) + 3,
(i3 * 4) + 0,
(i3 * 4) + 1,
(i3 * 4) + 2,
(i3 * 4) + 3};
return type(vqtbl1q_s8(int8x16_t(a), *(uint8x16_t *)tbl));
}
template<class type, const int i0, const int i1, const int i2, const int i3>
type shuffle_neon(const type &a, const type &b)
{
if (&a == &b) {
static const uint8_t tbl[16] = {(i0 * 4) + 0,
(i0 * 4) + 1,
(i0 * 4) + 2,
(i0 * 4) + 3,
(i1 * 4) + 0,
(i1 * 4) + 1,
(i1 * 4) + 2,
(i1 * 4) + 3,
(i2 * 4) + 0,
(i2 * 4) + 1,
(i2 * 4) + 2,
(i2 * 4) + 3,
(i3 * 4) + 0,
(i3 * 4) + 1,
(i3 * 4) + 2,
(i3 * 4) + 3};
return type(vqtbl1q_s8(int8x16_t(b), *(uint8x16_t *)tbl));
}
else {
static const uint8_t tbl[16] = {(i0 * 4) + 0,
(i0 * 4) + 1,
(i0 * 4) + 2,
(i0 * 4) + 3,
(i1 * 4) + 0,
(i1 * 4) + 1,
(i1 * 4) + 2,
(i1 * 4) + 3,
(i2 * 4) + 0 + 16,
(i2 * 4) + 1 + 16,
(i2 * 4) + 2 + 16,
(i2 * 4) + 3 + 16,
(i3 * 4) + 0 + 16,
(i3 * 4) + 1 + 16,
(i3 * 4) + 2 + 16,
(i3 * 4) + 3 + 16};
/* NOTE: This cannot all be put in a single line due to how MSVC ARM64
* implements the function calls as several layers of macros. */
int8x16x2_t t = {int8x16_t(a), int8x16_t(b)};
uint8x16_t idx = *(uint8x16_t *)tbl;
return type(vqtbl2q_s8(t, idx));
}
}
#endif /* __KERNEL_NEON */
/* Intrinsics Functions
*
* For fast bit operations. */
#if defined(__BMI__) && defined(__GNUC__)
# ifndef _tzcnt_u32
# define _tzcnt_u32 __tzcnt_u32
# endif
# ifndef _tzcnt_u64
# define _tzcnt_u64 __tzcnt_u64
# endif
#endif
#if defined(__LZCNT__)
# define _lzcnt_u32 __lzcnt32
# define _lzcnt_u64 __lzcnt64
#endif
#if defined(_WIN32) && !defined(__MINGW32__) && !defined(__clang__)
/* Intrinsic functions on Windows. */
__forceinline uint32_t __bsf(const uint32_t v)
{
# if defined(__KERNEL_AVX2__)
return _tzcnt_u32(v);
# else
unsigned long r = 0;
_BitScanForward(&r, v);
return r;
# endif
}
__forceinline uint32_t __bsr(const uint32_t v)
{
unsigned long r = 0;
_BitScanReverse(&r, v);
return r;
}
__forceinline uint32_t __btc(const uint32_t v, const uint32_t i)
{
long r = v;
_bittestandcomplement(&r, i);
return r;
}
__forceinline uint32_t bitscan(const uint32_t v)
{
# if defined(__KERNEL_AVX2__)
return _tzcnt_u32(v);
# else
return __bsf(v);
# endif
}
# if defined(__KERNEL_64_BIT__)
__forceinline uint64_t __bsf(const uint64_t v)
{
# if defined(__KERNEL_AVX2__)
return _tzcnt_u64(v);
# else
unsigned long r = 0;
_BitScanForward64(&r, v);
return r;
# endif
}
__forceinline uint64_t __bsr(const uint64_t v)
{
unsigned long r = 0;
_BitScanReverse64(&r, v);
return r;
}
__forceinline uint64_t __btc(const uint64_t v, const uint64_t i)
{
uint64_t r = v;
_bittestandcomplement64((__int64 *)&r, i);
return r;
}
__forceinline uint64_t bitscan(const uint64_t v)
{
# if defined(__KERNEL_AVX2__)
# if defined(__KERNEL_64_BIT__)
return _tzcnt_u64(v);
# else
return _tzcnt_u32(v);
# endif
# else
return __bsf(v);
# endif
}
# endif /* __KERNEL_64_BIT__ */
#elif (defined(__x86_64__) || defined(__i386__)) && defined(__KERNEL_SSE2__)
/* Intrinsic functions with x86 SSE. */
__forceinline uint32_t __bsf(const uint32_t v)
{
uint32_t r = 0;
asm("bsf %1,%0" : "=r"(r) : "r"(v));
return r;
}
__forceinline uint32_t __bsr(const uint32_t v)
{
uint32_t r = 0;
asm("bsr %1,%0" : "=r"(r) : "r"(v));
return r;
}
__forceinline uint32_t __btc(const uint32_t v, const uint32_t i)
{
uint32_t r = 0;
asm("btc %1,%0" : "=r"(r) : "r"(i), "0"(v) : "flags");
return r;
}
# if (defined(__KERNEL_64_BIT__) || defined(__APPLE__)) && \
!(defined(__ILP32__) && defined(__x86_64__))
__forceinline uint64_t __bsf(const uint64_t v)
{
uint64_t r = 0;
asm("bsf %1,%0" : "=r"(r) : "r"(v));
return r;
}
# endif
__forceinline uint64_t __bsr(const uint64_t v)
{
uint64_t r = 0;
asm("bsr %1,%0" : "=r"(r) : "r"(v));
return r;
}
__forceinline uint64_t __btc(const uint64_t v, const uint64_t i)
{
uint64_t r = 0;
asm("btc %1,%0" : "=r"(r) : "r"(i), "0"(v) : "flags");
return r;
}
__forceinline uint32_t bitscan(const uint32_t v)
{
# if defined(__KERNEL_AVX2__)
return _tzcnt_u32(v);
# else
return __bsf(v);
# endif
}
# if (defined(__KERNEL_64_BIT__) || defined(__APPLE__)) && \
!(defined(__ILP32__) && defined(__x86_64__))
__forceinline uint64_t bitscan(const uint64_t v)
{
# if defined(__KERNEL_AVX2__)
# if defined(__KERNEL_64_BIT__)
return _tzcnt_u64(v);
# else
return _tzcnt_u32(v);
# endif
# else
return __bsf(v);
# endif
}
# endif
#else
/* Intrinsic functions fallback for arbitrary processor. */
__forceinline uint32_t __bsf(const uint32_t x)
{
for (uint32_t i = 0; i < 32; i++) {
if (x & (uint32_t(1) << i)) {
return i;
}
}
return 32;
}
__forceinline uint32_t __bsr(const uint32_t x)
{
for (uint32_t i = 0; i < 32; i++) {
if (x & (uint32_t(1) << (31 - i))) {
return (31 - i);
}
}
return 32;
}
__forceinline uint32_t __btc(const uint32_t x, const uint32_t bit)
{
const uint32_t mask = uint32_t(1) << bit;
return x ^ mask;
}
__forceinline uint32_t __bsf(const uint64_t x)
{
for (uint32_t i = 0; i < 64; i++) {
if (x & (uint64_t(1) << i)) {
return i;
}
}
return 64;
}
__forceinline uint32_t __bsr(const uint64_t x)
{
for (uint32_t i = 0; i < 64; i++) {
if (x & (uint64_t(1) << (63 - i))) {
return (63 - i);
}
}
return 64;
}
__forceinline uint64_t __btc(const uint64_t x, const uint32_t bit)
{
const uint64_t mask = uint64_t(1) << bit;
return x ^ mask;
}
__forceinline uint32_t bitscan(const uint32_t value)
{
assert(value != 0);
return __bsf(value);
}
__forceinline uint64_t bitscan(const uint64_t value)
{
assert(value != 0);
return __bsf(value);
}
#endif /* Intrinsics */
/* Older GCC versions do not have _mm256_cvtss_f32 yet, so define it ourselves.
* _mm256_castps256_ps128 generates no instructions so this is just as efficient. */
#if defined(__KERNEL_AVX__) || defined(__KERNEL_AVX2__)
# undef _mm256_cvtss_f32
# define _mm256_cvtss_f32(a) (_mm_cvtss_f32(_mm256_castps256_ps128(a)))
#endif
/* quiet unused define warnings */
#if defined(__KERNEL_SSE2__) || defined(__KERNEL_SSE3__) || defined(__KERNEL_SSSE3__) || \
defined(__KERNEL_SSE42__) || defined(__KERNEL_AVX__) || defined(__KERNEL_AVX2__)
/* do nothing */
#endif
CCL_NAMESPACE_END

View File

@@ -0,0 +1,147 @@
/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#pragma once
#include <cstddef>
#include "util/defines.h"
#include "util/guarded_allocator.h"
CCL_NAMESPACE_BEGIN
/* Stack allocator for the use with STL. */
template<int SIZE, typename T> class ccl_try_align(16) StackAllocator {
public:
using size_type = size_t;
using difference_type = ptrdiff_t;
using pointer = T *;
using const_pointer = const T *;
using reference = T &;
using const_reference = const T &;
using value_type = T;
/* Allocator construction/destruction. */
StackAllocator() : pointer_(0), use_stack_(true) {}
StackAllocator(const StackAllocator & /*unused*/) : pointer_(0), use_stack_(true) {}
template<class U>
StackAllocator(const StackAllocator<SIZE, U> & /*unused*/) : pointer_(0), use_stack_(false)
{
}
/* Memory allocation/deallocation. */
T *allocate(const size_t n, const void *hint = nullptr)
{
(void)hint;
if (n == 0) {
return nullptr;
}
if (pointer_ + n >= SIZE || use_stack_ == false) {
size_t size = n * sizeof(T);
util_guarded_mem_alloc(size);
T *mem;
#ifdef WITH_BLENDER_GUARDEDALLOC
mem = (T *)MEM_new_uninitialized_aligned(size, 16, "Cycles Alloc");
#else
mem = (T *)malloc(size);
#endif
if (mem == nullptr) {
throw std::bad_alloc();
}
return mem;
}
T *mem = &data_[pointer_];
pointer_ += n;
return mem;
}
void deallocate(T *p, const size_t n)
{
if (p == nullptr) {
return;
}
if (p < data_ || p >= data_ + SIZE) {
util_guarded_mem_free(n * sizeof(T));
#ifdef WITH_BLENDER_GUARDEDALLOC
MEM_delete_void(static_cast<void *>(p));
#else
free(p);
#endif
return;
}
/* We don't support memory free for the stack allocator. */
}
/* Address of an reference. */
T *address(T &x) const
{
return &x;
}
const T *address(const T &x) const
{
return &x;
}
/* Object construction/destruction. */
void construct(T *p, const T &val)
{
if (p != nullptr) {
new (p) T(val);
}
}
void destroy(T *p)
{
p->~T();
}
/* Maximum allocation size. */
size_t max_size() const
{
return size_t(-1);
}
/* Rebind to other type of allocator. */
template<class U> struct rebind {
using other = StackAllocator<SIZE, U>;
};
/* Operators */
template<class U> StackAllocator &operator=(const StackAllocator<SIZE, U> & /*unused*/)
{
return *this;
}
StackAllocator<SIZE, T> &operator=(const StackAllocator & /*unused*/)
{
return *this;
}
bool operator==(const StackAllocator & /*other*/) const
{
return true;
}
bool operator!=(const StackAllocator &other) const
{
return !operator==(other);
}
private:
int pointer_;
bool use_stack_;
T data_[SIZE];
};
CCL_NAMESPACE_END

View File

@@ -0,0 +1,21 @@
/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
/* clang-format off */
#pragma once
/* #define static_assert triggers a bug in some clang-format versions, disable
* format for entire file to keep results consistent. */
CCL_NAMESPACE_BEGIN
#if defined(CYCLES_CUBIN_CC)
# define static_assert(statement, message)
#endif
#define static_assert_align(st, align) \
static_assert((sizeof(st) % (align) == 0), "Structure must be strictly aligned") // NOLINT
CCL_NAMESPACE_END

View File

@@ -0,0 +1,34 @@
/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#pragma once
#include "util/atomic.h"
CCL_NAMESPACE_BEGIN
class Stats {
public:
enum static_init_t { static_init = 0 };
Stats() : mem_used(0), mem_peak(0) {}
explicit Stats(static_init_t /*unused*/) {}
void mem_alloc(const size_t size)
{
atomic_add_and_fetch_z(&mem_used, size);
atomic_fetch_and_update_max_z(&mem_peak, mem_used);
}
void mem_free(const size_t size)
{
assert(mem_used >= size);
atomic_sub_and_fetch_z(&mem_used, size);
}
size_t mem_used;
size_t mem_peak;
};
CCL_NAMESPACE_END

View File

@@ -0,0 +1,326 @@
/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#include <cstdarg>
#include <cstdio>
#include <algorithm>
#include <cctype>
#include "util/string.h"
#include "util/types_float4.h"
#ifdef _WIN32
# include "util/windows.h"
# ifndef vsnprintf
# define vsnprintf _vsnprintf
# endif
#endif /* _WIN32 */
CCL_NAMESPACE_BEGIN
string string_printf(const char *format, ...)
{
vector<char> str(128, 0);
while (true) {
va_list args;
int result;
va_start(args, format);
result = vsnprintf(str.data(), str.size(), format, args);
va_end(args);
if (result == -1) {
/* not enough space or formatting error */
if (str.size() > 65536) {
assert(0);
return string("");
}
str.resize(str.size() * 2, 0);
continue;
}
if (result >= (int)str.size()) {
/* not enough space */
str.resize(result + 1, 0);
continue;
}
return string(str.data());
}
}
bool string_iequals(const string &a, const string &b)
{
if (a.size() == b.size()) {
for (size_t i = 0; i < a.size(); i++) {
if (toupper(a[i]) != toupper(b[i])) {
return false;
}
}
return true;
}
return false;
}
void string_split(vector<string> &tokens,
const string &str,
const string &separators,
bool skip_empty_tokens)
{
size_t token_start = 0;
size_t token_length = 0;
for (size_t i = 0; i < str.size(); ++i) {
const char ch = str[i];
if (separators.find(ch) == string::npos) {
/* Current character is not a separator,
* append it to token by increasing token length.
*/
++token_length;
}
else {
/* Current character is a separator,
* append current token to the list.
*/
if (!skip_empty_tokens || token_length > 0) {
const string token = str.substr(token_start, token_length);
tokens.push_back(token);
}
token_start = i + 1;
token_length = 0;
}
}
/* Append token from the tail of the string if exists. */
if (token_length) {
const string token = str.substr(token_start, token_length);
tokens.push_back(token);
}
}
bool string_startswith(const string_view s, const string_view start)
{
const size_t len = start.size();
if (len > s.size()) {
return false;
}
for (size_t i = 0; i < len; i++) {
if (s[i] != start[i]) {
return false;
}
}
return true;
}
bool string_endswith(const string_view s, const string_view end)
{
const size_t len = end.size();
if (len > s.size()) {
return false;
}
const size_t offset = s.size() - len;
for (size_t i = 0; i < len; i++) {
if (s[offset + i] != end[i]) {
return false;
}
}
return true;
}
string string_strip(const string &s)
{
string result = s;
result.erase(0, result.find_first_not_of(' '));
result.erase(result.find_last_not_of(' ') + 1);
return result;
}
void string_replace(string &haystack, const string &needle, const string &other)
{
size_t i = 0;
size_t index;
while ((index = haystack.find(needle, i)) != string::npos) {
haystack.replace(index, needle.size(), other);
i = index + other.size();
}
}
void string_replace_same_length(string &haystack, const string &needle, const string &other)
{
assert(needle.size() == other.size());
size_t pos = 0;
while (pos != string::npos) {
pos = haystack.find(needle, pos);
if (pos != string::npos) {
memcpy(haystack.data() + pos, other.data(), other.size());
pos += other.size();
}
}
}
string string_remove_trademark(const string &s)
{
string result = s;
/* Special case, so we don't leave sequential spaces behind. */
/* TODO(sergey): Consider using regex perhaps? */
string_replace(result, " (TM)", "");
string_replace(result, " (R)", "");
string_replace(result, "(TM)", "");
string_replace(result, "(R)", "");
return string_strip(result);
}
string string_from_bool(bool var)
{
if (var) {
return "True";
}
return "False";
}
string string_hex(const uint8_t *data, const size_t size)
{
string result;
for (size_t i = 0; i < size; i++) {
result += string_printf("%x", data[i]);
}
return result;
}
string to_string(const char *str)
{
return string(str);
}
string to_string(const float2 &v)
{
return string_printf("%f,%f", v.x, v.y);
}
string to_string(const float4 &v)
{
return string_printf("%f,%f,%f,%f", v.x, v.y, v.z, v.w);
}
string string_to_lower(const string &s)
{
string r = s;
std::transform(r.begin(), r.end(), r.begin(), [](char c) { return std::tolower(c); });
return r;
}
string string_remove_gpu_from_cpu_name(const string &s)
{
if (s.find("AMD") == std::string::npos) {
return s;
}
size_t pos = s.find("w/");
if (pos == std::string::npos) {
pos = s.find("with");
}
if (pos != std::string::npos) {
return string_strip(s.substr(0, pos));
}
return s;
}
/* Wide char strings helpers for Windows. */
#ifdef _WIN32
wstring string_to_wstring(const string &str)
{
const int length_wc = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), str.length(), nullptr, 0);
wstring str_wc(length_wc, 0);
MultiByteToWideChar(CP_UTF8, 0, str.c_str(), str.length(), &str_wc[0], length_wc);
return str_wc;
}
string string_from_wstring(const wstring &str)
{
int length_mb = WideCharToMultiByte(
CP_UTF8, 0, str.c_str(), str.size(), nullptr, 0, nullptr, nullptr);
string str_mb(length_mb, 0);
WideCharToMultiByte(
CP_UTF8, 0, str.c_str(), str.size(), &str_mb[0], length_mb, nullptr, nullptr);
return str_mb;
}
string string_to_ansi(const string &str)
{
const int length_wc = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), str.length(), nullptr, 0);
wstring str_wc(length_wc, 0);
MultiByteToWideChar(CP_UTF8, 0, str.c_str(), str.length(), &str_wc[0], length_wc);
int length_mb = WideCharToMultiByte(
CP_ACP, 0, str_wc.c_str(), str_wc.size(), nullptr, 0, nullptr, nullptr);
string str_mb(length_mb, 0);
WideCharToMultiByte(
CP_ACP, 0, str_wc.c_str(), str_wc.size(), &str_mb[0], length_mb, nullptr, nullptr);
return str_mb;
}
#endif /* _WIN32 */
string string_human_readable_size(size_t size)
{
static const char suffixes[] = "BKMGTPEZY";
const char *suffix = suffixes;
size_t r = 0;
while (size >= 1024) {
r = size % 1024;
size /= 1024;
suffix++;
}
if (*suffix != 'B') {
return string_printf("%.2f%c", double(size * 1024 + r) / 1024.0, *suffix);
}
return string_printf("%zuB", size);
}
string string_human_readable_number(size_t num)
{
if (num == 0) {
return "0";
}
/* Add thousands separators. */
char buf[32];
char *p = buf + 31;
*p = '\0';
int i = -1;
while (num) {
i++;
if (i && i % 3 == 0) {
*(--p) = ',';
}
*(--p) = '0' + (num % 10);
num /= 10;
}
return p;
}
CCL_NAMESPACE_END

View File

@@ -0,0 +1,73 @@
/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#pragma once
#include <cstring>
#include <string>
/* Use string view implementation from OIIO.
* Ideally, need to switch to `std::string_view`, but this first requires getting rid of using
* namespace OIIO as it causes symbol collision. */
#include <OpenImageIO/string_view.h>
#include "util/vector.h"
CCL_NAMESPACE_BEGIN
struct float2;
struct float4;
using std::string;
using std::to_string;
using OIIO::string_view;
#ifdef __GNUC__
# define PRINTF_ATTRIBUTE __attribute__((format(printf, 1, 2)))
#else
# define PRINTF_ATTRIBUTE
#endif
string string_printf(const char *format, ...) PRINTF_ATTRIBUTE;
bool string_iequals(const string &a, const string &b);
void string_split(vector<string> &tokens,
const string &str,
const string &separators = "\t ",
bool skip_empty_tokens = true);
void string_replace(string &haystack, const string &needle, const string &other);
void string_replace_same_length(string &haystack, const string &needle, const string &other);
bool string_startswith(string_view s, string_view start);
bool string_endswith(string_view s, string_view end);
string string_strip(const string &s);
string string_remove_trademark(const string &s);
string string_from_bool(const bool var);
string string_hex(const uint8_t *data, const size_t size);
string to_string(const char *str);
string to_string(const float2 &v);
string to_string(const float4 &v);
string string_to_lower(const string &s);
string string_remove_gpu_from_cpu_name(const string &s);
/* Wide char strings are only used on Windows to deal with non-ASCII
* characters in file names and such. No reason to use such strings
* for something else at this moment.
*
* Please note that strings are expected to be in UTF8 code-page, and
* if ANSI is needed then explicit conversion required.
*/
#ifdef _WIN32
using std::wstring;
wstring string_to_wstring(const string &path);
string string_from_wstring(const wstring &path);
string string_to_ansi(const string &str);
#endif
/* Make a string from a size in bytes in human readable form. */
string string_human_readable_size(const size_t size);
/* Make a string from a unit-less quantity in human readable form. */
string string_human_readable_number(const size_t num);
CCL_NAMESPACE_END

View File

@@ -0,0 +1,311 @@
/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#include "util/system.h"
#include "util/log.h"
#include "util/string.h"
#include <algorithm>
#include <cstring>
#ifdef _WIN32
# include <cstdio>
# if (!defined(FREE_WINDOWS))
# include <intrin.h>
# endif
# include "util/windows.h"
#elif defined(__APPLE__)
# include <cstdint>
# include <sys/ioctl.h>
# include <sys/resource.h>
# include <sys/sysctl.h>
# include <sys/types.h>
# include <unistd.h>
#else
# include <cstdint>
# include <sys/ioctl.h>
# include <sys/resource.h>
# include <unistd.h>
#endif
CCL_NAMESPACE_BEGIN
int system_console_width()
{
int columns = 0;
#ifdef _WIN32
CONSOLE_SCREEN_BUFFER_INFO csbi;
if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi)) {
columns = csbi.dwSize.X;
}
#else
struct winsize w;
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) == 0) {
columns = w.ws_col;
}
#endif
return (columns > 0) ? columns : 80;
}
/* Equivalent of Windows __cpuid for x86 processors on other platforms. */
#if (!defined(_WIN32) || defined(FREE_WINDOWS)) && (defined(__x86_64__) || defined(__i386__))
static void __cpuid(int data[4], int selector)
{
# if defined(__x86_64__)
asm("cpuid" : "=a"(data[0]), "=b"(data[1]), "=c"(data[2]), "=d"(data[3]) : "a"(selector));
# elif defined(__i386__)
asm("pushl %%ebx \n\t"
"cpuid \n\t"
"movl %%ebx, %1 \n\t"
"popl %%ebx \n\t"
: "=a"(data[0]), "=r"(data[1]), "=c"(data[2]), "=d"(data[3])
: "a"(selector)
: "ebx");
# else
data[0] = data[1] = data[2] = data[3] = 0;
# endif
}
#endif
string system_cpu_brand_string()
{
#if defined(__APPLE__)
/* Get from system on macOS. */
char modelname[512] = "";
size_t bufferlen = 512;
if (sysctlbyname("machdep.cpu.brand_string", &modelname, &bufferlen, nullptr, 0) == 0) {
return modelname;
}
#elif (defined(WIN32) || defined(__x86_64__) || defined(__i386__)) && !defined(_M_ARM64)
/* Get from intrinsics on Windows and x86. */
char buf[49] = {0};
int result[4] = {0};
__cpuid(result, 0x80000000);
if (result[0] != 0 && result[0] >= (int)0x80000004) {
__cpuid((int *)(buf + 0), 0x80000002);
__cpuid((int *)(buf + 16), 0x80000003);
__cpuid((int *)(buf + 32), 0x80000004);
string brand = buf;
/* Make it a bit more presentable. */
brand = string_remove_trademark(brand);
brand = string_remove_gpu_from_cpu_name(brand);
return brand;
}
#elif defined(_M_ARM64)
DWORD processorNameStringLength = 255;
char processorNameString[255];
if (RegGetValueA(HKEY_LOCAL_MACHINE,
"HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0",
"ProcessorNameString",
RRF_RT_REG_SZ,
nullptr,
&processorNameString,
&processorNameStringLength) == ERROR_SUCCESS)
{
return processorNameString;
}
#else
/* Get from /proc/cpuinfo on Unix systems. */
FILE *cpuinfo = fopen("/proc/cpuinfo", "r");
if (cpuinfo != nullptr) {
char cpuinfo_buf[513] = "";
fread(cpuinfo_buf, sizeof(cpuinfo_buf) - 1, 1, cpuinfo);
fclose(cpuinfo);
char *modelname = strstr(cpuinfo_buf, "model name");
if (modelname != nullptr) {
modelname = strchr(modelname, ':');
if (modelname != nullptr) {
modelname += 2;
char *modelname_end = strchr(modelname, '\n');
if (modelname_end != nullptr) {
*modelname_end = '\0';
return modelname;
}
}
}
}
#endif
return "Unknown CPU";
}
int system_cpu_bits()
{
return (sizeof(void *) * 8);
}
#if defined(__x86_64__) || defined(_M_X64) || defined(__i386__) || defined(_M_IX86)
struct CPUCapabilities {
bool sse42;
bool avx2;
bool f16c;
};
static CPUCapabilities &system_cpu_capabilities()
{
static CPUCapabilities caps = {};
static bool caps_init = false;
if (!caps_init) {
int result[4], num;
__cpuid(result, 0);
num = result[0];
if (num >= 1) {
__cpuid(result, 0x00000001);
const bool sse = (result[3] & ((int)1 << 25)) != 0;
const bool sse2 = (result[3] & ((int)1 << 26)) != 0;
const bool sse3 = (result[2] & ((int)1 << 0)) != 0;
const bool ssse3 = (result[2] & ((int)1 << 9)) != 0;
const bool sse41 = (result[2] & ((int)1 << 19)) != 0;
const bool sse42 = (result[2] & ((int)1 << 20)) != 0;
const bool fma3 = (result[2] & ((int)1 << 12)) != 0;
const bool os_uses_xsave_xrestore = (result[2] & ((int)1 << 27)) != 0;
const bool cpu_avx_support = (result[2] & ((int)1 << 28)) != 0;
/* Simplify to combined capabilities for which we specialize kernels. */
caps.sse42 = sse && sse2 && sse3 && ssse3 && sse41 && sse42;
if (os_uses_xsave_xrestore && cpu_avx_support) {
// Check if the OS will save the YMM registers
uint32_t xcr_feature_mask;
# if defined(__GNUC__)
int edx; /* not used */
/* actual opcode for xgetbv */
__asm__(".byte 0x0f, 0x01, 0xd0" : "=a"(xcr_feature_mask), "=d"(edx) : "c"(0));
# elif defined(_MSC_VER) && defined(_XCR_XFEATURE_ENABLED_MASK)
/* Minimum VS2010 SP1 compiler is required. */
xcr_feature_mask = (uint32_t)_xgetbv(_XCR_XFEATURE_ENABLED_MASK);
# else
xcr_feature_mask = 0;
# endif
const bool avx = (xcr_feature_mask & 0x6) == 0x6;
const bool f16c = (result[2] & ((int)1 << 29)) != 0;
caps.f16c = avx && f16c;
__cpuid(result, 0x00000007);
bool bmi1 = (result[1] & ((int)1 << 3)) != 0;
bool bmi2 = (result[1] & ((int)1 << 8)) != 0;
bool avx2 = (result[1] & ((int)1 << 5)) != 0;
caps.avx2 = sse && sse2 && sse3 && ssse3 && sse41 && sse42 && avx && f16c && avx2 &&
fma3 && bmi1 && bmi2;
}
}
caps_init = true;
}
return caps;
}
bool system_cpu_support_sse42()
{
CPUCapabilities &caps = system_cpu_capabilities();
return caps.sse42;
}
bool system_cpu_support_avx2()
{
/* F16C is considered part of AVX2 for our purpose, as all physical CPUs with
* AVX2 support also support it so there is no point having a separate kernel.
*
* Some cases where it might be missing is Rosetta or virtual machines, so we
* check for it just to be safe. */
CPUCapabilities &caps = system_cpu_capabilities();
return caps.avx2 && caps.f16c;
}
#else
bool system_cpu_support_sse42()
{
return false;
}
bool system_cpu_support_avx2()
{
return false;
}
#endif
size_t system_physical_ram()
{
#ifdef _WIN32
MEMORYSTATUSEX ram;
ram.dwLength = sizeof(ram);
GlobalMemoryStatusEx(&ram);
return ram.ullTotalPhys;
#elif defined(__APPLE__)
uint64_t ram = 0;
size_t len = sizeof(ram);
if (sysctlbyname("hw.memsize", &ram, &len, nullptr, 0) == 0) {
return ram;
}
return 0;
#else
size_t ps = sysconf(_SC_PAGESIZE);
size_t pn = sysconf(_SC_PHYS_PAGES);
return ps * pn;
#endif
}
uint64_t system_self_process_id()
{
#ifdef _WIN32
return GetCurrentProcessId();
#else
return getpid();
#endif
}
size_t system_max_open_files()
{
#if defined(_WIN32)
return _getmaxstdio();
#else
struct rlimit limit = {};
return (getrlimit(RLIMIT_NOFILE, &limit) == 0) ? limit.rlim_cur : SIZE_MAX;
#endif
}
void system_max_open_files_ensure()
{
/* The Windows maximum is 8192 open files. */
constexpr int max_open_files = 8192;
bool ok = true;
#if defined(_WIN32)
if (_getmaxstdio() < max_open_files) {
ok = _setmaxstdio(max_open_files) == max_open_files;
}
#else
struct rlimit limit = {};
ok = getrlimit(RLIMIT_NOFILE, &limit) == 0;
if (ok && limit.rlim_cur < rlim_t(max_open_files)) {
limit.rlim_cur = std::min(rlim_t(max_open_files), limit.rlim_max);
ok = setrlimit(RLIMIT_NOFILE, &limit) == 0;
}
#endif
if (!ok) {
LOG_DEBUG << "Failed to ensure max open files is at least " << max_open_files << ": "
<< strerror(errno);
}
}
CCL_NAMESPACE_END

View File

@@ -0,0 +1,33 @@
/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#pragma once
#include <cstdint>
#include <cstdlib>
#include <string>
CCL_NAMESPACE_BEGIN
/* Get width in characters of the current console output. */
int system_console_width();
std::string system_cpu_brand_string();
int system_cpu_bits();
bool system_cpu_support_sse42();
bool system_cpu_support_avx2();
size_t system_physical_ram();
/* Get identifier of the currently running process. */
uint64_t system_self_process_id();
size_t system_max_open_files();
/* Ensure the process can open many files simultaneously.
* This should be called once on application startup, as it is not thread safe. */
void system_max_open_files_ensure();
CCL_NAMESPACE_END

View File

@@ -0,0 +1,241 @@
/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#include "util/task.h"
#include "util/log.h"
#include "util/time.h"
CCL_NAMESPACE_BEGIN
/* Task Pool */
TaskPool::TaskPool() : start_time(time_dt()), num_tasks_pushed(0) {}
TaskPool::~TaskPool()
{
cancel();
}
void TaskPool::push(TaskRunFunction &&task)
{
tbb_group.run(std::move(task));
num_tasks_pushed++;
}
void TaskPool::wait_work(Summary *stats)
{
tbb_group.wait();
if (stats != nullptr) {
stats->time_total = time_dt() - start_time;
stats->num_tasks_handled = num_tasks_pushed;
}
num_tasks_pushed = 0;
}
void TaskPool::cancel()
{
if (num_tasks_pushed > 0) {
tbb_group.cancel();
tbb_group.wait();
num_tasks_pushed = 0;
}
}
bool TaskPool::canceled()
{
return tbb::is_current_task_group_canceling();
}
/* Task Scheduler */
thread_mutex TaskScheduler::mutex;
int TaskScheduler::users = 0;
int TaskScheduler::active_num_threads = 0;
unique_ptr<tbb::global_control> TaskScheduler::global_control;
void TaskScheduler::init(const int num_threads)
{
const thread_scoped_lock lock(mutex);
/* Multiple cycles instances can use this task scheduler, sharing the same
* threads, so we keep track of the number of users. */
++users;
if (users != 1) {
return;
}
if (num_threads > 0) {
/* Automatic number of threads. */
LOG_INFO << "Overriding number of TBB threads to " << num_threads << ".";
global_control = make_unique<tbb::global_control>(tbb::global_control::max_allowed_parallelism,
num_threads);
active_num_threads = num_threads;
}
else {
active_num_threads = tbb::this_task_arena::max_concurrency();
}
}
void TaskScheduler::exit()
{
const thread_scoped_lock lock(mutex);
users--;
if (users == 0) {
global_control.reset();
active_num_threads = 0;
}
}
void TaskScheduler::free_memory()
{
assert(users == 0);
}
int TaskScheduler::max_concurrency()
{
const thread_scoped_lock lock(mutex);
return (users > 0) ? active_num_threads : tbb::this_task_arena::max_concurrency();
}
/* Dedicated Task Pool */
DedicatedTaskPool::DedicatedTaskPool()
{
do_cancel = false;
do_exit = false;
num = 0;
worker_thread = make_unique<thread>([this] { thread_run(); });
}
DedicatedTaskPool::~DedicatedTaskPool()
{
wait();
do_exit = true;
queue_cond.notify_all();
worker_thread->join();
worker_thread.reset();
}
void DedicatedTaskPool::push(TaskRunFunction &&run, bool front)
{
num_increase();
/* add task to queue */
queue_mutex.lock();
if (front) {
queue.emplace_front(std::move(run));
}
else {
queue.emplace_back(std::move(run));
}
queue_cond.notify_one();
queue_mutex.unlock();
}
void DedicatedTaskPool::wait()
{
thread_scoped_lock num_lock(num_mutex);
while (num) {
num_cond.wait(num_lock);
}
}
void DedicatedTaskPool::cancel()
{
do_cancel = true;
clear();
wait();
do_cancel = false;
}
bool DedicatedTaskPool::canceled()
{
return do_cancel;
}
void DedicatedTaskPool::num_decrease(const int done)
{
const thread_scoped_lock num_lock(num_mutex);
num -= done;
assert(num >= 0);
if (num == 0) {
num_cond.notify_all();
}
}
void DedicatedTaskPool::num_increase()
{
const thread_scoped_lock num_lock(num_mutex);
num++;
num_cond.notify_all();
}
bool DedicatedTaskPool::thread_wait_pop(TaskRunFunction &task)
{
thread_scoped_lock queue_lock(queue_mutex);
while (queue.empty() && !do_exit) {
queue_cond.wait(queue_lock);
}
if (queue.empty()) {
assert(do_exit);
return false;
}
task = queue.front();
queue.pop_front();
return true;
}
void DedicatedTaskPool::thread_run()
{
TaskRunFunction task;
/* keep popping off tasks */
while (thread_wait_pop(task)) {
/* run task */
task();
/* delete task */
task = nullptr;
/* notify task was done */
num_decrease(1);
}
}
void DedicatedTaskPool::clear()
{
thread_scoped_lock queue_lock(queue_mutex);
/* erase all tasks from the queue */
const int done = queue.size();
queue.clear();
queue_lock.unlock();
/* notify done */
num_decrease(done);
}
string TaskPool::Summary::full_report() const
{
string report;
report += string_printf("Total time: %f\n", time_total);
report += string_printf("Tasks handled: %d", num_tasks_handled);
return report;
}
CCL_NAMESPACE_END

View File

@@ -0,0 +1,132 @@
/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#pragma once
#include "util/list.h"
#include "util/string.h"
#include "util/tbb.h"
#include "util/thread.h"
#include "util/unique_ptr.h"
CCL_NAMESPACE_BEGIN
class TaskPool;
class TaskScheduler;
using TaskRunFunction = std::function<void()>;
/* Task Pool
*
* Pool of tasks that will be executed by the central TaskScheduler. For each
* pool, we can wait for all tasks to be done, or cancel them before they are
* done.
*
* TaskRunFunction may be created with std::bind or lambda expressions. */
class TaskPool {
public:
struct Summary {
/* Time spent to handle all tasks. */
double time_total;
/* Number of all tasks handled by this pool. */
int num_tasks_handled;
/* A full multi-line description of the state of the pool after
* all work is done.
*/
string full_report() const;
};
TaskPool();
~TaskPool();
void push(TaskRunFunction &&task);
void wait_work(Summary *stats = nullptr); /* work and wait until all tasks are done */
void cancel(); /* cancel all tasks and wait until they are no longer executing */
static bool canceled(); /* For worker threads, test if current task pool canceled. */
protected:
tbb::task_group tbb_group;
/* ** Statistics ** */
/* Time stamp of first task pushed. */
double start_time;
/* Number of all tasks pushed to the pool. Cleared after wait_work() and cancel(). */
int num_tasks_pushed;
};
/**
* Task Scheduler
*
* Central scheduler that holds running threads ready to execute tasks. A single
* queue holds the task from all pools.
*/
class TaskScheduler {
public:
static void init(const int num_threads = 0);
static void exit();
static void free_memory();
/* Maximum number of threads that will work on task. Use as little as
* possible and leave scheduling and splitting up tasks to the scheduler. */
static int max_concurrency();
protected:
static thread_mutex mutex;
static int users;
static int active_num_threads;
#ifdef WITH_TBB_GLOBAL_CONTROL
static unique_ptr<tbb::global_control> global_control;
#endif
};
/* Dedicated Task Pool
*
* Like a TaskPool, but will launch one dedicated thread to execute all tasks.
*
* The run callback can be a lambda without arguments. */
class DedicatedTaskPool {
public:
DedicatedTaskPool();
~DedicatedTaskPool();
void push(TaskRunFunction &&run, bool front = false);
void wait(); /* wait until all tasks are done */
void cancel(); /* cancel all tasks, keep worker thread running */
bool canceled(); /* for worker thread, test if canceled */
protected:
void num_decrease(const int done);
void num_increase();
void thread_run();
bool thread_wait_pop(TaskRunFunction &task);
void clear();
thread_mutex num_mutex;
thread_condition_variable num_cond;
list<TaskRunFunction> queue;
thread_mutex queue_mutex;
thread_condition_variable queue_cond;
int num;
bool do_cancel;
bool do_exit;
unique_ptr<thread> worker_thread;
};
CCL_NAMESPACE_END

View File

@@ -0,0 +1,67 @@
/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#pragma once
/* TBB includes <windows.h>, do it ourselves first so we are sure
* WIN32_LEAN_AND_MEAN and similar are defined beforehand. */
#ifdef _WIN32
# include "util/windows.h"
#endif
#include <tbb/blocked_range2d.h>
#include <tbb/blocked_range3d.h>
#include <tbb/enumerable_thread_specific.h>
#include <tbb/parallel_for.h>
#include <tbb/parallel_for_each.h>
#include <tbb/parallel_reduce.h>
#include <tbb/task_arena.h>
#include <tbb/task_group.h>
#if TBB_INTERFACE_VERSION_MAJOR >= 10
# define WITH_TBB_GLOBAL_CONTROL
# define TBB_PREVIEW_GLOBAL_CONTROL 1
# include <tbb/global_control.h>
#endif
CCL_NAMESPACE_BEGIN
using tbb::blocked_range;
using tbb::blocked_range2d;
using tbb::blocked_range3d;
using tbb::enumerable_thread_specific;
using tbb::parallel_for;
using tbb::parallel_for_each;
using tbb::parallel_reduce;
static inline void thread_capture_fp_settings()
{
#if TBB_INTERFACE_VERSION_MAJOR >= 12
tbb::task_group_context *ctx = tbb::task::current_context();
#else
tbb::task_group_context *ctx = tbb::task::self().group();
#endif
if (ctx) {
ctx->capture_fp_settings();
}
}
static inline void parallel_for_cancel()
{
#if TBB_INTERFACE_VERSION_MAJOR >= 12
tbb::task_group_context *ctx = tbb::task::current_context();
if (ctx) {
ctx->cancel_group_execution();
}
#else
tbb::task::self().cancel_group_execution();
#endif
}
template<typename Function> static inline void isolate_task(const Function &function)
{
tbb::this_task_arena::isolate(function);
}
CCL_NAMESPACE_END

View File

@@ -0,0 +1,64 @@
/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#include "util/thread.h"
#if defined(__APPLE__) || defined(__linux__) && !defined(__GLIBC__)
#else
# include "util/system.h"
# ifdef _WIN32
# include "util/windows.h"
# endif
# include <system_error>
#endif
CCL_NAMESPACE_BEGIN
thread::thread(std::function<void()> run_cb) : run_cb_(run_cb), joined_(false)
{
#if defined(__APPLE__) || defined(__linux__) && !defined(__GLIBC__)
/* Set the stack size to 2MB to match GLIBC. The default 512KB on macOS is
* too small for Embree, and consistent stack size also makes things more
* predictable in general. */
pthread_attr_t attribute;
pthread_attr_init(&attribute);
pthread_attr_setstacksize(&attribute, 1024 * 1024 * 2);
pthread_create(&pthread_id, &attribute, run, (void *)this);
#else
std_thread = std::thread(&thread::run, this);
#endif
}
thread::~thread()
{
if (!joined_) {
join();
}
}
void *thread::run(void *arg)
{
thread *self = (thread *)(arg);
self->run_cb_();
return nullptr;
}
bool thread::join()
{
joined_ = true;
#if defined(__APPLE__) || defined(__linux__) && !defined(__GLIBC__)
return pthread_join(pthread_id, nullptr) == 0;
#else
try {
std_thread.join();
return true;
}
catch (const std::system_error &) {
return false;
}
#endif
}
CCL_NAMESPACE_END

View File

@@ -0,0 +1,73 @@
/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#pragma once
#include <condition_variable>
#include <functional>
#include <mutex>
#if defined(__APPLE__) || defined(__linux__) && !defined(__GLIBC__)
# include <pthread.h>
#else
# include <thread>
#endif
#ifdef _WIN32
# include "util/windows.h"
#endif
/* NOTE: Use tbb/spin_mutex.h instead of util_tbb.h because some of the TBB
* functionality requires RTTI, which is disabled for OSL kernel. */
#include <tbb/spin_mutex.h>
CCL_NAMESPACE_BEGIN
using thread_mutex = std::mutex;
using thread_scoped_lock = std::unique_lock<std::mutex>;
using thread_condition_variable = std::condition_variable;
/**
* Own thread implementation similar to std::thread, so we can set a
* custom stack size on macOS.
*/
class thread {
public:
thread(std::function<void()> run_cb);
~thread();
static void *run(void *arg);
bool join();
protected:
std::function<void()> run_cb_;
#if defined(__APPLE__) || defined(__linux__) && !defined(__GLIBC__)
pthread_t pthread_id;
#else
std::thread std_thread;
#endif
bool joined_;
};
using thread_spin_lock = tbb::spin_mutex;
class thread_scoped_spin_lock {
public:
explicit thread_scoped_spin_lock(thread_spin_lock &lock) : lock_(lock)
{
lock_.lock();
}
~thread_scoped_spin_lock()
{
lock_.unlock();
}
/* TODO(sergey): Implement manual control over lock/unlock. */
protected:
thread_spin_lock &lock_;
};
CCL_NAMESPACE_END

View File

@@ -0,0 +1,192 @@
/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#include "util/time.h"
#include <chrono>
#include <cstdlib>
#if !defined(_WIN32)
# include <sys/time.h>
# include <unistd.h>
#endif
#include "util/string.h"
#ifdef _WIN32
# include "util/windows.h"
#endif
#if defined(__x86_64__) || defined(_M_X64) || defined(__i386__) || defined(_M_IX86)
# ifdef _MSC_VER
# include <intrin.h>
# else
# include <x86intrin.h>
# endif
#endif
CCL_NAMESPACE_BEGIN
double time_dt()
{
return std::chrono::duration<double>(std::chrono::steady_clock::now().time_since_epoch())
.count();
}
#ifdef _WIN32
void time_sleep(const double t)
{
Sleep((int)(t * 1000));
}
#else
/* sleep t seconds */
void time_sleep(double t)
{
/* get whole seconds */
const int s = (int)t;
if (s >= 1) {
sleep(s);
/* adjust parameter to remove whole seconds */
t -= s;
}
/* get microseconds */
const int us = (int)(t * 1e6);
if (us > 0) {
usleep(us);
}
}
#endif
#if defined(__aarch64__) || defined(_M_ARM64)
/* Use cntvct_el0/cntfrq_el0 registers on ARM64. */
uint64_t time_fast_tick(uint32_t * /*last_cpu*/)
{
/* MSVC does not define __aarch64__, or support inline ASM */
# if !defined(__aarch64__)
return _ReadStatusReg(ARM64_CNTVCT_EL0);
# else
uint64_t counter;
asm("mrs %x0, cntvct_el0" : "=r"(counter));
return counter;
# endif
}
uint64_t time_fast_frequency()
{
/* MSVC does not define __aarch64__, or support inline ASM */
# if !defined(__aarch64__)
return _ReadStatusReg(ARM64_CNTFRQ_EL0);
# else
uint64_t freq;
asm("mrs %x0, cntfrq_el0" : "=r"(freq));
return freq;
# endif
}
#elif defined(__x86_64__) || defined(_M_X64)
/* Use RDTSCP on x86-64. */
uint64_t time_fast_tick(uint32_t *last_cpu)
{
return __rdtscp(last_cpu);
}
uint64_t time_fast_frequency()
{
static bool initialized = false;
static uint64_t frequency;
/* Unfortunately TSC does not provide a easily accessible frequency value, so roughly calibrate
* by sleeping a millisecond. Not ideal, but good enough for our purposes. */
if (!initialized) {
uint32_t cpu;
uint64_t start_tick = time_fast_tick(&cpu);
double start_precise = time_dt();
time_sleep(0.001);
uint64_t end_tick = time_fast_tick(&cpu);
double end_precise = time_dt();
frequency = uint64_t(double(end_tick - start_tick) / (end_precise - start_precise));
initialized = true;
}
return frequency;
}
#else
/* Fall back to std::chrono::steady_clock. */
uint64_t time_fast_tick(uint32_t * /*last_cpu*/)
{
auto now = std::chrono::steady_clock::now();
auto nanoseconds = std::chrono::time_point_cast<std::chrono::nanoseconds>(now);
return nanoseconds.time_since_epoch().count();
}
uint64_t time_fast_frequency()
{
return 1000000000;
}
#endif
/* Time in format "hours:minutes:seconds.hundreds" */
string time_human_readable_from_seconds(const double seconds)
{
const int h = (((int)seconds) / (60 * 60));
const int m = (((int)seconds) / 60) % 60;
const int s = (((int)seconds) % 60);
const int r = (((int)(seconds * 100)) % 100);
if (h > 0) {
return string_printf("%.2d:%.2d:%.2d.%.2d", h, m, s, r);
}
return string_printf("%.2d:%.2d.%.2d", m, s, r);
}
double time_human_readable_to_seconds(const string &time_string)
{
/* Those are multiplies of a corresponding token surrounded by : in the
* time string, which denotes how to convert value to seconds.
* Effectively: seconds, minutes, hours, days in seconds. */
const int multipliers[] = {1, 60, 60 * 60, 24 * 60 * 60};
const int num_multiplies = sizeof(multipliers) / sizeof(*multipliers);
if (time_string.empty()) {
return 0.0;
}
double result = 0.0;
/* Split fractions of a second from the encoded time. */
vector<string> fraction_tokens;
string_split(fraction_tokens, time_string, ".", false);
const int num_fraction_tokens = fraction_tokens.size();
if (num_fraction_tokens == 0) {
/* Time string is malformed. */
return 0.0;
}
if (fraction_tokens.size() == 1) {
/* There is no fraction of a second specified, the rest of the code
* handles this normally. */
}
else if (fraction_tokens.size() == 2) {
result = atof(fraction_tokens[1].c_str());
result *= ::pow(0.1, fraction_tokens[1].length());
}
else {
/* This is not a valid string, the result can not be reliable. */
return 0.0;
}
/* Split hours, minutes and seconds.
* Hours part is optional. */
vector<string> tokens;
string_split(tokens, fraction_tokens[0], ":", false);
const int num_tokens = tokens.size();
if (num_tokens > num_multiplies) {
/* Can not reliably represent the value. */
return 0.0;
}
for (int i = 0; i < num_tokens; ++i) {
result += atoi(tokens[num_tokens - i - 1].c_str()) * multipliers[i];
}
return result;
}
CCL_NAMESPACE_END

View File

@@ -0,0 +1,112 @@
/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#pragma once
#include <functional>
#include "util/string.h"
CCL_NAMESPACE_BEGIN
/* Give current time in seconds in double precision, with good accuracy. */
double time_dt();
/* Sleep for the specified number of seconds. */
void time_sleep(const double t);
/* Fast timer for applications where overhead is critical and some inaccuracy is acceptable.
*
* On x86, this uses RDTSCP, which also can check which CPU the code runs on, which in turn
* allows us to skip measurements where we moved CPU in-between (which might be invalid due
* to different clock states between cores and/or misleading due to OS scheduling). Therefore,
* we provide last_cpu to time_fast_tick, and it may set it if supported. */
uint64_t time_fast_tick(uint32_t *last_cpu);
uint64_t time_fast_frequency();
/* Scoped timer. */
class scoped_timer {
public:
explicit scoped_timer(double *value = nullptr) : value_(value)
{
time_start_ = time_dt();
}
~scoped_timer()
{
if (value_ != nullptr) {
*value_ = get_time();
}
}
double get_start() const
{
return time_start_;
}
double get_time() const
{
return time_dt() - time_start_;
}
protected:
double *value_;
double time_start_;
};
class fast_timer {
public:
fast_timer()
{
last_cpu = 0;
last_value = time_fast_tick(&last_cpu);
}
bool lap(uint64_t &delta)
{
uint32_t new_cpu = 0;
uint64_t new_value = time_fast_tick(&new_cpu);
const bool cpu_consistent = new_cpu == last_cpu;
delta = new_value - last_value;
last_cpu = new_cpu;
last_value = new_value;
return cpu_consistent;
}
protected:
uint32_t last_cpu;
uint64_t last_value;
};
class scoped_callback_timer {
public:
using callback_type = std::function<void(double)>;
explicit scoped_callback_timer(callback_type cb) : cb(cb) {}
~scoped_callback_timer()
{
if (cb) {
cb(timer.get_time());
}
}
protected:
scoped_timer timer;
callback_type cb;
};
/* Make human readable string from time, compatible with Blender metadata. */
string time_human_readable_from_seconds(const double seconds);
double time_human_readable_to_seconds(const string &time_string);
CCL_NAMESPACE_END

View File

@@ -0,0 +1,205 @@
/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#include "util/transform.h"
#include "util/projection.h"
#include "util/boundbox.h"
#include "util/math.h"
CCL_NAMESPACE_BEGIN
/* Transform Inverse */
Transform transform_transposed_inverse(const Transform &tfm)
{
const ProjectionTransform iprojection(transform_inverse(tfm));
return projection_to_transform(projection_transpose(iprojection));
}
/* Motion Transform */
float4 transform_to_quat(const Transform &tfm)
{
const double trace = (double)(tfm[0][0] + tfm[1][1] + tfm[2][2]);
float4 qt;
if (trace > 0.0) {
double s = sqrt(trace + 1.0);
qt.w = (float)(s / 2.0);
s = 0.5 / s;
qt.x = (float)((double)(tfm[2][1] - tfm[1][2]) * s);
qt.y = (float)((double)(tfm[0][2] - tfm[2][0]) * s);
qt.z = (float)((double)(tfm[1][0] - tfm[0][1]) * s);
}
else {
int i = 0;
if (tfm[1][1] > tfm[i][i]) {
i = 1;
}
if (tfm[2][2] > tfm[i][i]) {
i = 2;
}
const int j = (i + 1) % 3;
const int k = (j + 1) % 3;
double s = sqrt((double)(tfm[i][i] - (tfm[j][j] + tfm[k][k])) + 1.0);
double q[3];
q[i] = s * 0.5;
if (s != 0.0) {
s = 0.5 / s;
}
const double w = (double)(tfm[k][j] - tfm[j][k]) * s;
q[j] = (double)(tfm[j][i] + tfm[i][j]) * s;
q[k] = (double)(tfm[k][i] + tfm[i][k]) * s;
qt.x = (float)q[0];
qt.y = (float)q[1];
qt.z = (float)q[2];
qt.w = (float)w;
}
return qt;
}
static void transform_decompose(DecomposedTransform *decomp, const Transform *tfm)
{
/* extract translation */
decomp->y = make_float4(tfm->x.w, tfm->y.w, tfm->z.w, 0.0f);
/* extract rotation */
Transform M = *tfm;
M.x.w = 0.0f;
M.y.w = 0.0f;
M.z.w = 0.0f;
#if 0
Transform R = M;
float norm;
int iteration = 0;
do {
Transform Rnext;
Transform Rit = transform_transposed_inverse(R);
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
Rnext[i][j] = 0.5f * (R[i][j] + Rit[i][j]);
}
}
norm = 0.0f;
for (int i = 0; i < 3; i++) {
norm = max(norm,
fabsf(R[i][0] - Rnext[i][0]) + fabsf(R[i][1] - Rnext[i][1]) +
fabsf(R[i][2] - Rnext[i][2]));
}
R = Rnext;
iteration++;
} while (iteration < 100 && norm > 1e-4f);
if (transform_negative_scale(R)) {
R = R * transform_scale(-1.0f, -1.0f, -1.0f);
}
decomp->x = transform_to_quat(R);
/* extract scale and pack it */
Transform scale = transform_inverse(R) * M;
decomp->y.w = scale.x.x;
decomp->z = make_float4(scale.x.y, scale.x.z, scale.y.x, scale.y.y);
decomp->w = make_float4(scale.y.z, scale.z.x, scale.z.y, scale.z.z);
#else
float3 colx = transform_get_column(&M, 0);
float3 coly = transform_get_column(&M, 1);
float3 colz = transform_get_column(&M, 2);
/* extract scale and shear first */
float3 scale;
float3 shear;
scale.x = len(colx);
colx = safe_divide(colx, scale.x);
shear.z = dot(colx, coly);
coly -= shear.z * colx;
scale.y = len(coly);
coly = safe_divide(coly, scale.y);
shear.y = dot(colx, colz);
colz -= shear.y * colx;
shear.x = dot(coly, colz);
colz -= shear.x * coly;
scale.z = len(colz);
colz = safe_divide(colz, scale.z);
transform_set_column(&M, 0, colx);
transform_set_column(&M, 1, coly);
transform_set_column(&M, 2, colz);
if (transform_negative_scale(M)) {
scale *= -1.0f;
M = M * transform_scale(-1.0f, -1.0f, -1.0f);
}
decomp->x = transform_to_quat(M);
decomp->y.w = scale.x;
decomp->z = make_float4(shear.z, shear.y, 0.0f, scale.y);
decomp->w = make_float4(shear.x, 0.0f, 0.0f, scale.z);
#endif
}
void transform_motion_decompose(DecomposedTransform *decomp,
const Transform *motion,
const size_t size)
{
/* Decompose and correct rotation. */
for (size_t i = 0; i < size; i++) {
transform_decompose(decomp + i, motion + i);
if (i > 0) {
/* Ensure rotation around shortest angle, negated quaternions are the same
* but this means we don't have to do the check in quat_interpolate */
if (dot(decomp[i - 1].x, decomp[i].x) < 0.0f) {
decomp[i].x = -decomp[i].x;
}
}
}
/* Copy rotation to decomposed transform where scale is degenerate. This avoids weird object
* rotation interpolation when the scale goes to 0 for a time step.
*
* Note that this is very simple and naive implementation, which only deals with degenerated
* scale happening only on one frame. It is possible to improve it further by interpolating
* rotation into s degenerated range using rotation from time-steps from adjacent non-degenerated
* time steps. */
for (size_t i = 0; i < size; i++) {
const float3 scale = make_float3(decomp[i].y.w, decomp[i].z.w, decomp[i].w.w);
if (!is_zero(scale)) {
continue;
}
if (i > 0) {
decomp[i].x = decomp[i - 1].x;
}
else if (i < size - 1) {
decomp[i].x = decomp[i + 1].x;
}
}
}
Transform transform_from_viewplane(BoundBox2D &viewplane)
{
return transform_scale(1.0f / (viewplane.right - viewplane.left),
1.0f / (viewplane.top - viewplane.bottom),
1.0f) *
transform_translate(-viewplane.left, -viewplane.bottom, 0.0f);
}
CCL_NAMESPACE_END

View File

@@ -0,0 +1,730 @@
/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#pragma once
#ifndef __KERNEL_GPU__
# include <cstring>
#endif
#include "util/math.h"
#include "util/types.h"
#ifndef __KERNEL_GPU__
# include "util/system.h"
#endif
CCL_NAMESPACE_BEGIN
/* Transform p from a local coordinate system (spanned by X and Y) into global coordinates. */
template<class T> ccl_device_inline T to_global(const float2 p, const T X, const T Y)
{
return p.x * X + p.y * Y;
}
/* Transform p from a local coordinate system (spanned by X, Y and Z) into global coordinates. */
template<class T> ccl_device_inline T to_global(const float3 p, const T X, const T Y, const T Z)
{
return p.x * X + p.y * Y + p.z * Z;
}
/* Transform p from global coordinates into a local coordinate system (spanned by X and Y). */
template<class T> ccl_device_inline float2 to_local(const T p, const T X, const T Y)
{
return make_float2(dot(p, X), dot(p, Y));
}
/* Transform p from global coordinates into a local coordinate system (spanned by X, Y and Z). */
template<class T> ccl_device_inline float3 to_local(const T p, const T X, const T Y, const T Z)
{
return make_float3(dot(p, X), dot(p, Y), dot(p, Z));
}
template<class T>
ccl_device_inline dual3 to_local(const dual<T> p, const T X, const T Y, const T Z)
{
return make_float3(dot(p, X), dot(p, Y), dot(p, Z));
}
/* Affine transformation, stored as 4x3 matrix. */
struct Transform {
float4 x, y, z;
#ifndef __KERNEL_GPU__
float4 operator[](int i) const
{
return *(&x + i);
}
float4 &operator[](int i)
{
return *(&x + i);
}
#endif
};
/* Transform decomposed in rotation/translation/scale. we use the same data
* structure as Transform, and tightly pack decomposition into it. first the
* rotation (4), then translation (3), then 3x3 scale matrix (9). */
struct DecomposedTransform {
float4 x, y, z, w;
};
/* Functions */
#ifdef __KERNEL_METAL__
/* transform_point specialized for ccl_global */
ccl_device_inline float3 transform_point(const ccl_global Transform *t, const float3 a)
{
const ccl_global float3x3 &b(*(const ccl_global float3x3 *)t);
return (a * b).xyz + make_float3(t->x.w, t->y.w, t->z.w);
}
#endif
ccl_device_inline float3 transform_point(const ccl_private Transform *t, const float3 a)
{
#if defined(__KERNEL_SSE__) && defined(__KERNEL_SSE2__)
const float4 aa(a.m128);
float4 x = t->x;
float4 y = t->y;
float4 z = t->z;
float4 w(_mm_set_ps(1.0f, 0.0f, 0.0f, 0.0f));
_MM_TRANSPOSE4_PS(x.m128, y.m128, z.m128, w.m128);
float4 tmp = w;
tmp = madd(shuffle<2>(aa), z, tmp);
tmp = madd(shuffle<1>(aa), y, tmp);
tmp = madd(shuffle<0>(aa), x, tmp);
return float3(tmp.m128);
#elif defined(__KERNEL_METAL__)
const ccl_private float3x3 &b(*(const ccl_private float3x3 *)t);
return (a * b).xyz + make_float3(t->x.w, t->y.w, t->z.w);
#else
const float4 a_ = make_homogeneous(a);
return make_float3(dot(a_, t->x), dot(a_, t->y), dot(a_, t->z));
#endif
}
ccl_device_inline dual3 transform_point(const ccl_private Transform *t, const dual3 a)
{
#if defined(__KERNEL_SSE__) && defined(__KERNEL_SSE2__)
/* NOTE: `dot()` has large latency on Intel platforms, the following method of transpose + madd
* is faster. However, we did not measure on Neon platforms, it might be that `dot()` is fine
* there, and we can use the simpler implementation at the end of the function. */
float4 x = t->x;
float4 y = t->y;
float4 z = t->z;
float4 w(_mm_set_ps(1.0f, 0.0f, 0.0f, 0.0f));
_MM_TRANSPOSE4_PS(x.m128, y.m128, z.m128, w.m128);
float4 tmp = w;
dual3 result;
{
const float4 aa(a.val.m128);
tmp = madd(shuffle<2>(aa), z, tmp);
tmp = madd(shuffle<1>(aa), y, tmp);
tmp = madd(shuffle<0>(aa), x, tmp);
result.val = float3(tmp.m128);
}
{
const float4 dx(a.dx.m128);
tmp = shuffle<2>(dx) * z;
tmp = madd(shuffle<1>(dx), y, tmp);
tmp = madd(shuffle<0>(dx), x, tmp);
result.dx = float3(tmp.m128);
}
{
const float4 dy(a.dy.m128);
tmp = shuffle<2>(dy) * z;
tmp = madd(shuffle<1>(dy), y, tmp);
tmp = madd(shuffle<0>(dy), x, tmp);
result.dy = float3(tmp.m128);
}
return result;
#elif defined(__KERNEL_METAL__)
const ccl_private float3x3 &b(*(const ccl_private float3x3 *)t);
return {(a.val * b).xyz + make_float3(t->x.w, t->y.w, t->z.w), (a.dx * b).xyz, (a.dy * b).xyz};
#else
const dual4 a_ = make_homogeneous(a);
return make_float3(dot(a_, t->x), dot(a_, t->y), dot(a_, t->z));
#endif
}
ccl_device_inline float3 transform_direction(const ccl_private Transform *t, const float3 a)
{
#if defined(__KERNEL_SSE__) && defined(__KERNEL_SSE2__)
const float4 aa(a.m128);
float4 x = t->x;
float4 y = t->y;
float4 z = t->z;
float4 w(_mm_setzero_ps());
_MM_TRANSPOSE4_PS(x.m128, y.m128, z.m128, w.m128);
float4 tmp = shuffle<2>(aa) * z;
tmp = madd(shuffle<1>(aa), y, tmp);
tmp = madd(shuffle<0>(aa), x, tmp);
return float3(tmp.m128);
#elif defined(__KERNEL_METAL__)
const ccl_private float3x3 &b(*(const ccl_private float3x3 *)t);
return (a * b).xyz;
#else
const float4 a_ = make_float4(a, 0.0f);
return make_float3(dot(a_, t->x), dot(a_, t->y), dot(a_, t->z));
#endif
}
ccl_device_inline dual3 transform_direction(const ccl_private Transform *t, const dual3 a)
{
return to_local(a, make_float3(t->x), make_float3(t->y), make_float3(t->z));
}
template<class T>
ccl_device_inline T transform_direction_transposed(const ccl_private Transform *t, const T a)
{
const float3 x = make_float3(t->x.x, t->y.x, t->z.x);
const float3 y = make_float3(t->x.y, t->y.y, t->z.y);
const float3 z = make_float3(t->x.z, t->y.z, t->z.z);
return to_local(a, x, y, z);
}
ccl_device_inline Transform make_transform(const float a,
const float b,
const float c,
const float d,
const float e,
const float f,
const float g,
const float h,
const float i,
const float j,
const float k,
const float l)
{
Transform t;
t.x.x = a;
t.x.y = b;
t.x.z = c;
t.x.w = d;
t.y.x = e;
t.y.y = f;
t.y.z = g;
t.y.w = h;
t.z.x = i;
t.z.y = j;
t.z.z = k;
t.z.w = l;
return t;
}
ccl_device_inline Transform make_transform(const float3 x, const float3 y, const float3 z)
{
Transform t;
t.x = make_float4(x, 0.0f);
t.y = make_float4(y, 0.0f);
t.z = make_float4(z, 0.0f);
return t;
}
ccl_device_inline Transform euler_to_transform(const float3 euler)
{
const float cx = cosf(euler.x);
const float cy = cosf(euler.y);
const float cz = cosf(euler.z);
const float sx = sinf(euler.x);
const float sy = sinf(euler.y);
const float sz = sinf(euler.z);
Transform t;
t.x.x = cy * cz;
t.y.x = cy * sz;
t.z.x = -sy;
t.x.y = sy * sx * cz - cx * sz;
t.y.y = sy * sx * sz + cx * cz;
t.z.y = cy * sx;
t.x.z = sy * cx * cz + sx * sz;
t.y.z = sy * cx * sz - sx * cz;
t.z.z = cy * cx;
t.x.w = t.y.w = t.z.w = 0.0f;
return t;
}
/* Constructs a coordinate frame from a normalized normal. */
ccl_device_inline Transform make_transform_frame(const float3 N)
{
const float3 dx0 = cross(make_float3(1.0f, 0.0f, 0.0f), N);
const float3 dx1 = cross(make_float3(0.0f, 1.0f, 0.0f), N);
const float3 dx = normalize((dot(dx0, dx0) > dot(dx1, dx1)) ? dx0 : dx1);
const float3 dy = normalize(cross(N, dx));
return make_transform(dx.x, dx.y, dx.z, 0.0f, dy.x, dy.y, dy.z, 0.0f, N.x, N.y, N.z, 0.0f);
}
#if !defined(__KERNEL_METAL__)
ccl_device_inline Transform operator*(const Transform a, const Transform b)
{
const float4 c_x = make_float4(b.x.x, b.y.x, b.z.x, 0.0f);
const float4 c_y = make_float4(b.x.y, b.y.y, b.z.y, 0.0f);
const float4 c_z = make_float4(b.x.z, b.y.z, b.z.z, 0.0f);
const float4 c_w = make_float4(b.x.w, b.y.w, b.z.w, 1.0f);
Transform t;
t.x = make_float4(dot(a.x, c_x), dot(a.x, c_y), dot(a.x, c_z), dot(a.x, c_w));
t.y = make_float4(dot(a.y, c_x), dot(a.y, c_y), dot(a.y, c_z), dot(a.y, c_w));
t.z = make_float4(dot(a.z, c_x), dot(a.z, c_y), dot(a.z, c_z), dot(a.z, c_w));
return t;
}
#endif
ccl_device_inline Transform transform_zero()
{
Transform zero = {zero_float4(), zero_float4(), zero_float4()};
return zero;
}
#ifndef __KERNEL_GPU__
ccl_device_inline void print_transform(const char *label, const Transform &t)
{
print_float4(label, t.x);
print_float4(label, t.y);
print_float4(label, t.z);
printf("\n");
}
ccl_device_inline Transform transform_translate(const float3 t)
{
return make_transform(1, 0, 0, t.x, 0, 1, 0, t.y, 0, 0, 1, t.z);
}
ccl_device_inline Transform transform_translate(const float x, const float y, float z)
{
return transform_translate(make_float3(x, y, z));
}
ccl_device_inline void transform_translate(Transform &T, const float3 t)
{
T.x.w += t.x;
T.y.w += t.y;
T.z.w += t.z;
}
ccl_device_inline Transform transform_scale(const float3 s)
{
return make_transform(s.x, 0, 0, 0, 0, s.y, 0, 0, 0, 0, s.z, 0);
}
ccl_device_inline Transform transform_scale(const float x, const float y, float z)
{
return transform_scale(make_float3(x, y, z));
}
ccl_device_inline Transform transform_rotate(const float angle, float3 axis)
{
const float s = sinf(angle);
const float c = cosf(angle);
const float t = 1.0f - c;
axis = normalize(axis);
return make_transform(axis.x * axis.x * t + c,
axis.x * axis.y * t - s * axis.z,
axis.x * axis.z * t + s * axis.y,
0.0f,
axis.y * axis.x * t + s * axis.z,
axis.y * axis.y * t + c,
axis.y * axis.z * t - s * axis.x,
0.0f,
axis.z * axis.x * t - s * axis.y,
axis.z * axis.y * t + s * axis.x,
axis.z * axis.z * t + c,
0.0f);
}
/* Euler is assumed to be in XYZ order. */
ccl_device_inline Transform transform_euler(const float3 euler)
{
return transform_rotate(euler.z, make_float3(0.0f, 0.0f, 1.0f)) *
transform_rotate(euler.y, make_float3(0.0f, 1.0f, 0.0f)) *
transform_rotate(euler.x, make_float3(1.0f, 0.0f, 0.0f));
}
ccl_device_inline Transform transform_identity()
{
return transform_scale(1.0f, 1.0f, 1.0f);
}
ccl_device_inline bool operator==(const Transform &A, const Transform &B)
{
/* Using memcmp because it returns true for NaN unlike component ==,
* which we need for set_if_different for node sockets to copy the value. */
return memcmp(&A, &B, sizeof(Transform)) == 0;
}
ccl_device_inline bool operator!=(const Transform &A, const Transform &B)
{
return !(A == B);
}
ccl_device_inline bool transform_equal_threshold(const Transform &A,
const Transform &B,
const float threshold)
{
for (int x = 0; x < 3; x++) {
for (int y = 0; y < 4; y++) {
if (fabsf(A[x][y] - B[x][y]) > threshold) {
return false;
}
}
}
return true;
}
ccl_device_inline float3 transform_get_column(const Transform *t, const int column)
{
return make_float3(t->x[column], t->y[column], t->z[column]);
}
ccl_device_inline void transform_set_column(Transform *t, const int column, const float3 value)
{
t->x[column] = value.x;
t->y[column] = value.y;
t->z[column] = value.z;
}
ccl_device_inline float3 transform_get_translation(const Transform *t)
{
return transform_get_column(t, 3);
}
Transform transform_transposed_inverse(const Transform &tfm);
ccl_device_inline bool transform_uniform_scale(const Transform &tfm, float &scale)
{
/* the epsilon here is quite arbitrary, but this function is only used for
* surface area and bump, where we expect it to not be so sensitive */
const float eps = 1e-6f;
const float sx = len_squared(make_float3(tfm.x));
const float sy = len_squared(make_float3(tfm.y));
const float sz = len_squared(make_float3(tfm.z));
const float stx = len_squared(transform_get_column(&tfm, 0));
const float sty = len_squared(transform_get_column(&tfm, 1));
const float stz = len_squared(transform_get_column(&tfm, 2));
if (fabsf(sx - sy) < eps && fabsf(sx - sz) < eps && fabsf(sx - stx) < eps &&
fabsf(sx - sty) < eps && fabsf(sx - stz) < eps)
{
scale = sx;
return true;
}
return false;
}
ccl_device_inline bool transform_negative_scale(const Transform &tfm)
{
const float3 c0 = transform_get_column(&tfm, 0);
const float3 c1 = transform_get_column(&tfm, 1);
const float3 c2 = transform_get_column(&tfm, 2);
return (dot(cross(c0, c1), c2) < 0.0f);
}
ccl_device_inline Transform transform_clear_scale(const Transform &tfm)
{
Transform ntfm = tfm;
transform_set_column(&ntfm, 0, normalize(transform_get_column(&ntfm, 0)));
transform_set_column(&ntfm, 1, normalize(transform_get_column(&ntfm, 1)));
transform_set_column(&ntfm, 2, normalize(transform_get_column(&ntfm, 2)));
return ntfm;
}
ccl_device_inline Transform transform_empty()
{
return make_transform(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
}
#endif
/* Motion Transform */
ccl_device_inline float4 quat_interpolate(const float4 q1, const float4 q2, const float t)
{
/* Optix and MetalRT are using linear interpolation to interpolate motion transformations. */
#if defined(__KERNEL_GPU_RAYTRACING__)
return normalize((1.0f - t) * q1 + t * q2);
#else /* defined(__KERNEL_GPU_RAYTRACING__) */
/* NOTE: this does not ensure rotation around shortest angle, q1 and q2
* are assumed to be matched already in transform_motion_decompose */
const float costheta = dot(q1, q2);
/* possible optimization: it might be possible to precompute theta/qperp */
if (costheta > 0.9995f) {
/* linear interpolation in degenerate case */
return normalize((1.0f - t) * q1 + t * q2);
}
/* slerp */
const float theta = acosf(clamp(costheta, -1.0f, 1.0f));
const float4 qperp = normalize(q2 - q1 * costheta);
const float thetap = theta * t;
return q1 * cosf(thetap) + qperp * sinf(thetap);
#endif /* defined(__KERNEL_GPU_RAYTRACING__) */
}
#ifndef __KERNEL_GPU__
void transform_inverse_cpu_avx2(const Transform &tfm, Transform &itfm);
#endif
/* Custom cross and dot implementations that match Embree bit for bit.
* Normally we don't use SSE41/AVX outside the kernel, but for this it's
* important to match exactly for ray tracing precision. */
ccl_device_forceinline float3 transform_inverse_cross(const float3 a_, const float3 b_)
{
#if defined(__AVX2__) && defined(__KERNEL_SSE2__)
const __m128 a = (const __m128 &)a_;
const __m128 b = (const __m128 &)b_;
const __m128 a_shuffle = _mm_castsi128_ps(
_mm_shuffle_epi32(_mm_castps_si128(a), _MM_SHUFFLE(3, 0, 2, 1)));
const __m128 b_shuffle = _mm_castsi128_ps(
_mm_shuffle_epi32(_mm_castps_si128(b), _MM_SHUFFLE(3, 0, 2, 1)));
const __m128 r = _mm_castsi128_ps(
_mm_shuffle_epi32(_mm_castps_si128(_mm_fmsub_ps(a, b_shuffle, _mm_mul_ps(a_shuffle, b))),
_MM_SHUFFLE(3, 0, 2, 1)));
return (const float3 &)r;
#endif
return cross(a_, b_);
}
ccl_device_forceinline float transform_inverse_dot(const float3 a_, const float3 b_)
{
#if defined(__KERNEL_SSE__) && defined(__KERNEL_SSE42__)
const __m128 a = (const __m128 &)a_;
const __m128 b = (const __m128 &)b_;
return _mm_cvtss_f32(_mm_dp_ps(a, b, 0x7F));
#endif
return dot(a_, b_);
}
ccl_device_forceinline Transform transform_inverse_impl(const Transform tfm)
{
/* This implementation matches the one in Embree exactly, to ensure consistent
* results with the ray intersection of instances. */
float3 x = make_float3(tfm.x.x, tfm.y.x, tfm.z.x);
float3 y = make_float3(tfm.x.y, tfm.y.y, tfm.z.y);
float3 z = make_float3(tfm.x.z, tfm.y.z, tfm.z.z);
const float3 w = make_float3(tfm.x.w, tfm.y.w, tfm.z.w);
/* Compute determinant. */
float det = transform_inverse_dot(x, transform_inverse_cross(y, z));
if (det == 0.0f) {
/* Matrix is degenerate (e.g. 0 scale on some axis), ideally we should
* never be in this situation, but try to invert it anyway with tweak.
*
* This logic does not match Embree which would just give an invalid
* matrix. A better solution would be to remove this and ensure any object
* matrix is valid. */
x.x += 1e-8f;
y.y += 1e-8f;
z.z += 1e-8f;
det = transform_inverse_dot(x, cross(y, z));
if (det == 0.0f) {
det = FLT_MAX;
}
}
/* Divide adjoint matrix by the determinant to compute inverse of 3x3 matrix. */
const float3 inverse_x = transform_inverse_cross(y, z) / det;
const float3 inverse_y = transform_inverse_cross(z, x) / det;
const float3 inverse_z = transform_inverse_cross(x, y) / det;
/* Compute translation and fill transform. */
Transform itfm;
itfm.x = make_float4(inverse_x, -transform_inverse_dot(inverse_x, w));
itfm.y = make_float4(inverse_y, -transform_inverse_dot(inverse_y, w));
itfm.z = make_float4(inverse_z, -transform_inverse_dot(inverse_z, w));
return itfm;
}
ccl_device_inline Transform transform_inverse(const Transform tfm)
{
/* Optimized transform implementations. */
#ifndef __KERNEL_GPU__
if (system_cpu_support_avx2()) {
Transform itfm;
transform_inverse_cpu_avx2(tfm, itfm);
return itfm;
}
#endif
return transform_inverse_impl(tfm);
}
ccl_device_inline void transform_compose(ccl_private Transform *tfm,
const ccl_private DecomposedTransform *decomp)
{
/* rotation */
float q0;
float q1;
float q2;
float q3;
float qda;
float qdb;
float qdc;
float qaa;
float qab;
float qac;
float qbb;
float qbc;
float qcc;
q0 = M_SQRT2_F * decomp->x.w;
q1 = M_SQRT2_F * decomp->x.x;
q2 = M_SQRT2_F * decomp->x.y;
q3 = M_SQRT2_F * decomp->x.z;
qda = q0 * q1;
qdb = q0 * q2;
qdc = q0 * q3;
qaa = q1 * q1;
qab = q1 * q2;
qac = q1 * q3;
qbb = q2 * q2;
qbc = q2 * q3;
qcc = q3 * q3;
const float3 rotation_x = make_float3(1.0f - qbb - qcc, -qdc + qab, qdb + qac);
const float3 rotation_y = make_float3(qdc + qab, 1.0f - qaa - qcc, -qda + qbc);
const float3 rotation_z = make_float3(-qdb + qac, qda + qbc, 1.0f - qaa - qbb);
/* scale */
const float3 scale_x = make_float3(decomp->y.w, decomp->z.z, decomp->w.y);
const float3 scale_y = make_float3(decomp->z.x, decomp->z.w, decomp->w.z);
const float3 scale_z = make_float3(decomp->z.y, decomp->w.x, decomp->w.w);
/* compose with translation */
tfm->x = make_float4(
dot(rotation_x, scale_x), dot(rotation_x, scale_y), dot(rotation_x, scale_z), decomp->y.x);
tfm->y = make_float4(
dot(rotation_y, scale_x), dot(rotation_y, scale_y), dot(rotation_y, scale_z), decomp->y.y);
tfm->z = make_float4(
dot(rotation_z, scale_x), dot(rotation_z, scale_y), dot(rotation_z, scale_z), decomp->y.z);
}
/* Interpolate from array of decomposed transforms. */
ccl_device void transform_motion_array_interpolate(ccl_private Transform *tfm,
const ccl_global DecomposedTransform *motion,
const int numsteps,
const float time)
{
/* Figure out which steps we need to interpolate. */
const int maxstep = numsteps - 1;
const int step = min((int)(time * maxstep), maxstep - 1);
const float t = time * maxstep - step;
const ccl_global DecomposedTransform *a = motion + step;
const ccl_global DecomposedTransform *b = motion + step + 1;
/* Interpolate rotation, translation and scale. */
DecomposedTransform decomp;
decomp.x = quat_interpolate(a->x, b->x, t);
decomp.y = (1.0f - t) * a->y + t * b->y;
decomp.z = (1.0f - t) * a->z + t * b->z;
decomp.w = (1.0f - t) * a->w + t * b->w;
/* Compose rotation, translation, scale into matrix. */
transform_compose(tfm, &decomp);
}
ccl_device_inline bool transform_isfinite_safe(ccl_private Transform *tfm)
{
return isfinite_safe(tfm->x) && isfinite_safe(tfm->y) && isfinite_safe(tfm->z);
}
ccl_device_inline bool transform_decomposed_isfinite_safe(ccl_private DecomposedTransform *decomp)
{
return isfinite_safe(decomp->x) && isfinite_safe(decomp->y) && isfinite_safe(decomp->z) &&
isfinite_safe(decomp->w);
}
#ifndef __KERNEL_GPU__
class BoundBox2D;
ccl_device_inline bool operator==(const DecomposedTransform &A, const DecomposedTransform &B)
{
return memcmp(&A, &B, sizeof(DecomposedTransform)) == 0;
}
float4 transform_to_quat(const Transform &tfm);
void transform_motion_decompose(DecomposedTransform *decomp,
const Transform *motion,
const size_t size);
Transform transform_from_viewplane(BoundBox2D &viewplane);
#endif
/* Packed Transform.
*
* Transform type with no alignment requirements.
* It does not support any mathematical operations, only conversion to Transform. */
struct PackedTransform {
PackedTransform() = default;
ccl_device_inline_method PackedTransform(const Transform a) : x(a.x), y(a.y), z(a.z) {}
ccl_device_inline_method PackedTransform operator=(const Transform a)
{
x = a.x;
y = a.y;
z = a.z;
return *this;
}
packed_float4 x, y, z;
};
static_assert(alignof(PackedTransform) == alignof(float),
"PackedTransform expected to have the same alignment as float");
static_assert(sizeof(PackedTransform) == 48, "packed_float4 expected to be exactly 48 bytes");
ccl_device_inline Transform make_transform(const PackedTransform packed_tfm)
{
Transform tfm;
tfm.x = packed_tfm.x;
tfm.y = packed_tfm.y;
tfm.z = packed_tfm.z;
return tfm;
}
/* TODO: This can be removed when we know if no devices will require explicit
* address space qualifiers for this case. */
#define transform_point_auto transform_point
#define transform_direction_auto transform_direction
#define transform_direction_transposed_auto transform_direction_transposed
CCL_NAMESPACE_END

View File

@@ -0,0 +1,14 @@
/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#include "util/transform.h"
CCL_NAMESPACE_BEGIN
void transform_inverse_cpu_avx2(const Transform &tfm, Transform &itfm)
{
itfm = transform_inverse_impl(tfm);
}
CCL_NAMESPACE_END

View File

@@ -0,0 +1,34 @@
/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#pragma once
#include "util/types_base.h" // IWYU pragma: export
#include "util/types_uchar2.h" // IWYU pragma: export
#include "util/types_uchar3.h" // IWYU pragma: export
#include "util/types_uchar4.h" // IWYU pragma: export
#include "util/types_int2.h" // IWYU pragma: export
#include "util/types_int3.h" // IWYU pragma: export
#include "util/types_int4.h" // IWYU pragma: export
#include "util/types_int8.h" // IWYU pragma: export
#include "util/types_uint2.h" // IWYU pragma: export
#include "util/types_uint3.h" // IWYU pragma: export
#include "util/types_uint4.h" // IWYU pragma: export
#include "util/types_ushort4.h" // IWYU pragma: export
#include "util/types_float2.h" // IWYU pragma: export
#include "util/types_float3.h" // IWYU pragma: export
#include "util/types_float4.h" // IWYU pragma: export
#include "util/types_float8.h" // IWYU pragma: export
#include "util/types_normal.h" // IWYU pragma: export
#include "util/types_rgbe.h" // IWYU pragma: export
#include "util/types_spectrum.h" // IWYU pragma: export
#include "util/types_dual.h" // IWYU pragma: export

View File

@@ -0,0 +1,105 @@
/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#pragma once
#if !defined(__KERNEL_METAL__)
# include <cstdlib>
#endif
/* Standard Integer Types */
#if !defined(__KERNEL_GPU__)
# include <cstdint> // IWYU pragma: export
# include <cstdio>
#endif
#include "util/defines.h"
#ifndef __KERNEL_GPU__
# include "util/optimization.h" // IWYU pragma: export
# include "util/simd.h" // IWYU pragma: export
#endif
CCL_NAMESPACE_BEGIN
/* Types
*
* Define simpler unsigned type names, and integer with defined number of bits.
* Also vector types, named to be compatible with OpenCL builtin types, while
* working for CUDA and C++ too. */
/* Shorter Unsigned Names */
using uchar = unsigned char;
using uint = unsigned int;
using ushort = unsigned short;
/* Fixed Bits Types */
#ifndef __KERNEL_GPU__
/* Generic Memory Pointer */
using device_ptr = uint64_t;
#endif /* __KERNEL_GPU__ */
/* Make offset a multiple of alignment, rounding up. Alignment must be a power of two. */
ccl_device_inline size_t align_up(const size_t offset, const size_t alignment)
{
return (offset + alignment - 1) & ~(alignment - 1);
}
/* Divide x by y, rounding up. */
ccl_device_inline size_t divide_up(const size_t x, const size_t y)
{
return (x + y - 1) / y;
}
/* Divide x by (1 << y), rounding up. */
ccl_device_inline size_t divide_up_by_shift(const size_t x, const size_t y)
{
return (x + ((1 << y) - 1)) >> y;
}
/* Make x a multiple of another number, rounding up. */
ccl_device_inline size_t round_up(const size_t x, const size_t multiple)
{
return ((x + multiple - 1) / multiple) * multiple;
}
/* Make x a multiple of another number, rounding down. */
ccl_device_inline size_t round_down(const size_t x, const size_t multiple)
{
return (x / multiple) * multiple;
}
/* Test if x is a power of two. */
ccl_device_inline bool is_power_of_two(const size_t x)
{
return (x & (x - 1)) == 0;
}
CCL_NAMESPACE_END
/* Device side printf only tested on CUDA, may work on more GPU devices. */
#if !defined(__KERNEL_GPU__) || defined(__KERNEL_CUDA__)
# define __KERNEL_PRINTF__
#endif
#if defined __METAL_PRINTF__
# define print_float(label, a) metal::os_log_default.log_debug(label ": %.8f", a)
#else
ccl_device_inline void print_float(const ccl_private char *label, const float a)
{
# ifdef __KERNEL_PRINTF__
printf("%s: %.8f\n", label, (double)a);
# endif
}
#endif
/* Most GPU APIs matching native vector types, so we only need to implement them for
* CPU and oneAPI. */
#if defined(__KERNEL_GPU__) && !defined(__KERNEL_ONEAPI__)
# define __KERNEL_NATIVE_VECTOR_TYPES__
#endif

View File

@@ -0,0 +1,233 @@
/* SPDX-FileCopyrightText: 2025 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#pragma once
#include "util/types_float2.h"
#include "util/types_float3.h"
#include "util/types_float4.h"
CCL_NAMESPACE_BEGIN
template<class T> struct dual {
T val = T(), dx = T(), dy = T();
dual() = default;
ccl_device_inline_method explicit dual(const T val) : val(val) {}
ccl_device_inline_method dual(const T val, const T dx, const T dy) : val(val), dx(dx), dy(dy) {}
};
template<> struct dual<float> {
float val = 0.0f;
float dx = 0.0f;
float dy = 0.0f;
dual() = default;
ccl_device_inline_method explicit dual(const float val) : val(val) {}
ccl_device_inline_method dual(const float val, const float dx, const float dy)
: val(val), dx(dx), dy(dy)
{
}
};
template<> struct dual<float2> {
float2 val = make_float2(0.0f);
float2 dx = make_float2(0.0f);
float2 dy = make_float2(0.0f);
dual() = default;
ccl_device_inline_method explicit dual(const float2 val) : val(val) {}
ccl_device_inline_method dual(const float2 val, const float2 dx, const float2 dy)
: val(val), dx(dx), dy(dy)
{
}
ccl_device_inline_method dual<float> x() const
{
return {val.x, dx.x, dy.x};
}
ccl_device_inline_method dual<float> y() const
{
return {val.y, dx.y, dy.y};
}
};
template<> struct dual<float3> {
float3 val = make_float3(0.0f);
float3 dx = make_float3(0.0f);
float3 dy = make_float3(0.0f);
dual() = default;
ccl_device_inline_method explicit dual(const float3 val) : val(val) {}
ccl_device_inline_method dual(const float3 val, const float3 dx, const float3 dy)
: val(val), dx(dx), dy(dy)
{
}
ccl_device_inline_method dual<float> x() const
{
return {val.x, dx.x, dy.x};
}
ccl_device_inline_method dual<float> y() const
{
return {val.y, dx.y, dy.y};
}
ccl_device_inline_method dual<float> z() const
{
return {val.z, dx.z, dy.z};
}
};
template<> struct dual<float4> {
float4 val = make_float4(0.0f);
float4 dx = make_float4(0.0f);
float4 dy = make_float4(0.0f);
dual() = default;
ccl_device_inline_method explicit dual(const float4 val) : val(val) {}
ccl_device_inline_method dual(const float4 val, const float4 dx, const float4 dy)
: val(val), dx(dx), dy(dy)
{
}
ccl_device_inline_method dual<float> x() const
{
return {val.x, dx.x, dy.x};
}
ccl_device_inline_method dual<float> y() const
{
return {val.y, dx.y, dy.y};
}
ccl_device_inline_method dual<float> z() const
{
return {val.z, dx.z, dy.z};
}
ccl_device_inline_method dual<float> w() const
{
return {val.w, dx.w, dy.w};
}
};
using dual1 = dual<float>;
using dual2 = dual<float2>;
using dual3 = dual<float3>;
using dual4 = dual<float4>;
/* Dual type traits. */
template<typename T> struct is_dual {
ccl_static_constexpr bool value = false;
};
template<typename U> struct is_dual<dual<U>> {
ccl_static_constexpr bool value = true;
};
template<typename T> ccl_static_constexpr bool is_dual_v = is_dual<T>::value;
/* Base (non-dual) type. E.g. dual_base_t<dual3> = float3, dual_base_t<float3> = float3. */
template<typename T> struct dual_base_type {
using type = T;
};
template<typename U> struct dual_base_type<dual<U>> {
using type = U;
};
template<typename T> using dual_base_t = typename dual_base_type<T>::type;
/* Scalar type corresponding to a vector type. */
template<typename T> struct dual_scalar_type {
using type = T;
};
template<> struct dual_scalar_type<float3> {
using type = float;
};
template<> struct dual_scalar_type<dual3> {
using type = dual1;
};
template<typename T> using dual_scalar_t = typename dual_scalar_type<T>::type;
ccl_device_inline dual2 make_float2(const dual3 a)
{
return {make_float2(a.val), make_float2(a.dx), make_float2(a.dy)};
}
ccl_device_inline dual2 make_float2(const dual1 a, const dual1 b)
{
return {make_float2(a.val, b.val), make_float2(a.dx, b.dx), make_float2(a.dy, b.dy)};
}
ccl_device_inline dual3 make_float3(const dual1 a, const dual1 b, const dual1 c)
{
return {make_float3(a.val, b.val, c.val),
make_float3(a.dx, b.dx, c.dx),
make_float3(a.dy, b.dy, c.dy)};
}
template<class T> ccl_device_inline dual3 make_float3(const ccl_private dual<T> &a)
{
return {make_float3(a.val), make_float3(a.dx), make_float3(a.dy)};
}
ccl_device_inline dual4 make_float4(const dual1 a, const dual1 b, const dual1 c, const dual1 d)
{
return {make_float4(a.val, b.val, c.val, d.val),
make_float4(a.dx, b.dx, c.dx, d.dx),
make_float4(a.dy, b.dy, c.dy, d.dy)};
}
ccl_device_inline dual4 make_float4(const dual3 a)
{
return {make_float4(a.val), make_float4(a.dx, 0.0f), make_float4(a.dy, 0.0f)};
}
ccl_device_inline dual4 make_homogeneous(const dual3 a)
{
return {make_float4(a.val, 1.0f), make_float4(a.dx, 0.0f), make_float4(a.dy, 0.0f)};
}
ccl_device_inline void print_dual1(const ccl_private char *label, const dual1 a)
{
#ifdef __KERNEL_PRINTF__
printf("%s: {\nval = %.8f\n dx = %.8f\n dy = %.8f\n}\n",
label,
(double)a.val,
(double)a.dx,
(double)a.dy);
#else
(void)label;
(void)a;
#endif
}
ccl_device_inline void print_dual2(const ccl_private char *label, const dual2 a)
{
#ifdef __KERNEL_PRINTF__
printf("%s: {\nval = %.8f %.8f\n dx = %.8f %.8f\n dy = %.8f %.8f\n}\n",
label,
(double)a.val.x,
(double)a.val.y,
(double)a.dx.x,
(double)a.dx.y,
(double)a.dy.x,
(double)a.dy.y);
#else
(void)label;
(void)a;
#endif
}
ccl_device_inline void print_dual3(const ccl_private char *label, const dual3 a)
{
#ifdef __KERNEL_PRINTF__
printf("%s: {\nval = %.8f %.8f %.8f\n dx = %.8f %.8f %.8f\n dy = %.8f %.8f %.8f\n}\n",
label,
(double)a.val.x,
(double)a.val.y,
(double)a.val.z,
(double)a.dx.x,
(double)a.dx.y,
(double)a.dx.z,
(double)a.dy.x,
(double)a.dy.y,
(double)a.dy.z);
#else
(void)label;
(void)a;
#endif
}
CCL_NAMESPACE_END

View File

@@ -0,0 +1,71 @@
/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#pragma once
#include "util/types_base.h"
#include "util/types_int2.h"
CCL_NAMESPACE_BEGIN
#ifndef __KERNEL_NATIVE_VECTOR_TYPES__
struct float2 {
float x, y;
# ifndef __KERNEL_GPU__
__forceinline float operator[](int i) const;
__forceinline float &operator[](int i);
# endif
};
# ifndef __KERNEL_GPU__
__forceinline float float2::operator[](int i) const
{
util_assert(i >= 0);
util_assert(i < 2);
return *(&x + i);
}
__forceinline float &float2::operator[](int i)
{
util_assert(i >= 0);
util_assert(i < 2);
return *(&x + i);
}
# endif
ccl_device_inline float2 make_float2(const float x, const float y)
{
float2 a = {x, y};
return a;
}
#endif /* __KERNEL_NATIVE_VECTOR_TYPES__ */
ccl_device_inline float2 make_float2(const float f)
{
return {f, f};
}
ccl_device_inline float2 make_float2(const int2 i)
{
return make_float2((float)i.x, (float)i.y);
}
ccl_device_inline int2 make_int2(const float2 f)
{
return make_int2((int)f.x, (int)f.y);
}
#if defined __METAL_PRINTF__
# define print_float2(label, a) metal::os_log_default.log_debug(label ": %.8f %.8f", (a).x, (a).y)
#else
ccl_device_inline void print_float2(const ccl_private char *label, const float2 a)
{
# ifdef __KERNEL_PRINTF__
printf("%s: %.8f %.8f\n", label, (double)a.x, (double)a.y);
# endif
}
#endif
CCL_NAMESPACE_END

View File

@@ -0,0 +1,200 @@
/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#pragma once
#include "util/types_base.h"
#include "util/types_float2.h"
#include "util/types_int3.h"
#include "util/types_int4.h"
CCL_NAMESPACE_BEGIN
#ifndef __KERNEL_NATIVE_VECTOR_TYPES__
# ifdef __KERNEL_ONEAPI__
/* Define float3 as packed for oneAPI. */
struct float3
# else
struct ccl_try_align(16) float3
# endif
{
# ifdef __KERNEL_GPU__
/* Compact structure for GPU. */
float x, y, z;
# else
/* SIMD aligned structure for CPU. */
# ifdef __KERNEL_SSE__
union {
__m128 m128;
struct {
float x, y, z, w;
};
};
# else
float x, y, z, w;
# endif
# endif
# ifdef __KERNEL_SSE__
/* Convenient constructors and operators for SIMD, otherwise default is enough. */
__forceinline float3() = default;
__forceinline float3(const float3 &a) = default;
__forceinline explicit float3(const __m128 &a) : m128(a) {}
__forceinline operator const __m128 &() const
{
return m128;
}
__forceinline operator __m128 &()
{
return m128;
}
__forceinline float3 &operator=(const float3 &a)
{
m128 = a.m128;
return *this;
}
# endif
# ifndef __KERNEL_GPU__
__forceinline float operator[](int i) const
{
util_assert(i >= 0);
util_assert(i < 3);
return *(&x + i);
}
__forceinline float &operator[](int i)
{
util_assert(i >= 0);
util_assert(i < 3);
return *(&x + i);
}
# endif
};
ccl_device_inline float3 make_float3(const float x, const float y, float z)
{
# if defined(__KERNEL_GPU__)
return {x, y, z};
# elif defined(__KERNEL_SSE__)
return float3(_mm_set_ps(0.0f, z, y, x));
# else
return {x, y, z, 0.0f};
# endif
}
#endif /* __KERNEL_NATIVE_VECTOR_TYPES__ */
ccl_device_inline float3 make_float3(const float f)
{
#if defined(__KERNEL_GPU__)
return make_float3(f, f, f);
#elif defined(__KERNEL_SSE__)
return float3(_mm_set1_ps(f));
#else
return {f, f, f, f};
#endif
}
ccl_device_inline float3 make_float3(const float2 a)
{
return make_float3(a.x, a.y, 0.0f);
}
ccl_device_inline float3 make_float3(const float2 a, const float b)
{
return make_float3(a.x, a.y, b);
}
ccl_device_inline float3 make_float3(const int3 i)
{
#ifdef __KERNEL_SSE__
return float3(_mm_cvtepi32_ps(i.m128));
#else
return make_float3((float)i.x, (float)i.y, (float)i.z);
#endif
}
ccl_device_inline float3 make_float3(const float3 a)
{
return a;
}
#if defined __METAL_PRINTF__
# define print_float3(label, a) \
metal::os_log_default.log_debug(label ": %.8f %.8f %.8f", (a).x, (a).y, (a).z)
#else
ccl_device_inline void print_float3(const ccl_private char *label, const float3 a)
{
# ifdef __KERNEL_PRINTF__
printf("%s: %.8f %.8f %.8f\n", label, (double)a.x, (double)a.y, (double)a.z);
# else
(void)label;
(void)a;
# endif
}
#endif
ccl_device_inline float2 make_float2(const float3 a)
{
return make_float2(a.x, a.y);
}
ccl_device_inline int4 make_int4(const float3 f)
{
#if defined(__KERNEL_GPU__)
return make_int4((int)f.x, (int)f.y, (int)f.z, 0);
#elif defined(__KERNEL_SSE__)
return int4(_mm_cvtps_epi32(f.m128));
#else
return make_int4((int)f.x, (int)f.y, (int)f.z, (int)f.w);
#endif
}
ccl_device_inline int3 make_int3(const float3 f)
{
#ifdef __KERNEL_SSE__
return int3(_mm_cvtps_epi32(f.m128));
#else
return make_int3((int)f.x, (int)f.y, (int)f.z);
#endif
}
/* Packed float3
*
* Smaller float3 for storage. For math operations this must be converted to float3, so that on the
* CPU SIMD instructions can be used. */
#if defined(__KERNEL_METAL__)
/* Metal has native packed_float3. */
#elif defined(__KERNEL_CUDA__) || defined(__KERNEL_HIP__) || defined(__KERNEL_ONEAPI__)
/* CUDA, HIP and oneAPI float3 are already packed. */
using packed_float3 = float3;
#else
struct packed_float3 {
ccl_device_inline_method packed_float3() = default;
ccl_device_inline_method packed_float3(const float3 &a) : x(a.x), y(a.y), z(a.z) {}
ccl_device_inline_method operator float3() const
{
return make_float3(x, y, z);
}
ccl_device_inline_method packed_float3 &operator=(const float3 &a)
{
x = a.x;
y = a.y;
z = a.z;
return *this;
}
float x, y, z;
};
#endif
static_assert(sizeof(packed_float3) == 12, "packed_float3 expected to be exactly 12 bytes");
CCL_NAMESPACE_END

Some files were not shown because too many files have changed in this diff Show More