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