Commits

jeanphi  committed 3102327

Try to fix QBVH for 64bits.

Jeanphi

  • Participants
  • Parent commits f3bad75

Comments (0)

Files changed (2)

File accelerators/qbvhaccel.cpp

 	nPrims = vPrims.size();
 
 	// Temporary data for building
-	int *primsIndexes = new int[nPrims + 3]; // For the case where
+	u_long *primsIndexes = new u_long[nPrims + 3]; // For the case where
 	// the last quad would begin at the last primitive
 	// (or the second or third last primitive)
 
 	// it is not always the case => continue to use the normal bounds.
 	nNodes = 0;
 	nodes = AllocAligned<QBVHNode>(2 * nPrims - 1);
-	for (int i = 0; i < 2 * nPrims - 1; ++i)
+	for (u_long i = 0; i < 2 * nPrims - 1; ++i)
 		nodes[i] = QBVHNode();
 
 	// The arrays that will contain
 	BBox centroidsBbox;
 	
 	// Fill each base array
-	for (u_int i = 0; i < vPrims.size(); ++i) {
+	for (u_long i = 0; i < vPrims.size(); ++i) {
 		// This array will be reorganized during construction. 
 		primsIndexes[i] = i;
 
 }
 
 /***************************************************/
-void QBVHAccel::BuildTree(int start, int end, int *primsIndexes,
+void QBVHAccel::BuildTree(long start, long end, u_long *primsIndexes,
 	BBox *primsBboxes, Point *primsCentroids, const BBox &nodeBbox,
-	const BBox &centroidsBbox, int parentIndex, int childIndex, int depth)
+	const BBox &centroidsBbox, long parentIndex, long childIndex, int depth)
 {
 	// Create a leaf ?
 	//********
 		return;
 	}
 
-	int currentNode = parentIndex;
-	int leftChildIndex = childIndex;
-	int rightChildIndex = childIndex + 1;
+	long currentNode = parentIndex;
+	long leftChildIndex = childIndex;
+	long rightChildIndex = childIndex + 1;
 	
 	// Number of primitives in each bin
 	int bins[NB_BINS];
 	// Choose the split axis, taking the axis of maximum extent for the
 	// centroids (else weird cases can occur, where the maximum extent axis
 	// for the nodeBbox is an axis of 0 extent for the centroids one.).
-	int axis = centroidsBbox.MaximumExtent();
+	long axis = centroidsBbox.MaximumExtent();
 
 	// Precompute values that are constant with respect to the current
 	// primitive considered.
 		return;
 	}
 
-	for (int i = start; i < end; i += step) {
-		int primIndex = primsIndexes[i];
+	for (long i = start; i < end; i += step) {
+		long primIndex = primsIndexes[i];
 		
 		// Binning is relative to the centroids bbox and to the
 		// primitives' centroid.
 	BBox leftChildBbox, rightChildBbox;
 	BBox leftChildCentroidsBbox, rightChildCentroidsBbox;
 
-	int storeIndex = start;
-	for (int i = start; i < end; ++i) {
-		int primIndex = primsIndexes[i];
+	long storeIndex = start;
+	for (long i = start; i < end; ++i) {
+		long primIndex = primsIndexes[i];
 		
 		if (primsCentroids[primIndex][axis] <= splitPos) {
 			// Swap
 }
 
 /***************************************************/
-void QBVHAccel::CreateTempLeaf(int parentIndex, int childIndex,
-	int start, int end, const BBox &nodeBbox)
+void QBVHAccel::CreateTempLeaf(long parentIndex, long childIndex,
+	long start, long end, const BBox &nodeBbox)
 {
 	// The leaf is directly encoded in the intermediate node.
 	if (parentIndex == -1) {
 	// Encode the leaf in the original way,
 	// it will be transformed to a preswizzled format in a post-process.
 	
-	int nbPrimsTotal = end - start;
+	long nbPrimsTotal = end - start;
 	
 	QBVHNode &node = nodes[parentIndex];
 
 	}
 
 	// Next multiple of 4, divided by 4
-	int quads = ((nbPrimsTotal + 3) & ~3) >> 2;
+	long quads = ((nbPrimsTotal + 3) & ~3) >> 2;
 	
 	// Use the same encoding as the final one, but with a different meaning.
 	node.InitializeLeaf(childIndex, quads, start);
 	nQuads += quads;
 }
 
-void QBVHAccel::PreSwizzle(int nodeIndex, int *primsIndexes,
+void QBVHAccel::PreSwizzle(long nodeIndex, u_long *primsIndexes,
 	const vector<boost::shared_ptr<Primitive> > &vPrims)
 {
-	for (int i = 0; i < 4; ++i) {
+	for (long i = 0; i < 4; ++i) {
 		if (nodes[nodeIndex].ChildIsLeaf(i))
 			CreateSwizzledLeaf(nodeIndex, i, primsIndexes, vPrims);
 		else
 	}
 }
 
-void QBVHAccel::CreateSwizzledLeaf(int parentIndex, int childIndex,
-	int *primsIndexes, const vector<boost::shared_ptr<Primitive> > &vPrims)
+void QBVHAccel::CreateSwizzledLeaf(long parentIndex, long childIndex,
+	u_long *primsIndexes, const vector<boost::shared_ptr<Primitive> > &vPrims)
 {
 	QBVHNode &node = nodes[parentIndex];
 	if (node.LeafIsEmpty(childIndex))
 		return;
-	const int startQuad = nQuads;
-	const int nbQuads = node.NbQuadsInLeaf(childIndex);
+	const long startQuad = nQuads;
+	const long nbQuads = node.NbQuadsInLeaf(childIndex);
 
-	int primOffset = node.FirstQuadIndexForLeaf(childIndex);
-	int primNum = 4 * nQuads;
+	long primOffset = node.FirstQuadIndexForLeaf(childIndex);
+	long primNum = 4 * nQuads;
 
-	for (int q = 0; q < nbQuads; ++q) {
+	for (long q = 0; q < nbQuads; ++q) {
 		for (int i = 0; i < 4; ++i) {
 			new (&prims[primNum]) boost::shared_ptr<Primitive>(vPrims[primsIndexes[primOffset]]);
 			++primOffset;
 	node.InitializeLeaf(childIndex, nbQuads, startQuad);
 }
 
-int QBVHNode::BBoxIntersect(__m128 sseOrig[3], __m128 sseInvDir[3],
+long QBVHNode::BBoxIntersect(__m128 sseOrig[3], __m128 sseInvDir[3],
 	const __m128 &sseTMin, const __m128 &sseTMax,
 	const int sign[3]) const
 {
 	bool hit = false;
 	// The nodes stack, 256 nodes should be enough
 	int todoNode = 0; // the index in the stack
-	int nodeStack[256];
+	long nodeStack[256];
 	nodeStack[0] = 0; // first node to handle: root node
 	
 	while (todoNode >= 0) {
 			QBVHNode &node = nodes[nodeStack[todoNode]];
 			--todoNode;
 			
-			const int visit = node.BBoxIntersect(sseOrig, sseInvDir,
+			const long visit = node.BBoxIntersect(sseOrig, sseInvDir,
 				sseTMin, sseTMax, signs);
 
-			const int nodeIdx = (signs[node.axisMain] << 2) |
+			const long nodeIdx = (signs[node.axisMain] << 2) |
 				(signs[node.axisSubLeft] << 1) |
 				(signs[node.axisSubRight]);
 			
 			//----------------------
 			// It is a leaf,
 			// all the informations are encoded in the index
-			const int leafData = nodeStack[todoNode];
+			const long leafData = nodeStack[todoNode];
 			--todoNode;
 			
 			if (QBVHNode::IsEmpty(leafData))
 				continue;
 
 			// Perform intersection
-			const int nbQuadPrimitives = QBVHNode::NbQuadPrimitives(leafData);
+			const long nbQuadPrimitives = QBVHNode::NbQuadPrimitives(leafData);
 			
-			const int offset = QBVHNode::FirstQuadIndex(leafData);
+			const long offset = QBVHNode::FirstQuadIndex(leafData);
 
-			for (int primNumber = 4 * offset; primNumber < 4 * (offset + nbQuadPrimitives); ++primNumber) {
+			for (long primNumber = 4 * offset; primNumber < 4 * (offset + nbQuadPrimitives); ++primNumber) {
 				if (prims[primNumber]->Intersect(ray, isect))
 					hit = true;
 			}
 	// Main loop
 	// The nodes stack, 256 nodes should be enough
 	int todoNode = 0; // the index in the stack
-	int nodeStack[256];
+	long nodeStack[256];
 	nodeStack[0] = 0; // first node to handle: root node
 
 	int signs[3];
 			QBVHNode &node = nodes[nodeStack[todoNode]];
 			--todoNode;
 
-			const int visit = node.BBoxIntersect(sseOrig, sseInvDir,
+			const long visit = node.BBoxIntersect(sseOrig, sseInvDir,
 				sseTMin, sseTMax, signs);
 
-			const int nodeIdx = (signs[node.axisMain] << 2) |
+			const long nodeIdx = (signs[node.axisMain] << 2) |
 				(signs[node.axisSubLeft] << 1) |
 				(signs[node.axisSubRight]);
 			
 			//----------------------
 			// It is a leaf,
 			// all the informations are encoded in the index
-			const int leafData = nodeStack[todoNode];
+			const long leafData = nodeStack[todoNode];
 			--todoNode;
 			
 			if (QBVHNode::IsEmpty(leafData))
 				continue;
 
 			// Perform intersection
-			const int nbQuadPrimitives = QBVHNode::NbQuadPrimitives(leafData);
+			const long nbQuadPrimitives = QBVHNode::NbQuadPrimitives(leafData);
 			
-			const int offset = QBVHNode::FirstQuadIndex(leafData);
+			const long offset = QBVHNode::FirstQuadIndex(leafData);
 
-			for (int primNumber = 4 * offset; primNumber < 4 * (offset + nbQuadPrimitives); ++primNumber) {
+			for (long primNumber = 4 * offset; primNumber < 4 * (offset + nbQuadPrimitives); ++primNumber) {
 				if (prims[primNumber]->IntersectP(ray))
 					return true;
 			}
 /***************************************************/
 QBVHAccel::~QBVHAccel()
 {
-	for (int i = 0; i < nPrims; ++i)
+	for (u_long i = 0; i < nPrims; ++i)
 		prims[i].~shared_ptr();
 	FreeAligned(prims);
 	FreeAligned(nodes);
 void QBVHAccel::GetPrimitives(vector<boost::shared_ptr<Primitive> > &primitives)
 {
 	primitives.reserve(nPrims);
-	for(int i = 0; i < nPrims; ++i)
+	for(u_long i = 0; i < nPrims; ++i)
 		primitives.push_back(prims[i]);
 }
 

File accelerators/qbvhaccel.h

 	*/
 	inline QBVHNode() : axisMain(0), axisSubLeft(0), axisSubRight(0),
 		parentNodeIndex(-1) {
-		for (int i = 0; i < 3; ++i) {
+		for (long i = 0; i < 3; ++i) {
 			bboxes[0][i] = _mm_set1_ps(INFINITY);
 			bboxes[1][i] = _mm_set1_ps(-INFINITY);
 		}
 		
 		// All children are empty leaves by default
-		for (int i = 0; i < 4; ++i)
+		for (long i = 0; i < 4; ++i)
 			children[i] = emptyLeafNode;
 	}
 
 	   @param i
 	   @return
 	*/
-	inline bool ChildIsLeaf(int i) const {
+	inline bool ChildIsLeaf(long i) const {
 		return (children[i] < 0);
 	}
 
 	   Indicates whether the ith child is an empty leaf.
 	   @param i
 	*/
-	inline bool LeafIsEmpty(int i) const {
+	inline bool LeafIsEmpty(long i) const {
 		return (children[i] == emptyLeafNode);
 	}
 
 	   @param i
 	   @return
 	*/
-	inline int NbQuadsInLeaf(int i) const {
+	inline long NbQuadsInLeaf(long i) const {
 		return (((children[i] >> 27) & 15)) + 1;
 	}
 
 	   Return the number of group of 4 primitives, directly from the index.
 	   @param index
 	*/
-	inline static int NbQuadPrimitives(long index) {
+	inline static long NbQuadPrimitives(long index) {
 		return ((index >> 27) & 15) + 1;
 	}
 	
 	   @param i
 	   @return
 	*/
-	inline int NbPrimitivesInLeaf(int i) const {
+	inline long NbPrimitivesInLeaf(long i) const {
 		return NbQuadsInLeaf(i) * 4;
 	}
 
 	   @param i
 	   @return
 	*/
-	inline int FirstQuadIndexForLeaf(int i) const {
+	inline long FirstQuadIndexForLeaf(long i) const {
 		return children[i] & ~(31 << 27);
 	}
 	
 	   Same thing, directly from the index.
 	   @param index
 	*/
-	inline static int FirstQuadIndex(long index) {
+	inline static long FirstQuadIndex(long index) {
 		return index & ~(31 << 27);
 	}
 
  	   @param nbQuads
 	   @param firstQuadIndex
 	*/
-	inline void InitializeLeaf(int i, int nbQuads, int firstQuadIndex) {
+	inline void InitializeLeaf(long i, long nbQuads, long firstQuadIndex) {
 		// Take care to make a valid initialisation of the leaf.
 		if (nbQuads == 0) {
 			children[i] = emptyLeafNode;
 	   @param i
 	   @param bbox
 	*/
-	inline void SetBBox(int i, const BBox &bbox) {
-		for (int axis = 0; axis < 3; ++axis) {
+	inline void SetBBox(long i, const BBox &bbox) {
+		for (long axis = 0; axis < 3; ++axis) {
 			((float *)&(bboxes[0][axis]))[i] = bbox.pMin[axis];
 			((float *)&(bboxes[1][axis]))[i] = bbox.pMax[axis];
 		}
 	   @return an int used to index the array of paths in the bboxes
 	   (the visit array)
 	*/
-	int BBoxIntersect(__m128 sseOrig[3], __m128 sseInvDir[3],
+	long BBoxIntersect(__m128 sseOrig[3], __m128 sseInvDir[3],
 		const __m128 &sseTMin, const __m128 &sseTMax,
 		const int sign[3]) const;
 
 	   (its child number)
 	   @param depth the current depth.
 	*/
-	void BuildTree(int start, int end, int *primsIndexes, BBox *primsBboxes,
+	void BuildTree(long start, long end, u_long *primsIndexes, BBox *primsBboxes,
 		Point *primsCentroids, const BBox &nodeBbox,
-		const BBox &centroidsBbox, int parentIndex, int childIndex,
+		const BBox &centroidsBbox, long parentIndex, long childIndex,
 		int depth);
 	
 	/**
 	   @param end
 	   @param nodeBbox
 	*/
-	void CreateTempLeaf(int parentIndex, int childIndex, int start, int end,
+	void CreateTempLeaf(long parentIndex, long childIndex, long start, long end,
 		const BBox &nodeBbox);
 
 	/**
 	   @param childIndex
 	   @param nodeBbox
 	*/
-	inline int CreateIntermediateNode(int parentIndex, int childIndex,
+	inline long CreateIntermediateNode(long parentIndex, long childIndex,
 		const BBox &nodeBbox) {
-		int index = nNodes++; // increment after assignment
+		long index = nNodes++; // increment after assignment
 		nodes[index].parentNodeIndex = parentIndex;
 		if (parentIndex != -1) {
 			nodes[parentIndex].children[childIndex] = index;
 	   @param primsIndexes
 	   @param vPrims
 	*/
-	void PreSwizzle(int nodeIndex, int *primsIndexes,
+	void PreSwizzle(long nodeIndex, u_long *primsIndexes,
 		const vector<boost::shared_ptr<Primitive> > &vPrims);
 
 	/**
 	   @param primsIndexes
 	   @param vPrims
 	*/
-	void CreateSwizzledLeaf(int parentIndex, int childIndex, 
-		int *primsIndexes, const vector<boost::shared_ptr<Primitive> > &vPrims);
+	void CreateSwizzledLeaf(long parentIndex, long childIndex, 
+		u_long *primsIndexes, const vector<boost::shared_ptr<Primitive> > &vPrims);
 
 	/**
 	   the actual number of quads
 	*/
-	int nQuads;
+	long nQuads;
 
 	/**
 	   The primitive associated with each triangle. indexed by the number of quad
 	/**
 	   The number of primitives
 	*/
-	int nPrims;
+	u_long nPrims;
 
 	/**
 	   The nodes of the QBVH.
 	/**
 	   The number of nodes really used.
 	*/
-	int nNodes;
+	long nNodes;
 
 	/**
 	   The world bounding box of the QBVH.