vbo: Make #if 0'd debugging code compile.
[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 void
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
400 /**
401 * Helper function called by the other DrawArrays() functions below.
402 * This is where we handle primitive restart for drawing non-indexed
403 * arrays. If primitive restart is enabled, it typically means
404 * splitting one DrawArrays() into two.
405 */
406 static void
407 vbo_draw_arrays(struct gl_context *ctx, GLenum mode, GLint start,
408 GLsizei count, GLuint numInstances, GLuint baseInstance)
409 {
410 struct vbo_context *vbo = vbo_context(ctx);
411 struct _mesa_prim prim[2];
412
413 vbo_bind_arrays(ctx);
414
415 /* init most fields to zero */
416 memset(prim, 0, sizeof(prim));
417 prim[0].begin = 1;
418 prim[0].end = 1;
419 prim[0].mode = mode;
420 prim[0].num_instances = numInstances;
421 prim[0].base_instance = baseInstance;
422 prim[0].is_indirect = 0;
423
424 /* Implement the primitive restart index */
425 if (ctx->Array.PrimitiveRestart && !ctx->Array.PrimitiveRestartFixedIndex &&
426 ctx->Array.RestartIndex < count) {
427 GLuint primCount = 0;
428
429 if (ctx->Array.RestartIndex == start) {
430 /* special case: RestartIndex at beginning */
431 if (count > 1) {
432 prim[0].start = start + 1;
433 prim[0].count = count - 1;
434 primCount = 1;
435 }
436 }
437 else if (ctx->Array.RestartIndex == start + count - 1) {
438 /* special case: RestartIndex at end */
439 if (count > 1) {
440 prim[0].start = start;
441 prim[0].count = count - 1;
442 primCount = 1;
443 }
444 }
445 else {
446 /* general case: RestartIndex in middle, split into two prims */
447 prim[0].start = start;
448 prim[0].count = ctx->Array.RestartIndex - start;
449
450 prim[1] = prim[0];
451 prim[1].start = ctx->Array.RestartIndex + 1;
452 prim[1].count = count - prim[1].start;
453
454 primCount = 2;
455 }
456
457 if (primCount > 0) {
458 /* draw one or two prims */
459 vbo->draw_prims(ctx, prim, primCount, NULL,
460 GL_TRUE, start, start + count - 1, NULL, 0, NULL);
461 }
462 }
463 else {
464 /* no prim restart */
465 prim[0].start = start;
466 prim[0].count = count;
467
468 vbo->draw_prims(ctx, prim, 1, NULL,
469 GL_TRUE, start, start + count - 1,
470 NULL, 0, NULL);
471 }
472
473 if (MESA_DEBUG_FLAGS & DEBUG_ALWAYS_FLUSH) {
474 _mesa_flush(ctx);
475 }
476 }
477
478
479 /**
480 * Execute a glRectf() function.
481 */
482 static void GLAPIENTRY
483 vbo_exec_Rectf(GLfloat x1, GLfloat y1, GLfloat x2, GLfloat y2)
484 {
485 GET_CURRENT_CONTEXT(ctx);
486 ASSERT_OUTSIDE_BEGIN_END(ctx);
487
488 CALL_Begin(GET_DISPATCH(), (GL_QUADS));
489 CALL_Vertex2f(GET_DISPATCH(), (x1, y1));
490 CALL_Vertex2f(GET_DISPATCH(), (x2, y1));
491 CALL_Vertex2f(GET_DISPATCH(), (x2, y2));
492 CALL_Vertex2f(GET_DISPATCH(), (x1, y2));
493 CALL_End(GET_DISPATCH(), ());
494 }
495
496
497 static void GLAPIENTRY
498 vbo_exec_EvalMesh1(GLenum mode, GLint i1, GLint i2)
499 {
500 GET_CURRENT_CONTEXT(ctx);
501 GLint i;
502 GLfloat u, du;
503 GLenum prim;
504
505 switch (mode) {
506 case GL_POINT:
507 prim = GL_POINTS;
508 break;
509 case GL_LINE:
510 prim = GL_LINE_STRIP;
511 break;
512 default:
513 _mesa_error( ctx, GL_INVALID_ENUM, "glEvalMesh1(mode)" );
514 return;
515 }
516
517 /* No effect if vertex maps disabled.
518 */
519 if (!ctx->Eval.Map1Vertex4 &&
520 !ctx->Eval.Map1Vertex3)
521 return;
522
523 du = ctx->Eval.MapGrid1du;
524 u = ctx->Eval.MapGrid1u1 + i1 * du;
525
526 CALL_Begin(GET_DISPATCH(), (prim));
527 for (i=i1;i<=i2;i++,u+=du) {
528 CALL_EvalCoord1f(GET_DISPATCH(), (u));
529 }
530 CALL_End(GET_DISPATCH(), ());
531 }
532
533
534 static void GLAPIENTRY
535 vbo_exec_EvalMesh2(GLenum mode, GLint i1, GLint i2, GLint j1, GLint j2)
536 {
537 GET_CURRENT_CONTEXT(ctx);
538 GLfloat u, du, v, dv, v1, u1;
539 GLint i, j;
540
541 switch (mode) {
542 case GL_POINT:
543 case GL_LINE:
544 case GL_FILL:
545 break;
546 default:
547 _mesa_error( ctx, GL_INVALID_ENUM, "glEvalMesh2(mode)" );
548 return;
549 }
550
551 /* No effect if vertex maps disabled.
552 */
553 if (!ctx->Eval.Map2Vertex4 &&
554 !ctx->Eval.Map2Vertex3)
555 return;
556
557 du = ctx->Eval.MapGrid2du;
558 dv = ctx->Eval.MapGrid2dv;
559 v1 = ctx->Eval.MapGrid2v1 + j1 * dv;
560 u1 = ctx->Eval.MapGrid2u1 + i1 * du;
561
562 switch (mode) {
563 case GL_POINT:
564 CALL_Begin(GET_DISPATCH(), (GL_POINTS));
565 for (v=v1,j=j1;j<=j2;j++,v+=dv) {
566 for (u=u1,i=i1;i<=i2;i++,u+=du) {
567 CALL_EvalCoord2f(GET_DISPATCH(), (u, v));
568 }
569 }
570 CALL_End(GET_DISPATCH(), ());
571 break;
572 case GL_LINE:
573 for (v=v1,j=j1;j<=j2;j++,v+=dv) {
574 CALL_Begin(GET_DISPATCH(), (GL_LINE_STRIP));
575 for (u=u1,i=i1;i<=i2;i++,u+=du) {
576 CALL_EvalCoord2f(GET_DISPATCH(), (u, v));
577 }
578 CALL_End(GET_DISPATCH(), ());
579 }
580 for (u=u1,i=i1;i<=i2;i++,u+=du) {
581 CALL_Begin(GET_DISPATCH(), (GL_LINE_STRIP));
582 for (v=v1,j=j1;j<=j2;j++,v+=dv) {
583 CALL_EvalCoord2f(GET_DISPATCH(), (u, v));
584 }
585 CALL_End(GET_DISPATCH(), ());
586 }
587 break;
588 case GL_FILL:
589 for (v=v1,j=j1;j<j2;j++,v+=dv) {
590 CALL_Begin(GET_DISPATCH(), (GL_TRIANGLE_STRIP));
591 for (u=u1,i=i1;i<=i2;i++,u+=du) {
592 CALL_EvalCoord2f(GET_DISPATCH(), (u, v));
593 CALL_EvalCoord2f(GET_DISPATCH(), (u, v+dv));
594 }
595 CALL_End(GET_DISPATCH(), ());
596 }
597 break;
598 }
599 }
600
601
602 /**
603 * Called from glDrawArrays when in immediate mode (not display list mode).
604 */
605 static void GLAPIENTRY
606 vbo_exec_DrawArrays(GLenum mode, GLint start, GLsizei count)
607 {
608 GET_CURRENT_CONTEXT(ctx);
609
610 if (MESA_VERBOSE & VERBOSE_DRAW)
611 _mesa_debug(ctx, "glDrawArrays(%s, %d, %d)\n",
612 _mesa_enum_to_string(mode), start, count);
613
614 if (!_mesa_validate_DrawArrays(ctx, mode, count))
615 return;
616
617 if (0)
618 check_draw_arrays_data(ctx, start, count);
619
620 vbo_draw_arrays(ctx, mode, start, count, 1, 0);
621
622 if (0)
623 print_draw_arrays(ctx, mode, start, count);
624 }
625
626
627 /**
628 * Called from glDrawArraysInstanced when in immediate mode (not
629 * display list mode).
630 */
631 static void GLAPIENTRY
632 vbo_exec_DrawArraysInstanced(GLenum mode, GLint start, GLsizei count,
633 GLsizei numInstances)
634 {
635 GET_CURRENT_CONTEXT(ctx);
636
637 if (MESA_VERBOSE & VERBOSE_DRAW)
638 _mesa_debug(ctx, "glDrawArraysInstanced(%s, %d, %d, %d)\n",
639 _mesa_enum_to_string(mode), start, count, numInstances);
640
641 if (!_mesa_validate_DrawArraysInstanced(ctx, mode, start, count, numInstances))
642 return;
643
644 if (0)
645 check_draw_arrays_data(ctx, start, count);
646
647 vbo_draw_arrays(ctx, mode, start, count, numInstances, 0);
648
649 if (0)
650 print_draw_arrays(ctx, mode, start, count);
651 }
652
653
654 /**
655 * Called from glDrawArraysInstancedBaseInstance when in immediate mode.
656 */
657 static void GLAPIENTRY
658 vbo_exec_DrawArraysInstancedBaseInstance(GLenum mode, GLint first, GLsizei count,
659 GLsizei numInstances, GLuint baseInstance)
660 {
661 GET_CURRENT_CONTEXT(ctx);
662
663 if (MESA_VERBOSE & VERBOSE_DRAW)
664 _mesa_debug(ctx, "glDrawArraysInstancedBaseInstance(%s, %d, %d, %d, %d)\n",
665 _mesa_enum_to_string(mode), first, count,
666 numInstances, baseInstance);
667
668 if (!_mesa_validate_DrawArraysInstanced(ctx, mode, first, count,
669 numInstances))
670 return;
671
672 if (0)
673 check_draw_arrays_data(ctx, first, count);
674
675 vbo_draw_arrays(ctx, mode, first, count, numInstances, baseInstance);
676
677 if (0)
678 print_draw_arrays(ctx, mode, first, count);
679 }
680
681
682
683 /**
684 * Map GL_ELEMENT_ARRAY_BUFFER and print contents.
685 * For debugging.
686 */
687 #if 0
688 static void
689 dump_element_buffer(struct gl_context *ctx, GLenum type)
690 {
691 const GLvoid *map =
692 ctx->Driver.MapBufferRange(ctx, 0,
693 ctx->Array.VAO->IndexBufferObj->Size,
694 GL_MAP_READ_BIT,
695 ctx->Array.VAO->IndexBufferObj,
696 MAP_INTERNAL);
697 switch (type) {
698 case GL_UNSIGNED_BYTE:
699 {
700 const GLubyte *us = (const GLubyte *) map;
701 GLint i;
702 for (i = 0; i < ctx->Array.VAO->IndexBufferObj->Size; i++) {
703 printf("%02x ", us[i]);
704 if (i % 32 == 31)
705 printf("\n");
706 }
707 printf("\n");
708 }
709 break;
710 case GL_UNSIGNED_SHORT:
711 {
712 const GLushort *us = (const GLushort *) map;
713 GLint i;
714 for (i = 0; i < ctx->Array.VAO->IndexBufferObj->Size / 2; i++) {
715 printf("%04x ", us[i]);
716 if (i % 16 == 15)
717 printf("\n");
718 }
719 printf("\n");
720 }
721 break;
722 case GL_UNSIGNED_INT:
723 {
724 const GLuint *us = (const GLuint *) map;
725 GLint i;
726 for (i = 0; i < ctx->Array.VAO->IndexBufferObj->Size / 4; i++) {
727 printf("%08x ", us[i]);
728 if (i % 8 == 7)
729 printf("\n");
730 }
731 printf("\n");
732 }
733 break;
734 default:
735 ;
736 }
737
738 ctx->Driver.UnmapBuffer(ctx, ctx->Array.VAO->IndexBufferObj,
739 MAP_INTERNAL);
740 }
741 #endif
742
743
744 /**
745 * Inner support for both _mesa_DrawElements and _mesa_DrawRangeElements.
746 * Do the rendering for a glDrawElements or glDrawRangeElements call after
747 * we've validated buffer bounds, etc.
748 */
749 static void
750 vbo_validated_drawrangeelements(struct gl_context *ctx, GLenum mode,
751 GLboolean index_bounds_valid,
752 GLuint start, GLuint end,
753 GLsizei count, GLenum type,
754 const GLvoid *indices,
755 GLint basevertex, GLuint numInstances,
756 GLuint baseInstance)
757 {
758 struct vbo_context *vbo = vbo_context(ctx);
759 struct _mesa_index_buffer ib;
760 struct _mesa_prim prim[1];
761
762 vbo_bind_arrays(ctx);
763
764 ib.count = count;
765 ib.type = type;
766 ib.obj = ctx->Array.VAO->IndexBufferObj;
767 ib.ptr = indices;
768
769 prim[0].begin = 1;
770 prim[0].end = 1;
771 prim[0].weak = 0;
772 prim[0].pad = 0;
773 prim[0].mode = mode;
774 prim[0].start = 0;
775 prim[0].count = count;
776 prim[0].indexed = 1;
777 prim[0].is_indirect = 0;
778 prim[0].basevertex = basevertex;
779 prim[0].num_instances = numInstances;
780 prim[0].base_instance = baseInstance;
781
782 /* Need to give special consideration to rendering a range of
783 * indices starting somewhere above zero. Typically the
784 * application is issuing multiple DrawRangeElements() to draw
785 * successive primitives layed out linearly in the vertex arrays.
786 * Unless the vertex arrays are all in a VBO (or locked as with
787 * CVA), the OpenGL semantics imply that we need to re-read or
788 * re-upload the vertex data on each draw call.
789 *
790 * In the case of hardware tnl, we want to avoid starting the
791 * upload at zero, as it will mean every draw call uploads an
792 * increasing amount of not-used vertex data. Worse - in the
793 * software tnl module, all those vertices might be transformed and
794 * lit but never rendered.
795 *
796 * If we just upload or transform the vertices in start..end,
797 * however, the indices will be incorrect.
798 *
799 * At this level, we don't know exactly what the requirements of
800 * the backend are going to be, though it will likely boil down to
801 * either:
802 *
803 * 1) Do nothing, everything is in a VBO and is processed once
804 * only.
805 *
806 * 2) Adjust the indices and vertex arrays so that start becomes
807 * zero.
808 *
809 * Rather than doing anything here, I'll provide a helper function
810 * for the latter case elsewhere.
811 */
812
813 vbo->draw_prims(ctx, prim, 1, &ib,
814 index_bounds_valid, start, end, NULL, 0, NULL);
815
816 if (MESA_DEBUG_FLAGS & DEBUG_ALWAYS_FLUSH) {
817 _mesa_flush(ctx);
818 }
819 }
820
821
822 /**
823 * Called by glDrawRangeElementsBaseVertex() in immediate mode.
824 */
825 static void GLAPIENTRY
826 vbo_exec_DrawRangeElementsBaseVertex(GLenum mode,
827 GLuint start, GLuint end,
828 GLsizei count, GLenum type,
829 const GLvoid *indices,
830 GLint basevertex)
831 {
832 static GLuint warnCount = 0;
833 GLboolean index_bounds_valid = GL_TRUE;
834
835 /* This is only useful to catch invalid values in the "end" parameter
836 * like ~0.
837 */
838 GLuint max_element = 2 * 1000 * 1000 * 1000; /* just a big number */
839
840 GET_CURRENT_CONTEXT(ctx);
841
842 if (MESA_VERBOSE & VERBOSE_DRAW)
843 _mesa_debug(ctx,
844 "glDrawRangeElementsBaseVertex(%s, %u, %u, %d, %s, %p, %d)\n",
845 _mesa_enum_to_string(mode), start, end, count,
846 _mesa_enum_to_string(type), indices, basevertex);
847
848 if (!_mesa_validate_DrawRangeElements(ctx, mode, start, end, count,
849 type, indices))
850 return;
851
852 if ((int) end + basevertex < 0 ||
853 start + basevertex >= max_element) {
854 /* The application requested we draw using a range of indices that's
855 * outside the bounds of the current VBO. This is invalid and appears
856 * to give undefined results. The safest thing to do is to simply
857 * ignore the range, in case the application botched their range tracking
858 * but did provide valid indices. Also issue a warning indicating that
859 * the application is broken.
860 */
861 if (warnCount++ < 10) {
862 _mesa_warning(ctx, "glDrawRangeElements(start %u, end %u, "
863 "basevertex %d, count %d, type 0x%x, indices=%p):\n"
864 "\trange is outside VBO bounds (max=%u); ignoring.\n"
865 "\tThis should be fixed in the application.",
866 start, end, basevertex, count, type, indices,
867 max_element - 1);
868 }
869 index_bounds_valid = GL_FALSE;
870 }
871
872 /* NOTE: It's important that 'end' is a reasonable value.
873 * in _tnl_draw_prims(), we use end to determine how many vertices
874 * to transform. If it's too large, we can unnecessarily split prims
875 * or we can read/write out of memory in several different places!
876 */
877
878 /* Catch/fix some potential user errors */
879 if (type == GL_UNSIGNED_BYTE) {
880 start = MIN2(start, 0xff);
881 end = MIN2(end, 0xff);
882 }
883 else if (type == GL_UNSIGNED_SHORT) {
884 start = MIN2(start, 0xffff);
885 end = MIN2(end, 0xffff);
886 }
887
888 if (0) {
889 printf("glDraw[Range]Elements{,BaseVertex}"
890 "(start %u, end %u, type 0x%x, count %d) ElemBuf %u, "
891 "base %d\n",
892 start, end, type, count,
893 ctx->Array.VAO->IndexBufferObj->Name,
894 basevertex);
895 }
896
897 if ((int) start + basevertex < 0 ||
898 end + basevertex >= max_element)
899 index_bounds_valid = GL_FALSE;
900
901 #if 0
902 check_draw_elements_data(ctx, count, type, indices, basevertex);
903 #else
904 (void) check_draw_elements_data;
905 #endif
906
907 vbo_validated_drawrangeelements(ctx, mode, index_bounds_valid, start, end,
908 count, type, indices, basevertex, 1, 0);
909 }
910
911
912 /**
913 * Called by glDrawRangeElements() in immediate mode.
914 */
915 static void GLAPIENTRY
916 vbo_exec_DrawRangeElements(GLenum mode, GLuint start, GLuint end,
917 GLsizei count, GLenum type, const GLvoid *indices)
918 {
919 if (MESA_VERBOSE & VERBOSE_DRAW) {
920 GET_CURRENT_CONTEXT(ctx);
921 _mesa_debug(ctx,
922 "glDrawRangeElements(%s, %u, %u, %d, %s, %p)\n",
923 _mesa_enum_to_string(mode), start, end, count,
924 _mesa_enum_to_string(type), indices);
925 }
926
927 vbo_exec_DrawRangeElementsBaseVertex(mode, start, end, count, type,
928 indices, 0);
929 }
930
931
932 /**
933 * Called by glDrawElements() in immediate mode.
934 */
935 static void GLAPIENTRY
936 vbo_exec_DrawElements(GLenum mode, GLsizei count, GLenum type,
937 const GLvoid *indices)
938 {
939 GET_CURRENT_CONTEXT(ctx);
940
941 if (MESA_VERBOSE & VERBOSE_DRAW)
942 _mesa_debug(ctx, "glDrawElements(%s, %u, %s, %p)\n",
943 _mesa_enum_to_string(mode), count,
944 _mesa_enum_to_string(type), indices);
945
946 if (!_mesa_validate_DrawElements(ctx, mode, count, type, indices))
947 return;
948
949 vbo_validated_drawrangeelements(ctx, mode, GL_FALSE, ~0, ~0,
950 count, type, indices, 0, 1, 0);
951 }
952
953
954 /**
955 * Called by glDrawElementsBaseVertex() in immediate mode.
956 */
957 static void GLAPIENTRY
958 vbo_exec_DrawElementsBaseVertex(GLenum mode, GLsizei count, GLenum type,
959 const GLvoid *indices, GLint basevertex)
960 {
961 GET_CURRENT_CONTEXT(ctx);
962
963 if (MESA_VERBOSE & VERBOSE_DRAW)
964 _mesa_debug(ctx, "glDrawElementsBaseVertex(%s, %d, %s, %p, %d)\n",
965 _mesa_enum_to_string(mode), count,
966 _mesa_enum_to_string(type), indices, basevertex);
967
968 if (!_mesa_validate_DrawElements(ctx, mode, count, type, indices))
969 return;
970
971 vbo_validated_drawrangeelements(ctx, mode, GL_FALSE, ~0, ~0,
972 count, type, indices, basevertex, 1, 0);
973 }
974
975
976 /**
977 * Called by glDrawElementsInstanced() in immediate mode.
978 */
979 static void GLAPIENTRY
980 vbo_exec_DrawElementsInstanced(GLenum mode, GLsizei count, GLenum type,
981 const GLvoid *indices, GLsizei numInstances)
982 {
983 GET_CURRENT_CONTEXT(ctx);
984
985 if (MESA_VERBOSE & VERBOSE_DRAW)
986 _mesa_debug(ctx, "glDrawElementsInstanced(%s, %d, %s, %p, %d)\n",
987 _mesa_enum_to_string(mode), count,
988 _mesa_enum_to_string(type), indices, numInstances);
989
990 if (!_mesa_validate_DrawElementsInstanced(ctx, mode, count, type, indices,
991 numInstances))
992 return;
993
994 vbo_validated_drawrangeelements(ctx, mode, GL_FALSE, ~0, ~0,
995 count, type, indices, 0, numInstances, 0);
996 }
997
998
999 /**
1000 * Called by glDrawElementsInstancedBaseVertex() in immediate mode.
1001 */
1002 static void GLAPIENTRY
1003 vbo_exec_DrawElementsInstancedBaseVertex(GLenum mode, GLsizei count, GLenum type,
1004 const GLvoid *indices, GLsizei numInstances,
1005 GLint basevertex)
1006 {
1007 GET_CURRENT_CONTEXT(ctx);
1008
1009 if (MESA_VERBOSE & VERBOSE_DRAW)
1010 _mesa_debug(ctx, "glDrawElementsInstancedBaseVertex(%s, %d, %s, %p, %d; %d)\n",
1011 _mesa_enum_to_string(mode), count,
1012 _mesa_enum_to_string(type), indices,
1013 numInstances, basevertex);
1014
1015 if (!_mesa_validate_DrawElementsInstanced(ctx, mode, count, type, indices,
1016 numInstances))
1017 return;
1018
1019 vbo_validated_drawrangeelements(ctx, mode, GL_FALSE, ~0, ~0,
1020 count, type, indices, basevertex, numInstances, 0);
1021 }
1022
1023
1024 /**
1025 * Called by glDrawElementsInstancedBaseInstance() in immediate mode.
1026 */
1027 static void GLAPIENTRY
1028 vbo_exec_DrawElementsInstancedBaseInstance(GLenum mode, GLsizei count, GLenum type,
1029 const GLvoid *indices, GLsizei numInstances,
1030 GLuint baseInstance)
1031 {
1032 GET_CURRENT_CONTEXT(ctx);
1033
1034 if (MESA_VERBOSE & VERBOSE_DRAW)
1035 _mesa_debug(ctx, "glDrawElementsInstancedBaseInstance(%s, %d, %s, %p, %d, %d)\n",
1036 _mesa_enum_to_string(mode), count,
1037 _mesa_enum_to_string(type), indices,
1038 numInstances, baseInstance);
1039
1040 if (!_mesa_validate_DrawElementsInstanced(ctx, mode, count, type, indices,
1041 numInstances))
1042 return;
1043
1044 vbo_validated_drawrangeelements(ctx, mode, GL_FALSE, ~0, ~0,
1045 count, type, indices, 0, numInstances,
1046 baseInstance);
1047 }
1048
1049
1050 /**
1051 * Called by glDrawElementsInstancedBaseVertexBaseInstance() in immediate mode.
1052 */
1053 static void GLAPIENTRY
1054 vbo_exec_DrawElementsInstancedBaseVertexBaseInstance(GLenum mode, GLsizei count, GLenum type,
1055 const GLvoid *indices, GLsizei numInstances,
1056 GLint basevertex, GLuint baseInstance)
1057 {
1058 GET_CURRENT_CONTEXT(ctx);
1059
1060 if (MESA_VERBOSE & VERBOSE_DRAW)
1061 _mesa_debug(ctx, "glDrawElementsInstancedBaseVertexBaseInstance(%s, %d, %s, %p, %d, %d, %d)\n",
1062 _mesa_enum_to_string(mode), count,
1063 _mesa_enum_to_string(type), indices,
1064 numInstances, basevertex, baseInstance);
1065
1066 if (!_mesa_validate_DrawElementsInstanced(ctx, mode, count, type, indices,
1067 numInstances))
1068 return;
1069
1070 vbo_validated_drawrangeelements(ctx, mode, GL_FALSE, ~0, ~0,
1071 count, type, indices, basevertex, numInstances,
1072 baseInstance);
1073 }
1074
1075
1076 /**
1077 * Inner support for both _mesa_MultiDrawElements() and
1078 * _mesa_MultiDrawRangeElements().
1079 * This does the actual rendering after we've checked array indexes, etc.
1080 */
1081 static void
1082 vbo_validated_multidrawelements(struct gl_context *ctx, GLenum mode,
1083 const GLsizei *count, GLenum type,
1084 const GLvoid * const *indices,
1085 GLsizei primcount,
1086 const GLint *basevertex)
1087 {
1088 struct vbo_context *vbo = vbo_context(ctx);
1089 struct _mesa_index_buffer ib;
1090 struct _mesa_prim *prim;
1091 unsigned int index_type_size = vbo_sizeof_ib_type(type);
1092 uintptr_t min_index_ptr, max_index_ptr;
1093 GLboolean fallback = GL_FALSE;
1094 int i;
1095
1096 if (primcount == 0)
1097 return;
1098
1099 prim = calloc(primcount, sizeof(*prim));
1100 if (prim == NULL) {
1101 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glMultiDrawElements");
1102 return;
1103 }
1104
1105 vbo_bind_arrays(ctx);
1106
1107 min_index_ptr = (uintptr_t)indices[0];
1108 max_index_ptr = 0;
1109 for (i = 0; i < primcount; i++) {
1110 min_index_ptr = MIN2(min_index_ptr, (uintptr_t)indices[i]);
1111 max_index_ptr = MAX2(max_index_ptr, (uintptr_t)indices[i] +
1112 index_type_size * count[i]);
1113 }
1114
1115 /* Check if we can handle this thing as a bunch of index offsets from the
1116 * same index pointer. If we can't, then we have to fall back to doing
1117 * a draw_prims per primitive.
1118 * Check that the difference between each prim's indexes is a multiple of
1119 * the index/element size.
1120 */
1121 if (index_type_size != 1) {
1122 for (i = 0; i < primcount; i++) {
1123 if ((((uintptr_t)indices[i] - min_index_ptr) % index_type_size) != 0) {
1124 fallback = GL_TRUE;
1125 break;
1126 }
1127 }
1128 }
1129
1130 /* Draw primitives individually if one count is zero, so we can easily skip
1131 * that primitive.
1132 */
1133 for (i = 0; i < primcount; i++) {
1134 if (count[i] == 0) {
1135 fallback = GL_TRUE;
1136 break;
1137 }
1138 }
1139
1140 /* If the index buffer isn't in a VBO, then treating the application's
1141 * subranges of the index buffer as one large index buffer may lead to
1142 * us reading unmapped memory.
1143 */
1144 if (!_mesa_is_bufferobj(ctx->Array.VAO->IndexBufferObj))
1145 fallback = GL_TRUE;
1146
1147 if (!fallback) {
1148 ib.count = (max_index_ptr - min_index_ptr) / index_type_size;
1149 ib.type = type;
1150 ib.obj = ctx->Array.VAO->IndexBufferObj;
1151 ib.ptr = (void *)min_index_ptr;
1152
1153 for (i = 0; i < primcount; i++) {
1154 prim[i].begin = (i == 0);
1155 prim[i].end = (i == primcount - 1);
1156 prim[i].weak = 0;
1157 prim[i].pad = 0;
1158 prim[i].mode = mode;
1159 prim[i].start = ((uintptr_t)indices[i] - min_index_ptr) / index_type_size;
1160 prim[i].count = count[i];
1161 prim[i].indexed = 1;
1162 prim[i].num_instances = 1;
1163 prim[i].base_instance = 0;
1164 prim[i].draw_id = i;
1165 prim[i].is_indirect = 0;
1166 if (basevertex != NULL)
1167 prim[i].basevertex = basevertex[i];
1168 else
1169 prim[i].basevertex = 0;
1170 }
1171
1172 vbo->draw_prims(ctx, prim, primcount, &ib,
1173 false, ~0, ~0, NULL, 0, NULL);
1174 } else {
1175 /* render one prim at a time */
1176 for (i = 0; i < primcount; i++) {
1177 if (count[i] == 0)
1178 continue;
1179 ib.count = count[i];
1180 ib.type = type;
1181 ib.obj = ctx->Array.VAO->IndexBufferObj;
1182 ib.ptr = indices[i];
1183
1184 prim[0].begin = 1;
1185 prim[0].end = 1;
1186 prim[0].weak = 0;
1187 prim[0].pad = 0;
1188 prim[0].mode = mode;
1189 prim[0].start = 0;
1190 prim[0].count = count[i];
1191 prim[0].indexed = 1;
1192 prim[0].num_instances = 1;
1193 prim[0].base_instance = 0;
1194 prim[0].draw_id = i;
1195 prim[0].is_indirect = 0;
1196 if (basevertex != NULL)
1197 prim[0].basevertex = basevertex[i];
1198 else
1199 prim[0].basevertex = 0;
1200
1201 vbo->draw_prims(ctx, prim, 1, &ib,
1202 false, ~0, ~0, NULL, 0, NULL);
1203 }
1204 }
1205
1206 free(prim);
1207
1208 if (MESA_DEBUG_FLAGS & DEBUG_ALWAYS_FLUSH) {
1209 _mesa_flush(ctx);
1210 }
1211 }
1212
1213
1214 static void GLAPIENTRY
1215 vbo_exec_MultiDrawElements(GLenum mode,
1216 const GLsizei *count, GLenum type,
1217 const GLvoid * const *indices,
1218 GLsizei primcount)
1219 {
1220 GET_CURRENT_CONTEXT(ctx);
1221
1222 if (!_mesa_validate_MultiDrawElements(ctx, mode, count, type, indices,
1223 primcount))
1224 return;
1225
1226 vbo_validated_multidrawelements(ctx, mode, count, type, indices, primcount,
1227 NULL);
1228 }
1229
1230
1231 static void GLAPIENTRY
1232 vbo_exec_MultiDrawElementsBaseVertex(GLenum mode,
1233 const GLsizei *count, GLenum type,
1234 const GLvoid * const *indices,
1235 GLsizei primcount,
1236 const GLsizei *basevertex)
1237 {
1238 GET_CURRENT_CONTEXT(ctx);
1239
1240 if (!_mesa_validate_MultiDrawElements(ctx, mode, count, type, indices,
1241 primcount))
1242 return;
1243
1244 vbo_validated_multidrawelements(ctx, mode, count, type, indices, primcount,
1245 basevertex);
1246 }
1247
1248 static void
1249 vbo_draw_transform_feedback(struct gl_context *ctx, GLenum mode,
1250 struct gl_transform_feedback_object *obj,
1251 GLuint stream, GLuint numInstances)
1252 {
1253 struct vbo_context *vbo = vbo_context(ctx);
1254 struct _mesa_prim prim[2];
1255
1256 if (!_mesa_validate_DrawTransformFeedback(ctx, mode, obj, stream,
1257 numInstances)) {
1258 return;
1259 }
1260
1261 if (ctx->Driver.GetTransformFeedbackVertexCount &&
1262 (ctx->Const.AlwaysUseGetTransformFeedbackVertexCount ||
1263 !_mesa_all_varyings_in_vbos(ctx->Array.VAO))) {
1264 GLsizei n = ctx->Driver.GetTransformFeedbackVertexCount(ctx, obj, stream);
1265 vbo_draw_arrays(ctx, mode, 0, n, numInstances, 0);
1266 return;
1267 }
1268
1269 vbo_bind_arrays(ctx);
1270
1271 /* init most fields to zero */
1272 memset(prim, 0, sizeof(prim));
1273 prim[0].begin = 1;
1274 prim[0].end = 1;
1275 prim[0].mode = mode;
1276 prim[0].num_instances = numInstances;
1277 prim[0].base_instance = 0;
1278 prim[0].is_indirect = 0;
1279
1280 /* Maybe we should do some primitive splitting for primitive restart
1281 * (like in DrawArrays), but we have no way to know how many vertices
1282 * will be rendered. */
1283
1284 vbo->draw_prims(ctx, prim, 1, NULL,
1285 GL_FALSE, ~0, ~0, obj, stream, NULL);
1286
1287 if (MESA_DEBUG_FLAGS & DEBUG_ALWAYS_FLUSH) {
1288 _mesa_flush(ctx);
1289 }
1290 }
1291
1292 /**
1293 * Like DrawArrays, but take the count from a transform feedback object.
1294 * \param mode GL_POINTS, GL_LINES, GL_TRIANGLE_STRIP, etc.
1295 * \param name the transform feedback object
1296 * User still has to setup of the vertex attribute info with
1297 * glVertexPointer, glColorPointer, etc.
1298 * Part of GL_ARB_transform_feedback2.
1299 */
1300 static void GLAPIENTRY
1301 vbo_exec_DrawTransformFeedback(GLenum mode, GLuint name)
1302 {
1303 GET_CURRENT_CONTEXT(ctx);
1304 struct gl_transform_feedback_object *obj =
1305 _mesa_lookup_transform_feedback_object(ctx, name);
1306
1307 if (MESA_VERBOSE & VERBOSE_DRAW)
1308 _mesa_debug(ctx, "glDrawTransformFeedback(%s, %d)\n",
1309 _mesa_enum_to_string(mode), name);
1310
1311 vbo_draw_transform_feedback(ctx, mode, obj, 0, 1);
1312 }
1313
1314 static void GLAPIENTRY
1315 vbo_exec_DrawTransformFeedbackStream(GLenum mode, GLuint name, GLuint stream)
1316 {
1317 GET_CURRENT_CONTEXT(ctx);
1318 struct gl_transform_feedback_object *obj =
1319 _mesa_lookup_transform_feedback_object(ctx, name);
1320
1321 if (MESA_VERBOSE & VERBOSE_DRAW)
1322 _mesa_debug(ctx, "glDrawTransformFeedbackStream(%s, %u, %u)\n",
1323 _mesa_enum_to_string(mode), name, stream);
1324
1325 vbo_draw_transform_feedback(ctx, mode, obj, stream, 1);
1326 }
1327
1328 static void GLAPIENTRY
1329 vbo_exec_DrawTransformFeedbackInstanced(GLenum mode, GLuint name,
1330 GLsizei primcount)
1331 {
1332 GET_CURRENT_CONTEXT(ctx);
1333 struct gl_transform_feedback_object *obj =
1334 _mesa_lookup_transform_feedback_object(ctx, name);
1335
1336 if (MESA_VERBOSE & VERBOSE_DRAW)
1337 _mesa_debug(ctx, "glDrawTransformFeedbackInstanced(%s, %d)\n",
1338 _mesa_enum_to_string(mode), name);
1339
1340 vbo_draw_transform_feedback(ctx, mode, obj, 0, primcount);
1341 }
1342
1343 static void GLAPIENTRY
1344 vbo_exec_DrawTransformFeedbackStreamInstanced(GLenum mode, GLuint name,
1345 GLuint stream, GLsizei primcount)
1346 {
1347 GET_CURRENT_CONTEXT(ctx);
1348 struct gl_transform_feedback_object *obj =
1349 _mesa_lookup_transform_feedback_object(ctx, name);
1350
1351 if (MESA_VERBOSE & VERBOSE_DRAW)
1352 _mesa_debug(ctx, "glDrawTransformFeedbackStreamInstanced"
1353 "(%s, %u, %u, %i)\n",
1354 _mesa_enum_to_string(mode), name, stream, primcount);
1355
1356 vbo_draw_transform_feedback(ctx, mode, obj, stream, primcount);
1357 }
1358
1359 static void
1360 vbo_validated_drawarraysindirect(struct gl_context *ctx,
1361 GLenum mode, const GLvoid *indirect)
1362 {
1363 struct vbo_context *vbo = vbo_context(ctx);
1364
1365 vbo_bind_arrays(ctx);
1366
1367 vbo->draw_indirect_prims(ctx, mode,
1368 ctx->DrawIndirectBuffer, (GLsizeiptr)indirect,
1369 1 /* draw_count */, 16 /* stride */,
1370 NULL, 0, NULL);
1371
1372 if (MESA_DEBUG_FLAGS & DEBUG_ALWAYS_FLUSH)
1373 _mesa_flush(ctx);
1374 }
1375
1376 static void
1377 vbo_validated_multidrawarraysindirect(struct gl_context *ctx,
1378 GLenum mode,
1379 const GLvoid *indirect,
1380 GLsizei primcount, GLsizei stride)
1381 {
1382 struct vbo_context *vbo = vbo_context(ctx);
1383 GLsizeiptr offset = (GLsizeiptr)indirect;
1384
1385 if (primcount == 0)
1386 return;
1387
1388 vbo_bind_arrays(ctx);
1389
1390 vbo->draw_indirect_prims(ctx, mode,
1391 ctx->DrawIndirectBuffer, offset,
1392 primcount, stride,
1393 NULL, 0, NULL);
1394
1395 if (MESA_DEBUG_FLAGS & DEBUG_ALWAYS_FLUSH)
1396 _mesa_flush(ctx);
1397 }
1398
1399 static void
1400 vbo_validated_drawelementsindirect(struct gl_context *ctx,
1401 GLenum mode, GLenum type,
1402 const GLvoid *indirect)
1403 {
1404 struct vbo_context *vbo = vbo_context(ctx);
1405 struct _mesa_index_buffer ib;
1406
1407 vbo_bind_arrays(ctx);
1408
1409 ib.count = 0; /* unknown */
1410 ib.type = type;
1411 ib.obj = ctx->Array.VAO->IndexBufferObj;
1412 ib.ptr = NULL;
1413
1414 vbo->draw_indirect_prims(ctx, mode,
1415 ctx->DrawIndirectBuffer, (GLsizeiptr)indirect,
1416 1 /* draw_count */, 20 /* stride */,
1417 NULL, 0,
1418 &ib);
1419
1420 if (MESA_DEBUG_FLAGS & DEBUG_ALWAYS_FLUSH)
1421 _mesa_flush(ctx);
1422 }
1423
1424 static void
1425 vbo_validated_multidrawelementsindirect(struct gl_context *ctx,
1426 GLenum mode, GLenum type,
1427 const GLvoid *indirect,
1428 GLsizei primcount, GLsizei stride)
1429 {
1430 struct vbo_context *vbo = vbo_context(ctx);
1431 struct _mesa_index_buffer ib;
1432 GLsizeiptr offset = (GLsizeiptr)indirect;
1433
1434 if (primcount == 0)
1435 return;
1436
1437 vbo_bind_arrays(ctx);
1438
1439 /* NOTE: IndexBufferObj is guaranteed to be a VBO. */
1440
1441 ib.count = 0; /* unknown */
1442 ib.type = type;
1443 ib.obj = ctx->Array.VAO->IndexBufferObj;
1444 ib.ptr = NULL;
1445
1446 vbo->draw_indirect_prims(ctx, mode,
1447 ctx->DrawIndirectBuffer, offset,
1448 primcount, stride,
1449 NULL, 0,
1450 &ib);
1451
1452 if (MESA_DEBUG_FLAGS & DEBUG_ALWAYS_FLUSH)
1453 _mesa_flush(ctx);
1454 }
1455
1456 /**
1457 * Like [Multi]DrawArrays/Elements, but they take most arguments from
1458 * a buffer object.
1459 */
1460 static void GLAPIENTRY
1461 vbo_exec_DrawArraysIndirect(GLenum mode, const GLvoid *indirect)
1462 {
1463 GET_CURRENT_CONTEXT(ctx);
1464
1465 if (MESA_VERBOSE & VERBOSE_DRAW)
1466 _mesa_debug(ctx, "glDrawArraysIndirect(%s, %p)\n",
1467 _mesa_enum_to_string(mode), indirect);
1468
1469 if (!_mesa_validate_DrawArraysIndirect(ctx, mode, indirect))
1470 return;
1471
1472 vbo_validated_drawarraysindirect(ctx, mode, indirect);
1473 }
1474
1475 static void GLAPIENTRY
1476 vbo_exec_DrawElementsIndirect(GLenum mode, GLenum type,
1477 const GLvoid *indirect)
1478 {
1479 GET_CURRENT_CONTEXT(ctx);
1480
1481 if (MESA_VERBOSE & VERBOSE_DRAW)
1482 _mesa_debug(ctx, "glDrawElementsIndirect(%s, %s, %p)\n",
1483 _mesa_enum_to_string(mode),
1484 _mesa_enum_to_string(type), indirect);
1485
1486 if (!_mesa_validate_DrawElementsIndirect(ctx, mode, type, indirect))
1487 return;
1488
1489 vbo_validated_drawelementsindirect(ctx, mode, type, indirect);
1490 }
1491
1492 static void GLAPIENTRY
1493 vbo_exec_MultiDrawArraysIndirect(GLenum mode,
1494 const GLvoid *indirect,
1495 GLsizei primcount, GLsizei stride)
1496 {
1497 GET_CURRENT_CONTEXT(ctx);
1498
1499 if (MESA_VERBOSE & VERBOSE_DRAW)
1500 _mesa_debug(ctx, "glMultiDrawArraysIndirect(%s, %p, %i, %i)\n",
1501 _mesa_enum_to_string(mode), indirect, primcount, stride);
1502
1503 /* If <stride> is zero, the array elements are treated as tightly packed. */
1504 if (stride == 0)
1505 stride = 4 * sizeof(GLuint); /* sizeof(DrawArraysIndirectCommand) */
1506
1507 if (!_mesa_validate_MultiDrawArraysIndirect(ctx, mode,
1508 indirect,
1509 primcount, stride))
1510 return;
1511
1512 vbo_validated_multidrawarraysindirect(ctx, mode,
1513 indirect,
1514 primcount, stride);
1515 }
1516
1517 static void GLAPIENTRY
1518 vbo_exec_MultiDrawElementsIndirect(GLenum mode, GLenum type,
1519 const GLvoid *indirect,
1520 GLsizei primcount, GLsizei stride)
1521 {
1522 GET_CURRENT_CONTEXT(ctx);
1523
1524 if (MESA_VERBOSE & VERBOSE_DRAW)
1525 _mesa_debug(ctx, "glMultiDrawElementsIndirect(%s, %s, %p, %i, %i)\n",
1526 _mesa_enum_to_string(mode),
1527 _mesa_enum_to_string(type), indirect, primcount, stride);
1528
1529 /* If <stride> is zero, the array elements are treated as tightly packed. */
1530 if (stride == 0)
1531 stride = 5 * sizeof(GLuint); /* sizeof(DrawElementsIndirectCommand) */
1532
1533 if (!_mesa_validate_MultiDrawElementsIndirect(ctx, mode, type,
1534 indirect,
1535 primcount, stride))
1536 return;
1537
1538 vbo_validated_multidrawelementsindirect(ctx, mode, type,
1539 indirect,
1540 primcount, stride);
1541 }
1542
1543 static void
1544 vbo_validated_multidrawarraysindirectcount(struct gl_context *ctx,
1545 GLenum mode,
1546 GLintptr indirect,
1547 GLintptr drawcount,
1548 GLsizei maxdrawcount,
1549 GLsizei stride)
1550 {
1551 struct vbo_context *vbo = vbo_context(ctx);
1552 GLsizeiptr offset = indirect;
1553
1554 if (maxdrawcount == 0)
1555 return;
1556
1557 vbo_bind_arrays(ctx);
1558
1559 vbo->draw_indirect_prims(ctx, mode,
1560 ctx->DrawIndirectBuffer, offset,
1561 maxdrawcount, stride,
1562 ctx->ParameterBuffer, drawcount,
1563 NULL);
1564
1565 if (MESA_DEBUG_FLAGS & DEBUG_ALWAYS_FLUSH)
1566 _mesa_flush(ctx);
1567 }
1568
1569 static void
1570 vbo_validated_multidrawelementsindirectcount(struct gl_context *ctx,
1571 GLenum mode, GLenum type,
1572 GLintptr indirect,
1573 GLintptr drawcount,
1574 GLsizei maxdrawcount,
1575 GLsizei stride)
1576 {
1577 struct vbo_context *vbo = vbo_context(ctx);
1578 struct _mesa_index_buffer ib;
1579 GLsizeiptr offset = (GLsizeiptr)indirect;
1580
1581 if (maxdrawcount == 0)
1582 return;
1583
1584 vbo_bind_arrays(ctx);
1585
1586 /* NOTE: IndexBufferObj is guaranteed to be a VBO. */
1587
1588 ib.count = 0; /* unknown */
1589 ib.type = type;
1590 ib.obj = ctx->Array.VAO->IndexBufferObj;
1591 ib.ptr = NULL;
1592
1593 vbo->draw_indirect_prims(ctx, mode,
1594 ctx->DrawIndirectBuffer, offset,
1595 maxdrawcount, stride,
1596 ctx->ParameterBuffer, drawcount,
1597 &ib);
1598
1599 if (MESA_DEBUG_FLAGS & DEBUG_ALWAYS_FLUSH)
1600 _mesa_flush(ctx);
1601 }
1602
1603 static void GLAPIENTRY
1604 vbo_exec_MultiDrawArraysIndirectCount(GLenum mode,
1605 GLintptr indirect,
1606 GLintptr drawcount,
1607 GLsizei maxdrawcount, GLsizei stride)
1608 {
1609 GET_CURRENT_CONTEXT(ctx);
1610
1611 if (MESA_VERBOSE & VERBOSE_DRAW)
1612 _mesa_debug(ctx, "glMultiDrawArraysIndirectCountARB"
1613 "(%s, %lx, %lx, %i, %i)\n",
1614 _mesa_enum_to_string(mode), indirect,
1615 drawcount, maxdrawcount, stride);
1616
1617 /* If <stride> is zero, the array elements are treated as tightly packed. */
1618 if (stride == 0)
1619 stride = 4 * sizeof(GLuint); /* sizeof(DrawArraysIndirectCommand) */
1620
1621 if (!_mesa_validate_MultiDrawArraysIndirectCount(ctx, mode,
1622 indirect, drawcount,
1623 maxdrawcount, stride))
1624 return;
1625
1626 vbo_validated_multidrawarraysindirectcount(ctx, mode,
1627 indirect, drawcount,
1628 maxdrawcount, stride);
1629 }
1630
1631 static void GLAPIENTRY
1632 vbo_exec_MultiDrawElementsIndirectCount(GLenum mode, GLenum type,
1633 GLintptr indirect,
1634 GLintptr drawcount,
1635 GLsizei maxdrawcount, GLsizei stride)
1636 {
1637 GET_CURRENT_CONTEXT(ctx);
1638
1639 if (MESA_VERBOSE & VERBOSE_DRAW)
1640 _mesa_debug(ctx, "glMultiDrawElementsIndirectCountARB"
1641 "(%s, %s, %lx, %lx, %i, %i)\n",
1642 _mesa_enum_to_string(mode),
1643 _mesa_enum_to_string(type), indirect,
1644 drawcount, maxdrawcount, stride);
1645
1646 /* If <stride> is zero, the array elements are treated as tightly packed. */
1647 if (stride == 0)
1648 stride = 5 * sizeof(GLuint); /* sizeof(DrawElementsIndirectCommand) */
1649
1650 if (!_mesa_validate_MultiDrawElementsIndirectCount(ctx, mode, type,
1651 indirect, drawcount,
1652 maxdrawcount, stride))
1653 return;
1654
1655 vbo_validated_multidrawelementsindirectcount(ctx, mode, type,
1656 indirect, drawcount,
1657 maxdrawcount, stride);
1658 }
1659
1660
1661 /**
1662 * Initialize the dispatch table with the VBO functions for drawing.
1663 */
1664 void
1665 vbo_initialize_exec_dispatch(const struct gl_context *ctx,
1666 struct _glapi_table *exec)
1667 {
1668 SET_DrawArrays(exec, vbo_exec_DrawArrays);
1669 SET_DrawElements(exec, vbo_exec_DrawElements);
1670
1671 if (_mesa_is_desktop_gl(ctx) || _mesa_is_gles3(ctx)) {
1672 SET_DrawRangeElements(exec, vbo_exec_DrawRangeElements);
1673 }
1674
1675 SET_MultiDrawElementsEXT(exec, vbo_exec_MultiDrawElements);
1676
1677 if (ctx->API == API_OPENGL_COMPAT) {
1678 SET_Rectf(exec, vbo_exec_Rectf);
1679 SET_EvalMesh1(exec, vbo_exec_EvalMesh1);
1680 SET_EvalMesh2(exec, vbo_exec_EvalMesh2);
1681 }
1682
1683 if (ctx->API != API_OPENGLES &&
1684 ctx->Extensions.ARB_draw_elements_base_vertex) {
1685 SET_DrawElementsBaseVertex(exec, vbo_exec_DrawElementsBaseVertex);
1686 SET_MultiDrawElementsBaseVertex(exec, vbo_exec_MultiDrawElementsBaseVertex);
1687
1688 if (_mesa_is_desktop_gl(ctx) || _mesa_is_gles3(ctx)) {
1689 SET_DrawRangeElementsBaseVertex(exec, vbo_exec_DrawRangeElementsBaseVertex);
1690 SET_DrawElementsInstancedBaseVertex(exec, vbo_exec_DrawElementsInstancedBaseVertex);
1691 }
1692 }
1693
1694 if (_mesa_is_desktop_gl(ctx) || _mesa_is_gles3(ctx)) {
1695 SET_DrawArraysInstancedBaseInstance(exec, vbo_exec_DrawArraysInstancedBaseInstance);
1696 SET_DrawElementsInstancedBaseInstance(exec, vbo_exec_DrawElementsInstancedBaseInstance);
1697 SET_DrawElementsInstancedBaseVertexBaseInstance(exec, vbo_exec_DrawElementsInstancedBaseVertexBaseInstance);
1698 }
1699
1700 if (ctx->API == API_OPENGL_CORE || _mesa_is_gles31(ctx)) {
1701 SET_DrawArraysIndirect(exec, vbo_exec_DrawArraysIndirect);
1702 SET_DrawElementsIndirect(exec, vbo_exec_DrawElementsIndirect);
1703 }
1704
1705 if (ctx->API == API_OPENGL_CORE) {
1706 SET_MultiDrawArraysIndirect(exec, vbo_exec_MultiDrawArraysIndirect);
1707 SET_MultiDrawElementsIndirect(exec, vbo_exec_MultiDrawElementsIndirect);
1708 SET_MultiDrawArraysIndirectCountARB(exec, vbo_exec_MultiDrawArraysIndirectCount);
1709 SET_MultiDrawElementsIndirectCountARB(exec, vbo_exec_MultiDrawElementsIndirectCount);
1710 }
1711
1712 if (_mesa_is_desktop_gl(ctx) || _mesa_is_gles3(ctx)) {
1713 SET_DrawArraysInstancedARB(exec, vbo_exec_DrawArraysInstanced);
1714 SET_DrawElementsInstancedARB(exec, vbo_exec_DrawElementsInstanced);
1715 }
1716
1717 if (_mesa_is_desktop_gl(ctx)) {
1718 SET_DrawTransformFeedback(exec, vbo_exec_DrawTransformFeedback);
1719 SET_DrawTransformFeedbackStream(exec, vbo_exec_DrawTransformFeedbackStream);
1720 SET_DrawTransformFeedbackInstanced(exec, vbo_exec_DrawTransformFeedbackInstanced);
1721 SET_DrawTransformFeedbackStreamInstanced(exec, vbo_exec_DrawTransformFeedbackStreamInstanced);
1722 }
1723 }
1724
1725
1726
1727 /**
1728 * The following functions are only used for OpenGL ES 1/2 support.
1729 * And some aren't even supported (yet) in ES 1/2.
1730 */
1731
1732
1733 void GLAPIENTRY
1734 _mesa_DrawArrays(GLenum mode, GLint first, GLsizei count)
1735 {
1736 vbo_exec_DrawArrays(mode, first, count);
1737 }
1738
1739 void GLAPIENTRY
1740 _mesa_DrawArraysInstanced(GLenum mode, GLint first, GLsizei count,
1741 GLsizei primcount)
1742 {
1743 vbo_exec_DrawArraysInstanced(mode, first, count, primcount);
1744 }
1745
1746 void GLAPIENTRY
1747 _mesa_DrawElements(GLenum mode, GLsizei count, GLenum type,
1748 const GLvoid *indices)
1749 {
1750 vbo_exec_DrawElements(mode, count, type, indices);
1751 }
1752
1753
1754 void GLAPIENTRY
1755 _mesa_DrawElementsBaseVertex(GLenum mode, GLsizei count, GLenum type,
1756 const GLvoid *indices, GLint basevertex)
1757 {
1758 vbo_exec_DrawElementsBaseVertex(mode, count, type, indices, basevertex);
1759 }
1760
1761
1762 void GLAPIENTRY
1763 _mesa_DrawRangeElements(GLenum mode, GLuint start, GLuint end, GLsizei count,
1764 GLenum type, const GLvoid *indices)
1765 {
1766 vbo_exec_DrawRangeElements(mode, start, end, count, type, indices);
1767 }
1768
1769
1770 void GLAPIENTRY
1771 _mesa_DrawRangeElementsBaseVertex(GLenum mode, GLuint start, GLuint end,
1772 GLsizei count, GLenum type,
1773 const GLvoid *indices, GLint basevertex)
1774 {
1775 vbo_exec_DrawRangeElementsBaseVertex(mode, start, end, count, type,
1776 indices, basevertex);
1777 }
1778
1779
1780 void GLAPIENTRY
1781 _mesa_MultiDrawElementsEXT(GLenum mode, const GLsizei *count, GLenum type,
1782 const GLvoid **indices, GLsizei primcount)
1783 {
1784 vbo_exec_MultiDrawElements(mode, count, type, indices, primcount);
1785 }
1786
1787
1788 void GLAPIENTRY
1789 _mesa_MultiDrawElementsBaseVertex(GLenum mode,
1790 const GLsizei *count, GLenum type,
1791 const GLvoid **indices, GLsizei primcount,
1792 const GLint *basevertex)
1793 {
1794 vbo_exec_MultiDrawElementsBaseVertex(mode, count, type, indices,
1795 primcount, basevertex);
1796 }
1797
1798 void GLAPIENTRY
1799 _mesa_DrawTransformFeedback(GLenum mode, GLuint name)
1800 {
1801 vbo_exec_DrawTransformFeedback(mode, name);
1802 }