~linaro-graphics-wg/+junk/spandex-package

« back to all changes in this revision

Viewing changes to bd/benchmarks/opengles2/viewport.py

  • Committer: Alexandros Frantzis
  • Date: 2011-05-04 08:50:52 UTC
  • Revision ID: alexandros.frantzis@linaro.org-20110504085052-88gps4jrg317s6lc
Tags: upstream-1.1.3~git20110502.0ae20368
ImportĀ upstreamĀ versionĀ 1.1.3~git20110502.0ae20368

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
# Spandex benchmark and test framework.
 
3
#
 
4
# Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
 
5
#
 
6
# Contact: Kari J. Kangas <kari.j.kangas@nokia.com>
 
7
#
 
8
#   This framework is free software; you can redistribute it and/or modify it
 
9
# under the terms of the GNU Lesser General Public License as published by the
 
10
# Free Software Foundation, version 2.1 of the License.
 
11
#
 
12
#   This framework is distributed in the hope that it will be useful, but
 
13
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 
14
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 
15
# for more details.
 
16
#
 
17
#   You should have received a copy of the GNU Lesser General Public License
 
18
# along with this framework; if not, see <http://www.gnu.org/licenses/>.
 
19
#
 
20
 
 
21
######################################################################
 
22
from bmbuilder         import Benchmark
 
23
from lib.common.common import *
 
24
from lib.geom.mesh     import *
 
25
from common            import *
 
26
   
 
27
######################################################################
 
28
def createPlane( gridx, gridy ):
 
29
    vertexAttribute     = MeshVertexAttribute( MESH_TYPE_FIXED, 3 )
 
30
    texCoordAttribute   = []
 
31
    indexAttribute      = MeshAttribute( MESH_TYPE_UNSIGNED_SHORT )
 
32
 
 
33
    texCoordAttribute.append( MeshTexCoordAttribute( MESH_TYPE_FIXED, 2, [ 1.0, 1.0 ] ) )
 
34
        
 
35
    mesh = MeshStripPlane( gridx,
 
36
                           gridy,
 
37
                           vertexAttribute,
 
38
                           None,
 
39
                           None,
 
40
                           texCoordAttribute,
 
41
                           indexAttribute,
 
42
                           None,
 
43
                           MESH_CCW_WINDING,
 
44
                           False )
 
45
 
 
46
    mesh.translate( [ -0.5, -0.5, 0 ] )
 
47
    mesh.scale( [ 2.0, 2.0 ] )
 
48
 
 
49
    return mesh
 
50
       
 
51
######################################################################
 
52
def setupTexture( modules, indexTracker, w, h, c1, c2, textureType ):
 
53
    OpenGLES2 = modules[ 'OpenGLES2' ]
 
54
    
 
55
    textureDataIndex = indexTracker.allocIndex( 'OPENGLES2_TEXTURE_DATA_INDEX' )
 
56
    OpenGLES2.CreateCheckerTextureData( textureDataIndex,
 
57
                                        c1,
 
58
                                        c2,
 
59
                                        4, 4,
 
60
                                        w, h,
 
61
                                        'OFF',
 
62
                                        textureType )
 
63
 
 
64
    textureIndex = indexTracker.allocIndex( 'OPENGLES2_TEXTURE_INDEX' )
 
65
    OpenGLES2.GenTexture( textureIndex )
 
66
    OpenGLES2.BindTexture( 'GL_TEXTURE_2D', textureIndex )
 
67
    OpenGLES2.TexImage2D( textureDataIndex, 'GL_TEXTURE_2D' )
 
68
    OpenGLES2.TexParameter( 'GL_TEXTURE_2D',
 
69
                            'GL_LINEAR',
 
70
                            'GL_LINEAR',
 
71
                            'GL_CLAMP_TO_EDGE',
 
72
                            'GL_CLAMP_TO_EDGE' )
 
73
 
 
74
    return TextureSetup( textureDataIndex, textureIndex )
 
75
 
 
76
 
 
77
#----------------------------------------------------------------------
 
78
#----------------------------------------------------------------------
 
79
#----------------------------------------------------------------------
 
80
class ViewportBenchmark( Benchmark ):
 
81
    def __init__( self, gridx, gridy, overdraw, textureType, viewportRect ):
 
82
        Benchmark.__init__( self )
 
83
        self.gridx        = gridx
 
84
        self.gridy        = gridy
 
85
        self.overdraw     = overdraw
 
86
        self.textureType  = textureType
 
87
        self.viewportRect = viewportRect       
 
88
        self.name = 'OPENGLES2 viewport rct=%s (grd=%dx%d ovrdrw=%d txtr.tpe=%s)' % ( arrayToStr( self.viewportRect ),
 
89
                                                                                      self.gridx,
 
90
                                                                                      self.gridy,
 
91
                                                                                      self.overdraw,
 
92
                                                                                      textureTypeToStr( self.textureType ), )
 
93
 
 
94
    def build( self, target, modules ):
 
95
        OpenGLES2 = modules[ 'OpenGLES2' ]
 
96
 
 
97
        indexTracker = IndexTracker()
 
98
 
 
99
        rawVertexShaderSource = """
 
100
        precision mediump float;
 
101
        attribute vec4 gVertex;
 
102
        attribute vec4 gTexCoord0;
 
103
        varying vec4 gVSTexCoord0;
 
104
        uniform float gOffset;
 
105
 
 
106
        void main()
 
107
        {
 
108
           gl_Position = vec4( gVertex.x, gVertex.y, gVertex.z + gOffset, gVertex.w );
 
109
           gVSTexCoord0 = gTexCoord0;
 
110
        }"""
 
111
 
 
112
        rawFragmentShaderSource = """
 
113
        precision mediump float;
 
114
        uniform sampler2D gTexture0;
 
115
        varying vec4 gVSTexCoord0;
 
116
 
 
117
        void main()
 
118
        {
 
119
           gl_FragColor = texture2D( gTexture0, gVSTexCoord0.xy );
 
120
        }"""
 
121
 
 
122
        vertexShaderSource   = formatShader( rawVertexShaderSource )
 
123
        fragmentShaderSource = formatShader( rawFragmentShaderSource )
 
124
 
 
125
        ( screenWidth, screenHeight, ) = target.getScreenSize()
 
126
        
 
127
        # ------------------------------------------------------------
 
128
        # Init actions
 
129
        self.beginInitActions()
 
130
 
 
131
        state                   = setupGL2( modules, indexTracker, target )            
 
132
        mesh                    = createPlane( self.gridx, self.gridy )
 
133
        vertexDataSetup         = setupVertexData( modules, indexTracker, mesh )
 
134
        program                 = createProgram( modules, indexTracker, vertexShaderSource, fragmentShaderSource, vertexDataSetup )
 
135
        meshDataSetup           = setupMeshData( modules, indexTracker, vertexDataSetup )
 
136
        meshBufferDataSetup     = setupMeshBufferData( modules, indexTracker, meshDataSetup )
 
137
        useMeshBufferData( modules, indexTracker, meshBufferDataSetup, program )
 
138
 
 
139
        textureSetup            = setupTexture( modules,
 
140
                                                indexTracker,
 
141
                                                screenWidth, screenHeight,
 
142
                                                [ 1.0, 0.3, 0.3, 1.0 ],
 
143
                                                [ 0.3, 1.0, 0.3, 1.0 ],
 
144
                                                self.textureType )
 
145
 
 
146
        useTexture( modules, textureSetup, 0, program )
 
147
 
 
148
        offsetLocationIndex     = indexTracker.allocIndex( 'OPENGLES2_LOCATION_INDEX' )
 
149
        OpenGLES2.GetUniformLocation( offsetLocationIndex, program.programIndex, 'gOffset' )
 
150
 
 
151
        OpenGLES2.Enable( 'GL_DEPTH_TEST' )
 
152
        OpenGLES2.DepthFunc( 'GL_LEQUAL' )
 
153
 
 
154
        OpenGLES2.Clear( [ 'GL_COLOR_BUFFER_BIT', 'GL_DEPTH_BUFFER_BIT' ] )
 
155
        target.swapBuffers( state )
 
156
        OpenGLES2.Finish()
 
157
 
 
158
        if self.viewportRect:
 
159
            OpenGLES2.Viewport( int( self.viewportRect[ 0 ] ),
 
160
                                int( self.viewportRect[ 1 ] ),
 
161
                                int( self.viewportRect[ 2 ] ),
 
162
                                int( self.viewportRect[ 3 ] ) )
 
163
 
 
164
        OpenGLES2.CheckError( '' )
 
165
            
 
166
        # ------------------------------------------------------------
 
167
        # Start benchmark actions
 
168
        self.beginBenchmarkActions()
 
169
 
 
170
        OpenGLES2.Clear( [ 'GL_COLOR_BUFFER_BIT', 'GL_DEPTH_BUFFER_BIT' ] )
 
171
 
 
172
        delta   = 1.0 / float( self.overdraw )
 
173
        offset  = 1.0
 
174
 
 
175
        for i in range( self.overdraw ):
 
176
            OpenGLES2.Uniformf( offsetLocationIndex, 1, [ offset ] )
 
177
            drawMeshBufferData( modules, meshBufferDataSetup )
 
178
            offset -= delta
 
179
 
 
180
        target.swapBuffers( state )
 
181
 
 
182
        
 
183
#----------------------------------------------------------------------
 
184
#----------------------------------------------------------------------
 
185
#----------------------------------------------------------------------
 
186
class ViewportSwitchBenchmark( Benchmark ):
 
187
    def __init__( self, gridx, gridy, textureType, overdraw1, viewportRect1, overdraw2, viewportRect2 ):
 
188
        Benchmark.__init__( self )
 
189
        self.gridx         = gridx
 
190
        self.gridy         = gridy
 
191
        self.textureType   = textureType
 
192
        self.overdraw1     = overdraw1
 
193
        self.viewportRect1 = viewportRect1
 
194
        self.overdraw2     = overdraw2
 
195
        self.viewportRect2 = viewportRect2       
 
196
        self.name = 'OPENGLES2 viewport switch ovrdrw1=%d rct1=%s ovrdrw2=%d rct2=%s (grd=%dx%d txtr.tpe=%s)' % ( self.overdraw1,
 
197
                                                                                                                  arrayToStr( self.viewportRect1 ),
 
198
                                                                                                                  self.overdraw2,
 
199
                                                                                                                  arrayToStr( self.viewportRect2 ),
 
200
                                                                                                                  self.gridx,
 
201
                                                                                                                  self.gridy,
 
202
                                                                                                                  textureTypeToStr( self.textureType ), )
 
203
 
 
204
    def build( self, target, modules ):
 
205
        OpenGLES2 = modules[ 'OpenGLES2' ]
 
206
 
 
207
        indexTracker = IndexTracker()
 
208
 
 
209
        rawVertexShaderSource = """
 
210
        precision mediump float;
 
211
        attribute vec4 gVertex;
 
212
        attribute vec4 gTexCoord0;
 
213
        varying vec4 gVSTexCoord0;
 
214
        uniform float gOffset;
 
215
 
 
216
        void main()
 
217
        {
 
218
           gl_Position = vec4( gVertex.x, gVertex.y, gVertex.z + gOffset, gVertex.w );
 
219
           gVSTexCoord0 = gTexCoord0;
 
220
        }"""
 
221
 
 
222
        rawFragmentShaderSource = """
 
223
        precision mediump float;
 
224
        uniform sampler2D gTexture0;
 
225
        varying vec4 gVSTexCoord0;
 
226
 
 
227
        void main()
 
228
        {
 
229
           gl_FragColor = texture2D( gTexture0, gVSTexCoord0.xy );
 
230
        }"""
 
231
 
 
232
        vertexShaderSource   = formatShader( rawVertexShaderSource )
 
233
        fragmentShaderSource = formatShader( rawFragmentShaderSource )
 
234
 
 
235
        ( screenWidth, screenHeight, ) = target.getScreenSize()
 
236
        
 
237
        # ------------------------------------------------------------
 
238
        # Init actions
 
239
        self.beginInitActions()
 
240
 
 
241
        state                   = setupGL2( modules, indexTracker, target )            
 
242
        mesh                    = createPlane( self.gridx, self.gridy )
 
243
        vertexDataSetup         = setupVertexData( modules, indexTracker, mesh )
 
244
        program                 = createProgram( modules, indexTracker, vertexShaderSource, fragmentShaderSource, vertexDataSetup )
 
245
        meshDataSetup           = setupMeshData( modules, indexTracker, vertexDataSetup )
 
246
        meshBufferDataSetup     = setupMeshBufferData( modules, indexTracker, meshDataSetup )
 
247
        useMeshBufferData( modules, indexTracker, meshBufferDataSetup, program )
 
248
 
 
249
        textureSetup            = setupTexture( modules,
 
250
                                                indexTracker,
 
251
                                                screenWidth, screenHeight,
 
252
                                                [ 1.0, 0.5, 0.5, 1.0 ],
 
253
                                                [ 0.3, 1.0, 0.3, 1.0 ],
 
254
                                                self.textureType )
 
255
 
 
256
        useTexture( modules, textureSetup, 0, program )
 
257
 
 
258
        offsetLocationIndex     = indexTracker.allocIndex( 'OPENGLES2_LOCATION_INDEX' )
 
259
        OpenGLES2.GetUniformLocation( offsetLocationIndex, program.programIndex, 'gOffset' )
 
260
 
 
261
        textureDataIndex        = indexTracker.allocIndex( 'OPENGLES2_TEXTURE_DATA_INDEX' )
 
262
        OpenGLES2.CreateCheckerTextureData( textureDataIndex,
 
263
                                            [ 0.5, 0.7, 0.1, 0.5 ],
 
264
                                            [ 0.7, 0.5, 0.1, 0.5 ],
 
265
                                            16, 16,
 
266
                                            screenWidth, screenHeight,
 
267
                                            'OFF',
 
268
                                            self.textureType )
 
269
 
 
270
        textureIndex = indexTracker.allocIndex( 'OPENGLES2_TEXTURE_INDEX' )
 
271
        OpenGLES2.GenTexture( textureIndex )
 
272
        OpenGLES2.BindTexture( 'GL_TEXTURE_2D', textureIndex )
 
273
        OpenGLES2.TexImage2D( textureDataIndex, 'GL_TEXTURE_2D' )
 
274
        OpenGLES2.TexParameter( 'GL_TEXTURE_2D',
 
275
                                'GL_LINEAR',
 
276
                                'GL_LINEAR',
 
277
                                'GL_CLAMP_TO_EDGE',
 
278
                                'GL_CLAMP_TO_EDGE' )
 
279
       
 
280
        OpenGLES2.Enable( 'GL_DEPTH_TEST' )
 
281
        OpenGLES2.DepthFunc( 'GL_LEQUAL' )
 
282
 
 
283
        OpenGLES2.ClearColor( [ 1.0, 0.0, 0.0, 1.0 ] )
 
284
        
 
285
        OpenGLES2.CheckError( '' )
 
286
        
 
287
        # ------------------------------------------------------------
 
288
        # Start benchmark actions
 
289
        self.beginBenchmarkActions()
 
290
 
 
291
        OpenGLES2.Viewport( 0, 0, screenWidth, screenHeight )
 
292
        OpenGLES2.Clear( [ 'GL_COLOR_BUFFER_BIT', 'GL_DEPTH_BUFFER_BIT' ] )
 
293
 
 
294
        overdraw = self.overdraw1 + self.overdraw2
 
295
        delta    = 1.0 / float( overdraw )
 
296
        offset   = 1.0
 
297
 
 
298
        if self.viewportRect1:
 
299
            OpenGLES2.Viewport( int( self.viewportRect1[ 0 ] ),
 
300
                                int( self.viewportRect1[ 1 ] ),
 
301
                                int( self.viewportRect1[ 2 ] ),
 
302
                                int( self.viewportRect1[ 3 ] ) )
 
303
 
 
304
        OpenGLES2.BindTexture( 'GL_TEXTURE_2D', textureSetup.textureIndex )
 
305
            
 
306
        for i in range( self.overdraw1 ):
 
307
            OpenGLES2.Uniformf( offsetLocationIndex, 1, [ offset ] )
 
308
            drawMeshBufferData( modules, meshBufferDataSetup )
 
309
            offset -= delta
 
310
 
 
311
        if self.viewportRect2:
 
312
            OpenGLES2.Viewport( int( self.viewportRect2[ 0 ] ),
 
313
                                int( self.viewportRect2[ 1 ] ),
 
314
                                int( self.viewportRect2[ 2 ] ),
 
315
                                int( self.viewportRect2[ 3 ] ) )
 
316
 
 
317
        OpenGLES2.BindTexture( 'GL_TEXTURE_2D', textureIndex )
 
318
            
 
319
        for i in range( self.overdraw2 ):            
 
320
            OpenGLES2.Uniformf( offsetLocationIndex, 1, [ offset ] )
 
321
            drawMeshBufferData( modules, meshBufferDataSetup )
 
322
            offset -= delta
 
323
        
 
324
        target.swapBuffers( state )
 
325