|
| 1 | +// Copyright Contributors to the OpenVDB Project |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +#pragma once |
| 5 | + |
| 6 | +#define _USE_MATH_DEFINES |
| 7 | +#include <cmath> |
| 8 | +#include <chrono> |
| 9 | +#include <fstream> |
| 10 | +#include <nanovdb/NanoVDB.h> |
| 11 | +#include "ComputePrimitives.h" |
| 12 | + |
| 13 | +struct RenderOp; |
| 14 | +template<typename GridT> |
| 15 | +__global__ void renderIsoSurfacePersistentKernel(RenderOp renderOp, float* image, const GridT* grid, int numPixels, int* nextPixel); |
| 16 | + |
| 17 | +struct RenderOp |
| 18 | +{ |
| 19 | + using Vec3T = nanovdb::math::Vec3<float>; |
| 20 | + using RayT = nanovdb::math::Ray<float>; |
| 21 | + int mWidth, mHeight; |
| 22 | + float mDx, mIso, mWBBoxDimZ; |
| 23 | + Vec3T mWBBoxCenter; |
| 24 | + |
| 25 | + template<typename BufferT> |
| 26 | + RenderOp(nanovdb::GridHandle<BufferT>& handle, int width, int height) |
| 27 | + { |
| 28 | + mWidth = width; |
| 29 | + mHeight = height; |
| 30 | + const auto *metaData = handle.gridMetaData(); |
| 31 | + mDx = float(metaData->voxelSize()[0]); |
| 32 | + mIso = mDx; |
| 33 | + mWBBoxDimZ = (float)metaData->worldBBox().dim()[2] * 2; |
| 34 | + mWBBoxCenter = Vec3T(metaData->worldBBox().min() + metaData->worldBBox().dim() * 0.5f); |
| 35 | + } |
| 36 | + |
| 37 | + template<typename GridT> |
| 38 | + inline float renderImage(bool useCuda, float* image, const GridT* grid) |
| 39 | + { |
| 40 | + using ClockT = std::chrono::high_resolution_clock; |
| 41 | + auto t0 = ClockT::now(); |
| 42 | + |
| 43 | + computeForEach( |
| 44 | + useCuda, mWidth * mHeight, 512, __FILE__, __LINE__, [this, image, grid] __hostdev__(int start, int end) { |
| 45 | + (*this)(start, end, image, grid); |
| 46 | + }); |
| 47 | + computeSync(useCuda, __FILE__, __LINE__); |
| 48 | + |
| 49 | + auto t1 = ClockT::now(); |
| 50 | + auto duration = std::chrono::duration_cast<std::chrono::microseconds>(t1 - t0).count() / 1000.f; |
| 51 | + return duration; |
| 52 | + } |
| 53 | + |
| 54 | + template <typename GridT> |
| 55 | + inline __hostdev__ void operator()(int start, int end, float* image, const GridT* grid) const |
| 56 | + { |
| 57 | + static_assert(nanovdb::util::is_same<typename GridT::BuildType, float, nanovdb::ValueOnIndex, nanovdb::ValueIndex>::value, "only works for float and OnIndex grids"); |
| 58 | + auto acc = nanovdb::getAccessor<GridT, float>(*grid); |
| 59 | + for (int i = start; i < end; ++i) { |
| 60 | + this->renderPixel(i, image, grid, acc); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + template <typename GridT, typename AccT> |
| 65 | + inline __hostdev__ void renderPixel(int i, float* image, const GridT* grid, AccT& acc) const |
| 66 | + { |
| 67 | + float t0, v; |
| 68 | + nanovdb::Coord ijk; |
| 69 | + RayT iRay = this->getIndexRay(i, grid); |
| 70 | + if (nanovdb::math::isoCrossing(iRay, acc, ijk, v, t0, mIso)) {// intersect... |
| 71 | + this->composite(image, i, (t0 * mDx) / (mWBBoxDimZ * 2), 1.0f); |
| 72 | + } else { |
| 73 | + this->composite(image, i, 0.0f, 0.0f);// write background value. |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + template<typename GridT> |
| 78 | + inline float renderImagePersistent(float* image, const GridT* grid, int* nextPixel) const |
| 79 | + { |
| 80 | + int device = 0; |
| 81 | + NANOVDB_CUDA_CHECK_ERROR(cudaGetDevice(&device), __FILE__, __LINE__); |
| 82 | + |
| 83 | + cudaDeviceProp properties; |
| 84 | + NANOVDB_CUDA_CHECK_ERROR(cudaGetDeviceProperties(&properties, device), __FILE__, __LINE__); |
| 85 | + |
| 86 | + constexpr int blockSize = 256; |
| 87 | + // Launch a small, fixed pool of blocks that persists on the GPU and |
| 88 | + // pulls pixel work from a global counter instead of launching one |
| 89 | + // logical thread per pixel up front. |
| 90 | + int blockCount = properties.multiProcessorCount * 4; |
| 91 | + if (blockCount < 1) blockCount = 1; |
| 92 | + |
| 93 | + // Reset the work queue before each timed render. The kernel advances |
| 94 | + // this counter by one warp of pixels at a time. |
| 95 | + NANOVDB_CUDA_CHECK_ERROR(cudaMemset(nextPixel, 0, sizeof(int)), __FILE__, __LINE__); |
| 96 | + |
| 97 | + using ClockT = std::chrono::high_resolution_clock; |
| 98 | + auto t0 = ClockT::now(); |
| 99 | + |
| 100 | + const int numPixels = mWidth * mHeight; |
| 101 | + renderIsoSurfacePersistentKernel<GridT><<<blockCount, blockSize>>>(*this, image, grid, numPixels, nextPixel); |
| 102 | + NANOVDB_CUDA_CHECK_ERROR(cudaGetLastError(), __FILE__, __LINE__); |
| 103 | + NANOVDB_CUDA_CHECK_ERROR(cudaDeviceSynchronize(), __FILE__, __LINE__); |
| 104 | + |
| 105 | + auto t1 = ClockT::now(); |
| 106 | + auto duration = std::chrono::duration_cast<std::chrono::microseconds>(t1 - t0).count() / 1000.f; |
| 107 | + return duration; |
| 108 | + } |
| 109 | + |
| 110 | + template <typename GridT> |
| 111 | + inline __hostdev__ RayT getIndexRay(int i, const GridT *grid) const |
| 112 | + { |
| 113 | + // perspective camera along Z-axis... |
| 114 | + const uint32_t x = i % mWidth, y = i / mWidth; |
| 115 | + const float fov = 45.f; |
| 116 | + const float u = (float(x) + 0.5f) / mWidth; |
| 117 | + const float v = (float(y) + 0.5f) / mHeight; |
| 118 | + const float aspect = mWidth / float(mHeight); |
| 119 | + const float Px = (2.f * u - 1.f) * tanf(fov / 2 * 3.14159265358979323846f / 180.f) * aspect; |
| 120 | + const float Py = (2.f * v - 1.f) * tanf(fov / 2 * 3.14159265358979323846f / 180.f); |
| 121 | + const Vec3T origin = mWBBoxCenter + Vec3T(0, 0, mWBBoxDimZ); |
| 122 | + Vec3T dir(Px, Py, -1.f); |
| 123 | + dir.normalize(); |
| 124 | + RayT wRay(origin, dir); |
| 125 | + return wRay.worldToIndexF(*grid);// transform the ray to the grid's index-space. |
| 126 | + } |
| 127 | + |
| 128 | + inline __hostdev__ void composite(float* outImage, int offset, float value, float alpha) const |
| 129 | + { |
| 130 | + const uint32_t x = offset % mWidth, y = offset / mWidth; |
| 131 | + |
| 132 | + // checkerboard background... |
| 133 | + const int mask = 1 << 7; |
| 134 | + const float bg = ((x & mask) ^ (y & mask)) ? 1.0f : 0.5f; |
| 135 | + outImage[offset] = alpha * value + (1.0f - alpha) * bg; |
| 136 | + } |
| 137 | + |
| 138 | + inline void saveImage(const std::string& filename, const float* image) const |
| 139 | + { |
| 140 | + const auto isLittleEndian = []() -> bool { |
| 141 | + static int x = 1; |
| 142 | + static bool result = reinterpret_cast<uint8_t*>(&x)[0] == 1; |
| 143 | + return result; |
| 144 | + }; |
| 145 | + |
| 146 | + float scale = 1.0f; |
| 147 | + if (isLittleEndian()) scale = -scale; |
| 148 | + |
| 149 | + std::fstream fs(filename, std::ios::out | std::ios::binary); |
| 150 | + if (!fs.is_open()) throw std::runtime_error("Unable to open file: " + filename); |
| 151 | + |
| 152 | + fs << "Pf\n" |
| 153 | + << mWidth << "\n" |
| 154 | + << mHeight << "\n" |
| 155 | + << scale << "\n"; |
| 156 | + |
| 157 | + for (int i = 0; i < mWidth * mHeight; ++i) { |
| 158 | + float r = image[i]; |
| 159 | + fs.write((char*)&r, sizeof(float)); |
| 160 | + } |
| 161 | + } |
| 162 | +}; |
| 163 | + |
| 164 | +template<typename GridT> |
| 165 | +__global__ void renderIsoSurfacePersistentKernel(RenderOp renderOp, float* image, const GridT* grid, int numPixels, int* nextPixel) |
| 166 | +{ |
| 167 | + static_assert(nanovdb::util::is_same<typename GridT::BuildType, float, nanovdb::ValueOnIndex, nanovdb::ValueIndex>::value, "only works for float and OnIndex grids"); |
| 168 | + auto acc = nanovdb::getAccessor<GridT, float>(*grid); |
| 169 | + const unsigned int lane = threadIdx.x & 31u; |
| 170 | + |
| 171 | + // Keep the fixed set of launched threads busy until all pixels have been assigned. |
| 172 | + while (true) { |
| 173 | + int base = 0; |
| 174 | + // Each warp asks the shared counter for the next batch of 32 pixels. |
| 175 | + // Only lane 0 updates the counter; __shfl_sync copies lane 0's result |
| 176 | + // to the other lanes in the warp. |
| 177 | + if (lane == 0) base = atomicAdd(nextPixel, 32); |
| 178 | + base = __shfl_sync(0xFFFFFFFFu, base, 0); |
| 179 | + |
| 180 | + // Each lane renders one pixel from the batch: lane 0 renders base, |
| 181 | + // lane 1 renders base + 1, and so on. |
| 182 | + const int i = base + int(lane); |
| 183 | + if (i >= numPixels) break; |
| 184 | + |
| 185 | + renderOp.renderPixel(i, image, grid, acc); |
| 186 | + } |
| 187 | +} |
0 commit comments