Blender V4.5
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
35
41
43{
44 if (!size) {
45 return nullptr;
46 }
47
48 void *ptr = device->host_alloc(type, size);
49
50 if (ptr == nullptr) {
51 throw std::bad_alloc();
52 }
53
54 return ptr;
55}
56
58{
59 if (host_pointer) {
61 device->host_free(type, host_pointer, memory_size());
62 }
63 host_pointer = nullptr;
64 }
65
66 if (device_pointer) {
67 device->mem_free(*this);
68 }
69
70 data_size = 0;
71 data_width = 0;
72 data_height = 0;
73 data_depth = 0;
74}
75
77{
79 device->mem_alloc(*this);
80}
81
83{
84 if (host_pointer) {
85 device->mem_copy_to(*this);
86 }
87}
88
90{
91 if (host_pointer) {
92 device->mem_move_to_host(*this);
93 }
94}
95
96void device_memory::device_copy_from(const size_t y, const size_t w, size_t h, const size_t elem)
97{
99 device->mem_copy_from(*this, y, w, h, elem);
100}
101
103{
104 if (data_size) {
105 device->mem_zero(*this);
106 }
107}
108
110{
111 return (device->info.type == DEVICE_CPU);
112}
113
115 const size_t new_device_size,
116 device_ptr new_device_ptr)
117{
121
122 device = new_device;
123 device_size = new_device_size;
124 device_pointer = new_device_ptr;
125}
126
133
134bool device_memory::is_resident(Device *sub_device) const
135{
136 return device->is_resident(device_pointer, sub_device);
137}
138
139bool device_memory::is_shared(Device *sub_device) const
140{
141 return device->is_shared(shared_pointer, device_pointer, sub_device);
142}
143
144/* Device Sub `ptr`. */
145
146device_sub_ptr::device_sub_ptr(device_memory &mem, const size_t offset, const size_t size)
147 : device(mem.device)
148{
149 ptr = device->mem_alloc_sub_ptr(mem, offset, size);
150}
151
153{
154 device->mem_free_sub_ptr(ptr);
155}
156
157/* Device Texture */
158
160 const char *name,
161 const uint slot,
162 ImageDataType image_data_type,
163 InterpolationType interpolation,
164 ExtensionType extension)
166{
167 switch (image_data_type) {
170 data_elements = 4;
171 break;
174 data_elements = 1;
175 break;
178 data_elements = 4;
179 break;
186 data_elements = 1;
187 break;
190 data_elements = 4;
191 break;
194 data_elements = 1;
195 break;
198 data_elements = 4;
199 break;
202 data_elements = 1;
203 break;
205 assert(0);
206 return;
207 }
208
209 info.data_type = image_data_type;
210 info.interpolation = interpolation;
211 info.extension = extension;
212}
213
218
219/* Host memory allocation. */
220void *device_texture::alloc(const size_t width, const size_t height, const size_t depth)
221{
222 const size_t new_size = size(width, height, depth);
223
224 if (new_size != data_size) {
228 }
229
230 data_size = new_size;
231 data_width = width;
232 data_height = height;
233 data_depth = depth;
234
235 info.width = width;
236 info.height = height;
237 info.depth = depth;
238
239 return host_pointer;
240}
241
246
unsigned char uchar
unsigned int uint
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition btDbvt.cpp:52
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
bool is_resident(Device *sub_device) const
Definition memory.cpp:134
void * host_alloc(const size_t size)
Definition memory.cpp:42
device_memory(const device_memory &)=delete
void device_zero()
Definition memory.cpp:102
void device_alloc()
Definition memory.cpp:76
void device_copy_to()
Definition memory.cpp:82
void swap_device(Device *new_device, const size_t new_device_size, device_ptr new_device_ptr)
Definition memory.cpp:114
device_ptr original_device_ptr
void host_and_device_free()
Definition memory.cpp:57
bool is_shared(Device *sub_device) const
Definition memory.cpp:139
bool device_is_cpu()
Definition memory.cpp:109
void device_move_to_host()
Definition memory.cpp:89
void restore_device()
Definition memory.cpp:127
virtual ~device_memory()
Definition memory.cpp:36
void device_copy_from(const size_t y, const size_t w, size_t h, const size_t elem)
Definition memory.cpp:96
device_sub_ptr(device_memory &mem, const size_t offset, const size_t size)
Definition memory.cpp:146
size_t size(const size_t width, const size_t height, const size_t depth)
void copy_to_device()
Definition memory.cpp:242
~device_texture() override
Definition memory.cpp:214
void * alloc(const size_t width, const size_t height, const size_t depth=0)
Definition memory.cpp:220
device_texture(Device *device, const char *name, const uint slot, ImageDataType image_data_type, InterpolationType interpolation, ExtensionType extension)
Definition memory.cpp:159
static constexpr size_t datatype_size(DataType datatype)
@ MEM_TEXTURE
@ MEM_READ_ONLY
@ TYPE_UINT16
#define CCL_NAMESPACE_END
@ DEVICE_CPU
#define assert(assertion)
uint64_t device_ptr
Definition types_base.h:44
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
PointerRNA * ptr
Definition wm_files.cc:4227