Code refactor: move more memory allocation logic into device API.
[blender-staging.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_pointer(0),
28   data_size(0),
29   device_size(0),
30   data_width(0),
31   data_height(0),
32   data_depth(0),
33   type(type),
34   name(name),
35   interpolation(INTERPOLATION_NONE),
36   extension(EXTENSION_REPEAT),
37   device(device),
38   device_pointer(0)
39 {
40 }
41
42 device_memory::~device_memory()
43 {
44 }
45
46 device_ptr device_memory::host_alloc(size_t size)
47 {
48         if(!size) {
49                 return 0;
50         }
51
52         size_t alignment = device->mem_address_alignment();
53         device_ptr ptr = (device_ptr)util_aligned_malloc(size, alignment);
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(device_ptr ptr, size_t size)
66 {
67         if(ptr) {
68                 util_guarded_mem_free(size);
69                 util_aligned_free((void*)ptr);
70         }
71 }
72
73 void device_memory::device_alloc()
74 {
75         assert(!device_pointer && type != MEM_TEXTURE);
76         device->mem_alloc(*this);
77 }
78
79 void device_memory::device_free()
80 {
81         if(device_pointer) {
82                 device->mem_free(*this);
83         }
84 }
85
86 void device_memory::device_copy_to()
87 {
88         assert(type != MEM_PIXELS && type != MEM_WRITE_ONLY);
89         if(data_size) {
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         assert(type != MEM_PIXELS && type != MEM_WRITE_ONLY);
103         if(data_size) {
104                 device->mem_zero(*this);
105         }
106 }
107
108 /* Device Sub Ptr */
109
110 device_sub_ptr::device_sub_ptr(device_memory& mem, int offset, int size)
111 : device(mem.device)
112 {
113         ptr = device->mem_alloc_sub_ptr(mem, offset, size);
114 }
115
116 device_sub_ptr::~device_sub_ptr()
117 {
118         device->mem_free_sub_ptr(ptr);
119 }
120
121 CCL_NAMESPACE_END
122