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