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