#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 );
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);
/**
unsigned texture_units = 1;
unsigned i;
unsigned j;
- unsigned vertex_program_attribs;
+ unsigned vertex_program_attribs = 0;
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;
* 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 )
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;
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 )
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;
}
/* 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;
}
}
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;
}
}
- arrays->array_info_cache_valid = GL_TRUE;
arrays->DrawArrays = emit_DrawArrays_old;
arrays->DrawElements = emit_DrawElements_old;
}
arrays->DrawArrays = emit_DrawArrays_none;
arrays->DrawElements = emit_DrawElements_none;
}
+
+ arrays->array_info_cache_valid = GL_TRUE;
}
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,
*(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;
(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;
}
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);
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;
pc = emit_DrawArrays_header_old( gc, arrays, & elements_per_request,
}
switch( type ) {
- case GL_UNSIGNED_INT:
+ case GL_UNSIGNED_INT: {
+ const GLuint * ui_ptr = (const GLuint *) indices;
+
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;
+
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;
+
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,
}
+/**
+ * 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();
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 );
}
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;
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 );
}
}
-#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) \
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;
__glXSetError(gc, GL_INVALID_VALUE);
return;
}
-
+
switch ( type ) {
case GL_BYTE: opcode = X_GLrop_Normal3bv; break;
case GL_SHORT: opcode = X_GLrop_Normal3sv; break;
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;
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;
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;
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;
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;
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;
__glXSetError(gc, GL_INVALID_VALUE);
return;
}
-
+
switch ( type ) {
case GL_FLOAT: opcode = 4124; break;
case GL_DOUBLE: opcode = 4125; break;
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;
true_immediate_count = 4;
}
else {
+ true_immediate_count = size;
+
switch( type ) {
case GL_BYTE:
opcode = X_GLrop_VertexAttrib4bvARB;
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
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 ];
}