From 12700a7cd44f3f8f2a245d2b9d1782d988f62566 Mon Sep 17 00:00:00 2001 From: erwin coumans Date: Thu, 21 Mar 2013 17:36:52 -0700 Subject: [PATCH] added CompoundDemo --- btgui/OpenGLWindow/GLInstanceGraphicsShape.h | 22 ++ demo/gpudemo/GpuDemo.cpp | 4 +- demo/gpudemo/GpuDemo.h | 3 + demo/gpudemo/main_opengl3core.cpp | 4 +- demo/gpudemo/rigidbody/ConcaveScene.cpp | 36 +-- demo/gpudemo/rigidbody/GpuCompoundScene.cpp | 210 ++++++++++++++++++ demo/gpudemo/rigidbody/GpuCompoundScene.h | 27 +++ demo/gpudemo/rigidbody/GpuConvexScene.cpp | 2 +- opencl/gpu_rigidbody/host/btConfig.h | 4 +- .../host/btGpuBatchingPgsSolver.cpp | 4 +- .../gpu_rigidbody/host/btGpuNarrowPhase.cpp | 71 ++++++ 11 files changed, 355 insertions(+), 32 deletions(-) create mode 100644 btgui/OpenGLWindow/GLInstanceGraphicsShape.h create mode 100644 demo/gpudemo/rigidbody/GpuCompoundScene.cpp create mode 100644 demo/gpudemo/rigidbody/GpuCompoundScene.h diff --git a/btgui/OpenGLWindow/GLInstanceGraphicsShape.h b/btgui/OpenGLWindow/GLInstanceGraphicsShape.h new file mode 100644 index 000000000..4985944c4 --- /dev/null +++ b/btgui/OpenGLWindow/GLInstanceGraphicsShape.h @@ -0,0 +1,22 @@ +#ifndef GL_INSTANCE_GRAPHICS_SHAPE_H +#define GL_INSTANCE_GRAPHICS_SHAPE_H + +#include "BulletCommon/btAlignedObjectArray.h" + +struct GLInstanceVertex +{ + float xyzw[4]; + float normal[3]; + float uv[2]; +}; +struct GLInstanceGraphicsShape +{ + btAlignedObjectArray* m_vertices; + int m_numvertices; + btAlignedObjectArray* m_indices; + int m_numIndices; + float m_scaling[4]; +}; + +#endif //GL_INSTANCE_GRAPHICS_SHAPE_H + diff --git a/demo/gpudemo/GpuDemo.cpp b/demo/gpudemo/GpuDemo.cpp index f9c2a21c7..32805bedc 100644 --- a/demo/gpudemo/GpuDemo.cpp +++ b/demo/gpudemo/GpuDemo.cpp @@ -40,9 +40,9 @@ void GpuDemo::initCL(int preferredDeviceIndex, int preferredPlatformIndex) int ciErrNum = 0; //#ifdef CL_PLATFORM_INTEL - //cl_device_type deviceType = CL_DEVICE_TYPE_ALL; + cl_device_type deviceType = CL_DEVICE_TYPE_ALL; //#else - cl_device_type deviceType = CL_DEVICE_TYPE_GPU; + //cl_device_type deviceType = CL_DEVICE_TYPE_CPU; //#endif cl_platform_id platformId; diff --git a/demo/gpudemo/GpuDemo.h b/demo/gpudemo/GpuDemo.h index 1b3ba41db..149807b11 100644 --- a/demo/gpudemo/GpuDemo.h +++ b/demo/gpudemo/GpuDemo.h @@ -2,6 +2,9 @@ #define GPU_DEMO_H class GLInstancingRenderer; + + + class GpuDemo { protected: diff --git a/demo/gpudemo/main_opengl3core.cpp b/demo/gpudemo/main_opengl3core.cpp index a3e64a6d2..7cee56249 100644 --- a/demo/gpudemo/main_opengl3core.cpp +++ b/demo/gpudemo/main_opengl3core.cpp @@ -28,6 +28,7 @@ #include "rigidbody/GpuRigidBodyDemo.h" #include "rigidbody/ConcaveScene.h" #include "rigidbody/GpuConvexScene.h" +#include "rigidbody/GpuCompoundScene.h" //#include "BroadphaseBenchmark.h" @@ -63,9 +64,10 @@ btAlignedObjectArray demoNames; int selectedDemo = 0; GpuDemo::CreateFunc* allDemos[]= { + GpuCompoundScene::MyCreateFunc, ConcaveScene::MyCreateFunc, - GpuConvexScene::MyCreateFunc, + GpuRigidBodyDemo::MyCreateFunc, //BroadphaseBenchmark::CreateFunc, diff --git a/demo/gpudemo/rigidbody/ConcaveScene.cpp b/demo/gpudemo/rigidbody/ConcaveScene.cpp index f846f94b5..9e3da202c 100644 --- a/demo/gpudemo/rigidbody/ConcaveScene.cpp +++ b/demo/gpudemo/rigidbody/ConcaveScene.cpp @@ -19,24 +19,12 @@ #include"../../ObjLoader/objLoader.h" -struct GraphicsVertex -{ - float xyzw[4]; - float normal[3]; - float uv[2]; -}; -struct GraphicsShape -{ - btAlignedObjectArray* m_vertices; - int m_numvertices; - btAlignedObjectArray* m_indices; - int m_numIndices; - float m_scaling[4]; -}; +#include "OpenGLWindow/GLInstanceGraphicsShape.h" -GraphicsShape* createGraphicsShapeFromWavefrontObj(objLoader* obj) + +GLInstanceGraphicsShape* createGraphicsShapeFromWavefrontObj(objLoader* obj) { - btAlignedObjectArray* vertices = new btAlignedObjectArray; + btAlignedObjectArray* vertices = new btAlignedObjectArray; { // int numVertices = obj->vertexCount; // int numIndices = 0; @@ -81,7 +69,7 @@ GraphicsShape* createGraphicsShapeFromWavefrontObj(objLoader* obj) indicesPtr->push_back(vtxBaseIndex+1); indicesPtr->push_back(vtxBaseIndex+2); - GraphicsVertex vtx0; + GLInstanceVertex vtx0; vtx0.xyzw[0] = obj->vertexList[face->vertex_index[0]]->e[0]; vtx0.xyzw[1] = obj->vertexList[face->vertex_index[0]]->e[1]; vtx0.xyzw[2] = obj->vertexList[face->vertex_index[0]]->e[2]; @@ -90,7 +78,7 @@ GraphicsShape* createGraphicsShapeFromWavefrontObj(objLoader* obj) vtx0.uv[0] = 0.5f;//obj->textureList[face->vertex_index[0]]->e[0]; vtx0.uv[1] = 0.5f;//obj->textureList[face->vertex_index[0]]->e[1]; - GraphicsVertex vtx1; + GLInstanceVertex vtx1; vtx1.xyzw[0] = obj->vertexList[face->vertex_index[1]]->e[0]; vtx1.xyzw[1] = obj->vertexList[face->vertex_index[1]]->e[1]; vtx1.xyzw[2] = obj->vertexList[face->vertex_index[1]]->e[2]; @@ -98,7 +86,7 @@ GraphicsShape* createGraphicsShapeFromWavefrontObj(objLoader* obj) vtx1.uv[0] = 0.5f;//obj->textureList[face->vertex_index[1]]->e[0]; vtx1.uv[1] = 0.5f;//obj->textureList[face->vertex_index[1]]->e[1]; - GraphicsVertex vtx2; + GLInstanceVertex vtx2; vtx2.xyzw[0] = obj->vertexList[face->vertex_index[2]]->e[0]; vtx2.xyzw[1] = obj->vertexList[face->vertex_index[2]]->e[1]; vtx2.xyzw[2] = obj->vertexList[face->vertex_index[2]]->e[2]; @@ -134,7 +122,7 @@ GraphicsShape* createGraphicsShapeFromWavefrontObj(objLoader* obj) indicesPtr->push_back(vtxBaseIndex+2); indicesPtr->push_back(vtxBaseIndex+3); // - GraphicsVertex vtx3; + GLInstanceVertex vtx3; vtx3.xyzw[0] = obj->vertexList[face->vertex_index[3]]->e[0]; vtx3.xyzw[1] = obj->vertexList[face->vertex_index[3]]->e[1]; vtx3.xyzw[2] = obj->vertexList[face->vertex_index[3]]->e[2]; @@ -152,7 +140,7 @@ GraphicsShape* createGraphicsShapeFromWavefrontObj(objLoader* obj) } - GraphicsShape* gfxShape = new GraphicsShape; + GLInstanceGraphicsShape* gfxShape = new GLInstanceGraphicsShape; gfxShape->m_vertices = vertices; gfxShape->m_numvertices = vertices->size(); gfxShape->m_indices = indicesPtr; @@ -199,7 +187,7 @@ void ConcaveScene::setupScene(const ConstructionInfo& ci) int index=10; { - GraphicsShape* shape = createGraphicsShapeFromWavefrontObj(objData); + GLInstanceGraphicsShape* shape = createGraphicsShapeFromWavefrontObj(objData); btVector4 scaling(4,4,4,1); btAlignedObjectArray verts; @@ -214,7 +202,7 @@ void ConcaveScene::setupScene(const ConstructionInfo& ci) { int strideInBytes = 9*sizeof(float); int numVertices = sizeof(cube_vertices)/strideInBytes; - int numIndices = sizeof(cube_vertices)/sizeof(int); + int numIndices = sizeof(cube_indices)/sizeof(int); //int shapeId = ci.m_instancingRenderer->registerShape(&cube_vertices[0],numVertices,cube_indices,numIndices); //int shapeId = ci.m_instancingRenderer->registerShape(&cube_vertices[0],numVertices,cube_indices,numIndices); @@ -240,7 +228,7 @@ void ConcaveScene::setupScene(const ConstructionInfo& ci) int strideInBytes = 9*sizeof(float); int numVertices = sizeof(cube_vertices)/strideInBytes; - int numIndices = sizeof(cube_vertices)/sizeof(int); + int numIndices = sizeof(cube_indices)/sizeof(int); //int shapeId = ci.m_instancingRenderer->registerShape(&cube_vertices[0],numVertices,cube_indices,numIndices); int shapeId = ci.m_instancingRenderer->registerShape(&cube_vertices[0],numVertices,cube_indices,numIndices); int group=1; diff --git a/demo/gpudemo/rigidbody/GpuCompoundScene.cpp b/demo/gpudemo/rigidbody/GpuCompoundScene.cpp new file mode 100644 index 000000000..f0e62e0e5 --- /dev/null +++ b/demo/gpudemo/rigidbody/GpuCompoundScene.cpp @@ -0,0 +1,210 @@ +#include "GpuCompoundScene.h" +#include "GpuRigidBodyDemo.h" +#include "BulletCommon/btQuickprof.h" +#include "OpenGLWindow/ShapeData.h" + +#include "OpenGLWindow/GLInstancingRenderer.h" +#include "BulletCommon/btQuaternion.h" +#include "OpenGLWindow/btgWindowInterface.h" +#include "gpu_broadphase/host/btGpuSapBroadphase.h" +#include "../GpuDemoInternalData.h" +#include "basic_initialize/btOpenCLUtils.h" +#include "OpenGLWindow/OpenGLInclude.h" +#include "OpenGLWindow/GLInstanceRendererInternalData.h" +#include "parallel_primitives/host/btLauncherCL.h" +#include "gpu_rigidbody/host/btGpuRigidBodyPipeline.h" +#include "gpu_rigidbody/host/btGpuNarrowPhase.h" +#include "gpu_rigidbody/host/btConfig.h" +#include "GpuRigidBodyDemoInternalData.h" +#include "BulletCommon/btTransform.h" + +#include "OpenGLWindow/GLInstanceGraphicsShape.h" + + +#if 0 +GraphicsShape* createGraphicsShapeFromCompoundShape(btCompoundShape* compound) +{ + GraphicsShape* gfxShape = new GraphicsShape(); + btAlignedObjectArray* vertexArray = new btAlignedObjectArray; + btAlignedObjectArray* indexArray = new btAlignedObjectArray; + + + + //create a graphics shape for each child, combine them into a single graphics shape using their child transforms + for (int i=0;igetNumChildShapes();i++) + { + btAssert(compound->getChildShape(i)->isPolyhedral()); + if (compound->getChildShape(i)->isPolyhedral()) + { + btPolyhedralConvexShape* convexHull = (btPolyhedralConvexShape*) compound->getChildShape(i); + btTransform tr = compound->getChildTransform(i); + + const btConvexPolyhedron* polyhedron = convexHull->getConvexPolyhedron(); + GraphicsShape* childGfxShape = createGraphicsShapeFromConvexHull(polyhedron); + int baseIndex = vertexArray->size(); + + for (int j=0;jm_numIndices;j++) + indexArray->push_back(childGfxShape->m_indices[j]+baseIndex); + + GLInstanceVertex* orgVerts = (GLInstanceVertex*)childGfxShape->m_vertices; + + for (int j=0;jm_numvertices;j++) + { + GLInstanceVertex vtx; + btVector3 pos(orgVerts[j].xyzw[0],orgVerts[j].xyzw[1],orgVerts[j].xyzw[2]); + pos = tr*pos; + vtx.xyzw[0] = childGfxShape->m_scaling[0]*pos.x(); + vtx.xyzw[1] = childGfxShape->m_scaling[1]*pos.y(); + vtx.xyzw[2] = childGfxShape->m_scaling[2]*pos.z(); + vtx.xyzw[3] = 10.f; + + vtx.uv[0] = 0.5f; + vtx.uv[1] = 0.5f; + + btVector3 normal(orgVerts[j].normal[0],orgVerts[j].normal[1],orgVerts[j].normal[2]); + normal = tr.getBasis()*normal; + vtx.normal[0] = normal.x(); + vtx.normal[1] = normal.y(); + vtx.normal[2] = normal.z(); + vertexArray->push_back(vtx); + } + } + } + + btPolyhedralConvexShape* convexHull = (btPolyhedralConvexShape*) compound->getChildShape(0); + const btConvexPolyhedron* polyhedron = convexHull->getConvexPolyhedron(); + GraphicsShape* childGfxShape = createGraphicsShapeFromConvexHull(polyhedron); + + gfxShape->m_indices = &indexArray->at(0); + gfxShape->m_numIndices = indexArray->size(); + gfxShape->m_vertices = &vertexArray->at(0).xyzw[0]; + gfxShape->m_numvertices = vertexArray->size(); + gfxShape->m_scaling[0] = 1; + gfxShape->m_scaling[1] = 1; + gfxShape->m_scaling[2] = 1; + gfxShape->m_scaling[3] = 1; + + return gfxShape; +} +#endif + + + +void GpuCompoundScene::setupScene(const ConstructionInfo& ci) +{ + int strideInBytes = 9*sizeof(float); + int numVertices = sizeof(cube_vertices)/strideInBytes; + int numIndices = sizeof(cube_indices)/sizeof(int); + + btAlignedObjectArray vertexArray; + btAlignedObjectArray indexArray; + + + + + //int shapeId = ci.m_instancingRenderer->registerShape(&cube_vertices[0],numVertices,cube_indices,numIndices); + int group=1; + int mask=1; + int index=10; + float scaling[4] = {1,1,1,1}; + int colIndex = 0; + + GLInstanceVertex* cubeVerts = (GLInstanceVertex*)&cube_vertices[0]; + int stride2 = sizeof(GLInstanceVertex); + btAssert(stride2 == strideInBytes); + + { + int childColIndex = m_data->m_np->registerConvexHullShape(&cube_vertices[0],strideInBytes,numVertices, scaling); + + btVector3 childPositions[3] = { + btVector3(0,-2,0), + btVector3(0,0,0), + btVector3(0,2,0) + }; + + btAlignedObjectArray childShapes; + int numChildShapes = 3; + for (int i=0;im_np->registerCompoundShape(&childShapes); + + } + + int shapeId = ci.m_instancingRenderer->registerShape(&vertexArray[0].xyzw[0],vertexArray.size(),&indexArray[0],indexArray.size()); + + btVector4 colors[4] = + { + btVector4(1,0,0,1), + btVector4(0,1,0,1), + btVector4(0,0,1,1), + btVector4(0,1,1,1), + }; + + int curColor = 0; + for (int i=0;iregisterGraphicsInstance(shapeId,position,orn,color,scaling); + int pid = m_data->m_rigidBodyPipeline->registerPhysicsInstance(mass,position,orn,colIndex,index); + + index++; + } + } + } + + float camPos[4]={0,0,0};//65.5,4.5,65.5,0}; + //float camPos[4]={1,12.5,1.5,0}; + m_instancingRenderer->setCameraTargetPosition(camPos); + m_instancingRenderer->setCameraDistance(20); + +} + + + + diff --git a/demo/gpudemo/rigidbody/GpuCompoundScene.h b/demo/gpudemo/rigidbody/GpuCompoundScene.h new file mode 100644 index 000000000..f90b68bf9 --- /dev/null +++ b/demo/gpudemo/rigidbody/GpuCompoundScene.h @@ -0,0 +1,27 @@ +#ifndef GPU_COMPOUND_SCENE_H +#define GPU_COMPOUND_SCENE_H + +#include "GpuRigidBodyDemo.h" + +class GpuCompoundScene : public GpuRigidBodyDemo +{ +public: + + GpuCompoundScene(){} + virtual ~GpuCompoundScene(){} + virtual const char* getName() + { + return "GpuCompound"; + } + + static GpuDemo* MyCreateFunc() + { + GpuDemo* demo = new GpuCompoundScene; + return demo; + } + + virtual void setupScene(const ConstructionInfo& ci); + +}; + +#endif //GPU_COMPOUND_SCENE_H diff --git a/demo/gpudemo/rigidbody/GpuConvexScene.cpp b/demo/gpudemo/rigidbody/GpuConvexScene.cpp index 98cacdbf4..df9e69174 100644 --- a/demo/gpudemo/rigidbody/GpuConvexScene.cpp +++ b/demo/gpudemo/rigidbody/GpuConvexScene.cpp @@ -21,7 +21,7 @@ void GpuConvexScene::setupScene(const ConstructionInfo& ci) { int strideInBytes = 9*sizeof(float); int numVertices = sizeof(cube_vertices)/strideInBytes; - int numIndices = sizeof(cube_vertices)/sizeof(int); + int numIndices = sizeof(cube_indices)/sizeof(int); //int shapeId = ci.m_instancingRenderer->registerShape(&cube_vertices[0],numVertices,cube_indices,numIndices); int shapeId = ci.m_instancingRenderer->registerShape(&cube_vertices[0],numVertices,cube_indices,numIndices); int group=1; diff --git a/opencl/gpu_rigidbody/host/btConfig.h b/opencl/gpu_rigidbody/host/btConfig.h index 17cc98990..ff4365004 100644 --- a/opencl/gpu_rigidbody/host/btConfig.h +++ b/opencl/gpu_rigidbody/host/btConfig.h @@ -18,7 +18,7 @@ struct btConfig int m_maxTriConvexPairCapacity; btConfig() - :m_maxConvexBodies(128*1024), + :m_maxConvexBodies(32*1024), m_maxConvexShapes(8192), m_maxVerticesPerFace(64), m_maxFacesPerShape(64), @@ -26,7 +26,7 @@ struct btConfig m_maxConvexIndices(8192), m_maxConvexUniqueEdges(8192), m_maxCompoundChildShapes(8192), - m_maxTriConvexPairCapacity(256*1024) + m_maxTriConvexPairCapacity(32*1024) { m_maxBroadphasePairs = 16*m_maxConvexBodies; } diff --git a/opencl/gpu_rigidbody/host/btGpuBatchingPgsSolver.cpp b/opencl/gpu_rigidbody/host/btGpuBatchingPgsSolver.cpp index 170bc40df..d60fc1a23 100644 --- a/opencl/gpu_rigidbody/host/btGpuBatchingPgsSolver.cpp +++ b/opencl/gpu_rigidbody/host/btGpuBatchingPgsSolver.cpp @@ -35,7 +35,7 @@ enum }; -bool gpuBatchContacts = true; +bool gpuBatchContacts = false; bool gpuSolveConstraint = true; @@ -611,7 +611,7 @@ void btGpuBatchingPgsSolver::solveContacts(int numBodies, cl_mem bodyBuf, cl_mem numNonzeroGrid++; //printf("cpu batch\n"); - int simdWidth = -1; + int simdWidth = 32; int numBatches = sortConstraintByBatch( &cpuContacts[0]+offset, n, simdWidth,csCfg.m_staticIdx ,numBodies); // on GPU maxNumBatches = btMax(numBatches,maxNumBatches); diff --git a/opencl/gpu_rigidbody/host/btGpuNarrowPhase.cpp b/opencl/gpu_rigidbody/host/btGpuNarrowPhase.cpp index 306ce903e..e0304cc44 100644 --- a/opencl/gpu_rigidbody/host/btGpuNarrowPhase.cpp +++ b/opencl/gpu_rigidbody/host/btGpuNarrowPhase.cpp @@ -9,6 +9,7 @@ #include "btConfig.h" #include "../../gpu_sat/host/btOptimizedBvh.h" #include "../../gpu_sat/host/btTriangleIndexVertexArray.h" +#include "BulletGeometry/btAabbUtil2.h" struct btGpuNarrowPhaseInternalData { @@ -335,6 +336,76 @@ int btGpuNarrowPhase::registerConvexHullShape(btConvexUtility* utilPtr) } +int btGpuNarrowPhase::registerCompoundShape(btAlignedObjectArray* childShapes) +{ + + int collidableIndex = allocateCollidable(); + btCollidable& col = getCollidableCpu(collidableIndex); + col.m_shapeType = SHAPE_COMPOUND_OF_CONVEX_HULLS; + + col.m_shapeIndex = m_data->m_cpuChildShapes.size(); + { + btAssert(col.m_shapeIndex+childShapes->size()m_config.m_maxCompoundChildShapes); + for (int i=0;isize();i++) + { + m_data->m_cpuChildShapes.push_back(childShapes->at(i)); + } + //if writing the data directly is too slow, we can delay it and do it all at once in + m_data->m_gpuChildShapes->copyFromHost(m_data->m_cpuChildShapes); + } + + + + col.m_numChildShapes = childShapes->size(); + + + btSapAabb aabbWS; + btVector3 myAabbMin(1e30f,1e30f,1e30f); + btVector3 myAabbMax(-1e30f,-1e30f,-1e30f); + + //compute local AABB of the compound of all children + for (int i=0;isize();i++) + { + int childColIndex = childShapes->at(i).m_shapeIndex; + btCollidable& childCol = getCollidableCpu(childColIndex); + btSapAabb aabbLoc =m_data->m_localShapeAABBCPU->at(childColIndex); + + btVector3 childLocalAabbMin(aabbLoc.m_min[0],aabbLoc.m_min[1],aabbLoc.m_min[2]); + btVector3 childLocalAabbMax(aabbLoc.m_max[0],aabbLoc.m_max[1],aabbLoc.m_max[2]); + btVector3 aMin,aMax; + btScalar margin(0.f); + btTransform childTr; + childTr.setIdentity(); + + childTr.setOrigin(btVector3(childShapes->at(i).m_childPosition[0], + childShapes->at(i).m_childPosition[1], + childShapes->at(i).m_childPosition[2])); + childTr.setRotation(btQuaternion(childShapes->at(i).m_childOrientation[0], + childShapes->at(i).m_childOrientation[1], + childShapes->at(i).m_childOrientation[2], + childShapes->at(i).m_childOrientation[3])); + btTransformAabb(childLocalAabbMin,childLocalAabbMax,margin,childTr,aMin,aMax); + myAabbMin.setMin(aMin); + myAabbMax.setMax(aMax); + } + + aabbWS.m_min[0] = myAabbMin[0];//s_convexHeightField->m_aabb.m_min.x; + aabbWS.m_min[1]= myAabbMin[1];//s_convexHeightField->m_aabb.m_min.y; + aabbWS.m_min[2]= myAabbMin[2];//s_convexHeightField->m_aabb.m_min.z; + aabbWS.m_minIndices[3] = 0; + + aabbWS.m_max[0] = myAabbMax[0];//s_convexHeightField->m_aabb.m_max.x; + aabbWS.m_max[1]= myAabbMax[1];//s_convexHeightField->m_aabb.m_max.y; + aabbWS.m_max[2]= myAabbMax[2];//s_convexHeightField->m_aabb.m_max.z; + aabbWS.m_signedMaxIndices[3] = 0; + + m_data->m_localShapeAABBCPU->push_back(aabbWS); + m_data->m_localShapeAABBGPU->push_back(aabbWS); + clFinish(m_queue); + return collidableIndex; + +} + int btGpuNarrowPhase::registerConcaveMesh(btAlignedObjectArray* vertices, btAlignedObjectArray* indices,const float* scaling1) {