@@ -1394,3 +1394,92 @@ IECore::ConstCompoundDataPtr ShaderNetworkAlgo::expandSplineParameters( const IE
13941394 return parametersData;
13951395 }
13961396}
1397+
1398+
1399+ // ////////////////////////////////////////////////////////////////////////
1400+ // Render Adaptors
1401+ // ////////////////////////////////////////////////////////////////////////
1402+
1403+ namespace
1404+ {
1405+
1406+ struct RenderAdaptor
1407+ {
1408+ std::string name;
1409+ IECoreScene::ShaderNetworkAlgo::RenderAdaptorHashFunction hash;
1410+ IECoreScene::ShaderNetworkAlgo::RenderAdaptorFunction apply;
1411+ };
1412+
1413+ using RenderAdaptors = std::vector<RenderAdaptor>;
1414+ RenderAdaptors &renderAdaptors ()
1415+ {
1416+ static RenderAdaptors g_renderAdaptors;
1417+ return g_renderAdaptors;
1418+ }
1419+
1420+ bool g_stringSubstitutionsRegistration = [] () {
1421+
1422+ IECoreScene::ShaderNetworkAlgo::registerRenderAdaptor (
1423+ " stringSubstitution" ,
1424+ // Hash
1425+ [] ( const IECoreScene::ShaderNetwork *shaderNetwork, InternedString attributeName, const IECore::CompoundObject *attributes, IECore::MurmurHash &hash ) {
1426+ shaderNetwork->hashSubstitutions ( attributes, hash );
1427+ },
1428+ // Apply
1429+ [] ( IECoreScene::ShaderNetwork *shaderNetwork, InternedString attributeName, const IECore::CompoundObject *attributes ) {
1430+ shaderNetwork->applySubstitutions ( attributes );
1431+ }
1432+ );
1433+
1434+ return true ;
1435+ } ();
1436+
1437+ } // namespace
1438+
1439+ void ShaderNetworkAlgo::registerRenderAdaptor ( const std::string &name, RenderAdaptorHashFunction hashFunction, RenderAdaptorFunction adaptorFunction )
1440+ {
1441+ // Replace existing adaptor if it exists.
1442+ RenderAdaptors &a = renderAdaptors ();
1443+ for ( auto &x : a )
1444+ {
1445+ if ( x.name == name )
1446+ {
1447+ x.hash = hashFunction;
1448+ x.apply = adaptorFunction;
1449+ return ;
1450+ }
1451+ }
1452+ // Otherwise add new adaptor.
1453+ a.push_back ( { name, hashFunction, adaptorFunction } );
1454+ }
1455+
1456+ void ShaderNetworkAlgo::deregisterRenderAdaptor ( const std::string &name )
1457+ {
1458+ RenderAdaptors &a = renderAdaptors ();
1459+ a.erase (
1460+ std::remove_if (
1461+ a.begin (),
1462+ a.end (),
1463+ [&] ( const RenderAdaptor &x ) {
1464+ return x.name == name;
1465+ }
1466+ ),
1467+ a.end ()
1468+ );
1469+ }
1470+
1471+ void ShaderNetworkAlgo::hashRenderAdaptors ( const IECoreScene::ShaderNetwork *shaderNetwork, InternedString attributeName, const IECore::CompoundObject *attributes, IECore::MurmurHash &hash )
1472+ {
1473+ for ( const auto &x : renderAdaptors () )
1474+ {
1475+ x.hash ( shaderNetwork, attributeName, attributes, hash );
1476+ }
1477+ }
1478+
1479+ void ShaderNetworkAlgo::applyRenderAdaptors ( IECoreScene::ShaderNetwork *shaderNetwork, InternedString attributeName, const IECore::CompoundObject *attributes )
1480+ {
1481+ for ( const auto &x : renderAdaptors () )
1482+ {
1483+ x.apply ( shaderNetwork, attributeName, attributes );
1484+ }
1485+ }
0 commit comments