Skip to content

Commit

Permalink
Revert "ArnoldRenderer : Add special support for instancer object"
Browse files Browse the repository at this point in the history
This reverts commit 2b31768.
  • Loading branch information
danieldresser-ie committed Oct 21, 2023
1 parent cb44d52 commit d3f2e02
Showing 1 changed file with 3 additions and 140 deletions.
143 changes: 3 additions & 140 deletions src/IECoreArnold/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@
#include "IECore/SimpleTypedData.h"
#include "IECore/StringAlgo.h"
#include "IECore/VectorTypedData.h"
#include "IECore/ObjectVector.h"
#include "IECore/Version.h"

#include "boost/algorithm/string.hpp"
Expand Down Expand Up @@ -328,7 +327,6 @@ const AtString g_funcPtrArnoldString( "funcptr" );
const AtString g_ginstanceArnoldString( "ginstance" );
const AtString g_ignoreMotionBlurArnoldString( "ignore_motion_blur" );
const AtString g_inputArnoldString( "input" );
const AtString g_instancerCapsuleNameArnoldString( "instancerCapsule" );
const AtString g_lightGroupArnoldString( "light_group" );
const AtString g_shadowGroupArnoldString( "shadow_group" );
const AtString g_linearArnoldString( "linear" );
Expand Down Expand Up @@ -2041,7 +2039,7 @@ class Instance

public :

AtNode *node() const
AtNode *node()
{
return m_ginstance.get() ? m_ginstance.get() : m_node.get();
}
Expand All @@ -2067,9 +2065,6 @@ class Instance
// `InstanceCache::get()`. See comment in `nodesCreated()`.
friend class InstanceCache;

// Needed for current special case where ArnoldRendererBase directly creates `instancer` nodes
friend class ArnoldRendererBase;

// Non-instanced
Instance( const SharedAtNodePtr &node )
: m_node( node )
Expand Down Expand Up @@ -2893,96 +2888,6 @@ class ArnoldObject : public ArnoldObjectBase

IE_CORE_DECLAREPTR( ArnoldLight )

const AtString g_instancerArnoldString("instancer");
const AtString g_instanceMatrixArnoldString("instance_matrix");
const AtString g_instanceMotionStartArnoldString("instance_motion_start");
const AtString g_instanceMotionEndArnoldString("instance_motion_end");
const AtString g_instanceVisibilityArnoldString("instance_visibility");
const AtString g_nodesArnoldString("nodes");
const AtString g_nodeIdxsArnoldString("node_idxs");

void setupInstancer( ArnoldRendererBase *renderer, AtNode *result,
const std::vector<unsigned int> &prototypeIndices,
const std::vector<IECore::ObjectPtr> &prototypes, const std::vector<IECore::ObjectPtr> &prototypeAttributes,
const std::vector<float> &sampleTimes, const std::vector< const std::vector< Imath::M44f > * >& transforms,
const IECore::CompoundObject::ObjectMap &instanceAttributes
)
{
std::vector<char> prototypeVisibilities( prototypes.size() );

// TODO - does this leave Arnold in an inconsistent state if a sub-Capsule throws an exception?
std::vector<AtNode *> arnoldPrototypes( prototypes.size() );
for( unsigned int i = 0; i < prototypes.size(); i++ )
{
// This is currently only used inside a ProceduralRenderer, so we don't need to worry about
// global uniqueness of names. Currently not being nearly as careful here as when
// InstancerCapsule::render() creates names for each instance however, because hopefully we don't
// have millions of prototypes.
std::string name = "prototype" + std::to_string( i );

IECore::CompoundObjectPtr attributesWithOverride = IECore::runTimeCast<IECore::CompoundObject>( prototypeAttributes[i].get() )->copy();
attributesWithOverride->members()[ g_automaticInstancingAttributeName ] = new IECore::BoolData( false );

IECoreScenePreview::Renderer::AttributesInterfacePtr attributes = renderer->attributes( attributesWithOverride.get() );

// We aren't storing the objectHandle anywhere - this would be a problem for keeping the prototypes
// alive, except that this is currently only used within a ProceduralRenderer, which takes care of
// holding onto the child objects itself.
ArnoldRendererBase::ObjectInterfacePtr objectHandle = renderer->object( name, prototypes[i].get(), attributes.get() );

arnoldPrototypes[i] = static_cast< ArnoldObjectBase *>( objectHandle.get() )->instance().node();

prototypeVisibilities[i] = AiNodeGetByte( arnoldPrototypes[i], g_visibilityArnoldString );
AiNodeSetByte( arnoldPrototypes[i], g_visibilityArnoldString, 0 );

}

AiNodeSetArray( result, g_nodesArnoldString, AiArrayConvert( prototypes.size(), 1, AI_TYPE_NODE, &arnoldPrototypes[0] ) );
if( prototypeIndices.size() )
{
AiNodeSetArray( result, g_nodeIdxsArnoldString,
AiArrayConvert( prototypeIndices.size(), 1, AI_TYPE_UINT, &prototypeIndices[0] )
);
}

AtArray *matrices = AiArrayAllocate( transforms[0]->size(), transforms.size(), AI_TYPE_MATRIX );
for( unsigned int i = 0; i < transforms.size(); i++ )
{
AiArraySetKey( matrices, i, &( (*transforms[i])[0] ) );
}

AiNodeSetArray( result, g_instanceMatrixArnoldString, matrices );

AiNodeSetArray( result, g_instanceVisibilityArnoldString, AiArrayConvert( prototypeVisibilities.size(), 1, AI_TYPE_BYTE, &prototypeVisibilities[0] ) );

for( const auto &i : instanceAttributes )
{
bool array;
int arnoldType = ParameterAlgo::parameterType( i.second->typeId(), array );
const IECore::Data *attributeData = IECore::runTimeCast< IECore::Data >( i.second.get() );

if( arnoldType == AI_TYPE_NONE || !array || !attributeData )
{
IECore::msg( IECore::Msg::Warning, "ArnoldRenderer", "Instance attributes: " + i.first.string() + " : Unsupported type " + i.second->typeName() );
continue;
}

std::string testStr = std::string("constant ARRAY ") + AiParamGetTypeName( arnoldType );

AtString name( ( "instance_" + i.first.string() ).c_str() );
AiNodeDeclare( result, name, (std::string("constant ARRAY ") + AiParamGetTypeName( arnoldType )).c_str() );
AiNodeSetArray( result, name, ParameterAlgo::dataToArray( attributeData, arnoldType ) );
}


AiNodeDeclare( result, g_instanceMotionStartArnoldString, "constant ARRAY FLOAT" );
AiNodeDeclare( result, g_instanceMotionEndArnoldString, "constant ARRAY FLOAT" );
AiNodeSetArray( result, g_instanceMotionStartArnoldString, AiArrayConvert( 1, 1, AI_TYPE_FLOAT, &sampleTimes.front() ) );
AiNodeSetArray( result, g_instanceMotionEndArnoldString, AiArrayConvert( 1, 1, AI_TYPE_FLOAT, &sampleTimes.back() ) );
}



} // namespace

//////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -4350,50 +4255,8 @@ ArnoldRendererBase::ObjectInterfacePtr ArnoldRendererBase::object( const std::st
{
const IECore::MessageHandler::Scope s( m_messageHandler.get() );

const IECore::CompoundObject *possibleInstancer = IECore::runTimeCast< const IECore::CompoundObject >( object );
const IECore::CompoundObject *instancerParameters = nullptr;
if( possibleInstancer )
{
const IECore::BoolData *flag = possibleInstancer->member<IECore::BoolData>( "isInstancer", false );
if( flag && flag->readable() )
{
instancerParameters = possibleInstancer;
}
}

ObjectInterfacePtr result;
if( instancerParameters )
{
const IECore::UIntVectorData *prototypeIndicesData = instancerParameters->member<IECore::UIntVectorData>( "prototypeIndices", true );
const IECore::ObjectVector *prototypesData = instancerParameters->member<IECore::ObjectVector>( "prototypes", true );
const IECore::ObjectVector *prototypeAttributesData = instancerParameters->member<IECore::ObjectVector>( "prototypeAttributes", true );
const IECore::FloatVectorData *sampleTimesData = instancerParameters->member<IECore::FloatVectorData>( "sampleTimes", true );
const IECore::ObjectVector *instanceMatrixData = instancerParameters->member<IECore::ObjectVector>( "instanceMatrix", true );
const IECore::CompoundObject *instanceAttributesData = instancerParameters->member<IECore::CompoundObject>( "instanceAttributes", true );

std::vector< const std::vector< Imath::M44f > * > instanceMatrix;
for( const auto &i : instanceMatrixData->members() )
{
instanceMatrix.push_back( &IECore::runTimeCast< const IECore::M44fVectorData >( i.get() )->readable() );
}

SharedAtNodePtr node = SharedAtNodePtr( AiNode(
m_universe, g_instancerArnoldString, g_instancerCapsuleNameArnoldString, m_parentNode
), m_nodeDeleter );

setupInstancer( this, node.get(),
prototypeIndicesData->readable(), prototypesData->members(), prototypeAttributesData->members(),
sampleTimesData->readable(), instanceMatrix, instanceAttributesData->members()
);

Instance instance( node );
result = new ArnoldObject( instance );
}
else
{
Instance instance = m_instanceCache->get( object, attributes, name );
result = new ArnoldObject( instance );
}
Instance instance = m_instanceCache->get( object, attributes, name );
ObjectInterfacePtr result = new ArnoldObject( instance );
result->attributes( attributes );
return result;
}
Expand Down

0 comments on commit d3f2e02

Please sign in to comment.