2011-04-27 11:58:34 +00:00
|
|
|
/*
|
2013-08-18 14:16:15 +00:00
|
|
|
* Copyright 2011-2013 Blender Foundation
|
2011-04-27 11:58:34 +00:00
|
|
|
*
|
2013-08-18 14:16:15 +00:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2011-04-27 11:58:34 +00:00
|
|
|
*
|
2013-08-18 14:16:15 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-04-27 11:58:34 +00:00
|
|
|
*
|
2013-08-18 14:16:15 +00:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
2014-12-25 01:50:24 +00:00
|
|
|
* limitations under the License.
|
2011-04-27 11:58:34 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __DEVICE_MEMORY_H__
|
|
|
|
#define __DEVICE_MEMORY_H__
|
|
|
|
|
|
|
|
/* Device Memory
|
|
|
|
*
|
|
|
|
* This file defines data types that can be used in device memory arrays, and
|
|
|
|
* a device_vector<T> type to store such arrays.
|
|
|
|
*
|
|
|
|
* device_vector<T> contains an STL vector, metadata about the data type,
|
|
|
|
* dimensions, elements, and a device pointer. For the CPU device this is just
|
|
|
|
* a pointer to the STL vector data, as no copying needs to take place. For
|
|
|
|
* other devices this is a pointer to device memory, where we will copy memory
|
|
|
|
* to and from. */
|
|
|
|
|
|
|
|
#include "util_debug.h"
|
2014-01-15 14:29:22 +00:00
|
|
|
#include "util_half.h"
|
2011-04-27 11:58:34 +00:00
|
|
|
#include "util_types.h"
|
|
|
|
#include "util_vector.h"
|
|
|
|
|
|
|
|
CCL_NAMESPACE_BEGIN
|
|
|
|
|
2012-01-04 18:06:32 +00:00
|
|
|
enum MemoryType {
|
|
|
|
MEM_READ_ONLY,
|
|
|
|
MEM_WRITE_ONLY,
|
|
|
|
MEM_READ_WRITE
|
|
|
|
};
|
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
/* Supported Data Types */
|
|
|
|
|
|
|
|
enum DataType {
|
|
|
|
TYPE_UCHAR,
|
|
|
|
TYPE_UINT,
|
|
|
|
TYPE_INT,
|
2013-08-30 23:49:38 +00:00
|
|
|
TYPE_FLOAT,
|
|
|
|
TYPE_HALF
|
2011-04-27 11:58:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static inline size_t datatype_size(DataType datatype)
|
|
|
|
{
|
|
|
|
switch(datatype) {
|
|
|
|
case TYPE_UCHAR: return sizeof(uchar);
|
|
|
|
case TYPE_FLOAT: return sizeof(float);
|
|
|
|
case TYPE_UINT: return sizeof(uint);
|
|
|
|
case TYPE_INT: return sizeof(int);
|
2013-08-30 23:49:38 +00:00
|
|
|
case TYPE_HALF: return sizeof(half);
|
2011-04-27 11:58:34 +00:00
|
|
|
default: return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Traits for data types */
|
|
|
|
|
|
|
|
template<typename T> struct device_type_traits {
|
|
|
|
static const DataType data_type = TYPE_UCHAR;
|
|
|
|
static const int num_elements = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<> struct device_type_traits<uchar> {
|
|
|
|
static const DataType data_type = TYPE_UCHAR;
|
|
|
|
static const int num_elements = 1;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<> struct device_type_traits<uchar2> {
|
|
|
|
static const DataType data_type = TYPE_UCHAR;
|
|
|
|
static const int num_elements = 2;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<> struct device_type_traits<uchar3> {
|
|
|
|
static const DataType data_type = TYPE_UCHAR;
|
|
|
|
static const int num_elements = 3;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<> struct device_type_traits<uchar4> {
|
|
|
|
static const DataType data_type = TYPE_UCHAR;
|
|
|
|
static const int num_elements = 4;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<> struct device_type_traits<uint> {
|
|
|
|
static const DataType data_type = TYPE_UINT;
|
|
|
|
static const int num_elements = 1;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<> struct device_type_traits<uint2> {
|
|
|
|
static const DataType data_type = TYPE_UINT;
|
|
|
|
static const int num_elements = 2;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<> struct device_type_traits<uint3> {
|
|
|
|
static const DataType data_type = TYPE_UINT;
|
|
|
|
static const int num_elements = 3;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<> struct device_type_traits<uint4> {
|
|
|
|
static const DataType data_type = TYPE_UINT;
|
|
|
|
static const int num_elements = 4;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<> struct device_type_traits<int> {
|
|
|
|
static const DataType data_type = TYPE_INT;
|
|
|
|
static const int num_elements = 1;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<> struct device_type_traits<int2> {
|
|
|
|
static const DataType data_type = TYPE_INT;
|
|
|
|
static const int num_elements = 2;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<> struct device_type_traits<int3> {
|
|
|
|
static const DataType data_type = TYPE_INT;
|
|
|
|
static const int num_elements = 3;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<> struct device_type_traits<int4> {
|
|
|
|
static const DataType data_type = TYPE_INT;
|
|
|
|
static const int num_elements = 4;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<> struct device_type_traits<float> {
|
|
|
|
static const DataType data_type = TYPE_FLOAT;
|
|
|
|
static const int num_elements = 1;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<> struct device_type_traits<float2> {
|
|
|
|
static const DataType data_type = TYPE_FLOAT;
|
|
|
|
static const int num_elements = 2;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<> struct device_type_traits<float3> {
|
|
|
|
static const DataType data_type = TYPE_FLOAT;
|
|
|
|
static const int num_elements = 3;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<> struct device_type_traits<float4> {
|
|
|
|
static const DataType data_type = TYPE_FLOAT;
|
|
|
|
static const int num_elements = 4;
|
|
|
|
};
|
|
|
|
|
2016-06-19 18:17:03 +00:00
|
|
|
template<> struct device_type_traits<half> {
|
|
|
|
static const DataType data_type = TYPE_HALF;
|
|
|
|
static const int num_elements = 1;
|
|
|
|
};
|
|
|
|
|
2013-08-30 23:49:38 +00:00
|
|
|
template<> struct device_type_traits<half4> {
|
|
|
|
static const DataType data_type = TYPE_HALF;
|
|
|
|
static const int num_elements = 4;
|
|
|
|
};
|
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
/* Device Memory */
|
|
|
|
|
|
|
|
class device_memory
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
size_t memory_size() { return data_size*data_elements*datatype_size(data_type); }
|
|
|
|
|
|
|
|
/* data information */
|
|
|
|
DataType data_type;
|
|
|
|
int data_elements;
|
|
|
|
device_ptr data_pointer;
|
|
|
|
size_t data_size;
|
2014-09-04 11:22:40 +00:00
|
|
|
size_t device_size;
|
2011-04-27 11:58:34 +00:00
|
|
|
size_t data_width;
|
|
|
|
size_t data_height;
|
2014-03-29 12:03:48 +00:00
|
|
|
size_t data_depth;
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
/* device pointer */
|
|
|
|
device_ptr device_pointer;
|
|
|
|
|
2017-02-22 12:32:57 +00:00
|
|
|
device_memory()
|
|
|
|
{
|
|
|
|
data_type = device_type_traits<uchar>::data_type;
|
|
|
|
data_elements = device_type_traits<uchar>::num_elements;
|
|
|
|
data_pointer = 0;
|
|
|
|
data_size = 0;
|
|
|
|
device_size = 0;
|
|
|
|
data_width = 0;
|
|
|
|
data_height = 0;
|
|
|
|
data_depth = 0;
|
|
|
|
device_pointer = 0;
|
|
|
|
}
|
2011-04-27 11:58:34 +00:00
|
|
|
virtual ~device_memory() { assert(!device_pointer); }
|
|
|
|
|
2017-02-22 12:32:57 +00:00
|
|
|
void resize(size_t size)
|
|
|
|
{
|
|
|
|
data_size = size;
|
|
|
|
data_width = size;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2011-04-27 11:58:34 +00:00
|
|
|
/* no copying */
|
|
|
|
device_memory(const device_memory&);
|
|
|
|
device_memory& operator = (const device_memory&);
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Device Vector */
|
|
|
|
|
|
|
|
template<typename T> class device_vector : public device_memory
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
device_vector()
|
|
|
|
{
|
|
|
|
data_type = device_type_traits<T>::data_type;
|
|
|
|
data_elements = device_type_traits<T>::num_elements;
|
|
|
|
|
|
|
|
assert(data_elements > 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~device_vector() {}
|
|
|
|
|
|
|
|
/* vector functions */
|
2014-03-29 12:03:48 +00:00
|
|
|
T *resize(size_t width, size_t height = 0, size_t depth = 0)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
2014-03-29 12:03:48 +00:00
|
|
|
data_size = width * ((height == 0)? 1: height) * ((depth == 0)? 1: depth);
|
2015-09-04 07:38:10 +00:00
|
|
|
if(data.resize(data_size) == NULL) {
|
|
|
|
clear();
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-04-27 11:58:34 +00:00
|
|
|
data_width = width;
|
|
|
|
data_height = height;
|
2014-03-29 12:03:48 +00:00
|
|
|
data_depth = depth;
|
2015-06-30 21:23:38 +00:00
|
|
|
if(data_size == 0) {
|
|
|
|
data_pointer = 0;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
data_pointer = (device_ptr)&data[0];
|
2011-04-27 11:58:34 +00:00
|
|
|
return &data[0];
|
|
|
|
}
|
|
|
|
|
2014-03-29 12:03:48 +00:00
|
|
|
T *copy(T *ptr, size_t width, size_t height = 0, size_t depth = 0)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
2014-03-29 12:03:48 +00:00
|
|
|
T *mem = resize(width, height, depth);
|
2015-09-04 07:38:10 +00:00
|
|
|
if(mem != NULL) {
|
|
|
|
memcpy(mem, ptr, memory_size());
|
|
|
|
}
|
2011-04-27 11:58:34 +00:00
|
|
|
return mem;
|
|
|
|
}
|
|
|
|
|
2013-04-01 20:26:43 +00:00
|
|
|
void copy_at(T *ptr, size_t offset, size_t size)
|
|
|
|
{
|
|
|
|
if(size > 0) {
|
|
|
|
size_t mem_size = size*data_elements*datatype_size(data_type);
|
|
|
|
memcpy(&data[0] + offset, ptr, mem_size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-29 12:03:48 +00:00
|
|
|
void reference(T *ptr, size_t width, size_t height = 0, size_t depth = 0)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
|
|
|
data.clear();
|
2014-03-29 12:03:48 +00:00
|
|
|
data_size = width * ((height == 0)? 1: height) * ((depth == 0)? 1: depth);
|
2011-04-27 11:58:34 +00:00
|
|
|
data_pointer = (device_ptr)ptr;
|
|
|
|
data_width = width;
|
|
|
|
data_height = height;
|
2014-03-29 12:03:48 +00:00
|
|
|
data_depth = depth;
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void clear()
|
|
|
|
{
|
|
|
|
data.clear();
|
|
|
|
data_pointer = 0;
|
|
|
|
data_width = 0;
|
|
|
|
data_height = 0;
|
2014-03-29 12:03:48 +00:00
|
|
|
data_depth = 0;
|
2011-04-27 11:58:34 +00:00
|
|
|
data_size = 0;
|
2017-02-22 12:32:57 +00:00
|
|
|
device_pointer = 0;
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t size()
|
|
|
|
{
|
|
|
|
return data.size();
|
|
|
|
}
|
|
|
|
|
2014-10-03 08:52:04 +00:00
|
|
|
T* get_data()
|
|
|
|
{
|
|
|
|
return &data[0];
|
|
|
|
}
|
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
private:
|
|
|
|
array<T> data;
|
|
|
|
};
|
|
|
|
|
|
|
|
CCL_NAMESPACE_END
|
|
|
|
|
|
|
|
#endif /* __DEVICE_MEMORY_H__ */
|
|
|
|
|