Skip to content

Commit 07c6b59

Browse files
committed
Added IECoreGL::ToGLStateConverter.
This converts a CompoundObject representing a whole block of attributes directly into an IECoreGL::State object. Its current intended use is in Gaffer, where it will allow us to implement a faster Gaffer->GLScene coupling. This does duplicate some functionality from IECoreGL::Renderer, but we're justifying it by the Gaffer refactor being an important step towards a GL scene graph without an awkward Renderer interface in the way.
1 parent 633b706 commit 07c6b59

File tree

8 files changed

+506
-0
lines changed

8 files changed

+506
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
//////////////////////////////////////////////////////////////////////////
2+
//
3+
// Copyright (c) 2014, Image Engine Design Inc. 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 copyright
10+
// notice, this list of conditions and the following disclaimer.
11+
//
12+
// * Redistributions in binary form must reproduce the above copyright
13+
// notice, this list of conditions and the following disclaimer in the
14+
// documentation and/or other materials provided with the distribution.
15+
//
16+
// * Neither the name of Image Engine Design nor the names of any
17+
// other contributors to this software may be used to endorse or
18+
// promote products derived from this software without specific prior
19+
// written permission.
20+
//
21+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
22+
// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23+
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24+
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
25+
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26+
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27+
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28+
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29+
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30+
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31+
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32+
//
33+
//////////////////////////////////////////////////////////////////////////
34+
35+
#ifndef IECOREGL_TOGLSTATECONVERTER_H
36+
#define IECOREGL_TOGLSTATECONVERTER_H
37+
38+
#include "IECoreGL/ToGLConverter.h"
39+
40+
namespace IECore
41+
{
42+
43+
IE_CORE_FORWARDDECLARE( CompoundObject )
44+
IE_CORE_FORWARDDECLARE( Data )
45+
46+
} // namespace IECore
47+
48+
namespace IECoreGL
49+
{
50+
51+
IE_CORE_FORWARDDECLARE( State )
52+
IE_CORE_FORWARDDECLARE( StateComponent )
53+
54+
/// Converts IECore::CompoundObject objects containing shaders and attributes
55+
/// into IECoreGL::State objects.
56+
/// \ingroup conversionGroup
57+
class ToGLStateConverter : public ToGLConverter
58+
{
59+
60+
public :
61+
62+
typedef IECore::CompoundObject InputType;
63+
typedef IECoreGL::State ResultType;
64+
65+
IE_CORE_DECLARERUNTIMETYPEDEXTENSION( IECoreGL::ToGLStateConverter, ToGLStateConverterTypeId, ToGLConverter );
66+
67+
ToGLStateConverter( IECore::ConstCompoundObjectPtr toConvert = NULL );
68+
virtual ~ToGLStateConverter();
69+
70+
protected :
71+
72+
virtual IECore::RunTimeTypedPtr doConversion( IECore::ConstObjectPtr src, IECore::ConstCompoundObjectPtr operands ) const;
73+
74+
private :
75+
76+
static ConverterDescription<ToGLStateConverter> g_description;
77+
78+
};
79+
80+
IE_CORE_DECLAREPTR( ToGLStateConverter );
81+
82+
} // namespace IECoreGL
83+
84+
#endif // IECOREGL_TOGLSTATECONVERTER_H

include/IECoreGL/TypeIds.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ enum TypeId
121121
ToGLBufferConverterTypeId = 105078,
122122
UIntTextureTypeId = 105079,
123123
PrimitiveSelectableTypeId = 105080,
124+
ToGLStateConverterTypeId = 105081,
124125
LastCoreGLTypeId = 105999,
125126
};
126127

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//////////////////////////////////////////////////////////////////////////
2+
//
3+
// Copyright (c) 2014, Image Engine Design Inc. 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 copyright
10+
// notice, this list of conditions and the following disclaimer.
11+
//
12+
// * Redistributions in binary form must reproduce the above copyright
13+
// notice, this list of conditions and the following disclaimer in the
14+
// documentation and/or other materials provided with the distribution.
15+
//
16+
// * Neither the name of Image Engine Design nor the names of any
17+
// other contributors to this software may be used to endorse or
18+
// promote products derived from this software without specific prior
19+
// written permission.
20+
//
21+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
22+
// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23+
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24+
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
25+
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26+
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27+
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28+
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29+
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30+
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31+
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32+
//
33+
//////////////////////////////////////////////////////////////////////////
34+
35+
#ifndef IECOREGL_TOGLSTATECONVERTERBINDING_H
36+
#define IECOREGL_TOGLSTATECONVERTERBINDING_H
37+
38+
namespace IECoreGL
39+
{
40+
41+
void bindToGLStateConverter();
42+
43+
} // namespace IECoreGL
44+
45+
#endif // IECOREGL_TOGLSTATECONVERTERBINDING_H
Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
//////////////////////////////////////////////////////////////////////////
2+
//
3+
// Copyright (c) 2014, Image Engine Design Inc. 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 copyright
10+
// notice, this list of conditions and the following disclaimer.
11+
//
12+
// * Redistributions in binary form must reproduce the above copyright
13+
// notice, this list of conditions and the following disclaimer in the
14+
// documentation and/or other materials provided with the distribution.
15+
//
16+
// * Neither the name of Image Engine Design nor the names of any
17+
// other contributors to this software may be used to endorse or
18+
// promote products derived from this software without specific prior
19+
// written permission.
20+
//
21+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
22+
// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23+
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24+
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
25+
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26+
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27+
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28+
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29+
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30+
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31+
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32+
//
33+
//////////////////////////////////////////////////////////////////////////
34+
35+
#include "IECore/CompoundObject.h"
36+
#include "IECore/SimpleTypedData.h"
37+
#include "IECore/Shader.h"
38+
#include "IECore/ObjectVector.h"
39+
40+
#include "IECoreGL/ToGLStateConverter.h"
41+
#include "IECoreGL/Primitive.h"
42+
#include "IECoreGL/State.h"
43+
#include "IECoreGL/PointsPrimitive.h"
44+
#include "IECoreGL/CurvesPrimitive.h"
45+
#include "IECoreGL/ShaderStateComponent.h"
46+
#include "IECoreGL/ShaderLoader.h"
47+
#include "IECoreGL/TextureLoader.h"
48+
49+
using namespace IECore;
50+
using namespace IECoreGL;
51+
52+
//////////////////////////////////////////////////////////////////////////
53+
// Individual state converters
54+
//////////////////////////////////////////////////////////////////////////
55+
56+
namespace
57+
{
58+
59+
template<class T>
60+
StateComponentPtr attributeToTypedState( const IECore::Object *attribute )
61+
{
62+
typedef IECore::TypedData<typename T::ValueType> DataType;
63+
const DataType *d = runTimeCast<const DataType>( attribute );
64+
if( !d )
65+
{
66+
throw Exception( boost::str( boost::format( "Expected data of type \"%s\"" ) % DataType::staticTypeName() ) );
67+
}
68+
69+
return new T( d->readable() );
70+
}
71+
72+
StateComponentPtr attributeToUseGLPointsState( const IECore::Object *attribute )
73+
{
74+
const StringData *d = runTimeCast<const StringData>( attribute );
75+
if( !d )
76+
{
77+
throw Exception( "Expected data of type StringData" );
78+
}
79+
80+
GLPointsUsage u;
81+
const std::string &v = d->readable();
82+
if( v=="forGLPoints" )
83+
{
84+
u = ForPointsOnly;
85+
}
86+
else if( v=="forParticlesAndDisks" )
87+
{
88+
u = ForPointsAndDisks;
89+
}
90+
else if( v=="forAll" )
91+
{
92+
u = ForAll;
93+
}
94+
else
95+
{
96+
throw IECore::Exception( boost::str( boost::format( "Unsupported value \"%s\"." ) % v ) );
97+
}
98+
99+
return new PointsPrimitive::UseGLPoints( u );
100+
}
101+
102+
StateComponentPtr attributeToShaderState( const IECore::Object *attribute )
103+
{
104+
const IECore::Shader *shader = runTimeCast<const IECore::Shader>( attribute );
105+
if( !shader )
106+
{
107+
const ObjectVector *o = runTimeCast<const ObjectVector>( attribute );
108+
if( o && o->members().size() )
109+
{
110+
shader = runTimeCast<IECore::Shader>( o->members()[0].get() );
111+
}
112+
}
113+
if( !shader )
114+
{
115+
throw Exception( "Expected a Shader" );
116+
}
117+
118+
const StringData *vertexSourceData = shader->parametersData()->member<StringData>( "gl:vertexSource" );
119+
const StringData *geometrySourceData = shader->parametersData()->member<StringData>( "gl:geometrySource" );
120+
const StringData *fragmentSourceData = shader->parametersData()->member<StringData>( "gl:fragmentSource" );
121+
122+
std::string vertexSource = vertexSourceData ? vertexSourceData->readable() : "";
123+
std::string geometrySource = geometrySourceData ? geometrySourceData->readable() : "";
124+
std::string fragmentSource = fragmentSourceData ? fragmentSourceData->readable() : "";
125+
126+
if( vertexSource == "" && geometrySource == "" && fragmentSource == "" )
127+
{
128+
ShaderLoader::defaultShaderLoader()->loadSource( shader->getName(), vertexSource, geometrySource, fragmentSource );
129+
}
130+
131+
CompoundObjectPtr parametersData = new CompoundObject;
132+
for( CompoundDataMap::const_iterator it=shader->parameters().begin(); it!=shader->parameters().end(); it++ )
133+
{
134+
if( it->first!="gl:fragmentSource" && it->first!="gl:geometrySource" && it->first!="gl:vertexSource" )
135+
{
136+
parametersData->members()[it->first] = it->second;
137+
}
138+
}
139+
140+
return new ShaderStateComponent( ShaderLoader::defaultShaderLoader(), TextureLoader::defaultTextureLoader(), vertexSource, geometrySource, fragmentSource, parametersData );
141+
}
142+
143+
typedef StateComponentPtr (*AttributeToState)( const IECore::Object *attribute );
144+
typedef std::map<IECore::InternedString, AttributeToState> AttributeToStateMap;
145+
146+
const AttributeToStateMap &attributeToStateMap()
147+
{
148+
static AttributeToStateMap m;
149+
if( !m.size() )
150+
{
151+
m["gl:primitive:wireframe"] = attributeToTypedState<IECoreGL::Primitive::DrawWireframe>;
152+
m["gl:primitive:wireframeWidth"] = attributeToTypedState<IECoreGL::Primitive::WireframeWidth>;
153+
m["gl:primitive:bound"] = attributeToTypedState<IECoreGL::Primitive::DrawBound>;
154+
m["gl:primitive:solid"] = attributeToTypedState<IECoreGL::Primitive::DrawSolid>;
155+
m["gl:primitive:outline"] = attributeToTypedState<IECoreGL::Primitive::DrawOutline>;
156+
m["gl:primitive:outlineWidth"] = attributeToTypedState<IECoreGL::Primitive::OutlineWidth>;
157+
m["gl:primitive:points"] = attributeToTypedState<IECoreGL::Primitive::DrawPoints>;
158+
m["gl:primitive:pointWidth"] = attributeToTypedState<IECoreGL::Primitive::PointWidth>;
159+
m["gl:primitive:wireframeColor"] = attributeToTypedState<WireframeColorStateComponent>;
160+
m["gl:primitive:boundColor"] = attributeToTypedState<BoundColorStateComponent>;
161+
m["gl:primitive:outlineColor"] = attributeToTypedState<OutlineColorStateComponent>;
162+
m["gl:primitive:pointColor"] = attributeToTypedState<PointColorStateComponent>;
163+
m["gl:pointsPrimitive:useGLPoints"] = attributeToUseGLPointsState;
164+
m["gl:pointsPrimitive:glPointWidth"] = attributeToTypedState<IECoreGL::PointsPrimitive::GLPointWidth>;
165+
m["doubleSided"] = attributeToTypedState<DoubleSidedStateComponent>;
166+
m["gl:curvesPrimitive:useGLLines"] = attributeToTypedState<IECoreGL::CurvesPrimitive::UseGLLines>;
167+
m["gl:curvesPrimitive:glLineWidth"] = attributeToTypedState<IECoreGL::CurvesPrimitive::GLLineWidth>;
168+
m["gl:curvesPrimitive:ignoreBasis"] = attributeToTypedState<IECoreGL::CurvesPrimitive::IgnoreBasis>;
169+
m["gl:smoothing:points"] = attributeToTypedState<PointSmoothingStateComponent>;
170+
m["gl:smoothing:lines"] = attributeToTypedState<LineSmoothingStateComponent>;
171+
m["gl:smoothing:polygons"] = attributeToTypedState<PolygonSmoothingStateComponent>;
172+
m["gl:surface"] = attributeToShaderState;
173+
}
174+
return m;
175+
}
176+
177+
} // namespace
178+
179+
//////////////////////////////////////////////////////////////////////////
180+
// ToGLStateConverter implementation
181+
//////////////////////////////////////////////////////////////////////////
182+
183+
IE_CORE_DEFINERUNTIMETYPED( ToGLStateConverter );
184+
185+
ToGLConverter::ConverterDescription<ToGLStateConverter> ToGLStateConverter::g_description;
186+
187+
ToGLStateConverter::ToGLStateConverter( IECore::ConstCompoundObjectPtr toConvert )
188+
: ToGLConverter( "Converts IECore::CompoundObject objects to IECoreGL::State objects.", IECore::CompoundObjectTypeId )
189+
{
190+
srcParameter()->setValue( boost::const_pointer_cast<IECore::CompoundObject>( toConvert ) );
191+
}
192+
193+
ToGLStateConverter::~ToGLStateConverter()
194+
{
195+
}
196+
197+
IECore::RunTimeTypedPtr ToGLStateConverter::doConversion( IECore::ConstObjectPtr src, IECore::ConstCompoundObjectPtr operands ) const
198+
{
199+
const CompoundObject *co = runTimeCast<const CompoundObject>( src.get() );
200+
if( !co )
201+
{
202+
throw Exception( "Expected a CompoundObject" );
203+
}
204+
205+
const AttributeToStateMap &m = attributeToStateMap();
206+
207+
const StatePtr result = new State( false );
208+
for( CompoundObject::ObjectMap::const_iterator it = co->members().begin(), eIt = co->members().end(); it != eIt; ++it )
209+
{
210+
AttributeToStateMap::const_iterator mIt = m.find( it->first );
211+
if( mIt != m.end() )
212+
{
213+
StateComponentPtr s = mIt->second( it->second.get() );
214+
result->add( s );
215+
}
216+
}
217+
return result;
218+
}

src/IECoreGL/bindings/IECoreGLBinding.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
#include "IECoreGL/bindings/SplineToGLTextureConverterBinding.h"
7777
#include "IECoreGL/bindings/ShaderStateComponentBinding.h"
7878
#include "IECoreGL/bindings/CurvesPrimitiveBinding.h"
79+
#include "IECoreGL/bindings/ToGLStateConverterBinding.h"
7980

8081
using namespace IECoreGL;
8182
using namespace boost::python;
@@ -119,6 +120,7 @@ BOOST_PYTHON_MODULE( _IECoreGL )
119120
bindSplineToGLTextureConverter();
120121
bindShaderStateComponent();
121122
bindCurvesPrimitive();
123+
bindToGLStateConverter();
122124

123125
#ifdef IECORE_WITH_FREETYPE
124126

0 commit comments

Comments
 (0)