Blender  V3.3
memory.cpp
Go to the documentation of this file.
1 /* SPDX-License-Identifier: Apache-2.0
2  * Copyright 2011-2022 Blender Foundation */
3 
4 #include "device/memory.h"
5 #include "device/device.h"
6 
8 
9 /* Device Memory */
10 
12  : data_type(device_type_traits<uchar>::data_type),
13  data_elements(device_type_traits<uchar>::num_elements),
14  data_size(0),
15  device_size(0),
16  data_width(0),
17  data_height(0),
18  data_depth(0),
19  type(type),
20  name(name),
21  device(device),
22  device_pointer(0),
23  host_pointer(0),
24  shared_pointer(0),
25  shared_counter(0),
26  original_device_ptr(0),
27  original_device_size(0),
28  original_device(0),
29  need_realloc_(false),
30  modified(false)
31 {
32 }
33 
35 {
36  assert(shared_pointer == 0);
37  assert(shared_counter == 0);
38 }
39 
41 {
42  if (!size) {
43  return 0;
44  }
45 
47 
48  if (ptr) {
50  }
51  else {
52  throw std::bad_alloc();
53  }
54 
55  return ptr;
56 }
57 
59 {
60  if (host_pointer) {
63  host_pointer = 0;
64  }
65 }
66 
68 {
69  assert(!device_pointer && type != MEM_TEXTURE && type != MEM_GLOBAL);
70  device->mem_alloc(*this);
71 }
72 
74 {
75  if (device_pointer) {
76  device->mem_free(*this);
77  }
78 }
79 
81 {
82  if (host_pointer) {
83  device->mem_copy_to(*this);
84  }
85 }
86 
87 void device_memory::device_copy_from(size_t y, size_t w, size_t h, size_t elem)
88 {
89  assert(type != MEM_TEXTURE && type != MEM_READ_ONLY && type != MEM_GLOBAL);
90  device->mem_copy_from(*this, y, w, h, elem);
91 }
92 
94 {
95  if (data_size) {
96  device->mem_zero(*this);
97  }
98 }
99 
101 {
102  return (device->info.type == DEVICE_CPU);
103 }
104 
106  size_t new_device_size,
107  device_ptr new_device_ptr)
108 {
112 
113  device = new_device;
114  device_size = new_device_size;
115  device_pointer = new_device_ptr;
116 }
117 
119 {
123 }
124 
125 bool device_memory::is_resident(Device *sub_device) const
126 {
127  return device->is_resident(device_pointer, sub_device);
128 }
129 
130 /* Device Sub Ptr */
131 
132 device_sub_ptr::device_sub_ptr(device_memory &mem, size_t offset, size_t size) : device(mem.device)
133 {
135 }
136 
138 {
140 }
141 
142 /* Device Texture */
143 
145  const char *name,
146  const uint slot,
147  ImageDataType image_data_type,
148  InterpolationType interpolation,
149  ExtensionType extension)
150  : device_memory(device, name, MEM_TEXTURE), slot(slot)
151 {
152  switch (image_data_type) {
155  data_elements = 4;
156  break;
159  data_elements = 1;
160  break;
163  data_elements = 4;
164  break;
171  data_elements = 1;
172  break;
175  data_elements = 4;
176  break;
179  data_elements = 1;
180  break;
183  data_elements = 4;
184  break;
187  data_elements = 1;
188  break;
190  assert(0);
191  return;
192  }
193 
194  memset(&info, 0, sizeof(info));
195  info.data_type = image_data_type;
196  info.interpolation = interpolation;
197  info.extension = extension;
198 }
199 
201 {
202  device_free();
203  host_free();
204 }
205 
206 /* Host memory allocation. */
207 void *device_texture::alloc(const size_t width, const size_t height, const size_t depth)
208 {
209  const size_t new_size = size(width, height, depth);
210 
211  if (new_size != data_size) {
212  device_free();
213  host_free();
215  assert(device_pointer == 0);
216  }
217 
218  data_size = new_size;
219  data_width = width;
221  data_depth = depth;
222 
223  info.width = width;
224  info.height = height;
225  info.depth = depth;
226 
227  return host_pointer;
228 }
229 
231 {
232  device_copy_to();
233 }
234 
unsigned char uchar
Definition: BLI_sys_types.h:70
unsigned int uint
Definition: BLI_sys_types.h:67
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei height
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint y
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum type
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei width
void util_aligned_free(void *ptr)
CCL_NAMESPACE_BEGIN void * util_aligned_malloc(size_t size, int alignment)
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
DeviceType type
Definition: device/device.h:62
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:125
void device_zero()
Definition: memory.cpp:93
void device_alloc()
Definition: memory.cpp:67
void device_copy_to()
Definition: memory.cpp:80
void device_free()
Definition: memory.cpp:73
device_memory(Device *device, const char *name, MemoryType type)
Definition: memory.cpp:11
device_ptr original_device_ptr
bool device_is_cpu()
Definition: memory.cpp:100
device_ptr device_pointer
void restore_device()
Definition: memory.cpp:118
void * host_alloc(size_t size)
Definition: memory.cpp:40
void host_free()
Definition: memory.cpp:58
virtual ~device_memory()
Definition: memory.cpp:34
void device_copy_from(size_t y, size_t w, size_t h, size_t elem)
Definition: memory.cpp:87
void swap_device(Device *new_device, size_t new_device_size, device_ptr new_device_ptr)
Definition: memory.cpp:105
device_sub_ptr(device_memory &mem, size_t offset, size_t size)
Definition: memory.cpp:132
size_t size(const size_t width, const size_t height, const size_t depth)
void copy_to_device()
Definition: memory.cpp:230
void * alloc(const size_t width, const size_t height, const size_t depth=0)
Definition: memory.cpp:207
device_texture(Device *device, const char *name, const uint slot, ImageDataType image_data_type, InterpolationType interpolation, ExtensionType extension)
Definition: memory.cpp:144
#define CCL_NAMESPACE_END
Definition: cuda/compat.h:9
static constexpr size_t datatype_size(DataType datatype)
@ MEM_GLOBAL
@ MEM_TEXTURE
@ MEM_READ_ONLY
@ TYPE_FLOAT
@ TYPE_HALF
@ TYPE_UINT16
@ TYPE_UCHAR
#define MIN_ALIGNMENT_CPU_DATA_TYPES
@ DEVICE_CPU
Definition: device/device.h:38
void util_guarded_mem_free(size_t n)
void util_guarded_mem_alloc(size_t n)
ccl_gpu_kernel_postfix ccl_global float int int int int float bool int offset
uint data_type
Definition: util/texture.h:76
uint extension
Definition: util/texture.h:78
uint interpolation
Definition: util/texture.h:78
ImageDataType
Definition: util/texture.h:29
@ IMAGE_DATA_NUM_TYPES
Definition: util/texture.h:43
@ IMAGE_DATA_TYPE_BYTE
Definition: util/texture.h:34
@ IMAGE_DATA_TYPE_FLOAT
Definition: util/texture.h:33
@ IMAGE_DATA_TYPE_NANOVDB_FP16
Definition: util/texture.h:41
@ IMAGE_DATA_TYPE_FLOAT4
Definition: util/texture.h:30
@ IMAGE_DATA_TYPE_USHORT4
Definition: util/texture.h:36
@ IMAGE_DATA_TYPE_USHORT
Definition: util/texture.h:37
@ IMAGE_DATA_TYPE_NANOVDB_FLOAT
Definition: util/texture.h:38
@ IMAGE_DATA_TYPE_NANOVDB_FLOAT3
Definition: util/texture.h:39
@ IMAGE_DATA_TYPE_HALF
Definition: util/texture.h:35
@ IMAGE_DATA_TYPE_BYTE4
Definition: util/texture.h:31
@ IMAGE_DATA_TYPE_HALF4
Definition: util/texture.h:32
@ IMAGE_DATA_TYPE_NANOVDB_FPN
Definition: util/texture.h:40
InterpolationType
Definition: util/texture.h:19
ExtensionType
Definition: util/texture.h:61
uint64_t device_ptr
Definition: util/types.h:43
PointerRNA * ptr
Definition: wm_files.c:3480