st/program: rework basic variant interface
[mesa.git] / src / mesa / state_tracker / st_atom_shader.c
index b77000e38be25a31381a3dc869bd603b2169504d..c6faa3f07ffa205513bbfb01b1a1fae64b63dfbe 100644 (file)
 #include "st_texture.h"
 
 
-/** Compress the fog function enums into a 2-bit value */
-static GLuint
-translate_fog_mode(GLenum mode)
-{
-   switch (mode) {
-   case GL_LINEAR: return 1;
-   case GL_EXP:    return 2;
-   case GL_EXP2:   return 3;
-   default:
-      return 0;
-   }
-}
-
 static unsigned
 get_texture_target(struct gl_context *ctx, const unsigned unit)
 {
@@ -106,8 +93,8 @@ get_texture_target(struct gl_context *ctx, const unsigned unit)
  * Update fragment program state/atom.  This involves translating the
  * Mesa fragment program into a gallium fragment program and binding it.
  */
-static void
-update_fp( struct st_context *st )
+void
+st_update_fp( struct st_context *st )
 {
    struct st_fragment_program *stfp;
    struct st_fp_variant_key key;
@@ -116,56 +103,56 @@ update_fp( struct st_context *st )
    stfp = st_fragment_program(st->ctx->FragmentProgram._Current);
    assert(stfp->Base.Target == GL_FRAGMENT_PROGRAM_ARB);
 
-   memset(&key, 0, sizeof(key));
-   key.st = st->has_shareable_shaders ? NULL : st;
-
-   /* _NEW_FRAG_CLAMP */
-   key.clamp_color = st->clamp_frag_color_in_shader &&
-                     st->ctx->Color._ClampFragmentColor;
-
-   /* _NEW_MULTISAMPLE | _NEW_BUFFERS */
-   key.persample_shading =
-      st->force_persample_in_shader &&
-      _mesa_is_multisample_enabled(st->ctx) &&
-      st->ctx->Multisample.SampleShading &&
-      st->ctx->Multisample.MinSampleShadingValue *
-      _mesa_geometric_samples(st->ctx->DrawBuffer) > 1;
+   void *shader;
 
-   if (stfp->ati_fs) {
-      unsigned u;
-
-      if (st->ctx->Fog.Enabled) {
-         key.fog = translate_fog_mode(st->ctx->Fog.Mode);
-      }
-
-      for (u = 0; u < MAX_NUM_FRAGMENT_REGISTERS_ATI; u++) {
-         key.texture_targets[u] = get_texture_target(st->ctx, u);
+   if (st->shader_has_one_variant[MESA_SHADER_FRAGMENT] &&
+       !stfp->ati_fs && /* ATI_fragment_shader always has multiple variants */
+       !stfp->Base.ExternalSamplersUsed && /* external samplers need variants */
+       stfp->variants &&
+       !stfp->variants->key.drawpixels &&
+       !stfp->variants->key.bitmap) {
+      shader = stfp->variants->driver_shader;
+   } else {
+      memset(&key, 0, sizeof(key));
+      key.st = st->has_shareable_shaders ? NULL : st;
+
+      /* _NEW_FRAG_CLAMP */
+      key.clamp_color = st->clamp_frag_color_in_shader &&
+                        st->ctx->Color._ClampFragmentColor;
+
+      /* _NEW_MULTISAMPLE | _NEW_BUFFERS */
+      key.persample_shading =
+         st->force_persample_in_shader &&
+         _mesa_is_multisample_enabled(st->ctx) &&
+         st->ctx->Multisample.SampleShading &&
+         st->ctx->Multisample.MinSampleShadingValue *
+         _mesa_geometric_samples(st->ctx->DrawBuffer) > 1;
+
+      if (stfp->ati_fs) {
+         key.fog = st->ctx->Fog._PackedEnabledMode;
+
+         for (unsigned u = 0; u < MAX_NUM_FRAGMENT_REGISTERS_ATI; u++) {
+            key.texture_targets[u] = get_texture_target(st->ctx, u);
+         }
       }
-   }
 
-   key.external = st_get_external_sampler_key(st, &stfp->Base);
+      key.external = st_get_external_sampler_key(st, &stfp->Base);
 
-   st->fp_variant = st_get_fp_variant(st, stfp, &key);
+      shader = st_get_fp_variant(st, stfp, &key)->driver_shader;
+   }
 
    st_reference_fragprog(st, &st->fp, stfp);
 
-   cso_set_fragment_shader_handle(st->cso_context,
-                                  st->fp_variant->driver_shader);
+   cso_set_fragment_shader_handle(st->cso_context, shader);
 }
 
 
-const struct st_tracked_state st_update_fp = {
-   update_fp                                   /* update */
-};
-
-
-
 /**
  * Update vertex program state/atom.  This involves translating the
  * Mesa vertex program into a gallium fragment program and binding it.
  */
-static void
-update_vp( struct st_context *st )
+void
+st_update_vp( struct st_context *st )
 {
    struct st_vertex_program *stvp;
    struct st_vp_variant_key key;
@@ -177,131 +164,93 @@ update_vp( struct st_context *st )
    stvp = st_vertex_program(st->ctx->VertexProgram._Current);
    assert(stvp->Base.Target == GL_VERTEX_PROGRAM_ARB);
 
-   memset(&key, 0, sizeof key);
-   key.st = st->has_shareable_shaders ? NULL : st;
-
-   /* When this is true, we will add an extra input to the vertex
-    * shader translation (for edgeflags), an extra output with
-    * edgeflag semantics, and extend the vertex shader to pass through
-    * the input to the output.  We'll need to use similar logic to set
-    * up the extra vertex_element input for edgeflags.
-    */
-   key.passthrough_edgeflags = st->vertdata_edgeflags;
-
-   key.clamp_color = st->clamp_vert_color_in_shader &&
-                     st->ctx->Light._ClampVertexColor &&
-                     (stvp->Base.OutputsWritten &
-                      (VARYING_SLOT_COL0 |
-                       VARYING_SLOT_COL1 |
-                       VARYING_SLOT_BFC0 |
-                       VARYING_SLOT_BFC1));
-
-   st->vp_variant = st_get_vp_variant(st, stvp, &key);
+   if (st->shader_has_one_variant[MESA_SHADER_VERTEX] &&
+       stvp->variants &&
+       stvp->variants->key.passthrough_edgeflags == st->vertdata_edgeflags) {
+      st->vp_variant = stvp->variants;
+   } else {
+      memset(&key, 0, sizeof key);
+      key.st = st->has_shareable_shaders ? NULL : st;
+
+      /* When this is true, we will add an extra input to the vertex
+       * shader translation (for edgeflags), an extra output with
+       * edgeflag semantics, and extend the vertex shader to pass through
+       * the input to the output.  We'll need to use similar logic to set
+       * up the extra vertex_element input for edgeflags.
+       */
+      key.passthrough_edgeflags = st->vertdata_edgeflags;
+
+      key.clamp_color = st->clamp_vert_color_in_shader &&
+                        st->ctx->Light._ClampVertexColor &&
+                        (stvp->Base.info.outputs_written &
+                         (VARYING_SLOT_COL0 |
+                          VARYING_SLOT_COL1 |
+                          VARYING_SLOT_BFC0 |
+                          VARYING_SLOT_BFC1));
+
+      st->vp_variant = st_get_vp_variant(st, stvp, &key);
+   }
 
    st_reference_vertprog(st, &st->vp, stvp);
 
    cso_set_vertex_shader_handle(st->cso_context, 
                                 st->vp_variant->driver_shader);
-
-   st->vertex_result_to_slot = stvp->result_to_output;
 }
 
 
-const struct st_tracked_state st_update_vp = {
-   update_vp                                           /* update */
-};
-
-
-
-static void
-update_gp( struct st_context *st )
+static void *
+st_update_common_program(struct st_context *st, struct gl_program *prog,
+                         unsigned pipe_shader, struct st_common_program **dst)
 {
-   struct st_geometry_program *stgp;
+   struct st_common_program *stp;
 
-   if (!st->ctx->GeometryProgram._Current) {
-      cso_set_geometry_shader_handle(st->cso_context, NULL);
-      st_reference_geomprog(st, &st->gp, NULL);
-      return;
+   if (!prog) {
+      st_reference_prog(st, dst, NULL);
+      return NULL;
    }
 
-   stgp = st_geometry_program(st->ctx->GeometryProgram._Current);
-   assert(stgp->Base.Target == GL_GEOMETRY_PROGRAM_NV);
-
-   st->gp_variant = st_get_basic_variant(st, PIPE_SHADER_GEOMETRY,
-                                         &stgp->tgsi, &stgp->variants);
+   stp = st_common_program(prog);
+   st_reference_prog(st, dst, stp);
 
-   st_reference_geomprog(st, &st->gp, stgp);
+   if (st->shader_has_one_variant[prog->info.stage] && stp->variants)
+      return stp->variants->driver_shader;
 
-   cso_set_geometry_shader_handle(st->cso_context,
-                                  st->gp_variant->driver_shader);
+   return st_get_basic_variant(st, pipe_shader, stp)->driver_shader;
 }
 
-const struct st_tracked_state st_update_gp = {
-   update_gp                           /* update */
-};
-
-
 
-static void
-update_tcp( struct st_context *st )
+void
+st_update_gp(struct st_context *st)
 {
-   struct st_tessctrl_program *sttcp;
-
-   if (!st->ctx->TessCtrlProgram._Current) {
-      cso_set_tessctrl_shader_handle(st->cso_context, NULL);
-      st_reference_tesscprog(st, &st->tcp, NULL);
-      return;
-   }
-
-   sttcp = st_tessctrl_program(st->ctx->TessCtrlProgram._Current);
-   assert(sttcp->Base.Target == GL_TESS_CONTROL_PROGRAM_NV);
-
-   st->tcp_variant = st_get_basic_variant(st, PIPE_SHADER_TESS_CTRL,
-                                          &sttcp->tgsi, &sttcp->variants);
-
-   st_reference_tesscprog(st, &st->tcp, sttcp);
-
-   cso_set_tessctrl_shader_handle(st->cso_context,
-                                  st->tcp_variant->driver_shader);
+   void *shader = st_update_common_program(st,
+                                           st->ctx->GeometryProgram._Current,
+                                           PIPE_SHADER_GEOMETRY, &st->gp);
+   cso_set_geometry_shader_handle(st->cso_context, shader);
 }
 
-const struct st_tracked_state st_update_tcp = {
-   update_tcp                                  /* update */
-};
-
-
 
-static void
-update_tep( struct st_context *st )
+void
+st_update_tcp(struct st_context *st)
 {
-   struct st_tesseval_program *sttep;
-
-   if (!st->ctx->TessEvalProgram._Current) {
-      cso_set_tesseval_shader_handle(st->cso_context, NULL);
-      st_reference_tesseprog(st, &st->tep, NULL);
-      return;
-   }
-
-   sttep = st_tesseval_program(st->ctx->TessEvalProgram._Current);
-   assert(sttep->Base.Target == GL_TESS_EVALUATION_PROGRAM_NV);
-
-   st->tep_variant = st_get_basic_variant(st, PIPE_SHADER_TESS_EVAL,
-                                          &sttep->tgsi, &sttep->variants);
-
-   st_reference_tesseprog(st, &st->tep, sttep);
-
-   cso_set_tesseval_shader_handle(st->cso_context,
-                                  st->tep_variant->driver_shader);
+   void *shader = st_update_common_program(st,
+                                           st->ctx->TessCtrlProgram._Current,
+                                           PIPE_SHADER_TESS_CTRL, &st->tcp);
+   cso_set_tessctrl_shader_handle(st->cso_context, shader);
 }
 
-const struct st_tracked_state st_update_tep = {
-   update_tep                                  /* update */
-};
 
+void
+st_update_tep(struct st_context *st)
+{
+   void *shader = st_update_common_program(st,
+                                           st->ctx->TessEvalProgram._Current,
+                                           PIPE_SHADER_TESS_EVAL, &st->tep);
+   cso_set_tesseval_shader_handle(st->cso_context, shader);
+}
 
 
-static void
-update_cp( struct st_context *st )
+void
+st_update_cp( struct st_context *st )
 {
    struct st_compute_program *stcp;
 
@@ -314,14 +263,16 @@ update_cp( struct st_context *st )
    stcp = st_compute_program(st->ctx->ComputeProgram._Current);
    assert(stcp->Base.Target == GL_COMPUTE_PROGRAM_NV);
 
-   st->cp_variant = st_get_cp_variant(st, &stcp->tgsi, &stcp->variants);
+   void *shader;
+
+   if (st->shader_has_one_variant[MESA_SHADER_COMPUTE] && stcp->variants) {
+      shader = stcp->variants->driver_shader;
+   } else {
+      shader = st_get_cp_variant(st, &stcp->tgsi,
+                                 &stcp->variants)->driver_shader;
+   }
 
    st_reference_compprog(st, &st->cp, stcp);
 
-   cso_set_compute_shader_handle(st->cso_context,
-                                 st->cp_variant->driver_shader);
+   cso_set_compute_shader_handle(st->cso_context, shader);
 }
-
-const struct st_tracked_state st_update_cp = {
-   update_cp                           /* update */
-};