Blender  V2.93
device_memory.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2011-2017 Blender Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "device/device_memory.h"
18 #include "device/device.h"
19 
21 
22 /* Device Memory */
23 
25  : data_type(device_type_traits<uchar>::data_type),
26  data_elements(device_type_traits<uchar>::num_elements),
27  data_size(0),
28  device_size(0),
29  data_width(0),
30  data_height(0),
31  data_depth(0),
32  type(type),
33  name(name),
34  device(device),
35  device_pointer(0),
36  host_pointer(0),
37  shared_pointer(0),
38  shared_counter(0),
39  original_device_ptr(0),
40  original_device_size(0),
41  original_device(0),
42  need_realloc_(false),
43  modified(false)
44 {
45 }
46 
48  : data_type(other.data_type),
49  data_elements(other.data_elements),
50  data_size(other.data_size),
51  device_size(other.device_size),
52  data_width(other.data_width),
53  data_height(other.data_height),
54  data_depth(other.data_depth),
55  type(other.type),
56  name(other.name),
57  device(other.device),
58  device_pointer(other.device_pointer),
59  host_pointer(other.host_pointer),
60  shared_pointer(other.shared_pointer),
61  shared_counter(other.shared_counter),
62  original_device_ptr(other.original_device_ptr),
63  original_device_size(other.original_device_size),
64  original_device(other.original_device),
65  need_realloc_(other.need_realloc_),
66  modified(other.modified)
67 {
68  other.data_elements = 0;
69  other.data_size = 0;
70  other.device_size = 0;
71  other.data_width = 0;
72  other.data_height = 0;
73  other.data_depth = 0;
74  other.device = 0;
75  other.device_pointer = 0;
76  other.host_pointer = 0;
77  other.shared_pointer = 0;
78  other.shared_counter = 0;
79  other.original_device_ptr = 0;
80  other.original_device_size = 0;
81  other.original_device = 0;
82  other.need_realloc_ = false;
83  other.modified = false;
84 }
85 
87 {
88  assert(shared_pointer == 0);
89  assert(shared_counter == 0);
90 }
91 
93 {
94  if (!size) {
95  return 0;
96  }
97 
99 
100  if (ptr) {
102  }
103  else {
104  throw std::bad_alloc();
105  }
106 
107  return ptr;
108 }
109 
111 {
112  if (host_pointer) {
115  host_pointer = 0;
116  }
117 }
118 
120 {
121  assert(!device_pointer && type != MEM_TEXTURE && type != MEM_GLOBAL);
122  device->mem_alloc(*this);
123 }
124 
126 {
127  if (device_pointer) {
128  device->mem_free(*this);
129  }
130 }
131 
133 {
134  if (host_pointer) {
135  device->mem_copy_to(*this);
136  }
137 }
138 
139 void device_memory::device_copy_from(int y, int w, int h, int elem)
140 {
141  assert(type != MEM_TEXTURE && type != MEM_READ_ONLY && type != MEM_GLOBAL);
142  device->mem_copy_from(*this, y, w, h, elem);
143 }
144 
146 {
147  if (data_size) {
148  device->mem_zero(*this);
149  }
150 }
151 
153  size_t new_device_size,
154  device_ptr new_device_ptr)
155 {
159 
160  device = new_device;
161  device_size = new_device_size;
162  device_pointer = new_device_ptr;
163 }
164 
166 {
170 }
171 
172 bool device_memory::is_resident(Device *sub_device) const
173 {
174  return device->is_resident(device_pointer, sub_device);
175 }
176 
177 /* Device Sub Ptr */
178 
179 device_sub_ptr::device_sub_ptr(device_memory &mem, int offset, int size) : device(mem.device)
180 {
181  ptr = device->mem_alloc_sub_ptr(mem, offset, size);
182 }
183 
185 {
187 }
188 
189 /* Device Texture */
190 
192  const char *name,
193  const uint slot,
194  ImageDataType image_data_type,
195  InterpolationType interpolation,
196  ExtensionType extension)
197  : device_memory(device, name, MEM_TEXTURE), slot(slot)
198 {
199  switch (image_data_type) {
202  data_elements = 4;
203  break;
206  data_elements = 1;
207  break;
210  data_elements = 4;
211  break;
216  data_elements = 1;
217  break;
220  data_elements = 4;
221  break;
224  data_elements = 1;
225  break;
228  data_elements = 4;
229  break;
232  data_elements = 1;
233  break;
235  assert(0);
236  return;
237  }
238 
239  memset(&info, 0, sizeof(info));
240  info.data_type = image_data_type;
241  info.interpolation = interpolation;
242  info.extension = extension;
243 }
244 
246 {
247  device_free();
248  host_free();
249 }
250 
251 /* Host memory allocation. */
252 void *device_texture::alloc(const size_t width, const size_t height, const size_t depth)
253 {
254  const size_t new_size = size(width, height, depth);
255 
256  if (new_size != data_size) {
257  device_free();
258  host_free();
260  assert(device_pointer == 0);
261  }
262 
263  data_size = new_size;
264  data_width = width;
266  data_depth = depth;
267 
268  info.width = width;
269  info.height = height;
270  info.depth = depth;
271 
272  return host_pointer;
273 }
274 
276 {
277  device_copy_to();
278 }
279 
unsigned char uchar
Definition: BLI_sys_types.h:86
unsigned int uint
Definition: BLI_sys_types.h:83
_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
_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 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
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
Definition: device.h:293
virtual bool is_resident(device_ptr, Device *sub_device)
Definition: device.h:452
virtual device_ptr mem_alloc_sub_ptr(device_memory &, int, int)
Definition: device.h:324
virtual void mem_zero(device_memory &mem)=0
virtual void mem_copy_from(device_memory &mem, int y, int w, int h, int elem)=0
virtual void mem_free_sub_ptr(device_ptr)
Definition: device.h:330
virtual void mem_free(device_memory &mem)=0
virtual void mem_copy_to(device_memory &mem)=0
virtual void mem_alloc(device_memory &mem)=0
MemoryType type
bool is_resident(Device *sub_device) const
size_t data_height
void * host_pointer
size_t original_device_size
DataType data_type
device_memory(Device *device, const char *name, MemoryType type)
void device_copy_from(int y, int w, int h, int elem)
device_ptr original_device_ptr
Device * original_device
size_t memory_size()
device_ptr device_pointer
void * shared_pointer
void * host_alloc(size_t size)
virtual ~device_memory()
Device * device
size_t device_size
void swap_device(Device *new_device, size_t new_device_size, device_ptr new_device_ptr)
device_ptr ptr
device_sub_ptr(device_memory &mem, int offset, int size)
TextureInfo info
size_t size(const size_t width, const size_t height, const size_t depth)
void * alloc(const size_t width, const size_t height, const size_t depth=0)
device_texture(Device *device, const char *name, const uint slot, ImageDataType image_data_type, InterpolationType interpolation, ExtensionType extension)
static size_t datatype_size(DataType datatype)
Definition: device_memory.h:57
MemoryType
Definition: device_memory.h:35
@ MEM_GLOBAL
Definition: device_memory.h:39
@ MEM_TEXTURE
Definition: device_memory.h:40
@ MEM_READ_ONLY
Definition: device_memory.h:36
@ TYPE_FLOAT
Definition: device_memory.h:52
@ TYPE_HALF
Definition: device_memory.h:53
@ TYPE_UINT16
Definition: device_memory.h:49
@ TYPE_UCHAR
Definition: device_memory.h:48
#define CCL_NAMESPACE_END
uint data_type
Definition: util_texture.h:99
uint interpolation
Definition: util_texture.h:103
void util_aligned_free(void *ptr)
CCL_NAMESPACE_BEGIN void * util_aligned_malloc(size_t size, int alignment)
#define MIN_ALIGNMENT_CPU_DATA_TYPES
void util_guarded_mem_free(size_t n)
void util_guarded_mem_alloc(size_t n)
ImageDataType
Definition: util_texture.h:51
@ IMAGE_DATA_NUM_TYPES
Definition: util_texture.h:63
@ IMAGE_DATA_TYPE_BYTE
Definition: util_texture.h:56
@ IMAGE_DATA_TYPE_FLOAT
Definition: util_texture.h:55
@ IMAGE_DATA_TYPE_FLOAT4
Definition: util_texture.h:52
@ IMAGE_DATA_TYPE_USHORT4
Definition: util_texture.h:58
@ IMAGE_DATA_TYPE_USHORT
Definition: util_texture.h:59
@ IMAGE_DATA_TYPE_NANOVDB_FLOAT
Definition: util_texture.h:60
@ IMAGE_DATA_TYPE_NANOVDB_FLOAT3
Definition: util_texture.h:61
@ IMAGE_DATA_TYPE_HALF
Definition: util_texture.h:57
@ IMAGE_DATA_TYPE_BYTE4
Definition: util_texture.h:53
@ IMAGE_DATA_TYPE_HALF4
Definition: util_texture.h:54
InterpolationType
Definition: util_texture.h:38
ExtensionType
Definition: util_texture.h:84
uint64_t device_ptr
Definition: util_types.h:62
PointerRNA * ptr
Definition: wm_files.c:3157