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