mesa: fix GLSL program objects with more than 16 samplers combined

The problem is the sampler units are allocated from the same pool for all
shader stages, so if a vertex shader uses 12 samplers (0..11), the fragment
shader samplers start at index 12, leaving only 4 sampler units
for the fragment shader. The main cause is probably the fact that samplers
(texture unit -> sampler unit mapping, etc.) are tracked globally
for an entire program object.

This commit adapts the GLSL linker and core Mesa such that the sampler units
are assigned to sampler uniforms for each shader stage separately
(if a sampler uniform is used in all shader stages, it may occupy a different
sampler unit in each, and vice versa, an i-th sampler unit may refer to
a different sampler uniform in each shader stage), and the sampler-specific
variables are moved from gl_shader_program to gl_shader.

This doesn't require any driver changes, and it fixes piglit/max-samplers
for gallium and classic swrast. It also works with any number of shader
stages.

v2: - converted tabs to spaces
    - added an assertion to _mesa_get_sampler_uniform_value

Reviewed-by: Ian Romanick <ian.d.romanick@intel.com>
Reviewed-by: Eric Anholt <eric@anholt.net>
diff --git a/src/mesa/program/sampler.cpp b/src/mesa/program/sampler.cpp
index e3641aa..9b94127 100644
--- a/src/mesa/program/sampler.cpp
+++ b/src/mesa/program/sampler.cpp
@@ -34,6 +34,7 @@
 #include "main/compiler.h"
 #include "main/mtypes.h"
 #include "program/prog_parameter.h"
+#include "program/program.h"
 }
 
 class get_sampler_name : public ir_hierarchical_visitor
@@ -102,14 +103,16 @@
    ir_dereference *last;
 };
 
-extern "C" {
-int
+
+extern "C" int
 _mesa_get_sampler_uniform_value(class ir_dereference *sampler,
 				struct gl_shader_program *shader_program,
 				const struct gl_program *prog)
 {
    get_sampler_name getname(sampler, shader_program);
 
+   GLuint shader = _mesa_program_target_to_index(prog->Target);
+
    sampler->accept(&getname);
 
    unsigned location;
@@ -119,6 +122,15 @@
       return 0;
    }
 
-   return shader_program->UniformStorage[location].sampler + getname.offset;
-}
+   if (!shader_program->UniformStorage[location].sampler[shader].active) {
+      assert(0 && "cannot return a sampler");
+      linker_error(shader_program,
+		   "cannot return a sampler named %s, because it is not "
+                   "used in this shader stage. This is a driver bug.\n",
+                   getname.name);
+      return 0;
+   }
+
+   return shader_program->UniformStorage[location].sampler[shader].index +
+          getname.offset;
 }