Skip to content

Commit

Permalink
Function with CUDA
Browse files Browse the repository at this point in the history
  • Loading branch information
szellmann committed Dec 8, 2024
1 parent c26d62b commit 6d842ed
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
24 changes: 21 additions & 3 deletions scene/volume/spatial_field/NanoVDBField.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#ifdef WITH_CUDA
#include <cuda_runtime.h>
#endif
// nanovdb
#include <nanovdb/util/IO.h>
// ours
Expand All @@ -22,11 +25,26 @@ void NanoVDBField::commit()
return;
}

#ifdef WITH_CUDA
cudaStream_t stream; // TODO: move to global state/use the one there?!
cudaStreamCreate(&stream);
nanovdb::cuda::DeviceBuffer buffer(m_gridData->totalSize(),
/*host:*/true,
&stream);
memcpy(buffer.data(), m_gridData->data(), m_gridData->totalSize());
m_gridHandle = std::move(buffer);

m_gridHandle.deviceUpload(stream, false);

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

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

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

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

Expand All @@ -46,8 +64,8 @@ aabb NanoVDBField::bounds() const
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]}};
return aabb{{(float)lower[0], (float)lower[1], (float)lower[2]},
{(float)upper[0], (float)upper[1], (float)upper[2]}};
}

} // namespace visionaray
1 change: 1 addition & 0 deletions scene/volume/spatial_field/NanoVDBField.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "NanoVDBField.cpp"
6 changes: 5 additions & 1 deletion scene/volume/spatial_field/NanoVDBField.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <nanovdb/GridHandle.h>
#include <nanovdb/HostBuffer.h>
#ifdef WITH_CUDA
#include <nanovdb/util/cuda/CudaDeviceBuffer.h>
#include <nanovdb/cuda/DeviceBuffer.h>
#endif
// ours
#include "SpatialField.h"
Expand All @@ -27,7 +27,11 @@ struct NanoVDBField : public SpatialField

helium::IntrusivePtr<Array1D> m_gridData;

#ifdef WITH_CUDA
nanovdb::GridHandle<nanovdb::cuda::DeviceBuffer> m_gridHandle;
#else
nanovdb::GridHandle<nanovdb::HostBuffer> m_gridHandle;
#endif
};

} // namespace visionaray

0 comments on commit 6d842ed

Please sign in to comment.