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