2023-06-14 16:52:36 +10:00
|
|
|
/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
|
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: Apache-2.0 */
|
2018-11-09 11:54:24 +01:00
|
|
|
|
2024-12-26 17:53:56 +01:00
|
|
|
#pragma once
|
2018-11-09 11:54:24 +01:00
|
|
|
|
|
|
|
|
#include <cassert>
|
|
|
|
|
#include <cstring>
|
|
|
|
|
|
2021-10-24 14:19:19 +02:00
|
|
|
#include "util/aligned_malloc.h"
|
|
|
|
|
#include "util/vector.h"
|
2018-11-09 11:54:24 +01:00
|
|
|
|
|
|
|
|
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 */
|
|
|
|
|
|
2025-01-01 18:15:54 +01:00
|
|
|
template<typename T, const size_t alignment = MIN_ALIGNMENT_CPU_DATA_TYPES> class array {
|
2018-11-09 11:54:24 +01:00
|
|
|
public:
|
2024-12-26 17:53:55 +01:00
|
|
|
array() : data_(nullptr), datasize_(0), capacity_(0) {}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2025-01-01 18:15:54 +01:00
|
|
|
explicit array(const size_t newsize)
|
2018-11-09 11:54:24 +01:00
|
|
|
{
|
|
|
|
|
if (newsize == 0) {
|
2024-12-26 17:53:55 +01:00
|
|
|
data_ = nullptr;
|
2018-11-09 11:54:24 +01:00
|
|
|
datasize_ = 0;
|
|
|
|
|
capacity_ = 0;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
data_ = mem_allocate(newsize);
|
|
|
|
|
datasize_ = newsize;
|
|
|
|
|
capacity_ = datasize_;
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2018-11-09 11:54:24 +01:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-11-09 11:54:24 +01:00
|
|
|
array(const array &from)
|
|
|
|
|
{
|
|
|
|
|
if (from.datasize_ == 0) {
|
2024-12-26 17:53:55 +01:00
|
|
|
data_ = nullptr;
|
2018-11-09 11:54:24 +01:00
|
|
|
datasize_ = 0;
|
|
|
|
|
capacity_ = 0;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
data_ = mem_allocate(from.datasize_);
|
2019-06-21 16:24:56 +02:00
|
|
|
if (from.datasize_ > 0) {
|
2022-01-19 11:59:51 +01:00
|
|
|
mem_copy(data_, from.data_, from.datasize_);
|
2019-06-21 16:24:56 +02:00
|
|
|
}
|
2018-11-09 11:54:24 +01:00
|
|
|
datasize_ = from.datasize_;
|
|
|
|
|
capacity_ = datasize_;
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2018-11-09 11:54:24 +01:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2025-03-10 13:01:05 +01:00
|
|
|
array(array &&from)
|
|
|
|
|
{
|
|
|
|
|
data_ = from.data_;
|
|
|
|
|
datasize_ = from.datasize_;
|
|
|
|
|
capacity_ = from.capacity_;
|
|
|
|
|
|
|
|
|
|
from.data_ = nullptr;
|
|
|
|
|
from.datasize_ = 0;
|
|
|
|
|
from.capacity_ = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-09 11:54:24 +01:00
|
|
|
array &operator=(const array &from)
|
|
|
|
|
{
|
|
|
|
|
if (this != &from) {
|
|
|
|
|
resize(from.size());
|
2019-06-21 16:24:56 +02:00
|
|
|
if (datasize_ > 0) {
|
2022-01-19 11:59:51 +01:00
|
|
|
mem_copy(data_, from.data_, datasize_);
|
2019-06-21 16:24:56 +02:00
|
|
|
}
|
2018-11-09 11:54:24 +01:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-11-09 11:54:24 +01:00
|
|
|
return *this;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-11-09 11:54:24 +01:00
|
|
|
array &operator=(const vector<T> &from)
|
|
|
|
|
{
|
|
|
|
|
resize(from.size());
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-06-21 16:24:56 +02:00
|
|
|
if (from.size() > 0 && datasize_ > 0) {
|
2022-01-19 11:59:51 +01:00
|
|
|
mem_copy(data_, from.data(), datasize_);
|
2018-11-09 11:54:24 +01:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-11-09 11:54:24 +01:00
|
|
|
return *this;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-11-09 11:54:24 +01:00
|
|
|
~array()
|
|
|
|
|
{
|
|
|
|
|
mem_free(data_, capacity_);
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-11-09 11:54:24 +01:00
|
|
|
bool operator==(const array<T> &other) const
|
|
|
|
|
{
|
|
|
|
|
if (datasize_ != other.datasize_) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2019-06-21 16:24:56 +02:00
|
|
|
if (datasize_ == 0) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-11-09 11:54:24 +01:00
|
|
|
return memcmp(data_, other.data_, datasize_ * sizeof(T)) == 0;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-11-09 11:54:24 +01:00
|
|
|
bool operator!=(const array<T> &other) const
|
|
|
|
|
{
|
|
|
|
|
return !(*this == other);
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-11-09 11:54:24 +01:00
|
|
|
void steal_data(array &from)
|
|
|
|
|
{
|
|
|
|
|
if (this != &from) {
|
|
|
|
|
clear();
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-11-09 11:54:24 +01:00
|
|
|
data_ = from.data_;
|
|
|
|
|
datasize_ = from.datasize_;
|
|
|
|
|
capacity_ = from.capacity_;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2024-12-26 17:53:55 +01:00
|
|
|
from.data_ = nullptr;
|
2018-11-09 11:54:24 +01:00
|
|
|
from.datasize_ = 0;
|
|
|
|
|
from.capacity_ = 0;
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2018-11-09 11:54:24 +01:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
Cycles: optimize device updates
This optimizes device updates (during user edits or frame changes in
the viewport) by avoiding unnecessary computations. To achieve this,
we use a combination of the sockets' update flags as well as some new
flags passed to the various managers when tagging for an update to tell
exactly what the tagging is for (e.g. shader was modified, object was
removed, etc.).
Besides avoiding recomputations, we also avoid resending to the devices
unmodified data arrays, thus reducing bandwidth usage. For OptiX and
Embree, BVH packing was also multithreaded.
The performance improvements may vary depending on the used device (CPU
or GPU), and the content of the scene. Simple scenes (e.g. with no adaptive
subdivision or volumes) rendered using OptiX will benefit from this work
the most.
On average, for a variety of animated scenes, this gives a 3x speedup.
Reviewed By: #cycles, brecht
Maniphest Tasks: T79174
Differential Revision: https://developer.blender.org/D9555
2021-01-22 15:01:26 +01:00
|
|
|
void set_data(T *ptr_, size_t datasize)
|
|
|
|
|
{
|
|
|
|
|
clear();
|
|
|
|
|
data_ = ptr_;
|
|
|
|
|
datasize_ = datasize;
|
|
|
|
|
capacity_ = datasize;
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-09 11:54:24 +01:00
|
|
|
T *steal_pointer()
|
|
|
|
|
{
|
|
|
|
|
T *ptr = data_;
|
2024-12-26 17:53:55 +01:00
|
|
|
data_ = nullptr;
|
2018-11-09 11:54:24 +01:00
|
|
|
clear();
|
|
|
|
|
return ptr;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2025-01-01 18:15:54 +01:00
|
|
|
T *resize(const size_t newsize)
|
2018-11-09 11:54:24 +01:00
|
|
|
{
|
|
|
|
|
if (newsize == 0) {
|
|
|
|
|
clear();
|
|
|
|
|
}
|
|
|
|
|
else if (newsize != datasize_) {
|
|
|
|
|
if (newsize > capacity_) {
|
|
|
|
|
T *newdata = mem_allocate(newsize);
|
2024-12-26 17:53:55 +01:00
|
|
|
if (newdata == nullptr) {
|
2018-11-09 11:54:24 +01:00
|
|
|
/* Allocation failed, likely out of memory. */
|
|
|
|
|
clear();
|
2024-12-26 17:53:55 +01:00
|
|
|
return nullptr;
|
2018-11-09 11:54:24 +01:00
|
|
|
}
|
2024-12-26 17:53:59 +01:00
|
|
|
if (data_ != nullptr) {
|
2022-01-19 11:59:51 +01:00
|
|
|
mem_copy(newdata, data_, ((datasize_ < newsize) ? datasize_ : newsize));
|
2018-11-09 11:54:24 +01:00
|
|
|
mem_free(data_, capacity_);
|
|
|
|
|
}
|
|
|
|
|
data_ = newdata;
|
|
|
|
|
capacity_ = newsize;
|
|
|
|
|
}
|
|
|
|
|
datasize_ = newsize;
|
|
|
|
|
}
|
|
|
|
|
return data_;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2025-01-01 18:15:54 +01:00
|
|
|
T *resize(const size_t newsize, const T &value)
|
2018-11-09 11:54:24 +01:00
|
|
|
{
|
|
|
|
|
size_t oldsize = size();
|
|
|
|
|
resize(newsize);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-11-09 11:54:24 +01:00
|
|
|
for (size_t i = oldsize; i < size(); i++) {
|
|
|
|
|
data_[i] = value;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-11-09 11:54:24 +01:00
|
|
|
return data_;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-11-09 11:54:24 +01:00
|
|
|
void clear()
|
|
|
|
|
{
|
2024-12-26 17:53:55 +01:00
|
|
|
if (data_ != nullptr) {
|
2018-11-09 11:54:24 +01:00
|
|
|
mem_free(data_, capacity_);
|
2024-12-26 17:53:55 +01:00
|
|
|
data_ = nullptr;
|
2018-11-09 11:54:24 +01:00
|
|
|
}
|
|
|
|
|
datasize_ = 0;
|
|
|
|
|
capacity_ = 0;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-11-09 11:54:24 +01:00
|
|
|
size_t empty() const
|
|
|
|
|
{
|
|
|
|
|
return datasize_ == 0;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-11-09 11:54:24 +01:00
|
|
|
size_t size() const
|
|
|
|
|
{
|
|
|
|
|
return datasize_;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-11-09 11:54:24 +01:00
|
|
|
T *data()
|
|
|
|
|
{
|
|
|
|
|
return data_;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-11-09 11:54:24 +01:00
|
|
|
const T *data() const
|
|
|
|
|
{
|
|
|
|
|
return data_;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-11-09 11:54:24 +01:00
|
|
|
T &operator[](size_t i) const
|
|
|
|
|
{
|
|
|
|
|
assert(i < datasize_);
|
|
|
|
|
return data_[i];
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2020-11-04 11:17:38 +01:00
|
|
|
T *begin()
|
|
|
|
|
{
|
|
|
|
|
return data_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const T *begin() const
|
|
|
|
|
{
|
|
|
|
|
return data_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
T *end()
|
|
|
|
|
{
|
|
|
|
|
return data_ + datasize_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const T *end() const
|
|
|
|
|
{
|
|
|
|
|
return data_ + datasize_;
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-01 18:15:54 +01:00
|
|
|
void reserve(const size_t newcapacity)
|
2018-11-09 11:54:24 +01:00
|
|
|
{
|
|
|
|
|
if (newcapacity > capacity_) {
|
|
|
|
|
T *newdata = mem_allocate(newcapacity);
|
2024-12-26 17:53:55 +01:00
|
|
|
if (data_ != nullptr) {
|
2022-01-19 11:59:51 +01:00
|
|
|
mem_copy(newdata, data_, ((datasize_ < newcapacity) ? datasize_ : newcapacity));
|
2018-11-09 11:54:24 +01:00
|
|
|
mem_free(data_, capacity_);
|
|
|
|
|
}
|
|
|
|
|
data_ = newdata;
|
|
|
|
|
capacity_ = newcapacity;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-11-09 11:54:24 +01:00
|
|
|
size_t capacity() const
|
|
|
|
|
{
|
|
|
|
|
return capacity_;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-11-09 11:54:24 +01:00
|
|
|
// 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));
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-11-09 11:54:24 +01:00
|
|
|
data_[datasize_++] = t;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-11-09 11:54:24 +01:00
|
|
|
void push_back_reserved(const T &t)
|
|
|
|
|
{
|
|
|
|
|
assert(datasize_ < capacity_);
|
|
|
|
|
push_back_slow(t);
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-11-09 11:54:24 +01:00
|
|
|
void append(const array<T> &from)
|
|
|
|
|
{
|
|
|
|
|
if (from.size()) {
|
|
|
|
|
size_t old_size = size();
|
|
|
|
|
resize(old_size + from.size());
|
2022-01-19 11:59:51 +01:00
|
|
|
mem_copy(data_ + old_size, from.data(), from.size());
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2018-11-09 11:54:24 +01:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-11-09 11:54:24 +01:00
|
|
|
protected:
|
2025-01-01 18:15:54 +01:00
|
|
|
T *mem_allocate(const size_t N)
|
2018-11-09 11:54:24 +01:00
|
|
|
{
|
|
|
|
|
if (N == 0) {
|
2024-12-26 17:53:55 +01:00
|
|
|
return nullptr;
|
2018-11-09 11:54:24 +01:00
|
|
|
}
|
|
|
|
|
T *mem = (T *)util_aligned_malloc(sizeof(T) * N, alignment);
|
2025-01-09 12:04:08 +01:00
|
|
|
if (mem == nullptr) {
|
2018-11-09 11:54:24 +01:00
|
|
|
throw std::bad_alloc();
|
|
|
|
|
}
|
|
|
|
|
return mem;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2025-01-01 18:15:54 +01:00
|
|
|
void mem_free(T *mem, const size_t N)
|
2018-11-09 11:54:24 +01:00
|
|
|
{
|
2024-12-26 17:53:55 +01:00
|
|
|
if (mem != nullptr) {
|
2025-01-09 12:04:08 +01:00
|
|
|
util_aligned_free(mem, sizeof(T) * N);
|
2018-11-09 11:54:24 +01:00
|
|
|
}
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2024-12-26 17:53:59 +01:00
|
|
|
void mem_copy(T *mem_to, const T *mem_from, const size_t N)
|
2022-01-19 11:59:51 +01:00
|
|
|
{
|
|
|
|
|
memcpy((void *)mem_to, mem_from, sizeof(T) * N);
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-09 11:54:24 +01:00
|
|
|
T *data_;
|
|
|
|
|
size_t datasize_;
|
|
|
|
|
size_t capacity_;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
CCL_NAMESPACE_END
|