2017-08-01 12:30:58 +00:00
|
|
|
#ifndef B3_OPENCL_ARRAY_H
|
|
|
|
#define B3_OPENCL_ARRAY_H
|
|
|
|
|
|
|
|
#include "Bullet3Common/b3AlignedObjectArray.h"
|
|
|
|
#include "Bullet3OpenCL/Initialize/b3OpenCLInclude.h"
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
class b3OpenCLArray
|
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
size_t m_size;
|
|
|
|
size_t m_capacity;
|
|
|
|
cl_mem m_clBuffer;
|
2017-08-01 12:30:58 +00:00
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
cl_context m_clContext;
|
2017-08-01 12:30:58 +00:00
|
|
|
cl_command_queue m_commandQueue;
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
bool m_ownsMemory;
|
2017-08-01 12:30:58 +00:00
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
bool m_allowGrowingCapacity;
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
void deallocate()
|
|
|
|
{
|
|
|
|
if (m_clBuffer && m_ownsMemory)
|
|
|
|
{
|
|
|
|
clReleaseMemObject(m_clBuffer);
|
|
|
|
}
|
|
|
|
m_clBuffer = 0;
|
2019-01-03 13:26:51 +00:00
|
|
|
m_capacity = 0;
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
b3OpenCLArray<T>& operator=(const b3OpenCLArray<T>& src);
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
B3_FORCE_INLINE size_t allocSize(size_t size)
|
|
|
|
{
|
|
|
|
return (size ? size * 2 : 1);
|
|
|
|
}
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
public:
|
2019-01-03 13:26:51 +00:00
|
|
|
b3OpenCLArray(cl_context ctx, cl_command_queue queue, size_t initialCapacity = 0, bool allowGrowingCapacity = true)
|
|
|
|
: m_size(0), m_capacity(0), m_clBuffer(0), m_clContext(ctx), m_commandQueue(queue), m_ownsMemory(true), m_allowGrowingCapacity(true)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
if (initialCapacity)
|
|
|
|
{
|
|
|
|
reserve(initialCapacity);
|
|
|
|
}
|
|
|
|
m_allowGrowingCapacity = allowGrowingCapacity;
|
|
|
|
}
|
|
|
|
|
|
|
|
///this is an error-prone method with no error checking, be careful!
|
|
|
|
void setFromOpenCLBuffer(cl_mem buffer, size_t sizeInElements)
|
|
|
|
{
|
|
|
|
deallocate();
|
|
|
|
m_ownsMemory = false;
|
|
|
|
m_allowGrowingCapacity = false;
|
|
|
|
m_clBuffer = buffer;
|
|
|
|
m_size = sizeInElements;
|
|
|
|
m_capacity = sizeInElements;
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
// we could enable this assignment, but need to make sure to avoid accidental deep copies
|
|
|
|
// b3OpenCLArray<T>& operator=(const b3AlignedObjectArray<T>& src)
|
|
|
|
// {
|
|
|
|
// copyFromArray(src);
|
|
|
|
// return *this;
|
|
|
|
// }
|
2017-08-01 12:30:58 +00:00
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
cl_mem getBufferCL() const
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
return m_clBuffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~b3OpenCLArray()
|
|
|
|
{
|
|
|
|
deallocate();
|
2019-01-03 13:26:51 +00:00
|
|
|
m_size = 0;
|
|
|
|
m_capacity = 0;
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
B3_FORCE_INLINE bool push_back(const T& _Val, bool waitForCompletion = true)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
bool result = true;
|
|
|
|
size_t sz = size();
|
2019-01-03 13:26:51 +00:00
|
|
|
if (sz == capacity())
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
result = reserve(allocSize(size()));
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
copyFromHostPointer(&_Val, 1, sz, waitForCompletion);
|
|
|
|
m_size++;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
B3_FORCE_INLINE T forcedAt(size_t n) const
|
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
b3Assert(n >= 0);
|
|
|
|
b3Assert(n < capacity());
|
2017-08-01 12:30:58 +00:00
|
|
|
T elem;
|
2019-01-03 13:26:51 +00:00
|
|
|
copyToHostPointer(&elem, 1, n, true);
|
2017-08-01 12:30:58 +00:00
|
|
|
return elem;
|
|
|
|
}
|
|
|
|
|
|
|
|
B3_FORCE_INLINE T at(size_t n) const
|
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
b3Assert(n >= 0);
|
|
|
|
b3Assert(n < size());
|
2017-08-01 12:30:58 +00:00
|
|
|
T elem;
|
2019-01-03 13:26:51 +00:00
|
|
|
copyToHostPointer(&elem, 1, n, true);
|
2017-08-01 12:30:58 +00:00
|
|
|
return elem;
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
B3_FORCE_INLINE bool resize(size_t newsize, bool copyOldContents = true)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
bool result = true;
|
|
|
|
size_t curSize = size();
|
|
|
|
|
|
|
|
if (newsize < curSize)
|
|
|
|
{
|
|
|
|
//leave the OpenCL memory for now
|
2019-01-03 13:26:51 +00:00
|
|
|
}
|
|
|
|
else
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
if (newsize > size())
|
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
result = reserve(newsize, copyOldContents);
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//leave new data uninitialized (init in debug mode?)
|
|
|
|
//for (size_t i=curSize;i<newsize;i++) ...
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result)
|
|
|
|
{
|
|
|
|
m_size = newsize;
|
2019-01-03 13:26:51 +00:00
|
|
|
}
|
|
|
|
else
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
m_size = 0;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
B3_FORCE_INLINE size_t size() const
|
|
|
|
{
|
|
|
|
return m_size;
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
B3_FORCE_INLINE size_t capacity() const
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
return m_capacity;
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
B3_FORCE_INLINE bool reserve(size_t _Count, bool copyOldContents = true)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
bool result = true;
|
2017-08-01 12:30:58 +00:00
|
|
|
// determine new minimum length of allocated storage
|
|
|
|
if (capacity() < _Count)
|
2019-01-03 13:26:51 +00:00
|
|
|
{ // not enough room, reallocate
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
if (m_allowGrowingCapacity)
|
|
|
|
{
|
|
|
|
cl_int ciErrNum;
|
|
|
|
//create a new OpenCL buffer
|
2019-01-03 13:26:51 +00:00
|
|
|
size_t memSizeInBytes = sizeof(T) * _Count;
|
2017-08-01 12:30:58 +00:00
|
|
|
cl_mem buf = clCreateBuffer(m_clContext, CL_MEM_READ_WRITE, memSizeInBytes, NULL, &ciErrNum);
|
2019-01-03 13:26:51 +00:00
|
|
|
if (ciErrNum != CL_SUCCESS)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
b3Error("OpenCL out-of-memory\n");
|
|
|
|
_Count = 0;
|
|
|
|
result = false;
|
|
|
|
}
|
|
|
|
//#define B3_ALWAYS_INITIALIZE_OPENCL_BUFFERS
|
|
|
|
#ifdef B3_ALWAYS_INITIALIZE_OPENCL_BUFFERS
|
|
|
|
unsigned char* src = (unsigned char*)malloc(memSizeInBytes);
|
2019-01-03 13:26:51 +00:00
|
|
|
for (size_t i = 0; i < memSizeInBytes; i++)
|
2017-08-01 12:30:58 +00:00
|
|
|
src[i] = 0xbb;
|
2019-01-03 13:26:51 +00:00
|
|
|
ciErrNum = clEnqueueWriteBuffer(m_commandQueue, buf, CL_TRUE, 0, memSizeInBytes, src, 0, 0, 0);
|
|
|
|
b3Assert(ciErrNum == CL_SUCCESS);
|
2017-08-01 12:30:58 +00:00
|
|
|
clFinish(m_commandQueue);
|
|
|
|
free(src);
|
2019-01-03 13:26:51 +00:00
|
|
|
#endif //B3_ALWAYS_INITIALIZE_OPENCL_BUFFERS
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
if (result)
|
|
|
|
{
|
|
|
|
if (copyOldContents)
|
|
|
|
copyToCL(buf, size());
|
|
|
|
}
|
|
|
|
|
|
|
|
//deallocate the old buffer
|
|
|
|
deallocate();
|
|
|
|
|
|
|
|
m_clBuffer = buf;
|
|
|
|
|
|
|
|
m_capacity = _Count;
|
2019-01-03 13:26:51 +00:00
|
|
|
}
|
|
|
|
else
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
//fail: assert and
|
|
|
|
b3Assert(0);
|
|
|
|
deallocate();
|
2019-01-03 13:26:51 +00:00
|
|
|
result = false;
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
void copyToCL(cl_mem destination, size_t numElements, size_t firstElem = 0, size_t dstOffsetInElems = 0) const
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
if (numElements <= 0)
|
2017-08-01 12:30:58 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
b3Assert(m_clBuffer);
|
|
|
|
b3Assert(destination);
|
|
|
|
|
|
|
|
//likely some error, destination is same as source
|
|
|
|
b3Assert(m_clBuffer != destination);
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
b3Assert((firstElem + numElements) <= m_size);
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
cl_int status = 0;
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
b3Assert(numElements > 0);
|
|
|
|
b3Assert(numElements <= m_size);
|
2017-08-01 12:30:58 +00:00
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
size_t srcOffsetBytes = sizeof(T) * firstElem;
|
|
|
|
size_t dstOffsetInBytes = sizeof(T) * dstOffsetInElems;
|
2017-08-01 12:30:58 +00:00
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
status = clEnqueueCopyBuffer(m_commandQueue, m_clBuffer, destination,
|
|
|
|
srcOffsetBytes, dstOffsetInBytes, sizeof(T) * numElements, 0, 0, 0);
|
2017-08-01 12:30:58 +00:00
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
b3Assert(status == CL_SUCCESS);
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
void copyFromHost(const b3AlignedObjectArray<T>& srcArray, bool waitForCompletion = true)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
size_t newSize = srcArray.size();
|
|
|
|
|
|
|
|
bool copyOldContents = false;
|
2019-01-03 13:26:51 +00:00
|
|
|
resize(newSize, copyOldContents);
|
2017-08-01 12:30:58 +00:00
|
|
|
if (newSize)
|
2019-01-03 13:26:51 +00:00
|
|
|
copyFromHostPointer(&srcArray[0], newSize, 0, waitForCompletion);
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
void copyFromHostPointer(const T* src, size_t numElems, size_t destFirstElem = 0, bool waitForCompletion = true)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
b3Assert(numElems + destFirstElem <= capacity());
|
2017-08-01 12:30:58 +00:00
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
if (numElems + destFirstElem)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
cl_int status = 0;
|
2019-01-03 13:26:51 +00:00
|
|
|
size_t sizeInBytes = sizeof(T) * numElems;
|
|
|
|
status = clEnqueueWriteBuffer(m_commandQueue, m_clBuffer, 0, sizeof(T) * destFirstElem, sizeInBytes,
|
|
|
|
src, 0, 0, 0);
|
|
|
|
b3Assert(status == CL_SUCCESS);
|
2017-08-01 12:30:58 +00:00
|
|
|
if (waitForCompletion)
|
|
|
|
clFinish(m_commandQueue);
|
2019-01-03 13:26:51 +00:00
|
|
|
}
|
|
|
|
else
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
b3Error("copyFromHostPointer invalid range\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
void copyToHost(b3AlignedObjectArray<T>& destArray, bool waitForCompletion = true) const
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
destArray.resize(this->size());
|
|
|
|
if (size())
|
2019-01-03 13:26:51 +00:00
|
|
|
copyToHostPointer(&destArray[0], size(), 0, waitForCompletion);
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
void copyToHostPointer(T* destPtr, size_t numElem, size_t srcFirstElem = 0, bool waitForCompletion = true) const
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
b3Assert(numElem + srcFirstElem <= capacity());
|
2017-08-01 12:30:58 +00:00
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
if (numElem + srcFirstElem <= capacity())
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
cl_int status = 0;
|
2019-01-03 13:26:51 +00:00
|
|
|
status = clEnqueueReadBuffer(m_commandQueue, m_clBuffer, 0, sizeof(T) * srcFirstElem, sizeof(T) * numElem,
|
|
|
|
destPtr, 0, 0, 0);
|
|
|
|
b3Assert(status == CL_SUCCESS);
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
if (waitForCompletion)
|
|
|
|
clFinish(m_commandQueue);
|
2019-01-03 13:26:51 +00:00
|
|
|
}
|
|
|
|
else
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
b3Error("copyToHostPointer invalid range\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void copyFromOpenCLArray(const b3OpenCLArray& src)
|
|
|
|
{
|
|
|
|
size_t newSize = src.size();
|
|
|
|
resize(newSize);
|
|
|
|
if (size())
|
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
src.copyToCL(m_clBuffer, size());
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
#endif //B3_OPENCL_ARRAY_H
|