2016-07-19 17:35:25 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2015-2016 Mario Luzeiro <mrluzeiro@ua.pt>
|
2020-12-12 17:29:11 +00:00
|
|
|
* Copyright (C) 2015-2020 KiCad Developers, see AUTHORS.txt for contributors.
|
2016-07-19 17:35:25 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2021-01-03 22:23:00 +00:00
|
|
|
* @file bvh_packet_traversal.cpp
|
2021-06-09 19:32:58 +00:00
|
|
|
* @brief This file implements packet traversal over the BVH PBRT implementation.
|
2016-07-19 17:35:25 +00:00
|
|
|
*/
|
|
|
|
|
2021-01-03 22:23:00 +00:00
|
|
|
#include "bvh_pbrt.h"
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
#define BVH_RANGED_TRAVERSAL
|
|
|
|
//#define BVH_PARTITION_TRAVERSAL
|
|
|
|
|
|
|
|
|
|
|
|
#define MAX_TODOS 64
|
|
|
|
|
|
|
|
|
|
|
|
struct StackNode
|
|
|
|
{
|
|
|
|
int cell;
|
|
|
|
unsigned int ia; // Index to the first alive ray
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
static inline unsigned int getFirstHit( const RAYPACKET& aRayPacket, const BBOX_3D& aBBox,
|
|
|
|
unsigned int ia, HITINFO_PACKET* aHitInfoPacket )
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
|
|
|
float hitT;
|
|
|
|
|
2020-12-12 17:29:11 +00:00
|
|
|
if( aBBox.Intersect( aRayPacket.m_ray[ia], &hitT )
|
|
|
|
&& ( hitT < aHitInfoPacket[ia].m_HitInfo.m_tHit ) )
|
|
|
|
return ia;
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
if( !aRayPacket.m_Frustum.Intersect( aBBox ) )
|
|
|
|
return RAYPACKET_RAYS_PER_PACKET;
|
|
|
|
|
|
|
|
for( unsigned int i = ia + 1; i < RAYPACKET_RAYS_PER_PACKET; ++i )
|
|
|
|
{
|
2020-12-12 17:29:11 +00:00
|
|
|
if( aBBox.Intersect( aRayPacket.m_ray[i], &hitT )
|
|
|
|
&& ( hitT < aHitInfoPacket[i].m_HitInfo.m_tHit ) )
|
|
|
|
return i;
|
2016-07-19 17:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return RAYPACKET_RAYS_PER_PACKET;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef BVH_RANGED_TRAVERSAL
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
static inline unsigned int getLastHit( const RAYPACKET& aRayPacket, const BBOX_3D& aBBox,
|
|
|
|
unsigned int ia, HITINFO_PACKET* aHitInfoPacket )
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
|
|
|
for( unsigned int ie = (RAYPACKET_RAYS_PER_PACKET - 1); ie > ia; --ie )
|
|
|
|
{
|
|
|
|
float hitT;
|
|
|
|
|
2020-12-12 17:29:11 +00:00
|
|
|
if( aBBox.Intersect( aRayPacket.m_ray[ie], &hitT )
|
|
|
|
&& ( hitT < aHitInfoPacket[ie].m_HitInfo.m_tHit ) )
|
|
|
|
return ie + 1;
|
2016-07-19 17:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ia + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// "Large Ray Packets for Real-time Whitted Ray Tracing"
|
|
|
|
// http://cseweb.ucsd.edu/~ravir/whitted.pdf
|
|
|
|
|
|
|
|
// Ranged Traversal
|
2021-01-02 21:05:29 +00:00
|
|
|
bool BVH_PBRT::Intersect( const RAYPACKET& aRayPacket, HITINFO_PACKET* aHitInfoPacket ) const
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
2021-01-02 21:05:29 +00:00
|
|
|
if( m_nodes == nullptr )
|
2016-07-19 17:35:25 +00:00
|
|
|
return false;
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
if( &m_nodes[0] == nullptr )
|
2016-07-19 17:35:25 +00:00
|
|
|
return false;
|
|
|
|
|
2021-01-07 19:33:43 +00:00
|
|
|
bool anyHit = false;
|
2016-07-19 17:35:25 +00:00
|
|
|
int todoOffset = 0, nodeNum = 0;
|
|
|
|
StackNode todo[MAX_TODOS];
|
|
|
|
|
|
|
|
unsigned int ia = 0;
|
|
|
|
|
|
|
|
while( true )
|
|
|
|
{
|
|
|
|
const LinearBVHNode *curCell = &m_nodes[nodeNum];
|
|
|
|
|
|
|
|
ia = getFirstHit( aRayPacket, curCell->bounds, ia, aHitInfoPacket );
|
|
|
|
|
|
|
|
if( ia < RAYPACKET_RAYS_PER_PACKET )
|
|
|
|
{
|
|
|
|
if( curCell->nPrimitives == 0 )
|
|
|
|
{
|
2021-01-02 21:05:29 +00:00
|
|
|
StackNode& node = todo[todoOffset++];
|
2016-07-19 17:35:25 +00:00
|
|
|
node.cell = curCell->secondChildOffset;
|
|
|
|
node.ia = ia;
|
|
|
|
nodeNum = nodeNum + 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-01-02 21:05:29 +00:00
|
|
|
const unsigned int ie = getLastHit( aRayPacket, curCell->bounds, ia,
|
2016-07-19 17:35:25 +00:00
|
|
|
aHitInfoPacket );
|
|
|
|
|
|
|
|
for( int j = 0; j < curCell->nPrimitives; ++j )
|
|
|
|
{
|
2021-01-02 21:05:29 +00:00
|
|
|
const OBJECT_3D* obj = m_primitives[curCell->primitivesOffset + j];
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
if( aRayPacket.m_Frustum.Intersect( obj->GetBBox() ) )
|
|
|
|
{
|
|
|
|
for( unsigned int i = ia; i < ie; ++i )
|
|
|
|
{
|
2021-01-07 19:33:43 +00:00
|
|
|
const bool hit = obj->Intersect( aRayPacket.m_ray[i],
|
|
|
|
aHitInfoPacket[i].m_HitInfo );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2021-01-07 19:33:43 +00:00
|
|
|
if( hit )
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
2021-01-07 19:33:43 +00:00
|
|
|
anyHit |= hit;
|
|
|
|
aHitInfoPacket[i].m_hitresult |= hit;
|
2016-07-19 17:35:25 +00:00
|
|
|
aHitInfoPacket[i].m_HitInfo.m_acc_node_info = nodeNum;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( todoOffset == 0 )
|
|
|
|
break;
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
const StackNode& node = todo[--todoOffset];
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
nodeNum = node.cell;
|
|
|
|
ia = node.ia;
|
|
|
|
}
|
|
|
|
|
2021-01-07 19:33:43 +00:00
|
|
|
return anyHit;
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2021-01-07 19:33:43 +00:00
|
|
|
}
|
2016-07-19 17:35:25 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
// "Ray Tracing Deformable Scenes Using Dynamic Bounding Volume Hierarchies"
|
|
|
|
// http://www.cs.cmu.edu/afs/cs/academic/class/15869-f11/www/readings/wald07_packetbvh.pdf
|
|
|
|
|
|
|
|
#ifdef BVH_PARTITION_TRAVERSAL
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
static inline unsigned int getLastHit( const RAYPACKET& aRayPacket, const BBOX_3D& aBBox,
|
|
|
|
unsigned int ia, const unsigned int* aRayIndex,
|
|
|
|
HITINFO_PACKET* aHitInfoPacket )
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
|
|
|
for( unsigned int ie = (RAYPACKET_RAYS_PER_PACKET - 1); ie > ia; --ie )
|
|
|
|
{
|
|
|
|
float hitT;
|
|
|
|
|
2020-12-12 17:29:11 +00:00
|
|
|
if( aBBox.Intersect( aRayPacket.m_ray[ aRayIndex[ie] ], &hitT )
|
|
|
|
&& ( hitT < aHitInfoPacket[ aRayIndex[ie] ].m_HitInfo.m_tHit ) )
|
|
|
|
return ie + 1;
|
2016-07-19 17:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ia + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
static inline unsigned int partRays( const RAYPACKET& aRayPacket, const BBOX_3D& aBBox,
|
|
|
|
unsigned int ia, unsigned int* aRayIndex,
|
|
|
|
HITINFO_PACKET* aHitInfoPacket )
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
if( !aRayPacket.m_Frustum.Intersect( aBBox ) )
|
|
|
|
return RAYPACKET_RAYS_PER_PACKET;
|
|
|
|
|
|
|
|
unsigned int ie = 0;
|
|
|
|
|
|
|
|
for( unsigned int i = 0; i < ia; ++i )
|
|
|
|
{
|
|
|
|
float hitT;
|
2021-01-02 21:05:29 +00:00
|
|
|
|
2020-12-12 17:29:11 +00:00
|
|
|
if( aBBox.Intersect( aRayPacket.m_ray[ aRayIndex[i] ], &hitT )
|
|
|
|
&& ( hitT < aHitInfoPacket[ aRayIndex[i] ].m_HitInfo.m_tHit ) )
|
|
|
|
std::swap( aRayIndex[ie++], aRayIndex[i] );
|
2016-07-19 17:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ie;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
bool BVH_PBRT::Intersect( const RAYPACKET& aRayPacket, HITINFO_PACKET* aHitInfoPacket ) const
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
2021-01-07 19:33:43 +00:00
|
|
|
bool anyHit = false;
|
2016-07-19 17:35:25 +00:00
|
|
|
int todoOffset = 0, nodeNum = 0;
|
|
|
|
StackNode todo[MAX_TODOS];
|
|
|
|
|
|
|
|
unsigned int I[RAYPACKET_RAYS_PER_PACKET];
|
|
|
|
|
|
|
|
memcpy( I, m_I, RAYPACKET_RAYS_PER_PACKET * sizeof( unsigned int ) );
|
|
|
|
|
|
|
|
unsigned int ia = 0;
|
|
|
|
|
|
|
|
while( true )
|
|
|
|
{
|
|
|
|
const LinearBVHNode *curCell = &m_nodes[nodeNum];
|
|
|
|
|
|
|
|
ia = partRays( aRayPacket, curCell->bounds, ia, I, aHitInfoPacket );
|
|
|
|
|
|
|
|
if( ia < RAYPACKET_RAYS_PER_PACKET )
|
|
|
|
{
|
|
|
|
if( curCell->nPrimitives == 0 )
|
|
|
|
{
|
2021-01-02 21:05:29 +00:00
|
|
|
StackNode& node = todo[todoOffset++];
|
2016-07-19 17:35:25 +00:00
|
|
|
node.cell = curCell->secondChildOffset;
|
|
|
|
node.ia = ia;
|
|
|
|
nodeNum = nodeNum + 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-01-07 19:33:43 +00:00
|
|
|
unsigned int ie = getLastHit( aRayPacket, curCell->bounds, ia, I,
|
|
|
|
aHitInfoPacket );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
for( int j = 0; j < curCell->nPrimitives; ++j )
|
|
|
|
{
|
2021-01-02 21:05:29 +00:00
|
|
|
const OBJECT_3D* obj = m_primitives[curCell->primitivesOffset + j];
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
if( aRayPacket.m_Frustum.Intersect( obj->GetBBox() ) )
|
|
|
|
{
|
|
|
|
for( unsigned int i = 0; i < ie; ++i )
|
|
|
|
{
|
|
|
|
unsigned int idx = I[i];
|
|
|
|
|
2021-01-07 19:33:43 +00:00
|
|
|
bool hit = obj->Intersect( aRayPacket.m_ray[idx],
|
|
|
|
aHitInfoPacket[idx].m_HitInfo );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2021-01-07 19:33:43 +00:00
|
|
|
if( hit )
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
2021-01-07 19:33:43 +00:00
|
|
|
anyHit |= hit;
|
|
|
|
aHitInfoPacket[idx].m_hitresult |= hit;
|
2016-07-19 17:35:25 +00:00
|
|
|
aHitInfoPacket[idx].m_HitInfo.m_acc_node_info = nodeNum;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( todoOffset == 0 )
|
|
|
|
break;
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
const StackNode& node = todo[--todoOffset];
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
nodeNum = node.cell;
|
|
|
|
ia = node.ia;
|
|
|
|
}
|
|
|
|
|
2021-01-07 19:33:43 +00:00
|
|
|
return anyHit;
|
|
|
|
}
|
2016-07-19 17:35:25 +00:00
|
|
|
#endif
|