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