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