1/* 2 * Copyright © 2010 Pauli Nieminen 3 * 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: 10 * 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. 14 * 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. 22 */ 23 24 25/* 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. 30 */ 31 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" 41 42GLfloat *verts; 43 44GLubyte *color; 45 46GLuint *indices; 47 48#define rows 1000 /* Create 1000x1000 vertex grid */ 49#define row_width 5000.0 50#define grid_depth -50.0 51GLuint nr_verts_in_row = rows; 52GLuint nr_indices_in_strip = rows * 2; 53 54GLboolean double_buffer; 55GLboolean compiled = GL_TRUE; 56 57static void generate_verts( void ) 58{ 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); 62 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; 71 } 72 } 73 glVertexPointer( 3, GL_FLOAT, sizeof(verts[0])*4, verts ); 74} 75 76static void generate_colors( void ) 77{ 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); 81 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; 89 } 90 } 91 glColorPointer( 4, GL_UNSIGNED_BYTE, 0, color ); 92} 93 94static void generate_indices( void ) 95{ 96 unsigned strip, i; 97 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)); 101 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; 112 } 113 } 114 } 115} 116 117static void init( void ) 118{ 119 120 121 generate_verts(); 122 generate_colors(); 123 generate_indices(); 124 125 glClearColor( 0.0, 0.0, 0.0, 0.0 ); 126 glShadeModel( GL_SMOOTH ); 127 128 glEnableClientState( GL_VERTEX_ARRAY ); 129 glEnableClientState( GL_COLOR_ARRAY ); 130 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(); 136 137#ifdef GL_EXT_compiled_vertex_array 138 if ( compiled ) { 139 glLockArraysEXT( 0, rows * rows ); 140 } 141#endif 142} 143 144static void display( void ) 145{ 146 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); 147 148 glDrawElements( GL_TRIANGLE_STRIP, nr_indices_in_strip * (nr_verts_in_row - 1) , GL_UNSIGNED_INT, indices ); 149 150 if ( double_buffer ) 151 glutSwapBuffers(); 152 else 153 glFlush(); 154} 155 156static void keyboard( unsigned char key, int x, int y ) 157{ 158 switch ( key ) { 159 case 27: 160 exit( 0 ); 161 break; 162 } 163 164 glutPostRedisplay(); 165} 166 167static GLboolean args( int argc, char **argv ) 168{ 169 GLint i; 170 171 double_buffer = GL_TRUE; 172 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; 181 } 182 } 183 return GL_TRUE; 184} 185 186int main( int argc, char **argv ) 187{ 188 GLenum type; 189 char *string; 190 double version; 191 192 glutInit( &argc, argv ); 193 194 if ( args( argc, argv ) == GL_FALSE ) { 195 exit( 1 ); 196 } 197 198 type = GLUT_RGB | GLUT_DEPTH; 199 type |= ( double_buffer ) ? GLUT_DOUBLE : GLUT_SINGLE; 200 201 glutInitDisplayMode( type ); 202 glutInitWindowSize( 250, 250 ); 203 glutInitWindowPosition( 100, 100 ); 204 glutCreateWindow( "CVA Test" ); 205 206 /* Make sure the server supports GL 1.2 vertex arrays. 207 */ 208 string = (char *) glGetString( GL_VERSION ); 209 210 version = atof(string); 211 if ( version < 1.2 ) { 212 fprintf( stderr, "This program requires OpenGL 1.2 vertex arrays.\n" ); 213 exit( -1 ); 214 } 215 216 /* See if the server supports compiled vertex arrays. 217 */ 218 string = (char *) glGetString( GL_EXTENSIONS ); 219 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; 223 } 224 225 init(); 226 227 glutDisplayFunc( display ); 228 glutKeyboardFunc( keyboard ); 229 glutMainLoop(); 230 231 return 0; 232} 233