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"
85 #include "vbo_private.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 STATIC_ASSERT(sizeof(node
->attrsz
) == sizeof(save
->attrsz
));
440 memcpy(node
->attrsz
, save
->attrsz
, sizeof(node
->attrsz
));
441 STATIC_ASSERT(sizeof(node
->attrtype
) == sizeof(save
->attrtype
));
442 memcpy(node
->attrtype
, save
->attrtype
, sizeof(node
->attrtype
));
443 node
->vertex_size
= save
->vertex_size
;
444 node
->buffer_offset
=
445 (save
->buffer_map
- save
->vertex_store
->buffer_map
) * sizeof(GLfloat
);
446 node
->vertex_count
= save
->vert_count
;
447 node
->wrap_count
= save
->copied
.nr
;
448 node
->dangling_attr_ref
= save
->dangling_attr_ref
;
449 node
->prims
= save
->prims
;
450 node
->prim_count
= save
->prim_count
;
451 node
->vertex_store
= save
->vertex_store
;
452 node
->prim_store
= save
->prim_store
;
454 node
->vertex_store
->refcount
++;
455 node
->prim_store
->refcount
++;
457 if (node
->prims
[0].no_current_update
) {
458 node
->current_size
= 0;
459 node
->current_data
= NULL
;
462 node
->current_size
= node
->vertex_size
- node
->attrsz
[0];
463 node
->current_data
= NULL
;
465 if (node
->current_size
) {
466 /* If the malloc fails, we just pull the data out of the VBO
469 node
->current_data
= malloc(node
->current_size
* sizeof(GLfloat
));
470 if (node
->current_data
) {
471 const char *buffer
= (const char *) save
->vertex_store
->buffer_map
;
472 unsigned attr_offset
= node
->attrsz
[0] * sizeof(GLfloat
);
473 unsigned vertex_offset
= 0;
475 if (node
->vertex_count
)
477 (node
->vertex_count
- 1) * node
->vertex_size
* sizeof(GLfloat
);
479 memcpy(node
->current_data
,
480 buffer
+ node
->buffer_offset
+ vertex_offset
+ attr_offset
,
481 node
->current_size
* sizeof(GLfloat
));
486 assert(node
->attrsz
[VBO_ATTRIB_POS
] != 0 || node
->vertex_count
== 0);
488 if (save
->dangling_attr_ref
)
489 ctx
->ListState
.CurrentList
->Flags
|= DLIST_DANGLING_REFS
;
491 save
->vertex_store
->used
+= save
->vertex_size
* node
->vertex_count
;
492 save
->prim_store
->used
+= node
->prim_count
;
494 /* Copy duplicated vertices
496 save
->copied
.nr
= copy_vertices(ctx
, node
, save
->buffer_map
);
498 if (node
->prims
[node
->prim_count
- 1].mode
== GL_LINE_LOOP
) {
499 convert_line_loop_to_strip(save
, node
);
502 merge_prims(node
->prims
, &node
->prim_count
);
504 /* Deal with GL_COMPILE_AND_EXECUTE:
506 if (ctx
->ExecuteFlag
) {
507 struct _glapi_table
*dispatch
= GET_DISPATCH();
509 _glapi_set_dispatch(ctx
->Exec
);
511 const GLfloat
*buffer
= (const GLfloat
*)
512 ((const char *) save
->vertex_store
->buffer_map
+
513 node
->buffer_offset
);
515 vbo_loopback_vertex_list(ctx
, buffer
,
516 node
->attrsz
, node
->prims
, node
->prim_count
,
517 node
->wrap_count
, node
->vertex_size
);
519 _glapi_set_dispatch(dispatch
);
522 /* Decide whether the storage structs are full, or can be used for
523 * the next vertex lists as well.
525 if (save
->vertex_store
->used
>
526 VBO_SAVE_BUFFER_SIZE
- 16 * (save
->vertex_size
+ 4)) {
530 vbo_save_unmap_vertex_store(ctx
, save
->vertex_store
);
532 /* Release old reference:
534 save
->vertex_store
->refcount
--;
535 assert(save
->vertex_store
->refcount
!= 0);
536 save
->vertex_store
= NULL
;
538 /* Allocate and map new store:
540 save
->vertex_store
= alloc_vertex_store(ctx
);
541 save
->buffer_ptr
= vbo_save_map_vertex_store(ctx
, save
->vertex_store
);
542 save
->out_of_memory
= save
->buffer_ptr
== NULL
;
545 /* update buffer_ptr for next vertex */
546 save
->buffer_ptr
= save
->vertex_store
->buffer_map
547 + save
->vertex_store
->used
;
550 if (save
->prim_store
->used
> VBO_SAVE_PRIM_SIZE
- 6) {
551 save
->prim_store
->refcount
--;
552 assert(save
->prim_store
->refcount
!= 0);
553 save
->prim_store
= alloc_prim_store();
557 * If the vertex buffer offset is a multiple of the vertex size,
558 * we can use the _mesa_prim::start value to indicate where the
559 * vertices starts, instead of the buffer offset. Also see the
560 * bind_vertex_list() function.
562 if (aligned_vertex_buffer_offset(node
)) {
563 const unsigned start_offset
=
564 node
->buffer_offset
/ (node
->vertex_size
* sizeof(GLfloat
));
565 for (unsigned i
= 0; i
< save
->prim_count
; i
++) {
566 save
->prims
[i
].start
+= start_offset
;
568 node
->start_vertex
= start_offset
;
570 node
->start_vertex
= 0;
573 /* Reset our structures for the next run of vertices:
580 * This is called when we fill a vertex buffer before we hit a glEnd().
582 * TODO -- If no new vertices have been stored, don't bother saving it.
585 wrap_buffers(struct gl_context
*ctx
)
587 struct vbo_save_context
*save
= &vbo_context(ctx
)->save
;
588 GLint i
= save
->prim_count
- 1;
591 GLboolean no_current_update
;
593 assert(i
< (GLint
) save
->prim_max
);
596 /* Close off in-progress primitive.
598 save
->prims
[i
].count
= (save
->vert_count
- save
->prims
[i
].start
);
599 mode
= save
->prims
[i
].mode
;
600 weak
= save
->prims
[i
].weak
;
601 no_current_update
= save
->prims
[i
].no_current_update
;
603 /* store the copied vertices, and allocate a new list.
605 compile_vertex_list(ctx
);
607 /* Restart interrupted primitive
609 save
->prims
[0].mode
= mode
;
610 save
->prims
[0].weak
= weak
;
611 save
->prims
[0].no_current_update
= no_current_update
;
612 save
->prims
[0].begin
= 0;
613 save
->prims
[0].end
= 0;
614 save
->prims
[0].pad
= 0;
615 save
->prims
[0].start
= 0;
616 save
->prims
[0].count
= 0;
617 save
->prims
[0].num_instances
= 1;
618 save
->prims
[0].base_instance
= 0;
619 save
->prims
[0].is_indirect
= 0;
620 save
->prim_count
= 1;
625 * Called only when buffers are wrapped as the result of filling the
626 * vertex_store struct.
629 wrap_filled_vertex(struct gl_context
*ctx
)
631 struct vbo_save_context
*save
= &vbo_context(ctx
)->save
;
632 unsigned numComponents
;
634 /* Emit a glEnd to close off the last vertex list.
638 /* Copy stored stored vertices to start of new list.
640 assert(save
->max_vert
- save
->vert_count
> save
->copied
.nr
);
642 numComponents
= save
->copied
.nr
* save
->vertex_size
;
643 memcpy(save
->buffer_ptr
,
645 numComponents
* sizeof(fi_type
));
646 save
->buffer_ptr
+= numComponents
;
647 save
->vert_count
+= save
->copied
.nr
;
652 copy_to_current(struct gl_context
*ctx
)
654 struct vbo_save_context
*save
= &vbo_context(ctx
)->save
;
655 GLbitfield64 enabled
= save
->enabled
& (~BITFIELD64_BIT(VBO_ATTRIB_POS
));
658 const int i
= u_bit_scan64(&enabled
);
659 assert(save
->attrsz
[i
]);
661 save
->currentsz
[i
][0] = save
->attrsz
[i
];
662 COPY_CLEAN_4V_TYPE_AS_UNION(save
->current
[i
], save
->attrsz
[i
],
663 save
->attrptr
[i
], save
->attrtype
[i
]);
669 copy_from_current(struct gl_context
*ctx
)
671 struct vbo_save_context
*save
= &vbo_context(ctx
)->save
;
672 GLbitfield64 enabled
= save
->enabled
& (~BITFIELD64_BIT(VBO_ATTRIB_POS
));
675 const int i
= u_bit_scan64(&enabled
);
677 switch (save
->attrsz
[i
]) {
679 save
->attrptr
[i
][3] = save
->current
[i
][3];
681 save
->attrptr
[i
][2] = save
->current
[i
][2];
683 save
->attrptr
[i
][1] = save
->current
[i
][1];
685 save
->attrptr
[i
][0] = save
->current
[i
][0];
688 unreachable("Unexpected vertex attribute size");
695 * Called when we increase the size of a vertex attribute. For example,
696 * if we've seen one or more glTexCoord2f() calls and now we get a
697 * glTexCoord3f() call.
698 * Flush existing data, set new attrib size, replay copied vertices.
701 upgrade_vertex(struct gl_context
*ctx
, GLuint attr
, GLuint newsz
)
703 struct vbo_save_context
*save
= &vbo_context(ctx
)->save
;
708 /* Store the current run of vertices, and emit a GL_END. Emit a
709 * BEGIN in the new buffer.
711 if (save
->vert_count
)
714 assert(save
->copied
.nr
== 0);
716 /* Do a COPY_TO_CURRENT to ensure back-copying works for the case
717 * when the attribute already exists in the vertex and is having
718 * its size increased.
720 copy_to_current(ctx
);
724 oldsz
= save
->attrsz
[attr
];
725 save
->attrsz
[attr
] = newsz
;
726 save
->enabled
|= BITFIELD64_BIT(attr
);
728 save
->vertex_size
+= newsz
- oldsz
;
729 save
->max_vert
= ((VBO_SAVE_BUFFER_SIZE
- save
->vertex_store
->used
) /
731 save
->vert_count
= 0;
733 /* Recalculate all the attrptr[] values:
736 for (i
= 0; i
< VBO_ATTRIB_MAX
; i
++) {
737 if (save
->attrsz
[i
]) {
738 save
->attrptr
[i
] = tmp
;
739 tmp
+= save
->attrsz
[i
];
742 save
->attrptr
[i
] = NULL
; /* will not be dereferenced. */
746 /* Copy from current to repopulate the vertex with correct values.
748 copy_from_current(ctx
);
750 /* Replay stored vertices to translate them to new format here.
752 * If there are copied vertices and the new (upgraded) attribute
753 * has not been defined before, this list is somewhat degenerate,
754 * and will need fixup at runtime.
756 if (save
->copied
.nr
) {
757 const fi_type
*data
= save
->copied
.buffer
;
758 fi_type
*dest
= save
->buffer_map
;
760 /* Need to note this and fix up at runtime (or loopback):
762 if (attr
!= VBO_ATTRIB_POS
&& save
->currentsz
[attr
][0] == 0) {
764 save
->dangling_attr_ref
= GL_TRUE
;
767 for (i
= 0; i
< save
->copied
.nr
; i
++) {
768 GLbitfield64 enabled
= save
->enabled
;
770 const int j
= u_bit_scan64(&enabled
);
771 assert(save
->attrsz
[j
]);
774 COPY_CLEAN_4V_TYPE_AS_UNION(dest
, oldsz
, data
,
780 COPY_SZ_4V(dest
, newsz
, save
->current
[attr
]);
785 GLint sz
= save
->attrsz
[j
];
786 COPY_SZ_4V(dest
, sz
, data
);
793 save
->buffer_ptr
= dest
;
794 save
->vert_count
+= save
->copied
.nr
;
800 * This is called when the size of a vertex attribute changes.
801 * For example, after seeing one or more glTexCoord2f() calls we
802 * get a glTexCoord4f() or glTexCoord1f() call.
805 fixup_vertex(struct gl_context
*ctx
, GLuint attr
, GLuint sz
)
807 struct vbo_save_context
*save
= &vbo_context(ctx
)->save
;
809 if (sz
> save
->attrsz
[attr
]) {
810 /* New size is larger. Need to flush existing vertices and get
811 * an enlarged vertex format.
813 upgrade_vertex(ctx
, attr
, sz
);
815 else if (sz
< save
->active_sz
[attr
]) {
817 const fi_type
*id
= vbo_get_default_vals_as_union(save
->attrtype
[attr
]);
819 /* New size is equal or smaller - just need to fill in some
822 for (i
= sz
; i
<= save
->attrsz
[attr
]; i
++)
823 save
->attrptr
[attr
][i
- 1] = id
[i
- 1];
826 save
->active_sz
[attr
] = sz
;
831 * Reset the current size of all vertex attributes to the default
832 * value of 0. This signals that we haven't yet seen any per-vertex
833 * commands such as glNormal3f() or glTexCoord2f().
836 reset_vertex(struct gl_context
*ctx
)
838 struct vbo_save_context
*save
= &vbo_context(ctx
)->save
;
840 while (save
->enabled
) {
841 const int i
= u_bit_scan64(&save
->enabled
);
842 assert(save
->attrsz
[i
]);
844 save
->active_sz
[i
] = 0;
847 save
->vertex_size
= 0;
852 #define ERROR(err) _mesa_compile_error(ctx, err, __func__);
855 /* Only one size for each attribute may be active at once. Eg. if
856 * Color3f is installed/active, then Color4f may not be, even if the
857 * vertex actually contains 4 color coordinates. This is because the
858 * 3f version won't otherwise set color[3] to 1.0 -- this is the job
859 * of the chooser function when switching between Color4f and Color3f.
861 #define ATTR_UNION(A, N, T, C, V0, V1, V2, V3) \
863 struct vbo_save_context *save = &vbo_context(ctx)->save; \
865 if (save->active_sz[A] != N) \
866 fixup_vertex(ctx, A, N); \
869 C *dest = (C *)save->attrptr[A]; \
870 if (N>0) dest[0] = V0; \
871 if (N>1) dest[1] = V1; \
872 if (N>2) dest[2] = V2; \
873 if (N>3) dest[3] = V3; \
874 save->attrtype[A] = T; \
880 for (i = 0; i < save->vertex_size; i++) \
881 save->buffer_ptr[i] = save->vertex[i]; \
883 save->buffer_ptr += save->vertex_size; \
885 if (++save->vert_count >= save->max_vert) \
886 wrap_filled_vertex(ctx); \
890 #define TAG(x) _save_##x
892 #include "vbo_attrib_tmp.h"
896 #define MAT( ATTR, N, face, params ) \
898 if (face != GL_BACK) \
899 MAT_ATTR( ATTR, N, params ); /* front */ \
900 if (face != GL_FRONT) \
901 MAT_ATTR( ATTR + 1, N, params ); /* back */ \
906 * Save a glMaterial call found between glBegin/End.
907 * glMaterial calls outside Begin/End are handled in dlist.c.
909 static void GLAPIENTRY
910 _save_Materialfv(GLenum face
, GLenum pname
, const GLfloat
*params
)
912 GET_CURRENT_CONTEXT(ctx
);
914 if (face
!= GL_FRONT
&& face
!= GL_BACK
&& face
!= GL_FRONT_AND_BACK
) {
915 _mesa_compile_error(ctx
, GL_INVALID_ENUM
, "glMaterial(face)");
921 MAT(VBO_ATTRIB_MAT_FRONT_EMISSION
, 4, face
, params
);
924 MAT(VBO_ATTRIB_MAT_FRONT_AMBIENT
, 4, face
, params
);
927 MAT(VBO_ATTRIB_MAT_FRONT_DIFFUSE
, 4, face
, params
);
930 MAT(VBO_ATTRIB_MAT_FRONT_SPECULAR
, 4, face
, params
);
933 if (*params
< 0 || *params
> ctx
->Const
.MaxShininess
) {
934 _mesa_compile_error(ctx
, GL_INVALID_VALUE
, "glMaterial(shininess)");
937 MAT(VBO_ATTRIB_MAT_FRONT_SHININESS
, 1, face
, params
);
940 case GL_COLOR_INDEXES
:
941 MAT(VBO_ATTRIB_MAT_FRONT_INDEXES
, 3, face
, params
);
943 case GL_AMBIENT_AND_DIFFUSE
:
944 MAT(VBO_ATTRIB_MAT_FRONT_AMBIENT
, 4, face
, params
);
945 MAT(VBO_ATTRIB_MAT_FRONT_DIFFUSE
, 4, face
, params
);
948 _mesa_compile_error(ctx
, GL_INVALID_ENUM
, "glMaterial(pname)");
954 /* Cope with EvalCoord/CallList called within a begin/end object:
955 * -- Flush current buffer
956 * -- Fallback to opcodes for the rest of the begin/end object.
959 dlist_fallback(struct gl_context
*ctx
)
961 struct vbo_save_context
*save
= &vbo_context(ctx
)->save
;
963 if (save
->vert_count
|| save
->prim_count
) {
964 if (save
->prim_count
> 0) {
965 /* Close off in-progress primitive. */
966 GLint i
= save
->prim_count
- 1;
967 save
->prims
[i
].count
= save
->vert_count
- save
->prims
[i
].start
;
970 /* Need to replay this display list with loopback,
971 * unfortunately, otherwise this primitive won't be handled
974 save
->dangling_attr_ref
= GL_TRUE
;
976 compile_vertex_list(ctx
);
979 copy_to_current(ctx
);
982 if (save
->out_of_memory
) {
983 _mesa_install_save_vtxfmt(ctx
, &save
->vtxfmt_noop
);
986 _mesa_install_save_vtxfmt(ctx
, &ctx
->ListState
.ListVtxfmt
);
988 ctx
->Driver
.SaveNeedFlush
= GL_FALSE
;
992 static void GLAPIENTRY
993 _save_EvalCoord1f(GLfloat u
)
995 GET_CURRENT_CONTEXT(ctx
);
997 CALL_EvalCoord1f(ctx
->Save
, (u
));
1000 static void GLAPIENTRY
1001 _save_EvalCoord1fv(const GLfloat
* v
)
1003 GET_CURRENT_CONTEXT(ctx
);
1004 dlist_fallback(ctx
);
1005 CALL_EvalCoord1fv(ctx
->Save
, (v
));
1008 static void GLAPIENTRY
1009 _save_EvalCoord2f(GLfloat u
, GLfloat v
)
1011 GET_CURRENT_CONTEXT(ctx
);
1012 dlist_fallback(ctx
);
1013 CALL_EvalCoord2f(ctx
->Save
, (u
, v
));
1016 static void GLAPIENTRY
1017 _save_EvalCoord2fv(const GLfloat
* v
)
1019 GET_CURRENT_CONTEXT(ctx
);
1020 dlist_fallback(ctx
);
1021 CALL_EvalCoord2fv(ctx
->Save
, (v
));
1024 static void GLAPIENTRY
1025 _save_EvalPoint1(GLint i
)
1027 GET_CURRENT_CONTEXT(ctx
);
1028 dlist_fallback(ctx
);
1029 CALL_EvalPoint1(ctx
->Save
, (i
));
1032 static void GLAPIENTRY
1033 _save_EvalPoint2(GLint i
, GLint j
)
1035 GET_CURRENT_CONTEXT(ctx
);
1036 dlist_fallback(ctx
);
1037 CALL_EvalPoint2(ctx
->Save
, (i
, j
));
1040 static void GLAPIENTRY
1041 _save_CallList(GLuint l
)
1043 GET_CURRENT_CONTEXT(ctx
);
1044 dlist_fallback(ctx
);
1045 CALL_CallList(ctx
->Save
, (l
));
1048 static void GLAPIENTRY
1049 _save_CallLists(GLsizei n
, GLenum type
, const GLvoid
* v
)
1051 GET_CURRENT_CONTEXT(ctx
);
1052 dlist_fallback(ctx
);
1053 CALL_CallLists(ctx
->Save
, (n
, type
, v
));
1059 * Called when a glBegin is getting compiled into a display list.
1060 * Updating of ctx->Driver.CurrentSavePrimitive is already taken care of.
1063 vbo_save_NotifyBegin(struct gl_context
*ctx
, GLenum mode
)
1065 struct vbo_save_context
*save
= &vbo_context(ctx
)->save
;
1066 const GLuint i
= save
->prim_count
++;
1068 assert(i
< save
->prim_max
);
1069 save
->prims
[i
].mode
= mode
& VBO_SAVE_PRIM_MODE_MASK
;
1070 save
->prims
[i
].begin
= 1;
1071 save
->prims
[i
].end
= 0;
1072 save
->prims
[i
].weak
= (mode
& VBO_SAVE_PRIM_WEAK
) ? 1 : 0;
1073 save
->prims
[i
].no_current_update
=
1074 (mode
& VBO_SAVE_PRIM_NO_CURRENT_UPDATE
) ? 1 : 0;
1075 save
->prims
[i
].pad
= 0;
1076 save
->prims
[i
].start
= save
->vert_count
;
1077 save
->prims
[i
].count
= 0;
1078 save
->prims
[i
].num_instances
= 1;
1079 save
->prims
[i
].base_instance
= 0;
1080 save
->prims
[i
].is_indirect
= 0;
1082 if (save
->out_of_memory
) {
1083 _mesa_install_save_vtxfmt(ctx
, &save
->vtxfmt_noop
);
1086 _mesa_install_save_vtxfmt(ctx
, &save
->vtxfmt
);
1089 /* We need to call vbo_save_SaveFlushVertices() if there's state change */
1090 ctx
->Driver
.SaveNeedFlush
= GL_TRUE
;
1094 static void GLAPIENTRY
1097 GET_CURRENT_CONTEXT(ctx
);
1098 struct vbo_save_context
*save
= &vbo_context(ctx
)->save
;
1099 const GLint i
= save
->prim_count
- 1;
1101 ctx
->Driver
.CurrentSavePrimitive
= PRIM_OUTSIDE_BEGIN_END
;
1102 save
->prims
[i
].end
= 1;
1103 save
->prims
[i
].count
= (save
->vert_count
- save
->prims
[i
].start
);
1105 if (i
== (GLint
) save
->prim_max
- 1) {
1106 compile_vertex_list(ctx
);
1107 assert(save
->copied
.nr
== 0);
1110 /* Swap out this vertex format while outside begin/end. Any color,
1111 * etc. received between here and the next begin will be compiled
1114 if (save
->out_of_memory
) {
1115 _mesa_install_save_vtxfmt(ctx
, &save
->vtxfmt_noop
);
1118 _mesa_install_save_vtxfmt(ctx
, &ctx
->ListState
.ListVtxfmt
);
1123 static void GLAPIENTRY
1124 _save_Begin(GLenum mode
)
1126 GET_CURRENT_CONTEXT(ctx
);
1128 _mesa_compile_error(ctx
, GL_INVALID_OPERATION
, "Recursive glBegin");
1132 static void GLAPIENTRY
1133 _save_PrimitiveRestartNV(void)
1135 GET_CURRENT_CONTEXT(ctx
);
1136 struct vbo_save_context
*save
= &vbo_context(ctx
)->save
;
1138 if (save
->prim_count
== 0) {
1139 /* We're not inside a glBegin/End pair, so calling glPrimitiverRestartNV
1142 _mesa_compile_error(ctx
, GL_INVALID_OPERATION
,
1143 "glPrimitiveRestartNV called outside glBegin/End");
1145 /* get current primitive mode */
1146 GLenum curPrim
= save
->prims
[save
->prim_count
- 1].mode
;
1148 /* restart primitive */
1149 CALL_End(GET_DISPATCH(), ());
1150 vbo_save_NotifyBegin(ctx
, curPrim
);
1155 /* Unlike the functions above, these are to be hooked into the vtxfmt
1156 * maintained in ctx->ListState, active when the list is known or
1157 * suspected to be outside any begin/end primitive.
1158 * Note: OBE = Outside Begin/End
1160 static void GLAPIENTRY
1161 _save_OBE_Rectf(GLfloat x1
, GLfloat y1
, GLfloat x2
, GLfloat y2
)
1163 GET_CURRENT_CONTEXT(ctx
);
1164 vbo_save_NotifyBegin(ctx
, GL_QUADS
| VBO_SAVE_PRIM_WEAK
);
1165 CALL_Vertex2f(GET_DISPATCH(), (x1
, y1
));
1166 CALL_Vertex2f(GET_DISPATCH(), (x2
, y1
));
1167 CALL_Vertex2f(GET_DISPATCH(), (x2
, y2
));
1168 CALL_Vertex2f(GET_DISPATCH(), (x1
, y2
));
1169 CALL_End(GET_DISPATCH(), ());
1173 static void GLAPIENTRY
1174 _save_OBE_DrawArrays(GLenum mode
, GLint start
, GLsizei count
)
1176 GET_CURRENT_CONTEXT(ctx
);
1177 struct vbo_save_context
*save
= &vbo_context(ctx
)->save
;
1180 if (!_mesa_is_valid_prim_mode(ctx
, mode
)) {
1181 _mesa_compile_error(ctx
, GL_INVALID_ENUM
, "glDrawArrays(mode)");
1185 _mesa_compile_error(ctx
, GL_INVALID_VALUE
, "glDrawArrays(count<0)");
1189 if (save
->out_of_memory
)
1192 /* Make sure to process any VBO binding changes */
1193 _mesa_update_state(ctx
);
1197 vbo_save_NotifyBegin(ctx
, (mode
| VBO_SAVE_PRIM_WEAK
1198 | VBO_SAVE_PRIM_NO_CURRENT_UPDATE
));
1200 for (i
= 0; i
< count
; i
++)
1201 CALL_ArrayElement(GET_DISPATCH(), (start
+ i
));
1202 CALL_End(GET_DISPATCH(), ());
1204 _ae_unmap_vbos(ctx
);
1208 static void GLAPIENTRY
1209 _save_OBE_MultiDrawArrays(GLenum mode
, const GLint
*first
,
1210 const GLsizei
*count
, GLsizei primcount
)
1212 GET_CURRENT_CONTEXT(ctx
);
1215 if (!_mesa_is_valid_prim_mode(ctx
, mode
)) {
1216 _mesa_compile_error(ctx
, GL_INVALID_ENUM
, "glMultiDrawArrays(mode)");
1220 if (primcount
< 0) {
1221 _mesa_compile_error(ctx
, GL_INVALID_VALUE
,
1222 "glMultiDrawArrays(primcount<0)");
1226 for (i
= 0; i
< primcount
; i
++) {
1228 _mesa_compile_error(ctx
, GL_INVALID_VALUE
,
1229 "glMultiDrawArrays(count[i]<0)");
1234 for (i
= 0; i
< primcount
; i
++) {
1236 _save_OBE_DrawArrays(mode
, first
[i
], count
[i
]);
1242 /* Could do better by copying the arrays and element list intact and
1243 * then emitting an indexed prim at runtime.
1245 static void GLAPIENTRY
1246 _save_OBE_DrawElementsBaseVertex(GLenum mode
, GLsizei count
, GLenum type
,
1247 const GLvoid
* indices
, GLint basevertex
)
1249 GET_CURRENT_CONTEXT(ctx
);
1250 struct vbo_save_context
*save
= &vbo_context(ctx
)->save
;
1251 struct gl_buffer_object
*indexbuf
= ctx
->Array
.VAO
->IndexBufferObj
;
1254 if (!_mesa_is_valid_prim_mode(ctx
, mode
)) {
1255 _mesa_compile_error(ctx
, GL_INVALID_ENUM
, "glDrawElements(mode)");
1259 _mesa_compile_error(ctx
, GL_INVALID_VALUE
, "glDrawElements(count<0)");
1262 if (type
!= GL_UNSIGNED_BYTE
&&
1263 type
!= GL_UNSIGNED_SHORT
&&
1264 type
!= GL_UNSIGNED_INT
) {
1265 _mesa_compile_error(ctx
, GL_INVALID_VALUE
, "glDrawElements(count<0)");
1269 if (save
->out_of_memory
)
1272 /* Make sure to process any VBO binding changes */
1273 _mesa_update_state(ctx
);
1277 if (_mesa_is_bufferobj(indexbuf
))
1279 ADD_POINTERS(indexbuf
->Mappings
[MAP_INTERNAL
].Pointer
, indices
);
1281 vbo_save_NotifyBegin(ctx
, (mode
| VBO_SAVE_PRIM_WEAK
|
1282 VBO_SAVE_PRIM_NO_CURRENT_UPDATE
));
1285 case GL_UNSIGNED_BYTE
:
1286 for (i
= 0; i
< count
; i
++)
1287 CALL_ArrayElement(GET_DISPATCH(), (basevertex
+ ((GLubyte
*) indices
)[i
]));
1289 case GL_UNSIGNED_SHORT
:
1290 for (i
= 0; i
< count
; i
++)
1291 CALL_ArrayElement(GET_DISPATCH(), (basevertex
+ ((GLushort
*) indices
)[i
]));
1293 case GL_UNSIGNED_INT
:
1294 for (i
= 0; i
< count
; i
++)
1295 CALL_ArrayElement(GET_DISPATCH(), (basevertex
+ ((GLuint
*) indices
)[i
]));
1298 _mesa_error(ctx
, GL_INVALID_ENUM
, "glDrawElements(type)");
1302 CALL_End(GET_DISPATCH(), ());
1304 _ae_unmap_vbos(ctx
);
1307 static void GLAPIENTRY
1308 _save_OBE_DrawElements(GLenum mode
, GLsizei count
, GLenum type
,
1309 const GLvoid
* indices
)
1311 _save_OBE_DrawElementsBaseVertex(mode
, count
, type
, indices
, 0);
1315 static void GLAPIENTRY
1316 _save_OBE_DrawRangeElements(GLenum mode
, GLuint start
, GLuint end
,
1317 GLsizei count
, GLenum type
,
1318 const GLvoid
* indices
)
1320 GET_CURRENT_CONTEXT(ctx
);
1321 struct vbo_save_context
*save
= &vbo_context(ctx
)->save
;
1323 if (!_mesa_is_valid_prim_mode(ctx
, mode
)) {
1324 _mesa_compile_error(ctx
, GL_INVALID_ENUM
, "glDrawRangeElements(mode)");
1328 _mesa_compile_error(ctx
, GL_INVALID_VALUE
,
1329 "glDrawRangeElements(count<0)");
1332 if (type
!= GL_UNSIGNED_BYTE
&&
1333 type
!= GL_UNSIGNED_SHORT
&&
1334 type
!= GL_UNSIGNED_INT
) {
1335 _mesa_compile_error(ctx
, GL_INVALID_ENUM
, "glDrawRangeElements(type)");
1339 _mesa_compile_error(ctx
, GL_INVALID_VALUE
,
1340 "glDrawRangeElements(end < start)");
1344 if (save
->out_of_memory
)
1347 _save_OBE_DrawElements(mode
, count
, type
, indices
);
1351 static void GLAPIENTRY
1352 _save_OBE_MultiDrawElements(GLenum mode
, const GLsizei
*count
, GLenum type
,
1353 const GLvoid
* const *indices
, GLsizei primcount
)
1357 for (i
= 0; i
< primcount
; i
++) {
1359 CALL_DrawElements(GET_DISPATCH(), (mode
, count
[i
], type
, indices
[i
]));
1365 static void GLAPIENTRY
1366 _save_OBE_MultiDrawElementsBaseVertex(GLenum mode
, const GLsizei
*count
,
1368 const GLvoid
* const *indices
,
1370 const GLint
*basevertex
)
1374 for (i
= 0; i
< primcount
; i
++) {
1376 CALL_DrawElementsBaseVertex(GET_DISPATCH(), (mode
, count
[i
], type
,
1385 vtxfmt_init(struct gl_context
*ctx
)
1387 struct vbo_save_context
*save
= &vbo_context(ctx
)->save
;
1388 GLvertexformat
*vfmt
= &save
->vtxfmt
;
1390 vfmt
->ArrayElement
= _ae_ArrayElement
;
1392 vfmt
->Color3f
= _save_Color3f
;
1393 vfmt
->Color3fv
= _save_Color3fv
;
1394 vfmt
->Color4f
= _save_Color4f
;
1395 vfmt
->Color4fv
= _save_Color4fv
;
1396 vfmt
->EdgeFlag
= _save_EdgeFlag
;
1397 vfmt
->End
= _save_End
;
1398 vfmt
->PrimitiveRestartNV
= _save_PrimitiveRestartNV
;
1399 vfmt
->FogCoordfEXT
= _save_FogCoordfEXT
;
1400 vfmt
->FogCoordfvEXT
= _save_FogCoordfvEXT
;
1401 vfmt
->Indexf
= _save_Indexf
;
1402 vfmt
->Indexfv
= _save_Indexfv
;
1403 vfmt
->Materialfv
= _save_Materialfv
;
1404 vfmt
->MultiTexCoord1fARB
= _save_MultiTexCoord1f
;
1405 vfmt
->MultiTexCoord1fvARB
= _save_MultiTexCoord1fv
;
1406 vfmt
->MultiTexCoord2fARB
= _save_MultiTexCoord2f
;
1407 vfmt
->MultiTexCoord2fvARB
= _save_MultiTexCoord2fv
;
1408 vfmt
->MultiTexCoord3fARB
= _save_MultiTexCoord3f
;
1409 vfmt
->MultiTexCoord3fvARB
= _save_MultiTexCoord3fv
;
1410 vfmt
->MultiTexCoord4fARB
= _save_MultiTexCoord4f
;
1411 vfmt
->MultiTexCoord4fvARB
= _save_MultiTexCoord4fv
;
1412 vfmt
->Normal3f
= _save_Normal3f
;
1413 vfmt
->Normal3fv
= _save_Normal3fv
;
1414 vfmt
->SecondaryColor3fEXT
= _save_SecondaryColor3fEXT
;
1415 vfmt
->SecondaryColor3fvEXT
= _save_SecondaryColor3fvEXT
;
1416 vfmt
->TexCoord1f
= _save_TexCoord1f
;
1417 vfmt
->TexCoord1fv
= _save_TexCoord1fv
;
1418 vfmt
->TexCoord2f
= _save_TexCoord2f
;
1419 vfmt
->TexCoord2fv
= _save_TexCoord2fv
;
1420 vfmt
->TexCoord3f
= _save_TexCoord3f
;
1421 vfmt
->TexCoord3fv
= _save_TexCoord3fv
;
1422 vfmt
->TexCoord4f
= _save_TexCoord4f
;
1423 vfmt
->TexCoord4fv
= _save_TexCoord4fv
;
1424 vfmt
->Vertex2f
= _save_Vertex2f
;
1425 vfmt
->Vertex2fv
= _save_Vertex2fv
;
1426 vfmt
->Vertex3f
= _save_Vertex3f
;
1427 vfmt
->Vertex3fv
= _save_Vertex3fv
;
1428 vfmt
->Vertex4f
= _save_Vertex4f
;
1429 vfmt
->Vertex4fv
= _save_Vertex4fv
;
1430 vfmt
->VertexAttrib1fARB
= _save_VertexAttrib1fARB
;
1431 vfmt
->VertexAttrib1fvARB
= _save_VertexAttrib1fvARB
;
1432 vfmt
->VertexAttrib2fARB
= _save_VertexAttrib2fARB
;
1433 vfmt
->VertexAttrib2fvARB
= _save_VertexAttrib2fvARB
;
1434 vfmt
->VertexAttrib3fARB
= _save_VertexAttrib3fARB
;
1435 vfmt
->VertexAttrib3fvARB
= _save_VertexAttrib3fvARB
;
1436 vfmt
->VertexAttrib4fARB
= _save_VertexAttrib4fARB
;
1437 vfmt
->VertexAttrib4fvARB
= _save_VertexAttrib4fvARB
;
1439 vfmt
->VertexAttrib1fNV
= _save_VertexAttrib1fNV
;
1440 vfmt
->VertexAttrib1fvNV
= _save_VertexAttrib1fvNV
;
1441 vfmt
->VertexAttrib2fNV
= _save_VertexAttrib2fNV
;
1442 vfmt
->VertexAttrib2fvNV
= _save_VertexAttrib2fvNV
;
1443 vfmt
->VertexAttrib3fNV
= _save_VertexAttrib3fNV
;
1444 vfmt
->VertexAttrib3fvNV
= _save_VertexAttrib3fvNV
;
1445 vfmt
->VertexAttrib4fNV
= _save_VertexAttrib4fNV
;
1446 vfmt
->VertexAttrib4fvNV
= _save_VertexAttrib4fvNV
;
1448 /* integer-valued */
1449 vfmt
->VertexAttribI1i
= _save_VertexAttribI1i
;
1450 vfmt
->VertexAttribI2i
= _save_VertexAttribI2i
;
1451 vfmt
->VertexAttribI3i
= _save_VertexAttribI3i
;
1452 vfmt
->VertexAttribI4i
= _save_VertexAttribI4i
;
1453 vfmt
->VertexAttribI2iv
= _save_VertexAttribI2iv
;
1454 vfmt
->VertexAttribI3iv
= _save_VertexAttribI3iv
;
1455 vfmt
->VertexAttribI4iv
= _save_VertexAttribI4iv
;
1457 /* unsigned integer-valued */
1458 vfmt
->VertexAttribI1ui
= _save_VertexAttribI1ui
;
1459 vfmt
->VertexAttribI2ui
= _save_VertexAttribI2ui
;
1460 vfmt
->VertexAttribI3ui
= _save_VertexAttribI3ui
;
1461 vfmt
->VertexAttribI4ui
= _save_VertexAttribI4ui
;
1462 vfmt
->VertexAttribI2uiv
= _save_VertexAttribI2uiv
;
1463 vfmt
->VertexAttribI3uiv
= _save_VertexAttribI3uiv
;
1464 vfmt
->VertexAttribI4uiv
= _save_VertexAttribI4uiv
;
1466 vfmt
->VertexP2ui
= _save_VertexP2ui
;
1467 vfmt
->VertexP3ui
= _save_VertexP3ui
;
1468 vfmt
->VertexP4ui
= _save_VertexP4ui
;
1469 vfmt
->VertexP2uiv
= _save_VertexP2uiv
;
1470 vfmt
->VertexP3uiv
= _save_VertexP3uiv
;
1471 vfmt
->VertexP4uiv
= _save_VertexP4uiv
;
1473 vfmt
->TexCoordP1ui
= _save_TexCoordP1ui
;
1474 vfmt
->TexCoordP2ui
= _save_TexCoordP2ui
;
1475 vfmt
->TexCoordP3ui
= _save_TexCoordP3ui
;
1476 vfmt
->TexCoordP4ui
= _save_TexCoordP4ui
;
1477 vfmt
->TexCoordP1uiv
= _save_TexCoordP1uiv
;
1478 vfmt
->TexCoordP2uiv
= _save_TexCoordP2uiv
;
1479 vfmt
->TexCoordP3uiv
= _save_TexCoordP3uiv
;
1480 vfmt
->TexCoordP4uiv
= _save_TexCoordP4uiv
;
1482 vfmt
->MultiTexCoordP1ui
= _save_MultiTexCoordP1ui
;
1483 vfmt
->MultiTexCoordP2ui
= _save_MultiTexCoordP2ui
;
1484 vfmt
->MultiTexCoordP3ui
= _save_MultiTexCoordP3ui
;
1485 vfmt
->MultiTexCoordP4ui
= _save_MultiTexCoordP4ui
;
1486 vfmt
->MultiTexCoordP1uiv
= _save_MultiTexCoordP1uiv
;
1487 vfmt
->MultiTexCoordP2uiv
= _save_MultiTexCoordP2uiv
;
1488 vfmt
->MultiTexCoordP3uiv
= _save_MultiTexCoordP3uiv
;
1489 vfmt
->MultiTexCoordP4uiv
= _save_MultiTexCoordP4uiv
;
1491 vfmt
->NormalP3ui
= _save_NormalP3ui
;
1492 vfmt
->NormalP3uiv
= _save_NormalP3uiv
;
1494 vfmt
->ColorP3ui
= _save_ColorP3ui
;
1495 vfmt
->ColorP4ui
= _save_ColorP4ui
;
1496 vfmt
->ColorP3uiv
= _save_ColorP3uiv
;
1497 vfmt
->ColorP4uiv
= _save_ColorP4uiv
;
1499 vfmt
->SecondaryColorP3ui
= _save_SecondaryColorP3ui
;
1500 vfmt
->SecondaryColorP3uiv
= _save_SecondaryColorP3uiv
;
1502 vfmt
->VertexAttribP1ui
= _save_VertexAttribP1ui
;
1503 vfmt
->VertexAttribP2ui
= _save_VertexAttribP2ui
;
1504 vfmt
->VertexAttribP3ui
= _save_VertexAttribP3ui
;
1505 vfmt
->VertexAttribP4ui
= _save_VertexAttribP4ui
;
1507 vfmt
->VertexAttribP1uiv
= _save_VertexAttribP1uiv
;
1508 vfmt
->VertexAttribP2uiv
= _save_VertexAttribP2uiv
;
1509 vfmt
->VertexAttribP3uiv
= _save_VertexAttribP3uiv
;
1510 vfmt
->VertexAttribP4uiv
= _save_VertexAttribP4uiv
;
1512 vfmt
->VertexAttribL1d
= _save_VertexAttribL1d
;
1513 vfmt
->VertexAttribL2d
= _save_VertexAttribL2d
;
1514 vfmt
->VertexAttribL3d
= _save_VertexAttribL3d
;
1515 vfmt
->VertexAttribL4d
= _save_VertexAttribL4d
;
1517 vfmt
->VertexAttribL1dv
= _save_VertexAttribL1dv
;
1518 vfmt
->VertexAttribL2dv
= _save_VertexAttribL2dv
;
1519 vfmt
->VertexAttribL3dv
= _save_VertexAttribL3dv
;
1520 vfmt
->VertexAttribL4dv
= _save_VertexAttribL4dv
;
1522 vfmt
->VertexAttribL1ui64ARB
= _save_VertexAttribL1ui64ARB
;
1523 vfmt
->VertexAttribL1ui64vARB
= _save_VertexAttribL1ui64vARB
;
1525 /* This will all require us to fallback to saving the list as opcodes:
1527 vfmt
->CallList
= _save_CallList
;
1528 vfmt
->CallLists
= _save_CallLists
;
1530 vfmt
->EvalCoord1f
= _save_EvalCoord1f
;
1531 vfmt
->EvalCoord1fv
= _save_EvalCoord1fv
;
1532 vfmt
->EvalCoord2f
= _save_EvalCoord2f
;
1533 vfmt
->EvalCoord2fv
= _save_EvalCoord2fv
;
1534 vfmt
->EvalPoint1
= _save_EvalPoint1
;
1535 vfmt
->EvalPoint2
= _save_EvalPoint2
;
1537 /* These calls all generate GL_INVALID_OPERATION since this vtxfmt is
1538 * only used when we're inside a glBegin/End pair.
1540 vfmt
->Begin
= _save_Begin
;
1545 * Initialize the dispatch table with the VBO functions for display
1549 vbo_initialize_save_dispatch(const struct gl_context
*ctx
,
1550 struct _glapi_table
*exec
)
1552 SET_DrawArrays(exec
, _save_OBE_DrawArrays
);
1553 SET_MultiDrawArrays(exec
, _save_OBE_MultiDrawArrays
);
1554 SET_DrawElements(exec
, _save_OBE_DrawElements
);
1555 SET_DrawElementsBaseVertex(exec
, _save_OBE_DrawElementsBaseVertex
);
1556 SET_DrawRangeElements(exec
, _save_OBE_DrawRangeElements
);
1557 SET_MultiDrawElementsEXT(exec
, _save_OBE_MultiDrawElements
);
1558 SET_MultiDrawElementsBaseVertex(exec
, _save_OBE_MultiDrawElementsBaseVertex
);
1559 SET_Rectf(exec
, _save_OBE_Rectf
);
1560 /* Note: other glDraw functins aren't compiled into display lists */
1566 vbo_save_SaveFlushVertices(struct gl_context
*ctx
)
1568 struct vbo_save_context
*save
= &vbo_context(ctx
)->save
;
1570 /* Noop when we are actually active:
1572 if (ctx
->Driver
.CurrentSavePrimitive
<= PRIM_MAX
)
1575 if (save
->vert_count
|| save
->prim_count
)
1576 compile_vertex_list(ctx
);
1578 copy_to_current(ctx
);
1580 reset_counters(ctx
);
1581 ctx
->Driver
.SaveNeedFlush
= GL_FALSE
;
1586 * Called from glNewList when we're starting to compile a display list.
1589 vbo_save_NewList(struct gl_context
*ctx
, GLuint list
, GLenum mode
)
1591 struct vbo_save_context
*save
= &vbo_context(ctx
)->save
;
1596 if (!save
->prim_store
)
1597 save
->prim_store
= alloc_prim_store();
1599 if (!save
->vertex_store
)
1600 save
->vertex_store
= alloc_vertex_store(ctx
);
1602 save
->buffer_ptr
= vbo_save_map_vertex_store(ctx
, save
->vertex_store
);
1605 reset_counters(ctx
);
1606 ctx
->Driver
.SaveNeedFlush
= GL_FALSE
;
1611 * Called from glEndList when we're finished compiling a display list.
1614 vbo_save_EndList(struct gl_context
*ctx
)
1616 struct vbo_save_context
*save
= &vbo_context(ctx
)->save
;
1618 /* EndList called inside a (saved) Begin/End pair?
1620 if (_mesa_inside_dlist_begin_end(ctx
)) {
1621 if (save
->prim_count
> 0) {
1622 GLint i
= save
->prim_count
- 1;
1623 ctx
->Driver
.CurrentSavePrimitive
= PRIM_OUTSIDE_BEGIN_END
;
1624 save
->prims
[i
].end
= 0;
1625 save
->prims
[i
].count
= save
->vert_count
- save
->prims
[i
].start
;
1628 /* Make sure this vertex list gets replayed by the "loopback"
1631 save
->dangling_attr_ref
= GL_TRUE
;
1632 vbo_save_SaveFlushVertices(ctx
);
1634 /* Swap out this vertex format while outside begin/end. Any color,
1635 * etc. received between here and the next begin will be compiled
1638 _mesa_install_save_vtxfmt(ctx
, &ctx
->ListState
.ListVtxfmt
);
1641 vbo_save_unmap_vertex_store(ctx
, save
->vertex_store
);
1643 assert(save
->vertex_size
== 0);
1648 * Called from the display list code when we're about to execute a
1652 vbo_save_BeginCallList(struct gl_context
*ctx
, struct gl_display_list
*dlist
)
1654 struct vbo_save_context
*save
= &vbo_context(ctx
)->save
;
1655 save
->replay_flags
|= dlist
->Flags
;
1660 * Called from the display list code when we're finished executing a
1664 vbo_save_EndCallList(struct gl_context
*ctx
)
1666 struct vbo_save_context
*save
= &vbo_context(ctx
)->save
;
1668 if (ctx
->ListState
.CallDepth
== 1) {
1669 /* This is correct: want to keep only the VBO_SAVE_FALLBACK
1670 * flag, if it is set:
1672 save
->replay_flags
&= VBO_SAVE_FALLBACK
;
1678 * Called by display list code when a display list is being deleted.
1681 vbo_destroy_vertex_list(struct gl_context
*ctx
, void *data
)
1683 struct vbo_save_vertex_list
*node
= (struct vbo_save_vertex_list
*) data
;
1686 if (--node
->vertex_store
->refcount
== 0)
1687 free_vertex_store(ctx
, node
->vertex_store
);
1689 if (--node
->prim_store
->refcount
== 0)
1690 free(node
->prim_store
);
1692 free(node
->current_data
);
1693 node
->current_data
= NULL
;
1698 vbo_print_vertex_list(struct gl_context
*ctx
, void *data
, FILE *f
)
1700 struct vbo_save_vertex_list
*node
= (struct vbo_save_vertex_list
*) data
;
1702 struct gl_buffer_object
*buffer
= node
->vertex_store
?
1703 node
->vertex_store
->bufferobj
: NULL
;
1706 fprintf(f
, "VBO-VERTEX-LIST, %u vertices, %d primitives, %d vertsize, "
1708 node
->vertex_count
, node
->prim_count
, node
->vertex_size
,
1711 for (i
= 0; i
< node
->prim_count
; i
++) {
1712 struct _mesa_prim
*prim
= &node
->prims
[i
];
1713 fprintf(f
, " prim %d: %s%s %d..%d %s %s\n",
1715 _mesa_lookup_prim_by_nr(prim
->mode
),
1716 prim
->weak
? " (weak)" : "",
1718 prim
->start
+ prim
->count
,
1719 (prim
->begin
) ? "BEGIN" : "(wrap)",
1720 (prim
->end
) ? "END" : "(wrap)");
1726 * Called during context creation/init.
1729 current_init(struct gl_context
*ctx
)
1731 struct vbo_save_context
*save
= &vbo_context(ctx
)->save
;
1734 for (i
= VBO_ATTRIB_POS
; i
<= VBO_ATTRIB_GENERIC15
; i
++) {
1735 const GLuint j
= i
- VBO_ATTRIB_POS
;
1736 assert(j
< VERT_ATTRIB_MAX
);
1737 save
->currentsz
[i
] = &ctx
->ListState
.ActiveAttribSize
[j
];
1738 save
->current
[i
] = (fi_type
*) ctx
->ListState
.CurrentAttrib
[j
];
1741 for (i
= VBO_ATTRIB_FIRST_MATERIAL
; i
<= VBO_ATTRIB_LAST_MATERIAL
; i
++) {
1742 const GLuint j
= i
- VBO_ATTRIB_FIRST_MATERIAL
;
1743 assert(j
< MAT_ATTRIB_MAX
);
1744 save
->currentsz
[i
] = &ctx
->ListState
.ActiveMaterialSize
[j
];
1745 save
->current
[i
] = (fi_type
*) ctx
->ListState
.CurrentMaterial
[j
];
1751 * Initialize the display list compiler. Called during context creation.
1754 vbo_save_api_init(struct vbo_save_context
*save
)
1756 struct gl_context
*ctx
= save
->ctx
;
1759 save
->opcode_vertex_list
=
1760 _mesa_dlist_alloc_opcode(ctx
,
1761 sizeof(struct vbo_save_vertex_list
),
1762 vbo_save_playback_vertex_list
,
1763 vbo_destroy_vertex_list
,
1764 vbo_print_vertex_list
);
1768 _mesa_noop_vtxfmt_init(&save
->vtxfmt_noop
);
1770 /* These will actually get set again when binding/drawing */
1771 for (i
= 0; i
< VBO_ATTRIB_MAX
; i
++)
1772 save
->inputs
[i
] = &save
->arrays
[i
];