diff options
author | Patrick Mours <pmours@nvidia.com> | 2020-10-02 18:40:28 +0300 |
---|---|---|
committer | Patrick Mours <pmours@nvidia.com> | 2020-10-05 16:03:30 +0300 |
commit | 3df90de6c2268bef91d3754f71c7404cfbeeac90 (patch) | |
tree | d079ef4862e6db7233114554e478e0ef69303fc8 /intern/cycles/kernel | |
parent | 72e741ee62b54cbda9a7ad735cc2c6e4957c3af0 (diff) |
Cycles: Add NanoVDB support for rendering volumes
NanoVDB is a platform-independent sparse volume data structure that makes it possible to
use OpenVDB volumes on the GPU. This patch uses it for volume rendering in Cycles,
replacing the previous usage of dense 3D textures.
Since it has a big impact on memory usage and performance and changes the OpenVDB
branch used for the rest of Blender as well, this is not enabled by default yet, which will
happen only after 2.82 was branched off. To enable it, build both dependencies and Blender
itself with the "WITH_NANOVDB" CMake option.
Reviewed By: brecht
Differential Revision: https://developer.blender.org/D8794
Diffstat (limited to 'intern/cycles/kernel')
-rw-r--r-- | intern/cycles/kernel/CMakeLists.txt | 28 | ||||
-rw-r--r-- | intern/cycles/kernel/kernel_compat_optix.h | 1 | ||||
-rw-r--r-- | intern/cycles/kernel/kernels/cpu/kernel_cpu_image.h | 47 | ||||
-rw-r--r-- | intern/cycles/kernel/kernels/cuda/kernel_cuda_image.h | 67 | ||||
-rw-r--r-- | intern/cycles/kernel/kernels/opencl/kernel_opencl_image.h | 66 |
5 files changed, 197 insertions, 12 deletions
diff --git a/intern/cycles/kernel/CMakeLists.txt b/intern/cycles/kernel/CMakeLists.txt index 6651dc2d59d..c9afbb48e7e 100644 --- a/intern/cycles/kernel/CMakeLists.txt +++ b/intern/cycles/kernel/CMakeLists.txt @@ -442,6 +442,12 @@ if(WITH_CYCLES_CUDA_BINARIES) set(cuda_flags ${cuda_flags} -D __KERNEL_DEBUG__) endif() + if(WITH_NANOVDB) + set(cuda_flags ${cuda_flags} + -D WITH_NANOVDB + -I "${NANOVDB_INCLUDE_DIR}") + endif() + if(WITH_CYCLES_CUBIN_COMPILER) string(SUBSTRING ${arch} 3 -1 CUDA_ARCH) @@ -527,8 +533,14 @@ if(WITH_CYCLES_DEVICE_OPTIX AND WITH_CYCLES_CUDA_BINARIES) set(cuda_flags ${cuda_flags} -D __KERNEL_DEBUG__) endif() - if(WITH_CYCLES_CUBIN_COMPILER) + if(WITH_NANOVDB) + set(cuda_flags ${cuda_flags} + -D WITH_NANOVDB + -I "${NANOVDB_INCLUDE_DIR}") + endif() + + if(WITH_CYCLES_CUBIN_COMPILER) # Needed to find libnvrtc-builtins.so. Can't do it from inside # cycles_cubin_cc since the env variable is read before main() if(APPLE) @@ -709,3 +721,17 @@ delayed_install(${CMAKE_CURRENT_SOURCE_DIR} "${SRC_SVM_HEADERS}" ${CYCLES_INSTAL delayed_install(${CMAKE_CURRENT_SOURCE_DIR} "${SRC_GEOM_HEADERS}" ${CYCLES_INSTALL_PATH}/source/kernel/geom) delayed_install(${CMAKE_CURRENT_SOURCE_DIR} "${SRC_UTIL_HEADERS}" ${CYCLES_INSTALL_PATH}/source/util) delayed_install(${CMAKE_CURRENT_SOURCE_DIR} "${SRC_SPLIT_HEADERS}" ${CYCLES_INSTALL_PATH}/source/kernel/split) + + +if(WITH_NANOVDB) + set(SRC_NANOVDB_HEADERS + nanovdb/NanoVDB.h + nanovdb/CNanoVDB.h + ) + set(SRC_NANOVDB_UTIL_HEADERS + nanovdb/util/CSampleFromVoxels.h + nanovdb/util/SampleFromVoxels.h + ) + delayed_install(${NANOVDB_INCLUDE_DIR} "${SRC_NANOVDB_HEADERS}" ${CYCLES_INSTALL_PATH}/source/nanovdb) + delayed_install(${NANOVDB_INCLUDE_DIR} "${SRC_NANOVDB_UTIL_HEADERS}" ${CYCLES_INSTALL_PATH}/source/nanovdb/util) +endif() diff --git a/intern/cycles/kernel/kernel_compat_optix.h b/intern/cycles/kernel/kernel_compat_optix.h index 970f5cf864c..e58d8b2aa63 100644 --- a/intern/cycles/kernel/kernel_compat_optix.h +++ b/intern/cycles/kernel/kernel_compat_optix.h @@ -35,6 +35,7 @@ typedef unsigned int uint32_t; typedef unsigned long long uint64_t; typedef unsigned short half; typedef unsigned long long CUtexObject; + #ifdef CYCLES_CUBIN_CC # define FLT_MIN 1.175494350822287507969e-38f # define FLT_MAX 340282346638528859811704183484516925440.0f diff --git a/intern/cycles/kernel/kernels/cpu/kernel_cpu_image.h b/intern/cycles/kernel/kernels/cpu/kernel_cpu_image.h index f87501db258..347d0fec7f5 100644 --- a/intern/cycles/kernel/kernels/cpu/kernel_cpu_image.h +++ b/intern/cycles/kernel/kernels/cpu/kernel_cpu_image.h @@ -17,6 +17,11 @@ #ifndef __KERNEL_CPU_IMAGE_H__ #define __KERNEL_CPU_IMAGE_H__ +#ifdef WITH_NANOVDB +# include <nanovdb/NanoVDB.h> +# include <nanovdb/util/SampleFromVoxels.h> +#endif + CCL_NAMESPACE_BEGIN /* Make template functions private so symbols don't conflict between kernels with different @@ -470,6 +475,42 @@ template<typename T> struct TextureInterpolator { #undef SET_CUBIC_SPLINE_WEIGHTS }; +#ifdef WITH_NANOVDB +template<typename T> struct NanoVDBInterpolator { + static ccl_always_inline float4 read(float r) + { + return make_float4(r, r, r, 1.0f); + } + + static ccl_always_inline float4 read(nanovdb::Vec3f r) + { + return make_float4(r[0], r[1], r[2], 1.0f); + } + + static ccl_always_inline float4 + interp_3d(const TextureInfo &info, float x, float y, float z, InterpolationType interp) + { + nanovdb::NanoGrid<T> *const grid = (nanovdb::NanoGrid<T> *)info.data; + const nanovdb::NanoRoot<T> &root = grid->tree().root(); + + const nanovdb::Coord off(root.bbox().min()); + const nanovdb::Coord dim(root.bbox().dim()); + const nanovdb::Vec3f xyz(off[0] + x * dim[0], off[1] + y * dim[1], off[2] + z * dim[2]); + + typedef nanovdb::ReadAccessor<nanovdb::NanoRoot<T>> ReadAccessorT; + switch ((interp == INTERPOLATION_NONE) ? info.interpolation : interp) { + default: + case INTERPOLATION_LINEAR: + return read(nanovdb::SampleFromVoxels<ReadAccessorT, 1, false>(root)(xyz)); + case INTERPOLATION_CLOSEST: + return read(nanovdb::SampleFromVoxels<ReadAccessorT, 0, false>(root)(xyz)); + case INTERPOLATION_CUBIC: + return read(nanovdb::SampleFromVoxels<ReadAccessorT, 3, false>(root)(xyz)); + } + } +}; +#endif + ccl_device float4 kernel_tex_image_interp(KernelGlobals *kg, int id, float x, float y) { const TextureInfo &info = kernel_tex_fetch(__texture_info, id); @@ -526,6 +567,12 @@ ccl_device float4 kernel_tex_image_interp_3d(KernelGlobals *kg, return TextureInterpolator<ushort4>::interp_3d(info, P.x, P.y, P.z, interp); case IMAGE_DATA_TYPE_FLOAT4: return TextureInterpolator<float4>::interp_3d(info, P.x, P.y, P.z, interp); +#ifdef WITH_NANOVDB + case IMAGE_DATA_TYPE_NANOVDB_FLOAT: + return NanoVDBInterpolator<float>::interp_3d(info, P.x, P.y, P.z, interp); + case IMAGE_DATA_TYPE_NANOVDB_FLOAT3: + return NanoVDBInterpolator<nanovdb::Vec3f>::interp_3d(info, P.x, P.y, P.z, interp); +#endif default: assert(0); return make_float4( diff --git a/intern/cycles/kernel/kernels/cuda/kernel_cuda_image.h b/intern/cycles/kernel/kernels/cuda/kernel_cuda_image.h index 1d425d132a1..5a005a3f65b 100644 --- a/intern/cycles/kernel/kernels/cuda/kernel_cuda_image.h +++ b/intern/cycles/kernel/kernels/cuda/kernel_cuda_image.h @@ -14,6 +14,11 @@ * limitations under the License. */ +#ifdef WITH_NANOVDB +# include "nanovdb/NanoVDB.h" +# include "nanovdb/util/SampleFromVoxels.h" +#endif + /* w0, w1, w2, and w3 are the four cubic B-spline basis functions. */ ccl_device float cubic_w0(float a) { @@ -60,9 +65,10 @@ ccl_device float cubic_h1(float a) /* Fast bicubic texture lookup using 4 bilinear lookups, adapted from CUDA samples. */ template<typename T> -ccl_device T -kernel_tex_image_interp_bicubic(const TextureInfo &info, CUtexObject tex, float x, float y) +ccl_device T kernel_tex_image_interp_bicubic(const TextureInfo &info, float x, float y) { + CUtexObject tex = (CUtexObject)info.data; + x = (x * info.width) - 0.5f; y = (y * info.height) - 0.5f; @@ -84,9 +90,10 @@ kernel_tex_image_interp_bicubic(const TextureInfo &info, CUtexObject tex, float /* Fast tricubic texture lookup using 8 trilinear lookups. */ template<typename T> -ccl_device T kernel_tex_image_interp_bicubic_3d( - const TextureInfo &info, CUtexObject tex, float x, float y, float z) +ccl_device T kernel_tex_image_interp_bicubic_3d(const TextureInfo &info, float x, float y, float z) { + CUtexObject tex = (CUtexObject)info.data; + x = (x * info.width) - 0.5f; y = (y * info.height) - 0.5f; z = (z * info.depth) - 0.5f; @@ -118,19 +125,44 @@ ccl_device T kernel_tex_image_interp_bicubic_3d( g1y * (g0x * tex3D<T>(tex, x0, y1, z1) + g1x * tex3D<T>(tex, x1, y1, z1))); } +#ifdef WITH_NANOVDB +template<typename T> +ccl_device_inline T kernel_tex_image_interp_nanovdb( + const TextureInfo &info, float x, float y, float z, uint interpolation) +{ + nanovdb::NanoGrid<T> *const grid = (nanovdb::NanoGrid<T> *)info.data; + const nanovdb::NanoRoot<T> &root = grid->tree().root(); + + const nanovdb::Coord off(root.bbox().min()); + const nanovdb::Coord dim(root.bbox().dim()); + const nanovdb::Vec3f xyz(off[0] + x * dim[0], off[1] + y * dim[1], off[2] + z * dim[2]); + + typedef nanovdb::ReadAccessor<nanovdb::NanoRoot<T>> ReadAccessorT; + switch (interpolation) { + default: + case INTERPOLATION_LINEAR: + return nanovdb::SampleFromVoxels<ReadAccessorT, 1, false>(root)(xyz); + case INTERPOLATION_CLOSEST: + return nanovdb::SampleFromVoxels<ReadAccessorT, 0, false>(root)(xyz); + case INTERPOLATION_CUBIC: + return nanovdb::SampleFromVoxels<ReadAccessorT, 3, false>(root)(xyz); + } +} +#endif + ccl_device float4 kernel_tex_image_interp(KernelGlobals *kg, int id, float x, float y) { const TextureInfo &info = kernel_tex_fetch(__texture_info, id); - CUtexObject tex = (CUtexObject)info.data; /* float4, byte4, ushort4 and half4 */ const int texture_type = info.data_type; if (texture_type == IMAGE_DATA_TYPE_FLOAT4 || texture_type == IMAGE_DATA_TYPE_BYTE4 || texture_type == IMAGE_DATA_TYPE_HALF4 || texture_type == IMAGE_DATA_TYPE_USHORT4) { if (info.interpolation == INTERPOLATION_CUBIC) { - return kernel_tex_image_interp_bicubic<float4>(info, tex, x, y); + return kernel_tex_image_interp_bicubic<float4>(info, x, y); } else { + CUtexObject tex = (CUtexObject)info.data; return tex2D<float4>(tex, x, y); } } @@ -139,9 +171,10 @@ ccl_device float4 kernel_tex_image_interp(KernelGlobals *kg, int id, float x, fl float f; if (info.interpolation == INTERPOLATION_CUBIC) { - f = kernel_tex_image_interp_bicubic<float>(info, tex, x, y); + f = kernel_tex_image_interp_bicubic<float>(info, x, y); } else { + CUtexObject tex = (CUtexObject)info.data; f = tex2D<float>(tex, x, y); } @@ -164,16 +197,27 @@ ccl_device float4 kernel_tex_image_interp_3d(KernelGlobals *kg, const float y = P.y; const float z = P.z; - CUtexObject tex = (CUtexObject)info.data; uint interpolation = (interp == INTERPOLATION_NONE) ? info.interpolation : interp; - const int texture_type = info.data_type; + +#ifdef WITH_NANOVDB + if (texture_type == IMAGE_DATA_TYPE_NANOVDB_FLOAT) { + float f = kernel_tex_image_interp_nanovdb<float>(info, x, y, z, interpolation); + return make_float4(f, f, f, 1.0f); + } + if (texture_type == IMAGE_DATA_TYPE_NANOVDB_FLOAT3) { + nanovdb::Vec3f f = kernel_tex_image_interp_nanovdb<nanovdb::Vec3f>( + info, x, y, z, interpolation); + return make_float4(f[0], f[1], f[2], 1.0f); + } +#endif if (texture_type == IMAGE_DATA_TYPE_FLOAT4 || texture_type == IMAGE_DATA_TYPE_BYTE4 || texture_type == IMAGE_DATA_TYPE_HALF4 || texture_type == IMAGE_DATA_TYPE_USHORT4) { if (interpolation == INTERPOLATION_CUBIC) { - return kernel_tex_image_interp_bicubic_3d<float4>(info, tex, x, y, z); + return kernel_tex_image_interp_bicubic_3d<float4>(info, x, y, z); } else { + CUtexObject tex = (CUtexObject)info.data; return tex3D<float4>(tex, x, y, z); } } @@ -181,9 +225,10 @@ ccl_device float4 kernel_tex_image_interp_3d(KernelGlobals *kg, float f; if (interpolation == INTERPOLATION_CUBIC) { - f = kernel_tex_image_interp_bicubic_3d<float>(info, tex, x, y, z); + f = kernel_tex_image_interp_bicubic_3d<float>(info, x, y, z); } else { + CUtexObject tex = (CUtexObject)info.data; f = tex3D<float>(tex, x, y, z); } diff --git a/intern/cycles/kernel/kernels/opencl/kernel_opencl_image.h b/intern/cycles/kernel/kernels/opencl/kernel_opencl_image.h index 9ab374d1fba..2f44f249c5f 100644 --- a/intern/cycles/kernel/kernels/opencl/kernel_opencl_image.h +++ b/intern/cycles/kernel/kernels/opencl/kernel_opencl_image.h @@ -14,6 +14,11 @@ * limitations under the License. */ +#ifdef WITH_NANOVDB +# include "nanovdb/CNanoVDB.h" +# include "nanovdb/util/CSampleFromVoxels.h" +#endif + /* For OpenCL we do manual lookup and interpolation. */ ccl_device_inline ccl_global TextureInfo *kernel_tex_info(KernelGlobals *kg, uint id) @@ -223,6 +228,67 @@ ccl_device float4 kernel_tex_image_interp_3d(KernelGlobals *kg, int id, float3 P uint interpolation = (interp == INTERPOLATION_NONE) ? info->interpolation : interp; +#ifdef WITH_NANOVDB + if (info->data_type == IMAGE_DATA_TYPE_NANOVDB_FLOAT) { + ccl_global cnanovdb_griddata *grid = + (ccl_global cnanovdb_griddata *)(kg->buffers[info->cl_buffer] + info->data); + const ccl_global cnanovdb_rootdataF *root = cnanovdb_treedata_rootF( + cnanovdb_griddata_tree(grid)); + + cnanovdb_Vec3F xyz; + xyz.mVec[0] = root->mBBox_min.mVec[0] + + x * (root->mBBox_max.mVec[0] - root->mBBox_min.mVec[0]); + xyz.mVec[1] = root->mBBox_min.mVec[1] + + y * (root->mBBox_max.mVec[1] - root->mBBox_min.mVec[1]); + xyz.mVec[2] = root->mBBox_min.mVec[2] + + z * (root->mBBox_max.mVec[2] - root->mBBox_min.mVec[2]); + + cnanovdb_readaccessor acc; + cnanovdb_readaccessor_init(&acc, root); + + float value; + switch (interpolation) { + default: + case INTERPOLATION_LINEAR: + value = cnanovdb_sampleF_trilinear(&acc, &xyz); + break; + case INTERPOLATION_CLOSEST: + value = cnanovdb_sampleF_nearest(&acc, &xyz); + break; + } + return make_float4(value, value, value, 1.0f); + } + if (info->data_type == IMAGE_DATA_TYPE_NANOVDB_FLOAT3) { + ccl_global cnanovdb_griddata *grid = + (ccl_global cnanovdb_griddata *)(kg->buffers[info->cl_buffer] + info->data); + const ccl_global cnanovdb_rootdataF3 *root = cnanovdb_treedata_rootF3( + cnanovdb_griddata_tree(grid)); + + cnanovdb_Vec3F xyz; + xyz.mVec[0] = root->mBBox_min.mVec[0] + + x * (root->mBBox_max.mVec[0] - root->mBBox_min.mVec[0]); + xyz.mVec[1] = root->mBBox_min.mVec[1] + + y * (root->mBBox_max.mVec[1] - root->mBBox_min.mVec[1]); + xyz.mVec[2] = root->mBBox_min.mVec[2] + + z * (root->mBBox_max.mVec[2] - root->mBBox_min.mVec[2]); + + cnanovdb_readaccessor acc; + cnanovdb_readaccessor_init(&acc, root); + + cnanovdb_Vec3F value; + switch (interpolation) { + default: + case INTERPOLATION_LINEAR: + value = cnanovdb_sampleF3_trilinear(&acc, &xyz); + break; + case INTERPOLATION_CLOSEST: + value = cnanovdb_sampleF3_nearest(&acc, &xyz); + break; + } + return make_float4(value.mVec[0], value.mVec[1], value.mVec[2], 1.0f); + } +#endif + if (interpolation == INTERPOLATION_CLOSEST) { /* Closest interpolation. */ int ix, iy, iz; |