2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 #include "api_validate.h"
28 #include "bufferobj.h"
31 #include "mfeatures.h"
35 #include "transformfeedback.h"
40 * \return number of bytes in array [count] of type.
43 index_bytes(GLenum type, GLsizei count)
45 if (type == GL_UNSIGNED_INT) {
46 return count * sizeof(GLuint);
48 else if (type == GL_UNSIGNED_BYTE) {
49 return count * sizeof(GLubyte);
52 ASSERT(type == GL_UNSIGNED_SHORT);
53 return count * sizeof(GLushort);
59 * Find the max index in the given element/index buffer
62 _mesa_max_buffer_index(struct gl_context *ctx, GLuint count, GLenum type,
64 struct gl_buffer_object *elementBuf)
66 const GLubyte *map = NULL;
70 if (_mesa_is_bufferobj(elementBuf)) {
71 /* elements are in a user-defined buffer object. need to map it */
72 map = ctx->Driver.MapBufferRange(ctx, 0, elementBuf->Size,
73 GL_MAP_READ_BIT, elementBuf);
74 /* Actual address is the sum of pointers */
75 indices = (const GLvoid *) ADD_POINTERS(map, (const GLubyte *) indices);
78 if (type == GL_UNSIGNED_INT) {
79 for (i = 0; i < count; i++)
80 if (((GLuint *) indices)[i] > max)
81 max = ((GLuint *) indices)[i];
83 else if (type == GL_UNSIGNED_SHORT) {
84 for (i = 0; i < count; i++)
85 if (((GLushort *) indices)[i] > max)
86 max = ((GLushort *) indices)[i];
89 ASSERT(type == GL_UNSIGNED_BYTE);
90 for (i = 0; i < count; i++)
91 if (((GLubyte *) indices)[i] > max)
92 max = ((GLubyte *) indices)[i];
96 ctx->Driver.UnmapBuffer(ctx, elementBuf);
104 * Check if OK to draw arrays/elements.
107 check_valid_to_render(struct gl_context *ctx, const char *function)
109 if (!_mesa_valid_to_render(ctx, function)) {
115 /* For ES2, we can draw if any vertex array is enabled (and we
116 * should always have a vertex program/shader). */
117 if (ctx->Array.ArrayObj->_Enabled == 0x0 || !ctx->VertexProgram._Current)
122 /* For OpenGL ES, only draw if we have vertex positions
124 if (!ctx->Array.ArrayObj->VertexAttrib[VERT_ATTRIB_POS].Enabled)
128 case API_OPENGL_COMPAT:
129 case API_OPENGL_CORE:
131 const struct gl_shader_program *vsProg =
132 ctx->Shader.CurrentVertexProgram;
133 GLboolean haveVertexShader = (vsProg && vsProg->LinkStatus);
134 GLboolean haveVertexProgram = ctx->VertexProgram._Enabled;
135 if (haveVertexShader || haveVertexProgram) {
136 /* Draw regardless of whether or not we have any vertex arrays.
137 * (Ex: could draw a point using a constant vertex pos)
142 /* Draw if we have vertex positions (GL_VERTEX_ARRAY or generic
145 return (ctx->Array.ArrayObj->VertexAttrib[VERT_ATTRIB_POS].Enabled ||
146 ctx->Array.ArrayObj->VertexAttrib[VERT_ATTRIB_GENERIC0].Enabled);
152 assert(!"Invalid API value in check_valid_to_render()");
160 * Do bounds checking on array element indexes. Check that the vertices
161 * pointed to by the indices don't lie outside buffer object bounds.
162 * \return GL_TRUE if OK, GL_FALSE if any indexed vertex goes is out of bounds
165 check_index_bounds(struct gl_context *ctx, GLsizei count, GLenum type,
166 const GLvoid *indices, GLint basevertex)
168 struct _mesa_prim prim;
169 struct _mesa_index_buffer ib;
172 /* Only the X Server needs to do this -- otherwise, accessing outside
173 * array/BO bounds allows application termination.
175 if (!ctx->Const.CheckArrayBounds)
178 memset(&prim, 0, sizeof(prim));
181 memset(&ib, 0, sizeof(ib));
184 ib.obj = ctx->Array.ArrayObj->ElementArrayBufferObj;
186 vbo_get_minmax_indices(ctx, &prim, &ib, &min, &max, 1);
188 if ((int)(min + basevertex) < 0 ||
189 max + basevertex >= ctx->Array.ArrayObj->_MaxElement) {
190 /* the max element is out of bounds of one or more enabled arrays */
191 _mesa_warning(ctx, "glDrawElements() index=%u is out of bounds (max=%u)",
192 max, ctx->Array.ArrayObj->_MaxElement);
201 * Is 'mode' a valid value for glBegin(), glDrawArrays(), glDrawElements(),
202 * etc? The set of legal values depends on whether geometry shaders/programs
206 _mesa_valid_prim_mode(struct gl_context *ctx, GLenum mode, const char *name)
216 case GL_TRIANGLE_STRIP:
217 case GL_TRIANGLE_FAN:
223 valid_enum = (ctx->API == API_OPENGL_COMPAT);
225 case GL_LINES_ADJACENCY:
226 case GL_LINE_STRIP_ADJACENCY:
227 case GL_TRIANGLES_ADJACENCY:
228 case GL_TRIANGLE_STRIP_ADJACENCY:
229 valid_enum = _mesa_is_desktop_gl(ctx)
230 && ctx->Extensions.ARB_geometry_shader4;
238 _mesa_error(ctx, GL_INVALID_ENUM, "%s(mode=%x)", name, mode);
242 /* From the GL_EXT_transform_feedback spec:
244 * "The error INVALID_OPERATION is generated if Begin, or any command
245 * that performs an explicit Begin, is called when:
247 * * a geometry shader is not active and <mode> does not match the
248 * allowed begin modes for the current transform feedback state as
249 * given by table X.1.
251 * * a geometry shader is active and the output primitive type of the
252 * geometry shader does not match the allowed begin modes for the
253 * current transform feedback state as given by table X.1.
256 if (_mesa_is_xfb_active_and_unpaused(ctx)) {
257 GLboolean pass = GL_TRUE;
261 pass = ctx->TransformFeedback.Mode == GL_POINTS;
266 pass = ctx->TransformFeedback.Mode == GL_LINES;
269 pass = ctx->TransformFeedback.Mode == GL_TRIANGLES;
273 _mesa_error(ctx, GL_INVALID_OPERATION,
274 "%s(mode=%s vs transform feedback %s)",
276 _mesa_lookup_prim_by_nr(mode),
277 _mesa_lookup_prim_by_nr(ctx->TransformFeedback.Mode));
286 * Verify that the element type is valid.
288 * Generates \c GL_INVALID_ENUM and returns \c false if it is not.
291 valid_elements_type(struct gl_context *ctx, GLenum type, const char *name)
294 case GL_UNSIGNED_BYTE:
295 case GL_UNSIGNED_SHORT:
296 case GL_UNSIGNED_INT:
300 _mesa_error(ctx, GL_INVALID_ENUM, "%s(type = %s)", name,
301 _mesa_lookup_enum_by_nr(type));
307 * Error checking for glDrawElements(). Includes parameter checking
308 * and VBO bounds checking.
309 * \return GL_TRUE if OK to render, GL_FALSE if error found
312 _mesa_validate_DrawElements(struct gl_context *ctx,
313 GLenum mode, GLsizei count, GLenum type,
314 const GLvoid *indices, GLint basevertex)
316 FLUSH_CURRENT(ctx, 0);
318 /* From the GLES3 specification, section 2.14.2 (Transform Feedback
319 * Primitive Capture):
321 * The error INVALID_OPERATION is also generated by DrawElements,
322 * DrawElementsInstanced, and DrawRangeElements while transform feedback
323 * is active and not paused, regardless of mode.
325 if (_mesa_is_gles3(ctx) && _mesa_is_xfb_active_and_unpaused(ctx)) {
326 _mesa_error(ctx, GL_INVALID_OPERATION,
327 "glDrawElements(transform feedback active)");
333 _mesa_error(ctx, GL_INVALID_VALUE, "glDrawElements(count)" );
337 if (!_mesa_valid_prim_mode(ctx, mode, "glDrawElements")) {
341 if (!valid_elements_type(ctx, type, "glDrawElements"))
344 if (!check_valid_to_render(ctx, "glDrawElements"))
347 /* Vertex buffer object tests */
348 if (_mesa_is_bufferobj(ctx->Array.ArrayObj->ElementArrayBufferObj)) {
349 /* use indices in the buffer object */
350 /* make sure count doesn't go outside buffer bounds */
351 if (index_bytes(type, count) > ctx->Array.ArrayObj->ElementArrayBufferObj->Size) {
352 _mesa_warning(ctx, "glDrawElements index out of buffer bounds");
357 /* not using a VBO */
362 if (!check_index_bounds(ctx, count, type, indices, basevertex))
370 * Error checking for glMultiDrawElements(). Includes parameter checking
371 * and VBO bounds checking.
372 * \return GL_TRUE if OK to render, GL_FALSE if error found
375 _mesa_validate_MultiDrawElements(struct gl_context *ctx,
376 GLenum mode, const GLsizei *count,
377 GLenum type, const GLvoid * const *indices,
378 GLuint primcount, const GLint *basevertex)
382 FLUSH_CURRENT(ctx, 0);
384 for (i = 0; i < primcount; i++) {
387 _mesa_error(ctx, GL_INVALID_VALUE,
388 "glMultiDrawElements(count)" );
393 if (!_mesa_valid_prim_mode(ctx, mode, "glMultiDrawElements")) {
397 if (!valid_elements_type(ctx, type, "glMultiDrawElements"))
400 if (!check_valid_to_render(ctx, "glMultiDrawElements"))
403 /* Vertex buffer object tests */
404 if (_mesa_is_bufferobj(ctx->Array.ArrayObj->ElementArrayBufferObj)) {
405 /* use indices in the buffer object */
406 /* make sure count doesn't go outside buffer bounds */
407 for (i = 0; i < primcount; i++) {
408 if (index_bytes(type, count[i]) >
409 ctx->Array.ArrayObj->ElementArrayBufferObj->Size) {
411 "glMultiDrawElements index out of buffer bounds");
417 /* not using a VBO */
418 for (i = 0; i < primcount; i++) {
424 for (i = 0; i < primcount; i++) {
425 if (!check_index_bounds(ctx, count[i], type, indices[i],
426 basevertex ? basevertex[i] : 0))
435 * Error checking for glDrawRangeElements(). Includes parameter checking
436 * and VBO bounds checking.
437 * \return GL_TRUE if OK to render, GL_FALSE if error found
440 _mesa_validate_DrawRangeElements(struct gl_context *ctx, GLenum mode,
441 GLuint start, GLuint end,
442 GLsizei count, GLenum type,
443 const GLvoid *indices, GLint basevertex)
445 FLUSH_CURRENT(ctx, 0);
447 /* From the GLES3 specification, section 2.14.2 (Transform Feedback
448 * Primitive Capture):
450 * The error INVALID_OPERATION is also generated by DrawElements,
451 * DrawElementsInstanced, and DrawRangeElements while transform feedback
452 * is active and not paused, regardless of mode.
454 if (_mesa_is_gles3(ctx) && _mesa_is_xfb_active_and_unpaused(ctx)) {
455 _mesa_error(ctx, GL_INVALID_OPERATION,
456 "glDrawElements(transform feedback active)");
462 _mesa_error(ctx, GL_INVALID_VALUE, "glDrawRangeElements(count)" );
466 if (!_mesa_valid_prim_mode(ctx, mode, "glDrawRangeElements")) {
471 _mesa_error(ctx, GL_INVALID_VALUE, "glDrawRangeElements(end<start)");
475 if (!valid_elements_type(ctx, type, "glDrawRangeElements"))
478 if (!check_valid_to_render(ctx, "glDrawRangeElements"))
481 /* Vertex buffer object tests */
482 if (_mesa_is_bufferobj(ctx->Array.ArrayObj->ElementArrayBufferObj)) {
483 /* use indices in the buffer object */
484 /* make sure count doesn't go outside buffer bounds */
485 if (index_bytes(type, count) > ctx->Array.ArrayObj->ElementArrayBufferObj->Size) {
486 _mesa_warning(ctx, "glDrawRangeElements index out of buffer bounds");
491 /* not using a VBO */
496 if (!check_index_bounds(ctx, count, type, indices, basevertex))
504 * Called from the tnl module to error check the function parameters and
505 * verify that we really can draw something.
506 * \return GL_TRUE if OK to render, GL_FALSE if error found
509 _mesa_validate_DrawArrays(struct gl_context *ctx,
510 GLenum mode, GLint start, GLsizei count)
512 struct gl_transform_feedback_object *xfb_obj
513 = ctx->TransformFeedback.CurrentObject;
514 FLUSH_CURRENT(ctx, 0);
518 _mesa_error(ctx, GL_INVALID_VALUE, "glDrawArrays(count)" );
522 if (!_mesa_valid_prim_mode(ctx, mode, "glDrawArrays")) {
526 if (!check_valid_to_render(ctx, "glDrawArrays"))
529 if (ctx->Const.CheckArrayBounds) {
530 if (start + count > (GLint) ctx->Array.ArrayObj->_MaxElement)
534 /* From the GLES3 specification, section 2.14.2 (Transform Feedback
535 * Primitive Capture):
537 * The error INVALID_OPERATION is generated by DrawArrays and
538 * DrawArraysInstanced if recording the vertices of a primitive to the
539 * buffer objects being used for transform feedback purposes would result
540 * in either exceeding the limits of any buffer object’s size, or in
541 * exceeding the end position offset + size − 1, as set by
544 * This is in contrast to the behaviour of desktop GL, where the extra
545 * primitives are silently dropped from the transform feedback buffer.
547 if (_mesa_is_gles3(ctx) && _mesa_is_xfb_active_and_unpaused(ctx)) {
548 size_t prim_count = vbo_count_tessellated_primitives(mode, count, 1);
549 if (xfb_obj->GlesRemainingPrims < prim_count) {
550 _mesa_error(ctx, GL_INVALID_OPERATION,
551 "glDrawArrays(exceeds transform feedback size)");
554 xfb_obj->GlesRemainingPrims -= prim_count;
562 _mesa_validate_DrawArraysInstanced(struct gl_context *ctx, GLenum mode, GLint first,
563 GLsizei count, GLsizei numInstances)
565 struct gl_transform_feedback_object *xfb_obj
566 = ctx->TransformFeedback.CurrentObject;
567 FLUSH_CURRENT(ctx, 0);
571 _mesa_error(ctx, GL_INVALID_VALUE,
572 "glDrawArraysInstanced(count=%d)", count);
577 _mesa_error(ctx, GL_INVALID_VALUE,
578 "glDrawArraysInstanced(start=%d)", first);
582 if (!_mesa_valid_prim_mode(ctx, mode, "glDrawArraysInstanced")) {
586 if (numInstances <= 0) {
587 if (numInstances < 0)
588 _mesa_error(ctx, GL_INVALID_VALUE,
589 "glDrawArraysInstanced(numInstances=%d)", numInstances);
593 if (!check_valid_to_render(ctx, "glDrawArraysInstanced(invalid to render)"))
596 if (ctx->Const.CheckArrayBounds) {
597 if (first + count > (GLint) ctx->Array.ArrayObj->_MaxElement)
601 /* From the GLES3 specification, section 2.14.2 (Transform Feedback
602 * Primitive Capture):
604 * The error INVALID_OPERATION is generated by DrawArrays and
605 * DrawArraysInstanced if recording the vertices of a primitive to the
606 * buffer objects being used for transform feedback purposes would result
607 * in either exceeding the limits of any buffer object’s size, or in
608 * exceeding the end position offset + size − 1, as set by
611 * This is in contrast to the behaviour of desktop GL, where the extra
612 * primitives are silently dropped from the transform feedback buffer.
614 if (_mesa_is_gles3(ctx) && _mesa_is_xfb_active_and_unpaused(ctx)) {
616 = vbo_count_tessellated_primitives(mode, count, numInstances);
617 if (xfb_obj->GlesRemainingPrims < prim_count) {
618 _mesa_error(ctx, GL_INVALID_OPERATION,
619 "glDrawArraysInstanced(exceeds transform feedback size)");
622 xfb_obj->GlesRemainingPrims -= prim_count;
630 _mesa_validate_DrawElementsInstanced(struct gl_context *ctx,
631 GLenum mode, GLsizei count, GLenum type,
632 const GLvoid *indices, GLsizei numInstances,
635 FLUSH_CURRENT(ctx, 0);
637 /* From the GLES3 specification, section 2.14.2 (Transform Feedback
638 * Primitive Capture):
640 * The error INVALID_OPERATION is also generated by DrawElements,
641 * DrawElementsInstanced, and DrawRangeElements while transform feedback
642 * is active and not paused, regardless of mode.
644 if (_mesa_is_gles3(ctx) && _mesa_is_xfb_active_and_unpaused(ctx)) {
645 _mesa_error(ctx, GL_INVALID_OPERATION,
646 "glDrawElements(transform feedback active)");
652 _mesa_error(ctx, GL_INVALID_VALUE,
653 "glDrawElementsInstanced(count=%d)", count);
657 if (!_mesa_valid_prim_mode(ctx, mode, "glDrawElementsInstanced")) {
661 if (!valid_elements_type(ctx, type, "glDrawElementsInstanced"))
664 if (numInstances <= 0) {
665 if (numInstances < 0)
666 _mesa_error(ctx, GL_INVALID_VALUE,
667 "glDrawElementsInstanced(numInstances=%d)", numInstances);
671 if (!check_valid_to_render(ctx, "glDrawElementsInstanced"))
674 /* Vertex buffer object tests */
675 if (_mesa_is_bufferobj(ctx->Array.ArrayObj->ElementArrayBufferObj)) {
676 /* use indices in the buffer object */
677 /* make sure count doesn't go outside buffer bounds */
678 if (index_bytes(type, count) > ctx->Array.ArrayObj->ElementArrayBufferObj->Size) {
680 "glDrawElementsInstanced index out of buffer bounds");
685 /* not using a VBO */
690 if (!check_index_bounds(ctx, count, type, indices, basevertex))
698 _mesa_validate_DrawTransformFeedback(struct gl_context *ctx,
700 struct gl_transform_feedback_object *obj,
702 GLsizei numInstances)
704 FLUSH_CURRENT(ctx, 0);
706 if (!_mesa_valid_prim_mode(ctx, mode, "glDrawTransformFeedback*(mode)")) {
711 _mesa_error(ctx, GL_INVALID_VALUE, "glDrawTransformFeedback*(name)");
715 if (!obj->EndedAnytime) {
716 _mesa_error(ctx, GL_INVALID_OPERATION, "glDrawTransformFeedback*");
720 if (stream >= ctx->Const.MaxVertexStreams) {
721 _mesa_error(ctx, GL_INVALID_VALUE,
722 "glDrawTransformFeedbackStream*(index>=MaxVertexStream)");
726 if (numInstances <= 0) {
727 if (numInstances < 0)
728 _mesa_error(ctx, GL_INVALID_VALUE,
729 "glDrawTransformFeedback*Instanced(numInstances=%d)",
734 if (!check_valid_to_render(ctx, "glDrawTransformFeedback*")) {