vbo: convert display list GL_LINE_LOOP prims to GL_LINE_STRIP
[mesa.git] / src / mesa / vbo / vbo_save_api.c
1 /**************************************************************************
2
3 Copyright 2002-2008 VMware, Inc.
4
5 All Rights Reserved.
6
7 Permission is hereby granted, free of charge, to any person obtaining a
8 copy of this software and associated documentation files (the "Software"),
9 to deal in the Software without restriction, including without limitation
10 on the rights to use, copy, modify, merge, publish, distribute, sub
11 license, and/or sell copies of the Software, and to permit persons to whom
12 the Software is furnished to do so, subject to the following conditions:
13
14 The above copyright notice and this permission notice (including the next
15 paragraph) shall be included in all copies or substantial portions of the
16 Software.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21 VMWARE AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
22 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23 OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24 USE OR OTHER DEALINGS IN THE SOFTWARE.
25
26 **************************************************************************/
27
28 /*
29 * Authors:
30 * Keith Whitwell <keithw@vmware.com>
31 */
32
33
34
35 /* Display list compiler attempts to store lists of vertices with the
36 * same vertex layout. Additionally it attempts to minimize the need
37 * for execute-time fixup of these vertex lists, allowing them to be
38 * cached on hardware.
39 *
40 * There are still some circumstances where this can be thwarted, for
41 * example by building a list that consists of one very long primitive
42 * (eg Begin(Triangles), 1000 vertices, End), and calling that list
43 * from inside a different begin/end object (Begin(Lines), CallList,
44 * End).
45 *
46 * In that case the code will have to replay the list as individual
47 * commands through the Exec dispatch table, or fix up the copied
48 * vertices at execute-time.
49 *
50 * The other case where fixup is required is when a vertex attribute
51 * is introduced in the middle of a primitive. Eg:
52 * Begin(Lines)
53 * TexCoord1f() Vertex2f()
54 * TexCoord1f() Color3f() Vertex2f()
55 * End()
56 *
57 * If the current value of Color isn't known at compile-time, this
58 * primitive will require fixup.
59 *
60 *
61 * The list compiler currently doesn't attempt to compile lists
62 * containing EvalCoord or EvalPoint commands. On encountering one of
63 * these, compilation falls back to opcodes.
64 *
65 * This could be improved to fallback only when a mix of EvalCoord and
66 * Vertex commands are issued within a single primitive.
67 */
68
69
70 #include "main/glheader.h"
71 #include "main/bufferobj.h"
72 #include "main/context.h"
73 #include "main/dlist.h"
74 #include "main/enums.h"
75 #include "main/eval.h"
76 #include "main/macros.h"
77 #include "main/api_validate.h"
78 #include "main/api_arrayelt.h"
79 #include "main/vtxfmt.h"
80 #include "main/dispatch.h"
81
82 #include "vbo_context.h"
83 #include "vbo_noop.h"
84
85
86 #ifdef ERROR
87 #undef ERROR
88 #endif
89
90
91 /* An interesting VBO number/name to help with debugging */
92 #define VBO_BUF_ID 12345
93
94
95 /*
96 * NOTE: Old 'parity' issue is gone, but copying can still be
97 * wrong-footed on replay.
98 */
99 static GLuint
100 _save_copy_vertices(struct gl_context *ctx,
101 const struct vbo_save_vertex_list *node,
102 const fi_type * src_buffer)
103 {
104 struct vbo_save_context *save = &vbo_context(ctx)->save;
105 const struct _mesa_prim *prim = &node->prim[node->prim_count - 1];
106 GLuint nr = prim->count;
107 GLuint sz = save->vertex_size;
108 const fi_type *src = src_buffer + prim->start * sz;
109 fi_type *dst = save->copied.buffer;
110 GLuint ovf, i;
111
112 if (prim->end)
113 return 0;
114
115 switch (prim->mode) {
116 case GL_POINTS:
117 return 0;
118 case GL_LINES:
119 ovf = nr & 1;
120 for (i = 0; i < ovf; i++)
121 memcpy(dst + i * sz, src + (nr - ovf + i) * sz,
122 sz * sizeof(GLfloat));
123 return i;
124 case GL_TRIANGLES:
125 ovf = nr % 3;
126 for (i = 0; i < ovf; i++)
127 memcpy(dst + i * sz, src + (nr - ovf + i) * sz,
128 sz * sizeof(GLfloat));
129 return i;
130 case GL_QUADS:
131 ovf = nr & 3;
132 for (i = 0; i < ovf; i++)
133 memcpy(dst + i * sz, src + (nr - ovf + i) * sz,
134 sz * sizeof(GLfloat));
135 return i;
136 case GL_LINE_STRIP:
137 if (nr == 0)
138 return 0;
139 else {
140 memcpy(dst, src + (nr - 1) * sz, sz * sizeof(GLfloat));
141 return 1;
142 }
143 case GL_LINE_LOOP:
144 case GL_TRIANGLE_FAN:
145 case GL_POLYGON:
146 if (nr == 0)
147 return 0;
148 else if (nr == 1) {
149 memcpy(dst, src + 0, sz * sizeof(GLfloat));
150 return 1;
151 }
152 else {
153 memcpy(dst, src + 0, sz * sizeof(GLfloat));
154 memcpy(dst + sz, src + (nr - 1) * sz, sz * sizeof(GLfloat));
155 return 2;
156 }
157 case GL_TRIANGLE_STRIP:
158 case GL_QUAD_STRIP:
159 switch (nr) {
160 case 0:
161 ovf = 0;
162 break;
163 case 1:
164 ovf = 1;
165 break;
166 default:
167 ovf = 2 + (nr & 1);
168 break;
169 }
170 for (i = 0; i < ovf; i++)
171 memcpy(dst + i * sz, src + (nr - ovf + i) * sz,
172 sz * sizeof(GLfloat));
173 return i;
174 default:
175 assert(0);
176 return 0;
177 }
178 }
179
180
181 static struct vbo_save_vertex_store *
182 alloc_vertex_store(struct gl_context *ctx)
183 {
184 struct vbo_save_context *save = &vbo_context(ctx)->save;
185 struct vbo_save_vertex_store *vertex_store =
186 CALLOC_STRUCT(vbo_save_vertex_store);
187
188 /* obj->Name needs to be non-zero, but won't ever be examined more
189 * closely than that. In particular these buffers won't be entered
190 * into the hash and can never be confused with ones visible to the
191 * user. Perhaps there could be a special number for internal
192 * buffers:
193 */
194 vertex_store->bufferobj = ctx->Driver.NewBufferObject(ctx, VBO_BUF_ID);
195 if (vertex_store->bufferobj) {
196 save->out_of_memory =
197 !ctx->Driver.BufferData(ctx,
198 GL_ARRAY_BUFFER_ARB,
199 VBO_SAVE_BUFFER_SIZE * sizeof(GLfloat),
200 NULL, GL_STATIC_DRAW_ARB,
201 GL_MAP_WRITE_BIT |
202 GL_DYNAMIC_STORAGE_BIT,
203 vertex_store->bufferobj);
204 }
205 else {
206 save->out_of_memory = GL_TRUE;
207 }
208
209 if (save->out_of_memory) {
210 _mesa_error(ctx, GL_OUT_OF_MEMORY, "internal VBO allocation");
211 _mesa_install_save_vtxfmt(ctx, &save->vtxfmt_noop);
212 }
213
214 vertex_store->buffer = NULL;
215 vertex_store->used = 0;
216 vertex_store->refcount = 1;
217
218 return vertex_store;
219 }
220
221
222 static void
223 free_vertex_store(struct gl_context *ctx,
224 struct vbo_save_vertex_store *vertex_store)
225 {
226 assert(!vertex_store->buffer);
227
228 if (vertex_store->bufferobj) {
229 _mesa_reference_buffer_object(ctx, &vertex_store->bufferobj, NULL);
230 }
231
232 free(vertex_store);
233 }
234
235
236 fi_type *
237 vbo_save_map_vertex_store(struct gl_context *ctx,
238 struct vbo_save_vertex_store *vertex_store)
239 {
240 const GLbitfield access = (GL_MAP_WRITE_BIT |
241 GL_MAP_INVALIDATE_RANGE_BIT |
242 GL_MAP_UNSYNCHRONIZED_BIT |
243 GL_MAP_FLUSH_EXPLICIT_BIT);
244
245 assert(vertex_store->bufferobj);
246 assert(!vertex_store->buffer); /* the buffer should not be mapped */
247
248 if (vertex_store->bufferobj->Size > 0) {
249 /* Map the remaining free space in the VBO */
250 GLintptr offset = vertex_store->used * sizeof(GLfloat);
251 GLsizeiptr size = vertex_store->bufferobj->Size - offset;
252 fi_type *range = (fi_type *)
253 ctx->Driver.MapBufferRange(ctx, offset, size, access,
254 vertex_store->bufferobj,
255 MAP_INTERNAL);
256 if (range) {
257 /* compute address of start of whole buffer (needed elsewhere) */
258 vertex_store->buffer = range - vertex_store->used;
259 assert(vertex_store->buffer);
260 return range;
261 }
262 else {
263 vertex_store->buffer = NULL;
264 return NULL;
265 }
266 }
267 else {
268 /* probably ran out of memory for buffers */
269 return NULL;
270 }
271 }
272
273
274 void
275 vbo_save_unmap_vertex_store(struct gl_context *ctx,
276 struct vbo_save_vertex_store *vertex_store)
277 {
278 if (vertex_store->bufferobj->Size > 0) {
279 GLintptr offset = 0;
280 GLsizeiptr length = vertex_store->used * sizeof(GLfloat)
281 - vertex_store->bufferobj->Mappings[MAP_INTERNAL].Offset;
282
283 /* Explicitly flush the region we wrote to */
284 ctx->Driver.FlushMappedBufferRange(ctx, offset, length,
285 vertex_store->bufferobj,
286 MAP_INTERNAL);
287
288 ctx->Driver.UnmapBuffer(ctx, vertex_store->bufferobj, MAP_INTERNAL);
289 }
290 vertex_store->buffer = NULL;
291 }
292
293
294 static struct vbo_save_primitive_store *
295 alloc_prim_store(struct gl_context *ctx)
296 {
297 struct vbo_save_primitive_store *store =
298 CALLOC_STRUCT(vbo_save_primitive_store);
299 (void) ctx;
300 store->used = 0;
301 store->refcount = 1;
302 return store;
303 }
304
305
306 static void
307 _save_reset_counters(struct gl_context *ctx)
308 {
309 struct vbo_save_context *save = &vbo_context(ctx)->save;
310
311 save->prim = save->prim_store->buffer + save->prim_store->used;
312 save->buffer = save->vertex_store->buffer + save->vertex_store->used;
313
314 assert(save->buffer == save->buffer_ptr);
315
316 if (save->vertex_size)
317 save->max_vert = (VBO_SAVE_BUFFER_SIZE - save->vertex_store->used) /
318 save->vertex_size;
319 else
320 save->max_vert = 0;
321
322 save->vert_count = 0;
323 save->prim_count = 0;
324 save->prim_max = VBO_SAVE_PRIM_SIZE - save->prim_store->used;
325 save->dangling_attr_ref = GL_FALSE;
326 }
327
328 /**
329 * For a list of prims, try merging prims that can just be extensions of the
330 * previous prim.
331 */
332 static void
333 merge_prims(struct _mesa_prim *prim_list,
334 GLuint *prim_count)
335 {
336 GLuint i;
337 struct _mesa_prim *prev_prim = prim_list;
338
339 for (i = 1; i < *prim_count; i++) {
340 struct _mesa_prim *this_prim = prim_list + i;
341
342 vbo_try_prim_conversion(this_prim);
343
344 if (vbo_can_merge_prims(prev_prim, this_prim)) {
345 /* We've found a prim that just extend the previous one. Tack it
346 * onto the previous one, and let this primitive struct get dropped.
347 */
348 vbo_merge_prims(prev_prim, this_prim);
349 continue;
350 }
351
352 /* If any previous primitives have been dropped, then we need to copy
353 * this later one into the next available slot.
354 */
355 prev_prim++;
356 if (prev_prim != this_prim)
357 *prev_prim = *this_prim;
358 }
359
360 *prim_count = prev_prim - prim_list + 1;
361 }
362
363
364 /**
365 * Convert GL_LINE_LOOP primitive into GL_LINE_STRIP so that drivers
366 * don't have to worry about handling the _mesa_prim::begin/end flags.
367 * See https://bugs.freedesktop.org/show_bug.cgi?id=81174
368 */
369 static void
370 convert_line_loop_to_strip(struct vbo_save_context *save,
371 struct vbo_save_vertex_list *node)
372 {
373 struct _mesa_prim *prim = &node->prim[node->prim_count - 1];
374
375 assert(prim->mode == GL_LINE_LOOP);
376
377 if (prim->end) {
378 /* Copy the 0th vertex to end of the buffer and extend the
379 * vertex count by one to finish the line loop.
380 */
381 const GLuint sz = save->vertex_size;
382 /* 0th vertex: */
383 const fi_type *src = save->buffer + prim->start * sz;
384 /* end of buffer: */
385 fi_type *dst = save->buffer + (prim->start + prim->count) * sz;
386
387 memcpy(dst, src, sz * sizeof(float));
388
389 prim->count++;
390 node->count++;
391 save->vert_count++;
392 save->buffer_ptr += sz;
393 save->vertex_store->used += sz;
394 }
395
396 if (!prim->begin) {
397 /* Drawing the second or later section of a long line loop.
398 * Skip the 0th vertex.
399 */
400 prim->start++;
401 prim->count--;
402 }
403
404 prim->mode = GL_LINE_STRIP;
405 }
406
407
408 /**
409 * Insert the active immediate struct onto the display list currently
410 * being built.
411 */
412 static void
413 _save_compile_vertex_list(struct gl_context *ctx)
414 {
415 struct vbo_save_context *save = &vbo_context(ctx)->save;
416 struct vbo_save_vertex_list *node;
417
418 /* Allocate space for this structure in the display list currently
419 * being compiled.
420 */
421 node = (struct vbo_save_vertex_list *)
422 _mesa_dlist_alloc_aligned(ctx, save->opcode_vertex_list, sizeof(*node));
423
424 if (!node)
425 return;
426
427 /* Make sure the pointer is aligned to the size of a pointer */
428 assert((GLintptr) node % sizeof(void *) == 0);
429
430 /* Duplicate our template, increment refcounts to the storage structs:
431 */
432 memcpy(node->attrsz, save->attrsz, sizeof(node->attrsz));
433 memcpy(node->attrtype, save->attrtype, sizeof(node->attrtype));
434 node->vertex_size = save->vertex_size;
435 node->buffer_offset =
436 (save->buffer - save->vertex_store->buffer) * sizeof(GLfloat);
437 node->count = save->vert_count;
438 node->wrap_count = save->copied.nr;
439 node->dangling_attr_ref = save->dangling_attr_ref;
440 node->prim = save->prim;
441 node->prim_count = save->prim_count;
442 node->vertex_store = save->vertex_store;
443 node->prim_store = save->prim_store;
444
445 node->vertex_store->refcount++;
446 node->prim_store->refcount++;
447
448 if (node->prim[0].no_current_update) {
449 node->current_size = 0;
450 node->current_data = NULL;
451 }
452 else {
453 node->current_size = node->vertex_size - node->attrsz[0];
454 node->current_data = NULL;
455
456 if (node->current_size) {
457 /* If the malloc fails, we just pull the data out of the VBO
458 * later instead.
459 */
460 node->current_data = malloc(node->current_size * sizeof(GLfloat));
461 if (node->current_data) {
462 const char *buffer = (const char *) save->vertex_store->buffer;
463 unsigned attr_offset = node->attrsz[0] * sizeof(GLfloat);
464 unsigned vertex_offset = 0;
465
466 if (node->count)
467 vertex_offset =
468 (node->count - 1) * node->vertex_size * sizeof(GLfloat);
469
470 memcpy(node->current_data,
471 buffer + node->buffer_offset + vertex_offset + attr_offset,
472 node->current_size * sizeof(GLfloat));
473 }
474 }
475 }
476
477 assert(node->attrsz[VBO_ATTRIB_POS] != 0 || node->count == 0);
478
479 if (save->dangling_attr_ref)
480 ctx->ListState.CurrentList->Flags |= DLIST_DANGLING_REFS;
481
482 save->vertex_store->used += save->vertex_size * node->count;
483 save->prim_store->used += node->prim_count;
484
485 /* Copy duplicated vertices
486 */
487 save->copied.nr = _save_copy_vertices(ctx, node, save->buffer);
488
489 if (node->prim[node->prim_count - 1].mode == GL_LINE_LOOP) {
490 convert_line_loop_to_strip(save, node);
491 }
492
493 merge_prims(node->prim, &node->prim_count);
494
495 /* Deal with GL_COMPILE_AND_EXECUTE:
496 */
497 if (ctx->ExecuteFlag) {
498 struct _glapi_table *dispatch = GET_DISPATCH();
499
500 _glapi_set_dispatch(ctx->Exec);
501
502 vbo_loopback_vertex_list(ctx,
503 (const GLfloat *) ((const char *) save->
504 vertex_store->buffer +
505 node->buffer_offset),
506 node->attrsz, node->prim, node->prim_count,
507 node->wrap_count, node->vertex_size);
508
509 _glapi_set_dispatch(dispatch);
510 }
511
512 /* Decide whether the storage structs are full, or can be used for
513 * the next vertex lists as well.
514 */
515 if (save->vertex_store->used >
516 VBO_SAVE_BUFFER_SIZE - 16 * (save->vertex_size + 4)) {
517
518 /* Unmap old store:
519 */
520 vbo_save_unmap_vertex_store(ctx, save->vertex_store);
521
522 /* Release old reference:
523 */
524 save->vertex_store->refcount--;
525 assert(save->vertex_store->refcount != 0);
526 save->vertex_store = NULL;
527
528 /* Allocate and map new store:
529 */
530 save->vertex_store = alloc_vertex_store(ctx);
531 save->buffer_ptr = vbo_save_map_vertex_store(ctx, save->vertex_store);
532 save->out_of_memory = save->buffer_ptr == NULL;
533 }
534 else {
535 /* update buffer_ptr for next vertex */
536 save->buffer_ptr = save->vertex_store->buffer + save->vertex_store->used;
537 }
538
539 if (save->prim_store->used > VBO_SAVE_PRIM_SIZE - 6) {
540 save->prim_store->refcount--;
541 assert(save->prim_store->refcount != 0);
542 save->prim_store = alloc_prim_store(ctx);
543 }
544
545 /* Reset our structures for the next run of vertices:
546 */
547 _save_reset_counters(ctx);
548 }
549
550
551 /**
552 * This is called when we fill a vertex buffer before we hit a glEnd().
553 * We
554 * TODO -- If no new vertices have been stored, don't bother saving it.
555 */
556 static void
557 _save_wrap_buffers(struct gl_context *ctx)
558 {
559 struct vbo_save_context *save = &vbo_context(ctx)->save;
560 GLint i = save->prim_count - 1;
561 GLenum mode;
562 GLboolean weak;
563 GLboolean no_current_update;
564
565 assert(i < (GLint) save->prim_max);
566 assert(i >= 0);
567
568 /* Close off in-progress primitive.
569 */
570 save->prim[i].count = (save->vert_count - save->prim[i].start);
571 mode = save->prim[i].mode;
572 weak = save->prim[i].weak;
573 no_current_update = save->prim[i].no_current_update;
574
575 /* store the copied vertices, and allocate a new list.
576 */
577 _save_compile_vertex_list(ctx);
578
579 /* Restart interrupted primitive
580 */
581 save->prim[0].mode = mode;
582 save->prim[0].weak = weak;
583 save->prim[0].no_current_update = no_current_update;
584 save->prim[0].begin = 0;
585 save->prim[0].end = 0;
586 save->prim[0].pad = 0;
587 save->prim[0].start = 0;
588 save->prim[0].count = 0;
589 save->prim[0].num_instances = 1;
590 save->prim[0].base_instance = 0;
591 save->prim[0].is_indirect = 0;
592 save->prim_count = 1;
593 }
594
595
596 /**
597 * Called only when buffers are wrapped as the result of filling the
598 * vertex_store struct.
599 */
600 static void
601 _save_wrap_filled_vertex(struct gl_context *ctx)
602 {
603 struct vbo_save_context *save = &vbo_context(ctx)->save;
604 fi_type *data = save->copied.buffer;
605 GLuint i;
606
607 /* Emit a glEnd to close off the last vertex list.
608 */
609 _save_wrap_buffers(ctx);
610
611 /* Copy stored stored vertices to start of new list.
612 */
613 assert(save->max_vert - save->vert_count > save->copied.nr);
614
615 for (i = 0; i < save->copied.nr; i++) {
616 memcpy(save->buffer_ptr, data, save->vertex_size * sizeof(GLfloat));
617 data += save->vertex_size;
618 save->buffer_ptr += save->vertex_size;
619 save->vert_count++;
620 }
621 }
622
623
624 static void
625 _save_copy_to_current(struct gl_context *ctx)
626 {
627 struct vbo_save_context *save = &vbo_context(ctx)->save;
628 GLuint i;
629
630 for (i = VBO_ATTRIB_POS + 1; i < VBO_ATTRIB_MAX; i++) {
631 if (save->attrsz[i]) {
632 save->currentsz[i][0] = save->attrsz[i];
633 COPY_CLEAN_4V_TYPE_AS_UNION(save->current[i], save->attrsz[i],
634 save->attrptr[i], save->attrtype[i]);
635 }
636 }
637 }
638
639
640 static void
641 _save_copy_from_current(struct gl_context *ctx)
642 {
643 struct vbo_save_context *save = &vbo_context(ctx)->save;
644 GLint i;
645
646 for (i = VBO_ATTRIB_POS + 1; i < VBO_ATTRIB_MAX; i++) {
647 switch (save->attrsz[i]) {
648 case 4:
649 save->attrptr[i][3] = save->current[i][3];
650 case 3:
651 save->attrptr[i][2] = save->current[i][2];
652 case 2:
653 save->attrptr[i][1] = save->current[i][1];
654 case 1:
655 save->attrptr[i][0] = save->current[i][0];
656 case 0:
657 break;
658 }
659 }
660 }
661
662
663 /**
664 * Called when we increase the size of a vertex attribute. For example,
665 * if we've seen one or more glTexCoord2f() calls and now we get a
666 * glTexCoord3f() call.
667 * Flush existing data, set new attrib size, replay copied vertices.
668 */
669 static void
670 _save_upgrade_vertex(struct gl_context *ctx, GLuint attr, GLuint newsz)
671 {
672 struct vbo_save_context *save = &vbo_context(ctx)->save;
673 GLuint oldsz;
674 GLuint i;
675 fi_type *tmp;
676
677 /* Store the current run of vertices, and emit a GL_END. Emit a
678 * BEGIN in the new buffer.
679 */
680 if (save->vert_count)
681 _save_wrap_buffers(ctx);
682 else
683 assert(save->copied.nr == 0);
684
685 /* Do a COPY_TO_CURRENT to ensure back-copying works for the case
686 * when the attribute already exists in the vertex and is having
687 * its size increased.
688 */
689 _save_copy_to_current(ctx);
690
691 /* Fix up sizes:
692 */
693 oldsz = save->attrsz[attr];
694 save->attrsz[attr] = newsz;
695
696 save->vertex_size += newsz - oldsz;
697 save->max_vert = ((VBO_SAVE_BUFFER_SIZE - save->vertex_store->used) /
698 save->vertex_size);
699 save->vert_count = 0;
700
701 /* Recalculate all the attrptr[] values:
702 */
703 tmp = save->vertex;
704 for (i = 0; i < VBO_ATTRIB_MAX; i++) {
705 if (save->attrsz[i]) {
706 save->attrptr[i] = tmp;
707 tmp += save->attrsz[i];
708 }
709 else {
710 save->attrptr[i] = NULL; /* will not be dereferenced. */
711 }
712 }
713
714 /* Copy from current to repopulate the vertex with correct values.
715 */
716 _save_copy_from_current(ctx);
717
718 /* Replay stored vertices to translate them to new format here.
719 *
720 * If there are copied vertices and the new (upgraded) attribute
721 * has not been defined before, this list is somewhat degenerate,
722 * and will need fixup at runtime.
723 */
724 if (save->copied.nr) {
725 const fi_type *data = save->copied.buffer;
726 fi_type *dest = save->buffer;
727 GLuint j;
728
729 /* Need to note this and fix up at runtime (or loopback):
730 */
731 if (attr != VBO_ATTRIB_POS && save->currentsz[attr][0] == 0) {
732 assert(oldsz == 0);
733 save->dangling_attr_ref = GL_TRUE;
734 }
735
736 for (i = 0; i < save->copied.nr; i++) {
737 for (j = 0; j < VBO_ATTRIB_MAX; j++) {
738 if (save->attrsz[j]) {
739 if (j == attr) {
740 if (oldsz) {
741 COPY_CLEAN_4V_TYPE_AS_UNION(dest, oldsz, data,
742 save->attrtype[j]);
743 data += oldsz;
744 dest += newsz;
745 }
746 else {
747 COPY_SZ_4V(dest, newsz, save->current[attr]);
748 dest += newsz;
749 }
750 }
751 else {
752 GLint sz = save->attrsz[j];
753 COPY_SZ_4V(dest, sz, data);
754 data += sz;
755 dest += sz;
756 }
757 }
758 }
759 }
760
761 save->buffer_ptr = dest;
762 save->vert_count += save->copied.nr;
763 }
764 }
765
766
767 /**
768 * This is called when the size of a vertex attribute changes.
769 * For example, after seeing one or more glTexCoord2f() calls we
770 * get a glTexCoord4f() or glTexCoord1f() call.
771 */
772 static void
773 save_fixup_vertex(struct gl_context *ctx, GLuint attr, GLuint sz)
774 {
775 struct vbo_save_context *save = &vbo_context(ctx)->save;
776
777 if (sz > save->attrsz[attr]) {
778 /* New size is larger. Need to flush existing vertices and get
779 * an enlarged vertex format.
780 */
781 _save_upgrade_vertex(ctx, attr, sz);
782 }
783 else if (sz < save->active_sz[attr]) {
784 GLuint i;
785 const fi_type *id = vbo_get_default_vals_as_union(save->attrtype[attr]);
786
787 /* New size is equal or smaller - just need to fill in some
788 * zeros.
789 */
790 for (i = sz; i <= save->attrsz[attr]; i++)
791 save->attrptr[attr][i - 1] = id[i - 1];
792 }
793
794 save->active_sz[attr] = sz;
795 }
796
797
798 /**
799 * Reset the current size of all vertex attributes to the default
800 * value of 0. This signals that we haven't yet seen any per-vertex
801 * commands such as glNormal3f() or glTexCoord2f().
802 */
803 static void
804 _save_reset_vertex(struct gl_context *ctx)
805 {
806 struct vbo_save_context *save = &vbo_context(ctx)->save;
807 GLuint i;
808
809 for (i = 0; i < VBO_ATTRIB_MAX; i++) {
810 save->attrsz[i] = 0;
811 save->active_sz[i] = 0;
812 }
813
814 save->vertex_size = 0;
815 }
816
817
818
819 #define ERROR(err) _mesa_compile_error(ctx, err, __func__);
820
821
822 /* Only one size for each attribute may be active at once. Eg. if
823 * Color3f is installed/active, then Color4f may not be, even if the
824 * vertex actually contains 4 color coordinates. This is because the
825 * 3f version won't otherwise set color[3] to 1.0 -- this is the job
826 * of the chooser function when switching between Color4f and Color3f.
827 */
828 #define ATTR_UNION(A, N, T, C, V0, V1, V2, V3) \
829 do { \
830 struct vbo_save_context *save = &vbo_context(ctx)->save; \
831 \
832 if (save->active_sz[A] != N) \
833 save_fixup_vertex(ctx, A, N); \
834 \
835 { \
836 C *dest = (C *)save->attrptr[A]; \
837 if (N>0) dest[0] = V0; \
838 if (N>1) dest[1] = V1; \
839 if (N>2) dest[2] = V2; \
840 if (N>3) dest[3] = V3; \
841 save->attrtype[A] = T; \
842 } \
843 \
844 if ((A) == 0) { \
845 GLuint i; \
846 \
847 for (i = 0; i < save->vertex_size; i++) \
848 save->buffer_ptr[i] = save->vertex[i]; \
849 \
850 save->buffer_ptr += save->vertex_size; \
851 \
852 if (++save->vert_count >= save->max_vert) \
853 _save_wrap_filled_vertex(ctx); \
854 } \
855 } while (0)
856
857 #define TAG(x) _save_##x
858
859 #include "vbo_attrib_tmp.h"
860
861
862
863 #define MAT( ATTR, N, face, params ) \
864 do { \
865 if (face != GL_BACK) \
866 MAT_ATTR( ATTR, N, params ); /* front */ \
867 if (face != GL_FRONT) \
868 MAT_ATTR( ATTR + 1, N, params ); /* back */ \
869 } while (0)
870
871
872 /**
873 * Save a glMaterial call found between glBegin/End.
874 * glMaterial calls outside Begin/End are handled in dlist.c.
875 */
876 static void GLAPIENTRY
877 _save_Materialfv(GLenum face, GLenum pname, const GLfloat *params)
878 {
879 GET_CURRENT_CONTEXT(ctx);
880
881 if (face != GL_FRONT && face != GL_BACK && face != GL_FRONT_AND_BACK) {
882 _mesa_compile_error(ctx, GL_INVALID_ENUM, "glMaterial(face)");
883 return;
884 }
885
886 switch (pname) {
887 case GL_EMISSION:
888 MAT(VBO_ATTRIB_MAT_FRONT_EMISSION, 4, face, params);
889 break;
890 case GL_AMBIENT:
891 MAT(VBO_ATTRIB_MAT_FRONT_AMBIENT, 4, face, params);
892 break;
893 case GL_DIFFUSE:
894 MAT(VBO_ATTRIB_MAT_FRONT_DIFFUSE, 4, face, params);
895 break;
896 case GL_SPECULAR:
897 MAT(VBO_ATTRIB_MAT_FRONT_SPECULAR, 4, face, params);
898 break;
899 case GL_SHININESS:
900 if (*params < 0 || *params > ctx->Const.MaxShininess) {
901 _mesa_compile_error(ctx, GL_INVALID_VALUE, "glMaterial(shininess)");
902 }
903 else {
904 MAT(VBO_ATTRIB_MAT_FRONT_SHININESS, 1, face, params);
905 }
906 break;
907 case GL_COLOR_INDEXES:
908 MAT(VBO_ATTRIB_MAT_FRONT_INDEXES, 3, face, params);
909 break;
910 case GL_AMBIENT_AND_DIFFUSE:
911 MAT(VBO_ATTRIB_MAT_FRONT_AMBIENT, 4, face, params);
912 MAT(VBO_ATTRIB_MAT_FRONT_DIFFUSE, 4, face, params);
913 break;
914 default:
915 _mesa_compile_error(ctx, GL_INVALID_ENUM, "glMaterial(pname)");
916 return;
917 }
918 }
919
920
921 /* Cope with EvalCoord/CallList called within a begin/end object:
922 * -- Flush current buffer
923 * -- Fallback to opcodes for the rest of the begin/end object.
924 */
925 static void
926 dlist_fallback(struct gl_context *ctx)
927 {
928 struct vbo_save_context *save = &vbo_context(ctx)->save;
929
930 if (save->vert_count || save->prim_count) {
931 if (save->prim_count > 0) {
932 /* Close off in-progress primitive. */
933 GLint i = save->prim_count - 1;
934 save->prim[i].count = save->vert_count - save->prim[i].start;
935 }
936
937 /* Need to replay this display list with loopback,
938 * unfortunately, otherwise this primitive won't be handled
939 * properly:
940 */
941 save->dangling_attr_ref = GL_TRUE;
942
943 _save_compile_vertex_list(ctx);
944 }
945
946 _save_copy_to_current(ctx);
947 _save_reset_vertex(ctx);
948 _save_reset_counters(ctx);
949 if (save->out_of_memory) {
950 _mesa_install_save_vtxfmt(ctx, &save->vtxfmt_noop);
951 }
952 else {
953 _mesa_install_save_vtxfmt(ctx, &ctx->ListState.ListVtxfmt);
954 }
955 ctx->Driver.SaveNeedFlush = GL_FALSE;
956 }
957
958
959 static void GLAPIENTRY
960 _save_EvalCoord1f(GLfloat u)
961 {
962 GET_CURRENT_CONTEXT(ctx);
963 dlist_fallback(ctx);
964 CALL_EvalCoord1f(ctx->Save, (u));
965 }
966
967 static void GLAPIENTRY
968 _save_EvalCoord1fv(const GLfloat * v)
969 {
970 GET_CURRENT_CONTEXT(ctx);
971 dlist_fallback(ctx);
972 CALL_EvalCoord1fv(ctx->Save, (v));
973 }
974
975 static void GLAPIENTRY
976 _save_EvalCoord2f(GLfloat u, GLfloat v)
977 {
978 GET_CURRENT_CONTEXT(ctx);
979 dlist_fallback(ctx);
980 CALL_EvalCoord2f(ctx->Save, (u, v));
981 }
982
983 static void GLAPIENTRY
984 _save_EvalCoord2fv(const GLfloat * v)
985 {
986 GET_CURRENT_CONTEXT(ctx);
987 dlist_fallback(ctx);
988 CALL_EvalCoord2fv(ctx->Save, (v));
989 }
990
991 static void GLAPIENTRY
992 _save_EvalPoint1(GLint i)
993 {
994 GET_CURRENT_CONTEXT(ctx);
995 dlist_fallback(ctx);
996 CALL_EvalPoint1(ctx->Save, (i));
997 }
998
999 static void GLAPIENTRY
1000 _save_EvalPoint2(GLint i, GLint j)
1001 {
1002 GET_CURRENT_CONTEXT(ctx);
1003 dlist_fallback(ctx);
1004 CALL_EvalPoint2(ctx->Save, (i, j));
1005 }
1006
1007 static void GLAPIENTRY
1008 _save_CallList(GLuint l)
1009 {
1010 GET_CURRENT_CONTEXT(ctx);
1011 dlist_fallback(ctx);
1012 CALL_CallList(ctx->Save, (l));
1013 }
1014
1015 static void GLAPIENTRY
1016 _save_CallLists(GLsizei n, GLenum type, const GLvoid * v)
1017 {
1018 GET_CURRENT_CONTEXT(ctx);
1019 dlist_fallback(ctx);
1020 CALL_CallLists(ctx->Save, (n, type, v));
1021 }
1022
1023
1024
1025 /**
1026 * Called when a glBegin is getting compiled into a display list.
1027 * Updating of ctx->Driver.CurrentSavePrimitive is already taken care of.
1028 */
1029 GLboolean
1030 vbo_save_NotifyBegin(struct gl_context *ctx, GLenum mode)
1031 {
1032 struct vbo_save_context *save = &vbo_context(ctx)->save;
1033 const GLuint i = save->prim_count++;
1034
1035 assert(i < save->prim_max);
1036 save->prim[i].mode = mode & VBO_SAVE_PRIM_MODE_MASK;
1037 save->prim[i].begin = 1;
1038 save->prim[i].end = 0;
1039 save->prim[i].weak = (mode & VBO_SAVE_PRIM_WEAK) ? 1 : 0;
1040 save->prim[i].no_current_update =
1041 (mode & VBO_SAVE_PRIM_NO_CURRENT_UPDATE) ? 1 : 0;
1042 save->prim[i].pad = 0;
1043 save->prim[i].start = save->vert_count;
1044 save->prim[i].count = 0;
1045 save->prim[i].num_instances = 1;
1046 save->prim[i].base_instance = 0;
1047 save->prim[i].is_indirect = 0;
1048
1049 if (save->out_of_memory) {
1050 _mesa_install_save_vtxfmt(ctx, &save->vtxfmt_noop);
1051 }
1052 else {
1053 _mesa_install_save_vtxfmt(ctx, &save->vtxfmt);
1054 }
1055
1056 /* We need to call vbo_save_SaveFlushVertices() if there's state change */
1057 ctx->Driver.SaveNeedFlush = GL_TRUE;
1058
1059 /* GL_TRUE means we've handled this glBegin here; don't compile a BEGIN
1060 * opcode into the display list.
1061 */
1062 return GL_TRUE;
1063 }
1064
1065
1066 static void GLAPIENTRY
1067 _save_End(void)
1068 {
1069 GET_CURRENT_CONTEXT(ctx);
1070 struct vbo_save_context *save = &vbo_context(ctx)->save;
1071 const GLint i = save->prim_count - 1;
1072
1073 ctx->Driver.CurrentSavePrimitive = PRIM_OUTSIDE_BEGIN_END;
1074 save->prim[i].end = 1;
1075 save->prim[i].count = (save->vert_count - save->prim[i].start);
1076
1077 if (i == (GLint) save->prim_max - 1) {
1078 _save_compile_vertex_list(ctx);
1079 assert(save->copied.nr == 0);
1080 }
1081
1082 /* Swap out this vertex format while outside begin/end. Any color,
1083 * etc. received between here and the next begin will be compiled
1084 * as opcodes.
1085 */
1086 if (save->out_of_memory) {
1087 _mesa_install_save_vtxfmt(ctx, &save->vtxfmt_noop);
1088 }
1089 else {
1090 _mesa_install_save_vtxfmt(ctx, &ctx->ListState.ListVtxfmt);
1091 }
1092 }
1093
1094
1095 static void GLAPIENTRY
1096 _save_Begin(GLenum mode)
1097 {
1098 GET_CURRENT_CONTEXT(ctx);
1099 (void) mode;
1100 _mesa_compile_error(ctx, GL_INVALID_OPERATION, "Recursive glBegin");
1101 }
1102
1103
1104 static void GLAPIENTRY
1105 _save_PrimitiveRestartNV(void)
1106 {
1107 GLenum curPrim;
1108 GET_CURRENT_CONTEXT(ctx);
1109
1110 curPrim = ctx->Driver.CurrentSavePrimitive;
1111
1112 _save_End();
1113 _save_Begin(curPrim);
1114 }
1115
1116
1117 /* Unlike the functions above, these are to be hooked into the vtxfmt
1118 * maintained in ctx->ListState, active when the list is known or
1119 * suspected to be outside any begin/end primitive.
1120 * Note: OBE = Outside Begin/End
1121 */
1122 static void GLAPIENTRY
1123 _save_OBE_Rectf(GLfloat x1, GLfloat y1, GLfloat x2, GLfloat y2)
1124 {
1125 GET_CURRENT_CONTEXT(ctx);
1126 vbo_save_NotifyBegin(ctx, GL_QUADS | VBO_SAVE_PRIM_WEAK);
1127 CALL_Vertex2f(GET_DISPATCH(), (x1, y1));
1128 CALL_Vertex2f(GET_DISPATCH(), (x2, y1));
1129 CALL_Vertex2f(GET_DISPATCH(), (x2, y2));
1130 CALL_Vertex2f(GET_DISPATCH(), (x1, y2));
1131 CALL_End(GET_DISPATCH(), ());
1132 }
1133
1134
1135 static void GLAPIENTRY
1136 _save_OBE_DrawArrays(GLenum mode, GLint start, GLsizei count)
1137 {
1138 GET_CURRENT_CONTEXT(ctx);
1139 struct vbo_save_context *save = &vbo_context(ctx)->save;
1140 GLint i;
1141
1142 if (!_mesa_is_valid_prim_mode(ctx, mode)) {
1143 _mesa_compile_error(ctx, GL_INVALID_ENUM, "glDrawArrays(mode)");
1144 return;
1145 }
1146 if (count < 0) {
1147 _mesa_compile_error(ctx, GL_INVALID_VALUE, "glDrawArrays(count<0)");
1148 return;
1149 }
1150
1151 if (save->out_of_memory)
1152 return;
1153
1154 _ae_map_vbos(ctx);
1155
1156 vbo_save_NotifyBegin(ctx, (mode | VBO_SAVE_PRIM_WEAK
1157 | VBO_SAVE_PRIM_NO_CURRENT_UPDATE));
1158
1159 for (i = 0; i < count; i++)
1160 CALL_ArrayElement(GET_DISPATCH(), (start + i));
1161 CALL_End(GET_DISPATCH(), ());
1162
1163 _ae_unmap_vbos(ctx);
1164 }
1165
1166
1167 /* Could do better by copying the arrays and element list intact and
1168 * then emitting an indexed prim at runtime.
1169 */
1170 static void GLAPIENTRY
1171 _save_OBE_DrawElements(GLenum mode, GLsizei count, GLenum type,
1172 const GLvoid * indices)
1173 {
1174 GET_CURRENT_CONTEXT(ctx);
1175 struct vbo_save_context *save = &vbo_context(ctx)->save;
1176 struct gl_buffer_object *indexbuf = ctx->Array.VAO->IndexBufferObj;
1177 GLint i;
1178
1179 if (!_mesa_is_valid_prim_mode(ctx, mode)) {
1180 _mesa_compile_error(ctx, GL_INVALID_ENUM, "glDrawElements(mode)");
1181 return;
1182 }
1183 if (count < 0) {
1184 _mesa_compile_error(ctx, GL_INVALID_VALUE, "glDrawElements(count<0)");
1185 return;
1186 }
1187 if (type != GL_UNSIGNED_BYTE &&
1188 type != GL_UNSIGNED_SHORT &&
1189 type != GL_UNSIGNED_INT) {
1190 _mesa_compile_error(ctx, GL_INVALID_VALUE, "glDrawElements(count<0)");
1191 return;
1192 }
1193
1194 if (save->out_of_memory)
1195 return;
1196
1197 _ae_map_vbos(ctx);
1198
1199 if (_mesa_is_bufferobj(indexbuf))
1200 indices =
1201 ADD_POINTERS(indexbuf->Mappings[MAP_INTERNAL].Pointer, indices);
1202
1203 vbo_save_NotifyBegin(ctx, (mode | VBO_SAVE_PRIM_WEAK |
1204 VBO_SAVE_PRIM_NO_CURRENT_UPDATE));
1205
1206 switch (type) {
1207 case GL_UNSIGNED_BYTE:
1208 for (i = 0; i < count; i++)
1209 CALL_ArrayElement(GET_DISPATCH(), (((GLubyte *) indices)[i]));
1210 break;
1211 case GL_UNSIGNED_SHORT:
1212 for (i = 0; i < count; i++)
1213 CALL_ArrayElement(GET_DISPATCH(), (((GLushort *) indices)[i]));
1214 break;
1215 case GL_UNSIGNED_INT:
1216 for (i = 0; i < count; i++)
1217 CALL_ArrayElement(GET_DISPATCH(), (((GLuint *) indices)[i]));
1218 break;
1219 default:
1220 _mesa_error(ctx, GL_INVALID_ENUM, "glDrawElements(type)");
1221 break;
1222 }
1223
1224 CALL_End(GET_DISPATCH(), ());
1225
1226 _ae_unmap_vbos(ctx);
1227 }
1228
1229
1230 static void GLAPIENTRY
1231 _save_OBE_DrawRangeElements(GLenum mode, GLuint start, GLuint end,
1232 GLsizei count, GLenum type,
1233 const GLvoid * indices)
1234 {
1235 GET_CURRENT_CONTEXT(ctx);
1236 struct vbo_save_context *save = &vbo_context(ctx)->save;
1237
1238 if (!_mesa_is_valid_prim_mode(ctx, mode)) {
1239 _mesa_compile_error(ctx, GL_INVALID_ENUM, "glDrawRangeElements(mode)");
1240 return;
1241 }
1242 if (count < 0) {
1243 _mesa_compile_error(ctx, GL_INVALID_VALUE,
1244 "glDrawRangeElements(count<0)");
1245 return;
1246 }
1247 if (type != GL_UNSIGNED_BYTE &&
1248 type != GL_UNSIGNED_SHORT &&
1249 type != GL_UNSIGNED_INT) {
1250 _mesa_compile_error(ctx, GL_INVALID_ENUM, "glDrawRangeElements(type)");
1251 return;
1252 }
1253 if (end < start) {
1254 _mesa_compile_error(ctx, GL_INVALID_VALUE,
1255 "glDrawRangeElements(end < start)");
1256 return;
1257 }
1258
1259 if (save->out_of_memory)
1260 return;
1261
1262 _save_OBE_DrawElements(mode, count, type, indices);
1263 }
1264
1265
1266 static void GLAPIENTRY
1267 _save_OBE_MultiDrawElements(GLenum mode, const GLsizei *count, GLenum type,
1268 const GLvoid * const *indices, GLsizei primcount)
1269 {
1270 GLsizei i;
1271
1272 for (i = 0; i < primcount; i++) {
1273 if (count[i] > 0) {
1274 CALL_DrawElements(GET_DISPATCH(), (mode, count[i], type, indices[i]));
1275 }
1276 }
1277 }
1278
1279
1280 static void GLAPIENTRY
1281 _save_OBE_MultiDrawElementsBaseVertex(GLenum mode, const GLsizei *count,
1282 GLenum type,
1283 const GLvoid * const *indices,
1284 GLsizei primcount,
1285 const GLint *basevertex)
1286 {
1287 GLsizei i;
1288
1289 for (i = 0; i < primcount; i++) {
1290 if (count[i] > 0) {
1291 CALL_DrawElementsBaseVertex(GET_DISPATCH(), (mode, count[i], type,
1292 indices[i],
1293 basevertex[i]));
1294 }
1295 }
1296 }
1297
1298
1299 static void
1300 _save_vtxfmt_init(struct gl_context *ctx)
1301 {
1302 struct vbo_save_context *save = &vbo_context(ctx)->save;
1303 GLvertexformat *vfmt = &save->vtxfmt;
1304
1305 vfmt->ArrayElement = _ae_ArrayElement;
1306
1307 vfmt->Color3f = _save_Color3f;
1308 vfmt->Color3fv = _save_Color3fv;
1309 vfmt->Color4f = _save_Color4f;
1310 vfmt->Color4fv = _save_Color4fv;
1311 vfmt->EdgeFlag = _save_EdgeFlag;
1312 vfmt->End = _save_End;
1313 vfmt->PrimitiveRestartNV = _save_PrimitiveRestartNV;
1314 vfmt->FogCoordfEXT = _save_FogCoordfEXT;
1315 vfmt->FogCoordfvEXT = _save_FogCoordfvEXT;
1316 vfmt->Indexf = _save_Indexf;
1317 vfmt->Indexfv = _save_Indexfv;
1318 vfmt->Materialfv = _save_Materialfv;
1319 vfmt->MultiTexCoord1fARB = _save_MultiTexCoord1f;
1320 vfmt->MultiTexCoord1fvARB = _save_MultiTexCoord1fv;
1321 vfmt->MultiTexCoord2fARB = _save_MultiTexCoord2f;
1322 vfmt->MultiTexCoord2fvARB = _save_MultiTexCoord2fv;
1323 vfmt->MultiTexCoord3fARB = _save_MultiTexCoord3f;
1324 vfmt->MultiTexCoord3fvARB = _save_MultiTexCoord3fv;
1325 vfmt->MultiTexCoord4fARB = _save_MultiTexCoord4f;
1326 vfmt->MultiTexCoord4fvARB = _save_MultiTexCoord4fv;
1327 vfmt->Normal3f = _save_Normal3f;
1328 vfmt->Normal3fv = _save_Normal3fv;
1329 vfmt->SecondaryColor3fEXT = _save_SecondaryColor3fEXT;
1330 vfmt->SecondaryColor3fvEXT = _save_SecondaryColor3fvEXT;
1331 vfmt->TexCoord1f = _save_TexCoord1f;
1332 vfmt->TexCoord1fv = _save_TexCoord1fv;
1333 vfmt->TexCoord2f = _save_TexCoord2f;
1334 vfmt->TexCoord2fv = _save_TexCoord2fv;
1335 vfmt->TexCoord3f = _save_TexCoord3f;
1336 vfmt->TexCoord3fv = _save_TexCoord3fv;
1337 vfmt->TexCoord4f = _save_TexCoord4f;
1338 vfmt->TexCoord4fv = _save_TexCoord4fv;
1339 vfmt->Vertex2f = _save_Vertex2f;
1340 vfmt->Vertex2fv = _save_Vertex2fv;
1341 vfmt->Vertex3f = _save_Vertex3f;
1342 vfmt->Vertex3fv = _save_Vertex3fv;
1343 vfmt->Vertex4f = _save_Vertex4f;
1344 vfmt->Vertex4fv = _save_Vertex4fv;
1345 vfmt->VertexAttrib1fARB = _save_VertexAttrib1fARB;
1346 vfmt->VertexAttrib1fvARB = _save_VertexAttrib1fvARB;
1347 vfmt->VertexAttrib2fARB = _save_VertexAttrib2fARB;
1348 vfmt->VertexAttrib2fvARB = _save_VertexAttrib2fvARB;
1349 vfmt->VertexAttrib3fARB = _save_VertexAttrib3fARB;
1350 vfmt->VertexAttrib3fvARB = _save_VertexAttrib3fvARB;
1351 vfmt->VertexAttrib4fARB = _save_VertexAttrib4fARB;
1352 vfmt->VertexAttrib4fvARB = _save_VertexAttrib4fvARB;
1353
1354 vfmt->VertexAttrib1fNV = _save_VertexAttrib1fNV;
1355 vfmt->VertexAttrib1fvNV = _save_VertexAttrib1fvNV;
1356 vfmt->VertexAttrib2fNV = _save_VertexAttrib2fNV;
1357 vfmt->VertexAttrib2fvNV = _save_VertexAttrib2fvNV;
1358 vfmt->VertexAttrib3fNV = _save_VertexAttrib3fNV;
1359 vfmt->VertexAttrib3fvNV = _save_VertexAttrib3fvNV;
1360 vfmt->VertexAttrib4fNV = _save_VertexAttrib4fNV;
1361 vfmt->VertexAttrib4fvNV = _save_VertexAttrib4fvNV;
1362
1363 /* integer-valued */
1364 vfmt->VertexAttribI1i = _save_VertexAttribI1i;
1365 vfmt->VertexAttribI2i = _save_VertexAttribI2i;
1366 vfmt->VertexAttribI3i = _save_VertexAttribI3i;
1367 vfmt->VertexAttribI4i = _save_VertexAttribI4i;
1368 vfmt->VertexAttribI2iv = _save_VertexAttribI2iv;
1369 vfmt->VertexAttribI3iv = _save_VertexAttribI3iv;
1370 vfmt->VertexAttribI4iv = _save_VertexAttribI4iv;
1371
1372 /* unsigned integer-valued */
1373 vfmt->VertexAttribI1ui = _save_VertexAttribI1ui;
1374 vfmt->VertexAttribI2ui = _save_VertexAttribI2ui;
1375 vfmt->VertexAttribI3ui = _save_VertexAttribI3ui;
1376 vfmt->VertexAttribI4ui = _save_VertexAttribI4ui;
1377 vfmt->VertexAttribI2uiv = _save_VertexAttribI2uiv;
1378 vfmt->VertexAttribI3uiv = _save_VertexAttribI3uiv;
1379 vfmt->VertexAttribI4uiv = _save_VertexAttribI4uiv;
1380
1381 vfmt->VertexP2ui = _save_VertexP2ui;
1382 vfmt->VertexP3ui = _save_VertexP3ui;
1383 vfmt->VertexP4ui = _save_VertexP4ui;
1384 vfmt->VertexP2uiv = _save_VertexP2uiv;
1385 vfmt->VertexP3uiv = _save_VertexP3uiv;
1386 vfmt->VertexP4uiv = _save_VertexP4uiv;
1387
1388 vfmt->TexCoordP1ui = _save_TexCoordP1ui;
1389 vfmt->TexCoordP2ui = _save_TexCoordP2ui;
1390 vfmt->TexCoordP3ui = _save_TexCoordP3ui;
1391 vfmt->TexCoordP4ui = _save_TexCoordP4ui;
1392 vfmt->TexCoordP1uiv = _save_TexCoordP1uiv;
1393 vfmt->TexCoordP2uiv = _save_TexCoordP2uiv;
1394 vfmt->TexCoordP3uiv = _save_TexCoordP3uiv;
1395 vfmt->TexCoordP4uiv = _save_TexCoordP4uiv;
1396
1397 vfmt->MultiTexCoordP1ui = _save_MultiTexCoordP1ui;
1398 vfmt->MultiTexCoordP2ui = _save_MultiTexCoordP2ui;
1399 vfmt->MultiTexCoordP3ui = _save_MultiTexCoordP3ui;
1400 vfmt->MultiTexCoordP4ui = _save_MultiTexCoordP4ui;
1401 vfmt->MultiTexCoordP1uiv = _save_MultiTexCoordP1uiv;
1402 vfmt->MultiTexCoordP2uiv = _save_MultiTexCoordP2uiv;
1403 vfmt->MultiTexCoordP3uiv = _save_MultiTexCoordP3uiv;
1404 vfmt->MultiTexCoordP4uiv = _save_MultiTexCoordP4uiv;
1405
1406 vfmt->NormalP3ui = _save_NormalP3ui;
1407 vfmt->NormalP3uiv = _save_NormalP3uiv;
1408
1409 vfmt->ColorP3ui = _save_ColorP3ui;
1410 vfmt->ColorP4ui = _save_ColorP4ui;
1411 vfmt->ColorP3uiv = _save_ColorP3uiv;
1412 vfmt->ColorP4uiv = _save_ColorP4uiv;
1413
1414 vfmt->SecondaryColorP3ui = _save_SecondaryColorP3ui;
1415 vfmt->SecondaryColorP3uiv = _save_SecondaryColorP3uiv;
1416
1417 vfmt->VertexAttribP1ui = _save_VertexAttribP1ui;
1418 vfmt->VertexAttribP2ui = _save_VertexAttribP2ui;
1419 vfmt->VertexAttribP3ui = _save_VertexAttribP3ui;
1420 vfmt->VertexAttribP4ui = _save_VertexAttribP4ui;
1421
1422 vfmt->VertexAttribP1uiv = _save_VertexAttribP1uiv;
1423 vfmt->VertexAttribP2uiv = _save_VertexAttribP2uiv;
1424 vfmt->VertexAttribP3uiv = _save_VertexAttribP3uiv;
1425 vfmt->VertexAttribP4uiv = _save_VertexAttribP4uiv;
1426
1427 vfmt->VertexAttribL1d = _save_VertexAttribL1d;
1428 vfmt->VertexAttribL2d = _save_VertexAttribL2d;
1429 vfmt->VertexAttribL3d = _save_VertexAttribL3d;
1430 vfmt->VertexAttribL4d = _save_VertexAttribL4d;
1431
1432 vfmt->VertexAttribL1dv = _save_VertexAttribL1dv;
1433 vfmt->VertexAttribL2dv = _save_VertexAttribL2dv;
1434 vfmt->VertexAttribL3dv = _save_VertexAttribL3dv;
1435 vfmt->VertexAttribL4dv = _save_VertexAttribL4dv;
1436
1437 /* This will all require us to fallback to saving the list as opcodes:
1438 */
1439 vfmt->CallList = _save_CallList;
1440 vfmt->CallLists = _save_CallLists;
1441
1442 vfmt->EvalCoord1f = _save_EvalCoord1f;
1443 vfmt->EvalCoord1fv = _save_EvalCoord1fv;
1444 vfmt->EvalCoord2f = _save_EvalCoord2f;
1445 vfmt->EvalCoord2fv = _save_EvalCoord2fv;
1446 vfmt->EvalPoint1 = _save_EvalPoint1;
1447 vfmt->EvalPoint2 = _save_EvalPoint2;
1448
1449 /* These calls all generate GL_INVALID_OPERATION since this vtxfmt is
1450 * only used when we're inside a glBegin/End pair.
1451 */
1452 vfmt->Begin = _save_Begin;
1453 }
1454
1455
1456 /**
1457 * Initialize the dispatch table with the VBO functions for display
1458 * list compilation.
1459 */
1460 void
1461 vbo_initialize_save_dispatch(const struct gl_context *ctx,
1462 struct _glapi_table *exec)
1463 {
1464 SET_DrawArrays(exec, _save_OBE_DrawArrays);
1465 SET_DrawElements(exec, _save_OBE_DrawElements);
1466 SET_DrawRangeElements(exec, _save_OBE_DrawRangeElements);
1467 SET_MultiDrawElementsEXT(exec, _save_OBE_MultiDrawElements);
1468 SET_MultiDrawElementsBaseVertex(exec, _save_OBE_MultiDrawElementsBaseVertex);
1469 SET_Rectf(exec, _save_OBE_Rectf);
1470 /* Note: other glDraw functins aren't compiled into display lists */
1471 }
1472
1473
1474
1475 void
1476 vbo_save_SaveFlushVertices(struct gl_context *ctx)
1477 {
1478 struct vbo_save_context *save = &vbo_context(ctx)->save;
1479
1480 /* Noop when we are actually active:
1481 */
1482 if (ctx->Driver.CurrentSavePrimitive <= PRIM_MAX)
1483 return;
1484
1485 if (save->vert_count || save->prim_count)
1486 _save_compile_vertex_list(ctx);
1487
1488 _save_copy_to_current(ctx);
1489 _save_reset_vertex(ctx);
1490 _save_reset_counters(ctx);
1491 ctx->Driver.SaveNeedFlush = GL_FALSE;
1492 }
1493
1494
1495 void
1496 vbo_save_NewList(struct gl_context *ctx, GLuint list, GLenum mode)
1497 {
1498 struct vbo_save_context *save = &vbo_context(ctx)->save;
1499
1500 (void) list;
1501 (void) mode;
1502
1503 if (!save->prim_store)
1504 save->prim_store = alloc_prim_store(ctx);
1505
1506 if (!save->vertex_store)
1507 save->vertex_store = alloc_vertex_store(ctx);
1508
1509 save->buffer_ptr = vbo_save_map_vertex_store(ctx, save->vertex_store);
1510
1511 _save_reset_vertex(ctx);
1512 _save_reset_counters(ctx);
1513 ctx->Driver.SaveNeedFlush = GL_FALSE;
1514 }
1515
1516
1517 void
1518 vbo_save_EndList(struct gl_context *ctx)
1519 {
1520 struct vbo_save_context *save = &vbo_context(ctx)->save;
1521
1522 /* EndList called inside a (saved) Begin/End pair?
1523 */
1524 if (_mesa_inside_dlist_begin_end(ctx)) {
1525 if (save->prim_count > 0) {
1526 GLint i = save->prim_count - 1;
1527 ctx->Driver.CurrentSavePrimitive = PRIM_OUTSIDE_BEGIN_END;
1528 save->prim[i].end = 0;
1529 save->prim[i].count = save->vert_count - save->prim[i].start;
1530 }
1531
1532 /* Make sure this vertex list gets replayed by the "loopback"
1533 * mechanism:
1534 */
1535 save->dangling_attr_ref = GL_TRUE;
1536 vbo_save_SaveFlushVertices(ctx);
1537
1538 /* Swap out this vertex format while outside begin/end. Any color,
1539 * etc. received between here and the next begin will be compiled
1540 * as opcodes.
1541 */
1542 _mesa_install_save_vtxfmt(ctx, &ctx->ListState.ListVtxfmt);
1543 }
1544
1545 vbo_save_unmap_vertex_store(ctx, save->vertex_store);
1546
1547 assert(save->vertex_size == 0);
1548 }
1549
1550
1551 void
1552 vbo_save_BeginCallList(struct gl_context *ctx, struct gl_display_list *dlist)
1553 {
1554 struct vbo_save_context *save = &vbo_context(ctx)->save;
1555 save->replay_flags |= dlist->Flags;
1556 }
1557
1558
1559 void
1560 vbo_save_EndCallList(struct gl_context *ctx)
1561 {
1562 struct vbo_save_context *save = &vbo_context(ctx)->save;
1563
1564 if (ctx->ListState.CallDepth == 1) {
1565 /* This is correct: want to keep only the VBO_SAVE_FALLBACK
1566 * flag, if it is set:
1567 */
1568 save->replay_flags &= VBO_SAVE_FALLBACK;
1569 }
1570 }
1571
1572
1573 static void
1574 vbo_destroy_vertex_list(struct gl_context *ctx, void *data)
1575 {
1576 struct vbo_save_vertex_list *node = (struct vbo_save_vertex_list *) data;
1577 (void) ctx;
1578
1579 if (--node->vertex_store->refcount == 0)
1580 free_vertex_store(ctx, node->vertex_store);
1581
1582 if (--node->prim_store->refcount == 0)
1583 free(node->prim_store);
1584
1585 free(node->current_data);
1586 node->current_data = NULL;
1587 }
1588
1589
1590 static void
1591 vbo_print_vertex_list(struct gl_context *ctx, void *data, FILE *f)
1592 {
1593 struct vbo_save_vertex_list *node = (struct vbo_save_vertex_list *) data;
1594 GLuint i;
1595 struct gl_buffer_object *buffer = node->vertex_store ?
1596 node->vertex_store->bufferobj : NULL;
1597 (void) ctx;
1598
1599 fprintf(f, "VBO-VERTEX-LIST, %u vertices, %d primitives, %d vertsize, "
1600 "buffer %p\n",
1601 node->count, node->prim_count, node->vertex_size,
1602 buffer);
1603
1604 for (i = 0; i < node->prim_count; i++) {
1605 struct _mesa_prim *prim = &node->prim[i];
1606 fprintf(f, " prim %d: %s%s %d..%d %s %s\n",
1607 i,
1608 _mesa_lookup_prim_by_nr(prim->mode),
1609 prim->weak ? " (weak)" : "",
1610 prim->start,
1611 prim->start + prim->count,
1612 (prim->begin) ? "BEGIN" : "(wrap)",
1613 (prim->end) ? "END" : "(wrap)");
1614 }
1615 }
1616
1617
1618 /**
1619 * Called during context creation/init.
1620 */
1621 static void
1622 _save_current_init(struct gl_context *ctx)
1623 {
1624 struct vbo_save_context *save = &vbo_context(ctx)->save;
1625 GLint i;
1626
1627 for (i = VBO_ATTRIB_POS; i <= VBO_ATTRIB_GENERIC15; i++) {
1628 const GLuint j = i - VBO_ATTRIB_POS;
1629 assert(j < VERT_ATTRIB_MAX);
1630 save->currentsz[i] = &ctx->ListState.ActiveAttribSize[j];
1631 save->current[i] = (fi_type *) ctx->ListState.CurrentAttrib[j];
1632 }
1633
1634 for (i = VBO_ATTRIB_FIRST_MATERIAL; i <= VBO_ATTRIB_LAST_MATERIAL; i++) {
1635 const GLuint j = i - VBO_ATTRIB_FIRST_MATERIAL;
1636 assert(j < MAT_ATTRIB_MAX);
1637 save->currentsz[i] = &ctx->ListState.ActiveMaterialSize[j];
1638 save->current[i] = (fi_type *) ctx->ListState.CurrentMaterial[j];
1639 }
1640 }
1641
1642
1643 /**
1644 * Initialize the display list compiler. Called during context creation.
1645 */
1646 void
1647 vbo_save_api_init(struct vbo_save_context *save)
1648 {
1649 struct gl_context *ctx = save->ctx;
1650 GLuint i;
1651
1652 save->opcode_vertex_list =
1653 _mesa_dlist_alloc_opcode(ctx,
1654 sizeof(struct vbo_save_vertex_list),
1655 vbo_save_playback_vertex_list,
1656 vbo_destroy_vertex_list,
1657 vbo_print_vertex_list);
1658
1659 _save_vtxfmt_init(ctx);
1660 _save_current_init(ctx);
1661 _mesa_noop_vtxfmt_init(&save->vtxfmt_noop);
1662
1663 /* These will actually get set again when binding/drawing */
1664 for (i = 0; i < VBO_ATTRIB_MAX; i++)
1665 save->inputs[i] = &save->arrays[i];
1666 }