Ian Romanick | 63b2e42 | 2004-05-26 16:38:38 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright IBM Corporation 2002 |
| 3 | * All Rights Reserved. |
| 4 | * |
| 5 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 6 | * copy of this software and associated documentation files (the "Software"), |
| 7 | * to deal in the Software without restriction, including without limitation |
| 8 | * on the rights to use, copy, modify, merge, publish, distribute, sub |
| 9 | * license, and/or sell copies of the Software, and to permit persons to whom |
| 10 | * the Software is furnished to do so, subject to the following conditions: |
| 11 | * |
| 12 | * The above copyright notice and this permission notice (including the next |
| 13 | * paragraph) shall be included in all copies or substantial portions of the |
| 14 | * Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL |
| 19 | * VA LINUX SYSTEM, IBM AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM, |
| 20 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 21 | * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE |
| 22 | * USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 23 | */ |
| 24 | |
| 25 | /** |
| 26 | * \file api_speed.c |
| 27 | * Simple test to measure the overhead of making GL calls. |
| 28 | * |
| 29 | * The main purpose of this test is to measure the difference in calling |
| 30 | * overhead of different dispatch methods. Since it uses asm/timex.h to |
| 31 | * access the Pentium's cycle counters, it will probably only compile on |
| 32 | * Linux (though most architectures have a get_cycles function in timex.h). |
| 33 | * That is why it isn't in the default Makefile. |
| 34 | * |
| 35 | * \author Ian Romanick <idr@us.ibm.com> |
| 36 | */ |
| 37 | |
| 38 | #include <stdio.h> |
| 39 | #include <stdlib.h> |
Keith Whitwell | a58065d | 2009-03-10 13:11:23 +0000 | [diff] [blame] | 40 | #include <GL/glew.h> |
Ian Romanick | 63b2e42 | 2004-05-26 16:38:38 +0000 | [diff] [blame] | 41 | #include <GL/glut.h> |
| 42 | |
| 43 | #define inline __inline__ |
| 44 | #include <asm/timex.h> |
| 45 | |
| 46 | static float Width = 400; |
| 47 | static float Height = 400; |
| 48 | static unsigned count = 1000000; |
| 49 | |
| 50 | |
| 51 | static void Idle( void ) |
| 52 | { |
| 53 | glutPostRedisplay(); |
| 54 | } |
| 55 | |
| 56 | #define DO_FUNC(f,p) \ |
| 57 | do { \ |
| 58 | t0 = get_cycles(); \ |
| 59 | for ( i = 0 ; i < count ; i++ ) { \ |
| 60 | f p ; \ |
| 61 | } \ |
| 62 | t1 = get_cycles(); \ |
| 63 | printf("%u calls to % 20s required %llu cycles.\n", count, # f, t1 - t0); \ |
| 64 | } while( 0 ) |
| 65 | |
| 66 | /** |
| 67 | * Main display function. This is the place to add more API calls. |
| 68 | */ |
| 69 | static void Display( void ) |
| 70 | { |
| 71 | int i; |
| 72 | const float v[3] = { 1.0, 0.0, 0.0 }; |
| 73 | cycles_t t0; |
| 74 | cycles_t t1; |
| 75 | |
| 76 | glBegin(GL_TRIANGLE_STRIP); |
| 77 | |
| 78 | DO_FUNC( glColor3fv, (v) ); |
| 79 | DO_FUNC( glNormal3fv, (v) ); |
| 80 | DO_FUNC( glTexCoord2fv, (v) ); |
| 81 | DO_FUNC( glTexCoord3fv, (v) ); |
| 82 | DO_FUNC( glMultiTexCoord2fv, (GL_TEXTURE0, v) ); |
| 83 | DO_FUNC( glMultiTexCoord2f, (GL_TEXTURE0, 0.0, 0.0) ); |
Adam Jackson | 8f8e0f0 | 2005-05-20 00:44:37 +0000 | [diff] [blame] | 84 | DO_FUNC( glFogCoordfvEXT, (v) ); |
| 85 | DO_FUNC( glFogCoordfEXT, (0.5) ); |
Ian Romanick | 63b2e42 | 2004-05-26 16:38:38 +0000 | [diff] [blame] | 86 | |
| 87 | glEnd(); |
| 88 | |
| 89 | exit(0); |
| 90 | } |
| 91 | |
| 92 | |
| 93 | static void Reshape( int width, int height ) |
| 94 | { |
| 95 | Width = width; |
| 96 | Height = height; |
| 97 | glViewport( 0, 0, width, height ); |
| 98 | glMatrixMode( GL_PROJECTION ); |
| 99 | glLoadIdentity(); |
| 100 | glOrtho(0.0, width, 0.0, height, -1.0, 1.0); |
| 101 | glMatrixMode( GL_MODELVIEW ); |
| 102 | glLoadIdentity(); |
| 103 | } |
| 104 | |
| 105 | |
| 106 | static void Key( unsigned char key, int x, int y ) |
| 107 | { |
| 108 | (void) x; |
| 109 | (void) y; |
| 110 | switch (key) { |
| 111 | case 27: |
| 112 | exit(0); |
| 113 | break; |
| 114 | } |
| 115 | glutPostRedisplay(); |
| 116 | } |
| 117 | |
| 118 | |
| 119 | int main( int argc, char *argv[] ) |
| 120 | { |
| 121 | glutInit( &argc, argv ); |
| 122 | glutInitWindowSize( (int) Width, (int) Height ); |
| 123 | glutInitWindowPosition( 0, 0 ); |
| 124 | |
| 125 | glutInitDisplayMode( GLUT_RGB ); |
| 126 | |
| 127 | glutCreateWindow( argv[0] ); |
Keith Whitwell | a58065d | 2009-03-10 13:11:23 +0000 | [diff] [blame] | 128 | glewInit(); |
Ian Romanick | 63b2e42 | 2004-05-26 16:38:38 +0000 | [diff] [blame] | 129 | |
| 130 | if ( argc > 1 ) { |
| 131 | count = strtoul( argv[1], NULL, 0 ); |
| 132 | if ( count == 0 ) { |
| 133 | fprintf( stderr, "Usage: %s [iterations]\n", argv[0] ); |
| 134 | exit(1); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | glutReshapeFunc( Reshape ); |
| 139 | glutKeyboardFunc( Key ); |
| 140 | glutDisplayFunc( Display ); |
| 141 | glutIdleFunc( Idle ); |
| 142 | |
| 143 | glutMainLoop(); |
| 144 | return 0; |
| 145 | } |