Blender V4.3
memory.cpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#include "device/memory.h"
6#include "device/device.h"
7
9
10/* Device Memory */
11
12device_memory::device_memory(Device *device, const char *_name, MemoryType type)
13 : data_type(device_type_traits<uchar>::data_type),
14 data_elements(device_type_traits<uchar>::num_elements),
15 data_size(0),
16 device_size(0),
17 data_width(0),
18 data_height(0),
19 data_depth(0),
20 type(type),
21 name_storage(_name),
22 device(device),
23 device_pointer(0),
24 host_pointer(0),
25 shared_pointer(0),
26 shared_counter(0),
27 original_device_ptr(0),
28 original_device_size(0),
29 original_device(0),
30 need_realloc_(false),
31 modified(false)
32{
33 name = name_storage.c_str();
34}
35
37{
38 assert(shared_pointer == 0);
39 assert(shared_counter == 0);
40}
41
42void *device_memory::host_alloc(size_t size)
43{
44 if (!size) {
45 return 0;
46 }
47
49
50 if (ptr) {
52 }
53 else {
54 throw std::bad_alloc();
55 }
56
57 return ptr;
58}
59
68
70{
71 assert(!device_pointer && type != MEM_TEXTURE && type != MEM_GLOBAL);
72 device->mem_alloc(*this);
73}
74
76{
77 if (device_pointer) {
78 device->mem_free(*this);
79 }
80}
81
83{
84 if (host_pointer) {
85 device->mem_copy_to(*this);
86 }
87}
88
89void device_memory::device_copy_from(size_t y, size_t w, size_t h, size_t elem)
90{
91 assert(type != MEM_TEXTURE && type != MEM_READ_ONLY && type != MEM_GLOBAL);
92 device->mem_copy_from(*this, y, w, h, elem);
93}
94
96{
97 if (data_size) {
98 device->mem_zero(*this);
99 }
100}
101
103{
104 return (device->info.type == DEVICE_CPU);
105}
106
108 size_t new_device_size,
109 device_ptr new_device_ptr)
110{
114
115 device = new_device;
116 device_size = new_device_size;
117 device_pointer = new_device_ptr;
118}
119
126
127bool device_memory::is_resident(Device *sub_device) const
128{
129 return device->is_resident(device_pointer, sub_device);
130}
131
132/* Device Sub `ptr`. */
133
134device_sub_ptr::device_sub_ptr(device_memory &mem, size_t offset, size_t size) : device(mem.device)
135{
136 ptr = device->mem_alloc_sub_ptr(mem, offset, size);
137}
138
143
144/* Device Texture */
145
147 const char *name,
148 const uint slot,
149 ImageDataType image_data_type,
150 InterpolationType interpolation,
151 ExtensionType extension)
152 : device_memory(device, name, MEM_TEXTURE), slot(slot)
153{
154 switch (image_data_type) {
157 data_elements = 4;
158 break;
161 data_elements = 1;
162 break;
165 data_elements = 4;
166 break;
173 data_elements = 1;
174 break;
177 data_elements = 4;
178 break;
181 data_elements = 1;
182 break;
185 data_elements = 4;
186 break;
189 data_elements = 1;
190 break;
192 assert(0);
193 return;
194 }
195
196 memset(&info, 0, sizeof(info));
197 info.data_type = image_data_type;
198 info.interpolation = interpolation;
199 info.extension = extension;
200}
201
207
208/* Host memory allocation. */
209void *device_texture::alloc(const size_t width, const size_t height, const size_t depth)
210{
211 const size_t new_size = size(width, height, depth);
212
213 if (new_size != data_size) {
214 device_free();
215 host_free();
217 assert(device_pointer == 0);
218 }
219
220 data_size = new_size;
221 data_width = width;
222 data_height = height;
223 data_depth = depth;
224
225 info.width = width;
226 info.height = height;
227 info.depth = depth;
228
229 return host_pointer;
230}
231
236
unsigned char uchar
unsigned int uint
void util_aligned_free(void *ptr)
CCL_NAMESPACE_BEGIN void * util_aligned_malloc(size_t size, int alignment)
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
DeviceType type
virtual bool is_resident(device_ptr, Device *sub_device)
virtual void mem_zero(device_memory &mem)=0
virtual void mem_copy_from(device_memory &mem, size_t y, size_t w, size_t h, size_t elem)=0
virtual void mem_free_sub_ptr(device_ptr)
virtual void mem_free(device_memory &mem)=0
virtual void mem_copy_to(device_memory &mem)=0
virtual device_ptr mem_alloc_sub_ptr(device_memory &, size_t, size_t)
DeviceInfo info
virtual void mem_alloc(device_memory &mem)=0
bool is_resident(Device *sub_device) const
Definition memory.cpp:127
void device_zero()
Definition memory.cpp:95
void device_alloc()
Definition memory.cpp:69
void device_copy_to()
Definition memory.cpp:82
void device_free()
Definition memory.cpp:75
device_memory(Device *device, const char *name, MemoryType type)
Definition memory.cpp:12
device_ptr original_device_ptr
bool device_is_cpu()
Definition memory.cpp:102
void restore_device()
Definition memory.cpp:120
void * host_alloc(size_t size)
Definition memory.cpp:42
void host_free()
Definition memory.cpp:60
virtual ~device_memory()
Definition memory.cpp:36
void device_copy_from(size_t y, size_t w, size_t h, size_t elem)
Definition memory.cpp:89
void swap_device(Device *new_device, size_t new_device_size, device_ptr new_device_ptr)
Definition memory.cpp:107
device_sub_ptr(device_memory &mem, size_t offset, size_t size)
Definition memory.cpp:134
size_t size(const size_t width, const size_t height, const size_t depth)
void copy_to_device()
Definition memory.cpp:232
void * alloc(const size_t width, const size_t height, const size_t depth=0)
Definition memory.cpp:209
device_texture(Device *device, const char *name, const uint slot, ImageDataType image_data_type, InterpolationType interpolation, ExtensionType extension)
Definition memory.cpp:146
static constexpr size_t datatype_size(DataType datatype)
@ MEM_TEXTURE
@ MEM_READ_ONLY
@ TYPE_UINT16
#define MIN_ALIGNMENT_CPU_DATA_TYPES
#define CCL_NAMESPACE_END
@ DEVICE_CPU
void util_guarded_mem_free(size_t n)
void util_guarded_mem_alloc(size_t n)
uint interpolation
ImageDataType
@ IMAGE_DATA_NUM_TYPES
@ IMAGE_DATA_TYPE_BYTE
@ IMAGE_DATA_TYPE_FLOAT
@ IMAGE_DATA_TYPE_NANOVDB_FP16
@ IMAGE_DATA_TYPE_FLOAT4
@ IMAGE_DATA_TYPE_USHORT4
@ IMAGE_DATA_TYPE_USHORT
@ IMAGE_DATA_TYPE_NANOVDB_FLOAT
@ IMAGE_DATA_TYPE_NANOVDB_FLOAT3
@ IMAGE_DATA_TYPE_HALF
@ IMAGE_DATA_TYPE_BYTE4
@ IMAGE_DATA_TYPE_HALF4
@ IMAGE_DATA_TYPE_NANOVDB_FPN
InterpolationType
ExtensionType
uint64_t device_ptr
Definition util/types.h:45
PointerRNA * ptr
Definition wm_files.cc:4126