Bullet Collision Detection & Physics Library
btSoftBodySolverOutputCLtoGL.cpp
Go to the documentation of this file.
2 #include <stdio.h> //@todo: remove the debugging printf at some stage
7 
9 #define MSTRINGIFY(A) #A
11 #include "OpenCLC10/OutputToVertexArray.cl"
12 
13 
14 #define RELEASE_CL_KERNEL(kernelName) {if( kernelName ){ clReleaseKernel( kernelName ); kernelName = 0; }}
15 
16 static const size_t workGroupSize = 128;
17 
19 {
20 
21  btSoftBodySolver *solver = softBody->getSoftBodySolver();
23  btOpenCLSoftBodySolver *dxSolver = static_cast< btOpenCLSoftBodySolver * >( solver );
25  btOpenCLAcceleratedSoftBodyInterface* currentCloth = dxSolver->findSoftBodyInterface( softBody );
26  btSoftBodyVertexDataOpenCL &vertexData( dxSolver->m_vertexData );
27 
28  const int firstVertex = currentCloth->getFirstVertex();
29  const int lastVertex = firstVertex + currentCloth->getNumVertices();
30 
31  if( vertexBuffer->getBufferType() == btVertexBufferDescriptor::OPENGL_BUFFER ) {
32 
33  const btOpenGLInteropVertexBufferDescriptor *openGLVertexBuffer = static_cast< btOpenGLInteropVertexBufferDescriptor* >(vertexBuffer);
34  cl_int ciErrNum = CL_SUCCESS;
35 
36  cl_mem clBuffer = openGLVertexBuffer->getBuffer();
38  if( !vertexBuffer->hasNormals() )
40 
41  ciErrNum = clEnqueueAcquireGLObjects(m_cqCommandQue, 1, &clBuffer, 0, 0, NULL);
42  if( ciErrNum != CL_SUCCESS )
43  {
44  btAssert( 0 && "clEnqueueAcquireGLObjects(copySoftBodyToVertexBuffer)");
45  }
46 
47  int numVertices = currentCloth->getNumVertices();
48 
49  ciErrNum = clSetKernelArg(outputKernel, 0, sizeof(int), &firstVertex );
50  ciErrNum = clSetKernelArg(outputKernel, 1, sizeof(int), &numVertices );
51  ciErrNum = clSetKernelArg(outputKernel, 2, sizeof(cl_mem), (void*)&clBuffer );
52  if( vertexBuffer->hasVertexPositions() )
53  {
54  int vertexOffset = vertexBuffer->getVertexOffset();
55  int vertexStride = vertexBuffer->getVertexStride();
56  ciErrNum = clSetKernelArg(outputKernel, 3, sizeof(int), &vertexOffset );
57  ciErrNum = clSetKernelArg(outputKernel, 4, sizeof(int), &vertexStride );
58  ciErrNum = clSetKernelArg(outputKernel, 5, sizeof(cl_mem), (void*)&vertexData.m_clVertexPosition.m_buffer );
59 
60  }
61  if( vertexBuffer->hasNormals() )
62  {
63  int normalOffset = vertexBuffer->getNormalOffset();
64  int normalStride = vertexBuffer->getNormalStride();
65  ciErrNum = clSetKernelArg(outputKernel, 6, sizeof(int), &normalOffset );
66  ciErrNum = clSetKernelArg(outputKernel, 7, sizeof(int), &normalStride );
67  ciErrNum = clSetKernelArg(outputKernel, 8, sizeof(cl_mem), (void*)&vertexData.m_clVertexNormal.m_buffer );
68 
69  }
70  size_t numWorkItems = workGroupSize*((vertexData.getNumVertices() + (workGroupSize-1)) / workGroupSize);
71  ciErrNum = clEnqueueNDRangeKernel(m_cqCommandQue, outputKernel, 1, NULL, &numWorkItems, &workGroupSize,0 ,0 ,0);
72  if( ciErrNum != CL_SUCCESS )
73  {
74  btAssert( 0 && "enqueueNDRangeKernel(copySoftBodyToVertexBuffer)");
75  }
76 
77  ciErrNum = clEnqueueReleaseGLObjects(m_cqCommandQue, 1, &clBuffer, 0, 0, 0);
78  if( ciErrNum != CL_SUCCESS )
79  {
80  btAssert( 0 && "clEnqueueReleaseGLObjects(copySoftBodyToVertexBuffer)");
81  }
82  } else {
83  btAssert( "Undefined output for this solver output" == false );
84  }
85 
86  // clFinish in here may not be the best thing. It's possible that we should have a waitForFrameComplete function.
88 
89 } // btSoftBodySolverOutputCLtoGL::outputToVertexBuffers
90 
92 {
93  // Ensure current kernels are released first
95 
96  bool returnVal = true;
97 
99  return true;
100 
101  outputToVertexArrayWithNormalsKernel = clFunctions.compileCLKernelFromString( OutputToVertexArrayCLString, "OutputToVertexArrayWithNormalsKernel" ,"","OpenCLC10/OutputToVertexArray.cl");
102  outputToVertexArrayWithoutNormalsKernel = clFunctions.compileCLKernelFromString( OutputToVertexArrayCLString, "OutputToVertexArrayWithoutNormalsKernel" ,"","OpenCLC10/OutputToVertexArray.cl");
103 
104 
105  if( returnVal )
106  m_shadersInitialized = true;
107 
108  return returnVal;
109 } // btSoftBodySolverOutputCLtoGL::buildShaders
110 
112 {
115 
116  m_shadersInitialized = false;
117 } // btSoftBodySolverOutputCLtoGL::releaseKernels
118 
120 {
121  if( !m_shadersInitialized )
122  if( buildShaders() )
123  m_shadersInitialized = true;
124 
125  return m_shadersInitialized;
126 }
btOpenCLBuffer< Vectormath::Aos::Point3 > m_clVertexPosition
virtual bool hasVertexPositions() const
virtual BufferTypes getBufferType() const =0
Return the type of the vertex buffer descriptor.
#define btAssert(x)
Definition: btScalar.h:101
int32_t cl_int
Definition: cl_platform.h:70
virtual cl_kernel compileCLKernelFromString(const char *kernelSource, const char *kernelName, const char *additionalMacros, const char *srcFileNameForCaching)
Compile a compute shader kernel from a string and return the appropriate cl_kernel object...
SoftBody class to maintain information about a soft body instance within a solver.
CL_API_ENTRY cl_int CL_API_CALL clEnqueueNDRangeKernel(cl_command_queue, cl_kernel, cl_uint, const size_t *, const size_t *, const size_t *, cl_uint, const cl_event *, cl_event *) CL_API_SUFFIX__VERSION_1_0
Definition: MiniCL.cpp:429
static const size_t workGroupSize
#define CL_SUCCESS
Definition: cl.h:91
#define RELEASE_CL_KERNEL(kernelName)
CL_API_ENTRY cl_int CL_API_CALL clFinish(cl_command_queue) CL_API_SUFFIX__VERSION_1_0
Definition: MiniCL.cpp:749
virtual SolverTypes getSolverType() const =0
Return the type of the solver.
static char * OutputToVertexArrayCLString
struct _cl_kernel * cl_kernel
Definition: cl.h:45
btSoftBodySolver * getSoftBodySolver()
Definition: btSoftBody.h:913
CL_API_ENTRY cl_int CL_API_CALL clEnqueueReleaseGLObjects(cl_command_queue, cl_uint, const cl_mem *, cl_uint, const cl_event *, cl_event *) CL_API_SUFFIX__VERSION_1_0
struct _cl_mem * cl_mem
Definition: cl.h:43
CL_API_ENTRY cl_int CL_API_CALL clSetKernelArg(cl_kernel, cl_uint, size_t, const void *) CL_API_SUFFIX__VERSION_1_0
Definition: MiniCL.cpp:493
virtual int getVertexStride() const
Return the vertex stride in number of floats between vertices.
virtual int getVertexOffset() const
Return the vertex offset in floats from the base pointer.
virtual int getNormalOffset() const
Return the vertex offset in floats from the base pointer.
btOpenCLBuffer< Vectormath::Aos::Vector3 > m_clVertexNormal
CL_API_ENTRY cl_int CL_API_CALL clEnqueueAcquireGLObjects(cl_command_queue, cl_uint, const cl_mem *, cl_uint, const cl_event *, cl_event *) CL_API_SUFFIX__VERSION_1_0
virtual void copySoftBodyToVertexBuffer(const btSoftBody *const softBody, btVertexBufferDescriptor *vertexBuffer)
Output current computed vertex data to the vertex buffers for all cloths in the solver.
The btSoftBody is an class to simulate cloth and volumetric soft bodies.
Definition: btSoftBody.h:71
virtual int getNormalStride() const
Return the vertex stride in number of floats between vertices.