vbo: simplify some code in check_draw_elements_data()
[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_array_attributes *array = &vao->VertexAttrib[attrib];
55 if (array->Enabled) {
56 const struct gl_vertex_buffer_binding *binding =
57 &vao->VertexBinding[array->BufferBindingIndex];
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_array_attributes *array = &vao->VertexAttrib[attrib];
107 if (array->Enabled) {
108 const struct gl_vertex_buffer_binding *binding =
109 &vao->VertexBinding[array->BufferBindingIndex];
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(vao->IndexBufferObj)) {
132 elemMap = ctx->Driver.MapBufferRange(ctx, 0,
133 vao->IndexBufferObj->Size,
134 GL_MAP_READ_BIT,
135 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, vao->IndexBufferObj, MAP_INTERNAL);
166 }
167
168 for (k = 0; k < VERT_ATTRIB_MAX; k++) {
169 unmap_array_buffer(ctx, vao, k);
170 }
171 }
172
173
174 /**
175 * Check array data, looking for NaNs, etc.
176 */
177 static void
178 check_draw_arrays_data(struct gl_context *ctx, GLint start, GLsizei count)
179 {
180 /* TO DO */
181 }
182
183
184 /**
185 * Print info/data for glDrawArrays(), for debugging.
186 */
187 static void
188 print_draw_arrays(struct gl_context *ctx,
189 GLenum mode, GLint start, GLsizei count)
190 {
191 const struct gl_vertex_array_object *vao = ctx->Array.VAO;
192
193 printf("vbo_exec_DrawArrays(mode 0x%x, start %d, count %d):\n",
194 mode, start, count);
195
196 unsigned i;
197 for (i = 0; i < VERT_ATTRIB_MAX; ++i) {
198 const struct gl_array_attributes *array = &vao->VertexAttrib[i];
199 if (!array->Enabled)
200 continue;
201
202 const struct gl_vertex_buffer_binding *binding =
203 &vao->VertexBinding[array->BufferBindingIndex];
204 struct gl_buffer_object *bufObj = binding->BufferObj;
205
206 printf("attr %s: size %d stride %d enabled %d "
207 "ptr %p Bufobj %u\n",
208 gl_vert_attrib_name((gl_vert_attrib)i),
209 array->Size, binding->Stride, array->Enabled,
210 array->Ptr, bufObj->Name);
211
212 if (_mesa_is_bufferobj(bufObj)) {
213 GLubyte *p = ctx->Driver.MapBufferRange(ctx, 0, bufObj->Size,
214 GL_MAP_READ_BIT, bufObj,
215 MAP_INTERNAL);
216 int offset = (int) (GLintptr)
217 _mesa_vertex_attrib_address(array, binding);
218 float *f = (float *) (p + offset);
219 int *k = (int *) f;
220 int i;
221 int n = (count * binding->Stride) / 4;
222 if (n > 32)
223 n = 32;
224 printf(" Data at offset %d:\n", offset);
225 for (i = 0; i < n; i++) {
226 printf(" float[%d] = 0x%08x %f\n", i, k[i], f[i]);
227 }
228 ctx->Driver.UnmapBuffer(ctx, bufObj, MAP_INTERNAL);
229 }
230 }
231 }
232
233
234 /**
235 * Set the vbo->exec->inputs[] pointers to point to the enabled
236 * vertex arrays. This depends on the current vertex program/shader
237 * being executed because of whether or not generic vertex arrays
238 * alias the conventional vertex arrays.
239 * For arrays that aren't enabled, we set the input[attrib] pointer
240 * to point at a zero-stride current value "array".
241 */
242 static void
243 recalculate_input_bindings(struct gl_context *ctx)
244 {
245 struct vbo_context *vbo = vbo_context(ctx);
246 struct vbo_exec_context *exec = &vbo->exec;
247 const struct gl_array_attributes *array = ctx->Array.VAO->VertexAttrib;
248 struct gl_client_array *vertexAttrib = ctx->Array.VAO->_VertexAttrib;
249 const struct gl_client_array **inputs = &exec->array.inputs[0];
250 GLbitfield64 const_inputs = 0x0;
251 GLuint i;
252
253 switch (get_program_mode(ctx)) {
254 case VP_NONE:
255 /* When no vertex program is active (or the vertex program is generated
256 * from fixed-function state). We put the material values into the
257 * generic slots. This is the only situation where material values
258 * are available as per-vertex attributes.
259 */
260 for (i = 0; i < VERT_ATTRIB_FF_MAX; i++) {
261 if (array[VERT_ATTRIB_FF(i)].Enabled)
262 inputs[i] = &vertexAttrib[VERT_ATTRIB_FF(i)];
263 else {
264 inputs[i] = &vbo->currval[VBO_ATTRIB_POS+i];
265 const_inputs |= VERT_BIT(i);
266 }
267 }
268
269 for (i = 0; i < MAT_ATTRIB_MAX; i++) {
270 inputs[VERT_ATTRIB_GENERIC(i)] =
271 &vbo->currval[VBO_ATTRIB_MAT_FRONT_AMBIENT+i];
272 const_inputs |= VERT_BIT_GENERIC(i);
273 }
274
275 /* Could use just about anything, just to fill in the empty
276 * slots:
277 */
278 for (i = MAT_ATTRIB_MAX; i < VERT_ATTRIB_GENERIC_MAX; i++) {
279 inputs[VERT_ATTRIB_GENERIC(i)] = &vbo->currval[VBO_ATTRIB_GENERIC0+i];
280 const_inputs |= VERT_BIT_GENERIC(i);
281 }
282 break;
283
284 case VP_ARB:
285 /* There are no shaders in OpenGL ES 1.x, so this code path should be
286 * impossible to reach. The meta code is careful to not use shaders in
287 * ES1.
288 */
289 assert(ctx->API != API_OPENGLES);
290
291 /* In the compatibility profile of desktop OpenGL, the generic[0]
292 * attribute array aliases and overrides the legacy position array.
293 * Otherwise, legacy attributes available in the legacy slots,
294 * generic attributes in the generic slots and materials are not
295 * available as per-vertex attributes.
296 *
297 * In all other APIs, only the generic attributes exist, and none of the
298 * slots are considered "magic."
299 */
300 if (ctx->API == API_OPENGL_COMPAT) {
301 if (array[VERT_ATTRIB_GENERIC0].Enabled)
302 inputs[0] = &vertexAttrib[VERT_ATTRIB_GENERIC0];
303 else if (array[VERT_ATTRIB_POS].Enabled)
304 inputs[0] = &vertexAttrib[VERT_ATTRIB_POS];
305 else {
306 inputs[0] = &vbo->currval[VBO_ATTRIB_POS];
307 const_inputs |= VERT_BIT_POS;
308 }
309
310 for (i = 1; i < VERT_ATTRIB_FF_MAX; i++) {
311 if (array[VERT_ATTRIB_FF(i)].Enabled)
312 inputs[i] = &vertexAttrib[VERT_ATTRIB_FF(i)];
313 else {
314 inputs[i] = &vbo->currval[VBO_ATTRIB_POS+i];
315 const_inputs |= VERT_BIT_FF(i);
316 }
317 }
318
319 for (i = 1; i < VERT_ATTRIB_GENERIC_MAX; i++) {
320 if (array[VERT_ATTRIB_GENERIC(i)].Enabled)
321 inputs[VERT_ATTRIB_GENERIC(i)] =
322 &vertexAttrib[VERT_ATTRIB_GENERIC(i)];
323 else {
324 inputs[VERT_ATTRIB_GENERIC(i)] =
325 &vbo->currval[VBO_ATTRIB_GENERIC0+i];
326 const_inputs |= VERT_BIT_GENERIC(i);
327 }
328 }
329
330 inputs[VERT_ATTRIB_GENERIC0] = inputs[0];
331 } else {
332 /* Other parts of the code assume that inputs[0] through
333 * inputs[VERT_ATTRIB_FF_MAX] will be non-NULL. However, in OpenGL
334 * ES 2.0+ or OpenGL core profile, none of these arrays should ever
335 * be enabled.
336 */
337 for (i = 0; i < VERT_ATTRIB_FF_MAX; i++) {
338 assert(!array[VERT_ATTRIB_FF(i)].Enabled);
339
340 inputs[i] = &vbo->currval[VBO_ATTRIB_POS+i];
341 const_inputs |= VERT_BIT_FF(i);
342 }
343
344 for (i = 0; i < VERT_ATTRIB_GENERIC_MAX; i++) {
345 if (array[VERT_ATTRIB_GENERIC(i)].Enabled)
346 inputs[VERT_ATTRIB_GENERIC(i)] =
347 &vertexAttrib[VERT_ATTRIB_GENERIC(i)];
348 else {
349 inputs[VERT_ATTRIB_GENERIC(i)] =
350 &vbo->currval[VBO_ATTRIB_GENERIC0+i];
351 const_inputs |= VERT_BIT_GENERIC(i);
352 }
353 }
354 }
355
356 break;
357 }
358
359 _mesa_set_varying_vp_inputs( ctx, VERT_BIT_ALL & (~const_inputs) );
360 ctx->NewDriverState |= ctx->DriverFlags.NewArray;
361 }
362
363
364 /**
365 * Examine the enabled vertex arrays to set the exec->array.inputs[] values.
366 * These will point to the arrays to actually use for drawing. Some will
367 * be user-provided arrays, other will be zero-stride const-valued arrays.
368 * Note that this might set the _NEW_VARYING_VP_INPUTS dirty flag so state
369 * validation must be done after this call.
370 */
371 void
372 vbo_bind_arrays(struct gl_context *ctx)
373 {
374 struct vbo_context *vbo = vbo_context(ctx);
375 struct vbo_exec_context *exec = &vbo->exec;
376
377 vbo_draw_method(vbo, DRAW_ARRAYS);
378
379 if (exec->array.recalculate_inputs) {
380 recalculate_input_bindings(ctx);
381 exec->array.recalculate_inputs = GL_FALSE;
382
383 /* Again... because we may have changed the bitmask of per-vertex varying
384 * attributes. If we regenerate the fixed-function vertex program now
385 * we may be able to prune down the number of vertex attributes which we
386 * need in the shader.
387 */
388 if (ctx->NewState) {
389 /* Setting "validating" to TRUE prevents _mesa_update_state from
390 * invalidating what we just did.
391 */
392 exec->validating = GL_TRUE;
393 _mesa_update_state(ctx);
394 exec->validating = GL_FALSE;
395 }
396 }
397 }
398
399 /**
400 * Helper function called by the other DrawArrays() functions below.
401 * This is where we handle primitive restart for drawing non-indexed
402 * arrays. If primitive restart is enabled, it typically means
403 * splitting one DrawArrays() into two.
404 */
405 static void
406 vbo_draw_arrays(struct gl_context *ctx, GLenum mode, GLint start,
407 GLsizei count, GLuint numInstances, GLuint baseInstance)
408 {
409 struct vbo_context *vbo = vbo_context(ctx);
410 struct _mesa_prim prim[2];
411
412 vbo_bind_arrays(ctx);
413
414 /* init most fields to zero */
415 memset(prim, 0, sizeof(prim));
416 prim[0].begin = 1;
417 prim[0].end = 1;
418 prim[0].mode = mode;
419 prim[0].num_instances = numInstances;
420 prim[0].base_instance = baseInstance;
421 prim[0].is_indirect = 0;
422
423 /* Implement the primitive restart index */
424 if (ctx->Array.PrimitiveRestart && !ctx->Array.PrimitiveRestartFixedIndex &&
425 ctx->Array.RestartIndex < count) {
426 GLuint primCount = 0;
427
428 if (ctx->Array.RestartIndex == start) {
429 /* special case: RestartIndex at beginning */
430 if (count > 1) {
431 prim[0].start = start + 1;
432 prim[0].count = count - 1;
433 primCount = 1;
434 }
435 }
436 else if (ctx->Array.RestartIndex == start + count - 1) {
437 /* special case: RestartIndex at end */
438 if (count > 1) {
439 prim[0].start = start;
440 prim[0].count = count - 1;
441 primCount = 1;
442 }
443 }
444 else {
445 /* general case: RestartIndex in middle, split into two prims */
446 prim[0].start = start;
447 prim[0].count = ctx->Array.RestartIndex - start;
448
449 prim[1] = prim[0];
450 prim[1].start = ctx->Array.RestartIndex + 1;
451 prim[1].count = count - prim[1].start;
452
453 primCount = 2;
454 }
455
456 if (primCount > 0) {
457 /* draw one or two prims */
458 vbo->draw_prims(ctx, prim, primCount, NULL,
459 GL_TRUE, start, start + count - 1, NULL, 0, NULL);
460 }
461 }
462 else {
463 /* no prim restart */
464 prim[0].start = start;
465 prim[0].count = count;
466
467 vbo->draw_prims(ctx, prim, 1, NULL,
468 GL_TRUE, start, start + count - 1,
469 NULL, 0, NULL);
470 }
471
472 if (MESA_DEBUG_FLAGS & DEBUG_ALWAYS_FLUSH) {
473 _mesa_flush(ctx);
474 }
475 }
476
477
478 /**
479 * Execute a glRectf() function.
480 */
481 static void GLAPIENTRY
482 vbo_exec_Rectf(GLfloat x1, GLfloat y1, GLfloat x2, GLfloat y2)
483 {
484 GET_CURRENT_CONTEXT(ctx);
485 ASSERT_OUTSIDE_BEGIN_END(ctx);
486
487 CALL_Begin(GET_DISPATCH(), (GL_QUADS));
488 CALL_Vertex2f(GET_DISPATCH(), (x1, y1));
489 CALL_Vertex2f(GET_DISPATCH(), (x2, y1));
490 CALL_Vertex2f(GET_DISPATCH(), (x2, y2));
491 CALL_Vertex2f(GET_DISPATCH(), (x1, y2));
492 CALL_End(GET_DISPATCH(), ());
493 }
494
495
496 static void GLAPIENTRY
497 vbo_exec_EvalMesh1(GLenum mode, GLint i1, GLint i2)
498 {
499 GET_CURRENT_CONTEXT(ctx);
500 GLint i;
501 GLfloat u, du;
502 GLenum prim;
503
504 switch (mode) {
505 case GL_POINT:
506 prim = GL_POINTS;
507 break;
508 case GL_LINE:
509 prim = GL_LINE_STRIP;
510 break;
511 default:
512 _mesa_error( ctx, GL_INVALID_ENUM, "glEvalMesh1(mode)" );
513 return;
514 }
515
516 /* No effect if vertex maps disabled.
517 */
518 if (!ctx->Eval.Map1Vertex4 &&
519 !ctx->Eval.Map1Vertex3)
520 return;
521
522 du = ctx->Eval.MapGrid1du;
523 u = ctx->Eval.MapGrid1u1 + i1 * du;
524
525 CALL_Begin(GET_DISPATCH(), (prim));
526 for (i=i1;i<=i2;i++,u+=du) {
527 CALL_EvalCoord1f(GET_DISPATCH(), (u));
528 }
529 CALL_End(GET_DISPATCH(), ());
530 }
531
532
533 static void GLAPIENTRY
534 vbo_exec_EvalMesh2(GLenum mode, GLint i1, GLint i2, GLint j1, GLint j2)
535 {
536 GET_CURRENT_CONTEXT(ctx);
537 GLfloat u, du, v, dv, v1, u1;
538 GLint i, j;
539
540 switch (mode) {
541 case GL_POINT:
542 case GL_LINE:
543 case GL_FILL:
544 break;
545 default:
546 _mesa_error( ctx, GL_INVALID_ENUM, "glEvalMesh2(mode)" );
547 return;
548 }
549
550 /* No effect if vertex maps disabled.
551 */
552 if (!ctx->Eval.Map2Vertex4 &&
553 !ctx->Eval.Map2Vertex3)
554 return;
555
556 du = ctx->Eval.MapGrid2du;
557 dv = ctx->Eval.MapGrid2dv;
558 v1 = ctx->Eval.MapGrid2v1 + j1 * dv;
559 u1 = ctx->Eval.MapGrid2u1 + i1 * du;
560
561 switch (mode) {
562 case GL_POINT:
563 CALL_Begin(GET_DISPATCH(), (GL_POINTS));
564 for (v=v1,j=j1;j<=j2;j++,v+=dv) {
565 for (u=u1,i=i1;i<=i2;i++,u+=du) {
566 CALL_EvalCoord2f(GET_DISPATCH(), (u, v));
567 }
568 }
569 CALL_End(GET_DISPATCH(), ());
570 break;
571 case GL_LINE:
572 for (v=v1,j=j1;j<=j2;j++,v+=dv) {
573 CALL_Begin(GET_DISPATCH(), (GL_LINE_STRIP));
574 for (u=u1,i=i1;i<=i2;i++,u+=du) {
575 CALL_EvalCoord2f(GET_DISPATCH(), (u, v));
576 }
577 CALL_End(GET_DISPATCH(), ());
578 }
579 for (u=u1,i=i1;i<=i2;i++,u+=du) {
580 CALL_Begin(GET_DISPATCH(), (GL_LINE_STRIP));
581 for (v=v1,j=j1;j<=j2;j++,v+=dv) {
582 CALL_EvalCoord2f(GET_DISPATCH(), (u, v));
583 }
584 CALL_End(GET_DISPATCH(), ());
585 }
586 break;
587 case GL_FILL:
588 for (v=v1,j=j1;j<j2;j++,v+=dv) {
589 CALL_Begin(GET_DISPATCH(), (GL_TRIANGLE_STRIP));
590 for (u=u1,i=i1;i<=i2;i++,u+=du) {
591 CALL_EvalCoord2f(GET_DISPATCH(), (u, v));
592 CALL_EvalCoord2f(GET_DISPATCH(), (u, v+dv));
593 }
594 CALL_End(GET_DISPATCH(), ());
595 }
596 break;
597 }
598 }
599
600
601 /**
602 * Called from glDrawArrays when in immediate mode (not display list mode).
603 */
604 static void GLAPIENTRY
605 vbo_exec_DrawArrays(GLenum mode, GLint start, GLsizei count)
606 {
607 GET_CURRENT_CONTEXT(ctx);
608
609 if (MESA_VERBOSE & VERBOSE_DRAW)
610 _mesa_debug(ctx, "glDrawArrays(%s, %d, %d)\n",
611 _mesa_enum_to_string(mode), start, count);
612
613 if (!_mesa_validate_DrawArrays(ctx, mode, count))
614 return;
615
616 if (0)
617 check_draw_arrays_data(ctx, start, count);
618
619 vbo_draw_arrays(ctx, mode, start, count, 1, 0);
620
621 if (0)
622 print_draw_arrays(ctx, mode, start, count);
623 }
624
625
626 /**
627 * Called from glDrawArraysInstanced when in immediate mode (not
628 * display list mode).
629 */
630 static void GLAPIENTRY
631 vbo_exec_DrawArraysInstanced(GLenum mode, GLint start, GLsizei count,
632 GLsizei numInstances)
633 {
634 GET_CURRENT_CONTEXT(ctx);
635
636 if (MESA_VERBOSE & VERBOSE_DRAW)
637 _mesa_debug(ctx, "glDrawArraysInstanced(%s, %d, %d, %d)\n",
638 _mesa_enum_to_string(mode), start, count, numInstances);
639
640 if (!_mesa_validate_DrawArraysInstanced(ctx, mode, start, count, numInstances))
641 return;
642
643 if (0)
644 check_draw_arrays_data(ctx, start, count);
645
646 vbo_draw_arrays(ctx, mode, start, count, numInstances, 0);
647
648 if (0)
649 print_draw_arrays(ctx, mode, start, count);
650 }
651
652
653 /**
654 * Called from glDrawArraysInstancedBaseInstance when in immediate mode.
655 */
656 static void GLAPIENTRY
657 vbo_exec_DrawArraysInstancedBaseInstance(GLenum mode, GLint first, GLsizei count,
658 GLsizei numInstances, GLuint baseInstance)
659 {
660 GET_CURRENT_CONTEXT(ctx);
661
662 if (MESA_VERBOSE & VERBOSE_DRAW)
663 _mesa_debug(ctx, "glDrawArraysInstancedBaseInstance(%s, %d, %d, %d, %d)\n",
664 _mesa_enum_to_string(mode), first, count,
665 numInstances, baseInstance);
666
667 if (!_mesa_validate_DrawArraysInstanced(ctx, mode, first, count,
668 numInstances))
669 return;
670
671 if (0)
672 check_draw_arrays_data(ctx, first, count);
673
674 vbo_draw_arrays(ctx, mode, first, count, numInstances, baseInstance);
675
676 if (0)
677 print_draw_arrays(ctx, mode, first, count);
678 }
679
680
681
682 /**
683 * Map GL_ELEMENT_ARRAY_BUFFER and print contents.
684 * For debugging.
685 */
686 #if 0
687 static void
688 dump_element_buffer(struct gl_context *ctx, GLenum type)
689 {
690 const GLvoid *map =
691 ctx->Driver.MapBufferRange(ctx, 0,
692 ctx->Array.VAO->IndexBufferObj->Size,
693 GL_MAP_READ_BIT,
694 ctx->Array.VAO->IndexBufferObj,
695 MAP_INTERNAL);
696 switch (type) {
697 case GL_UNSIGNED_BYTE:
698 {
699 const GLubyte *us = (const GLubyte *) map;
700 GLint i;
701 for (i = 0; i < ctx->Array.VAO->IndexBufferObj->Size; i++) {
702 printf("%02x ", us[i]);
703 if (i % 32 == 31)
704 printf("\n");
705 }
706 printf("\n");
707 }
708 break;
709 case GL_UNSIGNED_SHORT:
710 {
711 const GLushort *us = (const GLushort *) map;
712 GLint i;
713 for (i = 0; i < ctx->Array.VAO->IndexBufferObj->Size / 2; i++) {
714 printf("%04x ", us[i]);
715 if (i % 16 == 15)
716 printf("\n");
717 }
718 printf("\n");
719 }
720 break;
721 case GL_UNSIGNED_INT:
722 {
723 const GLuint *us = (const GLuint *) map;
724 GLint i;
725 for (i = 0; i < ctx->Array.VAO->IndexBufferObj->Size / 4; i++) {
726 printf("%08x ", us[i]);
727 if (i % 8 == 7)
728 printf("\n");
729 }
730 printf("\n");
731 }
732 break;
733 default:
734 ;
735 }
736
737 ctx->Driver.UnmapBuffer(ctx, ctx->Array.VAO->IndexBufferObj,
738 MAP_INTERNAL);
739 }
740 #endif
741
742
743 /**
744 * Inner support for both _mesa_DrawElements and _mesa_DrawRangeElements.
745 * Do the rendering for a glDrawElements or glDrawRangeElements call after
746 * we've validated buffer bounds, etc.
747 */
748 static void
749 vbo_validated_drawrangeelements(struct gl_context *ctx, GLenum mode,
750 GLboolean index_bounds_valid,
751 GLuint start, GLuint end,
752 GLsizei count, GLenum type,
753 const GLvoid *indices,
754 GLint basevertex, GLuint numInstances,
755 GLuint baseInstance)
756 {
757 struct vbo_context *vbo = vbo_context(ctx);
758 struct _mesa_index_buffer ib;
759 struct _mesa_prim prim[1];
760
761 vbo_bind_arrays(ctx);
762
763 ib.count = count;
764 ib.type = type;
765 ib.obj = ctx->Array.VAO->IndexBufferObj;
766 ib.ptr = indices;
767
768 prim[0].begin = 1;
769 prim[0].end = 1;
770 prim[0].weak = 0;
771 prim[0].pad = 0;
772 prim[0].mode = mode;
773 prim[0].start = 0;
774 prim[0].count = count;
775 prim[0].indexed = 1;
776 prim[0].is_indirect = 0;
777 prim[0].basevertex = basevertex;
778 prim[0].num_instances = numInstances;
779 prim[0].base_instance = baseInstance;
780 prim[0].draw_id = 0;
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),
1615 (unsigned long)indirect, (unsigned long)drawcount,
1616 maxdrawcount, stride);
1617
1618 /* If <stride> is zero, the array elements are treated as tightly packed. */
1619 if (stride == 0)
1620 stride = 4 * sizeof(GLuint); /* sizeof(DrawArraysIndirectCommand) */
1621
1622 if (!_mesa_validate_MultiDrawArraysIndirectCount(ctx, mode,
1623 indirect, drawcount,
1624 maxdrawcount, stride))
1625 return;
1626
1627 vbo_validated_multidrawarraysindirectcount(ctx, mode,
1628 indirect, drawcount,
1629 maxdrawcount, stride);
1630 }
1631
1632 static void GLAPIENTRY
1633 vbo_exec_MultiDrawElementsIndirectCount(GLenum mode, GLenum type,
1634 GLintptr indirect,
1635 GLintptr drawcount,
1636 GLsizei maxdrawcount, GLsizei stride)
1637 {
1638 GET_CURRENT_CONTEXT(ctx);
1639
1640 if (MESA_VERBOSE & VERBOSE_DRAW)
1641 _mesa_debug(ctx, "glMultiDrawElementsIndirectCountARB"
1642 "(%s, %s, %lx, %lx, %i, %i)\n",
1643 _mesa_enum_to_string(mode), _mesa_enum_to_string(type),
1644 (unsigned long)indirect, (unsigned long)drawcount,
1645 maxdrawcount, stride);
1646
1647 /* If <stride> is zero, the array elements are treated as tightly packed. */
1648 if (stride == 0)
1649 stride = 5 * sizeof(GLuint); /* sizeof(DrawElementsIndirectCommand) */
1650
1651 if (!_mesa_validate_MultiDrawElementsIndirectCount(ctx, mode, type,
1652 indirect, drawcount,
1653 maxdrawcount, stride))
1654 return;
1655
1656 vbo_validated_multidrawelementsindirectcount(ctx, mode, type,
1657 indirect, drawcount,
1658 maxdrawcount, stride);
1659 }
1660
1661
1662 /**
1663 * Initialize the dispatch table with the VBO functions for drawing.
1664 */
1665 void
1666 vbo_initialize_exec_dispatch(const struct gl_context *ctx,
1667 struct _glapi_table *exec)
1668 {
1669 SET_DrawArrays(exec, vbo_exec_DrawArrays);
1670 SET_DrawElements(exec, vbo_exec_DrawElements);
1671
1672 if (_mesa_is_desktop_gl(ctx) || _mesa_is_gles3(ctx)) {
1673 SET_DrawRangeElements(exec, vbo_exec_DrawRangeElements);
1674 }
1675
1676 SET_MultiDrawElementsEXT(exec, vbo_exec_MultiDrawElements);
1677
1678 if (ctx->API == API_OPENGL_COMPAT) {
1679 SET_Rectf(exec, vbo_exec_Rectf);
1680 SET_EvalMesh1(exec, vbo_exec_EvalMesh1);
1681 SET_EvalMesh2(exec, vbo_exec_EvalMesh2);
1682 }
1683
1684 if (ctx->API != API_OPENGLES &&
1685 ctx->Extensions.ARB_draw_elements_base_vertex) {
1686 SET_DrawElementsBaseVertex(exec, vbo_exec_DrawElementsBaseVertex);
1687 SET_MultiDrawElementsBaseVertex(exec, vbo_exec_MultiDrawElementsBaseVertex);
1688
1689 if (_mesa_is_desktop_gl(ctx) || _mesa_is_gles3(ctx)) {
1690 SET_DrawRangeElementsBaseVertex(exec, vbo_exec_DrawRangeElementsBaseVertex);
1691 SET_DrawElementsInstancedBaseVertex(exec, vbo_exec_DrawElementsInstancedBaseVertex);
1692 }
1693 }
1694
1695 if (_mesa_is_desktop_gl(ctx) || _mesa_is_gles3(ctx)) {
1696 SET_DrawArraysInstancedBaseInstance(exec, vbo_exec_DrawArraysInstancedBaseInstance);
1697 SET_DrawElementsInstancedBaseInstance(exec, vbo_exec_DrawElementsInstancedBaseInstance);
1698 SET_DrawElementsInstancedBaseVertexBaseInstance(exec, vbo_exec_DrawElementsInstancedBaseVertexBaseInstance);
1699 }
1700
1701 if (ctx->API == API_OPENGL_CORE || _mesa_is_gles31(ctx)) {
1702 SET_DrawArraysIndirect(exec, vbo_exec_DrawArraysIndirect);
1703 SET_DrawElementsIndirect(exec, vbo_exec_DrawElementsIndirect);
1704 }
1705
1706 if (ctx->API == API_OPENGL_CORE) {
1707 SET_MultiDrawArraysIndirect(exec, vbo_exec_MultiDrawArraysIndirect);
1708 SET_MultiDrawElementsIndirect(exec, vbo_exec_MultiDrawElementsIndirect);
1709 SET_MultiDrawArraysIndirectCountARB(exec, vbo_exec_MultiDrawArraysIndirectCount);
1710 SET_MultiDrawElementsIndirectCountARB(exec, vbo_exec_MultiDrawElementsIndirectCount);
1711 }
1712
1713 if (_mesa_is_desktop_gl(ctx) || _mesa_is_gles3(ctx)) {
1714 SET_DrawArraysInstancedARB(exec, vbo_exec_DrawArraysInstanced);
1715 SET_DrawElementsInstancedARB(exec, vbo_exec_DrawElementsInstanced);
1716 }
1717
1718 if (_mesa_is_desktop_gl(ctx)) {
1719 SET_DrawTransformFeedback(exec, vbo_exec_DrawTransformFeedback);
1720 SET_DrawTransformFeedbackStream(exec, vbo_exec_DrawTransformFeedbackStream);
1721 SET_DrawTransformFeedbackInstanced(exec, vbo_exec_DrawTransformFeedbackInstanced);
1722 SET_DrawTransformFeedbackStreamInstanced(exec, vbo_exec_DrawTransformFeedbackStreamInstanced);
1723 }
1724 }
1725
1726
1727
1728 /**
1729 * The following functions are only used for OpenGL ES 1/2 support.
1730 * And some aren't even supported (yet) in ES 1/2.
1731 */
1732
1733
1734 void GLAPIENTRY
1735 _mesa_DrawArrays(GLenum mode, GLint first, GLsizei count)
1736 {
1737 vbo_exec_DrawArrays(mode, first, count);
1738 }
1739
1740 void GLAPIENTRY
1741 _mesa_DrawArraysInstanced(GLenum mode, GLint first, GLsizei count,
1742 GLsizei primcount)
1743 {
1744 vbo_exec_DrawArraysInstanced(mode, first, count, primcount);
1745 }
1746
1747 void GLAPIENTRY
1748 _mesa_DrawElements(GLenum mode, GLsizei count, GLenum type,
1749 const GLvoid *indices)
1750 {
1751 vbo_exec_DrawElements(mode, count, type, indices);
1752 }
1753
1754
1755 void GLAPIENTRY
1756 _mesa_DrawElementsBaseVertex(GLenum mode, GLsizei count, GLenum type,
1757 const GLvoid *indices, GLint basevertex)
1758 {
1759 vbo_exec_DrawElementsBaseVertex(mode, count, type, indices, basevertex);
1760 }
1761
1762
1763 void GLAPIENTRY
1764 _mesa_DrawRangeElements(GLenum mode, GLuint start, GLuint end, GLsizei count,
1765 GLenum type, const GLvoid *indices)
1766 {
1767 vbo_exec_DrawRangeElements(mode, start, end, count, type, indices);
1768 }
1769
1770
1771 void GLAPIENTRY
1772 _mesa_DrawRangeElementsBaseVertex(GLenum mode, GLuint start, GLuint end,
1773 GLsizei count, GLenum type,
1774 const GLvoid *indices, GLint basevertex)
1775 {
1776 vbo_exec_DrawRangeElementsBaseVertex(mode, start, end, count, type,
1777 indices, basevertex);
1778 }
1779
1780
1781 void GLAPIENTRY
1782 _mesa_MultiDrawElementsEXT(GLenum mode, const GLsizei *count, GLenum type,
1783 const GLvoid **indices, GLsizei primcount)
1784 {
1785 vbo_exec_MultiDrawElements(mode, count, type, indices, primcount);
1786 }
1787
1788
1789 void GLAPIENTRY
1790 _mesa_MultiDrawElementsBaseVertex(GLenum mode,
1791 const GLsizei *count, GLenum type,
1792 const GLvoid **indices, GLsizei primcount,
1793 const GLint *basevertex)
1794 {
1795 vbo_exec_MultiDrawElementsBaseVertex(mode, count, type, indices,
1796 primcount, basevertex);
1797 }
1798
1799 void GLAPIENTRY
1800 _mesa_DrawTransformFeedback(GLenum mode, GLuint name)
1801 {
1802 vbo_exec_DrawTransformFeedback(mode, name);
1803 }