i965: Move load_interpolated_input/barycentric_* intrinsics to the top.
[mesa.git] / src / mesa / state_tracker / st_atom_texture.c
index 7604202baf625bc1f5fabc1aae6f5dbed58a51cb..4b7ad77b47ac00f39849932ee31ed7154859446d 100644 (file)
@@ -1,6 +1,6 @@
 /**************************************************************************
  *
- * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
+ * Copyright 2007 VMware, Inc.
  * All Rights Reserved.
  *
  * Permission is hereby granted, free of charge, to any person obtaining a
@@ -18,7 +18,7 @@
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
- * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
+ * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
  /*
   * Authors:
-  *   Keith Whitwell <keith@tungstengraphics.com>
+  *   Keith Whitwell <keithw@vmware.com>
   *   Brian Paul
   */
 
 
+#include "main/context.h"
 #include "main/macros.h"
 #include "main/mtypes.h"
 #include "main/samplerobj.h"
+#include "main/teximage.h"
 #include "main/texobj.h"
 #include "program/prog_instruction.h"
 
 
 
 /**
- * Combine depth texture mode with "swizzle" so that depth mode swizzling
- * takes place before texture swizzling, and return the resulting swizzle.
- * If the format is not a depth format, return "swizzle" unchanged.
- *
- * \param format     PIPE_FORMAT_*.
- * \param swizzle    Texture swizzle, a bitmask computed using MAKE_SWIZZLE4.
- * \param depthmode  One of GL_LUMINANCE, GL_INTENSITY, GL_ALPHA, GL_RED.
+ * Return swizzle1(swizzle2)
  */
-static GLuint
-apply_depthmode(enum pipe_format format, GLuint swizzle, GLenum depthmode)
+static unsigned
+swizzle_swizzle(unsigned swizzle1, unsigned swizzle2)
 {
-   const struct util_format_description *desc =
-         util_format_description(format);
-   unsigned char swiz[4];
-   unsigned i;
-
-   if (desc->colorspace != UTIL_FORMAT_COLORSPACE_ZS ||
-       desc->swizzle[0] == UTIL_FORMAT_SWIZZLE_NONE) {
-      /* Not a depth format. */
-      return swizzle;
+   unsigned i, swz[4];
+
+   for (i = 0; i < 4; i++) {
+      unsigned s = GET_SWZ(swizzle1, i);
+      switch (s) {
+      case SWIZZLE_X:
+      case SWIZZLE_Y:
+      case SWIZZLE_Z:
+      case SWIZZLE_W:
+         swz[i] = GET_SWZ(swizzle2, s);
+         break;
+      case SWIZZLE_ZERO:
+         swz[i] = SWIZZLE_ZERO;
+         break;
+      case SWIZZLE_ONE:
+         swz[i] = SWIZZLE_ONE;
+         break;
+      default:
+         assert(!"Bad swizzle term");
+         swz[i] = SWIZZLE_X;
+      }
    }
 
-   for (i = 0; i < 4; i++)
-      swiz[i] = GET_SWZ(swizzle, i);
+   return MAKE_SWIZZLE4(swz[0], swz[1], swz[2], swz[3]);
+}
 
-   switch (depthmode) {
-      case GL_LUMINANCE:
-         /* Rewrite reads from W to ONE, and reads from XYZ to XXX. */
-         for (i = 0; i < 4; i++)
-            if (swiz[i] == SWIZZLE_W)
-               swiz[i] = SWIZZLE_ONE;
-            else if (swiz[i] < SWIZZLE_W)
-               swiz[i] = SWIZZLE_X;
-         break;
 
+/**
+ * Given a user-specified texture base format, the actual gallium texture
+ * format and the current GL_DEPTH_MODE, return a texture swizzle.
+ *
+ * Consider the case where the user requests a GL_RGB internal texture
+ * format the driver actually uses an RGBA format.  The A component should
+ * be ignored and sampling from the texture should always return (r,g,b,1).
+ * But if we rendered to the texture we might have written A values != 1.
+ * By sampling the texture with a ".xyz1" swizzle we'll get the expected A=1.
+ * This function computes the texture swizzle needed to get the expected
+ * values.
+ *
+ * In the case of depth textures, the GL_DEPTH_MODE state determines the
+ * texture swizzle.
+ *
+ * This result must be composed with the user-specified swizzle to get
+ * the final swizzle.
+ */
+static unsigned
+compute_texture_format_swizzle(GLenum baseFormat, GLenum depthMode,
+                               enum pipe_format actualFormat,
+                               unsigned glsl_version)
+{
+   switch (baseFormat) {
+   case GL_RGBA:
+      return SWIZZLE_XYZW;
+   case GL_RGB:
+      if (util_format_has_alpha(actualFormat))
+         return MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_ONE);
+      else
+         return SWIZZLE_XYZW;
+   case GL_RG:
+      if (util_format_get_nr_components(actualFormat) > 2)
+         return MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_ZERO, SWIZZLE_ONE);
+      else
+         return SWIZZLE_XYZW;
+   case GL_RED:
+      if (util_format_get_nr_components(actualFormat) > 1)
+         return MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_ZERO,
+                              SWIZZLE_ZERO, SWIZZLE_ONE);
+      else
+         return SWIZZLE_XYZW;
+   case GL_ALPHA:
+      if (util_format_get_nr_components(actualFormat) > 1)
+         return MAKE_SWIZZLE4(SWIZZLE_ZERO, SWIZZLE_ZERO,
+                              SWIZZLE_ZERO, SWIZZLE_W);
+      else
+         return SWIZZLE_XYZW;
+   case GL_LUMINANCE:
+      if (util_format_get_nr_components(actualFormat) > 1)
+         return MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_X, SWIZZLE_X, SWIZZLE_ONE);
+      else
+         return SWIZZLE_XYZW;
+   case GL_LUMINANCE_ALPHA:
+      if (util_format_get_nr_components(actualFormat) > 2)
+         return MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_X, SWIZZLE_X, SWIZZLE_W);
+      else
+         return SWIZZLE_XYZW;
+   case GL_INTENSITY:
+      if (util_format_get_nr_components(actualFormat) > 1)
+         return SWIZZLE_XXXX;
+      else
+         return SWIZZLE_XYZW;
+   case GL_STENCIL_INDEX:
+   case GL_DEPTH_STENCIL:
+   case GL_DEPTH_COMPONENT:
+      /* Now examine the depth mode */
+      switch (depthMode) {
+      case GL_LUMINANCE:
+         return MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_X, SWIZZLE_X, SWIZZLE_ONE);
       case GL_INTENSITY:
-         /* Rewrite reads from XYZW to XXXX. */
-         for (i = 0; i < 4; i++)
-            if (swiz[i] <= SWIZZLE_W)
-               swiz[i] = SWIZZLE_X;
-         break;
-
+         return MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_X, SWIZZLE_X, SWIZZLE_X);
       case GL_ALPHA:
-         /* Rewrite reads from W to X, and reads from XYZ to 000. */
-         for (i = 0; i < 4; i++)
-            if (swiz[i] == SWIZZLE_W)
-               swiz[i] = SWIZZLE_X;
-            else if (swiz[i] < SWIZZLE_W)
-               swiz[i] = SWIZZLE_ZERO;
-         break;
+         /* The texture(sampler*Shadow) functions from GLSL 1.30 ignore
+          * the depth mode and return float, while older shadow* functions
+          * and ARB_fp instructions return vec4 according to the depth mode.
+          *
+          * The problem with the GLSL 1.30 functions is that GL_ALPHA forces
+          * them to return 0, breaking them completely.
+          *
+          * A proper fix would increase code complexity and that's not worth
+          * it for a rarely used feature such as the GL_ALPHA depth mode
+          * in GL3. Therefore, change GL_ALPHA to GL_INTENSITY for all
+          * shaders that use GLSL 1.30 or later.
+          *
+          * BTW, it's required that sampler views are updated when
+          * shaders change (check_sampler_swizzle takes care of that).
+          */
+         if (glsl_version && glsl_version >= 130)
+            return SWIZZLE_XXXX;
+         else
+            return MAKE_SWIZZLE4(SWIZZLE_ZERO, SWIZZLE_ZERO,
+                                 SWIZZLE_ZERO, SWIZZLE_X);
       case GL_RED:
-        /* Rewrite reads W to 1, XYZ to X00 */
-        for (i = 0; i < 4; i++)
-           if (swiz[i] == SWIZZLE_W)
-              swiz[i] = SWIZZLE_ONE;
-           else if (swiz[i] == SWIZZLE_Y || swiz[i] == SWIZZLE_Z)
-              swiz[i] = SWIZZLE_ZERO;
-        break;
+         return MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_ZERO,
+                              SWIZZLE_ZERO, SWIZZLE_ONE);
+      default:
+         assert(!"Unexpected depthMode");
+         return SWIZZLE_XYZW;
+      }
+   default:
+      assert(!"Unexpected baseFormat");
+      return SWIZZLE_XYZW;
+   }
+}
+
+
+static unsigned
+get_texture_format_swizzle(const struct st_context *st,
+                           const struct st_texture_object *stObj,
+                           unsigned glsl_version)
+{
+   GLenum baseFormat = _mesa_texture_base_format(&stObj->base);
+   unsigned tex_swizzle;
+
+   if (baseFormat != GL_NONE) {
+      GLenum depth_mode = stObj->base.DepthMode;
+      /* In ES 3.0, DEPTH_TEXTURE_MODE is expected to be GL_RED for textures
+       * with depth component data specified with a sized internal format.
+       */
+      if (_mesa_is_gles3(st->ctx) &&
+          util_format_is_depth_or_stencil(stObj->pt->format)) {
+         const struct st_texture_image *firstImage =
+            st_texture_image_const(_mesa_base_tex_image(&stObj->base));
+         if (firstImage->base.InternalFormat != GL_DEPTH_COMPONENT &&
+             firstImage->base.InternalFormat != GL_DEPTH_STENCIL &&
+             firstImage->base.InternalFormat != GL_STENCIL_INDEX)
+            depth_mode = GL_RED;
+      }
+      tex_swizzle = compute_texture_format_swizzle(baseFormat,
+                                                   depth_mode,
+                                                   stObj->pt->format,
+                                                   glsl_version);
+   }
+   else {
+      tex_swizzle = SWIZZLE_XYZW;
    }
 
-   return MAKE_SWIZZLE4(swiz[0], swiz[1], swiz[2], swiz[3]);
+   /* Combine the texture format swizzle with user's swizzle */
+   return swizzle_swizzle(stObj->base._Swizzle, tex_swizzle);
 }
 
 
 /**
- * Return TRUE if the swizzling described by "swizzle" and
- * "depthmode" (for depth textures only) is different from the swizzling
- * set in the given sampler view.
+ * Return TRUE if the texture's sampler view swizzle is not equal to
+ * the texture's swizzle.
  *
- * \param sv         A sampler view.
- * \param swizzle    Texture swizzle, a bitmask computed using MAKE_SWIZZLE4.
- * \param depthmode  One of GL_LUMINANCE, GL_INTENSITY, GL_ALPHA.
+ * \param stObj  the st texture object,
  */
 static boolean
-check_sampler_swizzle(struct pipe_sampler_view *sv,
-                      GLuint swizzle, GLenum depthmode)
+check_sampler_swizzle(const struct st_context *st,
+                      const struct st_texture_object *stObj,
+                     struct pipe_sampler_view *sv, unsigned glsl_version)
+{
+   unsigned swizzle = get_texture_format_swizzle(st, stObj, glsl_version);
+
+   return ((sv->swizzle_r != GET_SWZ(swizzle, 0)) ||
+           (sv->swizzle_g != GET_SWZ(swizzle, 1)) ||
+           (sv->swizzle_b != GET_SWZ(swizzle, 2)) ||
+           (sv->swizzle_a != GET_SWZ(swizzle, 3)));
+}
+
+
+static unsigned last_level(struct st_texture_object *stObj)
 {
-   swizzle = apply_depthmode(sv->texture->format, swizzle, depthmode);
-
-   if ((sv->swizzle_r != GET_SWZ(swizzle, 0)) ||
-       (sv->swizzle_g != GET_SWZ(swizzle, 1)) ||
-       (sv->swizzle_b != GET_SWZ(swizzle, 2)) ||
-       (sv->swizzle_a != GET_SWZ(swizzle, 3)))
-      return TRUE;
-   return FALSE;
+   unsigned ret = MIN2(stObj->base.MinLevel + stObj->base._MaxLevel,
+                       stObj->pt->last_level);
+   if (stObj->base.Immutable)
+      ret = MIN2(ret, stObj->base.MinLevel + stObj->base.NumLevels - 1);
+   return ret;
 }
 
+static unsigned last_layer(struct st_texture_object *stObj)
+{
+   if (stObj->base.Immutable && stObj->pt->array_size > 1)
+      return MIN2(stObj->base.MinLayer + stObj->base.NumLayers - 1,
+                  stObj->pt->array_size - 1);
+   return stObj->pt->array_size - 1;
+}
 
 static struct pipe_sampler_view *
-st_create_texture_sampler_view_from_stobj(struct pipe_context *pipe,
+st_create_texture_sampler_view_from_stobj(struct st_context *st,
                                          struct st_texture_object *stObj,
-                                          const struct gl_sampler_object *samp,
-                                         enum pipe_format format)
+                                         enum pipe_format format,
+                                          unsigned glsl_version)
 {
    struct pipe_sampler_view templ;
-   GLuint swizzle = apply_depthmode(stObj->pt->format,
-                                    stObj->base._Swizzle,
-                                    stObj->base.DepthMode);
+   unsigned swizzle = get_texture_format_swizzle(st, stObj, glsl_version);
 
    u_sampler_view_default_template(&templ,
                                    stObj->pt,
@@ -164,14 +288,20 @@ st_create_texture_sampler_view_from_stobj(struct pipe_context *pipe,
          return NULL;
       size = MIN2(stObj->pt->width0 - base, (unsigned)stObj->base.BufferSize);
 
-      f = ((base * 8) / desc->block.bits) * desc->block.width;
-      n = ((size * 8) / desc->block.bits) * desc->block.width;
+      f = (base / (desc->block.bits / 8)) * desc->block.width;
+      n = (size / (desc->block.bits / 8)) * desc->block.width;
       if (!n)
          return NULL;
       templ.u.buf.first_element = f;
       templ.u.buf.last_element  = f + (n - 1);
    } else {
-      templ.u.tex.first_level = stObj->base.BaseLevel;
+      templ.u.tex.first_level = stObj->base.MinLevel + stObj->base.BaseLevel;
+      templ.u.tex.last_level = last_level(stObj);
+      assert(templ.u.tex.first_level <= templ.u.tex.last_level);
+      templ.u.tex.first_layer = stObj->base.MinLayer;
+      templ.u.tex.last_layer = last_layer(stObj);
+      assert(templ.u.tex.first_layer <= templ.u.tex.last_layer);
+      templ.target = gl_target_to_pipe(stObj->base.Target);
    }
 
    if (swizzle != SWIZZLE_NOOP) {
@@ -181,35 +311,67 @@ st_create_texture_sampler_view_from_stobj(struct pipe_context *pipe,
       templ.swizzle_a = GET_SWZ(swizzle, 3);
    }
 
-   return pipe->create_sampler_view(pipe, stObj->pt, &templ);
+   return st->pipe->create_sampler_view(st->pipe, stObj->pt, &templ);
 }
 
 
 static struct pipe_sampler_view *
-st_get_texture_sampler_view_from_stobj(struct st_texture_object *stObj,
-                                      struct pipe_context *pipe,
-                                       const struct gl_sampler_object *samp,
-                                      enum pipe_format format)
+st_get_texture_sampler_view_from_stobj(struct st_context *st,
+                                       struct st_texture_object *stObj,
+                                      enum pipe_format format,
+                                       unsigned glsl_version)
 {
+   struct pipe_sampler_view **sv;
+   const struct st_texture_image *firstImage;
    if (!stObj || !stObj->pt) {
       return NULL;
    }
 
-   if (!stObj->sampler_view) {
-      stObj->sampler_view =
-         st_create_texture_sampler_view_from_stobj(pipe, stObj, samp, format);
+   sv = st_texture_get_sampler_view(st, stObj);
+
+   if (util_format_is_depth_and_stencil(format)) {
+      if (stObj->base.StencilSampling)
+         format = util_format_stencil_only(format);
+      else {
+         firstImage = st_texture_image_const(_mesa_base_tex_image(&stObj->base));
+         if (firstImage->base._BaseFormat == GL_STENCIL_INDEX)
+            format = util_format_stencil_only(format);
+      }
    }
 
-   return stObj->sampler_view;
-}
+   /* if sampler view has changed dereference it */
+   if (*sv) {
+      if (check_sampler_swizzle(st, stObj, *sv, glsl_version) ||
+         (format != (*sv)->format) ||
+          gl_target_to_pipe(stObj->base.Target) != (*sv)->target ||
+          stObj->base.MinLevel + stObj->base.BaseLevel != (*sv)->u.tex.first_level ||
+          last_level(stObj) != (*sv)->u.tex.last_level ||
+          stObj->base.MinLayer != (*sv)->u.tex.first_layer ||
+          last_layer(stObj) != (*sv)->u.tex.last_layer) {
+        pipe_sampler_view_reference(sv, NULL);
+      }
+   }
 
+   if (!*sv) {
+      *sv = st_create_texture_sampler_view_from_stobj(st, stObj,
+                                                      format, glsl_version);
+
+   } else if ((*sv)->context != st->pipe) {
+      /* Recreate view in correct context, use existing view as template */
+      struct pipe_sampler_view *new_sv =
+         st->pipe->create_sampler_view(st->pipe, stObj->pt, *sv);
+      pipe_sampler_view_reference(sv, NULL);
+      *sv = new_sv;
+   }
+
+   return *sv;
+}
 
 static GLboolean
 update_single_texture(struct st_context *st,
                       struct pipe_sampler_view **sampler_view,
-                     GLuint texUnit)
+                     GLuint texUnit, unsigned glsl_version)
 {
-   struct pipe_context *pipe = st->pipe;
    struct gl_context *ctx = st->ctx;
    const struct gl_sampler_object *samp;
    struct gl_texture_object *texObj;
@@ -236,7 +398,7 @@ update_single_texture(struct st_context *st,
    /* Determine the format of the texture sampler view */
    if (texObj->Target == GL_TEXTURE_BUFFER) {
       view_format =
-         st_mesa_format_to_pipe_format(stObj->base._BufferObjectFormat);
+         st_mesa_format_to_pipe_format(st, stObj->base._BufferObjectFormat);
    }
    else {
       view_format =
@@ -248,20 +410,9 @@ update_single_texture(struct st_context *st,
       }
    }
 
-   /* if sampler view has changed dereference it */
-   if (stObj->sampler_view) {
-      if (check_sampler_swizzle(stObj->sampler_view,
-                               stObj->base._Swizzle,
-                               stObj->base.DepthMode) ||
-         (view_format != stObj->sampler_view->format) ||
-         stObj->base.BaseLevel != stObj->sampler_view->u.tex.first_level) {
-        pipe_sampler_view_release(pipe, &stObj->sampler_view);
-      }
-   }
-
-   *sampler_view = st_get_texture_sampler_view_from_stobj(stObj, pipe,
-                                                         samp,
-                                                         view_format);
+   *sampler_view =
+      st_get_texture_sampler_view_from_stobj(st, stObj, view_format,
+                                             glsl_version);
    return GL_TRUE;
 }
 
@@ -269,7 +420,7 @@ update_single_texture(struct st_context *st,
 
 static void
 update_textures(struct st_context *st,
-                unsigned shader_stage,
+                gl_shader_stage mesa_shader,
                 const struct gl_program *prog,
                 unsigned max_units,
                 struct pipe_sampler_view **sampler_views,
@@ -277,7 +428,11 @@ update_textures(struct st_context *st,
 {
    const GLuint old_max = *num_textures;
    GLbitfield samplers_used = prog->SamplersUsed;
-   GLuint unit, new_count;
+   GLuint unit;
+   struct gl_shader_program *shader =
+      st->ctx->_Shader->CurrentProgram[mesa_shader];
+   unsigned glsl_version = shader ? shader->Version : 0;
+   unsigned shader_stage = st_shader_stage_to_ptarget(mesa_shader);
 
    if (samplers_used == 0x0 && old_max == 0)
       return;
@@ -292,7 +447,8 @@ update_textures(struct st_context *st,
          const GLuint texUnit = prog->SamplerUnits[unit];
          GLboolean retval;
 
-         retval = update_single_texture(st, &sampler_view, texUnit);
+         retval = update_single_texture(st, &sampler_view, texUnit,
+                                        glsl_version);
          if (retval == GL_FALSE)
             continue;
 
@@ -306,16 +462,9 @@ update_textures(struct st_context *st,
       pipe_sampler_view_reference(&(sampler_views[unit]), sampler_view);
    }
 
-   /* Ex: if old_max = 3 and *num_textures = 1, we need to pass an
-    * array of views={X, NULL, NULL} to unref the old texture views
-    * at positions [1] and [2].
-    */
-   new_count = MAX2(*num_textures, old_max);
-   assert(new_count <= max_units);
-
    cso_set_sampler_views(st->cso_context,
                          shader_stage,
-                         new_count,
+                         *num_textures,
                          sampler_views);
 }
 
@@ -328,7 +477,7 @@ update_vertex_textures(struct st_context *st)
 
    if (ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits > 0) {
       update_textures(st,
-                      PIPE_SHADER_VERTEX,
+                      MESA_SHADER_VERTEX,
                       &ctx->VertexProgram._Current->Base,
                       ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits,
                       st->state.sampler_views[PIPE_SHADER_VERTEX],
@@ -343,7 +492,7 @@ update_fragment_textures(struct st_context *st)
    const struct gl_context *ctx = st->ctx;
 
    update_textures(st,
-                   PIPE_SHADER_FRAGMENT,
+                   MESA_SHADER_FRAGMENT,
                    &ctx->FragmentProgram._Current->Base,
                    ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits,
                    st->state.sampler_views[PIPE_SHADER_FRAGMENT],
@@ -358,20 +507,68 @@ update_geometry_textures(struct st_context *st)
 
    if (ctx->GeometryProgram._Current) {
       update_textures(st,
-                      PIPE_SHADER_GEOMETRY,
+                      MESA_SHADER_GEOMETRY,
                       &ctx->GeometryProgram._Current->Base,
-                      ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits,
+                      ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxTextureImageUnits,
                       st->state.sampler_views[PIPE_SHADER_GEOMETRY],
                       &st->state.num_sampler_views[PIPE_SHADER_GEOMETRY]);
    }
 }
 
 
+static void
+update_tessctrl_textures(struct st_context *st)
+{
+   const struct gl_context *ctx = st->ctx;
+
+   if (ctx->TessCtrlProgram._Current) {
+      update_textures(st,
+                      MESA_SHADER_TESS_CTRL,
+                      &ctx->TessCtrlProgram._Current->Base,
+                      ctx->Const.Program[MESA_SHADER_TESS_CTRL].MaxTextureImageUnits,
+                      st->state.sampler_views[PIPE_SHADER_TESS_CTRL],
+                      &st->state.num_sampler_views[PIPE_SHADER_TESS_CTRL]);
+   }
+}
+
+
+static void
+update_tesseval_textures(struct st_context *st)
+{
+   const struct gl_context *ctx = st->ctx;
+
+   if (ctx->TessEvalProgram._Current) {
+      update_textures(st,
+                      MESA_SHADER_TESS_EVAL,
+                      &ctx->TessEvalProgram._Current->Base,
+                      ctx->Const.Program[MESA_SHADER_TESS_EVAL].MaxTextureImageUnits,
+                      st->state.sampler_views[PIPE_SHADER_TESS_EVAL],
+                      &st->state.num_sampler_views[PIPE_SHADER_TESS_EVAL]);
+   }
+}
+
+
+static void
+update_compute_textures(struct st_context *st)
+{
+   const struct gl_context *ctx = st->ctx;
+
+   if (ctx->ComputeProgram._Current) {
+      update_textures(st,
+                      MESA_SHADER_COMPUTE,
+                      &ctx->ComputeProgram._Current->Base,
+                      ctx->Const.Program[MESA_SHADER_COMPUTE].MaxTextureImageUnits,
+                      st->state.sampler_views[PIPE_SHADER_COMPUTE],
+                      &st->state.num_sampler_views[PIPE_SHADER_COMPUTE]);
+   }
+}
+
+
 const struct st_tracked_state st_update_fragment_texture = {
    "st_update_texture",                                        /* name */
    {                                                   /* dirty */
       _NEW_TEXTURE,                                    /* mesa */
-      ST_NEW_FRAGMENT_PROGRAM,                         /* st */
+      ST_NEW_FRAGMENT_PROGRAM | ST_NEW_SAMPLER_VIEWS,  /* st */
    },
    update_fragment_textures                            /* update */
 };
@@ -381,7 +578,7 @@ const struct st_tracked_state st_update_vertex_texture = {
    "st_update_vertex_texture",                         /* name */
    {                                                   /* dirty */
       _NEW_TEXTURE,                                    /* mesa */
-      ST_NEW_VERTEX_PROGRAM,                           /* st */
+      ST_NEW_VERTEX_PROGRAM | ST_NEW_SAMPLER_VIEWS,    /* st */
    },
    update_vertex_textures                              /* update */
 };
@@ -391,52 +588,37 @@ const struct st_tracked_state st_update_geometry_texture = {
    "st_update_geometry_texture",                       /* name */
    {                                                   /* dirty */
       _NEW_TEXTURE,                                    /* mesa */
-      ST_NEW_GEOMETRY_PROGRAM,                         /* st */
+      ST_NEW_GEOMETRY_PROGRAM | ST_NEW_SAMPLER_VIEWS,  /* st */
    },
    update_geometry_textures                            /* update */
 };
 
 
+const struct st_tracked_state st_update_tessctrl_texture = {
+   "st_update_tessctrl_texture",                       /* name */
+   {                                                   /* dirty */
+      _NEW_TEXTURE,                                    /* mesa */
+      ST_NEW_TESSCTRL_PROGRAM | ST_NEW_SAMPLER_VIEWS,  /* st */
+   },
+   update_tessctrl_textures                            /* update */
+};
 
-static void
-finalize_textures(struct st_context *st)
-{
-   struct gl_context *ctx = st->ctx;
-   struct gl_fragment_program *fprog = ctx->FragmentProgram._Current;
-   const GLboolean prev_missing_textures = st->missing_textures;
-   GLuint su;
-
-   st->missing_textures = GL_FALSE;
-
-   for (su = 0; su < ctx->Const.MaxTextureCoordUnits; su++) {
-      if (fprog->Base.SamplersUsed & (1 << su)) {
-         const GLuint texUnit = fprog->Base.SamplerUnits[su];
-         struct gl_texture_object *texObj
-            = ctx->Texture.Unit[texUnit]._Current;
-
-         if (texObj) {
-            GLboolean retval;
-
-            retval = st_finalize_texture(ctx, st->pipe, texObj);
-            if (!retval) {
-               /* out of mem */
-               st->missing_textures = GL_TRUE;
-               continue;
-            }
-         }
-      }
-   }
 
-   if (prev_missing_textures != st->missing_textures)
-      st->dirty.st |= ST_NEW_FRAGMENT_PROGRAM;
-}
+const struct st_tracked_state st_update_tesseval_texture = {
+   "st_update_tesseval_texture",                       /* name */
+   {                                                   /* dirty */
+      _NEW_TEXTURE,                                    /* mesa */
+      ST_NEW_TESSEVAL_PROGRAM | ST_NEW_SAMPLER_VIEWS,  /* st */
+   },
+   update_tesseval_textures                            /* update */
+};
 
 
-const struct st_tracked_state st_finalize_textures = {
-   "st_finalize_textures",             /* name */
-   {                                   /* dirty */
-      _NEW_TEXTURE,                    /* mesa */
-      0,                               /* st */
+const struct st_tracked_state st_update_compute_texture = {
+   "st_update_compute_texture",                        /* name */
+   {                                                   /* dirty */
+      _NEW_TEXTURE,                                    /* mesa */
+      ST_NEW_COMPUTE_PROGRAM | ST_NEW_SAMPLER_VIEWS,   /* st */
    },
-   finalize_textures                   /* update */
+   update_compute_textures                             /* update */
 };