1 /**************************************************************************
3 * Copyright 2003 VMware, Inc.
4 * Copyright 2009 VMware, Inc.
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:
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
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.
27 **************************************************************************/
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"
42 #include "vbo_context.h"
46 * Check that element 'j' of the array has reasonable data.
48 * For debugging purposes; not normally used.
51 check_array_data(struct gl_context
*ctx
, struct gl_vertex_array_object
*vao
,
52 GLuint attrib
, GLuint j
)
54 const struct gl_array_attributes
*array
= &vao
->VertexAttrib
[attrib
];
56 const struct gl_vertex_buffer_binding
*binding
=
57 &vao
->BufferBinding
[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
) {
63 bo
->Mappings
[MAP_INTERNAL
].Pointer
=
64 ctx
->Driver
.MapBufferRange(ctx
, 0, bo
->Size
,
65 GL_MAP_READ_BIT
, bo
, MAP_INTERNAL
);
67 data
= ADD_POINTERS(_mesa_vertex_attrib_address(array
, binding
),
68 bo
->Mappings
[MAP_INTERNAL
].Pointer
);
70 switch (array
->Type
) {
73 GLfloat
*f
= (GLfloat
*) ((GLubyte
*) data
+ binding
->Stride
* j
);
75 for (k
= 0; k
< array
->Size
; k
++) {
76 if (IS_INF_OR_NAN(f
[k
]) || f
[k
] >= 1.0e20F
|| f
[k
] <= -1.0e10F
) {
77 printf("Bad array data:\n");
78 printf(" Element[%u].%u = %f\n", j
, k
, f
[k
]);
79 printf(" Array %u at %p\n", attrib
, (void *) array
);
80 printf(" Type 0x%x, Size %d, Stride %d\n",
81 array
->Type
, array
->Size
, binding
->Stride
);
82 printf(" Address/offset %p in Buffer Object %u\n",
83 array
->Ptr
, bo
->Name
);
84 f
[k
] = 1.0F
; /* XXX replace the bad value! */
86 /*assert(!IS_INF_OR_NAN(f[k])); */
98 * Unmap the buffer object referenced by given array, if mapped.
101 unmap_array_buffer(struct gl_context
*ctx
, struct gl_vertex_array_object
*vao
,
104 const struct gl_array_attributes
*array
= &vao
->VertexAttrib
[attrib
];
105 if (array
->Enabled
) {
106 const struct gl_vertex_buffer_binding
*binding
=
107 &vao
->BufferBinding
[array
->BufferBindingIndex
];
108 struct gl_buffer_object
*bo
= binding
->BufferObj
;
109 if (_mesa_is_bufferobj(bo
) && _mesa_bufferobj_mapped(bo
, MAP_INTERNAL
)) {
110 ctx
->Driver
.UnmapBuffer(ctx
, bo
, MAP_INTERNAL
);
117 * Examine the array's data for NaNs, etc.
118 * For debug purposes; not normally used.
121 check_draw_elements_data(struct gl_context
*ctx
, GLsizei count
,
122 GLenum elemType
, const void *elements
,
125 struct gl_vertex_array_object
*vao
= ctx
->Array
.VAO
;
130 if (_mesa_is_bufferobj(vao
->IndexBufferObj
)) {
131 elemMap
= ctx
->Driver
.MapBufferRange(ctx
, 0,
132 vao
->IndexBufferObj
->Size
,
134 vao
->IndexBufferObj
, MAP_INTERNAL
);
135 elements
= ADD_POINTERS(elements
, elemMap
);
138 for (i
= 0; i
< count
; i
++) {
143 case GL_UNSIGNED_BYTE
:
144 j
= ((const GLubyte
*) elements
)[i
];
146 case GL_UNSIGNED_SHORT
:
147 j
= ((const GLushort
*) elements
)[i
];
149 case GL_UNSIGNED_INT
:
150 j
= ((const GLuint
*) elements
)[i
];
156 /* check element j of each enabled array */
157 for (k
= 0; k
< VERT_ATTRIB_MAX
; k
++) {
158 check_array_data(ctx
, vao
, k
, j
);
162 if (_mesa_is_bufferobj(vao
->IndexBufferObj
)) {
163 ctx
->Driver
.UnmapBuffer(ctx
, vao
->IndexBufferObj
, MAP_INTERNAL
);
166 for (k
= 0; k
< VERT_ATTRIB_MAX
; k
++) {
167 unmap_array_buffer(ctx
, vao
, k
);
173 * Check array data, looking for NaNs, etc.
176 check_draw_arrays_data(struct gl_context
*ctx
, GLint start
, GLsizei count
)
183 * Check if we should skip the draw call even after validation was successful.
186 skip_validated_draw(struct gl_context
*ctx
)
190 /* For ES2, we can draw if we have a vertex program/shader). */
191 return ctx
->VertexProgram
._Current
== NULL
;
194 /* For OpenGL ES, only draw if we have vertex positions
196 if (ctx
->Array
.VAO
->VertexAttrib
[VERT_ATTRIB_POS
].Enabled
)
200 case API_OPENGL_CORE
:
201 /* Section 7.3 (Program Objects) of the OpenGL 4.5 Core Profile spec
204 * "If there is no active program for the vertex or fragment shader
205 * stages, the results of vertex and/or fragment processing will be
206 * undefined. However, this is not an error."
208 * The fragment shader is not tested here because other state (e.g.,
209 * GL_RASTERIZER_DISCARD) affects whether or not we actually care.
211 return ctx
->VertexProgram
._Current
== NULL
;
213 case API_OPENGL_COMPAT
:
214 if (ctx
->VertexProgram
._Current
!= NULL
) {
215 /* Draw regardless of whether or not we have any vertex arrays.
216 * (Ex: could draw a point using a constant vertex pos)
220 /* Draw if we have vertex positions (GL_VERTEX_ARRAY or generic
223 return (!ctx
->Array
.VAO
->VertexAttrib
[VERT_ATTRIB_POS
].Enabled
&&
224 !ctx
->Array
.VAO
->VertexAttrib
[VERT_ATTRIB_GENERIC0
].Enabled
);
229 unreachable("Invalid API value in check_valid_to_render()");
237 * Print info/data for glDrawArrays(), for debugging.
240 print_draw_arrays(struct gl_context
*ctx
,
241 GLenum mode
, GLint start
, GLsizei count
)
243 const struct gl_vertex_array_object
*vao
= ctx
->Array
.VAO
;
245 printf("vbo_exec_DrawArrays(mode 0x%x, start %d, count %d):\n",
249 for (i
= 0; i
< VERT_ATTRIB_MAX
; ++i
) {
250 const struct gl_array_attributes
*array
= &vao
->VertexAttrib
[i
];
254 const struct gl_vertex_buffer_binding
*binding
=
255 &vao
->BufferBinding
[array
->BufferBindingIndex
];
256 struct gl_buffer_object
*bufObj
= binding
->BufferObj
;
258 printf("attr %s: size %d stride %d enabled %d "
259 "ptr %p Bufobj %u\n",
260 gl_vert_attrib_name((gl_vert_attrib
) i
),
261 array
->Size
, binding
->Stride
, array
->Enabled
,
262 array
->Ptr
, bufObj
->Name
);
264 if (_mesa_is_bufferobj(bufObj
)) {
265 GLubyte
*p
= ctx
->Driver
.MapBufferRange(ctx
, 0, bufObj
->Size
,
266 GL_MAP_READ_BIT
, bufObj
,
268 int offset
= (int) (GLintptr
)
269 _mesa_vertex_attrib_address(array
, binding
);
270 float *f
= (float *) (p
+ offset
);
273 int n
= (count
* binding
->Stride
) / 4;
276 printf(" Data at offset %d:\n", offset
);
277 for (i
= 0; i
< n
; i
++) {
278 printf(" float[%d] = 0x%08x %f\n", i
, k
[i
], f
[i
]);
280 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, MAP_INTERNAL
);
287 * Set the vbo->exec->inputs[] pointers to point to the enabled
288 * vertex arrays. This depends on the current vertex program/shader
289 * being executed because of whether or not generic vertex arrays
290 * alias the conventional vertex arrays.
291 * For arrays that aren't enabled, we set the input[attrib] pointer
292 * to point at a zero-stride current value "array".
295 recalculate_input_bindings(struct gl_context
*ctx
)
297 struct vbo_context
*vbo
= vbo_context(ctx
);
298 struct vbo_exec_context
*exec
= &vbo
->exec
;
299 const struct gl_array_attributes
*array
= ctx
->Array
.VAO
->VertexAttrib
;
300 struct gl_vertex_array
*vertexAttrib
= ctx
->Array
.VAO
->_VertexAttrib
;
301 const struct gl_vertex_array
**inputs
= &exec
->array
.inputs
[0];
302 GLbitfield64 const_inputs
= 0x0;
305 switch (get_program_mode(ctx
)) {
307 /* When no vertex program is active (or the vertex program is generated
308 * from fixed-function state). We put the material values into the
309 * generic slots. This is the only situation where material values
310 * are available as per-vertex attributes.
312 for (i
= 0; i
< VERT_ATTRIB_FF_MAX
; i
++) {
313 if (array
[VERT_ATTRIB_FF(i
)].Enabled
)
314 inputs
[i
] = &vertexAttrib
[VERT_ATTRIB_FF(i
)];
316 inputs
[i
] = &vbo
->currval
[VBO_ATTRIB_POS
+ i
];
317 const_inputs
|= VERT_BIT(i
);
321 for (i
= 0; i
< MAT_ATTRIB_MAX
; i
++) {
322 inputs
[VERT_ATTRIB_GENERIC(i
)] =
323 &vbo
->currval
[VBO_ATTRIB_MAT_FRONT_AMBIENT
+ i
];
324 const_inputs
|= VERT_BIT_GENERIC(i
);
327 /* Could use just about anything, just to fill in the empty
330 for (i
= MAT_ATTRIB_MAX
; i
< VERT_ATTRIB_GENERIC_MAX
; i
++) {
331 inputs
[VERT_ATTRIB_GENERIC(i
)] =
332 &vbo
->currval
[VBO_ATTRIB_GENERIC0
+ i
];
333 const_inputs
|= VERT_BIT_GENERIC(i
);
338 /* There are no shaders in OpenGL ES 1.x, so this code path should be
339 * impossible to reach. The meta code is careful to not use shaders in
342 assert(ctx
->API
!= API_OPENGLES
);
344 /* In the compatibility profile of desktop OpenGL, the generic[0]
345 * attribute array aliases and overrides the legacy position array.
346 * Otherwise, legacy attributes available in the legacy slots,
347 * generic attributes in the generic slots and materials are not
348 * available as per-vertex attributes.
350 * In all other APIs, only the generic attributes exist, and none of the
351 * slots are considered "magic."
353 if (ctx
->API
== API_OPENGL_COMPAT
) {
354 if (array
[VERT_ATTRIB_GENERIC0
].Enabled
)
355 inputs
[0] = &vertexAttrib
[VERT_ATTRIB_GENERIC0
];
356 else if (array
[VERT_ATTRIB_POS
].Enabled
)
357 inputs
[0] = &vertexAttrib
[VERT_ATTRIB_POS
];
359 inputs
[0] = &vbo
->currval
[VBO_ATTRIB_POS
];
360 const_inputs
|= VERT_BIT_POS
;
363 for (i
= 1; i
< VERT_ATTRIB_FF_MAX
; i
++) {
364 if (array
[VERT_ATTRIB_FF(i
)].Enabled
)
365 inputs
[i
] = &vertexAttrib
[VERT_ATTRIB_FF(i
)];
367 inputs
[i
] = &vbo
->currval
[VBO_ATTRIB_POS
+ i
];
368 const_inputs
|= VERT_BIT_FF(i
);
372 for (i
= 1; i
< VERT_ATTRIB_GENERIC_MAX
; i
++) {
373 if (array
[VERT_ATTRIB_GENERIC(i
)].Enabled
)
374 inputs
[VERT_ATTRIB_GENERIC(i
)] =
375 &vertexAttrib
[VERT_ATTRIB_GENERIC(i
)];
377 inputs
[VERT_ATTRIB_GENERIC(i
)] =
378 &vbo
->currval
[VBO_ATTRIB_GENERIC0
+ i
];
379 const_inputs
|= VERT_BIT_GENERIC(i
);
383 inputs
[VERT_ATTRIB_GENERIC0
] = inputs
[0];
385 /* Other parts of the code assume that inputs[0] through
386 * inputs[VERT_ATTRIB_FF_MAX] will be non-NULL. However, in OpenGL
387 * ES 2.0+ or OpenGL core profile, none of these arrays should ever
390 for (i
= 0; i
< VERT_ATTRIB_FF_MAX
; i
++) {
391 assert(!array
[VERT_ATTRIB_FF(i
)].Enabled
);
393 inputs
[i
] = &vbo
->currval
[VBO_ATTRIB_POS
+ i
];
394 const_inputs
|= VERT_BIT_FF(i
);
397 for (i
= 0; i
< VERT_ATTRIB_GENERIC_MAX
; i
++) {
398 if (array
[VERT_ATTRIB_GENERIC(i
)].Enabled
)
399 inputs
[VERT_ATTRIB_GENERIC(i
)] =
400 &vertexAttrib
[VERT_ATTRIB_GENERIC(i
)];
402 inputs
[VERT_ATTRIB_GENERIC(i
)] =
403 &vbo
->currval
[VBO_ATTRIB_GENERIC0
+ i
];
404 const_inputs
|= VERT_BIT_GENERIC(i
);
412 _mesa_set_varying_vp_inputs(ctx
, VERT_BIT_ALL
& (~const_inputs
));
413 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewArray
;
418 * Examine the enabled vertex arrays to set the exec->array.inputs[] values.
419 * These will point to the arrays to actually use for drawing. Some will
420 * be user-provided arrays, other will be zero-stride const-valued arrays.
421 * Note that this might set the _NEW_VARYING_VP_INPUTS dirty flag so state
422 * validation must be done after this call.
425 vbo_bind_arrays(struct gl_context
*ctx
)
427 struct vbo_context
*vbo
= vbo_context(ctx
);
428 struct vbo_exec_context
*exec
= &vbo
->exec
;
430 vbo_draw_method(vbo
, DRAW_ARRAYS
);
432 if (exec
->array
.recalculate_inputs
) {
433 recalculate_input_bindings(ctx
);
434 exec
->array
.recalculate_inputs
= GL_FALSE
;
436 /* Again... because we may have changed the bitmask of per-vertex varying
437 * attributes. If we regenerate the fixed-function vertex program now
438 * we may be able to prune down the number of vertex attributes which we
439 * need in the shader.
442 /* Setting "validating" to TRUE prevents _mesa_update_state from
443 * invalidating what we just did.
445 exec
->validating
= GL_TRUE
;
446 _mesa_update_state(ctx
);
447 exec
->validating
= GL_FALSE
;
454 * Helper function called by the other DrawArrays() functions below.
455 * This is where we handle primitive restart for drawing non-indexed
456 * arrays. If primitive restart is enabled, it typically means
457 * splitting one DrawArrays() into two.
460 vbo_draw_arrays(struct gl_context
*ctx
, GLenum mode
, GLint start
,
461 GLsizei count
, GLuint numInstances
, GLuint baseInstance
,
464 struct vbo_context
*vbo
= vbo_context(ctx
);
465 struct _mesa_prim prim
;
467 if (skip_validated_draw(ctx
))
470 vbo_bind_arrays(ctx
);
472 /* OpenGL 4.5 says that primitive restart is ignored with non-indexed
475 memset(&prim
, 0, sizeof(prim
));
479 prim
.num_instances
= numInstances
;
480 prim
.base_instance
= baseInstance
;
481 prim
.draw_id
= drawID
;
482 prim
.is_indirect
= 0;
486 vbo
->draw_prims(ctx
, &prim
, 1, NULL
,
487 GL_TRUE
, start
, start
+ count
- 1, NULL
, 0, NULL
);
489 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
) {
496 * Execute a glRectf() function.
498 static void GLAPIENTRY
499 vbo_exec_Rectf(GLfloat x1
, GLfloat y1
, GLfloat x2
, GLfloat y2
)
501 GET_CURRENT_CONTEXT(ctx
);
502 ASSERT_OUTSIDE_BEGIN_END(ctx
);
504 CALL_Begin(GET_DISPATCH(), (GL_QUADS
));
505 CALL_Vertex2f(GET_DISPATCH(), (x1
, y1
));
506 CALL_Vertex2f(GET_DISPATCH(), (x2
, y1
));
507 CALL_Vertex2f(GET_DISPATCH(), (x2
, y2
));
508 CALL_Vertex2f(GET_DISPATCH(), (x1
, y2
));
509 CALL_End(GET_DISPATCH(), ());
513 static void GLAPIENTRY
514 vbo_exec_EvalMesh1(GLenum mode
, GLint i1
, GLint i2
)
516 GET_CURRENT_CONTEXT(ctx
);
526 prim
= GL_LINE_STRIP
;
529 _mesa_error(ctx
, GL_INVALID_ENUM
, "glEvalMesh1(mode)");
533 /* No effect if vertex maps disabled.
535 if (!ctx
->Eval
.Map1Vertex4
&& !ctx
->Eval
.Map1Vertex3
)
538 du
= ctx
->Eval
.MapGrid1du
;
539 u
= ctx
->Eval
.MapGrid1u1
+ i1
* du
;
541 CALL_Begin(GET_DISPATCH(), (prim
));
542 for (i
= i1
; i
<= i2
; i
++, u
+= du
) {
543 CALL_EvalCoord1f(GET_DISPATCH(), (u
));
545 CALL_End(GET_DISPATCH(), ());
549 static void GLAPIENTRY
550 vbo_exec_EvalMesh2(GLenum mode
, GLint i1
, GLint i2
, GLint j1
, GLint j2
)
552 GET_CURRENT_CONTEXT(ctx
);
553 GLfloat u
, du
, v
, dv
, v1
, u1
;
562 _mesa_error(ctx
, GL_INVALID_ENUM
, "glEvalMesh2(mode)");
566 /* No effect if vertex maps disabled.
568 if (!ctx
->Eval
.Map2Vertex4
&& !ctx
->Eval
.Map2Vertex3
)
571 du
= ctx
->Eval
.MapGrid2du
;
572 dv
= ctx
->Eval
.MapGrid2dv
;
573 v1
= ctx
->Eval
.MapGrid2v1
+ j1
* dv
;
574 u1
= ctx
->Eval
.MapGrid2u1
+ i1
* du
;
578 CALL_Begin(GET_DISPATCH(), (GL_POINTS
));
579 for (v
= v1
, j
= j1
; j
<= j2
; j
++, v
+= dv
) {
580 for (u
= u1
, i
= i1
; i
<= i2
; i
++, u
+= du
) {
581 CALL_EvalCoord2f(GET_DISPATCH(), (u
, v
));
584 CALL_End(GET_DISPATCH(), ());
587 for (v
= v1
, j
= j1
; j
<= j2
; j
++, v
+= dv
) {
588 CALL_Begin(GET_DISPATCH(), (GL_LINE_STRIP
));
589 for (u
= u1
, i
= i1
; i
<= i2
; i
++, u
+= du
) {
590 CALL_EvalCoord2f(GET_DISPATCH(), (u
, v
));
592 CALL_End(GET_DISPATCH(), ());
594 for (u
= u1
, i
= i1
; i
<= i2
; i
++, u
+= du
) {
595 CALL_Begin(GET_DISPATCH(), (GL_LINE_STRIP
));
596 for (v
= v1
, j
= j1
; j
<= j2
; j
++, v
+= dv
) {
597 CALL_EvalCoord2f(GET_DISPATCH(), (u
, v
));
599 CALL_End(GET_DISPATCH(), ());
603 for (v
= v1
, j
= j1
; j
< j2
; j
++, v
+= dv
) {
604 CALL_Begin(GET_DISPATCH(), (GL_TRIANGLE_STRIP
));
605 for (u
= u1
, i
= i1
; i
<= i2
; i
++, u
+= du
) {
606 CALL_EvalCoord2f(GET_DISPATCH(), (u
, v
));
607 CALL_EvalCoord2f(GET_DISPATCH(), (u
, v
+ dv
));
609 CALL_End(GET_DISPATCH(), ());
617 * Called from glDrawArrays when in immediate mode (not display list mode).
619 static void GLAPIENTRY
620 vbo_exec_DrawArrays(GLenum mode
, GLint start
, GLsizei count
)
622 GET_CURRENT_CONTEXT(ctx
);
624 if (MESA_VERBOSE
& VERBOSE_DRAW
)
625 _mesa_debug(ctx
, "glDrawArrays(%s, %d, %d)\n",
626 _mesa_enum_to_string(mode
), start
, count
);
628 if (_mesa_is_no_error_enabled(ctx
)) {
629 FLUSH_CURRENT(ctx
, 0);
632 _mesa_update_state(ctx
);
634 if (!_mesa_validate_DrawArrays(ctx
, mode
, count
))
639 check_draw_arrays_data(ctx
, start
, count
);
641 vbo_draw_arrays(ctx
, mode
, start
, count
, 1, 0, 0);
644 print_draw_arrays(ctx
, mode
, start
, count
);
649 * Called from glDrawArraysInstanced when in immediate mode (not
650 * display list mode).
652 static void GLAPIENTRY
653 vbo_exec_DrawArraysInstanced(GLenum mode
, GLint start
, GLsizei count
,
654 GLsizei numInstances
)
656 GET_CURRENT_CONTEXT(ctx
);
658 if (MESA_VERBOSE
& VERBOSE_DRAW
)
659 _mesa_debug(ctx
, "glDrawArraysInstanced(%s, %d, %d, %d)\n",
660 _mesa_enum_to_string(mode
), start
, count
, numInstances
);
663 if (_mesa_is_no_error_enabled(ctx
)) {
664 FLUSH_CURRENT(ctx
, 0);
667 _mesa_update_state(ctx
);
669 if (!_mesa_validate_DrawArraysInstanced(ctx
, mode
, start
, count
,
675 check_draw_arrays_data(ctx
, start
, count
);
677 vbo_draw_arrays(ctx
, mode
, start
, count
, numInstances
, 0, 0);
680 print_draw_arrays(ctx
, mode
, start
, count
);
685 * Called from glDrawArraysInstancedBaseInstance when in immediate mode.
687 static void GLAPIENTRY
688 vbo_exec_DrawArraysInstancedBaseInstance(GLenum mode
, GLint first
,
689 GLsizei count
, GLsizei numInstances
,
692 GET_CURRENT_CONTEXT(ctx
);
694 if (MESA_VERBOSE
& VERBOSE_DRAW
)
696 "glDrawArraysInstancedBaseInstance(%s, %d, %d, %d, %d)\n",
697 _mesa_enum_to_string(mode
), first
, count
,
698 numInstances
, baseInstance
);
700 if (_mesa_is_no_error_enabled(ctx
)) {
701 FLUSH_CURRENT(ctx
, 0);
704 _mesa_update_state(ctx
);
706 if (!_mesa_validate_DrawArraysInstanced(ctx
, mode
, first
, count
,
712 check_draw_arrays_data(ctx
, first
, count
);
714 vbo_draw_arrays(ctx
, mode
, first
, count
, numInstances
, baseInstance
, 0);
717 print_draw_arrays(ctx
, mode
, first
, count
);
722 * Called from glMultiDrawArrays when in immediate mode.
724 static void GLAPIENTRY
725 vbo_exec_MultiDrawArrays(GLenum mode
, const GLint
*first
,
726 const GLsizei
*count
, GLsizei primcount
)
728 GET_CURRENT_CONTEXT(ctx
);
731 if (MESA_VERBOSE
& VERBOSE_DRAW
)
733 "glMultiDrawArrays(%s, %p, %p, %d)\n",
734 _mesa_enum_to_string(mode
), first
, count
, primcount
);
736 if (!_mesa_validate_MultiDrawArrays(ctx
, mode
, count
, primcount
))
739 for (i
= 0; i
< primcount
; i
++) {
742 check_draw_arrays_data(ctx
, first
[i
], count
[i
]);
744 /* The GL_ARB_shader_draw_parameters spec adds the following after the
745 * pseudo-code describing glMultiDrawArrays:
747 * "The index of the draw (<i> in the above pseudo-code) may be
748 * read by a vertex shader as <gl_DrawIDARB>, as described in
751 vbo_draw_arrays(ctx
, mode
, first
[i
], count
[i
], 1, 0, i
);
754 print_draw_arrays(ctx
, mode
, first
[i
], count
[i
]);
762 * Map GL_ELEMENT_ARRAY_BUFFER and print contents.
767 dump_element_buffer(struct gl_context
*ctx
, GLenum type
)
770 ctx
->Driver
.MapBufferRange(ctx
, 0,
771 ctx
->Array
.VAO
->IndexBufferObj
->Size
,
773 ctx
->Array
.VAO
->IndexBufferObj
,
776 case GL_UNSIGNED_BYTE
:
778 const GLubyte
*us
= (const GLubyte
*) map
;
780 for (i
= 0; i
< ctx
->Array
.VAO
->IndexBufferObj
->Size
; i
++) {
781 printf("%02x ", us
[i
]);
788 case GL_UNSIGNED_SHORT
:
790 const GLushort
*us
= (const GLushort
*) map
;
792 for (i
= 0; i
< ctx
->Array
.VAO
->IndexBufferObj
->Size
/ 2; i
++) {
793 printf("%04x ", us
[i
]);
800 case GL_UNSIGNED_INT
:
802 const GLuint
*us
= (const GLuint
*) map
;
804 for (i
= 0; i
< ctx
->Array
.VAO
->IndexBufferObj
->Size
/ 4; i
++) {
805 printf("%08x ", us
[i
]);
816 ctx
->Driver
.UnmapBuffer(ctx
, ctx
->Array
.VAO
->IndexBufferObj
, MAP_INTERNAL
);
822 skip_draw_elements(struct gl_context
*ctx
, GLsizei count
,
823 const GLvoid
*indices
)
828 /* Not using a VBO for indices, so avoid NULL pointer derefs later.
830 if (!_mesa_is_bufferobj(ctx
->Array
.VAO
->IndexBufferObj
) && indices
== NULL
)
833 if (skip_validated_draw(ctx
))
841 * Inner support for both _mesa_DrawElements and _mesa_DrawRangeElements.
842 * Do the rendering for a glDrawElements or glDrawRangeElements call after
843 * we've validated buffer bounds, etc.
846 vbo_validated_drawrangeelements(struct gl_context
*ctx
, GLenum mode
,
847 GLboolean index_bounds_valid
,
848 GLuint start
, GLuint end
,
849 GLsizei count
, GLenum type
,
850 const GLvoid
* indices
,
851 GLint basevertex
, GLuint numInstances
,
854 struct vbo_context
*vbo
= vbo_context(ctx
);
855 struct _mesa_index_buffer ib
;
856 struct _mesa_prim prim
;
858 if (!index_bounds_valid
) {
863 if (skip_draw_elements(ctx
, count
, indices
))
866 vbo_bind_arrays(ctx
);
869 ib
.index_size
= vbo_sizeof_ib_type(type
);
870 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
881 prim
.is_indirect
= 0;
882 prim
.basevertex
= basevertex
;
883 prim
.num_instances
= numInstances
;
884 prim
.base_instance
= baseInstance
;
887 /* Need to give special consideration to rendering a range of
888 * indices starting somewhere above zero. Typically the
889 * application is issuing multiple DrawRangeElements() to draw
890 * successive primitives layed out linearly in the vertex arrays.
891 * Unless the vertex arrays are all in a VBO (or locked as with
892 * CVA), the OpenGL semantics imply that we need to re-read or
893 * re-upload the vertex data on each draw call.
895 * In the case of hardware tnl, we want to avoid starting the
896 * upload at zero, as it will mean every draw call uploads an
897 * increasing amount of not-used vertex data. Worse - in the
898 * software tnl module, all those vertices might be transformed and
899 * lit but never rendered.
901 * If we just upload or transform the vertices in start..end,
902 * however, the indices will be incorrect.
904 * At this level, we don't know exactly what the requirements of
905 * the backend are going to be, though it will likely boil down to
908 * 1) Do nothing, everything is in a VBO and is processed once
911 * 2) Adjust the indices and vertex arrays so that start becomes
914 * Rather than doing anything here, I'll provide a helper function
915 * for the latter case elsewhere.
918 vbo
->draw_prims(ctx
, &prim
, 1, &ib
,
919 index_bounds_valid
, start
, end
, NULL
, 0, NULL
);
921 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
) {
928 * Called by glDrawRangeElementsBaseVertex() in immediate mode.
930 static void GLAPIENTRY
931 vbo_exec_DrawRangeElementsBaseVertex(GLenum mode
, GLuint start
, GLuint end
,
932 GLsizei count
, GLenum type
,
933 const GLvoid
* indices
, GLint basevertex
)
935 static GLuint warnCount
= 0;
936 GLboolean index_bounds_valid
= GL_TRUE
;
938 /* This is only useful to catch invalid values in the "end" parameter
941 GLuint max_element
= 2 * 1000 * 1000 * 1000; /* just a big number */
943 GET_CURRENT_CONTEXT(ctx
);
945 if (MESA_VERBOSE
& VERBOSE_DRAW
)
947 "glDrawRangeElementsBaseVertex(%s, %u, %u, %d, %s, %p, %d)\n",
948 _mesa_enum_to_string(mode
), start
, end
, count
,
949 _mesa_enum_to_string(type
), indices
, basevertex
);
951 if (!_mesa_validate_DrawRangeElements(ctx
, mode
, start
, end
, count
,
955 if ((int) end
+ basevertex
< 0 || start
+ basevertex
>= max_element
) {
956 /* The application requested we draw using a range of indices that's
957 * outside the bounds of the current VBO. This is invalid and appears
958 * to give undefined results. The safest thing to do is to simply
959 * ignore the range, in case the application botched their range tracking
960 * but did provide valid indices. Also issue a warning indicating that
961 * the application is broken.
963 if (warnCount
++ < 10) {
964 _mesa_warning(ctx
, "glDrawRangeElements(start %u, end %u, "
965 "basevertex %d, count %d, type 0x%x, indices=%p):\n"
966 "\trange is outside VBO bounds (max=%u); ignoring.\n"
967 "\tThis should be fixed in the application.",
968 start
, end
, basevertex
, count
, type
, indices
,
971 index_bounds_valid
= GL_FALSE
;
974 /* NOTE: It's important that 'end' is a reasonable value.
975 * in _tnl_draw_prims(), we use end to determine how many vertices
976 * to transform. If it's too large, we can unnecessarily split prims
977 * or we can read/write out of memory in several different places!
980 /* Catch/fix some potential user errors */
981 if (type
== GL_UNSIGNED_BYTE
) {
982 start
= MIN2(start
, 0xff);
983 end
= MIN2(end
, 0xff);
985 else if (type
== GL_UNSIGNED_SHORT
) {
986 start
= MIN2(start
, 0xffff);
987 end
= MIN2(end
, 0xffff);
991 printf("glDraw[Range]Elements{,BaseVertex}"
992 "(start %u, end %u, type 0x%x, count %d) ElemBuf %u, "
994 start
, end
, type
, count
,
995 ctx
->Array
.VAO
->IndexBufferObj
->Name
, basevertex
);
998 if ((int) start
+ basevertex
< 0 || end
+ basevertex
>= max_element
)
999 index_bounds_valid
= GL_FALSE
;
1002 check_draw_elements_data(ctx
, count
, type
, indices
, basevertex
);
1004 (void) check_draw_elements_data
;
1007 if (!index_bounds_valid
) {
1012 vbo_validated_drawrangeelements(ctx
, mode
, index_bounds_valid
, start
, end
,
1013 count
, type
, indices
, basevertex
, 1, 0);
1018 * Called by glDrawRangeElements() in immediate mode.
1020 static void GLAPIENTRY
1021 vbo_exec_DrawRangeElements(GLenum mode
, GLuint start
, GLuint end
,
1022 GLsizei count
, GLenum type
, const GLvoid
* indices
)
1024 if (MESA_VERBOSE
& VERBOSE_DRAW
) {
1025 GET_CURRENT_CONTEXT(ctx
);
1027 "glDrawRangeElements(%s, %u, %u, %d, %s, %p)\n",
1028 _mesa_enum_to_string(mode
), start
, end
, count
,
1029 _mesa_enum_to_string(type
), indices
);
1032 vbo_exec_DrawRangeElementsBaseVertex(mode
, start
, end
, count
, type
,
1038 * Called by glDrawElements() in immediate mode.
1040 static void GLAPIENTRY
1041 vbo_exec_DrawElements(GLenum mode
, GLsizei count
, GLenum type
,
1042 const GLvoid
* indices
)
1044 GET_CURRENT_CONTEXT(ctx
);
1046 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1047 _mesa_debug(ctx
, "glDrawElements(%s, %u, %s, %p)\n",
1048 _mesa_enum_to_string(mode
), count
,
1049 _mesa_enum_to_string(type
), indices
);
1051 if (_mesa_is_no_error_enabled(ctx
)) {
1052 FLUSH_CURRENT(ctx
, 0);
1055 _mesa_update_state(ctx
);
1057 if (!_mesa_validate_DrawElements(ctx
, mode
, count
, type
, indices
))
1061 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, 0, ~0,
1062 count
, type
, indices
, 0, 1, 0);
1067 * Called by glDrawElementsBaseVertex() in immediate mode.
1069 static void GLAPIENTRY
1070 vbo_exec_DrawElementsBaseVertex(GLenum mode
, GLsizei count
, GLenum type
,
1071 const GLvoid
* indices
, GLint basevertex
)
1073 GET_CURRENT_CONTEXT(ctx
);
1075 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1076 _mesa_debug(ctx
, "glDrawElements(%s, %u, %s, %p)\n",
1077 _mesa_enum_to_string(mode
), count
,
1078 _mesa_enum_to_string(type
), indices
);
1080 if (_mesa_is_no_error_enabled(ctx
)) {
1081 FLUSH_CURRENT(ctx
, 0);
1084 _mesa_update_state(ctx
);
1086 if (!_mesa_validate_DrawElements(ctx
, mode
, count
, type
, indices
))
1090 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, 0, ~0,
1091 count
, type
, indices
, basevertex
, 1, 0);
1096 * Called by glDrawElementsInstanced() in immediate mode.
1098 static void GLAPIENTRY
1099 vbo_exec_DrawElementsInstanced(GLenum mode
, GLsizei count
, GLenum type
,
1100 const GLvoid
* indices
, GLsizei numInstances
)
1102 GET_CURRENT_CONTEXT(ctx
);
1104 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1105 _mesa_debug(ctx
, "glDrawElements(%s, %u, %s, %p)\n",
1106 _mesa_enum_to_string(mode
), count
,
1107 _mesa_enum_to_string(type
), indices
);
1109 if (_mesa_is_no_error_enabled(ctx
)) {
1110 FLUSH_CURRENT(ctx
, 0);
1113 _mesa_update_state(ctx
);
1115 if (!_mesa_validate_DrawElementsInstanced(ctx
, mode
, count
, type
,
1116 indices
, numInstances
))
1120 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, 0, ~0,
1121 count
, type
, indices
, 0, numInstances
, 0);
1126 * Called by glDrawElementsInstancedBaseVertex() in immediate mode.
1128 static void GLAPIENTRY
1129 vbo_exec_DrawElementsInstancedBaseVertex(GLenum mode
, GLsizei count
,
1130 GLenum type
, const GLvoid
* indices
,
1131 GLsizei numInstances
,
1134 GET_CURRENT_CONTEXT(ctx
);
1136 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1138 "glDrawElementsInstancedBaseVertex"
1139 "(%s, %d, %s, %p, %d; %d)\n",
1140 _mesa_enum_to_string(mode
), count
,
1141 _mesa_enum_to_string(type
), indices
,
1142 numInstances
, basevertex
);
1144 if (_mesa_is_no_error_enabled(ctx
)) {
1145 FLUSH_CURRENT(ctx
, 0);
1148 _mesa_update_state(ctx
);
1150 if (!_mesa_validate_DrawElementsInstanced(ctx
, mode
, count
, type
,
1151 indices
, numInstances
))
1155 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, 0, ~0,
1156 count
, type
, indices
,
1157 basevertex
, numInstances
, 0);
1162 * Called by glDrawElementsInstancedBaseInstance() in immediate mode.
1164 static void GLAPIENTRY
1165 vbo_exec_DrawElementsInstancedBaseInstance(GLenum mode
, GLsizei count
,
1167 const GLvoid
*indices
,
1168 GLsizei numInstances
,
1169 GLuint baseInstance
)
1171 GET_CURRENT_CONTEXT(ctx
);
1173 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1175 "glDrawElementsInstancedBaseInstance"
1176 "(%s, %d, %s, %p, %d, %d)\n",
1177 _mesa_enum_to_string(mode
), count
,
1178 _mesa_enum_to_string(type
), indices
,
1179 numInstances
, baseInstance
);
1181 if (_mesa_is_no_error_enabled(ctx
)) {
1182 FLUSH_CURRENT(ctx
, 0);
1185 _mesa_update_state(ctx
);
1187 if (!_mesa_validate_DrawElementsInstanced(ctx
, mode
, count
, type
,
1188 indices
, numInstances
))
1192 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, 0, ~0,
1193 count
, type
, indices
, 0, numInstances
,
1199 * Called by glDrawElementsInstancedBaseVertexBaseInstance() in immediate mode.
1201 static void GLAPIENTRY
1202 vbo_exec_DrawElementsInstancedBaseVertexBaseInstance(GLenum mode
,
1205 const GLvoid
*indices
,
1206 GLsizei numInstances
,
1208 GLuint baseInstance
)
1210 GET_CURRENT_CONTEXT(ctx
);
1212 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1214 "glDrawElementsInstancedBaseVertexBaseInstance"
1215 "(%s, %d, %s, %p, %d, %d, %d)\n",
1216 _mesa_enum_to_string(mode
), count
,
1217 _mesa_enum_to_string(type
), indices
,
1218 numInstances
, basevertex
, baseInstance
);
1220 if (_mesa_is_no_error_enabled(ctx
)) {
1221 FLUSH_CURRENT(ctx
, 0);
1224 _mesa_update_state(ctx
);
1226 if (!_mesa_validate_DrawElementsInstanced(ctx
, mode
, count
, type
,
1227 indices
, numInstances
))
1231 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, 0, ~0,
1232 count
, type
, indices
, basevertex
,
1233 numInstances
, baseInstance
);
1238 * Inner support for both _mesa_MultiDrawElements() and
1239 * _mesa_MultiDrawRangeElements().
1240 * This does the actual rendering after we've checked array indexes, etc.
1243 vbo_validated_multidrawelements(struct gl_context
*ctx
, GLenum mode
,
1244 const GLsizei
*count
, GLenum type
,
1245 const GLvoid
* const *indices
,
1246 GLsizei primcount
, const GLint
*basevertex
)
1248 struct vbo_context
*vbo
= vbo_context(ctx
);
1249 struct _mesa_index_buffer ib
;
1250 struct _mesa_prim
*prim
;
1251 unsigned int index_type_size
= vbo_sizeof_ib_type(type
);
1252 uintptr_t min_index_ptr
, max_index_ptr
;
1253 GLboolean fallback
= GL_FALSE
;
1259 prim
= calloc(primcount
, sizeof(*prim
));
1261 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glMultiDrawElements");
1265 vbo_bind_arrays(ctx
);
1267 min_index_ptr
= (uintptr_t) indices
[0];
1269 for (i
= 0; i
< primcount
; i
++) {
1270 min_index_ptr
= MIN2(min_index_ptr
, (uintptr_t) indices
[i
]);
1271 max_index_ptr
= MAX2(max_index_ptr
, (uintptr_t) indices
[i
] +
1272 index_type_size
* count
[i
]);
1275 /* Check if we can handle this thing as a bunch of index offsets from the
1276 * same index pointer. If we can't, then we have to fall back to doing
1277 * a draw_prims per primitive.
1278 * Check that the difference between each prim's indexes is a multiple of
1279 * the index/element size.
1281 if (index_type_size
!= 1) {
1282 for (i
= 0; i
< primcount
; i
++) {
1283 if ((((uintptr_t) indices
[i
] - min_index_ptr
) % index_type_size
) !=
1291 /* Draw primitives individually if one count is zero, so we can easily skip
1294 for (i
= 0; i
< primcount
; i
++) {
1295 if (count
[i
] == 0) {
1301 /* If the index buffer isn't in a VBO, then treating the application's
1302 * subranges of the index buffer as one large index buffer may lead to
1303 * us reading unmapped memory.
1305 if (!_mesa_is_bufferobj(ctx
->Array
.VAO
->IndexBufferObj
))
1309 ib
.count
= (max_index_ptr
- min_index_ptr
) / index_type_size
;
1310 ib
.index_size
= vbo_sizeof_ib_type(type
);
1311 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
1312 ib
.ptr
= (void *) min_index_ptr
;
1314 for (i
= 0; i
< primcount
; i
++) {
1315 prim
[i
].begin
= (i
== 0);
1316 prim
[i
].end
= (i
== primcount
- 1);
1319 prim
[i
].mode
= mode
;
1321 ((uintptr_t) indices
[i
] - min_index_ptr
) / index_type_size
;
1322 prim
[i
].count
= count
[i
];
1323 prim
[i
].indexed
= 1;
1324 prim
[i
].num_instances
= 1;
1325 prim
[i
].base_instance
= 0;
1326 prim
[i
].draw_id
= i
;
1327 prim
[i
].is_indirect
= 0;
1328 if (basevertex
!= NULL
)
1329 prim
[i
].basevertex
= basevertex
[i
];
1331 prim
[i
].basevertex
= 0;
1334 vbo
->draw_prims(ctx
, prim
, primcount
, &ib
,
1335 false, 0, ~0, NULL
, 0, NULL
);
1338 /* render one prim at a time */
1339 for (i
= 0; i
< primcount
; i
++) {
1342 ib
.count
= count
[i
];
1343 ib
.index_size
= vbo_sizeof_ib_type(type
);
1344 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
1345 ib
.ptr
= indices
[i
];
1351 prim
[0].mode
= mode
;
1353 prim
[0].count
= count
[i
];
1354 prim
[0].indexed
= 1;
1355 prim
[0].num_instances
= 1;
1356 prim
[0].base_instance
= 0;
1357 prim
[0].draw_id
= i
;
1358 prim
[0].is_indirect
= 0;
1359 if (basevertex
!= NULL
)
1360 prim
[0].basevertex
= basevertex
[i
];
1362 prim
[0].basevertex
= 0;
1364 vbo
->draw_prims(ctx
, prim
, 1, &ib
, false, 0, ~0, NULL
, 0, NULL
);
1370 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
) {
1376 static void GLAPIENTRY
1377 vbo_exec_MultiDrawElements(GLenum mode
,
1378 const GLsizei
*count
, GLenum type
,
1379 const GLvoid
* const *indices
, GLsizei primcount
)
1381 GET_CURRENT_CONTEXT(ctx
);
1383 if (!_mesa_validate_MultiDrawElements(ctx
, mode
, count
, type
, indices
,
1387 if (skip_validated_draw(ctx
))
1390 vbo_validated_multidrawelements(ctx
, mode
, count
, type
, indices
, primcount
,
1395 static void GLAPIENTRY
1396 vbo_exec_MultiDrawElementsBaseVertex(GLenum mode
,
1397 const GLsizei
*count
, GLenum type
,
1398 const GLvoid
* const *indices
,
1400 const GLsizei
*basevertex
)
1402 GET_CURRENT_CONTEXT(ctx
);
1404 if (!_mesa_validate_MultiDrawElements(ctx
, mode
, count
, type
, indices
,
1408 if (skip_validated_draw(ctx
))
1411 vbo_validated_multidrawelements(ctx
, mode
, count
, type
, indices
, primcount
,
1417 vbo_draw_transform_feedback(struct gl_context
*ctx
, GLenum mode
,
1418 struct gl_transform_feedback_object
*obj
,
1419 GLuint stream
, GLuint numInstances
)
1421 struct vbo_context
*vbo
= vbo_context(ctx
);
1422 struct _mesa_prim prim
;
1424 if (!_mesa_validate_DrawTransformFeedback(ctx
, mode
, obj
, stream
,
1429 if (ctx
->Driver
.GetTransformFeedbackVertexCount
&&
1430 (ctx
->Const
.AlwaysUseGetTransformFeedbackVertexCount
||
1431 !_mesa_all_varyings_in_vbos(ctx
->Array
.VAO
))) {
1433 ctx
->Driver
.GetTransformFeedbackVertexCount(ctx
, obj
, stream
);
1434 vbo_draw_arrays(ctx
, mode
, 0, n
, numInstances
, 0, 0);
1438 if (skip_validated_draw(ctx
))
1441 vbo_bind_arrays(ctx
);
1443 /* init most fields to zero */
1444 memset(&prim
, 0, sizeof(prim
));
1448 prim
.num_instances
= numInstances
;
1449 prim
.base_instance
= 0;
1450 prim
.is_indirect
= 0;
1452 /* Maybe we should do some primitive splitting for primitive restart
1453 * (like in DrawArrays), but we have no way to know how many vertices
1454 * will be rendered. */
1456 vbo
->draw_prims(ctx
, &prim
, 1, NULL
, GL_FALSE
, 0, ~0, obj
, stream
, NULL
);
1458 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
) {
1465 * Like DrawArrays, but take the count from a transform feedback object.
1466 * \param mode GL_POINTS, GL_LINES, GL_TRIANGLE_STRIP, etc.
1467 * \param name the transform feedback object
1468 * User still has to setup of the vertex attribute info with
1469 * glVertexPointer, glColorPointer, etc.
1470 * Part of GL_ARB_transform_feedback2.
1472 static void GLAPIENTRY
1473 vbo_exec_DrawTransformFeedback(GLenum mode
, GLuint name
)
1475 GET_CURRENT_CONTEXT(ctx
);
1476 struct gl_transform_feedback_object
*obj
=
1477 _mesa_lookup_transform_feedback_object(ctx
, name
);
1479 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1480 _mesa_debug(ctx
, "glDrawTransformFeedback(%s, %d)\n",
1481 _mesa_enum_to_string(mode
), name
);
1483 vbo_draw_transform_feedback(ctx
, mode
, obj
, 0, 1);
1487 static void GLAPIENTRY
1488 vbo_exec_DrawTransformFeedbackStream(GLenum mode
, GLuint name
, GLuint stream
)
1490 GET_CURRENT_CONTEXT(ctx
);
1491 struct gl_transform_feedback_object
*obj
=
1492 _mesa_lookup_transform_feedback_object(ctx
, name
);
1494 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1495 _mesa_debug(ctx
, "glDrawTransformFeedbackStream(%s, %u, %u)\n",
1496 _mesa_enum_to_string(mode
), name
, stream
);
1498 vbo_draw_transform_feedback(ctx
, mode
, obj
, stream
, 1);
1502 static void GLAPIENTRY
1503 vbo_exec_DrawTransformFeedbackInstanced(GLenum mode
, GLuint name
,
1506 GET_CURRENT_CONTEXT(ctx
);
1507 struct gl_transform_feedback_object
*obj
=
1508 _mesa_lookup_transform_feedback_object(ctx
, name
);
1510 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1511 _mesa_debug(ctx
, "glDrawTransformFeedbackInstanced(%s, %d)\n",
1512 _mesa_enum_to_string(mode
), name
);
1514 vbo_draw_transform_feedback(ctx
, mode
, obj
, 0, primcount
);
1518 static void GLAPIENTRY
1519 vbo_exec_DrawTransformFeedbackStreamInstanced(GLenum mode
, GLuint name
,
1523 GET_CURRENT_CONTEXT(ctx
);
1524 struct gl_transform_feedback_object
*obj
=
1525 _mesa_lookup_transform_feedback_object(ctx
, name
);
1527 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1528 _mesa_debug(ctx
, "glDrawTransformFeedbackStreamInstanced"
1529 "(%s, %u, %u, %i)\n",
1530 _mesa_enum_to_string(mode
), name
, stream
, primcount
);
1532 vbo_draw_transform_feedback(ctx
, mode
, obj
, stream
, primcount
);
1537 vbo_validated_drawarraysindirect(struct gl_context
*ctx
,
1538 GLenum mode
, const GLvoid
*indirect
)
1540 struct vbo_context
*vbo
= vbo_context(ctx
);
1542 vbo_bind_arrays(ctx
);
1544 vbo
->draw_indirect_prims(ctx
, mode
,
1545 ctx
->DrawIndirectBuffer
, (GLsizeiptr
) indirect
,
1546 1 /* draw_count */ , 16 /* stride */ ,
1549 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1555 vbo_validated_multidrawarraysindirect(struct gl_context
*ctx
,
1557 const GLvoid
*indirect
,
1558 GLsizei primcount
, GLsizei stride
)
1560 struct vbo_context
*vbo
= vbo_context(ctx
);
1561 GLsizeiptr offset
= (GLsizeiptr
) indirect
;
1566 vbo_bind_arrays(ctx
);
1568 vbo
->draw_indirect_prims(ctx
, mode
, ctx
->DrawIndirectBuffer
, offset
,
1569 primcount
, stride
, NULL
, 0, NULL
);
1571 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1577 vbo_validated_drawelementsindirect(struct gl_context
*ctx
,
1578 GLenum mode
, GLenum type
,
1579 const GLvoid
*indirect
)
1581 struct vbo_context
*vbo
= vbo_context(ctx
);
1582 struct _mesa_index_buffer ib
;
1584 vbo_bind_arrays(ctx
);
1586 ib
.count
= 0; /* unknown */
1587 ib
.index_size
= vbo_sizeof_ib_type(type
);
1588 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
1591 vbo
->draw_indirect_prims(ctx
, mode
,
1592 ctx
->DrawIndirectBuffer
, (GLsizeiptr
) indirect
,
1593 1 /* draw_count */ , 20 /* stride */ ,
1596 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1602 vbo_validated_multidrawelementsindirect(struct gl_context
*ctx
,
1603 GLenum mode
, GLenum type
,
1604 const GLvoid
*indirect
,
1605 GLsizei primcount
, GLsizei stride
)
1607 struct vbo_context
*vbo
= vbo_context(ctx
);
1608 struct _mesa_index_buffer ib
;
1609 GLsizeiptr offset
= (GLsizeiptr
) indirect
;
1614 vbo_bind_arrays(ctx
);
1616 /* NOTE: IndexBufferObj is guaranteed to be a VBO. */
1618 ib
.count
= 0; /* unknown */
1619 ib
.index_size
= vbo_sizeof_ib_type(type
);
1620 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
1623 vbo
->draw_indirect_prims(ctx
, mode
,
1624 ctx
->DrawIndirectBuffer
, offset
,
1625 primcount
, stride
, NULL
, 0, &ib
);
1627 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1633 * Like [Multi]DrawArrays/Elements, but they take most arguments from
1636 static void GLAPIENTRY
1637 vbo_exec_DrawArraysIndirect(GLenum mode
, const GLvoid
*indirect
)
1639 GET_CURRENT_CONTEXT(ctx
);
1641 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1642 _mesa_debug(ctx
, "glDrawArraysIndirect(%s, %p)\n",
1643 _mesa_enum_to_string(mode
), indirect
);
1645 if (_mesa_is_no_error_enabled(ctx
)) {
1646 FLUSH_CURRENT(ctx
, 0);
1649 _mesa_update_state(ctx
);
1651 if (!_mesa_validate_DrawArraysIndirect(ctx
, mode
, indirect
))
1655 if (skip_validated_draw(ctx
))
1658 vbo_validated_drawarraysindirect(ctx
, mode
, indirect
);
1662 static void GLAPIENTRY
1663 vbo_exec_DrawElementsIndirect(GLenum mode
, GLenum type
, const GLvoid
*indirect
)
1665 GET_CURRENT_CONTEXT(ctx
);
1667 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1668 _mesa_debug(ctx
, "glDrawElementsIndirect(%s, %s, %p)\n",
1669 _mesa_enum_to_string(mode
),
1670 _mesa_enum_to_string(type
), indirect
);
1672 if (_mesa_is_no_error_enabled(ctx
)) {
1673 FLUSH_CURRENT(ctx
, 0);
1676 _mesa_update_state(ctx
);
1678 if (!_mesa_validate_DrawElementsIndirect(ctx
, mode
, type
, indirect
))
1682 if (skip_validated_draw(ctx
))
1685 vbo_validated_drawelementsindirect(ctx
, mode
, type
, indirect
);
1689 static void GLAPIENTRY
1690 vbo_exec_MultiDrawArraysIndirect(GLenum mode
, const GLvoid
*indirect
,
1691 GLsizei primcount
, GLsizei stride
)
1693 GET_CURRENT_CONTEXT(ctx
);
1695 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1696 _mesa_debug(ctx
, "glMultiDrawArraysIndirect(%s, %p, %i, %i)\n",
1697 _mesa_enum_to_string(mode
), indirect
, primcount
, stride
);
1699 /* If <stride> is zero, the array elements are treated as tightly packed. */
1701 stride
= 4 * sizeof(GLuint
); /* sizeof(DrawArraysIndirectCommand) */
1703 if (!_mesa_validate_MultiDrawArraysIndirect(ctx
, mode
, indirect
,
1707 if (skip_validated_draw(ctx
))
1710 vbo_validated_multidrawarraysindirect(ctx
, mode
, indirect
,
1715 static void GLAPIENTRY
1716 vbo_exec_MultiDrawElementsIndirect(GLenum mode
, GLenum type
,
1717 const GLvoid
*indirect
,
1718 GLsizei primcount
, GLsizei stride
)
1720 GET_CURRENT_CONTEXT(ctx
);
1722 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1723 _mesa_debug(ctx
, "glMultiDrawElementsIndirect(%s, %s, %p, %i, %i)\n",
1724 _mesa_enum_to_string(mode
),
1725 _mesa_enum_to_string(type
), indirect
, primcount
, stride
);
1727 /* If <stride> is zero, the array elements are treated as tightly packed. */
1729 stride
= 5 * sizeof(GLuint
); /* sizeof(DrawElementsIndirectCommand) */
1731 if (!_mesa_validate_MultiDrawElementsIndirect(ctx
, mode
, type
, indirect
,
1735 if (skip_validated_draw(ctx
))
1738 vbo_validated_multidrawelementsindirect(ctx
, mode
, type
, indirect
,
1744 vbo_validated_multidrawarraysindirectcount(struct gl_context
*ctx
,
1748 GLsizei maxdrawcount
,
1751 struct vbo_context
*vbo
= vbo_context(ctx
);
1752 GLsizeiptr offset
= indirect
;
1754 if (maxdrawcount
== 0)
1757 vbo_bind_arrays(ctx
);
1759 vbo
->draw_indirect_prims(ctx
, mode
,
1760 ctx
->DrawIndirectBuffer
, offset
,
1761 maxdrawcount
, stride
,
1762 ctx
->ParameterBuffer
, drawcount
, NULL
);
1764 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1770 vbo_validated_multidrawelementsindirectcount(struct gl_context
*ctx
,
1771 GLenum mode
, GLenum type
,
1774 GLsizei maxdrawcount
,
1777 struct vbo_context
*vbo
= vbo_context(ctx
);
1778 struct _mesa_index_buffer ib
;
1779 GLsizeiptr offset
= (GLsizeiptr
) indirect
;
1781 if (maxdrawcount
== 0)
1784 vbo_bind_arrays(ctx
);
1786 /* NOTE: IndexBufferObj is guaranteed to be a VBO. */
1788 ib
.count
= 0; /* unknown */
1789 ib
.index_size
= vbo_sizeof_ib_type(type
);
1790 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
1793 vbo
->draw_indirect_prims(ctx
, mode
,
1794 ctx
->DrawIndirectBuffer
, offset
,
1795 maxdrawcount
, stride
,
1796 ctx
->ParameterBuffer
, drawcount
, &ib
);
1798 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1803 static void GLAPIENTRY
1804 vbo_exec_MultiDrawArraysIndirectCount(GLenum mode
, GLintptr indirect
,
1806 GLsizei maxdrawcount
, GLsizei stride
)
1808 GET_CURRENT_CONTEXT(ctx
);
1810 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1811 _mesa_debug(ctx
, "glMultiDrawArraysIndirectCountARB"
1812 "(%s, %lx, %lx, %i, %i)\n",
1813 _mesa_enum_to_string(mode
),
1814 (unsigned long) indirect
, (unsigned long) drawcount
,
1815 maxdrawcount
, stride
);
1817 /* If <stride> is zero, the array elements are treated as tightly packed. */
1819 stride
= 4 * sizeof(GLuint
); /* sizeof(DrawArraysIndirectCommand) */
1821 if (!_mesa_validate_MultiDrawArraysIndirectCount(ctx
, mode
,
1822 indirect
, drawcount
,
1823 maxdrawcount
, stride
))
1826 if (skip_validated_draw(ctx
))
1829 vbo_validated_multidrawarraysindirectcount(ctx
, mode
, indirect
, drawcount
,
1830 maxdrawcount
, stride
);
1834 static void GLAPIENTRY
1835 vbo_exec_MultiDrawElementsIndirectCount(GLenum mode
, GLenum type
,
1836 GLintptr indirect
, GLintptr drawcount
,
1837 GLsizei maxdrawcount
, GLsizei stride
)
1839 GET_CURRENT_CONTEXT(ctx
);
1841 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1842 _mesa_debug(ctx
, "glMultiDrawElementsIndirectCountARB"
1843 "(%s, %s, %lx, %lx, %i, %i)\n",
1844 _mesa_enum_to_string(mode
), _mesa_enum_to_string(type
),
1845 (unsigned long) indirect
, (unsigned long) drawcount
,
1846 maxdrawcount
, stride
);
1848 /* If <stride> is zero, the array elements are treated as tightly packed. */
1850 stride
= 5 * sizeof(GLuint
); /* sizeof(DrawElementsIndirectCommand) */
1852 if (!_mesa_validate_MultiDrawElementsIndirectCount(ctx
, mode
, type
,
1853 indirect
, drawcount
,
1854 maxdrawcount
, stride
))
1857 if (skip_validated_draw(ctx
))
1860 vbo_validated_multidrawelementsindirectcount(ctx
, mode
, type
, indirect
,
1861 drawcount
, maxdrawcount
,
1867 * Initialize the dispatch table with the VBO functions for drawing.
1870 vbo_initialize_exec_dispatch(const struct gl_context
*ctx
,
1871 struct _glapi_table
*exec
)
1873 SET_DrawArrays(exec
, vbo_exec_DrawArrays
);
1874 SET_DrawElements(exec
, vbo_exec_DrawElements
);
1876 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
)) {
1877 SET_DrawRangeElements(exec
, vbo_exec_DrawRangeElements
);
1880 SET_MultiDrawArrays(exec
, vbo_exec_MultiDrawArrays
);
1881 SET_MultiDrawElementsEXT(exec
, vbo_exec_MultiDrawElements
);
1883 if (ctx
->API
== API_OPENGL_COMPAT
) {
1884 SET_Rectf(exec
, vbo_exec_Rectf
);
1885 SET_EvalMesh1(exec
, vbo_exec_EvalMesh1
);
1886 SET_EvalMesh2(exec
, vbo_exec_EvalMesh2
);
1889 if (ctx
->API
!= API_OPENGLES
&&
1890 ctx
->Extensions
.ARB_draw_elements_base_vertex
) {
1891 SET_DrawElementsBaseVertex(exec
, vbo_exec_DrawElementsBaseVertex
);
1892 SET_MultiDrawElementsBaseVertex(exec
,
1893 vbo_exec_MultiDrawElementsBaseVertex
);
1895 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
)) {
1896 SET_DrawRangeElementsBaseVertex(exec
,
1897 vbo_exec_DrawRangeElementsBaseVertex
);
1898 SET_DrawElementsInstancedBaseVertex(exec
,
1899 vbo_exec_DrawElementsInstancedBaseVertex
);
1903 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
)) {
1904 SET_DrawArraysInstancedBaseInstance(exec
,
1905 vbo_exec_DrawArraysInstancedBaseInstance
);
1906 SET_DrawElementsInstancedBaseInstance(exec
,
1907 vbo_exec_DrawElementsInstancedBaseInstance
);
1908 SET_DrawElementsInstancedBaseVertexBaseInstance(exec
,
1909 vbo_exec_DrawElementsInstancedBaseVertexBaseInstance
);
1912 if (ctx
->API
== API_OPENGL_CORE
|| _mesa_is_gles31(ctx
)) {
1913 SET_DrawArraysIndirect(exec
, vbo_exec_DrawArraysIndirect
);
1914 SET_DrawElementsIndirect(exec
, vbo_exec_DrawElementsIndirect
);
1917 if (ctx
->API
== API_OPENGL_CORE
) {
1918 SET_MultiDrawArraysIndirect(exec
, vbo_exec_MultiDrawArraysIndirect
);
1919 SET_MultiDrawElementsIndirect(exec
, vbo_exec_MultiDrawElementsIndirect
);
1920 SET_MultiDrawArraysIndirectCountARB(exec
,
1921 vbo_exec_MultiDrawArraysIndirectCount
);
1922 SET_MultiDrawElementsIndirectCountARB(exec
,
1923 vbo_exec_MultiDrawElementsIndirectCount
);
1926 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
)) {
1927 SET_DrawArraysInstancedARB(exec
, vbo_exec_DrawArraysInstanced
);
1928 SET_DrawElementsInstancedARB(exec
, vbo_exec_DrawElementsInstanced
);
1931 if (_mesa_is_desktop_gl(ctx
)) {
1932 SET_DrawTransformFeedback(exec
, vbo_exec_DrawTransformFeedback
);
1933 SET_DrawTransformFeedbackStream(exec
,
1934 vbo_exec_DrawTransformFeedbackStream
);
1935 SET_DrawTransformFeedbackInstanced(exec
,
1936 vbo_exec_DrawTransformFeedbackInstanced
);
1937 SET_DrawTransformFeedbackStreamInstanced(exec
,
1938 vbo_exec_DrawTransformFeedbackStreamInstanced
);
1945 * The following functions are only used for OpenGL ES 1/2 support.
1946 * And some aren't even supported (yet) in ES 1/2.
1951 _mesa_DrawArrays(GLenum mode
, GLint first
, GLsizei count
)
1953 vbo_exec_DrawArrays(mode
, first
, count
);
1958 _mesa_DrawArraysInstanced(GLenum mode
, GLint first
, GLsizei count
,
1961 vbo_exec_DrawArraysInstanced(mode
, first
, count
, primcount
);
1966 _mesa_DrawElements(GLenum mode
, GLsizei count
, GLenum type
,
1967 const GLvoid
*indices
)
1969 vbo_exec_DrawElements(mode
, count
, type
, indices
);
1974 _mesa_DrawElementsBaseVertex(GLenum mode
, GLsizei count
, GLenum type
,
1975 const GLvoid
*indices
, GLint basevertex
)
1977 vbo_exec_DrawElementsBaseVertex(mode
, count
, type
, indices
, basevertex
);
1982 _mesa_DrawRangeElements(GLenum mode
, GLuint start
, GLuint end
, GLsizei count
,
1983 GLenum type
, const GLvoid
* indices
)
1985 vbo_exec_DrawRangeElements(mode
, start
, end
, count
, type
, indices
);
1990 _mesa_DrawRangeElementsBaseVertex(GLenum mode
, GLuint start
, GLuint end
,
1991 GLsizei count
, GLenum type
,
1992 const GLvoid
*indices
, GLint basevertex
)
1994 vbo_exec_DrawRangeElementsBaseVertex(mode
, start
, end
, count
, type
,
1995 indices
, basevertex
);
2000 _mesa_MultiDrawElementsEXT(GLenum mode
, const GLsizei
*count
, GLenum type
,
2001 const GLvoid
** indices
, GLsizei primcount
)
2003 vbo_exec_MultiDrawElements(mode
, count
, type
, indices
, primcount
);
2008 _mesa_MultiDrawElementsBaseVertex(GLenum mode
,
2009 const GLsizei
*count
, GLenum type
,
2010 const GLvoid
**indices
, GLsizei primcount
,
2011 const GLint
*basevertex
)
2013 vbo_exec_MultiDrawElementsBaseVertex(mode
, count
, type
, indices
,
2014 primcount
, basevertex
);
2019 _mesa_DrawTransformFeedback(GLenum mode
, GLuint name
)
2021 vbo_exec_DrawTransformFeedback(mode
, name
);