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