Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HYDRA-703 : Fix mesh adapter transform update #20

Merged
merged 3 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/mayaHydra/hydraExtensions/adapters/cameraAdapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ void MayaHydraCameraAdapter::CreateCallbacks()
dag,
+[](MObject& transformNode, MDagMessage::MatrixModifiedFlags& modified, void* clientData) {
auto* adapter = reinterpret_cast<MayaHydraCameraAdapter*>(clientData);
adapter->MarkDirty(HdCamera::DirtyTransform);
adapter->InvalidateTransform();
adapter->MarkDirty(HdCamera::DirtyTransform);
Comment on lines 104 to +106
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There were a couple places where we were dirtying the transform before invalidating it. I've unified all instances of this sequence of operations to first invalidate the transform and then dirty it.

},
reinterpret_cast<void*>(this),
&status);
Expand Down
8 changes: 4 additions & 4 deletions lib/mayaHydra/hydraExtensions/adapters/dagAdapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,17 +82,17 @@ void _TransformNodeDirty(MObject& node, MPlug& plug, void* clientData)
// that dirty as well...
if (adapter->IsVisible(false)) {
// Transform can change while dag path is hidden.
adapter->MarkDirty(HdChangeTracker::DirtyVisibility | HdChangeTracker::DirtyTransform);
adapter->InvalidateTransform();
adapter->MarkDirty(HdChangeTracker::DirtyVisibility | HdChangeTracker::DirtyTransform);
} else {
adapter->MarkDirty(HdChangeTracker::DirtyVisibility);
}
// We use IsVisible(checkDirty=false) because we need to make sure we
// DON'T update visibility from within this callback, since the change
// has't propagated yet
} else if (adapter->IsVisible(false)) {
adapter->MarkDirty(HdChangeTracker::DirtyTransform);
adapter->InvalidateTransform();
adapter->MarkDirty(HdChangeTracker::DirtyTransform);
}
}

Expand Down Expand Up @@ -210,9 +210,9 @@ void MayaHydraDagAdapter::CreateCallbacks()
void MayaHydraDagAdapter::MarkDirty(HdDirtyBits dirtyBits)
{
if (dirtyBits != 0) {
GetSceneProducer()->GetRenderIndex().GetChangeTracker().MarkRprimDirty(GetID(), dirtyBits);
GetSceneProducer()->MarkRprimDirty(GetID(), dirtyBits);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is where everything originates, as it is what caused (and now fixed) the issue for MeshAdapter; however, this line being the problem only highlighted a larger problem, which is that we were still using the Hydra 1.0 way of dirtying prims in some places in our Maya-native SceneIndex (and this is why this PR may be a bit larger than expected).

if (IsInstanced()) {
GetSceneProducer()->GetRenderIndex().GetChangeTracker().MarkInstancerDirty(GetInstancerID(), dirtyBits);
GetSceneProducer()->MarkInstancerDirty(GetInstancerID(), dirtyBits);
}
if (dirtyBits & HdChangeTracker::DirtyVisibility) {
_visibilityDirty = true;
Expand Down
4 changes: 2 additions & 2 deletions lib/mayaHydra/hydraExtensions/adapters/lightAdapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ void _dirtyTransform(MObject& node, void* clientData)
TF_UNUSED(node);
auto* adapter = reinterpret_cast<MayaHydraDagAdapter*>(clientData);
if (adapter->IsVisible()) {
adapter->InvalidateTransform();
adapter->MarkDirty(
HdLight::DirtyTransform | HdLight::DirtyParams | HdLight::DirtyShadowParams);
adapter->InvalidateTransform();
}
}

Expand All @@ -83,8 +83,8 @@ void _dirtyParams(MObject& node, void* clientData)
TF_UNUSED(node);
auto* adapter = reinterpret_cast<MayaHydraDagAdapter*>(clientData);
if (adapter->IsVisible()) {
adapter->MarkDirty(HdLight::DirtyParams | HdLight::DirtyShadowParams);
adapter->InvalidateTransform();
adapter->MarkDirty(HdLight::DirtyParams | HdLight::DirtyShadowParams);
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/mayaHydra/hydraExtensions/mayaHydraLibInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

PXR_NAMESPACE_OPEN_SCOPE

using SceneIndicesVector = std::vector<HdSceneIndexBasePtr>;//Be careful, these are not not Ref counted. Elements could become dangling
using SceneIndicesVector = std::vector<HdSceneIndexBaseRefPtr>;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See this comment and discussion : #4 (comment). Doing this now because I added a utility observer class along with the unit test for this issue, and that class needs its observed SceneIndex to remain valid for its whole lifetime.


/// In order to access this interface, call the function GetMayaHydraLibInterface()
class MayaHydraLibInterface
Expand Down
16 changes: 14 additions & 2 deletions lib/mayaHydra/hydraExtensions/mayaHydraSceneProducer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -315,14 +315,26 @@ void MayaHydraSceneProducer::MarkRprimDirty(const SdfPath& id, HdDirtyBits dirty
{
if (enableMayaNativeSceneIndex())
{
_sceneIndex->MarkPrimDirty(id, dirtyBits);
_sceneIndex->MarkRprimDirty(id, dirtyBits);
}
else
{
_sceneDelegate->GetRenderIndex().GetChangeTracker().MarkRprimDirty(id, dirtyBits);
}
}

void MayaHydraSceneProducer::MarkInstancerDirty(const SdfPath& id, HdDirtyBits dirtyBits)
{
if (enableMayaNativeSceneIndex())
{
_sceneIndex->MarkInstancerDirty(id, dirtyBits);
}
else
{
_sceneDelegate->GetRenderIndex().GetChangeTracker().MarkInstancerDirty(id, dirtyBits);
}
}

void MayaHydraSceneProducer::InsertSprim(
MayaHydraAdapter* adapter,
const TfToken& typeId,
Expand Down Expand Up @@ -355,7 +367,7 @@ void MayaHydraSceneProducer::MarkSprimDirty(const SdfPath& id, HdDirtyBits dirty
{
if (enableMayaNativeSceneIndex())
{
_sceneIndex->MarkPrimDirty(id, dirtyBits);
_sceneIndex->MarkSprimDirty(id, dirtyBits);
}
else
{
Expand Down
2 changes: 2 additions & 0 deletions lib/mayaHydra/hydraExtensions/mayaHydraSceneProducer.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ class MAYAHYDRALIB_API MayaHydraSceneProducer
// Mark a Rprim in hydra scene as dirty
void MarkRprimDirty(const SdfPath& id, HdDirtyBits dirtyBits);

void MarkInstancerDirty(const SdfPath& id, HdDirtyBits dirtyBits);

// Insert a Sprim to hydra scene
void InsertSprim(
MayaHydraAdapter* adapter,
Expand Down
38 changes: 26 additions & 12 deletions lib/mayaHydra/hydraExtensions/sceneIndex/mayaHydraSceneIndex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@ void MayaHydraSceneIndex::SetDefaultLight(const GlfSimpleLight& light)
_mayaDefaultLight.SetDiffuse(light.GetDiffuse());
_mayaDefaultLight.SetSpecular(light.GetSpecular());
_mayaDefaultLight.SetPosition(light.GetPosition());
MarkPrimDirty(_mayaDefaultLightPath, HdLight::DirtyParams);
MarkSprimDirty(_mayaDefaultLightPath, HdLight::DirtyParams);
}
}

Expand Down Expand Up @@ -943,18 +943,33 @@ void MayaHydraSceneIndex::_AddPrimAncestors(const SdfPath& path)

}

void MayaHydraSceneIndex::MarkPrimDirty(const SdfPath& id, HdDirtyBits dirtyBits)
void MayaHydraSceneIndex::MarkRprimDirty(const SdfPath& id, HdDirtyBits dirtyBits) {
_MarkPrimDirty(id, dirtyBits, HdDirtyBitsTranslator::RprimDirtyBitsToLocatorSet);
}

void MayaHydraSceneIndex::MarkSprimDirty(const SdfPath& id, HdDirtyBits dirtyBits)
{
_MarkPrimDirty(id, dirtyBits, HdDirtyBitsTranslator::SprimDirtyBitsToLocatorSet);
}

void MayaHydraSceneIndex::MarkBprimDirty(const SdfPath& id, HdDirtyBits dirtyBits)
{
_MarkPrimDirty(id, dirtyBits, HdDirtyBitsTranslator::BprimDirtyBitsToLocatorSet);
}

void MayaHydraSceneIndex::MarkInstancerDirty(const SdfPath& id, HdDirtyBits dirtyBits)
{
_MarkPrimDirty(id, dirtyBits, HdDirtyBitsTranslator::InstancerDirtyBitsToLocatorSet);
}

void MayaHydraSceneIndex::_MarkPrimDirty(
const SdfPath& id,
HdDirtyBits dirtyBits,
DirtyBitsToLocatorsFunc dirtyBitsToLocatorsFunc)
Comment on lines +946 to +968
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Broke up the previous MarkPrimDirty method into separate prim-category-specific methods : the reason for this is because there does not seem to be a reliable way to determine which DirtyBitsToLocatorSet method to use purely based off a prim's type. While this does look more like Hydra 1.0 in terms of API design, it also matches more closely how HdChangeTracker handles these calls for its own emulated scene index. When our different adapters all move to using Hydra 2.0 APIs, we can change this.

{
// Dispatch based on prim type.
HdSceneIndexPrim prim = GetPrim(id);
HdDataSourceLocatorSet locators;
if (HdPrimTypeIsGprim(prim.primType)) {
HdDirtyBitsTranslator::RprimDirtyBitsToLocatorSet(prim.primType, dirtyBits, &locators);
}
else {
HdDirtyBitsTranslator::SprimDirtyBitsToLocatorSet(prim.primType, dirtyBits, &locators);
}

dirtyBitsToLocatorsFunc(prim.primType, dirtyBits, &locators);
if (!locators.IsEmpty()) {
DirtyPrims({ {id, locators} });
}
Expand Down Expand Up @@ -1254,11 +1269,10 @@ void MayaHydraSceneIndex::RecreateAdapter(const SdfPath& id, const MObject& obj)
},
_materialAdapters)) {
auto& renderIndex = GetRenderIndex();
auto& changeTracker = renderIndex.GetChangeTracker();
for (const auto& rprimId : renderIndex.GetRprimIds()) {
const auto* rprim = renderIndex.GetRprim(rprimId);
if (rprim != nullptr && rprim->GetMaterialId() == id) {
changeTracker.MarkRprimDirty(rprimId, HdChangeTracker::DirtyMaterialId);
MarkRprimDirty(rprimId, HdChangeTracker::DirtyMaterialId);
}
}
if (MObjectHandle(obj).isValid()) {
Expand Down
15 changes: 11 additions & 4 deletions lib/mayaHydra/hydraExtensions/sceneIndex/mayaHydraSceneIndex.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
#include <pxr/imaging/hd/rendererPlugin.h>
#include <pxr/imaging/hdx/taskController.h>
#include <pxr/imaging/hd/retainedSceneIndex.h>
#include "pxr/imaging/hd/dirtyBitsTranslator.h"

#include <unordered_map>

Expand Down Expand Up @@ -106,10 +107,10 @@ class MAYAHYDRALIB_API MayaHydraSceneIndex : public HdRetainedSceneIndex, public
// Remove a primitive from hydra scene
void RemovePrim(const SdfPath& id);

// Mark a primitive in hydra scene as dirty
void MarkPrimDirty(
const SdfPath& id,
HdDirtyBits dirtyBits);
void MarkRprimDirty(const SdfPath& id, HdDirtyBits dirtyBits);
void MarkSprimDirty(const SdfPath& id, HdDirtyBits dirtyBits);
void MarkBprimDirty(const SdfPath& id, HdDirtyBits dirtyBits);
void MarkInstancerDirty(const SdfPath& id, HdDirtyBits dirtyBits);

// Operation that's performed on rendering a frame
void PreFrame(const MHWRender::MDrawContext& drawContext);
Expand Down Expand Up @@ -202,6 +203,12 @@ class MAYAHYDRALIB_API MayaHydraSceneIndex : public HdRetainedSceneIndex, public
using LightDagPathMap = std::unordered_map<std::string, MDagPath>;
LightDagPathMap _GetActiveLightPaths() const;
static VtValue CreateMayaDefaultMaterial();

using DirtyBitsToLocatorsFunc = std::function<void(TfToken const&, const HdDirtyBits, HdDataSourceLocatorSet*)>;
void _MarkPrimDirty(
const SdfPath& id,
HdDirtyBits dirtyBits,
DirtyBitsToLocatorsFunc dirtyBitsToLocatorsFunc);
private:
// ------------------------------------------------------------------------
// HdSceneIndexBase implementations
Expand Down
3 changes: 1 addition & 2 deletions lib/mayaHydra/mayaPlugin/renderOverride.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -643,8 +643,7 @@ MStatus MtohRenderOverride::Render(
if (_mayaHydraSceneProducer) {
params.camera = _mayaHydraSceneProducer->SetCameraViewport(camPath, _viewport);
if (vpDirty)
_mayaHydraSceneProducer->GetRenderIndex().GetChangeTracker().MarkSprimDirty(
params.camera, HdCamera::DirtyParams);
_mayaHydraSceneProducer->MarkSprimDirty(params.camera, HdCamera::DirtyParams);
}
}
} else {
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 @@ -30,6 +30,7 @@ set(TEST_SCRIPT_FILES
# both versions of the test in parallel will conflict.
set(TEST_SCRIPT_FILES_MESH_ADAPTER
testMeshes.py
cpp/testMeshAdapterTransform.py
)

foreach(script ${TEST_SCRIPT_FILES})
Expand Down
1 change: 1 addition & 0 deletions test/lib/mayaUsd/render/mayaToHydra/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ target_sources(${TARGET_NAME}
testFvpViewportInformationMultipleViewports.cpp
testFvpViewportInformationRendererSwitching.cpp
testUsdStageLayerMuting.cpp
testMeshAdapterTransform.cpp
)

# -----------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright 2023 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 "testUtils.h"

#include <pxr/imaging/hd/tokens.h>
#include <pxr/imaging/hd/xformSchema.h>

#include <maya/MGlobal.h>

#include <gtest/gtest.h>

PXR_NAMESPACE_USING_DIRECTIVE

namespace {
const std::string kCubeName = "testCube";
} // namespace

TEST(MeshAdapterTransform, testDirtying)
{
// Setup notifications accumulator for the first terminal scene index
const SceneIndicesVector& sceneIndices = GetTerminalSceneIndices();
ASSERT_GT(sceneIndices.size(), static_cast<size_t>(0));
SceneIndexNotificationsAccumulator notifsAccumulator(sceneIndices.front());

// The test cube should still be selected from the Python driver
MGlobal::executeCommand("move 3 5 8");

// Check if the cube mesh prim had its xform dirtied
bool cubeXformWasDirtied = false;
for (const auto& dirtiedPrimEntry : notifsAccumulator.GetDirtiedPrimEntries()) {
HdSceneIndexPrim prim
= notifsAccumulator.GetObservedSceneIndex()->GetPrim(dirtiedPrimEntry.primPath);

cubeXformWasDirtied = dirtiedPrimEntry.primPath.GetName() == kCubeName + "Shape"
&& prim.primType == HdPrimTypeTokens->mesh
&& dirtiedPrimEntry.dirtyLocators.Contains(HdXformSchema::GetDefaultLocator());

if (cubeXformWasDirtied) {
break;
}
}
EXPECT_TRUE(cubeXformWasDirtied);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Copyright 2023 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.
#
import maya.cmds as cmds
import fixturesUtils
import mtohUtils
from testUtils import PluginLoaded

class TestMeshAdapterTransform(mtohUtils.MayaHydraBaseTestCase):
# MayaHydraBaseTestCase.setUpClass requirement.
_file = __file__

def setupScene(self):
self.setHdStormRenderer()
cmds.polyCube(name="testCube")
cmds.refresh() # Refresh to create the MeshAdapter and its mesh prim at the origin

def test_Dirtying(self):
self.setupScene()
with PluginLoaded('mayaHydraCppTests'):
cmds.mayaHydraCppTest(f="MeshAdapterTransform.testDirtying")

if __name__ == '__main__':
fixturesUtils.runTests(globals())
Loading