Blender V5.0
BLI_compute_context.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
36
37#include "BLI_cache_mutex.hh"
38#include "BLI_string_ref.hh"
40
41namespace blender {
42
43class ComputeContext;
44
52
53 uint64_t hash() const
54 {
55 return v1;
56 }
57
59
60
68 template<size_t N, typename... Args>
69 static ComputeContextHash from(const ComputeContext *parent,
70 const char (&type_str)[N],
71 Args &&...args);
72
73 friend std::ostream &operator<<(std::ostream &stream, const ComputeContextHash &hash);
74
75 private:
80 template<typename... Args> static ComputeContextHash from_shallow_bytes(Args &&...args);
81
83 static ComputeContextHash from_bytes(const void *data, int64_t len);
84};
85
93 protected:
98 const ComputeContext *parent_ = nullptr;
99
106
107 private:
108 mutable CacheMutex hash_mutex_;
109
110 public:
112 virtual ~ComputeContext() = default;
113
115 {
116 hash_mutex_.ensure([&]() { hash_ = this->compute_hash(); });
117 return hash_;
118 }
119
120 const ComputeContext *parent() const
121 {
122 return parent_;
123 }
124
128 void print_stack(std::ostream &stream, StringRef name) const;
129
133 virtual void print_current_in_line(std::ostream &stream) const = 0;
134
135 friend std::ostream &operator<<(std::ostream &stream, const ComputeContext &compute_context);
136
137 private:
139 virtual ComputeContextHash compute_hash() const = 0;
140};
141
142template<size_t N, typename... Args>
144 const char (&type_str)[N],
145 Args &&...args)
146{
147 return ComputeContextHash::from_shallow_bytes(
148 parent ? parent->hash() : ComputeContextHash{0, 0}, type_str, args...);
149}
150
151template<typename... Args>
152inline ComputeContextHash ComputeContextHash::from_shallow_bytes(Args &&...args)
153{
154 /* Copy all values into a contiguous buffer. Intentionally don't use std::tuple to avoid any
155 * potential padding. */
156 constexpr int64_t size_sum = (sizeof(args) + ...);
157 char buffer[size_sum];
158 int64_t offset = 0;
159 (
160 [&] {
161 using Arg = std::remove_reference_t<std::remove_cv_t<Args>>;
162 static_assert(std::has_unique_object_representations_v<Arg>);
163 const Arg &arg = args;
164 memcpy(buffer + offset, &arg, sizeof(Arg));
165 offset += sizeof(Arg);
166 }(),
167 ...);
168 /* Compute the hash of that buffer. */
169 return ComputeContextHash::from_bytes(buffer, offset);
170}
171
172} // namespace blender
#define BLI_STRUCT_EQUALITY_OPERATORS_2(Type, m1, m2)
BMesh const char void * data
ATTR_WARN_UNUSED_RESULT const BMVert * v2
long long int int64_t
unsigned long long int uint64_t
ComputeContext(const ComputeContext *parent)
const ComputeContext * parent() const
void print_stack(std::ostream &stream, StringRef name) const
friend std::ostream & operator<<(std::ostream &stream, const ComputeContext &compute_context)
virtual ~ComputeContext()=default
const ComputeContext * parent_
const ComputeContextHash & hash() const
virtual void print_current_in_line(std::ostream &stream) const =0
#define N
#define hash
Definition noise_c.cc:154
const char * name
static ComputeContextHash from(const ComputeContext *parent, const char(&type_str)[N], Args &&...args)
friend std::ostream & operator<<(std::ostream &stream, const ComputeContextHash &hash)
uint len