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_private.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 sizeof_ib_type(GLenum type
)
120 case GL_UNSIGNED_INT
:
121 return sizeof(GLuint
);
122 case GL_UNSIGNED_SHORT
:
123 return sizeof(GLushort
);
124 case GL_UNSIGNED_BYTE
:
125 return sizeof(GLubyte
);
127 assert(!"unsupported index data type");
128 /* In case assert is turned off */
134 * Examine the array's data for NaNs, etc.
135 * For debug purposes; not normally used.
138 check_draw_elements_data(struct gl_context
*ctx
, GLsizei count
,
139 GLenum elemType
, const void *elements
,
142 struct gl_vertex_array_object
*vao
= ctx
->Array
.VAO
;
147 if (_mesa_is_bufferobj(vao
->IndexBufferObj
)) {
148 elemMap
= ctx
->Driver
.MapBufferRange(ctx
, 0,
149 vao
->IndexBufferObj
->Size
,
151 vao
->IndexBufferObj
, MAP_INTERNAL
);
152 elements
= ADD_POINTERS(elements
, elemMap
);
155 for (i
= 0; i
< count
; i
++) {
160 case GL_UNSIGNED_BYTE
:
161 j
= ((const GLubyte
*) elements
)[i
];
163 case GL_UNSIGNED_SHORT
:
164 j
= ((const GLushort
*) elements
)[i
];
166 case GL_UNSIGNED_INT
:
167 j
= ((const GLuint
*) elements
)[i
];
170 unreachable("Unexpected index buffer type");
173 /* check element j of each enabled array */
174 for (k
= 0; k
< VERT_ATTRIB_MAX
; k
++) {
175 check_array_data(ctx
, vao
, k
, j
);
179 if (_mesa_is_bufferobj(vao
->IndexBufferObj
)) {
180 ctx
->Driver
.UnmapBuffer(ctx
, vao
->IndexBufferObj
, MAP_INTERNAL
);
183 for (k
= 0; k
< VERT_ATTRIB_MAX
; k
++) {
184 unmap_array_buffer(ctx
, vao
, k
);
190 * Check array data, looking for NaNs, etc.
193 check_draw_arrays_data(struct gl_context
*ctx
, GLint start
, GLsizei count
)
200 * Check if we should skip the draw call even after validation was successful.
203 skip_validated_draw(struct gl_context
*ctx
)
207 /* For ES2, we can draw if we have a vertex program/shader). */
208 return ctx
->VertexProgram
._Current
== NULL
;
211 /* For OpenGL ES, only draw if we have vertex positions
213 if (!ctx
->Array
.VAO
->VertexAttrib
[VERT_ATTRIB_POS
].Enabled
)
217 case API_OPENGL_CORE
:
218 /* Section 7.3 (Program Objects) of the OpenGL 4.5 Core Profile spec
221 * "If there is no active program for the vertex or fragment shader
222 * stages, the results of vertex and/or fragment processing will be
223 * undefined. However, this is not an error."
225 * The fragment shader is not tested here because other state (e.g.,
226 * GL_RASTERIZER_DISCARD) affects whether or not we actually care.
228 return ctx
->VertexProgram
._Current
== NULL
;
230 case API_OPENGL_COMPAT
:
231 if (ctx
->VertexProgram
._Current
!= NULL
) {
232 /* Draw regardless of whether or not we have any vertex arrays.
233 * (Ex: could draw a point using a constant vertex pos)
237 /* Draw if we have vertex positions (GL_VERTEX_ARRAY or generic
240 return (!ctx
->Array
.VAO
->VertexAttrib
[VERT_ATTRIB_POS
].Enabled
&&
241 !ctx
->Array
.VAO
->VertexAttrib
[VERT_ATTRIB_GENERIC0
].Enabled
);
246 unreachable("Invalid API value in check_valid_to_render()");
254 * Print info/data for glDrawArrays(), for debugging.
257 print_draw_arrays(struct gl_context
*ctx
,
258 GLenum mode
, GLint start
, GLsizei count
)
260 const struct gl_vertex_array_object
*vao
= ctx
->Array
.VAO
;
262 printf("vbo_exec_DrawArrays(mode 0x%x, start %d, count %d):\n",
266 for (i
= 0; i
< VERT_ATTRIB_MAX
; ++i
) {
267 const struct gl_array_attributes
*array
= &vao
->VertexAttrib
[i
];
271 const struct gl_vertex_buffer_binding
*binding
=
272 &vao
->BufferBinding
[array
->BufferBindingIndex
];
273 struct gl_buffer_object
*bufObj
= binding
->BufferObj
;
275 printf("attr %s: size %d stride %d enabled %d "
276 "ptr %p Bufobj %u\n",
277 gl_vert_attrib_name((gl_vert_attrib
) i
),
278 array
->Size
, binding
->Stride
, array
->Enabled
,
279 array
->Ptr
, bufObj
->Name
);
281 if (_mesa_is_bufferobj(bufObj
)) {
282 GLubyte
*p
= ctx
->Driver
.MapBufferRange(ctx
, 0, bufObj
->Size
,
283 GL_MAP_READ_BIT
, bufObj
,
285 int offset
= (int) (GLintptr
)
286 _mesa_vertex_attrib_address(array
, binding
);
289 switch (array
->Type
) {
292 case GL_UNSIGNED_INT64_ARB
:
299 float *f
= (float *) (p
+ offset
);
302 int n
= (count
- 1) * (binding
->Stride
/ (4 * multiplier
))
306 printf(" Data at offset %d:\n", offset
);
309 printf(" double[%d] = 0x%016llx %lf\n", i
,
310 ((unsigned long long *) k
)[i
], ((double *) f
)[i
]);
312 printf(" float[%d] = 0x%08x %f\n", i
, k
[i
], f
[i
]);
315 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, MAP_INTERNAL
);
322 * Set the vbo->exec->inputs[] pointers to point to the enabled
323 * vertex arrays. This depends on the current vertex program/shader
324 * being executed because of whether or not generic vertex arrays
325 * alias the conventional vertex arrays.
326 * For arrays that aren't enabled, we set the input[attrib] pointer
327 * to point at a zero-stride current value "array".
330 recalculate_input_bindings(struct gl_context
*ctx
)
332 struct vbo_context
*vbo
= vbo_context(ctx
);
333 struct vbo_exec_context
*exec
= &vbo
->exec
;
334 const struct gl_vertex_array_object
*vao
= ctx
->Array
.VAO
;
335 const struct gl_vertex_array
*vertexAttrib
= vao
->_VertexArray
;
336 const struct gl_vertex_array
**inputs
= &exec
->array
.inputs
[0];
338 /* May shuffle the position and generic0 bits around */
339 GLbitfield vp_inputs
= _mesa_get_vao_vp_inputs(vao
);
341 const enum vp_mode program_mode
= get_vp_mode(ctx
);
342 const GLubyte
*const map
= _vbo_attribute_alias_map
[program_mode
];
343 switch (program_mode
) {
345 /* When no vertex program is active (or the vertex program is generated
346 * from fixed-function state). We put the material values into the
347 * generic slots. Since the vao has no material arrays, mute these
348 * slots from the enabled arrays so that the current material values
349 * are pulled instead of the vao arrays.
351 vp_inputs
&= VERT_BIT_FF_ALL
;
356 /* There are no shaders in OpenGL ES 1.x, so this code path should be
357 * impossible to reach. The meta code is careful to not use shaders in
360 assert(ctx
->API
!= API_OPENGLES
);
362 /* In the compatibility profile of desktop OpenGL, the generic[0]
363 * attribute array aliases and overrides the legacy position array.
364 * Otherwise, legacy attributes available in the legacy slots,
365 * generic attributes in the generic slots and materials are not
366 * available as per-vertex attributes.
368 * In all other APIs, only the generic attributes exist, and none of the
369 * slots are considered "magic."
372 /* Other parts of the code assume that inputs[VERT_ATTRIB_POS] through
373 * inputs[VERT_ATTRIB_FF_MAX] will be non-NULL. However, in OpenGL
374 * ES 2.0+ or OpenGL core profile, none of these arrays should ever
377 if (ctx
->API
!= API_OPENGL_COMPAT
)
378 vp_inputs
&= VERT_BIT_GENERIC_ALL
;
385 const gl_attribute_map_mode mode
= vao
->_AttributeMapMode
;
386 const GLubyte
*const vao_map
= _mesa_vao_attribute_map
[mode
];
387 for (unsigned vp_attrib
= 0; vp_attrib
< VERT_ATTRIB_MAX
; ++vp_attrib
) {
388 if (unlikely(vp_inputs
& VERT_BIT(vp_attrib
)))
389 inputs
[vp_attrib
] = &vertexAttrib
[vao_map
[vp_attrib
]];
391 inputs
[vp_attrib
] = &vbo
->currval
[map
[vp_attrib
]];
394 _mesa_set_varying_vp_inputs(ctx
, vp_inputs
);
395 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewArray
;
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.
407 vbo_bind_arrays(struct gl_context
*ctx
)
409 struct vbo_context
*vbo
= vbo_context(ctx
);
410 struct vbo_exec_context
*exec
= &vbo
->exec
;
412 _mesa_set_drawing_arrays(ctx
, vbo
->exec
.array
.inputs
);
414 if (exec
->array
.recalculate_inputs
) {
415 recalculate_input_bindings(ctx
);
416 exec
->array
.recalculate_inputs
= GL_FALSE
;
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.
424 /* Setting "validating" to TRUE prevents _mesa_update_state from
425 * invalidating what we just did.
427 exec
->validating
= GL_TRUE
;
428 _mesa_update_state(ctx
);
429 exec
->validating
= GL_FALSE
;
436 * Helper function called by the other DrawArrays() functions below.
437 * This is where we handle primitive restart for drawing non-indexed
438 * arrays. If primitive restart is enabled, it typically means
439 * splitting one DrawArrays() into two.
442 vbo_draw_arrays(struct gl_context
*ctx
, GLenum mode
, GLint start
,
443 GLsizei count
, GLuint numInstances
, GLuint baseInstance
,
446 struct vbo_context
*vbo
= vbo_context(ctx
);
447 struct _mesa_prim prim
;
449 if (skip_validated_draw(ctx
))
452 vbo_bind_arrays(ctx
);
454 /* OpenGL 4.5 says that primitive restart is ignored with non-indexed
457 memset(&prim
, 0, sizeof(prim
));
461 prim
.num_instances
= numInstances
;
462 prim
.base_instance
= baseInstance
;
463 prim
.draw_id
= drawID
;
464 prim
.is_indirect
= 0;
468 vbo
->draw_prims(ctx
, &prim
, 1, NULL
,
469 GL_TRUE
, start
, start
+ count
- 1, NULL
, 0, NULL
);
471 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
) {
478 * Execute a glRectf() function.
480 static void GLAPIENTRY
481 vbo_exec_Rectf(GLfloat x1
, GLfloat y1
, GLfloat x2
, GLfloat y2
)
483 GET_CURRENT_CONTEXT(ctx
);
484 ASSERT_OUTSIDE_BEGIN_END(ctx
);
486 CALL_Begin(GET_DISPATCH(), (GL_QUADS
));
487 CALL_Vertex2f(GET_DISPATCH(), (x1
, y1
));
488 CALL_Vertex2f(GET_DISPATCH(), (x2
, y1
));
489 CALL_Vertex2f(GET_DISPATCH(), (x2
, y2
));
490 CALL_Vertex2f(GET_DISPATCH(), (x1
, y2
));
491 CALL_End(GET_DISPATCH(), ());
495 static void GLAPIENTRY
496 vbo_exec_EvalMesh1(GLenum mode
, GLint i1
, GLint i2
)
498 GET_CURRENT_CONTEXT(ctx
);
508 prim
= GL_LINE_STRIP
;
511 _mesa_error(ctx
, GL_INVALID_ENUM
, "glEvalMesh1(mode)");
515 /* No effect if vertex maps disabled.
517 if (!ctx
->Eval
.Map1Vertex4
&& !ctx
->Eval
.Map1Vertex3
)
520 du
= ctx
->Eval
.MapGrid1du
;
521 u
= ctx
->Eval
.MapGrid1u1
+ i1
* du
;
523 CALL_Begin(GET_DISPATCH(), (prim
));
524 for (i
= i1
; i
<= i2
; i
++, u
+= du
) {
525 CALL_EvalCoord1f(GET_DISPATCH(), (u
));
527 CALL_End(GET_DISPATCH(), ());
531 static void GLAPIENTRY
532 vbo_exec_EvalMesh2(GLenum mode
, GLint i1
, GLint i2
, GLint j1
, GLint j2
)
534 GET_CURRENT_CONTEXT(ctx
);
535 GLfloat u
, du
, v
, dv
, v1
, u1
;
544 _mesa_error(ctx
, GL_INVALID_ENUM
, "glEvalMesh2(mode)");
548 /* No effect if vertex maps disabled.
550 if (!ctx
->Eval
.Map2Vertex4
&& !ctx
->Eval
.Map2Vertex3
)
553 du
= ctx
->Eval
.MapGrid2du
;
554 dv
= ctx
->Eval
.MapGrid2dv
;
555 v1
= ctx
->Eval
.MapGrid2v1
+ j1
* dv
;
556 u1
= ctx
->Eval
.MapGrid2u1
+ i1
* du
;
560 CALL_Begin(GET_DISPATCH(), (GL_POINTS
));
561 for (v
= v1
, j
= j1
; j
<= j2
; j
++, v
+= dv
) {
562 for (u
= u1
, i
= i1
; i
<= i2
; i
++, u
+= du
) {
563 CALL_EvalCoord2f(GET_DISPATCH(), (u
, v
));
566 CALL_End(GET_DISPATCH(), ());
569 for (v
= v1
, j
= j1
; j
<= j2
; j
++, v
+= dv
) {
570 CALL_Begin(GET_DISPATCH(), (GL_LINE_STRIP
));
571 for (u
= u1
, i
= i1
; i
<= i2
; i
++, u
+= du
) {
572 CALL_EvalCoord2f(GET_DISPATCH(), (u
, v
));
574 CALL_End(GET_DISPATCH(), ());
576 for (u
= u1
, i
= i1
; i
<= i2
; i
++, u
+= du
) {
577 CALL_Begin(GET_DISPATCH(), (GL_LINE_STRIP
));
578 for (v
= v1
, j
= j1
; j
<= j2
; j
++, v
+= dv
) {
579 CALL_EvalCoord2f(GET_DISPATCH(), (u
, v
));
581 CALL_End(GET_DISPATCH(), ());
585 for (v
= v1
, j
= j1
; j
< j2
; j
++, v
+= dv
) {
586 CALL_Begin(GET_DISPATCH(), (GL_TRIANGLE_STRIP
));
587 for (u
= u1
, i
= i1
; i
<= i2
; i
++, u
+= du
) {
588 CALL_EvalCoord2f(GET_DISPATCH(), (u
, v
));
589 CALL_EvalCoord2f(GET_DISPATCH(), (u
, v
+ dv
));
591 CALL_End(GET_DISPATCH(), ());
599 * Called from glDrawArrays when in immediate mode (not display list mode).
601 static void GLAPIENTRY
602 vbo_exec_DrawArrays(GLenum mode
, GLint start
, GLsizei count
)
604 GET_CURRENT_CONTEXT(ctx
);
606 if (MESA_VERBOSE
& VERBOSE_DRAW
)
607 _mesa_debug(ctx
, "glDrawArrays(%s, %d, %d)\n",
608 _mesa_enum_to_string(mode
), start
, count
);
610 if (_mesa_is_no_error_enabled(ctx
)) {
611 FLUSH_CURRENT(ctx
, 0);
614 _mesa_update_state(ctx
);
616 if (!_mesa_validate_DrawArrays(ctx
, mode
, count
))
621 check_draw_arrays_data(ctx
, start
, count
);
623 vbo_draw_arrays(ctx
, mode
, start
, count
, 1, 0, 0);
626 print_draw_arrays(ctx
, mode
, start
, count
);
631 * Called from glDrawArraysInstanced when in immediate mode (not
632 * display list mode).
634 static void GLAPIENTRY
635 vbo_exec_DrawArraysInstanced(GLenum mode
, GLint start
, GLsizei count
,
636 GLsizei numInstances
)
638 GET_CURRENT_CONTEXT(ctx
);
640 if (MESA_VERBOSE
& VERBOSE_DRAW
)
641 _mesa_debug(ctx
, "glDrawArraysInstanced(%s, %d, %d, %d)\n",
642 _mesa_enum_to_string(mode
), start
, count
, numInstances
);
645 if (_mesa_is_no_error_enabled(ctx
)) {
646 FLUSH_CURRENT(ctx
, 0);
649 _mesa_update_state(ctx
);
651 if (!_mesa_validate_DrawArraysInstanced(ctx
, mode
, start
, count
,
657 check_draw_arrays_data(ctx
, start
, count
);
659 vbo_draw_arrays(ctx
, mode
, start
, count
, numInstances
, 0, 0);
662 print_draw_arrays(ctx
, mode
, start
, count
);
667 * Called from glDrawArraysInstancedBaseInstance when in immediate mode.
669 static void GLAPIENTRY
670 vbo_exec_DrawArraysInstancedBaseInstance(GLenum mode
, GLint first
,
671 GLsizei count
, GLsizei numInstances
,
674 GET_CURRENT_CONTEXT(ctx
);
676 if (MESA_VERBOSE
& VERBOSE_DRAW
)
678 "glDrawArraysInstancedBaseInstance(%s, %d, %d, %d, %d)\n",
679 _mesa_enum_to_string(mode
), first
, count
,
680 numInstances
, baseInstance
);
682 if (_mesa_is_no_error_enabled(ctx
)) {
683 FLUSH_CURRENT(ctx
, 0);
686 _mesa_update_state(ctx
);
688 if (!_mesa_validate_DrawArraysInstanced(ctx
, mode
, first
, count
,
694 check_draw_arrays_data(ctx
, first
, count
);
696 vbo_draw_arrays(ctx
, mode
, first
, count
, numInstances
, baseInstance
, 0);
699 print_draw_arrays(ctx
, mode
, first
, count
);
704 * Called from glMultiDrawArrays when in immediate mode.
706 static void GLAPIENTRY
707 vbo_exec_MultiDrawArrays(GLenum mode
, const GLint
*first
,
708 const GLsizei
*count
, GLsizei primcount
)
710 GET_CURRENT_CONTEXT(ctx
);
713 if (MESA_VERBOSE
& VERBOSE_DRAW
)
715 "glMultiDrawArrays(%s, %p, %p, %d)\n",
716 _mesa_enum_to_string(mode
), first
, count
, primcount
);
718 if (_mesa_is_no_error_enabled(ctx
)) {
719 FLUSH_CURRENT(ctx
, 0);
722 _mesa_update_state(ctx
);
724 if (!_mesa_validate_MultiDrawArrays(ctx
, mode
, count
, primcount
))
728 for (i
= 0; i
< primcount
; i
++) {
731 check_draw_arrays_data(ctx
, first
[i
], count
[i
]);
733 /* The GL_ARB_shader_draw_parameters spec adds the following after the
734 * pseudo-code describing glMultiDrawArrays:
736 * "The index of the draw (<i> in the above pseudo-code) may be
737 * read by a vertex shader as <gl_DrawIDARB>, as described in
740 vbo_draw_arrays(ctx
, mode
, first
[i
], count
[i
], 1, 0, i
);
743 print_draw_arrays(ctx
, mode
, first
[i
], count
[i
]);
751 * Map GL_ELEMENT_ARRAY_BUFFER and print contents.
756 dump_element_buffer(struct gl_context
*ctx
, GLenum type
)
759 ctx
->Driver
.MapBufferRange(ctx
, 0,
760 ctx
->Array
.VAO
->IndexBufferObj
->Size
,
762 ctx
->Array
.VAO
->IndexBufferObj
,
765 case GL_UNSIGNED_BYTE
:
767 const GLubyte
*us
= (const GLubyte
*) map
;
769 for (i
= 0; i
< ctx
->Array
.VAO
->IndexBufferObj
->Size
; i
++) {
770 printf("%02x ", us
[i
]);
777 case GL_UNSIGNED_SHORT
:
779 const GLushort
*us
= (const GLushort
*) map
;
781 for (i
= 0; i
< ctx
->Array
.VAO
->IndexBufferObj
->Size
/ 2; i
++) {
782 printf("%04x ", us
[i
]);
789 case GL_UNSIGNED_INT
:
791 const GLuint
*us
= (const GLuint
*) map
;
793 for (i
= 0; i
< ctx
->Array
.VAO
->IndexBufferObj
->Size
/ 4; i
++) {
794 printf("%08x ", us
[i
]);
805 ctx
->Driver
.UnmapBuffer(ctx
, ctx
->Array
.VAO
->IndexBufferObj
, MAP_INTERNAL
);
811 skip_draw_elements(struct gl_context
*ctx
, GLsizei count
,
812 const GLvoid
*indices
)
817 /* Not using a VBO for indices, so avoid NULL pointer derefs later.
819 if (!_mesa_is_bufferobj(ctx
->Array
.VAO
->IndexBufferObj
) && indices
== NULL
)
822 if (skip_validated_draw(ctx
))
830 * Inner support for both _mesa_DrawElements and _mesa_DrawRangeElements.
831 * Do the rendering for a glDrawElements or glDrawRangeElements call after
832 * we've validated buffer bounds, etc.
835 vbo_validated_drawrangeelements(struct gl_context
*ctx
, GLenum mode
,
836 GLboolean index_bounds_valid
,
837 GLuint start
, GLuint end
,
838 GLsizei count
, GLenum type
,
839 const GLvoid
* indices
,
840 GLint basevertex
, GLuint numInstances
,
843 struct vbo_context
*vbo
= vbo_context(ctx
);
844 struct _mesa_index_buffer ib
;
845 struct _mesa_prim prim
;
847 if (!index_bounds_valid
) {
852 if (skip_draw_elements(ctx
, count
, indices
))
855 vbo_bind_arrays(ctx
);
858 ib
.index_size
= sizeof_ib_type(type
);
859 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
870 prim
.is_indirect
= 0;
871 prim
.basevertex
= basevertex
;
872 prim
.num_instances
= numInstances
;
873 prim
.base_instance
= baseInstance
;
876 /* Need to give special consideration to rendering a range of
877 * indices starting somewhere above zero. Typically the
878 * application is issuing multiple DrawRangeElements() to draw
879 * successive primitives layed out linearly in the vertex arrays.
880 * Unless the vertex arrays are all in a VBO (or locked as with
881 * CVA), the OpenGL semantics imply that we need to re-read or
882 * re-upload the vertex data on each draw call.
884 * In the case of hardware tnl, we want to avoid starting the
885 * upload at zero, as it will mean every draw call uploads an
886 * increasing amount of not-used vertex data. Worse - in the
887 * software tnl module, all those vertices might be transformed and
888 * lit but never rendered.
890 * If we just upload or transform the vertices in start..end,
891 * however, the indices will be incorrect.
893 * At this level, we don't know exactly what the requirements of
894 * the backend are going to be, though it will likely boil down to
897 * 1) Do nothing, everything is in a VBO and is processed once
900 * 2) Adjust the indices and vertex arrays so that start becomes
903 * Rather than doing anything here, I'll provide a helper function
904 * for the latter case elsewhere.
907 vbo
->draw_prims(ctx
, &prim
, 1, &ib
,
908 index_bounds_valid
, start
, end
, NULL
, 0, NULL
);
910 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
) {
917 * Called by glDrawRangeElementsBaseVertex() in immediate mode.
919 static void GLAPIENTRY
920 vbo_exec_DrawRangeElementsBaseVertex(GLenum mode
, GLuint start
, GLuint end
,
921 GLsizei count
, GLenum type
,
922 const GLvoid
* indices
, GLint basevertex
)
924 static GLuint warnCount
= 0;
925 GLboolean index_bounds_valid
= GL_TRUE
;
927 /* This is only useful to catch invalid values in the "end" parameter
930 GLuint max_element
= 2 * 1000 * 1000 * 1000; /* just a big number */
932 GET_CURRENT_CONTEXT(ctx
);
934 if (MESA_VERBOSE
& VERBOSE_DRAW
)
936 "glDrawRangeElementsBaseVertex(%s, %u, %u, %d, %s, %p, %d)\n",
937 _mesa_enum_to_string(mode
), start
, end
, count
,
938 _mesa_enum_to_string(type
), indices
, basevertex
);
940 if (_mesa_is_no_error_enabled(ctx
)) {
941 FLUSH_CURRENT(ctx
, 0);
944 _mesa_update_state(ctx
);
946 if (!_mesa_validate_DrawRangeElements(ctx
, mode
, start
, end
, count
,
951 if ((int) end
+ basevertex
< 0 || start
+ basevertex
>= max_element
) {
952 /* The application requested we draw using a range of indices that's
953 * outside the bounds of the current VBO. This is invalid and appears
954 * to give undefined results. The safest thing to do is to simply
955 * ignore the range, in case the application botched their range tracking
956 * but did provide valid indices. Also issue a warning indicating that
957 * the application is broken.
959 if (warnCount
++ < 10) {
960 _mesa_warning(ctx
, "glDrawRangeElements(start %u, end %u, "
961 "basevertex %d, count %d, type 0x%x, indices=%p):\n"
962 "\trange is outside VBO bounds (max=%u); ignoring.\n"
963 "\tThis should be fixed in the application.",
964 start
, end
, basevertex
, count
, type
, indices
,
967 index_bounds_valid
= GL_FALSE
;
970 /* NOTE: It's important that 'end' is a reasonable value.
971 * in _tnl_draw_prims(), we use end to determine how many vertices
972 * to transform. If it's too large, we can unnecessarily split prims
973 * or we can read/write out of memory in several different places!
976 /* Catch/fix some potential user errors */
977 if (type
== GL_UNSIGNED_BYTE
) {
978 start
= MIN2(start
, 0xff);
979 end
= MIN2(end
, 0xff);
981 else if (type
== GL_UNSIGNED_SHORT
) {
982 start
= MIN2(start
, 0xffff);
983 end
= MIN2(end
, 0xffff);
987 printf("glDraw[Range]Elements{,BaseVertex}"
988 "(start %u, end %u, type 0x%x, count %d) ElemBuf %u, "
990 start
, end
, type
, count
,
991 ctx
->Array
.VAO
->IndexBufferObj
->Name
, basevertex
);
994 if ((int) start
+ basevertex
< 0 || end
+ basevertex
>= max_element
)
995 index_bounds_valid
= GL_FALSE
;
998 check_draw_elements_data(ctx
, count
, type
, indices
, basevertex
);
1000 (void) check_draw_elements_data
;
1003 if (!index_bounds_valid
) {
1008 vbo_validated_drawrangeelements(ctx
, mode
, index_bounds_valid
, start
, end
,
1009 count
, type
, indices
, basevertex
, 1, 0);
1014 * Called by glDrawRangeElements() in immediate mode.
1016 static void GLAPIENTRY
1017 vbo_exec_DrawRangeElements(GLenum mode
, GLuint start
, GLuint end
,
1018 GLsizei count
, GLenum type
, const GLvoid
* indices
)
1020 if (MESA_VERBOSE
& VERBOSE_DRAW
) {
1021 GET_CURRENT_CONTEXT(ctx
);
1023 "glDrawRangeElements(%s, %u, %u, %d, %s, %p)\n",
1024 _mesa_enum_to_string(mode
), start
, end
, count
,
1025 _mesa_enum_to_string(type
), indices
);
1028 vbo_exec_DrawRangeElementsBaseVertex(mode
, start
, end
, count
, type
,
1034 * Called by glDrawElements() in immediate mode.
1036 static void GLAPIENTRY
1037 vbo_exec_DrawElements(GLenum mode
, GLsizei count
, GLenum type
,
1038 const GLvoid
* indices
)
1040 GET_CURRENT_CONTEXT(ctx
);
1042 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1043 _mesa_debug(ctx
, "glDrawElements(%s, %u, %s, %p)\n",
1044 _mesa_enum_to_string(mode
), count
,
1045 _mesa_enum_to_string(type
), indices
);
1047 if (_mesa_is_no_error_enabled(ctx
)) {
1048 FLUSH_CURRENT(ctx
, 0);
1051 _mesa_update_state(ctx
);
1053 if (!_mesa_validate_DrawElements(ctx
, mode
, count
, type
, indices
))
1057 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, 0, ~0,
1058 count
, type
, indices
, 0, 1, 0);
1063 * Called by glDrawElementsBaseVertex() in immediate mode.
1065 static void GLAPIENTRY
1066 vbo_exec_DrawElementsBaseVertex(GLenum mode
, GLsizei count
, GLenum type
,
1067 const GLvoid
* indices
, GLint basevertex
)
1069 GET_CURRENT_CONTEXT(ctx
);
1071 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1072 _mesa_debug(ctx
, "glDrawElements(%s, %u, %s, %p)\n",
1073 _mesa_enum_to_string(mode
), count
,
1074 _mesa_enum_to_string(type
), indices
);
1076 if (_mesa_is_no_error_enabled(ctx
)) {
1077 FLUSH_CURRENT(ctx
, 0);
1080 _mesa_update_state(ctx
);
1082 if (!_mesa_validate_DrawElements(ctx
, mode
, count
, type
, indices
))
1086 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, 0, ~0,
1087 count
, type
, indices
, basevertex
, 1, 0);
1092 * Called by glDrawElementsInstanced() in immediate mode.
1094 static void GLAPIENTRY
1095 vbo_exec_DrawElementsInstanced(GLenum mode
, GLsizei count
, GLenum type
,
1096 const GLvoid
* indices
, GLsizei numInstances
)
1098 GET_CURRENT_CONTEXT(ctx
);
1100 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1101 _mesa_debug(ctx
, "glDrawElements(%s, %u, %s, %p)\n",
1102 _mesa_enum_to_string(mode
), count
,
1103 _mesa_enum_to_string(type
), indices
);
1105 if (_mesa_is_no_error_enabled(ctx
)) {
1106 FLUSH_CURRENT(ctx
, 0);
1109 _mesa_update_state(ctx
);
1111 if (!_mesa_validate_DrawElementsInstanced(ctx
, mode
, count
, type
,
1112 indices
, numInstances
))
1116 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, 0, ~0,
1117 count
, type
, indices
, 0, numInstances
, 0);
1122 * Called by glDrawElementsInstancedBaseVertex() in immediate mode.
1124 static void GLAPIENTRY
1125 vbo_exec_DrawElementsInstancedBaseVertex(GLenum mode
, GLsizei count
,
1126 GLenum type
, const GLvoid
* indices
,
1127 GLsizei numInstances
,
1130 GET_CURRENT_CONTEXT(ctx
);
1132 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1134 "glDrawElementsInstancedBaseVertex"
1135 "(%s, %d, %s, %p, %d; %d)\n",
1136 _mesa_enum_to_string(mode
), count
,
1137 _mesa_enum_to_string(type
), indices
,
1138 numInstances
, basevertex
);
1140 if (_mesa_is_no_error_enabled(ctx
)) {
1141 FLUSH_CURRENT(ctx
, 0);
1144 _mesa_update_state(ctx
);
1146 if (!_mesa_validate_DrawElementsInstanced(ctx
, mode
, count
, type
,
1147 indices
, numInstances
))
1151 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, 0, ~0,
1152 count
, type
, indices
,
1153 basevertex
, numInstances
, 0);
1158 * Called by glDrawElementsInstancedBaseInstance() in immediate mode.
1160 static void GLAPIENTRY
1161 vbo_exec_DrawElementsInstancedBaseInstance(GLenum mode
, GLsizei count
,
1163 const GLvoid
*indices
,
1164 GLsizei numInstances
,
1165 GLuint baseInstance
)
1167 GET_CURRENT_CONTEXT(ctx
);
1169 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1171 "glDrawElementsInstancedBaseInstance"
1172 "(%s, %d, %s, %p, %d, %d)\n",
1173 _mesa_enum_to_string(mode
), count
,
1174 _mesa_enum_to_string(type
), indices
,
1175 numInstances
, baseInstance
);
1177 if (_mesa_is_no_error_enabled(ctx
)) {
1178 FLUSH_CURRENT(ctx
, 0);
1181 _mesa_update_state(ctx
);
1183 if (!_mesa_validate_DrawElementsInstanced(ctx
, mode
, count
, type
,
1184 indices
, numInstances
))
1188 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, 0, ~0,
1189 count
, type
, indices
, 0, numInstances
,
1195 * Called by glDrawElementsInstancedBaseVertexBaseInstance() in immediate mode.
1197 static void GLAPIENTRY
1198 vbo_exec_DrawElementsInstancedBaseVertexBaseInstance(GLenum mode
,
1201 const GLvoid
*indices
,
1202 GLsizei numInstances
,
1204 GLuint baseInstance
)
1206 GET_CURRENT_CONTEXT(ctx
);
1208 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1210 "glDrawElementsInstancedBaseVertexBaseInstance"
1211 "(%s, %d, %s, %p, %d, %d, %d)\n",
1212 _mesa_enum_to_string(mode
), count
,
1213 _mesa_enum_to_string(type
), indices
,
1214 numInstances
, basevertex
, baseInstance
);
1216 if (_mesa_is_no_error_enabled(ctx
)) {
1217 FLUSH_CURRENT(ctx
, 0);
1220 _mesa_update_state(ctx
);
1222 if (!_mesa_validate_DrawElementsInstanced(ctx
, mode
, count
, type
,
1223 indices
, numInstances
))
1227 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, 0, ~0,
1228 count
, type
, indices
, basevertex
,
1229 numInstances
, baseInstance
);
1234 * Inner support for both _mesa_MultiDrawElements() and
1235 * _mesa_MultiDrawRangeElements().
1236 * This does the actual rendering after we've checked array indexes, etc.
1239 vbo_validated_multidrawelements(struct gl_context
*ctx
, GLenum mode
,
1240 const GLsizei
*count
, GLenum type
,
1241 const GLvoid
* const *indices
,
1242 GLsizei primcount
, const GLint
*basevertex
)
1244 struct vbo_context
*vbo
= vbo_context(ctx
);
1245 struct _mesa_index_buffer ib
;
1246 struct _mesa_prim
*prim
;
1247 unsigned int index_type_size
= sizeof_ib_type(type
);
1248 uintptr_t min_index_ptr
, max_index_ptr
;
1249 GLboolean fallback
= GL_FALSE
;
1255 prim
= calloc(primcount
, sizeof(*prim
));
1257 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glMultiDrawElements");
1261 vbo_bind_arrays(ctx
);
1263 min_index_ptr
= (uintptr_t) indices
[0];
1265 for (i
= 0; i
< primcount
; i
++) {
1266 min_index_ptr
= MIN2(min_index_ptr
, (uintptr_t) indices
[i
]);
1267 max_index_ptr
= MAX2(max_index_ptr
, (uintptr_t) indices
[i
] +
1268 index_type_size
* count
[i
]);
1271 /* Check if we can handle this thing as a bunch of index offsets from the
1272 * same index pointer. If we can't, then we have to fall back to doing
1273 * a draw_prims per primitive.
1274 * Check that the difference between each prim's indexes is a multiple of
1275 * the index/element size.
1277 if (index_type_size
!= 1) {
1278 for (i
= 0; i
< primcount
; i
++) {
1279 if ((((uintptr_t) indices
[i
] - min_index_ptr
) % index_type_size
) !=
1287 /* Draw primitives individually if one count is zero, so we can easily skip
1290 for (i
= 0; i
< primcount
; i
++) {
1291 if (count
[i
] == 0) {
1297 /* If the index buffer isn't in a VBO, then treating the application's
1298 * subranges of the index buffer as one large index buffer may lead to
1299 * us reading unmapped memory.
1301 if (!_mesa_is_bufferobj(ctx
->Array
.VAO
->IndexBufferObj
))
1305 ib
.count
= (max_index_ptr
- min_index_ptr
) / index_type_size
;
1306 ib
.index_size
= sizeof_ib_type(type
);
1307 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
1308 ib
.ptr
= (void *) min_index_ptr
;
1310 for (i
= 0; i
< primcount
; i
++) {
1311 prim
[i
].begin
= (i
== 0);
1312 prim
[i
].end
= (i
== primcount
- 1);
1315 prim
[i
].mode
= mode
;
1317 ((uintptr_t) indices
[i
] - min_index_ptr
) / index_type_size
;
1318 prim
[i
].count
= count
[i
];
1319 prim
[i
].indexed
= 1;
1320 prim
[i
].num_instances
= 1;
1321 prim
[i
].base_instance
= 0;
1322 prim
[i
].draw_id
= i
;
1323 prim
[i
].is_indirect
= 0;
1324 if (basevertex
!= NULL
)
1325 prim
[i
].basevertex
= basevertex
[i
];
1327 prim
[i
].basevertex
= 0;
1330 vbo
->draw_prims(ctx
, prim
, primcount
, &ib
,
1331 false, 0, ~0, NULL
, 0, NULL
);
1334 /* render one prim at a time */
1335 for (i
= 0; i
< primcount
; i
++) {
1338 ib
.count
= count
[i
];
1339 ib
.index_size
= sizeof_ib_type(type
);
1340 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
1341 ib
.ptr
= indices
[i
];
1347 prim
[0].mode
= mode
;
1349 prim
[0].count
= count
[i
];
1350 prim
[0].indexed
= 1;
1351 prim
[0].num_instances
= 1;
1352 prim
[0].base_instance
= 0;
1353 prim
[0].draw_id
= i
;
1354 prim
[0].is_indirect
= 0;
1355 if (basevertex
!= NULL
)
1356 prim
[0].basevertex
= basevertex
[i
];
1358 prim
[0].basevertex
= 0;
1360 vbo
->draw_prims(ctx
, prim
, 1, &ib
, false, 0, ~0, NULL
, 0, NULL
);
1366 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
) {
1372 static void GLAPIENTRY
1373 vbo_exec_MultiDrawElements(GLenum mode
,
1374 const GLsizei
*count
, GLenum type
,
1375 const GLvoid
* const *indices
, GLsizei primcount
)
1377 GET_CURRENT_CONTEXT(ctx
);
1379 if (!_mesa_validate_MultiDrawElements(ctx
, mode
, count
, type
, indices
,
1383 if (skip_validated_draw(ctx
))
1386 vbo_validated_multidrawelements(ctx
, mode
, count
, type
, indices
, primcount
,
1391 static void GLAPIENTRY
1392 vbo_exec_MultiDrawElementsBaseVertex(GLenum mode
,
1393 const GLsizei
*count
, GLenum type
,
1394 const GLvoid
* const *indices
,
1396 const GLsizei
*basevertex
)
1398 GET_CURRENT_CONTEXT(ctx
);
1400 if (_mesa_is_no_error_enabled(ctx
)) {
1401 FLUSH_CURRENT(ctx
, 0);
1404 _mesa_update_state(ctx
);
1406 if (!_mesa_validate_MultiDrawElements(ctx
, mode
, count
, type
, indices
,
1411 if (skip_validated_draw(ctx
))
1414 vbo_validated_multidrawelements(ctx
, mode
, count
, type
, indices
, primcount
,
1420 vbo_draw_transform_feedback(struct gl_context
*ctx
, GLenum mode
,
1421 struct gl_transform_feedback_object
*obj
,
1422 GLuint stream
, GLuint numInstances
)
1424 struct vbo_context
*vbo
= vbo_context(ctx
);
1425 struct _mesa_prim prim
;
1427 if (_mesa_is_no_error_enabled(ctx
)) {
1428 FLUSH_CURRENT(ctx
, 0);
1431 _mesa_update_state(ctx
);
1433 if (!_mesa_validate_DrawTransformFeedback(ctx
, mode
, obj
, stream
,
1439 if (ctx
->Driver
.GetTransformFeedbackVertexCount
&&
1440 (ctx
->Const
.AlwaysUseGetTransformFeedbackVertexCount
||
1441 !_mesa_all_varyings_in_vbos(ctx
->Array
.VAO
))) {
1443 ctx
->Driver
.GetTransformFeedbackVertexCount(ctx
, obj
, stream
);
1444 vbo_draw_arrays(ctx
, mode
, 0, n
, numInstances
, 0, 0);
1448 if (skip_validated_draw(ctx
))
1451 vbo_bind_arrays(ctx
);
1453 /* init most fields to zero */
1454 memset(&prim
, 0, sizeof(prim
));
1458 prim
.num_instances
= numInstances
;
1459 prim
.base_instance
= 0;
1460 prim
.is_indirect
= 0;
1462 /* Maybe we should do some primitive splitting for primitive restart
1463 * (like in DrawArrays), but we have no way to know how many vertices
1464 * will be rendered. */
1466 vbo
->draw_prims(ctx
, &prim
, 1, NULL
, GL_FALSE
, 0, ~0, obj
, stream
, NULL
);
1468 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
) {
1475 * Like DrawArrays, but take the count from a transform feedback object.
1476 * \param mode GL_POINTS, GL_LINES, GL_TRIANGLE_STRIP, etc.
1477 * \param name the transform feedback object
1478 * User still has to setup of the vertex attribute info with
1479 * glVertexPointer, glColorPointer, etc.
1480 * Part of GL_ARB_transform_feedback2.
1482 static void GLAPIENTRY
1483 vbo_exec_DrawTransformFeedback(GLenum mode
, GLuint name
)
1485 GET_CURRENT_CONTEXT(ctx
);
1486 struct gl_transform_feedback_object
*obj
=
1487 _mesa_lookup_transform_feedback_object(ctx
, name
);
1489 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1490 _mesa_debug(ctx
, "glDrawTransformFeedback(%s, %d)\n",
1491 _mesa_enum_to_string(mode
), name
);
1493 vbo_draw_transform_feedback(ctx
, mode
, obj
, 0, 1);
1497 static void GLAPIENTRY
1498 vbo_exec_DrawTransformFeedbackStream(GLenum mode
, GLuint name
, GLuint stream
)
1500 GET_CURRENT_CONTEXT(ctx
);
1501 struct gl_transform_feedback_object
*obj
=
1502 _mesa_lookup_transform_feedback_object(ctx
, name
);
1504 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1505 _mesa_debug(ctx
, "glDrawTransformFeedbackStream(%s, %u, %u)\n",
1506 _mesa_enum_to_string(mode
), name
, stream
);
1508 vbo_draw_transform_feedback(ctx
, mode
, obj
, stream
, 1);
1512 static void GLAPIENTRY
1513 vbo_exec_DrawTransformFeedbackInstanced(GLenum mode
, GLuint name
,
1516 GET_CURRENT_CONTEXT(ctx
);
1517 struct gl_transform_feedback_object
*obj
=
1518 _mesa_lookup_transform_feedback_object(ctx
, name
);
1520 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1521 _mesa_debug(ctx
, "glDrawTransformFeedbackInstanced(%s, %d)\n",
1522 _mesa_enum_to_string(mode
), name
);
1524 vbo_draw_transform_feedback(ctx
, mode
, obj
, 0, primcount
);
1528 static void GLAPIENTRY
1529 vbo_exec_DrawTransformFeedbackStreamInstanced(GLenum mode
, GLuint name
,
1533 GET_CURRENT_CONTEXT(ctx
);
1534 struct gl_transform_feedback_object
*obj
=
1535 _mesa_lookup_transform_feedback_object(ctx
, name
);
1537 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1538 _mesa_debug(ctx
, "glDrawTransformFeedbackStreamInstanced"
1539 "(%s, %u, %u, %i)\n",
1540 _mesa_enum_to_string(mode
), name
, stream
, primcount
);
1542 vbo_draw_transform_feedback(ctx
, mode
, obj
, stream
, primcount
);
1547 vbo_validated_drawarraysindirect(struct gl_context
*ctx
,
1548 GLenum mode
, const GLvoid
*indirect
)
1550 struct vbo_context
*vbo
= vbo_context(ctx
);
1552 vbo_bind_arrays(ctx
);
1554 vbo
->draw_indirect_prims(ctx
, mode
,
1555 ctx
->DrawIndirectBuffer
, (GLsizeiptr
) indirect
,
1556 1 /* draw_count */ , 16 /* stride */ ,
1559 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1565 vbo_validated_multidrawarraysindirect(struct gl_context
*ctx
,
1567 const GLvoid
*indirect
,
1568 GLsizei primcount
, GLsizei stride
)
1570 struct vbo_context
*vbo
= vbo_context(ctx
);
1571 GLsizeiptr offset
= (GLsizeiptr
) indirect
;
1576 vbo_bind_arrays(ctx
);
1578 vbo
->draw_indirect_prims(ctx
, mode
, ctx
->DrawIndirectBuffer
, offset
,
1579 primcount
, stride
, NULL
, 0, NULL
);
1581 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1587 vbo_validated_drawelementsindirect(struct gl_context
*ctx
,
1588 GLenum mode
, GLenum type
,
1589 const GLvoid
*indirect
)
1591 struct vbo_context
*vbo
= vbo_context(ctx
);
1592 struct _mesa_index_buffer ib
;
1594 vbo_bind_arrays(ctx
);
1596 ib
.count
= 0; /* unknown */
1597 ib
.index_size
= sizeof_ib_type(type
);
1598 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
1601 vbo
->draw_indirect_prims(ctx
, mode
,
1602 ctx
->DrawIndirectBuffer
, (GLsizeiptr
) indirect
,
1603 1 /* draw_count */ , 20 /* stride */ ,
1606 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1612 vbo_validated_multidrawelementsindirect(struct gl_context
*ctx
,
1613 GLenum mode
, GLenum type
,
1614 const GLvoid
*indirect
,
1615 GLsizei primcount
, GLsizei stride
)
1617 struct vbo_context
*vbo
= vbo_context(ctx
);
1618 struct _mesa_index_buffer ib
;
1619 GLsizeiptr offset
= (GLsizeiptr
) indirect
;
1624 vbo_bind_arrays(ctx
);
1626 /* NOTE: IndexBufferObj is guaranteed to be a VBO. */
1628 ib
.count
= 0; /* unknown */
1629 ib
.index_size
= sizeof_ib_type(type
);
1630 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
1633 vbo
->draw_indirect_prims(ctx
, mode
,
1634 ctx
->DrawIndirectBuffer
, offset
,
1635 primcount
, stride
, NULL
, 0, &ib
);
1637 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1643 * Like [Multi]DrawArrays/Elements, but they take most arguments from
1646 static void GLAPIENTRY
1647 vbo_exec_DrawArraysIndirect(GLenum mode
, const GLvoid
*indirect
)
1649 GET_CURRENT_CONTEXT(ctx
);
1651 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1652 _mesa_debug(ctx
, "glDrawArraysIndirect(%s, %p)\n",
1653 _mesa_enum_to_string(mode
), indirect
);
1655 if (_mesa_is_no_error_enabled(ctx
)) {
1656 FLUSH_CURRENT(ctx
, 0);
1659 _mesa_update_state(ctx
);
1661 if (!_mesa_validate_DrawArraysIndirect(ctx
, mode
, indirect
))
1665 if (skip_validated_draw(ctx
))
1668 vbo_validated_drawarraysindirect(ctx
, mode
, indirect
);
1672 static void GLAPIENTRY
1673 vbo_exec_DrawElementsIndirect(GLenum mode
, GLenum type
, const GLvoid
*indirect
)
1675 GET_CURRENT_CONTEXT(ctx
);
1677 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1678 _mesa_debug(ctx
, "glDrawElementsIndirect(%s, %s, %p)\n",
1679 _mesa_enum_to_string(mode
),
1680 _mesa_enum_to_string(type
), indirect
);
1682 if (_mesa_is_no_error_enabled(ctx
)) {
1683 FLUSH_CURRENT(ctx
, 0);
1686 _mesa_update_state(ctx
);
1688 if (!_mesa_validate_DrawElementsIndirect(ctx
, mode
, type
, indirect
))
1692 if (skip_validated_draw(ctx
))
1695 vbo_validated_drawelementsindirect(ctx
, mode
, type
, indirect
);
1699 static void GLAPIENTRY
1700 vbo_exec_MultiDrawArraysIndirect(GLenum mode
, const GLvoid
*indirect
,
1701 GLsizei primcount
, GLsizei stride
)
1703 GET_CURRENT_CONTEXT(ctx
);
1705 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1706 _mesa_debug(ctx
, "glMultiDrawArraysIndirect(%s, %p, %i, %i)\n",
1707 _mesa_enum_to_string(mode
), indirect
, primcount
, stride
);
1709 /* If <stride> is zero, the array elements are treated as tightly packed. */
1711 stride
= 4 * sizeof(GLuint
); /* sizeof(DrawArraysIndirectCommand) */
1713 if (_mesa_is_no_error_enabled(ctx
)) {
1714 FLUSH_CURRENT(ctx
, 0);
1717 _mesa_update_state(ctx
);
1719 if (!_mesa_validate_MultiDrawArraysIndirect(ctx
, mode
, indirect
,
1724 if (skip_validated_draw(ctx
))
1727 vbo_validated_multidrawarraysindirect(ctx
, mode
, indirect
,
1732 static void GLAPIENTRY
1733 vbo_exec_MultiDrawElementsIndirect(GLenum mode
, GLenum type
,
1734 const GLvoid
*indirect
,
1735 GLsizei primcount
, GLsizei stride
)
1737 GET_CURRENT_CONTEXT(ctx
);
1739 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1740 _mesa_debug(ctx
, "glMultiDrawElementsIndirect(%s, %s, %p, %i, %i)\n",
1741 _mesa_enum_to_string(mode
),
1742 _mesa_enum_to_string(type
), indirect
, primcount
, stride
);
1744 /* If <stride> is zero, the array elements are treated as tightly packed. */
1746 stride
= 5 * sizeof(GLuint
); /* sizeof(DrawElementsIndirectCommand) */
1748 if (_mesa_is_no_error_enabled(ctx
)) {
1749 FLUSH_CURRENT(ctx
, 0);
1752 _mesa_update_state(ctx
);
1754 if (!_mesa_validate_MultiDrawElementsIndirect(ctx
, mode
, type
, indirect
,
1759 if (skip_validated_draw(ctx
))
1762 vbo_validated_multidrawelementsindirect(ctx
, mode
, type
, indirect
,
1768 vbo_validated_multidrawarraysindirectcount(struct gl_context
*ctx
,
1771 GLintptr drawcount_offset
,
1772 GLsizei maxdrawcount
,
1775 struct vbo_context
*vbo
= vbo_context(ctx
);
1776 GLsizeiptr offset
= indirect
;
1778 if (maxdrawcount
== 0)
1781 vbo_bind_arrays(ctx
);
1783 vbo
->draw_indirect_prims(ctx
, mode
,
1784 ctx
->DrawIndirectBuffer
, offset
,
1785 maxdrawcount
, stride
,
1786 ctx
->ParameterBuffer
, drawcount_offset
, NULL
);
1788 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1794 vbo_validated_multidrawelementsindirectcount(struct gl_context
*ctx
,
1795 GLenum mode
, GLenum type
,
1797 GLintptr drawcount_offset
,
1798 GLsizei maxdrawcount
,
1801 struct vbo_context
*vbo
= vbo_context(ctx
);
1802 struct _mesa_index_buffer ib
;
1803 GLsizeiptr offset
= (GLsizeiptr
) indirect
;
1805 if (maxdrawcount
== 0)
1808 vbo_bind_arrays(ctx
);
1810 /* NOTE: IndexBufferObj is guaranteed to be a VBO. */
1812 ib
.count
= 0; /* unknown */
1813 ib
.index_size
= sizeof_ib_type(type
);
1814 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
1817 vbo
->draw_indirect_prims(ctx
, mode
,
1818 ctx
->DrawIndirectBuffer
, offset
,
1819 maxdrawcount
, stride
,
1820 ctx
->ParameterBuffer
, drawcount_offset
, &ib
);
1822 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1827 static void GLAPIENTRY
1828 vbo_exec_MultiDrawArraysIndirectCount(GLenum mode
, GLintptr indirect
,
1829 GLintptr drawcount_offset
,
1830 GLsizei maxdrawcount
, GLsizei stride
)
1832 GET_CURRENT_CONTEXT(ctx
);
1834 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1835 _mesa_debug(ctx
, "glMultiDrawArraysIndirectCountARB"
1836 "(%s, %lx, %lx, %i, %i)\n",
1837 _mesa_enum_to_string(mode
),
1838 (unsigned long) indirect
, (unsigned long) drawcount_offset
,
1839 maxdrawcount
, stride
);
1841 /* If <stride> is zero, the array elements are treated as tightly packed. */
1843 stride
= 4 * sizeof(GLuint
); /* sizeof(DrawArraysIndirectCommand) */
1845 if (_mesa_is_no_error_enabled(ctx
)) {
1846 FLUSH_CURRENT(ctx
, 0);
1849 _mesa_update_state(ctx
);
1851 if (!_mesa_validate_MultiDrawArraysIndirectCount(ctx
, mode
,
1854 maxdrawcount
, stride
))
1858 if (skip_validated_draw(ctx
))
1861 vbo_validated_multidrawarraysindirectcount(ctx
, mode
, indirect
,
1863 maxdrawcount
, stride
);
1867 static void GLAPIENTRY
1868 vbo_exec_MultiDrawElementsIndirectCount(GLenum mode
, GLenum type
,
1870 GLintptr drawcount_offset
,
1871 GLsizei maxdrawcount
, GLsizei stride
)
1873 GET_CURRENT_CONTEXT(ctx
);
1875 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1876 _mesa_debug(ctx
, "glMultiDrawElementsIndirectCountARB"
1877 "(%s, %s, %lx, %lx, %i, %i)\n",
1878 _mesa_enum_to_string(mode
), _mesa_enum_to_string(type
),
1879 (unsigned long) indirect
, (unsigned long) drawcount_offset
,
1880 maxdrawcount
, stride
);
1882 /* If <stride> is zero, the array elements are treated as tightly packed. */
1884 stride
= 5 * sizeof(GLuint
); /* sizeof(DrawElementsIndirectCommand) */
1886 if (_mesa_is_no_error_enabled(ctx
)) {
1887 FLUSH_CURRENT(ctx
, 0);
1890 _mesa_update_state(ctx
);
1892 if (!_mesa_validate_MultiDrawElementsIndirectCount(ctx
, mode
, type
,
1895 maxdrawcount
, stride
))
1899 if (skip_validated_draw(ctx
))
1902 vbo_validated_multidrawelementsindirectcount(ctx
, mode
, type
, indirect
,
1903 drawcount_offset
, maxdrawcount
,
1909 * Initialize the dispatch table with the VBO functions for drawing.
1912 vbo_initialize_exec_dispatch(const struct gl_context
*ctx
,
1913 struct _glapi_table
*exec
)
1915 SET_DrawArrays(exec
, vbo_exec_DrawArrays
);
1916 SET_DrawElements(exec
, vbo_exec_DrawElements
);
1918 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
)) {
1919 SET_DrawRangeElements(exec
, vbo_exec_DrawRangeElements
);
1922 SET_MultiDrawArrays(exec
, vbo_exec_MultiDrawArrays
);
1923 SET_MultiDrawElementsEXT(exec
, vbo_exec_MultiDrawElements
);
1925 if (ctx
->API
== API_OPENGL_COMPAT
) {
1926 SET_Rectf(exec
, vbo_exec_Rectf
);
1927 SET_EvalMesh1(exec
, vbo_exec_EvalMesh1
);
1928 SET_EvalMesh2(exec
, vbo_exec_EvalMesh2
);
1931 if (ctx
->API
!= API_OPENGLES
&&
1932 ctx
->Extensions
.ARB_draw_elements_base_vertex
) {
1933 SET_DrawElementsBaseVertex(exec
, vbo_exec_DrawElementsBaseVertex
);
1934 SET_MultiDrawElementsBaseVertex(exec
,
1935 vbo_exec_MultiDrawElementsBaseVertex
);
1937 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
)) {
1938 SET_DrawRangeElementsBaseVertex(exec
,
1939 vbo_exec_DrawRangeElementsBaseVertex
);
1940 SET_DrawElementsInstancedBaseVertex(exec
,
1941 vbo_exec_DrawElementsInstancedBaseVertex
);
1945 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
)) {
1946 SET_DrawArraysInstancedBaseInstance(exec
,
1947 vbo_exec_DrawArraysInstancedBaseInstance
);
1948 SET_DrawElementsInstancedBaseInstance(exec
,
1949 vbo_exec_DrawElementsInstancedBaseInstance
);
1950 SET_DrawElementsInstancedBaseVertexBaseInstance(exec
,
1951 vbo_exec_DrawElementsInstancedBaseVertexBaseInstance
);
1954 if (ctx
->API
== API_OPENGL_CORE
|| _mesa_is_gles31(ctx
)) {
1955 SET_DrawArraysIndirect(exec
, vbo_exec_DrawArraysIndirect
);
1956 SET_DrawElementsIndirect(exec
, vbo_exec_DrawElementsIndirect
);
1959 if (ctx
->API
== API_OPENGL_CORE
) {
1960 SET_MultiDrawArraysIndirect(exec
, vbo_exec_MultiDrawArraysIndirect
);
1961 SET_MultiDrawElementsIndirect(exec
, vbo_exec_MultiDrawElementsIndirect
);
1962 SET_MultiDrawArraysIndirectCountARB(exec
,
1963 vbo_exec_MultiDrawArraysIndirectCount
);
1964 SET_MultiDrawElementsIndirectCountARB(exec
,
1965 vbo_exec_MultiDrawElementsIndirectCount
);
1968 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
)) {
1969 SET_DrawArraysInstancedARB(exec
, vbo_exec_DrawArraysInstanced
);
1970 SET_DrawElementsInstancedARB(exec
, vbo_exec_DrawElementsInstanced
);
1973 if (_mesa_is_desktop_gl(ctx
)) {
1974 SET_DrawTransformFeedback(exec
, vbo_exec_DrawTransformFeedback
);
1975 SET_DrawTransformFeedbackStream(exec
,
1976 vbo_exec_DrawTransformFeedbackStream
);
1977 SET_DrawTransformFeedbackInstanced(exec
,
1978 vbo_exec_DrawTransformFeedbackInstanced
);
1979 SET_DrawTransformFeedbackStreamInstanced(exec
,
1980 vbo_exec_DrawTransformFeedbackStreamInstanced
);
1987 * The following functions are only used for OpenGL ES 1/2 support.
1988 * And some aren't even supported (yet) in ES 1/2.
1993 _mesa_DrawArrays(GLenum mode
, GLint first
, GLsizei count
)
1995 vbo_exec_DrawArrays(mode
, first
, count
);
2000 _mesa_DrawArraysInstanced(GLenum mode
, GLint first
, GLsizei count
,
2003 vbo_exec_DrawArraysInstanced(mode
, first
, count
, primcount
);
2008 _mesa_DrawElements(GLenum mode
, GLsizei count
, GLenum type
,
2009 const GLvoid
*indices
)
2011 vbo_exec_DrawElements(mode
, count
, type
, indices
);
2016 _mesa_DrawElementsBaseVertex(GLenum mode
, GLsizei count
, GLenum type
,
2017 const GLvoid
*indices
, GLint basevertex
)
2019 vbo_exec_DrawElementsBaseVertex(mode
, count
, type
, indices
, basevertex
);
2024 _mesa_DrawRangeElements(GLenum mode
, GLuint start
, GLuint end
, GLsizei count
,
2025 GLenum type
, const GLvoid
* indices
)
2027 vbo_exec_DrawRangeElements(mode
, start
, end
, count
, type
, indices
);
2032 _mesa_DrawRangeElementsBaseVertex(GLenum mode
, GLuint start
, GLuint end
,
2033 GLsizei count
, GLenum type
,
2034 const GLvoid
*indices
, GLint basevertex
)
2036 vbo_exec_DrawRangeElementsBaseVertex(mode
, start
, end
, count
, type
,
2037 indices
, basevertex
);
2042 _mesa_MultiDrawElementsEXT(GLenum mode
, const GLsizei
*count
, GLenum type
,
2043 const GLvoid
** indices
, GLsizei primcount
)
2045 vbo_exec_MultiDrawElements(mode
, count
, type
, indices
, primcount
);
2050 _mesa_MultiDrawElementsBaseVertex(GLenum mode
,
2051 const GLsizei
*count
, GLenum type
,
2052 const GLvoid
**indices
, GLsizei primcount
,
2053 const GLint
*basevertex
)
2055 vbo_exec_MultiDrawElementsBaseVertex(mode
, count
, type
, indices
,
2056 primcount
, basevertex
);
2061 _mesa_DrawTransformFeedback(GLenum mode
, GLuint name
)
2063 vbo_exec_DrawTransformFeedback(mode
, name
);