<s> import os <EOL> import IECore <EOL> import Gaffer <EOL> import GafferOSL <EOL> import GafferOSLTest <EOL> class OSLRendererTest ( GafferOSLTest . OSLTestCase ) : <EOL> def rectanglePoints ( self , bound = IECore . Box2f ( IECore . V2f ( - <NUM_LIT:1> ) , IECore . V2f ( <NUM_LIT:1> ) ) , divisions = IECore . V2i ( <NUM_LIT:10> ) ) : <EOL> r = IECore . Rand48 ( ) <EOL> pData = IECore . V3fVectorData ( ) <EOL> uData = IECore . FloatVectorData ( ) <EOL> vData = IECore . FloatVectorData ( ) <EOL> floatUserData = IECore . FloatVectorData ( ) <EOL> colorUserData = IECore . Color3fVectorData ( ) <EOL> for y in range ( <NUM_LIT:0> , divisions . y ) : <EOL> for x in range ( <NUM_LIT:0> , divisions . x ) : <EOL> u = float ( x ) / float ( divisions . x - <NUM_LIT:1> ) <EOL> v = float ( y ) / float ( divisions . y - <NUM_LIT:1> ) <EOL> pData . append ( IECore . V3f ( <EOL> bound . min . x + u * bound . size ( ) . x , <EOL> bound . min . y + v * bound . size ( ) . y , <EOL> <NUM_LIT:0> <EOL> ) ) <EOL> uData . append ( u ) <EOL> vData . append ( v ) <EOL> floatUserData . append ( r . nextf ( <NUM_LIT:0> , <NUM_LIT:1> ) ) <EOL> colorUserData . append ( r . nextColor3f ( ) ) <EOL> return IECore . CompoundData ( { <EOL> "<STR_LIT:P>" : pData , <EOL> "<STR_LIT:u>" : uData , <EOL> "<STR_LIT:v>" : vData , <EOL> "<STR_LIT>" : floatUserData , <EOL> "<STR_LIT>" : colorUserData , <EOL> } ) <EOL> def test ( self ) : <EOL> s = self . compileShader ( os . path . dirname ( __file__ ) + "<STR_LIT>" ) <EOL> rp = self . rectanglePoints ( ) <EOL> r = GafferOSL . OSLRenderer ( ) <EOL> with IECore . WorldBlock ( r ) : <EOL> r . shader ( "<STR_LIT>" , s , { "<STR_LIT>" : IECore . Color3f ( <NUM_LIT:1> , <NUM_LIT:0.5> , <NUM_LIT> ) } ) <EOL> e = r . shadingEngine ( ) <EOL> p = e . shade ( rp ) <EOL> self . assertEqual ( p [ "<STR_LIT>" ] , IECore . Color3fVectorData ( [ IECore . Color3f ( <NUM_LIT:1> , <NUM_LIT:0.5> , <NUM_LIT> ) ] * <NUM_LIT:100> ) ) <EOL> def testNetwork ( self ) : <EOL> constant = self . compileShader ( os . path . dirname ( __file__ ) + "<STR_LIT>" ) <EOL> input = self . compileShader ( os . path . dirname ( __file__ ) + "<STR_LIT>" ) <EOL> rp = self . rectanglePoints ( ) <EOL> r = GafferOSL . OSLRenderer ( ) <EOL> with IECore . WorldBlock ( r ) : <EOL> r . shader ( "<STR_LIT>" , input , { "<STR_LIT:input>" : <NUM_LIT:0.5> , "<STR_LIT>" : "<STR_LIT:h>" } ) <EOL> r . shader ( "<STR_LIT>" , constant , { "<STR_LIT>" : "<STR_LIT>" } ) <EOL> e = r . shadingEngine ( ) <EOL> p = e . shade ( rp ) <EOL> self . assertEqual ( p [ "<STR_LIT>" ] , IECore . Color3fVectorData ( [ IECore . Color3f ( <NUM_LIT:0.5> ) ] * <NUM_LIT:100> ) ) <EOL> def testGlobals ( self ) : <EOL> shader = self . compileShader ( os . path . dirname ( __file__ ) + "<STR_LIT>" ) <EOL> rp = self . rectanglePoints ( ) <EOL> r = GafferOSL . OSLRenderer ( ) <EOL> with IECore . WorldBlock ( r ) : <EOL> for n in ( "<STR_LIT:P>" , "<STR_LIT:u>" , "<STR_LIT:v>" ) : <EOL> r . shader ( "<STR_LIT>" , shader , { "<STR_LIT>" : n } ) <EOL> p = r . shadingEngine ( ) . shade ( rp ) <EOL> v1 = p [ "<STR_LIT>" ] <EOL> v2 = rp [ n ] <EOL> for i in range ( <NUM_LIT:0> , len ( v1 ) ) : <EOL> self . assertEqual ( v1 [ i ] , IECore . Color3f ( v2 [ i ] ) ) <EOL> def testUserDataViaGetAttribute ( self ) : <EOL> shader = self . compileShader ( os . path . dirname ( __file__ ) + "<STR_LIT>" ) <EOL> rp = self . rectanglePoints ( ) <EOL> r = GafferOSL . OSLRenderer ( ) <EOL> with IECore . WorldBlock ( r ) : <EOL> r . shader ( "<STR_LIT>" , shader , { "<STR_LIT:name>" : "<STR_LIT>" } ) <EOL> p = r . shadingEngine ( ) . shade ( rp ) <EOL> for i , c in enumerate ( p [ "<STR_LIT>" ] ) : <EOL> self . assertEqual ( c . r , rp [ "<STR_LIT>" ] [ i ] ) <EOL> r . shader ( "<STR_LIT>" , shader , { "<STR_LIT:name>" : "<STR_LIT>" } ) <EOL> p = r . shadingEngine ( ) . shade ( rp ) <EOL> for i , c in enumerate ( p [ "<STR_LIT>" ] ) : <EOL> self . assertEqual ( c , rp [ "<STR_LIT>" ] [ i ] ) <EOL> def testStructs ( self ) : <EOL> shader = self . compileShader ( os . path . dirname ( __file__ ) + "<STR_LIT>" ) <EOL> constant = self . compileShader ( os . path . dirname ( __file__ ) + "<STR_LIT>" ) <EOL> rp = self . rectanglePoints ( ) <EOL> r = GafferOSL . OSLRenderer ( ) <EOL> with IECore . WorldBlock ( r ) : <EOL> r . shader ( "<STR_LIT>" , shader , { "<STR_LIT>" : IECore . Color3f ( <NUM_LIT:0.1> , <NUM_LIT> , <NUM_LIT> ) , "<STR_LIT>" : "<STR_LIT:h>" } ) <EOL> r . shader ( "<STR_LIT>" , constant , { "<STR_LIT>" : "<STR_LIT>" } ) <EOL> p = r . shadingEngine ( ) . shade ( rp ) <EOL> for c in p [ "<STR_LIT>" ] : <EOL> self . assertEqual ( c , IECore . Color3f ( <NUM_LIT:0.1> , <NUM_LIT> , <NUM_LIT> ) ) <EOL> def testClosureParameters ( self ) : <EOL> outputClosure = self . compileShader ( os . path . dirname ( __file__ ) + "<STR_LIT>" ) <EOL> inputClosure = self . compileShader ( os . path . dirname ( __file__ ) + "<STR_LIT>" ) <EOL> r = GafferOSL . OSLRenderer ( ) <EOL> with IECore . WorldBlock ( r ) : <EOL> r . shader ( "<STR_LIT>" , outputClosure , { "<STR_LIT:e>" : IECore . Color3f ( <NUM_LIT:0.1> , <NUM_LIT> , <NUM_LIT> ) , "<STR_LIT>" : "<STR_LIT:h>" } ) <EOL> r . shader ( "<STR_LIT>" , inputClosure , { "<STR_LIT:i>" : "<STR_LIT>" } ) <EOL> p = r . shadingEngine ( ) . shade ( self . rectanglePoints ( ) ) <EOL> for c in p [ "<STR_LIT>" ] : <EOL> self . assertEqual ( c , IECore . Color3f ( <NUM_LIT:0.1> , <NUM_LIT> , <NUM_LIT> ) ) <EOL> def testDebugClosure ( self ) : <EOL> shader = self . compileShader ( os . path . dirname ( __file__ ) + "<STR_LIT>" ) <EOL> r = GafferOSL . OSLRenderer ( ) <EOL> with IECore . WorldBlock ( r ) : <EOL> r . shader ( "<STR_LIT>" , shader , { "<STR_LIT:name>" : "<STR_LIT:a>" , "<STR_LIT>" : IECore . Color3f ( <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> ) } ) <EOL> points = self . rectanglePoints ( ) <EOL> shading = r . shadingEngine ( ) . shade ( self . rectanglePoints ( ) ) <EOL> self . assertTrue ( "<STR_LIT>" in shading ) <EOL> self . assertTrue ( "<STR_LIT:a>" in shading ) <EOL> self . assertEqual ( len ( shading [ "<STR_LIT:a>" ] ) , len ( points [ "<STR_LIT:P>" ] ) ) <EOL> for c in shading [ "<STR_LIT>" ] : <EOL> self . assertEqual ( c , IECore . Color3f ( <NUM_LIT:0> ) ) <EOL> for a in shading [ "<STR_LIT:a>" ] : <EOL> self . assertEqual ( a , IECore . Color3f ( <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> ) ) <EOL> def testMultipleDebugClosures ( self ) : <EOL> shader = self . compileShader ( os . path . dirname ( __file__ ) + "<STR_LIT>" ) <EOL> r = GafferOSL . OSLRenderer ( ) <EOL> with IECore . WorldBlock ( r ) : <EOL> r . shader ( "<STR_LIT>" , shader , { } ) <EOL> points = self . rectanglePoints ( ) <EOL> shading = r . shadingEngine ( ) . shade ( self . rectanglePoints ( ) ) <EOL> for n in ( "<STR_LIT:u>" , "<STR_LIT:v>" , "<STR_LIT:P>" ) : <EOL> for i in range ( <NUM_LIT:0> , len ( shading [ n ] ) ) : <EOL> self . assertEqual ( shading [ n ] [ i ] , IECore . Color3f ( points [ n ] [ i ] ) ) <EOL> def testTypedDebugClosure ( self ) : <EOL> shader = self . compileShader ( os . path . dirname ( __file__ ) + "<STR_LIT>" ) <EOL> r = GafferOSL . OSLRenderer ( ) <EOL> with IECore . WorldBlock ( r ) : <EOL> r . shader ( "<STR_LIT>" , shader , { } ) <EOL> points = self . rectanglePoints ( ) <EOL> shading = r . shadingEngine ( ) . shade ( self . rectanglePoints ( ) ) <EOL> self . assertTrue ( isinstance ( shading [ "<STR_LIT:f>" ] , IECore . FloatVectorData ) ) <EOL> self . assertTrue ( isinstance ( shading [ "<STR_LIT:p>" ] , IECore . V3fVectorData ) ) <EOL> self . assertTrue ( isinstance ( shading [ "<STR_LIT:v>" ] , IECore . V3fVectorData ) ) <EOL> self . assertTrue ( isinstance ( shading [ "<STR_LIT:n>" ] , IECore . V3fVectorData ) ) <EOL> self . assertTrue ( isinstance ( shading [ "<STR_LIT:c>" ] , IECore . Color3fVectorData ) ) <EOL> self . assertEqual ( shading [ "<STR_LIT:p>" ] . getInterpretation ( ) , IECore . GeometricData . Interpretation . Point ) <EOL> self . assertEqual ( shading [ "<STR_LIT:v>" ] . getInterpretation ( ) , IECore . GeometricData . Interpretation . Vector ) <EOL> self . assertEqual ( shading [ "<STR_LIT:n>" ] . getInterpretation ( ) , IECore . GeometricData . Interpretation . Normal ) <EOL> for i in range ( <NUM_LIT:0> , len ( points [ "<STR_LIT:P>" ] ) ) : <EOL> self . assertEqual ( shading [ "<STR_LIT:f>" ] [ i ] , points [ "<STR_LIT:u>" ] [ i ] ) <EOL> for n in ( "<STR_LIT:p>" , "<STR_LIT:v>" , "<STR_LIT:n>" , "<STR_LIT:c>" ) : <EOL> for i in range ( <NUM_LIT:0> , len ( points [ "<STR_LIT:P>" ] ) ) : <EOL> self . assertEqual ( shading [ n ] [ i ] [ <NUM_LIT:0> ] , points [ "<STR_LIT:P>" ] [ i ] [ <NUM_LIT:0> ] ) <EOL> self . assertEqual ( shading [ n ] [ i ] [ <NUM_LIT:1> ] , points [ "<STR_LIT:P>" ] [ i ] [ <NUM_LIT:1> ] ) <EOL> self . assertEqual ( shading [ n ] [ i ] [ <NUM_LIT:2> ] , points [ "<STR_LIT:P>" ] [ i ] [ <NUM_LIT:2> ] ) <EOL> def testDebugClosureWithInternalValue ( self ) : <EOL> shader = self . compileShader ( os . path . dirname ( __file__ ) + "<STR_LIT>" ) <EOL> r = GafferOSL . OSLRenderer ( ) <EOL> with IECore . WorldBlock ( r ) : <EOL> r . shader ( "<STR_LIT>" , shader , { "<STR_LIT:value>" : IECore . Color3f ( <NUM_LIT:1> , <NUM_LIT:0.5> , <NUM_LIT> ) } ) <EOL> points = self . rectanglePoints ( ) <EOL> shading = r . shadingEngine ( ) . shade ( self . rectanglePoints ( ) ) <EOL> self . assertTrue ( isinstance ( shading [ "<STR_LIT:f>" ] , IECore . FloatVectorData ) ) <EOL> self . assertTrue ( isinstance ( shading [ "<STR_LIT:p>" ] , IECore . V3fVectorData ) ) <EOL> self . assertTrue ( isinstance ( shading [ "<STR_LIT:v>" ] , IECore . V3fVectorData ) ) <EOL> self . assertTrue ( isinstance ( shading [ "<STR_LIT:n>" ] , IECore . V3fVectorData ) ) <EOL> self . assertTrue ( isinstance ( shading [ "<STR_LIT:c>" ] , IECore . Color3fVectorData ) ) <EOL> self . assertEqual ( shading [ "<STR_LIT:p>" ] . getInterpretation ( ) , IECore . GeometricData . Interpretation . Point ) <EOL> self . assertEqual ( shading [ "<STR_LIT:v>" ] . getInterpretation ( ) , IECore . GeometricData . Interpretation . Vector ) <EOL> self . assertEqual ( shading [ "<STR_LIT:n>" ] . getInterpretation ( ) , IECore . GeometricData . Interpretation . Normal ) <EOL> for i in range ( <NUM_LIT:0> , len ( points [ "<STR_LIT:P>" ] ) ) : <EOL> self . assertEqual ( shading [ "<STR_LIT:f>" ] [ i ] , <NUM_LIT:1> ) <EOL> self . assertEqual ( shading [ "<STR_LIT:p>" ] [ i ] , IECore . V3f ( <NUM_LIT:1> , <NUM_LIT:0.5> , <NUM_LIT> ) ) <EOL> self . assertEqual ( shading [ "<STR_LIT:v>" ] [ i ] , IECore . V3f ( <NUM_LIT:1> , <NUM_LIT:0.5> , <NUM_LIT> ) ) <EOL> self . assertEqual ( shading [ "<STR_LIT:n>" ] [ i ] , IECore . V3f ( <NUM_LIT:1> , <NUM_LIT:0.5> , <NUM_LIT> ) ) <EOL> self . assertEqual ( shading [ "<STR_LIT:c>" ] [ i ] , IECore . Color3f ( <NUM_LIT:1> , <NUM_LIT:0.5> , <NUM_LIT> ) ) <EOL> def testDebugClosureWithZeroValue ( self ) : <EOL> shader = self . compileShader ( os . path . dirname ( __file__ ) + "<STR_LIT>" ) <EOL> r = GafferOSL . OSLRenderer ( ) <EOL> with IECore . WorldBlock ( r ) : <EOL> r . shader ( "<STR_LIT>" , shader , { "<STR_LIT:value>" : IECore . Color3f ( <NUM_LIT:0> ) } ) <EOL> points = self . rectanglePoints ( ) <EOL> shading = r . shadingEngine ( ) . shade ( self . rectanglePoints ( ) ) <EOL> self . assertTrue ( isinstance ( shading [ "<STR_LIT:f>" ] , IECore . FloatVectorData ) ) <EOL> self . assertTrue ( isinstance ( shading [ "<STR_LIT:p>" ] , IECore . V3fVectorData ) ) <EOL> self . assertTrue ( isinstance ( shading [ "<STR_LIT:v>" ] , IECore . V3fVectorData ) ) <EOL> self . assertTrue ( isinstance ( shading [ "<STR_LIT:n>" ] , IECore . V3fVectorData ) ) <EOL> self . assertTrue ( isinstance ( shading [ "<STR_LIT:c>" ] , IECore . Color3fVectorData ) ) <EOL> self . assertEqual ( shading [ "<STR_LIT:p>" ] . getInterpretation ( ) , IECore . GeometricData . Interpretation . Point ) <EOL> self . assertEqual ( shading [ "<STR_LIT:v>" ] . getInterpretation ( ) , IECore . GeometricData . Interpretation . Vector ) <EOL> self . assertEqual ( shading [ "<STR_LIT:n>" ] . getInterpretation ( ) , IECore . GeometricData . Interpretation . Normal ) <EOL> for i in range ( <NUM_LIT:0> , len ( points [ "<STR_LIT:P>" ] ) ) : <EOL> self . assertEqual ( shading [ "<STR_LIT:f>" ] [ i ] , <NUM_LIT:0> ) <EOL> self . assertEqual ( shading [ "<STR_LIT:p>" ] [ i ] , IECore . V3f ( <NUM_LIT:0> ) ) <EOL> self . assertEqual ( shading [ "<STR_LIT:v>" ] [ i ] , IECore . V3f ( <NUM_LIT:0> ) ) <EOL> self . assertEqual ( shading [ "<STR_LIT:n>" ] [ i ] , IECore . V3f ( <NUM_LIT:0> ) ) <EOL> self . assertEqual ( shading [ "<STR_LIT:c>" ] [ i ] , IECore . Color3f ( <NUM_LIT:0> ) ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> unittest . main ( ) </s>
