1 /**************************************************************************
3 Copyright 2002 Tungsten Graphics Inc., Cedar Park, Texas.
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 TUNGSTEN GRAPHICS AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
22 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23 OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24 USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
30 * Keith Whitwell <keith@tungstengraphics.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.
75 #include "api_validate.h"
76 #include "api_arrayelt.h"
80 #include "vbo_context.h"
89 * NOTE: Old 'parity' issue is gone, but copying can still be
90 * wrong-footed on replay.
92 static GLuint
_save_copy_vertices( GLcontext
*ctx
,
93 const struct vbo_save_vertex_list
*node
,
94 const GLfloat
*src_buffer
)
96 struct vbo_save_context
*save
= &vbo_context( ctx
)->save
;
97 const struct _mesa_prim
*prim
= &node
->prim
[node
->prim_count
-1];
98 GLuint nr
= prim
->count
;
99 GLuint sz
= save
->vertex_size
;
100 const GLfloat
*src
= src_buffer
+ prim
->start
* sz
;
101 GLfloat
*dst
= save
->copied
.buffer
;
113 for (i
= 0 ; i
< ovf
; i
++)
114 _mesa_memcpy( dst
+i
*sz
, src
+(nr
-ovf
+i
)*sz
, sz
*sizeof(GLfloat
) );
118 for (i
= 0 ; i
< ovf
; i
++)
119 _mesa_memcpy( dst
+i
*sz
, src
+(nr
-ovf
+i
)*sz
, sz
*sizeof(GLfloat
) );
123 for (i
= 0 ; i
< ovf
; i
++)
124 _mesa_memcpy( dst
+i
*sz
, src
+(nr
-ovf
+i
)*sz
, sz
*sizeof(GLfloat
) );
130 _mesa_memcpy( dst
, src
+(nr
-1)*sz
, sz
*sizeof(GLfloat
) );
134 case GL_TRIANGLE_FAN
:
139 _mesa_memcpy( dst
, src
+0, sz
*sizeof(GLfloat
) );
142 _mesa_memcpy( dst
, src
+0, sz
*sizeof(GLfloat
) );
143 _mesa_memcpy( dst
+sz
, src
+(nr
-1)*sz
, sz
*sizeof(GLfloat
) );
146 case GL_TRIANGLE_STRIP
:
149 case 0: ovf
= 0; break;
150 case 1: ovf
= 1; break;
151 default: ovf
= 2 + (nr
&1); break;
153 for (i
= 0 ; i
< ovf
; i
++)
154 _mesa_memcpy( dst
+i
*sz
, src
+(nr
-ovf
+i
)*sz
, sz
*sizeof(GLfloat
) );
163 static struct vbo_save_vertex_store
*alloc_vertex_store( GLcontext
*ctx
)
165 struct vbo_save_vertex_store
*vertex_store
= CALLOC_STRUCT(vbo_save_vertex_store
);
167 /* obj->Name needs to be non-zero, but won't ever be examined more
168 * closely than that. In particular these buffers won't be entered
169 * into the hash and can never be confused with ones visible to the
170 * user. Perhaps there could be a special number for internal
173 vertex_store
->bufferobj
= ctx
->Driver
.NewBufferObject(ctx
, 1, GL_ARRAY_BUFFER_ARB
);
175 ctx
->Driver
.BufferData( ctx
,
177 VBO_SAVE_BUFFER_SIZE
* sizeof(GLfloat
),
180 vertex_store
->bufferobj
);
182 vertex_store
->buffer
= NULL
;
183 vertex_store
->used
= 0;
184 vertex_store
->refcount
= 1;
189 static void free_vertex_store( GLcontext
*ctx
, struct vbo_save_vertex_store
*vertex_store
)
191 assert(!vertex_store
->buffer
);
193 if (vertex_store
->bufferobj
)
194 ctx
->Driver
.DeleteBuffer( ctx
, vertex_store
->bufferobj
);
196 FREE( vertex_store
);
199 static GLfloat
*map_vertex_store( GLcontext
*ctx
, struct vbo_save_vertex_store
*vertex_store
)
201 assert(vertex_store
->bufferobj
);
202 assert(!vertex_store
->buffer
);
203 vertex_store
->buffer
= (GLfloat
*)ctx
->Driver
.MapBuffer(ctx
,
204 GL_ARRAY_BUFFER_ARB
, /* not used */
205 GL_WRITE_ONLY
, /* not used */
206 vertex_store
->bufferobj
);
208 assert(vertex_store
->buffer
);
209 return vertex_store
->buffer
+ vertex_store
->used
;
212 static void unmap_vertex_store( GLcontext
*ctx
, struct vbo_save_vertex_store
*vertex_store
)
214 ctx
->Driver
.UnmapBuffer( ctx
, GL_ARRAY_BUFFER_ARB
, vertex_store
->bufferobj
);
215 vertex_store
->buffer
= NULL
;
219 static struct vbo_save_primitive_store
*alloc_prim_store( GLcontext
*ctx
)
221 struct vbo_save_primitive_store
*store
= CALLOC_STRUCT(vbo_save_primitive_store
);
228 static void _save_reset_counters( GLcontext
*ctx
)
230 struct vbo_save_context
*save
= &vbo_context(ctx
)->save
;
232 save
->prim
= save
->prim_store
->buffer
+ save
->prim_store
->used
;
233 save
->buffer
= (save
->vertex_store
->buffer
+
234 save
->vertex_store
->used
);
236 assert(save
->buffer
== save
->vbptr
);
238 if (save
->vertex_size
)
239 save
->max_vert
= ((VBO_SAVE_BUFFER_SIZE
- save
->vertex_store
->used
) /
244 save
->vert_count
= 0;
245 save
->prim_count
= 0;
246 save
->prim_max
= VBO_SAVE_PRIM_SIZE
- save
->prim_store
->used
;
247 save
->dangling_attr_ref
= 0;
251 /* Insert the active immediate struct onto the display list currently
254 static void _save_compile_vertex_list( GLcontext
*ctx
)
256 struct vbo_save_context
*save
= &vbo_context(ctx
)->save
;
257 struct vbo_save_vertex_list
*node
;
259 /* Allocate space for this structure in the display list currently
262 node
= (struct vbo_save_vertex_list
*)
263 _mesa_alloc_instruction(ctx
, save
->opcode_vertex_list
, sizeof(*node
));
268 /* Duplicate our template, increment refcounts to the storage structs:
270 _mesa_memcpy(node
->attrsz
, save
->attrsz
, sizeof(node
->attrsz
));
271 node
->vertex_size
= save
->vertex_size
;
272 node
->buffer_offset
= (save
->buffer
- save
->vertex_store
->buffer
) * sizeof(GLfloat
);
273 node
->count
= save
->vert_count
;
274 node
->wrap_count
= save
->copied
.nr
;
275 node
->dangling_attr_ref
= save
->dangling_attr_ref
;
276 node
->prim
= save
->prim
;
277 node
->prim_count
= save
->prim_count
;
278 node
->vertex_store
= save
->vertex_store
;
279 node
->prim_store
= save
->prim_store
;
281 node
->vertex_store
->refcount
++;
282 node
->prim_store
->refcount
++;
284 assert(node
->attrsz
[VBO_ATTRIB_POS
] != 0 ||
287 if (save
->dangling_attr_ref
)
288 ctx
->ListState
.CurrentList
->flags
|= MESA_DLIST_DANGLING_REFS
;
290 save
->vertex_store
->used
+= save
->vertex_size
* node
->count
;
291 save
->prim_store
->used
+= node
->prim_count
;
294 /* Copy duplicated vertices
296 save
->copied
.nr
= _save_copy_vertices( ctx
, node
, save
->buffer
);
299 /* Deal with GL_COMPILE_AND_EXECUTE:
301 if (ctx
->ExecuteFlag
) {
302 struct _glapi_table
*dispatch
= GET_DISPATCH();
304 _glapi_set_dispatch(ctx
->Exec
);
306 vbo_loopback_vertex_list( ctx
,
307 (const GLfloat
*)((const char *)save
->vertex_store
->buffer
+
308 node
->buffer_offset
),
315 _glapi_set_dispatch(dispatch
);
319 /* Decide whether the storage structs are full, or can be used for
320 * the next vertex lists as well.
322 if (save
->vertex_store
->used
>
323 VBO_SAVE_BUFFER_SIZE
- 16 * (save
->vertex_size
+ 4)) {
327 unmap_vertex_store( ctx
, save
->vertex_store
);
329 /* Release old reference:
331 save
->vertex_store
->refcount
--;
332 assert(save
->vertex_store
->refcount
!= 0);
333 save
->vertex_store
= NULL
;
335 /* Allocate and map new store:
337 save
->vertex_store
= alloc_vertex_store( ctx
);
338 save
->vbptr
= map_vertex_store( ctx
, save
->vertex_store
);
341 if (save
->prim_store
->used
> VBO_SAVE_PRIM_SIZE
- 6) {
342 save
->prim_store
->refcount
--;
343 assert(save
->prim_store
->refcount
!= 0);
344 save
->prim_store
= alloc_prim_store( ctx
);
347 /* Reset our structures for the next run of vertices:
349 _save_reset_counters( ctx
);
353 /* TODO -- If no new vertices have been stored, don't bother saving
356 static void _save_wrap_buffers( GLcontext
*ctx
)
358 struct vbo_save_context
*save
= &vbo_context(ctx
)->save
;
359 GLint i
= save
->prim_count
- 1;
363 assert(i
< (GLint
) save
->prim_max
);
366 /* Close off in-progress primitive.
368 save
->prim
[i
].count
= (save
->vert_count
-
369 save
->prim
[i
].start
);
370 mode
= save
->prim
[i
].mode
;
371 weak
= save
->prim
[i
].weak
;
373 /* store the copied vertices, and allocate a new list.
375 _save_compile_vertex_list( ctx
);
377 /* Restart interrupted primitive
379 save
->prim
[0].mode
= mode
;
380 save
->prim
[0].weak
= weak
;
381 save
->prim
[0].begin
= 0;
382 save
->prim
[0].end
= 0;
383 save
->prim
[0].pad
= 0;
384 save
->prim
[0].start
= 0;
385 save
->prim
[0].count
= 0;
386 save
->prim_count
= 1;
391 /* Called only when buffers are wrapped as the result of filling the
392 * vertex_store struct.
394 static void _save_wrap_filled_vertex( GLcontext
*ctx
)
396 struct vbo_save_context
*save
= &vbo_context(ctx
)->save
;
397 GLfloat
*data
= save
->copied
.buffer
;
400 /* Emit a glEnd to close off the last vertex list.
402 _save_wrap_buffers( ctx
);
404 /* Copy stored stored vertices to start of new list.
406 assert(save
->max_vert
- save
->vert_count
> save
->copied
.nr
);
408 for (i
= 0 ; i
< save
->copied
.nr
; i
++) {
409 _mesa_memcpy( save
->vbptr
, data
, save
->vertex_size
* sizeof(GLfloat
));
410 data
+= save
->vertex_size
;
411 save
->vbptr
+= save
->vertex_size
;
417 static void _save_copy_to_current( GLcontext
*ctx
)
419 struct vbo_save_context
*save
= &vbo_context(ctx
)->save
;
422 for (i
= VBO_ATTRIB_POS
+1 ; i
< VBO_ATTRIB_MAX
; i
++) {
423 if (save
->attrsz
[i
]) {
424 save
->currentsz
[i
][0] = save
->attrsz
[i
];
425 COPY_CLEAN_4V(save
->current
[i
],
433 static void _save_copy_from_current( GLcontext
*ctx
)
435 struct vbo_save_context
*save
= &vbo_context(ctx
)->save
;
438 for (i
= VBO_ATTRIB_POS
+1 ; i
< VBO_ATTRIB_MAX
; i
++) {
439 switch (save
->attrsz
[i
]) {
440 case 4: save
->attrptr
[i
][3] = save
->current
[i
][3];
441 case 3: save
->attrptr
[i
][2] = save
->current
[i
][2];
442 case 2: save
->attrptr
[i
][1] = save
->current
[i
][1];
443 case 1: save
->attrptr
[i
][0] = save
->current
[i
][0];
452 /* Flush existing data, set new attrib size, replay copied vertices.
454 static void _save_upgrade_vertex( GLcontext
*ctx
,
458 struct vbo_save_context
*save
= &vbo_context(ctx
)->save
;
463 /* Store the current run of vertices, and emit a GL_END. Emit a
464 * BEGIN in the new buffer.
466 if (save
->vert_count
)
467 _save_wrap_buffers( ctx
);
469 assert( save
->copied
.nr
== 0 );
471 /* Do a COPY_TO_CURRENT to ensure back-copying works for the case
472 * when the attribute already exists in the vertex and is having
473 * its size increased.
475 _save_copy_to_current( ctx
);
479 oldsz
= save
->attrsz
[attr
];
480 save
->attrsz
[attr
] = newsz
;
482 save
->vertex_size
+= newsz
- oldsz
;
483 save
->max_vert
= ((VBO_SAVE_BUFFER_SIZE
- save
->vertex_store
->used
) /
485 save
->vert_count
= 0;
487 /* Recalculate all the attrptr[] values:
489 for (i
= 0, tmp
= save
->vertex
; i
< VBO_ATTRIB_MAX
; i
++) {
490 if (save
->attrsz
[i
]) {
491 save
->attrptr
[i
] = tmp
;
492 tmp
+= save
->attrsz
[i
];
495 save
->attrptr
[i
] = NULL
; /* will not be dereferenced. */
498 /* Copy from current to repopulate the vertex with correct values.
500 _save_copy_from_current( ctx
);
502 /* Replay stored vertices to translate them to new format here.
504 * If there are copied vertices and the new (upgraded) attribute
505 * has not been defined before, this list is somewhat degenerate,
506 * and will need fixup at runtime.
510 GLfloat
*data
= save
->copied
.buffer
;
511 GLfloat
*dest
= save
->buffer
;
514 /* Need to note this and fix up at runtime (or loopback):
516 if (attr
!= VBO_ATTRIB_POS
&& save
->currentsz
[attr
][0] == 0) {
518 save
->dangling_attr_ref
= GL_TRUE
;
521 for (i
= 0 ; i
< save
->copied
.nr
; i
++) {
522 for (j
= 0 ; j
< VBO_ATTRIB_MAX
; j
++) {
523 if (save
->attrsz
[j
]) {
526 COPY_CLEAN_4V( dest
, oldsz
, data
);
531 COPY_SZ_4V( dest
, newsz
, save
->current
[attr
] );
536 GLint sz
= save
->attrsz
[j
];
537 COPY_SZ_4V( dest
, sz
, data
);
546 save
->vert_count
+= save
->copied
.nr
;
550 static void save_fixup_vertex( GLcontext
*ctx
, GLuint attr
, GLuint sz
)
552 struct vbo_save_context
*save
= &vbo_context(ctx
)->save
;
554 if (sz
> save
->attrsz
[attr
]) {
555 /* New size is larger. Need to flush existing vertices and get
556 * an enlarged vertex format.
558 _save_upgrade_vertex( ctx
, attr
, sz
);
560 else if (sz
< save
->active_sz
[attr
]) {
561 static GLfloat id
[4] = { 0, 0, 0, 1 };
564 /* New size is equal or smaller - just need to fill in some
567 for (i
= sz
; i
<= save
->attrsz
[attr
] ; i
++)
568 save
->attrptr
[attr
][i
-1] = id
[i
-1];
571 save
->active_sz
[attr
] = sz
;
574 static void _save_reset_vertex( GLcontext
*ctx
)
576 struct vbo_save_context
*save
= &vbo_context(ctx
)->save
;
579 for (i
= 0 ; i
< VBO_ATTRIB_MAX
; i
++) {
581 save
->active_sz
[i
] = 0;
584 save
->vertex_size
= 0;
589 #define ERROR() _mesa_compile_error( ctx, GL_INVALID_ENUM, __FUNCTION__ );
592 /* Only one size for each attribute may be active at once. Eg. if
593 * Color3f is installed/active, then Color4f may not be, even if the
594 * vertex actually contains 4 color coordinates. This is because the
595 * 3f version won't otherwise set color[3] to 1.0 -- this is the job
596 * of the chooser function when switching between Color4f and Color3f.
598 #define ATTR( A, N, V0, V1, V2, V3 ) \
600 struct vbo_save_context *save = &vbo_context(ctx)->save; \
602 if (save->active_sz[A] != N) \
603 save_fixup_vertex(ctx, A, N); \
606 GLfloat *dest = save->attrptr[A]; \
607 if (N>0) dest[0] = V0; \
608 if (N>1) dest[1] = V1; \
609 if (N>2) dest[2] = V2; \
610 if (N>3) dest[3] = V3; \
616 for (i = 0; i < save->vertex_size; i++) \
617 save->vbptr[i] = save->vertex[i]; \
619 save->vbptr += save->vertex_size; \
621 if (++save->vert_count >= save->max_vert) \
622 _save_wrap_filled_vertex( ctx ); \
626 #define TAG(x) _save_##x
628 #include "vbo_attrib_tmp.h"
633 /* Cope with EvalCoord/CallList called within a begin/end object:
634 * -- Flush current buffer
635 * -- Fallback to opcodes for the rest of the begin/end object.
637 #define DO_FALLBACK(ctx) \
639 struct vbo_save_context *save = &vbo_context(ctx)->save; \
641 if (save->vert_count || save->prim_count) \
642 _save_compile_vertex_list( ctx ); \
644 _save_copy_to_current( ctx ); \
645 _save_reset_vertex( ctx ); \
646 _save_reset_counters( ctx ); \
647 _mesa_install_save_vtxfmt( ctx, &ctx->ListState.ListVtxfmt ); \
648 ctx->Driver.SaveNeedFlush = 0; \
651 static void GLAPIENTRY
_save_EvalCoord1f( GLfloat u
)
653 GET_CURRENT_CONTEXT(ctx
);
655 ctx
->Save
->EvalCoord1f( u
);
658 static void GLAPIENTRY
_save_EvalCoord1fv( const GLfloat
*v
)
660 GET_CURRENT_CONTEXT(ctx
);
662 ctx
->Save
->EvalCoord1fv( v
);
665 static void GLAPIENTRY
_save_EvalCoord2f( GLfloat u
, GLfloat v
)
667 GET_CURRENT_CONTEXT(ctx
);
669 ctx
->Save
->EvalCoord2f( u
, v
);
672 static void GLAPIENTRY
_save_EvalCoord2fv( const GLfloat
*v
)
674 GET_CURRENT_CONTEXT(ctx
);
676 ctx
->Save
->EvalCoord2fv( v
);
679 static void GLAPIENTRY
_save_EvalPoint1( GLint i
)
681 GET_CURRENT_CONTEXT(ctx
);
683 ctx
->Save
->EvalPoint1( i
);
686 static void GLAPIENTRY
_save_EvalPoint2( GLint i
, GLint j
)
688 GET_CURRENT_CONTEXT(ctx
);
690 ctx
->Save
->EvalPoint2( i
, j
);
693 static void GLAPIENTRY
_save_CallList( GLuint l
)
695 GET_CURRENT_CONTEXT(ctx
);
697 ctx
->Save
->CallList( l
);
700 static void GLAPIENTRY
_save_CallLists( GLsizei n
, GLenum type
, const GLvoid
*v
)
702 GET_CURRENT_CONTEXT(ctx
);
704 ctx
->Save
->CallLists( n
, type
, v
);
710 /* This begin is hooked into ... Updating of
711 * ctx->Driver.CurrentSavePrimitive is already taken care of.
713 GLboolean
vbo_save_NotifyBegin( GLcontext
*ctx
, GLenum mode
)
715 struct vbo_save_context
*save
= &vbo_context(ctx
)->save
;
717 GLuint i
= save
->prim_count
++;
719 assert(i
< save
->prim_max
);
720 save
->prim
[i
].mode
= mode
& ~VBO_SAVE_PRIM_WEAK
;
721 save
->prim
[i
].begin
= 1;
722 save
->prim
[i
].end
= 0;
723 save
->prim
[i
].weak
= (mode
& VBO_SAVE_PRIM_WEAK
) ? 1 : 0;
724 save
->prim
[i
].pad
= 0;
725 save
->prim
[i
].start
= save
->vert_count
;
726 save
->prim
[i
].count
= 0;
728 _mesa_install_save_vtxfmt( ctx
, &save
->vtxfmt
);
729 ctx
->Driver
.SaveNeedFlush
= 1;
735 static void GLAPIENTRY
_save_End( void )
737 GET_CURRENT_CONTEXT( ctx
);
738 struct vbo_save_context
*save
= &vbo_context(ctx
)->save
;
739 GLint i
= save
->prim_count
- 1;
741 ctx
->Driver
.CurrentSavePrimitive
= PRIM_OUTSIDE_BEGIN_END
;
742 save
->prim
[i
].end
= 1;
743 save
->prim
[i
].count
= (save
->vert_count
-
744 save
->prim
[i
].start
);
746 if (i
== (GLint
) save
->prim_max
- 1) {
747 _save_compile_vertex_list( ctx
);
748 assert(save
->copied
.nr
== 0);
751 /* Swap out this vertex format while outside begin/end. Any color,
752 * etc. received between here and the next begin will be compiled
755 _mesa_install_save_vtxfmt( ctx
, &ctx
->ListState
.ListVtxfmt
);
759 /* These are all errors as this vtxfmt is only installed inside
762 static void GLAPIENTRY
_save_DrawElements(GLenum mode
, GLsizei count
, GLenum type
,
763 const GLvoid
*indices
)
765 GET_CURRENT_CONTEXT(ctx
);
766 (void) mode
; (void) count
; (void) type
; (void) indices
;
767 _mesa_compile_error( ctx
, GL_INVALID_OPERATION
, "glDrawElements" );
771 static void GLAPIENTRY
_save_DrawRangeElements(GLenum mode
,
772 GLuint start
, GLuint end
,
773 GLsizei count
, GLenum type
,
774 const GLvoid
*indices
)
776 GET_CURRENT_CONTEXT(ctx
);
777 (void) mode
; (void) start
; (void) end
; (void) count
; (void) type
; (void) indices
;
778 _mesa_compile_error( ctx
, GL_INVALID_OPERATION
, "glDrawRangeElements" );
781 static void GLAPIENTRY
_save_DrawArrays(GLenum mode
, GLint start
, GLsizei count
)
783 GET_CURRENT_CONTEXT(ctx
);
784 (void) mode
; (void) start
; (void) count
;
785 _mesa_compile_error( ctx
, GL_INVALID_OPERATION
, "glDrawArrays" );
788 static void GLAPIENTRY
_save_Rectf( GLfloat x1
, GLfloat y1
, GLfloat x2
, GLfloat y2
)
790 GET_CURRENT_CONTEXT(ctx
);
791 (void) x1
; (void) y1
; (void) x2
; (void) y2
;
792 _mesa_compile_error( ctx
, GL_INVALID_OPERATION
, "glRectf" );
795 static void GLAPIENTRY
_save_EvalMesh1( GLenum mode
, GLint i1
, GLint i2
)
797 GET_CURRENT_CONTEXT(ctx
);
798 (void) mode
; (void) i1
; (void) i2
;
799 _mesa_compile_error( ctx
, GL_INVALID_OPERATION
, "glEvalMesh1" );
802 static void GLAPIENTRY
_save_EvalMesh2( GLenum mode
, GLint i1
, GLint i2
,
805 GET_CURRENT_CONTEXT(ctx
);
806 (void) mode
; (void) i1
; (void) i2
; (void) j1
; (void) j2
;
807 _mesa_compile_error( ctx
, GL_INVALID_OPERATION
, "glEvalMesh2" );
810 static void GLAPIENTRY
_save_Begin( GLenum mode
)
812 GET_CURRENT_CONTEXT( ctx
);
814 _mesa_compile_error( ctx
, GL_INVALID_OPERATION
, "Recursive glBegin" );
818 /* Unlike the functions above, these are to be hooked into the vtxfmt
819 * maintained in ctx->ListState, active when the list is known or
820 * suspected to be outside any begin/end primitive.
822 static void GLAPIENTRY
_save_OBE_Rectf( GLfloat x1
, GLfloat y1
, GLfloat x2
, GLfloat y2
)
824 GET_CURRENT_CONTEXT(ctx
);
825 vbo_save_NotifyBegin( ctx
, GL_QUADS
| VBO_SAVE_PRIM_WEAK
);
826 CALL_Vertex2f(GET_DISPATCH(), ( x1
, y1
));
827 CALL_Vertex2f(GET_DISPATCH(), ( x2
, y1
));
828 CALL_Vertex2f(GET_DISPATCH(), ( x2
, y2
));
829 CALL_Vertex2f(GET_DISPATCH(), ( x1
, y2
));
830 CALL_End(GET_DISPATCH(), ());
834 static void GLAPIENTRY
_save_OBE_DrawArrays(GLenum mode
, GLint start
, GLsizei count
)
836 GET_CURRENT_CONTEXT(ctx
);
839 if (!_mesa_validate_DrawArrays( ctx
, mode
, start
, count
))
844 vbo_save_NotifyBegin( ctx
, mode
| VBO_SAVE_PRIM_WEAK
);
846 for (i
= 0; i
< count
; i
++)
847 CALL_ArrayElement(GET_DISPATCH(), (start
+ i
));
848 CALL_End(GET_DISPATCH(), ());
850 _ae_unmap_vbos( ctx
);
853 /* Could do better by copying the arrays and element list intact and
854 * then emitting an indexed prim at runtime.
856 static void GLAPIENTRY
_save_OBE_DrawElements(GLenum mode
, GLsizei count
, GLenum type
,
857 const GLvoid
*indices
)
859 GET_CURRENT_CONTEXT(ctx
);
862 if (!_mesa_validate_DrawElements( ctx
, mode
, count
, type
, indices
))
867 vbo_save_NotifyBegin( ctx
, mode
| VBO_SAVE_PRIM_WEAK
);
870 case GL_UNSIGNED_BYTE
:
871 for (i
= 0 ; i
< count
; i
++)
872 CALL_ArrayElement(GET_DISPATCH(), ( ((GLubyte
*)indices
)[i
] ));
874 case GL_UNSIGNED_SHORT
:
875 for (i
= 0 ; i
< count
; i
++)
876 CALL_ArrayElement(GET_DISPATCH(), ( ((GLushort
*)indices
)[i
] ));
878 case GL_UNSIGNED_INT
:
879 for (i
= 0 ; i
< count
; i
++)
880 CALL_ArrayElement(GET_DISPATCH(), ( ((GLuint
*)indices
)[i
] ));
883 _mesa_error( ctx
, GL_INVALID_ENUM
, "glDrawElements(type)" );
887 CALL_End(GET_DISPATCH(), ());
889 _ae_unmap_vbos( ctx
);
892 static void GLAPIENTRY
_save_OBE_DrawRangeElements(GLenum mode
,
893 GLuint start
, GLuint end
,
894 GLsizei count
, GLenum type
,
895 const GLvoid
*indices
)
897 GET_CURRENT_CONTEXT(ctx
);
898 if (_mesa_validate_DrawRangeElements( ctx
, mode
,
900 count
, type
, indices
))
901 _save_OBE_DrawElements( mode
, count
, type
, indices
);
908 static void _save_vtxfmt_init( GLcontext
*ctx
)
910 struct vbo_save_context
*save
= &vbo_context(ctx
)->save
;
911 GLvertexformat
*vfmt
= &save
->vtxfmt
;
913 vfmt
->ArrayElement
= _ae_loopback_array_elt
; /* generic helper */
914 vfmt
->Begin
= _save_Begin
;
915 vfmt
->Color3f
= _save_Color3f
;
916 vfmt
->Color3fv
= _save_Color3fv
;
917 vfmt
->Color4f
= _save_Color4f
;
918 vfmt
->Color4fv
= _save_Color4fv
;
919 vfmt
->EdgeFlag
= _save_EdgeFlag
;
920 vfmt
->End
= _save_End
;
921 vfmt
->FogCoordfEXT
= _save_FogCoordfEXT
;
922 vfmt
->FogCoordfvEXT
= _save_FogCoordfvEXT
;
923 vfmt
->Indexf
= _save_Indexf
;
924 vfmt
->Indexfv
= _save_Indexfv
;
925 vfmt
->Materialfv
= _save_Materialfv
;
926 vfmt
->MultiTexCoord1fARB
= _save_MultiTexCoord1f
;
927 vfmt
->MultiTexCoord1fvARB
= _save_MultiTexCoord1fv
;
928 vfmt
->MultiTexCoord2fARB
= _save_MultiTexCoord2f
;
929 vfmt
->MultiTexCoord2fvARB
= _save_MultiTexCoord2fv
;
930 vfmt
->MultiTexCoord3fARB
= _save_MultiTexCoord3f
;
931 vfmt
->MultiTexCoord3fvARB
= _save_MultiTexCoord3fv
;
932 vfmt
->MultiTexCoord4fARB
= _save_MultiTexCoord4f
;
933 vfmt
->MultiTexCoord4fvARB
= _save_MultiTexCoord4fv
;
934 vfmt
->Normal3f
= _save_Normal3f
;
935 vfmt
->Normal3fv
= _save_Normal3fv
;
936 vfmt
->SecondaryColor3fEXT
= _save_SecondaryColor3fEXT
;
937 vfmt
->SecondaryColor3fvEXT
= _save_SecondaryColor3fvEXT
;
938 vfmt
->TexCoord1f
= _save_TexCoord1f
;
939 vfmt
->TexCoord1fv
= _save_TexCoord1fv
;
940 vfmt
->TexCoord2f
= _save_TexCoord2f
;
941 vfmt
->TexCoord2fv
= _save_TexCoord2fv
;
942 vfmt
->TexCoord3f
= _save_TexCoord3f
;
943 vfmt
->TexCoord3fv
= _save_TexCoord3fv
;
944 vfmt
->TexCoord4f
= _save_TexCoord4f
;
945 vfmt
->TexCoord4fv
= _save_TexCoord4fv
;
946 vfmt
->Vertex2f
= _save_Vertex2f
;
947 vfmt
->Vertex2fv
= _save_Vertex2fv
;
948 vfmt
->Vertex3f
= _save_Vertex3f
;
949 vfmt
->Vertex3fv
= _save_Vertex3fv
;
950 vfmt
->Vertex4f
= _save_Vertex4f
;
951 vfmt
->Vertex4fv
= _save_Vertex4fv
;
952 vfmt
->VertexAttrib1fARB
= _save_VertexAttrib1fARB
;
953 vfmt
->VertexAttrib1fvARB
= _save_VertexAttrib1fvARB
;
954 vfmt
->VertexAttrib2fARB
= _save_VertexAttrib2fARB
;
955 vfmt
->VertexAttrib2fvARB
= _save_VertexAttrib2fvARB
;
956 vfmt
->VertexAttrib3fARB
= _save_VertexAttrib3fARB
;
957 vfmt
->VertexAttrib3fvARB
= _save_VertexAttrib3fvARB
;
958 vfmt
->VertexAttrib4fARB
= _save_VertexAttrib4fARB
;
959 vfmt
->VertexAttrib4fvARB
= _save_VertexAttrib4fvARB
;
961 vfmt
->VertexAttrib1fNV
= _save_VertexAttrib1fNV
;
962 vfmt
->VertexAttrib1fvNV
= _save_VertexAttrib1fvNV
;
963 vfmt
->VertexAttrib2fNV
= _save_VertexAttrib2fNV
;
964 vfmt
->VertexAttrib2fvNV
= _save_VertexAttrib2fvNV
;
965 vfmt
->VertexAttrib3fNV
= _save_VertexAttrib3fNV
;
966 vfmt
->VertexAttrib3fvNV
= _save_VertexAttrib3fvNV
;
967 vfmt
->VertexAttrib4fNV
= _save_VertexAttrib4fNV
;
968 vfmt
->VertexAttrib4fvNV
= _save_VertexAttrib4fvNV
;
970 /* This will all require us to fallback to saving the list as opcodes:
972 vfmt
->CallList
= _save_CallList
; /* inside begin/end */
973 vfmt
->CallLists
= _save_CallLists
; /* inside begin/end */
974 vfmt
->EvalCoord1f
= _save_EvalCoord1f
;
975 vfmt
->EvalCoord1fv
= _save_EvalCoord1fv
;
976 vfmt
->EvalCoord2f
= _save_EvalCoord2f
;
977 vfmt
->EvalCoord2fv
= _save_EvalCoord2fv
;
978 vfmt
->EvalPoint1
= _save_EvalPoint1
;
979 vfmt
->EvalPoint2
= _save_EvalPoint2
;
981 /* These are all errors as we at least know we are in some sort of
984 vfmt
->EvalMesh1
= _save_EvalMesh1
;
985 vfmt
->EvalMesh2
= _save_EvalMesh2
;
986 vfmt
->Begin
= _save_Begin
;
987 vfmt
->Rectf
= _save_Rectf
;
988 vfmt
->DrawArrays
= _save_DrawArrays
;
989 vfmt
->DrawElements
= _save_DrawElements
;
990 vfmt
->DrawRangeElements
= _save_DrawRangeElements
;
995 void vbo_save_SaveFlushVertices( GLcontext
*ctx
)
997 struct vbo_save_context
*save
= &vbo_context(ctx
)->save
;
999 /* Noop when we are actually active:
1001 if (ctx
->Driver
.CurrentSavePrimitive
== PRIM_INSIDE_UNKNOWN_PRIM
||
1002 ctx
->Driver
.CurrentSavePrimitive
<= GL_POLYGON
)
1005 if (save
->vert_count
||
1007 _save_compile_vertex_list( ctx
);
1009 _save_copy_to_current( ctx
);
1010 _save_reset_vertex( ctx
);
1011 _save_reset_counters( ctx
);
1012 ctx
->Driver
.SaveNeedFlush
= 0;
1015 void vbo_save_NewList( GLcontext
*ctx
, GLuint list
, GLenum mode
)
1017 struct vbo_save_context
*save
= &vbo_context(ctx
)->save
;
1019 (void) list
; (void) mode
;
1021 if (!save
->prim_store
)
1022 save
->prim_store
= alloc_prim_store( ctx
);
1024 if (!save
->vertex_store
)
1025 save
->vertex_store
= alloc_vertex_store( ctx
);
1027 save
->vbptr
= map_vertex_store( ctx
, save
->vertex_store
);
1029 _save_reset_vertex( ctx
);
1030 _save_reset_counters( ctx
);
1031 ctx
->Driver
.SaveNeedFlush
= 0;
1034 void vbo_save_EndList( GLcontext
*ctx
)
1036 struct vbo_save_context
*save
= &vbo_context(ctx
)->save
;
1037 unmap_vertex_store( ctx
, save
->vertex_store
);
1039 assert(save
->vertex_size
== 0);
1042 void vbo_save_BeginCallList( GLcontext
*ctx
, struct mesa_display_list
*dlist
)
1044 struct vbo_save_context
*save
= &vbo_context(ctx
)->save
;
1045 save
->replay_flags
|= dlist
->flags
;
1048 void vbo_save_EndCallList( GLcontext
*ctx
)
1050 struct vbo_save_context
*save
= &vbo_context(ctx
)->save
;
1052 if (ctx
->ListState
.CallDepth
== 1) {
1053 /* This is correct: want to keep only the VBO_SAVE_FALLBACK
1054 * flag, if it is set:
1056 save
->replay_flags
&= VBO_SAVE_FALLBACK
;
1061 static void vbo_destroy_vertex_list( GLcontext
*ctx
, void *data
)
1063 struct vbo_save_vertex_list
*node
= (struct vbo_save_vertex_list
*)data
;
1066 if ( --node
->vertex_store
->refcount
== 0 )
1067 free_vertex_store( ctx
, node
->vertex_store
);
1069 if ( --node
->prim_store
->refcount
== 0 )
1070 FREE( node
->prim_store
);
1074 static void vbo_print_vertex_list( GLcontext
*ctx
, void *data
)
1076 struct vbo_save_vertex_list
*node
= (struct vbo_save_vertex_list
*)data
;
1080 _mesa_debug(NULL
, "VBO-VERTEX-LIST, %u vertices %d primitives, %d vertsize\n",
1085 for (i
= 0 ; i
< node
->prim_count
; i
++) {
1086 struct _mesa_prim
*prim
= &node
->prim
[i
];
1087 _mesa_debug(NULL
, " prim %d: %s%s %d..%d %s %s\n",
1089 _mesa_lookup_enum_by_nr(prim
->mode
),
1090 prim
->weak
? " (weak)" : "",
1092 prim
->start
+ prim
->count
,
1093 (prim
->begin
) ? "BEGIN" : "(wrap)",
1094 (prim
->end
) ? "END" : "(wrap)");
1099 static void _save_current_init( GLcontext
*ctx
)
1101 struct vbo_save_context
*save
= &vbo_context(ctx
)->save
;
1104 for (i
= VBO_ATTRIB_POS
; i
<= VBO_ATTRIB_GENERIC15
; i
++) {
1105 const GLuint j
= i
- VBO_ATTRIB_POS
;
1106 ASSERT(j
< VERT_ATTRIB_MAX
);
1107 save
->currentsz
[i
] = &ctx
->ListState
.ActiveAttribSize
[j
];
1108 save
->current
[i
] = ctx
->ListState
.CurrentAttrib
[j
];
1111 for (i
= VBO_ATTRIB_FIRST_MATERIAL
; i
<= VBO_ATTRIB_LAST_MATERIAL
; i
++) {
1112 const GLuint j
= i
- VBO_ATTRIB_FIRST_MATERIAL
;
1113 ASSERT(j
< MAT_ATTRIB_MAX
);
1114 save
->currentsz
[i
] = &ctx
->ListState
.ActiveMaterialSize
[j
];
1115 save
->current
[i
] = ctx
->ListState
.CurrentMaterial
[j
];
1120 * Initialize the display list compiler
1122 void vbo_save_api_init( struct vbo_save_context
*save
)
1124 GLcontext
*ctx
= save
->ctx
;
1127 save
->opcode_vertex_list
=
1128 _mesa_alloc_opcode( ctx
,
1129 sizeof(struct vbo_save_vertex_list
),
1130 vbo_save_playback_vertex_list
,
1131 vbo_destroy_vertex_list
,
1132 vbo_print_vertex_list
);
1134 ctx
->Driver
.NotifySaveBegin
= vbo_save_NotifyBegin
;
1136 _save_vtxfmt_init( ctx
);
1137 _save_current_init( ctx
);
1139 for (i
= 0; i
< VBO_ATTRIB_MAX
; i
++)
1140 save
->inputs
[i
] = &save
->arrays
[i
];
1142 /* Hook our array functions into the outside-begin-end vtxfmt in
1145 ctx
->ListState
.ListVtxfmt
.Rectf
= _save_OBE_Rectf
;
1146 ctx
->ListState
.ListVtxfmt
.DrawArrays
= _save_OBE_DrawArrays
;
1147 ctx
->ListState
.ListVtxfmt
.DrawElements
= _save_OBE_DrawElements
;
1148 ctx
->ListState
.ListVtxfmt
.DrawRangeElements
= _save_OBE_DrawRangeElements
;
1149 _mesa_install_save_vtxfmt( ctx
, &ctx
->ListState
.ListVtxfmt
);