Home | History | Annotate | Download | only in draw
      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 /**
     29  * AA line stage:  AA lines are converted to texture mapped triangles.
     30  *
     31  * Authors:  Brian Paul
     32  */
     33 
     34 
     35 #include "pipe/p_context.h"
     36 #include "pipe/p_defines.h"
     37 #include "pipe/p_shader_tokens.h"
     38 #include "util/u_inlines.h"
     39 
     40 #include "util/u_format.h"
     41 #include "util/u_math.h"
     42 #include "util/u_memory.h"
     43 #include "util/u_sampler.h"
     44 
     45 #include "tgsi/tgsi_transform.h"
     46 #include "tgsi/tgsi_dump.h"
     47 
     48 #include "draw_context.h"
     49 #include "draw_private.h"
     50 #include "draw_pipe.h"
     51 
     52 
     53 /** Approx number of new tokens for instructions in aa_transform_inst() */
     54 #define NUM_NEW_TOKENS 53
     55 
     56 
     57 /**
     58  * Size for the alpha texture used for antialiasing
     59  */
     60 #define TEXTURE_SIZE_LOG2  5   /* 32 x 32 */
     61 
     62 /**
     63  * Max texture level for the alpha texture used for antialiasing
     64  *
     65  * Don't use the 1x1 and 2x2 mipmap levels.
     66  */
     67 #define MAX_TEXTURE_LEVEL  (TEXTURE_SIZE_LOG2 - 2)
     68 
     69 
     70 /**
     71  * Subclass of pipe_shader_state to carry extra fragment shader info.
     72  */
     73 struct aaline_fragment_shader
     74 {
     75    struct pipe_shader_state state;
     76    void *driver_fs;
     77    void *aaline_fs;
     78    uint sampler_unit;
     79    int generic_attrib;  /**< texcoord/generic used for texture */
     80 };
     81 
     82 
     83 /**
     84  * Subclass of draw_stage
     85  */
     86 struct aaline_stage
     87 {
     88    struct draw_stage stage;
     89 
     90    float half_line_width;
     91 
     92    /** For AA lines, this is the vertex attrib slot for the new texcoords */
     93    uint tex_slot;
     94    /** position, not necessarily output zero */
     95    uint pos_slot;
     96 
     97    void *sampler_cso;
     98    struct pipe_resource *texture;
     99    struct pipe_sampler_view *sampler_view;
    100    uint num_samplers;
    101    uint num_sampler_views;
    102 
    103 
    104    /*
    105     * Currently bound state
    106     */
    107    struct aaline_fragment_shader *fs;
    108    struct {
    109       void *sampler[PIPE_MAX_SAMPLERS];
    110       struct pipe_sampler_view *sampler_views[PIPE_MAX_SHADER_SAMPLER_VIEWS];
    111    } state;
    112 
    113    /*
    114     * Driver interface/override functions
    115     */
    116    void * (*driver_create_fs_state)(struct pipe_context *,
    117                                     const struct pipe_shader_state *);
    118    void (*driver_bind_fs_state)(struct pipe_context *, void *);
    119    void (*driver_delete_fs_state)(struct pipe_context *, void *);
    120 
    121    void (*driver_bind_sampler_states)(struct pipe_context *,
    122                                       enum pipe_shader_type, unsigned,
    123                                       unsigned, void **);
    124 
    125    void (*driver_set_sampler_views)(struct pipe_context *,
    126                                     enum pipe_shader_type shader,
    127                                     unsigned start, unsigned count,
    128                                     struct pipe_sampler_view **);
    129 };
    130 
    131 
    132 
    133 /**
    134  * Subclass of tgsi_transform_context, used for transforming the
    135  * user's fragment shader to add the special AA instructions.
    136  */
    137 struct aa_transform_context {
    138    struct tgsi_transform_context base;
    139    uint tempsUsed;  /**< bitmask */
    140    int colorOutput; /**< which output is the primary color */
    141    uint samplersUsed;  /**< bitfield of samplers used */
    142    bool hasSview;
    143    int freeSampler;  /** an available sampler for the pstipple */
    144    int maxInput, maxGeneric;  /**< max input index found */
    145    int colorTemp, texTemp;  /**< temp registers */
    146 };
    147 
    148 
    149 /**
    150  * TGSI declaration transform callback.
    151  * Look for a free sampler, a free input attrib, and two free temp regs.
    152  */
    153 static void
    154 aa_transform_decl(struct tgsi_transform_context *ctx,
    155                   struct tgsi_full_declaration *decl)
    156 {
    157    struct aa_transform_context *aactx = (struct aa_transform_context *) ctx;
    158 
    159    if (decl->Declaration.File == TGSI_FILE_OUTPUT &&
    160        decl->Semantic.Name == TGSI_SEMANTIC_COLOR &&
    161        decl->Semantic.Index == 0) {
    162       aactx->colorOutput = decl->Range.First;
    163    }
    164    else if (decl->Declaration.File == TGSI_FILE_SAMPLER) {
    165       uint i;
    166       for (i = decl->Range.First;
    167            i <= decl->Range.Last; i++) {
    168          aactx->samplersUsed |= 1u << i;
    169       }
    170    }
    171    else if (decl->Declaration.File == TGSI_FILE_SAMPLER_VIEW) {
    172       aactx->hasSview = true;
    173    }
    174    else if (decl->Declaration.File == TGSI_FILE_INPUT) {
    175       if ((int) decl->Range.Last > aactx->maxInput)
    176          aactx->maxInput = decl->Range.Last;
    177       if (decl->Semantic.Name == TGSI_SEMANTIC_GENERIC &&
    178            (int) decl->Semantic.Index > aactx->maxGeneric) {
    179          aactx->maxGeneric = decl->Semantic.Index;
    180       }
    181    }
    182    else if (decl->Declaration.File == TGSI_FILE_TEMPORARY) {
    183       uint i;
    184       for (i = decl->Range.First;
    185            i <= decl->Range.Last; i++) {
    186          aactx->tempsUsed |= (1 << i);
    187       }
    188    }
    189 
    190    ctx->emit_declaration(ctx, decl);
    191 }
    192 
    193 
    194 /**
    195  * Find the lowest zero bit in the given word, or -1 if bitfield is all ones.
    196  */
    197 static int
    198 free_bit(uint bitfield)
    199 {
    200    return ffs(~bitfield) - 1;
    201 }
    202 
    203 
    204 /**
    205  * TGSI transform prolog callback.
    206  */
    207 static void
    208 aa_transform_prolog(struct tgsi_transform_context *ctx)
    209 {
    210    struct aa_transform_context *aactx = (struct aa_transform_context *) ctx;
    211    uint i;
    212 
    213    STATIC_ASSERT(sizeof(aactx->samplersUsed) * 8 >= PIPE_MAX_SAMPLERS);
    214 
    215    /* find free sampler */
    216    aactx->freeSampler = free_bit(aactx->samplersUsed);
    217    if (aactx->freeSampler < 0 || aactx->freeSampler >= PIPE_MAX_SAMPLERS)
    218       aactx->freeSampler = PIPE_MAX_SAMPLERS - 1;
    219 
    220    /* find two free temp regs */
    221    for (i = 0; i < 32; i++) {
    222       if ((aactx->tempsUsed & (1 << i)) == 0) {
    223       /* found a free temp */
    224       if (aactx->colorTemp < 0)
    225          aactx->colorTemp  = i;
    226       else if (aactx->texTemp < 0)
    227          aactx->texTemp  = i;
    228       else
    229          break;
    230       }
    231    }
    232    assert(aactx->colorTemp >= 0);
    233    assert(aactx->texTemp >= 0);
    234 
    235    /* declare new generic input/texcoord */
    236    tgsi_transform_input_decl(ctx, aactx->maxInput + 1,
    237                              TGSI_SEMANTIC_GENERIC, aactx->maxGeneric + 1,
    238                              TGSI_INTERPOLATE_LINEAR);
    239 
    240    /* declare new sampler */
    241    tgsi_transform_sampler_decl(ctx, aactx->freeSampler);
    242 
    243    /* if the src shader has SVIEW decl's for each SAMP decl, we
    244     * need to continue the trend and ensure there is a matching
    245     * SVIEW for the new SAMP we just created
    246     */
    247    if (aactx->hasSview) {
    248       tgsi_transform_sampler_view_decl(ctx,
    249                                        aactx->freeSampler,
    250                                        TGSI_TEXTURE_2D,
    251                                        TGSI_RETURN_TYPE_FLOAT);
    252    }
    253 
    254    /* declare new temp regs */
    255    tgsi_transform_temp_decl(ctx, aactx->texTemp);
    256    tgsi_transform_temp_decl(ctx, aactx->colorTemp);
    257 }
    258 
    259 
    260 /**
    261  * TGSI transform epilog callback.
    262  */
    263 static void
    264 aa_transform_epilog(struct tgsi_transform_context *ctx)
    265 {
    266    struct aa_transform_context *aactx = (struct aa_transform_context *) ctx;
    267 
    268    if (aactx->colorOutput != -1) {
    269       /* insert texture sampling code for antialiasing. */
    270 
    271       /* TEX texTemp, input_coord, sampler, 2D */
    272       tgsi_transform_tex_inst(ctx,
    273                               TGSI_FILE_TEMPORARY, aactx->texTemp,
    274                               TGSI_FILE_INPUT, aactx->maxInput + 1,
    275                               TGSI_TEXTURE_2D, aactx->freeSampler);
    276 
    277       /* MOV rgb */
    278       tgsi_transform_op1_inst(ctx, TGSI_OPCODE_MOV,
    279                               TGSI_FILE_OUTPUT, aactx->colorOutput,
    280                               TGSI_WRITEMASK_XYZ,
    281                               TGSI_FILE_TEMPORARY, aactx->colorTemp);
    282 
    283       /* MUL alpha */
    284       tgsi_transform_op2_inst(ctx, TGSI_OPCODE_MUL,
    285                               TGSI_FILE_OUTPUT, aactx->colorOutput,
    286                               TGSI_WRITEMASK_W,
    287                               TGSI_FILE_TEMPORARY, aactx->colorTemp,
    288                               TGSI_FILE_TEMPORARY, aactx->texTemp, false);
    289    }
    290 }
    291 
    292 
    293 /**
    294  * TGSI instruction transform callback.
    295  * Replace writes to result.color w/ a temp reg.
    296  */
    297 static void
    298 aa_transform_inst(struct tgsi_transform_context *ctx,
    299                   struct tgsi_full_instruction *inst)
    300 {
    301    struct aa_transform_context *aactx = (struct aa_transform_context *) ctx;
    302    uint i;
    303 
    304    /*
    305     * Look for writes to result.color and replace with colorTemp reg.
    306     */
    307    for (i = 0; i < inst->Instruction.NumDstRegs; i++) {
    308       struct tgsi_full_dst_register *dst = &inst->Dst[i];
    309       if (dst->Register.File == TGSI_FILE_OUTPUT &&
    310           dst->Register.Index == aactx->colorOutput) {
    311          dst->Register.File = TGSI_FILE_TEMPORARY;
    312          dst->Register.Index = aactx->colorTemp;
    313       }
    314    }
    315 
    316    ctx->emit_instruction(ctx, inst);
    317 }
    318 
    319 
    320 /**
    321  * Generate the frag shader we'll use for drawing AA lines.
    322  * This will be the user's shader plus some texture/modulate instructions.
    323  */
    324 static boolean
    325 generate_aaline_fs(struct aaline_stage *aaline)
    326 {
    327    struct pipe_context *pipe = aaline->stage.draw->pipe;
    328    const struct pipe_shader_state *orig_fs = &aaline->fs->state;
    329    struct pipe_shader_state aaline_fs;
    330    struct aa_transform_context transform;
    331    const uint newLen = tgsi_num_tokens(orig_fs->tokens) + NUM_NEW_TOKENS;
    332 
    333    aaline_fs = *orig_fs; /* copy to init */
    334    aaline_fs.tokens = tgsi_alloc_tokens(newLen);
    335    if (aaline_fs.tokens == NULL)
    336       return FALSE;
    337 
    338    memset(&transform, 0, sizeof(transform));
    339    transform.colorOutput = -1;
    340    transform.maxInput = -1;
    341    transform.maxGeneric = -1;
    342    transform.colorTemp = -1;
    343    transform.texTemp = -1;
    344    transform.base.prolog = aa_transform_prolog;
    345    transform.base.epilog = aa_transform_epilog;
    346    transform.base.transform_instruction = aa_transform_inst;
    347    transform.base.transform_declaration = aa_transform_decl;
    348 
    349    tgsi_transform_shader(orig_fs->tokens,
    350                          (struct tgsi_token *) aaline_fs.tokens,
    351                          newLen, &transform.base);
    352 
    353 #if 0 /* DEBUG */
    354    debug_printf("draw_aaline, orig shader:\n");
    355    tgsi_dump(orig_fs->tokens, 0);
    356    debug_printf("draw_aaline, new shader:\n");
    357    tgsi_dump(aaline_fs.tokens, 0);
    358 #endif
    359 
    360    aaline->fs->sampler_unit = transform.freeSampler;
    361 
    362    aaline->fs->aaline_fs = aaline->driver_create_fs_state(pipe, &aaline_fs);
    363    if (aaline->fs->aaline_fs == NULL)
    364       goto fail;
    365 
    366    aaline->fs->generic_attrib = transform.maxGeneric + 1;
    367    FREE((void *)aaline_fs.tokens);
    368    return TRUE;
    369 
    370 fail:
    371    FREE((void *)aaline_fs.tokens);
    372    return FALSE;
    373 }
    374 
    375 
    376 /**
    377  * Create the texture map we'll use for antialiasing the lines.
    378  */
    379 static boolean
    380 aaline_create_texture(struct aaline_stage *aaline)
    381 {
    382    struct pipe_context *pipe = aaline->stage.draw->pipe;
    383    struct pipe_screen *screen = pipe->screen;
    384    struct pipe_resource texTemp;
    385    struct pipe_sampler_view viewTempl;
    386    uint level;
    387 
    388    memset(&texTemp, 0, sizeof(texTemp));
    389    texTemp.target = PIPE_TEXTURE_2D;
    390    texTemp.format = PIPE_FORMAT_A8_UNORM; /* XXX verify supported by driver! */
    391    texTemp.last_level = MAX_TEXTURE_LEVEL;
    392    texTemp.width0 = 1 << TEXTURE_SIZE_LOG2;
    393    texTemp.height0 = 1 << TEXTURE_SIZE_LOG2;
    394    texTemp.depth0 = 1;
    395    texTemp.array_size = 1;
    396    texTemp.bind = PIPE_BIND_SAMPLER_VIEW;
    397 
    398    aaline->texture = screen->resource_create(screen, &texTemp);
    399    if (!aaline->texture)
    400       return FALSE;
    401 
    402    u_sampler_view_default_template(&viewTempl,
    403                                    aaline->texture,
    404                                    aaline->texture->format);
    405    aaline->sampler_view = pipe->create_sampler_view(pipe,
    406                                                     aaline->texture,
    407                                                     &viewTempl);
    408    if (!aaline->sampler_view) {
    409       return FALSE;
    410    }
    411 
    412    /* Fill in mipmap images.
    413     * Basically each level is solid opaque, except for the outermost
    414     * texels which are zero.  Special case the 1x1 and 2x2 levels
    415     * (though, those levels shouldn't be used - see the max_lod setting).
    416     */
    417    for (level = 0; level <= MAX_TEXTURE_LEVEL; level++) {
    418       struct pipe_transfer *transfer;
    419       struct pipe_box box;
    420       const uint size = u_minify(aaline->texture->width0, level);
    421       ubyte *data;
    422       uint i, j;
    423 
    424       assert(aaline->texture->width0 == aaline->texture->height0);
    425 
    426       u_box_origin_2d( size, size, &box );
    427 
    428       /* This texture is new, no need to flush.
    429        */
    430       data = pipe->transfer_map(pipe,
    431                                 aaline->texture,
    432                                 level,
    433                                 PIPE_TRANSFER_WRITE,
    434                                 &box, &transfer);
    435 
    436       if (!data)
    437          return FALSE;
    438 
    439       for (i = 0; i < size; i++) {
    440          for (j = 0; j < size; j++) {
    441             ubyte d;
    442             if (size == 1) {
    443                d = 255;
    444             }
    445             else if (size == 2) {
    446                d = 200; /* tuneable */
    447             }
    448             else if (i == 0 || j == 0 || i == size - 1 || j == size - 1) {
    449                d = 35;  /* edge texel */
    450             }
    451             else {
    452                d = 255;
    453             }
    454             data[i * transfer->stride + j] = d;
    455          }
    456       }
    457 
    458       /* unmap */
    459       pipe->transfer_unmap(pipe, transfer);
    460    }
    461    return TRUE;
    462 }
    463 
    464 
    465 /**
    466  * Create the sampler CSO that'll be used for antialiasing.
    467  * By using a mipmapped texture, we don't have to generate a different
    468  * texture image for each line size.
    469  */
    470 static boolean
    471 aaline_create_sampler(struct aaline_stage *aaline)
    472 {
    473    struct pipe_sampler_state sampler;
    474    struct pipe_context *pipe = aaline->stage.draw->pipe;
    475 
    476    memset(&sampler, 0, sizeof(sampler));
    477    sampler.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
    478    sampler.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
    479    sampler.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
    480    sampler.min_mip_filter = PIPE_TEX_MIPFILTER_LINEAR;
    481    sampler.min_img_filter = PIPE_TEX_FILTER_LINEAR;
    482    sampler.mag_img_filter = PIPE_TEX_FILTER_LINEAR;
    483    sampler.normalized_coords = 1;
    484    sampler.min_lod = 0.0f;
    485    sampler.max_lod = MAX_TEXTURE_LEVEL;
    486 
    487    aaline->sampler_cso = pipe->create_sampler_state(pipe, &sampler);
    488    if (aaline->sampler_cso == NULL)
    489       return FALSE;
    490 
    491    return TRUE;
    492 }
    493 
    494 
    495 /**
    496  * When we're about to draw our first AA line in a batch, this function is
    497  * called to tell the driver to bind our modified fragment shader.
    498  */
    499 static boolean
    500 bind_aaline_fragment_shader(struct aaline_stage *aaline)
    501 {
    502    struct draw_context *draw = aaline->stage.draw;
    503    struct pipe_context *pipe = draw->pipe;
    504 
    505    if (!aaline->fs->aaline_fs &&
    506        !generate_aaline_fs(aaline))
    507       return FALSE;
    508 
    509    draw->suspend_flushing = TRUE;
    510    aaline->driver_bind_fs_state(pipe, aaline->fs->aaline_fs);
    511    draw->suspend_flushing = FALSE;
    512 
    513    return TRUE;
    514 }
    515 
    516 
    517 
    518 static inline struct aaline_stage *
    519 aaline_stage( struct draw_stage *stage )
    520 {
    521    return (struct aaline_stage *) stage;
    522 }
    523 
    524 
    525 /**
    526  * Draw a wide line by drawing a quad, using geometry which will
    527  * fullfill GL's antialiased line requirements.
    528  */
    529 static void
    530 aaline_line(struct draw_stage *stage, struct prim_header *header)
    531 {
    532    const struct aaline_stage *aaline = aaline_stage(stage);
    533    const float half_width = aaline->half_line_width;
    534    struct prim_header tri;
    535    struct vertex_header *v[8];
    536    uint texPos = aaline->tex_slot;
    537    uint posPos = aaline->pos_slot;
    538    float *pos, *tex;
    539    float dx = header->v[1]->data[posPos][0] - header->v[0]->data[posPos][0];
    540    float dy = header->v[1]->data[posPos][1] - header->v[0]->data[posPos][1];
    541    double a = atan2(dy, dx);
    542    float c_a = (float) cos(a), s_a = (float) sin(a);
    543    uint i;
    544 
    545    /* XXX the ends of lines aren't quite perfect yet, but probably passable */
    546    dx = 0.5F * half_width;
    547    dy = half_width;
    548 
    549    /* allocate/dup new verts */
    550    for (i = 0; i < 8; i++) {
    551       v[i] = dup_vert(stage, header->v[i/4], i);
    552    }
    553 
    554    /*
    555     * Quad strip for line from v0 to v1 (*=endpoints):
    556     *
    557     *  1   3                     5   7
    558     *  +---+---------------------+---+
    559     *  |                             |
    560     *  | *v0                     v1* |
    561     *  |                             |
    562     *  +---+---------------------+---+
    563     *  0   2                     4   6
    564     */
    565 
    566    /* new verts */
    567    pos = v[0]->data[posPos];
    568    pos[0] += (-dx * c_a -  dy * s_a);
    569    pos[1] += (-dx * s_a +  dy * c_a);
    570 
    571    pos = v[1]->data[posPos];
    572    pos[0] += (-dx * c_a - -dy * s_a);
    573    pos[1] += (-dx * s_a + -dy * c_a);
    574 
    575    pos = v[2]->data[posPos];
    576    pos[0] += ( dx * c_a -  dy * s_a);
    577    pos[1] += ( dx * s_a +  dy * c_a);
    578 
    579    pos = v[3]->data[posPos];
    580    pos[0] += ( dx * c_a - -dy * s_a);
    581    pos[1] += ( dx * s_a + -dy * c_a);
    582 
    583    pos = v[4]->data[posPos];
    584    pos[0] += (-dx * c_a -  dy * s_a);
    585    pos[1] += (-dx * s_a +  dy * c_a);
    586 
    587    pos = v[5]->data[posPos];
    588    pos[0] += (-dx * c_a - -dy * s_a);
    589    pos[1] += (-dx * s_a + -dy * c_a);
    590 
    591    pos = v[6]->data[posPos];
    592    pos[0] += ( dx * c_a -  dy * s_a);
    593    pos[1] += ( dx * s_a +  dy * c_a);
    594 
    595    pos = v[7]->data[posPos];
    596    pos[0] += ( dx * c_a - -dy * s_a);
    597    pos[1] += ( dx * s_a + -dy * c_a);
    598 
    599    /* new texcoords */
    600    tex = v[0]->data[texPos];
    601    ASSIGN_4V(tex, 0, 0, 0, 1);
    602 
    603    tex = v[1]->data[texPos];
    604    ASSIGN_4V(tex, 0, 1, 0, 1);
    605 
    606    tex = v[2]->data[texPos];
    607    ASSIGN_4V(tex, .5, 0, 0, 1);
    608 
    609    tex = v[3]->data[texPos];
    610    ASSIGN_4V(tex, .5, 1, 0, 1);
    611 
    612    tex = v[4]->data[texPos];
    613    ASSIGN_4V(tex, .5, 0, 0, 1);
    614 
    615    tex = v[5]->data[texPos];
    616    ASSIGN_4V(tex, .5, 1, 0, 1);
    617 
    618    tex = v[6]->data[texPos];
    619    ASSIGN_4V(tex, 1, 0, 0, 1);
    620 
    621    tex = v[7]->data[texPos];
    622    ASSIGN_4V(tex, 1, 1, 0, 1);
    623 
    624    /* emit 6 tris for the quad strip */
    625    tri.v[0] = v[2];  tri.v[1] = v[1];  tri.v[2] = v[0];
    626    stage->next->tri( stage->next, &tri );
    627 
    628    tri.v[0] = v[3];  tri.v[1] = v[1];  tri.v[2] = v[2];
    629    stage->next->tri( stage->next, &tri );
    630 
    631    tri.v[0] = v[4];  tri.v[1] = v[3];  tri.v[2] = v[2];
    632    stage->next->tri( stage->next, &tri );
    633 
    634    tri.v[0] = v[5];  tri.v[1] = v[3];  tri.v[2] = v[4];
    635    stage->next->tri( stage->next, &tri );
    636 
    637    tri.v[0] = v[6];  tri.v[1] = v[5];  tri.v[2] = v[4];
    638    stage->next->tri( stage->next, &tri );
    639 
    640    tri.v[0] = v[7];  tri.v[1] = v[5];  tri.v[2] = v[6];
    641    stage->next->tri( stage->next, &tri );
    642 }
    643 
    644 
    645 static void
    646 aaline_first_line(struct draw_stage *stage, struct prim_header *header)
    647 {
    648    auto struct aaline_stage *aaline = aaline_stage(stage);
    649    struct draw_context *draw = stage->draw;
    650    struct pipe_context *pipe = draw->pipe;
    651    const struct pipe_rasterizer_state *rast = draw->rasterizer;
    652    uint num_samplers;
    653    uint num_sampler_views;
    654    void *r;
    655 
    656    assert(draw->rasterizer->line_smooth);
    657 
    658    if (draw->rasterizer->line_width <= 2.2)
    659       aaline->half_line_width = 1.1f;
    660    else
    661       aaline->half_line_width = 0.5f * draw->rasterizer->line_width;
    662 
    663    /*
    664     * Bind (generate) our fragprog, sampler and texture
    665     */
    666    if (!bind_aaline_fragment_shader(aaline)) {
    667       stage->line = draw_pipe_passthrough_line;
    668       stage->line(stage, header);
    669       return;
    670    }
    671 
    672    draw_aaline_prepare_outputs(draw, draw->pipeline.aaline);
    673 
    674    /* how many samplers? */
    675    /* we'll use sampler/texture[aaline->sampler_unit] for the alpha texture */
    676    num_samplers = MAX2(aaline->num_samplers, aaline->fs->sampler_unit + 1);
    677    num_sampler_views = MAX2(num_samplers, aaline->num_sampler_views);
    678 
    679    aaline->state.sampler[aaline->fs->sampler_unit] = aaline->sampler_cso;
    680    pipe_sampler_view_reference(&aaline->state.sampler_views[aaline->fs->sampler_unit],
    681                                aaline->sampler_view);
    682 
    683    draw->suspend_flushing = TRUE;
    684 
    685    aaline->driver_bind_sampler_states(pipe, PIPE_SHADER_FRAGMENT, 0,
    686                                       num_samplers, aaline->state.sampler);
    687 
    688    aaline->driver_set_sampler_views(pipe, PIPE_SHADER_FRAGMENT, 0,
    689                                     num_sampler_views, aaline->state.sampler_views);
    690 
    691    /* Disable triangle culling, stippling, unfilled mode etc. */
    692    r = draw_get_rasterizer_no_cull(draw, rast->scissor, rast->flatshade);
    693    pipe->bind_rasterizer_state(pipe, r);
    694 
    695    draw->suspend_flushing = FALSE;
    696 
    697    /* now really draw first line */
    698    stage->line = aaline_line;
    699    stage->line(stage, header);
    700 }
    701 
    702 
    703 static void
    704 aaline_flush(struct draw_stage *stage, unsigned flags)
    705 {
    706    struct draw_context *draw = stage->draw;
    707    struct aaline_stage *aaline = aaline_stage(stage);
    708    struct pipe_context *pipe = draw->pipe;
    709 
    710    stage->line = aaline_first_line;
    711    stage->next->flush( stage->next, flags );
    712 
    713    /* restore original frag shader, texture, sampler state */
    714    draw->suspend_flushing = TRUE;
    715    aaline->driver_bind_fs_state(pipe, aaline->fs ? aaline->fs->driver_fs : NULL);
    716 
    717    aaline->driver_bind_sampler_states(pipe, PIPE_SHADER_FRAGMENT, 0,
    718                                       aaline->num_samplers,
    719                                       aaline->state.sampler);
    720 
    721    aaline->driver_set_sampler_views(pipe, PIPE_SHADER_FRAGMENT, 0,
    722                                     aaline->num_samplers,
    723                                     aaline->state.sampler_views);
    724 
    725    /* restore original rasterizer state */
    726    if (draw->rast_handle) {
    727       pipe->bind_rasterizer_state(pipe, draw->rast_handle);
    728    }
    729 
    730    draw->suspend_flushing = FALSE;
    731 
    732    draw_remove_extra_vertex_attribs(draw);
    733 }
    734 
    735 
    736 static void
    737 aaline_reset_stipple_counter(struct draw_stage *stage)
    738 {
    739    stage->next->reset_stipple_counter( stage->next );
    740 }
    741 
    742 
    743 static void
    744 aaline_destroy(struct draw_stage *stage)
    745 {
    746    struct aaline_stage *aaline = aaline_stage(stage);
    747    struct pipe_context *pipe = stage->draw->pipe;
    748    uint i;
    749 
    750    for (i = 0; i < PIPE_MAX_SHADER_SAMPLER_VIEWS; i++) {
    751       pipe_sampler_view_reference(&aaline->state.sampler_views[i], NULL);
    752    }
    753 
    754    if (aaline->sampler_cso)
    755       pipe->delete_sampler_state(pipe, aaline->sampler_cso);
    756 
    757    if (aaline->texture)
    758       pipe_resource_reference(&aaline->texture, NULL);
    759 
    760    if (aaline->sampler_view) {
    761       pipe_sampler_view_reference(&aaline->sampler_view, NULL);
    762    }
    763 
    764    draw_free_temp_verts( stage );
    765 
    766    /* restore the old entry points */
    767    pipe->create_fs_state = aaline->driver_create_fs_state;
    768    pipe->bind_fs_state = aaline->driver_bind_fs_state;
    769    pipe->delete_fs_state = aaline->driver_delete_fs_state;
    770 
    771    pipe->bind_sampler_states = aaline->driver_bind_sampler_states;
    772    pipe->set_sampler_views = aaline->driver_set_sampler_views;
    773 
    774    FREE( stage );
    775 }
    776 
    777 
    778 static struct aaline_stage *
    779 draw_aaline_stage(struct draw_context *draw)
    780 {
    781    struct aaline_stage *aaline = CALLOC_STRUCT(aaline_stage);
    782    if (!aaline)
    783       return NULL;
    784 
    785    aaline->stage.draw = draw;
    786    aaline->stage.name = "aaline";
    787    aaline->stage.next = NULL;
    788    aaline->stage.point = draw_pipe_passthrough_point;
    789    aaline->stage.line = aaline_first_line;
    790    aaline->stage.tri = draw_pipe_passthrough_tri;
    791    aaline->stage.flush = aaline_flush;
    792    aaline->stage.reset_stipple_counter = aaline_reset_stipple_counter;
    793    aaline->stage.destroy = aaline_destroy;
    794 
    795    if (!draw_alloc_temp_verts( &aaline->stage, 8 ))
    796       goto fail;
    797 
    798    return aaline;
    799 
    800  fail:
    801    aaline->stage.destroy(&aaline->stage);
    802 
    803    return NULL;
    804 }
    805 
    806 
    807 static struct aaline_stage *
    808 aaline_stage_from_pipe(struct pipe_context *pipe)
    809 {
    810    struct draw_context *draw = (struct draw_context *) pipe->draw;
    811 
    812    if (draw) {
    813       return aaline_stage(draw->pipeline.aaline);
    814    } else {
    815       return NULL;
    816    }
    817 }
    818 
    819 
    820 /**
    821  * This function overrides the driver's create_fs_state() function and
    822  * will typically be called by the state tracker.
    823  */
    824 static void *
    825 aaline_create_fs_state(struct pipe_context *pipe,
    826                        const struct pipe_shader_state *fs)
    827 {
    828    struct aaline_stage *aaline = aaline_stage_from_pipe(pipe);
    829    struct aaline_fragment_shader *aafs = NULL;
    830 
    831    if (!aaline)
    832       return NULL;
    833 
    834    aafs = CALLOC_STRUCT(aaline_fragment_shader);
    835 
    836    if (!aafs)
    837       return NULL;
    838 
    839    aafs->state.tokens = tgsi_dup_tokens(fs->tokens);
    840 
    841    /* pass-through */
    842    aafs->driver_fs = aaline->driver_create_fs_state(pipe, fs);
    843 
    844    return aafs;
    845 }
    846 
    847 
    848 static void
    849 aaline_bind_fs_state(struct pipe_context *pipe, void *fs)
    850 {
    851    struct aaline_stage *aaline = aaline_stage_from_pipe(pipe);
    852    struct aaline_fragment_shader *aafs = (struct aaline_fragment_shader *) fs;
    853 
    854    if (!aaline) {
    855       return;
    856    }
    857 
    858    /* save current */
    859    aaline->fs = aafs;
    860    /* pass-through */
    861    aaline->driver_bind_fs_state(pipe, (aafs ? aafs->driver_fs : NULL));
    862 }
    863 
    864 
    865 static void
    866 aaline_delete_fs_state(struct pipe_context *pipe, void *fs)
    867 {
    868    struct aaline_stage *aaline = aaline_stage_from_pipe(pipe);
    869    struct aaline_fragment_shader *aafs = (struct aaline_fragment_shader *) fs;
    870 
    871    if (!aafs) {
    872       return;
    873    }
    874 
    875    if (aaline) {
    876       /* pass-through */
    877       aaline->driver_delete_fs_state(pipe, aafs->driver_fs);
    878 
    879       if (aafs->aaline_fs)
    880          aaline->driver_delete_fs_state(pipe, aafs->aaline_fs);
    881    }
    882 
    883    FREE((void*)aafs->state.tokens);
    884    FREE(aafs);
    885 }
    886 
    887 
    888 static void
    889 aaline_bind_sampler_states(struct pipe_context *pipe,
    890                            enum pipe_shader_type shader,
    891                            unsigned start, unsigned num, void **sampler)
    892 {
    893    struct aaline_stage *aaline = aaline_stage_from_pipe(pipe);
    894 
    895    assert(start == 0);
    896 
    897    if (!aaline) {
    898       return;
    899    }
    900 
    901    if (shader == PIPE_SHADER_FRAGMENT) {
    902       /* save current */
    903       memcpy(aaline->state.sampler, sampler, num * sizeof(void *));
    904       aaline->num_samplers = num;
    905    }
    906 
    907    /* pass-through */
    908    aaline->driver_bind_sampler_states(pipe, shader, start, num, sampler);
    909 }
    910 
    911 
    912 static void
    913 aaline_set_sampler_views(struct pipe_context *pipe,
    914                          enum pipe_shader_type shader,
    915                          unsigned start, unsigned num,
    916                          struct pipe_sampler_view **views)
    917 {
    918    struct aaline_stage *aaline = aaline_stage_from_pipe(pipe);
    919    uint i;
    920 
    921    if (!aaline) {
    922       return;
    923    }
    924 
    925    if (shader == PIPE_SHADER_FRAGMENT) {
    926       /* save current */
    927       for (i = 0; i < num; i++) {
    928          pipe_sampler_view_reference(&aaline->state.sampler_views[start + i],
    929                                      views[i]);
    930       }
    931       aaline->num_sampler_views = num;
    932    }
    933 
    934    /* pass-through */
    935    aaline->driver_set_sampler_views(pipe, shader, start, num, views);
    936 }
    937 
    938 
    939 void
    940 draw_aaline_prepare_outputs(struct draw_context *draw,
    941                             struct draw_stage *stage)
    942 {
    943    struct aaline_stage *aaline = aaline_stage(stage);
    944    const struct pipe_rasterizer_state *rast = draw->rasterizer;
    945 
    946    /* update vertex attrib info */
    947    aaline->pos_slot = draw_current_shader_position_output(draw);
    948 
    949    if (!rast->line_smooth)
    950       return;
    951 
    952    /* allocate the extra post-transformed vertex attribute */
    953    aaline->tex_slot = draw_alloc_extra_vertex_attrib(draw,
    954                                                      TGSI_SEMANTIC_GENERIC,
    955                                                      aaline->fs->generic_attrib);
    956 }
    957 
    958 /**
    959  * Called by drivers that want to install this AA line prim stage
    960  * into the draw module's pipeline.  This will not be used if the
    961  * hardware has native support for AA lines.
    962  */
    963 boolean
    964 draw_install_aaline_stage(struct draw_context *draw, struct pipe_context *pipe)
    965 {
    966    struct aaline_stage *aaline;
    967 
    968    pipe->draw = (void *) draw;
    969 
    970    /*
    971     * Create / install AA line drawing / prim stage
    972     */
    973    aaline = draw_aaline_stage( draw );
    974    if (!aaline)
    975       goto fail;
    976 
    977    /* save original driver functions */
    978    aaline->driver_create_fs_state = pipe->create_fs_state;
    979    aaline->driver_bind_fs_state = pipe->bind_fs_state;
    980    aaline->driver_delete_fs_state = pipe->delete_fs_state;
    981 
    982    aaline->driver_bind_sampler_states = pipe->bind_sampler_states;
    983    aaline->driver_set_sampler_views = pipe->set_sampler_views;
    984 
    985    /* create special texture, sampler state */
    986    if (!aaline_create_texture(aaline))
    987       goto fail;
    988 
    989    if (!aaline_create_sampler(aaline))
    990       goto fail;
    991 
    992    /* override the driver's functions */
    993    pipe->create_fs_state = aaline_create_fs_state;
    994    pipe->bind_fs_state = aaline_bind_fs_state;
    995    pipe->delete_fs_state = aaline_delete_fs_state;
    996 
    997    pipe->bind_sampler_states = aaline_bind_sampler_states;
    998    pipe->set_sampler_views = aaline_set_sampler_views;
    999 
   1000    /* Install once everything is known to be OK:
   1001     */
   1002    draw->pipeline.aaline = &aaline->stage;
   1003 
   1004    return TRUE;
   1005 
   1006  fail:
   1007    if (aaline)
   1008       aaline->stage.destroy( &aaline->stage );
   1009 
   1010    return FALSE;
   1011 }
   1012