Skip to content

Commit ad5d719

Browse files
unhyperbolicpixar-oss
authored andcommitted
Introducing HdExtComputationCpuCallback and using it in the HdExtComputation schema.
It replaces the HdExtComputationCallbackDataSource in dataSourceLegacyPrim.h. (Internal change: 2341896)
1 parent 4c94900 commit ad5d719

12 files changed

+124
-66
lines changed

pxr/imaging/hd/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ pxr_library(hd
6868
extComputation
6969
extComputationContext
7070
extComputationContextInternal
71+
extComputationCpuCallback
7172
extComputationInputComputationSchema
7273
extComputationOutputSchema
7374
extComputationPrimvarSchema

pxr/imaging/hd/dataSourceLegacyPrim.cpp

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include "pxr/imaging/hd/camera.h"
1010
#include "pxr/imaging/hd/dataSource.h"
11+
#include "pxr/imaging/hd/extComputationCpuCallback.h"
1112
#include "pxr/imaging/hd/material.h"
1213
#include "pxr/imaging/hd/light.h"
1314
#include "pxr/imaging/hd/meshTopology.h"
@@ -97,6 +98,24 @@ HdLegacyPrimTypeIsVolumeField(TfToken const &primType)
9798

9899
namespace {
99100

101+
class Hd_SceneDelegateExtComputationCpuCallback
102+
: public HdExtComputationCpuCallback
103+
{
104+
public:
105+
Hd_SceneDelegateExtComputationCpuCallback(
106+
const SdfPath &id, HdSceneDelegate * const sceneDelegate)
107+
: _id(id), _sceneDelegate(sceneDelegate) { }
108+
109+
void Compute(HdExtComputationContext * const ctx) override
110+
{
111+
_sceneDelegate->InvokeExtComputation(_id, ctx);
112+
}
113+
114+
private:
115+
const SdfPath _id;
116+
HdSceneDelegate * const _sceneDelegate;
117+
};
118+
100119
class Hd_DataSourceLegacyPrimvarValue : public HdSampledDataSource
101120
{
102121
public:
@@ -1972,8 +1991,12 @@ class Hd_DataSourceLegacyExtComputation : public HdContainerDataSource
19721991
std::string kernel = _sceneDelegate->GetExtComputationKernel(_id);
19731992
return HdRetainedTypedSampledDataSource<std::string>::New(kernel);
19741993
} else if (name == HdExtComputationSchemaTokens->cpuCallback) {
1975-
return HdExtComputationCallbackDataSource::New(
1976-
_id, _sceneDelegate);
1994+
return
1995+
HdRetainedTypedSampledDataSource<
1996+
HdExtComputationCpuCallbackSharedPtr>::New(
1997+
std::make_shared<
1998+
Hd_SceneDelegateExtComputationCpuCallback>(
1999+
_id, _sceneDelegate));
19772000
} else if (name == HdExtComputationSchemaTokens->dispatchCount) {
19782001
const VtValue vDispatch = _sceneDelegate->GetExtComputationInput(
19792002
_id, HdTokens->dispatchCount);
@@ -2343,14 +2366,6 @@ TfToken _InterpolationAsToken(HdInterpolation interpolation)
23432366

23442367
// ----------------------------------------------------------------------------
23452368

2346-
void
2347-
HdExtComputationCallbackDataSource::Invoke(HdExtComputationContext *context)
2348-
{
2349-
_sceneDelegate->InvokeExtComputation(_id, context);
2350-
}
2351-
2352-
// ----------------------------------------------------------------------------
2353-
23542369
HdDataSourceLegacyPrim::HdDataSourceLegacyPrim(
23552370
const SdfPath& id,
23562371
const TfToken& type,

pxr/imaging/hd/dataSourceLegacyPrim.h

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
PXR_NAMESPACE_OPEN_SCOPE
2424

2525
class HdSceneDelegate;
26-
class HdExtComputationContext;
2726

2827
#define HD_LEGACY_PRIMTYPE_TOKENS \
2928
/* Bprims */ \
@@ -33,32 +32,6 @@ class HdExtComputationContext;
3332
TF_DECLARE_PUBLIC_TOKENS(HdLegacyPrimTypeTokens, HD_API,
3433
HD_LEGACY_PRIMTYPE_TOKENS);
3534

36-
/// \class HdExtComputationCallbackDataSource
37-
///
38-
/// This is a data source which holds a legacy ext computation. It is used
39-
/// only during emulation of legacy scene delegates but is exposed here as it
40-
/// is used by HdSceneIndexAdapterSceneDelegate for emulation of legacy
41-
/// render delegates.
42-
///
43-
class HdExtComputationCallbackDataSource : public HdDataSourceBase
44-
{
45-
public:
46-
HD_DECLARE_DATASOURCE(HdExtComputationCallbackDataSource);
47-
48-
HdExtComputationCallbackDataSource(
49-
const SdfPath &id, HdSceneDelegate *sceneDelegate)
50-
: _id(id), _sceneDelegate(sceneDelegate) {}
51-
52-
HD_API
53-
void Invoke(HdExtComputationContext *context);
54-
55-
private:
56-
SdfPath _id;
57-
HdSceneDelegate *_sceneDelegate;
58-
};
59-
60-
HD_DECLARE_DATASOURCE_HANDLES(HdExtComputationCallbackDataSource);
61-
6235
/// \class HdDataSourceLegacyPrim
6336
///
6437
/// This is an HdContainerDataSource which represents a prim-level data source
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// Copyright 2024 Pixar
3+
//
4+
// Licensed under the terms set forth in the LICENSE.txt file available at
5+
// https://openusd.org/license.
6+
//
7+
#include "pxr/imaging/hd/extComputationCpuCallback.h"
8+
9+
PXR_NAMESPACE_OPEN_SCOPE
10+
11+
HdExtComputationCpuCallback::~HdExtComputationCpuCallback() = default;
12+
13+
PXR_NAMESPACE_CLOSE_SCOPE
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//
2+
// Copyright 2024 Pixar
3+
//
4+
// Licensed under the terms set forth in the LICENSE.txt file available at
5+
// https://openusd.org/license.
6+
//
7+
#ifndef PXR_IMAGING_HD_EXT_COMPUTATION_CPU_CALLBACK_H
8+
#define PXR_IMAGING_HD_EXT_COMPUTATION_CPU_CALLBACK_H
9+
10+
#include "pxr/pxr.h"
11+
#include "pxr/imaging/hd/api.h"
12+
13+
#include <memory>
14+
15+
PXR_NAMESPACE_OPEN_SCOPE
16+
17+
class HdExtComputationContext;
18+
using HdExtComputationCpuCallbackSharedPtr =
19+
std::shared_ptr<class HdExtComputationCpuCallback>;
20+
21+
/// \class HdExtComputationCallback
22+
///
23+
/// A callback for an ext computation filling the outputs given the
24+
/// input values and values of the input computations.
25+
///
26+
class HdExtComputationCpuCallback
27+
{
28+
public:
29+
HD_API virtual ~HdExtComputationCpuCallback();
30+
31+
/// Run the computation.
32+
virtual void Compute(HdExtComputationContext * ctx) = 0;
33+
};
34+
35+
PXR_NAMESPACE_CLOSE_SCOPE
36+
37+
#endif

pxr/imaging/hd/extComputationPrimvarsSchema.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,4 @@ HdExtComputationPrimvarsSchema::GetDefaultLocator()
8686
return locator;
8787
}
8888

89-
PXR_NAMESPACE_CLOSE_SCOPE
89+
PXR_NAMESPACE_CLOSE_SCOPE

pxr/imaging/hd/extComputationPrimvarsSchema.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,4 @@ class HdExtComputationPrimvarsSchema : public HdSchema
105105

106106
PXR_NAMESPACE_CLOSE_SCOPE
107107

108-
#endif
108+
#endif

pxr/imaging/hd/extComputationSchema.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ HdExtComputationSchema::GetGlslKernel() const
6060
HdExtComputationSchemaTokens->glslKernel);
6161
}
6262

63-
HdDataSourceBaseHandle
63+
HdExtComputationCpuCallbackDataSourceHandle
6464
HdExtComputationSchema::GetCpuCallback() const
6565
{
66-
return _GetTypedDataSource<HdDataSourceBase>(
66+
return _GetTypedDataSource<HdExtComputationCpuCallbackDataSource>(
6767
HdExtComputationSchemaTokens->cpuCallback);
6868
}
6969

@@ -88,7 +88,7 @@ HdExtComputationSchema::BuildRetained(
8888
const HdContainerDataSourceHandle &inputComputations,
8989
const HdContainerDataSourceHandle &outputs,
9090
const HdStringDataSourceHandle &glslKernel,
91-
const HdDataSourceBaseHandle &cpuCallback,
91+
const HdExtComputationCpuCallbackDataSourceHandle &cpuCallback,
9292
const HdSizetDataSourceHandle &dispatchCount,
9393
const HdSizetDataSourceHandle &elementCount
9494
)
@@ -169,7 +169,7 @@ HdExtComputationSchema::Builder::SetGlslKernel(
169169

170170
HdExtComputationSchema::Builder &
171171
HdExtComputationSchema::Builder::SetCpuCallback(
172-
const HdDataSourceBaseHandle &cpuCallback)
172+
const HdExtComputationCpuCallbackDataSourceHandle &cpuCallback)
173173
{
174174
_cpuCallback = cpuCallback;
175175
return *this;

pxr/imaging/hd/extComputationSchema.h

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@
3131
PXR_NAMESPACE_OPEN_SCOPE
3232

3333
// --(BEGIN CUSTOM CODE: Declares)--
34+
35+
using HdExtComputationCpuCallbackSharedPtr =
36+
std::shared_ptr<class HdExtComputationCpuCallback>;
37+
using HdExtComputationCpuCallbackDataSource =
38+
HdTypedSampledDataSource<HdExtComputationCpuCallbackSharedPtr>;
39+
using HdExtComputationCpuCallbackDataSourceHandle =
40+
HdExtComputationCpuCallbackDataSource::Handle;
41+
3442
// --(END CUSTOM CODE: Declares)--
3543

3644
#define HD_EXT_COMPUTATION_SCHEMA_TOKENS \
@@ -88,7 +96,7 @@ class HdExtComputationSchema : public HdSchema
8896
HdStringDataSourceHandle GetGlslKernel() const;
8997

9098
HD_API
91-
HdDataSourceBaseHandle GetCpuCallback() const;
99+
HdExtComputationCpuCallbackDataSourceHandle GetCpuCallback() const;
92100

93101
HD_API
94102
HdSizetDataSourceHandle GetDispatchCount() const;
@@ -168,7 +176,7 @@ class HdExtComputationSchema : public HdSchema
168176
const HdContainerDataSourceHandle &inputComputations,
169177
const HdContainerDataSourceHandle &outputs,
170178
const HdStringDataSourceHandle &glslKernel,
171-
const HdDataSourceBaseHandle &cpuCallback,
179+
const HdExtComputationCpuCallbackDataSourceHandle &cpuCallback,
172180
const HdSizetDataSourceHandle &dispatchCount,
173181
const HdSizetDataSourceHandle &elementCount
174182
);
@@ -196,7 +204,7 @@ class HdExtComputationSchema : public HdSchema
196204
const HdStringDataSourceHandle &glslKernel);
197205
HD_API
198206
Builder &SetCpuCallback(
199-
const HdDataSourceBaseHandle &cpuCallback);
207+
const HdExtComputationCpuCallbackDataSourceHandle &cpuCallback);
200208
HD_API
201209
Builder &SetDispatchCount(
202210
const HdSizetDataSourceHandle &dispatchCount);
@@ -213,7 +221,7 @@ class HdExtComputationSchema : public HdSchema
213221
HdContainerDataSourceHandle _inputComputations;
214222
HdContainerDataSourceHandle _outputs;
215223
HdStringDataSourceHandle _glslKernel;
216-
HdDataSourceBaseHandle _cpuCallback;
224+
HdExtComputationCpuCallbackDataSourceHandle _cpuCallback;
217225
HdSizetDataSourceHandle _dispatchCount;
218226
HdSizetDataSourceHandle _elementCount;
219227

@@ -224,4 +232,4 @@ class HdExtComputationSchema : public HdSchema
224232

225233
PXR_NAMESPACE_CLOSE_SCOPE
226234

227-
#endif
235+
#endif

pxr/imaging/hd/hdSchemaDefs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1179,7 +1179,7 @@
11791179
('inputComputations', 'HdExtComputationInputComputationContainerSchema', {}),
11801180
('outputs', 'HdExtComputationOutputContainerSchema', {}),
11811181
('glslKernel', T_STRING, {}),
1182-
('cpuCallback', T_BASE, {}),
1182+
('cpuCallback', 'HdExtComputationCpuCallbackDataSource', {}),
11831183
('dispatchCount', T_SIZET, {}),
11841184
('elementCount', T_SIZET, {}),
11851185
],

pxr/imaging/hd/sceneIndexAdapterSceneDelegate.cpp

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "pxr/imaging/hd/dataSourceTypeDefs.h"
1717
#include "pxr/imaging/hd/dirtyBitsTranslator.h"
1818
#include "pxr/imaging/hd/enums.h"
19+
#include "pxr/imaging/hd/extComputationCpuCallback.h"
1920
#include "pxr/imaging/hd/field.h"
2021
#include "pxr/imaging/hd/geomSubset.h"
2122
#include "pxr/imaging/hd/light.h"
@@ -2700,21 +2701,25 @@ HdSceneIndexAdapterSceneDelegate::GetExtComputationKernel(
27002701

27012702
void
27022703
HdSceneIndexAdapterSceneDelegate::InvokeExtComputation(
2703-
SdfPath const &computationId, HdExtComputationContext *context)
2704+
SdfPath const &computationId, HdExtComputationContext * const context)
27042705
{
27052706
TRACE_FUNCTION();
27062707
HF_MALLOC_TAG_FUNCTION();
27072708

2708-
HdSceneIndexPrim prim = _GetInputPrim(computationId);
2709-
if (HdExtComputationSchema extComputation =
2710-
HdExtComputationSchema::GetFromParent(prim.dataSource)) {
2711-
HdExtComputationCallbackDataSourceHandle ds =
2712-
HdExtComputationCallbackDataSource::Cast(
2713-
extComputation.GetCpuCallback());
2714-
if (ds) {
2715-
ds->Invoke(context);
2716-
}
2709+
const HdSceneIndexPrim prim = _GetInputPrim(computationId);
2710+
const HdExtComputationSchema extComputation =
2711+
HdExtComputationSchema::GetFromParent(prim.dataSource);
2712+
HdExtComputationCpuCallbackDataSourceHandle const ds =
2713+
extComputation.GetCpuCallback();
2714+
if (!ds) {
2715+
return;
2716+
}
2717+
HdExtComputationCpuCallbackSharedPtr const callback =
2718+
ds->GetTypedValue(0.0f);
2719+
if (!callback) {
2720+
return;
27172721
}
2722+
callback->Compute(context);
27182723
}
27192724

27202725
void

pxr/imaging/hdsi/extComputationPrimvarPruningSceneIndex.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "pxr/imaging/hd/dataSourceTypeDefs.h"
1313
#include "pxr/imaging/hd/dataSourceLegacyPrim.h"
1414
#include "pxr/imaging/hd/extComputationContextInternal.h"
15+
#include "pxr/imaging/hd/extComputationCpuCallback.h"
1516
#include "pxr/imaging/hd/extComputationSchema.h"
1617
#include "pxr/imaging/hd/extComputationInputComputationSchema.h"
1718
#include "pxr/imaging/hd/extComputationOutputSchema.h"
@@ -379,17 +380,22 @@ class _ExtComputationContext
379380
// Execute computation ....
380381
// Note: Handle only scene index emulated ext computations
381382
// via the cast below.
382-
if (HdExtComputationCallbackDataSourceHandle callbackDs =
383-
HdExtComputationCallbackDataSource::Cast(
384-
cs.GetCpuCallback())) {
385-
386-
callbackDs->Invoke(&executionContext);
387-
388-
} else {
383+
HdExtComputationCpuCallbackDataSourceHandle const ds =
384+
cs.GetCpuCallback();
385+
if (!ds) {
389386
TF_WARN("Could not find CPU callback data source for %s",
390387
compId.GetText());
391388
continue;
392389
}
390+
HdExtComputationCpuCallbackSharedPtr const callback =
391+
ds->GetTypedValue(0.0f);
392+
if (!callback) {
393+
TF_WARN("Invalid CPU callback for %s",
394+
compId.GetText());
395+
continue;
396+
}
397+
398+
callback->Compute(&executionContext);
393399

394400
// ... and add outputs to the value store.
395401
if (executionContext.HasComputationError()) {

0 commit comments

Comments
 (0)