@@ -202,8 +202,6 @@ SingletonConstruction( uint32_t index, PrimitiveContainer& primitives, BoxNode*
202202 leafType = InstanceType;
203203 }
204204
205- primNodes[0 ].m_parentAddr = 0 ;
206-
207205 BoxNode root;
208206 root.m_box0 = primitives.fetchAabb ( 0 );
209207 root.m_box1 .reset ();
@@ -415,58 +413,78 @@ extern "C" __global__ void ComputeMortonCodes_InstanceList_MatrixFrame(
415413 ComputeMortonCodes<InstanceList<MatrixFrame>>( primitives, centroidBox, mortonCodeKeys, mortonCodeValues );
416414}
417415
418- extern " C" __global__ void ResetCounters ( uint32_t primCount, BoxNode* boxNodes )
416+ template <typename PrimitiveContainer, typename PrimitiveNode, typename Header>
417+ __device__ void ResetCountersAndUpdateLeaves (
418+ const Header* header, PrimitiveContainer& primitives, BoxNode* boxNodes, PrimitiveNode* primNodes )
419419{
420420 const uint32_t index = threadIdx.x + blockIdx.x * blockDim.x ;
421- if ( index < primCount ) boxNodes[index].m_updateCounter = 0 ;
421+
422+ if ( index < header->m_boxNodeCount ) boxNodes[index].m_updateCounter = 0 ;
423+
424+ if constexpr ( is_same<PrimitiveNode, TriangleNode>::value )
425+ {
426+ if ( index < header->m_primNodeCount )
427+ {
428+ primNodes[index] = primitives.fetchTriangleNode ( { primNodes[index].m_primIndex0 , primNodes[index].m_primIndex1 } );
429+ }
430+ }
431+ else if constexpr ( is_same<PrimitiveNode, InstanceNode>::value )
432+ {
433+ if ( index < primitives.getFrameCount () ) primitives.convertFrame ( index );
434+
435+ if ( index < header->m_primNodeCount )
436+ {
437+ const uint32_t primIndex = primNodes[index].m_primIndex ;
438+ hiprtTransformHeader transform = primitives.fetchTransformHeader ( primIndex );
439+ primNodes[index].m_mask = primitives.fetchMask ( primIndex );
440+ if ( transform.frameCount == 1 )
441+ primNodes[index].m_identity =
442+ primitives.copyInvTransformMatrix ( transform.frameIndex , primNodes[index].m_matrix ) ? 1 : 0 ;
443+ }
444+ }
422445}
423446
424- template < typename InstanceList>
425- __device__ void ResetCountersAndUpdateFrames ( InstanceList& instanceList , BoxNode* boxNodes )
447+ extern " C " __global__ void ResetCountersAndUpdateLeaves_TriangleMesh_TriangleNode (
448+ const GeomHeader* header, TriangleMesh primitives , BoxNode* boxNodes, TriangleNode* primNodes )
426449{
427- const uint32_t index = threadIdx.x + blockIdx.x * blockDim.x ;
428- if ( index < instanceList.getCount () ) boxNodes[index].m_updateCounter = 0 ;
429- if ( index < instanceList.getFrameCount () ) instanceList.convertFrame ( index );
450+ ResetCountersAndUpdateLeaves ( header, primitives, boxNodes, primNodes );
430451}
431452
432- extern " C" __global__ void
433- ResetCountersAndUpdateFrames_InstanceList_SRTFrame ( InstanceList<SRTFrame> instanceList, BoxNode* boxNodes )
453+ extern " C" __global__ void ResetCountersAndUpdateLeaves_AabbList_CustomNode (
454+ const GeomHeader* header, AabbList primitives, BoxNode* boxNodes, CustomNode* primNodes )
434455{
435- ResetCountersAndUpdateFrames<InstanceList<SRTFrame>>( instanceList, boxNodes );
456+ ResetCountersAndUpdateLeaves ( header, primitives, boxNodes, primNodes );
436457}
437458
438- extern " C" __global__ void
439- ResetCountersAndUpdateFrames_InstanceList_MatrixFrame ( InstanceList<MatrixFrame> instanceList , BoxNode* boxNodes )
459+ extern " C" __global__ void ResetCountersAndUpdateLeaves_InstanceList_MatrixFrame_InstanceNode (
460+ const SceneHeader* header, InstanceList<MatrixFrame> primitives , BoxNode* boxNodes, InstanceNode* primNodes )
440461{
441- ResetCountersAndUpdateFrames<InstanceList<MatrixFrame>>( instanceList, boxNodes );
462+ ResetCountersAndUpdateLeaves ( header, primitives, boxNodes, primNodes );
442463}
443464
444- template <typename PrimitiveContainer, typename PrimitiveNode>
445- __device__ void FitBounds ( PrimitiveContainer& primitives, BoxNode* boxNodes, PrimitiveNode* primNodes )
465+ extern " C" __global__ void ResetCountersAndUpdateLeaves_InstanceList_SRTFrame_InstanceNode (
466+ const SceneHeader* header, InstanceList<SRTFrame> primitives, BoxNode* boxNodes, InstanceNode* primNodes )
467+ {
468+ ResetCountersAndUpdateLeaves ( header, primitives, boxNodes, primNodes );
469+ }
470+
471+ template <typename PrimitiveContainer, typename PrimitiveNode, typename Header>
472+ __device__ void FitBounds ( Header* header, PrimitiveContainer& primitives, BoxNode* boxNodes, PrimitiveNode* primNodes )
446473{
447474 uint32_t index = threadIdx.x + blockIdx.x * blockDim.x ;
448475
449- if ( index >= primitives. getCount () ) return ;
476+ if ( index >= header-> m_boxNodeCount ) return ;
450477
451- uint32_t parentAddr = primNodes[index].m_parentAddr ;
452- if constexpr ( is_same<PrimitiveNode, TriangleNode>::value )
453- {
454- primNodes[index] =
455- primitives.fetchTriangleNode ( make_uint2 ( primNodes[index].m_primIndex0 , primNodes[index].m_primIndex1 ) );
456- primNodes[index].m_parentAddr = parentAddr;
457- }
458- else if constexpr ( is_same<PrimitiveNode, InstanceNode>::value )
478+ BoxNode node = boxNodes[index];
479+ uint32_t internalCount = 0 ;
480+ for ( uint32_t i = 0 ; i < node.m_childCount ; ++i )
459481 {
460- const uint32_t primIndex = primNodes[index].m_primIndex ;
461- hiprtTransformHeader transform = primitives.fetchTransformHeader ( primIndex );
462- primNodes[index].m_mask = primitives.fetchMask ( primIndex );
463- if ( transform.frameCount == 1 )
464- primNodes[index].m_identity =
465- primitives.copyInvTransformMatrix ( transform.frameIndex , primNodes[index].m_matrix ) ? 1 : 0 ;
482+ if ( node.getChildType ( i ) == BoxType ) internalCount++;
466483 }
467484
468- index = parentAddr;
469- while ( index != InvalidValue && atomicAdd ( &boxNodes[index].m_updateCounter , 1 ) >= boxNodes[index].m_childCount - 1 )
485+ if ( internalCount > 0 ) return ;
486+
487+ while ( true )
470488 {
471489 __threadfence ();
472490
@@ -484,33 +502,40 @@ __device__ void FitBounds( PrimitiveContainer& primitives, BoxNode* boxNodes, Pr
484502 if ( node.m_childIndex3 != InvalidValue )
485503 node.m_box3 = getNodeBox ( node.m_childIndex3 , primitives, boxNodes, primNodes );
486504
487- index = boxNodes[index].m_parentAddr ;
505+ internalCount = 0 ;
506+ for ( uint32_t i = 0 ; i < node.m_childCount ; ++i )
507+ {
508+ if ( node.getChildType ( i ) == BoxType ) internalCount++;
509+ }
488510
489511 __threadfence ();
512+
513+ if ( atomicAdd ( &node.m_updateCounter , 1 ) < internalCount - 1 ) break ;
490514 }
491515}
492516
493517extern " C" __global__ void
494- FitBounds_TriangleMesh_TriangleNode ( TriangleMesh primitives, BoxNode* boxNodes, TriangleNode* primNodes )
518+ FitBounds_TriangleMesh_TriangleNode ( GeomHeader* header, TriangleMesh primitives, BoxNode* boxNodes, TriangleNode* primNodes )
495519{
496- FitBounds<TriangleMesh, TriangleNode>( primitives, boxNodes, primNodes );
520+ FitBounds<TriangleMesh, TriangleNode>( header, primitives, boxNodes, primNodes );
497521}
498522
499- extern " C" __global__ void FitBounds_AabbList_CustomNode ( AabbList primitives, BoxNode* boxNodes, CustomNode* primNodes )
523+ extern " C" __global__ void
524+ FitBounds_AabbList_CustomNode ( GeomHeader* header, AabbList primitives, BoxNode* boxNodes, CustomNode* primNodes )
500525{
501- FitBounds<AabbList, CustomNode>( primitives, boxNodes, primNodes );
526+ FitBounds<AabbList, CustomNode>( header, primitives, boxNodes, primNodes );
502527}
503528
504- extern " C" __global__ void
505- FitBounds_InstanceList_SRTFrame_InstanceNode ( InstanceList<SRTFrame> primitives, BoxNode* boxNodes, InstanceNode* primNodes )
529+ extern " C" __global__ void FitBounds_InstanceList_SRTFrame_InstanceNode (
530+ SceneHeader* header, InstanceList<SRTFrame> primitives, BoxNode* boxNodes, InstanceNode* primNodes )
506531{
507- FitBounds<InstanceList<SRTFrame>, InstanceNode>( primitives, boxNodes, primNodes );
532+ FitBounds<InstanceList<SRTFrame>, InstanceNode>( header, primitives, boxNodes, primNodes );
508533}
509534
510535extern " C" __global__ void FitBounds_InstanceList_MatrixFrame_InstanceNode (
511- InstanceList<MatrixFrame> primitives, BoxNode* boxNodes, InstanceNode* primNodes )
536+ SceneHeader* header, InstanceList<MatrixFrame> primitives, BoxNode* boxNodes, InstanceNode* primNodes )
512537{
513- FitBounds<InstanceList<MatrixFrame>, InstanceNode>( primitives, boxNodes, primNodes );
538+ FitBounds<InstanceList<MatrixFrame>, InstanceNode>( header, primitives, boxNodes, primNodes );
514539}
515540
516541template <typename PrimitiveContainer, typename PrimitiveNode, typename Header>
@@ -635,8 +660,7 @@ __device__ void Collapse(
635660 else
636661 primNodes[nodeAddr].m_transform = transform;
637662 }
638- primNodes[nodeAddr].m_parentAddr = parentAddr;
639- done = true ;
663+ done = true ;
640664 }
641665 }
642666
0 commit comments