-
Notifications
You must be signed in to change notification settings - Fork 201
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
MAYA106075 - Introducing color cache to improve performance #2059
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -732,6 +732,7 @@ void ProxyRenderDelegate::_Execute(const MHWRender::MFrameContext& frameContext) | |
MProfilingScope profilingScope( | ||
HdVP2RenderDelegate::sProfilerCategory, MProfiler::kColorC_L1, "Execute"); | ||
|
||
++_frameCounter; | ||
_UpdateRenderTags(); | ||
|
||
// If update for selection is enabled, the draw data for the "points" repr | ||
|
@@ -1484,21 +1485,28 @@ const MColor& ProxyRenderDelegate::GetWireframeColor() const { return _wireframe | |
|
||
GfVec3f ProxyRenderDelegate::GetCurveDefaultColor() | ||
{ | ||
MDoubleArray curveColorResult; | ||
{ | ||
std::lock_guard<std::mutex> mutexGuard(_mayaCommandEngineMutex); | ||
MGlobal::executeCommand( | ||
"int $index = `displayColor -q -dormant \"curve\"`; colorIndex -q $index;", | ||
curveColorResult); | ||
// Enter the mutex and check the cache | ||
std::lock_guard<std::mutex> mutexGuard(_mayaCommandEngineMutex); | ||
if (_dormantCurveColorCache.second == _frameCounter) { | ||
return _dormantCurveColorCache.first; | ||
} | ||
|
||
if (curveColorResult.length() == 3) { | ||
return GfVec3f(curveColorResult[0], curveColorResult[1], curveColorResult[2]); | ||
// Execute Maya command engine to fetch the color | ||
MDoubleArray colorResult; | ||
MGlobal::executeCommand( | ||
"int $index = `displayColor -q -dormant \"curve\"`; colorIndex -q $index;", colorResult); | ||
|
||
if (colorResult.length() == 3) { | ||
_dormantCurveColorCache.first = GfVec3f(colorResult[0], colorResult[1], colorResult[2]); | ||
} else { | ||
TF_WARN("Failed to obtain curve default color"); | ||
// In case of an error, return the default navy-blue color | ||
return GfVec3f(0.000f, 0.016f, 0.376f); | ||
_dormantCurveColorCache.first = GfVec3f(0.000f, 0.016f, 0.376f); | ||
} | ||
|
||
// Update the cache and return | ||
_dormantCurveColorCache.second = _frameCounter; | ||
return _dormantCurveColorCache.first; | ||
} | ||
|
||
//! \brief | ||
|
@@ -1508,16 +1516,31 @@ MColor ProxyRenderDelegate::GetSelectionHighlightColor(const TfToken& className) | |
static const MColor kDefaultActiveColor(1.0f, 1.0f, 1.0f, 1.0f); | ||
|
||
// Prepare to construct the query command. | ||
bool fromPalette = true; | ||
const char* queryName = "unsupported"; | ||
bool fromPalette = true; | ||
const char* queryName = "unsupported"; | ||
MColorCache* colorCache = nullptr; | ||
if (className.IsEmpty()) { | ||
colorCache = &_leadColorCache; | ||
fromPalette = false; | ||
queryName = "lead"; | ||
} else if (className == HdPrimTypeTokens->mesh) { | ||
colorCache = &_activeMeshColorCache; | ||
fromPalette = false; | ||
queryName = "polymeshActive"; | ||
} else if (className == HdPrimTypeTokens->basisCurves) { | ||
colorCache = &_activeCurveColorCache; | ||
queryName = "curve"; | ||
} else { | ||
TF_WARN( | ||
"ProxyRenderDelegate::GetSelectionHighlightColor - unsupported class: '%s'", | ||
className.GetString().c_str()); | ||
return kDefaultActiveColor; | ||
} | ||
|
||
// Enter the mutex and check the cache | ||
std::lock_guard<std::mutex> mutexGuard(_mayaCommandEngineMutex); | ||
if (colorCache->second == _frameCounter) { | ||
return colorCache->first; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could use a similar lock-free pattern here. |
||
} | ||
|
||
// Construct the query command string. | ||
|
@@ -1533,37 +1556,38 @@ MColor ProxyRenderDelegate::GetSelectionHighlightColor(const TfToken& className) | |
} | ||
|
||
// Query and return the selection color. | ||
{ | ||
MDoubleArray colorResult; | ||
std::lock_guard<std::mutex> mutexGuard(_mayaCommandEngineMutex); | ||
MGlobal::executeCommand(queryCommand, colorResult); | ||
|
||
if (colorResult.length() == 3) { | ||
MColor color(colorResult[0], colorResult[1], colorResult[2]); | ||
|
||
if (className.IsEmpty()) { | ||
// The 'lead' color is returned in display space, so we need to convert it to | ||
// rendering space. However, function MColorPickerUtilities::applyViewTransform | ||
// is supported only starting from Maya 2023, so in opposite case we just return | ||
// the default lead color. | ||
MDoubleArray colorResult; | ||
MGlobal::executeCommand(queryCommand, colorResult); | ||
|
||
if (colorResult.length() == 3) { | ||
MColor color(colorResult[0], colorResult[1], colorResult[2]); | ||
|
||
if (className.IsEmpty()) { | ||
// The 'lead' color is returned in display space, so we need to convert it to | ||
// rendering space. However, function MColorPickerUtilities::applyViewTransform | ||
// is supported only starting from Maya 2023, so in opposite case we just return | ||
// the default lead color. | ||
#if MAYA_API_VERSION >= 20230000 | ||
return MColorPickerUtilities::applyViewTransform( | ||
color, MColorPickerUtilities::kInverse); | ||
colorCache->first | ||
= MColorPickerUtilities::applyViewTransform(color, MColorPickerUtilities::kInverse); | ||
#else | ||
return kDefaultLeadColor; | ||
colorCache->first = kDefaultLeadColor; | ||
#endif | ||
} else { | ||
return color; | ||
} | ||
} else { | ||
TF_WARN( | ||
"Failed to obtain selection highlight color for '%s' objects", | ||
className.IsEmpty() ? "lead" : className.GetString().c_str()); | ||
colorCache->first = color; | ||
} | ||
} else { | ||
TF_WARN( | ||
"Failed to obtain selection highlight color for '%s' objects", | ||
className.IsEmpty() ? "lead" : className.GetString().c_str()); | ||
|
||
// In case of any failure, return the default color | ||
colorCache->first = className.IsEmpty() ? kDefaultLeadColor : kDefaultActiveColor; | ||
} | ||
|
||
// In case of any failure, return the default color | ||
return className.IsEmpty() ? kDefaultLeadColor : kDefaultActiveColor; | ||
// Update the cache and return | ||
colorCache->second = _frameCounter; | ||
return colorCache->first; | ||
} | ||
|
||
bool ProxyRenderDelegate::DrawRenderTag(const TfToken& renderTag) const | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think if we make _dormantCurveColorCache.second a std::atomic<uint64_t> then we could move acquiring the mutex to be after this check to see if we already have already read the value this frame. That would prevent lots of mutex acquires which might be faster.
In the case where the test fails we'd have to acquire the mutex and re-test to avoid reading the value two or more times, but that is okay.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The std::atomic<uint64_t> approach may be faster but not necessarily. We can try it if we still need to after this change.