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