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 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
);
287 float *f
= (float *) (p
+ offset
);
290 int n
= (count
* binding
->Stride
) / 4;
293 printf(" Data at offset %d:\n", offset
);
294 for (i
= 0; i
< n
; i
++) {
295 printf(" float[%d] = 0x%08x %f\n", i
, k
[i
], f
[i
]);
297 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, MAP_INTERNAL
);
304 * Set the vbo->exec->inputs[] pointers to point to the enabled
305 * vertex arrays. This depends on the current vertex program/shader
306 * being executed because of whether or not generic vertex arrays
307 * alias the conventional vertex arrays.
308 * For arrays that aren't enabled, we set the input[attrib] pointer
309 * to point at a zero-stride current value "array".
312 recalculate_input_bindings(struct gl_context
*ctx
)
314 struct vbo_context
*vbo
= vbo_context(ctx
);
315 struct vbo_exec_context
*exec
= &vbo
->exec
;
316 const struct gl_array_attributes
*array
= ctx
->Array
.VAO
->VertexAttrib
;
317 struct gl_vertex_array
*vertexAttrib
= ctx
->Array
.VAO
->_VertexAttrib
;
318 const struct gl_vertex_array
**inputs
= &exec
->array
.inputs
[0];
319 GLbitfield const_inputs
= 0x0;
322 switch (get_program_mode(ctx
)) {
324 /* When no vertex program is active (or the vertex program is generated
325 * from fixed-function state). We put the material values into the
326 * generic slots. This is the only situation where material values
327 * are available as per-vertex attributes.
329 for (i
= 0; i
< VERT_ATTRIB_FF_MAX
; i
++) {
330 if (array
[VERT_ATTRIB_FF(i
)].Enabled
)
331 inputs
[i
] = &vertexAttrib
[VERT_ATTRIB_FF(i
)];
333 inputs
[i
] = &vbo
->currval
[VBO_ATTRIB_POS
+ i
];
334 const_inputs
|= VERT_BIT(i
);
338 for (i
= 0; i
< MAT_ATTRIB_MAX
; i
++) {
339 inputs
[VERT_ATTRIB_GENERIC(i
)] =
340 &vbo
->currval
[VBO_ATTRIB_MAT_FRONT_AMBIENT
+ i
];
341 const_inputs
|= VERT_BIT_GENERIC(i
);
344 /* Could use just about anything, just to fill in the empty
347 for (i
= MAT_ATTRIB_MAX
; i
< VERT_ATTRIB_GENERIC_MAX
; i
++) {
348 inputs
[VERT_ATTRIB_GENERIC(i
)] =
349 &vbo
->currval
[VBO_ATTRIB_GENERIC0
+ i
];
350 const_inputs
|= VERT_BIT_GENERIC(i
);
355 /* There are no shaders in OpenGL ES 1.x, so this code path should be
356 * impossible to reach. The meta code is careful to not use shaders in
359 assert(ctx
->API
!= API_OPENGLES
);
361 /* In the compatibility profile of desktop OpenGL, the generic[0]
362 * attribute array aliases and overrides the legacy position array.
363 * Otherwise, legacy attributes available in the legacy slots,
364 * generic attributes in the generic slots and materials are not
365 * available as per-vertex attributes.
367 * In all other APIs, only the generic attributes exist, and none of the
368 * slots are considered "magic."
370 if (ctx
->API
== API_OPENGL_COMPAT
) {
371 if (array
[VERT_ATTRIB_GENERIC0
].Enabled
)
372 inputs
[0] = &vertexAttrib
[VERT_ATTRIB_GENERIC0
];
373 else if (array
[VERT_ATTRIB_POS
].Enabled
)
374 inputs
[0] = &vertexAttrib
[VERT_ATTRIB_POS
];
376 inputs
[0] = &vbo
->currval
[VBO_ATTRIB_GENERIC0
];
377 const_inputs
|= VERT_BIT_POS
;
380 for (i
= 1; i
< VERT_ATTRIB_FF_MAX
; i
++) {
381 if (array
[VERT_ATTRIB_FF(i
)].Enabled
)
382 inputs
[i
] = &vertexAttrib
[VERT_ATTRIB_FF(i
)];
384 inputs
[i
] = &vbo
->currval
[VBO_ATTRIB_POS
+ i
];
385 const_inputs
|= VERT_BIT_FF(i
);
389 for (i
= 1; i
< VERT_ATTRIB_GENERIC_MAX
; i
++) {
390 if (array
[VERT_ATTRIB_GENERIC(i
)].Enabled
)
391 inputs
[VERT_ATTRIB_GENERIC(i
)] =
392 &vertexAttrib
[VERT_ATTRIB_GENERIC(i
)];
394 inputs
[VERT_ATTRIB_GENERIC(i
)] =
395 &vbo
->currval
[VBO_ATTRIB_GENERIC0
+ i
];
396 const_inputs
|= VERT_BIT_GENERIC(i
);
400 inputs
[VERT_ATTRIB_GENERIC0
] = inputs
[0];
402 /* Other parts of the code assume that inputs[0] through
403 * inputs[VERT_ATTRIB_FF_MAX] will be non-NULL. However, in OpenGL
404 * ES 2.0+ or OpenGL core profile, none of these arrays should ever
407 for (i
= 0; i
< VERT_ATTRIB_FF_MAX
; i
++) {
408 assert(!array
[VERT_ATTRIB_FF(i
)].Enabled
);
410 inputs
[i
] = &vbo
->currval
[VBO_ATTRIB_POS
+ i
];
411 const_inputs
|= VERT_BIT_FF(i
);
414 for (i
= 0; i
< VERT_ATTRIB_GENERIC_MAX
; i
++) {
415 if (array
[VERT_ATTRIB_GENERIC(i
)].Enabled
)
416 inputs
[VERT_ATTRIB_GENERIC(i
)] =
417 &vertexAttrib
[VERT_ATTRIB_GENERIC(i
)];
419 inputs
[VERT_ATTRIB_GENERIC(i
)] =
420 &vbo
->currval
[VBO_ATTRIB_GENERIC0
+ i
];
421 const_inputs
|= VERT_BIT_GENERIC(i
);
429 _mesa_set_varying_vp_inputs(ctx
, VERT_BIT_ALL
& (~const_inputs
));
430 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewArray
;
435 * Examine the enabled vertex arrays to set the exec->array.inputs[] values.
436 * These will point to the arrays to actually use for drawing. Some will
437 * be user-provided arrays, other will be zero-stride const-valued arrays.
438 * Note that this might set the _NEW_VARYING_VP_INPUTS dirty flag so state
439 * validation must be done after this call.
442 vbo_bind_arrays(struct gl_context
*ctx
)
444 struct vbo_context
*vbo
= vbo_context(ctx
);
445 struct vbo_exec_context
*exec
= &vbo
->exec
;
447 vbo_draw_method(vbo
, DRAW_ARRAYS
);
449 if (exec
->array
.recalculate_inputs
) {
450 recalculate_input_bindings(ctx
);
451 exec
->array
.recalculate_inputs
= GL_FALSE
;
453 /* Again... because we may have changed the bitmask of per-vertex varying
454 * attributes. If we regenerate the fixed-function vertex program now
455 * we may be able to prune down the number of vertex attributes which we
456 * need in the shader.
459 /* Setting "validating" to TRUE prevents _mesa_update_state from
460 * invalidating what we just did.
462 exec
->validating
= GL_TRUE
;
463 _mesa_update_state(ctx
);
464 exec
->validating
= GL_FALSE
;
471 * Helper function called by the other DrawArrays() functions below.
472 * This is where we handle primitive restart for drawing non-indexed
473 * arrays. If primitive restart is enabled, it typically means
474 * splitting one DrawArrays() into two.
477 vbo_draw_arrays(struct gl_context
*ctx
, GLenum mode
, GLint start
,
478 GLsizei count
, GLuint numInstances
, GLuint baseInstance
,
481 struct vbo_context
*vbo
= vbo_context(ctx
);
482 struct _mesa_prim prim
;
484 if (skip_validated_draw(ctx
))
487 vbo_bind_arrays(ctx
);
489 /* OpenGL 4.5 says that primitive restart is ignored with non-indexed
492 memset(&prim
, 0, sizeof(prim
));
496 prim
.num_instances
= numInstances
;
497 prim
.base_instance
= baseInstance
;
498 prim
.draw_id
= drawID
;
499 prim
.is_indirect
= 0;
503 vbo
->draw_prims(ctx
, &prim
, 1, NULL
,
504 GL_TRUE
, start
, start
+ count
- 1, NULL
, 0, NULL
);
506 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
) {
513 * Execute a glRectf() function.
515 static void GLAPIENTRY
516 vbo_exec_Rectf(GLfloat x1
, GLfloat y1
, GLfloat x2
, GLfloat y2
)
518 GET_CURRENT_CONTEXT(ctx
);
519 ASSERT_OUTSIDE_BEGIN_END(ctx
);
521 CALL_Begin(GET_DISPATCH(), (GL_QUADS
));
522 CALL_Vertex2f(GET_DISPATCH(), (x1
, y1
));
523 CALL_Vertex2f(GET_DISPATCH(), (x2
, y1
));
524 CALL_Vertex2f(GET_DISPATCH(), (x2
, y2
));
525 CALL_Vertex2f(GET_DISPATCH(), (x1
, y2
));
526 CALL_End(GET_DISPATCH(), ());
530 static void GLAPIENTRY
531 vbo_exec_EvalMesh1(GLenum mode
, GLint i1
, GLint i2
)
533 GET_CURRENT_CONTEXT(ctx
);
543 prim
= GL_LINE_STRIP
;
546 _mesa_error(ctx
, GL_INVALID_ENUM
, "glEvalMesh1(mode)");
550 /* No effect if vertex maps disabled.
552 if (!ctx
->Eval
.Map1Vertex4
&& !ctx
->Eval
.Map1Vertex3
)
555 du
= ctx
->Eval
.MapGrid1du
;
556 u
= ctx
->Eval
.MapGrid1u1
+ i1
* du
;
558 CALL_Begin(GET_DISPATCH(), (prim
));
559 for (i
= i1
; i
<= i2
; i
++, u
+= du
) {
560 CALL_EvalCoord1f(GET_DISPATCH(), (u
));
562 CALL_End(GET_DISPATCH(), ());
566 static void GLAPIENTRY
567 vbo_exec_EvalMesh2(GLenum mode
, GLint i1
, GLint i2
, GLint j1
, GLint j2
)
569 GET_CURRENT_CONTEXT(ctx
);
570 GLfloat u
, du
, v
, dv
, v1
, u1
;
579 _mesa_error(ctx
, GL_INVALID_ENUM
, "glEvalMesh2(mode)");
583 /* No effect if vertex maps disabled.
585 if (!ctx
->Eval
.Map2Vertex4
&& !ctx
->Eval
.Map2Vertex3
)
588 du
= ctx
->Eval
.MapGrid2du
;
589 dv
= ctx
->Eval
.MapGrid2dv
;
590 v1
= ctx
->Eval
.MapGrid2v1
+ j1
* dv
;
591 u1
= ctx
->Eval
.MapGrid2u1
+ i1
* du
;
595 CALL_Begin(GET_DISPATCH(), (GL_POINTS
));
596 for (v
= v1
, j
= j1
; j
<= j2
; j
++, v
+= dv
) {
597 for (u
= u1
, i
= i1
; i
<= i2
; i
++, u
+= du
) {
598 CALL_EvalCoord2f(GET_DISPATCH(), (u
, v
));
601 CALL_End(GET_DISPATCH(), ());
604 for (v
= v1
, j
= j1
; j
<= j2
; j
++, v
+= dv
) {
605 CALL_Begin(GET_DISPATCH(), (GL_LINE_STRIP
));
606 for (u
= u1
, i
= i1
; i
<= i2
; i
++, u
+= du
) {
607 CALL_EvalCoord2f(GET_DISPATCH(), (u
, v
));
609 CALL_End(GET_DISPATCH(), ());
611 for (u
= u1
, i
= i1
; i
<= i2
; i
++, u
+= du
) {
612 CALL_Begin(GET_DISPATCH(), (GL_LINE_STRIP
));
613 for (v
= v1
, j
= j1
; j
<= j2
; j
++, v
+= dv
) {
614 CALL_EvalCoord2f(GET_DISPATCH(), (u
, v
));
616 CALL_End(GET_DISPATCH(), ());
620 for (v
= v1
, j
= j1
; j
< j2
; j
++, v
+= dv
) {
621 CALL_Begin(GET_DISPATCH(), (GL_TRIANGLE_STRIP
));
622 for (u
= u1
, i
= i1
; i
<= i2
; i
++, u
+= du
) {
623 CALL_EvalCoord2f(GET_DISPATCH(), (u
, v
));
624 CALL_EvalCoord2f(GET_DISPATCH(), (u
, v
+ dv
));
626 CALL_End(GET_DISPATCH(), ());
634 * Called from glDrawArrays when in immediate mode (not display list mode).
636 static void GLAPIENTRY
637 vbo_exec_DrawArrays(GLenum mode
, GLint start
, GLsizei count
)
639 GET_CURRENT_CONTEXT(ctx
);
641 if (MESA_VERBOSE
& VERBOSE_DRAW
)
642 _mesa_debug(ctx
, "glDrawArrays(%s, %d, %d)\n",
643 _mesa_enum_to_string(mode
), start
, count
);
645 if (_mesa_is_no_error_enabled(ctx
)) {
646 FLUSH_CURRENT(ctx
, 0);
649 _mesa_update_state(ctx
);
651 if (!_mesa_validate_DrawArrays(ctx
, mode
, count
))
656 check_draw_arrays_data(ctx
, start
, count
);
658 vbo_draw_arrays(ctx
, mode
, start
, count
, 1, 0, 0);
661 print_draw_arrays(ctx
, mode
, start
, count
);
666 * Called from glDrawArraysInstanced when in immediate mode (not
667 * display list mode).
669 static void GLAPIENTRY
670 vbo_exec_DrawArraysInstanced(GLenum mode
, GLint start
, GLsizei count
,
671 GLsizei numInstances
)
673 GET_CURRENT_CONTEXT(ctx
);
675 if (MESA_VERBOSE
& VERBOSE_DRAW
)
676 _mesa_debug(ctx
, "glDrawArraysInstanced(%s, %d, %d, %d)\n",
677 _mesa_enum_to_string(mode
), start
, count
, numInstances
);
680 if (_mesa_is_no_error_enabled(ctx
)) {
681 FLUSH_CURRENT(ctx
, 0);
684 _mesa_update_state(ctx
);
686 if (!_mesa_validate_DrawArraysInstanced(ctx
, mode
, start
, count
,
692 check_draw_arrays_data(ctx
, start
, count
);
694 vbo_draw_arrays(ctx
, mode
, start
, count
, numInstances
, 0, 0);
697 print_draw_arrays(ctx
, mode
, start
, count
);
702 * Called from glDrawArraysInstancedBaseInstance when in immediate mode.
704 static void GLAPIENTRY
705 vbo_exec_DrawArraysInstancedBaseInstance(GLenum mode
, GLint first
,
706 GLsizei count
, GLsizei numInstances
,
709 GET_CURRENT_CONTEXT(ctx
);
711 if (MESA_VERBOSE
& VERBOSE_DRAW
)
713 "glDrawArraysInstancedBaseInstance(%s, %d, %d, %d, %d)\n",
714 _mesa_enum_to_string(mode
), first
, count
,
715 numInstances
, baseInstance
);
717 if (_mesa_is_no_error_enabled(ctx
)) {
718 FLUSH_CURRENT(ctx
, 0);
721 _mesa_update_state(ctx
);
723 if (!_mesa_validate_DrawArraysInstanced(ctx
, mode
, first
, count
,
729 check_draw_arrays_data(ctx
, first
, count
);
731 vbo_draw_arrays(ctx
, mode
, first
, count
, numInstances
, baseInstance
, 0);
734 print_draw_arrays(ctx
, mode
, first
, count
);
739 * Called from glMultiDrawArrays when in immediate mode.
741 static void GLAPIENTRY
742 vbo_exec_MultiDrawArrays(GLenum mode
, const GLint
*first
,
743 const GLsizei
*count
, GLsizei primcount
)
745 GET_CURRENT_CONTEXT(ctx
);
748 if (MESA_VERBOSE
& VERBOSE_DRAW
)
750 "glMultiDrawArrays(%s, %p, %p, %d)\n",
751 _mesa_enum_to_string(mode
), first
, count
, primcount
);
753 if (_mesa_is_no_error_enabled(ctx
)) {
754 FLUSH_CURRENT(ctx
, 0);
757 _mesa_update_state(ctx
);
759 if (!_mesa_validate_MultiDrawArrays(ctx
, mode
, count
, primcount
))
763 for (i
= 0; i
< primcount
; i
++) {
766 check_draw_arrays_data(ctx
, first
[i
], count
[i
]);
768 /* The GL_ARB_shader_draw_parameters spec adds the following after the
769 * pseudo-code describing glMultiDrawArrays:
771 * "The index of the draw (<i> in the above pseudo-code) may be
772 * read by a vertex shader as <gl_DrawIDARB>, as described in
775 vbo_draw_arrays(ctx
, mode
, first
[i
], count
[i
], 1, 0, i
);
778 print_draw_arrays(ctx
, mode
, first
[i
], count
[i
]);
786 * Map GL_ELEMENT_ARRAY_BUFFER and print contents.
791 dump_element_buffer(struct gl_context
*ctx
, GLenum type
)
794 ctx
->Driver
.MapBufferRange(ctx
, 0,
795 ctx
->Array
.VAO
->IndexBufferObj
->Size
,
797 ctx
->Array
.VAO
->IndexBufferObj
,
800 case GL_UNSIGNED_BYTE
:
802 const GLubyte
*us
= (const GLubyte
*) map
;
804 for (i
= 0; i
< ctx
->Array
.VAO
->IndexBufferObj
->Size
; i
++) {
805 printf("%02x ", us
[i
]);
812 case GL_UNSIGNED_SHORT
:
814 const GLushort
*us
= (const GLushort
*) map
;
816 for (i
= 0; i
< ctx
->Array
.VAO
->IndexBufferObj
->Size
/ 2; i
++) {
817 printf("%04x ", us
[i
]);
824 case GL_UNSIGNED_INT
:
826 const GLuint
*us
= (const GLuint
*) map
;
828 for (i
= 0; i
< ctx
->Array
.VAO
->IndexBufferObj
->Size
/ 4; i
++) {
829 printf("%08x ", us
[i
]);
840 ctx
->Driver
.UnmapBuffer(ctx
, ctx
->Array
.VAO
->IndexBufferObj
, MAP_INTERNAL
);
846 skip_draw_elements(struct gl_context
*ctx
, GLsizei count
,
847 const GLvoid
*indices
)
852 /* Not using a VBO for indices, so avoid NULL pointer derefs later.
854 if (!_mesa_is_bufferobj(ctx
->Array
.VAO
->IndexBufferObj
) && indices
== NULL
)
857 if (skip_validated_draw(ctx
))
865 * Inner support for both _mesa_DrawElements and _mesa_DrawRangeElements.
866 * Do the rendering for a glDrawElements or glDrawRangeElements call after
867 * we've validated buffer bounds, etc.
870 vbo_validated_drawrangeelements(struct gl_context
*ctx
, GLenum mode
,
871 GLboolean index_bounds_valid
,
872 GLuint start
, GLuint end
,
873 GLsizei count
, GLenum type
,
874 const GLvoid
* indices
,
875 GLint basevertex
, GLuint numInstances
,
878 struct vbo_context
*vbo
= vbo_context(ctx
);
879 struct _mesa_index_buffer ib
;
880 struct _mesa_prim prim
;
882 if (!index_bounds_valid
) {
887 if (skip_draw_elements(ctx
, count
, indices
))
890 vbo_bind_arrays(ctx
);
893 ib
.index_size
= sizeof_ib_type(type
);
894 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
905 prim
.is_indirect
= 0;
906 prim
.basevertex
= basevertex
;
907 prim
.num_instances
= numInstances
;
908 prim
.base_instance
= baseInstance
;
911 /* Need to give special consideration to rendering a range of
912 * indices starting somewhere above zero. Typically the
913 * application is issuing multiple DrawRangeElements() to draw
914 * successive primitives layed out linearly in the vertex arrays.
915 * Unless the vertex arrays are all in a VBO (or locked as with
916 * CVA), the OpenGL semantics imply that we need to re-read or
917 * re-upload the vertex data on each draw call.
919 * In the case of hardware tnl, we want to avoid starting the
920 * upload at zero, as it will mean every draw call uploads an
921 * increasing amount of not-used vertex data. Worse - in the
922 * software tnl module, all those vertices might be transformed and
923 * lit but never rendered.
925 * If we just upload or transform the vertices in start..end,
926 * however, the indices will be incorrect.
928 * At this level, we don't know exactly what the requirements of
929 * the backend are going to be, though it will likely boil down to
932 * 1) Do nothing, everything is in a VBO and is processed once
935 * 2) Adjust the indices and vertex arrays so that start becomes
938 * Rather than doing anything here, I'll provide a helper function
939 * for the latter case elsewhere.
942 vbo
->draw_prims(ctx
, &prim
, 1, &ib
,
943 index_bounds_valid
, start
, end
, NULL
, 0, NULL
);
945 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
) {
952 * Called by glDrawRangeElementsBaseVertex() in immediate mode.
954 static void GLAPIENTRY
955 vbo_exec_DrawRangeElementsBaseVertex(GLenum mode
, GLuint start
, GLuint end
,
956 GLsizei count
, GLenum type
,
957 const GLvoid
* indices
, GLint basevertex
)
959 static GLuint warnCount
= 0;
960 GLboolean index_bounds_valid
= GL_TRUE
;
962 /* This is only useful to catch invalid values in the "end" parameter
965 GLuint max_element
= 2 * 1000 * 1000 * 1000; /* just a big number */
967 GET_CURRENT_CONTEXT(ctx
);
969 if (MESA_VERBOSE
& VERBOSE_DRAW
)
971 "glDrawRangeElementsBaseVertex(%s, %u, %u, %d, %s, %p, %d)\n",
972 _mesa_enum_to_string(mode
), start
, end
, count
,
973 _mesa_enum_to_string(type
), indices
, basevertex
);
975 if (_mesa_is_no_error_enabled(ctx
)) {
976 FLUSH_CURRENT(ctx
, 0);
979 _mesa_update_state(ctx
);
981 if (!_mesa_validate_DrawRangeElements(ctx
, mode
, start
, end
, count
,
986 if ((int) end
+ basevertex
< 0 || start
+ basevertex
>= max_element
) {
987 /* The application requested we draw using a range of indices that's
988 * outside the bounds of the current VBO. This is invalid and appears
989 * to give undefined results. The safest thing to do is to simply
990 * ignore the range, in case the application botched their range tracking
991 * but did provide valid indices. Also issue a warning indicating that
992 * the application is broken.
994 if (warnCount
++ < 10) {
995 _mesa_warning(ctx
, "glDrawRangeElements(start %u, end %u, "
996 "basevertex %d, count %d, type 0x%x, indices=%p):\n"
997 "\trange is outside VBO bounds (max=%u); ignoring.\n"
998 "\tThis should be fixed in the application.",
999 start
, end
, basevertex
, count
, type
, indices
,
1002 index_bounds_valid
= GL_FALSE
;
1005 /* NOTE: It's important that 'end' is a reasonable value.
1006 * in _tnl_draw_prims(), we use end to determine how many vertices
1007 * to transform. If it's too large, we can unnecessarily split prims
1008 * or we can read/write out of memory in several different places!
1011 /* Catch/fix some potential user errors */
1012 if (type
== GL_UNSIGNED_BYTE
) {
1013 start
= MIN2(start
, 0xff);
1014 end
= MIN2(end
, 0xff);
1016 else if (type
== GL_UNSIGNED_SHORT
) {
1017 start
= MIN2(start
, 0xffff);
1018 end
= MIN2(end
, 0xffff);
1022 printf("glDraw[Range]Elements{,BaseVertex}"
1023 "(start %u, end %u, type 0x%x, count %d) ElemBuf %u, "
1025 start
, end
, type
, count
,
1026 ctx
->Array
.VAO
->IndexBufferObj
->Name
, basevertex
);
1029 if ((int) start
+ basevertex
< 0 || end
+ basevertex
>= max_element
)
1030 index_bounds_valid
= GL_FALSE
;
1033 check_draw_elements_data(ctx
, count
, type
, indices
, basevertex
);
1035 (void) check_draw_elements_data
;
1038 if (!index_bounds_valid
) {
1043 vbo_validated_drawrangeelements(ctx
, mode
, index_bounds_valid
, start
, end
,
1044 count
, type
, indices
, basevertex
, 1, 0);
1049 * Called by glDrawRangeElements() in immediate mode.
1051 static void GLAPIENTRY
1052 vbo_exec_DrawRangeElements(GLenum mode
, GLuint start
, GLuint end
,
1053 GLsizei count
, GLenum type
, const GLvoid
* indices
)
1055 if (MESA_VERBOSE
& VERBOSE_DRAW
) {
1056 GET_CURRENT_CONTEXT(ctx
);
1058 "glDrawRangeElements(%s, %u, %u, %d, %s, %p)\n",
1059 _mesa_enum_to_string(mode
), start
, end
, count
,
1060 _mesa_enum_to_string(type
), indices
);
1063 vbo_exec_DrawRangeElementsBaseVertex(mode
, start
, end
, count
, type
,
1069 * Called by glDrawElements() in immediate mode.
1071 static void GLAPIENTRY
1072 vbo_exec_DrawElements(GLenum mode
, GLsizei count
, GLenum type
,
1073 const GLvoid
* indices
)
1075 GET_CURRENT_CONTEXT(ctx
);
1077 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1078 _mesa_debug(ctx
, "glDrawElements(%s, %u, %s, %p)\n",
1079 _mesa_enum_to_string(mode
), count
,
1080 _mesa_enum_to_string(type
), indices
);
1082 if (_mesa_is_no_error_enabled(ctx
)) {
1083 FLUSH_CURRENT(ctx
, 0);
1086 _mesa_update_state(ctx
);
1088 if (!_mesa_validate_DrawElements(ctx
, mode
, count
, type
, indices
))
1092 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, 0, ~0,
1093 count
, type
, indices
, 0, 1, 0);
1098 * Called by glDrawElementsBaseVertex() in immediate mode.
1100 static void GLAPIENTRY
1101 vbo_exec_DrawElementsBaseVertex(GLenum mode
, GLsizei count
, GLenum type
,
1102 const GLvoid
* indices
, GLint basevertex
)
1104 GET_CURRENT_CONTEXT(ctx
);
1106 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1107 _mesa_debug(ctx
, "glDrawElements(%s, %u, %s, %p)\n",
1108 _mesa_enum_to_string(mode
), count
,
1109 _mesa_enum_to_string(type
), indices
);
1111 if (_mesa_is_no_error_enabled(ctx
)) {
1112 FLUSH_CURRENT(ctx
, 0);
1115 _mesa_update_state(ctx
);
1117 if (!_mesa_validate_DrawElements(ctx
, mode
, count
, type
, indices
))
1121 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, 0, ~0,
1122 count
, type
, indices
, basevertex
, 1, 0);
1127 * Called by glDrawElementsInstanced() in immediate mode.
1129 static void GLAPIENTRY
1130 vbo_exec_DrawElementsInstanced(GLenum mode
, GLsizei count
, GLenum type
,
1131 const GLvoid
* indices
, GLsizei numInstances
)
1133 GET_CURRENT_CONTEXT(ctx
);
1135 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1136 _mesa_debug(ctx
, "glDrawElements(%s, %u, %s, %p)\n",
1137 _mesa_enum_to_string(mode
), count
,
1138 _mesa_enum_to_string(type
), indices
);
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
, 0, numInstances
, 0);
1157 * Called by glDrawElementsInstancedBaseVertex() in immediate mode.
1159 static void GLAPIENTRY
1160 vbo_exec_DrawElementsInstancedBaseVertex(GLenum mode
, GLsizei count
,
1161 GLenum type
, const GLvoid
* indices
,
1162 GLsizei numInstances
,
1165 GET_CURRENT_CONTEXT(ctx
);
1167 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1169 "glDrawElementsInstancedBaseVertex"
1170 "(%s, %d, %s, %p, %d; %d)\n",
1171 _mesa_enum_to_string(mode
), count
,
1172 _mesa_enum_to_string(type
), indices
,
1173 numInstances
, basevertex
);
1175 if (_mesa_is_no_error_enabled(ctx
)) {
1176 FLUSH_CURRENT(ctx
, 0);
1179 _mesa_update_state(ctx
);
1181 if (!_mesa_validate_DrawElementsInstanced(ctx
, mode
, count
, type
,
1182 indices
, numInstances
))
1186 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, 0, ~0,
1187 count
, type
, indices
,
1188 basevertex
, numInstances
, 0);
1193 * Called by glDrawElementsInstancedBaseInstance() in immediate mode.
1195 static void GLAPIENTRY
1196 vbo_exec_DrawElementsInstancedBaseInstance(GLenum mode
, GLsizei count
,
1198 const GLvoid
*indices
,
1199 GLsizei numInstances
,
1200 GLuint baseInstance
)
1202 GET_CURRENT_CONTEXT(ctx
);
1204 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1206 "glDrawElementsInstancedBaseInstance"
1207 "(%s, %d, %s, %p, %d, %d)\n",
1208 _mesa_enum_to_string(mode
), count
,
1209 _mesa_enum_to_string(type
), indices
,
1210 numInstances
, baseInstance
);
1212 if (_mesa_is_no_error_enabled(ctx
)) {
1213 FLUSH_CURRENT(ctx
, 0);
1216 _mesa_update_state(ctx
);
1218 if (!_mesa_validate_DrawElementsInstanced(ctx
, mode
, count
, type
,
1219 indices
, numInstances
))
1223 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, 0, ~0,
1224 count
, type
, indices
, 0, numInstances
,
1230 * Called by glDrawElementsInstancedBaseVertexBaseInstance() in immediate mode.
1232 static void GLAPIENTRY
1233 vbo_exec_DrawElementsInstancedBaseVertexBaseInstance(GLenum mode
,
1236 const GLvoid
*indices
,
1237 GLsizei numInstances
,
1239 GLuint baseInstance
)
1241 GET_CURRENT_CONTEXT(ctx
);
1243 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1245 "glDrawElementsInstancedBaseVertexBaseInstance"
1246 "(%s, %d, %s, %p, %d, %d, %d)\n",
1247 _mesa_enum_to_string(mode
), count
,
1248 _mesa_enum_to_string(type
), indices
,
1249 numInstances
, basevertex
, baseInstance
);
1251 if (_mesa_is_no_error_enabled(ctx
)) {
1252 FLUSH_CURRENT(ctx
, 0);
1255 _mesa_update_state(ctx
);
1257 if (!_mesa_validate_DrawElementsInstanced(ctx
, mode
, count
, type
,
1258 indices
, numInstances
))
1262 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, 0, ~0,
1263 count
, type
, indices
, basevertex
,
1264 numInstances
, baseInstance
);
1269 * Inner support for both _mesa_MultiDrawElements() and
1270 * _mesa_MultiDrawRangeElements().
1271 * This does the actual rendering after we've checked array indexes, etc.
1274 vbo_validated_multidrawelements(struct gl_context
*ctx
, GLenum mode
,
1275 const GLsizei
*count
, GLenum type
,
1276 const GLvoid
* const *indices
,
1277 GLsizei primcount
, const GLint
*basevertex
)
1279 struct vbo_context
*vbo
= vbo_context(ctx
);
1280 struct _mesa_index_buffer ib
;
1281 struct _mesa_prim
*prim
;
1282 unsigned int index_type_size
= sizeof_ib_type(type
);
1283 uintptr_t min_index_ptr
, max_index_ptr
;
1284 GLboolean fallback
= GL_FALSE
;
1290 prim
= calloc(primcount
, sizeof(*prim
));
1292 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glMultiDrawElements");
1296 vbo_bind_arrays(ctx
);
1298 min_index_ptr
= (uintptr_t) indices
[0];
1300 for (i
= 0; i
< primcount
; i
++) {
1301 min_index_ptr
= MIN2(min_index_ptr
, (uintptr_t) indices
[i
]);
1302 max_index_ptr
= MAX2(max_index_ptr
, (uintptr_t) indices
[i
] +
1303 index_type_size
* count
[i
]);
1306 /* Check if we can handle this thing as a bunch of index offsets from the
1307 * same index pointer. If we can't, then we have to fall back to doing
1308 * a draw_prims per primitive.
1309 * Check that the difference between each prim's indexes is a multiple of
1310 * the index/element size.
1312 if (index_type_size
!= 1) {
1313 for (i
= 0; i
< primcount
; i
++) {
1314 if ((((uintptr_t) indices
[i
] - min_index_ptr
) % index_type_size
) !=
1322 /* Draw primitives individually if one count is zero, so we can easily skip
1325 for (i
= 0; i
< primcount
; i
++) {
1326 if (count
[i
] == 0) {
1332 /* If the index buffer isn't in a VBO, then treating the application's
1333 * subranges of the index buffer as one large index buffer may lead to
1334 * us reading unmapped memory.
1336 if (!_mesa_is_bufferobj(ctx
->Array
.VAO
->IndexBufferObj
))
1340 ib
.count
= (max_index_ptr
- min_index_ptr
) / index_type_size
;
1341 ib
.index_size
= sizeof_ib_type(type
);
1342 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
1343 ib
.ptr
= (void *) min_index_ptr
;
1345 for (i
= 0; i
< primcount
; i
++) {
1346 prim
[i
].begin
= (i
== 0);
1347 prim
[i
].end
= (i
== primcount
- 1);
1350 prim
[i
].mode
= mode
;
1352 ((uintptr_t) indices
[i
] - min_index_ptr
) / index_type_size
;
1353 prim
[i
].count
= count
[i
];
1354 prim
[i
].indexed
= 1;
1355 prim
[i
].num_instances
= 1;
1356 prim
[i
].base_instance
= 0;
1357 prim
[i
].draw_id
= i
;
1358 prim
[i
].is_indirect
= 0;
1359 if (basevertex
!= NULL
)
1360 prim
[i
].basevertex
= basevertex
[i
];
1362 prim
[i
].basevertex
= 0;
1365 vbo
->draw_prims(ctx
, prim
, primcount
, &ib
,
1366 false, 0, ~0, NULL
, 0, NULL
);
1369 /* render one prim at a time */
1370 for (i
= 0; i
< primcount
; i
++) {
1373 ib
.count
= count
[i
];
1374 ib
.index_size
= sizeof_ib_type(type
);
1375 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
1376 ib
.ptr
= indices
[i
];
1382 prim
[0].mode
= mode
;
1384 prim
[0].count
= count
[i
];
1385 prim
[0].indexed
= 1;
1386 prim
[0].num_instances
= 1;
1387 prim
[0].base_instance
= 0;
1388 prim
[0].draw_id
= i
;
1389 prim
[0].is_indirect
= 0;
1390 if (basevertex
!= NULL
)
1391 prim
[0].basevertex
= basevertex
[i
];
1393 prim
[0].basevertex
= 0;
1395 vbo
->draw_prims(ctx
, prim
, 1, &ib
, false, 0, ~0, NULL
, 0, NULL
);
1401 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
) {
1407 static void GLAPIENTRY
1408 vbo_exec_MultiDrawElements(GLenum mode
,
1409 const GLsizei
*count
, GLenum type
,
1410 const GLvoid
* const *indices
, GLsizei primcount
)
1412 GET_CURRENT_CONTEXT(ctx
);
1414 if (!_mesa_validate_MultiDrawElements(ctx
, mode
, count
, type
, indices
,
1418 if (skip_validated_draw(ctx
))
1421 vbo_validated_multidrawelements(ctx
, mode
, count
, type
, indices
, primcount
,
1426 static void GLAPIENTRY
1427 vbo_exec_MultiDrawElementsBaseVertex(GLenum mode
,
1428 const GLsizei
*count
, GLenum type
,
1429 const GLvoid
* const *indices
,
1431 const GLsizei
*basevertex
)
1433 GET_CURRENT_CONTEXT(ctx
);
1435 if (_mesa_is_no_error_enabled(ctx
)) {
1436 FLUSH_CURRENT(ctx
, 0);
1439 _mesa_update_state(ctx
);
1441 if (!_mesa_validate_MultiDrawElements(ctx
, mode
, count
, type
, indices
,
1446 if (skip_validated_draw(ctx
))
1449 vbo_validated_multidrawelements(ctx
, mode
, count
, type
, indices
, primcount
,
1455 vbo_draw_transform_feedback(struct gl_context
*ctx
, GLenum mode
,
1456 struct gl_transform_feedback_object
*obj
,
1457 GLuint stream
, GLuint numInstances
)
1459 struct vbo_context
*vbo
= vbo_context(ctx
);
1460 struct _mesa_prim prim
;
1462 if (_mesa_is_no_error_enabled(ctx
)) {
1463 FLUSH_CURRENT(ctx
, 0);
1466 _mesa_update_state(ctx
);
1468 if (!_mesa_validate_DrawTransformFeedback(ctx
, mode
, obj
, stream
,
1474 if (ctx
->Driver
.GetTransformFeedbackVertexCount
&&
1475 (ctx
->Const
.AlwaysUseGetTransformFeedbackVertexCount
||
1476 !_mesa_all_varyings_in_vbos(ctx
->Array
.VAO
))) {
1478 ctx
->Driver
.GetTransformFeedbackVertexCount(ctx
, obj
, stream
);
1479 vbo_draw_arrays(ctx
, mode
, 0, n
, numInstances
, 0, 0);
1483 if (skip_validated_draw(ctx
))
1486 vbo_bind_arrays(ctx
);
1488 /* init most fields to zero */
1489 memset(&prim
, 0, sizeof(prim
));
1493 prim
.num_instances
= numInstances
;
1494 prim
.base_instance
= 0;
1495 prim
.is_indirect
= 0;
1497 /* Maybe we should do some primitive splitting for primitive restart
1498 * (like in DrawArrays), but we have no way to know how many vertices
1499 * will be rendered. */
1501 vbo
->draw_prims(ctx
, &prim
, 1, NULL
, GL_FALSE
, 0, ~0, obj
, stream
, NULL
);
1503 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
) {
1510 * Like DrawArrays, but take the count from a transform feedback object.
1511 * \param mode GL_POINTS, GL_LINES, GL_TRIANGLE_STRIP, etc.
1512 * \param name the transform feedback object
1513 * User still has to setup of the vertex attribute info with
1514 * glVertexPointer, glColorPointer, etc.
1515 * Part of GL_ARB_transform_feedback2.
1517 static void GLAPIENTRY
1518 vbo_exec_DrawTransformFeedback(GLenum mode
, GLuint name
)
1520 GET_CURRENT_CONTEXT(ctx
);
1521 struct gl_transform_feedback_object
*obj
=
1522 _mesa_lookup_transform_feedback_object(ctx
, name
);
1524 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1525 _mesa_debug(ctx
, "glDrawTransformFeedback(%s, %d)\n",
1526 _mesa_enum_to_string(mode
), name
);
1528 vbo_draw_transform_feedback(ctx
, mode
, obj
, 0, 1);
1532 static void GLAPIENTRY
1533 vbo_exec_DrawTransformFeedbackStream(GLenum mode
, GLuint name
, GLuint stream
)
1535 GET_CURRENT_CONTEXT(ctx
);
1536 struct gl_transform_feedback_object
*obj
=
1537 _mesa_lookup_transform_feedback_object(ctx
, name
);
1539 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1540 _mesa_debug(ctx
, "glDrawTransformFeedbackStream(%s, %u, %u)\n",
1541 _mesa_enum_to_string(mode
), name
, stream
);
1543 vbo_draw_transform_feedback(ctx
, mode
, obj
, stream
, 1);
1547 static void GLAPIENTRY
1548 vbo_exec_DrawTransformFeedbackInstanced(GLenum mode
, GLuint name
,
1551 GET_CURRENT_CONTEXT(ctx
);
1552 struct gl_transform_feedback_object
*obj
=
1553 _mesa_lookup_transform_feedback_object(ctx
, name
);
1555 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1556 _mesa_debug(ctx
, "glDrawTransformFeedbackInstanced(%s, %d)\n",
1557 _mesa_enum_to_string(mode
), name
);
1559 vbo_draw_transform_feedback(ctx
, mode
, obj
, 0, primcount
);
1563 static void GLAPIENTRY
1564 vbo_exec_DrawTransformFeedbackStreamInstanced(GLenum mode
, GLuint name
,
1568 GET_CURRENT_CONTEXT(ctx
);
1569 struct gl_transform_feedback_object
*obj
=
1570 _mesa_lookup_transform_feedback_object(ctx
, name
);
1572 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1573 _mesa_debug(ctx
, "glDrawTransformFeedbackStreamInstanced"
1574 "(%s, %u, %u, %i)\n",
1575 _mesa_enum_to_string(mode
), name
, stream
, primcount
);
1577 vbo_draw_transform_feedback(ctx
, mode
, obj
, stream
, primcount
);
1582 vbo_validated_drawarraysindirect(struct gl_context
*ctx
,
1583 GLenum mode
, const GLvoid
*indirect
)
1585 struct vbo_context
*vbo
= vbo_context(ctx
);
1587 vbo_bind_arrays(ctx
);
1589 vbo
->draw_indirect_prims(ctx
, mode
,
1590 ctx
->DrawIndirectBuffer
, (GLsizeiptr
) indirect
,
1591 1 /* draw_count */ , 16 /* stride */ ,
1594 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1600 vbo_validated_multidrawarraysindirect(struct gl_context
*ctx
,
1602 const GLvoid
*indirect
,
1603 GLsizei primcount
, GLsizei stride
)
1605 struct vbo_context
*vbo
= vbo_context(ctx
);
1606 GLsizeiptr offset
= (GLsizeiptr
) indirect
;
1611 vbo_bind_arrays(ctx
);
1613 vbo
->draw_indirect_prims(ctx
, mode
, ctx
->DrawIndirectBuffer
, offset
,
1614 primcount
, stride
, NULL
, 0, NULL
);
1616 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1622 vbo_validated_drawelementsindirect(struct gl_context
*ctx
,
1623 GLenum mode
, GLenum type
,
1624 const GLvoid
*indirect
)
1626 struct vbo_context
*vbo
= vbo_context(ctx
);
1627 struct _mesa_index_buffer ib
;
1629 vbo_bind_arrays(ctx
);
1631 ib
.count
= 0; /* unknown */
1632 ib
.index_size
= sizeof_ib_type(type
);
1633 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
1636 vbo
->draw_indirect_prims(ctx
, mode
,
1637 ctx
->DrawIndirectBuffer
, (GLsizeiptr
) indirect
,
1638 1 /* draw_count */ , 20 /* stride */ ,
1641 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1647 vbo_validated_multidrawelementsindirect(struct gl_context
*ctx
,
1648 GLenum mode
, GLenum type
,
1649 const GLvoid
*indirect
,
1650 GLsizei primcount
, GLsizei stride
)
1652 struct vbo_context
*vbo
= vbo_context(ctx
);
1653 struct _mesa_index_buffer ib
;
1654 GLsizeiptr offset
= (GLsizeiptr
) indirect
;
1659 vbo_bind_arrays(ctx
);
1661 /* NOTE: IndexBufferObj is guaranteed to be a VBO. */
1663 ib
.count
= 0; /* unknown */
1664 ib
.index_size
= sizeof_ib_type(type
);
1665 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
1668 vbo
->draw_indirect_prims(ctx
, mode
,
1669 ctx
->DrawIndirectBuffer
, offset
,
1670 primcount
, stride
, NULL
, 0, &ib
);
1672 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1678 * Like [Multi]DrawArrays/Elements, but they take most arguments from
1681 static void GLAPIENTRY
1682 vbo_exec_DrawArraysIndirect(GLenum mode
, const GLvoid
*indirect
)
1684 GET_CURRENT_CONTEXT(ctx
);
1686 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1687 _mesa_debug(ctx
, "glDrawArraysIndirect(%s, %p)\n",
1688 _mesa_enum_to_string(mode
), indirect
);
1690 if (_mesa_is_no_error_enabled(ctx
)) {
1691 FLUSH_CURRENT(ctx
, 0);
1694 _mesa_update_state(ctx
);
1696 if (!_mesa_validate_DrawArraysIndirect(ctx
, mode
, indirect
))
1700 if (skip_validated_draw(ctx
))
1703 vbo_validated_drawarraysindirect(ctx
, mode
, indirect
);
1707 static void GLAPIENTRY
1708 vbo_exec_DrawElementsIndirect(GLenum mode
, GLenum type
, const GLvoid
*indirect
)
1710 GET_CURRENT_CONTEXT(ctx
);
1712 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1713 _mesa_debug(ctx
, "glDrawElementsIndirect(%s, %s, %p)\n",
1714 _mesa_enum_to_string(mode
),
1715 _mesa_enum_to_string(type
), indirect
);
1717 if (_mesa_is_no_error_enabled(ctx
)) {
1718 FLUSH_CURRENT(ctx
, 0);
1721 _mesa_update_state(ctx
);
1723 if (!_mesa_validate_DrawElementsIndirect(ctx
, mode
, type
, indirect
))
1727 if (skip_validated_draw(ctx
))
1730 vbo_validated_drawelementsindirect(ctx
, mode
, type
, indirect
);
1734 static void GLAPIENTRY
1735 vbo_exec_MultiDrawArraysIndirect(GLenum mode
, const GLvoid
*indirect
,
1736 GLsizei primcount
, GLsizei stride
)
1738 GET_CURRENT_CONTEXT(ctx
);
1740 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1741 _mesa_debug(ctx
, "glMultiDrawArraysIndirect(%s, %p, %i, %i)\n",
1742 _mesa_enum_to_string(mode
), indirect
, primcount
, stride
);
1744 /* If <stride> is zero, the array elements are treated as tightly packed. */
1746 stride
= 4 * sizeof(GLuint
); /* sizeof(DrawArraysIndirectCommand) */
1748 if (_mesa_is_no_error_enabled(ctx
)) {
1749 FLUSH_CURRENT(ctx
, 0);
1752 _mesa_update_state(ctx
);
1754 if (!_mesa_validate_MultiDrawArraysIndirect(ctx
, mode
, indirect
,
1759 if (skip_validated_draw(ctx
))
1762 vbo_validated_multidrawarraysindirect(ctx
, mode
, indirect
,
1767 static void GLAPIENTRY
1768 vbo_exec_MultiDrawElementsIndirect(GLenum mode
, GLenum type
,
1769 const GLvoid
*indirect
,
1770 GLsizei primcount
, GLsizei stride
)
1772 GET_CURRENT_CONTEXT(ctx
);
1774 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1775 _mesa_debug(ctx
, "glMultiDrawElementsIndirect(%s, %s, %p, %i, %i)\n",
1776 _mesa_enum_to_string(mode
),
1777 _mesa_enum_to_string(type
), indirect
, primcount
, stride
);
1779 /* If <stride> is zero, the array elements are treated as tightly packed. */
1781 stride
= 5 * sizeof(GLuint
); /* sizeof(DrawElementsIndirectCommand) */
1783 if (_mesa_is_no_error_enabled(ctx
)) {
1784 FLUSH_CURRENT(ctx
, 0);
1787 _mesa_update_state(ctx
);
1789 if (!_mesa_validate_MultiDrawElementsIndirect(ctx
, mode
, type
, indirect
,
1794 if (skip_validated_draw(ctx
))
1797 vbo_validated_multidrawelementsindirect(ctx
, mode
, type
, indirect
,
1803 vbo_validated_multidrawarraysindirectcount(struct gl_context
*ctx
,
1807 GLsizei maxdrawcount
,
1810 struct vbo_context
*vbo
= vbo_context(ctx
);
1811 GLsizeiptr offset
= indirect
;
1813 if (maxdrawcount
== 0)
1816 vbo_bind_arrays(ctx
);
1818 vbo
->draw_indirect_prims(ctx
, mode
,
1819 ctx
->DrawIndirectBuffer
, offset
,
1820 maxdrawcount
, stride
,
1821 ctx
->ParameterBuffer
, drawcount
, NULL
);
1823 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1829 vbo_validated_multidrawelementsindirectcount(struct gl_context
*ctx
,
1830 GLenum mode
, GLenum type
,
1833 GLsizei maxdrawcount
,
1836 struct vbo_context
*vbo
= vbo_context(ctx
);
1837 struct _mesa_index_buffer ib
;
1838 GLsizeiptr offset
= (GLsizeiptr
) indirect
;
1840 if (maxdrawcount
== 0)
1843 vbo_bind_arrays(ctx
);
1845 /* NOTE: IndexBufferObj is guaranteed to be a VBO. */
1847 ib
.count
= 0; /* unknown */
1848 ib
.index_size
= sizeof_ib_type(type
);
1849 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
1852 vbo
->draw_indirect_prims(ctx
, mode
,
1853 ctx
->DrawIndirectBuffer
, offset
,
1854 maxdrawcount
, stride
,
1855 ctx
->ParameterBuffer
, drawcount
, &ib
);
1857 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1862 static void GLAPIENTRY
1863 vbo_exec_MultiDrawArraysIndirectCount(GLenum mode
, GLintptr indirect
,
1865 GLsizei maxdrawcount
, GLsizei stride
)
1867 GET_CURRENT_CONTEXT(ctx
);
1869 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1870 _mesa_debug(ctx
, "glMultiDrawArraysIndirectCountARB"
1871 "(%s, %lx, %lx, %i, %i)\n",
1872 _mesa_enum_to_string(mode
),
1873 (unsigned long) indirect
, (unsigned long) drawcount
,
1874 maxdrawcount
, stride
);
1876 /* If <stride> is zero, the array elements are treated as tightly packed. */
1878 stride
= 4 * sizeof(GLuint
); /* sizeof(DrawArraysIndirectCommand) */
1880 if (_mesa_is_no_error_enabled(ctx
)) {
1881 FLUSH_CURRENT(ctx
, 0);
1884 _mesa_update_state(ctx
);
1886 if (!_mesa_validate_MultiDrawArraysIndirectCount(ctx
, mode
,
1887 indirect
, drawcount
,
1888 maxdrawcount
, stride
))
1892 if (skip_validated_draw(ctx
))
1895 vbo_validated_multidrawarraysindirectcount(ctx
, mode
, indirect
, drawcount
,
1896 maxdrawcount
, stride
);
1900 static void GLAPIENTRY
1901 vbo_exec_MultiDrawElementsIndirectCount(GLenum mode
, GLenum type
,
1902 GLintptr indirect
, GLintptr drawcount
,
1903 GLsizei maxdrawcount
, GLsizei stride
)
1905 GET_CURRENT_CONTEXT(ctx
);
1907 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1908 _mesa_debug(ctx
, "glMultiDrawElementsIndirectCountARB"
1909 "(%s, %s, %lx, %lx, %i, %i)\n",
1910 _mesa_enum_to_string(mode
), _mesa_enum_to_string(type
),
1911 (unsigned long) indirect
, (unsigned long) drawcount
,
1912 maxdrawcount
, stride
);
1914 /* If <stride> is zero, the array elements are treated as tightly packed. */
1916 stride
= 5 * sizeof(GLuint
); /* sizeof(DrawElementsIndirectCommand) */
1918 if (_mesa_is_no_error_enabled(ctx
)) {
1919 FLUSH_CURRENT(ctx
, 0);
1922 _mesa_update_state(ctx
);
1924 if (!_mesa_validate_MultiDrawElementsIndirectCount(ctx
, mode
, type
,
1925 indirect
, drawcount
,
1926 maxdrawcount
, stride
))
1930 if (skip_validated_draw(ctx
))
1933 vbo_validated_multidrawelementsindirectcount(ctx
, mode
, type
, indirect
,
1934 drawcount
, maxdrawcount
,
1940 * Initialize the dispatch table with the VBO functions for drawing.
1943 vbo_initialize_exec_dispatch(const struct gl_context
*ctx
,
1944 struct _glapi_table
*exec
)
1946 SET_DrawArrays(exec
, vbo_exec_DrawArrays
);
1947 SET_DrawElements(exec
, vbo_exec_DrawElements
);
1949 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
)) {
1950 SET_DrawRangeElements(exec
, vbo_exec_DrawRangeElements
);
1953 SET_MultiDrawArrays(exec
, vbo_exec_MultiDrawArrays
);
1954 SET_MultiDrawElementsEXT(exec
, vbo_exec_MultiDrawElements
);
1956 if (ctx
->API
== API_OPENGL_COMPAT
) {
1957 SET_Rectf(exec
, vbo_exec_Rectf
);
1958 SET_EvalMesh1(exec
, vbo_exec_EvalMesh1
);
1959 SET_EvalMesh2(exec
, vbo_exec_EvalMesh2
);
1962 if (ctx
->API
!= API_OPENGLES
&&
1963 ctx
->Extensions
.ARB_draw_elements_base_vertex
) {
1964 SET_DrawElementsBaseVertex(exec
, vbo_exec_DrawElementsBaseVertex
);
1965 SET_MultiDrawElementsBaseVertex(exec
,
1966 vbo_exec_MultiDrawElementsBaseVertex
);
1968 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
)) {
1969 SET_DrawRangeElementsBaseVertex(exec
,
1970 vbo_exec_DrawRangeElementsBaseVertex
);
1971 SET_DrawElementsInstancedBaseVertex(exec
,
1972 vbo_exec_DrawElementsInstancedBaseVertex
);
1976 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
)) {
1977 SET_DrawArraysInstancedBaseInstance(exec
,
1978 vbo_exec_DrawArraysInstancedBaseInstance
);
1979 SET_DrawElementsInstancedBaseInstance(exec
,
1980 vbo_exec_DrawElementsInstancedBaseInstance
);
1981 SET_DrawElementsInstancedBaseVertexBaseInstance(exec
,
1982 vbo_exec_DrawElementsInstancedBaseVertexBaseInstance
);
1985 if (ctx
->API
== API_OPENGL_CORE
|| _mesa_is_gles31(ctx
)) {
1986 SET_DrawArraysIndirect(exec
, vbo_exec_DrawArraysIndirect
);
1987 SET_DrawElementsIndirect(exec
, vbo_exec_DrawElementsIndirect
);
1990 if (ctx
->API
== API_OPENGL_CORE
) {
1991 SET_MultiDrawArraysIndirect(exec
, vbo_exec_MultiDrawArraysIndirect
);
1992 SET_MultiDrawElementsIndirect(exec
, vbo_exec_MultiDrawElementsIndirect
);
1993 SET_MultiDrawArraysIndirectCountARB(exec
,
1994 vbo_exec_MultiDrawArraysIndirectCount
);
1995 SET_MultiDrawElementsIndirectCountARB(exec
,
1996 vbo_exec_MultiDrawElementsIndirectCount
);
1999 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
)) {
2000 SET_DrawArraysInstancedARB(exec
, vbo_exec_DrawArraysInstanced
);
2001 SET_DrawElementsInstancedARB(exec
, vbo_exec_DrawElementsInstanced
);
2004 if (_mesa_is_desktop_gl(ctx
)) {
2005 SET_DrawTransformFeedback(exec
, vbo_exec_DrawTransformFeedback
);
2006 SET_DrawTransformFeedbackStream(exec
,
2007 vbo_exec_DrawTransformFeedbackStream
);
2008 SET_DrawTransformFeedbackInstanced(exec
,
2009 vbo_exec_DrawTransformFeedbackInstanced
);
2010 SET_DrawTransformFeedbackStreamInstanced(exec
,
2011 vbo_exec_DrawTransformFeedbackStreamInstanced
);
2018 * The following functions are only used for OpenGL ES 1/2 support.
2019 * And some aren't even supported (yet) in ES 1/2.
2024 _mesa_DrawArrays(GLenum mode
, GLint first
, GLsizei count
)
2026 vbo_exec_DrawArrays(mode
, first
, count
);
2031 _mesa_DrawArraysInstanced(GLenum mode
, GLint first
, GLsizei count
,
2034 vbo_exec_DrawArraysInstanced(mode
, first
, count
, primcount
);
2039 _mesa_DrawElements(GLenum mode
, GLsizei count
, GLenum type
,
2040 const GLvoid
*indices
)
2042 vbo_exec_DrawElements(mode
, count
, type
, indices
);
2047 _mesa_DrawElementsBaseVertex(GLenum mode
, GLsizei count
, GLenum type
,
2048 const GLvoid
*indices
, GLint basevertex
)
2050 vbo_exec_DrawElementsBaseVertex(mode
, count
, type
, indices
, basevertex
);
2055 _mesa_DrawRangeElements(GLenum mode
, GLuint start
, GLuint end
, GLsizei count
,
2056 GLenum type
, const GLvoid
* indices
)
2058 vbo_exec_DrawRangeElements(mode
, start
, end
, count
, type
, indices
);
2063 _mesa_DrawRangeElementsBaseVertex(GLenum mode
, GLuint start
, GLuint end
,
2064 GLsizei count
, GLenum type
,
2065 const GLvoid
*indices
, GLint basevertex
)
2067 vbo_exec_DrawRangeElementsBaseVertex(mode
, start
, end
, count
, type
,
2068 indices
, basevertex
);
2073 _mesa_MultiDrawElementsEXT(GLenum mode
, const GLsizei
*count
, GLenum type
,
2074 const GLvoid
** indices
, GLsizei primcount
)
2076 vbo_exec_MultiDrawElements(mode
, count
, type
, indices
, primcount
);
2081 _mesa_MultiDrawElementsBaseVertex(GLenum mode
,
2082 const GLsizei
*count
, GLenum type
,
2083 const GLvoid
**indices
, GLsizei primcount
,
2084 const GLint
*basevertex
)
2086 vbo_exec_MultiDrawElementsBaseVertex(mode
, count
, type
, indices
,
2087 primcount
, basevertex
);
2092 _mesa_DrawTransformFeedback(GLenum mode
, GLuint name
)
2094 vbo_exec_DrawTransformFeedback(mode
, name
);