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