2
# Spandex benchmark and test framework.
4
# Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
6
# Contact: Kari J. Kangas <kari.j.kangas@nokia.com>
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.
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
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/>.
21
######################################################################
22
from bmbuilder import Benchmark
23
from lib.common.common import *
24
from lib.geom.mesh import *
27
######################################################################
28
def createPlane( gridx, gridy ):
29
vertexAttribute = MeshVertexAttribute( MESH_TYPE_FIXED, 3 )
30
texCoordAttribute = []
31
indexAttribute = MeshAttribute( MESH_TYPE_UNSIGNED_SHORT )
33
texCoordAttribute.append( MeshTexCoordAttribute( MESH_TYPE_FIXED, 2, [ 1.0, 1.0 ] ) )
35
mesh = MeshStripPlane( gridx,
46
mesh.translate( [ -0.5, -0.5, 0 ] )
47
mesh.scale( [ 2.0, 2.0 ] )
51
######################################################################
52
def setupTexture( modules, indexTracker, w, h, c1, c2, textureType ):
53
OpenGLES2 = modules[ 'OpenGLES2' ]
55
textureDataIndex = indexTracker.allocIndex( 'OPENGLES2_TEXTURE_DATA_INDEX' )
56
OpenGLES2.CreateCheckerTextureData( textureDataIndex,
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',
74
return TextureSetup( textureDataIndex, textureIndex )
77
#----------------------------------------------------------------------
78
#----------------------------------------------------------------------
79
#----------------------------------------------------------------------
80
class ViewportBenchmark( Benchmark ):
81
def __init__( self, gridx, gridy, overdraw, textureType, viewportRect ):
82
Benchmark.__init__( self )
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 ),
92
textureTypeToStr( self.textureType ), )
94
def build( self, target, modules ):
95
OpenGLES2 = modules[ 'OpenGLES2' ]
97
indexTracker = IndexTracker()
99
rawVertexShaderSource = """
100
precision mediump float;
101
attribute vec4 gVertex;
102
attribute vec4 gTexCoord0;
103
varying vec4 gVSTexCoord0;
104
uniform float gOffset;
108
gl_Position = vec4( gVertex.x, gVertex.y, gVertex.z + gOffset, gVertex.w );
109
gVSTexCoord0 = gTexCoord0;
112
rawFragmentShaderSource = """
113
precision mediump float;
114
uniform sampler2D gTexture0;
115
varying vec4 gVSTexCoord0;
119
gl_FragColor = texture2D( gTexture0, gVSTexCoord0.xy );
122
vertexShaderSource = formatShader( rawVertexShaderSource )
123
fragmentShaderSource = formatShader( rawFragmentShaderSource )
125
( screenWidth, screenHeight, ) = target.getScreenSize()
127
# ------------------------------------------------------------
129
self.beginInitActions()
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 )
139
textureSetup = setupTexture( modules,
141
screenWidth, screenHeight,
142
[ 1.0, 0.3, 0.3, 1.0 ],
143
[ 0.3, 1.0, 0.3, 1.0 ],
146
useTexture( modules, textureSetup, 0, program )
148
offsetLocationIndex = indexTracker.allocIndex( 'OPENGLES2_LOCATION_INDEX' )
149
OpenGLES2.GetUniformLocation( offsetLocationIndex, program.programIndex, 'gOffset' )
151
OpenGLES2.Enable( 'GL_DEPTH_TEST' )
152
OpenGLES2.DepthFunc( 'GL_LEQUAL' )
154
OpenGLES2.Clear( [ 'GL_COLOR_BUFFER_BIT', 'GL_DEPTH_BUFFER_BIT' ] )
155
target.swapBuffers( state )
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 ] ) )
164
OpenGLES2.CheckError( '' )
166
# ------------------------------------------------------------
167
# Start benchmark actions
168
self.beginBenchmarkActions()
170
OpenGLES2.Clear( [ 'GL_COLOR_BUFFER_BIT', 'GL_DEPTH_BUFFER_BIT' ] )
172
delta = 1.0 / float( self.overdraw )
175
for i in range( self.overdraw ):
176
OpenGLES2.Uniformf( offsetLocationIndex, 1, [ offset ] )
177
drawMeshBufferData( modules, meshBufferDataSetup )
180
target.swapBuffers( state )
183
#----------------------------------------------------------------------
184
#----------------------------------------------------------------------
185
#----------------------------------------------------------------------
186
class ViewportSwitchBenchmark( Benchmark ):
187
def __init__( self, gridx, gridy, textureType, overdraw1, viewportRect1, overdraw2, viewportRect2 ):
188
Benchmark.__init__( self )
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 ),
199
arrayToStr( self.viewportRect2 ),
202
textureTypeToStr( self.textureType ), )
204
def build( self, target, modules ):
205
OpenGLES2 = modules[ 'OpenGLES2' ]
207
indexTracker = IndexTracker()
209
rawVertexShaderSource = """
210
precision mediump float;
211
attribute vec4 gVertex;
212
attribute vec4 gTexCoord0;
213
varying vec4 gVSTexCoord0;
214
uniform float gOffset;
218
gl_Position = vec4( gVertex.x, gVertex.y, gVertex.z + gOffset, gVertex.w );
219
gVSTexCoord0 = gTexCoord0;
222
rawFragmentShaderSource = """
223
precision mediump float;
224
uniform sampler2D gTexture0;
225
varying vec4 gVSTexCoord0;
229
gl_FragColor = texture2D( gTexture0, gVSTexCoord0.xy );
232
vertexShaderSource = formatShader( rawVertexShaderSource )
233
fragmentShaderSource = formatShader( rawFragmentShaderSource )
235
( screenWidth, screenHeight, ) = target.getScreenSize()
237
# ------------------------------------------------------------
239
self.beginInitActions()
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 )
249
textureSetup = setupTexture( modules,
251
screenWidth, screenHeight,
252
[ 1.0, 0.5, 0.5, 1.0 ],
253
[ 0.3, 1.0, 0.3, 1.0 ],
256
useTexture( modules, textureSetup, 0, program )
258
offsetLocationIndex = indexTracker.allocIndex( 'OPENGLES2_LOCATION_INDEX' )
259
OpenGLES2.GetUniformLocation( offsetLocationIndex, program.programIndex, 'gOffset' )
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 ],
266
screenWidth, screenHeight,
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',
280
OpenGLES2.Enable( 'GL_DEPTH_TEST' )
281
OpenGLES2.DepthFunc( 'GL_LEQUAL' )
283
OpenGLES2.ClearColor( [ 1.0, 0.0, 0.0, 1.0 ] )
285
OpenGLES2.CheckError( '' )
287
# ------------------------------------------------------------
288
# Start benchmark actions
289
self.beginBenchmarkActions()
291
OpenGLES2.Viewport( 0, 0, screenWidth, screenHeight )
292
OpenGLES2.Clear( [ 'GL_COLOR_BUFFER_BIT', 'GL_DEPTH_BUFFER_BIT' ] )
294
overdraw = self.overdraw1 + self.overdraw2
295
delta = 1.0 / float( overdraw )
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 ] ) )
304
OpenGLES2.BindTexture( 'GL_TEXTURE_2D', textureSetup.textureIndex )
306
for i in range( self.overdraw1 ):
307
OpenGLES2.Uniformf( offsetLocationIndex, 1, [ offset ] )
308
drawMeshBufferData( modules, meshBufferDataSetup )
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 ] ) )
317
OpenGLES2.BindTexture( 'GL_TEXTURE_2D', textureIndex )
319
for i in range( self.overdraw2 ):
320
OpenGLES2.Uniformf( offsetLocationIndex, 1, [ offset ] )
321
drawMeshBufferData( modules, meshBufferDataSetup )
324
target.swapBuffers( state )