mesa: add glTextureParameteri/iv/f/fvEXT
[mesa.git] / src / mesa / main / transformfeedback.c
index d61db4eb62811ca4c8b776060a49130b38ae38ac..8eccdc20b7664b1deea624e7c0002fd144e16cdc 100644 (file)
@@ -39,8 +39,8 @@
 #include "transformfeedback.h"
 #include "shaderapi.h"
 #include "shaderobj.h"
-#include "main/dispatch.h"
 
+#include "program/program.h"
 #include "program/prog_parameter.h"
 
 struct using_program_tuple
@@ -346,7 +346,7 @@ _mesa_compute_max_transform_feedback_vertices(struct gl_context *ctx,
 
          /* Skip any inactive buffers, which have a stride of 0. */
          if (stride == 0)
-           continue;
+            continue;
 
          max_for_this_buffer = obj->Size[i] / (4 * stride);
          max_index = MIN2(max_index, max_for_this_buffer);
@@ -381,22 +381,22 @@ get_xfb_source(struct gl_context *ctx)
 }
 
 
-void GLAPIENTRY
-_mesa_BeginTransformFeedback(GLenum mode)
+static ALWAYS_INLINE void
+begin_transform_feedback(struct gl_context *ctx, GLenum mode, bool no_error)
 {
    struct gl_transform_feedback_object *obj;
    struct gl_transform_feedback_info *info = NULL;
+   struct gl_program *source;
    GLuint i;
    unsigned vertices_per_prim;
-   GET_CURRENT_CONTEXT(ctx);
 
    obj = ctx->TransformFeedback.CurrentObject;
 
    /* Figure out what pipeline stage is the source of data for transform
     * feedback.
     */
-   struct gl_program *source = get_xfb_source(ctx);
-   if (source == NULL) {
+   source = get_xfb_source(ctx);
+   if (!no_error && source == NULL) {
       _mesa_error(ctx, GL_INVALID_OPERATION,
                   "glBeginTransformFeedback(no program active)");
       return;
@@ -404,7 +404,7 @@ _mesa_BeginTransformFeedback(GLenum mode)
 
    info = source->sh.LinkedTransformFeedback;
 
-   if (info->NumOutputs == 0) {
+   if (!no_error && info->NumOutputs == 0) {
       _mesa_error(ctx, GL_INVALID_OPERATION,
                   "glBeginTransformFeedback(no varyings to record)");
       return;
@@ -421,23 +421,30 @@ _mesa_BeginTransformFeedback(GLenum mode)
       vertices_per_prim = 3;
       break;
    default:
-      _mesa_error(ctx, GL_INVALID_ENUM, "glBeginTransformFeedback(mode)");
-      return;
+      if (!no_error) {
+         _mesa_error(ctx, GL_INVALID_ENUM, "glBeginTransformFeedback(mode)");
+         return;
+      } else {
+         /* Stop compiler warnings */
+         unreachable("Error in API use when using KHR_no_error");
+      }
    }
 
-   if (obj->Active) {
-      _mesa_error(ctx, GL_INVALID_OPERATION,
-                  "glBeginTransformFeedback(already active)");
-      return;
-   }
+   if (!no_error) {
+      if (obj->Active) {
+         _mesa_error(ctx, GL_INVALID_OPERATION,
+                     "glBeginTransformFeedback(already active)");
+         return;
+      }
 
-   for (i = 0; i < ctx->Const.MaxTransformFeedbackBuffers; i++) {
-      if ((info->ActiveBuffers >> i) & 1) {
-         if (obj->BufferNames[i] == 0) {
-            _mesa_error(ctx, GL_INVALID_OPERATION,
-                        "glBeginTransformFeedback(binding point %d does not "
-                        "have a buffer object bound)", i);
-            return;
+      for (i = 0; i < ctx->Const.MaxTransformFeedbackBuffers; i++) {
+         if ((info->ActiveBuffers >> i) & 1) {
+            if (obj->BufferNames[i] == 0) {
+               _mesa_error(ctx, GL_INVALID_OPERATION,
+                           "glBeginTransformFeedback(binding point %d does not "
+                           "have a buffer object bound)", i);
+               return;
+            }
          }
       }
    }
@@ -464,6 +471,7 @@ _mesa_BeginTransformFeedback(GLenum mode)
 
    if (obj->program != source) {
       ctx->NewDriverState |= ctx->DriverFlags.NewTransformFeedbackProg;
+      _mesa_reference_program_(ctx, &obj->program, source);
       obj->program = source;
    }
 
@@ -473,31 +481,64 @@ _mesa_BeginTransformFeedback(GLenum mode)
 
 
 void GLAPIENTRY
-_mesa_EndTransformFeedback(void)
+_mesa_BeginTransformFeedback_no_error(GLenum mode)
 {
-   struct gl_transform_feedback_object *obj;
    GET_CURRENT_CONTEXT(ctx);
+   begin_transform_feedback(ctx, mode, true);
+}
 
-   obj = ctx->TransformFeedback.CurrentObject;
 
-   if (!obj->Active) {
-      _mesa_error(ctx, GL_INVALID_OPERATION,
-                  "glEndTransformFeedback(not active)");
-      return;
-   }
+void GLAPIENTRY
+_mesa_BeginTransformFeedback(GLenum mode)
+{
+   GET_CURRENT_CONTEXT(ctx);
+   begin_transform_feedback(ctx, mode, false);
+}
+
 
+static void
+end_transform_feedback(struct gl_context *ctx,
+                       struct gl_transform_feedback_object *obj)
+{
    FLUSH_VERTICES(ctx, 0);
    ctx->NewDriverState |= ctx->DriverFlags.NewTransformFeedback;
 
    assert(ctx->Driver.EndTransformFeedback);
    ctx->Driver.EndTransformFeedback(ctx, obj);
 
+   _mesa_reference_program_(ctx, &obj->program, NULL);
    ctx->TransformFeedback.CurrentObject->Active = GL_FALSE;
    ctx->TransformFeedback.CurrentObject->Paused = GL_FALSE;
    ctx->TransformFeedback.CurrentObject->EndedAnytime = GL_TRUE;
 }
 
 
+void GLAPIENTRY
+_mesa_EndTransformFeedback_no_error(void)
+{
+   GET_CURRENT_CONTEXT(ctx);
+   end_transform_feedback(ctx, ctx->TransformFeedback.CurrentObject);
+}
+
+
+void GLAPIENTRY
+_mesa_EndTransformFeedback(void)
+{
+   struct gl_transform_feedback_object *obj;
+   GET_CURRENT_CONTEXT(ctx);
+
+   obj = ctx->TransformFeedback.CurrentObject;
+
+   if (!obj->Active) {
+      _mesa_error(ctx, GL_INVALID_OPERATION,
+                  "glEndTransformFeedback(not active)");
+      return;
+   }
+
+   end_transform_feedback(ctx, obj);
+}
+
+
 /**
  * Helper used by BindBufferRange() and BindBufferBase().
  */
@@ -566,7 +607,7 @@ _mesa_validate_buffer_range_xfb(struct gl_context *ctx,
       _mesa_error(ctx, GL_INVALID_VALUE, "%s(size=%d must be a multiple of "
                   "four)", gl_methd_name, (int) size);
       return false;
-   }  
+   }
 
    if (offset & 0x3) {
       /* OpenGL 4.5 core profile, 6.7, pdf page 103: multiple of 4 */
@@ -693,13 +734,13 @@ _mesa_TransformFeedbackBufferBase(GLuint xfb, GLuint index, GLuint buffer)
 
    obj = lookup_transform_feedback_object_err(ctx, xfb,
                                               "glTransformFeedbackBufferBase");
-   if(!obj) {
+   if (!obj) {
       return;
    }
 
    bufObj = lookup_transform_feedback_bufferobj_err(ctx, buffer,
                                               "glTransformFeedbackBufferBase");
-   if(!bufObj) {
+   if (!bufObj) {
       return;
    }
 
@@ -716,13 +757,13 @@ _mesa_TransformFeedbackBufferRange(GLuint xfb, GLuint index, GLuint buffer,
 
    obj = lookup_transform_feedback_object_err(ctx, xfb,
                                               "glTransformFeedbackBufferRange");
-   if(!obj) {
+   if (!obj) {
       return;
    }
 
    bufObj = lookup_transform_feedback_bufferobj_err(ctx, buffer,
                                               "glTransformFeedbackBufferRange");
-   if(!bufObj) {
+   if (!bufObj) {
       return;
    }
 
@@ -740,12 +781,43 @@ _mesa_TransformFeedbackBufferRange(GLuint xfb, GLuint index, GLuint buffer,
  * offset in the buffer to start placing results.
  * This function is part of GL_EXT_transform_feedback, but not GL3.
  */
+static ALWAYS_INLINE void
+bind_buffer_offset(struct gl_context *ctx,
+                   struct gl_transform_feedback_object *obj, GLuint index,
+                   GLuint buffer, GLintptr offset, bool no_error)
+{
+   struct gl_buffer_object *bufObj;
+
+   if (buffer == 0) {
+      bufObj = ctx->Shared->NullBufferObj;
+   } else {
+      bufObj = _mesa_lookup_bufferobj(ctx, buffer);
+      if (!no_error && !bufObj) {
+         _mesa_error(ctx, GL_INVALID_OPERATION,
+                     "glBindBufferOffsetEXT(invalid buffer=%u)", buffer);
+         return;
+      }
+   }
+
+   _mesa_bind_buffer_range_xfb(ctx, obj, index, bufObj, offset, 0);
+}
+
+
+void GLAPIENTRY
+_mesa_BindBufferOffsetEXT_no_error(GLenum target, GLuint index, GLuint buffer,
+                                   GLintptr offset)
+{
+   GET_CURRENT_CONTEXT(ctx);
+   bind_buffer_offset(ctx, ctx->TransformFeedback.CurrentObject, index, buffer,
+                      offset, true);
+}
+
+
 void GLAPIENTRY
 _mesa_BindBufferOffsetEXT(GLenum target, GLuint index, GLuint buffer,
                           GLintptr offset)
 {
    struct gl_transform_feedback_object *obj;
-   struct gl_buffer_object *bufObj;
    GET_CURRENT_CONTEXT(ctx);
 
    if (target != GL_TRANSFORM_FEEDBACK_BUFFER) {
@@ -774,18 +846,7 @@ _mesa_BindBufferOffsetEXT(GLenum target, GLuint index, GLuint buffer,
       return;
    }
 
-   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);
-         return;
-      }
-   }
-
-   _mesa_bind_buffer_range_xfb(ctx, obj, index, bufObj, offset, 0);
+   bind_buffer_offset(ctx, obj, index, buffer, offset, false);
 }
 
 
@@ -793,6 +854,53 @@ _mesa_BindBufferOffsetEXT(GLenum target, GLuint index, GLuint buffer,
  * This function specifies the transform feedback outputs to be written
  * to the feedback buffer(s), and in what order.
  */
+static ALWAYS_INLINE void
+transform_feedback_varyings(struct gl_context *ctx,
+                            struct gl_shader_program *shProg, GLsizei count,
+                            const GLchar *const *varyings, GLenum bufferMode)
+{
+   GLint i;
+
+   /* free existing varyings, if any */
+   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 =
+      malloc(count * sizeof(GLchar *));
+
+   if (!shProg->TransformFeedback.VaryingNames) {
+      _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTransformFeedbackVaryings()");
+      return;
+   }
+
+   /* Save the new names and the count */
+   for (i = 0; i < count; i++) {
+      shProg->TransformFeedback.VaryingNames[i] = strdup(varyings[i]);
+   }
+   shProg->TransformFeedback.NumVarying = count;
+
+   shProg->TransformFeedback.BufferMode = bufferMode;
+
+   /* No need to invoke FLUSH_VERTICES or flag NewTransformFeedback since
+    * the varyings won't be used until shader link time.
+    */
+}
+
+
+void GLAPIENTRY
+_mesa_TransformFeedbackVaryings_no_error(GLuint program, GLsizei count,
+                                         const GLchar *const *varyings,
+                                         GLenum bufferMode)
+{
+   GET_CURRENT_CONTEXT(ctx);
+
+   struct gl_shader_program *shProg = _mesa_lookup_shader_program(ctx, program);
+   transform_feedback_varyings(ctx, shProg, count, varyings, bufferMode);
+}
+
 void GLAPIENTRY
 _mesa_TransformFeedbackVaryings(GLuint program, GLsizei count,
                                 const GLchar * const *varyings,
@@ -868,32 +976,7 @@ _mesa_TransformFeedbackVaryings(GLuint program, GLsizei count,
       }
    }
 
-   /* free existing varyings, if any */
-   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 =
-      malloc(count * sizeof(GLchar *));
-
-   if (!shProg->TransformFeedback.VaryingNames) {
-      _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTransformFeedbackVaryings()");
-      return;
-   }
-
-   /* Save the new names and the count */
-   for (i = 0; i < count; i++) {
-      shProg->TransformFeedback.VaryingNames[i] = strdup(varyings[i]);
-   }
-   shProg->TransformFeedback.NumVarying = count;
-
-   shProg->TransformFeedback.BufferMode = bufferMode;
-
-   /* No need to invoke FLUSH_VERTICES or flag NewTransformFeedback since
-    * the varyings won't be used until shader link time.
-    */
+   transform_feedback_varyings(ctx, shProg, count, varyings, bufferMode);
 }
 
 
@@ -1079,6 +1162,14 @@ bind_transform_feedback(struct gl_context *ctx, GLuint name, bool no_error)
 }
 
 
+void GLAPIENTRY
+_mesa_BindTransformFeedback_no_error(GLenum target, GLuint name)
+{
+   GET_CURRENT_CONTEXT(ctx);
+   bind_transform_feedback(ctx, name, true);
+}
+
+
 void GLAPIENTRY
 _mesa_BindTransformFeedback(GLenum target, GLuint name)
 {
@@ -1146,6 +1237,28 @@ _mesa_DeleteTransformFeedbacks(GLsizei n, const GLuint *names)
  * Pause transform feedback.
  * Part of GL_ARB_transform_feedback2.
  */
+static void
+pause_transform_feedback(struct gl_context *ctx,
+                         struct gl_transform_feedback_object *obj)
+{
+   FLUSH_VERTICES(ctx, 0);
+   ctx->NewDriverState |= ctx->DriverFlags.NewTransformFeedback;
+
+   assert(ctx->Driver.PauseTransformFeedback);
+   ctx->Driver.PauseTransformFeedback(ctx, obj);
+
+   obj->Paused = GL_TRUE;
+}
+
+
+void GLAPIENTRY
+_mesa_PauseTransformFeedback_no_error(void)
+{
+   GET_CURRENT_CONTEXT(ctx);
+   pause_transform_feedback(ctx, ctx->TransformFeedback.CurrentObject);
+}
+
+
 void GLAPIENTRY
 _mesa_PauseTransformFeedback(void)
 {
@@ -1160,13 +1273,7 @@ _mesa_PauseTransformFeedback(void)
       return;
    }
 
-   FLUSH_VERTICES(ctx, 0);
-   ctx->NewDriverState |= ctx->DriverFlags.NewTransformFeedback;
-
-   assert(ctx->Driver.PauseTransformFeedback);
-   ctx->Driver.PauseTransformFeedback(ctx, obj);
-
-   obj->Paused = GL_TRUE;
+   pause_transform_feedback(ctx, obj);
 }
 
 
@@ -1174,6 +1281,28 @@ _mesa_PauseTransformFeedback(void)
  * Resume transform feedback.
  * Part of GL_ARB_transform_feedback2.
  */
+static void
+resume_transform_feedback(struct gl_context *ctx,
+                          struct gl_transform_feedback_object *obj)
+{
+   FLUSH_VERTICES(ctx, 0);
+   ctx->NewDriverState |= ctx->DriverFlags.NewTransformFeedback;
+
+   obj->Paused = GL_FALSE;
+
+   assert(ctx->Driver.ResumeTransformFeedback);
+   ctx->Driver.ResumeTransformFeedback(ctx, obj);
+}
+
+
+void GLAPIENTRY
+_mesa_ResumeTransformFeedback_no_error(void)
+{
+   GET_CURRENT_CONTEXT(ctx);
+   resume_transform_feedback(ctx, ctx->TransformFeedback.CurrentObject);
+}
+
+
 void GLAPIENTRY
 _mesa_ResumeTransformFeedback(void)
 {
@@ -1199,13 +1328,7 @@ _mesa_ResumeTransformFeedback(void)
       return;
    }
 
-   FLUSH_VERTICES(ctx, 0);
-   ctx->NewDriverState |= ctx->DriverFlags.NewTransformFeedback;
-
-   obj->Paused = GL_FALSE;
-
-   assert(ctx->Driver.ResumeTransformFeedback);
-   ctx->Driver.ResumeTransformFeedback(ctx, obj);
+   resume_transform_feedback(ctx, obj);
 }
 
 extern void GLAPIENTRY
@@ -1216,7 +1339,7 @@ _mesa_GetTransformFeedbackiv(GLuint xfb, GLenum pname, GLint *param)
 
     obj = lookup_transform_feedback_object_err(ctx, xfb,
                                                "glGetTransformFeedbackiv");
-    if(!obj) {
+    if (!obj) {
        return;
     }
 
@@ -1242,7 +1365,7 @@ _mesa_GetTransformFeedbacki_v(GLuint xfb, GLenum pname, GLuint index,
 
    obj = lookup_transform_feedback_object_err(ctx, xfb,
                                               "glGetTransformFeedbacki_v");
-   if(!obj) {
+   if (!obj) {
       return;
    }
 
@@ -1271,7 +1394,7 @@ _mesa_GetTransformFeedbacki64_v(GLuint xfb, GLenum pname, GLuint index,
 
    obj = lookup_transform_feedback_object_err(ctx, xfb,
                                               "glGetTransformFeedbacki64_v");
-   if(!obj) {
+   if (!obj) {
       return;
    }
 
@@ -1281,12 +1404,34 @@ _mesa_GetTransformFeedbacki64_v(GLuint xfb, GLenum pname, GLuint index,
       return;
    }
 
+   /**
+    * This follows the same general rules used for BindBufferBase:
+    *
+    *   "To query the starting offset or size of the range of a buffer
+    *    object binding in an indexed array, call GetInteger64i_v with
+    *    target set to respectively the starting offset or binding size
+    *    name from table 6.5 for that array. Index must be in the range
+    *    zero to the number of bind points supported minus one. If the
+    *    starting offset or size was not specified when the buffer object
+    *    was bound (e.g. if it was bound with BindBufferBase), or if no
+    *    buffer object is bound to the target array at index, zero is
+    *    returned."
+    */
+   if (obj->RequestedSize[index] == 0 &&
+       (pname == GL_TRANSFORM_FEEDBACK_BUFFER_START ||
+        pname == GL_TRANSFORM_FEEDBACK_BUFFER_SIZE)) {
+      *param = 0;
+      return;
+   }
+
    compute_transform_feedback_buffer_sizes(obj);
    switch(pname) {
    case GL_TRANSFORM_FEEDBACK_BUFFER_START:
+      assert(obj->RequestedSize[index] > 0);
       *param = obj->Offset[index];
       break;
    case GL_TRANSFORM_FEEDBACK_BUFFER_SIZE:
+      assert(obj->RequestedSize[index] > 0);
       *param = obj->Size[index];
       break;
    default: