|
| 1 | +////////////////////////////////////////////////////////////////////////// |
| 2 | +// |
| 3 | +// Copyright (c) 2026, Cinesite VFX Ltd. All rights reserved. |
| 4 | +// |
| 5 | +// Redistribution and use in source and binary forms, with or without |
| 6 | +// modification, are permitted provided that the following conditions are |
| 7 | +// met: |
| 8 | +// |
| 9 | +// * Redistributions of source code must retain the above |
| 10 | +// copyright notice, this list of conditions and the following |
| 11 | +// disclaimer. |
| 12 | +// |
| 13 | +// * Redistributions in binary form must reproduce the above |
| 14 | +// copyright notice, this list of conditions and the following |
| 15 | +// disclaimer in the documentation and/or other materials provided with |
| 16 | +// the distribution. |
| 17 | +// |
| 18 | +// * Neither the name of John Haddon nor the names of |
| 19 | +// any other contributors to this software may be used to endorse or |
| 20 | +// promote products derived from this software without specific prior |
| 21 | +// written permission. |
| 22 | +// |
| 23 | +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
| 24 | +// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 25 | +// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 26 | +// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 27 | +// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 28 | +// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 29 | +// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 30 | +// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 31 | +// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 32 | +// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 33 | +// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 34 | +// |
| 35 | +////////////////////////////////////////////////////////////////////////// |
| 36 | + |
| 37 | +#include "GafferSceneTest/GlobalsSanitiser.h" |
| 38 | + |
| 39 | +#include "GafferScene/ScenePlug.h" |
| 40 | + |
| 41 | +#include "Gaffer/Process.h" |
| 42 | +#include "Gaffer/ScriptNode.h" |
| 43 | + |
| 44 | +#include "IECore/MessageHandler.h" |
| 45 | + |
| 46 | +#include "fmt/format.h" |
| 47 | + |
| 48 | +using namespace IECore; |
| 49 | +using namespace Gaffer; |
| 50 | +using namespace GafferScene; |
| 51 | +using namespace GafferSceneTest; |
| 52 | + |
| 53 | +GlobalsSanitiser::GlobalsSanitiser() |
| 54 | +{ |
| 55 | +} |
| 56 | + |
| 57 | +void GlobalsSanitiser::processStarted( const Gaffer::Process *process ) |
| 58 | +{ |
| 59 | + const CompoundObjectPlug *dependentGlobals = nullptr; |
| 60 | + auto it = m_dependentGlobalsMap.find( process->parent() ); |
| 61 | + if( it != m_dependentGlobalsMap.end() ) |
| 62 | + { |
| 63 | + // If some globals were dependent on our parent process, then |
| 64 | + // they are dependent on us too. |
| 65 | + dependentGlobals = it->second; |
| 66 | + } |
| 67 | + |
| 68 | + if( const ScenePlug *scene = process->plug()->parent<ScenePlug>() ) |
| 69 | + { |
| 70 | + if( process->plug() == scene->globalsPlug() ) |
| 71 | + { |
| 72 | + dependentGlobals = scene->globalsPlug(); |
| 73 | + } |
| 74 | + else if( dependentGlobals ) |
| 75 | + { |
| 76 | + warn( *process, dependentGlobals ); |
| 77 | + // No point issuing further warnings for upstream processes. |
| 78 | + dependentGlobals = nullptr; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + if( dependentGlobals ) |
| 83 | + { |
| 84 | + m_dependentGlobalsMap[process] = dependentGlobals; |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +void GlobalsSanitiser::processFinished( const Gaffer::Process *process ) |
| 89 | +{ |
| 90 | + auto it = m_dependentGlobalsMap.find( process ); |
| 91 | + if( it != m_dependentGlobalsMap.end() ) |
| 92 | + { |
| 93 | + // We can't erase in a thread-safe manner, so just store null. |
| 94 | + // This does mean we accumulate entries, but we only intend to |
| 95 | + // use the sanitiser for short bursts anyway. |
| 96 | + it->second = nullptr; |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +void GlobalsSanitiser::warn( const Gaffer::Process &process, const Gaffer::CompoundObjectPlug *dependentGlobals ) |
| 101 | +{ |
| 102 | + const Warning warning( process.plug(), dependentGlobals ); |
| 103 | + if( !m_warningsEmitted.insert( warning ).second ) |
| 104 | + { |
| 105 | + return; |
| 106 | + } |
| 107 | + |
| 108 | + IECore::msg( |
| 109 | + IECore::Msg::Warning, "GlobalsSanitiser", |
| 110 | + fmt::format( |
| 111 | + "Globals {} depends on {}", |
| 112 | + dependentGlobals->relativeName( dependentGlobals->ancestor<ScriptNode>() ), |
| 113 | + process.plug()->relativeName( process.plug()->ancestor<ScriptNode>() ) |
| 114 | + ) |
| 115 | + ); |
| 116 | +} |
0 commit comments