draw_pipe_pstipple.c revision 507fbe2d327efb8d608ce8e07436b97321560808
1/**************************************************************************
2 *
3 * Copyright 2008 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 * Polygon stipple stage:  implement polygon stipple with texture map and
30 * fragment program.  The fragment program samples the texture and does
31 * a fragment kill for the stipple-failing fragments.
32 *
33 * Authors:  Brian Paul
34 */
35
36
37#include "pipe/p_util.h"
38#include "pipe/p_inlines.h"
39#include "pipe/p_context.h"
40#include "pipe/p_defines.h"
41#include "pipe/p_shader_tokens.h"
42
43#include "tgsi/util/tgsi_transform.h"
44#include "tgsi/util/tgsi_dump.h"
45
46#include "draw_context.h"
47#include "draw_pipe.h"
48
49
50
51/**
52 * Subclass of pipe_shader_state to carry extra fragment shader info.
53 */
54struct pstip_fragment_shader
55{
56   struct pipe_shader_state state;
57   void *driver_fs;
58   void *pstip_fs;
59   uint sampler_unit;
60};
61
62
63/**
64 * Subclass of draw_stage
65 */
66struct pstip_stage
67{
68   struct draw_stage stage;
69
70   void *sampler_cso;
71   struct pipe_texture *texture;
72   uint num_samplers;
73   uint num_textures;
74
75   /*
76    * Currently bound state
77    */
78   struct pstip_fragment_shader *fs;
79   struct {
80      void *samplers[PIPE_MAX_SAMPLERS];
81      struct pipe_texture *textures[PIPE_MAX_SAMPLERS];
82      const struct pipe_poly_stipple *stipple;
83   } state;
84
85   /*
86    * Driver interface/override functions
87    */
88   void * (*driver_create_fs_state)(struct pipe_context *,
89                                    const struct pipe_shader_state *);
90   void (*driver_bind_fs_state)(struct pipe_context *, void *);
91   void (*driver_delete_fs_state)(struct pipe_context *, void *);
92
93   void (*driver_bind_sampler_states)(struct pipe_context *, unsigned, void **);
94
95   void (*driver_set_sampler_textures)(struct pipe_context *, unsigned,
96                                       struct pipe_texture **);
97
98   void (*driver_set_polygon_stipple)(struct pipe_context *,
99                                      const struct pipe_poly_stipple *);
100
101   struct pipe_context *pipe;
102};
103
104
105
106/**
107 * Subclass of tgsi_transform_context, used for transforming the
108 * user's fragment shader to add the special AA instructions.
109 */
110struct pstip_transform_context {
111   struct tgsi_transform_context base;
112   uint tempsUsed;  /**< bitmask */
113   int wincoordInput;
114   int maxInput;
115   uint samplersUsed;  /**< bitfield of samplers used */
116   int freeSampler;  /** an available sampler for the pstipple */
117   int texTemp;  /**< temp registers */
118   int numImmed;
119   boolean firstInstruction;
120};
121
122
123/**
124 * TGSI declaration transform callback.
125 * Look for a free sampler, a free input attrib, and two free temp regs.
126 */
127static void
128pstip_transform_decl(struct tgsi_transform_context *ctx,
129                     struct tgsi_full_declaration *decl)
130{
131   struct pstip_transform_context *pctx = (struct pstip_transform_context *) ctx;
132
133   if (decl->Declaration.File == TGSI_FILE_SAMPLER) {
134      uint i;
135      for (i = decl->u.DeclarationRange.First;
136           i <= decl->u.DeclarationRange.Last; i++) {
137         pctx->samplersUsed |= 1 << i;
138      }
139   }
140   else if (decl->Declaration.File == TGSI_FILE_INPUT) {
141      pctx->maxInput = MAX2(pctx->maxInput, (int) decl->u.DeclarationRange.Last);
142      if (decl->Semantic.SemanticName == TGSI_SEMANTIC_POSITION)
143         pctx->wincoordInput = (int) decl->u.DeclarationRange.First;
144   }
145   else if (decl->Declaration.File == TGSI_FILE_TEMPORARY) {
146      uint i;
147      for (i = decl->u.DeclarationRange.First;
148           i <= decl->u.DeclarationRange.Last; i++) {
149         pctx->tempsUsed |= (1 << i);
150      }
151   }
152
153   ctx->emit_declaration(ctx, decl);
154}
155
156
157static void
158pstip_transform_immed(struct tgsi_transform_context *ctx,
159                      struct tgsi_full_immediate *immed)
160{
161   struct pstip_transform_context *pctx = (struct pstip_transform_context *) ctx;
162   pctx->numImmed++;
163}
164
165
166/**
167 * Find the lowest zero bit in the given word, or -1 if bitfield is all ones.
168 */
169static int
170free_bit(uint bitfield)
171{
172   int i;
173   for (i = 0; i < 32; i++) {
174      if ((bitfield & (1 << i)) == 0)
175         return i;
176   }
177   return -1;
178}
179
180
181/**
182 * TGSI instruction transform callback.
183 * Replace writes to result.color w/ a temp reg.
184 * Upon END instruction, insert texture sampling code for antialiasing.
185 */
186static void
187pstip_transform_inst(struct tgsi_transform_context *ctx,
188                     struct tgsi_full_instruction *inst)
189{
190   struct pstip_transform_context *pctx = (struct pstip_transform_context *) ctx;
191
192   if (pctx->firstInstruction) {
193      /* emit our new declarations before the first instruction */
194
195      struct tgsi_full_declaration decl;
196      struct tgsi_full_instruction newInst;
197      uint i;
198      int wincoordInput;
199
200      /* find free sampler */
201      pctx->freeSampler = free_bit(pctx->samplersUsed);
202      if (pctx->freeSampler >= PIPE_MAX_SAMPLERS)
203         pctx->freeSampler = PIPE_MAX_SAMPLERS - 1;
204
205      if (pctx->wincoordInput < 0)
206         wincoordInput = pctx->maxInput + 1;
207      else
208         wincoordInput = pctx->wincoordInput;
209
210      /* find one free temp reg */
211      for (i = 0; i < 32; i++) {
212         if ((pctx->tempsUsed & (1 << i)) == 0) {
213            /* found a free temp */
214            if (pctx->texTemp < 0)
215               pctx->texTemp  = i;
216            else
217               break;
218         }
219      }
220      assert(pctx->texTemp >= 0);
221
222      if (pctx->wincoordInput < 0) {
223         /* declare new position input reg */
224         decl = tgsi_default_full_declaration();
225         decl.Declaration.File = TGSI_FILE_INPUT;
226         decl.Declaration.Semantic = 1;
227         decl.Semantic.SemanticName = TGSI_SEMANTIC_POSITION;
228         decl.Semantic.SemanticIndex = 0;
229         decl.Declaration.Interpolate = 1;
230         decl.Interpolation.Interpolate = TGSI_INTERPOLATE_LINEAR; /* XXX? */
231         decl.u.DeclarationRange.First =
232            decl.u.DeclarationRange.Last = wincoordInput;
233         ctx->emit_declaration(ctx, &decl);
234      }
235
236      /* declare new sampler */
237      decl = tgsi_default_full_declaration();
238      decl.Declaration.File = TGSI_FILE_SAMPLER;
239      decl.u.DeclarationRange.First =
240      decl.u.DeclarationRange.Last = pctx->freeSampler;
241      ctx->emit_declaration(ctx, &decl);
242
243      /* declare new temp regs */
244      decl = tgsi_default_full_declaration();
245      decl.Declaration.File = TGSI_FILE_TEMPORARY;
246      decl.u.DeclarationRange.First =
247      decl.u.DeclarationRange.Last = pctx->texTemp;
248      ctx->emit_declaration(ctx, &decl);
249
250      /* emit immediate = {1/32, 1/32, 1, 1}
251       * The index/position of this immediate will be pctx->numImmed
252       */
253      {
254         static const float value[4] = { 1.0/32, 1.0/32, 1.0, 1.0 };
255         struct tgsi_full_immediate immed;
256         uint size = 4;
257         immed = tgsi_default_full_immediate();
258         immed.Immediate.Size = 1 + size; /* one for the token itself */
259         immed.u.ImmediateFloat32 = (struct tgsi_immediate_float32 *) value;
260         ctx->emit_immediate(ctx, &immed);
261      }
262
263      pctx->firstInstruction = FALSE;
264
265
266      /*
267       * Insert new MUL/TEX/KILP instructions at start of program
268       * Take gl_FragCoord, divide by 32 (stipple size), sample the
269       * texture and kill fragment if needed.
270       *
271       * We'd like to use non-normalized texcoords to index into a RECT
272       * texture, but we can only use GL_REPEAT wrap mode with normalized
273       * texcoords.  Darn.
274       */
275
276      /* MUL texTemp, INPUT[wincoord], 1/32; */
277      newInst = tgsi_default_full_instruction();
278      newInst.Instruction.Opcode = TGSI_OPCODE_MUL;
279      newInst.Instruction.NumDstRegs = 1;
280      newInst.FullDstRegisters[0].DstRegister.File = TGSI_FILE_TEMPORARY;
281      newInst.FullDstRegisters[0].DstRegister.Index = pctx->texTemp;
282      newInst.Instruction.NumSrcRegs = 2;
283      newInst.FullSrcRegisters[0].SrcRegister.File = TGSI_FILE_INPUT;
284      newInst.FullSrcRegisters[0].SrcRegister.Index = wincoordInput;
285      newInst.FullSrcRegisters[1].SrcRegister.File = TGSI_FILE_IMMEDIATE;
286      newInst.FullSrcRegisters[1].SrcRegister.Index = pctx->numImmed;
287      ctx->emit_instruction(ctx, &newInst);
288
289      /* TEX texTemp, texTemp, sampler; */
290      newInst = tgsi_default_full_instruction();
291      newInst.Instruction.Opcode = TGSI_OPCODE_TEX;
292      newInst.Instruction.NumDstRegs = 1;
293      newInst.FullDstRegisters[0].DstRegister.File = TGSI_FILE_TEMPORARY;
294      newInst.FullDstRegisters[0].DstRegister.Index = pctx->texTemp;
295      newInst.Instruction.NumSrcRegs = 2;
296      newInst.InstructionExtTexture.Texture = TGSI_TEXTURE_2D;
297      newInst.FullSrcRegisters[0].SrcRegister.File = TGSI_FILE_TEMPORARY;
298      newInst.FullSrcRegisters[0].SrcRegister.Index = pctx->texTemp;
299      newInst.FullSrcRegisters[1].SrcRegister.File = TGSI_FILE_SAMPLER;
300      newInst.FullSrcRegisters[1].SrcRegister.Index = pctx->freeSampler;
301      ctx->emit_instruction(ctx, &newInst);
302
303      /* KILP texTemp;   # if texTemp < 0, KILL fragment */
304      newInst = tgsi_default_full_instruction();
305      newInst.Instruction.Opcode = TGSI_OPCODE_KILP;
306      newInst.Instruction.NumDstRegs = 0;
307      newInst.Instruction.NumSrcRegs = 1;
308      newInst.FullSrcRegisters[0].SrcRegister.File = TGSI_FILE_TEMPORARY;
309      newInst.FullSrcRegisters[0].SrcRegister.Index = pctx->texTemp;
310      newInst.FullSrcRegisters[0].SrcRegister.Negate = 1;
311      ctx->emit_instruction(ctx, &newInst);
312   }
313
314   /* emit this instruction */
315   ctx->emit_instruction(ctx, inst);
316}
317
318
319/**
320 * Generate the frag shader we'll use for doing polygon stipple.
321 * This will be the user's shader prefixed with a TEX and KIL instruction.
322 */
323static void
324generate_pstip_fs(struct pstip_stage *pstip)
325{
326   const struct pipe_shader_state *orig_fs = &pstip->fs->state;
327   /*struct draw_context *draw = pstip->stage.draw;*/
328   struct pipe_shader_state pstip_fs;
329   struct pstip_transform_context transform;
330
331#define MAX 1000
332
333   pstip_fs = *orig_fs; /* copy to init */
334   pstip_fs.tokens = MALLOC(sizeof(struct tgsi_token) * MAX);
335
336   memset(&transform, 0, sizeof(transform));
337   transform.wincoordInput = -1;
338   transform.maxInput = -1;
339   transform.texTemp = -1;
340   transform.firstInstruction = TRUE;
341   transform.base.transform_instruction = pstip_transform_inst;
342   transform.base.transform_declaration = pstip_transform_decl;
343   transform.base.transform_immediate = pstip_transform_immed;
344
345   tgsi_transform_shader(orig_fs->tokens,
346                         (struct tgsi_token *) pstip_fs.tokens,
347                         MAX, &transform.base);
348
349#if 0 /* DEBUG */
350   tgsi_dump(orig_fs->tokens, 0);
351   tgsi_dump(pstip_fs.tokens, 0);
352#endif
353
354   pstip->fs->sampler_unit = transform.freeSampler;
355   assert(pstip->fs->sampler_unit < PIPE_MAX_SAMPLERS);
356
357   pstip->fs->pstip_fs = pstip->driver_create_fs_state(pstip->pipe, &pstip_fs);
358}
359
360
361/**
362 * Load texture image with current stipple pattern.
363 */
364static void
365pstip_update_texture(struct pstip_stage *pstip)
366{
367   static const uint bit31 = 1 << 31;
368   struct pipe_context *pipe = pstip->pipe;
369   struct pipe_screen *screen = pipe->screen;
370   struct pipe_surface *surface;
371   const uint *stipple = pstip->state.stipple->stipple;
372   uint i, j;
373   ubyte *data;
374
375   surface = screen->get_tex_surface(screen, pstip->texture, 0, 0, 0);
376   data = pipe_surface_map(surface);
377
378   /*
379    * Load alpha texture.
380    * Note: 0 means keep the fragment, 255 means kill it.
381    * We'll negate the texel value and use KILP which kills if value
382    * is negative.
383    */
384   for (i = 0; i < 32; i++) {
385      for (j = 0; j < 32; j++) {
386         if (stipple[i] & (bit31 >> j)) {
387            /* fragment "on" */
388            data[i * surface->pitch + j] = 0;
389         }
390         else {
391            /* fragment "off" */
392            data[i * surface->pitch + j] = 255;
393         }
394      }
395   }
396
397   /* unmap */
398   pipe_surface_unmap(surface);
399   pipe_surface_reference(&surface, NULL);
400   pipe->texture_update(pipe, pstip->texture, 0, 0x1);
401}
402
403
404/**
405 * Create the texture map we'll use for stippling.
406 */
407static void
408pstip_create_texture(struct pstip_stage *pstip)
409{
410   struct pipe_context *pipe = pstip->pipe;
411   struct pipe_screen *screen = pipe->screen;
412   struct pipe_texture texTemp;
413
414   memset(&texTemp, 0, sizeof(texTemp));
415   texTemp.target = PIPE_TEXTURE_2D;
416   texTemp.format = PIPE_FORMAT_U_A8; /* XXX verify supported by driver! */
417   texTemp.last_level = 0;
418   texTemp.width[0] = 32;
419   texTemp.height[0] = 32;
420   texTemp.depth[0] = 1;
421   texTemp.cpp = 1;
422
423   pstip->texture = screen->texture_create(screen, &texTemp);
424   assert(pstip->texture->refcount == 1);
425}
426
427
428/**
429 * Create the sampler CSO that'll be used for antialiasing.
430 * By using a mipmapped texture, we don't have to generate a different
431 * texture image for each line size.
432 */
433static void
434pstip_create_sampler(struct pstip_stage *pstip)
435{
436   struct pipe_sampler_state sampler;
437   struct pipe_context *pipe = pstip->pipe;
438
439   memset(&sampler, 0, sizeof(sampler));
440   sampler.wrap_s = PIPE_TEX_WRAP_REPEAT;
441   sampler.wrap_t = PIPE_TEX_WRAP_REPEAT;
442   sampler.wrap_r = PIPE_TEX_WRAP_REPEAT;
443   sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
444   sampler.min_img_filter = PIPE_TEX_FILTER_NEAREST;
445   sampler.mag_img_filter = PIPE_TEX_FILTER_NEAREST;
446   sampler.normalized_coords = 1;
447   sampler.min_lod = 0.0f;
448   sampler.max_lod = 0.0f;
449
450   pstip->sampler_cso = pipe->create_sampler_state(pipe, &sampler);
451}
452
453
454/**
455 * When we're about to draw our first AA line in a batch, this function is
456 * called to tell the driver to bind our modified fragment shader.
457 */
458static void
459bind_pstip_fragment_shader(struct pstip_stage *pstip)
460{
461   if (!pstip->fs->pstip_fs) {
462      generate_pstip_fs(pstip);
463   }
464   pstip->driver_bind_fs_state(pstip->pipe, pstip->fs->pstip_fs);
465}
466
467
468
469static INLINE struct pstip_stage *
470pstip_stage( struct draw_stage *stage )
471{
472   return (struct pstip_stage *) stage;
473}
474
475
476static void
477passthrough_point(struct draw_stage *stage, struct prim_header *header)
478{
479   stage->next->point(stage->next, header);
480}
481
482
483static void
484passthrough_line(struct draw_stage *stage, struct prim_header *header)
485{
486   stage->next->line(stage->next, header);
487}
488
489
490static void
491passthrough_tri(struct draw_stage *stage, struct prim_header *header)
492{
493   stage->next->tri(stage->next, header);
494}
495
496
497
498static void
499pstip_first_tri(struct draw_stage *stage, struct prim_header *header)
500{
501   struct pstip_stage *pstip = pstip_stage(stage);
502   struct pipe_context *pipe = pstip->pipe;
503   uint num_samplers;
504
505   assert(stage->draw->rasterizer->poly_stipple_enable);
506
507   /* bind our fragprog */
508   bind_pstip_fragment_shader(pstip);
509
510   /* how many samplers? */
511   /* we'll use sampler/texture[pstip->sampler_unit] for the stipple */
512   num_samplers = MAX2(pstip->num_textures, pstip->num_samplers);
513   num_samplers = MAX2(num_samplers, pstip->fs->sampler_unit + 1);
514
515   /* plug in our sampler, texture */
516   pstip->state.samplers[pstip->fs->sampler_unit] = pstip->sampler_cso;
517   pipe_texture_reference(&pstip->state.textures[pstip->fs->sampler_unit],
518                          pstip->texture);
519
520   assert(num_samplers <= PIPE_MAX_SAMPLERS);
521
522   pstip->driver_bind_sampler_states(pipe, num_samplers, pstip->state.samplers);
523   pstip->driver_set_sampler_textures(pipe, num_samplers, pstip->state.textures);
524
525   /* now really draw first line */
526   stage->tri = passthrough_tri;
527   stage->tri(stage, header);
528}
529
530
531static void
532pstip_flush(struct draw_stage *stage, unsigned flags)
533{
534   /*struct draw_context *draw = stage->draw;*/
535   struct pstip_stage *pstip = pstip_stage(stage);
536   struct pipe_context *pipe = pstip->pipe;
537
538   stage->tri = pstip_first_tri;
539   stage->next->flush( stage->next, flags );
540
541   /* restore original frag shader */
542   pstip->driver_bind_fs_state(pipe, pstip->fs->driver_fs);
543
544   /* XXX restore original texture, sampler state */
545   pstip->driver_bind_sampler_states(pipe, pstip->num_samplers,
546                                     pstip->state.samplers);
547   pstip->driver_set_sampler_textures(pipe, pstip->num_textures,
548                                      pstip->state.textures);
549}
550
551
552static void
553pstip_reset_stipple_counter(struct draw_stage *stage)
554{
555   stage->next->reset_stipple_counter( stage->next );
556}
557
558
559static void
560pstip_destroy(struct draw_stage *stage)
561{
562   struct pstip_stage *pstip = pstip_stage(stage);
563
564   pstip->pipe->delete_sampler_state(pstip->pipe, pstip->sampler_cso);
565
566   pipe_texture_release(&pstip->texture);
567
568   draw_free_temp_verts( stage );
569   FREE( stage );
570}
571
572
573static struct pstip_stage *
574draw_pstip_stage(struct draw_context *draw)
575{
576   struct pstip_stage *pstip = CALLOC_STRUCT(pstip_stage);
577
578   draw_alloc_temp_verts( &pstip->stage, 8 );
579
580   pstip->stage.draw = draw;
581   pstip->stage.next = NULL;
582   pstip->stage.point = passthrough_point;
583   pstip->stage.line = passthrough_line;
584   pstip->stage.tri = pstip_first_tri;
585   pstip->stage.flush = pstip_flush;
586   pstip->stage.reset_stipple_counter = pstip_reset_stipple_counter;
587   pstip->stage.destroy = pstip_destroy;
588
589   return pstip;
590}
591
592
593static struct pstip_stage *
594pstip_stage_from_pipe(struct pipe_context *pipe)
595{
596   struct draw_context *draw = (struct draw_context *) pipe->draw;
597   return pstip_stage(draw->pipeline.pstipple);
598}
599
600
601/**
602 * This function overrides the driver's create_fs_state() function and
603 * will typically be called by the state tracker.
604 */
605static void *
606pstip_create_fs_state(struct pipe_context *pipe,
607                       const struct pipe_shader_state *fs)
608{
609   struct pstip_stage *pstip = pstip_stage_from_pipe(pipe);
610   struct pstip_fragment_shader *aafs = CALLOC_STRUCT(pstip_fragment_shader);
611
612   if (aafs) {
613      aafs->state = *fs;
614
615      /* pass-through */
616      aafs->driver_fs = pstip->driver_create_fs_state(pstip->pipe, fs);
617   }
618
619   return aafs;
620}
621
622
623static void
624pstip_bind_fs_state(struct pipe_context *pipe, void *fs)
625{
626   struct pstip_stage *pstip = pstip_stage_from_pipe(pipe);
627   struct pstip_fragment_shader *aafs = (struct pstip_fragment_shader *) fs;
628   /* save current */
629   pstip->fs = aafs;
630   /* pass-through */
631   pstip->driver_bind_fs_state(pstip->pipe,
632                               (aafs ? aafs->driver_fs : NULL));
633}
634
635
636static void
637pstip_delete_fs_state(struct pipe_context *pipe, void *fs)
638{
639   struct pstip_stage *pstip = pstip_stage_from_pipe(pipe);
640   struct pstip_fragment_shader *aafs = (struct pstip_fragment_shader *) fs;
641   /* pass-through */
642   pstip->driver_delete_fs_state(pstip->pipe, aafs->driver_fs);
643   FREE(aafs);
644}
645
646
647static void
648pstip_bind_sampler_states(struct pipe_context *pipe,
649                          unsigned num, void **sampler)
650{
651   struct pstip_stage *pstip = pstip_stage_from_pipe(pipe);
652   uint i;
653
654   /* save current */
655   memcpy(pstip->state.samplers, sampler, num * sizeof(void *));
656   for (i = num; i < PIPE_MAX_SAMPLERS; i++) {
657      pstip->state.samplers[i] = NULL;
658   }
659
660   pstip->num_samplers = num;
661   /* pass-through */
662   pstip->driver_bind_sampler_states(pstip->pipe, num, sampler);
663}
664
665
666static void
667pstip_set_sampler_textures(struct pipe_context *pipe,
668                           unsigned num, struct pipe_texture **texture)
669{
670   struct pstip_stage *pstip = pstip_stage_from_pipe(pipe);
671   uint i;
672
673   /* save current */
674   for (i = 0; i < num; i++) {
675      pipe_texture_reference(&pstip->state.textures[i], texture[i]);
676   }
677   for (; i < PIPE_MAX_SAMPLERS; i++) {
678      pipe_texture_reference(&pstip->state.textures[i], NULL);
679   }
680
681   pstip->num_textures = num;
682
683   /* pass-through */
684   pstip->driver_set_sampler_textures(pstip->pipe, num, texture);
685}
686
687
688static void
689pstip_set_polygon_stipple(struct pipe_context *pipe,
690                          const struct pipe_poly_stipple *stipple)
691{
692   struct pstip_stage *pstip = pstip_stage_from_pipe(pipe);
693   /* save current */
694   pstip->state.stipple = stipple;
695   /* pass-through */
696   pstip->driver_set_polygon_stipple(pstip->pipe, stipple);
697
698   pstip_update_texture(pstip);
699}
700
701
702
703/**
704 * Called by drivers that want to install this AA line prim stage
705 * into the draw module's pipeline.  This will not be used if the
706 * hardware has native support for AA lines.
707 */
708void
709draw_install_pstipple_stage(struct draw_context *draw,
710                            struct pipe_context *pipe)
711{
712   struct pstip_stage *pstip;
713
714   pipe->draw = (void *) draw;
715
716   /*
717    * Create / install AA line drawing / prim stage
718    */
719   pstip = draw_pstip_stage( draw );
720   assert(pstip);
721   draw->pipeline.pstipple = &pstip->stage;
722
723   pstip->pipe = pipe;
724
725   /* create special texture, sampler state */
726   pstip_create_texture(pstip);
727   pstip_create_sampler(pstip);
728
729   /* save original driver functions */
730   pstip->driver_create_fs_state = pipe->create_fs_state;
731   pstip->driver_bind_fs_state = pipe->bind_fs_state;
732   pstip->driver_delete_fs_state = pipe->delete_fs_state;
733
734   pstip->driver_bind_sampler_states = pipe->bind_sampler_states;
735   pstip->driver_set_sampler_textures = pipe->set_sampler_textures;
736   pstip->driver_set_polygon_stipple = pipe->set_polygon_stipple;
737
738   /* override the driver's functions */
739   pipe->create_fs_state = pstip_create_fs_state;
740   pipe->bind_fs_state = pstip_bind_fs_state;
741   pipe->delete_fs_state = pstip_delete_fs_state;
742
743   pipe->bind_sampler_states = pstip_bind_sampler_states;
744   pipe->set_sampler_textures = pstip_set_sampler_textures;
745   pipe->set_polygon_stipple = pstip_set_polygon_stipple;
746}
747