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