Skip to content

Commit

Permalink
rgb grid loading and majorant grid compute routine
Browse files Browse the repository at this point in the history
  • Loading branch information
cuteday committed Dec 6, 2023
1 parent 3158597 commit c4fa85c
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 18 deletions.
30 changes: 22 additions & 8 deletions src/render/media.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
KRR_NAMESPACE_BEGIN
/* Note that the function qualifier (e.g. inline) should be consistent between declaration and definition. */

void initializeMajorantGrid(MajorantGrid& majorantGrid,
nanovdb::FloatGrid *floatGrid) {
template <typename GridType>
void initializeMajorantGrid(MajorantGrid &majorantGrid, GridType *densityGrid) {
auto res = majorantGrid.res;
cudaDeviceSynchronize();
// [TODO] This device memory is not properly freed
Expand All @@ -30,14 +30,14 @@ void initializeMajorantGrid(MajorantGrid& majorantGrid,
float(z + 1) / majorantGrid.res.z())));

// Compute corresponding NanoVDB index-space bounds in floating-point.
nanovdb::Vec3R i0 = floatGrid->worldToIndexF(
nanovdb::Vec3R i0 = densityGrid->worldToIndexF(
nanovdb::Vec3R(wb.min().x(), wb.min().y(), wb.min().z()));
nanovdb::Vec3R i1 = floatGrid->worldToIndexF(
nanovdb::Vec3R i1 = densityGrid->worldToIndexF(
nanovdb::Vec3R(wb.max().x(), wb.max().y(), wb.max().z()));

// Now find integer index-space bounds, accounting for both
// filtering and the overall index bounding box.
auto bbox = floatGrid->indexBBox();
auto bbox = densityGrid->indexBBox();
float delta = 1.f; // Filter slop
int nx0 = max(int(i0[0] - delta), bbox.min()[0]);
int nx1 = min(int(i1[0] + delta), bbox.max()[0]);
Expand All @@ -47,17 +47,31 @@ void initializeMajorantGrid(MajorantGrid& majorantGrid,
int nz1 = min(int(i1[2] + delta), bbox.max()[2]);

float maxValue = 0;
auto accessor = floatGrid->getAccessor();
auto accessor = densityGrid->getAccessor();

for (int nz = nz0; nz <= nz1; ++nz)
for (int ny = ny0; ny <= ny1; ++ny)
for (int nx = nx0; nx <= nx1; ++nx)
maxValue = max(maxValue, accessor.getValue({nx, ny, nz}));

if constexpr (std::is_same_v<GridType, nanovdb::FloatGrid>) {
maxValue = max(maxValue, accessor.getValue({nx, ny, nz}));
} else if constexpr (std::is_same_v<GridType, nanovdb::Vec3fGrid>) {
auto value = accessor.getValue({nx, ny, nz});
RGB color = {value[0], value[1], value[2]};
RGBUnboundedSpectrum spectrum(color, *KRR_DEFAULT_COLORSPACE_GPU);
maxValue = max(maxValue, spectrum.maxValue());
} else {
static_assert(false, "Unsupported grid type!");
}
majorantGrid.set(x, y, z, maxValue);
}, 0);
}

// explicit instantiation of enable data types of vdb grids
template void initializeMajorantGrid<nanovdb::FloatGrid>(MajorantGrid &majorantGrid,
nanovdb::FloatGrid *densityGrid);
template void initializeMajorantGrid<nanovdb::Vec3fGrid>(MajorantGrid &majorantGrid,
nanovdb::Vec3fGrid *densityGrid);

KRR_HOST_DEVICE PhaseFunctionSample HGPhaseFunction::sample(const Vector3f &wo,
const Vector2f &u) const {
float g = clamp(this->g, -.99f, .99f);
Expand Down
3 changes: 2 additions & 1 deletion src/render/media.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ class MajorantGrid : public Grid<float, 3> {
AABB3f bounds;
};

void initializeMajorantGrid(MajorantGrid &majorantGrid, nanovdb::FloatGrid *floatGrid);
template <typename GridType>
void initializeMajorantGrid(MajorantGrid &majorantGrid, GridType *floatGrid);

class MajorantIterator {
public:
Expand Down
2 changes: 1 addition & 1 deletion src/scene/openvdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ bool OpenVDBImporter::import(const fs::path filepath, Scene::SharedPtr scene,
float temperatureOffset = params.value<float>("offset_temperature", 0);
float LeScale = params.value<float>("scale_le", 1);

auto densityGrid = std::dynamic_pointer_cast<NanoVDBGrid<float>>(loadNanoVDB(filepath, key_density));
auto densityGrid = loadNanoVDB(filepath, key_density);
auto temperatureGrid = std::dynamic_pointer_cast<NanoVDBGrid<float>>(loadNanoVDB(filepath, key_temperature));
auto volume = std::make_shared<VDBVolume>(sigma_a, sigma_s, g, densityGrid, temperatureGrid,
LeScale, temperaetureScale, temperatureOffset);
Expand Down
24 changes: 17 additions & 7 deletions src/util/volume.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,24 @@ NanoVDBGridBase::SharedPtr loadNanoVDB(std::filesystem::path path, string key) {
return nullptr;
}

auto grid = openvdb::gridPtrCast<openvdb::FloatGrid>(baseGrid);
auto transform = grid->transform();
auto handle = nanovdb::openToNanoVDB<nanovdb::CudaDeviceBuffer>(grid);
auto transform = baseGrid->transform();
auto handle = nanovdb::openToNanoVDB<nanovdb::CudaDeviceBuffer>(baseGrid);
const nanovdb::GridMetaData *metadata = handle.gridMetaData();
if (metadata->gridType() != nanovdb::GridType::Float) Log(Fatal, "only support float grid!");
float minValue, maxValue;
grid->evalMinMax(minValue, maxValue);
return std::make_shared<NanoVDBGrid<float>>(std::move(handle), maxValue);
if (metadata->gridType() == nanovdb::GridType::Float) {
float minValue, maxValue;
auto grid = openvdb::gridPtrCast<openvdb::FloatGrid>(baseGrid);
grid->evalMinMax(minValue, maxValue);
return std::make_shared<NanoVDBGrid<float>>(std::move(handle), maxValue);
} else if (metadata->gridType() == nanovdb::GridType::Vec3f) {
auto grid = openvdb::gridPtrCast<openvdb::Vec3fGrid>(baseGrid);
openvdb::Vec3f minValue, maxValue;
grid->evalMinMax(minValue, maxValue);
return std::make_shared<NanoVDBGrid<Array3f>>(std::move(handle),
Array3f{maxValue[0], maxValue[1], maxValue[2]});
} else {
Log(Fatal, "Unsupported data type for openvdb grid!");
return nullptr;
}
}

KRR_NAMESPACE_END
2 changes: 1 addition & 1 deletion src/util/volume.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ template<> class NanoVDBGrid<Array3f> : public NanoVDBGridBaseImpl<Array3f> {

NanoVDBGrid() = default;

NanoVDBGrid(nanovdb::GridHandle<nanovdb::CudaDeviceBuffer>&& density, float maxDensity) :
NanoVDBGrid(nanovdb::GridHandle<nanovdb::CudaDeviceBuffer> &&density, Array3f maxDensity) :
NanoVDBGridBaseImpl(std::move(density), maxDensity) {
densityGrid = densityHandle.grid<NativeType>();
nanovdb::BBox<nanovdb::Vec3R> bbox = densityGrid->worldBBox();
Expand Down

0 comments on commit c4fa85c

Please sign in to comment.