vbo: Use the VAO array enabled flags in vbo_exec_array.
[mesa.git] / src / mesa / vbo / vbo_exec_array.c
1 /**************************************************************************
2 *
3 * Copyright 2003 VMware, Inc.
4 * Copyright 2009 VMware, Inc.
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
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
17 * of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 *
27 **************************************************************************/
28
29 #include <stdio.h>
30 #include "main/arrayobj.h"
31 #include "main/glheader.h"
32 #include "main/context.h"
33 #include "main/state.h"
34 #include "main/api_validate.h"
35 #include "main/dispatch.h"
36 #include "main/varray.h"
37 #include "main/bufferobj.h"
38 #include "main/enums.h"
39 #include "main/macros.h"
40 #include "main/transformfeedback.h"
41
42 #include "vbo_context.h"
43
44
45 /**
46 * All vertex buffers should be in an unmapped state when we're about
47 * to draw.
48 */
49 static bool
50 check_input_buffers_are_unmapped(const struct gl_vertex_array_object *vao)
51 {
52 /* Walk the enabled arrays that have a vbo attached */
53 GLbitfield64 mask = vao->_Enabled & vao->VertexAttribBufferMask;
54
55 while (mask) {
56 int i = ffsll(mask) - 1;
57 const struct gl_vertex_attrib_array *attrib_array =
58 &vao->VertexAttrib[i];
59 const struct gl_vertex_buffer_binding *buffer_binding =
60 &vao->VertexBinding[attrib_array->VertexBinding];
61
62 /* Only enabled arrays shall appear in the _Enabled bitmask */
63 assert(attrib_array->Enabled);
64 /* We have already masked with vao->VertexAttribBufferMask */
65 assert(_mesa_is_bufferobj(buffer_binding->BufferObj));
66
67 /* Bail out once we find the first disallowed mapping */
68 if (_mesa_check_disallowed_mapping(buffer_binding->BufferObj))
69 return false;
70
71 /* We have handled everything that is bound to this buffer_binding. */
72 mask &= ~buffer_binding->_BoundArrays;
73 }
74
75 return true;
76 }
77
78
79 /**
80 * A debug function that may be called from other parts of Mesa as
81 * needed during debugging.
82 */
83 static bool
84 check_buffers_are_unmapped(struct gl_context *ctx)
85 {
86 struct vbo_context *vbo = vbo_context(ctx);
87 struct vbo_exec_context *exec = &vbo->exec;
88
89 /* check the current vertex arrays */
90 return !_mesa_check_disallowed_mapping(exec->vtx.bufferobj) &&
91 check_input_buffers_are_unmapped(ctx->Array.VAO);
92 }
93
94
95 /**
96 * Check that element 'j' of the array has reasonable data.
97 * Map VBO if needed.
98 * For debugging purposes; not normally used.
99 */
100 static void
101 check_array_data(struct gl_context *ctx, struct gl_vertex_array_object *vao,
102 GLuint attrib, GLuint j)
103 {
104 const struct gl_vertex_attrib_array *array = &vao->VertexAttrib[attrib];
105 if (array->Enabled) {
106 const struct gl_vertex_buffer_binding *binding =
107 &vao->VertexBinding[array->VertexBinding];
108 struct gl_buffer_object *bo = binding->BufferObj;
109 const void *data = array->Ptr;
110 if (_mesa_is_bufferobj(bo)) {
111 if (!bo->Mappings[MAP_INTERNAL].Pointer) {
112 /* need to map now */
113 bo->Mappings[MAP_INTERNAL].Pointer =
114 ctx->Driver.MapBufferRange(ctx, 0, bo->Size,
115 GL_MAP_READ_BIT, bo,
116 MAP_INTERNAL);
117 }
118 data = ADD_POINTERS(_mesa_vertex_attrib_address(array, binding),
119 bo->Mappings[MAP_INTERNAL].Pointer);
120 }
121 switch (array->Type) {
122 case GL_FLOAT:
123 {
124 GLfloat *f = (GLfloat *) ((GLubyte *) data + binding->Stride * j);
125 GLint k;
126 for (k = 0; k < array->Size; k++) {
127 if (IS_INF_OR_NAN(f[k]) ||
128 f[k] >= 1.0e20F || f[k] <= -1.0e10F) {
129 printf("Bad array data:\n");
130 printf(" Element[%u].%u = %f\n", j, k, f[k]);
131 printf(" Array %u at %p\n", attrib, (void* ) array);
132 printf(" Type 0x%x, Size %d, Stride %d\n",
133 array->Type, array->Size, binding->Stride);
134 printf(" Address/offset %p in Buffer Object %u\n",
135 array->Ptr, bo->Name);
136 f[k] = 1.0F; /* XXX replace the bad value! */
137 }
138 /*assert(!IS_INF_OR_NAN(f[k]));*/
139 }
140 }
141 break;
142 default:
143 ;
144 }
145 }
146 }
147
148
149 /**
150 * Unmap the buffer object referenced by given array, if mapped.
151 */
152 static void
153 unmap_array_buffer(struct gl_context *ctx, struct gl_vertex_array_object *vao,
154 GLuint attrib)
155 {
156 const struct gl_vertex_attrib_array *array = &vao->VertexAttrib[attrib];
157 if (array->Enabled) {
158 const struct gl_vertex_buffer_binding *binding =
159 &vao->VertexBinding[array->VertexBinding];
160 struct gl_buffer_object *bo = binding->BufferObj;
161 if (_mesa_is_bufferobj(bo) && _mesa_bufferobj_mapped(bo, MAP_INTERNAL)) {
162 ctx->Driver.UnmapBuffer(ctx, bo, MAP_INTERNAL);
163 }
164 }
165 }
166
167
168 /**
169 * Examine the array's data for NaNs, etc.
170 * For debug purposes; not normally used.
171 */
172 static void
173 check_draw_elements_data(struct gl_context *ctx, GLsizei count, GLenum elemType,
174 const void *elements, GLint basevertex)
175 {
176 struct gl_vertex_array_object *vao = ctx->Array.VAO;
177 const void *elemMap;
178 GLint i;
179 GLuint k;
180
181 if (_mesa_is_bufferobj(ctx->Array.VAO->IndexBufferObj)) {
182 elemMap = ctx->Driver.MapBufferRange(ctx, 0,
183 ctx->Array.VAO->IndexBufferObj->Size,
184 GL_MAP_READ_BIT,
185 ctx->Array.VAO->IndexBufferObj,
186 MAP_INTERNAL);
187 elements = ADD_POINTERS(elements, elemMap);
188 }
189
190 for (i = 0; i < count; i++) {
191 GLuint j;
192
193 /* j = element[i] */
194 switch (elemType) {
195 case GL_UNSIGNED_BYTE:
196 j = ((const GLubyte *) elements)[i];
197 break;
198 case GL_UNSIGNED_SHORT:
199 j = ((const GLushort *) elements)[i];
200 break;
201 case GL_UNSIGNED_INT:
202 j = ((const GLuint *) elements)[i];
203 break;
204 default:
205 assert(0);
206 }
207
208 /* check element j of each enabled array */
209 for (k = 0; k < VERT_ATTRIB_MAX; k++) {
210 check_array_data(ctx, vao, k, j);
211 }
212 }
213
214 if (_mesa_is_bufferobj(vao->IndexBufferObj)) {
215 ctx->Driver.UnmapBuffer(ctx, ctx->Array.VAO->IndexBufferObj,
216 MAP_INTERNAL);
217 }
218
219 for (k = 0; k < VERT_ATTRIB_MAX; k++) {
220 unmap_array_buffer(ctx, vao, k);
221 }
222 }
223
224
225 /**
226 * Check array data, looking for NaNs, etc.
227 */
228 static void
229 check_draw_arrays_data(struct gl_context *ctx, GLint start, GLsizei count)
230 {
231 /* TO DO */
232 }
233
234
235 /**
236 * Print info/data for glDrawArrays(), for debugging.
237 */
238 static void
239 print_draw_arrays(struct gl_context *ctx,
240 GLenum mode, GLint start, GLsizei count)
241 {
242 const struct gl_vertex_array_object *vao = ctx->Array.VAO;
243
244 printf("vbo_exec_DrawArrays(mode 0x%x, start %d, count %d):\n",
245 mode, start, count);
246
247 unsigned i;
248 for (i = 0; i < VERT_ATTRIB_MAX; ++i) {
249 const struct gl_vertex_attrib_array *array = &vao->VertexAttrib[i];
250 if (!array->Enabled)
251 continue;
252
253 const struct gl_vertex_buffer_binding *binding =
254 &vao->VertexBinding[array->VertexBinding];
255 struct gl_buffer_object *bufObj = binding->BufferObj;
256
257 printf("attr %s: size %d stride %d enabled %d "
258 "ptr %p Bufobj %u\n",
259 gl_vert_attrib_name((gl_vert_attrib)i),
260 array->Size, binding->Stride, array->Enabled,
261 array->Ptr, bufObj->Name);
262
263 if (_mesa_is_bufferobj(bufObj)) {
264 GLubyte *p = ctx->Driver.MapBufferRange(ctx, 0, bufObj->Size,
265 GL_MAP_READ_BIT, bufObj,
266 MAP_INTERNAL);
267 int offset = (int) (GLintptr)
268 _mesa_vertex_attrib_address(array, binding);
269 float *f = (float *) (p + offset);
270 int *k = (int *) f;
271 int i;
272 int n = (count * binding->Stride) / 4;
273 if (n > 32)
274 n = 32;
275 printf(" Data at offset %d:\n", offset);
276 for (i = 0; i < n; i++) {
277 printf(" float[%d] = 0x%08x %f\n", i, k[i], f[i]);
278 }
279 ctx->Driver.UnmapBuffer(ctx, bufObj, MAP_INTERNAL);
280 }
281 }
282 }
283
284
285 /**
286 * Set the vbo->exec->inputs[] pointers to point to the enabled
287 * vertex arrays. This depends on the current vertex program/shader
288 * being executed because of whether or not generic vertex arrays
289 * alias the conventional vertex arrays.
290 * For arrays that aren't enabled, we set the input[attrib] pointer
291 * to point at a zero-stride current value "array".
292 */
293 static void
294 recalculate_input_bindings(struct gl_context *ctx)
295 {
296 struct vbo_context *vbo = vbo_context(ctx);
297 struct vbo_exec_context *exec = &vbo->exec;
298 const struct gl_vertex_attrib_array *array = ctx->Array.VAO->VertexAttrib;
299 struct gl_client_array *vertexAttrib = ctx->Array.VAO->_VertexAttrib;
300 const struct gl_client_array **inputs = &exec->array.inputs[0];
301 GLbitfield64 const_inputs = 0x0;
302 GLuint i;
303
304 switch (get_program_mode(ctx)) {
305 case VP_NONE:
306 /* When no vertex program is active (or the vertex program is generated
307 * from fixed-function state). We put the material values into the
308 * generic slots. This is the only situation where material values
309 * are available as per-vertex attributes.
310 */
311 for (i = 0; i < VERT_ATTRIB_FF_MAX; i++) {
312 if (array[VERT_ATTRIB_FF(i)].Enabled)
313 inputs[i] = &vertexAttrib[VERT_ATTRIB_FF(i)];
314 else {
315 inputs[i] = &vbo->currval[VBO_ATTRIB_POS+i];
316 const_inputs |= VERT_BIT(i);
317 }
318 }
319
320 for (i = 0; i < MAT_ATTRIB_MAX; i++) {
321 inputs[VERT_ATTRIB_GENERIC(i)] =
322 &vbo->currval[VBO_ATTRIB_MAT_FRONT_AMBIENT+i];
323 const_inputs |= VERT_BIT_GENERIC(i);
324 }
325
326 /* Could use just about anything, just to fill in the empty
327 * slots:
328 */
329 for (i = MAT_ATTRIB_MAX; i < VERT_ATTRIB_GENERIC_MAX; i++) {
330 inputs[VERT_ATTRIB_GENERIC(i)] = &vbo->currval[VBO_ATTRIB_GENERIC0+i];
331 const_inputs |= VERT_BIT_GENERIC(i);
332 }
333 break;
334
335 case VP_ARB:
336 /* There are no shaders in OpenGL ES 1.x, so this code path should be
337 * impossible to reach. The meta code is careful to not use shaders in
338 * ES1.
339 */
340 assert(ctx->API != API_OPENGLES);
341
342 /* In the compatibility profile of desktop OpenGL, the generic[0]
343 * attribute array aliases and overrides the legacy position array.
344 * Otherwise, legacy attributes available in the legacy slots,
345 * generic attributes in the generic slots and materials are not
346 * available as per-vertex attributes.
347 *
348 * In all other APIs, only the generic attributes exist, and none of the
349 * slots are considered "magic."
350 */
351 if (ctx->API == API_OPENGL_COMPAT) {
352 if (array[VERT_ATTRIB_GENERIC0].Enabled)
353 inputs[0] = &vertexAttrib[VERT_ATTRIB_GENERIC0];
354 else if (array[VERT_ATTRIB_POS].Enabled)
355 inputs[0] = &vertexAttrib[VERT_ATTRIB_POS];
356 else {
357 inputs[0] = &vbo->currval[VBO_ATTRIB_POS];
358 const_inputs |= VERT_BIT_POS;
359 }
360
361 for (i = 1; i < VERT_ATTRIB_FF_MAX; i++) {
362 if (array[VERT_ATTRIB_FF(i)].Enabled)
363 inputs[i] = &vertexAttrib[VERT_ATTRIB_FF(i)];
364 else {
365 inputs[i] = &vbo->currval[VBO_ATTRIB_POS+i];
366 const_inputs |= VERT_BIT_FF(i);
367 }
368 }
369
370 for (i = 1; i < VERT_ATTRIB_GENERIC_MAX; i++) {
371 if (array[VERT_ATTRIB_GENERIC(i)].Enabled)
372 inputs[VERT_ATTRIB_GENERIC(i)] =
373 &vertexAttrib[VERT_ATTRIB_GENERIC(i)];
374 else {
375 inputs[VERT_ATTRIB_GENERIC(i)] =
376 &vbo->currval[VBO_ATTRIB_GENERIC0+i];
377 const_inputs |= VERT_BIT_GENERIC(i);
378 }
379 }
380
381 inputs[VERT_ATTRIB_GENERIC0] = inputs[0];
382 } else {
383 /* Other parts of the code assume that inputs[0] through
384 * inputs[VERT_ATTRIB_FF_MAX] will be non-NULL. However, in OpenGL
385 * ES 2.0+ or OpenGL core profile, none of these arrays should ever
386 * be enabled.
387 */
388 for (i = 0; i < VERT_ATTRIB_FF_MAX; i++) {
389 assert(!array[VERT_ATTRIB_FF(i)].Enabled);
390
391 inputs[i] = &vbo->currval[VBO_ATTRIB_POS+i];
392 const_inputs |= VERT_BIT_FF(i);
393 }
394
395 for (i = 0; i < VERT_ATTRIB_GENERIC_MAX; i++) {
396 if (array[VERT_ATTRIB_GENERIC(i)].Enabled)
397 inputs[VERT_ATTRIB_GENERIC(i)] =
398 &vertexAttrib[VERT_ATTRIB_GENERIC(i)];
399 else {
400 inputs[VERT_ATTRIB_GENERIC(i)] =
401 &vbo->currval[VBO_ATTRIB_GENERIC0+i];
402 const_inputs |= VERT_BIT_GENERIC(i);
403 }
404 }
405 }
406
407 break;
408 }
409
410 _mesa_set_varying_vp_inputs( ctx, VERT_BIT_ALL & (~const_inputs) );
411 ctx->NewDriverState |= ctx->DriverFlags.NewArray;
412 }
413
414
415 /**
416 * Examine the enabled vertex arrays to set the exec->array.inputs[] values.
417 * These will point to the arrays to actually use for drawing. Some will
418 * be user-provided arrays, other will be zero-stride const-valued arrays.
419 * Note that this might set the _NEW_VARYING_VP_INPUTS dirty flag so state
420 * validation must be done after this call.
421 */
422 bool
423 vbo_bind_arrays(struct gl_context *ctx)
424 {
425 struct vbo_context *vbo = vbo_context(ctx);
426 struct vbo_exec_context *exec = &vbo->exec;
427
428 vbo_draw_method(vbo, DRAW_ARRAYS);
429
430 if (exec->array.recalculate_inputs) {
431 recalculate_input_bindings(ctx);
432 exec->array.recalculate_inputs = GL_FALSE;
433
434 /* Again... because we may have changed the bitmask of per-vertex varying
435 * attributes. If we regenerate the fixed-function vertex program now
436 * we may be able to prune down the number of vertex attributes which we
437 * need in the shader.
438 */
439 if (ctx->NewState) {
440 /* Setting "validating" to TRUE prevents _mesa_update_state from
441 * invalidating what we just did.
442 */
443 exec->validating = GL_TRUE;
444 _mesa_update_state(ctx);
445 exec->validating = GL_FALSE;
446 }
447 }
448
449 if (!check_buffers_are_unmapped(ctx)) {
450 _mesa_error(ctx, GL_INVALID_OPERATION,
451 "draw call (vertex buffers are mapped)");
452 return false;
453 } else {
454 return true;
455 }
456 }
457
458 /**
459 * Helper function called by the other DrawArrays() functions below.
460 * This is where we handle primitive restart for drawing non-indexed
461 * arrays. If primitive restart is enabled, it typically means
462 * splitting one DrawArrays() into two.
463 */
464 static void
465 vbo_draw_arrays(struct gl_context *ctx, GLenum mode, GLint start,
466 GLsizei count, GLuint numInstances, GLuint baseInstance)
467 {
468 struct vbo_context *vbo = vbo_context(ctx);
469 struct _mesa_prim prim[2];
470
471 if (!vbo_bind_arrays(ctx))
472 return;
473
474 /* init most fields to zero */
475 memset(prim, 0, sizeof(prim));
476 prim[0].begin = 1;
477 prim[0].end = 1;
478 prim[0].mode = mode;
479 prim[0].num_instances = numInstances;
480 prim[0].base_instance = baseInstance;
481 prim[0].is_indirect = 0;
482
483 /* Implement the primitive restart index */
484 if (ctx->Array.PrimitiveRestart && !ctx->Array.PrimitiveRestartFixedIndex &&
485 ctx->Array.RestartIndex < count) {
486 GLuint primCount = 0;
487
488 if (ctx->Array.RestartIndex == start) {
489 /* special case: RestartIndex at beginning */
490 if (count > 1) {
491 prim[0].start = start + 1;
492 prim[0].count = count - 1;
493 primCount = 1;
494 }
495 }
496 else if (ctx->Array.RestartIndex == start + count - 1) {
497 /* special case: RestartIndex at end */
498 if (count > 1) {
499 prim[0].start = start;
500 prim[0].count = count - 1;
501 primCount = 1;
502 }
503 }
504 else {
505 /* general case: RestartIndex in middle, split into two prims */
506 prim[0].start = start;
507 prim[0].count = ctx->Array.RestartIndex - start;
508
509 prim[1] = prim[0];
510 prim[1].start = ctx->Array.RestartIndex + 1;
511 prim[1].count = count - prim[1].start;
512
513 primCount = 2;
514 }
515
516 if (primCount > 0) {
517 /* draw one or two prims */
518 vbo->draw_prims(ctx, prim, primCount, NULL,
519 GL_TRUE, start, start + count - 1, NULL, 0, NULL);
520 }
521 }
522 else {
523 /* no prim restart */
524 prim[0].start = start;
525 prim[0].count = count;
526
527 vbo->draw_prims(ctx, prim, 1, NULL,
528 GL_TRUE, start, start + count - 1,
529 NULL, 0, NULL);
530 }
531
532 if (MESA_DEBUG_FLAGS & DEBUG_ALWAYS_FLUSH) {
533 _mesa_flush(ctx);
534 }
535 }
536
537
538 /**
539 * Execute a glRectf() function.
540 */
541 static void GLAPIENTRY
542 vbo_exec_Rectf(GLfloat x1, GLfloat y1, GLfloat x2, GLfloat y2)
543 {
544 GET_CURRENT_CONTEXT(ctx);
545 ASSERT_OUTSIDE_BEGIN_END(ctx);
546
547 CALL_Begin(GET_DISPATCH(), (GL_QUADS));
548 CALL_Vertex2f(GET_DISPATCH(), (x1, y1));
549 CALL_Vertex2f(GET_DISPATCH(), (x2, y1));
550 CALL_Vertex2f(GET_DISPATCH(), (x2, y2));
551 CALL_Vertex2f(GET_DISPATCH(), (x1, y2));
552 CALL_End(GET_DISPATCH(), ());
553 }
554
555
556 static void GLAPIENTRY
557 vbo_exec_EvalMesh1(GLenum mode, GLint i1, GLint i2)
558 {
559 GET_CURRENT_CONTEXT(ctx);
560 GLint i;
561 GLfloat u, du;
562 GLenum prim;
563
564 switch (mode) {
565 case GL_POINT:
566 prim = GL_POINTS;
567 break;
568 case GL_LINE:
569 prim = GL_LINE_STRIP;
570 break;
571 default:
572 _mesa_error( ctx, GL_INVALID_ENUM, "glEvalMesh1(mode)" );
573 return;
574 }
575
576 /* No effect if vertex maps disabled.
577 */
578 if (!ctx->Eval.Map1Vertex4 &&
579 !ctx->Eval.Map1Vertex3)
580 return;
581
582 du = ctx->Eval.MapGrid1du;
583 u = ctx->Eval.MapGrid1u1 + i1 * du;
584
585 CALL_Begin(GET_DISPATCH(), (prim));
586 for (i=i1;i<=i2;i++,u+=du) {
587 CALL_EvalCoord1f(GET_DISPATCH(), (u));
588 }
589 CALL_End(GET_DISPATCH(), ());
590 }
591
592
593 static void GLAPIENTRY
594 vbo_exec_EvalMesh2(GLenum mode, GLint i1, GLint i2, GLint j1, GLint j2)
595 {
596 GET_CURRENT_CONTEXT(ctx);
597 GLfloat u, du, v, dv, v1, u1;
598 GLint i, j;
599
600 switch (mode) {
601 case GL_POINT:
602 case GL_LINE:
603 case GL_FILL:
604 break;
605 default:
606 _mesa_error( ctx, GL_INVALID_ENUM, "glEvalMesh2(mode)" );
607 return;
608 }
609
610 /* No effect if vertex maps disabled.
611 */
612 if (!ctx->Eval.Map2Vertex4 &&
613 !ctx->Eval.Map2Vertex3)
614 return;
615
616 du = ctx->Eval.MapGrid2du;
617 dv = ctx->Eval.MapGrid2dv;
618 v1 = ctx->Eval.MapGrid2v1 + j1 * dv;
619 u1 = ctx->Eval.MapGrid2u1 + i1 * du;
620
621 switch (mode) {
622 case GL_POINT:
623 CALL_Begin(GET_DISPATCH(), (GL_POINTS));
624 for (v=v1,j=j1;j<=j2;j++,v+=dv) {
625 for (u=u1,i=i1;i<=i2;i++,u+=du) {
626 CALL_EvalCoord2f(GET_DISPATCH(), (u, v));
627 }
628 }
629 CALL_End(GET_DISPATCH(), ());
630 break;
631 case GL_LINE:
632 for (v=v1,j=j1;j<=j2;j++,v+=dv) {
633 CALL_Begin(GET_DISPATCH(), (GL_LINE_STRIP));
634 for (u=u1,i=i1;i<=i2;i++,u+=du) {
635 CALL_EvalCoord2f(GET_DISPATCH(), (u, v));
636 }
637 CALL_End(GET_DISPATCH(), ());
638 }
639 for (u=u1,i=i1;i<=i2;i++,u+=du) {
640 CALL_Begin(GET_DISPATCH(), (GL_LINE_STRIP));
641 for (v=v1,j=j1;j<=j2;j++,v+=dv) {
642 CALL_EvalCoord2f(GET_DISPATCH(), (u, v));
643 }
644 CALL_End(GET_DISPATCH(), ());
645 }
646 break;
647 case GL_FILL:
648 for (v=v1,j=j1;j<j2;j++,v+=dv) {
649 CALL_Begin(GET_DISPATCH(), (GL_TRIANGLE_STRIP));
650 for (u=u1,i=i1;i<=i2;i++,u+=du) {
651 CALL_EvalCoord2f(GET_DISPATCH(), (u, v));
652 CALL_EvalCoord2f(GET_DISPATCH(), (u, v+dv));
653 }
654 CALL_End(GET_DISPATCH(), ());
655 }
656 break;
657 }
658 }
659
660
661 /**
662 * Called from glDrawArrays when in immediate mode (not display list mode).
663 */
664 static void GLAPIENTRY
665 vbo_exec_DrawArrays(GLenum mode, GLint start, GLsizei count)
666 {
667 GET_CURRENT_CONTEXT(ctx);
668
669 if (MESA_VERBOSE & VERBOSE_DRAW)
670 _mesa_debug(ctx, "glDrawArrays(%s, %d, %d)\n",
671 _mesa_enum_to_string(mode), start, count);
672
673 if (!_mesa_validate_DrawArrays(ctx, mode, count))
674 return;
675
676 if (0)
677 check_draw_arrays_data(ctx, start, count);
678
679 vbo_draw_arrays(ctx, mode, start, count, 1, 0);
680
681 if (0)
682 print_draw_arrays(ctx, mode, start, count);
683 }
684
685
686 /**
687 * Called from glDrawArraysInstanced when in immediate mode (not
688 * display list mode).
689 */
690 static void GLAPIENTRY
691 vbo_exec_DrawArraysInstanced(GLenum mode, GLint start, GLsizei count,
692 GLsizei numInstances)
693 {
694 GET_CURRENT_CONTEXT(ctx);
695
696 if (MESA_VERBOSE & VERBOSE_DRAW)
697 _mesa_debug(ctx, "glDrawArraysInstanced(%s, %d, %d, %d)\n",
698 _mesa_enum_to_string(mode), start, count, numInstances);
699
700 if (!_mesa_validate_DrawArraysInstanced(ctx, mode, start, count, numInstances))
701 return;
702
703 if (0)
704 check_draw_arrays_data(ctx, start, count);
705
706 vbo_draw_arrays(ctx, mode, start, count, numInstances, 0);
707
708 if (0)
709 print_draw_arrays(ctx, mode, start, count);
710 }
711
712
713 /**
714 * Called from glDrawArraysInstancedBaseInstance when in immediate mode.
715 */
716 static void GLAPIENTRY
717 vbo_exec_DrawArraysInstancedBaseInstance(GLenum mode, GLint first, GLsizei count,
718 GLsizei numInstances, GLuint baseInstance)
719 {
720 GET_CURRENT_CONTEXT(ctx);
721
722 if (MESA_VERBOSE & VERBOSE_DRAW)
723 _mesa_debug(ctx, "glDrawArraysInstancedBaseInstance(%s, %d, %d, %d, %d)\n",
724 _mesa_enum_to_string(mode), first, count,
725 numInstances, baseInstance);
726
727 if (!_mesa_validate_DrawArraysInstanced(ctx, mode, first, count,
728 numInstances))
729 return;
730
731 if (0)
732 check_draw_arrays_data(ctx, first, count);
733
734 vbo_draw_arrays(ctx, mode, first, count, numInstances, baseInstance);
735
736 if (0)
737 print_draw_arrays(ctx, mode, first, count);
738 }
739
740
741
742 /**
743 * Map GL_ELEMENT_ARRAY_BUFFER and print contents.
744 * For debugging.
745 */
746 #if 0
747 static void
748 dump_element_buffer(struct gl_context *ctx, GLenum type)
749 {
750 const GLvoid *map =
751 ctx->Driver.MapBufferRange(ctx, 0,
752 ctx->Array.VAO->IndexBufferObj->Size,
753 GL_MAP_READ_BIT,
754 ctx->Array.VAO->IndexBufferObj,
755 MAP_INTERNAL);
756 switch (type) {
757 case GL_UNSIGNED_BYTE:
758 {
759 const GLubyte *us = (const GLubyte *) map;
760 GLint i;
761 for (i = 0; i < ctx->Array.VAO->IndexBufferObj->Size; i++) {
762 printf("%02x ", us[i]);
763 if (i % 32 == 31)
764 printf("\n");
765 }
766 printf("\n");
767 }
768 break;
769 case GL_UNSIGNED_SHORT:
770 {
771 const GLushort *us = (const GLushort *) map;
772 GLint i;
773 for (i = 0; i < ctx->Array.VAO->IndexBufferObj->Size / 2; i++) {
774 printf("%04x ", us[i]);
775 if (i % 16 == 15)
776 printf("\n");
777 }
778 printf("\n");
779 }
780 break;
781 case GL_UNSIGNED_INT:
782 {
783 const GLuint *us = (const GLuint *) map;
784 GLint i;
785 for (i = 0; i < ctx->Array.VAO->IndexBufferObj->Size / 4; i++) {
786 printf("%08x ", us[i]);
787 if (i % 8 == 7)
788 printf("\n");
789 }
790 printf("\n");
791 }
792 break;
793 default:
794 ;
795 }
796
797 ctx->Driver.UnmapBuffer(ctx, ctx->Array.VAO->IndexBufferObj,
798 MAP_INTERNAL);
799 }
800 #endif
801
802
803 /**
804 * Inner support for both _mesa_DrawElements and _mesa_DrawRangeElements.
805 * Do the rendering for a glDrawElements or glDrawRangeElements call after
806 * we've validated buffer bounds, etc.
807 */
808 static void
809 vbo_validated_drawrangeelements(struct gl_context *ctx, GLenum mode,
810 GLboolean index_bounds_valid,
811 GLuint start, GLuint end,
812 GLsizei count, GLenum type,
813 const GLvoid *indices,
814 GLint basevertex, GLuint numInstances,
815 GLuint baseInstance)
816 {
817 struct vbo_context *vbo = vbo_context(ctx);
818 struct _mesa_index_buffer ib;
819 struct _mesa_prim prim[1];
820
821 if (!vbo_bind_arrays(ctx))
822 return;
823
824 ib.count = count;
825 ib.type = type;
826 ib.obj = ctx->Array.VAO->IndexBufferObj;
827 ib.ptr = indices;
828
829 prim[0].begin = 1;
830 prim[0].end = 1;
831 prim[0].weak = 0;
832 prim[0].pad = 0;
833 prim[0].mode = mode;
834 prim[0].start = 0;
835 prim[0].count = count;
836 prim[0].indexed = 1;
837 prim[0].is_indirect = 0;
838 prim[0].basevertex = basevertex;
839 prim[0].num_instances = numInstances;
840 prim[0].base_instance = baseInstance;
841
842 /* Need to give special consideration to rendering a range of
843 * indices starting somewhere above zero. Typically the
844 * application is issuing multiple DrawRangeElements() to draw
845 * successive primitives layed out linearly in the vertex arrays.
846 * Unless the vertex arrays are all in a VBO (or locked as with
847 * CVA), the OpenGL semantics imply that we need to re-read or
848 * re-upload the vertex data on each draw call.
849 *
850 * In the case of hardware tnl, we want to avoid starting the
851 * upload at zero, as it will mean every draw call uploads an
852 * increasing amount of not-used vertex data. Worse - in the
853 * software tnl module, all those vertices might be transformed and
854 * lit but never rendered.
855 *
856 * If we just upload or transform the vertices in start..end,
857 * however, the indices will be incorrect.
858 *
859 * At this level, we don't know exactly what the requirements of
860 * the backend are going to be, though it will likely boil down to
861 * either:
862 *
863 * 1) Do nothing, everything is in a VBO and is processed once
864 * only.
865 *
866 * 2) Adjust the indices and vertex arrays so that start becomes
867 * zero.
868 *
869 * Rather than doing anything here, I'll provide a helper function
870 * for the latter case elsewhere.
871 */
872
873 vbo->draw_prims(ctx, prim, 1, &ib,
874 index_bounds_valid, start, end, NULL, 0, NULL);
875
876 if (MESA_DEBUG_FLAGS & DEBUG_ALWAYS_FLUSH) {
877 _mesa_flush(ctx);
878 }
879 }
880
881
882 /**
883 * Called by glDrawRangeElementsBaseVertex() in immediate mode.
884 */
885 static void GLAPIENTRY
886 vbo_exec_DrawRangeElementsBaseVertex(GLenum mode,
887 GLuint start, GLuint end,
888 GLsizei count, GLenum type,
889 const GLvoid *indices,
890 GLint basevertex)
891 {
892 static GLuint warnCount = 0;
893 GLboolean index_bounds_valid = GL_TRUE;
894
895 /* This is only useful to catch invalid values in the "end" parameter
896 * like ~0.
897 */
898 GLuint max_element = 2 * 1000 * 1000 * 1000; /* just a big number */
899
900 GET_CURRENT_CONTEXT(ctx);
901
902 if (MESA_VERBOSE & VERBOSE_DRAW)
903 _mesa_debug(ctx,
904 "glDrawRangeElementsBaseVertex(%s, %u, %u, %d, %s, %p, %d)\n",
905 _mesa_enum_to_string(mode), start, end, count,
906 _mesa_enum_to_string(type), indices, basevertex);
907
908 if (!_mesa_validate_DrawRangeElements(ctx, mode, start, end, count,
909 type, indices))
910 return;
911
912 if ((int) end + basevertex < 0 ||
913 start + basevertex >= max_element) {
914 /* The application requested we draw using a range of indices that's
915 * outside the bounds of the current VBO. This is invalid and appears
916 * to give undefined results. The safest thing to do is to simply
917 * ignore the range, in case the application botched their range tracking
918 * but did provide valid indices. Also issue a warning indicating that
919 * the application is broken.
920 */
921 if (warnCount++ < 10) {
922 _mesa_warning(ctx, "glDrawRangeElements(start %u, end %u, "
923 "basevertex %d, count %d, type 0x%x, indices=%p):\n"
924 "\trange is outside VBO bounds (max=%u); ignoring.\n"
925 "\tThis should be fixed in the application.",
926 start, end, basevertex, count, type, indices,
927 max_element - 1);
928 }
929 index_bounds_valid = GL_FALSE;
930 }
931
932 /* NOTE: It's important that 'end' is a reasonable value.
933 * in _tnl_draw_prims(), we use end to determine how many vertices
934 * to transform. If it's too large, we can unnecessarily split prims
935 * or we can read/write out of memory in several different places!
936 */
937
938 /* Catch/fix some potential user errors */
939 if (type == GL_UNSIGNED_BYTE) {
940 start = MIN2(start, 0xff);
941 end = MIN2(end, 0xff);
942 }
943 else if (type == GL_UNSIGNED_SHORT) {
944 start = MIN2(start, 0xffff);
945 end = MIN2(end, 0xffff);
946 }
947
948 if (0) {
949 printf("glDraw[Range]Elements{,BaseVertex}"
950 "(start %u, end %u, type 0x%x, count %d) ElemBuf %u, "
951 "base %d\n",
952 start, end, type, count,
953 ctx->Array.VAO->IndexBufferObj->Name,
954 basevertex);
955 }
956
957 if ((int) start + basevertex < 0 ||
958 end + basevertex >= max_element)
959 index_bounds_valid = GL_FALSE;
960
961 #if 0
962 check_draw_elements_data(ctx, count, type, indices);
963 #else
964 (void) check_draw_elements_data;
965 #endif
966
967 vbo_validated_drawrangeelements(ctx, mode, index_bounds_valid, start, end,
968 count, type, indices, basevertex, 1, 0);
969 }
970
971
972 /**
973 * Called by glDrawRangeElements() in immediate mode.
974 */
975 static void GLAPIENTRY
976 vbo_exec_DrawRangeElements(GLenum mode, GLuint start, GLuint end,
977 GLsizei count, GLenum type, const GLvoid *indices)
978 {
979 if (MESA_VERBOSE & VERBOSE_DRAW) {
980 GET_CURRENT_CONTEXT(ctx);
981 _mesa_debug(ctx,
982 "glDrawRangeElements(%s, %u, %u, %d, %s, %p)\n",
983 _mesa_enum_to_string(mode), start, end, count,
984 _mesa_enum_to_string(type), indices);
985 }
986
987 vbo_exec_DrawRangeElementsBaseVertex(mode, start, end, count, type,
988 indices, 0);
989 }
990
991
992 /**
993 * Called by glDrawElements() in immediate mode.
994 */
995 static void GLAPIENTRY
996 vbo_exec_DrawElements(GLenum mode, GLsizei count, GLenum type,
997 const GLvoid *indices)
998 {
999 GET_CURRENT_CONTEXT(ctx);
1000
1001 if (MESA_VERBOSE & VERBOSE_DRAW)
1002 _mesa_debug(ctx, "glDrawElements(%s, %u, %s, %p)\n",
1003 _mesa_enum_to_string(mode), count,
1004 _mesa_enum_to_string(type), indices);
1005
1006 if (!_mesa_validate_DrawElements(ctx, mode, count, type, indices))
1007 return;
1008
1009 vbo_validated_drawrangeelements(ctx, mode, GL_FALSE, ~0, ~0,
1010 count, type, indices, 0, 1, 0);
1011 }
1012
1013
1014 /**
1015 * Called by glDrawElementsBaseVertex() in immediate mode.
1016 */
1017 static void GLAPIENTRY
1018 vbo_exec_DrawElementsBaseVertex(GLenum mode, GLsizei count, GLenum type,
1019 const GLvoid *indices, GLint basevertex)
1020 {
1021 GET_CURRENT_CONTEXT(ctx);
1022
1023 if (MESA_VERBOSE & VERBOSE_DRAW)
1024 _mesa_debug(ctx, "glDrawElementsBaseVertex(%s, %d, %s, %p, %d)\n",
1025 _mesa_enum_to_string(mode), count,
1026 _mesa_enum_to_string(type), indices, basevertex);
1027
1028 if (!_mesa_validate_DrawElements(ctx, mode, count, type, indices))
1029 return;
1030
1031 vbo_validated_drawrangeelements(ctx, mode, GL_FALSE, ~0, ~0,
1032 count, type, indices, basevertex, 1, 0);
1033 }
1034
1035
1036 /**
1037 * Called by glDrawElementsInstanced() in immediate mode.
1038 */
1039 static void GLAPIENTRY
1040 vbo_exec_DrawElementsInstanced(GLenum mode, GLsizei count, GLenum type,
1041 const GLvoid *indices, GLsizei numInstances)
1042 {
1043 GET_CURRENT_CONTEXT(ctx);
1044
1045 if (MESA_VERBOSE & VERBOSE_DRAW)
1046 _mesa_debug(ctx, "glDrawElementsInstanced(%s, %d, %s, %p, %d)\n",
1047 _mesa_enum_to_string(mode), count,
1048 _mesa_enum_to_string(type), indices, numInstances);
1049
1050 if (!_mesa_validate_DrawElementsInstanced(ctx, mode, count, type, indices,
1051 numInstances))
1052 return;
1053
1054 vbo_validated_drawrangeelements(ctx, mode, GL_FALSE, ~0, ~0,
1055 count, type, indices, 0, numInstances, 0);
1056 }
1057
1058
1059 /**
1060 * Called by glDrawElementsInstancedBaseVertex() in immediate mode.
1061 */
1062 static void GLAPIENTRY
1063 vbo_exec_DrawElementsInstancedBaseVertex(GLenum mode, GLsizei count, GLenum type,
1064 const GLvoid *indices, GLsizei numInstances,
1065 GLint basevertex)
1066 {
1067 GET_CURRENT_CONTEXT(ctx);
1068
1069 if (MESA_VERBOSE & VERBOSE_DRAW)
1070 _mesa_debug(ctx, "glDrawElementsInstancedBaseVertex(%s, %d, %s, %p, %d; %d)\n",
1071 _mesa_enum_to_string(mode), count,
1072 _mesa_enum_to_string(type), indices,
1073 numInstances, basevertex);
1074
1075 if (!_mesa_validate_DrawElementsInstanced(ctx, mode, count, type, indices,
1076 numInstances))
1077 return;
1078
1079 vbo_validated_drawrangeelements(ctx, mode, GL_FALSE, ~0, ~0,
1080 count, type, indices, basevertex, numInstances, 0);
1081 }
1082
1083
1084 /**
1085 * Called by glDrawElementsInstancedBaseInstance() in immediate mode.
1086 */
1087 static void GLAPIENTRY
1088 vbo_exec_DrawElementsInstancedBaseInstance(GLenum mode, GLsizei count, GLenum type,
1089 const GLvoid *indices, GLsizei numInstances,
1090 GLuint baseInstance)
1091 {
1092 GET_CURRENT_CONTEXT(ctx);
1093
1094 if (MESA_VERBOSE & VERBOSE_DRAW)
1095 _mesa_debug(ctx, "glDrawElementsInstancedBaseInstance(%s, %d, %s, %p, %d, %d)\n",
1096 _mesa_enum_to_string(mode), count,
1097 _mesa_enum_to_string(type), indices,
1098 numInstances, baseInstance);
1099
1100 if (!_mesa_validate_DrawElementsInstanced(ctx, mode, count, type, indices,
1101 numInstances))
1102 return;
1103
1104 vbo_validated_drawrangeelements(ctx, mode, GL_FALSE, ~0, ~0,
1105 count, type, indices, 0, numInstances,
1106 baseInstance);
1107 }
1108
1109
1110 /**
1111 * Called by glDrawElementsInstancedBaseVertexBaseInstance() in immediate mode.
1112 */
1113 static void GLAPIENTRY
1114 vbo_exec_DrawElementsInstancedBaseVertexBaseInstance(GLenum mode, GLsizei count, GLenum type,
1115 const GLvoid *indices, GLsizei numInstances,
1116 GLint basevertex, GLuint baseInstance)
1117 {
1118 GET_CURRENT_CONTEXT(ctx);
1119
1120 if (MESA_VERBOSE & VERBOSE_DRAW)
1121 _mesa_debug(ctx, "glDrawElementsInstancedBaseVertexBaseInstance(%s, %d, %s, %p, %d, %d, %d)\n",
1122 _mesa_enum_to_string(mode), count,
1123 _mesa_enum_to_string(type), indices,
1124 numInstances, basevertex, baseInstance);
1125
1126 if (!_mesa_validate_DrawElementsInstanced(ctx, mode, count, type, indices,
1127 numInstances))
1128 return;
1129
1130 vbo_validated_drawrangeelements(ctx, mode, GL_FALSE, ~0, ~0,
1131 count, type, indices, basevertex, numInstances,
1132 baseInstance);
1133 }
1134
1135
1136 /**
1137 * Inner support for both _mesa_MultiDrawElements() and
1138 * _mesa_MultiDrawRangeElements().
1139 * This does the actual rendering after we've checked array indexes, etc.
1140 */
1141 static void
1142 vbo_validated_multidrawelements(struct gl_context *ctx, GLenum mode,
1143 const GLsizei *count, GLenum type,
1144 const GLvoid * const *indices,
1145 GLsizei primcount,
1146 const GLint *basevertex)
1147 {
1148 struct vbo_context *vbo = vbo_context(ctx);
1149 struct _mesa_index_buffer ib;
1150 struct _mesa_prim *prim;
1151 unsigned int index_type_size = vbo_sizeof_ib_type(type);
1152 uintptr_t min_index_ptr, max_index_ptr;
1153 GLboolean fallback = GL_FALSE;
1154 int i;
1155
1156 if (primcount == 0)
1157 return;
1158
1159 prim = calloc(primcount, sizeof(*prim));
1160 if (prim == NULL) {
1161 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glMultiDrawElements");
1162 return;
1163 }
1164
1165 if (!vbo_bind_arrays(ctx)) {
1166 free(prim);
1167 return;
1168 }
1169
1170 min_index_ptr = (uintptr_t)indices[0];
1171 max_index_ptr = 0;
1172 for (i = 0; i < primcount; i++) {
1173 min_index_ptr = MIN2(min_index_ptr, (uintptr_t)indices[i]);
1174 max_index_ptr = MAX2(max_index_ptr, (uintptr_t)indices[i] +
1175 index_type_size * count[i]);
1176 }
1177
1178 /* Check if we can handle this thing as a bunch of index offsets from the
1179 * same index pointer. If we can't, then we have to fall back to doing
1180 * a draw_prims per primitive.
1181 * Check that the difference between each prim's indexes is a multiple of
1182 * the index/element size.
1183 */
1184 if (index_type_size != 1) {
1185 for (i = 0; i < primcount; i++) {
1186 if ((((uintptr_t)indices[i] - min_index_ptr) % index_type_size) != 0) {
1187 fallback = GL_TRUE;
1188 break;
1189 }
1190 }
1191 }
1192
1193 /* Draw primitives individually if one count is zero, so we can easily skip
1194 * that primitive.
1195 */
1196 for (i = 0; i < primcount; i++) {
1197 if (count[i] == 0) {
1198 fallback = GL_TRUE;
1199 break;
1200 }
1201 }
1202
1203 /* If the index buffer isn't in a VBO, then treating the application's
1204 * subranges of the index buffer as one large index buffer may lead to
1205 * us reading unmapped memory.
1206 */
1207 if (!_mesa_is_bufferobj(ctx->Array.VAO->IndexBufferObj))
1208 fallback = GL_TRUE;
1209
1210 if (!fallback) {
1211 ib.count = (max_index_ptr - min_index_ptr) / index_type_size;
1212 ib.type = type;
1213 ib.obj = ctx->Array.VAO->IndexBufferObj;
1214 ib.ptr = (void *)min_index_ptr;
1215
1216 for (i = 0; i < primcount; i++) {
1217 prim[i].begin = (i == 0);
1218 prim[i].end = (i == primcount - 1);
1219 prim[i].weak = 0;
1220 prim[i].pad = 0;
1221 prim[i].mode = mode;
1222 prim[i].start = ((uintptr_t)indices[i] - min_index_ptr) / index_type_size;
1223 prim[i].count = count[i];
1224 prim[i].indexed = 1;
1225 prim[i].num_instances = 1;
1226 prim[i].base_instance = 0;
1227 prim[i].draw_id = i;
1228 prim[i].is_indirect = 0;
1229 if (basevertex != NULL)
1230 prim[i].basevertex = basevertex[i];
1231 else
1232 prim[i].basevertex = 0;
1233 }
1234
1235 vbo->draw_prims(ctx, prim, primcount, &ib,
1236 false, ~0, ~0, NULL, 0, NULL);
1237 } else {
1238 /* render one prim at a time */
1239 for (i = 0; i < primcount; i++) {
1240 if (count[i] == 0)
1241 continue;
1242 ib.count = count[i];
1243 ib.type = type;
1244 ib.obj = ctx->Array.VAO->IndexBufferObj;
1245 ib.ptr = indices[i];
1246
1247 prim[0].begin = 1;
1248 prim[0].end = 1;
1249 prim[0].weak = 0;
1250 prim[0].pad = 0;
1251 prim[0].mode = mode;
1252 prim[0].start = 0;
1253 prim[0].count = count[i];
1254 prim[0].indexed = 1;
1255 prim[0].num_instances = 1;
1256 prim[0].base_instance = 0;
1257 prim[0].draw_id = i;
1258 prim[0].is_indirect = 0;
1259 if (basevertex != NULL)
1260 prim[0].basevertex = basevertex[i];
1261 else
1262 prim[0].basevertex = 0;
1263
1264 vbo->draw_prims(ctx, prim, 1, &ib,
1265 false, ~0, ~0, NULL, 0, NULL);
1266 }
1267 }
1268
1269 free(prim);
1270
1271 if (MESA_DEBUG_FLAGS & DEBUG_ALWAYS_FLUSH) {
1272 _mesa_flush(ctx);
1273 }
1274 }
1275
1276
1277 static void GLAPIENTRY
1278 vbo_exec_MultiDrawElements(GLenum mode,
1279 const GLsizei *count, GLenum type,
1280 const GLvoid * const *indices,
1281 GLsizei primcount)
1282 {
1283 GET_CURRENT_CONTEXT(ctx);
1284
1285 if (!_mesa_validate_MultiDrawElements(ctx, mode, count, type, indices,
1286 primcount))
1287 return;
1288
1289 vbo_validated_multidrawelements(ctx, mode, count, type, indices, primcount,
1290 NULL);
1291 }
1292
1293
1294 static void GLAPIENTRY
1295 vbo_exec_MultiDrawElementsBaseVertex(GLenum mode,
1296 const GLsizei *count, GLenum type,
1297 const GLvoid * const *indices,
1298 GLsizei primcount,
1299 const GLsizei *basevertex)
1300 {
1301 GET_CURRENT_CONTEXT(ctx);
1302
1303 if (!_mesa_validate_MultiDrawElements(ctx, mode, count, type, indices,
1304 primcount))
1305 return;
1306
1307 vbo_validated_multidrawelements(ctx, mode, count, type, indices, primcount,
1308 basevertex);
1309 }
1310
1311 static void
1312 vbo_draw_transform_feedback(struct gl_context *ctx, GLenum mode,
1313 struct gl_transform_feedback_object *obj,
1314 GLuint stream, GLuint numInstances)
1315 {
1316 struct vbo_context *vbo = vbo_context(ctx);
1317 struct _mesa_prim prim[2];
1318
1319 if (!_mesa_validate_DrawTransformFeedback(ctx, mode, obj, stream,
1320 numInstances)) {
1321 return;
1322 }
1323
1324 if (ctx->Driver.GetTransformFeedbackVertexCount &&
1325 (ctx->Const.AlwaysUseGetTransformFeedbackVertexCount ||
1326 !_mesa_all_varyings_in_vbos(ctx->Array.VAO))) {
1327 GLsizei n = ctx->Driver.GetTransformFeedbackVertexCount(ctx, obj, stream);
1328 vbo_draw_arrays(ctx, mode, 0, n, numInstances, 0);
1329 return;
1330 }
1331
1332 if (!vbo_bind_arrays(ctx))
1333 return;
1334
1335 /* init most fields to zero */
1336 memset(prim, 0, sizeof(prim));
1337 prim[0].begin = 1;
1338 prim[0].end = 1;
1339 prim[0].mode = mode;
1340 prim[0].num_instances = numInstances;
1341 prim[0].base_instance = 0;
1342 prim[0].is_indirect = 0;
1343
1344 /* Maybe we should do some primitive splitting for primitive restart
1345 * (like in DrawArrays), but we have no way to know how many vertices
1346 * will be rendered. */
1347
1348 vbo->draw_prims(ctx, prim, 1, NULL,
1349 GL_FALSE, ~0, ~0, obj, stream, NULL);
1350
1351 if (MESA_DEBUG_FLAGS & DEBUG_ALWAYS_FLUSH) {
1352 _mesa_flush(ctx);
1353 }
1354 }
1355
1356 /**
1357 * Like DrawArrays, but take the count from a transform feedback object.
1358 * \param mode GL_POINTS, GL_LINES, GL_TRIANGLE_STRIP, etc.
1359 * \param name the transform feedback object
1360 * User still has to setup of the vertex attribute info with
1361 * glVertexPointer, glColorPointer, etc.
1362 * Part of GL_ARB_transform_feedback2.
1363 */
1364 static void GLAPIENTRY
1365 vbo_exec_DrawTransformFeedback(GLenum mode, GLuint name)
1366 {
1367 GET_CURRENT_CONTEXT(ctx);
1368 struct gl_transform_feedback_object *obj =
1369 _mesa_lookup_transform_feedback_object(ctx, name);
1370
1371 if (MESA_VERBOSE & VERBOSE_DRAW)
1372 _mesa_debug(ctx, "glDrawTransformFeedback(%s, %d)\n",
1373 _mesa_enum_to_string(mode), name);
1374
1375 vbo_draw_transform_feedback(ctx, mode, obj, 0, 1);
1376 }
1377
1378 static void GLAPIENTRY
1379 vbo_exec_DrawTransformFeedbackStream(GLenum mode, GLuint name, GLuint stream)
1380 {
1381 GET_CURRENT_CONTEXT(ctx);
1382 struct gl_transform_feedback_object *obj =
1383 _mesa_lookup_transform_feedback_object(ctx, name);
1384
1385 if (MESA_VERBOSE & VERBOSE_DRAW)
1386 _mesa_debug(ctx, "glDrawTransformFeedbackStream(%s, %u, %u)\n",
1387 _mesa_enum_to_string(mode), name, stream);
1388
1389 vbo_draw_transform_feedback(ctx, mode, obj, stream, 1);
1390 }
1391
1392 static void GLAPIENTRY
1393 vbo_exec_DrawTransformFeedbackInstanced(GLenum mode, GLuint name,
1394 GLsizei primcount)
1395 {
1396 GET_CURRENT_CONTEXT(ctx);
1397 struct gl_transform_feedback_object *obj =
1398 _mesa_lookup_transform_feedback_object(ctx, name);
1399
1400 if (MESA_VERBOSE & VERBOSE_DRAW)
1401 _mesa_debug(ctx, "glDrawTransformFeedbackInstanced(%s, %d)\n",
1402 _mesa_enum_to_string(mode), name);
1403
1404 vbo_draw_transform_feedback(ctx, mode, obj, 0, primcount);
1405 }
1406
1407 static void GLAPIENTRY
1408 vbo_exec_DrawTransformFeedbackStreamInstanced(GLenum mode, GLuint name,
1409 GLuint stream, GLsizei primcount)
1410 {
1411 GET_CURRENT_CONTEXT(ctx);
1412 struct gl_transform_feedback_object *obj =
1413 _mesa_lookup_transform_feedback_object(ctx, name);
1414
1415 if (MESA_VERBOSE & VERBOSE_DRAW)
1416 _mesa_debug(ctx, "glDrawTransformFeedbackStreamInstanced"
1417 "(%s, %u, %u, %i)\n",
1418 _mesa_enum_to_string(mode), name, stream, primcount);
1419
1420 vbo_draw_transform_feedback(ctx, mode, obj, stream, primcount);
1421 }
1422
1423 static void
1424 vbo_validated_drawarraysindirect(struct gl_context *ctx,
1425 GLenum mode, const GLvoid *indirect)
1426 {
1427 struct vbo_context *vbo = vbo_context(ctx);
1428
1429 if (!vbo_bind_arrays(ctx))
1430 return;
1431
1432 vbo->draw_indirect_prims(ctx, mode,
1433 ctx->DrawIndirectBuffer, (GLsizeiptr)indirect,
1434 1 /* draw_count */, 16 /* stride */,
1435 NULL, 0, NULL);
1436
1437 if (MESA_DEBUG_FLAGS & DEBUG_ALWAYS_FLUSH)
1438 _mesa_flush(ctx);
1439 }
1440
1441 static void
1442 vbo_validated_multidrawarraysindirect(struct gl_context *ctx,
1443 GLenum mode,
1444 const GLvoid *indirect,
1445 GLsizei primcount, GLsizei stride)
1446 {
1447 struct vbo_context *vbo = vbo_context(ctx);
1448 GLsizeiptr offset = (GLsizeiptr)indirect;
1449
1450 if (primcount == 0)
1451 return;
1452
1453 if (!vbo_bind_arrays(ctx))
1454 return;
1455
1456 vbo->draw_indirect_prims(ctx, mode,
1457 ctx->DrawIndirectBuffer, offset,
1458 primcount, stride,
1459 NULL, 0, NULL);
1460
1461 if (MESA_DEBUG_FLAGS & DEBUG_ALWAYS_FLUSH)
1462 _mesa_flush(ctx);
1463 }
1464
1465 static void
1466 vbo_validated_drawelementsindirect(struct gl_context *ctx,
1467 GLenum mode, GLenum type,
1468 const GLvoid *indirect)
1469 {
1470 struct vbo_context *vbo = vbo_context(ctx);
1471 struct _mesa_index_buffer ib;
1472
1473 if (!vbo_bind_arrays(ctx))
1474 return;
1475
1476 ib.count = 0; /* unknown */
1477 ib.type = type;
1478 ib.obj = ctx->Array.VAO->IndexBufferObj;
1479 ib.ptr = NULL;
1480
1481 vbo->draw_indirect_prims(ctx, mode,
1482 ctx->DrawIndirectBuffer, (GLsizeiptr)indirect,
1483 1 /* draw_count */, 20 /* stride */,
1484 NULL, 0,
1485 &ib);
1486
1487 if (MESA_DEBUG_FLAGS & DEBUG_ALWAYS_FLUSH)
1488 _mesa_flush(ctx);
1489 }
1490
1491 static void
1492 vbo_validated_multidrawelementsindirect(struct gl_context *ctx,
1493 GLenum mode, GLenum type,
1494 const GLvoid *indirect,
1495 GLsizei primcount, GLsizei stride)
1496 {
1497 struct vbo_context *vbo = vbo_context(ctx);
1498 struct _mesa_index_buffer ib;
1499 GLsizeiptr offset = (GLsizeiptr)indirect;
1500
1501 if (primcount == 0)
1502 return;
1503
1504 if (!vbo_bind_arrays(ctx))
1505 return;
1506
1507 /* NOTE: IndexBufferObj is guaranteed to be a VBO. */
1508
1509 ib.count = 0; /* unknown */
1510 ib.type = type;
1511 ib.obj = ctx->Array.VAO->IndexBufferObj;
1512 ib.ptr = NULL;
1513
1514 vbo->draw_indirect_prims(ctx, mode,
1515 ctx->DrawIndirectBuffer, offset,
1516 primcount, stride,
1517 NULL, 0,
1518 &ib);
1519
1520 if (MESA_DEBUG_FLAGS & DEBUG_ALWAYS_FLUSH)
1521 _mesa_flush(ctx);
1522 }
1523
1524 /**
1525 * Like [Multi]DrawArrays/Elements, but they take most arguments from
1526 * a buffer object.
1527 */
1528 static void GLAPIENTRY
1529 vbo_exec_DrawArraysIndirect(GLenum mode, const GLvoid *indirect)
1530 {
1531 GET_CURRENT_CONTEXT(ctx);
1532
1533 if (MESA_VERBOSE & VERBOSE_DRAW)
1534 _mesa_debug(ctx, "glDrawArraysIndirect(%s, %p)\n",
1535 _mesa_enum_to_string(mode), indirect);
1536
1537 if (!_mesa_validate_DrawArraysIndirect(ctx, mode, indirect))
1538 return;
1539
1540 vbo_validated_drawarraysindirect(ctx, mode, indirect);
1541 }
1542
1543 static void GLAPIENTRY
1544 vbo_exec_DrawElementsIndirect(GLenum mode, GLenum type,
1545 const GLvoid *indirect)
1546 {
1547 GET_CURRENT_CONTEXT(ctx);
1548
1549 if (MESA_VERBOSE & VERBOSE_DRAW)
1550 _mesa_debug(ctx, "glDrawElementsIndirect(%s, %s, %p)\n",
1551 _mesa_enum_to_string(mode),
1552 _mesa_enum_to_string(type), indirect);
1553
1554 if (!_mesa_validate_DrawElementsIndirect(ctx, mode, type, indirect))
1555 return;
1556
1557 vbo_validated_drawelementsindirect(ctx, mode, type, indirect);
1558 }
1559
1560 static void GLAPIENTRY
1561 vbo_exec_MultiDrawArraysIndirect(GLenum mode,
1562 const GLvoid *indirect,
1563 GLsizei primcount, GLsizei stride)
1564 {
1565 GET_CURRENT_CONTEXT(ctx);
1566
1567 if (MESA_VERBOSE & VERBOSE_DRAW)
1568 _mesa_debug(ctx, "glMultiDrawArraysIndirect(%s, %p, %i, %i)\n",
1569 _mesa_enum_to_string(mode), indirect, primcount, stride);
1570
1571 /* If <stride> is zero, the array elements are treated as tightly packed. */
1572 if (stride == 0)
1573 stride = 4 * sizeof(GLuint); /* sizeof(DrawArraysIndirectCommand) */
1574
1575 if (!_mesa_validate_MultiDrawArraysIndirect(ctx, mode,
1576 indirect,
1577 primcount, stride))
1578 return;
1579
1580 vbo_validated_multidrawarraysindirect(ctx, mode,
1581 indirect,
1582 primcount, stride);
1583 }
1584
1585 static void GLAPIENTRY
1586 vbo_exec_MultiDrawElementsIndirect(GLenum mode, GLenum type,
1587 const GLvoid *indirect,
1588 GLsizei primcount, GLsizei stride)
1589 {
1590 GET_CURRENT_CONTEXT(ctx);
1591
1592 if (MESA_VERBOSE & VERBOSE_DRAW)
1593 _mesa_debug(ctx, "glMultiDrawElementsIndirect(%s, %s, %p, %i, %i)\n",
1594 _mesa_enum_to_string(mode),
1595 _mesa_enum_to_string(type), indirect, primcount, stride);
1596
1597 /* If <stride> is zero, the array elements are treated as tightly packed. */
1598 if (stride == 0)
1599 stride = 5 * sizeof(GLuint); /* sizeof(DrawElementsIndirectCommand) */
1600
1601 if (!_mesa_validate_MultiDrawElementsIndirect(ctx, mode, type,
1602 indirect,
1603 primcount, stride))
1604 return;
1605
1606 vbo_validated_multidrawelementsindirect(ctx, mode, type,
1607 indirect,
1608 primcount, stride);
1609 }
1610
1611 static void
1612 vbo_validated_multidrawarraysindirectcount(struct gl_context *ctx,
1613 GLenum mode,
1614 GLintptr indirect,
1615 GLintptr drawcount,
1616 GLsizei maxdrawcount,
1617 GLsizei stride)
1618 {
1619 struct vbo_context *vbo = vbo_context(ctx);
1620 GLsizeiptr offset = indirect;
1621
1622 if (maxdrawcount == 0)
1623 return;
1624
1625 if (!vbo_bind_arrays(ctx))
1626 return;
1627
1628 vbo->draw_indirect_prims(ctx, mode,
1629 ctx->DrawIndirectBuffer, offset,
1630 maxdrawcount, stride,
1631 ctx->ParameterBuffer, drawcount,
1632 NULL);
1633
1634 if (MESA_DEBUG_FLAGS & DEBUG_ALWAYS_FLUSH)
1635 _mesa_flush(ctx);
1636 }
1637
1638 static void
1639 vbo_validated_multidrawelementsindirectcount(struct gl_context *ctx,
1640 GLenum mode, GLenum type,
1641 GLintptr indirect,
1642 GLintptr drawcount,
1643 GLsizei maxdrawcount,
1644 GLsizei stride)
1645 {
1646 struct vbo_context *vbo = vbo_context(ctx);
1647 struct _mesa_index_buffer ib;
1648 GLsizeiptr offset = (GLsizeiptr)indirect;
1649
1650 if (maxdrawcount == 0)
1651 return;
1652
1653 if (!vbo_bind_arrays(ctx))
1654 return;
1655
1656 /* NOTE: IndexBufferObj is guaranteed to be a VBO. */
1657
1658 ib.count = 0; /* unknown */
1659 ib.type = type;
1660 ib.obj = ctx->Array.VAO->IndexBufferObj;
1661 ib.ptr = NULL;
1662
1663 vbo->draw_indirect_prims(ctx, mode,
1664 ctx->DrawIndirectBuffer, offset,
1665 maxdrawcount, stride,
1666 ctx->ParameterBuffer, drawcount,
1667 &ib);
1668
1669 if (MESA_DEBUG_FLAGS & DEBUG_ALWAYS_FLUSH)
1670 _mesa_flush(ctx);
1671 }
1672
1673 static void GLAPIENTRY
1674 vbo_exec_MultiDrawArraysIndirectCount(GLenum mode,
1675 GLintptr indirect,
1676 GLintptr drawcount,
1677 GLsizei maxdrawcount, GLsizei stride)
1678 {
1679 GET_CURRENT_CONTEXT(ctx);
1680
1681 if (MESA_VERBOSE & VERBOSE_DRAW)
1682 _mesa_debug(ctx, "glMultiDrawArraysIndirectCountARB"
1683 "(%s, %lx, %lx, %i, %i)\n",
1684 _mesa_enum_to_string(mode), indirect,
1685 drawcount, maxdrawcount, stride);
1686
1687 /* If <stride> is zero, the array elements are treated as tightly packed. */
1688 if (stride == 0)
1689 stride = 4 * sizeof(GLuint); /* sizeof(DrawArraysIndirectCommand) */
1690
1691 if (!_mesa_validate_MultiDrawArraysIndirectCount(ctx, mode,
1692 indirect, drawcount,
1693 maxdrawcount, stride))
1694 return;
1695
1696 vbo_validated_multidrawarraysindirectcount(ctx, mode,
1697 indirect, drawcount,
1698 maxdrawcount, stride);
1699 }
1700
1701 static void GLAPIENTRY
1702 vbo_exec_MultiDrawElementsIndirectCount(GLenum mode, GLenum type,
1703 GLintptr indirect,
1704 GLintptr drawcount,
1705 GLsizei maxdrawcount, GLsizei stride)
1706 {
1707 GET_CURRENT_CONTEXT(ctx);
1708
1709 if (MESA_VERBOSE & VERBOSE_DRAW)
1710 _mesa_debug(ctx, "glMultiDrawElementsIndirectCountARB"
1711 "(%s, %s, %lx, %lx, %i, %i)\n",
1712 _mesa_enum_to_string(mode),
1713 _mesa_enum_to_string(type), indirect,
1714 drawcount, maxdrawcount, stride);
1715
1716 /* If <stride> is zero, the array elements are treated as tightly packed. */
1717 if (stride == 0)
1718 stride = 5 * sizeof(GLuint); /* sizeof(DrawElementsIndirectCommand) */
1719
1720 if (!_mesa_validate_MultiDrawElementsIndirectCount(ctx, mode, type,
1721 indirect, drawcount,
1722 maxdrawcount, stride))
1723 return;
1724
1725 vbo_validated_multidrawelementsindirectcount(ctx, mode, type,
1726 indirect, drawcount,
1727 maxdrawcount, stride);
1728 }
1729
1730
1731 /**
1732 * Initialize the dispatch table with the VBO functions for drawing.
1733 */
1734 void
1735 vbo_initialize_exec_dispatch(const struct gl_context *ctx,
1736 struct _glapi_table *exec)
1737 {
1738 SET_DrawArrays(exec, vbo_exec_DrawArrays);
1739 SET_DrawElements(exec, vbo_exec_DrawElements);
1740
1741 if (_mesa_is_desktop_gl(ctx) || _mesa_is_gles3(ctx)) {
1742 SET_DrawRangeElements(exec, vbo_exec_DrawRangeElements);
1743 }
1744
1745 SET_MultiDrawElementsEXT(exec, vbo_exec_MultiDrawElements);
1746
1747 if (ctx->API == API_OPENGL_COMPAT) {
1748 SET_Rectf(exec, vbo_exec_Rectf);
1749 SET_EvalMesh1(exec, vbo_exec_EvalMesh1);
1750 SET_EvalMesh2(exec, vbo_exec_EvalMesh2);
1751 }
1752
1753 if (ctx->API != API_OPENGLES &&
1754 ctx->Extensions.ARB_draw_elements_base_vertex) {
1755 SET_DrawElementsBaseVertex(exec, vbo_exec_DrawElementsBaseVertex);
1756 SET_MultiDrawElementsBaseVertex(exec, vbo_exec_MultiDrawElementsBaseVertex);
1757
1758 if (_mesa_is_desktop_gl(ctx) || _mesa_is_gles3(ctx)) {
1759 SET_DrawRangeElementsBaseVertex(exec, vbo_exec_DrawRangeElementsBaseVertex);
1760 SET_DrawElementsInstancedBaseVertex(exec, vbo_exec_DrawElementsInstancedBaseVertex);
1761 }
1762 }
1763
1764 if (_mesa_is_desktop_gl(ctx) || _mesa_is_gles3(ctx)) {
1765 SET_DrawArraysInstancedBaseInstance(exec, vbo_exec_DrawArraysInstancedBaseInstance);
1766 SET_DrawElementsInstancedBaseInstance(exec, vbo_exec_DrawElementsInstancedBaseInstance);
1767 SET_DrawElementsInstancedBaseVertexBaseInstance(exec, vbo_exec_DrawElementsInstancedBaseVertexBaseInstance);
1768 }
1769
1770 if (ctx->API == API_OPENGL_CORE || _mesa_is_gles31(ctx)) {
1771 SET_DrawArraysIndirect(exec, vbo_exec_DrawArraysIndirect);
1772 SET_DrawElementsIndirect(exec, vbo_exec_DrawElementsIndirect);
1773 }
1774
1775 if (ctx->API == API_OPENGL_CORE) {
1776 SET_MultiDrawArraysIndirect(exec, vbo_exec_MultiDrawArraysIndirect);
1777 SET_MultiDrawElementsIndirect(exec, vbo_exec_MultiDrawElementsIndirect);
1778 SET_MultiDrawArraysIndirectCountARB(exec, vbo_exec_MultiDrawArraysIndirectCount);
1779 SET_MultiDrawElementsIndirectCountARB(exec, vbo_exec_MultiDrawElementsIndirectCount);
1780 }
1781
1782 if (_mesa_is_desktop_gl(ctx) || _mesa_is_gles3(ctx)) {
1783 SET_DrawArraysInstancedARB(exec, vbo_exec_DrawArraysInstanced);
1784 SET_DrawElementsInstancedARB(exec, vbo_exec_DrawElementsInstanced);
1785 }
1786
1787 if (_mesa_is_desktop_gl(ctx)) {
1788 SET_DrawTransformFeedback(exec, vbo_exec_DrawTransformFeedback);
1789 SET_DrawTransformFeedbackStream(exec, vbo_exec_DrawTransformFeedbackStream);
1790 SET_DrawTransformFeedbackInstanced(exec, vbo_exec_DrawTransformFeedbackInstanced);
1791 SET_DrawTransformFeedbackStreamInstanced(exec, vbo_exec_DrawTransformFeedbackStreamInstanced);
1792 }
1793 }
1794
1795
1796
1797 /**
1798 * The following functions are only used for OpenGL ES 1/2 support.
1799 * And some aren't even supported (yet) in ES 1/2.
1800 */
1801
1802
1803 void GLAPIENTRY
1804 _mesa_DrawArrays(GLenum mode, GLint first, GLsizei count)
1805 {
1806 vbo_exec_DrawArrays(mode, first, count);
1807 }
1808
1809 void GLAPIENTRY
1810 _mesa_DrawArraysInstanced(GLenum mode, GLint first, GLsizei count,
1811 GLsizei primcount)
1812 {
1813 vbo_exec_DrawArraysInstanced(mode, first, count, primcount);
1814 }
1815
1816 void GLAPIENTRY
1817 _mesa_DrawElements(GLenum mode, GLsizei count, GLenum type,
1818 const GLvoid *indices)
1819 {
1820 vbo_exec_DrawElements(mode, count, type, indices);
1821 }
1822
1823
1824 void GLAPIENTRY
1825 _mesa_DrawElementsBaseVertex(GLenum mode, GLsizei count, GLenum type,
1826 const GLvoid *indices, GLint basevertex)
1827 {
1828 vbo_exec_DrawElementsBaseVertex(mode, count, type, indices, basevertex);
1829 }
1830
1831
1832 void GLAPIENTRY
1833 _mesa_DrawRangeElements(GLenum mode, GLuint start, GLuint end, GLsizei count,
1834 GLenum type, const GLvoid *indices)
1835 {
1836 vbo_exec_DrawRangeElements(mode, start, end, count, type, indices);
1837 }
1838
1839
1840 void GLAPIENTRY
1841 _mesa_DrawRangeElementsBaseVertex(GLenum mode, GLuint start, GLuint end,
1842 GLsizei count, GLenum type,
1843 const GLvoid *indices, GLint basevertex)
1844 {
1845 vbo_exec_DrawRangeElementsBaseVertex(mode, start, end, count, type,
1846 indices, basevertex);
1847 }
1848
1849
1850 void GLAPIENTRY
1851 _mesa_MultiDrawElementsEXT(GLenum mode, const GLsizei *count, GLenum type,
1852 const GLvoid **indices, GLsizei primcount)
1853 {
1854 vbo_exec_MultiDrawElements(mode, count, type, indices, primcount);
1855 }
1856
1857
1858 void GLAPIENTRY
1859 _mesa_MultiDrawElementsBaseVertex(GLenum mode,
1860 const GLsizei *count, GLenum type,
1861 const GLvoid **indices, GLsizei primcount,
1862 const GLint *basevertex)
1863 {
1864 vbo_exec_MultiDrawElementsBaseVertex(mode, count, type, indices,
1865 primcount, basevertex);
1866 }
1867
1868 void GLAPIENTRY
1869 _mesa_DrawTransformFeedback(GLenum mode, GLuint name)
1870 {
1871 vbo_exec_DrawTransformFeedback(mode, name);
1872 }