d350976ba0
This patch adds a Hydra render delegate to Cycles, allowing Cycles to be used for rendering in applications that provide a Hydra viewport. The implementation was written from scratch against Cycles X, for integration into the Blender repository to make it possible to continue developing it in step with the rest of Cycles. For this purpose it follows the style of the rest of the Cycles code and can be built with a CMake option (`WITH_CYCLES_HYDRA_RENDER_DELEGATE=1`) similar to the existing standalone version of Cycles. Since Hydra render delegates need to be built against the exact USD version and other dependencies as the target application is using, this is intended to be built separate from Blender (`WITH_BLENDER=0` CMake option) and with support for library versions different from what Blender is using. As such the CMake build scripts for Windows had to be modified slightly, so that the Cycles Hydra render delegate can e.g. be built with MSVC 2017 again even though Blender requires MSVC 2019 now, and it's possible to specify custom paths to the USD SDK etc. The codebase supports building against the latest USD release 22.03 and all the way back to USD 20.08 (with some limitations). Reviewed By: brecht, LazyDodo Differential Revision: https://developer.blender.org/D14398
89 lines
2.1 KiB
C++
89 lines
2.1 KiB
C++
/* SPDX-License-Identifier: Apache-2.0
|
|
* Copyright 2022 NVIDIA Corporation
|
|
* Copyright 2022 Blender Foundation */
|
|
|
|
#include "hydra/field.h"
|
|
#include "hydra/session.h"
|
|
#include "scene/image_vdb.h"
|
|
#include "scene/scene.h"
|
|
|
|
#include <pxr/imaging/hd/sceneDelegate.h>
|
|
#include <pxr/usd/sdf/assetPath.h>
|
|
|
|
HDCYCLES_NAMESPACE_OPEN_SCOPE
|
|
|
|
// clang-format off
|
|
TF_DEFINE_PRIVATE_TOKENS(_tokens,
|
|
(fieldName)
|
|
);
|
|
// clang-format on
|
|
|
|
#ifdef WITH_OPENVDB
|
|
class HdCyclesVolumeLoader : public VDBImageLoader {
|
|
public:
|
|
HdCyclesVolumeLoader(const std::string &filePath, const std::string &gridName)
|
|
: VDBImageLoader(gridName)
|
|
{
|
|
openvdb::io::File file(filePath);
|
|
file.setCopyMaxBytes(0);
|
|
if (file.open()) {
|
|
grid = file.readGrid(gridName);
|
|
}
|
|
}
|
|
};
|
|
#endif
|
|
|
|
HdCyclesField::HdCyclesField(const SdfPath &bprimId, const TfToken &typeId) : HdField(bprimId)
|
|
{
|
|
}
|
|
|
|
HdCyclesField::~HdCyclesField()
|
|
{
|
|
}
|
|
|
|
HdDirtyBits HdCyclesField::GetInitialDirtyBitsMask() const
|
|
{
|
|
return DirtyBits::DirtyParams;
|
|
}
|
|
|
|
void HdCyclesField::Sync(HdSceneDelegate *sceneDelegate,
|
|
HdRenderParam *renderParam,
|
|
HdDirtyBits *dirtyBits)
|
|
{
|
|
#ifdef WITH_OPENVDB
|
|
VtValue value;
|
|
const SdfPath &id = GetId();
|
|
|
|
if (*dirtyBits & DirtyBits::DirtyParams) {
|
|
value = sceneDelegate->Get(id, HdFieldTokens->filePath);
|
|
if (value.IsHolding<SdfAssetPath>()) {
|
|
std::string filename = value.UncheckedGet<SdfAssetPath>().GetResolvedPath();
|
|
if (filename.empty()) {
|
|
filename = value.UncheckedGet<SdfAssetPath>().GetAssetPath();
|
|
}
|
|
|
|
# if PXR_VERSION >= 2108
|
|
value = sceneDelegate->Get(id, HdFieldTokens->fieldName);
|
|
# else
|
|
value = sceneDelegate->Get(id, _tokens->fieldName);
|
|
# endif
|
|
if (value.IsHolding<TfToken>()) {
|
|
ImageLoader *const loader = new HdCyclesVolumeLoader(
|
|
filename, value.UncheckedGet<TfToken>().GetString());
|
|
|
|
const SceneLock lock(renderParam);
|
|
|
|
ImageParams params;
|
|
params.frame = 0.0f;
|
|
|
|
_handle = lock.scene->image_manager->add_image(loader, params, false);
|
|
}
|
|
}
|
|
}
|
|
#endif
|
|
|
|
*dirtyBits = DirtyBits::Clean;
|
|
}
|
|
|
|
HDCYCLES_NAMESPACE_CLOSE_SCOPE
|