Merge branch 'gallium-embedded'
[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 struct pipe_context *pipe;
118 };
119
120
121
122 /**
123 * Subclass of tgsi_transform_context, used for transforming the
124 * user's fragment shader to add the special AA instructions.
125 */
126 struct aa_transform_context {
127 struct tgsi_transform_context base;
128 uint tempsUsed; /**< bitmask */
129 int colorOutput; /**< which output is the primary color */
130 uint samplersUsed; /**< bitfield of samplers used */
131 int freeSampler; /** an available sampler for the pstipple */
132 int maxInput, maxGeneric; /**< max input index found */
133 int colorTemp, texTemp; /**< temp registers */
134 boolean firstInstruction;
135 };
136
137
138 /**
139 * TGSI declaration transform callback.
140 * Look for a free sampler, a free input attrib, and two free temp regs.
141 */
142 static void
143 aa_transform_decl(struct tgsi_transform_context *ctx,
144 struct tgsi_full_declaration *decl)
145 {
146 struct aa_transform_context *aactx = (struct aa_transform_context *) ctx;
147
148 if (decl->Declaration.File == TGSI_FILE_OUTPUT &&
149 decl->Semantic.Name == TGSI_SEMANTIC_COLOR &&
150 decl->Semantic.Index == 0) {
151 aactx->colorOutput = decl->Range.First;
152 }
153 else if (decl->Declaration.File == TGSI_FILE_SAMPLER) {
154 uint i;
155 for (i = decl->Range.First;
156 i <= decl->Range.Last; i++) {
157 aactx->samplersUsed |= 1 << i;
158 }
159 }
160 else if (decl->Declaration.File == TGSI_FILE_INPUT) {
161 if ((int) decl->Range.Last > aactx->maxInput)
162 aactx->maxInput = decl->Range.Last;
163 if (decl->Semantic.Name == TGSI_SEMANTIC_GENERIC &&
164 (int) decl->Semantic.Index > aactx->maxGeneric) {
165 aactx->maxGeneric = decl->Semantic.Index;
166 }
167 }
168 else if (decl->Declaration.File == TGSI_FILE_TEMPORARY) {
169 uint i;
170 for (i = decl->Range.First;
171 i <= decl->Range.Last; i++) {
172 aactx->tempsUsed |= (1 << i);
173 }
174 }
175
176 ctx->emit_declaration(ctx, decl);
177 }
178
179
180 /**
181 * Find the lowest zero bit in the given word, or -1 if bitfield is all ones.
182 */
183 static int
184 free_bit(uint bitfield)
185 {
186 return ffs(~bitfield) - 1;
187 }
188
189
190 /**
191 * TGSI instruction transform callback.
192 * Replace writes to result.color w/ a temp reg.
193 * Upon END instruction, insert texture sampling code for antialiasing.
194 */
195 static void
196 aa_transform_inst(struct tgsi_transform_context *ctx,
197 struct tgsi_full_instruction *inst)
198 {
199 struct aa_transform_context *aactx = (struct aa_transform_context *) ctx;
200
201 if (aactx->firstInstruction) {
202 /* emit our new declarations before the first instruction */
203
204 struct tgsi_full_declaration decl;
205 uint i;
206
207 /* find free sampler */
208 aactx->freeSampler = free_bit(aactx->samplersUsed);
209 if (aactx->freeSampler >= PIPE_MAX_SAMPLERS)
210 aactx->freeSampler = PIPE_MAX_SAMPLERS - 1;
211
212 /* find two free temp regs */
213 for (i = 0; i < 32; i++) {
214 if ((aactx->tempsUsed & (1 << i)) == 0) {
215 /* found a free temp */
216 if (aactx->colorTemp < 0)
217 aactx->colorTemp = i;
218 else if (aactx->texTemp < 0)
219 aactx->texTemp = i;
220 else
221 break;
222 }
223 }
224 assert(aactx->colorTemp >= 0);
225 assert(aactx->texTemp >= 0);
226
227 /* declare new generic input/texcoord */
228 decl = tgsi_default_full_declaration();
229 decl.Declaration.File = TGSI_FILE_INPUT;
230 /* XXX this could be linear... */
231 decl.Declaration.Interpolate = TGSI_INTERPOLATE_PERSPECTIVE;
232 decl.Declaration.Semantic = 1;
233 decl.Semantic.Name = TGSI_SEMANTIC_GENERIC;
234 decl.Semantic.Index = aactx->maxGeneric + 1;
235 decl.Range.First =
236 decl.Range.Last = aactx->maxInput + 1;
237 ctx->emit_declaration(ctx, &decl);
238
239 /* declare new sampler */
240 decl = tgsi_default_full_declaration();
241 decl.Declaration.File = TGSI_FILE_SAMPLER;
242 decl.Range.First =
243 decl.Range.Last = aactx->freeSampler;
244 ctx->emit_declaration(ctx, &decl);
245
246 /* declare new temp regs */
247 decl = tgsi_default_full_declaration();
248 decl.Declaration.File = TGSI_FILE_TEMPORARY;
249 decl.Range.First =
250 decl.Range.Last = aactx->texTemp;
251 ctx->emit_declaration(ctx, &decl);
252
253 decl = tgsi_default_full_declaration();
254 decl.Declaration.File = TGSI_FILE_TEMPORARY;
255 decl.Range.First =
256 decl.Range.Last = aactx->colorTemp;
257 ctx->emit_declaration(ctx, &decl);
258
259 aactx->firstInstruction = FALSE;
260 }
261
262 if (inst->Instruction.Opcode == TGSI_OPCODE_END &&
263 aactx->colorOutput != -1) {
264 struct tgsi_full_instruction newInst;
265
266 /* TEX */
267 newInst = tgsi_default_full_instruction();
268 newInst.Instruction.Opcode = TGSI_OPCODE_TEX;
269 newInst.Instruction.NumDstRegs = 1;
270 newInst.Dst[0].Register.File = TGSI_FILE_TEMPORARY;
271 newInst.Dst[0].Register.Index = aactx->texTemp;
272 newInst.Instruction.NumSrcRegs = 2;
273 newInst.Instruction.Texture = TRUE;
274 newInst.Texture.Texture = TGSI_TEXTURE_2D;
275 newInst.Src[0].Register.File = TGSI_FILE_INPUT;
276 newInst.Src[0].Register.Index = aactx->maxInput + 1;
277 newInst.Src[1].Register.File = TGSI_FILE_SAMPLER;
278 newInst.Src[1].Register.Index = aactx->freeSampler;
279
280 ctx->emit_instruction(ctx, &newInst);
281
282 /* MOV rgb */
283 newInst = tgsi_default_full_instruction();
284 newInst.Instruction.Opcode = TGSI_OPCODE_MOV;
285 newInst.Instruction.NumDstRegs = 1;
286 newInst.Dst[0].Register.File = TGSI_FILE_OUTPUT;
287 newInst.Dst[0].Register.Index = aactx->colorOutput;
288 newInst.Dst[0].Register.WriteMask = TGSI_WRITEMASK_XYZ;
289 newInst.Instruction.NumSrcRegs = 1;
290 newInst.Src[0].Register.File = TGSI_FILE_TEMPORARY;
291 newInst.Src[0].Register.Index = aactx->colorTemp;
292 ctx->emit_instruction(ctx, &newInst);
293
294 /* MUL alpha */
295 newInst = tgsi_default_full_instruction();
296 newInst.Instruction.Opcode = TGSI_OPCODE_MUL;
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_W;
301 newInst.Instruction.NumSrcRegs = 2;
302 newInst.Src[0].Register.File = TGSI_FILE_TEMPORARY;
303 newInst.Src[0].Register.Index = aactx->colorTemp;
304 newInst.Src[1].Register.File = TGSI_FILE_TEMPORARY;
305 newInst.Src[1].Register.Index = aactx->texTemp;
306 ctx->emit_instruction(ctx, &newInst);
307
308 /* END */
309 newInst = tgsi_default_full_instruction();
310 newInst.Instruction.Opcode = TGSI_OPCODE_END;
311 newInst.Instruction.NumDstRegs = 0;
312 newInst.Instruction.NumSrcRegs = 0;
313 ctx->emit_instruction(ctx, &newInst);
314 }
315 else {
316 /* Not an END instruction.
317 * Look for writes to result.color and replace with colorTemp reg.
318 */
319 uint i;
320
321 for (i = 0; i < inst->Instruction.NumDstRegs; i++) {
322 struct tgsi_full_dst_register *dst = &inst->Dst[i];
323 if (dst->Register.File == TGSI_FILE_OUTPUT &&
324 dst->Register.Index == aactx->colorOutput) {
325 dst->Register.File = TGSI_FILE_TEMPORARY;
326 dst->Register.Index = aactx->colorTemp;
327 }
328 }
329
330 ctx->emit_instruction(ctx, inst);
331 }
332 }
333
334
335 /**
336 * Generate the frag shader we'll use for drawing AA lines.
337 * This will be the user's shader plus some texture/modulate instructions.
338 */
339 static boolean
340 generate_aaline_fs(struct aaline_stage *aaline)
341 {
342 const struct pipe_shader_state *orig_fs = &aaline->fs->state;
343 struct pipe_shader_state aaline_fs;
344 struct aa_transform_context transform;
345 const uint newLen = tgsi_num_tokens(orig_fs->tokens) + NUM_NEW_TOKENS;
346
347 aaline_fs = *orig_fs; /* copy to init */
348 aaline_fs.tokens = tgsi_alloc_tokens(newLen);
349 if (aaline_fs.tokens == NULL)
350 return FALSE;
351
352 memset(&transform, 0, sizeof(transform));
353 transform.colorOutput = -1;
354 transform.maxInput = -1;
355 transform.maxGeneric = -1;
356 transform.colorTemp = -1;
357 transform.texTemp = -1;
358 transform.firstInstruction = TRUE;
359 transform.base.transform_instruction = aa_transform_inst;
360 transform.base.transform_declaration = aa_transform_decl;
361
362 tgsi_transform_shader(orig_fs->tokens,
363 (struct tgsi_token *) aaline_fs.tokens,
364 newLen, &transform.base);
365
366 #if 0 /* DEBUG */
367 tgsi_dump(orig_fs->tokens, 0);
368 tgsi_dump(aaline_fs.tokens, 0);
369 #endif
370
371 aaline->fs->sampler_unit = transform.freeSampler;
372
373 aaline->fs->aaline_fs
374 = aaline->driver_create_fs_state(aaline->pipe, &aaline_fs);
375 if (aaline->fs->aaline_fs == NULL)
376 goto fail;
377
378 aaline->fs->generic_attrib = transform.maxGeneric + 1;
379 FREE((void *)aaline_fs.tokens);
380 return TRUE;
381
382 fail:
383 FREE((void *)aaline_fs.tokens);
384 return FALSE;
385 }
386
387
388 /**
389 * Create the texture map we'll use for antialiasing the lines.
390 */
391 static boolean
392 aaline_create_texture(struct aaline_stage *aaline)
393 {
394 struct pipe_context *pipe = aaline->pipe;
395 struct pipe_screen *screen = pipe->screen;
396 struct pipe_texture texTemp;
397 uint level;
398
399 memset(&texTemp, 0, sizeof(texTemp));
400 texTemp.target = PIPE_TEXTURE_2D;
401 texTemp.format = PIPE_FORMAT_A8_UNORM; /* XXX verify supported by driver! */
402 texTemp.last_level = MAX_TEXTURE_LEVEL;
403 texTemp.width0 = 1 << MAX_TEXTURE_LEVEL;
404 texTemp.height0 = 1 << MAX_TEXTURE_LEVEL;
405 texTemp.depth0 = 1;
406
407 aaline->texture = screen->texture_create(screen, &texTemp);
408 if (!aaline->texture)
409 return FALSE;
410
411 /* Fill in mipmap images.
412 * Basically each level is solid opaque, except for the outermost
413 * texels which are zero. Special case the 1x1 and 2x2 levels.
414 */
415 for (level = 0; level <= MAX_TEXTURE_LEVEL; level++) {
416 struct pipe_transfer *transfer;
417 const uint size = u_minify(aaline->texture->width0, level);
418 ubyte *data;
419 uint i, j;
420
421 assert(aaline->texture->width0 == aaline->texture->height0);
422
423 /* This texture is new, no need to flush.
424 */
425 transfer = screen->get_tex_transfer(screen, aaline->texture, 0, level, 0,
426 PIPE_TRANSFER_WRITE, 0, 0, size, size);
427 data = screen->transfer_map(screen, transfer);
428 if (data == NULL)
429 return FALSE;
430
431 for (i = 0; i < size; i++) {
432 for (j = 0; j < size; j++) {
433 ubyte d;
434 if (size == 1) {
435 d = 255;
436 }
437 else if (size == 2) {
438 d = 200; /* tuneable */
439 }
440 else if (i == 0 || j == 0 || i == size - 1 || j == size - 1) {
441 d = 0;
442 }
443 else {
444 d = 255;
445 }
446 data[i * transfer->stride + j] = d;
447 }
448 }
449
450 /* unmap */
451 screen->transfer_unmap(screen, transfer);
452 screen->tex_transfer_destroy(transfer);
453 }
454 return TRUE;
455 }
456
457
458 /**
459 * Create the sampler CSO that'll be used for antialiasing.
460 * By using a mipmapped texture, we don't have to generate a different
461 * texture image for each line size.
462 */
463 static boolean
464 aaline_create_sampler(struct aaline_stage *aaline)
465 {
466 struct pipe_sampler_state sampler;
467 struct pipe_context *pipe = aaline->pipe;
468
469 memset(&sampler, 0, sizeof(sampler));
470 sampler.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
471 sampler.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
472 sampler.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
473 sampler.min_mip_filter = PIPE_TEX_MIPFILTER_LINEAR;
474 sampler.min_img_filter = PIPE_TEX_FILTER_LINEAR;
475 sampler.mag_img_filter = PIPE_TEX_FILTER_LINEAR;
476 sampler.normalized_coords = 1;
477 sampler.min_lod = 0.0f;
478 sampler.max_lod = MAX_TEXTURE_LEVEL;
479
480 aaline->sampler_cso = pipe->create_sampler_state(pipe, &sampler);
481 if (aaline->sampler_cso == NULL)
482 return FALSE;
483
484 return TRUE;
485 }
486
487
488 /**
489 * When we're about to draw our first AA line in a batch, this function is
490 * called to tell the driver to bind our modified fragment shader.
491 */
492 static boolean
493 bind_aaline_fragment_shader(struct aaline_stage *aaline)
494 {
495 struct draw_context *draw = aaline->stage.draw;
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(aaline->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 = aaline->pipe;
643 uint num_samplers;
644
645 assert(draw->rasterizer->line_smooth);
646
647 if (draw->rasterizer->line_width <= 3.0)
648 aaline->half_line_width = 1.5f;
649 else
650 aaline->half_line_width = 0.5f * draw->rasterizer->line_width;
651
652 /*
653 * Bind (generate) our fragprog, sampler and texture
654 */
655 if (!bind_aaline_fragment_shader(aaline)) {
656 stage->line = draw_pipe_passthrough_line;
657 stage->line(stage, header);
658 return;
659 }
660
661 /* update vertex attrib info */
662 aaline->tex_slot = draw_current_shader_outputs(draw);
663 aaline->pos_slot = draw_current_shader_position_output(draw);;
664
665 /* advertise the extra post-transformed vertex attribute */
666 draw->extra_shader_outputs.semantic_name = TGSI_SEMANTIC_GENERIC;
667 draw->extra_shader_outputs.semantic_index = aaline->fs->generic_attrib;
668 draw->extra_shader_outputs.slot = aaline->tex_slot;
669
670 /* how many samplers? */
671 /* we'll use sampler/texture[pstip->sampler_unit] for the stipple */
672 num_samplers = MAX2(aaline->num_textures, aaline->num_samplers);
673 num_samplers = MAX2(num_samplers, aaline->fs->sampler_unit + 1);
674
675 aaline->state.sampler[aaline->fs->sampler_unit] = aaline->sampler_cso;
676 pipe_texture_reference(&aaline->state.texture[aaline->fs->sampler_unit],
677 aaline->texture);
678
679 draw->suspend_flushing = TRUE;
680 aaline->driver_bind_sampler_states(pipe, num_samplers, aaline->state.sampler);
681 aaline->driver_set_sampler_textures(pipe, num_samplers, aaline->state.texture);
682 draw->suspend_flushing = FALSE;
683
684 /* now really draw first line */
685 stage->line = aaline_line;
686 stage->line(stage, header);
687 }
688
689
690 static void
691 aaline_flush(struct draw_stage *stage, unsigned flags)
692 {
693 struct draw_context *draw = stage->draw;
694 struct aaline_stage *aaline = aaline_stage(stage);
695 struct pipe_context *pipe = aaline->pipe;
696
697 stage->line = aaline_first_line;
698 stage->next->flush( stage->next, flags );
699
700 /* restore original frag shader, texture, sampler state */
701 draw->suspend_flushing = TRUE;
702 aaline->driver_bind_fs_state(pipe, aaline->fs->driver_fs);
703 aaline->driver_bind_sampler_states(pipe, aaline->num_samplers,
704 aaline->state.sampler);
705 aaline->driver_set_sampler_textures(pipe, aaline->num_textures,
706 aaline->state.texture);
707 draw->suspend_flushing = FALSE;
708
709 draw->extra_shader_outputs.slot = 0;
710 }
711
712
713 static void
714 aaline_reset_stipple_counter(struct draw_stage *stage)
715 {
716 stage->next->reset_stipple_counter( stage->next );
717 }
718
719
720 static void
721 aaline_destroy(struct draw_stage *stage)
722 {
723 struct aaline_stage *aaline = aaline_stage(stage);
724 uint i;
725
726 for (i = 0; i < PIPE_MAX_SAMPLERS; i++) {
727 pipe_texture_reference(&aaline->state.texture[i], NULL);
728 }
729
730 if (aaline->sampler_cso)
731 aaline->pipe->delete_sampler_state(aaline->pipe, aaline->sampler_cso);
732
733 if (aaline->texture)
734 pipe_texture_reference(&aaline->texture, NULL);
735
736 draw_free_temp_verts( stage );
737
738 FREE( stage );
739 }
740
741
742 static struct aaline_stage *
743 draw_aaline_stage(struct draw_context *draw)
744 {
745 struct aaline_stage *aaline = CALLOC_STRUCT(aaline_stage);
746 if (aaline == NULL)
747 return NULL;
748
749 if (!draw_alloc_temp_verts( &aaline->stage, 8 ))
750 goto fail;
751
752 aaline->stage.draw = draw;
753 aaline->stage.name = "aaline";
754 aaline->stage.next = NULL;
755 aaline->stage.point = draw_pipe_passthrough_point;
756 aaline->stage.line = aaline_first_line;
757 aaline->stage.tri = draw_pipe_passthrough_tri;
758 aaline->stage.flush = aaline_flush;
759 aaline->stage.reset_stipple_counter = aaline_reset_stipple_counter;
760 aaline->stage.destroy = aaline_destroy;
761
762 return aaline;
763
764 fail:
765 if (aaline)
766 aaline_destroy(&aaline->stage);
767
768 return NULL;
769 }
770
771
772 static struct aaline_stage *
773 aaline_stage_from_pipe(struct pipe_context *pipe)
774 {
775 struct draw_context *draw = (struct draw_context *) pipe->draw;
776 return aaline_stage(draw->pipeline.aaline);
777 }
778
779
780 /**
781 * This function overrides the driver's create_fs_state() function and
782 * will typically be called by the state tracker.
783 */
784 static void *
785 aaline_create_fs_state(struct pipe_context *pipe,
786 const struct pipe_shader_state *fs)
787 {
788 struct aaline_stage *aaline = aaline_stage_from_pipe(pipe);
789 struct aaline_fragment_shader *aafs = CALLOC_STRUCT(aaline_fragment_shader);
790 if (aafs == NULL)
791 return NULL;
792
793 aafs->state = *fs;
794
795 /* pass-through */
796 aafs->driver_fs = aaline->driver_create_fs_state(aaline->pipe, fs);
797
798 return aafs;
799 }
800
801
802 static void
803 aaline_bind_fs_state(struct pipe_context *pipe, void *fs)
804 {
805 struct aaline_stage *aaline = aaline_stage_from_pipe(pipe);
806 struct aaline_fragment_shader *aafs = (struct aaline_fragment_shader *) fs;
807
808 /* save current */
809 aaline->fs = aafs;
810 /* pass-through */
811 aaline->driver_bind_fs_state(aaline->pipe,
812 (aafs ? aafs->driver_fs : NULL));
813 }
814
815
816 static void
817 aaline_delete_fs_state(struct pipe_context *pipe, void *fs)
818 {
819 struct aaline_stage *aaline = aaline_stage_from_pipe(pipe);
820 struct aaline_fragment_shader *aafs = (struct aaline_fragment_shader *) fs;
821 /* pass-through */
822 aaline->driver_delete_fs_state(aaline->pipe, aafs->driver_fs);
823
824 if (aafs->aaline_fs)
825 aaline->driver_delete_fs_state(aaline->pipe, aafs->aaline_fs);
826
827 FREE(aafs);
828 }
829
830
831 static void
832 aaline_bind_sampler_states(struct pipe_context *pipe,
833 unsigned num, void **sampler)
834 {
835 struct aaline_stage *aaline = aaline_stage_from_pipe(pipe);
836
837 /* save current */
838 memcpy(aaline->state.sampler, sampler, num * sizeof(void *));
839 aaline->num_samplers = num;
840
841 /* pass-through */
842 aaline->driver_bind_sampler_states(aaline->pipe, num, sampler);
843 }
844
845
846 static void
847 aaline_set_sampler_textures(struct pipe_context *pipe,
848 unsigned num, struct pipe_texture **texture)
849 {
850 struct aaline_stage *aaline = aaline_stage_from_pipe(pipe);
851 uint i;
852
853 /* save current */
854 for (i = 0; i < num; i++) {
855 pipe_texture_reference(&aaline->state.texture[i], texture[i]);
856 }
857 for ( ; i < PIPE_MAX_SAMPLERS; i++) {
858 pipe_texture_reference(&aaline->state.texture[i], NULL);
859 }
860 aaline->num_textures = num;
861
862 /* pass-through */
863 aaline->driver_set_sampler_textures(aaline->pipe, num, texture);
864 }
865
866
867 /**
868 * Called by drivers that want to install this AA line prim stage
869 * into the draw module's pipeline. This will not be used if the
870 * hardware has native support for AA lines.
871 */
872 boolean
873 draw_install_aaline_stage(struct draw_context *draw, struct pipe_context *pipe)
874 {
875 struct aaline_stage *aaline;
876
877 pipe->draw = (void *) draw;
878
879 /*
880 * Create / install AA line drawing / prim stage
881 */
882 aaline = draw_aaline_stage( draw );
883 if (!aaline)
884 goto fail;
885
886 aaline->pipe = pipe;
887
888 /* create special texture, sampler state */
889 if (!aaline_create_texture(aaline))
890 goto fail;
891
892 if (!aaline_create_sampler(aaline))
893 goto fail;
894
895 /* save original driver functions */
896 aaline->driver_create_fs_state = pipe->create_fs_state;
897 aaline->driver_bind_fs_state = pipe->bind_fs_state;
898 aaline->driver_delete_fs_state = pipe->delete_fs_state;
899
900 aaline->driver_bind_sampler_states = pipe->bind_fragment_sampler_states;
901 aaline->driver_set_sampler_textures = pipe->set_fragment_sampler_textures;
902
903 /* override the driver's functions */
904 pipe->create_fs_state = aaline_create_fs_state;
905 pipe->bind_fs_state = aaline_bind_fs_state;
906 pipe->delete_fs_state = aaline_delete_fs_state;
907
908 pipe->bind_fragment_sampler_states = aaline_bind_sampler_states;
909 pipe->set_fragment_sampler_textures = aaline_set_sampler_textures;
910
911 /* Install once everything is known to be OK:
912 */
913 draw->pipeline.aaline = &aaline->stage;
914
915 return TRUE;
916
917 fail:
918 if (aaline)
919 aaline->stage.destroy( &aaline->stage );
920
921 return FALSE;
922 }