Blender V5.3
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"
39#include "BLI_unique_hash.hh"
40
41namespace blender {
42
43class ComputeContext;
44
57 template<size_t N, typename... Args>
58 static ComputeContextHash from(const ComputeContext *parent,
59 const char (&type_str)[N],
60 Args &&...args);
61
62 friend std::ostream &operator<<(std::ostream &stream, const ComputeContextHash &hash);
63
68 template<typename... Args> static ComputeContextHash from_shallow_bytes(Args &&...args);
69
71 static ComputeContextHash from_bytes(const void *data, int64_t len);
72};
73
81 protected:
86 const ComputeContext *parent_ = nullptr;
87
94
95 private:
96 mutable CacheMutex hash_mutex_;
97
102 int parents_num_ = 0;
103
104 public:
106 : parent_(parent), parents_num_(parent ? parent->parents_num_ + 1 : 0)
107 {
108 }
109 virtual ~ComputeContext() = default;
110
112 {
113 hash_mutex_.ensure([&]() { hash_ = this->compute_hash(); });
114 return hash_;
115 }
116
117 const ComputeContext *parent() const
118 {
119 return parent_;
120 }
121
122 int parents_num() const
123 {
124 return parents_num_;
125 }
126
130 void print_stack(std::ostream &stream, StringRef name) const;
131
135 virtual void print_current_in_line(std::ostream &stream) const = 0;
136
137 friend std::ostream &operator<<(std::ostream &stream, const ComputeContext &compute_context);
138
139 private:
141 virtual ComputeContextHash compute_hash() const = 0;
142};
143
144template<size_t N, typename... Args>
146 const char (&type_str)[N],
147 Args &&...args)
148{
150 parent ? parent->hash() : ComputeContextHash{0, 0}, type_str, args...);
151}
152
153template<typename... Args>
155{
156 /* Copy all values into a contiguous buffer. Intentionally don't use std::tuple to avoid any
157 * potential padding. */
158 constexpr int64_t size_sum = (sizeof(args) + ...);
159 char buffer[size_sum];
160 int64_t offset = 0;
161 (
162 [&] {
163 using Arg = std::remove_reference_t<std::remove_cv_t<Args>>;
164 static_assert(std::has_unique_object_representations_v<Arg>);
165 const Arg &arg = args;
166 memcpy(buffer + offset, &arg, sizeof(Arg));
167 offset += sizeof(Arg);
168 }(),
169 ...);
170 /* Compute the hash of that buffer. */
171 return ComputeContextHash::from_bytes(buffer, offset);
172}
173
174} // namespace blender
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
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
#define N
BMesh Iterators.
__int64 int64_t
Definition BKE_text.h:90
BMesh const char void * data
static ComputeContextHash from_bytes(const void *data, int64_t len)
static ComputeContextHash from(const ComputeContext *parent, const char(&type_str)[N], Args &&...args)
static ComputeContextHash from_shallow_bytes(Args &&...args)
friend std::ostream & operator<<(std::ostream &stream, const ComputeContextHash &hash)