tgsi/scan: add tgsi_shader_info::reads_samplemask
[mesa.git] / src / gallium / auxiliary / draw / draw_pipe_pstipple.c
1 /**************************************************************************
2 *
3 * Copyright 2008 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 /**
29 * 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 53
57
58
59 /**
60 * Subclass of pipe_shader_state to carry extra fragment shader info.
61 */
62 struct 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 */
74 struct 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_SHADER_SAMPLER_VIEWS];
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,
103 unsigned, unsigned, void **);
104
105 void (*driver_set_sampler_views)(struct pipe_context *,
106 unsigned shader, unsigned start,
107 unsigned count,
108 struct pipe_sampler_view **);
109
110 void (*driver_set_polygon_stipple)(struct pipe_context *,
111 const struct pipe_poly_stipple *);
112
113 struct pipe_context *pipe;
114 };
115
116
117
118 /**
119 * Subclass of tgsi_transform_context, used for transforming the
120 * user's fragment shader to add the extra texture sample and fragment kill
121 * instructions.
122 */
123 struct pstip_transform_context {
124 struct tgsi_transform_context base;
125 uint tempsUsed; /**< bitmask */
126 int wincoordInput;
127 int maxInput;
128 uint samplersUsed; /**< bitfield of samplers used */
129 bool hasSview;
130 int freeSampler; /** an available sampler for the pstipple */
131 int texTemp; /**< temp registers */
132 int numImmed;
133 };
134
135
136 /**
137 * TGSI declaration transform callback.
138 * Look for a free sampler, a free input attrib, and two free temp regs.
139 */
140 static void
141 pstip_transform_decl(struct tgsi_transform_context *ctx,
142 struct tgsi_full_declaration *decl)
143 {
144 struct pstip_transform_context *pctx = (struct pstip_transform_context *) ctx;
145
146 if (decl->Declaration.File == TGSI_FILE_SAMPLER) {
147 uint i;
148 for (i = decl->Range.First;
149 i <= decl->Range.Last; i++) {
150 pctx->samplersUsed |= 1 << i;
151 }
152 }
153 else if (decl->Declaration.File == TGSI_FILE_SAMPLER_VIEW) {
154 pctx->hasSview = true;
155 }
156 else if (decl->Declaration.File == TGSI_FILE_INPUT) {
157 pctx->maxInput = MAX2(pctx->maxInput, (int) decl->Range.Last);
158 if (decl->Semantic.Name == TGSI_SEMANTIC_POSITION)
159 pctx->wincoordInput = (int) decl->Range.First;
160 }
161 else if (decl->Declaration.File == TGSI_FILE_TEMPORARY) {
162 uint i;
163 for (i = decl->Range.First;
164 i <= decl->Range.Last; i++) {
165 pctx->tempsUsed |= (1 << i);
166 }
167 }
168
169 ctx->emit_declaration(ctx, decl);
170 }
171
172
173 /**
174 * TGSI immediate declaration transform callback.
175 * We're just counting the number of immediates here.
176 */
177 static void
178 pstip_transform_immed(struct tgsi_transform_context *ctx,
179 struct tgsi_full_immediate *immed)
180 {
181 struct pstip_transform_context *pctx = (struct pstip_transform_context *) ctx;
182 ctx->emit_immediate(ctx, immed); /* emit to output shader */
183 pctx->numImmed++;
184 }
185
186
187 /**
188 * Find the lowest zero bit in the given word, or -1 if bitfield is all ones.
189 */
190 static int
191 free_bit(uint bitfield)
192 {
193 return ffs(~bitfield) - 1;
194 }
195
196
197 /**
198 * TGSI transform prolog callback.
199 */
200 static void
201 pstip_transform_prolog(struct tgsi_transform_context *ctx)
202 {
203 struct pstip_transform_context *pctx = (struct pstip_transform_context *) ctx;
204 uint i;
205 int wincoordInput;
206
207 /* find free sampler */
208 pctx->freeSampler = free_bit(pctx->samplersUsed);
209 if (pctx->freeSampler >= PIPE_MAX_SAMPLERS)
210 pctx->freeSampler = PIPE_MAX_SAMPLERS - 1;
211
212 if (pctx->wincoordInput < 0)
213 wincoordInput = pctx->maxInput + 1;
214 else
215 wincoordInput = pctx->wincoordInput;
216
217 /* find one free temp reg */
218 for (i = 0; i < 32; i++) {
219 if ((pctx->tempsUsed & (1 << i)) == 0) {
220 /* found a free temp */
221 if (pctx->texTemp < 0)
222 pctx->texTemp = i;
223 else
224 break;
225 }
226 }
227 assert(pctx->texTemp >= 0);
228
229 if (pctx->wincoordInput < 0) {
230 /* declare new position input reg */
231 tgsi_transform_input_decl(ctx, wincoordInput,
232 TGSI_SEMANTIC_POSITION, 1,
233 TGSI_INTERPOLATE_LINEAR);
234 }
235
236 /* declare new sampler */
237 tgsi_transform_sampler_decl(ctx, pctx->freeSampler);
238
239 /* if the src shader has SVIEW decl's for each SAMP decl, we
240 * need to continue the trend and ensure there is a matching
241 * SVIEW for the new SAMP we just created
242 */
243 if (pctx->hasSview) {
244 tgsi_transform_sampler_view_decl(ctx,
245 pctx->freeSampler,
246 TGSI_TEXTURE_2D,
247 TGSI_RETURN_TYPE_FLOAT);
248 }
249
250 /* declare new temp regs */
251 tgsi_transform_temp_decl(ctx, pctx->texTemp);
252
253 /* emit immediate = {1/32, 1/32, 1, 1}
254 * The index/position of this immediate will be pctx->numImmed
255 */
256 tgsi_transform_immediate_decl(ctx, 1.0/32.0, 1.0/32.0, 1.0, 1.0);
257
258 /*
259 * Insert new MUL/TEX/KILL_IF instructions at start of program
260 * Take gl_FragCoord, divide by 32 (stipple size), sample the
261 * texture and kill fragment if needed.
262 *
263 * We'd like to use non-normalized texcoords to index into a RECT
264 * texture, but we can only use GL_REPEAT wrap mode with normalized
265 * texcoords. Darn.
266 */
267
268 /* MUL texTemp, INPUT[wincoord], 1/32; */
269 tgsi_transform_op2_inst(ctx, TGSI_OPCODE_MUL,
270 TGSI_FILE_TEMPORARY, pctx->texTemp,
271 TGSI_WRITEMASK_XYZW,
272 TGSI_FILE_INPUT, wincoordInput,
273 TGSI_FILE_IMMEDIATE, pctx->numImmed);
274
275 /* TEX texTemp, texTemp, sampler; */
276 tgsi_transform_tex_2d_inst(ctx,
277 TGSI_FILE_TEMPORARY, pctx->texTemp,
278 TGSI_FILE_TEMPORARY, pctx->texTemp,
279 pctx->freeSampler);
280
281 /* KILL_IF -texTemp.wwww; # if -texTemp < 0, KILL fragment */
282 tgsi_transform_kill_inst(ctx,
283 TGSI_FILE_TEMPORARY, pctx->texTemp,
284 TGSI_SWIZZLE_W, TRUE);
285 }
286
287
288
289 /**
290 * Generate the frag shader we'll use for doing polygon stipple.
291 * This will be the user's shader prefixed with a TEX and KIL instruction.
292 */
293 static boolean
294 generate_pstip_fs(struct pstip_stage *pstip)
295 {
296 const struct pipe_shader_state *orig_fs = &pstip->fs->state;
297 /*struct draw_context *draw = pstip->stage.draw;*/
298 struct pipe_shader_state pstip_fs;
299 struct pstip_transform_context transform;
300 const uint newLen = tgsi_num_tokens(orig_fs->tokens) + NUM_NEW_TOKENS;
301
302 pstip_fs = *orig_fs; /* copy to init */
303 pstip_fs.tokens = tgsi_alloc_tokens(newLen);
304 if (pstip_fs.tokens == NULL)
305 return FALSE;
306
307 memset(&transform, 0, sizeof(transform));
308 transform.wincoordInput = -1;
309 transform.maxInput = -1;
310 transform.texTemp = -1;
311 transform.base.prolog = pstip_transform_prolog;
312 transform.base.transform_declaration = pstip_transform_decl;
313 transform.base.transform_immediate = pstip_transform_immed;
314
315 tgsi_transform_shader(orig_fs->tokens,
316 (struct tgsi_token *) pstip_fs.tokens,
317 newLen, &transform.base);
318
319 #if 0 /* DEBUG */
320 tgsi_dump(orig_fs->tokens, 0);
321 tgsi_dump(pstip_fs.tokens, 0);
322 #endif
323
324 assert(pstip->fs);
325
326 pstip->fs->sampler_unit = transform.freeSampler;
327 assert(pstip->fs->sampler_unit < PIPE_MAX_SAMPLERS);
328
329 pstip->fs->pstip_fs = pstip->driver_create_fs_state(pstip->pipe, &pstip_fs);
330
331 FREE((void *)pstip_fs.tokens);
332
333 if (!pstip->fs->pstip_fs)
334 return FALSE;
335
336 return TRUE;
337 }
338
339
340 /**
341 * Load texture image with current stipple pattern.
342 */
343 static void
344 pstip_update_texture(struct pstip_stage *pstip)
345 {
346 static const uint bit31 = 1 << 31;
347 struct pipe_context *pipe = pstip->pipe;
348 struct pipe_transfer *transfer;
349 const uint *stipple = pstip->state.stipple->stipple;
350 uint i, j;
351 ubyte *data;
352
353 data = pipe_transfer_map(pipe, pstip->texture, 0, 0,
354 PIPE_TRANSFER_WRITE, 0, 0, 32, 32, &transfer);
355
356 /*
357 * Load alpha texture.
358 * Note: 0 means keep the fragment, 255 means kill it.
359 * We'll negate the texel value and use KILL_IF which kills if value
360 * is negative.
361 */
362 for (i = 0; i < 32; i++) {
363 for (j = 0; j < 32; j++) {
364 if (stipple[i] & (bit31 >> j)) {
365 /* fragment "on" */
366 data[i * transfer->stride + j] = 0;
367 }
368 else {
369 /* fragment "off" */
370 data[i * transfer->stride + j] = 255;
371 }
372 }
373 }
374
375 /* unmap */
376 pipe_transfer_unmap(pipe, transfer);
377 }
378
379
380 /**
381 * Create the texture map we'll use for stippling.
382 */
383 static boolean
384 pstip_create_texture(struct pstip_stage *pstip)
385 {
386 struct pipe_context *pipe = pstip->pipe;
387 struct pipe_screen *screen = pipe->screen;
388 struct pipe_resource texTemp;
389 struct pipe_sampler_view viewTempl;
390
391 memset(&texTemp, 0, sizeof(texTemp));
392 texTemp.target = PIPE_TEXTURE_2D;
393 texTemp.format = PIPE_FORMAT_A8_UNORM; /* XXX verify supported by driver! */
394 texTemp.last_level = 0;
395 texTemp.width0 = 32;
396 texTemp.height0 = 32;
397 texTemp.depth0 = 1;
398 texTemp.array_size = 1;
399 texTemp.bind = PIPE_BIND_SAMPLER_VIEW;
400
401 pstip->texture = screen->resource_create(screen, &texTemp);
402 if (pstip->texture == NULL)
403 return FALSE;
404
405 u_sampler_view_default_template(&viewTempl,
406 pstip->texture,
407 pstip->texture->format);
408 pstip->sampler_view = pipe->create_sampler_view(pipe,
409 pstip->texture,
410 &viewTempl);
411 if (!pstip->sampler_view) {
412 return FALSE;
413 }
414
415 return TRUE;
416 }
417
418
419 /**
420 * Create the sampler CSO that'll be used for stippling.
421 */
422 static boolean
423 pstip_create_sampler(struct pstip_stage *pstip)
424 {
425 struct pipe_sampler_state sampler;
426 struct pipe_context *pipe = pstip->pipe;
427
428 memset(&sampler, 0, sizeof(sampler));
429 sampler.wrap_s = PIPE_TEX_WRAP_REPEAT;
430 sampler.wrap_t = PIPE_TEX_WRAP_REPEAT;
431 sampler.wrap_r = PIPE_TEX_WRAP_REPEAT;
432 sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
433 sampler.min_img_filter = PIPE_TEX_FILTER_NEAREST;
434 sampler.mag_img_filter = PIPE_TEX_FILTER_NEAREST;
435 sampler.normalized_coords = 1;
436 sampler.min_lod = 0.0f;
437 sampler.max_lod = 0.0f;
438
439 pstip->sampler_cso = pipe->create_sampler_state(pipe, &sampler);
440 if (pstip->sampler_cso == NULL)
441 return FALSE;
442
443 return TRUE;
444 }
445
446
447 /**
448 * When we're about to draw our first stipple polygon in a batch, this function
449 * is called to tell the driver to bind our modified fragment shader.
450 */
451 static boolean
452 bind_pstip_fragment_shader(struct pstip_stage *pstip)
453 {
454 struct draw_context *draw = pstip->stage.draw;
455 if (!pstip->fs->pstip_fs &&
456 !generate_pstip_fs(pstip))
457 return FALSE;
458
459 draw->suspend_flushing = TRUE;
460 pstip->driver_bind_fs_state(pstip->pipe, pstip->fs->pstip_fs);
461 draw->suspend_flushing = FALSE;
462 return TRUE;
463 }
464
465
466 static inline struct pstip_stage *
467 pstip_stage( struct draw_stage *stage )
468 {
469 return (struct pstip_stage *) stage;
470 }
471
472
473 static void
474 pstip_first_tri(struct draw_stage *stage, struct prim_header *header)
475 {
476 struct pstip_stage *pstip = pstip_stage(stage);
477 struct pipe_context *pipe = pstip->pipe;
478 struct draw_context *draw = stage->draw;
479 uint num_samplers;
480 uint num_sampler_views;
481
482 assert(stage->draw->rasterizer->poly_stipple_enable);
483
484 /* bind our fragprog */
485 if (!bind_pstip_fragment_shader(pstip)) {
486 stage->tri = draw_pipe_passthrough_tri;
487 stage->tri(stage, header);
488 return;
489 }
490
491
492 /* how many samplers? */
493 /* we'll use sampler/texture[pstip->sampler_unit] for the stipple */
494 num_samplers = MAX2(pstip->num_samplers, pstip->fs->sampler_unit + 1);
495 num_sampler_views = MAX2(pstip->num_sampler_views, num_samplers);
496
497 /* plug in our sampler, texture */
498 pstip->state.samplers[pstip->fs->sampler_unit] = pstip->sampler_cso;
499 pipe_sampler_view_reference(&pstip->state.sampler_views[pstip->fs->sampler_unit],
500 pstip->sampler_view);
501
502 assert(num_samplers <= PIPE_MAX_SAMPLERS);
503
504 draw->suspend_flushing = TRUE;
505
506 pstip->driver_bind_sampler_states(pipe, PIPE_SHADER_FRAGMENT, 0,
507 num_samplers, pstip->state.samplers);
508
509 pstip->driver_set_sampler_views(pipe, PIPE_SHADER_FRAGMENT, 0,
510 num_sampler_views, pstip->state.sampler_views);
511
512 draw->suspend_flushing = FALSE;
513
514 /* now really draw first triangle */
515 stage->tri = draw_pipe_passthrough_tri;
516 stage->tri(stage, header);
517 }
518
519
520 static void
521 pstip_flush(struct draw_stage *stage, unsigned flags)
522 {
523 struct draw_context *draw = stage->draw;
524 struct pstip_stage *pstip = pstip_stage(stage);
525 struct pipe_context *pipe = pstip->pipe;
526
527 stage->tri = pstip_first_tri;
528 stage->next->flush( stage->next, flags );
529
530 /* restore original frag shader, texture, sampler state */
531 draw->suspend_flushing = TRUE;
532 pstip->driver_bind_fs_state(pipe, pstip->fs ? pstip->fs->driver_fs : NULL);
533
534 pstip->driver_bind_sampler_states(pipe, PIPE_SHADER_FRAGMENT, 0,
535 pstip->num_samplers,
536 pstip->state.samplers);
537
538 pstip->driver_set_sampler_views(pipe, PIPE_SHADER_FRAGMENT, 0,
539 pstip->num_sampler_views,
540 pstip->state.sampler_views);
541
542 draw->suspend_flushing = FALSE;
543 }
544
545
546 static void
547 pstip_reset_stipple_counter(struct draw_stage *stage)
548 {
549 stage->next->reset_stipple_counter( stage->next );
550 }
551
552
553 static void
554 pstip_destroy(struct draw_stage *stage)
555 {
556 struct pstip_stage *pstip = pstip_stage(stage);
557 uint i;
558
559 for (i = 0; i < PIPE_MAX_SHADER_SAMPLER_VIEWS; i++) {
560 pipe_sampler_view_reference(&pstip->state.sampler_views[i], NULL);
561 }
562
563 pstip->pipe->delete_sampler_state(pstip->pipe, pstip->sampler_cso);
564
565 pipe_resource_reference(&pstip->texture, NULL);
566
567 if (pstip->sampler_view) {
568 pipe_sampler_view_reference(&pstip->sampler_view, NULL);
569 }
570
571 draw_free_temp_verts( stage );
572 FREE( stage );
573 }
574
575
576 /** Create a new polygon stipple drawing stage object */
577 static struct pstip_stage *
578 draw_pstip_stage(struct draw_context *draw, struct pipe_context *pipe)
579 {
580 struct pstip_stage *pstip = CALLOC_STRUCT(pstip_stage);
581 if (!pstip)
582 goto fail;
583
584 pstip->pipe = pipe;
585
586 pstip->stage.draw = draw;
587 pstip->stage.name = "pstip";
588 pstip->stage.next = NULL;
589 pstip->stage.point = draw_pipe_passthrough_point;
590 pstip->stage.line = draw_pipe_passthrough_line;
591 pstip->stage.tri = pstip_first_tri;
592 pstip->stage.flush = pstip_flush;
593 pstip->stage.reset_stipple_counter = pstip_reset_stipple_counter;
594 pstip->stage.destroy = pstip_destroy;
595
596 if (!draw_alloc_temp_verts( &pstip->stage, 8 ))
597 goto fail;
598
599 return pstip;
600
601 fail:
602 if (pstip)
603 pstip->stage.destroy( &pstip->stage );
604
605 return NULL;
606 }
607
608
609 static struct pstip_stage *
610 pstip_stage_from_pipe(struct pipe_context *pipe)
611 {
612 struct draw_context *draw = (struct draw_context *) pipe->draw;
613 return pstip_stage(draw->pipeline.pstipple);
614 }
615
616
617 /**
618 * This function overrides the driver's create_fs_state() function and
619 * will typically be called by the state tracker.
620 */
621 static void *
622 pstip_create_fs_state(struct pipe_context *pipe,
623 const struct pipe_shader_state *fs)
624 {
625 struct pstip_stage *pstip = pstip_stage_from_pipe(pipe);
626 struct pstip_fragment_shader *pstipfs = CALLOC_STRUCT(pstip_fragment_shader);
627
628 if (pstipfs) {
629 pstipfs->state.tokens = tgsi_dup_tokens(fs->tokens);
630
631 /* pass-through */
632 pstipfs->driver_fs = pstip->driver_create_fs_state(pstip->pipe, fs);
633 }
634
635 return pstipfs;
636 }
637
638
639 static void
640 pstip_bind_fs_state(struct pipe_context *pipe, void *fs)
641 {
642 struct pstip_stage *pstip = pstip_stage_from_pipe(pipe);
643 struct pstip_fragment_shader *pstipfs = (struct pstip_fragment_shader *) fs;
644 /* save current */
645 pstip->fs = pstipfs;
646 /* pass-through */
647 pstip->driver_bind_fs_state(pstip->pipe,
648 (pstipfs ? pstipfs->driver_fs : NULL));
649 }
650
651
652 static void
653 pstip_delete_fs_state(struct pipe_context *pipe, void *fs)
654 {
655 struct pstip_stage *pstip = pstip_stage_from_pipe(pipe);
656 struct pstip_fragment_shader *pstipfs = (struct pstip_fragment_shader *) fs;
657 /* pass-through */
658 pstip->driver_delete_fs_state(pstip->pipe, pstipfs->driver_fs);
659
660 if (pstipfs->pstip_fs)
661 pstip->driver_delete_fs_state(pstip->pipe, pstipfs->pstip_fs);
662
663 FREE((void*)pstipfs->state.tokens);
664 FREE(pstipfs);
665 }
666
667
668 static void
669 pstip_bind_sampler_states(struct pipe_context *pipe, unsigned shader,
670 unsigned start, unsigned num, void **sampler)
671 {
672 struct pstip_stage *pstip = pstip_stage_from_pipe(pipe);
673 uint i;
674
675 assert(start == 0);
676
677 if (shader == PIPE_SHADER_FRAGMENT) {
678 /* save current */
679 memcpy(pstip->state.samplers, sampler, num * sizeof(void *));
680 for (i = num; i < PIPE_MAX_SAMPLERS; i++) {
681 pstip->state.samplers[i] = NULL;
682 }
683 pstip->num_samplers = num;
684 }
685
686 /* pass-through */
687 pstip->driver_bind_sampler_states(pstip->pipe, shader, start, num, sampler);
688 }
689
690
691 static void
692 pstip_set_sampler_views(struct pipe_context *pipe,
693 unsigned shader, unsigned start, unsigned num,
694 struct pipe_sampler_view **views)
695 {
696 struct pstip_stage *pstip = pstip_stage_from_pipe(pipe);
697 uint i;
698
699 if (shader == PIPE_SHADER_FRAGMENT) {
700 /* save current */
701 for (i = 0; i < num; i++) {
702 pipe_sampler_view_reference(&pstip->state.sampler_views[start + i],
703 views[i]);
704 }
705 pstip->num_sampler_views = num;
706 }
707
708 /* pass-through */
709 pstip->driver_set_sampler_views(pstip->pipe, shader, start, num, views);
710 }
711
712
713 static void
714 pstip_set_polygon_stipple(struct pipe_context *pipe,
715 const struct pipe_poly_stipple *stipple)
716 {
717 struct pstip_stage *pstip = pstip_stage_from_pipe(pipe);
718
719 /* save current */
720 pstip->state.stipple = stipple;
721
722 /* pass-through */
723 pstip->driver_set_polygon_stipple(pstip->pipe, stipple);
724
725 pstip_update_texture(pstip);
726 }
727
728
729 /**
730 * Called by drivers that want to install this polygon stipple stage
731 * into the draw module's pipeline. This will not be used if the
732 * hardware has native support for polygon stipple.
733 */
734 boolean
735 draw_install_pstipple_stage(struct draw_context *draw,
736 struct pipe_context *pipe)
737 {
738 struct pstip_stage *pstip;
739
740 pipe->draw = (void *) draw;
741
742 /*
743 * Create / install pgon stipple drawing / prim stage
744 */
745 pstip = draw_pstip_stage( draw, pipe );
746 if (!pstip)
747 goto fail;
748
749 draw->pipeline.pstipple = &pstip->stage;
750
751 /* save original driver functions */
752 pstip->driver_create_fs_state = pipe->create_fs_state;
753 pstip->driver_bind_fs_state = pipe->bind_fs_state;
754 pstip->driver_delete_fs_state = pipe->delete_fs_state;
755
756 pstip->driver_bind_sampler_states = pipe->bind_sampler_states;
757 pstip->driver_set_sampler_views = pipe->set_sampler_views;
758 pstip->driver_set_polygon_stipple = pipe->set_polygon_stipple;
759
760 /* create special texture, sampler state */
761 if (!pstip_create_texture(pstip))
762 goto fail;
763
764 if (!pstip_create_sampler(pstip))
765 goto fail;
766
767 /* override the driver's functions */
768 pipe->create_fs_state = pstip_create_fs_state;
769 pipe->bind_fs_state = pstip_bind_fs_state;
770 pipe->delete_fs_state = pstip_delete_fs_state;
771
772 pipe->bind_sampler_states = pstip_bind_sampler_states;
773 pipe->set_sampler_views = pstip_set_sampler_views;
774 pipe->set_polygon_stipple = pstip_set_polygon_stipple;
775
776 return TRUE;
777
778 fail:
779 if (pstip)
780 pstip->stage.destroy( &pstip->stage );
781
782 return FALSE;
783 }