• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**************************************************************************
2  *
3  * Copyright 2007-2018 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 triangles (with extra generic)
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/format/u_format.h"
41 #include "util/u_math.h"
42 #include "util/u_memory.h"
43 
44 #include "tgsi/tgsi_transform.h"
45 #include "tgsi/tgsi_dump.h"
46 
47 #include "draw_context.h"
48 #include "draw_private.h"
49 #include "draw_pipe.h"
50 
51 #include "nir.h"
52 #include "nir/nir_draw_helpers.h"
53 
54 /** Approx number of new tokens for instructions in aa_transform_inst() */
55 #define NUM_NEW_TOKENS 53
56 
57 
58 /**
59  * Subclass of pipe_shader_state to carry extra fragment shader info.
60  */
61 struct aaline_fragment_shader
62 {
63    struct pipe_shader_state state;
64    void *driver_fs;
65    void *aaline_fs;
66    int generic_attrib;  /**< generic used for distance */
67 };
68 
69 
70 /**
71  * Subclass of draw_stage
72  */
73 struct aaline_stage
74 {
75    struct draw_stage stage;
76 
77    float half_line_width;
78 
79    /** For AA lines, this is the vertex attrib slot for new generic */
80    uint coord_slot;
81    /** position, not necessarily output zero */
82    uint pos_slot;
83 
84 
85    /*
86     * Currently bound state
87     */
88    struct aaline_fragment_shader *fs;
89 
90    /*
91     * Driver interface/override functions
92     */
93    void * (*driver_create_fs_state)(struct pipe_context *,
94                                     const struct pipe_shader_state *);
95    void (*driver_bind_fs_state)(struct pipe_context *, void *);
96    void (*driver_delete_fs_state)(struct pipe_context *, void *);
97 };
98 
99 
100 
101 /**
102  * Subclass of tgsi_transform_context, used for transforming the
103  * user's fragment shader to add the special AA instructions.
104  */
105 struct aa_transform_context {
106    struct tgsi_transform_context base;
107    uint64_t tempsUsed;  /**< bitmask */
108    int colorOutput; /**< which output is the primary color */
109    int maxInput, maxGeneric;  /**< max input index found */
110    int colorTemp, aaTemp;  /**< temp registers */
111 };
112 
113 /**
114  * TGSI declaration transform callback.
115  * Look for a free input attrib, and two free temp regs.
116  */
117 static void
aa_transform_decl(struct tgsi_transform_context * ctx,struct tgsi_full_declaration * decl)118 aa_transform_decl(struct tgsi_transform_context *ctx,
119                   struct tgsi_full_declaration *decl)
120 {
121    struct aa_transform_context *aactx = (struct aa_transform_context *)ctx;
122 
123    if (decl->Declaration.File == TGSI_FILE_OUTPUT &&
124        decl->Semantic.Name == TGSI_SEMANTIC_COLOR &&
125        decl->Semantic.Index == 0) {
126       aactx->colorOutput = decl->Range.First;
127    }
128    else if (decl->Declaration.File == TGSI_FILE_INPUT) {
129       if ((int) decl->Range.Last > aactx->maxInput)
130          aactx->maxInput = decl->Range.Last;
131       if (decl->Semantic.Name == TGSI_SEMANTIC_GENERIC &&
132            (int) decl->Semantic.Index > aactx->maxGeneric) {
133          aactx->maxGeneric = decl->Semantic.Index;
134       }
135    }
136    else if (decl->Declaration.File == TGSI_FILE_TEMPORARY) {
137       uint i;
138       for (i = decl->Range.First;
139            i <= decl->Range.Last; i++) {
140          /*
141           * XXX this bitfield doesn't really cut it...
142           */
143          aactx->tempsUsed |= UINT64_C(1) << i;
144       }
145    }
146 
147    ctx->emit_declaration(ctx, decl);
148 }
149 
150 
151 /**
152  * Find the lowest zero bit, or -1 if bitfield is all ones.
153  */
154 static int
free_bit(uint64_t bitfield)155 free_bit(uint64_t bitfield)
156 {
157    return ffsll(~bitfield) - 1;
158 }
159 
160 
161 /**
162  * TGSI transform prolog callback.
163  */
164 static void
aa_transform_prolog(struct tgsi_transform_context * ctx)165 aa_transform_prolog(struct tgsi_transform_context *ctx)
166 {
167    struct aa_transform_context *aactx = (struct aa_transform_context *) ctx;
168    uint64_t usedTemps = aactx->tempsUsed;
169 
170    /* find two free temp regs */
171    aactx->colorTemp = free_bit(usedTemps);
172    usedTemps |= UINT64_C(1) << aactx->colorTemp;
173    aactx->aaTemp = free_bit(usedTemps);
174    assert(aactx->colorTemp >= 0);
175    assert(aactx->aaTemp >= 0);
176 
177    /* declare new generic input/texcoord */
178    tgsi_transform_input_decl(ctx, aactx->maxInput + 1,
179                              TGSI_SEMANTIC_GENERIC, aactx->maxGeneric + 1,
180                              TGSI_INTERPOLATE_LINEAR);
181 
182    /* declare new temp regs */
183    tgsi_transform_temp_decl(ctx, aactx->aaTemp);
184    tgsi_transform_temp_decl(ctx, aactx->colorTemp);
185 }
186 
187 
188 /**
189  * TGSI transform epilog callback.
190  */
191 static void
aa_transform_epilog(struct tgsi_transform_context * ctx)192 aa_transform_epilog(struct tgsi_transform_context *ctx)
193 {
194    struct aa_transform_context *aactx = (struct aa_transform_context *) ctx;
195 
196    if (aactx->colorOutput != -1) {
197       struct tgsi_full_instruction inst;
198       /* insert distance-based coverage code for antialiasing. */
199 
200       /* saturate(linewidth - fabs(interpx), linelength - fabs(interpz) */
201       inst = tgsi_default_full_instruction();
202       inst.Instruction.Saturate = true;
203       inst.Instruction.Opcode = TGSI_OPCODE_ADD;
204       inst.Instruction.NumDstRegs = 1;
205       tgsi_transform_dst_reg(&inst.Dst[0], TGSI_FILE_TEMPORARY,
206                              aactx->aaTemp, TGSI_WRITEMASK_XZ);
207       inst.Instruction.NumSrcRegs = 2;
208       tgsi_transform_src_reg(&inst.Src[1], TGSI_FILE_INPUT, aactx->maxInput + 1,
209                              TGSI_SWIZZLE_X, TGSI_SWIZZLE_X,
210                              TGSI_SWIZZLE_Z, TGSI_SWIZZLE_Z);
211       tgsi_transform_src_reg(&inst.Src[0], TGSI_FILE_INPUT, aactx->maxInput + 1,
212                              TGSI_SWIZZLE_Y, TGSI_SWIZZLE_Y,
213                              TGSI_SWIZZLE_W, TGSI_SWIZZLE_W);
214       inst.Src[1].Register.Absolute = true;
215       inst.Src[1].Register.Negate = true;
216       ctx->emit_instruction(ctx, &inst);
217 
218       /* MUL width / height alpha */
219       tgsi_transform_op2_swz_inst(ctx, TGSI_OPCODE_MUL,
220                                   TGSI_FILE_TEMPORARY, aactx->aaTemp,
221                                   TGSI_WRITEMASK_W,
222                                   TGSI_FILE_TEMPORARY, aactx->aaTemp,
223                                   TGSI_SWIZZLE_X,
224                                   TGSI_FILE_TEMPORARY, aactx->aaTemp,
225                                   TGSI_SWIZZLE_Z, false);
226 
227       /* MOV rgb */
228       tgsi_transform_op1_inst(ctx, TGSI_OPCODE_MOV,
229                               TGSI_FILE_OUTPUT, aactx->colorOutput,
230                               TGSI_WRITEMASK_XYZ,
231                               TGSI_FILE_TEMPORARY, aactx->colorTemp);
232 
233       /* MUL alpha */
234       tgsi_transform_op2_inst(ctx, TGSI_OPCODE_MUL,
235                               TGSI_FILE_OUTPUT, aactx->colorOutput,
236                               TGSI_WRITEMASK_W,
237                               TGSI_FILE_TEMPORARY, aactx->colorTemp,
238                               TGSI_FILE_TEMPORARY, aactx->aaTemp, false);
239    }
240 }
241 
242 
243 /**
244  * TGSI instruction transform callback.
245  * Replace writes to result.color w/ a temp reg.
246  */
247 static void
aa_transform_inst(struct tgsi_transform_context * ctx,struct tgsi_full_instruction * inst)248 aa_transform_inst(struct tgsi_transform_context *ctx,
249                   struct tgsi_full_instruction *inst)
250 {
251    struct aa_transform_context *aactx = (struct aa_transform_context *) ctx;
252    uint i;
253 
254    /*
255     * Look for writes to result.color and replace with colorTemp reg.
256     */
257    for (i = 0; i < inst->Instruction.NumDstRegs; i++) {
258       struct tgsi_full_dst_register *dst = &inst->Dst[i];
259       if (dst->Register.File == TGSI_FILE_OUTPUT &&
260           dst->Register.Index == aactx->colorOutput) {
261          dst->Register.File = TGSI_FILE_TEMPORARY;
262          dst->Register.Index = aactx->colorTemp;
263       }
264    }
265 
266    ctx->emit_instruction(ctx, inst);
267 }
268 
269 
270 /**
271  * Generate the frag shader we'll use for drawing AA lines.
272  * This will be the user's shader plus some arithmetic instructions.
273  */
274 static boolean
generate_aaline_fs(struct aaline_stage * aaline)275 generate_aaline_fs(struct aaline_stage *aaline)
276 {
277    struct pipe_context *pipe = aaline->stage.draw->pipe;
278    const struct pipe_shader_state *orig_fs = &aaline->fs->state;
279    struct pipe_shader_state aaline_fs;
280    struct aa_transform_context transform;
281    const uint newLen = tgsi_num_tokens(orig_fs->tokens) + NUM_NEW_TOKENS;
282 
283    aaline_fs = *orig_fs; /* copy to init */
284 
285    memset(&transform, 0, sizeof(transform));
286    transform.colorOutput = -1;
287    transform.maxInput = -1;
288    transform.maxGeneric = -1;
289    transform.colorTemp = -1;
290    transform.aaTemp = -1;
291    transform.base.prolog = aa_transform_prolog;
292    transform.base.epilog = aa_transform_epilog;
293    transform.base.transform_instruction = aa_transform_inst;
294    transform.base.transform_declaration = aa_transform_decl;
295 
296    aaline_fs.tokens = tgsi_transform_shader(orig_fs->tokens, newLen, &transform.base);
297    if (!aaline_fs.tokens)
298       return false;
299 
300 #if 0 /* DEBUG */
301    debug_printf("draw_aaline, orig shader:\n");
302    tgsi_dump(orig_fs->tokens, 0);
303    debug_printf("draw_aaline, new shader:\n");
304    tgsi_dump(aaline_fs.tokens, 0);
305 #endif
306 
307    aaline->fs->aaline_fs = aaline->driver_create_fs_state(pipe, &aaline_fs);
308    if (aaline->fs->aaline_fs != NULL)
309       aaline->fs->generic_attrib = transform.maxGeneric + 1;
310 
311    FREE((void *)aaline_fs.tokens);
312    return aaline->fs->aaline_fs != NULL;
313 }
314 
315 static boolean
generate_aaline_fs_nir(struct aaline_stage * aaline)316 generate_aaline_fs_nir(struct aaline_stage *aaline)
317 {
318    struct pipe_context *pipe = aaline->stage.draw->pipe;
319    const struct pipe_shader_state *orig_fs = &aaline->fs->state;
320    struct pipe_shader_state aaline_fs;
321 
322    aaline_fs = *orig_fs; /* copy to init */
323    aaline_fs.ir.nir = nir_shader_clone(NULL, orig_fs->ir.nir);
324    if (!aaline_fs.ir.nir)
325       return FALSE;
326 
327    nir_lower_aaline_fs(aaline_fs.ir.nir, &aaline->fs->generic_attrib);
328    aaline->fs->aaline_fs = aaline->driver_create_fs_state(pipe, &aaline_fs);
329    if (aaline->fs->aaline_fs == NULL)
330       return FALSE;
331 
332    return TRUE;
333 }
334 
335 /**
336  * When we're about to draw our first AA line in a batch, this function is
337  * called to tell the driver to bind our modified fragment shader.
338  */
339 static boolean
bind_aaline_fragment_shader(struct aaline_stage * aaline)340 bind_aaline_fragment_shader(struct aaline_stage *aaline)
341 {
342    struct draw_context *draw = aaline->stage.draw;
343    struct pipe_context *pipe = draw->pipe;
344 
345    if (!aaline->fs->aaline_fs) {
346       if (aaline->fs->state.type == PIPE_SHADER_IR_NIR) {
347          if (!generate_aaline_fs_nir(aaline))
348             return FALSE;
349       } else
350          if (!generate_aaline_fs(aaline))
351             return FALSE;
352    }
353 
354    draw->suspend_flushing = TRUE;
355    aaline->driver_bind_fs_state(pipe, aaline->fs->aaline_fs);
356    draw->suspend_flushing = FALSE;
357 
358    return TRUE;
359 }
360 
361 
362 
363 static inline struct aaline_stage *
aaline_stage(struct draw_stage * stage)364 aaline_stage(struct draw_stage *stage)
365 {
366    return (struct aaline_stage *) stage;
367 }
368 
369 
370 /**
371  * Draw a wide line by drawing a quad, using geometry which will
372  * fullfill GL's antialiased line requirements.
373  */
374 static void
aaline_line(struct draw_stage * stage,struct prim_header * header)375 aaline_line(struct draw_stage *stage, struct prim_header *header)
376 {
377    const struct aaline_stage *aaline = aaline_stage(stage);
378    const float half_width = aaline->half_line_width;
379    struct prim_header tri;
380    struct vertex_header *v[8];
381    uint coordPos = aaline->coord_slot;
382    uint posPos = aaline->pos_slot;
383    float *pos, *tex;
384    float dx = header->v[1]->data[posPos][0] - header->v[0]->data[posPos][0];
385    float dy = header->v[1]->data[posPos][1] - header->v[0]->data[posPos][1];
386    float a = atan2f(dy, dx);
387    float c_a = cosf(a), s_a = sinf(a);
388    float half_length;
389    float t_l, t_w;
390    uint i;
391 
392    half_length = 0.5f * sqrtf(dx * dx + dy * dy);
393 
394    if (half_length < 0.5f) {
395       /*
396        * The logic we use for "normal" sized segments is incorrect
397        * for very short segments (basically because we only have
398        * one value to interpolate, not a distance to each endpoint).
399        * Therefore, we calculate half_length differently, so that for
400        * original line length (near) 0, we get alpha 0 - otherwise
401        * max alpha would still be 0.5. This also prevents us from
402        * artifacts due to degenerated lines (the endpoints being
403        * identical, which would still receive anywhere from alpha
404        * 0-0.5 otherwise) (at least the pstipple stage may generate
405        * such lines due to float inaccuracies if line length is very
406        * close to a integer).
407        * Might not be fully accurate neither (because the "strength" of
408        * the line is going to be determined by how close to the pixel
409        * center those 1 or 2 fragments are) but it's probably the best
410        * we can do.
411        */
412       half_length = 2.0f * half_length;
413    } else {
414       half_length = half_length + 0.5f;
415    }
416 
417    t_w = half_width;
418    t_l = 0.5f;
419 
420    /* allocate/dup new verts */
421    for (i = 0; i < 4; i++) {
422       v[i] = dup_vert(stage, header->v[i/2], i);
423    }
424 
425    /*
426     * Quad strip for line from v0 to v1 (*=endpoints):
427     *
428     *  1                             3
429     *  +-----------------------------+
430     *  |                             |
431     *  | *v0                     v1* |
432     *  |                             |
433     *  +-----------------------------+
434     *  0                             2
435     */
436 
437    /*
438     * We increase line length by 0.5 pixels (at each endpoint),
439     * and calculate the tri endpoints by moving them half-width
440     * distance away perpendicular to the line.
441     * XXX: since we change line endpoints (by 0.5 pixel), should
442     * actually re-interpolate all other values?
443     */
444 
445    /* new verts */
446    pos = v[0]->data[posPos];
447    pos[0] += (-t_l * c_a -  t_w * s_a);
448    pos[1] += (-t_l * s_a +  t_w * c_a);
449 
450    pos = v[1]->data[posPos];
451    pos[0] += (-t_l * c_a - -t_w * s_a);
452    pos[1] += (-t_l * s_a + -t_w * c_a);
453 
454    pos = v[2]->data[posPos];
455    pos[0] += (t_l * c_a -  t_w * s_a);
456    pos[1] += (t_l * s_a +  t_w * c_a);
457 
458    pos = v[3]->data[posPos];
459    pos[0] += (t_l * c_a - -t_w * s_a);
460    pos[1] += (t_l * s_a + -t_w * c_a);
461 
462    /* new texcoords */
463    tex = v[0]->data[coordPos];
464    ASSIGN_4V(tex, -half_width, half_width, -half_length, half_length);
465 
466    tex = v[1]->data[coordPos];
467    ASSIGN_4V(tex, half_width, half_width, -half_length, half_length);
468 
469    tex = v[2]->data[coordPos];
470    ASSIGN_4V(tex, -half_width, half_width, half_length, half_length);
471 
472    tex = v[3]->data[coordPos];
473    ASSIGN_4V(tex, half_width, half_width, half_length, half_length);
474 
475    tri.v[0] = v[2];  tri.v[1] = v[1];  tri.v[2] = v[0];
476    stage->next->tri(stage->next, &tri);
477 
478    tri.v[0] = v[3];  tri.v[1] = v[1];  tri.v[2] = v[2];
479    stage->next->tri(stage->next, &tri);
480 }
481 
482 
483 static void
aaline_first_line(struct draw_stage * stage,struct prim_header * header)484 aaline_first_line(struct draw_stage *stage, struct prim_header *header)
485 {
486    auto struct aaline_stage *aaline = aaline_stage(stage);
487    struct draw_context *draw = stage->draw;
488    struct pipe_context *pipe = draw->pipe;
489    const struct pipe_rasterizer_state *rast = draw->rasterizer;
490    void *r;
491 
492    assert(draw->rasterizer->line_smooth && !draw->rasterizer->multisample);
493 
494    if (draw->rasterizer->line_width <= 1.0)
495       aaline->half_line_width = 1.0;
496    else
497       aaline->half_line_width = 0.5f * draw->rasterizer->line_width + 0.5f;
498 
499    if (!draw->rasterizer->half_pixel_center)
500       /*
501        * The tex coords probably would need adjustments?
502        */
503       debug_printf("aa lines without half pixel center may be wrong\n");
504 
505    /*
506     * Bind (generate) our fragprog
507     */
508    if (!bind_aaline_fragment_shader(aaline)) {
509       stage->line = draw_pipe_passthrough_line;
510       stage->line(stage, header);
511       return;
512    }
513 
514    draw_aaline_prepare_outputs(draw, draw->pipeline.aaline);
515 
516    draw->suspend_flushing = TRUE;
517 
518    /* Disable triangle culling, stippling, unfilled mode etc. */
519    r = draw_get_rasterizer_no_cull(draw, rast);
520    pipe->bind_rasterizer_state(pipe, r);
521 
522    draw->suspend_flushing = FALSE;
523 
524    /* now really draw first line */
525    stage->line = aaline_line;
526    stage->line(stage, header);
527 }
528 
529 
530 static void
aaline_flush(struct draw_stage * stage,unsigned flags)531 aaline_flush(struct draw_stage *stage, unsigned flags)
532 {
533    struct draw_context *draw = stage->draw;
534    struct aaline_stage *aaline = aaline_stage(stage);
535    struct pipe_context *pipe = draw->pipe;
536 
537    stage->line = aaline_first_line;
538    stage->next->flush(stage->next, flags);
539 
540    /* restore original frag shader */
541    draw->suspend_flushing = TRUE;
542    aaline->driver_bind_fs_state(pipe, aaline->fs ? aaline->fs->driver_fs : NULL);
543 
544    /* restore original rasterizer state */
545    if (draw->rast_handle) {
546       pipe->bind_rasterizer_state(pipe, draw->rast_handle);
547    }
548 
549    draw->suspend_flushing = FALSE;
550 
551    draw_remove_extra_vertex_attribs(draw);
552 }
553 
554 
555 static void
aaline_reset_stipple_counter(struct draw_stage * stage)556 aaline_reset_stipple_counter(struct draw_stage *stage)
557 {
558    stage->next->reset_stipple_counter(stage->next);
559 }
560 
561 
562 static void
aaline_destroy(struct draw_stage * stage)563 aaline_destroy(struct draw_stage *stage)
564 {
565    struct aaline_stage *aaline = aaline_stage(stage);
566    struct pipe_context *pipe = stage->draw->pipe;
567 
568    draw_free_temp_verts(stage);
569 
570    /* restore the old entry points */
571    pipe->create_fs_state = aaline->driver_create_fs_state;
572    pipe->bind_fs_state = aaline->driver_bind_fs_state;
573    pipe->delete_fs_state = aaline->driver_delete_fs_state;
574 
575    FREE(stage);
576 }
577 
578 
579 static struct aaline_stage *
draw_aaline_stage(struct draw_context * draw)580 draw_aaline_stage(struct draw_context *draw)
581 {
582    struct aaline_stage *aaline = CALLOC_STRUCT(aaline_stage);
583    if (!aaline)
584       return NULL;
585 
586    aaline->stage.draw = draw;
587    aaline->stage.name = "aaline";
588    aaline->stage.next = NULL;
589    aaline->stage.point = draw_pipe_passthrough_point;
590    aaline->stage.line = aaline_first_line;
591    aaline->stage.tri = draw_pipe_passthrough_tri;
592    aaline->stage.flush = aaline_flush;
593    aaline->stage.reset_stipple_counter = aaline_reset_stipple_counter;
594    aaline->stage.destroy = aaline_destroy;
595 
596    if (!draw_alloc_temp_verts(&aaline->stage, 8)) {
597       aaline->stage.destroy(&aaline->stage);
598       return NULL;
599    }
600 
601    return aaline;
602 }
603 
604 
605 static struct aaline_stage *
aaline_stage_from_pipe(struct pipe_context * pipe)606 aaline_stage_from_pipe(struct pipe_context *pipe)
607 {
608    struct draw_context *draw = (struct draw_context *) pipe->draw;
609 
610    if (draw) {
611       return aaline_stage(draw->pipeline.aaline);
612    } else {
613       return NULL;
614    }
615 }
616 
617 
618 /**
619  * This function overrides the driver's create_fs_state() function and
620  * will typically be called by the gallium frontend.
621  */
622 static void *
aaline_create_fs_state(struct pipe_context * pipe,const struct pipe_shader_state * fs)623 aaline_create_fs_state(struct pipe_context *pipe,
624                        const struct pipe_shader_state *fs)
625 {
626    struct aaline_stage *aaline = aaline_stage_from_pipe(pipe);
627    struct aaline_fragment_shader *aafs = NULL;
628 
629    if (!aaline)
630       return NULL;
631 
632    aafs = CALLOC_STRUCT(aaline_fragment_shader);
633 
634    if (!aafs)
635       return NULL;
636 
637    aafs->state.type = fs->type;
638    if (fs->type == PIPE_SHADER_IR_TGSI)
639       aafs->state.tokens = tgsi_dup_tokens(fs->tokens);
640    else
641       aafs->state.ir.nir = nir_shader_clone(NULL, fs->ir.nir);
642 
643    /* pass-through */
644    aafs->driver_fs = aaline->driver_create_fs_state(pipe, fs);
645 
646    return aafs;
647 }
648 
649 
650 static void
aaline_bind_fs_state(struct pipe_context * pipe,void * fs)651 aaline_bind_fs_state(struct pipe_context *pipe, void *fs)
652 {
653    struct aaline_stage *aaline = aaline_stage_from_pipe(pipe);
654    struct aaline_fragment_shader *aafs = (struct aaline_fragment_shader *) fs;
655 
656    if (!aaline) {
657       return;
658    }
659 
660    /* save current */
661    aaline->fs = aafs;
662    /* pass-through */
663    aaline->driver_bind_fs_state(pipe, (aafs ? aafs->driver_fs : NULL));
664 }
665 
666 
667 static void
aaline_delete_fs_state(struct pipe_context * pipe,void * fs)668 aaline_delete_fs_state(struct pipe_context *pipe, void *fs)
669 {
670    struct aaline_stage *aaline = aaline_stage_from_pipe(pipe);
671    struct aaline_fragment_shader *aafs = (struct aaline_fragment_shader *) fs;
672 
673    if (!aafs) {
674       return;
675    }
676 
677    if (aaline) {
678       /* pass-through */
679       aaline->driver_delete_fs_state(pipe, aafs->driver_fs);
680 
681       if (aafs->aaline_fs)
682          aaline->driver_delete_fs_state(pipe, aafs->aaline_fs);
683    }
684 
685    if (aafs->state.type == PIPE_SHADER_IR_TGSI)
686       FREE((void*)aafs->state.tokens);
687    else
688       ralloc_free(aafs->state.ir.nir);
689    FREE(aafs);
690 }
691 
692 
693 void
draw_aaline_prepare_outputs(struct draw_context * draw,struct draw_stage * stage)694 draw_aaline_prepare_outputs(struct draw_context *draw,
695                             struct draw_stage *stage)
696 {
697    struct aaline_stage *aaline = aaline_stage(stage);
698    const struct pipe_rasterizer_state *rast = draw->rasterizer;
699 
700    /* update vertex attrib info */
701    aaline->pos_slot = draw_current_shader_position_output(draw);
702 
703    if (!rast->line_smooth || rast->multisample)
704       return;
705 
706    /* allocate the extra post-transformed vertex attribute */
707    if (aaline->fs && aaline->fs->aaline_fs)
708       aaline->coord_slot = draw_alloc_extra_vertex_attrib(draw,
709                                                           TGSI_SEMANTIC_GENERIC,
710                                                           aaline->fs->generic_attrib);
711    else
712       aaline->coord_slot = -1;
713 }
714 
715 /**
716  * Called by drivers that want to install this AA line prim stage
717  * into the draw module's pipeline.  This will not be used if the
718  * hardware has native support for AA lines.
719  */
720 boolean
draw_install_aaline_stage(struct draw_context * draw,struct pipe_context * pipe)721 draw_install_aaline_stage(struct draw_context *draw, struct pipe_context *pipe)
722 {
723    struct aaline_stage *aaline;
724 
725    pipe->draw = (void *) draw;
726 
727    /*
728     * Create / install AA line drawing / prim stage
729     */
730    aaline = draw_aaline_stage(draw);
731    if (!aaline)
732       return FALSE;
733 
734    /* save original driver functions */
735    aaline->driver_create_fs_state = pipe->create_fs_state;
736    aaline->driver_bind_fs_state = pipe->bind_fs_state;
737    aaline->driver_delete_fs_state = pipe->delete_fs_state;
738 
739    /* override the driver's functions */
740    pipe->create_fs_state = aaline_create_fs_state;
741    pipe->bind_fs_state = aaline_bind_fs_state;
742    pipe->delete_fs_state = aaline_delete_fs_state;
743 
744    /* Install once everything is known to be OK:
745     */
746    draw->pipeline.aaline = &aaline->stage;
747 
748    return TRUE;
749 }
750