mesa/main: Clamp GetUniformuiv values to be >= 0
[mesa.git] / src / mesa / main / uniform_query.cpp
index f177e9633370ab14d4ae3a42f3f854090b505c8c..314f7d8a1b01d2eab66398d00b90e412bdde1ad8 100644 (file)
@@ -25,6 +25,7 @@
  */
 
 #include <stdlib.h>
+#include <inttypes.h>  /* for PRIx64 macro */
 
 #include "main/core.h"
 #include "main/context.h"
@@ -320,16 +321,19 @@ _mesa_get_uniform(struct gl_context *ctx, GLuint program, GLint location,
    }
 
    {
-      unsigned elements = (uni->type->is_sampler())
-        ? 1 : uni->type->components();
-      const int dmul = uni->type->is_64bit() ? 2 : 1;
+      unsigned elements = uni->type->components();
+      /* XXX: Remove the sampler/image check workarounds when bindless is fully
+       * implemented.
+       */
+      const int dmul =
+         (uni->type->is_64bit() && !uni->type->is_sampler() && !uni->type->is_image()) ? 2 : 1;
       const int rmul = glsl_base_type_is_64bit(returnType) ? 2 : 1;
 
       /* Calculate the source base address *BEFORE* modifying elements to
        * account for the size of the user's buffer.
        */
       const union gl_constant_value *const src =
-        &uni->storage[offset * elements * dmul];
+         &uni->storage[offset * elements * dmul];
 
       assert(returnType == GLSL_TYPE_FLOAT || returnType == GLSL_TYPE_INT ||
              returnType == GLSL_TYPE_UINT || returnType == GLSL_TYPE_DOUBLE ||
@@ -338,100 +342,92 @@ _mesa_get_uniform(struct gl_context *ctx, GLuint program, GLint location,
       /* doubles have a different size than the other 3 types */
       unsigned bytes = sizeof(src[0]) * elements * rmul;
       if (bufSize < 0 || bytes > (unsigned) bufSize) {
-        _mesa_error( ctx, GL_INVALID_OPERATION,
-                    "glGetnUniform*vARB(out of bounds: bufSize is %d,"
-                    " but %u bytes are required)", bufSize, bytes );
-        return;
+         _mesa_error(ctx, GL_INVALID_OPERATION,
+                     "glGetnUniform*vARB(out of bounds: bufSize is %d,"
+                     " but %u bytes are required)", bufSize, bytes);
+         return;
       }
 
       /* If the return type and the uniform's native type are "compatible,"
        * just memcpy the data.  If the types are not compatible, perform a
        * slower convert-and-copy process.
        */
-      if (returnType == uni->type->base_type
-         || ((returnType == GLSL_TYPE_INT
-              || returnType == GLSL_TYPE_UINT)
-             &&
-             (uni->type->base_type == GLSL_TYPE_INT
-              || uni->type->base_type == GLSL_TYPE_UINT
-               || uni->type->base_type == GLSL_TYPE_SAMPLER
-               || uni->type->base_type == GLSL_TYPE_IMAGE))
-          || ((returnType == GLSL_TYPE_UINT64 ||
-               returnType == GLSL_TYPE_INT64 ) &&
-              (uni->type->base_type == GLSL_TYPE_UINT64 ||
-               uni->type->base_type == GLSL_TYPE_INT64))) {
-        memcpy(paramsOut, src, bytes);
+      if (returnType == uni->type->base_type ||
+          ((returnType == GLSL_TYPE_INT || returnType == GLSL_TYPE_UINT) &&
+           (uni->type->is_sampler() || uni->type->is_image()))) {
+         memcpy(paramsOut, src, bytes);
       } else {
-        union gl_constant_value *const dst =
-           (union gl_constant_value *) paramsOut;
-        /* This code could be optimized by putting the loop inside the switch
-         * statements.  However, this is not expected to be
-         * performance-critical code.
-         */
-        for (unsigned i = 0; i < elements; i++) {
-          int sidx = i * dmul;
-          int didx = i * rmul;
-
-           switch (returnType) {
-           case GLSL_TYPE_FLOAT:
-              switch (uni->type->base_type) {
-              case GLSL_TYPE_UINT:
-                 dst[didx].f = (float) src[sidx].u;
-                 break;
-              case GLSL_TYPE_INT:
-              case GLSL_TYPE_SAMPLER:
+         union gl_constant_value *const dst =
+            (union gl_constant_value *) paramsOut;
+         /* This code could be optimized by putting the loop inside the switch
+          * statements.  However, this is not expected to be
+          * performance-critical code.
+          */
+         for (unsigned i = 0; i < elements; i++) {
+            int sidx = i * dmul;
+            int didx = i * rmul;
+
+            switch (returnType) {
+            case GLSL_TYPE_FLOAT:
+               switch (uni->type->base_type) {
+               case GLSL_TYPE_UINT:
+                  dst[didx].f = (float) src[sidx].u;
+                  break;
+               case GLSL_TYPE_INT:
+               case GLSL_TYPE_SAMPLER:
                case GLSL_TYPE_IMAGE:
-                 dst[didx].f = (float) src[sidx].i;
-                 break;
-              case GLSL_TYPE_BOOL:
-                 dst[didx].f = src[sidx].i ? 1.0f : 0.0f;
-                 break;
+                  dst[didx].f = (float) src[sidx].i;
+                  break;
+               case GLSL_TYPE_BOOL:
+                  dst[didx].f = src[sidx].i ? 1.0f : 0.0f;
+                  break;
                case GLSL_TYPE_DOUBLE: {
                   double tmp;
                   memcpy(&tmp, &src[sidx].f, sizeof(tmp));
                   dst[didx].f = tmp;
-                 break;
+                  break;
                }
                case GLSL_TYPE_UINT64: {
                   uint64_t tmp;
                   memcpy(&tmp, &src[sidx].u, sizeof(tmp));
                   dst[didx].f = tmp;
                   break;
-               }
+                }
                case GLSL_TYPE_INT64: {
                   uint64_t tmp;
                   memcpy(&tmp, &src[sidx].i, sizeof(tmp));
                   dst[didx].f = tmp;
                   break;
                }
-              default:
-                 assert(!"Should not get here.");
-                 break;
-              }
-              break;
-           case GLSL_TYPE_DOUBLE:
-              switch (uni->type->base_type) {
+               default:
+                  assert(!"Should not get here.");
+                  break;
+               }
+               break;
+
+            case GLSL_TYPE_DOUBLE:
+               switch (uni->type->base_type) {
                case GLSL_TYPE_UINT: {
                   double tmp = src[sidx].u;
                   memcpy(&dst[didx].f, &tmp, sizeof(tmp));
-                 break;
+                  break;
                }
-              case GLSL_TYPE_INT:
-              case GLSL_TYPE_SAMPLER:
+               case GLSL_TYPE_INT:
+               case GLSL_TYPE_SAMPLER:
                case GLSL_TYPE_IMAGE: {
                   double tmp = src[sidx].i;
                   memcpy(&dst[didx].f, &tmp, sizeof(tmp));
-                 break;
+                  break;
                }
                case GLSL_TYPE_BOOL: {
                   double tmp = src[sidx].i ? 1.0 : 0.0;
                   memcpy(&dst[didx].f, &tmp, sizeof(tmp));
-                 break;
+                  break;
                }
                case GLSL_TYPE_FLOAT: {
                   double tmp = src[sidx].f;
                   memcpy(&dst[didx].f, &tmp, sizeof(tmp));
-                 break;
+                  break;
                }
                case GLSL_TYPE_UINT64: {
                   uint64_t tmpu;
@@ -449,42 +445,45 @@ _mesa_get_uniform(struct gl_context *ctx, GLuint program, GLint location,
                   memcpy(&dst[didx].f, &tmp, sizeof(tmp));
                   break;
                }
-              default:
-                 assert(!"Should not get here.");
-                 break;
-              }
-              break;
-           case GLSL_TYPE_INT:
-           case GLSL_TYPE_UINT:
-              switch (uni->type->base_type) {
-              case GLSL_TYPE_FLOAT:
-                 /* While the GL 3.2 core spec doesn't explicitly
-                  * state how conversion of float uniforms to integer
-                  * values works, in section 6.2 "State Tables" on
-                  * page 267 it says:
-                  *
-                  *     "Unless otherwise specified, when floating
-                  *      point state is returned as integer values or
-                  *      integer state is returned as floating-point
-                  *      values it is converted in the fashion
-                  *      described in section 6.1.2"
-                  *
-                  * That section, on page 248, says:
-                  *
-                  *     "If GetIntegerv or GetInteger64v are called,
-                  *      a floating-point value is rounded to the
-                  *      nearest integer..."
-                  */
-                 dst[didx].i = IROUND(src[sidx].f);
-                 break;
-              case GLSL_TYPE_BOOL:
-                 dst[didx].i = src[sidx].i ? 1 : 0;
-                 break;
+               default:
+                  assert(!"Should not get here.");
+                  break;
+               }
+               break;
+
+            case GLSL_TYPE_INT:
+               switch (uni->type->base_type) {
+               case GLSL_TYPE_FLOAT:
+                  /* While the GL 3.2 core spec doesn't explicitly
+                   * state how conversion of float uniforms to integer
+                   * values works, in section 6.2 "State Tables" on
+                   * page 267 it says:
+                   *
+                   *     "Unless otherwise specified, when floating
+                   *      point state is returned as integer values or
+                   *      integer state is returned as floating-point
+                   *      values it is converted in the fashion
+                   *      described in section 6.1.2"
+                   *
+                   * That section, on page 248, says:
+                   *
+                   *     "If GetIntegerv or GetInteger64v are called,
+                   *      a floating-point value is rounded to the
+                   *      nearest integer..."
+                   */
+                  dst[didx].i = IROUND(src[sidx].f);
+                  break;
+               case GLSL_TYPE_BOOL:
+                  dst[didx].i = src[sidx].i ? 1 : 0;
+                  break;
+               case GLSL_TYPE_UINT:
+                  dst[didx].i = MIN2(src[sidx].i, INT_MAX);
+                  break;
                case GLSL_TYPE_DOUBLE: {
                   double tmp;
                   memcpy(&tmp, &src[sidx].f, sizeof(tmp));
                   dst[didx].i = IROUNDD(tmp);
-                 break;
+                  break;
                }
                case GLSL_TYPE_UINT64: {
                   uint64_t tmp;
@@ -498,11 +497,58 @@ _mesa_get_uniform(struct gl_context *ctx, GLuint program, GLint location,
                   dst[didx].i = tmp;
                   break;
                }
-              default:
-                 assert(!"Should not get here.");
-                 break;
-              }
-              break;
+               default:
+                  assert(!"Should not get here.");
+                  break;
+               }
+               break;
+
+            case GLSL_TYPE_UINT:
+               switch (uni->type->base_type) {
+               case GLSL_TYPE_FLOAT:
+                  /* The spec isn't terribly clear how to handle negative
+                   * values with an unsigned return type.
+                   *
+                   * GL 4.5 section 2.2.2 ("Data Conversions for State
+                   * Query Commands") says:
+                   *
+                   * "If a value is so large in magnitude that it cannot be
+                   *  represented by the returned data type, then the nearest
+                   *  value representable using the requested type is
+                   *  returned."
+                   */
+                  dst[didx].u = src[sidx].f < 0.0f ?
+                     0u : (uint32_t) roundf(src[sidx].f);
+                  break;
+               case GLSL_TYPE_BOOL:
+                  dst[didx].i = src[sidx].i ? 1 : 0;
+                  break;
+               case GLSL_TYPE_INT:
+                  dst[didx].i = MAX2(src[sidx].i, 0);
+                  break;
+               case GLSL_TYPE_DOUBLE: {
+                  double tmp;
+                  memcpy(&tmp, &src[sidx].f, sizeof(tmp));
+                  dst[didx].u = tmp < 0.0 ? 0u : (uint32_t) round(tmp);
+                  break;
+               }
+               case GLSL_TYPE_UINT64: {
+                  uint64_t tmp;
+                  memcpy(&tmp, &src[sidx].u, sizeof(tmp));
+                  dst[didx].i = MIN2(tmp, INT_MAX);
+                  break;
+               }
+               case GLSL_TYPE_INT64: {
+                  int64_t tmp;
+                  memcpy(&tmp, &src[sidx].i, sizeof(tmp));
+                  dst[didx].i = MAX2(tmp, 0);
+                  break;
+               }
+               default:
+                  unreachable("invalid uniform type");
+               }
+               break;
+
             case GLSL_TYPE_INT64:
             case GLSL_TYPE_UINT64:
                switch (uni->type->base_type) {
@@ -533,11 +579,12 @@ _mesa_get_uniform(struct gl_context *ctx, GLuint program, GLint location,
                   break;
                }
                break;
-           default:
-              assert(!"Should not get here.");
-              break;
-           }
-        }
+
+            default:
+               assert(!"Should not get here.");
+               break;
+            }
+         }
       }
    }
 }
@@ -573,13 +620,13 @@ log_uniform(const void *values, enum glsl_base_type basicType,
       case GLSL_TYPE_UINT64: {
          uint64_t tmp;
          memcpy(&tmp, &v[i * 2].u, sizeof(tmp));
-         printf("%lu ", tmp);
+         printf("%" PRIu64 " ", tmp);
          break;
       }
       case GLSL_TYPE_INT64: {
          int64_t tmp;
          memcpy(&tmp, &v[i * 2].u, sizeof(tmp));
-         printf("%ld ", tmp);
+         printf("%" PRId64 " ", tmp);
          break;
       }
       case GLSL_TYPE_FLOAT:
@@ -647,10 +694,8 @@ _mesa_propagate_uniforms_to_driver_storage(struct gl_uniform_storage *uni,
 {
    unsigned i;
 
-   /* vector_elements and matrix_columns can be 0 for samplers.
-    */
-   const unsigned components = MAX2(1, uni->type->vector_elements);
-   const unsigned vectors = MAX2(1, uni->type->matrix_columns);
+   const unsigned components = uni->type->vector_elements;
+   const unsigned vectors = uni->type->matrix_columns;
    const int dmul = uni->type->is_64bit() ? 2 : 1;
 
    /* Store the data in the driver's requested type in the driver's storage
@@ -781,35 +826,28 @@ glsl_type_name(enum glsl_base_type type)
 }
 
 
-/**
- * Called via glUniform*() functions.
- */
-extern "C" void
-_mesa_uniform(GLint location, GLsizei count, const GLvoid *values,
-              struct gl_context *ctx, struct gl_shader_program *shProg,
-              enum glsl_base_type basicType, unsigned src_components)
+static struct gl_uniform_storage *
+validate_uniform(GLint location, GLsizei count, const GLvoid *values,
+                 unsigned *offset, struct gl_context *ctx,
+                 struct gl_shader_program *shProg,
+                 enum glsl_base_type basicType, unsigned src_components)
 {
-   unsigned offset;
-   int size_mul = glsl_base_type_is_64bit(basicType) ? 2 : 1;
-
    struct gl_uniform_storage *const uni =
-      validate_uniform_parameters(location, count, &offset,
+      validate_uniform_parameters(location, count, offset,
                                   ctx, shProg, "glUniform");
    if (uni == NULL)
-      return;
+      return NULL;
 
    if (uni->type->is_matrix()) {
       /* Can't set matrix uniforms (like mat4) with glUniform */
       _mesa_error(ctx, GL_INVALID_OPERATION,
                   "glUniform%u(uniform \"%s\"@%d is matrix)",
                   src_components, uni->name, location);
-      return;
+      return NULL;
    }
 
-   /* Verify that the types are compatible.
-    */
-   const unsigned components = uni->type->is_sampler()
-      ? 1 : uni->type->vector_elements;
+   /* Verify that the types are compatible. */
+   const unsigned components = uni->type->vector_elements;
 
    if (components != src_components) {
       /* glUniformN() must match float/vecN type */
@@ -817,7 +855,7 @@ _mesa_uniform(GLint location, GLsizei count, const GLvoid *values,
                   "glUniform%u(\"%s\"@%u has %u components, not %u)",
                   src_components, uni->name, location,
                   components, src_components);
-      return;
+      return NULL;
    }
 
    bool match;
@@ -842,12 +880,12 @@ _mesa_uniform(GLint location, GLsizei count, const GLvoid *values,
                   src_components, uni->name, location,
                   glsl_type_name(uni->type->base_type),
                   glsl_type_name(basicType));
-      return;
+      return NULL;
    }
 
    if (unlikely(ctx->_Shader->Flags & GLSL_UNIFORMS)) {
       log_uniform(values, basicType, components, 1, count,
-                 false, shProg, location, uni);
+                  false, shProg, location, uni);
    }
 
    /* Page 100 (page 116 of the PDF) of the OpenGL 3.0 spec says:
@@ -869,15 +907,14 @@ _mesa_uniform(GLint location, GLsizei count, const GLvoid *values,
     */
    if (uni->type->is_sampler()) {
       for (int i = 0; i < count; i++) {
-        const unsigned texUnit = ((unsigned *) values)[i];
+         const unsigned texUnit = ((unsigned *) values)[i];
 
          /* check that the sampler (tex unit index) is legal */
          if (texUnit >= ctx->Const.MaxCombinedTextureImageUnits) {
             _mesa_error(ctx, GL_INVALID_VALUE,
                         "glUniform1i(invalid sampler/tex unit index for "
-                       "uniform %d)",
-                        location);
-            return;
+                        "uniform %d)", location);
+            return NULL;
          }
       }
       /* We need to reset the validate flag on changes to samplers in case
@@ -895,11 +932,53 @@ _mesa_uniform(GLint location, GLsizei count, const GLvoid *values,
             _mesa_error(ctx, GL_INVALID_VALUE,
                         "glUniform1i(invalid image unit index for uniform %d)",
                         location);
-            return;
+            return NULL;
          }
       }
    }
 
+   return uni;
+}
+
+
+/**
+ * Called via glUniform*() functions.
+ */
+extern "C" void
+_mesa_uniform(GLint location, GLsizei count, const GLvoid *values,
+              struct gl_context *ctx, struct gl_shader_program *shProg,
+              enum glsl_base_type basicType, unsigned src_components)
+{
+   unsigned offset;
+   int size_mul = glsl_base_type_is_64bit(basicType) ? 2 : 1;
+
+   struct gl_uniform_storage *uni;
+   if (_mesa_is_no_error_enabled(ctx)) {
+      /* From Seciton 7.6 (UNIFORM VARIABLES) of the OpenGL 4.5 spec:
+       *
+       *   "If the value of location is -1, the Uniform* commands will
+       *   silently ignore the data passed in, and the current uniform values
+       *   will not be changed.
+       */
+      if (location == -1)
+         return;
+
+      uni = shProg->UniformRemapTable[location];
+
+      /* The array index specified by the uniform location is just the
+       * uniform location minus the base location of of the uniform.
+       */
+      assert(uni->array_elements > 0 || location == (int)uni->remap_location);
+      offset = location - uni->remap_location;
+   } else {
+      uni = validate_uniform(location, count, values, &offset, ctx, shProg,
+                             basicType, src_components);
+      if (!uni)
+         return;
+   }
+
+   const unsigned components = uni->type->vector_elements;
+
    /* Page 82 (page 96 of the PDF) of the OpenGL 2.1 spec says:
     *
     *     "When loading N elements starting at an arbitrary position k in a
@@ -921,19 +1000,19 @@ _mesa_uniform(GLint location, GLsizei count, const GLvoid *values,
     */
    if (!uni->type->is_boolean()) {
       memcpy(&uni->storage[size_mul * components * offset], values,
-            sizeof(uni->storage[0]) * components * count * size_mul);
+             sizeof(uni->storage[0]) * components * count * size_mul);
    } else {
       const union gl_constant_value *src =
-        (const union gl_constant_value *) values;
+         (const union gl_constant_value *) values;
       union gl_constant_value *dst = &uni->storage[components * offset];
       const unsigned elems = components * count;
 
       for (unsigned i = 0; i < elems; i++) {
-        if (basicType == GLSL_TYPE_FLOAT) {
+         if (basicType == GLSL_TYPE_FLOAT) {
             dst[i].i = src[i].f != 0.0f ? ctx->Const.UniformBooleanTrue : 0;
-        } else {
+         } else {
             dst[i].i = src[i].i != 0    ? ctx->Const.UniformBooleanTrue : 0;
-        }
+         }
       }
    }
 
@@ -944,12 +1023,15 @@ _mesa_uniform(GLint location, GLsizei count, const GLvoid *values,
     */
    if (uni->type->is_sampler()) {
       bool flushed = false;
+
+      shProg->SamplersValidated = GL_TRUE;
+
       for (int i = 0; i < MESA_SHADER_STAGES; i++) {
-        struct gl_linked_shader *const sh = shProg->_LinkedShaders[i];
+         struct gl_linked_shader *const sh = shProg->_LinkedShaders[i];
 
-        /* If the shader stage doesn't use the sampler uniform, skip this. */
-        if (!uni->opaque[i].active)
-           continue;
+         /* If the shader stage doesn't use the sampler uniform, skip this. */
+         if (!uni->opaque[i].active)
+            continue;
 
          bool changed = false;
          for (int j = 0; j < count; j++) {
@@ -960,17 +1042,17 @@ _mesa_uniform(GLint location, GLsizei count, const GLvoid *values,
             }
          }
 
-        if (changed) {
-           if (!flushed) {
-              FLUSH_VERTICES(ctx, _NEW_TEXTURE | _NEW_PROGRAM);
-              flushed = true;
-           }
+         if (changed) {
+            if (!flushed) {
+               FLUSH_VERTICES(ctx, _NEW_TEXTURE_OBJECT | _NEW_PROGRAM);
+               flushed = true;
+            }
 
             struct gl_program *const prog = sh->Program;
-           _mesa_update_shader_textures_used(shProg, prog);
+            _mesa_update_shader_textures_used(shProg, prog);
             if (ctx->Driver.SamplerUniformChange)
-              ctx->Driver.SamplerUniformChange(ctx, prog->Target, prog);
-        }
+               ctx->Driver.SamplerUniformChange(ctx, prog->Target, prog);
+         }
       }
    }
 
@@ -979,13 +1061,15 @@ _mesa_uniform(GLint location, GLsizei count, const GLvoid *values,
     */
    if (uni->type->is_image()) {
       for (int i = 0; i < MESA_SHADER_STAGES; i++) {
-        if (uni->opaque[i].active) {
-            struct gl_linked_shader *sh = shProg->_LinkedShaders[i];
+         struct gl_linked_shader *sh = shProg->_LinkedShaders[i];
 
-            for (int j = 0; j < count; j++)
-               sh->Program->sh.ImageUnits[uni->opaque[i].index + offset + j] =
-                  ((GLint *) values)[j];
-         }
+         /* If the shader stage doesn't use the image uniform, skip this. */
+         if (!uni->opaque[i].active)
+            continue;
+
+         for (int j = 0; j < count; j++)
+            sh->Program->sh.ImageUnits[uni->opaque[i].index + offset + j] =
+               ((GLint *) values)[j];
       }
 
       ctx->NewDriverState |= ctx->DriverFlags.NewImageUnits;