vbo: deduplicate copy_vertices functions
[mesa.git] / src / mesa / vbo / vbo_save_api.c
1 /**************************************************************************
2
3 Copyright 2002-2008 VMware, Inc.
4
5 All Rights Reserved.
6
7 Permission is hereby granted, free of charge, to any person obtaining a
8 copy of this software and associated documentation files (the "Software"),
9 to deal in the Software without restriction, including without limitation
10 on the rights to use, copy, modify, merge, publish, distribute, sub
11 license, and/or sell copies of the Software, and to permit persons to whom
12 the Software is furnished to do so, subject to the following conditions:
13
14 The above copyright notice and this permission notice (including the next
15 paragraph) shall be included in all copies or substantial portions of the
16 Software.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21 VMWARE AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
22 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23 OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24 USE OR OTHER DEALINGS IN THE SOFTWARE.
25
26 **************************************************************************/
27
28 /*
29 * Authors:
30 * Keith Whitwell <keithw@vmware.com>
31 */
32
33
34
35 /* Display list compiler attempts to store lists of vertices with the
36 * same vertex layout. Additionally it attempts to minimize the need
37 * for execute-time fixup of these vertex lists, allowing them to be
38 * cached on hardware.
39 *
40 * There are still some circumstances where this can be thwarted, for
41 * example by building a list that consists of one very long primitive
42 * (eg Begin(Triangles), 1000 vertices, End), and calling that list
43 * from inside a different begin/end object (Begin(Lines), CallList,
44 * End).
45 *
46 * In that case the code will have to replay the list as individual
47 * commands through the Exec dispatch table, or fix up the copied
48 * vertices at execute-time.
49 *
50 * The other case where fixup is required is when a vertex attribute
51 * is introduced in the middle of a primitive. Eg:
52 * Begin(Lines)
53 * TexCoord1f() Vertex2f()
54 * TexCoord1f() Color3f() Vertex2f()
55 * End()
56 *
57 * If the current value of Color isn't known at compile-time, this
58 * primitive will require fixup.
59 *
60 *
61 * The list compiler currently doesn't attempt to compile lists
62 * containing EvalCoord or EvalPoint commands. On encountering one of
63 * these, compilation falls back to opcodes.
64 *
65 * This could be improved to fallback only when a mix of EvalCoord and
66 * Vertex commands are issued within a single primitive.
67 */
68
69
70 #include "main/glheader.h"
71 #include "main/arrayobj.h"
72 #include "main/bufferobj.h"
73 #include "main/context.h"
74 #include "main/dlist.h"
75 #include "main/enums.h"
76 #include "main/eval.h"
77 #include "main/macros.h"
78 #include "main/draw_validate.h"
79 #include "main/api_arrayelt.h"
80 #include "main/vtxfmt.h"
81 #include "main/dispatch.h"
82 #include "main/state.h"
83 #include "main/varray.h"
84 #include "util/bitscan.h"
85
86 #include "vbo_noop.h"
87 #include "vbo_private.h"
88
89
90 #ifdef ERROR
91 #undef ERROR
92 #endif
93
94 /**
95 * Display list flag only used by this VBO code.
96 */
97 #define DLIST_DANGLING_REFS 0x1
98
99
100 /* An interesting VBO number/name to help with debugging */
101 #define VBO_BUF_ID 12345
102
103
104 /*
105 * NOTE: Old 'parity' issue is gone, but copying can still be
106 * wrong-footed on replay.
107 */
108 static GLuint
109 copy_vertices(struct gl_context *ctx,
110 const struct vbo_save_vertex_list *node,
111 const fi_type * src_buffer)
112 {
113 struct vbo_save_context *save = &vbo_context(ctx)->save;
114 struct _mesa_prim *prim = &node->prims[node->prim_count - 1];
115 GLuint sz = save->vertex_size;
116 const fi_type *src = src_buffer + prim->start * sz;
117 fi_type *dst = save->copied.buffer;
118
119 if (prim->end)
120 return 0;
121
122 return vbo_copy_vertices(ctx, prim->mode, prim, sz, true, dst, src);
123 }
124
125
126 static struct vbo_save_vertex_store *
127 alloc_vertex_store(struct gl_context *ctx)
128 {
129 struct vbo_save_context *save = &vbo_context(ctx)->save;
130 struct vbo_save_vertex_store *vertex_store =
131 CALLOC_STRUCT(vbo_save_vertex_store);
132
133 /* obj->Name needs to be non-zero, but won't ever be examined more
134 * closely than that. In particular these buffers won't be entered
135 * into the hash and can never be confused with ones visible to the
136 * user. Perhaps there could be a special number for internal
137 * buffers:
138 */
139 vertex_store->bufferobj = ctx->Driver.NewBufferObject(ctx, VBO_BUF_ID);
140 if (vertex_store->bufferobj) {
141 save->out_of_memory =
142 !ctx->Driver.BufferData(ctx,
143 GL_ARRAY_BUFFER_ARB,
144 VBO_SAVE_BUFFER_SIZE * sizeof(GLfloat),
145 NULL, GL_STATIC_DRAW_ARB,
146 GL_MAP_WRITE_BIT |
147 GL_DYNAMIC_STORAGE_BIT,
148 vertex_store->bufferobj);
149 }
150 else {
151 save->out_of_memory = GL_TRUE;
152 }
153
154 if (save->out_of_memory) {
155 _mesa_error(ctx, GL_OUT_OF_MEMORY, "internal VBO allocation");
156 _mesa_install_save_vtxfmt(ctx, &save->vtxfmt_noop);
157 }
158
159 vertex_store->buffer_map = NULL;
160 vertex_store->used = 0;
161
162 return vertex_store;
163 }
164
165
166 static void
167 free_vertex_store(struct gl_context *ctx,
168 struct vbo_save_vertex_store *vertex_store)
169 {
170 assert(!vertex_store->buffer_map);
171
172 if (vertex_store->bufferobj) {
173 _mesa_reference_buffer_object(ctx, &vertex_store->bufferobj, NULL);
174 }
175
176 free(vertex_store);
177 }
178
179
180 fi_type *
181 vbo_save_map_vertex_store(struct gl_context *ctx,
182 struct vbo_save_vertex_store *vertex_store)
183 {
184 const GLbitfield access = (GL_MAP_WRITE_BIT |
185 GL_MAP_INVALIDATE_RANGE_BIT |
186 GL_MAP_UNSYNCHRONIZED_BIT |
187 GL_MAP_FLUSH_EXPLICIT_BIT);
188
189 assert(vertex_store->bufferobj);
190 assert(!vertex_store->buffer_map); /* the buffer should not be mapped */
191
192 if (vertex_store->bufferobj->Size > 0) {
193 /* Map the remaining free space in the VBO */
194 GLintptr offset = vertex_store->used * sizeof(GLfloat);
195 GLsizeiptr size = vertex_store->bufferobj->Size - offset;
196 fi_type *range = (fi_type *)
197 ctx->Driver.MapBufferRange(ctx, offset, size, access,
198 vertex_store->bufferobj,
199 MAP_INTERNAL);
200 if (range) {
201 /* compute address of start of whole buffer (needed elsewhere) */
202 vertex_store->buffer_map = range - vertex_store->used;
203 assert(vertex_store->buffer_map);
204 return range;
205 }
206 else {
207 vertex_store->buffer_map = NULL;
208 return NULL;
209 }
210 }
211 else {
212 /* probably ran out of memory for buffers */
213 return NULL;
214 }
215 }
216
217
218 void
219 vbo_save_unmap_vertex_store(struct gl_context *ctx,
220 struct vbo_save_vertex_store *vertex_store)
221 {
222 if (vertex_store->bufferobj->Size > 0) {
223 GLintptr offset = 0;
224 GLsizeiptr length = vertex_store->used * sizeof(GLfloat)
225 - vertex_store->bufferobj->Mappings[MAP_INTERNAL].Offset;
226
227 /* Explicitly flush the region we wrote to */
228 ctx->Driver.FlushMappedBufferRange(ctx, offset, length,
229 vertex_store->bufferobj,
230 MAP_INTERNAL);
231
232 ctx->Driver.UnmapBuffer(ctx, vertex_store->bufferobj, MAP_INTERNAL);
233 }
234 vertex_store->buffer_map = NULL;
235 }
236
237
238 static struct vbo_save_primitive_store *
239 alloc_prim_store(void)
240 {
241 struct vbo_save_primitive_store *store =
242 CALLOC_STRUCT(vbo_save_primitive_store);
243 store->used = 0;
244 store->refcount = 1;
245 return store;
246 }
247
248
249 static void
250 reset_counters(struct gl_context *ctx)
251 {
252 struct vbo_save_context *save = &vbo_context(ctx)->save;
253
254 save->prims = save->prim_store->prims + save->prim_store->used;
255 save->buffer_map = save->vertex_store->buffer_map + save->vertex_store->used;
256
257 assert(save->buffer_map == save->buffer_ptr);
258
259 if (save->vertex_size)
260 save->max_vert = (VBO_SAVE_BUFFER_SIZE - save->vertex_store->used) /
261 save->vertex_size;
262 else
263 save->max_vert = 0;
264
265 save->vert_count = 0;
266 save->prim_count = 0;
267 save->prim_max = VBO_SAVE_PRIM_SIZE - save->prim_store->used;
268 save->dangling_attr_ref = GL_FALSE;
269 }
270
271 /**
272 * For a list of prims, try merging prims that can just be extensions of the
273 * previous prim.
274 */
275 static void
276 merge_prims(struct _mesa_prim *prim_list,
277 GLuint *prim_count)
278 {
279 GLuint i;
280 struct _mesa_prim *prev_prim = prim_list;
281
282 for (i = 1; i < *prim_count; i++) {
283 struct _mesa_prim *this_prim = prim_list + i;
284
285 vbo_try_prim_conversion(this_prim);
286
287 if (vbo_can_merge_prims(prev_prim, this_prim)) {
288 /* We've found a prim that just extend the previous one. Tack it
289 * onto the previous one, and let this primitive struct get dropped.
290 */
291 vbo_merge_prims(prev_prim, this_prim);
292 continue;
293 }
294
295 /* If any previous primitives have been dropped, then we need to copy
296 * this later one into the next available slot.
297 */
298 prev_prim++;
299 if (prev_prim != this_prim)
300 *prev_prim = *this_prim;
301 }
302
303 *prim_count = prev_prim - prim_list + 1;
304 }
305
306
307 /**
308 * Convert GL_LINE_LOOP primitive into GL_LINE_STRIP so that drivers
309 * don't have to worry about handling the _mesa_prim::begin/end flags.
310 * See https://bugs.freedesktop.org/show_bug.cgi?id=81174
311 */
312 static void
313 convert_line_loop_to_strip(struct vbo_save_context *save,
314 struct vbo_save_vertex_list *node)
315 {
316 struct _mesa_prim *prim = &node->prims[node->prim_count - 1];
317
318 assert(prim->mode == GL_LINE_LOOP);
319
320 if (prim->end) {
321 /* Copy the 0th vertex to end of the buffer and extend the
322 * vertex count by one to finish the line loop.
323 */
324 const GLuint sz = save->vertex_size;
325 /* 0th vertex: */
326 const fi_type *src = save->buffer_map + prim->start * sz;
327 /* end of buffer: */
328 fi_type *dst = save->buffer_map + (prim->start + prim->count) * sz;
329
330 memcpy(dst, src, sz * sizeof(float));
331
332 prim->count++;
333 node->vertex_count++;
334 save->vert_count++;
335 save->buffer_ptr += sz;
336 save->vertex_store->used += sz;
337 }
338
339 if (!prim->begin) {
340 /* Drawing the second or later section of a long line loop.
341 * Skip the 0th vertex.
342 */
343 prim->start++;
344 prim->count--;
345 }
346
347 prim->mode = GL_LINE_STRIP;
348 }
349
350
351 /* Compare the present vao if it has the same setup. */
352 static bool
353 compare_vao(gl_vertex_processing_mode mode,
354 const struct gl_vertex_array_object *vao,
355 const struct gl_buffer_object *bo, GLintptr buffer_offset,
356 GLuint stride, GLbitfield64 vao_enabled,
357 const GLubyte size[VBO_ATTRIB_MAX],
358 const GLenum16 type[VBO_ATTRIB_MAX],
359 const GLuint offset[VBO_ATTRIB_MAX])
360 {
361 if (!vao)
362 return false;
363
364 /* If the enabled arrays are not the same we are not equal. */
365 if (vao_enabled != vao->Enabled)
366 return false;
367
368 /* Check the buffer binding at 0 */
369 if (vao->BufferBinding[0].BufferObj != bo)
370 return false;
371 /* BufferBinding[0].Offset != buffer_offset is checked per attribute */
372 if (vao->BufferBinding[0].Stride != stride)
373 return false;
374 assert(vao->BufferBinding[0].InstanceDivisor == 0);
375
376 /* Retrieve the mapping from VBO_ATTRIB to VERT_ATTRIB space */
377 const GLubyte *const vao_to_vbo_map = _vbo_attribute_alias_map[mode];
378
379 /* Now check the enabled arrays */
380 GLbitfield mask = vao_enabled;
381 while (mask) {
382 const int attr = u_bit_scan(&mask);
383 const unsigned char vbo_attr = vao_to_vbo_map[attr];
384 const GLenum16 tp = type[vbo_attr];
385 const GLintptr off = offset[vbo_attr] + buffer_offset;
386 const struct gl_array_attributes *attrib = &vao->VertexAttrib[attr];
387 if (attrib->RelativeOffset + vao->BufferBinding[0].Offset != off)
388 return false;
389 if (attrib->Format.Type != tp)
390 return false;
391 if (attrib->Format.Size != size[vbo_attr])
392 return false;
393 assert(attrib->Format.Format == GL_RGBA);
394 assert(attrib->Format.Normalized == GL_FALSE);
395 assert(attrib->Format.Integer == vbo_attrtype_to_integer_flag(tp));
396 assert(attrib->Format.Doubles == vbo_attrtype_to_double_flag(tp));
397 assert(attrib->BufferBindingIndex == 0);
398 }
399
400 return true;
401 }
402
403
404 /* Create or reuse the vao for the vertex processing mode. */
405 static void
406 update_vao(struct gl_context *ctx,
407 gl_vertex_processing_mode mode,
408 struct gl_vertex_array_object **vao,
409 struct gl_buffer_object *bo, GLintptr buffer_offset,
410 GLuint stride, GLbitfield64 vbo_enabled,
411 const GLubyte size[VBO_ATTRIB_MAX],
412 const GLenum16 type[VBO_ATTRIB_MAX],
413 const GLuint offset[VBO_ATTRIB_MAX])
414 {
415 /* Compute the bitmasks of vao_enabled arrays */
416 GLbitfield vao_enabled = _vbo_get_vao_enabled_from_vbo(mode, vbo_enabled);
417
418 /*
419 * Check if we can possibly reuse the exisiting one.
420 * In the long term we should reset them when something changes.
421 */
422 if (compare_vao(mode, *vao, bo, buffer_offset, stride,
423 vao_enabled, size, type, offset))
424 return;
425
426 /* The initial refcount is 1 */
427 _mesa_reference_vao(ctx, vao, NULL);
428 *vao = _mesa_new_vao(ctx, ~((GLuint)0));
429
430 /*
431 * assert(stride <= ctx->Const.MaxVertexAttribStride);
432 * MaxVertexAttribStride is not set for drivers that does not
433 * expose GL 44 or GLES 31.
434 */
435
436 /* Bind the buffer object at binding point 0 */
437 _mesa_bind_vertex_buffer(ctx, *vao, 0, bo, buffer_offset, stride);
438
439 /* Retrieve the mapping from VBO_ATTRIB to VERT_ATTRIB space
440 * Note that the position/generic0 aliasing is done in the VAO.
441 */
442 const GLubyte *const vao_to_vbo_map = _vbo_attribute_alias_map[mode];
443 /* Now set the enable arrays */
444 GLbitfield mask = vao_enabled;
445 while (mask) {
446 const int vao_attr = u_bit_scan(&mask);
447 const GLubyte vbo_attr = vao_to_vbo_map[vao_attr];
448 assert(offset[vbo_attr] <= ctx->Const.MaxVertexAttribRelativeOffset);
449
450 _vbo_set_attrib_format(ctx, *vao, vao_attr, buffer_offset,
451 size[vbo_attr], type[vbo_attr], offset[vbo_attr]);
452 _mesa_vertex_attrib_binding(ctx, *vao, vao_attr, 0);
453 }
454 _mesa_enable_vertex_array_attribs(ctx, *vao, vao_enabled);
455 assert(vao_enabled == (*vao)->Enabled);
456 assert((vao_enabled & ~(*vao)->VertexAttribBufferMask) == 0);
457
458 /* Finalize and freeze the VAO */
459 _mesa_set_vao_immutable(ctx, *vao);
460 }
461
462
463 /**
464 * Insert the active immediate struct onto the display list currently
465 * being built.
466 */
467 static void
468 compile_vertex_list(struct gl_context *ctx)
469 {
470 struct vbo_save_context *save = &vbo_context(ctx)->save;
471 struct vbo_save_vertex_list *node;
472
473 /* Allocate space for this structure in the display list currently
474 * being compiled.
475 */
476 node = (struct vbo_save_vertex_list *)
477 _mesa_dlist_alloc_aligned(ctx, save->opcode_vertex_list, sizeof(*node));
478
479 if (!node)
480 return;
481
482 /* Make sure the pointer is aligned to the size of a pointer */
483 assert((GLintptr) node % sizeof(void *) == 0);
484
485 /* Duplicate our template, increment refcounts to the storage structs:
486 */
487 GLintptr old_offset = 0;
488 if (save->VAO[0]) {
489 old_offset = save->VAO[0]->BufferBinding[0].Offset
490 + save->VAO[0]->VertexAttrib[VERT_ATTRIB_POS].RelativeOffset;
491 }
492 const GLsizei stride = save->vertex_size*sizeof(GLfloat);
493 GLintptr buffer_offset =
494 (save->buffer_map - save->vertex_store->buffer_map) * sizeof(GLfloat);
495 assert(old_offset <= buffer_offset);
496 const GLintptr offset_diff = buffer_offset - old_offset;
497 GLuint start_offset = 0;
498 if (offset_diff > 0 && stride > 0 && offset_diff % stride == 0) {
499 /* The vertex size is an exact multiple of the buffer offset.
500 * This means that we can use zero-based vertex attribute pointers
501 * and specify the start of the primitive with the _mesa_prim::start
502 * field. This results in issuing several draw calls with identical
503 * vertex attribute information. This can result in fewer state
504 * changes in drivers. In particular, the Gallium CSO module will
505 * filter out redundant vertex buffer changes.
506 */
507 /* We cannot immediately update the primitives as some methods below
508 * still need the uncorrected start vertices
509 */
510 start_offset = offset_diff/stride;
511 assert(old_offset == buffer_offset - offset_diff);
512 buffer_offset = old_offset;
513 }
514 GLuint offsets[VBO_ATTRIB_MAX];
515 for (unsigned i = 0, offset = 0; i < VBO_ATTRIB_MAX; ++i) {
516 offsets[i] = offset;
517 offset += save->attrsz[i] * sizeof(GLfloat);
518 }
519 node->vertex_count = save->vert_count;
520 node->wrap_count = save->copied.nr;
521 node->prims = save->prims;
522 node->prim_count = save->prim_count;
523 node->prim_store = save->prim_store;
524
525 /* Create a pair of VAOs for the possible VERTEX_PROCESSING_MODEs
526 * Note that this may reuse the previous one of possible.
527 */
528 for (gl_vertex_processing_mode vpm = VP_MODE_FF; vpm < VP_MODE_MAX; ++vpm) {
529 /* create or reuse the vao */
530 update_vao(ctx, vpm, &save->VAO[vpm],
531 save->vertex_store->bufferobj, buffer_offset, stride,
532 save->enabled, save->attrsz, save->attrtype, offsets);
533 /* Reference the vao in the dlist */
534 node->VAO[vpm] = NULL;
535 _mesa_reference_vao(ctx, &node->VAO[vpm], save->VAO[vpm]);
536 }
537
538 node->prim_store->refcount++;
539
540 if (save->no_current_update) {
541 node->current_data = NULL;
542 }
543 else {
544 GLuint current_size = save->vertex_size - save->attrsz[0];
545 node->current_data = NULL;
546
547 if (current_size) {
548 node->current_data = malloc(current_size * sizeof(GLfloat));
549 if (node->current_data) {
550 const char *buffer = (const char *)save->buffer_map;
551 unsigned attr_offset = save->attrsz[0] * sizeof(GLfloat);
552 unsigned vertex_offset = 0;
553
554 if (node->vertex_count)
555 vertex_offset = (node->vertex_count - 1) * stride;
556
557 memcpy(node->current_data, buffer + vertex_offset + attr_offset,
558 current_size * sizeof(GLfloat));
559 } else {
560 _mesa_error(ctx, GL_OUT_OF_MEMORY, "Current value allocation");
561 }
562 }
563 }
564
565 assert(save->attrsz[VBO_ATTRIB_POS] != 0 || node->vertex_count == 0);
566
567 if (save->dangling_attr_ref)
568 ctx->ListState.CurrentList->Flags |= DLIST_DANGLING_REFS;
569
570 save->vertex_store->used += save->vertex_size * node->vertex_count;
571 save->prim_store->used += node->prim_count;
572
573 /* Copy duplicated vertices
574 */
575 save->copied.nr = copy_vertices(ctx, node, save->buffer_map);
576
577 if (node->prims[node->prim_count - 1].mode == GL_LINE_LOOP) {
578 convert_line_loop_to_strip(save, node);
579 }
580
581 merge_prims(node->prims, &node->prim_count);
582
583 /* Correct the primitive starts, we can only do this here as copy_vertices
584 * and convert_line_loop_to_strip above consume the uncorrected starts.
585 * On the other hand the _vbo_loopback_vertex_list call below needs the
586 * primitves to be corrected already.
587 */
588 for (unsigned i = 0; i < node->prim_count; i++) {
589 node->prims[i].start += start_offset;
590 }
591
592 /* Deal with GL_COMPILE_AND_EXECUTE:
593 */
594 if (ctx->ExecuteFlag) {
595 struct _glapi_table *dispatch = GET_DISPATCH();
596
597 _glapi_set_dispatch(ctx->Exec);
598
599 /* Note that the range of referenced vertices must be mapped already */
600 _vbo_loopback_vertex_list(ctx, node);
601
602 _glapi_set_dispatch(dispatch);
603 }
604
605 /* Decide whether the storage structs are full, or can be used for
606 * the next vertex lists as well.
607 */
608 if (save->vertex_store->used >
609 VBO_SAVE_BUFFER_SIZE - 16 * (save->vertex_size + 4)) {
610
611 /* Unmap old store:
612 */
613 vbo_save_unmap_vertex_store(ctx, save->vertex_store);
614
615 /* Release old reference:
616 */
617 free_vertex_store(ctx, save->vertex_store);
618 save->vertex_store = NULL;
619 /* When we have a new vbo, we will for sure need a new vao */
620 for (gl_vertex_processing_mode vpm = 0; vpm < VP_MODE_MAX; ++vpm)
621 _mesa_reference_vao(ctx, &save->VAO[vpm], NULL);
622
623 /* Allocate and map new store:
624 */
625 save->vertex_store = alloc_vertex_store(ctx);
626 save->buffer_ptr = vbo_save_map_vertex_store(ctx, save->vertex_store);
627 save->out_of_memory = save->buffer_ptr == NULL;
628 }
629 else {
630 /* update buffer_ptr for next vertex */
631 save->buffer_ptr = save->vertex_store->buffer_map
632 + save->vertex_store->used;
633 }
634
635 if (save->prim_store->used > VBO_SAVE_PRIM_SIZE - 6) {
636 save->prim_store->refcount--;
637 assert(save->prim_store->refcount != 0);
638 save->prim_store = alloc_prim_store();
639 }
640
641 /* Reset our structures for the next run of vertices:
642 */
643 reset_counters(ctx);
644 }
645
646
647 /**
648 * This is called when we fill a vertex buffer before we hit a glEnd().
649 * We
650 * TODO -- If no new vertices have been stored, don't bother saving it.
651 */
652 static void
653 wrap_buffers(struct gl_context *ctx)
654 {
655 struct vbo_save_context *save = &vbo_context(ctx)->save;
656 GLint i = save->prim_count - 1;
657 GLenum mode;
658
659 assert(i < (GLint) save->prim_max);
660 assert(i >= 0);
661
662 /* Close off in-progress primitive.
663 */
664 save->prims[i].count = (save->vert_count - save->prims[i].start);
665 mode = save->prims[i].mode;
666
667 /* store the copied vertices, and allocate a new list.
668 */
669 compile_vertex_list(ctx);
670
671 /* Restart interrupted primitive
672 */
673 save->prims[0].mode = mode;
674 save->prims[0].begin = 0;
675 save->prims[0].end = 0;
676 save->prims[0].start = 0;
677 save->prims[0].count = 0;
678 save->prim_count = 1;
679 }
680
681
682 /**
683 * Called only when buffers are wrapped as the result of filling the
684 * vertex_store struct.
685 */
686 static void
687 wrap_filled_vertex(struct gl_context *ctx)
688 {
689 struct vbo_save_context *save = &vbo_context(ctx)->save;
690 unsigned numComponents;
691
692 /* Emit a glEnd to close off the last vertex list.
693 */
694 wrap_buffers(ctx);
695
696 /* Copy stored stored vertices to start of new list.
697 */
698 assert(save->max_vert - save->vert_count > save->copied.nr);
699
700 numComponents = save->copied.nr * save->vertex_size;
701 memcpy(save->buffer_ptr,
702 save->copied.buffer,
703 numComponents * sizeof(fi_type));
704 save->buffer_ptr += numComponents;
705 save->vert_count += save->copied.nr;
706 }
707
708
709 static void
710 copy_to_current(struct gl_context *ctx)
711 {
712 struct vbo_save_context *save = &vbo_context(ctx)->save;
713 GLbitfield64 enabled = save->enabled & (~BITFIELD64_BIT(VBO_ATTRIB_POS));
714
715 while (enabled) {
716 const int i = u_bit_scan64(&enabled);
717 assert(save->attrsz[i]);
718
719 if (save->attrtype[i] == GL_DOUBLE ||
720 save->attrtype[i] == GL_UNSIGNED_INT64_ARB)
721 memcpy(save->current[i], save->attrptr[i], save->attrsz[i] * sizeof(GLfloat));
722 else
723 COPY_CLEAN_4V_TYPE_AS_UNION(save->current[i], save->attrsz[i],
724 save->attrptr[i], save->attrtype[i]);
725 }
726 }
727
728
729 static void
730 copy_from_current(struct gl_context *ctx)
731 {
732 struct vbo_save_context *save = &vbo_context(ctx)->save;
733 GLbitfield64 enabled = save->enabled & (~BITFIELD64_BIT(VBO_ATTRIB_POS));
734
735 while (enabled) {
736 const int i = u_bit_scan64(&enabled);
737
738 switch (save->attrsz[i]) {
739 case 4:
740 save->attrptr[i][3] = save->current[i][3];
741 case 3:
742 save->attrptr[i][2] = save->current[i][2];
743 case 2:
744 save->attrptr[i][1] = save->current[i][1];
745 case 1:
746 save->attrptr[i][0] = save->current[i][0];
747 break;
748 case 0:
749 unreachable("Unexpected vertex attribute size");
750 }
751 }
752 }
753
754
755 /**
756 * Called when we increase the size of a vertex attribute. For example,
757 * if we've seen one or more glTexCoord2f() calls and now we get a
758 * glTexCoord3f() call.
759 * Flush existing data, set new attrib size, replay copied vertices.
760 */
761 static void
762 upgrade_vertex(struct gl_context *ctx, GLuint attr, GLuint newsz)
763 {
764 struct vbo_save_context *save = &vbo_context(ctx)->save;
765 GLuint oldsz;
766 GLuint i;
767 fi_type *tmp;
768
769 /* Store the current run of vertices, and emit a GL_END. Emit a
770 * BEGIN in the new buffer.
771 */
772 if (save->vert_count)
773 wrap_buffers(ctx);
774 else
775 assert(save->copied.nr == 0);
776
777 /* Do a COPY_TO_CURRENT to ensure back-copying works for the case
778 * when the attribute already exists in the vertex and is having
779 * its size increased.
780 */
781 copy_to_current(ctx);
782
783 /* Fix up sizes:
784 */
785 oldsz = save->attrsz[attr];
786 save->attrsz[attr] = newsz;
787 save->enabled |= BITFIELD64_BIT(attr);
788
789 save->vertex_size += newsz - oldsz;
790 save->max_vert = ((VBO_SAVE_BUFFER_SIZE - save->vertex_store->used) /
791 save->vertex_size);
792 save->vert_count = 0;
793
794 /* Recalculate all the attrptr[] values:
795 */
796 tmp = save->vertex;
797 for (i = 0; i < VBO_ATTRIB_MAX; i++) {
798 if (save->attrsz[i]) {
799 save->attrptr[i] = tmp;
800 tmp += save->attrsz[i];
801 }
802 else {
803 save->attrptr[i] = NULL; /* will not be dereferenced. */
804 }
805 }
806
807 /* Copy from current to repopulate the vertex with correct values.
808 */
809 copy_from_current(ctx);
810
811 /* Replay stored vertices to translate them to new format here.
812 *
813 * If there are copied vertices and the new (upgraded) attribute
814 * has not been defined before, this list is somewhat degenerate,
815 * and will need fixup at runtime.
816 */
817 if (save->copied.nr) {
818 const fi_type *data = save->copied.buffer;
819 fi_type *dest = save->buffer_map;
820
821 /* Need to note this and fix up at runtime (or loopback):
822 */
823 if (attr != VBO_ATTRIB_POS && save->currentsz[attr][0] == 0) {
824 assert(oldsz == 0);
825 save->dangling_attr_ref = GL_TRUE;
826 }
827
828 for (i = 0; i < save->copied.nr; i++) {
829 GLbitfield64 enabled = save->enabled;
830 while (enabled) {
831 const int j = u_bit_scan64(&enabled);
832 assert(save->attrsz[j]);
833 if (j == attr) {
834 if (oldsz) {
835 COPY_CLEAN_4V_TYPE_AS_UNION(dest, oldsz, data,
836 save->attrtype[j]);
837 data += oldsz;
838 dest += newsz;
839 }
840 else {
841 COPY_SZ_4V(dest, newsz, save->current[attr]);
842 dest += newsz;
843 }
844 }
845 else {
846 GLint sz = save->attrsz[j];
847 COPY_SZ_4V(dest, sz, data);
848 data += sz;
849 dest += sz;
850 }
851 }
852 }
853
854 save->buffer_ptr = dest;
855 save->vert_count += save->copied.nr;
856 }
857 }
858
859
860 /**
861 * This is called when the size of a vertex attribute changes.
862 * For example, after seeing one or more glTexCoord2f() calls we
863 * get a glTexCoord4f() or glTexCoord1f() call.
864 */
865 static void
866 fixup_vertex(struct gl_context *ctx, GLuint attr,
867 GLuint sz, GLenum newType)
868 {
869 struct vbo_save_context *save = &vbo_context(ctx)->save;
870
871 if (sz > save->attrsz[attr] ||
872 newType != save->attrtype[attr]) {
873 /* New size is larger. Need to flush existing vertices and get
874 * an enlarged vertex format.
875 */
876 upgrade_vertex(ctx, attr, sz);
877 }
878 else if (sz < save->active_sz[attr]) {
879 GLuint i;
880 const fi_type *id = vbo_get_default_vals_as_union(save->attrtype[attr]);
881
882 /* New size is equal or smaller - just need to fill in some
883 * zeros.
884 */
885 for (i = sz; i <= save->attrsz[attr]; i++)
886 save->attrptr[attr][i - 1] = id[i - 1];
887 }
888
889 save->active_sz[attr] = sz;
890 }
891
892
893 /**
894 * Reset the current size of all vertex attributes to the default
895 * value of 0. This signals that we haven't yet seen any per-vertex
896 * commands such as glNormal3f() or glTexCoord2f().
897 */
898 static void
899 reset_vertex(struct gl_context *ctx)
900 {
901 struct vbo_save_context *save = &vbo_context(ctx)->save;
902
903 while (save->enabled) {
904 const int i = u_bit_scan64(&save->enabled);
905 assert(save->attrsz[i]);
906 save->attrsz[i] = 0;
907 save->active_sz[i] = 0;
908 }
909
910 save->vertex_size = 0;
911 }
912
913
914 /**
915 * If index=0, does glVertexAttrib*() alias glVertex() to emit a vertex?
916 * It depends on a few things, including whether we're inside or outside
917 * of glBegin/glEnd.
918 */
919 static inline bool
920 is_vertex_position(const struct gl_context *ctx, GLuint index)
921 {
922 return (index == 0 &&
923 _mesa_attr_zero_aliases_vertex(ctx) &&
924 _mesa_inside_dlist_begin_end(ctx));
925 }
926
927
928
929 #define ERROR(err) _mesa_compile_error(ctx, err, __func__);
930
931
932 /* Only one size for each attribute may be active at once. Eg. if
933 * Color3f is installed/active, then Color4f may not be, even if the
934 * vertex actually contains 4 color coordinates. This is because the
935 * 3f version won't otherwise set color[3] to 1.0 -- this is the job
936 * of the chooser function when switching between Color4f and Color3f.
937 */
938 #define ATTR_UNION(A, N, T, C, V0, V1, V2, V3) \
939 do { \
940 struct vbo_save_context *save = &vbo_context(ctx)->save; \
941 int sz = (sizeof(C) / sizeof(GLfloat)); \
942 \
943 if (save->active_sz[A] != N) \
944 fixup_vertex(ctx, A, N * sz, T); \
945 \
946 { \
947 C *dest = (C *)save->attrptr[A]; \
948 if (N>0) dest[0] = V0; \
949 if (N>1) dest[1] = V1; \
950 if (N>2) dest[2] = V2; \
951 if (N>3) dest[3] = V3; \
952 save->attrtype[A] = T; \
953 } \
954 \
955 if ((A) == 0) { \
956 GLuint i; \
957 \
958 for (i = 0; i < save->vertex_size; i++) \
959 save->buffer_ptr[i] = save->vertex[i]; \
960 \
961 save->buffer_ptr += save->vertex_size; \
962 \
963 if (++save->vert_count >= save->max_vert) \
964 wrap_filled_vertex(ctx); \
965 } \
966 } while (0)
967
968 #define TAG(x) _save_##x
969
970 #include "vbo_attrib_tmp.h"
971
972
973
974 #define MAT( ATTR, N, face, params ) \
975 do { \
976 if (face != GL_BACK) \
977 MAT_ATTR( ATTR, N, params ); /* front */ \
978 if (face != GL_FRONT) \
979 MAT_ATTR( ATTR + 1, N, params ); /* back */ \
980 } while (0)
981
982
983 /**
984 * Save a glMaterial call found between glBegin/End.
985 * glMaterial calls outside Begin/End are handled in dlist.c.
986 */
987 static void GLAPIENTRY
988 _save_Materialfv(GLenum face, GLenum pname, const GLfloat *params)
989 {
990 GET_CURRENT_CONTEXT(ctx);
991
992 if (face != GL_FRONT && face != GL_BACK && face != GL_FRONT_AND_BACK) {
993 _mesa_compile_error(ctx, GL_INVALID_ENUM, "glMaterial(face)");
994 return;
995 }
996
997 switch (pname) {
998 case GL_EMISSION:
999 MAT(VBO_ATTRIB_MAT_FRONT_EMISSION, 4, face, params);
1000 break;
1001 case GL_AMBIENT:
1002 MAT(VBO_ATTRIB_MAT_FRONT_AMBIENT, 4, face, params);
1003 break;
1004 case GL_DIFFUSE:
1005 MAT(VBO_ATTRIB_MAT_FRONT_DIFFUSE, 4, face, params);
1006 break;
1007 case GL_SPECULAR:
1008 MAT(VBO_ATTRIB_MAT_FRONT_SPECULAR, 4, face, params);
1009 break;
1010 case GL_SHININESS:
1011 if (*params < 0 || *params > ctx->Const.MaxShininess) {
1012 _mesa_compile_error(ctx, GL_INVALID_VALUE, "glMaterial(shininess)");
1013 }
1014 else {
1015 MAT(VBO_ATTRIB_MAT_FRONT_SHININESS, 1, face, params);
1016 }
1017 break;
1018 case GL_COLOR_INDEXES:
1019 MAT(VBO_ATTRIB_MAT_FRONT_INDEXES, 3, face, params);
1020 break;
1021 case GL_AMBIENT_AND_DIFFUSE:
1022 MAT(VBO_ATTRIB_MAT_FRONT_AMBIENT, 4, face, params);
1023 MAT(VBO_ATTRIB_MAT_FRONT_DIFFUSE, 4, face, params);
1024 break;
1025 default:
1026 _mesa_compile_error(ctx, GL_INVALID_ENUM, "glMaterial(pname)");
1027 return;
1028 }
1029 }
1030
1031
1032 /* Cope with EvalCoord/CallList called within a begin/end object:
1033 * -- Flush current buffer
1034 * -- Fallback to opcodes for the rest of the begin/end object.
1035 */
1036 static void
1037 dlist_fallback(struct gl_context *ctx)
1038 {
1039 struct vbo_save_context *save = &vbo_context(ctx)->save;
1040
1041 if (save->vert_count || save->prim_count) {
1042 if (save->prim_count > 0) {
1043 /* Close off in-progress primitive. */
1044 GLint i = save->prim_count - 1;
1045 save->prims[i].count = save->vert_count - save->prims[i].start;
1046 }
1047
1048 /* Need to replay this display list with loopback,
1049 * unfortunately, otherwise this primitive won't be handled
1050 * properly:
1051 */
1052 save->dangling_attr_ref = GL_TRUE;
1053
1054 compile_vertex_list(ctx);
1055 }
1056
1057 copy_to_current(ctx);
1058 reset_vertex(ctx);
1059 reset_counters(ctx);
1060 if (save->out_of_memory) {
1061 _mesa_install_save_vtxfmt(ctx, &save->vtxfmt_noop);
1062 }
1063 else {
1064 _mesa_install_save_vtxfmt(ctx, &ctx->ListState.ListVtxfmt);
1065 }
1066 ctx->Driver.SaveNeedFlush = GL_FALSE;
1067 }
1068
1069
1070 static void GLAPIENTRY
1071 _save_EvalCoord1f(GLfloat u)
1072 {
1073 GET_CURRENT_CONTEXT(ctx);
1074 dlist_fallback(ctx);
1075 CALL_EvalCoord1f(ctx->Save, (u));
1076 }
1077
1078 static void GLAPIENTRY
1079 _save_EvalCoord1fv(const GLfloat * v)
1080 {
1081 GET_CURRENT_CONTEXT(ctx);
1082 dlist_fallback(ctx);
1083 CALL_EvalCoord1fv(ctx->Save, (v));
1084 }
1085
1086 static void GLAPIENTRY
1087 _save_EvalCoord2f(GLfloat u, GLfloat v)
1088 {
1089 GET_CURRENT_CONTEXT(ctx);
1090 dlist_fallback(ctx);
1091 CALL_EvalCoord2f(ctx->Save, (u, v));
1092 }
1093
1094 static void GLAPIENTRY
1095 _save_EvalCoord2fv(const GLfloat * v)
1096 {
1097 GET_CURRENT_CONTEXT(ctx);
1098 dlist_fallback(ctx);
1099 CALL_EvalCoord2fv(ctx->Save, (v));
1100 }
1101
1102 static void GLAPIENTRY
1103 _save_EvalPoint1(GLint i)
1104 {
1105 GET_CURRENT_CONTEXT(ctx);
1106 dlist_fallback(ctx);
1107 CALL_EvalPoint1(ctx->Save, (i));
1108 }
1109
1110 static void GLAPIENTRY
1111 _save_EvalPoint2(GLint i, GLint j)
1112 {
1113 GET_CURRENT_CONTEXT(ctx);
1114 dlist_fallback(ctx);
1115 CALL_EvalPoint2(ctx->Save, (i, j));
1116 }
1117
1118 static void GLAPIENTRY
1119 _save_CallList(GLuint l)
1120 {
1121 GET_CURRENT_CONTEXT(ctx);
1122 dlist_fallback(ctx);
1123 CALL_CallList(ctx->Save, (l));
1124 }
1125
1126 static void GLAPIENTRY
1127 _save_CallLists(GLsizei n, GLenum type, const GLvoid * v)
1128 {
1129 GET_CURRENT_CONTEXT(ctx);
1130 dlist_fallback(ctx);
1131 CALL_CallLists(ctx->Save, (n, type, v));
1132 }
1133
1134
1135
1136 /**
1137 * Called when a glBegin is getting compiled into a display list.
1138 * Updating of ctx->Driver.CurrentSavePrimitive is already taken care of.
1139 */
1140 void
1141 vbo_save_NotifyBegin(struct gl_context *ctx, GLenum mode,
1142 bool no_current_update)
1143 {
1144 struct vbo_save_context *save = &vbo_context(ctx)->save;
1145 const GLuint i = save->prim_count++;
1146
1147 ctx->Driver.CurrentSavePrimitive = mode;
1148
1149 assert(i < save->prim_max);
1150 save->prims[i].mode = mode & VBO_SAVE_PRIM_MODE_MASK;
1151 save->prims[i].begin = 1;
1152 save->prims[i].end = 0;
1153 save->prims[i].start = save->vert_count;
1154 save->prims[i].count = 0;
1155
1156 save->no_current_update = no_current_update;
1157
1158 if (save->out_of_memory) {
1159 _mesa_install_save_vtxfmt(ctx, &save->vtxfmt_noop);
1160 }
1161 else {
1162 _mesa_install_save_vtxfmt(ctx, &save->vtxfmt);
1163 }
1164
1165 /* We need to call vbo_save_SaveFlushVertices() if there's state change */
1166 ctx->Driver.SaveNeedFlush = GL_TRUE;
1167 }
1168
1169
1170 static void GLAPIENTRY
1171 _save_End(void)
1172 {
1173 GET_CURRENT_CONTEXT(ctx);
1174 struct vbo_save_context *save = &vbo_context(ctx)->save;
1175 const GLint i = save->prim_count - 1;
1176
1177 ctx->Driver.CurrentSavePrimitive = PRIM_OUTSIDE_BEGIN_END;
1178 save->prims[i].end = 1;
1179 save->prims[i].count = (save->vert_count - save->prims[i].start);
1180
1181 if (i == (GLint) save->prim_max - 1) {
1182 compile_vertex_list(ctx);
1183 assert(save->copied.nr == 0);
1184 }
1185
1186 /* Swap out this vertex format while outside begin/end. Any color,
1187 * etc. received between here and the next begin will be compiled
1188 * as opcodes.
1189 */
1190 if (save->out_of_memory) {
1191 _mesa_install_save_vtxfmt(ctx, &save->vtxfmt_noop);
1192 }
1193 else {
1194 _mesa_install_save_vtxfmt(ctx, &ctx->ListState.ListVtxfmt);
1195 }
1196 }
1197
1198
1199 static void GLAPIENTRY
1200 _save_Begin(GLenum mode)
1201 {
1202 GET_CURRENT_CONTEXT(ctx);
1203 (void) mode;
1204 _mesa_compile_error(ctx, GL_INVALID_OPERATION, "Recursive glBegin");
1205 }
1206
1207
1208 static void GLAPIENTRY
1209 _save_PrimitiveRestartNV(void)
1210 {
1211 GET_CURRENT_CONTEXT(ctx);
1212 struct vbo_save_context *save = &vbo_context(ctx)->save;
1213
1214 if (save->prim_count == 0) {
1215 /* We're not inside a glBegin/End pair, so calling glPrimitiverRestartNV
1216 * is an error.
1217 */
1218 _mesa_compile_error(ctx, GL_INVALID_OPERATION,
1219 "glPrimitiveRestartNV called outside glBegin/End");
1220 } else {
1221 /* get current primitive mode */
1222 GLenum curPrim = save->prims[save->prim_count - 1].mode;
1223 bool no_current_update = save->no_current_update;
1224
1225 /* restart primitive */
1226 CALL_End(GET_DISPATCH(), ());
1227 vbo_save_NotifyBegin(ctx, curPrim, no_current_update);
1228 }
1229 }
1230
1231
1232 /* Unlike the functions above, these are to be hooked into the vtxfmt
1233 * maintained in ctx->ListState, active when the list is known or
1234 * suspected to be outside any begin/end primitive.
1235 * Note: OBE = Outside Begin/End
1236 */
1237 static void GLAPIENTRY
1238 _save_OBE_Rectf(GLfloat x1, GLfloat y1, GLfloat x2, GLfloat y2)
1239 {
1240 GET_CURRENT_CONTEXT(ctx);
1241 vbo_save_NotifyBegin(ctx, GL_QUADS, false);
1242 CALL_Vertex2f(GET_DISPATCH(), (x1, y1));
1243 CALL_Vertex2f(GET_DISPATCH(), (x2, y1));
1244 CALL_Vertex2f(GET_DISPATCH(), (x2, y2));
1245 CALL_Vertex2f(GET_DISPATCH(), (x1, y2));
1246 CALL_End(GET_DISPATCH(), ());
1247 }
1248
1249
1250 static void GLAPIENTRY
1251 _save_OBE_DrawArrays(GLenum mode, GLint start, GLsizei count)
1252 {
1253 GET_CURRENT_CONTEXT(ctx);
1254 struct gl_vertex_array_object *vao = ctx->Array.VAO;
1255 struct vbo_save_context *save = &vbo_context(ctx)->save;
1256 GLint i;
1257
1258 if (!_mesa_is_valid_prim_mode(ctx, mode)) {
1259 _mesa_compile_error(ctx, GL_INVALID_ENUM, "glDrawArrays(mode)");
1260 return;
1261 }
1262 if (count < 0) {
1263 _mesa_compile_error(ctx, GL_INVALID_VALUE, "glDrawArrays(count<0)");
1264 return;
1265 }
1266
1267 if (save->out_of_memory)
1268 return;
1269
1270 /* Make sure to process any VBO binding changes */
1271 _mesa_update_state(ctx);
1272
1273 _mesa_vao_map_arrays(ctx, vao, GL_MAP_READ_BIT);
1274
1275 vbo_save_NotifyBegin(ctx, mode, true);
1276
1277 for (i = 0; i < count; i++)
1278 _mesa_array_element(ctx, start + i);
1279 CALL_End(GET_DISPATCH(), ());
1280
1281 _mesa_vao_unmap_arrays(ctx, vao);
1282 }
1283
1284
1285 static void GLAPIENTRY
1286 _save_OBE_MultiDrawArrays(GLenum mode, const GLint *first,
1287 const GLsizei *count, GLsizei primcount)
1288 {
1289 GET_CURRENT_CONTEXT(ctx);
1290 GLint i;
1291
1292 if (!_mesa_is_valid_prim_mode(ctx, mode)) {
1293 _mesa_compile_error(ctx, GL_INVALID_ENUM, "glMultiDrawArrays(mode)");
1294 return;
1295 }
1296
1297 if (primcount < 0) {
1298 _mesa_compile_error(ctx, GL_INVALID_VALUE,
1299 "glMultiDrawArrays(primcount<0)");
1300 return;
1301 }
1302
1303 for (i = 0; i < primcount; i++) {
1304 if (count[i] < 0) {
1305 _mesa_compile_error(ctx, GL_INVALID_VALUE,
1306 "glMultiDrawArrays(count[i]<0)");
1307 return;
1308 }
1309 }
1310
1311 for (i = 0; i < primcount; i++) {
1312 if (count[i] > 0) {
1313 _save_OBE_DrawArrays(mode, first[i], count[i]);
1314 }
1315 }
1316 }
1317
1318
1319 static void
1320 array_element(struct gl_context *ctx,
1321 GLint basevertex, GLuint elt, unsigned index_size)
1322 {
1323 /* Section 10.3.5 Primitive Restart:
1324 * [...]
1325 * When one of the *BaseVertex drawing commands specified in section 10.5
1326 * is used, the primitive restart comparison occurs before the basevertex
1327 * offset is added to the array index.
1328 */
1329 /* If PrimitiveRestart is enabled and the index is the RestartIndex
1330 * then we call PrimitiveRestartNV and return.
1331 */
1332 if (ctx->Array._PrimitiveRestart &&
1333 elt == _mesa_primitive_restart_index(ctx, index_size)) {
1334 CALL_PrimitiveRestartNV(GET_DISPATCH(), ());
1335 return;
1336 }
1337
1338 _mesa_array_element(ctx, basevertex + elt);
1339 }
1340
1341
1342 /* Could do better by copying the arrays and element list intact and
1343 * then emitting an indexed prim at runtime.
1344 */
1345 static void GLAPIENTRY
1346 _save_OBE_DrawElementsBaseVertex(GLenum mode, GLsizei count, GLenum type,
1347 const GLvoid * indices, GLint basevertex)
1348 {
1349 GET_CURRENT_CONTEXT(ctx);
1350 struct vbo_save_context *save = &vbo_context(ctx)->save;
1351 struct gl_vertex_array_object *vao = ctx->Array.VAO;
1352 struct gl_buffer_object *indexbuf = vao->IndexBufferObj;
1353 GLint i;
1354
1355 if (!_mesa_is_valid_prim_mode(ctx, mode)) {
1356 _mesa_compile_error(ctx, GL_INVALID_ENUM, "glDrawElements(mode)");
1357 return;
1358 }
1359 if (count < 0) {
1360 _mesa_compile_error(ctx, GL_INVALID_VALUE, "glDrawElements(count<0)");
1361 return;
1362 }
1363 if (type != GL_UNSIGNED_BYTE &&
1364 type != GL_UNSIGNED_SHORT &&
1365 type != GL_UNSIGNED_INT) {
1366 _mesa_compile_error(ctx, GL_INVALID_VALUE, "glDrawElements(count<0)");
1367 return;
1368 }
1369
1370 if (save->out_of_memory)
1371 return;
1372
1373 /* Make sure to process any VBO binding changes */
1374 _mesa_update_state(ctx);
1375
1376 _mesa_vao_map(ctx, vao, GL_MAP_READ_BIT);
1377
1378 if (_mesa_is_bufferobj(indexbuf))
1379 indices =
1380 ADD_POINTERS(indexbuf->Mappings[MAP_INTERNAL].Pointer, indices);
1381
1382 vbo_save_NotifyBegin(ctx, mode, true);
1383
1384 switch (type) {
1385 case GL_UNSIGNED_BYTE:
1386 for (i = 0; i < count; i++)
1387 array_element(ctx, basevertex, ((GLubyte *) indices)[i], 1);
1388 break;
1389 case GL_UNSIGNED_SHORT:
1390 for (i = 0; i < count; i++)
1391 array_element(ctx, basevertex, ((GLushort *) indices)[i], 2);
1392 break;
1393 case GL_UNSIGNED_INT:
1394 for (i = 0; i < count; i++)
1395 array_element(ctx, basevertex, ((GLuint *) indices)[i], 4);
1396 break;
1397 default:
1398 _mesa_error(ctx, GL_INVALID_ENUM, "glDrawElements(type)");
1399 break;
1400 }
1401
1402 CALL_End(GET_DISPATCH(), ());
1403
1404 _mesa_vao_unmap(ctx, vao);
1405 }
1406
1407 static void GLAPIENTRY
1408 _save_OBE_DrawElements(GLenum mode, GLsizei count, GLenum type,
1409 const GLvoid * indices)
1410 {
1411 _save_OBE_DrawElementsBaseVertex(mode, count, type, indices, 0);
1412 }
1413
1414
1415 static void GLAPIENTRY
1416 _save_OBE_DrawRangeElements(GLenum mode, GLuint start, GLuint end,
1417 GLsizei count, GLenum type,
1418 const GLvoid * indices)
1419 {
1420 GET_CURRENT_CONTEXT(ctx);
1421 struct vbo_save_context *save = &vbo_context(ctx)->save;
1422
1423 if (!_mesa_is_valid_prim_mode(ctx, mode)) {
1424 _mesa_compile_error(ctx, GL_INVALID_ENUM, "glDrawRangeElements(mode)");
1425 return;
1426 }
1427 if (count < 0) {
1428 _mesa_compile_error(ctx, GL_INVALID_VALUE,
1429 "glDrawRangeElements(count<0)");
1430 return;
1431 }
1432 if (type != GL_UNSIGNED_BYTE &&
1433 type != GL_UNSIGNED_SHORT &&
1434 type != GL_UNSIGNED_INT) {
1435 _mesa_compile_error(ctx, GL_INVALID_ENUM, "glDrawRangeElements(type)");
1436 return;
1437 }
1438 if (end < start) {
1439 _mesa_compile_error(ctx, GL_INVALID_VALUE,
1440 "glDrawRangeElements(end < start)");
1441 return;
1442 }
1443
1444 if (save->out_of_memory)
1445 return;
1446
1447 _save_OBE_DrawElements(mode, count, type, indices);
1448 }
1449
1450
1451 static void GLAPIENTRY
1452 _save_OBE_MultiDrawElements(GLenum mode, const GLsizei *count, GLenum type,
1453 const GLvoid * const *indices, GLsizei primcount)
1454 {
1455 GLsizei i;
1456
1457 for (i = 0; i < primcount; i++) {
1458 if (count[i] > 0) {
1459 CALL_DrawElements(GET_DISPATCH(), (mode, count[i], type, indices[i]));
1460 }
1461 }
1462 }
1463
1464
1465 static void GLAPIENTRY
1466 _save_OBE_MultiDrawElementsBaseVertex(GLenum mode, const GLsizei *count,
1467 GLenum type,
1468 const GLvoid * const *indices,
1469 GLsizei primcount,
1470 const GLint *basevertex)
1471 {
1472 GLsizei i;
1473
1474 for (i = 0; i < primcount; i++) {
1475 if (count[i] > 0) {
1476 CALL_DrawElementsBaseVertex(GET_DISPATCH(), (mode, count[i], type,
1477 indices[i],
1478 basevertex[i]));
1479 }
1480 }
1481 }
1482
1483
1484 static void
1485 vtxfmt_init(struct gl_context *ctx)
1486 {
1487 struct vbo_save_context *save = &vbo_context(ctx)->save;
1488 GLvertexformat *vfmt = &save->vtxfmt;
1489
1490 #define NAME_AE(x) _ae_##x
1491 #define NAME_CALLLIST(x) _save_##x
1492 #define NAME(x) _save_##x
1493 #define NAME_ES(x) _save_##x##ARB
1494
1495 #include "vbo_init_tmp.h"
1496 }
1497
1498
1499 /**
1500 * Initialize the dispatch table with the VBO functions for display
1501 * list compilation.
1502 */
1503 void
1504 vbo_initialize_save_dispatch(const struct gl_context *ctx,
1505 struct _glapi_table *exec)
1506 {
1507 SET_DrawArrays(exec, _save_OBE_DrawArrays);
1508 SET_MultiDrawArrays(exec, _save_OBE_MultiDrawArrays);
1509 SET_DrawElements(exec, _save_OBE_DrawElements);
1510 SET_DrawElementsBaseVertex(exec, _save_OBE_DrawElementsBaseVertex);
1511 SET_DrawRangeElements(exec, _save_OBE_DrawRangeElements);
1512 SET_MultiDrawElementsEXT(exec, _save_OBE_MultiDrawElements);
1513 SET_MultiDrawElementsBaseVertex(exec, _save_OBE_MultiDrawElementsBaseVertex);
1514 SET_Rectf(exec, _save_OBE_Rectf);
1515 /* Note: other glDraw functins aren't compiled into display lists */
1516 }
1517
1518
1519
1520 void
1521 vbo_save_SaveFlushVertices(struct gl_context *ctx)
1522 {
1523 struct vbo_save_context *save = &vbo_context(ctx)->save;
1524
1525 /* Noop when we are actually active:
1526 */
1527 if (ctx->Driver.CurrentSavePrimitive <= PRIM_MAX)
1528 return;
1529
1530 if (save->vert_count || save->prim_count)
1531 compile_vertex_list(ctx);
1532
1533 copy_to_current(ctx);
1534 reset_vertex(ctx);
1535 reset_counters(ctx);
1536 ctx->Driver.SaveNeedFlush = GL_FALSE;
1537 }
1538
1539
1540 /**
1541 * Called from glNewList when we're starting to compile a display list.
1542 */
1543 void
1544 vbo_save_NewList(struct gl_context *ctx, GLuint list, GLenum mode)
1545 {
1546 struct vbo_save_context *save = &vbo_context(ctx)->save;
1547
1548 (void) list;
1549 (void) mode;
1550
1551 if (!save->prim_store)
1552 save->prim_store = alloc_prim_store();
1553
1554 if (!save->vertex_store)
1555 save->vertex_store = alloc_vertex_store(ctx);
1556
1557 save->buffer_ptr = vbo_save_map_vertex_store(ctx, save->vertex_store);
1558
1559 reset_vertex(ctx);
1560 reset_counters(ctx);
1561 ctx->Driver.SaveNeedFlush = GL_FALSE;
1562 }
1563
1564
1565 /**
1566 * Called from glEndList when we're finished compiling a display list.
1567 */
1568 void
1569 vbo_save_EndList(struct gl_context *ctx)
1570 {
1571 struct vbo_save_context *save = &vbo_context(ctx)->save;
1572
1573 /* EndList called inside a (saved) Begin/End pair?
1574 */
1575 if (_mesa_inside_dlist_begin_end(ctx)) {
1576 if (save->prim_count > 0) {
1577 GLint i = save->prim_count - 1;
1578 ctx->Driver.CurrentSavePrimitive = PRIM_OUTSIDE_BEGIN_END;
1579 save->prims[i].end = 0;
1580 save->prims[i].count = save->vert_count - save->prims[i].start;
1581 }
1582
1583 /* Make sure this vertex list gets replayed by the "loopback"
1584 * mechanism:
1585 */
1586 save->dangling_attr_ref = GL_TRUE;
1587 vbo_save_SaveFlushVertices(ctx);
1588
1589 /* Swap out this vertex format while outside begin/end. Any color,
1590 * etc. received between here and the next begin will be compiled
1591 * as opcodes.
1592 */
1593 _mesa_install_save_vtxfmt(ctx, &ctx->ListState.ListVtxfmt);
1594 }
1595
1596 vbo_save_unmap_vertex_store(ctx, save->vertex_store);
1597
1598 assert(save->vertex_size == 0);
1599 }
1600
1601
1602 /**
1603 * Called from the display list code when we're about to execute a
1604 * display list.
1605 */
1606 void
1607 vbo_save_BeginCallList(struct gl_context *ctx, struct gl_display_list *dlist)
1608 {
1609 struct vbo_save_context *save = &vbo_context(ctx)->save;
1610 save->replay_flags |= dlist->Flags;
1611 }
1612
1613
1614 /**
1615 * Called from the display list code when we're finished executing a
1616 * display list.
1617 */
1618 void
1619 vbo_save_EndCallList(struct gl_context *ctx)
1620 {
1621 struct vbo_save_context *save = &vbo_context(ctx)->save;
1622
1623 if (ctx->ListState.CallDepth == 1)
1624 save->replay_flags = 0;
1625 }
1626
1627
1628 /**
1629 * Called by display list code when a display list is being deleted.
1630 */
1631 static void
1632 vbo_destroy_vertex_list(struct gl_context *ctx, void *data)
1633 {
1634 struct vbo_save_vertex_list *node = (struct vbo_save_vertex_list *) data;
1635
1636 for (gl_vertex_processing_mode vpm = VP_MODE_FF; vpm < VP_MODE_MAX; ++vpm)
1637 _mesa_reference_vao(ctx, &node->VAO[vpm], NULL);
1638
1639 if (--node->prim_store->refcount == 0)
1640 free(node->prim_store);
1641
1642 free(node->current_data);
1643 node->current_data = NULL;
1644 }
1645
1646
1647 static void
1648 vbo_print_vertex_list(struct gl_context *ctx, void *data, FILE *f)
1649 {
1650 struct vbo_save_vertex_list *node = (struct vbo_save_vertex_list *) data;
1651 GLuint i;
1652 struct gl_buffer_object *buffer = node->VAO[0]->BufferBinding[0].BufferObj;
1653 const GLuint vertex_size = _vbo_save_get_stride(node)/sizeof(GLfloat);
1654 (void) ctx;
1655
1656 fprintf(f, "VBO-VERTEX-LIST, %u vertices, %d primitives, %d vertsize, "
1657 "buffer %p\n",
1658 node->vertex_count, node->prim_count, vertex_size,
1659 buffer);
1660
1661 for (i = 0; i < node->prim_count; i++) {
1662 struct _mesa_prim *prim = &node->prims[i];
1663 fprintf(f, " prim %d: %s %d..%d %s %s\n",
1664 i,
1665 _mesa_lookup_prim_by_nr(prim->mode),
1666 prim->start,
1667 prim->start + prim->count,
1668 (prim->begin) ? "BEGIN" : "(wrap)",
1669 (prim->end) ? "END" : "(wrap)");
1670 }
1671 }
1672
1673
1674 /**
1675 * Called during context creation/init.
1676 */
1677 static void
1678 current_init(struct gl_context *ctx)
1679 {
1680 struct vbo_save_context *save = &vbo_context(ctx)->save;
1681 GLint i;
1682
1683 for (i = VBO_ATTRIB_POS; i <= VBO_ATTRIB_GENERIC15; i++) {
1684 const GLuint j = i - VBO_ATTRIB_POS;
1685 assert(j < VERT_ATTRIB_MAX);
1686 save->currentsz[i] = &ctx->ListState.ActiveAttribSize[j];
1687 save->current[i] = (fi_type *) ctx->ListState.CurrentAttrib[j];
1688 }
1689
1690 for (i = VBO_ATTRIB_FIRST_MATERIAL; i <= VBO_ATTRIB_LAST_MATERIAL; i++) {
1691 const GLuint j = i - VBO_ATTRIB_FIRST_MATERIAL;
1692 assert(j < MAT_ATTRIB_MAX);
1693 save->currentsz[i] = &ctx->ListState.ActiveMaterialSize[j];
1694 save->current[i] = (fi_type *) ctx->ListState.CurrentMaterial[j];
1695 }
1696 }
1697
1698
1699 /**
1700 * Initialize the display list compiler. Called during context creation.
1701 */
1702 void
1703 vbo_save_api_init(struct vbo_save_context *save)
1704 {
1705 struct gl_context *ctx = save->ctx;
1706
1707 save->opcode_vertex_list =
1708 _mesa_dlist_alloc_opcode(ctx,
1709 sizeof(struct vbo_save_vertex_list),
1710 vbo_save_playback_vertex_list,
1711 vbo_destroy_vertex_list,
1712 vbo_print_vertex_list);
1713
1714 vtxfmt_init(ctx);
1715 current_init(ctx);
1716 _mesa_noop_vtxfmt_init(ctx, &save->vtxfmt_noop);
1717 }