libGL: Consolidate DRI initialization in dri_glx.c
[mesa.git] / src / glx / x11 / indirect_vertex_array.c
index fece228209871e992aa78392e73a4ad804399553..4f8284576e794864290c0a15e77531111ea51bd9 100644 (file)
 
 #include <inttypes.h>
 #include <assert.h>
-#include "glxclient.h"
-#include "packrender.h"
-#include "indirect.h"
 #include <string.h>
 
+#include "glxclient.h"
+#include "indirect.h"
 #include <GL/glxproto.h>
 #include "glxextensions.h"
 #include "indirect_vertex_array.h"
+#include "indirect_va_private.h"
+
+#define __GLX_PAD(n) (((n)+3) & ~3)
 
 /**
  * \file indirect_vertex_array.c
  * \author Ian Romanick <idr@us.ibm.com>
  */
 
-
-/**
- * State descriptor for a single array of vertex data.
- */
-struct array_state {
-    /**
-     * Pointer to the application supplied data.
-     */
-    const void * data;
-    
-    /**
-     * Enum representing the type of the application supplied data.
-     */
-    GLenum data_type;
-
-    /**
-     * Stride value supplied by the application.  This value is not used
-     * internally.  It is only kept so that it can be queried by the
-     * application using glGet*v.
-     */
-    GLsizei user_stride;
-
-    /**
-     * Calculated size, in bytes, of a single element in the array.  This
-     * is calculated based on \c count and the size of the data type
-     * represented by \c data_type.
-     */
-    GLsizei element_size;
-
-    /**
-     * Actual byte-stride from one element to the next.  This value will
-     * be equal to either \c user_stride or \c element_stride.
-     */
-    GLsizei true_stride;
-
-    /**
-     * Number of data values in each element.
-     */
-    GLint count;
-
-    /**
-     * "Normalized" data is on the range [0,1] (unsigned) or [-1,1] (signed).
-     * This is used for mapping integral types to floating point types.
-     */
-    GLboolean normalized;
-
-    /**
-     * Pre-calculated GLX protocol command header.
-     */
-    uint32_t header[2];
-    
-    /**
-     * Size of the header data.  For simple data, like glColorPointerfv,
-     * this is 4.  For complex data that requires either a count (e.g.,
-     * glWeightfvARB), an index (e.g., glVertexAttrib1fvARB), or a
-     * selector enum (e.g., glMultiTexCoord2fv) this is 8.
-     */
-    unsigned header_size;
-    
-    /**
-     * Set to \c GL_TRUE if this array is enabled.  Otherwise, it is set
-     * to \c GL_FALSE.
-     */
-    GLboolean  enabled;
-
-    /**
-     * For multi-arrayed data (e.g., texture coordinates, generic vertex
-     * program attributes, etc.), this specifies which array this is.
-     */
-    unsigned index;
-    
-    /**
-     * Per-array-type key.  For most arrays, this will be the GL enum for
-     * that array (e.g., GL_VERTEX_ARRAY for vertex data, GL_NORMAL_ARRAY
-     * for normal data, GL_TEXTURE_COORD_ARRAY for texture coordinate data,
-     * etc.).
-     */
-    GLenum key;
-
-    /**
-     * If this array can be used with the "classic" \c glDrawArrays protocol,
-     * this is set to \c GL_TRUE.  Otherwise, it is set to \c GL_FALSE.
-     */
-    GLboolean old_DrawArrays_possible;
-};
-
-
-/**
- * Array state that is pushed / poped by \c glPushClientAttrib and
- * \c glPopClientAttrib.
- */
-struct array_stack_state {
-    /**
-     * Pointer to the application supplied data.
-     */
-    const void * data;
-    
-    /**
-     * Enum representing the type of the application supplied data.
-     */
-    GLenum data_type;
-
-    /**
-     * Stride value supplied by the application.  This value is not used
-     * internally.  It is only kept so that it can be queried by the
-     * application using glGet*v.
-     */
-    GLsizei user_stride;
-
-    /**
-     * Number of data values in each element.
-     */
-    GLint count;
-
-    /**
-     * Per-array-type key.  For most arrays, this will be the GL enum for
-     * that array (e.g., GL_VERTEX_ARRAY for vertex data, GL_NORMAL_ARRAY
-     * for normal data, GL_TEXTURE_COORD_ARRAY for texture coordinate data,
-     * etc.).
-     */
-    GLenum key;
-
-    /**
-     * For multi-arrayed data (e.g., texture coordinates, generic vertex
-     * program attributes, etc.), this specifies which array this is.
-     */
-    unsigned index;
-
-    /**
-     * Set to \c GL_TRUE if this array is enabled.  Otherwise, it is set
-     * to \c GL_FALSE.
-     */
-    GLboolean  enabled;
-};
-
-
-/**
- * Collection of all the vertex array state.
- */
-struct array_state_vector {
-    /**
-     * Number of arrays tracked by \c ::arrays.
-     */
-    size_t num_arrays;
-
-    /**
-     * Array of vertex array state.  This array contains all of the valid
-     * vertex arrays.  If a vertex array isn't in this array, then it isn't
-     * valid.  For example, if an implementation does not support
-     * EXT_fog_coord, there won't be a GL_FOG_COORD_ARRAY entry in this
-     * array.
-     */
-    struct array_state * arrays;
-
-    /**
-     * Number of currently enabled arrays.  The value of this field is
-     * only valid if \c array_info_cache_valid is true.
-     */
-    size_t enabled_array_count;
-
-    /**
-     * \name ARRAY_INFO cache.
-     * 
-     * These fields track the state of the ARRAY_INFO cache.  The
-     * \c array_info_cache_size is the size of the actual data stored in
-     * \c array_info_cache.  \c array_info_cache_buffer_size is the size of
-     * the buffer.  This will always be greater than or equal to
-     * \c array_info_cache_size.
-     *
-     * \c large_header doesn't completely belong in this group.  This is a
-     * pointer to a buffer to hold the header information for DrawArrays in
-     * a RenderLarge command.  This buffer is immediately before
-     * \c array_info_cache.  The idea is that the header data will be written
-     * to \c large_header and a single call to \c __glXSendLargeChunk can be
-     * made to send the header and the ARRAY_INFO data.
-     * 
-     * \note
-     * \c array_info_cache_size and \c array_info_cache_buffer_size do
-     * NOT include the size of \c large_header.
-     */
-    /*@{*/
-    size_t array_info_cache_size;
-    size_t array_info_cache_buffer_size;
-    void * array_info_cache;
-    GLubyte * large_header;
-    /*@}*/
-
-
-    /**
-     * Is the cache of ARRAY_INFO data valid?  The cache can become invalid
-     * when one of several state changes occur.  Among these chages are
-     * modifying the array settings for an enabled array and enabling /
-     * disabling an array.
-     */
-    GLboolean array_info_cache_valid;
-
-    /**
-     * Is it possible to use the GL 1.1 / EXT_vertex_arrays protocol?  Use
-     * of this protocol is disabled with really old servers (i.e., servers
-     * that don't support GL 1.1 or EXT_vertex_arrays) or when an environment
-     * variable is set.
-     * 
-     * \todo
-     * GL 1.1 and EXT_vertex_arrays use identical protocol, but have different
-     * opcodes for \c glDrawArrays.  For servers that advertise one or the
-     * other, there should be a way to select which opcode to use.
-     */
-    GLboolean old_DrawArrays_possible;
-
-    /**
-     * Is it possible to use the new GL X.X / ARB_vertex_buffer_object
-     * protocol?
-     * 
-     * \todo
-     * This protocol has not yet been defined by the ARB, but is currently a
-     * work in progress.  This field is a place-holder.
-     */
-    GLboolean new_DrawArrays_possible;
-
-    /**
-     * Active texture unit set by \c glClientActiveTexture.
-     * 
-     * \sa __glXGetActiveTextureUnit
-     */
-    unsigned active_texture_unit;
-    
-    /**
-     * Number of supported texture units.  Even if ARB_multitexture /
-     * GL 1.3 are not supported, this will be at least 1.  When multitexture
-     * is supported, this will be the value queried by calling
-     * \c glGetIntegerv with \c GL_MAX_TEXTURE_UNITS.
-     * 
-     * \todo
-     * Investigate if this should be the value of \c GL_MAX_TEXTURE_COORDS
-     * instead (if GL 2.0 / ARB_fragment_shader / ARB_fragment_program /
-     * NV_fragment_program are supported).
-     */
-    unsigned num_texture_units;
-
-    /**
-     * Number of generic vertex program attribs.  If GL_ARB_vertex_program
-     * is not supported, this will be zero.  Otherwise it will be the value
-     * queries by calling \c glGetProgramiv with \c GL_VERTEX_PROGRAM_ARB
-     * and \c GL_MAX_PROGRAM_ATTRIBS_ARB.
-     */
-    unsigned num_vertex_program_attribs;
-
-    /**
-     * \n Methods for implementing various GL functions.
-     * 
-     * These method pointers are only valid \c array_info_cache_valid is set.
-     * When each function starts, it much check \c array_info_cache_valid.
-     * If it is not set, it must call \c fill_array_info_cache and call
-     * the new method.
-     * 
-     * \sa fill_array_info_cache
-     * 
-     * \todo
-     * Write code to plug these functions directly into the dispatch table.
-     */
-    /*@{*/
-    void (*DrawArrays)( GLenum, GLint, GLsizei );
-    void (*DrawElements)( GLenum mode, GLsizei count, GLenum type,
-                         const GLvoid *indices );
-    /*@}*/
-
-    struct array_stack_state * stack;
-    unsigned active_texture_unit_stack[ __GL_CLIENT_ATTRIB_STACK_DEPTH ];
-    unsigned stack_index;
-};
-
-
 static void emit_DrawArrays_none( GLenum mode, GLint first, GLsizei count );
 static void emit_DrawArrays_old ( GLenum mode, GLint first, GLsizei count );
 
@@ -353,6 +83,7 @@ static struct array_state * get_array_entry(
 static void fill_array_info_cache( struct array_state_vector * arrays );
 static GLboolean validate_mode(__GLXcontext *gc, GLenum mode);
 static GLboolean validate_count(__GLXcontext *gc, GLsizei count);
+static GLboolean validate_type(__GLXcontext *gc, GLenum type);
 
 
 /**
@@ -392,22 +123,15 @@ __glXInitVertexArrayState( __GLXcontext * gc )
     struct array_state_vector * arrays;
 
     unsigned array_count;
-    unsigned texture_units = 1;
-    unsigned i;
-    unsigned j;
-    unsigned vertex_program_attribs;
+    int texture_units = 1, vertex_program_attribs = 0;
+    unsigned i, j;
 
     GLboolean got_fog = GL_FALSE;
     GLboolean got_secondary_color = GL_FALSE;
 
 
-    arrays = malloc( sizeof( struct array_state_vector ) );
+    arrays = calloc( 1, sizeof( struct array_state_vector ) );
     state->array_state = arrays;
-    arrays->enabled_array_count = 0;
-    arrays->array_info_cache = NULL;
-    arrays->array_info_cache_size = 0;
-    arrays->array_info_cache_buffer_size = 0;
-    arrays->array_info_cache_valid= GL_FALSE;
 
     arrays->old_DrawArrays_possible = !state->NoDrawArraysProtocol;
     arrays->new_DrawArrays_possible = GL_FALSE;
@@ -425,7 +149,7 @@ __glXInitVertexArrayState( __GLXcontext * gc )
      * GL_COLOR_ARRAY, GL_INDEX_ARRAY, GL_TEXTURE_COORD_ARRAY, and
      * GL_EDGE_FLAG_ARRAY are supported.
      */
-    
+
     array_count = 5;
     
     if ( __glExtensionBitIsEnabled( gc, GL_EXT_fog_coord_bit )
@@ -442,26 +166,20 @@ __glXInitVertexArrayState( __GLXcontext * gc )
 
     if ( __glExtensionBitIsEnabled( gc, GL_ARB_multitexture_bit )
         || (gc->server_major > 1) || (gc->server_minor >= 3) ) {
-       glGetIntegerv( GL_MAX_TEXTURE_UNITS, & texture_units );
-    }
-    else {
-       texture_units = 1;
+       __indirect_glGetIntegerv( GL_MAX_TEXTURE_UNITS, & texture_units );
     }
 
     if ( __glExtensionBitIsEnabled( gc, GL_ARB_vertex_program_bit ) ) {
-       glGetProgramivARB( GL_VERTEX_PROGRAM_ARB, GL_MAX_PROGRAM_ATTRIBS_ARB,
-                          & vertex_program_attribs );
+       __indirect_glGetProgramivARB( GL_VERTEX_PROGRAM_ARB,
+                                     GL_MAX_PROGRAM_ATTRIBS_ARB,
+                                     & vertex_program_attribs );
     }
 
     arrays->num_texture_units = texture_units;
     arrays->num_vertex_program_attribs = vertex_program_attribs;
     array_count += texture_units + vertex_program_attribs;
     arrays->num_arrays = array_count;
-    arrays->arrays = malloc( sizeof( struct array_state ) * array_count );
-    
-    (void) memset( arrays->arrays, 0,
-                  sizeof( struct array_state ) * array_count );
-
+    arrays->arrays = calloc( array_count, sizeof( struct array_state ) );
 
     arrays->arrays[0].data_type = GL_FLOAT;
     arrays->arrays[0].count = 3;
@@ -542,6 +260,7 @@ __glXInitVertexArrayState( __GLXcontext * gc )
     arrays->arrays[i].key = GL_VERTEX_ARRAY;
     arrays->arrays[i].old_DrawArrays_possible = GL_TRUE;
 
+    assert( (i + 1) == arrays->num_arrays );
 
     arrays->stack_index = 0;
     arrays->stack = malloc( sizeof( struct array_stack_state )
@@ -659,15 +378,17 @@ static GLboolean
 allocate_array_info_cache( struct array_state_vector * arrays,
                           size_t required_size )
 {
+#define MAX_HEADER_SIZE 20
     if ( arrays->array_info_cache_buffer_size < required_size ) {
-       GLubyte * temp = realloc( arrays->array_info_cache, required_size + 20 );
+       GLubyte * temp = realloc( arrays->array_info_cache_base,
+                                 required_size + MAX_HEADER_SIZE );
 
        if ( temp == NULL ) {
            return GL_FALSE;
        }
 
-       arrays->large_header = temp;
-       arrays->array_info_cache = temp + 20;
+       arrays->array_info_cache_base = temp;
+       arrays->array_info_cache = temp + MAX_HEADER_SIZE;
        arrays->array_info_cache_buffer_size = required_size;
     }
 
@@ -688,21 +409,20 @@ fill_array_info_cache( struct array_state_vector * arrays )
     /* Determine how many arrays are enabled.
      */
 
-    arrays->enabled_array_count = 0;
+    arrays->enabled_client_array_count = 0;
     old_DrawArrays_possible = arrays->old_DrawArrays_possible;
     for ( i = 0 ; i < arrays->num_arrays ; i++ ) {
        if ( arrays->arrays[i].enabled ) {
-           arrays->enabled_array_count++;
+           arrays->enabled_client_array_count++;
            old_DrawArrays_possible &= arrays->arrays[i].old_DrawArrays_possible;
        }
     }
     
-    
     if ( arrays->new_DrawArrays_possible ) {
        assert( ! arrays->new_DrawArrays_possible );
     }
     else if ( old_DrawArrays_possible ) {
-       const size_t required_size = arrays->enabled_array_count * 12;
+       const size_t required_size = arrays->enabled_client_array_count * 12;
        uint32_t * info;
 
 
@@ -720,7 +440,6 @@ fill_array_info_cache( struct array_state_vector * arrays )
            }
        }
 
-        arrays->array_info_cache_valid = GL_TRUE;
        arrays->DrawArrays = emit_DrawArrays_old;
        arrays->DrawElements = emit_DrawElements_old;
     }
@@ -728,6 +447,8 @@ fill_array_info_cache( struct array_state_vector * arrays )
        arrays->DrawArrays = emit_DrawArrays_none;
        arrays->DrawElements = emit_DrawElements_none;
     }
+
+    arrays->array_info_cache_valid = GL_TRUE;
 }
 
 
@@ -764,14 +485,14 @@ emit_DrawArrays_none( GLenum mode, GLint first, GLsizei count )
 
     for ( i = 0 ; i < count ; i++ ) {
        if ( (pc + single_vertex_size) >= gc->bufEnd ) {
-           pc = __glXFlushRenderBuffer(gc, gc->pc);
+           pc = __glXFlushRenderBuffer(gc, pc);
        }
 
        pc = emit_element_none( pc, arrays, first + i );
     }
 
     if ( (pc + 4) >= gc->bufEnd ) {
-       pc = __glXFlushRenderBuffer(gc, gc->pc);
+       pc = __glXFlushRenderBuffer(gc, pc);
     }
 
     (void) memcpy( pc, end_cmd, 4 );
@@ -806,7 +527,7 @@ static GLubyte *
 emit_DrawArrays_header_old( __GLXcontext * gc,
                            struct array_state_vector * arrays,
                            size_t * elements_per_request,
-                           size_t * total_requests,
+                           unsigned int * total_requests,
                            GLenum mode, GLsizei count )
 {
     size_t command_size;
@@ -868,11 +589,11 @@ emit_DrawArrays_header_old( __GLXcontext * gc,
 
        command_size += 4;
 
-       pc = arrays->large_header;
+       pc = ((GLubyte *) arrays->array_info_cache) - (header_size + 4);
        *(uint32_t *)(pc +  0) = command_size;
        *(uint32_t *)(pc +  4) = X_GLrop_DrawArrays;
        *(uint32_t *)(pc +  8) = count;
-       *(uint32_t *)(pc + 12) = arrays->enabled_array_count;
+       *(uint32_t *)(pc + 12) = arrays->enabled_client_array_count;
        *(uint32_t *)(pc + 16) = mode;
 
        __glXSendLargeChunk( gc, 1, *total_requests, pc,
@@ -889,7 +610,7 @@ emit_DrawArrays_header_old( __GLXcontext * gc,
        *(uint16_t *)(pc +  0) = command_size;
        *(uint16_t *)(pc +  2) = X_GLrop_DrawArrays;
        *(uint32_t *)(pc +  4) = count;
-       *(uint32_t *)(pc +  8) = arrays->enabled_array_count;
+       *(uint32_t *)(pc +  8) = arrays->enabled_client_array_count;
        *(uint32_t *)(pc + 12) = mode;
 
        pc += header_size;
@@ -897,7 +618,7 @@ emit_DrawArrays_header_old( __GLXcontext * gc,
        (void) memcpy( pc, arrays->array_info_cache,
                       arrays->array_info_cache_size );
        pc += arrays->array_info_cache_size;
-       
+
        *elements_per_request = count;
        *total_requests = 0;
     }
@@ -1002,10 +723,10 @@ emit_DrawElements_none( GLenum mode, GLsizei count, GLenum type,
     pc += 8;
 
     for ( i = 0 ; i < count ; i++ ) {
-       unsigned  index;
+       unsigned  index = 0;
 
        if ( (pc + single_vertex_size) >= gc->bufEnd ) {
-           pc = __glXFlushRenderBuffer(gc, gc->pc);
+           pc = __glXFlushRenderBuffer(gc, pc);
        }
 
        switch( type ) {
@@ -1023,7 +744,7 @@ emit_DrawElements_none( GLenum mode, GLsizei count, GLenum type,
     }
 
     if ( (pc + 4) >= gc->bufEnd ) {
-       pc = __glXFlushRenderBuffer(gc, gc->pc);
+       pc = __glXFlushRenderBuffer(gc, pc);
     }
 
     (void) memcpy( pc, end_cmd, 4 );
@@ -1052,9 +773,7 @@ emit_DrawElements_old( GLenum mode, GLsizei count, GLenum type,
     unsigned total_requests = 0;
     unsigned i;
     unsigned req;
-    const GLuint   * ui_ptr = (const GLuint   *) indices;
-    const GLushort * us_ptr = (const GLushort *) indices;
-    const GLubyte  * ub_ptr = (const GLubyte  *) indices;
+    unsigned req_element=0;
 
 
     pc = emit_DrawArrays_header_old( gc, arrays, & elements_per_request,
@@ -1071,25 +790,34 @@ emit_DrawElements_old( GLenum mode, GLsizei count, GLenum type,
        }
 
        switch( type ) {
-       case GL_UNSIGNED_INT:
+       case GL_UNSIGNED_INT: {
+           const GLuint   * ui_ptr = (const GLuint   *) indices + req_element;
+
            for ( i = 0 ; i < elements_per_request ; i++ ) {
                const GLint index = (GLint) *(ui_ptr++);
                pc = emit_element_old( pc, arrays, index );
            }
            break;
-       case GL_UNSIGNED_SHORT:
+       }
+       case GL_UNSIGNED_SHORT: {
+           const GLushort * us_ptr = (const GLushort *) indices + req_element;
+
            for ( i = 0 ; i < elements_per_request ; i++ ) {
                const GLint index = (GLint) *(us_ptr++);
                pc = emit_element_old( pc, arrays, index );
            }
            break;
-       case GL_UNSIGNED_BYTE:
+       }
+       case GL_UNSIGNED_BYTE: {
+           const GLubyte  * ub_ptr = (const GLubyte  *) indices + req_element;
+
            for ( i = 0 ; i < elements_per_request ; i++ ) {
                const GLint index = (GLint) *(ub_ptr++);
                pc = emit_element_old( pc, arrays, index );
            }
            break;
        }
+       }
 
        if ( total_requests != 0 ) {
            __glXSendLargeChunk( gc, req, total_requests, gc->pc,
@@ -1099,6 +827,7 @@ emit_DrawElements_old( GLenum mode, GLsizei count, GLenum type,
        }
 
        count -= elements_per_request;
+       req_element += elements_per_request;
     }
 
 
@@ -1166,6 +895,28 @@ validate_count(__GLXcontext *gc, GLsizei count)
 }
 
 
+/**
+ * Validate that the \c type parameter to \c glDrawElements, et. al. is
+ * valid.  Only \c GL_UNSIGNED_BYTE, \c GL_UNSIGNED_SHORT, and
+ * \c GL_UNSIGNED_INT are valid.
+ *
+ * \returns
+ * \c GL_TRUE if the argument is valid, \c GL_FALSE if it is not.
+ */
+static GLboolean validate_type(__GLXcontext *gc, GLenum type)
+{
+    switch( type ) {
+    case GL_UNSIGNED_INT:
+    case GL_UNSIGNED_SHORT:
+    case GL_UNSIGNED_BYTE:
+       return GL_TRUE;
+     default:
+       __glXSetError(gc, GL_INVALID_ENUM);
+       return GL_FALSE;
+    }
+}
+
+
 void __indirect_glDrawArrays(GLenum mode, GLint first, GLsizei count)
 {
     __GLXcontext *gc = __glXGetCurrentContext();
@@ -1217,7 +968,8 @@ void __indirect_glDrawElements(GLenum mode, GLsizei count, GLenum type,
     struct array_state_vector * arrays = state->array_state;
 
 
-    if ( validate_mode(gc, mode) && validate_count(gc, count)  ) {
+    if ( validate_mode(gc, mode) && validate_count(gc, count)
+        && validate_type(gc, type) ) {
        if ( ! arrays->array_info_cache_valid ) {
            fill_array_info_cache( arrays );
        }
@@ -1237,7 +989,8 @@ void __indirect_glDrawRangeElements(GLenum mode, GLuint start, GLuint end,
     struct array_state_vector * arrays = state->array_state;
 
 
-    if ( validate_mode(gc, mode) && validate_count(gc, count) ) {
+    if ( validate_mode(gc, mode) && validate_count(gc, count)
+        && validate_type(gc, type) ) {
        if (end < start) {
            __glXSetError(gc, GL_INVALID_VALUE);
            return;
@@ -1287,7 +1040,7 @@ void __indirect_glMultiDrawElementsEXT(GLenum mode, const GLsizei *count,
     GLsizei  i;
 
 
-    if ( validate_mode(gc, mode) ) {
+    if ( validate_mode(gc, mode) && validate_type(gc, type) ) {
        if ( ! arrays->array_info_cache_valid ) {
            fill_array_info_cache( arrays );
        }
@@ -1301,12 +1054,13 @@ void __indirect_glMultiDrawElementsEXT(GLenum mode, const GLsizei *count,
 }
 
 
-#define COMMON_ARRAY_DATA_INIT(a, PTR, TYPE, STRIDE, COUNT, HDR_SIZE, OPCODE) \
+#define COMMON_ARRAY_DATA_INIT(a, PTR, TYPE, STRIDE, COUNT, NORMALIZED, HDR_SIZE, OPCODE) \
     do { \
        (a)->data = PTR; \
        (a)->data_type = TYPE; \
        (a)->user_stride = STRIDE; \
        (a)->count = COUNT; \
+       (a)->normalized = NORMALIZED; \
        \
        (a)->element_size = __glXTypeSize( TYPE ) * COUNT; \
        (a)->true_stride = (STRIDE == 0) \
@@ -1357,7 +1111,8 @@ void __indirect_glVertexPointer( GLint size, GLenum type, GLsizei stride,
 
     a = get_array_entry( arrays, GL_VERTEX_ARRAY, 0 );
     assert( a != NULL );
-    COMMON_ARRAY_DATA_INIT( a, pointer, type, stride, size, 4, opcode );
+    COMMON_ARRAY_DATA_INIT( a, pointer, type, stride, size, GL_FALSE, 4,
+                           opcode );
 
     if ( a->enabled ) {
        arrays->array_info_cache_valid = GL_FALSE;
@@ -1379,7 +1134,7 @@ void __indirect_glNormalPointer( GLenum type, GLsizei stride,
         __glXSetError(gc, GL_INVALID_VALUE);
         return;
     }
-    
+
     switch ( type ) {
     case GL_BYTE:      opcode = X_GLrop_Normal3bv; break;
     case GL_SHORT:     opcode = X_GLrop_Normal3sv; break;
@@ -1393,7 +1148,8 @@ void __indirect_glNormalPointer( GLenum type, GLsizei stride,
 
     a = get_array_entry( arrays, GL_NORMAL_ARRAY, 0 );
     assert( a != NULL );
-    COMMON_ARRAY_DATA_INIT( a, pointer, type, stride, 3, 4, opcode );
+    COMMON_ARRAY_DATA_INIT( a, pointer, type, stride, 3, GL_TRUE, 4,
+                           opcode );
 
     if ( a->enabled ) {
        arrays->array_info_cache_valid = GL_FALSE;
@@ -1456,7 +1212,8 @@ void __indirect_glColorPointer( GLint size, GLenum type, GLsizei stride,
 
     a = get_array_entry( arrays, GL_COLOR_ARRAY, 0 );
     assert( a != NULL );
-    COMMON_ARRAY_DATA_INIT( a, pointer, type, stride, size, 4, opcode );
+    COMMON_ARRAY_DATA_INIT( a, pointer, type, stride, size, GL_TRUE, 4,
+                           opcode );
 
     if ( a->enabled ) {
        arrays->array_info_cache_valid = GL_FALSE;
@@ -1492,7 +1249,8 @@ void __indirect_glIndexPointer( GLenum type, GLsizei stride,
 
     a = get_array_entry( arrays, GL_INDEX_ARRAY, 0 );
     assert( a != NULL );
-    COMMON_ARRAY_DATA_INIT( a, pointer, type, stride, 1, 4, opcode );
+    COMMON_ARRAY_DATA_INIT( a, pointer, type, stride, 1, GL_FALSE, 4,
+                           opcode );
 
     if ( a->enabled ) {
        arrays->array_info_cache_valid = GL_FALSE;
@@ -1516,7 +1274,8 @@ void __indirect_glEdgeFlagPointer( GLsizei stride, const GLvoid * pointer )
 
     a = get_array_entry( arrays, GL_EDGE_FLAG_ARRAY, 0 );
     assert( a != NULL );
-    COMMON_ARRAY_DATA_INIT( a, pointer, GL_UNSIGNED_BYTE, stride, 1, 4, X_GLrop_EdgeFlagv );
+    COMMON_ARRAY_DATA_INIT( a, pointer, GL_UNSIGNED_BYTE, stride, 1, GL_FALSE,
+                           4, X_GLrop_EdgeFlagv );
 
     if ( a->enabled ) {
        arrays->array_info_cache_valid = GL_FALSE;
@@ -1597,7 +1356,8 @@ void __indirect_glTexCoordPointer( GLint size, GLenum type, GLsizei stride,
 
     a = get_array_entry( arrays, GL_TEXTURE_COORD_ARRAY, index );
     assert( a != NULL );
-    COMMON_ARRAY_DATA_INIT( a, pointer, type, stride, size, header_size, opcode );
+    COMMON_ARRAY_DATA_INIT( a, pointer, type, stride, size, GL_FALSE,
+                           header_size, opcode );
 
     if ( a->enabled ) {
        arrays->array_info_cache_valid = GL_FALSE;
@@ -1640,7 +1400,8 @@ void __indirect_glSecondaryColorPointerEXT( GLint size, GLenum type, GLsizei str
         return;
     }
 
-    COMMON_ARRAY_DATA_INIT( a, pointer, type, stride, size, 4, opcode );
+    COMMON_ARRAY_DATA_INIT( a, pointer, type, stride, size, GL_TRUE, 4,
+                           opcode );
 
     if ( a->enabled ) {
        arrays->array_info_cache_valid = GL_FALSE;
@@ -1662,7 +1423,7 @@ void __indirect_glFogCoordPointerEXT( GLenum type, GLsizei stride,
         __glXSetError(gc, GL_INVALID_VALUE);
         return;
     }
-    
+
     switch ( type ) {
     case GL_FLOAT:             opcode = 4124; break;
     case GL_DOUBLE:            opcode = 4125; break;
@@ -1677,7 +1438,8 @@ void __indirect_glFogCoordPointerEXT( GLenum type, GLsizei stride,
         return;
     }
 
-    COMMON_ARRAY_DATA_INIT( a, pointer, type, stride, 1, 4, opcode );
+    COMMON_ARRAY_DATA_INIT( a, pointer, type, stride, 1, GL_FALSE, 4,
+                           opcode );
 
     if ( a->enabled ) {
        arrays->array_info_cache_valid = GL_FALSE;
@@ -1725,6 +1487,8 @@ void __indirect_glVertexAttribPointerARB(GLuint index, GLint size,
        true_immediate_count = 4;
     }
     else {
+       true_immediate_count = size;
+
        switch( type ) {
        case GL_BYTE:
            opcode = X_GLrop_VertexAttrib4bvARB;
@@ -1767,7 +1531,8 @@ void __indirect_glVertexAttribPointerARB(GLuint index, GLint size,
         return;
     }
 
-    COMMON_ARRAY_DATA_INIT( a, pointer, type, stride, size, 8, opcode );
+    COMMON_ARRAY_DATA_INIT( a, pointer, type, stride, size, normalized, 8,
+                           opcode );
 
     true_immediate_size = __glXTypeSize(type) * true_immediate_count;
     ((uint16_t *) (a)->header)[0] = __GLX_PAD(a->header_size 
@@ -1826,7 +1591,7 @@ void __indirect_glClientActiveTextureARB(GLenum texture)
     const GLint unit = (GLint) texture - GL_TEXTURE0;
 
 
-    if ( (unit < 0) || (unit > arrays->num_texture_units) ) {
+    if ( (unit < 0) || (unit >= arrays->num_texture_units) ) {
        __glXSetError(gc, GL_INVALID_ENUM);
        return;
     }
@@ -1904,7 +1669,7 @@ __glXGetArrayType( const __GLXattribute * const state,
                                                    key, index );
 
     if ( a != NULL ) {
-       *dest = (GLintptr) a->enabled;
+       *dest = (GLintptr) a->data_type;
     }
 
     return (a != NULL);
@@ -2001,13 +1766,14 @@ __glXPushArrayState( __GLXattribute * state )
     struct array_stack_state * stack = & arrays->stack[ (arrays->stack_index * arrays->num_arrays)];
     unsigned  i;
 
-
+    /* XXX are we pushing _all_ the necessary fields? */
     for ( i = 0 ; i < arrays->num_arrays ; i++ ) {
        stack[i].data = arrays->arrays[i].data;
        stack[i].data_type = arrays->arrays[i].data_type;
        stack[i].user_stride = arrays->arrays[i].user_stride;
        stack[i].count = arrays->arrays[i].count;
        stack[i].key = arrays->arrays[i].key;
+        stack[i].index = arrays->arrays[i].index;
        stack[i].enabled = arrays->arrays[i].enabled;
     }
 
@@ -2076,5 +1842,6 @@ __glXPopArrayState( __GLXattribute * state )
                             stack[i].enabled );
     }
 
-    arrays->active_texture_unit = arrays->active_texture_unit_stack[ arrays->stack_index ];
+    arrays->active_texture_unit = 
+      arrays->active_texture_unit_stack[ arrays->stack_index ];
 }