|
| 1 | +// SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA |
| 2 | +// CORPORATION & AFFILIATES. All rights reserved. |
| 3 | +// SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +#pragma once |
| 6 | + |
| 7 | +#include "cuBQL/builder/cuda/builder_common.h" |
| 8 | + |
| 9 | +namespace cuBQL { |
| 10 | + namespace cuda { |
| 11 | + |
| 12 | + template<typename T, int D> |
| 13 | + __global__ void |
| 14 | + refit_init(const typename BinaryBVH<T,D>::Node *nodes, |
| 15 | + uint32_t *refitData, |
| 16 | + int numNodes) |
| 17 | + { |
| 18 | + const int nodeID = threadIdx.x+blockIdx.x*blockDim.x; |
| 19 | + if (nodeID == 1 || nodeID >= numNodes) return; |
| 20 | + if (nodeID < 2) |
| 21 | + refitData[0] = 0; |
| 22 | + const auto &node = nodes[nodeID]; |
| 23 | + if (node.admin.count) return; |
| 24 | + |
| 25 | + refitData[node.admin.offset+0] = nodeID << 1; |
| 26 | + refitData[node.admin.offset+1] = nodeID << 1; |
| 27 | + } |
| 28 | + |
| 29 | + template<typename T, int D> |
| 30 | + __global__ |
| 31 | + void refit_run(BinaryBVH<T,D> bvh, |
| 32 | + uint32_t *refitData, |
| 33 | + const box_t<T,D> *boxes) |
| 34 | + { |
| 35 | + int nodeID = threadIdx.x+blockIdx.x*blockDim.x; |
| 36 | + if (nodeID == 1 || nodeID >= bvh.numNodes) return; |
| 37 | + |
| 38 | + typename BinaryBVH<T,D>::Node *node = &bvh.nodes[nodeID]; |
| 39 | + if (node->admin.count == 0) |
| 40 | + // this is a inner node - exit |
| 41 | + return; |
| 42 | + |
| 43 | + box_t<T,D> bounds; bounds.set_empty(); |
| 44 | + for (int i=0;i<node->admin.count;i++) { |
| 45 | + const box_t<T,D> primBox = boxes[bvh.primIDs[node->admin.offset+i]]; |
| 46 | + bounds.lower = min(bounds.lower,primBox.lower); |
| 47 | + bounds.upper = max(bounds.upper,primBox.upper); |
| 48 | + } |
| 49 | + |
| 50 | + int parentID = (refitData[nodeID] >> 1); |
| 51 | + while (true) { |
| 52 | + node->bounds = bounds; |
| 53 | + __threadfence(); |
| 54 | + if (node == bvh.nodes) |
| 55 | + break; |
| 56 | + |
| 57 | + uint32_t refitBits = atomicAdd(&refitData[parentID],1u); |
| 58 | + if ((refitBits & 1) == 0) |
| 59 | + // we're the first one - let other one do it |
| 60 | + break; |
| 61 | + |
| 62 | + nodeID = parentID; |
| 63 | + node = &bvh.nodes[parentID]; |
| 64 | + parentID = (refitBits >> 1); |
| 65 | + |
| 66 | + typename BinaryBVH<T,D>::Node l = bvh.nodes[node->admin.offset+0]; |
| 67 | + typename BinaryBVH<T,D>::Node r = bvh.nodes[node->admin.offset+1]; |
| 68 | + bounds.lower = min(l.bounds.lower,r.bounds.lower); |
| 69 | + bounds.upper = max(l.bounds.upper,r.bounds.upper); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + template<typename T, int D> |
| 74 | + void refit(BinaryBVH<T,D> &bvh, |
| 75 | + const box_t<T,D> *boxes, |
| 76 | + cudaStream_t s, |
| 77 | + GpuMemoryResource &memResource) |
| 78 | + { |
| 79 | + int numNodes = bvh.numNodes; |
| 80 | + |
| 81 | + uint32_t *refitData = 0; |
| 82 | + memResource.malloc((void**)&refitData,numNodes*sizeof(*refitData),s); |
| 83 | + |
| 84 | + refit_init<T,D><<<divRoundUp(numNodes,1024),1024,0,s>>> |
| 85 | + (bvh.nodes,refitData,numNodes); |
| 86 | + refit_run<<<divRoundUp(numNodes,32),32,0,s>>> |
| 87 | + (bvh,refitData,boxes); |
| 88 | + memResource.free((void*)refitData,s); |
| 89 | + // we're not syncing here - let APP do that |
| 90 | + } |
| 91 | + |
| 92 | + } // ::cuBQL::gpuBuilder_impl |
| 93 | +} // ::cuBQL |
0 commit comments