Bullet Collision Detection & Physics Library
btSoftBodySolverBuffer_OpenCL.h
Go to the documentation of this file.
1 /*
2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
4 
5 This software is provided 'as-is', without any express or implied warranty.
6 In no event will the authors be held liable for any damages arising from the use of this software.
7 Permission is granted to anyone to use this software for any purpose,
8 including commercial applications, and to alter it and redistribute it freely,
9 subject to the following restrictions:
10 
11 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
12 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
13 3. This notice may not be removed or altered from any source distribution.
14 */
15 
16 #ifndef BT_SOFT_BODY_SOLVER_BUFFER_OPENCL_H
17 #define BT_SOFT_BODY_SOLVER_BUFFER_OPENCL_H
18 
19 // OpenCL support
20 
21 #ifdef USE_MINICL
22  #include "MiniCL/cl.h"
23 #else //USE_MINICL
24  #ifdef __APPLE__
25  #include <OpenCL/OpenCL.h>
26  #else
27  #include <CL/cl.h>
28  #endif //__APPLE__
29 #endif//USE_MINICL
30 
31 #ifndef SAFE_RELEASE
32 #define SAFE_RELEASE(p) { if(p) { (p)->Release(); (p)=NULL; } }
33 #endif
34 
35 template <typename ElementType> class btOpenCLBuffer
36 {
37 public:
38 
42 
43 
44 
46 
47  int m_gpuSize;
48  bool m_onGPU;
51 
52 
53  bool createBuffer( cl_mem* preexistingBuffer = 0)
54  {
55 
56  cl_int err;
57 
58 
59  if( preexistingBuffer )
60  {
61  m_buffer = *preexistingBuffer;
62  }
63  else {
64 
66 
67  size_t size = m_CPUBuffer->size() * sizeof(ElementType);
68  // At a minimum the buffer must exist
69  if( size == 0 )
70  size = sizeof(ElementType);
71  m_buffer = clCreateBuffer(m_clContext, flags, size, 0, &err);
72  if( err != CL_SUCCESS )
73  {
74  btAssert( "Buffer::Buffer(m_buffer)");
75  }
76  }
77 
79 
80  return true;
81  }
82 
83 public:
85  :m_cqCommandQue(commandQue),
86  m_clContext(ctx),
87  m_buffer(0),
88  m_CPUBuffer(CPUBuffer),
89  m_gpuSize(0),
90  m_onGPU(false),
91  m_readOnlyOnGPU(readOnly),
92  m_allocated(false)
93  {
94  }
95 
97  {
99  }
100 
101 
102  bool moveToGPU()
103  {
104 
105 
106  cl_int err;
107 
108  if( (m_CPUBuffer->size() != m_gpuSize) )
109  {
110  m_onGPU = false;
111  }
112 
113  if( !m_allocated && m_CPUBuffer->size() == 0 )
114  {
115  // If it isn't on the GPU and yet there is no data on the CPU side this may cause a problem with some kernels.
116  // We should create *something* on the device side
117  if (!createBuffer()) {
118  return false;
119  }
120  m_allocated = true;
121  }
122 
123  if( !m_onGPU && m_CPUBuffer->size() > 0 )
124  {
125  if (!m_allocated || (m_CPUBuffer->size() != m_gpuSize)) {
126  if (!createBuffer()) {
127  return false;
128  }
129  m_allocated = true;
130  }
131 
132  size_t size = m_CPUBuffer->size() * sizeof(ElementType);
134  CL_FALSE,
135  0,
136  size,
137  &((*m_CPUBuffer)[0]),0,0,0);
138  if( err != CL_SUCCESS )
139  {
140  btAssert( "CommandQueue::enqueueWriteBuffer(m_buffer)" );
141  }
142 
143  m_onGPU = true;
144  }
145 
146  return true;
147 
148  }
149 
150  bool moveFromGPU()
151  {
152 
153  cl_int err;
154 
155  if (m_CPUBuffer->size() > 0) {
156  if (m_onGPU && !m_readOnlyOnGPU) {
157  size_t size = m_CPUBuffer->size() * sizeof(ElementType);
159  m_buffer,
160  CL_TRUE,
161  0,
162  size,
163  &((*m_CPUBuffer)[0]),0,0,0);
164 
165  if( err != CL_SUCCESS )
166  {
167  btAssert( "CommandQueue::enqueueReadBuffer(m_buffer)" );
168  }
169 
170  m_onGPU = false;
171  }
172  }
173 
174  return true;
175  }
176 
177  bool copyFromGPU()
178  {
179 
180  cl_int err;
181  size_t size = m_CPUBuffer->size() * sizeof(ElementType);
182 
183  if (m_CPUBuffer->size() > 0) {
184  if (m_onGPU && !m_readOnlyOnGPU) {
186  m_buffer,
187  CL_TRUE,
188  0,size,
189  &((*m_CPUBuffer)[0]),0,0,0);
190 
191  if( err != CL_SUCCESS )
192  {
193  btAssert( "CommandQueue::enqueueReadBuffer(m_buffer)");
194  }
195 
196  }
197  }
198 
199  return true;
200  }
201 
202  virtual void changedOnCPU()
203  {
204  m_onGPU = false;
205  }
206 }; // class btOpenCLBuffer
207 
208 
209 #endif // #ifndef BT_SOFT_BODY_SOLVER_BUFFER_OPENCL_H
struct _cl_context * cl_context
Definition: cl.h:41
CL_API_ENTRY cl_int CL_API_CALL clReleaseMemObject(cl_mem) CL_API_SUFFIX__VERSION_1_0
Definition: MiniCL.cpp:333
CL_API_ENTRY cl_int CL_API_CALL clEnqueueReadBuffer(cl_command_queue, cl_mem, cl_bool, size_t, size_t, void *, cl_uint, const cl_event *, cl_event *) CL_API_SUFFIX__VERSION_1_0
Definition: MiniCL.cpp:357
bool createBuffer(cl_mem *preexistingBuffer=0)
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition: btDbvt.cpp:51
#define btAssert(x)
Definition: btScalar.h:101
btAlignedObjectArray< ElementType > * m_CPUBuffer
CL_API_ENTRY cl_mem CL_API_CALL clCreateBuffer(cl_context, cl_mem_flags, size_t, void *, cl_int *) CL_API_SUFFIX__VERSION_1_0
Definition: MiniCL.cpp:598
int32_t cl_int
Definition: cl_platform.h:70
#define CL_TRUE
Definition: cl.h:144
#define CL_FALSE
Definition: cl.h:143
btOpenCLBuffer(cl_command_queue commandQue, cl_context ctx, btAlignedObjectArray< ElementType > *CPUBuffer, bool readOnly)
#define CL_SUCCESS
Definition: cl.h:91
int size() const
return the number of elements in the array
#define CL_MEM_READ_ONLY
Definition: cl.h:259
CL_API_ENTRY cl_int CL_API_CALL clEnqueueWriteBuffer(cl_command_queue, cl_mem, cl_bool, size_t, size_t, const void *, cl_uint, const cl_event *, cl_event *) CL_API_SUFFIX__VERSION_1_0
Definition: MiniCL.cpp:401
cl_command_queue m_cqCommandQue
struct _cl_mem * cl_mem
Definition: cl.h:43
struct _cl_command_queue * cl_command_queue
Definition: cl.h:42
#define CL_MEM_READ_WRITE
Definition: cl.h:257
cl_bitfield cl_mem_flags
Definition: cl.h:66