30 #define BT_SUPPRESS_OPENCL_ASSERTS
36 #include <OpenCL/OpenCL.h>
42 #define BT_DEFAULT_WORKGROUPSIZE 64
45 #define RELEASE_CL_KERNEL(kernelName) {if( kernelName ){ clReleaseKernel( kernelName ); kernelName = 0; }}
51 #define MSTRINGIFY(A) #A
53 #include "OpenCLC10/PrepareLinks.cl"
55 #include "OpenCLC10/UpdatePositionsFromVelocities.cl"
56 static const char* SolvePositionsCLString =
57 #include "OpenCLC10/SolvePositions.cl"
58 static const char* UpdateNodesCLString =
59 #include "OpenCLC10/UpdateNodes.cl"
60 static const char* UpdatePositionsCLString =
61 #include "OpenCLC10/UpdatePositions.cl"
62 static const char* UpdateConstantsCLString =
63 #include "OpenCLC10/UpdateConstants.cl"
64 static const char* IntegrateCLString =
65 #include "OpenCLC10/Integrate.cl"
66 static const char* ApplyForcesCLString =
67 #include "OpenCLC10/ApplyForces.cl"
68 static const char* UpdateFixedVertexPositionsCLString =
69 #include "OpenCLC10/UpdateFixedVertexPositions.cl"
70 static const char* UpdateNormalsCLString =
71 #include "OpenCLC10/UpdateNormals.cl"
72 static const char* VSolveLinksCLString =
73 #include "OpenCLC10/VSolveLinks.cl"
74 static const char* SolveCollisionsAndUpdateVelocitiesCLString =
75 #include "OpenCLC10/SolveCollisionsAndUpdateVelocities.cl"
165 :m_cqCommandQue(queue),
166 m_clLinks( queue, ctx, &m_links, false ),
170 m_clLinksCLength( queue, ctx, &m_linksCLength, false ),
171 m_clLinksLengthRatio( queue, ctx, &m_linksLengthRatio, false ),
191 int newSize = previousSize + numLinks;
265 batchValues.
resize( numLinks, 0 );
269 for(
int linkIndex = 0; linkIndex < numLinks; ++linkIndex )
273 if( vertex0 > maxVertex )
275 if( vertex1 > maxVertex )
278 int numVertices = maxVertex + 1;
284 vertexConnectedColourLists.
resize(numVertices);
289 for(
int linkIndex = 0; linkIndex < numLinks; ++linkIndex )
310 batchValues[linkIndex] = colour;
315 for(
int i = 0; i < numLinks; ++i )
317 int batch = batchValues[i];
318 if( batch >= batchCounts.
size() )
321 ++(batchCounts[batch]);
331 for(
int batchIndex = 0; batchIndex < batchCounts.
size(); ++batchIndex )
335 sum += batchCounts[batchIndex];
353 for(
int batch = 0; batch < batchCounts.
size(); ++batch )
354 batchCounts[batch] = 0;
357 for(
int linkIndex = 0; linkIndex < numLinks; ++linkIndex )
368 int batch = batchValues[linkIndex];
371 batchCounts[batch] = batchCounts[batch] + 1;
372 m_links[newLocation] = m_links_Backup[linkLocation];
374 m_linkStrength[newLocation] = m_linkStrength_Backup[linkLocation];
375 m_linksMassLSC[newLocation] = m_linksMassLSC_Backup[linkLocation];
394 m_clVertexIndices( queue, ctx, &m_vertexIndices, false ),
395 m_clArea( queue, ctx, &m_area, false ),
396 m_clNormal( queue, ctx, &m_normal, false )
408 int newSize = previousSize + numTriangles;
464 if( numTriangles == 0 )
469 batchValues.
resize( numTriangles );
473 for(
int triangleIndex = 0; triangleIndex < numTriangles; ++triangleIndex )
479 if( vertex0 > maxVertex )
481 if( vertex1 > maxVertex )
483 if( vertex2 > maxVertex )
486 int numVertices = maxVertex + 1;
492 vertexConnectedColourLists.
resize(numVertices);
499 for(
int triangleIndex = 0; triangleIndex < numTriangles; ++triangleIndex )
532 batchValues[triangleIndex] = colour;
538 for(
int i = 0; i < numTriangles; ++i )
540 int batch = batchValues[i];
541 if( batch >= batchCounts.
size() )
544 ++(batchCounts[batch]);
553 for(
int batchIndex = 0; batchIndex < batchCounts.
size(); ++batchIndex )
557 sum += batchCounts[batchIndex];
569 for(
int batch = 0; batch < batchCounts.
size(); ++batch )
570 batchCounts[batch] = 0;
573 for(
int triangleIndex = 0; triangleIndex < numTriangles; ++triangleIndex )
584 int batch = batchValues[triangleIndex];
587 batchCounts[batch] = batchCounts[batch] + 1;
588 m_vertexIndices[newLocation] = m_vertexIndices_Backup[triangleLocation];
589 m_area[newLocation] = m_area_Backup[triangleLocation];
590 m_normal[newLocation] = m_normal_Backup[triangleLocation];
604 m_linkData(queue, ctx),
605 m_vertexData(queue, ctx),
606 m_triangleData(queue, ctx),
607 m_defaultCLFunctions(queue, ctx),
608 m_currentCLFunctions(&m_defaultCLFunctions),
609 m_clPerClothAcceleration(queue, ctx, &m_perClothAcceleration, true ),
610 m_clPerClothWindVelocity(queue, ctx, &m_perClothWindVelocity, true ),
611 m_clPerClothDampingFactor(queue,ctx, &m_perClothDampingFactor, true ),
612 m_clPerClothVelocityCorrectionCoefficient(queue, ctx,&m_perClothVelocityCorrectionCoefficient, true ),
613 m_clPerClothLiftFactor(queue, ctx,&m_perClothLiftFactor, true ),
614 m_clPerClothDragFactor(queue, ctx,&m_perClothDragFactor, true ),
615 m_clPerClothMediumDensity(queue, ctx,&m_perClothMediumDensity, true ),
616 m_clPerClothCollisionObjects( queue, ctx, &m_perClothCollisionObjects, true ),
617 m_clCollisionObjectDetails( queue, ctx, &m_collisionObjectDetails, true ),
618 m_clPerClothFriction( queue, ctx, &m_perClothFriction, false ),
619 m_clAnchorPosition( queue, ctx, &m_anchorPosition, true ),
620 m_clAnchorIndex( queue, ctx, &m_anchorIndex, true),
621 m_cqCommandQue( queue ),
622 m_cxMainContext(ctx),
624 m_bUpdateAnchoredNodePos(bUpdateAchchoredNodePos)
685 for(
int softBodyIndex = 0; softBodyIndex <
m_softBodySet.
size(); ++softBodyIndex )
694 for(
int vertex = 0; vertex < numVertices; ++vertex )
700 softBody->
m_nodes[vertex].m_x.setX( vertexPosition.getX() );
701 softBody->
m_nodes[vertex].m_x.setY( vertexPosition.getY() );
702 softBody->
m_nodes[vertex].m_x.setZ( vertexPosition.getZ() );
704 softBody->
m_nodes[vertex].m_n.setX( normal.getX() );
705 softBody->
m_nodes[vertex].m_n.setY( normal.getY() );
706 softBody->
m_nodes[vertex].m_n.setZ( normal.getZ() );
722 int maxPiterations = 0;
723 int maxViterations = 0;
725 for(
int softBodyIndex = 0; softBodyIndex < softBodies.
size(); ++softBodyIndex )
727 btSoftBody *softBody = softBodies[ softBodyIndex ];
749 int maxVertices = numVertices;
755 int maxTriangles = numTriangles;
759 for(
int vertex = 0; vertex < numVertices; ++vertex )
761 Point3 multPoint(softBody->
m_nodes[vertex].m_x.getX(), softBody->
m_nodes[vertex].m_x.getY(), softBody->
m_nodes[vertex].m_x.getZ());
769 float vertexInverseMass = softBody->
m_nodes[vertex].m_im;
770 desc.setInverseMass(vertexInverseMass);
778 for(
int triangle = 0; triangle < numTriangles; ++triangle )
782 int vertexIndex0 = (softBody->
m_faces[triangle].m_n[0] - &(softBody->
m_nodes[0]));
783 int vertexIndex1 = (softBody->
m_faces[triangle].m_n[1] - &(softBody->
m_nodes[0]));
784 int vertexIndex2 = (softBody->
m_faces[triangle].m_n[2] - &(softBody->
m_nodes[0]));
802 for(
int link = 0; link < numLinks; ++link )
804 int vertexIndex0 = softBody->
m_links[link].m_n[0] - &(softBody->
m_nodes[0]);
805 int vertexIndex1 = softBody->
m_links[link].m_n[1] - &(softBody->
m_nodes[0]);
824 if ( piterations > maxPiterations )
825 maxPiterations = piterations;
829 if ( viterations > maxViterations )
830 maxViterations = viterations;
833 for(
int vertex = 0; vertex < numVertices; ++vertex )
835 if ( softBody->
m_nodes[vertex].m_im == 0 )
846 if ( numVertices > 0 )
848 for (
int anchorIndex = 0; anchorIndex < softBody->
m_anchors.
size(); anchorIndex++ )
854 nodeInfo.
clVertexIndex = firstVertex + (int)(anchorNode - firstNode);
855 nodeInfo.
pNode = anchorNode;
881 for(
int softBodyIndex = 0; softBodyIndex <
m_softBodySet.
size(); ++softBodyIndex )
931 btAssert( 0 &&
"enqueueNDRangeKernel(m_resetNormalsAndAreasKernel)" );
951 btAssert( 0 &&
"enqueueNDRangeKernel(m_normalizeNormalsAndAreasKernel)");
973 btAssert( 0 &&
"enqueueNDRangeKernel(m_normalizeNormalsAndAreasKernel)");
980 using namespace Vectormath::Aos;
1015 float dtInverseMass = solverdt*inverseMass;
1020 vertexForce += force;
1044 btAssert( 0 &&
"enqueueNDRangeKernel(m_updateFixedVertexPositionsKernel)");
1064 float fl = FLT_EPSILON;
1084 btAssert( 0 &&
"enqueueNDRangeKernel(m_applyForcesKernel)");
1114 btAssert( 0 &&
"enqueueNDRangeKernel(m_integrateKernel)");
1128 float area =
length( crossProduct );
1135 for(
int softBodyIndex = 0; softBodyIndex <
m_softBodySet.
size(); ++softBodyIndex )
1137 btVector3 minBound(-1e30,-1e30,-1e30), maxBound(1e30,1e30,1e30);
1138 m_softBodySet[softBodyIndex]->updateBounds( minBound, maxBound );
1147 using namespace Vectormath::Aos;
1157 for(
int linkIndex = 0; linkIndex < numLinks; ++linkIndex )
1164 float massLSC = (invMass0 + invMass1)/linearStiffness;
1167 float restLengthSquared = restLength*restLength;
1210 int currentCloth = 0;
1215 if( nextCloth != currentCloth )
1221 currentCloth = nextCloth;
1222 startIndex = collisionObject;
1279 if( m_numberOfVelocityIterations > 0 )
1323 btAssert( 0 &&
"enqueueNDRangeKernel(m_prepareLinksKernel)");
1342 btAssert( 0 &&
"enqueueNDRangeKernel(m_updatePositionsFromVelocitiesKernel)");
1364 btAssert( 0 &&
"enqueueNDRangeKernel(m_solvePositionsFromLinksKernel)");
1386 btAssert( 0 &&
"enqueueNDRangeKernel(m_vSolveLinksKernel)");
1409 btAssert( 0 &&
"enqueueNDRangeKernel(m_updateVelocitiesFromPositionsWithVelocitiesKernel)");
1432 btAssert( 0 &&
"enqueueNDRangeKernel(m_updateVelocitiesFromPositionsWithoutVelocitiesKernel)");
1468 btAssert( 0 &&
"enqueueNDRangeKernel(m_updateVelocitiesFromPositionsWithoutVelocitiesKernel)");
1492 const int lastVertex = firstVertex + currentCloth->
getNumVertices();
1506 float *vertexPointer = basePointer + vertexOffset;
1508 for(
int vertexIndex = firstVertex; vertexIndex < lastVertex; ++vertexIndex )
1511 *(vertexPointer + 0) = position.
getX();
1512 *(vertexPointer + 1) = position.
getY();
1513 *(vertexPointer + 2) = position.
getZ();
1514 vertexPointer += vertexStride;
1521 float *normalPointer = basePointer + normalOffset;
1523 for(
int vertexIndex = firstVertex; vertexIndex < lastVertex; ++vertexIndex )
1526 *(normalPointer + 0) = normal.
getX();
1527 *(normalPointer + 1) = normal.
getY();
1528 *(normalPointer + 2) = normal.
getZ();
1529 normalPointer += normalStride;
1540 printf(
"compiling kernelName: %s ",kernelName);
1543 size_t program_length = strlen(kernelSource);
1552 char* flags =
"-cl-mad-enable -DMAC -DGUID_ARG";
1555 const char* flags =
"-DGUID_ARG= ";
1558 char* compileFlags =
new char[strlen(additionalMacros) + strlen(flags) + 5];
1559 sprintf(compileFlags,
"%s %s", flags, additionalMacros);
1560 ciErrNum =
clBuildProgram(m_cpProgram, 0, NULL, compileFlags, NULL, NULL);
1567 for(
int i = 0; i < 2; ++i )
1570 size_t ret_val_size;
1572 build_log =
new char[ret_val_size+1];
1577 build_log[ret_val_size] =
'\0';
1580 printf(
"Error in clBuildProgram, Line %u in file %s, Log: \n%s\n !!!\n\n", __LINE__, __FILE__, build_log);
1583 #ifndef BT_SUPPRESS_OPENCL_ASSERTS
1585 #endif //BT_SUPPRESS_OPENCL_ASSERTS
1586 m_kernelCompilationFailures++;
1595 const char* msg =
"";
1599 msg =
"Program is not a valid program object.";
1602 msg =
"There is no successfully built executable for program.";
1605 msg =
"kernel_name is not found in program.";
1608 msg =
"the function definition for __kernel function given by kernel_name such as the number of arguments, the argument types are not the same for all devices for which the program executable has been built.";
1611 msg =
"kernel_name is NULL.";
1614 msg =
"Failure to allocate resources required by the OpenCL implementation on the host.";
1621 printf(
"Error in clCreateKernel for kernel '%s', error is \"%s\", Line %u in file %s !!!\n\n", kernelName, msg, __LINE__, __FILE__);
1623 #ifndef BT_SUPPRESS_OPENCL_ASSERTS
1625 #endif //BT_SUPPRESS_OPENCL_ASSERTS
1626 m_kernelCompilationFailures++;
1630 printf(
"ready. \n");
1631 delete [] compileFlags;
1633 m_kernelCompilationFailures++;
1693 return outTransform;
1698 float scalarMargin = (float)getSoftBody()->getCollisionShape()->getMargin();
1699 btVector3 vectorMargin( scalarMargin, scalarMargin, scalarMargin );
1700 m_softBody->m_bounds[0] = lowerBound - vectorMargin;
1701 m_softBody->m_bounds[1] = upperBound + vectorMargin;
1714 if( softBodyIndex >= 0 )
1728 newCollisionShapeDescription.
radius = capsule->getRadius();
1729 newCollisionShapeDescription.
halfHeight = capsule->getHalfHeight();
1730 newCollisionShapeDescription.
margin = capsule->getMargin();
1731 newCollisionShapeDescription.
upAxis = capsule->getUpAxis();
1732 newCollisionShapeDescription.
friction = friction;
1741 printf(
"Unsupported collision shape type\n");
1746 btAssert(0 &&
"Unknown soft body");
1756 for(
int softBodyIndex = 0; softBodyIndex <
m_softBodySet.
size(); ++softBodyIndex )
1759 if( softBodyInterface->
getSoftBody() == softBody )
1760 return softBodyInterface;
1768 for(
int softBodyIndex = 0; softBodyIndex <
m_softBodySet.
size(); ++softBodyIndex )
1771 if( softBodyInterface->
getSoftBody() == softBody )
1772 return softBodyIndex;
1791 const char* additionalMacros=
"";