Skip to content

Commit

Permalink
Introducing HdExtComputationCpuCallback and using it in the HdExtComp…
Browse files Browse the repository at this point in the history
…utation schema.

It replaces the HdExtComputationCallbackDataSource in dataSourceLegacyPrim.h.

(Internal change: 2341896)
  • Loading branch information
unhyperbolic authored and pixar-oss committed Sep 24, 2024
1 parent 4c94900 commit ad5d719
Show file tree
Hide file tree
Showing 12 changed files with 124 additions and 66 deletions.
1 change: 1 addition & 0 deletions pxr/imaging/hd/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ pxr_library(hd
extComputation
extComputationContext
extComputationContextInternal
extComputationCpuCallback
extComputationInputComputationSchema
extComputationOutputSchema
extComputationPrimvarSchema
Expand Down
35 changes: 25 additions & 10 deletions pxr/imaging/hd/dataSourceLegacyPrim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "pxr/imaging/hd/camera.h"
#include "pxr/imaging/hd/dataSource.h"
#include "pxr/imaging/hd/extComputationCpuCallback.h"
#include "pxr/imaging/hd/material.h"
#include "pxr/imaging/hd/light.h"
#include "pxr/imaging/hd/meshTopology.h"
Expand Down Expand Up @@ -97,6 +98,24 @@ HdLegacyPrimTypeIsVolumeField(TfToken const &primType)

namespace {

class Hd_SceneDelegateExtComputationCpuCallback
: public HdExtComputationCpuCallback
{
public:
Hd_SceneDelegateExtComputationCpuCallback(
const SdfPath &id, HdSceneDelegate * const sceneDelegate)
: _id(id), _sceneDelegate(sceneDelegate) { }

void Compute(HdExtComputationContext * const ctx) override
{
_sceneDelegate->InvokeExtComputation(_id, ctx);
}

private:
const SdfPath _id;
HdSceneDelegate * const _sceneDelegate;
};

class Hd_DataSourceLegacyPrimvarValue : public HdSampledDataSource
{
public:
Expand Down Expand Up @@ -1972,8 +1991,12 @@ class Hd_DataSourceLegacyExtComputation : public HdContainerDataSource
std::string kernel = _sceneDelegate->GetExtComputationKernel(_id);
return HdRetainedTypedSampledDataSource<std::string>::New(kernel);
} else if (name == HdExtComputationSchemaTokens->cpuCallback) {
return HdExtComputationCallbackDataSource::New(
_id, _sceneDelegate);
return
HdRetainedTypedSampledDataSource<
HdExtComputationCpuCallbackSharedPtr>::New(
std::make_shared<
Hd_SceneDelegateExtComputationCpuCallback>(
_id, _sceneDelegate));
} else if (name == HdExtComputationSchemaTokens->dispatchCount) {
const VtValue vDispatch = _sceneDelegate->GetExtComputationInput(
_id, HdTokens->dispatchCount);
Expand Down Expand Up @@ -2343,14 +2366,6 @@ TfToken _InterpolationAsToken(HdInterpolation interpolation)

// ----------------------------------------------------------------------------

void
HdExtComputationCallbackDataSource::Invoke(HdExtComputationContext *context)
{
_sceneDelegate->InvokeExtComputation(_id, context);
}

// ----------------------------------------------------------------------------

HdDataSourceLegacyPrim::HdDataSourceLegacyPrim(
const SdfPath& id,
const TfToken& type,
Expand Down
27 changes: 0 additions & 27 deletions pxr/imaging/hd/dataSourceLegacyPrim.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
PXR_NAMESPACE_OPEN_SCOPE

class HdSceneDelegate;
class HdExtComputationContext;

#define HD_LEGACY_PRIMTYPE_TOKENS \
/* Bprims */ \
Expand All @@ -33,32 +32,6 @@ class HdExtComputationContext;
TF_DECLARE_PUBLIC_TOKENS(HdLegacyPrimTypeTokens, HD_API,
HD_LEGACY_PRIMTYPE_TOKENS);

/// \class HdExtComputationCallbackDataSource
///
/// This is a data source which holds a legacy ext computation. It is used
/// only during emulation of legacy scene delegates but is exposed here as it
/// is used by HdSceneIndexAdapterSceneDelegate for emulation of legacy
/// render delegates.
///
class HdExtComputationCallbackDataSource : public HdDataSourceBase
{
public:
HD_DECLARE_DATASOURCE(HdExtComputationCallbackDataSource);

HdExtComputationCallbackDataSource(
const SdfPath &id, HdSceneDelegate *sceneDelegate)
: _id(id), _sceneDelegate(sceneDelegate) {}

HD_API
void Invoke(HdExtComputationContext *context);

private:
SdfPath _id;
HdSceneDelegate *_sceneDelegate;
};

HD_DECLARE_DATASOURCE_HANDLES(HdExtComputationCallbackDataSource);

/// \class HdDataSourceLegacyPrim
///
/// This is an HdContainerDataSource which represents a prim-level data source
Expand Down
13 changes: 13 additions & 0 deletions pxr/imaging/hd/extComputationCpuCallback.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// Copyright 2024 Pixar
//
// Licensed under the terms set forth in the LICENSE.txt file available at
// https://openusd.org/license.
//
#include "pxr/imaging/hd/extComputationCpuCallback.h"

PXR_NAMESPACE_OPEN_SCOPE

HdExtComputationCpuCallback::~HdExtComputationCpuCallback() = default;

PXR_NAMESPACE_CLOSE_SCOPE
37 changes: 37 additions & 0 deletions pxr/imaging/hd/extComputationCpuCallback.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//
// Copyright 2024 Pixar
//
// Licensed under the terms set forth in the LICENSE.txt file available at
// https://openusd.org/license.
//
#ifndef PXR_IMAGING_HD_EXT_COMPUTATION_CPU_CALLBACK_H
#define PXR_IMAGING_HD_EXT_COMPUTATION_CPU_CALLBACK_H

#include "pxr/pxr.h"
#include "pxr/imaging/hd/api.h"

#include <memory>

PXR_NAMESPACE_OPEN_SCOPE

class HdExtComputationContext;
using HdExtComputationCpuCallbackSharedPtr =
std::shared_ptr<class HdExtComputationCpuCallback>;

/// \class HdExtComputationCallback
///
/// A callback for an ext computation filling the outputs given the
/// input values and values of the input computations.
///
class HdExtComputationCpuCallback
{
public:
HD_API virtual ~HdExtComputationCpuCallback();

/// Run the computation.
virtual void Compute(HdExtComputationContext * ctx) = 0;
};

PXR_NAMESPACE_CLOSE_SCOPE

#endif
2 changes: 1 addition & 1 deletion pxr/imaging/hd/extComputationPrimvarsSchema.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,4 @@ HdExtComputationPrimvarsSchema::GetDefaultLocator()
return locator;
}

PXR_NAMESPACE_CLOSE_SCOPE
PXR_NAMESPACE_CLOSE_SCOPE
2 changes: 1 addition & 1 deletion pxr/imaging/hd/extComputationPrimvarsSchema.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,4 @@ class HdExtComputationPrimvarsSchema : public HdSchema

PXR_NAMESPACE_CLOSE_SCOPE

#endif
#endif
8 changes: 4 additions & 4 deletions pxr/imaging/hd/extComputationSchema.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ HdExtComputationSchema::GetGlslKernel() const
HdExtComputationSchemaTokens->glslKernel);
}

HdDataSourceBaseHandle
HdExtComputationCpuCallbackDataSourceHandle
HdExtComputationSchema::GetCpuCallback() const
{
return _GetTypedDataSource<HdDataSourceBase>(
return _GetTypedDataSource<HdExtComputationCpuCallbackDataSource>(
HdExtComputationSchemaTokens->cpuCallback);
}

Expand All @@ -88,7 +88,7 @@ HdExtComputationSchema::BuildRetained(
const HdContainerDataSourceHandle &inputComputations,
const HdContainerDataSourceHandle &outputs,
const HdStringDataSourceHandle &glslKernel,
const HdDataSourceBaseHandle &cpuCallback,
const HdExtComputationCpuCallbackDataSourceHandle &cpuCallback,
const HdSizetDataSourceHandle &dispatchCount,
const HdSizetDataSourceHandle &elementCount
)
Expand Down Expand Up @@ -169,7 +169,7 @@ HdExtComputationSchema::Builder::SetGlslKernel(

HdExtComputationSchema::Builder &
HdExtComputationSchema::Builder::SetCpuCallback(
const HdDataSourceBaseHandle &cpuCallback)
const HdExtComputationCpuCallbackDataSourceHandle &cpuCallback)
{
_cpuCallback = cpuCallback;
return *this;
Expand Down
18 changes: 13 additions & 5 deletions pxr/imaging/hd/extComputationSchema.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@
PXR_NAMESPACE_OPEN_SCOPE

// --(BEGIN CUSTOM CODE: Declares)--

using HdExtComputationCpuCallbackSharedPtr =
std::shared_ptr<class HdExtComputationCpuCallback>;
using HdExtComputationCpuCallbackDataSource =
HdTypedSampledDataSource<HdExtComputationCpuCallbackSharedPtr>;
using HdExtComputationCpuCallbackDataSourceHandle =
HdExtComputationCpuCallbackDataSource::Handle;

// --(END CUSTOM CODE: Declares)--

#define HD_EXT_COMPUTATION_SCHEMA_TOKENS \
Expand Down Expand Up @@ -88,7 +96,7 @@ class HdExtComputationSchema : public HdSchema
HdStringDataSourceHandle GetGlslKernel() const;

HD_API
HdDataSourceBaseHandle GetCpuCallback() const;
HdExtComputationCpuCallbackDataSourceHandle GetCpuCallback() const;

HD_API
HdSizetDataSourceHandle GetDispatchCount() const;
Expand Down Expand Up @@ -168,7 +176,7 @@ class HdExtComputationSchema : public HdSchema
const HdContainerDataSourceHandle &inputComputations,
const HdContainerDataSourceHandle &outputs,
const HdStringDataSourceHandle &glslKernel,
const HdDataSourceBaseHandle &cpuCallback,
const HdExtComputationCpuCallbackDataSourceHandle &cpuCallback,
const HdSizetDataSourceHandle &dispatchCount,
const HdSizetDataSourceHandle &elementCount
);
Expand Down Expand Up @@ -196,7 +204,7 @@ class HdExtComputationSchema : public HdSchema
const HdStringDataSourceHandle &glslKernel);
HD_API
Builder &SetCpuCallback(
const HdDataSourceBaseHandle &cpuCallback);
const HdExtComputationCpuCallbackDataSourceHandle &cpuCallback);
HD_API
Builder &SetDispatchCount(
const HdSizetDataSourceHandle &dispatchCount);
Expand All @@ -213,7 +221,7 @@ class HdExtComputationSchema : public HdSchema
HdContainerDataSourceHandle _inputComputations;
HdContainerDataSourceHandle _outputs;
HdStringDataSourceHandle _glslKernel;
HdDataSourceBaseHandle _cpuCallback;
HdExtComputationCpuCallbackDataSourceHandle _cpuCallback;
HdSizetDataSourceHandle _dispatchCount;
HdSizetDataSourceHandle _elementCount;

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

PXR_NAMESPACE_CLOSE_SCOPE

#endif
#endif
2 changes: 1 addition & 1 deletion pxr/imaging/hd/hdSchemaDefs.py
Original file line number Diff line number Diff line change
Expand Up @@ -1179,7 +1179,7 @@
('inputComputations', 'HdExtComputationInputComputationContainerSchema', {}),
('outputs', 'HdExtComputationOutputContainerSchema', {}),
('glslKernel', T_STRING, {}),
('cpuCallback', T_BASE, {}),
('cpuCallback', 'HdExtComputationCpuCallbackDataSource', {}),
('dispatchCount', T_SIZET, {}),
('elementCount', T_SIZET, {}),
],
Expand Down
25 changes: 15 additions & 10 deletions pxr/imaging/hd/sceneIndexAdapterSceneDelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "pxr/imaging/hd/dataSourceTypeDefs.h"
#include "pxr/imaging/hd/dirtyBitsTranslator.h"
#include "pxr/imaging/hd/enums.h"
#include "pxr/imaging/hd/extComputationCpuCallback.h"
#include "pxr/imaging/hd/field.h"
#include "pxr/imaging/hd/geomSubset.h"
#include "pxr/imaging/hd/light.h"
Expand Down Expand Up @@ -2700,21 +2701,25 @@ HdSceneIndexAdapterSceneDelegate::GetExtComputationKernel(

void
HdSceneIndexAdapterSceneDelegate::InvokeExtComputation(
SdfPath const &computationId, HdExtComputationContext *context)
SdfPath const &computationId, HdExtComputationContext * const context)
{
TRACE_FUNCTION();
HF_MALLOC_TAG_FUNCTION();

HdSceneIndexPrim prim = _GetInputPrim(computationId);
if (HdExtComputationSchema extComputation =
HdExtComputationSchema::GetFromParent(prim.dataSource)) {
HdExtComputationCallbackDataSourceHandle ds =
HdExtComputationCallbackDataSource::Cast(
extComputation.GetCpuCallback());
if (ds) {
ds->Invoke(context);
}
const HdSceneIndexPrim prim = _GetInputPrim(computationId);
const HdExtComputationSchema extComputation =
HdExtComputationSchema::GetFromParent(prim.dataSource);
HdExtComputationCpuCallbackDataSourceHandle const ds =
extComputation.GetCpuCallback();
if (!ds) {
return;
}
HdExtComputationCpuCallbackSharedPtr const callback =
ds->GetTypedValue(0.0f);
if (!callback) {
return;
}
callback->Compute(context);
}

void
Expand Down
20 changes: 13 additions & 7 deletions pxr/imaging/hdsi/extComputationPrimvarPruningSceneIndex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "pxr/imaging/hd/dataSourceTypeDefs.h"
#include "pxr/imaging/hd/dataSourceLegacyPrim.h"
#include "pxr/imaging/hd/extComputationContextInternal.h"
#include "pxr/imaging/hd/extComputationCpuCallback.h"
#include "pxr/imaging/hd/extComputationSchema.h"
#include "pxr/imaging/hd/extComputationInputComputationSchema.h"
#include "pxr/imaging/hd/extComputationOutputSchema.h"
Expand Down Expand Up @@ -379,17 +380,22 @@ class _ExtComputationContext
// Execute computation ....
// Note: Handle only scene index emulated ext computations
// via the cast below.
if (HdExtComputationCallbackDataSourceHandle callbackDs =
HdExtComputationCallbackDataSource::Cast(
cs.GetCpuCallback())) {

callbackDs->Invoke(&executionContext);

} else {
HdExtComputationCpuCallbackDataSourceHandle const ds =
cs.GetCpuCallback();
if (!ds) {
TF_WARN("Could not find CPU callback data source for %s",
compId.GetText());
continue;
}
HdExtComputationCpuCallbackSharedPtr const callback =
ds->GetTypedValue(0.0f);
if (!callback) {
TF_WARN("Invalid CPU callback for %s",
compId.GetText());
continue;
}

callback->Compute(&executionContext);

// ... and add outputs to the value store.
if (executionContext.HasComputationError()) {
Expand Down

0 comments on commit ad5d719

Please sign in to comment.