vbo: fix incorrect min/max_index values in display list draw call
[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_noop.h"
85 #include "vbo_private.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 node->start_vertex = start_offset;
567 } else {
568 node->start_vertex = 0;
569 }
570
571 /* Reset our structures for the next run of vertices:
572 */
573 reset_counters(ctx);
574 }
575
576
577 /**
578 * This is called when we fill a vertex buffer before we hit a glEnd().
579 * We
580 * TODO -- If no new vertices have been stored, don't bother saving it.
581 */
582 static void
583 wrap_buffers(struct gl_context *ctx)
584 {
585 struct vbo_save_context *save = &vbo_context(ctx)->save;
586 GLint i = save->prim_count - 1;
587 GLenum mode;
588 GLboolean weak;
589 GLboolean no_current_update;
590
591 assert(i < (GLint) save->prim_max);
592 assert(i >= 0);
593
594 /* Close off in-progress primitive.
595 */
596 save->prims[i].count = (save->vert_count - save->prims[i].start);
597 mode = save->prims[i].mode;
598 weak = save->prims[i].weak;
599 no_current_update = save->prims[i].no_current_update;
600
601 /* store the copied vertices, and allocate a new list.
602 */
603 compile_vertex_list(ctx);
604
605 /* Restart interrupted primitive
606 */
607 save->prims[0].mode = mode;
608 save->prims[0].weak = weak;
609 save->prims[0].no_current_update = no_current_update;
610 save->prims[0].begin = 0;
611 save->prims[0].end = 0;
612 save->prims[0].pad = 0;
613 save->prims[0].start = 0;
614 save->prims[0].count = 0;
615 save->prims[0].num_instances = 1;
616 save->prims[0].base_instance = 0;
617 save->prims[0].is_indirect = 0;
618 save->prim_count = 1;
619 }
620
621
622 /**
623 * Called only when buffers are wrapped as the result of filling the
624 * vertex_store struct.
625 */
626 static void
627 wrap_filled_vertex(struct gl_context *ctx)
628 {
629 struct vbo_save_context *save = &vbo_context(ctx)->save;
630 unsigned numComponents;
631
632 /* Emit a glEnd to close off the last vertex list.
633 */
634 wrap_buffers(ctx);
635
636 /* Copy stored stored vertices to start of new list.
637 */
638 assert(save->max_vert - save->vert_count > save->copied.nr);
639
640 numComponents = save->copied.nr * save->vertex_size;
641 memcpy(save->buffer_ptr,
642 save->copied.buffer,
643 numComponents * sizeof(fi_type));
644 save->buffer_ptr += numComponents;
645 save->vert_count += save->copied.nr;
646 }
647
648
649 static void
650 copy_to_current(struct gl_context *ctx)
651 {
652 struct vbo_save_context *save = &vbo_context(ctx)->save;
653 GLbitfield64 enabled = save->enabled & (~BITFIELD64_BIT(VBO_ATTRIB_POS));
654
655 while (enabled) {
656 const int i = u_bit_scan64(&enabled);
657 assert(save->attrsz[i]);
658
659 save->currentsz[i][0] = save->attrsz[i];
660 COPY_CLEAN_4V_TYPE_AS_UNION(save->current[i], save->attrsz[i],
661 save->attrptr[i], save->attrtype[i]);
662 }
663 }
664
665
666 static void
667 copy_from_current(struct gl_context *ctx)
668 {
669 struct vbo_save_context *save = &vbo_context(ctx)->save;
670 GLbitfield64 enabled = save->enabled & (~BITFIELD64_BIT(VBO_ATTRIB_POS));
671
672 while (enabled) {
673 const int i = u_bit_scan64(&enabled);
674
675 switch (save->attrsz[i]) {
676 case 4:
677 save->attrptr[i][3] = save->current[i][3];
678 case 3:
679 save->attrptr[i][2] = save->current[i][2];
680 case 2:
681 save->attrptr[i][1] = save->current[i][1];
682 case 1:
683 save->attrptr[i][0] = save->current[i][0];
684 break;
685 case 0:
686 unreachable("Unexpected vertex attribute size");
687 }
688 }
689 }
690
691
692 /**
693 * Called when we increase the size of a vertex attribute. For example,
694 * if we've seen one or more glTexCoord2f() calls and now we get a
695 * glTexCoord3f() call.
696 * Flush existing data, set new attrib size, replay copied vertices.
697 */
698 static void
699 upgrade_vertex(struct gl_context *ctx, GLuint attr, GLuint newsz)
700 {
701 struct vbo_save_context *save = &vbo_context(ctx)->save;
702 GLuint oldsz;
703 GLuint i;
704 fi_type *tmp;
705
706 /* Store the current run of vertices, and emit a GL_END. Emit a
707 * BEGIN in the new buffer.
708 */
709 if (save->vert_count)
710 wrap_buffers(ctx);
711 else
712 assert(save->copied.nr == 0);
713
714 /* Do a COPY_TO_CURRENT to ensure back-copying works for the case
715 * when the attribute already exists in the vertex and is having
716 * its size increased.
717 */
718 copy_to_current(ctx);
719
720 /* Fix up sizes:
721 */
722 oldsz = save->attrsz[attr];
723 save->attrsz[attr] = newsz;
724 save->enabled |= BITFIELD64_BIT(attr);
725
726 save->vertex_size += newsz - oldsz;
727 save->max_vert = ((VBO_SAVE_BUFFER_SIZE - save->vertex_store->used) /
728 save->vertex_size);
729 save->vert_count = 0;
730
731 /* Recalculate all the attrptr[] values:
732 */
733 tmp = save->vertex;
734 for (i = 0; i < VBO_ATTRIB_MAX; i++) {
735 if (save->attrsz[i]) {
736 save->attrptr[i] = tmp;
737 tmp += save->attrsz[i];
738 }
739 else {
740 save->attrptr[i] = NULL; /* will not be dereferenced. */
741 }
742 }
743
744 /* Copy from current to repopulate the vertex with correct values.
745 */
746 copy_from_current(ctx);
747
748 /* Replay stored vertices to translate them to new format here.
749 *
750 * If there are copied vertices and the new (upgraded) attribute
751 * has not been defined before, this list is somewhat degenerate,
752 * and will need fixup at runtime.
753 */
754 if (save->copied.nr) {
755 const fi_type *data = save->copied.buffer;
756 fi_type *dest = save->buffer_map;
757
758 /* Need to note this and fix up at runtime (or loopback):
759 */
760 if (attr != VBO_ATTRIB_POS && save->currentsz[attr][0] == 0) {
761 assert(oldsz == 0);
762 save->dangling_attr_ref = GL_TRUE;
763 }
764
765 for (i = 0; i < save->copied.nr; i++) {
766 GLbitfield64 enabled = save->enabled;
767 while (enabled) {
768 const int j = u_bit_scan64(&enabled);
769 assert(save->attrsz[j]);
770 if (j == attr) {
771 if (oldsz) {
772 COPY_CLEAN_4V_TYPE_AS_UNION(dest, oldsz, data,
773 save->attrtype[j]);
774 data += oldsz;
775 dest += newsz;
776 }
777 else {
778 COPY_SZ_4V(dest, newsz, save->current[attr]);
779 dest += newsz;
780 }
781 }
782 else {
783 GLint sz = save->attrsz[j];
784 COPY_SZ_4V(dest, sz, data);
785 data += sz;
786 dest += sz;
787 }
788 }
789 }
790
791 save->buffer_ptr = dest;
792 save->vert_count += save->copied.nr;
793 }
794 }
795
796
797 /**
798 * This is called when the size of a vertex attribute changes.
799 * For example, after seeing one or more glTexCoord2f() calls we
800 * get a glTexCoord4f() or glTexCoord1f() call.
801 */
802 static void
803 fixup_vertex(struct gl_context *ctx, GLuint attr, GLuint sz)
804 {
805 struct vbo_save_context *save = &vbo_context(ctx)->save;
806
807 if (sz > save->attrsz[attr]) {
808 /* New size is larger. Need to flush existing vertices and get
809 * an enlarged vertex format.
810 */
811 upgrade_vertex(ctx, attr, sz);
812 }
813 else if (sz < save->active_sz[attr]) {
814 GLuint i;
815 const fi_type *id = vbo_get_default_vals_as_union(save->attrtype[attr]);
816
817 /* New size is equal or smaller - just need to fill in some
818 * zeros.
819 */
820 for (i = sz; i <= save->attrsz[attr]; i++)
821 save->attrptr[attr][i - 1] = id[i - 1];
822 }
823
824 save->active_sz[attr] = sz;
825 }
826
827
828 /**
829 * Reset the current size of all vertex attributes to the default
830 * value of 0. This signals that we haven't yet seen any per-vertex
831 * commands such as glNormal3f() or glTexCoord2f().
832 */
833 static void
834 reset_vertex(struct gl_context *ctx)
835 {
836 struct vbo_save_context *save = &vbo_context(ctx)->save;
837
838 while (save->enabled) {
839 const int i = u_bit_scan64(&save->enabled);
840 assert(save->attrsz[i]);
841 save->attrsz[i] = 0;
842 save->active_sz[i] = 0;
843 }
844
845 save->vertex_size = 0;
846 }
847
848
849
850 #define ERROR(err) _mesa_compile_error(ctx, err, __func__);
851
852
853 /* Only one size for each attribute may be active at once. Eg. if
854 * Color3f is installed/active, then Color4f may not be, even if the
855 * vertex actually contains 4 color coordinates. This is because the
856 * 3f version won't otherwise set color[3] to 1.0 -- this is the job
857 * of the chooser function when switching between Color4f and Color3f.
858 */
859 #define ATTR_UNION(A, N, T, C, V0, V1, V2, V3) \
860 do { \
861 struct vbo_save_context *save = &vbo_context(ctx)->save; \
862 \
863 if (save->active_sz[A] != N) \
864 fixup_vertex(ctx, A, N); \
865 \
866 { \
867 C *dest = (C *)save->attrptr[A]; \
868 if (N>0) dest[0] = V0; \
869 if (N>1) dest[1] = V1; \
870 if (N>2) dest[2] = V2; \
871 if (N>3) dest[3] = V3; \
872 save->attrtype[A] = T; \
873 } \
874 \
875 if ((A) == 0) { \
876 GLuint i; \
877 \
878 for (i = 0; i < save->vertex_size; i++) \
879 save->buffer_ptr[i] = save->vertex[i]; \
880 \
881 save->buffer_ptr += save->vertex_size; \
882 \
883 if (++save->vert_count >= save->max_vert) \
884 wrap_filled_vertex(ctx); \
885 } \
886 } while (0)
887
888 #define TAG(x) _save_##x
889
890 #include "vbo_attrib_tmp.h"
891
892
893
894 #define MAT( ATTR, N, face, params ) \
895 do { \
896 if (face != GL_BACK) \
897 MAT_ATTR( ATTR, N, params ); /* front */ \
898 if (face != GL_FRONT) \
899 MAT_ATTR( ATTR + 1, N, params ); /* back */ \
900 } while (0)
901
902
903 /**
904 * Save a glMaterial call found between glBegin/End.
905 * glMaterial calls outside Begin/End are handled in dlist.c.
906 */
907 static void GLAPIENTRY
908 _save_Materialfv(GLenum face, GLenum pname, const GLfloat *params)
909 {
910 GET_CURRENT_CONTEXT(ctx);
911
912 if (face != GL_FRONT && face != GL_BACK && face != GL_FRONT_AND_BACK) {
913 _mesa_compile_error(ctx, GL_INVALID_ENUM, "glMaterial(face)");
914 return;
915 }
916
917 switch (pname) {
918 case GL_EMISSION:
919 MAT(VBO_ATTRIB_MAT_FRONT_EMISSION, 4, face, params);
920 break;
921 case GL_AMBIENT:
922 MAT(VBO_ATTRIB_MAT_FRONT_AMBIENT, 4, face, params);
923 break;
924 case GL_DIFFUSE:
925 MAT(VBO_ATTRIB_MAT_FRONT_DIFFUSE, 4, face, params);
926 break;
927 case GL_SPECULAR:
928 MAT(VBO_ATTRIB_MAT_FRONT_SPECULAR, 4, face, params);
929 break;
930 case GL_SHININESS:
931 if (*params < 0 || *params > ctx->Const.MaxShininess) {
932 _mesa_compile_error(ctx, GL_INVALID_VALUE, "glMaterial(shininess)");
933 }
934 else {
935 MAT(VBO_ATTRIB_MAT_FRONT_SHININESS, 1, face, params);
936 }
937 break;
938 case GL_COLOR_INDEXES:
939 MAT(VBO_ATTRIB_MAT_FRONT_INDEXES, 3, face, params);
940 break;
941 case GL_AMBIENT_AND_DIFFUSE:
942 MAT(VBO_ATTRIB_MAT_FRONT_AMBIENT, 4, face, params);
943 MAT(VBO_ATTRIB_MAT_FRONT_DIFFUSE, 4, face, params);
944 break;
945 default:
946 _mesa_compile_error(ctx, GL_INVALID_ENUM, "glMaterial(pname)");
947 return;
948 }
949 }
950
951
952 /* Cope with EvalCoord/CallList called within a begin/end object:
953 * -- Flush current buffer
954 * -- Fallback to opcodes for the rest of the begin/end object.
955 */
956 static void
957 dlist_fallback(struct gl_context *ctx)
958 {
959 struct vbo_save_context *save = &vbo_context(ctx)->save;
960
961 if (save->vert_count || save->prim_count) {
962 if (save->prim_count > 0) {
963 /* Close off in-progress primitive. */
964 GLint i = save->prim_count - 1;
965 save->prims[i].count = save->vert_count - save->prims[i].start;
966 }
967
968 /* Need to replay this display list with loopback,
969 * unfortunately, otherwise this primitive won't be handled
970 * properly:
971 */
972 save->dangling_attr_ref = GL_TRUE;
973
974 compile_vertex_list(ctx);
975 }
976
977 copy_to_current(ctx);
978 reset_vertex(ctx);
979 reset_counters(ctx);
980 if (save->out_of_memory) {
981 _mesa_install_save_vtxfmt(ctx, &save->vtxfmt_noop);
982 }
983 else {
984 _mesa_install_save_vtxfmt(ctx, &ctx->ListState.ListVtxfmt);
985 }
986 ctx->Driver.SaveNeedFlush = GL_FALSE;
987 }
988
989
990 static void GLAPIENTRY
991 _save_EvalCoord1f(GLfloat u)
992 {
993 GET_CURRENT_CONTEXT(ctx);
994 dlist_fallback(ctx);
995 CALL_EvalCoord1f(ctx->Save, (u));
996 }
997
998 static void GLAPIENTRY
999 _save_EvalCoord1fv(const GLfloat * v)
1000 {
1001 GET_CURRENT_CONTEXT(ctx);
1002 dlist_fallback(ctx);
1003 CALL_EvalCoord1fv(ctx->Save, (v));
1004 }
1005
1006 static void GLAPIENTRY
1007 _save_EvalCoord2f(GLfloat u, GLfloat v)
1008 {
1009 GET_CURRENT_CONTEXT(ctx);
1010 dlist_fallback(ctx);
1011 CALL_EvalCoord2f(ctx->Save, (u, v));
1012 }
1013
1014 static void GLAPIENTRY
1015 _save_EvalCoord2fv(const GLfloat * v)
1016 {
1017 GET_CURRENT_CONTEXT(ctx);
1018 dlist_fallback(ctx);
1019 CALL_EvalCoord2fv(ctx->Save, (v));
1020 }
1021
1022 static void GLAPIENTRY
1023 _save_EvalPoint1(GLint i)
1024 {
1025 GET_CURRENT_CONTEXT(ctx);
1026 dlist_fallback(ctx);
1027 CALL_EvalPoint1(ctx->Save, (i));
1028 }
1029
1030 static void GLAPIENTRY
1031 _save_EvalPoint2(GLint i, GLint j)
1032 {
1033 GET_CURRENT_CONTEXT(ctx);
1034 dlist_fallback(ctx);
1035 CALL_EvalPoint2(ctx->Save, (i, j));
1036 }
1037
1038 static void GLAPIENTRY
1039 _save_CallList(GLuint l)
1040 {
1041 GET_CURRENT_CONTEXT(ctx);
1042 dlist_fallback(ctx);
1043 CALL_CallList(ctx->Save, (l));
1044 }
1045
1046 static void GLAPIENTRY
1047 _save_CallLists(GLsizei n, GLenum type, const GLvoid * v)
1048 {
1049 GET_CURRENT_CONTEXT(ctx);
1050 dlist_fallback(ctx);
1051 CALL_CallLists(ctx->Save, (n, type, v));
1052 }
1053
1054
1055
1056 /**
1057 * Called when a glBegin is getting compiled into a display list.
1058 * Updating of ctx->Driver.CurrentSavePrimitive is already taken care of.
1059 */
1060 void
1061 vbo_save_NotifyBegin(struct gl_context *ctx, GLenum mode)
1062 {
1063 struct vbo_save_context *save = &vbo_context(ctx)->save;
1064 const GLuint i = save->prim_count++;
1065
1066 assert(i < save->prim_max);
1067 save->prims[i].mode = mode & VBO_SAVE_PRIM_MODE_MASK;
1068 save->prims[i].begin = 1;
1069 save->prims[i].end = 0;
1070 save->prims[i].weak = (mode & VBO_SAVE_PRIM_WEAK) ? 1 : 0;
1071 save->prims[i].no_current_update =
1072 (mode & VBO_SAVE_PRIM_NO_CURRENT_UPDATE) ? 1 : 0;
1073 save->prims[i].pad = 0;
1074 save->prims[i].start = save->vert_count;
1075 save->prims[i].count = 0;
1076 save->prims[i].num_instances = 1;
1077 save->prims[i].base_instance = 0;
1078 save->prims[i].is_indirect = 0;
1079
1080 if (save->out_of_memory) {
1081 _mesa_install_save_vtxfmt(ctx, &save->vtxfmt_noop);
1082 }
1083 else {
1084 _mesa_install_save_vtxfmt(ctx, &save->vtxfmt);
1085 }
1086
1087 /* We need to call vbo_save_SaveFlushVertices() if there's state change */
1088 ctx->Driver.SaveNeedFlush = GL_TRUE;
1089 }
1090
1091
1092 static void GLAPIENTRY
1093 _save_End(void)
1094 {
1095 GET_CURRENT_CONTEXT(ctx);
1096 struct vbo_save_context *save = &vbo_context(ctx)->save;
1097 const GLint i = save->prim_count - 1;
1098
1099 ctx->Driver.CurrentSavePrimitive = PRIM_OUTSIDE_BEGIN_END;
1100 save->prims[i].end = 1;
1101 save->prims[i].count = (save->vert_count - save->prims[i].start);
1102
1103 if (i == (GLint) save->prim_max - 1) {
1104 compile_vertex_list(ctx);
1105 assert(save->copied.nr == 0);
1106 }
1107
1108 /* Swap out this vertex format while outside begin/end. Any color,
1109 * etc. received between here and the next begin will be compiled
1110 * as opcodes.
1111 */
1112 if (save->out_of_memory) {
1113 _mesa_install_save_vtxfmt(ctx, &save->vtxfmt_noop);
1114 }
1115 else {
1116 _mesa_install_save_vtxfmt(ctx, &ctx->ListState.ListVtxfmt);
1117 }
1118 }
1119
1120
1121 static void GLAPIENTRY
1122 _save_Begin(GLenum mode)
1123 {
1124 GET_CURRENT_CONTEXT(ctx);
1125 (void) mode;
1126 _mesa_compile_error(ctx, GL_INVALID_OPERATION, "Recursive glBegin");
1127 }
1128
1129
1130 static void GLAPIENTRY
1131 _save_PrimitiveRestartNV(void)
1132 {
1133 GET_CURRENT_CONTEXT(ctx);
1134 struct vbo_save_context *save = &vbo_context(ctx)->save;
1135
1136 if (save->prim_count == 0) {
1137 /* We're not inside a glBegin/End pair, so calling glPrimitiverRestartNV
1138 * is an error.
1139 */
1140 _mesa_compile_error(ctx, GL_INVALID_OPERATION,
1141 "glPrimitiveRestartNV called outside glBegin/End");
1142 } else {
1143 /* get current primitive mode */
1144 GLenum curPrim = save->prims[save->prim_count - 1].mode;
1145
1146 /* restart primitive */
1147 CALL_End(GET_DISPATCH(), ());
1148 vbo_save_NotifyBegin(ctx, curPrim);
1149 }
1150 }
1151
1152
1153 /* Unlike the functions above, these are to be hooked into the vtxfmt
1154 * maintained in ctx->ListState, active when the list is known or
1155 * suspected to be outside any begin/end primitive.
1156 * Note: OBE = Outside Begin/End
1157 */
1158 static void GLAPIENTRY
1159 _save_OBE_Rectf(GLfloat x1, GLfloat y1, GLfloat x2, GLfloat y2)
1160 {
1161 GET_CURRENT_CONTEXT(ctx);
1162 vbo_save_NotifyBegin(ctx, GL_QUADS | VBO_SAVE_PRIM_WEAK);
1163 CALL_Vertex2f(GET_DISPATCH(), (x1, y1));
1164 CALL_Vertex2f(GET_DISPATCH(), (x2, y1));
1165 CALL_Vertex2f(GET_DISPATCH(), (x2, y2));
1166 CALL_Vertex2f(GET_DISPATCH(), (x1, y2));
1167 CALL_End(GET_DISPATCH(), ());
1168 }
1169
1170
1171 static void GLAPIENTRY
1172 _save_OBE_DrawArrays(GLenum mode, GLint start, GLsizei count)
1173 {
1174 GET_CURRENT_CONTEXT(ctx);
1175 struct vbo_save_context *save = &vbo_context(ctx)->save;
1176 GLint i;
1177
1178 if (!_mesa_is_valid_prim_mode(ctx, mode)) {
1179 _mesa_compile_error(ctx, GL_INVALID_ENUM, "glDrawArrays(mode)");
1180 return;
1181 }
1182 if (count < 0) {
1183 _mesa_compile_error(ctx, GL_INVALID_VALUE, "glDrawArrays(count<0)");
1184 return;
1185 }
1186
1187 if (save->out_of_memory)
1188 return;
1189
1190 /* Make sure to process any VBO binding changes */
1191 _mesa_update_state(ctx);
1192
1193 _ae_map_vbos(ctx);
1194
1195 vbo_save_NotifyBegin(ctx, (mode | VBO_SAVE_PRIM_WEAK
1196 | VBO_SAVE_PRIM_NO_CURRENT_UPDATE));
1197
1198 for (i = 0; i < count; i++)
1199 CALL_ArrayElement(GET_DISPATCH(), (start + i));
1200 CALL_End(GET_DISPATCH(), ());
1201
1202 _ae_unmap_vbos(ctx);
1203 }
1204
1205
1206 static void GLAPIENTRY
1207 _save_OBE_MultiDrawArrays(GLenum mode, const GLint *first,
1208 const GLsizei *count, GLsizei primcount)
1209 {
1210 GET_CURRENT_CONTEXT(ctx);
1211 GLint i;
1212
1213 if (!_mesa_is_valid_prim_mode(ctx, mode)) {
1214 _mesa_compile_error(ctx, GL_INVALID_ENUM, "glMultiDrawArrays(mode)");
1215 return;
1216 }
1217
1218 if (primcount < 0) {
1219 _mesa_compile_error(ctx, GL_INVALID_VALUE,
1220 "glMultiDrawArrays(primcount<0)");
1221 return;
1222 }
1223
1224 for (i = 0; i < primcount; i++) {
1225 if (count[i] < 0) {
1226 _mesa_compile_error(ctx, GL_INVALID_VALUE,
1227 "glMultiDrawArrays(count[i]<0)");
1228 return;
1229 }
1230 }
1231
1232 for (i = 0; i < primcount; i++) {
1233 if (count[i] > 0) {
1234 _save_OBE_DrawArrays(mode, first[i], count[i]);
1235 }
1236 }
1237 }
1238
1239
1240 /* Could do better by copying the arrays and element list intact and
1241 * then emitting an indexed prim at runtime.
1242 */
1243 static void GLAPIENTRY
1244 _save_OBE_DrawElementsBaseVertex(GLenum mode, GLsizei count, GLenum type,
1245 const GLvoid * indices, GLint basevertex)
1246 {
1247 GET_CURRENT_CONTEXT(ctx);
1248 struct vbo_save_context *save = &vbo_context(ctx)->save;
1249 struct gl_buffer_object *indexbuf = ctx->Array.VAO->IndexBufferObj;
1250 GLint i;
1251
1252 if (!_mesa_is_valid_prim_mode(ctx, mode)) {
1253 _mesa_compile_error(ctx, GL_INVALID_ENUM, "glDrawElements(mode)");
1254 return;
1255 }
1256 if (count < 0) {
1257 _mesa_compile_error(ctx, GL_INVALID_VALUE, "glDrawElements(count<0)");
1258 return;
1259 }
1260 if (type != GL_UNSIGNED_BYTE &&
1261 type != GL_UNSIGNED_SHORT &&
1262 type != GL_UNSIGNED_INT) {
1263 _mesa_compile_error(ctx, GL_INVALID_VALUE, "glDrawElements(count<0)");
1264 return;
1265 }
1266
1267 if (save->out_of_memory)
1268 return;
1269
1270 /* Make sure to process any VBO binding changes */
1271 _mesa_update_state(ctx);
1272
1273 _ae_map_vbos(ctx);
1274
1275 if (_mesa_is_bufferobj(indexbuf))
1276 indices =
1277 ADD_POINTERS(indexbuf->Mappings[MAP_INTERNAL].Pointer, indices);
1278
1279 vbo_save_NotifyBegin(ctx, (mode | VBO_SAVE_PRIM_WEAK |
1280 VBO_SAVE_PRIM_NO_CURRENT_UPDATE));
1281
1282 switch (type) {
1283 case GL_UNSIGNED_BYTE:
1284 for (i = 0; i < count; i++)
1285 CALL_ArrayElement(GET_DISPATCH(), (basevertex + ((GLubyte *) indices)[i]));
1286 break;
1287 case GL_UNSIGNED_SHORT:
1288 for (i = 0; i < count; i++)
1289 CALL_ArrayElement(GET_DISPATCH(), (basevertex + ((GLushort *) indices)[i]));
1290 break;
1291 case GL_UNSIGNED_INT:
1292 for (i = 0; i < count; i++)
1293 CALL_ArrayElement(GET_DISPATCH(), (basevertex + ((GLuint *) indices)[i]));
1294 break;
1295 default:
1296 _mesa_error(ctx, GL_INVALID_ENUM, "glDrawElements(type)");
1297 break;
1298 }
1299
1300 CALL_End(GET_DISPATCH(), ());
1301
1302 _ae_unmap_vbos(ctx);
1303 }
1304
1305 static void GLAPIENTRY
1306 _save_OBE_DrawElements(GLenum mode, GLsizei count, GLenum type,
1307 const GLvoid * indices)
1308 {
1309 _save_OBE_DrawElementsBaseVertex(mode, count, type, indices, 0);
1310 }
1311
1312
1313 static void GLAPIENTRY
1314 _save_OBE_DrawRangeElements(GLenum mode, GLuint start, GLuint end,
1315 GLsizei count, GLenum type,
1316 const GLvoid * indices)
1317 {
1318 GET_CURRENT_CONTEXT(ctx);
1319 struct vbo_save_context *save = &vbo_context(ctx)->save;
1320
1321 if (!_mesa_is_valid_prim_mode(ctx, mode)) {
1322 _mesa_compile_error(ctx, GL_INVALID_ENUM, "glDrawRangeElements(mode)");
1323 return;
1324 }
1325 if (count < 0) {
1326 _mesa_compile_error(ctx, GL_INVALID_VALUE,
1327 "glDrawRangeElements(count<0)");
1328 return;
1329 }
1330 if (type != GL_UNSIGNED_BYTE &&
1331 type != GL_UNSIGNED_SHORT &&
1332 type != GL_UNSIGNED_INT) {
1333 _mesa_compile_error(ctx, GL_INVALID_ENUM, "glDrawRangeElements(type)");
1334 return;
1335 }
1336 if (end < start) {
1337 _mesa_compile_error(ctx, GL_INVALID_VALUE,
1338 "glDrawRangeElements(end < start)");
1339 return;
1340 }
1341
1342 if (save->out_of_memory)
1343 return;
1344
1345 _save_OBE_DrawElements(mode, count, type, indices);
1346 }
1347
1348
1349 static void GLAPIENTRY
1350 _save_OBE_MultiDrawElements(GLenum mode, const GLsizei *count, GLenum type,
1351 const GLvoid * const *indices, GLsizei primcount)
1352 {
1353 GLsizei i;
1354
1355 for (i = 0; i < primcount; i++) {
1356 if (count[i] > 0) {
1357 CALL_DrawElements(GET_DISPATCH(), (mode, count[i], type, indices[i]));
1358 }
1359 }
1360 }
1361
1362
1363 static void GLAPIENTRY
1364 _save_OBE_MultiDrawElementsBaseVertex(GLenum mode, const GLsizei *count,
1365 GLenum type,
1366 const GLvoid * const *indices,
1367 GLsizei primcount,
1368 const GLint *basevertex)
1369 {
1370 GLsizei i;
1371
1372 for (i = 0; i < primcount; i++) {
1373 if (count[i] > 0) {
1374 CALL_DrawElementsBaseVertex(GET_DISPATCH(), (mode, count[i], type,
1375 indices[i],
1376 basevertex[i]));
1377 }
1378 }
1379 }
1380
1381
1382 static void
1383 vtxfmt_init(struct gl_context *ctx)
1384 {
1385 struct vbo_save_context *save = &vbo_context(ctx)->save;
1386 GLvertexformat *vfmt = &save->vtxfmt;
1387
1388 vfmt->ArrayElement = _ae_ArrayElement;
1389
1390 vfmt->Color3f = _save_Color3f;
1391 vfmt->Color3fv = _save_Color3fv;
1392 vfmt->Color4f = _save_Color4f;
1393 vfmt->Color4fv = _save_Color4fv;
1394 vfmt->EdgeFlag = _save_EdgeFlag;
1395 vfmt->End = _save_End;
1396 vfmt->PrimitiveRestartNV = _save_PrimitiveRestartNV;
1397 vfmt->FogCoordfEXT = _save_FogCoordfEXT;
1398 vfmt->FogCoordfvEXT = _save_FogCoordfvEXT;
1399 vfmt->Indexf = _save_Indexf;
1400 vfmt->Indexfv = _save_Indexfv;
1401 vfmt->Materialfv = _save_Materialfv;
1402 vfmt->MultiTexCoord1fARB = _save_MultiTexCoord1f;
1403 vfmt->MultiTexCoord1fvARB = _save_MultiTexCoord1fv;
1404 vfmt->MultiTexCoord2fARB = _save_MultiTexCoord2f;
1405 vfmt->MultiTexCoord2fvARB = _save_MultiTexCoord2fv;
1406 vfmt->MultiTexCoord3fARB = _save_MultiTexCoord3f;
1407 vfmt->MultiTexCoord3fvARB = _save_MultiTexCoord3fv;
1408 vfmt->MultiTexCoord4fARB = _save_MultiTexCoord4f;
1409 vfmt->MultiTexCoord4fvARB = _save_MultiTexCoord4fv;
1410 vfmt->Normal3f = _save_Normal3f;
1411 vfmt->Normal3fv = _save_Normal3fv;
1412 vfmt->SecondaryColor3fEXT = _save_SecondaryColor3fEXT;
1413 vfmt->SecondaryColor3fvEXT = _save_SecondaryColor3fvEXT;
1414 vfmt->TexCoord1f = _save_TexCoord1f;
1415 vfmt->TexCoord1fv = _save_TexCoord1fv;
1416 vfmt->TexCoord2f = _save_TexCoord2f;
1417 vfmt->TexCoord2fv = _save_TexCoord2fv;
1418 vfmt->TexCoord3f = _save_TexCoord3f;
1419 vfmt->TexCoord3fv = _save_TexCoord3fv;
1420 vfmt->TexCoord4f = _save_TexCoord4f;
1421 vfmt->TexCoord4fv = _save_TexCoord4fv;
1422 vfmt->Vertex2f = _save_Vertex2f;
1423 vfmt->Vertex2fv = _save_Vertex2fv;
1424 vfmt->Vertex3f = _save_Vertex3f;
1425 vfmt->Vertex3fv = _save_Vertex3fv;
1426 vfmt->Vertex4f = _save_Vertex4f;
1427 vfmt->Vertex4fv = _save_Vertex4fv;
1428 vfmt->VertexAttrib1fARB = _save_VertexAttrib1fARB;
1429 vfmt->VertexAttrib1fvARB = _save_VertexAttrib1fvARB;
1430 vfmt->VertexAttrib2fARB = _save_VertexAttrib2fARB;
1431 vfmt->VertexAttrib2fvARB = _save_VertexAttrib2fvARB;
1432 vfmt->VertexAttrib3fARB = _save_VertexAttrib3fARB;
1433 vfmt->VertexAttrib3fvARB = _save_VertexAttrib3fvARB;
1434 vfmt->VertexAttrib4fARB = _save_VertexAttrib4fARB;
1435 vfmt->VertexAttrib4fvARB = _save_VertexAttrib4fvARB;
1436
1437 vfmt->VertexAttrib1fNV = _save_VertexAttrib1fNV;
1438 vfmt->VertexAttrib1fvNV = _save_VertexAttrib1fvNV;
1439 vfmt->VertexAttrib2fNV = _save_VertexAttrib2fNV;
1440 vfmt->VertexAttrib2fvNV = _save_VertexAttrib2fvNV;
1441 vfmt->VertexAttrib3fNV = _save_VertexAttrib3fNV;
1442 vfmt->VertexAttrib3fvNV = _save_VertexAttrib3fvNV;
1443 vfmt->VertexAttrib4fNV = _save_VertexAttrib4fNV;
1444 vfmt->VertexAttrib4fvNV = _save_VertexAttrib4fvNV;
1445
1446 /* integer-valued */
1447 vfmt->VertexAttribI1i = _save_VertexAttribI1i;
1448 vfmt->VertexAttribI2i = _save_VertexAttribI2i;
1449 vfmt->VertexAttribI3i = _save_VertexAttribI3i;
1450 vfmt->VertexAttribI4i = _save_VertexAttribI4i;
1451 vfmt->VertexAttribI2iv = _save_VertexAttribI2iv;
1452 vfmt->VertexAttribI3iv = _save_VertexAttribI3iv;
1453 vfmt->VertexAttribI4iv = _save_VertexAttribI4iv;
1454
1455 /* unsigned integer-valued */
1456 vfmt->VertexAttribI1ui = _save_VertexAttribI1ui;
1457 vfmt->VertexAttribI2ui = _save_VertexAttribI2ui;
1458 vfmt->VertexAttribI3ui = _save_VertexAttribI3ui;
1459 vfmt->VertexAttribI4ui = _save_VertexAttribI4ui;
1460 vfmt->VertexAttribI2uiv = _save_VertexAttribI2uiv;
1461 vfmt->VertexAttribI3uiv = _save_VertexAttribI3uiv;
1462 vfmt->VertexAttribI4uiv = _save_VertexAttribI4uiv;
1463
1464 vfmt->VertexP2ui = _save_VertexP2ui;
1465 vfmt->VertexP3ui = _save_VertexP3ui;
1466 vfmt->VertexP4ui = _save_VertexP4ui;
1467 vfmt->VertexP2uiv = _save_VertexP2uiv;
1468 vfmt->VertexP3uiv = _save_VertexP3uiv;
1469 vfmt->VertexP4uiv = _save_VertexP4uiv;
1470
1471 vfmt->TexCoordP1ui = _save_TexCoordP1ui;
1472 vfmt->TexCoordP2ui = _save_TexCoordP2ui;
1473 vfmt->TexCoordP3ui = _save_TexCoordP3ui;
1474 vfmt->TexCoordP4ui = _save_TexCoordP4ui;
1475 vfmt->TexCoordP1uiv = _save_TexCoordP1uiv;
1476 vfmt->TexCoordP2uiv = _save_TexCoordP2uiv;
1477 vfmt->TexCoordP3uiv = _save_TexCoordP3uiv;
1478 vfmt->TexCoordP4uiv = _save_TexCoordP4uiv;
1479
1480 vfmt->MultiTexCoordP1ui = _save_MultiTexCoordP1ui;
1481 vfmt->MultiTexCoordP2ui = _save_MultiTexCoordP2ui;
1482 vfmt->MultiTexCoordP3ui = _save_MultiTexCoordP3ui;
1483 vfmt->MultiTexCoordP4ui = _save_MultiTexCoordP4ui;
1484 vfmt->MultiTexCoordP1uiv = _save_MultiTexCoordP1uiv;
1485 vfmt->MultiTexCoordP2uiv = _save_MultiTexCoordP2uiv;
1486 vfmt->MultiTexCoordP3uiv = _save_MultiTexCoordP3uiv;
1487 vfmt->MultiTexCoordP4uiv = _save_MultiTexCoordP4uiv;
1488
1489 vfmt->NormalP3ui = _save_NormalP3ui;
1490 vfmt->NormalP3uiv = _save_NormalP3uiv;
1491
1492 vfmt->ColorP3ui = _save_ColorP3ui;
1493 vfmt->ColorP4ui = _save_ColorP4ui;
1494 vfmt->ColorP3uiv = _save_ColorP3uiv;
1495 vfmt->ColorP4uiv = _save_ColorP4uiv;
1496
1497 vfmt->SecondaryColorP3ui = _save_SecondaryColorP3ui;
1498 vfmt->SecondaryColorP3uiv = _save_SecondaryColorP3uiv;
1499
1500 vfmt->VertexAttribP1ui = _save_VertexAttribP1ui;
1501 vfmt->VertexAttribP2ui = _save_VertexAttribP2ui;
1502 vfmt->VertexAttribP3ui = _save_VertexAttribP3ui;
1503 vfmt->VertexAttribP4ui = _save_VertexAttribP4ui;
1504
1505 vfmt->VertexAttribP1uiv = _save_VertexAttribP1uiv;
1506 vfmt->VertexAttribP2uiv = _save_VertexAttribP2uiv;
1507 vfmt->VertexAttribP3uiv = _save_VertexAttribP3uiv;
1508 vfmt->VertexAttribP4uiv = _save_VertexAttribP4uiv;
1509
1510 vfmt->VertexAttribL1d = _save_VertexAttribL1d;
1511 vfmt->VertexAttribL2d = _save_VertexAttribL2d;
1512 vfmt->VertexAttribL3d = _save_VertexAttribL3d;
1513 vfmt->VertexAttribL4d = _save_VertexAttribL4d;
1514
1515 vfmt->VertexAttribL1dv = _save_VertexAttribL1dv;
1516 vfmt->VertexAttribL2dv = _save_VertexAttribL2dv;
1517 vfmt->VertexAttribL3dv = _save_VertexAttribL3dv;
1518 vfmt->VertexAttribL4dv = _save_VertexAttribL4dv;
1519
1520 vfmt->VertexAttribL1ui64ARB = _save_VertexAttribL1ui64ARB;
1521 vfmt->VertexAttribL1ui64vARB = _save_VertexAttribL1ui64vARB;
1522
1523 /* This will all require us to fallback to saving the list as opcodes:
1524 */
1525 vfmt->CallList = _save_CallList;
1526 vfmt->CallLists = _save_CallLists;
1527
1528 vfmt->EvalCoord1f = _save_EvalCoord1f;
1529 vfmt->EvalCoord1fv = _save_EvalCoord1fv;
1530 vfmt->EvalCoord2f = _save_EvalCoord2f;
1531 vfmt->EvalCoord2fv = _save_EvalCoord2fv;
1532 vfmt->EvalPoint1 = _save_EvalPoint1;
1533 vfmt->EvalPoint2 = _save_EvalPoint2;
1534
1535 /* These calls all generate GL_INVALID_OPERATION since this vtxfmt is
1536 * only used when we're inside a glBegin/End pair.
1537 */
1538 vfmt->Begin = _save_Begin;
1539 }
1540
1541
1542 /**
1543 * Initialize the dispatch table with the VBO functions for display
1544 * list compilation.
1545 */
1546 void
1547 vbo_initialize_save_dispatch(const struct gl_context *ctx,
1548 struct _glapi_table *exec)
1549 {
1550 SET_DrawArrays(exec, _save_OBE_DrawArrays);
1551 SET_MultiDrawArrays(exec, _save_OBE_MultiDrawArrays);
1552 SET_DrawElements(exec, _save_OBE_DrawElements);
1553 SET_DrawElementsBaseVertex(exec, _save_OBE_DrawElementsBaseVertex);
1554 SET_DrawRangeElements(exec, _save_OBE_DrawRangeElements);
1555 SET_MultiDrawElementsEXT(exec, _save_OBE_MultiDrawElements);
1556 SET_MultiDrawElementsBaseVertex(exec, _save_OBE_MultiDrawElementsBaseVertex);
1557 SET_Rectf(exec, _save_OBE_Rectf);
1558 /* Note: other glDraw functins aren't compiled into display lists */
1559 }
1560
1561
1562
1563 void
1564 vbo_save_SaveFlushVertices(struct gl_context *ctx)
1565 {
1566 struct vbo_save_context *save = &vbo_context(ctx)->save;
1567
1568 /* Noop when we are actually active:
1569 */
1570 if (ctx->Driver.CurrentSavePrimitive <= PRIM_MAX)
1571 return;
1572
1573 if (save->vert_count || save->prim_count)
1574 compile_vertex_list(ctx);
1575
1576 copy_to_current(ctx);
1577 reset_vertex(ctx);
1578 reset_counters(ctx);
1579 ctx->Driver.SaveNeedFlush = GL_FALSE;
1580 }
1581
1582
1583 /**
1584 * Called from glNewList when we're starting to compile a display list.
1585 */
1586 void
1587 vbo_save_NewList(struct gl_context *ctx, GLuint list, GLenum mode)
1588 {
1589 struct vbo_save_context *save = &vbo_context(ctx)->save;
1590
1591 (void) list;
1592 (void) mode;
1593
1594 if (!save->prim_store)
1595 save->prim_store = alloc_prim_store();
1596
1597 if (!save->vertex_store)
1598 save->vertex_store = alloc_vertex_store(ctx);
1599
1600 save->buffer_ptr = vbo_save_map_vertex_store(ctx, save->vertex_store);
1601
1602 reset_vertex(ctx);
1603 reset_counters(ctx);
1604 ctx->Driver.SaveNeedFlush = GL_FALSE;
1605 }
1606
1607
1608 /**
1609 * Called from glEndList when we're finished compiling a display list.
1610 */
1611 void
1612 vbo_save_EndList(struct gl_context *ctx)
1613 {
1614 struct vbo_save_context *save = &vbo_context(ctx)->save;
1615
1616 /* EndList called inside a (saved) Begin/End pair?
1617 */
1618 if (_mesa_inside_dlist_begin_end(ctx)) {
1619 if (save->prim_count > 0) {
1620 GLint i = save->prim_count - 1;
1621 ctx->Driver.CurrentSavePrimitive = PRIM_OUTSIDE_BEGIN_END;
1622 save->prims[i].end = 0;
1623 save->prims[i].count = save->vert_count - save->prims[i].start;
1624 }
1625
1626 /* Make sure this vertex list gets replayed by the "loopback"
1627 * mechanism:
1628 */
1629 save->dangling_attr_ref = GL_TRUE;
1630 vbo_save_SaveFlushVertices(ctx);
1631
1632 /* Swap out this vertex format while outside begin/end. Any color,
1633 * etc. received between here and the next begin will be compiled
1634 * as opcodes.
1635 */
1636 _mesa_install_save_vtxfmt(ctx, &ctx->ListState.ListVtxfmt);
1637 }
1638
1639 vbo_save_unmap_vertex_store(ctx, save->vertex_store);
1640
1641 assert(save->vertex_size == 0);
1642 }
1643
1644
1645 /**
1646 * Called from the display list code when we're about to execute a
1647 * display list.
1648 */
1649 void
1650 vbo_save_BeginCallList(struct gl_context *ctx, struct gl_display_list *dlist)
1651 {
1652 struct vbo_save_context *save = &vbo_context(ctx)->save;
1653 save->replay_flags |= dlist->Flags;
1654 }
1655
1656
1657 /**
1658 * Called from the display list code when we're finished executing a
1659 * display list.
1660 */
1661 void
1662 vbo_save_EndCallList(struct gl_context *ctx)
1663 {
1664 struct vbo_save_context *save = &vbo_context(ctx)->save;
1665
1666 if (ctx->ListState.CallDepth == 1) {
1667 /* This is correct: want to keep only the VBO_SAVE_FALLBACK
1668 * flag, if it is set:
1669 */
1670 save->replay_flags &= VBO_SAVE_FALLBACK;
1671 }
1672 }
1673
1674
1675 /**
1676 * Called by display list code when a display list is being deleted.
1677 */
1678 static void
1679 vbo_destroy_vertex_list(struct gl_context *ctx, void *data)
1680 {
1681 struct vbo_save_vertex_list *node = (struct vbo_save_vertex_list *) data;
1682 (void) ctx;
1683
1684 if (--node->vertex_store->refcount == 0)
1685 free_vertex_store(ctx, node->vertex_store);
1686
1687 if (--node->prim_store->refcount == 0)
1688 free(node->prim_store);
1689
1690 free(node->current_data);
1691 node->current_data = NULL;
1692 }
1693
1694
1695 static void
1696 vbo_print_vertex_list(struct gl_context *ctx, void *data, FILE *f)
1697 {
1698 struct vbo_save_vertex_list *node = (struct vbo_save_vertex_list *) data;
1699 GLuint i;
1700 struct gl_buffer_object *buffer = node->vertex_store ?
1701 node->vertex_store->bufferobj : NULL;
1702 (void) ctx;
1703
1704 fprintf(f, "VBO-VERTEX-LIST, %u vertices, %d primitives, %d vertsize, "
1705 "buffer %p\n",
1706 node->vertex_count, node->prim_count, node->vertex_size,
1707 buffer);
1708
1709 for (i = 0; i < node->prim_count; i++) {
1710 struct _mesa_prim *prim = &node->prims[i];
1711 fprintf(f, " prim %d: %s%s %d..%d %s %s\n",
1712 i,
1713 _mesa_lookup_prim_by_nr(prim->mode),
1714 prim->weak ? " (weak)" : "",
1715 prim->start,
1716 prim->start + prim->count,
1717 (prim->begin) ? "BEGIN" : "(wrap)",
1718 (prim->end) ? "END" : "(wrap)");
1719 }
1720 }
1721
1722
1723 /**
1724 * Called during context creation/init.
1725 */
1726 static void
1727 current_init(struct gl_context *ctx)
1728 {
1729 struct vbo_save_context *save = &vbo_context(ctx)->save;
1730 GLint i;
1731
1732 for (i = VBO_ATTRIB_POS; i <= VBO_ATTRIB_GENERIC15; i++) {
1733 const GLuint j = i - VBO_ATTRIB_POS;
1734 assert(j < VERT_ATTRIB_MAX);
1735 save->currentsz[i] = &ctx->ListState.ActiveAttribSize[j];
1736 save->current[i] = (fi_type *) ctx->ListState.CurrentAttrib[j];
1737 }
1738
1739 for (i = VBO_ATTRIB_FIRST_MATERIAL; i <= VBO_ATTRIB_LAST_MATERIAL; i++) {
1740 const GLuint j = i - VBO_ATTRIB_FIRST_MATERIAL;
1741 assert(j < MAT_ATTRIB_MAX);
1742 save->currentsz[i] = &ctx->ListState.ActiveMaterialSize[j];
1743 save->current[i] = (fi_type *) ctx->ListState.CurrentMaterial[j];
1744 }
1745 }
1746
1747
1748 /**
1749 * Initialize the display list compiler. Called during context creation.
1750 */
1751 void
1752 vbo_save_api_init(struct vbo_save_context *save)
1753 {
1754 struct gl_context *ctx = save->ctx;
1755 GLuint i;
1756
1757 save->opcode_vertex_list =
1758 _mesa_dlist_alloc_opcode(ctx,
1759 sizeof(struct vbo_save_vertex_list),
1760 vbo_save_playback_vertex_list,
1761 vbo_destroy_vertex_list,
1762 vbo_print_vertex_list);
1763
1764 vtxfmt_init(ctx);
1765 current_init(ctx);
1766 _mesa_noop_vtxfmt_init(&save->vtxfmt_noop);
1767
1768 /* These will actually get set again when binding/drawing */
1769 for (i = 0; i < VBO_ATTRIB_MAX; i++)
1770 save->inputs[i] = &save->arrays[i];
1771 }