Blender V5.3
cycles/hydra/util.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2022 NVIDIA Corporation
2 * SPDX-FileCopyrightText: 2022 Blender Foundation
3 *
4 * SPDX-License-Identifier: Apache-2.0 */
5
6#pragma once
7
8#include "hydra/config.h"
9#include "scene/attribute.h"
10
11#include <pxr/base/vt/value.h>
12#include <pxr/imaging/hd/dataSource.h>
13#include <pxr/imaging/hd/enums.h>
14#include <pxr/imaging/hd/primvarsSchema.h>
15#include <pxr/imaging/hd/sceneIndex.h>
16#include <pxr/imaging/hd/timeSampleArray.h>
17#include <pxr/imaging/hd/types.h>
18
19PXR_NAMESPACE_OPEN_SCOPE
20class HdSceneDelegate;
21class SdfPath;
22class TfToken;
23PXR_NAMESPACE_CLOSE_SCOPE
24
26
27/* Prim data source from the scene index, or empty if none is reachable. */
28PXR_NS::HdSceneIndexPrim GetPrim(PXR_NS::HdSceneDelegate *delegate, const PXR_NS::SdfPath &id);
29
30/* Typed child data source, or null if missing or of a different type. */
31template<typename T>
32typename PXR_NS::HdTypedSampledDataSource<T>::Handle GetTypedDataSource(
33 const PXR_NS::HdContainerDataSourceHandle &container, const PXR_NS::TfToken &name)
34{
35 if (!container) {
36 return nullptr;
37 }
38 return PXR_NS::HdTypedSampledDataSource<T>::Cast(container->Get(name));
39}
40
41/* Typed value at shutter offset 0, or `fallback` if missing or of a different type. */
42template<typename T>
43T GetTypedValue(const PXR_NS::HdContainerDataSourceHandle &container,
44 const PXR_NS::TfToken &name,
45 const T &fallback = T())
46{
47 if (!container) {
48 return fallback;
49 }
50 const auto base = container->Get(name);
51 if (const auto typed = PXR_NS::HdTypedSampledDataSource<T>::Cast(base)) {
52 return typed->GetTypedValue(0.0f);
53 }
54 if (const auto sampled = PXR_NS::HdSampledDataSource::Cast(base)) {
55 PXR_NS::VtValue v = sampled->GetValue(0.0f);
56 if (v.IsHolding<T>()) {
57 return v.UncheckedGet<T>();
58 }
59 }
60 return fallback;
61}
62
63/* Sample a typed data source over the shutter into `out`. */
64template<typename T, unsigned int Capacity>
65void SampleTyped(const typename PXR_NS::HdTypedSampledDataSource<T>::Handle &ds,
66 float shutterOpen,
67 float shutterClose,
68 PXR_NS::HdTimeSampleArray<T, Capacity> *out)
69{
70 if (!ds) {
71 out->Resize(0);
72 return;
73 }
74 std::vector<float> times;
75 const bool hasSamples = ds->GetContributingSampleTimesForInterval(
76 shutterOpen, shutterClose, &times);
77 if (!hasSamples || times.empty()) {
78 out->Resize(1);
79 out->times[0] = 0.0f;
80 out->values[0] = ds->GetTypedValue(0.0f);
81 return;
82 }
83 out->Resize(times.size());
84 for (size_t i = 0; i < times.size(); ++i) {
85 out->times[i] = times[i];
86 out->values[i] = ds->GetTypedValue(times[i]);
87 }
88}
89
90/* Read a primvar's value at shutter offset 0. */
91PXR_NS::VtValue ReadPrimvar(const PXR_NS::HdPrimvarsSchema &primvars, const PXR_NS::TfToken &name);
92
93/* Read a primvar's interpolation. */
94PXR_NS::HdInterpolation ReadPrimvarInterpolation(const PXR_NS::HdPrimvarsSchema &primvars,
95 const PXR_NS::TfToken &name);
96
97/* Read a primvar's role token. */
98PXR_NS::TfToken ReadPrimvarRole(const PXR_NS::HdPrimvarsSchema &primvars,
99 const PXR_NS::TfToken &name);
100
101/* Enumerate primvar names whose interpolation matches. */
102PXR_NS::TfTokenVector PrimvarNamesAtInterpolation(const PXR_NS::HdPrimvarsSchema &primvars,
103 PXR_NS::HdInterpolation interpolation);
104
105/* Convert a Hydra primvar value to a Cycles attribute. */
106void ApplyPrimvars(CCL_NS::AttributeSet &attributes,
107 const CCL_NS::ustring &name,
108 PXR_NS::VtValue value,
109 CCL_NS::AttributeElement elem,
110 CCL_NS::AttributeStandard std);
111
void SampleTyped(const typename PXR_NS::HdTypedSampledDataSource< T >::Handle &ds, float shutterOpen, float shutterClose, PXR_NS::HdTimeSampleArray< T, Capacity > *out)
T GetTypedValue(const PXR_NS::HdContainerDataSourceHandle &container, const PXR_NS::TfToken &name, const T &fallback=T())
PXR_NS::HdTypedSampledDataSource< T >::Handle GetTypedDataSource(const PXR_NS::HdContainerDataSourceHandle &container, const PXR_NS::TfToken &name)
#define HDCYCLES_NAMESPACE_CLOSE_SCOPE
HdInterpolation ReadPrimvarInterpolation(const HdPrimvarsSchema &primvars, const TfToken &name)
TfToken ReadPrimvarRole(const HdPrimvarsSchema &primvars, const TfToken &name)
HDCYCLES_NAMESPACE_OPEN_SCOPE HdSceneIndexPrim GetPrim(HdSceneDelegate *delegate, const SdfPath &id)
TfTokenVector PrimvarNamesAtInterpolation(const HdPrimvarsSchema &primvars, HdInterpolation interpolation)
VtValue ReadPrimvar(const HdPrimvarsSchema &primvars, const TfToken &name)
void ApplyPrimvars(AttributeSet &attributes, const ustring &name, VtValue value, AttributeElement elem, AttributeStandard std)
#define T