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