2 Bullet Continuous Collision Detection and Physics Library
\r
3 Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
\r
5 This software is provided 'as-is', without any express or implied warranty.
\r
6 In no event will the authors be held liable for any damages arising from the use of this software.
\r
7 Permission is granted to anyone to use this software for any purpose,
\r
8 including commercial applications, and to alter it and redistribute it freely,
\r
9 subject to the following restrictions:
\r
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.
\r
12 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
\r
13 3. This notice may not be removed or altered from any source distribution.
\r
16 #ifndef BT_SOFT_BODY_SOLVER_BUFFER_OPENCL_H
\r
17 #define BT_SOFT_BODY_SOLVER_BUFFER_OPENCL_H
\r
22 #include "MiniCL/cl.h"
\r
25 #include <OpenCL/OpenCL.h>
\r
31 #ifndef SAFE_RELEASE
\r
32 #define SAFE_RELEASE(p) { if(p) { (p)->Release(); (p)=NULL; } }
\r
35 template <typename ElementType> class btOpenCLBuffer
\r
39 cl_command_queue m_cqCommandQue;
\r
40 cl_context m_clContext;
\r
45 btAlignedObjectArray< ElementType > * m_CPUBuffer;
\r
49 bool m_readOnlyOnGPU;
\r
53 bool createBuffer( cl_mem* preexistingBuffer = 0)
\r
59 if( preexistingBuffer )
\r
61 m_buffer = *preexistingBuffer;
\r
65 cl_mem_flags flags= m_readOnlyOnGPU ? CL_MEM_READ_ONLY : CL_MEM_READ_WRITE;
\r
67 size_t size = m_CPUBuffer->size() * sizeof(ElementType);
\r
68 // At a minimum the buffer must exist
\r
70 size = sizeof(ElementType);
\r
71 m_buffer = clCreateBuffer(m_clContext, flags, size, 0, &err);
\r
72 if( err != CL_SUCCESS )
\r
74 btAssert( "Buffer::Buffer(m_buffer)");
\r
78 m_gpuSize = m_CPUBuffer->size();
\r
84 btOpenCLBuffer( cl_command_queue commandQue,cl_context ctx, btAlignedObjectArray< ElementType >* CPUBuffer, bool readOnly)
\r
85 :m_cqCommandQue(commandQue),
\r
88 m_CPUBuffer(CPUBuffer),
\r
91 m_readOnlyOnGPU(readOnly),
\r
98 clReleaseMemObject(m_buffer);
\r
108 if( (m_CPUBuffer->size() != m_gpuSize) )
\r
113 if( !m_allocated && m_CPUBuffer->size() == 0 )
\r
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.
\r
116 // We should create *something* on the device side
\r
117 if (!createBuffer()) {
\r
120 m_allocated = true;
\r
123 if( !m_onGPU && m_CPUBuffer->size() > 0 )
\r
125 if (!m_allocated || (m_CPUBuffer->size() != m_gpuSize)) {
\r
126 if (!createBuffer()) {
\r
129 m_allocated = true;
\r
132 size_t size = m_CPUBuffer->size() * sizeof(ElementType);
\r
133 err = clEnqueueWriteBuffer(m_cqCommandQue,m_buffer,
\r
137 &((*m_CPUBuffer)[0]),0,0,0);
\r
138 if( err != CL_SUCCESS )
\r
140 btAssert( "CommandQueue::enqueueWriteBuffer(m_buffer)" );
\r
155 if (m_CPUBuffer->size() > 0) {
\r
156 if (m_onGPU && !m_readOnlyOnGPU) {
\r
157 size_t size = m_CPUBuffer->size() * sizeof(ElementType);
\r
158 err = clEnqueueReadBuffer(m_cqCommandQue,
\r
163 &((*m_CPUBuffer)[0]),0,0,0);
\r
165 if( err != CL_SUCCESS )
\r
167 btAssert( "CommandQueue::enqueueReadBuffer(m_buffer)" );
\r
181 size_t size = m_CPUBuffer->size() * sizeof(ElementType);
\r
183 if (m_CPUBuffer->size() > 0) {
\r
184 if (m_onGPU && !m_readOnlyOnGPU) {
\r
185 err = clEnqueueReadBuffer(m_cqCommandQue,
\r
189 &((*m_CPUBuffer)[0]),0,0,0);
\r
191 if( err != CL_SUCCESS )
\r
193 btAssert( "CommandQueue::enqueueReadBuffer(m_buffer)");
\r
202 virtual void changedOnCPU()
\r
206 }; // class btOpenCLBuffer
\r
209 #endif // #ifndef BT_SOFT_BODY_SOLVER_BUFFER_OPENCL_H