bb4261c9f9cde0c45127838d5748ecf432a22652
[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 * 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 void (*driver_bind_fragment_sampler_states)(struct pipe_context *, unsigned,
124 void **);
125
126 void (*driver_set_sampler_views)(struct pipe_context *,
127 unsigned,
128 struct pipe_sampler_view **);
129 };
130
131
132
133 /**
134 * Subclass of tgsi_transform_context, used for transforming the
135 * user's fragment shader to add the special AA instructions.
136 */
137 struct aa_transform_context {
138 struct tgsi_transform_context base;
139 uint tempsUsed; /**< bitmask */
140 int colorOutput; /**< which output is the primary color */
141 uint samplersUsed; /**< bitfield of samplers used */
142 int freeSampler; /** an available sampler for the pstipple */
143 int maxInput, maxGeneric; /**< max input index found */
144 int colorTemp, texTemp; /**< temp registers */
145 boolean firstInstruction;
146 };
147
148
149 /**
150 * TGSI declaration transform callback.
151 * Look for a free sampler, a free input attrib, and two free temp regs.
152 */
153 static void
154 aa_transform_decl(struct tgsi_transform_context *ctx,
155 struct tgsi_full_declaration *decl)
156 {
157 struct aa_transform_context *aactx = (struct aa_transform_context *) ctx;
158
159 if (decl->Declaration.File == TGSI_FILE_OUTPUT &&
160 decl->Semantic.Name == TGSI_SEMANTIC_COLOR &&
161 decl->Semantic.Index == 0) {
162 aactx->colorOutput = decl->Range.First;
163 }
164 else if (decl->Declaration.File == TGSI_FILE_SAMPLER) {
165 uint i;
166 for (i = decl->Range.First;
167 i <= decl->Range.Last; i++) {
168 aactx->samplersUsed |= 1 << i;
169 }
170 }
171 else if (decl->Declaration.File == TGSI_FILE_INPUT) {
172 if ((int) decl->Range.Last > aactx->maxInput)
173 aactx->maxInput = decl->Range.Last;
174 if (decl->Semantic.Name == TGSI_SEMANTIC_GENERIC &&
175 (int) decl->Semantic.Index > aactx->maxGeneric) {
176 aactx->maxGeneric = decl->Semantic.Index;
177 }
178 }
179 else if (decl->Declaration.File == TGSI_FILE_TEMPORARY) {
180 uint i;
181 for (i = decl->Range.First;
182 i <= decl->Range.Last; i++) {
183 aactx->tempsUsed |= (1 << i);
184 }
185 }
186
187 ctx->emit_declaration(ctx, decl);
188 }
189
190
191 /**
192 * Find the lowest zero bit in the given word, or -1 if bitfield is all ones.
193 */
194 static int
195 free_bit(uint bitfield)
196 {
197 return ffs(~bitfield) - 1;
198 }
199
200
201 /**
202 * TGSI instruction transform callback.
203 * Replace writes to result.color w/ a temp reg.
204 * Upon END instruction, insert texture sampling code for antialiasing.
205 */
206 static void
207 aa_transform_inst(struct tgsi_transform_context *ctx,
208 struct tgsi_full_instruction *inst)
209 {
210 struct aa_transform_context *aactx = (struct aa_transform_context *) ctx;
211
212 if (aactx->firstInstruction) {
213 /* emit our new declarations before the first instruction */
214
215 struct tgsi_full_declaration decl;
216 uint i;
217
218 /* find free sampler */
219 aactx->freeSampler = free_bit(aactx->samplersUsed);
220 if (aactx->freeSampler >= PIPE_MAX_SAMPLERS)
221 aactx->freeSampler = PIPE_MAX_SAMPLERS - 1;
222
223 /* find two free temp regs */
224 for (i = 0; i < 32; i++) {
225 if ((aactx->tempsUsed & (1 << i)) == 0) {
226 /* found a free temp */
227 if (aactx->colorTemp < 0)
228 aactx->colorTemp = i;
229 else if (aactx->texTemp < 0)
230 aactx->texTemp = i;
231 else
232 break;
233 }
234 }
235 assert(aactx->colorTemp >= 0);
236 assert(aactx->texTemp >= 0);
237
238 /* declare new generic input/texcoord */
239 decl = tgsi_default_full_declaration();
240 decl.Declaration.File = TGSI_FILE_INPUT;
241 /* XXX this could be linear... */
242 decl.Declaration.Interpolate = 1;
243 decl.Declaration.Semantic = 1;
244 decl.Semantic.Name = TGSI_SEMANTIC_GENERIC;
245 decl.Semantic.Index = aactx->maxGeneric + 1;
246 decl.Range.First =
247 decl.Range.Last = aactx->maxInput + 1;
248 decl.Interp.Interpolate = TGSI_INTERPOLATE_PERSPECTIVE;
249 ctx->emit_declaration(ctx, &decl);
250
251 /* declare new sampler */
252 decl = tgsi_default_full_declaration();
253 decl.Declaration.File = TGSI_FILE_SAMPLER;
254 decl.Range.First =
255 decl.Range.Last = aactx->freeSampler;
256 ctx->emit_declaration(ctx, &decl);
257
258 /* declare new temp regs */
259 decl = tgsi_default_full_declaration();
260 decl.Declaration.File = TGSI_FILE_TEMPORARY;
261 decl.Range.First =
262 decl.Range.Last = aactx->texTemp;
263 ctx->emit_declaration(ctx, &decl);
264
265 decl = tgsi_default_full_declaration();
266 decl.Declaration.File = TGSI_FILE_TEMPORARY;
267 decl.Range.First =
268 decl.Range.Last = aactx->colorTemp;
269 ctx->emit_declaration(ctx, &decl);
270
271 aactx->firstInstruction = FALSE;
272 }
273
274 if (inst->Instruction.Opcode == TGSI_OPCODE_END &&
275 aactx->colorOutput != -1) {
276 struct tgsi_full_instruction newInst;
277
278 /* TEX */
279 newInst = tgsi_default_full_instruction();
280 newInst.Instruction.Opcode = TGSI_OPCODE_TEX;
281 newInst.Instruction.NumDstRegs = 1;
282 newInst.Dst[0].Register.File = TGSI_FILE_TEMPORARY;
283 newInst.Dst[0].Register.Index = aactx->texTemp;
284 newInst.Instruction.NumSrcRegs = 2;
285 newInst.Instruction.Texture = TRUE;
286 newInst.Texture.Texture = TGSI_TEXTURE_2D;
287 newInst.Src[0].Register.File = TGSI_FILE_INPUT;
288 newInst.Src[0].Register.Index = aactx->maxInput + 1;
289 newInst.Src[1].Register.File = TGSI_FILE_SAMPLER;
290 newInst.Src[1].Register.Index = aactx->freeSampler;
291
292 ctx->emit_instruction(ctx, &newInst);
293
294 /* MOV rgb */
295 newInst = tgsi_default_full_instruction();
296 newInst.Instruction.Opcode = TGSI_OPCODE_MOV;
297 newInst.Instruction.NumDstRegs = 1;
298 newInst.Dst[0].Register.File = TGSI_FILE_OUTPUT;
299 newInst.Dst[0].Register.Index = aactx->colorOutput;
300 newInst.Dst[0].Register.WriteMask = TGSI_WRITEMASK_XYZ;
301 newInst.Instruction.NumSrcRegs = 1;
302 newInst.Src[0].Register.File = TGSI_FILE_TEMPORARY;
303 newInst.Src[0].Register.Index = aactx->colorTemp;
304 ctx->emit_instruction(ctx, &newInst);
305
306 /* MUL alpha */
307 newInst = tgsi_default_full_instruction();
308 newInst.Instruction.Opcode = TGSI_OPCODE_MUL;
309 newInst.Instruction.NumDstRegs = 1;
310 newInst.Dst[0].Register.File = TGSI_FILE_OUTPUT;
311 newInst.Dst[0].Register.Index = aactx->colorOutput;
312 newInst.Dst[0].Register.WriteMask = TGSI_WRITEMASK_W;
313 newInst.Instruction.NumSrcRegs = 2;
314 newInst.Src[0].Register.File = TGSI_FILE_TEMPORARY;
315 newInst.Src[0].Register.Index = aactx->colorTemp;
316 newInst.Src[1].Register.File = TGSI_FILE_TEMPORARY;
317 newInst.Src[1].Register.Index = aactx->texTemp;
318 ctx->emit_instruction(ctx, &newInst);
319
320 /* END */
321 newInst = tgsi_default_full_instruction();
322 newInst.Instruction.Opcode = TGSI_OPCODE_END;
323 newInst.Instruction.NumDstRegs = 0;
324 newInst.Instruction.NumSrcRegs = 0;
325 ctx->emit_instruction(ctx, &newInst);
326 }
327 else {
328 /* Not an END instruction.
329 * Look for writes to result.color and replace with colorTemp reg.
330 */
331 uint i;
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 /**
348 * Generate the frag shader we'll use for drawing AA lines.
349 * This will be the user's shader plus some texture/modulate instructions.
350 */
351 static boolean
352 generate_aaline_fs(struct aaline_stage *aaline)
353 {
354 struct pipe_context *pipe = aaline->stage.draw->pipe;
355 const struct pipe_shader_state *orig_fs = &aaline->fs->state;
356 struct pipe_shader_state aaline_fs;
357 struct aa_transform_context transform;
358 const uint newLen = tgsi_num_tokens(orig_fs->tokens) + NUM_NEW_TOKENS;
359
360 aaline_fs = *orig_fs; /* copy to init */
361 aaline_fs.tokens = tgsi_alloc_tokens(newLen);
362 if (aaline_fs.tokens == NULL)
363 return FALSE;
364
365 memset(&transform, 0, sizeof(transform));
366 transform.colorOutput = -1;
367 transform.maxInput = -1;
368 transform.maxGeneric = -1;
369 transform.colorTemp = -1;
370 transform.texTemp = -1;
371 transform.firstInstruction = TRUE;
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 if (aaline->driver_bind_sampler_states)
710 aaline->driver_bind_sampler_states(pipe, PIPE_SHADER_FRAGMENT, 0,
711 num_samplers, aaline->state.sampler);
712 else
713 aaline->driver_bind_fragment_sampler_states(pipe, num_samplers,
714 aaline->state.sampler);
715
716 aaline->driver_set_sampler_views(pipe, num_samplers, aaline->state.sampler_views);
717
718 /* Disable triangle culling, stippling, unfilled mode etc. */
719 r = draw_get_rasterizer_no_cull(draw, rast->scissor, rast->flatshade);
720 pipe->bind_rasterizer_state(pipe, r);
721
722 draw->suspend_flushing = FALSE;
723
724 /* now really draw first line */
725 stage->line = aaline_line;
726 stage->line(stage, header);
727 }
728
729
730 static void
731 aaline_flush(struct draw_stage *stage, unsigned flags)
732 {
733 struct draw_context *draw = stage->draw;
734 struct aaline_stage *aaline = aaline_stage(stage);
735 struct pipe_context *pipe = draw->pipe;
736
737 stage->line = aaline_first_line;
738 stage->next->flush( stage->next, flags );
739
740 /* restore original frag shader, texture, sampler state */
741 draw->suspend_flushing = TRUE;
742 aaline->driver_bind_fs_state(pipe, aaline->fs ? aaline->fs->driver_fs : NULL);
743
744 if (aaline->driver_bind_sampler_states)
745 aaline->driver_bind_sampler_states(pipe, PIPE_SHADER_FRAGMENT, 0,
746 aaline->num_samplers,
747 aaline->state.sampler);
748 else
749 aaline->driver_bind_fragment_sampler_states(pipe, aaline->num_samplers,
750 aaline->state.sampler);
751
752 aaline->driver_set_sampler_views(pipe,
753 aaline->num_sampler_views,
754 aaline->state.sampler_views);
755
756 /* restore original rasterizer state */
757 if (draw->rast_handle) {
758 pipe->bind_rasterizer_state(pipe, draw->rast_handle);
759 }
760
761 draw->suspend_flushing = FALSE;
762
763 draw_remove_extra_vertex_attribs(draw);
764 }
765
766
767 static void
768 aaline_reset_stipple_counter(struct draw_stage *stage)
769 {
770 stage->next->reset_stipple_counter( stage->next );
771 }
772
773
774 static void
775 aaline_destroy(struct draw_stage *stage)
776 {
777 struct aaline_stage *aaline = aaline_stage(stage);
778 struct pipe_context *pipe = stage->draw->pipe;
779 uint i;
780
781 for (i = 0; i < PIPE_MAX_SHADER_SAMPLER_VIEWS; i++) {
782 pipe_sampler_view_reference(&aaline->state.sampler_views[i], NULL);
783 }
784
785 if (aaline->sampler_cso)
786 pipe->delete_sampler_state(pipe, aaline->sampler_cso);
787
788 if (aaline->texture)
789 pipe_resource_reference(&aaline->texture, NULL);
790
791 if (aaline->sampler_view) {
792 pipe_sampler_view_reference(&aaline->sampler_view, NULL);
793 }
794
795 draw_free_temp_verts( stage );
796
797 /* restore the old entry points */
798 pipe->create_fs_state = aaline->driver_create_fs_state;
799 pipe->bind_fs_state = aaline->driver_bind_fs_state;
800 pipe->delete_fs_state = aaline->driver_delete_fs_state;
801
802 pipe->bind_sampler_states = aaline->driver_bind_sampler_states;
803 pipe->bind_fragment_sampler_states = aaline->driver_bind_fragment_sampler_states;
804 pipe->set_fragment_sampler_views = aaline->driver_set_sampler_views;
805
806 FREE( stage );
807 }
808
809
810 static struct aaline_stage *
811 draw_aaline_stage(struct draw_context *draw)
812 {
813 struct aaline_stage *aaline = CALLOC_STRUCT(aaline_stage);
814 if (aaline == NULL)
815 return NULL;
816
817 aaline->stage.draw = draw;
818 aaline->stage.name = "aaline";
819 aaline->stage.next = NULL;
820 aaline->stage.point = draw_pipe_passthrough_point;
821 aaline->stage.line = aaline_first_line;
822 aaline->stage.tri = draw_pipe_passthrough_tri;
823 aaline->stage.flush = aaline_flush;
824 aaline->stage.reset_stipple_counter = aaline_reset_stipple_counter;
825 aaline->stage.destroy = aaline_destroy;
826
827 if (!draw_alloc_temp_verts( &aaline->stage, 8 ))
828 goto fail;
829
830 return aaline;
831
832 fail:
833 if (aaline)
834 aaline->stage.destroy(&aaline->stage);
835
836 return NULL;
837 }
838
839
840 static struct aaline_stage *
841 aaline_stage_from_pipe(struct pipe_context *pipe)
842 {
843 struct draw_context *draw = (struct draw_context *) pipe->draw;
844
845 if (draw) {
846 return aaline_stage(draw->pipeline.aaline);
847 } else {
848 return NULL;
849 }
850 }
851
852
853 /**
854 * This function overrides the driver's create_fs_state() function and
855 * will typically be called by the state tracker.
856 */
857 static void *
858 aaline_create_fs_state(struct pipe_context *pipe,
859 const struct pipe_shader_state *fs)
860 {
861 struct aaline_stage *aaline = aaline_stage_from_pipe(pipe);
862 struct aaline_fragment_shader *aafs = NULL;
863
864 if (aaline == NULL)
865 return NULL;
866
867 aafs = CALLOC_STRUCT(aaline_fragment_shader);
868
869 if (aafs == NULL)
870 return NULL;
871
872 aafs->state.tokens = tgsi_dup_tokens(fs->tokens);
873
874 /* pass-through */
875 aafs->driver_fs = aaline->driver_create_fs_state(pipe, fs);
876
877 return aafs;
878 }
879
880
881 static void
882 aaline_bind_fs_state(struct pipe_context *pipe, void *fs)
883 {
884 struct aaline_stage *aaline = aaline_stage_from_pipe(pipe);
885 struct aaline_fragment_shader *aafs = (struct aaline_fragment_shader *) fs;
886
887 if (aaline == NULL) {
888 return;
889 }
890
891 /* save current */
892 aaline->fs = aafs;
893 /* pass-through */
894 aaline->driver_bind_fs_state(pipe, (aafs ? aafs->driver_fs : NULL));
895 }
896
897
898 static void
899 aaline_delete_fs_state(struct pipe_context *pipe, void *fs)
900 {
901 struct aaline_stage *aaline = aaline_stage_from_pipe(pipe);
902 struct aaline_fragment_shader *aafs = (struct aaline_fragment_shader *) fs;
903
904 if (aafs == NULL) {
905 return;
906 }
907
908 if (aaline != NULL) {
909 /* pass-through */
910 aaline->driver_delete_fs_state(pipe, aafs->driver_fs);
911
912 if (aafs->aaline_fs)
913 aaline->driver_delete_fs_state(pipe, aafs->aaline_fs);
914 }
915
916 FREE((void*)aafs->state.tokens);
917 FREE(aafs);
918 }
919
920
921 static void
922 aaline_bind_sampler_states(struct pipe_context *pipe, unsigned shader,
923 unsigned start, unsigned num, void **sampler)
924 {
925 struct aaline_stage *aaline = aaline_stage_from_pipe(pipe);
926
927 assert(start == 0);
928
929 if (aaline == NULL) {
930 return;
931 }
932
933 if (shader == PIPE_SHADER_FRAGMENT) {
934 /* save current */
935 memcpy(aaline->state.sampler, sampler, num * sizeof(void *));
936 aaline->num_samplers = num;
937 }
938
939 /* pass-through */
940 aaline->driver_bind_sampler_states(pipe, shader, start, num, sampler);
941 }
942
943
944 /* XXX deprecated / remove */
945 static void
946 aaline_bind_fragment_sampler_states(struct pipe_context *pipe,
947 unsigned num, void **sampler)
948 {
949 struct aaline_stage *aaline = aaline_stage_from_pipe(pipe);
950
951 if (aaline == NULL) {
952 return;
953 }
954
955 /* save current */
956 memcpy(aaline->state.sampler, sampler, num * sizeof(void *));
957 aaline->num_samplers = num;
958
959 /* pass-through */
960 aaline->driver_bind_fragment_sampler_states(pipe, num, sampler);
961 }
962
963
964 static void
965 aaline_set_sampler_views(struct pipe_context *pipe,
966 unsigned num,
967 struct pipe_sampler_view **views)
968 {
969 struct aaline_stage *aaline = aaline_stage_from_pipe(pipe);
970 uint i;
971
972 if (aaline == NULL) {
973 return;
974 }
975
976 /* save current */
977 for (i = 0; i < num; i++) {
978 pipe_sampler_view_reference(&aaline->state.sampler_views[i], views[i]);
979 }
980 for ( ; i < PIPE_MAX_SHADER_SAMPLER_VIEWS; i++) {
981 pipe_sampler_view_reference(&aaline->state.sampler_views[i], NULL);
982 }
983 aaline->num_sampler_views = num;
984
985 /* pass-through */
986 aaline->driver_set_sampler_views(pipe, num, views);
987 }
988
989
990 void
991 draw_aaline_prepare_outputs(struct draw_context *draw,
992 struct draw_stage *stage)
993 {
994 struct aaline_stage *aaline = aaline_stage(stage);
995 const struct pipe_rasterizer_state *rast = draw->rasterizer;
996
997 /* update vertex attrib info */
998 aaline->pos_slot = draw_current_shader_position_output(draw);;
999
1000 if (!rast->line_smooth)
1001 return;
1002
1003 /* allocate the extra post-transformed vertex attribute */
1004 aaline->tex_slot = draw_alloc_extra_vertex_attrib(draw,
1005 TGSI_SEMANTIC_GENERIC,
1006 aaline->fs->generic_attrib);
1007 }
1008
1009 /**
1010 * Called by drivers that want to install this AA line prim stage
1011 * into the draw module's pipeline. This will not be used if the
1012 * hardware has native support for AA lines.
1013 */
1014 boolean
1015 draw_install_aaline_stage(struct draw_context *draw, struct pipe_context *pipe)
1016 {
1017 struct aaline_stage *aaline;
1018
1019 pipe->draw = (void *) draw;
1020
1021 /*
1022 * Create / install AA line drawing / prim stage
1023 */
1024 aaline = draw_aaline_stage( draw );
1025 if (!aaline)
1026 goto fail;
1027
1028 /* create special texture, sampler state */
1029 if (!aaline_create_texture(aaline))
1030 goto fail;
1031
1032 if (!aaline_create_sampler(aaline))
1033 goto fail;
1034
1035 /* save original driver functions */
1036 aaline->driver_create_fs_state = pipe->create_fs_state;
1037 aaline->driver_bind_fs_state = pipe->bind_fs_state;
1038 aaline->driver_delete_fs_state = pipe->delete_fs_state;
1039
1040 aaline->driver_bind_sampler_states = pipe->bind_sampler_states;
1041 aaline->driver_bind_fragment_sampler_states = pipe->bind_fragment_sampler_states;
1042 aaline->driver_set_sampler_views = pipe->set_fragment_sampler_views;
1043
1044 /* override the driver's functions */
1045 pipe->create_fs_state = aaline_create_fs_state;
1046 pipe->bind_fs_state = aaline_bind_fs_state;
1047 pipe->delete_fs_state = aaline_delete_fs_state;
1048
1049 if (pipe->bind_sampler_states)
1050 pipe->bind_sampler_states = aaline_bind_sampler_states;
1051 else
1052 pipe->bind_fragment_sampler_states = aaline_bind_fragment_sampler_states;
1053
1054 pipe->set_fragment_sampler_views = aaline_set_sampler_views;
1055
1056 /* Install once everything is known to be OK:
1057 */
1058 draw->pipeline.aaline = &aaline->stage;
1059
1060 return TRUE;
1061
1062 fail:
1063 if (aaline)
1064 aaline->stage.destroy( &aaline->stage );
1065
1066 return FALSE;
1067 }