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