Test sizeof(drm_handle_t) instead of LONG64 when returning handles
[mesa.git] / src / glx / x11 / indirect_vertex_array.c
1 /*
2 * (C) Copyright IBM Corporation 2004, 2005
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sub license,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * IBM,
20 * AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
22 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 */
25
26 #include <inttypes.h>
27 #include <assert.h>
28 #include <string.h>
29
30 #include "glxclient.h"
31 #include "indirect.h"
32 #include <GL/glxproto.h>
33 #include "glxextensions.h"
34 #include "indirect_vertex_array.h"
35 #include "indirect_va_private.h"
36
37 #define __GLX_PAD(n) (((n)+3) & ~3)
38
39 /**
40 * \file indirect_vertex_array.c
41 * Implement GLX protocol for vertex arrays and vertex buffer objects.
42 *
43 * The most important function in this fill is \c fill_array_info_cache.
44 * The \c array_state_vector contains a cache of the ARRAY_INFO data sent
45 * in the DrawArrays protocol. Certain operations, such as enabling or
46 * disabling an array, can invalidate this cache. \c fill_array_info_cache
47 * fills-in this data. Additionally, it examines the enabled state and
48 * other factors to determine what "version" of DrawArrays protocoal can be
49 * used.
50 *
51 * Current, only two versions of DrawArrays protocol are implemented. The
52 * first version is the "none" protocol. This is the fallback when the
53 * server does not support GL 1.1 / EXT_vertex_arrays. It is implemented
54 * by sending batches of immediate mode commands that are equivalent to the
55 * DrawArrays protocol.
56 *
57 * The other protocol that is currently implemented is the "old" protocol.
58 * This is the GL 1.1 DrawArrays protocol. The only difference between GL
59 * 1.1 and EXT_vertex_arrays is the opcode used for the DrawArrays command.
60 * This protocol is called "old" because the ARB is in the process of
61 * defining a new protocol, which will probably be called wither "new" or
62 * "vbo", to support multiple texture coordinate arrays, generic attributes,
63 * and vertex buffer objects.
64 *
65 * \author Ian Romanick <idr@us.ibm.com>
66 */
67
68 static void emit_DrawArrays_none( GLenum mode, GLint first, GLsizei count );
69 static void emit_DrawArrays_old ( GLenum mode, GLint first, GLsizei count );
70
71 static void emit_DrawElements_none( GLenum mode, GLsizei count, GLenum type,
72 const GLvoid *indices );
73 static void emit_DrawElements_old ( GLenum mode, GLsizei count, GLenum type,
74 const GLvoid *indices );
75
76
77 static GLubyte * emit_element_none( GLubyte * dst,
78 const struct array_state_vector * arrays, unsigned index );
79 static GLubyte * emit_element_old( GLubyte * dst,
80 const struct array_state_vector * arrays, unsigned index );
81 static struct array_state * get_array_entry(
82 const struct array_state_vector * arrays, GLenum key, unsigned index );
83 static void fill_array_info_cache( struct array_state_vector * arrays );
84 static GLboolean validate_mode(__GLXcontext *gc, GLenum mode);
85 static GLboolean validate_count(__GLXcontext *gc, GLsizei count);
86 static GLboolean validate_type(__GLXcontext *gc, GLenum type);
87
88
89 /**
90 * Table of sizes, in bytes, of a GL types. All of the type enums are be in
91 * the range 0x1400 - 0x140F. That includes types added by extensions (i.e.,
92 * \c GL_HALF_FLOAT_NV). This elements of this table correspond to the
93 * type enums masked with 0x0f.
94 *
95 * \notes
96 * \c GL_HALF_FLOAT_NV is not included. Neither are \c GL_2_BYTES,
97 * \c GL_3_BYTES, or \c GL_4_BYTES.
98 */
99 const GLuint __glXTypeSize_table[16] = {
100 1, 1, 2, 2, 4, 4, 4, 0, 0, 0, 8, 0, 0, 0, 0, 0
101 };
102
103
104
105 /**
106 * Initialize vertex array state of a GLX context.
107 *
108 * \param gc GLX context whose vertex array state is to be initialized.
109 *
110 * \warning
111 * This function may only be called after __GLXcontext::gl_extension_bits,
112 * __GLXcontext::server_minor, and __GLXcontext::server_major have been
113 * initialized. These values are used to determine what vertex arrays are
114 * supported.
115 *
116 * \bug
117 * Return values from malloc are not properly tested.
118 */
119 void
120 __glXInitVertexArrayState( __GLXcontext * gc )
121 {
122 __GLXattribute * state = (__GLXattribute *)(gc->client_state_private);
123 struct array_state_vector * arrays;
124
125 unsigned array_count;
126 unsigned texture_units = 1;
127 unsigned i;
128 unsigned j;
129 unsigned vertex_program_attribs = 0;
130
131 GLboolean got_fog = GL_FALSE;
132 GLboolean got_secondary_color = GL_FALSE;
133
134
135 arrays = calloc( 1, sizeof( struct array_state_vector ) );
136 state->array_state = arrays;
137
138 arrays->old_DrawArrays_possible = !state->NoDrawArraysProtocol;
139 arrays->new_DrawArrays_possible = GL_FALSE;
140 arrays->DrawArrays = NULL;
141
142 arrays->active_texture_unit = 0;
143
144
145 /* Determine how many arrays are actually needed. Only arrays that
146 * are supported by the server are create. For example, if the server
147 * supports only 2 texture units, then only 2 texture coordinate arrays
148 * are created.
149 *
150 * At the very least, GL_VERTEX_ARRAY, GL_NORMAL_ARRAY,
151 * GL_COLOR_ARRAY, GL_INDEX_ARRAY, GL_TEXTURE_COORD_ARRAY, and
152 * GL_EDGE_FLAG_ARRAY are supported.
153 */
154
155 array_count = 5;
156
157 if ( __glExtensionBitIsEnabled( gc, GL_EXT_fog_coord_bit )
158 || (gc->server_major > 1) || (gc->server_minor >= 4) ) {
159 got_fog = GL_TRUE;
160 array_count++;
161 }
162
163 if ( __glExtensionBitIsEnabled( gc, GL_EXT_secondary_color_bit )
164 || (gc->server_major > 1) || (gc->server_minor >= 4) ) {
165 got_secondary_color = GL_TRUE;
166 array_count++;
167 }
168
169 if ( __glExtensionBitIsEnabled( gc, GL_ARB_multitexture_bit )
170 || (gc->server_major > 1) || (gc->server_minor >= 3) ) {
171 __indirect_glGetIntegerv( GL_MAX_TEXTURE_UNITS, & texture_units );
172 }
173
174 if ( __glExtensionBitIsEnabled( gc, GL_ARB_vertex_program_bit ) ) {
175 __indirect_glGetProgramivARB( GL_VERTEX_PROGRAM_ARB,
176 GL_MAX_PROGRAM_ATTRIBS_ARB,
177 & vertex_program_attribs );
178 }
179
180 arrays->num_texture_units = texture_units;
181 arrays->num_vertex_program_attribs = vertex_program_attribs;
182 array_count += texture_units + vertex_program_attribs;
183 arrays->num_arrays = array_count;
184 arrays->arrays = calloc( array_count, sizeof( struct array_state ) );
185
186 arrays->arrays[0].data_type = GL_FLOAT;
187 arrays->arrays[0].count = 3;
188 arrays->arrays[0].key = GL_NORMAL_ARRAY;
189 arrays->arrays[0].normalized = GL_TRUE;
190 arrays->arrays[0].old_DrawArrays_possible = GL_TRUE;
191
192 arrays->arrays[1].data_type = GL_FLOAT;
193 arrays->arrays[1].count = 4;
194 arrays->arrays[1].key = GL_COLOR_ARRAY;
195 arrays->arrays[1].normalized = GL_TRUE;
196 arrays->arrays[1].old_DrawArrays_possible = GL_TRUE;
197
198 arrays->arrays[2].data_type = GL_FLOAT;
199 arrays->arrays[2].count = 1;
200 arrays->arrays[2].key = GL_INDEX_ARRAY;
201 arrays->arrays[2].old_DrawArrays_possible = GL_TRUE;
202
203 arrays->arrays[3].data_type = GL_UNSIGNED_BYTE;
204 arrays->arrays[3].count = 1;
205 arrays->arrays[3].key = GL_EDGE_FLAG_ARRAY;
206 arrays->arrays[3].old_DrawArrays_possible = GL_TRUE;
207
208 for ( i = 0 ; i < texture_units ; i++ ) {
209 arrays->arrays[4 + i].data_type = GL_FLOAT;
210 arrays->arrays[4 + i].count = 4;
211 arrays->arrays[4 + i].key = GL_TEXTURE_COORD_ARRAY;
212
213 arrays->arrays[4 + i].old_DrawArrays_possible = (i == 0);
214 arrays->arrays[4 + i].index = i;
215
216 arrays->arrays[4 + i].header[1] = i + GL_TEXTURE0;
217 }
218
219 i = 4 + texture_units;
220
221 if ( got_fog ) {
222 arrays->arrays[i].data_type = GL_FLOAT;
223 arrays->arrays[i].count = 1;
224 arrays->arrays[i].key = GL_FOG_COORDINATE_ARRAY;
225 arrays->arrays[i].old_DrawArrays_possible = GL_TRUE;
226 i++;
227 }
228
229 if ( got_secondary_color ) {
230 arrays->arrays[i].data_type = GL_FLOAT;
231 arrays->arrays[i].count = 3;
232 arrays->arrays[i].key = GL_SECONDARY_COLOR_ARRAY;
233 arrays->arrays[i].old_DrawArrays_possible = GL_TRUE;
234 arrays->arrays[i].normalized = GL_TRUE;
235 i++;
236 }
237
238
239 for ( j = 0 ; j < vertex_program_attribs ; j++ ) {
240 const unsigned idx = (vertex_program_attribs - (j + 1));
241
242
243 arrays->arrays[idx + i].data_type = GL_FLOAT;
244 arrays->arrays[idx + i].count = 4;
245 arrays->arrays[idx + i].key = GL_VERTEX_ATTRIB_ARRAY_POINTER;
246
247 arrays->arrays[idx + i].old_DrawArrays_possible = 0;
248 arrays->arrays[idx + i].index = idx;
249
250 arrays->arrays[idx + i].header[1] = idx;
251 }
252
253 i += vertex_program_attribs;
254
255
256 /* Vertex array *must* be last becuase of the way that
257 * emit_DrawArrays_none works.
258 */
259
260 arrays->arrays[i].data_type = GL_FLOAT;
261 arrays->arrays[i].count = 4;
262 arrays->arrays[i].key = GL_VERTEX_ARRAY;
263 arrays->arrays[i].old_DrawArrays_possible = GL_TRUE;
264
265 assert( (i + 1) == arrays->num_arrays );
266
267 arrays->stack_index = 0;
268 arrays->stack = malloc( sizeof( struct array_stack_state )
269 * arrays->num_arrays );
270 }
271
272
273 /**
274 * Calculate the size of a single vertex for the "none" protocol. This is
275 * essentially the size of all the immediate-mode commands required to
276 * implement the enabled vertex arrays.
277 */
278 static size_t
279 calculate_single_vertex_size_none( const struct array_state_vector * arrays )
280 {
281 size_t single_vertex_size = 0;
282 unsigned i;
283
284
285 for ( i = 0 ; i < arrays->num_arrays ; i++ ) {
286 if ( arrays->arrays[i].enabled ) {
287 single_vertex_size += ((uint16_t *)arrays->arrays[i].header)[0];
288 }
289 }
290
291 return single_vertex_size;
292 }
293
294
295 /**
296 * Emit a single element using non-DrawArrays protocol.
297 */
298 GLubyte *
299 emit_element_none( GLubyte * dst,
300 const struct array_state_vector * arrays,
301 unsigned index )
302 {
303 unsigned i;
304
305
306 for ( i = 0 ; i < arrays->num_arrays ; i++ ) {
307 if ( arrays->arrays[i].enabled ) {
308 const size_t offset = index * arrays->arrays[i].true_stride;
309
310 /* The generic attributes can have more data than is in the
311 * elements. This is because a vertex array can be a 2 element,
312 * normalized, unsigned short, but the "closest" immediate mode
313 * protocol is for a 4Nus. Since the sizes are small, the
314 * performance impact on modern processors should be negligible.
315 */
316 (void) memset( dst, 0,
317 ((uint16_t *)arrays->arrays[i].header)[0] );
318
319 (void) memcpy( dst, arrays->arrays[i].header,
320 arrays->arrays[i].header_size );
321
322 dst += arrays->arrays[i].header_size;
323
324 (void) memcpy( dst, ((GLubyte *) arrays->arrays[i].data) + offset,
325 arrays->arrays[i].element_size );
326
327 dst += __GLX_PAD( arrays->arrays[i].element_size );
328 }
329 }
330
331 return dst;
332 }
333
334
335 /**
336 * Emit a single element using "old" DrawArrays protocol from
337 * EXT_vertex_arrays / OpenGL 1.1.
338 */
339 GLubyte *
340 emit_element_old( GLubyte * dst,
341 const struct array_state_vector * arrays,
342 unsigned index )
343 {
344 unsigned i;
345
346
347 for ( i = 0 ; i < arrays->num_arrays ; i++ ) {
348 if ( arrays->arrays[i].enabled ) {
349 const size_t offset = index * arrays->arrays[i].true_stride;
350
351 (void) memcpy( dst, ((GLubyte *) arrays->arrays[i].data) + offset,
352 arrays->arrays[i].element_size );
353
354 dst += __GLX_PAD( arrays->arrays[i].element_size );
355 }
356 }
357
358 return dst;
359 }
360
361
362 struct array_state *
363 get_array_entry( const struct array_state_vector * arrays,
364 GLenum key, unsigned index )
365 {
366 unsigned i;
367
368 for ( i = 0 ; i < arrays->num_arrays ; i++ ) {
369 if ( (arrays->arrays[i].key == key)
370 && (arrays->arrays[i].index == index) ) {
371 return & arrays->arrays[i];
372 }
373 }
374
375 return NULL;
376 }
377
378
379 static GLboolean
380 allocate_array_info_cache( struct array_state_vector * arrays,
381 size_t required_size )
382 {
383 #define MAX_HEADER_SIZE 20
384 if ( arrays->array_info_cache_buffer_size < required_size ) {
385 GLubyte * temp = realloc( arrays->array_info_cache_base,
386 required_size + MAX_HEADER_SIZE );
387
388 if ( temp == NULL ) {
389 return GL_FALSE;
390 }
391
392 arrays->array_info_cache_base = temp;
393 arrays->array_info_cache = temp + MAX_HEADER_SIZE;
394 arrays->array_info_cache_buffer_size = required_size;
395 }
396
397 arrays->array_info_cache_size = required_size;
398 return GL_TRUE;
399 }
400
401
402 /**
403 */
404 void
405 fill_array_info_cache( struct array_state_vector * arrays )
406 {
407 GLboolean old_DrawArrays_possible;
408 unsigned i;
409
410
411 /* Determine how many arrays are enabled.
412 */
413
414 arrays->enabled_client_array_count = 0;
415 old_DrawArrays_possible = arrays->old_DrawArrays_possible;
416 for ( i = 0 ; i < arrays->num_arrays ; i++ ) {
417 if ( arrays->arrays[i].enabled ) {
418 arrays->enabled_client_array_count++;
419 old_DrawArrays_possible &= arrays->arrays[i].old_DrawArrays_possible;
420 }
421 }
422
423
424 if ( arrays->new_DrawArrays_possible ) {
425 assert( ! arrays->new_DrawArrays_possible );
426 }
427 else if ( old_DrawArrays_possible ) {
428 const size_t required_size = arrays->enabled_client_array_count * 12;
429 uint32_t * info;
430
431
432 if ( ! allocate_array_info_cache( arrays, required_size ) ) {
433 return;
434 }
435
436
437 info = (uint32_t *) arrays->array_info_cache;
438 for ( i = 0 ; i < arrays->num_arrays ; i++ ) {
439 if ( arrays->arrays[i].enabled ) {
440 *(info++) = arrays->arrays[i].data_type;
441 *(info++) = arrays->arrays[i].count;
442 *(info++) = arrays->arrays[i].key;
443 }
444 }
445
446 arrays->DrawArrays = emit_DrawArrays_old;
447 arrays->DrawElements = emit_DrawElements_old;
448 }
449 else {
450 arrays->DrawArrays = emit_DrawArrays_none;
451 arrays->DrawElements = emit_DrawElements_none;
452 }
453
454 arrays->array_info_cache_valid = GL_TRUE;
455 }
456
457
458 /**
459 * Emit a \c glDrawArrays command using the "none" protocol. That is,
460 * emit immediate-mode commands that are equivalent to the requiested
461 * \c glDrawArrays command. This is used with servers that don't support
462 * the OpenGL 1.1 / EXT_vertex_arrays DrawArrays protocol or in cases where
463 * vertex state is enabled that is not compatible with that protocol.
464 */
465 void
466 emit_DrawArrays_none( GLenum mode, GLint first, GLsizei count )
467 {
468 __GLXcontext *gc = __glXGetCurrentContext();
469 const __GLXattribute * state =
470 (const __GLXattribute *)(gc->client_state_private);
471 struct array_state_vector * arrays = state->array_state;
472
473 size_t single_vertex_size;
474 GLubyte * pc;
475 unsigned i;
476 static const uint16_t begin_cmd[2] = { 8, X_GLrop_Begin };
477 static const uint16_t end_cmd[2] = { 4, X_GLrop_End };
478
479
480 single_vertex_size = calculate_single_vertex_size_none( arrays );
481
482 pc = gc->pc;
483
484 (void) memcpy( pc, begin_cmd, 4 );
485 *(int *)(pc + 4) = mode;
486
487 pc += 8;
488
489 for ( i = 0 ; i < count ; i++ ) {
490 if ( (pc + single_vertex_size) >= gc->bufEnd ) {
491 pc = __glXFlushRenderBuffer(gc, gc->pc);
492 }
493
494 pc = emit_element_none( pc, arrays, first + i );
495 }
496
497 if ( (pc + 4) >= gc->bufEnd ) {
498 pc = __glXFlushRenderBuffer(gc, gc->pc);
499 }
500
501 (void) memcpy( pc, end_cmd, 4 );
502 pc += 4;
503
504 gc->pc = pc;
505 if ( gc->pc > gc->limit ) {
506 (void) __glXFlushRenderBuffer(gc, gc->pc);
507 }
508 }
509
510
511 /**
512 * Emit the header data for the GL 1.1 / EXT_vertex_arrays DrawArrays
513 * protocol.
514 *
515 * \param gc GLX context.
516 * \param arrays Array state.
517 * \param elements_per_request Location to store the number of elements that
518 * can fit in a single Render / RenderLarge
519 * command.
520 * \param total_request Total number of requests for a RenderLarge
521 * command. If a Render command is used, this
522 * will be zero.
523 * \param mode Drawing mode.
524 * \param count Number of vertices.
525 *
526 * \returns
527 * A pointer to the buffer for array data.
528 */
529 static GLubyte *
530 emit_DrawArrays_header_old( __GLXcontext * gc,
531 struct array_state_vector * arrays,
532 size_t * elements_per_request,
533 size_t * total_requests,
534 GLenum mode, GLsizei count )
535 {
536 size_t command_size;
537 size_t single_vertex_size;
538 const unsigned header_size = 16;
539 unsigned i;
540 GLubyte * pc;
541
542
543 /* Determine the size of the whole command. This includes the header,
544 * the ARRAY_INFO data and the array data. Once this size is calculated,
545 * it will be known whether a Render or RenderLarge command is needed.
546 */
547
548 single_vertex_size = 0;
549 for ( i = 0 ; i < arrays->num_arrays ; i++ ) {
550 if ( arrays->arrays[i].enabled ) {
551 single_vertex_size += __GLX_PAD( arrays->arrays[i].element_size );
552 }
553 }
554
555 command_size = arrays->array_info_cache_size + header_size
556 + (single_vertex_size * count);
557
558
559 /* Write the header for either a Render command or a RenderLarge
560 * command. After the header is written, write the ARRAY_INFO data.
561 */
562
563 if ( command_size > gc->maxSmallRenderCommandSize ) {
564 /* maxSize is the maximum amount of data can be stuffed into a single
565 * packet. sz_xGLXRenderReq is added because bufSize is the maximum
566 * packet size minus sz_xGLXRenderReq.
567 */
568 const size_t maxSize = (gc->bufSize + sz_xGLXRenderReq)
569 - sz_xGLXRenderLargeReq;
570 unsigned vertex_requests;
571
572
573 /* Calculate the number of data packets that will be required to send
574 * the whole command. To do this, the number of verticies that
575 * will fit in a single buffer must be calculated.
576 *
577 * The important value here is elements_per_request. This is the
578 * number of complete array elements that will fit in a single
579 * buffer. There may be some wasted space at the end of the buffer,
580 * but splitting elements across buffer boundries would be painful.
581 */
582
583 elements_per_request[0] = maxSize / single_vertex_size;
584
585 vertex_requests = (count + elements_per_request[0] - 1)
586 / elements_per_request[0];
587
588 *total_requests = vertex_requests + 1;
589
590
591 __glXFlushRenderBuffer(gc, gc->pc);
592
593 command_size += 4;
594
595 pc = ((GLubyte *) arrays->array_info_cache) - (header_size + 4);
596 *(uint32_t *)(pc + 0) = command_size;
597 *(uint32_t *)(pc + 4) = X_GLrop_DrawArrays;
598 *(uint32_t *)(pc + 8) = count;
599 *(uint32_t *)(pc + 12) = arrays->enabled_client_array_count;
600 *(uint32_t *)(pc + 16) = mode;
601
602 __glXSendLargeChunk( gc, 1, *total_requests, pc,
603 header_size + 4 + arrays->array_info_cache_size );
604
605 pc = gc->pc;
606 }
607 else {
608 if ( (gc->pc + command_size) >= gc->bufEnd ) {
609 (void) __glXFlushRenderBuffer(gc, gc->pc);
610 }
611
612 pc = gc->pc;
613 *(uint16_t *)(pc + 0) = command_size;
614 *(uint16_t *)(pc + 2) = X_GLrop_DrawArrays;
615 *(uint32_t *)(pc + 4) = count;
616 *(uint32_t *)(pc + 8) = arrays->enabled_client_array_count;
617 *(uint32_t *)(pc + 12) = mode;
618
619 pc += header_size;
620
621 (void) memcpy( pc, arrays->array_info_cache,
622 arrays->array_info_cache_size );
623 pc += arrays->array_info_cache_size;
624
625 *elements_per_request = count;
626 *total_requests = 0;
627 }
628
629
630 return pc;
631 }
632
633
634 /**
635 */
636 void
637 emit_DrawArrays_old( GLenum mode, GLint first, GLsizei count )
638 {
639 __GLXcontext *gc = __glXGetCurrentContext();
640 const __GLXattribute * state =
641 (const __GLXattribute *)(gc->client_state_private);
642 struct array_state_vector * arrays = state->array_state;
643
644 GLubyte * pc;
645 size_t elements_per_request;
646 unsigned total_requests = 0;
647 unsigned i;
648 size_t total_sent = 0;
649
650
651 pc = emit_DrawArrays_header_old( gc, arrays, & elements_per_request,
652 & total_requests, mode, count);
653
654
655 /* Write the arrays.
656 */
657
658 if ( total_requests == 0 ) {
659 assert( elements_per_request >= count );
660
661 for ( i = 0 ; i < count ; i++ ) {
662 pc = emit_element_old( pc, arrays, i + first );
663 }
664
665 assert( pc <= gc->bufEnd );
666
667 gc->pc = pc;
668 if ( gc->pc > gc->limit ) {
669 (void) __glXFlushRenderBuffer(gc, gc->pc);
670 }
671 }
672 else {
673 unsigned req;
674
675
676 for ( req = 2 ; req <= total_requests ; req++ ) {
677 if ( count < elements_per_request ) {
678 elements_per_request = count;
679 }
680
681 pc = gc->pc;
682 for ( i = 0 ; i < elements_per_request ; i++ ) {
683 pc = emit_element_old( pc, arrays, i + first );
684 }
685
686 first += elements_per_request;
687
688 total_sent += (size_t) (pc - gc->pc);
689 __glXSendLargeChunk( gc, req, total_requests, gc->pc,
690 pc - gc->pc );
691
692 count -= elements_per_request;
693 }
694 }
695 }
696
697
698 void
699 emit_DrawElements_none( GLenum mode, GLsizei count, GLenum type,
700 const GLvoid *indices )
701 {
702 __GLXcontext *gc = __glXGetCurrentContext();
703 const __GLXattribute * state =
704 (const __GLXattribute *)(gc->client_state_private);
705 struct array_state_vector * arrays = state->array_state;
706 static const uint16_t begin_cmd[2] = { 8, X_GLrop_Begin };
707 static const uint16_t end_cmd[2] = { 4, X_GLrop_End };
708
709 GLubyte * pc;
710 size_t single_vertex_size;
711 unsigned i;
712
713
714 single_vertex_size = calculate_single_vertex_size_none( arrays );
715
716
717 if ( (gc->pc + single_vertex_size) >= gc->bufEnd ) {
718 gc->pc = __glXFlushRenderBuffer(gc, gc->pc);
719 }
720
721 pc = gc->pc;
722
723 (void) memcpy( pc, begin_cmd, 4 );
724 *(int *)(pc + 4) = mode;
725
726 pc += 8;
727
728 for ( i = 0 ; i < count ; i++ ) {
729 unsigned index = 0;
730
731 if ( (pc + single_vertex_size) >= gc->bufEnd ) {
732 pc = __glXFlushRenderBuffer(gc, gc->pc);
733 }
734
735 switch( type ) {
736 case GL_UNSIGNED_INT:
737 index = (unsigned) (((GLuint *) indices)[i]);
738 break;
739 case GL_UNSIGNED_SHORT:
740 index = (unsigned) (((GLushort *) indices)[i]);
741 break;
742 case GL_UNSIGNED_BYTE:
743 index = (unsigned) (((GLubyte *) indices)[i]);
744 break;
745 }
746 pc = emit_element_none( pc, arrays, index );
747 }
748
749 if ( (pc + 4) >= gc->bufEnd ) {
750 pc = __glXFlushRenderBuffer(gc, gc->pc);
751 }
752
753 (void) memcpy( pc, end_cmd, 4 );
754 pc += 4;
755
756 gc->pc = pc;
757 if ( gc->pc > gc->limit ) {
758 (void) __glXFlushRenderBuffer(gc, gc->pc);
759 }
760 }
761
762
763 /**
764 */
765 void
766 emit_DrawElements_old( GLenum mode, GLsizei count, GLenum type,
767 const GLvoid *indices )
768 {
769 __GLXcontext *gc = __glXGetCurrentContext();
770 const __GLXattribute * state =
771 (const __GLXattribute *)(gc->client_state_private);
772 struct array_state_vector * arrays = state->array_state;
773
774 GLubyte * pc;
775 size_t elements_per_request;
776 unsigned total_requests = 0;
777 unsigned i;
778 unsigned req;
779
780
781 pc = emit_DrawArrays_header_old( gc, arrays, & elements_per_request,
782 & total_requests, mode, count);
783
784
785 /* Write the arrays.
786 */
787
788 req = 2;
789 while ( count > 0 ) {
790 if ( count < elements_per_request ) {
791 elements_per_request = count;
792 }
793
794 switch( type ) {
795 case GL_UNSIGNED_INT: {
796 const GLuint * ui_ptr = (const GLuint *) indices;
797
798 for ( i = 0 ; i < elements_per_request ; i++ ) {
799 const GLint index = (GLint) *(ui_ptr++);
800 pc = emit_element_old( pc, arrays, index );
801 }
802 break;
803 }
804 case GL_UNSIGNED_SHORT: {
805 const GLushort * us_ptr = (const GLushort *) indices;
806
807 for ( i = 0 ; i < elements_per_request ; i++ ) {
808 const GLint index = (GLint) *(us_ptr++);
809 pc = emit_element_old( pc, arrays, index );
810 }
811 break;
812 }
813 case GL_UNSIGNED_BYTE: {
814 const GLubyte * ub_ptr = (const GLubyte *) indices;
815
816 for ( i = 0 ; i < elements_per_request ; i++ ) {
817 const GLint index = (GLint) *(ub_ptr++);
818 pc = emit_element_old( pc, arrays, index );
819 }
820 break;
821 }
822 }
823
824 if ( total_requests != 0 ) {
825 __glXSendLargeChunk( gc, req, total_requests, gc->pc,
826 pc - gc->pc );
827 pc = gc->pc;
828 req++;
829 }
830
831 count -= elements_per_request;
832 }
833
834
835 assert( (total_requests == 0) || ((req - 1) == total_requests) );
836
837 if ( total_requests == 0 ) {
838 assert( pc <= gc->bufEnd );
839
840 gc->pc = pc;
841 if ( gc->pc > gc->limit ) {
842 (void) __glXFlushRenderBuffer(gc, gc->pc);
843 }
844 }
845 }
846
847
848 /**
849 * Validate that the \c mode parameter to \c glDrawArrays, et. al. is valid.
850 * If it is not valid, then an error code is set in the GLX context.
851 *
852 * \returns
853 * \c GL_TRUE if the argument is valid, \c GL_FALSE if is not.
854 */
855 static GLboolean
856 validate_mode(__GLXcontext *gc, GLenum mode)
857 {
858 switch(mode) {
859 case GL_POINTS:
860 case GL_LINE_STRIP:
861 case GL_LINE_LOOP:
862 case GL_LINES:
863 case GL_TRIANGLE_STRIP:
864 case GL_TRIANGLE_FAN:
865 case GL_TRIANGLES:
866 case GL_QUAD_STRIP:
867 case GL_QUADS:
868 case GL_POLYGON:
869 break;
870 default:
871 __glXSetError(gc, GL_INVALID_ENUM);
872 return GL_FALSE;
873 }
874
875 return GL_TRUE;
876 }
877
878
879 /**
880 * Validate that the \c count parameter to \c glDrawArrays, et. al. is valid.
881 * A value less than zero is invalid and will result in \c GL_INVALID_VALUE
882 * being set. A value of zero will not result in an error being set, but
883 * will result in \c GL_FALSE being returned.
884 *
885 * \returns
886 * \c GL_TRUE if the argument is valid, \c GL_FALSE if it is not.
887 */
888 static GLboolean
889 validate_count(__GLXcontext *gc, GLsizei count)
890 {
891 if (count < 0) {
892 __glXSetError(gc, GL_INVALID_VALUE);
893 }
894
895 return (count > 0);
896 }
897
898
899 /**
900 * Validate that the \c type parameter to \c glDrawElements, et. al. is
901 * valid. Only \c GL_UNSIGNED_BYTE, \c GL_UNSIGNED_SHORT, and
902 * \c GL_UNSIGNED_INT are valid.
903 *
904 * \returns
905 * \c GL_TRUE if the argument is valid, \c GL_FALSE if it is not.
906 */
907 static GLboolean validate_type(__GLXcontext *gc, GLenum type)
908 {
909 switch( type ) {
910 case GL_UNSIGNED_INT:
911 case GL_UNSIGNED_SHORT:
912 case GL_UNSIGNED_BYTE:
913 return GL_TRUE;
914 default:
915 __glXSetError(gc, GL_INVALID_ENUM);
916 return GL_FALSE;
917 }
918 }
919
920
921 void __indirect_glDrawArrays(GLenum mode, GLint first, GLsizei count)
922 {
923 __GLXcontext *gc = __glXGetCurrentContext();
924 const __GLXattribute * state =
925 (const __GLXattribute *)(gc->client_state_private);
926 struct array_state_vector * arrays = state->array_state;
927
928
929 if ( validate_mode(gc, mode) && validate_count(gc, count) ) {
930 if ( ! arrays->array_info_cache_valid ) {
931 fill_array_info_cache( arrays );
932 }
933
934 arrays->DrawArrays(mode, first, count);
935 }
936 }
937
938
939 void __indirect_glArrayElement(GLint index)
940 {
941 __GLXcontext *gc = __glXGetCurrentContext();
942 const __GLXattribute * state =
943 (const __GLXattribute *)(gc->client_state_private);
944 struct array_state_vector * arrays = state->array_state;
945
946 size_t single_vertex_size;
947
948
949 single_vertex_size = calculate_single_vertex_size_none( arrays );
950
951 if ( (gc->pc + single_vertex_size) >= gc->bufEnd ) {
952 gc->pc = __glXFlushRenderBuffer(gc, gc->pc);
953 }
954
955 gc->pc = emit_element_none( gc->pc, arrays, index );
956
957 if ( gc->pc > gc->limit ) {
958 (void) __glXFlushRenderBuffer(gc, gc->pc);
959 }
960 }
961
962
963 void __indirect_glDrawElements(GLenum mode, GLsizei count, GLenum type,
964 const GLvoid *indices)
965 {
966 __GLXcontext *gc = __glXGetCurrentContext();
967 const __GLXattribute * state =
968 (const __GLXattribute *)(gc->client_state_private);
969 struct array_state_vector * arrays = state->array_state;
970
971
972 if ( validate_mode(gc, mode) && validate_count(gc, count)
973 && validate_type(gc, type) ) {
974 if ( ! arrays->array_info_cache_valid ) {
975 fill_array_info_cache( arrays );
976 }
977
978 arrays->DrawElements(mode, count, type, indices);
979 }
980 }
981
982
983 void __indirect_glDrawRangeElements(GLenum mode, GLuint start, GLuint end,
984 GLsizei count, GLenum type,
985 const GLvoid *indices)
986 {
987 __GLXcontext *gc = __glXGetCurrentContext();
988 const __GLXattribute * state =
989 (const __GLXattribute *)(gc->client_state_private);
990 struct array_state_vector * arrays = state->array_state;
991
992
993 if ( validate_mode(gc, mode) && validate_count(gc, count)
994 && validate_type(gc, type) ) {
995 if (end < start) {
996 __glXSetError(gc, GL_INVALID_VALUE);
997 return;
998 }
999
1000 if ( ! arrays->array_info_cache_valid ) {
1001 fill_array_info_cache( arrays );
1002 }
1003
1004 arrays->DrawElements(mode, count, type, indices);
1005 }
1006 }
1007
1008
1009 void __indirect_glMultiDrawArraysEXT(GLenum mode, GLint *first, GLsizei *count,
1010 GLsizei primcount)
1011 {
1012 __GLXcontext *gc = __glXGetCurrentContext();
1013 const __GLXattribute * state =
1014 (const __GLXattribute *)(gc->client_state_private);
1015 struct array_state_vector * arrays = state->array_state;
1016 GLsizei i;
1017
1018
1019 if ( validate_mode(gc, mode) ) {
1020 if ( ! arrays->array_info_cache_valid ) {
1021 fill_array_info_cache( arrays );
1022 }
1023
1024 for ( i = 0 ; i < primcount ; i++ ) {
1025 if ( validate_count( gc, count[i] ) ) {
1026 arrays->DrawArrays(mode, first[i], count[i]);
1027 }
1028 }
1029 }
1030 }
1031
1032
1033 void __indirect_glMultiDrawElementsEXT(GLenum mode, const GLsizei *count,
1034 GLenum type, const GLvoid ** indices,
1035 GLsizei primcount)
1036 {
1037 __GLXcontext *gc = __glXGetCurrentContext();
1038 const __GLXattribute * state =
1039 (const __GLXattribute *)(gc->client_state_private);
1040 struct array_state_vector * arrays = state->array_state;
1041 GLsizei i;
1042
1043
1044 if ( validate_mode(gc, mode) && validate_type(gc, type) ) {
1045 if ( ! arrays->array_info_cache_valid ) {
1046 fill_array_info_cache( arrays );
1047 }
1048
1049 for ( i = 0 ; i < primcount ; i++ ) {
1050 if ( validate_count( gc, count[i] ) ) {
1051 arrays->DrawElements(mode, count[i], type, indices[i]);
1052 }
1053 }
1054 }
1055 }
1056
1057
1058 #define COMMON_ARRAY_DATA_INIT(a, PTR, TYPE, STRIDE, COUNT, NORMALIZED, HDR_SIZE, OPCODE) \
1059 do { \
1060 (a)->data = PTR; \
1061 (a)->data_type = TYPE; \
1062 (a)->user_stride = STRIDE; \
1063 (a)->count = COUNT; \
1064 (a)->normalized = NORMALIZED; \
1065 \
1066 (a)->element_size = __glXTypeSize( TYPE ) * COUNT; \
1067 (a)->true_stride = (STRIDE == 0) \
1068 ? (a)->element_size : STRIDE; \
1069 \
1070 (a)->header_size = HDR_SIZE; \
1071 ((uint16_t *) (a)->header)[0] = __GLX_PAD((a)->header_size + (a)->element_size); \
1072 ((uint16_t *) (a)->header)[1] = OPCODE; \
1073 } while(0)
1074
1075
1076 void __indirect_glVertexPointer( GLint size, GLenum type, GLsizei stride,
1077 const GLvoid * pointer )
1078 {
1079 static const uint16_t short_ops[5] = {
1080 0, 0, X_GLrop_Vertex2sv, X_GLrop_Vertex3sv, X_GLrop_Vertex4sv
1081 };
1082 static const uint16_t int_ops[5] = {
1083 0, 0, X_GLrop_Vertex2iv, X_GLrop_Vertex3iv, X_GLrop_Vertex4iv
1084 };
1085 static const uint16_t float_ops[5] = {
1086 0, 0, X_GLrop_Vertex2fv, X_GLrop_Vertex3fv, X_GLrop_Vertex4fv
1087 };
1088 static const uint16_t double_ops[5] = {
1089 0, 0, X_GLrop_Vertex2dv, X_GLrop_Vertex3dv, X_GLrop_Vertex4dv
1090 };
1091 uint16_t opcode;
1092 __GLXcontext *gc = __glXGetCurrentContext();
1093 __GLXattribute * state = (__GLXattribute *)(gc->client_state_private);
1094 struct array_state_vector * arrays = state->array_state;
1095 struct array_state * a;
1096
1097
1098 if (size < 2 || size > 4 || stride < 0) {
1099 __glXSetError(gc, GL_INVALID_VALUE);
1100 return;
1101 }
1102
1103 switch ( type ) {
1104 case GL_SHORT: opcode = short_ops[size]; break;
1105 case GL_INT: opcode = int_ops[size]; break;
1106 case GL_FLOAT: opcode = float_ops[size]; break;
1107 case GL_DOUBLE: opcode = double_ops[size]; break;
1108 default:
1109 __glXSetError(gc, GL_INVALID_ENUM);
1110 return;
1111 }
1112
1113 a = get_array_entry( arrays, GL_VERTEX_ARRAY, 0 );
1114 assert( a != NULL );
1115 COMMON_ARRAY_DATA_INIT( a, pointer, type, stride, size, GL_FALSE, 4,
1116 opcode );
1117
1118 if ( a->enabled ) {
1119 arrays->array_info_cache_valid = GL_FALSE;
1120 }
1121 }
1122
1123
1124 void __indirect_glNormalPointer( GLenum type, GLsizei stride,
1125 const GLvoid * pointer )
1126 {
1127 uint16_t opcode;
1128 __GLXcontext *gc = __glXGetCurrentContext();
1129 __GLXattribute * state = (__GLXattribute *)(gc->client_state_private);
1130 struct array_state_vector * arrays = state->array_state;
1131 struct array_state * a;
1132
1133
1134 if (stride < 0) {
1135 __glXSetError(gc, GL_INVALID_VALUE);
1136 return;
1137 }
1138
1139 switch ( type ) {
1140 case GL_BYTE: opcode = X_GLrop_Normal3bv; break;
1141 case GL_SHORT: opcode = X_GLrop_Normal3sv; break;
1142 case GL_INT: opcode = X_GLrop_Normal3iv; break;
1143 case GL_FLOAT: opcode = X_GLrop_Normal3fv; break;
1144 case GL_DOUBLE: opcode = X_GLrop_Normal3dv; break;
1145 default:
1146 __glXSetError(gc, GL_INVALID_ENUM);
1147 return;
1148 }
1149
1150 a = get_array_entry( arrays, GL_NORMAL_ARRAY, 0 );
1151 assert( a != NULL );
1152 COMMON_ARRAY_DATA_INIT( a, pointer, type, stride, 3, GL_TRUE, 4,
1153 opcode );
1154
1155 if ( a->enabled ) {
1156 arrays->array_info_cache_valid = GL_FALSE;
1157 }
1158 }
1159
1160
1161 void __indirect_glColorPointer( GLint size, GLenum type, GLsizei stride,
1162 const GLvoid * pointer )
1163 {
1164 static const uint16_t byte_ops[5] = {
1165 0, 0, 0, X_GLrop_Color3bv, X_GLrop_Color4bv
1166 };
1167 static const uint16_t ubyte_ops[5] = {
1168 0, 0, 0, X_GLrop_Color3ubv, X_GLrop_Color4ubv
1169 };
1170 static const uint16_t short_ops[5] = {
1171 0, 0, 0, X_GLrop_Color3sv, X_GLrop_Color4sv
1172 };
1173 static const uint16_t ushort_ops[5] = {
1174 0, 0, 0, X_GLrop_Color3usv, X_GLrop_Color4usv
1175 };
1176 static const uint16_t int_ops[5] = {
1177 0, 0, 0, X_GLrop_Color3iv, X_GLrop_Color4iv
1178 };
1179 static const uint16_t uint_ops[5] = {
1180 0, 0, 0, X_GLrop_Color3uiv, X_GLrop_Color4uiv
1181 };
1182 static const uint16_t float_ops[5] = {
1183 0, 0, 0, X_GLrop_Color3fv, X_GLrop_Color4fv
1184 };
1185 static const uint16_t double_ops[5] = {
1186 0, 0, 0, X_GLrop_Color3dv, X_GLrop_Color4dv
1187 };
1188 uint16_t opcode;
1189 __GLXcontext *gc = __glXGetCurrentContext();
1190 __GLXattribute * state = (__GLXattribute *)(gc->client_state_private);
1191 struct array_state_vector * arrays = state->array_state;
1192 struct array_state * a;
1193
1194
1195 if (size < 3 || size > 4 || stride < 0) {
1196 __glXSetError(gc, GL_INVALID_VALUE);
1197 return;
1198 }
1199
1200 switch ( type ) {
1201 case GL_BYTE: opcode = byte_ops[size]; break;
1202 case GL_UNSIGNED_BYTE: opcode = ubyte_ops[size]; break;
1203 case GL_SHORT: opcode = short_ops[size]; break;
1204 case GL_UNSIGNED_SHORT: opcode = ushort_ops[size]; break;
1205 case GL_INT: opcode = int_ops[size]; break;
1206 case GL_UNSIGNED_INT: opcode = uint_ops[size]; break;
1207 case GL_FLOAT: opcode = float_ops[size]; break;
1208 case GL_DOUBLE: opcode = double_ops[size]; break;
1209 default:
1210 __glXSetError(gc, GL_INVALID_ENUM);
1211 return;
1212 }
1213
1214 a = get_array_entry( arrays, GL_COLOR_ARRAY, 0 );
1215 assert( a != NULL );
1216 COMMON_ARRAY_DATA_INIT( a, pointer, type, stride, size, GL_TRUE, 4,
1217 opcode );
1218
1219 if ( a->enabled ) {
1220 arrays->array_info_cache_valid = GL_FALSE;
1221 }
1222 }
1223
1224
1225 void __indirect_glIndexPointer( GLenum type, GLsizei stride,
1226 const GLvoid * pointer )
1227 {
1228 uint16_t opcode;
1229 __GLXcontext *gc = __glXGetCurrentContext();
1230 __GLXattribute * state = (__GLXattribute *)(gc->client_state_private);
1231 struct array_state_vector * arrays = state->array_state;
1232 struct array_state * a;
1233
1234
1235 if (stride < 0) {
1236 __glXSetError(gc, GL_INVALID_VALUE);
1237 return;
1238 }
1239
1240 switch ( type ) {
1241 case GL_UNSIGNED_BYTE: opcode = X_GLrop_Indexubv; break;
1242 case GL_SHORT: opcode = X_GLrop_Indexsv; break;
1243 case GL_INT: opcode = X_GLrop_Indexiv; break;
1244 case GL_FLOAT: opcode = X_GLrop_Indexfv; break;
1245 case GL_DOUBLE: opcode = X_GLrop_Indexdv; break;
1246 default:
1247 __glXSetError(gc, GL_INVALID_ENUM);
1248 return;
1249 }
1250
1251 a = get_array_entry( arrays, GL_INDEX_ARRAY, 0 );
1252 assert( a != NULL );
1253 COMMON_ARRAY_DATA_INIT( a, pointer, type, stride, 1, GL_FALSE, 4,
1254 opcode );
1255
1256 if ( a->enabled ) {
1257 arrays->array_info_cache_valid = GL_FALSE;
1258 }
1259 }
1260
1261
1262 void __indirect_glEdgeFlagPointer( GLsizei stride, const GLvoid * pointer )
1263 {
1264 __GLXcontext *gc = __glXGetCurrentContext();
1265 __GLXattribute * state = (__GLXattribute *)(gc->client_state_private);
1266 struct array_state_vector * arrays = state->array_state;
1267 struct array_state * a;
1268
1269
1270 if (stride < 0) {
1271 __glXSetError(gc, GL_INVALID_VALUE);
1272 return;
1273 }
1274
1275
1276 a = get_array_entry( arrays, GL_EDGE_FLAG_ARRAY, 0 );
1277 assert( a != NULL );
1278 COMMON_ARRAY_DATA_INIT( a, pointer, GL_UNSIGNED_BYTE, stride, 1, GL_FALSE,
1279 4, X_GLrop_EdgeFlagv );
1280
1281 if ( a->enabled ) {
1282 arrays->array_info_cache_valid = GL_FALSE;
1283 }
1284 }
1285
1286
1287 void __indirect_glTexCoordPointer( GLint size, GLenum type, GLsizei stride,
1288 const GLvoid * pointer )
1289 {
1290 static const uint16_t short_ops[5] = {
1291 0, X_GLrop_TexCoord1sv, X_GLrop_TexCoord2sv, X_GLrop_TexCoord3sv, X_GLrop_TexCoord4sv
1292 };
1293 static const uint16_t int_ops[5] = {
1294 0, X_GLrop_TexCoord1iv, X_GLrop_TexCoord2iv, X_GLrop_TexCoord3iv, X_GLrop_TexCoord4iv
1295 };
1296 static const uint16_t float_ops[5] = {
1297 0, X_GLrop_TexCoord1dv, X_GLrop_TexCoord2fv, X_GLrop_TexCoord3fv, X_GLrop_TexCoord4fv
1298 };
1299 static const uint16_t double_ops[5] = {
1300 0, X_GLrop_TexCoord1dv, X_GLrop_TexCoord2dv, X_GLrop_TexCoord3dv, X_GLrop_TexCoord4dv
1301 };
1302
1303 static const uint16_t mshort_ops[5] = {
1304 0, X_GLrop_MultiTexCoord1svARB, X_GLrop_MultiTexCoord2svARB, X_GLrop_MultiTexCoord3svARB, X_GLrop_MultiTexCoord4svARB
1305 };
1306 static const uint16_t mint_ops[5] = {
1307 0, X_GLrop_MultiTexCoord1ivARB, X_GLrop_MultiTexCoord2ivARB, X_GLrop_MultiTexCoord3ivARB, X_GLrop_MultiTexCoord4ivARB
1308 };
1309 static const uint16_t mfloat_ops[5] = {
1310 0, X_GLrop_MultiTexCoord1dvARB, X_GLrop_MultiTexCoord2fvARB, X_GLrop_MultiTexCoord3fvARB, X_GLrop_MultiTexCoord4fvARB
1311 };
1312 static const uint16_t mdouble_ops[5] = {
1313 0, X_GLrop_MultiTexCoord1dvARB, X_GLrop_MultiTexCoord2dvARB, X_GLrop_MultiTexCoord3dvARB, X_GLrop_MultiTexCoord4dvARB
1314 };
1315
1316 uint16_t opcode;
1317 __GLXcontext *gc = __glXGetCurrentContext();
1318 __GLXattribute * state = (__GLXattribute *)(gc->client_state_private);
1319 struct array_state_vector * arrays = state->array_state;
1320 struct array_state * a;
1321 unsigned header_size;
1322 unsigned index;
1323
1324
1325 if (size < 1 || size > 4 || stride < 0) {
1326 __glXSetError(gc, GL_INVALID_VALUE);
1327 return;
1328 }
1329
1330 index = arrays->active_texture_unit;
1331 if ( index == 0 ) {
1332 switch ( type ) {
1333 case GL_SHORT: opcode = short_ops[size]; break;
1334 case GL_INT: opcode = int_ops[size]; break;
1335 case GL_FLOAT: opcode = float_ops[size]; break;
1336 case GL_DOUBLE: opcode = double_ops[size]; break;
1337 default:
1338 __glXSetError(gc, GL_INVALID_ENUM);
1339 return;
1340 }
1341
1342 header_size = 4;
1343 }
1344 else {
1345 switch ( type ) {
1346 case GL_SHORT: opcode = mshort_ops[size]; break;
1347 case GL_INT: opcode = mint_ops[size]; break;
1348 case GL_FLOAT: opcode = mfloat_ops[size]; break;
1349 case GL_DOUBLE: opcode = mdouble_ops[size]; break;
1350 default:
1351 __glXSetError(gc, GL_INVALID_ENUM);
1352 return;
1353 }
1354
1355 header_size = 8;
1356 }
1357
1358 a = get_array_entry( arrays, GL_TEXTURE_COORD_ARRAY, index );
1359 assert( a != NULL );
1360 COMMON_ARRAY_DATA_INIT( a, pointer, type, stride, size, GL_FALSE,
1361 header_size, opcode );
1362
1363 if ( a->enabled ) {
1364 arrays->array_info_cache_valid = GL_FALSE;
1365 }
1366 }
1367
1368
1369 void __indirect_glSecondaryColorPointerEXT( GLint size, GLenum type, GLsizei stride,
1370 const GLvoid * pointer )
1371 {
1372 uint16_t opcode;
1373 __GLXcontext *gc = __glXGetCurrentContext();
1374 __GLXattribute * state = (__GLXattribute *)(gc->client_state_private);
1375 struct array_state_vector * arrays = state->array_state;
1376 struct array_state * a;
1377
1378
1379 if (size != 3 || stride < 0) {
1380 __glXSetError(gc, GL_INVALID_VALUE);
1381 return;
1382 }
1383
1384 switch ( type ) {
1385 case GL_BYTE: opcode = 4126; break;
1386 case GL_UNSIGNED_BYTE: opcode = 4131; break;
1387 case GL_SHORT: opcode = 4127; break;
1388 case GL_UNSIGNED_SHORT: opcode = 4132; break;
1389 case GL_INT: opcode = 4128; break;
1390 case GL_UNSIGNED_INT: opcode = 4133; break;
1391 case GL_FLOAT: opcode = 4129; break;
1392 case GL_DOUBLE: opcode = 4130; break;
1393 default:
1394 __glXSetError(gc, GL_INVALID_ENUM);
1395 return;
1396 }
1397
1398 a = get_array_entry( arrays, GL_SECONDARY_COLOR_ARRAY, 0 );
1399 if ( a == NULL ) {
1400 __glXSetError(gc, GL_INVALID_OPERATION);
1401 return;
1402 }
1403
1404 COMMON_ARRAY_DATA_INIT( a, pointer, type, stride, size, GL_TRUE, 4,
1405 opcode );
1406
1407 if ( a->enabled ) {
1408 arrays->array_info_cache_valid = GL_FALSE;
1409 }
1410 }
1411
1412
1413 void __indirect_glFogCoordPointerEXT( GLenum type, GLsizei stride,
1414 const GLvoid * pointer )
1415 {
1416 uint16_t opcode;
1417 __GLXcontext *gc = __glXGetCurrentContext();
1418 __GLXattribute * state = (__GLXattribute *)(gc->client_state_private);
1419 struct array_state_vector * arrays = state->array_state;
1420 struct array_state * a;
1421
1422
1423 if (stride < 0) {
1424 __glXSetError(gc, GL_INVALID_VALUE);
1425 return;
1426 }
1427
1428 switch ( type ) {
1429 case GL_FLOAT: opcode = 4124; break;
1430 case GL_DOUBLE: opcode = 4125; break;
1431 default:
1432 __glXSetError(gc, GL_INVALID_ENUM);
1433 return;
1434 }
1435
1436 a = get_array_entry( arrays, GL_FOG_COORD_ARRAY, 0 );
1437 if ( a == NULL ) {
1438 __glXSetError(gc, GL_INVALID_OPERATION);
1439 return;
1440 }
1441
1442 COMMON_ARRAY_DATA_INIT( a, pointer, type, stride, 1, GL_FALSE, 4,
1443 opcode );
1444
1445 if ( a->enabled ) {
1446 arrays->array_info_cache_valid = GL_FALSE;
1447 }
1448 }
1449
1450
1451 void __indirect_glVertexAttribPointerARB(GLuint index, GLint size,
1452 GLenum type, GLboolean normalized,
1453 GLsizei stride,
1454 const GLvoid * pointer)
1455 {
1456 static const uint16_t short_ops[5] = { 0, 4189, 4190, 4191, 4192 };
1457 static const uint16_t float_ops[5] = { 0, 4193, 4194, 4195, 4196 };
1458 static const uint16_t double_ops[5] = { 0, 4197, 4198, 4199, 4200 };
1459
1460 uint16_t opcode;
1461 __GLXcontext *gc = __glXGetCurrentContext();
1462 __GLXattribute * state = (__GLXattribute *)(gc->client_state_private);
1463 struct array_state_vector * arrays = state->array_state;
1464 struct array_state * a;
1465 unsigned true_immediate_count;
1466 unsigned true_immediate_size;
1467
1468
1469 if ( (size < 1) || (size > 4) || (stride < 0)
1470 || (index > arrays->num_vertex_program_attribs) ){
1471 __glXSetError(gc, GL_INVALID_VALUE);
1472 return;
1473 }
1474
1475 if ( normalized && (type != GL_FLOAT) && (type != GL_DOUBLE)) {
1476 switch( type ) {
1477 case GL_BYTE: opcode = X_GLrop_VertexAttrib4NbvARB; break;
1478 case GL_UNSIGNED_BYTE: opcode = X_GLrop_VertexAttrib4NubvARB; break;
1479 case GL_SHORT: opcode = X_GLrop_VertexAttrib4NsvARB; break;
1480 case GL_UNSIGNED_SHORT: opcode = X_GLrop_VertexAttrib4NusvARB; break;
1481 case GL_INT: opcode = X_GLrop_VertexAttrib4NivARB; break;
1482 case GL_UNSIGNED_INT: opcode = X_GLrop_VertexAttrib4NuivARB; break;
1483 default:
1484 __glXSetError(gc, GL_INVALID_ENUM);
1485 return;
1486 }
1487
1488 true_immediate_count = 4;
1489 }
1490 else {
1491 true_immediate_count = size;
1492
1493 switch( type ) {
1494 case GL_BYTE:
1495 opcode = X_GLrop_VertexAttrib4bvARB;
1496 true_immediate_count = 4;
1497 break;
1498 case GL_UNSIGNED_BYTE:
1499 opcode = X_GLrop_VertexAttrib4ubvARB;
1500 true_immediate_count = 4;
1501 break;
1502 case GL_SHORT:
1503 opcode = short_ops[size];
1504 break;
1505 case GL_UNSIGNED_SHORT:
1506 opcode = X_GLrop_VertexAttrib4usvARB;
1507 true_immediate_count = 4;
1508 break;
1509 case GL_INT:
1510 opcode = X_GLrop_VertexAttrib4ivARB;
1511 true_immediate_count = 4;
1512 break;
1513 case GL_UNSIGNED_INT:
1514 opcode = X_GLrop_VertexAttrib4uivARB;
1515 true_immediate_count = 4;
1516 break;
1517 case GL_FLOAT:
1518 opcode = float_ops[size];
1519 break;
1520 case GL_DOUBLE:
1521 opcode = double_ops[size];
1522 break;
1523 default:
1524 __glXSetError(gc, GL_INVALID_ENUM);
1525 return;
1526 }
1527 }
1528
1529 a = get_array_entry( arrays, GL_VERTEX_ATTRIB_ARRAY_POINTER, index );
1530 if ( a == NULL ) {
1531 __glXSetError(gc, GL_INVALID_OPERATION);
1532 return;
1533 }
1534
1535 COMMON_ARRAY_DATA_INIT( a, pointer, type, stride, size, normalized, 8,
1536 opcode );
1537
1538 true_immediate_size = __glXTypeSize(type) * true_immediate_count;
1539 ((uint16_t *) (a)->header)[0] = __GLX_PAD(a->header_size
1540 + true_immediate_size);
1541
1542 if ( a->enabled ) {
1543 arrays->array_info_cache_valid = GL_FALSE;
1544 }
1545 }
1546
1547
1548 /**
1549 * I don't have 100% confidence that this is correct. The different rules
1550 * about whether or not generic vertex attributes alias "classic" vertex
1551 * attributes (i.e., attrib1 ?= primary color) between ARB_vertex_program,
1552 * ARB_vertex_shader, and NV_vertex_program are a bit confusing. My
1553 * feeling is that the client-side doesn't have to worry about it. The
1554 * client just sends all the data to the server and lets the server deal
1555 * with it.
1556 */
1557 void __indirect_glVertexAttribPointerNV( GLuint index, GLint size,
1558 GLenum type, GLsizei stride,
1559 const GLvoid * pointer)
1560 {
1561 __GLXcontext *gc = __glXGetCurrentContext();
1562 GLboolean normalized = GL_FALSE;
1563
1564
1565 switch( type ) {
1566 case GL_UNSIGNED_BYTE:
1567 if ( size != 4 ) {
1568 __glXSetError(gc, GL_INVALID_VALUE);
1569 return;
1570 }
1571 normalized = GL_TRUE;
1572
1573 case GL_SHORT:
1574 case GL_FLOAT:
1575 case GL_DOUBLE:
1576 __indirect_glVertexAttribPointerARB(index, size, type,
1577 normalized,
1578 stride, pointer);
1579 return;
1580 default:
1581 __glXSetError(gc, GL_INVALID_ENUM);
1582 return;
1583 }
1584 }
1585
1586
1587 void __indirect_glClientActiveTextureARB(GLenum texture)
1588 {
1589 __GLXcontext * const gc = __glXGetCurrentContext();
1590 __GLXattribute * const state = (__GLXattribute *)(gc->client_state_private);
1591 struct array_state_vector * const arrays = state->array_state;
1592 const GLint unit = (GLint) texture - GL_TEXTURE0;
1593
1594
1595 if ( (unit < 0) || (unit > arrays->num_texture_units) ) {
1596 __glXSetError(gc, GL_INVALID_ENUM);
1597 return;
1598 }
1599
1600 arrays->active_texture_unit = unit;
1601 }
1602
1603
1604 /**
1605 */
1606 GLboolean
1607 __glXSetArrayEnable( __GLXattribute * state,
1608 GLenum key, unsigned index, GLboolean enable )
1609 {
1610 struct array_state_vector * arrays = state->array_state;
1611 struct array_state * a;
1612
1613
1614 if ( key == GL_TEXTURE_COORD_ARRAY ) {
1615 index = arrays->active_texture_unit;
1616 }
1617
1618 a = get_array_entry( arrays, key, index );
1619
1620 if ( (a != NULL) && (a->enabled != enable) ) {
1621 a->enabled = enable;
1622 arrays->array_info_cache_valid = GL_FALSE;
1623 }
1624
1625 return (a != NULL);
1626 }
1627
1628
1629 void
1630 __glXArrayDisableAll( __GLXattribute * state )
1631 {
1632 struct array_state_vector * arrays = state->array_state;
1633 unsigned i;
1634
1635
1636 for ( i = 0 ; i < arrays->num_arrays ; i++ ) {
1637 arrays->arrays[i].enabled = GL_FALSE;
1638 }
1639
1640 arrays->array_info_cache_valid = GL_FALSE;
1641 }
1642
1643
1644 /**
1645 */
1646 GLboolean
1647 __glXGetArrayEnable( const __GLXattribute * const state,
1648 GLenum key, unsigned index, GLintptr * dest )
1649 {
1650 const struct array_state_vector * arrays = state->array_state;
1651 const struct array_state * a = get_array_entry( (struct array_state_vector *) arrays,
1652 key, index );
1653
1654 if ( a != NULL ) {
1655 *dest = (GLintptr) a->enabled;
1656 }
1657
1658 return (a != NULL);
1659 }
1660
1661
1662 /**
1663 */
1664 GLboolean
1665 __glXGetArrayType( const __GLXattribute * const state,
1666 GLenum key, unsigned index, GLintptr * dest )
1667 {
1668 const struct array_state_vector * arrays = state->array_state;
1669 const struct array_state * a = get_array_entry( (struct array_state_vector *) arrays,
1670 key, index );
1671
1672 if ( a != NULL ) {
1673 *dest = (GLintptr) a->enabled;
1674 }
1675
1676 return (a != NULL);
1677 }
1678
1679
1680 /**
1681 */
1682 GLboolean
1683 __glXGetArraySize( const __GLXattribute * const state,
1684 GLenum key, unsigned index, GLintptr * dest )
1685 {
1686 const struct array_state_vector * arrays = state->array_state;
1687 const struct array_state * a = get_array_entry( (struct array_state_vector *) arrays,
1688 key, index );
1689
1690 if ( a != NULL ) {
1691 *dest = (GLintptr) a->count;
1692 }
1693
1694 return (a != NULL);
1695 }
1696
1697
1698 /**
1699 */
1700 GLboolean
1701 __glXGetArrayStride( const __GLXattribute * const state,
1702 GLenum key, unsigned index, GLintptr * dest )
1703 {
1704 const struct array_state_vector * arrays = state->array_state;
1705 const struct array_state * a = get_array_entry( (struct array_state_vector *) arrays,
1706 key, index );
1707
1708 if ( a != NULL ) {
1709 *dest = (GLintptr) a->user_stride;
1710 }
1711
1712 return (a != NULL);
1713 }
1714
1715
1716 /**
1717 */
1718 GLboolean
1719 __glXGetArrayPointer( const __GLXattribute * const state,
1720 GLenum key, unsigned index, void ** dest )
1721 {
1722 const struct array_state_vector * arrays = state->array_state;
1723 const struct array_state * a = get_array_entry( (struct array_state_vector *) arrays,
1724 key, index );
1725
1726
1727 if ( a != NULL ) {
1728 *dest = (void *) (a->data);
1729 }
1730
1731 return (a != NULL);
1732 }
1733
1734
1735 /**
1736 */
1737 GLboolean
1738 __glXGetArrayNormalized( const __GLXattribute * const state,
1739 GLenum key, unsigned index, GLintptr * dest )
1740 {
1741 const struct array_state_vector * arrays = state->array_state;
1742 const struct array_state * a = get_array_entry( (struct array_state_vector *) arrays,
1743 key, index );
1744
1745
1746 if ( a != NULL ) {
1747 *dest = (GLintptr) a->normalized;
1748 }
1749
1750 return (a != NULL);
1751 }
1752
1753
1754 /**
1755 */
1756 GLuint
1757 __glXGetActiveTextureUnit( const __GLXattribute * const state )
1758 {
1759 return state->array_state->active_texture_unit;
1760 }
1761
1762
1763 void
1764 __glXPushArrayState( __GLXattribute * state )
1765 {
1766 struct array_state_vector * arrays = state->array_state;
1767 struct array_stack_state * stack = & arrays->stack[ (arrays->stack_index * arrays->num_arrays)];
1768 unsigned i;
1769
1770
1771 for ( i = 0 ; i < arrays->num_arrays ; i++ ) {
1772 stack[i].data = arrays->arrays[i].data;
1773 stack[i].data_type = arrays->arrays[i].data_type;
1774 stack[i].user_stride = arrays->arrays[i].user_stride;
1775 stack[i].count = arrays->arrays[i].count;
1776 stack[i].key = arrays->arrays[i].key;
1777 stack[i].enabled = arrays->arrays[i].enabled;
1778 }
1779
1780 arrays->active_texture_unit_stack[ arrays->stack_index ] =
1781 arrays->active_texture_unit;
1782
1783 arrays->stack_index++;
1784 }
1785
1786
1787 void
1788 __glXPopArrayState( __GLXattribute * state )
1789 {
1790 struct array_state_vector * arrays = state->array_state;
1791 struct array_stack_state * stack;
1792 unsigned i;
1793
1794
1795 arrays->stack_index--;
1796 stack = & arrays->stack[ (arrays->stack_index * arrays->num_arrays) ];
1797
1798 for ( i = 0 ; i < arrays->num_arrays ; i++ ) {
1799 switch ( stack[i].key ) {
1800 case GL_NORMAL_ARRAY:
1801 __indirect_glNormalPointer( stack[i].data_type,
1802 stack[i].user_stride,
1803 stack[i].data );
1804 break;
1805 case GL_COLOR_ARRAY:
1806 __indirect_glColorPointer( stack[i].count,
1807 stack[i].data_type,
1808 stack[i].user_stride,
1809 stack[i].data );
1810 break;
1811 case GL_INDEX_ARRAY:
1812 __indirect_glIndexPointer( stack[i].data_type,
1813 stack[i].user_stride,
1814 stack[i].data );
1815 break;
1816 case GL_EDGE_FLAG_ARRAY:
1817 __indirect_glEdgeFlagPointer( stack[i].user_stride,
1818 stack[i].data );
1819 break;
1820 case GL_TEXTURE_COORD_ARRAY:
1821 arrays->active_texture_unit = stack[i].index;
1822 __indirect_glTexCoordPointer( stack[i].count,
1823 stack[i].data_type,
1824 stack[i].user_stride,
1825 stack[i].data );
1826 break;
1827 case GL_SECONDARY_COLOR_ARRAY:
1828 __indirect_glSecondaryColorPointerEXT( stack[i].count,
1829 stack[i].data_type,
1830 stack[i].user_stride,
1831 stack[i].data );
1832 break;
1833 case GL_FOG_COORDINATE_ARRAY:
1834 __indirect_glFogCoordPointerEXT( stack[i].data_type,
1835 stack[i].user_stride,
1836 stack[i].data );
1837 break;
1838
1839 }
1840
1841 __glXSetArrayEnable( state, stack[i].key, stack[i].index,
1842 stack[i].enabled );
1843 }
1844
1845 arrays->active_texture_unit =
1846 arrays->active_texture_unit_stack[ arrays->stack_index ];
1847 }