Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. /**************************************************************************
  2.  *
  3.  * Copyright 2007 VMware, Inc.
  4.  * All Rights Reserved.
  5.  *
  6.  * Permission is hereby granted, free of charge, to any person obtaining a
  7.  * copy of this software and associated documentation files (the
  8.  * "Software"), to deal in the Software without restriction, including
  9.  * without limitation the rights to use, copy, modify, merge, publish,
  10.  * distribute, sub license, and/or sell copies of the Software, and to
  11.  * permit persons to whom the Software is furnished to do so, subject to
  12.  * the following conditions:
  13.  *
  14.  * The above copyright notice and this permission notice (including the
  15.  * next paragraph) shall be included in all copies or substantial portions
  16.  * of the Software.
  17.  *
  18.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  19.  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20.  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
  21.  * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
  22.  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  23.  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  24.  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25.  *
  26.  **************************************************************************/
  27.  /*
  28.   * Authors:
  29.   *   Keith Whitwell <keithw@vmware.com>
  30.   *   Brian Paul
  31.   */
  32.  
  33.  
  34. #include "main/imports.h"
  35. #include "main/hash.h"
  36. #include "main/mtypes.h"
  37. #include "program/prog_parameter.h"
  38. #include "program/prog_print.h"
  39. #include "program/programopt.h"
  40.  
  41. #include "pipe/p_context.h"
  42. #include "pipe/p_defines.h"
  43. #include "pipe/p_shader_tokens.h"
  44. #include "draw/draw_context.h"
  45. #include "tgsi/tgsi_dump.h"
  46. #include "tgsi/tgsi_ureg.h"
  47.  
  48. #include "st_debug.h"
  49. #include "st_cb_bitmap.h"
  50. #include "st_cb_drawpixels.h"
  51. #include "st_context.h"
  52. #include "st_program.h"
  53. #include "st_mesa_to_tgsi.h"
  54. #include "cso_cache/cso_context.h"
  55.  
  56.  
  57.  
  58. /**
  59.  * Delete a vertex program variant.  Note the caller must unlink
  60.  * the variant from the linked list.
  61.  */
  62. static void
  63. delete_vp_variant(struct st_context *st, struct st_vp_variant *vpv)
  64. {
  65.    if (vpv->driver_shader)
  66.       cso_delete_vertex_shader(st->cso_context, vpv->driver_shader);
  67.      
  68.    if (vpv->draw_shader)
  69.       draw_delete_vertex_shader( st->draw, vpv->draw_shader );
  70.      
  71.    if (vpv->tgsi.tokens)
  72.       ureg_free_tokens(vpv->tgsi.tokens);
  73.      
  74.    free( vpv );
  75. }
  76.  
  77.  
  78.  
  79. /**
  80.  * Clean out any old compilations:
  81.  */
  82. void
  83. st_release_vp_variants( struct st_context *st,
  84.                         struct st_vertex_program *stvp )
  85. {
  86.    struct st_vp_variant *vpv;
  87.  
  88.    for (vpv = stvp->variants; vpv; ) {
  89.       struct st_vp_variant *next = vpv->next;
  90.       delete_vp_variant(st, vpv);
  91.       vpv = next;
  92.    }
  93.  
  94.    stvp->variants = NULL;
  95. }
  96.  
  97.  
  98.  
  99. /**
  100.  * Delete a fragment program variant.  Note the caller must unlink
  101.  * the variant from the linked list.
  102.  */
  103. static void
  104. delete_fp_variant(struct st_context *st, struct st_fp_variant *fpv)
  105. {
  106.    if (fpv->driver_shader)
  107.       cso_delete_fragment_shader(st->cso_context, fpv->driver_shader);
  108.    if (fpv->parameters)
  109.       _mesa_free_parameter_list(fpv->parameters);
  110.    if (fpv->tgsi.tokens)
  111.       ureg_free_tokens(fpv->tgsi.tokens);
  112.    free(fpv);
  113. }
  114.  
  115.  
  116. /**
  117.  * Free all variants of a fragment program.
  118.  */
  119. void
  120. st_release_fp_variants(struct st_context *st, struct st_fragment_program *stfp)
  121. {
  122.    struct st_fp_variant *fpv;
  123.  
  124.    for (fpv = stfp->variants; fpv; ) {
  125.       struct st_fp_variant *next = fpv->next;
  126.       delete_fp_variant(st, fpv);
  127.       fpv = next;
  128.    }
  129.  
  130.    stfp->variants = NULL;
  131. }
  132.  
  133.  
  134. /**
  135.  * Delete a geometry program variant.  Note the caller must unlink
  136.  * the variant from the linked list.
  137.  */
  138. static void
  139. delete_gp_variant(struct st_context *st, struct st_gp_variant *gpv)
  140. {
  141.    if (gpv->driver_shader)
  142.       cso_delete_geometry_shader(st->cso_context, gpv->driver_shader);
  143.      
  144.    free(gpv);
  145. }
  146.  
  147.  
  148. /**
  149.  * Free all variants of a geometry program.
  150.  */
  151. void
  152. st_release_gp_variants(struct st_context *st, struct st_geometry_program *stgp)
  153. {
  154.    struct st_gp_variant *gpv;
  155.  
  156.    for (gpv = stgp->variants; gpv; ) {
  157.       struct st_gp_variant *next = gpv->next;
  158.       delete_gp_variant(st, gpv);
  159.       gpv = next;
  160.    }
  161.  
  162.    stgp->variants = NULL;
  163. }
  164.  
  165.  
  166.  
  167.  
  168. /**
  169.  * Translate a Mesa vertex shader into a TGSI shader.
  170.  * \param outputMapping  to map vertex program output registers (VARYING_SLOT_x)
  171.  *       to TGSI output slots
  172.  * \param tokensOut  destination for TGSI tokens
  173.  * \return  pointer to cached pipe_shader object.
  174.  */
  175. void
  176. st_prepare_vertex_program(struct gl_context *ctx,
  177.                             struct st_vertex_program *stvp)
  178. {
  179.    struct st_context *st = st_context(ctx);
  180.    GLuint attr;
  181.  
  182.    stvp->num_inputs = 0;
  183.    stvp->num_outputs = 0;
  184.  
  185.    if (stvp->Base.IsPositionInvariant)
  186.       _mesa_insert_mvp_code(ctx, &stvp->Base);
  187.  
  188.    /*
  189.     * Determine number of inputs, the mappings between VERT_ATTRIB_x
  190.     * and TGSI generic input indexes, plus input attrib semantic info.
  191.     */
  192.    for (attr = 0; attr < VERT_ATTRIB_MAX; attr++) {
  193.       if ((stvp->Base.Base.InputsRead & BITFIELD64_BIT(attr)) != 0) {
  194.          stvp->input_to_index[attr] = stvp->num_inputs;
  195.          stvp->index_to_input[stvp->num_inputs] = attr;
  196.          stvp->num_inputs++;
  197.          if ((stvp->Base.Base.DoubleInputsRead & BITFIELD64_BIT(attr)) != 0) {
  198.             /* add placeholder for second part of a double attribute */
  199.             stvp->index_to_input[stvp->num_inputs] = ST_DOUBLE_ATTRIB_PLACEHOLDER;
  200.             stvp->num_inputs++;
  201.          }
  202.       }
  203.    }
  204.    /* bit of a hack, presetup potentially unused edgeflag input */
  205.    stvp->input_to_index[VERT_ATTRIB_EDGEFLAG] = stvp->num_inputs;
  206.    stvp->index_to_input[stvp->num_inputs] = VERT_ATTRIB_EDGEFLAG;
  207.  
  208.    /* Compute mapping of vertex program outputs to slots.
  209.     */
  210.    for (attr = 0; attr < VARYING_SLOT_MAX; attr++) {
  211.       if ((stvp->Base.Base.OutputsWritten & BITFIELD64_BIT(attr)) == 0) {
  212.          stvp->result_to_output[attr] = ~0;
  213.       }
  214.       else {
  215.          unsigned slot = stvp->num_outputs++;
  216.  
  217.          stvp->result_to_output[attr] = slot;
  218.  
  219.          switch (attr) {
  220.          case VARYING_SLOT_POS:
  221.             stvp->output_semantic_name[slot] = TGSI_SEMANTIC_POSITION;
  222.             stvp->output_semantic_index[slot] = 0;
  223.             break;
  224.          case VARYING_SLOT_COL0:
  225.             stvp->output_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
  226.             stvp->output_semantic_index[slot] = 0;
  227.             break;
  228.          case VARYING_SLOT_COL1:
  229.             stvp->output_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
  230.             stvp->output_semantic_index[slot] = 1;
  231.             break;
  232.          case VARYING_SLOT_BFC0:
  233.             stvp->output_semantic_name[slot] = TGSI_SEMANTIC_BCOLOR;
  234.             stvp->output_semantic_index[slot] = 0;
  235.             break;
  236.          case VARYING_SLOT_BFC1:
  237.             stvp->output_semantic_name[slot] = TGSI_SEMANTIC_BCOLOR;
  238.             stvp->output_semantic_index[slot] = 1;
  239.             break;
  240.          case VARYING_SLOT_FOGC:
  241.             stvp->output_semantic_name[slot] = TGSI_SEMANTIC_FOG;
  242.             stvp->output_semantic_index[slot] = 0;
  243.             break;
  244.          case VARYING_SLOT_PSIZ:
  245.             stvp->output_semantic_name[slot] = TGSI_SEMANTIC_PSIZE;
  246.             stvp->output_semantic_index[slot] = 0;
  247.             break;
  248.          case VARYING_SLOT_CLIP_DIST0:
  249.             stvp->output_semantic_name[slot] = TGSI_SEMANTIC_CLIPDIST;
  250.             stvp->output_semantic_index[slot] = 0;
  251.             break;
  252.          case VARYING_SLOT_CLIP_DIST1:
  253.             stvp->output_semantic_name[slot] = TGSI_SEMANTIC_CLIPDIST;
  254.             stvp->output_semantic_index[slot] = 1;
  255.             break;
  256.          case VARYING_SLOT_EDGE:
  257.             assert(0);
  258.             break;
  259.          case VARYING_SLOT_CLIP_VERTEX:
  260.             stvp->output_semantic_name[slot] = TGSI_SEMANTIC_CLIPVERTEX;
  261.             stvp->output_semantic_index[slot] = 0;
  262.             break;
  263.          case VARYING_SLOT_LAYER:
  264.             stvp->output_semantic_name[slot] = TGSI_SEMANTIC_LAYER;
  265.             stvp->output_semantic_index[slot] = 0;
  266.             break;
  267.          case VARYING_SLOT_VIEWPORT:
  268.             stvp->output_semantic_name[slot] = TGSI_SEMANTIC_VIEWPORT_INDEX;
  269.             stvp->output_semantic_index[slot] = 0;
  270.             break;
  271.  
  272.          case VARYING_SLOT_TEX0:
  273.          case VARYING_SLOT_TEX1:
  274.          case VARYING_SLOT_TEX2:
  275.          case VARYING_SLOT_TEX3:
  276.          case VARYING_SLOT_TEX4:
  277.          case VARYING_SLOT_TEX5:
  278.          case VARYING_SLOT_TEX6:
  279.          case VARYING_SLOT_TEX7:
  280.             if (st->needs_texcoord_semantic) {
  281.                stvp->output_semantic_name[slot] = TGSI_SEMANTIC_TEXCOORD;
  282.                stvp->output_semantic_index[slot] = attr - VARYING_SLOT_TEX0;
  283.                break;
  284.             }
  285.             /* fall through */
  286.          case VARYING_SLOT_VAR0:
  287.          default:
  288.             assert(attr < VARYING_SLOT_MAX);
  289.             stvp->output_semantic_name[slot] = TGSI_SEMANTIC_GENERIC;
  290.             stvp->output_semantic_index[slot] =
  291.                st_get_generic_varying_index(st, attr);
  292.             break;
  293.          }
  294.       }
  295.    }
  296.    /* similar hack to above, presetup potentially unused edgeflag output */
  297.    stvp->result_to_output[VARYING_SLOT_EDGE] = stvp->num_outputs;
  298.    stvp->output_semantic_name[stvp->num_outputs] = TGSI_SEMANTIC_EDGEFLAG;
  299.    stvp->output_semantic_index[stvp->num_outputs] = 0;
  300. }
  301.  
  302.  
  303. /**
  304.  * Translate a vertex program to create a new variant.
  305.  */
  306. static struct st_vp_variant *
  307. st_translate_vertex_program(struct st_context *st,
  308.                             struct st_vertex_program *stvp,
  309.                             const struct st_vp_variant_key *key)
  310. {
  311.    struct st_vp_variant *vpv = CALLOC_STRUCT(st_vp_variant);
  312.    struct pipe_context *pipe = st->pipe;
  313.    struct ureg_program *ureg;
  314.    enum pipe_error error;
  315.    unsigned num_outputs;
  316.  
  317.    st_prepare_vertex_program(st->ctx, stvp);
  318.  
  319.    if (!stvp->glsl_to_tgsi)
  320.    {
  321.       _mesa_remove_output_reads(&stvp->Base.Base, PROGRAM_OUTPUT);
  322.    }
  323.  
  324.    ureg = ureg_create( TGSI_PROCESSOR_VERTEX );
  325.    if (ureg == NULL) {
  326.       free(vpv);
  327.       return NULL;
  328.    }
  329.  
  330.    vpv->key = *key;
  331.  
  332.    vpv->num_inputs = stvp->num_inputs;
  333.    num_outputs = stvp->num_outputs;
  334.    if (key->passthrough_edgeflags) {
  335.       vpv->num_inputs++;
  336.       num_outputs++;
  337.    }
  338.  
  339.    if (ST_DEBUG & DEBUG_MESA) {
  340.       _mesa_print_program(&stvp->Base.Base);
  341.       _mesa_print_program_parameters(st->ctx, &stvp->Base.Base);
  342.       debug_printf("\n");
  343.    }
  344.  
  345.    if (stvp->glsl_to_tgsi)
  346.       error = st_translate_program(st->ctx,
  347.                                    TGSI_PROCESSOR_VERTEX,
  348.                                    ureg,
  349.                                    stvp->glsl_to_tgsi,
  350.                                    &stvp->Base.Base,
  351.                                    /* inputs */
  352.                                    vpv->num_inputs,
  353.                                    stvp->input_to_index,
  354.                                    NULL, /* input semantic name */
  355.                                    NULL, /* input semantic index */
  356.                                    NULL, /* interp mode */
  357.                                    NULL, /* interp location */
  358.                                    /* outputs */
  359.                                    num_outputs,
  360.                                    stvp->result_to_output,
  361.                                    stvp->output_semantic_name,
  362.                                    stvp->output_semantic_index,
  363.                                    key->passthrough_edgeflags,
  364.                                    key->clamp_color);
  365.    else
  366.       error = st_translate_mesa_program(st->ctx,
  367.                                         TGSI_PROCESSOR_VERTEX,
  368.                                         ureg,
  369.                                         &stvp->Base.Base,
  370.                                         /* inputs */
  371.                                         vpv->num_inputs,
  372.                                         stvp->input_to_index,
  373.                                         NULL, /* input semantic name */
  374.                                         NULL, /* input semantic index */
  375.                                         NULL,
  376.                                         /* outputs */
  377.                                         num_outputs,
  378.                                         stvp->result_to_output,
  379.                                         stvp->output_semantic_name,
  380.                                         stvp->output_semantic_index,
  381.                                         key->passthrough_edgeflags,
  382.                                         key->clamp_color);
  383.  
  384.    if (error)
  385.       goto fail;
  386.  
  387.    vpv->tgsi.tokens = ureg_get_tokens( ureg, NULL );
  388.    if (!vpv->tgsi.tokens)
  389.       goto fail;
  390.  
  391.    ureg_destroy( ureg );
  392.  
  393.    if (stvp->glsl_to_tgsi) {
  394.       st_translate_stream_output_info(stvp->glsl_to_tgsi,
  395.                                       stvp->result_to_output,
  396.                                       &vpv->tgsi.stream_output);
  397.    }
  398.  
  399.    if (ST_DEBUG & DEBUG_TGSI) {
  400.       tgsi_dump(vpv->tgsi.tokens, 0);
  401.       debug_printf("\n");
  402.    }
  403.  
  404.    vpv->driver_shader = pipe->create_vs_state(pipe, &vpv->tgsi);
  405.    return vpv;
  406.  
  407. fail:
  408.    debug_printf("%s: failed to translate Mesa program:\n", __func__);
  409.    _mesa_print_program(&stvp->Base.Base);
  410.    debug_assert(0);
  411.  
  412.    ureg_destroy( ureg );
  413.    return NULL;
  414. }
  415.  
  416.  
  417. /**
  418.  * Find/create a vertex program variant.
  419.  */
  420. struct st_vp_variant *
  421. st_get_vp_variant(struct st_context *st,
  422.                   struct st_vertex_program *stvp,
  423.                   const struct st_vp_variant_key *key)
  424. {
  425.    struct st_vp_variant *vpv;
  426.  
  427.    /* Search for existing variant */
  428.    for (vpv = stvp->variants; vpv; vpv = vpv->next) {
  429.       if (memcmp(&vpv->key, key, sizeof(*key)) == 0) {
  430.          break;
  431.       }
  432.    }
  433.  
  434.    if (!vpv) {
  435.       /* create now */
  436.       vpv = st_translate_vertex_program(st, stvp, key);
  437.       if (vpv) {
  438.          /* insert into list */
  439.          vpv->next = stvp->variants;
  440.          stvp->variants = vpv;
  441.       }
  442.    }
  443.  
  444.    return vpv;
  445. }
  446.  
  447.  
  448. static unsigned
  449. st_translate_interp(enum glsl_interp_qualifier glsl_qual, bool is_color)
  450. {
  451.    switch (glsl_qual) {
  452.    case INTERP_QUALIFIER_NONE:
  453.       if (is_color)
  454.          return TGSI_INTERPOLATE_COLOR;
  455.       return TGSI_INTERPOLATE_PERSPECTIVE;
  456.    case INTERP_QUALIFIER_SMOOTH:
  457.       return TGSI_INTERPOLATE_PERSPECTIVE;
  458.    case INTERP_QUALIFIER_FLAT:
  459.       return TGSI_INTERPOLATE_CONSTANT;
  460.    case INTERP_QUALIFIER_NOPERSPECTIVE:
  461.       return TGSI_INTERPOLATE_LINEAR;
  462.    default:
  463.       assert(0 && "unexpected interp mode in st_translate_interp()");
  464.       return TGSI_INTERPOLATE_PERSPECTIVE;
  465.    }
  466. }
  467.  
  468.  
  469. /**
  470.  * Translate a Mesa fragment shader into a TGSI shader using extra info in
  471.  * the key.
  472.  * \return  new fragment program variant
  473.  */
  474. static struct st_fp_variant *
  475. st_translate_fragment_program(struct st_context *st,
  476.                               struct st_fragment_program *stfp,
  477.                               const struct st_fp_variant_key *key)
  478. {
  479.    struct pipe_context *pipe = st->pipe;
  480.    struct st_fp_variant *variant = CALLOC_STRUCT(st_fp_variant);
  481.    GLboolean deleteFP = GL_FALSE;
  482.  
  483.    GLuint outputMapping[FRAG_RESULT_MAX];
  484.    GLuint inputMapping[VARYING_SLOT_MAX];
  485.    GLuint interpMode[PIPE_MAX_SHADER_INPUTS];  /* XXX size? */
  486.    GLuint interpLocation[PIPE_MAX_SHADER_INPUTS];
  487.    GLuint attr;
  488.    GLbitfield64 inputsRead;
  489.    struct ureg_program *ureg;
  490.  
  491.    GLboolean write_all = GL_FALSE;
  492.  
  493.    ubyte input_semantic_name[PIPE_MAX_SHADER_INPUTS];
  494.    ubyte input_semantic_index[PIPE_MAX_SHADER_INPUTS];
  495.    uint fs_num_inputs = 0;
  496.  
  497.    ubyte fs_output_semantic_name[PIPE_MAX_SHADER_OUTPUTS];
  498.    ubyte fs_output_semantic_index[PIPE_MAX_SHADER_OUTPUTS];
  499.    uint fs_num_outputs = 0;
  500.  
  501.    if (!variant)
  502.       return NULL;
  503.  
  504.    assert(!(key->bitmap && key->drawpixels));
  505.  
  506.    if (key->bitmap) {
  507.       /* glBitmap drawing */
  508.       struct gl_fragment_program *fp; /* we free this temp program below */
  509.  
  510.       st_make_bitmap_fragment_program(st, &stfp->Base,
  511.                                       &fp, &variant->bitmap_sampler);
  512.  
  513.       variant->parameters = _mesa_clone_parameter_list(fp->Base.Parameters);
  514.       stfp = st_fragment_program(fp);
  515.       deleteFP = GL_TRUE;
  516.    }
  517.    else if (key->drawpixels) {
  518.       /* glDrawPixels drawing */
  519.       struct gl_fragment_program *fp; /* we free this temp program below */
  520.  
  521.       if (key->drawpixels_z || key->drawpixels_stencil) {
  522.          fp = st_make_drawpix_z_stencil_program(st, key->drawpixels_z,
  523.                                                 key->drawpixels_stencil);
  524.       }
  525.       else {
  526.          /* RGBA */
  527.          st_make_drawpix_fragment_program(st, &stfp->Base, &fp);
  528.          variant->parameters = _mesa_clone_parameter_list(fp->Base.Parameters);
  529.          deleteFP = GL_TRUE;
  530.       }
  531.       stfp = st_fragment_program(fp);
  532.    }
  533.  
  534.    if (!stfp->glsl_to_tgsi)
  535.       _mesa_remove_output_reads(&stfp->Base.Base, PROGRAM_OUTPUT);
  536.  
  537.    /*
  538.     * Convert Mesa program inputs to TGSI input register semantics.
  539.     */
  540.    inputsRead = stfp->Base.Base.InputsRead;
  541.    for (attr = 0; attr < VARYING_SLOT_MAX; attr++) {
  542.       if ((inputsRead & BITFIELD64_BIT(attr)) != 0) {
  543.          const GLuint slot = fs_num_inputs++;
  544.  
  545.          inputMapping[attr] = slot;
  546.          if (stfp->Base.IsCentroid & BITFIELD64_BIT(attr))
  547.             interpLocation[slot] = TGSI_INTERPOLATE_LOC_CENTROID;
  548.          else if (stfp->Base.IsSample & BITFIELD64_BIT(attr))
  549.             interpLocation[slot] = TGSI_INTERPOLATE_LOC_SAMPLE;
  550.          else
  551.             interpLocation[slot] = TGSI_INTERPOLATE_LOC_CENTER;
  552.  
  553.          if (key->persample_shading)
  554.             interpLocation[slot] = TGSI_INTERPOLATE_LOC_SAMPLE;
  555.  
  556.          switch (attr) {
  557.          case VARYING_SLOT_POS:
  558.             input_semantic_name[slot] = TGSI_SEMANTIC_POSITION;
  559.             input_semantic_index[slot] = 0;
  560.             interpMode[slot] = TGSI_INTERPOLATE_LINEAR;
  561.             break;
  562.          case VARYING_SLOT_COL0:
  563.             input_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
  564.             input_semantic_index[slot] = 0;
  565.             interpMode[slot] = st_translate_interp(stfp->Base.InterpQualifier[attr],
  566.                                                    TRUE);
  567.             break;
  568.          case VARYING_SLOT_COL1:
  569.             input_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
  570.             input_semantic_index[slot] = 1;
  571.             interpMode[slot] = st_translate_interp(stfp->Base.InterpQualifier[attr],
  572.                                                    TRUE);
  573.             break;
  574.          case VARYING_SLOT_FOGC:
  575.             input_semantic_name[slot] = TGSI_SEMANTIC_FOG;
  576.             input_semantic_index[slot] = 0;
  577.             interpMode[slot] = TGSI_INTERPOLATE_PERSPECTIVE;
  578.             break;
  579.          case VARYING_SLOT_FACE:
  580.             input_semantic_name[slot] = TGSI_SEMANTIC_FACE;
  581.             input_semantic_index[slot] = 0;
  582.             interpMode[slot] = TGSI_INTERPOLATE_CONSTANT;
  583.             break;
  584.          case VARYING_SLOT_PRIMITIVE_ID:
  585.             input_semantic_name[slot] = TGSI_SEMANTIC_PRIMID;
  586.             input_semantic_index[slot] = 0;
  587.             interpMode[slot] = TGSI_INTERPOLATE_CONSTANT;
  588.             break;
  589.          case VARYING_SLOT_LAYER:
  590.             input_semantic_name[slot] = TGSI_SEMANTIC_LAYER;
  591.             input_semantic_index[slot] = 0;
  592.             interpMode[slot] = TGSI_INTERPOLATE_CONSTANT;
  593.             break;
  594.          case VARYING_SLOT_VIEWPORT:
  595.             input_semantic_name[slot] = TGSI_SEMANTIC_VIEWPORT_INDEX;
  596.             input_semantic_index[slot] = 0;
  597.             interpMode[slot] = TGSI_INTERPOLATE_CONSTANT;
  598.             break;
  599.          case VARYING_SLOT_CLIP_DIST0:
  600.             input_semantic_name[slot] = TGSI_SEMANTIC_CLIPDIST;
  601.             input_semantic_index[slot] = 0;
  602.             interpMode[slot] = TGSI_INTERPOLATE_PERSPECTIVE;
  603.             break;
  604.          case VARYING_SLOT_CLIP_DIST1:
  605.             input_semantic_name[slot] = TGSI_SEMANTIC_CLIPDIST;
  606.             input_semantic_index[slot] = 1;
  607.             interpMode[slot] = TGSI_INTERPOLATE_PERSPECTIVE;
  608.             break;
  609.             /* In most cases, there is nothing special about these
  610.              * inputs, so adopt a convention to use the generic
  611.              * semantic name and the mesa VARYING_SLOT_ number as the
  612.              * index.
  613.              *
  614.              * All that is required is that the vertex shader labels
  615.              * its own outputs similarly, and that the vertex shader
  616.              * generates at least every output required by the
  617.              * fragment shader plus fixed-function hardware (such as
  618.              * BFC).
  619.              *
  620.              * However, some drivers may need us to identify the PNTC and TEXi
  621.              * varyings if, for example, their capability to replace them with
  622.              * sprite coordinates is limited.
  623.              */
  624.          case VARYING_SLOT_PNTC:
  625.             if (st->needs_texcoord_semantic) {
  626.                input_semantic_name[slot] = TGSI_SEMANTIC_PCOORD;
  627.                input_semantic_index[slot] = 0;
  628.                interpMode[slot] = TGSI_INTERPOLATE_LINEAR;
  629.                break;
  630.             }
  631.             /* fall through */
  632.          case VARYING_SLOT_TEX0:
  633.          case VARYING_SLOT_TEX1:
  634.          case VARYING_SLOT_TEX2:
  635.          case VARYING_SLOT_TEX3:
  636.          case VARYING_SLOT_TEX4:
  637.          case VARYING_SLOT_TEX5:
  638.          case VARYING_SLOT_TEX6:
  639.          case VARYING_SLOT_TEX7:
  640.             if (st->needs_texcoord_semantic) {
  641.                input_semantic_name[slot] = TGSI_SEMANTIC_TEXCOORD;
  642.                input_semantic_index[slot] = attr - VARYING_SLOT_TEX0;
  643.                interpMode[slot] =
  644.                   st_translate_interp(stfp->Base.InterpQualifier[attr], FALSE);
  645.                break;
  646.             }
  647.             /* fall through */
  648.          case VARYING_SLOT_VAR0:
  649.          default:
  650.             /* Semantic indices should be zero-based because drivers may choose
  651.              * to assign a fixed slot determined by that index.
  652.              * This is useful because ARB_separate_shader_objects uses location
  653.              * qualifiers for linkage, and if the semantic index corresponds to
  654.              * these locations, linkage passes in the driver become unecessary.
  655.              *
  656.              * If needs_texcoord_semantic is true, no semantic indices will be
  657.              * consumed for the TEXi varyings, and we can base the locations of
  658.              * the user varyings on VAR0.  Otherwise, we use TEX0 as base index.
  659.              */
  660.             assert(attr >= VARYING_SLOT_TEX0);
  661.             input_semantic_name[slot] = TGSI_SEMANTIC_GENERIC;
  662.             input_semantic_index[slot] = st_get_generic_varying_index(st, attr);
  663.             if (attr == VARYING_SLOT_PNTC)
  664.                interpMode[slot] = TGSI_INTERPOLATE_LINEAR;
  665.             else
  666.                interpMode[slot] = st_translate_interp(stfp->Base.InterpQualifier[attr],
  667.                                                       FALSE);
  668.             break;
  669.          }
  670.       }
  671.       else {
  672.          inputMapping[attr] = -1;
  673.       }
  674.    }
  675.  
  676.    /*
  677.     * Semantics and mapping for outputs
  678.     */
  679.    {
  680.       uint numColors = 0;
  681.       GLbitfield64 outputsWritten = stfp->Base.Base.OutputsWritten;
  682.  
  683.       /* if z is written, emit that first */
  684.       if (outputsWritten & BITFIELD64_BIT(FRAG_RESULT_DEPTH)) {
  685.          fs_output_semantic_name[fs_num_outputs] = TGSI_SEMANTIC_POSITION;
  686.          fs_output_semantic_index[fs_num_outputs] = 0;
  687.          outputMapping[FRAG_RESULT_DEPTH] = fs_num_outputs;
  688.          fs_num_outputs++;
  689.          outputsWritten &= ~(1 << FRAG_RESULT_DEPTH);
  690.       }
  691.  
  692.       if (outputsWritten & BITFIELD64_BIT(FRAG_RESULT_STENCIL)) {
  693.          fs_output_semantic_name[fs_num_outputs] = TGSI_SEMANTIC_STENCIL;
  694.          fs_output_semantic_index[fs_num_outputs] = 0;
  695.          outputMapping[FRAG_RESULT_STENCIL] = fs_num_outputs;
  696.          fs_num_outputs++;
  697.          outputsWritten &= ~(1 << FRAG_RESULT_STENCIL);
  698.       }
  699.  
  700.       if (outputsWritten & BITFIELD64_BIT(FRAG_RESULT_SAMPLE_MASK)) {
  701.          fs_output_semantic_name[fs_num_outputs] = TGSI_SEMANTIC_SAMPLEMASK;
  702.          fs_output_semantic_index[fs_num_outputs] = 0;
  703.          outputMapping[FRAG_RESULT_SAMPLE_MASK] = fs_num_outputs;
  704.          fs_num_outputs++;
  705.          outputsWritten &= ~(1 << FRAG_RESULT_SAMPLE_MASK);
  706.       }
  707.  
  708.       /* handle remaining outputs (color) */
  709.       for (attr = 0; attr < FRAG_RESULT_MAX; attr++) {
  710.          if (outputsWritten & BITFIELD64_BIT(attr)) {
  711.             switch (attr) {
  712.             case FRAG_RESULT_DEPTH:
  713.             case FRAG_RESULT_STENCIL:
  714.             case FRAG_RESULT_SAMPLE_MASK:
  715.                /* handled above */
  716.                assert(0);
  717.                break;
  718.             case FRAG_RESULT_COLOR:
  719.                write_all = GL_TRUE; /* fallthrough */
  720.             default:
  721.                assert(attr == FRAG_RESULT_COLOR ||
  722.                       (FRAG_RESULT_DATA0 <= attr && attr < FRAG_RESULT_MAX));
  723.                fs_output_semantic_name[fs_num_outputs] = TGSI_SEMANTIC_COLOR;
  724.                fs_output_semantic_index[fs_num_outputs] = numColors;
  725.                outputMapping[attr] = fs_num_outputs;
  726.                numColors++;
  727.                break;
  728.             }
  729.  
  730.             fs_num_outputs++;
  731.          }
  732.       }
  733.    }
  734.  
  735.    ureg = ureg_create( TGSI_PROCESSOR_FRAGMENT );
  736.    if (ureg == NULL) {
  737.       free(variant);
  738.       return NULL;
  739.    }
  740.  
  741.    if (ST_DEBUG & DEBUG_MESA) {
  742.       _mesa_print_program(&stfp->Base.Base);
  743.       _mesa_print_program_parameters(st->ctx, &stfp->Base.Base);
  744.       debug_printf("\n");
  745.    }
  746.    if (write_all == GL_TRUE)
  747.       ureg_property(ureg, TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS, 1);
  748.  
  749.    if (stfp->Base.FragDepthLayout != FRAG_DEPTH_LAYOUT_NONE) {
  750.       switch (stfp->Base.FragDepthLayout) {
  751.       case FRAG_DEPTH_LAYOUT_ANY:
  752.          ureg_property(ureg, TGSI_PROPERTY_FS_DEPTH_LAYOUT,
  753.                        TGSI_FS_DEPTH_LAYOUT_ANY);
  754.          break;
  755.       case FRAG_DEPTH_LAYOUT_GREATER:
  756.          ureg_property(ureg, TGSI_PROPERTY_FS_DEPTH_LAYOUT,
  757.                        TGSI_FS_DEPTH_LAYOUT_GREATER);
  758.          break;
  759.       case FRAG_DEPTH_LAYOUT_LESS:
  760.          ureg_property(ureg, TGSI_PROPERTY_FS_DEPTH_LAYOUT,
  761.                        TGSI_FS_DEPTH_LAYOUT_LESS);
  762.          break;
  763.       case FRAG_DEPTH_LAYOUT_UNCHANGED:
  764.          ureg_property(ureg, TGSI_PROPERTY_FS_DEPTH_LAYOUT,
  765.                        TGSI_FS_DEPTH_LAYOUT_UNCHANGED);
  766.          break;
  767.       default:
  768.          assert(0);
  769.       }
  770.    }
  771.  
  772.    if (stfp->glsl_to_tgsi)
  773.       st_translate_program(st->ctx,
  774.                            TGSI_PROCESSOR_FRAGMENT,
  775.                            ureg,
  776.                            stfp->glsl_to_tgsi,
  777.                            &stfp->Base.Base,
  778.                            /* inputs */
  779.                            fs_num_inputs,
  780.                            inputMapping,
  781.                            input_semantic_name,
  782.                            input_semantic_index,
  783.                            interpMode,
  784.                            interpLocation,
  785.                            /* outputs */
  786.                            fs_num_outputs,
  787.                            outputMapping,
  788.                            fs_output_semantic_name,
  789.                            fs_output_semantic_index, FALSE,
  790.                            key->clamp_color );
  791.    else
  792.       st_translate_mesa_program(st->ctx,
  793.                                 TGSI_PROCESSOR_FRAGMENT,
  794.                                 ureg,
  795.                                 &stfp->Base.Base,
  796.                                 /* inputs */
  797.                                 fs_num_inputs,
  798.                                 inputMapping,
  799.                                 input_semantic_name,
  800.                                 input_semantic_index,
  801.                                 interpMode,
  802.                                 /* outputs */
  803.                                 fs_num_outputs,
  804.                                 outputMapping,
  805.                                 fs_output_semantic_name,
  806.                                 fs_output_semantic_index, FALSE,
  807.                                 key->clamp_color);
  808.  
  809.    variant->tgsi.tokens = ureg_get_tokens( ureg, NULL );
  810.    ureg_destroy( ureg );
  811.  
  812.    if (ST_DEBUG & DEBUG_TGSI) {
  813.       tgsi_dump(variant->tgsi.tokens, 0/*TGSI_DUMP_VERBOSE*/);
  814.       debug_printf("\n");
  815.    }
  816.  
  817.    /* fill in variant */
  818.    variant->driver_shader = pipe->create_fs_state(pipe, &variant->tgsi);
  819.    variant->key = *key;
  820.  
  821.    if (deleteFP) {
  822.       /* Free the temporary program made above */
  823.       struct gl_fragment_program *fp = &stfp->Base;
  824.       _mesa_reference_fragprog(st->ctx, &fp, NULL);
  825.    }
  826.  
  827.    return variant;
  828. }
  829.  
  830.  
  831. /**
  832.  * Translate fragment program if needed.
  833.  */
  834. struct st_fp_variant *
  835. st_get_fp_variant(struct st_context *st,
  836.                   struct st_fragment_program *stfp,
  837.                   const struct st_fp_variant_key *key)
  838. {
  839.    struct st_fp_variant *fpv;
  840.  
  841.    /* Search for existing variant */
  842.    for (fpv = stfp->variants; fpv; fpv = fpv->next) {
  843.       if (memcmp(&fpv->key, key, sizeof(*key)) == 0) {
  844.          break;
  845.       }
  846.    }
  847.  
  848.    if (!fpv) {
  849.       /* create new */
  850.       fpv = st_translate_fragment_program(st, stfp, key);
  851.       if (fpv) {
  852.          /* insert into list */
  853.          fpv->next = stfp->variants;
  854.          stfp->variants = fpv;
  855.       }
  856.    }
  857.  
  858.    return fpv;
  859. }
  860.  
  861.  
  862. /**
  863.  * Translate a geometry program to create a new variant.
  864.  */
  865. static struct st_gp_variant *
  866. st_translate_geometry_program(struct st_context *st,
  867.                               struct st_geometry_program *stgp,
  868.                               const struct st_gp_variant_key *key)
  869. {
  870.    GLuint inputMapping[VARYING_SLOT_MAX];
  871.    GLuint outputMapping[VARYING_SLOT_MAX];
  872.    struct pipe_context *pipe = st->pipe;
  873.    GLuint attr;
  874.  
  875.    uint gs_num_inputs = 0;
  876.  
  877.    ubyte input_semantic_name[PIPE_MAX_SHADER_INPUTS];
  878.    ubyte input_semantic_index[PIPE_MAX_SHADER_INPUTS];
  879.  
  880.    ubyte gs_output_semantic_name[PIPE_MAX_SHADER_OUTPUTS];
  881.    ubyte gs_output_semantic_index[PIPE_MAX_SHADER_OUTPUTS];
  882.    uint gs_num_outputs = 0;
  883.  
  884.    GLint i;
  885.    struct ureg_program *ureg;
  886.    struct pipe_shader_state state = {0};
  887.    struct st_gp_variant *gpv;
  888.  
  889.    gpv = CALLOC_STRUCT(st_gp_variant);
  890.    if (!gpv)
  891.       return NULL;
  892.  
  893.    ureg = ureg_create(TGSI_PROCESSOR_GEOMETRY);
  894.    if (ureg == NULL) {
  895.       free(gpv);
  896.       return NULL;
  897.    }
  898.  
  899.    memset(inputMapping, 0, sizeof(inputMapping));
  900.    memset(outputMapping, 0, sizeof(outputMapping));
  901.  
  902.    /*
  903.     * Convert Mesa program inputs to TGSI input register semantics.
  904.     */
  905.    for (attr = 0; attr < VARYING_SLOT_MAX; attr++) {
  906.       if ((stgp->Base.Base.InputsRead & BITFIELD64_BIT(attr)) != 0) {
  907.          const GLuint slot = gs_num_inputs++;
  908.  
  909.          inputMapping[attr] = slot;
  910.  
  911.          switch (attr) {
  912.          case VARYING_SLOT_PRIMITIVE_ID:
  913.             input_semantic_name[slot] = TGSI_SEMANTIC_PRIMID;
  914.             input_semantic_index[slot] = 0;
  915.             break;
  916.          case VARYING_SLOT_POS:
  917.             input_semantic_name[slot] = TGSI_SEMANTIC_POSITION;
  918.             input_semantic_index[slot] = 0;
  919.             break;
  920.          case VARYING_SLOT_COL0:
  921.             input_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
  922.             input_semantic_index[slot] = 0;
  923.             break;
  924.          case VARYING_SLOT_COL1:
  925.             input_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
  926.             input_semantic_index[slot] = 1;
  927.             break;
  928.          case VARYING_SLOT_FOGC:
  929.             input_semantic_name[slot] = TGSI_SEMANTIC_FOG;
  930.             input_semantic_index[slot] = 0;
  931.             break;
  932.          case VARYING_SLOT_CLIP_VERTEX:
  933.             input_semantic_name[slot] = TGSI_SEMANTIC_CLIPVERTEX;
  934.             input_semantic_index[slot] = 0;
  935.             break;
  936.          case VARYING_SLOT_CLIP_DIST0:
  937.             input_semantic_name[slot] = TGSI_SEMANTIC_CLIPDIST;
  938.             input_semantic_index[slot] = 0;
  939.             break;
  940.          case VARYING_SLOT_CLIP_DIST1:
  941.             input_semantic_name[slot] = TGSI_SEMANTIC_CLIPDIST;
  942.             input_semantic_index[slot] = 1;
  943.             break;
  944.          case VARYING_SLOT_PSIZ:
  945.             input_semantic_name[slot] = TGSI_SEMANTIC_PSIZE;
  946.             input_semantic_index[slot] = 0;
  947.             break;
  948.          case VARYING_SLOT_TEX0:
  949.          case VARYING_SLOT_TEX1:
  950.          case VARYING_SLOT_TEX2:
  951.          case VARYING_SLOT_TEX3:
  952.          case VARYING_SLOT_TEX4:
  953.          case VARYING_SLOT_TEX5:
  954.          case VARYING_SLOT_TEX6:
  955.          case VARYING_SLOT_TEX7:
  956.             if (st->needs_texcoord_semantic) {
  957.                input_semantic_name[slot] = TGSI_SEMANTIC_TEXCOORD;
  958.                input_semantic_index[slot] = attr - VARYING_SLOT_TEX0;
  959.                break;
  960.             }
  961.             /* fall through */
  962.          case VARYING_SLOT_VAR0:
  963.          default:
  964.             assert(attr >= VARYING_SLOT_VAR0 && attr < VARYING_SLOT_MAX);
  965.             input_semantic_name[slot] = TGSI_SEMANTIC_GENERIC;
  966.             input_semantic_index[slot] =
  967.                st_get_generic_varying_index(st, attr);
  968.          break;
  969.          }
  970.       }
  971.    }
  972.  
  973.    /* initialize output semantics to defaults */
  974.    for (i = 0; i < PIPE_MAX_SHADER_OUTPUTS; i++) {
  975.       gs_output_semantic_name[i] = TGSI_SEMANTIC_GENERIC;
  976.       gs_output_semantic_index[i] = 0;
  977.    }
  978.  
  979.    /*
  980.     * Determine number of outputs, the (default) output register
  981.     * mapping and the semantic information for each output.
  982.     */
  983.    for (attr = 0; attr < VARYING_SLOT_MAX; attr++) {
  984.       if (stgp->Base.Base.OutputsWritten & BITFIELD64_BIT(attr)) {
  985.          GLuint slot = gs_num_outputs++;
  986.  
  987.          outputMapping[attr] = slot;
  988.  
  989.          switch (attr) {
  990.          case VARYING_SLOT_POS:
  991.             assert(slot == 0);
  992.             gs_output_semantic_name[slot] = TGSI_SEMANTIC_POSITION;
  993.             gs_output_semantic_index[slot] = 0;
  994.             break;
  995.          case VARYING_SLOT_COL0:
  996.             gs_output_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
  997.             gs_output_semantic_index[slot] = 0;
  998.             break;
  999.          case VARYING_SLOT_COL1:
  1000.             gs_output_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
  1001.             gs_output_semantic_index[slot] = 1;
  1002.             break;
  1003.          case VARYING_SLOT_BFC0:
  1004.             gs_output_semantic_name[slot] = TGSI_SEMANTIC_BCOLOR;
  1005.             gs_output_semantic_index[slot] = 0;
  1006.             break;
  1007.          case VARYING_SLOT_BFC1:
  1008.             gs_output_semantic_name[slot] = TGSI_SEMANTIC_BCOLOR;
  1009.             gs_output_semantic_index[slot] = 1;
  1010.             break;
  1011.          case VARYING_SLOT_FOGC:
  1012.             gs_output_semantic_name[slot] = TGSI_SEMANTIC_FOG;
  1013.             gs_output_semantic_index[slot] = 0;
  1014.             break;
  1015.          case VARYING_SLOT_PSIZ:
  1016.             gs_output_semantic_name[slot] = TGSI_SEMANTIC_PSIZE;
  1017.             gs_output_semantic_index[slot] = 0;
  1018.             break;
  1019.          case VARYING_SLOT_CLIP_VERTEX:
  1020.             gs_output_semantic_name[slot] = TGSI_SEMANTIC_CLIPVERTEX;
  1021.             gs_output_semantic_index[slot] = 0;
  1022.             break;
  1023.          case VARYING_SLOT_CLIP_DIST0:
  1024.             gs_output_semantic_name[slot] = TGSI_SEMANTIC_CLIPDIST;
  1025.             gs_output_semantic_index[slot] = 0;
  1026.             break;
  1027.          case VARYING_SLOT_CLIP_DIST1:
  1028.             gs_output_semantic_name[slot] = TGSI_SEMANTIC_CLIPDIST;
  1029.             gs_output_semantic_index[slot] = 1;
  1030.             break;
  1031.          case VARYING_SLOT_LAYER:
  1032.             gs_output_semantic_name[slot] = TGSI_SEMANTIC_LAYER;
  1033.             gs_output_semantic_index[slot] = 0;
  1034.             break;
  1035.          case VARYING_SLOT_PRIMITIVE_ID:
  1036.             gs_output_semantic_name[slot] = TGSI_SEMANTIC_PRIMID;
  1037.             gs_output_semantic_index[slot] = 0;
  1038.             break;
  1039.          case VARYING_SLOT_VIEWPORT:
  1040.             gs_output_semantic_name[slot] = TGSI_SEMANTIC_VIEWPORT_INDEX;
  1041.             gs_output_semantic_index[slot] = 0;
  1042.             break;
  1043.          case VARYING_SLOT_TEX0:
  1044.          case VARYING_SLOT_TEX1:
  1045.          case VARYING_SLOT_TEX2:
  1046.          case VARYING_SLOT_TEX3:
  1047.          case VARYING_SLOT_TEX4:
  1048.          case VARYING_SLOT_TEX5:
  1049.          case VARYING_SLOT_TEX6:
  1050.          case VARYING_SLOT_TEX7:
  1051.             if (st->needs_texcoord_semantic) {
  1052.                gs_output_semantic_name[slot] = TGSI_SEMANTIC_TEXCOORD;
  1053.                gs_output_semantic_index[slot] = attr - VARYING_SLOT_TEX0;
  1054.                break;
  1055.             }
  1056.             /* fall through */
  1057.          case VARYING_SLOT_VAR0:
  1058.          default:
  1059.             assert(slot < ARRAY_SIZE(gs_output_semantic_name));
  1060.             assert(attr >= VARYING_SLOT_VAR0);
  1061.             gs_output_semantic_name[slot] = TGSI_SEMANTIC_GENERIC;
  1062.             gs_output_semantic_index[slot] =
  1063.                st_get_generic_varying_index(st, attr);
  1064.          break;
  1065.          }
  1066.       }
  1067.    }
  1068.  
  1069.    ureg_property(ureg, TGSI_PROPERTY_GS_INPUT_PRIM, stgp->Base.InputType);
  1070.    ureg_property(ureg, TGSI_PROPERTY_GS_OUTPUT_PRIM, stgp->Base.OutputType);
  1071.    ureg_property(ureg, TGSI_PROPERTY_GS_MAX_OUTPUT_VERTICES,
  1072.                  stgp->Base.VerticesOut);
  1073.    ureg_property(ureg, TGSI_PROPERTY_GS_INVOCATIONS, stgp->Base.Invocations);
  1074.  
  1075.    st_translate_program(st->ctx,
  1076.                         TGSI_PROCESSOR_GEOMETRY,
  1077.                         ureg,
  1078.                         stgp->glsl_to_tgsi,
  1079.                         &stgp->Base.Base,
  1080.                         /* inputs */
  1081.                         gs_num_inputs,
  1082.                         inputMapping,
  1083.                         input_semantic_name,
  1084.                         input_semantic_index,
  1085.                         NULL,
  1086.                         NULL,
  1087.                         /* outputs */
  1088.                         gs_num_outputs,
  1089.                         outputMapping,
  1090.                         gs_output_semantic_name,
  1091.                         gs_output_semantic_index,
  1092.                         FALSE,
  1093.                         FALSE);
  1094.  
  1095.    state.tokens = ureg_get_tokens(ureg, NULL);
  1096.    ureg_destroy(ureg);
  1097.  
  1098.    st_translate_stream_output_info(stgp->glsl_to_tgsi,
  1099.                                    outputMapping,
  1100.                                    &state.stream_output);
  1101.  
  1102.    if ((ST_DEBUG & DEBUG_TGSI) && (ST_DEBUG & DEBUG_MESA)) {
  1103.       _mesa_print_program(&stgp->Base.Base);
  1104.       debug_printf("\n");
  1105.    }
  1106.  
  1107.    if (ST_DEBUG & DEBUG_TGSI) {
  1108.       tgsi_dump(state.tokens, 0);
  1109.       debug_printf("\n");
  1110.    }
  1111.  
  1112.    /* fill in new variant */
  1113.    gpv->driver_shader = pipe->create_gs_state(pipe, &state);
  1114.    gpv->key = *key;
  1115.  
  1116.    ureg_free_tokens(state.tokens);
  1117.    return gpv;
  1118. }
  1119.  
  1120.  
  1121. /**
  1122.  * Get/create geometry program variant.
  1123.  */
  1124. struct st_gp_variant *
  1125. st_get_gp_variant(struct st_context *st,
  1126.                   struct st_geometry_program *stgp,
  1127.                   const struct st_gp_variant_key *key)
  1128. {
  1129.    struct st_gp_variant *gpv;
  1130.  
  1131.    /* Search for existing variant */
  1132.    for (gpv = stgp->variants; gpv; gpv = gpv->next) {
  1133.       if (memcmp(&gpv->key, key, sizeof(*key)) == 0) {
  1134.          break;
  1135.       }
  1136.    }
  1137.  
  1138.    if (!gpv) {
  1139.       /* create new */
  1140.       gpv = st_translate_geometry_program(st, stgp, key);
  1141.       if (gpv) {
  1142.          /* insert into list */
  1143.          gpv->next = stgp->variants;
  1144.          stgp->variants = gpv;
  1145.       }
  1146.    }
  1147.  
  1148.    return gpv;
  1149. }
  1150.  
  1151.  
  1152. /**
  1153.  * Vert/Geom/Frag programs have per-context variants.  Free all the
  1154.  * variants attached to the given program which match the given context.
  1155.  */
  1156. static void
  1157. destroy_program_variants(struct st_context *st, struct gl_program *program)
  1158. {
  1159.    if (!program || program == &_mesa_DummyProgram)
  1160.       return;
  1161.  
  1162.    switch (program->Target) {
  1163.    case GL_VERTEX_PROGRAM_ARB:
  1164.       {
  1165.          struct st_vertex_program *stvp = (struct st_vertex_program *) program;
  1166.          struct st_vp_variant *vpv, **prevPtr = &stvp->variants;
  1167.  
  1168.          for (vpv = stvp->variants; vpv; ) {
  1169.             struct st_vp_variant *next = vpv->next;
  1170.             if (vpv->key.st == st) {
  1171.                /* unlink from list */
  1172.                *prevPtr = next;
  1173.                /* destroy this variant */
  1174.                delete_vp_variant(st, vpv);
  1175.             }
  1176.             else {
  1177.                prevPtr = &vpv->next;
  1178.             }
  1179.             vpv = next;
  1180.          }
  1181.       }
  1182.       break;
  1183.    case GL_FRAGMENT_PROGRAM_ARB:
  1184.       {
  1185.          struct st_fragment_program *stfp =
  1186.             (struct st_fragment_program *) program;
  1187.          struct st_fp_variant *fpv, **prevPtr = &stfp->variants;
  1188.  
  1189.          for (fpv = stfp->variants; fpv; ) {
  1190.             struct st_fp_variant *next = fpv->next;
  1191.             if (fpv->key.st == st) {
  1192.                /* unlink from list */
  1193.                *prevPtr = next;
  1194.                /* destroy this variant */
  1195.                delete_fp_variant(st, fpv);
  1196.             }
  1197.             else {
  1198.                prevPtr = &fpv->next;
  1199.             }
  1200.             fpv = next;
  1201.          }
  1202.       }
  1203.       break;
  1204.    case MESA_GEOMETRY_PROGRAM:
  1205.       {
  1206.          struct st_geometry_program *stgp =
  1207.             (struct st_geometry_program *) program;
  1208.          struct st_gp_variant *gpv, **prevPtr = &stgp->variants;
  1209.  
  1210.          for (gpv = stgp->variants; gpv; ) {
  1211.             struct st_gp_variant *next = gpv->next;
  1212.             if (gpv->key.st == st) {
  1213.                /* unlink from list */
  1214.                *prevPtr = next;
  1215.                /* destroy this variant */
  1216.                delete_gp_variant(st, gpv);
  1217.             }
  1218.             else {
  1219.                prevPtr = &gpv->next;
  1220.             }
  1221.             gpv = next;
  1222.          }
  1223.       }
  1224.       break;
  1225.    default:
  1226.       _mesa_problem(NULL, "Unexpected program target 0x%x in "
  1227.                     "destroy_program_variants_cb()", program->Target);
  1228.    }
  1229. }
  1230.  
  1231.  
  1232. /**
  1233.  * Callback for _mesa_HashWalk.  Free all the shader's program variants
  1234.  * which match the given context.
  1235.  */
  1236. static void
  1237. destroy_shader_program_variants_cb(GLuint key, void *data, void *userData)
  1238. {
  1239.    struct st_context *st = (struct st_context *) userData;
  1240.    struct gl_shader *shader = (struct gl_shader *) data;
  1241.  
  1242.    switch (shader->Type) {
  1243.    case GL_SHADER_PROGRAM_MESA:
  1244.       {
  1245.          struct gl_shader_program *shProg = (struct gl_shader_program *) data;
  1246.          GLuint i;
  1247.  
  1248.          for (i = 0; i < shProg->NumShaders; i++) {
  1249.             destroy_program_variants(st, shProg->Shaders[i]->Program);
  1250.          }
  1251.  
  1252.          for (i = 0; i < ARRAY_SIZE(shProg->_LinkedShaders); i++) {
  1253.             if (shProg->_LinkedShaders[i])
  1254.                destroy_program_variants(st, shProg->_LinkedShaders[i]->Program);
  1255.          }
  1256.       }
  1257.       break;
  1258.    case GL_VERTEX_SHADER:
  1259.    case GL_FRAGMENT_SHADER:
  1260.    case GL_GEOMETRY_SHADER:
  1261.       {
  1262.          destroy_program_variants(st, shader->Program);
  1263.       }
  1264.       break;
  1265.    default:
  1266.       assert(0);
  1267.    }
  1268. }
  1269.  
  1270.  
  1271. /**
  1272.  * Callback for _mesa_HashWalk.  Free all the program variants which match
  1273.  * the given context.
  1274.  */
  1275. static void
  1276. destroy_program_variants_cb(GLuint key, void *data, void *userData)
  1277. {
  1278.    struct st_context *st = (struct st_context *) userData;
  1279.    struct gl_program *program = (struct gl_program *) data;
  1280.    destroy_program_variants(st, program);
  1281. }
  1282.  
  1283.  
  1284. /**
  1285.  * Walk over all shaders and programs to delete any variants which
  1286.  * belong to the given context.
  1287.  * This is called during context tear-down.
  1288.  */
  1289. void
  1290. st_destroy_program_variants(struct st_context *st)
  1291. {
  1292.    /* ARB vert/frag program */
  1293.    _mesa_HashWalk(st->ctx->Shared->Programs,
  1294.                   destroy_program_variants_cb, st);
  1295.  
  1296.    /* GLSL vert/frag/geom shaders */
  1297.    _mesa_HashWalk(st->ctx->Shared->ShaderObjects,
  1298.                   destroy_shader_program_variants_cb, st);
  1299. }
  1300.  
  1301.  
  1302. /**
  1303.  * For debugging, print/dump the current vertex program.
  1304.  */
  1305. void
  1306. st_print_current_vertex_program(void)
  1307. {
  1308.    GET_CURRENT_CONTEXT(ctx);
  1309.  
  1310.    if (ctx->VertexProgram._Current) {
  1311.       struct st_vertex_program *stvp =
  1312.          (struct st_vertex_program *) ctx->VertexProgram._Current;
  1313.       struct st_vp_variant *stv;
  1314.  
  1315.       debug_printf("Vertex program %u\n", stvp->Base.Base.Id);
  1316.  
  1317.       for (stv = stvp->variants; stv; stv = stv->next) {
  1318.          debug_printf("variant %p\n", stv);
  1319.          tgsi_dump(stv->tgsi.tokens, 0);
  1320.       }
  1321.    }
  1322. }
  1323.  
  1324.  
  1325. /**
  1326.  * Compile one shader variant.
  1327.  */
  1328. void
  1329. st_precompile_shader_variant(struct st_context *st,
  1330.                              struct gl_program *prog)
  1331. {
  1332.    switch (prog->Target) {
  1333.    case GL_VERTEX_PROGRAM_ARB: {
  1334.       struct st_vertex_program *p = (struct st_vertex_program *)prog;
  1335.       struct st_vp_variant_key key;
  1336.  
  1337.       memset(&key, 0, sizeof(key));
  1338.       key.st = st;
  1339.       st_get_vp_variant(st, p, &key);
  1340.       break;
  1341.    }
  1342.  
  1343.    case GL_GEOMETRY_PROGRAM_NV: {
  1344.       struct st_geometry_program *p = (struct st_geometry_program *)prog;
  1345.       struct st_gp_variant_key key;
  1346.  
  1347.       memset(&key, 0, sizeof(key));
  1348.       key.st = st;
  1349.       st_get_gp_variant(st, p, &key);
  1350.       break;
  1351.    }
  1352.  
  1353.    case GL_FRAGMENT_PROGRAM_ARB: {
  1354.       struct st_fragment_program *p = (struct st_fragment_program *)prog;
  1355.       struct st_fp_variant_key key;
  1356.  
  1357.       memset(&key, 0, sizeof(key));
  1358.       key.st = st;
  1359.       st_get_fp_variant(st, p, &key);
  1360.       break;
  1361.    }
  1362.  
  1363.    default:
  1364.       assert(0);
  1365.    }
  1366. }
  1367.