mesa: assert that key->fragprog_inputs_read value isn't too large
[mesa.git] / src / mesa / main / transformfeedback.c
index f2c1435d9e76f1cfb3881985f6ab54d74b90c6e1..1e3a7ea0f32dade3749068c459937ceb48a021ef 100644 (file)
@@ -44,9 +44,6 @@
 #include "program/prog_parameter.h"
 
 
-#if FEATURE_EXT_transform_feedback
-
-
 /**
  * Do reference counting of transform feedback buffers.
  */
@@ -171,40 +168,6 @@ _mesa_free_transform_feedback(struct gl_context *ctx)
 }
 
 
-#else /* FEATURE_EXT_transform_feedback */
-
-/* forward declarations */
-static struct gl_transform_feedback_object *
-new_transform_feedback(struct gl_context *ctx, GLuint name);
-
-static void
-delete_transform_feedback(struct gl_context *ctx,
-                          struct gl_transform_feedback_object *obj);
-
-/* dummy per-context init/clean-up for transform feedback */
-void
-_mesa_init_transform_feedback(struct gl_context *ctx)
-{
-   ctx->TransformFeedback.DefaultObject = new_transform_feedback(ctx, 0);
-   ctx->TransformFeedback.CurrentObject = ctx->TransformFeedback.DefaultObject;
-   _mesa_reference_buffer_object(ctx,
-                                 &ctx->TransformFeedback.CurrentBuffer,
-                                 ctx->Shared->NullBufferObj);
-}
-
-void
-_mesa_free_transform_feedback(struct gl_context *ctx)
-{
-   _mesa_reference_buffer_object(ctx,
-                                 &ctx->TransformFeedback.CurrentBuffer,
-                                 NULL);
-   ctx->TransformFeedback.CurrentObject = NULL;
-   delete_transform_feedback(ctx, ctx->TransformFeedback.DefaultObject);
-}
-
-#endif /* FEATURE_EXT_transform_feedback */
-
-
 /** Default fallback for ctx->Driver.NewTransformFeedback() */
 static struct gl_transform_feedback_object *
 new_transform_feedback(struct gl_context *ctx, GLuint name)
@@ -233,9 +196,6 @@ delete_transform_feedback(struct gl_context *ctx,
 }
 
 
-#if FEATURE_EXT_transform_feedback
-
-
 /** Default fallback for ctx->Driver.BeginTransformFeedback() */
 static void
 begin_transform_feedback(struct gl_context *ctx, GLenum mode,
@@ -286,14 +246,15 @@ _mesa_init_transform_feedback_functions(struct dd_function_table *driver)
 
 
 void
-_mesa_init_transform_feedback_dispatch(struct _glapi_table *disp)
+_mesa_init_transform_feedback_dispatch(const struct gl_context *ctx,
+                                       struct _glapi_table *disp)
 {
    /* EXT_transform_feedback */
    SET_BeginTransformFeedbackEXT(disp, _mesa_BeginTransformFeedback);
    SET_EndTransformFeedbackEXT(disp, _mesa_EndTransformFeedback);
-   SET_BindBufferRangeEXT(disp, _mesa_BindBufferRange);
-   SET_BindBufferBaseEXT(disp, _mesa_BindBufferBase);
-   SET_BindBufferOffsetEXT(disp, _mesa_BindBufferOffsetEXT);
+   if (_mesa_is_desktop_gl(ctx)) {
+      SET_BindBufferOffsetEXT(disp, _mesa_BindBufferOffsetEXT);
+   }
    SET_TransformFeedbackVaryingsEXT(disp, _mesa_TransformFeedbackVaryings);
    SET_GetTransformFeedbackVaryingEXT(disp, _mesa_GetTransformFeedbackVarying);
    /* ARB_transform_feedback2 */
@@ -316,7 +277,7 @@ _mesa_BeginTransformFeedback(GLenum mode)
 {
    struct gl_transform_feedback_object *obj;
    struct gl_transform_feedback_info *info;
-   int i;
+   GLuint i;
    GET_CURRENT_CONTEXT(ctx);
 
    obj = ctx->TransformFeedback.CurrentObject;
@@ -430,19 +391,16 @@ bind_buffer_range(struct gl_context *ctx, GLuint index,
 /**
  * Specify a buffer object to receive vertex shader results.  Plus,
  * specify the starting offset to place the results, and max size.
+ * Called from the glBindBufferRange() function.
  */
-void GLAPIENTRY
-_mesa_BindBufferRange(GLenum target, GLuint index,
-                      GLuint buffer, GLintptr offset, GLsizeiptr size)
+void
+_mesa_bind_buffer_range_transform_feedback(struct gl_context *ctx,
+                                          GLuint index,
+                                          struct gl_buffer_object *bufObj,
+                                          GLintptr offset,
+                                          GLsizeiptr size)
 {
    struct gl_transform_feedback_object *obj;
-   struct gl_buffer_object *bufObj;
-   GET_CURRENT_CONTEXT(ctx);
-
-   if (target != GL_TRANSFORM_FEEDBACK_BUFFER) {
-      _mesa_error(ctx, GL_INVALID_ENUM, "glBindBufferRange(target)");
-      return;
-   }
 
    obj = ctx->TransformFeedback.CurrentObject;
 
@@ -452,13 +410,13 @@ _mesa_BindBufferRange(GLenum target, GLuint index,
       return;
    }
 
-   if (index >= ctx->Const.MaxTransformFeedbackSeparateAttribs) {
+   if (index >= ctx->Const.MaxTransformFeedbackBuffers) {
       _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferRange(index=%d)", index);
       return;
    }
 
-   if ((size <= 0) || (size & 0x3)) {
-      /* must be positive and multiple of four */
+   if (size & 0x3) {
+      /* must a multiple of four */
       _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferRange(size=%d)", (int) size);
       return;
    }  
@@ -470,20 +428,6 @@ _mesa_BindBufferRange(GLenum target, GLuint index,
       return;
    }  
 
-   bufObj = _mesa_lookup_bufferobj(ctx, buffer);
-   if (!bufObj) {
-      _mesa_error(ctx, GL_INVALID_OPERATION,
-                  "glBindBufferRange(invalid buffer=%u)", buffer);
-      return;
-   }
-
-   if (offset + size > bufObj->Size) {
-      _mesa_error(ctx, GL_INVALID_VALUE,
-                  "glBindBufferRange(offset + size %d > buffer size %d)",
-                 (int) (offset + size), (int) (bufObj->Size));
-      return;
-   }  
-
    bind_buffer_range(ctx, index, bufObj, offset, size);
 }
 
@@ -491,19 +435,15 @@ _mesa_BindBufferRange(GLenum target, GLuint index,
 /**
  * Specify a buffer object to receive vertex shader results.
  * As above, but start at offset = 0.
+ * Called from the glBindBufferBase() function.
  */
-void GLAPIENTRY
-_mesa_BindBufferBase(GLenum target, GLuint index, GLuint buffer)
+void
+_mesa_bind_buffer_base_transform_feedback(struct gl_context *ctx,
+                                         GLuint index,
+                                         struct gl_buffer_object *bufObj)
 {
    struct gl_transform_feedback_object *obj;
-   struct gl_buffer_object *bufObj;
    GLsizeiptr size;
-   GET_CURRENT_CONTEXT(ctx);
-
-   if (target != GL_TRANSFORM_FEEDBACK_BUFFER) {
-      _mesa_error(ctx, GL_INVALID_ENUM, "glBindBufferBase(target)");
-      return;
-   }
 
    obj = ctx->TransformFeedback.CurrentObject;
 
@@ -513,18 +453,11 @@ _mesa_BindBufferBase(GLenum target, GLuint index, GLuint buffer)
       return;
    }
 
-   if (index >= ctx->Const.MaxTransformFeedbackSeparateAttribs) {
+   if (index >= ctx->Const.MaxTransformFeedbackBuffers) {
       _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferBase(index=%d)", index);
       return;
    }
 
-   bufObj = _mesa_lookup_bufferobj(ctx, buffer);
-   if (!bufObj) {
-      _mesa_error(ctx, GL_INVALID_OPERATION,
-                  "glBindBufferBase(invalid buffer=%u)", buffer);
-      return;
-   }
-
    /* default size is the buffer size rounded down to nearest
     * multiple of four.
     */
@@ -561,7 +494,7 @@ _mesa_BindBufferOffsetEXT(GLenum target, GLuint index, GLuint buffer,
       return;
    }
 
-   if (index >= ctx->Const.MaxTransformFeedbackSeparateAttribs) {
+   if (index >= ctx->Const.MaxTransformFeedbackBuffers) {
       _mesa_error(ctx, GL_INVALID_VALUE,
                   "glBindBufferOffsetEXT(index=%d)", index);
       return;
@@ -574,7 +507,12 @@ _mesa_BindBufferOffsetEXT(GLenum target, GLuint index, GLuint buffer,
       return;
    }
 
-   bufObj = _mesa_lookup_bufferobj(ctx, buffer);
+   if (buffer == 0) {
+      bufObj = ctx->Shared->NullBufferObj;
+   } else {
+      bufObj = _mesa_lookup_bufferobj(ctx, buffer);
+   }
+
    if (!bufObj) {
       _mesa_error(ctx, GL_INVALID_OPERATION,
                   "glBindBufferOffsetEXT(invalid buffer=%u)", buffer);
@@ -599,7 +537,7 @@ _mesa_TransformFeedbackVaryings(GLuint program, GLsizei count,
                                 const GLchar **varyings, GLenum bufferMode)
 {
    struct gl_shader_program *shProg;
-   GLuint i;
+   GLint i;
    GET_CURRENT_CONTEXT(ctx);
 
    switch (bufferMode) {
@@ -615,7 +553,7 @@ _mesa_TransformFeedbackVaryings(GLuint program, GLsizei count,
 
    if (count < 0 ||
        (bufferMode == GL_SEPARATE_ATTRIBS &&
-        (GLuint) count > ctx->Const.MaxTransformFeedbackSeparateAttribs)) {
+        (GLuint) count > ctx->Const.MaxTransformFeedbackBuffers)) {
       _mesa_error(ctx, GL_INVALID_VALUE,
                   "glTransformFeedbackVaryings(count=%d)", count);
       return;
@@ -628,15 +566,47 @@ _mesa_TransformFeedbackVaryings(GLuint program, GLsizei count,
       return;
    }
 
+   if (ctx->Extensions.ARB_transform_feedback3) {
+      if (bufferMode == GL_INTERLEAVED_ATTRIBS) {
+         unsigned buffers = 1;
+
+         for (i = 0; i < count; i++) {
+            if (strcmp(varyings[i], "gl_NextBuffer") == 0)
+               buffers++;
+         }
+
+         if (buffers > ctx->Const.MaxTransformFeedbackBuffers) {
+            _mesa_error(ctx, GL_INVALID_OPERATION,
+                        "glTransformFeedbackVaryings(too many gl_NextBuffer "
+                        "occurences)");
+            return;
+         }
+      } else {
+         for (i = 0; i < count; i++) {
+            if (strcmp(varyings[i], "gl_NextBuffer") == 0 ||
+                strcmp(varyings[i], "gl_SkipComponents1") == 0 ||
+                strcmp(varyings[i], "gl_SkipComponents2") == 0 ||
+                strcmp(varyings[i], "gl_SkipComponents3") == 0 ||
+                strcmp(varyings[i], "gl_SkipComponents4") == 0) {
+               _mesa_error(ctx, GL_INVALID_OPERATION,
+                           "glTransformFeedbackVaryings(SEPARATE_ATTRIBS,"
+                           "varying=%s)",
+                           varyings[i]);
+               return;
+            }
+         }
+      }
+   }
+
    /* free existing varyings, if any */
-   for (i = 0; i < shProg->TransformFeedback.NumVarying; i++) {
+   for (i = 0; i < (GLint) shProg->TransformFeedback.NumVarying; i++) {
       free(shProg->TransformFeedback.VaryingNames[i]);
    }
    free(shProg->TransformFeedback.VaryingNames);
 
    /* allocate new memory for varying names */
    shProg->TransformFeedback.VaryingNames =
-      (GLchar **) malloc(count * sizeof(GLchar *));
+      malloc(count * sizeof(GLchar *));
 
    if (!shProg->TransformFeedback.VaryingNames) {
       _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTransformFeedbackVaryings()");
@@ -644,7 +614,7 @@ _mesa_TransformFeedbackVaryings(GLuint program, GLsizei count,
    }
 
    /* Save the new names and the count */
-   for (i = 0; i < (GLuint) count; i++) {
+   for (i = 0; i < count; i++) {
       shProg->TransformFeedback.VaryingNames[i] = _mesa_strdup(varyings[i]);
    }
    shProg->TransformFeedback.NumVarying = count;
@@ -678,7 +648,7 @@ _mesa_GetTransformFeedbackVarying(GLuint program, GLuint index,
    }
 
    linked_xfb_info = &shProg->LinkedTransformFeedback;
-   if (index >= linked_xfb_info->NumVarying) {
+   if (index >= (GLuint) linked_xfb_info->NumVarying) {
       _mesa_error(ctx, GL_INVALID_VALUE,
                   "glGetTransformFeedbackVaryings(index=%u)", index);
       return;
@@ -894,5 +864,3 @@ _mesa_ResumeTransformFeedback(void)
    assert(ctx->Driver.ResumeTransformFeedback);
    ctx->Driver.ResumeTransformFeedback(ctx, obj);
 }
-
-#endif /* FEATURE_EXT_transform_feedback */