gallium/tgsi: Add a helper for initializing ureg from a shader_info.
[mesa.git] / src / mesa / state_tracker / st_program.c
index 5b0cc4a6a36b80fafbe42a62b0ed76c5876b5cf0..f052655efcee8d76ef5734eac9d8972dd634915d 100644 (file)
@@ -1,8 +1,8 @@
 /**************************************************************************
- * 
+ *
  * Copyright 2007 VMware, Inc.
  * All Rights Reserved.
- * 
+ *
  * Permission is hereby granted, free of charge, to any person obtaining a
  * copy of this software and associated documentation files (the
  * "Software"), to deal in the Software without restriction, including
  * distribute, sub license, and/or sell copies of the Software, and to
  * permit persons to whom the Software is furnished to do so, subject to
  * the following conditions:
- * 
+ *
  * The above copyright notice and this permission notice (including the
  * next paragraph) shall be included in all copies or substantial portions
  * of the Software.
- * 
+ *
  * 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.
@@ -22,7 +22,7 @@
  * 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:
@@ -32,7 +32,7 @@
 
 
 #include "main/errors.h"
-#include "main/imports.h"
+
 #include "main/hash.h"
 #include "main/mtypes.h"
 #include "program/prog_parameter.h"
@@ -40,7 +40,9 @@
 #include "program/prog_to_nir.h"
 #include "program/programopt.h"
 
+#include "compiler/nir/nir.h"
 #include "compiler/nir/nir_serialize.h"
+#include "draw/draw_context.h"
 
 #include "pipe/p_context.h"
 #include "pipe/p_defines.h"
@@ -51,6 +53,8 @@
 #include "tgsi/tgsi_parse.h"
 #include "tgsi/tgsi_ureg.h"
 
+#include "util/u_memory.h"
+
 #include "st_debug.h"
 #include "st_cb_bitmap.h"
 #include "st_cb_drawpixels.h"
@@ -66,6 +70,8 @@
 #include "cso_cache/cso_context.h"
 
 
+static void
+destroy_program_variants(struct st_context *st, struct gl_program *target);
 
 static void
 set_affected_state_flags(uint64_t *states,
@@ -208,138 +214,41 @@ st_set_prog_affected_state_flags(struct gl_program *prog)
    }
 }
 
-static void
-delete_ir(struct pipe_shader_state *ir)
-{
-   if (ir->tokens) {
-      ureg_free_tokens(ir->tokens);
-      ir->tokens = NULL;
-   }
-
-   /* Note: Any setup of ->ir.nir that has had pipe->create_*_state called on
-    * it has resulted in the driver taking ownership of the NIR.  Those
-    * callers should be NULLing out the nir field in any pipe_shader_state
-    * that might have this called in order to indicate that.
-    *
-    * GLSL IR and ARB programs will have set gl_program->nir to the same
-    * shader as ir->ir.nir, so it will be freed by _mesa_delete_program().
-    */
-}
-
-/**
- * Delete a vertex program variant.  Note the caller must unlink
- * the variant from the linked list.
- */
-static void
-delete_vp_variant(struct st_context *st, struct st_vp_variant *vpv)
-{
-   if (vpv->driver_shader) {
-      if (st->has_shareable_shaders || vpv->key.st == st) {
-         cso_delete_vertex_shader(st->cso_context, vpv->driver_shader);
-      } else {
-         st_save_zombie_shader(vpv->key.st, PIPE_SHADER_VERTEX,
-                               vpv->driver_shader);
-      }
-   }
-
-   if (vpv->draw_shader)
-      draw_delete_vertex_shader( st->draw, vpv->draw_shader );
-
-   if (vpv->tokens)
-      ureg_free_tokens(vpv->tokens);
-
-   free( vpv );
-}
-
-
-
-/**
- * Clean out any old compilations:
- */
-void
-st_release_vp_variants( struct st_context *st,
-                        struct st_program *stvp )
-{
-   struct st_vp_variant *vpv;
-
-   for (vpv = stvp->vp_variants; vpv; ) {
-      struct st_vp_variant *next = vpv->next;
-      delete_vp_variant(st, vpv);
-      vpv = next;
-   }
-
-   stvp->vp_variants = NULL;
-
-   delete_ir(&stvp->state);
-}
-
-
 
 /**
- * Delete a fragment program variant.  Note the caller must unlink
- * the variant from the linked list.
+ * Delete a shader variant.  Note the caller must unlink the variant from
+ * the linked list.
  */
 static void
-delete_fp_variant(struct st_context *st, struct st_fp_variant *fpv)
-{
-   if (fpv->driver_shader) {
-      if (st->has_shareable_shaders || fpv->key.st == st) {
-         cso_delete_fragment_shader(st->cso_context, fpv->driver_shader);
-      } else {
-         st_save_zombie_shader(fpv->key.st, PIPE_SHADER_FRAGMENT,
-                               fpv->driver_shader);
-      }
-   }
-
-   free(fpv);
-}
-
-
-/**
- * Free all variants of a fragment program.
- */
-void
-st_release_fp_variants(struct st_context *st, struct st_program *stfp)
-{
-   struct st_fp_variant *fpv;
-
-   for (fpv = stfp->fp_variants; fpv; ) {
-      struct st_fp_variant *next = fpv->next;
-      delete_fp_variant(st, fpv);
-      fpv = next;
-   }
-
-   stfp->fp_variants = NULL;
-
-   delete_ir(&stfp->state);
-}
-
-
-/**
- * Delete a basic program variant.  Note the caller must unlink
- * the variant from the linked list.
- */
-static void
-delete_common_variant(struct st_context *st, struct st_common_variant *v,
-                      GLenum target)
+delete_variant(struct st_context *st, struct st_variant *v, GLenum target)
 {
    if (v->driver_shader) {
-      if (st->has_shareable_shaders || v->key.st == st) {
+      if (target == GL_VERTEX_PROGRAM_ARB &&
+          ((struct st_common_variant*)v)->key.is_draw_shader) {
+         /* Draw shader. */
+         draw_delete_vertex_shader(st->draw, v->driver_shader);
+      } else if (st->has_shareable_shaders || v->st == st) {
          /* The shader's context matches the calling context, or we
           * don't care.
           */
          switch (target) {
+         case GL_VERTEX_PROGRAM_ARB:
+            st->pipe->delete_vs_state(st->pipe, v->driver_shader);
+            break;
          case GL_TESS_CONTROL_PROGRAM_NV:
-            cso_delete_tessctrl_shader(st->cso_context, v->driver_shader);
+            st->pipe->delete_tcs_state(st->pipe, v->driver_shader);
             break;
          case GL_TESS_EVALUATION_PROGRAM_NV:
-            cso_delete_tesseval_shader(st->cso_context, v->driver_shader);
+            st->pipe->delete_tes_state(st->pipe, v->driver_shader);
             break;
          case GL_GEOMETRY_PROGRAM_NV:
-            cso_delete_geometry_shader(st->cso_context, v->driver_shader);
+            st->pipe->delete_gs_state(st->pipe, v->driver_shader);
+            break;
+         case GL_FRAGMENT_PROGRAM_ARB:
+            st->pipe->delete_fs_state(st->pipe, v->driver_shader);
             break;
          case GL_COMPUTE_PROGRAM_NV:
-            cso_delete_compute_shader(st->cso_context, v->driver_shader);
+            st->pipe->delete_compute_state(st->pipe, v->driver_shader);
             break;
          default:
             unreachable("bad shader type in delete_basic_variant");
@@ -348,44 +257,98 @@ delete_common_variant(struct st_context *st, struct st_common_variant *v,
          /* We can't delete a shader with a context different from the one
           * that created it.  Add it to the creating context's zombie list.
           */
-         enum pipe_shader_type type;
-         switch (target) {
-         case GL_TESS_CONTROL_PROGRAM_NV:
-            type = PIPE_SHADER_TESS_CTRL;
-            break;
-         case GL_TESS_EVALUATION_PROGRAM_NV:
-            type = PIPE_SHADER_TESS_EVAL;
-            break;
-         case GL_GEOMETRY_PROGRAM_NV:
-            type = PIPE_SHADER_GEOMETRY;
-            break;
-         default:
-            unreachable("");
-         }
-         st_save_zombie_shader(v->key.st, type, v->driver_shader);
+         enum pipe_shader_type type =
+            pipe_shader_type_from_mesa(_mesa_program_enum_to_shader_stage(target));
+
+         st_save_zombie_shader(v->st, type, v->driver_shader);
       }
    }
 
    free(v);
 }
 
+static void
+st_unbind_program(struct st_context *st, struct st_program *p)
+{
+   /* Unbind the shader in cso_context and re-bind in st/mesa. */
+   switch (p->Base.info.stage) {
+   case MESA_SHADER_VERTEX:
+      cso_set_vertex_shader_handle(st->cso_context, NULL);
+      st->dirty |= ST_NEW_VS_STATE;
+      break;
+   case MESA_SHADER_TESS_CTRL:
+      cso_set_tessctrl_shader_handle(st->cso_context, NULL);
+      st->dirty |= ST_NEW_TCS_STATE;
+      break;
+   case MESA_SHADER_TESS_EVAL:
+      cso_set_tesseval_shader_handle(st->cso_context, NULL);
+      st->dirty |= ST_NEW_TES_STATE;
+      break;
+   case MESA_SHADER_GEOMETRY:
+      cso_set_geometry_shader_handle(st->cso_context, NULL);
+      st->dirty |= ST_NEW_GS_STATE;
+      break;
+   case MESA_SHADER_FRAGMENT:
+      cso_set_fragment_shader_handle(st->cso_context, NULL);
+      st->dirty |= ST_NEW_FS_STATE;
+      break;
+   case MESA_SHADER_COMPUTE:
+      cso_set_compute_shader_handle(st->cso_context, NULL);
+      st->dirty |= ST_NEW_CS_STATE;
+      break;
+   default:
+      unreachable("invalid shader type");
+   }
+}
 
 /**
  * Free all basic program variants.
  */
 void
-st_release_common_variants(struct st_context *st, struct st_program *p)
+st_release_variants(struct st_context *st, struct st_program *p)
 {
-   struct st_common_variant *v;
+   struct st_variant *v;
+
+   /* If we are releasing shaders, re-bind them, because we don't
+    * know which shaders are bound in the driver.
+    */
+   if (p->variants)
+      st_unbind_program(st, p);
 
    for (v = p->variants; v; ) {
-      struct st_common_variant *next = v->next;
-      delete_common_variant(st, v, p->Base.Target);
+      struct st_variant *next = v->next;
+      delete_variant(st, v, p->Base.Target);
       v = next;
    }
 
    p->variants = NULL;
-   delete_ir(&p->state);
+
+   if (p->state.tokens) {
+      ureg_free_tokens(p->state.tokens);
+      p->state.tokens = NULL;
+   }
+
+   /* Note: Any setup of ->ir.nir that has had pipe->create_*_state called on
+    * it has resulted in the driver taking ownership of the NIR.  Those
+    * callers should be NULLing out the nir field in any pipe_shader_state
+    * that might have this called in order to indicate that.
+    *
+    * GLSL IR and ARB programs will have set gl_program->nir to the same
+    * shader as ir->ir.nir, so it will be freed by _mesa_delete_program().
+    */
+}
+
+/**
+ * Free all basic program variants and unref program.
+ */
+void
+st_release_program(struct st_context *st, struct st_program **p)
+{
+   if (!*p)
+      return;
+
+   destroy_program_variants(st, &((*p)->Base));
+   st_reference_prog(st, p, NULL);
 }
 
 void
@@ -425,6 +388,7 @@ st_translate_prog_to_nir(struct st_context *st, struct gl_program *prog,
 
    NIR_PASS_V(nir, st_nir_lower_wpos_ytransform, prog, screen);
    NIR_PASS_V(nir, nir_lower_system_values);
+   NIR_PASS_V(nir, nir_lower_compute_system_values, NULL);
 
    /* Optimise NIR */
    NIR_PASS_V(nir, nir_opt_constant_folding);
@@ -532,8 +496,6 @@ st_translate_vertex_program(struct st_context *st,
    if (stp->Base.arb.IsPositionInvariant)
       _mesa_insert_mvp_code(st->ctx, &stp->Base);
 
-   st_prepare_vertex_program(stp);
-
    /* ARB_vp: */
    if (!stp->glsl_to_tgsi) {
       _mesa_remove_output_reads(&stp->Base, PROGRAM_OUTPUT);
@@ -548,9 +510,53 @@ st_translate_vertex_program(struct st_context *st,
       if (stp->Base.Parameters->NumParameters)
          stp->affected_states |= ST_NEW_VS_CONSTANTS;
 
-      /* No samplers are allowed in ARB_vp. */
+      /* Translate to NIR if preferred. */
+      if (PIPE_SHADER_IR_NIR ==
+          st->pipe->screen->get_shader_param(st->pipe->screen,
+                                             PIPE_SHADER_VERTEX,
+                                             PIPE_SHADER_CAP_PREFERRED_IR)) {
+         assert(!stp->glsl_to_tgsi);
+
+         if (stp->Base.nir)
+            ralloc_free(stp->Base.nir);
+
+         if (stp->serialized_nir) {
+            free(stp->serialized_nir);
+            stp->serialized_nir = NULL;
+         }
+
+         stp->state.type = PIPE_SHADER_IR_NIR;
+         stp->Base.nir = st_translate_prog_to_nir(st, &stp->Base,
+                                                  MESA_SHADER_VERTEX);
+
+         /* We must update stp->Base.info after translation and before
+          * st_prepare_vertex_program is called, because inputs_read
+          * may become outdated after NIR optimization passes.
+          *
+          * For ffvp/ARB_vp inputs_read is populated based
+          * on declared attributes without taking their usage into
+          * consideration. When creating shader variants we expect
+          * that their inputs_read would match the base ones for
+          * input mapping to work properly.
+          */
+         nir_shader_gather_info(stp->Base.nir,
+                                nir_shader_get_entrypoint(stp->Base.nir));
+         st_nir_assign_vs_in_locations(stp->Base.nir);
+         stp->Base.info = stp->Base.nir->info;
+
+         /* For st_draw_feedback, we need to generate TGSI too if draw doesn't
+          * use LLVM.
+          */
+         /* TODO: Draw can't handle lowered IO. */
+         if (draw_has_llvm() && !stp->Base.info.io_lowered) {
+            st_prepare_vertex_program(stp);
+            return true;
+         }
+      }
    }
 
+   st_prepare_vertex_program(stp);
+
    /* Get semantic names and indices. */
    for (attr = 0; attr < VARYING_SLOT_MAX; attr++) {
       if (stp->Base.info.outputs_written & BITFIELD64_BIT(attr)) {
@@ -570,12 +576,7 @@ st_translate_vertex_program(struct st_context *st,
    if (ureg == NULL)
       return false;
 
-   if (stp->Base.info.clip_distance_array_size)
-      ureg_property(ureg, TGSI_PROPERTY_NUM_CLIPDIST_ENABLED,
-                    stp->Base.info.clip_distance_array_size);
-   if (stp->Base.info.cull_distance_array_size)
-      ureg_property(ureg, TGSI_PROPERTY_NUM_CULLDIST_ENABLED,
-                    stp->Base.info.cull_distance_array_size);
+   ureg_setup_shader_info(ureg, &stp->Base.info);
 
    if (ST_DEBUG & DEBUG_MESA) {
       _mesa_print_program(&stp->Base);
@@ -639,56 +640,83 @@ st_translate_vertex_program(struct st_context *st,
       st_store_ir_in_disk_cache(st, &stp->Base, false);
    }
 
-   /* Translate to NIR.
-    *
-    * This must be done after the translation to TGSI is done, because
-    * we'll pass the NIR shader to the driver and the TGSI version to
-    * the draw module for the select/feedback/rasterpos code.
-    */
-   if (st->pipe->screen->get_shader_param(st->pipe->screen,
-                                          PIPE_SHADER_VERTEX,
-                                          PIPE_SHADER_CAP_PREFERRED_IR)) {
-      assert(!stp->glsl_to_tgsi);
-
-      nir_shader *nir =
-         st_translate_prog_to_nir(st, &stp->Base, MESA_SHADER_VERTEX);
-
-      if (stp->state.ir.nir)
-         ralloc_free(stp->state.ir.nir);
-      stp->state.type = PIPE_SHADER_IR_NIR;
-      stp->state.ir.nir = nir;
-      stp->Base.nir = nir;
-      return true;
-   }
-
    return stp->state.tokens != NULL;
 }
 
 static struct nir_shader *
 get_nir_shader(struct st_context *st, struct st_program *stp)
 {
-   if (stp->state.ir.nir)
-      return nir_shader_clone(NULL, stp->state.ir.nir);
+   if (stp->Base.nir) {
+      nir_shader *nir = stp->Base.nir;
+
+      /* The first shader variant takes ownership of NIR, so that there is
+       * no cloning. Additional shader variants are always generated from
+       * serialized NIR to save memory.
+       */
+      stp->Base.nir = NULL;
+      assert(stp->serialized_nir && stp->serialized_nir_size);
+      return nir;
+   }
 
    struct blob_reader blob_reader;
    const struct nir_shader_compiler_options *options =
       st->ctx->Const.ShaderCompilerOptions[stp->Base.info.stage].NirOptions;
 
-   blob_reader_init(&blob_reader, stp->nir_binary, stp->nir_size);
+   blob_reader_init(&blob_reader, stp->serialized_nir, stp->serialized_nir_size);
    return nir_deserialize(NULL, options, &blob_reader);
 }
 
+static void
+lower_ucp(struct st_context *st,
+          struct nir_shader *nir,
+          unsigned ucp_enables,
+          struct gl_program_parameter_list *params)
+{
+   if (nir->info.outputs_written & VARYING_BIT_CLIP_DIST0)
+      NIR_PASS_V(nir, nir_lower_clip_disable, ucp_enables);
+   else {
+      struct pipe_screen *screen = st->pipe->screen;
+      bool can_compact = screen->get_param(screen,
+                                           PIPE_CAP_NIR_COMPACT_ARRAYS);
+      bool use_eye = st->ctx->_Shader->CurrentProgram[MESA_SHADER_VERTEX] != NULL;
+
+      gl_state_index16 clipplane_state[MAX_CLIP_PLANES][STATE_LENGTH];
+      for (int i = 0; i < MAX_CLIP_PLANES; ++i) {
+         if (use_eye) {
+            clipplane_state[i][0] = STATE_CLIPPLANE;
+            clipplane_state[i][1] = i;
+         } else {
+            clipplane_state[i][0] = STATE_INTERNAL;
+            clipplane_state[i][1] = STATE_CLIP_INTERNAL;
+            clipplane_state[i][2] = i;
+         }
+         _mesa_add_state_reference(params, clipplane_state[i]);
+      }
+
+      if (nir->info.stage == MESA_SHADER_VERTEX) {
+         NIR_PASS_V(nir, nir_lower_clip_vs, ucp_enables,
+                    true, can_compact, clipplane_state);
+      } else if (nir->info.stage == MESA_SHADER_GEOMETRY) {
+         NIR_PASS_V(nir, nir_lower_clip_gs, ucp_enables,
+                    can_compact, clipplane_state);
+      }
+
+      NIR_PASS_V(nir, nir_lower_io_to_temporaries,
+                 nir_shader_get_entrypoint(nir), true, false);
+      NIR_PASS_V(nir, nir_lower_global_vars_to_local);
+   }
+}
+
 static const gl_state_index16 depth_range_state[STATE_LENGTH] =
    { STATE_DEPTH_RANGE };
 
-static struct st_vp_variant *
+static struct st_common_variant *
 st_create_vp_variant(struct st_context *st,
                      struct st_program *stvp,
                      const struct st_common_variant_key *key)
 {
-   struct st_vp_variant *vpv = CALLOC_STRUCT(st_vp_variant);
+   struct st_common_variant *vpv = CALLOC_STRUCT(st_common_variant);
    struct pipe_context *pipe = st->pipe;
-   struct pipe_screen *screen = pipe->screen;
    struct pipe_shader_state state = {0};
 
    static const gl_state_index16 point_size_state[STATE_LENGTH] =
@@ -696,11 +724,13 @@ st_create_vp_variant(struct st_context *st,
    struct gl_program_parameter_list *params = stvp->Base.Parameters;
 
    vpv->key = *key;
-   vpv->num_inputs = ((struct st_vertex_program*)stvp)->num_inputs;
 
    state.stream_output = stvp->state.stream_output;
 
-   if (stvp->state.type == PIPE_SHADER_IR_NIR) {
+   if (stvp->state.type == PIPE_SHADER_IR_NIR &&
+       (!key->is_draw_shader ||
+        /* TODO: Draw can't handle lowered IO. */
+        (draw_has_llvm() && !stvp->Base.info.io_lowered))) {
       bool finalize = false;
 
       state.type = PIPE_SHADER_IR_NIR;
@@ -711,7 +741,6 @@ st_create_vp_variant(struct st_context *st,
       }
       if (key->passthrough_edgeflags) {
          NIR_PASS_V(state.ir.nir, nir_lower_passthrough_edgeflags);
-         vpv->num_inputs++;
          finalize = true;
       }
 
@@ -723,28 +752,7 @@ st_create_vp_variant(struct st_context *st,
       }
 
       if (key->lower_ucp) {
-         bool can_compact = screen->get_param(screen,
-                                              PIPE_CAP_NIR_COMPACT_ARRAYS);
-
-         bool use_eye = st->ctx->_Shader->CurrentProgram[MESA_SHADER_VERTEX] != NULL;
-         gl_state_index16 clipplane_state[MAX_CLIP_PLANES][STATE_LENGTH];
-         for (int i = 0; i < MAX_CLIP_PLANES; ++i) {
-            if (use_eye) {
-               clipplane_state[i][0] = STATE_CLIPPLANE;
-               clipplane_state[i][1] = i;
-            } else {
-               clipplane_state[i][0] = STATE_INTERNAL;
-               clipplane_state[i][1] = STATE_CLIP_INTERNAL;
-               clipplane_state[i][2] = i;
-            }
-            _mesa_add_state_reference(params, clipplane_state[i]);
-         }
-
-         NIR_PASS_V(state.ir.nir, nir_lower_clip_vs, key->lower_ucp,
-                    true, can_compact, clipplane_state);
-         NIR_PASS_V(state.ir.nir, nir_lower_io_to_temporaries,
-                    nir_shader_get_entrypoint(state.ir.nir), true, false);
-         NIR_PASS_V(state.ir.nir, nir_lower_global_vars_to_local);
+         lower_ucp(st, state.ir.nir, key->lower_ucp, params);
          finalize = true;
       }
 
@@ -760,18 +768,10 @@ st_create_vp_variant(struct st_context *st,
       if (ST_DEBUG & DEBUG_PRINT_IR)
          nir_print_shader(state.ir.nir, stderr);
 
-      vpv->driver_shader = pipe->create_vs_state(pipe, &state);
-
-      /* When generating a NIR program, we usually don't have TGSI tokens.
-       * However, we do create them for ARB_vertex_program / fixed-function VS
-       * programs which we may need to use with the draw module for legacy
-       * feedback/select emulation.  If they exist, copy them.
-       *
-       * TODO: Lowering for shader variants is not applied to TGSI when
-       * generating a NIR shader.
-       */
-      if (stvp->state.tokens)
-         vpv->tokens = tgsi_dup_tokens(stvp->state.tokens);
+      if (key->is_draw_shader)
+         vpv->base.driver_shader = draw_create_vertex_shader(st->draw, &state);
+      else
+         vpv->base.driver_shader = pipe->create_vs_state(pipe, &state);
 
       return vpv;
    }
@@ -791,11 +791,9 @@ st_create_vp_variant(struct st_context *st,
       if (tokens) {
          tgsi_free_tokens(state.tokens);
          state.tokens = tokens;
-
-         if (key->passthrough_edgeflags)
-            vpv->num_inputs++;
-      } else
+      } else {
          fprintf(stderr, "mesa: cannot emulate deprecated features\n");
+      }
    }
 
    if (key->lower_depth_clamp) {
@@ -813,9 +811,15 @@ st_create_vp_variant(struct st_context *st,
    if (ST_DEBUG & DEBUG_PRINT_IR)
       tgsi_dump(state.tokens, 0);
 
-   vpv->driver_shader = pipe->create_vs_state(pipe, &state);
-   /* Save this for selection/feedback/rasterpos. */
-   vpv->tokens = state.tokens;
+   if (key->is_draw_shader)
+      vpv->base.driver_shader = draw_create_vertex_shader(st->draw, &state);
+   else
+      vpv->base.driver_shader = pipe->create_vs_state(pipe, &state);
+
+   if (state.tokens) {
+      tgsi_free_tokens(state.tokens);
+   }
+
    return vpv;
 }
 
@@ -823,16 +827,17 @@ st_create_vp_variant(struct st_context *st,
 /**
  * Find/create a vertex program variant.
  */
-struct st_vp_variant *
+struct st_common_variant *
 st_get_vp_variant(struct st_context *st,
                   struct st_program *stp,
                   const struct st_common_variant_key *key)
 {
    struct st_vertex_program *stvp = (struct st_vertex_program *)stp;
-   struct st_vp_variant *vpv;
+   struct st_common_variant *vpv;
 
    /* Search for existing variant */
-   for (vpv = stp->vp_variants; vpv; vpv = vpv->next) {
+   for (vpv = st_common_variant(stp->variants); vpv;
+        vpv = st_common_variant(vpv->base.next)) {
       if (memcmp(&vpv->key, key, sizeof(*key)) == 0) {
          break;
       }
@@ -842,16 +847,19 @@ st_get_vp_variant(struct st_context *st,
       /* create now */
       vpv = st_create_vp_variant(st, stp, key);
       if (vpv) {
-          for (unsigned index = 0; index < vpv->num_inputs; ++index) {
-             unsigned attr = stvp->index_to_input[index];
-             if (attr == ST_DOUBLE_ATTRIB_PLACEHOLDER)
-                continue;
-             vpv->vert_attrib_mask |= 1u << attr;
-          }
+         vpv->base.st = key->st;
+
+         unsigned num_inputs = stvp->num_inputs + key->passthrough_edgeflags;
+         for (unsigned index = 0; index < num_inputs; ++index) {
+            unsigned attr = stvp->index_to_input[index];
+            if (attr == ST_DOUBLE_ATTRIB_PLACEHOLDER)
+               continue;
+            vpv->vert_attrib_mask |= 1u << attr;
+         }
 
          /* insert into list */
-         vpv->next = stp->vp_variants;
-         stp->vp_variants = vpv;
+         vpv->base.next = stp->variants;
+         stp->variants = &vpv->base;
       }
    }
 
@@ -894,16 +902,20 @@ st_translate_fragment_program(struct st_context *st,
 
       /* Translate to NIR. */
       if (!stfp->ati_fs &&
+          PIPE_SHADER_IR_NIR ==
           st->pipe->screen->get_shader_param(st->pipe->screen,
                                              PIPE_SHADER_FRAGMENT,
                                              PIPE_SHADER_CAP_PREFERRED_IR)) {
          nir_shader *nir =
             st_translate_prog_to_nir(st, &stfp->Base, MESA_SHADER_FRAGMENT);
 
-         if (stfp->state.ir.nir)
-            ralloc_free(stfp->state.ir.nir);
+         if (stfp->Base.nir)
+            ralloc_free(stfp->Base.nir);
+         if (stfp->serialized_nir) {
+            free(stfp->serialized_nir);
+            stfp->serialized_nir = NULL;
+         }
          stfp->state.type = PIPE_SHADER_IR_NIR;
-         stfp->state.ir.nir = nir;
          stfp->Base.nir = nir;
          return true;
       }
@@ -1141,6 +1153,8 @@ st_translate_fragment_program(struct st_context *st,
    if (ureg == NULL)
       return false;
 
+   ureg_setup_shader_info(ureg, &stfp->Base.info);
+
    if (ST_DEBUG & DEBUG_MESA) {
       _mesa_print_program(&stfp->Base);
       _mesa_print_program_parameters(st->ctx, &stfp->Base);
@@ -1149,29 +1163,6 @@ st_translate_fragment_program(struct st_context *st,
    if (write_all == GL_TRUE)
       ureg_property(ureg, TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS, 1);
 
-   if (stfp->Base.info.fs.depth_layout != FRAG_DEPTH_LAYOUT_NONE) {
-      switch (stfp->Base.info.fs.depth_layout) {
-      case FRAG_DEPTH_LAYOUT_ANY:
-         ureg_property(ureg, TGSI_PROPERTY_FS_DEPTH_LAYOUT,
-                       TGSI_FS_DEPTH_LAYOUT_ANY);
-         break;
-      case FRAG_DEPTH_LAYOUT_GREATER:
-         ureg_property(ureg, TGSI_PROPERTY_FS_DEPTH_LAYOUT,
-                       TGSI_FS_DEPTH_LAYOUT_GREATER);
-         break;
-      case FRAG_DEPTH_LAYOUT_LESS:
-         ureg_property(ureg, TGSI_PROPERTY_FS_DEPTH_LAYOUT,
-                       TGSI_FS_DEPTH_LAYOUT_LESS);
-         break;
-      case FRAG_DEPTH_LAYOUT_UNCHANGED:
-         ureg_property(ureg, TGSI_PROPERTY_FS_DEPTH_LAYOUT,
-                       TGSI_FS_DEPTH_LAYOUT_UNCHANGED);
-         break;
-      default:
-         assert(0);
-      }
-   }
-
    if (stfp->glsl_to_tgsi) {
       st_translate_program(st->ctx,
                            PIPE_SHADER_FRAGMENT,
@@ -1280,13 +1271,14 @@ st_create_fp_variant(struct st_context *st,
       }
 
       if (key->lower_two_sided_color) {
-         NIR_PASS_V(state.ir.nir, nir_lower_two_sided_color);
+         bool face_sysval = st->ctx->Const.GLSLFrontFacingIsSysVal;
+         NIR_PASS_V(state.ir.nir, nir_lower_two_sided_color, face_sysval);
          finalize = true;
       }
 
       if (key->persample_shading) {
           nir_shader *shader = state.ir.nir;
-          nir_foreach_variable(var, &shader->inputs)
+          nir_foreach_shader_in_variable(var, shader)
              var->data.sample = true;
           finalize = true;
       }
@@ -1342,6 +1334,10 @@ st_create_fp_variant(struct st_context *st,
       if (unlikely(key->external.lower_nv12 || key->external.lower_iyuv ||
                    key->external.lower_xy_uxvx || key->external.lower_yx_xuxv ||
                    key->external.lower_ayuv || key->external.lower_xyuv)) {
+
+         st_nir_lower_samplers(pipe->screen, state.ir.nir,
+                               stfp->shader_program, &stfp->Base);
+
          nir_lower_tex_options options = {0};
          options.lower_y_uv_external = key->external.lower_nv12;
          options.lower_y_u_v_external = key->external.lower_iyuv;
@@ -1360,7 +1356,8 @@ st_create_fp_variant(struct st_context *st,
 
       /* This pass needs to happen *after* nir_lower_sampler */
       if (unlikely(key->external.lower_nv12 || key->external.lower_iyuv ||
-                   key->external.lower_xy_uxvx || key->external.lower_yx_xuxv)) {
+                   key->external.lower_xy_uxvx || key->external.lower_yx_xuxv ||
+                   key->external.lower_ayuv || key->external.lower_xyuv)) {
          NIR_PASS_V(state.ir.nir, st_nir_lower_tex_src_plane,
                     ~stfp->Base.SamplersUsed,
                     key->external.lower_nv12 || key->external.lower_xy_uxvx ||
@@ -1382,7 +1379,7 @@ st_create_fp_variant(struct st_context *st,
       if (ST_DEBUG & DEBUG_PRINT_IR)
          nir_print_shader(state.ir.nir, stderr);
 
-      variant->driver_shader = pipe->create_fs_state(pipe, &state);
+      variant->base.driver_shader = pipe->create_fs_state(pipe, &state);
       variant->key = *key;
 
       return variant;
@@ -1514,7 +1511,7 @@ st_create_fp_variant(struct st_context *st,
       tgsi_dump(state.tokens, 0);
 
    /* fill in variant */
-   variant->driver_shader = pipe->create_fs_state(pipe, &state);
+   variant->base.driver_shader = pipe->create_fs_state(pipe, &state);
    variant->key = *key;
 
    if (state.tokens != stfp->state.tokens)
@@ -1533,7 +1530,8 @@ st_get_fp_variant(struct st_context *st,
    struct st_fp_variant *fpv;
 
    /* Search for existing variant */
-   for (fpv = stfp->fp_variants; fpv; fpv = fpv->next) {
+   for (fpv = st_fp_variant(stfp->variants); fpv;
+        fpv = st_fp_variant(fpv->base.next)) {
       if (memcmp(&fpv->key, key, sizeof(*key)) == 0) {
          break;
       }
@@ -1543,6 +1541,8 @@ st_get_fp_variant(struct st_context *st,
       /* create new */
       fpv = st_create_fp_variant(st, stfp, key);
       if (fpv) {
+         fpv->base.st = key->st;
+
          if (key->bitmap || key->drawpixels) {
             /* Regular variants should always come before the
              * bitmap & drawpixels variants, (unless there
@@ -1550,17 +1550,17 @@ st_get_fp_variant(struct st_context *st,
              * st_update_fp can take a fast path when
              * shader_has_one_variant is set.
              */
-            if (!stfp->fp_variants) {
-               stfp->fp_variants = fpv;
+            if (!stfp->variants) {
+               stfp->variants = &fpv->base;
             } else {
                /* insert into list after the first one */
-               fpv->next = stfp->fp_variants->next;
-               stfp->fp_variants->next = fpv;
+               fpv->base.next = stfp->variants->next;
+               stfp->variants->next = &fpv->base;
             }
          } else {
             /* insert into list */
-            fpv->next = stfp->fp_variants;
-            stfp->fp_variants = fpv;
+            fpv->base.next = stfp->variants;
+            stfp->variants = &fpv->base;
          }
       }
    }
@@ -1584,48 +1584,7 @@ st_translate_common_program(struct st_context *st,
    if (ureg == NULL)
       return false;
 
-   switch (stage) {
-   case PIPE_SHADER_TESS_CTRL:
-      ureg_property(ureg, TGSI_PROPERTY_TCS_VERTICES_OUT,
-                    stp->Base.info.tess.tcs_vertices_out);
-      break;
-
-   case PIPE_SHADER_TESS_EVAL:
-      if (stp->Base.info.tess.primitive_mode == GL_ISOLINES)
-         ureg_property(ureg, TGSI_PROPERTY_TES_PRIM_MODE, GL_LINES);
-      else
-         ureg_property(ureg, TGSI_PROPERTY_TES_PRIM_MODE,
-                       stp->Base.info.tess.primitive_mode);
-
-      STATIC_ASSERT((TESS_SPACING_EQUAL + 1) % 3 == PIPE_TESS_SPACING_EQUAL);
-      STATIC_ASSERT((TESS_SPACING_FRACTIONAL_ODD + 1) % 3 ==
-                    PIPE_TESS_SPACING_FRACTIONAL_ODD);
-      STATIC_ASSERT((TESS_SPACING_FRACTIONAL_EVEN + 1) % 3 ==
-                    PIPE_TESS_SPACING_FRACTIONAL_EVEN);
-
-      ureg_property(ureg, TGSI_PROPERTY_TES_SPACING,
-                    (stp->Base.info.tess.spacing + 1) % 3);
-
-      ureg_property(ureg, TGSI_PROPERTY_TES_VERTEX_ORDER_CW,
-                    !stp->Base.info.tess.ccw);
-      ureg_property(ureg, TGSI_PROPERTY_TES_POINT_MODE,
-                    stp->Base.info.tess.point_mode);
-      break;
-
-   case PIPE_SHADER_GEOMETRY:
-      ureg_property(ureg, TGSI_PROPERTY_GS_INPUT_PRIM,
-                    stp->Base.info.gs.input_primitive);
-      ureg_property(ureg, TGSI_PROPERTY_GS_OUTPUT_PRIM,
-                    stp->Base.info.gs.output_primitive);
-      ureg_property(ureg, TGSI_PROPERTY_GS_MAX_OUTPUT_VERTICES,
-                    stp->Base.info.gs.vertices_out);
-      ureg_property(ureg, TGSI_PROPERTY_GS_INVOCATIONS,
-                    stp->Base.info.gs.invocations);
-      break;
-
-   default:
-      break;
-   }
+   ureg_setup_shader_info(ureg, &stp->Base.info);
 
    ubyte inputSlotToAttr[VARYING_SLOT_TESS_MAX];
    ubyte inputMapping[VARYING_SLOT_TESS_MAX];
@@ -1647,13 +1606,6 @@ st_translate_common_program(struct st_context *st,
    memset(outputMapping, 0, sizeof(outputMapping));
    memset(&stp->state, 0, sizeof(stp->state));
 
-   if (prog->info.clip_distance_array_size)
-      ureg_property(ureg, TGSI_PROPERTY_NUM_CLIPDIST_ENABLED,
-                    prog->info.clip_distance_array_size);
-   if (prog->info.cull_distance_array_size)
-      ureg_property(ureg, TGSI_PROPERTY_NUM_CULLDIST_ENABLED,
-                    prog->info.cull_distance_array_size);
-
    /*
     * Convert Mesa program inputs to TGSI input register semantics.
     */
@@ -1760,25 +1712,25 @@ st_translate_common_program(struct st_context *st,
 /**
  * Get/create a basic program variant.
  */
-struct st_common_variant *
+struct st_variant *
 st_get_common_variant(struct st_context *st,
                       struct st_program *prog,
                       const struct st_common_variant_key *key)
 {
    struct pipe_context *pipe = st->pipe;
-   struct st_common_variant *v;
+   struct st_variant *v;
    struct pipe_shader_state state = {0};
+   struct gl_program_parameter_list *params = prog->Base.Parameters;
 
    /* Search for existing variant */
    for (v = prog->variants; v; v = v->next) {
-      if (memcmp(&v->key, key, sizeof(*key)) == 0) {
+      if (memcmp(&st_common_variant(v)->key, key, sizeof(*key)) == 0)
          break;
-      }
    }
 
    if (!v) {
       /* create new */
-      v = CALLOC_STRUCT(st_common_variant);
+      v = (struct st_variant*)CALLOC_STRUCT(st_common_variant);
       if (v) {
         if (prog->state.type == PIPE_SHADER_IR_NIR) {
             bool finalize = false;
@@ -1791,6 +1743,11 @@ st_get_common_variant(struct st_context *st,
                finalize = true;
             }
 
+            if (key->lower_ucp) {
+               lower_ucp(st, state.ir.nir, key->lower_ucp, params);
+               finalize = true;
+            }
+
             state.stream_output = prog->state.stream_output;
 
             if (finalize || !st->allow_st_finalize_nir_twice) {
@@ -1853,7 +1810,8 @@ st_get_common_variant(struct st_context *st,
             return NULL;
          }
 
-         v->key = *key;
+         st_common_variant(v)->key = *key;
+         v->st = key->st;
 
          /* insert into list */
          v->next = prog->variants;
@@ -1875,74 +1833,27 @@ destroy_program_variants(struct st_context *st, struct gl_program *target)
    if (!target || target == &_mesa_DummyProgram)
       return;
 
-   switch (target->Target) {
-   case GL_VERTEX_PROGRAM_ARB:
-      {
-         struct st_program *stvp = (struct st_program *) target;
-         struct st_vp_variant *vpv, **prevPtr = &stvp->vp_variants;
-
-         for (vpv = stvp->vp_variants; vpv; ) {
-            struct st_vp_variant *next = vpv->next;
-            if (vpv->key.st == st) {
-               /* unlink from list */
-               *prevPtr = next;
-               /* destroy this variant */
-               delete_vp_variant(st, vpv);
-            }
-            else {
-               prevPtr = &vpv->next;
-            }
-            vpv = next;
-         }
-      }
-      break;
-   case GL_FRAGMENT_PROGRAM_ARB:
-      {
-         struct st_program *stfp =
-            (struct st_program *) target;
-         struct st_fp_variant *fpv, **prevPtr = &stfp->fp_variants;
-
-         for (fpv = stfp->fp_variants; fpv; ) {
-            struct st_fp_variant *next = fpv->next;
-            if (fpv->key.st == st) {
-               /* unlink from list */
-               *prevPtr = next;
-               /* destroy this variant */
-               delete_fp_variant(st, fpv);
-            }
-            else {
-               prevPtr = &fpv->next;
-            }
-            fpv = next;
+   struct st_program *p = st_program(target);
+   struct st_variant *v, **prevPtr = &p->variants;
+   bool unbound = false;
+
+   for (v = p->variants; v; ) {
+      struct st_variant *next = v->next;
+      if (v->st == st) {
+         if (!unbound) {
+            st_unbind_program(st, p);
+            unbound = true;
          }
+
+         /* unlink from list */
+         *prevPtr = next;
+         /* destroy this variant */
+         delete_variant(st, v, target->Target);
       }
-      break;
-   case GL_GEOMETRY_PROGRAM_NV:
-   case GL_TESS_CONTROL_PROGRAM_NV:
-   case GL_TESS_EVALUATION_PROGRAM_NV:
-   case GL_COMPUTE_PROGRAM_NV:
-      {
-         struct st_program *p = st_program(target);
-         struct st_common_variant *v, **prevPtr = &p->variants;
-
-         for (v = p->variants; v; ) {
-            struct st_common_variant *next = v->next;
-            if (v->key.st == st) {
-               /* unlink from list */
-               *prevPtr = next;
-               /* destroy this variant */
-               delete_common_variant(st, v, target->Target);
-            }
-            else {
-               prevPtr = &v->next;
-            }
-            v = next;
-         }
+      else {
+         prevPtr = &v->next;
       }
-      break;
-   default:
-      _mesa_problem(NULL, "Unexpected program target 0x%x in "
-                    "destroy_program_variants_cb()", target->Target);
+      v = next;
    }
 }
 
@@ -2069,42 +1980,41 @@ st_precompile_shader_variant(struct st_context *st,
 }
 
 void
-st_finalize_program(struct st_context *st, struct gl_program *prog)
+st_serialize_nir(struct st_program *stp)
 {
-   struct st_program *stp = (struct st_program *)prog;
+   if (!stp->serialized_nir) {
+      struct blob blob;
+      size_t size;
+
+      blob_init(&blob);
+      nir_serialize(&blob, stp->Base.nir, false);
+      blob_finish_get_buffer(&blob, &stp->serialized_nir, &size);
+      stp->serialized_nir_size = size;
+   }
+}
 
+void
+st_finalize_program(struct st_context *st, struct gl_program *prog)
+{
    if (st->current_program[prog->info.stage] == prog) {
       if (prog->info.stage == MESA_SHADER_VERTEX)
-         st->dirty |= ST_NEW_VERTEX_PROGRAM(st, stp);
+         st->dirty |= ST_NEW_VERTEX_PROGRAM(st, (struct st_program *)prog);
       else
-         st->dirty |= stp->affected_states;
+         st->dirty |= ((struct st_program *)prog)->affected_states;
    }
 
-   if (prog->nir)
+   if (prog->nir) {
       nir_sweep(prog->nir);
 
+      /* This is only needed for ARB_vp/fp programs and when the disk cache
+       * is disabled. If the disk cache is enabled, GLSL programs are
+       * serialized in write_nir_to_cache.
+       */
+      st_serialize_nir(st_program(prog));
+   }
+
    /* Create Gallium shaders now instead of on demand. */
    if (ST_DEBUG & DEBUG_PRECOMPILE ||
        st->shader_has_one_variant[prog->info.stage])
       st_precompile_shader_variant(st, prog);
-
-   /* Additional shader variants are always generated from serialized NIR
-    * to save memory.
-    */
-   if (prog->nir) {
-      /* Serialize NIR. */
-      struct blob blob;
-      blob_init(&blob);
-      nir_serialize(&blob, prog->nir, false);
-      stp->nir_binary = malloc(blob.size);
-      memcpy(stp->nir_binary, blob.data, blob.size);
-      stp->nir_size = blob.size;
-      blob_finish(&blob);
-
-      /* Free NIR. */
-      assert(stp->state.ir.nir == prog->nir);
-      ralloc_free(prog->nir);
-      prog->nir = NULL;
-      stp->state.ir.nir = NULL;
-   }
 }