Skip to content

Commit

Permalink
HYDRA-927 - Expose complexity settings in globalRenderSettings (#106)
Browse files Browse the repository at this point in the history
* Implement global refinement level for USD primitives

* Automated test for the feature

* - Implement Fvp::DisplayStyleOverrideSceneIndex and use it to set global refine level
- Maya native prims are excluded from gloabl refine level
- Also fixed a bug with WireframeSelectionHighlightSceneIndex where the function addExcludedSceneRoot was called with a wrong path

* Minor improvements requested by code review

* Renaming mtohRefinementLevel to mayaHydraRefinementLevel
  • Loading branch information
vlasovi authored Mar 26, 2024
1 parent 9490586 commit 456be15
Show file tree
Hide file tree
Showing 13 changed files with 472 additions and 6 deletions.
2 changes: 2 additions & 0 deletions lib/flowViewport/sceneIndex/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ target_sources(${TARGET_NAME}
fvpSceneIndexUtils.cpp
fvpSelectionSceneIndex.cpp
fvpWireframeSelectionHighlightSceneIndex.cpp
fvpDisplayStyleOverrideSceneIndex.cpp
fvpBBoxSceneIndex.cpp
)

Expand All @@ -22,6 +23,7 @@ set(HEADERS
fvpSceneIndexUtils.h
fvpSelectionSceneIndex.h
fvpWireframeSelectionHighlightSceneIndex.h
fvpDisplayStyleOverrideSceneIndex.h
fvpBBoxSceneIndex.h
)

Expand Down
230 changes: 230 additions & 0 deletions lib/flowViewport/sceneIndex/fvpDisplayStyleOverrideSceneIndex.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
// Copyright 2024 Autodesk
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

#include "flowViewport/sceneIndex/fvpDisplayStyleOverrideSceneIndex.h"

#include "pxr/imaging/hd/tokens.h"
#include "pxr/imaging/hd/legacyDisplayStyleSchema.h"
#include "pxr/imaging/hd/overlayContainerDataSource.h"
#include "pxr/imaging/hd/sceneIndexPrimView.h"
#include "pxr/imaging/hd/retainedDataSource.h"

namespace FVP_NS_DEF {

PXR_NAMESPACE_USING_DIRECTIVE

namespace DisplayStyleSceneIndex_Impl
{

using OptionalInt = DisplayStyleOverrideSceneIndex::OptionalInt;

struct _StyleInfo
{
OptionalInt refineLevel;
/// Retained data source storing refineLevel (or null ptr if empty optional
/// value) to avoid allocating a data source for every prim.
HdDataSourceBaseHandle refineLevelDs;
};

/// Data source for locator displayStyle.
class _DisplayStyleDataSource : public HdContainerDataSource
{
public:
HD_DECLARE_DATASOURCE(_DisplayStyleDataSource);

HdDataSourceBaseHandle Get(const TfToken &name) override
{
if (name == HdLegacyDisplayStyleSchemaTokens->refineLevel) {
return _styleInfo->refineLevelDs;
}
return nullptr;
}

TfTokenVector GetNames() override
{
static const TfTokenVector names = {
HdLegacyDisplayStyleSchemaTokens->refineLevel
};

return names;
}

private:
_DisplayStyleDataSource(_StyleInfoSharedPtr const &styleInfo)
: _styleInfo(styleInfo)
{
}

_StyleInfoSharedPtr _styleInfo;
};

} // namespace DisplayStyleSceneIndex_Impl

using namespace DisplayStyleSceneIndex_Impl;

DisplayStyleOverrideSceneIndexRefPtr
DisplayStyleOverrideSceneIndex::New(
const HdSceneIndexBaseRefPtr &inputSceneIndex)
{
return TfCreateRefPtr(
new DisplayStyleOverrideSceneIndex(
inputSceneIndex));
}

DisplayStyleOverrideSceneIndex::
DisplayStyleOverrideSceneIndex(
const HdSceneIndexBaseRefPtr &inputSceneIndex)
: HdSingleInputFilteringSceneIndexBase(inputSceneIndex)
, _styleInfo(std::make_shared<_StyleInfo>())
, _overlayDs(
HdRetainedContainerDataSource::New(
HdLegacyDisplayStyleSchemaTokens->displayStyle,
_DisplayStyleDataSource::New(_styleInfo)))
{
}

HdSceneIndexPrim
DisplayStyleOverrideSceneIndex::GetPrim(
const SdfPath &primPath) const
{
HdSceneIndexPrim prim = _GetInputSceneIndex()->GetPrim(primPath);
if (prim.dataSource) {
if (!isExcluded(primPath) && prim.primType == HdPrimTypeTokens->mesh) {
prim.dataSource =
HdOverlayContainerDataSource::New(
_overlayDs, prim.dataSource);
}
}
return prim;
}

SdfPathVector
DisplayStyleOverrideSceneIndex::GetChildPrimPaths(
const SdfPath &primPath) const
{
return _GetInputSceneIndex()->GetChildPrimPaths(primPath);
}

void
DisplayStyleOverrideSceneIndex::SetRefineLevel(
const OptionalInt &refineLevel)
{
if (refineLevel == _styleInfo->refineLevel) {
return;
}

_styleInfo->refineLevel = refineLevel;
_styleInfo->refineLevelDs =
refineLevel
? HdRetainedTypedSampledDataSource<int>::New(*refineLevel)
: nullptr;

static const HdDataSourceLocatorSet locators(
HdLegacyDisplayStyleSchema::GetDefaultLocator()
.Append(HdLegacyDisplayStyleSchemaTokens->refineLevel));

_DirtyAllPrims(locators);
}

void
DisplayStyleOverrideSceneIndex::_DirtyAllPrims(
const HdDataSourceLocatorSet &locators)
{
if (!_IsObserved()) {
return;
}

HdSceneIndexObserver::DirtiedPrimEntries entries;
for (const SdfPath &path : HdSceneIndexPrimView(_GetInputSceneIndex())) {
entries.push_back({path, locators});
}

_SendPrimsDirtied(entries);
}

void
DisplayStyleOverrideSceneIndex::_PrimsAdded(
const HdSceneIndexBase &sender,
const HdSceneIndexObserver::AddedPrimEntries &entries)
{
if (!_IsObserved()) {
return;
}

_SendPrimsAdded(entries);
}

void
DisplayStyleOverrideSceneIndex::_PrimsRemoved(
const HdSceneIndexBase &sender,
const HdSceneIndexObserver::RemovedPrimEntries &entries)
{
if (!_IsObserved()) {
return;
}

_SendPrimsRemoved(entries);
}

void
DisplayStyleOverrideSceneIndex::_PrimsDirtied(
const HdSceneIndexBase &sender,
const HdSceneIndexObserver::DirtiedPrimEntries &entries)
{
if (!_IsObserved()) {
return;
}

_SendPrimsDirtied(entries);
}

void DisplayStyleOverrideSceneIndex::addExcludedSceneRoot(
const PXR_NS::SdfPath& sceneRoot
)
{
_excludedSceneRoots.emplace(sceneRoot);
}

bool DisplayStyleOverrideSceneIndex::isExcluded(
const PXR_NS::SdfPath& sceneRoot
) const
{
for (const auto& excluded : _excludedSceneRoots) {
if (sceneRoot.HasPrefix(excluded)) {
return true;
}
}
return false;
}

bool operator==(
const DisplayStyleOverrideSceneIndex::OptionalInt &a,
const DisplayStyleOverrideSceneIndex::OptionalInt &b)
{
if (a.hasValue == false && b.hasValue == false) {
return true;
}

return a.hasValue == b.hasValue && a.value == b.value;
}

bool operator!=(
const DisplayStyleOverrideSceneIndex::OptionalInt &a,
const DisplayStyleOverrideSceneIndex::OptionalInt &b)
{
return !(a == b);
}

} //end of namespace FVP_NS_DEF
124 changes: 124 additions & 0 deletions lib/flowViewport/sceneIndex/fvpDisplayStyleOverrideSceneIndex.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
// Copyright 2024 Autodesk
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#ifndef FVP_DISPLAY_STYLE_OVERRIDE_SCENE_INDEX_H
#define FVP_DISPLAY_STYLE_OVERRIDE_SCENE_INDEX_H

#include "flowViewport/api.h"

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

#include <set>

namespace FVP_NS_DEF {

namespace DisplayStyleSceneIndex_Impl
{
struct _StyleInfo;
using _StyleInfoSharedPtr = std::shared_ptr<_StyleInfo>;
}

class DisplayStyleOverrideSceneIndex;
typedef PXR_NS::TfRefPtr<DisplayStyleOverrideSceneIndex> DisplayStyleOverrideSceneIndexRefPtr;
typedef PXR_NS::TfRefPtr<const DisplayStyleOverrideSceneIndex> DisplayStyleOverrideSceneIndexConstRefPtr;

///
/// \class DisplayStyleOverrideSceneIndex
///
/// A scene index overriding the display style for each prim.
///
class DisplayStyleOverrideSceneIndex :
public PXR_NS::HdSingleInputFilteringSceneIndexBase
{
public:
FVP_API
static DisplayStyleOverrideSceneIndexRefPtr
New(const PXR_NS::HdSceneIndexBaseRefPtr &inputSceneIndex);

FVP_API
PXR_NS::HdSceneIndexPrim GetPrim(const PXR_NS::SdfPath &primPath) const override;

FVP_API
PXR_NS::SdfPathVector GetChildPrimPaths(const PXR_NS::SdfPath &primPath) const override;

/// A replacement for std::optional<int> that is not available until C++17.
struct OptionalInt
{
bool hasValue = false;
int value = 0;

operator bool() const { return hasValue; }
int operator*() const { return value; }
};

/// Sets the refine level (at data source locator displayStyle:refineLevel)
/// for every prim in the input scene inedx.
///
/// If an empty optional value is provided, a null data source will be
/// returned for the data source locator.
///
FVP_API
void SetRefineLevel(const OptionalInt &refineLevel);

FVP_API
void addExcludedSceneRoot(const PXR_NS::SdfPath& sceneRoot);

protected:
FVP_API
DisplayStyleOverrideSceneIndex(
const PXR_NS::HdSceneIndexBaseRefPtr &inputSceneIndex);

FVP_API
void _PrimsAdded(
const PXR_NS::HdSceneIndexBase &sender,
const PXR_NS::HdSceneIndexObserver::AddedPrimEntries &entries) override;

FVP_API
void _PrimsRemoved(
const PXR_NS::HdSceneIndexBase &sender,
const PXR_NS::HdSceneIndexObserver::RemovedPrimEntries &entries) override;

FVP_API
void _PrimsDirtied(
const PXR_NS::HdSceneIndexBase &sender,
const PXR_NS::HdSceneIndexObserver::DirtiedPrimEntries &entries) override;

private:
void _DirtyAllPrims(const PXR_NS::HdDataSourceLocatorSet &locators);

bool isExcluded(const PXR_NS::SdfPath& sceneRoot) const;

std::set<PXR_NS::SdfPath> _excludedSceneRoots;

DisplayStyleSceneIndex_Impl::
_StyleInfoSharedPtr const _styleInfo;

/// Prim overlay data source.
PXR_NS::HdContainerDataSourceHandle const _overlayDs;
};

HDSI_API
bool operator==(
const DisplayStyleOverrideSceneIndex::OptionalInt &a,
const DisplayStyleOverrideSceneIndex::OptionalInt &b);

HDSI_API
bool operator!=(
const DisplayStyleOverrideSceneIndex::OptionalInt &a,
const DisplayStyleOverrideSceneIndex::OptionalInt &b);

} //end of namespace FVP_NS_DEF

#endif //FVP_DISPLAY_STYLE_OVERRIDE_SCENE_INDEX_H
1 change: 1 addition & 0 deletions lib/mayaHydra/hydraExtensions/mayaHydraParams.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ struct MayaHydraParams
float motionSampleStart = 0;
float motionSampleEnd = 0;
bool displaySmoothMeshes = true;
int refineLevel = 0;

bool motionSamplesEnabled() const { return motionSampleStart != 0 || motionSampleEnd != 0; }
};
Expand Down
Loading

0 comments on commit 456be15

Please sign in to comment.