broadcom/vc5: Fix CLIF dumping of lists that aren't capped by a HALT.
[mesa.git] / src / mesa / state_tracker / st_atom_shader.c
index 176f3ea68d745f176dce0f33e24f7c779582163e..b5ba33a48845d8ade1d3851c886efc8b552aa7e1 100644 (file)
@@ -1,6 +1,6 @@
 /**************************************************************************
  * 
- * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
+ * Copyright 2003 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.
 
 #include "main/imports.h"
 #include "main/mtypes.h"
-#include "main/macros.h"
-#include "shader/program.h"
+#include "main/framebuffer.h"
+#include "main/texobj.h"
+#include "main/texstate.h"
+#include "program/program.h"
 
 #include "pipe/p_context.h"
 #include "pipe/p_shader_tokens.h"
-
 #include "util/u_simple_shaders.h"
-
 #include "cso_cache/cso_context.h"
+#include "util/u_debug.h"
 
 #include "st_context.h"
 #include "st_atom.h"
 #include "st_program.h"
-#include "st_atom_shader.h"
-#include "st_mesa_to_tgsi.h"
+#include "st_texture.h"
+
+
+static unsigned
+get_texture_target(struct gl_context *ctx, const unsigned unit)
+{
+   struct gl_texture_object *texObj = _mesa_get_tex_unit(ctx, unit)->_Current;
+   gl_texture_index index;
+
+   if (texObj) {
+      index = _mesa_tex_target_to_index(ctx, texObj->Target);
+   } else {
+      /* fallback for missing texture */
+      index = TEXTURE_2D_INDEX;
+   }
 
+   /* Map mesa texture target to TGSI texture target.
+    * Copied from st_mesa_to_tgsi.c, the shadow part is omitted */
+   switch(index) {
+   case TEXTURE_2D_MULTISAMPLE_INDEX: return TGSI_TEXTURE_2D_MSAA;
+   case TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX: return TGSI_TEXTURE_2D_ARRAY_MSAA;
+   case TEXTURE_BUFFER_INDEX: return TGSI_TEXTURE_BUFFER;
+   case TEXTURE_1D_INDEX:   return TGSI_TEXTURE_1D;
+   case TEXTURE_2D_INDEX:   return TGSI_TEXTURE_2D;
+   case TEXTURE_3D_INDEX:   return TGSI_TEXTURE_3D;
+   case TEXTURE_CUBE_INDEX: return TGSI_TEXTURE_CUBE;
+   case TEXTURE_CUBE_ARRAY_INDEX: return TGSI_TEXTURE_CUBE_ARRAY;
+   case TEXTURE_RECT_INDEX: return TGSI_TEXTURE_RECT;
+   case TEXTURE_1D_ARRAY_INDEX:   return TGSI_TEXTURE_1D_ARRAY;
+   case TEXTURE_2D_ARRAY_INDEX:   return TGSI_TEXTURE_2D_ARRAY;
+   case TEXTURE_EXTERNAL_INDEX:   return TGSI_TEXTURE_2D;
+   default:
+      debug_assert(0);
+      return TGSI_TEXTURE_1D;
+   }
+}
 
 
 /**
- * Translate fragment program if needed.
+ * Update fragment program state/atom.  This involves translating the
+ * Mesa fragment program into a gallium fragment program and binding it.
  */
-static void
-translate_fp(struct st_context *st,
-             struct st_fragment_program *stfp)
+void
+st_update_fp( struct st_context *st )
 {
-   const GLbitfield fragInputsRead = stfp->Base.Base.InputsRead;
-
-   if (!stfp->state.tokens) {
-      GLuint inAttr, numIn = 0;
+   struct st_fragment_program *stfp;
+   struct st_fp_variant_key key;
 
-      for (inAttr = 0; inAttr < FRAG_ATTRIB_MAX; inAttr++) {
-         if (fragInputsRead & (1 << inAttr)) {
-            stfp->input_to_slot[inAttr] = numIn;
-            numIn++;
-         }
-         else {
-            stfp->input_to_slot[inAttr] = -1;
+   assert(st->ctx->FragmentProgram._Current);
+   stfp = st_fragment_program(st->ctx->FragmentProgram._Current);
+   assert(stfp->Base.Target == GL_FRAGMENT_PROGRAM_ARB);
+
+   void *shader;
+
+   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);
          }
       }
 
-      stfp->num_input_slots = numIn;
+      key.external = st_get_external_sampler_key(st, &stfp->Base);
 
-      assert(stfp->Base.Base.NumInstructions > 1);
-
-      st_translate_fragment_program(st, stfp, stfp->input_to_slot);
+      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, shader);
+}
 
 
 /**
- * Find a translated vertex program that corresponds to stvp and
- * has outputs matched to stfp's inputs.
- * This performs vertex and fragment translation (to TGSI) when needed.
+ * Update vertex program state/atom.  This involves translating the
+ * Mesa vertex program into a gallium fragment program and binding it.
  */
-static struct st_vp_varient *
-find_translated_vp(struct st_context *st,
-                   struct st_vertex_program *stvp )
+void
+st_update_vp( struct st_context *st )
 {
-   struct st_vp_varient *vpv;
-   struct st_vp_varient_key key;
-
-   /* Nothing in our key yet.  This will change:
-    */
-   memset(&key, 0, sizeof key);
-
-   /* 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.
-    * _NEW_POLYGON, ST_NEW_EDGEFLAGS_DATA
-    */
-   key.passthrough_edgeflags = (st->vertdata_edgeflags && (
-                                st->ctx->Polygon.FrontMode != GL_FILL ||
-                                st->ctx->Polygon.BackMode != GL_FILL));
-
+   struct st_vertex_program *stvp;
+   struct st_vp_variant_key key;
 
-   /* Do we need to throw away old translations after a change in the
-    * GL program string?
+   /* find active shader and params -- Should be covered by
+    * ST_NEW_VERTEX_PROGRAM
     */
-   if (stvp->serialNo != stvp->lastSerialNo) {
-      /* These may have changed if the program string changed.
+   assert(st->ctx->VertexProgram._Current);
+   stvp = st_vertex_program(st->ctx->VertexProgram._Current);
+   assert(stvp->Base.Target == GL_VERTEX_PROGRAM_ARB);
+
+   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.
        */
-      st_prepare_vertex_program( st, stvp );
+      key.passthrough_edgeflags = st->vertdata_edgeflags;
 
-      /* We are now up-to-date:
-       */
-      stvp->lastSerialNo = stvp->serialNo;
-   }
-   
-   /* See if we've got a translated vertex program whose outputs match
-    * the fragment program's inputs.
-    */
-   for (vpv = stvp->varients; vpv; vpv = vpv->next) {
-      if (memcmp(&vpv->key, &key, sizeof key) == 0) {
-         break;
-      }
-   }
+      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));
 
-   /* No?  Perform new translation here. */
-   if (!vpv) {
-      vpv = st_translate_vertex_program(st, stvp, &key);
-      if (!vpv)
-         return NULL;
-      
-      vpv->next = stvp->varients;
-      stvp->varients = vpv;
+      st->vp_variant = st_get_vp_variant(st, stvp, &key);
    }
 
-   return vpv;
+   st_reference_vertprog(st, &st->vp, stvp);
+
+   cso_set_vertex_shader_handle(st->cso_context, 
+                                st->vp_variant->driver_shader);
 }
 
 
-/**
- * Return pointer to a pass-through fragment shader.
- * This shader is used when a texture is missing/incomplete.
- */
 static void *
-get_passthrough_fs(struct st_context *st)
+st_update_common_program(struct st_context *st, struct gl_program *prog,
+                         unsigned pipe_shader, struct st_common_program **dst)
 {
-   if (!st->passthrough_fs) {
-      st->passthrough_fs =
-         util_make_fragment_passthrough_shader(st->pipe);
+   struct st_common_program *stp;
+
+   if (!prog) {
+      st_reference_prog(st, dst, NULL);
+      return NULL;
    }
 
-   return st->passthrough_fs;
-}
+   stp = st_common_program(prog);
+   st_reference_prog(st, dst, stp);
 
+   if (st->shader_has_one_variant[prog->info.stage] && stp->variants)
+      return stp->variants->driver_shader;
 
-/**
- * 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 )
-{
-   struct st_fragment_program *stfp;
+   return st_get_basic_variant(st, pipe_shader, &stp->tgsi,
+                               &stp->variants)->driver_shader;
+}
 
-   assert(st->ctx->FragmentProgram._Current);
-   stfp = st_fragment_program(st->ctx->FragmentProgram._Current);
-   assert(stfp->Base.Base.Target == GL_FRAGMENT_PROGRAM_ARB);
 
-   translate_fp(st, stfp);
+void
+st_update_gp(struct st_context *st)
+{
+   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);
+}
 
-   st_reference_fragprog(st, &st->fp, stfp);
 
-   if (st->missing_textures) {
-      /* use a pass-through frag shader that uses no textures */
-      void *fs = get_passthrough_fs(st);
-      cso_set_fragment_shader_handle(st->cso_context, fs);
-   }
-   else {
-      cso_set_fragment_shader_handle(st->cso_context, stfp->driver_shader);
-   }
+void
+st_update_tcp(struct st_context *st)
+{
+   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_fp = {
-   "st_update_fp",                                     /* name */
-   {                                                   /* dirty */
-      0,                                               /* mesa */
-      ST_NEW_FRAGMENT_PROGRAM                           /* st */
-   },
-   update_fp                                   /* 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);
+}
 
 
-/**
- * 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_cp( struct st_context *st )
 {
-   struct st_vertex_program *stvp;
+   struct st_compute_program *stcp;
 
-   /* find active shader and params -- Should be covered by
-    * ST_NEW_VERTEX_PROGRAM
-    */
-   assert(st->ctx->VertexProgram._Current);
-   stvp = st_vertex_program(st->ctx->VertexProgram._Current);
-   assert(stvp->Base.Base.Target == GL_VERTEX_PROGRAM_ARB);
+   if (!st->ctx->ComputeProgram._Current) {
+      cso_set_compute_shader_handle(st->cso_context, NULL);
+      st_reference_compprog(st, &st->cp, NULL);
+      return;
+   }
 
-   st->vp_varient = find_translated_vp(st, stvp);
+   stcp = st_compute_program(st->ctx->ComputeProgram._Current);
+   assert(stcp->Base.Target == GL_COMPUTE_PROGRAM_NV);
 
-   st_reference_vertprog(st, &st->vp, stvp);
+   void *shader;
 
-   cso_set_vertex_shader_handle(st->cso_context, 
-                                st->vp_varient->driver_shader);
-
-   st->vertex_result_to_slot = stvp->result_to_output;
-}
+   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);
 
-const struct st_tracked_state st_update_vp = {
-   "st_update_vp",                                     /* name */
-   {                                                   /* dirty */
-      _NEW_POLYGON,                                    /* mesa */
-      ST_NEW_VERTEX_PROGRAM | ST_NEW_EDGEFLAGS_DATA    /* st */
-   },
-   update_vp                                   /* update */
-};
+   cso_set_compute_shader_handle(st->cso_context, shader);
+}