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