draw/aaline: stronger guard against no free samplers (v2)
[mesa.git] / src / gallium / auxiliary / draw / draw_pipe_aaline.c
1 /**************************************************************************
2 *
3 * Copyright 2007 VMware, Inc.
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 VMWARE 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 * AA line stage: AA lines are converted to texture mapped triangles.
30 *
31 * Authors: Brian Paul
32 */
33
34
35 #include "pipe/p_context.h"
36 #include "pipe/p_defines.h"
37 #include "pipe/p_shader_tokens.h"
38 #include "util/u_inlines.h"
39
40 #include "util/u_format.h"
41 #include "util/u_math.h"
42 #include "util/u_memory.h"
43 #include "util/u_sampler.h"
44
45 #include "tgsi/tgsi_transform.h"
46 #include "tgsi/tgsi_dump.h"
47
48 #include "draw_context.h"
49 #include "draw_private.h"
50 #include "draw_pipe.h"
51
52
53 /** Approx number of new tokens for instructions in aa_transform_inst() */
54 #define NUM_NEW_TOKENS 53
55
56
57 /**
58 * Size for the alpha texture used for antialiasing
59 */
60 #define TEXTURE_SIZE_LOG2 5 /* 32 x 32 */
61
62 /**
63 * Max texture level for the alpha texture used for antialiasing
64 *
65 * Don't use the 1x1 and 2x2 mipmap levels.
66 */
67 #define MAX_TEXTURE_LEVEL (TEXTURE_SIZE_LOG2 - 2)
68
69
70 /**
71 * Subclass of pipe_shader_state to carry extra fragment shader info.
72 */
73 struct aaline_fragment_shader
74 {
75 struct pipe_shader_state state;
76 void *driver_fs;
77 void *aaline_fs;
78 uint sampler_unit;
79 int generic_attrib; /**< texcoord/generic used for texture */
80 };
81
82
83 /**
84 * Subclass of draw_stage
85 */
86 struct aaline_stage
87 {
88 struct draw_stage stage;
89
90 float half_line_width;
91
92 /** For AA lines, this is the vertex attrib slot for the new texcoords */
93 uint tex_slot;
94 /** position, not necessarily output zero */
95 uint pos_slot;
96
97 void *sampler_cso;
98 struct pipe_resource *texture;
99 struct pipe_sampler_view *sampler_view;
100 uint num_samplers;
101 uint num_sampler_views;
102
103
104 /*
105 * Currently bound state
106 */
107 struct aaline_fragment_shader *fs;
108 struct {
109 void *sampler[PIPE_MAX_SAMPLERS];
110 struct pipe_sampler_view *sampler_views[PIPE_MAX_SHADER_SAMPLER_VIEWS];
111 } state;
112
113 /*
114 * Driver interface/override functions
115 */
116 void * (*driver_create_fs_state)(struct pipe_context *,
117 const struct pipe_shader_state *);
118 void (*driver_bind_fs_state)(struct pipe_context *, void *);
119 void (*driver_delete_fs_state)(struct pipe_context *, void *);
120
121 void (*driver_bind_sampler_states)(struct pipe_context *, unsigned, unsigned,
122 unsigned, void **);
123
124 void (*driver_set_sampler_views)(struct pipe_context *, unsigned shader,
125 unsigned start, unsigned count,
126 struct pipe_sampler_view **);
127 };
128
129
130
131 /**
132 * Subclass of tgsi_transform_context, used for transforming the
133 * user's fragment shader to add the special AA instructions.
134 */
135 struct aa_transform_context {
136 struct tgsi_transform_context base;
137 uint tempsUsed; /**< bitmask */
138 int colorOutput; /**< which output is the primary color */
139 uint samplersUsed; /**< bitfield of samplers used */
140 bool hasSview;
141 int freeSampler; /** an available sampler for the pstipple */
142 int maxInput, maxGeneric; /**< max input index found */
143 int colorTemp, texTemp; /**< temp registers */
144 };
145
146
147 /**
148 * TGSI declaration transform callback.
149 * Look for a free sampler, a free input attrib, and two free temp regs.
150 */
151 static void
152 aa_transform_decl(struct tgsi_transform_context *ctx,
153 struct tgsi_full_declaration *decl)
154 {
155 struct aa_transform_context *aactx = (struct aa_transform_context *) ctx;
156
157 if (decl->Declaration.File == TGSI_FILE_OUTPUT &&
158 decl->Semantic.Name == TGSI_SEMANTIC_COLOR &&
159 decl->Semantic.Index == 0) {
160 aactx->colorOutput = decl->Range.First;
161 }
162 else if (decl->Declaration.File == TGSI_FILE_SAMPLER) {
163 uint i;
164 for (i = decl->Range.First;
165 i <= decl->Range.Last; i++) {
166 aactx->samplersUsed |= 1u << i;
167 }
168 }
169 else if (decl->Declaration.File == TGSI_FILE_SAMPLER_VIEW) {
170 aactx->hasSview = true;
171 }
172 else if (decl->Declaration.File == TGSI_FILE_INPUT) {
173 if ((int) decl->Range.Last > aactx->maxInput)
174 aactx->maxInput = decl->Range.Last;
175 if (decl->Semantic.Name == TGSI_SEMANTIC_GENERIC &&
176 (int) decl->Semantic.Index > aactx->maxGeneric) {
177 aactx->maxGeneric = decl->Semantic.Index;
178 }
179 }
180 else if (decl->Declaration.File == TGSI_FILE_TEMPORARY) {
181 uint i;
182 for (i = decl->Range.First;
183 i <= decl->Range.Last; i++) {
184 aactx->tempsUsed |= (1 << i);
185 }
186 }
187
188 ctx->emit_declaration(ctx, decl);
189 }
190
191
192 /**
193 * Find the lowest zero bit in the given word, or -1 if bitfield is all ones.
194 */
195 static int
196 free_bit(uint bitfield)
197 {
198 return ffs(~bitfield) - 1;
199 }
200
201
202 /**
203 * TGSI transform prolog callback.
204 */
205 static void
206 aa_transform_prolog(struct tgsi_transform_context *ctx)
207 {
208 struct aa_transform_context *aactx = (struct aa_transform_context *) ctx;
209 uint i;
210
211 STATIC_ASSERT(sizeof(aactx->samplersUsed) * 8 >= PIPE_MAX_SAMPLERS);
212
213 /* find free sampler */
214 aactx->freeSampler = free_bit(aactx->samplersUsed);
215 if (aactx->freeSampler < 0 || aactx->freeSampler >= PIPE_MAX_SAMPLERS)
216 aactx->freeSampler = PIPE_MAX_SAMPLERS - 1;
217
218 /* find two free temp regs */
219 for (i = 0; i < 32; i++) {
220 if ((aactx->tempsUsed & (1 << i)) == 0) {
221 /* found a free temp */
222 if (aactx->colorTemp < 0)
223 aactx->colorTemp = i;
224 else if (aactx->texTemp < 0)
225 aactx->texTemp = i;
226 else
227 break;
228 }
229 }
230 assert(aactx->colorTemp >= 0);
231 assert(aactx->texTemp >= 0);
232
233 /* declare new generic input/texcoord */
234 tgsi_transform_input_decl(ctx, aactx->maxInput + 1,
235 TGSI_SEMANTIC_GENERIC, aactx->maxGeneric + 1,
236 TGSI_INTERPOLATE_LINEAR);
237
238 /* declare new sampler */
239 tgsi_transform_sampler_decl(ctx, aactx->freeSampler);
240
241 /* if the src shader has SVIEW decl's for each SAMP decl, we
242 * need to continue the trend and ensure there is a matching
243 * SVIEW for the new SAMP we just created
244 */
245 if (aactx->hasSview) {
246 tgsi_transform_sampler_view_decl(ctx,
247 aactx->freeSampler,
248 TGSI_TEXTURE_2D,
249 TGSI_RETURN_TYPE_FLOAT);
250 }
251
252 /* declare new temp regs */
253 tgsi_transform_temp_decl(ctx, aactx->texTemp);
254 tgsi_transform_temp_decl(ctx, aactx->colorTemp);
255 }
256
257
258 /**
259 * TGSI transform epilog callback.
260 */
261 static void
262 aa_transform_epilog(struct tgsi_transform_context *ctx)
263 {
264 struct aa_transform_context *aactx = (struct aa_transform_context *) ctx;
265
266 if (aactx->colorOutput != -1) {
267 /* insert texture sampling code for antialiasing. */
268
269 /* TEX texTemp, input_coord, sampler, 2D */
270 tgsi_transform_tex_inst(ctx,
271 TGSI_FILE_TEMPORARY, aactx->texTemp,
272 TGSI_FILE_INPUT, aactx->maxInput + 1,
273 TGSI_TEXTURE_2D, aactx->freeSampler);
274
275 /* MOV rgb */
276 tgsi_transform_op1_inst(ctx, TGSI_OPCODE_MOV,
277 TGSI_FILE_OUTPUT, aactx->colorOutput,
278 TGSI_WRITEMASK_XYZ,
279 TGSI_FILE_TEMPORARY, aactx->colorTemp);
280
281 /* MUL alpha */
282 tgsi_transform_op2_inst(ctx, TGSI_OPCODE_MUL,
283 TGSI_FILE_OUTPUT, aactx->colorOutput,
284 TGSI_WRITEMASK_W,
285 TGSI_FILE_TEMPORARY, aactx->colorTemp,
286 TGSI_FILE_TEMPORARY, aactx->texTemp);
287 }
288 }
289
290
291 /**
292 * TGSI instruction transform callback.
293 * Replace writes to result.color w/ a temp reg.
294 */
295 static void
296 aa_transform_inst(struct tgsi_transform_context *ctx,
297 struct tgsi_full_instruction *inst)
298 {
299 struct aa_transform_context *aactx = (struct aa_transform_context *) ctx;
300 uint i;
301
302 /*
303 * Look for writes to result.color and replace with colorTemp reg.
304 */
305 for (i = 0; i < inst->Instruction.NumDstRegs; i++) {
306 struct tgsi_full_dst_register *dst = &inst->Dst[i];
307 if (dst->Register.File == TGSI_FILE_OUTPUT &&
308 dst->Register.Index == aactx->colorOutput) {
309 dst->Register.File = TGSI_FILE_TEMPORARY;
310 dst->Register.Index = aactx->colorTemp;
311 }
312 }
313
314 ctx->emit_instruction(ctx, inst);
315 }
316
317
318 /**
319 * Generate the frag shader we'll use for drawing AA lines.
320 * This will be the user's shader plus some texture/modulate instructions.
321 */
322 static boolean
323 generate_aaline_fs(struct aaline_stage *aaline)
324 {
325 struct pipe_context *pipe = aaline->stage.draw->pipe;
326 const struct pipe_shader_state *orig_fs = &aaline->fs->state;
327 struct pipe_shader_state aaline_fs;
328 struct aa_transform_context transform;
329 const uint newLen = tgsi_num_tokens(orig_fs->tokens) + NUM_NEW_TOKENS;
330
331 aaline_fs = *orig_fs; /* copy to init */
332 aaline_fs.tokens = tgsi_alloc_tokens(newLen);
333 if (aaline_fs.tokens == NULL)
334 return FALSE;
335
336 memset(&transform, 0, sizeof(transform));
337 transform.colorOutput = -1;
338 transform.maxInput = -1;
339 transform.maxGeneric = -1;
340 transform.colorTemp = -1;
341 transform.texTemp = -1;
342 transform.base.prolog = aa_transform_prolog;
343 transform.base.epilog = aa_transform_epilog;
344 transform.base.transform_instruction = aa_transform_inst;
345 transform.base.transform_declaration = aa_transform_decl;
346
347 tgsi_transform_shader(orig_fs->tokens,
348 (struct tgsi_token *) aaline_fs.tokens,
349 newLen, &transform.base);
350
351 #if 0 /* DEBUG */
352 debug_printf("draw_aaline, orig shader:\n");
353 tgsi_dump(orig_fs->tokens, 0);
354 debug_printf("draw_aaline, new shader:\n");
355 tgsi_dump(aaline_fs.tokens, 0);
356 #endif
357
358 aaline->fs->sampler_unit = transform.freeSampler;
359
360 aaline->fs->aaline_fs = aaline->driver_create_fs_state(pipe, &aaline_fs);
361 if (aaline->fs->aaline_fs == NULL)
362 goto fail;
363
364 aaline->fs->generic_attrib = transform.maxGeneric + 1;
365 FREE((void *)aaline_fs.tokens);
366 return TRUE;
367
368 fail:
369 FREE((void *)aaline_fs.tokens);
370 return FALSE;
371 }
372
373
374 /**
375 * Create the texture map we'll use for antialiasing the lines.
376 */
377 static boolean
378 aaline_create_texture(struct aaline_stage *aaline)
379 {
380 struct pipe_context *pipe = aaline->stage.draw->pipe;
381 struct pipe_screen *screen = pipe->screen;
382 struct pipe_resource texTemp;
383 struct pipe_sampler_view viewTempl;
384 uint level;
385
386 memset(&texTemp, 0, sizeof(texTemp));
387 texTemp.target = PIPE_TEXTURE_2D;
388 texTemp.format = PIPE_FORMAT_A8_UNORM; /* XXX verify supported by driver! */
389 texTemp.last_level = MAX_TEXTURE_LEVEL;
390 texTemp.width0 = 1 << TEXTURE_SIZE_LOG2;
391 texTemp.height0 = 1 << TEXTURE_SIZE_LOG2;
392 texTemp.depth0 = 1;
393 texTemp.array_size = 1;
394 texTemp.bind = PIPE_BIND_SAMPLER_VIEW;
395
396 aaline->texture = screen->resource_create(screen, &texTemp);
397 if (!aaline->texture)
398 return FALSE;
399
400 u_sampler_view_default_template(&viewTempl,
401 aaline->texture,
402 aaline->texture->format);
403 aaline->sampler_view = pipe->create_sampler_view(pipe,
404 aaline->texture,
405 &viewTempl);
406 if (!aaline->sampler_view) {
407 return FALSE;
408 }
409
410 /* Fill in mipmap images.
411 * Basically each level is solid opaque, except for the outermost
412 * texels which are zero. Special case the 1x1 and 2x2 levels
413 * (though, those levels shouldn't be used - see the max_lod setting).
414 */
415 for (level = 0; level <= MAX_TEXTURE_LEVEL; level++) {
416 struct pipe_transfer *transfer;
417 struct pipe_box box;
418 const uint size = u_minify(aaline->texture->width0, level);
419 ubyte *data;
420 uint i, j;
421
422 assert(aaline->texture->width0 == aaline->texture->height0);
423
424 u_box_origin_2d( size, size, &box );
425
426 /* This texture is new, no need to flush.
427 */
428 data = pipe->transfer_map(pipe,
429 aaline->texture,
430 level,
431 PIPE_TRANSFER_WRITE,
432 &box, &transfer);
433
434 if (!data)
435 return FALSE;
436
437 for (i = 0; i < size; i++) {
438 for (j = 0; j < size; j++) {
439 ubyte d;
440 if (size == 1) {
441 d = 255;
442 }
443 else if (size == 2) {
444 d = 200; /* tuneable */
445 }
446 else if (i == 0 || j == 0 || i == size - 1 || j == size - 1) {
447 d = 35; /* edge texel */
448 }
449 else {
450 d = 255;
451 }
452 data[i * transfer->stride + j] = d;
453 }
454 }
455
456 /* unmap */
457 pipe->transfer_unmap(pipe, transfer);
458 }
459 return TRUE;
460 }
461
462
463 /**
464 * Create the sampler CSO that'll be used for antialiasing.
465 * By using a mipmapped texture, we don't have to generate a different
466 * texture image for each line size.
467 */
468 static boolean
469 aaline_create_sampler(struct aaline_stage *aaline)
470 {
471 struct pipe_sampler_state sampler;
472 struct pipe_context *pipe = aaline->stage.draw->pipe;
473
474 memset(&sampler, 0, sizeof(sampler));
475 sampler.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
476 sampler.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
477 sampler.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
478 sampler.min_mip_filter = PIPE_TEX_MIPFILTER_LINEAR;
479 sampler.min_img_filter = PIPE_TEX_FILTER_LINEAR;
480 sampler.mag_img_filter = PIPE_TEX_FILTER_LINEAR;
481 sampler.normalized_coords = 1;
482 sampler.min_lod = 0.0f;
483 sampler.max_lod = MAX_TEXTURE_LEVEL;
484
485 aaline->sampler_cso = pipe->create_sampler_state(pipe, &sampler);
486 if (aaline->sampler_cso == NULL)
487 return FALSE;
488
489 return TRUE;
490 }
491
492
493 /**
494 * When we're about to draw our first AA line in a batch, this function is
495 * called to tell the driver to bind our modified fragment shader.
496 */
497 static boolean
498 bind_aaline_fragment_shader(struct aaline_stage *aaline)
499 {
500 struct draw_context *draw = aaline->stage.draw;
501 struct pipe_context *pipe = draw->pipe;
502
503 if (!aaline->fs->aaline_fs &&
504 !generate_aaline_fs(aaline))
505 return FALSE;
506
507 draw->suspend_flushing = TRUE;
508 aaline->driver_bind_fs_state(pipe, aaline->fs->aaline_fs);
509 draw->suspend_flushing = FALSE;
510
511 return TRUE;
512 }
513
514
515
516 static inline struct aaline_stage *
517 aaline_stage( struct draw_stage *stage )
518 {
519 return (struct aaline_stage *) stage;
520 }
521
522
523 /**
524 * Draw a wide line by drawing a quad, using geometry which will
525 * fullfill GL's antialiased line requirements.
526 */
527 static void
528 aaline_line(struct draw_stage *stage, struct prim_header *header)
529 {
530 const struct aaline_stage *aaline = aaline_stage(stage);
531 const float half_width = aaline->half_line_width;
532 struct prim_header tri;
533 struct vertex_header *v[8];
534 uint texPos = aaline->tex_slot;
535 uint posPos = aaline->pos_slot;
536 float *pos, *tex;
537 float dx = header->v[1]->data[posPos][0] - header->v[0]->data[posPos][0];
538 float dy = header->v[1]->data[posPos][1] - header->v[0]->data[posPos][1];
539 double a = atan2(dy, dx);
540 float c_a = (float) cos(a), s_a = (float) sin(a);
541 uint i;
542
543 /* XXX the ends of lines aren't quite perfect yet, but probably passable */
544 dx = 0.5F * half_width;
545 dy = half_width;
546
547 /* allocate/dup new verts */
548 for (i = 0; i < 8; i++) {
549 v[i] = dup_vert(stage, header->v[i/4], i);
550 }
551
552 /*
553 * Quad strip for line from v0 to v1 (*=endpoints):
554 *
555 * 1 3 5 7
556 * +---+---------------------+---+
557 * | |
558 * | *v0 v1* |
559 * | |
560 * +---+---------------------+---+
561 * 0 2 4 6
562 */
563
564 /* new verts */
565 pos = v[0]->data[posPos];
566 pos[0] += (-dx * c_a - dy * s_a);
567 pos[1] += (-dx * s_a + dy * c_a);
568
569 pos = v[1]->data[posPos];
570 pos[0] += (-dx * c_a - -dy * s_a);
571 pos[1] += (-dx * s_a + -dy * c_a);
572
573 pos = v[2]->data[posPos];
574 pos[0] += ( dx * c_a - dy * s_a);
575 pos[1] += ( dx * s_a + dy * c_a);
576
577 pos = v[3]->data[posPos];
578 pos[0] += ( dx * c_a - -dy * s_a);
579 pos[1] += ( dx * s_a + -dy * c_a);
580
581 pos = v[4]->data[posPos];
582 pos[0] += (-dx * c_a - dy * s_a);
583 pos[1] += (-dx * s_a + dy * c_a);
584
585 pos = v[5]->data[posPos];
586 pos[0] += (-dx * c_a - -dy * s_a);
587 pos[1] += (-dx * s_a + -dy * c_a);
588
589 pos = v[6]->data[posPos];
590 pos[0] += ( dx * c_a - dy * s_a);
591 pos[1] += ( dx * s_a + dy * c_a);
592
593 pos = v[7]->data[posPos];
594 pos[0] += ( dx * c_a - -dy * s_a);
595 pos[1] += ( dx * s_a + -dy * c_a);
596
597 /* new texcoords */
598 tex = v[0]->data[texPos];
599 ASSIGN_4V(tex, 0, 0, 0, 1);
600
601 tex = v[1]->data[texPos];
602 ASSIGN_4V(tex, 0, 1, 0, 1);
603
604 tex = v[2]->data[texPos];
605 ASSIGN_4V(tex, .5, 0, 0, 1);
606
607 tex = v[3]->data[texPos];
608 ASSIGN_4V(tex, .5, 1, 0, 1);
609
610 tex = v[4]->data[texPos];
611 ASSIGN_4V(tex, .5, 0, 0, 1);
612
613 tex = v[5]->data[texPos];
614 ASSIGN_4V(tex, .5, 1, 0, 1);
615
616 tex = v[6]->data[texPos];
617 ASSIGN_4V(tex, 1, 0, 0, 1);
618
619 tex = v[7]->data[texPos];
620 ASSIGN_4V(tex, 1, 1, 0, 1);
621
622 /* emit 6 tris for the quad strip */
623 tri.v[0] = v[2]; tri.v[1] = v[1]; tri.v[2] = v[0];
624 stage->next->tri( stage->next, &tri );
625
626 tri.v[0] = v[3]; tri.v[1] = v[1]; tri.v[2] = v[2];
627 stage->next->tri( stage->next, &tri );
628
629 tri.v[0] = v[4]; tri.v[1] = v[3]; tri.v[2] = v[2];
630 stage->next->tri( stage->next, &tri );
631
632 tri.v[0] = v[5]; tri.v[1] = v[3]; tri.v[2] = v[4];
633 stage->next->tri( stage->next, &tri );
634
635 tri.v[0] = v[6]; tri.v[1] = v[5]; tri.v[2] = v[4];
636 stage->next->tri( stage->next, &tri );
637
638 tri.v[0] = v[7]; tri.v[1] = v[5]; tri.v[2] = v[6];
639 stage->next->tri( stage->next, &tri );
640 }
641
642
643 static void
644 aaline_first_line(struct draw_stage *stage, struct prim_header *header)
645 {
646 auto struct aaline_stage *aaline = aaline_stage(stage);
647 struct draw_context *draw = stage->draw;
648 struct pipe_context *pipe = draw->pipe;
649 const struct pipe_rasterizer_state *rast = draw->rasterizer;
650 uint num_samplers;
651 uint num_sampler_views;
652 void *r;
653
654 assert(draw->rasterizer->line_smooth);
655
656 if (draw->rasterizer->line_width <= 2.2)
657 aaline->half_line_width = 1.1f;
658 else
659 aaline->half_line_width = 0.5f * draw->rasterizer->line_width;
660
661 /*
662 * Bind (generate) our fragprog, sampler and texture
663 */
664 if (!bind_aaline_fragment_shader(aaline)) {
665 stage->line = draw_pipe_passthrough_line;
666 stage->line(stage, header);
667 return;
668 }
669
670 draw_aaline_prepare_outputs(draw, draw->pipeline.aaline);
671
672 /* how many samplers? */
673 /* we'll use sampler/texture[aaline->sampler_unit] for the alpha texture */
674 num_samplers = MAX2(aaline->num_samplers, aaline->fs->sampler_unit + 1);
675 num_sampler_views = MAX2(num_samplers, aaline->num_sampler_views);
676
677 aaline->state.sampler[aaline->fs->sampler_unit] = aaline->sampler_cso;
678 pipe_sampler_view_reference(&aaline->state.sampler_views[aaline->fs->sampler_unit],
679 aaline->sampler_view);
680
681 draw->suspend_flushing = TRUE;
682
683 aaline->driver_bind_sampler_states(pipe, PIPE_SHADER_FRAGMENT, 0,
684 num_samplers, aaline->state.sampler);
685
686 aaline->driver_set_sampler_views(pipe, PIPE_SHADER_FRAGMENT, 0,
687 num_sampler_views, aaline->state.sampler_views);
688
689 /* Disable triangle culling, stippling, unfilled mode etc. */
690 r = draw_get_rasterizer_no_cull(draw, rast->scissor, rast->flatshade);
691 pipe->bind_rasterizer_state(pipe, r);
692
693 draw->suspend_flushing = FALSE;
694
695 /* now really draw first line */
696 stage->line = aaline_line;
697 stage->line(stage, header);
698 }
699
700
701 static void
702 aaline_flush(struct draw_stage *stage, unsigned flags)
703 {
704 struct draw_context *draw = stage->draw;
705 struct aaline_stage *aaline = aaline_stage(stage);
706 struct pipe_context *pipe = draw->pipe;
707
708 stage->line = aaline_first_line;
709 stage->next->flush( stage->next, flags );
710
711 /* restore original frag shader, texture, sampler state */
712 draw->suspend_flushing = TRUE;
713 aaline->driver_bind_fs_state(pipe, aaline->fs ? aaline->fs->driver_fs : NULL);
714
715 aaline->driver_bind_sampler_states(pipe, PIPE_SHADER_FRAGMENT, 0,
716 aaline->num_samplers,
717 aaline->state.sampler);
718
719 aaline->driver_set_sampler_views(pipe, PIPE_SHADER_FRAGMENT, 0,
720 aaline->num_samplers,
721 aaline->state.sampler_views);
722
723 /* restore original rasterizer state */
724 if (draw->rast_handle) {
725 pipe->bind_rasterizer_state(pipe, draw->rast_handle);
726 }
727
728 draw->suspend_flushing = FALSE;
729
730 draw_remove_extra_vertex_attribs(draw);
731 }
732
733
734 static void
735 aaline_reset_stipple_counter(struct draw_stage *stage)
736 {
737 stage->next->reset_stipple_counter( stage->next );
738 }
739
740
741 static void
742 aaline_destroy(struct draw_stage *stage)
743 {
744 struct aaline_stage *aaline = aaline_stage(stage);
745 struct pipe_context *pipe = stage->draw->pipe;
746 uint i;
747
748 for (i = 0; i < PIPE_MAX_SHADER_SAMPLER_VIEWS; i++) {
749 pipe_sampler_view_reference(&aaline->state.sampler_views[i], NULL);
750 }
751
752 if (aaline->sampler_cso)
753 pipe->delete_sampler_state(pipe, aaline->sampler_cso);
754
755 if (aaline->texture)
756 pipe_resource_reference(&aaline->texture, NULL);
757
758 if (aaline->sampler_view) {
759 pipe_sampler_view_reference(&aaline->sampler_view, NULL);
760 }
761
762 draw_free_temp_verts( stage );
763
764 /* restore the old entry points */
765 pipe->create_fs_state = aaline->driver_create_fs_state;
766 pipe->bind_fs_state = aaline->driver_bind_fs_state;
767 pipe->delete_fs_state = aaline->driver_delete_fs_state;
768
769 pipe->bind_sampler_states = aaline->driver_bind_sampler_states;
770 pipe->set_sampler_views = aaline->driver_set_sampler_views;
771
772 FREE( stage );
773 }
774
775
776 static struct aaline_stage *
777 draw_aaline_stage(struct draw_context *draw)
778 {
779 struct aaline_stage *aaline = CALLOC_STRUCT(aaline_stage);
780 if (!aaline)
781 return NULL;
782
783 aaline->stage.draw = draw;
784 aaline->stage.name = "aaline";
785 aaline->stage.next = NULL;
786 aaline->stage.point = draw_pipe_passthrough_point;
787 aaline->stage.line = aaline_first_line;
788 aaline->stage.tri = draw_pipe_passthrough_tri;
789 aaline->stage.flush = aaline_flush;
790 aaline->stage.reset_stipple_counter = aaline_reset_stipple_counter;
791 aaline->stage.destroy = aaline_destroy;
792
793 if (!draw_alloc_temp_verts( &aaline->stage, 8 ))
794 goto fail;
795
796 return aaline;
797
798 fail:
799 aaline->stage.destroy(&aaline->stage);
800
801 return NULL;
802 }
803
804
805 static struct aaline_stage *
806 aaline_stage_from_pipe(struct pipe_context *pipe)
807 {
808 struct draw_context *draw = (struct draw_context *) pipe->draw;
809
810 if (draw) {
811 return aaline_stage(draw->pipeline.aaline);
812 } else {
813 return NULL;
814 }
815 }
816
817
818 /**
819 * This function overrides the driver's create_fs_state() function and
820 * will typically be called by the state tracker.
821 */
822 static void *
823 aaline_create_fs_state(struct pipe_context *pipe,
824 const struct pipe_shader_state *fs)
825 {
826 struct aaline_stage *aaline = aaline_stage_from_pipe(pipe);
827 struct aaline_fragment_shader *aafs = NULL;
828
829 if (!aaline)
830 return NULL;
831
832 aafs = CALLOC_STRUCT(aaline_fragment_shader);
833
834 if (!aafs)
835 return NULL;
836
837 aafs->state.tokens = tgsi_dup_tokens(fs->tokens);
838
839 /* pass-through */
840 aafs->driver_fs = aaline->driver_create_fs_state(pipe, fs);
841
842 return aafs;
843 }
844
845
846 static void
847 aaline_bind_fs_state(struct pipe_context *pipe, void *fs)
848 {
849 struct aaline_stage *aaline = aaline_stage_from_pipe(pipe);
850 struct aaline_fragment_shader *aafs = (struct aaline_fragment_shader *) fs;
851
852 if (!aaline) {
853 return;
854 }
855
856 /* save current */
857 aaline->fs = aafs;
858 /* pass-through */
859 aaline->driver_bind_fs_state(pipe, (aafs ? aafs->driver_fs : NULL));
860 }
861
862
863 static void
864 aaline_delete_fs_state(struct pipe_context *pipe, void *fs)
865 {
866 struct aaline_stage *aaline = aaline_stage_from_pipe(pipe);
867 struct aaline_fragment_shader *aafs = (struct aaline_fragment_shader *) fs;
868
869 if (!aafs) {
870 return;
871 }
872
873 if (aaline) {
874 /* pass-through */
875 aaline->driver_delete_fs_state(pipe, aafs->driver_fs);
876
877 if (aafs->aaline_fs)
878 aaline->driver_delete_fs_state(pipe, aafs->aaline_fs);
879 }
880
881 FREE((void*)aafs->state.tokens);
882 FREE(aafs);
883 }
884
885
886 static void
887 aaline_bind_sampler_states(struct pipe_context *pipe, unsigned shader,
888 unsigned start, unsigned num, void **sampler)
889 {
890 struct aaline_stage *aaline = aaline_stage_from_pipe(pipe);
891
892 assert(start == 0);
893
894 if (!aaline) {
895 return;
896 }
897
898 if (shader == PIPE_SHADER_FRAGMENT) {
899 /* save current */
900 memcpy(aaline->state.sampler, sampler, num * sizeof(void *));
901 aaline->num_samplers = num;
902 }
903
904 /* pass-through */
905 aaline->driver_bind_sampler_states(pipe, shader, start, num, sampler);
906 }
907
908
909 static void
910 aaline_set_sampler_views(struct pipe_context *pipe, unsigned shader,
911 unsigned start, unsigned num,
912 struct pipe_sampler_view **views)
913 {
914 struct aaline_stage *aaline = aaline_stage_from_pipe(pipe);
915 uint i;
916
917 if (!aaline) {
918 return;
919 }
920
921 if (shader == PIPE_SHADER_FRAGMENT) {
922 /* save current */
923 for (i = 0; i < num; i++) {
924 pipe_sampler_view_reference(&aaline->state.sampler_views[start + i],
925 views[i]);
926 }
927 aaline->num_sampler_views = num;
928 }
929
930 /* pass-through */
931 aaline->driver_set_sampler_views(pipe, shader, start, num, views);
932 }
933
934
935 void
936 draw_aaline_prepare_outputs(struct draw_context *draw,
937 struct draw_stage *stage)
938 {
939 struct aaline_stage *aaline = aaline_stage(stage);
940 const struct pipe_rasterizer_state *rast = draw->rasterizer;
941
942 /* update vertex attrib info */
943 aaline->pos_slot = draw_current_shader_position_output(draw);
944
945 if (!rast->line_smooth)
946 return;
947
948 /* allocate the extra post-transformed vertex attribute */
949 aaline->tex_slot = draw_alloc_extra_vertex_attrib(draw,
950 TGSI_SEMANTIC_GENERIC,
951 aaline->fs->generic_attrib);
952 }
953
954 /**
955 * Called by drivers that want to install this AA line prim stage
956 * into the draw module's pipeline. This will not be used if the
957 * hardware has native support for AA lines.
958 */
959 boolean
960 draw_install_aaline_stage(struct draw_context *draw, struct pipe_context *pipe)
961 {
962 struct aaline_stage *aaline;
963
964 pipe->draw = (void *) draw;
965
966 /*
967 * Create / install AA line drawing / prim stage
968 */
969 aaline = draw_aaline_stage( draw );
970 if (!aaline)
971 goto fail;
972
973 /* save original driver functions */
974 aaline->driver_create_fs_state = pipe->create_fs_state;
975 aaline->driver_bind_fs_state = pipe->bind_fs_state;
976 aaline->driver_delete_fs_state = pipe->delete_fs_state;
977
978 aaline->driver_bind_sampler_states = pipe->bind_sampler_states;
979 aaline->driver_set_sampler_views = pipe->set_sampler_views;
980
981 /* create special texture, sampler state */
982 if (!aaline_create_texture(aaline))
983 goto fail;
984
985 if (!aaline_create_sampler(aaline))
986 goto fail;
987
988 /* override the driver's functions */
989 pipe->create_fs_state = aaline_create_fs_state;
990 pipe->bind_fs_state = aaline_bind_fs_state;
991 pipe->delete_fs_state = aaline_delete_fs_state;
992
993 pipe->bind_sampler_states = aaline_bind_sampler_states;
994 pipe->set_sampler_views = aaline_set_sampler_views;
995
996 /* Install once everything is known to be OK:
997 */
998 draw->pipeline.aaline = &aaline->stage;
999
1000 return TRUE;
1001
1002 fail:
1003 if (aaline)
1004 aaline->stage.destroy( &aaline->stage );
1005
1006 return FALSE;
1007 }