2
* Copyright 2011, Blender Foundation.
4
* This program is free software; you can redistribute it and/or
5
* modify it under the terms of the GNU General Public License
6
* as published by the Free Software Foundation; either version 2
7
* of the License, or (at your option) any later version.
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software Foundation,
16
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
#ifndef __DEVICE_MEMORY_H__
20
#define __DEVICE_MEMORY_H__
24
* This file defines data types that can be used in device memory arrays, and
25
* a device_vector<T> type to store such arrays.
27
* device_vector<T> contains an STL vector, metadata about the data type,
28
* dimensions, elements, and a device pointer. For the CPU device this is just
29
* a pointer to the STL vector data, as no copying needs to take place. For
30
* other devices this is a pointer to device memory, where we will copy memory
33
#include "util_debug.h"
34
#include "util_types.h"
35
#include "util_vector.h"
45
/* Supported Data Types */
54
static inline size_t datatype_size(DataType datatype)
57
case TYPE_UCHAR: return sizeof(uchar);
58
case TYPE_FLOAT: return sizeof(float);
59
case TYPE_UINT: return sizeof(uint);
60
case TYPE_INT: return sizeof(int);
65
/* Traits for data types */
67
template<typename T> struct device_type_traits {
68
static const DataType data_type = TYPE_UCHAR;
69
static const int num_elements = 0;
72
template<> struct device_type_traits<uchar> {
73
static const DataType data_type = TYPE_UCHAR;
74
static const int num_elements = 1;
77
template<> struct device_type_traits<uchar2> {
78
static const DataType data_type = TYPE_UCHAR;
79
static const int num_elements = 2;
82
template<> struct device_type_traits<uchar3> {
83
static const DataType data_type = TYPE_UCHAR;
84
static const int num_elements = 3;
87
template<> struct device_type_traits<uchar4> {
88
static const DataType data_type = TYPE_UCHAR;
89
static const int num_elements = 4;
92
template<> struct device_type_traits<uint> {
93
static const DataType data_type = TYPE_UINT;
94
static const int num_elements = 1;
97
template<> struct device_type_traits<uint2> {
98
static const DataType data_type = TYPE_UINT;
99
static const int num_elements = 2;
102
template<> struct device_type_traits<uint3> {
103
static const DataType data_type = TYPE_UINT;
104
static const int num_elements = 3;
107
template<> struct device_type_traits<uint4> {
108
static const DataType data_type = TYPE_UINT;
109
static const int num_elements = 4;
112
template<> struct device_type_traits<int> {
113
static const DataType data_type = TYPE_INT;
114
static const int num_elements = 1;
117
template<> struct device_type_traits<int2> {
118
static const DataType data_type = TYPE_INT;
119
static const int num_elements = 2;
122
template<> struct device_type_traits<int3> {
123
static const DataType data_type = TYPE_INT;
124
static const int num_elements = 3;
127
template<> struct device_type_traits<int4> {
128
static const DataType data_type = TYPE_INT;
129
static const int num_elements = 4;
132
template<> struct device_type_traits<float> {
133
static const DataType data_type = TYPE_FLOAT;
134
static const int num_elements = 1;
137
template<> struct device_type_traits<float2> {
138
static const DataType data_type = TYPE_FLOAT;
139
static const int num_elements = 2;
142
template<> struct device_type_traits<float3> {
143
static const DataType data_type = TYPE_FLOAT;
144
static const int num_elements = 3;
147
template<> struct device_type_traits<float4> {
148
static const DataType data_type = TYPE_FLOAT;
149
static const int num_elements = 4;
157
size_t memory_size() { return data_size*data_elements*datatype_size(data_type); }
159
/* data information */
162
device_ptr data_pointer;
168
device_ptr device_pointer;
172
virtual ~device_memory() { assert(!device_pointer); }
175
device_memory(const device_memory&);
176
device_memory& operator = (const device_memory&);
181
template<typename T> class device_vector : public device_memory
186
data_type = device_type_traits<T>::data_type;
187
data_elements = device_type_traits<T>::num_elements;
193
assert(data_elements > 0);
198
virtual ~device_vector() {}
200
/* vector functions */
201
T *resize(size_t width, size_t height = 0)
203
data_size = (height == 0)? width: width*height;
204
data.resize(data_size);
205
data_pointer = (device_ptr)&data[0];
207
data_height = height;
212
T *copy(T *ptr, size_t width, size_t height = 0)
214
T *mem = resize(width, height);
215
memcpy(mem, ptr, memory_size());
219
void reference(T *ptr, size_t width, size_t height = 0)
222
data_size = (height == 0)? width: width*height;
223
data_pointer = (device_ptr)ptr;
225
data_height = height;
249
#endif /* __DEVICE_MEMORY_H__ */