Skip to content

Commit

Permalink
WIP : Handle auto-proxying of plugs without input connections
Browse files Browse the repository at this point in the history
  • Loading branch information
danieldresser-ie committed May 8, 2024
1 parent 8a23aa6 commit 10e8643
Showing 1 changed file with 91 additions and 0 deletions.
91 changes: 91 additions & 0 deletions src/GafferScene/ShaderTweaks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@

#include "IECore/SimpleTypedData.h"
#include "IECore/StringAlgo.h"
#include "IECore/TypeTraits.h"
#include "IECore/DataAlgo.h"

#include "fmt/format.h"

Expand Down Expand Up @@ -78,6 +80,77 @@ std::pair<const GafferScene::Shader *, const Gaffer::Plug *> shaderOutput( const
return { nullptr, nullptr };
}

DataPtr castDataToType( const Data* source, const Data *target )
{
DataPtr result;
if( source->typeId() == target->typeId() )
{
result = source->copy();
}

dispatch( target,
[source, &result]( const auto *targetTyped )
{
using TargetType = typename std::remove_const_t<std::remove_pointer_t<decltype( targetTyped )> >;
if constexpr( TypeTraits::IsSimpleTypedData<TargetType>::value )
{
using TargetValueType = typename TargetType::ValueType;
if constexpr( std::is_arithmetic_v< TargetValueType > )
{
dispatch( source,
[&result]( const auto *sourceTyped )
{
using SourceType = typename std::remove_const_t<std::remove_pointer_t<decltype( sourceTyped )> >;
if constexpr( TypeTraits::IsNumericSimpleTypedData<SourceType>::value )
{
result = new TargetType( sourceTyped->readable() );
}
}
);
return;
}

if constexpr( TypeTraits::IsVec3<TargetValueType>::value || TypeTraits::IsColor<TargetValueType>::value )
{
dispatch( source,
[&result]( const auto *sourceTyped )
{
using SourceType = typename std::remove_const_t<std::remove_pointer_t<decltype( sourceTyped )> >;
if constexpr( TypeTraits::IsSimpleTypedData<SourceType>::value )
{
using SourceValueType = typename SourceType::ValueType;
if constexpr(
TypeTraits::IsVec3TypedData<SourceValueType>::value ||
TypeTraits::IsColor<SourceValueType>::value
)
{
typename TargetType::ValueType r;
r[0] = sourceTyped->readable()[0];
r[1] = sourceTyped->readable()[1];
r[2] = sourceTyped->readable()[2];
result = new TargetType( r );
}
}
}
);
return;
}
}

}
);

if( !result )
{
throw IECore::Exception( fmt::format(
"Cannot connect auto proxy from \"{}\" tweak to shader input of type \"{}\"",
source->typeName(), target->typeName()
) );
}

return result;
}

} // namespace

GAFFER_NODE_DEFINE_TYPE( ShaderTweaks );
Expand Down Expand Up @@ -373,6 +446,24 @@ bool ShaderTweaks::applyTweaks( IECoreScene::ShaderNetwork *shaderNetwork, Tweak
{
shaderNetwork->addConnection( { originalInput, dest } );
}
else
{
const IECoreScene::Shader *proxyConnectedShader = shaderNetwork->getShader( dest.shader );
if( !proxyConnectedShader )
{
throw IECore::Exception( fmt::format( "ShaderTweakProxy connected to non-existent shader \"{}\"", dest.shader.string() ) );
}

// Regular tweak
auto modifiedShader = modifiedShaders.insert( { dest.shader, nullptr } );
if( modifiedShader.second )
{
modifiedShader.first->second = proxyConnectedShader->copy();
}

const IECore::Data *origDestParameter = modifiedShader.first->second->parameters().at(dest.name).get();
modifiedShader.first->second->parameters()[dest.name] = castDataToType( shader->parameters().at(parameter.name).get(), origDestParameter );
}
}
else
{
Expand Down

0 comments on commit 10e8643

Please sign in to comment.