Blender V5.0
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
34
40
42{
43 if (!size) {
44 return nullptr;
45 }
46
47 void *ptr = device->host_alloc(type, size);
48
49 if (ptr == nullptr) {
50 throw std::bad_alloc();
51 }
52
53 return ptr;
54}
55
57{
58 if (host_pointer) {
60 device->host_free(type, host_pointer, memory_size());
61 }
62 host_pointer = nullptr;
63 }
64
65 if (device_pointer) {
66 device->mem_free(*this);
67 }
68
69 data_size = 0;
70 data_width = 0;
71 data_height = 0;
72}
73
75{
77 device->mem_alloc(*this);
78}
79
81{
82 if (host_pointer) {
83 device->mem_copy_to(*this);
84 }
85}
86
88{
89 if (host_pointer) {
90 device->mem_move_to_host(*this);
91 }
92}
93
94void device_memory::device_copy_from(const size_t y, const size_t w, size_t h, const size_t elem)
95{
97 device->mem_copy_from(*this, y, w, h, elem);
98}
99
101{
102 if (data_size) {
103 device->mem_zero(*this);
104 }
105}
106
108{
109 return (device->info.type == DEVICE_CPU);
110}
111
113 const size_t new_device_size,
114 device_ptr new_device_ptr)
115{
119
120 device = new_device;
121 device_size = new_device_size;
122 device_pointer = new_device_ptr;
123}
124
131
132bool device_memory::is_resident(Device *sub_device) const
133{
134 return device->is_resident(device_pointer, sub_device);
135}
136
137bool device_memory::is_shared(Device *sub_device) const
138{
139 return device->is_shared(shared_pointer, device_pointer, sub_device);
140}
141
142/* Device Sub `ptr`. */
143
144device_sub_ptr::device_sub_ptr(device_memory &mem, const size_t offset, const size_t size)
145 : device(mem.device)
146{
147 ptr = device->mem_alloc_sub_ptr(mem, offset, size);
148}
149
151{
152 device->mem_free_sub_ptr(ptr);
153}
154
155/* Device Texture */
156
158 const char *name,
159 const uint slot,
160 ImageDataType image_data_type,
161 InterpolationType interpolation,
162 ExtensionType extension)
164{
165 switch (image_data_type) {
168 data_elements = 4;
169 break;
172 data_elements = 1;
173 break;
176 data_elements = 4;
177 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)
221{
222 const size_t new_size = size(width, height);
223
224 if (new_size != data_size) {
228 }
229
230 data_size = new_size;
231 data_width = width;
232 data_height = height;
233
234 info.width = width;
235 info.height = height;
236
237 return host_pointer;
238}
239
244
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:132
void * host_alloc(const size_t size)
Definition memory.cpp:41
device_memory(const device_memory &)=delete
void device_zero()
Definition memory.cpp:100
void device_alloc()
Definition memory.cpp:74
void device_copy_to()
Definition memory.cpp:80
void swap_device(Device *new_device, const size_t new_device_size, device_ptr new_device_ptr)
Definition memory.cpp:112
device_ptr original_device_ptr
void host_and_device_free()
Definition memory.cpp:56
bool is_shared(Device *sub_device) const
Definition memory.cpp:137
bool device_is_cpu()
Definition memory.cpp:107
void device_move_to_host()
Definition memory.cpp:87
void restore_device()
Definition memory.cpp:125
virtual ~device_memory()
Definition memory.cpp:35
void device_copy_from(const size_t y, const size_t w, size_t h, const size_t elem)
Definition memory.cpp:94
device_sub_ptr(device_memory &mem, const size_t offset, const size_t size)
Definition memory.cpp:144
void copy_to_device()
Definition memory.cpp:240
~device_texture() override
Definition memory.cpp:214
void * alloc(const size_t width, const size_t height)
Definition memory.cpp:220
size_t size(const size_t width, const size_t height)
device_texture(Device *device, const char *name, const uint slot, ImageDataType image_data_type, InterpolationType interpolation, ExtensionType extension)
Definition memory.cpp:157
static constexpr size_t datatype_size(DataType datatype)
@ MEM_TEXTURE
@ MEM_READ_ONLY
@ TYPE_UINT16
#define CCL_NAMESPACE_END
@ DEVICE_CPU
#define assert(assertion)
ImageDataType
Definition texture.h:32
@ IMAGE_DATA_NUM_TYPES
Definition texture.h:48
@ IMAGE_DATA_TYPE_BYTE
Definition texture.h:37
@ IMAGE_DATA_TYPE_FLOAT
Definition texture.h:36
@ IMAGE_DATA_TYPE_NANOVDB_FP16
Definition texture.h:45
@ IMAGE_DATA_TYPE_FLOAT4
Definition texture.h:33
@ IMAGE_DATA_TYPE_USHORT4
Definition texture.h:39
@ IMAGE_DATA_TYPE_USHORT
Definition texture.h:40
@ IMAGE_DATA_TYPE_NANOVDB_EMPTY
Definition texture.h:46
@ IMAGE_DATA_TYPE_NANOVDB_FLOAT
Definition texture.h:41
@ IMAGE_DATA_TYPE_NANOVDB_FLOAT3
Definition texture.h:42
@ IMAGE_DATA_TYPE_HALF
Definition texture.h:38
@ IMAGE_DATA_TYPE_BYTE4
Definition texture.h:34
@ IMAGE_DATA_TYPE_HALF4
Definition texture.h:35
@ IMAGE_DATA_TYPE_NANOVDB_FLOAT4
Definition texture.h:43
@ IMAGE_DATA_TYPE_NANOVDB_FPN
Definition texture.h:44
InterpolationType
Definition texture.h:22
ExtensionType
Definition texture.h:71
uint64_t device_ptr
Definition types_base.h:44
PointerRNA * ptr
Definition wm_files.cc:4238