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