st/mesa: fix glCopyPixels bugs/crashes when src region need clipping
[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 #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
46 #include "tgsi/tgsi_transform.h"
47 #include "tgsi/tgsi_dump.h"
48
49 #include "draw_context.h"
50 #include "draw_pipe.h"
51
52
53 /** Approx number of new tokens for instructions in pstip_transform_inst() */
54 #define NUM_NEW_TOKENS 50
55
56
57 /**
58 * Subclass of pipe_shader_state to carry extra fragment shader info.
59 */
60 struct pstip_fragment_shader
61 {
62 struct pipe_shader_state state;
63 void *driver_fs;
64 void *pstip_fs;
65 uint sampler_unit;
66 };
67
68
69 /**
70 * Subclass of draw_stage
71 */
72 struct pstip_stage
73 {
74 struct draw_stage stage;
75
76 void *sampler_cso;
77 struct pipe_texture *texture;
78 uint num_samplers;
79 uint num_textures;
80
81 /*
82 * Currently bound state
83 */
84 struct pstip_fragment_shader *fs;
85 struct {
86 void *samplers[PIPE_MAX_SAMPLERS];
87 struct pipe_texture *textures[PIPE_MAX_SAMPLERS];
88 const struct pipe_poly_stipple *stipple;
89 } state;
90
91 /*
92 * Driver interface/override functions
93 */
94 void * (*driver_create_fs_state)(struct pipe_context *,
95 const struct pipe_shader_state *);
96 void (*driver_bind_fs_state)(struct pipe_context *, void *);
97 void (*driver_delete_fs_state)(struct pipe_context *, void *);
98
99 void (*driver_bind_sampler_states)(struct pipe_context *, unsigned, void **);
100
101 void (*driver_set_sampler_textures)(struct pipe_context *, unsigned,
102 struct pipe_texture **);
103
104 void (*driver_set_polygon_stipple)(struct pipe_context *,
105 const struct pipe_poly_stipple *);
106
107 struct pipe_context *pipe;
108 };
109
110
111
112 /**
113 * Subclass of tgsi_transform_context, used for transforming the
114 * user's fragment shader to add the special AA instructions.
115 */
116 struct pstip_transform_context {
117 struct tgsi_transform_context base;
118 uint tempsUsed; /**< bitmask */
119 int wincoordInput;
120 int maxInput;
121 uint samplersUsed; /**< bitfield of samplers used */
122 int freeSampler; /** an available sampler for the pstipple */
123 int texTemp; /**< temp registers */
124 int numImmed;
125 boolean firstInstruction;
126 };
127
128
129 /**
130 * TGSI declaration transform callback.
131 * Look for a free sampler, a free input attrib, and two free temp regs.
132 */
133 static void
134 pstip_transform_decl(struct tgsi_transform_context *ctx,
135 struct tgsi_full_declaration *decl)
136 {
137 struct pstip_transform_context *pctx = (struct pstip_transform_context *) ctx;
138
139 if (decl->Declaration.File == TGSI_FILE_SAMPLER) {
140 uint i;
141 for (i = decl->Range.First;
142 i <= decl->Range.Last; i++) {
143 pctx->samplersUsed |= 1 << i;
144 }
145 }
146 else if (decl->Declaration.File == TGSI_FILE_INPUT) {
147 pctx->maxInput = MAX2(pctx->maxInput, (int) decl->Range.Last);
148 if (decl->Semantic.Name == TGSI_SEMANTIC_POSITION)
149 pctx->wincoordInput = (int) decl->Range.First;
150 }
151 else if (decl->Declaration.File == TGSI_FILE_TEMPORARY) {
152 uint i;
153 for (i = decl->Range.First;
154 i <= decl->Range.Last; i++) {
155 pctx->tempsUsed |= (1 << i);
156 }
157 }
158
159 ctx->emit_declaration(ctx, decl);
160 }
161
162
163 static void
164 pstip_transform_immed(struct tgsi_transform_context *ctx,
165 struct tgsi_full_immediate *immed)
166 {
167 struct pstip_transform_context *pctx = (struct pstip_transform_context *) ctx;
168 pctx->numImmed++;
169 }
170
171
172 /**
173 * Find the lowest zero bit in the given word, or -1 if bitfield is all ones.
174 */
175 static int
176 free_bit(uint bitfield)
177 {
178 return ffs(~bitfield) - 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 const uint newLen = tgsi_num_tokens(orig_fs->tokens) + NUM_NEW_TOKENS;
335
336 pstip_fs = *orig_fs; /* copy to init */
337 pstip_fs.tokens = tgsi_alloc_tokens(newLen);
338 if (pstip_fs.tokens == NULL)
339 return FALSE;
340
341 memset(&transform, 0, sizeof(transform));
342 transform.wincoordInput = -1;
343 transform.maxInput = -1;
344 transform.texTemp = -1;
345 transform.firstInstruction = TRUE;
346 transform.base.transform_instruction = pstip_transform_inst;
347 transform.base.transform_declaration = pstip_transform_decl;
348 transform.base.transform_immediate = pstip_transform_immed;
349
350 tgsi_transform_shader(orig_fs->tokens,
351 (struct tgsi_token *) pstip_fs.tokens,
352 newLen, &transform.base);
353
354 #if 0 /* DEBUG */
355 tgsi_dump(orig_fs->tokens, 0);
356 tgsi_dump(pstip_fs.tokens, 0);
357 #endif
358
359 pstip->fs->sampler_unit = transform.freeSampler;
360 assert(pstip->fs->sampler_unit < PIPE_MAX_SAMPLERS);
361
362 pstip->fs->pstip_fs = pstip->driver_create_fs_state(pstip->pipe, &pstip_fs);
363
364 FREE((void *)pstip_fs.tokens);
365 return TRUE;
366 }
367
368
369 /**
370 * Load texture image with current stipple pattern.
371 */
372 static void
373 pstip_update_texture(struct pstip_stage *pstip)
374 {
375 static const uint bit31 = 1 << 31;
376 struct pipe_context *pipe = pstip->pipe;
377 struct pipe_screen *screen = pipe->screen;
378 struct pipe_transfer *transfer;
379 const uint *stipple = pstip->state.stipple->stipple;
380 uint i, j;
381 ubyte *data;
382
383 /* XXX: want to avoid flushing just because we use stipple:
384 */
385 pipe->flush( pipe, PIPE_FLUSH_TEXTURE_CACHE, NULL );
386
387 transfer = screen->get_tex_transfer(screen, pstip->texture, 0, 0, 0,
388 PIPE_TRANSFER_WRITE, 0, 0, 32, 32);
389 data = screen->transfer_map(screen, transfer);
390
391 /*
392 * Load alpha texture.
393 * Note: 0 means keep the fragment, 255 means kill it.
394 * We'll negate the texel value and use KILP which kills if value
395 * is negative.
396 */
397 for (i = 0; i < 32; i++) {
398 for (j = 0; j < 32; j++) {
399 if (stipple[i] & (bit31 >> j)) {
400 /* fragment "on" */
401 data[i * transfer->stride + j] = 0;
402 }
403 else {
404 /* fragment "off" */
405 data[i * transfer->stride + j] = 255;
406 }
407 }
408 }
409
410 /* unmap */
411 screen->transfer_unmap(screen, transfer);
412 screen->tex_transfer_destroy(transfer);
413 }
414
415
416 /**
417 * Create the texture map we'll use for stippling.
418 */
419 static boolean
420 pstip_create_texture(struct pstip_stage *pstip)
421 {
422 struct pipe_context *pipe = pstip->pipe;
423 struct pipe_screen *screen = pipe->screen;
424 struct pipe_texture texTemp;
425
426 memset(&texTemp, 0, sizeof(texTemp));
427 texTemp.target = PIPE_TEXTURE_2D;
428 texTemp.format = PIPE_FORMAT_A8_UNORM; /* XXX verify supported by driver! */
429 texTemp.last_level = 0;
430 texTemp.width0 = 32;
431 texTemp.height0 = 32;
432 texTemp.depth0 = 1;
433
434 pstip->texture = screen->texture_create(screen, &texTemp);
435 if (pstip->texture == NULL)
436 return FALSE;
437
438 return TRUE;
439 }
440
441
442 /**
443 * Create the sampler CSO that'll be used for stippling.
444 */
445 static boolean
446 pstip_create_sampler(struct pstip_stage *pstip)
447 {
448 struct pipe_sampler_state sampler;
449 struct pipe_context *pipe = pstip->pipe;
450
451 memset(&sampler, 0, sizeof(sampler));
452 sampler.wrap_s = PIPE_TEX_WRAP_REPEAT;
453 sampler.wrap_t = PIPE_TEX_WRAP_REPEAT;
454 sampler.wrap_r = PIPE_TEX_WRAP_REPEAT;
455 sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
456 sampler.min_img_filter = PIPE_TEX_FILTER_NEAREST;
457 sampler.mag_img_filter = PIPE_TEX_FILTER_NEAREST;
458 sampler.normalized_coords = 1;
459 sampler.min_lod = 0.0f;
460 sampler.max_lod = 0.0f;
461
462 pstip->sampler_cso = pipe->create_sampler_state(pipe, &sampler);
463 if (pstip->sampler_cso == NULL)
464 return FALSE;
465
466 return TRUE;
467 }
468
469
470 /**
471 * When we're about to draw our first stipple polygon in a batch, this function
472 * is called to tell the driver to bind our modified fragment shader.
473 */
474 static boolean
475 bind_pstip_fragment_shader(struct pstip_stage *pstip)
476 {
477 struct draw_context *draw = pstip->stage.draw;
478 if (!pstip->fs->pstip_fs &&
479 !generate_pstip_fs(pstip))
480 return FALSE;
481
482 draw->suspend_flushing = TRUE;
483 pstip->driver_bind_fs_state(pstip->pipe, pstip->fs->pstip_fs);
484 draw->suspend_flushing = FALSE;
485 return TRUE;
486 }
487
488
489 static INLINE struct pstip_stage *
490 pstip_stage( struct draw_stage *stage )
491 {
492 return (struct pstip_stage *) stage;
493 }
494
495
496 static void
497 pstip_first_tri(struct draw_stage *stage, struct prim_header *header)
498 {
499 struct pstip_stage *pstip = pstip_stage(stage);
500 struct pipe_context *pipe = pstip->pipe;
501 struct draw_context *draw = stage->draw;
502 uint num_samplers;
503
504 assert(stage->draw->rasterizer->poly_stipple_enable);
505
506 /* bind our fragprog */
507 if (!bind_pstip_fragment_shader(pstip)) {
508 stage->tri = draw_pipe_passthrough_tri;
509 stage->tri(stage, header);
510 return;
511 }
512
513
514 /* how many samplers? */
515 /* we'll use sampler/texture[pstip->sampler_unit] for the stipple */
516 num_samplers = MAX2(pstip->num_textures, pstip->num_samplers);
517 num_samplers = MAX2(num_samplers, pstip->fs->sampler_unit + 1);
518
519 /* plug in our sampler, texture */
520 pstip->state.samplers[pstip->fs->sampler_unit] = pstip->sampler_cso;
521 pipe_texture_reference(&pstip->state.textures[pstip->fs->sampler_unit],
522 pstip->texture);
523
524 assert(num_samplers <= PIPE_MAX_SAMPLERS);
525
526 draw->suspend_flushing = TRUE;
527 pstip->driver_bind_sampler_states(pipe, num_samplers, pstip->state.samplers);
528 pstip->driver_set_sampler_textures(pipe, num_samplers, pstip->state.textures);
529 draw->suspend_flushing = FALSE;
530
531 /* now really draw first triangle */
532 stage->tri = draw_pipe_passthrough_tri;
533 stage->tri(stage, header);
534 }
535
536
537 static void
538 pstip_flush(struct draw_stage *stage, unsigned flags)
539 {
540 struct draw_context *draw = stage->draw;
541 struct pstip_stage *pstip = pstip_stage(stage);
542 struct pipe_context *pipe = pstip->pipe;
543
544 stage->tri = pstip_first_tri;
545 stage->next->flush( stage->next, flags );
546
547 /* restore original frag shader, texture, sampler state */
548 draw->suspend_flushing = TRUE;
549 pstip->driver_bind_fs_state(pipe, pstip->fs->driver_fs);
550 pstip->driver_bind_sampler_states(pipe, pstip->num_samplers,
551 pstip->state.samplers);
552 pstip->driver_set_sampler_textures(pipe, pstip->num_textures,
553 pstip->state.textures);
554 draw->suspend_flushing = FALSE;
555 }
556
557
558 static void
559 pstip_reset_stipple_counter(struct draw_stage *stage)
560 {
561 stage->next->reset_stipple_counter( stage->next );
562 }
563
564
565 static void
566 pstip_destroy(struct draw_stage *stage)
567 {
568 struct pstip_stage *pstip = pstip_stage(stage);
569 uint i;
570
571 for (i = 0; i < PIPE_MAX_SAMPLERS; i++) {
572 pipe_texture_reference(&pstip->state.textures[i], NULL);
573 }
574
575 pstip->pipe->delete_sampler_state(pstip->pipe, pstip->sampler_cso);
576
577 pipe_texture_reference(&pstip->texture, NULL);
578
579 draw_free_temp_verts( stage );
580 FREE( stage );
581 }
582
583
584 static struct pstip_stage *
585 draw_pstip_stage(struct draw_context *draw)
586 {
587 struct pstip_stage *pstip = CALLOC_STRUCT(pstip_stage);
588
589 draw_alloc_temp_verts( &pstip->stage, 8 );
590
591 pstip->stage.draw = draw;
592 pstip->stage.name = "pstip";
593 pstip->stage.next = NULL;
594 pstip->stage.point = draw_pipe_passthrough_point;
595 pstip->stage.line = draw_pipe_passthrough_line;
596 pstip->stage.tri = pstip_first_tri;
597 pstip->stage.flush = pstip_flush;
598 pstip->stage.reset_stipple_counter = pstip_reset_stipple_counter;
599 pstip->stage.destroy = pstip_destroy;
600
601 return pstip;
602 }
603
604
605 static struct pstip_stage *
606 pstip_stage_from_pipe(struct pipe_context *pipe)
607 {
608 struct draw_context *draw = (struct draw_context *) pipe->draw;
609 return pstip_stage(draw->pipeline.pstipple);
610 }
611
612
613 /**
614 * This function overrides the driver's create_fs_state() function and
615 * will typically be called by the state tracker.
616 */
617 static void *
618 pstip_create_fs_state(struct pipe_context *pipe,
619 const struct pipe_shader_state *fs)
620 {
621 struct pstip_stage *pstip = pstip_stage_from_pipe(pipe);
622 struct pstip_fragment_shader *aafs = CALLOC_STRUCT(pstip_fragment_shader);
623
624 if (aafs) {
625 aafs->state = *fs;
626
627 /* pass-through */
628 aafs->driver_fs = pstip->driver_create_fs_state(pstip->pipe, fs);
629 }
630
631 return aafs;
632 }
633
634
635 static void
636 pstip_bind_fs_state(struct pipe_context *pipe, void *fs)
637 {
638 struct pstip_stage *pstip = pstip_stage_from_pipe(pipe);
639 struct pstip_fragment_shader *aafs = (struct pstip_fragment_shader *) fs;
640 /* save current */
641 pstip->fs = aafs;
642 /* pass-through */
643 pstip->driver_bind_fs_state(pstip->pipe,
644 (aafs ? aafs->driver_fs : NULL));
645 }
646
647
648 static void
649 pstip_delete_fs_state(struct pipe_context *pipe, void *fs)
650 {
651 struct pstip_stage *pstip = pstip_stage_from_pipe(pipe);
652 struct pstip_fragment_shader *aafs = (struct pstip_fragment_shader *) fs;
653 /* pass-through */
654 pstip->driver_delete_fs_state(pstip->pipe, aafs->driver_fs);
655
656 if (aafs->pstip_fs)
657 pstip->driver_delete_fs_state(pstip->pipe, aafs->pstip_fs);
658
659 FREE(aafs);
660 }
661
662
663 static void
664 pstip_bind_sampler_states(struct pipe_context *pipe,
665 unsigned num, void **sampler)
666 {
667 struct pstip_stage *pstip = pstip_stage_from_pipe(pipe);
668 uint i;
669
670 /* save current */
671 memcpy(pstip->state.samplers, sampler, num * sizeof(void *));
672 for (i = num; i < PIPE_MAX_SAMPLERS; i++) {
673 pstip->state.samplers[i] = NULL;
674 }
675
676 pstip->num_samplers = num;
677 /* pass-through */
678 pstip->driver_bind_sampler_states(pstip->pipe, num, sampler);
679 }
680
681
682 static void
683 pstip_set_sampler_textures(struct pipe_context *pipe,
684 unsigned num, struct pipe_texture **texture)
685 {
686 struct pstip_stage *pstip = pstip_stage_from_pipe(pipe);
687 uint i;
688
689 /* save current */
690 for (i = 0; i < num; i++) {
691 pipe_texture_reference(&pstip->state.textures[i], texture[i]);
692 }
693 for (; i < PIPE_MAX_SAMPLERS; i++) {
694 pipe_texture_reference(&pstip->state.textures[i], NULL);
695 }
696
697 pstip->num_textures = num;
698
699 /* pass-through */
700 pstip->driver_set_sampler_textures(pstip->pipe, num, texture);
701 }
702
703
704 static void
705 pstip_set_polygon_stipple(struct pipe_context *pipe,
706 const struct pipe_poly_stipple *stipple)
707 {
708 struct pstip_stage *pstip = pstip_stage_from_pipe(pipe);
709
710 /* save current */
711 pstip->state.stipple = stipple;
712
713 /* pass-through */
714 pstip->driver_set_polygon_stipple(pstip->pipe, stipple);
715
716 pstip_update_texture(pstip);
717 }
718
719
720 /**
721 * Called by drivers that want to install this polygon stipple stage
722 * into the draw module's pipeline. This will not be used if the
723 * hardware has native support for polygon stipple.
724 */
725 boolean
726 draw_install_pstipple_stage(struct draw_context *draw,
727 struct pipe_context *pipe)
728 {
729 struct pstip_stage *pstip;
730
731 pipe->draw = (void *) draw;
732
733 /*
734 * Create / install pgon stipple drawing / prim stage
735 */
736 pstip = draw_pstip_stage( draw );
737 if (pstip == NULL)
738 goto fail;
739
740 draw->pipeline.pstipple = &pstip->stage;
741
742 pstip->pipe = pipe;
743
744 /* create special texture, sampler state */
745 if (!pstip_create_texture(pstip))
746 goto fail;
747
748 if (!pstip_create_sampler(pstip))
749 goto fail;
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_fragment_sampler_states;
757 pstip->driver_set_sampler_textures = pipe->set_fragment_sampler_textures;
758 pstip->driver_set_polygon_stipple = pipe->set_polygon_stipple;
759
760 /* override the driver's functions */
761 pipe->create_fs_state = pstip_create_fs_state;
762 pipe->bind_fs_state = pstip_bind_fs_state;
763 pipe->delete_fs_state = pstip_delete_fs_state;
764
765 pipe->bind_fragment_sampler_states = pstip_bind_sampler_states;
766 pipe->set_fragment_sampler_textures = pstip_set_sampler_textures;
767 pipe->set_polygon_stipple = pstip_set_polygon_stipple;
768
769 return TRUE;
770
771 fail:
772 if (pstip)
773 pstip->stage.destroy( &pstip->stage );
774
775 return FALSE;
776 }