Sync with trunk r63637.
[reactos.git] / dll / opengl / mesa / vbo / vbo_exec_api.c
1 /**************************************************************************
2
3 Copyright 2002-2008 Tungsten Graphics Inc., Cedar Park, Texas.
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 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.
25
26 **************************************************************************/
27
28 /*
29 * Authors:
30 * Keith Whitwell <keith@tungstengraphics.com>
31 */
32
33 #include <precomp.h>
34
35 #ifdef ERROR
36 #undef ERROR
37 #endif
38
39
40 /** ID/name for immediate-mode VBO */
41 #define IMM_BUFFER_NAME 0xaabbccdd
42
43
44 static void reset_attrfv( struct vbo_exec_context *exec );
45
46
47 /**
48 * Close off the last primitive, execute the buffer, restart the
49 * primitive.
50 */
51 static void vbo_exec_wrap_buffers( struct vbo_exec_context *exec )
52 {
53 if (exec->vtx.prim_count == 0) {
54 exec->vtx.copied.nr = 0;
55 exec->vtx.vert_count = 0;
56 exec->vtx.buffer_ptr = exec->vtx.buffer_map;
57 }
58 else {
59 GLuint last_begin = exec->vtx.prim[exec->vtx.prim_count-1].begin;
60 GLuint last_count;
61
62 if (exec->ctx->Driver.CurrentExecPrimitive != PRIM_OUTSIDE_BEGIN_END) {
63 GLint i = exec->vtx.prim_count - 1;
64 assert(i >= 0);
65 exec->vtx.prim[i].count = (exec->vtx.vert_count -
66 exec->vtx.prim[i].start);
67 }
68
69 last_count = exec->vtx.prim[exec->vtx.prim_count-1].count;
70
71 /* Execute the buffer and save copied vertices.
72 */
73 if (exec->vtx.vert_count)
74 vbo_exec_vtx_flush( exec, GL_FALSE );
75 else {
76 exec->vtx.prim_count = 0;
77 exec->vtx.copied.nr = 0;
78 }
79
80 /* Emit a glBegin to start the new list.
81 */
82 assert(exec->vtx.prim_count == 0);
83
84 if (exec->ctx->Driver.CurrentExecPrimitive != PRIM_OUTSIDE_BEGIN_END) {
85 exec->vtx.prim[0].mode = exec->ctx->Driver.CurrentExecPrimitive;
86 exec->vtx.prim[0].start = 0;
87 exec->vtx.prim[0].count = 0;
88 exec->vtx.prim_count++;
89
90 if (exec->vtx.copied.nr == last_count)
91 exec->vtx.prim[0].begin = last_begin;
92 }
93 }
94 }
95
96
97 /**
98 * Deal with buffer wrapping where provoked by the vertex buffer
99 * filling up, as opposed to upgrade_vertex().
100 */
101 void vbo_exec_vtx_wrap( struct vbo_exec_context *exec )
102 {
103 GLfloat *data = exec->vtx.copied.buffer;
104 GLuint i;
105
106 /* Run pipeline on current vertices, copy wrapped vertices
107 * to exec->vtx.copied.
108 */
109 vbo_exec_wrap_buffers( exec );
110
111 /* Copy stored stored vertices to start of new list.
112 */
113 assert(exec->vtx.max_vert - exec->vtx.vert_count > exec->vtx.copied.nr);
114
115 for (i = 0 ; i < exec->vtx.copied.nr ; i++) {
116 memcpy( exec->vtx.buffer_ptr, data,
117 exec->vtx.vertex_size * sizeof(GLfloat));
118 exec->vtx.buffer_ptr += exec->vtx.vertex_size;
119 data += exec->vtx.vertex_size;
120 exec->vtx.vert_count++;
121 }
122
123 exec->vtx.copied.nr = 0;
124 }
125
126
127 /**
128 * Copy the active vertex's values to the ctx->Current fields.
129 */
130 static void vbo_exec_copy_to_current( struct vbo_exec_context *exec )
131 {
132 struct gl_context *ctx = exec->ctx;
133 struct vbo_context *vbo = vbo_context(ctx);
134 GLuint i;
135
136 for (i = VBO_ATTRIB_POS+1 ; i < VBO_ATTRIB_MAX ; i++) {
137 if (exec->vtx.attrsz[i]) {
138 /* Note: the exec->vtx.current[i] pointers point into the
139 * ctx->Current.Attrib and ctx->Light.Material.Attrib arrays.
140 */
141 GLfloat *current = (GLfloat *)vbo->currval[i].Ptr;
142 GLfloat tmp[4];
143
144 COPY_CLEAN_4V(tmp,
145 exec->vtx.attrsz[i],
146 exec->vtx.attrptr[i]);
147
148 if (memcmp(current, tmp, sizeof(tmp)) != 0) {
149 memcpy(current, tmp, sizeof(tmp));
150
151 /* Given that we explicitly state size here, there is no need
152 * for the COPY_CLEAN above, could just copy 16 bytes and be
153 * done. The only problem is when Mesa accesses ctx->Current
154 * directly.
155 */
156 vbo->currval[i].Size = exec->vtx.attrsz[i];
157 assert(vbo->currval[i].Type == GL_FLOAT);
158 vbo->currval[i]._ElementSize = vbo->currval[i].Size * sizeof(GLfloat);
159
160 /* This triggers rather too much recalculation of Mesa state
161 * that doesn't get used (eg light positions).
162 */
163 if (i >= VBO_ATTRIB_MAT_FRONT_AMBIENT && i <= VBO_ATTRIB_MAT_BACK_INDEXES)
164 ctx->NewState |= _NEW_LIGHT;
165
166 ctx->NewState |= _NEW_CURRENT_ATTRIB;
167 }
168 }
169 }
170
171 /* Colormaterial -- this kindof sucks.
172 */
173 if (ctx->Light.ColorMaterialEnabled &&
174 exec->vtx.attrsz[VBO_ATTRIB_COLOR]) {
175 _mesa_update_color_material(ctx,
176 ctx->Current.Attrib[VBO_ATTRIB_COLOR]);
177 }
178 }
179
180
181 /**
182 * Copy current vertex attribute values into the current vertex.
183 */
184 static void
185 vbo_exec_copy_from_current(struct vbo_exec_context *exec)
186 {
187 struct gl_context *ctx = exec->ctx;
188 struct vbo_context *vbo = vbo_context(ctx);
189 GLint i;
190
191 for (i = VBO_ATTRIB_POS + 1; i < VBO_ATTRIB_MAX; i++) {
192 const GLfloat *current = (GLfloat *) vbo->currval[i].Ptr;
193 switch (exec->vtx.attrsz[i]) {
194 case 4: exec->vtx.attrptr[i][3] = current[3];
195 case 3: exec->vtx.attrptr[i][2] = current[2];
196 case 2: exec->vtx.attrptr[i][1] = current[1];
197 case 1: exec->vtx.attrptr[i][0] = current[0];
198 break;
199 }
200 }
201 }
202
203
204 /**
205 * Flush existing data, set new attrib size, replay copied vertices.
206 * This is called when we transition from a small vertex attribute size
207 * to a larger one. Ex: glTexCoord2f -> glTexCoord4f.
208 * We need to go back over the previous 2-component texcoords and insert
209 * zero and one values.
210 */
211 static void
212 vbo_exec_wrap_upgrade_vertex(struct vbo_exec_context *exec,
213 GLuint attr, GLuint newSize )
214 {
215 struct gl_context *ctx = exec->ctx;
216 struct vbo_context *vbo = vbo_context(ctx);
217 const GLint lastcount = exec->vtx.vert_count;
218 GLfloat *old_attrptr[VBO_ATTRIB_MAX];
219 const GLuint old_vtx_size = exec->vtx.vertex_size; /* floats per vertex */
220 const GLuint oldSize = exec->vtx.attrsz[attr];
221 GLuint i;
222
223 /* Run pipeline on current vertices, copy wrapped vertices
224 * to exec->vtx.copied.
225 */
226 vbo_exec_wrap_buffers( exec );
227
228 if (unlikely(exec->vtx.copied.nr)) {
229 /* We're in the middle of a primitive, keep the old vertex
230 * format around to be able to translate the copied vertices to
231 * the new format.
232 */
233 memcpy(old_attrptr, exec->vtx.attrptr, sizeof(old_attrptr));
234 }
235
236 if (unlikely(oldSize)) {
237 /* Do a COPY_TO_CURRENT to ensure back-copying works for the
238 * case when the attribute already exists in the vertex and is
239 * having its size increased.
240 */
241 vbo_exec_copy_to_current( exec );
242 }
243
244 /* Heuristic: Attempt to isolate attributes received outside
245 * begin/end so that they don't bloat the vertices.
246 */
247 if (ctx->Driver.CurrentExecPrimitive == PRIM_OUTSIDE_BEGIN_END &&
248 !oldSize && lastcount > 8 && exec->vtx.vertex_size) {
249 vbo_exec_copy_to_current( exec );
250 reset_attrfv( exec );
251 }
252
253 /* Fix up sizes:
254 */
255 exec->vtx.attrsz[attr] = newSize;
256 exec->vtx.vertex_size += newSize - oldSize;
257 exec->vtx.max_vert = ((VBO_VERT_BUFFER_SIZE - exec->vtx.buffer_used) /
258 (exec->vtx.vertex_size * sizeof(GLfloat)));
259 exec->vtx.vert_count = 0;
260 exec->vtx.buffer_ptr = exec->vtx.buffer_map;
261
262 if (unlikely(oldSize)) {
263 /* Size changed, recalculate all the attrptr[] values
264 */
265 GLfloat *tmp = exec->vtx.vertex;
266
267 for (i = 0 ; i < VBO_ATTRIB_MAX ; i++) {
268 if (exec->vtx.attrsz[i]) {
269 exec->vtx.attrptr[i] = tmp;
270 tmp += exec->vtx.attrsz[i];
271 }
272 else
273 exec->vtx.attrptr[i] = NULL; /* will not be dereferenced */
274 }
275
276 /* Copy from current to repopulate the vertex with correct
277 * values.
278 */
279 vbo_exec_copy_from_current( exec );
280 }
281 else {
282 /* Just have to append the new attribute at the end */
283 exec->vtx.attrptr[attr] = exec->vtx.vertex +
284 exec->vtx.vertex_size - newSize;
285 }
286
287 /* Replay stored vertices to translate them
288 * to new format here.
289 *
290 * -- No need to replay - just copy piecewise
291 */
292 if (unlikely(exec->vtx.copied.nr)) {
293 GLfloat *data = exec->vtx.copied.buffer;
294 GLfloat *dest = exec->vtx.buffer_ptr;
295 GLuint j;
296
297 assert(exec->vtx.buffer_ptr == exec->vtx.buffer_map);
298
299 for (i = 0 ; i < exec->vtx.copied.nr ; i++) {
300 for (j = 0 ; j < VBO_ATTRIB_MAX ; j++) {
301 GLuint sz = exec->vtx.attrsz[j];
302
303 if (sz) {
304 GLint old_offset = old_attrptr[j] - exec->vtx.vertex;
305 GLint new_offset = exec->vtx.attrptr[j] - exec->vtx.vertex;
306
307 if (j == attr) {
308 if (oldSize) {
309 GLfloat tmp[4];
310 COPY_CLEAN_4V(tmp, oldSize, data + old_offset);
311 COPY_SZ_4V(dest + new_offset, newSize, tmp);
312 } else {
313 GLfloat *current = (GLfloat *)vbo->currval[j].Ptr;
314 COPY_SZ_4V(dest + new_offset, sz, current);
315 }
316 }
317 else {
318 COPY_SZ_4V(dest + new_offset, sz, data + old_offset);
319 }
320 }
321 }
322
323 data += old_vtx_size;
324 dest += exec->vtx.vertex_size;
325 }
326
327 exec->vtx.buffer_ptr = dest;
328 exec->vtx.vert_count += exec->vtx.copied.nr;
329 exec->vtx.copied.nr = 0;
330 }
331 }
332
333
334 /**
335 * This is when a vertex attribute transitions to a different size.
336 * For example, we saw a bunch of glTexCoord2f() calls and now we got a
337 * glTexCoord4f() call. We promote the array from size=2 to size=4.
338 */
339 static void
340 vbo_exec_fixup_vertex(struct gl_context *ctx, GLuint attr, GLuint newSize)
341 {
342 struct vbo_exec_context *exec = &vbo_context(ctx)->exec;
343
344 if (newSize > exec->vtx.attrsz[attr]) {
345 /* New size is larger. Need to flush existing vertices and get
346 * an enlarged vertex format.
347 */
348 vbo_exec_wrap_upgrade_vertex( exec, attr, newSize );
349 }
350 else if (newSize < exec->vtx.active_sz[attr]) {
351 static const GLfloat id[4] = { 0, 0, 0, 1 };
352 GLuint i;
353
354 /* New size is smaller - just need to fill in some
355 * zeros. Don't need to flush or wrap.
356 */
357 for (i = newSize; i <= exec->vtx.attrsz[attr]; i++)
358 exec->vtx.attrptr[attr][i-1] = id[i-1];
359 }
360
361 exec->vtx.active_sz[attr] = newSize;
362
363 /* Does setting NeedFlush belong here? Necessitates resetting
364 * vtxfmt on each flush (otherwise flags won't get reset
365 * afterwards).
366 */
367 if (attr == 0)
368 ctx->Driver.NeedFlush |= FLUSH_STORED_VERTICES;
369 }
370
371
372 /**
373 * This macro is used to implement all the glVertex, glColor, glTexCoord,
374 * glVertexAttrib, etc functions.
375 */
376 #define ATTR( A, N, V0, V1, V2, V3 ) \
377 do { \
378 struct vbo_exec_context *exec = &vbo_context(ctx)->exec; \
379 \
380 if (unlikely(!(ctx->Driver.NeedFlush & FLUSH_UPDATE_CURRENT))) \
381 ctx->Driver.BeginVertices( ctx ); \
382 \
383 if (unlikely(exec->vtx.active_sz[A] != N)) \
384 vbo_exec_fixup_vertex(ctx, A, N); \
385 \
386 { \
387 GLfloat *dest = exec->vtx.attrptr[A]; \
388 if (N>0) dest[0] = V0; \
389 if (N>1) dest[1] = V1; \
390 if (N>2) dest[2] = V2; \
391 if (N>3) dest[3] = V3; \
392 } \
393 \
394 if ((A) == 0) { \
395 /* This is a glVertex call */ \
396 GLuint i; \
397 \
398 for (i = 0; i < exec->vtx.vertex_size; i++) \
399 exec->vtx.buffer_ptr[i] = exec->vtx.vertex[i]; \
400 \
401 exec->vtx.buffer_ptr += exec->vtx.vertex_size; \
402 \
403 /* Set FLUSH_STORED_VERTICES to indicate that there's now */ \
404 /* something to draw (not just updating a color or texcoord).*/ \
405 ctx->Driver.NeedFlush |= FLUSH_STORED_VERTICES; \
406 \
407 if (++exec->vtx.vert_count >= exec->vtx.max_vert) \
408 vbo_exec_vtx_wrap( exec ); \
409 } \
410 } while (0)
411
412
413 #define ERROR(err) _mesa_error( ctx, err, __FUNCTION__ )
414 #define TAG(x) vbo_##x
415
416 #include "vbo_attrib_tmp.h"
417
418
419
420 /**
421 * Execute a glMaterial call. Note that if GL_COLOR_MATERIAL is enabled,
422 * this may be a (partial) no-op.
423 */
424 static void GLAPIENTRY
425 vbo_Materialfv(GLenum face, GLenum pname, const GLfloat *params)
426 {
427 GLbitfield updateMats;
428 GET_CURRENT_CONTEXT(ctx);
429
430 /* This function should be a no-op when it tries to update material
431 * attributes which are currently tracking glColor via glColorMaterial.
432 * The updateMats var will be a mask of the MAT_BIT_FRONT/BACK_x bits
433 * indicating which material attributes can actually be updated below.
434 */
435 if (ctx->Light.ColorMaterialEnabled) {
436 updateMats = ~ctx->Light.ColorMaterialBitmask;
437 }
438 else {
439 /* GL_COLOR_MATERIAL is disabled so don't skip any material updates */
440 updateMats = ALL_MATERIAL_BITS;
441 }
442
443 if (face == GL_FRONT) {
444 updateMats &= FRONT_MATERIAL_BITS;
445 }
446 else if (face == GL_BACK) {
447 updateMats &= BACK_MATERIAL_BITS;
448 }
449 else if (face != GL_FRONT_AND_BACK) {
450 _mesa_error(ctx, GL_INVALID_ENUM, "glMaterial(invalid face)");
451 return;
452 }
453
454 switch (pname) {
455 case GL_EMISSION:
456 if (updateMats & MAT_BIT_FRONT_EMISSION)
457 MAT_ATTR(VBO_ATTRIB_MAT_FRONT_EMISSION, 4, params);
458 if (updateMats & MAT_BIT_BACK_EMISSION)
459 MAT_ATTR(VBO_ATTRIB_MAT_BACK_EMISSION, 4, params);
460 break;
461 case GL_AMBIENT:
462 if (updateMats & MAT_BIT_FRONT_AMBIENT)
463 MAT_ATTR(VBO_ATTRIB_MAT_FRONT_AMBIENT, 4, params);
464 if (updateMats & MAT_BIT_BACK_AMBIENT)
465 MAT_ATTR(VBO_ATTRIB_MAT_BACK_AMBIENT, 4, params);
466 break;
467 case GL_DIFFUSE:
468 if (updateMats & MAT_BIT_FRONT_DIFFUSE)
469 MAT_ATTR(VBO_ATTRIB_MAT_FRONT_DIFFUSE, 4, params);
470 if (updateMats & MAT_BIT_BACK_DIFFUSE)
471 MAT_ATTR(VBO_ATTRIB_MAT_BACK_DIFFUSE, 4, params);
472 break;
473 case GL_SPECULAR:
474 if (updateMats & MAT_BIT_FRONT_SPECULAR)
475 MAT_ATTR(VBO_ATTRIB_MAT_FRONT_SPECULAR, 4, params);
476 if (updateMats & MAT_BIT_BACK_SPECULAR)
477 MAT_ATTR(VBO_ATTRIB_MAT_BACK_SPECULAR, 4, params);
478 break;
479 case GL_SHININESS:
480 if (*params < 0 || *params > ctx->Const.MaxShininess) {
481 _mesa_error(ctx, GL_INVALID_VALUE,
482 "glMaterial(invalid shininess: %f out range [0, %f])",
483 *params, ctx->Const.MaxShininess);
484 return;
485 }
486 if (updateMats & MAT_BIT_FRONT_SHININESS)
487 MAT_ATTR(VBO_ATTRIB_MAT_FRONT_SHININESS, 1, params);
488 if (updateMats & MAT_BIT_BACK_SHININESS)
489 MAT_ATTR(VBO_ATTRIB_MAT_BACK_SHININESS, 1, params);
490 break;
491 case GL_COLOR_INDEXES:
492 if (updateMats & MAT_BIT_FRONT_INDEXES)
493 MAT_ATTR(VBO_ATTRIB_MAT_FRONT_INDEXES, 3, params);
494 if (updateMats & MAT_BIT_BACK_INDEXES)
495 MAT_ATTR(VBO_ATTRIB_MAT_BACK_INDEXES, 3, params);
496 break;
497 case GL_AMBIENT_AND_DIFFUSE:
498 if (updateMats & MAT_BIT_FRONT_AMBIENT)
499 MAT_ATTR(VBO_ATTRIB_MAT_FRONT_AMBIENT, 4, params);
500 if (updateMats & MAT_BIT_FRONT_DIFFUSE)
501 MAT_ATTR(VBO_ATTRIB_MAT_FRONT_DIFFUSE, 4, params);
502 if (updateMats & MAT_BIT_BACK_AMBIENT)
503 MAT_ATTR(VBO_ATTRIB_MAT_BACK_AMBIENT, 4, params);
504 if (updateMats & MAT_BIT_BACK_DIFFUSE)
505 MAT_ATTR(VBO_ATTRIB_MAT_BACK_DIFFUSE, 4, params);
506 break;
507 default:
508 _mesa_error(ctx, GL_INVALID_ENUM, "glMaterialfv(pname)");
509 return;
510 }
511 }
512
513
514 /**
515 * Flush (draw) vertices.
516 * \param unmap - leave VBO unmapped after flushing?
517 */
518 static void
519 vbo_exec_FlushVertices_internal(struct vbo_exec_context *exec, GLboolean unmap)
520 {
521 if (exec->vtx.vert_count || unmap) {
522 vbo_exec_vtx_flush( exec, unmap );
523 }
524
525 if (exec->vtx.vertex_size) {
526 vbo_exec_copy_to_current( exec );
527 reset_attrfv( exec );
528 }
529 }
530
531
532 #if FEATURE_beginend
533
534
535 #if FEATURE_evaluators
536
537 static void GLAPIENTRY vbo_exec_EvalCoord1f( GLfloat u )
538 {
539 GET_CURRENT_CONTEXT( ctx );
540 struct vbo_exec_context *exec = &vbo_context(ctx)->exec;
541
542 {
543 GLint i;
544 if (exec->eval.recalculate_maps)
545 vbo_exec_eval_update( exec );
546
547 for (i = 0; i <= VBO_ATTRIB_TEX; i++) {
548 if (exec->eval.map1[i].map)
549 if (exec->vtx.active_sz[i] != exec->eval.map1[i].sz)
550 vbo_exec_fixup_vertex( ctx, i, exec->eval.map1[i].sz );
551 }
552 }
553
554
555 memcpy( exec->vtx.copied.buffer, exec->vtx.vertex,
556 exec->vtx.vertex_size * sizeof(GLfloat));
557
558 vbo_exec_do_EvalCoord1f( exec, u );
559
560 memcpy( exec->vtx.vertex, exec->vtx.copied.buffer,
561 exec->vtx.vertex_size * sizeof(GLfloat));
562 }
563
564 static void GLAPIENTRY vbo_exec_EvalCoord2f( GLfloat u, GLfloat v )
565 {
566 GET_CURRENT_CONTEXT( ctx );
567 struct vbo_exec_context *exec = &vbo_context(ctx)->exec;
568
569 {
570 GLint i;
571 if (exec->eval.recalculate_maps)
572 vbo_exec_eval_update( exec );
573
574 for (i = 0; i <= VBO_ATTRIB_TEX; i++) {
575 if (exec->eval.map2[i].map)
576 if (exec->vtx.active_sz[i] != exec->eval.map2[i].sz)
577 vbo_exec_fixup_vertex( ctx, i, exec->eval.map2[i].sz );
578 }
579
580 if (ctx->Eval.AutoNormal)
581 if (exec->vtx.active_sz[VBO_ATTRIB_NORMAL] != 3)
582 vbo_exec_fixup_vertex( ctx, VBO_ATTRIB_NORMAL, 3 );
583 }
584
585 memcpy( exec->vtx.copied.buffer, exec->vtx.vertex,
586 exec->vtx.vertex_size * sizeof(GLfloat));
587
588 vbo_exec_do_EvalCoord2f( exec, u, v );
589
590 memcpy( exec->vtx.vertex, exec->vtx.copied.buffer,
591 exec->vtx.vertex_size * sizeof(GLfloat));
592 }
593
594 static void GLAPIENTRY vbo_exec_EvalCoord1fv( const GLfloat *u )
595 {
596 vbo_exec_EvalCoord1f( u[0] );
597 }
598
599 static void GLAPIENTRY vbo_exec_EvalCoord2fv( const GLfloat *u )
600 {
601 vbo_exec_EvalCoord2f( u[0], u[1] );
602 }
603
604 static void GLAPIENTRY vbo_exec_EvalPoint1( GLint i )
605 {
606 GET_CURRENT_CONTEXT( ctx );
607 GLfloat du = ((ctx->Eval.MapGrid1u2 - ctx->Eval.MapGrid1u1) /
608 (GLfloat) ctx->Eval.MapGrid1un);
609 GLfloat u = i * du + ctx->Eval.MapGrid1u1;
610
611 vbo_exec_EvalCoord1f( u );
612 }
613
614
615 static void GLAPIENTRY vbo_exec_EvalPoint2( GLint i, GLint j )
616 {
617 GET_CURRENT_CONTEXT( ctx );
618 GLfloat du = ((ctx->Eval.MapGrid2u2 - ctx->Eval.MapGrid2u1) /
619 (GLfloat) ctx->Eval.MapGrid2un);
620 GLfloat dv = ((ctx->Eval.MapGrid2v2 - ctx->Eval.MapGrid2v1) /
621 (GLfloat) ctx->Eval.MapGrid2vn);
622 GLfloat u = i * du + ctx->Eval.MapGrid2u1;
623 GLfloat v = j * dv + ctx->Eval.MapGrid2v1;
624
625 vbo_exec_EvalCoord2f( u, v );
626 }
627
628
629 static void GLAPIENTRY
630 vbo_exec_EvalMesh1(GLenum mode, GLint i1, GLint i2)
631 {
632 GET_CURRENT_CONTEXT(ctx);
633 GLint i;
634 GLfloat u, du;
635 GLenum prim;
636
637 ASSERT_OUTSIDE_BEGIN_END(ctx);
638
639 switch (mode) {
640 case GL_POINT:
641 prim = GL_POINTS;
642 break;
643 case GL_LINE:
644 prim = GL_LINE_STRIP;
645 break;
646 default:
647 _mesa_error( ctx, GL_INVALID_ENUM, "glEvalMesh1(mode)" );
648 return;
649 }
650
651 /* No effect if vertex maps disabled.
652 */
653 if (!ctx->Eval.Map1Vertex4 &&
654 !ctx->Eval.Map1Vertex3)
655 return;
656
657 du = ctx->Eval.MapGrid1du;
658 u = ctx->Eval.MapGrid1u1 + i1 * du;
659
660 CALL_Begin(GET_DISPATCH(), (prim));
661 for (i=i1;i<=i2;i++,u+=du) {
662 CALL_EvalCoord1f(GET_DISPATCH(), (u));
663 }
664 CALL_End(GET_DISPATCH(), ());
665 }
666
667
668 static void GLAPIENTRY
669 vbo_exec_EvalMesh2(GLenum mode, GLint i1, GLint i2, GLint j1, GLint j2)
670 {
671 GET_CURRENT_CONTEXT(ctx);
672 GLfloat u, du, v, dv, v1, u1;
673 GLint i, j;
674
675 ASSERT_OUTSIDE_BEGIN_END(ctx);
676
677 switch (mode) {
678 case GL_POINT:
679 case GL_LINE:
680 case GL_FILL:
681 break;
682 default:
683 _mesa_error( ctx, GL_INVALID_ENUM, "glEvalMesh2(mode)" );
684 return;
685 }
686
687 /* No effect if vertex maps disabled.
688 */
689 if (!ctx->Eval.Map2Vertex4 &&
690 !ctx->Eval.Map2Vertex3)
691 return;
692
693 du = ctx->Eval.MapGrid2du;
694 dv = ctx->Eval.MapGrid2dv;
695 v1 = ctx->Eval.MapGrid2v1 + j1 * dv;
696 u1 = ctx->Eval.MapGrid2u1 + i1 * du;
697
698 switch (mode) {
699 case GL_POINT:
700 CALL_Begin(GET_DISPATCH(), (GL_POINTS));
701 for (v=v1,j=j1;j<=j2;j++,v+=dv) {
702 for (u=u1,i=i1;i<=i2;i++,u+=du) {
703 CALL_EvalCoord2f(GET_DISPATCH(), (u, v));
704 }
705 }
706 CALL_End(GET_DISPATCH(), ());
707 break;
708 case GL_LINE:
709 for (v=v1,j=j1;j<=j2;j++,v+=dv) {
710 CALL_Begin(GET_DISPATCH(), (GL_LINE_STRIP));
711 for (u=u1,i=i1;i<=i2;i++,u+=du) {
712 CALL_EvalCoord2f(GET_DISPATCH(), (u, v));
713 }
714 CALL_End(GET_DISPATCH(), ());
715 }
716 for (u=u1,i=i1;i<=i2;i++,u+=du) {
717 CALL_Begin(GET_DISPATCH(), (GL_LINE_STRIP));
718 for (v=v1,j=j1;j<=j2;j++,v+=dv) {
719 CALL_EvalCoord2f(GET_DISPATCH(), (u, v));
720 }
721 CALL_End(GET_DISPATCH(), ());
722 }
723 break;
724 case GL_FILL:
725 for (v=v1,j=j1;j<j2;j++,v+=dv) {
726 CALL_Begin(GET_DISPATCH(), (GL_TRIANGLE_STRIP));
727 for (u=u1,i=i1;i<=i2;i++,u+=du) {
728 CALL_EvalCoord2f(GET_DISPATCH(), (u, v));
729 CALL_EvalCoord2f(GET_DISPATCH(), (u, v+dv));
730 }
731 CALL_End(GET_DISPATCH(), ());
732 }
733 break;
734 }
735 }
736
737 #endif /* FEATURE_evaluators */
738
739
740 /**
741 * Execute a glRectf() function. This is not suitable for GL_COMPILE
742 * modes (as the test for outside begin/end is not compiled),
743 * but may be useful for drivers in circumstances which exclude
744 * display list interactions.
745 *
746 * (None of the functions in this file are suitable for GL_COMPILE
747 * modes).
748 */
749 static void GLAPIENTRY
750 vbo_exec_Rectf(GLfloat x1, GLfloat y1, GLfloat x2, GLfloat y2)
751 {
752 GET_CURRENT_CONTEXT(ctx);
753 ASSERT_OUTSIDE_BEGIN_END(ctx);
754
755 CALL_Begin(GET_DISPATCH(), (GL_QUADS));
756 CALL_Vertex2f(GET_DISPATCH(), (x1, y1));
757 CALL_Vertex2f(GET_DISPATCH(), (x2, y1));
758 CALL_Vertex2f(GET_DISPATCH(), (x2, y2));
759 CALL_Vertex2f(GET_DISPATCH(), (x1, y2));
760 CALL_End(GET_DISPATCH(), ());
761 }
762
763
764 /**
765 * Called via glBegin.
766 */
767 static void GLAPIENTRY vbo_exec_Begin( GLenum mode )
768 {
769 GET_CURRENT_CONTEXT( ctx );
770
771 if (ctx->Driver.CurrentExecPrimitive == PRIM_OUTSIDE_BEGIN_END) {
772 struct vbo_exec_context *exec = &vbo_context(ctx)->exec;
773 int i;
774
775 if (!_mesa_valid_prim_mode(ctx, mode)) {
776 _mesa_error(ctx, GL_INVALID_ENUM, "glBegin");
777 return;
778 }
779
780 vbo_draw_method(exec, DRAW_BEGIN_END);
781
782 if (ctx->Driver.PrepareExecBegin)
783 ctx->Driver.PrepareExecBegin(ctx);
784
785 if (ctx->NewState) {
786 _mesa_update_state( ctx );
787
788 CALL_Begin(ctx->Exec, (mode));
789 return;
790 }
791
792 if (!_mesa_valid_to_render(ctx, "glBegin")) {
793 return;
794 }
795
796 /* Heuristic: attempt to isolate attributes occuring outside
797 * begin/end pairs.
798 */
799 if (exec->vtx.vertex_size && !exec->vtx.attrsz[0])
800 vbo_exec_FlushVertices_internal(exec, GL_FALSE);
801
802 i = exec->vtx.prim_count++;
803 exec->vtx.prim[i].mode = mode;
804 exec->vtx.prim[i].begin = 1;
805 exec->vtx.prim[i].end = 0;
806 exec->vtx.prim[i].indexed = 0;
807 exec->vtx.prim[i].weak = 0;
808 exec->vtx.prim[i].pad = 0;
809 exec->vtx.prim[i].start = exec->vtx.vert_count;
810 exec->vtx.prim[i].count = 0;
811 exec->vtx.prim[i].num_instances = 1;
812
813 ctx->Driver.CurrentExecPrimitive = mode;
814 }
815 else
816 _mesa_error( ctx, GL_INVALID_OPERATION, "glBegin" );
817
818 }
819
820
821 /**
822 * Called via glEnd.
823 */
824 static void GLAPIENTRY vbo_exec_End( void )
825 {
826 GET_CURRENT_CONTEXT( ctx );
827
828 if (ctx->Driver.CurrentExecPrimitive != PRIM_OUTSIDE_BEGIN_END) {
829 struct vbo_exec_context *exec = &vbo_context(ctx)->exec;
830
831 if (exec->vtx.prim_count > 0) {
832 /* close off current primitive */
833 int idx = exec->vtx.vert_count;
834 int i = exec->vtx.prim_count - 1;
835
836 exec->vtx.prim[i].end = 1;
837 exec->vtx.prim[i].count = idx - exec->vtx.prim[i].start;
838 }
839
840 ctx->Driver.CurrentExecPrimitive = PRIM_OUTSIDE_BEGIN_END;
841
842 if (exec->vtx.prim_count == VBO_MAX_PRIM)
843 vbo_exec_vtx_flush( exec, GL_FALSE );
844 }
845 else
846 _mesa_error( ctx, GL_INVALID_OPERATION, "glEnd" );
847 }
848
849
850 static void vbo_exec_vtxfmt_init( struct vbo_exec_context *exec )
851 {
852 GLvertexformat *vfmt = &exec->vtxfmt;
853
854 _MESA_INIT_ARRAYELT_VTXFMT(vfmt, _ae_);
855
856 vfmt->Begin = vbo_exec_Begin;
857 vfmt->End = vbo_exec_End;
858
859 _MESA_INIT_DLIST_VTXFMT(vfmt, _mesa_);
860 _MESA_INIT_EVAL_VTXFMT(vfmt, vbo_exec_);
861
862 vfmt->Rectf = vbo_exec_Rectf;
863
864 /* from attrib_tmp.h:
865 */
866 vfmt->Color3f = vbo_Color3f;
867 vfmt->Color3fv = vbo_Color3fv;
868 vfmt->Color4f = vbo_Color4f;
869 vfmt->Color4fv = vbo_Color4fv;
870 vfmt->FogCoordfEXT = vbo_FogCoordfEXT;
871 vfmt->FogCoordfvEXT = vbo_FogCoordfvEXT;
872 vfmt->Normal3f = vbo_Normal3f;
873 vfmt->Normal3fv = vbo_Normal3fv;
874 vfmt->TexCoord1f = vbo_TexCoord1f;
875 vfmt->TexCoord1fv = vbo_TexCoord1fv;
876 vfmt->TexCoord2f = vbo_TexCoord2f;
877 vfmt->TexCoord2fv = vbo_TexCoord2fv;
878 vfmt->TexCoord3f = vbo_TexCoord3f;
879 vfmt->TexCoord3fv = vbo_TexCoord3fv;
880 vfmt->TexCoord4f = vbo_TexCoord4f;
881 vfmt->TexCoord4fv = vbo_TexCoord4fv;
882 vfmt->Vertex2f = vbo_Vertex2f;
883 vfmt->Vertex2fv = vbo_Vertex2fv;
884 vfmt->Vertex3f = vbo_Vertex3f;
885 vfmt->Vertex3fv = vbo_Vertex3fv;
886 vfmt->Vertex4f = vbo_Vertex4f;
887 vfmt->Vertex4fv = vbo_Vertex4fv;
888
889 vfmt->VertexAttrib1fNV = vbo_VertexAttrib1fNV;
890 vfmt->VertexAttrib1fvNV = vbo_VertexAttrib1fvNV;
891 vfmt->VertexAttrib2fNV = vbo_VertexAttrib2fNV;
892 vfmt->VertexAttrib2fvNV = vbo_VertexAttrib2fvNV;
893 vfmt->VertexAttrib3fNV = vbo_VertexAttrib3fNV;
894 vfmt->VertexAttrib3fvNV = vbo_VertexAttrib3fvNV;
895 vfmt->VertexAttrib4fNV = vbo_VertexAttrib4fNV;
896 vfmt->VertexAttrib4fvNV = vbo_VertexAttrib4fvNV;
897
898 vfmt->Materialfv = vbo_Materialfv;
899
900 vfmt->EdgeFlag = vbo_EdgeFlag;
901 vfmt->Indexf = vbo_Indexf;
902 vfmt->Indexfv = vbo_Indexfv;
903 }
904
905
906 #else /* FEATURE_beginend */
907
908
909 static void vbo_exec_vtxfmt_init( struct vbo_exec_context *exec )
910 {
911 /* silence warnings */
912 (void) vbo_Color3f;
913 (void) vbo_Color3fv;
914 (void) vbo_Color4f;
915 (void) vbo_Color4fv;
916 (void) vbo_FogCoordfEXT;
917 (void) vbo_FogCoordfvEXT;
918 (void) vbo_MultiTexCoord1f;
919 (void) vbo_MultiTexCoord1fv;
920 (void) vbo_MultiTexCoord2f;
921 (void) vbo_MultiTexCoord2fv;
922 (void) vbo_MultiTexCoord3f;
923 (void) vbo_MultiTexCoord3fv;
924 (void) vbo_MultiTexCoord4f;
925 (void) vbo_MultiTexCoord4fv;
926 (void) vbo_Normal3f;
927 (void) vbo_Normal3fv;
928 (void) vbo_TexCoord1f;
929 (void) vbo_TexCoord1fv;
930 (void) vbo_TexCoord2f;
931 (void) vbo_TexCoord2fv;
932 (void) vbo_TexCoord3f;
933 (void) vbo_TexCoord3fv;
934 (void) vbo_TexCoord4f;
935 (void) vbo_TexCoord4fv;
936 (void) vbo_Vertex2f;
937 (void) vbo_Vertex2fv;
938 (void) vbo_Vertex3f;
939 (void) vbo_Vertex3fv;
940 (void) vbo_Vertex4f;
941 (void) vbo_Vertex4fv;
942
943 (void) vbo_VertexAttrib1fNV;
944 (void) vbo_VertexAttrib1fvNV;
945 (void) vbo_VertexAttrib2fNV;
946 (void) vbo_VertexAttrib2fvNV;
947 (void) vbo_VertexAttrib3fNV;
948 (void) vbo_VertexAttrib3fvNV;
949 (void) vbo_VertexAttrib4fNV;
950 (void) vbo_VertexAttrib4fvNV;
951
952 (void) vbo_Materialfv;
953
954 (void) vbo_EdgeFlag;
955 (void) vbo_Indexf;
956 (void) vbo_Indexfv;
957 }
958
959
960 #endif /* FEATURE_beginend */
961
962
963 /**
964 * Tell the VBO module to use a real OpenGL vertex buffer object to
965 * store accumulated immediate-mode vertex data.
966 * This replaces the malloced buffer which was created in
967 * vb_exec_vtx_init() below.
968 */
969 void vbo_use_buffer_objects(struct gl_context *ctx)
970 {
971 struct vbo_exec_context *exec = &vbo_context(ctx)->exec;
972 /* Any buffer name but 0 can be used here since this bufferobj won't
973 * go into the bufferobj hashtable.
974 */
975 GLuint bufName = IMM_BUFFER_NAME;
976 GLenum target = GL_ARRAY_BUFFER_ARB;
977 GLenum usage = GL_STREAM_DRAW_ARB;
978 GLsizei size = VBO_VERT_BUFFER_SIZE;
979
980 /* Make sure this func is only used once */
981 assert(exec->vtx.bufferobj == ctx->Shared->NullBufferObj);
982 if (exec->vtx.buffer_map) {
983 _mesa_align_free(exec->vtx.buffer_map);
984 exec->vtx.buffer_map = NULL;
985 exec->vtx.buffer_ptr = NULL;
986 }
987
988 /* Allocate a real buffer object now */
989 _mesa_reference_buffer_object(ctx, &exec->vtx.bufferobj, NULL);
990 exec->vtx.bufferobj = ctx->Driver.NewBufferObject(ctx, bufName, target);
991 if (!ctx->Driver.BufferData(ctx, target, size, NULL, usage, exec->vtx.bufferobj)) {
992 _mesa_error(ctx, GL_OUT_OF_MEMORY, "VBO allocation");
993 }
994 }
995
996
997 /**
998 * If this function is called, all VBO buffers will be unmapped when
999 * we flush.
1000 * Otherwise, if a simple command like glColor3f() is called and we flush,
1001 * the current VBO may be left mapped.
1002 */
1003 void
1004 vbo_always_unmap_buffers(struct gl_context *ctx)
1005 {
1006 struct vbo_exec_context *exec = &vbo_context(ctx)->exec;
1007 exec->begin_vertices_flags |= FLUSH_STORED_VERTICES;
1008 }
1009
1010
1011 void vbo_exec_vtx_init( struct vbo_exec_context *exec )
1012 {
1013 struct gl_context *ctx = exec->ctx;
1014 struct vbo_context *vbo = vbo_context(ctx);
1015 GLuint i;
1016
1017 /* Allocate a buffer object. Will just reuse this object
1018 * continuously, unless vbo_use_buffer_objects() is called to enable
1019 * use of real VBOs.
1020 */
1021 _mesa_reference_buffer_object(ctx,
1022 &exec->vtx.bufferobj,
1023 ctx->Shared->NullBufferObj);
1024
1025 ASSERT(!exec->vtx.buffer_map);
1026 exec->vtx.buffer_map = (GLfloat *)_mesa_align_malloc(VBO_VERT_BUFFER_SIZE, 64);
1027 exec->vtx.buffer_ptr = exec->vtx.buffer_map;
1028
1029 vbo_exec_vtxfmt_init( exec );
1030 _mesa_noop_vtxfmt_init(&exec->vtxfmt_noop);
1031
1032 /* Hook our functions into the dispatch table.
1033 */
1034 _mesa_install_exec_vtxfmt( ctx, &exec->vtxfmt );
1035
1036 for (i = 0 ; i < VBO_ATTRIB_MAX ; i++) {
1037 ASSERT(i < Elements(exec->vtx.attrsz));
1038 exec->vtx.attrsz[i] = 0;
1039 ASSERT(i < Elements(exec->vtx.active_sz));
1040 exec->vtx.active_sz[i] = 0;
1041 }
1042 for (i = 0 ; i < VERT_ATTRIB_MAX; i++) {
1043 ASSERT(i < Elements(exec->vtx.inputs));
1044 ASSERT(i < Elements(exec->vtx.arrays));
1045 exec->vtx.inputs[i] = &exec->vtx.arrays[i];
1046 }
1047
1048 {
1049 struct gl_client_array *arrays = exec->vtx.arrays;
1050 unsigned i;
1051
1052 memcpy(arrays, vbo->legacy_currval,
1053 VERT_ATTRIB_MAX * sizeof(arrays[0]));
1054 for (i = 0; i < VERT_ATTRIB_MAX; ++i) {
1055 struct gl_client_array *array;
1056 array = &arrays[VERT_ATTRIB(i)];
1057 array->BufferObj = NULL;
1058 _mesa_reference_buffer_object(ctx, &arrays->BufferObj,
1059 vbo->legacy_currval[i].BufferObj);
1060 }
1061 }
1062
1063 exec->vtx.vertex_size = 0;
1064
1065 exec->begin_vertices_flags = FLUSH_UPDATE_CURRENT;
1066 }
1067
1068
1069 void vbo_exec_vtx_destroy( struct vbo_exec_context *exec )
1070 {
1071 /* using a real VBO for vertex data */
1072 struct gl_context *ctx = exec->ctx;
1073 unsigned i;
1074
1075 /* True VBOs should already be unmapped
1076 */
1077 if (exec->vtx.buffer_map) {
1078 ASSERT(exec->vtx.bufferobj->Name == 0 ||
1079 exec->vtx.bufferobj->Name == IMM_BUFFER_NAME);
1080 if (exec->vtx.bufferobj->Name == 0) {
1081 _mesa_align_free(exec->vtx.buffer_map);
1082 exec->vtx.buffer_map = NULL;
1083 exec->vtx.buffer_ptr = NULL;
1084 }
1085 }
1086
1087 /* Drop any outstanding reference to the vertex buffer
1088 */
1089 for (i = 0; i < Elements(exec->vtx.arrays); i++) {
1090 _mesa_reference_buffer_object(ctx,
1091 &exec->vtx.arrays[i].BufferObj,
1092 NULL);
1093 }
1094
1095 /* Free the vertex buffer. Unmap first if needed.
1096 */
1097 if (_mesa_bufferobj_mapped(exec->vtx.bufferobj)) {
1098 ctx->Driver.UnmapBuffer(ctx, exec->vtx.bufferobj);
1099 }
1100 _mesa_reference_buffer_object(ctx, &exec->vtx.bufferobj, NULL);
1101 }
1102
1103
1104 /**
1105 * Called upon first glVertex, glColor, glTexCoord, etc.
1106 */
1107 void vbo_exec_BeginVertices( struct gl_context *ctx )
1108 {
1109 struct vbo_exec_context *exec = &vbo_context(ctx)->exec;
1110
1111 vbo_exec_vtx_map( exec );
1112
1113 assert((ctx->Driver.NeedFlush & FLUSH_UPDATE_CURRENT) == 0);
1114 assert(exec->begin_vertices_flags);
1115
1116 ctx->Driver.NeedFlush |= exec->begin_vertices_flags;
1117 }
1118
1119
1120 /**
1121 * Called via ctx->Driver.FlushVertices()
1122 * \param flags bitmask of FLUSH_STORED_VERTICES, FLUSH_UPDATE_CURRENT
1123 */
1124 void vbo_exec_FlushVertices( struct gl_context *ctx, GLuint flags )
1125 {
1126 struct vbo_exec_context *exec = &vbo_context(ctx)->exec;
1127
1128 #ifdef DEBUG
1129 /* debug check: make sure we don't get called recursively */
1130 exec->flush_call_depth++;
1131 assert(exec->flush_call_depth == 1);
1132 #endif
1133
1134 if (ctx->Driver.CurrentExecPrimitive != PRIM_OUTSIDE_BEGIN_END) {
1135 /* We've had glBegin but not glEnd! */
1136 #ifdef DEBUG
1137 exec->flush_call_depth--;
1138 assert(exec->flush_call_depth == 0);
1139 #endif
1140 return;
1141 }
1142
1143 /* Flush (draw), and make sure VBO is left unmapped when done */
1144 vbo_exec_FlushVertices_internal(exec, GL_TRUE);
1145
1146 /* Need to do this to ensure BeginVertices gets called again:
1147 */
1148 ctx->Driver.NeedFlush &= ~(FLUSH_UPDATE_CURRENT | flags);
1149
1150 #ifdef DEBUG
1151 exec->flush_call_depth--;
1152 assert(exec->flush_call_depth == 0);
1153 #endif
1154 }
1155
1156
1157 static void reset_attrfv( struct vbo_exec_context *exec )
1158 {
1159 GLuint i;
1160
1161 for (i = 0 ; i < VBO_ATTRIB_MAX ; i++) {
1162 exec->vtx.attrsz[i] = 0;
1163 exec->vtx.active_sz[i] = 0;
1164 }
1165
1166 exec->vtx.vertex_size = 0;
1167 }