@@ -1472,3 +1472,92 @@ IECore::ConstCompoundDataPtr ShaderNetworkAlgo::expandSplineParameters( const IE
14721472 return parametersData;
14731473 }
14741474}
1475+
1476+
1477+ // ////////////////////////////////////////////////////////////////////////
1478+ // Render Adaptors
1479+ // ////////////////////////////////////////////////////////////////////////
1480+
1481+ namespace
1482+ {
1483+
1484+ struct RenderAdaptor
1485+ {
1486+ std::string name;
1487+ IECoreScene::ShaderNetworkAlgo::RenderAdaptorHashFunction hash;
1488+ IECoreScene::ShaderNetworkAlgo::RenderAdaptorFunction apply;
1489+ };
1490+
1491+ using RenderAdaptors = std::vector<RenderAdaptor>;
1492+ RenderAdaptors &renderAdaptors ()
1493+ {
1494+ static RenderAdaptors g_renderAdaptors;
1495+ return g_renderAdaptors;
1496+ }
1497+
1498+ bool g_stringSubstitutionsRegistration = [] () {
1499+
1500+ IECoreScene::ShaderNetworkAlgo::registerRenderAdaptor (
1501+ " stringSubstitution" ,
1502+ // Hash
1503+ [] ( const IECoreScene::ShaderNetwork *shaderNetwork, InternedString attributeName, const IECore::CompoundObject *attributes, IECore::MurmurHash &hash ) {
1504+ shaderNetwork->hashSubstitutions ( attributes, hash );
1505+ },
1506+ // Apply
1507+ [] ( IECoreScene::ShaderNetwork *shaderNetwork, InternedString attributeName, const IECore::CompoundObject *attributes ) {
1508+ shaderNetwork->applySubstitutions ( attributes );
1509+ }
1510+ );
1511+
1512+ return true ;
1513+ } ();
1514+
1515+ } // namespace
1516+
1517+ void ShaderNetworkAlgo::registerRenderAdaptor ( const std::string &name, RenderAdaptorHashFunction hashFunction, RenderAdaptorFunction adaptorFunction )
1518+ {
1519+ // Replace existing adaptor if it exists.
1520+ RenderAdaptors &a = renderAdaptors ();
1521+ for ( auto &x : a )
1522+ {
1523+ if ( x.name == name )
1524+ {
1525+ x.hash = hashFunction;
1526+ x.apply = adaptorFunction;
1527+ return ;
1528+ }
1529+ }
1530+ // Otherwise add new adaptor.
1531+ a.push_back ( { name, hashFunction, adaptorFunction } );
1532+ }
1533+
1534+ void ShaderNetworkAlgo::deregisterRenderAdaptor ( const std::string &name )
1535+ {
1536+ RenderAdaptors &a = renderAdaptors ();
1537+ a.erase (
1538+ std::remove_if (
1539+ a.begin (),
1540+ a.end (),
1541+ [&] ( const RenderAdaptor &x ) {
1542+ return x.name == name;
1543+ }
1544+ ),
1545+ a.end ()
1546+ );
1547+ }
1548+
1549+ void ShaderNetworkAlgo::hashRenderAdaptors ( const IECoreScene::ShaderNetwork *shaderNetwork, InternedString attributeName, const IECore::CompoundObject *attributes, IECore::MurmurHash &hash )
1550+ {
1551+ for ( const auto &x : renderAdaptors () )
1552+ {
1553+ x.hash ( shaderNetwork, attributeName, attributes, hash );
1554+ }
1555+ }
1556+
1557+ void ShaderNetworkAlgo::applyRenderAdaptors ( IECoreScene::ShaderNetwork *shaderNetwork, InternedString attributeName, const IECore::CompoundObject *attributes )
1558+ {
1559+ for ( const auto &x : renderAdaptors () )
1560+ {
1561+ x.apply ( shaderNetwork, attributeName, attributes );
1562+ }
1563+ }
0 commit comments