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