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