|
| 1 | +// Copyright Contributors to the OpenVDB Project |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +// the following files are from OpenVDB |
| 5 | +#include <openvdb/tools/Morphology.h> |
| 6 | +#include <openvdb/util/CpuTimer.h> |
| 7 | + |
| 8 | +// the following files are from NanoVDB |
| 9 | +#include <nanovdb/NanoVDB.h> |
| 10 | +#include <nanovdb/cuda/DeviceBuffer.h> |
| 11 | +#include <nanovdb/tools/CreateNanoGrid.h> |
| 12 | + |
| 13 | +template<typename BuildT> |
| 14 | +void mainDilateGrid( |
| 15 | + nanovdb::NanoGrid<BuildT> *deviceGridOriginal, |
| 16 | + nanovdb::NanoGrid<BuildT> *deviceGridDilated, |
| 17 | + nanovdb::NanoGrid<BuildT> *indexGridOriginal, |
| 18 | + nanovdb::NanoGrid<BuildT> *indexGridDilated, |
| 19 | + uint32_t nnType, |
| 20 | + uint32_t benchmark_iters |
| 21 | +); |
| 22 | + |
| 23 | +/// @brief This example depends on OpenVDB, NanoVDB, and CUDA |
| 24 | +int main(int argc, char *argv[]) |
| 25 | +{ |
| 26 | + using GridT = openvdb::FloatGrid; |
| 27 | + using BuildT = nanovdb::ValueOnIndex; |
| 28 | + |
| 29 | + // Select the type of dilation here. The NN_EDGE case supports leaf dilation too (currently) |
| 30 | + // openvdb::tools::NearestNeighbors nnType = openvdb::tools::NN_FACE_EDGE_VERTEX; |
| 31 | + openvdb::tools::NearestNeighbors nnType = openvdb::tools::NN_FACE; |
| 32 | + |
| 33 | + openvdb::util::CpuTimer cpuTimer; |
| 34 | + const bool printGridDiagnostics = true; |
| 35 | + |
| 36 | + try { |
| 37 | + |
| 38 | + if (argc<2) OPENVDB_THROW(openvdb::ValueError, "usage: "+std::string(argv[0])+" input.vdb [<iterations>]\n"); |
| 39 | + int benchmark_iters = 10; |
| 40 | + if (argc > 2) sscanf(argv[2], "%d", &benchmark_iters); |
| 41 | + |
| 42 | + // Read the initial level set from file |
| 43 | + |
| 44 | + cpuTimer.start("Read input VDB file"); |
| 45 | + openvdb::initialize(); |
| 46 | + openvdb::io::File inFile(argv[1]); |
| 47 | + inFile.open(false); // disable delayed loading |
| 48 | + auto baseGrids = inFile.getGrids(); |
| 49 | + inFile.close(); |
| 50 | + auto grid = openvdb::gridPtrCast<GridT>(baseGrids->at(0)); |
| 51 | + openvdb::FloatGrid* ptr = grid.get(); // raw pointer |
| 52 | + if (!grid) OPENVDB_THROW(openvdb::ValueError, "First grid is not a FloatGrid\n"); |
| 53 | + cpuTimer.stop(); |
| 54 | + |
| 55 | + // Convert to indexGrid (original, un-dilated) |
| 56 | + cpuTimer.start("Converting openVDB input to indexGrid (original version)"); |
| 57 | + auto handleOriginal = nanovdb::tools::openToIndexVDB<BuildT, nanovdb::cuda::DeviceBuffer>( |
| 58 | + grid, |
| 59 | + 0u, // Don't copy data channel |
| 60 | + false, // No stats |
| 61 | + false, // No tiles |
| 62 | + 1 // Verbose mode |
| 63 | + ); |
| 64 | + auto *indexGridOriginal = handleOriginal.grid<BuildT>(); |
| 65 | + cpuTimer.stop(); |
| 66 | + |
| 67 | + if (printGridDiagnostics) { |
| 68 | + std::cout << "============ Original Grid ===========" << std::endl; |
| 69 | + std::cout << "Allocated values [valueCount()] : " << indexGridOriginal->valueCount() << std::endl; |
| 70 | + std::cout << "Active voxels [activeVoxelCount()] : " << indexGridOriginal->activeVoxelCount() << std::endl; |
| 71 | + auto minCorner = indexGridOriginal->indexBBox().min(), maxCorner = indexGridOriginal->indexBBox().max(); |
| 72 | + std::cout << "Index-space bounding box : [" << minCorner.x() << "," << minCorner.y() << "," << minCorner.z() |
| 73 | + << "] -> [" << maxCorner.x() << "," << maxCorner.y() << "," << maxCorner.z() << "]" << std::endl; |
| 74 | + std::cout << "Leaf nodes : " << indexGridOriginal->tree().nodeCount(0) << std::endl; |
| 75 | + std::cout << "Lower internal nodes : " << indexGridOriginal->tree().nodeCount(1) << std::endl; |
| 76 | + std::cout << "Upper internal nodes : " << indexGridOriginal->tree().nodeCount(2) << std::endl; |
| 77 | + std::cout << "Leaf-level occupancy : " |
| 78 | + << 100.f * (float)(indexGridOriginal->activeVoxelCount())/(float)(indexGridOriginal->tree().nodeCount(0) * 512) |
| 79 | + << "%" << std::endl; |
| 80 | + std::cout << "Memory usage : " << indexGridOriginal->gridSize() << " bytes" << std::endl; |
| 81 | + } |
| 82 | + |
| 83 | + // Dilation (CPU/OpenVDB version) |
| 84 | + cpuTimer.start("Dilating openVDB (on CPU)"); |
| 85 | + openvdb::tools::dilateActiveValues(grid->tree(), 1, nnType); |
| 86 | + cpuTimer.stop(); |
| 87 | + |
| 88 | + // Convert to indexGrid (dilated) |
| 89 | + cpuTimer.start("Converting openVDB input to indexGrid (dilated version)"); |
| 90 | + auto handleDilated = nanovdb::tools::openToIndexVDB<BuildT, nanovdb::cuda::DeviceBuffer>( |
| 91 | + grid, |
| 92 | + 0u, // Don't copy data channel |
| 93 | + false, // No stats |
| 94 | + false, // No tiles |
| 95 | + 1 // Verbose mode |
| 96 | + ); |
| 97 | + cpuTimer.stop(); |
| 98 | + |
| 99 | + auto *indexGridDilated = handleDilated.grid<BuildT>(); |
| 100 | + |
| 101 | + if (printGridDiagnostics) { |
| 102 | + std::cout << "============ Dilated Grid ============" << std::endl; |
| 103 | + std::cout << "Allocated values [valueCount()] : " << indexGridDilated->valueCount() << std::endl; |
| 104 | + std::cout << "Active voxels [activeVoxelCount()] : " << indexGridDilated->activeVoxelCount() << std::endl; |
| 105 | + auto minCorner = indexGridDilated->indexBBox().min(), maxCorner = indexGridDilated->indexBBox().max(); |
| 106 | + std::cout << "Index-space bounding box : [" << minCorner.x() << "," << minCorner.y() << "," << minCorner.z() |
| 107 | + << "] -> [" << maxCorner.x() << "," << maxCorner.y() << "," << maxCorner.z() << "]" << std::endl; |
| 108 | + std::cout << "Leaf nodes : " << indexGridDilated->tree().nodeCount(0) << std::endl; |
| 109 | + std::cout << "Lower internal nodes : " << indexGridDilated->tree().nodeCount(1) << std::endl; |
| 110 | + std::cout << "Upper internal nodes : " << indexGridDilated->tree().nodeCount(2) << std::endl; |
| 111 | + std::cout << "Leaf-level occupancy : " |
| 112 | + << 100.f * (float)(indexGridDilated->activeVoxelCount())/(float)(indexGridDilated->tree().nodeCount(0) * 512) |
| 113 | + << "%" << std::endl; |
| 114 | + std::cout << "Memory usage : " << indexGridDilated->gridSize() << " bytes" << std::endl; |
| 115 | + } |
| 116 | + |
| 117 | + // Copy both NanoVDB grids to GPU |
| 118 | + handleOriginal.deviceUpload(); |
| 119 | + handleDilated.deviceUpload(); |
| 120 | + auto* deviceGridOriginal = handleOriginal.deviceGrid<BuildT>(); |
| 121 | + auto* deviceGridDilated = handleDilated.deviceGrid<BuildT>(); |
| 122 | + if (!deviceGridOriginal || !deviceGridDilated) |
| 123 | + OPENVDB_THROW(openvdb::RuntimeError, "Failure while uploading indexGrids to GPU"); |
| 124 | + |
| 125 | + // Launch benchmark |
| 126 | + mainDilateGrid( deviceGridOriginal, deviceGridDilated, indexGridOriginal, indexGridDilated, nnType, benchmark_iters ); |
| 127 | + |
| 128 | + } |
| 129 | + catch (const std::exception& e) { |
| 130 | + std::cerr << "An exception occurred: \"" << e.what() << "\"" << std::endl; |
| 131 | + } |
| 132 | + return 0; |
| 133 | +} |
0 commit comments