Merge remote branch 'main/master' into radeon-rewrite
[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->DeclarationRange.First;
137 i <= decl->DeclarationRange.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->DeclarationRange.Last);
143 if (decl->Semantic.SemanticName == TGSI_SEMANTIC_POSITION)
144 pctx->wincoordInput = (int) decl->DeclarationRange.First;
145 }
146 else if (decl->Declaration.File == TGSI_FILE_TEMPORARY) {
147 uint i;
148 for (i = decl->DeclarationRange.First;
149 i <= decl->DeclarationRange.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.SemanticName = TGSI_SEMANTIC_POSITION;
230 decl.Semantic.SemanticIndex = 0;
231 decl.DeclarationRange.First =
232 decl.DeclarationRange.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.DeclarationRange.First =
240 decl.DeclarationRange.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.DeclarationRange.First =
247 decl.DeclarationRange.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.Pointer = (void *) value;
260 ctx->emit_immediate(ctx, &immed);
261 }
262
263 pctx->firstInstruction = FALSE;
264
265
266 /*
267 * Insert new MUL/TEX/KILP instructions at start of program
268 * Take gl_FragCoord, divide by 32 (stipple size), sample the
269 * texture and kill fragment if needed.
270 *
271 * We'd like to use non-normalized texcoords to index into a RECT
272 * texture, but we can only use GL_REPEAT wrap mode with normalized
273 * texcoords. Darn.
274 */
275
276 /* MUL texTemp, INPUT[wincoord], 1/32; */
277 newInst = tgsi_default_full_instruction();
278 newInst.Instruction.Opcode = TGSI_OPCODE_MUL;
279 newInst.Instruction.NumDstRegs = 1;
280 newInst.FullDstRegisters[0].DstRegister.File = TGSI_FILE_TEMPORARY;
281 newInst.FullDstRegisters[0].DstRegister.Index = pctx->texTemp;
282 newInst.Instruction.NumSrcRegs = 2;
283 newInst.FullSrcRegisters[0].SrcRegister.File = TGSI_FILE_INPUT;
284 newInst.FullSrcRegisters[0].SrcRegister.Index = wincoordInput;
285 newInst.FullSrcRegisters[1].SrcRegister.File = TGSI_FILE_IMMEDIATE;
286 newInst.FullSrcRegisters[1].SrcRegister.Index = pctx->numImmed;
287 ctx->emit_instruction(ctx, &newInst);
288
289 /* TEX texTemp, texTemp, sampler; */
290 newInst = tgsi_default_full_instruction();
291 newInst.Instruction.Opcode = TGSI_OPCODE_TEX;
292 newInst.Instruction.NumDstRegs = 1;
293 newInst.FullDstRegisters[0].DstRegister.File = TGSI_FILE_TEMPORARY;
294 newInst.FullDstRegisters[0].DstRegister.Index = pctx->texTemp;
295 newInst.Instruction.NumSrcRegs = 2;
296 newInst.InstructionExtTexture.Texture = TGSI_TEXTURE_2D;
297 newInst.FullSrcRegisters[0].SrcRegister.File = TGSI_FILE_TEMPORARY;
298 newInst.FullSrcRegisters[0].SrcRegister.Index = pctx->texTemp;
299 newInst.FullSrcRegisters[1].SrcRegister.File = TGSI_FILE_SAMPLER;
300 newInst.FullSrcRegisters[1].SrcRegister.Index = pctx->freeSampler;
301 ctx->emit_instruction(ctx, &newInst);
302
303 /* KIL -texTemp; # if -texTemp < 0, KILL fragment */
304 newInst = tgsi_default_full_instruction();
305 newInst.Instruction.Opcode = TGSI_OPCODE_KIL;
306 newInst.Instruction.NumDstRegs = 0;
307 newInst.Instruction.NumSrcRegs = 1;
308 newInst.FullSrcRegisters[0].SrcRegister.File = TGSI_FILE_TEMPORARY;
309 newInst.FullSrcRegisters[0].SrcRegister.Index = pctx->texTemp;
310 newInst.FullSrcRegisters[0].SrcRegister.Negate = 1;
311 ctx->emit_instruction(ctx, &newInst);
312 }
313
314 /* emit this instruction */
315 ctx->emit_instruction(ctx, inst);
316 }
317
318
319 /**
320 * Generate the frag shader we'll use for doing polygon stipple.
321 * This will be the user's shader prefixed with a TEX and KIL instruction.
322 */
323 static boolean
324 generate_pstip_fs(struct pstip_stage *pstip)
325 {
326 const struct pipe_shader_state *orig_fs = &pstip->fs->state;
327 /*struct draw_context *draw = pstip->stage.draw;*/
328 struct pipe_shader_state pstip_fs;
329 struct pstip_transform_context transform;
330
331 #define MAX 1000
332
333 pstip_fs = *orig_fs; /* copy to init */
334 pstip_fs.tokens = MALLOC(sizeof(struct tgsi_token) * MAX);
335 if (pstip_fs.tokens == NULL)
336 return FALSE;
337
338 memset(&transform, 0, sizeof(transform));
339 transform.wincoordInput = -1;
340 transform.maxInput = -1;
341 transform.texTemp = -1;
342 transform.firstInstruction = TRUE;
343 transform.base.transform_instruction = pstip_transform_inst;
344 transform.base.transform_declaration = pstip_transform_decl;
345 transform.base.transform_immediate = pstip_transform_immed;
346
347 tgsi_transform_shader(orig_fs->tokens,
348 (struct tgsi_token *) pstip_fs.tokens,
349 MAX, &transform.base);
350
351 #if 0 /* DEBUG */
352 tgsi_dump(orig_fs->tokens, 0);
353 tgsi_dump(pstip_fs.tokens, 0);
354 #endif
355
356 pstip->fs->sampler_unit = transform.freeSampler;
357 assert(pstip->fs->sampler_unit < PIPE_MAX_SAMPLERS);
358
359 pstip->fs->pstip_fs = pstip->driver_create_fs_state(pstip->pipe, &pstip_fs);
360
361 return TRUE;
362 }
363
364
365 /**
366 * Load texture image with current stipple pattern.
367 */
368 static void
369 pstip_update_texture(struct pstip_stage *pstip)
370 {
371 static const uint bit31 = 1 << 31;
372 struct pipe_context *pipe = pstip->pipe;
373 struct pipe_screen *screen = pipe->screen;
374 struct pipe_transfer *transfer;
375 const uint *stipple = pstip->state.stipple->stipple;
376 uint i, j;
377 ubyte *data;
378
379 /* XXX: want to avoid flushing just because we use stipple:
380 */
381 pipe->flush( pipe, PIPE_FLUSH_TEXTURE_CACHE, NULL );
382
383 transfer = screen->get_tex_transfer(screen, pstip->texture, 0, 0, 0,
384 PIPE_TRANSFER_WRITE, 0, 0, 32, 32);
385 data = screen->transfer_map(screen, transfer);
386
387 /*
388 * Load alpha texture.
389 * Note: 0 means keep the fragment, 255 means kill it.
390 * We'll negate the texel value and use KILP which kills if value
391 * is negative.
392 */
393 for (i = 0; i < 32; i++) {
394 for (j = 0; j < 32; j++) {
395 if (stipple[i] & (bit31 >> j)) {
396 /* fragment "on" */
397 data[i * transfer->stride + j] = 0;
398 }
399 else {
400 /* fragment "off" */
401 data[i * transfer->stride + j] = 255;
402 }
403 }
404 }
405
406 /* unmap */
407 screen->transfer_unmap(screen, transfer);
408 screen->tex_transfer_destroy(transfer);
409 }
410
411
412 /**
413 * Create the texture map we'll use for stippling.
414 */
415 static boolean
416 pstip_create_texture(struct pstip_stage *pstip)
417 {
418 struct pipe_context *pipe = pstip->pipe;
419 struct pipe_screen *screen = pipe->screen;
420 struct pipe_texture texTemp;
421
422 memset(&texTemp, 0, sizeof(texTemp));
423 texTemp.target = PIPE_TEXTURE_2D;
424 texTemp.format = PIPE_FORMAT_A8_UNORM; /* XXX verify supported by driver! */
425 texTemp.last_level = 0;
426 texTemp.width[0] = 32;
427 texTemp.height[0] = 32;
428 texTemp.depth[0] = 1;
429 pf_get_block(texTemp.format, &texTemp.block);
430
431 pstip->texture = screen->texture_create(screen, &texTemp);
432 if (pstip->texture == NULL)
433 return FALSE;
434
435 return TRUE;
436 }
437
438
439 /**
440 * Create the sampler CSO that'll be used for stippling.
441 */
442 static boolean
443 pstip_create_sampler(struct pstip_stage *pstip)
444 {
445 struct pipe_sampler_state sampler;
446 struct pipe_context *pipe = pstip->pipe;
447
448 memset(&sampler, 0, sizeof(sampler));
449 sampler.wrap_s = PIPE_TEX_WRAP_REPEAT;
450 sampler.wrap_t = PIPE_TEX_WRAP_REPEAT;
451 sampler.wrap_r = PIPE_TEX_WRAP_REPEAT;
452 sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
453 sampler.min_img_filter = PIPE_TEX_FILTER_NEAREST;
454 sampler.mag_img_filter = PIPE_TEX_FILTER_NEAREST;
455 sampler.normalized_coords = 1;
456 sampler.min_lod = 0.0f;
457 sampler.max_lod = 0.0f;
458
459 pstip->sampler_cso = pipe->create_sampler_state(pipe, &sampler);
460 if (pstip->sampler_cso == NULL)
461 return FALSE;
462
463 return TRUE;
464 }
465
466
467 /**
468 * When we're about to draw our first stipple polygon in a batch, this function
469 * is called to tell the driver to bind our modified fragment shader.
470 */
471 static boolean
472 bind_pstip_fragment_shader(struct pstip_stage *pstip)
473 {
474 struct draw_context *draw = pstip->stage.draw;
475 if (!pstip->fs->pstip_fs &&
476 !generate_pstip_fs(pstip))
477 return FALSE;
478
479 draw->suspend_flushing = TRUE;
480 pstip->driver_bind_fs_state(pstip->pipe, pstip->fs->pstip_fs);
481 draw->suspend_flushing = FALSE;
482 return TRUE;
483 }
484
485
486 static INLINE struct pstip_stage *
487 pstip_stage( struct draw_stage *stage )
488 {
489 return (struct pstip_stage *) stage;
490 }
491
492
493 static void
494 pstip_first_tri(struct draw_stage *stage, struct prim_header *header)
495 {
496 struct pstip_stage *pstip = pstip_stage(stage);
497 struct pipe_context *pipe = pstip->pipe;
498 struct draw_context *draw = stage->draw;
499 uint num_samplers;
500
501 assert(stage->draw->rasterizer->poly_stipple_enable);
502
503 /* bind our fragprog */
504 if (!bind_pstip_fragment_shader(pstip)) {
505 stage->tri = draw_pipe_passthrough_tri;
506 stage->tri(stage, header);
507 return;
508 }
509
510
511 /* how many samplers? */
512 /* we'll use sampler/texture[pstip->sampler_unit] for the stipple */
513 num_samplers = MAX2(pstip->num_textures, pstip->num_samplers);
514 num_samplers = MAX2(num_samplers, pstip->fs->sampler_unit + 1);
515
516 /* plug in our sampler, texture */
517 pstip->state.samplers[pstip->fs->sampler_unit] = pstip->sampler_cso;
518 pipe_texture_reference(&pstip->state.textures[pstip->fs->sampler_unit],
519 pstip->texture);
520
521 assert(num_samplers <= PIPE_MAX_SAMPLERS);
522
523 draw->suspend_flushing = TRUE;
524 pstip->driver_bind_sampler_states(pipe, num_samplers, pstip->state.samplers);
525 pstip->driver_set_sampler_textures(pipe, num_samplers, pstip->state.textures);
526 draw->suspend_flushing = FALSE;
527
528 /* now really draw first triangle */
529 stage->tri = draw_pipe_passthrough_tri;
530 stage->tri(stage, header);
531 }
532
533
534 static void
535 pstip_flush(struct draw_stage *stage, unsigned flags)
536 {
537 struct draw_context *draw = stage->draw;
538 struct pstip_stage *pstip = pstip_stage(stage);
539 struct pipe_context *pipe = pstip->pipe;
540
541 stage->tri = pstip_first_tri;
542 stage->next->flush( stage->next, flags );
543
544 /* restore original frag shader, texture, sampler state */
545 draw->suspend_flushing = TRUE;
546 pstip->driver_bind_fs_state(pipe, pstip->fs->driver_fs);
547 pstip->driver_bind_sampler_states(pipe, pstip->num_samplers,
548 pstip->state.samplers);
549 pstip->driver_set_sampler_textures(pipe, pstip->num_textures,
550 pstip->state.textures);
551 draw->suspend_flushing = FALSE;
552 }
553
554
555 static void
556 pstip_reset_stipple_counter(struct draw_stage *stage)
557 {
558 stage->next->reset_stipple_counter( stage->next );
559 }
560
561
562 static void
563 pstip_destroy(struct draw_stage *stage)
564 {
565 struct pstip_stage *pstip = pstip_stage(stage);
566 uint i;
567
568 for (i = 0; i < PIPE_MAX_SAMPLERS; i++) {
569 pipe_texture_reference(&pstip->state.textures[i], NULL);
570 }
571
572 pstip->pipe->delete_sampler_state(pstip->pipe, pstip->sampler_cso);
573
574 pipe_texture_reference(&pstip->texture, NULL);
575
576 draw_free_temp_verts( stage );
577 FREE( stage );
578 }
579
580
581 static struct pstip_stage *
582 draw_pstip_stage(struct draw_context *draw)
583 {
584 struct pstip_stage *pstip = CALLOC_STRUCT(pstip_stage);
585
586 draw_alloc_temp_verts( &pstip->stage, 8 );
587
588 pstip->stage.draw = draw;
589 pstip->stage.next = NULL;
590 pstip->stage.point = draw_pipe_passthrough_point;
591 pstip->stage.line = draw_pipe_passthrough_line;
592 pstip->stage.tri = pstip_first_tri;
593 pstip->stage.flush = pstip_flush;
594 pstip->stage.reset_stipple_counter = pstip_reset_stipple_counter;
595 pstip->stage.destroy = pstip_destroy;
596
597 return pstip;
598 }
599
600
601 static struct pstip_stage *
602 pstip_stage_from_pipe(struct pipe_context *pipe)
603 {
604 struct draw_context *draw = (struct draw_context *) pipe->draw;
605 return pstip_stage(draw->pipeline.pstipple);
606 }
607
608
609 /**
610 * This function overrides the driver's create_fs_state() function and
611 * will typically be called by the state tracker.
612 */
613 static void *
614 pstip_create_fs_state(struct pipe_context *pipe,
615 const struct pipe_shader_state *fs)
616 {
617 struct pstip_stage *pstip = pstip_stage_from_pipe(pipe);
618 struct pstip_fragment_shader *aafs = CALLOC_STRUCT(pstip_fragment_shader);
619
620 if (aafs) {
621 aafs->state = *fs;
622
623 /* pass-through */
624 aafs->driver_fs = pstip->driver_create_fs_state(pstip->pipe, fs);
625 }
626
627 return aafs;
628 }
629
630
631 static void
632 pstip_bind_fs_state(struct pipe_context *pipe, void *fs)
633 {
634 struct pstip_stage *pstip = pstip_stage_from_pipe(pipe);
635 struct pstip_fragment_shader *aafs = (struct pstip_fragment_shader *) fs;
636 /* save current */
637 pstip->fs = aafs;
638 /* pass-through */
639 pstip->driver_bind_fs_state(pstip->pipe,
640 (aafs ? aafs->driver_fs : NULL));
641 }
642
643
644 static void
645 pstip_delete_fs_state(struct pipe_context *pipe, void *fs)
646 {
647 struct pstip_stage *pstip = pstip_stage_from_pipe(pipe);
648 struct pstip_fragment_shader *aafs = (struct pstip_fragment_shader *) fs;
649 /* pass-through */
650 pstip->driver_delete_fs_state(pstip->pipe, aafs->driver_fs);
651 FREE(aafs);
652 }
653
654
655 static void
656 pstip_bind_sampler_states(struct pipe_context *pipe,
657 unsigned num, void **sampler)
658 {
659 struct pstip_stage *pstip = pstip_stage_from_pipe(pipe);
660 uint i;
661
662 /* save current */
663 memcpy(pstip->state.samplers, sampler, num * sizeof(void *));
664 for (i = num; i < PIPE_MAX_SAMPLERS; i++) {
665 pstip->state.samplers[i] = NULL;
666 }
667
668 pstip->num_samplers = num;
669 /* pass-through */
670 pstip->driver_bind_sampler_states(pstip->pipe, num, sampler);
671 }
672
673
674 static void
675 pstip_set_sampler_textures(struct pipe_context *pipe,
676 unsigned num, struct pipe_texture **texture)
677 {
678 struct pstip_stage *pstip = pstip_stage_from_pipe(pipe);
679 uint i;
680
681 /* save current */
682 for (i = 0; i < num; i++) {
683 pipe_texture_reference(&pstip->state.textures[i], texture[i]);
684 }
685 for (; i < PIPE_MAX_SAMPLERS; i++) {
686 pipe_texture_reference(&pstip->state.textures[i], NULL);
687 }
688
689 pstip->num_textures = num;
690
691 /* pass-through */
692 pstip->driver_set_sampler_textures(pstip->pipe, num, texture);
693 }
694
695
696 static void
697 pstip_set_polygon_stipple(struct pipe_context *pipe,
698 const struct pipe_poly_stipple *stipple)
699 {
700 struct pstip_stage *pstip = pstip_stage_from_pipe(pipe);
701
702 /* save current */
703 pstip->state.stipple = stipple;
704
705 /* pass-through */
706 pstip->driver_set_polygon_stipple(pstip->pipe, stipple);
707
708 pstip_update_texture(pstip);
709 }
710
711
712 /**
713 * Called by drivers that want to install this polygon stipple stage
714 * into the draw module's pipeline. This will not be used if the
715 * hardware has native support for polygon stipple.
716 */
717 boolean
718 draw_install_pstipple_stage(struct draw_context *draw,
719 struct pipe_context *pipe)
720 {
721 struct pstip_stage *pstip;
722
723 pipe->draw = (void *) draw;
724
725 /*
726 * Create / install pgon stipple drawing / prim stage
727 */
728 pstip = draw_pstip_stage( draw );
729 if (pstip == NULL)
730 goto fail;
731
732 draw->pipeline.pstipple = &pstip->stage;
733
734 pstip->pipe = pipe;
735
736 /* create special texture, sampler state */
737 if (!pstip_create_texture(pstip))
738 goto fail;
739
740 if (!pstip_create_sampler(pstip))
741 goto fail;
742
743 /* save original driver functions */
744 pstip->driver_create_fs_state = pipe->create_fs_state;
745 pstip->driver_bind_fs_state = pipe->bind_fs_state;
746 pstip->driver_delete_fs_state = pipe->delete_fs_state;
747
748 pstip->driver_bind_sampler_states = pipe->bind_sampler_states;
749 pstip->driver_set_sampler_textures = pipe->set_sampler_textures;
750 pstip->driver_set_polygon_stipple = pipe->set_polygon_stipple;
751
752 /* override the driver's functions */
753 pipe->create_fs_state = pstip_create_fs_state;
754 pipe->bind_fs_state = pstip_bind_fs_state;
755 pipe->delete_fs_state = pstip_delete_fs_state;
756
757 pipe->bind_sampler_states = pstip_bind_sampler_states;
758 pipe->set_sampler_textures = pstip_set_sampler_textures;
759 pipe->set_polygon_stipple = pstip_set_polygon_stipple;
760
761 return TRUE;
762
763 fail:
764 if (pstip)
765 pstip->stage.destroy( &pstip->stage );
766
767 return FALSE;
768 }