📄 indirect_vertex_array.c
字号:
/* * (C) Copyright IBM Corporation 2004, 2005 * All Rights Reserved. * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sub license, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice (including the next * paragraph) shall be included in all copies or substantial portions of the * Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL * IBM, * AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */#include <inttypes.h>#include <assert.h>#include <string.h>#include "glxclient.h"#include "indirect.h"#include <GL/glxproto.h>#include "glxextensions.h"#include "indirect_vertex_array.h"#include "indirect_vertex_array_priv.h"#define __GLX_PAD(n) (((n)+3) & ~3)/** * \file indirect_vertex_array.c * Implement GLX protocol for vertex arrays and vertex buffer objects. * * The most important function in this fill is \c fill_array_info_cache. * The \c array_state_vector contains a cache of the ARRAY_INFO data sent * in the DrawArrays protocol. Certain operations, such as enabling or * disabling an array, can invalidate this cache. \c fill_array_info_cache * fills-in this data. Additionally, it examines the enabled state and * other factors to determine what "version" of DrawArrays protocoal can be * used. * * Current, only two versions of DrawArrays protocol are implemented. The * first version is the "none" protocol. This is the fallback when the * server does not support GL 1.1 / EXT_vertex_arrays. It is implemented * by sending batches of immediate mode commands that are equivalent to the * DrawArrays protocol. * * The other protocol that is currently implemented is the "old" protocol. * This is the GL 1.1 DrawArrays protocol. The only difference between GL * 1.1 and EXT_vertex_arrays is the opcode used for the DrawArrays command. * This protocol is called "old" because the ARB is in the process of * defining a new protocol, which will probably be called wither "new" or * "vbo", to support multiple texture coordinate arrays, generic attributes, * and vertex buffer objects. * * \author Ian Romanick <ian.d.romanick@intel.com> */static void emit_DrawArrays_none( GLenum mode, GLint first, GLsizei count );static void emit_DrawArrays_old ( GLenum mode, GLint first, GLsizei count );static void emit_DrawElements_none( GLenum mode, GLsizei count, GLenum type, const GLvoid *indices );static void emit_DrawElements_old ( GLenum mode, GLsizei count, GLenum type, const GLvoid *indices );static GLubyte * emit_element_none( GLubyte * dst, const struct array_state_vector * arrays, unsigned index );static GLubyte * emit_element_old( GLubyte * dst, const struct array_state_vector * arrays, unsigned index );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 validate_mode(__GLXcontext *gc, GLenum mode);static GLboolean validate_count(__GLXcontext *gc, GLsizei count);static GLboolean validate_type(__GLXcontext *gc, GLenum type);/** * Table of sizes, in bytes, of a GL types. All of the type enums are be in * the range 0x1400 - 0x140F. That includes types added by extensions (i.e., * \c GL_HALF_FLOAT_NV). This elements of this table correspond to the * type enums masked with 0x0f. * * \notes * \c GL_HALF_FLOAT_NV is not included. Neither are \c GL_2_BYTES, * \c GL_3_BYTES, or \c GL_4_BYTES. */const GLuint __glXTypeSize_table[16] = { 1, 1, 2, 2, 4, 4, 4, 0, 0, 0, 8, 0, 0, 0, 0, 0};/** * Free the per-context array state that was allocated with * __glXInitVertexArrayState(). */void__glXFreeVertexArrayState( __GLXcontext * gc ){ __GLXattribute * state = (__GLXattribute *)(gc->client_state_private); struct array_state_vector* arrays = state->array_state; if (arrays) { if (arrays->stack) { free(arrays->stack); arrays->stack = NULL; } if (arrays->arrays) { free(arrays->arrays); arrays->arrays = NULL; } free(arrays); arrays = NULL; state->array_state = NULL; }}/** * Initialize vertex array state of a GLX context. * * \param gc GLX context whose vertex array state is to be initialized. * * \warning * This function may only be called after __GLXcontext::gl_extension_bits, * __GLXcontext::server_minor, and __GLXcontext::server_major have been * initialized. These values are used to determine what vertex arrays are * supported. * * \bug * Return values from malloc are not properly tested. */void__glXInitVertexArrayState( __GLXcontext * gc ){ __GLXattribute * state = (__GLXattribute *)(gc->client_state_private); struct array_state_vector * arrays; unsigned array_count; int texture_units = 1, vertex_program_attribs = 0; unsigned i, j; GLboolean got_fog = GL_FALSE; GLboolean got_secondary_color = GL_FALSE; arrays = calloc( 1, sizeof( struct array_state_vector ) ); state->array_state = arrays; arrays->old_DrawArrays_possible = !state->NoDrawArraysProtocol; arrays->new_DrawArrays_possible = GL_FALSE; arrays->DrawArrays = NULL; arrays->active_texture_unit = 0; /* Determine how many arrays are actually needed. Only arrays that * are supported by the server are create. For example, if the server * supports only 2 texture units, then only 2 texture coordinate arrays * are created. * * At the very least, GL_VERTEX_ARRAY, GL_NORMAL_ARRAY, * 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 ) || (gc->server_major > 1) || (gc->server_minor >= 4) ) { got_fog = GL_TRUE; array_count++; } if ( __glExtensionBitIsEnabled( gc, GL_EXT_secondary_color_bit ) || (gc->server_major > 1) || (gc->server_minor >= 4) ) { got_secondary_color = GL_TRUE; array_count++; } if ( __glExtensionBitIsEnabled( gc, GL_ARB_multitexture_bit ) || (gc->server_major > 1) || (gc->server_minor >= 3) ) { __indirect_glGetIntegerv( GL_MAX_TEXTURE_UNITS, & texture_units ); } 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; arrays->num_vertex_program_attribs = vertex_program_attribs; array_count += texture_units + vertex_program_attribs; arrays->num_arrays = 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; arrays->arrays[2].count = 1; arrays->arrays[2].key = GL_INDEX_ARRAY; arrays->arrays[2].old_DrawArrays_possible = GL_TRUE; arrays->arrays[3].data_type = GL_UNSIGNED_BYTE; arrays->arrays[3].count = 1; arrays->arrays[3].key = GL_EDGE_FLAG_ARRAY; arrays->arrays[3].old_DrawArrays_possible = GL_TRUE; for ( i = 0 ; i < texture_units ; i++ ) { 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; arrays->arrays[4 + i].header[1] = i + GL_TEXTURE0; } i = 4 + texture_units; if ( got_fog ) { arrays->arrays[i].data_type = GL_FLOAT; arrays->arrays[i].count = 1; arrays->arrays[i].key = GL_FOG_COORDINATE_ARRAY; arrays->arrays[i].old_DrawArrays_possible = GL_TRUE; i++; } if ( got_secondary_color ) { arrays->arrays[i].data_type = GL_FLOAT; 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. */ arrays->arrays[i].data_type = GL_FLOAT; arrays->arrays[i].count = 4; 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 ) * arrays->num_arrays );}/** * Calculate the size of a single vertex for the "none" protocol. This is * essentially the size of all the immediate-mode commands required to * implement the enabled vertex arrays. */static size_tcalculate_single_vertex_size_none( const struct array_state_vector * arrays ){ size_t single_vertex_size = 0; unsigned i; for ( i = 0 ; i < arrays->num_arrays ; i++ ) { if ( arrays->arrays[i].enabled ) { single_vertex_size += ((uint16_t *)arrays->arrays[i].header)[0]; } } return single_vertex_size;}/** * Emit a single element using non-DrawArrays protocol. */GLubyte *emit_element_none( GLubyte * dst, const struct array_state_vector * arrays, unsigned index ){ unsigned i; for ( i = 0 ; i < arrays->num_arrays ; i++ ) { 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 ); dst += arrays->arrays[i].header_size; (void) memcpy( dst, ((GLubyte *) arrays->arrays[i].data) + offset, arrays->arrays[i].element_size ); dst += __GLX_PAD( arrays->arrays[i].element_size ); } } return dst;}/** * Emit a single element using "old" DrawArrays protocol from * EXT_vertex_arrays / OpenGL 1.1. */GLubyte *emit_element_old( GLubyte * dst, const struct array_state_vector * arrays, unsigned index ){ unsigned i; for ( i = 0 ; i < arrays->num_arrays ; i++ ) { if ( arrays->arrays[i].enabled ) { const size_t offset = index * arrays->arrays[i].true_stride; (void) memcpy( dst, ((GLubyte *) arrays->arrays[i].data) + offset, arrays->arrays[i].element_size ); dst += __GLX_PAD( arrays->arrays[i].element_size ); } } return dst;}struct array_state *get_array_entry( const struct array_state_vector * arrays, GLenum key, unsigned index ){ unsigned i; for ( i = 0 ; i < arrays->num_arrays ; i++ ) { if ( (arrays->arrays[i].key == key) && (arrays->arrays[i].index == index) ) { return & arrays->arrays[i]; } } return NULL;}static GLbooleanallocate_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_base, required_size + MAX_HEADER_SIZE ); if ( temp == NULL ) { return GL_FALSE; } arrays->array_info_cache_base = temp; arrays->array_info_cache = temp + MAX_HEADER_SIZE; arrays->array_info_cache_buffer_size = required_size; } arrays->array_info_cache_size = required_size; return GL_TRUE;}/** */voidfill_array_info_cache( struct array_state_vector * arrays ){ GLboolean old_DrawArrays_possible; unsigned i; /* Determine how many arrays are enabled. */ 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_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_client_array_count * 12; uint32_t * info; if ( ! allocate_array_info_cache( arrays, required_size ) ) { return; } info = (uint32_t *) arrays->array_info_cache; for ( i = 0 ; i < arrays->num_arrays ; i++ ) { if ( arrays->arrays[i].enabled ) { *(info++) = arrays->arrays[i].data_type; *(info++) = arrays->arrays[i].count; *(info++) = arrays->arrays[i].key; } } arrays->DrawArrays = emit_DrawArrays_old; arrays->DrawElements = emit_DrawElements_old; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -