Skip to content

Commit

Permalink
nanovdb
Browse files Browse the repository at this point in the history
  • Loading branch information
szellmann committed Dec 7, 2024
1 parent b47974c commit c26d62b
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 2 deletions.
21 changes: 21 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ if (hip)
find_package(hip)
endif()

option(ANARI_VISIONARAY_ENABLE_NANOVDB "Enable NanoVDB spatial field type" OFF)
set(nanovdb ${ANARI_VISIONARAY_ENABLE_NANOVDB})
if (nanovdb)
find_package(OpenVDB COMPONENTS nanovdb REQUIRED)
endif()

anari_generate_queries(
NAME visionaray
PREFIX VisionarayDevice
Expand Down Expand Up @@ -121,6 +127,11 @@ target_sources(${PROJECT_NAME} PRIVATE
VisionarayDeviceQueries.cpp
)

if (nanovdb)
target_sources(${PROJECT_NAME} PRIVATE scene/volume/spatial_field/NanoVDBField.cpp)
target_compile_definitions(${PROJECT_NAME} PRIVATE WITH_NANOVDB=1)
endif()

include(GenerateExportHeader)
generate_export_header(${PROJECT_NAME}
EXPORT_MACRO_NAME "VISIONARAY_DEVICE_INTERFACE"
Expand Down Expand Up @@ -160,6 +171,11 @@ if (cuda)
PROPERTIES COMPILE_FLAGS "--extended-lambda --expt-relaxed-constexpr"
)
target_compile_definitions(${PROJECT_NAME}_cuda PRIVATE WITH_CUDA=1)
if (nanovdb)
target_sources(${PROJECT_NAME}_cuda PRIVATE
scene/volume/spatial_field/NanoVDBField.cpp)
target_compile_definitions(${PROJECT_NAME}_cuda PRIVATE WITH_NANOVDB=1)
endif()

generate_export_header(${PROJECT_NAME}_cuda
EXPORT_MACRO_NAME "VISIONARAY_DEVICE_INTERFACE"
Expand Down Expand Up @@ -188,6 +204,11 @@ if (hip)
VisionarayHIPDeviceQueries.cpp
)
target_compile_definitions(${PROJECT_NAME}_hip PRIVATE WITH_HIP=1)
if (nanovdb)
target_sources(${PROJECT_NAME}_hip PRIVATE
scene/volume/spatial_field/NanoVDBField.cpp)
target_compile_definitions(${PROJECT_NAME}_hip PRIVATE WITH_NANOVDB=1)
endif()

target_link_libraries(${PROJECT_NAME}_hip
visionaray::visionaray anari::anari anari::helium)
Expand Down
18 changes: 17 additions & 1 deletion DeviceCopyableObjects.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ using hip_index_bvh = index_bvh_t<hip::device_vector<P>, hip::device_vector<
} // namespace visionaray
#endif

#ifdef WITH_NANOVDB
#include <nanovdb/NanoVDB.h>
#endif

namespace visionaray {

namespace dco {
Expand Down Expand Up @@ -383,7 +387,7 @@ inline GridAccel createGridAccel()

struct SpatialField
{
enum Type { StructuredRegular, Unstructured, BlockStructured, Unknown, };
enum Type { StructuredRegular, Unstructured, BlockStructured, NanoVDB, Unknown, };
Type type;
unsigned fieldID;
float delta;
Expand Down Expand Up @@ -438,6 +442,11 @@ struct SpatialField
index_bvh<Block>::bvh_ref samplingBVH;
#endif
} asBlockStructured;
#ifdef WITH_NANOVDB
struct {
nanovdb::NanoGrid<float> *grid;
} asNanoVDB;
#endif
};
};

Expand Down Expand Up @@ -486,6 +495,13 @@ inline bool sampleField(const SpatialField &sf, vec3 P, float &value) {
value = basisPRD[0]/basisPRD[1];
return true;
}
#ifdef WITH_NANOVDB
else if (sf.type == SpatialField::NanoVDB) {
auto acc = sf.asNanoVDB.grid->getAccessor();
value = acc.getValue(nanovdb::Coord(P.x,P.y,P.z));
return true;
}
#endif

return false;
}
Expand Down
53 changes: 53 additions & 0 deletions scene/volume/spatial_field/NanoVDBField.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// nanovdb
#include <nanovdb/util/IO.h>
// ours
#include "NanoVDBField.h"

namespace visionaray {

NanoVDBField::NanoVDBField(VisionarayGlobalState *d)
: SpatialField(d)
{
vfield.type = dco::SpatialField::NanoVDB;
}

void NanoVDBField::commit()
{
m_gridData = getParamObject<helium::Array1D>("gridData");

if (!m_gridData) {
reportMessage(ANARI_SEVERITY_WARNING,
"missing required parameter 'gridHandle' on nanovdb spatial field");

return;
}

auto buffer = nanovdb::HostBuffer::createFull(m_gridData->totalSize(),
(void *)m_gridData->data());
m_gridHandle = std::move(buffer);

vfield.asNanoVDB.grid = m_gridHandle.grid<float>();

vfield.voxelSpaceTransform = mat4x3(mat3::identity(),float3{0.f,0.f,0.f});

dispatch();
}

bool NanoVDBField::isValid() const
{
return (bool)m_gridHandle;
}

aabb NanoVDBField::bounds() const
{
if (!isValid())
return {};

auto bbox = m_gridHandle.gridMetaData()->indexBBox();
auto lower = bbox.min();
auto upper = bbox.max();
return aabb{{lower[0], lower[1], lower[2]},
{upper[0], upper[1], upper[2]}};
}

} // namespace visionaray
33 changes: 33 additions & 0 deletions scene/volume/spatial_field/NanoVDBField.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

#pragma once

// nanovdb
#include <nanovdb/NanoVDB.h>
#include <nanovdb/GridHandle.h>
#include <nanovdb/HostBuffer.h>
#ifdef WITH_CUDA
#include <nanovdb/util/cuda/CudaDeviceBuffer.h>
#endif
// ours
#include "SpatialField.h"
#include "array/Array1D.h"

namespace visionaray {

struct NanoVDBField : public SpatialField
{
NanoVDBField(VisionarayGlobalState *d);

void commit() override;

bool isValid() const override;

aabb bounds() const override;
private:

helium::IntrusivePtr<Array1D> m_gridData;

nanovdb::GridHandle<nanovdb::HostBuffer> m_gridHandle;
};

} // namespace visionaray
9 changes: 8 additions & 1 deletion scene/volume/spatial_field/SpatialField.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
#include "BlockStructuredField.h"
#include "StructuredRegularField.h"
#include "UnstructuredField.h"
#ifdef WITH_NANOVDB
#include "NanoVDBField.h"
#endif

namespace visionaray {

Expand All @@ -30,7 +33,11 @@ SpatialField *SpatialField::createInstance(
else if (subtype == "unstructured")
return new UnstructuredField(s);
else if (subtype == "amr" || subtype == "blockStructured")
return new BlockStructuredField(s);
return new BlockStructuredField(s);
#ifdef WITH_NANOVDB
else if (subtype == "nanovdb" || subtype == "vdb")
return new NanoVDBField(s);
#endif
else
return (SpatialField *)new UnknownObject(ANARI_SPATIAL_FIELD, s);
}
Expand Down

0 comments on commit c26d62b

Please sign in to comment.