Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/physics/src/bullet/BulletMultiThreaded/SpuBatchRaycaster.cpp @ 1966

Last change on this file since 1966 was 1966, checked in by rgrieder, 16 years ago

Let's go for multithreaded physics!

  • Property svn:eol-style set to native
File size: 4.4 KB
Line 
1/*
2Bullet Continuous Collision Detection and Physics Library
3Copyright (c) 2003-2007 Erwin Coumans  http://bulletphysics.com
4
5This software is provided 'as-is', without any express or implied warranty.
6In no event will the authors be held liable for any damages arising from the use of this software.
7Permission is granted to anyone to use this software for any purpose,
8including commercial applications, and to alter it and redistribute it freely,
9subject to the following restrictions:
10
111. 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.
122. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
133. This notice may not be removed or altered from any source distribution.
14*/
15
16#include <new>
17#include "BulletCollision/CollisionShapes/btCollisionShape.h"
18#include "LinearMath/btAlignedAllocator.h"
19#include "SpuBatchRaycaster.h"
20
21SpuBatchRaycaster::SpuBatchRaycaster (class     btThreadSupportInterface* threadInterface, int maxNumOutstandingTasks)
22{
23        m_threadInterface = threadInterface;
24
25        castUponObjectWrappers = NULL;
26        numCastUponObjectWrappers = 0;
27
28        m_spuRaycastTaskProcess = new SpuRaycastTaskProcess(m_threadInterface,maxNumOutstandingTasks); // FIXME non constant
29}
30
31SpuBatchRaycaster::~SpuBatchRaycaster ()
32{
33        if (castUponObjectWrappers)
34        {
35                btAlignedFree (castUponObjectWrappers);
36                castUponObjectWrappers = NULL;
37        }
38}
39
40void
41SpuBatchRaycaster::setCollisionObjects (btCollisionObjectArray& castUponObjects, int numCastUponObjects)
42{
43        if (castUponObjectWrappers)
44        {
45                btAlignedFree (castUponObjectWrappers);
46                castUponObjectWrappers = NULL;
47        }
48
49        castUponObjectWrappers = (SpuCollisionObjectWrapper*)btAlignedAlloc (sizeof(SpuCollisionObjectWrapper) * numCastUponObjects,16);
50        numCastUponObjectWrappers = numCastUponObjects;
51
52        for (int i = 0; i < numCastUponObjectWrappers; i++)
53        {
54                castUponObjectWrappers[i] = SpuCollisionObjectWrapper(castUponObjects[i]);
55        }
56}
57
58void
59SpuBatchRaycaster::setCollisionObjectsSkipPE (btCollisionObjectArray& castUponObjects, int numCastUponObjects)
60{
61        if (castUponObjectWrappers)
62        {
63                btAlignedFree (castUponObjectWrappers);
64                castUponObjectWrappers = NULL;
65        }
66
67        int numNonPEShapes = 0;
68        for (int i = 0; i < numCastUponObjects; i++)
69        {
70                const btCollisionShape* shape = castUponObjects[i]->getCollisionShape();
71
72                if (shape->getShapeType () == BOX_SHAPE_PROXYTYPE ||
73                        shape->getShapeType () == SPHERE_SHAPE_PROXYTYPE ||
74                        shape->getShapeType () == CAPSULE_SHAPE_PROXYTYPE)
75                {
76                        continue;
77                }
78
79                numNonPEShapes++;
80        }
81
82        castUponObjectWrappers = (SpuCollisionObjectWrapper*)btAlignedAlloc (sizeof(SpuCollisionObjectWrapper) * numNonPEShapes,16);
83        numCastUponObjectWrappers = numNonPEShapes;
84
85        int index = 0;
86        for (int i = 0; i < numCastUponObjects; i++)
87        {
88                const btCollisionShape* shape = castUponObjects[i]->getCollisionShape();
89
90                if (shape->getShapeType () == BOX_SHAPE_PROXYTYPE ||
91                        shape->getShapeType () == SPHERE_SHAPE_PROXYTYPE ||
92                        shape->getShapeType () == CAPSULE_SHAPE_PROXYTYPE)
93                {
94                        continue;
95                }
96
97                castUponObjectWrappers[index] = SpuCollisionObjectWrapper(castUponObjects[i]);
98                index++;
99        }
100
101//      printf("Number of shapes bullet is casting against: %d\n", numNonPEShapes);
102        btAssert (index == numNonPEShapes);
103}
104
105void
106SpuBatchRaycaster::addRay (const btVector3& rayFrom, const btVector3& rayTo, const btScalar hitFraction)
107{
108        SpuRaycastTaskWorkUnitOut workUnitOut;
109        workUnitOut.hitFraction = hitFraction;
110        workUnitOut.hitNormal = btVector3(0.0, 1.0, 0.0);
111
112        rayBatchOutput.push_back (workUnitOut);
113
114        SpuRaycastTaskWorkUnit workUnit;
115        workUnit.rayFrom = rayFrom;
116        workUnit.rayTo = rayTo;
117        rayBatch.push_back (workUnit);
118}
119
120void
121SpuBatchRaycaster::clearRays ()
122{
123        rayBatch.clear ();
124        rayBatchOutput.clear ();
125}
126
127void
128SpuBatchRaycaster::performBatchRaycast ()
129{
130        m_spuRaycastTaskProcess->initialize2 (castUponObjectWrappers, numCastUponObjectWrappers);
131
132        for (int i = 0; i < rayBatch.size(); i++)
133        {
134                rayBatch[i].output = &rayBatchOutput[i]; // assign output memory location
135                m_spuRaycastTaskProcess->addWorkToTask(rayBatch[i]);
136        }
137
138        m_spuRaycastTaskProcess->flush2 ();
139}
140
141const SpuRaycastTaskWorkUnitOut&
142SpuBatchRaycaster::operator [] (int i) const
143{
144        return rayBatchOutput[i];
145}
146
147int
148SpuBatchRaycaster::getNumRays () const
149{
150        return rayBatchOutput.size();
151}
Note: See TracBrowser for help on using the repository browser.