vbo: don't check ctx->NewState twice in glBegin
[mesa.git] / src / mesa / vbo / vbo_exec_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 #include "main/glheader.h"
34 #include "main/bufferobj.h"
35 #include "main/context.h"
36 #include "main/macros.h"
37 #include "main/vtxfmt.h"
38 #include "main/dlist.h"
39 #include "main/eval.h"
40 #include "main/state.h"
41 #include "main/light.h"
42 #include "main/api_arrayelt.h"
43 #include "main/draw_validate.h"
44 #include "main/dispatch.h"
45 #include "util/bitscan.h"
46
47 #include "vbo_noop.h"
48 #include "vbo_private.h"
49
50
51 /** ID/name for immediate-mode VBO */
52 #define IMM_BUFFER_NAME 0xaabbccdd
53
54
55 static void
56 vbo_reset_all_attr(struct vbo_exec_context *exec);
57
58
59 /**
60 * Close off the last primitive, execute the buffer, restart the
61 * primitive. This is called when we fill a vertex buffer before
62 * hitting glEnd.
63 */
64 static void
65 vbo_exec_wrap_buffers(struct vbo_exec_context *exec)
66 {
67 if (exec->vtx.prim_count == 0) {
68 exec->vtx.copied.nr = 0;
69 exec->vtx.vert_count = 0;
70 exec->vtx.buffer_ptr = exec->vtx.buffer_map;
71 }
72 else {
73 struct _mesa_prim *last_prim = &exec->vtx.prim[exec->vtx.prim_count - 1];
74 const GLuint last_begin = last_prim->begin;
75 GLuint last_count;
76
77 if (_mesa_inside_begin_end(exec->ctx)) {
78 last_prim->count = exec->vtx.vert_count - last_prim->start;
79 }
80
81 last_count = last_prim->count;
82
83 /* Special handling for wrapping GL_LINE_LOOP */
84 if (last_prim->mode == GL_LINE_LOOP &&
85 last_count > 0 &&
86 !last_prim->end) {
87 /* draw this section of the incomplete line loop as a line strip */
88 last_prim->mode = GL_LINE_STRIP;
89 if (!last_prim->begin) {
90 /* This is not the first section of the line loop, so don't
91 * draw the 0th vertex. We're saving it until we draw the
92 * very last section of the loop.
93 */
94 last_prim->start++;
95 last_prim->count--;
96 }
97 }
98
99 /* Execute the buffer and save copied vertices.
100 */
101 if (exec->vtx.vert_count)
102 vbo_exec_vtx_flush(exec, GL_FALSE);
103 else {
104 exec->vtx.prim_count = 0;
105 exec->vtx.copied.nr = 0;
106 }
107
108 /* Emit a glBegin to start the new list.
109 */
110 assert(exec->vtx.prim_count == 0);
111
112 if (_mesa_inside_begin_end(exec->ctx)) {
113 exec->vtx.prim[0].mode = exec->ctx->Driver.CurrentExecPrimitive;
114 exec->vtx.prim[0].begin = 0;
115 exec->vtx.prim[0].end = 0;
116 exec->vtx.prim[0].start = 0;
117 exec->vtx.prim[0].count = 0;
118 exec->vtx.prim_count++;
119
120 if (exec->vtx.copied.nr == last_count)
121 exec->vtx.prim[0].begin = last_begin;
122 }
123 }
124 }
125
126
127 /**
128 * Deal with buffer wrapping where provoked by the vertex buffer
129 * filling up, as opposed to upgrade_vertex().
130 */
131 static void
132 vbo_exec_vtx_wrap(struct vbo_exec_context *exec)
133 {
134 unsigned numComponents;
135
136 /* Run pipeline on current vertices, copy wrapped vertices
137 * to exec->vtx.copied.
138 */
139 vbo_exec_wrap_buffers(exec);
140
141 if (!exec->vtx.buffer_ptr) {
142 /* probably ran out of memory earlier when allocating the VBO */
143 return;
144 }
145
146 /* Copy stored stored vertices to start of new list.
147 */
148 assert(exec->vtx.max_vert - exec->vtx.vert_count > exec->vtx.copied.nr);
149
150 numComponents = exec->vtx.copied.nr * exec->vtx.vertex_size;
151 memcpy(exec->vtx.buffer_ptr,
152 exec->vtx.copied.buffer,
153 numComponents * sizeof(fi_type));
154 exec->vtx.buffer_ptr += numComponents;
155 exec->vtx.vert_count += exec->vtx.copied.nr;
156
157 exec->vtx.copied.nr = 0;
158 }
159
160
161 /**
162 * Copy the active vertex's values to the ctx->Current fields.
163 */
164 static void
165 vbo_exec_copy_to_current(struct vbo_exec_context *exec)
166 {
167 struct gl_context *ctx = exec->ctx;
168 struct vbo_context *vbo = vbo_context(ctx);
169 GLbitfield64 enabled = exec->vtx.enabled & (~BITFIELD64_BIT(VBO_ATTRIB_POS));
170
171 while (enabled) {
172 const int i = u_bit_scan64(&enabled);
173
174 /* Note: the exec->vtx.current[i] pointers point into the
175 * ctx->Current.Attrib and ctx->Light.Material.Attrib arrays.
176 */
177 GLfloat *current = (GLfloat *)vbo->current[i].Ptr;
178 fi_type tmp[8]; /* space for doubles */
179 int dmul = 1;
180
181 if (exec->vtx.attr[i].type == GL_DOUBLE ||
182 exec->vtx.attr[i].type == GL_UNSIGNED_INT64_ARB)
183 dmul = 2;
184
185 assert(exec->vtx.attr[i].size);
186
187 if (exec->vtx.attr[i].type == GL_DOUBLE ||
188 exec->vtx.attr[i].type == GL_UNSIGNED_INT64_ARB) {
189 memset(tmp, 0, sizeof(tmp));
190 memcpy(tmp, exec->vtx.attrptr[i], exec->vtx.attr[i].size * sizeof(GLfloat));
191 } else {
192 COPY_CLEAN_4V_TYPE_AS_UNION(tmp,
193 exec->vtx.attr[i].size,
194 exec->vtx.attrptr[i],
195 exec->vtx.attr[i].type);
196 }
197
198 if (exec->vtx.attr[i].type != vbo->current[i].Format.Type ||
199 memcmp(current, tmp, 4 * sizeof(GLfloat) * dmul) != 0) {
200 memcpy(current, tmp, 4 * sizeof(GLfloat) * dmul);
201
202 /* Given that we explicitly state size here, there is no need
203 * for the COPY_CLEAN above, could just copy 16 bytes and be
204 * done. The only problem is when Mesa accesses ctx->Current
205 * directly.
206 */
207 /* Size here is in components - not bytes */
208 vbo_set_vertex_format(&vbo->current[i].Format,
209 exec->vtx.attr[i].size / dmul,
210 exec->vtx.attr[i].type);
211
212 /* This triggers rather too much recalculation of Mesa state
213 * that doesn't get used (eg light positions).
214 */
215 if (i >= VBO_ATTRIB_MAT_FRONT_AMBIENT &&
216 i <= VBO_ATTRIB_MAT_BACK_INDEXES)
217 ctx->NewState |= _NEW_LIGHT;
218
219 ctx->NewState |= _NEW_CURRENT_ATTRIB;
220 }
221 }
222
223 /* Colormaterial -- this kindof sucks.
224 */
225 if (ctx->Light.ColorMaterialEnabled &&
226 exec->vtx.attr[VBO_ATTRIB_COLOR0].size) {
227 _mesa_update_color_material(ctx,
228 ctx->Current.Attrib[VBO_ATTRIB_COLOR0]);
229 }
230 }
231
232
233 /**
234 * Copy current vertex attribute values into the current vertex.
235 */
236 static void
237 vbo_exec_copy_from_current(struct vbo_exec_context *exec)
238 {
239 struct gl_context *ctx = exec->ctx;
240 struct vbo_context *vbo = vbo_context(ctx);
241 GLint i;
242
243 for (i = VBO_ATTRIB_POS + 1; i < VBO_ATTRIB_MAX; i++) {
244 if (exec->vtx.attr[i].type == GL_DOUBLE ||
245 exec->vtx.attr[i].type == GL_UNSIGNED_INT64_ARB) {
246 memcpy(exec->vtx.attrptr[i], vbo->current[i].Ptr,
247 exec->vtx.attr[i].size * sizeof(GLfloat));
248 } else {
249 const fi_type *current = (fi_type *) vbo->current[i].Ptr;
250 switch (exec->vtx.attr[i].size) {
251 case 4: exec->vtx.attrptr[i][3] = current[3];
252 case 3: exec->vtx.attrptr[i][2] = current[2];
253 case 2: exec->vtx.attrptr[i][1] = current[1];
254 case 1: exec->vtx.attrptr[i][0] = current[0];
255 break;
256 }
257 }
258 }
259 }
260
261
262 /**
263 * Flush existing data, set new attrib size, replay copied vertices.
264 * This is called when we transition from a small vertex attribute size
265 * to a larger one. Ex: glTexCoord2f -> glTexCoord4f.
266 * We need to go back over the previous 2-component texcoords and insert
267 * zero and one values.
268 * \param attr VBO_ATTRIB_x vertex attribute value
269 */
270 static void
271 vbo_exec_wrap_upgrade_vertex(struct vbo_exec_context *exec,
272 GLuint attr, GLuint newSize)
273 {
274 struct gl_context *ctx = exec->ctx;
275 struct vbo_context *vbo = vbo_context(ctx);
276 const GLint lastcount = exec->vtx.vert_count;
277 fi_type *old_attrptr[VBO_ATTRIB_MAX];
278 const GLuint old_vtx_size = exec->vtx.vertex_size; /* floats per vertex */
279 const GLuint oldSize = exec->vtx.attr[attr].size;
280 GLuint i;
281
282 assert(attr < VBO_ATTRIB_MAX);
283
284 /* Run pipeline on current vertices, copy wrapped vertices
285 * to exec->vtx.copied.
286 */
287 vbo_exec_wrap_buffers(exec);
288
289 if (unlikely(exec->vtx.copied.nr)) {
290 /* We're in the middle of a primitive, keep the old vertex
291 * format around to be able to translate the copied vertices to
292 * the new format.
293 */
294 memcpy(old_attrptr, exec->vtx.attrptr, sizeof(old_attrptr));
295 }
296
297 if (unlikely(oldSize)) {
298 /* Do a COPY_TO_CURRENT to ensure back-copying works for the
299 * case when the attribute already exists in the vertex and is
300 * having its size increased.
301 */
302 vbo_exec_copy_to_current(exec);
303 }
304
305 /* Heuristic: Attempt to isolate attributes received outside
306 * begin/end so that they don't bloat the vertices.
307 */
308 if (!_mesa_inside_begin_end(ctx) &&
309 !oldSize && lastcount > 8 && exec->vtx.vertex_size) {
310 vbo_exec_copy_to_current(exec);
311 vbo_reset_all_attr(exec);
312 }
313
314 /* Fix up sizes:
315 */
316 exec->vtx.attr[attr].size = newSize;
317 exec->vtx.vertex_size += newSize - oldSize;
318 exec->vtx.max_vert = vbo_compute_max_verts(exec);
319 exec->vtx.vert_count = 0;
320 exec->vtx.buffer_ptr = exec->vtx.buffer_map;
321 exec->vtx.enabled |= BITFIELD64_BIT(attr);
322
323 if (unlikely(oldSize)) {
324 /* Size changed, recalculate all the attrptr[] values
325 */
326 fi_type *tmp = exec->vtx.vertex;
327
328 for (i = 0 ; i < VBO_ATTRIB_MAX ; i++) {
329 if (exec->vtx.attr[i].size) {
330 exec->vtx.attrptr[i] = tmp;
331 tmp += exec->vtx.attr[i].size;
332 }
333 else
334 exec->vtx.attrptr[i] = NULL; /* will not be dereferenced */
335 }
336
337 /* Copy from current to repopulate the vertex with correct
338 * values.
339 */
340 vbo_exec_copy_from_current(exec);
341 }
342 else {
343 /* Just have to append the new attribute at the end */
344 exec->vtx.attrptr[attr] = exec->vtx.vertex +
345 exec->vtx.vertex_size - newSize;
346 }
347
348 /* Replay stored vertices to translate them
349 * to new format here.
350 *
351 * -- No need to replay - just copy piecewise
352 */
353 if (unlikely(exec->vtx.copied.nr)) {
354 fi_type *data = exec->vtx.copied.buffer;
355 fi_type *dest = exec->vtx.buffer_ptr;
356
357 assert(exec->vtx.buffer_ptr == exec->vtx.buffer_map);
358
359 for (i = 0 ; i < exec->vtx.copied.nr ; i++) {
360 GLbitfield64 enabled = exec->vtx.enabled;
361 while (enabled) {
362 const int j = u_bit_scan64(&enabled);
363 GLuint sz = exec->vtx.attr[j].size;
364 GLint old_offset = old_attrptr[j] - exec->vtx.vertex;
365 GLint new_offset = exec->vtx.attrptr[j] - exec->vtx.vertex;
366
367 assert(sz);
368
369 if (j == attr) {
370 if (oldSize) {
371 fi_type tmp[4];
372 COPY_CLEAN_4V_TYPE_AS_UNION(tmp, oldSize,
373 data + old_offset,
374 exec->vtx.attr[j].type);
375 COPY_SZ_4V(dest + new_offset, newSize, tmp);
376 } else {
377 fi_type *current = (fi_type *)vbo->current[j].Ptr;
378 COPY_SZ_4V(dest + new_offset, sz, current);
379 }
380 }
381 else {
382 COPY_SZ_4V(dest + new_offset, sz, data + old_offset);
383 }
384 }
385
386 data += old_vtx_size;
387 dest += exec->vtx.vertex_size;
388 }
389
390 exec->vtx.buffer_ptr = dest;
391 exec->vtx.vert_count += exec->vtx.copied.nr;
392 exec->vtx.copied.nr = 0;
393 }
394 }
395
396
397 /**
398 * This is when a vertex attribute transitions to a different size.
399 * For example, we saw a bunch of glTexCoord2f() calls and now we got a
400 * glTexCoord4f() call. We promote the array from size=2 to size=4.
401 * \param newSize size of new vertex (number of 32-bit words).
402 * \param attr VBO_ATTRIB_x vertex attribute value
403 */
404 static void
405 vbo_exec_fixup_vertex(struct gl_context *ctx, GLuint attr,
406 GLuint newSize, GLenum newType)
407 {
408 struct vbo_exec_context *exec = &vbo_context(ctx)->exec;
409
410 assert(attr < VBO_ATTRIB_MAX);
411
412 if (newSize > exec->vtx.attr[attr].size ||
413 newType != exec->vtx.attr[attr].type) {
414 /* New size is larger. Need to flush existing vertices and get
415 * an enlarged vertex format.
416 */
417 vbo_exec_wrap_upgrade_vertex(exec, attr, newSize);
418 }
419 else if (newSize < exec->vtx.attr[attr].active_size) {
420 GLuint i;
421 const fi_type *id =
422 vbo_get_default_vals_as_union(exec->vtx.attr[attr].type);
423
424 /* New size is smaller - just need to fill in some
425 * zeros. Don't need to flush or wrap.
426 */
427 for (i = newSize; i <= exec->vtx.attr[attr].size; i++)
428 exec->vtx.attrptr[attr][i-1] = id[i-1];
429 }
430
431 exec->vtx.attr[attr].active_size = newSize;
432 exec->vtx.attr[attr].type = newType;
433
434 /* Does setting NeedFlush belong here? Necessitates resetting
435 * vtxfmt on each flush (otherwise flags won't get reset
436 * afterwards).
437 */
438 if (attr == 0)
439 ctx->Driver.NeedFlush |= FLUSH_STORED_VERTICES;
440 }
441
442
443 /**
444 * If index=0, does glVertexAttrib*() alias glVertex() to emit a vertex?
445 * It depends on a few things, including whether we're inside or outside
446 * of glBegin/glEnd.
447 */
448 static inline bool
449 is_vertex_position(const struct gl_context *ctx, GLuint index)
450 {
451 return (index == 0 &&
452 _mesa_attr_zero_aliases_vertex(ctx) &&
453 _mesa_inside_begin_end(ctx));
454 }
455
456
457 /**
458 * This macro is used to implement all the glVertex, glColor, glTexCoord,
459 * glVertexAttrib, etc functions.
460 * \param A VBO_ATTRIB_x attribute index
461 * \param N attribute size (1..4)
462 * \param T type (GL_FLOAT, GL_DOUBLE, GL_INT, GL_UNSIGNED_INT)
463 * \param C cast type (fi_type or double)
464 * \param V0, V1, v2, V3 attribute value
465 */
466 #define ATTR_UNION(A, N, T, C, V0, V1, V2, V3) \
467 do { \
468 struct vbo_exec_context *exec = &vbo_context(ctx)->exec; \
469 int sz = (sizeof(C) / sizeof(GLfloat)); \
470 \
471 assert(sz == 1 || sz == 2); \
472 \
473 /* check if attribute size or type is changing */ \
474 if (unlikely(exec->vtx.attr[A].active_size != N * sz) || \
475 unlikely(exec->vtx.attr[A].type != T)) { \
476 vbo_exec_fixup_vertex(ctx, A, N * sz, T); \
477 } \
478 \
479 /* store vertex attribute in vertex buffer */ \
480 { \
481 C *dest = (C *)exec->vtx.attrptr[A]; \
482 if (N>0) dest[0] = V0; \
483 if (N>1) dest[1] = V1; \
484 if (N>2) dest[2] = V2; \
485 if (N>3) dest[3] = V3; \
486 assert(exec->vtx.attr[A].type == T); \
487 } \
488 \
489 if ((A) == 0) { \
490 /* This is a glVertex call */ \
491 GLuint i; \
492 \
493 if (unlikely(!exec->vtx.buffer_ptr)) { \
494 vbo_exec_vtx_map(exec); \
495 } \
496 assert(exec->vtx.buffer_ptr); \
497 \
498 /* copy 32-bit words */ \
499 for (i = 0; i < exec->vtx.vertex_size; i++) \
500 exec->vtx.buffer_ptr[i] = exec->vtx.vertex[i]; \
501 \
502 exec->vtx.buffer_ptr += exec->vtx.vertex_size; \
503 \
504 /* Set FLUSH_STORED_VERTICES to indicate that there's now */ \
505 /* something to draw (not just updating a color or texcoord).*/ \
506 ctx->Driver.NeedFlush |= FLUSH_UPDATE_CURRENT | \
507 FLUSH_STORED_VERTICES; \
508 \
509 if (++exec->vtx.vert_count >= exec->vtx.max_vert) \
510 vbo_exec_vtx_wrap(exec); \
511 } else { \
512 /* we now have accumulated per-vertex attributes */ \
513 ctx->Driver.NeedFlush |= FLUSH_UPDATE_CURRENT; \
514 } \
515 } while (0)
516
517
518 #undef ERROR
519 #define ERROR(err) _mesa_error(ctx, err, __func__)
520 #define TAG(x) vbo_exec_##x
521
522 #include "vbo_attrib_tmp.h"
523
524
525
526 /**
527 * Execute a glMaterial call. Note that if GL_COLOR_MATERIAL is enabled,
528 * this may be a (partial) no-op.
529 */
530 static void GLAPIENTRY
531 vbo_exec_Materialfv(GLenum face, GLenum pname, const GLfloat *params)
532 {
533 GLbitfield updateMats;
534 GET_CURRENT_CONTEXT(ctx);
535
536 /* This function should be a no-op when it tries to update material
537 * attributes which are currently tracking glColor via glColorMaterial.
538 * The updateMats var will be a mask of the MAT_BIT_FRONT/BACK_x bits
539 * indicating which material attributes can actually be updated below.
540 */
541 if (ctx->Light.ColorMaterialEnabled) {
542 updateMats = ~ctx->Light._ColorMaterialBitmask;
543 }
544 else {
545 /* GL_COLOR_MATERIAL is disabled so don't skip any material updates */
546 updateMats = ALL_MATERIAL_BITS;
547 }
548
549 if (ctx->API == API_OPENGL_COMPAT && face == GL_FRONT) {
550 updateMats &= FRONT_MATERIAL_BITS;
551 }
552 else if (ctx->API == API_OPENGL_COMPAT && face == GL_BACK) {
553 updateMats &= BACK_MATERIAL_BITS;
554 }
555 else if (face != GL_FRONT_AND_BACK) {
556 _mesa_error(ctx, GL_INVALID_ENUM, "glMaterial(invalid face)");
557 return;
558 }
559
560 switch (pname) {
561 case GL_EMISSION:
562 if (updateMats & MAT_BIT_FRONT_EMISSION)
563 MAT_ATTR(VBO_ATTRIB_MAT_FRONT_EMISSION, 4, params);
564 if (updateMats & MAT_BIT_BACK_EMISSION)
565 MAT_ATTR(VBO_ATTRIB_MAT_BACK_EMISSION, 4, params);
566 break;
567 case GL_AMBIENT:
568 if (updateMats & MAT_BIT_FRONT_AMBIENT)
569 MAT_ATTR(VBO_ATTRIB_MAT_FRONT_AMBIENT, 4, params);
570 if (updateMats & MAT_BIT_BACK_AMBIENT)
571 MAT_ATTR(VBO_ATTRIB_MAT_BACK_AMBIENT, 4, params);
572 break;
573 case GL_DIFFUSE:
574 if (updateMats & MAT_BIT_FRONT_DIFFUSE)
575 MAT_ATTR(VBO_ATTRIB_MAT_FRONT_DIFFUSE, 4, params);
576 if (updateMats & MAT_BIT_BACK_DIFFUSE)
577 MAT_ATTR(VBO_ATTRIB_MAT_BACK_DIFFUSE, 4, params);
578 break;
579 case GL_SPECULAR:
580 if (updateMats & MAT_BIT_FRONT_SPECULAR)
581 MAT_ATTR(VBO_ATTRIB_MAT_FRONT_SPECULAR, 4, params);
582 if (updateMats & MAT_BIT_BACK_SPECULAR)
583 MAT_ATTR(VBO_ATTRIB_MAT_BACK_SPECULAR, 4, params);
584 break;
585 case GL_SHININESS:
586 if (*params < 0 || *params > ctx->Const.MaxShininess) {
587 _mesa_error(ctx, GL_INVALID_VALUE,
588 "glMaterial(invalid shininess: %f out range [0, %f])",
589 *params, ctx->Const.MaxShininess);
590 return;
591 }
592 if (updateMats & MAT_BIT_FRONT_SHININESS)
593 MAT_ATTR(VBO_ATTRIB_MAT_FRONT_SHININESS, 1, params);
594 if (updateMats & MAT_BIT_BACK_SHININESS)
595 MAT_ATTR(VBO_ATTRIB_MAT_BACK_SHININESS, 1, params);
596 break;
597 case GL_COLOR_INDEXES:
598 if (ctx->API != API_OPENGL_COMPAT) {
599 _mesa_error(ctx, GL_INVALID_ENUM, "glMaterialfv(pname)");
600 return;
601 }
602 if (updateMats & MAT_BIT_FRONT_INDEXES)
603 MAT_ATTR(VBO_ATTRIB_MAT_FRONT_INDEXES, 3, params);
604 if (updateMats & MAT_BIT_BACK_INDEXES)
605 MAT_ATTR(VBO_ATTRIB_MAT_BACK_INDEXES, 3, params);
606 break;
607 case GL_AMBIENT_AND_DIFFUSE:
608 if (updateMats & MAT_BIT_FRONT_AMBIENT)
609 MAT_ATTR(VBO_ATTRIB_MAT_FRONT_AMBIENT, 4, params);
610 if (updateMats & MAT_BIT_FRONT_DIFFUSE)
611 MAT_ATTR(VBO_ATTRIB_MAT_FRONT_DIFFUSE, 4, params);
612 if (updateMats & MAT_BIT_BACK_AMBIENT)
613 MAT_ATTR(VBO_ATTRIB_MAT_BACK_AMBIENT, 4, params);
614 if (updateMats & MAT_BIT_BACK_DIFFUSE)
615 MAT_ATTR(VBO_ATTRIB_MAT_BACK_DIFFUSE, 4, params);
616 break;
617 default:
618 _mesa_error(ctx, GL_INVALID_ENUM, "glMaterialfv(pname)");
619 return;
620 }
621 }
622
623
624 /**
625 * Flush (draw) vertices.
626 * \param unmap - leave VBO unmapped after flushing?
627 */
628 static void
629 vbo_exec_FlushVertices_internal(struct vbo_exec_context *exec, GLboolean unmap)
630 {
631 if (exec->vtx.vert_count || unmap) {
632 vbo_exec_vtx_flush(exec, unmap);
633 }
634
635 if (exec->vtx.vertex_size) {
636 vbo_exec_copy_to_current(exec);
637 vbo_reset_all_attr(exec);
638 }
639 }
640
641
642 static void GLAPIENTRY
643 vbo_exec_EvalCoord1f(GLfloat u)
644 {
645 GET_CURRENT_CONTEXT(ctx);
646 struct vbo_exec_context *exec = &vbo_context(ctx)->exec;
647
648 {
649 GLint i;
650 if (exec->eval.recalculate_maps)
651 vbo_exec_eval_update(exec);
652
653 for (i = 0; i <= VBO_ATTRIB_TEX7; i++) {
654 if (exec->eval.map1[i].map)
655 if (exec->vtx.attr[i].active_size != exec->eval.map1[i].sz)
656 vbo_exec_fixup_vertex(ctx, i, exec->eval.map1[i].sz, GL_FLOAT);
657 }
658 }
659
660 memcpy(exec->vtx.copied.buffer, exec->vtx.vertex,
661 exec->vtx.vertex_size * sizeof(GLfloat));
662
663 vbo_exec_do_EvalCoord1f(exec, u);
664
665 memcpy(exec->vtx.vertex, exec->vtx.copied.buffer,
666 exec->vtx.vertex_size * sizeof(GLfloat));
667 }
668
669
670 static void GLAPIENTRY
671 vbo_exec_EvalCoord2f(GLfloat u, GLfloat v)
672 {
673 GET_CURRENT_CONTEXT(ctx);
674 struct vbo_exec_context *exec = &vbo_context(ctx)->exec;
675
676 {
677 GLint i;
678 if (exec->eval.recalculate_maps)
679 vbo_exec_eval_update(exec);
680
681 for (i = 0; i <= VBO_ATTRIB_TEX7; i++) {
682 if (exec->eval.map2[i].map)
683 if (exec->vtx.attr[i].active_size != exec->eval.map2[i].sz)
684 vbo_exec_fixup_vertex(ctx, i, exec->eval.map2[i].sz, GL_FLOAT);
685 }
686
687 if (ctx->Eval.AutoNormal)
688 if (exec->vtx.attr[VBO_ATTRIB_NORMAL].active_size != 3)
689 vbo_exec_fixup_vertex(ctx, VBO_ATTRIB_NORMAL, 3, GL_FLOAT);
690 }
691
692 memcpy(exec->vtx.copied.buffer, exec->vtx.vertex,
693 exec->vtx.vertex_size * sizeof(GLfloat));
694
695 vbo_exec_do_EvalCoord2f(exec, u, v);
696
697 memcpy(exec->vtx.vertex, exec->vtx.copied.buffer,
698 exec->vtx.vertex_size * sizeof(GLfloat));
699 }
700
701
702 static void GLAPIENTRY
703 vbo_exec_EvalCoord1fv(const GLfloat *u)
704 {
705 vbo_exec_EvalCoord1f(u[0]);
706 }
707
708
709 static void GLAPIENTRY
710 vbo_exec_EvalCoord2fv(const GLfloat *u)
711 {
712 vbo_exec_EvalCoord2f(u[0], u[1]);
713 }
714
715
716 static void GLAPIENTRY
717 vbo_exec_EvalPoint1(GLint i)
718 {
719 GET_CURRENT_CONTEXT(ctx);
720 GLfloat du = ((ctx->Eval.MapGrid1u2 - ctx->Eval.MapGrid1u1) /
721 (GLfloat) ctx->Eval.MapGrid1un);
722 GLfloat u = i * du + ctx->Eval.MapGrid1u1;
723
724 vbo_exec_EvalCoord1f(u);
725 }
726
727
728 static void GLAPIENTRY
729 vbo_exec_EvalPoint2(GLint i, GLint j)
730 {
731 GET_CURRENT_CONTEXT(ctx);
732 GLfloat du = ((ctx->Eval.MapGrid2u2 - ctx->Eval.MapGrid2u1) /
733 (GLfloat) ctx->Eval.MapGrid2un);
734 GLfloat dv = ((ctx->Eval.MapGrid2v2 - ctx->Eval.MapGrid2v1) /
735 (GLfloat) ctx->Eval.MapGrid2vn);
736 GLfloat u = i * du + ctx->Eval.MapGrid2u1;
737 GLfloat v = j * dv + ctx->Eval.MapGrid2v1;
738
739 vbo_exec_EvalCoord2f(u, v);
740 }
741
742
743 /**
744 * Called via glBegin.
745 */
746 static void GLAPIENTRY
747 vbo_exec_Begin(GLenum mode)
748 {
749 GET_CURRENT_CONTEXT(ctx);
750 struct vbo_context *vbo = vbo_context(ctx);
751 struct vbo_exec_context *exec = &vbo->exec;
752 int i;
753
754 if (_mesa_inside_begin_end(ctx)) {
755 _mesa_error(ctx, GL_INVALID_OPERATION, "glBegin");
756 return;
757 }
758
759 if (!_mesa_valid_prim_mode(ctx, mode, "glBegin")) {
760 return;
761 }
762
763 if (!_mesa_valid_to_render(ctx, "glBegin")) {
764 return;
765 }
766
767 /* Heuristic: attempt to isolate attributes occurring outside
768 * begin/end pairs.
769 */
770 if (exec->vtx.vertex_size && !exec->vtx.attr[VBO_ATTRIB_POS].size)
771 vbo_exec_FlushVertices_internal(exec, GL_FALSE);
772
773 i = exec->vtx.prim_count++;
774 exec->vtx.prim[i].mode = mode;
775 exec->vtx.prim[i].begin = 1;
776 exec->vtx.prim[i].end = 0;
777 exec->vtx.prim[i].indexed = 0;
778 exec->vtx.prim[i].pad = 0;
779 exec->vtx.prim[i].start = exec->vtx.vert_count;
780 exec->vtx.prim[i].count = 0;
781 exec->vtx.prim[i].num_instances = 1;
782 exec->vtx.prim[i].base_instance = 0;
783 exec->vtx.prim[i].is_indirect = 0;
784
785 ctx->Driver.CurrentExecPrimitive = mode;
786
787 ctx->Exec = ctx->BeginEnd;
788
789 /* We may have been called from a display list, in which case we should
790 * leave dlist.c's dispatch table in place.
791 */
792 if (ctx->CurrentClientDispatch == ctx->MarshalExec) {
793 ctx->CurrentServerDispatch = ctx->Exec;
794 } else if (ctx->CurrentClientDispatch == ctx->OutsideBeginEnd) {
795 ctx->CurrentClientDispatch = ctx->Exec;
796 _glapi_set_dispatch(ctx->CurrentClientDispatch);
797 } else {
798 assert(ctx->CurrentClientDispatch == ctx->Save);
799 }
800 }
801
802
803 /**
804 * Try to merge / concatenate the two most recent VBO primitives.
805 */
806 static void
807 try_vbo_merge(struct vbo_exec_context *exec)
808 {
809 struct _mesa_prim *cur = &exec->vtx.prim[exec->vtx.prim_count - 1];
810
811 assert(exec->vtx.prim_count >= 1);
812
813 vbo_try_prim_conversion(cur);
814
815 if (exec->vtx.prim_count >= 2) {
816 struct _mesa_prim *prev = &exec->vtx.prim[exec->vtx.prim_count - 2];
817 assert(prev == cur - 1);
818
819 if (vbo_can_merge_prims(prev, cur)) {
820 assert(cur->begin);
821 assert(cur->end);
822 assert(prev->begin);
823 assert(prev->end);
824 vbo_merge_prims(prev, cur);
825 exec->vtx.prim_count--; /* drop the last primitive */
826 }
827 }
828 }
829
830
831 /**
832 * Called via glEnd.
833 */
834 static void GLAPIENTRY
835 vbo_exec_End(void)
836 {
837 GET_CURRENT_CONTEXT(ctx);
838 struct vbo_exec_context *exec = &vbo_context(ctx)->exec;
839
840 if (!_mesa_inside_begin_end(ctx)) {
841 _mesa_error(ctx, GL_INVALID_OPERATION, "glEnd");
842 return;
843 }
844
845 ctx->Exec = ctx->OutsideBeginEnd;
846
847 if (ctx->CurrentClientDispatch == ctx->MarshalExec) {
848 ctx->CurrentServerDispatch = ctx->Exec;
849 } else if (ctx->CurrentClientDispatch == ctx->BeginEnd) {
850 ctx->CurrentClientDispatch = ctx->Exec;
851 _glapi_set_dispatch(ctx->CurrentClientDispatch);
852 }
853
854 if (exec->vtx.prim_count > 0) {
855 /* close off current primitive */
856 struct _mesa_prim *last_prim = &exec->vtx.prim[exec->vtx.prim_count - 1];
857
858 last_prim->end = 1;
859 last_prim->count = exec->vtx.vert_count - last_prim->start;
860
861 /* Special handling for GL_LINE_LOOP */
862 if (last_prim->mode == GL_LINE_LOOP && last_prim->begin == 0) {
863 /* We're finishing drawing a line loop. Append 0th vertex onto
864 * end of vertex buffer so we can draw it as a line strip.
865 */
866 const fi_type *src = exec->vtx.buffer_map +
867 last_prim->start * exec->vtx.vertex_size;
868 fi_type *dst = exec->vtx.buffer_map +
869 exec->vtx.vert_count * exec->vtx.vertex_size;
870
871 /* copy 0th vertex to end of buffer */
872 memcpy(dst, src, exec->vtx.vertex_size * sizeof(fi_type));
873
874 last_prim->start++; /* skip vertex0 */
875 /* note that last_prim->count stays unchanged */
876 last_prim->mode = GL_LINE_STRIP;
877
878 /* Increment the vertex count so the next primitive doesn't
879 * overwrite the last vertex which we just added.
880 */
881 exec->vtx.vert_count++;
882 exec->vtx.buffer_ptr += exec->vtx.vertex_size;
883 }
884
885 try_vbo_merge(exec);
886 }
887
888 ctx->Driver.CurrentExecPrimitive = PRIM_OUTSIDE_BEGIN_END;
889
890 if (exec->vtx.prim_count == VBO_MAX_PRIM)
891 vbo_exec_vtx_flush(exec, GL_FALSE);
892
893 if (MESA_DEBUG_FLAGS & DEBUG_ALWAYS_FLUSH) {
894 _mesa_flush(ctx);
895 }
896 }
897
898
899 /**
900 * Called via glPrimitiveRestartNV()
901 */
902 static void GLAPIENTRY
903 vbo_exec_PrimitiveRestartNV(void)
904 {
905 GLenum curPrim;
906 GET_CURRENT_CONTEXT(ctx);
907
908 curPrim = ctx->Driver.CurrentExecPrimitive;
909
910 if (curPrim == PRIM_OUTSIDE_BEGIN_END) {
911 _mesa_error(ctx, GL_INVALID_OPERATION, "glPrimitiveRestartNV");
912 }
913 else {
914 vbo_exec_End();
915 vbo_exec_Begin(curPrim);
916 }
917 }
918
919
920 static void
921 vbo_exec_vtxfmt_init(struct vbo_exec_context *exec)
922 {
923 struct gl_context *ctx = exec->ctx;
924 GLvertexformat *vfmt = &exec->vtxfmt;
925
926 #define NAME_AE(x) _ae_##x
927 #define NAME_CALLLIST(x) _mesa_##x
928 #define NAME(x) vbo_exec_##x
929 #define NAME_ES(x) _es_##x
930
931 #include "vbo_init_tmp.h"
932 }
933
934
935 /**
936 * Tell the VBO module to use a real OpenGL vertex buffer object to
937 * store accumulated immediate-mode vertex data.
938 * This replaces the malloced buffer which was created in
939 * vb_exec_vtx_init() below.
940 */
941 void
942 vbo_use_buffer_objects(struct gl_context *ctx)
943 {
944 struct vbo_exec_context *exec = &vbo_context(ctx)->exec;
945 /* Any buffer name but 0 can be used here since this bufferobj won't
946 * go into the bufferobj hashtable.
947 */
948 GLuint bufName = IMM_BUFFER_NAME;
949
950 /* Make sure this func is only used once */
951 assert(exec->vtx.bufferobj == ctx->Shared->NullBufferObj);
952
953 _mesa_align_free(exec->vtx.buffer_map);
954 exec->vtx.buffer_map = NULL;
955 exec->vtx.buffer_ptr = NULL;
956
957 /* Allocate a real buffer object now */
958 _mesa_reference_buffer_object(ctx, &exec->vtx.bufferobj, NULL);
959 exec->vtx.bufferobj = ctx->Driver.NewBufferObject(ctx, bufName);
960 }
961
962
963 void
964 vbo_exec_vtx_init(struct vbo_exec_context *exec)
965 {
966 struct gl_context *ctx = exec->ctx;
967 GLuint i;
968
969 /* Allocate a buffer object. Will just reuse this object
970 * continuously, unless vbo_use_buffer_objects() is called to enable
971 * use of real VBOs.
972 */
973 _mesa_reference_buffer_object(ctx,
974 &exec->vtx.bufferobj,
975 ctx->Shared->NullBufferObj);
976
977 assert(!exec->vtx.buffer_map);
978 exec->vtx.buffer_map = _mesa_align_malloc(VBO_VERT_BUFFER_SIZE, 64);
979 exec->vtx.buffer_ptr = exec->vtx.buffer_map;
980
981 vbo_exec_vtxfmt_init(exec);
982 _mesa_noop_vtxfmt_init(ctx, &exec->vtxfmt_noop);
983
984 exec->vtx.enabled = 0;
985 for (i = 0 ; i < ARRAY_SIZE(exec->vtx.attr); i++) {
986 exec->vtx.attr[i].size = 0;
987 exec->vtx.attr[i].type = GL_FLOAT;
988 exec->vtx.attr[i].active_size = 0;
989 }
990
991 exec->vtx.vertex_size = 0;
992 }
993
994
995 void
996 vbo_exec_vtx_destroy(struct vbo_exec_context *exec)
997 {
998 /* using a real VBO for vertex data */
999 struct gl_context *ctx = exec->ctx;
1000
1001 /* True VBOs should already be unmapped
1002 */
1003 if (exec->vtx.buffer_map) {
1004 assert(exec->vtx.bufferobj->Name == 0 ||
1005 exec->vtx.bufferobj->Name == IMM_BUFFER_NAME);
1006 if (exec->vtx.bufferobj->Name == 0) {
1007 _mesa_align_free(exec->vtx.buffer_map);
1008 exec->vtx.buffer_map = NULL;
1009 exec->vtx.buffer_ptr = NULL;
1010 }
1011 }
1012
1013 /* Free the vertex buffer. Unmap first if needed.
1014 */
1015 if (_mesa_bufferobj_mapped(exec->vtx.bufferobj, MAP_INTERNAL)) {
1016 ctx->Driver.UnmapBuffer(ctx, exec->vtx.bufferobj, MAP_INTERNAL);
1017 }
1018 _mesa_reference_buffer_object(ctx, &exec->vtx.bufferobj, NULL);
1019 }
1020
1021
1022 /**
1023 * If inside glBegin()/glEnd(), it should assert(0). Otherwise, if
1024 * FLUSH_STORED_VERTICES bit in \p flags is set flushes any buffered
1025 * vertices, if FLUSH_UPDATE_CURRENT bit is set updates
1026 * __struct gl_contextRec::Current and gl_light_attrib::Material
1027 *
1028 * Note that the default T&L engine never clears the
1029 * FLUSH_UPDATE_CURRENT bit, even after performing the update.
1030 *
1031 * \param flags bitmask of FLUSH_STORED_VERTICES, FLUSH_UPDATE_CURRENT
1032 */
1033 void
1034 vbo_exec_FlushVertices(struct gl_context *ctx, GLuint flags)
1035 {
1036 struct vbo_exec_context *exec = &vbo_context(ctx)->exec;
1037
1038 #ifndef NDEBUG
1039 /* debug check: make sure we don't get called recursively */
1040 exec->flush_call_depth++;
1041 assert(exec->flush_call_depth == 1);
1042 #endif
1043
1044 if (_mesa_inside_begin_end(ctx)) {
1045 /* We've had glBegin but not glEnd! */
1046 #ifndef NDEBUG
1047 exec->flush_call_depth--;
1048 assert(exec->flush_call_depth == 0);
1049 #endif
1050 return;
1051 }
1052
1053 /* Flush (draw), and make sure VBO is left unmapped when done */
1054 vbo_exec_FlushVertices_internal(exec, GL_TRUE);
1055
1056 /* Clear the dirty flush flags, because the flush is finished. */
1057 ctx->Driver.NeedFlush &= ~(FLUSH_UPDATE_CURRENT | flags);
1058
1059 #ifndef NDEBUG
1060 exec->flush_call_depth--;
1061 assert(exec->flush_call_depth == 0);
1062 #endif
1063 }
1064
1065
1066 /**
1067 * Reset the vertex attribute by setting its size to zero.
1068 */
1069 static void
1070 vbo_reset_attr(struct vbo_exec_context *exec, GLuint attr)
1071 {
1072 exec->vtx.attr[attr].size = 0;
1073 exec->vtx.attr[attr].type = GL_FLOAT;
1074 exec->vtx.attr[attr].active_size = 0;
1075 }
1076
1077
1078 static void
1079 vbo_reset_all_attr(struct vbo_exec_context *exec)
1080 {
1081 while (exec->vtx.enabled) {
1082 const int i = u_bit_scan64(&exec->vtx.enabled);
1083 vbo_reset_attr(exec, i);
1084 }
1085
1086 exec->vtx.vertex_size = 0;
1087 }
1088
1089
1090 void GLAPIENTRY
1091 _es_Color4f(GLfloat r, GLfloat g, GLfloat b, GLfloat a)
1092 {
1093 vbo_exec_Color4f(r, g, b, a);
1094 }
1095
1096
1097 void GLAPIENTRY
1098 _es_Normal3f(GLfloat x, GLfloat y, GLfloat z)
1099 {
1100 vbo_exec_Normal3f(x, y, z);
1101 }
1102
1103
1104 void GLAPIENTRY
1105 _es_MultiTexCoord4f(GLenum target, GLfloat s, GLfloat t, GLfloat r, GLfloat q)
1106 {
1107 vbo_exec_MultiTexCoord4f(target, s, t, r, q);
1108 }
1109
1110
1111 void GLAPIENTRY
1112 _es_Materialfv(GLenum face, GLenum pname, const GLfloat *params)
1113 {
1114 vbo_exec_Materialfv(face, pname, params);
1115 }
1116
1117
1118 void GLAPIENTRY
1119 _es_Materialf(GLenum face, GLenum pname, GLfloat param)
1120 {
1121 GLfloat p[4];
1122 p[0] = param;
1123 p[1] = p[2] = p[3] = 0.0F;
1124 vbo_exec_Materialfv(face, pname, p);
1125 }
1126
1127
1128 /**
1129 * A special version of glVertexAttrib4f that does not treat index 0 as
1130 * VBO_ATTRIB_POS.
1131 */
1132 static void
1133 VertexAttrib4f_nopos(GLuint index, GLfloat x, GLfloat y, GLfloat z, GLfloat w)
1134 {
1135 GET_CURRENT_CONTEXT(ctx);
1136 if (index < MAX_VERTEX_GENERIC_ATTRIBS)
1137 ATTRF(VBO_ATTRIB_GENERIC0 + index, 4, x, y, z, w);
1138 else
1139 ERROR(GL_INVALID_VALUE);
1140 }
1141
1142 void GLAPIENTRY
1143 _es_VertexAttrib4f(GLuint index, GLfloat x, GLfloat y, GLfloat z, GLfloat w)
1144 {
1145 VertexAttrib4f_nopos(index, x, y, z, w);
1146 }
1147
1148
1149 void GLAPIENTRY
1150 _es_VertexAttrib1f(GLuint indx, GLfloat x)
1151 {
1152 VertexAttrib4f_nopos(indx, x, 0.0f, 0.0f, 1.0f);
1153 }
1154
1155
1156 void GLAPIENTRY
1157 _es_VertexAttrib1fv(GLuint indx, const GLfloat* values)
1158 {
1159 VertexAttrib4f_nopos(indx, values[0], 0.0f, 0.0f, 1.0f);
1160 }
1161
1162
1163 void GLAPIENTRY
1164 _es_VertexAttrib2f(GLuint indx, GLfloat x, GLfloat y)
1165 {
1166 VertexAttrib4f_nopos(indx, x, y, 0.0f, 1.0f);
1167 }
1168
1169
1170 void GLAPIENTRY
1171 _es_VertexAttrib2fv(GLuint indx, const GLfloat* values)
1172 {
1173 VertexAttrib4f_nopos(indx, values[0], values[1], 0.0f, 1.0f);
1174 }
1175
1176
1177 void GLAPIENTRY
1178 _es_VertexAttrib3f(GLuint indx, GLfloat x, GLfloat y, GLfloat z)
1179 {
1180 VertexAttrib4f_nopos(indx, x, y, z, 1.0f);
1181 }
1182
1183
1184 void GLAPIENTRY
1185 _es_VertexAttrib3fv(GLuint indx, const GLfloat* values)
1186 {
1187 VertexAttrib4f_nopos(indx, values[0], values[1], values[2], 1.0f);
1188 }
1189
1190
1191 void GLAPIENTRY
1192 _es_VertexAttrib4fv(GLuint indx, const GLfloat* values)
1193 {
1194 VertexAttrib4f_nopos(indx, values[0], values[1], values[2], values[3]);
1195 }