| 36 | }; |
| 37 | |
| 38 | void* rtcBuildBVHMorton(const RTCBuildArguments* arguments) |
| 39 | { |
| 40 | BVH* bvh = (BVH*) arguments->bvh; |
| 41 | RTCBuildPrimitive* prims_i = arguments->primitives; |
| 42 | size_t primitiveCount = arguments->primitiveCount; |
| 43 | RTCCreateNodeFunction createNode = arguments->createNode; |
| 44 | RTCSetNodeChildrenFunction setNodeChildren = arguments->setNodeChildren; |
| 45 | RTCSetNodeBoundsFunction setNodeBounds = arguments->setNodeBounds; |
| 46 | RTCCreateLeafFunction createLeaf = arguments->createLeaf; |
| 47 | RTCProgressMonitorFunction buildProgress = arguments->buildProgress; |
| 48 | void* userPtr = arguments->userPtr; |
| 49 | |
| 50 | std::atomic<size_t> progress(0); |
| 51 | |
| 52 | /* initialize temporary arrays for morton builder */ |
| 53 | PrimRef* prims = (PrimRef*) prims_i; |
| 54 | mvector<BVHBuilderMorton::BuildPrim>& morton_src = bvh->morton_src; |
| 55 | mvector<BVHBuilderMorton::BuildPrim>& morton_tmp = bvh->morton_tmp; |
| 56 | morton_src.resize(primitiveCount); |
| 57 | morton_tmp.resize(primitiveCount); |
| 58 | |
| 59 | /* compute centroid bounds */ |
| 60 | const BBox3fa centBounds = parallel_reduce ( size_t(0), primitiveCount, BBox3fa(empty), [&](const range<size_t>& r) -> BBox3fa { |
| 61 | |
| 62 | BBox3fa bounds(empty); |
| 63 | for (size_t i=r.begin(); i<r.end(); i++) |
| 64 | bounds.extend(prims[i].bounds().center2()); |
| 65 | return bounds; |
| 66 | }, BBox3fa::merge); |
| 67 | |
| 68 | /* compute morton codes */ |
| 69 | BVHBuilderMorton::MortonCodeMapping mapping(centBounds); |
| 70 | parallel_for ( size_t(0), primitiveCount, [&](const range<size_t>& r) { |
| 71 | BVHBuilderMorton::MortonCodeGenerator generator(mapping,&morton_src[r.begin()]); |
| 72 | for (size_t i=r.begin(); i<r.end(); i++) { |
| 73 | generator(prims[i].bounds(),(unsigned) i); |
| 74 | } |
| 75 | }); |
| 76 | |
| 77 | /* start morton build */ |
| 78 | std::pair<void*,BBox3fa> root = BVHBuilderMorton::build<std::pair<void*,BBox3fa>>( |
| 79 | |
| 80 | /* thread local allocator for fast allocations */ |
| 81 | [&] () -> FastAllocator::CachedAllocator { |
| 82 | return bvh->allocator.getCachedAllocator(); |
| 83 | }, |
| 84 | |
| 85 | /* lambda function that allocates BVH nodes */ |
| 86 | [&] ( const FastAllocator::CachedAllocator& alloc, size_t N ) -> void* { |
| 87 | return createNode((RTCThreadLocalAllocator)&alloc, (unsigned int)N,userPtr); |
| 88 | }, |
| 89 | |
| 90 | /* lambda function that sets bounds */ |
| 91 | [&] (void* node, const std::pair<void*,BBox3fa>* children, size_t N) -> std::pair<void*,BBox3fa> |
| 92 | { |
| 93 | BBox3fa bounds = empty; |
| 94 | void* childptrs[BVHBuilderMorton::MAX_BRANCHING_FACTOR]; |
| 95 | const RTCBounds* cbounds[BVHBuilderMorton::MAX_BRANCHING_FACTOR]; |
no test coverage detected