Major commit for preparing 1.1.1 release
[spandex:spandex.git] / vt2 / lib / testcases / opengles2 / buffers.py
1 # -*- mode: python -*-
2
3 #
4 # Spandex benchmark and test framework.
5 #
6 # Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
7 #
8 # Contact: Kari J. Kangas <kari.j.kangas@nokia.com>
9 #
10 #   This framework is free software; you can redistribute it and/or modify it
11 # under the terms of the GNU Lesser General Public License as published by the
12 # Free Software Foundation, version 2.1 of the License.
13 #
14 #   This framework is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
17 # for more details.
18 #
19 #   You should have received a copy of the GNU Lesser General Public License
20 # along with this framework; if not, see <http://www.gnu.org/licenses/>.
21 #
22
23 import vrt
24
25 from vtcommon           import *
26 from vtmesh             import *
27 from vttransformation   import *
28 from gles2common        import *
29
30 import math
31
32 WIDTH, HEIGHT = ( 320, 240, )
33 GRIDX, GRIDY  = ( 4, 4 )
34
35 ######################################################################
36 def createPlane( gridx, gridy ):
37         vertexAttribute   = MeshVertexAttribute( MESH_TYPE_FIXED, 3 )
38         colorAttribute    = MeshVertexAttribute( MESH_TYPE_UNSIGNED_BYTE, 4 )
39         texCoordAttribute = []
40         indexAttribute    = MeshAttribute( MESH_TYPE_UNSIGNED_SHORT )
41     
42         mesh = MeshStripPlane( gridx,
43                                gridy,
44                                vertexAttribute,
45                                colorAttribute,
46                                None,
47                                texCoordAttribute,
48                                indexAttribute,
49                                None,
50                                MESH_CCW_WINDING,
51                                False )
52         
53         mesh.translate( [ -0.5, -0.5, 0 ] )
54         mesh.scale( [ 2.0, 2.0 ] )
55
56         return mesh
57        
58 ######################################################################
59 def setupTexture( modules, indexTracker, w, h, gradient, c1, c2, textureType, minF, magF ):
60         OpenGLES2 = modules[ 'OpenGLES2' ]
61     
62         textureDataIndex = indexTracker.allocIndex( 'OPENGLES2_TEXTURE_DATA_INDEX' )
63         OpenGLES2.CreateGradientTextureData( textureDataIndex,
64                                              gradient,
65                                              c1,
66                                              c2,
67                                              w, h,
68                                              'OFF',
69                                              textureType )
70
71         textureIndex = indexTracker.allocIndex( 'OPENGLES2_TEXTURE_INDEX' )
72         OpenGLES2.GenTexture( textureIndex )
73         OpenGLES2.BindTexture( 'GL_TEXTURE_2D', textureIndex )
74         OpenGLES2.TexImage2D( textureDataIndex, 'GL_TEXTURE_2D' )
75         OpenGLES2.TexParameter( 'GL_TEXTURE_2D',
76                                 minF,
77                                 magF,
78                                 'GL_REPEAT',
79                                 'GL_REPEAT' )
80
81         return TextureSetup( textureDataIndex, textureIndex )
82
83 ######################################################################
84 class FramebufferWithDepth( vrt.VisualTestCase ):
85
86         ''' The purpose of the test: test framebuffer with depth.
87
88         Expected output: first the test draws a full screen mesh with a 4x4
89         vertex grid. The vertex colors are defined so that the lower-left vertex
90         has color [0.0,0.0,1.0,1.0] and the upper-right vertex has color
91         [1.0,1.0,1.0,1.0]. In between, the vertex color is linearly interpolated
92         from left to right, from bottom to top. The vertex depth values are
93         defined -1.0 and 1.0 in a similar fashion. The first mesh is drawn using
94         GL_ALWAYS depth func, after which the depth function is set
95         GL_LESS. Secondly, the test draws a full screen mesh with a 4x4 vertex
96         grid. The vertex colors are defined [1.0,1.0,1.0,1.0] (lower-left) and
97         [1.0,0.0,0.0,1.0] (upper-right). In between, the vertex color is
98         linearly interpolated from left to right, from bottom to top. The vertex
99         depth values are defined 1.0 and 0.0 in a similar fashion. Viewport size
100         320*240.
101         '''
102         testValues = { ( 'color0Format', 'depthFormat' ) : [ ( 'GL_RGBA4', 'GL_DEPTH_COMPONENT16' ),
103                                                              ( 'GL_RGB5_A1', 'GL_DEPTH_COMPONENT16' ),
104                                                              ( 'GL_RGB565', 'GL_DEPTH_COMPONENT16' ),
105                                                              ]
106                        }
107         
108         def __init__( self, color0Format, depthFormat ):
109                 vrt.VisualTestCase.__init__( self )
110                 self.repeats       = 1
111                 self.color0Format  = color0Format
112                 self.depthFormat   = depthFormat
113                 self.gridx         = GRIDX
114                 self.gridy         = GRIDY
115                 self.overdraw      = 1
116                 self.name = "OPENGLES2 basic framebuffer, color0=%s, depth=%s" % ( self.color0Format[ 3: ],
117                                                                                    self.depthFormat[ 3: ], )
118         
119         def build( self, target, modules ):       
120                 OpenGLES2 = modules[ 'OpenGLES2' ]
121                 Gltest    = modules[ 'Gltest' ]
122
123                 indexTracker = IndexTracker()
124                 
125                 vertexShaderSource = """
126                     precision mediump float;
127                     attribute vec4 gVertex;
128                     attribute vec4 gColor;
129                     varying vec4 gVSColor;
130                     uniform float gOffset;
131
132                     void main()
133                     {
134                         gl_Position = vec4( gVertex.x, gVertex.y, gVertex.z + gOffset, gVertex.w );
135                         gVSColor = gColor;
136                     }"""
137
138                 fragmentShaderSource = """
139                     precision mediump float;
140                     varying vec4 gVSColor;
141
142                     void main()
143                     {
144                         gl_FragColor = gVSColor;
145                     }"""
146
147                 vsSource = formatShader( vertexShaderSource )
148                 fsSource = formatShader( fragmentShaderSource )
149                 
150                 # ------------------------------------------------------------
151                 # Init actions
152                 self.beginInitActions()
153
154                 defaultAttributes = target.getDefaultAttributes( VT_API_OPENGLES2 )
155                 state = target.setup( modules,
156                                       indexTracker,
157                                       1, 1,
158                                       [ VT_API_OPENGLES2 ],
159                                       defaultAttributes )
160
161                 framebufferIndex = indexTracker.allocIndex( 'OPENGLES2_FRAMEBUFFER_INDEX' )
162                 OpenGLES2.GenFramebuffer( framebufferIndex )
163                 OpenGLES2.BindFramebuffer( 'GL_FRAMEBUFFER',
164                                            framebufferIndex )
165                 
166                 color0RenderbufferIndex = indexTracker.allocIndex( 'OPENGLES2_RENDERBUFFER_INDEX' )
167                 OpenGLES2.GenRenderbuffer( color0RenderbufferIndex )
168                 OpenGLES2.BindRenderbuffer( 'GL_RENDERBUFFER',
169                                             color0RenderbufferIndex )
170                 OpenGLES2.RenderbufferStorage( 'GL_RENDERBUFFER',
171                                                self.color0Format,
172                                                WIDTH, HEIGHT )
173                 
174                 depthRenderbufferIndex = indexTracker.allocIndex( 'OPENGLES2_RENDERBUFFER_INDEX' )
175                 OpenGLES2.GenRenderbuffer( depthRenderbufferIndex )
176                 OpenGLES2.BindRenderbuffer( 'GL_RENDERBUFFER',
177                                             depthRenderbufferIndex )
178                 OpenGLES2.RenderbufferStorage( 'GL_RENDERBUFFER',
179                                                self.depthFormat,
180                                                WIDTH, HEIGHT )
181
182                 OpenGLES2.FramebufferRenderbuffer( 'GL_FRAMEBUFFER',
183                                                    'GL_COLOR_ATTACHMENT0',
184                                                    'GL_RENDERBUFFER',
185                                                    color0RenderbufferIndex )
186
187                 OpenGLES2.FramebufferRenderbuffer( 'GL_FRAMEBUFFER',
188                                                    'GL_DEPTH_ATTACHMENT',
189                                                    'GL_RENDERBUFFER',
190                                                    depthRenderbufferIndex )
191
192                 OpenGLES2.CheckFramebufferStatus( 'GL_FRAMEBUFFER' )
193                 
194                 mesh1 = createPlane( self.gridx, self.gridy )
195                 mesh1.gradientColors( [ 0.0, 0.0, 1.0, 1.0 ], [ 1.0, 1.0, 1.0, 1.0 ] )
196                 mesh1.depthGradient( -1.0, 1.0 )
197                 
198                 mesh2 = createPlane( self.gridx, self.gridy )
199                 mesh2.gradientColors( [ 1.0, 0.0, 0.0, 1.0 ], [ 1.0, 1.0, 1.0, 1.0 ] )
200                 mesh2.depthGradient( 1.0, -1.0 )
201                 
202                 vertexDataSetup1 = setupVertexData( modules, indexTracker, mesh1 )
203                 vertexDataSetup2 = setupVertexData( modules, indexTracker, mesh2 )
204                 
205                 program = createProgram( modules, indexTracker, vsSource, fsSource, vertexDataSetup1 )
206                 useVertexData( modules, indexTracker, vertexDataSetup1, program )
207
208                 offsetLocationIndex     = indexTracker.allocIndex( 'OPENGLES2_LOCATION_INDEX' )
209                 OpenGLES2.GetUniformLocation( offsetLocationIndex, program.programIndex, 'gOffset' )
210
211                 OpenGLES2.Viewport( 0, 0, WIDTH, HEIGHT );
212                 OpenGLES2.ClearColor( [ 0.0, 0.0, 0.0, 1.0 ] )
213                 
214                 OpenGLES2.Disable( 'GL_DITHER' )
215                 
216                 # ------------------------------------------------------------
217                 # Start benchmark actions
218                 self.beginBenchmarkActions()
219
220                 OpenGLES2.Clear( [ 'GL_COLOR_BUFFER_BIT', 'GL_DEPTH_BUFFER_BIT' ] )
221
222                 OpenGLES2.Enable( 'GL_DEPTH_TEST' )
223                 OpenGLES2.DepthFunc( 'GL_ALWAYS' )                                
224                 OpenGLES2.Uniformf( offsetLocationIndex, 1, [ 0.0 ] )
225                 drawVertexData( modules, vertexDataSetup1 )
226                 
227                 OpenGLES2.DepthFunc( 'GL_LESS' )
228
229                 useVertexData( modules, indexTracker, vertexDataSetup2, program )
230                 OpenGLES2.Uniformf( offsetLocationIndex, 1, [ 0.0 ] )
231                 drawVertexData( modules, vertexDataSetup2 )
232
233                 OpenGLES2.CheckError()
234                 Gltest.SaveTarga( -1, 0, 0, WIDTH, HEIGHT, target.outputPath() )
235
236 ######################################################################
237 class FramebufferWithStencil( vrt.VisualTestCase ):
238
239         ''' The purpose of the test: test framebuffer with stencil.
240
241         Expected output: a blue rectangle in the middle of a red
242         rectangle. First, the buffers are cleared, color to [0,0,0,1] and
243         stencil to 1. Secondly a rectangle at the center of stencil buffer is
244         cleared to 2 using scissoring. Stencil function is set as GL_EQUAL, 2
245         0xFF, stencil operation is set as GL_INCR, GL_KEEP, GL_INCR. Stencil and
246         depth tests are enabled, depth test is set GL_LEQUAL so drawing always
247         passes. Finally, the test first draws a blue full screen rect, followed
248         by a red full screen screen rect. Viewport size 320*240.
249         '''
250         testValues = { ( 'color0Format', 'stencilFormat' ) : [ ( 'GL_RGBA4', 'GL_STENCIL_INDEX8' ),
251                                                                ( 'GL_RGB5_A1', 'GL_STENCIL_INDEX8' ),
252                                                                ( 'GL_RGB565', 'GL_STENCIL_INDEX8' ),
253                                                                ]
254                        }
255         
256         def __init__( self, color0Format, stencilFormat ):
257                 vrt.VisualTestCase.__init__( self )
258                 self.repeats       = 1
259                 self.color0Format  = color0Format
260                 self.stencilFormat = stencilFormat
261                 self.gridx         = GRIDX
262                 self.gridy         = GRIDY
263                 self.overdraw      = 1
264                 self.name = "OPENGLES2 framebuffer with stencil, color0=%s, stencil=%s" % ( self.color0Format[ 3: ],
265                                                                                             self.stencilFormat[ 3: ], )
266         
267         def build( self, target, modules ):       
268                 OpenGLES2 = modules[ 'OpenGLES2' ]
269                 Gltest    = modules[ 'Gltest' ]
270
271                 indexTracker = IndexTracker()
272                 
273                 vertexShaderSource = """
274                     precision mediump float;
275                     attribute vec4 gVertex;
276                     attribute vec4 gColor;
277                     varying vec4 gVSColor;
278                     uniform float gOffset;
279
280                     void main()
281                     {
282                         gl_Position = vec4( gVertex.x, gVertex.y, gVertex.z + gOffset, gVertex.w );
283                         gVSColor = gColor;
284                     }"""
285
286                 fragmentShaderSource = """
287                     precision mediump float;
288                     varying vec4 gVSColor;
289
290                     void main()
291                     {
292                         gl_FragColor = gVSColor;
293                     }"""
294
295                 vsSource = formatShader( vertexShaderSource )
296                 fsSource = formatShader( fragmentShaderSource )
297                 
298                 # ------------------------------------------------------------
299                 # Init actions
300                 self.beginInitActions()
301
302                 defaultAttributes = target.getDefaultAttributes( VT_API_OPENGLES2 )
303                 state = target.setup( modules,
304                                       indexTracker,
305                                       1, 1,
306                                       [ VT_API_OPENGLES2 ],
307                                       defaultAttributes )
308
309                 framebufferIndex = indexTracker.allocIndex( 'OPENGLES2_FRAMEBUFFER_INDEX' )
310                 OpenGLES2.GenFramebuffer( framebufferIndex )
311                 OpenGLES2.BindFramebuffer( 'GL_FRAMEBUFFER',
312                                            framebufferIndex )
313                 
314                 color0RenderbufferIndex = indexTracker.allocIndex( 'OPENGLES2_RENDERBUFFER_INDEX' )
315                 OpenGLES2.GenRenderbuffer( color0RenderbufferIndex )
316                 OpenGLES2.BindRenderbuffer( 'GL_RENDERBUFFER',
317                                             color0RenderbufferIndex )
318                 OpenGLES2.RenderbufferStorage( 'GL_RENDERBUFFER',
319                                                self.color0Format,
320                                                WIDTH, HEIGHT )
321                 
322                 stencilRenderbufferIndex = indexTracker.allocIndex( 'OPENGLES2_RENDERBUFFER_INDEX' )
323                 OpenGLES2.GenRenderbuffer( stencilRenderbufferIndex )
324                 OpenGLES2.BindRenderbuffer( 'GL_RENDERBUFFER',
325                                             stencilRenderbufferIndex )
326                 OpenGLES2.RenderbufferStorage( 'GL_RENDERBUFFER',
327                                                self.stencilFormat,
328                                                WIDTH, HEIGHT )
329
330                 OpenGLES2.FramebufferRenderbuffer( 'GL_FRAMEBUFFER',
331                                                    'GL_COLOR_ATTACHMENT0',
332                                                    'GL_RENDERBUFFER',
333                                                    color0RenderbufferIndex )
334
335                 OpenGLES2.FramebufferRenderbuffer( 'GL_FRAMEBUFFER',
336                                                    'GL_STENCIL_ATTACHMENT',
337                                                    'GL_RENDERBUFFER',
338                                                    stencilRenderbufferIndex )
339
340                 OpenGLES2.CheckFramebufferStatus( 'GL_FRAMEBUFFER' )
341                 
342                 mesh1 = createPlane( self.gridx, self.gridy )
343                 mesh1.gradientColors( [ 0.0, 0.0, 1.0, 1.0 ], [ 1.0, 1.0, 1.0, 1.0 ] )
344
345                 mesh2 = createPlane( self.gridx, self.gridy )
346                 mesh2.gradientColors( [ 1.0, 0.0, 0.0, 1.0 ], [ 1.0, 1.0, 1.0, 1.0 ] )
347                 
348                 vertexDataSetup1 = setupVertexData( modules, indexTracker, mesh1 )
349                 vertexDataSetup2 = setupVertexData( modules, indexTracker, mesh2 )
350                 
351                 program = createProgram( modules, indexTracker, vsSource, fsSource, vertexDataSetup1 )
352                 useVertexData( modules, indexTracker, vertexDataSetup1, program )
353
354                 offsetLocationIndex     = indexTracker.allocIndex( 'OPENGLES2_LOCATION_INDEX' )
355                 OpenGLES2.GetUniformLocation( offsetLocationIndex, program.programIndex, 'gOffset' )
356
357                 OpenGLES2.Viewport( 0, 0, WIDTH, HEIGHT );
358                 OpenGLES2.ClearColor( [ 0.0, 0.0, 0.0, 1.0 ] )
359                 OpenGLES2.ClearStencil( 1 )
360
361                 OpenGLES2.Clear( [ 'GL_COLOR_BUFFER_BIT', 'GL_STENCIL_BUFFER_BIT' ] )
362                 
363                 w = WIDTH  / 2
364                 h = HEIGHT / 2
365                 x = ( WIDTH - w  ) / 2
366                 y = ( HEIGHT - h ) / 2
367                 
368                 OpenGLES2.Scissor( x, y, w, h )
369                 OpenGLES2.Enable( 'GL_SCISSOR_TEST' )
370
371                 OpenGLES2.ClearStencil( 2 )
372                 OpenGLES2.Clear( [ 'GL_STENCIL_BUFFER_BIT' ] )
373                 OpenGLES2.Disable( 'GL_SCISSOR_TEST' )
374
375                 OpenGLES2.StencilFunc( 'GL_EQUAL', 2, '0xFF' )
376                 OpenGLES2.StencilOp( 'GL_INCR', 'GL_KEEP', 'GL_INCR' )
377                 OpenGLES2.Enable( 'GL_STENCIL_TEST' )
378
379                 OpenGLES2.Clear( [ 'GL_DEPTH_BUFFER_BIT' ] )
380                 OpenGLES2.Enable( 'GL_DEPTH_TEST' )
381                 OpenGLES2.DepthFunc( 'GL_LEQUAL' )
382                 
383                 OpenGLES2.Disable( 'GL_DITHER' )
384                 
385                 # ------------------------------------------------------------
386                 # Start benchmark actions
387                 self.beginBenchmarkActions()
388                 
389                 OpenGLES2.Uniformf( offsetLocationIndex, 1, [ 0.7 ] )
390                 drawVertexData( modules, vertexDataSetup1 )
391
392                 useVertexData( modules, indexTracker, vertexDataSetup2, program )               
393                 OpenGLES2.Uniformf( offsetLocationIndex, 1, [ 0.5 ] )
394                 drawVertexData( modules, vertexDataSetup2 )
395
396                 OpenGLES2.CheckError()
397                 Gltest.SaveTarga( -1, 0, 0, WIDTH, HEIGHT, target.outputPath() )
398                 
399 ######################################################################
400 class FramebufferWithDepthAndStencil( vrt.VisualTestCase ):
401
402         ''' The purpose of the test: test framebuffer with depth and stencil.
403
404         Expected output: a blue rectangle in the middle of a red
405         rectangle. First, the buffers are cleared, color to [0,0,0,1] and
406         stencil to 1. Secondly a rectangle at the center of stencil buffer is
407         cleared to 2 using scissoring. Stencil function is set as GL_EQUAL, 2
408         0xFF, stencil operation is set as GL_INCR, GL_KEEP, GL_INCR. Stencil and
409         depth tests are enabled, depth test is set GL_LEQUAL so drawing always
410         passes. Finally, the test first draws a blue full screen rect, followed
411         by a red full screen screen rect. Viewport size 320*240.
412         '''
413         testValues = { ( 'color0Format', 'depthFormat', 'stencilFormat' ) : [ ( 'GL_RGBA4', 'GL_DEPTH_COMPONENT16', 'GL_STENCIL_INDEX8' ),
414                                                                               ( 'GL_RGB5_A1', 'GL_DEPTH_COMPONENT16', 'GL_STENCIL_INDEX8' ),
415                                                                               ( 'GL_RGB565', 'GL_DEPTH_COMPONENT16', 'GL_STENCIL_INDEX8' ),
416                                                                               ]
417                        }
418         
419         def __init__( self, color0Format, depthFormat, stencilFormat ):
420                 vrt.VisualTestCase.__init__( self )
421                 self.repeats       = 1
422                 self.color0Format  = color0Format
423                 self.depthFormat   = depthFormat
424                 self.stencilFormat = stencilFormat
425                 self.gridx         = GRIDX
426                 self.gridy         = GRIDY
427                 self.overdraw      = 1
428                 self.name = "OPENGLES2 framebuffer with depth and stencil, color0=%s, depth=%s, stencil=%s" % ( self.color0Format[ 3: ],
429                                                                                                                 self.depthFormat[ 3: ],
430                                                                                                                 self.stencilFormat[ 3: ], )
431         
432         def build( self, target, modules ):       
433                 OpenGLES2 = modules[ 'OpenGLES2' ]
434                 Gltest    = modules[ 'Gltest' ]
435
436                 indexTracker = IndexTracker()
437                 
438                 vertexShaderSource = """
439                     precision mediump float;
440                     attribute vec4 gVertex;
441                     attribute vec4 gColor;
442                     varying vec4 gVSColor;
443                     uniform float gOffset;
444
445                     void main()
446                     {
447                         gl_Position = vec4( gVertex.x, gVertex.y, gVertex.z + gOffset, gVertex.w );
448                         gVSColor = gColor;
449                     }"""
450
451                 fragmentShaderSource = """
452                     precision mediump float;
453                     varying vec4 gVSColor;
454
455                     void main()
456                     {
457                         gl_FragColor = gVSColor;
458                     }"""
459
460                 vsSource = formatShader( vertexShaderSource )
461                 fsSource = formatShader( fragmentShaderSource )
462                 
463                 # ------------------------------------------------------------
464                 # Init actions
465                 self.beginInitActions()
466
467                 defaultAttributes = target.getDefaultAttributes( VT_API_OPENGLES2 )
468                 state = target.setup( modules,
469                                       indexTracker,
470                                       1, 1,
471                                       [ VT_API_OPENGLES2 ],
472                                       defaultAttributes )
473
474                 framebufferIndex = indexTracker.allocIndex( 'OPENGLES2_FRAMEBUFFER_INDEX' )
475                 OpenGLES2.GenFramebuffer( framebufferIndex )
476                 OpenGLES2.BindFramebuffer( 'GL_FRAMEBUFFER',
477                                            framebufferIndex )
478                 
479                 color0RenderbufferIndex = indexTracker.allocIndex( 'OPENGLES2_RENDERBUFFER_INDEX' )
480                 OpenGLES2.GenRenderbuffer( color0RenderbufferIndex )
481                 OpenGLES2.BindRenderbuffer( 'GL_RENDERBUFFER',
482                                             color0RenderbufferIndex )
483                 OpenGLES2.RenderbufferStorage( 'GL_RENDERBUFFER',
484                                                self.color0Format,
485                                                WIDTH, HEIGHT )
486                 
487                 depthRenderbufferIndex = indexTracker.allocIndex( 'OPENGLES2_RENDERBUFFER_INDEX' )
488                 OpenGLES2.GenRenderbuffer( depthRenderbufferIndex )
489                 OpenGLES2.BindRenderbuffer( 'GL_RENDERBUFFER',
490                                             depthRenderbufferIndex )
491                 OpenGLES2.RenderbufferStorage( 'GL_RENDERBUFFER',
492                                                self.depthFormat,
493                                                WIDTH, HEIGHT )
494
495                 stencilRenderbufferIndex = indexTracker.allocIndex( 'OPENGLES2_RENDERBUFFER_INDEX' )
496                 OpenGLES2.GenRenderbuffer( stencilRenderbufferIndex )
497                 OpenGLES2.BindRenderbuffer( 'GL_RENDERBUFFER',
498                                             stencilRenderbufferIndex )
499                 OpenGLES2.RenderbufferStorage( 'GL_RENDERBUFFER',
500                                                self.stencilFormat,
501                                                WIDTH, HEIGHT )
502
503                 OpenGLES2.FramebufferRenderbuffer( 'GL_FRAMEBUFFER',
504                                                    'GL_COLOR_ATTACHMENT0',
505                                                    'GL_RENDERBUFFER',
506                                                    color0RenderbufferIndex )
507
508                 OpenGLES2.FramebufferRenderbuffer( 'GL_FRAMEBUFFER',
509                                                    'GL_DEPTH_ATTACHMENT',
510                                                    'GL_RENDERBUFFER',
511                                                    depthRenderbufferIndex )
512
513                 OpenGLES2.FramebufferRenderbuffer( 'GL_FRAMEBUFFER',
514                                                    'GL_STENCIL_ATTACHMENT',
515                                                    'GL_RENDERBUFFER',
516                                                    stencilRenderbufferIndex )
517
518                 OpenGLES2.CheckFramebufferStatus( 'GL_FRAMEBUFFER' )
519
520                 mesh1 = createPlane( self.gridx, self.gridy )
521                 mesh1.gradientColors( [ 0.0, 0.0, 1.0, 1.0 ], [ 1.0, 1.0, 1.0, 1.0 ] )
522
523                 mesh2 = createPlane( self.gridx, self.gridy )
524                 mesh2.gradientColors( [ 1.0, 0.0, 0.0, 1.0 ], [ 1.0, 1.0, 1.0, 1.0 ] )
525                 
526                 vertexDataSetup1 = setupVertexData( modules, indexTracker, mesh1 )
527                 vertexDataSetup2 = setupVertexData( modules, indexTracker, mesh2 )
528                 
529                 program = createProgram( modules, indexTracker, vsSource, fsSource, vertexDataSetup1 )
530                 useVertexData( modules, indexTracker, vertexDataSetup1, program )
531
532                 offsetLocationIndex     = indexTracker.allocIndex( 'OPENGLES2_LOCATION_INDEX' )
533                 OpenGLES2.GetUniformLocation( offsetLocationIndex, program.programIndex, 'gOffset' )
534
535                 OpenGLES2.Viewport( 0, 0, WIDTH, HEIGHT );
536                 OpenGLES2.ClearColor( [ 0.0, 0.0, 0.0, 1.0 ] )
537                 OpenGLES2.ClearStencil( 1 )
538
539                 OpenGLES2.Clear( [ 'GL_COLOR_BUFFER_BIT', 'GL_STENCIL_BUFFER_BIT' ] )
540                 
541                 w = WIDTH  / 2
542                 h = HEIGHT / 2
543                 x = ( WIDTH - w  ) / 2
544                 y = ( HEIGHT - h ) / 2
545                 
546                 OpenGLES2.Scissor( x, y, w, h )
547                 OpenGLES2.Enable( 'GL_SCISSOR_TEST' )
548
549                 OpenGLES2.ClearStencil( 2 )
550                 OpenGLES2.Clear( [ 'GL_STENCIL_BUFFER_BIT' ] )
551                 OpenGLES2.Disable( 'GL_SCISSOR_TEST' )
552
553                 OpenGLES2.StencilFunc( 'GL_EQUAL', 2, '0xFF' )
554                 OpenGLES2.StencilOp( 'GL_INCR', 'GL_KEEP', 'GL_INCR' )
555                 OpenGLES2.Enable( 'GL_STENCIL_TEST' )
556
557                 OpenGLES2.Clear( [ 'GL_DEPTH_BUFFER_BIT' ] )
558                 OpenGLES2.Enable( 'GL_DEPTH_TEST' )
559                 OpenGLES2.DepthFunc( 'GL_LEQUAL' )
560
561                 OpenGLES2.Disable( 'GL_DITHER' )
562                 
563                 # ------------------------------------------------------------
564                 # Start benchmark actions
565                 self.beginBenchmarkActions()
566                 
567                 OpenGLES2.Uniformf( offsetLocationIndex, 1, [ 0.7 ] )
568                 drawVertexData( modules, vertexDataSetup1 )
569
570                 useVertexData( modules, indexTracker, vertexDataSetup2, program )               
571                 OpenGLES2.Uniformf( offsetLocationIndex, 1, [ 0.5 ] )
572                 drawVertexData( modules, vertexDataSetup2 )
573
574                 OpenGLES2.CheckError()
575                 Gltest.SaveTarga( -1, 0, 0, WIDTH, HEIGHT, target.outputPath() )
576
577 ######################################################################
578 def createPlane2( gridx, gridy, tcx, tcy, colorAttributes, texCoordAttributes ):
579         vertexAttribute   = MeshVertexAttribute( MESH_TYPE_FIXED, 3 )
580         colorAttribute    = None
581         texCoordAttribute = []
582         indexAttribute    = MeshAttribute( MESH_TYPE_UNSIGNED_SHORT )
583     
584         if colorAttributes:
585                 colorAttribute = MeshVertexAttribute( MESH_TYPE_UNSIGNED_BYTE, 4 )
586
587         if texCoordAttributes:
588                 for i in range( len( texCoordAttributes ) ):
589                         texCoordAttribute.append( MeshTexCoordAttribute( MESH_TYPE_FIXED,
590                                                                          2,
591                                                                          [ float( tcx ),
592                                                                            float( tcy ) ] ) )
593
594         mesh = MeshStripPlane( gridx,
595                                gridy,
596                                vertexAttribute,
597                                colorAttribute,
598                                None,
599                                texCoordAttribute,
600                                indexAttribute,
601                                None,
602                                MESH_CCW_WINDING,
603                                False )
604         
605         mesh.translate( [ -0.5, -0.5, 0 ] )
606         mesh.scale( [ 2.0, 2.0 ] )
607
608         return mesh
609        
610 ######################################################################
611 def setupTexture( modules, indexTracker, w, h, gradient, c1, c2, textureType, minF, magF ):
612         OpenGLES2 = modules[ 'OpenGLES2' ]
613     
614         textureDataIndex = indexTracker.allocIndex( 'OPENGLES2_TEXTURE_DATA_INDEX' )
615         OpenGLES2.CreateGradientTextureData( textureDataIndex,
616                                              gradient,
617                                              c1,
618                                              c2,
619                                              w, h,
620                                              'OFF',
621                                              textureType )
622
623         textureIndex = indexTracker.allocIndex( 'OPENGLES2_TEXTURE_INDEX' )
624         OpenGLES2.GenTexture( textureIndex )
625         OpenGLES2.BindTexture( 'GL_TEXTURE_2D', textureIndex )
626         OpenGLES2.TexImage2D( textureDataIndex, 'GL_TEXTURE_2D' )
627         OpenGLES2.TexParameter( 'GL_TEXTURE_2D',
628                                 minF,
629                                 magF,
630                                 'GL_REPEAT',
631                                 'GL_REPEAT' )
632
633         return TextureSetup( textureDataIndex, textureIndex )
634                 
635 ######################################################################
636 class FramebufferTexture2DColor( vrt.VisualTestCase ):
637
638         ''' The purpose of the test: test using texture as framebuffer color
639         attachment.
640
641         Expected output: full screen textured rectangle. The texture comprises
642         four rectangles, red in bottom-left, green in bottom-right, blue in
643         top-left, and white in top-right. The texture data is drawn using
644         texture as framebuffer color attachment. Viewport size 320*240.
645         '''
646
647         testValues = { ( 'textureType', 'pot' ) : [ ( 'OPENGLES2_RGB565', True ),
648                                                     ( 'OPENGLES2_RGB565', False ),
649                                                     ( 'OPENGLES2_RGB888', True ),
650                                                     ( 'OPENGLES2_RGB888', False ),
651                                                     ( 'OPENGLES2_RGBA4444', True ),
652                                                     ( 'OPENGLES2_RGBA4444', False ),                                             
653                                                     ( 'OPENGLES2_RGBA5551', True ),
654                                                     ( 'OPENGLES2_RGBA5551', False ),                                             
655                                                     ( 'OPENGLES2_RGBA8888', True ),
656                                                     ( 'OPENGLES2_RGBA8888', False ),                                             
657                                                     ]
658                        }
659         
660         def __init__( self, textureType, pot ):
661                 vrt.VisualTestCase.__init__( self )
662                 self.textureType = textureType
663                 self.pot         = pot
664                 self.repeats     = 1
665                 self.gridx       = GRIDX
666                 self.gridy       = GRIDY
667                 self.overdraw    = 1
668                 self.name = "OPENGLES2 FramebufferTexture2D color, texture type=%s, pot=%s" % ( textureTypeToStr( self.textureType ),
669                                                                                                 boolToStr( self.pot ), )
670         
671         def build( self, target, modules ):       
672                 OpenGLES2 = modules[ 'OpenGLES2' ]
673                 Gltest    = modules[ 'Gltest' ]
674
675                 indexTracker = IndexTracker()
676
677                 vertexShaderSource = """
678                     precision mediump float;
679                     attribute vec4 gVertex;
680                     attribute vec4 gTexCoord0;
681                     varying vec4 gVSTexCoord0;
682                     uniform float gOffset;
683
684                     void main()
685                     {
686                         gl_Position = vec4( gVertex.x, gVertex.y, gVertex.z + gOffset, gVertex.w );
687                         gVSTexCoord0 = gTexCoord0;
688                     }"""
689
690                 fragmentShaderSource = """
691                     precision mediump float;
692                     uniform sampler2D gTexture0;
693                     varying vec4 gVSTexCoord0;
694
695                     void main()
696                     {
697                         gl_FragColor = texture2D( gTexture0, gVSTexCoord0.xy );
698                     }"""
699
700                 vsSource = formatShader( vertexShaderSource )
701                 fsSource = formatShader( fragmentShaderSource )
702                 
703                 # ------------------------------------------------------------
704                 # Init actions
705                 self.beginInitActions()
706
707                 defaultAttributes = target.getDefaultAttributes( VT_API_OPENGLES2 )
708                 state             = target.setup( modules,
709                                                   indexTracker,
710                                                   WIDTH, HEIGHT,
711                                                   [ VT_API_OPENGLES2 ],
712                                                   defaultAttributes )
713
714                 textureW = toPot( WIDTH, True )[ 0 ]
715                 textureH = toPot( HEIGHT, True )[ 0 ]
716
717                 if not self.pot:
718                         textureW -= 1
719                         textureH -= 1
720                 
721                 framebufferIndex = indexTracker.allocIndex( 'OPENGLES2_FRAMEBUFFER_INDEX' )
722                 OpenGLES2.GenFramebuffer( framebufferIndex )
723                 OpenGLES2.BindFramebuffer( 'GL_FRAMEBUFFER',
724                                            framebufferIndex )
725
726                 textureDataIndex = indexTracker.allocIndex( 'OPENGLES2_TEXTURE_DATA_INDEX' )
727                 OpenGLES2.CreateNullTextureData( textureDataIndex,
728                                                  textureW, textureH,
729                                                  'OFF',
730                                                  self.textureType )
731
732                 textureIndex = indexTracker.allocIndex( 'OPENGLES2_TEXTURE_INDEX' )
733                 OpenGLES2.GenTexture( textureIndex )
734                 OpenGLES2.BindTexture( 'GL_TEXTURE_2D',
735                                        textureIndex )
736                 OpenGLES2.TexImage2D( textureDataIndex,
737                                       'GL_TEXTURE_2D' )
738
739                 OpenGLES2.FramebufferTexture2D( 'GL_FRAMEBUFFER',
740                                                 'GL_COLOR_ATTACHMENT0',
741                                                 'GL_TEXTURE_2D',
742                                                 textureIndex,
743                                                 0 )
744
745                 OpenGLES2.CheckFramebufferStatus( 'GL_FRAMEBUFFER' )
746                 OpenGLES2.Viewport( 0, 0, textureW, textureH );
747                 
748                 w = textureW / 2
749                 h = textureH / 2
750                 x = ( textureW - w  ) / 2
751                 y = ( textureH - h ) / 2 
752
753                 OpenGLES2.Enable( 'GL_SCISSOR_TEST' )                
754                 OpenGLES2.Scissor( 0, 0, textureW / 2, textureH / 2 )
755                 OpenGLES2.ClearColor( [ 1.0, 0.0, 0.0, 1.0 ] )
756                 OpenGLES2.Clear( [ 'GL_COLOR_BUFFER_BIT' ] )
757
758                 OpenGLES2.Scissor( textureW / 2, 0, textureW / 2, textureH / 2 )
759                 OpenGLES2.ClearColor( [ 0.0, 1.0, 0.0, 1.0 ] )
760                 OpenGLES2.Clear( [ 'GL_COLOR_BUFFER_BIT' ] )
761
762                 OpenGLES2.Scissor( 0, textureH /2, textureW / 2, textureH / 2 )
763                 OpenGLES2.ClearColor( [ 0.0, 0.0, 1.0, 1.0 ] )
764                 OpenGLES2.Clear( [ 'GL_COLOR_BUFFER_BIT' ] )
765                 
766                 OpenGLES2.Scissor( textureW / 2, textureH / 2, textureW / 2, textureH / 2 )
767                 OpenGLES2.ClearColor( [ 1.0, 1.0, 1.0, 1.0 ] )
768                 OpenGLES2.Clear( [ 'GL_COLOR_BUFFER_BIT' ] )
769
770                 OpenGLES2.Disable( 'GL_SCISSOR_TEST' )
771                 OpenGLES2.BindFramebuffer( 'GL_FRAMEBUFFER',
772                                            -1 )
773                 
774                 mesh              = createPlane2( self.gridx, self.gridy, 1.0, 1.0, False, [ True ] )
775                 
776                 vertexDataSetup   = setupVertexData( modules, indexTracker, mesh )
777                 program           = createProgram( modules, indexTracker, vsSource, fsSource, vertexDataSetup )
778                 
779                 useVertexData( modules, indexTracker, vertexDataSetup, program )
780
781                 OpenGLES2.ActiveTexture( 0 )
782                 OpenGLES2.BindTexture( 'GL_TEXTURE_2D', textureIndex )
783                 OpenGLES2.TexParameter( 'GL_TEXTURE_2D',
784                                         'GL_LINEAR',
785                                         'GL_LINEAR',
786                                         'GL_CLAMP_TO_EDGE',
787                                         'GL_CLAMP_TO_EDGE' )
788                 
789                 OpenGLES2.Uniformi( program.textureLocationIndices[ 0 ], 1, [ 0 ] )
790                 
791                 offsetLocationIndex     = indexTracker.allocIndex( 'OPENGLES2_LOCATION_INDEX' )
792                 OpenGLES2.GetUniformLocation( offsetLocationIndex, program.programIndex, 'gOffset' )
793
794                 OpenGLES2.Viewport( 0, 0, WIDTH, HEIGHT );
795                 OpenGLES2.ClearColor( [ 0.0, 0.0, 0.0, 1.0 ] )
796
797                 OpenGLES2.Disable( 'GL_DITHER' )
798                 
799                 # ------------------------------------------------------------
800                 # Start benchmark actions
801                 self.beginBenchmarkActions()
802                 
803                 OpenGLES2.Clear( [ 'GL_COLOR_BUFFER_BIT' ] )
804
805                 OpenGLES2.Uniformf( offsetLocationIndex, 1, [ 0 ] )
806                 drawVertexData( modules, vertexDataSetup )
807
808                 OpenGLES2.CheckError()
809                 Gltest.SaveTarga( -1, 0, 0, WIDTH, HEIGHT, target.outputPath() )               
810
811 ######################################################################
812 class FramebufferTexture2DColorNpot( vrt.VisualTestCase ):
813
814         ''' The purpose of the test: test using npot texture as framebuffer
815         color attachment, with GL_CLAMP_TO_EDGE wrapping mode.
816
817         Expected output: full screen textured rectangle with texture coordinates
818         running from 0 to 1.0 with GL_CLAMP_TO_EDGE wrapping mode. The texture
819         comprises four rectangles, red in bottom-left, green in bottom-right,
820         blue in top-left, and white in top-right. The texture data is drawn
821         using texture as framebuffer color attachment. Viewport size 320*240.
822         '''
823
824         testValues = { ( 'textureType' ) : [ ( 'OPENGLES2_RGB565' ),
825                                              ( 'OPENGLES2_RGB888' ),
826                                              ( 'OPENGLES2_RGBA4444' ),
827                                              ( 'OPENGLES2_RGBA5551' ),
828                                              ( 'OPENGLES2_RGBA8888' ),
829                                              ]
830                        }
831         
832         def __init__( self, textureType ):
833                 vrt.VisualTestCase.__init__( self )
834                 self.textureType = textureType
835                 self.repeats     = 1
836                 self.gridx       = GRIDX
837                 self.gridy       = GRIDY
838                 self.overdraw    = 1
839                 self.name = "OPENGLES2 FramebufferTexture2D color NPOT texture, texture type=%s" % ( textureTypeToStr( self.textureType ), )
840         
841         def build( self, target, modules ):       
842                 OpenGLES2 = modules[ 'OpenGLES2' ]
843                 Gltest    = modules[ 'Gltest' ]
844
845                 indexTracker = IndexTracker()
846
847                 vertexShaderSource = """
848                     precision mediump float;
849                     attribute vec4 gVertex;
850                     attribute vec4 gTexCoord0;
851                     varying vec4 gVSTexCoord0;
852                     uniform float gOffset;
853
854                     void main()
855                     {
856                         gl_Position = vec4( gVertex.x, gVertex.y, gVertex.z + gOffset, gVertex.w );
857                         gVSTexCoord0 = gTexCoord0;
858                     }"""
859
860                 fragmentShaderSource = """
861                     precision mediump float;
862                     uniform sampler2D gTexture0;
863                     varying vec4 gVSTexCoord0;
864
865                     void main()
866                     {
867                         gl_FragColor = texture2D( gTexture0, gVSTexCoord0.xy );
868                     }"""
869
870                 vsSource = formatShader( vertexShaderSource )
871                 fsSource = formatShader( fragmentShaderSource )
872                 
873                 # ------------------------------------------------------------
874                 # Init actions
875                 self.beginInitActions()
876
877                 defaultAttributes = target.getDefaultAttributes( VT_API_OPENGLES2 )
878                 state             = target.setup( modules,
879                                                   indexTracker,
880                                                   WIDTH, HEIGHT,
881                                                   [ VT_API_OPENGLES2 ],
882                                                   defaultAttributes )
883                
884                 textureW = toPot( WIDTH, True )[ 0 ] - 1 
885                 textureH = toPot( HEIGHT, True )[ 0 ] - 1
886
887                 framebufferIndex = indexTracker.allocIndex( 'OPENGLES2_FRAMEBUFFER_INDEX' )
888                 OpenGLES2.GenFramebuffer( framebufferIndex )
889                 OpenGLES2.BindFramebuffer( 'GL_FRAMEBUFFER',
890                                            framebufferIndex )
891
892                 textureDataIndex = indexTracker.allocIndex( 'OPENGLES2_TEXTURE_DATA_INDEX' )
893                 OpenGLES2.CreateNullTextureData( textureDataIndex,
894                                                  textureW, textureH,
895                                                  'OFF',
896                                                  self.textureType )
897
898                 textureIndex = indexTracker.allocIndex( 'OPENGLES2_TEXTURE_INDEX' )
899                 OpenGLES2.GenTexture( textureIndex )
900                 OpenGLES2.BindTexture( 'GL_TEXTURE_2D',
901                                        textureIndex )
902                 OpenGLES2.TexImage2D( textureDataIndex,
903                                       'GL_TEXTURE_2D' )
904
905                 OpenGLES2.FramebufferTexture2D( 'GL_FRAMEBUFFER',
906                                                 'GL_COLOR_ATTACHMENT0',
907                                                 'GL_TEXTURE_2D',
908                                                 textureIndex,
909                                                 0 )
910
911                 OpenGLES2.CheckFramebufferStatus( 'GL_FRAMEBUFFER' )
912                 OpenGLES2.Viewport( 0, 0, textureW, textureH );
913                 
914                 w = textureW / 2
915                 h = textureH / 2
916                 x = ( textureW - w  ) / 2
917                 y = ( textureH - h ) / 2 
918
919                 OpenGLES2.Enable( 'GL_SCISSOR_TEST' )                
920                 OpenGLES2.Scissor( 0, 0, textureW / 2, textureH / 2 )
921                 OpenGLES2.ClearColor( [ 1.0, 0.0, 0.0, 1.0 ] )
922                 OpenGLES2.Clear( [ 'GL_COLOR_BUFFER_BIT' ] )
923
924                 OpenGLES2.Scissor( textureW / 2, 0, textureW / 2, textureH / 2 )
925                 OpenGLES2.ClearColor( [ 0.0, 1.0, 0.0, 1.0 ] )
926                 OpenGLES2.Clear( [ 'GL_COLOR_BUFFER_BIT' ] )
927
928                 OpenGLES2.Scissor( 0, textureH /2, textureW / 2, textureH / 2 )
929                 OpenGLES2.ClearColor( [ 0.0, 0.0, 1.0, 1.0 ] )
930                 OpenGLES2.Clear( [ 'GL_COLOR_BUFFER_BIT' ] )
931                 
932                 OpenGLES2.Scissor( textureW / 2, textureH / 2, textureW / 2, textureH / 2 )
933                 OpenGLES2.ClearColor( [ 1.0, 1.0, 1.0, 1.0 ] )
934                 OpenGLES2.Clear( [ 'GL_COLOR_BUFFER_BIT' ] )
935
936                 OpenGLES2.Disable( 'GL_SCISSOR_TEST' )
937                 OpenGLES2.BindFramebuffer( 'GL_FRAMEBUFFER',
938                                            -1 )
939                 
940                 mesh              = createPlane2( self.gridx, self.gridy, 1.0, 1.0, False, [ True ] )
941                 
942                 vertexDataSetup   = setupVertexData( modules, indexTracker, mesh )
943                 program           = createProgram( modules, indexTracker, vsSource, fsSource, vertexDataSetup )
944                 
945                 useVertexData( modules, indexTracker, vertexDataSetup, program )
946
947                 OpenGLES2.ActiveTexture( 0 )
948                 OpenGLES2.BindTexture( 'GL_TEXTURE_2D', textureIndex )
949                 OpenGLES2.TexParameter( 'GL_TEXTURE_2D',
950                                         'GL_LINEAR',
951                                         'GL_LINEAR',
952                                         'GL_CLAMP_TO_EDGE',
953                                         'GL_CLAMP_TO_EDGE' )
954                 
955                 OpenGLES2.Uniformi( program.textureLocationIndices[ 0 ], 1, [ 0 ] )
956                 
957                 offsetLocationIndex     = indexTracker.allocIndex( 'OPENGLES2_LOCATION_INDEX' )
958                 OpenGLES2.GetUniformLocation( offsetLocationIndex, program.programIndex, 'gOffset' )
959
960                 OpenGLES2.Viewport( 0, 0, WIDTH, HEIGHT );
961                 OpenGLES2.ClearColor( [ 0.0, 0.0, 0.0, 1.0 ] )
962
963                 OpenGLES2.Disable( 'GL_DITHER' )
964                 
965                 # ------------------------------------------------------------
966                 # Start benchmark actions
967                 self.beginBenchmarkActions()
968                 
969                 OpenGLES2.Clear( [ 'GL_COLOR_BUFFER_BIT' ] )
970
971                 OpenGLES2.Uniformf( offsetLocationIndex, 1, [ 0 ] )
972                 drawVertexData( modules, vertexDataSetup )
973
974                 OpenGLES2.CheckError()
975                 Gltest.SaveTarga( -1, 0, 0, WIDTH, HEIGHT, target.outputPath() )               
976
977 ######################################################################
978 class FramebufferTexture2DColorNpotExt( vrt.VisualTestCase ):
979
980         ''' The purpose of the test: test using npot texture as framebuffer
981         color attachment, with GL_MIRRORED_REPEAT wrapping mode. Requires
982         GL_OES_texture_npot extension.
983
984         Expected output: full screen textured rectangle with texture coordinates
985         running from 0 to 2.5 with GL_MIRRORED_REPEAT wrapping mode. The texture
986         comprises four rectangles, red in bottom-left, green in bottom-right,
987         blue in top-left, and white in top-right. The texture data is drawn
988         using texture as framebuffer color attachment. Viewport size 320*240.
989         '''
990
991         testValues = { ( 'textureType' ) : [ ( 'OPENGLES2_RGB565' ),
992                                              ( 'OPENGLES2_RGB888' ),
993                                              ( 'OPENGLES2_RGBA4444' ),
994                                              ( 'OPENGLES2_RGBA5551' ),
995                                              ( 'OPENGLES2_RGBA8888' ),
996                                              ]
997                        }
998         
999         def __init__( self, textureType ):
1000                 vrt.VisualTestCase.__init__( self )
1001                 self.textureType = textureType
1002                 self.repeats     = 1
1003                 self.gridx       = GRIDX
1004                 self.gridy       = GRIDY
1005                 self.overdraw    = 1
1006                 self.name = "OPENGLES2 FramebufferTexture2D color NPOT texture ext, texture type=%s" % ( textureTypeToStr( self.textureType ), )
1007         
1008         def build( self, target, modules ):       
1009                 OpenGLES2 = modules[ 'OpenGLES2' ]
1010                 Gltest    = modules[ 'Gltest' ]
1011
1012                 indexTracker = IndexTracker()
1013
1014                 vertexShaderSource = """
1015                     precision mediump float;
1016                     attribute vec4 gVertex;
1017                     attribute vec4 gTexCoord0;
1018                     varying vec4 gVSTexCoord0;
1019                     uniform float gOffset;
1020
1021                     void main()
1022                     {
1023                         gl_Position = vec4( gVertex.x, gVertex.y, gVertex.z + gOffset, gVertex.w );
1024                         gVSTexCoord0 = gTexCoord0;
1025                     }"""
1026
1027                 fragmentShaderSource = """
1028                     precision mediump float;
1029                     uniform sampler2D gTexture0;
1030                     varying vec4 gVSTexCoord0;
1031
1032                     void main()
1033                     {
1034                         gl_FragColor = texture2D( gTexture0, gVSTexCoord0.xy );
1035                     }"""
1036
1037                 vsSource = formatShader( vertexShaderSource )
1038                 fsSource = formatShader( fragmentShaderSource )
1039                 
1040                 # ------------------------------------------------------------
1041                 # Init actions
1042                 self.beginInitActions()
1043
1044                 defaultAttributes = target.getDefaultAttributes( VT_API_OPENGLES2 )
1045                 state             = target.setup( modules,
1046                                                   indexTracker,
1047                                                   WIDTH, HEIGHT,
1048                                                   [ VT_API_OPENGLES2 ],
1049                                                   defaultAttributes )
1050
1051                 OpenGLES2.CheckExtension( 'GL_OES_texture_npot' )
1052                 
1053                 textureW = toPot( WIDTH, True )[ 0 ] - 1 
1054                 textureH = toPot( HEIGHT, True )[ 0 ] - 1
1055
1056                 framebufferIndex = indexTracker.allocIndex( 'OPENGLES2_FRAMEBUFFER_INDEX' )
1057                 OpenGLES2.GenFramebuffer( framebufferIndex )
1058                 OpenGLES2.BindFramebuffer( 'GL_FRAMEBUFFER',
1059                                            framebufferIndex )
1060
1061                 textureDataIndex = indexTracker.allocIndex( 'OPENGLES2_TEXTURE_DATA_INDEX' )
1062                 OpenGLES2.CreateNullTextureData( textureDataIndex,
1063                                                  textureW, textureH,
1064                                                  'OFF',
1065                                                  self.textureType )
1066
1067                 textureIndex = indexTracker.allocIndex( 'OPENGLES2_TEXTURE_INDEX' )
1068                 OpenGLES2.GenTexture( textureIndex )
1069                 OpenGLES2.BindTexture( 'GL_TEXTURE_2D',
1070                                        textureIndex )
1071                 OpenGLES2.TexImage2D( textureDataIndex,
1072                                       'GL_TEXTURE_2D' )
1073
1074                 OpenGLES2.FramebufferTexture2D( 'GL_FRAMEBUFFER',
1075                                                 'GL_COLOR_ATTACHMENT0',
1076                                                 'GL_TEXTURE_2D',
1077                                                 textureIndex,
1078                                                 0 )
1079
1080                 OpenGLES2.CheckFramebufferStatus( 'GL_FRAMEBUFFER' )
1081                 OpenGLES2.Viewport( 0, 0, textureW, textureH );
1082                 
1083                 w = textureW / 2
1084                 h = textureH / 2
1085                 x = ( textureW - w  ) / 2
1086                 y = ( textureH - h ) / 2 
1087
1088                 OpenGLES2.Enable( 'GL_SCISSOR_TEST' )                
1089                 OpenGLES2.Scissor( 0, 0, textureW / 2, textureH / 2 )
1090                 OpenGLES2.ClearColor( [ 1.0, 0.0, 0.0, 1.0 ] )
1091                 OpenGLES2.Clear( [ 'GL_COLOR_BUFFER_BIT' ] )
1092
1093                 OpenGLES2.Scissor( textureW / 2, 0, textureW / 2, textureH / 2 )
1094                 OpenGLES2.ClearColor( [ 0.0, 1.0, 0.0, 1.0 ] )
1095                 OpenGLES2.Clear( [ 'GL_COLOR_BUFFER_BIT' ] )
1096
1097                 OpenGLES2.Scissor( 0, textureH /2, textureW / 2, textureH / 2 )
1098                 OpenGLES2.ClearColor( [ 0.0, 0.0, 1.0, 1.0 ] )
1099                 OpenGLES2.Clear( [ 'GL_COLOR_BUFFER_BIT' ] )
1100                 
1101                 OpenGLES2.Scissor( textureW / 2, textureH / 2, textureW / 2, textureH / 2 )
1102                 OpenGLES2.ClearColor( [ 1.0, 1.0, 1.0, 1.0 ] )
1103                 OpenGLES2.Clear( [ 'GL_COLOR_BUFFER_BIT' ] )
1104
1105                 OpenGLES2.Disable( 'GL_SCISSOR_TEST' )
1106                 OpenGLES2.BindFramebuffer( 'GL_FRAMEBUFFER',
1107                                            -1 )
1108                 
1109                 mesh              = createPlane2( self.gridx, self.gridy, 2.5, 2.5, False, [ True ] )
1110                 
1111                 vertexDataSetup   = setupVertexData( modules, indexTracker, mesh )
1112                 program           = createProgram( modules, indexTracker, vsSource, fsSource, vertexDataSetup )
1113                 
1114                 useVertexData( modules, indexTracker, vertexDataSetup, program )
1115
1116                 OpenGLES2.ActiveTexture( 0 )
1117                 OpenGLES2.BindTexture( 'GL_TEXTURE_2D', textureIndex )
1118                 OpenGLES2.TexParameter( 'GL_TEXTURE_2D',
1119                                         'GL_LINEAR',
1120                                         'GL_LINEAR',
1121                                         'GL_MIRRORED_REPEAT',
1122                                         'GL_MIRRORED_REPEAT' )
1123                 
1124                 OpenGLES2.Uniformi( program.textureLocationIndices[ 0 ], 1, [ 0 ] )
1125                 
1126                 offsetLocationIndex     = indexTracker.allocIndex( 'OPENGLES2_LOCATION_INDEX' )
1127                 OpenGLES2.GetUniformLocation( offsetLocationIndex, program.programIndex, 'gOffset' )
1128
1129                 OpenGLES2.Viewport( 0, 0, WIDTH, HEIGHT );
1130                 OpenGLES2.ClearColor( [ 0.0, 0.0, 0.0, 1.0 ] )
1131
1132                 OpenGLES2.Disable( 'GL_DITHER' )
1133                 
1134                 # ------------------------------------------------------------
1135                 # Start benchmark actions
1136                 self.beginBenchmarkActions()
1137                 
1138                 OpenGLES2.Clear( [ 'GL_COLOR_BUFFER_BIT' ] )
1139
1140                 OpenGLES2.Uniformf( offsetLocationIndex, 1, [ 0 ] )
1141                 drawVertexData( modules, vertexDataSetup )
1142
1143                 OpenGLES2.CheckError()
1144                 Gltest.SaveTarga( -1, 0, 0, WIDTH, HEIGHT, target.outputPath() )               
1145
1146                 
1147 ######################################################################
1148 class FramebufferTexture2DDepthExt( vrt.VisualTestCase ):
1149
1150         ''' The purpose of the test: test using depth texture as framebuffer
1151         depth attachment. Requires GL_OES_depth_texture extension.
1152
1153         Expected output: surface divided in four rectangles, black in
1154         bottom-left, dark grey in bottom-right, light grey in top-left, and
1155         white in top-right. Viewport size 320*240.
1156         '''
1157
1158         testValues = { ( 'textureType' ) : [ ( 'OPENGLES2_DEPTH' ),
1159                                              ( 'OPENGLES2_DEPTH32' ),
1160                                              ]
1161                        }
1162         
1163         def __init__( self, textureType ):
1164                 vrt.VisualTestCase.__init__( self )
1165                 self.textureType = textureType
1166                 self.repeats     = 1
1167                 self.gridx       = GRIDX
1168                 self.gridy       = GRIDY
1169                 self.overdraw    = 1
1170                 self.name = "OPENGLES2 FramebufferTexture2D depth ext, texture type=%s" % ( textureTypeToStr( self.textureType ), )
1171         
1172         def build( self, target, modules ):       
1173                 OpenGLES2 = modules[ 'OpenGLES2' ]
1174                 Gltest    = modules[ 'Gltest' ]
1175
1176                 indexTracker = IndexTracker()
1177
1178                 vertexShaderSource = """
1179                     precision mediump float;
1180                     attribute vec4 gVertex;
1181                     attribute vec4 gTexCoord0;
1182                     varying vec4 gVSTexCoord0;
1183                     uniform float gOffset;
1184
1185                     void main()
1186                     {
1187                         gl_Position = vec4( gVertex.x, gVertex.y, gVertex.z + gOffset, gVertex.w );
1188                         gVSTexCoord0 = gTexCoord0;
1189                     }"""
1190
1191                 fragmentShaderSource = """
1192                     precision mediump float;
1193                     uniform sampler2D gTexture0;
1194                     varying vec4 gVSTexCoord0;
1195
1196                     void main()
1197                     {
1198                         gl_FragColor = texture2D( gTexture0, gVSTexCoord0.xy );
1199                     }"""
1200
1201                 vsSource = formatShader( vertexShaderSource )
1202                 fsSource = formatShader( fragmentShaderSource )
1203                 
1204                 # ------------------------------------------------------------
1205                 # Init actions
1206                 self.beginInitActions()
1207
1208                 defaultAttributes = target.getDefaultAttributes( VT_API_OPENGLES2 )
1209                 state             = target.setup( modules,
1210                                                   indexTracker,
1211                                                   WIDTH, HEIGHT,
1212                                                   [ VT_API_OPENGLES2 ],
1213                                                   defaultAttributes )
1214
1215                 OpenGLES2.CheckExtension( 'GL_OES_depth_texture' )
1216                 
1217                 framebufferIndex = indexTracker.allocIndex( 'OPENGLES2_FRAMEBUFFER_INDEX' )
1218                 OpenGLES2.GenFramebuffer( framebufferIndex )
1219                 OpenGLES2.BindFramebuffer( 'GL_FRAMEBUFFER',
1220                                            framebufferIndex )
1221
1222                 textureW = toPot( WIDTH, True )[ 0 ]
1223                 textureH = toPot( HEIGHT, True )[ 0 ]
1224                 
1225                 textureDataIndex = indexTracker.allocIndex( 'OPENGLES2_TEXTURE_DATA_INDEX' )
1226                 OpenGLES2.CreateNullTextureData( textureDataIndex,
1227                                                  textureW, textureH,
1228                                                  'OFF',
1229                                                  self.textureType )
1230                 
1231                 textureIndex = indexTracker.allocIndex( 'OPENGLES2_TEXTURE_INDEX' )
1232                 OpenGLES2.GenTexture( textureIndex )
1233                 OpenGLES2.BindTexture( 'GL_TEXTURE_2D',
1234                                        textureIndex )
1235                 OpenGLES2.TexImage2D( textureDataIndex,
1236                                       'GL_TEXTURE_2D' )
1237                                 
1238                 OpenGLES2.FramebufferTexture2D( 'GL_FRAMEBUFFER',
1239                                                 'GL_DEPTH_ATTACHMENT',
1240                                                 'GL_TEXTURE_2D',
1241                                                 textureIndex,
1242                                                 0 )
1243
1244                 OpenGLES2.CheckFramebufferStatus( 'GL_FRAMEBUFFER' )
1245                 OpenGLES2.Viewport( 0, 0, textureW, textureH );
1246                 
1247                 w = textureW / 2
1248                 h = textureH / 2
1249                 x = ( textureW - w ) / 2
1250                 y = ( textureW - h ) / 2 
1251
1252                 OpenGLES2.Enable( 'GL_SCISSOR_TEST' )                
1253                 OpenGLES2.Scissor( 0, 0, textureW / 2, textureH / 2 )
1254                 OpenGLES2.ClearDepth( 0.0 )
1255                 OpenGLES2.Clear( [ 'GL_DEPTH_BUFFER_BIT' ] )
1256
1257                 OpenGLES2.Scissor( textureW / 2, 0, textureW / 2, textureH / 2 )
1258                 OpenGLES2.ClearDepth( 0.33 )
1259                 OpenGLES2.Clear( [ 'GL_DEPTH_BUFFER_BIT' ] )
1260
1261                 OpenGLES2.Scissor( 0, textureH /2, textureW / 2, textureH / 2 )
1262                 OpenGLES2.ClearDepth( 0.66 )
1263                 OpenGLES2.Clear( [ 'GL_DEPTH_BUFFER_BIT' ] )
1264                 
1265                 OpenGLES2.Scissor( textureW / 2, textureH / 2, textureW / 2, textureH / 2 )
1266                 OpenGLES2.ClearDepth( 1.0 )
1267                 OpenGLES2.Clear( [ 'GL_DEPTH_BUFFER_BIT' ] )
1268
1269                 OpenGLES2.Disable( 'GL_SCISSOR_TEST' )
1270                 OpenGLES2.BindFramebuffer( 'GL_FRAMEBUFFER',
1271                                            -1 )
1272                 
1273                 mesh              = createPlane2( self.gridx, self.gridy, 1.0, 1.0, False, [ True ] )
1274                 
1275                 vertexDataSetup   = setupVertexData( modules, indexTracker, mesh )
1276                 program           = createProgram( modules, indexTracker, vsSource, fsSource, vertexDataSetup )
1277                 
1278                 useVertexData( modules, indexTracker, vertexDataSetup, program )
1279
1280                 OpenGLES2.ActiveTexture( 0 )
1281                 OpenGLES2.BindTexture( 'GL_TEXTURE_2D', textureIndex )
1282                 OpenGLES2.TexParameter( 'GL_TEXTURE_2D',
1283                                         'GL_LINEAR',
1284                                         'GL_LINEAR',
1285                                         'GL_CLAMP_TO_EDGE',
1286                                         'GL_CLAMP_TO_EDGE' )
1287                 
1288                 OpenGLES2.Uniformi( program.textureLocationIndices[ 0 ], 1, [ 0 ] )
1289                 
1290                 offsetLocationIndex     = indexTracker.allocIndex( 'OPENGLES2_LOCATION_INDEX' )
1291                 OpenGLES2.GetUniformLocation( offsetLocationIndex, program.programIndex, 'gOffset' )
1292
1293                 OpenGLES2.Viewport( 0, 0, WIDTH, HEIGHT );
1294                 OpenGLES2.ClearColor( [ 0.0, 0.0, 0.0, 1.0 ] )
1295
1296                 OpenGLES2.Disable( 'GL_DITHER' )
1297                 
1298                 # ------------------------------------------------------------
1299                 # Start benchmark actions
1300                 self.beginBenchmarkActions()
1301                 
1302                 OpenGLES2.Clear( [ 'GL_COLOR_BUFFER_BIT' ] )
1303
1304                 OpenGLES2.Uniformf( offsetLocationIndex, 1, [ 0 ] )
1305                 drawVertexData( modules, vertexDataSetup )
1306
1307                 OpenGLES2.CheckError()
1308                 Gltest.SaveTarga( -1, 0, 0, WIDTH, HEIGHT, target.outputPath() )               
1309
1310                 
1311 ######################################################################
1312 class FramebufferTexture2DDepthCubemapExt( vrt.VisualTestCase ):
1313
1314         ''' The purpose of the test: test using cubemap as framebuffer depth
1315         attachment. Requires GL_OES_depth_texture extension.
1316
1317         Expected output: a gray-shaded sphere on a gray background. Center tile
1318         should have the lightest gray, tile on the right should have black
1319         color. Top tile should have lighter gray than bottom tile. Viewport size
1320         320*240.
1321         '''
1322         testValues = { ( 'textureType' ) : [ ( 'OPENGLES2_DEPTH' ),
1323                                              ( 'OPENGLES2_DEPTH32' ),
1324                                              ]
1325                        }
1326         
1327         def __init__( self, textureType ):
1328                 vrt.VisualTestCase.__init__( self )
1329                 self.textureType = textureType
1330                 self.repeats     = 1
1331                 self.name = "OPENGLES2 FramebufferTexture2D cubemap depth ext, texture type=%s" % ( textureTypeToStr( self.textureType ), )
1332                 
1333         def build( self, target, modules ):       
1334                 OpenGLES2 = modules[ 'OpenGLES2' ]
1335                 Gltest    = modules[ 'Gltest' ]
1336
1337                 indexTracker = IndexTracker()
1338
1339                 vertexShaderSource = """
1340                     precision mediump float;
1341                     attribute vec4 gVertex;
1342                     attribute vec3 gNormal;
1343                     uniform mat4 gModelView;
1344                     uniform mat4 gProj;
1345                     varying vec3 gVSTexCoord;
1346                                                         
1347                     void main()
1348                     {
1349                         vec4 pos = gModelView * gVertex;
1350                         vec3 normal = normalize( gModelView * vec4( gNormal, 0.0 ) ).xyz;
1351                         gl_Position = gProj * pos;
1352                         vec3 view = -pos.xyz;
1353                         gVSTexCoord = reflect( -view, normal );
1354                         gVSTexCoord.y = -gVSTexCoord.y;
1355                     } """
1356                                 
1357                 fragmentShaderSource = """
1358                     precision mediump float;                                                       
1359                     uniform samplerCube gCubeMap0;
1360                     varying vec3 gVSTexCoord;
1361
1362                     void main()
1363                     {
1364                         gl_FragColor = textureCube( gCubeMap0, gVSTexCoord );
1365                     } """
1366
1367                 vsSource = formatShader( vertexShaderSource )
1368                 fsSource = formatShader( fragmentShaderSource )
1369                 
1370                 # ------------------------------------------------------------
1371                 # Init actions
1372                 self.beginInitActions()
1373
1374                 defaultAttributes = target.getDefaultAttributes( VT_API_OPENGLES2 )
1375                 state             = target.setup( modules,
1376                                                   indexTracker,
1377                                                   WIDTH, HEIGHT,
1378                                                   [ VT_API_OPENGLES2 ],
1379                                                   defaultAttributes )
1380
1381                 OpenGLES2.CheckExtension( 'GL_OES_depth_texture' )
1382                 
1383                 framebufferIndex = indexTracker.allocIndex( 'OPENGLES2_FRAMEBUFFER_INDEX' )
1384                 OpenGLES2.GenFramebuffer( framebufferIndex )
1385                 OpenGLES2.BindFramebuffer( 'GL_FRAMEBUFFER',
1386                                            framebufferIndex )
1387
1388                 depths = [ 0.0,     ## positive x
1389                            0.20,    ## negative x
1390                            0.40,    ## positive y
1391                            0.60,    ## negative y
1392                            0.80,    ## positive z
1393                            1.0      ## negative z, WHITE
1394                            ]
1395
1396                 textureW = 64
1397                 textureH = 64
1398                 textureSize = max( textureW, textureH )
1399                                 
1400                 textureIndex = indexTracker.allocIndex( 'OPENGLES2_TEXTURE_INDEX' )
1401                 OpenGLES2.GenTexture( textureIndex )            
1402                 OpenGLES2.BindTexture( 'GL_TEXTURE_CUBE_MAP', textureIndex )
1403
1404                 faces = [ 'GL_TEXTURE_CUBE_MAP_POSITIVE_X',
1405                           'GL_TEXTURE_CUBE_MAP_NEGATIVE_X',
1406                           'GL_TEXTURE_CUBE_MAP_POSITIVE_Y',
1407                           'GL_TEXTURE_CUBE_MAP_NEGATIVE_Y',
1408                           'GL_TEXTURE_CUBE_MAP_POSITIVE_Z',
1409                           'GL_TEXTURE_CUBE_MAP_NEGATIVE_Z' ]
1410
1411                 for i in range( 6 ):
1412                         textureDataIndex = indexTracker.allocIndex( 'OPENGLES2_TEXTURE_DATA_INDEX' )
1413                         OpenGLES2.CreateNullTextureData( textureDataIndex,
1414                                                          textureSize,
1415                                                          textureSize,
1416                                                          'OFF',
1417                                                          self.textureType )
1418              
1419                         OpenGLES2.TexImage2D( textureDataIndex, faces[ i ] );
1420                                                 
1421                 OpenGLES2.BindTexture( 'GL_TEXTURE_CUBE_MAP', -1 )
1422                         
1423                 for i in range( 6 ):
1424                         OpenGLES2.FramebufferTexture2D( 'GL_FRAMEBUFFER',
1425                                                         'GL_DEPTH_ATTACHMENT',
1426                                                         faces[ i ],
1427                                                         textureIndex,
1428                                                         0 )
1429
1430                         OpenGLES2.CheckFramebufferStatus( 'GL_FRAMEBUFFER' )
1431
1432                         OpenGLES2.ClearDepth( depths[ i ] )
1433                         OpenGLES2.Clear( [ 'GL_DEPTH_BUFFER_BIT' ] )
1434
1435                 OpenGLES2.BindFramebuffer( 'GL_FRAMEBUFFER',
1436                                            -1 )
1437                 
1438                 OpenGLES2.BindTexture( 'GL_TEXTURE_CUBE_MAP', textureIndex )                        
1439                 OpenGLES2.TexParameter( 'GL_TEXTURE_CUBE_MAP',
1440                                         'GL_LINEAR',
1441                                         'GL_LINEAR',
1442                                         'GL_CLAMP_TO_EDGE',
1443                                         'GL_CLAMP_TO_EDGE' )
1444                 
1445                 vertices = [ -10.000000,  10.000000,  10.000000,
1446                               10.000000,  10.000000,  10.000000,
1447                               10.000000, -10.000000,  10.000000,
1448                               -10.000000, -10.000000,  10.000000,
1449                               10.000000,  10.000000, -10.000000,
1450                               -10.000000,  10.000000, -10.000000,
1451                               -10.000000, -10.000000, -10.000000,
1452                               10.000000, -10.000000, -10.000000 ]
1453         
1454                 indices = [ 1, 4, 7, 7, 2, 1,  # positive x
1455                             5, 0, 3, 3, 6, 5,  # negative x
1456                             5, 4, 1, 1, 0, 5,
1457                             3, 2, 7, 7, 6, 3, 
1458                             0, 1, 2, 2, 3, 0,
1459                             4, 5, 6, 6, 7, 4 ]
1460
1461                 long = 32
1462                 lat  = 32
1463                 rad  = 1.0
1464
1465                 sphere      = Sphere( long, lat, rad )
1466                 objectSetup = setupObject( modules, indexTracker, sphere )
1467                 program     = createCubeProgram( modules, indexTracker, vsSource, fsSource, objectSetup )
1468                 useObject( modules, indexTracker, objectSetup, program )
1469
1470                 modelviewLocationIndex = indexTracker.allocIndex( 'OPENGLES2_LOCATION_INDEX' )
1471                 OpenGLES2.GetUniformLocation( modelviewLocationIndex, program[ 0 ], "gModelView" )
1472                 matMV = mvMatrix( 0 )
1473                 OpenGLES2.UniformMatrix( modelviewLocationIndex, 1, matMV )
1474                
1475                 projectionLocationIndex = indexTracker.allocIndex( 'OPENGLES2_LOCATION_INDEX' )
1476                 OpenGLES2.GetUniformLocation( projectionLocationIndex, program[ 0 ], "gProj" )
1477                 matProj = pMatrix( WIDTH / HEIGHT )
1478                 OpenGLES2.UniformMatrix( projectionLocationIndex, 1, matProj )                
1479                 
1480                 OpenGLES2.ClearColor( [ 0.5, 0.5, 0.5, 1.0 ] )
1481         
1482                 OpenGLES2.Enable( 'GL_DEPTH_TEST')
1483                 OpenGLES2.Enable( 'GL_CULL_FACE' )
1484                 OpenGLES2.CullFace( 'GL_BACK' )
1485                 OpenGLES2.Viewport( 0, 0, WIDTH, HEIGHT );
1486
1487                 OpenGLES2.Disable( 'GL_DITHER' )
1488                 
1489                 # ------------------------------------------------------------
1490                 # Start benchmark actions
1491                 self.beginBenchmarkActions()
1492         
1493                 OpenGLES2.Clear( [ 'GL_COLOR_BUFFER_BIT', 'GL_DEPTH_BUFFER_BIT' ] )
1494
1495                 drawObject( modules, objectSetup )                
1496
1497                 OpenGLES2.CheckError()                
1498                 Gltest.SaveTarga( -1, 0, 0, WIDTH, HEIGHT, target.outputPath() )               
1499
1500                 
1501 ######################################################################
1502 def mvMatrix( angle ):
1503         RAD = 360.0 / ( 2.0 * math.pi )
1504         angle = angle /  RAD
1505         mv       = [ 0 ] * 16
1506         mv[ 0 ]  = +math.cos( angle )
1507         mv[ 2 ]  = +math.sin( angle )
1508         mv[ 5 ]  = 1.0
1509         mv[ 8 ]  = -math.sin( angle )
1510         mv[ 10 ] = +math.cos( angle )
1511         mv[ 14 ] = -5.0
1512         mv[ 15 ] = 1.0
1513
1514         return mv
1515
1516 ######################################################################
1517 def pMatrix( aspectRatio ):
1518         mp       = [ 0 ] * 16
1519         mp[ 0 ]  = math.cos( 0.25 ) / math.sin( 0.25 )
1520         mp[ 5 ]  = mp[ 0 ] * aspectRatio
1521         mp[ 10 ] = - 10.0 / 9.0
1522         mp[ 11 ] = - 1.0
1523         mp[ 14 ] = - 10.0 / 9.0
1524     
1525         return mp
1526
1527 ######################################################################
1528 class FramebufferTexture2DColorCubemap( vrt.VisualTestCase ):
1529
1530         ''' The purpose of the test: test using cubemap as framebuffer color
1531         attachment.
1532
1533         Expected output: a multicolored sphere on a gray background. With RGB
1534         textures, the sphere colors should be center=magenta, left=green,
1535         up=yellow, right=red, down=blue, border=white. Viewport size 320*240.
1536         '''
1537         testValues = { ( 'textureType' ) : [ ( 'OPENGLES2_RGB565' ),
1538                                              ( 'OPENGLES2_RGB888' ),
1539                                              ( 'OPENGLES2_RGBA4444' ),
1540                                              ( 'OPENGLES2_RGBA5551' ),
1541                                              ( 'OPENGLES2_RGBA8888' ),
1542                                              ]
1543                        }
1544         
1545         def __init__( self, textureType ):
1546                 vrt.VisualTestCase.__init__( self )
1547                 self.textureType = textureType
1548                 self.repeats     = 1
1549                 self.name = "OPENGLES2 FramebufferTexture2D cubemap color, texture type=%s" % ( textureTypeToStr( self.textureType ), )
1550                 
1551         def build( self, target, modules ):       
1552                 OpenGLES2 = modules[ 'OpenGLES2' ]
1553                 Gltest    = modules[ 'Gltest' ]
1554
1555                 indexTracker = IndexTracker()
1556
1557                 vertexShaderSource = """
1558                     precision mediump float;
1559                     attribute vec4 gVertex;
1560                     attribute vec3 gNormal;
1561                     uniform mat4 gModelView;
1562                     uniform mat4 gProj;
1563                     varying vec3 gVSTexCoord;
1564                                                         
1565                     void main()
1566                     {
1567                         vec4 pos = gModelView * gVertex;
1568                         vec3 normal = normalize( gModelView * vec4( gNormal, 0.0 ) ).xyz;
1569                         gl_Position = gProj * pos;
1570                         vec3 view = -pos.xyz;
1571                         gVSTexCoord = reflect( -view, normal );
1572                         gVSTexCoord.y = -gVSTexCoord.y;
1573                     } """
1574                                 
1575                 fragmentShaderSource = """
1576                     precision mediump float;                                                       
1577                     uniform samplerCube gCubeMap0;
1578                     varying vec3 gVSTexCoord;
1579
1580                     void main()
1581                     {
1582                         gl_FragColor = textureCube( gCubeMap0, gVSTexCoord );
1583                     } """
1584
1585                 vsSource = formatShader( vertexShaderSource )
1586                 fsSource = formatShader( fragmentShaderSource )
1587                 
1588                 # ------------------------------------------------------------
1589                 # Init actions
1590                 self.beginInitActions()
1591
1592                 defaultAttributes = target.getDefaultAttributes( VT_API_OPENGLES2 )
1593                 state             = target.setup( modules,
1594                                                   indexTracker,
1595                                                   WIDTH, HEIGHT,
1596                                                   [ VT_API_OPENGLES2 ],
1597                                                   defaultAttributes )
1598
1599                 framebufferIndex = indexTracker.allocIndex( 'OPENGLES2_FRAMEBUFFER_INDEX' )
1600                 OpenGLES2.GenFramebuffer( framebufferIndex )
1601                 OpenGLES2.BindFramebuffer( 'GL_FRAMEBUFFER',
1602                                            framebufferIndex )
1603
1604                 colors = [ [ 1.0, 0.0, 0.0, 1 ],  ## positive x, RED
1605                            [ 0.0, 1.0, 0.0, 1 ],  ## negative x, GREEN
1606                            [ 0.0, 0.0, 1.0, 1 ],  ## positive y, BLUE
1607                            [ 1.0, 1.0, 0.0, 1 ],  ## negative y, CYAN
1608                            [ 1.0, 0.0, 1.0, 1 ],  ## positive z, MAGENDA
1609                            [ 1.0, 1.0, 1.0, 1 ] ] ## negative z, WHITE
1610
1611                 textureW = 64
1612                 textureH = 64
1613                 textureSize = max( textureW, textureH )
1614                                 
1615                 textureIndex = indexTracker.allocIndex( 'OPENGLES2_TEXTURE_INDEX' )
1616                 OpenGLES2.GenTexture( textureIndex )            
1617                 OpenGLES2.BindTexture( 'GL_TEXTURE_CUBE_MAP', textureIndex )
1618
1619                 faces = [ 'GL_TEXTURE_CUBE_MAP_POSITIVE_X',
1620                           'GL_TEXTURE_CUBE_MAP_NEGATIVE_X',
1621                           'GL_TEXTURE_CUBE_MAP_POSITIVE_Y',
1622                           'GL_TEXTURE_CUBE_MAP_NEGATIVE_Y',
1623                           'GL_TEXTURE_CUBE_MAP_POSITIVE_Z',
1624                           'GL_TEXTURE_CUBE_MAP_NEGATIVE_Z' ]
1625
1626                 for i in range( 6 ):
1627                         textureDataIndex = indexTracker.allocIndex( 'OPENGLES2_TEXTURE_DATA_INDEX' )
1628                         OpenGLES2.CreateNullTextureData( textureDataIndex,
1629                                                          textureSize,
1630                                                          textureSize,
1631                                                          'OFF',
1632                                                          self.textureType )
1633              
1634                         OpenGLES2.TexImage2D( textureDataIndex, faces[ i ] );
1635                         
1636                 OpenGLES2.BindTexture( 'GL_TEXTURE_CUBE_MAP', -1 )
1637                         
1638                 for i in range( 6 ):
1639                         OpenGLES2.FramebufferTexture2D( 'GL_FRAMEBUFFER',
1640                                                         'GL_COLOR_ATTACHMENT0',
1641                                                         faces[ i ],
1642                                                         textureIndex,
1643                                                         0 )
1644
1645                         OpenGLES2.CheckFramebufferStatus( 'GL_FRAMEBUFFER' )
1646                         OpenGLES2.Viewport( 0, 0, textureSize, textureSize );
1647                         
1648                         OpenGLES2.ClearColor( colors[ i ] )
1649                         OpenGLES2.Clear( [ 'GL_COLOR_BUFFER_BIT' ] )
1650
1651                 OpenGLES2.BindFramebuffer( 'GL_FRAMEBUFFER',
1652                                            -1 )
1653                 
1654                 OpenGLES2.BindTexture( 'GL_TEXTURE_CUBE_MAP', textureIndex )                        
1655                 OpenGLES2.TexParameter( 'GL_TEXTURE_CUBE_MAP',
1656                                         'GL_LINEAR',
1657                                         'GL_LINEAR',
1658                                         'GL_CLAMP_TO_EDGE',
1659                                         'GL_CLAMP_TO_EDGE' )
1660                 
1661                 vertices = [ -10.000000,  10.000000,  10.000000,
1662                               10.000000,  10.000000,  10.000000,
1663                               10.000000, -10.000000,  10.000000,
1664                               -10.000000, -10.000000,  10.000000,
1665                               10.000000,  10.000000, -10.000000,
1666                               -10.000000,  10.000000, -10.000000,
1667                               -10.000000, -10.000000, -10.000000,
1668                               10.000000, -10.000000, -10.000000 ]
1669         
1670                 indices = [ 1, 4, 7, 7, 2, 1,  # positive x
1671                             5, 0, 3, 3, 6, 5,  # negative x
1672                             5, 4, 1, 1, 0, 5,
1673                             3, 2, 7, 7, 6, 3, 
1674                             0, 1, 2, 2, 3, 0,
1675                             4, 5, 6, 6, 7, 4 ]
1676
1677                 long = 32
1678                 lat  = 32
1679                 rad  = 1.0
1680
1681                 sphere      = Sphere( long, lat, rad )
1682                 objectSetup = setupObject( modules, indexTracker, sphere )
1683                 program     = createCubeProgram( modules, indexTracker, vsSource, fsSource, objectSetup )
1684                 useObject( modules, indexTracker, objectSetup, program )
1685
1686                 modelviewLocationIndex = indexTracker.allocIndex( 'OPENGLES2_LOCATION_INDEX' )
1687                 OpenGLES2.GetUniformLocation( modelviewLocationIndex, program[ 0 ], "gModelView" )
1688                 matMV = mvMatrix( 0 )
1689                 OpenGLES2.UniformMatrix( modelviewLocationIndex, 1, matMV )
1690                
1691                 projectionLocationIndex = indexTracker.allocIndex( 'OPENGLES2_LOCATION_INDEX' )
1692                 OpenGLES2.GetUniformLocation( projectionLocationIndex, program[ 0 ], "gProj" )
1693                 matProj = pMatrix( WIDTH / HEIGHT )
1694                 OpenGLES2.UniformMatrix( projectionLocationIndex, 1, matProj )                
1695                 
1696                 OpenGLES2.ClearColor( [ 0.5, 0.5, 0.5, 1.0 ] )
1697         
1698                 OpenGLES2.Enable( 'GL_DEPTH_TEST')
1699                 OpenGLES2.Enable( 'GL_CULL_FACE' )
1700                 OpenGLES2.CullFace( 'GL_BACK' )
1701                 OpenGLES2.Viewport( 0, 0, WIDTH, HEIGHT );
1702                 
1703                 OpenGLES2.Disable( 'GL_DITHER' )
1704                                 
1705                 # ------------------------------------------------------------
1706                 # Start benchmark actions
1707                 self.beginBenchmarkActions()
1708         
1709                 OpenGLES2.Clear( [ 'GL_COLOR_BUFFER_BIT', 'GL_DEPTH_BUFFER_BIT' ] )
1710
1711                 drawObject( modules, objectSetup )                
1712
1713                 OpenGLES2.CheckError()                
1714                 Gltest.SaveTarga( -1, 0, 0, WIDTH, HEIGHT, target.outputPath() )
1715
1716 ######################################################################
1717 class FramebufferTexture2DColorCubemapNpot( vrt.VisualTestCase ):
1718
1719         ''' The purpose of the test: test using NPOT cubemap as framebuffer
1720         color attachment.
1721
1722         Expected output: a multicolored sphere on a gray background. With RGB
1723         textures, the sphere colors should be center=magenta, left=green,
1724         up=yellow, right=red, down=blue, border=white. Viewport size 320*240.
1725         '''
1726         testValues = { ( 'textureType' ) : [ ( 'OPENGLES2_RGB565' ),
1727                                              ( 'OPENGLES2_RGB888' ),
1728                                              ( 'OPENGLES2_RGBA4444' ),
1729                                              ( 'OPENGLES2_RGBA5551' ),
1730                                              ( 'OPENGLES2_RGBA8888' ),
1731                                              ]
1732                        }
1733         
1734         def __init__( self, textureType ):
1735                 vrt.VisualTestCase.__init__( self )
1736                 self.textureType = textureType
1737                 self.repeats     = 1
1738                 self.name = "OPENGLES2 NPOT FramebufferTexture2D cubemap color, texture type=%s" % ( textureTypeToStr( self.textureType ), )
1739                 
1740         def build( self, target, modules ):       
1741                 OpenGLES2 = modules[ 'OpenGLES2' ]
1742                 Gltest    = modules[ 'Gltest' ]
1743
1744                 indexTracker = IndexTracker()
1745
1746                 vertexShaderSource = """
1747                     precision mediump float;
1748                     attribute vec4 gVertex;
1749                     attribute vec3 gNormal;
1750                     uniform mat4 gModelView;
1751                     uniform mat4 gProj;
1752                     varying vec3 gVSTexCoord;
1753                                                         
1754                     void main()
1755                     {
1756                         vec4 pos = gModelView * gVertex;
1757                         vec3 normal = normalize( gModelView * vec4( gNormal, 0.0 ) ).xyz;
1758                         gl_Position = gProj * pos;
1759                         vec3 view = -pos.xyz;
1760                         gVSTexCoord = reflect( -view, normal );
1761                         gVSTexCoord.y = -gVSTexCoord.y;
1762                     } """
1763                                 
1764                 fragmentShaderSource = """
1765                     precision mediump float;                                                       
1766                     uniform samplerCube gCubeMap0;
1767                     varying vec3 gVSTexCoord;
1768
1769                     void main()
1770                     {
1771                         gl_FragColor = textureCube( gCubeMap0, gVSTexCoord );
1772                     } """
1773
1774                 vsSource = formatShader( vertexShaderSource )
1775                 fsSource = formatShader( fragmentShaderSource )
1776                 
1777                 # ------------------------------------------------------------
1778                 # Init actions
1779                 self.beginInitActions()
1780
1781                 defaultAttributes = target.getDefaultAttributes( VT_API_OPENGLES2 )
1782                 state             = target.setup( modules,
1783                                                   indexTracker,
1784                                                   WIDTH, HEIGHT,
1785                                                   [ VT_API_OPENGLES2 ],
1786                                                   defaultAttributes )
1787
1788                 framebufferIndex = indexTracker.allocIndex( 'OPENGLES2_FRAMEBUFFER_INDEX' )
1789                 OpenGLES2.GenFramebuffer( framebufferIndex )
1790                 OpenGLES2.BindFramebuffer( 'GL_FRAMEBUFFER',
1791                                            framebufferIndex )
1792
1793                 colors = [ [ 1.0, 0.0, 0.0, 1 ],  ## positive x, RED
1794                            [ 0.0, 1.0, 0.0, 1 ],  ## negative x, GREEN
1795                            [ 0.0, 0.0, 1.0, 1 ],  ## positive y, BLUE
1796                            [ 1.0, 1.0, 0.0, 1 ],  ## negative y, CYAN
1797                            [ 1.0, 0.0, 1.0, 1 ],  ## positive z, MAGENDA
1798                            [ 1.0, 1.0, 1.0, 1 ] ] ## negative z, WHITE
1799
1800                 textureW = 64 - 1
1801                 textureH = 64 - 1
1802                 textureSize = max( textureW, textureH )
1803                                 
1804                 textureIndex = indexTracker.allocIndex( 'OPENGLES2_TEXTURE_INDEX' )
1805                 OpenGLES2.GenTexture( textureIndex )            
1806                 OpenGLES2.BindTexture( 'GL_TEXTURE_CUBE_MAP', textureIndex )
1807
1808                 faces = [ 'GL_TEXTURE_CUBE_MAP_POSITIVE_X',
1809                           'GL_TEXTURE_CUBE_MAP_NEGATIVE_X',
1810                           'GL_TEXTURE_CUBE_MAP_POSITIVE_Y',
1811                           'GL_TEXTURE_CUBE_MAP_NEGATIVE_Y',
1812                           'GL_TEXTURE_CUBE_MAP_POSITIVE_Z',
1813                           'GL_TEXTURE_CUBE_MAP_NEGATIVE_Z' ]
1814
1815                 for i in range( 6 ):
1816                         textureDataIndex = indexTracker.allocIndex( 'OPENGLES2_TEXTURE_DATA_INDEX' )
1817                         OpenGLES2.CreateNullTextureData( textureDataIndex,
1818                                                          textureSize,
1819                                                          textureSize,
1820                                                          'OFF',
1821                                                          self.textureType )
1822              
1823                         OpenGLES2.TexImage2D( textureDataIndex, faces[ i ] );
1824                                                 
1825                 OpenGLES2.BindTexture( 'GL_TEXTURE_CUBE_MAP', -1 )
1826                         
1827                 for i in range( 6 ):
1828                         OpenGLES2.FramebufferTexture2D( 'GL_FRAMEBUFFER',
1829                                                         'GL_COLOR_ATTACHMENT0',
1830                                                         faces[ i ],
1831                                                         textureIndex,
1832                                                         0 )
1833
1834                         OpenGLES2.CheckFramebufferStatus( 'GL_FRAMEBUFFER' )
1835
1836                         OpenGLES2.ClearColor( colors[ i ] )
1837                         OpenGLES2.Clear( [ 'GL_COLOR_BUFFER_BIT' ] )
1838
1839                 OpenGLES2.BindFramebuffer( 'GL_FRAMEBUFFER',
1840                                            -1 )
1841                 
1842                 OpenGLES2.BindTexture( 'GL_TEXTURE_CUBE_MAP', textureIndex )                        
1843                 OpenGLES2.TexParameter( 'GL_TEXTURE_CUBE_MAP',
1844                                         'GL_LINEAR',
1845                                         'GL_LINEAR',
1846                                         'GL_CLAMP_TO_EDGE',
1847                                         'GL_CLAMP_TO_EDGE' )
1848                 
1849                 vertices = [ -10.000000,  10.000000,  10.000000,
1850                               10.000000,  10.000000,  10.000000,
1851                               10.000000, -10.000000,  10.000000,
1852                               -10.000000, -10.000000,  10.000000,
1853                               10.000000,  10.000000, -10.000000,
1854                               -10.000000,  10.000000, -10.000000,
1855                               -10.000000, -10.000000, -10.000000,
1856                               10.000000, -10.000000, -10.000000 ]
1857         
1858                 indices = [ 1, 4, 7, 7, 2, 1,  # positive x
1859                             5, 0, 3, 3, 6, 5,  # negative x
1860                             5, 4, 1, 1, 0, 5,
1861                             3, 2, 7, 7, 6, 3, 
1862                             0, 1, 2, 2, 3, 0,
1863                             4, 5, 6, 6, 7, 4 ]
1864
1865                 long = 32
1866                 lat  = 32
1867                 rad  = 1.0
1868
1869                 sphere      = Sphere( long, lat, rad )
1870                 objectSetup = setupObject( modules, indexTracker, sphere )
1871                 program     = createCubeProgram( modules, indexTracker, vsSource, fsSource, objectSetup )
1872                 useObject( modules, indexTracker, objectSetup, program )
1873
1874                 modelviewLocationIndex = indexTracker.allocIndex( 'OPENGLES2_LOCATION_INDEX' )
1875                 OpenGLES2.GetUniformLocation( modelviewLocationIndex, program[ 0 ], "gModelView" )
1876                 matMV = mvMatrix( 0 )
1877                 OpenGLES2.UniformMatrix( modelviewLocationIndex, 1, matMV )
1878                
1879                 projectionLocationIndex = indexTracker.allocIndex( 'OPENGLES2_LOCATION_INDEX' )
1880                 OpenGLES2.GetUniformLocation( projectionLocationIndex, program[ 0 ], "gProj" )
1881                 matProj = pMatrix( WIDTH / HEIGHT )
1882                 OpenGLES2.UniformMatrix( projectionLocationIndex, 1, matProj )                
1883                 
1884                 OpenGLES2.ClearColor( [ 0.5, 0.5, 0.5, 1.0 ] )
1885         
1886                 OpenGLES2.Enable( 'GL_DEPTH_TEST')
1887                 OpenGLES2.Enable( 'GL_CULL_FACE' )
1888                 OpenGLES2.CullFace( 'GL_BACK' )
1889                 OpenGLES2.Viewport( 0, 0, WIDTH, HEIGHT );
1890                 
1891                 OpenGLES2.Disable( 'GL_DITHER' )
1892                 
1893                 # ------------------------------------------------------------
1894                 # Start benchmark actions
1895                 self.beginBenchmarkActions()
1896         
1897                 OpenGLES2.Clear( [ 'GL_COLOR_BUFFER_BIT', 'GL_DEPTH_BUFFER_BIT' ] )
1898
1899                 drawObject( modules, objectSetup )                
1900
1901                 OpenGLES2.CheckError()                
1902                 Gltest.SaveTarga( -1, 0, 0, WIDTH, HEIGHT, target.outputPath() )
1903
1904 ######################################################################
1905 class FramebufferTexture2DColorCubemapNpotExt( vrt.VisualTestCase ):
1906
1907         ''' The purpose of the test: test using NPOT cubemap as framebuffer
1908         color attachment. Requires GL_OES_texture_npot extension.
1909
1910         Expected output: a multicolored sphere on a gray background. With RGB
1911         textures, the sphere colors should be center=magenta, left=green,
1912         up=yellow, right=red, down=blue, border=white. Viewport size 320*240.
1913         '''
1914         testValues = { ( 'textureType' ) : [ ( 'OPENGLES2_RGB565' ),
1915                                              ( 'OPENGLES2_RGB888' ),
1916                                              ( 'OPENGLES2_RGBA4444' ),
1917                                              ( 'OPENGLES2_RGBA5551' ),
1918                                              ( 'OPENGLES2_RGBA8888' ),
1919                                              ]
1920                        }
1921         
1922         def __init__( self, textureType ):
1923                 vrt.VisualTestCase.__init__( self )
1924                 self.textureType = textureType
1925                 self.repeats     = 1
1926                 self.name = "OPENGLES2 NPOT FramebufferTexture2D cubemap color ext, texture type=%s" % ( textureTypeToStr( self.textureType ), )
1927                 
1928         def build( self, target, modules ):       
1929                 OpenGLES2 = modules[ 'OpenGLES2' ]
1930                 Gltest    = modules[ 'Gltest' ]
1931
1932                 indexTracker = IndexTracker()
1933
1934                 vertexShaderSource = """
1935                     precision mediump float;
1936                     attribute vec4 gVertex;
1937                     attribute vec3 gNormal;
1938                     uniform mat4 gModelView;
1939                     uniform mat4 gProj;
1940                     varying vec3 gVSTexCoord;
1941                                                         
1942                     void main()
1943                     {
1944                         vec4 pos = gModelView * gVertex;
1945                         vec3 normal = normalize( gModelView * vec4( gNormal, 0.0 ) ).xyz;
1946                         gl_Position = gProj * pos;
1947                         vec3 view = -pos.xyz;
1948                         gVSTexCoord = reflect( -view, normal );
1949                         gVSTexCoord.y = -gVSTexCoord.y;
1950                     } """
1951                                 
1952                 fragmentShaderSource = """
1953                     precision mediump float;                                                       
1954                     uniform samplerCube gCubeMap0;
1955                     varying vec3 gVSTexCoord;
1956
1957                     void main()
1958                     {
1959                         gl_FragColor = textureCube( gCubeMap0, gVSTexCoord );
1960                     } """
1961
1962                 vsSource = formatShader( vertexShaderSource )
1963                 fsSource = formatShader( fragmentShaderSource )
1964                 
1965                 # ------------------------------------------------------------
1966                 # Init actions
1967                 self.beginInitActions()
1968
1969                 defaultAttributes = target.getDefaultAttributes( VT_API_OPENGLES2 )
1970                 state             = target.setup( modules,
1971                                                   indexTracker,
1972                                                   WIDTH, HEIGHT,
1973                                                   [ VT_API_OPENGLES2 ],
1974                                                   defaultAttributes )
1975
1976                 OpenGLES2.CheckExtension( 'GL_OES_texture_npot' )
1977                 
1978                 framebufferIndex = indexTracker.allocIndex( 'OPENGLES2_FRAMEBUFFER_INDEX' )
1979                 OpenGLES2.GenFramebuffer( framebufferIndex )
1980                 OpenGLES2.BindFramebuffer( 'GL_FRAMEBUFFER',
1981                                            framebufferIndex )
1982
1983                 colors = [ [ 1.0, 0.0, 0.0, 1 ],  ## positive x, RED
1984                            [ 0.0, 1.0, 0.0, 1 ],  ## negative x, GREEN
1985                            [ 0.0, 0.0, 1.0, 1 ],  ## positive y, BLUE
1986                            [ 1.0, 1.0, 0.0, 1 ],  ## negative y, CYAN
1987                            [ 1.0, 0.0, 1.0, 1 ],  ## positive z, MAGENDA
1988                            [ 1.0, 1.0, 1.0, 1 ] ] ## negative z, WHITE
1989
1990                 textureW = 64 - 1
1991                 textureH = 64 - 1
1992                 textureSize = max( textureW, textureH )
1993                                 
1994                 textureIndex = indexTracker.allocIndex( 'OPENGLES2_TEXTURE_INDEX' )
1995                 OpenGLES2.GenTexture( textureIndex )            
1996                 OpenGLES2.BindTexture( 'GL_TEXTURE_CUBE_MAP', textureIndex )
1997
1998                 faces = [ 'GL_TEXTURE_CUBE_MAP_POSITIVE_X',
1999                           'GL_TEXTURE_CUBE_MAP_NEGATIVE_X',
2000                           'GL_TEXTURE_CUBE_MAP_POSITIVE_Y',
2001                           'GL_TEXTURE_CUBE_MAP_NEGATIVE_Y',
2002                           'GL_TEXTURE_CUBE_MAP_POSITIVE_Z',
2003                           'GL_TEXTURE_CUBE_MAP_NEGATIVE_Z' ]
2004
2005                 for i in range( 6 ):
2006                         textureDataIndex = indexTracker.allocIndex( 'OPENGLES2_TEXTURE_DATA_INDEX' )
2007                         OpenGLES2.CreateNullTextureData( textureDataIndex,
2008                                                          textureSize,
2009                                                          textureSize,
2010                                                          'OFF',
2011                                                          self.textureType )
2012              
2013                         OpenGLES2.TexImage2D( textureDataIndex, faces[ i ] );
2014                                                 
2015                 OpenGLES2.BindTexture( 'GL_TEXTURE_CUBE_MAP', -1 )
2016                         
2017                 for i in range( 6 ):
2018                         OpenGLES2.FramebufferTexture2D( 'GL_FRAMEBUFFER',
2019                                                         'GL_COLOR_ATTACHMENT0',
2020                                                         faces[ i ],
2021                                                         textureIndex,
2022                                                         0 )
2023
2024                         OpenGLES2.CheckFramebufferStatus( 'GL_FRAMEBUFFER' )
2025
2026                         OpenGLES2.ClearColor( colors[ i ] )
2027                         OpenGLES2.Clear( [ 'GL_COLOR_BUFFER_BIT' ] )
2028
2029                 OpenGLES2.BindFramebuffer( 'GL_FRAMEBUFFER',
2030                                            -1 )
2031                 
2032                 OpenGLES2.BindTexture( 'GL_TEXTURE_CUBE_MAP', textureIndex )                        
2033                 OpenGLES2.TexParameter( 'GL_TEXTURE_CUBE_MAP',
2034                                         'GL_LINEAR',
2035                                         'GL_LINEAR',
2036                                         'GL_MIRRORED_REPEAT',
2037                                         'GL_MIRRORED_REPEAT' )
2038                 
2039                 vertices = [ -10.000000,  10.000000,  10.000000,
2040                               10.000000,  10.000000,  10.000000,
2041                               10.000000, -10.000000,  10.000000,
2042                               -10.000000, -10.000000,  10.000000,
2043                               10.000000,  10.000000, -10.000000,
2044                               -10.000000,  10.000000, -10.000000,
2045                               -10.000000, -10.000000, -10.000000,
2046                               10.000000, -10.000000, -10.000000 ]
2047         
2048                 indices = [ 1, 4, 7, 7, 2, 1,  # positive x
2049                             5, 0, 3, 3, 6, 5,  # negative x
2050                             5, 4, 1, 1, 0, 5,
2051                             3, 2, 7, 7, 6, 3, 
2052                             0, 1, 2, 2, 3, 0,
2053                             4, 5, 6, 6, 7, 4 ]
2054
2055                 long = 32
2056                 lat  = 32
2057                 rad  = 1.0
2058
2059                 sphere      = Sphere( long, lat, rad )
2060                 objectSetup = setupObject( modules, indexTracker, sphere )
2061                 program     = createCubeProgram( modules, indexTracker, vsSource, fsSource, objectSetup )
2062                 useObject( modules, indexTracker, objectSetup, program )
2063
2064                 modelviewLocationIndex = indexTracker.allocIndex( 'OPENGLES2_LOCATION_INDEX' )
2065                 OpenGLES2.GetUniformLocation( modelviewLocationIndex, program[ 0 ], "gModelView" )
2066                 matMV = mvMatrix( 0 )
2067                 OpenGLES2.UniformMatrix( modelviewLocationIndex, 1, matMV )
2068                
2069                 projectionLocationIndex = indexTracker.allocIndex( 'OPENGLES2_LOCATION_INDEX' )
2070                 OpenGLES2.GetUniformLocation( projectionLocationIndex, program[ 0 ], "gProj" )
2071                 matProj = pMatrix( WIDTH / HEIGHT )
2072                 OpenGLES2.UniformMatrix( projectionLocationIndex, 1, matProj )                
2073                 
2074                 OpenGLES2.ClearColor( [ 0.5, 0.5, 0.5, 1.0 ] )
2075         
2076                 OpenGLES2.Enable( 'GL_DEPTH_TEST')
2077                 OpenGLES2.Enable( 'GL_CULL_FACE' )
2078                 OpenGLES2.CullFace( 'GL_BACK' )
2079                 OpenGLES2.Viewport( 0, 0, WIDTH, HEIGHT );
2080                 
2081                 OpenGLES2.Disable( 'GL_DITHER' )
2082                 
2083                 # ------------------------------------------------------------
2084                 # Start benchmark actions
2085                 self.beginBenchmarkActions()
2086         
2087                 OpenGLES2.Clear( [ 'GL_COLOR_BUFFER_BIT', 'GL_DEPTH_BUFFER_BIT' ] )
2088
2089                 drawObject( modules, objectSetup )                
2090
2091                 OpenGLES2.CheckError()                
2092                 Gltest.SaveTarga( -1, 0, 0, WIDTH, HEIGHT, target.outputPath() )
2093                
2094
2095 ######################################################################
2096 class TwoFramebuffers( vrt.VisualTestCase ):
2097
2098         ''' The purpose of the test: test using multiple framebuffers.
2099
2100         Expected output: green surface. Viewport size 320*240.
2101         '''
2102         
2103         def __init__( self ):
2104                 vrt.VisualTestCase.__init__( self )
2105                 self.repeats       = 1
2106                 self.color0Format  = 'GL_RGB565'
2107                 self.gridx         = GRIDX
2108                 self.gridy         = GRIDY
2109                 self.overdraw      = 1
2110                 self.name = "OPENGLES2 two framebuffers"
2111         
2112         def build( self, target, modules ):       
2113                 OpenGLES2 = modules[ 'OpenGLES2' ]
2114                 Gltest    = modules[ 'Gltest' ]
2115
2116                 indexTracker = IndexTracker()
2117                 
2118                 vertexShaderSource = """
2119                     precision mediump float;
2120                     attribute vec4 gVertex;
2121                     attribute vec4 gColor;
2122                     varying vec4 gVSColor;
2123                     uniform float gOffset;
2124
2125                     void main()
2126                     {
2127                         gl_Position = vec4( gVertex.x, gVertex.y, gVertex.z + gOffset, gVertex.w );
2128                         gVSColor = gColor;
2129                     }"""
2130
2131                 fragmentShaderSource = """
2132                     precision mediump float;
2133                     varying vec4 gVSColor;
2134
2135                     void main()
2136                     {
2137                         gl_FragColor = gVSColor;
2138                     }"""
2139
2140                 vsSource = formatShader( vertexShaderSource )
2141                 fsSource = formatShader( fragmentShaderSource )
2142                 
2143                 # ------------------------------------------------------------
2144                 # Init actions
2145                 self.beginInitActions()
2146
2147                 defaultAttributes = target.getDefaultAttributes( VT_API_OPENGLES2 )
2148                 state = target.setup( modules,
2149                                       indexTracker,
2150                                       1, 1,
2151                                       [ VT_API_OPENGLES2 ],
2152                                       defaultAttributes )
2153
2154                 framebuffer1Index = indexTracker.allocIndex( 'OPENGLES2_FRAMEBUFFER_INDEX' )
2155                 OpenGLES2.GenFramebuffer( framebuffer1Index )
2156                 OpenGLES2.BindFramebuffer( 'GL_FRAMEBUFFER',
2157                                            framebuffer1Index )
2158
2159                 color0Renderbuffer1Index = indexTracker.allocIndex( 'OPENGLES2_RENDERBUFFER_INDEX' )
2160                 OpenGLES2.GenRenderbuffer( color0Renderbuffer1Index )
2161                 OpenGLES2.BindRenderbuffer( 'GL_RENDERBUFFER',
2162                                             color0Renderbuffer1Index )
2163                 OpenGLES2.RenderbufferStorage( 'GL_RENDERBUFFER',
2164                                                self.color0Format,
2165                                                WIDTH, HEIGHT )
2166                 
2167                 OpenGLES2.FramebufferRenderbuffer( 'GL_FRAMEBUFFER',
2168                                                    'GL_COLOR_ATTACHMENT0',
2169                                                    'GL_RENDERBUFFER',
2170                                                    color0Renderbuffer1Index )
2171
2172                 OpenGLES2.CheckFramebufferStatus( 'GL_FRAMEBUFFER' )
2173
2174                 framebuffer2Index = indexTracker.allocIndex( 'OPENGLES2_FRAMEBUFFER_INDEX' )
2175                 OpenGLES2.GenFramebuffer( framebuffer2Index )
2176                 OpenGLES2.BindFramebuffer( 'GL_FRAMEBUFFER',
2177                                            framebuffer2Index )
2178
2179                 color0Renderbuffer2Index = indexTracker.allocIndex( 'OPENGLES2_RENDERBUFFER_INDEX' )
2180                 OpenGLES2.GenRenderbuffer( color0Renderbuffer2Index )
2181                 OpenGLES2.BindRenderbuffer( 'GL_RENDERBUFFER',
2182                                             color0Renderbuffer2Index )
2183                 OpenGLES2.RenderbufferStorage( 'GL_RENDERBUFFER',
2184                                                self.color0Format,
2185                                                WIDTH, HEIGHT )
2186                 
2187                 OpenGLES2.FramebufferRenderbuffer( 'GL_FRAMEBUFFER',
2188                                                    'GL_COLOR_ATTACHMENT0',
2189                                                    'GL_RENDERBUFFER',
2190                                                    color0Renderbuffer2Index )
2191
2192                 OpenGLES2.Viewport( 0, 0, WIDTH, HEIGHT );
2193
2194                 OpenGLES2.Disable( 'GL_DITHER' )
2195                 
2196                 # ------------------------------------------------------------
2197                 # Start benchmark actions
2198                 self.beginBenchmarkActions()
2199
2200                 OpenGLES2.BindFramebuffer( 'GL_FRAMEBUFFER',
2201                                            framebuffer1Index )
2202
2203                 OpenGLES2.ClearColor( [ 0.0, 1.0, 0.0, 1.0 ] )               
2204                 OpenGLES2.Clear( [ 'GL_COLOR_BUFFER_BIT' ] )
2205                
2206                 OpenGLES2.BindFramebuffer( 'GL_FRAMEBUFFER',
2207                                            framebuffer2Index )
2208                 
2209                 OpenGLES2.ClearColor( [ 1.0, 0.0, 0.0, 1.0 ] )               
2210                 OpenGLES2.Clear( [ 'GL_COLOR_BUFFER_BIT' ] )
2211
2212                 OpenGLES2.BindFramebuffer( 'GL_FRAMEBUFFER',
2213                                            framebuffer1Index )
2214
2215                 OpenGLES2.CheckError()                
2216                 Gltest.SaveTarga( -1, 0, 0, WIDTH, HEIGHT, target.outputPath() )
2217
2218 ######################################################################
2219 class DeleteFramebufferInUse( vrt.VisualTestCase ):
2220
2221         ''' The purpose of the test: test deleting framebuffer in use.
2222
2223         Expected output: green surface. Viewport size 320*240.
2224         '''
2225         
2226         def __init__( self ):
2227                 vrt.VisualTestCase.__init__( self )
2228                 self.repeats       = 1
2229                 self.color0Format  = 'GL_RGB565'
2230                 self.gridx         = GRIDX
2231                 self.gridy         = GRIDY
2232                 self.overdraw      = 1
2233                 self.name = "OPENGLES2 delete framebuffer in use"
2234         
2235         def build( self, target, modules ):       
2236                 OpenGLES2 = modules[ 'OpenGLES2' ]
2237                 Gltest    = modules[ 'Gltest' ]
2238
2239                 indexTracker = IndexTracker()
2240                 
2241                 vertexShaderSource = """
2242                     precision mediump float;
2243                     attribute vec4 gVertex;
2244                     attribute vec4 gColor;
2245                     varying vec4 gVSColor;
2246                     uniform float gOffset;
2247
2248                     void main()
2249                     {
2250                         gl_Position = vec4( gVertex.x, gVertex.y, gVertex.z + gOffset, gVertex.w );
2251                         gVSColor = gColor;
2252                     }"""
2253
2254                 fragmentShaderSource = """
2255                     precision mediump float;
2256                     varying vec4 gVSColor;
2257
2258                     void main()
2259                     {
2260                         gl_FragColor = gVSColor;
2261                     }"""
2262
2263                 vsSource = formatShader( vertexShaderSource )
2264                 fsSource = formatShader( fragmentShaderSource )
2265                 
2266                 # ------------------------------------------------------------
2267                 # Init actions
2268                 self.beginInitActions()
2269
2270                 defaultAttributes = target.getDefaultAttributes( VT_API_OPENGLES2 )
2271                 state = target.setup( modules,
2272                                       indexTracker,
2273                                       1, 1,
2274                                       [ VT_API_OPENGLES2 ],
2275                                       defaultAttributes )
2276
2277                 OpenGLES2.ClearColor( [ 0.0, 1.0, 0.0, 1.0 ] )               
2278                 OpenGLES2.Clear( [ 'GL_COLOR_BUFFER_BIT' ] )               
2279                 
2280                 framebufferIndex = indexTracker.allocIndex( 'OPENGLES2_FRAMEBUFFER_INDEX' )
2281                 OpenGLES2.GenFramebuffer( framebufferIndex )
2282                 OpenGLES2.BindFramebuffer( 'GL_FRAMEBUFFER',
2283                                            framebufferIndex )
2284
2285                 color0RenderbufferIndex = indexTracker.allocIndex( 'OPENGLES2_RENDERBUFFER_INDEX' )
2286                 OpenGLES2.GenRenderbuffer( color0RenderbufferIndex )
2287                 OpenGLES2.BindRenderbuffer( 'GL_RENDERBUFFER',
2288                                             color0RenderbufferIndex )
2289                 OpenGLES2.RenderbufferStorage( 'GL_RENDERBUFFER',
2290                                                self.color0Format,
2291                                                WIDTH, HEIGHT )
2292                 
2293                 OpenGLES2.FramebufferRenderbuffer( 'GL_FRAMEBUFFER',
2294                                                    'GL_COLOR_ATTACHMENT0',
2295                                                    'GL_RENDERBUFFER',
2296                                                    color0RenderbufferIndex )
2297
2298                 OpenGLES2.CheckFramebufferStatus( 'GL_FRAMEBUFFER' )
2299
2300                 OpenGLES2.ClearColor( [ 1.0, 0.0, 0.0, 1.0 ] )
2301                 OpenGLES2.Clear( [ 'GL_COLOR_BUFFER_BIT' ] )               
2302                 OpenGLES2.Viewport( 0, 0, WIDTH, HEIGHT );
2303
2304                 OpenGLES2.Disable( 'GL_DITHER' )
2305                 
2306                 # ------------------------------------------------------------
2307                 # Start benchmark actions
2308                 self.beginBenchmarkActions()
2309
2310                 OpenGLES2.DeleteFramebuffer( framebufferIndex )
2311
2312                 OpenGLES2.CheckError()                
2313                 Gltest.SaveTarga( -1, 0, 0, WIDTH, HEIGHT, target.outputPath() )
2314                 
2315 ######################################################################
2316 class FramebufferOES_rgb8_rgba8( vrt.VisualTestCase ):
2317
2318         ''' The purpose of the test: test framebuffer with RGB8 and RGBA8 color
2319         formats. Requires GL_OES_rgb8_rgba8 extension.
2320
2321         Expected output: full screen plane with a 4x4 vertex grid. The vertex
2322         colors are defined so that the lower-left vertex has color
2323         [0.0,0.0,0.5,1.0] and the upper-right vertex has color
2324         [1.0,1.0,1.0,1.0]. Between those the vertex color is linearly
2325         interpolated from left to right, from bottom to top. Viewport size
2326         320*240.
2327         '''
2328         testValues = { ( 'color0Format' ) : [ ( 'GL_RGB8_OES' ),
2329                                               ( 'GL_RGBA8_OES' ),
2330                                               ]
2331                        }
2332         
2333         def __init__( self, color0Format ):
2334                 vrt.VisualTestCase.__init__( self )
2335                 self.repeats       = 1
2336                 self.color0Format  = color0Format
2337                 self.gridx         = GRIDX
2338                 self.gridy         = GRIDY
2339                 self.overdraw      = 1
2340                 self.name = "OPENGLES2 framebuffer OES_rgb8_rgba8 extension, color0=%s" % ( self.color0Format[ 3: ], )
2341         
2342         def build( self, target, modules ):       
2343                 OpenGLES2 = modules[ 'OpenGLES2' ]
2344                 Gltest    = modules[ 'Gltest' ]
2345
2346                 indexTracker = IndexTracker()
2347                 
2348                 vertexShaderSource = """
2349                     precision mediump float;
2350                     attribute vec4 gVertex;
2351                     attribute vec4 gColor;
2352                     varying vec4 gVSColor;
2353                     uniform float gOffset;
2354
2355                     void main()
2356                     {
2357                         gl_Position = vec4( gVertex.x, gVertex.y, gVertex.z + gOffset, gVertex.w );
2358                         gVSColor = gColor;
2359                     }"""
2360
2361                 fragmentShaderSource = """
2362                     precision mediump float;
2363                     varying vec4 gVSColor;
2364
2365                     void main()
2366                     {
2367                         gl_FragColor = gVSColor;
2368                     }"""
2369
2370                 vsSource = formatShader( vertexShaderSource )
2371                 fsSource = formatShader( fragmentShaderSource )
2372                 
2373                 # ------------------------------------------------------------
2374                 # Init actions
2375                 self.beginInitActions()
2376
2377                 defaultAttributes = target.getDefaultAttributes( VT_API_OPENGLES2 )
2378                 state = target.setup( modules,
2379                                       indexTracker,
2380                                       1, 1,
2381                                       [ VT_API_OPENGLES2 ],
2382                                       defaultAttributes )
2383
2384                 OpenGLES2.CheckExtension( 'GL_OES_rgb8_rgba8' )
2385                 
2386                 framebufferIndex = indexTracker.allocIndex( 'OPENGLES2_FRAMEBUFFER_INDEX' )
2387                 OpenGLES2.GenFramebuffer( framebufferIndex )
2388                 OpenGLES2.BindFramebuffer( 'GL_FRAMEBUFFER',
2389                                            framebufferIndex )
2390                 
2391                 color0RenderbufferIndex = indexTracker.allocIndex( 'OPENGLES2_RENDERBUFFER_INDEX' )
2392                 OpenGLES2.GenRenderbuffer( color0RenderbufferIndex )
2393                 OpenGLES2.BindRenderbuffer( 'GL_RENDERBUFFER',
2394                                             color0RenderbufferIndex )
2395                 OpenGLES2.RenderbufferStorage( 'GL_RENDERBUFFER',
2396                                                self.color0Format,
2397                                                WIDTH, HEIGHT )
2398                 
2399                 OpenGLES2.FramebufferRenderbuffer( 'GL_FRAMEBUFFER',
2400                                                    'GL_COLOR_ATTACHMENT0',
2401                                                    'GL_RENDERBUFFER',
2402                                                    color0RenderbufferIndex )
2403
2404                 OpenGLES2.CheckFramebufferStatus( 'GL_FRAMEBUFFER' )
2405                 
2406                 mesh = createPlane( self.gridx, self.gridy )
2407                 mesh.gradientColors( [ 0.0, 0.0, 1.0, 1.0 ], [ 1.0, 1.0, 1.0, 1.0 ] )               
2408                 
2409                 vertexDataSetup = setupVertexData( modules, indexTracker, mesh )
2410                 
2411                 program = createProgram( modules, indexTracker, vsSource, fsSource, vertexDataSetup )
2412                 useVertexData( modules, indexTracker, vertexDataSetup, program )
2413
2414                 offsetLocationIndex     = indexTracker.allocIndex( 'OPENGLES2_LOCATION_INDEX' )
2415                 OpenGLES2.GetUniformLocation( offsetLocationIndex, program.programIndex, 'gOffset' )
2416
2417                 OpenGLES2.Viewport( 0, 0, WIDTH, HEIGHT );
2418                 OpenGLES2.ClearColor( [ 0.0, 0.0, 0.0, 1.0 ] )
2419
2420                 OpenGLES2.Disable( 'GL_DITHER' )
2421                 
2422                 # ------------------------------------------------------------
2423                 # Start benchmark actions
2424                 self.beginBenchmarkActions()
2425
2426                 OpenGLES2.Clear( [ 'GL_COLOR_BUFFER_BIT' ] )
2427
2428                 OpenGLES2.Uniformf( offsetLocationIndex, 1, [ 0.0 ] )
2429                 drawVertexData( modules, vertexDataSetup )
2430
2431                 OpenGLES2.CheckError()
2432                 Gltest.SaveTarga( -1, 0, 0, WIDTH, HEIGHT, target.outputPath() )
2433                 
2434 ######################################################################
2435 class FramebufferOES_depth24( vrt.VisualTestCase ):
2436
2437         ''' The purpose of the test: test framebuffer with DEPTH24. Requires
2438         GL_OES_depth24 extension.
2439
2440         Expected output: first the test draws a full screen mesh with a 4x4
2441         vertex grid. The vertex colors are defined so that the lower-left vertex
2442         has color [0.0,0.0,1.0,1.0] and the upper-right vertex has color
2443         [1.0,1.0,1.0,1.0]. In between, the vertex color is linearly interpolated
2444         from left to right, from bottom to top. The vertex depth values are
2445         defined -1.0 and 1.0 in a similar fashion. The first mesh is drawn using
2446         GL_ALWAYS depth func, after which the depth function is set
2447         GL_LESS. Secondly, the test draws a full screen mesh with a 4x4 vertex
2448         grid. The vertex colors are defined [1.0,1.0,1.0,1.0] (lower-left) and
2449         [1.0,0.0,0.0,1.0] (upper-right). In between, the vertex color is
2450         linearly interpolated from left to right, from bottom to top. The vertex
2451         depth values are defined 1.0 and 0.0 in a similar fashion. Viewport size
2452         320*240.
2453         '''
2454         testValues = { ( 'color0Format', 'depthFormat' ) : [ ( 'GL_RGB565', 'GL_DEPTH_COMPONENT24_OES' ),
2455                                                              ]
2456                        }
2457         
2458         def __init__( self, color0Format, depthFormat ):
2459                 vrt.VisualTestCase.__init__( self )
2460                 self.repeats       = 1
2461                 self.color0Format  = color0Format
2462                 self.depthFormat   = depthFormat
2463                 self.gridx         = GRIDX
2464                 self.gridy         = GRIDY
2465                 self.overdraw      = 1
2466                 self.name = "OPENGLES2 framebuffer OES_depth24 extension, color0=%s, depth=%s" % ( self.color0Format[ 3: ],
2467                                                                                                    self.depthFormat[ 3: ], )
2468         
2469         def build( self, target, modules ):       
2470                 OpenGLES2 = modules[ 'OpenGLES2' ]
2471                 Gltest    = modules[ 'Gltest' ]
2472
2473                 indexTracker = IndexTracker()
2474                 
2475                 vertexShaderSource = """
2476                     precision mediump float;
2477                     attribute vec4 gVertex;
2478                     attribute vec4 gColor;
2479                     varying vec4 gVSColor;
2480                     uniform float gOffset;
2481
2482                     void main()
2483                     {
2484                         gl_Position = vec4( gVertex.x, gVertex.y, gVertex.z + gOffset, gVertex.w );
2485                         gVSColor = gColor;
2486                     }"""
2487
2488                 fragmentShaderSource = """
2489                     precision mediump float;
2490                     varying vec4 gVSColor;
2491
2492                     void main()
2493                     {
2494                         gl_FragColor = gVSColor;
2495                     }"""
2496
2497                 vsSource = formatShader( vertexShaderSource )
2498                 fsSource = formatShader( fragmentShaderSource )
2499                 
2500                 # ------------------------------------------------------------
2501                 # Init actions
2502                 self.beginInitActions()
2503
2504                 defaultAttributes = target.getDefaultAttributes( VT_API_OPENGLES2 )
2505                 state = target.setup( modules,
2506                                       indexTracker,
2507                                       1, 1,
2508                                       [ VT_API_OPENGLES2 ],
2509                                       defaultAttributes )
2510
2511                 OpenGLES2.CheckExtension( 'GL_OES_depth24' )
2512                 
2513                 framebufferIndex = indexTracker.allocIndex( 'OPENGLES2_FRAMEBUFFER_INDEX' )
2514                 OpenGLES2.GenFramebuffer( framebufferIndex )
2515                 OpenGLES2.BindFramebuffer( 'GL_FRAMEBUFFER',
2516                                            framebufferIndex )
2517                 
2518                 color0RenderbufferIndex = indexTracker.allocIndex( 'OPENGLES2_RENDERBUFFER_INDEX' )
2519                 OpenGLES2.GenRenderbuffer( color0RenderbufferIndex )
2520                 OpenGLES2.BindRenderbuffer( 'GL_RENDERBUFFER',
2521                                             color0RenderbufferIndex )
2522                 OpenGLES2.RenderbufferStorage( 'GL_RENDERBUFFER',
2523                                                self.color0Format,
2524                                                WIDTH, HEIGHT )
2525                 
2526                 depthRenderbufferIndex = indexTracker.allocIndex( 'OPENGLES2_RENDERBUFFER_INDEX' )
2527                 OpenGLES2.GenRenderbuffer( depthRenderbufferIndex )
2528                 OpenGLES2.BindRenderbuffer( 'GL_RENDERBUFFER',
2529                                             depthRenderbufferIndex )
2530                 OpenGLES2.RenderbufferStorage( 'GL_RENDERBUFFER',
2531                                                self.depthFormat,
2532                                                WIDTH, HEIGHT )
2533
2534                 OpenGLES2.FramebufferRenderbuffer( 'GL_FRAMEBUFFER',
2535                                                    'GL_COLOR_ATTACHMENT0',
2536                                                    'GL_RENDERBUFFER',
2537                                                    color0RenderbufferIndex )
2538
2539                 OpenGLES2.FramebufferRenderbuffer( 'GL_FRAMEBUFFER',
2540                                                    'GL_DEPTH_ATTACHMENT',
2541                                                    'GL_RENDERBUFFER',
2542                                                    depthRenderbufferIndex )
2543
2544                 OpenGLES2.CheckFramebufferStatus( 'GL_FRAMEBUFFER' )
2545                 
2546                 mesh1 = createPlane( self.gridx, self.gridy )
2547                 mesh1.gradientColors( [ 0.0, 0.0, 1.0, 1.0 ], [ 1.0, 1.0, 1.0, 1.0 ] )
2548                 mesh1.depthGradient( -1.0, 1.0 )
2549                 
2550                 mesh2 = createPlane( self.gridx, self.gridy )
2551                 mesh2.gradientColors( [ 1.0, 0.0, 0.0, 1.0 ], [ 1.0, 1.0, 1.0, 1.0 ] )
2552                 mesh2.depthGradient( 1.0, -1.0 )
2553                 
2554                 vertexDataSetup1 = setupVertexData( modules, indexTracker, mesh1 )
2555                 vertexDataSetup2 = setupVertexData( modules, indexTracker, mesh2 )
2556                 
2557                 program = createProgram( modules, indexTracker, vsSource, fsSource, vertexDataSetup1 )
2558                 useVertexData( modules, indexTracker, vertexDataSetup1, program )
2559
2560                 offsetLocationIndex     = indexTracker.allocIndex( 'OPENGLES2_LOCATION_INDEX' )
2561                 OpenGLES2.GetUniformLocation( offsetLocationIndex, program.programIndex, 'gOffset' )
2562
2563                 OpenGLES2.Viewport( 0, 0, WIDTH, HEIGHT );
2564                 OpenGLES2.ClearColor( [ 0.0, 0.0, 0.0, 1.0 ] )
2565
2566                 OpenGLES2.Disable( 'GL_DITHER' )
2567                 
2568                 # ------------------------------------------------------------
2569                 # Start benchmark actions
2570                 self.beginBenchmarkActions()
2571
2572                 OpenGLES2.Clear( [ 'GL_COLOR_BUFFER_BIT', 'GL_DEPTH_BUFFER_BIT' ] )
2573
2574                 OpenGLES2.Enable( 'GL_DEPTH_TEST' )
2575                 OpenGLES2.DepthFunc( 'GL_ALWAYS' )                                
2576                 OpenGLES2.Uniformf( offsetLocationIndex, 1, [ 0.7 ] )
2577                 drawVertexData( modules, vertexDataSetup1 )
2578                 
2579                 OpenGLES2.DepthFunc( 'GL_LESS' )
2580
2581                 useVertexData( modules, indexTracker, vertexDataSetup2, program )
2582                 OpenGLES2.Uniformf( offsetLocationIndex, 1, [ 0.5 ] )
2583                 drawVertexData( modules, vertexDataSetup2 )
2584
2585                 OpenGLES2.CheckError()
2586                 Gltest.SaveTarga( -1, 0, 0, WIDTH, HEIGHT, target.outputPath() )
2587
2588 ######################################################################
2589 class FramebufferOES_stencil4( vrt.VisualTestCase ):
2590
2591         ''' The purpose of the test: test framebuffer with stencil. Requires
2592         GL_OES_stencil4 extension.
2593
2594         Expected output: a blue rectangle in the middle of a red
2595         rectangle. Viewport size 320*240.
2596         '''
2597         testValues = { ( 'color0Format', 'stencilFormat' ) : [ ( 'GL_RGB565', 'GL_STENCIL_INDEX4_OES' ),
2598                                                                ]
2599                        }
2600         
2601         def __init__( self, color0Format, stencilFormat ):
2602                 vrt.VisualTestCase.__init__( self )
2603                 self.repeats       = 1
2604                 self.color0Format  = color0Format
2605                 self.stencilFormat = stencilFormat
2606                 self.gridx         = GRIDX
2607                 self.gridy         = GRIDY
2608                 self.overdraw      = 1
2609                 self.name = "OPENGLES2 framebuffer OES_stencil4 extension, color0=%s, stencil=%s" % ( self.color0Format[ 3: ],
2610                                                                                                       self.stencilFormat[ 3: ], )
2611         
2612         def build( self, target, modules ):       
2613                 OpenGLES2 = modules[ 'OpenGLES2' ]
2614                 Gltest    = modules[ 'Gltest' ]
2615
2616                 indexTracker = IndexTracker()
2617                 
2618                 vertexShaderSource = """
2619                     precision mediump float;
2620                     attribute vec4 gVertex;
2621                     attribute vec4 gColor;
2622                     varying vec4 gVSColor;
2623                     uniform float gOffset;
2624
2625                     void main()
2626                     {
2627                         gl_Position = vec4( gVertex.x, gVertex.y, gVertex.z + gOffset, gVertex.w );
2628                         gVSColor = gColor;
2629                     }"""
2630
2631                 fragmentShaderSource = """
2632                     precision mediump float;
2633                     varying vec4 gVSColor;
2634
2635                     void main()
2636                     {
2637                         gl_FragColor = gVSColor;
2638                     }"""
2639
2640                 vsSource = formatShader( vertexShaderSource )
2641                 fsSource = formatShader( fragmentShaderSource )
2642                 
2643                 # ------------------------------------------------------------
2644                 # Init actions
2645                 self.beginInitActions()
2646
2647                 defaultAttributes = target.getDefaultAttributes( VT_API_OPENGLES2 )
2648                 state = target.setup( modules,
2649                                       indexTracker,
2650                                       1, 1,
2651                                       [ VT_API_OPENGLES2 ],
2652                                       defaultAttributes )
2653
2654                 OpenGLES2.CheckExtension( 'GL_OES_stencil4' )
2655                 
2656                 framebufferIndex = indexTracker.allocIndex( 'OPENGLES2_FRAMEBUFFER_INDEX' )
2657                 OpenGLES2.GenFramebuffer( framebufferIndex )
2658                 OpenGLES2.BindFramebuffer( 'GL_FRAMEBUFFER',
2659                                            framebufferIndex )
2660                 
2661                 color0RenderbufferIndex = indexTracker.allocIndex( 'OPENGLES2_RENDERBUFFER_INDEX' )
2662                 OpenGLES2.GenRenderbuffer( color0RenderbufferIndex )
2663                 OpenGLES2.BindRenderbuffer( 'GL_RENDERBUFFER',
2664                                             color0RenderbufferIndex )
2665                 OpenGLES2.RenderbufferStorage( 'GL_RENDERBUFFER',
2666                                                self.color0Format,
2667                                                WIDTH, HEIGHT )
2668                 
2669                 stencilRenderbufferIndex = indexTracker.allocIndex( 'OPENGLES2_RENDERBUFFER_INDEX' )
2670                 OpenGLES2.GenRenderbuffer( stencilRenderbufferIndex )
2671                 OpenGLES2.BindRenderbuffer( 'GL_RENDERBUFFER',
2672                                             stencilRenderbufferIndex )
2673                 OpenGLES2.RenderbufferStorage( 'GL_RENDERBUFFER',
2674                                                self.stencilFormat,
2675                                                WIDTH, HEIGHT )
2676
2677                 OpenGLES2.FramebufferRenderbuffer( 'GL_FRAMEBUFFER',
2678                                                    'GL_COLOR_ATTACHMENT0',
2679                                                    'GL_RENDERBUFFER',
2680                                                    color0RenderbufferIndex )
2681
2682                 OpenGLES2.FramebufferRenderbuffer( 'GL_FRAMEBUFFER',
2683                                                    'GL_STENCIL_ATTACHMENT',
2684                                                    'GL_RENDERBUFFER',
2685                                                    stencilRenderbufferIndex )
2686
2687                 OpenGLES2.CheckFramebufferStatus( 'GL_FRAMEBUFFER' )
2688                 
2689                 mesh1 = createPlane( self.gridx, self.gridy )
2690                 mesh1.gradientColors( [ 0.0, 0.0, 1.0, 1.0 ], [ 1.0, 1.0, 1.0, 1.0 ] )
2691
2692                 mesh2 = createPlane( self.gridx, self.gridy )
2693                 mesh2.gradientColors( [ 1.0, 0.0, 0.0, 1.0 ], [ 1.0, 1.0, 1.0, 1.0 ] )
2694                 
2695                 vertexDataSetup1 = setupVertexData( modules, indexTracker, mesh1 )
2696                 vertexDataSetup2 = setupVertexData( modules, indexTracker, mesh2 )
2697                 
2698                 program = createProgram( modules, indexTracker, vsSource, fsSource, vertexDataSetup1 )
2699                 useVertexData( modules, indexTracker, vertexDataSetup1, program )
2700
2701                 offsetLocationIndex     = indexTracker.allocIndex( 'OPENGLES2_LOCATION_INDEX' )
2702                 OpenGLES2.GetUniformLocation( offsetLocationIndex, program.programIndex, 'gOffset' )
2703
2704                 OpenGLES2.Viewport( 0, 0, WIDTH, HEIGHT );
2705                 OpenGLES2.ClearColor( [ 0.0, 0.0, 0.0, 1.0 ] )
2706                 OpenGLES2.ClearStencil( 1 )