libGL: Consolidate DRI initialization in dri_glx.c
[mesa.git] / src / glx / x11 / indirect_vertex_array.c
index 8bc7e449021294047005c0f6dd88c0db9ea7d8cc..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;
-
-    /**
-     * 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;
-
-    /**
-     * \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 );
 
@@ -337,8 +81,9 @@ static GLubyte * emit_element_old( GLubyte * dst,
 static struct array_state * get_array_entry(
     const struct array_state_vector * arrays, GLenum key, unsigned index );
 static void fill_array_info_cache( struct array_state_vector * arrays );
-static GLboolean glx_validate_array_args(__GLXcontext *gc, GLenum mode,
-    GLsizei count);
+static GLboolean validate_mode(__GLXcontext *gc, GLenum mode);
+static GLboolean validate_count(__GLXcontext *gc, GLsizei count);
+static GLboolean validate_type(__GLXcontext *gc, GLenum type);
 
 
 /**
@@ -378,20 +123,15 @@ __glXInitVertexArrayState( __GLXcontext * gc )
     struct array_state_vector * arrays;
 
     unsigned array_count;
-    unsigned texture_units = 1;
-    unsigned i;
+    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;
@@ -409,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 )
@@ -426,29 +166,31 @@ __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 );
+       __indirect_glGetIntegerv( GL_MAX_TEXTURE_UNITS, & texture_units );
     }
-    else {
-       texture_units = 1;
+
+    if ( __glExtensionBitIsEnabled( gc, GL_ARB_vertex_program_bit ) ) {
+       __indirect_glGetProgramivARB( GL_VERTEX_PROGRAM_ARB,
+                                     GL_MAX_PROGRAM_ATTRIBS_ARB,
+                                     & vertex_program_attribs );
     }
 
     arrays->num_texture_units = texture_units;
-    array_count += 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;
     arrays->arrays[0].key = GL_NORMAL_ARRAY;
+    arrays->arrays[0].normalized = GL_TRUE;
     arrays->arrays[0].old_DrawArrays_possible = GL_TRUE;
 
     arrays->arrays[1].data_type = GL_FLOAT;
     arrays->arrays[1].count = 4;
     arrays->arrays[1].key = GL_COLOR_ARRAY;
+    arrays->arrays[1].normalized = GL_TRUE;
     arrays->arrays[1].old_DrawArrays_possible = GL_TRUE;
 
     arrays->arrays[2].data_type = GL_FLOAT;
@@ -465,7 +207,7 @@ __glXInitVertexArrayState( __GLXcontext * gc )
        arrays->arrays[4 + i].data_type = GL_FLOAT;
        arrays->arrays[4 + i].count = 4;
        arrays->arrays[4 + i].key = GL_TEXTURE_COORD_ARRAY;
-       
+
        arrays->arrays[4 + i].old_DrawArrays_possible = (i == 0);
        arrays->arrays[4 + i].index = i;
 
@@ -487,10 +229,28 @@ __glXInitVertexArrayState( __GLXcontext * gc )
        arrays->arrays[i].count = 3;
        arrays->arrays[i].key = GL_SECONDARY_COLOR_ARRAY;
        arrays->arrays[i].old_DrawArrays_possible = GL_TRUE;
+       arrays->arrays[i].normalized = GL_TRUE;
        i++;
     }
 
 
+    for ( j = 0 ; j < vertex_program_attribs ; j++ ) {
+       const unsigned idx = (vertex_program_attribs - (j + 1));
+
+
+       arrays->arrays[idx + i].data_type = GL_FLOAT;
+       arrays->arrays[idx + i].count = 4;
+       arrays->arrays[idx + i].key = GL_VERTEX_ATTRIB_ARRAY_POINTER;
+
+       arrays->arrays[idx + i].old_DrawArrays_possible = 0;
+       arrays->arrays[idx + i].index = idx;
+
+       arrays->arrays[idx + i].header[1] = idx;
+    }
+
+    i += vertex_program_attribs;
+
+
     /* Vertex array *must* be last becuase of the way that
      * emit_DrawArrays_none works.
      */
@@ -500,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 )
@@ -521,8 +282,7 @@ calculate_single_vertex_size_none( const struct array_state_vector * arrays )
 
     for ( i = 0 ; i < arrays->num_arrays ; i++ ) {
        if ( arrays->arrays[i].enabled ) {
-           single_vertex_size += __GLX_PAD(arrays->arrays[i].element_size)
-             + arrays->arrays[i].header_size;
+           single_vertex_size += ((uint16_t *)arrays->arrays[i].header)[0];
        }
     }
     
@@ -545,6 +305,15 @@ emit_element_none( GLubyte * dst,
        if ( arrays->arrays[i].enabled ) {
            const size_t offset = index * arrays->arrays[i].true_stride;
 
+           /* The generic attributes can have more data than is in the
+            * elements.  This is because a vertex array can be a 2 element,
+            * normalized, unsigned short, but the "closest" immediate mode
+            * protocol is for a 4Nus.  Since the sizes are small, the
+            * performance impact on modern processors should be negligible.
+            */
+           (void) memset( dst, 0,
+                          ((uint16_t *)arrays->arrays[i].header)[0] );
+
            (void) memcpy( dst, arrays->arrays[i].header, 
                           arrays->arrays[i].header_size );
 
@@ -609,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;
     }
 
@@ -638,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;
 
 
@@ -670,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;
     }
@@ -678,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;
 }
 
 
@@ -714,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 );
@@ -756,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;
@@ -818,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,
@@ -839,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;
@@ -847,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;
     }
@@ -952,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 ) {
@@ -973,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 );
@@ -1002,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,
@@ -1021,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,
@@ -1049,6 +827,7 @@ emit_DrawElements_old( GLenum mode, GLsizei count, GLenum type,
        }
 
        count -= elements_per_request;
+       req_element += elements_per_request;
     }
 
 
@@ -1116,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();
@@ -1167,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 );
        }
@@ -1187,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;
@@ -1237,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 );
        }
@@ -1251,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) \
@@ -1307,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;
@@ -1329,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;
@@ -1343,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;
@@ -1406,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;
@@ -1442,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;
@@ -1466,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;
@@ -1547,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;
@@ -1590,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;
@@ -1612,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;
@@ -1627,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;
@@ -1635,6 +1447,142 @@ void __indirect_glFogCoordPointerEXT( GLenum type, GLsizei stride,
 }
 
 
+void __indirect_glVertexAttribPointerARB(GLuint index, GLint size,
+                                        GLenum type, GLboolean normalized,
+                                        GLsizei stride,
+                                        const GLvoid * pointer)
+{
+    static const uint16_t short_ops[5]  = { 0, 4189, 4190, 4191, 4192 };
+    static const uint16_t float_ops[5]  = { 0, 4193, 4194, 4195, 4196 };
+    static const uint16_t double_ops[5] = { 0, 4197, 4198, 4199, 4200 };
+
+    uint16_t opcode;
+    __GLXcontext *gc = __glXGetCurrentContext();
+    __GLXattribute * state = (__GLXattribute *)(gc->client_state_private);
+    struct array_state_vector * arrays = state->array_state;
+    struct array_state * a;
+    unsigned true_immediate_count;
+    unsigned true_immediate_size;
+
+
+    if ( (size < 1) || (size > 4) || (stride < 0) 
+        || (index > arrays->num_vertex_program_attribs) ){
+        __glXSetError(gc, GL_INVALID_VALUE);
+        return;
+    }
+
+    if ( normalized && (type != GL_FLOAT) && (type != GL_DOUBLE)) {
+       switch( type ) {
+       case GL_BYTE:           opcode = X_GLrop_VertexAttrib4NbvARB;  break;
+       case GL_UNSIGNED_BYTE:  opcode = X_GLrop_VertexAttrib4NubvARB; break;
+       case GL_SHORT:          opcode = X_GLrop_VertexAttrib4NsvARB;  break;
+       case GL_UNSIGNED_SHORT: opcode = X_GLrop_VertexAttrib4NusvARB; break;
+       case GL_INT:            opcode = X_GLrop_VertexAttrib4NivARB;  break;
+       case GL_UNSIGNED_INT:   opcode = X_GLrop_VertexAttrib4NuivARB; break;
+       default:
+           __glXSetError(gc, GL_INVALID_ENUM);
+           return;
+       }
+       
+       true_immediate_count = 4;
+    }
+    else {
+       true_immediate_count = size;
+
+       switch( type ) {
+       case GL_BYTE:
+           opcode = X_GLrop_VertexAttrib4bvARB;
+           true_immediate_count = 4;
+           break;
+       case GL_UNSIGNED_BYTE:
+           opcode = X_GLrop_VertexAttrib4ubvARB;
+           true_immediate_count = 4;
+           break;
+       case GL_SHORT:
+           opcode = short_ops[size];
+           break;
+       case GL_UNSIGNED_SHORT:
+           opcode = X_GLrop_VertexAttrib4usvARB;
+           true_immediate_count = 4;
+           break;
+       case GL_INT:
+            opcode = X_GLrop_VertexAttrib4ivARB;
+           true_immediate_count = 4;
+           break;
+       case GL_UNSIGNED_INT:
+           opcode = X_GLrop_VertexAttrib4uivARB;
+           true_immediate_count = 4;
+           break;
+       case GL_FLOAT:
+           opcode = float_ops[size];
+           break;
+       case GL_DOUBLE:
+           opcode = double_ops[size];
+           break;
+       default:
+           __glXSetError(gc, GL_INVALID_ENUM);
+           return;
+       }
+    }
+
+    a = get_array_entry( arrays, GL_VERTEX_ATTRIB_ARRAY_POINTER, index );
+    if ( a == NULL ) {
+        __glXSetError(gc, GL_INVALID_OPERATION);
+        return;
+    }
+
+    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 
+                                             + true_immediate_size);
+
+    if ( a->enabled ) {
+       arrays->array_info_cache_valid = GL_FALSE;
+    }
+}
+
+
+/**
+ * I don't have 100% confidence that this is correct.  The different rules
+ * about whether or not generic vertex attributes alias "classic" vertex
+ * attributes (i.e., attrib1 ?= primary color) between ARB_vertex_program,
+ * ARB_vertex_shader, and NV_vertex_program are a bit confusing.  My
+ * feeling is that the client-side doesn't have to worry about it.  The
+ * client just sends all the data to the server and lets the server deal
+ * with it.
+ */
+void __indirect_glVertexAttribPointerNV( GLuint index, GLint size,
+                                        GLenum type, GLsizei stride,
+                                        const GLvoid * pointer)
+{
+    __GLXcontext *gc = __glXGetCurrentContext();
+    GLboolean normalized = GL_FALSE;
+
+
+    switch( type ) {
+    case GL_UNSIGNED_BYTE:
+       if ( size != 4 ) {
+           __glXSetError(gc, GL_INVALID_VALUE);
+           return;
+       }
+       normalized = GL_TRUE;
+
+    case GL_SHORT:
+    case GL_FLOAT:
+    case GL_DOUBLE:
+       __indirect_glVertexAttribPointerARB(index, size, type,
+                                           normalized,
+                                           stride, pointer);
+       return;
+    default:
+       __glXSetError(gc, GL_INVALID_ENUM);
+       return;
+    }
+}
+
+
 void __indirect_glClientActiveTextureARB(GLenum texture)
 {
     __GLXcontext * const gc = __glXGetCurrentContext();
@@ -1643,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;
     }
@@ -1721,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);
@@ -1776,7 +1724,26 @@ __glXGetArrayPointer( const __GLXattribute * const state,
 
 
     if ( a != NULL ) {
-       *dest = a->data;
+       *dest = (void *) (a->data);
+    }
+
+    return (a != NULL);
+}
+
+
+/**
+ */
+GLboolean
+__glXGetArrayNormalized( const __GLXattribute * const state,
+                        GLenum key, unsigned index, GLintptr * dest )
+{
+    const struct array_state_vector * arrays = state->array_state;
+    const struct array_state * a = get_array_entry( (struct array_state_vector *) arrays,
+                                                   key, index );
+
+
+    if ( a != NULL ) {
+       *dest = (GLintptr) a->normalized;
     }
 
     return (a != NULL);
@@ -1799,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;
     }
 
@@ -1874,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 ];
 }