Skip to content

Commit

Permalink
Merge pull request #3503 from Autodesk/gamaj/LOOKDEVX-2128/export_com…
Browse files Browse the repository at this point in the history
…mon_color_management_api

LOOKDEVX-2128 - Allow specializing the topo neutral graph generator
  • Loading branch information
seando-adsk authored Dec 11, 2023
2 parents d01378b + 7eeb0cb commit 833a5b7
Show file tree
Hide file tree
Showing 5 changed files with 253 additions and 136 deletions.
2 changes: 1 addition & 1 deletion lib/mayaUsd/render/MaterialXGenOgsXml/ShaderGenUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class MAYAUSD_CORE_PUBLIC TopoNeutralGraph
// Get the watch list gathered while traversing
const WatchList& getWatchList() const;

private:
protected:
mx::NodePtr cloneNode(const mx::Node& node, mx::GraphElement& container);
mx::OutputPtr findNodeGraphOutput(const mx::Input& input, const std::string& outputName);
std::string gatherChannels(const mx::Input& input);
Expand Down
2 changes: 2 additions & 0 deletions lib/mayaUsd/render/vp2RenderDelegate/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ target_sources(${PROJECT_NAME}
meshViewportCompute.cpp
points.cpp
proxyRenderDelegate.cpp
colorManagementPreferences.cpp
render_delegate.cpp
render_param.cpp
sampler.cpp
Expand All @@ -24,6 +25,7 @@ target_sources(${PROJECT_NAME}

set(HEADERS
proxyRenderDelegate.h
colorManagementPreferences.h
)

# -----------------------------------------------------------------------------
Expand Down
141 changes: 141 additions & 0 deletions lib/mayaUsd/render/vp2RenderDelegate/colorManagementPreferences.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
//
// 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 "colorManagementPreferences.h"

#include <maya/MEventMessage.h>
#include <maya/MGlobal.h>
#include <maya/MSceneMessage.h>

#include <set>

namespace MAYAUSD_NS_DEF {

// We will cache the color management preferences since they are used in many loops.
ColorManagementPreferences::~ColorManagementPreferences() { RemoveSinks(); }

bool ColorManagementPreferences::Active() { return Get()._active; }

const MString& ColorManagementPreferences::RenderingSpaceName()
{
return Get()._renderingSpaceName;
}

const MString& ColorManagementPreferences::sRGBName() { return Get()._sRGBName; }

std::string ColorManagementPreferences::getFileRule(const std::string& path)
{
MString colorRuleCmd;
colorRuleCmd.format("colorManagementFileRules -evaluate \"^1s\";", MString(path.c_str()));
return MGlobal::executeCommandStringResult(colorRuleCmd).asChar();
}

void ColorManagementPreferences::SetDirty()
{
auto& self = InternalGet();
self._dirty = true;
}

void ColorManagementPreferences::MayaExit()
{
auto& self = InternalGet();
self.RemoveSinks();
}

ColorManagementPreferences::ColorManagementPreferences() = default;

const ColorManagementPreferences& ColorManagementPreferences::Get()
{
auto& self = InternalGet();
self.Refresh();
return self;
}
ColorManagementPreferences& ColorManagementPreferences::InternalGet()
{
static ColorManagementPreferences _self;
return _self;
}

void ColorManagementPreferences::RemoveSinks()
{
for (auto id : _mayaColorManagementCallbackIds) {
MMessage::removeCallback(id);
}
_mayaColorManagementCallbackIds.clear();
}

void colorManagementRefreshCB(void*) { ColorManagementPreferences::SetDirty(); }

void mayaExitingCB(void*) { ColorManagementPreferences::MayaExit(); }

void ColorManagementPreferences::Refresh()
{
if (_mayaColorManagementCallbackIds.empty()) {
// Monitor color management prefs
_mayaColorManagementCallbackIds.push_back(MEventMessage::addEventCallback(
"colorMgtEnabledChanged", colorManagementRefreshCB, this));
_mayaColorManagementCallbackIds.push_back(MEventMessage::addEventCallback(
"colorMgtWorkingSpaceChanged", colorManagementRefreshCB, this));
_mayaColorManagementCallbackIds.push_back(MEventMessage::addEventCallback(
"colorMgtConfigChanged", colorManagementRefreshCB, this));
_mayaColorManagementCallbackIds.push_back(MEventMessage::addEventCallback(
"colorMgtConfigFilePathChanged", colorManagementRefreshCB, this));
// The color management settings are quietly reset on file new:
_mayaColorManagementCallbackIds.push_back(
MSceneMessage::addCallback(MSceneMessage::kBeforeNew, colorManagementRefreshCB, this));
_mayaColorManagementCallbackIds.push_back(
MSceneMessage::addCallback(MSceneMessage::kBeforeOpen, colorManagementRefreshCB, this));

// Cleanup on exit:
_mayaColorManagementCallbackIds.push_back(
MSceneMessage::addCallback(MSceneMessage::kMayaExiting, mayaExitingCB, this));
}

if (!_dirty) {
return;
}
_dirty = false;

int isActive = 0;
MGlobal::executeCommand("colorManagementPrefs -q -cmEnabled", isActive, false, false);
if (!isActive) {
_active = false;
return;
}

_active = true;

_renderingSpaceName
= MGlobal::executeCommandStringResult("colorManagementPrefs -q -renderingSpaceName");

// Need some robustness around sRGB since not all OCIO configs declare it the same way:
const auto sRGBAliases
= std::set<std::string> { "sRGB", "sRGB - Texture",
"srgb_tx", "Utility - sRGB - Texture",
"srgb_texture", "Input - Generic - sRGB - Texture" };

MStringArray allInputSpaces;
MGlobal::executeCommand(
"colorManagementPrefs -q -inputSpaceNames", allInputSpaces, false, false);

for (auto&& spaceName : allInputSpaces) {
if (sRGBAliases.count(spaceName.asChar())) {
_sRGBName = spaceName;
break;
}
}
}

} // namespace MAYAUSD_NS_DEF
84 changes: 84 additions & 0 deletions lib/mayaUsd/render/vp2RenderDelegate/colorManagementPreferences.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
//
// 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.
//
#ifndef COLOR_MANAGEMENT_PREFERENCES
#define COLOR_MANAGEMENT_PREFERENCES

#include <mayaUsd/base/api.h>

#include <maya/MMessage.h>
#include <maya/MString.h>

#include <vector>

namespace MAYAUSD_NS_DEF {

/*! \brief Cache of color management preferences and queries.
Getting the information involves calling MEL scripts, so we cache the results for better
performance.
*/
class MAYAUSD_CORE_PUBLIC ColorManagementPreferences
{
public:
~ColorManagementPreferences();

/*! \brief Is color management active.
*/
static bool Active();

/*! \brief The current DCC rendering space name.
*/
static const MString& RenderingSpaceName();

/*! \brief The current DCC color space name for plain sRGB
Color management config files can rename or alias the sRGB color space name. We try a few
common names and remember the first one that is found in the config.
*/
static const MString& sRGBName();

/*! \brief Returns the OCIO color space name according to config file rules.
\param path The path of the file to be color managed.
*/
static std::string getFileRule(const std::string& path);

/*! \brief Utility function to reset all cached data.
*/
static void SetDirty();

/*! \brief Utility function to reset all message handlers on exit.
*/
static void MayaExit();

private:
ColorManagementPreferences();
static const ColorManagementPreferences& Get();
static ColorManagementPreferences& InternalGet();

bool _dirty = true;
bool _active = false;
MString _renderingSpaceName;
MString _sRGBName;
std::vector<MCallbackId> _mayaColorManagementCallbackIds;

void Refresh();
void RemoveSinks();
};

} // namespace MAYAUSD_NS_DEF

#endif
Loading

0 comments on commit 833a5b7

Please sign in to comment.