Merge branch 'master' into blender2.8
[blender.git] / intern / cycles / device / device_memory.cpp
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.h"
18 #include "device/device_memory.h"
19
20 CCL_NAMESPACE_BEGIN
21
22 /* Device Memory */
23
24 device_memory::device_memory(Device *device, const char *name, MemoryType type)
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   interpolation(INTERPOLATION_NONE),
35   extension(EXTENSION_REPEAT),
36   device(device),
37   device_pointer(0),
38   host_pointer(0),
39   shared_pointer(0)
40 {
41 }
42
43 device_memory::~device_memory()
44 {
45 }
46
47 void *device_memory::host_alloc(size_t size)
48 {
49         if(!size) {
50                 return 0;
51         }
52
53         void *ptr = util_aligned_malloc(size, MIN_ALIGNMENT_CPU_DATA_TYPES);
54
55         if(ptr) {
56                 util_guarded_mem_alloc(size);
57         }
58         else {
59                 throw std::bad_alloc();
60         }
61
62         return ptr;
63 }
64
65 void device_memory::host_free()
66 {
67         if(host_pointer) {
68                 util_guarded_mem_free(memory_size());
69                 util_aligned_free((void*)host_pointer);
70                 host_pointer = 0;
71         }
72 }
73
74 void device_memory::device_alloc()
75 {
76         assert(!device_pointer && type != MEM_TEXTURE);
77         device->mem_alloc(*this);
78 }
79
80 void device_memory::device_free()
81 {
82         if(device_pointer) {
83                 device->mem_free(*this);
84         }
85 }
86
87 void device_memory::device_copy_to()
88 {
89         if(host_pointer) {
90                 device->mem_copy_to(*this);
91         }
92 }
93
94 void device_memory::device_copy_from(int y, int w, int h, int elem)
95 {
96         assert(type != MEM_TEXTURE && type != MEM_READ_ONLY);
97         device->mem_copy_from(*this, y, w, h, elem);
98 }
99
100 void device_memory::device_zero()
101 {
102         if(data_size) {
103                 device->mem_zero(*this);
104         }
105 }
106
107 void device_memory::swap_device(Device *new_device,
108                                 size_t new_device_size,
109                                 device_ptr new_device_ptr)
110 {
111         original_device = device;
112         original_device_size = device_size;
113         original_device_ptr = device_pointer;
114
115         device = new_device;
116         device_size = new_device_size;
117         device_pointer = new_device_ptr;
118 }
119
120 void device_memory::restore_device()
121 {
122         device = original_device;
123         device_size = original_device_size;
124         device_pointer = original_device_ptr;
125 }
126
127 /* Device Sub Ptr */
128
129 device_sub_ptr::device_sub_ptr(device_memory& mem, int offset, int size)
130 : device(mem.device)
131 {
132         ptr = device->mem_alloc_sub_ptr(mem, offset, size);
133 }
134
135 device_sub_ptr::~device_sub_ptr()
136 {
137         device->mem_free_sub_ptr(ptr);
138 }
139
140 CCL_NAMESPACE_END