Skip to content

Commit

Permalink
HYDRA-312 : Add Scene Index for Texture Toggling (#131)
Browse files Browse the repository at this point in the history
* Add Scene Index to support texture togglging

* Add unit test

* style changes

* renamed variable
  • Loading branch information
roopavr-adsk authored May 28, 2024
1 parent c0d05d8 commit 514fed5
Show file tree
Hide file tree
Showing 12 changed files with 635 additions and 2 deletions.
2 changes: 2 additions & 0 deletions lib/flowViewport/sceneIndex/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ target_sources(${TARGET_NAME}
fvpSelectionSceneIndex.cpp
fvpWireframeSelectionHighlightSceneIndex.cpp
fvpDisplayStyleOverrideSceneIndex.cpp
fvpPruneTexturesSceneIndex.cpp
fvpBBoxSceneIndex.cpp
fvpReprSelectorSceneIndex.cpp
fvpBlockPrimRemovalPropagationSceneIndex.cpp
Expand All @@ -26,6 +27,7 @@ set(HEADERS
fvpSelectionSceneIndex.h
fvpWireframeSelectionHighlightSceneIndex.h
fvpDisplayStyleOverrideSceneIndex.h
fvpPruneTexturesSceneIndex.h
fvpBBoxSceneIndex.h
fvpReprSelectorSceneIndex.h
fvpBlockPrimRemovalPropagationSceneIndex.h
Expand Down
104 changes: 104 additions & 0 deletions lib/flowViewport/sceneIndex/fvpPruneTexturesSceneIndex.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// 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/fvpPruneTexturesSceneIndex.h"

#include <pxr/base/tf/staticTokens.h>
#include <pxr/imaging/hd/sceneIndexPrimView.h>
#include <pxr/imaging/hd/materialSchema.h>

#include <iostream>
namespace FVP_NS_DEF {

PXR_NAMESPACE_USING_DIRECTIVE

TF_DEFINE_PRIVATE_TOKENS(
_tokens,
(UsdPreviewSurface)
(ND_standard_surface_surfaceshader)
);

namespace {

void
_PruneTexturesFromMatNetwork(
HdMaterialNetworkInterface *networkInterface)
{
if (!networkInterface) {
return;
}
const TfTokenVector nodeNames = networkInterface->GetNodeNames();
for (TfToken const &nodeName : nodeNames) {
const TfToken nodeType = networkInterface->GetNodeType(nodeName);
if (nodeType == _tokens->ND_standard_surface_surfaceshader ||
nodeType == _tokens->UsdPreviewSurface) {
// Look for incoming connection(textures) to surface shader params
TfTokenVector inputConnections = networkInterface->GetNodeInputConnectionNames(nodeName);
for (TfToken const &connection : inputConnections) {
// Trivially remove all input connections to match Maya VP2 behavior
networkInterface->DeleteNodeInputConnection(nodeName, connection);
}
}
}
}

} // Anonymous namespace

// static
PruneTexturesSceneIndexRefPtr
PruneTexturesSceneIndex::New(
const HdSceneIndexBaseRefPtr &inputSceneIndex)
{
return TfCreateRefPtr(
new PruneTexturesSceneIndex(inputSceneIndex));
}

void
PruneTexturesSceneIndex::MarkTexturesDirty(bool isTextured)
{
_needsTexturesPruned = isTextured;
const HdDataSourceLocatorSet locators(
HdMaterialSchema::GetDefaultLocator()
.Append(HdMaterialSchemaTokens->material));

_DirtyAllPrims(locators);
}

PruneTexturesSceneIndex::PruneTexturesSceneIndex(
HdSceneIndexBaseRefPtr const &inputSceneIndex)
: HdMaterialFilteringSceneIndexBase(inputSceneIndex)
{
}

PruneTexturesSceneIndex::FilteringFnc
PruneTexturesSceneIndex::_GetFilteringFunction() const
{
return !_needsTexturesPruned ?
_PruneTexturesFromMatNetwork :
[](HdMaterialNetworkInterface*){};
}

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

} //end of namespace FVP_NS_DEF
56 changes: 56 additions & 0 deletions lib/flowViewport/sceneIndex/fvpPruneTexturesSceneIndex.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// 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_PRUNE_TEXTURE_SCENE_INDEX_NEW_H
#define FVP_PRUNE_TEXTURE_SCENE_INDEX_NEW_H

#include "flowViewport/api.h"
#include "flowViewport/sceneIndex/fvpSceneIndexUtils.h"

#include <pxr/imaging/hdsi/api.h>
#include <pxr/imaging/hd/materialFilteringSceneIndexBase.h>
#include <pxr/imaging/hd/materialNetworkInterface.h>

namespace FVP_NS_DEF {

class PruneTexturesSceneIndex;
typedef PXR_NS::TfRefPtr<PruneTexturesSceneIndex> PruneTexturesSceneIndexRefPtr;
typedef PXR_NS::TfRefPtr<const PruneTexturesSceneIndex> PruneTexturesSceneIndexConstRefPtr;

class PruneTexturesSceneIndex :
public PXR_NS::HdMaterialFilteringSceneIndexBase
{
public:
FVP_API
static PruneTexturesSceneIndexRefPtr New(
const PXR_NS::HdSceneIndexBaseRefPtr &inputScene);

FVP_API
void MarkTexturesDirty(bool isTextured);

bool _needsTexturesPruned = false;

void _DirtyAllPrims(const PXR_NS::HdDataSourceLocatorSet locators);

protected:
FilteringFnc _GetFilteringFunction() const override;

private:
PruneTexturesSceneIndex(
PXR_NS::HdSceneIndexBaseRefPtr const &inputSceneIndex);
};

} //end of namespace FVP_NS_DEF

#endif //FVP_PRUNE_TEXTURE_SCENE_INDEX_H
18 changes: 16 additions & 2 deletions lib/mayaHydra/mayaPlugin/renderOverride.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1088,6 +1088,15 @@ MStatus MtohRenderOverride::Render(
_displayStyleSceneIndex->SetRefineLevel({true, delegateParams.refineLevel});
}

// Toggle textures in the material network
const unsigned int currentDisplayMode = drawContext.getDisplayStyle();
bool isTextured = currentDisplayMode & MHWRender::MFrameContext::kTextured;
if (_pruneTexturesSceneIndex &&
_currentlyTextured != isTextured) {
_pruneTexturesSceneIndex->MarkTexturesDirty(isTextured);
_currentlyTextured = isTextured;
}

HdxRenderTaskParams params;
params.enableLighting = true;
params.enableSceneMaterials = true;
Expand Down Expand Up @@ -1389,6 +1398,8 @@ void MtohRenderOverride::ClearHydraResources(bool fullReset)
#endif

_displayStyleSceneIndex = nullptr;
_pruneTexturesSceneIndex = nullptr;
_currentlyTextured = false;
_selectionSceneIndex.Reset();
_selection.reset();
_wireframeColorInterfaceImp.reset();
Expand Down Expand Up @@ -1436,6 +1447,10 @@ void MtohRenderOverride::_CreateSceneIndicesChainAfterMergingSceneIndex(const MH
Fvp::DisplayStyleOverrideSceneIndex::New(_inputSceneIndexOfFilteringSceneIndicesChain);
_displayStyleSceneIndex->addExcludedSceneRoot(MAYA_NATIVE_ROOT); // Maya native prims don't use global refinement

// Add texture disabling Scene Index
_lastFilteringSceneIndexBeforeCustomFiltering = _pruneTexturesSceneIndex =
Fvp::PruneTexturesSceneIndex::New(_lastFilteringSceneIndexBeforeCustomFiltering);

const unsigned int currentDisplayStyle = drawContext.getDisplayStyle();
const MFrameContext::WireOnShadedMode wireOnShadedMode = MFrameContext::wireOnShadedMode();//Get the user preference

Expand Down Expand Up @@ -1941,8 +1956,7 @@ bool MtohRenderOverride::_NeedToRecreateTheSceneIndicesChain(unsigned int curren
{
if (areDifferentForOneOfTheseBits(currentDisplayStyle, _oldDisplayStyle,
MHWRender::MFrameContext::kGouraudShaded |
MHWRender::MFrameContext::kTextured |
MHWRender::MFrameContext::kWireFrame |
MHWRender::MFrameContext::kWireFrame |
MHWRender::MFrameContext::kBoundingBox )
){
return true;
Expand Down
3 changes: 3 additions & 0 deletions lib/mayaHydra/mayaPlugin/renderOverride.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#include <flowViewport/selection/fvpSelectionTracker.h>
#include <flowViewport/selection/fvpSelectionFwd.h>
#include <flowViewport/sceneIndex/fvpDisplayStyleOverrideSceneIndex.h>
#include <flowViewport/sceneIndex/fvpPruneTexturesSceneIndex.h>
#include <flowViewport/sceneIndex/fvpBlockPrimRemovalPropagationSceneIndex.h>

#include <pxr/base/tf/singleton.h>
Expand Down Expand Up @@ -247,6 +248,7 @@ class MtohRenderOverride : public MHWRender::MRenderOverride
HdSceneIndexBaseRefPtr _lastFilteringSceneIndexBeforeCustomFiltering {nullptr};
HdSceneIndexBaseRefPtr _inputSceneIndexOfFilteringSceneIndicesChain {nullptr};
Fvp::DisplayStyleOverrideSceneIndexRefPtr _displayStyleSceneIndex;
Fvp::PruneTexturesSceneIndexRefPtr _pruneTexturesSceneIndex;
HdRenderIndex* _renderIndex = nullptr;
Fvp::SelectionTrackerSharedPtr _fvpSelectionTracker;
Fvp::SelectionSceneIndexRefPtr _selectionSceneIndex;
Expand Down Expand Up @@ -295,6 +297,7 @@ class MtohRenderOverride : public MHWRender::MRenderOverride
bool _initializationAttempted = false;
bool _initializationSucceeded = false;
bool _hasDefaultLighting = false;
bool _currentlyTextured = false;
unsigned int _oldDisplayStyle {0};
bool _useDefaultMaterial;
bool _xRayEnabled;
Expand Down
1 change: 1 addition & 0 deletions test/lib/mayaUsd/render/mayaToHydra/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ set(INTERACTIVE_TEST_SCRIPT_FILES
testFootPrintNode.py
testBackgroundColor.py
testGrid.py
testUsdTextureToggle.py
# To be reenabled after investigation
#testDataProducerSelHighlight.py
testPassingNormalsOnMayaNative.py
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
48 changes: 48 additions & 0 deletions test/lib/mayaUsd/render/mayaToHydra/testUsdTextureToggle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#
# Copyright 2024 Autodesk, Inc. All rights reserved.
#
# 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.
#

import maya.cmds as cmds
import fixturesUtils
import mtohUtils
import mayaUtils

import platform

class TestUsdTextureToggle(mtohUtils.MayaHydraBaseTestCase): #Subclassing mtohUtils.MayaHydraBaseTestCase to be able to call self.assertSnapshotClose
# MayaHydraBaseTestCase.setUpClass requirement.
_file = __file__

IMAGE_DIFF_FAIL_THRESHOLD = 0.2
IMAGE_DIFF_FAIL_PERCENT = 0.55

def test_UsdTextureToggle(self):

# open simple Maya scene
testFile = mayaUtils.openTestScene(
"testUsdTextureToggle",
"testUsdTextureToggle.ma", useTestSettings=False)
cmds.refresh()

panel = mayaUtils.activeModelPanel()
self.assertSnapshotClose("usd_texture_on" + ".png", self.IMAGE_DIFF_FAIL_THRESHOLD, self.IMAGE_DIFF_FAIL_PERCENT)

cmds.refresh()
cmds.modelEditor(panel, edit=True, displayTextures=False)
self.assertSnapshotClose("usd_texture_off" + ".png", self.IMAGE_DIFF_FAIL_THRESHOLD, self.IMAGE_DIFF_FAIL_PERCENT)


if __name__ == '__main__':
fixturesUtils.runTests(globals())
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 514fed5

Please sign in to comment.