WIP - port to Mali EGL
[mesa-demos/mali.git] / src / tests / cva_huge.c
blob8ce7d535cf93fbdad30f0a703408e100f11e6099
1 /*
2 * Copyright © 2010 Pauli Nieminen
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
26 * Test case for huge cva arrays. Mesa code has to split this to multiple VBOs
27 * which had memory access error.
28 * This test case doesn't render incorrectly but valgrind showed the memory
29 * access error.
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <string.h>
35 #include <stddef.h> /* for ptrdiff_t, referenced by GL.h when GL_GLEXT_LEGACY defined */
36 #ifdef _WIN32
37 #include <windows.h>
38 #endif
39 #define GL_GLEXT_LEGACY
40 #include "glut_wrap.h"
42 GLfloat *verts;
44 GLubyte *color;
46 GLuint *indices;
48 #define rows 1000 /* Create 1000x1000 vertex grid */
49 #define row_width 5000.0
50 #define grid_depth -50.0
51 GLuint nr_verts_in_row = rows;
52 GLuint nr_indices_in_strip = rows * 2;
54 GLboolean double_buffer;
55 GLboolean compiled = GL_TRUE;
57 static void generate_verts( void )
59 unsigned x, y;
60 GLfloat step = row_width /(GLfloat)(nr_verts_in_row - 1);
61 verts = malloc(sizeof(verts[0]) * 4 * nr_verts_in_row * nr_verts_in_row);
63 for (y = 0; y < nr_verts_in_row; ++y) {
64 for (x = 0; x < nr_verts_in_row; ++x) {
65 unsigned idx = 4*(x + y * nr_verts_in_row);
66 verts[idx + 0] = step * x - row_width/2.0;
67 verts[idx + 1] = step * y - row_width/2.0;
68 /* deep enough not to be vissible */
69 verts[idx + 2] = grid_depth;
70 verts[idx + 3] = 0.0;
73 glVertexPointer( 3, GL_FLOAT, sizeof(verts[0])*4, verts );
76 static void generate_colors( void )
78 unsigned x, y;
79 GLfloat step = 255.0/(GLfloat)(nr_verts_in_row - 1);
80 color = malloc(sizeof(color[0]) * 4 * nr_verts_in_row * nr_verts_in_row);
82 for (y = 0; y < nr_verts_in_row; ++y) {
83 for (x = 0; x < nr_verts_in_row; ++x) {
84 unsigned idx = 4*(x + y * nr_verts_in_row);
85 color[idx + 0] = (GLubyte)(step * x);
86 color[idx + 1] = 0x00;
87 color[idx + 2] = (GLubyte)(step * y);
88 color[idx + 3] = 0x00;
91 glColorPointer( 4, GL_UNSIGNED_BYTE, 0, color );
94 static void generate_indices( void )
96 unsigned strip, i;
98 /* indice size * number of strips * number of indices in strip */
99 indices = malloc(sizeof(indices[0]) * (nr_verts_in_row - 1) *
100 (nr_indices_in_strip));
102 for (strip = 0; strip < nr_verts_in_row - 1; strip += 2) {
103 for (i = 0; i < nr_indices_in_strip; i+=2) {
104 unsigned idx = i + strip * nr_indices_in_strip;
105 unsigned idx2 = (nr_indices_in_strip - i - 2) + (strip +
106 1) * (nr_indices_in_strip);
107 indices[idx + 1] = i/2 + strip*nr_verts_in_row;
108 indices[idx] = i/2 + (strip + 1)*nr_verts_in_row;
109 if (strip + 1 < nr_verts_in_row - 1) {
110 indices[idx2] = i/2 + (strip + 1)*nr_verts_in_row;
111 indices[idx2 + 1] = i/2 + (strip + 2)*nr_verts_in_row;
117 static void init( void )
121 generate_verts();
122 generate_colors();
123 generate_indices();
125 glClearColor( 0.0, 0.0, 0.0, 0.0 );
126 glShadeModel( GL_SMOOTH );
128 glEnableClientState( GL_VERTEX_ARRAY );
129 glEnableClientState( GL_COLOR_ARRAY );
131 glMatrixMode( GL_PROJECTION );
132 glLoadIdentity();
133 glFrustum( -100.0, 100.0, -100.0, 100.0, 1.0, 100.0 );
134 glMatrixMode( GL_MODELVIEW );
135 glLoadIdentity();
137 #ifdef GL_EXT_compiled_vertex_array
138 if ( compiled ) {
139 glLockArraysEXT( 0, rows * rows );
141 #endif
144 static void display( void )
146 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
148 glDrawElements( GL_TRIANGLE_STRIP, nr_indices_in_strip * (nr_verts_in_row - 1) , GL_UNSIGNED_INT, indices );
150 if ( double_buffer )
151 glutSwapBuffers();
152 else
153 glFlush();
156 static void keyboard( unsigned char key, int x, int y )
158 switch ( key ) {
159 case 27:
160 exit( 0 );
161 break;
164 glutPostRedisplay();
167 static GLboolean args( int argc, char **argv )
169 GLint i;
171 double_buffer = GL_TRUE;
173 for ( i = 1 ; i < argc ; i++ ) {
174 if ( strcmp( argv[i], "-sb" ) == 0 ) {
175 double_buffer = GL_FALSE;
176 } else if ( strcmp( argv[i], "-db" ) == 0 ) {
177 double_buffer = GL_TRUE;
178 } else {
179 fprintf( stderr, "%s (Bad option).\n", argv[i] );
180 return GL_FALSE;
183 return GL_TRUE;
186 int main( int argc, char **argv )
188 GLenum type;
189 char *string;
190 double version;
192 glutInit( &argc, argv );
194 if ( args( argc, argv ) == GL_FALSE ) {
195 exit( 1 );
198 type = GLUT_RGB | GLUT_DEPTH;
199 type |= ( double_buffer ) ? GLUT_DOUBLE : GLUT_SINGLE;
201 glutInitDisplayMode( type );
202 glutInitWindowSize( 250, 250 );
203 glutInitWindowPosition( 100, 100 );
204 glutCreateWindow( "CVA Test" );
206 /* Make sure the server supports GL 1.2 vertex arrays.
208 string = (char *) glGetString( GL_VERSION );
210 version = atof(string);
211 if ( version < 1.2 ) {
212 fprintf( stderr, "This program requires OpenGL 1.2 vertex arrays.\n" );
213 exit( -1 );
216 /* See if the server supports compiled vertex arrays.
218 string = (char *) glGetString( GL_EXTENSIONS );
220 if ( !strstr( string, "GL_EXT_compiled_vertex_array" ) ) {
221 fprintf( stderr, "Compiled vertex arrays not supported by this renderer.\n" );
222 compiled = GL_FALSE;
225 init();
227 glutDisplayFunc( display );
228 glutKeyboardFunc( keyboard );
229 glutMainLoop();
231 return 0;