1 /**************************************************************************
3 Copyright 2002-2008 VMware, Inc.
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:
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
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.
26 **************************************************************************/
30 * Keith Whitwell <keithw@vmware.com>
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
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,
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.
50 * The other case where fixup is required is when a vertex attribute
51 * is introduced in the middle of a primitive. Eg:
53 * TexCoord1f() Vertex2f()
54 * TexCoord1f() Color3f() Vertex2f()
57 * If the current value of Color isn't known at compile-time, this
58 * primitive will require fixup.
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.
65 * This could be improved to fallback only when a mix of EvalCoord and
66 * Vertex commands are issued within a single primitive.
70 #include "main/glheader.h"
71 #include "main/bufferobj.h"
72 #include "main/context.h"
73 #include "main/dlist.h"
74 #include "main/enums.h"
75 #include "main/eval.h"
76 #include "main/macros.h"
77 #include "main/api_validate.h"
78 #include "main/api_arrayelt.h"
79 #include "main/vtxfmt.h"
80 #include "main/dispatch.h"
81 #include "main/state.h"
82 #include "util/bitscan.h"
84 #include "vbo_context.h"
93 * Display list flag only used by this VBO code.
95 #define DLIST_DANGLING_REFS 0x1
98 /* An interesting VBO number/name to help with debugging */
99 #define VBO_BUF_ID 12345
103 * NOTE: Old 'parity' issue is gone, but copying can still be
104 * wrong-footed on replay.
107 copy_vertices(struct gl_context
*ctx
,
108 const struct vbo_save_vertex_list
*node
,
109 const fi_type
* src_buffer
)
111 struct vbo_save_context
*save
= &vbo_context(ctx
)->save
;
112 const struct _mesa_prim
*prim
= &node
->prims
[node
->prim_count
- 1];
113 GLuint nr
= prim
->count
;
114 GLuint sz
= save
->vertex_size
;
115 const fi_type
*src
= src_buffer
+ prim
->start
* sz
;
116 fi_type
*dst
= save
->copied
.buffer
;
122 switch (prim
->mode
) {
127 for (i
= 0; i
< ovf
; i
++)
128 memcpy(dst
+ i
* sz
, src
+ (nr
- ovf
+ i
) * sz
,
129 sz
* sizeof(GLfloat
));
133 for (i
= 0; i
< ovf
; i
++)
134 memcpy(dst
+ i
* sz
, src
+ (nr
- ovf
+ i
) * sz
,
135 sz
* sizeof(GLfloat
));
139 for (i
= 0; i
< ovf
; i
++)
140 memcpy(dst
+ i
* sz
, src
+ (nr
- ovf
+ i
) * sz
,
141 sz
* sizeof(GLfloat
));
147 memcpy(dst
, src
+ (nr
- 1) * sz
, sz
* sizeof(GLfloat
));
151 case GL_TRIANGLE_FAN
:
156 memcpy(dst
, src
+ 0, sz
* sizeof(GLfloat
));
160 memcpy(dst
, src
+ 0, sz
* sizeof(GLfloat
));
161 memcpy(dst
+ sz
, src
+ (nr
- 1) * sz
, sz
* sizeof(GLfloat
));
164 case GL_TRIANGLE_STRIP
:
177 for (i
= 0; i
< ovf
; i
++)
178 memcpy(dst
+ i
* sz
, src
+ (nr
- ovf
+ i
) * sz
,
179 sz
* sizeof(GLfloat
));
182 unreachable("Unexpected primitive type");
188 static struct vbo_save_vertex_store
*
189 alloc_vertex_store(struct gl_context
*ctx
)
191 struct vbo_save_context
*save
= &vbo_context(ctx
)->save
;
192 struct vbo_save_vertex_store
*vertex_store
=
193 CALLOC_STRUCT(vbo_save_vertex_store
);
195 /* obj->Name needs to be non-zero, but won't ever be examined more
196 * closely than that. In particular these buffers won't be entered
197 * into the hash and can never be confused with ones visible to the
198 * user. Perhaps there could be a special number for internal
201 vertex_store
->bufferobj
= ctx
->Driver
.NewBufferObject(ctx
, VBO_BUF_ID
);
202 if (vertex_store
->bufferobj
) {
203 save
->out_of_memory
=
204 !ctx
->Driver
.BufferData(ctx
,
206 VBO_SAVE_BUFFER_SIZE
* sizeof(GLfloat
),
207 NULL
, GL_STATIC_DRAW_ARB
,
209 GL_DYNAMIC_STORAGE_BIT
,
210 vertex_store
->bufferobj
);
213 save
->out_of_memory
= GL_TRUE
;
216 if (save
->out_of_memory
) {
217 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "internal VBO allocation");
218 _mesa_install_save_vtxfmt(ctx
, &save
->vtxfmt_noop
);
221 vertex_store
->buffer_map
= NULL
;
222 vertex_store
->used
= 0;
223 vertex_store
->refcount
= 1;
230 free_vertex_store(struct gl_context
*ctx
,
231 struct vbo_save_vertex_store
*vertex_store
)
233 assert(!vertex_store
->buffer_map
);
235 if (vertex_store
->bufferobj
) {
236 _mesa_reference_buffer_object(ctx
, &vertex_store
->bufferobj
, NULL
);
244 vbo_save_map_vertex_store(struct gl_context
*ctx
,
245 struct vbo_save_vertex_store
*vertex_store
)
247 const GLbitfield access
= (GL_MAP_WRITE_BIT
|
248 GL_MAP_INVALIDATE_RANGE_BIT
|
249 GL_MAP_UNSYNCHRONIZED_BIT
|
250 GL_MAP_FLUSH_EXPLICIT_BIT
);
252 assert(vertex_store
->bufferobj
);
253 assert(!vertex_store
->buffer_map
); /* the buffer should not be mapped */
255 if (vertex_store
->bufferobj
->Size
> 0) {
256 /* Map the remaining free space in the VBO */
257 GLintptr offset
= vertex_store
->used
* sizeof(GLfloat
);
258 GLsizeiptr size
= vertex_store
->bufferobj
->Size
- offset
;
259 fi_type
*range
= (fi_type
*)
260 ctx
->Driver
.MapBufferRange(ctx
, offset
, size
, access
,
261 vertex_store
->bufferobj
,
264 /* compute address of start of whole buffer (needed elsewhere) */
265 vertex_store
->buffer_map
= range
- vertex_store
->used
;
266 assert(vertex_store
->buffer_map
);
270 vertex_store
->buffer_map
= NULL
;
275 /* probably ran out of memory for buffers */
282 vbo_save_unmap_vertex_store(struct gl_context
*ctx
,
283 struct vbo_save_vertex_store
*vertex_store
)
285 if (vertex_store
->bufferobj
->Size
> 0) {
287 GLsizeiptr length
= vertex_store
->used
* sizeof(GLfloat
)
288 - vertex_store
->bufferobj
->Mappings
[MAP_INTERNAL
].Offset
;
290 /* Explicitly flush the region we wrote to */
291 ctx
->Driver
.FlushMappedBufferRange(ctx
, offset
, length
,
292 vertex_store
->bufferobj
,
295 ctx
->Driver
.UnmapBuffer(ctx
, vertex_store
->bufferobj
, MAP_INTERNAL
);
297 vertex_store
->buffer_map
= NULL
;
301 static struct vbo_save_primitive_store
*
302 alloc_prim_store(void)
304 struct vbo_save_primitive_store
*store
=
305 CALLOC_STRUCT(vbo_save_primitive_store
);
313 reset_counters(struct gl_context
*ctx
)
315 struct vbo_save_context
*save
= &vbo_context(ctx
)->save
;
317 save
->prims
= save
->prim_store
->prims
+ save
->prim_store
->used
;
318 save
->buffer_map
= save
->vertex_store
->buffer_map
+ save
->vertex_store
->used
;
320 assert(save
->buffer_map
== save
->buffer_ptr
);
322 if (save
->vertex_size
)
323 save
->max_vert
= (VBO_SAVE_BUFFER_SIZE
- save
->vertex_store
->used
) /
328 save
->vert_count
= 0;
329 save
->prim_count
= 0;
330 save
->prim_max
= VBO_SAVE_PRIM_SIZE
- save
->prim_store
->used
;
331 save
->dangling_attr_ref
= GL_FALSE
;
335 * For a list of prims, try merging prims that can just be extensions of the
339 merge_prims(struct _mesa_prim
*prim_list
,
343 struct _mesa_prim
*prev_prim
= prim_list
;
345 for (i
= 1; i
< *prim_count
; i
++) {
346 struct _mesa_prim
*this_prim
= prim_list
+ i
;
348 vbo_try_prim_conversion(this_prim
);
350 if (vbo_can_merge_prims(prev_prim
, this_prim
)) {
351 /* We've found a prim that just extend the previous one. Tack it
352 * onto the previous one, and let this primitive struct get dropped.
354 vbo_merge_prims(prev_prim
, this_prim
);
358 /* If any previous primitives have been dropped, then we need to copy
359 * this later one into the next available slot.
362 if (prev_prim
!= this_prim
)
363 *prev_prim
= *this_prim
;
366 *prim_count
= prev_prim
- prim_list
+ 1;
371 * Convert GL_LINE_LOOP primitive into GL_LINE_STRIP so that drivers
372 * don't have to worry about handling the _mesa_prim::begin/end flags.
373 * See https://bugs.freedesktop.org/show_bug.cgi?id=81174
376 convert_line_loop_to_strip(struct vbo_save_context
*save
,
377 struct vbo_save_vertex_list
*node
)
379 struct _mesa_prim
*prim
= &node
->prims
[node
->prim_count
- 1];
381 assert(prim
->mode
== GL_LINE_LOOP
);
384 /* Copy the 0th vertex to end of the buffer and extend the
385 * vertex count by one to finish the line loop.
387 const GLuint sz
= save
->vertex_size
;
389 const fi_type
*src
= save
->buffer_map
+ prim
->start
* sz
;
391 fi_type
*dst
= save
->buffer_map
+ (prim
->start
+ prim
->count
) * sz
;
393 memcpy(dst
, src
, sz
* sizeof(float));
396 node
->vertex_count
++;
398 save
->buffer_ptr
+= sz
;
399 save
->vertex_store
->used
+= sz
;
403 /* Drawing the second or later section of a long line loop.
404 * Skip the 0th vertex.
410 prim
->mode
= GL_LINE_STRIP
;
415 * Insert the active immediate struct onto the display list currently
419 compile_vertex_list(struct gl_context
*ctx
)
421 struct vbo_save_context
*save
= &vbo_context(ctx
)->save
;
422 struct vbo_save_vertex_list
*node
;
424 /* Allocate space for this structure in the display list currently
427 node
= (struct vbo_save_vertex_list
*)
428 _mesa_dlist_alloc_aligned(ctx
, save
->opcode_vertex_list
, sizeof(*node
));
433 /* Make sure the pointer is aligned to the size of a pointer */
434 assert((GLintptr
) node
% sizeof(void *) == 0);
436 /* Duplicate our template, increment refcounts to the storage structs:
438 node
->enabled
= save
->enabled
;
439 memcpy(node
->attrsz
, save
->attrsz
, sizeof(node
->attrsz
));
440 memcpy(node
->attrtype
, save
->attrtype
, sizeof(node
->attrtype
));
441 node
->vertex_size
= save
->vertex_size
;
442 node
->buffer_offset
=
443 (save
->buffer_map
- save
->vertex_store
->buffer_map
) * sizeof(GLfloat
);
444 node
->vertex_count
= save
->vert_count
;
445 node
->wrap_count
= save
->copied
.nr
;
446 node
->dangling_attr_ref
= save
->dangling_attr_ref
;
447 node
->prims
= save
->prims
;
448 node
->prim_count
= save
->prim_count
;
449 node
->vertex_store
= save
->vertex_store
;
450 node
->prim_store
= save
->prim_store
;
452 node
->vertex_store
->refcount
++;
453 node
->prim_store
->refcount
++;
455 if (node
->prims
[0].no_current_update
) {
456 node
->current_size
= 0;
457 node
->current_data
= NULL
;
460 node
->current_size
= node
->vertex_size
- node
->attrsz
[0];
461 node
->current_data
= NULL
;
463 if (node
->current_size
) {
464 /* If the malloc fails, we just pull the data out of the VBO
467 node
->current_data
= malloc(node
->current_size
* sizeof(GLfloat
));
468 if (node
->current_data
) {
469 const char *buffer
= (const char *) save
->vertex_store
->buffer_map
;
470 unsigned attr_offset
= node
->attrsz
[0] * sizeof(GLfloat
);
471 unsigned vertex_offset
= 0;
473 if (node
->vertex_count
)
475 (node
->vertex_count
- 1) * node
->vertex_size
* sizeof(GLfloat
);
477 memcpy(node
->current_data
,
478 buffer
+ node
->buffer_offset
+ vertex_offset
+ attr_offset
,
479 node
->current_size
* sizeof(GLfloat
));
484 assert(node
->attrsz
[VBO_ATTRIB_POS
] != 0 || node
->vertex_count
== 0);
486 if (save
->dangling_attr_ref
)
487 ctx
->ListState
.CurrentList
->Flags
|= DLIST_DANGLING_REFS
;
489 save
->vertex_store
->used
+= save
->vertex_size
* node
->vertex_count
;
490 save
->prim_store
->used
+= node
->prim_count
;
492 /* Copy duplicated vertices
494 save
->copied
.nr
= copy_vertices(ctx
, node
, save
->buffer_map
);
496 if (node
->prims
[node
->prim_count
- 1].mode
== GL_LINE_LOOP
) {
497 convert_line_loop_to_strip(save
, node
);
500 merge_prims(node
->prims
, &node
->prim_count
);
502 /* Deal with GL_COMPILE_AND_EXECUTE:
504 if (ctx
->ExecuteFlag
) {
505 struct _glapi_table
*dispatch
= GET_DISPATCH();
507 _glapi_set_dispatch(ctx
->Exec
);
509 const GLfloat
*buffer
= (const GLfloat
*)
510 ((const char *) save
->vertex_store
->buffer_map
+
511 node
->buffer_offset
);
513 vbo_loopback_vertex_list(ctx
, buffer
,
514 node
->attrsz
, node
->prims
, node
->prim_count
,
515 node
->wrap_count
, node
->vertex_size
);
517 _glapi_set_dispatch(dispatch
);
520 /* Decide whether the storage structs are full, or can be used for
521 * the next vertex lists as well.
523 if (save
->vertex_store
->used
>
524 VBO_SAVE_BUFFER_SIZE
- 16 * (save
->vertex_size
+ 4)) {
528 vbo_save_unmap_vertex_store(ctx
, save
->vertex_store
);
530 /* Release old reference:
532 save
->vertex_store
->refcount
--;
533 assert(save
->vertex_store
->refcount
!= 0);
534 save
->vertex_store
= NULL
;
536 /* Allocate and map new store:
538 save
->vertex_store
= alloc_vertex_store(ctx
);
539 save
->buffer_ptr
= vbo_save_map_vertex_store(ctx
, save
->vertex_store
);
540 save
->out_of_memory
= save
->buffer_ptr
== NULL
;
543 /* update buffer_ptr for next vertex */
544 save
->buffer_ptr
= save
->vertex_store
->buffer_map
545 + save
->vertex_store
->used
;
548 if (save
->prim_store
->used
> VBO_SAVE_PRIM_SIZE
- 6) {
549 save
->prim_store
->refcount
--;
550 assert(save
->prim_store
->refcount
!= 0);
551 save
->prim_store
= alloc_prim_store();
555 * If the vertex buffer offset is a multiple of the vertex size,
556 * we can use the _mesa_prim::start value to indicate where the
557 * vertices starts, instead of the buffer offset. Also see the
558 * bind_vertex_list() function.
560 if (aligned_vertex_buffer_offset(node
)) {
561 const unsigned start_offset
=
562 node
->buffer_offset
/ (node
->vertex_size
* sizeof(GLfloat
));
563 for (unsigned i
= 0; i
< save
->prim_count
; i
++) {
564 save
->prims
[i
].start
+= start_offset
;
568 /* Reset our structures for the next run of vertices:
575 * This is called when we fill a vertex buffer before we hit a glEnd().
577 * TODO -- If no new vertices have been stored, don't bother saving it.
580 wrap_buffers(struct gl_context
*ctx
)
582 struct vbo_save_context
*save
= &vbo_context(ctx
)->save
;
583 GLint i
= save
->prim_count
- 1;
586 GLboolean no_current_update
;
588 assert(i
< (GLint
) save
->prim_max
);
591 /* Close off in-progress primitive.
593 save
->prims
[i
].count
= (save
->vert_count
- save
->prims
[i
].start
);
594 mode
= save
->prims
[i
].mode
;
595 weak
= save
->prims
[i
].weak
;
596 no_current_update
= save
->prims
[i
].no_current_update
;
598 /* store the copied vertices, and allocate a new list.
600 compile_vertex_list(ctx
);
602 /* Restart interrupted primitive
604 save
->prims
[0].mode
= mode
;
605 save
->prims
[0].weak
= weak
;
606 save
->prims
[0].no_current_update
= no_current_update
;
607 save
->prims
[0].begin
= 0;
608 save
->prims
[0].end
= 0;
609 save
->prims
[0].pad
= 0;
610 save
->prims
[0].start
= 0;
611 save
->prims
[0].count
= 0;
612 save
->prims
[0].num_instances
= 1;
613 save
->prims
[0].base_instance
= 0;
614 save
->prims
[0].is_indirect
= 0;
615 save
->prim_count
= 1;
620 * Called only when buffers are wrapped as the result of filling the
621 * vertex_store struct.
624 wrap_filled_vertex(struct gl_context
*ctx
)
626 struct vbo_save_context
*save
= &vbo_context(ctx
)->save
;
627 unsigned numComponents
;
629 /* Emit a glEnd to close off the last vertex list.
633 /* Copy stored stored vertices to start of new list.
635 assert(save
->max_vert
- save
->vert_count
> save
->copied
.nr
);
637 numComponents
= save
->copied
.nr
* save
->vertex_size
;
638 memcpy(save
->buffer_ptr
,
640 numComponents
* sizeof(fi_type
));
641 save
->buffer_ptr
+= numComponents
;
642 save
->vert_count
+= save
->copied
.nr
;
647 copy_to_current(struct gl_context
*ctx
)
649 struct vbo_save_context
*save
= &vbo_context(ctx
)->save
;
650 GLbitfield64 enabled
= save
->enabled
& (~BITFIELD64_BIT(VBO_ATTRIB_POS
));
653 const int i
= u_bit_scan64(&enabled
);
654 assert(save
->attrsz
[i
]);
656 save
->currentsz
[i
][0] = save
->attrsz
[i
];
657 COPY_CLEAN_4V_TYPE_AS_UNION(save
->current
[i
], save
->attrsz
[i
],
658 save
->attrptr
[i
], save
->attrtype
[i
]);
664 copy_from_current(struct gl_context
*ctx
)
666 struct vbo_save_context
*save
= &vbo_context(ctx
)->save
;
667 GLbitfield64 enabled
= save
->enabled
& (~BITFIELD64_BIT(VBO_ATTRIB_POS
));
670 const int i
= u_bit_scan64(&enabled
);
672 switch (save
->attrsz
[i
]) {
674 save
->attrptr
[i
][3] = save
->current
[i
][3];
676 save
->attrptr
[i
][2] = save
->current
[i
][2];
678 save
->attrptr
[i
][1] = save
->current
[i
][1];
680 save
->attrptr
[i
][0] = save
->current
[i
][0];
683 unreachable("Unexpected vertex attribute size");
690 * Called when we increase the size of a vertex attribute. For example,
691 * if we've seen one or more glTexCoord2f() calls and now we get a
692 * glTexCoord3f() call.
693 * Flush existing data, set new attrib size, replay copied vertices.
696 upgrade_vertex(struct gl_context
*ctx
, GLuint attr
, GLuint newsz
)
698 struct vbo_save_context
*save
= &vbo_context(ctx
)->save
;
703 /* Store the current run of vertices, and emit a GL_END. Emit a
704 * BEGIN in the new buffer.
706 if (save
->vert_count
)
709 assert(save
->copied
.nr
== 0);
711 /* Do a COPY_TO_CURRENT to ensure back-copying works for the case
712 * when the attribute already exists in the vertex and is having
713 * its size increased.
715 copy_to_current(ctx
);
719 oldsz
= save
->attrsz
[attr
];
720 save
->attrsz
[attr
] = newsz
;
721 save
->enabled
|= BITFIELD64_BIT(attr
);
723 save
->vertex_size
+= newsz
- oldsz
;
724 save
->max_vert
= ((VBO_SAVE_BUFFER_SIZE
- save
->vertex_store
->used
) /
726 save
->vert_count
= 0;
728 /* Recalculate all the attrptr[] values:
731 for (i
= 0; i
< VBO_ATTRIB_MAX
; i
++) {
732 if (save
->attrsz
[i
]) {
733 save
->attrptr
[i
] = tmp
;
734 tmp
+= save
->attrsz
[i
];
737 save
->attrptr
[i
] = NULL
; /* will not be dereferenced. */
741 /* Copy from current to repopulate the vertex with correct values.
743 copy_from_current(ctx
);
745 /* Replay stored vertices to translate them to new format here.
747 * If there are copied vertices and the new (upgraded) attribute
748 * has not been defined before, this list is somewhat degenerate,
749 * and will need fixup at runtime.
751 if (save
->copied
.nr
) {
752 const fi_type
*data
= save
->copied
.buffer
;
753 fi_type
*dest
= save
->buffer_map
;
755 /* Need to note this and fix up at runtime (or loopback):
757 if (attr
!= VBO_ATTRIB_POS
&& save
->currentsz
[attr
][0] == 0) {
759 save
->dangling_attr_ref
= GL_TRUE
;
762 for (i
= 0; i
< save
->copied
.nr
; i
++) {
763 GLbitfield64 enabled
= save
->enabled
;
765 const int j
= u_bit_scan64(&enabled
);
766 assert(save
->attrsz
[j
]);
769 COPY_CLEAN_4V_TYPE_AS_UNION(dest
, oldsz
, data
,
775 COPY_SZ_4V(dest
, newsz
, save
->current
[attr
]);
780 GLint sz
= save
->attrsz
[j
];
781 COPY_SZ_4V(dest
, sz
, data
);
788 save
->buffer_ptr
= dest
;
789 save
->vert_count
+= save
->copied
.nr
;
795 * This is called when the size of a vertex attribute changes.
796 * For example, after seeing one or more glTexCoord2f() calls we
797 * get a glTexCoord4f() or glTexCoord1f() call.
800 fixup_vertex(struct gl_context
*ctx
, GLuint attr
, GLuint sz
)
802 struct vbo_save_context
*save
= &vbo_context(ctx
)->save
;
804 if (sz
> save
->attrsz
[attr
]) {
805 /* New size is larger. Need to flush existing vertices and get
806 * an enlarged vertex format.
808 upgrade_vertex(ctx
, attr
, sz
);
810 else if (sz
< save
->active_sz
[attr
]) {
812 const fi_type
*id
= vbo_get_default_vals_as_union(save
->attrtype
[attr
]);
814 /* New size is equal or smaller - just need to fill in some
817 for (i
= sz
; i
<= save
->attrsz
[attr
]; i
++)
818 save
->attrptr
[attr
][i
- 1] = id
[i
- 1];
821 save
->active_sz
[attr
] = sz
;
826 * Reset the current size of all vertex attributes to the default
827 * value of 0. This signals that we haven't yet seen any per-vertex
828 * commands such as glNormal3f() or glTexCoord2f().
831 reset_vertex(struct gl_context
*ctx
)
833 struct vbo_save_context
*save
= &vbo_context(ctx
)->save
;
835 while (save
->enabled
) {
836 const int i
= u_bit_scan64(&save
->enabled
);
837 assert(save
->attrsz
[i
]);
839 save
->active_sz
[i
] = 0;
842 save
->vertex_size
= 0;
847 #define ERROR(err) _mesa_compile_error(ctx, err, __func__);
850 /* Only one size for each attribute may be active at once. Eg. if
851 * Color3f is installed/active, then Color4f may not be, even if the
852 * vertex actually contains 4 color coordinates. This is because the
853 * 3f version won't otherwise set color[3] to 1.0 -- this is the job
854 * of the chooser function when switching between Color4f and Color3f.
856 #define ATTR_UNION(A, N, T, C, V0, V1, V2, V3) \
858 struct vbo_save_context *save = &vbo_context(ctx)->save; \
860 if (save->active_sz[A] != N) \
861 fixup_vertex(ctx, A, N); \
864 C *dest = (C *)save->attrptr[A]; \
865 if (N>0) dest[0] = V0; \
866 if (N>1) dest[1] = V1; \
867 if (N>2) dest[2] = V2; \
868 if (N>3) dest[3] = V3; \
869 save->attrtype[A] = T; \
875 for (i = 0; i < save->vertex_size; i++) \
876 save->buffer_ptr[i] = save->vertex[i]; \
878 save->buffer_ptr += save->vertex_size; \
880 if (++save->vert_count >= save->max_vert) \
881 wrap_filled_vertex(ctx); \
885 #define TAG(x) _save_##x
887 #include "vbo_attrib_tmp.h"
891 #define MAT( ATTR, N, face, params ) \
893 if (face != GL_BACK) \
894 MAT_ATTR( ATTR, N, params ); /* front */ \
895 if (face != GL_FRONT) \
896 MAT_ATTR( ATTR + 1, N, params ); /* back */ \
901 * Save a glMaterial call found between glBegin/End.
902 * glMaterial calls outside Begin/End are handled in dlist.c.
904 static void GLAPIENTRY
905 _save_Materialfv(GLenum face
, GLenum pname
, const GLfloat
*params
)
907 GET_CURRENT_CONTEXT(ctx
);
909 if (face
!= GL_FRONT
&& face
!= GL_BACK
&& face
!= GL_FRONT_AND_BACK
) {
910 _mesa_compile_error(ctx
, GL_INVALID_ENUM
, "glMaterial(face)");
916 MAT(VBO_ATTRIB_MAT_FRONT_EMISSION
, 4, face
, params
);
919 MAT(VBO_ATTRIB_MAT_FRONT_AMBIENT
, 4, face
, params
);
922 MAT(VBO_ATTRIB_MAT_FRONT_DIFFUSE
, 4, face
, params
);
925 MAT(VBO_ATTRIB_MAT_FRONT_SPECULAR
, 4, face
, params
);
928 if (*params
< 0 || *params
> ctx
->Const
.MaxShininess
) {
929 _mesa_compile_error(ctx
, GL_INVALID_VALUE
, "glMaterial(shininess)");
932 MAT(VBO_ATTRIB_MAT_FRONT_SHININESS
, 1, face
, params
);
935 case GL_COLOR_INDEXES
:
936 MAT(VBO_ATTRIB_MAT_FRONT_INDEXES
, 3, face
, params
);
938 case GL_AMBIENT_AND_DIFFUSE
:
939 MAT(VBO_ATTRIB_MAT_FRONT_AMBIENT
, 4, face
, params
);
940 MAT(VBO_ATTRIB_MAT_FRONT_DIFFUSE
, 4, face
, params
);
943 _mesa_compile_error(ctx
, GL_INVALID_ENUM
, "glMaterial(pname)");
949 /* Cope with EvalCoord/CallList called within a begin/end object:
950 * -- Flush current buffer
951 * -- Fallback to opcodes for the rest of the begin/end object.
954 dlist_fallback(struct gl_context
*ctx
)
956 struct vbo_save_context
*save
= &vbo_context(ctx
)->save
;
958 if (save
->vert_count
|| save
->prim_count
) {
959 if (save
->prim_count
> 0) {
960 /* Close off in-progress primitive. */
961 GLint i
= save
->prim_count
- 1;
962 save
->prims
[i
].count
= save
->vert_count
- save
->prims
[i
].start
;
965 /* Need to replay this display list with loopback,
966 * unfortunately, otherwise this primitive won't be handled
969 save
->dangling_attr_ref
= GL_TRUE
;
971 compile_vertex_list(ctx
);
974 copy_to_current(ctx
);
977 if (save
->out_of_memory
) {
978 _mesa_install_save_vtxfmt(ctx
, &save
->vtxfmt_noop
);
981 _mesa_install_save_vtxfmt(ctx
, &ctx
->ListState
.ListVtxfmt
);
983 ctx
->Driver
.SaveNeedFlush
= GL_FALSE
;
987 static void GLAPIENTRY
988 _save_EvalCoord1f(GLfloat u
)
990 GET_CURRENT_CONTEXT(ctx
);
992 CALL_EvalCoord1f(ctx
->Save
, (u
));
995 static void GLAPIENTRY
996 _save_EvalCoord1fv(const GLfloat
* v
)
998 GET_CURRENT_CONTEXT(ctx
);
1000 CALL_EvalCoord1fv(ctx
->Save
, (v
));
1003 static void GLAPIENTRY
1004 _save_EvalCoord2f(GLfloat u
, GLfloat v
)
1006 GET_CURRENT_CONTEXT(ctx
);
1007 dlist_fallback(ctx
);
1008 CALL_EvalCoord2f(ctx
->Save
, (u
, v
));
1011 static void GLAPIENTRY
1012 _save_EvalCoord2fv(const GLfloat
* v
)
1014 GET_CURRENT_CONTEXT(ctx
);
1015 dlist_fallback(ctx
);
1016 CALL_EvalCoord2fv(ctx
->Save
, (v
));
1019 static void GLAPIENTRY
1020 _save_EvalPoint1(GLint i
)
1022 GET_CURRENT_CONTEXT(ctx
);
1023 dlist_fallback(ctx
);
1024 CALL_EvalPoint1(ctx
->Save
, (i
));
1027 static void GLAPIENTRY
1028 _save_EvalPoint2(GLint i
, GLint j
)
1030 GET_CURRENT_CONTEXT(ctx
);
1031 dlist_fallback(ctx
);
1032 CALL_EvalPoint2(ctx
->Save
, (i
, j
));
1035 static void GLAPIENTRY
1036 _save_CallList(GLuint l
)
1038 GET_CURRENT_CONTEXT(ctx
);
1039 dlist_fallback(ctx
);
1040 CALL_CallList(ctx
->Save
, (l
));
1043 static void GLAPIENTRY
1044 _save_CallLists(GLsizei n
, GLenum type
, const GLvoid
* v
)
1046 GET_CURRENT_CONTEXT(ctx
);
1047 dlist_fallback(ctx
);
1048 CALL_CallLists(ctx
->Save
, (n
, type
, v
));
1054 * Called when a glBegin is getting compiled into a display list.
1055 * Updating of ctx->Driver.CurrentSavePrimitive is already taken care of.
1058 vbo_save_NotifyBegin(struct gl_context
*ctx
, GLenum mode
)
1060 struct vbo_save_context
*save
= &vbo_context(ctx
)->save
;
1061 const GLuint i
= save
->prim_count
++;
1063 assert(i
< save
->prim_max
);
1064 save
->prims
[i
].mode
= mode
& VBO_SAVE_PRIM_MODE_MASK
;
1065 save
->prims
[i
].begin
= 1;
1066 save
->prims
[i
].end
= 0;
1067 save
->prims
[i
].weak
= (mode
& VBO_SAVE_PRIM_WEAK
) ? 1 : 0;
1068 save
->prims
[i
].no_current_update
=
1069 (mode
& VBO_SAVE_PRIM_NO_CURRENT_UPDATE
) ? 1 : 0;
1070 save
->prims
[i
].pad
= 0;
1071 save
->prims
[i
].start
= save
->vert_count
;
1072 save
->prims
[i
].count
= 0;
1073 save
->prims
[i
].num_instances
= 1;
1074 save
->prims
[i
].base_instance
= 0;
1075 save
->prims
[i
].is_indirect
= 0;
1077 if (save
->out_of_memory
) {
1078 _mesa_install_save_vtxfmt(ctx
, &save
->vtxfmt_noop
);
1081 _mesa_install_save_vtxfmt(ctx
, &save
->vtxfmt
);
1084 /* We need to call vbo_save_SaveFlushVertices() if there's state change */
1085 ctx
->Driver
.SaveNeedFlush
= GL_TRUE
;
1089 static void GLAPIENTRY
1092 GET_CURRENT_CONTEXT(ctx
);
1093 struct vbo_save_context
*save
= &vbo_context(ctx
)->save
;
1094 const GLint i
= save
->prim_count
- 1;
1096 ctx
->Driver
.CurrentSavePrimitive
= PRIM_OUTSIDE_BEGIN_END
;
1097 save
->prims
[i
].end
= 1;
1098 save
->prims
[i
].count
= (save
->vert_count
- save
->prims
[i
].start
);
1100 if (i
== (GLint
) save
->prim_max
- 1) {
1101 compile_vertex_list(ctx
);
1102 assert(save
->copied
.nr
== 0);
1105 /* Swap out this vertex format while outside begin/end. Any color,
1106 * etc. received between here and the next begin will be compiled
1109 if (save
->out_of_memory
) {
1110 _mesa_install_save_vtxfmt(ctx
, &save
->vtxfmt_noop
);
1113 _mesa_install_save_vtxfmt(ctx
, &ctx
->ListState
.ListVtxfmt
);
1118 static void GLAPIENTRY
1119 _save_Begin(GLenum mode
)
1121 GET_CURRENT_CONTEXT(ctx
);
1123 _mesa_compile_error(ctx
, GL_INVALID_OPERATION
, "Recursive glBegin");
1127 static void GLAPIENTRY
1128 _save_PrimitiveRestartNV(void)
1130 GET_CURRENT_CONTEXT(ctx
);
1131 struct vbo_save_context
*save
= &vbo_context(ctx
)->save
;
1133 if (save
->prim_count
== 0) {
1134 /* We're not inside a glBegin/End pair, so calling glPrimitiverRestartNV
1137 _mesa_compile_error(ctx
, GL_INVALID_OPERATION
,
1138 "glPrimitiveRestartNV called outside glBegin/End");
1140 /* get current primitive mode */
1141 GLenum curPrim
= save
->prims
[save
->prim_count
- 1].mode
;
1143 /* restart primitive */
1144 CALL_End(GET_DISPATCH(), ());
1145 vbo_save_NotifyBegin(ctx
, curPrim
);
1150 /* Unlike the functions above, these are to be hooked into the vtxfmt
1151 * maintained in ctx->ListState, active when the list is known or
1152 * suspected to be outside any begin/end primitive.
1153 * Note: OBE = Outside Begin/End
1155 static void GLAPIENTRY
1156 _save_OBE_Rectf(GLfloat x1
, GLfloat y1
, GLfloat x2
, GLfloat y2
)
1158 GET_CURRENT_CONTEXT(ctx
);
1159 vbo_save_NotifyBegin(ctx
, GL_QUADS
| VBO_SAVE_PRIM_WEAK
);
1160 CALL_Vertex2f(GET_DISPATCH(), (x1
, y1
));
1161 CALL_Vertex2f(GET_DISPATCH(), (x2
, y1
));
1162 CALL_Vertex2f(GET_DISPATCH(), (x2
, y2
));
1163 CALL_Vertex2f(GET_DISPATCH(), (x1
, y2
));
1164 CALL_End(GET_DISPATCH(), ());
1168 static void GLAPIENTRY
1169 _save_OBE_DrawArrays(GLenum mode
, GLint start
, GLsizei count
)
1171 GET_CURRENT_CONTEXT(ctx
);
1172 struct vbo_save_context
*save
= &vbo_context(ctx
)->save
;
1175 if (!_mesa_is_valid_prim_mode(ctx
, mode
)) {
1176 _mesa_compile_error(ctx
, GL_INVALID_ENUM
, "glDrawArrays(mode)");
1180 _mesa_compile_error(ctx
, GL_INVALID_VALUE
, "glDrawArrays(count<0)");
1184 if (save
->out_of_memory
)
1187 /* Make sure to process any VBO binding changes */
1188 _mesa_update_state(ctx
);
1192 vbo_save_NotifyBegin(ctx
, (mode
| VBO_SAVE_PRIM_WEAK
1193 | VBO_SAVE_PRIM_NO_CURRENT_UPDATE
));
1195 for (i
= 0; i
< count
; i
++)
1196 CALL_ArrayElement(GET_DISPATCH(), (start
+ i
));
1197 CALL_End(GET_DISPATCH(), ());
1199 _ae_unmap_vbos(ctx
);
1203 static void GLAPIENTRY
1204 _save_OBE_MultiDrawArrays(GLenum mode
, const GLint
*first
,
1205 const GLsizei
*count
, GLsizei primcount
)
1207 GET_CURRENT_CONTEXT(ctx
);
1210 if (!_mesa_is_valid_prim_mode(ctx
, mode
)) {
1211 _mesa_compile_error(ctx
, GL_INVALID_ENUM
, "glMultiDrawArrays(mode)");
1215 if (primcount
< 0) {
1216 _mesa_compile_error(ctx
, GL_INVALID_VALUE
,
1217 "glMultiDrawArrays(primcount<0)");
1221 for (i
= 0; i
< primcount
; i
++) {
1223 _mesa_compile_error(ctx
, GL_INVALID_VALUE
,
1224 "glMultiDrawArrays(count[i]<0)");
1229 for (i
= 0; i
< primcount
; i
++) {
1231 _save_OBE_DrawArrays(mode
, first
[i
], count
[i
]);
1237 /* Could do better by copying the arrays and element list intact and
1238 * then emitting an indexed prim at runtime.
1240 static void GLAPIENTRY
1241 _save_OBE_DrawElementsBaseVertex(GLenum mode
, GLsizei count
, GLenum type
,
1242 const GLvoid
* indices
, GLint basevertex
)
1244 GET_CURRENT_CONTEXT(ctx
);
1245 struct vbo_save_context
*save
= &vbo_context(ctx
)->save
;
1246 struct gl_buffer_object
*indexbuf
= ctx
->Array
.VAO
->IndexBufferObj
;
1249 if (!_mesa_is_valid_prim_mode(ctx
, mode
)) {
1250 _mesa_compile_error(ctx
, GL_INVALID_ENUM
, "glDrawElements(mode)");
1254 _mesa_compile_error(ctx
, GL_INVALID_VALUE
, "glDrawElements(count<0)");
1257 if (type
!= GL_UNSIGNED_BYTE
&&
1258 type
!= GL_UNSIGNED_SHORT
&&
1259 type
!= GL_UNSIGNED_INT
) {
1260 _mesa_compile_error(ctx
, GL_INVALID_VALUE
, "glDrawElements(count<0)");
1264 if (save
->out_of_memory
)
1267 /* Make sure to process any VBO binding changes */
1268 _mesa_update_state(ctx
);
1272 if (_mesa_is_bufferobj(indexbuf
))
1274 ADD_POINTERS(indexbuf
->Mappings
[MAP_INTERNAL
].Pointer
, indices
);
1276 vbo_save_NotifyBegin(ctx
, (mode
| VBO_SAVE_PRIM_WEAK
|
1277 VBO_SAVE_PRIM_NO_CURRENT_UPDATE
));
1280 case GL_UNSIGNED_BYTE
:
1281 for (i
= 0; i
< count
; i
++)
1282 CALL_ArrayElement(GET_DISPATCH(), (basevertex
+ ((GLubyte
*) indices
)[i
]));
1284 case GL_UNSIGNED_SHORT
:
1285 for (i
= 0; i
< count
; i
++)
1286 CALL_ArrayElement(GET_DISPATCH(), (basevertex
+ ((GLushort
*) indices
)[i
]));
1288 case GL_UNSIGNED_INT
:
1289 for (i
= 0; i
< count
; i
++)
1290 CALL_ArrayElement(GET_DISPATCH(), (basevertex
+ ((GLuint
*) indices
)[i
]));
1293 _mesa_error(ctx
, GL_INVALID_ENUM
, "glDrawElements(type)");
1297 CALL_End(GET_DISPATCH(), ());
1299 _ae_unmap_vbos(ctx
);
1302 static void GLAPIENTRY
1303 _save_OBE_DrawElements(GLenum mode
, GLsizei count
, GLenum type
,
1304 const GLvoid
* indices
)
1306 _save_OBE_DrawElementsBaseVertex(mode
, count
, type
, indices
, 0);
1310 static void GLAPIENTRY
1311 _save_OBE_DrawRangeElements(GLenum mode
, GLuint start
, GLuint end
,
1312 GLsizei count
, GLenum type
,
1313 const GLvoid
* indices
)
1315 GET_CURRENT_CONTEXT(ctx
);
1316 struct vbo_save_context
*save
= &vbo_context(ctx
)->save
;
1318 if (!_mesa_is_valid_prim_mode(ctx
, mode
)) {
1319 _mesa_compile_error(ctx
, GL_INVALID_ENUM
, "glDrawRangeElements(mode)");
1323 _mesa_compile_error(ctx
, GL_INVALID_VALUE
,
1324 "glDrawRangeElements(count<0)");
1327 if (type
!= GL_UNSIGNED_BYTE
&&
1328 type
!= GL_UNSIGNED_SHORT
&&
1329 type
!= GL_UNSIGNED_INT
) {
1330 _mesa_compile_error(ctx
, GL_INVALID_ENUM
, "glDrawRangeElements(type)");
1334 _mesa_compile_error(ctx
, GL_INVALID_VALUE
,
1335 "glDrawRangeElements(end < start)");
1339 if (save
->out_of_memory
)
1342 _save_OBE_DrawElements(mode
, count
, type
, indices
);
1346 static void GLAPIENTRY
1347 _save_OBE_MultiDrawElements(GLenum mode
, const GLsizei
*count
, GLenum type
,
1348 const GLvoid
* const *indices
, GLsizei primcount
)
1352 for (i
= 0; i
< primcount
; i
++) {
1354 CALL_DrawElements(GET_DISPATCH(), (mode
, count
[i
], type
, indices
[i
]));
1360 static void GLAPIENTRY
1361 _save_OBE_MultiDrawElementsBaseVertex(GLenum mode
, const GLsizei
*count
,
1363 const GLvoid
* const *indices
,
1365 const GLint
*basevertex
)
1369 for (i
= 0; i
< primcount
; i
++) {
1371 CALL_DrawElementsBaseVertex(GET_DISPATCH(), (mode
, count
[i
], type
,
1380 vtxfmt_init(struct gl_context
*ctx
)
1382 struct vbo_save_context
*save
= &vbo_context(ctx
)->save
;
1383 GLvertexformat
*vfmt
= &save
->vtxfmt
;
1385 vfmt
->ArrayElement
= _ae_ArrayElement
;
1387 vfmt
->Color3f
= _save_Color3f
;
1388 vfmt
->Color3fv
= _save_Color3fv
;
1389 vfmt
->Color4f
= _save_Color4f
;
1390 vfmt
->Color4fv
= _save_Color4fv
;
1391 vfmt
->EdgeFlag
= _save_EdgeFlag
;
1392 vfmt
->End
= _save_End
;
1393 vfmt
->PrimitiveRestartNV
= _save_PrimitiveRestartNV
;
1394 vfmt
->FogCoordfEXT
= _save_FogCoordfEXT
;
1395 vfmt
->FogCoordfvEXT
= _save_FogCoordfvEXT
;
1396 vfmt
->Indexf
= _save_Indexf
;
1397 vfmt
->Indexfv
= _save_Indexfv
;
1398 vfmt
->Materialfv
= _save_Materialfv
;
1399 vfmt
->MultiTexCoord1fARB
= _save_MultiTexCoord1f
;
1400 vfmt
->MultiTexCoord1fvARB
= _save_MultiTexCoord1fv
;
1401 vfmt
->MultiTexCoord2fARB
= _save_MultiTexCoord2f
;
1402 vfmt
->MultiTexCoord2fvARB
= _save_MultiTexCoord2fv
;
1403 vfmt
->MultiTexCoord3fARB
= _save_MultiTexCoord3f
;
1404 vfmt
->MultiTexCoord3fvARB
= _save_MultiTexCoord3fv
;
1405 vfmt
->MultiTexCoord4fARB
= _save_MultiTexCoord4f
;
1406 vfmt
->MultiTexCoord4fvARB
= _save_MultiTexCoord4fv
;
1407 vfmt
->Normal3f
= _save_Normal3f
;
1408 vfmt
->Normal3fv
= _save_Normal3fv
;
1409 vfmt
->SecondaryColor3fEXT
= _save_SecondaryColor3fEXT
;
1410 vfmt
->SecondaryColor3fvEXT
= _save_SecondaryColor3fvEXT
;
1411 vfmt
->TexCoord1f
= _save_TexCoord1f
;
1412 vfmt
->TexCoord1fv
= _save_TexCoord1fv
;
1413 vfmt
->TexCoord2f
= _save_TexCoord2f
;
1414 vfmt
->TexCoord2fv
= _save_TexCoord2fv
;
1415 vfmt
->TexCoord3f
= _save_TexCoord3f
;
1416 vfmt
->TexCoord3fv
= _save_TexCoord3fv
;
1417 vfmt
->TexCoord4f
= _save_TexCoord4f
;
1418 vfmt
->TexCoord4fv
= _save_TexCoord4fv
;
1419 vfmt
->Vertex2f
= _save_Vertex2f
;
1420 vfmt
->Vertex2fv
= _save_Vertex2fv
;
1421 vfmt
->Vertex3f
= _save_Vertex3f
;
1422 vfmt
->Vertex3fv
= _save_Vertex3fv
;
1423 vfmt
->Vertex4f
= _save_Vertex4f
;
1424 vfmt
->Vertex4fv
= _save_Vertex4fv
;
1425 vfmt
->VertexAttrib1fARB
= _save_VertexAttrib1fARB
;
1426 vfmt
->VertexAttrib1fvARB
= _save_VertexAttrib1fvARB
;
1427 vfmt
->VertexAttrib2fARB
= _save_VertexAttrib2fARB
;
1428 vfmt
->VertexAttrib2fvARB
= _save_VertexAttrib2fvARB
;
1429 vfmt
->VertexAttrib3fARB
= _save_VertexAttrib3fARB
;
1430 vfmt
->VertexAttrib3fvARB
= _save_VertexAttrib3fvARB
;
1431 vfmt
->VertexAttrib4fARB
= _save_VertexAttrib4fARB
;
1432 vfmt
->VertexAttrib4fvARB
= _save_VertexAttrib4fvARB
;
1434 vfmt
->VertexAttrib1fNV
= _save_VertexAttrib1fNV
;
1435 vfmt
->VertexAttrib1fvNV
= _save_VertexAttrib1fvNV
;
1436 vfmt
->VertexAttrib2fNV
= _save_VertexAttrib2fNV
;
1437 vfmt
->VertexAttrib2fvNV
= _save_VertexAttrib2fvNV
;
1438 vfmt
->VertexAttrib3fNV
= _save_VertexAttrib3fNV
;
1439 vfmt
->VertexAttrib3fvNV
= _save_VertexAttrib3fvNV
;
1440 vfmt
->VertexAttrib4fNV
= _save_VertexAttrib4fNV
;
1441 vfmt
->VertexAttrib4fvNV
= _save_VertexAttrib4fvNV
;
1443 /* integer-valued */
1444 vfmt
->VertexAttribI1i
= _save_VertexAttribI1i
;
1445 vfmt
->VertexAttribI2i
= _save_VertexAttribI2i
;
1446 vfmt
->VertexAttribI3i
= _save_VertexAttribI3i
;
1447 vfmt
->VertexAttribI4i
= _save_VertexAttribI4i
;
1448 vfmt
->VertexAttribI2iv
= _save_VertexAttribI2iv
;
1449 vfmt
->VertexAttribI3iv
= _save_VertexAttribI3iv
;
1450 vfmt
->VertexAttribI4iv
= _save_VertexAttribI4iv
;
1452 /* unsigned integer-valued */
1453 vfmt
->VertexAttribI1ui
= _save_VertexAttribI1ui
;
1454 vfmt
->VertexAttribI2ui
= _save_VertexAttribI2ui
;
1455 vfmt
->VertexAttribI3ui
= _save_VertexAttribI3ui
;
1456 vfmt
->VertexAttribI4ui
= _save_VertexAttribI4ui
;
1457 vfmt
->VertexAttribI2uiv
= _save_VertexAttribI2uiv
;
1458 vfmt
->VertexAttribI3uiv
= _save_VertexAttribI3uiv
;
1459 vfmt
->VertexAttribI4uiv
= _save_VertexAttribI4uiv
;
1461 vfmt
->VertexP2ui
= _save_VertexP2ui
;
1462 vfmt
->VertexP3ui
= _save_VertexP3ui
;
1463 vfmt
->VertexP4ui
= _save_VertexP4ui
;
1464 vfmt
->VertexP2uiv
= _save_VertexP2uiv
;
1465 vfmt
->VertexP3uiv
= _save_VertexP3uiv
;
1466 vfmt
->VertexP4uiv
= _save_VertexP4uiv
;
1468 vfmt
->TexCoordP1ui
= _save_TexCoordP1ui
;
1469 vfmt
->TexCoordP2ui
= _save_TexCoordP2ui
;
1470 vfmt
->TexCoordP3ui
= _save_TexCoordP3ui
;
1471 vfmt
->TexCoordP4ui
= _save_TexCoordP4ui
;
1472 vfmt
->TexCoordP1uiv
= _save_TexCoordP1uiv
;
1473 vfmt
->TexCoordP2uiv
= _save_TexCoordP2uiv
;
1474 vfmt
->TexCoordP3uiv
= _save_TexCoordP3uiv
;
1475 vfmt
->TexCoordP4uiv
= _save_TexCoordP4uiv
;
1477 vfmt
->MultiTexCoordP1ui
= _save_MultiTexCoordP1ui
;
1478 vfmt
->MultiTexCoordP2ui
= _save_MultiTexCoordP2ui
;
1479 vfmt
->MultiTexCoordP3ui
= _save_MultiTexCoordP3ui
;
1480 vfmt
->MultiTexCoordP4ui
= _save_MultiTexCoordP4ui
;
1481 vfmt
->MultiTexCoordP1uiv
= _save_MultiTexCoordP1uiv
;
1482 vfmt
->MultiTexCoordP2uiv
= _save_MultiTexCoordP2uiv
;
1483 vfmt
->MultiTexCoordP3uiv
= _save_MultiTexCoordP3uiv
;
1484 vfmt
->MultiTexCoordP4uiv
= _save_MultiTexCoordP4uiv
;
1486 vfmt
->NormalP3ui
= _save_NormalP3ui
;
1487 vfmt
->NormalP3uiv
= _save_NormalP3uiv
;
1489 vfmt
->ColorP3ui
= _save_ColorP3ui
;
1490 vfmt
->ColorP4ui
= _save_ColorP4ui
;
1491 vfmt
->ColorP3uiv
= _save_ColorP3uiv
;
1492 vfmt
->ColorP4uiv
= _save_ColorP4uiv
;
1494 vfmt
->SecondaryColorP3ui
= _save_SecondaryColorP3ui
;
1495 vfmt
->SecondaryColorP3uiv
= _save_SecondaryColorP3uiv
;
1497 vfmt
->VertexAttribP1ui
= _save_VertexAttribP1ui
;
1498 vfmt
->VertexAttribP2ui
= _save_VertexAttribP2ui
;
1499 vfmt
->VertexAttribP3ui
= _save_VertexAttribP3ui
;
1500 vfmt
->VertexAttribP4ui
= _save_VertexAttribP4ui
;
1502 vfmt
->VertexAttribP1uiv
= _save_VertexAttribP1uiv
;
1503 vfmt
->VertexAttribP2uiv
= _save_VertexAttribP2uiv
;
1504 vfmt
->VertexAttribP3uiv
= _save_VertexAttribP3uiv
;
1505 vfmt
->VertexAttribP4uiv
= _save_VertexAttribP4uiv
;
1507 vfmt
->VertexAttribL1d
= _save_VertexAttribL1d
;
1508 vfmt
->VertexAttribL2d
= _save_VertexAttribL2d
;
1509 vfmt
->VertexAttribL3d
= _save_VertexAttribL3d
;
1510 vfmt
->VertexAttribL4d
= _save_VertexAttribL4d
;
1512 vfmt
->VertexAttribL1dv
= _save_VertexAttribL1dv
;
1513 vfmt
->VertexAttribL2dv
= _save_VertexAttribL2dv
;
1514 vfmt
->VertexAttribL3dv
= _save_VertexAttribL3dv
;
1515 vfmt
->VertexAttribL4dv
= _save_VertexAttribL4dv
;
1517 vfmt
->VertexAttribL1ui64ARB
= _save_VertexAttribL1ui64ARB
;
1518 vfmt
->VertexAttribL1ui64vARB
= _save_VertexAttribL1ui64vARB
;
1520 /* This will all require us to fallback to saving the list as opcodes:
1522 vfmt
->CallList
= _save_CallList
;
1523 vfmt
->CallLists
= _save_CallLists
;
1525 vfmt
->EvalCoord1f
= _save_EvalCoord1f
;
1526 vfmt
->EvalCoord1fv
= _save_EvalCoord1fv
;
1527 vfmt
->EvalCoord2f
= _save_EvalCoord2f
;
1528 vfmt
->EvalCoord2fv
= _save_EvalCoord2fv
;
1529 vfmt
->EvalPoint1
= _save_EvalPoint1
;
1530 vfmt
->EvalPoint2
= _save_EvalPoint2
;
1532 /* These calls all generate GL_INVALID_OPERATION since this vtxfmt is
1533 * only used when we're inside a glBegin/End pair.
1535 vfmt
->Begin
= _save_Begin
;
1540 * Initialize the dispatch table with the VBO functions for display
1544 vbo_initialize_save_dispatch(const struct gl_context
*ctx
,
1545 struct _glapi_table
*exec
)
1547 SET_DrawArrays(exec
, _save_OBE_DrawArrays
);
1548 SET_MultiDrawArrays(exec
, _save_OBE_MultiDrawArrays
);
1549 SET_DrawElements(exec
, _save_OBE_DrawElements
);
1550 SET_DrawElementsBaseVertex(exec
, _save_OBE_DrawElementsBaseVertex
);
1551 SET_DrawRangeElements(exec
, _save_OBE_DrawRangeElements
);
1552 SET_MultiDrawElementsEXT(exec
, _save_OBE_MultiDrawElements
);
1553 SET_MultiDrawElementsBaseVertex(exec
, _save_OBE_MultiDrawElementsBaseVertex
);
1554 SET_Rectf(exec
, _save_OBE_Rectf
);
1555 /* Note: other glDraw functins aren't compiled into display lists */
1561 vbo_save_SaveFlushVertices(struct gl_context
*ctx
)
1563 struct vbo_save_context
*save
= &vbo_context(ctx
)->save
;
1565 /* Noop when we are actually active:
1567 if (ctx
->Driver
.CurrentSavePrimitive
<= PRIM_MAX
)
1570 if (save
->vert_count
|| save
->prim_count
)
1571 compile_vertex_list(ctx
);
1573 copy_to_current(ctx
);
1575 reset_counters(ctx
);
1576 ctx
->Driver
.SaveNeedFlush
= GL_FALSE
;
1581 * Called from glNewList when we're starting to compile a display list.
1584 vbo_save_NewList(struct gl_context
*ctx
, GLuint list
, GLenum mode
)
1586 struct vbo_save_context
*save
= &vbo_context(ctx
)->save
;
1591 if (!save
->prim_store
)
1592 save
->prim_store
= alloc_prim_store();
1594 if (!save
->vertex_store
)
1595 save
->vertex_store
= alloc_vertex_store(ctx
);
1597 save
->buffer_ptr
= vbo_save_map_vertex_store(ctx
, save
->vertex_store
);
1600 reset_counters(ctx
);
1601 ctx
->Driver
.SaveNeedFlush
= GL_FALSE
;
1606 * Called from glEndList when we're finished compiling a display list.
1609 vbo_save_EndList(struct gl_context
*ctx
)
1611 struct vbo_save_context
*save
= &vbo_context(ctx
)->save
;
1613 /* EndList called inside a (saved) Begin/End pair?
1615 if (_mesa_inside_dlist_begin_end(ctx
)) {
1616 if (save
->prim_count
> 0) {
1617 GLint i
= save
->prim_count
- 1;
1618 ctx
->Driver
.CurrentSavePrimitive
= PRIM_OUTSIDE_BEGIN_END
;
1619 save
->prims
[i
].end
= 0;
1620 save
->prims
[i
].count
= save
->vert_count
- save
->prims
[i
].start
;
1623 /* Make sure this vertex list gets replayed by the "loopback"
1626 save
->dangling_attr_ref
= GL_TRUE
;
1627 vbo_save_SaveFlushVertices(ctx
);
1629 /* Swap out this vertex format while outside begin/end. Any color,
1630 * etc. received between here and the next begin will be compiled
1633 _mesa_install_save_vtxfmt(ctx
, &ctx
->ListState
.ListVtxfmt
);
1636 vbo_save_unmap_vertex_store(ctx
, save
->vertex_store
);
1638 assert(save
->vertex_size
== 0);
1643 * Called from the display list code when we're about to execute a
1647 vbo_save_BeginCallList(struct gl_context
*ctx
, struct gl_display_list
*dlist
)
1649 struct vbo_save_context
*save
= &vbo_context(ctx
)->save
;
1650 save
->replay_flags
|= dlist
->Flags
;
1655 * Called from the display list code when we're finished executing a
1659 vbo_save_EndCallList(struct gl_context
*ctx
)
1661 struct vbo_save_context
*save
= &vbo_context(ctx
)->save
;
1663 if (ctx
->ListState
.CallDepth
== 1) {
1664 /* This is correct: want to keep only the VBO_SAVE_FALLBACK
1665 * flag, if it is set:
1667 save
->replay_flags
&= VBO_SAVE_FALLBACK
;
1673 * Called by display list code when a display list is being deleted.
1676 vbo_destroy_vertex_list(struct gl_context
*ctx
, void *data
)
1678 struct vbo_save_vertex_list
*node
= (struct vbo_save_vertex_list
*) data
;
1681 if (--node
->vertex_store
->refcount
== 0)
1682 free_vertex_store(ctx
, node
->vertex_store
);
1684 if (--node
->prim_store
->refcount
== 0)
1685 free(node
->prim_store
);
1687 free(node
->current_data
);
1688 node
->current_data
= NULL
;
1693 vbo_print_vertex_list(struct gl_context
*ctx
, void *data
, FILE *f
)
1695 struct vbo_save_vertex_list
*node
= (struct vbo_save_vertex_list
*) data
;
1697 struct gl_buffer_object
*buffer
= node
->vertex_store
?
1698 node
->vertex_store
->bufferobj
: NULL
;
1701 fprintf(f
, "VBO-VERTEX-LIST, %u vertices, %d primitives, %d vertsize, "
1703 node
->vertex_count
, node
->prim_count
, node
->vertex_size
,
1706 for (i
= 0; i
< node
->prim_count
; i
++) {
1707 struct _mesa_prim
*prim
= &node
->prims
[i
];
1708 fprintf(f
, " prim %d: %s%s %d..%d %s %s\n",
1710 _mesa_lookup_prim_by_nr(prim
->mode
),
1711 prim
->weak
? " (weak)" : "",
1713 prim
->start
+ prim
->count
,
1714 (prim
->begin
) ? "BEGIN" : "(wrap)",
1715 (prim
->end
) ? "END" : "(wrap)");
1721 * Called during context creation/init.
1724 current_init(struct gl_context
*ctx
)
1726 struct vbo_save_context
*save
= &vbo_context(ctx
)->save
;
1729 for (i
= VBO_ATTRIB_POS
; i
<= VBO_ATTRIB_GENERIC15
; i
++) {
1730 const GLuint j
= i
- VBO_ATTRIB_POS
;
1731 assert(j
< VERT_ATTRIB_MAX
);
1732 save
->currentsz
[i
] = &ctx
->ListState
.ActiveAttribSize
[j
];
1733 save
->current
[i
] = (fi_type
*) ctx
->ListState
.CurrentAttrib
[j
];
1736 for (i
= VBO_ATTRIB_FIRST_MATERIAL
; i
<= VBO_ATTRIB_LAST_MATERIAL
; i
++) {
1737 const GLuint j
= i
- VBO_ATTRIB_FIRST_MATERIAL
;
1738 assert(j
< MAT_ATTRIB_MAX
);
1739 save
->currentsz
[i
] = &ctx
->ListState
.ActiveMaterialSize
[j
];
1740 save
->current
[i
] = (fi_type
*) ctx
->ListState
.CurrentMaterial
[j
];
1746 * Initialize the display list compiler. Called during context creation.
1749 vbo_save_api_init(struct vbo_save_context
*save
)
1751 struct gl_context
*ctx
= save
->ctx
;
1754 save
->opcode_vertex_list
=
1755 _mesa_dlist_alloc_opcode(ctx
,
1756 sizeof(struct vbo_save_vertex_list
),
1757 vbo_save_playback_vertex_list
,
1758 vbo_destroy_vertex_list
,
1759 vbo_print_vertex_list
);
1763 _mesa_noop_vtxfmt_init(&save
->vtxfmt_noop
);
1765 /* These will actually get set again when binding/drawing */
1766 for (i
= 0; i
< VBO_ATTRIB_MAX
; i
++)
1767 save
->inputs
[i
] = &save
->arrays
[i
];