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