glsl: lower mediump temporaries to 16 bits except structures (v2)
[mesa.git] / src / mesa / main / transformfeedback.c
index 02867b5b8906b458ae6b4ec79209f6524c587a14..5826447844ab51ae51abf61f63ca75ed82c43ced 100644 (file)
 #include "transformfeedback.h"
 #include "shaderapi.h"
 #include "shaderobj.h"
-#include "main/dispatch.h"
 
+#include "program/program.h"
 #include "program/prog_parameter.h"
 
+#include "util/u_memory.h"
+
 struct using_program_tuple
 {
    struct gl_program *prog;
@@ -142,8 +144,7 @@ _mesa_init_transform_feedback(struct gl_context *ctx)
    ctx->TransformFeedback.Objects = _mesa_NewHashTable();
 
    _mesa_reference_buffer_object(ctx,
-                                 &ctx->TransformFeedback.CurrentBuffer,
-                                 ctx->Shared->NullBufferObj);
+                                 &ctx->TransformFeedback.CurrentBuffer, NULL);
 }
 
 
@@ -198,6 +199,27 @@ _mesa_init_transform_feedback_object(struct gl_transform_feedback_object *obj,
    obj->EverBound = GL_FALSE;
 }
 
+/**
+ * Delete a transform feedback object.  Called via
+ * ctx->Driver->DeleteTransformFeedback, if not overwritten by driver.  In
+ * the latter case, called from the driver after all driver-specific clean-up
+ * has been done.
+ *
+ * \param ctx GL context to wich transform feedback object belongs.
+ * \param obj Transform feedback object due to be deleted.
+ */
+void
+_mesa_delete_transform_feedback_object(struct gl_context *ctx,
+                                       struct gl_transform_feedback_object
+                                              *obj)
+{
+   for (unsigned i = 0; i < ARRAY_SIZE(obj->Buffers); i++) {
+      _mesa_reference_buffer_object(ctx, &obj->Buffers[i], NULL);
+   }
+
+   free(obj->Label);
+   free(obj);
+}
 
 /** Default fallback for ctx->Driver.NewTransformFeedback() */
 static struct gl_transform_feedback_object *
@@ -213,22 +235,6 @@ new_transform_feedback_fallback(struct gl_context *ctx, GLuint name)
    return obj;
 }
 
-/** Default fallback for ctx->Driver.DeleteTransformFeedback() */
-static void
-delete_transform_feedback_fallback(struct gl_context *ctx,
-                                   struct gl_transform_feedback_object *obj)
-{
-   GLuint i;
-
-   for (i = 0; i < ARRAY_SIZE(obj->Buffers); i++) {
-      _mesa_reference_buffer_object(ctx, &obj->Buffers[i], NULL);
-   }
-
-   free(obj->Label);
-   free(obj);
-}
-
-
 /** Default fallback for ctx->Driver.BeginTransformFeedback() */
 static void
 begin_transform_feedback_fallback(struct gl_context *ctx, GLenum mode,
@@ -270,7 +276,7 @@ void
 _mesa_init_transform_feedback_functions(struct dd_function_table *driver)
 {
    driver->NewTransformFeedback = new_transform_feedback_fallback;
-   driver->DeleteTransformFeedback = delete_transform_feedback_fallback;
+   driver->DeleteTransformFeedback = _mesa_delete_transform_feedback_object;
    driver->BeginTransformFeedback = begin_transform_feedback_fallback;
    driver->EndTransformFeedback = end_transform_feedback_fallback;
    driver->PauseTransformFeedback = pause_transform_feedback_fallback;
@@ -346,7 +352,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);
@@ -471,6 +477,7 @@ begin_transform_feedback(struct gl_context *ctx, GLenum mode, bool no_error)
 
    if (obj->program != source) {
       ctx->NewDriverState |= ctx->DriverFlags.NewTransformFeedbackProg;
+      _mesa_reference_program_(ctx, &obj->program, source);
       obj->program = source;
    }
 
@@ -505,6 +512,7 @@ end_transform_feedback(struct gl_context *ctx,
    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;
@@ -605,7 +613,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 */
@@ -627,7 +635,7 @@ _mesa_validate_buffer_range_xfb(struct gl_context *ctx,
       return false;
    }
 
-   if (size <= 0 && (dsa || bufObj != ctx->Shared->NullBufferObj)) {
+   if (size <= 0 && (dsa || bufObj)) {
       /* OpenGL 4.5 core profile, 6.1, pdf page 82: "An INVALID_VALUE error is
        * generated by BindBufferRange if buffer is non-zero and size is less
        * than or equal to zero."
@@ -703,20 +711,22 @@ lookup_transform_feedback_object_err(struct gl_context *ctx,
  */
 static struct gl_buffer_object *
 lookup_transform_feedback_bufferobj_err(struct gl_context *ctx,
-                                        GLuint buffer, const char* func)
+                                        GLuint buffer, const char* func,
+                                        bool *error)
 {
-   struct gl_buffer_object *bufObj;
+   struct gl_buffer_object *bufObj = NULL;
+
+   *error = false;
 
    /* OpenGL 4.5 core profile, 13.2, pdf page 444: buffer must be zero or the
     * name of an existing buffer object.
     */
-   if (buffer == 0) {
-      bufObj = ctx->Shared->NullBufferObj;
-   } else {
+   if (buffer) {
       bufObj = _mesa_lookup_bufferobj(ctx, buffer);
       if (!bufObj) {
          _mesa_error(ctx, GL_INVALID_VALUE, "%s(invalid buffer=%u)", func,
                      buffer);
+         *error = true;
       }
    }
 
@@ -732,13 +742,15 @@ _mesa_TransformFeedbackBufferBase(GLuint xfb, GLuint index, GLuint buffer)
 
    obj = lookup_transform_feedback_object_err(ctx, xfb,
                                               "glTransformFeedbackBufferBase");
-   if(!obj) {
+   if (!obj) {
       return;
    }
 
+   bool error;
    bufObj = lookup_transform_feedback_bufferobj_err(ctx, buffer,
-                                              "glTransformFeedbackBufferBase");
-   if(!bufObj) {
+                                              "glTransformFeedbackBufferBase",
+                                                    &error);
+   if (error) {
       return;
    }
 
@@ -755,13 +767,15 @@ _mesa_TransformFeedbackBufferRange(GLuint xfb, GLuint index, GLuint buffer,
 
    obj = lookup_transform_feedback_object_err(ctx, xfb,
                                               "glTransformFeedbackBufferRange");
-   if(!obj) {
+   if (!obj) {
       return;
    }
 
+   bool error;
    bufObj = lookup_transform_feedback_bufferobj_err(ctx, buffer,
-                                              "glTransformFeedbackBufferRange");
-   if(!bufObj) {
+                                              "glTransformFeedbackBufferRange",
+                                                    &error);
+   if (error) {
       return;
    }
 
@@ -779,12 +793,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 = NULL;
+   } 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) {
@@ -813,18 +858,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);
 }
 
 
@@ -832,6 +866,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,
@@ -907,32 +988,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);
 }
 
 
@@ -1251,6 +1307,14 @@ resume_transform_feedback(struct gl_context *ctx,
 }
 
 
+void GLAPIENTRY
+_mesa_ResumeTransformFeedback_no_error(void)
+{
+   GET_CURRENT_CONTEXT(ctx);
+   resume_transform_feedback(ctx, ctx->TransformFeedback.CurrentObject);
+}
+
+
 void GLAPIENTRY
 _mesa_ResumeTransformFeedback(void)
 {
@@ -1287,7 +1351,7 @@ _mesa_GetTransformFeedbackiv(GLuint xfb, GLenum pname, GLint *param)
 
     obj = lookup_transform_feedback_object_err(ctx, xfb,
                                                "glGetTransformFeedbackiv");
-    if(!obj) {
+    if (!obj) {
        return;
     }
 
@@ -1313,7 +1377,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;
    }
 
@@ -1342,7 +1406,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;
    }
 
@@ -1352,12 +1416,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: