<s> import numpy as np <EOL> from numpy . testing import assert_allclose <EOL> from vispy . app import Canvas <EOL> from vispy . gloo import ( Texture2D , Texture3D , Program , FrameBuffer , <EOL> RenderBuffer , set_viewport , clear ) <EOL> from vispy . gloo . util import draw_texture , _screenshot <EOL> from vispy . testing import ( requires_application , has_pyopengl , <EOL> run_tests_if_main , <EOL> assert_raises , assert_equal ) <EOL> @ requires_application ( ) <EOL> def test_use_textures ( ) : <EOL> """<STR_LIT>""" <EOL> assert_raises ( ValueError , Texture2D , np . zeros ( ( <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:3> ) , np . float32 ) , <EOL> format = '<STR_LIT>' ) <EOL> @ requires_application ( ) <EOL> def test_use_framebuffer ( ) : <EOL> """<STR_LIT>""" <EOL> shape = ( <NUM_LIT:100> , <NUM_LIT> ) <EOL> data = np . random . rand ( * shape ) . astype ( np . float32 ) <EOL> use_shape = shape + ( <NUM_LIT:3> , ) <EOL> with Canvas ( size = shape [ : : - <NUM_LIT:1> ] ) as c : <EOL> orig_tex = Texture2D ( data ) <EOL> fbo_tex = Texture2D ( use_shape , format = '<STR_LIT>' ) <EOL> rbo = RenderBuffer ( shape , '<STR_LIT>' ) <EOL> fbo = FrameBuffer ( color = fbo_tex ) <EOL> c . context . glir . set_verbose ( True ) <EOL> assert_equal ( c . size , shape [ : : - <NUM_LIT:1> ] ) <EOL> set_viewport ( ( <NUM_LIT:0> , <NUM_LIT:0> ) + c . size ) <EOL> with fbo : <EOL> draw_texture ( orig_tex ) <EOL> draw_texture ( fbo_tex ) <EOL> out_tex = _screenshot ( ) [ : : - <NUM_LIT:1> , : , <NUM_LIT:0> ] . astype ( np . float32 ) <EOL> assert_equal ( out_tex . shape , c . size [ : : - <NUM_LIT:1> ] ) <EOL> assert_raises ( TypeError , FrameBuffer . color_buffer . fset , fbo , <NUM_LIT:1.> ) <EOL> assert_raises ( TypeError , FrameBuffer . depth_buffer . fset , fbo , <NUM_LIT:1.> ) <EOL> assert_raises ( TypeError , FrameBuffer . stencil_buffer . fset , fbo , <NUM_LIT:1.> ) <EOL> fbo . color_buffer = rbo <EOL> fbo . depth_buffer = RenderBuffer ( shape ) <EOL> fbo . stencil_buffer = None <EOL> print ( ( fbo . color_buffer , fbo . depth_buffer , fbo . stencil_buffer ) ) <EOL> clear ( color = '<STR_LIT>' ) <EOL> with fbo : <EOL> clear ( color = '<STR_LIT>' ) <EOL> draw_texture ( orig_tex ) <EOL> out_rbo = _screenshot ( ) [ : , : , <NUM_LIT:0> ] . astype ( np . float32 ) <EOL> assert_allclose ( data * <NUM_LIT> , out_tex , atol = <NUM_LIT:1> ) <EOL> assert_allclose ( data * <NUM_LIT> , out_rbo , atol = <NUM_LIT:1> ) <EOL> @ requires_application ( ) <EOL> def test_use_texture3D ( ) : <EOL> """<STR_LIT>""" <EOL> vals = [ <NUM_LIT:0> , <NUM_LIT:200> , <NUM_LIT:100> , <NUM_LIT:0> , <NUM_LIT:255> , <NUM_LIT:0> , <NUM_LIT:100> ] <EOL> d , h , w = len ( vals ) , <NUM_LIT:3> , <NUM_LIT:5> <EOL> data = np . zeros ( ( d , h , w ) , np . float32 ) <EOL> VERT_SHADER = """<STR_LIT>""" <EOL> FRAG_SHADER = """<STR_LIT>""" <EOL> for ii , val in enumerate ( vals ) : <EOL> data [ ii , : <NUM_LIT:2> , : <NUM_LIT:3> ] = val / <NUM_LIT> <EOL> with Canvas ( size = ( <NUM_LIT:100> , <NUM_LIT:100> ) ) as c : <EOL> if not has_pyopengl ( ) : <EOL> t = Texture3D ( data ) <EOL> assert_raises ( ImportError , t . glir . flush , c . context . shared . parser ) <EOL> return <EOL> program = Program ( VERT_SHADER , FRAG_SHADER ) <EOL> program [ '<STR_LIT>' ] = [ [ - <NUM_LIT:1.> , - <NUM_LIT:1.> ] , [ <NUM_LIT:1.> , - <NUM_LIT:1.> ] , [ - <NUM_LIT:1.> , <NUM_LIT:1.> ] , [ <NUM_LIT:1.> , <NUM_LIT:1.> ] ] <EOL> tex = Texture3D ( data , interpolation = '<STR_LIT>' ) <EOL> assert_equal ( tex . width , w ) <EOL> assert_equal ( tex . height , h ) <EOL> assert_equal ( tex . depth , d ) <EOL> program [ '<STR_LIT>' ] = tex <EOL> for ii , val in enumerate ( vals ) : <EOL> set_viewport ( <NUM_LIT:0> , <NUM_LIT:0> , w , h ) <EOL> clear ( color = '<STR_LIT>' ) <EOL> iii = ( ii + <NUM_LIT:0.5> ) / float ( d ) <EOL> print ( ii , iii ) <EOL> program [ '<STR_LIT:i>' ] = iii <EOL> program . draw ( '<STR_LIT>' ) <EOL> out = _screenshot ( ) [ : , : , <NUM_LIT:0> ] . astype ( int ) [ : : - <NUM_LIT:1> ] <EOL> expected = np . zeros_like ( out ) <EOL> expected [ : <NUM_LIT:2> , : <NUM_LIT:3> ] = val <EOL> assert_allclose ( out , expected , atol = <NUM_LIT:1.> / <NUM_LIT> ) <EOL> @ requires_application ( ) <EOL> def test_use_uniforms ( ) : <EOL> """<STR_LIT>""" <EOL> VERT_SHADER = """<STR_LIT>""" <EOL> FRAG_SHADER = """<STR_LIT>""" <EOL> shape = ( <NUM_LIT> , <NUM_LIT> ) <EOL> with Canvas ( size = shape ) as c : <EOL> c . context . glir . set_verbose ( True ) <EOL> assert_equal ( c . size , shape [ : : - <NUM_LIT:1> ] ) <EOL> shape = ( <NUM_LIT:3> , <NUM_LIT:3> ) <EOL> set_viewport ( ( <NUM_LIT:0> , <NUM_LIT:0> ) + shape ) <EOL> program = Program ( VERT_SHADER , FRAG_SHADER ) <EOL> program [ '<STR_LIT>' ] = [ [ - <NUM_LIT:1.> , - <NUM_LIT:1.> ] , [ <NUM_LIT:1.> , - <NUM_LIT:1.> ] , [ - <NUM_LIT:1.> , <NUM_LIT:1.> ] , [ <NUM_LIT:1.> , <NUM_LIT:1.> ] ] <EOL> program [ '<STR_LIT>' ] = np . ones ( ( <NUM_LIT:2> , <NUM_LIT:3> ) ) <EOL> c . context . clear ( '<STR_LIT:k>' ) <EOL> program . draw ( '<STR_LIT>' ) <EOL> out = _screenshot ( ) <EOL> assert_allclose ( out [ : , : , <NUM_LIT:0> ] / <NUM_LIT> , np . ones ( shape ) , atol = <NUM_LIT:1.> / <NUM_LIT> ) <EOL> program [ '<STR_LIT>' ] = np . zeros ( <NUM_LIT:3> , np . float32 ) <EOL> c . context . clear ( '<STR_LIT:k>' ) <EOL> program . draw ( '<STR_LIT>' ) <EOL> out = _screenshot ( ) <EOL> assert_allclose ( out [ : , : , <NUM_LIT:0> ] / <NUM_LIT> , <NUM_LIT> / <NUM_LIT> * np . ones ( shape ) , <EOL> atol = <NUM_LIT:1.> / <NUM_LIT> ) <EOL> assert_raises ( ValueError , program . __setitem__ , '<STR_LIT>' , <EOL> np . zeros ( <NUM_LIT:3> , np . float32 ) ) <EOL> program [ '<STR_LIT>' ] = np . zeros ( ( <NUM_LIT:2> , <NUM_LIT:3> ) , np . float32 ) <EOL> program [ '<STR_LIT>' ] = np . ones ( <NUM_LIT:3> , np . float32 ) <EOL> c . context . clear ( ( <NUM_LIT> , ) * <NUM_LIT:3> ) <EOL> program . draw ( '<STR_LIT>' ) <EOL> out = _screenshot ( ) <EOL> assert_allclose ( out [ : , : , <NUM_LIT:0> ] / <NUM_LIT> , <NUM_LIT> / <NUM_LIT> * np . ones ( shape ) , <EOL> atol = <NUM_LIT:1.> / <NUM_LIT> ) <EOL> run_tests_if_main ( ) </s>
