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