Merge commit 'origin/gallium-master-merge'
[mesa.git] / src / mesa / shader / shader_api.c
index 854f8bfdaa61c215edf03c8a46cefe5980a12cf1..a5cf1ca1d440a7830a93e067c19ca8c0d7f30f84 100644 (file)
@@ -47,7 +47,7 @@
 #include "shader/shader_api.h"
 #include "shader/slang/slang_compile.h"
 #include "shader/slang/slang_link.h"
-
+#include "glapi/dispatch.h"
 
 
 #ifndef GL_PROGRAM_BINARY_LENGTH_OES
@@ -370,6 +370,31 @@ _mesa_lookup_shader_err(GLcontext *ctx, GLuint name, const char *caller)
 }
 
 
+/**
+ * Return mask of GLSL_x flags by examining the MESA_GLSL env var.
+ */
+static GLbitfield
+get_shader_flags(void)
+{
+   GLbitfield flags = 0x0;
+   const char *env = _mesa_getenv("MESA_GLSL");
+
+   if (env) {
+      if (_mesa_strstr(env, "dump"))
+         flags |= GLSL_DUMP;
+      if (_mesa_strstr(env, "log"))
+         flags |= GLSL_LOG;
+      if (_mesa_strstr(env, "nopt"))
+         flags |= GLSL_NO_OPT;
+      else if (_mesa_strstr(env, "opt"))
+         flags |= GLSL_OPT;
+      if (_mesa_strstr(env, "uniform"))
+         flags |= GLSL_UNIFORMS;
+   }
+
+   return flags;
+}
+
 
 /**
  * Initialize context's shader state.
@@ -383,6 +408,7 @@ _mesa_init_shader_state(GLcontext * ctx)
    ctx->Shader.EmitHighLevelInstructions = GL_TRUE;
    ctx->Shader.EmitCondCodes = GL_FALSE;/*GL_TRUE;*/ /* XXX probably want GL_FALSE... */
    ctx->Shader.EmitComments = GL_FALSE;
+   ctx->Shader.Flags = get_shader_flags();
 }
 
 
@@ -455,7 +481,13 @@ _mesa_attach_shader(GLcontext *ctx, GLuint program, GLuint shader)
    n = shProg->NumShaders;
    for (i = 0; i < n; i++) {
       if (shProg->Shaders[i] == sh) {
-         /* already attached */
+         /* The shader is already attched to this program.  The
+          * GL_ARB_shader_objects spec says:
+          *
+          *     "The error INVALID_OPERATION is generated by AttachObjectARB
+          *     if <obj> is already attached to <containerObj>."
+          */
+         _mesa_error(ctx, GL_INVALID_OPERATION, "glAttachShader");
          return;
       }
    }
@@ -919,24 +951,15 @@ _mesa_get_attached_shaders(GLcontext *ctx, GLuint program, GLsizei maxCount,
 static GLuint
 _mesa_get_handle(GLcontext *ctx, GLenum pname)
 {
-#if 0
-   GET_CURRENT_CONTEXT(ctx);
-
-   switch (pname) {
-   case GL_PROGRAM_OBJECT_ARB:
-      {
-         struct gl2_program_intf **pro = ctx->Shader.CurrentProgram;
-
-         if (pro != NULL)
-            return (**pro)._container._generic.
-               GetName((struct gl2_generic_intf **) (pro));
-      }
-      break;
-   default:
+   GLint handle = 0;
+   
+   if (pname == GL_PROGRAM_OBJECT_ARB) {
+      CALL_GetIntegerv(ctx->Exec, (GL_CURRENT_PROGRAM, &handle));
+   } else {
       _mesa_error(ctx, GL_INVALID_ENUM, "glGetHandleARB");
    }
-#endif
-   return 0;
+
+   return handle;
 }
 
 
@@ -1367,7 +1390,10 @@ _mesa_compile_shader(GLcontext *ctx, GLuint shaderObj)
    if (!sh)
       return;
 
-   sh->CompileStatus = _slang_compile(ctx, sh);
+   /* this call will set the sh->CompileStatus field to indicate if
+    * compilation was successful.
+    */
+   (void) _slang_compile(ctx, sh);
 }
 
 
@@ -1425,10 +1451,22 @@ _mesa_use_program(GLcontext *ctx, GLuint program)
 
 
 /**
- * Update the vertex and fragment program's TexturesUsed arrays.
+ * Update the vertex/fragment program's TexturesUsed array.
+ *
+ * This needs to be called after glUniform(set sampler var) is called.
+ * A call to glUniform(samplerVar, value) causes a sampler to point to a
+ * particular texture unit.  We know the sampler's texture target
+ * (1D/2D/3D/etc) from compile time but the sampler's texture unit is
+ * set by glUniform() calls.
+ *
+ * So, scan the program->SamplerUnits[] and program->SamplerTargets[]
+ * information to update the prog->TexturesUsed[] values.
+ * Each value of TexturesUsed[unit] is one of zero, TEXTURE_1D_INDEX,
+ * TEXTURE_2D_INDEX, TEXTURE_3D_INDEX, etc.
+ * We'll use that info for state validation before rendering.
  */
-static void
-update_textures_used(struct gl_program *prog)
+void
+_mesa_update_shader_textures_used(struct gl_program *prog)
 {
    GLuint s;
 
@@ -1515,10 +1553,12 @@ set_program_uniform(GLcontext *ctx, struct gl_program *program,
                     GLenum type, GLsizei count, GLint elems,
                     const void *values)
 {
+   struct gl_program_parameter *param =
+      &program->Parameters->Parameters[index];
+
    assert(offset >= 0);
 
-   if (!compatible_types(type,
-                         program->Parameters->Parameters[index].DataType)) {
+   if (!compatible_types(type, param->DataType)) {
       _mesa_error(ctx, GL_INVALID_OPERATION, "glUniform(type mismatch)");
       return;
    }
@@ -1528,7 +1568,7 @@ set_program_uniform(GLcontext *ctx, struct gl_program *program,
       return;
    }
 
-   if (program->Parameters->Parameters[index].Type == PROGRAM_SAMPLER) {
+   if (param->Type == PROGRAM_SAMPLER) {
       /* This controls which texture unit which is used by a sampler */
       GLuint texUnit, sampler;
 
@@ -1552,16 +1592,16 @@ set_program_uniform(GLcontext *ctx, struct gl_program *program,
 
       /* This maps a sampler to a texture unit: */
       program->SamplerUnits[sampler] = texUnit;
-      update_textures_used(program);
+      _mesa_update_shader_textures_used(program);
 
       FLUSH_VERTICES(ctx, _NEW_TEXTURE);
    }
    else {
       /* ordinary uniform variable */
       GLsizei k, i;
-      GLint slots = (program->Parameters->Parameters[index].Size + 3) / 4;
+      GLint slots = (param->Size + 3) / 4;
 
-      if (count * elems > (GLint) program->Parameters->Parameters[index].Size) {
+      if (count * elems > (GLint) param->Size) {
          _mesa_error(ctx, GL_INVALID_OPERATION, "glUniform(count too large)");
          return;
       }
@@ -1570,7 +1610,8 @@ set_program_uniform(GLcontext *ctx, struct gl_program *program,
          count = slots;
 
       for (k = 0; k < count; k++) {
-         GLfloat *uniformVal = program->Parameters->ParameterValues[index + offset + k];
+         GLfloat *uniformVal =
+            program->Parameters->ParameterValues[index + offset + k];
          if (is_integer_type(type)) {
             const GLint *iValues = ((const GLint *) values) + k * elems;
             for (i = 0; i < elems; i++) {
@@ -1585,7 +1626,7 @@ set_program_uniform(GLcontext *ctx, struct gl_program *program,
          }
 
          /* if the uniform is bool-valued, convert to 1.0 or 0.0 */
-         if (is_boolean_type(program->Parameters->Parameters[index].DataType)) {
+         if (is_boolean_type(param->DataType)) {
             for (i = 0; i < elems; i++) {
                uniformVal[i] = uniformVal[i] ? 1.0f : 0.0f;
             }
@@ -1603,7 +1644,9 @@ _mesa_uniform(GLcontext *ctx, GLint location, GLsizei count,
               const GLvoid *values, GLenum type)
 {
    struct gl_shader_program *shProg = ctx->Shader.CurrentProgram;
+   struct gl_uniform *uniform;
    GLint elems, offset;
+   GLenum basicType;
 
    if (!shProg || !shProg->LinkStatus) {
       _mesa_error(ctx, GL_INVALID_OPERATION, "glUniform(program not linked)");
@@ -1627,19 +1670,35 @@ _mesa_uniform(GLcontext *ctx, GLint location, GLsizei count,
 
    switch (type) {
    case GL_FLOAT:
+      basicType = GL_FLOAT;
+      elems = 1;
+      break;
    case GL_INT:
+      basicType = GL_INT;
       elems = 1;
       break;
    case GL_FLOAT_VEC2:
+      basicType = GL_FLOAT;
+      elems = 2;
+      break;
    case GL_INT_VEC2:
+      basicType = GL_INT;
       elems = 2;
       break;
    case GL_FLOAT_VEC3:
+      basicType = GL_FLOAT;
+      elems = 3;
+      break;
    case GL_INT_VEC3:
+      basicType = GL_INT;
       elems = 3;
       break;
    case GL_FLOAT_VEC4:
+      basicType = GL_FLOAT;
+      elems = 4;
+      break;
    case GL_INT_VEC4:
+      basicType = GL_INT;
       elems = 4;
       break;
    default:
@@ -1649,12 +1708,33 @@ _mesa_uniform(GLcontext *ctx, GLint location, GLsizei count,
 
    FLUSH_VERTICES(ctx, _NEW_PROGRAM);
 
+   uniform = &shProg->Uniforms->Uniforms[location];
+
+   if (ctx->Shader.Flags & GLSL_UNIFORMS) {
+      GLint i;
+      _mesa_printf("Mesa: set program %u uniform %s (loc %d) to: ",
+                   shProg->Name, uniform->Name, location);
+      if (basicType == GL_INT) {
+         const GLint *v = (const GLint *) values;
+         for (i = 0; i < count * elems; i++) {
+            _mesa_printf("%d ", v[i]);
+         }
+      }
+      else {
+         const GLfloat *v = (const GLfloat *) values;
+         for (i = 0; i < count * elems; i++) {
+            _mesa_printf("%g ", v[i]);
+         }
+      }
+      _mesa_printf("\n");
+   }
+
    /* A uniform var may be used by both a vertex shader and a fragment
     * shader.  We may need to update one or both shader's uniform here:
     */
    if (shProg->VertexProgram) {
       /* convert uniform location to program parameter index */
-      GLint index = shProg->Uniforms->Uniforms[location].VertPos;
+      GLint index = uniform->VertPos;
       if (index >= 0) {
          set_program_uniform(ctx, &shProg->VertexProgram->Base,
                              index, offset, type, count, elems, values);
@@ -1663,12 +1743,14 @@ _mesa_uniform(GLcontext *ctx, GLint location, GLsizei count,
 
    if (shProg->FragmentProgram) {
       /* convert uniform location to program parameter index */
-      GLint index = shProg->Uniforms->Uniforms[location].FragPos;
+      GLint index = uniform->FragPos;
       if (index >= 0) {
          set_program_uniform(ctx, &shProg->FragmentProgram->Base,
                              index, offset, type, count, elems, values);
       }
    }
+
+   uniform->Initialized = GL_TRUE;
 }
 
 
@@ -1735,8 +1817,9 @@ _mesa_uniform_matrix(GLcontext *ctx, GLint cols, GLint rows,
                      GLenum matrixType, GLint location, GLsizei count,
                      GLboolean transpose, const GLfloat *values)
 {
-   GLint offset;
    struct gl_shader_program *shProg = ctx->Shader.CurrentProgram;
+   struct gl_uniform *uniform;
+   GLint offset;
 
    if (!shProg || !shProg->LinkStatus) {
       _mesa_error(ctx, GL_INVALID_OPERATION,
@@ -1760,9 +1843,11 @@ _mesa_uniform_matrix(GLcontext *ctx, GLint cols, GLint rows,
 
    FLUSH_VERTICES(ctx, _NEW_PROGRAM);
 
+   uniform = &shProg->Uniforms->Uniforms[location];
+
    if (shProg->VertexProgram) {
       /* convert uniform location to program parameter index */
-      GLint index = shProg->Uniforms->Uniforms[location].VertPos;
+      GLint index = uniform->VertPos;
       if (index >= 0) {
          set_program_uniform_matrix(ctx, &shProg->VertexProgram->Base,
                                     index, offset,
@@ -1772,13 +1857,15 @@ _mesa_uniform_matrix(GLcontext *ctx, GLint cols, GLint rows,
 
    if (shProg->FragmentProgram) {
       /* convert uniform location to program parameter index */
-      GLint index = shProg->Uniforms->Uniforms[location].FragPos;
+      GLint index = uniform->FragPos;
       if (index >= 0) {
          set_program_uniform_matrix(ctx, &shProg->FragmentProgram->Base,
                                     index, offset,
                                     count, rows, cols, transpose, values);
       }
    }
+
+   uniform->Initialized = GL_TRUE;
 }