Blender V5.3
cpu/device_impl.cpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
6
7#include <algorithm>
8#include <cstdlib>
9#include <cstring>
10
11/* So ImathMath is included before our kernel_cpu_compat. */
12#ifdef WITH_OSL
13/* So no context pollution happens from indirectly included windows.h */
14# ifdef _WIN32
15# include "util/windows.h"
16# endif
17# include <OSL/oslexec.h>
18#endif
19
20#ifdef WITH_EMBREE
21# include <embree4/rtcore.h>
22#endif
23
24#include "device/cpu/kernel.h"
25
26#include "device/device.h"
27
29#include "kernel/globals.h"
30#include "kernel/types.h"
31
32#include "bvh/embree.h"
33
34#include "session/buffers.h"
35
36#include "util/guiding.h"
37#include "util/log.h"
38#include "util/progress.h"
39#include "util/task.h"
40#include "util/types_image.h"
41
43
44CPUDevice::CPUDevice(const DeviceInfo &info_, Stats &stats_, Profiler &profiler_, bool headless_)
45 : Device(info_, stats_, profiler_, headless_)
46{
47 image_info = make_unique<device_vector<KernelImageInfo>>(this, "image_info", MEM_GLOBAL);
48
49 /* Pick any kernel, all of them are supposed to have same level of microarchitecture
50 * optimization. */
52 << " CPU kernels.";
53
54 if (info.cpu_threads == 0) {
56 }
57
58#ifdef WITH_EMBREE
59 embree_device = rtcNewDevice("verbose=0");
60#endif
61}
62
64{
65#ifdef WITH_EMBREE
66 rtcReleaseDevice(embree_device);
67#endif
68
69 image_info->free();
70}
71
73{
74 BVHLayoutMask bvh_layout_mask = BVH_LAYOUT_BVH2;
75#ifdef WITH_EMBREE
76 bvh_layout_mask |= BVH_LAYOUT_EMBREE;
77#endif /* WITH_EMBREE */
78 return bvh_layout_mask;
79}
80
82{
83 if (mem.type == MEM_IMAGE_TEXTURE) {
84 assert(!"mem_alloc not supported for images.");
85 }
86 else if (mem.type == MEM_GLOBAL) {
87 assert(!"mem_alloc not supported for global memory.");
88 }
89 else {
90 LOG_DEBUG << "Buffer allocate: " << mem.log_name() << ", "
91 << string_human_readable_number(mem.memory_size()) << " bytes. ("
93
94 if (mem.type == MEM_DEVICE_ONLY) {
95 size_t alignment = MIN_ALIGNMENT_DEVICE_MEMORY;
96 void *data = util_aligned_malloc(mem.memory_size(), alignment);
97 mem.device_pointer = (device_ptr)data;
98 }
99 else {
100 assert(!(mem.host_pointer == nullptr && mem.memory_size() > 0));
102 }
103
104 mem.device_size = mem.memory_size();
106 }
107}
108
110{
111 if (mem.type == MEM_GLOBAL) {
112 global_free(mem);
113 global_alloc(mem);
114 }
115 else if (mem.type == MEM_IMAGE_TEXTURE) {
116 image_free((device_image &)mem);
118 }
119 else {
120 if (!mem.device_pointer) {
121 mem_alloc(mem);
122 }
123
124 /* copy is no-op */
125 }
126}
127
129{
130 /* no-op */
131}
132
134 device_memory & /*mem*/, size_t /*y*/, size_t /*w*/, size_t /*h*/, size_t /*elem*/)
135{
136 /* no-op */
137}
138
140{
141 /* Nothing to do data is already in host buffer. */
142}
143
145{
146 if (!mem.device_pointer) {
147 mem_alloc(mem);
148 }
149
150 if (mem.device_pointer) {
151 memset((void *)mem.device_pointer, 0, mem.memory_size());
152 }
153}
154
156{
157 if (mem.type == MEM_GLOBAL) {
158 global_free(mem);
159 }
160 else if (mem.type == MEM_IMAGE_TEXTURE) {
161 image_free((device_image &)mem);
162 }
163 else if (mem.device_pointer) {
164 if (mem.type == MEM_DEVICE_ONLY) {
165 util_aligned_free((void *)mem.device_pointer, mem.memory_size());
166 }
167 mem.device_pointer = 0;
169 mem.device_size = 0;
170 }
171}
172
173device_ptr CPUDevice::mem_alloc_sub_ptr(device_memory &mem, const size_t offset, size_t /*size*/)
174{
175 return (device_ptr)(((char *)mem.device_pointer) + mem.memory_elements_size(offset));
176}
177
178void CPUDevice::const_copy_to(const char *name, void *host, const size_t size)
179{
180#ifdef WITH_EMBREE
181 if (strcmp(name, "data") == 0) {
182 assert(size <= sizeof(KernelData));
183
184 /* Update scene handle (since it is different for each device on multi devices).
185 * This must be a raw pointer copy since at some points during scene update this
186 * pointer may be invalid. */
187 KernelData *const data = (KernelData *)host;
188 data->device_bvh = embree_traversable;
189 }
190#endif
191
192 /* Update both the main one, and the per-thread globals in case of updates during
193 * render from e.g. the texture cache. */
194 kernel_const_copy(&kernel_globals, name, host, size);
196 kernel_const_copy(&kg, name, host, size);
197 }
198}
199
201{
202 LOG_DEBUG << "Global memory allocate: " << mem.log_name() << ", "
203 << string_human_readable_number(mem.memory_size()) << " bytes. ("
205
206 /* Update both the main one, and the per-thread globals in case of updates during
207 * render from e.g. the texture cache. */
211 }
212
214 mem.device_size = mem.memory_size();
216}
217
219{
220 if (mem.device_pointer) {
221 mem.device_pointer = 0;
223 mem.device_size = 0;
224 }
225}
226
228{
229 LOG_DEBUG << "Texture allocate: " << mem.log_name() << ", "
230 << string_human_readable_number(mem.memory_size()) << " bytes. ("
232
234 mem.device_size = mem.memory_size();
236
237 const uint image_info_id = mem.image_info_id;
238 if (image_info_id >= image_info->size()) {
239 /* Geometric growth to amortize reallocation cost. */
240 const size_t new_size = max(size_t(image_info_id) + 128, image_info->size() * 2);
241
243 make_unique<device_vector<KernelImageInfo>>(this, "image_info", MEM_GLOBAL);
244 new_info->resize(new_size);
245
246 if (image_info->size() > 0) {
247 std::copy_n(image_info->data(), image_info->size(), new_info->data());
248 }
249
250 /* Move old vector to backup list to keep memory alive for concurrent access. */
251 old_image_infos.push_back(std::move(image_info));
252 image_info = std::move(new_info);
253
254 /* Update kernel globals pointers immediately. */
255 image_info->copy_to_device();
256 }
257
258 /* Assign with the final host pointer in one go, as this is updating memory
259 * that is read by live rendering threads. It works because the smaller array
260 * with same contents for the first part is kept alive. */
263 (*image_info)[image_info_id] = info;
264}
265
267{
268 if (mem.device_pointer) {
269 mem.device_pointer = 0;
271 mem.device_size = 0;
272 }
273}
274
276{
277 return true;
278}
279
280void CPUDevice::build_bvh(BVH *bvh, Progress &progress, bool refit)
281{
282#ifdef WITH_EMBREE
283 if (bvh->params.bvh_layout == BVH_LAYOUT_EMBREE ||
288 {
289 BVHEmbree *const bvh_embree = static_cast<BVHEmbree *>(bvh);
290 if (refit) {
291 bvh_embree->refit(progress);
292 }
293 else {
294 bvh_embree->build(progress, &stats, embree_device);
295 }
296
297 if (bvh->params.top_level) {
298# if RTC_VERSION >= 40400
299 embree_traversable = rtcGetSceneTraversable(bvh_embree->scene);
300# else
301 embree_traversable = bvh_embree->scene;
302# endif
303 }
304 }
305 else
306#endif
307 {
308 Device::build_bvh(bvh, progress, refit);
309 }
310}
311
313{
314#if defined(WITH_PATH_GUIDING)
315 if (!guiding_device) {
316 if (guiding_device_type() == 8) {
317 guiding_device = make_unique<openpgl::cpp::Device>(PGL_DEVICE_TYPE_CPU_8);
318 }
319 else if (guiding_device_type() == 4) {
320 guiding_device = make_unique<openpgl::cpp::Device>(PGL_DEVICE_TYPE_CPU_4);
321 }
322 }
323 return guiding_device.get();
324#else
325 return nullptr;
326#endif
327}
328
330{
332
334 OSLGlobals *osl_globals = get_cpu_osl_memory();
335 for (int i = 0; i < info.cpu_threads; i++) {
336 kernel_thread_globals_.emplace_back(kernel_globals, osl_globals, profiler, i);
337 }
338
340}
341
347
349{
350#ifdef WITH_OSL
351 return &osl_globals;
352#else
353 return nullptr;
354#endif
355}
356
358 KernelImageLoadRequestedGPU /*image_load_requested_gpu*/)
359{
360 kernel_globals.image_load_requested_cpu = image_load_requested_cpu;
361}
362
363bool CPUDevice::load_kernels(const uint64_t /*kernel_features*/)
364{
365 return true;
366}
367
CCL_NAMESPACE_BEGIN void * util_aligned_malloc(const size_t size, const int alignment)
void util_aligned_free(void *ptr, const size_t size)
unsigned long long int uint64_t
btScalar max
void refit(btStridingMeshInterface *triangles, const btVector3 &aabbMin, const btVector3 &aabbMax)
BVHLayout bvh_layout
Definition params.h:83
bool top_level
Definition params.h:80
Definition bvh/bvh.h:67
BVHParams params
Definition bvh/bvh.h:69
void release_cpu_kernel_thread_globals() override
list< unique_ptr< device_vector< KernelImageInfo > > > old_image_infos
CPUDevice(const DeviceInfo &info_, Stats &stats_, Profiler &profiler_, bool headless_)
void image_free(device_image &mem)
device_ptr mem_alloc_sub_ptr(device_memory &mem, const size_t offset, size_t) override
void set_image_cache_func(KernelImageLoadRequestedCPU image_load_requested_cpu, KernelImageLoadRequestedGPU image_load_requested_gpu) override
void mem_or_from_device(device_memory &mem) override
void mem_free(device_memory &mem) override
bool load_kernels(uint64_t kernel_features) override
void mem_alloc(device_memory &mem) override
bool has_unified_image_memory_all() const override
void const_copy_to(const char *name, void *host, const size_t size) override
void global_alloc(device_memory &mem)
~CPUDevice() override
vector< ThreadKernelGlobalsCPU > kernel_thread_globals_
void image_alloc(device_image &mem)
void mem_copy_from(device_memory &mem, const size_t y, size_t w, const size_t h, size_t elem) override
void mem_move_to_host(device_memory &mem) override
unique_ptr< device_vector< KernelImageInfo > > image_info
BVHLayoutMask get_bvh_layout_mask(uint64_t kernel_features) const override
void mem_zero(device_memory &mem) override
KernelGlobalsCPU kernel_globals
void * get_guiding_device() const override
void build_bvh(BVH *bvh, Progress &progress, bool refit) override
OSLGlobals * get_cpu_osl_memory() override
void mem_copy_to(device_memory &mem) override
vector< ThreadKernelGlobalsCPU > * acquire_cpu_kernel_thread_globals() override
void global_free(device_memory &mem)
const char * get_uarch_name() const
IntegratorInitFunction integrator_init_from_camera
Device(const DeviceInfo &info_, Stats &stats_, Profiler &profiler_, bool headless_)
static const CPUKernels & get_cpu_kernels()
friend class device_memory
Profiler & profiler
Stats & stats
virtual void build_bvh(BVH *bvh, Progress &progress, bool refit)
DeviceInfo info
void mem_alloc(const size_t size)
Definition util/stats.h:18
void mem_free(const size_t size)
Definition util/stats.h:24
static int max_concurrency()
Definition task.cpp:96
KernelImageInfo info
string log_name() const override
Definition memory.cpp:280
size_t memory_elements_size(const int elements)
const char * global_name() const
Definition memory.cpp:83
virtual string log_name() const
Definition memory.cpp:88
@ MEM_IMAGE_TEXTURE
@ MEM_DEVICE_ONLY
#define MIN_ALIGNMENT_DEVICE_MEMORY
#define CCL_NAMESPACE_END
#define assert(assertion)
CCL_NAMESPACE_BEGIN void kernel_const_copy(KernelGlobalsCPU *kg, const char *name, void *host, size_t)
void kernel_global_memory_copy(KernelGlobalsCPU *kg, const char *name, void *mem, const size_t size)
@ BVH_LAYOUT_MULTI_HIPRT_EMBREE
@ BVH_LAYOUT_EMBREE
@ BVH_LAYOUT_BVH2
@ BVH_LAYOUT_MULTI_METAL_EMBREE
@ BVH_LAYOUT_MULTI_EMBREEGPU_EMBREE
@ BVH_LAYOUT_MULTI_OPTIX_EMBREE
#define LOG_DEBUG
Definition log.h:107
#define LOG_INFO
Definition log.h:106
int BVHLayoutMask
Definition params.h:50
string string_human_readable_size(size_t size)
Definition string.cpp:333
string string_human_readable_number(size_t num)
Definition string.cpp:352
KernelImageLoadRequestedCPU image_load_requested_cpu
uint64_t device_ptr
Definition types_base.h:44
std::function< void(size_t, int, int, int, KernelTileDescriptor &tile_descriptor)> KernelImageLoadRequestedCPU
std::function< void(DeviceQueue &)> KernelImageLoadRequestedGPU