Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

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