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